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