blob: 711616fc4d8287b58f8450ba8b241f6284fbe7c9 [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002// 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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000033#include "bootstrapper.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000034#include "jsregexp.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000035#include "isolate.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
37
38namespace v8 {
39namespace internal {
40
41#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +000042
43void ToNumberStub::Generate(MacroAssembler* masm) {
44 // The ToNumber stub takes one argument in eax.
45 NearLabel check_heap_number, call_builtin;
46 __ test(eax, Immediate(kSmiTagMask));
47 __ j(not_zero, &check_heap_number);
48 __ ret(0);
49
50 __ bind(&check_heap_number);
51 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000052 Factory* factory = masm->isolate()->factory();
53 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org7a392b32011-01-31 11:30:36 +000054 __ j(not_equal, &call_builtin);
55 __ ret(0);
56
57 __ bind(&call_builtin);
58 __ pop(ecx); // Pop return address.
59 __ push(eax);
60 __ push(ecx); // Push return address.
61 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
62}
63
64
ricow@chromium.org65fae842010-08-25 15:26:24 +000065void FastNewClosureStub::Generate(MacroAssembler* masm) {
66 // Create a new closure from the given function info in new
67 // space. Set the context to the current context in esi.
68 Label gc;
69 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
70
71 // Get the function info from the stack.
72 __ mov(edx, Operand(esp, 1 * kPointerSize));
73
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 int map_index = strict_mode_ == kStrictMode
75 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
76 : Context::FUNCTION_MAP_INDEX;
77
ricow@chromium.org65fae842010-08-25 15:26:24 +000078 // Compute the function map in the current global context and set that
79 // as the map of the allocated object.
80 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
81 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 __ mov(ecx, Operand(ecx, Context::SlotOffset(map_index)));
ricow@chromium.org65fae842010-08-25 15:26:24 +000083 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
84
85 // Initialize the rest of the function. We don't have to update the
86 // write barrier because the allocated object is in new space.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000087 Factory* factory = masm->isolate()->factory();
88 __ mov(ebx, Immediate(factory->empty_fixed_array()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000089 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
90 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
91 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000092 Immediate(factory->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000093 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
94 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
95 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000096 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000097 Immediate(factory->undefined_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000098
99 // Initialize the code pointer in the function to be the one
100 // found in the shared function info object.
101 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
102 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
103 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
104
105 // Return and remove the on-stack parameter.
106 __ ret(1 * kPointerSize);
107
108 // Create a new closure through the slower runtime call.
109 __ bind(&gc);
110 __ pop(ecx); // Temporarily remove return address.
111 __ pop(edx);
112 __ push(esi);
113 __ push(edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000114 __ push(Immediate(factory->false_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000115 __ push(ecx); // Restore return address.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000116 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000117}
118
119
120void FastNewContextStub::Generate(MacroAssembler* masm) {
121 // Try to allocate the context in new space.
122 Label gc;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000123 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
124 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000125 eax, ebx, ecx, &gc, TAG_OBJECT);
126
127 // Get the function from the stack.
128 __ mov(ecx, Operand(esp, 1 * kPointerSize));
129
130 // Setup the object header.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000131 Factory* factory = masm->isolate()->factory();
132 __ mov(FieldOperand(eax, HeapObject::kMapOffset), factory->context_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000133 __ mov(FieldOperand(eax, Context::kLengthOffset),
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000134 Immediate(Smi::FromInt(length)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135
136 // Setup the fixed slots.
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000137 __ Set(ebx, Immediate(0)); // Set to NULL.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000138 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
139 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
140 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
141 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
142
143 // Copy the global object from the surrounding context. We go through the
144 // context in the function (ecx) to match the allocation behavior we have
145 // in the runtime system (see Heap::AllocateFunctionContext).
146 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
147 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
148 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
149
150 // Initialize the rest of the slots to undefined.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000151 __ mov(ebx, factory->undefined_value());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000152 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000153 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
154 }
155
156 // Return and remove the on-stack parameter.
157 __ mov(esi, Operand(eax));
158 __ ret(1 * kPointerSize);
159
160 // Need to collect. Call into runtime system.
161 __ bind(&gc);
162 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
163}
164
165
166void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
167 // Stack layout on entry:
168 //
169 // [esp + kPointerSize]: constant elements.
170 // [esp + (2 * kPointerSize)]: literal index.
171 // [esp + (3 * kPointerSize)]: literals array.
172
173 // All sizes here are multiples of kPointerSize.
174 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
175 int size = JSArray::kSize + elements_size;
176
177 // Load boilerplate object into ecx and check if we need to create a
178 // boilerplate.
179 Label slow_case;
180 __ mov(ecx, Operand(esp, 3 * kPointerSize));
181 __ mov(eax, Operand(esp, 2 * kPointerSize));
182 STATIC_ASSERT(kPointerSize == 4);
183 STATIC_ASSERT(kSmiTagSize == 1);
184 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000185 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
186 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000187 Factory* factory = masm->isolate()->factory();
188 __ cmp(ecx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000189 __ j(equal, &slow_case);
190
191 if (FLAG_debug_code) {
192 const char* message;
193 Handle<Map> expected_map;
194 if (mode_ == CLONE_ELEMENTS) {
195 message = "Expected (writable) fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000196 expected_map = factory->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000197 } else {
198 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
199 message = "Expected copy-on-write fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000200 expected_map = factory->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000201 }
202 __ push(ecx);
203 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
204 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
205 __ Assert(equal, message);
206 __ pop(ecx);
207 }
208
209 // Allocate both the JS array and the elements array in one big
210 // allocation. This avoids multiple limit checks.
211 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
212
213 // Copy the JS array part.
214 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
215 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
216 __ mov(ebx, FieldOperand(ecx, i));
217 __ mov(FieldOperand(eax, i), ebx);
218 }
219 }
220
221 if (length_ > 0) {
222 // Get hold of the elements array of the boilerplate and setup the
223 // elements pointer in the resulting object.
224 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
225 __ lea(edx, Operand(eax, JSArray::kSize));
226 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
227
228 // Copy the elements array.
229 for (int i = 0; i < elements_size; i += kPointerSize) {
230 __ mov(ebx, FieldOperand(ecx, i));
231 __ mov(FieldOperand(edx, i), ebx);
232 }
233 }
234
235 // Return and remove the on-stack parameters.
236 __ ret(3 * kPointerSize);
237
238 __ bind(&slow_case);
239 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
240}
241
242
243// NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
244void ToBooleanStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000245 NearLabel false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000246 __ mov(eax, Operand(esp, 1 * kPointerSize));
247
248 // 'null' => false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000249 Factory* factory = masm->isolate()->factory();
250 __ cmp(eax, factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000251 __ j(equal, &false_result);
252
253 // Get the map and type of the heap object.
254 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
255 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
256
257 // Undetectable => false.
258 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
259 1 << Map::kIsUndetectable);
260 __ j(not_zero, &false_result);
261
262 // JavaScript object => true.
263 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
264 __ j(above_equal, &true_result);
265
266 // String value => false iff empty.
267 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
268 __ j(above_equal, &not_string);
269 STATIC_ASSERT(kSmiTag == 0);
270 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
271 __ j(zero, &false_result);
272 __ jmp(&true_result);
273
274 __ bind(&not_string);
275 // HeapNumber => false iff +0, -0, or NaN.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000276 __ cmp(edx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000277 __ j(not_equal, &true_result);
278 __ fldz();
279 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
280 __ FCmp();
281 __ j(zero, &false_result);
282 // Fall through to |true_result|.
283
284 // Return 1/0 for true/false in eax.
285 __ bind(&true_result);
286 __ mov(eax, 1);
287 __ ret(1 * kPointerSize);
288 __ bind(&false_result);
289 __ mov(eax, 0);
290 __ ret(1 * kPointerSize);
291}
292
293
294const char* GenericBinaryOpStub::GetName() {
295 if (name_ != NULL) return name_;
296 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000297 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
298 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000299 if (name_ == NULL) return "OOM";
300 const char* op_name = Token::Name(op_);
301 const char* overwrite_name;
302 switch (mode_) {
303 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
304 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
305 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
306 default: overwrite_name = "UnknownOverwrite"; break;
307 }
308
309 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
310 "GenericBinaryOpStub_%s_%s%s_%s%s_%s_%s",
311 op_name,
312 overwrite_name,
313 (flags_ & NO_SMI_CODE_IN_STUB) ? "_NoSmiInStub" : "",
314 args_in_registers_ ? "RegArgs" : "StackArgs",
315 args_reversed_ ? "_R" : "",
316 static_operands_type_.ToString(),
317 BinaryOpIC::GetName(runtime_operands_type_));
318 return name_;
319}
320
321
322void GenericBinaryOpStub::GenerateCall(
323 MacroAssembler* masm,
324 Register left,
325 Register right) {
326 if (!ArgsInRegistersSupported()) {
327 // Pass arguments on the stack.
328 __ push(left);
329 __ push(right);
330 } else {
331 // The calling convention with registers is left in edx and right in eax.
332 Register left_arg = edx;
333 Register right_arg = eax;
334 if (!(left.is(left_arg) && right.is(right_arg))) {
335 if (left.is(right_arg) && right.is(left_arg)) {
336 if (IsOperationCommutative()) {
337 SetArgsReversed();
338 } else {
339 __ xchg(left, right);
340 }
341 } else if (left.is(left_arg)) {
342 __ mov(right_arg, right);
343 } else if (right.is(right_arg)) {
344 __ mov(left_arg, left);
345 } else if (left.is(right_arg)) {
346 if (IsOperationCommutative()) {
347 __ mov(left_arg, right);
348 SetArgsReversed();
349 } else {
350 // Order of moves important to avoid destroying left argument.
351 __ mov(left_arg, left);
352 __ mov(right_arg, right);
353 }
354 } else if (right.is(left_arg)) {
355 if (IsOperationCommutative()) {
356 __ mov(right_arg, left);
357 SetArgsReversed();
358 } else {
359 // Order of moves important to avoid destroying right argument.
360 __ mov(right_arg, right);
361 __ mov(left_arg, left);
362 }
363 } else {
364 // Order of moves is not important.
365 __ mov(left_arg, left);
366 __ mov(right_arg, right);
367 }
368 }
369
370 // Update flags to indicate that arguments are in registers.
371 SetArgsInRegisters();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000372 __ IncrementCounter(
373 masm->isolate()->counters()->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000374 }
375
376 // Call the stub.
377 __ CallStub(this);
378}
379
380
381void GenericBinaryOpStub::GenerateCall(
382 MacroAssembler* masm,
383 Register left,
384 Smi* right) {
385 if (!ArgsInRegistersSupported()) {
386 // Pass arguments on the stack.
387 __ push(left);
388 __ push(Immediate(right));
389 } else {
390 // The calling convention with registers is left in edx and right in eax.
391 Register left_arg = edx;
392 Register right_arg = eax;
393 if (left.is(left_arg)) {
394 __ mov(right_arg, Immediate(right));
395 } else if (left.is(right_arg) && IsOperationCommutative()) {
396 __ mov(left_arg, Immediate(right));
397 SetArgsReversed();
398 } else {
399 // For non-commutative operations, left and right_arg might be
400 // the same register. Therefore, the order of the moves is
401 // important here in order to not overwrite left before moving
402 // it to left_arg.
403 __ mov(left_arg, left);
404 __ mov(right_arg, Immediate(right));
405 }
406
407 // Update flags to indicate that arguments are in registers.
408 SetArgsInRegisters();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000409 __ IncrementCounter(
410 masm->isolate()->counters()->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000411 }
412
413 // Call the stub.
414 __ CallStub(this);
415}
416
417
418void GenericBinaryOpStub::GenerateCall(
419 MacroAssembler* masm,
420 Smi* left,
421 Register right) {
422 if (!ArgsInRegistersSupported()) {
423 // Pass arguments on the stack.
424 __ push(Immediate(left));
425 __ push(right);
426 } else {
427 // The calling convention with registers is left in edx and right in eax.
428 Register left_arg = edx;
429 Register right_arg = eax;
430 if (right.is(right_arg)) {
431 __ mov(left_arg, Immediate(left));
432 } else if (right.is(left_arg) && IsOperationCommutative()) {
433 __ mov(right_arg, Immediate(left));
434 SetArgsReversed();
435 } else {
436 // For non-commutative operations, right and left_arg might be
437 // the same register. Therefore, the order of the moves is
438 // important here in order to not overwrite right before moving
439 // it to right_arg.
440 __ mov(right_arg, right);
441 __ mov(left_arg, Immediate(left));
442 }
443 // Update flags to indicate that arguments are in registers.
444 SetArgsInRegisters();
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000445 Counters* counters = masm->isolate()->counters();
446 __ IncrementCounter(counters->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000447 }
448
449 // Call the stub.
450 __ CallStub(this);
451}
452
453
454class FloatingPointHelper : public AllStatic {
455 public:
456
457 enum ArgLocation {
458 ARGS_ON_STACK,
459 ARGS_IN_REGISTERS
460 };
461
462 // Code pattern for loading a floating point value. Input value must
463 // be either a smi or a heap number object (fp value). Requirements:
464 // operand in register number. Returns operand as floating point number
465 // on FPU stack.
466 static void LoadFloatOperand(MacroAssembler* masm, Register number);
467
468 // Code pattern for loading floating point values. Input values must
469 // be either smi or heap number objects (fp values). Requirements:
470 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
471 // Returns operands as floating point numbers on FPU stack.
472 static void LoadFloatOperands(MacroAssembler* masm,
473 Register scratch,
474 ArgLocation arg_location = ARGS_ON_STACK);
475
476 // Similar to LoadFloatOperand but assumes that both operands are smis.
477 // Expects operands in edx, eax.
478 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
479
480 // Test if operands are smi or number objects (fp). Requirements:
481 // operand_1 in eax, operand_2 in edx; falls through on float
482 // operands, jumps to the non_float label otherwise.
483 static void CheckFloatOperands(MacroAssembler* masm,
484 Label* non_float,
485 Register scratch);
486
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000487 // Checks that the two floating point numbers on top of the FPU stack
488 // have int32 values.
489 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
490 Label* non_int32);
491
ricow@chromium.org65fae842010-08-25 15:26:24 +0000492 // Takes the operands in edx and eax and loads them as integers in eax
493 // and ecx.
494 static void LoadAsIntegers(MacroAssembler* masm,
495 TypeInfo type_info,
496 bool use_sse3,
497 Label* operand_conversion_failure);
498 static void LoadNumbersAsIntegers(MacroAssembler* masm,
499 TypeInfo type_info,
500 bool use_sse3,
501 Label* operand_conversion_failure);
502 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
503 bool use_sse3,
504 Label* operand_conversion_failure);
505
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000506 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
507 // operands are pushed on the stack, and that their conversions to int32
508 // are in eax and ecx. Checks that the original numbers were in the int32
509 // range.
510 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
511 bool use_sse3,
512 Label* not_int32);
513
514 // Assumes that operands are smis or heap numbers and loads them
515 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000516 // Leaves operands unchanged.
517 static void LoadSSE2Operands(MacroAssembler* masm);
518
519 // Test if operands are numbers (smi or HeapNumber objects), and load
520 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
521 // either operand is not a number. Operands are in edx and eax.
522 // Leaves operands unchanged.
523 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
524
525 // Similar to LoadSSE2Operands but assumes that both operands are smis.
526 // Expects operands in edx, eax.
527 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000528
529 // Checks that the two floating point numbers loaded into xmm0 and xmm1
530 // have int32 values.
531 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
532 Label* non_int32,
533 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000534};
535
536
537void GenericBinaryOpStub::GenerateSmiCode(MacroAssembler* masm, Label* slow) {
538 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
539 // dividend in eax and edx free for the division. Use eax, ebx for those.
540 Comment load_comment(masm, "-- Load arguments");
541 Register left = edx;
542 Register right = eax;
543 if (op_ == Token::DIV || op_ == Token::MOD) {
544 left = eax;
545 right = ebx;
546 if (HasArgsInRegisters()) {
547 __ mov(ebx, eax);
548 __ mov(eax, edx);
549 }
550 }
551 if (!HasArgsInRegisters()) {
552 __ mov(right, Operand(esp, 1 * kPointerSize));
553 __ mov(left, Operand(esp, 2 * kPointerSize));
554 }
555
556 if (static_operands_type_.IsSmi()) {
557 if (FLAG_debug_code) {
558 __ AbortIfNotSmi(left);
559 __ AbortIfNotSmi(right);
560 }
561 if (op_ == Token::BIT_OR) {
562 __ or_(right, Operand(left));
563 GenerateReturn(masm);
564 return;
565 } else if (op_ == Token::BIT_AND) {
566 __ and_(right, Operand(left));
567 GenerateReturn(masm);
568 return;
569 } else if (op_ == Token::BIT_XOR) {
570 __ xor_(right, Operand(left));
571 GenerateReturn(masm);
572 return;
573 }
574 }
575
576 // 2. Prepare the smi check of both operands by oring them together.
577 Comment smi_check_comment(masm, "-- Smi check arguments");
578 Label not_smis;
579 Register combined = ecx;
580 ASSERT(!left.is(combined) && !right.is(combined));
581 switch (op_) {
582 case Token::BIT_OR:
583 // Perform the operation into eax and smi check the result. Preserve
584 // eax in case the result is not a smi.
585 ASSERT(!left.is(ecx) && !right.is(ecx));
586 __ mov(ecx, right);
587 __ or_(right, Operand(left)); // Bitwise or is commutative.
588 combined = right;
589 break;
590
591 case Token::BIT_XOR:
592 case Token::BIT_AND:
593 case Token::ADD:
594 case Token::SUB:
595 case Token::MUL:
596 case Token::DIV:
597 case Token::MOD:
598 __ mov(combined, right);
599 __ or_(combined, Operand(left));
600 break;
601
602 case Token::SHL:
603 case Token::SAR:
604 case Token::SHR:
605 // Move the right operand into ecx for the shift operation, use eax
606 // for the smi check register.
607 ASSERT(!left.is(ecx) && !right.is(ecx));
608 __ mov(ecx, right);
609 __ or_(right, Operand(left));
610 combined = right;
611 break;
612
613 default:
614 break;
615 }
616
617 // 3. Perform the smi check of the operands.
618 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
619 __ test(combined, Immediate(kSmiTagMask));
620 __ j(not_zero, &not_smis, not_taken);
621
622 // 4. Operands are both smis, perform the operation leaving the result in
623 // eax and check the result if necessary.
624 Comment perform_smi(masm, "-- Perform smi operation");
625 Label use_fp_on_smis;
626 switch (op_) {
627 case Token::BIT_OR:
628 // Nothing to do.
629 break;
630
631 case Token::BIT_XOR:
632 ASSERT(right.is(eax));
633 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
634 break;
635
636 case Token::BIT_AND:
637 ASSERT(right.is(eax));
638 __ and_(right, Operand(left)); // Bitwise and is commutative.
639 break;
640
641 case Token::SHL:
642 // Remove tags from operands (but keep sign).
643 __ SmiUntag(left);
644 __ SmiUntag(ecx);
645 // Perform the operation.
646 __ shl_cl(left);
647 // Check that the *signed* result fits in a smi.
648 __ cmp(left, 0xc0000000);
649 __ j(sign, &use_fp_on_smis, not_taken);
650 // Tag the result and store it in register eax.
651 __ SmiTag(left);
652 __ mov(eax, left);
653 break;
654
655 case Token::SAR:
656 // Remove tags from operands (but keep sign).
657 __ SmiUntag(left);
658 __ SmiUntag(ecx);
659 // Perform the operation.
660 __ sar_cl(left);
661 // Tag the result and store it in register eax.
662 __ SmiTag(left);
663 __ mov(eax, left);
664 break;
665
666 case Token::SHR:
667 // Remove tags from operands (but keep sign).
668 __ SmiUntag(left);
669 __ SmiUntag(ecx);
670 // Perform the operation.
671 __ shr_cl(left);
672 // Check that the *unsigned* result fits in a smi.
673 // Neither of the two high-order bits can be set:
674 // - 0x80000000: high bit would be lost when smi tagging.
675 // - 0x40000000: this number would convert to negative when
676 // Smi tagging these two cases can only happen with shifts
677 // by 0 or 1 when handed a valid smi.
678 __ test(left, Immediate(0xc0000000));
679 __ j(not_zero, slow, not_taken);
680 // Tag the result and store it in register eax.
681 __ SmiTag(left);
682 __ mov(eax, left);
683 break;
684
685 case Token::ADD:
686 ASSERT(right.is(eax));
687 __ add(right, Operand(left)); // Addition is commutative.
688 __ j(overflow, &use_fp_on_smis, not_taken);
689 break;
690
691 case Token::SUB:
692 __ sub(left, Operand(right));
693 __ j(overflow, &use_fp_on_smis, not_taken);
694 __ mov(eax, left);
695 break;
696
697 case Token::MUL:
698 // If the smi tag is 0 we can just leave the tag on one operand.
699 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
700 // We can't revert the multiplication if the result is not a smi
701 // so save the right operand.
702 __ mov(ebx, right);
703 // Remove tag from one of the operands (but keep sign).
704 __ SmiUntag(right);
705 // Do multiplication.
706 __ imul(right, Operand(left)); // Multiplication is commutative.
707 __ j(overflow, &use_fp_on_smis, not_taken);
708 // Check for negative zero result. Use combined = left | right.
709 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
710 break;
711
712 case Token::DIV:
713 // We can't revert the division if the result is not a smi so
714 // save the left operand.
715 __ mov(edi, left);
716 // Check for 0 divisor.
717 __ test(right, Operand(right));
718 __ j(zero, &use_fp_on_smis, not_taken);
719 // Sign extend left into edx:eax.
720 ASSERT(left.is(eax));
721 __ cdq();
722 // Divide edx:eax by right.
723 __ idiv(right);
724 // Check for the corner case of dividing the most negative smi by
725 // -1. We cannot use the overflow flag, since it is not set by idiv
726 // instruction.
727 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
728 __ cmp(eax, 0x40000000);
729 __ j(equal, &use_fp_on_smis);
730 // Check for negative zero result. Use combined = left | right.
731 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
732 // Check that the remainder is zero.
733 __ test(edx, Operand(edx));
734 __ j(not_zero, &use_fp_on_smis);
735 // Tag the result and store it in register eax.
736 __ SmiTag(eax);
737 break;
738
739 case Token::MOD:
740 // Check for 0 divisor.
741 __ test(right, Operand(right));
742 __ j(zero, &not_smis, not_taken);
743
744 // Sign extend left into edx:eax.
745 ASSERT(left.is(eax));
746 __ cdq();
747 // Divide edx:eax by right.
748 __ idiv(right);
749 // Check for negative zero result. Use combined = left | right.
750 __ NegativeZeroTest(edx, combined, slow);
751 // Move remainder to register eax.
752 __ mov(eax, edx);
753 break;
754
755 default:
756 UNREACHABLE();
757 }
758
759 // 5. Emit return of result in eax.
760 GenerateReturn(masm);
761
762 // 6. For some operations emit inline code to perform floating point
763 // operations on known smis (e.g., if the result of the operation
764 // overflowed the smi range).
765 switch (op_) {
766 case Token::SHL: {
767 Comment perform_float(masm, "-- Perform float operation on smis");
768 __ bind(&use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000769 if (runtime_operands_type_ != BinaryOpIC::UNINIT_OR_SMI) {
770 // Result we want is in left == edx, so we can put the allocated heap
771 // number in eax.
772 __ AllocateHeapNumber(eax, ecx, ebx, slow);
773 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000774 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000775 CpuFeatures::Scope use_sse2(SSE2);
776 __ cvtsi2sd(xmm0, Operand(left));
777 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
778 } else {
779 // It's OK to overwrite the right argument on the stack because we
780 // are about to return.
781 __ mov(Operand(esp, 1 * kPointerSize), left);
782 __ fild_s(Operand(esp, 1 * kPointerSize));
783 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
784 }
785 GenerateReturn(masm);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000786 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000787 ASSERT(runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI);
788 __ jmp(slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000789 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000790 break;
791 }
792
793 case Token::ADD:
794 case Token::SUB:
795 case Token::MUL:
796 case Token::DIV: {
797 Comment perform_float(masm, "-- Perform float operation on smis");
798 __ bind(&use_fp_on_smis);
799 // Restore arguments to edx, eax.
800 switch (op_) {
801 case Token::ADD:
802 // Revert right = right + left.
803 __ sub(right, Operand(left));
804 break;
805 case Token::SUB:
806 // Revert left = left - right.
807 __ add(left, Operand(right));
808 break;
809 case Token::MUL:
810 // Right was clobbered but a copy is in ebx.
811 __ mov(right, ebx);
812 break;
813 case Token::DIV:
814 // Left was clobbered but a copy is in edi. Right is in ebx for
815 // division.
816 __ mov(edx, edi);
817 __ mov(eax, right);
818 break;
819 default: UNREACHABLE();
820 break;
821 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000822 if (runtime_operands_type_ != BinaryOpIC::UNINIT_OR_SMI) {
823 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000824 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000825 CpuFeatures::Scope use_sse2(SSE2);
826 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
827 switch (op_) {
828 case Token::ADD: __ addsd(xmm0, xmm1); break;
829 case Token::SUB: __ subsd(xmm0, xmm1); break;
830 case Token::MUL: __ mulsd(xmm0, xmm1); break;
831 case Token::DIV: __ divsd(xmm0, xmm1); break;
832 default: UNREACHABLE();
833 }
834 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
835 } else { // SSE2 not available, use FPU.
836 FloatingPointHelper::LoadFloatSmis(masm, ebx);
837 switch (op_) {
838 case Token::ADD: __ faddp(1); break;
839 case Token::SUB: __ fsubp(1); break;
840 case Token::MUL: __ fmulp(1); break;
841 case Token::DIV: __ fdivp(1); break;
842 default: UNREACHABLE();
843 }
844 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000845 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000846 __ mov(eax, ecx);
847 GenerateReturn(masm);
848 } else {
849 ASSERT(runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI);
850 __ jmp(slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000851 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000852 break;
853 }
854
855 default:
856 break;
857 }
858
859 // 7. Non-smi operands, fall out to the non-smi code with the operands in
860 // edx and eax.
861 Comment done_comment(masm, "-- Enter non-smi code");
862 __ bind(&not_smis);
863 switch (op_) {
864 case Token::BIT_OR:
865 case Token::SHL:
866 case Token::SAR:
867 case Token::SHR:
868 // Right operand is saved in ecx and eax was destroyed by the smi
869 // check.
870 __ mov(eax, ecx);
871 break;
872
873 case Token::DIV:
874 case Token::MOD:
875 // Operands are in eax, ebx at this point.
876 __ mov(edx, eax);
877 __ mov(eax, ebx);
878 break;
879
880 default:
881 break;
882 }
883}
884
885
886void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
887 Label call_runtime;
888
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000889 Counters* counters = masm->isolate()->counters();
890 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000891
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000892 if (runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI) {
893 Label slow;
894 if (ShouldGenerateSmiCode()) GenerateSmiCode(masm, &slow);
895 __ bind(&slow);
896 GenerateTypeTransition(masm);
897 }
898
ricow@chromium.org65fae842010-08-25 15:26:24 +0000899 // Generate fast case smi code if requested. This flag is set when the fast
900 // case smi code is not generated by the caller. Generating it here will speed
901 // up common operations.
902 if (ShouldGenerateSmiCode()) {
903 GenerateSmiCode(masm, &call_runtime);
904 } else if (op_ != Token::MOD) { // MOD goes straight to runtime.
905 if (!HasArgsInRegisters()) {
906 GenerateLoadArguments(masm);
907 }
908 }
909
910 // Floating point case.
911 if (ShouldGenerateFPCode()) {
912 switch (op_) {
913 case Token::ADD:
914 case Token::SUB:
915 case Token::MUL:
916 case Token::DIV: {
917 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
918 HasSmiCodeInStub()) {
919 // Execution reaches this point when the first non-smi argument occurs
920 // (and only if smi code is generated). This is the right moment to
921 // patch to HEAP_NUMBERS state. The transition is attempted only for
922 // the four basic operations. The stub stays in the DEFAULT state
923 // forever for all other operations (also if smi code is skipped).
924 GenerateTypeTransition(masm);
925 break;
926 }
927
928 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000929 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000930 CpuFeatures::Scope use_sse2(SSE2);
931 if (static_operands_type_.IsNumber()) {
932 if (FLAG_debug_code) {
933 // Assert at runtime that inputs are only numbers.
934 __ AbortIfNotNumber(edx);
935 __ AbortIfNotNumber(eax);
936 }
937 if (static_operands_type_.IsSmi()) {
938 if (FLAG_debug_code) {
939 __ AbortIfNotSmi(edx);
940 __ AbortIfNotSmi(eax);
941 }
942 FloatingPointHelper::LoadSSE2Smis(masm, ecx);
943 } else {
944 FloatingPointHelper::LoadSSE2Operands(masm);
945 }
946 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000947 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000948 }
949
950 switch (op_) {
951 case Token::ADD: __ addsd(xmm0, xmm1); break;
952 case Token::SUB: __ subsd(xmm0, xmm1); break;
953 case Token::MUL: __ mulsd(xmm0, xmm1); break;
954 case Token::DIV: __ divsd(xmm0, xmm1); break;
955 default: UNREACHABLE();
956 }
957 GenerateHeapResultAllocation(masm, &call_runtime);
958 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
959 GenerateReturn(masm);
960 } else { // SSE2 not available, use FPU.
961 if (static_operands_type_.IsNumber()) {
962 if (FLAG_debug_code) {
963 // Assert at runtime that inputs are only numbers.
964 __ AbortIfNotNumber(edx);
965 __ AbortIfNotNumber(eax);
966 }
967 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000968 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000969 }
970 FloatingPointHelper::LoadFloatOperands(
971 masm,
972 ecx,
973 FloatingPointHelper::ARGS_IN_REGISTERS);
974 switch (op_) {
975 case Token::ADD: __ faddp(1); break;
976 case Token::SUB: __ fsubp(1); break;
977 case Token::MUL: __ fmulp(1); break;
978 case Token::DIV: __ fdivp(1); break;
979 default: UNREACHABLE();
980 }
981 Label after_alloc_failure;
982 GenerateHeapResultAllocation(masm, &after_alloc_failure);
983 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
984 GenerateReturn(masm);
985 __ bind(&after_alloc_failure);
986 __ ffree();
987 __ jmp(&call_runtime);
988 }
989 __ bind(&not_floats);
990 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
991 !HasSmiCodeInStub()) {
992 // Execution reaches this point when the first non-number argument
993 // occurs (and only if smi code is skipped from the stub, otherwise
994 // the patching has already been done earlier in this case branch).
995 // Try patching to STRINGS for ADD operation.
996 if (op_ == Token::ADD) {
997 GenerateTypeTransition(masm);
998 }
999 }
1000 break;
1001 }
1002 case Token::MOD: {
1003 // For MOD we go directly to runtime in the non-smi case.
1004 break;
1005 }
1006 case Token::BIT_OR:
1007 case Token::BIT_AND:
1008 case Token::BIT_XOR:
1009 case Token::SAR:
1010 case Token::SHL:
1011 case Token::SHR: {
1012 Label non_smi_result;
1013 FloatingPointHelper::LoadAsIntegers(masm,
1014 static_operands_type_,
1015 use_sse3_,
1016 &call_runtime);
1017 switch (op_) {
1018 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1019 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1020 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1021 case Token::SAR: __ sar_cl(eax); break;
1022 case Token::SHL: __ shl_cl(eax); break;
1023 case Token::SHR: __ shr_cl(eax); break;
1024 default: UNREACHABLE();
1025 }
1026 if (op_ == Token::SHR) {
1027 // Check if result is non-negative and fits in a smi.
1028 __ test(eax, Immediate(0xc0000000));
1029 __ j(not_zero, &call_runtime);
1030 } else {
1031 // Check if result fits in a smi.
1032 __ cmp(eax, 0xc0000000);
1033 __ j(negative, &non_smi_result);
1034 }
1035 // Tag smi result and return.
1036 __ SmiTag(eax);
1037 GenerateReturn(masm);
1038
1039 // All ops except SHR return a signed int32 that we load in
1040 // a HeapNumber.
1041 if (op_ != Token::SHR) {
1042 __ bind(&non_smi_result);
1043 // Allocate a heap number if needed.
1044 __ mov(ebx, Operand(eax)); // ebx: result
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001045 NearLabel skip_allocation;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001046 switch (mode_) {
1047 case OVERWRITE_LEFT:
1048 case OVERWRITE_RIGHT:
1049 // If the operand was an object, we skip the
1050 // allocation of a heap number.
1051 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1052 1 * kPointerSize : 2 * kPointerSize));
1053 __ test(eax, Immediate(kSmiTagMask));
1054 __ j(not_zero, &skip_allocation, not_taken);
1055 // Fall through!
1056 case NO_OVERWRITE:
1057 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1058 __ bind(&skip_allocation);
1059 break;
1060 default: UNREACHABLE();
1061 }
1062 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001063 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001064 CpuFeatures::Scope use_sse2(SSE2);
1065 __ cvtsi2sd(xmm0, Operand(ebx));
1066 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1067 } else {
1068 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1069 __ fild_s(Operand(esp, 1 * kPointerSize));
1070 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1071 }
1072 GenerateReturn(masm);
1073 }
1074 break;
1075 }
1076 default: UNREACHABLE(); break;
1077 }
1078 }
1079
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001080 // If all else fails, use the runtime system to get the correct
1081 // result. If arguments was passed in registers now place them on the
1082 // stack in the correct order below the return address.
1083
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001084 // Avoid hitting the string ADD code below when allocation fails in
1085 // the floating point code above.
1086 if (op_ != Token::ADD) {
1087 __ bind(&call_runtime);
1088 }
1089
ricow@chromium.org65fae842010-08-25 15:26:24 +00001090 if (HasArgsInRegisters()) {
1091 GenerateRegisterArgsPush(masm);
1092 }
1093
1094 switch (op_) {
1095 case Token::ADD: {
1096 // Test for string arguments before calling runtime.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001097
1098 // If this stub has already generated FP-specific code then the arguments
1099 // are already in edx, eax
1100 if (!ShouldGenerateFPCode() && !HasArgsInRegisters()) {
1101 GenerateLoadArguments(masm);
1102 }
1103
1104 // Registers containing left and right operands respectively.
1105 Register lhs, rhs;
1106 if (HasArgsReversed()) {
1107 lhs = eax;
1108 rhs = edx;
1109 } else {
1110 lhs = edx;
1111 rhs = eax;
1112 }
1113
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001114 // Test if left operand is a string.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001115 NearLabel lhs_not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001116 __ test(lhs, Immediate(kSmiTagMask));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001117 __ j(zero, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001118 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, ecx);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001119 __ j(above_equal, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001120
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001121 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
1122 __ TailCallStub(&string_add_left_stub);
1123
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001124 NearLabel call_runtime_with_args;
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001125 // Left operand is not a string, test right.
1126 __ bind(&lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001127 __ test(rhs, Immediate(kSmiTagMask));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001128 __ j(zero, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001129 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001130 __ j(above_equal, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001131
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001132 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
1133 __ TailCallStub(&string_add_right_stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001134
ricow@chromium.org65fae842010-08-25 15:26:24 +00001135 // Neither argument is a string.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001136 __ bind(&call_runtime);
1137 if (HasArgsInRegisters()) {
1138 GenerateRegisterArgsPush(masm);
1139 }
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001140 __ bind(&call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001141 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1142 break;
1143 }
1144 case Token::SUB:
1145 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1146 break;
1147 case Token::MUL:
1148 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1149 break;
1150 case Token::DIV:
1151 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1152 break;
1153 case Token::MOD:
1154 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1155 break;
1156 case Token::BIT_OR:
1157 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1158 break;
1159 case Token::BIT_AND:
1160 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1161 break;
1162 case Token::BIT_XOR:
1163 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1164 break;
1165 case Token::SAR:
1166 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1167 break;
1168 case Token::SHL:
1169 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1170 break;
1171 case Token::SHR:
1172 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1173 break;
1174 default:
1175 UNREACHABLE();
1176 }
1177}
1178
1179
1180void GenericBinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
1181 Label* alloc_failure) {
1182 Label skip_allocation;
1183 OverwriteMode mode = mode_;
1184 if (HasArgsReversed()) {
1185 if (mode == OVERWRITE_RIGHT) {
1186 mode = OVERWRITE_LEFT;
1187 } else if (mode == OVERWRITE_LEFT) {
1188 mode = OVERWRITE_RIGHT;
1189 }
1190 }
1191 switch (mode) {
1192 case OVERWRITE_LEFT: {
1193 // If the argument in edx is already an object, we skip the
1194 // allocation of a heap number.
1195 __ test(edx, Immediate(kSmiTagMask));
1196 __ j(not_zero, &skip_allocation, not_taken);
1197 // Allocate a heap number for the result. Keep eax and edx intact
1198 // for the possible runtime call.
1199 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1200 // Now edx can be overwritten losing one of the arguments as we are
1201 // now done and will not need it any more.
1202 __ mov(edx, Operand(ebx));
1203 __ bind(&skip_allocation);
1204 // Use object in edx as a result holder
1205 __ mov(eax, Operand(edx));
1206 break;
1207 }
1208 case OVERWRITE_RIGHT:
1209 // If the argument in eax is already an object, we skip the
1210 // allocation of a heap number.
1211 __ test(eax, Immediate(kSmiTagMask));
1212 __ j(not_zero, &skip_allocation, not_taken);
1213 // Fall through!
1214 case NO_OVERWRITE:
1215 // Allocate a heap number for the result. Keep eax and edx intact
1216 // for the possible runtime call.
1217 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1218 // Now eax can be overwritten losing one of the arguments as we are
1219 // now done and will not need it any more.
1220 __ mov(eax, ebx);
1221 __ bind(&skip_allocation);
1222 break;
1223 default: UNREACHABLE();
1224 }
1225}
1226
1227
1228void GenericBinaryOpStub::GenerateLoadArguments(MacroAssembler* masm) {
1229 // If arguments are not passed in registers read them from the stack.
1230 ASSERT(!HasArgsInRegisters());
1231 __ mov(eax, Operand(esp, 1 * kPointerSize));
1232 __ mov(edx, Operand(esp, 2 * kPointerSize));
1233}
1234
1235
1236void GenericBinaryOpStub::GenerateReturn(MacroAssembler* masm) {
1237 // If arguments are not passed in registers remove them from the stack before
1238 // returning.
1239 if (!HasArgsInRegisters()) {
1240 __ ret(2 * kPointerSize); // Remove both operands
1241 } else {
1242 __ ret(0);
1243 }
1244}
1245
1246
1247void GenericBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
1248 ASSERT(HasArgsInRegisters());
1249 __ pop(ecx);
1250 if (HasArgsReversed()) {
1251 __ push(eax);
1252 __ push(edx);
1253 } else {
1254 __ push(edx);
1255 __ push(eax);
1256 }
1257 __ push(ecx);
1258}
1259
1260
1261void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1262 // Ensure the operands are on the stack.
1263 if (HasArgsInRegisters()) {
1264 GenerateRegisterArgsPush(masm);
1265 }
1266
1267 __ pop(ecx); // Save return address.
1268
1269 // Left and right arguments are now on top.
1270 // Push this stub's key. Although the operation and the type info are
1271 // encoded into the key, the encoding is opaque, so push them too.
1272 __ push(Immediate(Smi::FromInt(MinorKey())));
1273 __ push(Immediate(Smi::FromInt(op_)));
1274 __ push(Immediate(Smi::FromInt(runtime_operands_type_)));
1275
1276 __ push(ecx); // Push return address.
1277
1278 // Patch the caller to an appropriate specialized stub and return the
1279 // operation result to the caller of the stub.
1280 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001281 ExternalReference(IC_Utility(IC::kBinaryOp_Patch), masm->isolate()),
ricow@chromium.org65fae842010-08-25 15:26:24 +00001282 5,
1283 1);
1284}
1285
1286
1287Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
1288 GenericBinaryOpStub stub(key, type_info);
1289 return stub.GetCode();
1290}
1291
1292
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001293Handle<Code> GetTypeRecordingBinaryOpStub(int key,
1294 TRBinaryOpIC::TypeInfo type_info,
1295 TRBinaryOpIC::TypeInfo result_type_info) {
1296 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
1297 return stub.GetCode();
1298}
1299
1300
1301void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1302 __ pop(ecx); // Save return address.
1303 __ push(edx);
1304 __ push(eax);
1305 // Left and right arguments are now on top.
1306 // Push this stub's key. Although the operation and the type info are
1307 // encoded into the key, the encoding is opaque, so push them too.
1308 __ push(Immediate(Smi::FromInt(MinorKey())));
1309 __ push(Immediate(Smi::FromInt(op_)));
1310 __ push(Immediate(Smi::FromInt(operands_type_)));
1311
1312 __ push(ecx); // Push return address.
1313
1314 // Patch the caller to an appropriate specialized stub and return the
1315 // operation result to the caller of the stub.
1316 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001317 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
1318 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001319 5,
1320 1);
1321}
1322
1323
1324// Prepare for a type transition runtime call when the args are already on
1325// the stack, under the return address.
1326void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
1327 MacroAssembler* masm) {
1328 __ pop(ecx); // Save return address.
1329 // Left and right arguments are already on top of the stack.
1330 // Push this stub's key. Although the operation and the type info are
1331 // encoded into the key, the encoding is opaque, so push them too.
1332 __ push(Immediate(Smi::FromInt(MinorKey())));
1333 __ push(Immediate(Smi::FromInt(op_)));
1334 __ push(Immediate(Smi::FromInt(operands_type_)));
1335
1336 __ push(ecx); // Push return address.
1337
1338 // Patch the caller to an appropriate specialized stub and return the
1339 // operation result to the caller of the stub.
1340 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001341 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
1342 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001343 5,
1344 1);
1345}
1346
1347
1348void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
1349 switch (operands_type_) {
1350 case TRBinaryOpIC::UNINITIALIZED:
1351 GenerateTypeTransition(masm);
1352 break;
1353 case TRBinaryOpIC::SMI:
1354 GenerateSmiStub(masm);
1355 break;
1356 case TRBinaryOpIC::INT32:
1357 GenerateInt32Stub(masm);
1358 break;
1359 case TRBinaryOpIC::HEAP_NUMBER:
1360 GenerateHeapNumberStub(masm);
1361 break;
1362 case TRBinaryOpIC::STRING:
1363 GenerateStringStub(masm);
1364 break;
1365 case TRBinaryOpIC::GENERIC:
1366 GenerateGeneric(masm);
1367 break;
1368 default:
1369 UNREACHABLE();
1370 }
1371}
1372
1373
1374const char* TypeRecordingBinaryOpStub::GetName() {
1375 if (name_ != NULL) return name_;
1376 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001377 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
1378 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001379 if (name_ == NULL) return "OOM";
1380 const char* op_name = Token::Name(op_);
1381 const char* overwrite_name;
1382 switch (mode_) {
1383 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1384 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1385 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1386 default: overwrite_name = "UnknownOverwrite"; break;
1387 }
1388
1389 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1390 "TypeRecordingBinaryOpStub_%s_%s_%s",
1391 op_name,
1392 overwrite_name,
1393 TRBinaryOpIC::GetName(operands_type_));
1394 return name_;
1395}
1396
1397
1398void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
1399 Label* slow,
1400 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
1401 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
1402 // dividend in eax and edx free for the division. Use eax, ebx for those.
1403 Comment load_comment(masm, "-- Load arguments");
1404 Register left = edx;
1405 Register right = eax;
1406 if (op_ == Token::DIV || op_ == Token::MOD) {
1407 left = eax;
1408 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001409 __ mov(ebx, eax);
1410 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001411 }
1412
1413
1414 // 2. Prepare the smi check of both operands by oring them together.
1415 Comment smi_check_comment(masm, "-- Smi check arguments");
1416 Label not_smis;
1417 Register combined = ecx;
1418 ASSERT(!left.is(combined) && !right.is(combined));
1419 switch (op_) {
1420 case Token::BIT_OR:
1421 // Perform the operation into eax and smi check the result. Preserve
1422 // eax in case the result is not a smi.
1423 ASSERT(!left.is(ecx) && !right.is(ecx));
1424 __ mov(ecx, right);
1425 __ or_(right, Operand(left)); // Bitwise or is commutative.
1426 combined = right;
1427 break;
1428
1429 case Token::BIT_XOR:
1430 case Token::BIT_AND:
1431 case Token::ADD:
1432 case Token::SUB:
1433 case Token::MUL:
1434 case Token::DIV:
1435 case Token::MOD:
1436 __ mov(combined, right);
1437 __ or_(combined, Operand(left));
1438 break;
1439
1440 case Token::SHL:
1441 case Token::SAR:
1442 case Token::SHR:
1443 // Move the right operand into ecx for the shift operation, use eax
1444 // for the smi check register.
1445 ASSERT(!left.is(ecx) && !right.is(ecx));
1446 __ mov(ecx, right);
1447 __ or_(right, Operand(left));
1448 combined = right;
1449 break;
1450
1451 default:
1452 break;
1453 }
1454
1455 // 3. Perform the smi check of the operands.
1456 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1457 __ test(combined, Immediate(kSmiTagMask));
1458 __ j(not_zero, &not_smis, not_taken);
1459
1460 // 4. Operands are both smis, perform the operation leaving the result in
1461 // eax and check the result if necessary.
1462 Comment perform_smi(masm, "-- Perform smi operation");
1463 Label use_fp_on_smis;
1464 switch (op_) {
1465 case Token::BIT_OR:
1466 // Nothing to do.
1467 break;
1468
1469 case Token::BIT_XOR:
1470 ASSERT(right.is(eax));
1471 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1472 break;
1473
1474 case Token::BIT_AND:
1475 ASSERT(right.is(eax));
1476 __ and_(right, Operand(left)); // Bitwise and is commutative.
1477 break;
1478
1479 case Token::SHL:
1480 // Remove tags from operands (but keep sign).
1481 __ SmiUntag(left);
1482 __ SmiUntag(ecx);
1483 // Perform the operation.
1484 __ shl_cl(left);
1485 // Check that the *signed* result fits in a smi.
1486 __ cmp(left, 0xc0000000);
1487 __ j(sign, &use_fp_on_smis, not_taken);
1488 // Tag the result and store it in register eax.
1489 __ SmiTag(left);
1490 __ mov(eax, left);
1491 break;
1492
1493 case Token::SAR:
1494 // Remove tags from operands (but keep sign).
1495 __ SmiUntag(left);
1496 __ SmiUntag(ecx);
1497 // Perform the operation.
1498 __ sar_cl(left);
1499 // Tag the result and store it in register eax.
1500 __ SmiTag(left);
1501 __ mov(eax, left);
1502 break;
1503
1504 case Token::SHR:
1505 // Remove tags from operands (but keep sign).
1506 __ SmiUntag(left);
1507 __ SmiUntag(ecx);
1508 // Perform the operation.
1509 __ shr_cl(left);
1510 // Check that the *unsigned* result fits in a smi.
1511 // Neither of the two high-order bits can be set:
1512 // - 0x80000000: high bit would be lost when smi tagging.
1513 // - 0x40000000: this number would convert to negative when
1514 // Smi tagging these two cases can only happen with shifts
1515 // by 0 or 1 when handed a valid smi.
1516 __ test(left, Immediate(0xc0000000));
1517 __ j(not_zero, slow, not_taken);
1518 // Tag the result and store it in register eax.
1519 __ SmiTag(left);
1520 __ mov(eax, left);
1521 break;
1522
1523 case Token::ADD:
1524 ASSERT(right.is(eax));
1525 __ add(right, Operand(left)); // Addition is commutative.
1526 __ j(overflow, &use_fp_on_smis, not_taken);
1527 break;
1528
1529 case Token::SUB:
1530 __ sub(left, Operand(right));
1531 __ j(overflow, &use_fp_on_smis, not_taken);
1532 __ mov(eax, left);
1533 break;
1534
1535 case Token::MUL:
1536 // If the smi tag is 0 we can just leave the tag on one operand.
1537 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1538 // We can't revert the multiplication if the result is not a smi
1539 // so save the right operand.
1540 __ mov(ebx, right);
1541 // Remove tag from one of the operands (but keep sign).
1542 __ SmiUntag(right);
1543 // Do multiplication.
1544 __ imul(right, Operand(left)); // Multiplication is commutative.
1545 __ j(overflow, &use_fp_on_smis, not_taken);
1546 // Check for negative zero result. Use combined = left | right.
1547 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1548 break;
1549
1550 case Token::DIV:
1551 // We can't revert the division if the result is not a smi so
1552 // save the left operand.
1553 __ mov(edi, left);
1554 // Check for 0 divisor.
1555 __ test(right, Operand(right));
1556 __ j(zero, &use_fp_on_smis, not_taken);
1557 // Sign extend left into edx:eax.
1558 ASSERT(left.is(eax));
1559 __ cdq();
1560 // Divide edx:eax by right.
1561 __ idiv(right);
1562 // Check for the corner case of dividing the most negative smi by
1563 // -1. We cannot use the overflow flag, since it is not set by idiv
1564 // instruction.
1565 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1566 __ cmp(eax, 0x40000000);
1567 __ j(equal, &use_fp_on_smis);
1568 // Check for negative zero result. Use combined = left | right.
1569 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1570 // Check that the remainder is zero.
1571 __ test(edx, Operand(edx));
1572 __ j(not_zero, &use_fp_on_smis);
1573 // Tag the result and store it in register eax.
1574 __ SmiTag(eax);
1575 break;
1576
1577 case Token::MOD:
1578 // Check for 0 divisor.
1579 __ test(right, Operand(right));
1580 __ j(zero, &not_smis, not_taken);
1581
1582 // Sign extend left into edx:eax.
1583 ASSERT(left.is(eax));
1584 __ cdq();
1585 // Divide edx:eax by right.
1586 __ idiv(right);
1587 // Check for negative zero result. Use combined = left | right.
1588 __ NegativeZeroTest(edx, combined, slow);
1589 // Move remainder to register eax.
1590 __ mov(eax, edx);
1591 break;
1592
1593 default:
1594 UNREACHABLE();
1595 }
1596
1597 // 5. Emit return of result in eax. Some operations have registers pushed.
1598 switch (op_) {
1599 case Token::ADD:
1600 case Token::SUB:
1601 case Token::MUL:
1602 case Token::DIV:
1603 __ ret(0);
1604 break;
1605 case Token::MOD:
1606 case Token::BIT_OR:
1607 case Token::BIT_AND:
1608 case Token::BIT_XOR:
1609 case Token::SAR:
1610 case Token::SHL:
1611 case Token::SHR:
1612 __ ret(2 * kPointerSize);
1613 break;
1614 default:
1615 UNREACHABLE();
1616 }
1617
1618 // 6. For some operations emit inline code to perform floating point
1619 // operations on known smis (e.g., if the result of the operation
1620 // overflowed the smi range).
1621 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1622 __ bind(&use_fp_on_smis);
1623 switch (op_) {
1624 // Undo the effects of some operations, and some register moves.
1625 case Token::SHL:
1626 // The arguments are saved on the stack, and only used from there.
1627 break;
1628 case Token::ADD:
1629 // Revert right = right + left.
1630 __ sub(right, Operand(left));
1631 break;
1632 case Token::SUB:
1633 // Revert left = left - right.
1634 __ add(left, Operand(right));
1635 break;
1636 case Token::MUL:
1637 // Right was clobbered but a copy is in ebx.
1638 __ mov(right, ebx);
1639 break;
1640 case Token::DIV:
1641 // Left was clobbered but a copy is in edi. Right is in ebx for
1642 // division. They should be in eax, ebx for jump to not_smi.
1643 __ mov(eax, edi);
1644 break;
1645 default:
1646 // No other operators jump to use_fp_on_smis.
1647 break;
1648 }
1649 __ jmp(&not_smis);
1650 } else {
1651 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1652 switch (op_) {
1653 case Token::SHL: {
1654 Comment perform_float(masm, "-- Perform float operation on smis");
1655 __ bind(&use_fp_on_smis);
1656 // Result we want is in left == edx, so we can put the allocated heap
1657 // number in eax.
1658 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1659 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001660 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001661 CpuFeatures::Scope use_sse2(SSE2);
1662 __ cvtsi2sd(xmm0, Operand(left));
1663 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1664 } else {
1665 // It's OK to overwrite the right argument on the stack because we
1666 // are about to return.
1667 __ mov(Operand(esp, 1 * kPointerSize), left);
1668 __ fild_s(Operand(esp, 1 * kPointerSize));
1669 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1670 }
1671 __ ret(2 * kPointerSize);
1672 break;
1673 }
1674
1675 case Token::ADD:
1676 case Token::SUB:
1677 case Token::MUL:
1678 case Token::DIV: {
1679 Comment perform_float(masm, "-- Perform float operation on smis");
1680 __ bind(&use_fp_on_smis);
1681 // Restore arguments to edx, eax.
1682 switch (op_) {
1683 case Token::ADD:
1684 // Revert right = right + left.
1685 __ sub(right, Operand(left));
1686 break;
1687 case Token::SUB:
1688 // Revert left = left - right.
1689 __ add(left, Operand(right));
1690 break;
1691 case Token::MUL:
1692 // Right was clobbered but a copy is in ebx.
1693 __ mov(right, ebx);
1694 break;
1695 case Token::DIV:
1696 // Left was clobbered but a copy is in edi. Right is in ebx for
1697 // division.
1698 __ mov(edx, edi);
1699 __ mov(eax, right);
1700 break;
1701 default: UNREACHABLE();
1702 break;
1703 }
1704 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001705 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001706 CpuFeatures::Scope use_sse2(SSE2);
1707 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1708 switch (op_) {
1709 case Token::ADD: __ addsd(xmm0, xmm1); break;
1710 case Token::SUB: __ subsd(xmm0, xmm1); break;
1711 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1712 case Token::DIV: __ divsd(xmm0, xmm1); break;
1713 default: UNREACHABLE();
1714 }
1715 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1716 } else { // SSE2 not available, use FPU.
1717 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1718 switch (op_) {
1719 case Token::ADD: __ faddp(1); break;
1720 case Token::SUB: __ fsubp(1); break;
1721 case Token::MUL: __ fmulp(1); break;
1722 case Token::DIV: __ fdivp(1); break;
1723 default: UNREACHABLE();
1724 }
1725 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1726 }
1727 __ mov(eax, ecx);
1728 __ ret(0);
1729 break;
1730 }
1731
1732 default:
1733 break;
1734 }
1735 }
1736
1737 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1738 // edx and eax.
1739 Comment done_comment(masm, "-- Enter non-smi code");
1740 __ bind(&not_smis);
1741 switch (op_) {
1742 case Token::BIT_OR:
1743 case Token::SHL:
1744 case Token::SAR:
1745 case Token::SHR:
1746 // Right operand is saved in ecx and eax was destroyed by the smi
1747 // check.
1748 __ mov(eax, ecx);
1749 break;
1750
1751 case Token::DIV:
1752 case Token::MOD:
1753 // Operands are in eax, ebx at this point.
1754 __ mov(edx, eax);
1755 __ mov(eax, ebx);
1756 break;
1757
1758 default:
1759 break;
1760 }
1761}
1762
1763
1764void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1765 Label call_runtime;
1766
1767 switch (op_) {
1768 case Token::ADD:
1769 case Token::SUB:
1770 case Token::MUL:
1771 case Token::DIV:
1772 break;
1773 case Token::MOD:
1774 case Token::BIT_OR:
1775 case Token::BIT_AND:
1776 case Token::BIT_XOR:
1777 case Token::SAR:
1778 case Token::SHL:
1779 case Token::SHR:
1780 GenerateRegisterArgsPush(masm);
1781 break;
1782 default:
1783 UNREACHABLE();
1784 }
1785
1786 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1787 result_type_ == TRBinaryOpIC::SMI) {
1788 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1789 } else {
1790 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1791 }
1792 __ bind(&call_runtime);
1793 switch (op_) {
1794 case Token::ADD:
1795 case Token::SUB:
1796 case Token::MUL:
1797 case Token::DIV:
1798 GenerateTypeTransition(masm);
1799 break;
1800 case Token::MOD:
1801 case Token::BIT_OR:
1802 case Token::BIT_AND:
1803 case Token::BIT_XOR:
1804 case Token::SAR:
1805 case Token::SHL:
1806 case Token::SHR:
1807 GenerateTypeTransitionWithSavedArgs(masm);
1808 break;
1809 default:
1810 UNREACHABLE();
1811 }
1812}
1813
1814
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001815void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001816 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1817 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001818 // Try to add arguments as strings, otherwise, transition to the generic
1819 // TRBinaryOpIC type.
1820 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001821 GenerateTypeTransition(masm);
1822}
1823
1824
1825void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1826 Label call_runtime;
1827 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1828
1829 // Floating point case.
1830 switch (op_) {
1831 case Token::ADD:
1832 case Token::SUB:
1833 case Token::MUL:
1834 case Token::DIV: {
1835 Label not_floats;
1836 Label not_int32;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001837 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001838 CpuFeatures::Scope use_sse2(SSE2);
1839 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1840 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1841 switch (op_) {
1842 case Token::ADD: __ addsd(xmm0, xmm1); break;
1843 case Token::SUB: __ subsd(xmm0, xmm1); break;
1844 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1845 case Token::DIV: __ divsd(xmm0, xmm1); break;
1846 default: UNREACHABLE();
1847 }
1848 // Check result type if it is currently Int32.
1849 if (result_type_ <= TRBinaryOpIC::INT32) {
1850 __ cvttsd2si(ecx, Operand(xmm0));
1851 __ cvtsi2sd(xmm2, Operand(ecx));
1852 __ ucomisd(xmm0, xmm2);
1853 __ j(not_zero, &not_int32);
1854 __ j(carry, &not_int32);
1855 }
1856 GenerateHeapResultAllocation(masm, &call_runtime);
1857 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1858 __ ret(0);
1859 } else { // SSE2 not available, use FPU.
1860 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1861 FloatingPointHelper::LoadFloatOperands(
1862 masm,
1863 ecx,
1864 FloatingPointHelper::ARGS_IN_REGISTERS);
1865 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1866 switch (op_) {
1867 case Token::ADD: __ faddp(1); break;
1868 case Token::SUB: __ fsubp(1); break;
1869 case Token::MUL: __ fmulp(1); break;
1870 case Token::DIV: __ fdivp(1); break;
1871 default: UNREACHABLE();
1872 }
1873 Label after_alloc_failure;
1874 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1875 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1876 __ ret(0);
1877 __ bind(&after_alloc_failure);
1878 __ ffree();
1879 __ jmp(&call_runtime);
1880 }
1881
1882 __ bind(&not_floats);
1883 __ bind(&not_int32);
1884 GenerateTypeTransition(masm);
1885 break;
1886 }
1887
1888 case Token::MOD: {
1889 // For MOD we go directly to runtime in the non-smi case.
1890 break;
1891 }
1892 case Token::BIT_OR:
1893 case Token::BIT_AND:
1894 case Token::BIT_XOR:
1895 case Token::SAR:
1896 case Token::SHL:
1897 case Token::SHR: {
1898 GenerateRegisterArgsPush(masm);
1899 Label not_floats;
1900 Label not_int32;
1901 Label non_smi_result;
1902 /* {
1903 CpuFeatures::Scope use_sse2(SSE2);
1904 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1905 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1906 }*/
1907 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1908 use_sse3_,
1909 &not_floats);
1910 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1911 &not_int32);
1912 switch (op_) {
1913 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1914 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1915 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1916 case Token::SAR: __ sar_cl(eax); break;
1917 case Token::SHL: __ shl_cl(eax); break;
1918 case Token::SHR: __ shr_cl(eax); break;
1919 default: UNREACHABLE();
1920 }
1921 if (op_ == Token::SHR) {
1922 // Check if result is non-negative and fits in a smi.
1923 __ test(eax, Immediate(0xc0000000));
1924 __ j(not_zero, &call_runtime);
1925 } else {
1926 // Check if result fits in a smi.
1927 __ cmp(eax, 0xc0000000);
1928 __ j(negative, &non_smi_result);
1929 }
1930 // Tag smi result and return.
1931 __ SmiTag(eax);
1932 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1933
1934 // All ops except SHR return a signed int32 that we load in
1935 // a HeapNumber.
1936 if (op_ != Token::SHR) {
1937 __ bind(&non_smi_result);
1938 // Allocate a heap number if needed.
1939 __ mov(ebx, Operand(eax)); // ebx: result
1940 NearLabel skip_allocation;
1941 switch (mode_) {
1942 case OVERWRITE_LEFT:
1943 case OVERWRITE_RIGHT:
1944 // If the operand was an object, we skip the
1945 // allocation of a heap number.
1946 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1947 1 * kPointerSize : 2 * kPointerSize));
1948 __ test(eax, Immediate(kSmiTagMask));
1949 __ j(not_zero, &skip_allocation, not_taken);
1950 // Fall through!
1951 case NO_OVERWRITE:
1952 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1953 __ bind(&skip_allocation);
1954 break;
1955 default: UNREACHABLE();
1956 }
1957 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001958 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001959 CpuFeatures::Scope use_sse2(SSE2);
1960 __ cvtsi2sd(xmm0, Operand(ebx));
1961 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1962 } else {
1963 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1964 __ fild_s(Operand(esp, 1 * kPointerSize));
1965 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1966 }
1967 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1968 }
1969
1970 __ bind(&not_floats);
1971 __ bind(&not_int32);
1972 GenerateTypeTransitionWithSavedArgs(masm);
1973 break;
1974 }
1975 default: UNREACHABLE(); break;
1976 }
1977
1978 // If an allocation fails, or SHR or MOD hit a hard case,
1979 // use the runtime system to get the correct result.
1980 __ bind(&call_runtime);
1981
1982 switch (op_) {
1983 case Token::ADD:
1984 GenerateRegisterArgsPush(masm);
1985 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1986 break;
1987 case Token::SUB:
1988 GenerateRegisterArgsPush(masm);
1989 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1990 break;
1991 case Token::MUL:
1992 GenerateRegisterArgsPush(masm);
1993 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1994 break;
1995 case Token::DIV:
1996 GenerateRegisterArgsPush(masm);
1997 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1998 break;
1999 case Token::MOD:
2000 GenerateRegisterArgsPush(masm);
2001 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2002 break;
2003 case Token::BIT_OR:
2004 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2005 break;
2006 case Token::BIT_AND:
2007 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2008 break;
2009 case Token::BIT_XOR:
2010 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2011 break;
2012 case Token::SAR:
2013 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2014 break;
2015 case Token::SHL:
2016 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2017 break;
2018 case Token::SHR:
2019 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2020 break;
2021 default:
2022 UNREACHABLE();
2023 }
2024}
2025
2026
2027void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
2028 Label call_runtime;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002029 ASSERT(operands_type_ == TRBinaryOpIC::HEAP_NUMBER);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002030
2031 // Floating point case.
2032 switch (op_) {
2033 case Token::ADD:
2034 case Token::SUB:
2035 case Token::MUL:
2036 case Token::DIV: {
2037 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002038 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002039 CpuFeatures::Scope use_sse2(SSE2);
2040 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2041
2042 switch (op_) {
2043 case Token::ADD: __ addsd(xmm0, xmm1); break;
2044 case Token::SUB: __ subsd(xmm0, xmm1); break;
2045 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2046 case Token::DIV: __ divsd(xmm0, xmm1); break;
2047 default: UNREACHABLE();
2048 }
2049 GenerateHeapResultAllocation(masm, &call_runtime);
2050 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2051 __ ret(0);
2052 } else { // SSE2 not available, use FPU.
2053 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2054 FloatingPointHelper::LoadFloatOperands(
2055 masm,
2056 ecx,
2057 FloatingPointHelper::ARGS_IN_REGISTERS);
2058 switch (op_) {
2059 case Token::ADD: __ faddp(1); break;
2060 case Token::SUB: __ fsubp(1); break;
2061 case Token::MUL: __ fmulp(1); break;
2062 case Token::DIV: __ fdivp(1); break;
2063 default: UNREACHABLE();
2064 }
2065 Label after_alloc_failure;
2066 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2067 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2068 __ ret(0);
2069 __ bind(&after_alloc_failure);
2070 __ ffree();
2071 __ jmp(&call_runtime);
2072 }
2073
2074 __ bind(&not_floats);
2075 GenerateTypeTransition(masm);
2076 break;
2077 }
2078
2079 case Token::MOD: {
2080 // For MOD we go directly to runtime in the non-smi case.
2081 break;
2082 }
2083 case Token::BIT_OR:
2084 case Token::BIT_AND:
2085 case Token::BIT_XOR:
2086 case Token::SAR:
2087 case Token::SHL:
2088 case Token::SHR: {
2089 GenerateRegisterArgsPush(masm);
2090 Label not_floats;
2091 Label non_smi_result;
2092 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2093 use_sse3_,
2094 &not_floats);
2095 switch (op_) {
2096 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2097 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2098 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2099 case Token::SAR: __ sar_cl(eax); break;
2100 case Token::SHL: __ shl_cl(eax); break;
2101 case Token::SHR: __ shr_cl(eax); break;
2102 default: UNREACHABLE();
2103 }
2104 if (op_ == Token::SHR) {
2105 // Check if result is non-negative and fits in a smi.
2106 __ test(eax, Immediate(0xc0000000));
2107 __ j(not_zero, &call_runtime);
2108 } else {
2109 // Check if result fits in a smi.
2110 __ cmp(eax, 0xc0000000);
2111 __ j(negative, &non_smi_result);
2112 }
2113 // Tag smi result and return.
2114 __ SmiTag(eax);
2115 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2116
2117 // All ops except SHR return a signed int32 that we load in
2118 // a HeapNumber.
2119 if (op_ != Token::SHR) {
2120 __ bind(&non_smi_result);
2121 // Allocate a heap number if needed.
2122 __ mov(ebx, Operand(eax)); // ebx: result
2123 NearLabel skip_allocation;
2124 switch (mode_) {
2125 case OVERWRITE_LEFT:
2126 case OVERWRITE_RIGHT:
2127 // If the operand was an object, we skip the
2128 // allocation of a heap number.
2129 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2130 1 * kPointerSize : 2 * kPointerSize));
2131 __ test(eax, Immediate(kSmiTagMask));
2132 __ j(not_zero, &skip_allocation, not_taken);
2133 // Fall through!
2134 case NO_OVERWRITE:
2135 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2136 __ bind(&skip_allocation);
2137 break;
2138 default: UNREACHABLE();
2139 }
2140 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002141 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002142 CpuFeatures::Scope use_sse2(SSE2);
2143 __ cvtsi2sd(xmm0, Operand(ebx));
2144 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2145 } else {
2146 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2147 __ fild_s(Operand(esp, 1 * kPointerSize));
2148 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2149 }
2150 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2151 }
2152
2153 __ bind(&not_floats);
2154 GenerateTypeTransitionWithSavedArgs(masm);
2155 break;
2156 }
2157 default: UNREACHABLE(); break;
2158 }
2159
2160 // If an allocation fails, or SHR or MOD hit a hard case,
2161 // use the runtime system to get the correct result.
2162 __ bind(&call_runtime);
2163
2164 switch (op_) {
2165 case Token::ADD:
2166 GenerateRegisterArgsPush(masm);
2167 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2168 break;
2169 case Token::SUB:
2170 GenerateRegisterArgsPush(masm);
2171 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2172 break;
2173 case Token::MUL:
2174 GenerateRegisterArgsPush(masm);
2175 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2176 break;
2177 case Token::DIV:
2178 GenerateRegisterArgsPush(masm);
2179 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2180 break;
2181 case Token::MOD:
2182 GenerateRegisterArgsPush(masm);
2183 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2184 break;
2185 case Token::BIT_OR:
2186 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2187 break;
2188 case Token::BIT_AND:
2189 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2190 break;
2191 case Token::BIT_XOR:
2192 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2193 break;
2194 case Token::SAR:
2195 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2196 break;
2197 case Token::SHL:
2198 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2199 break;
2200 case Token::SHR:
2201 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2202 break;
2203 default:
2204 UNREACHABLE();
2205 }
2206}
2207
2208
2209void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
2210 Label call_runtime;
2211
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002212 Counters* counters = masm->isolate()->counters();
2213 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002214
2215 switch (op_) {
2216 case Token::ADD:
2217 case Token::SUB:
2218 case Token::MUL:
2219 case Token::DIV:
2220 break;
2221 case Token::MOD:
2222 case Token::BIT_OR:
2223 case Token::BIT_AND:
2224 case Token::BIT_XOR:
2225 case Token::SAR:
2226 case Token::SHL:
2227 case Token::SHR:
2228 GenerateRegisterArgsPush(masm);
2229 break;
2230 default:
2231 UNREACHABLE();
2232 }
2233
2234 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
2235
2236 // Floating point case.
2237 switch (op_) {
2238 case Token::ADD:
2239 case Token::SUB:
2240 case Token::MUL:
2241 case Token::DIV: {
2242 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002243 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002244 CpuFeatures::Scope use_sse2(SSE2);
2245 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2246
2247 switch (op_) {
2248 case Token::ADD: __ addsd(xmm0, xmm1); break;
2249 case Token::SUB: __ subsd(xmm0, xmm1); break;
2250 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2251 case Token::DIV: __ divsd(xmm0, xmm1); break;
2252 default: UNREACHABLE();
2253 }
2254 GenerateHeapResultAllocation(masm, &call_runtime);
2255 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2256 __ ret(0);
2257 } else { // SSE2 not available, use FPU.
2258 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2259 FloatingPointHelper::LoadFloatOperands(
2260 masm,
2261 ecx,
2262 FloatingPointHelper::ARGS_IN_REGISTERS);
2263 switch (op_) {
2264 case Token::ADD: __ faddp(1); break;
2265 case Token::SUB: __ fsubp(1); break;
2266 case Token::MUL: __ fmulp(1); break;
2267 case Token::DIV: __ fdivp(1); break;
2268 default: UNREACHABLE();
2269 }
2270 Label after_alloc_failure;
2271 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2272 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2273 __ ret(0);
2274 __ bind(&after_alloc_failure);
2275 __ ffree();
2276 __ jmp(&call_runtime);
2277 }
2278 __ bind(&not_floats);
2279 break;
2280 }
2281 case Token::MOD: {
2282 // For MOD we go directly to runtime in the non-smi case.
2283 break;
2284 }
2285 case Token::BIT_OR:
2286 case Token::BIT_AND:
2287 case Token::BIT_XOR:
2288 case Token::SAR:
2289 case Token::SHL:
2290 case Token::SHR: {
2291 Label non_smi_result;
2292 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2293 use_sse3_,
2294 &call_runtime);
2295 switch (op_) {
2296 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2297 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2298 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2299 case Token::SAR: __ sar_cl(eax); break;
2300 case Token::SHL: __ shl_cl(eax); break;
2301 case Token::SHR: __ shr_cl(eax); break;
2302 default: UNREACHABLE();
2303 }
2304 if (op_ == Token::SHR) {
2305 // Check if result is non-negative and fits in a smi.
2306 __ test(eax, Immediate(0xc0000000));
2307 __ j(not_zero, &call_runtime);
2308 } else {
2309 // Check if result fits in a smi.
2310 __ cmp(eax, 0xc0000000);
2311 __ j(negative, &non_smi_result);
2312 }
2313 // Tag smi result and return.
2314 __ SmiTag(eax);
2315 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
2316
2317 // All ops except SHR return a signed int32 that we load in
2318 // a HeapNumber.
2319 if (op_ != Token::SHR) {
2320 __ bind(&non_smi_result);
2321 // Allocate a heap number if needed.
2322 __ mov(ebx, Operand(eax)); // ebx: result
2323 NearLabel skip_allocation;
2324 switch (mode_) {
2325 case OVERWRITE_LEFT:
2326 case OVERWRITE_RIGHT:
2327 // If the operand was an object, we skip the
2328 // allocation of a heap number.
2329 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2330 1 * kPointerSize : 2 * kPointerSize));
2331 __ test(eax, Immediate(kSmiTagMask));
2332 __ j(not_zero, &skip_allocation, not_taken);
2333 // Fall through!
2334 case NO_OVERWRITE:
2335 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2336 __ bind(&skip_allocation);
2337 break;
2338 default: UNREACHABLE();
2339 }
2340 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002341 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002342 CpuFeatures::Scope use_sse2(SSE2);
2343 __ cvtsi2sd(xmm0, Operand(ebx));
2344 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2345 } else {
2346 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2347 __ fild_s(Operand(esp, 1 * kPointerSize));
2348 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2349 }
2350 __ ret(2 * kPointerSize);
2351 }
2352 break;
2353 }
2354 default: UNREACHABLE(); break;
2355 }
2356
2357 // If all else fails, use the runtime system to get the correct
2358 // result.
2359 __ bind(&call_runtime);
2360 switch (op_) {
2361 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002362 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002363 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002364 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2365 break;
2366 }
2367 case Token::SUB:
2368 GenerateRegisterArgsPush(masm);
2369 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2370 break;
2371 case Token::MUL:
2372 GenerateRegisterArgsPush(masm);
2373 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2374 break;
2375 case Token::DIV:
2376 GenerateRegisterArgsPush(masm);
2377 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2378 break;
2379 case Token::MOD:
2380 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2381 break;
2382 case Token::BIT_OR:
2383 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2384 break;
2385 case Token::BIT_AND:
2386 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2387 break;
2388 case Token::BIT_XOR:
2389 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2390 break;
2391 case Token::SAR:
2392 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2393 break;
2394 case Token::SHL:
2395 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2396 break;
2397 case Token::SHR:
2398 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2399 break;
2400 default:
2401 UNREACHABLE();
2402 }
2403}
2404
2405
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002406void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002407 ASSERT(op_ == Token::ADD);
2408 NearLabel left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002409
2410 // Registers containing left and right operands respectively.
2411 Register left = edx;
2412 Register right = eax;
2413
2414 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002415 __ test(left, Immediate(kSmiTagMask));
2416 __ j(zero, &left_not_string);
2417 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
2418 __ j(above_equal, &left_not_string);
2419
2420 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2421 GenerateRegisterArgsPush(masm);
2422 __ TailCallStub(&string_add_left_stub);
2423
2424 // Left operand is not a string, test right.
2425 __ bind(&left_not_string);
2426 __ test(right, Immediate(kSmiTagMask));
2427 __ j(zero, &call_runtime);
2428 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
2429 __ j(above_equal, &call_runtime);
2430
2431 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2432 GenerateRegisterArgsPush(masm);
2433 __ TailCallStub(&string_add_right_stub);
2434
2435 // Neither argument is a string.
2436 __ bind(&call_runtime);
2437}
2438
2439
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002440void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2441 MacroAssembler* masm,
2442 Label* alloc_failure) {
2443 Label skip_allocation;
2444 OverwriteMode mode = mode_;
2445 switch (mode) {
2446 case OVERWRITE_LEFT: {
2447 // If the argument in edx is already an object, we skip the
2448 // allocation of a heap number.
2449 __ test(edx, Immediate(kSmiTagMask));
2450 __ j(not_zero, &skip_allocation, not_taken);
2451 // Allocate a heap number for the result. Keep eax and edx intact
2452 // for the possible runtime call.
2453 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2454 // Now edx can be overwritten losing one of the arguments as we are
2455 // now done and will not need it any more.
2456 __ mov(edx, Operand(ebx));
2457 __ bind(&skip_allocation);
2458 // Use object in edx as a result holder
2459 __ mov(eax, Operand(edx));
2460 break;
2461 }
2462 case OVERWRITE_RIGHT:
2463 // If the argument in eax is already an object, we skip the
2464 // allocation of a heap number.
2465 __ test(eax, Immediate(kSmiTagMask));
2466 __ j(not_zero, &skip_allocation, not_taken);
2467 // Fall through!
2468 case NO_OVERWRITE:
2469 // Allocate a heap number for the result. Keep eax and edx intact
2470 // for the possible runtime call.
2471 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2472 // Now eax can be overwritten losing one of the arguments as we are
2473 // now done and will not need it any more.
2474 __ mov(eax, ebx);
2475 __ bind(&skip_allocation);
2476 break;
2477 default: UNREACHABLE();
2478 }
2479}
2480
2481
2482void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2483 __ pop(ecx);
2484 __ push(edx);
2485 __ push(eax);
2486 __ push(ecx);
2487}
2488
2489
ricow@chromium.org65fae842010-08-25 15:26:24 +00002490void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002491 // TAGGED case:
2492 // Input:
2493 // esp[4]: tagged number input argument (should be number).
2494 // esp[0]: return address.
2495 // Output:
2496 // eax: tagged double result.
2497 // UNTAGGED case:
2498 // Input::
2499 // esp[0]: return address.
2500 // xmm1: untagged double input argument
2501 // Output:
2502 // xmm1: untagged double result.
2503
ricow@chromium.org65fae842010-08-25 15:26:24 +00002504 Label runtime_call;
2505 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002506 Label skip_cache;
2507 const bool tagged = (argument_type_ == TAGGED);
2508 if (tagged) {
2509 // Test that eax is a number.
2510 NearLabel input_not_smi;
2511 NearLabel loaded;
2512 __ mov(eax, Operand(esp, kPointerSize));
2513 __ test(eax, Immediate(kSmiTagMask));
2514 __ j(not_zero, &input_not_smi);
2515 // Input is a smi. Untag and load it onto the FPU stack.
2516 // Then load the low and high words of the double into ebx, edx.
2517 STATIC_ASSERT(kSmiTagSize == 1);
2518 __ sar(eax, 1);
2519 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2520 __ mov(Operand(esp, 0), eax);
2521 __ fild_s(Operand(esp, 0));
2522 __ fst_d(Operand(esp, 0));
2523 __ pop(edx);
2524 __ pop(ebx);
2525 __ jmp(&loaded);
2526 __ bind(&input_not_smi);
2527 // Check if input is a HeapNumber.
2528 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002529 Factory* factory = masm->isolate()->factory();
2530 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002531 __ j(not_equal, &runtime_call);
2532 // Input is a HeapNumber. Push it on the FPU stack and load its
2533 // low and high words into ebx, edx.
2534 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2535 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2536 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002537
whesse@chromium.org023421e2010-12-21 12:19:12 +00002538 __ bind(&loaded);
2539 } else { // UNTAGGED.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002540 if (masm->isolate()->cpu_features()->IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002541 CpuFeatures::Scope sse4_scope(SSE4_1);
2542 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2543 } else {
2544 __ pshufd(xmm0, xmm1, 0x1);
2545 __ movd(Operand(edx), xmm0);
2546 }
2547 __ movd(Operand(ebx), xmm1);
2548 }
2549
2550 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002551 // ebx = low 32 bits of double value
2552 // edx = high 32 bits of double value
2553 // Compute hash (the shifts are arithmetic):
2554 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2555 __ mov(ecx, ebx);
2556 __ xor_(ecx, Operand(edx));
2557 __ mov(eax, ecx);
2558 __ sar(eax, 16);
2559 __ xor_(ecx, Operand(eax));
2560 __ mov(eax, ecx);
2561 __ sar(eax, 8);
2562 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002563 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2564 __ and_(Operand(ecx),
2565 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002566
whesse@chromium.org023421e2010-12-21 12:19:12 +00002567 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002568 // ebx = low 32 bits of double value.
2569 // edx = high 32 bits of double value.
2570 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002571 ExternalReference cache_array =
2572 ExternalReference::transcendental_cache_array_address(masm->isolate());
2573 __ mov(eax, Immediate(cache_array));
2574 int cache_array_index =
2575 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2576 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002577 // Eax points to the cache for the type type_.
2578 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2579 __ test(eax, Operand(eax));
2580 __ j(zero, &runtime_call_clear_stack);
2581#ifdef DEBUG
2582 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002583 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002584 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2585 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2586 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2587 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2588 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2589 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2590 CHECK_EQ(0, elem_in0 - elem_start);
2591 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2592 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2593 }
2594#endif
2595 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2596 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2597 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2598 // Check if cache matches: Double value is stored in uint32_t[2] array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002599 NearLabel cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002600 __ cmp(ebx, Operand(ecx, 0));
2601 __ j(not_equal, &cache_miss);
2602 __ cmp(edx, Operand(ecx, kIntSize));
2603 __ j(not_equal, &cache_miss);
2604 // Cache hit!
2605 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002606 if (tagged) {
2607 __ fstp(0);
2608 __ ret(kPointerSize);
2609 } else { // UNTAGGED.
2610 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2611 __ Ret();
2612 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002613
2614 __ bind(&cache_miss);
2615 // Update cache with new value.
2616 // We are short on registers, so use no_reg as scratch.
2617 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002618 if (tagged) {
2619 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2620 } else { // UNTAGGED.
2621 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2622 __ sub(Operand(esp), Immediate(kDoubleSize));
2623 __ movdbl(Operand(esp, 0), xmm1);
2624 __ fld_d(Operand(esp, 0));
2625 __ add(Operand(esp), Immediate(kDoubleSize));
2626 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002627 GenerateOperation(masm);
2628 __ mov(Operand(ecx, 0), ebx);
2629 __ mov(Operand(ecx, kIntSize), edx);
2630 __ mov(Operand(ecx, 2 * kIntSize), eax);
2631 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002632 if (tagged) {
2633 __ ret(kPointerSize);
2634 } else { // UNTAGGED.
2635 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2636 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002637
whesse@chromium.org023421e2010-12-21 12:19:12 +00002638 // Skip cache and return answer directly, only in untagged case.
2639 __ bind(&skip_cache);
2640 __ sub(Operand(esp), Immediate(kDoubleSize));
2641 __ movdbl(Operand(esp, 0), xmm1);
2642 __ fld_d(Operand(esp, 0));
2643 GenerateOperation(masm);
2644 __ fstp_d(Operand(esp, 0));
2645 __ movdbl(xmm1, Operand(esp, 0));
2646 __ add(Operand(esp), Immediate(kDoubleSize));
2647 // We return the value in xmm1 without adding it to the cache, but
2648 // we cause a scavenging GC so that future allocations will succeed.
2649 __ EnterInternalFrame();
2650 // Allocate an unused object bigger than a HeapNumber.
2651 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2652 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2653 __ LeaveInternalFrame();
2654 __ Ret();
2655 }
2656
2657 // Call runtime, doing whatever allocation and cleanup is necessary.
2658 if (tagged) {
2659 __ bind(&runtime_call_clear_stack);
2660 __ fstp(0);
2661 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002662 ExternalReference runtime =
2663 ExternalReference(RuntimeFunction(), masm->isolate());
2664 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002665 } else { // UNTAGGED.
2666 __ bind(&runtime_call_clear_stack);
2667 __ bind(&runtime_call);
2668 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2669 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2670 __ EnterInternalFrame();
2671 __ push(eax);
2672 __ CallRuntime(RuntimeFunction(), 1);
2673 __ LeaveInternalFrame();
2674 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2675 __ Ret();
2676 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002677}
2678
2679
2680Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2681 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002682 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2683 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002684 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002685 default:
2686 UNIMPLEMENTED();
2687 return Runtime::kAbort;
2688 }
2689}
2690
2691
2692void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2693 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002694 // Input value is on FP stack, and also in ebx/edx.
2695 // Input value is possibly in xmm1.
2696 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002697 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2698 // Both fsin and fcos require arguments in the range +/-2^63 and
2699 // return NaN for infinities and NaN. They can share all code except
2700 // the actual fsin/fcos operation.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002701 NearLabel in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002702 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2703 // work. We must reduce it to the appropriate range.
2704 __ mov(edi, edx);
2705 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2706 int supported_exponent_limit =
2707 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2708 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
2709 __ j(below, &in_range, taken);
2710 // Check for infinity and NaN. Both return NaN for sin.
2711 __ cmp(Operand(edi), Immediate(0x7ff00000));
2712 NearLabel non_nan_result;
2713 __ j(not_equal, &non_nan_result, taken);
2714 // Input is +/-Infinity or NaN. Result is NaN.
2715 __ fstp(0);
2716 // NaN is represented by 0x7ff8000000000000.
2717 __ push(Immediate(0x7ff80000));
2718 __ push(Immediate(0));
2719 __ fld_d(Operand(esp, 0));
2720 __ add(Operand(esp), Immediate(2 * kPointerSize));
2721 __ jmp(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002722
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002723 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002724
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002725 // Use fpmod to restrict argument to the range +/-2*PI.
2726 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2727 __ fldpi();
2728 __ fadd(0);
2729 __ fld(1);
2730 // FPU Stack: input, 2*pi, input.
2731 {
2732 NearLabel no_exceptions;
2733 __ fwait();
2734 __ fnstsw_ax();
2735 // Clear if Illegal Operand or Zero Division exceptions are set.
2736 __ test(Operand(eax), Immediate(5));
2737 __ j(zero, &no_exceptions);
2738 __ fnclex();
2739 __ bind(&no_exceptions);
2740 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002741
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002742 // Compute st(0) % st(1)
2743 {
2744 NearLabel partial_remainder_loop;
2745 __ bind(&partial_remainder_loop);
2746 __ fprem1();
2747 __ fwait();
2748 __ fnstsw_ax();
2749 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2750 // If C2 is set, computation only has partial result. Loop to
2751 // continue computation.
2752 __ j(not_zero, &partial_remainder_loop);
2753 }
2754 // FPU Stack: input, 2*pi, input % 2*pi
2755 __ fstp(2);
2756 __ fstp(0);
2757 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2758
2759 // FPU Stack: input % 2*pi
2760 __ bind(&in_range);
2761 switch (type_) {
2762 case TranscendentalCache::SIN:
2763 __ fsin();
2764 break;
2765 case TranscendentalCache::COS:
2766 __ fcos();
2767 break;
2768 default:
2769 UNREACHABLE();
2770 }
2771 __ bind(&done);
2772 } else {
2773 ASSERT(type_ == TranscendentalCache::LOG);
2774 __ fldln2();
2775 __ fxch();
2776 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002777 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002778}
2779
2780
2781// Get the integer part of a heap number. Surprisingly, all this bit twiddling
2782// is faster than using the built-in instructions on floating point registers.
2783// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
2784// trashed registers.
2785void IntegerConvert(MacroAssembler* masm,
2786 Register source,
2787 TypeInfo type_info,
2788 bool use_sse3,
2789 Label* conversion_failure) {
2790 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
2791 Label done, right_exponent, normal_exponent;
2792 Register scratch = ebx;
2793 Register scratch2 = edi;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002794 if (type_info.IsInteger32() &&
2795 masm->isolate()->cpu_features()->IsEnabled(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002796 CpuFeatures::Scope scope(SSE2);
2797 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
2798 return;
2799 }
2800 if (!type_info.IsInteger32() || !use_sse3) {
2801 // Get exponent word.
2802 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
2803 // Get exponent alone in scratch2.
2804 __ mov(scratch2, scratch);
2805 __ and_(scratch2, HeapNumber::kExponentMask);
2806 }
2807 if (use_sse3) {
2808 CpuFeatures::Scope scope(SSE3);
2809 if (!type_info.IsInteger32()) {
2810 // Check whether the exponent is too big for a 64 bit signed integer.
2811 static const uint32_t kTooBigExponent =
2812 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2813 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
2814 __ j(greater_equal, conversion_failure);
2815 }
2816 // Load x87 register with heap number.
2817 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
2818 // Reserve space for 64 bit answer.
2819 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2820 // Do conversion, which cannot fail because we checked the exponent.
2821 __ fisttp_d(Operand(esp, 0));
2822 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
2823 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2824 } else {
2825 // Load ecx with zero. We use this either for the final shift or
2826 // for the answer.
2827 __ xor_(ecx, Operand(ecx));
2828 // Check whether the exponent matches a 32 bit signed int that cannot be
2829 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
2830 // exponent is 30 (biased). This is the exponent that we are fastest at and
2831 // also the highest exponent we can handle here.
2832 const uint32_t non_smi_exponent =
2833 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
2834 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
2835 // If we have a match of the int32-but-not-Smi exponent then skip some
2836 // logic.
2837 __ j(equal, &right_exponent);
2838 // If the exponent is higher than that then go to slow case. This catches
2839 // numbers that don't fit in a signed int32, infinities and NaNs.
2840 __ j(less, &normal_exponent);
2841
2842 {
2843 // Handle a big exponent. The only reason we have this code is that the
2844 // >>> operator has a tendency to generate numbers with an exponent of 31.
2845 const uint32_t big_non_smi_exponent =
2846 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
2847 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
2848 __ j(not_equal, conversion_failure);
2849 // We have the big exponent, typically from >>>. This means the number is
2850 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
2851 __ mov(scratch2, scratch);
2852 __ and_(scratch2, HeapNumber::kMantissaMask);
2853 // Put back the implicit 1.
2854 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
2855 // Shift up the mantissa bits to take up the space the exponent used to
2856 // take. We just orred in the implicit bit so that took care of one and
2857 // we want to use the full unsigned range so we subtract 1 bit from the
2858 // shift distance.
2859 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
2860 __ shl(scratch2, big_shift_distance);
2861 // Get the second half of the double.
2862 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
2863 // Shift down 21 bits to get the most significant 11 bits or the low
2864 // mantissa word.
2865 __ shr(ecx, 32 - big_shift_distance);
2866 __ or_(ecx, Operand(scratch2));
2867 // We have the answer in ecx, but we may need to negate it.
2868 __ test(scratch, Operand(scratch));
2869 __ j(positive, &done);
2870 __ neg(ecx);
2871 __ jmp(&done);
2872 }
2873
2874 __ bind(&normal_exponent);
2875 // Exponent word in scratch, exponent part of exponent word in scratch2.
2876 // Zero in ecx.
2877 // We know the exponent is smaller than 30 (biased). If it is less than
2878 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
2879 // it rounds to zero.
2880 const uint32_t zero_exponent =
2881 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
2882 __ sub(Operand(scratch2), Immediate(zero_exponent));
2883 // ecx already has a Smi zero.
2884 __ j(less, &done);
2885
2886 // We have a shifted exponent between 0 and 30 in scratch2.
2887 __ shr(scratch2, HeapNumber::kExponentShift);
2888 __ mov(ecx, Immediate(30));
2889 __ sub(ecx, Operand(scratch2));
2890
2891 __ bind(&right_exponent);
2892 // Here ecx is the shift, scratch is the exponent word.
2893 // Get the top bits of the mantissa.
2894 __ and_(scratch, HeapNumber::kMantissaMask);
2895 // Put back the implicit 1.
2896 __ or_(scratch, 1 << HeapNumber::kExponentShift);
2897 // Shift up the mantissa bits to take up the space the exponent used to
2898 // take. We have kExponentShift + 1 significant bits int he low end of the
2899 // word. Shift them to the top bits.
2900 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2901 __ shl(scratch, shift_distance);
2902 // Get the second half of the double. For some exponents we don't
2903 // actually need this because the bits get shifted out again, but
2904 // it's probably slower to test than just to do it.
2905 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
2906 // Shift down 22 bits to get the most significant 10 bits or the low
2907 // mantissa word.
2908 __ shr(scratch2, 32 - shift_distance);
2909 __ or_(scratch2, Operand(scratch));
2910 // Move down according to the exponent.
2911 __ shr_cl(scratch2);
2912 // Now the unsigned answer is in scratch2. We need to move it to ecx and
2913 // we may need to fix the sign.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002914 NearLabel negative;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002915 __ xor_(ecx, Operand(ecx));
2916 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
2917 __ j(greater, &negative);
2918 __ mov(ecx, scratch2);
2919 __ jmp(&done);
2920 __ bind(&negative);
2921 __ sub(ecx, Operand(scratch2));
2922 __ bind(&done);
2923 }
2924}
2925
2926
2927// Input: edx, eax are the left and right objects of a bit op.
2928// Output: eax, ecx are left and right integers for a bit op.
2929void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
2930 TypeInfo type_info,
2931 bool use_sse3,
2932 Label* conversion_failure) {
2933 // Check float operands.
2934 Label arg1_is_object, check_undefined_arg1;
2935 Label arg2_is_object, check_undefined_arg2;
2936 Label load_arg2, done;
2937
2938 if (!type_info.IsDouble()) {
2939 if (!type_info.IsSmi()) {
2940 __ test(edx, Immediate(kSmiTagMask));
2941 __ j(not_zero, &arg1_is_object);
2942 } else {
2943 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
2944 }
2945 __ SmiUntag(edx);
2946 __ jmp(&load_arg2);
2947 }
2948
2949 __ bind(&arg1_is_object);
2950
2951 // Get the untagged integer version of the edx heap number in ecx.
2952 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
2953 __ mov(edx, ecx);
2954
2955 // Here edx has the untagged integer, eax has a Smi or a heap number.
2956 __ bind(&load_arg2);
2957 if (!type_info.IsDouble()) {
2958 // Test if arg2 is a Smi.
2959 if (!type_info.IsSmi()) {
2960 __ test(eax, Immediate(kSmiTagMask));
2961 __ j(not_zero, &arg2_is_object);
2962 } else {
2963 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
2964 }
2965 __ SmiUntag(eax);
2966 __ mov(ecx, eax);
2967 __ jmp(&done);
2968 }
2969
2970 __ bind(&arg2_is_object);
2971
2972 // Get the untagged integer version of the eax heap number in ecx.
2973 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
2974 __ bind(&done);
2975 __ mov(eax, edx);
2976}
2977
2978
2979// Input: edx, eax are the left and right objects of a bit op.
2980// Output: eax, ecx are left and right integers for a bit op.
2981void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2982 bool use_sse3,
2983 Label* conversion_failure) {
2984 // Check float operands.
2985 Label arg1_is_object, check_undefined_arg1;
2986 Label arg2_is_object, check_undefined_arg2;
2987 Label load_arg2, done;
2988
2989 // Test if arg1 is a Smi.
2990 __ test(edx, Immediate(kSmiTagMask));
2991 __ j(not_zero, &arg1_is_object);
2992
2993 __ SmiUntag(edx);
2994 __ jmp(&load_arg2);
2995
2996 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2997 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002998 Factory* factory = masm->isolate()->factory();
2999 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003000 __ j(not_equal, conversion_failure);
3001 __ mov(edx, Immediate(0));
3002 __ jmp(&load_arg2);
3003
3004 __ bind(&arg1_is_object);
3005 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003006 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003007 __ j(not_equal, &check_undefined_arg1);
3008
3009 // Get the untagged integer version of the edx heap number in ecx.
3010 IntegerConvert(masm,
3011 edx,
3012 TypeInfo::Unknown(),
3013 use_sse3,
3014 conversion_failure);
3015 __ mov(edx, ecx);
3016
3017 // Here edx has the untagged integer, eax has a Smi or a heap number.
3018 __ bind(&load_arg2);
3019
3020 // Test if arg2 is a Smi.
3021 __ test(eax, Immediate(kSmiTagMask));
3022 __ j(not_zero, &arg2_is_object);
3023
3024 __ SmiUntag(eax);
3025 __ mov(ecx, eax);
3026 __ jmp(&done);
3027
3028 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
3029 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003030 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003031 __ j(not_equal, conversion_failure);
3032 __ mov(ecx, Immediate(0));
3033 __ jmp(&done);
3034
3035 __ bind(&arg2_is_object);
3036 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003037 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003038 __ j(not_equal, &check_undefined_arg2);
3039
3040 // Get the untagged integer version of the eax heap number in ecx.
3041 IntegerConvert(masm,
3042 eax,
3043 TypeInfo::Unknown(),
3044 use_sse3,
3045 conversion_failure);
3046 __ bind(&done);
3047 __ mov(eax, edx);
3048}
3049
3050
3051void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
3052 TypeInfo type_info,
3053 bool use_sse3,
3054 Label* conversion_failure) {
3055 if (type_info.IsNumber()) {
3056 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
3057 } else {
3058 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
3059 }
3060}
3061
3062
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003063void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
3064 bool use_sse3,
3065 Label* not_int32) {
3066 return;
3067}
3068
3069
ricow@chromium.org65fae842010-08-25 15:26:24 +00003070void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
3071 Register number) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003072 NearLabel load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003073
3074 __ test(number, Immediate(kSmiTagMask));
3075 __ j(zero, &load_smi, not_taken);
3076 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
3077 __ jmp(&done);
3078
3079 __ bind(&load_smi);
3080 __ SmiUntag(number);
3081 __ push(number);
3082 __ fild_s(Operand(esp, 0));
3083 __ pop(number);
3084
3085 __ bind(&done);
3086}
3087
3088
3089void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003090 NearLabel load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003091 // Load operand in edx into xmm0.
3092 __ test(edx, Immediate(kSmiTagMask));
3093 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
3094 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3095
3096 __ bind(&load_eax);
3097 // Load operand in eax into xmm1.
3098 __ test(eax, Immediate(kSmiTagMask));
3099 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
3100 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3101 __ jmp(&done);
3102
3103 __ bind(&load_smi_edx);
3104 __ SmiUntag(edx); // Untag smi before converting to float.
3105 __ cvtsi2sd(xmm0, Operand(edx));
3106 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3107 __ jmp(&load_eax);
3108
3109 __ bind(&load_smi_eax);
3110 __ SmiUntag(eax); // Untag smi before converting to float.
3111 __ cvtsi2sd(xmm1, Operand(eax));
3112 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3113
3114 __ bind(&done);
3115}
3116
3117
3118void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
3119 Label* not_numbers) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003120 NearLabel load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003121 // Load operand in edx into xmm0, or branch to not_numbers.
3122 __ test(edx, Immediate(kSmiTagMask));
3123 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003124 Factory* factory = masm->isolate()->factory();
3125 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003126 __ j(not_equal, not_numbers); // Argument in edx is not a number.
3127 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3128 __ bind(&load_eax);
3129 // Load operand in eax into xmm1, or branch to not_numbers.
3130 __ test(eax, Immediate(kSmiTagMask));
3131 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003132 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003133 __ j(equal, &load_float_eax);
3134 __ jmp(not_numbers); // Argument in eax is not a number.
3135 __ bind(&load_smi_edx);
3136 __ SmiUntag(edx); // Untag smi before converting to float.
3137 __ cvtsi2sd(xmm0, Operand(edx));
3138 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3139 __ jmp(&load_eax);
3140 __ bind(&load_smi_eax);
3141 __ SmiUntag(eax); // Untag smi before converting to float.
3142 __ cvtsi2sd(xmm1, Operand(eax));
3143 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3144 __ jmp(&done);
3145 __ bind(&load_float_eax);
3146 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3147 __ bind(&done);
3148}
3149
3150
3151void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
3152 Register scratch) {
3153 const Register left = edx;
3154 const Register right = eax;
3155 __ mov(scratch, left);
3156 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3157 __ SmiUntag(scratch);
3158 __ cvtsi2sd(xmm0, Operand(scratch));
3159
3160 __ mov(scratch, right);
3161 __ SmiUntag(scratch);
3162 __ cvtsi2sd(xmm1, Operand(scratch));
3163}
3164
3165
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003166void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
3167 Label* non_int32,
3168 Register scratch) {
3169 __ cvttsd2si(scratch, Operand(xmm0));
3170 __ cvtsi2sd(xmm2, Operand(scratch));
3171 __ ucomisd(xmm0, xmm2);
3172 __ j(not_zero, non_int32);
3173 __ j(carry, non_int32);
3174 __ cvttsd2si(scratch, Operand(xmm1));
3175 __ cvtsi2sd(xmm2, Operand(scratch));
3176 __ ucomisd(xmm1, xmm2);
3177 __ j(not_zero, non_int32);
3178 __ j(carry, non_int32);
3179}
3180
3181
ricow@chromium.org65fae842010-08-25 15:26:24 +00003182void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
3183 Register scratch,
3184 ArgLocation arg_location) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003185 NearLabel load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003186 if (arg_location == ARGS_IN_REGISTERS) {
3187 __ mov(scratch, edx);
3188 } else {
3189 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3190 }
3191 __ test(scratch, Immediate(kSmiTagMask));
3192 __ j(zero, &load_smi_1, not_taken);
3193 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3194 __ bind(&done_load_1);
3195
3196 if (arg_location == ARGS_IN_REGISTERS) {
3197 __ mov(scratch, eax);
3198 } else {
3199 __ mov(scratch, Operand(esp, 1 * kPointerSize));
3200 }
3201 __ test(scratch, Immediate(kSmiTagMask));
3202 __ j(zero, &load_smi_2, not_taken);
3203 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3204 __ jmp(&done);
3205
3206 __ bind(&load_smi_1);
3207 __ SmiUntag(scratch);
3208 __ push(scratch);
3209 __ fild_s(Operand(esp, 0));
3210 __ pop(scratch);
3211 __ jmp(&done_load_1);
3212
3213 __ bind(&load_smi_2);
3214 __ SmiUntag(scratch);
3215 __ push(scratch);
3216 __ fild_s(Operand(esp, 0));
3217 __ pop(scratch);
3218
3219 __ bind(&done);
3220}
3221
3222
3223void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
3224 Register scratch) {
3225 const Register left = edx;
3226 const Register right = eax;
3227 __ mov(scratch, left);
3228 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3229 __ SmiUntag(scratch);
3230 __ push(scratch);
3231 __ fild_s(Operand(esp, 0));
3232
3233 __ mov(scratch, right);
3234 __ SmiUntag(scratch);
3235 __ mov(Operand(esp, 0), scratch);
3236 __ fild_s(Operand(esp, 0));
3237 __ pop(scratch);
3238}
3239
3240
3241void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
3242 Label* non_float,
3243 Register scratch) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003244 NearLabel test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003245 // Test if both operands are floats or smi -> scratch=k_is_float;
3246 // Otherwise scratch = k_not_float.
3247 __ test(edx, Immediate(kSmiTagMask));
3248 __ j(zero, &test_other, not_taken); // argument in edx is OK
3249 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003250 Factory* factory = masm->isolate()->factory();
3251 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003252 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
3253
3254 __ bind(&test_other);
3255 __ test(eax, Immediate(kSmiTagMask));
3256 __ j(zero, &done); // argument in eax is OK
3257 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003258 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003259 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
3260
3261 // Fall-through: Both operands are numbers.
3262 __ bind(&done);
3263}
3264
3265
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003266void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
3267 Label* non_int32) {
3268 return;
3269}
3270
3271
ricow@chromium.org65fae842010-08-25 15:26:24 +00003272void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003273 Label slow, done, undo;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003274
3275 if (op_ == Token::SUB) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003276 if (include_smi_code_) {
3277 // Check whether the value is a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003278 NearLabel try_float;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003279 __ test(eax, Immediate(kSmiTagMask));
3280 __ j(not_zero, &try_float, not_taken);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003281
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003282 if (negative_zero_ == kStrictNegativeZero) {
3283 // Go slow case if the value of the expression is zero
3284 // to make sure that we switch between 0 and -0.
3285 __ test(eax, Operand(eax));
3286 __ j(zero, &slow, not_taken);
3287 }
3288
3289 // The value of the expression is a smi that is not zero. Try
3290 // optimistic subtraction '0 - value'.
3291 __ mov(edx, Operand(eax));
3292 __ Set(eax, Immediate(0));
3293 __ sub(eax, Operand(edx));
3294 __ j(overflow, &undo, not_taken);
3295 __ StubReturn(1);
3296
3297 // Try floating point case.
3298 __ bind(&try_float);
3299 } else if (FLAG_debug_code) {
3300 __ AbortIfSmi(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003301 }
3302
ricow@chromium.org65fae842010-08-25 15:26:24 +00003303 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003304 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003305 __ j(not_equal, &slow);
3306 if (overwrite_ == UNARY_OVERWRITE) {
3307 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
3308 __ xor_(edx, HeapNumber::kSignMask); // Flip sign.
3309 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), edx);
3310 } else {
3311 __ mov(edx, Operand(eax));
3312 // edx: operand
3313 __ AllocateHeapNumber(eax, ebx, ecx, &undo);
3314 // eax: allocated 'empty' number
3315 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3316 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
3317 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
3318 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
3319 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
3320 }
3321 } else if (op_ == Token::BIT_NOT) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003322 if (include_smi_code_) {
3323 Label non_smi;
3324 __ test(eax, Immediate(kSmiTagMask));
3325 __ j(not_zero, &non_smi);
3326 __ not_(eax);
3327 __ and_(eax, ~kSmiTagMask); // Remove inverted smi-tag.
3328 __ ret(0);
3329 __ bind(&non_smi);
3330 } else if (FLAG_debug_code) {
3331 __ AbortIfSmi(eax);
3332 }
3333
ricow@chromium.org65fae842010-08-25 15:26:24 +00003334 // Check if the operand is a heap number.
3335 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003336 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003337 __ j(not_equal, &slow, not_taken);
3338
3339 // Convert the heap number in eax to an untagged integer in ecx.
3340 IntegerConvert(masm,
3341 eax,
3342 TypeInfo::Unknown(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003343 masm->isolate()->cpu_features()->IsSupported(SSE3),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003344 &slow);
3345
3346 // Do the bitwise operation and check if the result fits in a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003347 NearLabel try_float;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003348 __ not_(ecx);
3349 __ cmp(ecx, 0xc0000000);
3350 __ j(sign, &try_float, not_taken);
3351
3352 // Tag the result as a smi and we're done.
3353 STATIC_ASSERT(kSmiTagSize == 1);
3354 __ lea(eax, Operand(ecx, times_2, kSmiTag));
3355 __ jmp(&done);
3356
3357 // Try to store the result in a heap number.
3358 __ bind(&try_float);
3359 if (overwrite_ == UNARY_NO_OVERWRITE) {
3360 // Allocate a fresh heap number, but don't overwrite eax until
3361 // we're sure we can do it without going through the slow case
3362 // that needs the value in eax.
3363 __ AllocateHeapNumber(ebx, edx, edi, &slow);
3364 __ mov(eax, Operand(ebx));
3365 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003366 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003367 CpuFeatures::Scope use_sse2(SSE2);
3368 __ cvtsi2sd(xmm0, Operand(ecx));
3369 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
3370 } else {
3371 __ push(ecx);
3372 __ fild_s(Operand(esp, 0));
3373 __ pop(ecx);
3374 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
3375 }
3376 } else {
3377 UNIMPLEMENTED();
3378 }
3379
3380 // Return from the stub.
3381 __ bind(&done);
3382 __ StubReturn(1);
3383
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003384 // Restore eax and go slow case.
3385 __ bind(&undo);
3386 __ mov(eax, Operand(edx));
3387
ricow@chromium.org65fae842010-08-25 15:26:24 +00003388 // Handle the slow case by jumping to the JavaScript builtin.
3389 __ bind(&slow);
3390 __ pop(ecx); // pop return address.
3391 __ push(eax);
3392 __ push(ecx); // push return address
3393 switch (op_) {
3394 case Token::SUB:
3395 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
3396 break;
3397 case Token::BIT_NOT:
3398 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
3399 break;
3400 default:
3401 UNREACHABLE();
3402 }
3403}
3404
3405
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003406void MathPowStub::Generate(MacroAssembler* masm) {
3407 // Registers are used as follows:
3408 // edx = base
3409 // eax = exponent
3410 // ecx = temporary, result
3411
3412 CpuFeatures::Scope use_sse2(SSE2);
3413 Label allocate_return, call_runtime;
3414
3415 // Load input parameters.
3416 __ mov(edx, Operand(esp, 2 * kPointerSize));
3417 __ mov(eax, Operand(esp, 1 * kPointerSize));
3418
3419 // Save 1 in xmm3 - we need this several times later on.
3420 __ mov(ecx, Immediate(1));
3421 __ cvtsi2sd(xmm3, Operand(ecx));
3422
3423 Label exponent_nonsmi;
3424 Label base_nonsmi;
3425 // If the exponent is a heap number go to that specific case.
3426 __ test(eax, Immediate(kSmiTagMask));
3427 __ j(not_zero, &exponent_nonsmi);
3428 __ test(edx, Immediate(kSmiTagMask));
3429 __ j(not_zero, &base_nonsmi);
3430
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003431 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003432 Label powi;
3433 __ SmiUntag(edx);
3434 __ cvtsi2sd(xmm0, Operand(edx));
3435 __ jmp(&powi);
3436 // exponent is smi and base is a heapnumber.
3437 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003438 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003439 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003440 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003441 __ j(not_equal, &call_runtime);
3442
3443 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3444
3445 // Optimized version of pow if exponent is a smi.
3446 // xmm0 contains the base.
3447 __ bind(&powi);
3448 __ SmiUntag(eax);
3449
3450 // Save exponent in base as we need to check if exponent is negative later.
3451 // We know that base and exponent are in different registers.
3452 __ mov(edx, eax);
3453
3454 // Get absolute value of exponent.
3455 NearLabel no_neg;
3456 __ cmp(eax, 0);
3457 __ j(greater_equal, &no_neg);
3458 __ neg(eax);
3459 __ bind(&no_neg);
3460
3461 // Load xmm1 with 1.
3462 __ movsd(xmm1, xmm3);
3463 NearLabel while_true;
3464 NearLabel no_multiply;
3465
3466 __ bind(&while_true);
3467 __ shr(eax, 1);
3468 __ j(not_carry, &no_multiply);
3469 __ mulsd(xmm1, xmm0);
3470 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003471 __ mulsd(xmm0, xmm0);
3472 __ j(not_zero, &while_true);
3473
3474 // base has the original value of the exponent - if the exponent is
3475 // negative return 1/result.
3476 __ test(edx, Operand(edx));
3477 __ j(positive, &allocate_return);
3478 // Special case if xmm1 has reached infinity.
3479 __ mov(ecx, Immediate(0x7FB00000));
3480 __ movd(xmm0, Operand(ecx));
3481 __ cvtss2sd(xmm0, xmm0);
3482 __ ucomisd(xmm0, xmm1);
3483 __ j(equal, &call_runtime);
3484 __ divsd(xmm3, xmm1);
3485 __ movsd(xmm1, xmm3);
3486 __ jmp(&allocate_return);
3487
3488 // exponent (or both) is a heapnumber - no matter what we should now work
3489 // on doubles.
3490 __ bind(&exponent_nonsmi);
3491 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003492 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003493 __ j(not_equal, &call_runtime);
3494 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3495 // Test if exponent is nan.
3496 __ ucomisd(xmm1, xmm1);
3497 __ j(parity_even, &call_runtime);
3498
3499 NearLabel base_not_smi;
3500 NearLabel handle_special_cases;
3501 __ test(edx, Immediate(kSmiTagMask));
3502 __ j(not_zero, &base_not_smi);
3503 __ SmiUntag(edx);
3504 __ cvtsi2sd(xmm0, Operand(edx));
3505 __ jmp(&handle_special_cases);
3506
3507 __ bind(&base_not_smi);
3508 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003509 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003510 __ j(not_equal, &call_runtime);
3511 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3512 __ and_(ecx, HeapNumber::kExponentMask);
3513 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
3514 // base is NaN or +/-Infinity
3515 __ j(greater_equal, &call_runtime);
3516 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3517
3518 // base is in xmm0 and exponent is in xmm1.
3519 __ bind(&handle_special_cases);
3520 NearLabel not_minus_half;
3521 // Test for -0.5.
3522 // Load xmm2 with -0.5.
3523 __ mov(ecx, Immediate(0xBF000000));
3524 __ movd(xmm2, Operand(ecx));
3525 __ cvtss2sd(xmm2, xmm2);
3526 // xmm2 now has -0.5.
3527 __ ucomisd(xmm2, xmm1);
3528 __ j(not_equal, &not_minus_half);
3529
3530 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003531 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3532 __ xorpd(xmm1, xmm1);
3533 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003534 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003535 __ divsd(xmm3, xmm1);
3536 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003537 __ jmp(&allocate_return);
3538
3539 // Test for 0.5.
3540 __ bind(&not_minus_half);
3541 // Load xmm2 with 0.5.
3542 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
3543 __ addsd(xmm2, xmm3);
3544 // xmm2 now has 0.5.
3545 __ ucomisd(xmm2, xmm1);
3546 __ j(not_equal, &call_runtime);
3547 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003548 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3549 __ xorpd(xmm1, xmm1);
3550 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003551 __ sqrtsd(xmm1, xmm1);
3552
3553 __ bind(&allocate_return);
3554 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
3555 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
3556 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003557 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003558
3559 __ bind(&call_runtime);
3560 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3561}
3562
3563
ricow@chromium.org65fae842010-08-25 15:26:24 +00003564void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3565 // The key is in edx and the parameter count is in eax.
3566
3567 // The displacement is used for skipping the frame pointer on the
3568 // stack. It is the offset of the last parameter (if any) relative
3569 // to the frame pointer.
3570 static const int kDisplacement = 1 * kPointerSize;
3571
3572 // Check that the key is a smi.
3573 Label slow;
3574 __ test(edx, Immediate(kSmiTagMask));
3575 __ j(not_zero, &slow, not_taken);
3576
3577 // Check if the calling frame is an arguments adaptor frame.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003578 NearLabel adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003579 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3580 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
3581 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3582 __ j(equal, &adaptor);
3583
3584 // Check index against formal parameters count limit passed in
3585 // through register eax. Use unsigned comparison to get negative
3586 // check for free.
3587 __ cmp(edx, Operand(eax));
3588 __ j(above_equal, &slow, not_taken);
3589
3590 // Read the argument from the stack and return it.
3591 STATIC_ASSERT(kSmiTagSize == 1);
3592 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3593 __ lea(ebx, Operand(ebp, eax, times_2, 0));
3594 __ neg(edx);
3595 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3596 __ ret(0);
3597
3598 // Arguments adaptor case: Check index against actual arguments
3599 // limit found in the arguments adaptor frame. Use unsigned
3600 // comparison to get negative check for free.
3601 __ bind(&adaptor);
3602 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3603 __ cmp(edx, Operand(ecx));
3604 __ j(above_equal, &slow, not_taken);
3605
3606 // Read the argument from the stack and return it.
3607 STATIC_ASSERT(kSmiTagSize == 1);
3608 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3609 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
3610 __ neg(edx);
3611 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3612 __ ret(0);
3613
3614 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3615 // by calling the runtime system.
3616 __ bind(&slow);
3617 __ pop(ebx); // Return address.
3618 __ push(edx);
3619 __ push(ebx);
3620 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3621}
3622
3623
3624void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3625 // esp[0] : return address
3626 // esp[4] : number of parameters
3627 // esp[8] : receiver displacement
3628 // esp[16] : function
3629
3630 // The displacement is used for skipping the return address and the
3631 // frame pointer on the stack. It is the offset of the last
3632 // parameter (if any) relative to the frame pointer.
3633 static const int kDisplacement = 2 * kPointerSize;
3634
3635 // Check if the calling frame is an arguments adaptor frame.
3636 Label adaptor_frame, try_allocate, runtime;
3637 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3638 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
3639 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3640 __ j(equal, &adaptor_frame);
3641
3642 // Get the length from the frame.
3643 __ mov(ecx, Operand(esp, 1 * kPointerSize));
3644 __ jmp(&try_allocate);
3645
3646 // Patch the arguments.length and the parameters pointer.
3647 __ bind(&adaptor_frame);
3648 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3649 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3650 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
3651 __ mov(Operand(esp, 2 * kPointerSize), edx);
3652
3653 // Try the new space allocation. Start out with computing the size of
3654 // the arguments object and the elements array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003655 NearLabel add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003656 __ bind(&try_allocate);
3657 __ test(ecx, Operand(ecx));
3658 __ j(zero, &add_arguments_object);
3659 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3660 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003661 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003662
3663 // Do the allocation of both objects in one go.
3664 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3665
3666 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003667 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3668 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003669 __ mov(edi, Operand(edi,
3670 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003671
3672 // Copy the JS object part.
3673 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3674 __ mov(ebx, FieldOperand(edi, i));
3675 __ mov(FieldOperand(eax, i), ebx);
3676 }
3677
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003678 if (type_ == NEW_NON_STRICT) {
3679 // Setup the callee in-object property.
3680 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3681 __ mov(ebx, Operand(esp, 3 * kPointerSize));
3682 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3683 Heap::kArgumentsCalleeIndex * kPointerSize),
3684 ebx);
3685 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003686
3687 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003688 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003689 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003690 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3691 Heap::kArgumentsLengthIndex * kPointerSize),
3692 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003693
3694 // If there are no actual arguments, we're done.
3695 Label done;
3696 __ test(ecx, Operand(ecx));
3697 __ j(zero, &done);
3698
3699 // Get the parameters pointer from the stack.
3700 __ mov(edx, Operand(esp, 2 * kPointerSize));
3701
3702 // Setup the elements pointer in the allocated arguments object and
3703 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003704 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003705 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3706 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003707 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003708
ricow@chromium.org65fae842010-08-25 15:26:24 +00003709 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3710 // Untag the length for the loop below.
3711 __ SmiUntag(ecx);
3712
3713 // Copy the fixed array slots.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003714 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003715 __ bind(&loop);
3716 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3717 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3718 __ add(Operand(edi), Immediate(kPointerSize));
3719 __ sub(Operand(edx), Immediate(kPointerSize));
3720 __ dec(ecx);
3721 __ j(not_zero, &loop);
3722
3723 // Return and remove the on-stack parameters.
3724 __ bind(&done);
3725 __ ret(3 * kPointerSize);
3726
3727 // Do the runtime call to allocate the arguments object.
3728 __ bind(&runtime);
3729 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3730}
3731
3732
3733void RegExpExecStub::Generate(MacroAssembler* masm) {
3734 // Just jump directly to runtime if native RegExp is not selected at compile
3735 // time or if regexp entry in generated code is turned off runtime switch or
3736 // at compilation.
3737#ifdef V8_INTERPRETED_REGEXP
3738 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3739#else // V8_INTERPRETED_REGEXP
3740 if (!FLAG_regexp_entry_native) {
3741 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3742 return;
3743 }
3744
3745 // Stack frame on entry.
3746 // esp[0]: return address
3747 // esp[4]: last_match_info (expected JSArray)
3748 // esp[8]: previous index
3749 // esp[12]: subject string
3750 // esp[16]: JSRegExp object
3751
3752 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3753 static const int kPreviousIndexOffset = 2 * kPointerSize;
3754 static const int kSubjectOffset = 3 * kPointerSize;
3755 static const int kJSRegExpOffset = 4 * kPointerSize;
3756
3757 Label runtime, invoke_regexp;
3758
3759 // Ensure that a RegExp stack is allocated.
3760 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003761 ExternalReference::address_of_regexp_stack_memory_address(
3762 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003763 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003764 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003765 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3766 __ test(ebx, Operand(ebx));
3767 __ j(zero, &runtime, not_taken);
3768
3769 // Check that the first argument is a JSRegExp object.
3770 __ mov(eax, Operand(esp, kJSRegExpOffset));
3771 STATIC_ASSERT(kSmiTag == 0);
3772 __ test(eax, Immediate(kSmiTagMask));
3773 __ j(zero, &runtime);
3774 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3775 __ j(not_equal, &runtime);
3776 // Check that the RegExp has been compiled (data contains a fixed array).
3777 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3778 if (FLAG_debug_code) {
3779 __ test(ecx, Immediate(kSmiTagMask));
3780 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3781 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3782 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3783 }
3784
3785 // ecx: RegExp data (FixedArray)
3786 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3787 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3788 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3789 __ j(not_equal, &runtime);
3790
3791 // ecx: RegExp data (FixedArray)
3792 // Check that the number of captures fit in the static offsets vector buffer.
3793 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3794 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3795 // uses the asumption that smis are 2 * their untagged value.
3796 STATIC_ASSERT(kSmiTag == 0);
3797 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3798 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3799 // Check that the static offsets vector buffer is large enough.
3800 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3801 __ j(above, &runtime);
3802
3803 // ecx: RegExp data (FixedArray)
3804 // edx: Number of capture registers
3805 // Check that the second argument is a string.
3806 __ mov(eax, Operand(esp, kSubjectOffset));
3807 __ test(eax, Immediate(kSmiTagMask));
3808 __ j(zero, &runtime);
3809 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3810 __ j(NegateCondition(is_string), &runtime);
3811 // Get the length of the string to ebx.
3812 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3813
3814 // ebx: Length of subject string as a smi
3815 // ecx: RegExp data (FixedArray)
3816 // edx: Number of capture registers
3817 // Check that the third argument is a positive smi less than the subject
3818 // string length. A negative value will be greater (unsigned comparison).
3819 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3820 __ test(eax, Immediate(kSmiTagMask));
3821 __ j(not_zero, &runtime);
3822 __ cmp(eax, Operand(ebx));
3823 __ j(above_equal, &runtime);
3824
3825 // ecx: RegExp data (FixedArray)
3826 // edx: Number of capture registers
3827 // Check that the fourth object is a JSArray object.
3828 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3829 __ test(eax, Immediate(kSmiTagMask));
3830 __ j(zero, &runtime);
3831 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3832 __ j(not_equal, &runtime);
3833 // Check that the JSArray is in fast case.
3834 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3835 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003836 Factory* factory = masm->isolate()->factory();
3837 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003838 __ j(not_equal, &runtime);
3839 // Check that the last match info has space for the capture registers and the
3840 // additional information.
3841 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3842 __ SmiUntag(eax);
3843 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3844 __ cmp(edx, Operand(eax));
3845 __ j(greater, &runtime);
3846
3847 // ecx: RegExp data (FixedArray)
3848 // Check the representation and encoding of the subject string.
3849 Label seq_ascii_string, seq_two_byte_string, check_code;
3850 __ mov(eax, Operand(esp, kSubjectOffset));
3851 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3852 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3853 // First check for flat two byte string.
3854 __ and_(ebx,
3855 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3856 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3857 __ j(zero, &seq_two_byte_string);
3858 // Any other flat string must be a flat ascii string.
3859 __ test(Operand(ebx),
3860 Immediate(kIsNotStringMask | kStringRepresentationMask));
3861 __ j(zero, &seq_ascii_string);
3862
3863 // Check for flat cons string.
3864 // A flat cons string is a cons string where the second part is the empty
3865 // string. In that case the subject string is just the first part of the cons
3866 // string. Also in this case the first part of the cons string is known to be
3867 // a sequential string or an external string.
3868 STATIC_ASSERT(kExternalStringTag != 0);
3869 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3870 __ test(Operand(ebx),
3871 Immediate(kIsNotStringMask | kExternalStringTag));
3872 __ j(not_zero, &runtime);
3873 // String is a cons string.
3874 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003875 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003876 __ j(not_equal, &runtime);
3877 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3878 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3879 // String is a cons string with empty second part.
3880 // eax: first part of cons string.
3881 // ebx: map of first part of cons string.
3882 // Is first part a flat two byte string?
3883 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3884 kStringRepresentationMask | kStringEncodingMask);
3885 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3886 __ j(zero, &seq_two_byte_string);
3887 // Any other flat string must be ascii.
3888 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3889 kStringRepresentationMask);
3890 __ j(not_zero, &runtime);
3891
3892 __ bind(&seq_ascii_string);
3893 // eax: subject string (flat ascii)
3894 // ecx: RegExp data (FixedArray)
3895 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3896 __ Set(edi, Immediate(1)); // Type is ascii.
3897 __ jmp(&check_code);
3898
3899 __ bind(&seq_two_byte_string);
3900 // eax: subject string (flat two byte)
3901 // ecx: RegExp data (FixedArray)
3902 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3903 __ Set(edi, Immediate(0)); // Type is two byte.
3904
3905 __ bind(&check_code);
3906 // Check that the irregexp code has been generated for the actual string
3907 // encoding. If it has, the field contains a code object otherwise it contains
3908 // the hole.
3909 __ CmpObjectType(edx, CODE_TYPE, ebx);
3910 __ j(not_equal, &runtime);
3911
3912 // eax: subject string
3913 // edx: code
3914 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3915 // Load used arguments before starting to push arguments for call to native
3916 // RegExp code to avoid handling changing stack height.
3917 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3918 __ SmiUntag(ebx); // Previous index from smi.
3919
3920 // eax: subject string
3921 // ebx: previous index
3922 // edx: code
3923 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3924 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003925 Counters* counters = masm->isolate()->counters();
3926 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003927
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003928 // Isolates: note we add an additional parameter here (isolate pointer).
3929 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003930 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003931
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003932 // Argument 8: Pass current isolate address.
3933 __ mov(Operand(esp, 7 * kPointerSize),
3934 Immediate(ExternalReference::isolate_address()));
3935
ricow@chromium.org65fae842010-08-25 15:26:24 +00003936 // Argument 7: Indicate that this is a direct call from JavaScript.
3937 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3938
3939 // Argument 6: Start (high end) of backtracking stack memory area.
3940 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3941 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3942 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3943
3944 // Argument 5: static offsets vector buffer.
3945 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003946 Immediate(ExternalReference::address_of_static_offsets_vector(
3947 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003948
3949 // Argument 4: End of string data
3950 // Argument 3: Start of string data
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003951 NearLabel setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003952 __ test(edi, Operand(edi));
3953 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
3954 __ j(zero, &setup_two_byte);
3955 __ SmiUntag(edi);
3956 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3957 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3958 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3959 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3960 __ jmp(&setup_rest);
3961
3962 __ bind(&setup_two_byte);
3963 STATIC_ASSERT(kSmiTag == 0);
3964 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3965 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3966 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3967 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3968 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3969
3970 __ bind(&setup_rest);
3971
3972 // Argument 2: Previous index.
3973 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3974
3975 // Argument 1: Subject string.
3976 __ mov(Operand(esp, 0 * kPointerSize), eax);
3977
3978 // Locate the code entry and call it.
3979 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003980 __ call(Operand(edx));
3981
3982 // Drop arguments and come back to JS mode.
3983 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003984
3985 // Check the result.
3986 Label success;
3987 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
3988 __ j(equal, &success, taken);
3989 Label failure;
3990 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
3991 __ j(equal, &failure, taken);
3992 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3993 // If not exception it can only be retry. Handle that in the runtime system.
3994 __ j(not_equal, &runtime);
3995 // Result must now be exception. If there is no pending exception already a
3996 // stack overflow (on the backtrack stack) was detected in RegExp code but
3997 // haven't created the exception yet. Handle that in the runtime system.
3998 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003999 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4000 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004001 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004002 Operand::StaticVariable(ExternalReference::the_hole_value_location(
4003 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004004 __ mov(eax, Operand::StaticVariable(pending_exception));
4005 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004006 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004007 // For exception, throw the exception again.
4008
4009 // Clear the pending exception variable.
4010 __ mov(Operand::StaticVariable(pending_exception), edx);
4011
4012 // Special handling of termination exceptions which are uncatchable
4013 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004014 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004015 Label throw_termination_exception;
4016 __ j(equal, &throw_termination_exception);
4017
4018 // Handle normal exception by following handler chain.
4019 __ Throw(eax);
4020
4021 __ bind(&throw_termination_exception);
4022 __ ThrowUncatchable(TERMINATION, eax);
4023
ricow@chromium.org65fae842010-08-25 15:26:24 +00004024 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004025 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004026 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004027 __ ret(4 * kPointerSize);
4028
4029 // Load RegExp data.
4030 __ bind(&success);
4031 __ mov(eax, Operand(esp, kJSRegExpOffset));
4032 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
4033 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
4034 // Calculate number of capture registers (number_of_captures + 1) * 2.
4035 STATIC_ASSERT(kSmiTag == 0);
4036 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4037 __ add(Operand(edx), Immediate(2)); // edx was a smi.
4038
4039 // edx: Number of capture registers
4040 // Load last_match_info which is still known to be a fast case JSArray.
4041 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4042 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
4043
4044 // ebx: last_match_info backing store (FixedArray)
4045 // edx: number of capture registers
4046 // Store the capture count.
4047 __ SmiTag(edx); // Number of capture registers to smi.
4048 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
4049 __ SmiUntag(edx); // Number of capture registers back from smi.
4050 // Store last subject and last input.
4051 __ mov(eax, Operand(esp, kSubjectOffset));
4052 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
4053 __ mov(ecx, ebx);
4054 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
4055 __ mov(eax, Operand(esp, kSubjectOffset));
4056 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
4057 __ mov(ecx, ebx);
4058 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
4059
4060 // Get the static offsets vector filled by the native regexp code.
4061 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004062 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004063 __ mov(ecx, Immediate(address_of_static_offsets_vector));
4064
4065 // ebx: last_match_info backing store (FixedArray)
4066 // ecx: offsets vector
4067 // edx: number of capture registers
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004068 NearLabel next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004069 // Capture register counter starts from number of capture registers and
4070 // counts down until wraping after zero.
4071 __ bind(&next_capture);
4072 __ sub(Operand(edx), Immediate(1));
4073 __ j(negative, &done);
4074 // Read the value from the static offsets vector buffer.
4075 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
4076 __ SmiTag(edi);
4077 // Store the smi value in the last match info.
4078 __ mov(FieldOperand(ebx,
4079 edx,
4080 times_pointer_size,
4081 RegExpImpl::kFirstCaptureOffset),
4082 edi);
4083 __ jmp(&next_capture);
4084 __ bind(&done);
4085
4086 // Return last match info.
4087 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4088 __ ret(4 * kPointerSize);
4089
4090 // Do the runtime call to execute the regexp.
4091 __ bind(&runtime);
4092 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4093#endif // V8_INTERPRETED_REGEXP
4094}
4095
4096
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004097void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4098 const int kMaxInlineLength = 100;
4099 Label slowcase;
4100 NearLabel done;
4101 __ mov(ebx, Operand(esp, kPointerSize * 3));
4102 __ test(ebx, Immediate(kSmiTagMask));
4103 __ j(not_zero, &slowcase);
4104 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
4105 __ j(above, &slowcase);
4106 // Smi-tagging is equivalent to multiplying by 2.
4107 STATIC_ASSERT(kSmiTag == 0);
4108 STATIC_ASSERT(kSmiTagSize == 1);
4109 // Allocate RegExpResult followed by FixedArray with size in ebx.
4110 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4111 // Elements: [Map][Length][..elements..]
4112 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
4113 times_half_pointer_size,
4114 ebx, // In: Number of elements (times 2, being a smi)
4115 eax, // Out: Start of allocation (tagged).
4116 ecx, // Out: End of allocation.
4117 edx, // Scratch register
4118 &slowcase,
4119 TAG_OBJECT);
4120 // eax: Start of allocated area, object-tagged.
4121
4122 // Set JSArray map to global.regexp_result_map().
4123 // Set empty properties FixedArray.
4124 // Set elements to point to FixedArray allocated right after the JSArray.
4125 // Interleave operations for better latency.
4126 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004127 Factory* factory = masm->isolate()->factory();
4128 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004129 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
4130 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
4131 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
4132 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
4133 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
4134 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
4135
4136 // Set input, index and length fields from arguments.
4137 __ mov(ecx, Operand(esp, kPointerSize * 1));
4138 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
4139 __ mov(ecx, Operand(esp, kPointerSize * 2));
4140 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
4141 __ mov(ecx, Operand(esp, kPointerSize * 3));
4142 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
4143
4144 // Fill out the elements FixedArray.
4145 // eax: JSArray.
4146 // ebx: FixedArray.
4147 // ecx: Number of elements in array, as smi.
4148
4149 // Set map.
4150 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004151 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004152 // Set length.
4153 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
4154 // Fill contents of fixed-array with the-hole.
4155 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004156 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004157 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
4158 // Fill fixed array elements with hole.
4159 // eax: JSArray.
4160 // ecx: Number of elements to fill.
4161 // ebx: Start of elements in FixedArray.
4162 // edx: the hole.
4163 Label loop;
4164 __ test(ecx, Operand(ecx));
4165 __ bind(&loop);
4166 __ j(less_equal, &done); // Jump if ecx is negative or zero.
4167 __ sub(Operand(ecx), Immediate(1));
4168 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
4169 __ jmp(&loop);
4170
4171 __ bind(&done);
4172 __ ret(3 * kPointerSize);
4173
4174 __ bind(&slowcase);
4175 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4176}
4177
4178
ricow@chromium.org65fae842010-08-25 15:26:24 +00004179void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
4180 Register object,
4181 Register result,
4182 Register scratch1,
4183 Register scratch2,
4184 bool object_is_smi,
4185 Label* not_found) {
4186 // Use of registers. Register result is used as a temporary.
4187 Register number_string_cache = result;
4188 Register mask = scratch1;
4189 Register scratch = scratch2;
4190
4191 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004192 ExternalReference roots_address =
4193 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004194 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
4195 __ mov(number_string_cache,
4196 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4197 // Make the hash mask from the length of the number string cache. It
4198 // contains two elements (number and string) for each cache entry.
4199 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
4200 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
4201 __ sub(Operand(mask), Immediate(1)); // Make mask.
4202
4203 // Calculate the entry in the number string cache. The hash value in the
4204 // number string cache for smis is just the smi value, and the hash for
4205 // doubles is the xor of the upper and lower words. See
4206 // Heap::GetNumberStringCache.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004207 NearLabel smi_hash_calculated;
4208 NearLabel load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004209 if (object_is_smi) {
4210 __ mov(scratch, object);
4211 __ SmiUntag(scratch);
4212 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004213 NearLabel not_smi, hash_calculated;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004214 STATIC_ASSERT(kSmiTag == 0);
4215 __ test(object, Immediate(kSmiTagMask));
4216 __ j(not_zero, &not_smi);
4217 __ mov(scratch, object);
4218 __ SmiUntag(scratch);
4219 __ jmp(&smi_hash_calculated);
4220 __ bind(&not_smi);
4221 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004222 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004223 __ j(not_equal, not_found);
4224 STATIC_ASSERT(8 == kDoubleSize);
4225 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
4226 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
4227 // Object is heap number and hash is now in scratch. Calculate cache index.
4228 __ and_(scratch, Operand(mask));
4229 Register index = scratch;
4230 Register probe = mask;
4231 __ mov(probe,
4232 FieldOperand(number_string_cache,
4233 index,
4234 times_twice_pointer_size,
4235 FixedArray::kHeaderSize));
4236 __ test(probe, Immediate(kSmiTagMask));
4237 __ j(zero, not_found);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004238 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004239 CpuFeatures::Scope fscope(SSE2);
4240 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
4241 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
4242 __ ucomisd(xmm0, xmm1);
4243 } else {
4244 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
4245 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
4246 __ FCmp();
4247 }
4248 __ j(parity_even, not_found); // Bail out if NaN is involved.
4249 __ j(not_equal, not_found); // The cache did not contain this value.
4250 __ jmp(&load_result_from_cache);
4251 }
4252
4253 __ bind(&smi_hash_calculated);
4254 // Object is smi and hash is now in scratch. Calculate cache index.
4255 __ and_(scratch, Operand(mask));
4256 Register index = scratch;
4257 // Check if the entry is the smi we are looking for.
4258 __ cmp(object,
4259 FieldOperand(number_string_cache,
4260 index,
4261 times_twice_pointer_size,
4262 FixedArray::kHeaderSize));
4263 __ j(not_equal, not_found);
4264
4265 // Get the result from the cache.
4266 __ bind(&load_result_from_cache);
4267 __ mov(result,
4268 FieldOperand(number_string_cache,
4269 index,
4270 times_twice_pointer_size,
4271 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004272 Counters* counters = masm->isolate()->counters();
4273 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004274}
4275
4276
4277void NumberToStringStub::Generate(MacroAssembler* masm) {
4278 Label runtime;
4279
4280 __ mov(ebx, Operand(esp, kPointerSize));
4281
4282 // Generate code to lookup number in the number string cache.
4283 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
4284 __ ret(1 * kPointerSize);
4285
4286 __ bind(&runtime);
4287 // Handle number to string in the runtime system if not found in the cache.
4288 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
4289}
4290
4291
4292static int NegativeComparisonResult(Condition cc) {
4293 ASSERT(cc != equal);
4294 ASSERT((cc == less) || (cc == less_equal)
4295 || (cc == greater) || (cc == greater_equal));
4296 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
4297}
4298
4299void CompareStub::Generate(MacroAssembler* masm) {
4300 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4301
4302 Label check_unequal_objects, done;
4303
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004304 // Compare two smis if required.
4305 if (include_smi_compare_) {
4306 Label non_smi, smi_done;
4307 __ mov(ecx, Operand(edx));
4308 __ or_(ecx, Operand(eax));
4309 __ test(ecx, Immediate(kSmiTagMask));
4310 __ j(not_zero, &non_smi, not_taken);
4311 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
4312 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004313 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004314 __ bind(&smi_done);
4315 __ mov(eax, edx);
4316 __ ret(0);
4317 __ bind(&non_smi);
4318 } else if (FLAG_debug_code) {
4319 __ mov(ecx, Operand(edx));
4320 __ or_(ecx, Operand(eax));
4321 __ test(ecx, Immediate(kSmiTagMask));
4322 __ Assert(not_zero, "Unexpected smi operands.");
4323 }
4324
ricow@chromium.org65fae842010-08-25 15:26:24 +00004325 // NOTICE! This code is only reached after a smi-fast-case check, so
4326 // it is certain that at least one operand isn't a smi.
4327
4328 // Identical objects can be compared fast, but there are some tricky cases
4329 // for NaN and undefined.
4330 {
4331 Label not_identical;
4332 __ cmp(eax, Operand(edx));
4333 __ j(not_equal, &not_identical);
4334
4335 if (cc_ != equal) {
4336 // Check for undefined. undefined OP undefined is false even though
4337 // undefined == undefined.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004338 NearLabel check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004339 __ cmp(edx, masm->isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004340 __ j(not_equal, &check_for_nan);
4341 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4342 __ ret(0);
4343 __ bind(&check_for_nan);
4344 }
4345
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004346 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00004347 // so we do the second best thing - test it ourselves.
4348 // Note: if cc_ != equal, never_nan_nan_ is not used.
4349 if (never_nan_nan_ && (cc_ == equal)) {
4350 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4351 __ ret(0);
4352 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004353 NearLabel heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004354 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004355 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004356 __ j(equal, &heap_number);
4357 if (cc_ != equal) {
4358 // Call runtime on identical JSObjects. Otherwise return equal.
4359 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4360 __ j(above_equal, &not_identical);
4361 }
4362 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4363 __ ret(0);
4364
4365 __ bind(&heap_number);
4366 // It is a heap number, so return non-equal if it's NaN and equal if
4367 // it's not NaN.
4368 // The representation of NaN values has all exponent bits (52..62) set,
4369 // and not all mantissa bits (0..51) clear.
4370 // We only accept QNaNs, which have bit 51 set.
4371 // Read top bits of double representation (second word of value).
4372
4373 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
4374 // all bits in the mask are set. We only need to check the word
4375 // that contains the exponent and high bit of the mantissa.
4376 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
4377 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004378 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004379 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
4380 // bits.
4381 __ add(edx, Operand(edx));
4382 __ cmp(edx, kQuietNaNHighBitsMask << 1);
4383 if (cc_ == equal) {
4384 STATIC_ASSERT(EQUAL != 1);
4385 __ setcc(above_equal, eax);
4386 __ ret(0);
4387 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004388 NearLabel nan;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004389 __ j(above_equal, &nan);
4390 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4391 __ ret(0);
4392 __ bind(&nan);
4393 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4394 __ ret(0);
4395 }
4396 }
4397
4398 __ bind(&not_identical);
4399 }
4400
4401 // Strict equality can quickly decide whether objects are equal.
4402 // Non-strict object equality is slower, so it is handled later in the stub.
4403 if (cc_ == equal && strict_) {
4404 Label slow; // Fallthrough label.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004405 NearLabel not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004406 // If we're doing a strict equality comparison, we don't have to do
4407 // type conversion, so we generate code to do fast comparison for objects
4408 // and oddballs. Non-smi numbers and strings still go through the usual
4409 // slow-case code.
4410 // If either is a Smi (we know that not both are), then they can only
4411 // be equal if the other is a HeapNumber. If so, use the slow case.
4412 STATIC_ASSERT(kSmiTag == 0);
4413 ASSERT_EQ(0, Smi::FromInt(0));
4414 __ mov(ecx, Immediate(kSmiTagMask));
4415 __ and_(ecx, Operand(eax));
4416 __ test(ecx, Operand(edx));
4417 __ j(not_zero, &not_smis);
4418 // One operand is a smi.
4419
4420 // Check whether the non-smi is a heap number.
4421 STATIC_ASSERT(kSmiTagMask == 1);
4422 // ecx still holds eax & kSmiTag, which is either zero or one.
4423 __ sub(Operand(ecx), Immediate(0x01));
4424 __ mov(ebx, edx);
4425 __ xor_(ebx, Operand(eax));
4426 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
4427 __ xor_(ebx, Operand(eax));
4428 // if eax was smi, ebx is now edx, else eax.
4429
4430 // Check if the non-smi operand is a heap number.
4431 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004432 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004433 // If heap number, handle it in the slow case.
4434 __ j(equal, &slow);
4435 // Return non-equal (ebx is not zero)
4436 __ mov(eax, ebx);
4437 __ ret(0);
4438
4439 __ bind(&not_smis);
4440 // If either operand is a JSObject or an oddball value, then they are not
4441 // equal since their pointers are different
4442 // There is no test for undetectability in strict equality.
4443
4444 // Get the type of the first operand.
4445 // If the first object is a JS object, we have done pointer comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004446 NearLabel first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004447 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4448 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4449 __ j(below, &first_non_object);
4450
4451 // Return non-zero (eax is not zero)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004452 NearLabel return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004453 STATIC_ASSERT(kHeapObjectTag != 0);
4454 __ bind(&return_not_equal);
4455 __ ret(0);
4456
4457 __ bind(&first_non_object);
4458 // Check for oddballs: true, false, null, undefined.
4459 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4460 __ j(equal, &return_not_equal);
4461
4462 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
4463 __ j(above_equal, &return_not_equal);
4464
4465 // Check for oddballs: true, false, null, undefined.
4466 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4467 __ j(equal, &return_not_equal);
4468
4469 // Fall through to the general case.
4470 __ bind(&slow);
4471 }
4472
4473 // Generate the number comparison code.
4474 if (include_number_compare_) {
4475 Label non_number_comparison;
4476 Label unordered;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004477 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004478 CpuFeatures::Scope use_sse2(SSE2);
4479 CpuFeatures::Scope use_cmov(CMOV);
4480
4481 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
4482 __ ucomisd(xmm0, xmm1);
4483
4484 // Don't base result on EFLAGS when a NaN is involved.
4485 __ j(parity_even, &unordered, not_taken);
4486 // Return a result of -1, 0, or 1, based on EFLAGS.
4487 __ mov(eax, 0); // equal
4488 __ mov(ecx, Immediate(Smi::FromInt(1)));
4489 __ cmov(above, eax, Operand(ecx));
4490 __ mov(ecx, Immediate(Smi::FromInt(-1)));
4491 __ cmov(below, eax, Operand(ecx));
4492 __ ret(0);
4493 } else {
4494 FloatingPointHelper::CheckFloatOperands(
4495 masm, &non_number_comparison, ebx);
4496 FloatingPointHelper::LoadFloatOperand(masm, eax);
4497 FloatingPointHelper::LoadFloatOperand(masm, edx);
4498 __ FCmp();
4499
4500 // Don't base result on EFLAGS when a NaN is involved.
4501 __ j(parity_even, &unordered, not_taken);
4502
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004503 NearLabel below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004504 // Return a result of -1, 0, or 1, based on EFLAGS.
4505 __ j(below, &below_label, not_taken);
4506 __ j(above, &above_label, not_taken);
4507
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004508 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004509 __ ret(0);
4510
4511 __ bind(&below_label);
4512 __ mov(eax, Immediate(Smi::FromInt(-1)));
4513 __ ret(0);
4514
4515 __ bind(&above_label);
4516 __ mov(eax, Immediate(Smi::FromInt(1)));
4517 __ ret(0);
4518 }
4519
4520 // If one of the numbers was NaN, then the result is always false.
4521 // The cc is never not-equal.
4522 __ bind(&unordered);
4523 ASSERT(cc_ != not_equal);
4524 if (cc_ == less || cc_ == less_equal) {
4525 __ mov(eax, Immediate(Smi::FromInt(1)));
4526 } else {
4527 __ mov(eax, Immediate(Smi::FromInt(-1)));
4528 }
4529 __ ret(0);
4530
4531 // The number comparison code did not provide a valid result.
4532 __ bind(&non_number_comparison);
4533 }
4534
4535 // Fast negative check for symbol-to-symbol equality.
4536 Label check_for_strings;
4537 if (cc_ == equal) {
4538 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4539 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4540
4541 // We've already checked for object identity, so if both operands
4542 // are symbols they aren't equal. Register eax already holds a
4543 // non-zero value, which indicates not equal, so just return.
4544 __ ret(0);
4545 }
4546
4547 __ bind(&check_for_strings);
4548
4549 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4550 &check_unequal_objects);
4551
4552 // Inline comparison of ascii strings.
4553 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4554 edx,
4555 eax,
4556 ecx,
4557 ebx,
4558 edi);
4559#ifdef DEBUG
4560 __ Abort("Unexpected fall-through from string comparison");
4561#endif
4562
4563 __ bind(&check_unequal_objects);
4564 if (cc_ == equal && !strict_) {
4565 // Non-strict equality. Objects are unequal if
4566 // they are both JSObjects and not undetectable,
4567 // and their pointers are different.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004568 NearLabel not_both_objects;
4569 NearLabel return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004570 // At most one is a smi, so we can test for smi by adding the two.
4571 // A smi plus a heap object has the low bit set, a heap object plus
4572 // a heap object has the low bit clear.
4573 STATIC_ASSERT(kSmiTag == 0);
4574 STATIC_ASSERT(kSmiTagMask == 1);
4575 __ lea(ecx, Operand(eax, edx, times_1, 0));
4576 __ test(ecx, Immediate(kSmiTagMask));
4577 __ j(not_zero, &not_both_objects);
4578 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4579 __ j(below, &not_both_objects);
4580 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
4581 __ j(below, &not_both_objects);
4582 // We do not bail out after this point. Both are JSObjects, and
4583 // they are equal if and only if both are undetectable.
4584 // The and of the undetectable flags is 1 if and only if they are equal.
4585 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4586 1 << Map::kIsUndetectable);
4587 __ j(zero, &return_unequal);
4588 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4589 1 << Map::kIsUndetectable);
4590 __ j(zero, &return_unequal);
4591 // The objects are both undetectable, so they both compare as the value
4592 // undefined, and are equal.
4593 __ Set(eax, Immediate(EQUAL));
4594 __ bind(&return_unequal);
4595 // Return non-equal by returning the non-zero object pointer in eax,
4596 // or return equal if we fell through to here.
4597 __ ret(0); // rax, rdx were pushed
4598 __ bind(&not_both_objects);
4599 }
4600
4601 // Push arguments below the return address.
4602 __ pop(ecx);
4603 __ push(edx);
4604 __ push(eax);
4605
4606 // Figure out which native to call and setup the arguments.
4607 Builtins::JavaScript builtin;
4608 if (cc_ == equal) {
4609 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4610 } else {
4611 builtin = Builtins::COMPARE;
4612 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4613 }
4614
4615 // Restore return address on the stack.
4616 __ push(ecx);
4617
4618 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4619 // tagged as a small integer.
4620 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4621}
4622
4623
4624void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4625 Label* label,
4626 Register object,
4627 Register scratch) {
4628 __ test(object, Immediate(kSmiTagMask));
4629 __ j(zero, label);
4630 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4631 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4632 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4633 __ cmp(scratch, kSymbolTag | kStringTag);
4634 __ j(not_equal, label);
4635}
4636
4637
4638void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004639 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004640}
4641
4642
4643void CallFunctionStub::Generate(MacroAssembler* masm) {
4644 Label slow;
4645
4646 // If the receiver might be a value (string, number or boolean) check for this
4647 // and box it if it is.
4648 if (ReceiverMightBeValue()) {
4649 // Get the receiver from the stack.
4650 // +1 ~ return address
4651 Label receiver_is_value, receiver_is_js_object;
4652 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
4653
4654 // Check if receiver is a smi (which is a number value).
4655 __ test(eax, Immediate(kSmiTagMask));
4656 __ j(zero, &receiver_is_value, not_taken);
4657
4658 // Check if the receiver is a valid JS object.
4659 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
4660 __ j(above_equal, &receiver_is_js_object);
4661
4662 // Call the runtime to box the value.
4663 __ bind(&receiver_is_value);
4664 __ EnterInternalFrame();
4665 __ push(eax);
4666 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
4667 __ LeaveInternalFrame();
4668 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
4669
4670 __ bind(&receiver_is_js_object);
4671 }
4672
4673 // Get the function to call from the stack.
4674 // +2 ~ receiver, return address
4675 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4676
4677 // Check that the function really is a JavaScript function.
4678 __ test(edi, Immediate(kSmiTagMask));
4679 __ j(zero, &slow, not_taken);
4680 // Goto slow case if we do not have a function.
4681 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
4682 __ j(not_equal, &slow, not_taken);
4683
4684 // Fast-case: Just invoke the function.
4685 ParameterCount actual(argc_);
4686 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
4687
4688 // Slow-case: Non-function called.
4689 __ bind(&slow);
4690 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4691 // of the original receiver from the call site).
4692 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4693 __ Set(eax, Immediate(argc_));
4694 __ Set(ebx, Immediate(0));
4695 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004696 Handle<Code> adaptor =
4697 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004698 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4699}
4700
4701
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004702bool CEntryStub::NeedsImmovableCode() {
4703 return false;
4704}
4705
4706
ricow@chromium.org65fae842010-08-25 15:26:24 +00004707void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004708 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004709}
4710
4711
ricow@chromium.org65fae842010-08-25 15:26:24 +00004712void CEntryStub::GenerateCore(MacroAssembler* masm,
4713 Label* throw_normal_exception,
4714 Label* throw_termination_exception,
4715 Label* throw_out_of_memory_exception,
4716 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004717 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004718 // eax: result parameter for PerformGC, if any
4719 // ebx: pointer to C function (C callee-saved)
4720 // ebp: frame pointer (restored after C call)
4721 // esp: stack pointer (restored after C call)
4722 // edi: number of arguments including receiver (C callee-saved)
4723 // esi: pointer to the first argument (C callee-saved)
4724
4725 // Result returned in eax, or eax+edx if result_size_ is 2.
4726
4727 // Check stack alignment.
4728 if (FLAG_debug_code) {
4729 __ CheckStackAlignment();
4730 }
4731
4732 if (do_gc) {
4733 // Pass failure code returned from last attempt as first argument to
4734 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4735 // stack alignment is known to be correct. This function takes one argument
4736 // which is passed on the stack, and we know that the stack has been
4737 // prepared to pass at least one argument.
4738 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4739 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4740 }
4741
4742 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004743 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004744 if (always_allocate_scope) {
4745 __ inc(Operand::StaticVariable(scope_depth));
4746 }
4747
4748 // Call C function.
4749 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4750 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004751 __ mov(Operand(esp, 2 * kPointerSize),
4752 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004753 __ call(Operand(ebx));
4754 // Result is in eax or edx:eax - do not destroy these registers!
4755
4756 if (always_allocate_scope) {
4757 __ dec(Operand::StaticVariable(scope_depth));
4758 }
4759
4760 // Make sure we're not trying to return 'the hole' from the runtime
4761 // call as this may lead to crashes in the IC code later.
4762 if (FLAG_debug_code) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004763 NearLabel okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004764 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004765 __ j(not_equal, &okay);
4766 __ int3();
4767 __ bind(&okay);
4768 }
4769
4770 // Check for failure result.
4771 Label failure_returned;
4772 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4773 __ lea(ecx, Operand(eax, 1));
4774 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4775 __ test(ecx, Immediate(kFailureTagMask));
4776 __ j(zero, &failure_returned, not_taken);
4777
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004778 ExternalReference pending_exception_address(
4779 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004780
4781 // Check that there is no pending exception, otherwise we
4782 // should have returned some failure value.
4783 if (FLAG_debug_code) {
4784 __ push(edx);
4785 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004786 ExternalReference::the_hole_value_location(masm->isolate())));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004787 NearLabel okay;
4788 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4789 // Cannot use check here as it attempts to generate call into runtime.
4790 __ j(equal, &okay);
4791 __ int3();
4792 __ bind(&okay);
4793 __ pop(edx);
4794 }
4795
ricow@chromium.org65fae842010-08-25 15:26:24 +00004796 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004797 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004798 __ ret(0);
4799
4800 // Handling of failure.
4801 __ bind(&failure_returned);
4802
4803 Label retry;
4804 // If the returned exception is RETRY_AFTER_GC continue at retry label
4805 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4806 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4807 __ j(zero, &retry, taken);
4808
4809 // Special handling of out of memory exceptions.
4810 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4811 __ j(equal, throw_out_of_memory_exception);
4812
4813 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004814 ExternalReference the_hole_location =
4815 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004816 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004817 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004818 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4819
4820 // Special handling of termination exceptions which are uncatchable
4821 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004822 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004823 __ j(equal, throw_termination_exception);
4824
4825 // Handle normal exception.
4826 __ jmp(throw_normal_exception);
4827
4828 // Retry.
4829 __ bind(&retry);
4830}
4831
4832
4833void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4834 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004835 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004836}
4837
4838
4839void CEntryStub::Generate(MacroAssembler* masm) {
4840 // eax: number of arguments including receiver
4841 // ebx: pointer to C function (C callee-saved)
4842 // ebp: frame pointer (restored after C call)
4843 // esp: stack pointer (restored after C call)
4844 // esi: current context (C callee-saved)
4845 // edi: JS function of the caller (C callee-saved)
4846
4847 // NOTE: Invocations of builtins may return failure objects instead
4848 // of a proper result. The builtin entry handles this by performing
4849 // a garbage collection and retrying the builtin (twice).
4850
4851 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004852 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004853
4854 // eax: result parameter for PerformGC, if any (setup below)
4855 // ebx: pointer to builtin function (C callee-saved)
4856 // ebp: frame pointer (restored after C call)
4857 // esp: stack pointer (restored after C call)
4858 // edi: number of arguments including receiver (C callee-saved)
4859 // esi: argv pointer (C callee-saved)
4860
4861 Label throw_normal_exception;
4862 Label throw_termination_exception;
4863 Label throw_out_of_memory_exception;
4864
4865 // Call into the runtime system.
4866 GenerateCore(masm,
4867 &throw_normal_exception,
4868 &throw_termination_exception,
4869 &throw_out_of_memory_exception,
4870 false,
4871 false);
4872
4873 // Do space-specific GC and retry runtime call.
4874 GenerateCore(masm,
4875 &throw_normal_exception,
4876 &throw_termination_exception,
4877 &throw_out_of_memory_exception,
4878 true,
4879 false);
4880
4881 // Do full GC and retry runtime call one final time.
4882 Failure* failure = Failure::InternalError();
4883 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4884 GenerateCore(masm,
4885 &throw_normal_exception,
4886 &throw_termination_exception,
4887 &throw_out_of_memory_exception,
4888 true,
4889 true);
4890
4891 __ bind(&throw_out_of_memory_exception);
4892 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4893
4894 __ bind(&throw_termination_exception);
4895 GenerateThrowUncatchable(masm, TERMINATION);
4896
4897 __ bind(&throw_normal_exception);
4898 GenerateThrowTOS(masm);
4899}
4900
4901
4902void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4903 Label invoke, exit;
4904#ifdef ENABLE_LOGGING_AND_PROFILING
4905 Label not_outermost_js, not_outermost_js_2;
4906#endif
4907
4908 // Setup frame.
4909 __ push(ebp);
4910 __ mov(ebp, Operand(esp));
4911
4912 // Push marker in two places.
4913 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4914 __ push(Immediate(Smi::FromInt(marker))); // context slot
4915 __ push(Immediate(Smi::FromInt(marker))); // function slot
4916 // Save callee-saved registers (C calling conventions).
4917 __ push(edi);
4918 __ push(esi);
4919 __ push(ebx);
4920
4921 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004922 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004923 __ push(Operand::StaticVariable(c_entry_fp));
4924
4925#ifdef ENABLE_LOGGING_AND_PROFILING
4926 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004927 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4928 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004929 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4930 __ j(not_equal, &not_outermost_js);
4931 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4932 __ bind(&not_outermost_js);
4933#endif
4934
4935 // Call a faked try-block that does the invoke.
4936 __ call(&invoke);
4937
4938 // Caught exception: Store result (exception) in the pending
4939 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004940 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4941 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004942 __ mov(Operand::StaticVariable(pending_exception), eax);
4943 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4944 __ jmp(&exit);
4945
4946 // Invoke: Link this frame into the handler chain.
4947 __ bind(&invoke);
4948 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4949
4950 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004951 ExternalReference the_hole_location =
4952 ExternalReference::the_hole_value_location(masm->isolate());
4953 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004954 __ mov(Operand::StaticVariable(pending_exception), edx);
4955
4956 // Fake a receiver (NULL).
4957 __ push(Immediate(0)); // receiver
4958
4959 // Invoke the function by calling through JS entry trampoline
4960 // builtin and pop the faked function when we return. Notice that we
4961 // cannot store a reference to the trampoline code directly in this
4962 // stub, because the builtin stubs may not have been generated yet.
4963 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004964 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004965 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004966 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004967 __ mov(edx, Immediate(construct_entry));
4968 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004969 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004970 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004971 __ mov(edx, Immediate(entry));
4972 }
4973 __ mov(edx, Operand(edx, 0)); // deref address
4974 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4975 __ call(Operand(edx));
4976
4977 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004978 __ pop(Operand::StaticVariable(ExternalReference(
4979 Isolate::k_handler_address,
4980 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004981 // Pop next_sp.
4982 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
4983
4984#ifdef ENABLE_LOGGING_AND_PROFILING
4985 // If current EBP value is the same as js_entry_sp value, it means that
4986 // the current function is the outermost.
4987 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
4988 __ j(not_equal, &not_outermost_js_2);
4989 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4990 __ bind(&not_outermost_js_2);
4991#endif
4992
4993 // Restore the top frame descriptor from the stack.
4994 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004995 __ pop(Operand::StaticVariable(ExternalReference(
4996 Isolate::k_c_entry_fp_address,
4997 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004998
4999 // Restore callee-saved registers (C calling conventions).
5000 __ pop(ebx);
5001 __ pop(esi);
5002 __ pop(edi);
5003 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
5004
5005 // Restore frame pointer and return.
5006 __ pop(ebp);
5007 __ ret(0);
5008}
5009
5010
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005011// Generate stub code for instanceof.
5012// This code can patch a call site inlined cache of the instance of check,
5013// which looks like this.
5014//
5015// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
5016// 75 0a jne <some near label>
5017// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
5018//
5019// If call site patching is requested the stack will have the delta from the
5020// return address to the cmp instruction just below the return address. This
5021// also means that call site patching can only take place with arguments in
5022// registers. TOS looks like this when call site patching is requested
5023//
5024// esp[0] : return address
5025// esp[4] : delta from return address to cmp instruction
5026//
ricow@chromium.org65fae842010-08-25 15:26:24 +00005027void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005028 // Call site inlining and patching implies arguments in registers.
5029 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
5030
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005031 // Fixed register usage throughout the stub.
5032 Register object = eax; // Object (lhs).
5033 Register map = ebx; // Map of the object.
5034 Register function = edx; // Function (rhs).
5035 Register prototype = edi; // Prototype of the function.
5036 Register scratch = ecx;
5037
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005038 // Constants describing the call site code to patch.
5039 static const int kDeltaToCmpImmediate = 2;
5040 static const int kDeltaToMov = 8;
5041 static const int kDeltaToMovImmediate = 9;
5042 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
5043 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
5044 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
5045
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005046 ExternalReference roots_address =
5047 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005048
5049 ASSERT_EQ(object.code(), InstanceofStub::left().code());
5050 ASSERT_EQ(function.code(), InstanceofStub::right().code());
5051
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005052 // Get the object and function - they are always both needed.
5053 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005054 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005055 __ mov(object, Operand(esp, 2 * kPointerSize));
5056 __ mov(function, Operand(esp, 1 * kPointerSize));
5057 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005058
5059 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005060 __ test(object, Immediate(kSmiTagMask));
5061 __ j(zero, &not_js_object, not_taken);
5062 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005063
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005064 // If there is a call site cache don't look in the global cache, but do the
5065 // real lookup and update the call site cache.
5066 if (!HasCallSiteInlineCheck()) {
5067 // Look up the function and the map in the instanceof cache.
5068 NearLabel miss;
5069 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5070 __ cmp(function,
5071 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5072 __ j(not_equal, &miss);
5073 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5074 __ cmp(map, Operand::StaticArray(
5075 scratch, times_pointer_size, roots_address));
5076 __ j(not_equal, &miss);
5077 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5078 __ mov(eax, Operand::StaticArray(
5079 scratch, times_pointer_size, roots_address));
5080 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
5081 __ bind(&miss);
5082 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005083
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005084 // Get the prototype of the function.
5085 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005086
5087 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005088 __ test(prototype, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005089 __ j(zero, &slow, not_taken);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005090 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005091
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005092 // Update the global instanceof or call site inlined cache with the current
5093 // map and function. The cached answer will be set when it is known below.
5094 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005095 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5096 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
5097 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5098 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
5099 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005100 } else {
5101 // The constants for the code patching are based on no push instructions
5102 // at the call site.
5103 ASSERT(HasArgsInRegisters());
5104 // Get return address and delta to inlined map check.
5105 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5106 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5107 if (FLAG_debug_code) {
5108 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
5109 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
5110 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
5111 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
5112 }
5113 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
5114 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005115
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005116 // Loop through the prototype chain of the object looking for the function
5117 // prototype.
5118 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005119 NearLabel loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005120 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005121 __ cmp(scratch, Operand(prototype));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005122 __ j(equal, &is_instance);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005123 Factory* factory = masm->isolate()->factory();
5124 __ cmp(Operand(scratch), Immediate(factory->null_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005125 __ j(equal, &is_not_instance);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005126 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5127 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005128 __ jmp(&loop);
5129
5130 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005131 if (!HasCallSiteInlineCheck()) {
5132 __ Set(eax, Immediate(0));
5133 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5134 __ mov(Operand::StaticArray(scratch,
5135 times_pointer_size, roots_address), eax);
5136 } else {
5137 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005138 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005139 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5140 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5141 if (FLAG_debug_code) {
5142 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5143 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5144 }
5145 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5146 if (!ReturnTrueFalseObject()) {
5147 __ Set(eax, Immediate(0));
5148 }
5149 }
5150 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005151
5152 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005153 if (!HasCallSiteInlineCheck()) {
5154 __ Set(eax, Immediate(Smi::FromInt(1)));
5155 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5156 __ mov(Operand::StaticArray(
5157 scratch, times_pointer_size, roots_address), eax);
5158 } else {
5159 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005160 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005161 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5162 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5163 if (FLAG_debug_code) {
5164 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5165 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5166 }
5167 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5168 if (!ReturnTrueFalseObject()) {
5169 __ Set(eax, Immediate(Smi::FromInt(1)));
5170 }
5171 }
5172 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005173
5174 Label object_not_null, object_not_null_or_smi;
5175 __ bind(&not_js_object);
5176 // Before null, smi and string value checks, check that the rhs is a function
5177 // as for a non-function rhs an exception needs to be thrown.
5178 __ test(function, Immediate(kSmiTagMask));
5179 __ j(zero, &slow, not_taken);
5180 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
5181 __ j(not_equal, &slow, not_taken);
5182
5183 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005184 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005185 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005186 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005187 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005188
5189 __ bind(&object_not_null);
5190 // Smi values is not instance of anything.
5191 __ test(object, Immediate(kSmiTagMask));
5192 __ j(not_zero, &object_not_null_or_smi, not_taken);
5193 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005194 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005195
5196 __ bind(&object_not_null_or_smi);
5197 // String values is not instance of anything.
5198 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
5199 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005200 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005201 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005202
5203 // Slow-case: Go through the JavaScript implementation.
5204 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005205 if (!ReturnTrueFalseObject()) {
5206 // Tail call the builtin which returns 0 or 1.
5207 if (HasArgsInRegisters()) {
5208 // Push arguments below return address.
5209 __ pop(scratch);
5210 __ push(object);
5211 __ push(function);
5212 __ push(scratch);
5213 }
5214 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
5215 } else {
5216 // Call the builtin and convert 0/1 to true/false.
5217 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005218 __ push(object);
5219 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005220 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
5221 __ LeaveInternalFrame();
5222 NearLabel true_value, done;
5223 __ test(eax, Operand(eax));
5224 __ j(zero, &true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005225 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005226 __ jmp(&done);
5227 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005228 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005229 __ bind(&done);
5230 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005231 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005232}
5233
5234
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005235Register InstanceofStub::left() { return eax; }
5236
5237
5238Register InstanceofStub::right() { return edx; }
5239
5240
ricow@chromium.org65fae842010-08-25 15:26:24 +00005241int CompareStub::MinorKey() {
5242 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5243 // stubs the never NaN NaN condition is only taken into account if the
5244 // condition is equals.
5245 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
5246 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5247 return ConditionField::encode(static_cast<unsigned>(cc_))
5248 | RegisterField::encode(false) // lhs_ and rhs_ are not used
5249 | StrictField::encode(strict_)
5250 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005251 | IncludeNumberCompareField::encode(include_number_compare_)
5252 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005253}
5254
5255
5256// Unfortunately you have to run without snapshots to see most of these
5257// names in the profile since most compare stubs end up in the snapshot.
5258const char* CompareStub::GetName() {
5259 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5260
5261 if (name_ != NULL) return name_;
5262 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005263 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
5264 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005265 if (name_ == NULL) return "OOM";
5266
5267 const char* cc_name;
5268 switch (cc_) {
5269 case less: cc_name = "LT"; break;
5270 case greater: cc_name = "GT"; break;
5271 case less_equal: cc_name = "LE"; break;
5272 case greater_equal: cc_name = "GE"; break;
5273 case equal: cc_name = "EQ"; break;
5274 case not_equal: cc_name = "NE"; break;
5275 default: cc_name = "UnknownCondition"; break;
5276 }
5277
5278 const char* strict_name = "";
5279 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
5280 strict_name = "_STRICT";
5281 }
5282
5283 const char* never_nan_nan_name = "";
5284 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
5285 never_nan_nan_name = "_NO_NAN";
5286 }
5287
5288 const char* include_number_compare_name = "";
5289 if (!include_number_compare_) {
5290 include_number_compare_name = "_NO_NUMBER";
5291 }
5292
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005293 const char* include_smi_compare_name = "";
5294 if (!include_smi_compare_) {
5295 include_smi_compare_name = "_NO_SMI";
5296 }
5297
ricow@chromium.org65fae842010-08-25 15:26:24 +00005298 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005299 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00005300 cc_name,
5301 strict_name,
5302 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005303 include_number_compare_name,
5304 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005305 return name_;
5306}
5307
5308
5309// -------------------------------------------------------------------------
5310// StringCharCodeAtGenerator
5311
5312void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
5313 Label flat_string;
5314 Label ascii_string;
5315 Label got_char_code;
5316
5317 // If the receiver is a smi trigger the non-string case.
5318 STATIC_ASSERT(kSmiTag == 0);
5319 __ test(object_, Immediate(kSmiTagMask));
5320 __ j(zero, receiver_not_string_);
5321
5322 // Fetch the instance type of the receiver into result register.
5323 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5324 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5325 // If the receiver is not a string trigger the non-string case.
5326 __ test(result_, Immediate(kIsNotStringMask));
5327 __ j(not_zero, receiver_not_string_);
5328
5329 // If the index is non-smi trigger the non-smi case.
5330 STATIC_ASSERT(kSmiTag == 0);
5331 __ test(index_, Immediate(kSmiTagMask));
5332 __ j(not_zero, &index_not_smi_);
5333
5334 // Put smi-tagged index into scratch register.
5335 __ mov(scratch_, index_);
5336 __ bind(&got_smi_index_);
5337
5338 // Check for index out of range.
5339 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
5340 __ j(above_equal, index_out_of_range_);
5341
5342 // We need special handling for non-flat strings.
5343 STATIC_ASSERT(kSeqStringTag == 0);
5344 __ test(result_, Immediate(kStringRepresentationMask));
5345 __ j(zero, &flat_string);
5346
5347 // Handle non-flat strings.
5348 __ test(result_, Immediate(kIsConsStringMask));
5349 __ j(zero, &call_runtime_);
5350
5351 // ConsString.
5352 // Check whether the right hand side is the empty string (i.e. if
5353 // this is really a flat string in a cons string). If that is not
5354 // the case we would rather go to the runtime system now to flatten
5355 // the string.
5356 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005357 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005358 __ j(not_equal, &call_runtime_);
5359 // Get the first of the two strings and load its instance type.
5360 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
5361 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5362 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5363 // If the first cons component is also non-flat, then go to runtime.
5364 STATIC_ASSERT(kSeqStringTag == 0);
5365 __ test(result_, Immediate(kStringRepresentationMask));
5366 __ j(not_zero, &call_runtime_);
5367
5368 // Check for 1-byte or 2-byte string.
5369 __ bind(&flat_string);
5370 STATIC_ASSERT(kAsciiStringTag != 0);
5371 __ test(result_, Immediate(kStringEncodingMask));
5372 __ j(not_zero, &ascii_string);
5373
5374 // 2-byte string.
5375 // Load the 2-byte character code into the result register.
5376 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
5377 __ movzx_w(result_, FieldOperand(object_,
5378 scratch_, times_1, // Scratch is smi-tagged.
5379 SeqTwoByteString::kHeaderSize));
5380 __ jmp(&got_char_code);
5381
5382 // ASCII string.
5383 // Load the byte into the result register.
5384 __ bind(&ascii_string);
5385 __ SmiUntag(scratch_);
5386 __ movzx_b(result_, FieldOperand(object_,
5387 scratch_, times_1,
5388 SeqAsciiString::kHeaderSize));
5389 __ bind(&got_char_code);
5390 __ SmiTag(result_);
5391 __ bind(&exit_);
5392}
5393
5394
5395void StringCharCodeAtGenerator::GenerateSlow(
5396 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5397 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5398
5399 // Index is not a smi.
5400 __ bind(&index_not_smi_);
5401 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005402 __ CheckMap(index_,
5403 masm->isolate()->factory()->heap_number_map(),
5404 index_not_number_,
5405 true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005406 call_helper.BeforeCall(masm);
5407 __ push(object_);
5408 __ push(index_);
5409 __ push(index_); // Consumed by runtime conversion function.
5410 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5411 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5412 } else {
5413 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5414 // NumberToSmi discards numbers that are not exact integers.
5415 __ CallRuntime(Runtime::kNumberToSmi, 1);
5416 }
5417 if (!scratch_.is(eax)) {
5418 // Save the conversion result before the pop instructions below
5419 // have a chance to overwrite it.
5420 __ mov(scratch_, eax);
5421 }
5422 __ pop(index_);
5423 __ pop(object_);
5424 // Reload the instance type.
5425 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5426 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5427 call_helper.AfterCall(masm);
5428 // If index is still not a smi, it must be out of range.
5429 STATIC_ASSERT(kSmiTag == 0);
5430 __ test(scratch_, Immediate(kSmiTagMask));
5431 __ j(not_zero, index_out_of_range_);
5432 // Otherwise, return to the fast path.
5433 __ jmp(&got_smi_index_);
5434
5435 // Call runtime. We get here when the receiver is a string and the
5436 // index is a number, but the code of getting the actual character
5437 // is too complex (e.g., when the string needs to be flattened).
5438 __ bind(&call_runtime_);
5439 call_helper.BeforeCall(masm);
5440 __ push(object_);
5441 __ push(index_);
5442 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5443 if (!result_.is(eax)) {
5444 __ mov(result_, eax);
5445 }
5446 call_helper.AfterCall(masm);
5447 __ jmp(&exit_);
5448
5449 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5450}
5451
5452
5453// -------------------------------------------------------------------------
5454// StringCharFromCodeGenerator
5455
5456void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5457 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5458 STATIC_ASSERT(kSmiTag == 0);
5459 STATIC_ASSERT(kSmiShiftSize == 0);
5460 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5461 __ test(code_,
5462 Immediate(kSmiTagMask |
5463 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
5464 __ j(not_zero, &slow_case_, not_taken);
5465
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005466 Factory* factory = masm->isolate()->factory();
5467 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005468 STATIC_ASSERT(kSmiTag == 0);
5469 STATIC_ASSERT(kSmiTagSize == 1);
5470 STATIC_ASSERT(kSmiShiftSize == 0);
5471 // At this point code register contains smi tagged ascii char code.
5472 __ mov(result_, FieldOperand(result_,
5473 code_, times_half_pointer_size,
5474 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005475 __ cmp(result_, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005476 __ j(equal, &slow_case_, not_taken);
5477 __ bind(&exit_);
5478}
5479
5480
5481void StringCharFromCodeGenerator::GenerateSlow(
5482 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5483 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5484
5485 __ bind(&slow_case_);
5486 call_helper.BeforeCall(masm);
5487 __ push(code_);
5488 __ CallRuntime(Runtime::kCharFromCode, 1);
5489 if (!result_.is(eax)) {
5490 __ mov(result_, eax);
5491 }
5492 call_helper.AfterCall(masm);
5493 __ jmp(&exit_);
5494
5495 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5496}
5497
5498
5499// -------------------------------------------------------------------------
5500// StringCharAtGenerator
5501
5502void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5503 char_code_at_generator_.GenerateFast(masm);
5504 char_from_code_generator_.GenerateFast(masm);
5505}
5506
5507
5508void StringCharAtGenerator::GenerateSlow(
5509 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5510 char_code_at_generator_.GenerateSlow(masm, call_helper);
5511 char_from_code_generator_.GenerateSlow(masm, call_helper);
5512}
5513
5514
5515void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005516 Label string_add_runtime, call_builtin;
5517 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005518
5519 // Load the two arguments.
5520 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5521 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5522
5523 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005524 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005525 __ test(eax, Immediate(kSmiTagMask));
5526 __ j(zero, &string_add_runtime);
5527 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
5528 __ j(above_equal, &string_add_runtime);
5529
5530 // First argument is a a string, test second.
5531 __ test(edx, Immediate(kSmiTagMask));
5532 __ j(zero, &string_add_runtime);
5533 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
5534 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005535 } else {
5536 // Here at least one of the arguments is definitely a string.
5537 // We convert the one that is not known to be a string.
5538 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5539 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5540 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5541 &call_builtin);
5542 builtin_id = Builtins::STRING_ADD_RIGHT;
5543 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5544 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5545 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5546 &call_builtin);
5547 builtin_id = Builtins::STRING_ADD_LEFT;
5548 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005549 }
5550
5551 // Both arguments are strings.
5552 // eax: first string
5553 // edx: second string
5554 // Check if either of the strings are empty. In that case return the other.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005555 NearLabel second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005556 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5557 STATIC_ASSERT(kSmiTag == 0);
5558 __ test(ecx, Operand(ecx));
5559 __ j(not_zero, &second_not_zero_length);
5560 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005561 Counters* counters = masm->isolate()->counters();
5562 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005563 __ ret(2 * kPointerSize);
5564 __ bind(&second_not_zero_length);
5565 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5566 STATIC_ASSERT(kSmiTag == 0);
5567 __ test(ebx, Operand(ebx));
5568 __ j(not_zero, &both_not_zero_length);
5569 // First string is empty, result is second string which is in edx.
5570 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005571 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005572 __ ret(2 * kPointerSize);
5573
5574 // Both strings are non-empty.
5575 // eax: first string
5576 // ebx: length of first string as a smi
5577 // ecx: length of second string as a smi
5578 // edx: second string
5579 // Look at the length of the result of adding the two strings.
5580 Label string_add_flat_result, longer_than_two;
5581 __ bind(&both_not_zero_length);
5582 __ add(ebx, Operand(ecx));
5583 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5584 // Handle exceptionally long strings in the runtime system.
5585 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005586 // Use the symbol table when adding two one character strings, as it
5587 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005588 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
5589 __ j(not_equal, &longer_than_two);
5590
5591 // Check that both strings are non-external ascii strings.
5592 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
5593 &string_add_runtime);
5594
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005595 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005596 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5597 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5598
5599 // Try to lookup two character string in symbol table. If it is not found
5600 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005601 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005602 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005603 masm, ebx, ecx, eax, edx, edi,
5604 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005605 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005606 __ ret(2 * kPointerSize);
5607
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005608 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005609 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005610 // Reload the arguments.
5611 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5612 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5613 // Get the two characters forming the new string.
5614 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5615 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5616 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005617 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005618 __ AllocateAsciiString(eax, // Result.
5619 2, // Length.
5620 edi, // Scratch 1.
5621 edx, // Scratch 2.
5622 &string_add_runtime);
5623 // Pack both characters in ebx.
5624 __ shl(ecx, kBitsPerByte);
5625 __ or_(ebx, Operand(ecx));
5626 // Set the characters in the new string.
5627 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005628 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005629 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005630
5631 __ bind(&longer_than_two);
5632 // Check if resulting string will be flat.
5633 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
5634 __ j(below, &string_add_flat_result);
5635
5636 // If result is not supposed to be flat allocate a cons string object. If both
5637 // strings are ascii the result is an ascii cons string.
5638 Label non_ascii, allocated, ascii_data;
5639 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5640 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5641 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5642 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5643 __ and_(ecx, Operand(edi));
5644 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5645 __ test(ecx, Immediate(kAsciiStringTag));
5646 __ j(zero, &non_ascii);
5647 __ bind(&ascii_data);
5648 // Allocate an acsii cons string.
5649 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5650 __ bind(&allocated);
5651 // Fill the fields of the cons string.
5652 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5653 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5654 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5655 Immediate(String::kEmptyHashField));
5656 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5657 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5658 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005659 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005660 __ ret(2 * kPointerSize);
5661 __ bind(&non_ascii);
5662 // At least one of the strings is two-byte. Check whether it happens
5663 // to contain only ascii characters.
5664 // ecx: first instance type AND second instance type.
5665 // edi: second instance type.
5666 __ test(ecx, Immediate(kAsciiDataHintMask));
5667 __ j(not_zero, &ascii_data);
5668 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5669 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5670 __ xor_(edi, Operand(ecx));
5671 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5672 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5673 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5674 __ j(equal, &ascii_data);
5675 // Allocate a two byte cons string.
5676 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5677 __ jmp(&allocated);
5678
5679 // Handle creating a flat result. First check that both strings are not
5680 // external strings.
5681 // eax: first string
5682 // ebx: length of resulting flat string as a smi
5683 // edx: second string
5684 __ bind(&string_add_flat_result);
5685 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5686 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5687 __ and_(ecx, kStringRepresentationMask);
5688 __ cmp(ecx, kExternalStringTag);
5689 __ j(equal, &string_add_runtime);
5690 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5691 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5692 __ and_(ecx, kStringRepresentationMask);
5693 __ cmp(ecx, kExternalStringTag);
5694 __ j(equal, &string_add_runtime);
5695 // Now check if both strings are ascii strings.
5696 // eax: first string
5697 // ebx: length of resulting flat string as a smi
5698 // edx: second string
5699 Label non_ascii_string_add_flat_result;
5700 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5701 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5702 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5703 __ j(zero, &non_ascii_string_add_flat_result);
5704 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5705 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5706 __ j(zero, &string_add_runtime);
5707
ricow@chromium.org65fae842010-08-25 15:26:24 +00005708 // Both strings are ascii strings. As they are short they are both flat.
5709 // ebx: length of resulting flat string as a smi
5710 __ SmiUntag(ebx);
5711 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5712 // eax: result string
5713 __ mov(ecx, eax);
5714 // Locate first character of result.
5715 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5716 // Load first argument and locate first character.
5717 __ mov(edx, Operand(esp, 2 * kPointerSize));
5718 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5719 __ SmiUntag(edi);
5720 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5721 // eax: result string
5722 // ecx: first character of result
5723 // edx: first char of first argument
5724 // edi: length of first argument
5725 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5726 // Load second argument and locate first character.
5727 __ mov(edx, Operand(esp, 1 * kPointerSize));
5728 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5729 __ SmiUntag(edi);
5730 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5731 // eax: result string
5732 // ecx: next character of result
5733 // edx: first char of second argument
5734 // edi: length of second argument
5735 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005736 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005737 __ ret(2 * kPointerSize);
5738
5739 // Handle creating a flat two byte result.
5740 // eax: first string - known to be two byte
5741 // ebx: length of resulting flat string as a smi
5742 // edx: second string
5743 __ bind(&non_ascii_string_add_flat_result);
5744 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5745 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5746 __ j(not_zero, &string_add_runtime);
5747 // Both strings are two byte strings. As they are short they are both
5748 // flat.
5749 __ SmiUntag(ebx);
5750 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5751 // eax: result string
5752 __ mov(ecx, eax);
5753 // Locate first character of result.
5754 __ add(Operand(ecx),
5755 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5756 // Load first argument and locate first character.
5757 __ mov(edx, Operand(esp, 2 * kPointerSize));
5758 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5759 __ SmiUntag(edi);
5760 __ add(Operand(edx),
5761 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5762 // eax: result string
5763 // ecx: first character of result
5764 // edx: first char of first argument
5765 // edi: length of first argument
5766 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5767 // Load second argument and locate first character.
5768 __ mov(edx, Operand(esp, 1 * kPointerSize));
5769 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5770 __ SmiUntag(edi);
5771 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5772 // eax: result string
5773 // ecx: next character of result
5774 // edx: first char of second argument
5775 // edi: length of second argument
5776 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005777 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005778 __ ret(2 * kPointerSize);
5779
5780 // Just jump to runtime to add the two strings.
5781 __ bind(&string_add_runtime);
5782 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005783
5784 if (call_builtin.is_linked()) {
5785 __ bind(&call_builtin);
5786 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5787 }
5788}
5789
5790
5791void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5792 int stack_offset,
5793 Register arg,
5794 Register scratch1,
5795 Register scratch2,
5796 Register scratch3,
5797 Label* slow) {
5798 // First check if the argument is already a string.
5799 Label not_string, done;
5800 __ test(arg, Immediate(kSmiTagMask));
5801 __ j(zero, &not_string);
5802 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5803 __ j(below, &done);
5804
5805 // Check the number to string cache.
5806 Label not_cached;
5807 __ bind(&not_string);
5808 // Puts the cached result into scratch1.
5809 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5810 arg,
5811 scratch1,
5812 scratch2,
5813 scratch3,
5814 false,
5815 &not_cached);
5816 __ mov(arg, scratch1);
5817 __ mov(Operand(esp, stack_offset), arg);
5818 __ jmp(&done);
5819
5820 // Check if the argument is a safe string wrapper.
5821 __ bind(&not_cached);
5822 __ test(arg, Immediate(kSmiTagMask));
5823 __ j(zero, slow);
5824 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5825 __ j(not_equal, slow);
5826 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5827 1 << Map::kStringWrapperSafeForDefaultValueOf);
5828 __ j(zero, slow);
5829 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5830 __ mov(Operand(esp, stack_offset), arg);
5831
5832 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005833}
5834
5835
5836void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5837 Register dest,
5838 Register src,
5839 Register count,
5840 Register scratch,
5841 bool ascii) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005842 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005843 __ bind(&loop);
5844 // This loop just copies one character at a time, as it is only used for very
5845 // short strings.
5846 if (ascii) {
5847 __ mov_b(scratch, Operand(src, 0));
5848 __ mov_b(Operand(dest, 0), scratch);
5849 __ add(Operand(src), Immediate(1));
5850 __ add(Operand(dest), Immediate(1));
5851 } else {
5852 __ mov_w(scratch, Operand(src, 0));
5853 __ mov_w(Operand(dest, 0), scratch);
5854 __ add(Operand(src), Immediate(2));
5855 __ add(Operand(dest), Immediate(2));
5856 }
5857 __ sub(Operand(count), Immediate(1));
5858 __ j(not_zero, &loop);
5859}
5860
5861
5862void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5863 Register dest,
5864 Register src,
5865 Register count,
5866 Register scratch,
5867 bool ascii) {
5868 // Copy characters using rep movs of doublewords.
5869 // The destination is aligned on a 4 byte boundary because we are
5870 // copying to the beginning of a newly allocated string.
5871 ASSERT(dest.is(edi)); // rep movs destination
5872 ASSERT(src.is(esi)); // rep movs source
5873 ASSERT(count.is(ecx)); // rep movs count
5874 ASSERT(!scratch.is(dest));
5875 ASSERT(!scratch.is(src));
5876 ASSERT(!scratch.is(count));
5877
5878 // Nothing to do for zero characters.
5879 Label done;
5880 __ test(count, Operand(count));
5881 __ j(zero, &done);
5882
5883 // Make count the number of bytes to copy.
5884 if (!ascii) {
5885 __ shl(count, 1);
5886 }
5887
5888 // Don't enter the rep movs if there are less than 4 bytes to copy.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005889 NearLabel last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005890 __ test(count, Immediate(~3));
5891 __ j(zero, &last_bytes);
5892
5893 // Copy from edi to esi using rep movs instruction.
5894 __ mov(scratch, count);
5895 __ sar(count, 2); // Number of doublewords to copy.
5896 __ cld();
5897 __ rep_movs();
5898
5899 // Find number of bytes left.
5900 __ mov(count, scratch);
5901 __ and_(count, 3);
5902
5903 // Check if there are more bytes to copy.
5904 __ bind(&last_bytes);
5905 __ test(count, Operand(count));
5906 __ j(zero, &done);
5907
5908 // Copy remaining characters.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005909 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005910 __ bind(&loop);
5911 __ mov_b(scratch, Operand(src, 0));
5912 __ mov_b(Operand(dest, 0), scratch);
5913 __ add(Operand(src), Immediate(1));
5914 __ add(Operand(dest), Immediate(1));
5915 __ sub(Operand(count), Immediate(1));
5916 __ j(not_zero, &loop);
5917
5918 __ bind(&done);
5919}
5920
5921
5922void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5923 Register c1,
5924 Register c2,
5925 Register scratch1,
5926 Register scratch2,
5927 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005928 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005929 Label* not_found) {
5930 // Register scratch3 is the general scratch register in this function.
5931 Register scratch = scratch3;
5932
5933 // Make sure that both characters are not digits as such strings has a
5934 // different hash algorithm. Don't try to look for these in the symbol table.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005935 NearLabel not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005936 __ mov(scratch, c1);
5937 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5938 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
5939 __ j(above, &not_array_index);
5940 __ mov(scratch, c2);
5941 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5942 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005943 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005944
5945 __ bind(&not_array_index);
5946 // Calculate the two character string hash.
5947 Register hash = scratch1;
5948 GenerateHashInit(masm, hash, c1, scratch);
5949 GenerateHashAddCharacter(masm, hash, c2, scratch);
5950 GenerateHashGetHash(masm, hash, scratch);
5951
5952 // Collect the two characters in a register.
5953 Register chars = c1;
5954 __ shl(c2, kBitsPerByte);
5955 __ or_(chars, Operand(c2));
5956
5957 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5958 // hash: hash of two character string.
5959
5960 // Load the symbol table.
5961 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005962 ExternalReference roots_address =
5963 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005964 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5965 __ mov(symbol_table,
5966 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5967
5968 // Calculate capacity mask from the symbol table capacity.
5969 Register mask = scratch2;
5970 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5971 __ SmiUntag(mask);
5972 __ sub(Operand(mask), Immediate(1));
5973
5974 // Registers
5975 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5976 // hash: hash of two character string
5977 // symbol_table: symbol table
5978 // mask: capacity mask
5979 // scratch: -
5980
5981 // Perform a number of probes in the symbol table.
5982 static const int kProbes = 4;
5983 Label found_in_symbol_table;
5984 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5985 for (int i = 0; i < kProbes; i++) {
5986 // Calculate entry in symbol table.
5987 __ mov(scratch, hash);
5988 if (i > 0) {
5989 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5990 }
5991 __ and_(scratch, Operand(mask));
5992
5993 // Load the entry from the symbol table.
5994 Register candidate = scratch; // Scratch register contains candidate.
5995 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5996 __ mov(candidate,
5997 FieldOperand(symbol_table,
5998 scratch,
5999 times_pointer_size,
6000 SymbolTable::kElementsStartOffset));
6001
6002 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006003 Factory* factory = masm->isolate()->factory();
6004 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00006005 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006006 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00006007 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006008
6009 // If length is not 2 the string is not a candidate.
6010 __ cmp(FieldOperand(candidate, String::kLengthOffset),
6011 Immediate(Smi::FromInt(2)));
6012 __ j(not_equal, &next_probe[i]);
6013
6014 // As we are out of registers save the mask on the stack and use that
6015 // register as a temporary.
6016 __ push(mask);
6017 Register temp = mask;
6018
6019 // Check that the candidate is a non-external ascii string.
6020 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
6021 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
6022 __ JumpIfInstanceTypeIsNotSequentialAscii(
6023 temp, temp, &next_probe_pop_mask[i]);
6024
6025 // Check if the two characters match.
6026 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
6027 __ and_(temp, 0x0000ffff);
6028 __ cmp(chars, Operand(temp));
6029 __ j(equal, &found_in_symbol_table);
6030 __ bind(&next_probe_pop_mask[i]);
6031 __ pop(mask);
6032 __ bind(&next_probe[i]);
6033 }
6034
6035 // No matching 2 character string found by probing.
6036 __ jmp(not_found);
6037
6038 // Scratch register contains result when we fall through to here.
6039 Register result = scratch;
6040 __ bind(&found_in_symbol_table);
6041 __ pop(mask); // Pop saved mask from the stack.
6042 if (!result.is(eax)) {
6043 __ mov(eax, result);
6044 }
6045}
6046
6047
6048void StringHelper::GenerateHashInit(MacroAssembler* masm,
6049 Register hash,
6050 Register character,
6051 Register scratch) {
6052 // hash = character + (character << 10);
6053 __ mov(hash, character);
6054 __ shl(hash, 10);
6055 __ add(hash, Operand(character));
6056 // hash ^= hash >> 6;
6057 __ mov(scratch, hash);
6058 __ sar(scratch, 6);
6059 __ xor_(hash, Operand(scratch));
6060}
6061
6062
6063void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
6064 Register hash,
6065 Register character,
6066 Register scratch) {
6067 // hash += character;
6068 __ add(hash, Operand(character));
6069 // hash += hash << 10;
6070 __ mov(scratch, hash);
6071 __ shl(scratch, 10);
6072 __ add(hash, Operand(scratch));
6073 // hash ^= hash >> 6;
6074 __ mov(scratch, hash);
6075 __ sar(scratch, 6);
6076 __ xor_(hash, Operand(scratch));
6077}
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);
6086 __ add(hash, Operand(scratch));
6087 // hash ^= hash >> 11;
6088 __ mov(scratch, hash);
6089 __ sar(scratch, 11);
6090 __ xor_(hash, Operand(scratch));
6091 // hash += hash << 15;
6092 __ mov(scratch, hash);
6093 __ shl(scratch, 15);
6094 __ add(hash, Operand(scratch));
6095
6096 // if (hash == 0) hash = 27;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006097 NearLabel hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006098 __ test(hash, Operand(hash));
6099 __ j(not_zero, &hash_not_zero);
6100 __ mov(hash, Immediate(27));
6101 __ bind(&hash_not_zero);
6102}
6103
6104
6105void SubStringStub::Generate(MacroAssembler* masm) {
6106 Label runtime;
6107
6108 // Stack frame on entry.
6109 // esp[0]: return address
6110 // esp[4]: to
6111 // esp[8]: from
6112 // esp[12]: string
6113
6114 // Make sure first argument is a string.
6115 __ mov(eax, Operand(esp, 3 * kPointerSize));
6116 STATIC_ASSERT(kSmiTag == 0);
6117 __ test(eax, Immediate(kSmiTagMask));
6118 __ j(zero, &runtime);
6119 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.
6128 __ test(ecx, Immediate(kSmiTagMask));
6129 __ j(not_zero, &runtime);
6130 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
6131 __ test(edx, Immediate(kSmiTagMask));
6132 __ j(not_zero, &runtime);
6133 __ sub(ecx, Operand(edx));
6134 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
6135 Label return_eax;
6136 __ j(equal, &return_eax);
6137 // Special handling of sub-strings of length 1 and 2. One character strings
6138 // are handled in the runtime system (looked up in the single character
6139 // cache). Two character strings are looked for in the symbol cache.
6140 __ SmiUntag(ecx); // Result length is no longer smi.
6141 __ cmp(ecx, 2);
6142 __ j(greater, &result_longer_than_two);
6143 __ j(less, &runtime);
6144
6145 // Sub string of length 2 requested.
6146 // eax: string
6147 // ebx: instance type
6148 // ecx: sub string length (value is 2)
6149 // edx: from index (smi)
6150 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
6151
6152 // Get the two characters forming the sub string.
6153 __ SmiUntag(edx); // From index is no longer smi.
6154 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
6155 __ movzx_b(ecx,
6156 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
6157
6158 // Try to lookup two character string in symbol table.
6159 Label make_two_character_string;
6160 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00006161 masm, ebx, ecx, eax, edx, edi,
6162 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006163 __ ret(3 * kPointerSize);
6164
6165 __ bind(&make_two_character_string);
6166 // Setup registers for allocating the two character string.
6167 __ mov(eax, Operand(esp, 3 * kPointerSize));
6168 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
6169 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
6170 __ Set(ecx, Immediate(2));
6171
6172 __ bind(&result_longer_than_two);
6173 // eax: string
6174 // ebx: instance type
6175 // ecx: result string length
6176 // Check for flat ascii string
6177 Label non_ascii_flat;
6178 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
6179
6180 // Allocate the result.
6181 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
6182
6183 // eax: result string
6184 // ecx: result string length
6185 __ mov(edx, esi); // esi used by following code.
6186 // Locate first character of result.
6187 __ mov(edi, eax);
6188 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6189 // Load string argument and locate character of sub string start.
6190 __ mov(esi, Operand(esp, 3 * kPointerSize));
6191 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6192 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6193 __ SmiUntag(ebx);
6194 __ add(esi, Operand(ebx));
6195
6196 // eax: result string
6197 // ecx: result length
6198 // edx: original value of esi
6199 // edi: first character of result
6200 // esi: character of sub string start
6201 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
6202 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006203 Counters* counters = masm->isolate()->counters();
6204 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006205 __ ret(3 * kPointerSize);
6206
6207 __ bind(&non_ascii_flat);
6208 // eax: string
6209 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
6210 // ecx: result string length
6211 // Check for flat two byte string
6212 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
6213 __ j(not_equal, &runtime);
6214
6215 // Allocate the result.
6216 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
6217
6218 // eax: result string
6219 // ecx: result string length
6220 __ mov(edx, esi); // esi used by following code.
6221 // Locate first character of result.
6222 __ mov(edi, eax);
6223 __ add(Operand(edi),
6224 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6225 // Load string argument and locate character of sub string start.
6226 __ mov(esi, Operand(esp, 3 * kPointerSize));
6227 __ add(Operand(esi),
6228 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6229 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6230 // As from is a smi it is 2 times the value which matches the size of a two
6231 // byte character.
6232 STATIC_ASSERT(kSmiTag == 0);
6233 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
6234 __ add(esi, Operand(ebx));
6235
6236 // eax: result string
6237 // ecx: result length
6238 // edx: original value of esi
6239 // edi: first character of result
6240 // esi: character of sub string start
6241 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
6242 __ mov(esi, edx); // Restore esi.
6243
6244 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006245 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006246 __ ret(3 * kPointerSize);
6247
6248 // Just jump to runtime to create the sub string.
6249 __ bind(&runtime);
6250 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6251}
6252
6253
6254void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6255 Register left,
6256 Register right,
6257 Register scratch1,
6258 Register scratch2,
6259 Register scratch3) {
6260 Label result_not_equal;
6261 Label result_greater;
6262 Label compare_lengths;
6263
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006264 Counters* counters = masm->isolate()->counters();
6265 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006266
6267 // Find minimum length.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006268 NearLabel left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006269 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
6270 __ mov(scratch3, scratch1);
6271 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
6272
6273 Register length_delta = scratch3;
6274
6275 __ j(less_equal, &left_shorter);
6276 // Right string is shorter. Change scratch1 to be length of right string.
6277 __ sub(scratch1, Operand(length_delta));
6278 __ bind(&left_shorter);
6279
6280 Register min_length = scratch1;
6281
6282 // If either length is zero, just compare lengths.
6283 __ test(min_length, Operand(min_length));
6284 __ j(zero, &compare_lengths);
6285
6286 // Change index to run from -min_length to -1 by adding min_length
6287 // to string start. This means that loop ends when index reaches zero,
6288 // which doesn't need an additional compare.
6289 __ SmiUntag(min_length);
6290 __ lea(left,
6291 FieldOperand(left,
6292 min_length, times_1,
6293 SeqAsciiString::kHeaderSize));
6294 __ lea(right,
6295 FieldOperand(right,
6296 min_length, times_1,
6297 SeqAsciiString::kHeaderSize));
6298 __ neg(min_length);
6299
6300 Register index = min_length; // index = -min_length;
6301
6302 {
6303 // Compare loop.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006304 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006305 __ bind(&loop);
6306 // Compare characters.
6307 __ mov_b(scratch2, Operand(left, index, times_1, 0));
6308 __ cmpb(scratch2, Operand(right, index, times_1, 0));
6309 __ j(not_equal, &result_not_equal);
6310 __ add(Operand(index), Immediate(1));
6311 __ j(not_zero, &loop);
6312 }
6313
6314 // Compare lengths - strings up to min-length are equal.
6315 __ bind(&compare_lengths);
6316 __ test(length_delta, Operand(length_delta));
6317 __ j(not_zero, &result_not_equal);
6318
6319 // Result is EQUAL.
6320 STATIC_ASSERT(EQUAL == 0);
6321 STATIC_ASSERT(kSmiTag == 0);
6322 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6323 __ ret(0);
6324
6325 __ bind(&result_not_equal);
6326 __ j(greater, &result_greater);
6327
6328 // Result is LESS.
6329 __ Set(eax, Immediate(Smi::FromInt(LESS)));
6330 __ ret(0);
6331
6332 // Result is GREATER.
6333 __ bind(&result_greater);
6334 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
6335 __ ret(0);
6336}
6337
6338
6339void StringCompareStub::Generate(MacroAssembler* masm) {
6340 Label runtime;
6341
6342 // Stack frame on entry.
6343 // esp[0]: return address
6344 // esp[4]: right string
6345 // esp[8]: left string
6346
6347 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
6348 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
6349
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006350 NearLabel not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006351 __ cmp(edx, Operand(eax));
6352 __ j(not_equal, &not_same);
6353 STATIC_ASSERT(EQUAL == 0);
6354 STATIC_ASSERT(kSmiTag == 0);
6355 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006356 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006357 __ ret(2 * kPointerSize);
6358
6359 __ bind(&not_same);
6360
6361 // Check that both objects are sequential ascii strings.
6362 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
6363
6364 // Compare flat ascii strings.
6365 // Drop arguments from the stack.
6366 __ pop(ecx);
6367 __ add(Operand(esp), Immediate(2 * kPointerSize));
6368 __ push(ecx);
6369 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
6370
6371 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6372 // tagged as a small integer.
6373 __ bind(&runtime);
6374 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6375}
6376
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006377
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006378void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6379 ASSERT(state_ == CompareIC::SMIS);
6380 NearLabel miss;
6381 __ mov(ecx, Operand(edx));
6382 __ or_(ecx, Operand(eax));
6383 __ test(ecx, Immediate(kSmiTagMask));
6384 __ j(not_zero, &miss, not_taken);
6385
6386 if (GetCondition() == equal) {
6387 // For equality we do not care about the sign of the result.
6388 __ sub(eax, Operand(edx));
6389 } else {
6390 NearLabel done;
6391 __ sub(edx, Operand(eax));
6392 __ j(no_overflow, &done);
6393 // Correct sign of result in case of overflow.
6394 __ not_(edx);
6395 __ bind(&done);
6396 __ mov(eax, edx);
6397 }
6398 __ ret(0);
6399
6400 __ bind(&miss);
6401 GenerateMiss(masm);
6402}
6403
6404
6405void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6406 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6407
6408 NearLabel generic_stub;
6409 NearLabel unordered;
6410 NearLabel miss;
6411 __ mov(ecx, Operand(edx));
6412 __ and_(ecx, Operand(eax));
6413 __ test(ecx, Immediate(kSmiTagMask));
6414 __ j(zero, &generic_stub, not_taken);
6415
6416 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
6417 __ j(not_equal, &miss, not_taken);
6418 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
6419 __ j(not_equal, &miss, not_taken);
6420
6421 // Inlining the double comparison and falling back to the general compare
6422 // stub if NaN is involved or SS2 or CMOV is unsupported.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006423 CpuFeatures* cpu_features = masm->isolate()->cpu_features();
6424 if (cpu_features->IsSupported(SSE2) && cpu_features->IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006425 CpuFeatures::Scope scope1(SSE2);
6426 CpuFeatures::Scope scope2(CMOV);
6427
6428 // Load left and right operand
6429 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
6430 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
6431
6432 // Compare operands
6433 __ ucomisd(xmm0, xmm1);
6434
6435 // Don't base result on EFLAGS when a NaN is involved.
6436 __ j(parity_even, &unordered, not_taken);
6437
6438 // Return a result of -1, 0, or 1, based on EFLAGS.
6439 // Performing mov, because xor would destroy the flag register.
6440 __ mov(eax, 0); // equal
6441 __ mov(ecx, Immediate(Smi::FromInt(1)));
6442 __ cmov(above, eax, Operand(ecx));
6443 __ mov(ecx, Immediate(Smi::FromInt(-1)));
6444 __ cmov(below, eax, Operand(ecx));
6445 __ ret(0);
6446
6447 __ bind(&unordered);
6448 }
6449
6450 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
6451 __ bind(&generic_stub);
6452 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
6453
6454 __ bind(&miss);
6455 GenerateMiss(masm);
6456}
6457
6458
6459void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6460 ASSERT(state_ == CompareIC::OBJECTS);
6461 NearLabel miss;
6462 __ mov(ecx, Operand(edx));
6463 __ and_(ecx, Operand(eax));
6464 __ test(ecx, Immediate(kSmiTagMask));
6465 __ j(zero, &miss, not_taken);
6466
6467 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
6468 __ j(not_equal, &miss, not_taken);
6469 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
6470 __ j(not_equal, &miss, not_taken);
6471
6472 ASSERT(GetCondition() == equal);
6473 __ sub(eax, Operand(edx));
6474 __ ret(0);
6475
6476 __ bind(&miss);
6477 GenerateMiss(masm);
6478}
6479
6480
6481void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6482 // Save the registers.
6483 __ pop(ecx);
6484 __ push(edx);
6485 __ push(eax);
6486 __ push(ecx);
6487
6488 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006489 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6490 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006491 __ EnterInternalFrame();
6492 __ push(edx);
6493 __ push(eax);
6494 __ push(Immediate(Smi::FromInt(op_)));
6495 __ CallExternalReference(miss, 3);
6496 __ LeaveInternalFrame();
6497
6498 // Compute the entry point of the rewritten stub.
6499 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6500
6501 // Restore registers.
6502 __ pop(ecx);
6503 __ pop(eax);
6504 __ pop(edx);
6505 __ push(ecx);
6506
6507 // Do a tail call to the rewritten stub.
6508 __ jmp(Operand(edi));
6509}
6510
6511
ricow@chromium.org65fae842010-08-25 15:26:24 +00006512#undef __
6513
6514} } // namespace v8::internal
6515
6516#endif // V8_TARGET_ARCH_IA32