blob: 76fcc88cbfad2b30df277c6ae50859fce69f8921 [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002// 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)
Steve Block1e0659c2011-05-24 12:43:12 +010040
41void ToNumberStub::Generate(MacroAssembler* masm) {
42 // The ToNumber stub takes one argument in eax.
43 NearLabel check_heap_number, call_builtin;
44 __ SmiTest(rax);
45 __ j(not_zero, &check_heap_number);
46 __ Ret();
47
48 __ bind(&check_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +010049 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
50 Heap::kHeapNumberMapRootIndex);
Steve Block1e0659c2011-05-24 12:43:12 +010051 __ j(not_equal, &call_builtin);
52 __ Ret();
53
54 __ bind(&call_builtin);
55 __ pop(rcx); // Pop return address.
56 __ push(rax);
57 __ push(rcx); // Push return address.
58 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
59}
60
61
Kristian Monsen80d68ea2010-09-08 11:05:35 +010062void FastNewClosureStub::Generate(MacroAssembler* masm) {
63 // Create a new closure from the given function info in new
64 // space. Set the context to the current context in rsi.
65 Label gc;
66 __ AllocateInNewSpace(JSFunction::kSize, rax, rbx, rcx, &gc, TAG_OBJECT);
67
68 // Get the function info from the stack.
69 __ movq(rdx, Operand(rsp, 1 * kPointerSize));
70
Steve Block44f0eee2011-05-26 01:26:41 +010071 int map_index = strict_mode_ == kStrictMode
72 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
73 : Context::FUNCTION_MAP_INDEX;
74
Kristian Monsen80d68ea2010-09-08 11:05:35 +010075 // Compute the function map in the current global context and set that
76 // as the map of the allocated object.
77 __ movq(rcx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
78 __ movq(rcx, FieldOperand(rcx, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +010079 __ movq(rcx, Operand(rcx, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010080 __ movq(FieldOperand(rax, JSObject::kMapOffset), rcx);
81
82 // Initialize the rest of the function. We don't have to update the
83 // write barrier because the allocated object is in new space.
84 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
85 __ LoadRoot(rcx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +010086 __ LoadRoot(rdi, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010087 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
88 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rbx);
89 __ movq(FieldOperand(rax, JSFunction::kPrototypeOrInitialMapOffset), rcx);
90 __ movq(FieldOperand(rax, JSFunction::kSharedFunctionInfoOffset), rdx);
91 __ movq(FieldOperand(rax, JSFunction::kContextOffset), rsi);
92 __ movq(FieldOperand(rax, JSFunction::kLiteralsOffset), rbx);
Ben Murdochb0fe1622011-05-05 13:52:32 +010093 __ movq(FieldOperand(rax, JSFunction::kNextFunctionLinkOffset), rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010094
95 // Initialize the code pointer in the function to be the one
96 // found in the shared function info object.
97 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
98 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
99 __ movq(FieldOperand(rax, JSFunction::kCodeEntryOffset), rdx);
100
101
102 // Return and remove the on-stack parameter.
103 __ ret(1 * kPointerSize);
104
105 // Create a new closure through the slower runtime call.
106 __ bind(&gc);
107 __ pop(rcx); // Temporarily remove return address.
108 __ pop(rdx);
109 __ push(rsi);
110 __ push(rdx);
Steve Block44f0eee2011-05-26 01:26:41 +0100111 __ PushRoot(Heap::kFalseValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100112 __ push(rcx); // Restore return address.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800113 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100114}
115
116
117void FastNewContextStub::Generate(MacroAssembler* masm) {
118 // Try to allocate the context in new space.
119 Label gc;
120 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
121 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
122 rax, rbx, rcx, &gc, TAG_OBJECT);
123
124 // Get the function from the stack.
125 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
126
127 // Setup the object header.
128 __ LoadRoot(kScratchRegister, Heap::kContextMapRootIndex);
129 __ movq(FieldOperand(rax, HeapObject::kMapOffset), kScratchRegister);
130 __ Move(FieldOperand(rax, FixedArray::kLengthOffset), Smi::FromInt(length));
131
132 // Setup the fixed slots.
Steve Block9fac8402011-05-12 15:51:54 +0100133 __ Set(rbx, 0); // Set to NULL.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100134 __ movq(Operand(rax, Context::SlotOffset(Context::CLOSURE_INDEX)), rcx);
135 __ movq(Operand(rax, Context::SlotOffset(Context::FCONTEXT_INDEX)), rax);
136 __ movq(Operand(rax, Context::SlotOffset(Context::PREVIOUS_INDEX)), rbx);
137 __ movq(Operand(rax, Context::SlotOffset(Context::EXTENSION_INDEX)), rbx);
138
139 // Copy the global object from the surrounding context.
140 __ movq(rbx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
141 __ movq(Operand(rax, Context::SlotOffset(Context::GLOBAL_INDEX)), rbx);
142
143 // Initialize the rest of the slots to undefined.
144 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
145 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
146 __ movq(Operand(rax, Context::SlotOffset(i)), rbx);
147 }
148
149 // Return and remove the on-stack parameter.
150 __ movq(rsi, rax);
151 __ ret(1 * kPointerSize);
152
153 // Need to collect. Call into runtime system.
154 __ bind(&gc);
155 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
156}
157
158
159void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
160 // Stack layout on entry:
161 //
162 // [rsp + kPointerSize]: constant elements.
163 // [rsp + (2 * kPointerSize)]: literal index.
164 // [rsp + (3 * kPointerSize)]: literals array.
165
166 // All sizes here are multiples of kPointerSize.
167 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
168 int size = JSArray::kSize + elements_size;
169
170 // Load boilerplate object into rcx and check if we need to create a
171 // boilerplate.
172 Label slow_case;
173 __ movq(rcx, Operand(rsp, 3 * kPointerSize));
174 __ movq(rax, Operand(rsp, 2 * kPointerSize));
175 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
176 __ movq(rcx,
177 FieldOperand(rcx, index.reg, index.scale, FixedArray::kHeaderSize));
178 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
179 __ j(equal, &slow_case);
180
181 if (FLAG_debug_code) {
182 const char* message;
183 Heap::RootListIndex expected_map_index;
184 if (mode_ == CLONE_ELEMENTS) {
185 message = "Expected (writable) fixed array";
186 expected_map_index = Heap::kFixedArrayMapRootIndex;
187 } else {
188 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
189 message = "Expected copy-on-write fixed array";
190 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
191 }
192 __ push(rcx);
193 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
194 __ CompareRoot(FieldOperand(rcx, HeapObject::kMapOffset),
195 expected_map_index);
196 __ Assert(equal, message);
197 __ pop(rcx);
198 }
199
200 // Allocate both the JS array and the elements array in one big
201 // allocation. This avoids multiple limit checks.
202 __ AllocateInNewSpace(size, rax, rbx, rdx, &slow_case, TAG_OBJECT);
203
204 // Copy the JS array part.
205 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
206 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
207 __ movq(rbx, FieldOperand(rcx, i));
208 __ movq(FieldOperand(rax, i), rbx);
209 }
210 }
211
212 if (length_ > 0) {
213 // Get hold of the elements array of the boilerplate and setup the
214 // elements pointer in the resulting object.
215 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
216 __ lea(rdx, Operand(rax, JSArray::kSize));
217 __ movq(FieldOperand(rax, JSArray::kElementsOffset), rdx);
218
219 // Copy the elements array.
220 for (int i = 0; i < elements_size; i += kPointerSize) {
221 __ movq(rbx, FieldOperand(rcx, i));
222 __ movq(FieldOperand(rdx, i), rbx);
223 }
224 }
225
226 // Return and remove the on-stack parameters.
227 __ ret(3 * kPointerSize);
228
229 __ bind(&slow_case);
230 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
231}
232
233
234void ToBooleanStub::Generate(MacroAssembler* masm) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100235 NearLabel false_result, true_result, not_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100236 __ movq(rax, Operand(rsp, 1 * kPointerSize));
237
238 // 'null' => false.
239 __ CompareRoot(rax, Heap::kNullValueRootIndex);
240 __ j(equal, &false_result);
241
242 // Get the map and type of the heap object.
243 // We don't use CmpObjectType because we manipulate the type field.
244 __ movq(rdx, FieldOperand(rax, HeapObject::kMapOffset));
245 __ movzxbq(rcx, FieldOperand(rdx, Map::kInstanceTypeOffset));
246
247 // Undetectable => false.
248 __ movzxbq(rbx, FieldOperand(rdx, Map::kBitFieldOffset));
249 __ and_(rbx, Immediate(1 << Map::kIsUndetectable));
250 __ j(not_zero, &false_result);
251
252 // JavaScript object => true.
253 __ cmpq(rcx, Immediate(FIRST_JS_OBJECT_TYPE));
254 __ j(above_equal, &true_result);
255
256 // String value => false iff empty.
257 __ cmpq(rcx, Immediate(FIRST_NONSTRING_TYPE));
258 __ j(above_equal, &not_string);
259 __ movq(rdx, FieldOperand(rax, String::kLengthOffset));
260 __ SmiTest(rdx);
261 __ j(zero, &false_result);
262 __ jmp(&true_result);
263
264 __ bind(&not_string);
265 __ CompareRoot(rdx, Heap::kHeapNumberMapRootIndex);
266 __ j(not_equal, &true_result);
267 // HeapNumber => false iff +0, -0, or NaN.
268 // These three cases set the zero flag when compared to zero using ucomisd.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100269 __ xorps(xmm0, xmm0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100270 __ ucomisd(xmm0, FieldOperand(rax, HeapNumber::kValueOffset));
271 __ j(zero, &false_result);
272 // Fall through to |true_result|.
273
274 // Return 1/0 for true/false in rax.
275 __ bind(&true_result);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100276 __ Set(rax, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100277 __ ret(1 * kPointerSize);
278 __ bind(&false_result);
Steve Block9fac8402011-05-12 15:51:54 +0100279 __ Set(rax, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100280 __ ret(1 * kPointerSize);
281}
282
283
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100284class FloatingPointHelper : public AllStatic {
285 public:
286 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
287 // If the operands are not both numbers, jump to not_numbers.
288 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
289 // NumberOperands assumes both are smis or heap numbers.
290 static void LoadSSE2SmiOperands(MacroAssembler* masm);
291 static void LoadSSE2NumberOperands(MacroAssembler* masm);
292 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
293 Label* not_numbers);
294
295 // Takes the operands in rdx and rax and loads them as integers in rax
296 // and rcx.
297 static void LoadAsIntegers(MacroAssembler* masm,
298 Label* operand_conversion_failure,
299 Register heap_number_map);
300 // As above, but we know the operands to be numbers. In that case,
301 // conversion can't fail.
302 static void LoadNumbersAsIntegers(MacroAssembler* masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100303
304 // Tries to convert two values to smis losslessly.
305 // This fails if either argument is not a Smi nor a HeapNumber,
306 // or if it's a HeapNumber with a value that can't be converted
307 // losslessly to a Smi. In that case, control transitions to the
308 // on_not_smis label.
309 // On success, either control goes to the on_success label (if one is
310 // provided), or it falls through at the end of the code (if on_success
311 // is NULL).
312 // On success, both first and second holds Smi tagged values.
313 // One of first or second must be non-Smi when entering.
314 static void NumbersToSmis(MacroAssembler* masm,
315 Register first,
316 Register second,
317 Register scratch1,
318 Register scratch2,
319 Register scratch3,
320 Label* on_success,
321 Label* on_not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100322};
323
324
Ben Murdochb0fe1622011-05-05 13:52:32 +0100325Handle<Code> GetTypeRecordingBinaryOpStub(int key,
326 TRBinaryOpIC::TypeInfo type_info,
327 TRBinaryOpIC::TypeInfo result_type_info) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100328 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
329 return stub.GetCode();
330}
331
332
333void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
334 __ pop(rcx); // Save return address.
335 __ push(rdx);
336 __ push(rax);
337 // Left and right arguments are now on top.
338 // Push this stub's key. Although the operation and the type info are
339 // encoded into the key, the encoding is opaque, so push them too.
340 __ Push(Smi::FromInt(MinorKey()));
341 __ Push(Smi::FromInt(op_));
342 __ Push(Smi::FromInt(operands_type_));
343
344 __ push(rcx); // Push return address.
345
346 // Patch the caller to an appropriate specialized stub and return the
347 // operation result to the caller of the stub.
348 __ TailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100349 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
350 masm->isolate()),
Ben Murdoch086aeea2011-05-13 15:57:08 +0100351 5,
352 1);
353}
354
355
Ben Murdoch086aeea2011-05-13 15:57:08 +0100356void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
357 switch (operands_type_) {
358 case TRBinaryOpIC::UNINITIALIZED:
359 GenerateTypeTransition(masm);
360 break;
361 case TRBinaryOpIC::SMI:
362 GenerateSmiStub(masm);
363 break;
364 case TRBinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +0100365 UNREACHABLE();
366 // The int32 case is identical to the Smi case. We avoid creating this
367 // ic state on x64.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100368 break;
369 case TRBinaryOpIC::HEAP_NUMBER:
370 GenerateHeapNumberStub(masm);
371 break;
Steve Block44f0eee2011-05-26 01:26:41 +0100372 case TRBinaryOpIC::ODDBALL:
373 GenerateOddballStub(masm);
374 break;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100375 case TRBinaryOpIC::BOTH_STRING:
376 GenerateBothStringStub(masm);
377 break;
Ben Murdoch086aeea2011-05-13 15:57:08 +0100378 case TRBinaryOpIC::STRING:
379 GenerateStringStub(masm);
380 break;
381 case TRBinaryOpIC::GENERIC:
382 GenerateGeneric(masm);
383 break;
384 default:
385 UNREACHABLE();
386 }
387}
388
389
390const char* TypeRecordingBinaryOpStub::GetName() {
391 if (name_ != NULL) return name_;
392 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +0100393 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
394 kMaxNameLength);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100395 if (name_ == NULL) return "OOM";
396 const char* op_name = Token::Name(op_);
397 const char* overwrite_name;
398 switch (mode_) {
399 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
400 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
401 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
402 default: overwrite_name = "UnknownOverwrite"; break;
403 }
404
405 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
406 "TypeRecordingBinaryOpStub_%s_%s_%s",
407 op_name,
408 overwrite_name,
409 TRBinaryOpIC::GetName(operands_type_));
410 return name_;
411}
412
413
414void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
415 Label* slow,
416 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
Steve Block1e0659c2011-05-24 12:43:12 +0100417
Steve Block1e0659c2011-05-24 12:43:12 +0100418 // Arguments to TypeRecordingBinaryOpStub are in rdx and rax.
419 Register left = rdx;
420 Register right = rax;
421
Ben Murdoch8b112d22011-06-08 16:22:53 +0100422 // We only generate heapnumber answers for overflowing calculations
423 // for the four basic arithmetic operations and logical right shift by 0.
424 bool generate_inline_heapnumber_results =
425 (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) &&
426 (op_ == Token::ADD || op_ == Token::SUB ||
427 op_ == Token::MUL || op_ == Token::DIV || op_ == Token::SHR);
Steve Block1e0659c2011-05-24 12:43:12 +0100428
429 // Smi check of both operands. If op is BIT_OR, the check is delayed
430 // until after the OR operation.
431 Label not_smis;
432 Label use_fp_on_smis;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100433 Label fail;
Steve Block1e0659c2011-05-24 12:43:12 +0100434
435 if (op_ != Token::BIT_OR) {
436 Comment smi_check_comment(masm, "-- Smi check arguments");
437 __ JumpIfNotBothSmi(left, right, &not_smis);
438 }
439
Ben Murdoch8b112d22011-06-08 16:22:53 +0100440 Label smi_values;
441 __ bind(&smi_values);
Steve Block1e0659c2011-05-24 12:43:12 +0100442 // Perform the operation.
443 Comment perform_smi(masm, "-- Perform smi operation");
444 switch (op_) {
445 case Token::ADD:
446 ASSERT(right.is(rax));
447 __ SmiAdd(right, right, left, &use_fp_on_smis); // ADD is commutative.
448 break;
449
450 case Token::SUB:
451 __ SmiSub(left, left, right, &use_fp_on_smis);
452 __ movq(rax, left);
453 break;
454
455 case Token::MUL:
456 ASSERT(right.is(rax));
457 __ SmiMul(right, right, left, &use_fp_on_smis); // MUL is commutative.
458 break;
459
460 case Token::DIV:
461 // SmiDiv will not accept left in rdx or right in rax.
462 left = rcx;
463 right = rbx;
464 __ movq(rbx, rax);
465 __ movq(rcx, rdx);
466 __ SmiDiv(rax, left, right, &use_fp_on_smis);
467 break;
468
469 case Token::MOD:
470 // SmiMod will not accept left in rdx or right in rax.
471 left = rcx;
472 right = rbx;
473 __ movq(rbx, rax);
474 __ movq(rcx, rdx);
475 __ SmiMod(rax, left, right, &use_fp_on_smis);
476 break;
477
478 case Token::BIT_OR: {
479 ASSERT(right.is(rax));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100480 __ SmiOrIfSmis(right, right, left, &not_smis); // BIT_OR is commutative.
Steve Block1e0659c2011-05-24 12:43:12 +0100481 break;
482 }
483 case Token::BIT_XOR:
484 ASSERT(right.is(rax));
485 __ SmiXor(right, right, left); // BIT_XOR is commutative.
486 break;
487
488 case Token::BIT_AND:
489 ASSERT(right.is(rax));
490 __ SmiAnd(right, right, left); // BIT_AND is commutative.
491 break;
492
493 case Token::SHL:
494 __ SmiShiftLeft(left, left, right);
495 __ movq(rax, left);
496 break;
497
498 case Token::SAR:
499 __ SmiShiftArithmeticRight(left, left, right);
500 __ movq(rax, left);
501 break;
502
503 case Token::SHR:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100504 __ SmiShiftLogicalRight(left, left, right, &use_fp_on_smis);
Steve Block1e0659c2011-05-24 12:43:12 +0100505 __ movq(rax, left);
506 break;
507
508 default:
509 UNREACHABLE();
510 }
511
512 // 5. Emit return of result in rax. Some operations have registers pushed.
513 __ ret(0);
514
Ben Murdoch8b112d22011-06-08 16:22:53 +0100515 if (use_fp_on_smis.is_linked()) {
516 // 6. For some operations emit inline code to perform floating point
517 // operations on known smis (e.g., if the result of the operation
518 // overflowed the smi range).
519 __ bind(&use_fp_on_smis);
520 if (op_ == Token::DIV || op_ == Token::MOD) {
521 // Restore left and right to rdx and rax.
522 __ movq(rdx, rcx);
523 __ movq(rax, rbx);
Steve Block1e0659c2011-05-24 12:43:12 +0100524 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100525
526 if (generate_inline_heapnumber_results) {
527 __ AllocateHeapNumber(rcx, rbx, slow);
528 Comment perform_float(masm, "-- Perform float operation on smis");
529 if (op_ == Token::SHR) {
530 __ SmiToInteger32(left, left);
531 __ cvtqsi2sd(xmm0, left);
532 } else {
533 FloatingPointHelper::LoadSSE2SmiOperands(masm);
534 switch (op_) {
535 case Token::ADD: __ addsd(xmm0, xmm1); break;
536 case Token::SUB: __ subsd(xmm0, xmm1); break;
537 case Token::MUL: __ mulsd(xmm0, xmm1); break;
538 case Token::DIV: __ divsd(xmm0, xmm1); break;
539 default: UNREACHABLE();
540 }
541 }
542 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm0);
543 __ movq(rax, rcx);
544 __ ret(0);
545 } else {
546 __ jmp(&fail);
547 }
Steve Block1e0659c2011-05-24 12:43:12 +0100548 }
549
550 // 7. Non-smi operands reach the end of the code generated by
551 // GenerateSmiCode, and fall through to subsequent code,
552 // with the operands in rdx and rax.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100553 // But first we check if non-smi values are HeapNumbers holding
554 // values that could be smi.
Steve Block1e0659c2011-05-24 12:43:12 +0100555 __ bind(&not_smis);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100556 Comment done_comment(masm, "-- Enter non-smi code");
557 FloatingPointHelper::NumbersToSmis(masm, left, right, rbx, rdi, rcx,
558 &smi_values, &fail);
559 __ jmp(&smi_values);
560 __ bind(&fail);
Steve Block1e0659c2011-05-24 12:43:12 +0100561}
562
563
564void TypeRecordingBinaryOpStub::GenerateFloatingPointCode(
565 MacroAssembler* masm,
566 Label* allocation_failure,
567 Label* non_numeric_failure) {
568 switch (op_) {
569 case Token::ADD:
570 case Token::SUB:
571 case Token::MUL:
572 case Token::DIV: {
573 FloatingPointHelper::LoadSSE2UnknownOperands(masm, non_numeric_failure);
574
575 switch (op_) {
576 case Token::ADD: __ addsd(xmm0, xmm1); break;
577 case Token::SUB: __ subsd(xmm0, xmm1); break;
578 case Token::MUL: __ mulsd(xmm0, xmm1); break;
579 case Token::DIV: __ divsd(xmm0, xmm1); break;
580 default: UNREACHABLE();
581 }
582 GenerateHeapResultAllocation(masm, allocation_failure);
583 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
584 __ ret(0);
585 break;
586 }
587 case Token::MOD: {
588 // For MOD we jump to the allocation_failure label, to call runtime.
589 __ jmp(allocation_failure);
590 break;
591 }
592 case Token::BIT_OR:
593 case Token::BIT_AND:
594 case Token::BIT_XOR:
595 case Token::SAR:
596 case Token::SHL:
597 case Token::SHR: {
598 Label non_smi_shr_result;
599 Register heap_number_map = r9;
600 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
601 FloatingPointHelper::LoadAsIntegers(masm, non_numeric_failure,
602 heap_number_map);
603 switch (op_) {
604 case Token::BIT_OR: __ orl(rax, rcx); break;
605 case Token::BIT_AND: __ andl(rax, rcx); break;
606 case Token::BIT_XOR: __ xorl(rax, rcx); break;
607 case Token::SAR: __ sarl_cl(rax); break;
608 case Token::SHL: __ shll_cl(rax); break;
609 case Token::SHR: {
610 __ shrl_cl(rax);
611 // Check if result is negative. This can only happen for a shift
612 // by zero.
613 __ testl(rax, rax);
614 __ j(negative, &non_smi_shr_result);
615 break;
616 }
617 default: UNREACHABLE();
618 }
619 STATIC_ASSERT(kSmiValueSize == 32);
620 // Tag smi result and return.
621 __ Integer32ToSmi(rax, rax);
622 __ Ret();
623
624 // Logical shift right can produce an unsigned int32 that is not
625 // an int32, and so is not in the smi range. Allocate a heap number
626 // in that case.
627 if (op_ == Token::SHR) {
628 __ bind(&non_smi_shr_result);
629 Label allocation_failed;
630 __ movl(rbx, rax); // rbx holds result value (uint32 value as int64).
631 // Allocate heap number in new space.
632 // Not using AllocateHeapNumber macro in order to reuse
633 // already loaded heap_number_map.
634 __ AllocateInNewSpace(HeapNumber::kSize,
635 rax,
636 rcx,
637 no_reg,
638 &allocation_failed,
639 TAG_OBJECT);
640 // Set the map.
641 if (FLAG_debug_code) {
642 __ AbortIfNotRootValue(heap_number_map,
643 Heap::kHeapNumberMapRootIndex,
644 "HeapNumberMap register clobbered.");
645 }
646 __ movq(FieldOperand(rax, HeapObject::kMapOffset),
647 heap_number_map);
648 __ cvtqsi2sd(xmm0, rbx);
649 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
650 __ Ret();
651
652 __ bind(&allocation_failed);
653 // We need tagged values in rdx and rax for the following code,
654 // not int32 in rax and rcx.
655 __ Integer32ToSmi(rax, rcx);
656 __ Integer32ToSmi(rdx, rax);
657 __ jmp(allocation_failure);
658 }
659 break;
660 }
661 default: UNREACHABLE(); break;
662 }
663 // No fall-through from this generated code.
664 if (FLAG_debug_code) {
665 __ Abort("Unexpected fall-through in "
666 "TypeRecordingBinaryStub::GenerateFloatingPointCode.");
667 }
668}
669
670
671void TypeRecordingBinaryOpStub::GenerateStringAddCode(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100672 ASSERT(op_ == Token::ADD);
673 NearLabel left_not_string, call_runtime;
674
Steve Block1e0659c2011-05-24 12:43:12 +0100675 // Registers containing left and right operands respectively.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100676 Register left = rdx;
677 Register right = rax;
Steve Block1e0659c2011-05-24 12:43:12 +0100678
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100679 // Test if left operand is a string.
680 __ JumpIfSmi(left, &left_not_string);
681 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
682 __ j(above_equal, &left_not_string);
683 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
684 GenerateRegisterArgsPush(masm);
685 __ TailCallStub(&string_add_left_stub);
Steve Block1e0659c2011-05-24 12:43:12 +0100686
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100687 // Left operand is not a string, test right.
688 __ bind(&left_not_string);
689 __ JumpIfSmi(right, &call_runtime);
690 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
691 __ j(above_equal, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +0100692
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100693 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
694 GenerateRegisterArgsPush(masm);
695 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +0100696
Steve Block1e0659c2011-05-24 12:43:12 +0100697 // Neither argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100698 __ bind(&call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +0100699}
700
701
702void TypeRecordingBinaryOpStub::GenerateCallRuntimeCode(MacroAssembler* masm) {
703 GenerateRegisterArgsPush(masm);
704 switch (op_) {
705 case Token::ADD:
706 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
707 break;
708 case Token::SUB:
709 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
710 break;
711 case Token::MUL:
712 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
713 break;
714 case Token::DIV:
715 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
716 break;
717 case Token::MOD:
718 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
719 break;
720 case Token::BIT_OR:
721 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
722 break;
723 case Token::BIT_AND:
724 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
725 break;
726 case Token::BIT_XOR:
727 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
728 break;
729 case Token::SAR:
730 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
731 break;
732 case Token::SHL:
733 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
734 break;
735 case Token::SHR:
736 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
737 break;
738 default:
739 UNREACHABLE();
740 }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100741}
742
743
744void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100745 Label call_runtime;
746 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
747 result_type_ == TRBinaryOpIC::SMI) {
748 // Only allow smi results.
749 GenerateSmiCode(masm, NULL, NO_HEAPNUMBER_RESULTS);
750 } else {
751 // Allow heap number result and don't make a transition if a heap number
752 // cannot be allocated.
753 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
754 }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100755
Ben Murdoch8b112d22011-06-08 16:22:53 +0100756 // Code falls through if the result is not returned as either a smi or heap
757 // number.
Steve Block1e0659c2011-05-24 12:43:12 +0100758 GenerateTypeTransition(masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100759
760 if (call_runtime.is_linked()) {
761 __ bind(&call_runtime);
762 GenerateCallRuntimeCode(masm);
763 }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100764}
765
766
767void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100768 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +0100769 ASSERT(op_ == Token::ADD);
770 GenerateStringAddCode(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100771 // Try to add arguments as strings, otherwise, transition to the generic
772 // TRBinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +0100773 GenerateTypeTransition(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100774}
775
776
Ben Murdoch8b112d22011-06-08 16:22:53 +0100777void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
778 Label call_runtime;
779 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
780 ASSERT(op_ == Token::ADD);
781 // If both arguments are strings, call the string add stub.
782 // Otherwise, do a transition.
783
784 // Registers containing left and right operands respectively.
785 Register left = rdx;
786 Register right = rax;
787
788 // Test if left operand is a string.
789 __ JumpIfSmi(left, &call_runtime);
790 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
791 __ j(above_equal, &call_runtime);
792
793 // Test if right operand is a string.
794 __ JumpIfSmi(right, &call_runtime);
795 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
796 __ j(above_equal, &call_runtime);
797
798 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
799 GenerateRegisterArgsPush(masm);
800 __ TailCallStub(&string_add_stub);
801
802 __ bind(&call_runtime);
803 GenerateTypeTransition(masm);
804}
805
806
Steve Block44f0eee2011-05-26 01:26:41 +0100807void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
808 Label call_runtime;
809
810 if (op_ == Token::ADD) {
811 // Handle string addition here, because it is the only operation
812 // that does not do a ToNumber conversion on the operands.
813 GenerateStringAddCode(masm);
814 }
815
816 // Convert oddball arguments to numbers.
817 NearLabel check, done;
818 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
819 __ j(not_equal, &check);
820 if (Token::IsBitOp(op_)) {
821 __ xor_(rdx, rdx);
822 } else {
823 __ LoadRoot(rdx, Heap::kNanValueRootIndex);
824 }
825 __ jmp(&done);
826 __ bind(&check);
827 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
828 __ j(not_equal, &done);
829 if (Token::IsBitOp(op_)) {
830 __ xor_(rax, rax);
831 } else {
832 __ LoadRoot(rax, Heap::kNanValueRootIndex);
833 }
834 __ bind(&done);
835
836 GenerateHeapNumberStub(masm);
837}
838
839
Ben Murdoch086aeea2011-05-13 15:57:08 +0100840void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +0100841 Label gc_required, not_number;
842 GenerateFloatingPointCode(masm, &gc_required, &not_number);
843
844 __ bind(&not_number);
845 GenerateTypeTransition(masm);
846
847 __ bind(&gc_required);
848 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100849}
850
851
852void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +0100853 Label call_runtime, call_string_add_or_runtime;
854
855 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
856
857 GenerateFloatingPointCode(masm, &call_runtime, &call_string_add_or_runtime);
858
859 __ bind(&call_string_add_or_runtime);
860 if (op_ == Token::ADD) {
861 GenerateStringAddCode(masm);
862 }
863
864 __ bind(&call_runtime);
865 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +0100866}
867
868
869void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
870 MacroAssembler* masm,
871 Label* alloc_failure) {
Steve Block1e0659c2011-05-24 12:43:12 +0100872 Label skip_allocation;
873 OverwriteMode mode = mode_;
874 switch (mode) {
875 case OVERWRITE_LEFT: {
876 // If the argument in rdx is already an object, we skip the
877 // allocation of a heap number.
878 __ JumpIfNotSmi(rdx, &skip_allocation);
879 // Allocate a heap number for the result. Keep eax and edx intact
880 // for the possible runtime call.
881 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
882 // Now rdx can be overwritten losing one of the arguments as we are
883 // now done and will not need it any more.
884 __ movq(rdx, rbx);
885 __ bind(&skip_allocation);
886 // Use object in rdx as a result holder
887 __ movq(rax, rdx);
888 break;
889 }
890 case OVERWRITE_RIGHT:
891 // If the argument in rax is already an object, we skip the
892 // allocation of a heap number.
893 __ JumpIfNotSmi(rax, &skip_allocation);
894 // Fall through!
895 case NO_OVERWRITE:
896 // Allocate a heap number for the result. Keep rax and rdx intact
897 // for the possible runtime call.
898 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
899 // Now rax can be overwritten losing one of the arguments as we are
900 // now done and will not need it any more.
901 __ movq(rax, rbx);
902 __ bind(&skip_allocation);
903 break;
904 default: UNREACHABLE();
905 }
Ben Murdoch086aeea2011-05-13 15:57:08 +0100906}
907
908
909void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
910 __ pop(rcx);
911 __ push(rdx);
912 __ push(rax);
913 __ push(rcx);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100914}
915
916
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100917void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100918 // TAGGED case:
919 // Input:
920 // rsp[8]: argument (should be number).
921 // rsp[0]: return address.
922 // Output:
923 // rax: tagged double result.
924 // UNTAGGED case:
925 // Input::
926 // rsp[0]: return address.
927 // xmm1: untagged double input argument
928 // Output:
929 // xmm1: untagged double result.
930
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100931 Label runtime_call;
932 Label runtime_call_clear_stack;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100933 Label skip_cache;
934 const bool tagged = (argument_type_ == TAGGED);
935 if (tagged) {
936 NearLabel input_not_smi;
937 NearLabel loaded;
938 // Test that rax is a number.
939 __ movq(rax, Operand(rsp, kPointerSize));
940 __ JumpIfNotSmi(rax, &input_not_smi);
941 // Input is a smi. Untag and load it onto the FPU stack.
942 // Then load the bits of the double into rbx.
943 __ SmiToInteger32(rax, rax);
944 __ subq(rsp, Immediate(kDoubleSize));
945 __ cvtlsi2sd(xmm1, rax);
946 __ movsd(Operand(rsp, 0), xmm1);
947 __ movq(rbx, xmm1);
948 __ movq(rdx, xmm1);
949 __ fld_d(Operand(rsp, 0));
950 __ addq(rsp, Immediate(kDoubleSize));
951 __ jmp(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100952
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100953 __ bind(&input_not_smi);
954 // Check if input is a HeapNumber.
955 __ LoadRoot(rbx, Heap::kHeapNumberMapRootIndex);
956 __ cmpq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
957 __ j(not_equal, &runtime_call);
958 // Input is a HeapNumber. Push it on the FPU stack and load its
959 // bits into rbx.
960 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
961 __ movq(rbx, FieldOperand(rax, HeapNumber::kValueOffset));
962 __ movq(rdx, rbx);
963
964 __ bind(&loaded);
965 } else { // UNTAGGED.
966 __ movq(rbx, xmm1);
967 __ movq(rdx, xmm1);
968 }
969
970 // ST[0] == double value, if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100971 // rbx = bits of double value.
972 // rdx = also bits of double value.
973 // Compute hash (h is 32 bits, bits are 64 and the shifts are arithmetic):
974 // h = h0 = bits ^ (bits >> 32);
975 // h ^= h >> 16;
976 // h ^= h >> 8;
977 // h = h & (cacheSize - 1);
978 // or h = (h0 ^ (h0 >> 8) ^ (h0 >> 16) ^ (h0 >> 24)) & (cacheSize - 1)
979 __ sar(rdx, Immediate(32));
980 __ xorl(rdx, rbx);
981 __ movl(rcx, rdx);
982 __ movl(rax, rdx);
983 __ movl(rdi, rdx);
984 __ sarl(rdx, Immediate(8));
985 __ sarl(rcx, Immediate(16));
986 __ sarl(rax, Immediate(24));
987 __ xorl(rcx, rdx);
988 __ xorl(rax, rdi);
989 __ xorl(rcx, rax);
Steve Block44f0eee2011-05-26 01:26:41 +0100990 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
991 __ andl(rcx, Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100992
993 // ST[0] == double value.
994 // rbx = bits of double value.
995 // rcx = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +0100996 ExternalReference cache_array =
997 ExternalReference::transcendental_cache_array_address(masm->isolate());
998 __ movq(rax, cache_array);
999 int cache_array_index =
1000 type_ * sizeof(Isolate::Current()->transcendental_cache()->caches_[0]);
1001 __ movq(rax, Operand(rax, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001002 // rax points to the cache for the type type_.
1003 // If NULL, the cache hasn't been initialized yet, so go through runtime.
1004 __ testq(rax, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001005 __ j(zero, &runtime_call_clear_stack); // Only clears stack if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001006#ifdef DEBUG
1007 // Check that the layout of cache elements match expectations.
1008 { // NOLINT - doesn't like a single brace on a line.
Steve Block44f0eee2011-05-26 01:26:41 +01001009 TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001010 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
1011 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
1012 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
1013 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
1014 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
1015 // Two uint_32's and a pointer per element.
1016 CHECK_EQ(16, static_cast<int>(elem2_start - elem_start));
1017 CHECK_EQ(0, static_cast<int>(elem_in0 - elem_start));
1018 CHECK_EQ(kIntSize, static_cast<int>(elem_in1 - elem_start));
1019 CHECK_EQ(2 * kIntSize, static_cast<int>(elem_out - elem_start));
1020 }
1021#endif
1022 // Find the address of the rcx'th entry in the cache, i.e., &rax[rcx*16].
1023 __ addl(rcx, rcx);
1024 __ lea(rcx, Operand(rax, rcx, times_8, 0));
1025 // Check if cache matches: Double value is stored in uint32_t[2] array.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001026 NearLabel cache_miss;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001027 __ cmpq(rbx, Operand(rcx, 0));
1028 __ j(not_equal, &cache_miss);
1029 // Cache hit!
1030 __ movq(rax, Operand(rcx, 2 * kIntSize));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001031 if (tagged) {
1032 __ fstp(0); // Clear FPU stack.
1033 __ ret(kPointerSize);
1034 } else { // UNTAGGED.
1035 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1036 __ Ret();
1037 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001038
1039 __ bind(&cache_miss);
1040 // Update cache with new value.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001041 if (tagged) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001042 __ AllocateHeapNumber(rax, rdi, &runtime_call_clear_stack);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001043 } else { // UNTAGGED.
1044 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1045 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
1046 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
1047 }
1048 GenerateOperation(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001049 __ movq(Operand(rcx, 0), rbx);
1050 __ movq(Operand(rcx, 2 * kIntSize), rax);
1051 __ fstp_d(FieldOperand(rax, HeapNumber::kValueOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001052 if (tagged) {
1053 __ ret(kPointerSize);
1054 } else { // UNTAGGED.
1055 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1056 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001057
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001058 // Skip cache and return answer directly, only in untagged case.
1059 __ bind(&skip_cache);
1060 __ subq(rsp, Immediate(kDoubleSize));
1061 __ movsd(Operand(rsp, 0), xmm1);
1062 __ fld_d(Operand(rsp, 0));
1063 GenerateOperation(masm);
1064 __ fstp_d(Operand(rsp, 0));
1065 __ movsd(xmm1, Operand(rsp, 0));
1066 __ addq(rsp, Immediate(kDoubleSize));
1067 // We return the value in xmm1 without adding it to the cache, but
1068 // we cause a scavenging GC so that future allocations will succeed.
1069 __ EnterInternalFrame();
1070 // Allocate an unused object bigger than a HeapNumber.
1071 __ Push(Smi::FromInt(2 * kDoubleSize));
1072 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
1073 __ LeaveInternalFrame();
1074 __ Ret();
1075 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001076
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001077 // Call runtime, doing whatever allocation and cleanup is necessary.
1078 if (tagged) {
1079 __ bind(&runtime_call_clear_stack);
1080 __ fstp(0);
1081 __ bind(&runtime_call);
Steve Block44f0eee2011-05-26 01:26:41 +01001082 __ TailCallExternalReference(
1083 ExternalReference(RuntimeFunction(), masm->isolate()), 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001084 } else { // UNTAGGED.
1085 __ bind(&runtime_call_clear_stack);
1086 __ bind(&runtime_call);
1087 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1088 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
1089 __ EnterInternalFrame();
1090 __ push(rax);
1091 __ CallRuntime(RuntimeFunction(), 1);
1092 __ LeaveInternalFrame();
1093 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1094 __ Ret();
1095 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001096}
1097
1098
1099Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
1100 switch (type_) {
1101 // Add more cases when necessary.
1102 case TranscendentalCache::SIN: return Runtime::kMath_sin;
1103 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001104 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001105 default:
1106 UNIMPLEMENTED();
1107 return Runtime::kAbort;
1108 }
1109}
1110
1111
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001112void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001113 // Registers:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001114 // rax: Newly allocated HeapNumber, which must be preserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001115 // rbx: Bits of input double. Must be preserved.
1116 // rcx: Pointer to cache entry. Must be preserved.
1117 // st(0): Input double
1118 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001119 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
1120 // Both fsin and fcos require arguments in the range +/-2^63 and
1121 // return NaN for infinities and NaN. They can share all code except
1122 // the actual fsin/fcos operation.
1123 Label in_range;
1124 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
1125 // work. We must reduce it to the appropriate range.
1126 __ movq(rdi, rbx);
1127 // Move exponent and sign bits to low bits.
1128 __ shr(rdi, Immediate(HeapNumber::kMantissaBits));
1129 // Remove sign bit.
1130 __ andl(rdi, Immediate((1 << HeapNumber::kExponentBits) - 1));
1131 int supported_exponent_limit = (63 + HeapNumber::kExponentBias);
1132 __ cmpl(rdi, Immediate(supported_exponent_limit));
1133 __ j(below, &in_range);
1134 // Check for infinity and NaN. Both return NaN for sin.
1135 __ cmpl(rdi, Immediate(0x7ff));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001136 NearLabel non_nan_result;
1137 __ j(not_equal, &non_nan_result);
1138 // Input is +/-Infinity or NaN. Result is NaN.
1139 __ fstp(0);
1140 __ LoadRoot(kScratchRegister, Heap::kNanValueRootIndex);
1141 __ fld_d(FieldOperand(kScratchRegister, HeapNumber::kValueOffset));
1142 __ jmp(&done);
1143
1144 __ bind(&non_nan_result);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001145
Ben Murdochb0fe1622011-05-05 13:52:32 +01001146 // Use fpmod to restrict argument to the range +/-2*PI.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001147 __ movq(rdi, rax); // Save rax before using fnstsw_ax.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001148 __ fldpi();
1149 __ fadd(0);
1150 __ fld(1);
1151 // FPU Stack: input, 2*pi, input.
1152 {
1153 Label no_exceptions;
1154 __ fwait();
1155 __ fnstsw_ax();
1156 // Clear if Illegal Operand or Zero Division exceptions are set.
1157 __ testl(rax, Immediate(5)); // #IO and #ZD flags of FPU status word.
1158 __ j(zero, &no_exceptions);
1159 __ fnclex();
1160 __ bind(&no_exceptions);
1161 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001162
Ben Murdochb0fe1622011-05-05 13:52:32 +01001163 // Compute st(0) % st(1)
1164 {
1165 NearLabel partial_remainder_loop;
1166 __ bind(&partial_remainder_loop);
1167 __ fprem1();
1168 __ fwait();
1169 __ fnstsw_ax();
1170 __ testl(rax, Immediate(0x400)); // Check C2 bit of FPU status word.
1171 // If C2 is set, computation only has partial result. Loop to
1172 // continue computation.
1173 __ j(not_zero, &partial_remainder_loop);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001174 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001175 // FPU Stack: input, 2*pi, input % 2*pi
1176 __ fstp(2);
1177 // FPU Stack: input % 2*pi, 2*pi,
1178 __ fstp(0);
1179 // FPU Stack: input % 2*pi
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001180 __ movq(rax, rdi); // Restore rax, pointer to the new HeapNumber.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001181 __ bind(&in_range);
1182 switch (type_) {
1183 case TranscendentalCache::SIN:
1184 __ fsin();
1185 break;
1186 case TranscendentalCache::COS:
1187 __ fcos();
1188 break;
1189 default:
1190 UNREACHABLE();
1191 }
1192 __ bind(&done);
1193 } else {
1194 ASSERT(type_ == TranscendentalCache::LOG);
1195 __ fldln2();
1196 __ fxch();
1197 __ fyl2x();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001198 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001199}
1200
1201
1202// Get the integer part of a heap number.
1203// Overwrites the contents of rdi, rbx and rcx. Result cannot be rdi or rbx.
1204void IntegerConvert(MacroAssembler* masm,
1205 Register result,
1206 Register source) {
1207 // Result may be rcx. If result and source are the same register, source will
1208 // be overwritten.
1209 ASSERT(!result.is(rdi) && !result.is(rbx));
1210 // TODO(lrn): When type info reaches here, if value is a 32-bit integer, use
1211 // cvttsd2si (32-bit version) directly.
1212 Register double_exponent = rbx;
1213 Register double_value = rdi;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001214 NearLabel done, exponent_63_plus;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001215 // Get double and extract exponent.
1216 __ movq(double_value, FieldOperand(source, HeapNumber::kValueOffset));
1217 // Clear result preemptively, in case we need to return zero.
1218 __ xorl(result, result);
1219 __ movq(xmm0, double_value); // Save copy in xmm0 in case we need it there.
1220 // Double to remove sign bit, shift exponent down to least significant bits.
1221 // and subtract bias to get the unshifted, unbiased exponent.
1222 __ lea(double_exponent, Operand(double_value, double_value, times_1, 0));
1223 __ shr(double_exponent, Immediate(64 - HeapNumber::kExponentBits));
1224 __ subl(double_exponent, Immediate(HeapNumber::kExponentBias));
1225 // Check whether the exponent is too big for a 63 bit unsigned integer.
1226 __ cmpl(double_exponent, Immediate(63));
1227 __ j(above_equal, &exponent_63_plus);
1228 // Handle exponent range 0..62.
1229 __ cvttsd2siq(result, xmm0);
1230 __ jmp(&done);
1231
1232 __ bind(&exponent_63_plus);
1233 // Exponent negative or 63+.
1234 __ cmpl(double_exponent, Immediate(83));
1235 // If exponent negative or above 83, number contains no significant bits in
1236 // the range 0..2^31, so result is zero, and rcx already holds zero.
1237 __ j(above, &done);
1238
1239 // Exponent in rage 63..83.
1240 // Mantissa * 2^exponent contains bits in the range 2^0..2^31, namely
1241 // the least significant exponent-52 bits.
1242
1243 // Negate low bits of mantissa if value is negative.
1244 __ addq(double_value, double_value); // Move sign bit to carry.
1245 __ sbbl(result, result); // And convert carry to -1 in result register.
1246 // if scratch2 is negative, do (scratch2-1)^-1, otherwise (scratch2-0)^0.
1247 __ addl(double_value, result);
1248 // Do xor in opposite directions depending on where we want the result
1249 // (depending on whether result is rcx or not).
1250
1251 if (result.is(rcx)) {
1252 __ xorl(double_value, result);
1253 // Left shift mantissa by (exponent - mantissabits - 1) to save the
1254 // bits that have positional values below 2^32 (the extra -1 comes from the
1255 // doubling done above to move the sign bit into the carry flag).
1256 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
1257 __ shll_cl(double_value);
1258 __ movl(result, double_value);
1259 } else {
1260 // As the then-branch, but move double-value to result before shifting.
1261 __ xorl(result, double_value);
1262 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
1263 __ shll_cl(result);
1264 }
1265
1266 __ bind(&done);
1267}
1268
1269
1270// Input: rdx, rax are the left and right objects of a bit op.
1271// Output: rax, rcx are left and right integers for a bit op.
1272void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm) {
1273 // Check float operands.
1274 Label done;
1275 Label rax_is_smi;
1276 Label rax_is_object;
1277 Label rdx_is_object;
1278
1279 __ JumpIfNotSmi(rdx, &rdx_is_object);
1280 __ SmiToInteger32(rdx, rdx);
1281 __ JumpIfSmi(rax, &rax_is_smi);
1282
1283 __ bind(&rax_is_object);
1284 IntegerConvert(masm, rcx, rax); // Uses rdi, rcx and rbx.
1285 __ jmp(&done);
1286
1287 __ bind(&rdx_is_object);
1288 IntegerConvert(masm, rdx, rdx); // Uses rdi, rcx and rbx.
1289 __ JumpIfNotSmi(rax, &rax_is_object);
1290 __ bind(&rax_is_smi);
1291 __ SmiToInteger32(rcx, rax);
1292
1293 __ bind(&done);
1294 __ movl(rax, rdx);
1295}
1296
1297
1298// Input: rdx, rax are the left and right objects of a bit op.
1299// Output: rax, rcx are left and right integers for a bit op.
Steve Block1e0659c2011-05-24 12:43:12 +01001300// Jump to conversion_failure: rdx and rax are unchanged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001301void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
1302 Label* conversion_failure,
1303 Register heap_number_map) {
1304 // Check float operands.
1305 Label arg1_is_object, check_undefined_arg1;
1306 Label arg2_is_object, check_undefined_arg2;
1307 Label load_arg2, done;
1308
1309 __ JumpIfNotSmi(rdx, &arg1_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001310 __ SmiToInteger32(r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001311 __ jmp(&load_arg2);
1312
1313 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1314 __ bind(&check_undefined_arg1);
1315 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
1316 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001317 __ Set(r8, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001318 __ jmp(&load_arg2);
1319
1320 __ bind(&arg1_is_object);
1321 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), heap_number_map);
1322 __ j(not_equal, &check_undefined_arg1);
Steve Block1e0659c2011-05-24 12:43:12 +01001323 // Get the untagged integer version of the rdx heap number in rcx.
1324 IntegerConvert(masm, r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001325
Steve Block1e0659c2011-05-24 12:43:12 +01001326 // Here r8 has the untagged integer, rax has a Smi or a heap number.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001327 __ bind(&load_arg2);
1328 // Test if arg2 is a Smi.
1329 __ JumpIfNotSmi(rax, &arg2_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001330 __ SmiToInteger32(rcx, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001331 __ jmp(&done);
1332
1333 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1334 __ bind(&check_undefined_arg2);
1335 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1336 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001337 __ Set(rcx, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001338 __ jmp(&done);
1339
1340 __ bind(&arg2_is_object);
1341 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), heap_number_map);
1342 __ j(not_equal, &check_undefined_arg2);
1343 // Get the untagged integer version of the rax heap number in rcx.
1344 IntegerConvert(masm, rcx, rax);
1345 __ bind(&done);
Steve Block1e0659c2011-05-24 12:43:12 +01001346 __ movl(rax, r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001347}
1348
1349
1350void FloatingPointHelper::LoadSSE2SmiOperands(MacroAssembler* masm) {
1351 __ SmiToInteger32(kScratchRegister, rdx);
1352 __ cvtlsi2sd(xmm0, kScratchRegister);
1353 __ SmiToInteger32(kScratchRegister, rax);
1354 __ cvtlsi2sd(xmm1, kScratchRegister);
1355}
1356
1357
1358void FloatingPointHelper::LoadSSE2NumberOperands(MacroAssembler* masm) {
1359 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, done;
1360 // Load operand in rdx into xmm0.
1361 __ JumpIfSmi(rdx, &load_smi_rdx);
1362 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1363 // Load operand in rax into xmm1.
1364 __ JumpIfSmi(rax, &load_smi_rax);
1365 __ bind(&load_nonsmi_rax);
1366 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1367 __ jmp(&done);
1368
1369 __ bind(&load_smi_rdx);
1370 __ SmiToInteger32(kScratchRegister, rdx);
1371 __ cvtlsi2sd(xmm0, kScratchRegister);
1372 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1373
1374 __ bind(&load_smi_rax);
1375 __ SmiToInteger32(kScratchRegister, rax);
1376 __ cvtlsi2sd(xmm1, kScratchRegister);
1377
1378 __ bind(&done);
1379}
1380
1381
1382void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
1383 Label* not_numbers) {
1384 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
1385 // Load operand in rdx into xmm0, or branch to not_numbers.
1386 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
1387 __ JumpIfSmi(rdx, &load_smi_rdx);
1388 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
1389 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
1390 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1391 // Load operand in rax into xmm1, or branch to not_numbers.
1392 __ JumpIfSmi(rax, &load_smi_rax);
1393
1394 __ bind(&load_nonsmi_rax);
1395 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), rcx);
1396 __ j(not_equal, not_numbers);
1397 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1398 __ jmp(&done);
1399
1400 __ bind(&load_smi_rdx);
1401 __ SmiToInteger32(kScratchRegister, rdx);
1402 __ cvtlsi2sd(xmm0, kScratchRegister);
1403 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1404
1405 __ bind(&load_smi_rax);
1406 __ SmiToInteger32(kScratchRegister, rax);
1407 __ cvtlsi2sd(xmm1, kScratchRegister);
1408 __ bind(&done);
1409}
1410
1411
Ben Murdoch8b112d22011-06-08 16:22:53 +01001412void FloatingPointHelper::NumbersToSmis(MacroAssembler* masm,
1413 Register first,
1414 Register second,
1415 Register scratch1,
1416 Register scratch2,
1417 Register scratch3,
1418 Label* on_success,
1419 Label* on_not_smis) {
1420 Register heap_number_map = scratch3;
1421 Register smi_result = scratch1;
1422 Label done;
1423
1424 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1425
1426 NearLabel first_smi, check_second;
1427 __ JumpIfSmi(first, &first_smi);
1428 __ cmpq(FieldOperand(first, HeapObject::kMapOffset), heap_number_map);
1429 __ j(not_equal, on_not_smis);
1430 // Convert HeapNumber to smi if possible.
1431 __ movsd(xmm0, FieldOperand(first, HeapNumber::kValueOffset));
1432 __ movq(scratch2, xmm0);
1433 __ cvttsd2siq(smi_result, xmm0);
1434 // Check if conversion was successful by converting back and
1435 // comparing to the original double's bits.
1436 __ cvtlsi2sd(xmm1, smi_result);
1437 __ movq(kScratchRegister, xmm1);
1438 __ cmpq(scratch2, kScratchRegister);
1439 __ j(not_equal, on_not_smis);
1440 __ Integer32ToSmi(first, smi_result);
1441
1442 __ bind(&check_second);
1443 __ JumpIfSmi(second, (on_success != NULL) ? on_success : &done);
1444 __ bind(&first_smi);
1445 if (FLAG_debug_code) {
1446 // Second should be non-smi if we get here.
1447 __ AbortIfSmi(second);
1448 }
1449 __ cmpq(FieldOperand(second, HeapObject::kMapOffset), heap_number_map);
1450 __ j(not_equal, on_not_smis);
1451 // Convert second to smi, if possible.
1452 __ movsd(xmm0, FieldOperand(second, HeapNumber::kValueOffset));
1453 __ movq(scratch2, xmm0);
1454 __ cvttsd2siq(smi_result, xmm0);
1455 __ cvtlsi2sd(xmm1, smi_result);
1456 __ movq(kScratchRegister, xmm1);
1457 __ cmpq(scratch2, kScratchRegister);
1458 __ j(not_equal, on_not_smis);
1459 __ Integer32ToSmi(second, smi_result);
1460 if (on_success != NULL) {
1461 __ jmp(on_success);
1462 } else {
1463 __ bind(&done);
1464 }
1465}
1466
1467
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001468void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
1469 Label slow, done;
1470
1471 if (op_ == Token::SUB) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001472 if (include_smi_code_) {
1473 // Check whether the value is a smi.
1474 Label try_float;
1475 __ JumpIfNotSmi(rax, &try_float);
1476 if (negative_zero_ == kIgnoreNegativeZero) {
1477 __ SmiCompare(rax, Smi::FromInt(0));
1478 __ j(equal, &done);
1479 }
1480 __ SmiNeg(rax, rax, &done);
Ben Murdochf87a2032010-10-22 12:50:53 +01001481 __ jmp(&slow); // zero, if not handled above, and Smi::kMinValue.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001482
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001483 // Try floating point case.
1484 __ bind(&try_float);
1485 } else if (FLAG_debug_code) {
1486 __ AbortIfSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001487 }
1488
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001489 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
1490 Heap::kHeapNumberMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001491 __ j(not_equal, &slow);
1492 // Operand is a float, negate its value by flipping sign bit.
1493 __ movq(rdx, FieldOperand(rax, HeapNumber::kValueOffset));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001494 __ Set(kScratchRegister, 0x01);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001495 __ shl(kScratchRegister, Immediate(63));
1496 __ xor_(rdx, kScratchRegister); // Flip sign.
1497 // rdx is value to store.
1498 if (overwrite_ == UNARY_OVERWRITE) {
1499 __ movq(FieldOperand(rax, HeapNumber::kValueOffset), rdx);
1500 } else {
1501 __ AllocateHeapNumber(rcx, rbx, &slow);
1502 // rcx: allocated 'empty' number
1503 __ movq(FieldOperand(rcx, HeapNumber::kValueOffset), rdx);
1504 __ movq(rax, rcx);
1505 }
1506 } else if (op_ == Token::BIT_NOT) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001507 if (include_smi_code_) {
1508 Label try_float;
1509 __ JumpIfNotSmi(rax, &try_float);
1510 __ SmiNot(rax, rax);
1511 __ jmp(&done);
1512 // Try floating point case.
1513 __ bind(&try_float);
1514 } else if (FLAG_debug_code) {
1515 __ AbortIfSmi(rax);
1516 }
1517
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001518 // Check if the operand is a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001519 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
1520 Heap::kHeapNumberMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001521 __ j(not_equal, &slow);
1522
1523 // Convert the heap number in rax to an untagged integer in rcx.
1524 IntegerConvert(masm, rax, rax);
1525
1526 // Do the bitwise operation and smi tag the result.
1527 __ notl(rax);
1528 __ Integer32ToSmi(rax, rax);
1529 }
1530
1531 // Return from the stub.
1532 __ bind(&done);
1533 __ StubReturn(1);
1534
1535 // Handle the slow case by jumping to the JavaScript builtin.
1536 __ bind(&slow);
1537 __ pop(rcx); // pop return address
1538 __ push(rax);
1539 __ push(rcx); // push return address
1540 switch (op_) {
1541 case Token::SUB:
1542 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
1543 break;
1544 case Token::BIT_NOT:
1545 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
1546 break;
1547 default:
1548 UNREACHABLE();
1549 }
1550}
1551
1552
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001553void MathPowStub::Generate(MacroAssembler* masm) {
1554 // Registers are used as follows:
1555 // rdx = base
1556 // rax = exponent
1557 // rcx = temporary, result
1558
1559 Label allocate_return, call_runtime;
1560
1561 // Load input parameters.
1562 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
1563 __ movq(rax, Operand(rsp, 1 * kPointerSize));
1564
1565 // Save 1 in xmm3 - we need this several times later on.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001566 __ Set(rcx, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001567 __ cvtlsi2sd(xmm3, rcx);
1568
1569 Label exponent_nonsmi;
1570 Label base_nonsmi;
1571 // If the exponent is a heap number go to that specific case.
1572 __ JumpIfNotSmi(rax, &exponent_nonsmi);
1573 __ JumpIfNotSmi(rdx, &base_nonsmi);
1574
1575 // Optimized version when both exponent and base are smis.
1576 Label powi;
1577 __ SmiToInteger32(rdx, rdx);
1578 __ cvtlsi2sd(xmm0, rdx);
1579 __ jmp(&powi);
1580 // Exponent is a smi and base is a heapnumber.
1581 __ bind(&base_nonsmi);
1582 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
1583 Heap::kHeapNumberMapRootIndex);
1584 __ j(not_equal, &call_runtime);
1585
1586 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1587
1588 // Optimized version of pow if exponent is a smi.
1589 // xmm0 contains the base.
1590 __ bind(&powi);
1591 __ SmiToInteger32(rax, rax);
1592
1593 // Save exponent in base as we need to check if exponent is negative later.
1594 // We know that base and exponent are in different registers.
1595 __ movq(rdx, rax);
1596
1597 // Get absolute value of exponent.
1598 NearLabel no_neg;
1599 __ cmpl(rax, Immediate(0));
1600 __ j(greater_equal, &no_neg);
1601 __ negl(rax);
1602 __ bind(&no_neg);
1603
1604 // Load xmm1 with 1.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001605 __ movaps(xmm1, xmm3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001606 NearLabel while_true;
1607 NearLabel no_multiply;
1608
1609 __ bind(&while_true);
1610 __ shrl(rax, Immediate(1));
1611 __ j(not_carry, &no_multiply);
1612 __ mulsd(xmm1, xmm0);
1613 __ bind(&no_multiply);
1614 __ mulsd(xmm0, xmm0);
1615 __ j(not_zero, &while_true);
1616
1617 // Base has the original value of the exponent - if the exponent is
1618 // negative return 1/result.
1619 __ testl(rdx, rdx);
1620 __ j(positive, &allocate_return);
1621 // Special case if xmm1 has reached infinity.
1622 __ divsd(xmm3, xmm1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001623 __ movaps(xmm1, xmm3);
1624 __ xorps(xmm0, xmm0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001625 __ ucomisd(xmm0, xmm1);
1626 __ j(equal, &call_runtime);
1627
1628 __ jmp(&allocate_return);
1629
1630 // Exponent (or both) is a heapnumber - no matter what we should now work
1631 // on doubles.
1632 __ bind(&exponent_nonsmi);
1633 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
1634 Heap::kHeapNumberMapRootIndex);
1635 __ j(not_equal, &call_runtime);
1636 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1637 // Test if exponent is nan.
1638 __ ucomisd(xmm1, xmm1);
1639 __ j(parity_even, &call_runtime);
1640
1641 NearLabel base_not_smi;
1642 NearLabel handle_special_cases;
1643 __ JumpIfNotSmi(rdx, &base_not_smi);
1644 __ SmiToInteger32(rdx, rdx);
1645 __ cvtlsi2sd(xmm0, rdx);
1646 __ jmp(&handle_special_cases);
1647
1648 __ bind(&base_not_smi);
1649 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
1650 Heap::kHeapNumberMapRootIndex);
1651 __ j(not_equal, &call_runtime);
1652 __ movl(rcx, FieldOperand(rdx, HeapNumber::kExponentOffset));
1653 __ andl(rcx, Immediate(HeapNumber::kExponentMask));
1654 __ cmpl(rcx, Immediate(HeapNumber::kExponentMask));
1655 // base is NaN or +/-Infinity
1656 __ j(greater_equal, &call_runtime);
1657 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1658
1659 // base is in xmm0 and exponent is in xmm1.
1660 __ bind(&handle_special_cases);
1661 NearLabel not_minus_half;
1662 // Test for -0.5.
1663 // Load xmm2 with -0.5.
1664 __ movq(rcx, V8_UINT64_C(0xBFE0000000000000), RelocInfo::NONE);
1665 __ movq(xmm2, rcx);
1666 // xmm2 now has -0.5.
1667 __ ucomisd(xmm2, xmm1);
1668 __ j(not_equal, &not_minus_half);
1669
1670 // Calculates reciprocal of square root.
1671 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001672 __ xorps(xmm1, xmm1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001673 __ addsd(xmm1, xmm0);
1674 __ sqrtsd(xmm1, xmm1);
1675 __ divsd(xmm3, xmm1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001676 __ movaps(xmm1, xmm3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001677 __ jmp(&allocate_return);
1678
1679 // Test for 0.5.
1680 __ bind(&not_minus_half);
1681 // Load xmm2 with 0.5.
1682 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
1683 __ addsd(xmm2, xmm3);
1684 // xmm2 now has 0.5.
1685 __ ucomisd(xmm2, xmm1);
1686 __ j(not_equal, &call_runtime);
1687 // Calculates square root.
1688 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001689 __ xorps(xmm1, xmm1);
1690 __ addsd(xmm1, xmm0); // Convert -0 to 0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001691 __ sqrtsd(xmm1, xmm1);
1692
1693 __ bind(&allocate_return);
1694 __ AllocateHeapNumber(rcx, rax, &call_runtime);
1695 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm1);
1696 __ movq(rax, rcx);
1697 __ ret(2 * kPointerSize);
1698
1699 __ bind(&call_runtime);
1700 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
1701}
1702
1703
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001704void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
1705 // The key is in rdx and the parameter count is in rax.
1706
1707 // The displacement is used for skipping the frame pointer on the
1708 // stack. It is the offset of the last parameter (if any) relative
1709 // to the frame pointer.
1710 static const int kDisplacement = 1 * kPointerSize;
1711
1712 // Check that the key is a smi.
1713 Label slow;
1714 __ JumpIfNotSmi(rdx, &slow);
1715
Steve Block44f0eee2011-05-26 01:26:41 +01001716 // Check if the calling frame is an arguments adaptor frame. We look at the
1717 // context offset, and if the frame is not a regular one, then we find a
1718 // Smi instead of the context. We can't use SmiCompare here, because that
1719 // only works for comparing two smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001720 Label adaptor;
1721 __ movq(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001722 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
1723 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001724 __ j(equal, &adaptor);
1725
1726 // Check index against formal parameters count limit passed in
1727 // through register rax. Use unsigned comparison to get negative
1728 // check for free.
1729 __ cmpq(rdx, rax);
1730 __ j(above_equal, &slow);
1731
1732 // Read the argument from the stack and return it.
1733 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
1734 __ lea(rbx, Operand(rbp, index.reg, index.scale, 0));
1735 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
1736 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
1737 __ Ret();
1738
1739 // Arguments adaptor case: Check index against actual arguments
1740 // limit found in the arguments adaptor frame. Use unsigned
1741 // comparison to get negative check for free.
1742 __ bind(&adaptor);
1743 __ movq(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1744 __ cmpq(rdx, rcx);
1745 __ j(above_equal, &slow);
1746
1747 // Read the argument from the stack and return it.
1748 index = masm->SmiToIndex(rax, rcx, kPointerSizeLog2);
1749 __ lea(rbx, Operand(rbx, index.reg, index.scale, 0));
1750 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
1751 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
1752 __ Ret();
1753
1754 // Slow-case: Handle non-smi or out-of-bounds access to arguments
1755 // by calling the runtime system.
1756 __ bind(&slow);
1757 __ pop(rbx); // Return address.
1758 __ push(rdx);
1759 __ push(rbx);
1760 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
1761}
1762
1763
1764void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
1765 // rsp[0] : return address
1766 // rsp[8] : number of parameters
1767 // rsp[16] : receiver displacement
1768 // rsp[24] : function
1769
1770 // The displacement is used for skipping the return address and the
1771 // frame pointer on the stack. It is the offset of the last
1772 // parameter (if any) relative to the frame pointer.
1773 static const int kDisplacement = 2 * kPointerSize;
1774
1775 // Check if the calling frame is an arguments adaptor frame.
1776 Label adaptor_frame, try_allocate, runtime;
1777 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001778 __ Cmp(Operand(rdx, StandardFrameConstants::kContextOffset),
1779 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001780 __ j(equal, &adaptor_frame);
1781
1782 // Get the length from the frame.
1783 __ SmiToInteger32(rcx, Operand(rsp, 1 * kPointerSize));
1784 __ jmp(&try_allocate);
1785
1786 // Patch the arguments.length and the parameters pointer.
1787 __ bind(&adaptor_frame);
1788 __ SmiToInteger32(rcx,
1789 Operand(rdx,
1790 ArgumentsAdaptorFrameConstants::kLengthOffset));
1791 // Space on stack must already hold a smi.
1792 __ Integer32ToSmiField(Operand(rsp, 1 * kPointerSize), rcx);
1793 // Do not clobber the length index for the indexing operation since
1794 // it is used compute the size for allocation later.
1795 __ lea(rdx, Operand(rdx, rcx, times_pointer_size, kDisplacement));
1796 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
1797
1798 // Try the new space allocation. Start out with computing the size of
1799 // the arguments object and the elements array.
1800 Label add_arguments_object;
1801 __ bind(&try_allocate);
1802 __ testl(rcx, rcx);
1803 __ j(zero, &add_arguments_object);
1804 __ leal(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
1805 __ bind(&add_arguments_object);
Steve Block44f0eee2011-05-26 01:26:41 +01001806 __ addl(rcx, Immediate(GetArgumentsObjectSize()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001807
1808 // Do the allocation of both objects in one go.
1809 __ AllocateInNewSpace(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
1810
1811 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001812 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
1813 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001814 __ movq(rdi, Operand(rdi,
1815 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001816
1817 // Copy the JS object part.
1818 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
1819 __ movq(kScratchRegister, FieldOperand(rdi, 0 * kPointerSize));
1820 __ movq(rdx, FieldOperand(rdi, 1 * kPointerSize));
1821 __ movq(rbx, FieldOperand(rdi, 2 * kPointerSize));
1822 __ movq(FieldOperand(rax, 0 * kPointerSize), kScratchRegister);
1823 __ movq(FieldOperand(rax, 1 * kPointerSize), rdx);
1824 __ movq(FieldOperand(rax, 2 * kPointerSize), rbx);
1825
Steve Block44f0eee2011-05-26 01:26:41 +01001826 if (type_ == NEW_NON_STRICT) {
1827 // Setup the callee in-object property.
1828 ASSERT(Heap::kArgumentsCalleeIndex == 1);
1829 __ movq(kScratchRegister, Operand(rsp, 3 * kPointerSize));
1830 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
1831 Heap::kArgumentsCalleeIndex * kPointerSize),
1832 kScratchRegister);
1833 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001834
1835 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01001836 ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001837 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001838 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
1839 Heap::kArgumentsLengthIndex * kPointerSize),
1840 rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001841
1842 // If there are no actual arguments, we're done.
1843 Label done;
1844 __ SmiTest(rcx);
1845 __ j(zero, &done);
1846
1847 // Get the parameters pointer from the stack and untag the length.
1848 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
1849
1850 // Setup the elements pointer in the allocated arguments object and
1851 // initialize the header in the elements fixed array.
Steve Block44f0eee2011-05-26 01:26:41 +01001852 __ lea(rdi, Operand(rax, GetArgumentsObjectSize()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001853 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
1854 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
1855 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
1856 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
1857 __ SmiToInteger32(rcx, rcx); // Untag length for the loop below.
1858
1859 // Copy the fixed array slots.
1860 Label loop;
1861 __ bind(&loop);
1862 __ movq(kScratchRegister, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
1863 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize), kScratchRegister);
1864 __ addq(rdi, Immediate(kPointerSize));
1865 __ subq(rdx, Immediate(kPointerSize));
1866 __ decl(rcx);
1867 __ j(not_zero, &loop);
1868
1869 // Return and remove the on-stack parameters.
1870 __ bind(&done);
1871 __ ret(3 * kPointerSize);
1872
1873 // Do the runtime call to allocate the arguments object.
1874 __ bind(&runtime);
1875 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
1876}
1877
1878
1879void RegExpExecStub::Generate(MacroAssembler* masm) {
1880 // Just jump directly to runtime if native RegExp is not selected at compile
1881 // time or if regexp entry in generated code is turned off runtime switch or
1882 // at compilation.
1883#ifdef V8_INTERPRETED_REGEXP
1884 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1885#else // V8_INTERPRETED_REGEXP
1886 if (!FLAG_regexp_entry_native) {
1887 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1888 return;
1889 }
1890
1891 // Stack frame on entry.
Steve Block1e0659c2011-05-24 12:43:12 +01001892 // rsp[0]: return address
1893 // rsp[8]: last_match_info (expected JSArray)
1894 // rsp[16]: previous index
1895 // rsp[24]: subject string
1896 // rsp[32]: JSRegExp object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001897
1898 static const int kLastMatchInfoOffset = 1 * kPointerSize;
1899 static const int kPreviousIndexOffset = 2 * kPointerSize;
1900 static const int kSubjectOffset = 3 * kPointerSize;
1901 static const int kJSRegExpOffset = 4 * kPointerSize;
1902
1903 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001904 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01001905 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001906 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01001907 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001908 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01001909 ExternalReference::address_of_regexp_stack_memory_size(isolate);
1910 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001911 __ testq(kScratchRegister, kScratchRegister);
1912 __ j(zero, &runtime);
1913
1914
1915 // Check that the first argument is a JSRegExp object.
1916 __ movq(rax, Operand(rsp, kJSRegExpOffset));
1917 __ JumpIfSmi(rax, &runtime);
1918 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
1919 __ j(not_equal, &runtime);
1920 // Check that the RegExp has been compiled (data contains a fixed array).
Steve Block44f0eee2011-05-26 01:26:41 +01001921 __ movq(rax, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001922 if (FLAG_debug_code) {
Steve Block44f0eee2011-05-26 01:26:41 +01001923 Condition is_smi = masm->CheckSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001924 __ Check(NegateCondition(is_smi),
1925 "Unexpected type for RegExp data, FixedArray expected");
Steve Block44f0eee2011-05-26 01:26:41 +01001926 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001927 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
1928 }
1929
Steve Block44f0eee2011-05-26 01:26:41 +01001930 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001931 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
Steve Block44f0eee2011-05-26 01:26:41 +01001932 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001933 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
1934 __ j(not_equal, &runtime);
1935
Steve Block44f0eee2011-05-26 01:26:41 +01001936 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001937 // Check that the number of captures fit in the static offsets vector buffer.
1938 __ SmiToInteger32(rdx,
Steve Block44f0eee2011-05-26 01:26:41 +01001939 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001940 // Calculate number of capture registers (number_of_captures + 1) * 2.
1941 __ leal(rdx, Operand(rdx, rdx, times_1, 2));
1942 // Check that the static offsets vector buffer is large enough.
1943 __ cmpl(rdx, Immediate(OffsetsVector::kStaticOffsetsVectorSize));
1944 __ j(above, &runtime);
1945
Steve Block44f0eee2011-05-26 01:26:41 +01001946 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001947 // rdx: Number of capture registers
1948 // Check that the second argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001949 __ movq(rdi, Operand(rsp, kSubjectOffset));
1950 __ JumpIfSmi(rdi, &runtime);
1951 Condition is_string = masm->IsObjectStringType(rdi, rbx, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001952 __ j(NegateCondition(is_string), &runtime);
1953
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001954 // rdi: Subject string.
1955 // rax: RegExp data (FixedArray).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001956 // rdx: Number of capture registers.
1957 // Check that the third argument is a positive smi less than the string
1958 // length. A negative value will be greater (unsigned comparison).
1959 __ movq(rbx, Operand(rsp, kPreviousIndexOffset));
1960 __ JumpIfNotSmi(rbx, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001961 __ SmiCompare(rbx, FieldOperand(rdi, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001962 __ j(above_equal, &runtime);
1963
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001964 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001965 // rdx: Number of capture registers
1966 // Check that the fourth object is a JSArray object.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001967 __ movq(rdi, Operand(rsp, kLastMatchInfoOffset));
1968 __ JumpIfSmi(rdi, &runtime);
1969 __ CmpObjectType(rdi, JS_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001970 __ j(not_equal, &runtime);
1971 // Check that the JSArray is in fast case.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001972 __ movq(rbx, FieldOperand(rdi, JSArray::kElementsOffset));
1973 __ movq(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001974 __ CompareRoot(FieldOperand(rbx, HeapObject::kMapOffset),
1975 Heap::kFixedArrayMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001976 __ j(not_equal, &runtime);
1977 // Check that the last match info has space for the capture registers and the
1978 // additional information. Ensure no overflow in add.
1979 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001980 __ SmiToInteger32(rdi, FieldOperand(rbx, FixedArray::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001981 __ addl(rdx, Immediate(RegExpImpl::kLastMatchOverhead));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001982 __ cmpl(rdx, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001983 __ j(greater, &runtime);
1984
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001985 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001986 // Check the representation and encoding of the subject string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001987 NearLabel seq_ascii_string, seq_two_byte_string, check_code;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001988 __ movq(rdi, Operand(rsp, kSubjectOffset));
1989 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001990 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1991 // First check for flat two byte string.
1992 __ andb(rbx, Immediate(
1993 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask));
1994 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1995 __ j(zero, &seq_two_byte_string);
1996 // Any other flat string must be a flat ascii string.
1997 __ testb(rbx, Immediate(kIsNotStringMask | kStringRepresentationMask));
1998 __ j(zero, &seq_ascii_string);
1999
2000 // Check for flat cons string.
2001 // A flat cons string is a cons string where the second part is the empty
2002 // string. In that case the subject string is just the first part of the cons
2003 // string. Also in this case the first part of the cons string is known to be
2004 // a sequential string or an external string.
2005 STATIC_ASSERT(kExternalStringTag !=0);
2006 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
2007 __ testb(rbx, Immediate(kIsNotStringMask | kExternalStringTag));
2008 __ j(not_zero, &runtime);
2009 // String is a cons string.
Steve Block44f0eee2011-05-26 01:26:41 +01002010 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
2011 Heap::kEmptyStringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002012 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002013 __ movq(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
2014 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002015 // String is a cons string with empty second part.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002016 // rdi: first part of cons string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002017 // rbx: map of first part of cons string.
2018 // Is first part a flat two byte string?
2019 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2020 Immediate(kStringRepresentationMask | kStringEncodingMask));
2021 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
2022 __ j(zero, &seq_two_byte_string);
2023 // Any other flat string must be ascii.
2024 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2025 Immediate(kStringRepresentationMask));
2026 __ j(not_zero, &runtime);
2027
2028 __ bind(&seq_ascii_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002029 // rdi: subject string (sequential ascii)
2030 // rax: RegExp data (FixedArray)
2031 __ movq(r11, FieldOperand(rax, JSRegExp::kDataAsciiCodeOffset));
2032 __ Set(rcx, 1); // Type is ascii.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002033 __ jmp(&check_code);
2034
2035 __ bind(&seq_two_byte_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002036 // rdi: subject string (flat two-byte)
2037 // rax: RegExp data (FixedArray)
2038 __ movq(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
2039 __ Set(rcx, 0); // Type is two byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002040
2041 __ bind(&check_code);
2042 // Check that the irregexp code has been generated for the actual string
2043 // encoding. If it has, the field contains a code object otherwise it contains
2044 // the hole.
2045 __ CmpObjectType(r11, CODE_TYPE, kScratchRegister);
2046 __ j(not_equal, &runtime);
2047
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002048 // rdi: subject string
2049 // rcx: encoding of subject string (1 if ascii, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002050 // r11: code
2051 // Load used arguments before starting to push arguments for call to native
2052 // RegExp code to avoid handling changing stack height.
2053 __ SmiToInteger64(rbx, Operand(rsp, kPreviousIndexOffset));
2054
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002055 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002056 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002057 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002058 // r11: code
2059 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002060 Counters* counters = masm->isolate()->counters();
2061 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002062
Steve Block44f0eee2011-05-26 01:26:41 +01002063 // Isolates: note we add an additional parameter here (isolate pointer).
2064 static const int kRegExpExecuteArguments = 8;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002065 int argument_slots_on_stack =
2066 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
Steve Block44f0eee2011-05-26 01:26:41 +01002067 __ EnterApiExitFrame(argument_slots_on_stack);
2068
2069 // Argument 8: Pass current isolate address.
2070 // __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2071 // Immediate(ExternalReference::isolate_address()));
2072 __ LoadAddress(kScratchRegister, ExternalReference::isolate_address());
2073 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2074 kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002075
2076 // Argument 7: Indicate that this is a direct call from JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002077 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kPointerSize),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002078 Immediate(1));
2079
2080 // Argument 6: Start (high end) of backtracking stack memory area.
2081 __ movq(kScratchRegister, address_of_regexp_stack_memory_address);
2082 __ movq(r9, Operand(kScratchRegister, 0));
2083 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
2084 __ addq(r9, Operand(kScratchRegister, 0));
2085 // Argument 6 passed in r9 on Linux and on the stack on Windows.
2086#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002087 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kPointerSize), r9);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002088#endif
2089
2090 // Argument 5: static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01002091 __ LoadAddress(r8,
2092 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002093 // Argument 5 passed in r8 on Linux and on the stack on Windows.
2094#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002095 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kPointerSize), r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002096#endif
2097
2098 // First four arguments are passed in registers on both Linux and Windows.
2099#ifdef _WIN64
2100 Register arg4 = r9;
2101 Register arg3 = r8;
2102 Register arg2 = rdx;
2103 Register arg1 = rcx;
2104#else
2105 Register arg4 = rcx;
2106 Register arg3 = rdx;
2107 Register arg2 = rsi;
2108 Register arg1 = rdi;
2109#endif
2110
2111 // Keep track on aliasing between argX defined above and the registers used.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002112 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002113 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002114 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002115 // r11: code
2116
2117 // Argument 4: End of string data
2118 // Argument 3: Start of string data
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002119 NearLabel setup_two_byte, setup_rest;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002120 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002121 __ j(zero, &setup_two_byte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002122 __ SmiToInteger32(rcx, FieldOperand(rdi, String::kLengthOffset));
2123 __ lea(arg4, FieldOperand(rdi, rcx, times_1, SeqAsciiString::kHeaderSize));
2124 __ lea(arg3, FieldOperand(rdi, rbx, times_1, SeqAsciiString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002125 __ jmp(&setup_rest);
2126 __ bind(&setup_two_byte);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002127 __ SmiToInteger32(rcx, FieldOperand(rdi, String::kLengthOffset));
2128 __ lea(arg4, FieldOperand(rdi, rcx, times_2, SeqTwoByteString::kHeaderSize));
2129 __ lea(arg3, FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002130
2131 __ bind(&setup_rest);
2132 // Argument 2: Previous index.
2133 __ movq(arg2, rbx);
2134
2135 // Argument 1: Subject string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002136#ifdef _WIN64
2137 __ movq(arg1, rdi);
2138#else
2139 // Already there in AMD64 calling convention.
2140 ASSERT(arg1.is(rdi));
2141#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002142
2143 // Locate the code entry and call it.
2144 __ addq(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002145 __ call(r11);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002146
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002147 __ LeaveApiExitFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002148
2149 // Check the result.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002150 NearLabel success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002151 Label exception;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002152 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::SUCCESS));
2153 __ j(equal, &success);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002154 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002155 __ j(equal, &exception);
2156 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
2157 // If none of the above, it can only be retry.
2158 // Handle that in the runtime system.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002159 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002160
2161 // For failure return null.
2162 __ LoadRoot(rax, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002163 __ ret(4 * kPointerSize);
2164
2165 // Load RegExp data.
2166 __ bind(&success);
2167 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2168 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
2169 __ SmiToInteger32(rax,
2170 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
2171 // Calculate number of capture registers (number_of_captures + 1) * 2.
2172 __ leal(rdx, Operand(rax, rax, times_1, 2));
2173
2174 // rdx: Number of capture registers
2175 // Load last_match_info which is still known to be a fast case JSArray.
2176 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2177 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
2178
2179 // rbx: last_match_info backing store (FixedArray)
2180 // rdx: number of capture registers
2181 // Store the capture count.
2182 __ Integer32ToSmi(kScratchRegister, rdx);
2183 __ movq(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
2184 kScratchRegister);
2185 // Store last subject and last input.
2186 __ movq(rax, Operand(rsp, kSubjectOffset));
2187 __ movq(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
2188 __ movq(rcx, rbx);
2189 __ RecordWrite(rcx, RegExpImpl::kLastSubjectOffset, rax, rdi);
2190 __ movq(rax, Operand(rsp, kSubjectOffset));
2191 __ movq(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
2192 __ movq(rcx, rbx);
2193 __ RecordWrite(rcx, RegExpImpl::kLastInputOffset, rax, rdi);
2194
2195 // Get the static offsets vector filled by the native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002196 __ LoadAddress(rcx,
2197 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002198
2199 // rbx: last_match_info backing store (FixedArray)
2200 // rcx: offsets vector
2201 // rdx: number of capture registers
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002202 NearLabel next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002203 // Capture register counter starts from number of capture registers and
2204 // counts down until wraping after zero.
2205 __ bind(&next_capture);
2206 __ subq(rdx, Immediate(1));
2207 __ j(negative, &done);
2208 // Read the value from the static offsets vector buffer and make it a smi.
2209 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002210 __ Integer32ToSmi(rdi, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002211 // Store the smi value in the last match info.
2212 __ movq(FieldOperand(rbx,
2213 rdx,
2214 times_pointer_size,
2215 RegExpImpl::kFirstCaptureOffset),
2216 rdi);
2217 __ jmp(&next_capture);
2218 __ bind(&done);
2219
2220 // Return last match info.
2221 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2222 __ ret(4 * kPointerSize);
2223
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002224 __ bind(&exception);
2225 // Result must now be exception. If there is no pending exception already a
2226 // stack overflow (on the backtrack stack) was detected in RegExp code but
2227 // haven't created the exception yet. Handle that in the runtime system.
2228 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01002229 ExternalReference pending_exception_address(
2230 Isolate::k_pending_exception_address, isolate);
2231 Operand pending_exception_operand =
2232 masm->ExternalOperand(pending_exception_address, rbx);
2233 __ movq(rax, pending_exception_operand);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002234 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
2235 __ cmpq(rax, rdx);
2236 __ j(equal, &runtime);
Steve Block44f0eee2011-05-26 01:26:41 +01002237 __ movq(pending_exception_operand, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002238
2239 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
2240 NearLabel termination_exception;
2241 __ j(equal, &termination_exception);
2242 __ Throw(rax);
2243
2244 __ bind(&termination_exception);
2245 __ ThrowUncatchable(TERMINATION, rax);
2246
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002247 // Do the runtime call to execute the regexp.
2248 __ bind(&runtime);
2249 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2250#endif // V8_INTERPRETED_REGEXP
2251}
2252
2253
Ben Murdochb0fe1622011-05-05 13:52:32 +01002254void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
2255 const int kMaxInlineLength = 100;
2256 Label slowcase;
2257 Label done;
2258 __ movq(r8, Operand(rsp, kPointerSize * 3));
2259 __ JumpIfNotSmi(r8, &slowcase);
2260 __ SmiToInteger32(rbx, r8);
2261 __ cmpl(rbx, Immediate(kMaxInlineLength));
2262 __ j(above, &slowcase);
2263 // Smi-tagging is equivalent to multiplying by 2.
2264 STATIC_ASSERT(kSmiTag == 0);
2265 STATIC_ASSERT(kSmiTagSize == 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002266 // Allocate RegExpResult followed by FixedArray with size in rbx.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002267 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
2268 // Elements: [Map][Length][..elements..]
2269 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
2270 times_pointer_size,
2271 rbx, // In: Number of elements.
2272 rax, // Out: Start of allocation (tagged).
2273 rcx, // Out: End of allocation.
2274 rdx, // Scratch register
2275 &slowcase,
2276 TAG_OBJECT);
2277 // rax: Start of allocated area, object-tagged.
2278 // rbx: Number of array elements as int32.
2279 // r8: Number of array elements as smi.
2280
2281 // Set JSArray map to global.regexp_result_map().
2282 __ movq(rdx, ContextOperand(rsi, Context::GLOBAL_INDEX));
2283 __ movq(rdx, FieldOperand(rdx, GlobalObject::kGlobalContextOffset));
2284 __ movq(rdx, ContextOperand(rdx, Context::REGEXP_RESULT_MAP_INDEX));
2285 __ movq(FieldOperand(rax, HeapObject::kMapOffset), rdx);
2286
2287 // Set empty properties FixedArray.
Steve Block44f0eee2011-05-26 01:26:41 +01002288 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
2289 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002290
2291 // Set elements to point to FixedArray allocated right after the JSArray.
2292 __ lea(rcx, Operand(rax, JSRegExpResult::kSize));
2293 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rcx);
2294
2295 // Set input, index and length fields from arguments.
2296 __ movq(r8, Operand(rsp, kPointerSize * 1));
2297 __ movq(FieldOperand(rax, JSRegExpResult::kInputOffset), r8);
2298 __ movq(r8, Operand(rsp, kPointerSize * 2));
2299 __ movq(FieldOperand(rax, JSRegExpResult::kIndexOffset), r8);
2300 __ movq(r8, Operand(rsp, kPointerSize * 3));
2301 __ movq(FieldOperand(rax, JSArray::kLengthOffset), r8);
2302
2303 // Fill out the elements FixedArray.
2304 // rax: JSArray.
2305 // rcx: FixedArray.
2306 // rbx: Number of elements in array as int32.
2307
2308 // Set map.
Steve Block44f0eee2011-05-26 01:26:41 +01002309 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
2310 __ movq(FieldOperand(rcx, HeapObject::kMapOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002311 // Set length.
2312 __ Integer32ToSmi(rdx, rbx);
2313 __ movq(FieldOperand(rcx, FixedArray::kLengthOffset), rdx);
2314 // Fill contents of fixed-array with the-hole.
Steve Block44f0eee2011-05-26 01:26:41 +01002315 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002316 __ lea(rcx, FieldOperand(rcx, FixedArray::kHeaderSize));
2317 // Fill fixed array elements with hole.
2318 // rax: JSArray.
2319 // rbx: Number of elements in array that remains to be filled, as int32.
2320 // rcx: Start of elements in FixedArray.
2321 // rdx: the hole.
2322 Label loop;
2323 __ testl(rbx, rbx);
2324 __ bind(&loop);
Steve Block1e0659c2011-05-24 12:43:12 +01002325 __ j(less_equal, &done); // Jump if rcx is negative or zero.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002326 __ subl(rbx, Immediate(1));
2327 __ movq(Operand(rcx, rbx, times_pointer_size, 0), rdx);
2328 __ jmp(&loop);
2329
2330 __ bind(&done);
2331 __ ret(3 * kPointerSize);
2332
2333 __ bind(&slowcase);
2334 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
2335}
2336
2337
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002338void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
2339 Register object,
2340 Register result,
2341 Register scratch1,
2342 Register scratch2,
2343 bool object_is_smi,
2344 Label* not_found) {
2345 // Use of registers. Register result is used as a temporary.
2346 Register number_string_cache = result;
2347 Register mask = scratch1;
2348 Register scratch = scratch2;
2349
2350 // Load the number string cache.
2351 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2352
2353 // Make the hash mask from the length of the number string cache. It
2354 // contains two elements (number and string) for each cache entry.
2355 __ SmiToInteger32(
2356 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2357 __ shrl(mask, Immediate(1));
2358 __ subq(mask, Immediate(1)); // Make mask.
2359
2360 // Calculate the entry in the number string cache. The hash value in the
2361 // number string cache for smis is just the smi value, and the hash for
2362 // doubles is the xor of the upper and lower words. See
2363 // Heap::GetNumberStringCache.
2364 Label is_smi;
2365 Label load_result_from_cache;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002366 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002367 if (!object_is_smi) {
2368 __ JumpIfSmi(object, &is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002369 __ CheckMap(object, factory->heap_number_map(), not_found, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002370
2371 STATIC_ASSERT(8 == kDoubleSize);
2372 __ movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2373 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2374 GenerateConvertHashCodeToIndex(masm, scratch, mask);
2375
2376 Register index = scratch;
2377 Register probe = mask;
2378 __ movq(probe,
2379 FieldOperand(number_string_cache,
2380 index,
2381 times_1,
2382 FixedArray::kHeaderSize));
2383 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002384 __ movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2385 __ movsd(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
2386 __ ucomisd(xmm0, xmm1);
2387 __ j(parity_even, not_found); // Bail out if NaN is involved.
2388 __ j(not_equal, not_found); // The cache did not contain this value.
2389 __ jmp(&load_result_from_cache);
2390 }
2391
2392 __ bind(&is_smi);
2393 __ SmiToInteger32(scratch, object);
2394 GenerateConvertHashCodeToIndex(masm, scratch, mask);
2395
2396 Register index = scratch;
2397 // Check if the entry is the smi we are looking for.
2398 __ cmpq(object,
2399 FieldOperand(number_string_cache,
2400 index,
2401 times_1,
2402 FixedArray::kHeaderSize));
2403 __ j(not_equal, not_found);
2404
2405 // Get the result from the cache.
2406 __ bind(&load_result_from_cache);
2407 __ movq(result,
2408 FieldOperand(number_string_cache,
2409 index,
2410 times_1,
2411 FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002412 Counters* counters = masm->isolate()->counters();
2413 __ IncrementCounter(counters->number_to_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002414}
2415
2416
2417void NumberToStringStub::GenerateConvertHashCodeToIndex(MacroAssembler* masm,
2418 Register hash,
2419 Register mask) {
2420 __ and_(hash, mask);
2421 // Each entry in string cache consists of two pointer sized fields,
2422 // but times_twice_pointer_size (multiplication by 16) scale factor
2423 // is not supported by addrmode on x64 platform.
2424 // So we have to premultiply entry index before lookup.
2425 __ shl(hash, Immediate(kPointerSizeLog2 + 1));
2426}
2427
2428
2429void NumberToStringStub::Generate(MacroAssembler* masm) {
2430 Label runtime;
2431
2432 __ movq(rbx, Operand(rsp, kPointerSize));
2433
2434 // Generate code to lookup number in the number string cache.
2435 GenerateLookupNumberStringCache(masm, rbx, rax, r8, r9, false, &runtime);
2436 __ ret(1 * kPointerSize);
2437
2438 __ bind(&runtime);
2439 // Handle number to string in the runtime system if not found in the cache.
2440 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
2441}
2442
2443
2444static int NegativeComparisonResult(Condition cc) {
2445 ASSERT(cc != equal);
2446 ASSERT((cc == less) || (cc == less_equal)
2447 || (cc == greater) || (cc == greater_equal));
2448 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
2449}
2450
2451
2452void CompareStub::Generate(MacroAssembler* masm) {
2453 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
2454
2455 Label check_unequal_objects, done;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002456 Factory* factory = masm->isolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002457
2458 // Compare two smis if required.
2459 if (include_smi_compare_) {
2460 Label non_smi, smi_done;
2461 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
2462 __ subq(rdx, rax);
2463 __ j(no_overflow, &smi_done);
Ben Murdochf87a2032010-10-22 12:50:53 +01002464 __ not_(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002465 __ bind(&smi_done);
2466 __ movq(rax, rdx);
2467 __ ret(0);
2468 __ bind(&non_smi);
2469 } else if (FLAG_debug_code) {
2470 Label ok;
2471 __ JumpIfNotSmi(rdx, &ok);
2472 __ JumpIfNotSmi(rax, &ok);
2473 __ Abort("CompareStub: smi operands");
2474 __ bind(&ok);
2475 }
2476
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002477 // The compare stub returns a positive, negative, or zero 64-bit integer
2478 // value in rax, corresponding to result of comparing the two inputs.
2479 // NOTICE! This code is only reached after a smi-fast-case check, so
2480 // it is certain that at least one operand isn't a smi.
2481
2482 // Two identical objects are equal unless they are both NaN or undefined.
2483 {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002484 NearLabel not_identical;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002485 __ cmpq(rax, rdx);
2486 __ j(not_equal, &not_identical);
2487
2488 if (cc_ != equal) {
2489 // Check for undefined. undefined OP undefined is false even though
2490 // undefined == undefined.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002491 NearLabel check_for_nan;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002492 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
2493 __ j(not_equal, &check_for_nan);
2494 __ Set(rax, NegativeComparisonResult(cc_));
2495 __ ret(0);
2496 __ bind(&check_for_nan);
2497 }
2498
Steve Block44f0eee2011-05-26 01:26:41 +01002499 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002500 // so we do the second best thing - test it ourselves.
2501 // Note: if cc_ != equal, never_nan_nan_ is not used.
2502 // We cannot set rax to EQUAL until just before return because
2503 // rax must be unchanged on jump to not_identical.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002504 if (never_nan_nan_ && (cc_ == equal)) {
2505 __ Set(rax, EQUAL);
2506 __ ret(0);
2507 } else {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002508 NearLabel heap_number;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002509 // If it's not a heap number, then return equal for (in)equality operator.
2510 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
Ben Murdoch8b112d22011-06-08 16:22:53 +01002511 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002512 __ j(equal, &heap_number);
2513 if (cc_ != equal) {
2514 // Call runtime on identical JSObjects. Otherwise return equal.
2515 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
2516 __ j(above_equal, &not_identical);
2517 }
2518 __ Set(rax, EQUAL);
2519 __ ret(0);
2520
2521 __ bind(&heap_number);
2522 // It is a heap number, so return equal if it's not NaN.
2523 // For NaN, return 1 for every condition except greater and
2524 // greater-equal. Return -1 for them, so the comparison yields
2525 // false for all conditions except not-equal.
2526 __ Set(rax, EQUAL);
2527 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
2528 __ ucomisd(xmm0, xmm0);
2529 __ setcc(parity_even, rax);
2530 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
2531 if (cc_ == greater_equal || cc_ == greater) {
2532 __ neg(rax);
2533 }
2534 __ ret(0);
2535 }
2536
2537 __ bind(&not_identical);
2538 }
2539
2540 if (cc_ == equal) { // Both strict and non-strict.
2541 Label slow; // Fallthrough label.
2542
2543 // If we're doing a strict equality comparison, we don't have to do
2544 // type conversion, so we generate code to do fast comparison for objects
2545 // and oddballs. Non-smi numbers and strings still go through the usual
2546 // slow-case code.
2547 if (strict_) {
2548 // If either is a Smi (we know that not both are), then they can only
2549 // be equal if the other is a HeapNumber. If so, use the slow case.
2550 {
2551 Label not_smis;
2552 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
2553
2554 // Check if the non-smi operand is a heap number.
2555 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
Ben Murdoch8b112d22011-06-08 16:22:53 +01002556 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002557 // If heap number, handle it in the slow case.
2558 __ j(equal, &slow);
2559 // Return non-equal. ebx (the lower half of rbx) is not zero.
2560 __ movq(rax, rbx);
2561 __ ret(0);
2562
2563 __ bind(&not_smis);
2564 }
2565
2566 // If either operand is a JSObject or an oddball value, then they are not
2567 // equal since their pointers are different
2568 // There is no test for undetectability in strict equality.
2569
2570 // If the first object is a JS object, we have done pointer comparison.
2571 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002572 NearLabel first_non_object;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002573 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
2574 __ j(below, &first_non_object);
2575 // Return non-zero (eax (not rax) is not zero)
2576 Label return_not_equal;
2577 STATIC_ASSERT(kHeapObjectTag != 0);
2578 __ bind(&return_not_equal);
2579 __ ret(0);
2580
2581 __ bind(&first_non_object);
2582 // Check for oddballs: true, false, null, undefined.
2583 __ CmpInstanceType(rcx, ODDBALL_TYPE);
2584 __ j(equal, &return_not_equal);
2585
2586 __ CmpObjectType(rdx, FIRST_JS_OBJECT_TYPE, rcx);
2587 __ j(above_equal, &return_not_equal);
2588
2589 // Check for oddballs: true, false, null, undefined.
2590 __ CmpInstanceType(rcx, ODDBALL_TYPE);
2591 __ j(equal, &return_not_equal);
2592
2593 // Fall through to the general case.
2594 }
2595 __ bind(&slow);
2596 }
2597
2598 // Generate the number comparison code.
2599 if (include_number_compare_) {
2600 Label non_number_comparison;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002601 NearLabel unordered;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002602 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
2603 __ xorl(rax, rax);
2604 __ xorl(rcx, rcx);
2605 __ ucomisd(xmm0, xmm1);
2606
2607 // Don't base result on EFLAGS when a NaN is involved.
2608 __ j(parity_even, &unordered);
2609 // Return a result of -1, 0, or 1, based on EFLAGS.
2610 __ setcc(above, rax);
2611 __ setcc(below, rcx);
2612 __ subq(rax, rcx);
2613 __ ret(0);
2614
2615 // If one of the numbers was NaN, then the result is always false.
2616 // The cc is never not-equal.
2617 __ bind(&unordered);
2618 ASSERT(cc_ != not_equal);
2619 if (cc_ == less || cc_ == less_equal) {
2620 __ Set(rax, 1);
2621 } else {
2622 __ Set(rax, -1);
2623 }
2624 __ ret(0);
2625
2626 // The number comparison code did not provide a valid result.
2627 __ bind(&non_number_comparison);
2628 }
2629
2630 // Fast negative check for symbol-to-symbol equality.
2631 Label check_for_strings;
2632 if (cc_ == equal) {
2633 BranchIfNonSymbol(masm, &check_for_strings, rax, kScratchRegister);
2634 BranchIfNonSymbol(masm, &check_for_strings, rdx, kScratchRegister);
2635
2636 // We've already checked for object identity, so if both operands
2637 // are symbols they aren't equal. Register eax (not rax) already holds a
2638 // non-zero value, which indicates not equal, so just return.
2639 __ ret(0);
2640 }
2641
2642 __ bind(&check_for_strings);
2643
2644 __ JumpIfNotBothSequentialAsciiStrings(
2645 rdx, rax, rcx, rbx, &check_unequal_objects);
2646
2647 // Inline comparison of ascii strings.
2648 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
2649 rdx,
2650 rax,
2651 rcx,
2652 rbx,
2653 rdi,
2654 r8);
2655
2656#ifdef DEBUG
2657 __ Abort("Unexpected fall-through from string comparison");
2658#endif
2659
2660 __ bind(&check_unequal_objects);
2661 if (cc_ == equal && !strict_) {
2662 // Not strict equality. Objects are unequal if
2663 // they are both JSObjects and not undetectable,
2664 // and their pointers are different.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002665 NearLabel not_both_objects, return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002666 // At most one is a smi, so we can test for smi by adding the two.
2667 // A smi plus a heap object has the low bit set, a heap object plus
2668 // a heap object has the low bit clear.
2669 STATIC_ASSERT(kSmiTag == 0);
2670 STATIC_ASSERT(kSmiTagMask == 1);
2671 __ lea(rcx, Operand(rax, rdx, times_1, 0));
2672 __ testb(rcx, Immediate(kSmiTagMask));
2673 __ j(not_zero, &not_both_objects);
2674 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rbx);
2675 __ j(below, &not_both_objects);
2676 __ CmpObjectType(rdx, FIRST_JS_OBJECT_TYPE, rcx);
2677 __ j(below, &not_both_objects);
2678 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
2679 Immediate(1 << Map::kIsUndetectable));
2680 __ j(zero, &return_unequal);
2681 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
2682 Immediate(1 << Map::kIsUndetectable));
2683 __ j(zero, &return_unequal);
2684 // The objects are both undetectable, so they both compare as the value
2685 // undefined, and are equal.
2686 __ Set(rax, EQUAL);
2687 __ bind(&return_unequal);
Steve Block1e0659c2011-05-24 12:43:12 +01002688 // Return non-equal by returning the non-zero object pointer in rax,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002689 // or return equal if we fell through to here.
2690 __ ret(0);
2691 __ bind(&not_both_objects);
2692 }
2693
2694 // Push arguments below the return address to prepare jump to builtin.
2695 __ pop(rcx);
2696 __ push(rdx);
2697 __ push(rax);
2698
2699 // Figure out which native to call and setup the arguments.
2700 Builtins::JavaScript builtin;
2701 if (cc_ == equal) {
2702 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
2703 } else {
2704 builtin = Builtins::COMPARE;
2705 __ Push(Smi::FromInt(NegativeComparisonResult(cc_)));
2706 }
2707
2708 // Restore return address on the stack.
2709 __ push(rcx);
2710
2711 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2712 // tagged as a small integer.
2713 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
2714}
2715
2716
2717void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
2718 Label* label,
2719 Register object,
2720 Register scratch) {
2721 __ JumpIfSmi(object, label);
2722 __ movq(scratch, FieldOperand(object, HeapObject::kMapOffset));
2723 __ movzxbq(scratch,
2724 FieldOperand(scratch, Map::kInstanceTypeOffset));
2725 // Ensure that no non-strings have the symbol bit set.
2726 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
2727 STATIC_ASSERT(kSymbolTag != 0);
2728 __ testb(scratch, Immediate(kIsSymbolMask));
2729 __ j(zero, label);
2730}
2731
2732
2733void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002734 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002735}
2736
2737
2738void CallFunctionStub::Generate(MacroAssembler* masm) {
2739 Label slow;
2740
2741 // If the receiver might be a value (string, number or boolean) check for this
2742 // and box it if it is.
2743 if (ReceiverMightBeValue()) {
2744 // Get the receiver from the stack.
2745 // +1 ~ return address
2746 Label receiver_is_value, receiver_is_js_object;
2747 __ movq(rax, Operand(rsp, (argc_ + 1) * kPointerSize));
2748
2749 // Check if receiver is a smi (which is a number value).
2750 __ JumpIfSmi(rax, &receiver_is_value);
2751
2752 // Check if the receiver is a valid JS object.
2753 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rdi);
2754 __ j(above_equal, &receiver_is_js_object);
2755
2756 // Call the runtime to box the value.
2757 __ bind(&receiver_is_value);
2758 __ EnterInternalFrame();
2759 __ push(rax);
2760 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2761 __ LeaveInternalFrame();
2762 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rax);
2763
2764 __ bind(&receiver_is_js_object);
2765 }
2766
2767 // Get the function to call from the stack.
2768 // +2 ~ receiver, return address
2769 __ movq(rdi, Operand(rsp, (argc_ + 2) * kPointerSize));
2770
2771 // Check that the function really is a JavaScript function.
2772 __ JumpIfSmi(rdi, &slow);
2773 // Goto slow case if we do not have a function.
2774 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
2775 __ j(not_equal, &slow);
2776
2777 // Fast-case: Just invoke the function.
2778 ParameterCount actual(argc_);
2779 __ InvokeFunction(rdi, actual, JUMP_FUNCTION);
2780
2781 // Slow-case: Non-function called.
2782 __ bind(&slow);
2783 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2784 // of the original receiver from the call site).
2785 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rdi);
2786 __ Set(rax, argc_);
2787 __ Set(rbx, 0);
2788 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01002789 Handle<Code> adaptor =
2790 Isolate::Current()->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002791 __ Jump(adaptor, RelocInfo::CODE_TARGET);
2792}
2793
2794
Steve Block44f0eee2011-05-26 01:26:41 +01002795bool CEntryStub::NeedsImmovableCode() {
2796 return false;
2797}
2798
2799
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002800void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002801 // Throw exception in eax.
2802 __ Throw(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002803}
2804
2805
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002806void CEntryStub::GenerateCore(MacroAssembler* masm,
2807 Label* throw_normal_exception,
2808 Label* throw_termination_exception,
2809 Label* throw_out_of_memory_exception,
2810 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01002811 bool always_allocate_scope) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002812 // rax: result parameter for PerformGC, if any.
2813 // rbx: pointer to C function (C callee-saved).
2814 // rbp: frame pointer (restored after C call).
2815 // rsp: stack pointer (restored after C call).
2816 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01002817 // r15: pointer to the first argument (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002818 // This pointer is reused in LeaveExitFrame(), so it is stored in a
2819 // callee-saved register.
2820
2821 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
2822 // Complex results must be written to address passed as first argument.
2823 // AMD64 calling convention: a struct of two pointers in rax+rdx
2824
2825 // Check stack alignment.
2826 if (FLAG_debug_code) {
2827 __ CheckStackAlignment();
2828 }
2829
2830 if (do_gc) {
2831 // Pass failure code returned from last attempt as first argument to
2832 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
2833 // stack is known to be aligned. This function takes one argument which is
2834 // passed in register.
2835#ifdef _WIN64
2836 __ movq(rcx, rax);
2837#else // _WIN64
2838 __ movq(rdi, rax);
2839#endif
2840 __ movq(kScratchRegister,
2841 FUNCTION_ADDR(Runtime::PerformGC),
2842 RelocInfo::RUNTIME_ENTRY);
2843 __ call(kScratchRegister);
2844 }
2845
2846 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01002847 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002848 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01002849 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
2850 __ incl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002851 }
2852
2853 // Call C function.
2854#ifdef _WIN64
2855 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9
2856 // Store Arguments object on stack, below the 4 WIN64 ABI parameter slots.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002857 __ movq(StackSpaceOperand(0), r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01002858 __ movq(StackSpaceOperand(1), r15); // argv.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002859 if (result_size_ < 2) {
2860 // Pass a pointer to the Arguments object as the first argument.
2861 // Return result in single register (rax).
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002862 __ lea(rcx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01002863 __ LoadAddress(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002864 } else {
2865 ASSERT_EQ(2, result_size_);
2866 // Pass a pointer to the result location as the first argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002867 __ lea(rcx, StackSpaceOperand(2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002868 // Pass a pointer to the Arguments object as the second argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002869 __ lea(rdx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01002870 __ LoadAddress(r8, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002871 }
2872
2873#else // _WIN64
2874 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
2875 __ movq(rdi, r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01002876 __ movq(rsi, r15); // argv.
2877 __ movq(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002878#endif
2879 __ call(rbx);
2880 // Result is in rax - do not destroy this register!
2881
2882 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01002883 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
2884 __ decl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002885 }
2886
2887 // Check for failure result.
2888 Label failure_returned;
2889 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
2890#ifdef _WIN64
2891 // If return value is on the stack, pop it to registers.
2892 if (result_size_ > 1) {
2893 ASSERT_EQ(2, result_size_);
2894 // Read result values stored on stack. Result is stored
2895 // above the four argument mirror slots and the two
2896 // Arguments object slots.
2897 __ movq(rax, Operand(rsp, 6 * kPointerSize));
2898 __ movq(rdx, Operand(rsp, 7 * kPointerSize));
2899 }
2900#endif
2901 __ lea(rcx, Operand(rax, 1));
2902 // Lower 2 bits of rcx are 0 iff rax has failure tag.
2903 __ testl(rcx, Immediate(kFailureTagMask));
2904 __ j(zero, &failure_returned);
2905
2906 // Exit the JavaScript to C++ exit frame.
Steve Block1e0659c2011-05-24 12:43:12 +01002907 __ LeaveExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002908 __ ret(0);
2909
2910 // Handling of failure.
2911 __ bind(&failure_returned);
2912
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002913 NearLabel retry;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002914 // If the returned exception is RETRY_AFTER_GC continue at retry label
2915 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
2916 __ testl(rax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
2917 __ j(zero, &retry);
2918
2919 // Special handling of out of memory exceptions.
2920 __ movq(kScratchRegister, Failure::OutOfMemoryException(), RelocInfo::NONE);
2921 __ cmpq(rax, kScratchRegister);
2922 __ j(equal, throw_out_of_memory_exception);
2923
2924 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01002925 ExternalReference pending_exception_address(
2926 Isolate::k_pending_exception_address, masm->isolate());
2927 Operand pending_exception_operand =
2928 masm->ExternalOperand(pending_exception_address);
2929 __ movq(rax, pending_exception_operand);
2930 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
2931 __ movq(pending_exception_operand, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002932
2933 // Special handling of termination exceptions which are uncatchable
2934 // by javascript code.
2935 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
2936 __ j(equal, throw_termination_exception);
2937
2938 // Handle normal exception.
2939 __ jmp(throw_normal_exception);
2940
2941 // Retry.
2942 __ bind(&retry);
2943}
2944
2945
2946void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
2947 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002948 __ ThrowUncatchable(type, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002949}
2950
2951
2952void CEntryStub::Generate(MacroAssembler* masm) {
2953 // rax: number of arguments including receiver
2954 // rbx: pointer to C function (C callee-saved)
2955 // rbp: frame pointer of calling JS frame (restored after C call)
2956 // rsp: stack pointer (restored after C call)
2957 // rsi: current context (restored)
2958
2959 // NOTE: Invocations of builtins may return failure objects
2960 // instead of a proper result. The builtin entry handles
2961 // this by performing a garbage collection and retrying the
2962 // builtin once.
2963
2964 // Enter the exit frame that transitions from JavaScript to C++.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002965#ifdef _WIN64
2966 int arg_stack_space = (result_size_ < 2 ? 2 : 4);
2967#else
2968 int arg_stack_space = 0;
2969#endif
Steve Block1e0659c2011-05-24 12:43:12 +01002970 __ EnterExitFrame(arg_stack_space, save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002971
2972 // rax: Holds the context at this point, but should not be used.
2973 // On entry to code generated by GenerateCore, it must hold
2974 // a failure result if the collect_garbage argument to GenerateCore
2975 // is true. This failure result can be the result of code
2976 // generated by a previous call to GenerateCore. The value
2977 // of rax is then passed to Runtime::PerformGC.
2978 // rbx: pointer to builtin function (C callee-saved).
2979 // rbp: frame pointer of exit frame (restored after C call).
2980 // rsp: stack pointer (restored after C call).
2981 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01002982 // r15: argv pointer (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002983
2984 Label throw_normal_exception;
2985 Label throw_termination_exception;
2986 Label throw_out_of_memory_exception;
2987
2988 // Call into the runtime system.
2989 GenerateCore(masm,
2990 &throw_normal_exception,
2991 &throw_termination_exception,
2992 &throw_out_of_memory_exception,
2993 false,
2994 false);
2995
2996 // Do space-specific GC and retry runtime call.
2997 GenerateCore(masm,
2998 &throw_normal_exception,
2999 &throw_termination_exception,
3000 &throw_out_of_memory_exception,
3001 true,
3002 false);
3003
3004 // Do full GC and retry runtime call one final time.
3005 Failure* failure = Failure::InternalError();
3006 __ movq(rax, failure, RelocInfo::NONE);
3007 GenerateCore(masm,
3008 &throw_normal_exception,
3009 &throw_termination_exception,
3010 &throw_out_of_memory_exception,
3011 true,
3012 true);
3013
3014 __ bind(&throw_out_of_memory_exception);
3015 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3016
3017 __ bind(&throw_termination_exception);
3018 GenerateThrowUncatchable(masm, TERMINATION);
3019
3020 __ bind(&throw_normal_exception);
3021 GenerateThrowTOS(masm);
3022}
3023
3024
3025void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3026 Label invoke, exit;
3027#ifdef ENABLE_LOGGING_AND_PROFILING
3028 Label not_outermost_js, not_outermost_js_2;
3029#endif
Steve Block44f0eee2011-05-26 01:26:41 +01003030 { // NOLINT. Scope block confuses linter.
3031 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
3032 // Setup frame.
3033 __ push(rbp);
3034 __ movq(rbp, rsp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003035
Steve Block44f0eee2011-05-26 01:26:41 +01003036 // Push the stack frame type marker twice.
3037 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3038 // Scratch register is neither callee-save, nor an argument register on any
3039 // platform. It's free to use at this point.
3040 // Cannot use smi-register for loading yet.
3041 __ movq(kScratchRegister,
3042 reinterpret_cast<uint64_t>(Smi::FromInt(marker)),
3043 RelocInfo::NONE);
3044 __ push(kScratchRegister); // context slot
3045 __ push(kScratchRegister); // function slot
3046 // Save callee-saved registers (X64/Win64 calling conventions).
3047 __ push(r12);
3048 __ push(r13);
3049 __ push(r14);
3050 __ push(r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003051#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01003052 __ push(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
3053 __ push(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003054#endif
Steve Block44f0eee2011-05-26 01:26:41 +01003055 __ push(rbx);
3056 // TODO(X64): On Win64, if we ever use XMM6-XMM15, the low low 64 bits are
3057 // callee save as well.
3058
3059 // Set up the roots and smi constant registers.
3060 // Needs to be done before any further smi loads.
3061 __ InitializeSmiConstantRegister();
3062 __ InitializeRootRegister();
3063 }
3064
3065 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003066
3067 // Save copies of the top frame descriptor on the stack.
Steve Block44f0eee2011-05-26 01:26:41 +01003068 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, isolate);
3069 {
3070 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
3071 __ push(c_entry_fp_operand);
3072 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003073
3074#ifdef ENABLE_LOGGING_AND_PROFILING
3075 // If this is the outermost JS call, set js_entry_sp value.
Steve Block44f0eee2011-05-26 01:26:41 +01003076 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address, isolate);
3077 __ Load(rax, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003078 __ testq(rax, rax);
3079 __ j(not_zero, &not_outermost_js);
3080 __ movq(rax, rbp);
Steve Block44f0eee2011-05-26 01:26:41 +01003081 __ Store(js_entry_sp, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003082 __ bind(&not_outermost_js);
3083#endif
3084
3085 // Call a faked try-block that does the invoke.
3086 __ call(&invoke);
3087
3088 // Caught exception: Store result (exception) in the pending
3089 // exception field in the JSEnv and return a failure sentinel.
Steve Block44f0eee2011-05-26 01:26:41 +01003090 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3091 isolate);
3092 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003093 __ movq(rax, Failure::Exception(), RelocInfo::NONE);
3094 __ jmp(&exit);
3095
3096 // Invoke: Link this frame into the handler chain.
3097 __ bind(&invoke);
3098 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3099
3100 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003101 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
3102 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003103
3104 // Fake a receiver (NULL).
3105 __ push(Immediate(0)); // receiver
3106
3107 // Invoke the function by calling through JS entry trampoline
3108 // builtin and pop the faked function when we return. We load the address
3109 // from an external reference instead of inlining the call target address
3110 // directly in the code, because the builtin stubs may not have been
3111 // generated yet at the time this code is generated.
3112 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003113 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3114 isolate);
3115 __ Load(rax, construct_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003116 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003117 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
3118 __ Load(rax, entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003119 }
3120 __ lea(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
3121 __ call(kScratchRegister);
3122
3123 // Unlink this frame from the handler chain.
Steve Block44f0eee2011-05-26 01:26:41 +01003124 Operand handler_operand =
3125 masm->ExternalOperand(ExternalReference(Isolate::k_handler_address,
3126 isolate));
3127 __ pop(handler_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003128 // Pop next_sp.
3129 __ addq(rsp, Immediate(StackHandlerConstants::kSize - kPointerSize));
3130
3131#ifdef ENABLE_LOGGING_AND_PROFILING
Steve Block1e0659c2011-05-24 12:43:12 +01003132 // If current RBP value is the same as js_entry_sp value, it means that
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003133 // the current function is the outermost.
3134 __ movq(kScratchRegister, js_entry_sp);
3135 __ cmpq(rbp, Operand(kScratchRegister, 0));
3136 __ j(not_equal, &not_outermost_js_2);
3137 __ movq(Operand(kScratchRegister, 0), Immediate(0));
3138 __ bind(&not_outermost_js_2);
3139#endif
3140
3141 // Restore the top frame descriptor from the stack.
3142 __ bind(&exit);
Steve Block44f0eee2011-05-26 01:26:41 +01003143 {
3144 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
3145 __ pop(c_entry_fp_operand);
3146 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003147
3148 // Restore callee-saved registers (X64 conventions).
3149 __ pop(rbx);
3150#ifdef _WIN64
3151 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
3152 __ pop(rsi);
3153 __ pop(rdi);
3154#endif
3155 __ pop(r15);
3156 __ pop(r14);
3157 __ pop(r13);
3158 __ pop(r12);
3159 __ addq(rsp, Immediate(2 * kPointerSize)); // remove markers
3160
3161 // Restore frame pointer and return.
3162 __ pop(rbp);
3163 __ ret(0);
3164}
3165
3166
3167void InstanceofStub::Generate(MacroAssembler* masm) {
3168 // Implements "value instanceof function" operator.
Steve Block44f0eee2011-05-26 01:26:41 +01003169 // Expected input state with no inline cache:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003170 // rsp[0] : return address
3171 // rsp[1] : function pointer
3172 // rsp[2] : value
Steve Block44f0eee2011-05-26 01:26:41 +01003173 // Expected input state with an inline one-element cache:
3174 // rsp[0] : return address
3175 // rsp[1] : offset from return address to location of inline cache
3176 // rsp[2] : function pointer
3177 // rsp[3] : value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003178 // Returns a bitwise zero to indicate that the value
3179 // is and instance of the function and anything else to
3180 // indicate that the value is not an instance.
3181
Ben Murdoch8b112d22011-06-08 16:22:53 +01003182 static const int kOffsetToMapCheckValue = 2;
3183 static const int kOffsetToResultValue = 18;
Steve Block44f0eee2011-05-26 01:26:41 +01003184 // The last 4 bytes of the instruction sequence
Ben Murdoch8b112d22011-06-08 16:22:53 +01003185 // movq(rdi, FieldOperand(rax, HeapObject::kMapOffset))
Steve Block44f0eee2011-05-26 01:26:41 +01003186 // Move(kScratchRegister, FACTORY->the_hole_value())
3187 // in front of the hole value address.
3188 static const unsigned int kWordBeforeMapCheckValue = 0xBA49FF78;
3189 // The last 4 bytes of the instruction sequence
3190 // __ j(not_equal, &cache_miss);
3191 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
3192 // before the offset of the hole value in the root array.
3193 static const unsigned int kWordBeforeResultValue = 0x458B4909;
3194 // Only the inline check flag is supported on X64.
3195 ASSERT(flags_ == kNoFlags || HasCallSiteInlineCheck());
3196 int extra_stack_space = HasCallSiteInlineCheck() ? kPointerSize : 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003197
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003198 // Get the object - go slow case if it's a smi.
3199 Label slow;
Steve Block44f0eee2011-05-26 01:26:41 +01003200
3201 __ movq(rax, Operand(rsp, 2 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003202 __ JumpIfSmi(rax, &slow);
3203
3204 // Check that the left hand is a JS object. Leave its map in rax.
3205 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rax);
3206 __ j(below, &slow);
3207 __ CmpInstanceType(rax, LAST_JS_OBJECT_TYPE);
3208 __ j(above, &slow);
3209
3210 // Get the prototype of the function.
Steve Block44f0eee2011-05-26 01:26:41 +01003211 __ movq(rdx, Operand(rsp, 1 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003212 // rdx is function, rax is map.
3213
Steve Block44f0eee2011-05-26 01:26:41 +01003214 // If there is a call site cache don't look in the global cache, but do the
3215 // real lookup and update the call site cache.
3216 if (!HasCallSiteInlineCheck()) {
3217 // Look up the function and the map in the instanceof cache.
3218 NearLabel miss;
3219 __ CompareRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
3220 __ j(not_equal, &miss);
3221 __ CompareRoot(rax, Heap::kInstanceofCacheMapRootIndex);
3222 __ j(not_equal, &miss);
3223 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
3224 __ ret(2 * kPointerSize);
3225 __ bind(&miss);
3226 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003227
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003228 __ TryGetFunctionPrototype(rdx, rbx, &slow);
3229
3230 // Check that the function prototype is a JS object.
3231 __ JumpIfSmi(rbx, &slow);
3232 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, kScratchRegister);
3233 __ j(below, &slow);
3234 __ CmpInstanceType(kScratchRegister, LAST_JS_OBJECT_TYPE);
3235 __ j(above, &slow);
3236
3237 // Register mapping:
3238 // rax is object map.
3239 // rdx is function.
3240 // rbx is function prototype.
Steve Block44f0eee2011-05-26 01:26:41 +01003241 if (!HasCallSiteInlineCheck()) {
3242 __ StoreRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
3243 __ StoreRoot(rax, Heap::kInstanceofCacheMapRootIndex);
3244 } else {
3245 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3246 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3247 __ movq(Operand(kScratchRegister, kOffsetToMapCheckValue), rax);
3248 if (FLAG_debug_code) {
3249 __ movl(rdi, Immediate(kWordBeforeMapCheckValue));
3250 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), rdi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003251 __ Assert(equal, "InstanceofStub unexpected call site cache (check).");
Steve Block44f0eee2011-05-26 01:26:41 +01003252 }
3253 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003254
3255 __ movq(rcx, FieldOperand(rax, Map::kPrototypeOffset));
3256
3257 // Loop through the prototype chain looking for the function prototype.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003258 NearLabel loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003259 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
3260 __ bind(&loop);
3261 __ cmpq(rcx, rbx);
3262 __ j(equal, &is_instance);
3263 __ cmpq(rcx, kScratchRegister);
3264 // The code at is_not_instance assumes that kScratchRegister contains a
3265 // non-zero GCable value (the null object in this case).
3266 __ j(equal, &is_not_instance);
3267 __ movq(rcx, FieldOperand(rcx, HeapObject::kMapOffset));
3268 __ movq(rcx, FieldOperand(rcx, Map::kPrototypeOffset));
3269 __ jmp(&loop);
3270
3271 __ bind(&is_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01003272 if (!HasCallSiteInlineCheck()) {
3273 __ xorl(rax, rax);
3274 // Store bitwise zero in the cache. This is a Smi in GC terms.
3275 STATIC_ASSERT(kSmiTag == 0);
3276 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
3277 } else {
3278 // Store offset of true in the root array at the inline check site.
3279 ASSERT((Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias
3280 == 0xB0 - 0x100);
3281 __ movl(rax, Immediate(0xB0)); // TrueValue is at -10 * kPointerSize.
3282 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3283 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3284 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
3285 if (FLAG_debug_code) {
3286 __ movl(rax, Immediate(kWordBeforeResultValue));
3287 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003288 __ Assert(equal, "InstanceofStub unexpected call site cache (mov).");
Steve Block44f0eee2011-05-26 01:26:41 +01003289 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003290 __ Set(rax, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003291 }
3292 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003293
3294 __ bind(&is_not_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01003295 if (!HasCallSiteInlineCheck()) {
3296 // We have to store a non-zero value in the cache.
3297 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
3298 } else {
3299 // Store offset of false in the root array at the inline check site.
3300 ASSERT((Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias
3301 == 0xB8 - 0x100);
3302 __ movl(rax, Immediate(0xB8)); // FalseValue is at -9 * kPointerSize.
3303 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3304 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3305 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
3306 if (FLAG_debug_code) {
3307 __ movl(rax, Immediate(kWordBeforeResultValue));
3308 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
3309 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
3310 }
3311 }
3312 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003313
3314 // Slow-case: Go through the JavaScript implementation.
3315 __ bind(&slow);
Steve Block44f0eee2011-05-26 01:26:41 +01003316 if (HasCallSiteInlineCheck()) {
3317 // Remove extra value from the stack.
3318 __ pop(rcx);
3319 __ pop(rax);
3320 __ push(rcx);
3321 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003322 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
3323}
3324
3325
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003326// Passing arguments in registers is not supported.
3327Register InstanceofStub::left() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01003328
3329
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003330Register InstanceofStub::right() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01003331
3332
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003333int CompareStub::MinorKey() {
3334 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3335 // stubs the never NaN NaN condition is only taken into account if the
3336 // condition is equals.
3337 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
3338 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3339 return ConditionField::encode(static_cast<unsigned>(cc_))
3340 | RegisterField::encode(false) // lhs_ and rhs_ are not used
3341 | StrictField::encode(strict_)
3342 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003343 | IncludeNumberCompareField::encode(include_number_compare_)
3344 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003345}
3346
3347
3348// Unfortunately you have to run without snapshots to see most of these
3349// names in the profile since most compare stubs end up in the snapshot.
3350const char* CompareStub::GetName() {
3351 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3352
3353 if (name_ != NULL) return name_;
3354 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01003355 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
3356 kMaxNameLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003357 if (name_ == NULL) return "OOM";
3358
3359 const char* cc_name;
3360 switch (cc_) {
3361 case less: cc_name = "LT"; break;
3362 case greater: cc_name = "GT"; break;
3363 case less_equal: cc_name = "LE"; break;
3364 case greater_equal: cc_name = "GE"; break;
3365 case equal: cc_name = "EQ"; break;
3366 case not_equal: cc_name = "NE"; break;
3367 default: cc_name = "UnknownCondition"; break;
3368 }
3369
3370 const char* strict_name = "";
3371 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
3372 strict_name = "_STRICT";
3373 }
3374
3375 const char* never_nan_nan_name = "";
3376 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
3377 never_nan_nan_name = "_NO_NAN";
3378 }
3379
3380 const char* include_number_compare_name = "";
3381 if (!include_number_compare_) {
3382 include_number_compare_name = "_NO_NUMBER";
3383 }
3384
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003385 const char* include_smi_compare_name = "";
3386 if (!include_smi_compare_) {
3387 include_smi_compare_name = "_NO_SMI";
3388 }
3389
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003390 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
3391 "CompareStub_%s%s%s%s",
3392 cc_name,
3393 strict_name,
3394 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003395 include_number_compare_name,
3396 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003397 return name_;
3398}
3399
3400
3401// -------------------------------------------------------------------------
3402// StringCharCodeAtGenerator
3403
3404void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3405 Label flat_string;
3406 Label ascii_string;
3407 Label got_char_code;
3408
3409 // If the receiver is a smi trigger the non-string case.
3410 __ JumpIfSmi(object_, receiver_not_string_);
3411
3412 // Fetch the instance type of the receiver into result register.
3413 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
3414 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3415 // If the receiver is not a string trigger the non-string case.
3416 __ testb(result_, Immediate(kIsNotStringMask));
3417 __ j(not_zero, receiver_not_string_);
3418
3419 // If the index is non-smi trigger the non-smi case.
3420 __ JumpIfNotSmi(index_, &index_not_smi_);
3421
3422 // Put smi-tagged index into scratch register.
3423 __ movq(scratch_, index_);
3424 __ bind(&got_smi_index_);
3425
3426 // Check for index out of range.
3427 __ SmiCompare(scratch_, FieldOperand(object_, String::kLengthOffset));
3428 __ j(above_equal, index_out_of_range_);
3429
3430 // We need special handling for non-flat strings.
3431 STATIC_ASSERT(kSeqStringTag == 0);
3432 __ testb(result_, Immediate(kStringRepresentationMask));
3433 __ j(zero, &flat_string);
3434
3435 // Handle non-flat strings.
3436 __ testb(result_, Immediate(kIsConsStringMask));
3437 __ j(zero, &call_runtime_);
3438
3439 // ConsString.
3440 // Check whether the right hand side is the empty string (i.e. if
3441 // this is really a flat string in a cons string). If that is not
3442 // the case we would rather go to the runtime system now to flatten
3443 // the string.
3444 __ CompareRoot(FieldOperand(object_, ConsString::kSecondOffset),
3445 Heap::kEmptyStringRootIndex);
3446 __ j(not_equal, &call_runtime_);
3447 // Get the first of the two strings and load its instance type.
3448 __ movq(object_, FieldOperand(object_, ConsString::kFirstOffset));
3449 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
3450 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3451 // If the first cons component is also non-flat, then go to runtime.
3452 STATIC_ASSERT(kSeqStringTag == 0);
3453 __ testb(result_, Immediate(kStringRepresentationMask));
3454 __ j(not_zero, &call_runtime_);
3455
3456 // Check for 1-byte or 2-byte string.
3457 __ bind(&flat_string);
3458 STATIC_ASSERT(kAsciiStringTag != 0);
3459 __ testb(result_, Immediate(kStringEncodingMask));
3460 __ j(not_zero, &ascii_string);
3461
3462 // 2-byte string.
3463 // Load the 2-byte character code into the result register.
3464 __ SmiToInteger32(scratch_, scratch_);
3465 __ movzxwl(result_, FieldOperand(object_,
3466 scratch_, times_2,
3467 SeqTwoByteString::kHeaderSize));
3468 __ jmp(&got_char_code);
3469
3470 // ASCII string.
3471 // Load the byte into the result register.
3472 __ bind(&ascii_string);
3473 __ SmiToInteger32(scratch_, scratch_);
3474 __ movzxbl(result_, FieldOperand(object_,
3475 scratch_, times_1,
3476 SeqAsciiString::kHeaderSize));
3477 __ bind(&got_char_code);
3478 __ Integer32ToSmi(result_, result_);
3479 __ bind(&exit_);
3480}
3481
3482
3483void StringCharCodeAtGenerator::GenerateSlow(
3484 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3485 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3486
Ben Murdoch8b112d22011-06-08 16:22:53 +01003487 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003488 // Index is not a smi.
3489 __ bind(&index_not_smi_);
3490 // If index is a heap number, try converting it to an integer.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003491 __ CheckMap(index_, factory->heap_number_map(), index_not_number_, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003492 call_helper.BeforeCall(masm);
3493 __ push(object_);
3494 __ push(index_);
3495 __ push(index_); // Consumed by runtime conversion function.
3496 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3497 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3498 } else {
3499 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3500 // NumberToSmi discards numbers that are not exact integers.
3501 __ CallRuntime(Runtime::kNumberToSmi, 1);
3502 }
3503 if (!scratch_.is(rax)) {
3504 // Save the conversion result before the pop instructions below
3505 // have a chance to overwrite it.
3506 __ movq(scratch_, rax);
3507 }
3508 __ pop(index_);
3509 __ pop(object_);
3510 // Reload the instance type.
3511 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
3512 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3513 call_helper.AfterCall(masm);
3514 // If index is still not a smi, it must be out of range.
3515 __ JumpIfNotSmi(scratch_, index_out_of_range_);
3516 // Otherwise, return to the fast path.
3517 __ jmp(&got_smi_index_);
3518
3519 // Call runtime. We get here when the receiver is a string and the
3520 // index is a number, but the code of getting the actual character
3521 // is too complex (e.g., when the string needs to be flattened).
3522 __ bind(&call_runtime_);
3523 call_helper.BeforeCall(masm);
3524 __ push(object_);
3525 __ push(index_);
3526 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3527 if (!result_.is(rax)) {
3528 __ movq(result_, rax);
3529 }
3530 call_helper.AfterCall(masm);
3531 __ jmp(&exit_);
3532
3533 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3534}
3535
3536
3537// -------------------------------------------------------------------------
3538// StringCharFromCodeGenerator
3539
3540void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3541 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3542 __ JumpIfNotSmi(code_, &slow_case_);
3543 __ SmiCompare(code_, Smi::FromInt(String::kMaxAsciiCharCode));
3544 __ j(above, &slow_case_);
3545
3546 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3547 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
3548 __ movq(result_, FieldOperand(result_, index.reg, index.scale,
3549 FixedArray::kHeaderSize));
3550 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
3551 __ j(equal, &slow_case_);
3552 __ bind(&exit_);
3553}
3554
3555
3556void StringCharFromCodeGenerator::GenerateSlow(
3557 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3558 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3559
3560 __ bind(&slow_case_);
3561 call_helper.BeforeCall(masm);
3562 __ push(code_);
3563 __ CallRuntime(Runtime::kCharFromCode, 1);
3564 if (!result_.is(rax)) {
3565 __ movq(result_, rax);
3566 }
3567 call_helper.AfterCall(masm);
3568 __ jmp(&exit_);
3569
3570 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3571}
3572
3573
3574// -------------------------------------------------------------------------
3575// StringCharAtGenerator
3576
3577void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3578 char_code_at_generator_.GenerateFast(masm);
3579 char_from_code_generator_.GenerateFast(masm);
3580}
3581
3582
3583void StringCharAtGenerator::GenerateSlow(
3584 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3585 char_code_at_generator_.GenerateSlow(masm, call_helper);
3586 char_from_code_generator_.GenerateSlow(masm, call_helper);
3587}
3588
3589
3590void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003591 Label string_add_runtime, call_builtin;
3592 Builtins::JavaScript builtin_id = Builtins::ADD;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003593
3594 // Load the two arguments.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003595 __ movq(rax, Operand(rsp, 2 * kPointerSize)); // First argument (left).
3596 __ movq(rdx, Operand(rsp, 1 * kPointerSize)); // Second argument (right).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003597
3598 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003599 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003600 Condition is_smi;
3601 is_smi = masm->CheckSmi(rax);
3602 __ j(is_smi, &string_add_runtime);
3603 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, r8);
3604 __ j(above_equal, &string_add_runtime);
3605
3606 // First argument is a a string, test second.
3607 is_smi = masm->CheckSmi(rdx);
3608 __ j(is_smi, &string_add_runtime);
3609 __ CmpObjectType(rdx, FIRST_NONSTRING_TYPE, r9);
3610 __ j(above_equal, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003611 } else {
3612 // Here at least one of the arguments is definitely a string.
3613 // We convert the one that is not known to be a string.
3614 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
3615 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
3616 GenerateConvertArgument(masm, 2 * kPointerSize, rax, rbx, rcx, rdi,
3617 &call_builtin);
3618 builtin_id = Builtins::STRING_ADD_RIGHT;
3619 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
3620 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
3621 GenerateConvertArgument(masm, 1 * kPointerSize, rdx, rbx, rcx, rdi,
3622 &call_builtin);
3623 builtin_id = Builtins::STRING_ADD_LEFT;
3624 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003625 }
3626
3627 // Both arguments are strings.
3628 // rax: first string
3629 // rdx: second string
3630 // Check if either of the strings are empty. In that case return the other.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003631 NearLabel second_not_zero_length, both_not_zero_length;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003632 __ movq(rcx, FieldOperand(rdx, String::kLengthOffset));
3633 __ SmiTest(rcx);
3634 __ j(not_zero, &second_not_zero_length);
3635 // Second string is empty, result is first string which is already in rax.
Steve Block44f0eee2011-05-26 01:26:41 +01003636 Counters* counters = masm->isolate()->counters();
3637 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003638 __ ret(2 * kPointerSize);
3639 __ bind(&second_not_zero_length);
3640 __ movq(rbx, FieldOperand(rax, String::kLengthOffset));
3641 __ SmiTest(rbx);
3642 __ j(not_zero, &both_not_zero_length);
3643 // First string is empty, result is second string which is in rdx.
3644 __ movq(rax, rdx);
Steve Block44f0eee2011-05-26 01:26:41 +01003645 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003646 __ ret(2 * kPointerSize);
3647
3648 // Both strings are non-empty.
3649 // rax: first string
3650 // rbx: length of first string
3651 // rcx: length of second string
3652 // rdx: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003653 // r8: map of first string (if flags_ == NO_STRING_ADD_FLAGS)
3654 // r9: map of second string (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003655 Label string_add_flat_result, longer_than_two;
3656 __ bind(&both_not_zero_length);
3657
3658 // If arguments where known to be strings, maps are not loaded to r8 and r9
3659 // by the code above.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003660 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003661 __ movq(r8, FieldOperand(rax, HeapObject::kMapOffset));
3662 __ movq(r9, FieldOperand(rdx, HeapObject::kMapOffset));
3663 }
3664 // Get the instance types of the two strings as they will be needed soon.
3665 __ movzxbl(r8, FieldOperand(r8, Map::kInstanceTypeOffset));
3666 __ movzxbl(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
3667
3668 // Look at the length of the result of adding the two strings.
3669 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue / 2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003670 __ SmiAdd(rbx, rbx, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01003671 // Use the symbol table when adding two one character strings, as it
3672 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003673 __ SmiCompare(rbx, Smi::FromInt(2));
3674 __ j(not_equal, &longer_than_two);
3675
3676 // Check that both strings are non-external ascii strings.
3677 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r8, r9, rbx, rcx,
3678 &string_add_runtime);
3679
3680 // Get the two characters forming the sub string.
3681 __ movzxbq(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
3682 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
3683
3684 // Try to lookup two character string in symbol table. If it is not found
3685 // just allocate a new one.
3686 Label make_two_character_string, make_flat_ascii_string;
3687 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Steve Block44f0eee2011-05-26 01:26:41 +01003688 masm, rbx, rcx, r14, r11, rdi, r15, &make_two_character_string);
3689 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003690 __ ret(2 * kPointerSize);
3691
3692 __ bind(&make_two_character_string);
3693 __ Set(rbx, 2);
3694 __ jmp(&make_flat_ascii_string);
3695
3696 __ bind(&longer_than_two);
3697 // Check if resulting string will be flat.
3698 __ SmiCompare(rbx, Smi::FromInt(String::kMinNonFlatLength));
3699 __ j(below, &string_add_flat_result);
3700 // Handle exceptionally long strings in the runtime system.
3701 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
3702 __ SmiCompare(rbx, Smi::FromInt(String::kMaxLength));
3703 __ j(above, &string_add_runtime);
3704
3705 // If result is not supposed to be flat, allocate a cons string object. If
3706 // both strings are ascii the result is an ascii cons string.
3707 // rax: first string
3708 // rbx: length of resulting flat string
3709 // rdx: second string
3710 // r8: instance type of first string
3711 // r9: instance type of second string
3712 Label non_ascii, allocated, ascii_data;
3713 __ movl(rcx, r8);
3714 __ and_(rcx, r9);
3715 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3716 __ testl(rcx, Immediate(kAsciiStringTag));
3717 __ j(zero, &non_ascii);
3718 __ bind(&ascii_data);
3719 // Allocate an acsii cons string.
3720 __ AllocateAsciiConsString(rcx, rdi, no_reg, &string_add_runtime);
3721 __ bind(&allocated);
3722 // Fill the fields of the cons string.
3723 __ movq(FieldOperand(rcx, ConsString::kLengthOffset), rbx);
3724 __ movq(FieldOperand(rcx, ConsString::kHashFieldOffset),
3725 Immediate(String::kEmptyHashField));
3726 __ movq(FieldOperand(rcx, ConsString::kFirstOffset), rax);
3727 __ movq(FieldOperand(rcx, ConsString::kSecondOffset), rdx);
3728 __ movq(rax, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01003729 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003730 __ ret(2 * kPointerSize);
3731 __ bind(&non_ascii);
3732 // At least one of the strings is two-byte. Check whether it happens
3733 // to contain only ascii characters.
3734 // rcx: first instance type AND second instance type.
3735 // r8: first instance type.
3736 // r9: second instance type.
3737 __ testb(rcx, Immediate(kAsciiDataHintMask));
3738 __ j(not_zero, &ascii_data);
3739 __ xor_(r8, r9);
3740 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
3741 __ andb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
3742 __ cmpb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
3743 __ j(equal, &ascii_data);
3744 // Allocate a two byte cons string.
3745 __ AllocateConsString(rcx, rdi, no_reg, &string_add_runtime);
3746 __ jmp(&allocated);
3747
3748 // Handle creating a flat result. First check that both strings are not
3749 // external strings.
3750 // rax: first string
3751 // rbx: length of resulting flat string as smi
3752 // rdx: second string
3753 // r8: instance type of first string
3754 // r9: instance type of first string
3755 __ bind(&string_add_flat_result);
3756 __ SmiToInteger32(rbx, rbx);
3757 __ movl(rcx, r8);
3758 __ and_(rcx, Immediate(kStringRepresentationMask));
3759 __ cmpl(rcx, Immediate(kExternalStringTag));
3760 __ j(equal, &string_add_runtime);
3761 __ movl(rcx, r9);
3762 __ and_(rcx, Immediate(kStringRepresentationMask));
3763 __ cmpl(rcx, Immediate(kExternalStringTag));
3764 __ j(equal, &string_add_runtime);
3765 // Now check if both strings are ascii strings.
3766 // rax: first string
3767 // rbx: length of resulting flat string
3768 // rdx: second string
3769 // r8: instance type of first string
3770 // r9: instance type of second string
3771 Label non_ascii_string_add_flat_result;
3772 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3773 __ testl(r8, Immediate(kAsciiStringTag));
3774 __ j(zero, &non_ascii_string_add_flat_result);
3775 __ testl(r9, Immediate(kAsciiStringTag));
3776 __ j(zero, &string_add_runtime);
3777
3778 __ bind(&make_flat_ascii_string);
3779 // Both strings are ascii strings. As they are short they are both flat.
3780 __ AllocateAsciiString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
3781 // rcx: result string
3782 __ movq(rbx, rcx);
3783 // Locate first character of result.
3784 __ addq(rcx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3785 // Locate first character of first argument
3786 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
3787 __ addq(rax, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3788 // rax: first char of first argument
3789 // rbx: result string
3790 // rcx: first character of result
3791 // rdx: second string
3792 // rdi: length of first argument
3793 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, true);
3794 // Locate first character of second argument.
3795 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
3796 __ addq(rdx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3797 // rbx: result string
3798 // rcx: next character of result
3799 // rdx: first char of second argument
3800 // rdi: length of second argument
3801 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, true);
3802 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01003803 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003804 __ ret(2 * kPointerSize);
3805
3806 // Handle creating a flat two byte result.
3807 // rax: first string - known to be two byte
3808 // rbx: length of resulting flat string
3809 // rdx: second string
3810 // r8: instance type of first string
3811 // r9: instance type of first string
3812 __ bind(&non_ascii_string_add_flat_result);
3813 __ and_(r9, Immediate(kAsciiStringTag));
3814 __ j(not_zero, &string_add_runtime);
3815 // Both strings are two byte strings. As they are short they are both
3816 // flat.
3817 __ AllocateTwoByteString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
3818 // rcx: result string
3819 __ movq(rbx, rcx);
3820 // Locate first character of result.
3821 __ addq(rcx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3822 // Locate first character of first argument.
3823 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
3824 __ addq(rax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3825 // rax: first char of first argument
3826 // rbx: result string
3827 // rcx: first character of result
3828 // rdx: second argument
3829 // rdi: length of first argument
3830 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, false);
3831 // Locate first character of second argument.
3832 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
3833 __ addq(rdx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3834 // rbx: result string
3835 // rcx: next character of result
3836 // rdx: first char of second argument
3837 // rdi: length of second argument
3838 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, false);
3839 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01003840 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003841 __ ret(2 * kPointerSize);
3842
3843 // Just jump to runtime to add the two strings.
3844 __ bind(&string_add_runtime);
3845 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003846
3847 if (call_builtin.is_linked()) {
3848 __ bind(&call_builtin);
3849 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
3850 }
3851}
3852
3853
3854void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
3855 int stack_offset,
3856 Register arg,
3857 Register scratch1,
3858 Register scratch2,
3859 Register scratch3,
3860 Label* slow) {
3861 // First check if the argument is already a string.
3862 Label not_string, done;
3863 __ JumpIfSmi(arg, &not_string);
3864 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
3865 __ j(below, &done);
3866
3867 // Check the number to string cache.
3868 Label not_cached;
3869 __ bind(&not_string);
3870 // Puts the cached result into scratch1.
3871 NumberToStringStub::GenerateLookupNumberStringCache(masm,
3872 arg,
3873 scratch1,
3874 scratch2,
3875 scratch3,
3876 false,
3877 &not_cached);
3878 __ movq(arg, scratch1);
3879 __ movq(Operand(rsp, stack_offset), arg);
3880 __ jmp(&done);
3881
3882 // Check if the argument is a safe string wrapper.
3883 __ bind(&not_cached);
3884 __ JumpIfSmi(arg, slow);
3885 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
3886 __ j(not_equal, slow);
3887 __ testb(FieldOperand(scratch1, Map::kBitField2Offset),
3888 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
3889 __ j(zero, slow);
3890 __ movq(arg, FieldOperand(arg, JSValue::kValueOffset));
3891 __ movq(Operand(rsp, stack_offset), arg);
3892
3893 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003894}
3895
3896
3897void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3898 Register dest,
3899 Register src,
3900 Register count,
3901 bool ascii) {
3902 Label loop;
3903 __ bind(&loop);
3904 // This loop just copies one character at a time, as it is only used for very
3905 // short strings.
3906 if (ascii) {
3907 __ movb(kScratchRegister, Operand(src, 0));
3908 __ movb(Operand(dest, 0), kScratchRegister);
3909 __ incq(src);
3910 __ incq(dest);
3911 } else {
3912 __ movzxwl(kScratchRegister, Operand(src, 0));
3913 __ movw(Operand(dest, 0), kScratchRegister);
3914 __ addq(src, Immediate(2));
3915 __ addq(dest, Immediate(2));
3916 }
3917 __ decl(count);
3918 __ j(not_zero, &loop);
3919}
3920
3921
3922void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
3923 Register dest,
3924 Register src,
3925 Register count,
3926 bool ascii) {
3927 // Copy characters using rep movs of doublewords. Align destination on 4 byte
3928 // boundary before starting rep movs. Copy remaining characters after running
3929 // rep movs.
3930 // Count is positive int32, dest and src are character pointers.
3931 ASSERT(dest.is(rdi)); // rep movs destination
3932 ASSERT(src.is(rsi)); // rep movs source
3933 ASSERT(count.is(rcx)); // rep movs count
3934
3935 // Nothing to do for zero characters.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003936 NearLabel done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003937 __ testl(count, count);
3938 __ j(zero, &done);
3939
3940 // Make count the number of bytes to copy.
3941 if (!ascii) {
3942 STATIC_ASSERT(2 == sizeof(uc16));
3943 __ addl(count, count);
3944 }
3945
3946 // Don't enter the rep movs if there are less than 4 bytes to copy.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003947 NearLabel last_bytes;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003948 __ testl(count, Immediate(~7));
3949 __ j(zero, &last_bytes);
3950
3951 // Copy from edi to esi using rep movs instruction.
3952 __ movl(kScratchRegister, count);
3953 __ shr(count, Immediate(3)); // Number of doublewords to copy.
3954 __ repmovsq();
3955
3956 // Find number of bytes left.
3957 __ movl(count, kScratchRegister);
3958 __ and_(count, Immediate(7));
3959
3960 // Check if there are more bytes to copy.
3961 __ bind(&last_bytes);
3962 __ testl(count, count);
3963 __ j(zero, &done);
3964
3965 // Copy remaining characters.
3966 Label loop;
3967 __ bind(&loop);
3968 __ movb(kScratchRegister, Operand(src, 0));
3969 __ movb(Operand(dest, 0), kScratchRegister);
3970 __ incq(src);
3971 __ incq(dest);
3972 __ decl(count);
3973 __ j(not_zero, &loop);
3974
3975 __ bind(&done);
3976}
3977
3978void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
3979 Register c1,
3980 Register c2,
3981 Register scratch1,
3982 Register scratch2,
3983 Register scratch3,
3984 Register scratch4,
3985 Label* not_found) {
3986 // Register scratch3 is the general scratch register in this function.
3987 Register scratch = scratch3;
3988
3989 // Make sure that both characters are not digits as such strings has a
3990 // different hash algorithm. Don't try to look for these in the symbol table.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003991 NearLabel not_array_index;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003992 __ leal(scratch, Operand(c1, -'0'));
3993 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
3994 __ j(above, &not_array_index);
3995 __ leal(scratch, Operand(c2, -'0'));
3996 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
3997 __ j(below_equal, not_found);
3998
3999 __ bind(&not_array_index);
4000 // Calculate the two character string hash.
4001 Register hash = scratch1;
4002 GenerateHashInit(masm, hash, c1, scratch);
4003 GenerateHashAddCharacter(masm, hash, c2, scratch);
4004 GenerateHashGetHash(masm, hash, scratch);
4005
4006 // Collect the two characters in a register.
4007 Register chars = c1;
4008 __ shl(c2, Immediate(kBitsPerByte));
4009 __ orl(chars, c2);
4010
4011 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4012 // hash: hash of two character string.
4013
4014 // Load the symbol table.
4015 Register symbol_table = c2;
4016 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4017
4018 // Calculate capacity mask from the symbol table capacity.
4019 Register mask = scratch2;
4020 __ SmiToInteger32(mask,
4021 FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4022 __ decl(mask);
4023
Steve Block44f0eee2011-05-26 01:26:41 +01004024 Register map = scratch4;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004025
4026 // Registers
4027 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4028 // hash: hash of two character string (32-bit int)
4029 // symbol_table: symbol table
4030 // mask: capacity mask (32-bit int)
Steve Block44f0eee2011-05-26 01:26:41 +01004031 // map: -
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004032 // scratch: -
4033
4034 // Perform a number of probes in the symbol table.
4035 static const int kProbes = 4;
4036 Label found_in_symbol_table;
4037 Label next_probe[kProbes];
4038 for (int i = 0; i < kProbes; i++) {
4039 // Calculate entry in symbol table.
4040 __ movl(scratch, hash);
4041 if (i > 0) {
4042 __ addl(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
4043 }
4044 __ andl(scratch, mask);
4045
Steve Block44f0eee2011-05-26 01:26:41 +01004046 // Load the entry from the symbol table.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004047 Register candidate = scratch; // Scratch register contains candidate.
4048 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4049 __ movq(candidate,
4050 FieldOperand(symbol_table,
4051 scratch,
4052 times_pointer_size,
4053 SymbolTable::kElementsStartOffset));
4054
4055 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01004056 NearLabel is_string;
4057 __ CmpObjectType(candidate, ODDBALL_TYPE, map);
4058 __ j(not_equal, &is_string);
4059
4060 __ CompareRoot(candidate, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004061 __ j(equal, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01004062 // Must be null (deleted entry).
4063 __ jmp(&next_probe[i]);
4064
4065 __ bind(&is_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004066
4067 // If length is not 2 the string is not a candidate.
4068 __ SmiCompare(FieldOperand(candidate, String::kLengthOffset),
4069 Smi::FromInt(2));
4070 __ j(not_equal, &next_probe[i]);
4071
4072 // We use kScratchRegister as a temporary register in assumption that
4073 // JumpIfInstanceTypeIsNotSequentialAscii does not use it implicitly
4074 Register temp = kScratchRegister;
4075
4076 // Check that the candidate is a non-external ascii string.
Steve Block44f0eee2011-05-26 01:26:41 +01004077 __ movzxbl(temp, FieldOperand(map, Map::kInstanceTypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004078 __ JumpIfInstanceTypeIsNotSequentialAscii(
4079 temp, temp, &next_probe[i]);
4080
4081 // Check if the two characters match.
4082 __ movl(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4083 __ andl(temp, Immediate(0x0000ffff));
4084 __ cmpl(chars, temp);
4085 __ j(equal, &found_in_symbol_table);
4086 __ bind(&next_probe[i]);
4087 }
4088
4089 // No matching 2 character string found by probing.
4090 __ jmp(not_found);
4091
4092 // Scratch register contains result when we fall through to here.
4093 Register result = scratch;
4094 __ bind(&found_in_symbol_table);
4095 if (!result.is(rax)) {
4096 __ movq(rax, result);
4097 }
4098}
4099
4100
4101void StringHelper::GenerateHashInit(MacroAssembler* masm,
4102 Register hash,
4103 Register character,
4104 Register scratch) {
4105 // hash = character + (character << 10);
4106 __ movl(hash, character);
4107 __ shll(hash, Immediate(10));
4108 __ addl(hash, character);
4109 // hash ^= hash >> 6;
4110 __ movl(scratch, hash);
4111 __ sarl(scratch, Immediate(6));
4112 __ xorl(hash, scratch);
4113}
4114
4115
4116void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4117 Register hash,
4118 Register character,
4119 Register scratch) {
4120 // hash += character;
4121 __ addl(hash, character);
4122 // hash += hash << 10;
4123 __ movl(scratch, hash);
4124 __ shll(scratch, Immediate(10));
4125 __ addl(hash, scratch);
4126 // hash ^= hash >> 6;
4127 __ movl(scratch, hash);
4128 __ sarl(scratch, Immediate(6));
4129 __ xorl(hash, scratch);
4130}
4131
4132
4133void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4134 Register hash,
4135 Register scratch) {
4136 // hash += hash << 3;
4137 __ leal(hash, Operand(hash, hash, times_8, 0));
4138 // hash ^= hash >> 11;
4139 __ movl(scratch, hash);
4140 __ sarl(scratch, Immediate(11));
4141 __ xorl(hash, scratch);
4142 // hash += hash << 15;
4143 __ movl(scratch, hash);
4144 __ shll(scratch, Immediate(15));
4145 __ addl(hash, scratch);
4146
4147 // if (hash == 0) hash = 27;
4148 Label hash_not_zero;
4149 __ j(not_zero, &hash_not_zero);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004150 __ Set(hash, 27);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004151 __ bind(&hash_not_zero);
4152}
4153
4154void SubStringStub::Generate(MacroAssembler* masm) {
4155 Label runtime;
4156
4157 // Stack frame on entry.
4158 // rsp[0]: return address
4159 // rsp[8]: to
4160 // rsp[16]: from
4161 // rsp[24]: string
4162
4163 const int kToOffset = 1 * kPointerSize;
4164 const int kFromOffset = kToOffset + kPointerSize;
4165 const int kStringOffset = kFromOffset + kPointerSize;
4166 const int kArgumentsSize = (kStringOffset + kPointerSize) - kToOffset;
4167
4168 // Make sure first argument is a string.
4169 __ movq(rax, Operand(rsp, kStringOffset));
4170 STATIC_ASSERT(kSmiTag == 0);
4171 __ testl(rax, Immediate(kSmiTagMask));
4172 __ j(zero, &runtime);
4173 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
4174 __ j(NegateCondition(is_string), &runtime);
4175
4176 // rax: string
4177 // rbx: instance type
4178 // Calculate length of sub string using the smi values.
4179 Label result_longer_than_two;
4180 __ movq(rcx, Operand(rsp, kToOffset));
4181 __ movq(rdx, Operand(rsp, kFromOffset));
Ben Murdochf87a2032010-10-22 12:50:53 +01004182 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004183
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004184 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004185 __ cmpq(FieldOperand(rax, String::kLengthOffset), rcx);
4186 Label return_rax;
4187 __ j(equal, &return_rax);
4188 // Special handling of sub-strings of length 1 and 2. One character strings
4189 // are handled in the runtime system (looked up in the single character
4190 // cache). Two character strings are looked for in the symbol cache.
4191 __ SmiToInteger32(rcx, rcx);
4192 __ cmpl(rcx, Immediate(2));
4193 __ j(greater, &result_longer_than_two);
4194 __ j(less, &runtime);
4195
4196 // Sub string of length 2 requested.
4197 // rax: string
4198 // rbx: instance type
4199 // rcx: sub string length (value is 2)
4200 // rdx: from index (smi)
4201 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &runtime);
4202
4203 // Get the two characters forming the sub string.
4204 __ SmiToInteger32(rdx, rdx); // From index is no longer smi.
4205 __ movzxbq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
4206 __ movzxbq(rcx,
4207 FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize + 1));
4208
4209 // Try to lookup two character string in symbol table.
4210 Label make_two_character_string;
4211 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4212 masm, rbx, rcx, rax, rdx, rdi, r14, &make_two_character_string);
4213 __ ret(3 * kPointerSize);
4214
4215 __ bind(&make_two_character_string);
4216 // Setup registers for allocating the two character string.
4217 __ movq(rax, Operand(rsp, kStringOffset));
4218 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
4219 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
4220 __ Set(rcx, 2);
4221
4222 __ bind(&result_longer_than_two);
4223
4224 // rax: string
4225 // rbx: instance type
4226 // rcx: result string length
4227 // Check for flat ascii string
4228 Label non_ascii_flat;
4229 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &non_ascii_flat);
4230
4231 // Allocate the result.
4232 __ AllocateAsciiString(rax, rcx, rbx, rdx, rdi, &runtime);
4233
4234 // rax: result string
4235 // rcx: result string length
4236 __ movq(rdx, rsi); // esi used by following code.
4237 // Locate first character of result.
4238 __ lea(rdi, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4239 // Load string argument and locate character of sub string start.
4240 __ movq(rsi, Operand(rsp, kStringOffset));
4241 __ movq(rbx, Operand(rsp, kFromOffset));
4242 {
4243 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_1);
4244 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
4245 SeqAsciiString::kHeaderSize - kHeapObjectTag));
4246 }
4247
4248 // rax: result string
4249 // rcx: result length
4250 // rdx: original value of rsi
4251 // rdi: first character of result
4252 // rsi: character of sub string start
4253 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, true);
4254 __ movq(rsi, rdx); // Restore rsi.
Steve Block44f0eee2011-05-26 01:26:41 +01004255 Counters* counters = masm->isolate()->counters();
4256 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004257 __ ret(kArgumentsSize);
4258
4259 __ bind(&non_ascii_flat);
4260 // rax: string
4261 // rbx: instance type & kStringRepresentationMask | kStringEncodingMask
4262 // rcx: result string length
4263 // Check for sequential two byte string
4264 __ cmpb(rbx, Immediate(kSeqStringTag | kTwoByteStringTag));
4265 __ j(not_equal, &runtime);
4266
4267 // Allocate the result.
4268 __ AllocateTwoByteString(rax, rcx, rbx, rdx, rdi, &runtime);
4269
4270 // rax: result string
4271 // rcx: result string length
4272 __ movq(rdx, rsi); // esi used by following code.
4273 // Locate first character of result.
4274 __ lea(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
4275 // Load string argument and locate character of sub string start.
4276 __ movq(rsi, Operand(rsp, kStringOffset));
4277 __ movq(rbx, Operand(rsp, kFromOffset));
4278 {
4279 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_2);
4280 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
4281 SeqAsciiString::kHeaderSize - kHeapObjectTag));
4282 }
4283
4284 // rax: result string
4285 // rcx: result length
4286 // rdx: original value of rsi
4287 // rdi: first character of result
4288 // rsi: character of sub string start
4289 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, false);
4290 __ movq(rsi, rdx); // Restore esi.
4291
4292 __ bind(&return_rax);
Steve Block44f0eee2011-05-26 01:26:41 +01004293 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004294 __ ret(kArgumentsSize);
4295
4296 // Just jump to runtime to create the sub string.
4297 __ bind(&runtime);
4298 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4299}
4300
4301
4302void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4303 Register left,
4304 Register right,
4305 Register scratch1,
4306 Register scratch2,
4307 Register scratch3,
4308 Register scratch4) {
4309 // Ensure that you can always subtract a string length from a non-negative
4310 // number (e.g. another length).
4311 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
4312
4313 // Find minimum length and length difference.
4314 __ movq(scratch1, FieldOperand(left, String::kLengthOffset));
4315 __ movq(scratch4, scratch1);
4316 __ SmiSub(scratch4,
4317 scratch4,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004318 FieldOperand(right, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004319 // Register scratch4 now holds left.length - right.length.
4320 const Register length_difference = scratch4;
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004321 NearLabel left_shorter;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004322 __ j(less, &left_shorter);
4323 // The right string isn't longer that the left one.
4324 // Get the right string's length by subtracting the (non-negative) difference
4325 // from the left string's length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004326 __ SmiSub(scratch1, scratch1, length_difference);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004327 __ bind(&left_shorter);
4328 // Register scratch1 now holds Min(left.length, right.length).
4329 const Register min_length = scratch1;
4330
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004331 NearLabel compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004332 // If min-length is zero, go directly to comparing lengths.
4333 __ SmiTest(min_length);
4334 __ j(zero, &compare_lengths);
4335
4336 __ SmiToInteger32(min_length, min_length);
4337
4338 // Registers scratch2 and scratch3 are free.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004339 NearLabel result_not_equal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004340 Label loop;
4341 {
4342 // Check characters 0 .. min_length - 1 in a loop.
4343 // Use scratch3 as loop index, min_length as limit and scratch2
4344 // for computation.
4345 const Register index = scratch3;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004346 __ Set(index, 0); // Index into strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004347 __ bind(&loop);
4348 // Compare characters.
4349 // TODO(lrn): Could we load more than one character at a time?
4350 __ movb(scratch2, FieldOperand(left,
4351 index,
4352 times_1,
4353 SeqAsciiString::kHeaderSize));
4354 // Increment index and use -1 modifier on next load to give
4355 // the previous load extra time to complete.
4356 __ addl(index, Immediate(1));
4357 __ cmpb(scratch2, FieldOperand(right,
4358 index,
4359 times_1,
4360 SeqAsciiString::kHeaderSize - 1));
4361 __ j(not_equal, &result_not_equal);
4362 __ cmpl(index, min_length);
4363 __ j(not_equal, &loop);
4364 }
4365 // Completed loop without finding different characters.
4366 // Compare lengths (precomputed).
4367 __ bind(&compare_lengths);
4368 __ SmiTest(length_difference);
4369 __ j(not_zero, &result_not_equal);
4370
4371 // Result is EQUAL.
4372 __ Move(rax, Smi::FromInt(EQUAL));
4373 __ ret(0);
4374
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004375 NearLabel result_greater;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004376 __ bind(&result_not_equal);
4377 // Unequal comparison of left to right, either character or length.
4378 __ j(greater, &result_greater);
4379
4380 // Result is LESS.
4381 __ Move(rax, Smi::FromInt(LESS));
4382 __ ret(0);
4383
4384 // Result is GREATER.
4385 __ bind(&result_greater);
4386 __ Move(rax, Smi::FromInt(GREATER));
4387 __ ret(0);
4388}
4389
4390
4391void StringCompareStub::Generate(MacroAssembler* masm) {
4392 Label runtime;
4393
4394 // Stack frame on entry.
4395 // rsp[0]: return address
4396 // rsp[8]: right string
4397 // rsp[16]: left string
4398
4399 __ movq(rdx, Operand(rsp, 2 * kPointerSize)); // left
4400 __ movq(rax, Operand(rsp, 1 * kPointerSize)); // right
4401
4402 // Check for identity.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004403 NearLabel not_same;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004404 __ cmpq(rdx, rax);
4405 __ j(not_equal, &not_same);
4406 __ Move(rax, Smi::FromInt(EQUAL));
Steve Block44f0eee2011-05-26 01:26:41 +01004407 Counters* counters = masm->isolate()->counters();
4408 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004409 __ ret(2 * kPointerSize);
4410
4411 __ bind(&not_same);
4412
4413 // Check that both are sequential ASCII strings.
4414 __ JumpIfNotBothSequentialAsciiStrings(rdx, rax, rcx, rbx, &runtime);
4415
4416 // Inline comparison of ascii strings.
Steve Block44f0eee2011-05-26 01:26:41 +01004417 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004418 // Drop arguments from the stack
4419 __ pop(rcx);
4420 __ addq(rsp, Immediate(2 * kPointerSize));
4421 __ push(rcx);
4422 GenerateCompareFlatAsciiStrings(masm, rdx, rax, rcx, rbx, rdi, r8);
4423
4424 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4425 // tagged as a small integer.
4426 __ bind(&runtime);
4427 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4428}
4429
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004430
Ben Murdochb0fe1622011-05-05 13:52:32 +01004431void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01004432 ASSERT(state_ == CompareIC::SMIS);
4433 NearLabel miss;
4434 __ JumpIfNotBothSmi(rdx, rax, &miss);
4435
4436 if (GetCondition() == equal) {
4437 // For equality we do not care about the sign of the result.
4438 __ subq(rax, rdx);
4439 } else {
4440 NearLabel done;
4441 __ subq(rdx, rax);
4442 __ j(no_overflow, &done);
4443 // Correct sign of result in case of overflow.
4444 __ SmiNot(rdx, rdx);
4445 __ bind(&done);
4446 __ movq(rax, rdx);
4447 }
4448 __ ret(0);
4449
4450 __ bind(&miss);
4451 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004452}
4453
4454
4455void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01004456 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
4457
4458 NearLabel generic_stub;
4459 NearLabel unordered;
4460 NearLabel miss;
4461 Condition either_smi = masm->CheckEitherSmi(rax, rdx);
4462 __ j(either_smi, &generic_stub);
4463
4464 __ CmpObjectType(rax, HEAP_NUMBER_TYPE, rcx);
4465 __ j(not_equal, &miss);
4466 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
4467 __ j(not_equal, &miss);
4468
4469 // Load left and right operand
4470 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
4471 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
4472
4473 // Compare operands
4474 __ ucomisd(xmm0, xmm1);
4475
4476 // Don't base result on EFLAGS when a NaN is involved.
4477 __ j(parity_even, &unordered);
4478
4479 // Return a result of -1, 0, or 1, based on EFLAGS.
4480 // Performing mov, because xor would destroy the flag register.
4481 __ movl(rax, Immediate(0));
4482 __ movl(rcx, Immediate(0));
4483 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
4484 __ sbbq(rax, rcx); // Subtract one if below (aka. carry set).
4485 __ ret(0);
4486
4487 __ bind(&unordered);
4488
4489 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
4490 __ bind(&generic_stub);
4491 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4492
4493 __ bind(&miss);
4494 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004495}
4496
4497
4498void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01004499 ASSERT(state_ == CompareIC::OBJECTS);
4500 NearLabel miss;
4501 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
4502 __ j(either_smi, &miss);
4503
4504 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
4505 __ j(not_equal, &miss, not_taken);
4506 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
4507 __ j(not_equal, &miss, not_taken);
4508
4509 ASSERT(GetCondition() == equal);
4510 __ subq(rax, rdx);
4511 __ ret(0);
4512
4513 __ bind(&miss);
4514 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004515}
4516
4517
4518void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01004519 // Save the registers.
4520 __ pop(rcx);
4521 __ push(rdx);
4522 __ push(rax);
4523 __ push(rcx);
4524
4525 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01004526 ExternalReference miss =
4527 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01004528 __ EnterInternalFrame();
4529 __ push(rdx);
4530 __ push(rax);
4531 __ Push(Smi::FromInt(op_));
4532 __ CallExternalReference(miss, 3);
4533 __ LeaveInternalFrame();
4534
4535 // Compute the entry point of the rewritten stub.
4536 __ lea(rdi, FieldOperand(rax, Code::kHeaderSize));
4537
4538 // Restore registers.
4539 __ pop(rcx);
4540 __ pop(rax);
4541 __ pop(rdx);
4542 __ push(rcx);
4543
4544 // Do a tail call to the rewritten stub.
4545 __ jmp(rdi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004546}
4547
Steve Block1e0659c2011-05-24 12:43:12 +01004548
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004549#undef __
4550
4551} } // namespace v8::internal
4552
4553#endif // V8_TARGET_ARCH_X64