blob: dccf36b2521e82c0f25ccb6da4b9f74979af7b18 [file] [log] [blame]
ricow@chromium.org65fae842010-08-25 15:26:24 +00001// 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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000033#include "bootstrapper.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000034#include "jsregexp.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000035#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);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000152 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
153 FixedArray::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000154 __ 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 {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000871 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000872 }
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 {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000892 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000893 }
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
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001004 // 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.
1007
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001008 // 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
ricow@chromium.org65fae842010-08-25 15:26:24 +00001014 if (HasArgsInRegisters()) {
1015 GenerateRegisterArgsPush(masm);
1016 }
1017
1018 switch (op_) {
1019 case Token::ADD: {
1020 // Test for string arguments before calling runtime.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001021
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
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001038 // Test if left operand is a string.
1039 Label lhs_not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001040 __ test(lhs, Immediate(kSmiTagMask));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001041 __ j(zero, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001042 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, ecx);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001043 __ j(above_equal, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001044
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001045 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
1046 __ TailCallStub(&string_add_left_stub);
1047
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001048 Label call_runtime_with_args;
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001049 // Left operand is not a string, test right.
1050 __ bind(&lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001051 __ test(rhs, Immediate(kSmiTagMask));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001052 __ j(zero, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001053 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001054 __ j(above_equal, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001055
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001056 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
1057 __ TailCallStub(&string_add_right_stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001058
ricow@chromium.org65fae842010-08-25 15:26:24 +00001059 // Neither argument is a string.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001060 __ bind(&call_runtime);
1061 if (HasArgsInRegisters()) {
1062 GenerateRegisterArgsPush(masm);
1063 }
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001064 __ bind(&call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001065 __ 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) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001882 Label slow, done, undo;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001883
1884 if (op_ == Token::SUB) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001885 if (include_smi_code_) {
1886 // Check whether the value is a smi.
1887 Label try_float;
1888 __ test(eax, Immediate(kSmiTagMask));
1889 __ j(not_zero, &try_float, not_taken);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001890
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001891 if (negative_zero_ == kStrictNegativeZero) {
1892 // Go slow case if the value of the expression is zero
1893 // to make sure that we switch between 0 and -0.
1894 __ test(eax, Operand(eax));
1895 __ j(zero, &slow, not_taken);
1896 }
1897
1898 // The value of the expression is a smi that is not zero. Try
1899 // optimistic subtraction '0 - value'.
1900 __ mov(edx, Operand(eax));
1901 __ Set(eax, Immediate(0));
1902 __ sub(eax, Operand(edx));
1903 __ j(overflow, &undo, not_taken);
1904 __ StubReturn(1);
1905
1906 // Try floating point case.
1907 __ bind(&try_float);
1908 } else if (FLAG_debug_code) {
1909 __ AbortIfSmi(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001910 }
1911
ricow@chromium.org65fae842010-08-25 15:26:24 +00001912 __ 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) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001931 if (include_smi_code_) {
1932 Label non_smi;
1933 __ test(eax, Immediate(kSmiTagMask));
1934 __ j(not_zero, &non_smi);
1935 __ not_(eax);
1936 __ and_(eax, ~kSmiTagMask); // Remove inverted smi-tag.
1937 __ ret(0);
1938 __ bind(&non_smi);
1939 } else if (FLAG_debug_code) {
1940 __ AbortIfSmi(eax);
1941 }
1942
ricow@chromium.org65fae842010-08-25 15:26:24 +00001943 // Check if the operand is a heap number.
1944 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
1945 __ cmp(edx, Factory::heap_number_map());
1946 __ j(not_equal, &slow, not_taken);
1947
1948 // Convert the heap number in eax to an untagged integer in ecx.
1949 IntegerConvert(masm,
1950 eax,
1951 TypeInfo::Unknown(),
1952 CpuFeatures::IsSupported(SSE3),
1953 &slow);
1954
1955 // Do the bitwise operation and check if the result fits in a smi.
1956 Label try_float;
1957 __ not_(ecx);
1958 __ cmp(ecx, 0xc0000000);
1959 __ j(sign, &try_float, not_taken);
1960
1961 // Tag the result as a smi and we're done.
1962 STATIC_ASSERT(kSmiTagSize == 1);
1963 __ lea(eax, Operand(ecx, times_2, kSmiTag));
1964 __ jmp(&done);
1965
1966 // Try to store the result in a heap number.
1967 __ bind(&try_float);
1968 if (overwrite_ == UNARY_NO_OVERWRITE) {
1969 // Allocate a fresh heap number, but don't overwrite eax until
1970 // we're sure we can do it without going through the slow case
1971 // that needs the value in eax.
1972 __ AllocateHeapNumber(ebx, edx, edi, &slow);
1973 __ mov(eax, Operand(ebx));
1974 }
1975 if (CpuFeatures::IsSupported(SSE2)) {
1976 CpuFeatures::Scope use_sse2(SSE2);
1977 __ cvtsi2sd(xmm0, Operand(ecx));
1978 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1979 } else {
1980 __ push(ecx);
1981 __ fild_s(Operand(esp, 0));
1982 __ pop(ecx);
1983 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1984 }
1985 } else {
1986 UNIMPLEMENTED();
1987 }
1988
1989 // Return from the stub.
1990 __ bind(&done);
1991 __ StubReturn(1);
1992
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00001993 // Restore eax and go slow case.
1994 __ bind(&undo);
1995 __ mov(eax, Operand(edx));
1996
ricow@chromium.org65fae842010-08-25 15:26:24 +00001997 // Handle the slow case by jumping to the JavaScript builtin.
1998 __ bind(&slow);
1999 __ pop(ecx); // pop return address.
2000 __ push(eax);
2001 __ push(ecx); // push return address
2002 switch (op_) {
2003 case Token::SUB:
2004 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
2005 break;
2006 case Token::BIT_NOT:
2007 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
2008 break;
2009 default:
2010 UNREACHABLE();
2011 }
2012}
2013
2014
2015void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2016 // The key is in edx and the parameter count is in eax.
2017
2018 // The displacement is used for skipping the frame pointer on the
2019 // stack. It is the offset of the last parameter (if any) relative
2020 // to the frame pointer.
2021 static const int kDisplacement = 1 * kPointerSize;
2022
2023 // Check that the key is a smi.
2024 Label slow;
2025 __ test(edx, Immediate(kSmiTagMask));
2026 __ j(not_zero, &slow, not_taken);
2027
2028 // Check if the calling frame is an arguments adaptor frame.
2029 Label adaptor;
2030 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2031 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2032 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2033 __ j(equal, &adaptor);
2034
2035 // Check index against formal parameters count limit passed in
2036 // through register eax. Use unsigned comparison to get negative
2037 // check for free.
2038 __ cmp(edx, Operand(eax));
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(ebp, eax, times_2, 0));
2045 __ neg(edx);
2046 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2047 __ ret(0);
2048
2049 // Arguments adaptor case: Check index against actual arguments
2050 // limit found in the arguments adaptor frame. Use unsigned
2051 // comparison to get negative check for free.
2052 __ bind(&adaptor);
2053 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2054 __ cmp(edx, Operand(ecx));
2055 __ j(above_equal, &slow, not_taken);
2056
2057 // Read the argument from the stack and return it.
2058 STATIC_ASSERT(kSmiTagSize == 1);
2059 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2060 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2061 __ neg(edx);
2062 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2063 __ ret(0);
2064
2065 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2066 // by calling the runtime system.
2067 __ bind(&slow);
2068 __ pop(ebx); // Return address.
2069 __ push(edx);
2070 __ push(ebx);
2071 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2072}
2073
2074
2075void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2076 // esp[0] : return address
2077 // esp[4] : number of parameters
2078 // esp[8] : receiver displacement
2079 // esp[16] : function
2080
2081 // The displacement is used for skipping the return address and the
2082 // frame pointer on the stack. It is the offset of the last
2083 // parameter (if any) relative to the frame pointer.
2084 static const int kDisplacement = 2 * kPointerSize;
2085
2086 // Check if the calling frame is an arguments adaptor frame.
2087 Label adaptor_frame, try_allocate, runtime;
2088 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2089 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2090 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2091 __ j(equal, &adaptor_frame);
2092
2093 // Get the length from the frame.
2094 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2095 __ jmp(&try_allocate);
2096
2097 // Patch the arguments.length and the parameters pointer.
2098 __ bind(&adaptor_frame);
2099 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2100 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2101 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
2102 __ mov(Operand(esp, 2 * kPointerSize), edx);
2103
2104 // Try the new space allocation. Start out with computing the size of
2105 // the arguments object and the elements array.
2106 Label add_arguments_object;
2107 __ bind(&try_allocate);
2108 __ test(ecx, Operand(ecx));
2109 __ j(zero, &add_arguments_object);
2110 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
2111 __ bind(&add_arguments_object);
2112 __ add(Operand(ecx), Immediate(Heap::kArgumentsObjectSize));
2113
2114 // Do the allocation of both objects in one go.
2115 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
2116
2117 // Get the arguments boilerplate from the current (global) context.
2118 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
2119 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2120 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
2121 __ mov(edi, Operand(edi, offset));
2122
2123 // Copy the JS object part.
2124 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2125 __ mov(ebx, FieldOperand(edi, i));
2126 __ mov(FieldOperand(eax, i), ebx);
2127 }
2128
2129 // Setup the callee in-object property.
2130 STATIC_ASSERT(Heap::arguments_callee_index == 0);
2131 __ mov(ebx, Operand(esp, 3 * kPointerSize));
2132 __ mov(FieldOperand(eax, JSObject::kHeaderSize), ebx);
2133
2134 // Get the length (smi tagged) and set that as an in-object property too.
2135 STATIC_ASSERT(Heap::arguments_length_index == 1);
2136 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2137 __ mov(FieldOperand(eax, JSObject::kHeaderSize + kPointerSize), ecx);
2138
2139 // If there are no actual arguments, we're done.
2140 Label done;
2141 __ test(ecx, Operand(ecx));
2142 __ j(zero, &done);
2143
2144 // Get the parameters pointer from the stack.
2145 __ mov(edx, Operand(esp, 2 * kPointerSize));
2146
2147 // Setup the elements pointer in the allocated arguments object and
2148 // initialize the header in the elements fixed array.
2149 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
2150 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
2151 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
2152 Immediate(Factory::fixed_array_map()));
2153 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
2154 // Untag the length for the loop below.
2155 __ SmiUntag(ecx);
2156
2157 // Copy the fixed array slots.
2158 Label loop;
2159 __ bind(&loop);
2160 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
2161 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
2162 __ add(Operand(edi), Immediate(kPointerSize));
2163 __ sub(Operand(edx), Immediate(kPointerSize));
2164 __ dec(ecx);
2165 __ j(not_zero, &loop);
2166
2167 // Return and remove the on-stack parameters.
2168 __ bind(&done);
2169 __ ret(3 * kPointerSize);
2170
2171 // Do the runtime call to allocate the arguments object.
2172 __ bind(&runtime);
2173 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2174}
2175
2176
2177void RegExpExecStub::Generate(MacroAssembler* masm) {
2178 // Just jump directly to runtime if native RegExp is not selected at compile
2179 // time or if regexp entry in generated code is turned off runtime switch or
2180 // at compilation.
2181#ifdef V8_INTERPRETED_REGEXP
2182 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2183#else // V8_INTERPRETED_REGEXP
2184 if (!FLAG_regexp_entry_native) {
2185 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2186 return;
2187 }
2188
2189 // Stack frame on entry.
2190 // esp[0]: return address
2191 // esp[4]: last_match_info (expected JSArray)
2192 // esp[8]: previous index
2193 // esp[12]: subject string
2194 // esp[16]: JSRegExp object
2195
2196 static const int kLastMatchInfoOffset = 1 * kPointerSize;
2197 static const int kPreviousIndexOffset = 2 * kPointerSize;
2198 static const int kSubjectOffset = 3 * kPointerSize;
2199 static const int kJSRegExpOffset = 4 * kPointerSize;
2200
2201 Label runtime, invoke_regexp;
2202
2203 // Ensure that a RegExp stack is allocated.
2204 ExternalReference address_of_regexp_stack_memory_address =
2205 ExternalReference::address_of_regexp_stack_memory_address();
2206 ExternalReference address_of_regexp_stack_memory_size =
2207 ExternalReference::address_of_regexp_stack_memory_size();
2208 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
2209 __ test(ebx, Operand(ebx));
2210 __ j(zero, &runtime, not_taken);
2211
2212 // Check that the first argument is a JSRegExp object.
2213 __ mov(eax, Operand(esp, kJSRegExpOffset));
2214 STATIC_ASSERT(kSmiTag == 0);
2215 __ test(eax, Immediate(kSmiTagMask));
2216 __ j(zero, &runtime);
2217 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
2218 __ j(not_equal, &runtime);
2219 // Check that the RegExp has been compiled (data contains a fixed array).
2220 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
2221 if (FLAG_debug_code) {
2222 __ test(ecx, Immediate(kSmiTagMask));
2223 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
2224 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
2225 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
2226 }
2227
2228 // ecx: RegExp data (FixedArray)
2229 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
2230 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
2231 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
2232 __ j(not_equal, &runtime);
2233
2234 // ecx: RegExp data (FixedArray)
2235 // Check that the number of captures fit in the static offsets vector buffer.
2236 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
2237 // Calculate number of capture registers (number_of_captures + 1) * 2. This
2238 // uses the asumption that smis are 2 * their untagged value.
2239 STATIC_ASSERT(kSmiTag == 0);
2240 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2241 __ add(Operand(edx), Immediate(2)); // edx was a smi.
2242 // Check that the static offsets vector buffer is large enough.
2243 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
2244 __ j(above, &runtime);
2245
2246 // ecx: RegExp data (FixedArray)
2247 // edx: Number of capture registers
2248 // Check that the second argument is a string.
2249 __ mov(eax, Operand(esp, kSubjectOffset));
2250 __ test(eax, Immediate(kSmiTagMask));
2251 __ j(zero, &runtime);
2252 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2253 __ j(NegateCondition(is_string), &runtime);
2254 // Get the length of the string to ebx.
2255 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
2256
2257 // ebx: Length of subject string as a smi
2258 // ecx: RegExp data (FixedArray)
2259 // edx: Number of capture registers
2260 // Check that the third argument is a positive smi less than the subject
2261 // string length. A negative value will be greater (unsigned comparison).
2262 __ mov(eax, Operand(esp, kPreviousIndexOffset));
2263 __ test(eax, Immediate(kSmiTagMask));
2264 __ j(not_zero, &runtime);
2265 __ cmp(eax, Operand(ebx));
2266 __ j(above_equal, &runtime);
2267
2268 // ecx: RegExp data (FixedArray)
2269 // edx: Number of capture registers
2270 // Check that the fourth object is a JSArray object.
2271 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2272 __ test(eax, Immediate(kSmiTagMask));
2273 __ j(zero, &runtime);
2274 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
2275 __ j(not_equal, &runtime);
2276 // Check that the JSArray is in fast case.
2277 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
2278 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
2279 __ cmp(eax, Factory::fixed_array_map());
2280 __ j(not_equal, &runtime);
2281 // Check that the last match info has space for the capture registers and the
2282 // additional information.
2283 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
2284 __ SmiUntag(eax);
2285 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
2286 __ cmp(edx, Operand(eax));
2287 __ j(greater, &runtime);
2288
2289 // ecx: RegExp data (FixedArray)
2290 // Check the representation and encoding of the subject string.
2291 Label seq_ascii_string, seq_two_byte_string, check_code;
2292 __ mov(eax, Operand(esp, kSubjectOffset));
2293 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2294 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2295 // First check for flat two byte string.
2296 __ and_(ebx,
2297 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
2298 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
2299 __ j(zero, &seq_two_byte_string);
2300 // Any other flat string must be a flat ascii string.
2301 __ test(Operand(ebx),
2302 Immediate(kIsNotStringMask | kStringRepresentationMask));
2303 __ j(zero, &seq_ascii_string);
2304
2305 // Check for flat cons string.
2306 // A flat cons string is a cons string where the second part is the empty
2307 // string. In that case the subject string is just the first part of the cons
2308 // string. Also in this case the first part of the cons string is known to be
2309 // a sequential string or an external string.
2310 STATIC_ASSERT(kExternalStringTag != 0);
2311 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
2312 __ test(Operand(ebx),
2313 Immediate(kIsNotStringMask | kExternalStringTag));
2314 __ j(not_zero, &runtime);
2315 // String is a cons string.
2316 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
2317 __ cmp(Operand(edx), Factory::empty_string());
2318 __ j(not_equal, &runtime);
2319 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
2320 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2321 // String is a cons string with empty second part.
2322 // eax: first part of cons string.
2323 // ebx: map of first part of cons string.
2324 // Is first part a flat two byte string?
2325 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
2326 kStringRepresentationMask | kStringEncodingMask);
2327 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
2328 __ j(zero, &seq_two_byte_string);
2329 // Any other flat string must be ascii.
2330 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
2331 kStringRepresentationMask);
2332 __ j(not_zero, &runtime);
2333
2334 __ bind(&seq_ascii_string);
2335 // eax: subject string (flat ascii)
2336 // ecx: RegExp data (FixedArray)
2337 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
2338 __ Set(edi, Immediate(1)); // Type is ascii.
2339 __ jmp(&check_code);
2340
2341 __ bind(&seq_two_byte_string);
2342 // eax: subject string (flat two byte)
2343 // ecx: RegExp data (FixedArray)
2344 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
2345 __ Set(edi, Immediate(0)); // Type is two byte.
2346
2347 __ bind(&check_code);
2348 // Check that the irregexp code has been generated for the actual string
2349 // encoding. If it has, the field contains a code object otherwise it contains
2350 // the hole.
2351 __ CmpObjectType(edx, CODE_TYPE, ebx);
2352 __ j(not_equal, &runtime);
2353
2354 // eax: subject string
2355 // edx: code
2356 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
2357 // Load used arguments before starting to push arguments for call to native
2358 // RegExp code to avoid handling changing stack height.
2359 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
2360 __ SmiUntag(ebx); // Previous index from smi.
2361
2362 // eax: subject string
2363 // ebx: previous index
2364 // edx: code
2365 // edi: encoding of subject string (1 if ascii 0 if two_byte);
2366 // All checks done. Now push arguments for native regexp code.
2367 __ IncrementCounter(&Counters::regexp_entry_native, 1);
2368
2369 static const int kRegExpExecuteArguments = 7;
2370 __ PrepareCallCFunction(kRegExpExecuteArguments, ecx);
2371
2372 // Argument 7: Indicate that this is a direct call from JavaScript.
2373 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
2374
2375 // Argument 6: Start (high end) of backtracking stack memory area.
2376 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
2377 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
2378 __ mov(Operand(esp, 5 * kPointerSize), ecx);
2379
2380 // Argument 5: static offsets vector buffer.
2381 __ mov(Operand(esp, 4 * kPointerSize),
2382 Immediate(ExternalReference::address_of_static_offsets_vector()));
2383
2384 // Argument 4: End of string data
2385 // Argument 3: Start of string data
2386 Label setup_two_byte, setup_rest;
2387 __ test(edi, Operand(edi));
2388 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
2389 __ j(zero, &setup_two_byte);
2390 __ SmiUntag(edi);
2391 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
2392 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
2393 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
2394 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
2395 __ jmp(&setup_rest);
2396
2397 __ bind(&setup_two_byte);
2398 STATIC_ASSERT(kSmiTag == 0);
2399 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
2400 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
2401 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
2402 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
2403 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
2404
2405 __ bind(&setup_rest);
2406
2407 // Argument 2: Previous index.
2408 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2409
2410 // Argument 1: Subject string.
2411 __ mov(Operand(esp, 0 * kPointerSize), eax);
2412
2413 // Locate the code entry and call it.
2414 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
2415 __ CallCFunction(edx, kRegExpExecuteArguments);
2416
2417 // Check the result.
2418 Label success;
2419 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
2420 __ j(equal, &success, taken);
2421 Label failure;
2422 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
2423 __ j(equal, &failure, taken);
2424 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
2425 // If not exception it can only be retry. Handle that in the runtime system.
2426 __ j(not_equal, &runtime);
2427 // Result must now be exception. If there is no pending exception already a
2428 // stack overflow (on the backtrack stack) was detected in RegExp code but
2429 // haven't created the exception yet. Handle that in the runtime system.
2430 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
2431 ExternalReference pending_exception(Top::k_pending_exception_address);
2432 __ mov(eax,
2433 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
2434 __ cmp(eax, Operand::StaticVariable(pending_exception));
2435 __ j(equal, &runtime);
2436 __ bind(&failure);
2437 // For failure and exception return null.
2438 __ mov(Operand(eax), Factory::null_value());
2439 __ ret(4 * kPointerSize);
2440
2441 // Load RegExp data.
2442 __ bind(&success);
2443 __ mov(eax, Operand(esp, kJSRegExpOffset));
2444 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
2445 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
2446 // Calculate number of capture registers (number_of_captures + 1) * 2.
2447 STATIC_ASSERT(kSmiTag == 0);
2448 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2449 __ add(Operand(edx), Immediate(2)); // edx was a smi.
2450
2451 // edx: Number of capture registers
2452 // Load last_match_info which is still known to be a fast case JSArray.
2453 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2454 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
2455
2456 // ebx: last_match_info backing store (FixedArray)
2457 // edx: number of capture registers
2458 // Store the capture count.
2459 __ SmiTag(edx); // Number of capture registers to smi.
2460 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
2461 __ SmiUntag(edx); // Number of capture registers back from smi.
2462 // Store last subject and last input.
2463 __ mov(eax, Operand(esp, kSubjectOffset));
2464 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
2465 __ mov(ecx, ebx);
2466 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
2467 __ mov(eax, Operand(esp, kSubjectOffset));
2468 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
2469 __ mov(ecx, ebx);
2470 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
2471
2472 // Get the static offsets vector filled by the native regexp code.
2473 ExternalReference address_of_static_offsets_vector =
2474 ExternalReference::address_of_static_offsets_vector();
2475 __ mov(ecx, Immediate(address_of_static_offsets_vector));
2476
2477 // ebx: last_match_info backing store (FixedArray)
2478 // ecx: offsets vector
2479 // edx: number of capture registers
2480 Label next_capture, done;
2481 // Capture register counter starts from number of capture registers and
2482 // counts down until wraping after zero.
2483 __ bind(&next_capture);
2484 __ sub(Operand(edx), Immediate(1));
2485 __ j(negative, &done);
2486 // Read the value from the static offsets vector buffer.
2487 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
2488 __ SmiTag(edi);
2489 // Store the smi value in the last match info.
2490 __ mov(FieldOperand(ebx,
2491 edx,
2492 times_pointer_size,
2493 RegExpImpl::kFirstCaptureOffset),
2494 edi);
2495 __ jmp(&next_capture);
2496 __ bind(&done);
2497
2498 // Return last match info.
2499 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
2500 __ ret(4 * kPointerSize);
2501
2502 // Do the runtime call to execute the regexp.
2503 __ bind(&runtime);
2504 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2505#endif // V8_INTERPRETED_REGEXP
2506}
2507
2508
2509void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
2510 Register object,
2511 Register result,
2512 Register scratch1,
2513 Register scratch2,
2514 bool object_is_smi,
2515 Label* not_found) {
2516 // Use of registers. Register result is used as a temporary.
2517 Register number_string_cache = result;
2518 Register mask = scratch1;
2519 Register scratch = scratch2;
2520
2521 // Load the number string cache.
2522 ExternalReference roots_address = ExternalReference::roots_address();
2523 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
2524 __ mov(number_string_cache,
2525 Operand::StaticArray(scratch, times_pointer_size, roots_address));
2526 // Make the hash mask from the length of the number string cache. It
2527 // contains two elements (number and string) for each cache entry.
2528 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2529 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
2530 __ sub(Operand(mask), Immediate(1)); // Make mask.
2531
2532 // Calculate the entry in the number string cache. The hash value in the
2533 // number string cache for smis is just the smi value, and the hash for
2534 // doubles is the xor of the upper and lower words. See
2535 // Heap::GetNumberStringCache.
2536 Label smi_hash_calculated;
2537 Label load_result_from_cache;
2538 if (object_is_smi) {
2539 __ mov(scratch, object);
2540 __ SmiUntag(scratch);
2541 } else {
2542 Label not_smi, hash_calculated;
2543 STATIC_ASSERT(kSmiTag == 0);
2544 __ test(object, Immediate(kSmiTagMask));
2545 __ j(not_zero, &not_smi);
2546 __ mov(scratch, object);
2547 __ SmiUntag(scratch);
2548 __ jmp(&smi_hash_calculated);
2549 __ bind(&not_smi);
2550 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
2551 Factory::heap_number_map());
2552 __ j(not_equal, not_found);
2553 STATIC_ASSERT(8 == kDoubleSize);
2554 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2555 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2556 // Object is heap number and hash is now in scratch. Calculate cache index.
2557 __ and_(scratch, Operand(mask));
2558 Register index = scratch;
2559 Register probe = mask;
2560 __ mov(probe,
2561 FieldOperand(number_string_cache,
2562 index,
2563 times_twice_pointer_size,
2564 FixedArray::kHeaderSize));
2565 __ test(probe, Immediate(kSmiTagMask));
2566 __ j(zero, not_found);
2567 if (CpuFeatures::IsSupported(SSE2)) {
2568 CpuFeatures::Scope fscope(SSE2);
2569 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2570 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
2571 __ ucomisd(xmm0, xmm1);
2572 } else {
2573 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
2574 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
2575 __ FCmp();
2576 }
2577 __ j(parity_even, not_found); // Bail out if NaN is involved.
2578 __ j(not_equal, not_found); // The cache did not contain this value.
2579 __ jmp(&load_result_from_cache);
2580 }
2581
2582 __ bind(&smi_hash_calculated);
2583 // Object is smi and hash is now in scratch. Calculate cache index.
2584 __ and_(scratch, Operand(mask));
2585 Register index = scratch;
2586 // Check if the entry is the smi we are looking for.
2587 __ cmp(object,
2588 FieldOperand(number_string_cache,
2589 index,
2590 times_twice_pointer_size,
2591 FixedArray::kHeaderSize));
2592 __ j(not_equal, not_found);
2593
2594 // Get the result from the cache.
2595 __ bind(&load_result_from_cache);
2596 __ mov(result,
2597 FieldOperand(number_string_cache,
2598 index,
2599 times_twice_pointer_size,
2600 FixedArray::kHeaderSize + kPointerSize));
2601 __ IncrementCounter(&Counters::number_to_string_native, 1);
2602}
2603
2604
2605void NumberToStringStub::Generate(MacroAssembler* masm) {
2606 Label runtime;
2607
2608 __ mov(ebx, Operand(esp, kPointerSize));
2609
2610 // Generate code to lookup number in the number string cache.
2611 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
2612 __ ret(1 * kPointerSize);
2613
2614 __ bind(&runtime);
2615 // Handle number to string in the runtime system if not found in the cache.
2616 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
2617}
2618
2619
2620static int NegativeComparisonResult(Condition cc) {
2621 ASSERT(cc != equal);
2622 ASSERT((cc == less) || (cc == less_equal)
2623 || (cc == greater) || (cc == greater_equal));
2624 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
2625}
2626
2627void CompareStub::Generate(MacroAssembler* masm) {
2628 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
2629
2630 Label check_unequal_objects, done;
2631
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002632 // Compare two smis if required.
2633 if (include_smi_compare_) {
2634 Label non_smi, smi_done;
2635 __ mov(ecx, Operand(edx));
2636 __ or_(ecx, Operand(eax));
2637 __ test(ecx, Immediate(kSmiTagMask));
2638 __ j(not_zero, &non_smi, not_taken);
2639 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
2640 __ j(no_overflow, &smi_done);
2641 __ neg(edx); // Correct sign in case of overflow.
2642 __ bind(&smi_done);
2643 __ mov(eax, edx);
2644 __ ret(0);
2645 __ bind(&non_smi);
2646 } else if (FLAG_debug_code) {
2647 __ mov(ecx, Operand(edx));
2648 __ or_(ecx, Operand(eax));
2649 __ test(ecx, Immediate(kSmiTagMask));
2650 __ Assert(not_zero, "Unexpected smi operands.");
2651 }
2652
ricow@chromium.org65fae842010-08-25 15:26:24 +00002653 // NOTICE! This code is only reached after a smi-fast-case check, so
2654 // it is certain that at least one operand isn't a smi.
2655
2656 // Identical objects can be compared fast, but there are some tricky cases
2657 // for NaN and undefined.
2658 {
2659 Label not_identical;
2660 __ cmp(eax, Operand(edx));
2661 __ j(not_equal, &not_identical);
2662
2663 if (cc_ != equal) {
2664 // Check for undefined. undefined OP undefined is false even though
2665 // undefined == undefined.
2666 Label check_for_nan;
2667 __ cmp(edx, Factory::undefined_value());
2668 __ j(not_equal, &check_for_nan);
2669 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2670 __ ret(0);
2671 __ bind(&check_for_nan);
2672 }
2673
2674 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
2675 // so we do the second best thing - test it ourselves.
2676 // Note: if cc_ != equal, never_nan_nan_ is not used.
2677 if (never_nan_nan_ && (cc_ == equal)) {
2678 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2679 __ ret(0);
2680 } else {
2681 Label heap_number;
2682 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2683 Immediate(Factory::heap_number_map()));
2684 __ j(equal, &heap_number);
2685 if (cc_ != equal) {
2686 // Call runtime on identical JSObjects. Otherwise return equal.
2687 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2688 __ j(above_equal, &not_identical);
2689 }
2690 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2691 __ ret(0);
2692
2693 __ bind(&heap_number);
2694 // It is a heap number, so return non-equal if it's NaN and equal if
2695 // it's not NaN.
2696 // The representation of NaN values has all exponent bits (52..62) set,
2697 // and not all mantissa bits (0..51) clear.
2698 // We only accept QNaNs, which have bit 51 set.
2699 // Read top bits of double representation (second word of value).
2700
2701 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
2702 // all bits in the mask are set. We only need to check the word
2703 // that contains the exponent and high bit of the mantissa.
2704 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
2705 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
2706 __ xor_(eax, Operand(eax));
2707 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
2708 // bits.
2709 __ add(edx, Operand(edx));
2710 __ cmp(edx, kQuietNaNHighBitsMask << 1);
2711 if (cc_ == equal) {
2712 STATIC_ASSERT(EQUAL != 1);
2713 __ setcc(above_equal, eax);
2714 __ ret(0);
2715 } else {
2716 Label nan;
2717 __ j(above_equal, &nan);
2718 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2719 __ ret(0);
2720 __ bind(&nan);
2721 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2722 __ ret(0);
2723 }
2724 }
2725
2726 __ bind(&not_identical);
2727 }
2728
2729 // Strict equality can quickly decide whether objects are equal.
2730 // Non-strict object equality is slower, so it is handled later in the stub.
2731 if (cc_ == equal && strict_) {
2732 Label slow; // Fallthrough label.
2733 Label not_smis;
2734 // If we're doing a strict equality comparison, we don't have to do
2735 // type conversion, so we generate code to do fast comparison for objects
2736 // and oddballs. Non-smi numbers and strings still go through the usual
2737 // slow-case code.
2738 // If either is a Smi (we know that not both are), then they can only
2739 // be equal if the other is a HeapNumber. If so, use the slow case.
2740 STATIC_ASSERT(kSmiTag == 0);
2741 ASSERT_EQ(0, Smi::FromInt(0));
2742 __ mov(ecx, Immediate(kSmiTagMask));
2743 __ and_(ecx, Operand(eax));
2744 __ test(ecx, Operand(edx));
2745 __ j(not_zero, &not_smis);
2746 // One operand is a smi.
2747
2748 // Check whether the non-smi is a heap number.
2749 STATIC_ASSERT(kSmiTagMask == 1);
2750 // ecx still holds eax & kSmiTag, which is either zero or one.
2751 __ sub(Operand(ecx), Immediate(0x01));
2752 __ mov(ebx, edx);
2753 __ xor_(ebx, Operand(eax));
2754 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
2755 __ xor_(ebx, Operand(eax));
2756 // if eax was smi, ebx is now edx, else eax.
2757
2758 // Check if the non-smi operand is a heap number.
2759 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
2760 Immediate(Factory::heap_number_map()));
2761 // If heap number, handle it in the slow case.
2762 __ j(equal, &slow);
2763 // Return non-equal (ebx is not zero)
2764 __ mov(eax, ebx);
2765 __ ret(0);
2766
2767 __ bind(&not_smis);
2768 // If either operand is a JSObject or an oddball value, then they are not
2769 // equal since their pointers are different
2770 // There is no test for undetectability in strict equality.
2771
2772 // Get the type of the first operand.
2773 // If the first object is a JS object, we have done pointer comparison.
2774 Label first_non_object;
2775 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
2776 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2777 __ j(below, &first_non_object);
2778
2779 // Return non-zero (eax is not zero)
2780 Label return_not_equal;
2781 STATIC_ASSERT(kHeapObjectTag != 0);
2782 __ bind(&return_not_equal);
2783 __ ret(0);
2784
2785 __ bind(&first_non_object);
2786 // Check for oddballs: true, false, null, undefined.
2787 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2788 __ j(equal, &return_not_equal);
2789
2790 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
2791 __ j(above_equal, &return_not_equal);
2792
2793 // Check for oddballs: true, false, null, undefined.
2794 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2795 __ j(equal, &return_not_equal);
2796
2797 // Fall through to the general case.
2798 __ bind(&slow);
2799 }
2800
2801 // Generate the number comparison code.
2802 if (include_number_compare_) {
2803 Label non_number_comparison;
2804 Label unordered;
2805 if (CpuFeatures::IsSupported(SSE2)) {
2806 CpuFeatures::Scope use_sse2(SSE2);
2807 CpuFeatures::Scope use_cmov(CMOV);
2808
2809 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
2810 __ ucomisd(xmm0, xmm1);
2811
2812 // Don't base result on EFLAGS when a NaN is involved.
2813 __ j(parity_even, &unordered, not_taken);
2814 // Return a result of -1, 0, or 1, based on EFLAGS.
2815 __ mov(eax, 0); // equal
2816 __ mov(ecx, Immediate(Smi::FromInt(1)));
2817 __ cmov(above, eax, Operand(ecx));
2818 __ mov(ecx, Immediate(Smi::FromInt(-1)));
2819 __ cmov(below, eax, Operand(ecx));
2820 __ ret(0);
2821 } else {
2822 FloatingPointHelper::CheckFloatOperands(
2823 masm, &non_number_comparison, ebx);
2824 FloatingPointHelper::LoadFloatOperand(masm, eax);
2825 FloatingPointHelper::LoadFloatOperand(masm, edx);
2826 __ FCmp();
2827
2828 // Don't base result on EFLAGS when a NaN is involved.
2829 __ j(parity_even, &unordered, not_taken);
2830
2831 Label below_label, above_label;
2832 // Return a result of -1, 0, or 1, based on EFLAGS.
2833 __ j(below, &below_label, not_taken);
2834 __ j(above, &above_label, not_taken);
2835
2836 __ xor_(eax, Operand(eax));
2837 __ ret(0);
2838
2839 __ bind(&below_label);
2840 __ mov(eax, Immediate(Smi::FromInt(-1)));
2841 __ ret(0);
2842
2843 __ bind(&above_label);
2844 __ mov(eax, Immediate(Smi::FromInt(1)));
2845 __ ret(0);
2846 }
2847
2848 // If one of the numbers was NaN, then the result is always false.
2849 // The cc is never not-equal.
2850 __ bind(&unordered);
2851 ASSERT(cc_ != not_equal);
2852 if (cc_ == less || cc_ == less_equal) {
2853 __ mov(eax, Immediate(Smi::FromInt(1)));
2854 } else {
2855 __ mov(eax, Immediate(Smi::FromInt(-1)));
2856 }
2857 __ ret(0);
2858
2859 // The number comparison code did not provide a valid result.
2860 __ bind(&non_number_comparison);
2861 }
2862
2863 // Fast negative check for symbol-to-symbol equality.
2864 Label check_for_strings;
2865 if (cc_ == equal) {
2866 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
2867 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
2868
2869 // We've already checked for object identity, so if both operands
2870 // are symbols they aren't equal. Register eax already holds a
2871 // non-zero value, which indicates not equal, so just return.
2872 __ ret(0);
2873 }
2874
2875 __ bind(&check_for_strings);
2876
2877 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
2878 &check_unequal_objects);
2879
2880 // Inline comparison of ascii strings.
2881 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
2882 edx,
2883 eax,
2884 ecx,
2885 ebx,
2886 edi);
2887#ifdef DEBUG
2888 __ Abort("Unexpected fall-through from string comparison");
2889#endif
2890
2891 __ bind(&check_unequal_objects);
2892 if (cc_ == equal && !strict_) {
2893 // Non-strict equality. Objects are unequal if
2894 // they are both JSObjects and not undetectable,
2895 // and their pointers are different.
2896 Label not_both_objects;
2897 Label return_unequal;
2898 // At most one is a smi, so we can test for smi by adding the two.
2899 // A smi plus a heap object has the low bit set, a heap object plus
2900 // a heap object has the low bit clear.
2901 STATIC_ASSERT(kSmiTag == 0);
2902 STATIC_ASSERT(kSmiTagMask == 1);
2903 __ lea(ecx, Operand(eax, edx, times_1, 0));
2904 __ test(ecx, Immediate(kSmiTagMask));
2905 __ j(not_zero, &not_both_objects);
2906 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
2907 __ j(below, &not_both_objects);
2908 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
2909 __ j(below, &not_both_objects);
2910 // We do not bail out after this point. Both are JSObjects, and
2911 // they are equal if and only if both are undetectable.
2912 // The and of the undetectable flags is 1 if and only if they are equal.
2913 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
2914 1 << Map::kIsUndetectable);
2915 __ j(zero, &return_unequal);
2916 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
2917 1 << Map::kIsUndetectable);
2918 __ j(zero, &return_unequal);
2919 // The objects are both undetectable, so they both compare as the value
2920 // undefined, and are equal.
2921 __ Set(eax, Immediate(EQUAL));
2922 __ bind(&return_unequal);
2923 // Return non-equal by returning the non-zero object pointer in eax,
2924 // or return equal if we fell through to here.
2925 __ ret(0); // rax, rdx were pushed
2926 __ bind(&not_both_objects);
2927 }
2928
2929 // Push arguments below the return address.
2930 __ pop(ecx);
2931 __ push(edx);
2932 __ push(eax);
2933
2934 // Figure out which native to call and setup the arguments.
2935 Builtins::JavaScript builtin;
2936 if (cc_ == equal) {
2937 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
2938 } else {
2939 builtin = Builtins::COMPARE;
2940 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
2941 }
2942
2943 // Restore return address on the stack.
2944 __ push(ecx);
2945
2946 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2947 // tagged as a small integer.
2948 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
2949}
2950
2951
2952void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
2953 Label* label,
2954 Register object,
2955 Register scratch) {
2956 __ test(object, Immediate(kSmiTagMask));
2957 __ j(zero, label);
2958 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
2959 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
2960 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
2961 __ cmp(scratch, kSymbolTag | kStringTag);
2962 __ j(not_equal, label);
2963}
2964
2965
2966void StackCheckStub::Generate(MacroAssembler* masm) {
2967 // Because builtins always remove the receiver from the stack, we
2968 // have to fake one to avoid underflowing the stack. The receiver
2969 // must be inserted below the return address on the stack so we
2970 // temporarily store that in a register.
2971 __ pop(eax);
2972 __ push(Immediate(Smi::FromInt(0)));
2973 __ push(eax);
2974
2975 // Do tail-call to runtime routine.
2976 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
2977}
2978
2979
2980void CallFunctionStub::Generate(MacroAssembler* masm) {
2981 Label slow;
2982
2983 // If the receiver might be a value (string, number or boolean) check for this
2984 // and box it if it is.
2985 if (ReceiverMightBeValue()) {
2986 // Get the receiver from the stack.
2987 // +1 ~ return address
2988 Label receiver_is_value, receiver_is_js_object;
2989 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
2990
2991 // Check if receiver is a smi (which is a number value).
2992 __ test(eax, Immediate(kSmiTagMask));
2993 __ j(zero, &receiver_is_value, not_taken);
2994
2995 // Check if the receiver is a valid JS object.
2996 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
2997 __ j(above_equal, &receiver_is_js_object);
2998
2999 // Call the runtime to box the value.
3000 __ bind(&receiver_is_value);
3001 __ EnterInternalFrame();
3002 __ push(eax);
3003 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
3004 __ LeaveInternalFrame();
3005 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
3006
3007 __ bind(&receiver_is_js_object);
3008 }
3009
3010 // Get the function to call from the stack.
3011 // +2 ~ receiver, return address
3012 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
3013
3014 // Check that the function really is a JavaScript function.
3015 __ test(edi, Immediate(kSmiTagMask));
3016 __ j(zero, &slow, not_taken);
3017 // Goto slow case if we do not have a function.
3018 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
3019 __ j(not_equal, &slow, not_taken);
3020
3021 // Fast-case: Just invoke the function.
3022 ParameterCount actual(argc_);
3023 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
3024
3025 // Slow-case: Non-function called.
3026 __ bind(&slow);
3027 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3028 // of the original receiver from the call site).
3029 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
3030 __ Set(eax, Immediate(argc_));
3031 __ Set(ebx, Immediate(0));
3032 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
3033 Handle<Code> adaptor(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline));
3034 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3035}
3036
3037
3038void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
3039 // eax holds the exception.
3040
3041 // Adjust this code if not the case.
3042 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
3043
3044 // Drop the sp to the top of the handler.
3045 ExternalReference handler_address(Top::k_handler_address);
3046 __ mov(esp, Operand::StaticVariable(handler_address));
3047
3048 // Restore next handler and frame pointer, discard handler state.
3049 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3050 __ pop(Operand::StaticVariable(handler_address));
3051 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
3052 __ pop(ebp);
3053 __ pop(edx); // Remove state.
3054
3055 // Before returning we restore the context from the frame pointer if
3056 // not NULL. The frame pointer is NULL in the exception handler of
3057 // a JS entry frame.
3058 __ xor_(esi, Operand(esi)); // Tentatively set context pointer to NULL.
3059 Label skip;
3060 __ cmp(ebp, 0);
3061 __ j(equal, &skip, not_taken);
3062 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
3063 __ bind(&skip);
3064
3065 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
3066 __ ret(0);
3067}
3068
3069
3070// If true, a Handle<T> passed by value is passed and returned by
3071// using the location_ field directly. If false, it is passed and
3072// returned as a pointer to a handle.
3073#ifdef USING_BSD_ABI
3074static const bool kPassHandlesDirectly = true;
3075#else
3076static const bool kPassHandlesDirectly = false;
3077#endif
3078
3079
3080void ApiGetterEntryStub::Generate(MacroAssembler* masm) {
3081 Label empty_handle;
3082 Label prologue;
3083 Label promote_scheduled_exception;
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003084 __ EnterApiExitFrame(kStackSpace, kArgc);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003085 STATIC_ASSERT(kArgc == 4);
3086 if (kPassHandlesDirectly) {
3087 // When handles as passed directly we don't have to allocate extra
3088 // space for and pass an out parameter.
3089 __ mov(Operand(esp, 0 * kPointerSize), ebx); // name.
3090 __ mov(Operand(esp, 1 * kPointerSize), eax); // arguments pointer.
3091 } else {
3092 // The function expects three arguments to be passed but we allocate
3093 // four to get space for the output cell. The argument slots are filled
3094 // as follows:
3095 //
3096 // 3: output cell
3097 // 2: arguments pointer
3098 // 1: name
3099 // 0: pointer to the output cell
3100 //
3101 // Note that this is one more "argument" than the function expects
3102 // so the out cell will have to be popped explicitly after returning
3103 // from the function.
3104 __ mov(Operand(esp, 1 * kPointerSize), ebx); // name.
3105 __ mov(Operand(esp, 2 * kPointerSize), eax); // arguments pointer.
3106 __ mov(ebx, esp);
3107 __ add(Operand(ebx), Immediate(3 * kPointerSize));
3108 __ mov(Operand(esp, 0 * kPointerSize), ebx); // output
3109 __ mov(Operand(esp, 3 * kPointerSize), Immediate(0)); // out cell.
3110 }
3111 // Call the api function!
3112 __ call(fun()->address(), RelocInfo::RUNTIME_ENTRY);
3113 // Check if the function scheduled an exception.
3114 ExternalReference scheduled_exception_address =
3115 ExternalReference::scheduled_exception_address();
3116 __ cmp(Operand::StaticVariable(scheduled_exception_address),
3117 Immediate(Factory::the_hole_value()));
3118 __ j(not_equal, &promote_scheduled_exception, not_taken);
3119 if (!kPassHandlesDirectly) {
3120 // The returned value is a pointer to the handle holding the result.
3121 // Dereference this to get to the location.
3122 __ mov(eax, Operand(eax, 0));
3123 }
3124 // Check if the result handle holds 0.
3125 __ test(eax, Operand(eax));
3126 __ j(zero, &empty_handle, not_taken);
3127 // It was non-zero. Dereference to get the result value.
3128 __ mov(eax, Operand(eax, 0));
3129 __ bind(&prologue);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003130 __ LeaveExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003131 __ ret(0);
3132 __ bind(&promote_scheduled_exception);
3133 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
3134 __ bind(&empty_handle);
3135 // It was zero; the result is undefined.
3136 __ mov(eax, Factory::undefined_value());
3137 __ jmp(&prologue);
3138}
3139
3140
3141void CEntryStub::GenerateCore(MacroAssembler* masm,
3142 Label* throw_normal_exception,
3143 Label* throw_termination_exception,
3144 Label* throw_out_of_memory_exception,
3145 bool do_gc,
3146 bool always_allocate_scope,
3147 int /* alignment_skew */) {
3148 // eax: result parameter for PerformGC, if any
3149 // ebx: pointer to C function (C callee-saved)
3150 // ebp: frame pointer (restored after C call)
3151 // esp: stack pointer (restored after C call)
3152 // edi: number of arguments including receiver (C callee-saved)
3153 // esi: pointer to the first argument (C callee-saved)
3154
3155 // Result returned in eax, or eax+edx if result_size_ is 2.
3156
3157 // Check stack alignment.
3158 if (FLAG_debug_code) {
3159 __ CheckStackAlignment();
3160 }
3161
3162 if (do_gc) {
3163 // Pass failure code returned from last attempt as first argument to
3164 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
3165 // stack alignment is known to be correct. This function takes one argument
3166 // which is passed on the stack, and we know that the stack has been
3167 // prepared to pass at least one argument.
3168 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
3169 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
3170 }
3171
3172 ExternalReference scope_depth =
3173 ExternalReference::heap_always_allocate_scope_depth();
3174 if (always_allocate_scope) {
3175 __ inc(Operand::StaticVariable(scope_depth));
3176 }
3177
3178 // Call C function.
3179 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
3180 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
3181 __ call(Operand(ebx));
3182 // Result is in eax or edx:eax - do not destroy these registers!
3183
3184 if (always_allocate_scope) {
3185 __ dec(Operand::StaticVariable(scope_depth));
3186 }
3187
3188 // Make sure we're not trying to return 'the hole' from the runtime
3189 // call as this may lead to crashes in the IC code later.
3190 if (FLAG_debug_code) {
3191 Label okay;
3192 __ cmp(eax, Factory::the_hole_value());
3193 __ j(not_equal, &okay);
3194 __ int3();
3195 __ bind(&okay);
3196 }
3197
3198 // Check for failure result.
3199 Label failure_returned;
3200 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3201 __ lea(ecx, Operand(eax, 1));
3202 // Lower 2 bits of ecx are 0 iff eax has failure tag.
3203 __ test(ecx, Immediate(kFailureTagMask));
3204 __ j(zero, &failure_returned, not_taken);
3205
3206 // Exit the JavaScript to C++ exit frame.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003207 __ LeaveExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003208 __ ret(0);
3209
3210 // Handling of failure.
3211 __ bind(&failure_returned);
3212
3213 Label retry;
3214 // If the returned exception is RETRY_AFTER_GC continue at retry label
3215 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3216 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3217 __ j(zero, &retry, taken);
3218
3219 // Special handling of out of memory exceptions.
3220 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
3221 __ j(equal, throw_out_of_memory_exception);
3222
3223 // Retrieve the pending exception and clear the variable.
3224 ExternalReference pending_exception_address(Top::k_pending_exception_address);
3225 __ mov(eax, Operand::StaticVariable(pending_exception_address));
3226 __ mov(edx,
3227 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
3228 __ mov(Operand::StaticVariable(pending_exception_address), edx);
3229
3230 // Special handling of termination exceptions which are uncatchable
3231 // by javascript code.
3232 __ cmp(eax, Factory::termination_exception());
3233 __ j(equal, throw_termination_exception);
3234
3235 // Handle normal exception.
3236 __ jmp(throw_normal_exception);
3237
3238 // Retry.
3239 __ bind(&retry);
3240}
3241
3242
3243void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3244 UncatchableExceptionType type) {
3245 // Adjust this code if not the case.
3246 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
3247
3248 // Drop sp to the top stack handler.
3249 ExternalReference handler_address(Top::k_handler_address);
3250 __ mov(esp, Operand::StaticVariable(handler_address));
3251
3252 // Unwind the handlers until the ENTRY handler is found.
3253 Label loop, done;
3254 __ bind(&loop);
3255 // Load the type of the current stack handler.
3256 const int kStateOffset = StackHandlerConstants::kStateOffset;
3257 __ cmp(Operand(esp, kStateOffset), Immediate(StackHandler::ENTRY));
3258 __ j(equal, &done);
3259 // Fetch the next handler in the list.
3260 const int kNextOffset = StackHandlerConstants::kNextOffset;
3261 __ mov(esp, Operand(esp, kNextOffset));
3262 __ jmp(&loop);
3263 __ bind(&done);
3264
3265 // Set the top handler address to next handler past the current ENTRY handler.
3266 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3267 __ pop(Operand::StaticVariable(handler_address));
3268
3269 if (type == OUT_OF_MEMORY) {
3270 // Set external caught exception to false.
3271 ExternalReference external_caught(Top::k_external_caught_exception_address);
3272 __ mov(eax, false);
3273 __ mov(Operand::StaticVariable(external_caught), eax);
3274
3275 // Set pending exception and eax to out of memory exception.
3276 ExternalReference pending_exception(Top::k_pending_exception_address);
3277 __ mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
3278 __ mov(Operand::StaticVariable(pending_exception), eax);
3279 }
3280
3281 // Clear the context pointer.
3282 __ xor_(esi, Operand(esi));
3283
3284 // Restore fp from handler and discard handler state.
3285 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 1 * kPointerSize);
3286 __ pop(ebp);
3287 __ pop(edx); // State.
3288
3289 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
3290 __ ret(0);
3291}
3292
3293
3294void CEntryStub::Generate(MacroAssembler* masm) {
3295 // eax: number of arguments including receiver
3296 // ebx: pointer to C function (C callee-saved)
3297 // ebp: frame pointer (restored after C call)
3298 // esp: stack pointer (restored after C call)
3299 // esi: current context (C callee-saved)
3300 // edi: JS function of the caller (C callee-saved)
3301
3302 // NOTE: Invocations of builtins may return failure objects instead
3303 // of a proper result. The builtin entry handles this by performing
3304 // a garbage collection and retrying the builtin (twice).
3305
3306 // Enter the exit frame that transitions from JavaScript to C++.
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +00003307 __ EnterExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003308
3309 // eax: result parameter for PerformGC, if any (setup below)
3310 // ebx: pointer to builtin function (C callee-saved)
3311 // ebp: frame pointer (restored after C call)
3312 // esp: stack pointer (restored after C call)
3313 // edi: number of arguments including receiver (C callee-saved)
3314 // esi: argv pointer (C callee-saved)
3315
3316 Label throw_normal_exception;
3317 Label throw_termination_exception;
3318 Label throw_out_of_memory_exception;
3319
3320 // Call into the runtime system.
3321 GenerateCore(masm,
3322 &throw_normal_exception,
3323 &throw_termination_exception,
3324 &throw_out_of_memory_exception,
3325 false,
3326 false);
3327
3328 // Do space-specific GC and retry runtime call.
3329 GenerateCore(masm,
3330 &throw_normal_exception,
3331 &throw_termination_exception,
3332 &throw_out_of_memory_exception,
3333 true,
3334 false);
3335
3336 // Do full GC and retry runtime call one final time.
3337 Failure* failure = Failure::InternalError();
3338 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
3339 GenerateCore(masm,
3340 &throw_normal_exception,
3341 &throw_termination_exception,
3342 &throw_out_of_memory_exception,
3343 true,
3344 true);
3345
3346 __ bind(&throw_out_of_memory_exception);
3347 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3348
3349 __ bind(&throw_termination_exception);
3350 GenerateThrowUncatchable(masm, TERMINATION);
3351
3352 __ bind(&throw_normal_exception);
3353 GenerateThrowTOS(masm);
3354}
3355
3356
3357void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3358 Label invoke, exit;
3359#ifdef ENABLE_LOGGING_AND_PROFILING
3360 Label not_outermost_js, not_outermost_js_2;
3361#endif
3362
3363 // Setup frame.
3364 __ push(ebp);
3365 __ mov(ebp, Operand(esp));
3366
3367 // Push marker in two places.
3368 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3369 __ push(Immediate(Smi::FromInt(marker))); // context slot
3370 __ push(Immediate(Smi::FromInt(marker))); // function slot
3371 // Save callee-saved registers (C calling conventions).
3372 __ push(edi);
3373 __ push(esi);
3374 __ push(ebx);
3375
3376 // Save copies of the top frame descriptor on the stack.
3377 ExternalReference c_entry_fp(Top::k_c_entry_fp_address);
3378 __ push(Operand::StaticVariable(c_entry_fp));
3379
3380#ifdef ENABLE_LOGGING_AND_PROFILING
3381 // If this is the outermost JS call, set js_entry_sp value.
3382 ExternalReference js_entry_sp(Top::k_js_entry_sp_address);
3383 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
3384 __ j(not_equal, &not_outermost_js);
3385 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
3386 __ bind(&not_outermost_js);
3387#endif
3388
3389 // Call a faked try-block that does the invoke.
3390 __ call(&invoke);
3391
3392 // Caught exception: Store result (exception) in the pending
3393 // exception field in the JSEnv and return a failure sentinel.
3394 ExternalReference pending_exception(Top::k_pending_exception_address);
3395 __ mov(Operand::StaticVariable(pending_exception), eax);
3396 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
3397 __ jmp(&exit);
3398
3399 // Invoke: Link this frame into the handler chain.
3400 __ bind(&invoke);
3401 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3402
3403 // Clear any pending exceptions.
3404 __ mov(edx,
3405 Operand::StaticVariable(ExternalReference::the_hole_value_location()));
3406 __ mov(Operand::StaticVariable(pending_exception), edx);
3407
3408 // Fake a receiver (NULL).
3409 __ push(Immediate(0)); // receiver
3410
3411 // Invoke the function by calling through JS entry trampoline
3412 // builtin and pop the faked function when we return. Notice that we
3413 // cannot store a reference to the trampoline code directly in this
3414 // stub, because the builtin stubs may not have been generated yet.
3415 if (is_construct) {
3416 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
3417 __ mov(edx, Immediate(construct_entry));
3418 } else {
3419 ExternalReference entry(Builtins::JSEntryTrampoline);
3420 __ mov(edx, Immediate(entry));
3421 }
3422 __ mov(edx, Operand(edx, 0)); // deref address
3423 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
3424 __ call(Operand(edx));
3425
3426 // Unlink this frame from the handler chain.
3427 __ pop(Operand::StaticVariable(ExternalReference(Top::k_handler_address)));
3428 // Pop next_sp.
3429 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
3430
3431#ifdef ENABLE_LOGGING_AND_PROFILING
3432 // If current EBP value is the same as js_entry_sp value, it means that
3433 // the current function is the outermost.
3434 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
3435 __ j(not_equal, &not_outermost_js_2);
3436 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
3437 __ bind(&not_outermost_js_2);
3438#endif
3439
3440 // Restore the top frame descriptor from the stack.
3441 __ bind(&exit);
3442 __ pop(Operand::StaticVariable(ExternalReference(Top::k_c_entry_fp_address)));
3443
3444 // Restore callee-saved registers (C calling conventions).
3445 __ pop(ebx);
3446 __ pop(esi);
3447 __ pop(edi);
3448 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
3449
3450 // Restore frame pointer and return.
3451 __ pop(ebp);
3452 __ ret(0);
3453}
3454
3455
3456void InstanceofStub::Generate(MacroAssembler* masm) {
3457 // Get the object - go slow case if it's a smi.
3458 Label slow;
3459 __ mov(eax, Operand(esp, 2 * kPointerSize)); // 2 ~ return address, function
3460 __ test(eax, Immediate(kSmiTagMask));
3461 __ j(zero, &slow, not_taken);
3462
3463 // Check that the left hand is a JS object.
3464 __ IsObjectJSObjectType(eax, eax, edx, &slow);
3465
3466 // Get the prototype of the function.
3467 __ mov(edx, Operand(esp, 1 * kPointerSize)); // 1 ~ return address
3468 // edx is function, eax is map.
3469
3470 // Look up the function and the map in the instanceof cache.
3471 Label miss;
3472 ExternalReference roots_address = ExternalReference::roots_address();
3473 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
3474 __ cmp(edx, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3475 __ j(not_equal, &miss);
3476 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
3477 __ cmp(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3478 __ j(not_equal, &miss);
3479 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3480 __ mov(eax, Operand::StaticArray(ecx, times_pointer_size, roots_address));
3481 __ ret(2 * kPointerSize);
3482
3483 __ bind(&miss);
3484 __ TryGetFunctionPrototype(edx, ebx, ecx, &slow);
3485
3486 // Check that the function prototype is a JS object.
3487 __ test(ebx, Immediate(kSmiTagMask));
3488 __ j(zero, &slow, not_taken);
3489 __ IsObjectJSObjectType(ebx, ecx, ecx, &slow);
3490
3491 // Register mapping:
3492 // eax is object map.
3493 // edx is function.
3494 // ebx is function prototype.
3495 __ mov(ecx, Immediate(Heap::kInstanceofCacheMapRootIndex));
3496 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3497 __ mov(ecx, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
3498 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), edx);
3499
3500 __ mov(ecx, FieldOperand(eax, Map::kPrototypeOffset));
3501
3502 // Loop through the prototype chain looking for the function prototype.
3503 Label loop, is_instance, is_not_instance;
3504 __ bind(&loop);
3505 __ cmp(ecx, Operand(ebx));
3506 __ j(equal, &is_instance);
3507 __ cmp(Operand(ecx), Immediate(Factory::null_value()));
3508 __ j(equal, &is_not_instance);
3509 __ mov(ecx, FieldOperand(ecx, HeapObject::kMapOffset));
3510 __ mov(ecx, FieldOperand(ecx, Map::kPrototypeOffset));
3511 __ jmp(&loop);
3512
3513 __ bind(&is_instance);
3514 __ Set(eax, Immediate(0));
3515 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3516 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3517 __ ret(2 * kPointerSize);
3518
3519 __ bind(&is_not_instance);
3520 __ Set(eax, Immediate(Smi::FromInt(1)));
3521 __ mov(ecx, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
3522 __ mov(Operand::StaticArray(ecx, times_pointer_size, roots_address), eax);
3523 __ ret(2 * kPointerSize);
3524
3525 // Slow-case: Go through the JavaScript implementation.
3526 __ bind(&slow);
3527 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
3528}
3529
3530
3531int CompareStub::MinorKey() {
3532 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3533 // stubs the never NaN NaN condition is only taken into account if the
3534 // condition is equals.
3535 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
3536 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3537 return ConditionField::encode(static_cast<unsigned>(cc_))
3538 | RegisterField::encode(false) // lhs_ and rhs_ are not used
3539 | StrictField::encode(strict_)
3540 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003541 | IncludeNumberCompareField::encode(include_number_compare_)
3542 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003543}
3544
3545
3546// Unfortunately you have to run without snapshots to see most of these
3547// names in the profile since most compare stubs end up in the snapshot.
3548const char* CompareStub::GetName() {
3549 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3550
3551 if (name_ != NULL) return name_;
3552 const int kMaxNameLength = 100;
3553 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
3554 if (name_ == NULL) return "OOM";
3555
3556 const char* cc_name;
3557 switch (cc_) {
3558 case less: cc_name = "LT"; break;
3559 case greater: cc_name = "GT"; break;
3560 case less_equal: cc_name = "LE"; break;
3561 case greater_equal: cc_name = "GE"; break;
3562 case equal: cc_name = "EQ"; break;
3563 case not_equal: cc_name = "NE"; break;
3564 default: cc_name = "UnknownCondition"; break;
3565 }
3566
3567 const char* strict_name = "";
3568 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
3569 strict_name = "_STRICT";
3570 }
3571
3572 const char* never_nan_nan_name = "";
3573 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
3574 never_nan_nan_name = "_NO_NAN";
3575 }
3576
3577 const char* include_number_compare_name = "";
3578 if (!include_number_compare_) {
3579 include_number_compare_name = "_NO_NUMBER";
3580 }
3581
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003582 const char* include_smi_compare_name = "";
3583 if (!include_smi_compare_) {
3584 include_smi_compare_name = "_NO_SMI";
3585 }
3586
ricow@chromium.org65fae842010-08-25 15:26:24 +00003587 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003588 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00003589 cc_name,
3590 strict_name,
3591 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003592 include_number_compare_name,
3593 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003594 return name_;
3595}
3596
3597
3598// -------------------------------------------------------------------------
3599// StringCharCodeAtGenerator
3600
3601void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3602 Label flat_string;
3603 Label ascii_string;
3604 Label got_char_code;
3605
3606 // If the receiver is a smi trigger the non-string case.
3607 STATIC_ASSERT(kSmiTag == 0);
3608 __ test(object_, Immediate(kSmiTagMask));
3609 __ j(zero, receiver_not_string_);
3610
3611 // Fetch the instance type of the receiver into result register.
3612 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3613 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3614 // If the receiver is not a string trigger the non-string case.
3615 __ test(result_, Immediate(kIsNotStringMask));
3616 __ j(not_zero, receiver_not_string_);
3617
3618 // If the index is non-smi trigger the non-smi case.
3619 STATIC_ASSERT(kSmiTag == 0);
3620 __ test(index_, Immediate(kSmiTagMask));
3621 __ j(not_zero, &index_not_smi_);
3622
3623 // Put smi-tagged index into scratch register.
3624 __ mov(scratch_, index_);
3625 __ bind(&got_smi_index_);
3626
3627 // Check for index out of range.
3628 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
3629 __ j(above_equal, index_out_of_range_);
3630
3631 // We need special handling for non-flat strings.
3632 STATIC_ASSERT(kSeqStringTag == 0);
3633 __ test(result_, Immediate(kStringRepresentationMask));
3634 __ j(zero, &flat_string);
3635
3636 // Handle non-flat strings.
3637 __ test(result_, Immediate(kIsConsStringMask));
3638 __ j(zero, &call_runtime_);
3639
3640 // ConsString.
3641 // Check whether the right hand side is the empty string (i.e. if
3642 // this is really a flat string in a cons string). If that is not
3643 // the case we would rather go to the runtime system now to flatten
3644 // the string.
3645 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
3646 Immediate(Factory::empty_string()));
3647 __ j(not_equal, &call_runtime_);
3648 // Get the first of the two strings and load its instance type.
3649 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
3650 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3651 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3652 // If the first cons component is also non-flat, then go to runtime.
3653 STATIC_ASSERT(kSeqStringTag == 0);
3654 __ test(result_, Immediate(kStringRepresentationMask));
3655 __ j(not_zero, &call_runtime_);
3656
3657 // Check for 1-byte or 2-byte string.
3658 __ bind(&flat_string);
3659 STATIC_ASSERT(kAsciiStringTag != 0);
3660 __ test(result_, Immediate(kStringEncodingMask));
3661 __ j(not_zero, &ascii_string);
3662
3663 // 2-byte string.
3664 // Load the 2-byte character code into the result register.
3665 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
3666 __ movzx_w(result_, FieldOperand(object_,
3667 scratch_, times_1, // Scratch is smi-tagged.
3668 SeqTwoByteString::kHeaderSize));
3669 __ jmp(&got_char_code);
3670
3671 // ASCII string.
3672 // Load the byte into the result register.
3673 __ bind(&ascii_string);
3674 __ SmiUntag(scratch_);
3675 __ movzx_b(result_, FieldOperand(object_,
3676 scratch_, times_1,
3677 SeqAsciiString::kHeaderSize));
3678 __ bind(&got_char_code);
3679 __ SmiTag(result_);
3680 __ bind(&exit_);
3681}
3682
3683
3684void StringCharCodeAtGenerator::GenerateSlow(
3685 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3686 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3687
3688 // Index is not a smi.
3689 __ bind(&index_not_smi_);
3690 // If index is a heap number, try converting it to an integer.
3691 __ CheckMap(index_, Factory::heap_number_map(), index_not_number_, true);
3692 call_helper.BeforeCall(masm);
3693 __ push(object_);
3694 __ push(index_);
3695 __ push(index_); // Consumed by runtime conversion function.
3696 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3697 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3698 } else {
3699 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3700 // NumberToSmi discards numbers that are not exact integers.
3701 __ CallRuntime(Runtime::kNumberToSmi, 1);
3702 }
3703 if (!scratch_.is(eax)) {
3704 // Save the conversion result before the pop instructions below
3705 // have a chance to overwrite it.
3706 __ mov(scratch_, eax);
3707 }
3708 __ pop(index_);
3709 __ pop(object_);
3710 // Reload the instance type.
3711 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3712 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3713 call_helper.AfterCall(masm);
3714 // If index is still not a smi, it must be out of range.
3715 STATIC_ASSERT(kSmiTag == 0);
3716 __ test(scratch_, Immediate(kSmiTagMask));
3717 __ j(not_zero, index_out_of_range_);
3718 // Otherwise, return to the fast path.
3719 __ jmp(&got_smi_index_);
3720
3721 // Call runtime. We get here when the receiver is a string and the
3722 // index is a number, but the code of getting the actual character
3723 // is too complex (e.g., when the string needs to be flattened).
3724 __ bind(&call_runtime_);
3725 call_helper.BeforeCall(masm);
3726 __ push(object_);
3727 __ push(index_);
3728 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3729 if (!result_.is(eax)) {
3730 __ mov(result_, eax);
3731 }
3732 call_helper.AfterCall(masm);
3733 __ jmp(&exit_);
3734
3735 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3736}
3737
3738
3739// -------------------------------------------------------------------------
3740// StringCharFromCodeGenerator
3741
3742void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3743 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3744 STATIC_ASSERT(kSmiTag == 0);
3745 STATIC_ASSERT(kSmiShiftSize == 0);
3746 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3747 __ test(code_,
3748 Immediate(kSmiTagMask |
3749 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3750 __ j(not_zero, &slow_case_, not_taken);
3751
3752 __ Set(result_, Immediate(Factory::single_character_string_cache()));
3753 STATIC_ASSERT(kSmiTag == 0);
3754 STATIC_ASSERT(kSmiTagSize == 1);
3755 STATIC_ASSERT(kSmiShiftSize == 0);
3756 // At this point code register contains smi tagged ascii char code.
3757 __ mov(result_, FieldOperand(result_,
3758 code_, times_half_pointer_size,
3759 FixedArray::kHeaderSize));
3760 __ cmp(result_, Factory::undefined_value());
3761 __ j(equal, &slow_case_, not_taken);
3762 __ bind(&exit_);
3763}
3764
3765
3766void StringCharFromCodeGenerator::GenerateSlow(
3767 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3768 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3769
3770 __ bind(&slow_case_);
3771 call_helper.BeforeCall(masm);
3772 __ push(code_);
3773 __ CallRuntime(Runtime::kCharFromCode, 1);
3774 if (!result_.is(eax)) {
3775 __ mov(result_, eax);
3776 }
3777 call_helper.AfterCall(masm);
3778 __ jmp(&exit_);
3779
3780 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3781}
3782
3783
3784// -------------------------------------------------------------------------
3785// StringCharAtGenerator
3786
3787void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3788 char_code_at_generator_.GenerateFast(masm);
3789 char_from_code_generator_.GenerateFast(masm);
3790}
3791
3792
3793void StringCharAtGenerator::GenerateSlow(
3794 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3795 char_code_at_generator_.GenerateSlow(masm, call_helper);
3796 char_from_code_generator_.GenerateSlow(masm, call_helper);
3797}
3798
3799
3800void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003801 Label string_add_runtime, call_builtin;
3802 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003803
3804 // Load the two arguments.
3805 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
3806 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
3807
3808 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003809 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003810 __ test(eax, Immediate(kSmiTagMask));
3811 __ j(zero, &string_add_runtime);
3812 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
3813 __ j(above_equal, &string_add_runtime);
3814
3815 // First argument is a a string, test second.
3816 __ test(edx, Immediate(kSmiTagMask));
3817 __ j(zero, &string_add_runtime);
3818 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
3819 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003820 } else {
3821 // Here at least one of the arguments is definitely a string.
3822 // We convert the one that is not known to be a string.
3823 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
3824 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
3825 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
3826 &call_builtin);
3827 builtin_id = Builtins::STRING_ADD_RIGHT;
3828 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
3829 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
3830 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
3831 &call_builtin);
3832 builtin_id = Builtins::STRING_ADD_LEFT;
3833 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003834 }
3835
3836 // Both arguments are strings.
3837 // eax: first string
3838 // edx: second string
3839 // Check if either of the strings are empty. In that case return the other.
3840 Label second_not_zero_length, both_not_zero_length;
3841 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
3842 STATIC_ASSERT(kSmiTag == 0);
3843 __ test(ecx, Operand(ecx));
3844 __ j(not_zero, &second_not_zero_length);
3845 // Second string is empty, result is first string which is already in eax.
3846 __ IncrementCounter(&Counters::string_add_native, 1);
3847 __ ret(2 * kPointerSize);
3848 __ bind(&second_not_zero_length);
3849 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3850 STATIC_ASSERT(kSmiTag == 0);
3851 __ test(ebx, Operand(ebx));
3852 __ j(not_zero, &both_not_zero_length);
3853 // First string is empty, result is second string which is in edx.
3854 __ mov(eax, edx);
3855 __ IncrementCounter(&Counters::string_add_native, 1);
3856 __ ret(2 * kPointerSize);
3857
3858 // Both strings are non-empty.
3859 // eax: first string
3860 // ebx: length of first string as a smi
3861 // ecx: length of second string as a smi
3862 // edx: second string
3863 // Look at the length of the result of adding the two strings.
3864 Label string_add_flat_result, longer_than_two;
3865 __ bind(&both_not_zero_length);
3866 __ add(ebx, Operand(ecx));
3867 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
3868 // Handle exceptionally long strings in the runtime system.
3869 __ j(overflow, &string_add_runtime);
3870 // Use the runtime system when adding two one character strings, as it
3871 // contains optimizations for this specific case using the symbol table.
3872 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
3873 __ j(not_equal, &longer_than_two);
3874
3875 // Check that both strings are non-external ascii strings.
3876 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
3877 &string_add_runtime);
3878
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003879 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003880 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
3881 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
3882
3883 // Try to lookup two character string in symbol table. If it is not found
3884 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003885 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003886 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003887 masm, ebx, ecx, eax, edx, edi,
3888 &make_two_character_string_no_reload, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003889 __ IncrementCounter(&Counters::string_add_native, 1);
3890 __ ret(2 * kPointerSize);
3891
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003892 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003893 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00003894 // Reload the arguments.
3895 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
3896 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
3897 // Get the two characters forming the new string.
3898 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
3899 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
3900 __ bind(&make_two_character_string_no_reload);
3901 __ IncrementCounter(&Counters::string_add_make_two_char, 1);
3902 __ AllocateAsciiString(eax, // Result.
3903 2, // Length.
3904 edi, // Scratch 1.
3905 edx, // Scratch 2.
3906 &string_add_runtime);
3907 // Pack both characters in ebx.
3908 __ shl(ecx, kBitsPerByte);
3909 __ or_(ebx, Operand(ecx));
3910 // Set the characters in the new string.
3911 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
3912 __ IncrementCounter(&Counters::string_add_native, 1);
3913 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003914
3915 __ bind(&longer_than_two);
3916 // Check if resulting string will be flat.
3917 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
3918 __ j(below, &string_add_flat_result);
3919
3920 // If result is not supposed to be flat allocate a cons string object. If both
3921 // strings are ascii the result is an ascii cons string.
3922 Label non_ascii, allocated, ascii_data;
3923 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
3924 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
3925 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
3926 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
3927 __ and_(ecx, Operand(edi));
3928 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3929 __ test(ecx, Immediate(kAsciiStringTag));
3930 __ j(zero, &non_ascii);
3931 __ bind(&ascii_data);
3932 // Allocate an acsii cons string.
3933 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
3934 __ bind(&allocated);
3935 // Fill the fields of the cons string.
3936 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
3937 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
3938 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
3939 Immediate(String::kEmptyHashField));
3940 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
3941 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
3942 __ mov(eax, ecx);
3943 __ IncrementCounter(&Counters::string_add_native, 1);
3944 __ ret(2 * kPointerSize);
3945 __ bind(&non_ascii);
3946 // At least one of the strings is two-byte. Check whether it happens
3947 // to contain only ascii characters.
3948 // ecx: first instance type AND second instance type.
3949 // edi: second instance type.
3950 __ test(ecx, Immediate(kAsciiDataHintMask));
3951 __ j(not_zero, &ascii_data);
3952 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3953 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3954 __ xor_(edi, Operand(ecx));
3955 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
3956 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
3957 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
3958 __ j(equal, &ascii_data);
3959 // Allocate a two byte cons string.
3960 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
3961 __ jmp(&allocated);
3962
3963 // Handle creating a flat result. First check that both strings are not
3964 // external strings.
3965 // eax: first string
3966 // ebx: length of resulting flat string as a smi
3967 // edx: second string
3968 __ bind(&string_add_flat_result);
3969 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3970 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3971 __ and_(ecx, kStringRepresentationMask);
3972 __ cmp(ecx, kExternalStringTag);
3973 __ j(equal, &string_add_runtime);
3974 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3975 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
3976 __ and_(ecx, kStringRepresentationMask);
3977 __ cmp(ecx, kExternalStringTag);
3978 __ j(equal, &string_add_runtime);
3979 // Now check if both strings are ascii strings.
3980 // eax: first string
3981 // ebx: length of resulting flat string as a smi
3982 // edx: second string
3983 Label non_ascii_string_add_flat_result;
3984 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
3985 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3986 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
3987 __ j(zero, &non_ascii_string_add_flat_result);
3988 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
3989 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
3990 __ j(zero, &string_add_runtime);
3991
ricow@chromium.org65fae842010-08-25 15:26:24 +00003992 // Both strings are ascii strings. As they are short they are both flat.
3993 // ebx: length of resulting flat string as a smi
3994 __ SmiUntag(ebx);
3995 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
3996 // eax: result string
3997 __ mov(ecx, eax);
3998 // Locate first character of result.
3999 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4000 // Load first argument and locate first character.
4001 __ mov(edx, Operand(esp, 2 * kPointerSize));
4002 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4003 __ SmiUntag(edi);
4004 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4005 // eax: result string
4006 // ecx: first character of result
4007 // edx: first char of first argument
4008 // edi: length of first argument
4009 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
4010 // Load second argument and locate first character.
4011 __ mov(edx, Operand(esp, 1 * kPointerSize));
4012 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4013 __ SmiUntag(edi);
4014 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4015 // eax: result string
4016 // ecx: next character of result
4017 // edx: first char of second argument
4018 // edi: length of second argument
4019 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
4020 __ IncrementCounter(&Counters::string_add_native, 1);
4021 __ ret(2 * kPointerSize);
4022
4023 // Handle creating a flat two byte result.
4024 // eax: first string - known to be two byte
4025 // ebx: length of resulting flat string as a smi
4026 // edx: second string
4027 __ bind(&non_ascii_string_add_flat_result);
4028 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4029 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
4030 __ j(not_zero, &string_add_runtime);
4031 // Both strings are two byte strings. As they are short they are both
4032 // flat.
4033 __ SmiUntag(ebx);
4034 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
4035 // eax: result string
4036 __ mov(ecx, eax);
4037 // Locate first character of result.
4038 __ add(Operand(ecx),
4039 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4040 // Load first argument and locate first character.
4041 __ mov(edx, Operand(esp, 2 * kPointerSize));
4042 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4043 __ SmiUntag(edi);
4044 __ add(Operand(edx),
4045 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4046 // eax: result string
4047 // ecx: first character of result
4048 // edx: first char of first argument
4049 // edi: length of first argument
4050 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
4051 // Load second argument and locate first character.
4052 __ mov(edx, Operand(esp, 1 * kPointerSize));
4053 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4054 __ SmiUntag(edi);
4055 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4056 // eax: result string
4057 // ecx: next character of result
4058 // edx: first char of second argument
4059 // edi: length of second argument
4060 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
4061 __ IncrementCounter(&Counters::string_add_native, 1);
4062 __ ret(2 * kPointerSize);
4063
4064 // Just jump to runtime to add the two strings.
4065 __ bind(&string_add_runtime);
4066 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004067
4068 if (call_builtin.is_linked()) {
4069 __ bind(&call_builtin);
4070 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
4071 }
4072}
4073
4074
4075void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
4076 int stack_offset,
4077 Register arg,
4078 Register scratch1,
4079 Register scratch2,
4080 Register scratch3,
4081 Label* slow) {
4082 // First check if the argument is already a string.
4083 Label not_string, done;
4084 __ test(arg, Immediate(kSmiTagMask));
4085 __ j(zero, &not_string);
4086 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
4087 __ j(below, &done);
4088
4089 // Check the number to string cache.
4090 Label not_cached;
4091 __ bind(&not_string);
4092 // Puts the cached result into scratch1.
4093 NumberToStringStub::GenerateLookupNumberStringCache(masm,
4094 arg,
4095 scratch1,
4096 scratch2,
4097 scratch3,
4098 false,
4099 &not_cached);
4100 __ mov(arg, scratch1);
4101 __ mov(Operand(esp, stack_offset), arg);
4102 __ jmp(&done);
4103
4104 // Check if the argument is a safe string wrapper.
4105 __ bind(&not_cached);
4106 __ test(arg, Immediate(kSmiTagMask));
4107 __ j(zero, slow);
4108 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
4109 __ j(not_equal, slow);
4110 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
4111 1 << Map::kStringWrapperSafeForDefaultValueOf);
4112 __ j(zero, slow);
4113 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
4114 __ mov(Operand(esp, stack_offset), arg);
4115
4116 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004117}
4118
4119
4120void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4121 Register dest,
4122 Register src,
4123 Register count,
4124 Register scratch,
4125 bool ascii) {
4126 Label loop;
4127 __ bind(&loop);
4128 // This loop just copies one character at a time, as it is only used for very
4129 // short strings.
4130 if (ascii) {
4131 __ mov_b(scratch, Operand(src, 0));
4132 __ mov_b(Operand(dest, 0), scratch);
4133 __ add(Operand(src), Immediate(1));
4134 __ add(Operand(dest), Immediate(1));
4135 } else {
4136 __ mov_w(scratch, Operand(src, 0));
4137 __ mov_w(Operand(dest, 0), scratch);
4138 __ add(Operand(src), Immediate(2));
4139 __ add(Operand(dest), Immediate(2));
4140 }
4141 __ sub(Operand(count), Immediate(1));
4142 __ j(not_zero, &loop);
4143}
4144
4145
4146void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
4147 Register dest,
4148 Register src,
4149 Register count,
4150 Register scratch,
4151 bool ascii) {
4152 // Copy characters using rep movs of doublewords.
4153 // The destination is aligned on a 4 byte boundary because we are
4154 // copying to the beginning of a newly allocated string.
4155 ASSERT(dest.is(edi)); // rep movs destination
4156 ASSERT(src.is(esi)); // rep movs source
4157 ASSERT(count.is(ecx)); // rep movs count
4158 ASSERT(!scratch.is(dest));
4159 ASSERT(!scratch.is(src));
4160 ASSERT(!scratch.is(count));
4161
4162 // Nothing to do for zero characters.
4163 Label done;
4164 __ test(count, Operand(count));
4165 __ j(zero, &done);
4166
4167 // Make count the number of bytes to copy.
4168 if (!ascii) {
4169 __ shl(count, 1);
4170 }
4171
4172 // Don't enter the rep movs if there are less than 4 bytes to copy.
4173 Label last_bytes;
4174 __ test(count, Immediate(~3));
4175 __ j(zero, &last_bytes);
4176
4177 // Copy from edi to esi using rep movs instruction.
4178 __ mov(scratch, count);
4179 __ sar(count, 2); // Number of doublewords to copy.
4180 __ cld();
4181 __ rep_movs();
4182
4183 // Find number of bytes left.
4184 __ mov(count, scratch);
4185 __ and_(count, 3);
4186
4187 // Check if there are more bytes to copy.
4188 __ bind(&last_bytes);
4189 __ test(count, Operand(count));
4190 __ j(zero, &done);
4191
4192 // Copy remaining characters.
4193 Label loop;
4194 __ bind(&loop);
4195 __ mov_b(scratch, Operand(src, 0));
4196 __ mov_b(Operand(dest, 0), scratch);
4197 __ add(Operand(src), Immediate(1));
4198 __ add(Operand(dest), Immediate(1));
4199 __ sub(Operand(count), Immediate(1));
4200 __ j(not_zero, &loop);
4201
4202 __ bind(&done);
4203}
4204
4205
4206void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4207 Register c1,
4208 Register c2,
4209 Register scratch1,
4210 Register scratch2,
4211 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004212 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00004213 Label* not_found) {
4214 // Register scratch3 is the general scratch register in this function.
4215 Register scratch = scratch3;
4216
4217 // Make sure that both characters are not digits as such strings has a
4218 // different hash algorithm. Don't try to look for these in the symbol table.
4219 Label not_array_index;
4220 __ mov(scratch, c1);
4221 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
4222 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
4223 __ j(above, &not_array_index);
4224 __ mov(scratch, c2);
4225 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
4226 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004227 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004228
4229 __ bind(&not_array_index);
4230 // Calculate the two character string hash.
4231 Register hash = scratch1;
4232 GenerateHashInit(masm, hash, c1, scratch);
4233 GenerateHashAddCharacter(masm, hash, c2, scratch);
4234 GenerateHashGetHash(masm, hash, scratch);
4235
4236 // Collect the two characters in a register.
4237 Register chars = c1;
4238 __ shl(c2, kBitsPerByte);
4239 __ or_(chars, Operand(c2));
4240
4241 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4242 // hash: hash of two character string.
4243
4244 // Load the symbol table.
4245 Register symbol_table = c2;
4246 ExternalReference roots_address = ExternalReference::roots_address();
4247 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
4248 __ mov(symbol_table,
4249 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4250
4251 // Calculate capacity mask from the symbol table capacity.
4252 Register mask = scratch2;
4253 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4254 __ SmiUntag(mask);
4255 __ sub(Operand(mask), Immediate(1));
4256
4257 // Registers
4258 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4259 // hash: hash of two character string
4260 // symbol_table: symbol table
4261 // mask: capacity mask
4262 // scratch: -
4263
4264 // Perform a number of probes in the symbol table.
4265 static const int kProbes = 4;
4266 Label found_in_symbol_table;
4267 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
4268 for (int i = 0; i < kProbes; i++) {
4269 // Calculate entry in symbol table.
4270 __ mov(scratch, hash);
4271 if (i > 0) {
4272 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
4273 }
4274 __ and_(scratch, Operand(mask));
4275
4276 // Load the entry from the symbol table.
4277 Register candidate = scratch; // Scratch register contains candidate.
4278 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4279 __ mov(candidate,
4280 FieldOperand(symbol_table,
4281 scratch,
4282 times_pointer_size,
4283 SymbolTable::kElementsStartOffset));
4284
4285 // If entry is undefined no string with this hash can be found.
4286 __ cmp(candidate, Factory::undefined_value());
4287 __ j(equal, not_found);
4288
4289 // If length is not 2 the string is not a candidate.
4290 __ cmp(FieldOperand(candidate, String::kLengthOffset),
4291 Immediate(Smi::FromInt(2)));
4292 __ j(not_equal, &next_probe[i]);
4293
4294 // As we are out of registers save the mask on the stack and use that
4295 // register as a temporary.
4296 __ push(mask);
4297 Register temp = mask;
4298
4299 // Check that the candidate is a non-external ascii string.
4300 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
4301 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
4302 __ JumpIfInstanceTypeIsNotSequentialAscii(
4303 temp, temp, &next_probe_pop_mask[i]);
4304
4305 // Check if the two characters match.
4306 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4307 __ and_(temp, 0x0000ffff);
4308 __ cmp(chars, Operand(temp));
4309 __ j(equal, &found_in_symbol_table);
4310 __ bind(&next_probe_pop_mask[i]);
4311 __ pop(mask);
4312 __ bind(&next_probe[i]);
4313 }
4314
4315 // No matching 2 character string found by probing.
4316 __ jmp(not_found);
4317
4318 // Scratch register contains result when we fall through to here.
4319 Register result = scratch;
4320 __ bind(&found_in_symbol_table);
4321 __ pop(mask); // Pop saved mask from the stack.
4322 if (!result.is(eax)) {
4323 __ mov(eax, result);
4324 }
4325}
4326
4327
4328void StringHelper::GenerateHashInit(MacroAssembler* masm,
4329 Register hash,
4330 Register character,
4331 Register scratch) {
4332 // hash = character + (character << 10);
4333 __ mov(hash, character);
4334 __ shl(hash, 10);
4335 __ add(hash, Operand(character));
4336 // hash ^= hash >> 6;
4337 __ mov(scratch, hash);
4338 __ sar(scratch, 6);
4339 __ xor_(hash, Operand(scratch));
4340}
4341
4342
4343void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4344 Register hash,
4345 Register character,
4346 Register scratch) {
4347 // hash += character;
4348 __ add(hash, Operand(character));
4349 // hash += hash << 10;
4350 __ mov(scratch, hash);
4351 __ shl(scratch, 10);
4352 __ add(hash, Operand(scratch));
4353 // hash ^= hash >> 6;
4354 __ mov(scratch, hash);
4355 __ sar(scratch, 6);
4356 __ xor_(hash, Operand(scratch));
4357}
4358
4359
4360void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4361 Register hash,
4362 Register scratch) {
4363 // hash += hash << 3;
4364 __ mov(scratch, hash);
4365 __ shl(scratch, 3);
4366 __ add(hash, Operand(scratch));
4367 // hash ^= hash >> 11;
4368 __ mov(scratch, hash);
4369 __ sar(scratch, 11);
4370 __ xor_(hash, Operand(scratch));
4371 // hash += hash << 15;
4372 __ mov(scratch, hash);
4373 __ shl(scratch, 15);
4374 __ add(hash, Operand(scratch));
4375
4376 // if (hash == 0) hash = 27;
4377 Label hash_not_zero;
4378 __ test(hash, Operand(hash));
4379 __ j(not_zero, &hash_not_zero);
4380 __ mov(hash, Immediate(27));
4381 __ bind(&hash_not_zero);
4382}
4383
4384
4385void SubStringStub::Generate(MacroAssembler* masm) {
4386 Label runtime;
4387
4388 // Stack frame on entry.
4389 // esp[0]: return address
4390 // esp[4]: to
4391 // esp[8]: from
4392 // esp[12]: string
4393
4394 // Make sure first argument is a string.
4395 __ mov(eax, Operand(esp, 3 * kPointerSize));
4396 STATIC_ASSERT(kSmiTag == 0);
4397 __ test(eax, Immediate(kSmiTagMask));
4398 __ j(zero, &runtime);
4399 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
4400 __ j(NegateCondition(is_string), &runtime);
4401
4402 // eax: string
4403 // ebx: instance type
4404
4405 // Calculate length of sub string using the smi values.
4406 Label result_longer_than_two;
4407 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
4408 __ test(ecx, Immediate(kSmiTagMask));
4409 __ j(not_zero, &runtime);
4410 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
4411 __ test(edx, Immediate(kSmiTagMask));
4412 __ j(not_zero, &runtime);
4413 __ sub(ecx, Operand(edx));
4414 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
4415 Label return_eax;
4416 __ j(equal, &return_eax);
4417 // Special handling of sub-strings of length 1 and 2. One character strings
4418 // are handled in the runtime system (looked up in the single character
4419 // cache). Two character strings are looked for in the symbol cache.
4420 __ SmiUntag(ecx); // Result length is no longer smi.
4421 __ cmp(ecx, 2);
4422 __ j(greater, &result_longer_than_two);
4423 __ j(less, &runtime);
4424
4425 // Sub string of length 2 requested.
4426 // eax: string
4427 // ebx: instance type
4428 // ecx: sub string length (value is 2)
4429 // edx: from index (smi)
4430 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
4431
4432 // Get the two characters forming the sub string.
4433 __ SmiUntag(edx); // From index is no longer smi.
4434 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
4435 __ movzx_b(ecx,
4436 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
4437
4438 // Try to lookup two character string in symbol table.
4439 Label make_two_character_string;
4440 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004441 masm, ebx, ecx, eax, edx, edi,
4442 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004443 __ ret(3 * kPointerSize);
4444
4445 __ bind(&make_two_character_string);
4446 // Setup registers for allocating the two character string.
4447 __ mov(eax, Operand(esp, 3 * kPointerSize));
4448 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
4449 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
4450 __ Set(ecx, Immediate(2));
4451
4452 __ bind(&result_longer_than_two);
4453 // eax: string
4454 // ebx: instance type
4455 // ecx: result string length
4456 // Check for flat ascii string
4457 Label non_ascii_flat;
4458 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
4459
4460 // Allocate the result.
4461 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
4462
4463 // eax: result string
4464 // ecx: result string length
4465 __ mov(edx, esi); // esi used by following code.
4466 // Locate first character of result.
4467 __ mov(edi, eax);
4468 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4469 // Load string argument and locate character of sub string start.
4470 __ mov(esi, Operand(esp, 3 * kPointerSize));
4471 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4472 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
4473 __ SmiUntag(ebx);
4474 __ add(esi, Operand(ebx));
4475
4476 // eax: result string
4477 // ecx: result length
4478 // edx: original value of esi
4479 // edi: first character of result
4480 // esi: character of sub string start
4481 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
4482 __ mov(esi, edx); // Restore esi.
4483 __ IncrementCounter(&Counters::sub_string_native, 1);
4484 __ ret(3 * kPointerSize);
4485
4486 __ bind(&non_ascii_flat);
4487 // eax: string
4488 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
4489 // ecx: result string length
4490 // Check for flat two byte string
4491 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
4492 __ j(not_equal, &runtime);
4493
4494 // Allocate the result.
4495 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
4496
4497 // eax: result string
4498 // ecx: result string length
4499 __ mov(edx, esi); // esi used by following code.
4500 // Locate first character of result.
4501 __ mov(edi, eax);
4502 __ add(Operand(edi),
4503 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4504 // Load string argument and locate character of sub string start.
4505 __ mov(esi, Operand(esp, 3 * kPointerSize));
4506 __ add(Operand(esi),
4507 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4508 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
4509 // As from is a smi it is 2 times the value which matches the size of a two
4510 // byte character.
4511 STATIC_ASSERT(kSmiTag == 0);
4512 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4513 __ add(esi, Operand(ebx));
4514
4515 // eax: result string
4516 // ecx: result length
4517 // edx: original value of esi
4518 // edi: first character of result
4519 // esi: character of sub string start
4520 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
4521 __ mov(esi, edx); // Restore esi.
4522
4523 __ bind(&return_eax);
4524 __ IncrementCounter(&Counters::sub_string_native, 1);
4525 __ ret(3 * kPointerSize);
4526
4527 // Just jump to runtime to create the sub string.
4528 __ bind(&runtime);
4529 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4530}
4531
4532
4533void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4534 Register left,
4535 Register right,
4536 Register scratch1,
4537 Register scratch2,
4538 Register scratch3) {
4539 Label result_not_equal;
4540 Label result_greater;
4541 Label compare_lengths;
4542
4543 __ IncrementCounter(&Counters::string_compare_native, 1);
4544
4545 // Find minimum length.
4546 Label left_shorter;
4547 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
4548 __ mov(scratch3, scratch1);
4549 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
4550
4551 Register length_delta = scratch3;
4552
4553 __ j(less_equal, &left_shorter);
4554 // Right string is shorter. Change scratch1 to be length of right string.
4555 __ sub(scratch1, Operand(length_delta));
4556 __ bind(&left_shorter);
4557
4558 Register min_length = scratch1;
4559
4560 // If either length is zero, just compare lengths.
4561 __ test(min_length, Operand(min_length));
4562 __ j(zero, &compare_lengths);
4563
4564 // Change index to run from -min_length to -1 by adding min_length
4565 // to string start. This means that loop ends when index reaches zero,
4566 // which doesn't need an additional compare.
4567 __ SmiUntag(min_length);
4568 __ lea(left,
4569 FieldOperand(left,
4570 min_length, times_1,
4571 SeqAsciiString::kHeaderSize));
4572 __ lea(right,
4573 FieldOperand(right,
4574 min_length, times_1,
4575 SeqAsciiString::kHeaderSize));
4576 __ neg(min_length);
4577
4578 Register index = min_length; // index = -min_length;
4579
4580 {
4581 // Compare loop.
4582 Label loop;
4583 __ bind(&loop);
4584 // Compare characters.
4585 __ mov_b(scratch2, Operand(left, index, times_1, 0));
4586 __ cmpb(scratch2, Operand(right, index, times_1, 0));
4587 __ j(not_equal, &result_not_equal);
4588 __ add(Operand(index), Immediate(1));
4589 __ j(not_zero, &loop);
4590 }
4591
4592 // Compare lengths - strings up to min-length are equal.
4593 __ bind(&compare_lengths);
4594 __ test(length_delta, Operand(length_delta));
4595 __ j(not_zero, &result_not_equal);
4596
4597 // Result is EQUAL.
4598 STATIC_ASSERT(EQUAL == 0);
4599 STATIC_ASSERT(kSmiTag == 0);
4600 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4601 __ ret(0);
4602
4603 __ bind(&result_not_equal);
4604 __ j(greater, &result_greater);
4605
4606 // Result is LESS.
4607 __ Set(eax, Immediate(Smi::FromInt(LESS)));
4608 __ ret(0);
4609
4610 // Result is GREATER.
4611 __ bind(&result_greater);
4612 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
4613 __ ret(0);
4614}
4615
4616
4617void StringCompareStub::Generate(MacroAssembler* masm) {
4618 Label runtime;
4619
4620 // Stack frame on entry.
4621 // esp[0]: return address
4622 // esp[4]: right string
4623 // esp[8]: left string
4624
4625 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
4626 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
4627
4628 Label not_same;
4629 __ cmp(edx, Operand(eax));
4630 __ j(not_equal, &not_same);
4631 STATIC_ASSERT(EQUAL == 0);
4632 STATIC_ASSERT(kSmiTag == 0);
4633 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4634 __ IncrementCounter(&Counters::string_compare_native, 1);
4635 __ ret(2 * kPointerSize);
4636
4637 __ bind(&not_same);
4638
4639 // Check that both objects are sequential ascii strings.
4640 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
4641
4642 // Compare flat ascii strings.
4643 // Drop arguments from the stack.
4644 __ pop(ecx);
4645 __ add(Operand(esp), Immediate(2 * kPointerSize));
4646 __ push(ecx);
4647 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
4648
4649 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4650 // tagged as a small integer.
4651 __ bind(&runtime);
4652 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4653}
4654
4655#undef __
4656
4657} } // namespace v8::internal
4658
4659#endif // V8_TARGET_ARCH_IA32