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