Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Side by Side Diff: src/ia32/code-stubs-ia32.cc

Issue 3195022: Move code stubs from codegen*.* files to code-stub*.* files. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/ia32/code-stubs-ia32.h ('k') | src/ia32/codegen-ia32.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // 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
32 #include "bootstrapper.h"
33 #include "code-stubs-ia32.h"
34 #include "codegen-inl.h"
35 #include "regexp-macro-assembler.h"
36
37 namespace v8 {
38 namespace internal {
39
40 #define __ ACCESS_MASM(masm)
41 void FastNewClosureStub::Generate(MacroAssembler* masm) {
42 // Create a new closure from the given function info in new
43 // space. Set the context to the current context in esi.
44 Label gc;
45 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
46
47 // Get the function info from the stack.
48 __ mov(edx, Operand(esp, 1 * kPointerSize));
49
50 // Compute the function map in the current global context and set that
51 // as the map of the allocated object.
52 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
53 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
54 __ mov(ecx, Operand(ecx, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
55 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
56
57 // Initialize the rest of the function. We don't have to update the
58 // write barrier because the allocated object is in new space.
59 __ mov(ebx, Immediate(Factory::empty_fixed_array()));
60 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
61 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
62 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
63 Immediate(Factory::the_hole_value()));
64 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
65 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
66 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
67
68 // Initialize the code pointer in the function to be the one
69 // found in the shared function info object.
70 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
71 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
72 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
73
74 // Return and remove the on-stack parameter.
75 __ ret(1 * kPointerSize);
76
77 // Create a new closure through the slower runtime call.
78 __ bind(&gc);
79 __ pop(ecx); // Temporarily remove return address.
80 __ pop(edx);
81 __ push(esi);
82 __ push(edx);
83 __ push(ecx); // Restore return address.
84 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
85 }
86
87
88 void FastNewContextStub::Generate(MacroAssembler* masm) {
89 // Try to allocate the context in new space.
90 Label gc;
91 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
92 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
93 eax, ebx, ecx, &gc, TAG_OBJECT);
94
95 // Get the function from the stack.
96 __ mov(ecx, Operand(esp, 1 * kPointerSize));
97
98 // Setup the object header.
99 __ mov(FieldOperand(eax, HeapObject::kMapOffset), Factory::context_map());
100 __ mov(FieldOperand(eax, Context::kLengthOffset),
101 Immediate(Smi::FromInt(length)));
102
103 // Setup the fixed slots.
104 __ xor_(ebx, Operand(ebx)); // Set to NULL.
105 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
106 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
107 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
108 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
109
110 // Copy the global object from the surrounding context. We go through the
111 // context in the function (ecx) to match the allocation behavior we have
112 // in the runtime system (see Heap::AllocateFunctionContext).
113 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
114 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
115 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
116
117 // Initialize the rest of the slots to undefined.
118 __ mov(ebx, Factory::undefined_value());
119 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
120 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
121 }
122
123 // Return and remove the on-stack parameter.
124 __ mov(esi, Operand(eax));
125 __ ret(1 * kPointerSize);
126
127 // Need to collect. Call into runtime system.
128 __ bind(&gc);
129 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
130 }
131
132
133 void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
134 // Stack layout on entry:
135 //
136 // [esp + kPointerSize]: constant elements.
137 // [esp + (2 * kPointerSize)]: literal index.
138 // [esp + (3 * kPointerSize)]: literals array.
139
140 // All sizes here are multiples of kPointerSize.
141 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
142 int size = JSArray::kSize + elements_size;
143
144 // Load boilerplate object into ecx and check if we need to create a
145 // boilerplate.
146 Label slow_case;
147 __ mov(ecx, Operand(esp, 3 * kPointerSize));
148 __ mov(eax, Operand(esp, 2 * kPointerSize));
149 STATIC_ASSERT(kPointerSize == 4);
150 STATIC_ASSERT(kSmiTagSize == 1);
151 STATIC_ASSERT(kSmiTag == 0);
152 __ mov(ecx, CodeGenerator::FixedArrayElementOperand(ecx, eax));
153 __ cmp(ecx, Factory::undefined_value());
154 __ j(equal, &slow_case);
155
156 if (FLAG_debug_code) {
157 const char* message;
158 Handle<Map> expected_map;
159 if (mode_ == CLONE_ELEMENTS) {
160 message = "Expected (writable) fixed array";
161 expected_map = Factory::fixed_array_map();
162 } else {
163 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
164 message = "Expected copy-on-write fixed array";
165 expected_map = Factory::fixed_cow_array_map();
166 }
167 __ push(ecx);
168 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
169 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
170 __ Assert(equal, message);
171 __ pop(ecx);
172 }
173
174 // Allocate both the JS array and the elements array in one big
175 // allocation. This avoids multiple limit checks.
176 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
177
178 // Copy the JS array part.
179 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
180 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
181 __ mov(ebx, FieldOperand(ecx, i));
182 __ mov(FieldOperand(eax, i), ebx);
183 }
184 }
185
186 if (length_ > 0) {
187 // Get hold of the elements array of the boilerplate and setup the
188 // elements pointer in the resulting object.
189 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
190 __ lea(edx, Operand(eax, JSArray::kSize));
191 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
192
193 // Copy the elements array.
194 for (int i = 0; i < elements_size; i += kPointerSize) {
195 __ mov(ebx, FieldOperand(ecx, i));
196 __ mov(FieldOperand(edx, i), ebx);
197 }
198 }
199
200 // Return and remove the on-stack parameters.
201 __ ret(3 * kPointerSize);
202
203 __ bind(&slow_case);
204 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
205 }
206
207
208 // NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
209 void ToBooleanStub::Generate(MacroAssembler* masm) {
210 Label false_result, true_result, not_string;
211 __ mov(eax, Operand(esp, 1 * kPointerSize));
212
213 // 'null' => false.
214 __ cmp(eax, Factory::null_value());
215 __ j(equal, &false_result);
216
217 // Get the map and type of the heap object.
218 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
219 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
220
221 // Undetectable => false.
222 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
223 1 << Map::kIsUndetectable);
224 __ j(not_zero, &false_result);
225
226 // JavaScript object => true.
227 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
228 __ j(above_equal, &true_result);
229
230 // String value => false iff empty.
231 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
232 __ j(above_equal, &not_string);
233 STATIC_ASSERT(kSmiTag == 0);
234 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
235 __ j(zero, &false_result);
236 __ jmp(&true_result);
237
238 __ bind(&not_string);
239 // HeapNumber => false iff +0, -0, or NaN.
240 __ cmp(edx, Factory::heap_number_map());
241 __ j(not_equal, &true_result);
242 __ fldz();
243 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
244 __ FCmp();
245 __ j(zero, &false_result);
246 // Fall through to |true_result|.
247
248 // Return 1/0 for true/false in eax.
249 __ bind(&true_result);
250 __ mov(eax, 1);
251 __ ret(1 * kPointerSize);
252 __ bind(&false_result);
253 __ mov(eax, 0);
254 __ ret(1 * kPointerSize);
255 }
256
257
258 const char* GenericBinaryOpStub::GetName() {
259 if (name_ != NULL) return name_;
260 const int kMaxNameLength = 100;
261 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
262 if (name_ == NULL) return "OOM";
263 const char* op_name = Token::Name(op_);
264 const char* overwrite_name;
265 switch (mode_) {
266 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
267 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
268 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
269 default: overwrite_name = "UnknownOverwrite"; break;
270 }
271
272 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
273 "GenericBinaryOpStub_%s_%s%s_%s%s_%s_%s",
274 op_name,
275 overwrite_name,
276 (flags_ & NO_SMI_CODE_IN_STUB) ? "_NoSmiInStub" : "",
277 args_in_registers_ ? "RegArgs" : "StackArgs",
278 args_reversed_ ? "_R" : "",
279 static_operands_type_.ToString(),
280 BinaryOpIC::GetName(runtime_operands_type_));
281 return name_;
282 }
283
284
285 void GenericBinaryOpStub::GenerateCall(
286 MacroAssembler* masm,
287 Register left,
288 Register right) {
289 if (!ArgsInRegistersSupported()) {
290 // Pass arguments on the stack.
291 __ push(left);
292 __ push(right);
293 } else {
294 // The calling convention with registers is left in edx and right in eax.
295 Register left_arg = edx;
296 Register right_arg = eax;
297 if (!(left.is(left_arg) && right.is(right_arg))) {
298 if (left.is(right_arg) && right.is(left_arg)) {
299 if (IsOperationCommutative()) {
300 SetArgsReversed();
301 } else {
302 __ xchg(left, right);
303 }
304 } else if (left.is(left_arg)) {
305 __ mov(right_arg, right);
306 } else if (right.is(right_arg)) {
307 __ mov(left_arg, left);
308 } else if (left.is(right_arg)) {
309 if (IsOperationCommutative()) {
310 __ mov(left_arg, right);
311 SetArgsReversed();
312 } else {
313 // Order of moves important to avoid destroying left argument.
314 __ mov(left_arg, left);
315 __ mov(right_arg, right);
316 }
317 } else if (right.is(left_arg)) {
318 if (IsOperationCommutative()) {
319 __ mov(right_arg, left);
320 SetArgsReversed();
321 } else {
322 // Order of moves important to avoid destroying right argument.
323 __ mov(right_arg, right);
324 __ mov(left_arg, left);
325 }
326 } else {
327 // Order of moves is not important.
328 __ mov(left_arg, left);
329 __ mov(right_arg, right);
330 }
331 }
332
333 // Update flags to indicate that arguments are in registers.
334 SetArgsInRegisters();
335 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
336 }
337
338 // Call the stub.
339 __ CallStub(this);
340 }
341
342
343 void GenericBinaryOpStub::GenerateCall(
344 MacroAssembler* masm,
345 Register left,
346 Smi* right) {
347 if (!ArgsInRegistersSupported()) {
348 // Pass arguments on the stack.
349 __ push(left);
350 __ push(Immediate(right));
351 } else {
352 // The calling convention with registers is left in edx and right in eax.
353 Register left_arg = edx;
354 Register right_arg = eax;
355 if (left.is(left_arg)) {
356 __ mov(right_arg, Immediate(right));
357 } else if (left.is(right_arg) && IsOperationCommutative()) {
358 __ mov(left_arg, Immediate(right));
359 SetArgsReversed();
360 } else {
361 // For non-commutative operations, left and right_arg might be
362 // the same register. Therefore, the order of the moves is
363 // important here in order to not overwrite left before moving
364 // it to left_arg.
365 __ mov(left_arg, left);
366 __ mov(right_arg, Immediate(right));
367 }
368
369 // Update flags to indicate that arguments are in registers.
370 SetArgsInRegisters();
371 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
372 }
373
374 // Call the stub.
375 __ CallStub(this);
376 }
377
378
379 void GenericBinaryOpStub::GenerateCall(
380 MacroAssembler* masm,
381 Smi* left,
382 Register right) {
383 if (!ArgsInRegistersSupported()) {
384 // Pass arguments on the stack.
385 __ push(Immediate(left));
386 __ push(right);
387 } else {
388 // The calling convention with registers is left in edx and right in eax.
389 Register left_arg = edx;
390 Register right_arg = eax;
391 if (right.is(right_arg)) {
392 __ mov(left_arg, Immediate(left));
393 } else if (right.is(left_arg) && IsOperationCommutative()) {
394 __ mov(right_arg, Immediate(left));
395 SetArgsReversed();
396 } else {
397 // For non-commutative operations, right and left_arg might be
398 // the same register. Therefore, the order of the moves is
399 // important here in order to not overwrite right before moving
400 // it to right_arg.
401 __ mov(right_arg, right);
402 __ mov(left_arg, Immediate(left));
403 }
404 // Update flags to indicate that arguments are in registers.
405 SetArgsInRegisters();
406 __ IncrementCounter(&Counters::generic_binary_stub_calls_regs, 1);
407 }
408
409 // Call the stub.
410 __ CallStub(this);
411 }
412
413
414 class FloatingPointHelper : public AllStatic {
415 public:
416
417 enum ArgLocation {
418 ARGS_ON_STACK,
419 ARGS_IN_REGISTERS
420 };
421
422 // Code pattern for loading a floating point value. Input value must
423 // be either a smi or a heap number object (fp value). Requirements:
424 // operand in register number. Returns operand as floating point number
425 // on FPU stack.
426 static void LoadFloatOperand(MacroAssembler* masm, Register number);
427
428 // Code pattern for loading floating point values. Input values must
429 // be either smi or heap number objects (fp values). Requirements:
430 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
431 // Returns operands as floating point numbers on FPU stack.
432 static void LoadFloatOperands(MacroAssembler* masm,
433 Register scratch,
434 ArgLocation arg_location = ARGS_ON_STACK);
435
436 // Similar to LoadFloatOperand but assumes that both operands are smis.
437 // Expects operands in edx, eax.
438 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
439
440 // Test if operands are smi or number objects (fp). Requirements:
441 // operand_1 in eax, operand_2 in edx; falls through on float
442 // operands, jumps to the non_float label otherwise.
443 static void CheckFloatOperands(MacroAssembler* masm,
444 Label* non_float,
445 Register scratch);
446
447 // Takes the operands in edx and eax and loads them as integers in eax
448 // and ecx.
449 static void LoadAsIntegers(MacroAssembler* masm,
450 TypeInfo type_info,
451 bool use_sse3,
452 Label* operand_conversion_failure);
453 static void LoadNumbersAsIntegers(MacroAssembler* masm,
454 TypeInfo type_info,
455 bool use_sse3,
456 Label* operand_conversion_failure);
457 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
458 bool use_sse3,
459 Label* operand_conversion_failure);
460
461 // Test if operands are smis or heap numbers and load them
462 // into xmm0 and xmm1 if they are. Operands are in edx and eax.
463 // Leaves operands unchanged.
464 static void LoadSSE2Operands(MacroAssembler* masm);
465
466 // Test if operands are numbers (smi or HeapNumber objects), and load
467 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
468 // either operand is not a number. Operands are in edx and eax.
469 // Leaves operands unchanged.
470 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
471
472 // Similar to LoadSSE2Operands but assumes that both operands are smis.
473 // Expects operands in edx, eax.
474 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
475 };
476
477
478 void GenericBinaryOpStub::GenerateSmiCode(MacroAssembler* masm, Label* slow) {
479 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
480 // dividend in eax and edx free for the division. Use eax, ebx for those.
481 Comment load_comment(masm, "-- Load arguments");
482 Register left = edx;
483 Register right = eax;
484 if (op_ == Token::DIV || op_ == Token::MOD) {
485 left = eax;
486 right = ebx;
487 if (HasArgsInRegisters()) {
488 __ mov(ebx, eax);
489 __ mov(eax, edx);
490 }
491 }
492 if (!HasArgsInRegisters()) {
493 __ mov(right, Operand(esp, 1 * kPointerSize));
494 __ mov(left, Operand(esp, 2 * kPointerSize));
495 }
496
497 if (static_operands_type_.IsSmi()) {
498 if (FLAG_debug_code) {
499 __ AbortIfNotSmi(left);
500 __ AbortIfNotSmi(right);
501 }
502 if (op_ == Token::BIT_OR) {
503 __ or_(right, Operand(left));
504 GenerateReturn(masm);
505 return;
506 } else if (op_ == Token::BIT_AND) {
507 __ and_(right, Operand(left));
508 GenerateReturn(masm);
509 return;
510 } else if (op_ == Token::BIT_XOR) {
511 __ xor_(right, Operand(left));
512 GenerateReturn(masm);
513 return;
514 }
515 }
516
517 // 2. Prepare the smi check of both operands by oring them together.
518 Comment smi_check_comment(masm, "-- Smi check arguments");
519 Label not_smis;
520 Register combined = ecx;
521 ASSERT(!left.is(combined) && !right.is(combined));
522 switch (op_) {
523 case Token::BIT_OR:
524 // Perform the operation into eax and smi check the result. Preserve
525 // eax in case the result is not a smi.
526 ASSERT(!left.is(ecx) && !right.is(ecx));
527 __ mov(ecx, right);
528 __ or_(right, Operand(left)); // Bitwise or is commutative.
529 combined = right;
530 break;
531
532 case Token::BIT_XOR:
533 case Token::BIT_AND:
534 case Token::ADD:
535 case Token::SUB:
536 case Token::MUL:
537 case Token::DIV:
538 case Token::MOD:
539 __ mov(combined, right);
540 __ or_(combined, Operand(left));
541 break;
542
543 case Token::SHL:
544 case Token::SAR:
545 case Token::SHR:
546 // Move the right operand into ecx for the shift operation, use eax
547 // for the smi check register.
548 ASSERT(!left.is(ecx) && !right.is(ecx));
549 __ mov(ecx, right);
550 __ or_(right, Operand(left));
551 combined = right;
552 break;
553
554 default:
555 break;
556 }
557
558 // 3. Perform the smi check of the operands.
559 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
560 __ test(combined, Immediate(kSmiTagMask));
561 __ j(not_zero, &not_smis, not_taken);
562
563 // 4. Operands are both smis, perform the operation leaving the result in
564 // eax and check the result if necessary.
565 Comment perform_smi(masm, "-- Perform smi operation");
566 Label use_fp_on_smis;
567 switch (op_) {
568 case Token::BIT_OR:
569 // Nothing to do.
570 break;
571
572 case Token::BIT_XOR:
573 ASSERT(right.is(eax));
574 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
575 break;
576
577 case Token::BIT_AND:
578 ASSERT(right.is(eax));
579 __ and_(right, Operand(left)); // Bitwise and is commutative.
580 break;
581
582 case Token::SHL:
583 // Remove tags from operands (but keep sign).
584 __ SmiUntag(left);
585 __ SmiUntag(ecx);
586 // Perform the operation.
587 __ shl_cl(left);
588 // Check that the *signed* result fits in a smi.
589 __ cmp(left, 0xc0000000);
590 __ j(sign, &use_fp_on_smis, not_taken);
591 // Tag the result and store it in register eax.
592 __ SmiTag(left);
593 __ mov(eax, left);
594 break;
595
596 case Token::SAR:
597 // Remove tags from operands (but keep sign).
598 __ SmiUntag(left);
599 __ SmiUntag(ecx);
600 // Perform the operation.
601 __ sar_cl(left);
602 // Tag the result and store it in register eax.
603 __ SmiTag(left);
604 __ mov(eax, left);
605 break;
606
607 case Token::SHR:
608 // Remove tags from operands (but keep sign).
609 __ SmiUntag(left);
610 __ SmiUntag(ecx);
611 // Perform the operation.
612 __ shr_cl(left);
613 // Check that the *unsigned* result fits in a smi.
614 // Neither of the two high-order bits can be set:
615 // - 0x80000000: high bit would be lost when smi tagging.
616 // - 0x40000000: this number would convert to negative when
617 // Smi tagging these two cases can only happen with shifts
618 // by 0 or 1 when handed a valid smi.
619 __ test(left, Immediate(0xc0000000));
620 __ j(not_zero, slow, not_taken);
621 // Tag the result and store it in register eax.
622 __ SmiTag(left);
623 __ mov(eax, left);
624 break;
625
626 case Token::ADD:
627 ASSERT(right.is(eax));
628 __ add(right, Operand(left)); // Addition is commutative.
629 __ j(overflow, &use_fp_on_smis, not_taken);
630 break;
631
632 case Token::SUB:
633 __ sub(left, Operand(right));
634 __ j(overflow, &use_fp_on_smis, not_taken);
635 __ mov(eax, left);
636 break;
637
638 case Token::MUL:
639 // If the smi tag is 0 we can just leave the tag on one operand.
640 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
641 // We can't revert the multiplication if the result is not a smi
642 // so save the right operand.
643 __ mov(ebx, right);
644 // Remove tag from one of the operands (but keep sign).
645 __ SmiUntag(right);
646 // Do multiplication.
647 __ imul(right, Operand(left)); // Multiplication is commutative.
648 __ j(overflow, &use_fp_on_smis, not_taken);
649 // Check for negative zero result. Use combined = left | right.
650 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
651 break;
652
653 case Token::DIV:
654 // We can't revert the division if the result is not a smi so
655 // save the left operand.
656 __ mov(edi, left);
657 // Check for 0 divisor.
658 __ test(right, Operand(right));
659 __ j(zero, &use_fp_on_smis, not_taken);
660 // Sign extend left into edx:eax.
661 ASSERT(left.is(eax));
662 __ cdq();
663 // Divide edx:eax by right.
664 __ idiv(right);
665 // Check for the corner case of dividing the most negative smi by
666 // -1. We cannot use the overflow flag, since it is not set by idiv
667 // instruction.
668 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
669 __ cmp(eax, 0x40000000);
670 __ j(equal, &use_fp_on_smis);
671 // Check for negative zero result. Use combined = left | right.
672 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
673 // Check that the remainder is zero.
674 __ test(edx, Operand(edx));
675 __ j(not_zero, &use_fp_on_smis);
676 // Tag the result and store it in register eax.
677 __ SmiTag(eax);
678 break;
679
680 case Token::MOD:
681 // Check for 0 divisor.
682 __ test(right, Operand(right));
683 __ j(zero, &not_smis, not_taken);
684
685 // Sign extend left into edx:eax.
686 ASSERT(left.is(eax));
687 __ cdq();
688 // Divide edx:eax by right.
689 __ idiv(right);
690 // Check for negative zero result. Use combined = left | right.
691 __ NegativeZeroTest(edx, combined, slow);
692 // Move remainder to register eax.
693 __ mov(eax, edx);
694 break;
695
696 default:
697 UNREACHABLE();
698 }
699
700 // 5. Emit return of result in eax.
701 GenerateReturn(masm);
702
703 // 6. For some operations emit inline code to perform floating point
704 // operations on known smis (e.g., if the result of the operation
705 // overflowed the smi range).
706 switch (op_) {
707 case Token::SHL: {
708 Comment perform_float(masm, "-- Perform float operation on smis");
709 __ bind(&use_fp_on_smis);
710 // Result we want is in left == edx, so we can put the allocated heap
711 // number in eax.
712 __ AllocateHeapNumber(eax, ecx, ebx, slow);
713 // Store the result in the HeapNumber and return.
714 if (CpuFeatures::IsSupported(SSE2)) {
715 CpuFeatures::Scope use_sse2(SSE2);
716 __ cvtsi2sd(xmm0, Operand(left));
717 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
718 } else {
719 // It's OK to overwrite the right argument on the stack because we
720 // are about to return.
721 __ mov(Operand(esp, 1 * kPointerSize), left);
722 __ fild_s(Operand(esp, 1 * kPointerSize));
723 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
724 }
725 GenerateReturn(masm);
726 break;
727 }
728
729 case Token::ADD:
730 case Token::SUB:
731 case Token::MUL:
732 case Token::DIV: {
733 Comment perform_float(masm, "-- Perform float operation on smis");
734 __ bind(&use_fp_on_smis);
735 // Restore arguments to edx, eax.
736 switch (op_) {
737 case Token::ADD:
738 // Revert right = right + left.
739 __ sub(right, Operand(left));
740 break;
741 case Token::SUB:
742 // Revert left = left - right.
743 __ add(left, Operand(right));
744 break;
745 case Token::MUL:
746 // Right was clobbered but a copy is in ebx.
747 __ mov(right, ebx);
748 break;
749 case Token::DIV:
750 // Left was clobbered but a copy is in edi. Right is in ebx for
751 // division.
752 __ mov(edx, edi);
753 __ mov(eax, right);
754 break;
755 default: UNREACHABLE();
756 break;
757 }
758 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
759 if (CpuFeatures::IsSupported(SSE2)) {
760 CpuFeatures::Scope use_sse2(SSE2);
761 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
762 switch (op_) {
763 case Token::ADD: __ addsd(xmm0, xmm1); break;
764 case Token::SUB: __ subsd(xmm0, xmm1); break;
765 case Token::MUL: __ mulsd(xmm0, xmm1); break;
766 case Token::DIV: __ divsd(xmm0, xmm1); break;
767 default: UNREACHABLE();
768 }
769 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
770 } else { // SSE2 not available, use FPU.
771 FloatingPointHelper::LoadFloatSmis(masm, ebx);
772 switch (op_) {
773 case Token::ADD: __ faddp(1); break;
774 case Token::SUB: __ fsubp(1); break;
775 case Token::MUL: __ fmulp(1); break;
776 case Token::DIV: __ fdivp(1); break;
777 default: UNREACHABLE();
778 }
779 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
780 }
781 __ mov(eax, ecx);
782 GenerateReturn(masm);
783 break;
784 }
785
786 default:
787 break;
788 }
789
790 // 7. Non-smi operands, fall out to the non-smi code with the operands in
791 // edx and eax.
792 Comment done_comment(masm, "-- Enter non-smi code");
793 __ bind(&not_smis);
794 switch (op_) {
795 case Token::BIT_OR:
796 case Token::SHL:
797 case Token::SAR:
798 case Token::SHR:
799 // Right operand is saved in ecx and eax was destroyed by the smi
800 // check.
801 __ mov(eax, ecx);
802 break;
803
804 case Token::DIV:
805 case Token::MOD:
806 // Operands are in eax, ebx at this point.
807 __ mov(edx, eax);
808 __ mov(eax, ebx);
809 break;
810
811 default:
812 break;
813 }
814 }
815
816
817 void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
818 Label call_runtime;
819
820 __ IncrementCounter(&Counters::generic_binary_stub_calls, 1);
821
822 // Generate fast case smi code if requested. This flag is set when the fast
823 // case smi code is not generated by the caller. Generating it here will speed
824 // up common operations.
825 if (ShouldGenerateSmiCode()) {
826 GenerateSmiCode(masm, &call_runtime);
827 } else if (op_ != Token::MOD) { // MOD goes straight to runtime.
828 if (!HasArgsInRegisters()) {
829 GenerateLoadArguments(masm);
830 }
831 }
832
833 // Floating point case.
834 if (ShouldGenerateFPCode()) {
835 switch (op_) {
836 case Token::ADD:
837 case Token::SUB:
838 case Token::MUL:
839 case Token::DIV: {
840 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
841 HasSmiCodeInStub()) {
842 // Execution reaches this point when the first non-smi argument occurs
843 // (and only if smi code is generated). This is the right moment to
844 // patch to HEAP_NUMBERS state. The transition is attempted only for
845 // the four basic operations. The stub stays in the DEFAULT state
846 // forever for all other operations (also if smi code is skipped).
847 GenerateTypeTransition(masm);
848 break;
849 }
850
851 Label not_floats;
852 if (CpuFeatures::IsSupported(SSE2)) {
853 CpuFeatures::Scope use_sse2(SSE2);
854 if (static_operands_type_.IsNumber()) {
855 if (FLAG_debug_code) {
856 // Assert at runtime that inputs are only numbers.
857 __ AbortIfNotNumber(edx);
858 __ AbortIfNotNumber(eax);
859 }
860 if (static_operands_type_.IsSmi()) {
861 if (FLAG_debug_code) {
862 __ AbortIfNotSmi(edx);
863 __ AbortIfNotSmi(eax);
864 }
865 FloatingPointHelper::LoadSSE2Smis(masm, ecx);
866 } else {
867 FloatingPointHelper::LoadSSE2Operands(masm);
868 }
869 } else {
870 FloatingPointHelper::LoadSSE2Operands(masm, &call_runtime);
871 }
872
873 switch (op_) {
874 case Token::ADD: __ addsd(xmm0, xmm1); break;
875 case Token::SUB: __ subsd(xmm0, xmm1); break;
876 case Token::MUL: __ mulsd(xmm0, xmm1); break;
877 case Token::DIV: __ divsd(xmm0, xmm1); break;
878 default: UNREACHABLE();
879 }
880 GenerateHeapResultAllocation(masm, &call_runtime);
881 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
882 GenerateReturn(masm);
883 } else { // SSE2 not available, use FPU.
884 if (static_operands_type_.IsNumber()) {
885 if (FLAG_debug_code) {
886 // Assert at runtime that inputs are only numbers.
887 __ AbortIfNotNumber(edx);
888 __ AbortIfNotNumber(eax);
889 }
890 } else {
891 FloatingPointHelper::CheckFloatOperands(masm, &call_runtime, ebx);
892 }
893 FloatingPointHelper::LoadFloatOperands(
894 masm,
895 ecx,
896 FloatingPointHelper::ARGS_IN_REGISTERS);
897 switch (op_) {
898 case Token::ADD: __ faddp(1); break;
899 case Token::SUB: __ fsubp(1); break;
900 case Token::MUL: __ fmulp(1); break;
901 case Token::DIV: __ fdivp(1); break;
902 default: UNREACHABLE();
903 }
904 Label after_alloc_failure;
905 GenerateHeapResultAllocation(masm, &after_alloc_failure);
906 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
907 GenerateReturn(masm);
908 __ bind(&after_alloc_failure);
909 __ ffree();
910 __ jmp(&call_runtime);
911 }
912 __ bind(&not_floats);
913 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
914 !HasSmiCodeInStub()) {
915 // Execution reaches this point when the first non-number argument
916 // occurs (and only if smi code is skipped from the stub, otherwise
917 // the patching has already been done earlier in this case branch).
918 // Try patching to STRINGS for ADD operation.
919 if (op_ == Token::ADD) {
920 GenerateTypeTransition(masm);
921 }
922 }
923 break;
924 }
925 case Token::MOD: {
926 // For MOD we go directly to runtime in the non-smi case.
927 break;
928 }
929 case Token::BIT_OR:
930 case Token::BIT_AND:
931 case Token::BIT_XOR:
932 case Token::SAR:
933 case Token::SHL:
934 case Token::SHR: {
935 Label non_smi_result;
936 FloatingPointHelper::LoadAsIntegers(masm,
937 static_operands_type_,
938 use_sse3_,
939 &call_runtime);
940 switch (op_) {
941 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
942 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
943 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
944 case Token::SAR: __ sar_cl(eax); break;
945 case Token::SHL: __ shl_cl(eax); break;
946 case Token::SHR: __ shr_cl(eax); break;
947 default: UNREACHABLE();
948 }
949 if (op_ == Token::SHR) {
950 // Check if result is non-negative and fits in a smi.
951 __ test(eax, Immediate(0xc0000000));
952 __ j(not_zero, &call_runtime);
953 } else {
954 // Check if result fits in a smi.
955 __ cmp(eax, 0xc0000000);
956 __ j(negative, &non_smi_result);
957 }
958 // Tag smi result and return.
959 __ SmiTag(eax);
960 GenerateReturn(masm);
961
962 // All ops except SHR return a signed int32 that we load in
963 // a HeapNumber.
964 if (op_ != Token::SHR) {
965 __ bind(&non_smi_result);
966 // Allocate a heap number if needed.
967 __ mov(ebx, Operand(eax)); // ebx: result
968 Label skip_allocation;
969 switch (mode_) {
970 case OVERWRITE_LEFT:
971 case OVERWRITE_RIGHT:
972 // If the operand was an object, we skip the
973 // allocation of a heap number.
974 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
975 1 * kPointerSize : 2 * kPointerSize));
976 __ test(eax, Immediate(kSmiTagMask));
977 __ j(not_zero, &skip_allocation, not_taken);
978 // Fall through!
979 case NO_OVERWRITE:
980 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
981 __ bind(&skip_allocation);
982 break;
983 default: UNREACHABLE();
984 }
985 // Store the result in the HeapNumber and return.
986 if (CpuFeatures::IsSupported(SSE2)) {
987 CpuFeatures::Scope use_sse2(SSE2);
988 __ cvtsi2sd(xmm0, Operand(ebx));
989 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
990 } else {
991 __ mov(Operand(esp, 1 * kPointerSize), ebx);
992 __ fild_s(Operand(esp, 1 * kPointerSize));
993 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
994 }
995 GenerateReturn(masm);
996 }
997 break;
998 }
999 default: UNREACHABLE(); break;
1000 }
1001 }
1002
1003 // If all else fails, use the runtime system to get the correct
1004 // result. If arguments was passed in registers now place them on the
1005 // stack in the correct order below the return address.
1006 __ bind(&call_runtime);
1007 if (HasArgsInRegisters()) {
1008 GenerateRegisterArgsPush(masm);
1009 }
1010
1011 switch (op_) {
1012 case Token::ADD: {
1013 // Test for string arguments before calling runtime.
1014 Label not_strings, not_string1, string1, string1_smi2;
1015
1016 // If this stub has already generated FP-specific code then the arguments
1017 // are already in edx, eax
1018 if (!ShouldGenerateFPCode() && !HasArgsInRegisters()) {
1019 GenerateLoadArguments(masm);
1020 }
1021
1022 // Registers containing left and right operands respectively.
1023 Register lhs, rhs;
1024 if (HasArgsReversed()) {
1025 lhs = eax;
1026 rhs = edx;
1027 } else {
1028 lhs = edx;
1029 rhs = eax;
1030 }
1031
1032 // Test if first argument is a string.
1033 __ test(lhs, Immediate(kSmiTagMask));
1034 __ j(zero, &not_string1);
1035 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, ecx);
1036 __ j(above_equal, &not_string1);
1037
1038 // First argument is a string, test second.
1039 __ test(rhs, Immediate(kSmiTagMask));
1040 __ j(zero, &string1_smi2);
1041 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
1042 __ j(above_equal, &string1);
1043
1044 // First and second argument are strings. Jump to the string add stub.
1045 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1046 __ TailCallStub(&string_add_stub);
1047
1048 __ bind(&string1_smi2);
1049 // First argument is a string, second is a smi. Try to lookup the number
1050 // string for the smi in the number string cache.
1051 NumberToStringStub::GenerateLookupNumberStringCache(
1052 masm, rhs, edi, ebx, ecx, true, &string1);
1053
1054 // Replace second argument on stack and tailcall string add stub to make
1055 // the result.
1056 __ mov(Operand(esp, 1 * kPointerSize), edi);
1057 __ TailCallStub(&string_add_stub);
1058
1059 // Only first argument is a string.
1060 __ bind(&string1);
1061 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_FUNCTION);
1062
1063 // First argument was not a string, test second.
1064 __ bind(&not_string1);
1065 __ test(rhs, Immediate(kSmiTagMask));
1066 __ j(zero, &not_strings);
1067 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
1068 __ j(above_equal, &not_strings);
1069
1070 // Only second argument is a string.
1071 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_FUNCTION);
1072
1073 __ bind(&not_strings);
1074 // Neither argument is a string.
1075 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1076 break;
1077 }
1078 case Token::SUB:
1079 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1080 break;
1081 case Token::MUL:
1082 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1083 break;
1084 case Token::DIV:
1085 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1086 break;
1087 case Token::MOD:
1088 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1089 break;
1090 case Token::BIT_OR:
1091 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1092 break;
1093 case Token::BIT_AND:
1094 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1095 break;
1096 case Token::BIT_XOR:
1097 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1098 break;
1099 case Token::SAR:
1100 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1101 break;
1102 case Token::SHL:
1103 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1104 break;
1105 case Token::SHR:
1106 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1107 break;
1108 default:
1109 UNREACHABLE();
1110 }
1111 }
1112
1113
1114 void GenericBinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
1115 Label* alloc_failure) {
1116 Label skip_allocation;
1117 OverwriteMode mode = mode_;
1118 if (HasArgsReversed()) {
1119 if (mode == OVERWRITE_RIGHT) {
1120 mode = OVERWRITE_LEFT;
1121 } else if (mode == OVERWRITE_LEFT) {
1122 mode = OVERWRITE_RIGHT;
1123 }
1124 }
1125 switch (mode) {
1126 case OVERWRITE_LEFT: {
1127 // If the argument in edx is already an object, we skip the
1128 // allocation of a heap number.
1129 __ test(edx, Immediate(kSmiTagMask));
1130 __ j(not_zero, &skip_allocation, not_taken);
1131 // Allocate a heap number for the result. Keep eax and edx intact
1132 // for the possible runtime call.
1133 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1134 // Now edx can be overwritten losing one of the arguments as we are
1135 // now done and will not need it any more.
1136 __ mov(edx, Operand(ebx));
1137 __ bind(&skip_allocation);
1138 // Use object in edx as a result holder
1139 __ mov(eax, Operand(edx));
1140 break;
1141 }
1142 case OVERWRITE_RIGHT:
1143 // If the argument in eax is already an object, we skip the
1144 // allocation of a heap number.
1145 __ test(eax, Immediate(kSmiTagMask));
1146 __ j(not_zero, &skip_allocation, not_taken);
1147 // Fall through!
1148 case NO_OVERWRITE:
1149 // Allocate a heap number for the result. Keep eax and edx intact
1150 // for the possible runtime call.
1151 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1152 // Now eax can be overwritten losing one of the arguments as we are
1153 // now done and will not need it any more.
1154 __ mov(eax, ebx);
1155 __ bind(&skip_allocation);
1156 break;
1157 default: UNREACHABLE();
1158 }
1159 }
1160
1161
1162 void GenericBinaryOpStub::GenerateLoadArguments(MacroAssembler* masm) {
1163 // If arguments are not passed in registers read them from the stack.
1164 ASSERT(!HasArgsInRegisters());
1165 __ mov(eax, Operand(esp, 1 * kPointerSize));
1166 __ mov(edx, Operand(esp, 2 * kPointerSize));
1167 }
1168
1169
1170 void GenericBinaryOpStub::GenerateReturn(MacroAssembler* masm) {
1171 // If arguments are not passed in registers remove them from the stack before
1172 // returning.
1173 if (!HasArgsInRegisters()) {
1174 __ ret(2 * kPointerSize); // Remove both operands
1175 } else {
1176 __ ret(0);
1177 }
1178 }
1179
1180
1181 void GenericBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
1182 ASSERT(HasArgsInRegisters());
1183 __ pop(ecx);
1184 if (HasArgsReversed()) {
1185 __ push(eax);
1186 __ push(edx);
1187 } else {
1188 __ push(edx);
1189 __ push(eax);
1190 }
1191 __ push(ecx);
1192 }
1193
1194
1195 void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1196 // Ensure the operands are on the stack.
1197 if (HasArgsInRegisters()) {
1198 GenerateRegisterArgsPush(masm);
1199 }
1200
1201 __ pop(ecx); // Save return address.
1202
1203 // Left and right arguments are now on top.
1204 // Push this stub's key. Although the operation and the type info are
1205 // encoded into the key, the encoding is opaque, so push them too.
1206 __ push(Immediate(Smi::FromInt(MinorKey())));
1207 __ push(Immediate(Smi::FromInt(op_)));
1208 __ push(Immediate(Smi::FromInt(runtime_operands_type_)));
1209
1210 __ push(ecx); // Push return address.
1211
1212 // Patch the caller to an appropriate specialized stub and return the
1213 // operation result to the caller of the stub.
1214 __ TailCallExternalReference(
1215 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
1216 5,
1217 1);
1218 }
1219
1220
1221 Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
1222 GenericBinaryOpStub stub(key, type_info);
1223 return stub.GetCode();
1224 }
1225
1226
1227 void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
1228 // Input on stack:
1229 // esp[4]: argument (should be number).
1230 // esp[0]: return address.
1231 // Test that eax is a number.
1232 Label runtime_call;
1233 Label runtime_call_clear_stack;
1234 Label input_not_smi;
1235 Label loaded;
1236 __ mov(eax, Operand(esp, kPointerSize));
1237 __ test(eax, Immediate(kSmiTagMask));
1238 __ j(not_zero, &input_not_smi);
1239 // Input is a smi. Untag and load it onto the FPU stack.
1240 // Then load the low and high words of the double into ebx, edx.
1241 STATIC_ASSERT(kSmiTagSize == 1);
1242 __ sar(eax, 1);
1243 __ sub(Operand(esp), Immediate(2 * kPointerSize));
1244 __ mov(Operand(esp, 0), eax);
1245 __ fild_s(Operand(esp, 0));
1246 __ fst_d(Operand(esp, 0));
1247 __ pop(edx);
1248 __ pop(ebx);
1249 __ jmp(&loaded);
1250 __ bind(&input_not_smi);
1251 // Check if input is a HeapNumber.
1252 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1253 __ cmp(Operand(ebx), Immediate(Factory::heap_number_map()));
1254 __ j(not_equal, &runtime_call);
1255 // Input is a HeapNumber. Push it on the FPU stack and load its
1256 // low and high words into ebx, edx.
1257 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
1258 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
1259 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
1260
1261 __ bind(&loaded);
1262 // ST[0] == double value
1263 // ebx = low 32 bits of double value
1264 // edx = high 32 bits of double value
1265 // Compute hash (the shifts are arithmetic):
1266 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
1267 __ mov(ecx, ebx);
1268 __ xor_(ecx, Operand(edx));
1269 __ mov(eax, ecx);
1270 __ sar(eax, 16);
1271 __ xor_(ecx, Operand(eax));
1272 __ mov(eax, ecx);
1273 __ sar(eax, 8);
1274 __ xor_(ecx, Operand(eax));
1275 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
1276 __ and_(Operand(ecx), Immediate(TranscendentalCache::kCacheSize - 1));
1277
1278 // ST[0] == double value.
1279 // ebx = low 32 bits of double value.
1280 // edx = high 32 bits of double value.
1281 // ecx = TranscendentalCache::hash(double value).
1282 __ mov(eax,
1283 Immediate(ExternalReference::transcendental_cache_array_address()));
1284 // Eax points to cache array.
1285 __ mov(eax, Operand(eax, type_ * sizeof(TranscendentalCache::caches_[0])));
1286 // Eax points to the cache for the type type_.
1287 // If NULL, the cache hasn't been initialized yet, so go through runtime.
1288 __ test(eax, Operand(eax));
1289 __ j(zero, &runtime_call_clear_stack);
1290 #ifdef DEBUG
1291 // Check that the layout of cache elements match expectations.
1292 { TranscendentalCache::Element test_elem[2];
1293 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
1294 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
1295 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
1296 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
1297 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
1298 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
1299 CHECK_EQ(0, elem_in0 - elem_start);
1300 CHECK_EQ(kIntSize, elem_in1 - elem_start);
1301 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
1302 }
1303 #endif
1304 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
1305 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
1306 __ lea(ecx, Operand(eax, ecx, times_4, 0));
1307 // Check if cache matches: Double value is stored in uint32_t[2] array.
1308 Label cache_miss;
1309 __ cmp(ebx, Operand(ecx, 0));
1310 __ j(not_equal, &cache_miss);
1311 __ cmp(edx, Operand(ecx, kIntSize));
1312 __ j(not_equal, &cache_miss);
1313 // Cache hit!
1314 __ mov(eax, Operand(ecx, 2 * kIntSize));
1315 __ fstp(0);
1316 __ ret(kPointerSize);
1317
1318 __ bind(&cache_miss);
1319 // Update cache with new value.
1320 // We are short on registers, so use no_reg as scratch.
1321 // This gives slightly larger code.
1322 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
1323 GenerateOperation(masm);
1324 __ mov(Operand(ecx, 0), ebx);
1325 __ mov(Operand(ecx, kIntSize), edx);
1326 __ mov(Operand(ecx, 2 * kIntSize), eax);
1327 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1328 __ ret(kPointerSize);
1329
1330 __ bind(&runtime_call_clear_stack);
1331 __ fstp(0);
1332 __ bind(&runtime_call);
1333 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
1334 }
1335
1336
1337 Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
1338 switch (type_) {
1339 // Add more cases when necessary.
1340 case TranscendentalCache::SIN: return Runtime::kMath_sin;
1341 case TranscendentalCache::COS: return Runtime::kMath_cos;
1342 default:
1343 UNIMPLEMENTED();
1344 return Runtime::kAbort;
1345 }
1346 }
1347
1348
1349 void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
1350 // Only free register is edi.
1351 Label done;
1352 ASSERT(type_ == TranscendentalCache::SIN ||
1353 type_ == TranscendentalCache::COS);
1354 // More transcendental types can be added later.
1355
1356 // Both fsin and fcos require arguments in the range +/-2^63 and
1357 // return NaN for infinities and NaN. They can share all code except
1358 // the actual fsin/fcos operation.
1359 Label in_range;
1360 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
1361 // work. We must reduce it to the appropriate range.
1362 __ mov(edi, edx);
1363 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
1364 int supported_exponent_limit =
1365 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
1366 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
1367 __ j(below, &in_range, taken);
1368 // Check for infinity and NaN. Both return NaN for sin.
1369 __ cmp(Operand(edi), Immediate(0x7ff00000));
1370 Label non_nan_result;
1371 __ j(not_equal, &non_nan_result, taken);
1372 // Input is +/-Infinity or NaN. Result is NaN.
1373 __ fstp(0);
1374 // NaN is represented by 0x7ff8000000000000.
1375 __ push(Immediate(0x7ff80000));
1376 __ push(Immediate(0));
1377 __ fld_d(Operand(esp, 0));
1378 __ add(Operand(esp), Immediate(2 * kPointerSize));
1379 __ jmp(&done);
1380
1381 __ bind(&non_nan_result);
1382
1383 // Use fpmod to restrict argument to the range +/-2*PI.
1384 __ mov(edi, eax); // Save eax before using fnstsw_ax.
1385 __ fldpi();
1386 __ fadd(0);
1387 __ fld(1);
1388 // FPU Stack: input, 2*pi, input.
1389 {
1390 Label no_exceptions;
1391 __ fwait();
1392 __ fnstsw_ax();
1393 // Clear if Illegal Operand or Zero Division exceptions are set.
1394 __ test(Operand(eax), Immediate(5));
1395 __ j(zero, &no_exceptions);
1396 __ fnclex();
1397 __ bind(&no_exceptions);
1398 }
1399
1400 // Compute st(0) % st(1)
1401 {
1402 Label partial_remainder_loop;
1403 __ bind(&partial_remainder_loop);
1404 __ fprem1();
1405 __ fwait();
1406 __ fnstsw_ax();
1407 __ test(Operand(eax), Immediate(0x400 /* C2 */));
1408 // If C2 is set, computation only has partial result. Loop to
1409 // continue computation.
1410 __ j(not_zero, &partial_remainder_loop);
1411 }
1412 // FPU Stack: input, 2*pi, input % 2*pi
1413 __ fstp(2);
1414 __ fstp(0);
1415 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
1416
1417 // FPU Stack: input % 2*pi
1418 __ bind(&in_range);
1419 switch (type_) {
1420 case TranscendentalCache::SIN:
1421 __ fsin();
1422 break;
1423 case TranscendentalCache::COS:
1424 __ fcos();
1425 break;
1426 default:
1427 UNREACHABLE();
1428 }
1429 __ bind(&done);
1430 }
1431
1432
1433 // Get the integer part of a heap number. Surprisingly, all this bit twiddling
1434 // is faster than using the built-in instructions on floating point registers.
1435 // Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
1436 // trashed registers.
1437 void IntegerConvert(MacroAssembler* masm,
1438 Register source,
1439 TypeInfo type_info,
1440 bool use_sse3,
1441 Label* conversion_failure) {
1442 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
1443 Label done, right_exponent, normal_exponent;
1444 Register scratch = ebx;
1445 Register scratch2 = edi;
1446 if (type_info.IsInteger32() && CpuFeatures::IsEnabled(SSE2)) {
1447 CpuFeatures::Scope scope(SSE2);
1448 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
1449 return;
1450 }
1451 if (!type_info.IsInteger32() || !use_sse3) {
1452 // Get exponent word.
1453 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
1454 // Get exponent alone in scratch2.
1455 __ mov(scratch2, scratch);
1456 __ and_(scratch2, HeapNumber::kExponentMask);
1457 }
1458 if (use_sse3) {
1459 CpuFeatures::Scope scope(SSE3);
1460 if (!type_info.IsInteger32()) {
1461 // Check whether the exponent is too big for a 64 bit signed integer.
1462 static const uint32_t kTooBigExponent =
1463 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
1464 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
1465 __ j(greater_equal, conversion_failure);
1466 }
1467 // Load x87 register with heap number.
1468 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
1469 // Reserve space for 64 bit answer.
1470 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
1471 // Do conversion, which cannot fail because we checked the exponent.
1472 __ fisttp_d(Operand(esp, 0));
1473 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
1474 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
1475 } else {
1476 // Load ecx with zero. We use this either for the final shift or
1477 // for the answer.
1478 __ xor_(ecx, Operand(ecx));
1479 // Check whether the exponent matches a 32 bit signed int that cannot be
1480 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
1481 // exponent is 30 (biased). This is the exponent that we are fastest at and
1482 // also the highest exponent we can handle here.
1483 const uint32_t non_smi_exponent =
1484 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
1485 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
1486 // If we have a match of the int32-but-not-Smi exponent then skip some
1487 // logic.
1488 __ j(equal, &right_exponent);
1489 // If the exponent is higher than that then go to slow case. This catches
1490 // numbers that don't fit in a signed int32, infinities and NaNs.
1491 __ j(less, &normal_exponent);
1492
1493 {
1494 // Handle a big exponent. The only reason we have this code is that the
1495 // >>> operator has a tendency to generate numbers with an exponent of 31.
1496 const uint32_t big_non_smi_exponent =
1497 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
1498 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
1499 __ j(not_equal, conversion_failure);
1500 // We have the big exponent, typically from >>>. This means the number is
1501 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
1502 __ mov(scratch2, scratch);
1503 __ and_(scratch2, HeapNumber::kMantissaMask);
1504 // Put back the implicit 1.
1505 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
1506 // Shift up the mantissa bits to take up the space the exponent used to
1507 // take. We just orred in the implicit bit so that took care of one and
1508 // we want to use the full unsigned range so we subtract 1 bit from the
1509 // shift distance.
1510 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
1511 __ shl(scratch2, big_shift_distance);
1512 // Get the second half of the double.
1513 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
1514 // Shift down 21 bits to get the most significant 11 bits or the low
1515 // mantissa word.
1516 __ shr(ecx, 32 - big_shift_distance);
1517 __ or_(ecx, Operand(scratch2));
1518 // We have the answer in ecx, but we may need to negate it.
1519 __ test(scratch, Operand(scratch));
1520 __ j(positive, &done);
1521 __ neg(ecx);
1522 __ jmp(&done);
1523 }
1524
1525 __ bind(&normal_exponent);
1526 // Exponent word in scratch, exponent part of exponent word in scratch2.
1527 // Zero in ecx.
1528 // We know the exponent is smaller than 30 (biased). If it is less than
1529 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1530 // it rounds to zero.
1531 const uint32_t zero_exponent =
1532 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
1533 __ sub(Operand(scratch2), Immediate(zero_exponent));
1534 // ecx already has a Smi zero.
1535 __ j(less, &done);
1536
1537 // We have a shifted exponent between 0 and 30 in scratch2.
1538 __ shr(scratch2, HeapNumber::kExponentShift);
1539 __ mov(ecx, Immediate(30));
1540 __ sub(ecx, Operand(scratch2));
1541
1542 __ bind(&right_exponent);
1543 // Here ecx is the shift, scratch is the exponent word.
1544 // Get the top bits of the mantissa.
1545 __ and_(scratch, HeapNumber::kMantissaMask);
1546 // Put back the implicit 1.
1547 __ or_(scratch, 1 << HeapNumber::kExponentShift);
1548 // Shift up the mantissa bits to take up the space the exponent used to
1549 // take. We have kExponentShift + 1 significant bits int he low end of the
1550 // word. Shift them to the top bits.
1551 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1552 __ shl(scratch, shift_distance);
1553 // Get the second half of the double. For some exponents we don't
1554 // actually need this because the bits get shifted out again, but
1555 // it's probably slower to test than just to do it.
1556 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
1557 // Shift down 22 bits to get the most significant 10 bits or the low
1558 // mantissa word.
1559 __ shr(scratch2, 32 - shift_distance);
1560 __ or_(scratch2, Operand(scratch));
1561 // Move down according to the exponent.
1562 __ shr_cl(scratch2);
1563 // Now the unsigned answer is in scratch2. We need to move it to ecx and
1564 // we may need to fix the sign.
1565 Label negative;
1566 __ xor_(ecx, Operand(ecx));
1567 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
1568 __ j(greater, &negative);
1569 __ mov(ecx, scratch2);
1570 __ jmp(&done);
1571 __ bind(&negative);
1572 __ sub(ecx, Operand(scratch2));
1573 __ bind(&done);
1574 }
1575 }
1576
1577
1578 // Input: edx, eax are the left and right objects of a bit op.
1579 // Output: eax, ecx are left and right integers for a bit op.
1580 void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
1581 TypeInfo type_info,
1582 bool use_sse3,
1583 Label* conversion_failure) {
1584 // Check float operands.
1585 Label arg1_is_object, check_undefined_arg1;
1586 Label arg2_is_object, check_undefined_arg2;
1587 Label load_arg2, done;
1588
1589 if (!type_info.IsDouble()) {
1590 if (!type_info.IsSmi()) {
1591 __ test(edx, Immediate(kSmiTagMask));
1592 __ j(not_zero, &arg1_is_object);
1593 } else {
1594 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
1595 }
1596 __ SmiUntag(edx);
1597 __ jmp(&load_arg2);
1598 }
1599
1600 __ bind(&arg1_is_object);
1601
1602 // Get the untagged integer version of the edx heap number in ecx.
1603 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
1604 __ mov(edx, ecx);
1605
1606 // Here edx has the untagged integer, eax has a Smi or a heap number.
1607 __ bind(&load_arg2);
1608 if (!type_info.IsDouble()) {
1609 // Test if arg2 is a Smi.
1610 if (!type_info.IsSmi()) {
1611 __ test(eax, Immediate(kSmiTagMask));
1612 __ j(not_zero, &arg2_is_object);
1613 } else {
1614 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
1615 }
1616 __ SmiUntag(eax);
1617 __ mov(ecx, eax);
1618 __ jmp(&done);
1619 }
1620
1621 __ bind(&arg2_is_object);
1622
1623 // Get the untagged integer version of the eax heap number in ecx.
1624 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
1625 __ bind(&done);
1626 __ mov(eax, edx);
1627 }
1628
1629
1630 // Input: edx, eax are the left and right objects of a bit op.
1631 // Output: eax, ecx are left and right integers for a bit op.
1632 void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
1633 bool use_sse3,
1634 Label* conversion_failure) {
1635 // Check float operands.
1636 Label arg1_is_object, check_undefined_arg1;
1637 Label arg2_is_object, check_undefined_arg2;
1638 Label load_arg2, done;
1639
1640 // Test if arg1 is a Smi.
1641 __ test(edx, Immediate(kSmiTagMask));
1642 __ j(not_zero, &arg1_is_object);
1643
1644 __ SmiUntag(edx);
1645 __ jmp(&load_arg2);
1646
1647 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1648 __ bind(&check_undefined_arg1);
1649 __ cmp(edx, Factory::undefined_value());
1650 __ j(not_equal, conversion_failure);
1651 __ mov(edx, Immediate(0));
1652 __ jmp(&load_arg2);
1653
1654 __ bind(&arg1_is_object);
1655 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
1656 __ cmp(ebx, Factory::heap_number_map());
1657 __ j(not_equal, &check_undefined_arg1);
1658
1659 // Get the untagged integer version of the edx heap number in ecx.
1660 IntegerConvert(masm,
1661 edx,
1662 TypeInfo::Unknown(),
1663 use_sse3,
1664 conversion_failure);
1665 __ mov(edx, ecx);
1666
1667 // Here edx has the untagged integer, eax has a Smi or a heap number.
1668 __ bind(&load_arg2);
1669
1670 // Test if arg2 is a Smi.
1671 __ test(eax, Immediate(kSmiTagMask));
1672 __ j(not_zero, &arg2_is_object);
1673
1674 __ SmiUntag(eax);
1675 __ mov(ecx, eax);
1676 __ jmp(&done);
1677
1678 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1679 __ bind(&check_undefined_arg2);
1680 __ cmp(eax, Factory::undefined_value());
1681 __ j(not_equal, conversion_failure);
1682 __ mov(ecx, Immediate(0));
1683 __ jmp(&done);
1684
1685 __ bind(&arg2_is_object);
1686 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1687 __ cmp(ebx, Factory::heap_number_map());
1688 __ j(not_equal, &check_undefined_arg2);
1689
1690 // Get the untagged integer version of the eax heap number in ecx.
1691 IntegerConvert(masm,
1692 eax,
1693 TypeInfo::Unknown(),
1694 use_sse3,
1695 conversion_failure);
1696 __ bind(&done);
1697 __ mov(eax, edx);
1698 }
1699
1700
1701 void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
1702 TypeInfo type_info,
1703 bool use_sse3,
1704 Label* conversion_failure) {
1705 if (type_info.IsNumber()) {
1706 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
1707 } else {
1708 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
1709 }
1710 }
1711
1712
1713 void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
1714 Register number) {
1715 Label load_smi, done;
1716
1717 __ test(number, Immediate(kSmiTagMask));
1718 __ j(zero, &load_smi, not_taken);
1719 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
1720 __ jmp(&done);
1721
1722 __ bind(&load_smi);
1723 __ SmiUntag(number);
1724 __ push(number);
1725 __ fild_s(Operand(esp, 0));
1726 __ pop(number);
1727
1728 __ bind(&done);
1729 }
1730
1731
1732 void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
1733 Label load_smi_edx, load_eax, load_smi_eax, done;
1734 // Load operand in edx into xmm0.
1735 __ test(edx, Immediate(kSmiTagMask));
1736 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
1737 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
1738
1739 __ bind(&load_eax);
1740 // Load operand in eax into xmm1.
1741 __ test(eax, Immediate(kSmiTagMask));
1742 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
1743 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
1744 __ jmp(&done);
1745
1746 __ bind(&load_smi_edx);
1747 __ SmiUntag(edx); // Untag smi before converting to float.
1748 __ cvtsi2sd(xmm0, Operand(edx));
1749 __ SmiTag(edx); // Retag smi for heap number overwriting test.
1750 __ jmp(&load_eax);
1751
1752 __ bind(&load_smi_eax);
1753 __ SmiUntag(eax); // Untag smi before converting to float.
1754 __ cvtsi2sd(xmm1, Operand(eax));
1755 __ SmiTag(eax); // Retag smi for heap number overwriting test.
1756
1757 __ bind(&done);
1758 }
1759
1760
1761 void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
1762 Label* not_numbers) {
1763 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
1764 // Load operand in edx into xmm0, or branch to not_numbers.
1765 __ test(edx, Immediate(kSmiTagMask));
1766 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
1767 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), Factory::heap_number_map());
1768 __ j(not_equal, not_numbers); // Argument in edx is not a number.
1769 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
1770 __ bind(&load_eax);
1771 // Load operand in eax into xmm1, or branch to not_numbers.
1772 __ test(eax, Immediate(kSmiTagMask));
1773 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
1774 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), Factory::heap_number_map());
1775 __ j(equal, &load_float_eax);
1776 __ jmp(not_numbers); // Argument in eax is not a number.
1777 __ bind(&load_smi_edx);
1778 __ SmiUntag(edx); // Untag smi before converting to float.
1779 __ cvtsi2sd(xmm0, Operand(edx));
1780 __ SmiTag(edx); // Retag smi for heap number overwriting test.
1781 __ jmp(&load_eax);
1782 __ bind(&load_smi_eax);
1783 __ SmiUntag(eax); // Untag smi before converting to float.
1784 __ cvtsi2sd(xmm1, Operand(eax));
1785 __ SmiTag(eax); // Retag smi for heap number overwriting test.
1786 __ jmp(&done);
1787 __ bind(&load_float_eax);
1788 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
1789 __ bind(&done);
1790 }
1791
1792
1793 void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
1794 Register scratch) {
1795 const Register left = edx;
1796 const Register right = eax;
1797 __ mov(scratch, left);
1798 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
1799 __ SmiUntag(scratch);
1800 __ cvtsi2sd(xmm0, Operand(scratch));
1801
1802 __ mov(scratch, right);
1803 __ SmiUntag(scratch);
1804 __ cvtsi2sd(xmm1, Operand(scratch));
1805 }
1806
1807
1808 void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
1809 Register scratch,
1810 ArgLocation arg_location) {
1811 Label load_smi_1, load_smi_2, done_load_1, done;
1812 if (arg_location == ARGS_IN_REGISTERS) {
1813 __ mov(scratch, edx);
1814 } else {
1815 __ mov(scratch, Operand(esp, 2 * kPointerSize));
1816 }
1817 __ test(scratch, Immediate(kSmiTagMask));
1818 __ j(zero, &load_smi_1, not_taken);
1819 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
1820 __ bind(&done_load_1);
1821
1822 if (arg_location == ARGS_IN_REGISTERS) {
1823 __ mov(scratch, eax);
1824 } else {
1825 __ mov(scratch, Operand(esp, 1 * kPointerSize));
1826 }
1827 __ test(scratch, Immediate(kSmiTagMask));
1828 __ j(zero, &load_smi_2, not_taken);
1829 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
1830 __ jmp(&done);
1831
1832 __ bind(&load_smi_1);
1833 __ SmiUntag(scratch);
1834 __ push(scratch);
1835 __ fild_s(Operand(esp, 0));
1836 __ pop(scratch);
1837 __ jmp(&done_load_1);
1838
1839 __ bind(&load_smi_2);
1840 __ SmiUntag(scratch);
1841 __ push(scratch);
1842 __ fild_s(Operand(esp, 0));
1843 __ pop(scratch);
1844
1845 __ bind(&done);
1846 }
1847
1848
1849 void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
1850 Register scratch) {
1851 const Register left = edx;
1852 const Register right = eax;
1853 __ mov(scratch, left);
1854 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
1855 __ SmiUntag(scratch);
1856 __ push(scratch);
1857 __ fild_s(Operand(esp, 0));
1858
1859 __ mov(scratch, right);
1860 __ SmiUntag(scratch);
1861 __ mov(Operand(esp, 0), scratch);
1862 __ fild_s(Operand(esp, 0));
1863 __ pop(scratch);
1864 }
1865
1866
1867 void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
1868 Label* non_float,
1869 Register scratch) {
1870 Label test_other, done;
1871 // Test if both operands are floats or smi -> scratch=k_is_float;
1872 // Otherwise scratch = k_not_float.
1873 __ test(edx, Immediate(kSmiTagMask));
1874 __ j(zero, &test_other, not_taken); // argument in edx is OK
1875 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
1876 __ cmp(scratch, Factory::heap_number_map());
1877 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
1878
1879 __ bind(&test_other);
1880 __ test(eax, Immediate(kSmiTagMask));
1881 __ j(zero, &done); // argument in eax is OK
1882 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
1883 __ cmp(scratch, Factory::heap_number_map());
1884 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
1885
1886 // Fall-through: Both operands are numbers.
1887 __ bind(&done);
1888 }
1889
1890
1891 void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
1892 Label slow, done;
1893
1894 if (op_ == Token::SUB) {
1895 // Check whether the value is a smi.
1896 Label try_float;
1897 __ test(eax, Immediate(kSmiTagMask));
1898 __ j(not_zero, &try_float, not_taken);
1899
1900 if (negative_zero_ == kStrictNegativeZero) {
1901 // Go slow case if the value of the expression is zero
1902 // to make sure that we switch between 0 and -0.
1903 __ test(eax, Operand(eax));
1904 __ j(zero, &slow, not_taken);
1905 }
1906
1907 // The value of the expression is a smi that is not zero. Try
1908 // optimistic subtraction '0 - value'.
1909 Label undo;
1910 __ mov(edx, Operand(eax));
1911 __ Set(eax, Immediate(0));
1912 __ sub(eax, Operand(edx));
1913 __ j(no_overflow, &done, taken);
1914
1915 // Restore eax and go slow case.
1916 __ bind(&undo);
1917 __ mov(eax, Operand(edx));
1918 __ jmp(&slow);
1919
1920 // Try floating point case.
1921 __ bind(&try_float);
1922 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
1923 __ cmp(edx, Factory::heap_number_map());
1924 __ j(not_equal, &slow);
1925 if (overwrite_ == UNARY_OVERWRITE) {
1926 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
1927 __ xor_(edx, HeapNumber::kSignMask); // Flip sign.
1928 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), edx);
1929 } else {
1930 __ mov(edx, Operand(eax));
1931 // edx: operand
1932 __ AllocateHeapNumber(eax, ebx, ecx, &undo);
1933 // eax: allocated 'empty' number
1934 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
1935 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
1936 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
1937 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
1938 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
1939 }
1940 } else if (op_ == Token::BIT_NOT) {
1941 // Check if the operand is a heap number.
1942 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
1943 __ cmp(edx, Factory::heap_number_map());
1944 __ j(not_equal, &slow, not_taken);
1945
1946 // Convert the heap number in eax to an untagged integer in ecx.
1947 IntegerConvert(masm,
1948 eax,
1949 TypeInfo::Unknown(),
1950 CpuFeatures::IsSupported(SSE3),
1951 &slow);
1952
1953 // Do the bitwise operation and check if the result fits in a smi.
1954 Label try_float;
1955 __ not_(ecx);
1956 __ cmp(ecx, 0xc0000000);
1957 __ j(sign, &try_float, not_taken);
1958
1959 // Tag the result as a smi and we're done.
1960 STATIC_ASSERT(kSmiTagSize == 1);
1961 __ lea(eax, Operand(ecx, times_2, kSmiTag));
1962 __ jmp(&done);
1963
1964 // Try to store the result in a heap number.
1965 __ bind(&try_float);
1966 if (overwrite_ == UNARY_NO_OVERWRITE) {
1967 // Allocate a fresh heap number, but don't overwrite eax until
1968 // we're sure we can do it without going through the slow case
1969 // that needs the value in eax.
1970 __ AllocateHeapNumber(ebx, edx, edi, &slow);
1971 __ mov(eax, Operand(ebx));
1972 }
1973 if (CpuFeatures::IsSupported(SSE2)) {
1974 CpuFeatures::Scope use_sse2(SSE2);
1975 __ cvtsi2sd(xmm0, Operand(ecx));
1976 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1977 } else {
1978 __ push(ecx);
1979 __ fild_s(Operand(esp, 0));
1980 __ pop(ecx);
1981 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1982 }
1983 } else {
1984 UNIMPLEMENTED();
1985 }
1986
1987 // Return from the stub.
1988 __ bind(&done);
1989 __ StubReturn(1);
1990
1991 // Handle the slow case by jumping to the JavaScript builtin.
1992 __ bind(&slow);
1993 __ pop(ecx); // pop return address.
1994 __ push(eax);
1995 __ push(ecx); // push return address
1996 switch (op_) {
1997 case Token::SUB:
1998 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
1999 break;
2000 case Token::BIT_NOT:
2001 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
2002 break;
2003 default:
2004 UNREACHABLE();
2005 }
2006 }
2007
2008
2009 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2010 // The key is in edx and the parameter count is in eax.
2011
2012 // The displacement is used for skipping the frame pointer on the
2013 // stack. It is the offset of the last parameter (if any) relative
2014 // to the frame pointer.
2015 static const int kDisplacement = 1 * kPointerSize;
2016
2017 // Check that the key is a smi.
2018 Label slow;
2019 __ test(edx, Immediate(kSmiTagMask));
2020 __ j(not_zero, &slow, not_taken);
2021
2022 // Check if the calling frame is an arguments adaptor frame.
2023 Label adaptor;
2024 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2025 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2026 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2027 __ j(equal, &adaptor);
2028
2029 // Check index against formal parameters count limit passed in
2030 // through register eax. Use unsigned comparison to get negative
2031 // check for free.
2032 __ cmp(edx, Operand(eax));
2033 __ j(above_equal, &slow, not_taken);
2034
2035 // Read the argument from the stack and return it.
2036 STATIC_ASSERT(kSmiTagSize == 1);
2037 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2038 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2039 __ neg(edx);
2040 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2041 __ ret(0);
2042
2043 // Arguments adaptor case: Check index against actual arguments
2044 // limit found in the arguments adaptor frame. Use unsigned
2045 // comparison to get negative check for free.
2046 __ bind(&adaptor);
2047 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2048 __ cmp(edx, Operand(ecx));
2049 __ j(above_equal, &slow, not_taken);
2050
2051 // Read the argument from the stack and return it.
2052 STATIC_ASSERT(kSmiTagSize == 1);
2053 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2054 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2055 __ neg(edx);
2056 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2057 __ ret(0);
2058
2059 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2060 // by calling the runtime system.
2061 __ bind(&slow);
2062 __ pop(ebx); // Return address.
2063 __ push(edx);
2064 __ push(ebx);
2065 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2066 }
2067
2068
2069 void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2070 // esp[0] : return address
2071 // esp[4] : number of parameters
2072 // esp[8] : receiver displacement
2073 // esp[16] : function
2074
2075 // The displacement is used for skipping the return address and the
2076 // frame pointer on the stack. It is the offset of the last
2077 // parameter (if any) relative to the frame pointer.
2078 static const int kDisplacement = 2 * kPointerSize;
2079
2080 // Check if the calling frame is an arguments adaptor frame.
2081 Label adaptor_frame, try_allocate, runtime;
2082 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2083 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2084 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2085 __ j(equal, &adaptor_frame);
2086
2087 // Get the length from the frame.
2088 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2089 __ jmp(&try_allocate);
2090
2091 // Patch the arguments.length and the parameters pointer.
2092 __ bind(&adaptor_frame);
2093 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2094 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2095 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
2096 __ mov(Operand(esp, 2 * kPointerSize), edx);
2097
2098 // Try the new space allocation. Start out with computing the size of
2099 // the arguments object and the elements array.
2100 Label add_arguments_object;
2101 __ bind(&try_allocate);
2102 __ test(ecx, Operand(ecx));
2103 __ j(zero, &add_arguments_object);
2104 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
2105 __ bind(&add_arguments_object);
2106 __ add(Operand(ecx), Immediate(Heap::kArgumentsObjectSize));
2107
2108 // Do the allocation of both objects in one go.
2109 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
2110
2111 // Get the arguments boilerplate from the current (global) context.
2112 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
2113 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2114 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
2115 __ mov(edi, Operand(edi, offset));
2116
2117 // Copy the JS object part.
2118 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2119 __ mov(ebx, FieldOperand(edi, i));
2120 __ mov(FieldOperand(eax, i), ebx);
2121 }
2122
2123 // Setup the callee in-object property.
2124 STATIC_ASSERT(Heap::arguments_callee_index == 0);
2125 __ mov(ebx, Operand(esp, 3 * kPointerSize));
2126 __ mov(FieldOperand(eax, JSObject::kHeaderSize), ebx);
2127
2128 // Get the length (smi tagged) and set that as an in-object property too.
2129 STATIC_ASSERT(Heap::arguments_length_index == 1);
2130 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2131 __ mov(FieldOperand(eax, JSObject::kHeaderSize + kPointerSize), ecx);
2132
2133 // If there are no actual arguments, we're done.
2134 Label done;
2135 __ test(ecx, Operand(ecx));
2136 __ j(zero, &done);
2137
2138 // Get the parameters pointer from the stack.
2139 __ mov(edx, Operand(esp, 2 * kPointerSize));
2140
2141 // Setup the elements pointer in the allocated arguments object and
2142 // initialize the header in the elements fixed array.
2143 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
2144 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
2145 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
2146 Immediate(Factory::fixed_array_map()));
2147 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
2148 // Untag the length for the loop below.
2149 __ SmiUntag(ecx);
2150
2151 // Copy the fixed array slots.
2152 Label loop;
2153 __ bind(&loop);
2154 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
2155 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
2156 __ add(Operand(edi), Immediate(kPointerSize));
2157 __ sub(Operand(edx), Immediate(kPointerSize));
2158 __ dec(ecx);
2159 __ j(not_zero, &loop);
2160
2161 // Return and remove the on-stack parameters.
2162 __ bind(&done);
2163 __ ret(3 * kPointerSize);
2164
2165 // Do the runtime call to allocate the arguments object.
2166 __ bind(&runtime);
2167 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2168 }
2169
2170
2171 void RegExpExecStub::Generate(MacroAssembler* masm) {
2172 // Just jump directly to runtime if native RegExp is not selected at compile
2173 // time or if regexp entry in generated code is turned off runtime switch or
2174 // at compilation.
2175 #ifdef V8_INTERPRETED_REGEXP
2176 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2177 #else // V8_INTERPRETED_REGEXP
2178 if (!FLAG_regexp_entry_native) {
2179 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2180 return;
2181 }
2182
2183 // Stack frame on entry.
2184 // esp[0]: return address
2185 // esp[4]: last_match_info (expected JSArray)
2186 // esp[8]: previous index
2187 // esp[12]: subject string
2188 // esp[16]: JSRegExp object
2189
2190 static const int kLastMatchInfoOffset = 1 * kPointerSize;
2191 static const int kPreviousIndexOffset = 2 * kPointerSize;
2192 static const int kSubjectOffset = 3 * kPointerSize;
2193 static const int kJSRegExpOffset = 4 * kPointerSize;
2194
2195 Label runtime, invoke_regexp;
2196
2197 // Ensure that a RegExp stack is allocated.
2198 ExternalReference address_of_regexp_stack_memory_address =
2199 ExternalReference::address_of_regexp_stack_memory_address();
2200 ExternalReference address_of_regexp_stack_memory_size =
2201 ExternalReference::address_of_regexp_stack_memory_size();
2202 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
2203 __ test(ebx, Operand(ebx));
2204 __ j(zero, &runtime, not_taken);
2205
2206 // Check that the first argument is a JSRegExp object.
2207 __ mov(eax, Operand(esp, kJSRegExpOffset));
2208 STATIC_ASSERT(kSmiTag == 0);
2209 __ test(eax, Immediate(kSmiTagMask));
2210 __ j(zero, &runtime);
2211 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
2212 __ j(not_equal, &runtime);
2213 // Check that the RegExp has been compiled (data contains a fixed array).
2214 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
2215 if (FLAG_debug_code) {
2216 __ test(ecx, Immediate(kSmiTagMask));
2217 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
2218 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
2219 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
2220 }
2221
2222 // ecx: RegExp data (FixedArray)
2223 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
2224 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
2225 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
2226 __ j(not_equal, &runtime);
2227
2228 // ecx: RegExp data (FixedArray)
2229 // Check that the number of captures fit in the static offsets vector buffer.
2230 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
2231 // Calculate number of capture registers (number_of_captures + 1) * 2. This
2232 // uses the asumption that smis are 2 * their untagged value.
2233 STATIC_ASSERT(kSmiTag == 0);
2234 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2235 __ add(Operand(edx), Immediate(2)); // edx was a smi.
2236 // Check that the static offsets vector buffer is large enough.
2237 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
2238 __ j(above, &runtime);
2239
2240 // ecx: RegExp data (FixedArray)
2241 // edx: Number of capture registers
2242 // Check that the second argument is a string.
2243 __ mov(eax, Operand(esp, kSubjectOffset));
2244 __ test(eax, Immediate(kSmiTagMask));
2245 __ j(zero, &runtime);
2246 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2247 __ j(NegateCondition(is_string), &runtime);
2248 // Get the length of the string to ebx.
2249 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
2250
2251 // ebx: Length of subject string as a smi
2252 // ecx: RegExp data (FixedArray)
2253 // edx: Number of capture registers
2254 // Check that the third argument is a positive smi less than the subject
2255 // string length. A negative value will be greater (unsigned comparison).
2256 __ mov(eax, Operand(esp, kPreviousIndexOffset));
2257 __ test(eax, Immediate(kSmiTagMask));
2258 __ j(not_zero, &runtime);
2259 __ cmp(eax, Operand(ebx));
2260 __ j(above_equal, &runtime);
2261
2262 // ecx: RegExp data (FixedArray)
2263 // edx: Number of capture registers
2264 // Check that the fourth object is a JSArray object.
2265 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2266 __ test(eax, Immediate(kSmiTagMask));
2267 __ j(zero, &runtime);
2268 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
2269 __ j(not_equal, &runtime);
2270 // Check that the JSArray is in fast case.
2271 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
2272 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
2273 __ cmp(eax, Factory::fixed_array_map());
2274 __ j(not_equal, &runtime);
2275 // Check that the last match info has space for the capture registers and the
2276 // additional information.
2277 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
2278 __ SmiUntag(eax);
2279 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
2280 __ cmp(edx, Operand(eax));
2281 __ j(greater, &runtime);
2282
2283 // ecx: RegExp data (FixedArray)
2284 // Check the representation and encoding of the subject string.
2285 Label seq_ascii_string, seq_two_byte_string, check_code;
2286 __ mov(eax, Operand(esp, kSubjectOffset));
2287 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2288 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2289 // First check for flat two byte string.
2290 __ and_(ebx,
2291 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2292 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
2293 __ j(zero, &seq_two_byte_string);
2294 // Any other flat string must be a flat ascii string.
2295 __ test(Operand(ebx),
2296 Immediate(kIsNotStringMask | kStringRepresentationMask));
2297 __ j(zero, &seq_ascii_string);
2298
2299 // Check for flat cons string.
2300 // A flat cons string is a cons string where the second part is the empty
2301 // string. In that case the subject string is just the first part of the cons
2302 // string. Also in this case the first part of the cons string is known to be
2303 // a sequential string or an external string.
2304 STATIC_ASSERT(kExternalStringTag != 0);
2305 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
2306 __ test(Operand(ebx),
2307 Immediate(kIsNotStringMask | kExternalStringTag));
2308 __ j(not_zero, &runtime);
2309 // String is a cons string.
2310 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
2311 __ cmp(Operand(edx), Factory::empty_string());
2312 __ j(not_equal, &runtime);
2313 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
2314 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2315 // String is a cons string with empty second part.
2316 // eax: first part of cons string.
2317 // ebx: map of first part of cons string.
2318 // Is first part a flat two byte string?
2319 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
2320 kStringRepresentationMask | kStringEncodingMask);
2321 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
2322 __ j(zero, &seq_two_byte_string);
2323 // Any other flat string must be ascii.
2324 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
2325 kStringRepresentationMask);
2326 __ j(not_zero, &runtime);
2327
2328 __ bind(&seq_ascii_string);
2329 // eax: subject string (flat ascii)
2330 // ecx: RegExp data (FixedArray)
2331 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
2332 __ Set(edi, Immediate(1)); // Type is ascii.
2333 __ jmp(&check_code);
2334
2335 __ bind(&seq_two_byte_string);
2336 // eax: subject string (flat two byte)
2337 // ecx: RegExp data (FixedArray)
2338 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
2339 __ Set(edi, Immediate(0)); // Type is two byte.
2340
2341 __ bind(&check_code);
2342 // Check that the irregexp code has been generated for the actual string
2343 // encoding. If it has, the field contains a code object otherwise it contains
2344 // the hole.
2345 __ CmpObjectType(edx, CODE_TYPE, ebx);
2346 __ j(not_equal, &runtime);
2347
2348 // eax: subject string
2349 // edx: code
2350 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
2351 // Load used arguments before starting to push arguments for call to native
2352 // RegExp code to avoid handling changing stack height.
2353 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
2354 __ SmiUntag(ebx); // Previous index from smi.
2355
2356 // eax: subject string
2357 // ebx: previous index
2358 // edx: code
2359 // edi: encoding of subject string (1 if ascii 0 if two_byte);
2360 // All checks done. Now push arguments for native regexp code.
2361 __ IncrementCounter(&Counters::regexp_entry_native, 1);
2362
2363 static const int kRegExpExecuteArguments = 7;
2364 __ PrepareCallCFunction(kRegExpExecuteArguments, ecx);
2365
2366 // Argument 7: Indicate that this is a direct call from JavaScript.
2367 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
2368
2369 // Argument 6: Start (high end) of backtracking stack memory area.
2370 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
2371 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
2372 __ mov(Operand(esp, 5 * kPointerSize), ecx);
2373
2374 // Argument 5: static offsets vector buffer.
2375 __ mov(Operand(esp, 4 * kPointerSize),
2376 Immediate(ExternalReference::address_of_static_offsets_vector()));
2377
2378 // Argument 4: End of string data
2379 // Argument 3: Start of string data
2380 Label setup_two_byte, setup_rest;
2381 __ test(edi, Operand(edi));
2382 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
2383 __ j(zero, &setup_two_byte);
2384 __ SmiUntag(edi);
2385 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
2386 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
2387 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
2388 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
2389 __ jmp(&setup_rest);
2390
2391 __ bind(&setup_two_byte);
2392 STATIC_ASSERT(kSmiTag == 0);
2393 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
2394 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
2395 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
2396 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
2397 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
2398
2399 __ bind(&setup_rest);
2400
2401 // Argument 2: Previous index.
2402 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2403
2404 // Argument 1: Subject string.
2405 __ mov(Operand(esp, 0 * kPointerSize), eax);
2406
2407 // Locate the code entry and call it.
2408 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
2409 __ CallCFunction(edx, kRegExpExecuteArguments);
2410
2411 // Check the result.
2412 Label success;
2413 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
2414 __ j(equal, &success, taken);
2415 Label failure;
2416 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
2417 __ j(equal, &failure, taken);
2418 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
2419 // If not exception it can only be retry. Handle that in the runtime system.
2420 __ j(not_equal, &runtime);
2421 // Result must now be exception. If there is no pending exception already a
2422 // stack overflow (on the backtrack stack) was detected in RegExp code but
2423 // haven't created the exception yet. Handle that in the runtime system.
2424 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
2425 ExternalReference pending_exception(Top::k_pending_exception_address);
2426 __ mov(eax,
2427 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
2428 __ cmp(eax, Operand::StaticVariable(pending_exception));
2429 __ j(equal, &runtime);
2430 __ bind(&failure);
2431 // For failure and exception return null.
2432 __ mov(Operand(eax), Factory::null_value());
2433 __ ret(4 * kPointerSize);
2434
2435 // Load RegExp data.
2436 __ bind(&success);
2437 __ mov(eax, Operand(esp, kJSRegExpOffset));
2438 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
2439 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
2440 // Calculate number of capture registers (number_of_captures + 1) * 2.
2441 STATIC_ASSERT(kSmiTag == 0);
2442 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2443 __ add(Operand(edx), Immediate(2)); // edx was a smi.
2444
2445 // edx: Number of capture registers
2446 // Load last_match_info which is still known to be a fast case JSArray.
2447 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2448 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
2449
2450 // ebx: last_match_info backing store (FixedArray)
2451 // edx: number of capture registers
2452 // Store the capture count.
2453 __ SmiTag(edx); // Number of capture registers to smi.
2454 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
2455 __ SmiUntag(edx); // Number of capture registers back from smi.
2456 // Store last subject and last input.
2457 __ mov(eax, Operand(esp, kSubjectOffset));
2458 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
2459 __ mov(ecx, ebx);
2460 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
2461 __ mov(eax, Operand(esp, kSubjectOffset));
2462 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
2463 __ mov(ecx, ebx);
2464 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
2465
2466 // Get the static offsets vector filled by the native regexp code.
2467 ExternalReference address_of_static_offsets_vector =
2468 ExternalReference::address_of_static_offsets_vector();
2469 __ mov(ecx, Immediate(address_of_static_offsets_vector));
2470
2471 // ebx: last_match_info backing store (FixedArray)
2472 // ecx: offsets vector
2473 // edx: number of capture registers
2474 Label next_capture, done;
2475 // Capture register counter starts from number of capture registers and
2476 // counts down until wraping after zero.
2477 __ bind(&next_capture);
2478 __ sub(Operand(edx), Immediate(1));
2479 __ j(negative, &done);
2480 // Read the value from the static offsets vector buffer.
2481 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
2482 __ SmiTag(edi);
2483 // Store the smi value in the last match info.
2484 __ mov(FieldOperand(ebx,
2485 edx,
2486 times_pointer_size,
2487 RegExpImpl::kFirstCaptureOffset),
2488 edi);
2489 __ jmp(&next_capture);
2490 __ bind(&done);
2491
2492 // Return last match info.
2493 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2494 __ ret(4 * kPointerSize);
2495
2496 // Do the runtime call to execute the regexp.
2497 __ bind(&runtime);
2498 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2499 #endif // V8_INTERPRETED_REGEXP
2500 }
2501
2502
2503 void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
2504 Register object,
2505 Register result,
2506 Register scratch1,
2507 Register scratch2,
2508 bool object_is_smi,
2509 Label* not_found) {
2510 // Use of registers. Register result is used as a temporary.
2511 Register number_string_cache = result;
2512 Register mask = scratch1;
2513 Register scratch = scratch2;
2514
2515 // Load the number string cache.
2516 ExternalReference roots_address = ExternalReference::roots_address();
2517 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
2518 __ mov(number_string_cache,
2519 Operand::StaticArray(scratch, times_pointer_size, roots_address));
2520 // Make the hash mask from the length of the number string cache. It
2521 // contains two elements (number and string) for each cache entry.
2522 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2523 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
2524 __ sub(Operand(mask), Immediate(1)); // Make mask.
2525
2526 // Calculate the entry in the number string cache. The hash value in the
2527 // number string cache for smis is just the smi value, and the hash for
2528 // doubles is the xor of the upper and lower words. See
2529 // Heap::GetNumberStringCache.
2530 Label smi_hash_calculated;
2531 Label load_result_from_cache;
2532 if (object_is_smi) {
2533 __ mov(scratch, object);
2534 __ SmiUntag(scratch);
2535 } else {
2536 Label not_smi, hash_calculated;
2537 STATIC_ASSERT(kSmiTag == 0);
2538 __ test(object, Immediate(kSmiTagMask));
2539 __ j(not_zero, &not_smi);
2540 __ mov(scratch, object);
2541 __ SmiUntag(scratch);
2542 __ jmp(&smi_hash_calculated);
2543 __ bind(&not_smi);
2544 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
2545 Factory::heap_number_map());
2546 __ j(not_equal, not_found);
2547 STATIC_ASSERT(8 == kDoubleSize);
2548 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2549 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2550 // Object is heap number and hash is now in scratch. Calculate cache index.
2551 __ and_(scratch, Operand(mask));
2552 Register index = scratch;
2553 Register probe = mask;
2554 __ mov(probe,
2555 FieldOperand(number_string_cache,
2556 index,
2557 times_twice_pointer_size,
2558 FixedArray::kHeaderSize));
2559 __ test(probe, Immediate(kSmiTagMask));
2560 __ j(zero, not_found);
2561 if (CpuFeatures::IsSupported(SSE2)) {
2562 CpuFeatures::Scope fscope(SSE2);
2563 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2564 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
2565 __ ucomisd(xmm0, xmm1);
2566 } else {
2567 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
2568 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
2569 __ FCmp();
2570 }
2571 __ j(parity_even, not_found); // Bail out if NaN is involved.
2572 __ j(not_equal, not_found); // The cache did not contain this value.
2573 __ jmp(&load_result_from_cache);
2574 }
2575
2576 __ bind(&smi_hash_calculated);
2577 // Object is smi and hash is now in scratch. Calculate cache index.
2578 __ and_(scratch, Operand(mask));
2579 Register index = scratch;
2580 // Check if the entry is the smi we are looking for.
2581 __ cmp(object,
2582 FieldOperand(number_string_cache,
2583 index,
2584 times_twice_pointer_size,
2585 FixedArray::kHeaderSize));
2586 __ j(not_equal, not_found);
2587
2588 // Get the result from the cache.
2589 __ bind(&load_result_from_cache);
2590 __ mov(result,
2591 FieldOperand(number_string_cache,
2592 index,
2593 times_twice_pointer_size,
2594 FixedArray::kHeaderSize + kPointerSize));
2595 __ IncrementCounter(&Counters::number_to_string_native, 1);
2596 }
2597
2598
2599 void NumberToStringStub::Generate(MacroAssembler* masm) {
2600 Label runtime;
2601
2602 __ mov(ebx, Operand(esp, kPointerSize));
2603
2604 // Generate code to lookup number in the number string cache.
2605 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
2606 __ ret(1 * kPointerSize);
2607
2608 __ bind(&runtime);
2609 // Handle number to string in the runtime system if not found in the cache.
2610 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
2611 }
2612
2613
2614 static int NegativeComparisonResult(Condition cc) {
2615 ASSERT(cc != equal);
2616 ASSERT((cc == less) || (cc == less_equal)
2617 || (cc == greater) || (cc == greater_equal));
2618 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
2619 }
2620
2621 void CompareStub::Generate(MacroAssembler* masm) {
2622 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
2623
2624 Label check_unequal_objects, done;
2625
2626 // NOTICE! This code is only reached after a smi-fast-case check, so
2627 // it is certain that at least one operand isn't a smi.
2628
2629 // Identical objects can be compared fast, but there are some tricky cases
2630 // for NaN and undefined.
2631 {
2632 Label not_identical;
2633 __ cmp(eax, Operand(edx));
2634 __ j(not_equal, &not_identical);
2635
2636 if (cc_ != equal) {
2637 // Check for undefined. undefined OP undefined is false even though
2638 // undefined == undefined.
2639 Label check_for_nan;
2640 __ cmp(edx, Factory::undefined_value());
2641 __ j(not_equal, &check_for_nan);
2642 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2643 __ ret(0);
2644 __ bind(&check_for_nan);
2645 }
2646
2647 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
2648 // so we do the second best thing - test it ourselves.
2649 // Note: if cc_ != equal, never_nan_nan_ is not used.
2650 if (never_nan_nan_ && (cc_ == equal)) {
2651 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2652 __ ret(0);
2653 } else {
2654 Label heap_number;
2655 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2656 Immediate(Factory::heap_number_map()));
2657 __ j(equal, &heap_number);
2658 if (cc_ != equal) {
2659 // Call runtime on identical JSObjects. Otherwise return equal.
2660 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2661 __ j(above_equal, &not_identical);
2662 }
2663 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2664 __ ret(0);
2665
2666 __ bind(&heap_number);
2667 // It is a heap number, so return non-equal if it's NaN and equal if
2668 // it's not NaN.
2669 // The representation of NaN values has all exponent bits (52..62) set,
2670 // and not all mantissa bits (0..51) clear.
2671 // We only accept QNaNs, which have bit 51 set.
2672 // Read top bits of double representation (second word of value).
2673
2674 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
2675 // all bits in the mask are set. We only need to check the word
2676 // that contains the exponent and high bit of the mantissa.
2677 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
2678 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
2679 __ xor_(eax, Operand(eax));
2680 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
2681 // bits.
2682 __ add(edx, Operand(edx));
2683 __ cmp(edx, kQuietNaNHighBitsMask << 1);
2684 if (cc_ == equal) {
2685 STATIC_ASSERT(EQUAL != 1);
2686 __ setcc(above_equal, eax);
2687 __ ret(0);
2688 } else {
2689 Label nan;
2690 __ j(above_equal, &nan);
2691 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2692 __ ret(0);
2693 __ bind(&nan);
2694 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2695 __ ret(0);
2696 }
2697 }
2698
2699 __ bind(&not_identical);
2700 }
2701
2702 // Strict equality can quickly decide whether objects are equal.
2703 // Non-strict object equality is slower, so it is handled later in the stub.
2704 if (cc_ == equal && strict_) {
2705 Label slow; // Fallthrough label.
2706 Label not_smis;
2707 // If we're doing a strict equality comparison, we don't have to do
2708 // type conversion, so we generate code to do fast comparison for objects
2709 // and oddballs. Non-smi numbers and strings still go through the usual
2710 // slow-case code.
2711 // If either is a Smi (we know that not both are), then they can only
2712 // be equal if the other is a HeapNumber. If so, use the slow case.
2713 STATIC_ASSERT(kSmiTag == 0);
2714 ASSERT_EQ(0, Smi::FromInt(0));
2715 __ mov(ecx, Immediate(kSmiTagMask));
2716 __ and_(ecx, Operand(eax));
2717 __ test(ecx, Operand(edx));
2718 __ j(not_zero, &not_smis);
2719 // One operand is a smi.
2720
2721 // Check whether the non-smi is a heap number.
2722 STATIC_ASSERT(kSmiTagMask == 1);
2723 // ecx still holds eax & kSmiTag, which is either zero or one.
2724 __ sub(Operand(ecx), Immediate(0x01));
2725 __ mov(ebx, edx);
2726 __ xor_(ebx, Operand(eax));
2727 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
2728 __ xor_(ebx, Operand(eax));
2729 // if eax was smi, ebx is now edx, else eax.
2730
2731 // Check if the non-smi operand is a heap number.
2732 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
2733 Immediate(Factory::heap_number_map()));
2734 // If heap number, handle it in the slow case.
2735 __ j(equal, &slow);
2736 // Return non-equal (ebx is not zero)
2737 __ mov(eax, ebx);
2738 __ ret(0);
2739
2740 __ bind(&not_smis);
2741 // If either operand is a JSObject or an oddball value, then they are not
2742 // equal since their pointers are different
2743 // There is no test for undetectability in strict equality.
2744
2745 // Get the type of the first operand.
2746 // If the first object is a JS object, we have done pointer comparison.
2747 Label first_non_object;
2748 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2749 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2750 __ j(below, &first_non_object);
2751
2752 // Return non-zero (eax is not zero)
2753 Label return_not_equal;
2754 STATIC_ASSERT(kHeapObjectTag != 0);
2755 __ bind(&return_not_equal);
2756 __ ret(0);
2757
2758 __ bind(&first_non_object);
2759 // Check for oddballs: true, false, null, undefined.
2760 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2761 __ j(equal, &return_not_equal);
2762
2763 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
2764 __ j(above_equal, &return_not_equal);
2765
2766 // Check for oddballs: true, false, null, undefined.
2767 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2768 __ j(equal, &return_not_equal);
2769
2770 // Fall through to the general case.
2771 __ bind(&slow);
2772 }
2773
2774 // Generate the number comparison code.
2775 if (include_number_compare_) {
2776 Label non_number_comparison;
2777 Label unordered;
2778 if (CpuFeatures::IsSupported(SSE2)) {
2779 CpuFeatures::Scope use_sse2(SSE2);
2780 CpuFeatures::Scope use_cmov(CMOV);
2781
2782 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
2783 __ ucomisd(xmm0, xmm1);
2784
2785 // Don't base result on EFLAGS when a NaN is involved.
2786 __ j(parity_even, &unordered, not_taken);
2787 // Return a result of -1, 0, or 1, based on EFLAGS.
2788 __ mov(eax, 0); // equal
2789 __ mov(ecx, Immediate(Smi::FromInt(1)));
2790 __ cmov(above, eax, Operand(ecx));
2791 __ mov(ecx, Immediate(Smi::FromInt(-1)));
2792 __ cmov(below, eax, Operand(ecx));
2793 __ ret(0);
2794 } else {
2795 FloatingPointHelper::CheckFloatOperands(
2796 masm, &non_number_comparison, ebx);
2797 FloatingPointHelper::LoadFloatOperand(masm, eax);
2798 FloatingPointHelper::LoadFloatOperand(masm, edx);
2799 __ FCmp();
2800
2801 // Don't base result on EFLAGS when a NaN is involved.
2802 __ j(parity_even, &unordered, not_taken);
2803
2804 Label below_label, above_label;
2805 // Return a result of -1, 0, or 1, based on EFLAGS.
2806 __ j(below, &below_label, not_taken);
2807 __ j(above, &above_label, not_taken);
2808
2809 __ xor_(eax, Operand(eax));
2810 __ ret(0);
2811
2812 __ bind(&below_label);
2813 __ mov(eax, Immediate(Smi::FromInt(-1)));
2814 __ ret(0);
2815
2816 __ bind(&above_label);
2817 __ mov(eax, Immediate(Smi::FromInt(1)));
2818 __ ret(0);
2819 }
2820
2821 // If one of the numbers was NaN, then the result is always false.
2822 // The cc is never not-equal.
2823 __ bind(&unordered);
2824 ASSERT(cc_ != not_equal);
2825 if (cc_ == less || cc_ == less_equal) {
2826 __ mov(eax, Immediate(Smi::FromInt(1)));
2827 } else {
2828 __ mov(eax, Immediate(Smi::FromInt(-1)));
2829 }
2830 __ ret(0);
2831
2832 // The number comparison code did not provide a valid result.
2833 __ bind(&non_number_comparison);
2834 }
2835
2836 // Fast negative check for symbol-to-symbol equality.
2837 Label check_for_strings;
2838 if (cc_ == equal) {
2839 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
2840 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
2841
2842 // We've already checked for object identity, so if both operands
2843 // are symbols they aren't equal. Register eax already holds a
2844 // non-zero value, which indicates not equal, so just return.
2845 __ ret(0);
2846 }
2847
2848 __ bind(&check_for_strings);
2849
2850 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
2851 &check_unequal_objects);
2852
2853 // Inline comparison of ascii strings.
2854 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
2855 edx,
2856 eax,
2857 ecx,
2858 ebx,
2859 edi);
2860 #ifdef DEBUG
2861 __ Abort("Unexpected fall-through from string comparison");
2862 #endif
2863
2864 __ bind(&check_unequal_objects);
2865 if (cc_ == equal && !strict_) {
2866 // Non-strict equality. Objects are unequal if
2867 // they are both JSObjects and not undetectable,
2868 // and their pointers are different.
2869 Label not_both_objects;
2870 Label return_unequal;
2871 // At most one is a smi, so we can test for smi by adding the two.
2872 // A smi plus a heap object has the low bit set, a heap object plus
2873 // a heap object has the low bit clear.
2874 STATIC_ASSERT(kSmiTag == 0);
2875 STATIC_ASSERT(kSmiTagMask == 1);
2876 __ lea(ecx, Operand(eax, edx, times_1, 0));
2877 __ test(ecx, Immediate(kSmiTagMask));
2878 __ j(not_zero, &not_both_objects);
2879 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2880 __ j(below, &not_both_objects);
2881 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
2882 __ j(below, &not_both_objects);
2883 // We do not bail out after this point. Both are JSObjects, and
2884 // they are equal if and only if both are undetectable.
2885 // The and of the undetectable flags is 1 if and only if they are equal.
2886 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
2887 1 << Map::kIsUndetectable);
2888 __ j(zero, &return_unequal);
2889 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
2890 1 << Map::kIsUndetectable);
2891 __ j(zero, &return_unequal);
2892 // The objects are both undetectable, so they both compare as the value
2893 // undefined, and are equal.
2894 __ Set(eax, Immediate(EQUAL));
2895 __ bind(&return_unequal);
2896 // Return non-equal by returning the non-zero object pointer in eax,
2897 // or return equal if we fell through to here.
2898 __ ret(0); // rax, rdx were pushed
2899 __ bind(&not_both_objects);
2900 }
2901
2902 // Push arguments below the return address.
2903 __ pop(ecx);
2904 __ push(edx);
2905 __ push(eax);
2906
2907 // Figure out which native to call and setup the arguments.
2908 Builtins::JavaScript builtin;
2909 if (cc_ == equal) {
2910 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
2911 } else {
2912 builtin = Builtins::COMPARE;
2913 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2914 }
2915
2916 // Restore return address on the stack.
2917 __ push(ecx);
2918
2919 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2920 // tagged as a small integer.
2921 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
2922 }
2923
2924
2925 void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
2926 Label* label,
2927 Register object,
2928 Register scratch) {
2929 __ test(object, Immediate(kSmiTagMask));
2930 __ j(zero, label);
2931 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
2932 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
2933 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
2934 __ cmp(scratch, kSymbolTag | kStringTag);
2935 __ j(not_equal, label);
2936 }
2937
2938
2939 void StackCheckStub::Generate(MacroAssembler* masm) {
2940 // Because builtins always remove the receiver from the stack, we
2941 // have to fake one to avoid underflowing the stack. The receiver
2942 // must be inserted below the return address on the stack so we
2943 // temporarily store that in a register.
2944 __ pop(eax);
2945 __ push(Immediate(Smi::FromInt(0)));
2946 __ push(eax);
2947
2948 // Do tail-call to runtime routine.
2949 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
2950 }
2951
2952
2953 void CallFunctionStub::Generate(MacroAssembler* masm) {
2954 Label slow;
2955
2956 // If the receiver might be a value (string, number or boolean) check for this
2957 // and box it if it is.
2958 if (ReceiverMightBeValue()) {
2959 // Get the receiver from the stack.
2960 // +1 ~ return address
2961 Label receiver_is_value, receiver_is_js_object;
2962 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
2963
2964 // Check if receiver is a smi (which is a number value).
2965 __ test(eax, Immediate(kSmiTagMask));
2966 __ j(zero, &receiver_is_value, not_taken);
2967
2968 // Check if the receiver is a valid JS object.
2969 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
2970 __ j(above_equal, &receiver_is_js_object);
2971
2972 // Call the runtime to box the value.
2973 __ bind(&receiver_is_value);
2974 __ EnterInternalFrame();
2975 __ push(eax);
2976 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2977 __ LeaveInternalFrame();
2978 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
2979
2980 __ bind(&receiver_is_js_object);
2981 }
2982
2983 // Get the function to call from the stack.
2984 // +2 ~ receiver, return address
2985 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
2986
2987 // Check that the function really is a JavaScript function.
2988 __ test(edi, Immediate(kSmiTagMask));
2989 __ j(zero, &slow, not_taken);
2990 // Goto slow case if we do not have a function.
2991 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2992 __ j(not_equal, &slow, not_taken);
2993
2994 // Fast-case: Just invoke the function.
2995 ParameterCount actual(argc_);
2996 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
2997
2998 // Slow-case: Non-function called.
2999 __ bind(&slow);
3000 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3001 // of the original receiver from the call site).
3002 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
3003 __ Set(eax, Immediate(argc_));
3004 __ Set(ebx, Immediate(0));
3005 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
3006 Handle<Code> adaptor(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
3007 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3008 }
3009
3010
3011 void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3012 // eax holds the exception.
3013
3014 // Adjust this code if not the case.
3015 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
3016
3017 // Drop the sp to the top of the handler.
3018 ExternalReference handler_address(Top::k_handler_address);
3019 __ mov(esp, Operand::StaticVariable(handler_address));
3020
3021 // Restore next handler and frame pointer, discard handler state.
3022 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3023 __ pop(Operand::StaticVariable(handler_address));
3024 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
3025 __ pop(ebp);
3026 __ pop(edx); // Remove state.
3027
3028 // Before returning we restore the context from the frame pointer if
3029 // not NULL. The frame pointer is NULL in the exception handler of
3030 // a JS entry frame.
3031 __ xor_(esi, Operand(esi)); // Tentatively set context pointer to NULL.
3032 Label skip;
3033 __ cmp(ebp, 0);
3034 __ j(equal, &skip, not_taken);
3035 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3036 __ bind(&skip);
3037
3038 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
3039 __ ret(0);
3040 }
3041
3042
3043 // If true, a Handle<T> passed by value is passed and returned by
3044 // using the location_ field directly. If false, it is passed and
3045 // returned as a pointer to a handle.
3046 #ifdef USING_BSD_ABI
3047 static const bool kPassHandlesDirectly = true;
3048 #else
3049 static const bool kPassHandlesDirectly = false;
3050 #endif
3051
3052
3053 void ApiGetterEntryStub::Generate(MacroAssembler* masm) {
3054 Label empty_handle;
3055 Label prologue;
3056 Label promote_scheduled_exception;
3057 __ EnterApiExitFrame(ExitFrame::MODE_NORMAL, kStackSpace, kArgc);
3058 STATIC_ASSERT(kArgc == 4);
3059 if (kPassHandlesDirectly) {
3060 // When handles as passed directly we don't have to allocate extra
3061 // space for and pass an out parameter.
3062 __ mov(Operand(esp, 0 * kPointerSize), ebx); // name.
3063 __ mov(Operand(esp, 1 * kPointerSize), eax); // arguments pointer.
3064 } else {
3065 // The function expects three arguments to be passed but we allocate
3066 // four to get space for the output cell. The argument slots are filled
3067 // as follows:
3068 //
3069 // 3: output cell
3070 // 2: arguments pointer
3071 // 1: name
3072 // 0: pointer to the output cell
3073 //
3074 // Note that this is one more "argument" than the function expects
3075 // so the out cell will have to be popped explicitly after returning
3076 // from the function.
3077 __ mov(Operand(esp, 1 * kPointerSize), ebx); // name.
3078 __ mov(Operand(esp, 2 * kPointerSize), eax); // arguments pointer.
3079 __ mov(ebx, esp);
3080 __ add(Operand(ebx), Immediate(3 * kPointerSize));
3081 __ mov(Operand(esp, 0 * kPointerSize), ebx); // output
3082 __ mov(Operand(esp, 3 * kPointerSize), Immediate(0)); // out cell.
3083 }
3084 // Call the api function!
3085 __ call(fun()->address(), RelocInfo::RUNTIME_ENTRY);
3086 // Check if the function scheduled an exception.
3087 ExternalReference scheduled_exception_address =
3088 ExternalReference::scheduled_exception_address();
3089 __ cmp(Operand::StaticVariable(scheduled_exception_address),
3090 Immediate(Factory::the_hole_value()));
3091 __ j(not_equal, &promote_scheduled_exception, not_taken);
3092 if (!kPassHandlesDirectly) {
3093 // The returned value is a pointer to the handle holding the result.
3094 // Dereference this to get to the location.
3095 __ mov(eax, Operand(eax, 0));
3096 }
3097 // Check if the result handle holds 0.
3098 __ test(eax, Operand(eax));
3099 __ j(zero, &empty_handle, not_taken);
3100 // It was non-zero. Dereference to get the result value.
3101 __ mov(eax, Operand(eax, 0));
3102 __ bind(&prologue);
3103 __ LeaveExitFrame(ExitFrame::MODE_NORMAL);
3104 __ ret(0);
3105 __ bind(&promote_scheduled_exception);
3106 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
3107 __ bind(&empty_handle);
3108 // It was zero; the result is undefined.
3109 __ mov(eax, Factory::undefined_value());
3110 __ jmp(&prologue);
3111 }
3112
3113
3114 void CEntryStub::GenerateCore(MacroAssembler* masm,
3115 Label* throw_normal_exception,
3116 Label* throw_termination_exception,
3117 Label* throw_out_of_memory_exception,
3118 bool do_gc,
3119 bool always_allocate_scope,
3120 int /* alignment_skew */) {
3121 // eax: result parameter for PerformGC, if any
3122 // ebx: pointer to C function (C callee-saved)
3123 // ebp: frame pointer (restored after C call)
3124 // esp: stack pointer (restored after C call)
3125 // edi: number of arguments including receiver (C callee-saved)
3126 // esi: pointer to the first argument (C callee-saved)
3127
3128 // Result returned in eax, or eax+edx if result_size_ is 2.
3129
3130 // Check stack alignment.
3131 if (FLAG_debug_code) {
3132 __ CheckStackAlignment();
3133 }
3134
3135 if (do_gc) {
3136 // Pass failure code returned from last attempt as first argument to
3137 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
3138 // stack alignment is known to be correct. This function takes one argument
3139 // which is passed on the stack, and we know that the stack has been
3140 // prepared to pass at least one argument.
3141 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
3142 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3143 }
3144
3145 ExternalReference scope_depth =
3146 ExternalReference::heap_always_allocate_scope_depth();
3147 if (always_allocate_scope) {
3148 __ inc(Operand::StaticVariable(scope_depth));
3149 }
3150
3151 // Call C function.
3152 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
3153 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
3154 __ call(Operand(ebx));
3155 // Result is in eax or edx:eax - do not destroy these registers!
3156
3157 if (always_allocate_scope) {
3158 __ dec(Operand::StaticVariable(scope_depth));
3159 }
3160
3161 // Make sure we're not trying to return 'the hole' from the runtime
3162 // call as this may lead to crashes in the IC code later.
3163 if (FLAG_debug_code) {
3164 Label okay;
3165 __ cmp(eax, Factory::the_hole_value());
3166 __ j(not_equal, &okay);
3167 __ int3();
3168 __ bind(&okay);
3169 }
3170
3171 // Check for failure result.
3172 Label failure_returned;
3173 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3174 __ lea(ecx, Operand(eax, 1));
3175 // Lower 2 bits of ecx are 0 iff eax has failure tag.
3176 __ test(ecx, Immediate(kFailureTagMask));
3177 __ j(zero, &failure_returned, not_taken);
3178
3179 // Exit the JavaScript to C++ exit frame.
3180 __ LeaveExitFrame(mode_);
3181 __ ret(0);
3182
3183 // Handling of failure.
3184 __ bind(&failure_returned);
3185
3186 Label retry;
3187 // If the returned exception is RETRY_AFTER_GC continue at retry label
3188 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3189 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3190 __ j(zero, &retry, taken);
3191
3192 // Special handling of out of memory exceptions.
3193 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
3194 __ j(equal, throw_out_of_memory_exception);
3195
3196 // Retrieve the pending exception and clear the variable.
3197 ExternalReference pending_exception_address(Top::k_pending_exception_address);
3198 __ mov(eax, Operand::StaticVariable(pending_exception_address));
3199 __ mov(edx,
3200 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
3201 __ mov(Operand::StaticVariable(pending_exception_address), edx);
3202
3203 // Special handling of termination exceptions which are uncatchable
3204 // by javascript code.
3205 __ cmp(eax, Factory::termination_exception());
3206 __ j(equal, throw_termination_exception);
3207
3208 // Handle normal exception.
3209 __ jmp(throw_normal_exception);
3210
3211 // Retry.
3212 __ bind(&retry);
3213 }
3214
3215
3216 void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3217 UncatchableExceptionType type) {
3218 // Adjust this code if not the case.
3219 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
3220
3221 // Drop sp to the top stack handler.
3222 ExternalReference handler_address(Top::k_handler_address);
3223 __ mov(esp, Operand::StaticVariable(handler_address));
3224
3225 // Unwind the handlers until the ENTRY handler is found.
3226 Label loop, done;
3227 __ bind(&loop);
3228 // Load the type of the current stack handler.
3229 const int kStateOffset = StackHandlerConstants::kStateOffset;
3230 __ cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
3231 __ j(equal, &done);
3232 // Fetch the next handler in the list.
3233 const int kNextOffset = StackHandlerConstants::kNextOffset;
3234 __ mov(esp, Operand(esp, kNextOffset));
3235 __ jmp(&loop);
3236 __ bind(&done);
3237
3238 // Set the top handler address to next handler past the current ENTRY handler.
3239 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3240 __ pop(Operand::StaticVariable(handler_address));
3241
3242 if (type == OUT_OF_MEMORY) {
3243 // Set external caught exception to false.
3244 ExternalReference external_caught(Top::k_external_caught_exception_address);
3245 __ mov(eax, false);
3246 __ mov(Operand::StaticVariable(external_caught), eax);
3247
3248 // Set pending exception and eax to out of memory exception.
3249 ExternalReference pending_exception(Top::k_pending_exception_address);
3250 __ mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
3251 __ mov(Operand::StaticVariable(pending_exception), eax);
3252 }
3253
3254 // Clear the context pointer.
3255 __ xor_(esi, Operand(esi));
3256
3257 // Restore fp from handler and discard handler state.
3258 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
3259 __ pop(ebp);
3260 __ pop(edx); // State.
3261
3262 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
3263 __ ret(0);
3264 }
3265
3266
3267 void CEntryStub::Generate(MacroAssembler* masm) {
3268 // eax: number of arguments including receiver
3269 // ebx: pointer to C function (C callee-saved)
3270 // ebp: frame pointer (restored after C call)
3271 // esp: stack pointer (restored after C call)
3272 // esi: current context (C callee-saved)
3273 // edi: JS function of the caller (C callee-saved)
3274
3275 // NOTE: Invocations of builtins may return failure objects instead
3276 // of a proper result. The builtin entry handles this by performing
3277 // a garbage collection and retrying the builtin (twice).
3278
3279 // Enter the exit frame that transitions from JavaScript to C++.
3280 __ EnterExitFrame(mode_);
3281
3282 // eax: result parameter for PerformGC, if any (setup below)
3283 // ebx: pointer to builtin function (C callee-saved)
3284 // ebp: frame pointer (restored after C call)
3285 // esp: stack pointer (restored after C call)
3286 // edi: number of arguments including receiver (C callee-saved)
3287 // esi: argv pointer (C callee-saved)
3288
3289 Label throw_normal_exception;
3290 Label throw_termination_exception;
3291 Label throw_out_of_memory_exception;
3292
3293 // Call into the runtime system.
3294 GenerateCore(masm,
3295 &throw_normal_exception,
3296 &throw_termination_exception,
3297 &throw_out_of_memory_exception,
3298 false,
3299 false);
3300
3301 // Do space-specific GC and retry runtime call.
3302 GenerateCore(masm,
3303 &throw_normal_exception,
3304 &throw_termination_exception,
3305 &throw_out_of_memory_exception,
3306 true,
3307 false);
3308
3309 // Do full GC and retry runtime call one final time.
3310 Failure* failure = Failure::InternalError();
3311 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
3312 GenerateCore(masm,
3313 &throw_normal_exception,
3314 &throw_termination_exception,
3315 &throw_out_of_memory_exception,
3316 true,
3317 true);
3318
3319 __ bind(&throw_out_of_memory_exception);
3320 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3321
3322 __ bind(&throw_termination_exception);
3323 GenerateThrowUncatchable(masm, TERMINATION);
3324
3325 __ bind(&throw_normal_exception);
3326 GenerateThrowTOS(masm);
3327 }
3328
3329
3330 void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3331 Label invoke, exit;
3332 #ifdef ENABLE_LOGGING_AND_PROFILING
3333 Label not_outermost_js, not_outermost_js_2;
3334 #endif
3335
3336 // Setup frame.
3337 __ push(ebp);
3338 __ mov(ebp, Operand(esp));
3339
3340 // Push marker in two places.
3341 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3342 __ push(Immediate(Smi::FromInt(marker))); // context slot
3343 __ push(Immediate(Smi::FromInt(marker))); // function slot
3344 // Save callee-saved registers (C calling conventions).
3345 __ push(edi);
3346 __ push(esi);
3347 __ push(ebx);
3348
3349 // Save copies of the top frame descriptor on the stack.
3350 ExternalReference c_entry_fp(Top::k_c_entry_fp_address);
3351 __ push(Operand::StaticVariable(c_entry_fp));
3352
3353 #ifdef ENABLE_LOGGING_AND_PROFILING
3354 // If this is the outermost JS call, set js_entry_sp value.
3355 ExternalReference js_entry_sp(Top::k_js_entry_sp_address);
3356 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
3357 __ j(not_equal, &not_outermost_js);
3358 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
3359 __ bind(&not_outermost_js);
3360 #endif
3361
3362 // Call a faked try-block that does the invoke.
3363 __ call(&invoke);
3364
3365 // Caught exception: Store result (exception) in the pending
3366 // exception field in the JSEnv and return a failure sentinel.
3367 ExternalReference pending_exception(Top::k_pending_exception_address);
3368 __ mov(Operand::StaticVariable(pending_exception), eax);
3369 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
3370 __ jmp(&exit);
3371
3372 // Invoke: Link this frame into the handler chain.
3373 __ bind(&invoke);
3374 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3375
3376 // Clear any pending exceptions.
3377 __ mov(edx,
3378 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
3379 __ mov(Operand::StaticVariable(pending_exception), edx);
3380
3381 // Fake a receiver (NULL).
3382 __ push(Immediate(0)); // receiver
3383
3384 // Invoke the function by calling through JS entry trampoline
3385 // builtin and pop the faked function when we return. Notice that we
3386 // cannot store a reference to the trampoline code directly in this
3387 // stub, because the builtin stubs may not have been generated yet.
3388 if (is_construct) {
3389 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
3390 __ mov(edx, Immediate(construct_entry));
3391 } else {
3392 ExternalReference entry(Builtins::JSEntryTrampoline);
3393 __ mov(edx, Immediate(entry));
3394 }
3395 __ mov(edx, Operand(edx, 0)); // deref address
3396 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
3397 __ call(Operand(edx));
3398
3399 // Unlink this frame from the handler chain.
3400 __ pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
3401 // Pop next_sp.
3402 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
3403
3404 #ifdef ENABLE_LOGGING_AND_PROFILING
3405 // If current EBP value is the same as js_entry_sp value, it means that
3406 // the current function is the outermost.
3407 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
3408 __ j(not_equal, &not_outermost_js_2);
3409 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
3410 __ bind(&not_outermost_js_2);
3411 #endif
3412
3413 // Restore the top frame descriptor from the stack.
3414 __ bind(&exit);
3415 __ pop(Operand::StaticVariable(ExternalReference(Top::k_c_entry_fp_address)));
3416
3417 // Restore callee-saved registers (C calling conventions).
3418 __ pop(ebx);
3419 __ pop(esi);
3420 __ pop(edi);
3421 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
3422
3423 // Restore frame pointer and return.
3424 __ pop(ebp);
3425 __ ret(0);
3426 }
3427
3428
3429 void InstanceofStub::Generate(MacroAssembler* masm) {
3430 // Get the object - go slow case if it's a smi.
3431 Label slow;
3432 __ mov(eax, Operand(esp, 2 * kPointerSize)); // 2 ~ return address, function
3433 __ test(eax, Immediate(kSmiTagMask));
3434 __ j(zero, &slow, not_taken);
3435
3436 // Check that the left hand is a JS object.
3437 __ IsObjectJSObjectType(eax, eax, edx, &slow);
3438
3439 // Get the prototype of the function.
3440 __ mov(edx, Operand(esp, 1 * kPointerSize)); // 1 ~ return address
3441 // edx is function, eax is map.
3442
3443 // Look up the function and the map in the instanceof cache.
3444 Label miss;
3445 ExternalReference roots_address = ExternalReference::roots_address();
3446 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
3447 __ cmp(edx, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3448 __ j(not_equal, &miss);
3449 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
3450 __ cmp(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3451 __ j(not_equal, &miss);
3452 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3453 __ mov(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3454 __ ret(2 * kPointerSize);
3455
3456 __ bind(&miss);
3457 __ TryGetFunctionPrototype(edx, ebx, ecx, &slow);
3458
3459 // Check that the function prototype is a JS object.
3460 __ test(ebx, Immediate(kSmiTagMask));
3461 __ j(zero, &slow, not_taken);
3462 __ IsObjectJSObjectType(ebx, ecx, ecx, &slow);
3463
3464 // Register mapping:
3465 // eax is object map.
3466 // edx is function.
3467 // ebx is function prototype.
3468 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
3469 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3470 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
3471 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), edx);
3472
3473 __ mov(ecx, FieldOperand(eax, Map::kPrototypeOffset));
3474
3475 // Loop through the prototype chain looking for the function prototype.
3476 Label loop, is_instance, is_not_instance;
3477 __ bind(&loop);
3478 __ cmp(ecx, Operand(ebx));
3479 __ j(equal, &is_instance);
3480 __ cmp(Operand(ecx), Immediate(Factory::null_value()));
3481 __ j(equal, &is_not_instance);
3482 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
3483 __ mov(ecx, FieldOperand(ecx, Map::kPrototypeOffset));
3484 __ jmp(&loop);
3485
3486 __ bind(&is_instance);
3487 __ Set(eax, Immediate(0));
3488 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3489 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3490 __ ret(2 * kPointerSize);
3491
3492 __ bind(&is_not_instance);
3493 __ Set(eax, Immediate(Smi::FromInt(1)));
3494 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3495 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3496 __ ret(2 * kPointerSize);
3497
3498 // Slow-case: Go through the JavaScript implementation.
3499 __ bind(&slow);
3500 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
3501 }
3502
3503
3504 int CompareStub::MinorKey() {
3505 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3506 // stubs the never NaN NaN condition is only taken into account if the
3507 // condition is equals.
3508 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
3509 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3510 return ConditionField::encode(static_cast<unsigned>(cc_))
3511 | RegisterField::encode(false) // lhs_ and rhs_ are not used
3512 | StrictField::encode(strict_)
3513 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
3514 | IncludeNumberCompareField::encode(include_number_compare_);
3515 }
3516
3517
3518 // Unfortunately you have to run without snapshots to see most of these
3519 // names in the profile since most compare stubs end up in the snapshot.
3520 const char* CompareStub::GetName() {
3521 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3522
3523 if (name_ != NULL) return name_;
3524 const int kMaxNameLength = 100;
3525 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
3526 if (name_ == NULL) return "OOM";
3527
3528 const char* cc_name;
3529 switch (cc_) {
3530 case less: cc_name = "LT"; break;
3531 case greater: cc_name = "GT"; break;
3532 case less_equal: cc_name = "LE"; break;
3533 case greater_equal: cc_name = "GE"; break;
3534 case equal: cc_name = "EQ"; break;
3535 case not_equal: cc_name = "NE"; break;
3536 default: cc_name = "UnknownCondition"; break;
3537 }
3538
3539 const char* strict_name = "";
3540 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
3541 strict_name = "_STRICT";
3542 }
3543
3544 const char* never_nan_nan_name = "";
3545 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
3546 never_nan_nan_name = "_NO_NAN";
3547 }
3548
3549 const char* include_number_compare_name = "";
3550 if (!include_number_compare_) {
3551 include_number_compare_name = "_NO_NUMBER";
3552 }
3553
3554 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
3555 "CompareStub_%s%s%s%s",
3556 cc_name,
3557 strict_name,
3558 never_nan_nan_name,
3559 include_number_compare_name);
3560 return name_;
3561 }
3562
3563
3564 // -------------------------------------------------------------------------
3565 // StringCharCodeAtGenerator
3566
3567 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3568 Label flat_string;
3569 Label ascii_string;
3570 Label got_char_code;
3571
3572 // If the receiver is a smi trigger the non-string case.
3573 STATIC_ASSERT(kSmiTag == 0);
3574 __ test(object_, Immediate(kSmiTagMask));
3575 __ j(zero, receiver_not_string_);
3576
3577 // Fetch the instance type of the receiver into result register.
3578 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3579 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3580 // If the receiver is not a string trigger the non-string case.
3581 __ test(result_, Immediate(kIsNotStringMask));
3582 __ j(not_zero, receiver_not_string_);
3583
3584 // If the index is non-smi trigger the non-smi case.
3585 STATIC_ASSERT(kSmiTag == 0);
3586 __ test(index_, Immediate(kSmiTagMask));
3587 __ j(not_zero, &index_not_smi_);
3588
3589 // Put smi-tagged index into scratch register.
3590 __ mov(scratch_, index_);
3591 __ bind(&got_smi_index_);
3592
3593 // Check for index out of range.
3594 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
3595 __ j(above_equal, index_out_of_range_);
3596
3597 // We need special handling for non-flat strings.
3598 STATIC_ASSERT(kSeqStringTag == 0);
3599 __ test(result_, Immediate(kStringRepresentationMask));
3600 __ j(zero, &flat_string);
3601
3602 // Handle non-flat strings.
3603 __ test(result_, Immediate(kIsConsStringMask));
3604 __ j(zero, &call_runtime_);
3605
3606 // ConsString.
3607 // Check whether the right hand side is the empty string (i.e. if
3608 // this is really a flat string in a cons string). If that is not
3609 // the case we would rather go to the runtime system now to flatten
3610 // the string.
3611 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
3612 Immediate(Factory::empty_string()));
3613 __ j(not_equal, &call_runtime_);
3614 // Get the first of the two strings and load its instance type.
3615 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
3616 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3617 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3618 // If the first cons component is also non-flat, then go to runtime.
3619 STATIC_ASSERT(kSeqStringTag == 0);
3620 __ test(result_, Immediate(kStringRepresentationMask));
3621 __ j(not_zero, &call_runtime_);
3622
3623 // Check for 1-byte or 2-byte string.
3624 __ bind(&flat_string);
3625 STATIC_ASSERT(kAsciiStringTag != 0);
3626 __ test(result_, Immediate(kStringEncodingMask));
3627 __ j(not_zero, &ascii_string);
3628
3629 // 2-byte string.
3630 // Load the 2-byte character code into the result register.
3631 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3632 __ movzx_w(result_, FieldOperand(object_,
3633 scratch_, times_1, // Scratch is smi-tagged.
3634 SeqTwoByteString::kHeaderSize));
3635 __ jmp(&got_char_code);
3636
3637 // ASCII string.
3638 // Load the byte into the result register.
3639 __ bind(&ascii_string);
3640 __ SmiUntag(scratch_);
3641 __ movzx_b(result_, FieldOperand(object_,
3642 scratch_, times_1,
3643 SeqAsciiString::kHeaderSize));
3644 __ bind(&got_char_code);
3645 __ SmiTag(result_);
3646 __ bind(&exit_);
3647 }
3648
3649
3650 void StringCharCodeAtGenerator::GenerateSlow(
3651 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3652 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3653
3654 // Index is not a smi.
3655 __ bind(&index_not_smi_);
3656 // If index is a heap number, try converting it to an integer.
3657 __ CheckMap(index_, Factory::heap_number_map(), index_not_number_, true);
3658 call_helper.BeforeCall(masm);
3659 __ push(object_);
3660 __ push(index_);
3661 __ push(index_); // Consumed by runtime conversion function.
3662 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3663 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3664 } else {
3665 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3666 // NumberToSmi discards numbers that are not exact integers.
3667 __ CallRuntime(Runtime::kNumberToSmi, 1);
3668 }
3669 if (!scratch_.is(eax)) {
3670 // Save the conversion result before the pop instructions below
3671 // have a chance to overwrite it.
3672 __ mov(scratch_, eax);
3673 }
3674 __ pop(index_);
3675 __ pop(object_);
3676 // Reload the instance type.
3677 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3678 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3679 call_helper.AfterCall(masm);
3680 // If index is still not a smi, it must be out of range.
3681 STATIC_ASSERT(kSmiTag == 0);
3682 __ test(scratch_, Immediate(kSmiTagMask));
3683 __ j(not_zero, index_out_of_range_);
3684 // Otherwise, return to the fast path.
3685 __ jmp(&got_smi_index_);
3686
3687 // Call runtime. We get here when the receiver is a string and the
3688 // index is a number, but the code of getting the actual character
3689 // is too complex (e.g., when the string needs to be flattened).
3690 __ bind(&call_runtime_);
3691 call_helper.BeforeCall(masm);
3692 __ push(object_);
3693 __ push(index_);
3694 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3695 if (!result_.is(eax)) {
3696 __ mov(result_, eax);
3697 }
3698 call_helper.AfterCall(masm);
3699 __ jmp(&exit_);
3700
3701 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3702 }
3703
3704
3705 // -------------------------------------------------------------------------
3706 // StringCharFromCodeGenerator
3707
3708 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3709 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3710 STATIC_ASSERT(kSmiTag == 0);
3711 STATIC_ASSERT(kSmiShiftSize == 0);
3712 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3713 __ test(code_,
3714 Immediate(kSmiTagMask |
3715 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3716 __ j(not_zero, &slow_case_, not_taken);
3717
3718 __ Set(result_, Immediate(Factory::single_character_string_cache()));
3719 STATIC_ASSERT(kSmiTag == 0);
3720 STATIC_ASSERT(kSmiTagSize == 1);
3721 STATIC_ASSERT(kSmiShiftSize == 0);
3722 // At this point code register contains smi tagged ascii char code.
3723 __ mov(result_, FieldOperand(result_,
3724 code_, times_half_pointer_size,
3725 FixedArray::kHeaderSize));
3726 __ cmp(result_, Factory::undefined_value());
3727 __ j(equal, &slow_case_, not_taken);
3728 __ bind(&exit_);
3729 }
3730
3731
3732 void StringCharFromCodeGenerator::GenerateSlow(
3733 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3734 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3735
3736 __ bind(&slow_case_);
3737 call_helper.BeforeCall(masm);
3738 __ push(code_);
3739 __ CallRuntime(Runtime::kCharFromCode, 1);
3740 if (!result_.is(eax)) {
3741 __ mov(result_, eax);
3742 }
3743 call_helper.AfterCall(masm);
3744 __ jmp(&exit_);
3745
3746 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3747 }
3748
3749
3750 // -------------------------------------------------------------------------
3751 // StringCharAtGenerator
3752
3753 void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3754 char_code_at_generator_.GenerateFast(masm);
3755 char_from_code_generator_.GenerateFast(masm);
3756 }
3757
3758
3759 void StringCharAtGenerator::GenerateSlow(
3760 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3761 char_code_at_generator_.GenerateSlow(masm, call_helper);
3762 char_from_code_generator_.GenerateSlow(masm, call_helper);
3763 }
3764
3765
3766 void StringAddStub::Generate(MacroAssembler* masm) {
3767 Label string_add_runtime;
3768
3769 // Load the two arguments.
3770 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
3771 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
3772
3773 // Make sure that both arguments are strings if not known in advance.
3774 if (string_check_) {
3775 __ test(eax, Immediate(kSmiTagMask));
3776 __ j(zero, &string_add_runtime);
3777 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
3778 __ j(above_equal, &string_add_runtime);
3779
3780 // First argument is a a string, test second.
3781 __ test(edx, Immediate(kSmiTagMask));
3782 __ j(zero, &string_add_runtime);
3783 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
3784 __ j(above_equal, &string_add_runtime);
3785 }
3786
3787 // Both arguments are strings.
3788 // eax: first string
3789 // edx: second string
3790 // Check if either of the strings are empty. In that case return the other.
3791 Label second_not_zero_length, both_not_zero_length;
3792 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
3793 STATIC_ASSERT(kSmiTag == 0);
3794 __ test(ecx, Operand(ecx));
3795 __ j(not_zero, &second_not_zero_length);
3796 // Second string is empty, result is first string which is already in eax.
3797 __ IncrementCounter(&Counters::string_add_native, 1);
3798 __ ret(2 * kPointerSize);
3799 __ bind(&second_not_zero_length);
3800 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3801 STATIC_ASSERT(kSmiTag == 0);
3802 __ test(ebx, Operand(ebx));
3803 __ j(not_zero, &both_not_zero_length);
3804 // First string is empty, result is second string which is in edx.
3805 __ mov(eax, edx);
3806 __ IncrementCounter(&Counters::string_add_native, 1);
3807 __ ret(2 * kPointerSize);
3808
3809 // Both strings are non-empty.
3810 // eax: first string
3811 // ebx: length of first string as a smi
3812 // ecx: length of second string as a smi
3813 // edx: second string
3814 // Look at the length of the result of adding the two strings.
3815 Label string_add_flat_result, longer_than_two;
3816 __ bind(&both_not_zero_length);
3817 __ add(ebx, Operand(ecx));
3818 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
3819 // Handle exceptionally long strings in the runtime system.
3820 __ j(overflow, &string_add_runtime);
3821 // Use the runtime system when adding two one character strings, as it
3822 // contains optimizations for this specific case using the symbol table.
3823 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
3824 __ j(not_equal, &longer_than_two);
3825
3826 // Check that both strings are non-external ascii strings.
3827 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
3828 &string_add_runtime);
3829
3830 // Get the two characters forming the sub string.
3831 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
3832 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
3833
3834 // Try to lookup two character string in symbol table. If it is not found
3835 // just allocate a new one.
3836 Label make_two_character_string, make_flat_ascii_string;
3837 StringHelper::GenerateTwoCharacterSymbolTableProbe(
3838 masm, ebx, ecx, eax, edx, edi, &make_two_character_string);
3839 __ IncrementCounter(&Counters::string_add_native, 1);
3840 __ ret(2 * kPointerSize);
3841
3842 __ bind(&make_two_character_string);
3843 __ Set(ebx, Immediate(Smi::FromInt(2)));
3844 __ jmp(&make_flat_ascii_string);
3845
3846 __ bind(&longer_than_two);
3847 // Check if resulting string will be flat.
3848 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
3849 __ j(below, &string_add_flat_result);
3850
3851 // If result is not supposed to be flat allocate a cons string object. If both
3852 // strings are ascii the result is an ascii cons string.
3853 Label non_ascii, allocated, ascii_data;
3854 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
3855 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
3856 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
3857 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
3858 __ and_(ecx, Operand(edi));
3859 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3860 __ test(ecx, Immediate(kAsciiStringTag));
3861 __ j(zero, &non_ascii);
3862 __ bind(&ascii_data);
3863 // Allocate an acsii cons string.
3864 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
3865 __ bind(&allocated);
3866 // Fill the fields of the cons string.
3867 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
3868 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
3869 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
3870 Immediate(String::kEmptyHashField));
3871 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
3872 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
3873 __ mov(eax, ecx);
3874 __ IncrementCounter(&Counters::string_add_native, 1);
3875 __ ret(2 * kPointerSize);
3876 __ bind(&non_ascii);
3877 // At least one of the strings is two-byte. Check whether it happens
3878 // to contain only ascii characters.
3879 // ecx: first instance type AND second instance type.
3880 // edi: second instance type.
3881 __ test(ecx, Immediate(kAsciiDataHintMask));
3882 __ j(not_zero, &ascii_data);
3883 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3884 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3885 __ xor_(edi, Operand(ecx));
3886 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
3887 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
3888 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
3889 __ j(equal, &ascii_data);
3890 // Allocate a two byte cons string.
3891 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
3892 __ jmp(&allocated);
3893
3894 // Handle creating a flat result. First check that both strings are not
3895 // external strings.
3896 // eax: first string
3897 // ebx: length of resulting flat string as a smi
3898 // edx: second string
3899 __ bind(&string_add_flat_result);
3900 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3901 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3902 __ and_(ecx, kStringRepresentationMask);
3903 __ cmp(ecx, kExternalStringTag);
3904 __ j(equal, &string_add_runtime);
3905 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3906 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3907 __ and_(ecx, kStringRepresentationMask);
3908 __ cmp(ecx, kExternalStringTag);
3909 __ j(equal, &string_add_runtime);
3910 // Now check if both strings are ascii strings.
3911 // eax: first string
3912 // ebx: length of resulting flat string as a smi
3913 // edx: second string
3914 Label non_ascii_string_add_flat_result;
3915 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3916 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3917 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
3918 __ j(zero, &non_ascii_string_add_flat_result);
3919 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3920 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
3921 __ j(zero, &string_add_runtime);
3922
3923 __ bind(&make_flat_ascii_string);
3924 // Both strings are ascii strings. As they are short they are both flat.
3925 // ebx: length of resulting flat string as a smi
3926 __ SmiUntag(ebx);
3927 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
3928 // eax: result string
3929 __ mov(ecx, eax);
3930 // Locate first character of result.
3931 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3932 // Load first argument and locate first character.
3933 __ mov(edx, Operand(esp, 2 * kPointerSize));
3934 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
3935 __ SmiUntag(edi);
3936 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3937 // eax: result string
3938 // ecx: first character of result
3939 // edx: first char of first argument
3940 // edi: length of first argument
3941 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
3942 // Load second argument and locate first character.
3943 __ mov(edx, Operand(esp, 1 * kPointerSize));
3944 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
3945 __ SmiUntag(edi);
3946 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3947 // eax: result string
3948 // ecx: next character of result
3949 // edx: first char of second argument
3950 // edi: length of second argument
3951 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
3952 __ IncrementCounter(&Counters::string_add_native, 1);
3953 __ ret(2 * kPointerSize);
3954
3955 // Handle creating a flat two byte result.
3956 // eax: first string - known to be two byte
3957 // ebx: length of resulting flat string as a smi
3958 // edx: second string
3959 __ bind(&non_ascii_string_add_flat_result);
3960 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3961 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
3962 __ j(not_zero, &string_add_runtime);
3963 // Both strings are two byte strings. As they are short they are both
3964 // flat.
3965 __ SmiUntag(ebx);
3966 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
3967 // eax: result string
3968 __ mov(ecx, eax);
3969 // Locate first character of result.
3970 __ add(Operand(ecx),
3971 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3972 // Load first argument and locate first character.
3973 __ mov(edx, Operand(esp, 2 * kPointerSize));
3974 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
3975 __ SmiUntag(edi);
3976 __ add(Operand(edx),
3977 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3978 // eax: result string
3979 // ecx: first character of result
3980 // edx: first char of first argument
3981 // edi: length of first argument
3982 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
3983 // Load second argument and locate first character.
3984 __ mov(edx, Operand(esp, 1 * kPointerSize));
3985 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
3986 __ SmiUntag(edi);
3987 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3988 // eax: result string
3989 // ecx: next character of result
3990 // edx: first char of second argument
3991 // edi: length of second argument
3992 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
3993 __ IncrementCounter(&Counters::string_add_native, 1);
3994 __ ret(2 * kPointerSize);
3995
3996 // Just jump to runtime to add the two strings.
3997 __ bind(&string_add_runtime);
3998 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
3999 }
4000
4001
4002 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4003 Register dest,
4004 Register src,
4005 Register count,
4006 Register scratch,
4007 bool ascii) {
4008 Label loop;
4009 __ bind(&loop);
4010 // This loop just copies one character at a time, as it is only used for very
4011 // short strings.
4012 if (ascii) {
4013 __ mov_b(scratch, Operand(src, 0));
4014 __ mov_b(Operand(dest, 0), scratch);
4015 __ add(Operand(src), Immediate(1));
4016 __ add(Operand(dest), Immediate(1));
4017 } else {
4018 __ mov_w(scratch, Operand(src, 0));
4019 __ mov_w(Operand(dest, 0), scratch);
4020 __ add(Operand(src), Immediate(2));
4021 __ add(Operand(dest), Immediate(2));
4022 }
4023 __ sub(Operand(count), Immediate(1));
4024 __ j(not_zero, &loop);
4025 }
4026
4027
4028 void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
4029 Register dest,
4030 Register src,
4031 Register count,
4032 Register scratch,
4033 bool ascii) {
4034 // Copy characters using rep movs of doublewords.
4035 // The destination is aligned on a 4 byte boundary because we are
4036 // copying to the beginning of a newly allocated string.
4037 ASSERT(dest.is(edi)); // rep movs destination
4038 ASSERT(src.is(esi)); // rep movs source
4039 ASSERT(count.is(ecx)); // rep movs count
4040 ASSERT(!scratch.is(dest));
4041 ASSERT(!scratch.is(src));
4042 ASSERT(!scratch.is(count));
4043
4044 // Nothing to do for zero characters.
4045 Label done;
4046 __ test(count, Operand(count));
4047 __ j(zero, &done);
4048
4049 // Make count the number of bytes to copy.
4050 if (!ascii) {
4051 __ shl(count, 1);
4052 }
4053
4054 // Don't enter the rep movs if there are less than 4 bytes to copy.
4055 Label last_bytes;
4056 __ test(count, Immediate(~3));
4057 __ j(zero, &last_bytes);
4058
4059 // Copy from edi to esi using rep movs instruction.
4060 __ mov(scratch, count);
4061 __ sar(count, 2); // Number of doublewords to copy.
4062 __ cld();
4063 __ rep_movs();
4064
4065 // Find number of bytes left.
4066 __ mov(count, scratch);
4067 __ and_(count, 3);
4068
4069 // Check if there are more bytes to copy.
4070 __ bind(&last_bytes);
4071 __ test(count, Operand(count));
4072 __ j(zero, &done);
4073
4074 // Copy remaining characters.
4075 Label loop;
4076 __ bind(&loop);
4077 __ mov_b(scratch, Operand(src, 0));
4078 __ mov_b(Operand(dest, 0), scratch);
4079 __ add(Operand(src), Immediate(1));
4080 __ add(Operand(dest), Immediate(1));
4081 __ sub(Operand(count), Immediate(1));
4082 __ j(not_zero, &loop);
4083
4084 __ bind(&done);
4085 }
4086
4087
4088 void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4089 Register c1,
4090 Register c2,
4091 Register scratch1,
4092 Register scratch2,
4093 Register scratch3,
4094 Label* not_found) {
4095 // Register scratch3 is the general scratch register in this function.
4096 Register scratch = scratch3;
4097
4098 // Make sure that both characters are not digits as such strings has a
4099 // different hash algorithm. Don't try to look for these in the symbol table.
4100 Label not_array_index;
4101 __ mov(scratch, c1);
4102 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
4103 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
4104 __ j(above, &not_array_index);
4105 __ mov(scratch, c2);
4106 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
4107 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
4108 __ j(below_equal, not_found);
4109
4110 __ bind(&not_array_index);
4111 // Calculate the two character string hash.
4112 Register hash = scratch1;
4113 GenerateHashInit(masm, hash, c1, scratch);
4114 GenerateHashAddCharacter(masm, hash, c2, scratch);
4115 GenerateHashGetHash(masm, hash, scratch);
4116
4117 // Collect the two characters in a register.
4118 Register chars = c1;
4119 __ shl(c2, kBitsPerByte);
4120 __ or_(chars, Operand(c2));
4121
4122 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4123 // hash: hash of two character string.
4124
4125 // Load the symbol table.
4126 Register symbol_table = c2;
4127 ExternalReference roots_address = ExternalReference::roots_address();
4128 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
4129 __ mov(symbol_table,
4130 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4131
4132 // Calculate capacity mask from the symbol table capacity.
4133 Register mask = scratch2;
4134 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4135 __ SmiUntag(mask);
4136 __ sub(Operand(mask), Immediate(1));
4137
4138 // Registers
4139 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4140 // hash: hash of two character string
4141 // symbol_table: symbol table
4142 // mask: capacity mask
4143 // scratch: -
4144
4145 // Perform a number of probes in the symbol table.
4146 static const int kProbes = 4;
4147 Label found_in_symbol_table;
4148 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
4149 for (int i = 0; i < kProbes; i++) {
4150 // Calculate entry in symbol table.
4151 __ mov(scratch, hash);
4152 if (i > 0) {
4153 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
4154 }
4155 __ and_(scratch, Operand(mask));
4156
4157 // Load the entry from the symbol table.
4158 Register candidate = scratch; // Scratch register contains candidate.
4159 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4160 __ mov(candidate,
4161 FieldOperand(symbol_table,
4162 scratch,
4163 times_pointer_size,
4164 SymbolTable::kElementsStartOffset));
4165
4166 // If entry is undefined no string with this hash can be found.
4167 __ cmp(candidate, Factory::undefined_value());
4168 __ j(equal, not_found);
4169
4170 // If length is not 2 the string is not a candidate.
4171 __ cmp(FieldOperand(candidate, String::kLengthOffset),
4172 Immediate(Smi::FromInt(2)));
4173 __ j(not_equal, &next_probe[i]);
4174
4175 // As we are out of registers save the mask on the stack and use that
4176 // register as a temporary.
4177 __ push(mask);
4178 Register temp = mask;
4179
4180 // Check that the candidate is a non-external ascii string.
4181 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
4182 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
4183 __ JumpIfInstanceTypeIsNotSequentialAscii(
4184 temp, temp, &next_probe_pop_mask[i]);
4185
4186 // Check if the two characters match.
4187 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4188 __ and_(temp, 0x0000ffff);
4189 __ cmp(chars, Operand(temp));
4190 __ j(equal, &found_in_symbol_table);
4191 __ bind(&next_probe_pop_mask[i]);
4192 __ pop(mask);
4193 __ bind(&next_probe[i]);
4194 }
4195
4196 // No matching 2 character string found by probing.
4197 __ jmp(not_found);
4198
4199 // Scratch register contains result when we fall through to here.
4200 Register result = scratch;
4201 __ bind(&found_in_symbol_table);
4202 __ pop(mask); // Pop saved mask from the stack.
4203 if (!result.is(eax)) {
4204 __ mov(eax, result);
4205 }
4206 }
4207
4208
4209 void StringHelper::GenerateHashInit(MacroAssembler* masm,
4210 Register hash,
4211 Register character,
4212 Register scratch) {
4213 // hash = character + (character << 10);
4214 __ mov(hash, character);
4215 __ shl(hash, 10);
4216 __ add(hash, Operand(character));
4217 // hash ^= hash >> 6;
4218 __ mov(scratch, hash);
4219 __ sar(scratch, 6);
4220 __ xor_(hash, Operand(scratch));
4221 }
4222
4223
4224 void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4225 Register hash,
4226 Register character,
4227 Register scratch) {
4228 // hash += character;
4229 __ add(hash, Operand(character));
4230 // hash += hash << 10;
4231 __ mov(scratch, hash);
4232 __ shl(scratch, 10);
4233 __ add(hash, Operand(scratch));
4234 // hash ^= hash >> 6;
4235 __ mov(scratch, hash);
4236 __ sar(scratch, 6);
4237 __ xor_(hash, Operand(scratch));
4238 }
4239
4240
4241 void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4242 Register hash,
4243 Register scratch) {
4244 // hash += hash << 3;
4245 __ mov(scratch, hash);
4246 __ shl(scratch, 3);
4247 __ add(hash, Operand(scratch));
4248 // hash ^= hash >> 11;
4249 __ mov(scratch, hash);
4250 __ sar(scratch, 11);
4251 __ xor_(hash, Operand(scratch));
4252 // hash += hash << 15;
4253 __ mov(scratch, hash);
4254 __ shl(scratch, 15);
4255 __ add(hash, Operand(scratch));
4256
4257 // if (hash == 0) hash = 27;
4258 Label hash_not_zero;
4259 __ test(hash, Operand(hash));
4260 __ j(not_zero, &hash_not_zero);
4261 __ mov(hash, Immediate(27));
4262 __ bind(&hash_not_zero);
4263 }
4264
4265
4266 void SubStringStub::Generate(MacroAssembler* masm) {
4267 Label runtime;
4268
4269 // Stack frame on entry.
4270 // esp[0]: return address
4271 // esp[4]: to
4272 // esp[8]: from
4273 // esp[12]: string
4274
4275 // Make sure first argument is a string.
4276 __ mov(eax, Operand(esp, 3 * kPointerSize));
4277 STATIC_ASSERT(kSmiTag == 0);
4278 __ test(eax, Immediate(kSmiTagMask));
4279 __ j(zero, &runtime);
4280 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
4281 __ j(NegateCondition(is_string), &runtime);
4282
4283 // eax: string
4284 // ebx: instance type
4285
4286 // Calculate length of sub string using the smi values.
4287 Label result_longer_than_two;
4288 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
4289 __ test(ecx, Immediate(kSmiTagMask));
4290 __ j(not_zero, &runtime);
4291 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
4292 __ test(edx, Immediate(kSmiTagMask));
4293 __ j(not_zero, &runtime);
4294 __ sub(ecx, Operand(edx));
4295 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
4296 Label return_eax;
4297 __ j(equal, &return_eax);
4298 // Special handling of sub-strings of length 1 and 2. One character strings
4299 // are handled in the runtime system (looked up in the single character
4300 // cache). Two character strings are looked for in the symbol cache.
4301 __ SmiUntag(ecx); // Result length is no longer smi.
4302 __ cmp(ecx, 2);
4303 __ j(greater, &result_longer_than_two);
4304 __ j(less, &runtime);
4305
4306 // Sub string of length 2 requested.
4307 // eax: string
4308 // ebx: instance type
4309 // ecx: sub string length (value is 2)
4310 // edx: from index (smi)
4311 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
4312
4313 // Get the two characters forming the sub string.
4314 __ SmiUntag(edx); // From index is no longer smi.
4315 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
4316 __ movzx_b(ecx,
4317 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
4318
4319 // Try to lookup two character string in symbol table.
4320 Label make_two_character_string;
4321 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4322 masm, ebx, ecx, eax, edx, edi, &make_two_character_string);
4323 __ ret(3 * kPointerSize);
4324
4325 __ bind(&make_two_character_string);
4326 // Setup registers for allocating the two character string.
4327 __ mov(eax, Operand(esp, 3 * kPointerSize));
4328 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
4329 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
4330 __ Set(ecx, Immediate(2));
4331
4332 __ bind(&result_longer_than_two);
4333 // eax: string
4334 // ebx: instance type
4335 // ecx: result string length
4336 // Check for flat ascii string
4337 Label non_ascii_flat;
4338 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
4339
4340 // Allocate the result.
4341 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
4342
4343 // eax: result string
4344 // ecx: result string length
4345 __ mov(edx, esi); // esi used by following code.
4346 // Locate first character of result.
4347 __ mov(edi, eax);
4348 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4349 // Load string argument and locate character of sub string start.
4350 __ mov(esi, Operand(esp, 3 * kPointerSize));
4351 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4352 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
4353 __ SmiUntag(ebx);
4354 __ add(esi, Operand(ebx));
4355
4356 // eax: result string
4357 // ecx: result length
4358 // edx: original value of esi
4359 // edi: first character of result
4360 // esi: character of sub string start
4361 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
4362 __ mov(esi, edx); // Restore esi.
4363 __ IncrementCounter(&Counters::sub_string_native, 1);
4364 __ ret(3 * kPointerSize);
4365
4366 __ bind(&non_ascii_flat);
4367 // eax: string
4368 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
4369 // ecx: result string length
4370 // Check for flat two byte string
4371 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
4372 __ j(not_equal, &runtime);
4373
4374 // Allocate the result.
4375 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
4376
4377 // eax: result string
4378 // ecx: result string length
4379 __ mov(edx, esi); // esi used by following code.
4380 // Locate first character of result.
4381 __ mov(edi, eax);
4382 __ add(Operand(edi),
4383 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4384 // Load string argument and locate character of sub string start.
4385 __ mov(esi, Operand(esp, 3 * kPointerSize));
4386 __ add(Operand(esi),
4387 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4388 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
4389 // As from is a smi it is 2 times the value which matches the size of a two
4390 // byte character.
4391 STATIC_ASSERT(kSmiTag == 0);
4392 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4393 __ add(esi, Operand(ebx));
4394
4395 // eax: result string
4396 // ecx: result length
4397 // edx: original value of esi
4398 // edi: first character of result
4399 // esi: character of sub string start
4400 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
4401 __ mov(esi, edx); // Restore esi.
4402
4403 __ bind(&return_eax);
4404 __ IncrementCounter(&Counters::sub_string_native, 1);
4405 __ ret(3 * kPointerSize);
4406
4407 // Just jump to runtime to create the sub string.
4408 __ bind(&runtime);
4409 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4410 }
4411
4412
4413 void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4414 Register left,
4415 Register right,
4416 Register scratch1,
4417 Register scratch2,
4418 Register scratch3) {
4419 Label result_not_equal;
4420 Label result_greater;
4421 Label compare_lengths;
4422
4423 __ IncrementCounter(&Counters::string_compare_native, 1);
4424
4425 // Find minimum length.
4426 Label left_shorter;
4427 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
4428 __ mov(scratch3, scratch1);
4429 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
4430
4431 Register length_delta = scratch3;
4432
4433 __ j(less_equal, &left_shorter);
4434 // Right string is shorter. Change scratch1 to be length of right string.
4435 __ sub(scratch1, Operand(length_delta));
4436 __ bind(&left_shorter);
4437
4438 Register min_length = scratch1;
4439
4440 // If either length is zero, just compare lengths.
4441 __ test(min_length, Operand(min_length));
4442 __ j(zero, &compare_lengths);
4443
4444 // Change index to run from -min_length to -1 by adding min_length
4445 // to string start. This means that loop ends when index reaches zero,
4446 // which doesn't need an additional compare.
4447 __ SmiUntag(min_length);
4448 __ lea(left,
4449 FieldOperand(left,
4450 min_length, times_1,
4451 SeqAsciiString::kHeaderSize));
4452 __ lea(right,
4453 FieldOperand(right,
4454 min_length, times_1,
4455 SeqAsciiString::kHeaderSize));
4456 __ neg(min_length);
4457
4458 Register index = min_length; // index = -min_length;
4459
4460 {
4461 // Compare loop.
4462 Label loop;
4463 __ bind(&loop);
4464 // Compare characters.
4465 __ mov_b(scratch2, Operand(left, index, times_1, 0));
4466 __ cmpb(scratch2, Operand(right, index, times_1, 0));
4467 __ j(not_equal, &result_not_equal);
4468 __ add(Operand(index), Immediate(1));
4469 __ j(not_zero, &loop);
4470 }
4471
4472 // Compare lengths - strings up to min-length are equal.
4473 __ bind(&compare_lengths);
4474 __ test(length_delta, Operand(length_delta));
4475 __ j(not_zero, &result_not_equal);
4476
4477 // Result is EQUAL.
4478 STATIC_ASSERT(EQUAL == 0);
4479 STATIC_ASSERT(kSmiTag == 0);
4480 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4481 __ ret(0);
4482
4483 __ bind(&result_not_equal);
4484 __ j(greater, &result_greater);
4485
4486 // Result is LESS.
4487 __ Set(eax, Immediate(Smi::FromInt(LESS)));
4488 __ ret(0);
4489
4490 // Result is GREATER.
4491 __ bind(&result_greater);
4492 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
4493 __ ret(0);
4494 }
4495
4496
4497 void StringCompareStub::Generate(MacroAssembler* masm) {
4498 Label runtime;
4499
4500 // Stack frame on entry.
4501 // esp[0]: return address
4502 // esp[4]: right string
4503 // esp[8]: left string
4504
4505 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
4506 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
4507
4508 Label not_same;
4509 __ cmp(edx, Operand(eax));
4510 __ j(not_equal, &not_same);
4511 STATIC_ASSERT(EQUAL == 0);
4512 STATIC_ASSERT(kSmiTag == 0);
4513 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4514 __ IncrementCounter(&Counters::string_compare_native, 1);
4515 __ ret(2 * kPointerSize);
4516
4517 __ bind(&not_same);
4518
4519 // Check that both objects are sequential ascii strings.
4520 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
4521
4522 // Compare flat ascii strings.
4523 // Drop arguments from the stack.
4524 __ pop(ecx);
4525 __ add(Operand(esp), Immediate(2 * kPointerSize));
4526 __ push(ecx);
4527 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
4528
4529 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4530 // tagged as a small integer.
4531 __ bind(&runtime);
4532 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4533 }
4534
4535 #undef __
4536
4537 } } // namespace v8::internal
4538
4539 #endif // V8_TARGET_ARCH_IA32
OLDNEW
« no previous file with comments | « src/ia32/code-stubs-ia32.h ('k') | src/ia32/codegen-ia32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698