blob: 27dbe2109815af52bae8b0a3674fc3122d7d8e5f [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
32#include "bootstrapper.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000033#include "code-stubs.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000034#include "isolate.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000035#include "jsregexp.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.
karlklose@chromium.org83a47282011-05-11 11:54:09 +000045 Label check_heap_number, call_builtin;
whesse@chromium.org7b260152011-06-20 15:33:18 +000046 __ JumpIfNotSmi(eax, &check_heap_number, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000047 __ ret(0);
48
49 __ bind(&check_heap_number);
50 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000051 Factory* factory = masm->isolate()->factory();
52 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000053 __ j(not_equal, &call_builtin, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000054 __ 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.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000086 Factory* factory = masm->isolate()->factory();
87 __ mov(ebx, Immediate(factory->empty_fixed_array()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000088 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
89 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
90 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000091 Immediate(factory->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000092 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
93 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
94 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000095 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000096 Immediate(factory->undefined_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000097
98 // Initialize the code pointer in the function to be the one
99 // found in the shared function info object.
100 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
101 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
102 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
103
104 // Return and remove the on-stack parameter.
105 __ ret(1 * kPointerSize);
106
107 // Create a new closure through the slower runtime call.
108 __ bind(&gc);
109 __ pop(ecx); // Temporarily remove return address.
110 __ pop(edx);
111 __ push(esi);
112 __ push(edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000113 __ push(Immediate(factory->false_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000114 __ push(ecx); // Restore return address.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000115 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000116}
117
118
119void FastNewContextStub::Generate(MacroAssembler* masm) {
120 // Try to allocate the context in new space.
121 Label gc;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000122 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
123 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000124 eax, ebx, ecx, &gc, TAG_OBJECT);
125
126 // Get the function from the stack.
127 __ mov(ecx, Operand(esp, 1 * kPointerSize));
128
129 // Setup the object header.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000130 Factory* factory = masm->isolate()->factory();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000131 __ mov(FieldOperand(eax, HeapObject::kMapOffset),
132 factory->function_context_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000133 __ mov(FieldOperand(eax, Context::kLengthOffset),
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000134 Immediate(Smi::FromInt(length)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135
136 // Setup the fixed slots.
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000137 __ Set(ebx, Immediate(0)); // Set to NULL.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000138 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
139 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000140 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), esi);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000141 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
142
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000143 // Copy the global object from the previous context.
144 __ mov(ebx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000145 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
146
147 // Initialize the rest of the slots to undefined.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +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);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000159 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000160}
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));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000184 Factory* factory = masm->isolate()->factory();
185 __ cmp(ecx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000186 __ j(equal, &slow_case);
187
188 if (FLAG_debug_code) {
189 const char* message;
190 Handle<Map> expected_map;
191 if (mode_ == CLONE_ELEMENTS) {
192 message = "Expected (writable) fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000193 expected_map = factory->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000194 } else {
195 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
196 message = "Expected copy-on-write fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000197 expected_map = factory->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000198 }
199 __ push(ecx);
200 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
201 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
202 __ Assert(equal, message);
203 __ pop(ecx);
204 }
205
206 // Allocate both the JS array and the elements array in one big
207 // allocation. This avoids multiple limit checks.
208 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
209
210 // Copy the JS array part.
211 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
212 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
213 __ mov(ebx, FieldOperand(ecx, i));
214 __ mov(FieldOperand(eax, i), ebx);
215 }
216 }
217
218 if (length_ > 0) {
219 // Get hold of the elements array of the boilerplate and setup the
220 // elements pointer in the resulting object.
221 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
222 __ lea(edx, Operand(eax, JSArray::kSize));
223 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
224
225 // Copy the elements array.
226 for (int i = 0; i < elements_size; i += kPointerSize) {
227 __ mov(ebx, FieldOperand(ecx, i));
228 __ mov(FieldOperand(edx, i), ebx);
229 }
230 }
231
232 // Return and remove the on-stack parameters.
233 __ ret(3 * kPointerSize);
234
235 __ bind(&slow_case);
236 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
237}
238
239
240// NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
241void ToBooleanStub::Generate(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000242 Label false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000243 __ mov(eax, Operand(esp, 1 * kPointerSize));
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000244 Factory* factory = masm->isolate()->factory();
245
246 // undefined -> false
247 __ cmp(eax, factory->undefined_value());
248 __ j(equal, &false_result);
249
250 // Boolean -> its value
251 __ cmp(eax, factory->true_value());
252 __ j(equal, &true_result);
253 __ cmp(eax, factory->false_value());
254 __ j(equal, &false_result);
255
256 // Smis: 0 -> false, all other -> true
257 __ test(eax, Operand(eax));
258 __ j(zero, &false_result);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000259 __ JumpIfSmi(eax, &true_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000260
261 // 'null' => false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000262 __ cmp(eax, factory->null_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000263 __ j(equal, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000264
265 // Get the map and type of the heap object.
266 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
267 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
268
269 // Undetectable => false.
270 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
271 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000272 __ j(not_zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000273
274 // JavaScript object => true.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +0000275 __ CmpInstanceType(edx, FIRST_SPEC_OBJECT_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000276 __ j(above_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000277
278 // String value => false iff empty.
279 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000280 __ j(above_equal, &not_string, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000281 STATIC_ASSERT(kSmiTag == 0);
282 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000283 __ j(zero, &false_result, Label::kNear);
284 __ jmp(&true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000285
286 __ bind(&not_string);
287 // HeapNumber => false iff +0, -0, or NaN.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000288 __ cmp(edx, factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000289 __ j(not_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000290 __ fldz();
291 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
292 __ FCmp();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000293 __ j(zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000294 // Fall through to |true_result|.
295
296 // Return 1/0 for true/false in eax.
297 __ bind(&true_result);
298 __ mov(eax, 1);
299 __ ret(1 * kPointerSize);
300 __ bind(&false_result);
301 __ mov(eax, 0);
302 __ ret(1 * kPointerSize);
303}
304
305
ricow@chromium.org65fae842010-08-25 15:26:24 +0000306class FloatingPointHelper : public AllStatic {
307 public:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000308 enum ArgLocation {
309 ARGS_ON_STACK,
310 ARGS_IN_REGISTERS
311 };
312
313 // Code pattern for loading a floating point value. Input value must
314 // be either a smi or a heap number object (fp value). Requirements:
315 // operand in register number. Returns operand as floating point number
316 // on FPU stack.
317 static void LoadFloatOperand(MacroAssembler* masm, Register number);
318
319 // Code pattern for loading floating point values. Input values must
320 // be either smi or heap number objects (fp values). Requirements:
321 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
322 // Returns operands as floating point numbers on FPU stack.
323 static void LoadFloatOperands(MacroAssembler* masm,
324 Register scratch,
325 ArgLocation arg_location = ARGS_ON_STACK);
326
327 // Similar to LoadFloatOperand but assumes that both operands are smis.
328 // Expects operands in edx, eax.
329 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
330
331 // Test if operands are smi or number objects (fp). Requirements:
332 // operand_1 in eax, operand_2 in edx; falls through on float
333 // operands, jumps to the non_float label otherwise.
334 static void CheckFloatOperands(MacroAssembler* masm,
335 Label* non_float,
336 Register scratch);
337
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 // Checks that the two floating point numbers on top of the FPU stack
339 // have int32 values.
340 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
341 Label* non_int32);
342
ricow@chromium.org65fae842010-08-25 15:26:24 +0000343 // Takes the operands in edx and eax and loads them as integers in eax
344 // and ecx.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000345 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
346 bool use_sse3,
347 Label* operand_conversion_failure);
348
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000349 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
350 // operands are pushed on the stack, and that their conversions to int32
351 // are in eax and ecx. Checks that the original numbers were in the int32
352 // range.
353 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
354 bool use_sse3,
355 Label* not_int32);
356
357 // Assumes that operands are smis or heap numbers and loads them
358 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000359 // Leaves operands unchanged.
360 static void LoadSSE2Operands(MacroAssembler* masm);
361
362 // Test if operands are numbers (smi or HeapNumber objects), and load
363 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
364 // either operand is not a number. Operands are in edx and eax.
365 // Leaves operands unchanged.
366 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
367
368 // Similar to LoadSSE2Operands but assumes that both operands are smis.
369 // Expects operands in edx, eax.
370 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000371
372 // Checks that the two floating point numbers loaded into xmm0 and xmm1
373 // have int32 values.
374 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
375 Label* non_int32,
376 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000377};
378
379
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000380// Get the integer part of a heap number. Surprisingly, all this bit twiddling
381// is faster than using the built-in instructions on floating point registers.
382// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
383// trashed registers.
384static void IntegerConvert(MacroAssembler* masm,
385 Register source,
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000386 bool use_sse3,
387 Label* conversion_failure) {
388 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
389 Label done, right_exponent, normal_exponent;
390 Register scratch = ebx;
391 Register scratch2 = edi;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000392 // Get exponent word.
393 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
394 // Get exponent alone in scratch2.
395 __ mov(scratch2, scratch);
396 __ and_(scratch2, HeapNumber::kExponentMask);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000397 if (use_sse3) {
398 CpuFeatures::Scope scope(SSE3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000399 // Check whether the exponent is too big for a 64 bit signed integer.
400 static const uint32_t kTooBigExponent =
401 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
402 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
403 __ j(greater_equal, conversion_failure);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000404 // Load x87 register with heap number.
405 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
406 // Reserve space for 64 bit answer.
407 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
408 // Do conversion, which cannot fail because we checked the exponent.
409 __ fisttp_d(Operand(esp, 0));
410 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
411 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
412 } else {
413 // Load ecx with zero. We use this either for the final shift or
414 // for the answer.
415 __ xor_(ecx, Operand(ecx));
416 // Check whether the exponent matches a 32 bit signed int that cannot be
417 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
418 // exponent is 30 (biased). This is the exponent that we are fastest at and
419 // also the highest exponent we can handle here.
420 const uint32_t non_smi_exponent =
421 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
422 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
423 // If we have a match of the int32-but-not-Smi exponent then skip some
424 // logic.
425 __ j(equal, &right_exponent);
426 // If the exponent is higher than that then go to slow case. This catches
427 // numbers that don't fit in a signed int32, infinities and NaNs.
428 __ j(less, &normal_exponent);
429
430 {
431 // Handle a big exponent. The only reason we have this code is that the
432 // >>> operator has a tendency to generate numbers with an exponent of 31.
433 const uint32_t big_non_smi_exponent =
434 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
435 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
436 __ j(not_equal, conversion_failure);
437 // We have the big exponent, typically from >>>. This means the number is
438 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
439 __ mov(scratch2, scratch);
440 __ and_(scratch2, HeapNumber::kMantissaMask);
441 // Put back the implicit 1.
442 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
443 // Shift up the mantissa bits to take up the space the exponent used to
444 // take. We just orred in the implicit bit so that took care of one and
445 // we want to use the full unsigned range so we subtract 1 bit from the
446 // shift distance.
447 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
448 __ shl(scratch2, big_shift_distance);
449 // Get the second half of the double.
450 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
451 // Shift down 21 bits to get the most significant 11 bits or the low
452 // mantissa word.
453 __ shr(ecx, 32 - big_shift_distance);
454 __ or_(ecx, Operand(scratch2));
455 // We have the answer in ecx, but we may need to negate it.
456 __ test(scratch, Operand(scratch));
457 __ j(positive, &done);
458 __ neg(ecx);
459 __ jmp(&done);
460 }
461
462 __ bind(&normal_exponent);
463 // Exponent word in scratch, exponent part of exponent word in scratch2.
464 // Zero in ecx.
465 // We know the exponent is smaller than 30 (biased). If it is less than
466 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
467 // it rounds to zero.
468 const uint32_t zero_exponent =
469 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
470 __ sub(Operand(scratch2), Immediate(zero_exponent));
471 // ecx already has a Smi zero.
472 __ j(less, &done);
473
474 // We have a shifted exponent between 0 and 30 in scratch2.
475 __ shr(scratch2, HeapNumber::kExponentShift);
476 __ mov(ecx, Immediate(30));
477 __ sub(ecx, Operand(scratch2));
478
479 __ bind(&right_exponent);
480 // Here ecx is the shift, scratch is the exponent word.
481 // Get the top bits of the mantissa.
482 __ and_(scratch, HeapNumber::kMantissaMask);
483 // Put back the implicit 1.
484 __ or_(scratch, 1 << HeapNumber::kExponentShift);
485 // Shift up the mantissa bits to take up the space the exponent used to
486 // take. We have kExponentShift + 1 significant bits int he low end of the
487 // word. Shift them to the top bits.
488 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
489 __ shl(scratch, shift_distance);
490 // Get the second half of the double. For some exponents we don't
491 // actually need this because the bits get shifted out again, but
492 // it's probably slower to test than just to do it.
493 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
494 // Shift down 22 bits to get the most significant 10 bits or the low
495 // mantissa word.
496 __ shr(scratch2, 32 - shift_distance);
497 __ or_(scratch2, Operand(scratch));
498 // Move down according to the exponent.
499 __ shr_cl(scratch2);
500 // Now the unsigned answer is in scratch2. We need to move it to ecx and
501 // we may need to fix the sign.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000502 Label negative;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000503 __ xor_(ecx, Operand(ecx));
504 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000505 __ j(greater, &negative, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000506 __ mov(ecx, scratch2);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000507 __ jmp(&done, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000508 __ bind(&negative);
509 __ sub(ecx, Operand(scratch2));
510 __ bind(&done);
511 }
512}
513
514
danno@chromium.org40cb8782011-05-25 07:58:50 +0000515const char* UnaryOpStub::GetName() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000516 if (name_ != NULL) return name_;
517 const int kMaxNameLength = 100;
518 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
519 kMaxNameLength);
520 if (name_ == NULL) return "OOM";
521 const char* op_name = Token::Name(op_);
522 const char* overwrite_name = NULL; // Make g++ happy.
523 switch (mode_) {
524 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
525 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
526 }
527
528 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000529 "UnaryOpStub_%s_%s_%s",
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000530 op_name,
531 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000532 UnaryOpIC::GetName(operand_type_));
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000533 return name_;
534}
535
536
537// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000538void UnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000539 switch (operand_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000540 case UnaryOpIC::UNINITIALIZED:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000541 GenerateTypeTransition(masm);
542 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000543 case UnaryOpIC::SMI:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000544 GenerateSmiStub(masm);
545 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000546 case UnaryOpIC::HEAP_NUMBER:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000547 GenerateHeapNumberStub(masm);
548 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000549 case UnaryOpIC::GENERIC:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000550 GenerateGenericStub(masm);
551 break;
552 }
553}
554
555
danno@chromium.org40cb8782011-05-25 07:58:50 +0000556void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000557 __ pop(ecx); // Save return address.
558 __ push(eax);
559 // the argument is now on top.
560 // Push this stub's key. Although the operation and the type info are
561 // encoded into the key, the encoding is opaque, so push them too.
562 __ push(Immediate(Smi::FromInt(MinorKey())));
563 __ push(Immediate(Smi::FromInt(op_)));
564 __ push(Immediate(Smi::FromInt(operand_type_)));
565
566 __ push(ecx); // Push return address.
567
568 // Patch the caller to an appropriate specialized stub and return the
569 // operation result to the caller of the stub.
570 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000571 ExternalReference(IC_Utility(IC::kUnaryOp_Patch),
572 masm->isolate()), 4, 1);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000573}
574
575
576// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000577void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000578 switch (op_) {
579 case Token::SUB:
580 GenerateSmiStubSub(masm);
581 break;
582 case Token::BIT_NOT:
583 GenerateSmiStubBitNot(masm);
584 break;
585 default:
586 UNREACHABLE();
587 }
588}
589
590
danno@chromium.org40cb8782011-05-25 07:58:50 +0000591void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000592 Label non_smi, undo, slow;
593 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
594 Label::kNear, Label::kNear, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000595 __ bind(&undo);
596 GenerateSmiCodeUndo(masm);
597 __ bind(&non_smi);
598 __ bind(&slow);
599 GenerateTypeTransition(masm);
600}
601
602
danno@chromium.org40cb8782011-05-25 07:58:50 +0000603void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000604 Label non_smi;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000605 GenerateSmiCodeBitNot(masm, &non_smi);
606 __ bind(&non_smi);
607 GenerateTypeTransition(masm);
608}
609
610
danno@chromium.org40cb8782011-05-25 07:58:50 +0000611void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
612 Label* non_smi,
613 Label* undo,
614 Label* slow,
615 Label::Distance non_smi_near,
616 Label::Distance undo_near,
617 Label::Distance slow_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000618 // Check whether the value is a smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000619 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000620
621 // We can't handle -0 with smis, so use a type transition for that case.
622 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000623 __ j(zero, slow, slow_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000624
625 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
626 __ mov(edx, Operand(eax));
627 __ Set(eax, Immediate(0));
628 __ sub(eax, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000629 __ j(overflow, undo, undo_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000630 __ ret(0);
631}
632
633
danno@chromium.org40cb8782011-05-25 07:58:50 +0000634void UnaryOpStub::GenerateSmiCodeBitNot(
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000635 MacroAssembler* masm,
636 Label* non_smi,
637 Label::Distance non_smi_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000638 // Check whether the value is a smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000639 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000640
641 // Flip bits and revert inverted smi-tag.
642 __ not_(eax);
643 __ and_(eax, ~kSmiTagMask);
644 __ ret(0);
645}
646
647
danno@chromium.org40cb8782011-05-25 07:58:50 +0000648void UnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000649 __ mov(eax, Operand(edx));
650}
651
652
653// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000654void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000655 switch (op_) {
656 case Token::SUB:
657 GenerateHeapNumberStubSub(masm);
658 break;
659 case Token::BIT_NOT:
660 GenerateHeapNumberStubBitNot(masm);
661 break;
662 default:
663 UNREACHABLE();
664 }
665}
666
667
danno@chromium.org40cb8782011-05-25 07:58:50 +0000668void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000669 Label non_smi, undo, slow, call_builtin;
670 GenerateSmiCodeSub(masm, &non_smi, &undo, &call_builtin, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000671 __ bind(&non_smi);
672 GenerateHeapNumberCodeSub(masm, &slow);
673 __ bind(&undo);
674 GenerateSmiCodeUndo(masm);
675 __ bind(&slow);
676 GenerateTypeTransition(masm);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000677 __ bind(&call_builtin);
678 GenerateGenericCodeFallback(masm);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000679}
680
681
danno@chromium.org40cb8782011-05-25 07:58:50 +0000682void UnaryOpStub::GenerateHeapNumberStubBitNot(
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000683 MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000684 Label non_smi, slow;
685 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000686 __ bind(&non_smi);
687 GenerateHeapNumberCodeBitNot(masm, &slow);
688 __ bind(&slow);
689 GenerateTypeTransition(masm);
690}
691
692
danno@chromium.org40cb8782011-05-25 07:58:50 +0000693void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
694 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000695 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
696 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
697 __ j(not_equal, slow);
698
699 if (mode_ == UNARY_OVERWRITE) {
700 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
701 Immediate(HeapNumber::kSignMask)); // Flip sign.
702 } else {
703 __ mov(edx, Operand(eax));
704 // edx: operand
705
706 Label slow_allocate_heapnumber, heapnumber_allocated;
707 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
708 __ jmp(&heapnumber_allocated);
709
710 __ bind(&slow_allocate_heapnumber);
711 __ EnterInternalFrame();
712 __ push(edx);
713 __ CallRuntime(Runtime::kNumberAlloc, 0);
714 __ pop(edx);
715 __ LeaveInternalFrame();
716
717 __ bind(&heapnumber_allocated);
718 // eax: allocated 'empty' number
719 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
720 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
721 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
722 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
723 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
724 }
725 __ ret(0);
726}
727
728
danno@chromium.org40cb8782011-05-25 07:58:50 +0000729void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
730 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000731 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
732 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
733 __ j(not_equal, slow);
734
735 // Convert the heap number in eax to an untagged integer in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000736 IntegerConvert(masm, eax, CpuFeatures::IsSupported(SSE3), slow);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000737
738 // Do the bitwise operation and check if the result fits in a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000739 Label try_float;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000740 __ not_(ecx);
741 __ cmp(ecx, 0xc0000000);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000742 __ j(sign, &try_float, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000743
744 // Tag the result as a smi and we're done.
745 STATIC_ASSERT(kSmiTagSize == 1);
746 __ lea(eax, Operand(ecx, times_2, kSmiTag));
747 __ ret(0);
748
749 // Try to store the result in a heap number.
750 __ bind(&try_float);
751 if (mode_ == UNARY_NO_OVERWRITE) {
752 Label slow_allocate_heapnumber, heapnumber_allocated;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000753 __ mov(ebx, eax);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000754 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
755 __ jmp(&heapnumber_allocated);
756
757 __ bind(&slow_allocate_heapnumber);
758 __ EnterInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000759 // Push the original HeapNumber on the stack. The integer value can't
760 // be stored since it's untagged and not in the smi range (so we can't
761 // smi-tag it). We'll recalculate the value after the GC instead.
762 __ push(ebx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000763 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000764 // New HeapNumber is in eax.
765 __ pop(edx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000766 __ LeaveInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000767 // IntegerConvert uses ebx and edi as scratch registers.
768 // This conversion won't go slow-case.
769 IntegerConvert(masm, edx, CpuFeatures::IsSupported(SSE3), slow);
770 __ not_(ecx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000771
772 __ bind(&heapnumber_allocated);
773 }
774 if (CpuFeatures::IsSupported(SSE2)) {
775 CpuFeatures::Scope use_sse2(SSE2);
776 __ cvtsi2sd(xmm0, Operand(ecx));
777 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
778 } else {
779 __ push(ecx);
780 __ fild_s(Operand(esp, 0));
781 __ pop(ecx);
782 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
783 }
784 __ ret(0);
785}
786
787
788// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000789void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000790 switch (op_) {
791 case Token::SUB:
792 GenerateGenericStubSub(masm);
793 break;
794 case Token::BIT_NOT:
795 GenerateGenericStubBitNot(masm);
796 break;
797 default:
798 UNREACHABLE();
799 }
800}
801
802
danno@chromium.org40cb8782011-05-25 07:58:50 +0000803void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000804 Label non_smi, undo, slow;
805 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000806 __ bind(&non_smi);
807 GenerateHeapNumberCodeSub(masm, &slow);
808 __ bind(&undo);
809 GenerateSmiCodeUndo(masm);
810 __ bind(&slow);
811 GenerateGenericCodeFallback(masm);
812}
813
814
danno@chromium.org40cb8782011-05-25 07:58:50 +0000815void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000816 Label non_smi, slow;
817 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000818 __ bind(&non_smi);
819 GenerateHeapNumberCodeBitNot(masm, &slow);
820 __ bind(&slow);
821 GenerateGenericCodeFallback(masm);
822}
823
824
danno@chromium.org40cb8782011-05-25 07:58:50 +0000825void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000826 // Handle the slow case by jumping to the corresponding JavaScript builtin.
827 __ pop(ecx); // pop return address.
828 __ push(eax);
829 __ push(ecx); // push return address
830 switch (op_) {
831 case Token::SUB:
832 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
833 break;
834 case Token::BIT_NOT:
835 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
836 break;
837 default:
838 UNREACHABLE();
839 }
840}
841
842
danno@chromium.org40cb8782011-05-25 07:58:50 +0000843void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000844 __ pop(ecx); // Save return address.
845 __ push(edx);
846 __ push(eax);
847 // Left and right arguments are now on top.
848 // Push this stub's key. Although the operation and the type info are
849 // encoded into the key, the encoding is opaque, so push them too.
850 __ push(Immediate(Smi::FromInt(MinorKey())));
851 __ push(Immediate(Smi::FromInt(op_)));
852 __ push(Immediate(Smi::FromInt(operands_type_)));
853
854 __ push(ecx); // Push return address.
855
856 // Patch the caller to an appropriate specialized stub and return the
857 // operation result to the caller of the stub.
858 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000859 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000860 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000861 5,
862 1);
863}
864
865
866// Prepare for a type transition runtime call when the args are already on
867// the stack, under the return address.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000868void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000869 __ pop(ecx); // Save return address.
870 // Left and right arguments are already on top of the stack.
871 // Push this stub's key. Although the operation and the type info are
872 // encoded into the key, the encoding is opaque, so push them too.
873 __ push(Immediate(Smi::FromInt(MinorKey())));
874 __ push(Immediate(Smi::FromInt(op_)));
875 __ push(Immediate(Smi::FromInt(operands_type_)));
876
877 __ push(ecx); // Push return address.
878
879 // Patch the caller to an appropriate specialized stub and return the
880 // operation result to the caller of the stub.
881 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000882 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000883 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000884 5,
885 1);
886}
887
888
danno@chromium.org40cb8782011-05-25 07:58:50 +0000889void BinaryOpStub::Generate(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000890 switch (operands_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000891 case BinaryOpIC::UNINITIALIZED:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000892 GenerateTypeTransition(masm);
893 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000894 case BinaryOpIC::SMI:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000895 GenerateSmiStub(masm);
896 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000897 case BinaryOpIC::INT32:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000898 GenerateInt32Stub(masm);
899 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000900 case BinaryOpIC::HEAP_NUMBER:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000901 GenerateHeapNumberStub(masm);
902 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000903 case BinaryOpIC::ODDBALL:
lrn@chromium.org7516f052011-03-30 08:52:27 +0000904 GenerateOddballStub(masm);
905 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000906 case BinaryOpIC::BOTH_STRING:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000907 GenerateBothStringStub(masm);
908 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000909 case BinaryOpIC::STRING:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000910 GenerateStringStub(masm);
911 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000912 case BinaryOpIC::GENERIC:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000913 GenerateGeneric(masm);
914 break;
915 default:
916 UNREACHABLE();
917 }
918}
919
920
danno@chromium.org40cb8782011-05-25 07:58:50 +0000921const char* BinaryOpStub::GetName() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000922 if (name_ != NULL) return name_;
923 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000924 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
925 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000926 if (name_ == NULL) return "OOM";
927 const char* op_name = Token::Name(op_);
928 const char* overwrite_name;
929 switch (mode_) {
930 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
931 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
932 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
933 default: overwrite_name = "UnknownOverwrite"; break;
934 }
935
936 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000937 "BinaryOpStub_%s_%s_%s",
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000938 op_name,
939 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000940 BinaryOpIC::GetName(operands_type_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000941 return name_;
942}
943
944
danno@chromium.org40cb8782011-05-25 07:58:50 +0000945void BinaryOpStub::GenerateSmiCode(
946 MacroAssembler* masm,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000947 Label* slow,
948 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
949 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
950 // dividend in eax and edx free for the division. Use eax, ebx for those.
951 Comment load_comment(masm, "-- Load arguments");
952 Register left = edx;
953 Register right = eax;
954 if (op_ == Token::DIV || op_ == Token::MOD) {
955 left = eax;
956 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000957 __ mov(ebx, eax);
958 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000959 }
960
961
962 // 2. Prepare the smi check of both operands by oring them together.
963 Comment smi_check_comment(masm, "-- Smi check arguments");
964 Label not_smis;
965 Register combined = ecx;
966 ASSERT(!left.is(combined) && !right.is(combined));
967 switch (op_) {
968 case Token::BIT_OR:
969 // Perform the operation into eax and smi check the result. Preserve
970 // eax in case the result is not a smi.
971 ASSERT(!left.is(ecx) && !right.is(ecx));
972 __ mov(ecx, right);
973 __ or_(right, Operand(left)); // Bitwise or is commutative.
974 combined = right;
975 break;
976
977 case Token::BIT_XOR:
978 case Token::BIT_AND:
979 case Token::ADD:
980 case Token::SUB:
981 case Token::MUL:
982 case Token::DIV:
983 case Token::MOD:
984 __ mov(combined, right);
985 __ or_(combined, Operand(left));
986 break;
987
988 case Token::SHL:
989 case Token::SAR:
990 case Token::SHR:
991 // Move the right operand into ecx for the shift operation, use eax
992 // for the smi check register.
993 ASSERT(!left.is(ecx) && !right.is(ecx));
994 __ mov(ecx, right);
995 __ or_(right, Operand(left));
996 combined = right;
997 break;
998
999 default:
1000 break;
1001 }
1002
1003 // 3. Perform the smi check of the operands.
1004 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001005 __ JumpIfNotSmi(combined, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001006
1007 // 4. Operands are both smis, perform the operation leaving the result in
1008 // eax and check the result if necessary.
1009 Comment perform_smi(masm, "-- Perform smi operation");
1010 Label use_fp_on_smis;
1011 switch (op_) {
1012 case Token::BIT_OR:
1013 // Nothing to do.
1014 break;
1015
1016 case Token::BIT_XOR:
1017 ASSERT(right.is(eax));
1018 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1019 break;
1020
1021 case Token::BIT_AND:
1022 ASSERT(right.is(eax));
1023 __ and_(right, Operand(left)); // Bitwise and is commutative.
1024 break;
1025
1026 case Token::SHL:
1027 // Remove tags from operands (but keep sign).
1028 __ SmiUntag(left);
1029 __ SmiUntag(ecx);
1030 // Perform the operation.
1031 __ shl_cl(left);
1032 // Check that the *signed* result fits in a smi.
1033 __ cmp(left, 0xc0000000);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001034 __ j(sign, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001035 // Tag the result and store it in register eax.
1036 __ SmiTag(left);
1037 __ mov(eax, left);
1038 break;
1039
1040 case Token::SAR:
1041 // Remove tags from operands (but keep sign).
1042 __ SmiUntag(left);
1043 __ SmiUntag(ecx);
1044 // Perform the operation.
1045 __ sar_cl(left);
1046 // Tag the result and store it in register eax.
1047 __ SmiTag(left);
1048 __ mov(eax, left);
1049 break;
1050
1051 case Token::SHR:
1052 // Remove tags from operands (but keep sign).
1053 __ SmiUntag(left);
1054 __ SmiUntag(ecx);
1055 // Perform the operation.
1056 __ shr_cl(left);
1057 // Check that the *unsigned* result fits in a smi.
1058 // Neither of the two high-order bits can be set:
1059 // - 0x80000000: high bit would be lost when smi tagging.
1060 // - 0x40000000: this number would convert to negative when
1061 // Smi tagging these two cases can only happen with shifts
1062 // by 0 or 1 when handed a valid smi.
1063 __ test(left, Immediate(0xc0000000));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001064 __ j(not_zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001065 // Tag the result and store it in register eax.
1066 __ SmiTag(left);
1067 __ mov(eax, left);
1068 break;
1069
1070 case Token::ADD:
1071 ASSERT(right.is(eax));
1072 __ add(right, Operand(left)); // Addition is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001073 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001074 break;
1075
1076 case Token::SUB:
1077 __ sub(left, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001078 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001079 __ mov(eax, left);
1080 break;
1081
1082 case Token::MUL:
1083 // If the smi tag is 0 we can just leave the tag on one operand.
1084 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1085 // We can't revert the multiplication if the result is not a smi
1086 // so save the right operand.
1087 __ mov(ebx, right);
1088 // Remove tag from one of the operands (but keep sign).
1089 __ SmiUntag(right);
1090 // Do multiplication.
1091 __ imul(right, Operand(left)); // Multiplication is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001092 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001093 // Check for negative zero result. Use combined = left | right.
1094 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1095 break;
1096
1097 case Token::DIV:
1098 // We can't revert the division if the result is not a smi so
1099 // save the left operand.
1100 __ mov(edi, left);
1101 // Check for 0 divisor.
1102 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001103 __ j(zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001104 // Sign extend left into edx:eax.
1105 ASSERT(left.is(eax));
1106 __ cdq();
1107 // Divide edx:eax by right.
1108 __ idiv(right);
1109 // Check for the corner case of dividing the most negative smi by
1110 // -1. We cannot use the overflow flag, since it is not set by idiv
1111 // instruction.
1112 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1113 __ cmp(eax, 0x40000000);
1114 __ j(equal, &use_fp_on_smis);
1115 // Check for negative zero result. Use combined = left | right.
1116 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1117 // Check that the remainder is zero.
1118 __ test(edx, Operand(edx));
1119 __ j(not_zero, &use_fp_on_smis);
1120 // Tag the result and store it in register eax.
1121 __ SmiTag(eax);
1122 break;
1123
1124 case Token::MOD:
1125 // Check for 0 divisor.
1126 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001127 __ j(zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001128
1129 // Sign extend left into edx:eax.
1130 ASSERT(left.is(eax));
1131 __ cdq();
1132 // Divide edx:eax by right.
1133 __ idiv(right);
1134 // Check for negative zero result. Use combined = left | right.
1135 __ NegativeZeroTest(edx, combined, slow);
1136 // Move remainder to register eax.
1137 __ mov(eax, edx);
1138 break;
1139
1140 default:
1141 UNREACHABLE();
1142 }
1143
1144 // 5. Emit return of result in eax. Some operations have registers pushed.
1145 switch (op_) {
1146 case Token::ADD:
1147 case Token::SUB:
1148 case Token::MUL:
1149 case Token::DIV:
1150 __ ret(0);
1151 break;
1152 case Token::MOD:
1153 case Token::BIT_OR:
1154 case Token::BIT_AND:
1155 case Token::BIT_XOR:
1156 case Token::SAR:
1157 case Token::SHL:
1158 case Token::SHR:
1159 __ ret(2 * kPointerSize);
1160 break;
1161 default:
1162 UNREACHABLE();
1163 }
1164
1165 // 6. For some operations emit inline code to perform floating point
1166 // operations on known smis (e.g., if the result of the operation
1167 // overflowed the smi range).
1168 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1169 __ bind(&use_fp_on_smis);
1170 switch (op_) {
1171 // Undo the effects of some operations, and some register moves.
1172 case Token::SHL:
1173 // The arguments are saved on the stack, and only used from there.
1174 break;
1175 case Token::ADD:
1176 // Revert right = right + left.
1177 __ sub(right, Operand(left));
1178 break;
1179 case Token::SUB:
1180 // Revert left = left - right.
1181 __ add(left, Operand(right));
1182 break;
1183 case Token::MUL:
1184 // Right was clobbered but a copy is in ebx.
1185 __ mov(right, ebx);
1186 break;
1187 case Token::DIV:
1188 // Left was clobbered but a copy is in edi. Right is in ebx for
1189 // division. They should be in eax, ebx for jump to not_smi.
1190 __ mov(eax, edi);
1191 break;
1192 default:
1193 // No other operators jump to use_fp_on_smis.
1194 break;
1195 }
1196 __ jmp(&not_smis);
1197 } else {
1198 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1199 switch (op_) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001200 case Token::SHL:
1201 case Token::SHR: {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001202 Comment perform_float(masm, "-- Perform float operation on smis");
1203 __ bind(&use_fp_on_smis);
1204 // Result we want is in left == edx, so we can put the allocated heap
1205 // number in eax.
1206 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1207 // Store the result in the HeapNumber and return.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001208 // It's OK to overwrite the arguments on the stack because we
1209 // are about to return.
1210 if (op_ == Token::SHR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001211 __ mov(Operand(esp, 1 * kPointerSize), left);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001212 __ mov(Operand(esp, 2 * kPointerSize), Immediate(0));
1213 __ fild_d(Operand(esp, 1 * kPointerSize));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001214 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001215 } else {
1216 ASSERT_EQ(Token::SHL, op_);
1217 if (CpuFeatures::IsSupported(SSE2)) {
1218 CpuFeatures::Scope use_sse2(SSE2);
1219 __ cvtsi2sd(xmm0, Operand(left));
1220 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1221 } else {
1222 __ mov(Operand(esp, 1 * kPointerSize), left);
1223 __ fild_s(Operand(esp, 1 * kPointerSize));
1224 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1225 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001226 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001227 __ ret(2 * kPointerSize);
1228 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001229 }
1230
1231 case Token::ADD:
1232 case Token::SUB:
1233 case Token::MUL:
1234 case Token::DIV: {
1235 Comment perform_float(masm, "-- Perform float operation on smis");
1236 __ bind(&use_fp_on_smis);
1237 // Restore arguments to edx, eax.
1238 switch (op_) {
1239 case Token::ADD:
1240 // Revert right = right + left.
1241 __ sub(right, Operand(left));
1242 break;
1243 case Token::SUB:
1244 // Revert left = left - right.
1245 __ add(left, Operand(right));
1246 break;
1247 case Token::MUL:
1248 // Right was clobbered but a copy is in ebx.
1249 __ mov(right, ebx);
1250 break;
1251 case Token::DIV:
1252 // Left was clobbered but a copy is in edi. Right is in ebx for
1253 // division.
1254 __ mov(edx, edi);
1255 __ mov(eax, right);
1256 break;
1257 default: UNREACHABLE();
1258 break;
1259 }
1260 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001261 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001262 CpuFeatures::Scope use_sse2(SSE2);
1263 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1264 switch (op_) {
1265 case Token::ADD: __ addsd(xmm0, xmm1); break;
1266 case Token::SUB: __ subsd(xmm0, xmm1); break;
1267 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1268 case Token::DIV: __ divsd(xmm0, xmm1); break;
1269 default: UNREACHABLE();
1270 }
1271 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1272 } else { // SSE2 not available, use FPU.
1273 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1274 switch (op_) {
1275 case Token::ADD: __ faddp(1); break;
1276 case Token::SUB: __ fsubp(1); break;
1277 case Token::MUL: __ fmulp(1); break;
1278 case Token::DIV: __ fdivp(1); break;
1279 default: UNREACHABLE();
1280 }
1281 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1282 }
1283 __ mov(eax, ecx);
1284 __ ret(0);
1285 break;
1286 }
1287
1288 default:
1289 break;
1290 }
1291 }
1292
1293 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1294 // edx and eax.
1295 Comment done_comment(masm, "-- Enter non-smi code");
1296 __ bind(&not_smis);
1297 switch (op_) {
1298 case Token::BIT_OR:
1299 case Token::SHL:
1300 case Token::SAR:
1301 case Token::SHR:
1302 // Right operand is saved in ecx and eax was destroyed by the smi
1303 // check.
1304 __ mov(eax, ecx);
1305 break;
1306
1307 case Token::DIV:
1308 case Token::MOD:
1309 // Operands are in eax, ebx at this point.
1310 __ mov(edx, eax);
1311 __ mov(eax, ebx);
1312 break;
1313
1314 default:
1315 break;
1316 }
1317}
1318
1319
danno@chromium.org40cb8782011-05-25 07:58:50 +00001320void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001321 Label call_runtime;
1322
1323 switch (op_) {
1324 case Token::ADD:
1325 case Token::SUB:
1326 case Token::MUL:
1327 case Token::DIV:
1328 break;
1329 case Token::MOD:
1330 case Token::BIT_OR:
1331 case Token::BIT_AND:
1332 case Token::BIT_XOR:
1333 case Token::SAR:
1334 case Token::SHL:
1335 case Token::SHR:
1336 GenerateRegisterArgsPush(masm);
1337 break;
1338 default:
1339 UNREACHABLE();
1340 }
1341
danno@chromium.org40cb8782011-05-25 07:58:50 +00001342 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1343 result_type_ == BinaryOpIC::SMI) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001344 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1345 } else {
1346 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1347 }
1348 __ bind(&call_runtime);
1349 switch (op_) {
1350 case Token::ADD:
1351 case Token::SUB:
1352 case Token::MUL:
1353 case Token::DIV:
1354 GenerateTypeTransition(masm);
1355 break;
1356 case Token::MOD:
1357 case Token::BIT_OR:
1358 case Token::BIT_AND:
1359 case Token::BIT_XOR:
1360 case Token::SAR:
1361 case Token::SHL:
1362 case Token::SHR:
1363 GenerateTypeTransitionWithSavedArgs(masm);
1364 break;
1365 default:
1366 UNREACHABLE();
1367 }
1368}
1369
1370
danno@chromium.org40cb8782011-05-25 07:58:50 +00001371void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1372 ASSERT(operands_type_ == BinaryOpIC::STRING);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001373 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001374 // Try to add arguments as strings, otherwise, transition to the generic
danno@chromium.org40cb8782011-05-25 07:58:50 +00001375 // BinaryOpIC type.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001376 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001377 GenerateTypeTransition(masm);
1378}
1379
1380
danno@chromium.org40cb8782011-05-25 07:58:50 +00001381void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001382 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001383 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001384 ASSERT(op_ == Token::ADD);
1385 // If both arguments are strings, call the string add stub.
1386 // Otherwise, do a transition.
1387
1388 // Registers containing left and right operands respectively.
1389 Register left = edx;
1390 Register right = eax;
1391
1392 // Test if left operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001393 __ JumpIfSmi(left, &call_runtime);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001394 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1395 __ j(above_equal, &call_runtime);
1396
1397 // Test if right operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001398 __ JumpIfSmi(right, &call_runtime);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001399 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1400 __ j(above_equal, &call_runtime);
1401
1402 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1403 GenerateRegisterArgsPush(masm);
1404 __ TailCallStub(&string_add_stub);
1405
1406 __ bind(&call_runtime);
1407 GenerateTypeTransition(masm);
1408}
1409
1410
danno@chromium.org40cb8782011-05-25 07:58:50 +00001411void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001412 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001413 ASSERT(operands_type_ == BinaryOpIC::INT32);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001414
1415 // Floating point case.
1416 switch (op_) {
1417 case Token::ADD:
1418 case Token::SUB:
1419 case Token::MUL:
1420 case Token::DIV: {
1421 Label not_floats;
1422 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001423 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001424 CpuFeatures::Scope use_sse2(SSE2);
1425 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1426 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1427 switch (op_) {
1428 case Token::ADD: __ addsd(xmm0, xmm1); break;
1429 case Token::SUB: __ subsd(xmm0, xmm1); break;
1430 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1431 case Token::DIV: __ divsd(xmm0, xmm1); break;
1432 default: UNREACHABLE();
1433 }
1434 // Check result type if it is currently Int32.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001435 if (result_type_ <= BinaryOpIC::INT32) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001436 __ cvttsd2si(ecx, Operand(xmm0));
1437 __ cvtsi2sd(xmm2, Operand(ecx));
1438 __ ucomisd(xmm0, xmm2);
1439 __ j(not_zero, &not_int32);
1440 __ j(carry, &not_int32);
1441 }
1442 GenerateHeapResultAllocation(masm, &call_runtime);
1443 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1444 __ ret(0);
1445 } else { // SSE2 not available, use FPU.
1446 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1447 FloatingPointHelper::LoadFloatOperands(
1448 masm,
1449 ecx,
1450 FloatingPointHelper::ARGS_IN_REGISTERS);
1451 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1452 switch (op_) {
1453 case Token::ADD: __ faddp(1); break;
1454 case Token::SUB: __ fsubp(1); break;
1455 case Token::MUL: __ fmulp(1); break;
1456 case Token::DIV: __ fdivp(1); break;
1457 default: UNREACHABLE();
1458 }
1459 Label after_alloc_failure;
1460 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1461 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1462 __ ret(0);
1463 __ bind(&after_alloc_failure);
1464 __ ffree();
1465 __ jmp(&call_runtime);
1466 }
1467
1468 __ bind(&not_floats);
1469 __ bind(&not_int32);
1470 GenerateTypeTransition(masm);
1471 break;
1472 }
1473
1474 case Token::MOD: {
1475 // For MOD we go directly to runtime in the non-smi case.
1476 break;
1477 }
1478 case Token::BIT_OR:
1479 case Token::BIT_AND:
1480 case Token::BIT_XOR:
1481 case Token::SAR:
1482 case Token::SHL:
1483 case Token::SHR: {
1484 GenerateRegisterArgsPush(masm);
1485 Label not_floats;
1486 Label not_int32;
1487 Label non_smi_result;
1488 /* {
1489 CpuFeatures::Scope use_sse2(SSE2);
1490 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1491 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1492 }*/
1493 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1494 use_sse3_,
1495 &not_floats);
1496 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1497 &not_int32);
1498 switch (op_) {
1499 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1500 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1501 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1502 case Token::SAR: __ sar_cl(eax); break;
1503 case Token::SHL: __ shl_cl(eax); break;
1504 case Token::SHR: __ shr_cl(eax); break;
1505 default: UNREACHABLE();
1506 }
1507 if (op_ == Token::SHR) {
1508 // Check if result is non-negative and fits in a smi.
1509 __ test(eax, Immediate(0xc0000000));
1510 __ j(not_zero, &call_runtime);
1511 } else {
1512 // Check if result fits in a smi.
1513 __ cmp(eax, 0xc0000000);
1514 __ j(negative, &non_smi_result);
1515 }
1516 // Tag smi result and return.
1517 __ SmiTag(eax);
1518 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1519
1520 // All ops except SHR return a signed int32 that we load in
1521 // a HeapNumber.
1522 if (op_ != Token::SHR) {
1523 __ bind(&non_smi_result);
1524 // Allocate a heap number if needed.
1525 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001526 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001527 switch (mode_) {
1528 case OVERWRITE_LEFT:
1529 case OVERWRITE_RIGHT:
1530 // If the operand was an object, we skip the
1531 // allocation of a heap number.
1532 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1533 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001534 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001535 // Fall through!
1536 case NO_OVERWRITE:
1537 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1538 __ bind(&skip_allocation);
1539 break;
1540 default: UNREACHABLE();
1541 }
1542 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001543 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001544 CpuFeatures::Scope use_sse2(SSE2);
1545 __ cvtsi2sd(xmm0, Operand(ebx));
1546 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1547 } else {
1548 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1549 __ fild_s(Operand(esp, 1 * kPointerSize));
1550 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1551 }
1552 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1553 }
1554
1555 __ bind(&not_floats);
1556 __ bind(&not_int32);
1557 GenerateTypeTransitionWithSavedArgs(masm);
1558 break;
1559 }
1560 default: UNREACHABLE(); break;
1561 }
1562
1563 // If an allocation fails, or SHR or MOD hit a hard case,
1564 // use the runtime system to get the correct result.
1565 __ bind(&call_runtime);
1566
1567 switch (op_) {
1568 case Token::ADD:
1569 GenerateRegisterArgsPush(masm);
1570 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1571 break;
1572 case Token::SUB:
1573 GenerateRegisterArgsPush(masm);
1574 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1575 break;
1576 case Token::MUL:
1577 GenerateRegisterArgsPush(masm);
1578 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1579 break;
1580 case Token::DIV:
1581 GenerateRegisterArgsPush(masm);
1582 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1583 break;
1584 case Token::MOD:
1585 GenerateRegisterArgsPush(masm);
1586 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1587 break;
1588 case Token::BIT_OR:
1589 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1590 break;
1591 case Token::BIT_AND:
1592 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1593 break;
1594 case Token::BIT_XOR:
1595 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1596 break;
1597 case Token::SAR:
1598 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1599 break;
1600 case Token::SHL:
1601 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1602 break;
1603 case Token::SHR:
1604 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1605 break;
1606 default:
1607 UNREACHABLE();
1608 }
1609}
1610
1611
danno@chromium.org40cb8782011-05-25 07:58:50 +00001612void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001613 if (op_ == Token::ADD) {
1614 // Handle string addition here, because it is the only operation
1615 // that does not do a ToNumber conversion on the operands.
1616 GenerateAddStrings(masm);
1617 }
1618
danno@chromium.org160a7b02011-04-18 15:51:38 +00001619 Factory* factory = masm->isolate()->factory();
1620
lrn@chromium.org7516f052011-03-30 08:52:27 +00001621 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001622 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001623 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001624 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001625 if (Token::IsBitOp(op_)) {
1626 __ xor_(edx, Operand(edx));
1627 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001628 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001629 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001630 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001631 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001632 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001633 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001634 if (Token::IsBitOp(op_)) {
1635 __ xor_(eax, Operand(eax));
1636 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001637 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001638 }
1639 __ bind(&done);
1640
1641 GenerateHeapNumberStub(masm);
1642}
1643
1644
danno@chromium.org40cb8782011-05-25 07:58:50 +00001645void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001646 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001647
1648 // Floating point case.
1649 switch (op_) {
1650 case Token::ADD:
1651 case Token::SUB:
1652 case Token::MUL:
1653 case Token::DIV: {
1654 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001655 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001656 CpuFeatures::Scope use_sse2(SSE2);
1657 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1658
1659 switch (op_) {
1660 case Token::ADD: __ addsd(xmm0, xmm1); break;
1661 case Token::SUB: __ subsd(xmm0, xmm1); break;
1662 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1663 case Token::DIV: __ divsd(xmm0, xmm1); break;
1664 default: UNREACHABLE();
1665 }
1666 GenerateHeapResultAllocation(masm, &call_runtime);
1667 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1668 __ ret(0);
1669 } else { // SSE2 not available, use FPU.
1670 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1671 FloatingPointHelper::LoadFloatOperands(
1672 masm,
1673 ecx,
1674 FloatingPointHelper::ARGS_IN_REGISTERS);
1675 switch (op_) {
1676 case Token::ADD: __ faddp(1); break;
1677 case Token::SUB: __ fsubp(1); break;
1678 case Token::MUL: __ fmulp(1); break;
1679 case Token::DIV: __ fdivp(1); break;
1680 default: UNREACHABLE();
1681 }
1682 Label after_alloc_failure;
1683 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1684 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1685 __ ret(0);
1686 __ bind(&after_alloc_failure);
1687 __ ffree();
1688 __ jmp(&call_runtime);
1689 }
1690
1691 __ bind(&not_floats);
1692 GenerateTypeTransition(masm);
1693 break;
1694 }
1695
1696 case Token::MOD: {
1697 // For MOD we go directly to runtime in the non-smi case.
1698 break;
1699 }
1700 case Token::BIT_OR:
1701 case Token::BIT_AND:
1702 case Token::BIT_XOR:
1703 case Token::SAR:
1704 case Token::SHL:
1705 case Token::SHR: {
1706 GenerateRegisterArgsPush(masm);
1707 Label not_floats;
1708 Label non_smi_result;
1709 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1710 use_sse3_,
1711 &not_floats);
1712 switch (op_) {
1713 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1714 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1715 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1716 case Token::SAR: __ sar_cl(eax); break;
1717 case Token::SHL: __ shl_cl(eax); break;
1718 case Token::SHR: __ shr_cl(eax); break;
1719 default: UNREACHABLE();
1720 }
1721 if (op_ == Token::SHR) {
1722 // Check if result is non-negative and fits in a smi.
1723 __ test(eax, Immediate(0xc0000000));
1724 __ j(not_zero, &call_runtime);
1725 } else {
1726 // Check if result fits in a smi.
1727 __ cmp(eax, 0xc0000000);
1728 __ j(negative, &non_smi_result);
1729 }
1730 // Tag smi result and return.
1731 __ SmiTag(eax);
1732 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1733
1734 // All ops except SHR return a signed int32 that we load in
1735 // a HeapNumber.
1736 if (op_ != Token::SHR) {
1737 __ bind(&non_smi_result);
1738 // Allocate a heap number if needed.
1739 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001740 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001741 switch (mode_) {
1742 case OVERWRITE_LEFT:
1743 case OVERWRITE_RIGHT:
1744 // If the operand was an object, we skip the
1745 // allocation of a heap number.
1746 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1747 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001748 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001749 // Fall through!
1750 case NO_OVERWRITE:
1751 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1752 __ bind(&skip_allocation);
1753 break;
1754 default: UNREACHABLE();
1755 }
1756 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001757 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001758 CpuFeatures::Scope use_sse2(SSE2);
1759 __ cvtsi2sd(xmm0, Operand(ebx));
1760 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1761 } else {
1762 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1763 __ fild_s(Operand(esp, 1 * kPointerSize));
1764 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1765 }
1766 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1767 }
1768
1769 __ bind(&not_floats);
1770 GenerateTypeTransitionWithSavedArgs(masm);
1771 break;
1772 }
1773 default: UNREACHABLE(); break;
1774 }
1775
1776 // If an allocation fails, or SHR or MOD hit a hard case,
1777 // use the runtime system to get the correct result.
1778 __ bind(&call_runtime);
1779
1780 switch (op_) {
1781 case Token::ADD:
1782 GenerateRegisterArgsPush(masm);
1783 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1784 break;
1785 case Token::SUB:
1786 GenerateRegisterArgsPush(masm);
1787 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1788 break;
1789 case Token::MUL:
1790 GenerateRegisterArgsPush(masm);
1791 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1792 break;
1793 case Token::DIV:
1794 GenerateRegisterArgsPush(masm);
1795 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1796 break;
1797 case Token::MOD:
1798 GenerateRegisterArgsPush(masm);
1799 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1800 break;
1801 case Token::BIT_OR:
1802 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1803 break;
1804 case Token::BIT_AND:
1805 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1806 break;
1807 case Token::BIT_XOR:
1808 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1809 break;
1810 case Token::SAR:
1811 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1812 break;
1813 case Token::SHL:
1814 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1815 break;
1816 case Token::SHR:
1817 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1818 break;
1819 default:
1820 UNREACHABLE();
1821 }
1822}
1823
1824
danno@chromium.org40cb8782011-05-25 07:58:50 +00001825void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001826 Label call_runtime;
1827
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001828 Counters* counters = masm->isolate()->counters();
1829 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001830
1831 switch (op_) {
1832 case Token::ADD:
1833 case Token::SUB:
1834 case Token::MUL:
1835 case Token::DIV:
1836 break;
1837 case Token::MOD:
1838 case Token::BIT_OR:
1839 case Token::BIT_AND:
1840 case Token::BIT_XOR:
1841 case Token::SAR:
1842 case Token::SHL:
1843 case Token::SHR:
1844 GenerateRegisterArgsPush(masm);
1845 break;
1846 default:
1847 UNREACHABLE();
1848 }
1849
1850 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1851
1852 // Floating point case.
1853 switch (op_) {
1854 case Token::ADD:
1855 case Token::SUB:
1856 case Token::MUL:
1857 case Token::DIV: {
1858 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001859 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001860 CpuFeatures::Scope use_sse2(SSE2);
1861 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1862
1863 switch (op_) {
1864 case Token::ADD: __ addsd(xmm0, xmm1); break;
1865 case Token::SUB: __ subsd(xmm0, xmm1); break;
1866 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1867 case Token::DIV: __ divsd(xmm0, xmm1); break;
1868 default: UNREACHABLE();
1869 }
1870 GenerateHeapResultAllocation(masm, &call_runtime);
1871 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1872 __ ret(0);
1873 } else { // SSE2 not available, use FPU.
1874 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1875 FloatingPointHelper::LoadFloatOperands(
1876 masm,
1877 ecx,
1878 FloatingPointHelper::ARGS_IN_REGISTERS);
1879 switch (op_) {
1880 case Token::ADD: __ faddp(1); break;
1881 case Token::SUB: __ fsubp(1); break;
1882 case Token::MUL: __ fmulp(1); break;
1883 case Token::DIV: __ fdivp(1); break;
1884 default: UNREACHABLE();
1885 }
1886 Label after_alloc_failure;
1887 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1888 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1889 __ ret(0);
1890 __ bind(&after_alloc_failure);
1891 __ ffree();
1892 __ jmp(&call_runtime);
1893 }
1894 __ bind(&not_floats);
1895 break;
1896 }
1897 case Token::MOD: {
1898 // For MOD we go directly to runtime in the non-smi case.
1899 break;
1900 }
1901 case Token::BIT_OR:
1902 case Token::BIT_AND:
1903 case Token::BIT_XOR:
1904 case Token::SAR:
1905 case Token::SHL:
1906 case Token::SHR: {
1907 Label non_smi_result;
1908 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1909 use_sse3_,
1910 &call_runtime);
1911 switch (op_) {
1912 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1913 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1914 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1915 case Token::SAR: __ sar_cl(eax); break;
1916 case Token::SHL: __ shl_cl(eax); break;
1917 case Token::SHR: __ shr_cl(eax); break;
1918 default: UNREACHABLE();
1919 }
1920 if (op_ == Token::SHR) {
1921 // Check if result is non-negative and fits in a smi.
1922 __ test(eax, Immediate(0xc0000000));
1923 __ j(not_zero, &call_runtime);
1924 } else {
1925 // Check if result fits in a smi.
1926 __ cmp(eax, 0xc0000000);
1927 __ j(negative, &non_smi_result);
1928 }
1929 // Tag smi result and return.
1930 __ SmiTag(eax);
1931 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1932
1933 // All ops except SHR return a signed int32 that we load in
1934 // a HeapNumber.
1935 if (op_ != Token::SHR) {
1936 __ bind(&non_smi_result);
1937 // Allocate a heap number if needed.
1938 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001939 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001940 switch (mode_) {
1941 case OVERWRITE_LEFT:
1942 case OVERWRITE_RIGHT:
1943 // If the operand was an object, we skip the
1944 // allocation of a heap number.
1945 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1946 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001947 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001948 // Fall through!
1949 case NO_OVERWRITE:
1950 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1951 __ bind(&skip_allocation);
1952 break;
1953 default: UNREACHABLE();
1954 }
1955 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001956 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001957 CpuFeatures::Scope use_sse2(SSE2);
1958 __ cvtsi2sd(xmm0, Operand(ebx));
1959 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1960 } else {
1961 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1962 __ fild_s(Operand(esp, 1 * kPointerSize));
1963 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1964 }
1965 __ ret(2 * kPointerSize);
1966 }
1967 break;
1968 }
1969 default: UNREACHABLE(); break;
1970 }
1971
1972 // If all else fails, use the runtime system to get the correct
1973 // result.
1974 __ bind(&call_runtime);
1975 switch (op_) {
1976 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001977 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001978 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001979 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1980 break;
1981 }
1982 case Token::SUB:
1983 GenerateRegisterArgsPush(masm);
1984 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1985 break;
1986 case Token::MUL:
1987 GenerateRegisterArgsPush(masm);
1988 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1989 break;
1990 case Token::DIV:
1991 GenerateRegisterArgsPush(masm);
1992 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1993 break;
1994 case Token::MOD:
1995 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1996 break;
1997 case Token::BIT_OR:
1998 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1999 break;
2000 case Token::BIT_AND:
2001 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2002 break;
2003 case Token::BIT_XOR:
2004 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2005 break;
2006 case Token::SAR:
2007 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2008 break;
2009 case Token::SHL:
2010 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2011 break;
2012 case Token::SHR:
2013 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2014 break;
2015 default:
2016 UNREACHABLE();
2017 }
2018}
2019
2020
danno@chromium.org40cb8782011-05-25 07:58:50 +00002021void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002022 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002023 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002024
2025 // Registers containing left and right operands respectively.
2026 Register left = edx;
2027 Register right = eax;
2028
2029 // Test if left operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002030 __ JumpIfSmi(left, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002031 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002032 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002033
2034 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2035 GenerateRegisterArgsPush(masm);
2036 __ TailCallStub(&string_add_left_stub);
2037
2038 // Left operand is not a string, test right.
2039 __ bind(&left_not_string);
whesse@chromium.org7b260152011-06-20 15:33:18 +00002040 __ JumpIfSmi(right, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002041 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002042 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002043
2044 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2045 GenerateRegisterArgsPush(masm);
2046 __ TailCallStub(&string_add_right_stub);
2047
2048 // Neither argument is a string.
2049 __ bind(&call_runtime);
2050}
2051
2052
danno@chromium.org40cb8782011-05-25 07:58:50 +00002053void BinaryOpStub::GenerateHeapResultAllocation(
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002054 MacroAssembler* masm,
2055 Label* alloc_failure) {
2056 Label skip_allocation;
2057 OverwriteMode mode = mode_;
2058 switch (mode) {
2059 case OVERWRITE_LEFT: {
2060 // If the argument in edx is already an object, we skip the
2061 // allocation of a heap number.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002062 __ JumpIfNotSmi(edx, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002063 // Allocate a heap number for the result. Keep eax and edx intact
2064 // for the possible runtime call.
2065 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2066 // Now edx can be overwritten losing one of the arguments as we are
2067 // now done and will not need it any more.
2068 __ mov(edx, Operand(ebx));
2069 __ bind(&skip_allocation);
2070 // Use object in edx as a result holder
2071 __ mov(eax, Operand(edx));
2072 break;
2073 }
2074 case OVERWRITE_RIGHT:
2075 // If the argument in eax is already an object, we skip the
2076 // allocation of a heap number.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002077 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002078 // Fall through!
2079 case NO_OVERWRITE:
2080 // Allocate a heap number for the result. Keep eax and edx intact
2081 // for the possible runtime call.
2082 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2083 // Now eax can be overwritten losing one of the arguments as we are
2084 // now done and will not need it any more.
2085 __ mov(eax, ebx);
2086 __ bind(&skip_allocation);
2087 break;
2088 default: UNREACHABLE();
2089 }
2090}
2091
2092
danno@chromium.org40cb8782011-05-25 07:58:50 +00002093void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002094 __ pop(ecx);
2095 __ push(edx);
2096 __ push(eax);
2097 __ push(ecx);
2098}
2099
2100
ricow@chromium.org65fae842010-08-25 15:26:24 +00002101void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002102 // TAGGED case:
2103 // Input:
2104 // esp[4]: tagged number input argument (should be number).
2105 // esp[0]: return address.
2106 // Output:
2107 // eax: tagged double result.
2108 // UNTAGGED case:
2109 // Input::
2110 // esp[0]: return address.
2111 // xmm1: untagged double input argument
2112 // Output:
2113 // xmm1: untagged double result.
2114
ricow@chromium.org65fae842010-08-25 15:26:24 +00002115 Label runtime_call;
2116 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002117 Label skip_cache;
2118 const bool tagged = (argument_type_ == TAGGED);
2119 if (tagged) {
2120 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002121 Label input_not_smi;
2122 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002123 __ mov(eax, Operand(esp, kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002124 __ JumpIfNotSmi(eax, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002125 // Input is a smi. Untag and load it onto the FPU stack.
2126 // Then load the low and high words of the double into ebx, edx.
2127 STATIC_ASSERT(kSmiTagSize == 1);
2128 __ sar(eax, 1);
2129 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2130 __ mov(Operand(esp, 0), eax);
2131 __ fild_s(Operand(esp, 0));
2132 __ fst_d(Operand(esp, 0));
2133 __ pop(edx);
2134 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002135 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002136 __ bind(&input_not_smi);
2137 // Check if input is a HeapNumber.
2138 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002139 Factory* factory = masm->isolate()->factory();
2140 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002141 __ j(not_equal, &runtime_call);
2142 // Input is a HeapNumber. Push it on the FPU stack and load its
2143 // low and high words into ebx, edx.
2144 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2145 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2146 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002147
whesse@chromium.org023421e2010-12-21 12:19:12 +00002148 __ bind(&loaded);
2149 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002150 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002151 CpuFeatures::Scope sse4_scope(SSE4_1);
2152 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2153 } else {
2154 __ pshufd(xmm0, xmm1, 0x1);
2155 __ movd(Operand(edx), xmm0);
2156 }
2157 __ movd(Operand(ebx), xmm1);
2158 }
2159
2160 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002161 // ebx = low 32 bits of double value
2162 // edx = high 32 bits of double value
2163 // Compute hash (the shifts are arithmetic):
2164 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2165 __ mov(ecx, ebx);
2166 __ xor_(ecx, Operand(edx));
2167 __ mov(eax, ecx);
2168 __ sar(eax, 16);
2169 __ xor_(ecx, Operand(eax));
2170 __ mov(eax, ecx);
2171 __ sar(eax, 8);
2172 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002173 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2174 __ and_(Operand(ecx),
2175 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002176
whesse@chromium.org023421e2010-12-21 12:19:12 +00002177 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002178 // ebx = low 32 bits of double value.
2179 // edx = high 32 bits of double value.
2180 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002181 ExternalReference cache_array =
2182 ExternalReference::transcendental_cache_array_address(masm->isolate());
2183 __ mov(eax, Immediate(cache_array));
2184 int cache_array_index =
2185 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2186 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002187 // Eax points to the cache for the type type_.
2188 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2189 __ test(eax, Operand(eax));
2190 __ j(zero, &runtime_call_clear_stack);
2191#ifdef DEBUG
2192 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002193 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002194 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2195 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2196 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2197 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2198 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2199 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2200 CHECK_EQ(0, elem_in0 - elem_start);
2201 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2202 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2203 }
2204#endif
2205 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2206 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2207 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2208 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002209 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002210 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002211 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002212 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002213 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002214 // Cache hit!
2215 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002216 if (tagged) {
2217 __ fstp(0);
2218 __ ret(kPointerSize);
2219 } else { // UNTAGGED.
2220 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2221 __ Ret();
2222 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002223
2224 __ bind(&cache_miss);
2225 // Update cache with new value.
2226 // We are short on registers, so use no_reg as scratch.
2227 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002228 if (tagged) {
2229 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2230 } else { // UNTAGGED.
2231 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2232 __ sub(Operand(esp), Immediate(kDoubleSize));
2233 __ movdbl(Operand(esp, 0), xmm1);
2234 __ fld_d(Operand(esp, 0));
2235 __ add(Operand(esp), Immediate(kDoubleSize));
2236 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002237 GenerateOperation(masm);
2238 __ mov(Operand(ecx, 0), ebx);
2239 __ mov(Operand(ecx, kIntSize), edx);
2240 __ mov(Operand(ecx, 2 * kIntSize), eax);
2241 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002242 if (tagged) {
2243 __ ret(kPointerSize);
2244 } else { // UNTAGGED.
2245 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2246 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002247
whesse@chromium.org023421e2010-12-21 12:19:12 +00002248 // Skip cache and return answer directly, only in untagged case.
2249 __ bind(&skip_cache);
2250 __ sub(Operand(esp), Immediate(kDoubleSize));
2251 __ movdbl(Operand(esp, 0), xmm1);
2252 __ fld_d(Operand(esp, 0));
2253 GenerateOperation(masm);
2254 __ fstp_d(Operand(esp, 0));
2255 __ movdbl(xmm1, Operand(esp, 0));
2256 __ add(Operand(esp), Immediate(kDoubleSize));
2257 // We return the value in xmm1 without adding it to the cache, but
2258 // we cause a scavenging GC so that future allocations will succeed.
2259 __ EnterInternalFrame();
2260 // Allocate an unused object bigger than a HeapNumber.
2261 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2262 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2263 __ LeaveInternalFrame();
2264 __ Ret();
2265 }
2266
2267 // Call runtime, doing whatever allocation and cleanup is necessary.
2268 if (tagged) {
2269 __ bind(&runtime_call_clear_stack);
2270 __ fstp(0);
2271 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002272 ExternalReference runtime =
2273 ExternalReference(RuntimeFunction(), masm->isolate());
2274 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002275 } else { // UNTAGGED.
2276 __ bind(&runtime_call_clear_stack);
2277 __ bind(&runtime_call);
2278 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2279 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2280 __ EnterInternalFrame();
2281 __ push(eax);
2282 __ CallRuntime(RuntimeFunction(), 1);
2283 __ LeaveInternalFrame();
2284 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2285 __ Ret();
2286 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002287}
2288
2289
2290Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2291 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002292 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2293 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002294 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002295 default:
2296 UNIMPLEMENTED();
2297 return Runtime::kAbort;
2298 }
2299}
2300
2301
2302void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2303 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002304 // Input value is on FP stack, and also in ebx/edx.
2305 // Input value is possibly in xmm1.
2306 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002307 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2308 // Both fsin and fcos require arguments in the range +/-2^63 and
2309 // return NaN for infinities and NaN. They can share all code except
2310 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002311 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002312 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2313 // work. We must reduce it to the appropriate range.
2314 __ mov(edi, edx);
2315 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2316 int supported_exponent_limit =
2317 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2318 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002319 __ j(below, &in_range, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002320 // Check for infinity and NaN. Both return NaN for sin.
2321 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002322 Label non_nan_result;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002323 __ j(not_equal, &non_nan_result, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002324 // Input is +/-Infinity or NaN. Result is NaN.
2325 __ fstp(0);
2326 // NaN is represented by 0x7ff8000000000000.
2327 __ push(Immediate(0x7ff80000));
2328 __ push(Immediate(0));
2329 __ fld_d(Operand(esp, 0));
2330 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002331 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002332
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002333 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002334
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002335 // Use fpmod to restrict argument to the range +/-2*PI.
2336 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2337 __ fldpi();
2338 __ fadd(0);
2339 __ fld(1);
2340 // FPU Stack: input, 2*pi, input.
2341 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002342 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002343 __ fwait();
2344 __ fnstsw_ax();
2345 // Clear if Illegal Operand or Zero Division exceptions are set.
2346 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002347 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002348 __ fnclex();
2349 __ bind(&no_exceptions);
2350 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002351
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002352 // Compute st(0) % st(1)
2353 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002354 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002355 __ bind(&partial_remainder_loop);
2356 __ fprem1();
2357 __ fwait();
2358 __ fnstsw_ax();
2359 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2360 // If C2 is set, computation only has partial result. Loop to
2361 // continue computation.
2362 __ j(not_zero, &partial_remainder_loop);
2363 }
2364 // FPU Stack: input, 2*pi, input % 2*pi
2365 __ fstp(2);
2366 __ fstp(0);
2367 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2368
2369 // FPU Stack: input % 2*pi
2370 __ bind(&in_range);
2371 switch (type_) {
2372 case TranscendentalCache::SIN:
2373 __ fsin();
2374 break;
2375 case TranscendentalCache::COS:
2376 __ fcos();
2377 break;
2378 default:
2379 UNREACHABLE();
2380 }
2381 __ bind(&done);
2382 } else {
2383 ASSERT(type_ == TranscendentalCache::LOG);
2384 __ fldln2();
2385 __ fxch();
2386 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002387 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002388}
2389
2390
ricow@chromium.org65fae842010-08-25 15:26:24 +00002391// Input: edx, eax are the left and right objects of a bit op.
2392// Output: eax, ecx are left and right integers for a bit op.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002393void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2394 bool use_sse3,
2395 Label* conversion_failure) {
2396 // Check float operands.
2397 Label arg1_is_object, check_undefined_arg1;
2398 Label arg2_is_object, check_undefined_arg2;
2399 Label load_arg2, done;
2400
2401 // Test if arg1 is a Smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002402 __ JumpIfNotSmi(edx, &arg1_is_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002403
2404 __ SmiUntag(edx);
2405 __ jmp(&load_arg2);
2406
2407 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2408 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002409 Factory* factory = masm->isolate()->factory();
2410 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002411 __ j(not_equal, conversion_failure);
2412 __ mov(edx, Immediate(0));
2413 __ jmp(&load_arg2);
2414
2415 __ bind(&arg1_is_object);
2416 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002417 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002418 __ j(not_equal, &check_undefined_arg1);
2419
2420 // Get the untagged integer version of the edx heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002421 IntegerConvert(masm, edx, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002422 __ mov(edx, ecx);
2423
2424 // Here edx has the untagged integer, eax has a Smi or a heap number.
2425 __ bind(&load_arg2);
2426
2427 // Test if arg2 is a Smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002428 __ JumpIfNotSmi(eax, &arg2_is_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002429
2430 __ SmiUntag(eax);
2431 __ mov(ecx, eax);
2432 __ jmp(&done);
2433
2434 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2435 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002436 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002437 __ j(not_equal, conversion_failure);
2438 __ mov(ecx, Immediate(0));
2439 __ jmp(&done);
2440
2441 __ bind(&arg2_is_object);
2442 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002443 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002444 __ j(not_equal, &check_undefined_arg2);
2445
2446 // Get the untagged integer version of the eax heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002447 IntegerConvert(masm, eax, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002448 __ bind(&done);
2449 __ mov(eax, edx);
2450}
2451
2452
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002453void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2454 bool use_sse3,
2455 Label* not_int32) {
2456 return;
2457}
2458
2459
ricow@chromium.org65fae842010-08-25 15:26:24 +00002460void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2461 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002462 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002463
whesse@chromium.org7b260152011-06-20 15:33:18 +00002464 __ JumpIfSmi(number, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002465 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002466 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002467
2468 __ bind(&load_smi);
2469 __ SmiUntag(number);
2470 __ push(number);
2471 __ fild_s(Operand(esp, 0));
2472 __ pop(number);
2473
2474 __ bind(&done);
2475}
2476
2477
2478void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002479 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002480 // Load operand in edx into xmm0.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002481 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002482 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2483
2484 __ bind(&load_eax);
2485 // Load operand in eax into xmm1.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002486 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002487 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002488 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002489
2490 __ bind(&load_smi_edx);
2491 __ SmiUntag(edx); // Untag smi before converting to float.
2492 __ cvtsi2sd(xmm0, Operand(edx));
2493 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2494 __ jmp(&load_eax);
2495
2496 __ bind(&load_smi_eax);
2497 __ SmiUntag(eax); // Untag smi before converting to float.
2498 __ cvtsi2sd(xmm1, Operand(eax));
2499 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2500
2501 __ bind(&done);
2502}
2503
2504
2505void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2506 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002507 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002508 // Load operand in edx into xmm0, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002509 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002510 Factory* factory = masm->isolate()->factory();
2511 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002512 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2513 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2514 __ bind(&load_eax);
2515 // Load operand in eax into xmm1, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002516 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002517 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002518 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002519 __ jmp(not_numbers); // Argument in eax is not a number.
2520 __ bind(&load_smi_edx);
2521 __ SmiUntag(edx); // Untag smi before converting to float.
2522 __ cvtsi2sd(xmm0, Operand(edx));
2523 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2524 __ jmp(&load_eax);
2525 __ bind(&load_smi_eax);
2526 __ SmiUntag(eax); // Untag smi before converting to float.
2527 __ cvtsi2sd(xmm1, Operand(eax));
2528 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002529 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002530 __ bind(&load_float_eax);
2531 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2532 __ bind(&done);
2533}
2534
2535
2536void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2537 Register scratch) {
2538 const Register left = edx;
2539 const Register right = eax;
2540 __ mov(scratch, left);
2541 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2542 __ SmiUntag(scratch);
2543 __ cvtsi2sd(xmm0, Operand(scratch));
2544
2545 __ mov(scratch, right);
2546 __ SmiUntag(scratch);
2547 __ cvtsi2sd(xmm1, Operand(scratch));
2548}
2549
2550
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002551void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2552 Label* non_int32,
2553 Register scratch) {
2554 __ cvttsd2si(scratch, Operand(xmm0));
2555 __ cvtsi2sd(xmm2, Operand(scratch));
2556 __ ucomisd(xmm0, xmm2);
2557 __ j(not_zero, non_int32);
2558 __ j(carry, non_int32);
2559 __ cvttsd2si(scratch, Operand(xmm1));
2560 __ cvtsi2sd(xmm2, Operand(scratch));
2561 __ ucomisd(xmm1, xmm2);
2562 __ j(not_zero, non_int32);
2563 __ j(carry, non_int32);
2564}
2565
2566
ricow@chromium.org65fae842010-08-25 15:26:24 +00002567void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2568 Register scratch,
2569 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002570 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002571 if (arg_location == ARGS_IN_REGISTERS) {
2572 __ mov(scratch, edx);
2573 } else {
2574 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2575 }
whesse@chromium.org7b260152011-06-20 15:33:18 +00002576 __ JumpIfSmi(scratch, &load_smi_1, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002577 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2578 __ bind(&done_load_1);
2579
2580 if (arg_location == ARGS_IN_REGISTERS) {
2581 __ mov(scratch, eax);
2582 } else {
2583 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2584 }
whesse@chromium.org7b260152011-06-20 15:33:18 +00002585 __ JumpIfSmi(scratch, &load_smi_2, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002586 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002587 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002588
2589 __ bind(&load_smi_1);
2590 __ SmiUntag(scratch);
2591 __ push(scratch);
2592 __ fild_s(Operand(esp, 0));
2593 __ pop(scratch);
2594 __ jmp(&done_load_1);
2595
2596 __ bind(&load_smi_2);
2597 __ SmiUntag(scratch);
2598 __ push(scratch);
2599 __ fild_s(Operand(esp, 0));
2600 __ pop(scratch);
2601
2602 __ bind(&done);
2603}
2604
2605
2606void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2607 Register scratch) {
2608 const Register left = edx;
2609 const Register right = eax;
2610 __ mov(scratch, left);
2611 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2612 __ SmiUntag(scratch);
2613 __ push(scratch);
2614 __ fild_s(Operand(esp, 0));
2615
2616 __ mov(scratch, right);
2617 __ SmiUntag(scratch);
2618 __ mov(Operand(esp, 0), scratch);
2619 __ fild_s(Operand(esp, 0));
2620 __ pop(scratch);
2621}
2622
2623
2624void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2625 Label* non_float,
2626 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002627 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002628 // Test if both operands are floats or smi -> scratch=k_is_float;
2629 // Otherwise scratch = k_not_float.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002630 __ JumpIfSmi(edx, &test_other, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002631 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002632 Factory* factory = masm->isolate()->factory();
2633 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002634 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2635
2636 __ bind(&test_other);
whesse@chromium.org7b260152011-06-20 15:33:18 +00002637 __ JumpIfSmi(eax, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002638 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002639 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002640 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2641
2642 // Fall-through: Both operands are numbers.
2643 __ bind(&done);
2644}
2645
2646
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002647void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2648 Label* non_int32) {
2649 return;
2650}
2651
2652
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002653void MathPowStub::Generate(MacroAssembler* masm) {
2654 // Registers are used as follows:
2655 // edx = base
2656 // eax = exponent
2657 // ecx = temporary, result
2658
2659 CpuFeatures::Scope use_sse2(SSE2);
2660 Label allocate_return, call_runtime;
2661
2662 // Load input parameters.
2663 __ mov(edx, Operand(esp, 2 * kPointerSize));
2664 __ mov(eax, Operand(esp, 1 * kPointerSize));
2665
2666 // Save 1 in xmm3 - we need this several times later on.
2667 __ mov(ecx, Immediate(1));
2668 __ cvtsi2sd(xmm3, Operand(ecx));
2669
2670 Label exponent_nonsmi;
2671 Label base_nonsmi;
2672 // If the exponent is a heap number go to that specific case.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002673 __ JumpIfNotSmi(eax, &exponent_nonsmi);
2674 __ JumpIfNotSmi(edx, &base_nonsmi);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002675
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002676 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002677 Label powi;
2678 __ SmiUntag(edx);
2679 __ cvtsi2sd(xmm0, Operand(edx));
2680 __ jmp(&powi);
2681 // exponent is smi and base is a heapnumber.
2682 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002683 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002684 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002685 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002686 __ j(not_equal, &call_runtime);
2687
2688 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2689
2690 // Optimized version of pow if exponent is a smi.
2691 // xmm0 contains the base.
2692 __ bind(&powi);
2693 __ SmiUntag(eax);
2694
2695 // Save exponent in base as we need to check if exponent is negative later.
2696 // We know that base and exponent are in different registers.
2697 __ mov(edx, eax);
2698
2699 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002700 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002701 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002702 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002703 __ neg(eax);
2704 __ bind(&no_neg);
2705
2706 // Load xmm1 with 1.
2707 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002708 Label while_true;
2709 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002710
2711 __ bind(&while_true);
2712 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002713 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002714 __ mulsd(xmm1, xmm0);
2715 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002716 __ mulsd(xmm0, xmm0);
2717 __ j(not_zero, &while_true);
2718
2719 // base has the original value of the exponent - if the exponent is
2720 // negative return 1/result.
2721 __ test(edx, Operand(edx));
2722 __ j(positive, &allocate_return);
2723 // Special case if xmm1 has reached infinity.
2724 __ mov(ecx, Immediate(0x7FB00000));
2725 __ movd(xmm0, Operand(ecx));
2726 __ cvtss2sd(xmm0, xmm0);
2727 __ ucomisd(xmm0, xmm1);
2728 __ j(equal, &call_runtime);
2729 __ divsd(xmm3, xmm1);
2730 __ movsd(xmm1, xmm3);
2731 __ jmp(&allocate_return);
2732
2733 // exponent (or both) is a heapnumber - no matter what we should now work
2734 // on doubles.
2735 __ bind(&exponent_nonsmi);
2736 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002737 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002738 __ j(not_equal, &call_runtime);
2739 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2740 // Test if exponent is nan.
2741 __ ucomisd(xmm1, xmm1);
2742 __ j(parity_even, &call_runtime);
2743
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002744 Label base_not_smi;
2745 Label handle_special_cases;
whesse@chromium.org7b260152011-06-20 15:33:18 +00002746 __ JumpIfNotSmi(edx, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002747 __ SmiUntag(edx);
2748 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002749 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002750
2751 __ bind(&base_not_smi);
2752 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002753 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002754 __ j(not_equal, &call_runtime);
2755 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2756 __ and_(ecx, HeapNumber::kExponentMask);
2757 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2758 // base is NaN or +/-Infinity
2759 __ j(greater_equal, &call_runtime);
2760 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2761
2762 // base is in xmm0 and exponent is in xmm1.
2763 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002764 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002765 // Test for -0.5.
2766 // Load xmm2 with -0.5.
2767 __ mov(ecx, Immediate(0xBF000000));
2768 __ movd(xmm2, Operand(ecx));
2769 __ cvtss2sd(xmm2, xmm2);
2770 // xmm2 now has -0.5.
2771 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002772 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002773
2774 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002775 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002776 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002777 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002778 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002779 __ divsd(xmm3, xmm1);
2780 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002781 __ jmp(&allocate_return);
2782
2783 // Test for 0.5.
2784 __ bind(&not_minus_half);
2785 // Load xmm2 with 0.5.
2786 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2787 __ addsd(xmm2, xmm3);
2788 // xmm2 now has 0.5.
2789 __ ucomisd(xmm2, xmm1);
2790 __ j(not_equal, &call_runtime);
2791 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002792 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002793 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002794 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002795 __ sqrtsd(xmm1, xmm1);
2796
2797 __ bind(&allocate_return);
2798 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2799 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2800 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002801 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002802
2803 __ bind(&call_runtime);
2804 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2805}
2806
2807
ricow@chromium.org65fae842010-08-25 15:26:24 +00002808void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2809 // The key is in edx and the parameter count is in eax.
2810
2811 // The displacement is used for skipping the frame pointer on the
2812 // stack. It is the offset of the last parameter (if any) relative
2813 // to the frame pointer.
2814 static const int kDisplacement = 1 * kPointerSize;
2815
2816 // Check that the key is a smi.
2817 Label slow;
whesse@chromium.org7b260152011-06-20 15:33:18 +00002818 __ JumpIfNotSmi(edx, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002819
2820 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002821 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002822 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2823 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2824 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002825 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002826
2827 // Check index against formal parameters count limit passed in
2828 // through register eax. Use unsigned comparison to get negative
2829 // check for free.
2830 __ cmp(edx, Operand(eax));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002831 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002832
2833 // Read the argument from the stack and return it.
2834 STATIC_ASSERT(kSmiTagSize == 1);
2835 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2836 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2837 __ neg(edx);
2838 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2839 __ ret(0);
2840
2841 // Arguments adaptor case: Check index against actual arguments
2842 // limit found in the arguments adaptor frame. Use unsigned
2843 // comparison to get negative check for free.
2844 __ bind(&adaptor);
2845 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2846 __ cmp(edx, Operand(ecx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002847 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002848
2849 // Read the argument from the stack and return it.
2850 STATIC_ASSERT(kSmiTagSize == 1);
2851 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2852 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2853 __ neg(edx);
2854 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2855 __ ret(0);
2856
2857 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2858 // by calling the runtime system.
2859 __ bind(&slow);
2860 __ pop(ebx); // Return address.
2861 __ push(edx);
2862 __ push(ebx);
2863 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2864}
2865
2866
whesse@chromium.org7b260152011-06-20 15:33:18 +00002867void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002868 // esp[0] : return address
2869 // esp[4] : number of parameters
2870 // esp[8] : receiver displacement
whesse@chromium.org7b260152011-06-20 15:33:18 +00002871 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00002872
whesse@chromium.org7b260152011-06-20 15:33:18 +00002873 // Check if the calling frame is an arguments adaptor frame.
2874 Label runtime;
2875 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2876 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2877 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2878 __ j(not_equal, &runtime, Label::kNear);
2879
2880 // Patch the arguments.length and the parameters pointer.
2881 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2882 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2883 __ lea(edx, Operand(edx, ecx, times_2,
2884 StandardFrameConstants::kCallerSPOffset));
2885 __ mov(Operand(esp, 2 * kPointerSize), edx);
2886
2887 __ bind(&runtime);
2888 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2889}
2890
2891
2892void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
2893 // esp[0] : return address
2894 // esp[4] : number of parameters (tagged)
2895 // esp[8] : receiver displacement
2896 // esp[12] : function
2897
2898 // ebx = parameter count (tagged)
2899 __ mov(ebx, Operand(esp, 1 * kPointerSize));
2900
2901 // Check if the calling frame is an arguments adaptor frame.
2902 // TODO(rossberg): Factor out some of the bits that are shared with the other
2903 // Generate* functions.
2904 Label runtime;
2905 Label adaptor_frame, try_allocate;
2906 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2907 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2908 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2909 __ j(equal, &adaptor_frame, Label::kNear);
2910
2911 // No adaptor, parameter count = argument count.
2912 __ mov(ecx, ebx);
2913 __ jmp(&try_allocate, Label::kNear);
2914
2915 // We have an adaptor frame. Patch the parameters pointer.
2916 __ bind(&adaptor_frame);
2917 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2918 __ lea(edx, Operand(edx, ecx, times_2,
2919 StandardFrameConstants::kCallerSPOffset));
2920 __ mov(Operand(esp, 2 * kPointerSize), edx);
2921
2922 // ebx = parameter count (tagged)
2923 // ecx = argument count (tagged)
2924 // esp[4] = parameter count (tagged)
2925 // esp[8] = address of receiver argument
2926 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
2927 __ cmp(ebx, Operand(ecx));
2928 __ j(less_equal, &try_allocate, Label::kNear);
2929 __ mov(ebx, ecx);
2930
2931 __ bind(&try_allocate);
2932
2933 // Save mapped parameter count.
2934 __ push(ebx);
2935
2936 // Compute the sizes of backing store, parameter map, and arguments object.
2937 // 1. Parameter map, has 2 extra words containing context and backing store.
2938 const int kParameterMapHeaderSize =
2939 FixedArray::kHeaderSize + 2 * kPointerSize;
2940 Label no_parameter_map;
2941 __ test(ebx, Operand(ebx));
2942 __ j(zero, &no_parameter_map, Label::kNear);
2943 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
2944 __ bind(&no_parameter_map);
2945
2946 // 2. Backing store.
2947 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
2948
2949 // 3. Arguments object.
2950 __ add(Operand(ebx), Immediate(Heap::kArgumentsObjectSize));
2951
2952 // Do the allocation of all three objects in one go.
2953 __ AllocateInNewSpace(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
2954
2955 // eax = address of new object(s) (tagged)
2956 // ecx = argument count (tagged)
2957 // esp[0] = mapped parameter count (tagged)
2958 // esp[8] = parameter count (tagged)
2959 // esp[12] = address of receiver argument
2960 // Get the arguments boilerplate from the current (global) context into edi.
2961 Label has_mapped_parameters, copy;
2962 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2963 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
2964 __ mov(ebx, Operand(esp, 0 * kPointerSize));
2965 __ test(ebx, Operand(ebx));
2966 __ j(not_zero, &has_mapped_parameters, Label::kNear);
2967 __ mov(edi, Operand(edi,
2968 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX)));
2969 __ jmp(&copy, Label::kNear);
2970
2971 __ bind(&has_mapped_parameters);
2972 __ mov(edi, Operand(edi,
2973 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX)));
2974 __ bind(&copy);
2975
2976 // eax = address of new object (tagged)
2977 // ebx = mapped parameter count (tagged)
2978 // ecx = argument count (tagged)
2979 // edi = address of boilerplate object (tagged)
2980 // esp[0] = mapped parameter count (tagged)
2981 // esp[8] = parameter count (tagged)
2982 // esp[12] = address of receiver argument
2983 // Copy the JS object part.
2984 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2985 __ mov(edx, FieldOperand(edi, i));
2986 __ mov(FieldOperand(eax, i), edx);
2987 }
2988
2989 // Setup the callee in-object property.
2990 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2991 __ mov(edx, Operand(esp, 4 * kPointerSize));
2992 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2993 Heap::kArgumentsCalleeIndex * kPointerSize),
2994 edx);
2995
2996 // Use the length (smi tagged) and set that as an in-object property too.
2997 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2998 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2999 Heap::kArgumentsLengthIndex * kPointerSize),
3000 ecx);
3001
3002 // Setup the elements pointer in the allocated arguments object.
3003 // If we allocated a parameter map, edi will point there, otherwise to the
3004 // backing store.
3005 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
3006 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3007
3008 // eax = address of new object (tagged)
3009 // ebx = mapped parameter count (tagged)
3010 // ecx = argument count (tagged)
3011 // edi = address of parameter map or backing store (tagged)
3012 // esp[0] = mapped parameter count (tagged)
3013 // esp[8] = parameter count (tagged)
3014 // esp[12] = address of receiver argument
3015 // Free a register.
3016 __ push(eax);
3017
3018 // Initialize parameter map. If there are no mapped arguments, we're done.
3019 Label skip_parameter_map;
3020 __ test(ebx, Operand(ebx));
3021 __ j(zero, &skip_parameter_map);
3022
3023 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3024 Immediate(FACTORY->non_strict_arguments_elements_map()));
3025 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
3026 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
3027 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
3028 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
3029 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
3030
3031 // Copy the parameter slots and the holes in the arguments.
3032 // We need to fill in mapped_parameter_count slots. They index the context,
3033 // where parameters are stored in reverse order, at
3034 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
3035 // The mapped parameter thus need to get indices
3036 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
3037 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
3038 // We loop from right to left.
3039 Label parameters_loop, parameters_test;
3040 __ push(ecx);
3041 __ mov(eax, Operand(esp, 2 * kPointerSize));
3042 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
3043 __ add(ebx, Operand(esp, 4 * kPointerSize));
3044 __ sub(ebx, Operand(eax));
3045 __ mov(ecx, FACTORY->the_hole_value());
3046 __ mov(edx, edi);
3047 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
3048 // eax = loop variable (tagged)
3049 // ebx = mapping index (tagged)
3050 // ecx = the hole value
3051 // edx = address of parameter map (tagged)
3052 // edi = address of backing store (tagged)
3053 // esp[0] = argument count (tagged)
3054 // esp[4] = address of new object (tagged)
3055 // esp[8] = mapped parameter count (tagged)
3056 // esp[16] = parameter count (tagged)
3057 // esp[20] = address of receiver argument
3058 __ jmp(&parameters_test, Label::kNear);
3059
3060 __ bind(&parameters_loop);
3061 __ sub(Operand(eax), Immediate(Smi::FromInt(1)));
3062 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
3063 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
3064 __ add(Operand(ebx), Immediate(Smi::FromInt(1)));
3065 __ bind(&parameters_test);
3066 __ test(eax, Operand(eax));
3067 __ j(not_zero, &parameters_loop, Label::kNear);
3068 __ pop(ecx);
3069
3070 __ bind(&skip_parameter_map);
3071
3072 // ecx = argument count (tagged)
3073 // edi = address of backing store (tagged)
3074 // esp[0] = address of new object (tagged)
3075 // esp[4] = mapped parameter count (tagged)
3076 // esp[12] = parameter count (tagged)
3077 // esp[16] = address of receiver argument
3078 // Copy arguments header and remaining slots (if there are any).
3079 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3080 Immediate(FACTORY->fixed_array_map()));
3081 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3082
3083 Label arguments_loop, arguments_test;
3084 __ mov(ebx, Operand(esp, 1 * kPointerSize));
3085 __ mov(edx, Operand(esp, 4 * kPointerSize));
3086 __ sub(Operand(edx), ebx); // Is there a smarter way to do negative scaling?
3087 __ sub(Operand(edx), ebx);
3088 __ jmp(&arguments_test, Label::kNear);
3089
3090 __ bind(&arguments_loop);
3091 __ sub(Operand(edx), Immediate(kPointerSize));
3092 __ mov(eax, Operand(edx, 0));
3093 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
3094 __ add(Operand(ebx), Immediate(Smi::FromInt(1)));
3095
3096 __ bind(&arguments_test);
3097 __ cmp(ebx, Operand(ecx));
3098 __ j(less, &arguments_loop, Label::kNear);
3099
3100 // Restore.
3101 __ pop(eax); // Address of arguments object.
3102 __ pop(ebx); // Parameter count.
3103
3104 // Return and remove the on-stack parameters.
3105 __ ret(3 * kPointerSize);
3106
3107 // Do the runtime call to allocate the arguments object.
3108 __ bind(&runtime);
3109 __ pop(eax); // Remove saved parameter count.
3110 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
3111 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
3112}
3113
3114
3115void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
3116 // esp[0] : return address
3117 // esp[4] : number of parameters
3118 // esp[8] : receiver displacement
3119 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00003120
3121 // Check if the calling frame is an arguments adaptor frame.
3122 Label adaptor_frame, try_allocate, runtime;
3123 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3124 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
3125 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3126 __ j(equal, &adaptor_frame);
3127
3128 // Get the length from the frame.
3129 __ mov(ecx, Operand(esp, 1 * kPointerSize));
3130 __ jmp(&try_allocate);
3131
3132 // Patch the arguments.length and the parameters pointer.
3133 __ bind(&adaptor_frame);
3134 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3135 __ mov(Operand(esp, 1 * kPointerSize), ecx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003136 __ lea(edx, Operand(edx, ecx, times_2,
3137 StandardFrameConstants::kCallerSPOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003138 __ mov(Operand(esp, 2 * kPointerSize), edx);
3139
3140 // Try the new space allocation. Start out with computing the size of
3141 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003142 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003143 __ bind(&try_allocate);
3144 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003145 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003146 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3147 __ bind(&add_arguments_object);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003148 __ add(Operand(ecx), Immediate(Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003149
3150 // Do the allocation of both objects in one go.
3151 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3152
3153 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003154 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3155 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003156 const int offset =
3157 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
3158 __ mov(edi, Operand(edi, offset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003159
3160 // Copy the JS object part.
3161 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3162 __ mov(ebx, FieldOperand(edi, i));
3163 __ mov(FieldOperand(eax, i), ebx);
3164 }
3165
ricow@chromium.org65fae842010-08-25 15:26:24 +00003166 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003167 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003168 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003169 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
whesse@chromium.org7b260152011-06-20 15:33:18 +00003170 Heap::kArgumentsLengthIndex * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003171 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003172
3173 // If there are no actual arguments, we're done.
3174 Label done;
3175 __ test(ecx, Operand(ecx));
3176 __ j(zero, &done);
3177
3178 // Get the parameters pointer from the stack.
3179 __ mov(edx, Operand(esp, 2 * kPointerSize));
3180
3181 // Setup the elements pointer in the allocated arguments object and
3182 // initialize the header in the elements fixed array.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003183 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003184 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3185 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
whesse@chromium.org7b260152011-06-20 15:33:18 +00003186 Immediate(FACTORY->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003187
ricow@chromium.org65fae842010-08-25 15:26:24 +00003188 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3189 // Untag the length for the loop below.
3190 __ SmiUntag(ecx);
3191
3192 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003193 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003194 __ bind(&loop);
3195 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3196 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3197 __ add(Operand(edi), Immediate(kPointerSize));
3198 __ sub(Operand(edx), Immediate(kPointerSize));
3199 __ dec(ecx);
3200 __ j(not_zero, &loop);
3201
3202 // Return and remove the on-stack parameters.
3203 __ bind(&done);
3204 __ ret(3 * kPointerSize);
3205
3206 // Do the runtime call to allocate the arguments object.
3207 __ bind(&runtime);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003208 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003209}
3210
3211
3212void RegExpExecStub::Generate(MacroAssembler* masm) {
3213 // Just jump directly to runtime if native RegExp is not selected at compile
3214 // time or if regexp entry in generated code is turned off runtime switch or
3215 // at compilation.
3216#ifdef V8_INTERPRETED_REGEXP
3217 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3218#else // V8_INTERPRETED_REGEXP
3219 if (!FLAG_regexp_entry_native) {
3220 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3221 return;
3222 }
3223
3224 // Stack frame on entry.
3225 // esp[0]: return address
3226 // esp[4]: last_match_info (expected JSArray)
3227 // esp[8]: previous index
3228 // esp[12]: subject string
3229 // esp[16]: JSRegExp object
3230
3231 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3232 static const int kPreviousIndexOffset = 2 * kPointerSize;
3233 static const int kSubjectOffset = 3 * kPointerSize;
3234 static const int kJSRegExpOffset = 4 * kPointerSize;
3235
3236 Label runtime, invoke_regexp;
3237
3238 // Ensure that a RegExp stack is allocated.
3239 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003240 ExternalReference::address_of_regexp_stack_memory_address(
3241 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003242 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003243 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003244 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3245 __ test(ebx, Operand(ebx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003246 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003247
3248 // Check that the first argument is a JSRegExp object.
3249 __ mov(eax, Operand(esp, kJSRegExpOffset));
3250 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003251 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003252 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3253 __ j(not_equal, &runtime);
3254 // Check that the RegExp has been compiled (data contains a fixed array).
3255 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3256 if (FLAG_debug_code) {
3257 __ test(ecx, Immediate(kSmiTagMask));
3258 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3259 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3260 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3261 }
3262
3263 // ecx: RegExp data (FixedArray)
3264 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3265 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3266 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3267 __ j(not_equal, &runtime);
3268
3269 // ecx: RegExp data (FixedArray)
3270 // Check that the number of captures fit in the static offsets vector buffer.
3271 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3272 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3273 // uses the asumption that smis are 2 * their untagged value.
3274 STATIC_ASSERT(kSmiTag == 0);
3275 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3276 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3277 // Check that the static offsets vector buffer is large enough.
3278 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3279 __ j(above, &runtime);
3280
3281 // ecx: RegExp data (FixedArray)
3282 // edx: Number of capture registers
3283 // Check that the second argument is a string.
3284 __ mov(eax, Operand(esp, kSubjectOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003285 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003286 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3287 __ j(NegateCondition(is_string), &runtime);
3288 // Get the length of the string to ebx.
3289 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3290
3291 // ebx: Length of subject string as a smi
3292 // ecx: RegExp data (FixedArray)
3293 // edx: Number of capture registers
3294 // Check that the third argument is a positive smi less than the subject
3295 // string length. A negative value will be greater (unsigned comparison).
3296 __ mov(eax, Operand(esp, kPreviousIndexOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003297 __ JumpIfNotSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003298 __ cmp(eax, Operand(ebx));
3299 __ j(above_equal, &runtime);
3300
3301 // ecx: RegExp data (FixedArray)
3302 // edx: Number of capture registers
3303 // Check that the fourth object is a JSArray object.
3304 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003305 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003306 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3307 __ j(not_equal, &runtime);
3308 // Check that the JSArray is in fast case.
3309 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3310 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003311 Factory* factory = masm->isolate()->factory();
3312 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003313 __ j(not_equal, &runtime);
3314 // Check that the last match info has space for the capture registers and the
3315 // additional information.
3316 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3317 __ SmiUntag(eax);
3318 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3319 __ cmp(edx, Operand(eax));
3320 __ j(greater, &runtime);
3321
3322 // ecx: RegExp data (FixedArray)
3323 // Check the representation and encoding of the subject string.
3324 Label seq_ascii_string, seq_two_byte_string, check_code;
3325 __ mov(eax, Operand(esp, kSubjectOffset));
3326 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3327 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3328 // First check for flat two byte string.
3329 __ and_(ebx,
3330 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3331 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3332 __ j(zero, &seq_two_byte_string);
3333 // Any other flat string must be a flat ascii string.
3334 __ test(Operand(ebx),
3335 Immediate(kIsNotStringMask | kStringRepresentationMask));
3336 __ j(zero, &seq_ascii_string);
3337
3338 // Check for flat cons string.
3339 // A flat cons string is a cons string where the second part is the empty
3340 // string. In that case the subject string is just the first part of the cons
3341 // string. Also in this case the first part of the cons string is known to be
3342 // a sequential string or an external string.
3343 STATIC_ASSERT(kExternalStringTag != 0);
3344 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3345 __ test(Operand(ebx),
3346 Immediate(kIsNotStringMask | kExternalStringTag));
3347 __ j(not_zero, &runtime);
3348 // String is a cons string.
3349 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003350 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003351 __ j(not_equal, &runtime);
3352 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3353 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3354 // String is a cons string with empty second part.
3355 // eax: first part of cons string.
3356 // ebx: map of first part of cons string.
3357 // Is first part a flat two byte string?
3358 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3359 kStringRepresentationMask | kStringEncodingMask);
3360 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3361 __ j(zero, &seq_two_byte_string);
3362 // Any other flat string must be ascii.
3363 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3364 kStringRepresentationMask);
3365 __ j(not_zero, &runtime);
3366
3367 __ bind(&seq_ascii_string);
3368 // eax: subject string (flat ascii)
3369 // ecx: RegExp data (FixedArray)
3370 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3371 __ Set(edi, Immediate(1)); // Type is ascii.
3372 __ jmp(&check_code);
3373
3374 __ bind(&seq_two_byte_string);
3375 // eax: subject string (flat two byte)
3376 // ecx: RegExp data (FixedArray)
3377 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3378 __ Set(edi, Immediate(0)); // Type is two byte.
3379
3380 __ bind(&check_code);
3381 // Check that the irregexp code has been generated for the actual string
3382 // encoding. If it has, the field contains a code object otherwise it contains
3383 // the hole.
3384 __ CmpObjectType(edx, CODE_TYPE, ebx);
3385 __ j(not_equal, &runtime);
3386
3387 // eax: subject string
3388 // edx: code
3389 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3390 // Load used arguments before starting to push arguments for call to native
3391 // RegExp code to avoid handling changing stack height.
3392 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3393 __ SmiUntag(ebx); // Previous index from smi.
3394
3395 // eax: subject string
3396 // ebx: previous index
3397 // edx: code
3398 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3399 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003400 Counters* counters = masm->isolate()->counters();
3401 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003402
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003403 // Isolates: note we add an additional parameter here (isolate pointer).
3404 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003405 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003406
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003407 // Argument 8: Pass current isolate address.
3408 __ mov(Operand(esp, 7 * kPointerSize),
3409 Immediate(ExternalReference::isolate_address()));
3410
ricow@chromium.org65fae842010-08-25 15:26:24 +00003411 // Argument 7: Indicate that this is a direct call from JavaScript.
3412 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3413
3414 // Argument 6: Start (high end) of backtracking stack memory area.
3415 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3416 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3417 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3418
3419 // Argument 5: static offsets vector buffer.
3420 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003421 Immediate(ExternalReference::address_of_static_offsets_vector(
3422 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003423
3424 // Argument 4: End of string data
3425 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003426 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003427 __ test(edi, Operand(edi));
3428 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003429 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003430 __ SmiUntag(edi);
3431 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3432 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3433 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3434 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003435 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003436
3437 __ bind(&setup_two_byte);
3438 STATIC_ASSERT(kSmiTag == 0);
3439 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3440 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3441 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3442 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3443 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3444
3445 __ bind(&setup_rest);
3446
3447 // Argument 2: Previous index.
3448 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3449
3450 // Argument 1: Subject string.
3451 __ mov(Operand(esp, 0 * kPointerSize), eax);
3452
3453 // Locate the code entry and call it.
3454 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003455 __ call(Operand(edx));
3456
3457 // Drop arguments and come back to JS mode.
3458 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003459
3460 // Check the result.
3461 Label success;
3462 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003463 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003464 Label failure;
3465 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003466 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003467 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3468 // If not exception it can only be retry. Handle that in the runtime system.
3469 __ j(not_equal, &runtime);
3470 // Result must now be exception. If there is no pending exception already a
3471 // stack overflow (on the backtrack stack) was detected in RegExp code but
3472 // haven't created the exception yet. Handle that in the runtime system.
3473 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003474 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3475 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003476 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003477 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3478 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003479 __ mov(eax, Operand::StaticVariable(pending_exception));
3480 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003481 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003482 // For exception, throw the exception again.
3483
3484 // Clear the pending exception variable.
3485 __ mov(Operand::StaticVariable(pending_exception), edx);
3486
3487 // Special handling of termination exceptions which are uncatchable
3488 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003489 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003490 Label throw_termination_exception;
3491 __ j(equal, &throw_termination_exception);
3492
3493 // Handle normal exception by following handler chain.
3494 __ Throw(eax);
3495
3496 __ bind(&throw_termination_exception);
3497 __ ThrowUncatchable(TERMINATION, eax);
3498
ricow@chromium.org65fae842010-08-25 15:26:24 +00003499 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003500 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003501 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003502 __ ret(4 * kPointerSize);
3503
3504 // Load RegExp data.
3505 __ bind(&success);
3506 __ mov(eax, Operand(esp, kJSRegExpOffset));
3507 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3508 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3509 // Calculate number of capture registers (number_of_captures + 1) * 2.
3510 STATIC_ASSERT(kSmiTag == 0);
3511 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3512 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3513
3514 // edx: Number of capture registers
3515 // Load last_match_info which is still known to be a fast case JSArray.
3516 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3517 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3518
3519 // ebx: last_match_info backing store (FixedArray)
3520 // edx: number of capture registers
3521 // Store the capture count.
3522 __ SmiTag(edx); // Number of capture registers to smi.
3523 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3524 __ SmiUntag(edx); // Number of capture registers back from smi.
3525 // Store last subject and last input.
3526 __ mov(eax, Operand(esp, kSubjectOffset));
3527 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3528 __ mov(ecx, ebx);
3529 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3530 __ mov(eax, Operand(esp, kSubjectOffset));
3531 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3532 __ mov(ecx, ebx);
3533 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3534
3535 // Get the static offsets vector filled by the native regexp code.
3536 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003537 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003538 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3539
3540 // ebx: last_match_info backing store (FixedArray)
3541 // ecx: offsets vector
3542 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003543 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003544 // Capture register counter starts from number of capture registers and
3545 // counts down until wraping after zero.
3546 __ bind(&next_capture);
3547 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003548 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003549 // Read the value from the static offsets vector buffer.
3550 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3551 __ SmiTag(edi);
3552 // Store the smi value in the last match info.
3553 __ mov(FieldOperand(ebx,
3554 edx,
3555 times_pointer_size,
3556 RegExpImpl::kFirstCaptureOffset),
3557 edi);
3558 __ jmp(&next_capture);
3559 __ bind(&done);
3560
3561 // Return last match info.
3562 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3563 __ ret(4 * kPointerSize);
3564
3565 // Do the runtime call to execute the regexp.
3566 __ bind(&runtime);
3567 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3568#endif // V8_INTERPRETED_REGEXP
3569}
3570
3571
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003572void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3573 const int kMaxInlineLength = 100;
3574 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003575 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003576 __ mov(ebx, Operand(esp, kPointerSize * 3));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003577 __ JumpIfNotSmi(ebx, &slowcase);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003578 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3579 __ j(above, &slowcase);
3580 // Smi-tagging is equivalent to multiplying by 2.
3581 STATIC_ASSERT(kSmiTag == 0);
3582 STATIC_ASSERT(kSmiTagSize == 1);
3583 // Allocate RegExpResult followed by FixedArray with size in ebx.
3584 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3585 // Elements: [Map][Length][..elements..]
3586 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3587 times_half_pointer_size,
3588 ebx, // In: Number of elements (times 2, being a smi)
3589 eax, // Out: Start of allocation (tagged).
3590 ecx, // Out: End of allocation.
3591 edx, // Scratch register
3592 &slowcase,
3593 TAG_OBJECT);
3594 // eax: Start of allocated area, object-tagged.
3595
3596 // Set JSArray map to global.regexp_result_map().
3597 // Set empty properties FixedArray.
3598 // Set elements to point to FixedArray allocated right after the JSArray.
3599 // Interleave operations for better latency.
3600 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003601 Factory* factory = masm->isolate()->factory();
3602 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003603 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3604 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3605 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3606 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3607 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3608 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3609
3610 // Set input, index and length fields from arguments.
3611 __ mov(ecx, Operand(esp, kPointerSize * 1));
3612 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3613 __ mov(ecx, Operand(esp, kPointerSize * 2));
3614 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3615 __ mov(ecx, Operand(esp, kPointerSize * 3));
3616 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3617
3618 // Fill out the elements FixedArray.
3619 // eax: JSArray.
3620 // ebx: FixedArray.
3621 // ecx: Number of elements in array, as smi.
3622
3623 // Set map.
3624 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003625 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003626 // Set length.
3627 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3628 // Fill contents of fixed-array with the-hole.
3629 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003630 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003631 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3632 // Fill fixed array elements with hole.
3633 // eax: JSArray.
3634 // ecx: Number of elements to fill.
3635 // ebx: Start of elements in FixedArray.
3636 // edx: the hole.
3637 Label loop;
3638 __ test(ecx, Operand(ecx));
3639 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003640 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003641 __ sub(Operand(ecx), Immediate(1));
3642 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3643 __ jmp(&loop);
3644
3645 __ bind(&done);
3646 __ ret(3 * kPointerSize);
3647
3648 __ bind(&slowcase);
3649 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3650}
3651
3652
ricow@chromium.org65fae842010-08-25 15:26:24 +00003653void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3654 Register object,
3655 Register result,
3656 Register scratch1,
3657 Register scratch2,
3658 bool object_is_smi,
3659 Label* not_found) {
3660 // Use of registers. Register result is used as a temporary.
3661 Register number_string_cache = result;
3662 Register mask = scratch1;
3663 Register scratch = scratch2;
3664
3665 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003666 ExternalReference roots_address =
3667 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003668 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3669 __ mov(number_string_cache,
3670 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3671 // Make the hash mask from the length of the number string cache. It
3672 // contains two elements (number and string) for each cache entry.
3673 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3674 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3675 __ sub(Operand(mask), Immediate(1)); // Make mask.
3676
3677 // Calculate the entry in the number string cache. The hash value in the
3678 // number string cache for smis is just the smi value, and the hash for
3679 // doubles is the xor of the upper and lower words. See
3680 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003681 Label smi_hash_calculated;
3682 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003683 if (object_is_smi) {
3684 __ mov(scratch, object);
3685 __ SmiUntag(scratch);
3686 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003687 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003688 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003689 __ JumpIfNotSmi(object, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003690 __ mov(scratch, object);
3691 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003692 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003693 __ bind(&not_smi);
3694 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003695 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003696 __ j(not_equal, not_found);
3697 STATIC_ASSERT(8 == kDoubleSize);
3698 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3699 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3700 // Object is heap number and hash is now in scratch. Calculate cache index.
3701 __ and_(scratch, Operand(mask));
3702 Register index = scratch;
3703 Register probe = mask;
3704 __ mov(probe,
3705 FieldOperand(number_string_cache,
3706 index,
3707 times_twice_pointer_size,
3708 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003709 __ JumpIfSmi(probe, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003710 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003711 CpuFeatures::Scope fscope(SSE2);
3712 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3713 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3714 __ ucomisd(xmm0, xmm1);
3715 } else {
3716 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3717 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3718 __ FCmp();
3719 }
3720 __ j(parity_even, not_found); // Bail out if NaN is involved.
3721 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003722 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003723 }
3724
3725 __ bind(&smi_hash_calculated);
3726 // Object is smi and hash is now in scratch. Calculate cache index.
3727 __ and_(scratch, Operand(mask));
3728 Register index = scratch;
3729 // Check if the entry is the smi we are looking for.
3730 __ cmp(object,
3731 FieldOperand(number_string_cache,
3732 index,
3733 times_twice_pointer_size,
3734 FixedArray::kHeaderSize));
3735 __ j(not_equal, not_found);
3736
3737 // Get the result from the cache.
3738 __ bind(&load_result_from_cache);
3739 __ mov(result,
3740 FieldOperand(number_string_cache,
3741 index,
3742 times_twice_pointer_size,
3743 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003744 Counters* counters = masm->isolate()->counters();
3745 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003746}
3747
3748
3749void NumberToStringStub::Generate(MacroAssembler* masm) {
3750 Label runtime;
3751
3752 __ mov(ebx, Operand(esp, kPointerSize));
3753
3754 // Generate code to lookup number in the number string cache.
3755 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3756 __ ret(1 * kPointerSize);
3757
3758 __ bind(&runtime);
3759 // Handle number to string in the runtime system if not found in the cache.
3760 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3761}
3762
3763
3764static int NegativeComparisonResult(Condition cc) {
3765 ASSERT(cc != equal);
3766 ASSERT((cc == less) || (cc == less_equal)
3767 || (cc == greater) || (cc == greater_equal));
3768 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3769}
3770
3771void CompareStub::Generate(MacroAssembler* masm) {
3772 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3773
3774 Label check_unequal_objects, done;
3775
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003776 // Compare two smis if required.
3777 if (include_smi_compare_) {
3778 Label non_smi, smi_done;
3779 __ mov(ecx, Operand(edx));
3780 __ or_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003781 __ JumpIfNotSmi(ecx, &non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003782 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3783 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003784 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003785 __ bind(&smi_done);
3786 __ mov(eax, edx);
3787 __ ret(0);
3788 __ bind(&non_smi);
3789 } else if (FLAG_debug_code) {
3790 __ mov(ecx, Operand(edx));
3791 __ or_(ecx, Operand(eax));
3792 __ test(ecx, Immediate(kSmiTagMask));
3793 __ Assert(not_zero, "Unexpected smi operands.");
3794 }
3795
ricow@chromium.org65fae842010-08-25 15:26:24 +00003796 // NOTICE! This code is only reached after a smi-fast-case check, so
3797 // it is certain that at least one operand isn't a smi.
3798
3799 // Identical objects can be compared fast, but there are some tricky cases
3800 // for NaN and undefined.
3801 {
3802 Label not_identical;
3803 __ cmp(eax, Operand(edx));
3804 __ j(not_equal, &not_identical);
3805
3806 if (cc_ != equal) {
3807 // Check for undefined. undefined OP undefined is false even though
3808 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003809 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003810 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003811 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003812 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3813 __ ret(0);
3814 __ bind(&check_for_nan);
3815 }
3816
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003817 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003818 // so we do the second best thing - test it ourselves.
3819 // Note: if cc_ != equal, never_nan_nan_ is not used.
3820 if (never_nan_nan_ && (cc_ == equal)) {
3821 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3822 __ ret(0);
3823 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003824 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003825 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003826 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003827 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003828 if (cc_ != equal) {
3829 // Call runtime on identical JSObjects. Otherwise return equal.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003830 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003831 __ j(above_equal, &not_identical);
3832 }
3833 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3834 __ ret(0);
3835
3836 __ bind(&heap_number);
3837 // It is a heap number, so return non-equal if it's NaN and equal if
3838 // it's not NaN.
3839 // The representation of NaN values has all exponent bits (52..62) set,
3840 // and not all mantissa bits (0..51) clear.
3841 // We only accept QNaNs, which have bit 51 set.
3842 // Read top bits of double representation (second word of value).
3843
3844 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3845 // all bits in the mask are set. We only need to check the word
3846 // that contains the exponent and high bit of the mantissa.
3847 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3848 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003849 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003850 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3851 // bits.
3852 __ add(edx, Operand(edx));
3853 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3854 if (cc_ == equal) {
3855 STATIC_ASSERT(EQUAL != 1);
3856 __ setcc(above_equal, eax);
3857 __ ret(0);
3858 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003859 Label nan;
3860 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003861 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3862 __ ret(0);
3863 __ bind(&nan);
3864 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3865 __ ret(0);
3866 }
3867 }
3868
3869 __ bind(&not_identical);
3870 }
3871
3872 // Strict equality can quickly decide whether objects are equal.
3873 // Non-strict object equality is slower, so it is handled later in the stub.
3874 if (cc_ == equal && strict_) {
3875 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003876 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003877 // If we're doing a strict equality comparison, we don't have to do
3878 // type conversion, so we generate code to do fast comparison for objects
3879 // and oddballs. Non-smi numbers and strings still go through the usual
3880 // slow-case code.
3881 // If either is a Smi (we know that not both are), then they can only
3882 // be equal if the other is a HeapNumber. If so, use the slow case.
3883 STATIC_ASSERT(kSmiTag == 0);
3884 ASSERT_EQ(0, Smi::FromInt(0));
3885 __ mov(ecx, Immediate(kSmiTagMask));
3886 __ and_(ecx, Operand(eax));
3887 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003888 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003889 // One operand is a smi.
3890
3891 // Check whether the non-smi is a heap number.
3892 STATIC_ASSERT(kSmiTagMask == 1);
3893 // ecx still holds eax & kSmiTag, which is either zero or one.
3894 __ sub(Operand(ecx), Immediate(0x01));
3895 __ mov(ebx, edx);
3896 __ xor_(ebx, Operand(eax));
3897 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3898 __ xor_(ebx, Operand(eax));
3899 // if eax was smi, ebx is now edx, else eax.
3900
3901 // Check if the non-smi operand is a heap number.
3902 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003903 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003904 // If heap number, handle it in the slow case.
3905 __ j(equal, &slow);
3906 // Return non-equal (ebx is not zero)
3907 __ mov(eax, ebx);
3908 __ ret(0);
3909
3910 __ bind(&not_smis);
3911 // If either operand is a JSObject or an oddball value, then they are not
3912 // equal since their pointers are different
3913 // There is no test for undetectability in strict equality.
3914
3915 // Get the type of the first operand.
3916 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003917 Label first_non_object;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003918 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
3919 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003920 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003921
3922 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003923 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003924 STATIC_ASSERT(kHeapObjectTag != 0);
3925 __ bind(&return_not_equal);
3926 __ ret(0);
3927
3928 __ bind(&first_non_object);
3929 // Check for oddballs: true, false, null, undefined.
3930 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3931 __ j(equal, &return_not_equal);
3932
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003933 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003934 __ j(above_equal, &return_not_equal);
3935
3936 // Check for oddballs: true, false, null, undefined.
3937 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3938 __ j(equal, &return_not_equal);
3939
3940 // Fall through to the general case.
3941 __ bind(&slow);
3942 }
3943
3944 // Generate the number comparison code.
3945 if (include_number_compare_) {
3946 Label non_number_comparison;
3947 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003948 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003949 CpuFeatures::Scope use_sse2(SSE2);
3950 CpuFeatures::Scope use_cmov(CMOV);
3951
3952 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3953 __ ucomisd(xmm0, xmm1);
3954
3955 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003956 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003957 // Return a result of -1, 0, or 1, based on EFLAGS.
3958 __ mov(eax, 0); // equal
3959 __ mov(ecx, Immediate(Smi::FromInt(1)));
3960 __ cmov(above, eax, Operand(ecx));
3961 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3962 __ cmov(below, eax, Operand(ecx));
3963 __ ret(0);
3964 } else {
3965 FloatingPointHelper::CheckFloatOperands(
3966 masm, &non_number_comparison, ebx);
3967 FloatingPointHelper::LoadFloatOperand(masm, eax);
3968 FloatingPointHelper::LoadFloatOperand(masm, edx);
3969 __ FCmp();
3970
3971 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003972 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003973
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003974 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003975 // Return a result of -1, 0, or 1, based on EFLAGS.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003976 __ j(below, &below_label);
3977 __ j(above, &above_label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003978
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003979 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003980 __ ret(0);
3981
3982 __ bind(&below_label);
3983 __ mov(eax, Immediate(Smi::FromInt(-1)));
3984 __ ret(0);
3985
3986 __ bind(&above_label);
3987 __ mov(eax, Immediate(Smi::FromInt(1)));
3988 __ ret(0);
3989 }
3990
3991 // If one of the numbers was NaN, then the result is always false.
3992 // The cc is never not-equal.
3993 __ bind(&unordered);
3994 ASSERT(cc_ != not_equal);
3995 if (cc_ == less || cc_ == less_equal) {
3996 __ mov(eax, Immediate(Smi::FromInt(1)));
3997 } else {
3998 __ mov(eax, Immediate(Smi::FromInt(-1)));
3999 }
4000 __ ret(0);
4001
4002 // The number comparison code did not provide a valid result.
4003 __ bind(&non_number_comparison);
4004 }
4005
4006 // Fast negative check for symbol-to-symbol equality.
4007 Label check_for_strings;
4008 if (cc_ == equal) {
4009 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4010 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4011
4012 // We've already checked for object identity, so if both operands
4013 // are symbols they aren't equal. Register eax already holds a
4014 // non-zero value, which indicates not equal, so just return.
4015 __ ret(0);
4016 }
4017
4018 __ bind(&check_for_strings);
4019
4020 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4021 &check_unequal_objects);
4022
4023 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004024 if (cc_ == equal) {
4025 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00004026 edx,
4027 eax,
4028 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00004029 ebx);
4030 } else {
4031 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4032 edx,
4033 eax,
4034 ecx,
4035 ebx,
4036 edi);
4037 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004038#ifdef DEBUG
4039 __ Abort("Unexpected fall-through from string comparison");
4040#endif
4041
4042 __ bind(&check_unequal_objects);
4043 if (cc_ == equal && !strict_) {
4044 // Non-strict equality. Objects are unequal if
4045 // they are both JSObjects and not undetectable,
4046 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004047 Label not_both_objects;
4048 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004049 // At most one is a smi, so we can test for smi by adding the two.
4050 // A smi plus a heap object has the low bit set, a heap object plus
4051 // a heap object has the low bit clear.
4052 STATIC_ASSERT(kSmiTag == 0);
4053 STATIC_ASSERT(kSmiTagMask == 1);
4054 __ lea(ecx, Operand(eax, edx, times_1, 0));
4055 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004056 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004057 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004058 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004059 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004060 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004061 // We do not bail out after this point. Both are JSObjects, and
4062 // they are equal if and only if both are undetectable.
4063 // The and of the undetectable flags is 1 if and only if they are equal.
4064 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4065 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004066 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004067 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4068 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004069 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004070 // The objects are both undetectable, so they both compare as the value
4071 // undefined, and are equal.
4072 __ Set(eax, Immediate(EQUAL));
4073 __ bind(&return_unequal);
4074 // Return non-equal by returning the non-zero object pointer in eax,
4075 // or return equal if we fell through to here.
4076 __ ret(0); // rax, rdx were pushed
4077 __ bind(&not_both_objects);
4078 }
4079
4080 // Push arguments below the return address.
4081 __ pop(ecx);
4082 __ push(edx);
4083 __ push(eax);
4084
4085 // Figure out which native to call and setup the arguments.
4086 Builtins::JavaScript builtin;
4087 if (cc_ == equal) {
4088 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4089 } else {
4090 builtin = Builtins::COMPARE;
4091 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4092 }
4093
4094 // Restore return address on the stack.
4095 __ push(ecx);
4096
4097 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4098 // tagged as a small integer.
4099 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4100}
4101
4102
4103void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4104 Label* label,
4105 Register object,
4106 Register scratch) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004107 __ JumpIfSmi(object, label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004108 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4109 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4110 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4111 __ cmp(scratch, kSymbolTag | kStringTag);
4112 __ j(not_equal, label);
4113}
4114
4115
4116void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004117 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004118}
4119
4120
4121void CallFunctionStub::Generate(MacroAssembler* masm) {
4122 Label slow;
4123
danno@chromium.org40cb8782011-05-25 07:58:50 +00004124 // The receiver might implicitly be the global object. This is
4125 // indicated by passing the hole as the receiver to the call
4126 // function stub.
4127 if (ReceiverMightBeImplicit()) {
4128 Label call;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004129 // Get the receiver from the stack.
4130 // +1 ~ return address
ricow@chromium.org65fae842010-08-25 15:26:24 +00004131 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004132 // Call as function is indicated with the hole.
4133 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
4134 __ j(not_equal, &call, Label::kNear);
4135 // Patch the receiver on the stack with the global receiver object.
4136 __ mov(ebx, GlobalObjectOperand());
4137 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
4138 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), ebx);
4139 __ bind(&call);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004140 }
4141
4142 // Get the function to call from the stack.
4143 // +2 ~ receiver, return address
4144 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4145
4146 // Check that the function really is a JavaScript function.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004147 __ JumpIfSmi(edi, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004148 // Goto slow case if we do not have a function.
4149 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004150 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004151
4152 // Fast-case: Just invoke the function.
4153 ParameterCount actual(argc_);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004154
4155 if (ReceiverMightBeImplicit()) {
4156 Label call_as_function;
4157 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
4158 __ j(equal, &call_as_function);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004159 __ InvokeFunction(edi,
4160 actual,
4161 JUMP_FUNCTION,
4162 NullCallWrapper(),
4163 CALL_AS_METHOD);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004164 __ bind(&call_as_function);
4165 }
4166 __ InvokeFunction(edi,
4167 actual,
4168 JUMP_FUNCTION,
4169 NullCallWrapper(),
4170 CALL_AS_FUNCTION);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004171
4172 // Slow-case: Non-function called.
4173 __ bind(&slow);
4174 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4175 // of the original receiver from the call site).
4176 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4177 __ Set(eax, Immediate(argc_));
4178 __ Set(ebx, Immediate(0));
4179 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004180 Handle<Code> adaptor =
4181 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004182 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4183}
4184
4185
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004186bool CEntryStub::NeedsImmovableCode() {
4187 return false;
4188}
4189
4190
ricow@chromium.org65fae842010-08-25 15:26:24 +00004191void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004192 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004193}
4194
4195
ricow@chromium.org65fae842010-08-25 15:26:24 +00004196void CEntryStub::GenerateCore(MacroAssembler* masm,
4197 Label* throw_normal_exception,
4198 Label* throw_termination_exception,
4199 Label* throw_out_of_memory_exception,
4200 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004201 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004202 // eax: result parameter for PerformGC, if any
4203 // ebx: pointer to C function (C callee-saved)
4204 // ebp: frame pointer (restored after C call)
4205 // esp: stack pointer (restored after C call)
4206 // edi: number of arguments including receiver (C callee-saved)
4207 // esi: pointer to the first argument (C callee-saved)
4208
4209 // Result returned in eax, or eax+edx if result_size_ is 2.
4210
4211 // Check stack alignment.
4212 if (FLAG_debug_code) {
4213 __ CheckStackAlignment();
4214 }
4215
4216 if (do_gc) {
4217 // Pass failure code returned from last attempt as first argument to
4218 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4219 // stack alignment is known to be correct. This function takes one argument
4220 // which is passed on the stack, and we know that the stack has been
4221 // prepared to pass at least one argument.
4222 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4223 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4224 }
4225
4226 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004227 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004228 if (always_allocate_scope) {
4229 __ inc(Operand::StaticVariable(scope_depth));
4230 }
4231
4232 // Call C function.
4233 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4234 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004235 __ mov(Operand(esp, 2 * kPointerSize),
4236 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004237 __ call(Operand(ebx));
4238 // Result is in eax or edx:eax - do not destroy these registers!
4239
4240 if (always_allocate_scope) {
4241 __ dec(Operand::StaticVariable(scope_depth));
4242 }
4243
4244 // Make sure we're not trying to return 'the hole' from the runtime
4245 // call as this may lead to crashes in the IC code later.
4246 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004247 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004248 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004249 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004250 __ int3();
4251 __ bind(&okay);
4252 }
4253
4254 // Check for failure result.
4255 Label failure_returned;
4256 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4257 __ lea(ecx, Operand(eax, 1));
4258 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4259 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004260 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004261
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004262 ExternalReference pending_exception_address(
4263 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004264
4265 // Check that there is no pending exception, otherwise we
4266 // should have returned some failure value.
4267 if (FLAG_debug_code) {
4268 __ push(edx);
4269 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004270 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004271 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004272 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4273 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004274 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004275 __ int3();
4276 __ bind(&okay);
4277 __ pop(edx);
4278 }
4279
ricow@chromium.org65fae842010-08-25 15:26:24 +00004280 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004281 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004282 __ ret(0);
4283
4284 // Handling of failure.
4285 __ bind(&failure_returned);
4286
4287 Label retry;
4288 // If the returned exception is RETRY_AFTER_GC continue at retry label
4289 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4290 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004291 __ j(zero, &retry);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004292
4293 // Special handling of out of memory exceptions.
4294 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4295 __ j(equal, throw_out_of_memory_exception);
4296
4297 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004298 ExternalReference the_hole_location =
4299 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004300 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004301 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004302 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4303
4304 // Special handling of termination exceptions which are uncatchable
4305 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004306 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004307 __ j(equal, throw_termination_exception);
4308
4309 // Handle normal exception.
4310 __ jmp(throw_normal_exception);
4311
4312 // Retry.
4313 __ bind(&retry);
4314}
4315
4316
4317void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4318 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004319 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004320}
4321
4322
4323void CEntryStub::Generate(MacroAssembler* masm) {
4324 // eax: number of arguments including receiver
4325 // ebx: pointer to C function (C callee-saved)
4326 // ebp: frame pointer (restored after C call)
4327 // esp: stack pointer (restored after C call)
4328 // esi: current context (C callee-saved)
4329 // edi: JS function of the caller (C callee-saved)
4330
4331 // NOTE: Invocations of builtins may return failure objects instead
4332 // of a proper result. The builtin entry handles this by performing
4333 // a garbage collection and retrying the builtin (twice).
4334
4335 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004336 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004337
4338 // eax: result parameter for PerformGC, if any (setup below)
4339 // ebx: pointer to builtin function (C callee-saved)
4340 // ebp: frame pointer (restored after C call)
4341 // esp: stack pointer (restored after C call)
4342 // edi: number of arguments including receiver (C callee-saved)
4343 // esi: argv pointer (C callee-saved)
4344
4345 Label throw_normal_exception;
4346 Label throw_termination_exception;
4347 Label throw_out_of_memory_exception;
4348
4349 // Call into the runtime system.
4350 GenerateCore(masm,
4351 &throw_normal_exception,
4352 &throw_termination_exception,
4353 &throw_out_of_memory_exception,
4354 false,
4355 false);
4356
4357 // Do space-specific GC and retry runtime call.
4358 GenerateCore(masm,
4359 &throw_normal_exception,
4360 &throw_termination_exception,
4361 &throw_out_of_memory_exception,
4362 true,
4363 false);
4364
4365 // Do full GC and retry runtime call one final time.
4366 Failure* failure = Failure::InternalError();
4367 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4368 GenerateCore(masm,
4369 &throw_normal_exception,
4370 &throw_termination_exception,
4371 &throw_out_of_memory_exception,
4372 true,
4373 true);
4374
4375 __ bind(&throw_out_of_memory_exception);
4376 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4377
4378 __ bind(&throw_termination_exception);
4379 GenerateThrowUncatchable(masm, TERMINATION);
4380
4381 __ bind(&throw_normal_exception);
4382 GenerateThrowTOS(masm);
4383}
4384
4385
4386void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4387 Label invoke, exit;
4388#ifdef ENABLE_LOGGING_AND_PROFILING
4389 Label not_outermost_js, not_outermost_js_2;
4390#endif
4391
4392 // Setup frame.
4393 __ push(ebp);
4394 __ mov(ebp, Operand(esp));
4395
4396 // Push marker in two places.
4397 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4398 __ push(Immediate(Smi::FromInt(marker))); // context slot
4399 __ push(Immediate(Smi::FromInt(marker))); // function slot
4400 // Save callee-saved registers (C calling conventions).
4401 __ push(edi);
4402 __ push(esi);
4403 __ push(ebx);
4404
4405 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004406 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004407 __ push(Operand::StaticVariable(c_entry_fp));
4408
4409#ifdef ENABLE_LOGGING_AND_PROFILING
4410 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004411 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4412 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004413 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4414 __ j(not_equal, &not_outermost_js);
4415 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004416 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4417 Label cont;
4418 __ jmp(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004419 __ bind(&not_outermost_js);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004420 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
4421 __ bind(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004422#endif
4423
4424 // Call a faked try-block that does the invoke.
4425 __ call(&invoke);
4426
4427 // Caught exception: Store result (exception) in the pending
4428 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004429 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4430 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004431 __ mov(Operand::StaticVariable(pending_exception), eax);
4432 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4433 __ jmp(&exit);
4434
4435 // Invoke: Link this frame into the handler chain.
4436 __ bind(&invoke);
4437 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4438
4439 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004440 ExternalReference the_hole_location =
4441 ExternalReference::the_hole_value_location(masm->isolate());
4442 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004443 __ mov(Operand::StaticVariable(pending_exception), edx);
4444
4445 // Fake a receiver (NULL).
4446 __ push(Immediate(0)); // receiver
4447
4448 // Invoke the function by calling through JS entry trampoline
4449 // builtin and pop the faked function when we return. Notice that we
4450 // cannot store a reference to the trampoline code directly in this
4451 // stub, because the builtin stubs may not have been generated yet.
4452 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004453 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004454 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004455 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004456 __ mov(edx, Immediate(construct_entry));
4457 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004458 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004459 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004460 __ mov(edx, Immediate(entry));
4461 }
4462 __ mov(edx, Operand(edx, 0)); // deref address
4463 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4464 __ call(Operand(edx));
4465
4466 // Unlink this frame from the handler chain.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004467 __ PopTryHandler();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004468
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004469 __ bind(&exit);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004470#ifdef ENABLE_LOGGING_AND_PROFILING
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004471 // Check if the current stack frame is marked as the outermost JS frame.
4472 __ pop(ebx);
4473 __ cmp(Operand(ebx),
4474 Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004475 __ j(not_equal, &not_outermost_js_2);
4476 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4477 __ bind(&not_outermost_js_2);
4478#endif
4479
4480 // Restore the top frame descriptor from the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004481 __ pop(Operand::StaticVariable(ExternalReference(
4482 Isolate::k_c_entry_fp_address,
4483 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004484
4485 // Restore callee-saved registers (C calling conventions).
4486 __ pop(ebx);
4487 __ pop(esi);
4488 __ pop(edi);
4489 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4490
4491 // Restore frame pointer and return.
4492 __ pop(ebp);
4493 __ ret(0);
4494}
4495
4496
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004497// Generate stub code for instanceof.
4498// This code can patch a call site inlined cache of the instance of check,
4499// which looks like this.
4500//
4501// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4502// 75 0a jne <some near label>
4503// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4504//
4505// If call site patching is requested the stack will have the delta from the
4506// return address to the cmp instruction just below the return address. This
4507// also means that call site patching can only take place with arguments in
4508// registers. TOS looks like this when call site patching is requested
4509//
4510// esp[0] : return address
4511// esp[4] : delta from return address to cmp instruction
4512//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004513void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004514 // Call site inlining and patching implies arguments in registers.
4515 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4516
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004517 // Fixed register usage throughout the stub.
4518 Register object = eax; // Object (lhs).
4519 Register map = ebx; // Map of the object.
4520 Register function = edx; // Function (rhs).
4521 Register prototype = edi; // Prototype of the function.
4522 Register scratch = ecx;
4523
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004524 // Constants describing the call site code to patch.
4525 static const int kDeltaToCmpImmediate = 2;
4526 static const int kDeltaToMov = 8;
4527 static const int kDeltaToMovImmediate = 9;
4528 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4529 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4530 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4531
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004532 ExternalReference roots_address =
4533 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004534
4535 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4536 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4537
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004538 // Get the object and function - they are always both needed.
4539 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004540 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004541 __ mov(object, Operand(esp, 2 * kPointerSize));
4542 __ mov(function, Operand(esp, 1 * kPointerSize));
4543 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004544
4545 // Check that the left hand is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004546 __ JumpIfSmi(object, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004547 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004548
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004549 // If there is a call site cache don't look in the global cache, but do the
4550 // real lookup and update the call site cache.
4551 if (!HasCallSiteInlineCheck()) {
4552 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004553 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004554 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4555 __ cmp(function,
4556 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004557 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004558 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4559 __ cmp(map, Operand::StaticArray(
4560 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004561 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004562 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4563 __ mov(eax, Operand::StaticArray(
4564 scratch, times_pointer_size, roots_address));
4565 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4566 __ bind(&miss);
4567 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004568
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004569 // Get the prototype of the function.
4570 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004571
4572 // Check that the function prototype is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004573 __ JumpIfSmi(prototype, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004574 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004575
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004576 // Update the global instanceof or call site inlined cache with the current
4577 // map and function. The cached answer will be set when it is known below.
4578 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004579 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4580 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4581 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4582 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4583 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004584 } else {
4585 // The constants for the code patching are based on no push instructions
4586 // at the call site.
4587 ASSERT(HasArgsInRegisters());
4588 // Get return address and delta to inlined map check.
4589 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4590 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4591 if (FLAG_debug_code) {
4592 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4593 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4594 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4595 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4596 }
4597 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4598 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004599
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004600 // Loop through the prototype chain of the object looking for the function
4601 // prototype.
4602 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004603 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004604 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004605 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004606 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004607 Factory* factory = masm->isolate()->factory();
4608 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004609 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004610 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4611 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004612 __ jmp(&loop);
4613
4614 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004615 if (!HasCallSiteInlineCheck()) {
4616 __ Set(eax, Immediate(0));
4617 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4618 __ mov(Operand::StaticArray(scratch,
4619 times_pointer_size, roots_address), eax);
4620 } else {
4621 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004622 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004623 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4624 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4625 if (FLAG_debug_code) {
4626 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4627 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4628 }
4629 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4630 if (!ReturnTrueFalseObject()) {
4631 __ Set(eax, Immediate(0));
4632 }
4633 }
4634 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004635
4636 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004637 if (!HasCallSiteInlineCheck()) {
4638 __ Set(eax, Immediate(Smi::FromInt(1)));
4639 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4640 __ mov(Operand::StaticArray(
4641 scratch, times_pointer_size, roots_address), eax);
4642 } else {
4643 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004644 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004645 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4646 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4647 if (FLAG_debug_code) {
4648 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4649 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4650 }
4651 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4652 if (!ReturnTrueFalseObject()) {
4653 __ Set(eax, Immediate(Smi::FromInt(1)));
4654 }
4655 }
4656 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004657
4658 Label object_not_null, object_not_null_or_smi;
4659 __ bind(&not_js_object);
4660 // Before null, smi and string value checks, check that the rhs is a function
4661 // as for a non-function rhs an exception needs to be thrown.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004662 __ JumpIfSmi(function, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004663 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004664 __ j(not_equal, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004665
4666 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004667 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004668 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004669 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004670 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004671
4672 __ bind(&object_not_null);
4673 // Smi values is not instance of anything.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004674 __ JumpIfNotSmi(object, &object_not_null_or_smi);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004675 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004676 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004677
4678 __ bind(&object_not_null_or_smi);
4679 // String values is not instance of anything.
4680 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4681 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004682 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004683 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004684
4685 // Slow-case: Go through the JavaScript implementation.
4686 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004687 if (!ReturnTrueFalseObject()) {
4688 // Tail call the builtin which returns 0 or 1.
4689 if (HasArgsInRegisters()) {
4690 // Push arguments below return address.
4691 __ pop(scratch);
4692 __ push(object);
4693 __ push(function);
4694 __ push(scratch);
4695 }
4696 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4697 } else {
4698 // Call the builtin and convert 0/1 to true/false.
4699 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004700 __ push(object);
4701 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004702 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4703 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004704 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004705 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004706 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004707 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004708 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004709 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004710 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004711 __ bind(&done);
4712 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004713 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004714}
4715
4716
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004717Register InstanceofStub::left() { return eax; }
4718
4719
4720Register InstanceofStub::right() { return edx; }
4721
4722
ricow@chromium.org65fae842010-08-25 15:26:24 +00004723int CompareStub::MinorKey() {
4724 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4725 // stubs the never NaN NaN condition is only taken into account if the
4726 // condition is equals.
4727 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4728 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4729 return ConditionField::encode(static_cast<unsigned>(cc_))
4730 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4731 | StrictField::encode(strict_)
4732 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004733 | IncludeNumberCompareField::encode(include_number_compare_)
4734 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004735}
4736
4737
4738// Unfortunately you have to run without snapshots to see most of these
4739// names in the profile since most compare stubs end up in the snapshot.
4740const char* CompareStub::GetName() {
4741 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4742
4743 if (name_ != NULL) return name_;
4744 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004745 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4746 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004747 if (name_ == NULL) return "OOM";
4748
4749 const char* cc_name;
4750 switch (cc_) {
4751 case less: cc_name = "LT"; break;
4752 case greater: cc_name = "GT"; break;
4753 case less_equal: cc_name = "LE"; break;
4754 case greater_equal: cc_name = "GE"; break;
4755 case equal: cc_name = "EQ"; break;
4756 case not_equal: cc_name = "NE"; break;
4757 default: cc_name = "UnknownCondition"; break;
4758 }
4759
4760 const char* strict_name = "";
4761 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4762 strict_name = "_STRICT";
4763 }
4764
4765 const char* never_nan_nan_name = "";
4766 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4767 never_nan_nan_name = "_NO_NAN";
4768 }
4769
4770 const char* include_number_compare_name = "";
4771 if (!include_number_compare_) {
4772 include_number_compare_name = "_NO_NUMBER";
4773 }
4774
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004775 const char* include_smi_compare_name = "";
4776 if (!include_smi_compare_) {
4777 include_smi_compare_name = "_NO_SMI";
4778 }
4779
ricow@chromium.org65fae842010-08-25 15:26:24 +00004780 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004781 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004782 cc_name,
4783 strict_name,
4784 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004785 include_number_compare_name,
4786 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004787 return name_;
4788}
4789
4790
4791// -------------------------------------------------------------------------
4792// StringCharCodeAtGenerator
4793
4794void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4795 Label flat_string;
4796 Label ascii_string;
4797 Label got_char_code;
4798
4799 // If the receiver is a smi trigger the non-string case.
4800 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004801 __ JumpIfSmi(object_, receiver_not_string_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004802
4803 // Fetch the instance type of the receiver into result register.
4804 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4805 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4806 // If the receiver is not a string trigger the non-string case.
4807 __ test(result_, Immediate(kIsNotStringMask));
4808 __ j(not_zero, receiver_not_string_);
4809
4810 // If the index is non-smi trigger the non-smi case.
4811 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004812 __ JumpIfNotSmi(index_, &index_not_smi_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004813
4814 // Put smi-tagged index into scratch register.
4815 __ mov(scratch_, index_);
4816 __ bind(&got_smi_index_);
4817
4818 // Check for index out of range.
4819 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4820 __ j(above_equal, index_out_of_range_);
4821
4822 // We need special handling for non-flat strings.
4823 STATIC_ASSERT(kSeqStringTag == 0);
4824 __ test(result_, Immediate(kStringRepresentationMask));
4825 __ j(zero, &flat_string);
4826
4827 // Handle non-flat strings.
4828 __ test(result_, Immediate(kIsConsStringMask));
4829 __ j(zero, &call_runtime_);
4830
4831 // ConsString.
4832 // Check whether the right hand side is the empty string (i.e. if
4833 // this is really a flat string in a cons string). If that is not
4834 // the case we would rather go to the runtime system now to flatten
4835 // the string.
4836 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004837 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004838 __ j(not_equal, &call_runtime_);
4839 // Get the first of the two strings and load its instance type.
4840 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4841 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4842 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4843 // If the first cons component is also non-flat, then go to runtime.
4844 STATIC_ASSERT(kSeqStringTag == 0);
4845 __ test(result_, Immediate(kStringRepresentationMask));
4846 __ j(not_zero, &call_runtime_);
4847
4848 // Check for 1-byte or 2-byte string.
4849 __ bind(&flat_string);
4850 STATIC_ASSERT(kAsciiStringTag != 0);
4851 __ test(result_, Immediate(kStringEncodingMask));
4852 __ j(not_zero, &ascii_string);
4853
4854 // 2-byte string.
4855 // Load the 2-byte character code into the result register.
4856 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4857 __ movzx_w(result_, FieldOperand(object_,
4858 scratch_, times_1, // Scratch is smi-tagged.
4859 SeqTwoByteString::kHeaderSize));
4860 __ jmp(&got_char_code);
4861
4862 // ASCII string.
4863 // Load the byte into the result register.
4864 __ bind(&ascii_string);
4865 __ SmiUntag(scratch_);
4866 __ movzx_b(result_, FieldOperand(object_,
4867 scratch_, times_1,
4868 SeqAsciiString::kHeaderSize));
4869 __ bind(&got_char_code);
4870 __ SmiTag(result_);
4871 __ bind(&exit_);
4872}
4873
4874
4875void StringCharCodeAtGenerator::GenerateSlow(
4876 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4877 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4878
4879 // Index is not a smi.
4880 __ bind(&index_not_smi_);
4881 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004882 __ CheckMap(index_,
4883 masm->isolate()->factory()->heap_number_map(),
4884 index_not_number_,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004885 DONT_DO_SMI_CHECK);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004886 call_helper.BeforeCall(masm);
4887 __ push(object_);
4888 __ push(index_);
4889 __ push(index_); // Consumed by runtime conversion function.
4890 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4891 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4892 } else {
4893 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4894 // NumberToSmi discards numbers that are not exact integers.
4895 __ CallRuntime(Runtime::kNumberToSmi, 1);
4896 }
4897 if (!scratch_.is(eax)) {
4898 // Save the conversion result before the pop instructions below
4899 // have a chance to overwrite it.
4900 __ mov(scratch_, eax);
4901 }
4902 __ pop(index_);
4903 __ pop(object_);
4904 // Reload the instance type.
4905 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4906 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4907 call_helper.AfterCall(masm);
4908 // If index is still not a smi, it must be out of range.
4909 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004910 __ JumpIfNotSmi(scratch_, index_out_of_range_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004911 // Otherwise, return to the fast path.
4912 __ jmp(&got_smi_index_);
4913
4914 // Call runtime. We get here when the receiver is a string and the
4915 // index is a number, but the code of getting the actual character
4916 // is too complex (e.g., when the string needs to be flattened).
4917 __ bind(&call_runtime_);
4918 call_helper.BeforeCall(masm);
4919 __ push(object_);
4920 __ push(index_);
4921 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4922 if (!result_.is(eax)) {
4923 __ mov(result_, eax);
4924 }
4925 call_helper.AfterCall(masm);
4926 __ jmp(&exit_);
4927
4928 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4929}
4930
4931
4932// -------------------------------------------------------------------------
4933// StringCharFromCodeGenerator
4934
4935void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4936 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4937 STATIC_ASSERT(kSmiTag == 0);
4938 STATIC_ASSERT(kSmiShiftSize == 0);
4939 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4940 __ test(code_,
4941 Immediate(kSmiTagMask |
4942 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004943 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004944
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004945 Factory* factory = masm->isolate()->factory();
4946 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004947 STATIC_ASSERT(kSmiTag == 0);
4948 STATIC_ASSERT(kSmiTagSize == 1);
4949 STATIC_ASSERT(kSmiShiftSize == 0);
4950 // At this point code register contains smi tagged ascii char code.
4951 __ mov(result_, FieldOperand(result_,
4952 code_, times_half_pointer_size,
4953 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004954 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004955 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004956 __ bind(&exit_);
4957}
4958
4959
4960void StringCharFromCodeGenerator::GenerateSlow(
4961 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4962 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4963
4964 __ bind(&slow_case_);
4965 call_helper.BeforeCall(masm);
4966 __ push(code_);
4967 __ CallRuntime(Runtime::kCharFromCode, 1);
4968 if (!result_.is(eax)) {
4969 __ mov(result_, eax);
4970 }
4971 call_helper.AfterCall(masm);
4972 __ jmp(&exit_);
4973
4974 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4975}
4976
4977
4978// -------------------------------------------------------------------------
4979// StringCharAtGenerator
4980
4981void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4982 char_code_at_generator_.GenerateFast(masm);
4983 char_from_code_generator_.GenerateFast(masm);
4984}
4985
4986
4987void StringCharAtGenerator::GenerateSlow(
4988 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4989 char_code_at_generator_.GenerateSlow(masm, call_helper);
4990 char_from_code_generator_.GenerateSlow(masm, call_helper);
4991}
4992
4993
4994void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004995 Label string_add_runtime, call_builtin;
4996 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004997
4998 // Load the two arguments.
4999 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5000 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5001
5002 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005003 if (flags_ == NO_STRING_ADD_FLAGS) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00005004 __ JumpIfSmi(eax, &string_add_runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005005 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
5006 __ j(above_equal, &string_add_runtime);
5007
5008 // First argument is a a string, test second.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005009 __ JumpIfSmi(edx, &string_add_runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005010 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
5011 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005012 } else {
5013 // Here at least one of the arguments is definitely a string.
5014 // We convert the one that is not known to be a string.
5015 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5016 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5017 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5018 &call_builtin);
5019 builtin_id = Builtins::STRING_ADD_RIGHT;
5020 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5021 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5022 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5023 &call_builtin);
5024 builtin_id = Builtins::STRING_ADD_LEFT;
5025 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005026 }
5027
5028 // Both arguments are strings.
5029 // eax: first string
5030 // edx: second string
5031 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005032 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005033 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5034 STATIC_ASSERT(kSmiTag == 0);
5035 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005036 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005037 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005038 Counters* counters = masm->isolate()->counters();
5039 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005040 __ ret(2 * kPointerSize);
5041 __ bind(&second_not_zero_length);
5042 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5043 STATIC_ASSERT(kSmiTag == 0);
5044 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005045 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005046 // First string is empty, result is second string which is in edx.
5047 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005048 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005049 __ ret(2 * kPointerSize);
5050
5051 // Both strings are non-empty.
5052 // eax: first string
5053 // ebx: length of first string as a smi
5054 // ecx: length of second string as a smi
5055 // edx: second string
5056 // Look at the length of the result of adding the two strings.
5057 Label string_add_flat_result, longer_than_two;
5058 __ bind(&both_not_zero_length);
5059 __ add(ebx, Operand(ecx));
5060 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5061 // Handle exceptionally long strings in the runtime system.
5062 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005063 // Use the symbol table when adding two one character strings, as it
5064 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005065 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
5066 __ j(not_equal, &longer_than_two);
5067
5068 // Check that both strings are non-external ascii strings.
5069 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
5070 &string_add_runtime);
5071
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005072 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005073 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5074 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5075
5076 // Try to lookup two character string in symbol table. If it is not found
5077 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005078 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005079 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005080 masm, ebx, ecx, eax, edx, edi,
5081 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005082 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005083 __ ret(2 * kPointerSize);
5084
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005085 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005086 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005087 // Reload the arguments.
5088 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5089 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5090 // Get the two characters forming the new string.
5091 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5092 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5093 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005094 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005095 __ AllocateAsciiString(eax, // Result.
5096 2, // Length.
5097 edi, // Scratch 1.
5098 edx, // Scratch 2.
5099 &string_add_runtime);
5100 // Pack both characters in ebx.
5101 __ shl(ecx, kBitsPerByte);
5102 __ or_(ebx, Operand(ecx));
5103 // Set the characters in the new string.
5104 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005105 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005106 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005107
5108 __ bind(&longer_than_two);
5109 // Check if resulting string will be flat.
5110 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
5111 __ j(below, &string_add_flat_result);
5112
5113 // If result is not supposed to be flat allocate a cons string object. If both
5114 // strings are ascii the result is an ascii cons string.
5115 Label non_ascii, allocated, ascii_data;
5116 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5117 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5118 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5119 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5120 __ and_(ecx, Operand(edi));
5121 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5122 __ test(ecx, Immediate(kAsciiStringTag));
5123 __ j(zero, &non_ascii);
5124 __ bind(&ascii_data);
5125 // Allocate an acsii cons string.
5126 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5127 __ bind(&allocated);
5128 // Fill the fields of the cons string.
5129 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5130 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5131 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5132 Immediate(String::kEmptyHashField));
5133 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5134 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5135 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005136 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005137 __ ret(2 * kPointerSize);
5138 __ bind(&non_ascii);
5139 // At least one of the strings is two-byte. Check whether it happens
5140 // to contain only ascii characters.
5141 // ecx: first instance type AND second instance type.
5142 // edi: second instance type.
5143 __ test(ecx, Immediate(kAsciiDataHintMask));
5144 __ j(not_zero, &ascii_data);
5145 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5146 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5147 __ xor_(edi, Operand(ecx));
5148 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5149 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5150 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5151 __ j(equal, &ascii_data);
5152 // Allocate a two byte cons string.
5153 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5154 __ jmp(&allocated);
5155
5156 // Handle creating a flat result. First check that both strings are not
5157 // external strings.
5158 // eax: first string
5159 // ebx: length of resulting flat string as a smi
5160 // edx: second string
5161 __ bind(&string_add_flat_result);
5162 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5163 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5164 __ and_(ecx, kStringRepresentationMask);
5165 __ cmp(ecx, kExternalStringTag);
5166 __ j(equal, &string_add_runtime);
5167 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5168 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5169 __ and_(ecx, kStringRepresentationMask);
5170 __ cmp(ecx, kExternalStringTag);
5171 __ j(equal, &string_add_runtime);
5172 // Now check if both strings are ascii strings.
5173 // eax: first string
5174 // ebx: length of resulting flat string as a smi
5175 // edx: second string
5176 Label non_ascii_string_add_flat_result;
5177 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5178 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5179 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5180 __ j(zero, &non_ascii_string_add_flat_result);
5181 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5182 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5183 __ j(zero, &string_add_runtime);
5184
ricow@chromium.org65fae842010-08-25 15:26:24 +00005185 // Both strings are ascii strings. As they are short they are both flat.
5186 // ebx: length of resulting flat string as a smi
5187 __ SmiUntag(ebx);
5188 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5189 // eax: result string
5190 __ mov(ecx, eax);
5191 // Locate first character of result.
5192 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5193 // Load first argument and locate first character.
5194 __ mov(edx, Operand(esp, 2 * kPointerSize));
5195 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5196 __ SmiUntag(edi);
5197 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5198 // eax: result string
5199 // ecx: first character of result
5200 // edx: first char of first argument
5201 // edi: length of first argument
5202 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5203 // Load second argument and locate first character.
5204 __ mov(edx, Operand(esp, 1 * kPointerSize));
5205 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5206 __ SmiUntag(edi);
5207 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5208 // eax: result string
5209 // ecx: next character of result
5210 // edx: first char of second argument
5211 // edi: length of second argument
5212 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005213 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005214 __ ret(2 * kPointerSize);
5215
5216 // Handle creating a flat two byte result.
5217 // eax: first string - known to be two byte
5218 // ebx: length of resulting flat string as a smi
5219 // edx: second string
5220 __ bind(&non_ascii_string_add_flat_result);
5221 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5222 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5223 __ j(not_zero, &string_add_runtime);
5224 // Both strings are two byte strings. As they are short they are both
5225 // flat.
5226 __ SmiUntag(ebx);
5227 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5228 // eax: result string
5229 __ mov(ecx, eax);
5230 // Locate first character of result.
5231 __ add(Operand(ecx),
5232 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5233 // Load first argument and locate first character.
5234 __ mov(edx, Operand(esp, 2 * kPointerSize));
5235 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5236 __ SmiUntag(edi);
5237 __ add(Operand(edx),
5238 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5239 // eax: result string
5240 // ecx: first character of result
5241 // edx: first char of first argument
5242 // edi: length of first argument
5243 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5244 // Load second argument and locate first character.
5245 __ mov(edx, Operand(esp, 1 * kPointerSize));
5246 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5247 __ SmiUntag(edi);
5248 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5249 // eax: result string
5250 // ecx: next character of result
5251 // edx: first char of second argument
5252 // edi: length of second argument
5253 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005254 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005255 __ ret(2 * kPointerSize);
5256
5257 // Just jump to runtime to add the two strings.
5258 __ bind(&string_add_runtime);
5259 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005260
5261 if (call_builtin.is_linked()) {
5262 __ bind(&call_builtin);
5263 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5264 }
5265}
5266
5267
5268void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5269 int stack_offset,
5270 Register arg,
5271 Register scratch1,
5272 Register scratch2,
5273 Register scratch3,
5274 Label* slow) {
5275 // First check if the argument is already a string.
5276 Label not_string, done;
whesse@chromium.org7b260152011-06-20 15:33:18 +00005277 __ JumpIfSmi(arg, &not_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005278 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5279 __ j(below, &done);
5280
5281 // Check the number to string cache.
5282 Label not_cached;
5283 __ bind(&not_string);
5284 // Puts the cached result into scratch1.
5285 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5286 arg,
5287 scratch1,
5288 scratch2,
5289 scratch3,
5290 false,
5291 &not_cached);
5292 __ mov(arg, scratch1);
5293 __ mov(Operand(esp, stack_offset), arg);
5294 __ jmp(&done);
5295
5296 // Check if the argument is a safe string wrapper.
5297 __ bind(&not_cached);
whesse@chromium.org7b260152011-06-20 15:33:18 +00005298 __ JumpIfSmi(arg, slow);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005299 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5300 __ j(not_equal, slow);
5301 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5302 1 << Map::kStringWrapperSafeForDefaultValueOf);
5303 __ j(zero, slow);
5304 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5305 __ mov(Operand(esp, stack_offset), arg);
5306
5307 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005308}
5309
5310
5311void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5312 Register dest,
5313 Register src,
5314 Register count,
5315 Register scratch,
5316 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005317 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005318 __ bind(&loop);
5319 // This loop just copies one character at a time, as it is only used for very
5320 // short strings.
5321 if (ascii) {
5322 __ mov_b(scratch, Operand(src, 0));
5323 __ mov_b(Operand(dest, 0), scratch);
5324 __ add(Operand(src), Immediate(1));
5325 __ add(Operand(dest), Immediate(1));
5326 } else {
5327 __ mov_w(scratch, Operand(src, 0));
5328 __ mov_w(Operand(dest, 0), scratch);
5329 __ add(Operand(src), Immediate(2));
5330 __ add(Operand(dest), Immediate(2));
5331 }
5332 __ sub(Operand(count), Immediate(1));
5333 __ j(not_zero, &loop);
5334}
5335
5336
5337void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5338 Register dest,
5339 Register src,
5340 Register count,
5341 Register scratch,
5342 bool ascii) {
5343 // Copy characters using rep movs of doublewords.
5344 // The destination is aligned on a 4 byte boundary because we are
5345 // copying to the beginning of a newly allocated string.
5346 ASSERT(dest.is(edi)); // rep movs destination
5347 ASSERT(src.is(esi)); // rep movs source
5348 ASSERT(count.is(ecx)); // rep movs count
5349 ASSERT(!scratch.is(dest));
5350 ASSERT(!scratch.is(src));
5351 ASSERT(!scratch.is(count));
5352
5353 // Nothing to do for zero characters.
5354 Label done;
5355 __ test(count, Operand(count));
5356 __ j(zero, &done);
5357
5358 // Make count the number of bytes to copy.
5359 if (!ascii) {
5360 __ shl(count, 1);
5361 }
5362
5363 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005364 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005365 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005366 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005367
5368 // Copy from edi to esi using rep movs instruction.
5369 __ mov(scratch, count);
5370 __ sar(count, 2); // Number of doublewords to copy.
5371 __ cld();
5372 __ rep_movs();
5373
5374 // Find number of bytes left.
5375 __ mov(count, scratch);
5376 __ and_(count, 3);
5377
5378 // Check if there are more bytes to copy.
5379 __ bind(&last_bytes);
5380 __ test(count, Operand(count));
5381 __ j(zero, &done);
5382
5383 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005384 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005385 __ bind(&loop);
5386 __ mov_b(scratch, Operand(src, 0));
5387 __ mov_b(Operand(dest, 0), scratch);
5388 __ add(Operand(src), Immediate(1));
5389 __ add(Operand(dest), Immediate(1));
5390 __ sub(Operand(count), Immediate(1));
5391 __ j(not_zero, &loop);
5392
5393 __ bind(&done);
5394}
5395
5396
5397void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5398 Register c1,
5399 Register c2,
5400 Register scratch1,
5401 Register scratch2,
5402 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005403 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005404 Label* not_found) {
5405 // Register scratch3 is the general scratch register in this function.
5406 Register scratch = scratch3;
5407
5408 // Make sure that both characters are not digits as such strings has a
5409 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005410 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005411 __ mov(scratch, c1);
5412 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5413 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005414 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005415 __ mov(scratch, c2);
5416 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5417 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005418 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005419
5420 __ bind(&not_array_index);
5421 // Calculate the two character string hash.
5422 Register hash = scratch1;
5423 GenerateHashInit(masm, hash, c1, scratch);
5424 GenerateHashAddCharacter(masm, hash, c2, scratch);
5425 GenerateHashGetHash(masm, hash, scratch);
5426
5427 // Collect the two characters in a register.
5428 Register chars = c1;
5429 __ shl(c2, kBitsPerByte);
5430 __ or_(chars, Operand(c2));
5431
5432 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5433 // hash: hash of two character string.
5434
5435 // Load the symbol table.
5436 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005437 ExternalReference roots_address =
5438 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005439 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5440 __ mov(symbol_table,
5441 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5442
5443 // Calculate capacity mask from the symbol table capacity.
5444 Register mask = scratch2;
5445 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5446 __ SmiUntag(mask);
5447 __ sub(Operand(mask), Immediate(1));
5448
5449 // Registers
5450 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5451 // hash: hash of two character string
5452 // symbol_table: symbol table
5453 // mask: capacity mask
5454 // scratch: -
5455
5456 // Perform a number of probes in the symbol table.
5457 static const int kProbes = 4;
5458 Label found_in_symbol_table;
5459 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5460 for (int i = 0; i < kProbes; i++) {
5461 // Calculate entry in symbol table.
5462 __ mov(scratch, hash);
5463 if (i > 0) {
5464 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5465 }
5466 __ and_(scratch, Operand(mask));
5467
5468 // Load the entry from the symbol table.
5469 Register candidate = scratch; // Scratch register contains candidate.
5470 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5471 __ mov(candidate,
5472 FieldOperand(symbol_table,
5473 scratch,
5474 times_pointer_size,
5475 SymbolTable::kElementsStartOffset));
5476
5477 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005478 Factory* factory = masm->isolate()->factory();
5479 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005480 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005481 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005482 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005483
5484 // If length is not 2 the string is not a candidate.
5485 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5486 Immediate(Smi::FromInt(2)));
5487 __ j(not_equal, &next_probe[i]);
5488
5489 // As we are out of registers save the mask on the stack and use that
5490 // register as a temporary.
5491 __ push(mask);
5492 Register temp = mask;
5493
5494 // Check that the candidate is a non-external ascii string.
5495 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5496 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5497 __ JumpIfInstanceTypeIsNotSequentialAscii(
5498 temp, temp, &next_probe_pop_mask[i]);
5499
5500 // Check if the two characters match.
5501 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5502 __ and_(temp, 0x0000ffff);
5503 __ cmp(chars, Operand(temp));
5504 __ j(equal, &found_in_symbol_table);
5505 __ bind(&next_probe_pop_mask[i]);
5506 __ pop(mask);
5507 __ bind(&next_probe[i]);
5508 }
5509
5510 // No matching 2 character string found by probing.
5511 __ jmp(not_found);
5512
5513 // Scratch register contains result when we fall through to here.
5514 Register result = scratch;
5515 __ bind(&found_in_symbol_table);
5516 __ pop(mask); // Pop saved mask from the stack.
5517 if (!result.is(eax)) {
5518 __ mov(eax, result);
5519 }
5520}
5521
5522
5523void StringHelper::GenerateHashInit(MacroAssembler* masm,
5524 Register hash,
5525 Register character,
5526 Register scratch) {
5527 // hash = character + (character << 10);
5528 __ mov(hash, character);
5529 __ shl(hash, 10);
5530 __ add(hash, Operand(character));
5531 // hash ^= hash >> 6;
5532 __ mov(scratch, hash);
5533 __ sar(scratch, 6);
5534 __ xor_(hash, Operand(scratch));
5535}
5536
5537
5538void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5539 Register hash,
5540 Register character,
5541 Register scratch) {
5542 // hash += character;
5543 __ add(hash, Operand(character));
5544 // hash += hash << 10;
5545 __ mov(scratch, hash);
5546 __ shl(scratch, 10);
5547 __ add(hash, Operand(scratch));
5548 // hash ^= hash >> 6;
5549 __ mov(scratch, hash);
5550 __ sar(scratch, 6);
5551 __ xor_(hash, Operand(scratch));
5552}
5553
5554
5555void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5556 Register hash,
5557 Register scratch) {
5558 // hash += hash << 3;
5559 __ mov(scratch, hash);
5560 __ shl(scratch, 3);
5561 __ add(hash, Operand(scratch));
5562 // hash ^= hash >> 11;
5563 __ mov(scratch, hash);
5564 __ sar(scratch, 11);
5565 __ xor_(hash, Operand(scratch));
5566 // hash += hash << 15;
5567 __ mov(scratch, hash);
5568 __ shl(scratch, 15);
5569 __ add(hash, Operand(scratch));
5570
5571 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005572 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005573 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005574 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005575 __ mov(hash, Immediate(27));
5576 __ bind(&hash_not_zero);
5577}
5578
5579
5580void SubStringStub::Generate(MacroAssembler* masm) {
5581 Label runtime;
5582
5583 // Stack frame on entry.
5584 // esp[0]: return address
5585 // esp[4]: to
5586 // esp[8]: from
5587 // esp[12]: string
5588
5589 // Make sure first argument is a string.
5590 __ mov(eax, Operand(esp, 3 * kPointerSize));
5591 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00005592 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005593 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5594 __ j(NegateCondition(is_string), &runtime);
5595
5596 // eax: string
5597 // ebx: instance type
5598
5599 // Calculate length of sub string using the smi values.
5600 Label result_longer_than_two;
5601 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005602 __ JumpIfNotSmi(ecx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005603 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005604 __ JumpIfNotSmi(edx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005605 __ sub(ecx, Operand(edx));
5606 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5607 Label return_eax;
5608 __ j(equal, &return_eax);
5609 // Special handling of sub-strings of length 1 and 2. One character strings
5610 // are handled in the runtime system (looked up in the single character
5611 // cache). Two character strings are looked for in the symbol cache.
5612 __ SmiUntag(ecx); // Result length is no longer smi.
5613 __ cmp(ecx, 2);
5614 __ j(greater, &result_longer_than_two);
5615 __ j(less, &runtime);
5616
5617 // Sub string of length 2 requested.
5618 // eax: string
5619 // ebx: instance type
5620 // ecx: sub string length (value is 2)
5621 // edx: from index (smi)
5622 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5623
5624 // Get the two characters forming the sub string.
5625 __ SmiUntag(edx); // From index is no longer smi.
5626 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5627 __ movzx_b(ecx,
5628 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5629
5630 // Try to lookup two character string in symbol table.
5631 Label make_two_character_string;
5632 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005633 masm, ebx, ecx, eax, edx, edi,
5634 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005635 __ ret(3 * kPointerSize);
5636
5637 __ bind(&make_two_character_string);
5638 // Setup registers for allocating the two character string.
5639 __ mov(eax, Operand(esp, 3 * kPointerSize));
5640 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5641 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5642 __ Set(ecx, Immediate(2));
5643
5644 __ bind(&result_longer_than_two);
5645 // eax: string
5646 // ebx: instance type
5647 // ecx: result string length
5648 // Check for flat ascii string
5649 Label non_ascii_flat;
5650 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5651
5652 // Allocate the result.
5653 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5654
5655 // eax: result string
5656 // ecx: result string length
5657 __ mov(edx, esi); // esi used by following code.
5658 // Locate first character of result.
5659 __ mov(edi, eax);
5660 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5661 // Load string argument and locate character of sub string start.
5662 __ mov(esi, Operand(esp, 3 * kPointerSize));
5663 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5664 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5665 __ SmiUntag(ebx);
5666 __ add(esi, Operand(ebx));
5667
5668 // eax: result string
5669 // ecx: result length
5670 // edx: original value of esi
5671 // edi: first character of result
5672 // esi: character of sub string start
5673 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5674 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005675 Counters* counters = masm->isolate()->counters();
5676 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005677 __ ret(3 * kPointerSize);
5678
5679 __ bind(&non_ascii_flat);
5680 // eax: string
5681 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5682 // ecx: result string length
5683 // Check for flat two byte string
5684 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5685 __ j(not_equal, &runtime);
5686
5687 // Allocate the result.
5688 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5689
5690 // eax: result string
5691 // ecx: result string length
5692 __ mov(edx, esi); // esi used by following code.
5693 // Locate first character of result.
5694 __ mov(edi, eax);
5695 __ add(Operand(edi),
5696 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5697 // Load string argument and locate character of sub string start.
5698 __ mov(esi, Operand(esp, 3 * kPointerSize));
5699 __ add(Operand(esi),
5700 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5701 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5702 // As from is a smi it is 2 times the value which matches the size of a two
5703 // byte character.
5704 STATIC_ASSERT(kSmiTag == 0);
5705 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5706 __ add(esi, Operand(ebx));
5707
5708 // eax: result string
5709 // ecx: result length
5710 // edx: original value of esi
5711 // edi: first character of result
5712 // esi: character of sub string start
5713 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5714 __ mov(esi, edx); // Restore esi.
5715
5716 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005717 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005718 __ ret(3 * kPointerSize);
5719
5720 // Just jump to runtime to create the sub string.
5721 __ bind(&runtime);
5722 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5723}
5724
5725
lrn@chromium.org1c092762011-05-09 09:42:16 +00005726void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5727 Register left,
5728 Register right,
5729 Register scratch1,
5730 Register scratch2) {
5731 Register length = scratch1;
5732
5733 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005734 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005735 __ mov(length, FieldOperand(left, String::kLengthOffset));
5736 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005737 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005738 __ bind(&strings_not_equal);
5739 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5740 __ ret(0);
5741
5742 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005743 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005744 __ bind(&check_zero_length);
5745 STATIC_ASSERT(kSmiTag == 0);
5746 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005747 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005748 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5749 __ ret(0);
5750
5751 // Compare characters.
5752 __ bind(&compare_chars);
5753 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005754 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005755
5756 // Characters are equal.
5757 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5758 __ ret(0);
5759}
5760
5761
ricow@chromium.org65fae842010-08-25 15:26:24 +00005762void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5763 Register left,
5764 Register right,
5765 Register scratch1,
5766 Register scratch2,
5767 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005768 Counters* counters = masm->isolate()->counters();
5769 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005770
5771 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005772 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005773 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5774 __ mov(scratch3, scratch1);
5775 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5776
5777 Register length_delta = scratch3;
5778
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005779 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005780 // Right string is shorter. Change scratch1 to be length of right string.
5781 __ sub(scratch1, Operand(length_delta));
5782 __ bind(&left_shorter);
5783
5784 Register min_length = scratch1;
5785
5786 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005787 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005788 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005789 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005790
lrn@chromium.org1c092762011-05-09 09:42:16 +00005791 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005792 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005793 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005794 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005795
5796 // Compare lengths - strings up to min-length are equal.
5797 __ bind(&compare_lengths);
5798 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005799 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005800
5801 // Result is EQUAL.
5802 STATIC_ASSERT(EQUAL == 0);
5803 STATIC_ASSERT(kSmiTag == 0);
5804 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5805 __ ret(0);
5806
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005807 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005808 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005809 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005810
5811 // Result is LESS.
5812 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5813 __ ret(0);
5814
5815 // Result is GREATER.
5816 __ bind(&result_greater);
5817 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5818 __ ret(0);
5819}
5820
5821
lrn@chromium.org1c092762011-05-09 09:42:16 +00005822void StringCompareStub::GenerateAsciiCharsCompareLoop(
5823 MacroAssembler* masm,
5824 Register left,
5825 Register right,
5826 Register length,
5827 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005828 Label* chars_not_equal,
5829 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005830 // Change index to run from -length to -1 by adding length to string
5831 // start. This means that loop ends when index reaches zero, which
5832 // doesn't need an additional compare.
5833 __ SmiUntag(length);
5834 __ lea(left,
5835 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5836 __ lea(right,
5837 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5838 __ neg(length);
5839 Register index = length; // index = -length;
5840
5841 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005842 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005843 __ bind(&loop);
5844 __ mov_b(scratch, Operand(left, index, times_1, 0));
5845 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005846 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005847 __ add(Operand(index), Immediate(1));
5848 __ j(not_zero, &loop);
5849}
5850
5851
ricow@chromium.org65fae842010-08-25 15:26:24 +00005852void StringCompareStub::Generate(MacroAssembler* masm) {
5853 Label runtime;
5854
5855 // Stack frame on entry.
5856 // esp[0]: return address
5857 // esp[4]: right string
5858 // esp[8]: left string
5859
5860 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5861 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5862
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005863 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005864 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005865 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005866 STATIC_ASSERT(EQUAL == 0);
5867 STATIC_ASSERT(kSmiTag == 0);
5868 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005869 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005870 __ ret(2 * kPointerSize);
5871
5872 __ bind(&not_same);
5873
5874 // Check that both objects are sequential ascii strings.
5875 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5876
5877 // Compare flat ascii strings.
5878 // Drop arguments from the stack.
5879 __ pop(ecx);
5880 __ add(Operand(esp), Immediate(2 * kPointerSize));
5881 __ push(ecx);
5882 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5883
5884 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5885 // tagged as a small integer.
5886 __ bind(&runtime);
5887 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5888}
5889
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005890
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005891void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5892 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005893 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005894 __ mov(ecx, Operand(edx));
5895 __ or_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005896 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005897
5898 if (GetCondition() == equal) {
5899 // For equality we do not care about the sign of the result.
5900 __ sub(eax, Operand(edx));
5901 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005902 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005903 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005904 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005905 // Correct sign of result in case of overflow.
5906 __ not_(edx);
5907 __ bind(&done);
5908 __ mov(eax, edx);
5909 }
5910 __ ret(0);
5911
5912 __ bind(&miss);
5913 GenerateMiss(masm);
5914}
5915
5916
5917void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5918 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5919
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005920 Label generic_stub;
5921 Label unordered;
5922 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005923 __ mov(ecx, Operand(edx));
5924 __ and_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005925 __ JumpIfSmi(ecx, &generic_stub, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005926
5927 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005928 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005929 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005930 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005931
5932 // Inlining the double comparison and falling back to the general compare
5933 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005934 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005935 CpuFeatures::Scope scope1(SSE2);
5936 CpuFeatures::Scope scope2(CMOV);
5937
5938 // Load left and right operand
5939 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5940 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5941
5942 // Compare operands
5943 __ ucomisd(xmm0, xmm1);
5944
5945 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005946 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005947
5948 // Return a result of -1, 0, or 1, based on EFLAGS.
5949 // Performing mov, because xor would destroy the flag register.
5950 __ mov(eax, 0); // equal
5951 __ mov(ecx, Immediate(Smi::FromInt(1)));
5952 __ cmov(above, eax, Operand(ecx));
5953 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5954 __ cmov(below, eax, Operand(ecx));
5955 __ ret(0);
5956
5957 __ bind(&unordered);
5958 }
5959
5960 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5961 __ bind(&generic_stub);
5962 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5963
5964 __ bind(&miss);
5965 GenerateMiss(masm);
5966}
5967
5968
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005969void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5970 ASSERT(state_ == CompareIC::SYMBOLS);
5971 ASSERT(GetCondition() == equal);
5972
5973 // Registers containing left and right operands respectively.
5974 Register left = edx;
5975 Register right = eax;
5976 Register tmp1 = ecx;
5977 Register tmp2 = ebx;
5978
5979 // Check that both operands are heap objects.
5980 Label miss;
5981 __ mov(tmp1, Operand(left));
5982 STATIC_ASSERT(kSmiTag == 0);
5983 __ and_(tmp1, Operand(right));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005984 __ JumpIfSmi(tmp1, &miss, Label::kNear);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005985
5986 // Check that both operands are symbols.
5987 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5988 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5989 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5990 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5991 STATIC_ASSERT(kSymbolTag != 0);
5992 __ and_(tmp1, Operand(tmp2));
5993 __ test(tmp1, Immediate(kIsSymbolMask));
5994 __ j(zero, &miss, Label::kNear);
5995
5996 // Symbols are compared by identity.
5997 Label done;
5998 __ cmp(left, Operand(right));
5999 // Make sure eax is non-zero. At this point input operands are
6000 // guaranteed to be non-zero.
6001 ASSERT(right.is(eax));
6002 __ j(not_equal, &done, Label::kNear);
6003 STATIC_ASSERT(EQUAL == 0);
6004 STATIC_ASSERT(kSmiTag == 0);
6005 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6006 __ bind(&done);
6007 __ ret(0);
6008
6009 __ bind(&miss);
6010 GenerateMiss(masm);
6011}
6012
6013
lrn@chromium.org1c092762011-05-09 09:42:16 +00006014void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6015 ASSERT(state_ == CompareIC::STRINGS);
6016 ASSERT(GetCondition() == equal);
6017 Label miss;
6018
6019 // Registers containing left and right operands respectively.
6020 Register left = edx;
6021 Register right = eax;
6022 Register tmp1 = ecx;
6023 Register tmp2 = ebx;
6024 Register tmp3 = edi;
6025
6026 // Check that both operands are heap objects.
6027 __ mov(tmp1, Operand(left));
6028 STATIC_ASSERT(kSmiTag == 0);
6029 __ and_(tmp1, Operand(right));
whesse@chromium.org7b260152011-06-20 15:33:18 +00006030 __ JumpIfSmi(tmp1, &miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006031
6032 // Check that both operands are strings. This leaves the instance
6033 // types loaded in tmp1 and tmp2.
6034 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
6035 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
6036 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
6037 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
6038 __ mov(tmp3, tmp1);
6039 STATIC_ASSERT(kNotStringTag != 0);
6040 __ or_(tmp3, Operand(tmp2));
6041 __ test(tmp3, Immediate(kIsNotStringMask));
6042 __ j(not_zero, &miss);
6043
6044 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006045 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006046 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006047 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006048 STATIC_ASSERT(EQUAL == 0);
6049 STATIC_ASSERT(kSmiTag == 0);
6050 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6051 __ ret(0);
6052
6053 // Handle not identical strings.
6054 __ bind(&not_same);
6055
6056 // Check that both strings are symbols. If they are, we're done
6057 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006058 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006059 STATIC_ASSERT(kSymbolTag != 0);
6060 __ and_(tmp1, Operand(tmp2));
6061 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006062 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006063 // Make sure eax is non-zero. At this point input operands are
6064 // guaranteed to be non-zero.
6065 ASSERT(right.is(eax));
6066 __ ret(0);
6067
6068 // Check that both strings are sequential ASCII.
6069 Label runtime;
6070 __ bind(&do_compare);
6071 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
6072
6073 // Compare flat ASCII strings. Returns when done.
6074 StringCompareStub::GenerateFlatAsciiStringEquals(
6075 masm, left, right, tmp1, tmp2);
6076
6077 // Handle more complex cases in runtime.
6078 __ bind(&runtime);
6079 __ pop(tmp1); // Return address.
6080 __ push(left);
6081 __ push(right);
6082 __ push(tmp1);
6083 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6084
6085 __ bind(&miss);
6086 GenerateMiss(masm);
6087}
6088
6089
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006090void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6091 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006092 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006093 __ mov(ecx, Operand(edx));
6094 __ and_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00006095 __ JumpIfSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006096
6097 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006098 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006099 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006100 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006101
6102 ASSERT(GetCondition() == equal);
6103 __ sub(eax, Operand(edx));
6104 __ ret(0);
6105
6106 __ bind(&miss);
6107 GenerateMiss(masm);
6108}
6109
6110
6111void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6112 // Save the registers.
6113 __ pop(ecx);
6114 __ push(edx);
6115 __ push(eax);
6116 __ push(ecx);
6117
6118 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006119 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6120 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006121 __ EnterInternalFrame();
6122 __ push(edx);
6123 __ push(eax);
6124 __ push(Immediate(Smi::FromInt(op_)));
6125 __ CallExternalReference(miss, 3);
6126 __ LeaveInternalFrame();
6127
6128 // Compute the entry point of the rewritten stub.
6129 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6130
6131 // Restore registers.
6132 __ pop(ecx);
6133 __ pop(eax);
6134 __ pop(edx);
6135 __ push(ecx);
6136
6137 // Do a tail call to the rewritten stub.
6138 __ jmp(Operand(edi));
6139}
6140
6141
lrn@chromium.org1c092762011-05-09 09:42:16 +00006142// Helper function used to check that the dictionary doesn't contain
6143// the property. This function may return false negatives, so miss_label
6144// must always call a backup property check that is complete.
6145// This function is safe to call if the receiver has fast properties.
6146// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006147MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
6148 MacroAssembler* masm,
6149 Label* miss,
6150 Label* done,
6151 Register properties,
6152 String* name,
6153 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00006154 ASSERT(name->IsSymbol());
6155
6156 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6157 // not equal to the name and kProbes-th slot is not used (its name is the
6158 // undefined value), it guarantees the hash table doesn't contain the
6159 // property. It's true even if some slots represent deleted properties
6160 // (their names are the null value).
6161 for (int i = 0; i < kInlinedProbes; i++) {
6162 // Compute the masked index: (hash + i + i * i) & mask.
6163 Register index = r0;
6164 // Capacity is smi 2^n.
6165 __ mov(index, FieldOperand(properties, kCapacityOffset));
6166 __ dec(index);
6167 __ and_(Operand(index),
6168 Immediate(Smi::FromInt(name->Hash() +
6169 StringDictionary::GetProbeOffset(i))));
6170
6171 // Scale the index by multiplying by the entry size.
6172 ASSERT(StringDictionary::kEntrySize == 3);
6173 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
6174 Register entity_name = r0;
6175 // Having undefined at this place means the name is not contained.
6176 ASSERT_EQ(kSmiTagSize, 1);
6177 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
6178 kElementsStartOffset - kHeapObjectTag));
6179 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006180 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006181
6182 // Stop if found the property.
6183 __ cmp(entity_name, Handle<String>(name));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006184 __ j(equal, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006185
6186 // Check if the entry name is not a symbol.
6187 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6188 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6189 kIsSymbolMask);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006190 __ j(zero, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006191 }
6192
6193 StringDictionaryLookupStub stub(properties,
6194 r0,
6195 r0,
6196 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6197 __ push(Immediate(Handle<Object>(name)));
6198 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006199 MaybeObject* result = masm->TryCallStub(&stub);
6200 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006201 __ test(r0, Operand(r0));
6202 __ j(not_zero, miss);
6203 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006204 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006205}
6206
6207
6208// Probe the string dictionary in the |elements| register. Jump to the
6209// |done| label if a property with the given name is found leaving the
6210// index into the dictionary in |r0|. Jump to the |miss| label
6211// otherwise.
6212void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6213 Label* miss,
6214 Label* done,
6215 Register elements,
6216 Register name,
6217 Register r0,
6218 Register r1) {
6219 // Assert that name contains a string.
6220 if (FLAG_debug_code) __ AbortIfNotString(name);
6221
6222 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6223 __ shr(r1, kSmiTagSize); // convert smi to int
6224 __ dec(r1);
6225
6226 // Generate an unrolled loop that performs a few probes before
6227 // giving up. Measurements done on Gmail indicate that 2 probes
6228 // cover ~93% of loads from dictionaries.
6229 for (int i = 0; i < kInlinedProbes; i++) {
6230 // Compute the masked index: (hash + i + i * i) & mask.
6231 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6232 __ shr(r0, String::kHashShift);
6233 if (i > 0) {
6234 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6235 }
6236 __ and_(r0, Operand(r1));
6237
6238 // Scale the index by multiplying by the entry size.
6239 ASSERT(StringDictionary::kEntrySize == 3);
6240 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6241
6242 // Check if the key is identical to the name.
6243 __ cmp(name, Operand(elements,
6244 r0,
6245 times_4,
6246 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006247 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006248 }
6249
6250 StringDictionaryLookupStub stub(elements,
6251 r1,
6252 r0,
6253 POSITIVE_LOOKUP);
6254 __ push(name);
6255 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6256 __ shr(r0, String::kHashShift);
6257 __ push(r0);
6258 __ CallStub(&stub);
6259
6260 __ test(r1, Operand(r1));
6261 __ j(zero, miss);
6262 __ jmp(done);
6263}
6264
6265
6266void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6267 // Stack frame on entry:
6268 // esp[0 * kPointerSize]: return address.
6269 // esp[1 * kPointerSize]: key's hash.
6270 // esp[2 * kPointerSize]: key.
6271 // Registers:
6272 // dictionary_: StringDictionary to probe.
6273 // result_: used as scratch.
6274 // index_: will hold an index of entry if lookup is successful.
6275 // might alias with result_.
6276 // Returns:
6277 // result_ is zero if lookup failed, non zero otherwise.
6278
6279 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6280
6281 Register scratch = result_;
6282
6283 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6284 __ dec(scratch);
6285 __ SmiUntag(scratch);
6286 __ push(scratch);
6287
6288 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6289 // not equal to the name and kProbes-th slot is not used (its name is the
6290 // undefined value), it guarantees the hash table doesn't contain the
6291 // property. It's true even if some slots represent deleted properties
6292 // (their names are the null value).
6293 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6294 // Compute the masked index: (hash + i + i * i) & mask.
6295 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6296 if (i > 0) {
6297 __ add(Operand(scratch),
6298 Immediate(StringDictionary::GetProbeOffset(i)));
6299 }
6300 __ and_(scratch, Operand(esp, 0));
6301
6302 // Scale the index by multiplying by the entry size.
6303 ASSERT(StringDictionary::kEntrySize == 3);
6304 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6305
6306 // Having undefined at this place means the name is not contained.
6307 ASSERT_EQ(kSmiTagSize, 1);
6308 __ mov(scratch, Operand(dictionary_,
6309 index_,
6310 times_pointer_size,
6311 kElementsStartOffset - kHeapObjectTag));
6312 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6313 __ j(equal, &not_in_dictionary);
6314
6315 // Stop if found the property.
6316 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6317 __ j(equal, &in_dictionary);
6318
6319 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6320 // If we hit a non symbol key during negative lookup
6321 // we have to bailout as this key might be equal to the
6322 // key we are looking for.
6323
6324 // Check if the entry name is not a symbol.
6325 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6326 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6327 kIsSymbolMask);
6328 __ j(zero, &maybe_in_dictionary);
6329 }
6330 }
6331
6332 __ bind(&maybe_in_dictionary);
6333 // If we are doing negative lookup then probing failure should be
6334 // treated as a lookup success. For positive lookup probing failure
6335 // should be treated as lookup failure.
6336 if (mode_ == POSITIVE_LOOKUP) {
6337 __ mov(result_, Immediate(0));
6338 __ Drop(1);
6339 __ ret(2 * kPointerSize);
6340 }
6341
6342 __ bind(&in_dictionary);
6343 __ mov(result_, Immediate(1));
6344 __ Drop(1);
6345 __ ret(2 * kPointerSize);
6346
6347 __ bind(&not_in_dictionary);
6348 __ mov(result_, Immediate(0));
6349 __ Drop(1);
6350 __ ret(2 * kPointerSize);
6351}
6352
6353
ricow@chromium.org65fae842010-08-25 15:26:24 +00006354#undef __
6355
6356} } // namespace v8::internal
6357
6358#endif // V8_TARGET_ARCH_IA32