blob: 78daf7cf420c69afe33b54e8d231798802d1bc10 [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.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000774 if (CpuFeatures::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);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000824 if (CpuFeatures::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;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +0000929 if (CpuFeatures::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.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001063 if (CpuFeatures::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;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001362 case TRBinaryOpIC::ODDBALL:
1363 GenerateOddballStub(masm);
1364 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001365 case TRBinaryOpIC::STRING:
1366 GenerateStringStub(masm);
1367 break;
1368 case TRBinaryOpIC::GENERIC:
1369 GenerateGeneric(masm);
1370 break;
1371 default:
1372 UNREACHABLE();
1373 }
1374}
1375
1376
1377const char* TypeRecordingBinaryOpStub::GetName() {
1378 if (name_ != NULL) return name_;
1379 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001380 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
1381 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001382 if (name_ == NULL) return "OOM";
1383 const char* op_name = Token::Name(op_);
1384 const char* overwrite_name;
1385 switch (mode_) {
1386 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1387 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1388 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1389 default: overwrite_name = "UnknownOverwrite"; break;
1390 }
1391
1392 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1393 "TypeRecordingBinaryOpStub_%s_%s_%s",
1394 op_name,
1395 overwrite_name,
1396 TRBinaryOpIC::GetName(operands_type_));
1397 return name_;
1398}
1399
1400
1401void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
1402 Label* slow,
1403 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
1404 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
1405 // dividend in eax and edx free for the division. Use eax, ebx for those.
1406 Comment load_comment(masm, "-- Load arguments");
1407 Register left = edx;
1408 Register right = eax;
1409 if (op_ == Token::DIV || op_ == Token::MOD) {
1410 left = eax;
1411 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001412 __ mov(ebx, eax);
1413 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001414 }
1415
1416
1417 // 2. Prepare the smi check of both operands by oring them together.
1418 Comment smi_check_comment(masm, "-- Smi check arguments");
1419 Label not_smis;
1420 Register combined = ecx;
1421 ASSERT(!left.is(combined) && !right.is(combined));
1422 switch (op_) {
1423 case Token::BIT_OR:
1424 // Perform the operation into eax and smi check the result. Preserve
1425 // eax in case the result is not a smi.
1426 ASSERT(!left.is(ecx) && !right.is(ecx));
1427 __ mov(ecx, right);
1428 __ or_(right, Operand(left)); // Bitwise or is commutative.
1429 combined = right;
1430 break;
1431
1432 case Token::BIT_XOR:
1433 case Token::BIT_AND:
1434 case Token::ADD:
1435 case Token::SUB:
1436 case Token::MUL:
1437 case Token::DIV:
1438 case Token::MOD:
1439 __ mov(combined, right);
1440 __ or_(combined, Operand(left));
1441 break;
1442
1443 case Token::SHL:
1444 case Token::SAR:
1445 case Token::SHR:
1446 // Move the right operand into ecx for the shift operation, use eax
1447 // for the smi check register.
1448 ASSERT(!left.is(ecx) && !right.is(ecx));
1449 __ mov(ecx, right);
1450 __ or_(right, Operand(left));
1451 combined = right;
1452 break;
1453
1454 default:
1455 break;
1456 }
1457
1458 // 3. Perform the smi check of the operands.
1459 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1460 __ test(combined, Immediate(kSmiTagMask));
1461 __ j(not_zero, &not_smis, not_taken);
1462
1463 // 4. Operands are both smis, perform the operation leaving the result in
1464 // eax and check the result if necessary.
1465 Comment perform_smi(masm, "-- Perform smi operation");
1466 Label use_fp_on_smis;
1467 switch (op_) {
1468 case Token::BIT_OR:
1469 // Nothing to do.
1470 break;
1471
1472 case Token::BIT_XOR:
1473 ASSERT(right.is(eax));
1474 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1475 break;
1476
1477 case Token::BIT_AND:
1478 ASSERT(right.is(eax));
1479 __ and_(right, Operand(left)); // Bitwise and is commutative.
1480 break;
1481
1482 case Token::SHL:
1483 // Remove tags from operands (but keep sign).
1484 __ SmiUntag(left);
1485 __ SmiUntag(ecx);
1486 // Perform the operation.
1487 __ shl_cl(left);
1488 // Check that the *signed* result fits in a smi.
1489 __ cmp(left, 0xc0000000);
1490 __ j(sign, &use_fp_on_smis, not_taken);
1491 // Tag the result and store it in register eax.
1492 __ SmiTag(left);
1493 __ mov(eax, left);
1494 break;
1495
1496 case Token::SAR:
1497 // Remove tags from operands (but keep sign).
1498 __ SmiUntag(left);
1499 __ SmiUntag(ecx);
1500 // Perform the operation.
1501 __ sar_cl(left);
1502 // Tag the result and store it in register eax.
1503 __ SmiTag(left);
1504 __ mov(eax, left);
1505 break;
1506
1507 case Token::SHR:
1508 // Remove tags from operands (but keep sign).
1509 __ SmiUntag(left);
1510 __ SmiUntag(ecx);
1511 // Perform the operation.
1512 __ shr_cl(left);
1513 // Check that the *unsigned* result fits in a smi.
1514 // Neither of the two high-order bits can be set:
1515 // - 0x80000000: high bit would be lost when smi tagging.
1516 // - 0x40000000: this number would convert to negative when
1517 // Smi tagging these two cases can only happen with shifts
1518 // by 0 or 1 when handed a valid smi.
1519 __ test(left, Immediate(0xc0000000));
1520 __ j(not_zero, slow, not_taken);
1521 // Tag the result and store it in register eax.
1522 __ SmiTag(left);
1523 __ mov(eax, left);
1524 break;
1525
1526 case Token::ADD:
1527 ASSERT(right.is(eax));
1528 __ add(right, Operand(left)); // Addition is commutative.
1529 __ j(overflow, &use_fp_on_smis, not_taken);
1530 break;
1531
1532 case Token::SUB:
1533 __ sub(left, Operand(right));
1534 __ j(overflow, &use_fp_on_smis, not_taken);
1535 __ mov(eax, left);
1536 break;
1537
1538 case Token::MUL:
1539 // If the smi tag is 0 we can just leave the tag on one operand.
1540 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1541 // We can't revert the multiplication if the result is not a smi
1542 // so save the right operand.
1543 __ mov(ebx, right);
1544 // Remove tag from one of the operands (but keep sign).
1545 __ SmiUntag(right);
1546 // Do multiplication.
1547 __ imul(right, Operand(left)); // Multiplication is commutative.
1548 __ j(overflow, &use_fp_on_smis, not_taken);
1549 // Check for negative zero result. Use combined = left | right.
1550 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1551 break;
1552
1553 case Token::DIV:
1554 // We can't revert the division if the result is not a smi so
1555 // save the left operand.
1556 __ mov(edi, left);
1557 // Check for 0 divisor.
1558 __ test(right, Operand(right));
1559 __ j(zero, &use_fp_on_smis, not_taken);
1560 // Sign extend left into edx:eax.
1561 ASSERT(left.is(eax));
1562 __ cdq();
1563 // Divide edx:eax by right.
1564 __ idiv(right);
1565 // Check for the corner case of dividing the most negative smi by
1566 // -1. We cannot use the overflow flag, since it is not set by idiv
1567 // instruction.
1568 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1569 __ cmp(eax, 0x40000000);
1570 __ j(equal, &use_fp_on_smis);
1571 // Check for negative zero result. Use combined = left | right.
1572 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1573 // Check that the remainder is zero.
1574 __ test(edx, Operand(edx));
1575 __ j(not_zero, &use_fp_on_smis);
1576 // Tag the result and store it in register eax.
1577 __ SmiTag(eax);
1578 break;
1579
1580 case Token::MOD:
1581 // Check for 0 divisor.
1582 __ test(right, Operand(right));
1583 __ j(zero, &not_smis, not_taken);
1584
1585 // Sign extend left into edx:eax.
1586 ASSERT(left.is(eax));
1587 __ cdq();
1588 // Divide edx:eax by right.
1589 __ idiv(right);
1590 // Check for negative zero result. Use combined = left | right.
1591 __ NegativeZeroTest(edx, combined, slow);
1592 // Move remainder to register eax.
1593 __ mov(eax, edx);
1594 break;
1595
1596 default:
1597 UNREACHABLE();
1598 }
1599
1600 // 5. Emit return of result in eax. Some operations have registers pushed.
1601 switch (op_) {
1602 case Token::ADD:
1603 case Token::SUB:
1604 case Token::MUL:
1605 case Token::DIV:
1606 __ ret(0);
1607 break;
1608 case Token::MOD:
1609 case Token::BIT_OR:
1610 case Token::BIT_AND:
1611 case Token::BIT_XOR:
1612 case Token::SAR:
1613 case Token::SHL:
1614 case Token::SHR:
1615 __ ret(2 * kPointerSize);
1616 break;
1617 default:
1618 UNREACHABLE();
1619 }
1620
1621 // 6. For some operations emit inline code to perform floating point
1622 // operations on known smis (e.g., if the result of the operation
1623 // overflowed the smi range).
1624 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1625 __ bind(&use_fp_on_smis);
1626 switch (op_) {
1627 // Undo the effects of some operations, and some register moves.
1628 case Token::SHL:
1629 // The arguments are saved on the stack, and only used from there.
1630 break;
1631 case Token::ADD:
1632 // Revert right = right + left.
1633 __ sub(right, Operand(left));
1634 break;
1635 case Token::SUB:
1636 // Revert left = left - right.
1637 __ add(left, Operand(right));
1638 break;
1639 case Token::MUL:
1640 // Right was clobbered but a copy is in ebx.
1641 __ mov(right, ebx);
1642 break;
1643 case Token::DIV:
1644 // Left was clobbered but a copy is in edi. Right is in ebx for
1645 // division. They should be in eax, ebx for jump to not_smi.
1646 __ mov(eax, edi);
1647 break;
1648 default:
1649 // No other operators jump to use_fp_on_smis.
1650 break;
1651 }
1652 __ jmp(&not_smis);
1653 } else {
1654 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1655 switch (op_) {
1656 case Token::SHL: {
1657 Comment perform_float(masm, "-- Perform float operation on smis");
1658 __ bind(&use_fp_on_smis);
1659 // Result we want is in left == edx, so we can put the allocated heap
1660 // number in eax.
1661 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1662 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001663 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001664 CpuFeatures::Scope use_sse2(SSE2);
1665 __ cvtsi2sd(xmm0, Operand(left));
1666 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1667 } else {
1668 // It's OK to overwrite the right argument on the stack because we
1669 // are about to return.
1670 __ mov(Operand(esp, 1 * kPointerSize), left);
1671 __ fild_s(Operand(esp, 1 * kPointerSize));
1672 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1673 }
1674 __ ret(2 * kPointerSize);
1675 break;
1676 }
1677
1678 case Token::ADD:
1679 case Token::SUB:
1680 case Token::MUL:
1681 case Token::DIV: {
1682 Comment perform_float(masm, "-- Perform float operation on smis");
1683 __ bind(&use_fp_on_smis);
1684 // Restore arguments to edx, eax.
1685 switch (op_) {
1686 case Token::ADD:
1687 // Revert right = right + left.
1688 __ sub(right, Operand(left));
1689 break;
1690 case Token::SUB:
1691 // Revert left = left - right.
1692 __ add(left, Operand(right));
1693 break;
1694 case Token::MUL:
1695 // Right was clobbered but a copy is in ebx.
1696 __ mov(right, ebx);
1697 break;
1698 case Token::DIV:
1699 // Left was clobbered but a copy is in edi. Right is in ebx for
1700 // division.
1701 __ mov(edx, edi);
1702 __ mov(eax, right);
1703 break;
1704 default: UNREACHABLE();
1705 break;
1706 }
1707 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001708 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001709 CpuFeatures::Scope use_sse2(SSE2);
1710 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1711 switch (op_) {
1712 case Token::ADD: __ addsd(xmm0, xmm1); break;
1713 case Token::SUB: __ subsd(xmm0, xmm1); break;
1714 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1715 case Token::DIV: __ divsd(xmm0, xmm1); break;
1716 default: UNREACHABLE();
1717 }
1718 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1719 } else { // SSE2 not available, use FPU.
1720 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1721 switch (op_) {
1722 case Token::ADD: __ faddp(1); break;
1723 case Token::SUB: __ fsubp(1); break;
1724 case Token::MUL: __ fmulp(1); break;
1725 case Token::DIV: __ fdivp(1); break;
1726 default: UNREACHABLE();
1727 }
1728 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1729 }
1730 __ mov(eax, ecx);
1731 __ ret(0);
1732 break;
1733 }
1734
1735 default:
1736 break;
1737 }
1738 }
1739
1740 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1741 // edx and eax.
1742 Comment done_comment(masm, "-- Enter non-smi code");
1743 __ bind(&not_smis);
1744 switch (op_) {
1745 case Token::BIT_OR:
1746 case Token::SHL:
1747 case Token::SAR:
1748 case Token::SHR:
1749 // Right operand is saved in ecx and eax was destroyed by the smi
1750 // check.
1751 __ mov(eax, ecx);
1752 break;
1753
1754 case Token::DIV:
1755 case Token::MOD:
1756 // Operands are in eax, ebx at this point.
1757 __ mov(edx, eax);
1758 __ mov(eax, ebx);
1759 break;
1760
1761 default:
1762 break;
1763 }
1764}
1765
1766
1767void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1768 Label call_runtime;
1769
1770 switch (op_) {
1771 case Token::ADD:
1772 case Token::SUB:
1773 case Token::MUL:
1774 case Token::DIV:
1775 break;
1776 case Token::MOD:
1777 case Token::BIT_OR:
1778 case Token::BIT_AND:
1779 case Token::BIT_XOR:
1780 case Token::SAR:
1781 case Token::SHL:
1782 case Token::SHR:
1783 GenerateRegisterArgsPush(masm);
1784 break;
1785 default:
1786 UNREACHABLE();
1787 }
1788
1789 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1790 result_type_ == TRBinaryOpIC::SMI) {
1791 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1792 } else {
1793 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1794 }
1795 __ bind(&call_runtime);
1796 switch (op_) {
1797 case Token::ADD:
1798 case Token::SUB:
1799 case Token::MUL:
1800 case Token::DIV:
1801 GenerateTypeTransition(masm);
1802 break;
1803 case Token::MOD:
1804 case Token::BIT_OR:
1805 case Token::BIT_AND:
1806 case Token::BIT_XOR:
1807 case Token::SAR:
1808 case Token::SHL:
1809 case Token::SHR:
1810 GenerateTypeTransitionWithSavedArgs(masm);
1811 break;
1812 default:
1813 UNREACHABLE();
1814 }
1815}
1816
1817
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001818void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001819 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1820 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001821 // Try to add arguments as strings, otherwise, transition to the generic
1822 // TRBinaryOpIC type.
1823 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001824 GenerateTypeTransition(masm);
1825}
1826
1827
1828void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1829 Label call_runtime;
1830 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1831
1832 // Floating point case.
1833 switch (op_) {
1834 case Token::ADD:
1835 case Token::SUB:
1836 case Token::MUL:
1837 case Token::DIV: {
1838 Label not_floats;
1839 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001840 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001841 CpuFeatures::Scope use_sse2(SSE2);
1842 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1843 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1844 switch (op_) {
1845 case Token::ADD: __ addsd(xmm0, xmm1); break;
1846 case Token::SUB: __ subsd(xmm0, xmm1); break;
1847 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1848 case Token::DIV: __ divsd(xmm0, xmm1); break;
1849 default: UNREACHABLE();
1850 }
1851 // Check result type if it is currently Int32.
1852 if (result_type_ <= TRBinaryOpIC::INT32) {
1853 __ cvttsd2si(ecx, Operand(xmm0));
1854 __ cvtsi2sd(xmm2, Operand(ecx));
1855 __ ucomisd(xmm0, xmm2);
1856 __ j(not_zero, &not_int32);
1857 __ j(carry, &not_int32);
1858 }
1859 GenerateHeapResultAllocation(masm, &call_runtime);
1860 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1861 __ ret(0);
1862 } else { // SSE2 not available, use FPU.
1863 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1864 FloatingPointHelper::LoadFloatOperands(
1865 masm,
1866 ecx,
1867 FloatingPointHelper::ARGS_IN_REGISTERS);
1868 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1869 switch (op_) {
1870 case Token::ADD: __ faddp(1); break;
1871 case Token::SUB: __ fsubp(1); break;
1872 case Token::MUL: __ fmulp(1); break;
1873 case Token::DIV: __ fdivp(1); break;
1874 default: UNREACHABLE();
1875 }
1876 Label after_alloc_failure;
1877 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1878 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1879 __ ret(0);
1880 __ bind(&after_alloc_failure);
1881 __ ffree();
1882 __ jmp(&call_runtime);
1883 }
1884
1885 __ bind(&not_floats);
1886 __ bind(&not_int32);
1887 GenerateTypeTransition(masm);
1888 break;
1889 }
1890
1891 case Token::MOD: {
1892 // For MOD we go directly to runtime in the non-smi case.
1893 break;
1894 }
1895 case Token::BIT_OR:
1896 case Token::BIT_AND:
1897 case Token::BIT_XOR:
1898 case Token::SAR:
1899 case Token::SHL:
1900 case Token::SHR: {
1901 GenerateRegisterArgsPush(masm);
1902 Label not_floats;
1903 Label not_int32;
1904 Label non_smi_result;
1905 /* {
1906 CpuFeatures::Scope use_sse2(SSE2);
1907 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1908 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1909 }*/
1910 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1911 use_sse3_,
1912 &not_floats);
1913 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1914 &not_int32);
1915 switch (op_) {
1916 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1917 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1918 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1919 case Token::SAR: __ sar_cl(eax); break;
1920 case Token::SHL: __ shl_cl(eax); break;
1921 case Token::SHR: __ shr_cl(eax); break;
1922 default: UNREACHABLE();
1923 }
1924 if (op_ == Token::SHR) {
1925 // Check if result is non-negative and fits in a smi.
1926 __ test(eax, Immediate(0xc0000000));
1927 __ j(not_zero, &call_runtime);
1928 } else {
1929 // Check if result fits in a smi.
1930 __ cmp(eax, 0xc0000000);
1931 __ j(negative, &non_smi_result);
1932 }
1933 // Tag smi result and return.
1934 __ SmiTag(eax);
1935 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1936
1937 // All ops except SHR return a signed int32 that we load in
1938 // a HeapNumber.
1939 if (op_ != Token::SHR) {
1940 __ bind(&non_smi_result);
1941 // Allocate a heap number if needed.
1942 __ mov(ebx, Operand(eax)); // ebx: result
1943 NearLabel skip_allocation;
1944 switch (mode_) {
1945 case OVERWRITE_LEFT:
1946 case OVERWRITE_RIGHT:
1947 // If the operand was an object, we skip the
1948 // allocation of a heap number.
1949 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1950 1 * kPointerSize : 2 * kPointerSize));
1951 __ test(eax, Immediate(kSmiTagMask));
1952 __ j(not_zero, &skip_allocation, not_taken);
1953 // Fall through!
1954 case NO_OVERWRITE:
1955 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1956 __ bind(&skip_allocation);
1957 break;
1958 default: UNREACHABLE();
1959 }
1960 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001961 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001962 CpuFeatures::Scope use_sse2(SSE2);
1963 __ cvtsi2sd(xmm0, Operand(ebx));
1964 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1965 } else {
1966 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1967 __ fild_s(Operand(esp, 1 * kPointerSize));
1968 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1969 }
1970 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1971 }
1972
1973 __ bind(&not_floats);
1974 __ bind(&not_int32);
1975 GenerateTypeTransitionWithSavedArgs(masm);
1976 break;
1977 }
1978 default: UNREACHABLE(); break;
1979 }
1980
1981 // If an allocation fails, or SHR or MOD hit a hard case,
1982 // use the runtime system to get the correct result.
1983 __ bind(&call_runtime);
1984
1985 switch (op_) {
1986 case Token::ADD:
1987 GenerateRegisterArgsPush(masm);
1988 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1989 break;
1990 case Token::SUB:
1991 GenerateRegisterArgsPush(masm);
1992 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1993 break;
1994 case Token::MUL:
1995 GenerateRegisterArgsPush(masm);
1996 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1997 break;
1998 case Token::DIV:
1999 GenerateRegisterArgsPush(masm);
2000 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2001 break;
2002 case Token::MOD:
2003 GenerateRegisterArgsPush(masm);
2004 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2005 break;
2006 case Token::BIT_OR:
2007 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2008 break;
2009 case Token::BIT_AND:
2010 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2011 break;
2012 case Token::BIT_XOR:
2013 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2014 break;
2015 case Token::SAR:
2016 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2017 break;
2018 case Token::SHL:
2019 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2020 break;
2021 case Token::SHR:
2022 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2023 break;
2024 default:
2025 UNREACHABLE();
2026 }
2027}
2028
2029
lrn@chromium.org7516f052011-03-30 08:52:27 +00002030void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
2031 Label call_runtime;
2032
2033 if (op_ == Token::ADD) {
2034 // Handle string addition here, because it is the only operation
2035 // that does not do a ToNumber conversion on the operands.
2036 GenerateAddStrings(masm);
2037 }
2038
2039 // Convert odd ball arguments to numbers.
2040 NearLabel check, done;
2041 __ cmp(edx, FACTORY->undefined_value());
2042 __ j(not_equal, &check);
2043 if (Token::IsBitOp(op_)) {
2044 __ xor_(edx, Operand(edx));
2045 } else {
2046 __ mov(edx, Immediate(FACTORY->nan_value()));
2047 }
2048 __ jmp(&done);
2049 __ bind(&check);
2050 __ cmp(eax, FACTORY->undefined_value());
2051 __ j(not_equal, &done);
2052 if (Token::IsBitOp(op_)) {
2053 __ xor_(eax, Operand(eax));
2054 } else {
2055 __ mov(eax, Immediate(FACTORY->nan_value()));
2056 }
2057 __ bind(&done);
2058
2059 GenerateHeapNumberStub(masm);
2060}
2061
2062
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002063void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
2064 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002065
2066 // Floating point case.
2067 switch (op_) {
2068 case Token::ADD:
2069 case Token::SUB:
2070 case Token::MUL:
2071 case Token::DIV: {
2072 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002073 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002074 CpuFeatures::Scope use_sse2(SSE2);
2075 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2076
2077 switch (op_) {
2078 case Token::ADD: __ addsd(xmm0, xmm1); break;
2079 case Token::SUB: __ subsd(xmm0, xmm1); break;
2080 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2081 case Token::DIV: __ divsd(xmm0, xmm1); break;
2082 default: UNREACHABLE();
2083 }
2084 GenerateHeapResultAllocation(masm, &call_runtime);
2085 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2086 __ ret(0);
2087 } else { // SSE2 not available, use FPU.
2088 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2089 FloatingPointHelper::LoadFloatOperands(
2090 masm,
2091 ecx,
2092 FloatingPointHelper::ARGS_IN_REGISTERS);
2093 switch (op_) {
2094 case Token::ADD: __ faddp(1); break;
2095 case Token::SUB: __ fsubp(1); break;
2096 case Token::MUL: __ fmulp(1); break;
2097 case Token::DIV: __ fdivp(1); break;
2098 default: UNREACHABLE();
2099 }
2100 Label after_alloc_failure;
2101 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2102 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2103 __ ret(0);
2104 __ bind(&after_alloc_failure);
2105 __ ffree();
2106 __ jmp(&call_runtime);
2107 }
2108
2109 __ bind(&not_floats);
2110 GenerateTypeTransition(masm);
2111 break;
2112 }
2113
2114 case Token::MOD: {
2115 // For MOD we go directly to runtime in the non-smi case.
2116 break;
2117 }
2118 case Token::BIT_OR:
2119 case Token::BIT_AND:
2120 case Token::BIT_XOR:
2121 case Token::SAR:
2122 case Token::SHL:
2123 case Token::SHR: {
2124 GenerateRegisterArgsPush(masm);
2125 Label not_floats;
2126 Label non_smi_result;
2127 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2128 use_sse3_,
2129 &not_floats);
2130 switch (op_) {
2131 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2132 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2133 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2134 case Token::SAR: __ sar_cl(eax); break;
2135 case Token::SHL: __ shl_cl(eax); break;
2136 case Token::SHR: __ shr_cl(eax); break;
2137 default: UNREACHABLE();
2138 }
2139 if (op_ == Token::SHR) {
2140 // Check if result is non-negative and fits in a smi.
2141 __ test(eax, Immediate(0xc0000000));
2142 __ j(not_zero, &call_runtime);
2143 } else {
2144 // Check if result fits in a smi.
2145 __ cmp(eax, 0xc0000000);
2146 __ j(negative, &non_smi_result);
2147 }
2148 // Tag smi result and return.
2149 __ SmiTag(eax);
2150 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2151
2152 // All ops except SHR return a signed int32 that we load in
2153 // a HeapNumber.
2154 if (op_ != Token::SHR) {
2155 __ bind(&non_smi_result);
2156 // Allocate a heap number if needed.
2157 __ mov(ebx, Operand(eax)); // ebx: result
2158 NearLabel skip_allocation;
2159 switch (mode_) {
2160 case OVERWRITE_LEFT:
2161 case OVERWRITE_RIGHT:
2162 // If the operand was an object, we skip the
2163 // allocation of a heap number.
2164 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2165 1 * kPointerSize : 2 * kPointerSize));
2166 __ test(eax, Immediate(kSmiTagMask));
2167 __ j(not_zero, &skip_allocation, not_taken);
2168 // Fall through!
2169 case NO_OVERWRITE:
2170 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2171 __ bind(&skip_allocation);
2172 break;
2173 default: UNREACHABLE();
2174 }
2175 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002176 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002177 CpuFeatures::Scope use_sse2(SSE2);
2178 __ cvtsi2sd(xmm0, Operand(ebx));
2179 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2180 } else {
2181 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2182 __ fild_s(Operand(esp, 1 * kPointerSize));
2183 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2184 }
2185 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2186 }
2187
2188 __ bind(&not_floats);
2189 GenerateTypeTransitionWithSavedArgs(masm);
2190 break;
2191 }
2192 default: UNREACHABLE(); break;
2193 }
2194
2195 // If an allocation fails, or SHR or MOD hit a hard case,
2196 // use the runtime system to get the correct result.
2197 __ bind(&call_runtime);
2198
2199 switch (op_) {
2200 case Token::ADD:
2201 GenerateRegisterArgsPush(masm);
2202 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2203 break;
2204 case Token::SUB:
2205 GenerateRegisterArgsPush(masm);
2206 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2207 break;
2208 case Token::MUL:
2209 GenerateRegisterArgsPush(masm);
2210 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2211 break;
2212 case Token::DIV:
2213 GenerateRegisterArgsPush(masm);
2214 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2215 break;
2216 case Token::MOD:
2217 GenerateRegisterArgsPush(masm);
2218 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2219 break;
2220 case Token::BIT_OR:
2221 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2222 break;
2223 case Token::BIT_AND:
2224 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2225 break;
2226 case Token::BIT_XOR:
2227 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2228 break;
2229 case Token::SAR:
2230 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2231 break;
2232 case Token::SHL:
2233 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2234 break;
2235 case Token::SHR:
2236 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2237 break;
2238 default:
2239 UNREACHABLE();
2240 }
2241}
2242
2243
2244void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
2245 Label call_runtime;
2246
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002247 Counters* counters = masm->isolate()->counters();
2248 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002249
2250 switch (op_) {
2251 case Token::ADD:
2252 case Token::SUB:
2253 case Token::MUL:
2254 case Token::DIV:
2255 break;
2256 case Token::MOD:
2257 case Token::BIT_OR:
2258 case Token::BIT_AND:
2259 case Token::BIT_XOR:
2260 case Token::SAR:
2261 case Token::SHL:
2262 case Token::SHR:
2263 GenerateRegisterArgsPush(masm);
2264 break;
2265 default:
2266 UNREACHABLE();
2267 }
2268
2269 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
2270
2271 // Floating point case.
2272 switch (op_) {
2273 case Token::ADD:
2274 case Token::SUB:
2275 case Token::MUL:
2276 case Token::DIV: {
2277 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002278 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002279 CpuFeatures::Scope use_sse2(SSE2);
2280 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2281
2282 switch (op_) {
2283 case Token::ADD: __ addsd(xmm0, xmm1); break;
2284 case Token::SUB: __ subsd(xmm0, xmm1); break;
2285 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2286 case Token::DIV: __ divsd(xmm0, xmm1); break;
2287 default: UNREACHABLE();
2288 }
2289 GenerateHeapResultAllocation(masm, &call_runtime);
2290 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2291 __ ret(0);
2292 } else { // SSE2 not available, use FPU.
2293 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2294 FloatingPointHelper::LoadFloatOperands(
2295 masm,
2296 ecx,
2297 FloatingPointHelper::ARGS_IN_REGISTERS);
2298 switch (op_) {
2299 case Token::ADD: __ faddp(1); break;
2300 case Token::SUB: __ fsubp(1); break;
2301 case Token::MUL: __ fmulp(1); break;
2302 case Token::DIV: __ fdivp(1); break;
2303 default: UNREACHABLE();
2304 }
2305 Label after_alloc_failure;
2306 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2307 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2308 __ ret(0);
2309 __ bind(&after_alloc_failure);
2310 __ ffree();
2311 __ jmp(&call_runtime);
2312 }
2313 __ bind(&not_floats);
2314 break;
2315 }
2316 case Token::MOD: {
2317 // For MOD we go directly to runtime in the non-smi case.
2318 break;
2319 }
2320 case Token::BIT_OR:
2321 case Token::BIT_AND:
2322 case Token::BIT_XOR:
2323 case Token::SAR:
2324 case Token::SHL:
2325 case Token::SHR: {
2326 Label non_smi_result;
2327 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2328 use_sse3_,
2329 &call_runtime);
2330 switch (op_) {
2331 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2332 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2333 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2334 case Token::SAR: __ sar_cl(eax); break;
2335 case Token::SHL: __ shl_cl(eax); break;
2336 case Token::SHR: __ shr_cl(eax); break;
2337 default: UNREACHABLE();
2338 }
2339 if (op_ == Token::SHR) {
2340 // Check if result is non-negative and fits in a smi.
2341 __ test(eax, Immediate(0xc0000000));
2342 __ j(not_zero, &call_runtime);
2343 } else {
2344 // Check if result fits in a smi.
2345 __ cmp(eax, 0xc0000000);
2346 __ j(negative, &non_smi_result);
2347 }
2348 // Tag smi result and return.
2349 __ SmiTag(eax);
2350 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
2351
2352 // All ops except SHR return a signed int32 that we load in
2353 // a HeapNumber.
2354 if (op_ != Token::SHR) {
2355 __ bind(&non_smi_result);
2356 // Allocate a heap number if needed.
2357 __ mov(ebx, Operand(eax)); // ebx: result
2358 NearLabel skip_allocation;
2359 switch (mode_) {
2360 case OVERWRITE_LEFT:
2361 case OVERWRITE_RIGHT:
2362 // If the operand was an object, we skip the
2363 // allocation of a heap number.
2364 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2365 1 * kPointerSize : 2 * kPointerSize));
2366 __ test(eax, Immediate(kSmiTagMask));
2367 __ j(not_zero, &skip_allocation, not_taken);
2368 // Fall through!
2369 case NO_OVERWRITE:
2370 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2371 __ bind(&skip_allocation);
2372 break;
2373 default: UNREACHABLE();
2374 }
2375 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002376 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002377 CpuFeatures::Scope use_sse2(SSE2);
2378 __ cvtsi2sd(xmm0, Operand(ebx));
2379 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2380 } else {
2381 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2382 __ fild_s(Operand(esp, 1 * kPointerSize));
2383 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2384 }
2385 __ ret(2 * kPointerSize);
2386 }
2387 break;
2388 }
2389 default: UNREACHABLE(); break;
2390 }
2391
2392 // If all else fails, use the runtime system to get the correct
2393 // result.
2394 __ bind(&call_runtime);
2395 switch (op_) {
2396 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002397 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002398 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002399 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2400 break;
2401 }
2402 case Token::SUB:
2403 GenerateRegisterArgsPush(masm);
2404 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2405 break;
2406 case Token::MUL:
2407 GenerateRegisterArgsPush(masm);
2408 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2409 break;
2410 case Token::DIV:
2411 GenerateRegisterArgsPush(masm);
2412 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2413 break;
2414 case Token::MOD:
2415 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2416 break;
2417 case Token::BIT_OR:
2418 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2419 break;
2420 case Token::BIT_AND:
2421 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2422 break;
2423 case Token::BIT_XOR:
2424 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2425 break;
2426 case Token::SAR:
2427 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2428 break;
2429 case Token::SHL:
2430 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2431 break;
2432 case Token::SHR:
2433 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2434 break;
2435 default:
2436 UNREACHABLE();
2437 }
2438}
2439
2440
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002441void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002442 ASSERT(op_ == Token::ADD);
2443 NearLabel left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002444
2445 // Registers containing left and right operands respectively.
2446 Register left = edx;
2447 Register right = eax;
2448
2449 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002450 __ test(left, Immediate(kSmiTagMask));
2451 __ j(zero, &left_not_string);
2452 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
2453 __ j(above_equal, &left_not_string);
2454
2455 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2456 GenerateRegisterArgsPush(masm);
2457 __ TailCallStub(&string_add_left_stub);
2458
2459 // Left operand is not a string, test right.
2460 __ bind(&left_not_string);
2461 __ test(right, Immediate(kSmiTagMask));
2462 __ j(zero, &call_runtime);
2463 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
2464 __ j(above_equal, &call_runtime);
2465
2466 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2467 GenerateRegisterArgsPush(masm);
2468 __ TailCallStub(&string_add_right_stub);
2469
2470 // Neither argument is a string.
2471 __ bind(&call_runtime);
2472}
2473
2474
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002475void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2476 MacroAssembler* masm,
2477 Label* alloc_failure) {
2478 Label skip_allocation;
2479 OverwriteMode mode = mode_;
2480 switch (mode) {
2481 case OVERWRITE_LEFT: {
2482 // If the argument in edx is already an object, we skip the
2483 // allocation of a heap number.
2484 __ test(edx, Immediate(kSmiTagMask));
2485 __ j(not_zero, &skip_allocation, not_taken);
2486 // Allocate a heap number for the result. Keep eax and edx intact
2487 // for the possible runtime call.
2488 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2489 // Now edx can be overwritten losing one of the arguments as we are
2490 // now done and will not need it any more.
2491 __ mov(edx, Operand(ebx));
2492 __ bind(&skip_allocation);
2493 // Use object in edx as a result holder
2494 __ mov(eax, Operand(edx));
2495 break;
2496 }
2497 case OVERWRITE_RIGHT:
2498 // If the argument in eax is already an object, we skip the
2499 // allocation of a heap number.
2500 __ test(eax, Immediate(kSmiTagMask));
2501 __ j(not_zero, &skip_allocation, not_taken);
2502 // Fall through!
2503 case NO_OVERWRITE:
2504 // Allocate a heap number for the result. Keep eax and edx intact
2505 // for the possible runtime call.
2506 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2507 // Now eax can be overwritten losing one of the arguments as we are
2508 // now done and will not need it any more.
2509 __ mov(eax, ebx);
2510 __ bind(&skip_allocation);
2511 break;
2512 default: UNREACHABLE();
2513 }
2514}
2515
2516
2517void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2518 __ pop(ecx);
2519 __ push(edx);
2520 __ push(eax);
2521 __ push(ecx);
2522}
2523
2524
ricow@chromium.org65fae842010-08-25 15:26:24 +00002525void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002526 // TAGGED case:
2527 // Input:
2528 // esp[4]: tagged number input argument (should be number).
2529 // esp[0]: return address.
2530 // Output:
2531 // eax: tagged double result.
2532 // UNTAGGED case:
2533 // Input::
2534 // esp[0]: return address.
2535 // xmm1: untagged double input argument
2536 // Output:
2537 // xmm1: untagged double result.
2538
ricow@chromium.org65fae842010-08-25 15:26:24 +00002539 Label runtime_call;
2540 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002541 Label skip_cache;
2542 const bool tagged = (argument_type_ == TAGGED);
2543 if (tagged) {
2544 // Test that eax is a number.
2545 NearLabel input_not_smi;
2546 NearLabel loaded;
2547 __ mov(eax, Operand(esp, kPointerSize));
2548 __ test(eax, Immediate(kSmiTagMask));
2549 __ j(not_zero, &input_not_smi);
2550 // Input is a smi. Untag and load it onto the FPU stack.
2551 // Then load the low and high words of the double into ebx, edx.
2552 STATIC_ASSERT(kSmiTagSize == 1);
2553 __ sar(eax, 1);
2554 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2555 __ mov(Operand(esp, 0), eax);
2556 __ fild_s(Operand(esp, 0));
2557 __ fst_d(Operand(esp, 0));
2558 __ pop(edx);
2559 __ pop(ebx);
2560 __ jmp(&loaded);
2561 __ bind(&input_not_smi);
2562 // Check if input is a HeapNumber.
2563 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002564 Factory* factory = masm->isolate()->factory();
2565 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002566 __ j(not_equal, &runtime_call);
2567 // Input is a HeapNumber. Push it on the FPU stack and load its
2568 // low and high words into ebx, edx.
2569 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2570 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2571 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002572
whesse@chromium.org023421e2010-12-21 12:19:12 +00002573 __ bind(&loaded);
2574 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002575 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002576 CpuFeatures::Scope sse4_scope(SSE4_1);
2577 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2578 } else {
2579 __ pshufd(xmm0, xmm1, 0x1);
2580 __ movd(Operand(edx), xmm0);
2581 }
2582 __ movd(Operand(ebx), xmm1);
2583 }
2584
2585 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002586 // ebx = low 32 bits of double value
2587 // edx = high 32 bits of double value
2588 // Compute hash (the shifts are arithmetic):
2589 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2590 __ mov(ecx, ebx);
2591 __ xor_(ecx, Operand(edx));
2592 __ mov(eax, ecx);
2593 __ sar(eax, 16);
2594 __ xor_(ecx, Operand(eax));
2595 __ mov(eax, ecx);
2596 __ sar(eax, 8);
2597 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002598 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2599 __ and_(Operand(ecx),
2600 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002601
whesse@chromium.org023421e2010-12-21 12:19:12 +00002602 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002603 // ebx = low 32 bits of double value.
2604 // edx = high 32 bits of double value.
2605 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002606 ExternalReference cache_array =
2607 ExternalReference::transcendental_cache_array_address(masm->isolate());
2608 __ mov(eax, Immediate(cache_array));
2609 int cache_array_index =
2610 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2611 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002612 // Eax points to the cache for the type type_.
2613 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2614 __ test(eax, Operand(eax));
2615 __ j(zero, &runtime_call_clear_stack);
2616#ifdef DEBUG
2617 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002618 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002619 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2620 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2621 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2622 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2623 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2624 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2625 CHECK_EQ(0, elem_in0 - elem_start);
2626 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2627 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2628 }
2629#endif
2630 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2631 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2632 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2633 // Check if cache matches: Double value is stored in uint32_t[2] array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002634 NearLabel cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002635 __ cmp(ebx, Operand(ecx, 0));
2636 __ j(not_equal, &cache_miss);
2637 __ cmp(edx, Operand(ecx, kIntSize));
2638 __ j(not_equal, &cache_miss);
2639 // Cache hit!
2640 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002641 if (tagged) {
2642 __ fstp(0);
2643 __ ret(kPointerSize);
2644 } else { // UNTAGGED.
2645 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2646 __ Ret();
2647 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002648
2649 __ bind(&cache_miss);
2650 // Update cache with new value.
2651 // We are short on registers, so use no_reg as scratch.
2652 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002653 if (tagged) {
2654 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2655 } else { // UNTAGGED.
2656 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2657 __ sub(Operand(esp), Immediate(kDoubleSize));
2658 __ movdbl(Operand(esp, 0), xmm1);
2659 __ fld_d(Operand(esp, 0));
2660 __ add(Operand(esp), Immediate(kDoubleSize));
2661 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002662 GenerateOperation(masm);
2663 __ mov(Operand(ecx, 0), ebx);
2664 __ mov(Operand(ecx, kIntSize), edx);
2665 __ mov(Operand(ecx, 2 * kIntSize), eax);
2666 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002667 if (tagged) {
2668 __ ret(kPointerSize);
2669 } else { // UNTAGGED.
2670 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2671 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002672
whesse@chromium.org023421e2010-12-21 12:19:12 +00002673 // Skip cache and return answer directly, only in untagged case.
2674 __ bind(&skip_cache);
2675 __ sub(Operand(esp), Immediate(kDoubleSize));
2676 __ movdbl(Operand(esp, 0), xmm1);
2677 __ fld_d(Operand(esp, 0));
2678 GenerateOperation(masm);
2679 __ fstp_d(Operand(esp, 0));
2680 __ movdbl(xmm1, Operand(esp, 0));
2681 __ add(Operand(esp), Immediate(kDoubleSize));
2682 // We return the value in xmm1 without adding it to the cache, but
2683 // we cause a scavenging GC so that future allocations will succeed.
2684 __ EnterInternalFrame();
2685 // Allocate an unused object bigger than a HeapNumber.
2686 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2687 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2688 __ LeaveInternalFrame();
2689 __ Ret();
2690 }
2691
2692 // Call runtime, doing whatever allocation and cleanup is necessary.
2693 if (tagged) {
2694 __ bind(&runtime_call_clear_stack);
2695 __ fstp(0);
2696 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002697 ExternalReference runtime =
2698 ExternalReference(RuntimeFunction(), masm->isolate());
2699 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002700 } else { // UNTAGGED.
2701 __ bind(&runtime_call_clear_stack);
2702 __ bind(&runtime_call);
2703 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2704 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2705 __ EnterInternalFrame();
2706 __ push(eax);
2707 __ CallRuntime(RuntimeFunction(), 1);
2708 __ LeaveInternalFrame();
2709 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2710 __ Ret();
2711 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002712}
2713
2714
2715Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2716 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002717 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2718 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002719 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002720 default:
2721 UNIMPLEMENTED();
2722 return Runtime::kAbort;
2723 }
2724}
2725
2726
2727void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2728 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002729 // Input value is on FP stack, and also in ebx/edx.
2730 // Input value is possibly in xmm1.
2731 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002732 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2733 // Both fsin and fcos require arguments in the range +/-2^63 and
2734 // return NaN for infinities and NaN. They can share all code except
2735 // the actual fsin/fcos operation.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002736 NearLabel in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002737 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2738 // work. We must reduce it to the appropriate range.
2739 __ mov(edi, edx);
2740 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2741 int supported_exponent_limit =
2742 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2743 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
2744 __ j(below, &in_range, taken);
2745 // Check for infinity and NaN. Both return NaN for sin.
2746 __ cmp(Operand(edi), Immediate(0x7ff00000));
2747 NearLabel non_nan_result;
2748 __ j(not_equal, &non_nan_result, taken);
2749 // Input is +/-Infinity or NaN. Result is NaN.
2750 __ fstp(0);
2751 // NaN is represented by 0x7ff8000000000000.
2752 __ push(Immediate(0x7ff80000));
2753 __ push(Immediate(0));
2754 __ fld_d(Operand(esp, 0));
2755 __ add(Operand(esp), Immediate(2 * kPointerSize));
2756 __ jmp(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002757
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002758 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002759
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002760 // Use fpmod to restrict argument to the range +/-2*PI.
2761 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2762 __ fldpi();
2763 __ fadd(0);
2764 __ fld(1);
2765 // FPU Stack: input, 2*pi, input.
2766 {
2767 NearLabel no_exceptions;
2768 __ fwait();
2769 __ fnstsw_ax();
2770 // Clear if Illegal Operand or Zero Division exceptions are set.
2771 __ test(Operand(eax), Immediate(5));
2772 __ j(zero, &no_exceptions);
2773 __ fnclex();
2774 __ bind(&no_exceptions);
2775 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002776
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002777 // Compute st(0) % st(1)
2778 {
2779 NearLabel partial_remainder_loop;
2780 __ bind(&partial_remainder_loop);
2781 __ fprem1();
2782 __ fwait();
2783 __ fnstsw_ax();
2784 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2785 // If C2 is set, computation only has partial result. Loop to
2786 // continue computation.
2787 __ j(not_zero, &partial_remainder_loop);
2788 }
2789 // FPU Stack: input, 2*pi, input % 2*pi
2790 __ fstp(2);
2791 __ fstp(0);
2792 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2793
2794 // FPU Stack: input % 2*pi
2795 __ bind(&in_range);
2796 switch (type_) {
2797 case TranscendentalCache::SIN:
2798 __ fsin();
2799 break;
2800 case TranscendentalCache::COS:
2801 __ fcos();
2802 break;
2803 default:
2804 UNREACHABLE();
2805 }
2806 __ bind(&done);
2807 } else {
2808 ASSERT(type_ == TranscendentalCache::LOG);
2809 __ fldln2();
2810 __ fxch();
2811 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002812 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002813}
2814
2815
2816// Get the integer part of a heap number. Surprisingly, all this bit twiddling
2817// is faster than using the built-in instructions on floating point registers.
2818// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
2819// trashed registers.
2820void IntegerConvert(MacroAssembler* masm,
2821 Register source,
2822 TypeInfo type_info,
2823 bool use_sse3,
2824 Label* conversion_failure) {
2825 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
2826 Label done, right_exponent, normal_exponent;
2827 Register scratch = ebx;
2828 Register scratch2 = edi;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002829 if (type_info.IsInteger32() && CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002830 CpuFeatures::Scope scope(SSE2);
2831 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
2832 return;
2833 }
2834 if (!type_info.IsInteger32() || !use_sse3) {
2835 // Get exponent word.
2836 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
2837 // Get exponent alone in scratch2.
2838 __ mov(scratch2, scratch);
2839 __ and_(scratch2, HeapNumber::kExponentMask);
2840 }
2841 if (use_sse3) {
2842 CpuFeatures::Scope scope(SSE3);
2843 if (!type_info.IsInteger32()) {
2844 // Check whether the exponent is too big for a 64 bit signed integer.
2845 static const uint32_t kTooBigExponent =
2846 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2847 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
2848 __ j(greater_equal, conversion_failure);
2849 }
2850 // Load x87 register with heap number.
2851 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
2852 // Reserve space for 64 bit answer.
2853 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2854 // Do conversion, which cannot fail because we checked the exponent.
2855 __ fisttp_d(Operand(esp, 0));
2856 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
2857 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2858 } else {
2859 // Load ecx with zero. We use this either for the final shift or
2860 // for the answer.
2861 __ xor_(ecx, Operand(ecx));
2862 // Check whether the exponent matches a 32 bit signed int that cannot be
2863 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
2864 // exponent is 30 (biased). This is the exponent that we are fastest at and
2865 // also the highest exponent we can handle here.
2866 const uint32_t non_smi_exponent =
2867 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
2868 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
2869 // If we have a match of the int32-but-not-Smi exponent then skip some
2870 // logic.
2871 __ j(equal, &right_exponent);
2872 // If the exponent is higher than that then go to slow case. This catches
2873 // numbers that don't fit in a signed int32, infinities and NaNs.
2874 __ j(less, &normal_exponent);
2875
2876 {
2877 // Handle a big exponent. The only reason we have this code is that the
2878 // >>> operator has a tendency to generate numbers with an exponent of 31.
2879 const uint32_t big_non_smi_exponent =
2880 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
2881 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
2882 __ j(not_equal, conversion_failure);
2883 // We have the big exponent, typically from >>>. This means the number is
2884 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
2885 __ mov(scratch2, scratch);
2886 __ and_(scratch2, HeapNumber::kMantissaMask);
2887 // Put back the implicit 1.
2888 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
2889 // Shift up the mantissa bits to take up the space the exponent used to
2890 // take. We just orred in the implicit bit so that took care of one and
2891 // we want to use the full unsigned range so we subtract 1 bit from the
2892 // shift distance.
2893 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
2894 __ shl(scratch2, big_shift_distance);
2895 // Get the second half of the double.
2896 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
2897 // Shift down 21 bits to get the most significant 11 bits or the low
2898 // mantissa word.
2899 __ shr(ecx, 32 - big_shift_distance);
2900 __ or_(ecx, Operand(scratch2));
2901 // We have the answer in ecx, but we may need to negate it.
2902 __ test(scratch, Operand(scratch));
2903 __ j(positive, &done);
2904 __ neg(ecx);
2905 __ jmp(&done);
2906 }
2907
2908 __ bind(&normal_exponent);
2909 // Exponent word in scratch, exponent part of exponent word in scratch2.
2910 // Zero in ecx.
2911 // We know the exponent is smaller than 30 (biased). If it is less than
2912 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
2913 // it rounds to zero.
2914 const uint32_t zero_exponent =
2915 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
2916 __ sub(Operand(scratch2), Immediate(zero_exponent));
2917 // ecx already has a Smi zero.
2918 __ j(less, &done);
2919
2920 // We have a shifted exponent between 0 and 30 in scratch2.
2921 __ shr(scratch2, HeapNumber::kExponentShift);
2922 __ mov(ecx, Immediate(30));
2923 __ sub(ecx, Operand(scratch2));
2924
2925 __ bind(&right_exponent);
2926 // Here ecx is the shift, scratch is the exponent word.
2927 // Get the top bits of the mantissa.
2928 __ and_(scratch, HeapNumber::kMantissaMask);
2929 // Put back the implicit 1.
2930 __ or_(scratch, 1 << HeapNumber::kExponentShift);
2931 // Shift up the mantissa bits to take up the space the exponent used to
2932 // take. We have kExponentShift + 1 significant bits int he low end of the
2933 // word. Shift them to the top bits.
2934 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2935 __ shl(scratch, shift_distance);
2936 // Get the second half of the double. For some exponents we don't
2937 // actually need this because the bits get shifted out again, but
2938 // it's probably slower to test than just to do it.
2939 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
2940 // Shift down 22 bits to get the most significant 10 bits or the low
2941 // mantissa word.
2942 __ shr(scratch2, 32 - shift_distance);
2943 __ or_(scratch2, Operand(scratch));
2944 // Move down according to the exponent.
2945 __ shr_cl(scratch2);
2946 // Now the unsigned answer is in scratch2. We need to move it to ecx and
2947 // we may need to fix the sign.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002948 NearLabel negative;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002949 __ xor_(ecx, Operand(ecx));
2950 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
2951 __ j(greater, &negative);
2952 __ mov(ecx, scratch2);
2953 __ jmp(&done);
2954 __ bind(&negative);
2955 __ sub(ecx, Operand(scratch2));
2956 __ bind(&done);
2957 }
2958}
2959
2960
2961// Input: edx, eax are the left and right objects of a bit op.
2962// Output: eax, ecx are left and right integers for a bit op.
2963void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
2964 TypeInfo type_info,
2965 bool use_sse3,
2966 Label* conversion_failure) {
2967 // Check float operands.
2968 Label arg1_is_object, check_undefined_arg1;
2969 Label arg2_is_object, check_undefined_arg2;
2970 Label load_arg2, done;
2971
2972 if (!type_info.IsDouble()) {
2973 if (!type_info.IsSmi()) {
2974 __ test(edx, Immediate(kSmiTagMask));
2975 __ j(not_zero, &arg1_is_object);
2976 } else {
2977 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
2978 }
2979 __ SmiUntag(edx);
2980 __ jmp(&load_arg2);
2981 }
2982
2983 __ bind(&arg1_is_object);
2984
2985 // Get the untagged integer version of the edx heap number in ecx.
2986 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
2987 __ mov(edx, ecx);
2988
2989 // Here edx has the untagged integer, eax has a Smi or a heap number.
2990 __ bind(&load_arg2);
2991 if (!type_info.IsDouble()) {
2992 // Test if arg2 is a Smi.
2993 if (!type_info.IsSmi()) {
2994 __ test(eax, Immediate(kSmiTagMask));
2995 __ j(not_zero, &arg2_is_object);
2996 } else {
2997 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
2998 }
2999 __ SmiUntag(eax);
3000 __ mov(ecx, eax);
3001 __ jmp(&done);
3002 }
3003
3004 __ bind(&arg2_is_object);
3005
3006 // Get the untagged integer version of the eax heap number in ecx.
3007 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
3008 __ bind(&done);
3009 __ mov(eax, edx);
3010}
3011
3012
3013// Input: edx, eax are the left and right objects of a bit op.
3014// Output: eax, ecx are left and right integers for a bit op.
3015void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
3016 bool use_sse3,
3017 Label* conversion_failure) {
3018 // Check float operands.
3019 Label arg1_is_object, check_undefined_arg1;
3020 Label arg2_is_object, check_undefined_arg2;
3021 Label load_arg2, done;
3022
3023 // Test if arg1 is a Smi.
3024 __ test(edx, Immediate(kSmiTagMask));
3025 __ j(not_zero, &arg1_is_object);
3026
3027 __ SmiUntag(edx);
3028 __ jmp(&load_arg2);
3029
3030 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
3031 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003032 Factory* factory = masm->isolate()->factory();
3033 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003034 __ j(not_equal, conversion_failure);
3035 __ mov(edx, Immediate(0));
3036 __ jmp(&load_arg2);
3037
3038 __ bind(&arg1_is_object);
3039 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003040 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003041 __ j(not_equal, &check_undefined_arg1);
3042
3043 // Get the untagged integer version of the edx heap number in ecx.
3044 IntegerConvert(masm,
3045 edx,
3046 TypeInfo::Unknown(),
3047 use_sse3,
3048 conversion_failure);
3049 __ mov(edx, ecx);
3050
3051 // Here edx has the untagged integer, eax has a Smi or a heap number.
3052 __ bind(&load_arg2);
3053
3054 // Test if arg2 is a Smi.
3055 __ test(eax, Immediate(kSmiTagMask));
3056 __ j(not_zero, &arg2_is_object);
3057
3058 __ SmiUntag(eax);
3059 __ mov(ecx, eax);
3060 __ jmp(&done);
3061
3062 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
3063 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003064 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003065 __ j(not_equal, conversion_failure);
3066 __ mov(ecx, Immediate(0));
3067 __ jmp(&done);
3068
3069 __ bind(&arg2_is_object);
3070 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003071 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003072 __ j(not_equal, &check_undefined_arg2);
3073
3074 // Get the untagged integer version of the eax heap number in ecx.
3075 IntegerConvert(masm,
3076 eax,
3077 TypeInfo::Unknown(),
3078 use_sse3,
3079 conversion_failure);
3080 __ bind(&done);
3081 __ mov(eax, edx);
3082}
3083
3084
3085void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
3086 TypeInfo type_info,
3087 bool use_sse3,
3088 Label* conversion_failure) {
3089 if (type_info.IsNumber()) {
3090 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
3091 } else {
3092 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
3093 }
3094}
3095
3096
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003097void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
3098 bool use_sse3,
3099 Label* not_int32) {
3100 return;
3101}
3102
3103
ricow@chromium.org65fae842010-08-25 15:26:24 +00003104void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
3105 Register number) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003106 NearLabel load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003107
3108 __ test(number, Immediate(kSmiTagMask));
3109 __ j(zero, &load_smi, not_taken);
3110 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
3111 __ jmp(&done);
3112
3113 __ bind(&load_smi);
3114 __ SmiUntag(number);
3115 __ push(number);
3116 __ fild_s(Operand(esp, 0));
3117 __ pop(number);
3118
3119 __ bind(&done);
3120}
3121
3122
3123void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003124 NearLabel load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003125 // Load operand in edx into xmm0.
3126 __ test(edx, Immediate(kSmiTagMask));
3127 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
3128 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3129
3130 __ bind(&load_eax);
3131 // Load operand in eax into xmm1.
3132 __ test(eax, Immediate(kSmiTagMask));
3133 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
3134 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3135 __ jmp(&done);
3136
3137 __ bind(&load_smi_edx);
3138 __ SmiUntag(edx); // Untag smi before converting to float.
3139 __ cvtsi2sd(xmm0, Operand(edx));
3140 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3141 __ jmp(&load_eax);
3142
3143 __ bind(&load_smi_eax);
3144 __ SmiUntag(eax); // Untag smi before converting to float.
3145 __ cvtsi2sd(xmm1, Operand(eax));
3146 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3147
3148 __ bind(&done);
3149}
3150
3151
3152void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
3153 Label* not_numbers) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003154 NearLabel load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003155 // Load operand in edx into xmm0, or branch to not_numbers.
3156 __ test(edx, Immediate(kSmiTagMask));
3157 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003158 Factory* factory = masm->isolate()->factory();
3159 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003160 __ j(not_equal, not_numbers); // Argument in edx is not a number.
3161 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3162 __ bind(&load_eax);
3163 // Load operand in eax into xmm1, or branch to not_numbers.
3164 __ test(eax, Immediate(kSmiTagMask));
3165 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003166 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003167 __ j(equal, &load_float_eax);
3168 __ jmp(not_numbers); // Argument in eax is not a number.
3169 __ bind(&load_smi_edx);
3170 __ SmiUntag(edx); // Untag smi before converting to float.
3171 __ cvtsi2sd(xmm0, Operand(edx));
3172 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3173 __ jmp(&load_eax);
3174 __ bind(&load_smi_eax);
3175 __ SmiUntag(eax); // Untag smi before converting to float.
3176 __ cvtsi2sd(xmm1, Operand(eax));
3177 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3178 __ jmp(&done);
3179 __ bind(&load_float_eax);
3180 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3181 __ bind(&done);
3182}
3183
3184
3185void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
3186 Register scratch) {
3187 const Register left = edx;
3188 const Register right = eax;
3189 __ mov(scratch, left);
3190 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3191 __ SmiUntag(scratch);
3192 __ cvtsi2sd(xmm0, Operand(scratch));
3193
3194 __ mov(scratch, right);
3195 __ SmiUntag(scratch);
3196 __ cvtsi2sd(xmm1, Operand(scratch));
3197}
3198
3199
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003200void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
3201 Label* non_int32,
3202 Register scratch) {
3203 __ cvttsd2si(scratch, Operand(xmm0));
3204 __ cvtsi2sd(xmm2, Operand(scratch));
3205 __ ucomisd(xmm0, xmm2);
3206 __ j(not_zero, non_int32);
3207 __ j(carry, non_int32);
3208 __ cvttsd2si(scratch, Operand(xmm1));
3209 __ cvtsi2sd(xmm2, Operand(scratch));
3210 __ ucomisd(xmm1, xmm2);
3211 __ j(not_zero, non_int32);
3212 __ j(carry, non_int32);
3213}
3214
3215
ricow@chromium.org65fae842010-08-25 15:26:24 +00003216void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
3217 Register scratch,
3218 ArgLocation arg_location) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003219 NearLabel load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003220 if (arg_location == ARGS_IN_REGISTERS) {
3221 __ mov(scratch, edx);
3222 } else {
3223 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3224 }
3225 __ test(scratch, Immediate(kSmiTagMask));
3226 __ j(zero, &load_smi_1, not_taken);
3227 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3228 __ bind(&done_load_1);
3229
3230 if (arg_location == ARGS_IN_REGISTERS) {
3231 __ mov(scratch, eax);
3232 } else {
3233 __ mov(scratch, Operand(esp, 1 * kPointerSize));
3234 }
3235 __ test(scratch, Immediate(kSmiTagMask));
3236 __ j(zero, &load_smi_2, not_taken);
3237 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3238 __ jmp(&done);
3239
3240 __ bind(&load_smi_1);
3241 __ SmiUntag(scratch);
3242 __ push(scratch);
3243 __ fild_s(Operand(esp, 0));
3244 __ pop(scratch);
3245 __ jmp(&done_load_1);
3246
3247 __ bind(&load_smi_2);
3248 __ SmiUntag(scratch);
3249 __ push(scratch);
3250 __ fild_s(Operand(esp, 0));
3251 __ pop(scratch);
3252
3253 __ bind(&done);
3254}
3255
3256
3257void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
3258 Register scratch) {
3259 const Register left = edx;
3260 const Register right = eax;
3261 __ mov(scratch, left);
3262 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3263 __ SmiUntag(scratch);
3264 __ push(scratch);
3265 __ fild_s(Operand(esp, 0));
3266
3267 __ mov(scratch, right);
3268 __ SmiUntag(scratch);
3269 __ mov(Operand(esp, 0), scratch);
3270 __ fild_s(Operand(esp, 0));
3271 __ pop(scratch);
3272}
3273
3274
3275void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
3276 Label* non_float,
3277 Register scratch) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003278 NearLabel test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003279 // Test if both operands are floats or smi -> scratch=k_is_float;
3280 // Otherwise scratch = k_not_float.
3281 __ test(edx, Immediate(kSmiTagMask));
3282 __ j(zero, &test_other, not_taken); // argument in edx is OK
3283 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003284 Factory* factory = masm->isolate()->factory();
3285 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003286 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
3287
3288 __ bind(&test_other);
3289 __ test(eax, Immediate(kSmiTagMask));
3290 __ j(zero, &done); // argument in eax is OK
3291 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003292 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003293 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
3294
3295 // Fall-through: Both operands are numbers.
3296 __ bind(&done);
3297}
3298
3299
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003300void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
3301 Label* non_int32) {
3302 return;
3303}
3304
3305
ricow@chromium.org65fae842010-08-25 15:26:24 +00003306void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003307 Label slow, done, undo;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003308
3309 if (op_ == Token::SUB) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003310 if (include_smi_code_) {
3311 // Check whether the value is a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003312 NearLabel try_float;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003313 __ test(eax, Immediate(kSmiTagMask));
3314 __ j(not_zero, &try_float, not_taken);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003315
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003316 if (negative_zero_ == kStrictNegativeZero) {
3317 // Go slow case if the value of the expression is zero
3318 // to make sure that we switch between 0 and -0.
3319 __ test(eax, Operand(eax));
3320 __ j(zero, &slow, not_taken);
3321 }
3322
3323 // The value of the expression is a smi that is not zero. Try
3324 // optimistic subtraction '0 - value'.
3325 __ mov(edx, Operand(eax));
3326 __ Set(eax, Immediate(0));
3327 __ sub(eax, Operand(edx));
3328 __ j(overflow, &undo, not_taken);
3329 __ StubReturn(1);
3330
3331 // Try floating point case.
3332 __ bind(&try_float);
3333 } else if (FLAG_debug_code) {
3334 __ AbortIfSmi(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003335 }
3336
ricow@chromium.org65fae842010-08-25 15:26:24 +00003337 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003338 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003339 __ j(not_equal, &slow);
3340 if (overwrite_ == UNARY_OVERWRITE) {
3341 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
3342 __ xor_(edx, HeapNumber::kSignMask); // Flip sign.
3343 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), edx);
3344 } else {
3345 __ mov(edx, Operand(eax));
3346 // edx: operand
3347 __ AllocateHeapNumber(eax, ebx, ecx, &undo);
3348 // eax: allocated 'empty' number
3349 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3350 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
3351 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
3352 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
3353 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
3354 }
3355 } else if (op_ == Token::BIT_NOT) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003356 if (include_smi_code_) {
3357 Label non_smi;
3358 __ test(eax, Immediate(kSmiTagMask));
3359 __ j(not_zero, &non_smi);
3360 __ not_(eax);
3361 __ and_(eax, ~kSmiTagMask); // Remove inverted smi-tag.
3362 __ ret(0);
3363 __ bind(&non_smi);
3364 } else if (FLAG_debug_code) {
3365 __ AbortIfSmi(eax);
3366 }
3367
ricow@chromium.org65fae842010-08-25 15:26:24 +00003368 // Check if the operand is a heap number.
3369 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003370 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003371 __ j(not_equal, &slow, not_taken);
3372
3373 // Convert the heap number in eax to an untagged integer in ecx.
3374 IntegerConvert(masm,
3375 eax,
3376 TypeInfo::Unknown(),
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003377 CpuFeatures::IsSupported(SSE3),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003378 &slow);
3379
3380 // Do the bitwise operation and check if the result fits in a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003381 NearLabel try_float;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003382 __ not_(ecx);
3383 __ cmp(ecx, 0xc0000000);
3384 __ j(sign, &try_float, not_taken);
3385
3386 // Tag the result as a smi and we're done.
3387 STATIC_ASSERT(kSmiTagSize == 1);
3388 __ lea(eax, Operand(ecx, times_2, kSmiTag));
3389 __ jmp(&done);
3390
3391 // Try to store the result in a heap number.
3392 __ bind(&try_float);
3393 if (overwrite_ == UNARY_NO_OVERWRITE) {
3394 // Allocate a fresh heap number, but don't overwrite eax until
3395 // we're sure we can do it without going through the slow case
3396 // that needs the value in eax.
3397 __ AllocateHeapNumber(ebx, edx, edi, &slow);
3398 __ mov(eax, Operand(ebx));
3399 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003400 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003401 CpuFeatures::Scope use_sse2(SSE2);
3402 __ cvtsi2sd(xmm0, Operand(ecx));
3403 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
3404 } else {
3405 __ push(ecx);
3406 __ fild_s(Operand(esp, 0));
3407 __ pop(ecx);
3408 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
3409 }
3410 } else {
3411 UNIMPLEMENTED();
3412 }
3413
3414 // Return from the stub.
3415 __ bind(&done);
3416 __ StubReturn(1);
3417
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003418 // Restore eax and go slow case.
3419 __ bind(&undo);
3420 __ mov(eax, Operand(edx));
3421
ricow@chromium.org65fae842010-08-25 15:26:24 +00003422 // Handle the slow case by jumping to the JavaScript builtin.
3423 __ bind(&slow);
3424 __ pop(ecx); // pop return address.
3425 __ push(eax);
3426 __ push(ecx); // push return address
3427 switch (op_) {
3428 case Token::SUB:
3429 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
3430 break;
3431 case Token::BIT_NOT:
3432 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
3433 break;
3434 default:
3435 UNREACHABLE();
3436 }
3437}
3438
3439
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003440void MathPowStub::Generate(MacroAssembler* masm) {
3441 // Registers are used as follows:
3442 // edx = base
3443 // eax = exponent
3444 // ecx = temporary, result
3445
3446 CpuFeatures::Scope use_sse2(SSE2);
3447 Label allocate_return, call_runtime;
3448
3449 // Load input parameters.
3450 __ mov(edx, Operand(esp, 2 * kPointerSize));
3451 __ mov(eax, Operand(esp, 1 * kPointerSize));
3452
3453 // Save 1 in xmm3 - we need this several times later on.
3454 __ mov(ecx, Immediate(1));
3455 __ cvtsi2sd(xmm3, Operand(ecx));
3456
3457 Label exponent_nonsmi;
3458 Label base_nonsmi;
3459 // If the exponent is a heap number go to that specific case.
3460 __ test(eax, Immediate(kSmiTagMask));
3461 __ j(not_zero, &exponent_nonsmi);
3462 __ test(edx, Immediate(kSmiTagMask));
3463 __ j(not_zero, &base_nonsmi);
3464
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003465 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003466 Label powi;
3467 __ SmiUntag(edx);
3468 __ cvtsi2sd(xmm0, Operand(edx));
3469 __ jmp(&powi);
3470 // exponent is smi and base is a heapnumber.
3471 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003472 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003473 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003474 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003475 __ j(not_equal, &call_runtime);
3476
3477 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3478
3479 // Optimized version of pow if exponent is a smi.
3480 // xmm0 contains the base.
3481 __ bind(&powi);
3482 __ SmiUntag(eax);
3483
3484 // Save exponent in base as we need to check if exponent is negative later.
3485 // We know that base and exponent are in different registers.
3486 __ mov(edx, eax);
3487
3488 // Get absolute value of exponent.
3489 NearLabel no_neg;
3490 __ cmp(eax, 0);
3491 __ j(greater_equal, &no_neg);
3492 __ neg(eax);
3493 __ bind(&no_neg);
3494
3495 // Load xmm1 with 1.
3496 __ movsd(xmm1, xmm3);
3497 NearLabel while_true;
3498 NearLabel no_multiply;
3499
3500 __ bind(&while_true);
3501 __ shr(eax, 1);
3502 __ j(not_carry, &no_multiply);
3503 __ mulsd(xmm1, xmm0);
3504 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003505 __ mulsd(xmm0, xmm0);
3506 __ j(not_zero, &while_true);
3507
3508 // base has the original value of the exponent - if the exponent is
3509 // negative return 1/result.
3510 __ test(edx, Operand(edx));
3511 __ j(positive, &allocate_return);
3512 // Special case if xmm1 has reached infinity.
3513 __ mov(ecx, Immediate(0x7FB00000));
3514 __ movd(xmm0, Operand(ecx));
3515 __ cvtss2sd(xmm0, xmm0);
3516 __ ucomisd(xmm0, xmm1);
3517 __ j(equal, &call_runtime);
3518 __ divsd(xmm3, xmm1);
3519 __ movsd(xmm1, xmm3);
3520 __ jmp(&allocate_return);
3521
3522 // exponent (or both) is a heapnumber - no matter what we should now work
3523 // on doubles.
3524 __ bind(&exponent_nonsmi);
3525 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003526 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003527 __ j(not_equal, &call_runtime);
3528 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3529 // Test if exponent is nan.
3530 __ ucomisd(xmm1, xmm1);
3531 __ j(parity_even, &call_runtime);
3532
3533 NearLabel base_not_smi;
3534 NearLabel handle_special_cases;
3535 __ test(edx, Immediate(kSmiTagMask));
3536 __ j(not_zero, &base_not_smi);
3537 __ SmiUntag(edx);
3538 __ cvtsi2sd(xmm0, Operand(edx));
3539 __ jmp(&handle_special_cases);
3540
3541 __ bind(&base_not_smi);
3542 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003543 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003544 __ j(not_equal, &call_runtime);
3545 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3546 __ and_(ecx, HeapNumber::kExponentMask);
3547 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
3548 // base is NaN or +/-Infinity
3549 __ j(greater_equal, &call_runtime);
3550 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3551
3552 // base is in xmm0 and exponent is in xmm1.
3553 __ bind(&handle_special_cases);
3554 NearLabel not_minus_half;
3555 // Test for -0.5.
3556 // Load xmm2 with -0.5.
3557 __ mov(ecx, Immediate(0xBF000000));
3558 __ movd(xmm2, Operand(ecx));
3559 __ cvtss2sd(xmm2, xmm2);
3560 // xmm2 now has -0.5.
3561 __ ucomisd(xmm2, xmm1);
3562 __ j(not_equal, &not_minus_half);
3563
3564 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003565 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3566 __ xorpd(xmm1, xmm1);
3567 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003568 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003569 __ divsd(xmm3, xmm1);
3570 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003571 __ jmp(&allocate_return);
3572
3573 // Test for 0.5.
3574 __ bind(&not_minus_half);
3575 // Load xmm2 with 0.5.
3576 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
3577 __ addsd(xmm2, xmm3);
3578 // xmm2 now has 0.5.
3579 __ ucomisd(xmm2, xmm1);
3580 __ j(not_equal, &call_runtime);
3581 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003582 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3583 __ xorpd(xmm1, xmm1);
3584 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003585 __ sqrtsd(xmm1, xmm1);
3586
3587 __ bind(&allocate_return);
3588 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
3589 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
3590 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003591 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003592
3593 __ bind(&call_runtime);
3594 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3595}
3596
3597
ricow@chromium.org65fae842010-08-25 15:26:24 +00003598void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3599 // The key is in edx and the parameter count is in eax.
3600
3601 // The displacement is used for skipping the frame pointer on the
3602 // stack. It is the offset of the last parameter (if any) relative
3603 // to the frame pointer.
3604 static const int kDisplacement = 1 * kPointerSize;
3605
3606 // Check that the key is a smi.
3607 Label slow;
3608 __ test(edx, Immediate(kSmiTagMask));
3609 __ j(not_zero, &slow, not_taken);
3610
3611 // Check if the calling frame is an arguments adaptor frame.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003612 NearLabel adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003613 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3614 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
3615 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3616 __ j(equal, &adaptor);
3617
3618 // Check index against formal parameters count limit passed in
3619 // through register eax. Use unsigned comparison to get negative
3620 // check for free.
3621 __ cmp(edx, Operand(eax));
3622 __ j(above_equal, &slow, not_taken);
3623
3624 // Read the argument from the stack and return it.
3625 STATIC_ASSERT(kSmiTagSize == 1);
3626 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3627 __ lea(ebx, Operand(ebp, eax, times_2, 0));
3628 __ neg(edx);
3629 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3630 __ ret(0);
3631
3632 // Arguments adaptor case: Check index against actual arguments
3633 // limit found in the arguments adaptor frame. Use unsigned
3634 // comparison to get negative check for free.
3635 __ bind(&adaptor);
3636 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3637 __ cmp(edx, Operand(ecx));
3638 __ j(above_equal, &slow, not_taken);
3639
3640 // Read the argument from the stack and return it.
3641 STATIC_ASSERT(kSmiTagSize == 1);
3642 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3643 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
3644 __ neg(edx);
3645 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3646 __ ret(0);
3647
3648 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3649 // by calling the runtime system.
3650 __ bind(&slow);
3651 __ pop(ebx); // Return address.
3652 __ push(edx);
3653 __ push(ebx);
3654 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3655}
3656
3657
3658void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3659 // esp[0] : return address
3660 // esp[4] : number of parameters
3661 // esp[8] : receiver displacement
3662 // esp[16] : function
3663
3664 // The displacement is used for skipping the return address and the
3665 // frame pointer on the stack. It is the offset of the last
3666 // parameter (if any) relative to the frame pointer.
3667 static const int kDisplacement = 2 * kPointerSize;
3668
3669 // Check if the calling frame is an arguments adaptor frame.
3670 Label adaptor_frame, try_allocate, runtime;
3671 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3672 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
3673 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3674 __ j(equal, &adaptor_frame);
3675
3676 // Get the length from the frame.
3677 __ mov(ecx, Operand(esp, 1 * kPointerSize));
3678 __ jmp(&try_allocate);
3679
3680 // Patch the arguments.length and the parameters pointer.
3681 __ bind(&adaptor_frame);
3682 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3683 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3684 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
3685 __ mov(Operand(esp, 2 * kPointerSize), edx);
3686
3687 // Try the new space allocation. Start out with computing the size of
3688 // the arguments object and the elements array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003689 NearLabel add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003690 __ bind(&try_allocate);
3691 __ test(ecx, Operand(ecx));
3692 __ j(zero, &add_arguments_object);
3693 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3694 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003695 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003696
3697 // Do the allocation of both objects in one go.
3698 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3699
3700 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003701 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3702 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003703 __ mov(edi, Operand(edi,
3704 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003705
3706 // Copy the JS object part.
3707 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3708 __ mov(ebx, FieldOperand(edi, i));
3709 __ mov(FieldOperand(eax, i), ebx);
3710 }
3711
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003712 if (type_ == NEW_NON_STRICT) {
3713 // Setup the callee in-object property.
3714 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3715 __ mov(ebx, Operand(esp, 3 * kPointerSize));
3716 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3717 Heap::kArgumentsCalleeIndex * kPointerSize),
3718 ebx);
3719 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003720
3721 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003722 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003723 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003724 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3725 Heap::kArgumentsLengthIndex * kPointerSize),
3726 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003727
3728 // If there are no actual arguments, we're done.
3729 Label done;
3730 __ test(ecx, Operand(ecx));
3731 __ j(zero, &done);
3732
3733 // Get the parameters pointer from the stack.
3734 __ mov(edx, Operand(esp, 2 * kPointerSize));
3735
3736 // Setup the elements pointer in the allocated arguments object and
3737 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003738 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003739 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3740 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003741 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003742
ricow@chromium.org65fae842010-08-25 15:26:24 +00003743 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3744 // Untag the length for the loop below.
3745 __ SmiUntag(ecx);
3746
3747 // Copy the fixed array slots.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003748 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003749 __ bind(&loop);
3750 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3751 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3752 __ add(Operand(edi), Immediate(kPointerSize));
3753 __ sub(Operand(edx), Immediate(kPointerSize));
3754 __ dec(ecx);
3755 __ j(not_zero, &loop);
3756
3757 // Return and remove the on-stack parameters.
3758 __ bind(&done);
3759 __ ret(3 * kPointerSize);
3760
3761 // Do the runtime call to allocate the arguments object.
3762 __ bind(&runtime);
3763 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3764}
3765
3766
3767void RegExpExecStub::Generate(MacroAssembler* masm) {
3768 // Just jump directly to runtime if native RegExp is not selected at compile
3769 // time or if regexp entry in generated code is turned off runtime switch or
3770 // at compilation.
3771#ifdef V8_INTERPRETED_REGEXP
3772 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3773#else // V8_INTERPRETED_REGEXP
3774 if (!FLAG_regexp_entry_native) {
3775 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3776 return;
3777 }
3778
3779 // Stack frame on entry.
3780 // esp[0]: return address
3781 // esp[4]: last_match_info (expected JSArray)
3782 // esp[8]: previous index
3783 // esp[12]: subject string
3784 // esp[16]: JSRegExp object
3785
3786 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3787 static const int kPreviousIndexOffset = 2 * kPointerSize;
3788 static const int kSubjectOffset = 3 * kPointerSize;
3789 static const int kJSRegExpOffset = 4 * kPointerSize;
3790
3791 Label runtime, invoke_regexp;
3792
3793 // Ensure that a RegExp stack is allocated.
3794 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003795 ExternalReference::address_of_regexp_stack_memory_address(
3796 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003797 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003798 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003799 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3800 __ test(ebx, Operand(ebx));
3801 __ j(zero, &runtime, not_taken);
3802
3803 // Check that the first argument is a JSRegExp object.
3804 __ mov(eax, Operand(esp, kJSRegExpOffset));
3805 STATIC_ASSERT(kSmiTag == 0);
3806 __ test(eax, Immediate(kSmiTagMask));
3807 __ j(zero, &runtime);
3808 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3809 __ j(not_equal, &runtime);
3810 // Check that the RegExp has been compiled (data contains a fixed array).
3811 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3812 if (FLAG_debug_code) {
3813 __ test(ecx, Immediate(kSmiTagMask));
3814 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3815 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3816 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3817 }
3818
3819 // ecx: RegExp data (FixedArray)
3820 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3821 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3822 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3823 __ j(not_equal, &runtime);
3824
3825 // ecx: RegExp data (FixedArray)
3826 // Check that the number of captures fit in the static offsets vector buffer.
3827 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3828 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3829 // uses the asumption that smis are 2 * their untagged value.
3830 STATIC_ASSERT(kSmiTag == 0);
3831 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3832 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3833 // Check that the static offsets vector buffer is large enough.
3834 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3835 __ j(above, &runtime);
3836
3837 // ecx: RegExp data (FixedArray)
3838 // edx: Number of capture registers
3839 // Check that the second argument is a string.
3840 __ mov(eax, Operand(esp, kSubjectOffset));
3841 __ test(eax, Immediate(kSmiTagMask));
3842 __ j(zero, &runtime);
3843 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3844 __ j(NegateCondition(is_string), &runtime);
3845 // Get the length of the string to ebx.
3846 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3847
3848 // ebx: Length of subject string as a smi
3849 // ecx: RegExp data (FixedArray)
3850 // edx: Number of capture registers
3851 // Check that the third argument is a positive smi less than the subject
3852 // string length. A negative value will be greater (unsigned comparison).
3853 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3854 __ test(eax, Immediate(kSmiTagMask));
3855 __ j(not_zero, &runtime);
3856 __ cmp(eax, Operand(ebx));
3857 __ j(above_equal, &runtime);
3858
3859 // ecx: RegExp data (FixedArray)
3860 // edx: Number of capture registers
3861 // Check that the fourth object is a JSArray object.
3862 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3863 __ test(eax, Immediate(kSmiTagMask));
3864 __ j(zero, &runtime);
3865 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3866 __ j(not_equal, &runtime);
3867 // Check that the JSArray is in fast case.
3868 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3869 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003870 Factory* factory = masm->isolate()->factory();
3871 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003872 __ j(not_equal, &runtime);
3873 // Check that the last match info has space for the capture registers and the
3874 // additional information.
3875 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3876 __ SmiUntag(eax);
3877 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3878 __ cmp(edx, Operand(eax));
3879 __ j(greater, &runtime);
3880
3881 // ecx: RegExp data (FixedArray)
3882 // Check the representation and encoding of the subject string.
3883 Label seq_ascii_string, seq_two_byte_string, check_code;
3884 __ mov(eax, Operand(esp, kSubjectOffset));
3885 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3886 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3887 // First check for flat two byte string.
3888 __ and_(ebx,
3889 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3890 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3891 __ j(zero, &seq_two_byte_string);
3892 // Any other flat string must be a flat ascii string.
3893 __ test(Operand(ebx),
3894 Immediate(kIsNotStringMask | kStringRepresentationMask));
3895 __ j(zero, &seq_ascii_string);
3896
3897 // Check for flat cons string.
3898 // A flat cons string is a cons string where the second part is the empty
3899 // string. In that case the subject string is just the first part of the cons
3900 // string. Also in this case the first part of the cons string is known to be
3901 // a sequential string or an external string.
3902 STATIC_ASSERT(kExternalStringTag != 0);
3903 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3904 __ test(Operand(ebx),
3905 Immediate(kIsNotStringMask | kExternalStringTag));
3906 __ j(not_zero, &runtime);
3907 // String is a cons string.
3908 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003909 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003910 __ j(not_equal, &runtime);
3911 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3912 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3913 // String is a cons string with empty second part.
3914 // eax: first part of cons string.
3915 // ebx: map of first part of cons string.
3916 // Is first part a flat two byte string?
3917 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3918 kStringRepresentationMask | kStringEncodingMask);
3919 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3920 __ j(zero, &seq_two_byte_string);
3921 // Any other flat string must be ascii.
3922 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3923 kStringRepresentationMask);
3924 __ j(not_zero, &runtime);
3925
3926 __ bind(&seq_ascii_string);
3927 // eax: subject string (flat ascii)
3928 // ecx: RegExp data (FixedArray)
3929 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3930 __ Set(edi, Immediate(1)); // Type is ascii.
3931 __ jmp(&check_code);
3932
3933 __ bind(&seq_two_byte_string);
3934 // eax: subject string (flat two byte)
3935 // ecx: RegExp data (FixedArray)
3936 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3937 __ Set(edi, Immediate(0)); // Type is two byte.
3938
3939 __ bind(&check_code);
3940 // Check that the irregexp code has been generated for the actual string
3941 // encoding. If it has, the field contains a code object otherwise it contains
3942 // the hole.
3943 __ CmpObjectType(edx, CODE_TYPE, ebx);
3944 __ j(not_equal, &runtime);
3945
3946 // eax: subject string
3947 // edx: code
3948 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3949 // Load used arguments before starting to push arguments for call to native
3950 // RegExp code to avoid handling changing stack height.
3951 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3952 __ SmiUntag(ebx); // Previous index from smi.
3953
3954 // eax: subject string
3955 // ebx: previous index
3956 // edx: code
3957 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3958 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003959 Counters* counters = masm->isolate()->counters();
3960 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003961
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003962 // Isolates: note we add an additional parameter here (isolate pointer).
3963 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003964 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003965
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003966 // Argument 8: Pass current isolate address.
3967 __ mov(Operand(esp, 7 * kPointerSize),
3968 Immediate(ExternalReference::isolate_address()));
3969
ricow@chromium.org65fae842010-08-25 15:26:24 +00003970 // Argument 7: Indicate that this is a direct call from JavaScript.
3971 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3972
3973 // Argument 6: Start (high end) of backtracking stack memory area.
3974 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3975 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3976 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3977
3978 // Argument 5: static offsets vector buffer.
3979 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003980 Immediate(ExternalReference::address_of_static_offsets_vector(
3981 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003982
3983 // Argument 4: End of string data
3984 // Argument 3: Start of string data
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003985 NearLabel setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003986 __ test(edi, Operand(edi));
3987 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
3988 __ j(zero, &setup_two_byte);
3989 __ SmiUntag(edi);
3990 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3991 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3992 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3993 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3994 __ jmp(&setup_rest);
3995
3996 __ bind(&setup_two_byte);
3997 STATIC_ASSERT(kSmiTag == 0);
3998 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3999 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
4000 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
4001 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
4002 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
4003
4004 __ bind(&setup_rest);
4005
4006 // Argument 2: Previous index.
4007 __ mov(Operand(esp, 1 * kPointerSize), ebx);
4008
4009 // Argument 1: Subject string.
4010 __ mov(Operand(esp, 0 * kPointerSize), eax);
4011
4012 // Locate the code entry and call it.
4013 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004014 __ call(Operand(edx));
4015
4016 // Drop arguments and come back to JS mode.
4017 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004018
4019 // Check the result.
4020 Label success;
4021 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
4022 __ j(equal, &success, taken);
4023 Label failure;
4024 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
4025 __ j(equal, &failure, taken);
4026 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
4027 // If not exception it can only be retry. Handle that in the runtime system.
4028 __ j(not_equal, &runtime);
4029 // Result must now be exception. If there is no pending exception already a
4030 // stack overflow (on the backtrack stack) was detected in RegExp code but
4031 // haven't created the exception yet. Handle that in the runtime system.
4032 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004033 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4034 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004035 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004036 Operand::StaticVariable(ExternalReference::the_hole_value_location(
4037 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004038 __ mov(eax, Operand::StaticVariable(pending_exception));
4039 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004040 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004041 // For exception, throw the exception again.
4042
4043 // Clear the pending exception variable.
4044 __ mov(Operand::StaticVariable(pending_exception), edx);
4045
4046 // Special handling of termination exceptions which are uncatchable
4047 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004048 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004049 Label throw_termination_exception;
4050 __ j(equal, &throw_termination_exception);
4051
4052 // Handle normal exception by following handler chain.
4053 __ Throw(eax);
4054
4055 __ bind(&throw_termination_exception);
4056 __ ThrowUncatchable(TERMINATION, eax);
4057
ricow@chromium.org65fae842010-08-25 15:26:24 +00004058 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004059 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004060 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004061 __ ret(4 * kPointerSize);
4062
4063 // Load RegExp data.
4064 __ bind(&success);
4065 __ mov(eax, Operand(esp, kJSRegExpOffset));
4066 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
4067 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
4068 // Calculate number of capture registers (number_of_captures + 1) * 2.
4069 STATIC_ASSERT(kSmiTag == 0);
4070 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4071 __ add(Operand(edx), Immediate(2)); // edx was a smi.
4072
4073 // edx: Number of capture registers
4074 // Load last_match_info which is still known to be a fast case JSArray.
4075 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4076 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
4077
4078 // ebx: last_match_info backing store (FixedArray)
4079 // edx: number of capture registers
4080 // Store the capture count.
4081 __ SmiTag(edx); // Number of capture registers to smi.
4082 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
4083 __ SmiUntag(edx); // Number of capture registers back from smi.
4084 // Store last subject and last input.
4085 __ mov(eax, Operand(esp, kSubjectOffset));
4086 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
4087 __ mov(ecx, ebx);
4088 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
4089 __ mov(eax, Operand(esp, kSubjectOffset));
4090 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
4091 __ mov(ecx, ebx);
4092 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
4093
4094 // Get the static offsets vector filled by the native regexp code.
4095 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004096 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004097 __ mov(ecx, Immediate(address_of_static_offsets_vector));
4098
4099 // ebx: last_match_info backing store (FixedArray)
4100 // ecx: offsets vector
4101 // edx: number of capture registers
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004102 NearLabel next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004103 // Capture register counter starts from number of capture registers and
4104 // counts down until wraping after zero.
4105 __ bind(&next_capture);
4106 __ sub(Operand(edx), Immediate(1));
4107 __ j(negative, &done);
4108 // Read the value from the static offsets vector buffer.
4109 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
4110 __ SmiTag(edi);
4111 // Store the smi value in the last match info.
4112 __ mov(FieldOperand(ebx,
4113 edx,
4114 times_pointer_size,
4115 RegExpImpl::kFirstCaptureOffset),
4116 edi);
4117 __ jmp(&next_capture);
4118 __ bind(&done);
4119
4120 // Return last match info.
4121 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4122 __ ret(4 * kPointerSize);
4123
4124 // Do the runtime call to execute the regexp.
4125 __ bind(&runtime);
4126 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4127#endif // V8_INTERPRETED_REGEXP
4128}
4129
4130
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004131void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4132 const int kMaxInlineLength = 100;
4133 Label slowcase;
4134 NearLabel done;
4135 __ mov(ebx, Operand(esp, kPointerSize * 3));
4136 __ test(ebx, Immediate(kSmiTagMask));
4137 __ j(not_zero, &slowcase);
4138 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
4139 __ j(above, &slowcase);
4140 // Smi-tagging is equivalent to multiplying by 2.
4141 STATIC_ASSERT(kSmiTag == 0);
4142 STATIC_ASSERT(kSmiTagSize == 1);
4143 // Allocate RegExpResult followed by FixedArray with size in ebx.
4144 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4145 // Elements: [Map][Length][..elements..]
4146 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
4147 times_half_pointer_size,
4148 ebx, // In: Number of elements (times 2, being a smi)
4149 eax, // Out: Start of allocation (tagged).
4150 ecx, // Out: End of allocation.
4151 edx, // Scratch register
4152 &slowcase,
4153 TAG_OBJECT);
4154 // eax: Start of allocated area, object-tagged.
4155
4156 // Set JSArray map to global.regexp_result_map().
4157 // Set empty properties FixedArray.
4158 // Set elements to point to FixedArray allocated right after the JSArray.
4159 // Interleave operations for better latency.
4160 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004161 Factory* factory = masm->isolate()->factory();
4162 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004163 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
4164 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
4165 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
4166 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
4167 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
4168 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
4169
4170 // Set input, index and length fields from arguments.
4171 __ mov(ecx, Operand(esp, kPointerSize * 1));
4172 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
4173 __ mov(ecx, Operand(esp, kPointerSize * 2));
4174 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
4175 __ mov(ecx, Operand(esp, kPointerSize * 3));
4176 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
4177
4178 // Fill out the elements FixedArray.
4179 // eax: JSArray.
4180 // ebx: FixedArray.
4181 // ecx: Number of elements in array, as smi.
4182
4183 // Set map.
4184 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004185 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004186 // Set length.
4187 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
4188 // Fill contents of fixed-array with the-hole.
4189 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004190 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004191 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
4192 // Fill fixed array elements with hole.
4193 // eax: JSArray.
4194 // ecx: Number of elements to fill.
4195 // ebx: Start of elements in FixedArray.
4196 // edx: the hole.
4197 Label loop;
4198 __ test(ecx, Operand(ecx));
4199 __ bind(&loop);
4200 __ j(less_equal, &done); // Jump if ecx is negative or zero.
4201 __ sub(Operand(ecx), Immediate(1));
4202 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
4203 __ jmp(&loop);
4204
4205 __ bind(&done);
4206 __ ret(3 * kPointerSize);
4207
4208 __ bind(&slowcase);
4209 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4210}
4211
4212
ricow@chromium.org65fae842010-08-25 15:26:24 +00004213void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
4214 Register object,
4215 Register result,
4216 Register scratch1,
4217 Register scratch2,
4218 bool object_is_smi,
4219 Label* not_found) {
4220 // Use of registers. Register result is used as a temporary.
4221 Register number_string_cache = result;
4222 Register mask = scratch1;
4223 Register scratch = scratch2;
4224
4225 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004226 ExternalReference roots_address =
4227 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004228 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
4229 __ mov(number_string_cache,
4230 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4231 // Make the hash mask from the length of the number string cache. It
4232 // contains two elements (number and string) for each cache entry.
4233 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
4234 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
4235 __ sub(Operand(mask), Immediate(1)); // Make mask.
4236
4237 // Calculate the entry in the number string cache. The hash value in the
4238 // number string cache for smis is just the smi value, and the hash for
4239 // doubles is the xor of the upper and lower words. See
4240 // Heap::GetNumberStringCache.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004241 NearLabel smi_hash_calculated;
4242 NearLabel load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004243 if (object_is_smi) {
4244 __ mov(scratch, object);
4245 __ SmiUntag(scratch);
4246 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004247 NearLabel not_smi, hash_calculated;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004248 STATIC_ASSERT(kSmiTag == 0);
4249 __ test(object, Immediate(kSmiTagMask));
4250 __ j(not_zero, &not_smi);
4251 __ mov(scratch, object);
4252 __ SmiUntag(scratch);
4253 __ jmp(&smi_hash_calculated);
4254 __ bind(&not_smi);
4255 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004256 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004257 __ j(not_equal, not_found);
4258 STATIC_ASSERT(8 == kDoubleSize);
4259 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
4260 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
4261 // Object is heap number and hash is now in scratch. Calculate cache index.
4262 __ and_(scratch, Operand(mask));
4263 Register index = scratch;
4264 Register probe = mask;
4265 __ mov(probe,
4266 FieldOperand(number_string_cache,
4267 index,
4268 times_twice_pointer_size,
4269 FixedArray::kHeaderSize));
4270 __ test(probe, Immediate(kSmiTagMask));
4271 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004272 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004273 CpuFeatures::Scope fscope(SSE2);
4274 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
4275 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
4276 __ ucomisd(xmm0, xmm1);
4277 } else {
4278 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
4279 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
4280 __ FCmp();
4281 }
4282 __ j(parity_even, not_found); // Bail out if NaN is involved.
4283 __ j(not_equal, not_found); // The cache did not contain this value.
4284 __ jmp(&load_result_from_cache);
4285 }
4286
4287 __ bind(&smi_hash_calculated);
4288 // Object is smi and hash is now in scratch. Calculate cache index.
4289 __ and_(scratch, Operand(mask));
4290 Register index = scratch;
4291 // Check if the entry is the smi we are looking for.
4292 __ cmp(object,
4293 FieldOperand(number_string_cache,
4294 index,
4295 times_twice_pointer_size,
4296 FixedArray::kHeaderSize));
4297 __ j(not_equal, not_found);
4298
4299 // Get the result from the cache.
4300 __ bind(&load_result_from_cache);
4301 __ mov(result,
4302 FieldOperand(number_string_cache,
4303 index,
4304 times_twice_pointer_size,
4305 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004306 Counters* counters = masm->isolate()->counters();
4307 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004308}
4309
4310
4311void NumberToStringStub::Generate(MacroAssembler* masm) {
4312 Label runtime;
4313
4314 __ mov(ebx, Operand(esp, kPointerSize));
4315
4316 // Generate code to lookup number in the number string cache.
4317 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
4318 __ ret(1 * kPointerSize);
4319
4320 __ bind(&runtime);
4321 // Handle number to string in the runtime system if not found in the cache.
4322 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
4323}
4324
4325
4326static int NegativeComparisonResult(Condition cc) {
4327 ASSERT(cc != equal);
4328 ASSERT((cc == less) || (cc == less_equal)
4329 || (cc == greater) || (cc == greater_equal));
4330 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
4331}
4332
4333void CompareStub::Generate(MacroAssembler* masm) {
4334 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4335
4336 Label check_unequal_objects, done;
4337
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004338 // Compare two smis if required.
4339 if (include_smi_compare_) {
4340 Label non_smi, smi_done;
4341 __ mov(ecx, Operand(edx));
4342 __ or_(ecx, Operand(eax));
4343 __ test(ecx, Immediate(kSmiTagMask));
4344 __ j(not_zero, &non_smi, not_taken);
4345 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
4346 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004347 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004348 __ bind(&smi_done);
4349 __ mov(eax, edx);
4350 __ ret(0);
4351 __ bind(&non_smi);
4352 } else if (FLAG_debug_code) {
4353 __ mov(ecx, Operand(edx));
4354 __ or_(ecx, Operand(eax));
4355 __ test(ecx, Immediate(kSmiTagMask));
4356 __ Assert(not_zero, "Unexpected smi operands.");
4357 }
4358
ricow@chromium.org65fae842010-08-25 15:26:24 +00004359 // NOTICE! This code is only reached after a smi-fast-case check, so
4360 // it is certain that at least one operand isn't a smi.
4361
4362 // Identical objects can be compared fast, but there are some tricky cases
4363 // for NaN and undefined.
4364 {
4365 Label not_identical;
4366 __ cmp(eax, Operand(edx));
4367 __ j(not_equal, &not_identical);
4368
4369 if (cc_ != equal) {
4370 // Check for undefined. undefined OP undefined is false even though
4371 // undefined == undefined.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004372 NearLabel check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004373 __ cmp(edx, masm->isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004374 __ j(not_equal, &check_for_nan);
4375 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4376 __ ret(0);
4377 __ bind(&check_for_nan);
4378 }
4379
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004380 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00004381 // so we do the second best thing - test it ourselves.
4382 // Note: if cc_ != equal, never_nan_nan_ is not used.
4383 if (never_nan_nan_ && (cc_ == equal)) {
4384 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4385 __ ret(0);
4386 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004387 NearLabel heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004388 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004389 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004390 __ j(equal, &heap_number);
4391 if (cc_ != equal) {
4392 // Call runtime on identical JSObjects. Otherwise return equal.
4393 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4394 __ j(above_equal, &not_identical);
4395 }
4396 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4397 __ ret(0);
4398
4399 __ bind(&heap_number);
4400 // It is a heap number, so return non-equal if it's NaN and equal if
4401 // it's not NaN.
4402 // The representation of NaN values has all exponent bits (52..62) set,
4403 // and not all mantissa bits (0..51) clear.
4404 // We only accept QNaNs, which have bit 51 set.
4405 // Read top bits of double representation (second word of value).
4406
4407 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
4408 // all bits in the mask are set. We only need to check the word
4409 // that contains the exponent and high bit of the mantissa.
4410 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
4411 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004412 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004413 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
4414 // bits.
4415 __ add(edx, Operand(edx));
4416 __ cmp(edx, kQuietNaNHighBitsMask << 1);
4417 if (cc_ == equal) {
4418 STATIC_ASSERT(EQUAL != 1);
4419 __ setcc(above_equal, eax);
4420 __ ret(0);
4421 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004422 NearLabel nan;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004423 __ j(above_equal, &nan);
4424 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4425 __ ret(0);
4426 __ bind(&nan);
4427 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4428 __ ret(0);
4429 }
4430 }
4431
4432 __ bind(&not_identical);
4433 }
4434
4435 // Strict equality can quickly decide whether objects are equal.
4436 // Non-strict object equality is slower, so it is handled later in the stub.
4437 if (cc_ == equal && strict_) {
4438 Label slow; // Fallthrough label.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004439 NearLabel not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004440 // If we're doing a strict equality comparison, we don't have to do
4441 // type conversion, so we generate code to do fast comparison for objects
4442 // and oddballs. Non-smi numbers and strings still go through the usual
4443 // slow-case code.
4444 // If either is a Smi (we know that not both are), then they can only
4445 // be equal if the other is a HeapNumber. If so, use the slow case.
4446 STATIC_ASSERT(kSmiTag == 0);
4447 ASSERT_EQ(0, Smi::FromInt(0));
4448 __ mov(ecx, Immediate(kSmiTagMask));
4449 __ and_(ecx, Operand(eax));
4450 __ test(ecx, Operand(edx));
4451 __ j(not_zero, &not_smis);
4452 // One operand is a smi.
4453
4454 // Check whether the non-smi is a heap number.
4455 STATIC_ASSERT(kSmiTagMask == 1);
4456 // ecx still holds eax & kSmiTag, which is either zero or one.
4457 __ sub(Operand(ecx), Immediate(0x01));
4458 __ mov(ebx, edx);
4459 __ xor_(ebx, Operand(eax));
4460 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
4461 __ xor_(ebx, Operand(eax));
4462 // if eax was smi, ebx is now edx, else eax.
4463
4464 // Check if the non-smi operand is a heap number.
4465 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004466 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004467 // If heap number, handle it in the slow case.
4468 __ j(equal, &slow);
4469 // Return non-equal (ebx is not zero)
4470 __ mov(eax, ebx);
4471 __ ret(0);
4472
4473 __ bind(&not_smis);
4474 // If either operand is a JSObject or an oddball value, then they are not
4475 // equal since their pointers are different
4476 // There is no test for undetectability in strict equality.
4477
4478 // Get the type of the first operand.
4479 // If the first object is a JS object, we have done pointer comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004480 NearLabel first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004481 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4482 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4483 __ j(below, &first_non_object);
4484
4485 // Return non-zero (eax is not zero)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004486 NearLabel return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004487 STATIC_ASSERT(kHeapObjectTag != 0);
4488 __ bind(&return_not_equal);
4489 __ ret(0);
4490
4491 __ bind(&first_non_object);
4492 // Check for oddballs: true, false, null, undefined.
4493 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4494 __ j(equal, &return_not_equal);
4495
4496 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
4497 __ j(above_equal, &return_not_equal);
4498
4499 // Check for oddballs: true, false, null, undefined.
4500 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4501 __ j(equal, &return_not_equal);
4502
4503 // Fall through to the general case.
4504 __ bind(&slow);
4505 }
4506
4507 // Generate the number comparison code.
4508 if (include_number_compare_) {
4509 Label non_number_comparison;
4510 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004511 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004512 CpuFeatures::Scope use_sse2(SSE2);
4513 CpuFeatures::Scope use_cmov(CMOV);
4514
4515 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
4516 __ ucomisd(xmm0, xmm1);
4517
4518 // Don't base result on EFLAGS when a NaN is involved.
4519 __ j(parity_even, &unordered, not_taken);
4520 // Return a result of -1, 0, or 1, based on EFLAGS.
4521 __ mov(eax, 0); // equal
4522 __ mov(ecx, Immediate(Smi::FromInt(1)));
4523 __ cmov(above, eax, Operand(ecx));
4524 __ mov(ecx, Immediate(Smi::FromInt(-1)));
4525 __ cmov(below, eax, Operand(ecx));
4526 __ ret(0);
4527 } else {
4528 FloatingPointHelper::CheckFloatOperands(
4529 masm, &non_number_comparison, ebx);
4530 FloatingPointHelper::LoadFloatOperand(masm, eax);
4531 FloatingPointHelper::LoadFloatOperand(masm, edx);
4532 __ FCmp();
4533
4534 // Don't base result on EFLAGS when a NaN is involved.
4535 __ j(parity_even, &unordered, not_taken);
4536
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004537 NearLabel below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004538 // Return a result of -1, 0, or 1, based on EFLAGS.
4539 __ j(below, &below_label, not_taken);
4540 __ j(above, &above_label, not_taken);
4541
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004542 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004543 __ ret(0);
4544
4545 __ bind(&below_label);
4546 __ mov(eax, Immediate(Smi::FromInt(-1)));
4547 __ ret(0);
4548
4549 __ bind(&above_label);
4550 __ mov(eax, Immediate(Smi::FromInt(1)));
4551 __ ret(0);
4552 }
4553
4554 // If one of the numbers was NaN, then the result is always false.
4555 // The cc is never not-equal.
4556 __ bind(&unordered);
4557 ASSERT(cc_ != not_equal);
4558 if (cc_ == less || cc_ == less_equal) {
4559 __ mov(eax, Immediate(Smi::FromInt(1)));
4560 } else {
4561 __ mov(eax, Immediate(Smi::FromInt(-1)));
4562 }
4563 __ ret(0);
4564
4565 // The number comparison code did not provide a valid result.
4566 __ bind(&non_number_comparison);
4567 }
4568
4569 // Fast negative check for symbol-to-symbol equality.
4570 Label check_for_strings;
4571 if (cc_ == equal) {
4572 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4573 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4574
4575 // We've already checked for object identity, so if both operands
4576 // are symbols they aren't equal. Register eax already holds a
4577 // non-zero value, which indicates not equal, so just return.
4578 __ ret(0);
4579 }
4580
4581 __ bind(&check_for_strings);
4582
4583 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4584 &check_unequal_objects);
4585
4586 // Inline comparison of ascii strings.
4587 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4588 edx,
4589 eax,
4590 ecx,
4591 ebx,
4592 edi);
4593#ifdef DEBUG
4594 __ Abort("Unexpected fall-through from string comparison");
4595#endif
4596
4597 __ bind(&check_unequal_objects);
4598 if (cc_ == equal && !strict_) {
4599 // Non-strict equality. Objects are unequal if
4600 // they are both JSObjects and not undetectable,
4601 // and their pointers are different.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004602 NearLabel not_both_objects;
4603 NearLabel return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004604 // At most one is a smi, so we can test for smi by adding the two.
4605 // A smi plus a heap object has the low bit set, a heap object plus
4606 // a heap object has the low bit clear.
4607 STATIC_ASSERT(kSmiTag == 0);
4608 STATIC_ASSERT(kSmiTagMask == 1);
4609 __ lea(ecx, Operand(eax, edx, times_1, 0));
4610 __ test(ecx, Immediate(kSmiTagMask));
4611 __ j(not_zero, &not_both_objects);
4612 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4613 __ j(below, &not_both_objects);
4614 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
4615 __ j(below, &not_both_objects);
4616 // We do not bail out after this point. Both are JSObjects, and
4617 // they are equal if and only if both are undetectable.
4618 // The and of the undetectable flags is 1 if and only if they are equal.
4619 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4620 1 << Map::kIsUndetectable);
4621 __ j(zero, &return_unequal);
4622 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4623 1 << Map::kIsUndetectable);
4624 __ j(zero, &return_unequal);
4625 // The objects are both undetectable, so they both compare as the value
4626 // undefined, and are equal.
4627 __ Set(eax, Immediate(EQUAL));
4628 __ bind(&return_unequal);
4629 // Return non-equal by returning the non-zero object pointer in eax,
4630 // or return equal if we fell through to here.
4631 __ ret(0); // rax, rdx were pushed
4632 __ bind(&not_both_objects);
4633 }
4634
4635 // Push arguments below the return address.
4636 __ pop(ecx);
4637 __ push(edx);
4638 __ push(eax);
4639
4640 // Figure out which native to call and setup the arguments.
4641 Builtins::JavaScript builtin;
4642 if (cc_ == equal) {
4643 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4644 } else {
4645 builtin = Builtins::COMPARE;
4646 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4647 }
4648
4649 // Restore return address on the stack.
4650 __ push(ecx);
4651
4652 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4653 // tagged as a small integer.
4654 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4655}
4656
4657
4658void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4659 Label* label,
4660 Register object,
4661 Register scratch) {
4662 __ test(object, Immediate(kSmiTagMask));
4663 __ j(zero, label);
4664 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4665 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4666 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4667 __ cmp(scratch, kSymbolTag | kStringTag);
4668 __ j(not_equal, label);
4669}
4670
4671
4672void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004673 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004674}
4675
4676
4677void CallFunctionStub::Generate(MacroAssembler* masm) {
4678 Label slow;
4679
4680 // If the receiver might be a value (string, number or boolean) check for this
4681 // and box it if it is.
4682 if (ReceiverMightBeValue()) {
4683 // Get the receiver from the stack.
4684 // +1 ~ return address
4685 Label receiver_is_value, receiver_is_js_object;
4686 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
4687
4688 // Check if receiver is a smi (which is a number value).
4689 __ test(eax, Immediate(kSmiTagMask));
4690 __ j(zero, &receiver_is_value, not_taken);
4691
4692 // Check if the receiver is a valid JS object.
4693 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
4694 __ j(above_equal, &receiver_is_js_object);
4695
4696 // Call the runtime to box the value.
4697 __ bind(&receiver_is_value);
4698 __ EnterInternalFrame();
4699 __ push(eax);
4700 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
4701 __ LeaveInternalFrame();
4702 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
4703
4704 __ bind(&receiver_is_js_object);
4705 }
4706
4707 // Get the function to call from the stack.
4708 // +2 ~ receiver, return address
4709 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4710
4711 // Check that the function really is a JavaScript function.
4712 __ test(edi, Immediate(kSmiTagMask));
4713 __ j(zero, &slow, not_taken);
4714 // Goto slow case if we do not have a function.
4715 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
4716 __ j(not_equal, &slow, not_taken);
4717
4718 // Fast-case: Just invoke the function.
4719 ParameterCount actual(argc_);
4720 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
4721
4722 // Slow-case: Non-function called.
4723 __ bind(&slow);
4724 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4725 // of the original receiver from the call site).
4726 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4727 __ Set(eax, Immediate(argc_));
4728 __ Set(ebx, Immediate(0));
4729 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004730 Handle<Code> adaptor =
4731 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004732 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4733}
4734
4735
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004736bool CEntryStub::NeedsImmovableCode() {
4737 return false;
4738}
4739
4740
ricow@chromium.org65fae842010-08-25 15:26:24 +00004741void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004742 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004743}
4744
4745
ricow@chromium.org65fae842010-08-25 15:26:24 +00004746void CEntryStub::GenerateCore(MacroAssembler* masm,
4747 Label* throw_normal_exception,
4748 Label* throw_termination_exception,
4749 Label* throw_out_of_memory_exception,
4750 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004751 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004752 // eax: result parameter for PerformGC, if any
4753 // ebx: pointer to C function (C callee-saved)
4754 // ebp: frame pointer (restored after C call)
4755 // esp: stack pointer (restored after C call)
4756 // edi: number of arguments including receiver (C callee-saved)
4757 // esi: pointer to the first argument (C callee-saved)
4758
4759 // Result returned in eax, or eax+edx if result_size_ is 2.
4760
4761 // Check stack alignment.
4762 if (FLAG_debug_code) {
4763 __ CheckStackAlignment();
4764 }
4765
4766 if (do_gc) {
4767 // Pass failure code returned from last attempt as first argument to
4768 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4769 // stack alignment is known to be correct. This function takes one argument
4770 // which is passed on the stack, and we know that the stack has been
4771 // prepared to pass at least one argument.
4772 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4773 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4774 }
4775
4776 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004777 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004778 if (always_allocate_scope) {
4779 __ inc(Operand::StaticVariable(scope_depth));
4780 }
4781
4782 // Call C function.
4783 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4784 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004785 __ mov(Operand(esp, 2 * kPointerSize),
4786 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004787 __ call(Operand(ebx));
4788 // Result is in eax or edx:eax - do not destroy these registers!
4789
4790 if (always_allocate_scope) {
4791 __ dec(Operand::StaticVariable(scope_depth));
4792 }
4793
4794 // Make sure we're not trying to return 'the hole' from the runtime
4795 // call as this may lead to crashes in the IC code later.
4796 if (FLAG_debug_code) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004797 NearLabel okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004798 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004799 __ j(not_equal, &okay);
4800 __ int3();
4801 __ bind(&okay);
4802 }
4803
4804 // Check for failure result.
4805 Label failure_returned;
4806 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4807 __ lea(ecx, Operand(eax, 1));
4808 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4809 __ test(ecx, Immediate(kFailureTagMask));
4810 __ j(zero, &failure_returned, not_taken);
4811
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004812 ExternalReference pending_exception_address(
4813 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004814
4815 // Check that there is no pending exception, otherwise we
4816 // should have returned some failure value.
4817 if (FLAG_debug_code) {
4818 __ push(edx);
4819 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004820 ExternalReference::the_hole_value_location(masm->isolate())));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004821 NearLabel okay;
4822 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4823 // Cannot use check here as it attempts to generate call into runtime.
4824 __ j(equal, &okay);
4825 __ int3();
4826 __ bind(&okay);
4827 __ pop(edx);
4828 }
4829
ricow@chromium.org65fae842010-08-25 15:26:24 +00004830 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004831 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004832 __ ret(0);
4833
4834 // Handling of failure.
4835 __ bind(&failure_returned);
4836
4837 Label retry;
4838 // If the returned exception is RETRY_AFTER_GC continue at retry label
4839 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4840 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4841 __ j(zero, &retry, taken);
4842
4843 // Special handling of out of memory exceptions.
4844 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4845 __ j(equal, throw_out_of_memory_exception);
4846
4847 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004848 ExternalReference the_hole_location =
4849 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004850 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004851 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004852 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4853
4854 // Special handling of termination exceptions which are uncatchable
4855 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004856 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004857 __ j(equal, throw_termination_exception);
4858
4859 // Handle normal exception.
4860 __ jmp(throw_normal_exception);
4861
4862 // Retry.
4863 __ bind(&retry);
4864}
4865
4866
4867void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4868 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004869 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004870}
4871
4872
4873void CEntryStub::Generate(MacroAssembler* masm) {
4874 // eax: number of arguments including receiver
4875 // ebx: pointer to C function (C callee-saved)
4876 // ebp: frame pointer (restored after C call)
4877 // esp: stack pointer (restored after C call)
4878 // esi: current context (C callee-saved)
4879 // edi: JS function of the caller (C callee-saved)
4880
4881 // NOTE: Invocations of builtins may return failure objects instead
4882 // of a proper result. The builtin entry handles this by performing
4883 // a garbage collection and retrying the builtin (twice).
4884
4885 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004886 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004887
4888 // eax: result parameter for PerformGC, if any (setup below)
4889 // ebx: pointer to builtin function (C callee-saved)
4890 // ebp: frame pointer (restored after C call)
4891 // esp: stack pointer (restored after C call)
4892 // edi: number of arguments including receiver (C callee-saved)
4893 // esi: argv pointer (C callee-saved)
4894
4895 Label throw_normal_exception;
4896 Label throw_termination_exception;
4897 Label throw_out_of_memory_exception;
4898
4899 // Call into the runtime system.
4900 GenerateCore(masm,
4901 &throw_normal_exception,
4902 &throw_termination_exception,
4903 &throw_out_of_memory_exception,
4904 false,
4905 false);
4906
4907 // Do space-specific GC and retry runtime call.
4908 GenerateCore(masm,
4909 &throw_normal_exception,
4910 &throw_termination_exception,
4911 &throw_out_of_memory_exception,
4912 true,
4913 false);
4914
4915 // Do full GC and retry runtime call one final time.
4916 Failure* failure = Failure::InternalError();
4917 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4918 GenerateCore(masm,
4919 &throw_normal_exception,
4920 &throw_termination_exception,
4921 &throw_out_of_memory_exception,
4922 true,
4923 true);
4924
4925 __ bind(&throw_out_of_memory_exception);
4926 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4927
4928 __ bind(&throw_termination_exception);
4929 GenerateThrowUncatchable(masm, TERMINATION);
4930
4931 __ bind(&throw_normal_exception);
4932 GenerateThrowTOS(masm);
4933}
4934
4935
4936void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4937 Label invoke, exit;
4938#ifdef ENABLE_LOGGING_AND_PROFILING
4939 Label not_outermost_js, not_outermost_js_2;
4940#endif
4941
4942 // Setup frame.
4943 __ push(ebp);
4944 __ mov(ebp, Operand(esp));
4945
4946 // Push marker in two places.
4947 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4948 __ push(Immediate(Smi::FromInt(marker))); // context slot
4949 __ push(Immediate(Smi::FromInt(marker))); // function slot
4950 // Save callee-saved registers (C calling conventions).
4951 __ push(edi);
4952 __ push(esi);
4953 __ push(ebx);
4954
4955 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004956 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004957 __ push(Operand::StaticVariable(c_entry_fp));
4958
4959#ifdef ENABLE_LOGGING_AND_PROFILING
4960 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004961 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4962 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004963 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4964 __ j(not_equal, &not_outermost_js);
4965 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4966 __ bind(&not_outermost_js);
4967#endif
4968
4969 // Call a faked try-block that does the invoke.
4970 __ call(&invoke);
4971
4972 // Caught exception: Store result (exception) in the pending
4973 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004974 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4975 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004976 __ mov(Operand::StaticVariable(pending_exception), eax);
4977 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4978 __ jmp(&exit);
4979
4980 // Invoke: Link this frame into the handler chain.
4981 __ bind(&invoke);
4982 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4983
4984 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004985 ExternalReference the_hole_location =
4986 ExternalReference::the_hole_value_location(masm->isolate());
4987 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004988 __ mov(Operand::StaticVariable(pending_exception), edx);
4989
4990 // Fake a receiver (NULL).
4991 __ push(Immediate(0)); // receiver
4992
4993 // Invoke the function by calling through JS entry trampoline
4994 // builtin and pop the faked function when we return. Notice that we
4995 // cannot store a reference to the trampoline code directly in this
4996 // stub, because the builtin stubs may not have been generated yet.
4997 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004998 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004999 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005000 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005001 __ mov(edx, Immediate(construct_entry));
5002 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005003 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005004 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005005 __ mov(edx, Immediate(entry));
5006 }
5007 __ mov(edx, Operand(edx, 0)); // deref address
5008 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
5009 __ call(Operand(edx));
5010
5011 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005012 __ pop(Operand::StaticVariable(ExternalReference(
5013 Isolate::k_handler_address,
5014 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005015 // Pop next_sp.
5016 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
5017
5018#ifdef ENABLE_LOGGING_AND_PROFILING
5019 // If current EBP value is the same as js_entry_sp value, it means that
5020 // the current function is the outermost.
5021 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
5022 __ j(not_equal, &not_outermost_js_2);
5023 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
5024 __ bind(&not_outermost_js_2);
5025#endif
5026
5027 // Restore the top frame descriptor from the stack.
5028 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005029 __ pop(Operand::StaticVariable(ExternalReference(
5030 Isolate::k_c_entry_fp_address,
5031 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005032
5033 // Restore callee-saved registers (C calling conventions).
5034 __ pop(ebx);
5035 __ pop(esi);
5036 __ pop(edi);
5037 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
5038
5039 // Restore frame pointer and return.
5040 __ pop(ebp);
5041 __ ret(0);
5042}
5043
5044
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005045// Generate stub code for instanceof.
5046// This code can patch a call site inlined cache of the instance of check,
5047// which looks like this.
5048//
5049// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
5050// 75 0a jne <some near label>
5051// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
5052//
5053// If call site patching is requested the stack will have the delta from the
5054// return address to the cmp instruction just below the return address. This
5055// also means that call site patching can only take place with arguments in
5056// registers. TOS looks like this when call site patching is requested
5057//
5058// esp[0] : return address
5059// esp[4] : delta from return address to cmp instruction
5060//
ricow@chromium.org65fae842010-08-25 15:26:24 +00005061void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005062 // Call site inlining and patching implies arguments in registers.
5063 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
5064
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005065 // Fixed register usage throughout the stub.
5066 Register object = eax; // Object (lhs).
5067 Register map = ebx; // Map of the object.
5068 Register function = edx; // Function (rhs).
5069 Register prototype = edi; // Prototype of the function.
5070 Register scratch = ecx;
5071
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005072 // Constants describing the call site code to patch.
5073 static const int kDeltaToCmpImmediate = 2;
5074 static const int kDeltaToMov = 8;
5075 static const int kDeltaToMovImmediate = 9;
5076 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
5077 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
5078 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
5079
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005080 ExternalReference roots_address =
5081 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005082
5083 ASSERT_EQ(object.code(), InstanceofStub::left().code());
5084 ASSERT_EQ(function.code(), InstanceofStub::right().code());
5085
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005086 // Get the object and function - they are always both needed.
5087 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005088 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005089 __ mov(object, Operand(esp, 2 * kPointerSize));
5090 __ mov(function, Operand(esp, 1 * kPointerSize));
5091 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005092
5093 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005094 __ test(object, Immediate(kSmiTagMask));
5095 __ j(zero, &not_js_object, not_taken);
5096 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005097
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005098 // If there is a call site cache don't look in the global cache, but do the
5099 // real lookup and update the call site cache.
5100 if (!HasCallSiteInlineCheck()) {
5101 // Look up the function and the map in the instanceof cache.
5102 NearLabel miss;
5103 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5104 __ cmp(function,
5105 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5106 __ j(not_equal, &miss);
5107 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5108 __ cmp(map, Operand::StaticArray(
5109 scratch, times_pointer_size, roots_address));
5110 __ j(not_equal, &miss);
5111 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5112 __ mov(eax, Operand::StaticArray(
5113 scratch, times_pointer_size, roots_address));
5114 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
5115 __ bind(&miss);
5116 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005117
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005118 // Get the prototype of the function.
5119 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005120
5121 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005122 __ test(prototype, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005123 __ j(zero, &slow, not_taken);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005124 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005125
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005126 // Update the global instanceof or call site inlined cache with the current
5127 // map and function. The cached answer will be set when it is known below.
5128 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005129 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5130 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
5131 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5132 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
5133 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005134 } else {
5135 // The constants for the code patching are based on no push instructions
5136 // at the call site.
5137 ASSERT(HasArgsInRegisters());
5138 // Get return address and delta to inlined map check.
5139 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5140 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5141 if (FLAG_debug_code) {
5142 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
5143 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
5144 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
5145 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
5146 }
5147 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
5148 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005149
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005150 // Loop through the prototype chain of the object looking for the function
5151 // prototype.
5152 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005153 NearLabel loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005154 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005155 __ cmp(scratch, Operand(prototype));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005156 __ j(equal, &is_instance);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005157 Factory* factory = masm->isolate()->factory();
5158 __ cmp(Operand(scratch), Immediate(factory->null_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005159 __ j(equal, &is_not_instance);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005160 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5161 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005162 __ jmp(&loop);
5163
5164 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005165 if (!HasCallSiteInlineCheck()) {
5166 __ Set(eax, Immediate(0));
5167 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5168 __ mov(Operand::StaticArray(scratch,
5169 times_pointer_size, roots_address), eax);
5170 } else {
5171 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005172 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005173 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5174 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5175 if (FLAG_debug_code) {
5176 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5177 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5178 }
5179 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5180 if (!ReturnTrueFalseObject()) {
5181 __ Set(eax, Immediate(0));
5182 }
5183 }
5184 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005185
5186 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005187 if (!HasCallSiteInlineCheck()) {
5188 __ Set(eax, Immediate(Smi::FromInt(1)));
5189 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5190 __ mov(Operand::StaticArray(
5191 scratch, times_pointer_size, roots_address), eax);
5192 } else {
5193 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005194 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005195 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5196 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5197 if (FLAG_debug_code) {
5198 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5199 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5200 }
5201 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5202 if (!ReturnTrueFalseObject()) {
5203 __ Set(eax, Immediate(Smi::FromInt(1)));
5204 }
5205 }
5206 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005207
5208 Label object_not_null, object_not_null_or_smi;
5209 __ bind(&not_js_object);
5210 // Before null, smi and string value checks, check that the rhs is a function
5211 // as for a non-function rhs an exception needs to be thrown.
5212 __ test(function, Immediate(kSmiTagMask));
5213 __ j(zero, &slow, not_taken);
5214 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
5215 __ j(not_equal, &slow, not_taken);
5216
5217 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005218 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005219 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005220 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005221 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005222
5223 __ bind(&object_not_null);
5224 // Smi values is not instance of anything.
5225 __ test(object, Immediate(kSmiTagMask));
5226 __ j(not_zero, &object_not_null_or_smi, not_taken);
5227 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005228 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005229
5230 __ bind(&object_not_null_or_smi);
5231 // String values is not instance of anything.
5232 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
5233 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005234 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005235 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005236
5237 // Slow-case: Go through the JavaScript implementation.
5238 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005239 if (!ReturnTrueFalseObject()) {
5240 // Tail call the builtin which returns 0 or 1.
5241 if (HasArgsInRegisters()) {
5242 // Push arguments below return address.
5243 __ pop(scratch);
5244 __ push(object);
5245 __ push(function);
5246 __ push(scratch);
5247 }
5248 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
5249 } else {
5250 // Call the builtin and convert 0/1 to true/false.
5251 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005252 __ push(object);
5253 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005254 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
5255 __ LeaveInternalFrame();
5256 NearLabel true_value, done;
5257 __ test(eax, Operand(eax));
5258 __ j(zero, &true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005259 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005260 __ jmp(&done);
5261 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005262 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005263 __ bind(&done);
5264 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005265 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005266}
5267
5268
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005269Register InstanceofStub::left() { return eax; }
5270
5271
5272Register InstanceofStub::right() { return edx; }
5273
5274
ricow@chromium.org65fae842010-08-25 15:26:24 +00005275int CompareStub::MinorKey() {
5276 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5277 // stubs the never NaN NaN condition is only taken into account if the
5278 // condition is equals.
5279 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
5280 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5281 return ConditionField::encode(static_cast<unsigned>(cc_))
5282 | RegisterField::encode(false) // lhs_ and rhs_ are not used
5283 | StrictField::encode(strict_)
5284 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005285 | IncludeNumberCompareField::encode(include_number_compare_)
5286 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005287}
5288
5289
5290// Unfortunately you have to run without snapshots to see most of these
5291// names in the profile since most compare stubs end up in the snapshot.
5292const char* CompareStub::GetName() {
5293 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5294
5295 if (name_ != NULL) return name_;
5296 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005297 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
5298 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005299 if (name_ == NULL) return "OOM";
5300
5301 const char* cc_name;
5302 switch (cc_) {
5303 case less: cc_name = "LT"; break;
5304 case greater: cc_name = "GT"; break;
5305 case less_equal: cc_name = "LE"; break;
5306 case greater_equal: cc_name = "GE"; break;
5307 case equal: cc_name = "EQ"; break;
5308 case not_equal: cc_name = "NE"; break;
5309 default: cc_name = "UnknownCondition"; break;
5310 }
5311
5312 const char* strict_name = "";
5313 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
5314 strict_name = "_STRICT";
5315 }
5316
5317 const char* never_nan_nan_name = "";
5318 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
5319 never_nan_nan_name = "_NO_NAN";
5320 }
5321
5322 const char* include_number_compare_name = "";
5323 if (!include_number_compare_) {
5324 include_number_compare_name = "_NO_NUMBER";
5325 }
5326
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005327 const char* include_smi_compare_name = "";
5328 if (!include_smi_compare_) {
5329 include_smi_compare_name = "_NO_SMI";
5330 }
5331
ricow@chromium.org65fae842010-08-25 15:26:24 +00005332 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005333 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00005334 cc_name,
5335 strict_name,
5336 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005337 include_number_compare_name,
5338 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005339 return name_;
5340}
5341
5342
5343// -------------------------------------------------------------------------
5344// StringCharCodeAtGenerator
5345
5346void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
5347 Label flat_string;
5348 Label ascii_string;
5349 Label got_char_code;
5350
5351 // If the receiver is a smi trigger the non-string case.
5352 STATIC_ASSERT(kSmiTag == 0);
5353 __ test(object_, Immediate(kSmiTagMask));
5354 __ j(zero, receiver_not_string_);
5355
5356 // Fetch the instance type of the receiver into result register.
5357 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5358 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5359 // If the receiver is not a string trigger the non-string case.
5360 __ test(result_, Immediate(kIsNotStringMask));
5361 __ j(not_zero, receiver_not_string_);
5362
5363 // If the index is non-smi trigger the non-smi case.
5364 STATIC_ASSERT(kSmiTag == 0);
5365 __ test(index_, Immediate(kSmiTagMask));
5366 __ j(not_zero, &index_not_smi_);
5367
5368 // Put smi-tagged index into scratch register.
5369 __ mov(scratch_, index_);
5370 __ bind(&got_smi_index_);
5371
5372 // Check for index out of range.
5373 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
5374 __ j(above_equal, index_out_of_range_);
5375
5376 // We need special handling for non-flat strings.
5377 STATIC_ASSERT(kSeqStringTag == 0);
5378 __ test(result_, Immediate(kStringRepresentationMask));
5379 __ j(zero, &flat_string);
5380
5381 // Handle non-flat strings.
5382 __ test(result_, Immediate(kIsConsStringMask));
5383 __ j(zero, &call_runtime_);
5384
5385 // ConsString.
5386 // Check whether the right hand side is the empty string (i.e. if
5387 // this is really a flat string in a cons string). If that is not
5388 // the case we would rather go to the runtime system now to flatten
5389 // the string.
5390 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005391 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005392 __ j(not_equal, &call_runtime_);
5393 // Get the first of the two strings and load its instance type.
5394 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
5395 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5396 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5397 // If the first cons component is also non-flat, then go to runtime.
5398 STATIC_ASSERT(kSeqStringTag == 0);
5399 __ test(result_, Immediate(kStringRepresentationMask));
5400 __ j(not_zero, &call_runtime_);
5401
5402 // Check for 1-byte or 2-byte string.
5403 __ bind(&flat_string);
5404 STATIC_ASSERT(kAsciiStringTag != 0);
5405 __ test(result_, Immediate(kStringEncodingMask));
5406 __ j(not_zero, &ascii_string);
5407
5408 // 2-byte string.
5409 // Load the 2-byte character code into the result register.
5410 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
5411 __ movzx_w(result_, FieldOperand(object_,
5412 scratch_, times_1, // Scratch is smi-tagged.
5413 SeqTwoByteString::kHeaderSize));
5414 __ jmp(&got_char_code);
5415
5416 // ASCII string.
5417 // Load the byte into the result register.
5418 __ bind(&ascii_string);
5419 __ SmiUntag(scratch_);
5420 __ movzx_b(result_, FieldOperand(object_,
5421 scratch_, times_1,
5422 SeqAsciiString::kHeaderSize));
5423 __ bind(&got_char_code);
5424 __ SmiTag(result_);
5425 __ bind(&exit_);
5426}
5427
5428
5429void StringCharCodeAtGenerator::GenerateSlow(
5430 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5431 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5432
5433 // Index is not a smi.
5434 __ bind(&index_not_smi_);
5435 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005436 __ CheckMap(index_,
5437 masm->isolate()->factory()->heap_number_map(),
5438 index_not_number_,
5439 true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005440 call_helper.BeforeCall(masm);
5441 __ push(object_);
5442 __ push(index_);
5443 __ push(index_); // Consumed by runtime conversion function.
5444 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5445 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5446 } else {
5447 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5448 // NumberToSmi discards numbers that are not exact integers.
5449 __ CallRuntime(Runtime::kNumberToSmi, 1);
5450 }
5451 if (!scratch_.is(eax)) {
5452 // Save the conversion result before the pop instructions below
5453 // have a chance to overwrite it.
5454 __ mov(scratch_, eax);
5455 }
5456 __ pop(index_);
5457 __ pop(object_);
5458 // Reload the instance type.
5459 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5460 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5461 call_helper.AfterCall(masm);
5462 // If index is still not a smi, it must be out of range.
5463 STATIC_ASSERT(kSmiTag == 0);
5464 __ test(scratch_, Immediate(kSmiTagMask));
5465 __ j(not_zero, index_out_of_range_);
5466 // Otherwise, return to the fast path.
5467 __ jmp(&got_smi_index_);
5468
5469 // Call runtime. We get here when the receiver is a string and the
5470 // index is a number, but the code of getting the actual character
5471 // is too complex (e.g., when the string needs to be flattened).
5472 __ bind(&call_runtime_);
5473 call_helper.BeforeCall(masm);
5474 __ push(object_);
5475 __ push(index_);
5476 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5477 if (!result_.is(eax)) {
5478 __ mov(result_, eax);
5479 }
5480 call_helper.AfterCall(masm);
5481 __ jmp(&exit_);
5482
5483 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5484}
5485
5486
5487// -------------------------------------------------------------------------
5488// StringCharFromCodeGenerator
5489
5490void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5491 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5492 STATIC_ASSERT(kSmiTag == 0);
5493 STATIC_ASSERT(kSmiShiftSize == 0);
5494 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5495 __ test(code_,
5496 Immediate(kSmiTagMask |
5497 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
5498 __ j(not_zero, &slow_case_, not_taken);
5499
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005500 Factory* factory = masm->isolate()->factory();
5501 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005502 STATIC_ASSERT(kSmiTag == 0);
5503 STATIC_ASSERT(kSmiTagSize == 1);
5504 STATIC_ASSERT(kSmiShiftSize == 0);
5505 // At this point code register contains smi tagged ascii char code.
5506 __ mov(result_, FieldOperand(result_,
5507 code_, times_half_pointer_size,
5508 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005509 __ cmp(result_, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005510 __ j(equal, &slow_case_, not_taken);
5511 __ bind(&exit_);
5512}
5513
5514
5515void StringCharFromCodeGenerator::GenerateSlow(
5516 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5517 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5518
5519 __ bind(&slow_case_);
5520 call_helper.BeforeCall(masm);
5521 __ push(code_);
5522 __ CallRuntime(Runtime::kCharFromCode, 1);
5523 if (!result_.is(eax)) {
5524 __ mov(result_, eax);
5525 }
5526 call_helper.AfterCall(masm);
5527 __ jmp(&exit_);
5528
5529 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5530}
5531
5532
5533// -------------------------------------------------------------------------
5534// StringCharAtGenerator
5535
5536void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5537 char_code_at_generator_.GenerateFast(masm);
5538 char_from_code_generator_.GenerateFast(masm);
5539}
5540
5541
5542void StringCharAtGenerator::GenerateSlow(
5543 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5544 char_code_at_generator_.GenerateSlow(masm, call_helper);
5545 char_from_code_generator_.GenerateSlow(masm, call_helper);
5546}
5547
5548
5549void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005550 Label string_add_runtime, call_builtin;
5551 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005552
5553 // Load the two arguments.
5554 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5555 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5556
5557 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005558 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005559 __ test(eax, Immediate(kSmiTagMask));
5560 __ j(zero, &string_add_runtime);
5561 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
5562 __ j(above_equal, &string_add_runtime);
5563
5564 // First argument is a a string, test second.
5565 __ test(edx, Immediate(kSmiTagMask));
5566 __ j(zero, &string_add_runtime);
5567 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
5568 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005569 } else {
5570 // Here at least one of the arguments is definitely a string.
5571 // We convert the one that is not known to be a string.
5572 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5573 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5574 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5575 &call_builtin);
5576 builtin_id = Builtins::STRING_ADD_RIGHT;
5577 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5578 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5579 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5580 &call_builtin);
5581 builtin_id = Builtins::STRING_ADD_LEFT;
5582 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005583 }
5584
5585 // Both arguments are strings.
5586 // eax: first string
5587 // edx: second string
5588 // Check if either of the strings are empty. In that case return the other.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005589 NearLabel second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005590 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5591 STATIC_ASSERT(kSmiTag == 0);
5592 __ test(ecx, Operand(ecx));
5593 __ j(not_zero, &second_not_zero_length);
5594 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005595 Counters* counters = masm->isolate()->counters();
5596 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005597 __ ret(2 * kPointerSize);
5598 __ bind(&second_not_zero_length);
5599 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5600 STATIC_ASSERT(kSmiTag == 0);
5601 __ test(ebx, Operand(ebx));
5602 __ j(not_zero, &both_not_zero_length);
5603 // First string is empty, result is second string which is in edx.
5604 __ mov(eax, edx);
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
5608 // Both strings are non-empty.
5609 // eax: first string
5610 // ebx: length of first string as a smi
5611 // ecx: length of second string as a smi
5612 // edx: second string
5613 // Look at the length of the result of adding the two strings.
5614 Label string_add_flat_result, longer_than_two;
5615 __ bind(&both_not_zero_length);
5616 __ add(ebx, Operand(ecx));
5617 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5618 // Handle exceptionally long strings in the runtime system.
5619 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005620 // Use the symbol table when adding two one character strings, as it
5621 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005622 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
5623 __ j(not_equal, &longer_than_two);
5624
5625 // Check that both strings are non-external ascii strings.
5626 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
5627 &string_add_runtime);
5628
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005629 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005630 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5631 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5632
5633 // Try to lookup two character string in symbol table. If it is not found
5634 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005635 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005636 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005637 masm, ebx, ecx, eax, edx, edi,
5638 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005639 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005640 __ ret(2 * kPointerSize);
5641
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005642 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005643 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005644 // Reload the arguments.
5645 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5646 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5647 // Get the two characters forming the new string.
5648 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5649 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5650 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005651 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005652 __ AllocateAsciiString(eax, // Result.
5653 2, // Length.
5654 edi, // Scratch 1.
5655 edx, // Scratch 2.
5656 &string_add_runtime);
5657 // Pack both characters in ebx.
5658 __ shl(ecx, kBitsPerByte);
5659 __ or_(ebx, Operand(ecx));
5660 // Set the characters in the new string.
5661 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005662 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005663 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005664
5665 __ bind(&longer_than_two);
5666 // Check if resulting string will be flat.
5667 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
5668 __ j(below, &string_add_flat_result);
5669
5670 // If result is not supposed to be flat allocate a cons string object. If both
5671 // strings are ascii the result is an ascii cons string.
5672 Label non_ascii, allocated, ascii_data;
5673 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5674 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5675 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5676 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5677 __ and_(ecx, Operand(edi));
5678 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5679 __ test(ecx, Immediate(kAsciiStringTag));
5680 __ j(zero, &non_ascii);
5681 __ bind(&ascii_data);
5682 // Allocate an acsii cons string.
5683 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5684 __ bind(&allocated);
5685 // Fill the fields of the cons string.
5686 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5687 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5688 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5689 Immediate(String::kEmptyHashField));
5690 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5691 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5692 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005693 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005694 __ ret(2 * kPointerSize);
5695 __ bind(&non_ascii);
5696 // At least one of the strings is two-byte. Check whether it happens
5697 // to contain only ascii characters.
5698 // ecx: first instance type AND second instance type.
5699 // edi: second instance type.
5700 __ test(ecx, Immediate(kAsciiDataHintMask));
5701 __ j(not_zero, &ascii_data);
5702 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5703 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5704 __ xor_(edi, Operand(ecx));
5705 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5706 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5707 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5708 __ j(equal, &ascii_data);
5709 // Allocate a two byte cons string.
5710 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5711 __ jmp(&allocated);
5712
5713 // Handle creating a flat result. First check that both strings are not
5714 // external strings.
5715 // eax: first string
5716 // ebx: length of resulting flat string as a smi
5717 // edx: second string
5718 __ bind(&string_add_flat_result);
5719 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5720 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5721 __ and_(ecx, kStringRepresentationMask);
5722 __ cmp(ecx, kExternalStringTag);
5723 __ j(equal, &string_add_runtime);
5724 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5725 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5726 __ and_(ecx, kStringRepresentationMask);
5727 __ cmp(ecx, kExternalStringTag);
5728 __ j(equal, &string_add_runtime);
5729 // Now check if both strings are ascii strings.
5730 // eax: first string
5731 // ebx: length of resulting flat string as a smi
5732 // edx: second string
5733 Label non_ascii_string_add_flat_result;
5734 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5735 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5736 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5737 __ j(zero, &non_ascii_string_add_flat_result);
5738 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5739 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5740 __ j(zero, &string_add_runtime);
5741
ricow@chromium.org65fae842010-08-25 15:26:24 +00005742 // Both strings are ascii strings. As they are short they are both flat.
5743 // ebx: length of resulting flat string as a smi
5744 __ SmiUntag(ebx);
5745 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5746 // eax: result string
5747 __ mov(ecx, eax);
5748 // Locate first character of result.
5749 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5750 // Load first argument and locate first character.
5751 __ mov(edx, Operand(esp, 2 * kPointerSize));
5752 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5753 __ SmiUntag(edi);
5754 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5755 // eax: result string
5756 // ecx: first character of result
5757 // edx: first char of first argument
5758 // edi: length of first argument
5759 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5760 // Load second argument and locate first character.
5761 __ mov(edx, Operand(esp, 1 * kPointerSize));
5762 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5763 __ SmiUntag(edi);
5764 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5765 // eax: result string
5766 // ecx: next character of result
5767 // edx: first char of second argument
5768 // edi: length of second argument
5769 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005770 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005771 __ ret(2 * kPointerSize);
5772
5773 // Handle creating a flat two byte result.
5774 // eax: first string - known to be two byte
5775 // ebx: length of resulting flat string as a smi
5776 // edx: second string
5777 __ bind(&non_ascii_string_add_flat_result);
5778 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5779 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5780 __ j(not_zero, &string_add_runtime);
5781 // Both strings are two byte strings. As they are short they are both
5782 // flat.
5783 __ SmiUntag(ebx);
5784 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5785 // eax: result string
5786 __ mov(ecx, eax);
5787 // Locate first character of result.
5788 __ add(Operand(ecx),
5789 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5790 // Load first argument and locate first character.
5791 __ mov(edx, Operand(esp, 2 * kPointerSize));
5792 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5793 __ SmiUntag(edi);
5794 __ add(Operand(edx),
5795 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5796 // eax: result string
5797 // ecx: first character of result
5798 // edx: first char of first argument
5799 // edi: length of first argument
5800 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5801 // Load second argument and locate first character.
5802 __ mov(edx, Operand(esp, 1 * kPointerSize));
5803 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5804 __ SmiUntag(edi);
5805 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5806 // eax: result string
5807 // ecx: next character of result
5808 // edx: first char of second argument
5809 // edi: length of second argument
5810 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005811 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005812 __ ret(2 * kPointerSize);
5813
5814 // Just jump to runtime to add the two strings.
5815 __ bind(&string_add_runtime);
5816 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005817
5818 if (call_builtin.is_linked()) {
5819 __ bind(&call_builtin);
5820 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5821 }
5822}
5823
5824
5825void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5826 int stack_offset,
5827 Register arg,
5828 Register scratch1,
5829 Register scratch2,
5830 Register scratch3,
5831 Label* slow) {
5832 // First check if the argument is already a string.
5833 Label not_string, done;
5834 __ test(arg, Immediate(kSmiTagMask));
5835 __ j(zero, &not_string);
5836 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5837 __ j(below, &done);
5838
5839 // Check the number to string cache.
5840 Label not_cached;
5841 __ bind(&not_string);
5842 // Puts the cached result into scratch1.
5843 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5844 arg,
5845 scratch1,
5846 scratch2,
5847 scratch3,
5848 false,
5849 &not_cached);
5850 __ mov(arg, scratch1);
5851 __ mov(Operand(esp, stack_offset), arg);
5852 __ jmp(&done);
5853
5854 // Check if the argument is a safe string wrapper.
5855 __ bind(&not_cached);
5856 __ test(arg, Immediate(kSmiTagMask));
5857 __ j(zero, slow);
5858 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5859 __ j(not_equal, slow);
5860 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5861 1 << Map::kStringWrapperSafeForDefaultValueOf);
5862 __ j(zero, slow);
5863 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5864 __ mov(Operand(esp, stack_offset), arg);
5865
5866 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005867}
5868
5869
5870void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5871 Register dest,
5872 Register src,
5873 Register count,
5874 Register scratch,
5875 bool ascii) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005876 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005877 __ bind(&loop);
5878 // This loop just copies one character at a time, as it is only used for very
5879 // short strings.
5880 if (ascii) {
5881 __ mov_b(scratch, Operand(src, 0));
5882 __ mov_b(Operand(dest, 0), scratch);
5883 __ add(Operand(src), Immediate(1));
5884 __ add(Operand(dest), Immediate(1));
5885 } else {
5886 __ mov_w(scratch, Operand(src, 0));
5887 __ mov_w(Operand(dest, 0), scratch);
5888 __ add(Operand(src), Immediate(2));
5889 __ add(Operand(dest), Immediate(2));
5890 }
5891 __ sub(Operand(count), Immediate(1));
5892 __ j(not_zero, &loop);
5893}
5894
5895
5896void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5897 Register dest,
5898 Register src,
5899 Register count,
5900 Register scratch,
5901 bool ascii) {
5902 // Copy characters using rep movs of doublewords.
5903 // The destination is aligned on a 4 byte boundary because we are
5904 // copying to the beginning of a newly allocated string.
5905 ASSERT(dest.is(edi)); // rep movs destination
5906 ASSERT(src.is(esi)); // rep movs source
5907 ASSERT(count.is(ecx)); // rep movs count
5908 ASSERT(!scratch.is(dest));
5909 ASSERT(!scratch.is(src));
5910 ASSERT(!scratch.is(count));
5911
5912 // Nothing to do for zero characters.
5913 Label done;
5914 __ test(count, Operand(count));
5915 __ j(zero, &done);
5916
5917 // Make count the number of bytes to copy.
5918 if (!ascii) {
5919 __ shl(count, 1);
5920 }
5921
5922 // Don't enter the rep movs if there are less than 4 bytes to copy.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005923 NearLabel last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005924 __ test(count, Immediate(~3));
5925 __ j(zero, &last_bytes);
5926
5927 // Copy from edi to esi using rep movs instruction.
5928 __ mov(scratch, count);
5929 __ sar(count, 2); // Number of doublewords to copy.
5930 __ cld();
5931 __ rep_movs();
5932
5933 // Find number of bytes left.
5934 __ mov(count, scratch);
5935 __ and_(count, 3);
5936
5937 // Check if there are more bytes to copy.
5938 __ bind(&last_bytes);
5939 __ test(count, Operand(count));
5940 __ j(zero, &done);
5941
5942 // Copy remaining characters.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005943 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005944 __ bind(&loop);
5945 __ mov_b(scratch, Operand(src, 0));
5946 __ mov_b(Operand(dest, 0), scratch);
5947 __ add(Operand(src), Immediate(1));
5948 __ add(Operand(dest), Immediate(1));
5949 __ sub(Operand(count), Immediate(1));
5950 __ j(not_zero, &loop);
5951
5952 __ bind(&done);
5953}
5954
5955
5956void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5957 Register c1,
5958 Register c2,
5959 Register scratch1,
5960 Register scratch2,
5961 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005962 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005963 Label* not_found) {
5964 // Register scratch3 is the general scratch register in this function.
5965 Register scratch = scratch3;
5966
5967 // Make sure that both characters are not digits as such strings has a
5968 // different hash algorithm. Don't try to look for these in the symbol table.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005969 NearLabel not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005970 __ mov(scratch, c1);
5971 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5972 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
5973 __ j(above, &not_array_index);
5974 __ mov(scratch, c2);
5975 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5976 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005977 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005978
5979 __ bind(&not_array_index);
5980 // Calculate the two character string hash.
5981 Register hash = scratch1;
5982 GenerateHashInit(masm, hash, c1, scratch);
5983 GenerateHashAddCharacter(masm, hash, c2, scratch);
5984 GenerateHashGetHash(masm, hash, scratch);
5985
5986 // Collect the two characters in a register.
5987 Register chars = c1;
5988 __ shl(c2, kBitsPerByte);
5989 __ or_(chars, Operand(c2));
5990
5991 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5992 // hash: hash of two character string.
5993
5994 // Load the symbol table.
5995 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005996 ExternalReference roots_address =
5997 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005998 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5999 __ mov(symbol_table,
6000 Operand::StaticArray(scratch, times_pointer_size, roots_address));
6001
6002 // Calculate capacity mask from the symbol table capacity.
6003 Register mask = scratch2;
6004 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
6005 __ SmiUntag(mask);
6006 __ sub(Operand(mask), Immediate(1));
6007
6008 // Registers
6009 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
6010 // hash: hash of two character string
6011 // symbol_table: symbol table
6012 // mask: capacity mask
6013 // scratch: -
6014
6015 // Perform a number of probes in the symbol table.
6016 static const int kProbes = 4;
6017 Label found_in_symbol_table;
6018 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
6019 for (int i = 0; i < kProbes; i++) {
6020 // Calculate entry in symbol table.
6021 __ mov(scratch, hash);
6022 if (i > 0) {
6023 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
6024 }
6025 __ and_(scratch, Operand(mask));
6026
6027 // Load the entry from the symbol table.
6028 Register candidate = scratch; // Scratch register contains candidate.
6029 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
6030 __ mov(candidate,
6031 FieldOperand(symbol_table,
6032 scratch,
6033 times_pointer_size,
6034 SymbolTable::kElementsStartOffset));
6035
6036 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006037 Factory* factory = masm->isolate()->factory();
6038 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00006039 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006040 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00006041 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006042
6043 // If length is not 2 the string is not a candidate.
6044 __ cmp(FieldOperand(candidate, String::kLengthOffset),
6045 Immediate(Smi::FromInt(2)));
6046 __ j(not_equal, &next_probe[i]);
6047
6048 // As we are out of registers save the mask on the stack and use that
6049 // register as a temporary.
6050 __ push(mask);
6051 Register temp = mask;
6052
6053 // Check that the candidate is a non-external ascii string.
6054 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
6055 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
6056 __ JumpIfInstanceTypeIsNotSequentialAscii(
6057 temp, temp, &next_probe_pop_mask[i]);
6058
6059 // Check if the two characters match.
6060 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
6061 __ and_(temp, 0x0000ffff);
6062 __ cmp(chars, Operand(temp));
6063 __ j(equal, &found_in_symbol_table);
6064 __ bind(&next_probe_pop_mask[i]);
6065 __ pop(mask);
6066 __ bind(&next_probe[i]);
6067 }
6068
6069 // No matching 2 character string found by probing.
6070 __ jmp(not_found);
6071
6072 // Scratch register contains result when we fall through to here.
6073 Register result = scratch;
6074 __ bind(&found_in_symbol_table);
6075 __ pop(mask); // Pop saved mask from the stack.
6076 if (!result.is(eax)) {
6077 __ mov(eax, result);
6078 }
6079}
6080
6081
6082void StringHelper::GenerateHashInit(MacroAssembler* masm,
6083 Register hash,
6084 Register character,
6085 Register scratch) {
6086 // hash = character + (character << 10);
6087 __ mov(hash, character);
6088 __ shl(hash, 10);
6089 __ add(hash, Operand(character));
6090 // hash ^= hash >> 6;
6091 __ mov(scratch, hash);
6092 __ sar(scratch, 6);
6093 __ xor_(hash, Operand(scratch));
6094}
6095
6096
6097void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
6098 Register hash,
6099 Register character,
6100 Register scratch) {
6101 // hash += character;
6102 __ add(hash, Operand(character));
6103 // hash += hash << 10;
6104 __ mov(scratch, hash);
6105 __ shl(scratch, 10);
6106 __ add(hash, Operand(scratch));
6107 // hash ^= hash >> 6;
6108 __ mov(scratch, hash);
6109 __ sar(scratch, 6);
6110 __ xor_(hash, Operand(scratch));
6111}
6112
6113
6114void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
6115 Register hash,
6116 Register scratch) {
6117 // hash += hash << 3;
6118 __ mov(scratch, hash);
6119 __ shl(scratch, 3);
6120 __ add(hash, Operand(scratch));
6121 // hash ^= hash >> 11;
6122 __ mov(scratch, hash);
6123 __ sar(scratch, 11);
6124 __ xor_(hash, Operand(scratch));
6125 // hash += hash << 15;
6126 __ mov(scratch, hash);
6127 __ shl(scratch, 15);
6128 __ add(hash, Operand(scratch));
6129
6130 // if (hash == 0) hash = 27;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006131 NearLabel hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006132 __ test(hash, Operand(hash));
6133 __ j(not_zero, &hash_not_zero);
6134 __ mov(hash, Immediate(27));
6135 __ bind(&hash_not_zero);
6136}
6137
6138
6139void SubStringStub::Generate(MacroAssembler* masm) {
6140 Label runtime;
6141
6142 // Stack frame on entry.
6143 // esp[0]: return address
6144 // esp[4]: to
6145 // esp[8]: from
6146 // esp[12]: string
6147
6148 // Make sure first argument is a string.
6149 __ mov(eax, Operand(esp, 3 * kPointerSize));
6150 STATIC_ASSERT(kSmiTag == 0);
6151 __ test(eax, Immediate(kSmiTagMask));
6152 __ j(zero, &runtime);
6153 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
6154 __ j(NegateCondition(is_string), &runtime);
6155
6156 // eax: string
6157 // ebx: instance type
6158
6159 // Calculate length of sub string using the smi values.
6160 Label result_longer_than_two;
6161 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
6162 __ test(ecx, Immediate(kSmiTagMask));
6163 __ j(not_zero, &runtime);
6164 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
6165 __ test(edx, Immediate(kSmiTagMask));
6166 __ j(not_zero, &runtime);
6167 __ sub(ecx, Operand(edx));
6168 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
6169 Label return_eax;
6170 __ j(equal, &return_eax);
6171 // Special handling of sub-strings of length 1 and 2. One character strings
6172 // are handled in the runtime system (looked up in the single character
6173 // cache). Two character strings are looked for in the symbol cache.
6174 __ SmiUntag(ecx); // Result length is no longer smi.
6175 __ cmp(ecx, 2);
6176 __ j(greater, &result_longer_than_two);
6177 __ j(less, &runtime);
6178
6179 // Sub string of length 2 requested.
6180 // eax: string
6181 // ebx: instance type
6182 // ecx: sub string length (value is 2)
6183 // edx: from index (smi)
6184 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
6185
6186 // Get the two characters forming the sub string.
6187 __ SmiUntag(edx); // From index is no longer smi.
6188 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
6189 __ movzx_b(ecx,
6190 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
6191
6192 // Try to lookup two character string in symbol table.
6193 Label make_two_character_string;
6194 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00006195 masm, ebx, ecx, eax, edx, edi,
6196 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006197 __ ret(3 * kPointerSize);
6198
6199 __ bind(&make_two_character_string);
6200 // Setup registers for allocating the two character string.
6201 __ mov(eax, Operand(esp, 3 * kPointerSize));
6202 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
6203 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
6204 __ Set(ecx, Immediate(2));
6205
6206 __ bind(&result_longer_than_two);
6207 // eax: string
6208 // ebx: instance type
6209 // ecx: result string length
6210 // Check for flat ascii string
6211 Label non_ascii_flat;
6212 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
6213
6214 // Allocate the result.
6215 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
6216
6217 // eax: result string
6218 // ecx: result string length
6219 __ mov(edx, esi); // esi used by following code.
6220 // Locate first character of result.
6221 __ mov(edi, eax);
6222 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6223 // Load string argument and locate character of sub string start.
6224 __ mov(esi, Operand(esp, 3 * kPointerSize));
6225 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6226 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6227 __ SmiUntag(ebx);
6228 __ add(esi, Operand(ebx));
6229
6230 // eax: result string
6231 // ecx: result length
6232 // edx: original value of esi
6233 // edi: first character of result
6234 // esi: character of sub string start
6235 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
6236 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006237 Counters* counters = masm->isolate()->counters();
6238 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006239 __ ret(3 * kPointerSize);
6240
6241 __ bind(&non_ascii_flat);
6242 // eax: string
6243 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
6244 // ecx: result string length
6245 // Check for flat two byte string
6246 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
6247 __ j(not_equal, &runtime);
6248
6249 // Allocate the result.
6250 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
6251
6252 // eax: result string
6253 // ecx: result string length
6254 __ mov(edx, esi); // esi used by following code.
6255 // Locate first character of result.
6256 __ mov(edi, eax);
6257 __ add(Operand(edi),
6258 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6259 // Load string argument and locate character of sub string start.
6260 __ mov(esi, Operand(esp, 3 * kPointerSize));
6261 __ add(Operand(esi),
6262 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6263 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6264 // As from is a smi it is 2 times the value which matches the size of a two
6265 // byte character.
6266 STATIC_ASSERT(kSmiTag == 0);
6267 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
6268 __ add(esi, Operand(ebx));
6269
6270 // eax: result string
6271 // ecx: result length
6272 // edx: original value of esi
6273 // edi: first character of result
6274 // esi: character of sub string start
6275 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
6276 __ mov(esi, edx); // Restore esi.
6277
6278 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006279 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006280 __ ret(3 * kPointerSize);
6281
6282 // Just jump to runtime to create the sub string.
6283 __ bind(&runtime);
6284 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6285}
6286
6287
6288void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6289 Register left,
6290 Register right,
6291 Register scratch1,
6292 Register scratch2,
6293 Register scratch3) {
6294 Label result_not_equal;
6295 Label result_greater;
6296 Label compare_lengths;
6297
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006298 Counters* counters = masm->isolate()->counters();
6299 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006300
6301 // Find minimum length.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006302 NearLabel left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006303 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
6304 __ mov(scratch3, scratch1);
6305 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
6306
6307 Register length_delta = scratch3;
6308
6309 __ j(less_equal, &left_shorter);
6310 // Right string is shorter. Change scratch1 to be length of right string.
6311 __ sub(scratch1, Operand(length_delta));
6312 __ bind(&left_shorter);
6313
6314 Register min_length = scratch1;
6315
6316 // If either length is zero, just compare lengths.
6317 __ test(min_length, Operand(min_length));
6318 __ j(zero, &compare_lengths);
6319
6320 // Change index to run from -min_length to -1 by adding min_length
6321 // to string start. This means that loop ends when index reaches zero,
6322 // which doesn't need an additional compare.
6323 __ SmiUntag(min_length);
6324 __ lea(left,
6325 FieldOperand(left,
6326 min_length, times_1,
6327 SeqAsciiString::kHeaderSize));
6328 __ lea(right,
6329 FieldOperand(right,
6330 min_length, times_1,
6331 SeqAsciiString::kHeaderSize));
6332 __ neg(min_length);
6333
6334 Register index = min_length; // index = -min_length;
6335
6336 {
6337 // Compare loop.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006338 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006339 __ bind(&loop);
6340 // Compare characters.
6341 __ mov_b(scratch2, Operand(left, index, times_1, 0));
6342 __ cmpb(scratch2, Operand(right, index, times_1, 0));
6343 __ j(not_equal, &result_not_equal);
6344 __ add(Operand(index), Immediate(1));
6345 __ j(not_zero, &loop);
6346 }
6347
6348 // Compare lengths - strings up to min-length are equal.
6349 __ bind(&compare_lengths);
6350 __ test(length_delta, Operand(length_delta));
6351 __ j(not_zero, &result_not_equal);
6352
6353 // Result is EQUAL.
6354 STATIC_ASSERT(EQUAL == 0);
6355 STATIC_ASSERT(kSmiTag == 0);
6356 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6357 __ ret(0);
6358
6359 __ bind(&result_not_equal);
6360 __ j(greater, &result_greater);
6361
6362 // Result is LESS.
6363 __ Set(eax, Immediate(Smi::FromInt(LESS)));
6364 __ ret(0);
6365
6366 // Result is GREATER.
6367 __ bind(&result_greater);
6368 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
6369 __ ret(0);
6370}
6371
6372
6373void StringCompareStub::Generate(MacroAssembler* masm) {
6374 Label runtime;
6375
6376 // Stack frame on entry.
6377 // esp[0]: return address
6378 // esp[4]: right string
6379 // esp[8]: left string
6380
6381 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
6382 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
6383
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006384 NearLabel not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006385 __ cmp(edx, Operand(eax));
6386 __ j(not_equal, &not_same);
6387 STATIC_ASSERT(EQUAL == 0);
6388 STATIC_ASSERT(kSmiTag == 0);
6389 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00006390 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006391 __ ret(2 * kPointerSize);
6392
6393 __ bind(&not_same);
6394
6395 // Check that both objects are sequential ascii strings.
6396 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
6397
6398 // Compare flat ascii strings.
6399 // Drop arguments from the stack.
6400 __ pop(ecx);
6401 __ add(Operand(esp), Immediate(2 * kPointerSize));
6402 __ push(ecx);
6403 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
6404
6405 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6406 // tagged as a small integer.
6407 __ bind(&runtime);
6408 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6409}
6410
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006411
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006412void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6413 ASSERT(state_ == CompareIC::SMIS);
6414 NearLabel miss;
6415 __ mov(ecx, Operand(edx));
6416 __ or_(ecx, Operand(eax));
6417 __ test(ecx, Immediate(kSmiTagMask));
6418 __ j(not_zero, &miss, not_taken);
6419
6420 if (GetCondition() == equal) {
6421 // For equality we do not care about the sign of the result.
6422 __ sub(eax, Operand(edx));
6423 } else {
6424 NearLabel done;
6425 __ sub(edx, Operand(eax));
6426 __ j(no_overflow, &done);
6427 // Correct sign of result in case of overflow.
6428 __ not_(edx);
6429 __ bind(&done);
6430 __ mov(eax, edx);
6431 }
6432 __ ret(0);
6433
6434 __ bind(&miss);
6435 GenerateMiss(masm);
6436}
6437
6438
6439void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6440 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6441
6442 NearLabel generic_stub;
6443 NearLabel unordered;
6444 NearLabel miss;
6445 __ mov(ecx, Operand(edx));
6446 __ and_(ecx, Operand(eax));
6447 __ test(ecx, Immediate(kSmiTagMask));
6448 __ j(zero, &generic_stub, not_taken);
6449
6450 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
6451 __ j(not_equal, &miss, not_taken);
6452 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
6453 __ j(not_equal, &miss, not_taken);
6454
6455 // Inlining the double comparison and falling back to the general compare
6456 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00006457 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006458 CpuFeatures::Scope scope1(SSE2);
6459 CpuFeatures::Scope scope2(CMOV);
6460
6461 // Load left and right operand
6462 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
6463 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
6464
6465 // Compare operands
6466 __ ucomisd(xmm0, xmm1);
6467
6468 // Don't base result on EFLAGS when a NaN is involved.
6469 __ j(parity_even, &unordered, not_taken);
6470
6471 // Return a result of -1, 0, or 1, based on EFLAGS.
6472 // Performing mov, because xor would destroy the flag register.
6473 __ mov(eax, 0); // equal
6474 __ mov(ecx, Immediate(Smi::FromInt(1)));
6475 __ cmov(above, eax, Operand(ecx));
6476 __ mov(ecx, Immediate(Smi::FromInt(-1)));
6477 __ cmov(below, eax, Operand(ecx));
6478 __ ret(0);
6479
6480 __ bind(&unordered);
6481 }
6482
6483 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
6484 __ bind(&generic_stub);
6485 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
6486
6487 __ bind(&miss);
6488 GenerateMiss(masm);
6489}
6490
6491
6492void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6493 ASSERT(state_ == CompareIC::OBJECTS);
6494 NearLabel miss;
6495 __ mov(ecx, Operand(edx));
6496 __ and_(ecx, Operand(eax));
6497 __ test(ecx, Immediate(kSmiTagMask));
6498 __ j(zero, &miss, not_taken);
6499
6500 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
6501 __ j(not_equal, &miss, not_taken);
6502 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
6503 __ j(not_equal, &miss, not_taken);
6504
6505 ASSERT(GetCondition() == equal);
6506 __ sub(eax, Operand(edx));
6507 __ ret(0);
6508
6509 __ bind(&miss);
6510 GenerateMiss(masm);
6511}
6512
6513
6514void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6515 // Save the registers.
6516 __ pop(ecx);
6517 __ push(edx);
6518 __ push(eax);
6519 __ push(ecx);
6520
6521 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006522 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6523 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006524 __ EnterInternalFrame();
6525 __ push(edx);
6526 __ push(eax);
6527 __ push(Immediate(Smi::FromInt(op_)));
6528 __ CallExternalReference(miss, 3);
6529 __ LeaveInternalFrame();
6530
6531 // Compute the entry point of the rewritten stub.
6532 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6533
6534 // Restore registers.
6535 __ pop(ecx);
6536 __ pop(eax);
6537 __ pop(edx);
6538 __ push(ecx);
6539
6540 // Do a tail call to the rewritten stub.
6541 __ jmp(Operand(edi));
6542}
6543
6544
ricow@chromium.org65fae842010-08-25 15:26:24 +00006545#undef __
6546
6547} } // namespace v8::internal
6548
6549#endif // V8_TARGET_ARCH_IA32