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