blob: eaff1b8fad1dd608b57950067808ebabe28ea183 [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002// 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"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000035#include "isolate.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
37
38namespace v8 {
39namespace internal {
40
41#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +000042
43void ToNumberStub::Generate(MacroAssembler* masm) {
44 // The ToNumber stub takes one argument in eax.
45 NearLabel check_heap_number, call_builtin;
46 __ test(eax, Immediate(kSmiTagMask));
47 __ j(not_zero, &check_heap_number);
48 __ ret(0);
49
50 __ bind(&check_heap_number);
51 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052 __ cmp(Operand(ebx), Immediate(FACTORY->heap_number_map()));
whesse@chromium.org7a392b32011-01-31 11:30:36 +000053 __ j(not_equal, &call_builtin);
54 __ ret(0);
55
56 __ bind(&call_builtin);
57 __ pop(ecx); // Pop return address.
58 __ push(eax);
59 __ push(ecx); // Push return address.
60 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
61}
62
63
ricow@chromium.org65fae842010-08-25 15:26:24 +000064void FastNewClosureStub::Generate(MacroAssembler* masm) {
65 // Create a new closure from the given function info in new
66 // space. Set the context to the current context in esi.
67 Label gc;
68 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
69
70 // Get the function info from the stack.
71 __ mov(edx, Operand(esp, 1 * kPointerSize));
72
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000073 int map_index = strict_mode_ == kStrictMode
74 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
75 : Context::FUNCTION_MAP_INDEX;
76
ricow@chromium.org65fae842010-08-25 15:26:24 +000077 // Compute the function map in the current global context and set that
78 // as the map of the allocated object.
79 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
80 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000081 __ mov(ecx, Operand(ecx, Context::SlotOffset(map_index)));
ricow@chromium.org65fae842010-08-25 15:26:24 +000082 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
83
84 // Initialize the rest of the function. We don't have to update the
85 // write barrier because the allocated object is in new space.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 __ mov(ebx, Immediate(FACTORY->empty_fixed_array()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000087 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
88 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
89 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000090 Immediate(FACTORY->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000091 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
92 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
93 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000094 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000095 Immediate(FACTORY->undefined_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000096
97 // Initialize the code pointer in the function to be the one
98 // found in the shared function info object.
99 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
100 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
101 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
102
103 // Return and remove the on-stack parameter.
104 __ ret(1 * kPointerSize);
105
106 // Create a new closure through the slower runtime call.
107 __ bind(&gc);
108 __ pop(ecx); // Temporarily remove return address.
109 __ pop(edx);
110 __ push(esi);
111 __ push(edx);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000112 __ push(Immediate(FACTORY->false_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000113 __ push(ecx); // Restore return address.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000114 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000115}
116
117
118void FastNewContextStub::Generate(MacroAssembler* masm) {
119 // Try to allocate the context in new space.
120 Label gc;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000121 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
122 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000123 eax, ebx, ecx, &gc, TAG_OBJECT);
124
125 // Get the function from the stack.
126 __ mov(ecx, Operand(esp, 1 * kPointerSize));
127
128 // Setup the object header.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000129 __ mov(FieldOperand(eax, HeapObject::kMapOffset), FACTORY->context_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000130 __ mov(FieldOperand(eax, Context::kLengthOffset),
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000131 Immediate(Smi::FromInt(length)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000132
133 // Setup the fixed slots.
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000134 __ Set(ebx, Immediate(0)); // Set to NULL.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
136 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
137 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
138 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
139
140 // Copy the global object from the surrounding context. We go through the
141 // context in the function (ecx) to match the allocation behavior we have
142 // in the runtime system (see Heap::AllocateFunctionContext).
143 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
144 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
145 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
146
147 // Initialize the rest of the slots to undefined.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000148 __ mov(ebx, FACTORY->undefined_value());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000149 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000150 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
151 }
152
153 // Return and remove the on-stack parameter.
154 __ mov(esi, Operand(eax));
155 __ ret(1 * kPointerSize);
156
157 // Need to collect. Call into runtime system.
158 __ bind(&gc);
159 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
160}
161
162
163void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
164 // Stack layout on entry:
165 //
166 // [esp + kPointerSize]: constant elements.
167 // [esp + (2 * kPointerSize)]: literal index.
168 // [esp + (3 * kPointerSize)]: literals array.
169
170 // All sizes here are multiples of kPointerSize.
171 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
172 int size = JSArray::kSize + elements_size;
173
174 // Load boilerplate object into ecx and check if we need to create a
175 // boilerplate.
176 Label slow_case;
177 __ mov(ecx, Operand(esp, 3 * kPointerSize));
178 __ mov(eax, Operand(esp, 2 * kPointerSize));
179 STATIC_ASSERT(kPointerSize == 4);
180 STATIC_ASSERT(kSmiTagSize == 1);
181 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000182 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
183 FixedArray::kHeaderSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000184 __ cmp(ecx, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000185 __ j(equal, &slow_case);
186
187 if (FLAG_debug_code) {
188 const char* message;
189 Handle<Map> expected_map;
190 if (mode_ == CLONE_ELEMENTS) {
191 message = "Expected (writable) fixed array";
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 expected_map = FACTORY->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000193 } else {
194 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
195 message = "Expected copy-on-write fixed array";
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000196 expected_map = FACTORY->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000197 }
198 __ push(ecx);
199 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
200 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
201 __ Assert(equal, message);
202 __ pop(ecx);
203 }
204
205 // Allocate both the JS array and the elements array in one big
206 // allocation. This avoids multiple limit checks.
207 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
208
209 // Copy the JS array part.
210 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
211 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
212 __ mov(ebx, FieldOperand(ecx, i));
213 __ mov(FieldOperand(eax, i), ebx);
214 }
215 }
216
217 if (length_ > 0) {
218 // Get hold of the elements array of the boilerplate and setup the
219 // elements pointer in the resulting object.
220 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
221 __ lea(edx, Operand(eax, JSArray::kSize));
222 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
223
224 // Copy the elements array.
225 for (int i = 0; i < elements_size; i += kPointerSize) {
226 __ mov(ebx, FieldOperand(ecx, i));
227 __ mov(FieldOperand(edx, i), ebx);
228 }
229 }
230
231 // Return and remove the on-stack parameters.
232 __ ret(3 * kPointerSize);
233
234 __ bind(&slow_case);
235 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
236}
237
238
239// NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
240void ToBooleanStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000241 NearLabel false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000242 __ mov(eax, Operand(esp, 1 * kPointerSize));
243
244 // 'null' => false.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000245 __ cmp(eax, FACTORY->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000246 __ j(equal, &false_result);
247
248 // Get the map and type of the heap object.
249 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
250 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
251
252 // Undetectable => false.
253 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
254 1 << Map::kIsUndetectable);
255 __ j(not_zero, &false_result);
256
257 // JavaScript object => true.
258 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
259 __ j(above_equal, &true_result);
260
261 // String value => false iff empty.
262 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
263 __ j(above_equal, &not_string);
264 STATIC_ASSERT(kSmiTag == 0);
265 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
266 __ j(zero, &false_result);
267 __ jmp(&true_result);
268
269 __ bind(&not_string);
270 // HeapNumber => false iff +0, -0, or NaN.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000271 __ cmp(edx, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000272 __ j(not_equal, &true_result);
273 __ fldz();
274 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
275 __ FCmp();
276 __ j(zero, &false_result);
277 // Fall through to |true_result|.
278
279 // Return 1/0 for true/false in eax.
280 __ bind(&true_result);
281 __ mov(eax, 1);
282 __ ret(1 * kPointerSize);
283 __ bind(&false_result);
284 __ mov(eax, 0);
285 __ ret(1 * kPointerSize);
286}
287
288
289const char* GenericBinaryOpStub::GetName() {
290 if (name_ != NULL) return name_;
291 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000292 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
293 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000294 if (name_ == NULL) return "OOM";
295 const char* op_name = Token::Name(op_);
296 const char* overwrite_name;
297 switch (mode_) {
298 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
299 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
300 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
301 default: overwrite_name = "UnknownOverwrite"; break;
302 }
303
304 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
305 "GenericBinaryOpStub_%s_%s%s_%s%s_%s_%s",
306 op_name,
307 overwrite_name,
308 (flags_ & NO_SMI_CODE_IN_STUB) ? "_NoSmiInStub" : "",
309 args_in_registers_ ? "RegArgs" : "StackArgs",
310 args_reversed_ ? "_R" : "",
311 static_operands_type_.ToString(),
312 BinaryOpIC::GetName(runtime_operands_type_));
313 return name_;
314}
315
316
317void GenericBinaryOpStub::GenerateCall(
318 MacroAssembler* masm,
319 Register left,
320 Register right) {
321 if (!ArgsInRegistersSupported()) {
322 // Pass arguments on the stack.
323 __ push(left);
324 __ push(right);
325 } else {
326 // The calling convention with registers is left in edx and right in eax.
327 Register left_arg = edx;
328 Register right_arg = eax;
329 if (!(left.is(left_arg) && right.is(right_arg))) {
330 if (left.is(right_arg) && right.is(left_arg)) {
331 if (IsOperationCommutative()) {
332 SetArgsReversed();
333 } else {
334 __ xchg(left, right);
335 }
336 } else if (left.is(left_arg)) {
337 __ mov(right_arg, right);
338 } else if (right.is(right_arg)) {
339 __ mov(left_arg, left);
340 } else if (left.is(right_arg)) {
341 if (IsOperationCommutative()) {
342 __ mov(left_arg, right);
343 SetArgsReversed();
344 } else {
345 // Order of moves important to avoid destroying left argument.
346 __ mov(left_arg, left);
347 __ mov(right_arg, right);
348 }
349 } else if (right.is(left_arg)) {
350 if (IsOperationCommutative()) {
351 __ mov(right_arg, left);
352 SetArgsReversed();
353 } else {
354 // Order of moves important to avoid destroying right argument.
355 __ mov(right_arg, right);
356 __ mov(left_arg, left);
357 }
358 } else {
359 // Order of moves is not important.
360 __ mov(left_arg, left);
361 __ mov(right_arg, right);
362 }
363 }
364
365 // Update flags to indicate that arguments are in registers.
366 SetArgsInRegisters();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000367 __ IncrementCounter(COUNTERS->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000368 }
369
370 // Call the stub.
371 __ CallStub(this);
372}
373
374
375void GenericBinaryOpStub::GenerateCall(
376 MacroAssembler* masm,
377 Register left,
378 Smi* right) {
379 if (!ArgsInRegistersSupported()) {
380 // Pass arguments on the stack.
381 __ push(left);
382 __ push(Immediate(right));
383 } else {
384 // The calling convention with registers is left in edx and right in eax.
385 Register left_arg = edx;
386 Register right_arg = eax;
387 if (left.is(left_arg)) {
388 __ mov(right_arg, Immediate(right));
389 } else if (left.is(right_arg) && IsOperationCommutative()) {
390 __ mov(left_arg, Immediate(right));
391 SetArgsReversed();
392 } else {
393 // For non-commutative operations, left and right_arg might be
394 // the same register. Therefore, the order of the moves is
395 // important here in order to not overwrite left before moving
396 // it to left_arg.
397 __ mov(left_arg, left);
398 __ mov(right_arg, Immediate(right));
399 }
400
401 // Update flags to indicate that arguments are in registers.
402 SetArgsInRegisters();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000403 __ IncrementCounter(COUNTERS->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000404 }
405
406 // Call the stub.
407 __ CallStub(this);
408}
409
410
411void GenericBinaryOpStub::GenerateCall(
412 MacroAssembler* masm,
413 Smi* left,
414 Register right) {
415 if (!ArgsInRegistersSupported()) {
416 // Pass arguments on the stack.
417 __ push(Immediate(left));
418 __ push(right);
419 } else {
420 // The calling convention with registers is left in edx and right in eax.
421 Register left_arg = edx;
422 Register right_arg = eax;
423 if (right.is(right_arg)) {
424 __ mov(left_arg, Immediate(left));
425 } else if (right.is(left_arg) && IsOperationCommutative()) {
426 __ mov(right_arg, Immediate(left));
427 SetArgsReversed();
428 } else {
429 // For non-commutative operations, right and left_arg might be
430 // the same register. Therefore, the order of the moves is
431 // important here in order to not overwrite right before moving
432 // it to right_arg.
433 __ mov(right_arg, right);
434 __ mov(left_arg, Immediate(left));
435 }
436 // Update flags to indicate that arguments are in registers.
437 SetArgsInRegisters();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000438 __ IncrementCounter(COUNTERS->generic_binary_stub_calls_regs(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000439 }
440
441 // Call the stub.
442 __ CallStub(this);
443}
444
445
446class FloatingPointHelper : public AllStatic {
447 public:
448
449 enum ArgLocation {
450 ARGS_ON_STACK,
451 ARGS_IN_REGISTERS
452 };
453
454 // Code pattern for loading a floating point value. Input value must
455 // be either a smi or a heap number object (fp value). Requirements:
456 // operand in register number. Returns operand as floating point number
457 // on FPU stack.
458 static void LoadFloatOperand(MacroAssembler* masm, Register number);
459
460 // Code pattern for loading floating point values. Input values must
461 // be either smi or heap number objects (fp values). Requirements:
462 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
463 // Returns operands as floating point numbers on FPU stack.
464 static void LoadFloatOperands(MacroAssembler* masm,
465 Register scratch,
466 ArgLocation arg_location = ARGS_ON_STACK);
467
468 // Similar to LoadFloatOperand but assumes that both operands are smis.
469 // Expects operands in edx, eax.
470 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
471
472 // Test if operands are smi or number objects (fp). Requirements:
473 // operand_1 in eax, operand_2 in edx; falls through on float
474 // operands, jumps to the non_float label otherwise.
475 static void CheckFloatOperands(MacroAssembler* masm,
476 Label* non_float,
477 Register scratch);
478
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000479 // Checks that the two floating point numbers on top of the FPU stack
480 // have int32 values.
481 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
482 Label* non_int32);
483
ricow@chromium.org65fae842010-08-25 15:26:24 +0000484 // Takes the operands in edx and eax and loads them as integers in eax
485 // and ecx.
486 static void LoadAsIntegers(MacroAssembler* masm,
487 TypeInfo type_info,
488 bool use_sse3,
489 Label* operand_conversion_failure);
490 static void LoadNumbersAsIntegers(MacroAssembler* masm,
491 TypeInfo type_info,
492 bool use_sse3,
493 Label* operand_conversion_failure);
494 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
495 bool use_sse3,
496 Label* operand_conversion_failure);
497
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000498 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
499 // operands are pushed on the stack, and that their conversions to int32
500 // are in eax and ecx. Checks that the original numbers were in the int32
501 // range.
502 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
503 bool use_sse3,
504 Label* not_int32);
505
506 // Assumes that operands are smis or heap numbers and loads them
507 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000508 // Leaves operands unchanged.
509 static void LoadSSE2Operands(MacroAssembler* masm);
510
511 // Test if operands are numbers (smi or HeapNumber objects), and load
512 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
513 // either operand is not a number. Operands are in edx and eax.
514 // Leaves operands unchanged.
515 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
516
517 // Similar to LoadSSE2Operands but assumes that both operands are smis.
518 // Expects operands in edx, eax.
519 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000520
521 // Checks that the two floating point numbers loaded into xmm0 and xmm1
522 // have int32 values.
523 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
524 Label* non_int32,
525 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000526};
527
528
529void GenericBinaryOpStub::GenerateSmiCode(MacroAssembler* masm, Label* slow) {
530 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
531 // dividend in eax and edx free for the division. Use eax, ebx for those.
532 Comment load_comment(masm, "-- Load arguments");
533 Register left = edx;
534 Register right = eax;
535 if (op_ == Token::DIV || op_ == Token::MOD) {
536 left = eax;
537 right = ebx;
538 if (HasArgsInRegisters()) {
539 __ mov(ebx, eax);
540 __ mov(eax, edx);
541 }
542 }
543 if (!HasArgsInRegisters()) {
544 __ mov(right, Operand(esp, 1 * kPointerSize));
545 __ mov(left, Operand(esp, 2 * kPointerSize));
546 }
547
548 if (static_operands_type_.IsSmi()) {
549 if (FLAG_debug_code) {
550 __ AbortIfNotSmi(left);
551 __ AbortIfNotSmi(right);
552 }
553 if (op_ == Token::BIT_OR) {
554 __ or_(right, Operand(left));
555 GenerateReturn(masm);
556 return;
557 } else if (op_ == Token::BIT_AND) {
558 __ and_(right, Operand(left));
559 GenerateReturn(masm);
560 return;
561 } else if (op_ == Token::BIT_XOR) {
562 __ xor_(right, Operand(left));
563 GenerateReturn(masm);
564 return;
565 }
566 }
567
568 // 2. Prepare the smi check of both operands by oring them together.
569 Comment smi_check_comment(masm, "-- Smi check arguments");
570 Label not_smis;
571 Register combined = ecx;
572 ASSERT(!left.is(combined) && !right.is(combined));
573 switch (op_) {
574 case Token::BIT_OR:
575 // Perform the operation into eax and smi check the result. Preserve
576 // eax in case the result is not a smi.
577 ASSERT(!left.is(ecx) && !right.is(ecx));
578 __ mov(ecx, right);
579 __ or_(right, Operand(left)); // Bitwise or is commutative.
580 combined = right;
581 break;
582
583 case Token::BIT_XOR:
584 case Token::BIT_AND:
585 case Token::ADD:
586 case Token::SUB:
587 case Token::MUL:
588 case Token::DIV:
589 case Token::MOD:
590 __ mov(combined, right);
591 __ or_(combined, Operand(left));
592 break;
593
594 case Token::SHL:
595 case Token::SAR:
596 case Token::SHR:
597 // Move the right operand into ecx for the shift operation, use eax
598 // for the smi check register.
599 ASSERT(!left.is(ecx) && !right.is(ecx));
600 __ mov(ecx, right);
601 __ or_(right, Operand(left));
602 combined = right;
603 break;
604
605 default:
606 break;
607 }
608
609 // 3. Perform the smi check of the operands.
610 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
611 __ test(combined, Immediate(kSmiTagMask));
612 __ j(not_zero, &not_smis, not_taken);
613
614 // 4. Operands are both smis, perform the operation leaving the result in
615 // eax and check the result if necessary.
616 Comment perform_smi(masm, "-- Perform smi operation");
617 Label use_fp_on_smis;
618 switch (op_) {
619 case Token::BIT_OR:
620 // Nothing to do.
621 break;
622
623 case Token::BIT_XOR:
624 ASSERT(right.is(eax));
625 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
626 break;
627
628 case Token::BIT_AND:
629 ASSERT(right.is(eax));
630 __ and_(right, Operand(left)); // Bitwise and is commutative.
631 break;
632
633 case Token::SHL:
634 // Remove tags from operands (but keep sign).
635 __ SmiUntag(left);
636 __ SmiUntag(ecx);
637 // Perform the operation.
638 __ shl_cl(left);
639 // Check that the *signed* result fits in a smi.
640 __ cmp(left, 0xc0000000);
641 __ j(sign, &use_fp_on_smis, not_taken);
642 // Tag the result and store it in register eax.
643 __ SmiTag(left);
644 __ mov(eax, left);
645 break;
646
647 case Token::SAR:
648 // Remove tags from operands (but keep sign).
649 __ SmiUntag(left);
650 __ SmiUntag(ecx);
651 // Perform the operation.
652 __ sar_cl(left);
653 // Tag the result and store it in register eax.
654 __ SmiTag(left);
655 __ mov(eax, left);
656 break;
657
658 case Token::SHR:
659 // Remove tags from operands (but keep sign).
660 __ SmiUntag(left);
661 __ SmiUntag(ecx);
662 // Perform the operation.
663 __ shr_cl(left);
664 // Check that the *unsigned* result fits in a smi.
665 // Neither of the two high-order bits can be set:
666 // - 0x80000000: high bit would be lost when smi tagging.
667 // - 0x40000000: this number would convert to negative when
668 // Smi tagging these two cases can only happen with shifts
669 // by 0 or 1 when handed a valid smi.
670 __ test(left, Immediate(0xc0000000));
671 __ j(not_zero, slow, not_taken);
672 // Tag the result and store it in register eax.
673 __ SmiTag(left);
674 __ mov(eax, left);
675 break;
676
677 case Token::ADD:
678 ASSERT(right.is(eax));
679 __ add(right, Operand(left)); // Addition is commutative.
680 __ j(overflow, &use_fp_on_smis, not_taken);
681 break;
682
683 case Token::SUB:
684 __ sub(left, Operand(right));
685 __ j(overflow, &use_fp_on_smis, not_taken);
686 __ mov(eax, left);
687 break;
688
689 case Token::MUL:
690 // If the smi tag is 0 we can just leave the tag on one operand.
691 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
692 // We can't revert the multiplication if the result is not a smi
693 // so save the right operand.
694 __ mov(ebx, right);
695 // Remove tag from one of the operands (but keep sign).
696 __ SmiUntag(right);
697 // Do multiplication.
698 __ imul(right, Operand(left)); // Multiplication is commutative.
699 __ j(overflow, &use_fp_on_smis, not_taken);
700 // Check for negative zero result. Use combined = left | right.
701 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
702 break;
703
704 case Token::DIV:
705 // We can't revert the division if the result is not a smi so
706 // save the left operand.
707 __ mov(edi, left);
708 // Check for 0 divisor.
709 __ test(right, Operand(right));
710 __ j(zero, &use_fp_on_smis, not_taken);
711 // Sign extend left into edx:eax.
712 ASSERT(left.is(eax));
713 __ cdq();
714 // Divide edx:eax by right.
715 __ idiv(right);
716 // Check for the corner case of dividing the most negative smi by
717 // -1. We cannot use the overflow flag, since it is not set by idiv
718 // instruction.
719 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
720 __ cmp(eax, 0x40000000);
721 __ j(equal, &use_fp_on_smis);
722 // Check for negative zero result. Use combined = left | right.
723 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
724 // Check that the remainder is zero.
725 __ test(edx, Operand(edx));
726 __ j(not_zero, &use_fp_on_smis);
727 // Tag the result and store it in register eax.
728 __ SmiTag(eax);
729 break;
730
731 case Token::MOD:
732 // Check for 0 divisor.
733 __ test(right, Operand(right));
734 __ j(zero, &not_smis, not_taken);
735
736 // Sign extend left into edx:eax.
737 ASSERT(left.is(eax));
738 __ cdq();
739 // Divide edx:eax by right.
740 __ idiv(right);
741 // Check for negative zero result. Use combined = left | right.
742 __ NegativeZeroTest(edx, combined, slow);
743 // Move remainder to register eax.
744 __ mov(eax, edx);
745 break;
746
747 default:
748 UNREACHABLE();
749 }
750
751 // 5. Emit return of result in eax.
752 GenerateReturn(masm);
753
754 // 6. For some operations emit inline code to perform floating point
755 // operations on known smis (e.g., if the result of the operation
756 // overflowed the smi range).
757 switch (op_) {
758 case Token::SHL: {
759 Comment perform_float(masm, "-- Perform float operation on smis");
760 __ bind(&use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000761 if (runtime_operands_type_ != BinaryOpIC::UNINIT_OR_SMI) {
762 // Result we want is in left == edx, so we can put the allocated heap
763 // number in eax.
764 __ AllocateHeapNumber(eax, ecx, ebx, slow);
765 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000766 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000767 CpuFeatures::Scope use_sse2(SSE2);
768 __ cvtsi2sd(xmm0, Operand(left));
769 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
770 } else {
771 // It's OK to overwrite the right argument on the stack because we
772 // are about to return.
773 __ mov(Operand(esp, 1 * kPointerSize), left);
774 __ fild_s(Operand(esp, 1 * kPointerSize));
775 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
776 }
777 GenerateReturn(masm);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000778 } else {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000779 ASSERT(runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI);
780 __ jmp(slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000781 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000782 break;
783 }
784
785 case Token::ADD:
786 case Token::SUB:
787 case Token::MUL:
788 case Token::DIV: {
789 Comment perform_float(masm, "-- Perform float operation on smis");
790 __ bind(&use_fp_on_smis);
791 // Restore arguments to edx, eax.
792 switch (op_) {
793 case Token::ADD:
794 // Revert right = right + left.
795 __ sub(right, Operand(left));
796 break;
797 case Token::SUB:
798 // Revert left = left - right.
799 __ add(left, Operand(right));
800 break;
801 case Token::MUL:
802 // Right was clobbered but a copy is in ebx.
803 __ mov(right, ebx);
804 break;
805 case Token::DIV:
806 // Left was clobbered but a copy is in edi. Right is in ebx for
807 // division.
808 __ mov(edx, edi);
809 __ mov(eax, right);
810 break;
811 default: UNREACHABLE();
812 break;
813 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000814 if (runtime_operands_type_ != BinaryOpIC::UNINIT_OR_SMI) {
815 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000816 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000817 CpuFeatures::Scope use_sse2(SSE2);
818 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
819 switch (op_) {
820 case Token::ADD: __ addsd(xmm0, xmm1); break;
821 case Token::SUB: __ subsd(xmm0, xmm1); break;
822 case Token::MUL: __ mulsd(xmm0, xmm1); break;
823 case Token::DIV: __ divsd(xmm0, xmm1); break;
824 default: UNREACHABLE();
825 }
826 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
827 } else { // SSE2 not available, use FPU.
828 FloatingPointHelper::LoadFloatSmis(masm, ebx);
829 switch (op_) {
830 case Token::ADD: __ faddp(1); break;
831 case Token::SUB: __ fsubp(1); break;
832 case Token::MUL: __ fmulp(1); break;
833 case Token::DIV: __ fdivp(1); break;
834 default: UNREACHABLE();
835 }
836 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000837 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000838 __ mov(eax, ecx);
839 GenerateReturn(masm);
840 } else {
841 ASSERT(runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI);
842 __ jmp(slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000843 }
ricow@chromium.org65fae842010-08-25 15:26:24 +0000844 break;
845 }
846
847 default:
848 break;
849 }
850
851 // 7. Non-smi operands, fall out to the non-smi code with the operands in
852 // edx and eax.
853 Comment done_comment(masm, "-- Enter non-smi code");
854 __ bind(&not_smis);
855 switch (op_) {
856 case Token::BIT_OR:
857 case Token::SHL:
858 case Token::SAR:
859 case Token::SHR:
860 // Right operand is saved in ecx and eax was destroyed by the smi
861 // check.
862 __ mov(eax, ecx);
863 break;
864
865 case Token::DIV:
866 case Token::MOD:
867 // Operands are in eax, ebx at this point.
868 __ mov(edx, eax);
869 __ mov(eax, ebx);
870 break;
871
872 default:
873 break;
874 }
875}
876
877
878void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
879 Label call_runtime;
880
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000881 __ IncrementCounter(COUNTERS->generic_binary_stub_calls(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000882
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000883 if (runtime_operands_type_ == BinaryOpIC::UNINIT_OR_SMI) {
884 Label slow;
885 if (ShouldGenerateSmiCode()) GenerateSmiCode(masm, &slow);
886 __ bind(&slow);
887 GenerateTypeTransition(masm);
888 }
889
ricow@chromium.org65fae842010-08-25 15:26:24 +0000890 // Generate fast case smi code if requested. This flag is set when the fast
891 // case smi code is not generated by the caller. Generating it here will speed
892 // up common operations.
893 if (ShouldGenerateSmiCode()) {
894 GenerateSmiCode(masm, &call_runtime);
895 } else if (op_ != Token::MOD) { // MOD goes straight to runtime.
896 if (!HasArgsInRegisters()) {
897 GenerateLoadArguments(masm);
898 }
899 }
900
901 // Floating point case.
902 if (ShouldGenerateFPCode()) {
903 switch (op_) {
904 case Token::ADD:
905 case Token::SUB:
906 case Token::MUL:
907 case Token::DIV: {
908 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
909 HasSmiCodeInStub()) {
910 // Execution reaches this point when the first non-smi argument occurs
911 // (and only if smi code is generated). This is the right moment to
912 // patch to HEAP_NUMBERS state. The transition is attempted only for
913 // the four basic operations. The stub stays in the DEFAULT state
914 // forever for all other operations (also if smi code is skipped).
915 GenerateTypeTransition(masm);
916 break;
917 }
918
919 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000920 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000921 CpuFeatures::Scope use_sse2(SSE2);
922 if (static_operands_type_.IsNumber()) {
923 if (FLAG_debug_code) {
924 // Assert at runtime that inputs are only numbers.
925 __ AbortIfNotNumber(edx);
926 __ AbortIfNotNumber(eax);
927 }
928 if (static_operands_type_.IsSmi()) {
929 if (FLAG_debug_code) {
930 __ AbortIfNotSmi(edx);
931 __ AbortIfNotSmi(eax);
932 }
933 FloatingPointHelper::LoadSSE2Smis(masm, ecx);
934 } else {
935 FloatingPointHelper::LoadSSE2Operands(masm);
936 }
937 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000938 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000939 }
940
941 switch (op_) {
942 case Token::ADD: __ addsd(xmm0, xmm1); break;
943 case Token::SUB: __ subsd(xmm0, xmm1); break;
944 case Token::MUL: __ mulsd(xmm0, xmm1); break;
945 case Token::DIV: __ divsd(xmm0, xmm1); break;
946 default: UNREACHABLE();
947 }
948 GenerateHeapResultAllocation(masm, &call_runtime);
949 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
950 GenerateReturn(masm);
951 } else { // SSE2 not available, use FPU.
952 if (static_operands_type_.IsNumber()) {
953 if (FLAG_debug_code) {
954 // Assert at runtime that inputs are only numbers.
955 __ AbortIfNotNumber(edx);
956 __ AbortIfNotNumber(eax);
957 }
958 } else {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +0000959 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000960 }
961 FloatingPointHelper::LoadFloatOperands(
962 masm,
963 ecx,
964 FloatingPointHelper::ARGS_IN_REGISTERS);
965 switch (op_) {
966 case Token::ADD: __ faddp(1); break;
967 case Token::SUB: __ fsubp(1); break;
968 case Token::MUL: __ fmulp(1); break;
969 case Token::DIV: __ fdivp(1); break;
970 default: UNREACHABLE();
971 }
972 Label after_alloc_failure;
973 GenerateHeapResultAllocation(masm, &after_alloc_failure);
974 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
975 GenerateReturn(masm);
976 __ bind(&after_alloc_failure);
977 __ ffree();
978 __ jmp(&call_runtime);
979 }
980 __ bind(&not_floats);
981 if (runtime_operands_type_ == BinaryOpIC::DEFAULT &&
982 !HasSmiCodeInStub()) {
983 // Execution reaches this point when the first non-number argument
984 // occurs (and only if smi code is skipped from the stub, otherwise
985 // the patching has already been done earlier in this case branch).
986 // Try patching to STRINGS for ADD operation.
987 if (op_ == Token::ADD) {
988 GenerateTypeTransition(masm);
989 }
990 }
991 break;
992 }
993 case Token::MOD: {
994 // For MOD we go directly to runtime in the non-smi case.
995 break;
996 }
997 case Token::BIT_OR:
998 case Token::BIT_AND:
999 case Token::BIT_XOR:
1000 case Token::SAR:
1001 case Token::SHL:
1002 case Token::SHR: {
1003 Label non_smi_result;
1004 FloatingPointHelper::LoadAsIntegers(masm,
1005 static_operands_type_,
1006 use_sse3_,
1007 &call_runtime);
1008 switch (op_) {
1009 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1010 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1011 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1012 case Token::SAR: __ sar_cl(eax); break;
1013 case Token::SHL: __ shl_cl(eax); break;
1014 case Token::SHR: __ shr_cl(eax); break;
1015 default: UNREACHABLE();
1016 }
1017 if (op_ == Token::SHR) {
1018 // Check if result is non-negative and fits in a smi.
1019 __ test(eax, Immediate(0xc0000000));
1020 __ j(not_zero, &call_runtime);
1021 } else {
1022 // Check if result fits in a smi.
1023 __ cmp(eax, 0xc0000000);
1024 __ j(negative, &non_smi_result);
1025 }
1026 // Tag smi result and return.
1027 __ SmiTag(eax);
1028 GenerateReturn(masm);
1029
1030 // All ops except SHR return a signed int32 that we load in
1031 // a HeapNumber.
1032 if (op_ != Token::SHR) {
1033 __ bind(&non_smi_result);
1034 // Allocate a heap number if needed.
1035 __ mov(ebx, Operand(eax)); // ebx: result
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001036 NearLabel skip_allocation;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001037 switch (mode_) {
1038 case OVERWRITE_LEFT:
1039 case OVERWRITE_RIGHT:
1040 // If the operand was an object, we skip the
1041 // allocation of a heap number.
1042 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1043 1 * kPointerSize : 2 * kPointerSize));
1044 __ test(eax, Immediate(kSmiTagMask));
1045 __ j(not_zero, &skip_allocation, not_taken);
1046 // Fall through!
1047 case NO_OVERWRITE:
1048 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1049 __ bind(&skip_allocation);
1050 break;
1051 default: UNREACHABLE();
1052 }
1053 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001054 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001055 CpuFeatures::Scope use_sse2(SSE2);
1056 __ cvtsi2sd(xmm0, Operand(ebx));
1057 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1058 } else {
1059 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1060 __ fild_s(Operand(esp, 1 * kPointerSize));
1061 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1062 }
1063 GenerateReturn(masm);
1064 }
1065 break;
1066 }
1067 default: UNREACHABLE(); break;
1068 }
1069 }
1070
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001071 // If all else fails, use the runtime system to get the correct
1072 // result. If arguments was passed in registers now place them on the
1073 // stack in the correct order below the return address.
1074
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001075 // Avoid hitting the string ADD code below when allocation fails in
1076 // the floating point code above.
1077 if (op_ != Token::ADD) {
1078 __ bind(&call_runtime);
1079 }
1080
ricow@chromium.org65fae842010-08-25 15:26:24 +00001081 if (HasArgsInRegisters()) {
1082 GenerateRegisterArgsPush(masm);
1083 }
1084
1085 switch (op_) {
1086 case Token::ADD: {
1087 // Test for string arguments before calling runtime.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001088
1089 // If this stub has already generated FP-specific code then the arguments
1090 // are already in edx, eax
1091 if (!ShouldGenerateFPCode() && !HasArgsInRegisters()) {
1092 GenerateLoadArguments(masm);
1093 }
1094
1095 // Registers containing left and right operands respectively.
1096 Register lhs, rhs;
1097 if (HasArgsReversed()) {
1098 lhs = eax;
1099 rhs = edx;
1100 } else {
1101 lhs = edx;
1102 rhs = eax;
1103 }
1104
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001105 // Test if left operand is a string.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001106 NearLabel lhs_not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001107 __ test(lhs, Immediate(kSmiTagMask));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001108 __ j(zero, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001109 __ CmpObjectType(lhs, FIRST_NONSTRING_TYPE, ecx);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001110 __ j(above_equal, &lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001111
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001112 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
1113 __ TailCallStub(&string_add_left_stub);
1114
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00001115 NearLabel call_runtime_with_args;
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001116 // Left operand is not a string, test right.
1117 __ bind(&lhs_not_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001118 __ test(rhs, Immediate(kSmiTagMask));
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001119 __ j(zero, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001120 __ CmpObjectType(rhs, FIRST_NONSTRING_TYPE, ecx);
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001121 __ j(above_equal, &call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001122
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001123 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
1124 __ TailCallStub(&string_add_right_stub);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001125
ricow@chromium.org65fae842010-08-25 15:26:24 +00001126 // Neither argument is a string.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00001127 __ bind(&call_runtime);
1128 if (HasArgsInRegisters()) {
1129 GenerateRegisterArgsPush(masm);
1130 }
ager@chromium.org5b2fbee2010-09-08 06:38:15 +00001131 __ bind(&call_runtime_with_args);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001132 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1133 break;
1134 }
1135 case Token::SUB:
1136 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1137 break;
1138 case Token::MUL:
1139 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1140 break;
1141 case Token::DIV:
1142 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1143 break;
1144 case Token::MOD:
1145 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1146 break;
1147 case Token::BIT_OR:
1148 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1149 break;
1150 case Token::BIT_AND:
1151 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1152 break;
1153 case Token::BIT_XOR:
1154 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1155 break;
1156 case Token::SAR:
1157 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1158 break;
1159 case Token::SHL:
1160 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1161 break;
1162 case Token::SHR:
1163 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1164 break;
1165 default:
1166 UNREACHABLE();
1167 }
1168}
1169
1170
1171void GenericBinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
1172 Label* alloc_failure) {
1173 Label skip_allocation;
1174 OverwriteMode mode = mode_;
1175 if (HasArgsReversed()) {
1176 if (mode == OVERWRITE_RIGHT) {
1177 mode = OVERWRITE_LEFT;
1178 } else if (mode == OVERWRITE_LEFT) {
1179 mode = OVERWRITE_RIGHT;
1180 }
1181 }
1182 switch (mode) {
1183 case OVERWRITE_LEFT: {
1184 // If the argument in edx is already an object, we skip the
1185 // allocation of a heap number.
1186 __ test(edx, Immediate(kSmiTagMask));
1187 __ j(not_zero, &skip_allocation, not_taken);
1188 // Allocate a heap number for the result. Keep eax and edx intact
1189 // for the possible runtime call.
1190 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1191 // Now edx can be overwritten losing one of the arguments as we are
1192 // now done and will not need it any more.
1193 __ mov(edx, Operand(ebx));
1194 __ bind(&skip_allocation);
1195 // Use object in edx as a result holder
1196 __ mov(eax, Operand(edx));
1197 break;
1198 }
1199 case OVERWRITE_RIGHT:
1200 // If the argument in eax is already an object, we skip the
1201 // allocation of a heap number.
1202 __ test(eax, Immediate(kSmiTagMask));
1203 __ j(not_zero, &skip_allocation, not_taken);
1204 // Fall through!
1205 case NO_OVERWRITE:
1206 // Allocate a heap number for the result. Keep eax and edx intact
1207 // for the possible runtime call.
1208 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
1209 // Now eax can be overwritten losing one of the arguments as we are
1210 // now done and will not need it any more.
1211 __ mov(eax, ebx);
1212 __ bind(&skip_allocation);
1213 break;
1214 default: UNREACHABLE();
1215 }
1216}
1217
1218
1219void GenericBinaryOpStub::GenerateLoadArguments(MacroAssembler* masm) {
1220 // If arguments are not passed in registers read them from the stack.
1221 ASSERT(!HasArgsInRegisters());
1222 __ mov(eax, Operand(esp, 1 * kPointerSize));
1223 __ mov(edx, Operand(esp, 2 * kPointerSize));
1224}
1225
1226
1227void GenericBinaryOpStub::GenerateReturn(MacroAssembler* masm) {
1228 // If arguments are not passed in registers remove them from the stack before
1229 // returning.
1230 if (!HasArgsInRegisters()) {
1231 __ ret(2 * kPointerSize); // Remove both operands
1232 } else {
1233 __ ret(0);
1234 }
1235}
1236
1237
1238void GenericBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
1239 ASSERT(HasArgsInRegisters());
1240 __ pop(ecx);
1241 if (HasArgsReversed()) {
1242 __ push(eax);
1243 __ push(edx);
1244 } else {
1245 __ push(edx);
1246 __ push(eax);
1247 }
1248 __ push(ecx);
1249}
1250
1251
1252void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1253 // Ensure the operands are on the stack.
1254 if (HasArgsInRegisters()) {
1255 GenerateRegisterArgsPush(masm);
1256 }
1257
1258 __ pop(ecx); // Save return address.
1259
1260 // Left and right arguments are now on top.
1261 // Push this stub's key. Although the operation and the type info are
1262 // encoded into the key, the encoding is opaque, so push them too.
1263 __ push(Immediate(Smi::FromInt(MinorKey())));
1264 __ push(Immediate(Smi::FromInt(op_)));
1265 __ push(Immediate(Smi::FromInt(runtime_operands_type_)));
1266
1267 __ push(ecx); // Push return address.
1268
1269 // Patch the caller to an appropriate specialized stub and return the
1270 // operation result to the caller of the stub.
1271 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001272 ExternalReference(IC_Utility(IC::kBinaryOp_Patch), masm->isolate()),
ricow@chromium.org65fae842010-08-25 15:26:24 +00001273 5,
1274 1);
1275}
1276
1277
1278Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
1279 GenericBinaryOpStub stub(key, type_info);
1280 return stub.GetCode();
1281}
1282
1283
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001284Handle<Code> GetTypeRecordingBinaryOpStub(int key,
1285 TRBinaryOpIC::TypeInfo type_info,
1286 TRBinaryOpIC::TypeInfo result_type_info) {
1287 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
1288 return stub.GetCode();
1289}
1290
1291
1292void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1293 __ pop(ecx); // Save return address.
1294 __ push(edx);
1295 __ push(eax);
1296 // Left and right arguments are now on top.
1297 // Push this stub's key. Although the operation and the type info are
1298 // encoded into the key, the encoding is opaque, so push them too.
1299 __ push(Immediate(Smi::FromInt(MinorKey())));
1300 __ push(Immediate(Smi::FromInt(op_)));
1301 __ push(Immediate(Smi::FromInt(operands_type_)));
1302
1303 __ push(ecx); // Push return address.
1304
1305 // Patch the caller to an appropriate specialized stub and return the
1306 // operation result to the caller of the stub.
1307 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001308 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
1309 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001310 5,
1311 1);
1312}
1313
1314
1315// Prepare for a type transition runtime call when the args are already on
1316// the stack, under the return address.
1317void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
1318 MacroAssembler* masm) {
1319 __ pop(ecx); // Save return address.
1320 // Left and right arguments are already on top of the stack.
1321 // Push this stub's key. Although the operation and the type info are
1322 // encoded into the key, the encoding is opaque, so push them too.
1323 __ push(Immediate(Smi::FromInt(MinorKey())));
1324 __ push(Immediate(Smi::FromInt(op_)));
1325 __ push(Immediate(Smi::FromInt(operands_type_)));
1326
1327 __ push(ecx); // Push return address.
1328
1329 // Patch the caller to an appropriate specialized stub and return the
1330 // operation result to the caller of the stub.
1331 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001332 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
1333 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001334 5,
1335 1);
1336}
1337
1338
1339void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
1340 switch (operands_type_) {
1341 case TRBinaryOpIC::UNINITIALIZED:
1342 GenerateTypeTransition(masm);
1343 break;
1344 case TRBinaryOpIC::SMI:
1345 GenerateSmiStub(masm);
1346 break;
1347 case TRBinaryOpIC::INT32:
1348 GenerateInt32Stub(masm);
1349 break;
1350 case TRBinaryOpIC::HEAP_NUMBER:
1351 GenerateHeapNumberStub(masm);
1352 break;
1353 case TRBinaryOpIC::STRING:
1354 GenerateStringStub(masm);
1355 break;
1356 case TRBinaryOpIC::GENERIC:
1357 GenerateGeneric(masm);
1358 break;
1359 default:
1360 UNREACHABLE();
1361 }
1362}
1363
1364
1365const char* TypeRecordingBinaryOpStub::GetName() {
1366 if (name_ != NULL) return name_;
1367 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001368 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
1369 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001370 if (name_ == NULL) return "OOM";
1371 const char* op_name = Token::Name(op_);
1372 const char* overwrite_name;
1373 switch (mode_) {
1374 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1375 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1376 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1377 default: overwrite_name = "UnknownOverwrite"; break;
1378 }
1379
1380 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1381 "TypeRecordingBinaryOpStub_%s_%s_%s",
1382 op_name,
1383 overwrite_name,
1384 TRBinaryOpIC::GetName(operands_type_));
1385 return name_;
1386}
1387
1388
1389void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
1390 Label* slow,
1391 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
1392 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
1393 // dividend in eax and edx free for the division. Use eax, ebx for those.
1394 Comment load_comment(masm, "-- Load arguments");
1395 Register left = edx;
1396 Register right = eax;
1397 if (op_ == Token::DIV || op_ == Token::MOD) {
1398 left = eax;
1399 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001400 __ mov(ebx, eax);
1401 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001402 }
1403
1404
1405 // 2. Prepare the smi check of both operands by oring them together.
1406 Comment smi_check_comment(masm, "-- Smi check arguments");
1407 Label not_smis;
1408 Register combined = ecx;
1409 ASSERT(!left.is(combined) && !right.is(combined));
1410 switch (op_) {
1411 case Token::BIT_OR:
1412 // Perform the operation into eax and smi check the result. Preserve
1413 // eax in case the result is not a smi.
1414 ASSERT(!left.is(ecx) && !right.is(ecx));
1415 __ mov(ecx, right);
1416 __ or_(right, Operand(left)); // Bitwise or is commutative.
1417 combined = right;
1418 break;
1419
1420 case Token::BIT_XOR:
1421 case Token::BIT_AND:
1422 case Token::ADD:
1423 case Token::SUB:
1424 case Token::MUL:
1425 case Token::DIV:
1426 case Token::MOD:
1427 __ mov(combined, right);
1428 __ or_(combined, Operand(left));
1429 break;
1430
1431 case Token::SHL:
1432 case Token::SAR:
1433 case Token::SHR:
1434 // Move the right operand into ecx for the shift operation, use eax
1435 // for the smi check register.
1436 ASSERT(!left.is(ecx) && !right.is(ecx));
1437 __ mov(ecx, right);
1438 __ or_(right, Operand(left));
1439 combined = right;
1440 break;
1441
1442 default:
1443 break;
1444 }
1445
1446 // 3. Perform the smi check of the operands.
1447 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1448 __ test(combined, Immediate(kSmiTagMask));
1449 __ j(not_zero, &not_smis, not_taken);
1450
1451 // 4. Operands are both smis, perform the operation leaving the result in
1452 // eax and check the result if necessary.
1453 Comment perform_smi(masm, "-- Perform smi operation");
1454 Label use_fp_on_smis;
1455 switch (op_) {
1456 case Token::BIT_OR:
1457 // Nothing to do.
1458 break;
1459
1460 case Token::BIT_XOR:
1461 ASSERT(right.is(eax));
1462 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1463 break;
1464
1465 case Token::BIT_AND:
1466 ASSERT(right.is(eax));
1467 __ and_(right, Operand(left)); // Bitwise and is commutative.
1468 break;
1469
1470 case Token::SHL:
1471 // Remove tags from operands (but keep sign).
1472 __ SmiUntag(left);
1473 __ SmiUntag(ecx);
1474 // Perform the operation.
1475 __ shl_cl(left);
1476 // Check that the *signed* result fits in a smi.
1477 __ cmp(left, 0xc0000000);
1478 __ j(sign, &use_fp_on_smis, not_taken);
1479 // Tag the result and store it in register eax.
1480 __ SmiTag(left);
1481 __ mov(eax, left);
1482 break;
1483
1484 case Token::SAR:
1485 // Remove tags from operands (but keep sign).
1486 __ SmiUntag(left);
1487 __ SmiUntag(ecx);
1488 // Perform the operation.
1489 __ sar_cl(left);
1490 // Tag the result and store it in register eax.
1491 __ SmiTag(left);
1492 __ mov(eax, left);
1493 break;
1494
1495 case Token::SHR:
1496 // Remove tags from operands (but keep sign).
1497 __ SmiUntag(left);
1498 __ SmiUntag(ecx);
1499 // Perform the operation.
1500 __ shr_cl(left);
1501 // Check that the *unsigned* result fits in a smi.
1502 // Neither of the two high-order bits can be set:
1503 // - 0x80000000: high bit would be lost when smi tagging.
1504 // - 0x40000000: this number would convert to negative when
1505 // Smi tagging these two cases can only happen with shifts
1506 // by 0 or 1 when handed a valid smi.
1507 __ test(left, Immediate(0xc0000000));
1508 __ j(not_zero, slow, not_taken);
1509 // Tag the result and store it in register eax.
1510 __ SmiTag(left);
1511 __ mov(eax, left);
1512 break;
1513
1514 case Token::ADD:
1515 ASSERT(right.is(eax));
1516 __ add(right, Operand(left)); // Addition is commutative.
1517 __ j(overflow, &use_fp_on_smis, not_taken);
1518 break;
1519
1520 case Token::SUB:
1521 __ sub(left, Operand(right));
1522 __ j(overflow, &use_fp_on_smis, not_taken);
1523 __ mov(eax, left);
1524 break;
1525
1526 case Token::MUL:
1527 // If the smi tag is 0 we can just leave the tag on one operand.
1528 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1529 // We can't revert the multiplication if the result is not a smi
1530 // so save the right operand.
1531 __ mov(ebx, right);
1532 // Remove tag from one of the operands (but keep sign).
1533 __ SmiUntag(right);
1534 // Do multiplication.
1535 __ imul(right, Operand(left)); // Multiplication is commutative.
1536 __ j(overflow, &use_fp_on_smis, not_taken);
1537 // Check for negative zero result. Use combined = left | right.
1538 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1539 break;
1540
1541 case Token::DIV:
1542 // We can't revert the division if the result is not a smi so
1543 // save the left operand.
1544 __ mov(edi, left);
1545 // Check for 0 divisor.
1546 __ test(right, Operand(right));
1547 __ j(zero, &use_fp_on_smis, not_taken);
1548 // Sign extend left into edx:eax.
1549 ASSERT(left.is(eax));
1550 __ cdq();
1551 // Divide edx:eax by right.
1552 __ idiv(right);
1553 // Check for the corner case of dividing the most negative smi by
1554 // -1. We cannot use the overflow flag, since it is not set by idiv
1555 // instruction.
1556 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1557 __ cmp(eax, 0x40000000);
1558 __ j(equal, &use_fp_on_smis);
1559 // Check for negative zero result. Use combined = left | right.
1560 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1561 // Check that the remainder is zero.
1562 __ test(edx, Operand(edx));
1563 __ j(not_zero, &use_fp_on_smis);
1564 // Tag the result and store it in register eax.
1565 __ SmiTag(eax);
1566 break;
1567
1568 case Token::MOD:
1569 // Check for 0 divisor.
1570 __ test(right, Operand(right));
1571 __ j(zero, &not_smis, not_taken);
1572
1573 // Sign extend left into edx:eax.
1574 ASSERT(left.is(eax));
1575 __ cdq();
1576 // Divide edx:eax by right.
1577 __ idiv(right);
1578 // Check for negative zero result. Use combined = left | right.
1579 __ NegativeZeroTest(edx, combined, slow);
1580 // Move remainder to register eax.
1581 __ mov(eax, edx);
1582 break;
1583
1584 default:
1585 UNREACHABLE();
1586 }
1587
1588 // 5. Emit return of result in eax. Some operations have registers pushed.
1589 switch (op_) {
1590 case Token::ADD:
1591 case Token::SUB:
1592 case Token::MUL:
1593 case Token::DIV:
1594 __ ret(0);
1595 break;
1596 case Token::MOD:
1597 case Token::BIT_OR:
1598 case Token::BIT_AND:
1599 case Token::BIT_XOR:
1600 case Token::SAR:
1601 case Token::SHL:
1602 case Token::SHR:
1603 __ ret(2 * kPointerSize);
1604 break;
1605 default:
1606 UNREACHABLE();
1607 }
1608
1609 // 6. For some operations emit inline code to perform floating point
1610 // operations on known smis (e.g., if the result of the operation
1611 // overflowed the smi range).
1612 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1613 __ bind(&use_fp_on_smis);
1614 switch (op_) {
1615 // Undo the effects of some operations, and some register moves.
1616 case Token::SHL:
1617 // The arguments are saved on the stack, and only used from there.
1618 break;
1619 case Token::ADD:
1620 // Revert right = right + left.
1621 __ sub(right, Operand(left));
1622 break;
1623 case Token::SUB:
1624 // Revert left = left - right.
1625 __ add(left, Operand(right));
1626 break;
1627 case Token::MUL:
1628 // Right was clobbered but a copy is in ebx.
1629 __ mov(right, ebx);
1630 break;
1631 case Token::DIV:
1632 // Left was clobbered but a copy is in edi. Right is in ebx for
1633 // division. They should be in eax, ebx for jump to not_smi.
1634 __ mov(eax, edi);
1635 break;
1636 default:
1637 // No other operators jump to use_fp_on_smis.
1638 break;
1639 }
1640 __ jmp(&not_smis);
1641 } else {
1642 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1643 switch (op_) {
1644 case Token::SHL: {
1645 Comment perform_float(masm, "-- Perform float operation on smis");
1646 __ bind(&use_fp_on_smis);
1647 // Result we want is in left == edx, so we can put the allocated heap
1648 // number in eax.
1649 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1650 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001651 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001652 CpuFeatures::Scope use_sse2(SSE2);
1653 __ cvtsi2sd(xmm0, Operand(left));
1654 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1655 } else {
1656 // It's OK to overwrite the right argument on the stack because we
1657 // are about to return.
1658 __ mov(Operand(esp, 1 * kPointerSize), left);
1659 __ fild_s(Operand(esp, 1 * kPointerSize));
1660 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1661 }
1662 __ ret(2 * kPointerSize);
1663 break;
1664 }
1665
1666 case Token::ADD:
1667 case Token::SUB:
1668 case Token::MUL:
1669 case Token::DIV: {
1670 Comment perform_float(masm, "-- Perform float operation on smis");
1671 __ bind(&use_fp_on_smis);
1672 // Restore arguments to edx, eax.
1673 switch (op_) {
1674 case Token::ADD:
1675 // Revert right = right + left.
1676 __ sub(right, Operand(left));
1677 break;
1678 case Token::SUB:
1679 // Revert left = left - right.
1680 __ add(left, Operand(right));
1681 break;
1682 case Token::MUL:
1683 // Right was clobbered but a copy is in ebx.
1684 __ mov(right, ebx);
1685 break;
1686 case Token::DIV:
1687 // Left was clobbered but a copy is in edi. Right is in ebx for
1688 // division.
1689 __ mov(edx, edi);
1690 __ mov(eax, right);
1691 break;
1692 default: UNREACHABLE();
1693 break;
1694 }
1695 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001696 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001697 CpuFeatures::Scope use_sse2(SSE2);
1698 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1699 switch (op_) {
1700 case Token::ADD: __ addsd(xmm0, xmm1); break;
1701 case Token::SUB: __ subsd(xmm0, xmm1); break;
1702 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1703 case Token::DIV: __ divsd(xmm0, xmm1); break;
1704 default: UNREACHABLE();
1705 }
1706 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1707 } else { // SSE2 not available, use FPU.
1708 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1709 switch (op_) {
1710 case Token::ADD: __ faddp(1); break;
1711 case Token::SUB: __ fsubp(1); break;
1712 case Token::MUL: __ fmulp(1); break;
1713 case Token::DIV: __ fdivp(1); break;
1714 default: UNREACHABLE();
1715 }
1716 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1717 }
1718 __ mov(eax, ecx);
1719 __ ret(0);
1720 break;
1721 }
1722
1723 default:
1724 break;
1725 }
1726 }
1727
1728 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1729 // edx and eax.
1730 Comment done_comment(masm, "-- Enter non-smi code");
1731 __ bind(&not_smis);
1732 switch (op_) {
1733 case Token::BIT_OR:
1734 case Token::SHL:
1735 case Token::SAR:
1736 case Token::SHR:
1737 // Right operand is saved in ecx and eax was destroyed by the smi
1738 // check.
1739 __ mov(eax, ecx);
1740 break;
1741
1742 case Token::DIV:
1743 case Token::MOD:
1744 // Operands are in eax, ebx at this point.
1745 __ mov(edx, eax);
1746 __ mov(eax, ebx);
1747 break;
1748
1749 default:
1750 break;
1751 }
1752}
1753
1754
1755void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1756 Label call_runtime;
1757
1758 switch (op_) {
1759 case Token::ADD:
1760 case Token::SUB:
1761 case Token::MUL:
1762 case Token::DIV:
1763 break;
1764 case Token::MOD:
1765 case Token::BIT_OR:
1766 case Token::BIT_AND:
1767 case Token::BIT_XOR:
1768 case Token::SAR:
1769 case Token::SHL:
1770 case Token::SHR:
1771 GenerateRegisterArgsPush(masm);
1772 break;
1773 default:
1774 UNREACHABLE();
1775 }
1776
1777 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1778 result_type_ == TRBinaryOpIC::SMI) {
1779 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1780 } else {
1781 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1782 }
1783 __ bind(&call_runtime);
1784 switch (op_) {
1785 case Token::ADD:
1786 case Token::SUB:
1787 case Token::MUL:
1788 case Token::DIV:
1789 GenerateTypeTransition(masm);
1790 break;
1791 case Token::MOD:
1792 case Token::BIT_OR:
1793 case Token::BIT_AND:
1794 case Token::BIT_XOR:
1795 case Token::SAR:
1796 case Token::SHL:
1797 case Token::SHR:
1798 GenerateTypeTransitionWithSavedArgs(masm);
1799 break;
1800 default:
1801 UNREACHABLE();
1802 }
1803}
1804
1805
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001806void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001807 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1808 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001809 // Try to add arguments as strings, otherwise, transition to the generic
1810 // TRBinaryOpIC type.
1811 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001812 GenerateTypeTransition(masm);
1813}
1814
1815
1816void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1817 Label call_runtime;
1818 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1819
1820 // Floating point case.
1821 switch (op_) {
1822 case Token::ADD:
1823 case Token::SUB:
1824 case Token::MUL:
1825 case Token::DIV: {
1826 Label not_floats;
1827 Label not_int32;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001828 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001829 CpuFeatures::Scope use_sse2(SSE2);
1830 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1831 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1832 switch (op_) {
1833 case Token::ADD: __ addsd(xmm0, xmm1); break;
1834 case Token::SUB: __ subsd(xmm0, xmm1); break;
1835 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1836 case Token::DIV: __ divsd(xmm0, xmm1); break;
1837 default: UNREACHABLE();
1838 }
1839 // Check result type if it is currently Int32.
1840 if (result_type_ <= TRBinaryOpIC::INT32) {
1841 __ cvttsd2si(ecx, Operand(xmm0));
1842 __ cvtsi2sd(xmm2, Operand(ecx));
1843 __ ucomisd(xmm0, xmm2);
1844 __ j(not_zero, &not_int32);
1845 __ j(carry, &not_int32);
1846 }
1847 GenerateHeapResultAllocation(masm, &call_runtime);
1848 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1849 __ ret(0);
1850 } else { // SSE2 not available, use FPU.
1851 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1852 FloatingPointHelper::LoadFloatOperands(
1853 masm,
1854 ecx,
1855 FloatingPointHelper::ARGS_IN_REGISTERS);
1856 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1857 switch (op_) {
1858 case Token::ADD: __ faddp(1); break;
1859 case Token::SUB: __ fsubp(1); break;
1860 case Token::MUL: __ fmulp(1); break;
1861 case Token::DIV: __ fdivp(1); break;
1862 default: UNREACHABLE();
1863 }
1864 Label after_alloc_failure;
1865 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1866 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1867 __ ret(0);
1868 __ bind(&after_alloc_failure);
1869 __ ffree();
1870 __ jmp(&call_runtime);
1871 }
1872
1873 __ bind(&not_floats);
1874 __ bind(&not_int32);
1875 GenerateTypeTransition(masm);
1876 break;
1877 }
1878
1879 case Token::MOD: {
1880 // For MOD we go directly to runtime in the non-smi case.
1881 break;
1882 }
1883 case Token::BIT_OR:
1884 case Token::BIT_AND:
1885 case Token::BIT_XOR:
1886 case Token::SAR:
1887 case Token::SHL:
1888 case Token::SHR: {
1889 GenerateRegisterArgsPush(masm);
1890 Label not_floats;
1891 Label not_int32;
1892 Label non_smi_result;
1893 /* {
1894 CpuFeatures::Scope use_sse2(SSE2);
1895 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1896 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1897 }*/
1898 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1899 use_sse3_,
1900 &not_floats);
1901 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1902 &not_int32);
1903 switch (op_) {
1904 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1905 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1906 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1907 case Token::SAR: __ sar_cl(eax); break;
1908 case Token::SHL: __ shl_cl(eax); break;
1909 case Token::SHR: __ shr_cl(eax); break;
1910 default: UNREACHABLE();
1911 }
1912 if (op_ == Token::SHR) {
1913 // Check if result is non-negative and fits in a smi.
1914 __ test(eax, Immediate(0xc0000000));
1915 __ j(not_zero, &call_runtime);
1916 } else {
1917 // Check if result fits in a smi.
1918 __ cmp(eax, 0xc0000000);
1919 __ j(negative, &non_smi_result);
1920 }
1921 // Tag smi result and return.
1922 __ SmiTag(eax);
1923 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1924
1925 // All ops except SHR return a signed int32 that we load in
1926 // a HeapNumber.
1927 if (op_ != Token::SHR) {
1928 __ bind(&non_smi_result);
1929 // Allocate a heap number if needed.
1930 __ mov(ebx, Operand(eax)); // ebx: result
1931 NearLabel skip_allocation;
1932 switch (mode_) {
1933 case OVERWRITE_LEFT:
1934 case OVERWRITE_RIGHT:
1935 // If the operand was an object, we skip the
1936 // allocation of a heap number.
1937 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1938 1 * kPointerSize : 2 * kPointerSize));
1939 __ test(eax, Immediate(kSmiTagMask));
1940 __ j(not_zero, &skip_allocation, not_taken);
1941 // Fall through!
1942 case NO_OVERWRITE:
1943 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1944 __ bind(&skip_allocation);
1945 break;
1946 default: UNREACHABLE();
1947 }
1948 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001949 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001950 CpuFeatures::Scope use_sse2(SSE2);
1951 __ cvtsi2sd(xmm0, Operand(ebx));
1952 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1953 } else {
1954 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1955 __ fild_s(Operand(esp, 1 * kPointerSize));
1956 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1957 }
1958 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1959 }
1960
1961 __ bind(&not_floats);
1962 __ bind(&not_int32);
1963 GenerateTypeTransitionWithSavedArgs(masm);
1964 break;
1965 }
1966 default: UNREACHABLE(); break;
1967 }
1968
1969 // If an allocation fails, or SHR or MOD hit a hard case,
1970 // use the runtime system to get the correct result.
1971 __ bind(&call_runtime);
1972
1973 switch (op_) {
1974 case Token::ADD:
1975 GenerateRegisterArgsPush(masm);
1976 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1977 break;
1978 case Token::SUB:
1979 GenerateRegisterArgsPush(masm);
1980 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1981 break;
1982 case Token::MUL:
1983 GenerateRegisterArgsPush(masm);
1984 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1985 break;
1986 case Token::DIV:
1987 GenerateRegisterArgsPush(masm);
1988 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1989 break;
1990 case Token::MOD:
1991 GenerateRegisterArgsPush(masm);
1992 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1993 break;
1994 case Token::BIT_OR:
1995 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1996 break;
1997 case Token::BIT_AND:
1998 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1999 break;
2000 case Token::BIT_XOR:
2001 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2002 break;
2003 case Token::SAR:
2004 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2005 break;
2006 case Token::SHL:
2007 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2008 break;
2009 case Token::SHR:
2010 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2011 break;
2012 default:
2013 UNREACHABLE();
2014 }
2015}
2016
2017
2018void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
2019 Label call_runtime;
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00002020 ASSERT(operands_type_ == TRBinaryOpIC::HEAP_NUMBER);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002021
2022 // Floating point case.
2023 switch (op_) {
2024 case Token::ADD:
2025 case Token::SUB:
2026 case Token::MUL:
2027 case Token::DIV: {
2028 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002029 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002030 CpuFeatures::Scope use_sse2(SSE2);
2031 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2032
2033 switch (op_) {
2034 case Token::ADD: __ addsd(xmm0, xmm1); break;
2035 case Token::SUB: __ subsd(xmm0, xmm1); break;
2036 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2037 case Token::DIV: __ divsd(xmm0, xmm1); break;
2038 default: UNREACHABLE();
2039 }
2040 GenerateHeapResultAllocation(masm, &call_runtime);
2041 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2042 __ ret(0);
2043 } else { // SSE2 not available, use FPU.
2044 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2045 FloatingPointHelper::LoadFloatOperands(
2046 masm,
2047 ecx,
2048 FloatingPointHelper::ARGS_IN_REGISTERS);
2049 switch (op_) {
2050 case Token::ADD: __ faddp(1); break;
2051 case Token::SUB: __ fsubp(1); break;
2052 case Token::MUL: __ fmulp(1); break;
2053 case Token::DIV: __ fdivp(1); break;
2054 default: UNREACHABLE();
2055 }
2056 Label after_alloc_failure;
2057 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2058 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2059 __ ret(0);
2060 __ bind(&after_alloc_failure);
2061 __ ffree();
2062 __ jmp(&call_runtime);
2063 }
2064
2065 __ bind(&not_floats);
2066 GenerateTypeTransition(masm);
2067 break;
2068 }
2069
2070 case Token::MOD: {
2071 // For MOD we go directly to runtime in the non-smi case.
2072 break;
2073 }
2074 case Token::BIT_OR:
2075 case Token::BIT_AND:
2076 case Token::BIT_XOR:
2077 case Token::SAR:
2078 case Token::SHL:
2079 case Token::SHR: {
2080 GenerateRegisterArgsPush(masm);
2081 Label not_floats;
2082 Label non_smi_result;
2083 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2084 use_sse3_,
2085 &not_floats);
2086 switch (op_) {
2087 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2088 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2089 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2090 case Token::SAR: __ sar_cl(eax); break;
2091 case Token::SHL: __ shl_cl(eax); break;
2092 case Token::SHR: __ shr_cl(eax); break;
2093 default: UNREACHABLE();
2094 }
2095 if (op_ == Token::SHR) {
2096 // Check if result is non-negative and fits in a smi.
2097 __ test(eax, Immediate(0xc0000000));
2098 __ j(not_zero, &call_runtime);
2099 } else {
2100 // Check if result fits in a smi.
2101 __ cmp(eax, 0xc0000000);
2102 __ j(negative, &non_smi_result);
2103 }
2104 // Tag smi result and return.
2105 __ SmiTag(eax);
2106 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2107
2108 // All ops except SHR return a signed int32 that we load in
2109 // a HeapNumber.
2110 if (op_ != Token::SHR) {
2111 __ bind(&non_smi_result);
2112 // Allocate a heap number if needed.
2113 __ mov(ebx, Operand(eax)); // ebx: result
2114 NearLabel skip_allocation;
2115 switch (mode_) {
2116 case OVERWRITE_LEFT:
2117 case OVERWRITE_RIGHT:
2118 // If the operand was an object, we skip the
2119 // allocation of a heap number.
2120 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2121 1 * kPointerSize : 2 * kPointerSize));
2122 __ test(eax, Immediate(kSmiTagMask));
2123 __ j(not_zero, &skip_allocation, not_taken);
2124 // Fall through!
2125 case NO_OVERWRITE:
2126 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2127 __ bind(&skip_allocation);
2128 break;
2129 default: UNREACHABLE();
2130 }
2131 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002132 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002133 CpuFeatures::Scope use_sse2(SSE2);
2134 __ cvtsi2sd(xmm0, Operand(ebx));
2135 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2136 } else {
2137 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2138 __ fild_s(Operand(esp, 1 * kPointerSize));
2139 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2140 }
2141 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2142 }
2143
2144 __ bind(&not_floats);
2145 GenerateTypeTransitionWithSavedArgs(masm);
2146 break;
2147 }
2148 default: UNREACHABLE(); break;
2149 }
2150
2151 // If an allocation fails, or SHR or MOD hit a hard case,
2152 // use the runtime system to get the correct result.
2153 __ bind(&call_runtime);
2154
2155 switch (op_) {
2156 case Token::ADD:
2157 GenerateRegisterArgsPush(masm);
2158 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2159 break;
2160 case Token::SUB:
2161 GenerateRegisterArgsPush(masm);
2162 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2163 break;
2164 case Token::MUL:
2165 GenerateRegisterArgsPush(masm);
2166 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2167 break;
2168 case Token::DIV:
2169 GenerateRegisterArgsPush(masm);
2170 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2171 break;
2172 case Token::MOD:
2173 GenerateRegisterArgsPush(masm);
2174 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2175 break;
2176 case Token::BIT_OR:
2177 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2178 break;
2179 case Token::BIT_AND:
2180 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2181 break;
2182 case Token::BIT_XOR:
2183 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2184 break;
2185 case Token::SAR:
2186 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2187 break;
2188 case Token::SHL:
2189 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2190 break;
2191 case Token::SHR:
2192 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2193 break;
2194 default:
2195 UNREACHABLE();
2196 }
2197}
2198
2199
2200void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
2201 Label call_runtime;
2202
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002203 __ IncrementCounter(COUNTERS->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002204
2205 switch (op_) {
2206 case Token::ADD:
2207 case Token::SUB:
2208 case Token::MUL:
2209 case Token::DIV:
2210 break;
2211 case Token::MOD:
2212 case Token::BIT_OR:
2213 case Token::BIT_AND:
2214 case Token::BIT_XOR:
2215 case Token::SAR:
2216 case Token::SHL:
2217 case Token::SHR:
2218 GenerateRegisterArgsPush(masm);
2219 break;
2220 default:
2221 UNREACHABLE();
2222 }
2223
2224 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
2225
2226 // Floating point case.
2227 switch (op_) {
2228 case Token::ADD:
2229 case Token::SUB:
2230 case Token::MUL:
2231 case Token::DIV: {
2232 Label not_floats;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002233 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002234 CpuFeatures::Scope use_sse2(SSE2);
2235 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2236
2237 switch (op_) {
2238 case Token::ADD: __ addsd(xmm0, xmm1); break;
2239 case Token::SUB: __ subsd(xmm0, xmm1); break;
2240 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2241 case Token::DIV: __ divsd(xmm0, xmm1); break;
2242 default: UNREACHABLE();
2243 }
2244 GenerateHeapResultAllocation(masm, &call_runtime);
2245 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2246 __ ret(0);
2247 } else { // SSE2 not available, use FPU.
2248 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2249 FloatingPointHelper::LoadFloatOperands(
2250 masm,
2251 ecx,
2252 FloatingPointHelper::ARGS_IN_REGISTERS);
2253 switch (op_) {
2254 case Token::ADD: __ faddp(1); break;
2255 case Token::SUB: __ fsubp(1); break;
2256 case Token::MUL: __ fmulp(1); break;
2257 case Token::DIV: __ fdivp(1); break;
2258 default: UNREACHABLE();
2259 }
2260 Label after_alloc_failure;
2261 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2262 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2263 __ ret(0);
2264 __ bind(&after_alloc_failure);
2265 __ ffree();
2266 __ jmp(&call_runtime);
2267 }
2268 __ bind(&not_floats);
2269 break;
2270 }
2271 case Token::MOD: {
2272 // For MOD we go directly to runtime in the non-smi case.
2273 break;
2274 }
2275 case Token::BIT_OR:
2276 case Token::BIT_AND:
2277 case Token::BIT_XOR:
2278 case Token::SAR:
2279 case Token::SHL:
2280 case Token::SHR: {
2281 Label non_smi_result;
2282 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2283 use_sse3_,
2284 &call_runtime);
2285 switch (op_) {
2286 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
2287 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
2288 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
2289 case Token::SAR: __ sar_cl(eax); break;
2290 case Token::SHL: __ shl_cl(eax); break;
2291 case Token::SHR: __ shr_cl(eax); break;
2292 default: UNREACHABLE();
2293 }
2294 if (op_ == Token::SHR) {
2295 // Check if result is non-negative and fits in a smi.
2296 __ test(eax, Immediate(0xc0000000));
2297 __ j(not_zero, &call_runtime);
2298 } else {
2299 // Check if result fits in a smi.
2300 __ cmp(eax, 0xc0000000);
2301 __ j(negative, &non_smi_result);
2302 }
2303 // Tag smi result and return.
2304 __ SmiTag(eax);
2305 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
2306
2307 // All ops except SHR return a signed int32 that we load in
2308 // a HeapNumber.
2309 if (op_ != Token::SHR) {
2310 __ bind(&non_smi_result);
2311 // Allocate a heap number if needed.
2312 __ mov(ebx, Operand(eax)); // ebx: result
2313 NearLabel skip_allocation;
2314 switch (mode_) {
2315 case OVERWRITE_LEFT:
2316 case OVERWRITE_RIGHT:
2317 // If the operand was an object, we skip the
2318 // allocation of a heap number.
2319 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2320 1 * kPointerSize : 2 * kPointerSize));
2321 __ test(eax, Immediate(kSmiTagMask));
2322 __ j(not_zero, &skip_allocation, not_taken);
2323 // Fall through!
2324 case NO_OVERWRITE:
2325 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2326 __ bind(&skip_allocation);
2327 break;
2328 default: UNREACHABLE();
2329 }
2330 // Store the result in the HeapNumber and return.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002331 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002332 CpuFeatures::Scope use_sse2(SSE2);
2333 __ cvtsi2sd(xmm0, Operand(ebx));
2334 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2335 } else {
2336 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2337 __ fild_s(Operand(esp, 1 * kPointerSize));
2338 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2339 }
2340 __ ret(2 * kPointerSize);
2341 }
2342 break;
2343 }
2344 default: UNREACHABLE(); break;
2345 }
2346
2347 // If all else fails, use the runtime system to get the correct
2348 // result.
2349 __ bind(&call_runtime);
2350 switch (op_) {
2351 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002352 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002353 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002354 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2355 break;
2356 }
2357 case Token::SUB:
2358 GenerateRegisterArgsPush(masm);
2359 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2360 break;
2361 case Token::MUL:
2362 GenerateRegisterArgsPush(masm);
2363 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2364 break;
2365 case Token::DIV:
2366 GenerateRegisterArgsPush(masm);
2367 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2368 break;
2369 case Token::MOD:
2370 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2371 break;
2372 case Token::BIT_OR:
2373 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2374 break;
2375 case Token::BIT_AND:
2376 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2377 break;
2378 case Token::BIT_XOR:
2379 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2380 break;
2381 case Token::SAR:
2382 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2383 break;
2384 case Token::SHL:
2385 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2386 break;
2387 case Token::SHR:
2388 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2389 break;
2390 default:
2391 UNREACHABLE();
2392 }
2393}
2394
2395
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002396void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002397 ASSERT(op_ == Token::ADD);
2398 NearLabel left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002399
2400 // Registers containing left and right operands respectively.
2401 Register left = edx;
2402 Register right = eax;
2403
2404 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002405 __ test(left, Immediate(kSmiTagMask));
2406 __ j(zero, &left_not_string);
2407 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
2408 __ j(above_equal, &left_not_string);
2409
2410 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2411 GenerateRegisterArgsPush(masm);
2412 __ TailCallStub(&string_add_left_stub);
2413
2414 // Left operand is not a string, test right.
2415 __ bind(&left_not_string);
2416 __ test(right, Immediate(kSmiTagMask));
2417 __ j(zero, &call_runtime);
2418 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
2419 __ j(above_equal, &call_runtime);
2420
2421 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2422 GenerateRegisterArgsPush(masm);
2423 __ TailCallStub(&string_add_right_stub);
2424
2425 // Neither argument is a string.
2426 __ bind(&call_runtime);
2427}
2428
2429
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002430void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2431 MacroAssembler* masm,
2432 Label* alloc_failure) {
2433 Label skip_allocation;
2434 OverwriteMode mode = mode_;
2435 switch (mode) {
2436 case OVERWRITE_LEFT: {
2437 // If the argument in edx is already an object, we skip the
2438 // allocation of a heap number.
2439 __ test(edx, Immediate(kSmiTagMask));
2440 __ j(not_zero, &skip_allocation, not_taken);
2441 // Allocate a heap number for the result. Keep eax and edx intact
2442 // for the possible runtime call.
2443 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2444 // Now edx can be overwritten losing one of the arguments as we are
2445 // now done and will not need it any more.
2446 __ mov(edx, Operand(ebx));
2447 __ bind(&skip_allocation);
2448 // Use object in edx as a result holder
2449 __ mov(eax, Operand(edx));
2450 break;
2451 }
2452 case OVERWRITE_RIGHT:
2453 // If the argument in eax is already an object, we skip the
2454 // allocation of a heap number.
2455 __ test(eax, Immediate(kSmiTagMask));
2456 __ j(not_zero, &skip_allocation, not_taken);
2457 // Fall through!
2458 case NO_OVERWRITE:
2459 // Allocate a heap number for the result. Keep eax and edx intact
2460 // for the possible runtime call.
2461 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2462 // Now eax can be overwritten losing one of the arguments as we are
2463 // now done and will not need it any more.
2464 __ mov(eax, ebx);
2465 __ bind(&skip_allocation);
2466 break;
2467 default: UNREACHABLE();
2468 }
2469}
2470
2471
2472void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2473 __ pop(ecx);
2474 __ push(edx);
2475 __ push(eax);
2476 __ push(ecx);
2477}
2478
2479
ricow@chromium.org65fae842010-08-25 15:26:24 +00002480void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002481 // TAGGED case:
2482 // Input:
2483 // esp[4]: tagged number input argument (should be number).
2484 // esp[0]: return address.
2485 // Output:
2486 // eax: tagged double result.
2487 // UNTAGGED case:
2488 // Input::
2489 // esp[0]: return address.
2490 // xmm1: untagged double input argument
2491 // Output:
2492 // xmm1: untagged double result.
2493
ricow@chromium.org65fae842010-08-25 15:26:24 +00002494 Label runtime_call;
2495 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002496 Label skip_cache;
2497 const bool tagged = (argument_type_ == TAGGED);
2498 if (tagged) {
2499 // Test that eax is a number.
2500 NearLabel input_not_smi;
2501 NearLabel loaded;
2502 __ mov(eax, Operand(esp, kPointerSize));
2503 __ test(eax, Immediate(kSmiTagMask));
2504 __ j(not_zero, &input_not_smi);
2505 // Input is a smi. Untag and load it onto the FPU stack.
2506 // Then load the low and high words of the double into ebx, edx.
2507 STATIC_ASSERT(kSmiTagSize == 1);
2508 __ sar(eax, 1);
2509 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2510 __ mov(Operand(esp, 0), eax);
2511 __ fild_s(Operand(esp, 0));
2512 __ fst_d(Operand(esp, 0));
2513 __ pop(edx);
2514 __ pop(ebx);
2515 __ jmp(&loaded);
2516 __ bind(&input_not_smi);
2517 // Check if input is a HeapNumber.
2518 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002519 __ cmp(Operand(ebx), Immediate(FACTORY->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002520 __ j(not_equal, &runtime_call);
2521 // Input is a HeapNumber. Push it on the FPU stack and load its
2522 // low and high words into ebx, edx.
2523 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2524 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2525 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002526
whesse@chromium.org023421e2010-12-21 12:19:12 +00002527 __ bind(&loaded);
2528 } else { // UNTAGGED.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002529 if (masm->isolate()->cpu_features()->IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002530 CpuFeatures::Scope sse4_scope(SSE4_1);
2531 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2532 } else {
2533 __ pshufd(xmm0, xmm1, 0x1);
2534 __ movd(Operand(edx), xmm0);
2535 }
2536 __ movd(Operand(ebx), xmm1);
2537 }
2538
2539 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002540 // ebx = low 32 bits of double value
2541 // edx = high 32 bits of double value
2542 // Compute hash (the shifts are arithmetic):
2543 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2544 __ mov(ecx, ebx);
2545 __ xor_(ecx, Operand(edx));
2546 __ mov(eax, ecx);
2547 __ sar(eax, 16);
2548 __ xor_(ecx, Operand(eax));
2549 __ mov(eax, ecx);
2550 __ sar(eax, 8);
2551 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002552 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2553 __ and_(Operand(ecx),
2554 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002555
whesse@chromium.org023421e2010-12-21 12:19:12 +00002556 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002557 // ebx = low 32 bits of double value.
2558 // edx = high 32 bits of double value.
2559 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002560 ExternalReference cache_array =
2561 ExternalReference::transcendental_cache_array_address(masm->isolate());
2562 __ mov(eax, Immediate(cache_array));
2563 int cache_array_index =
2564 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2565 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002566 // Eax points to the cache for the type type_.
2567 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2568 __ test(eax, Operand(eax));
2569 __ j(zero, &runtime_call_clear_stack);
2570#ifdef DEBUG
2571 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002572 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002573 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2574 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2575 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2576 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2577 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2578 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2579 CHECK_EQ(0, elem_in0 - elem_start);
2580 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2581 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2582 }
2583#endif
2584 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2585 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2586 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2587 // Check if cache matches: Double value is stored in uint32_t[2] array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002588 NearLabel cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002589 __ cmp(ebx, Operand(ecx, 0));
2590 __ j(not_equal, &cache_miss);
2591 __ cmp(edx, Operand(ecx, kIntSize));
2592 __ j(not_equal, &cache_miss);
2593 // Cache hit!
2594 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002595 if (tagged) {
2596 __ fstp(0);
2597 __ ret(kPointerSize);
2598 } else { // UNTAGGED.
2599 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2600 __ Ret();
2601 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002602
2603 __ bind(&cache_miss);
2604 // Update cache with new value.
2605 // We are short on registers, so use no_reg as scratch.
2606 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002607 if (tagged) {
2608 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2609 } else { // UNTAGGED.
2610 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2611 __ sub(Operand(esp), Immediate(kDoubleSize));
2612 __ movdbl(Operand(esp, 0), xmm1);
2613 __ fld_d(Operand(esp, 0));
2614 __ add(Operand(esp), Immediate(kDoubleSize));
2615 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002616 GenerateOperation(masm);
2617 __ mov(Operand(ecx, 0), ebx);
2618 __ mov(Operand(ecx, kIntSize), edx);
2619 __ mov(Operand(ecx, 2 * kIntSize), eax);
2620 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002621 if (tagged) {
2622 __ ret(kPointerSize);
2623 } else { // UNTAGGED.
2624 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2625 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002626
whesse@chromium.org023421e2010-12-21 12:19:12 +00002627 // Skip cache and return answer directly, only in untagged case.
2628 __ bind(&skip_cache);
2629 __ sub(Operand(esp), Immediate(kDoubleSize));
2630 __ movdbl(Operand(esp, 0), xmm1);
2631 __ fld_d(Operand(esp, 0));
2632 GenerateOperation(masm);
2633 __ fstp_d(Operand(esp, 0));
2634 __ movdbl(xmm1, Operand(esp, 0));
2635 __ add(Operand(esp), Immediate(kDoubleSize));
2636 // We return the value in xmm1 without adding it to the cache, but
2637 // we cause a scavenging GC so that future allocations will succeed.
2638 __ EnterInternalFrame();
2639 // Allocate an unused object bigger than a HeapNumber.
2640 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2641 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2642 __ LeaveInternalFrame();
2643 __ Ret();
2644 }
2645
2646 // Call runtime, doing whatever allocation and cleanup is necessary.
2647 if (tagged) {
2648 __ bind(&runtime_call_clear_stack);
2649 __ fstp(0);
2650 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002651 ExternalReference runtime =
2652 ExternalReference(RuntimeFunction(), masm->isolate());
2653 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002654 } else { // UNTAGGED.
2655 __ bind(&runtime_call_clear_stack);
2656 __ bind(&runtime_call);
2657 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2658 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2659 __ EnterInternalFrame();
2660 __ push(eax);
2661 __ CallRuntime(RuntimeFunction(), 1);
2662 __ LeaveInternalFrame();
2663 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2664 __ Ret();
2665 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002666}
2667
2668
2669Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2670 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002671 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2672 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002673 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002674 default:
2675 UNIMPLEMENTED();
2676 return Runtime::kAbort;
2677 }
2678}
2679
2680
2681void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2682 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002683 // Input value is on FP stack, and also in ebx/edx.
2684 // Input value is possibly in xmm1.
2685 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002686 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2687 // Both fsin and fcos require arguments in the range +/-2^63 and
2688 // return NaN for infinities and NaN. They can share all code except
2689 // the actual fsin/fcos operation.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002690 NearLabel in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002691 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2692 // work. We must reduce it to the appropriate range.
2693 __ mov(edi, edx);
2694 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2695 int supported_exponent_limit =
2696 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2697 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
2698 __ j(below, &in_range, taken);
2699 // Check for infinity and NaN. Both return NaN for sin.
2700 __ cmp(Operand(edi), Immediate(0x7ff00000));
2701 NearLabel non_nan_result;
2702 __ j(not_equal, &non_nan_result, taken);
2703 // Input is +/-Infinity or NaN. Result is NaN.
2704 __ fstp(0);
2705 // NaN is represented by 0x7ff8000000000000.
2706 __ push(Immediate(0x7ff80000));
2707 __ push(Immediate(0));
2708 __ fld_d(Operand(esp, 0));
2709 __ add(Operand(esp), Immediate(2 * kPointerSize));
2710 __ jmp(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002711
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002712 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002713
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002714 // Use fpmod to restrict argument to the range +/-2*PI.
2715 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2716 __ fldpi();
2717 __ fadd(0);
2718 __ fld(1);
2719 // FPU Stack: input, 2*pi, input.
2720 {
2721 NearLabel no_exceptions;
2722 __ fwait();
2723 __ fnstsw_ax();
2724 // Clear if Illegal Operand or Zero Division exceptions are set.
2725 __ test(Operand(eax), Immediate(5));
2726 __ j(zero, &no_exceptions);
2727 __ fnclex();
2728 __ bind(&no_exceptions);
2729 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002730
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002731 // Compute st(0) % st(1)
2732 {
2733 NearLabel partial_remainder_loop;
2734 __ bind(&partial_remainder_loop);
2735 __ fprem1();
2736 __ fwait();
2737 __ fnstsw_ax();
2738 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2739 // If C2 is set, computation only has partial result. Loop to
2740 // continue computation.
2741 __ j(not_zero, &partial_remainder_loop);
2742 }
2743 // FPU Stack: input, 2*pi, input % 2*pi
2744 __ fstp(2);
2745 __ fstp(0);
2746 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2747
2748 // FPU Stack: input % 2*pi
2749 __ bind(&in_range);
2750 switch (type_) {
2751 case TranscendentalCache::SIN:
2752 __ fsin();
2753 break;
2754 case TranscendentalCache::COS:
2755 __ fcos();
2756 break;
2757 default:
2758 UNREACHABLE();
2759 }
2760 __ bind(&done);
2761 } else {
2762 ASSERT(type_ == TranscendentalCache::LOG);
2763 __ fldln2();
2764 __ fxch();
2765 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002766 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002767}
2768
2769
2770// Get the integer part of a heap number. Surprisingly, all this bit twiddling
2771// is faster than using the built-in instructions on floating point registers.
2772// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
2773// trashed registers.
2774void IntegerConvert(MacroAssembler* masm,
2775 Register source,
2776 TypeInfo type_info,
2777 bool use_sse3,
2778 Label* conversion_failure) {
2779 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
2780 Label done, right_exponent, normal_exponent;
2781 Register scratch = ebx;
2782 Register scratch2 = edi;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002783 if (type_info.IsInteger32() &&
2784 masm->isolate()->cpu_features()->IsEnabled(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002785 CpuFeatures::Scope scope(SSE2);
2786 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
2787 return;
2788 }
2789 if (!type_info.IsInteger32() || !use_sse3) {
2790 // Get exponent word.
2791 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
2792 // Get exponent alone in scratch2.
2793 __ mov(scratch2, scratch);
2794 __ and_(scratch2, HeapNumber::kExponentMask);
2795 }
2796 if (use_sse3) {
2797 CpuFeatures::Scope scope(SSE3);
2798 if (!type_info.IsInteger32()) {
2799 // Check whether the exponent is too big for a 64 bit signed integer.
2800 static const uint32_t kTooBigExponent =
2801 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
2802 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
2803 __ j(greater_equal, conversion_failure);
2804 }
2805 // Load x87 register with heap number.
2806 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
2807 // Reserve space for 64 bit answer.
2808 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2809 // Do conversion, which cannot fail because we checked the exponent.
2810 __ fisttp_d(Operand(esp, 0));
2811 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
2812 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
2813 } else {
2814 // Load ecx with zero. We use this either for the final shift or
2815 // for the answer.
2816 __ xor_(ecx, Operand(ecx));
2817 // Check whether the exponent matches a 32 bit signed int that cannot be
2818 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
2819 // exponent is 30 (biased). This is the exponent that we are fastest at and
2820 // also the highest exponent we can handle here.
2821 const uint32_t non_smi_exponent =
2822 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
2823 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
2824 // If we have a match of the int32-but-not-Smi exponent then skip some
2825 // logic.
2826 __ j(equal, &right_exponent);
2827 // If the exponent is higher than that then go to slow case. This catches
2828 // numbers that don't fit in a signed int32, infinities and NaNs.
2829 __ j(less, &normal_exponent);
2830
2831 {
2832 // Handle a big exponent. The only reason we have this code is that the
2833 // >>> operator has a tendency to generate numbers with an exponent of 31.
2834 const uint32_t big_non_smi_exponent =
2835 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
2836 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
2837 __ j(not_equal, conversion_failure);
2838 // We have the big exponent, typically from >>>. This means the number is
2839 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
2840 __ mov(scratch2, scratch);
2841 __ and_(scratch2, HeapNumber::kMantissaMask);
2842 // Put back the implicit 1.
2843 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
2844 // Shift up the mantissa bits to take up the space the exponent used to
2845 // take. We just orred in the implicit bit so that took care of one and
2846 // we want to use the full unsigned range so we subtract 1 bit from the
2847 // shift distance.
2848 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
2849 __ shl(scratch2, big_shift_distance);
2850 // Get the second half of the double.
2851 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
2852 // Shift down 21 bits to get the most significant 11 bits or the low
2853 // mantissa word.
2854 __ shr(ecx, 32 - big_shift_distance);
2855 __ or_(ecx, Operand(scratch2));
2856 // We have the answer in ecx, but we may need to negate it.
2857 __ test(scratch, Operand(scratch));
2858 __ j(positive, &done);
2859 __ neg(ecx);
2860 __ jmp(&done);
2861 }
2862
2863 __ bind(&normal_exponent);
2864 // Exponent word in scratch, exponent part of exponent word in scratch2.
2865 // Zero in ecx.
2866 // We know the exponent is smaller than 30 (biased). If it is less than
2867 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
2868 // it rounds to zero.
2869 const uint32_t zero_exponent =
2870 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
2871 __ sub(Operand(scratch2), Immediate(zero_exponent));
2872 // ecx already has a Smi zero.
2873 __ j(less, &done);
2874
2875 // We have a shifted exponent between 0 and 30 in scratch2.
2876 __ shr(scratch2, HeapNumber::kExponentShift);
2877 __ mov(ecx, Immediate(30));
2878 __ sub(ecx, Operand(scratch2));
2879
2880 __ bind(&right_exponent);
2881 // Here ecx is the shift, scratch is the exponent word.
2882 // Get the top bits of the mantissa.
2883 __ and_(scratch, HeapNumber::kMantissaMask);
2884 // Put back the implicit 1.
2885 __ or_(scratch, 1 << HeapNumber::kExponentShift);
2886 // Shift up the mantissa bits to take up the space the exponent used to
2887 // take. We have kExponentShift + 1 significant bits int he low end of the
2888 // word. Shift them to the top bits.
2889 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
2890 __ shl(scratch, shift_distance);
2891 // Get the second half of the double. For some exponents we don't
2892 // actually need this because the bits get shifted out again, but
2893 // it's probably slower to test than just to do it.
2894 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
2895 // Shift down 22 bits to get the most significant 10 bits or the low
2896 // mantissa word.
2897 __ shr(scratch2, 32 - shift_distance);
2898 __ or_(scratch2, Operand(scratch));
2899 // Move down according to the exponent.
2900 __ shr_cl(scratch2);
2901 // Now the unsigned answer is in scratch2. We need to move it to ecx and
2902 // we may need to fix the sign.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002903 NearLabel negative;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002904 __ xor_(ecx, Operand(ecx));
2905 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
2906 __ j(greater, &negative);
2907 __ mov(ecx, scratch2);
2908 __ jmp(&done);
2909 __ bind(&negative);
2910 __ sub(ecx, Operand(scratch2));
2911 __ bind(&done);
2912 }
2913}
2914
2915
2916// Input: edx, eax are the left and right objects of a bit op.
2917// Output: eax, ecx are left and right integers for a bit op.
2918void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
2919 TypeInfo type_info,
2920 bool use_sse3,
2921 Label* conversion_failure) {
2922 // Check float operands.
2923 Label arg1_is_object, check_undefined_arg1;
2924 Label arg2_is_object, check_undefined_arg2;
2925 Label load_arg2, done;
2926
2927 if (!type_info.IsDouble()) {
2928 if (!type_info.IsSmi()) {
2929 __ test(edx, Immediate(kSmiTagMask));
2930 __ j(not_zero, &arg1_is_object);
2931 } else {
2932 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
2933 }
2934 __ SmiUntag(edx);
2935 __ jmp(&load_arg2);
2936 }
2937
2938 __ bind(&arg1_is_object);
2939
2940 // Get the untagged integer version of the edx heap number in ecx.
2941 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
2942 __ mov(edx, ecx);
2943
2944 // Here edx has the untagged integer, eax has a Smi or a heap number.
2945 __ bind(&load_arg2);
2946 if (!type_info.IsDouble()) {
2947 // Test if arg2 is a Smi.
2948 if (!type_info.IsSmi()) {
2949 __ test(eax, Immediate(kSmiTagMask));
2950 __ j(not_zero, &arg2_is_object);
2951 } else {
2952 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
2953 }
2954 __ SmiUntag(eax);
2955 __ mov(ecx, eax);
2956 __ jmp(&done);
2957 }
2958
2959 __ bind(&arg2_is_object);
2960
2961 // Get the untagged integer version of the eax heap number in ecx.
2962 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
2963 __ bind(&done);
2964 __ mov(eax, edx);
2965}
2966
2967
2968// Input: edx, eax are the left and right objects of a bit op.
2969// Output: eax, ecx are left and right integers for a bit op.
2970void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2971 bool use_sse3,
2972 Label* conversion_failure) {
2973 // Check float operands.
2974 Label arg1_is_object, check_undefined_arg1;
2975 Label arg2_is_object, check_undefined_arg2;
2976 Label load_arg2, done;
2977
2978 // Test if arg1 is a Smi.
2979 __ test(edx, Immediate(kSmiTagMask));
2980 __ j(not_zero, &arg1_is_object);
2981
2982 __ SmiUntag(edx);
2983 __ jmp(&load_arg2);
2984
2985 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2986 __ bind(&check_undefined_arg1);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002987 __ cmp(edx, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002988 __ j(not_equal, conversion_failure);
2989 __ mov(edx, Immediate(0));
2990 __ jmp(&load_arg2);
2991
2992 __ bind(&arg1_is_object);
2993 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002994 __ cmp(ebx, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002995 __ j(not_equal, &check_undefined_arg1);
2996
2997 // Get the untagged integer version of the edx heap number in ecx.
2998 IntegerConvert(masm,
2999 edx,
3000 TypeInfo::Unknown(),
3001 use_sse3,
3002 conversion_failure);
3003 __ mov(edx, ecx);
3004
3005 // Here edx has the untagged integer, eax has a Smi or a heap number.
3006 __ bind(&load_arg2);
3007
3008 // Test if arg2 is a Smi.
3009 __ test(eax, Immediate(kSmiTagMask));
3010 __ j(not_zero, &arg2_is_object);
3011
3012 __ SmiUntag(eax);
3013 __ mov(ecx, eax);
3014 __ jmp(&done);
3015
3016 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
3017 __ bind(&check_undefined_arg2);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003018 __ cmp(eax, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003019 __ j(not_equal, conversion_failure);
3020 __ mov(ecx, Immediate(0));
3021 __ jmp(&done);
3022
3023 __ bind(&arg2_is_object);
3024 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003025 __ cmp(ebx, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003026 __ j(not_equal, &check_undefined_arg2);
3027
3028 // Get the untagged integer version of the eax heap number in ecx.
3029 IntegerConvert(masm,
3030 eax,
3031 TypeInfo::Unknown(),
3032 use_sse3,
3033 conversion_failure);
3034 __ bind(&done);
3035 __ mov(eax, edx);
3036}
3037
3038
3039void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
3040 TypeInfo type_info,
3041 bool use_sse3,
3042 Label* conversion_failure) {
3043 if (type_info.IsNumber()) {
3044 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
3045 } else {
3046 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
3047 }
3048}
3049
3050
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003051void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
3052 bool use_sse3,
3053 Label* not_int32) {
3054 return;
3055}
3056
3057
ricow@chromium.org65fae842010-08-25 15:26:24 +00003058void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
3059 Register number) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003060 NearLabel load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003061
3062 __ test(number, Immediate(kSmiTagMask));
3063 __ j(zero, &load_smi, not_taken);
3064 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
3065 __ jmp(&done);
3066
3067 __ bind(&load_smi);
3068 __ SmiUntag(number);
3069 __ push(number);
3070 __ fild_s(Operand(esp, 0));
3071 __ pop(number);
3072
3073 __ bind(&done);
3074}
3075
3076
3077void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003078 NearLabel load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003079 // Load operand in edx into xmm0.
3080 __ test(edx, Immediate(kSmiTagMask));
3081 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
3082 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3083
3084 __ bind(&load_eax);
3085 // Load operand in eax into xmm1.
3086 __ test(eax, Immediate(kSmiTagMask));
3087 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
3088 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3089 __ jmp(&done);
3090
3091 __ bind(&load_smi_edx);
3092 __ SmiUntag(edx); // Untag smi before converting to float.
3093 __ cvtsi2sd(xmm0, Operand(edx));
3094 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3095 __ jmp(&load_eax);
3096
3097 __ bind(&load_smi_eax);
3098 __ SmiUntag(eax); // Untag smi before converting to float.
3099 __ cvtsi2sd(xmm1, Operand(eax));
3100 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3101
3102 __ bind(&done);
3103}
3104
3105
3106void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
3107 Label* not_numbers) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003108 NearLabel load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003109 // Load operand in edx into xmm0, or branch to not_numbers.
3110 __ test(edx, Immediate(kSmiTagMask));
3111 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003112 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003113 __ j(not_equal, not_numbers); // Argument in edx is not a number.
3114 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3115 __ bind(&load_eax);
3116 // Load operand in eax into xmm1, or branch to not_numbers.
3117 __ test(eax, Immediate(kSmiTagMask));
3118 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003119 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003120 __ j(equal, &load_float_eax);
3121 __ jmp(not_numbers); // Argument in eax is not a number.
3122 __ bind(&load_smi_edx);
3123 __ SmiUntag(edx); // Untag smi before converting to float.
3124 __ cvtsi2sd(xmm0, Operand(edx));
3125 __ SmiTag(edx); // Retag smi for heap number overwriting test.
3126 __ jmp(&load_eax);
3127 __ bind(&load_smi_eax);
3128 __ SmiUntag(eax); // Untag smi before converting to float.
3129 __ cvtsi2sd(xmm1, Operand(eax));
3130 __ SmiTag(eax); // Retag smi for heap number overwriting test.
3131 __ jmp(&done);
3132 __ bind(&load_float_eax);
3133 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3134 __ bind(&done);
3135}
3136
3137
3138void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
3139 Register scratch) {
3140 const Register left = edx;
3141 const Register right = eax;
3142 __ mov(scratch, left);
3143 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3144 __ SmiUntag(scratch);
3145 __ cvtsi2sd(xmm0, Operand(scratch));
3146
3147 __ mov(scratch, right);
3148 __ SmiUntag(scratch);
3149 __ cvtsi2sd(xmm1, Operand(scratch));
3150}
3151
3152
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003153void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
3154 Label* non_int32,
3155 Register scratch) {
3156 __ cvttsd2si(scratch, Operand(xmm0));
3157 __ cvtsi2sd(xmm2, Operand(scratch));
3158 __ ucomisd(xmm0, xmm2);
3159 __ j(not_zero, non_int32);
3160 __ j(carry, non_int32);
3161 __ cvttsd2si(scratch, Operand(xmm1));
3162 __ cvtsi2sd(xmm2, Operand(scratch));
3163 __ ucomisd(xmm1, xmm2);
3164 __ j(not_zero, non_int32);
3165 __ j(carry, non_int32);
3166}
3167
3168
ricow@chromium.org65fae842010-08-25 15:26:24 +00003169void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
3170 Register scratch,
3171 ArgLocation arg_location) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003172 NearLabel load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003173 if (arg_location == ARGS_IN_REGISTERS) {
3174 __ mov(scratch, edx);
3175 } else {
3176 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3177 }
3178 __ test(scratch, Immediate(kSmiTagMask));
3179 __ j(zero, &load_smi_1, not_taken);
3180 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3181 __ bind(&done_load_1);
3182
3183 if (arg_location == ARGS_IN_REGISTERS) {
3184 __ mov(scratch, eax);
3185 } else {
3186 __ mov(scratch, Operand(esp, 1 * kPointerSize));
3187 }
3188 __ test(scratch, Immediate(kSmiTagMask));
3189 __ j(zero, &load_smi_2, not_taken);
3190 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
3191 __ jmp(&done);
3192
3193 __ bind(&load_smi_1);
3194 __ SmiUntag(scratch);
3195 __ push(scratch);
3196 __ fild_s(Operand(esp, 0));
3197 __ pop(scratch);
3198 __ jmp(&done_load_1);
3199
3200 __ bind(&load_smi_2);
3201 __ SmiUntag(scratch);
3202 __ push(scratch);
3203 __ fild_s(Operand(esp, 0));
3204 __ pop(scratch);
3205
3206 __ bind(&done);
3207}
3208
3209
3210void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
3211 Register scratch) {
3212 const Register left = edx;
3213 const Register right = eax;
3214 __ mov(scratch, left);
3215 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
3216 __ SmiUntag(scratch);
3217 __ push(scratch);
3218 __ fild_s(Operand(esp, 0));
3219
3220 __ mov(scratch, right);
3221 __ SmiUntag(scratch);
3222 __ mov(Operand(esp, 0), scratch);
3223 __ fild_s(Operand(esp, 0));
3224 __ pop(scratch);
3225}
3226
3227
3228void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
3229 Label* non_float,
3230 Register scratch) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003231 NearLabel test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003232 // Test if both operands are floats or smi -> scratch=k_is_float;
3233 // Otherwise scratch = k_not_float.
3234 __ test(edx, Immediate(kSmiTagMask));
3235 __ j(zero, &test_other, not_taken); // argument in edx is OK
3236 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003237 __ cmp(scratch, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003238 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
3239
3240 __ bind(&test_other);
3241 __ test(eax, Immediate(kSmiTagMask));
3242 __ j(zero, &done); // argument in eax is OK
3243 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003244 __ cmp(scratch, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003245 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
3246
3247 // Fall-through: Both operands are numbers.
3248 __ bind(&done);
3249}
3250
3251
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003252void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
3253 Label* non_int32) {
3254 return;
3255}
3256
3257
ricow@chromium.org65fae842010-08-25 15:26:24 +00003258void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003259 Label slow, done, undo;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003260
3261 if (op_ == Token::SUB) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003262 if (include_smi_code_) {
3263 // Check whether the value is a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003264 NearLabel try_float;
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003265 __ test(eax, Immediate(kSmiTagMask));
3266 __ j(not_zero, &try_float, not_taken);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003267
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003268 if (negative_zero_ == kStrictNegativeZero) {
3269 // Go slow case if the value of the expression is zero
3270 // to make sure that we switch between 0 and -0.
3271 __ test(eax, Operand(eax));
3272 __ j(zero, &slow, not_taken);
3273 }
3274
3275 // The value of the expression is a smi that is not zero. Try
3276 // optimistic subtraction '0 - value'.
3277 __ mov(edx, Operand(eax));
3278 __ Set(eax, Immediate(0));
3279 __ sub(eax, Operand(edx));
3280 __ j(overflow, &undo, not_taken);
3281 __ StubReturn(1);
3282
3283 // Try floating point case.
3284 __ bind(&try_float);
3285 } else if (FLAG_debug_code) {
3286 __ AbortIfSmi(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003287 }
3288
ricow@chromium.org65fae842010-08-25 15:26:24 +00003289 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003290 __ cmp(edx, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003291 __ j(not_equal, &slow);
3292 if (overwrite_ == UNARY_OVERWRITE) {
3293 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
3294 __ xor_(edx, HeapNumber::kSignMask); // Flip sign.
3295 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), edx);
3296 } else {
3297 __ mov(edx, Operand(eax));
3298 // edx: operand
3299 __ AllocateHeapNumber(eax, ebx, ecx, &undo);
3300 // eax: allocated 'empty' number
3301 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3302 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
3303 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
3304 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
3305 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
3306 }
3307 } else if (op_ == Token::BIT_NOT) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003308 if (include_smi_code_) {
3309 Label non_smi;
3310 __ test(eax, Immediate(kSmiTagMask));
3311 __ j(not_zero, &non_smi);
3312 __ not_(eax);
3313 __ and_(eax, ~kSmiTagMask); // Remove inverted smi-tag.
3314 __ ret(0);
3315 __ bind(&non_smi);
3316 } else if (FLAG_debug_code) {
3317 __ AbortIfSmi(eax);
3318 }
3319
ricow@chromium.org65fae842010-08-25 15:26:24 +00003320 // Check if the operand is a heap number.
3321 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003322 __ cmp(edx, FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003323 __ j(not_equal, &slow, not_taken);
3324
3325 // Convert the heap number in eax to an untagged integer in ecx.
3326 IntegerConvert(masm,
3327 eax,
3328 TypeInfo::Unknown(),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003329 masm->isolate()->cpu_features()->IsSupported(SSE3),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003330 &slow);
3331
3332 // Do the bitwise operation and check if the result fits in a smi.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003333 NearLabel try_float;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003334 __ not_(ecx);
3335 __ cmp(ecx, 0xc0000000);
3336 __ j(sign, &try_float, not_taken);
3337
3338 // Tag the result as a smi and we're done.
3339 STATIC_ASSERT(kSmiTagSize == 1);
3340 __ lea(eax, Operand(ecx, times_2, kSmiTag));
3341 __ jmp(&done);
3342
3343 // Try to store the result in a heap number.
3344 __ bind(&try_float);
3345 if (overwrite_ == UNARY_NO_OVERWRITE) {
3346 // Allocate a fresh heap number, but don't overwrite eax until
3347 // we're sure we can do it without going through the slow case
3348 // that needs the value in eax.
3349 __ AllocateHeapNumber(ebx, edx, edi, &slow);
3350 __ mov(eax, Operand(ebx));
3351 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003352 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003353 CpuFeatures::Scope use_sse2(SSE2);
3354 __ cvtsi2sd(xmm0, Operand(ecx));
3355 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
3356 } else {
3357 __ push(ecx);
3358 __ fild_s(Operand(esp, 0));
3359 __ pop(ecx);
3360 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
3361 }
3362 } else {
3363 UNIMPLEMENTED();
3364 }
3365
3366 // Return from the stub.
3367 __ bind(&done);
3368 __ StubReturn(1);
3369
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003370 // Restore eax and go slow case.
3371 __ bind(&undo);
3372 __ mov(eax, Operand(edx));
3373
ricow@chromium.org65fae842010-08-25 15:26:24 +00003374 // Handle the slow case by jumping to the JavaScript builtin.
3375 __ bind(&slow);
3376 __ pop(ecx); // pop return address.
3377 __ push(eax);
3378 __ push(ecx); // push return address
3379 switch (op_) {
3380 case Token::SUB:
3381 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
3382 break;
3383 case Token::BIT_NOT:
3384 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
3385 break;
3386 default:
3387 UNREACHABLE();
3388 }
3389}
3390
3391
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003392void MathPowStub::Generate(MacroAssembler* masm) {
3393 // Registers are used as follows:
3394 // edx = base
3395 // eax = exponent
3396 // ecx = temporary, result
3397
3398 CpuFeatures::Scope use_sse2(SSE2);
3399 Label allocate_return, call_runtime;
3400
3401 // Load input parameters.
3402 __ mov(edx, Operand(esp, 2 * kPointerSize));
3403 __ mov(eax, Operand(esp, 1 * kPointerSize));
3404
3405 // Save 1 in xmm3 - we need this several times later on.
3406 __ mov(ecx, Immediate(1));
3407 __ cvtsi2sd(xmm3, Operand(ecx));
3408
3409 Label exponent_nonsmi;
3410 Label base_nonsmi;
3411 // If the exponent is a heap number go to that specific case.
3412 __ test(eax, Immediate(kSmiTagMask));
3413 __ j(not_zero, &exponent_nonsmi);
3414 __ test(edx, Immediate(kSmiTagMask));
3415 __ j(not_zero, &base_nonsmi);
3416
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003417 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003418 Label powi;
3419 __ SmiUntag(edx);
3420 __ cvtsi2sd(xmm0, Operand(edx));
3421 __ jmp(&powi);
3422 // exponent is smi and base is a heapnumber.
3423 __ bind(&base_nonsmi);
3424 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003425 FACTORY->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003426 __ j(not_equal, &call_runtime);
3427
3428 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3429
3430 // Optimized version of pow if exponent is a smi.
3431 // xmm0 contains the base.
3432 __ bind(&powi);
3433 __ SmiUntag(eax);
3434
3435 // Save exponent in base as we need to check if exponent is negative later.
3436 // We know that base and exponent are in different registers.
3437 __ mov(edx, eax);
3438
3439 // Get absolute value of exponent.
3440 NearLabel no_neg;
3441 __ cmp(eax, 0);
3442 __ j(greater_equal, &no_neg);
3443 __ neg(eax);
3444 __ bind(&no_neg);
3445
3446 // Load xmm1 with 1.
3447 __ movsd(xmm1, xmm3);
3448 NearLabel while_true;
3449 NearLabel no_multiply;
3450
3451 __ bind(&while_true);
3452 __ shr(eax, 1);
3453 __ j(not_carry, &no_multiply);
3454 __ mulsd(xmm1, xmm0);
3455 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003456 __ mulsd(xmm0, xmm0);
3457 __ j(not_zero, &while_true);
3458
3459 // base has the original value of the exponent - if the exponent is
3460 // negative return 1/result.
3461 __ test(edx, Operand(edx));
3462 __ j(positive, &allocate_return);
3463 // Special case if xmm1 has reached infinity.
3464 __ mov(ecx, Immediate(0x7FB00000));
3465 __ movd(xmm0, Operand(ecx));
3466 __ cvtss2sd(xmm0, xmm0);
3467 __ ucomisd(xmm0, xmm1);
3468 __ j(equal, &call_runtime);
3469 __ divsd(xmm3, xmm1);
3470 __ movsd(xmm1, xmm3);
3471 __ jmp(&allocate_return);
3472
3473 // exponent (or both) is a heapnumber - no matter what we should now work
3474 // on doubles.
3475 __ bind(&exponent_nonsmi);
3476 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003477 FACTORY->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003478 __ j(not_equal, &call_runtime);
3479 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3480 // Test if exponent is nan.
3481 __ ucomisd(xmm1, xmm1);
3482 __ j(parity_even, &call_runtime);
3483
3484 NearLabel base_not_smi;
3485 NearLabel handle_special_cases;
3486 __ test(edx, Immediate(kSmiTagMask));
3487 __ j(not_zero, &base_not_smi);
3488 __ SmiUntag(edx);
3489 __ cvtsi2sd(xmm0, Operand(edx));
3490 __ jmp(&handle_special_cases);
3491
3492 __ bind(&base_not_smi);
3493 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003494 FACTORY->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003495 __ j(not_equal, &call_runtime);
3496 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
3497 __ and_(ecx, HeapNumber::kExponentMask);
3498 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
3499 // base is NaN or +/-Infinity
3500 __ j(greater_equal, &call_runtime);
3501 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3502
3503 // base is in xmm0 and exponent is in xmm1.
3504 __ bind(&handle_special_cases);
3505 NearLabel not_minus_half;
3506 // Test for -0.5.
3507 // Load xmm2 with -0.5.
3508 __ mov(ecx, Immediate(0xBF000000));
3509 __ movd(xmm2, Operand(ecx));
3510 __ cvtss2sd(xmm2, xmm2);
3511 // xmm2 now has -0.5.
3512 __ ucomisd(xmm2, xmm1);
3513 __ j(not_equal, &not_minus_half);
3514
3515 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003516 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3517 __ xorpd(xmm1, xmm1);
3518 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003519 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003520 __ divsd(xmm3, xmm1);
3521 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003522 __ jmp(&allocate_return);
3523
3524 // Test for 0.5.
3525 __ bind(&not_minus_half);
3526 // Load xmm2 with 0.5.
3527 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
3528 __ addsd(xmm2, xmm3);
3529 // xmm2 now has 0.5.
3530 __ ucomisd(xmm2, xmm1);
3531 __ j(not_equal, &call_runtime);
3532 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00003533 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3534 __ xorpd(xmm1, xmm1);
3535 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003536 __ sqrtsd(xmm1, xmm1);
3537
3538 __ bind(&allocate_return);
3539 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
3540 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
3541 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00003542 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003543
3544 __ bind(&call_runtime);
3545 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3546}
3547
3548
ricow@chromium.org65fae842010-08-25 15:26:24 +00003549void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3550 // The key is in edx and the parameter count is in eax.
3551
3552 // The displacement is used for skipping the frame pointer on the
3553 // stack. It is the offset of the last parameter (if any) relative
3554 // to the frame pointer.
3555 static const int kDisplacement = 1 * kPointerSize;
3556
3557 // Check that the key is a smi.
3558 Label slow;
3559 __ test(edx, Immediate(kSmiTagMask));
3560 __ j(not_zero, &slow, not_taken);
3561
3562 // Check if the calling frame is an arguments adaptor frame.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003563 NearLabel adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003564 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3565 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
3566 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3567 __ j(equal, &adaptor);
3568
3569 // Check index against formal parameters count limit passed in
3570 // through register eax. Use unsigned comparison to get negative
3571 // check for free.
3572 __ cmp(edx, Operand(eax));
3573 __ j(above_equal, &slow, not_taken);
3574
3575 // Read the argument from the stack and return it.
3576 STATIC_ASSERT(kSmiTagSize == 1);
3577 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3578 __ lea(ebx, Operand(ebp, eax, times_2, 0));
3579 __ neg(edx);
3580 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3581 __ ret(0);
3582
3583 // Arguments adaptor case: Check index against actual arguments
3584 // limit found in the arguments adaptor frame. Use unsigned
3585 // comparison to get negative check for free.
3586 __ bind(&adaptor);
3587 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3588 __ cmp(edx, Operand(ecx));
3589 __ j(above_equal, &slow, not_taken);
3590
3591 // Read the argument from the stack and return it.
3592 STATIC_ASSERT(kSmiTagSize == 1);
3593 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3594 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
3595 __ neg(edx);
3596 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3597 __ ret(0);
3598
3599 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3600 // by calling the runtime system.
3601 __ bind(&slow);
3602 __ pop(ebx); // Return address.
3603 __ push(edx);
3604 __ push(ebx);
3605 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3606}
3607
3608
3609void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3610 // esp[0] : return address
3611 // esp[4] : number of parameters
3612 // esp[8] : receiver displacement
3613 // esp[16] : function
3614
3615 // The displacement is used for skipping the return address and the
3616 // frame pointer on the stack. It is the offset of the last
3617 // parameter (if any) relative to the frame pointer.
3618 static const int kDisplacement = 2 * kPointerSize;
3619
3620 // Check if the calling frame is an arguments adaptor frame.
3621 Label adaptor_frame, try_allocate, runtime;
3622 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3623 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
3624 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3625 __ j(equal, &adaptor_frame);
3626
3627 // Get the length from the frame.
3628 __ mov(ecx, Operand(esp, 1 * kPointerSize));
3629 __ jmp(&try_allocate);
3630
3631 // Patch the arguments.length and the parameters pointer.
3632 __ bind(&adaptor_frame);
3633 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3634 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3635 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
3636 __ mov(Operand(esp, 2 * kPointerSize), edx);
3637
3638 // Try the new space allocation. Start out with computing the size of
3639 // the arguments object and the elements array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003640 NearLabel add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003641 __ bind(&try_allocate);
3642 __ test(ecx, Operand(ecx));
3643 __ j(zero, &add_arguments_object);
3644 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3645 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003646 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003647
3648 // Do the allocation of both objects in one go.
3649 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3650
3651 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003652 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3653 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003654 __ mov(edi, Operand(edi,
3655 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003656
3657 // Copy the JS object part.
3658 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3659 __ mov(ebx, FieldOperand(edi, i));
3660 __ mov(FieldOperand(eax, i), ebx);
3661 }
3662
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003663 if (type_ == NEW_NON_STRICT) {
3664 // Setup the callee in-object property.
3665 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3666 __ mov(ebx, Operand(esp, 3 * kPointerSize));
3667 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3668 Heap::kArgumentsCalleeIndex * kPointerSize),
3669 ebx);
3670 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003671
3672 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003673 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003674 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003675 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3676 Heap::kArgumentsLengthIndex * kPointerSize),
3677 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003678
3679 // If there are no actual arguments, we're done.
3680 Label done;
3681 __ test(ecx, Operand(ecx));
3682 __ j(zero, &done);
3683
3684 // Get the parameters pointer from the stack.
3685 __ mov(edx, Operand(esp, 2 * kPointerSize));
3686
3687 // Setup the elements pointer in the allocated arguments object and
3688 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003689 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003690 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3691 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003692 Immediate(FACTORY->fixed_array_map()));
3693
ricow@chromium.org65fae842010-08-25 15:26:24 +00003694 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3695 // Untag the length for the loop below.
3696 __ SmiUntag(ecx);
3697
3698 // Copy the fixed array slots.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003699 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003700 __ bind(&loop);
3701 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3702 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3703 __ add(Operand(edi), Immediate(kPointerSize));
3704 __ sub(Operand(edx), Immediate(kPointerSize));
3705 __ dec(ecx);
3706 __ j(not_zero, &loop);
3707
3708 // Return and remove the on-stack parameters.
3709 __ bind(&done);
3710 __ ret(3 * kPointerSize);
3711
3712 // Do the runtime call to allocate the arguments object.
3713 __ bind(&runtime);
3714 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3715}
3716
3717
3718void RegExpExecStub::Generate(MacroAssembler* masm) {
3719 // Just jump directly to runtime if native RegExp is not selected at compile
3720 // time or if regexp entry in generated code is turned off runtime switch or
3721 // at compilation.
3722#ifdef V8_INTERPRETED_REGEXP
3723 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3724#else // V8_INTERPRETED_REGEXP
3725 if (!FLAG_regexp_entry_native) {
3726 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3727 return;
3728 }
3729
3730 // Stack frame on entry.
3731 // esp[0]: return address
3732 // esp[4]: last_match_info (expected JSArray)
3733 // esp[8]: previous index
3734 // esp[12]: subject string
3735 // esp[16]: JSRegExp object
3736
3737 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3738 static const int kPreviousIndexOffset = 2 * kPointerSize;
3739 static const int kSubjectOffset = 3 * kPointerSize;
3740 static const int kJSRegExpOffset = 4 * kPointerSize;
3741
3742 Label runtime, invoke_regexp;
3743
3744 // Ensure that a RegExp stack is allocated.
3745 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003746 ExternalReference::address_of_regexp_stack_memory_address(
3747 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003748 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003749 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003750 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3751 __ test(ebx, Operand(ebx));
3752 __ j(zero, &runtime, not_taken);
3753
3754 // Check that the first argument is a JSRegExp object.
3755 __ mov(eax, Operand(esp, kJSRegExpOffset));
3756 STATIC_ASSERT(kSmiTag == 0);
3757 __ test(eax, Immediate(kSmiTagMask));
3758 __ j(zero, &runtime);
3759 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3760 __ j(not_equal, &runtime);
3761 // Check that the RegExp has been compiled (data contains a fixed array).
3762 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3763 if (FLAG_debug_code) {
3764 __ test(ecx, Immediate(kSmiTagMask));
3765 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3766 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3767 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3768 }
3769
3770 // ecx: RegExp data (FixedArray)
3771 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3772 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3773 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3774 __ j(not_equal, &runtime);
3775
3776 // ecx: RegExp data (FixedArray)
3777 // Check that the number of captures fit in the static offsets vector buffer.
3778 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3779 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3780 // uses the asumption that smis are 2 * their untagged value.
3781 STATIC_ASSERT(kSmiTag == 0);
3782 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3783 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3784 // Check that the static offsets vector buffer is large enough.
3785 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3786 __ j(above, &runtime);
3787
3788 // ecx: RegExp data (FixedArray)
3789 // edx: Number of capture registers
3790 // Check that the second argument is a string.
3791 __ mov(eax, Operand(esp, kSubjectOffset));
3792 __ test(eax, Immediate(kSmiTagMask));
3793 __ j(zero, &runtime);
3794 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3795 __ j(NegateCondition(is_string), &runtime);
3796 // Get the length of the string to ebx.
3797 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3798
3799 // ebx: Length of subject string as a smi
3800 // ecx: RegExp data (FixedArray)
3801 // edx: Number of capture registers
3802 // Check that the third argument is a positive smi less than the subject
3803 // string length. A negative value will be greater (unsigned comparison).
3804 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3805 __ test(eax, Immediate(kSmiTagMask));
3806 __ j(not_zero, &runtime);
3807 __ cmp(eax, Operand(ebx));
3808 __ j(above_equal, &runtime);
3809
3810 // ecx: RegExp data (FixedArray)
3811 // edx: Number of capture registers
3812 // Check that the fourth object is a JSArray object.
3813 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3814 __ test(eax, Immediate(kSmiTagMask));
3815 __ j(zero, &runtime);
3816 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3817 __ j(not_equal, &runtime);
3818 // Check that the JSArray is in fast case.
3819 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3820 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003821 __ cmp(eax, FACTORY->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003822 __ j(not_equal, &runtime);
3823 // Check that the last match info has space for the capture registers and the
3824 // additional information.
3825 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3826 __ SmiUntag(eax);
3827 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3828 __ cmp(edx, Operand(eax));
3829 __ j(greater, &runtime);
3830
3831 // ecx: RegExp data (FixedArray)
3832 // Check the representation and encoding of the subject string.
3833 Label seq_ascii_string, seq_two_byte_string, check_code;
3834 __ mov(eax, Operand(esp, kSubjectOffset));
3835 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3836 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3837 // First check for flat two byte string.
3838 __ and_(ebx,
3839 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3840 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3841 __ j(zero, &seq_two_byte_string);
3842 // Any other flat string must be a flat ascii string.
3843 __ test(Operand(ebx),
3844 Immediate(kIsNotStringMask | kStringRepresentationMask));
3845 __ j(zero, &seq_ascii_string);
3846
3847 // Check for flat cons string.
3848 // A flat cons string is a cons string where the second part is the empty
3849 // string. In that case the subject string is just the first part of the cons
3850 // string. Also in this case the first part of the cons string is known to be
3851 // a sequential string or an external string.
3852 STATIC_ASSERT(kExternalStringTag != 0);
3853 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3854 __ test(Operand(ebx),
3855 Immediate(kIsNotStringMask | kExternalStringTag));
3856 __ j(not_zero, &runtime);
3857 // String is a cons string.
3858 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003859 __ cmp(Operand(edx), FACTORY->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003860 __ j(not_equal, &runtime);
3861 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3862 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3863 // String is a cons string with empty second part.
3864 // eax: first part of cons string.
3865 // ebx: map of first part of cons string.
3866 // Is first part a flat two byte string?
3867 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3868 kStringRepresentationMask | kStringEncodingMask);
3869 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3870 __ j(zero, &seq_two_byte_string);
3871 // Any other flat string must be ascii.
3872 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3873 kStringRepresentationMask);
3874 __ j(not_zero, &runtime);
3875
3876 __ bind(&seq_ascii_string);
3877 // eax: subject string (flat ascii)
3878 // ecx: RegExp data (FixedArray)
3879 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3880 __ Set(edi, Immediate(1)); // Type is ascii.
3881 __ jmp(&check_code);
3882
3883 __ bind(&seq_two_byte_string);
3884 // eax: subject string (flat two byte)
3885 // ecx: RegExp data (FixedArray)
3886 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3887 __ Set(edi, Immediate(0)); // Type is two byte.
3888
3889 __ bind(&check_code);
3890 // Check that the irregexp code has been generated for the actual string
3891 // encoding. If it has, the field contains a code object otherwise it contains
3892 // the hole.
3893 __ CmpObjectType(edx, CODE_TYPE, ebx);
3894 __ j(not_equal, &runtime);
3895
3896 // eax: subject string
3897 // edx: code
3898 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3899 // Load used arguments before starting to push arguments for call to native
3900 // RegExp code to avoid handling changing stack height.
3901 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3902 __ SmiUntag(ebx); // Previous index from smi.
3903
3904 // eax: subject string
3905 // ebx: previous index
3906 // edx: code
3907 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3908 // All checks done. Now push arguments for native regexp code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003909 __ IncrementCounter(COUNTERS->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003910
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003911 // Isolates: note we add an additional parameter here (isolate pointer).
3912 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003913 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003914
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003915 // Argument 8: Pass current isolate address.
3916 __ mov(Operand(esp, 7 * kPointerSize),
3917 Immediate(ExternalReference::isolate_address()));
3918
ricow@chromium.org65fae842010-08-25 15:26:24 +00003919 // Argument 7: Indicate that this is a direct call from JavaScript.
3920 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3921
3922 // Argument 6: Start (high end) of backtracking stack memory area.
3923 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3924 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3925 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3926
3927 // Argument 5: static offsets vector buffer.
3928 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003929 Immediate(ExternalReference::address_of_static_offsets_vector(
3930 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003931
3932 // Argument 4: End of string data
3933 // Argument 3: Start of string data
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003934 NearLabel setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003935 __ test(edi, Operand(edi));
3936 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
3937 __ j(zero, &setup_two_byte);
3938 __ SmiUntag(edi);
3939 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3940 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3941 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3942 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3943 __ jmp(&setup_rest);
3944
3945 __ bind(&setup_two_byte);
3946 STATIC_ASSERT(kSmiTag == 0);
3947 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3948 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3949 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3950 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3951 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3952
3953 __ bind(&setup_rest);
3954
3955 // Argument 2: Previous index.
3956 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3957
3958 // Argument 1: Subject string.
3959 __ mov(Operand(esp, 0 * kPointerSize), eax);
3960
3961 // Locate the code entry and call it.
3962 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003963 __ call(Operand(edx));
3964
3965 // Drop arguments and come back to JS mode.
3966 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003967
3968 // Check the result.
3969 Label success;
3970 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
3971 __ j(equal, &success, taken);
3972 Label failure;
3973 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
3974 __ j(equal, &failure, taken);
3975 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3976 // If not exception it can only be retry. Handle that in the runtime system.
3977 __ j(not_equal, &runtime);
3978 // Result must now be exception. If there is no pending exception already a
3979 // stack overflow (on the backtrack stack) was detected in RegExp code but
3980 // haven't created the exception yet. Handle that in the runtime system.
3981 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003982 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3983 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003984 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003985 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3986 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003987 __ mov(eax, Operand::StaticVariable(pending_exception));
3988 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003989 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003990 // For exception, throw the exception again.
3991
3992 // Clear the pending exception variable.
3993 __ mov(Operand::StaticVariable(pending_exception), edx);
3994
3995 // Special handling of termination exceptions which are uncatchable
3996 // by javascript code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003997 __ cmp(eax, FACTORY->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003998 Label throw_termination_exception;
3999 __ j(equal, &throw_termination_exception);
4000
4001 // Handle normal exception by following handler chain.
4002 __ Throw(eax);
4003
4004 __ bind(&throw_termination_exception);
4005 __ ThrowUncatchable(TERMINATION, eax);
4006
ricow@chromium.org65fae842010-08-25 15:26:24 +00004007 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004008 // For failure to match, return null.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004009 __ mov(Operand(eax), FACTORY->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004010 __ ret(4 * kPointerSize);
4011
4012 // Load RegExp data.
4013 __ bind(&success);
4014 __ mov(eax, Operand(esp, kJSRegExpOffset));
4015 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
4016 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
4017 // Calculate number of capture registers (number_of_captures + 1) * 2.
4018 STATIC_ASSERT(kSmiTag == 0);
4019 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4020 __ add(Operand(edx), Immediate(2)); // edx was a smi.
4021
4022 // edx: Number of capture registers
4023 // Load last_match_info which is still known to be a fast case JSArray.
4024 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4025 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
4026
4027 // ebx: last_match_info backing store (FixedArray)
4028 // edx: number of capture registers
4029 // Store the capture count.
4030 __ SmiTag(edx); // Number of capture registers to smi.
4031 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
4032 __ SmiUntag(edx); // Number of capture registers back from smi.
4033 // Store last subject and last input.
4034 __ mov(eax, Operand(esp, kSubjectOffset));
4035 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
4036 __ mov(ecx, ebx);
4037 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
4038 __ mov(eax, Operand(esp, kSubjectOffset));
4039 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
4040 __ mov(ecx, ebx);
4041 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
4042
4043 // Get the static offsets vector filled by the native regexp code.
4044 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004045 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004046 __ mov(ecx, Immediate(address_of_static_offsets_vector));
4047
4048 // ebx: last_match_info backing store (FixedArray)
4049 // ecx: offsets vector
4050 // edx: number of capture registers
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004051 NearLabel next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004052 // Capture register counter starts from number of capture registers and
4053 // counts down until wraping after zero.
4054 __ bind(&next_capture);
4055 __ sub(Operand(edx), Immediate(1));
4056 __ j(negative, &done);
4057 // Read the value from the static offsets vector buffer.
4058 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
4059 __ SmiTag(edi);
4060 // Store the smi value in the last match info.
4061 __ mov(FieldOperand(ebx,
4062 edx,
4063 times_pointer_size,
4064 RegExpImpl::kFirstCaptureOffset),
4065 edi);
4066 __ jmp(&next_capture);
4067 __ bind(&done);
4068
4069 // Return last match info.
4070 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
4071 __ ret(4 * kPointerSize);
4072
4073 // Do the runtime call to execute the regexp.
4074 __ bind(&runtime);
4075 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4076#endif // V8_INTERPRETED_REGEXP
4077}
4078
4079
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004080void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4081 const int kMaxInlineLength = 100;
4082 Label slowcase;
4083 NearLabel done;
4084 __ mov(ebx, Operand(esp, kPointerSize * 3));
4085 __ test(ebx, Immediate(kSmiTagMask));
4086 __ j(not_zero, &slowcase);
4087 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
4088 __ j(above, &slowcase);
4089 // Smi-tagging is equivalent to multiplying by 2.
4090 STATIC_ASSERT(kSmiTag == 0);
4091 STATIC_ASSERT(kSmiTagSize == 1);
4092 // Allocate RegExpResult followed by FixedArray with size in ebx.
4093 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4094 // Elements: [Map][Length][..elements..]
4095 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
4096 times_half_pointer_size,
4097 ebx, // In: Number of elements (times 2, being a smi)
4098 eax, // Out: Start of allocation (tagged).
4099 ecx, // Out: End of allocation.
4100 edx, // Scratch register
4101 &slowcase,
4102 TAG_OBJECT);
4103 // eax: Start of allocated area, object-tagged.
4104
4105 // Set JSArray map to global.regexp_result_map().
4106 // Set empty properties FixedArray.
4107 // Set elements to point to FixedArray allocated right after the JSArray.
4108 // Interleave operations for better latency.
4109 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004110 __ mov(ecx, Immediate(FACTORY->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004111 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
4112 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
4113 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
4114 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
4115 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
4116 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
4117
4118 // Set input, index and length fields from arguments.
4119 __ mov(ecx, Operand(esp, kPointerSize * 1));
4120 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
4121 __ mov(ecx, Operand(esp, kPointerSize * 2));
4122 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
4123 __ mov(ecx, Operand(esp, kPointerSize * 3));
4124 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
4125
4126 // Fill out the elements FixedArray.
4127 // eax: JSArray.
4128 // ebx: FixedArray.
4129 // ecx: Number of elements in array, as smi.
4130
4131 // Set map.
4132 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004133 Immediate(FACTORY->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004134 // Set length.
4135 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
4136 // Fill contents of fixed-array with the-hole.
4137 __ SmiUntag(ecx);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004138 __ mov(edx, Immediate(FACTORY->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004139 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
4140 // Fill fixed array elements with hole.
4141 // eax: JSArray.
4142 // ecx: Number of elements to fill.
4143 // ebx: Start of elements in FixedArray.
4144 // edx: the hole.
4145 Label loop;
4146 __ test(ecx, Operand(ecx));
4147 __ bind(&loop);
4148 __ j(less_equal, &done); // Jump if ecx is negative or zero.
4149 __ sub(Operand(ecx), Immediate(1));
4150 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
4151 __ jmp(&loop);
4152
4153 __ bind(&done);
4154 __ ret(3 * kPointerSize);
4155
4156 __ bind(&slowcase);
4157 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4158}
4159
4160
ricow@chromium.org65fae842010-08-25 15:26:24 +00004161void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
4162 Register object,
4163 Register result,
4164 Register scratch1,
4165 Register scratch2,
4166 bool object_is_smi,
4167 Label* not_found) {
4168 // Use of registers. Register result is used as a temporary.
4169 Register number_string_cache = result;
4170 Register mask = scratch1;
4171 Register scratch = scratch2;
4172
4173 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004174 ExternalReference roots_address =
4175 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004176 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
4177 __ mov(number_string_cache,
4178 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4179 // Make the hash mask from the length of the number string cache. It
4180 // contains two elements (number and string) for each cache entry.
4181 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
4182 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
4183 __ sub(Operand(mask), Immediate(1)); // Make mask.
4184
4185 // Calculate the entry in the number string cache. The hash value in the
4186 // number string cache for smis is just the smi value, and the hash for
4187 // doubles is the xor of the upper and lower words. See
4188 // Heap::GetNumberStringCache.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004189 NearLabel smi_hash_calculated;
4190 NearLabel load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004191 if (object_is_smi) {
4192 __ mov(scratch, object);
4193 __ SmiUntag(scratch);
4194 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004195 NearLabel not_smi, hash_calculated;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004196 STATIC_ASSERT(kSmiTag == 0);
4197 __ test(object, Immediate(kSmiTagMask));
4198 __ j(not_zero, &not_smi);
4199 __ mov(scratch, object);
4200 __ SmiUntag(scratch);
4201 __ jmp(&smi_hash_calculated);
4202 __ bind(&not_smi);
4203 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004204 FACTORY->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004205 __ j(not_equal, not_found);
4206 STATIC_ASSERT(8 == kDoubleSize);
4207 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
4208 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
4209 // Object is heap number and hash is now in scratch. Calculate cache index.
4210 __ and_(scratch, Operand(mask));
4211 Register index = scratch;
4212 Register probe = mask;
4213 __ mov(probe,
4214 FieldOperand(number_string_cache,
4215 index,
4216 times_twice_pointer_size,
4217 FixedArray::kHeaderSize));
4218 __ test(probe, Immediate(kSmiTagMask));
4219 __ j(zero, not_found);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004220 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004221 CpuFeatures::Scope fscope(SSE2);
4222 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
4223 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
4224 __ ucomisd(xmm0, xmm1);
4225 } else {
4226 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
4227 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
4228 __ FCmp();
4229 }
4230 __ j(parity_even, not_found); // Bail out if NaN is involved.
4231 __ j(not_equal, not_found); // The cache did not contain this value.
4232 __ jmp(&load_result_from_cache);
4233 }
4234
4235 __ bind(&smi_hash_calculated);
4236 // Object is smi and hash is now in scratch. Calculate cache index.
4237 __ and_(scratch, Operand(mask));
4238 Register index = scratch;
4239 // Check if the entry is the smi we are looking for.
4240 __ cmp(object,
4241 FieldOperand(number_string_cache,
4242 index,
4243 times_twice_pointer_size,
4244 FixedArray::kHeaderSize));
4245 __ j(not_equal, not_found);
4246
4247 // Get the result from the cache.
4248 __ bind(&load_result_from_cache);
4249 __ mov(result,
4250 FieldOperand(number_string_cache,
4251 index,
4252 times_twice_pointer_size,
4253 FixedArray::kHeaderSize + kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004254 __ IncrementCounter(COUNTERS->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004255}
4256
4257
4258void NumberToStringStub::Generate(MacroAssembler* masm) {
4259 Label runtime;
4260
4261 __ mov(ebx, Operand(esp, kPointerSize));
4262
4263 // Generate code to lookup number in the number string cache.
4264 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
4265 __ ret(1 * kPointerSize);
4266
4267 __ bind(&runtime);
4268 // Handle number to string in the runtime system if not found in the cache.
4269 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
4270}
4271
4272
4273static int NegativeComparisonResult(Condition cc) {
4274 ASSERT(cc != equal);
4275 ASSERT((cc == less) || (cc == less_equal)
4276 || (cc == greater) || (cc == greater_equal));
4277 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
4278}
4279
4280void CompareStub::Generate(MacroAssembler* masm) {
4281 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4282
4283 Label check_unequal_objects, done;
4284
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004285 // Compare two smis if required.
4286 if (include_smi_compare_) {
4287 Label non_smi, smi_done;
4288 __ mov(ecx, Operand(edx));
4289 __ or_(ecx, Operand(eax));
4290 __ test(ecx, Immediate(kSmiTagMask));
4291 __ j(not_zero, &non_smi, not_taken);
4292 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
4293 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004294 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004295 __ bind(&smi_done);
4296 __ mov(eax, edx);
4297 __ ret(0);
4298 __ bind(&non_smi);
4299 } else if (FLAG_debug_code) {
4300 __ mov(ecx, Operand(edx));
4301 __ or_(ecx, Operand(eax));
4302 __ test(ecx, Immediate(kSmiTagMask));
4303 __ Assert(not_zero, "Unexpected smi operands.");
4304 }
4305
ricow@chromium.org65fae842010-08-25 15:26:24 +00004306 // NOTICE! This code is only reached after a smi-fast-case check, so
4307 // it is certain that at least one operand isn't a smi.
4308
4309 // Identical objects can be compared fast, but there are some tricky cases
4310 // for NaN and undefined.
4311 {
4312 Label not_identical;
4313 __ cmp(eax, Operand(edx));
4314 __ j(not_equal, &not_identical);
4315
4316 if (cc_ != equal) {
4317 // Check for undefined. undefined OP undefined is false even though
4318 // undefined == undefined.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004319 NearLabel check_for_nan;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004320 __ cmp(edx, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004321 __ j(not_equal, &check_for_nan);
4322 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4323 __ ret(0);
4324 __ bind(&check_for_nan);
4325 }
4326
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004327 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00004328 // so we do the second best thing - test it ourselves.
4329 // Note: if cc_ != equal, never_nan_nan_ is not used.
4330 if (never_nan_nan_ && (cc_ == equal)) {
4331 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4332 __ ret(0);
4333 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004334 NearLabel heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004335 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004336 Immediate(FACTORY->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004337 __ j(equal, &heap_number);
4338 if (cc_ != equal) {
4339 // Call runtime on identical JSObjects. Otherwise return equal.
4340 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4341 __ j(above_equal, &not_identical);
4342 }
4343 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4344 __ ret(0);
4345
4346 __ bind(&heap_number);
4347 // It is a heap number, so return non-equal if it's NaN and equal if
4348 // it's not NaN.
4349 // The representation of NaN values has all exponent bits (52..62) set,
4350 // and not all mantissa bits (0..51) clear.
4351 // We only accept QNaNs, which have bit 51 set.
4352 // Read top bits of double representation (second word of value).
4353
4354 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
4355 // all bits in the mask are set. We only need to check the word
4356 // that contains the exponent and high bit of the mantissa.
4357 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
4358 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004359 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004360 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
4361 // bits.
4362 __ add(edx, Operand(edx));
4363 __ cmp(edx, kQuietNaNHighBitsMask << 1);
4364 if (cc_ == equal) {
4365 STATIC_ASSERT(EQUAL != 1);
4366 __ setcc(above_equal, eax);
4367 __ ret(0);
4368 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004369 NearLabel nan;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004370 __ j(above_equal, &nan);
4371 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4372 __ ret(0);
4373 __ bind(&nan);
4374 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4375 __ ret(0);
4376 }
4377 }
4378
4379 __ bind(&not_identical);
4380 }
4381
4382 // Strict equality can quickly decide whether objects are equal.
4383 // Non-strict object equality is slower, so it is handled later in the stub.
4384 if (cc_ == equal && strict_) {
4385 Label slow; // Fallthrough label.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004386 NearLabel not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004387 // If we're doing a strict equality comparison, we don't have to do
4388 // type conversion, so we generate code to do fast comparison for objects
4389 // and oddballs. Non-smi numbers and strings still go through the usual
4390 // slow-case code.
4391 // If either is a Smi (we know that not both are), then they can only
4392 // be equal if the other is a HeapNumber. If so, use the slow case.
4393 STATIC_ASSERT(kSmiTag == 0);
4394 ASSERT_EQ(0, Smi::FromInt(0));
4395 __ mov(ecx, Immediate(kSmiTagMask));
4396 __ and_(ecx, Operand(eax));
4397 __ test(ecx, Operand(edx));
4398 __ j(not_zero, &not_smis);
4399 // One operand is a smi.
4400
4401 // Check whether the non-smi is a heap number.
4402 STATIC_ASSERT(kSmiTagMask == 1);
4403 // ecx still holds eax & kSmiTag, which is either zero or one.
4404 __ sub(Operand(ecx), Immediate(0x01));
4405 __ mov(ebx, edx);
4406 __ xor_(ebx, Operand(eax));
4407 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
4408 __ xor_(ebx, Operand(eax));
4409 // if eax was smi, ebx is now edx, else eax.
4410
4411 // Check if the non-smi operand is a heap number.
4412 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004413 Immediate(FACTORY->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004414 // If heap number, handle it in the slow case.
4415 __ j(equal, &slow);
4416 // Return non-equal (ebx is not zero)
4417 __ mov(eax, ebx);
4418 __ ret(0);
4419
4420 __ bind(&not_smis);
4421 // If either operand is a JSObject or an oddball value, then they are not
4422 // equal since their pointers are different
4423 // There is no test for undetectability in strict equality.
4424
4425 // Get the type of the first operand.
4426 // If the first object is a JS object, we have done pointer comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004427 NearLabel first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004428 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
4429 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4430 __ j(below, &first_non_object);
4431
4432 // Return non-zero (eax is not zero)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004433 NearLabel return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004434 STATIC_ASSERT(kHeapObjectTag != 0);
4435 __ bind(&return_not_equal);
4436 __ ret(0);
4437
4438 __ bind(&first_non_object);
4439 // Check for oddballs: true, false, null, undefined.
4440 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4441 __ j(equal, &return_not_equal);
4442
4443 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
4444 __ j(above_equal, &return_not_equal);
4445
4446 // Check for oddballs: true, false, null, undefined.
4447 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4448 __ j(equal, &return_not_equal);
4449
4450 // Fall through to the general case.
4451 __ bind(&slow);
4452 }
4453
4454 // Generate the number comparison code.
4455 if (include_number_compare_) {
4456 Label non_number_comparison;
4457 Label unordered;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004458 if (masm->isolate()->cpu_features()->IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004459 CpuFeatures::Scope use_sse2(SSE2);
4460 CpuFeatures::Scope use_cmov(CMOV);
4461
4462 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
4463 __ ucomisd(xmm0, xmm1);
4464
4465 // Don't base result on EFLAGS when a NaN is involved.
4466 __ j(parity_even, &unordered, not_taken);
4467 // Return a result of -1, 0, or 1, based on EFLAGS.
4468 __ mov(eax, 0); // equal
4469 __ mov(ecx, Immediate(Smi::FromInt(1)));
4470 __ cmov(above, eax, Operand(ecx));
4471 __ mov(ecx, Immediate(Smi::FromInt(-1)));
4472 __ cmov(below, eax, Operand(ecx));
4473 __ ret(0);
4474 } else {
4475 FloatingPointHelper::CheckFloatOperands(
4476 masm, &non_number_comparison, ebx);
4477 FloatingPointHelper::LoadFloatOperand(masm, eax);
4478 FloatingPointHelper::LoadFloatOperand(masm, edx);
4479 __ FCmp();
4480
4481 // Don't base result on EFLAGS when a NaN is involved.
4482 __ j(parity_even, &unordered, not_taken);
4483
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004484 NearLabel below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004485 // Return a result of -1, 0, or 1, based on EFLAGS.
4486 __ j(below, &below_label, not_taken);
4487 __ j(above, &above_label, not_taken);
4488
lrn@chromium.org5d00b602011-01-05 09:51:43 +00004489 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004490 __ ret(0);
4491
4492 __ bind(&below_label);
4493 __ mov(eax, Immediate(Smi::FromInt(-1)));
4494 __ ret(0);
4495
4496 __ bind(&above_label);
4497 __ mov(eax, Immediate(Smi::FromInt(1)));
4498 __ ret(0);
4499 }
4500
4501 // If one of the numbers was NaN, then the result is always false.
4502 // The cc is never not-equal.
4503 __ bind(&unordered);
4504 ASSERT(cc_ != not_equal);
4505 if (cc_ == less || cc_ == less_equal) {
4506 __ mov(eax, Immediate(Smi::FromInt(1)));
4507 } else {
4508 __ mov(eax, Immediate(Smi::FromInt(-1)));
4509 }
4510 __ ret(0);
4511
4512 // The number comparison code did not provide a valid result.
4513 __ bind(&non_number_comparison);
4514 }
4515
4516 // Fast negative check for symbol-to-symbol equality.
4517 Label check_for_strings;
4518 if (cc_ == equal) {
4519 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4520 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4521
4522 // We've already checked for object identity, so if both operands
4523 // are symbols they aren't equal. Register eax already holds a
4524 // non-zero value, which indicates not equal, so just return.
4525 __ ret(0);
4526 }
4527
4528 __ bind(&check_for_strings);
4529
4530 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4531 &check_unequal_objects);
4532
4533 // Inline comparison of ascii strings.
4534 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4535 edx,
4536 eax,
4537 ecx,
4538 ebx,
4539 edi);
4540#ifdef DEBUG
4541 __ Abort("Unexpected fall-through from string comparison");
4542#endif
4543
4544 __ bind(&check_unequal_objects);
4545 if (cc_ == equal && !strict_) {
4546 // Non-strict equality. Objects are unequal if
4547 // they are both JSObjects and not undetectable,
4548 // and their pointers are different.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004549 NearLabel not_both_objects;
4550 NearLabel return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004551 // At most one is a smi, so we can test for smi by adding the two.
4552 // A smi plus a heap object has the low bit set, a heap object plus
4553 // a heap object has the low bit clear.
4554 STATIC_ASSERT(kSmiTag == 0);
4555 STATIC_ASSERT(kSmiTagMask == 1);
4556 __ lea(ecx, Operand(eax, edx, times_1, 0));
4557 __ test(ecx, Immediate(kSmiTagMask));
4558 __ j(not_zero, &not_both_objects);
4559 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
4560 __ j(below, &not_both_objects);
4561 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
4562 __ j(below, &not_both_objects);
4563 // We do not bail out after this point. Both are JSObjects, and
4564 // they are equal if and only if both are undetectable.
4565 // The and of the undetectable flags is 1 if and only if they are equal.
4566 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4567 1 << Map::kIsUndetectable);
4568 __ j(zero, &return_unequal);
4569 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4570 1 << Map::kIsUndetectable);
4571 __ j(zero, &return_unequal);
4572 // The objects are both undetectable, so they both compare as the value
4573 // undefined, and are equal.
4574 __ Set(eax, Immediate(EQUAL));
4575 __ bind(&return_unequal);
4576 // Return non-equal by returning the non-zero object pointer in eax,
4577 // or return equal if we fell through to here.
4578 __ ret(0); // rax, rdx were pushed
4579 __ bind(&not_both_objects);
4580 }
4581
4582 // Push arguments below the return address.
4583 __ pop(ecx);
4584 __ push(edx);
4585 __ push(eax);
4586
4587 // Figure out which native to call and setup the arguments.
4588 Builtins::JavaScript builtin;
4589 if (cc_ == equal) {
4590 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4591 } else {
4592 builtin = Builtins::COMPARE;
4593 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4594 }
4595
4596 // Restore return address on the stack.
4597 __ push(ecx);
4598
4599 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4600 // tagged as a small integer.
4601 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4602}
4603
4604
4605void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4606 Label* label,
4607 Register object,
4608 Register scratch) {
4609 __ test(object, Immediate(kSmiTagMask));
4610 __ j(zero, label);
4611 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4612 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4613 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4614 __ cmp(scratch, kSymbolTag | kStringTag);
4615 __ j(not_equal, label);
4616}
4617
4618
4619void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004620 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004621}
4622
4623
4624void CallFunctionStub::Generate(MacroAssembler* masm) {
4625 Label slow;
4626
4627 // If the receiver might be a value (string, number or boolean) check for this
4628 // and box it if it is.
4629 if (ReceiverMightBeValue()) {
4630 // Get the receiver from the stack.
4631 // +1 ~ return address
4632 Label receiver_is_value, receiver_is_js_object;
4633 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
4634
4635 // Check if receiver is a smi (which is a number value).
4636 __ test(eax, Immediate(kSmiTagMask));
4637 __ j(zero, &receiver_is_value, not_taken);
4638
4639 // Check if the receiver is a valid JS object.
4640 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
4641 __ j(above_equal, &receiver_is_js_object);
4642
4643 // Call the runtime to box the value.
4644 __ bind(&receiver_is_value);
4645 __ EnterInternalFrame();
4646 __ push(eax);
4647 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
4648 __ LeaveInternalFrame();
4649 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
4650
4651 __ bind(&receiver_is_js_object);
4652 }
4653
4654 // Get the function to call from the stack.
4655 // +2 ~ receiver, return address
4656 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4657
4658 // Check that the function really is a JavaScript function.
4659 __ test(edi, Immediate(kSmiTagMask));
4660 __ j(zero, &slow, not_taken);
4661 // Goto slow case if we do not have a function.
4662 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
4663 __ j(not_equal, &slow, not_taken);
4664
4665 // Fast-case: Just invoke the function.
4666 ParameterCount actual(argc_);
4667 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
4668
4669 // Slow-case: Non-function called.
4670 __ bind(&slow);
4671 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4672 // of the original receiver from the call site).
4673 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4674 __ Set(eax, Immediate(argc_));
4675 __ Set(ebx, Immediate(0));
4676 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004677 Handle<Code> adaptor(masm->isolate()->builtins()->builtin(
4678 Builtins::ArgumentsAdaptorTrampoline));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004679 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4680}
4681
4682
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004683bool CEntryStub::NeedsImmovableCode() {
4684 return false;
4685}
4686
4687
ricow@chromium.org65fae842010-08-25 15:26:24 +00004688void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004689 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004690}
4691
4692
ricow@chromium.org65fae842010-08-25 15:26:24 +00004693void CEntryStub::GenerateCore(MacroAssembler* masm,
4694 Label* throw_normal_exception,
4695 Label* throw_termination_exception,
4696 Label* throw_out_of_memory_exception,
4697 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004698 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004699 // eax: result parameter for PerformGC, if any
4700 // ebx: pointer to C function (C callee-saved)
4701 // ebp: frame pointer (restored after C call)
4702 // esp: stack pointer (restored after C call)
4703 // edi: number of arguments including receiver (C callee-saved)
4704 // esi: pointer to the first argument (C callee-saved)
4705
4706 // Result returned in eax, or eax+edx if result_size_ is 2.
4707
4708 // Check stack alignment.
4709 if (FLAG_debug_code) {
4710 __ CheckStackAlignment();
4711 }
4712
4713 if (do_gc) {
4714 // Pass failure code returned from last attempt as first argument to
4715 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4716 // stack alignment is known to be correct. This function takes one argument
4717 // which is passed on the stack, and we know that the stack has been
4718 // prepared to pass at least one argument.
4719 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4720 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4721 }
4722
4723 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004724 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004725 if (always_allocate_scope) {
4726 __ inc(Operand::StaticVariable(scope_depth));
4727 }
4728
4729 // Call C function.
4730 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4731 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004732 __ mov(Operand(esp, 2 * kPointerSize),
4733 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004734 __ call(Operand(ebx));
4735 // Result is in eax or edx:eax - do not destroy these registers!
4736
4737 if (always_allocate_scope) {
4738 __ dec(Operand::StaticVariable(scope_depth));
4739 }
4740
4741 // Make sure we're not trying to return 'the hole' from the runtime
4742 // call as this may lead to crashes in the IC code later.
4743 if (FLAG_debug_code) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004744 NearLabel okay;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004745 __ cmp(eax, FACTORY->the_hole_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004746 __ j(not_equal, &okay);
4747 __ int3();
4748 __ bind(&okay);
4749 }
4750
4751 // Check for failure result.
4752 Label failure_returned;
4753 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4754 __ lea(ecx, Operand(eax, 1));
4755 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4756 __ test(ecx, Immediate(kFailureTagMask));
4757 __ j(zero, &failure_returned, not_taken);
4758
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004759 ExternalReference pending_exception_address(
4760 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004761
4762 // Check that there is no pending exception, otherwise we
4763 // should have returned some failure value.
4764 if (FLAG_debug_code) {
4765 __ push(edx);
4766 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004767 ExternalReference::the_hole_value_location(masm->isolate())));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004768 NearLabel okay;
4769 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4770 // Cannot use check here as it attempts to generate call into runtime.
4771 __ j(equal, &okay);
4772 __ int3();
4773 __ bind(&okay);
4774 __ pop(edx);
4775 }
4776
ricow@chromium.org65fae842010-08-25 15:26:24 +00004777 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004778 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004779 __ ret(0);
4780
4781 // Handling of failure.
4782 __ bind(&failure_returned);
4783
4784 Label retry;
4785 // If the returned exception is RETRY_AFTER_GC continue at retry label
4786 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4787 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4788 __ j(zero, &retry, taken);
4789
4790 // Special handling of out of memory exceptions.
4791 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4792 __ j(equal, throw_out_of_memory_exception);
4793
4794 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004795 ExternalReference the_hole_location =
4796 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004797 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004798 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004799 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4800
4801 // Special handling of termination exceptions which are uncatchable
4802 // by javascript code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004803 __ cmp(eax, FACTORY->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004804 __ j(equal, throw_termination_exception);
4805
4806 // Handle normal exception.
4807 __ jmp(throw_normal_exception);
4808
4809 // Retry.
4810 __ bind(&retry);
4811}
4812
4813
4814void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4815 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004816 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004817}
4818
4819
4820void CEntryStub::Generate(MacroAssembler* masm) {
4821 // eax: number of arguments including receiver
4822 // ebx: pointer to C function (C callee-saved)
4823 // ebp: frame pointer (restored after C call)
4824 // esp: stack pointer (restored after C call)
4825 // esi: current context (C callee-saved)
4826 // edi: JS function of the caller (C callee-saved)
4827
4828 // NOTE: Invocations of builtins may return failure objects instead
4829 // of a proper result. The builtin entry handles this by performing
4830 // a garbage collection and retrying the builtin (twice).
4831
4832 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004833 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004834
4835 // eax: result parameter for PerformGC, if any (setup below)
4836 // ebx: pointer to builtin function (C callee-saved)
4837 // ebp: frame pointer (restored after C call)
4838 // esp: stack pointer (restored after C call)
4839 // edi: number of arguments including receiver (C callee-saved)
4840 // esi: argv pointer (C callee-saved)
4841
4842 Label throw_normal_exception;
4843 Label throw_termination_exception;
4844 Label throw_out_of_memory_exception;
4845
4846 // Call into the runtime system.
4847 GenerateCore(masm,
4848 &throw_normal_exception,
4849 &throw_termination_exception,
4850 &throw_out_of_memory_exception,
4851 false,
4852 false);
4853
4854 // Do space-specific GC and retry runtime call.
4855 GenerateCore(masm,
4856 &throw_normal_exception,
4857 &throw_termination_exception,
4858 &throw_out_of_memory_exception,
4859 true,
4860 false);
4861
4862 // Do full GC and retry runtime call one final time.
4863 Failure* failure = Failure::InternalError();
4864 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4865 GenerateCore(masm,
4866 &throw_normal_exception,
4867 &throw_termination_exception,
4868 &throw_out_of_memory_exception,
4869 true,
4870 true);
4871
4872 __ bind(&throw_out_of_memory_exception);
4873 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4874
4875 __ bind(&throw_termination_exception);
4876 GenerateThrowUncatchable(masm, TERMINATION);
4877
4878 __ bind(&throw_normal_exception);
4879 GenerateThrowTOS(masm);
4880}
4881
4882
4883void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4884 Label invoke, exit;
4885#ifdef ENABLE_LOGGING_AND_PROFILING
4886 Label not_outermost_js, not_outermost_js_2;
4887#endif
4888
4889 // Setup frame.
4890 __ push(ebp);
4891 __ mov(ebp, Operand(esp));
4892
4893 // Push marker in two places.
4894 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4895 __ push(Immediate(Smi::FromInt(marker))); // context slot
4896 __ push(Immediate(Smi::FromInt(marker))); // function slot
4897 // Save callee-saved registers (C calling conventions).
4898 __ push(edi);
4899 __ push(esi);
4900 __ push(ebx);
4901
4902 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004903 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004904 __ push(Operand::StaticVariable(c_entry_fp));
4905
4906#ifdef ENABLE_LOGGING_AND_PROFILING
4907 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004908 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4909 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004910 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4911 __ j(not_equal, &not_outermost_js);
4912 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4913 __ bind(&not_outermost_js);
4914#endif
4915
4916 // Call a faked try-block that does the invoke.
4917 __ call(&invoke);
4918
4919 // Caught exception: Store result (exception) in the pending
4920 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004921 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4922 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004923 __ mov(Operand::StaticVariable(pending_exception), eax);
4924 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4925 __ jmp(&exit);
4926
4927 // Invoke: Link this frame into the handler chain.
4928 __ bind(&invoke);
4929 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4930
4931 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004932 ExternalReference the_hole_location =
4933 ExternalReference::the_hole_value_location(masm->isolate());
4934 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004935 __ mov(Operand::StaticVariable(pending_exception), edx);
4936
4937 // Fake a receiver (NULL).
4938 __ push(Immediate(0)); // receiver
4939
4940 // Invoke the function by calling through JS entry trampoline
4941 // builtin and pop the faked function when we return. Notice that we
4942 // cannot store a reference to the trampoline code directly in this
4943 // stub, because the builtin stubs may not have been generated yet.
4944 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004945 ExternalReference construct_entry(
4946 Builtins::JSConstructEntryTrampoline,
4947 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004948 __ mov(edx, Immediate(construct_entry));
4949 } else {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004950 ExternalReference entry(Builtins::JSEntryTrampoline,
4951 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004952 __ mov(edx, Immediate(entry));
4953 }
4954 __ mov(edx, Operand(edx, 0)); // deref address
4955 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4956 __ call(Operand(edx));
4957
4958 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004959 __ pop(Operand::StaticVariable(ExternalReference(
4960 Isolate::k_handler_address,
4961 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004962 // Pop next_sp.
4963 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
4964
4965#ifdef ENABLE_LOGGING_AND_PROFILING
4966 // If current EBP value is the same as js_entry_sp value, it means that
4967 // the current function is the outermost.
4968 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
4969 __ j(not_equal, &not_outermost_js_2);
4970 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4971 __ bind(&not_outermost_js_2);
4972#endif
4973
4974 // Restore the top frame descriptor from the stack.
4975 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004976 __ pop(Operand::StaticVariable(ExternalReference(
4977 Isolate::k_c_entry_fp_address,
4978 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004979
4980 // Restore callee-saved registers (C calling conventions).
4981 __ pop(ebx);
4982 __ pop(esi);
4983 __ pop(edi);
4984 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4985
4986 // Restore frame pointer and return.
4987 __ pop(ebp);
4988 __ ret(0);
4989}
4990
4991
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004992// Generate stub code for instanceof.
4993// This code can patch a call site inlined cache of the instance of check,
4994// which looks like this.
4995//
4996// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4997// 75 0a jne <some near label>
4998// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4999//
5000// If call site patching is requested the stack will have the delta from the
5001// return address to the cmp instruction just below the return address. This
5002// also means that call site patching can only take place with arguments in
5003// registers. TOS looks like this when call site patching is requested
5004//
5005// esp[0] : return address
5006// esp[4] : delta from return address to cmp instruction
5007//
ricow@chromium.org65fae842010-08-25 15:26:24 +00005008void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005009 // Call site inlining and patching implies arguments in registers.
5010 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
5011
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005012 // Fixed register usage throughout the stub.
5013 Register object = eax; // Object (lhs).
5014 Register map = ebx; // Map of the object.
5015 Register function = edx; // Function (rhs).
5016 Register prototype = edi; // Prototype of the function.
5017 Register scratch = ecx;
5018
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005019 // Constants describing the call site code to patch.
5020 static const int kDeltaToCmpImmediate = 2;
5021 static const int kDeltaToMov = 8;
5022 static const int kDeltaToMovImmediate = 9;
5023 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
5024 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
5025 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
5026
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005027 ExternalReference roots_address =
5028 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005029
5030 ASSERT_EQ(object.code(), InstanceofStub::left().code());
5031 ASSERT_EQ(function.code(), InstanceofStub::right().code());
5032
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005033 // Get the object and function - they are always both needed.
5034 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005035 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005036 __ mov(object, Operand(esp, 2 * kPointerSize));
5037 __ mov(function, Operand(esp, 1 * kPointerSize));
5038 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005039
5040 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005041 __ test(object, Immediate(kSmiTagMask));
5042 __ j(zero, &not_js_object, not_taken);
5043 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005044
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005045 // If there is a call site cache don't look in the global cache, but do the
5046 // real lookup and update the call site cache.
5047 if (!HasCallSiteInlineCheck()) {
5048 // Look up the function and the map in the instanceof cache.
5049 NearLabel miss;
5050 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5051 __ cmp(function,
5052 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5053 __ j(not_equal, &miss);
5054 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5055 __ cmp(map, Operand::StaticArray(
5056 scratch, times_pointer_size, roots_address));
5057 __ j(not_equal, &miss);
5058 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5059 __ mov(eax, Operand::StaticArray(
5060 scratch, times_pointer_size, roots_address));
5061 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
5062 __ bind(&miss);
5063 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005064
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005065 // Get the prototype of the function.
5066 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005067
5068 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005069 __ test(prototype, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005070 __ j(zero, &slow, not_taken);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005071 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005072
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005073 // Update the global instanceof or call site inlined cache with the current
5074 // map and function. The cached answer will be set when it is known below.
5075 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005076 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5077 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
5078 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
5079 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
5080 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005081 } else {
5082 // The constants for the code patching are based on no push instructions
5083 // at the call site.
5084 ASSERT(HasArgsInRegisters());
5085 // Get return address and delta to inlined map check.
5086 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5087 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5088 if (FLAG_debug_code) {
5089 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
5090 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
5091 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
5092 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
5093 }
5094 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
5095 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005096
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005097 // Loop through the prototype chain of the object looking for the function
5098 // prototype.
5099 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005100 NearLabel loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005101 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005102 __ cmp(scratch, Operand(prototype));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005103 __ j(equal, &is_instance);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005104 __ cmp(Operand(scratch), Immediate(FACTORY->null_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005105 __ j(equal, &is_not_instance);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005106 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5107 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005108 __ jmp(&loop);
5109
5110 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005111 if (!HasCallSiteInlineCheck()) {
5112 __ Set(eax, Immediate(0));
5113 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5114 __ mov(Operand::StaticArray(scratch,
5115 times_pointer_size, roots_address), eax);
5116 } else {
5117 // Get return address and delta to inlined map check.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005118 __ mov(eax, FACTORY->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005119 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5120 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5121 if (FLAG_debug_code) {
5122 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5123 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5124 }
5125 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5126 if (!ReturnTrueFalseObject()) {
5127 __ Set(eax, Immediate(0));
5128 }
5129 }
5130 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005131
5132 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005133 if (!HasCallSiteInlineCheck()) {
5134 __ Set(eax, Immediate(Smi::FromInt(1)));
5135 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5136 __ mov(Operand::StaticArray(
5137 scratch, times_pointer_size, roots_address), eax);
5138 } else {
5139 // Get return address and delta to inlined map check.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005140 __ mov(eax, FACTORY->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005141 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5142 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5143 if (FLAG_debug_code) {
5144 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5145 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5146 }
5147 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5148 if (!ReturnTrueFalseObject()) {
5149 __ Set(eax, Immediate(Smi::FromInt(1)));
5150 }
5151 }
5152 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005153
5154 Label object_not_null, object_not_null_or_smi;
5155 __ bind(&not_js_object);
5156 // Before null, smi and string value checks, check that the rhs is a function
5157 // as for a non-function rhs an exception needs to be thrown.
5158 __ test(function, Immediate(kSmiTagMask));
5159 __ j(zero, &slow, not_taken);
5160 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
5161 __ j(not_equal, &slow, not_taken);
5162
5163 // Null is not instance of anything.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005164 __ cmp(object, FACTORY->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005165 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005166 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005167 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005168
5169 __ bind(&object_not_null);
5170 // Smi values is not instance of anything.
5171 __ test(object, Immediate(kSmiTagMask));
5172 __ j(not_zero, &object_not_null_or_smi, not_taken);
5173 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005174 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005175
5176 __ bind(&object_not_null_or_smi);
5177 // String values is not instance of anything.
5178 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
5179 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005180 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005181 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005182
5183 // Slow-case: Go through the JavaScript implementation.
5184 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005185 if (!ReturnTrueFalseObject()) {
5186 // Tail call the builtin which returns 0 or 1.
5187 if (HasArgsInRegisters()) {
5188 // Push arguments below return address.
5189 __ pop(scratch);
5190 __ push(object);
5191 __ push(function);
5192 __ push(scratch);
5193 }
5194 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
5195 } else {
5196 // Call the builtin and convert 0/1 to true/false.
5197 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005198 __ push(object);
5199 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005200 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
5201 __ LeaveInternalFrame();
5202 NearLabel true_value, done;
5203 __ test(eax, Operand(eax));
5204 __ j(zero, &true_value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005205 __ mov(eax, FACTORY->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005206 __ jmp(&done);
5207 __ bind(&true_value);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005208 __ mov(eax, FACTORY->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005209 __ bind(&done);
5210 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00005211 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005212}
5213
5214
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00005215Register InstanceofStub::left() { return eax; }
5216
5217
5218Register InstanceofStub::right() { return edx; }
5219
5220
ricow@chromium.org65fae842010-08-25 15:26:24 +00005221int CompareStub::MinorKey() {
5222 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5223 // stubs the never NaN NaN condition is only taken into account if the
5224 // condition is equals.
5225 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
5226 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5227 return ConditionField::encode(static_cast<unsigned>(cc_))
5228 | RegisterField::encode(false) // lhs_ and rhs_ are not used
5229 | StrictField::encode(strict_)
5230 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005231 | IncludeNumberCompareField::encode(include_number_compare_)
5232 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005233}
5234
5235
5236// Unfortunately you have to run without snapshots to see most of these
5237// names in the profile since most compare stubs end up in the snapshot.
5238const char* CompareStub::GetName() {
5239 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5240
5241 if (name_ != NULL) return name_;
5242 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005243 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
5244 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005245 if (name_ == NULL) return "OOM";
5246
5247 const char* cc_name;
5248 switch (cc_) {
5249 case less: cc_name = "LT"; break;
5250 case greater: cc_name = "GT"; break;
5251 case less_equal: cc_name = "LE"; break;
5252 case greater_equal: cc_name = "GE"; break;
5253 case equal: cc_name = "EQ"; break;
5254 case not_equal: cc_name = "NE"; break;
5255 default: cc_name = "UnknownCondition"; break;
5256 }
5257
5258 const char* strict_name = "";
5259 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
5260 strict_name = "_STRICT";
5261 }
5262
5263 const char* never_nan_nan_name = "";
5264 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
5265 never_nan_nan_name = "_NO_NAN";
5266 }
5267
5268 const char* include_number_compare_name = "";
5269 if (!include_number_compare_) {
5270 include_number_compare_name = "_NO_NUMBER";
5271 }
5272
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005273 const char* include_smi_compare_name = "";
5274 if (!include_smi_compare_) {
5275 include_smi_compare_name = "_NO_SMI";
5276 }
5277
ricow@chromium.org65fae842010-08-25 15:26:24 +00005278 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005279 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00005280 cc_name,
5281 strict_name,
5282 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00005283 include_number_compare_name,
5284 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005285 return name_;
5286}
5287
5288
5289// -------------------------------------------------------------------------
5290// StringCharCodeAtGenerator
5291
5292void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
5293 Label flat_string;
5294 Label ascii_string;
5295 Label got_char_code;
5296
5297 // If the receiver is a smi trigger the non-string case.
5298 STATIC_ASSERT(kSmiTag == 0);
5299 __ test(object_, Immediate(kSmiTagMask));
5300 __ j(zero, receiver_not_string_);
5301
5302 // Fetch the instance type of the receiver into result register.
5303 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5304 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5305 // If the receiver is not a string trigger the non-string case.
5306 __ test(result_, Immediate(kIsNotStringMask));
5307 __ j(not_zero, receiver_not_string_);
5308
5309 // If the index is non-smi trigger the non-smi case.
5310 STATIC_ASSERT(kSmiTag == 0);
5311 __ test(index_, Immediate(kSmiTagMask));
5312 __ j(not_zero, &index_not_smi_);
5313
5314 // Put smi-tagged index into scratch register.
5315 __ mov(scratch_, index_);
5316 __ bind(&got_smi_index_);
5317
5318 // Check for index out of range.
5319 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
5320 __ j(above_equal, index_out_of_range_);
5321
5322 // We need special handling for non-flat strings.
5323 STATIC_ASSERT(kSeqStringTag == 0);
5324 __ test(result_, Immediate(kStringRepresentationMask));
5325 __ j(zero, &flat_string);
5326
5327 // Handle non-flat strings.
5328 __ test(result_, Immediate(kIsConsStringMask));
5329 __ j(zero, &call_runtime_);
5330
5331 // ConsString.
5332 // Check whether the right hand side is the empty string (i.e. if
5333 // this is really a flat string in a cons string). If that is not
5334 // the case we would rather go to the runtime system now to flatten
5335 // the string.
5336 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005337 Immediate(FACTORY->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005338 __ j(not_equal, &call_runtime_);
5339 // Get the first of the two strings and load its instance type.
5340 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
5341 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5342 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5343 // If the first cons component is also non-flat, then go to runtime.
5344 STATIC_ASSERT(kSeqStringTag == 0);
5345 __ test(result_, Immediate(kStringRepresentationMask));
5346 __ j(not_zero, &call_runtime_);
5347
5348 // Check for 1-byte or 2-byte string.
5349 __ bind(&flat_string);
5350 STATIC_ASSERT(kAsciiStringTag != 0);
5351 __ test(result_, Immediate(kStringEncodingMask));
5352 __ j(not_zero, &ascii_string);
5353
5354 // 2-byte string.
5355 // Load the 2-byte character code into the result register.
5356 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
5357 __ movzx_w(result_, FieldOperand(object_,
5358 scratch_, times_1, // Scratch is smi-tagged.
5359 SeqTwoByteString::kHeaderSize));
5360 __ jmp(&got_char_code);
5361
5362 // ASCII string.
5363 // Load the byte into the result register.
5364 __ bind(&ascii_string);
5365 __ SmiUntag(scratch_);
5366 __ movzx_b(result_, FieldOperand(object_,
5367 scratch_, times_1,
5368 SeqAsciiString::kHeaderSize));
5369 __ bind(&got_char_code);
5370 __ SmiTag(result_);
5371 __ bind(&exit_);
5372}
5373
5374
5375void StringCharCodeAtGenerator::GenerateSlow(
5376 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5377 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5378
5379 // Index is not a smi.
5380 __ bind(&index_not_smi_);
5381 // If index is a heap number, try converting it to an integer.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005382 __ CheckMap(index_, FACTORY->heap_number_map(), index_not_number_, true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005383 call_helper.BeforeCall(masm);
5384 __ push(object_);
5385 __ push(index_);
5386 __ push(index_); // Consumed by runtime conversion function.
5387 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5388 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5389 } else {
5390 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5391 // NumberToSmi discards numbers that are not exact integers.
5392 __ CallRuntime(Runtime::kNumberToSmi, 1);
5393 }
5394 if (!scratch_.is(eax)) {
5395 // Save the conversion result before the pop instructions below
5396 // have a chance to overwrite it.
5397 __ mov(scratch_, eax);
5398 }
5399 __ pop(index_);
5400 __ pop(object_);
5401 // Reload the instance type.
5402 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5403 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5404 call_helper.AfterCall(masm);
5405 // If index is still not a smi, it must be out of range.
5406 STATIC_ASSERT(kSmiTag == 0);
5407 __ test(scratch_, Immediate(kSmiTagMask));
5408 __ j(not_zero, index_out_of_range_);
5409 // Otherwise, return to the fast path.
5410 __ jmp(&got_smi_index_);
5411
5412 // Call runtime. We get here when the receiver is a string and the
5413 // index is a number, but the code of getting the actual character
5414 // is too complex (e.g., when the string needs to be flattened).
5415 __ bind(&call_runtime_);
5416 call_helper.BeforeCall(masm);
5417 __ push(object_);
5418 __ push(index_);
5419 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5420 if (!result_.is(eax)) {
5421 __ mov(result_, eax);
5422 }
5423 call_helper.AfterCall(masm);
5424 __ jmp(&exit_);
5425
5426 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5427}
5428
5429
5430// -------------------------------------------------------------------------
5431// StringCharFromCodeGenerator
5432
5433void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5434 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5435 STATIC_ASSERT(kSmiTag == 0);
5436 STATIC_ASSERT(kSmiShiftSize == 0);
5437 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5438 __ test(code_,
5439 Immediate(kSmiTagMask |
5440 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
5441 __ j(not_zero, &slow_case_, not_taken);
5442
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005443 __ Set(result_, Immediate(FACTORY->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00005444 STATIC_ASSERT(kSmiTag == 0);
5445 STATIC_ASSERT(kSmiTagSize == 1);
5446 STATIC_ASSERT(kSmiShiftSize == 0);
5447 // At this point code register contains smi tagged ascii char code.
5448 __ mov(result_, FieldOperand(result_,
5449 code_, times_half_pointer_size,
5450 FixedArray::kHeaderSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005451 __ cmp(result_, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005452 __ j(equal, &slow_case_, not_taken);
5453 __ bind(&exit_);
5454}
5455
5456
5457void StringCharFromCodeGenerator::GenerateSlow(
5458 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5459 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5460
5461 __ bind(&slow_case_);
5462 call_helper.BeforeCall(masm);
5463 __ push(code_);
5464 __ CallRuntime(Runtime::kCharFromCode, 1);
5465 if (!result_.is(eax)) {
5466 __ mov(result_, eax);
5467 }
5468 call_helper.AfterCall(masm);
5469 __ jmp(&exit_);
5470
5471 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5472}
5473
5474
5475// -------------------------------------------------------------------------
5476// StringCharAtGenerator
5477
5478void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5479 char_code_at_generator_.GenerateFast(masm);
5480 char_from_code_generator_.GenerateFast(masm);
5481}
5482
5483
5484void StringCharAtGenerator::GenerateSlow(
5485 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5486 char_code_at_generator_.GenerateSlow(masm, call_helper);
5487 char_from_code_generator_.GenerateSlow(masm, call_helper);
5488}
5489
5490
5491void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005492 Label string_add_runtime, call_builtin;
5493 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005494
5495 // Load the two arguments.
5496 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5497 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5498
5499 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005500 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00005501 __ test(eax, Immediate(kSmiTagMask));
5502 __ j(zero, &string_add_runtime);
5503 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
5504 __ j(above_equal, &string_add_runtime);
5505
5506 // First argument is a a string, test second.
5507 __ test(edx, Immediate(kSmiTagMask));
5508 __ j(zero, &string_add_runtime);
5509 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
5510 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005511 } else {
5512 // Here at least one of the arguments is definitely a string.
5513 // We convert the one that is not known to be a string.
5514 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5515 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5516 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5517 &call_builtin);
5518 builtin_id = Builtins::STRING_ADD_RIGHT;
5519 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5520 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5521 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5522 &call_builtin);
5523 builtin_id = Builtins::STRING_ADD_LEFT;
5524 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005525 }
5526
5527 // Both arguments are strings.
5528 // eax: first string
5529 // edx: second string
5530 // Check if either of the strings are empty. In that case return the other.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005531 NearLabel second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005532 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5533 STATIC_ASSERT(kSmiTag == 0);
5534 __ test(ecx, Operand(ecx));
5535 __ j(not_zero, &second_not_zero_length);
5536 // Second string is empty, result is first string which is already in eax.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005537 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005538 __ ret(2 * kPointerSize);
5539 __ bind(&second_not_zero_length);
5540 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5541 STATIC_ASSERT(kSmiTag == 0);
5542 __ test(ebx, Operand(ebx));
5543 __ j(not_zero, &both_not_zero_length);
5544 // First string is empty, result is second string which is in edx.
5545 __ mov(eax, edx);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005546 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005547 __ ret(2 * kPointerSize);
5548
5549 // Both strings are non-empty.
5550 // eax: first string
5551 // ebx: length of first string as a smi
5552 // ecx: length of second string as a smi
5553 // edx: second string
5554 // Look at the length of the result of adding the two strings.
5555 Label string_add_flat_result, longer_than_two;
5556 __ bind(&both_not_zero_length);
5557 __ add(ebx, Operand(ecx));
5558 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5559 // Handle exceptionally long strings in the runtime system.
5560 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005561 // Use the symbol table when adding two one character strings, as it
5562 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005563 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
5564 __ j(not_equal, &longer_than_two);
5565
5566 // Check that both strings are non-external ascii strings.
5567 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
5568 &string_add_runtime);
5569
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005570 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005571 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5572 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5573
5574 // Try to lookup two character string in symbol table. If it is not found
5575 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005576 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005577 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005578 masm, ebx, ecx, eax, edx, edi,
5579 &make_two_character_string_no_reload, &make_two_character_string);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005580 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005581 __ ret(2 * kPointerSize);
5582
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005583 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005584 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005585 // Reload the arguments.
5586 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5587 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5588 // Get the two characters forming the new string.
5589 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5590 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5591 __ bind(&make_two_character_string_no_reload);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005592 __ IncrementCounter(COUNTERS->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005593 __ AllocateAsciiString(eax, // Result.
5594 2, // Length.
5595 edi, // Scratch 1.
5596 edx, // Scratch 2.
5597 &string_add_runtime);
5598 // Pack both characters in ebx.
5599 __ shl(ecx, kBitsPerByte);
5600 __ or_(ebx, Operand(ecx));
5601 // Set the characters in the new string.
5602 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005603 __ IncrementCounter(COUNTERS->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005604 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005605
5606 __ bind(&longer_than_two);
5607 // Check if resulting string will be flat.
5608 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
5609 __ j(below, &string_add_flat_result);
5610
5611 // If result is not supposed to be flat allocate a cons string object. If both
5612 // strings are ascii the result is an ascii cons string.
5613 Label non_ascii, allocated, ascii_data;
5614 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5615 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5616 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5617 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5618 __ and_(ecx, Operand(edi));
5619 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5620 __ test(ecx, Immediate(kAsciiStringTag));
5621 __ j(zero, &non_ascii);
5622 __ bind(&ascii_data);
5623 // Allocate an acsii cons string.
5624 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5625 __ bind(&allocated);
5626 // Fill the fields of the cons string.
5627 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5628 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5629 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5630 Immediate(String::kEmptyHashField));
5631 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5632 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5633 __ mov(eax, ecx);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005634 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005635 __ ret(2 * kPointerSize);
5636 __ bind(&non_ascii);
5637 // At least one of the strings is two-byte. Check whether it happens
5638 // to contain only ascii characters.
5639 // ecx: first instance type AND second instance type.
5640 // edi: second instance type.
5641 __ test(ecx, Immediate(kAsciiDataHintMask));
5642 __ j(not_zero, &ascii_data);
5643 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5644 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5645 __ xor_(edi, Operand(ecx));
5646 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5647 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5648 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5649 __ j(equal, &ascii_data);
5650 // Allocate a two byte cons string.
5651 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5652 __ jmp(&allocated);
5653
5654 // Handle creating a flat result. First check that both strings are not
5655 // external strings.
5656 // eax: first string
5657 // ebx: length of resulting flat string as a smi
5658 // edx: second string
5659 __ bind(&string_add_flat_result);
5660 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5661 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5662 __ and_(ecx, kStringRepresentationMask);
5663 __ cmp(ecx, kExternalStringTag);
5664 __ j(equal, &string_add_runtime);
5665 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5666 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5667 __ and_(ecx, kStringRepresentationMask);
5668 __ cmp(ecx, kExternalStringTag);
5669 __ j(equal, &string_add_runtime);
5670 // Now check if both strings are ascii strings.
5671 // eax: first string
5672 // ebx: length of resulting flat string as a smi
5673 // edx: second string
5674 Label non_ascii_string_add_flat_result;
5675 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5676 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5677 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5678 __ j(zero, &non_ascii_string_add_flat_result);
5679 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5680 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5681 __ j(zero, &string_add_runtime);
5682
ricow@chromium.org65fae842010-08-25 15:26:24 +00005683 // Both strings are ascii strings. As they are short they are both flat.
5684 // ebx: length of resulting flat string as a smi
5685 __ SmiUntag(ebx);
5686 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5687 // eax: result string
5688 __ mov(ecx, eax);
5689 // Locate first character of result.
5690 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5691 // Load first argument and locate first character.
5692 __ mov(edx, Operand(esp, 2 * kPointerSize));
5693 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5694 __ SmiUntag(edi);
5695 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5696 // eax: result string
5697 // ecx: first character of result
5698 // edx: first char of first argument
5699 // edi: length of first argument
5700 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5701 // Load second argument and locate first character.
5702 __ mov(edx, Operand(esp, 1 * kPointerSize));
5703 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5704 __ SmiUntag(edi);
5705 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5706 // eax: result string
5707 // ecx: next character of result
5708 // edx: first char of second argument
5709 // edi: length of second argument
5710 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005711 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005712 __ ret(2 * kPointerSize);
5713
5714 // Handle creating a flat two byte result.
5715 // eax: first string - known to be two byte
5716 // ebx: length of resulting flat string as a smi
5717 // edx: second string
5718 __ bind(&non_ascii_string_add_flat_result);
5719 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5720 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5721 __ j(not_zero, &string_add_runtime);
5722 // Both strings are two byte strings. As they are short they are both
5723 // flat.
5724 __ SmiUntag(ebx);
5725 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5726 // eax: result string
5727 __ mov(ecx, eax);
5728 // Locate first character of result.
5729 __ add(Operand(ecx),
5730 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5731 // Load first argument and locate first character.
5732 __ mov(edx, Operand(esp, 2 * kPointerSize));
5733 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5734 __ SmiUntag(edi);
5735 __ add(Operand(edx),
5736 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5737 // eax: result string
5738 // ecx: first character of result
5739 // edx: first char of first argument
5740 // edi: length of first argument
5741 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5742 // Load second argument and locate first character.
5743 __ mov(edx, Operand(esp, 1 * kPointerSize));
5744 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5745 __ SmiUntag(edi);
5746 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5747 // eax: result string
5748 // ecx: next character of result
5749 // edx: first char of second argument
5750 // edi: length of second argument
5751 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005752 __ IncrementCounter(COUNTERS->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005753 __ ret(2 * kPointerSize);
5754
5755 // Just jump to runtime to add the two strings.
5756 __ bind(&string_add_runtime);
5757 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005758
5759 if (call_builtin.is_linked()) {
5760 __ bind(&call_builtin);
5761 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5762 }
5763}
5764
5765
5766void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5767 int stack_offset,
5768 Register arg,
5769 Register scratch1,
5770 Register scratch2,
5771 Register scratch3,
5772 Label* slow) {
5773 // First check if the argument is already a string.
5774 Label not_string, done;
5775 __ test(arg, Immediate(kSmiTagMask));
5776 __ j(zero, &not_string);
5777 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5778 __ j(below, &done);
5779
5780 // Check the number to string cache.
5781 Label not_cached;
5782 __ bind(&not_string);
5783 // Puts the cached result into scratch1.
5784 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5785 arg,
5786 scratch1,
5787 scratch2,
5788 scratch3,
5789 false,
5790 &not_cached);
5791 __ mov(arg, scratch1);
5792 __ mov(Operand(esp, stack_offset), arg);
5793 __ jmp(&done);
5794
5795 // Check if the argument is a safe string wrapper.
5796 __ bind(&not_cached);
5797 __ test(arg, Immediate(kSmiTagMask));
5798 __ j(zero, slow);
5799 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5800 __ j(not_equal, slow);
5801 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5802 1 << Map::kStringWrapperSafeForDefaultValueOf);
5803 __ j(zero, slow);
5804 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5805 __ mov(Operand(esp, stack_offset), arg);
5806
5807 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005808}
5809
5810
5811void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5812 Register dest,
5813 Register src,
5814 Register count,
5815 Register scratch,
5816 bool ascii) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005817 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005818 __ bind(&loop);
5819 // This loop just copies one character at a time, as it is only used for very
5820 // short strings.
5821 if (ascii) {
5822 __ mov_b(scratch, Operand(src, 0));
5823 __ mov_b(Operand(dest, 0), scratch);
5824 __ add(Operand(src), Immediate(1));
5825 __ add(Operand(dest), Immediate(1));
5826 } else {
5827 __ mov_w(scratch, Operand(src, 0));
5828 __ mov_w(Operand(dest, 0), scratch);
5829 __ add(Operand(src), Immediate(2));
5830 __ add(Operand(dest), Immediate(2));
5831 }
5832 __ sub(Operand(count), Immediate(1));
5833 __ j(not_zero, &loop);
5834}
5835
5836
5837void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5838 Register dest,
5839 Register src,
5840 Register count,
5841 Register scratch,
5842 bool ascii) {
5843 // Copy characters using rep movs of doublewords.
5844 // The destination is aligned on a 4 byte boundary because we are
5845 // copying to the beginning of a newly allocated string.
5846 ASSERT(dest.is(edi)); // rep movs destination
5847 ASSERT(src.is(esi)); // rep movs source
5848 ASSERT(count.is(ecx)); // rep movs count
5849 ASSERT(!scratch.is(dest));
5850 ASSERT(!scratch.is(src));
5851 ASSERT(!scratch.is(count));
5852
5853 // Nothing to do for zero characters.
5854 Label done;
5855 __ test(count, Operand(count));
5856 __ j(zero, &done);
5857
5858 // Make count the number of bytes to copy.
5859 if (!ascii) {
5860 __ shl(count, 1);
5861 }
5862
5863 // Don't enter the rep movs if there are less than 4 bytes to copy.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005864 NearLabel last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005865 __ test(count, Immediate(~3));
5866 __ j(zero, &last_bytes);
5867
5868 // Copy from edi to esi using rep movs instruction.
5869 __ mov(scratch, count);
5870 __ sar(count, 2); // Number of doublewords to copy.
5871 __ cld();
5872 __ rep_movs();
5873
5874 // Find number of bytes left.
5875 __ mov(count, scratch);
5876 __ and_(count, 3);
5877
5878 // Check if there are more bytes to copy.
5879 __ bind(&last_bytes);
5880 __ test(count, Operand(count));
5881 __ j(zero, &done);
5882
5883 // Copy remaining characters.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005884 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005885 __ bind(&loop);
5886 __ mov_b(scratch, Operand(src, 0));
5887 __ mov_b(Operand(dest, 0), scratch);
5888 __ add(Operand(src), Immediate(1));
5889 __ add(Operand(dest), Immediate(1));
5890 __ sub(Operand(count), Immediate(1));
5891 __ j(not_zero, &loop);
5892
5893 __ bind(&done);
5894}
5895
5896
5897void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5898 Register c1,
5899 Register c2,
5900 Register scratch1,
5901 Register scratch2,
5902 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005903 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005904 Label* not_found) {
5905 // Register scratch3 is the general scratch register in this function.
5906 Register scratch = scratch3;
5907
5908 // Make sure that both characters are not digits as such strings has a
5909 // different hash algorithm. Don't try to look for these in the symbol table.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005910 NearLabel not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005911 __ mov(scratch, c1);
5912 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5913 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
5914 __ j(above, &not_array_index);
5915 __ mov(scratch, c2);
5916 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5917 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005918 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005919
5920 __ bind(&not_array_index);
5921 // Calculate the two character string hash.
5922 Register hash = scratch1;
5923 GenerateHashInit(masm, hash, c1, scratch);
5924 GenerateHashAddCharacter(masm, hash, c2, scratch);
5925 GenerateHashGetHash(masm, hash, scratch);
5926
5927 // Collect the two characters in a register.
5928 Register chars = c1;
5929 __ shl(c2, kBitsPerByte);
5930 __ or_(chars, Operand(c2));
5931
5932 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5933 // hash: hash of two character string.
5934
5935 // Load the symbol table.
5936 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005937 ExternalReference roots_address =
5938 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005939 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5940 __ mov(symbol_table,
5941 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5942
5943 // Calculate capacity mask from the symbol table capacity.
5944 Register mask = scratch2;
5945 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5946 __ SmiUntag(mask);
5947 __ sub(Operand(mask), Immediate(1));
5948
5949 // Registers
5950 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5951 // hash: hash of two character string
5952 // symbol_table: symbol table
5953 // mask: capacity mask
5954 // scratch: -
5955
5956 // Perform a number of probes in the symbol table.
5957 static const int kProbes = 4;
5958 Label found_in_symbol_table;
5959 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5960 for (int i = 0; i < kProbes; i++) {
5961 // Calculate entry in symbol table.
5962 __ mov(scratch, hash);
5963 if (i > 0) {
5964 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5965 }
5966 __ and_(scratch, Operand(mask));
5967
5968 // Load the entry from the symbol table.
5969 Register candidate = scratch; // Scratch register contains candidate.
5970 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5971 __ mov(candidate,
5972 FieldOperand(symbol_table,
5973 scratch,
5974 times_pointer_size,
5975 SymbolTable::kElementsStartOffset));
5976
5977 // If entry is undefined no string with this hash can be found.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005978 __ cmp(candidate, FACTORY->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005979 __ j(equal, not_found);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005980 __ cmp(candidate, FACTORY->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005981 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005982
5983 // If length is not 2 the string is not a candidate.
5984 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5985 Immediate(Smi::FromInt(2)));
5986 __ j(not_equal, &next_probe[i]);
5987
5988 // As we are out of registers save the mask on the stack and use that
5989 // register as a temporary.
5990 __ push(mask);
5991 Register temp = mask;
5992
5993 // Check that the candidate is a non-external ascii string.
5994 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5995 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5996 __ JumpIfInstanceTypeIsNotSequentialAscii(
5997 temp, temp, &next_probe_pop_mask[i]);
5998
5999 // Check if the two characters match.
6000 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
6001 __ and_(temp, 0x0000ffff);
6002 __ cmp(chars, Operand(temp));
6003 __ j(equal, &found_in_symbol_table);
6004 __ bind(&next_probe_pop_mask[i]);
6005 __ pop(mask);
6006 __ bind(&next_probe[i]);
6007 }
6008
6009 // No matching 2 character string found by probing.
6010 __ jmp(not_found);
6011
6012 // Scratch register contains result when we fall through to here.
6013 Register result = scratch;
6014 __ bind(&found_in_symbol_table);
6015 __ pop(mask); // Pop saved mask from the stack.
6016 if (!result.is(eax)) {
6017 __ mov(eax, result);
6018 }
6019}
6020
6021
6022void StringHelper::GenerateHashInit(MacroAssembler* masm,
6023 Register hash,
6024 Register character,
6025 Register scratch) {
6026 // hash = character + (character << 10);
6027 __ mov(hash, character);
6028 __ shl(hash, 10);
6029 __ add(hash, Operand(character));
6030 // hash ^= hash >> 6;
6031 __ mov(scratch, hash);
6032 __ sar(scratch, 6);
6033 __ xor_(hash, Operand(scratch));
6034}
6035
6036
6037void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
6038 Register hash,
6039 Register character,
6040 Register scratch) {
6041 // hash += character;
6042 __ add(hash, Operand(character));
6043 // hash += hash << 10;
6044 __ mov(scratch, hash);
6045 __ shl(scratch, 10);
6046 __ add(hash, Operand(scratch));
6047 // hash ^= hash >> 6;
6048 __ mov(scratch, hash);
6049 __ sar(scratch, 6);
6050 __ xor_(hash, Operand(scratch));
6051}
6052
6053
6054void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
6055 Register hash,
6056 Register scratch) {
6057 // hash += hash << 3;
6058 __ mov(scratch, hash);
6059 __ shl(scratch, 3);
6060 __ add(hash, Operand(scratch));
6061 // hash ^= hash >> 11;
6062 __ mov(scratch, hash);
6063 __ sar(scratch, 11);
6064 __ xor_(hash, Operand(scratch));
6065 // hash += hash << 15;
6066 __ mov(scratch, hash);
6067 __ shl(scratch, 15);
6068 __ add(hash, Operand(scratch));
6069
6070 // if (hash == 0) hash = 27;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006071 NearLabel hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006072 __ test(hash, Operand(hash));
6073 __ j(not_zero, &hash_not_zero);
6074 __ mov(hash, Immediate(27));
6075 __ bind(&hash_not_zero);
6076}
6077
6078
6079void SubStringStub::Generate(MacroAssembler* masm) {
6080 Label runtime;
6081
6082 // Stack frame on entry.
6083 // esp[0]: return address
6084 // esp[4]: to
6085 // esp[8]: from
6086 // esp[12]: string
6087
6088 // Make sure first argument is a string.
6089 __ mov(eax, Operand(esp, 3 * kPointerSize));
6090 STATIC_ASSERT(kSmiTag == 0);
6091 __ test(eax, Immediate(kSmiTagMask));
6092 __ j(zero, &runtime);
6093 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
6094 __ j(NegateCondition(is_string), &runtime);
6095
6096 // eax: string
6097 // ebx: instance type
6098
6099 // Calculate length of sub string using the smi values.
6100 Label result_longer_than_two;
6101 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
6102 __ test(ecx, Immediate(kSmiTagMask));
6103 __ j(not_zero, &runtime);
6104 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
6105 __ test(edx, Immediate(kSmiTagMask));
6106 __ j(not_zero, &runtime);
6107 __ sub(ecx, Operand(edx));
6108 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
6109 Label return_eax;
6110 __ j(equal, &return_eax);
6111 // Special handling of sub-strings of length 1 and 2. One character strings
6112 // are handled in the runtime system (looked up in the single character
6113 // cache). Two character strings are looked for in the symbol cache.
6114 __ SmiUntag(ecx); // Result length is no longer smi.
6115 __ cmp(ecx, 2);
6116 __ j(greater, &result_longer_than_two);
6117 __ j(less, &runtime);
6118
6119 // Sub string of length 2 requested.
6120 // eax: string
6121 // ebx: instance type
6122 // ecx: sub string length (value is 2)
6123 // edx: from index (smi)
6124 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
6125
6126 // Get the two characters forming the sub string.
6127 __ SmiUntag(edx); // From index is no longer smi.
6128 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
6129 __ movzx_b(ecx,
6130 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
6131
6132 // Try to lookup two character string in symbol table.
6133 Label make_two_character_string;
6134 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00006135 masm, ebx, ecx, eax, edx, edi,
6136 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006137 __ ret(3 * kPointerSize);
6138
6139 __ bind(&make_two_character_string);
6140 // Setup registers for allocating the two character string.
6141 __ mov(eax, Operand(esp, 3 * kPointerSize));
6142 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
6143 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
6144 __ Set(ecx, Immediate(2));
6145
6146 __ bind(&result_longer_than_two);
6147 // eax: string
6148 // ebx: instance type
6149 // ecx: result string length
6150 // Check for flat ascii string
6151 Label non_ascii_flat;
6152 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
6153
6154 // Allocate the result.
6155 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
6156
6157 // eax: result string
6158 // ecx: result string length
6159 __ mov(edx, esi); // esi used by following code.
6160 // Locate first character of result.
6161 __ mov(edi, eax);
6162 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6163 // Load string argument and locate character of sub string start.
6164 __ mov(esi, Operand(esp, 3 * kPointerSize));
6165 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6166 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6167 __ SmiUntag(ebx);
6168 __ add(esi, Operand(ebx));
6169
6170 // eax: result string
6171 // ecx: result length
6172 // edx: original value of esi
6173 // edi: first character of result
6174 // esi: character of sub string start
6175 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
6176 __ mov(esi, edx); // Restore esi.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006177 __ IncrementCounter(COUNTERS->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006178 __ ret(3 * kPointerSize);
6179
6180 __ bind(&non_ascii_flat);
6181 // eax: string
6182 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
6183 // ecx: result string length
6184 // Check for flat two byte string
6185 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
6186 __ j(not_equal, &runtime);
6187
6188 // Allocate the result.
6189 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
6190
6191 // eax: result string
6192 // ecx: result string length
6193 __ mov(edx, esi); // esi used by following code.
6194 // Locate first character of result.
6195 __ mov(edi, eax);
6196 __ add(Operand(edi),
6197 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6198 // Load string argument and locate character of sub string start.
6199 __ mov(esi, Operand(esp, 3 * kPointerSize));
6200 __ add(Operand(esi),
6201 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6202 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
6203 // As from is a smi it is 2 times the value which matches the size of a two
6204 // byte character.
6205 STATIC_ASSERT(kSmiTag == 0);
6206 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
6207 __ add(esi, Operand(ebx));
6208
6209 // eax: result string
6210 // ecx: result length
6211 // edx: original value of esi
6212 // edi: first character of result
6213 // esi: character of sub string start
6214 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
6215 __ mov(esi, edx); // Restore esi.
6216
6217 __ bind(&return_eax);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006218 __ IncrementCounter(COUNTERS->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006219 __ ret(3 * kPointerSize);
6220
6221 // Just jump to runtime to create the sub string.
6222 __ bind(&runtime);
6223 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6224}
6225
6226
6227void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6228 Register left,
6229 Register right,
6230 Register scratch1,
6231 Register scratch2,
6232 Register scratch3) {
6233 Label result_not_equal;
6234 Label result_greater;
6235 Label compare_lengths;
6236
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006237 __ IncrementCounter(COUNTERS->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006238
6239 // Find minimum length.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006240 NearLabel left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006241 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
6242 __ mov(scratch3, scratch1);
6243 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
6244
6245 Register length_delta = scratch3;
6246
6247 __ j(less_equal, &left_shorter);
6248 // Right string is shorter. Change scratch1 to be length of right string.
6249 __ sub(scratch1, Operand(length_delta));
6250 __ bind(&left_shorter);
6251
6252 Register min_length = scratch1;
6253
6254 // If either length is zero, just compare lengths.
6255 __ test(min_length, Operand(min_length));
6256 __ j(zero, &compare_lengths);
6257
6258 // Change index to run from -min_length to -1 by adding min_length
6259 // to string start. This means that loop ends when index reaches zero,
6260 // which doesn't need an additional compare.
6261 __ SmiUntag(min_length);
6262 __ lea(left,
6263 FieldOperand(left,
6264 min_length, times_1,
6265 SeqAsciiString::kHeaderSize));
6266 __ lea(right,
6267 FieldOperand(right,
6268 min_length, times_1,
6269 SeqAsciiString::kHeaderSize));
6270 __ neg(min_length);
6271
6272 Register index = min_length; // index = -min_length;
6273
6274 {
6275 // Compare loop.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006276 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006277 __ bind(&loop);
6278 // Compare characters.
6279 __ mov_b(scratch2, Operand(left, index, times_1, 0));
6280 __ cmpb(scratch2, Operand(right, index, times_1, 0));
6281 __ j(not_equal, &result_not_equal);
6282 __ add(Operand(index), Immediate(1));
6283 __ j(not_zero, &loop);
6284 }
6285
6286 // Compare lengths - strings up to min-length are equal.
6287 __ bind(&compare_lengths);
6288 __ test(length_delta, Operand(length_delta));
6289 __ j(not_zero, &result_not_equal);
6290
6291 // Result is EQUAL.
6292 STATIC_ASSERT(EQUAL == 0);
6293 STATIC_ASSERT(kSmiTag == 0);
6294 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6295 __ ret(0);
6296
6297 __ bind(&result_not_equal);
6298 __ j(greater, &result_greater);
6299
6300 // Result is LESS.
6301 __ Set(eax, Immediate(Smi::FromInt(LESS)));
6302 __ ret(0);
6303
6304 // Result is GREATER.
6305 __ bind(&result_greater);
6306 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
6307 __ ret(0);
6308}
6309
6310
6311void StringCompareStub::Generate(MacroAssembler* masm) {
6312 Label runtime;
6313
6314 // Stack frame on entry.
6315 // esp[0]: return address
6316 // esp[4]: right string
6317 // esp[8]: left string
6318
6319 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
6320 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
6321
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00006322 NearLabel not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00006323 __ cmp(edx, Operand(eax));
6324 __ j(not_equal, &not_same);
6325 STATIC_ASSERT(EQUAL == 0);
6326 STATIC_ASSERT(kSmiTag == 0);
6327 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006328 __ IncrementCounter(COUNTERS->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00006329 __ ret(2 * kPointerSize);
6330
6331 __ bind(&not_same);
6332
6333 // Check that both objects are sequential ascii strings.
6334 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
6335
6336 // Compare flat ascii strings.
6337 // Drop arguments from the stack.
6338 __ pop(ecx);
6339 __ add(Operand(esp), Immediate(2 * kPointerSize));
6340 __ push(ecx);
6341 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
6342
6343 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6344 // tagged as a small integer.
6345 __ bind(&runtime);
6346 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6347}
6348
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006349
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006350void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6351 ASSERT(state_ == CompareIC::SMIS);
6352 NearLabel miss;
6353 __ mov(ecx, Operand(edx));
6354 __ or_(ecx, Operand(eax));
6355 __ test(ecx, Immediate(kSmiTagMask));
6356 __ j(not_zero, &miss, not_taken);
6357
6358 if (GetCondition() == equal) {
6359 // For equality we do not care about the sign of the result.
6360 __ sub(eax, Operand(edx));
6361 } else {
6362 NearLabel done;
6363 __ sub(edx, Operand(eax));
6364 __ j(no_overflow, &done);
6365 // Correct sign of result in case of overflow.
6366 __ not_(edx);
6367 __ bind(&done);
6368 __ mov(eax, edx);
6369 }
6370 __ ret(0);
6371
6372 __ bind(&miss);
6373 GenerateMiss(masm);
6374}
6375
6376
6377void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6378 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6379
6380 NearLabel generic_stub;
6381 NearLabel unordered;
6382 NearLabel miss;
6383 __ mov(ecx, Operand(edx));
6384 __ and_(ecx, Operand(eax));
6385 __ test(ecx, Immediate(kSmiTagMask));
6386 __ j(zero, &generic_stub, not_taken);
6387
6388 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
6389 __ j(not_equal, &miss, not_taken);
6390 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
6391 __ j(not_equal, &miss, not_taken);
6392
6393 // Inlining the double comparison and falling back to the general compare
6394 // stub if NaN is involved or SS2 or CMOV is unsupported.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006395 CpuFeatures* cpu_features = masm->isolate()->cpu_features();
6396 if (cpu_features->IsSupported(SSE2) && cpu_features->IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006397 CpuFeatures::Scope scope1(SSE2);
6398 CpuFeatures::Scope scope2(CMOV);
6399
6400 // Load left and right operand
6401 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
6402 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
6403
6404 // Compare operands
6405 __ ucomisd(xmm0, xmm1);
6406
6407 // Don't base result on EFLAGS when a NaN is involved.
6408 __ j(parity_even, &unordered, not_taken);
6409
6410 // Return a result of -1, 0, or 1, based on EFLAGS.
6411 // Performing mov, because xor would destroy the flag register.
6412 __ mov(eax, 0); // equal
6413 __ mov(ecx, Immediate(Smi::FromInt(1)));
6414 __ cmov(above, eax, Operand(ecx));
6415 __ mov(ecx, Immediate(Smi::FromInt(-1)));
6416 __ cmov(below, eax, Operand(ecx));
6417 __ ret(0);
6418
6419 __ bind(&unordered);
6420 }
6421
6422 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
6423 __ bind(&generic_stub);
6424 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
6425
6426 __ bind(&miss);
6427 GenerateMiss(masm);
6428}
6429
6430
6431void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6432 ASSERT(state_ == CompareIC::OBJECTS);
6433 NearLabel miss;
6434 __ mov(ecx, Operand(edx));
6435 __ and_(ecx, Operand(eax));
6436 __ test(ecx, Immediate(kSmiTagMask));
6437 __ j(zero, &miss, not_taken);
6438
6439 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
6440 __ j(not_equal, &miss, not_taken);
6441 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
6442 __ j(not_equal, &miss, not_taken);
6443
6444 ASSERT(GetCondition() == equal);
6445 __ sub(eax, Operand(edx));
6446 __ ret(0);
6447
6448 __ bind(&miss);
6449 GenerateMiss(masm);
6450}
6451
6452
6453void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6454 // Save the registers.
6455 __ pop(ecx);
6456 __ push(edx);
6457 __ push(eax);
6458 __ push(ecx);
6459
6460 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006461 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6462 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006463 __ EnterInternalFrame();
6464 __ push(edx);
6465 __ push(eax);
6466 __ push(Immediate(Smi::FromInt(op_)));
6467 __ CallExternalReference(miss, 3);
6468 __ LeaveInternalFrame();
6469
6470 // Compute the entry point of the rewritten stub.
6471 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6472
6473 // Restore registers.
6474 __ pop(ecx);
6475 __ pop(eax);
6476 __ pop(edx);
6477 __ push(ecx);
6478
6479 // Do a tail call to the rewritten stub.
6480 __ jmp(Operand(edi));
6481}
6482
6483
ricow@chromium.org65fae842010-08-25 15:26:24 +00006484#undef __
6485
6486} } // namespace v8::internal
6487
6488#endif // V8_TARGET_ARCH_IA32