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