blob: e3eb122bc6568641f8df5b2a8c58c20579c8bbba [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);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000139 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), esi);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000140 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
141
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000142 // Copy the global object from the previous context.
143 __ mov(ebx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000144 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
145
146 // Initialize the rest of the slots to undefined.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000147 __ mov(ebx, factory->undefined_value());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000148 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000149 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
150 }
151
152 // Return and remove the on-stack parameter.
153 __ mov(esi, Operand(eax));
154 __ ret(1 * kPointerSize);
155
156 // Need to collect. Call into runtime system.
157 __ bind(&gc);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000158 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000159}
160
161
162void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
163 // Stack layout on entry:
164 //
165 // [esp + kPointerSize]: constant elements.
166 // [esp + (2 * kPointerSize)]: literal index.
167 // [esp + (3 * kPointerSize)]: literals array.
168
169 // All sizes here are multiples of kPointerSize.
170 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
171 int size = JSArray::kSize + elements_size;
172
173 // Load boilerplate object into ecx and check if we need to create a
174 // boilerplate.
175 Label slow_case;
176 __ mov(ecx, Operand(esp, 3 * kPointerSize));
177 __ mov(eax, Operand(esp, 2 * kPointerSize));
178 STATIC_ASSERT(kPointerSize == 4);
179 STATIC_ASSERT(kSmiTagSize == 1);
180 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000181 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
182 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000183 Factory* factory = masm->isolate()->factory();
184 __ cmp(ecx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000185 __ j(equal, &slow_case);
186
187 if (FLAG_debug_code) {
188 const char* message;
189 Handle<Map> expected_map;
190 if (mode_ == CLONE_ELEMENTS) {
191 message = "Expected (writable) fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000192 expected_map = factory->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000193 } else {
194 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
195 message = "Expected copy-on-write fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000196 expected_map = factory->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000197 }
198 __ push(ecx);
199 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
200 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
201 __ Assert(equal, message);
202 __ pop(ecx);
203 }
204
205 // Allocate both the JS array and the elements array in one big
206 // allocation. This avoids multiple limit checks.
207 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
208
209 // Copy the JS array part.
210 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
211 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
212 __ mov(ebx, FieldOperand(ecx, i));
213 __ mov(FieldOperand(eax, i), ebx);
214 }
215 }
216
217 if (length_ > 0) {
218 // Get hold of the elements array of the boilerplate and setup the
219 // elements pointer in the resulting object.
220 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
221 __ lea(edx, Operand(eax, JSArray::kSize));
222 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
223
224 // Copy the elements array.
225 for (int i = 0; i < elements_size; i += kPointerSize) {
226 __ mov(ebx, FieldOperand(ecx, i));
227 __ mov(FieldOperand(edx, i), ebx);
228 }
229 }
230
231 // Return and remove the on-stack parameters.
232 __ ret(3 * kPointerSize);
233
234 __ bind(&slow_case);
235 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
236}
237
238
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000239// The stub returns zero for false, and a non-zero value for true.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000240void ToBooleanStub::Generate(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000241 Label false_result, true_result, not_string;
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000242 Factory* factory = masm->isolate()->factory();
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000243 const Register map = edx;
244
245 __ mov(eax, Operand(esp, 1 * kPointerSize));
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000246
247 // undefined -> false
248 __ cmp(eax, factory->undefined_value());
249 __ j(equal, &false_result);
250
251 // Boolean -> its value
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000252 __ cmp(eax, factory->false_value());
253 __ j(equal, &false_result);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000254 __ cmp(eax, factory->true_value());
255 __ j(equal, &true_result);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000256
257 // Smis: 0 -> false, all other -> true
258 __ test(eax, Operand(eax));
259 __ j(zero, &false_result);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000260 __ JumpIfSmi(eax, &true_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000261
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000262 // 'null' -> false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000263 __ cmp(eax, factory->null_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000264 __ j(equal, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000265
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000266 // Get the map of the heap object.
267 __ mov(map, FieldOperand(eax, HeapObject::kMapOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000268
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000269 // Undetectable -> false.
270 __ test_b(FieldOperand(map, Map::kBitFieldOffset),
ricow@chromium.org65fae842010-08-25 15:26:24 +0000271 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
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000274 // JavaScript object -> true.
275 __ CmpInstanceType(map, 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
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000278 // String value -> false iff empty.
279 __ CmpInstanceType(map, 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 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000282 __ j(zero, &false_result, Label::kNear);
283 __ jmp(&true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000284
285 __ bind(&not_string);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000286 // HeapNumber -> false iff +0, -0, or NaN.
287 __ cmp(map, factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000288 __ j(not_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000289 __ fldz();
290 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
291 __ FCmp();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000292 __ j(zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000293 // Fall through to |true_result|.
294
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000295 // Return 1/0 for true/false in tos_.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000296 __ bind(&true_result);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000297 __ mov(tos_, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000298 __ ret(1 * kPointerSize);
299 __ bind(&false_result);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +0000300 __ mov(tos_, 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000301 __ ret(1 * kPointerSize);
302}
303
304
ricow@chromium.org65fae842010-08-25 15:26:24 +0000305class FloatingPointHelper : public AllStatic {
306 public:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000307 enum ArgLocation {
308 ARGS_ON_STACK,
309 ARGS_IN_REGISTERS
310 };
311
312 // Code pattern for loading a floating point value. Input value must
313 // be either a smi or a heap number object (fp value). Requirements:
314 // operand in register number. Returns operand as floating point number
315 // on FPU stack.
316 static void LoadFloatOperand(MacroAssembler* masm, Register number);
317
318 // Code pattern for loading floating point values. Input values must
319 // be either smi or heap number objects (fp values). Requirements:
320 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
321 // Returns operands as floating point numbers on FPU stack.
322 static void LoadFloatOperands(MacroAssembler* masm,
323 Register scratch,
324 ArgLocation arg_location = ARGS_ON_STACK);
325
326 // Similar to LoadFloatOperand but assumes that both operands are smis.
327 // Expects operands in edx, eax.
328 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
329
330 // Test if operands are smi or number objects (fp). Requirements:
331 // operand_1 in eax, operand_2 in edx; falls through on float
332 // operands, jumps to the non_float label otherwise.
333 static void CheckFloatOperands(MacroAssembler* masm,
334 Label* non_float,
335 Register scratch);
336
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000337 // Checks that the two floating point numbers on top of the FPU stack
338 // have int32 values.
339 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
340 Label* non_int32);
341
ricow@chromium.org65fae842010-08-25 15:26:24 +0000342 // Takes the operands in edx and eax and loads them as integers in eax
343 // and ecx.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000344 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
345 bool use_sse3,
346 Label* operand_conversion_failure);
347
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000348 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
349 // operands are pushed on the stack, and that their conversions to int32
350 // are in eax and ecx. Checks that the original numbers were in the int32
351 // range.
352 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
353 bool use_sse3,
354 Label* not_int32);
355
356 // Assumes that operands are smis or heap numbers and loads them
357 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000358 // Leaves operands unchanged.
359 static void LoadSSE2Operands(MacroAssembler* masm);
360
361 // Test if operands are numbers (smi or HeapNumber objects), and load
362 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
363 // either operand is not a number. Operands are in edx and eax.
364 // Leaves operands unchanged.
365 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
366
367 // Similar to LoadSSE2Operands but assumes that both operands are smis.
368 // Expects operands in edx, eax.
369 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000370
371 // Checks that the two floating point numbers loaded into xmm0 and xmm1
372 // have int32 values.
373 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
374 Label* non_int32,
375 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000376};
377
378
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000379// Get the integer part of a heap number. Surprisingly, all this bit twiddling
380// is faster than using the built-in instructions on floating point registers.
381// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
382// trashed registers.
383static void IntegerConvert(MacroAssembler* masm,
384 Register source,
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000385 bool use_sse3,
386 Label* conversion_failure) {
387 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
388 Label done, right_exponent, normal_exponent;
389 Register scratch = ebx;
390 Register scratch2 = edi;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000391 // Get exponent word.
392 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
393 // Get exponent alone in scratch2.
394 __ mov(scratch2, scratch);
395 __ and_(scratch2, HeapNumber::kExponentMask);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000396 if (use_sse3) {
397 CpuFeatures::Scope scope(SSE3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000398 // Check whether the exponent is too big for a 64 bit signed integer.
399 static const uint32_t kTooBigExponent =
400 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
401 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
402 __ j(greater_equal, conversion_failure);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000403 // Load x87 register with heap number.
404 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
405 // Reserve space for 64 bit answer.
406 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
407 // Do conversion, which cannot fail because we checked the exponent.
408 __ fisttp_d(Operand(esp, 0));
409 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
410 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
411 } else {
412 // Load ecx with zero. We use this either for the final shift or
413 // for the answer.
414 __ xor_(ecx, Operand(ecx));
415 // Check whether the exponent matches a 32 bit signed int that cannot be
416 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
417 // exponent is 30 (biased). This is the exponent that we are fastest at and
418 // also the highest exponent we can handle here.
419 const uint32_t non_smi_exponent =
420 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
421 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
422 // If we have a match of the int32-but-not-Smi exponent then skip some
423 // logic.
424 __ j(equal, &right_exponent);
425 // If the exponent is higher than that then go to slow case. This catches
426 // numbers that don't fit in a signed int32, infinities and NaNs.
427 __ j(less, &normal_exponent);
428
429 {
430 // Handle a big exponent. The only reason we have this code is that the
431 // >>> operator has a tendency to generate numbers with an exponent of 31.
432 const uint32_t big_non_smi_exponent =
433 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
434 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
435 __ j(not_equal, conversion_failure);
436 // We have the big exponent, typically from >>>. This means the number is
437 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
438 __ mov(scratch2, scratch);
439 __ and_(scratch2, HeapNumber::kMantissaMask);
440 // Put back the implicit 1.
441 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
442 // Shift up the mantissa bits to take up the space the exponent used to
443 // take. We just orred in the implicit bit so that took care of one and
444 // we want to use the full unsigned range so we subtract 1 bit from the
445 // shift distance.
446 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
447 __ shl(scratch2, big_shift_distance);
448 // Get the second half of the double.
449 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
450 // Shift down 21 bits to get the most significant 11 bits or the low
451 // mantissa word.
452 __ shr(ecx, 32 - big_shift_distance);
453 __ or_(ecx, Operand(scratch2));
454 // We have the answer in ecx, but we may need to negate it.
455 __ test(scratch, Operand(scratch));
456 __ j(positive, &done);
457 __ neg(ecx);
458 __ jmp(&done);
459 }
460
461 __ bind(&normal_exponent);
462 // Exponent word in scratch, exponent part of exponent word in scratch2.
463 // Zero in ecx.
464 // We know the exponent is smaller than 30 (biased). If it is less than
465 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
466 // it rounds to zero.
467 const uint32_t zero_exponent =
468 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
469 __ sub(Operand(scratch2), Immediate(zero_exponent));
470 // ecx already has a Smi zero.
471 __ j(less, &done);
472
473 // We have a shifted exponent between 0 and 30 in scratch2.
474 __ shr(scratch2, HeapNumber::kExponentShift);
475 __ mov(ecx, Immediate(30));
476 __ sub(ecx, Operand(scratch2));
477
478 __ bind(&right_exponent);
479 // Here ecx is the shift, scratch is the exponent word.
480 // Get the top bits of the mantissa.
481 __ and_(scratch, HeapNumber::kMantissaMask);
482 // Put back the implicit 1.
483 __ or_(scratch, 1 << HeapNumber::kExponentShift);
484 // Shift up the mantissa bits to take up the space the exponent used to
485 // take. We have kExponentShift + 1 significant bits int he low end of the
486 // word. Shift them to the top bits.
487 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
488 __ shl(scratch, shift_distance);
489 // Get the second half of the double. For some exponents we don't
490 // actually need this because the bits get shifted out again, but
491 // it's probably slower to test than just to do it.
492 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
493 // Shift down 22 bits to get the most significant 10 bits or the low
494 // mantissa word.
495 __ shr(scratch2, 32 - shift_distance);
496 __ or_(scratch2, Operand(scratch));
497 // Move down according to the exponent.
498 __ shr_cl(scratch2);
499 // Now the unsigned answer is in scratch2. We need to move it to ecx and
500 // we may need to fix the sign.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000501 Label negative;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000502 __ xor_(ecx, Operand(ecx));
503 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000504 __ j(greater, &negative, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000505 __ mov(ecx, scratch2);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000506 __ jmp(&done, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000507 __ bind(&negative);
508 __ sub(ecx, Operand(scratch2));
509 __ bind(&done);
510 }
511}
512
513
danno@chromium.org40cb8782011-05-25 07:58:50 +0000514const char* UnaryOpStub::GetName() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000515 if (name_ != NULL) return name_;
516 const int kMaxNameLength = 100;
517 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
518 kMaxNameLength);
519 if (name_ == NULL) return "OOM";
520 const char* op_name = Token::Name(op_);
521 const char* overwrite_name = NULL; // Make g++ happy.
522 switch (mode_) {
523 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
524 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
525 }
526
527 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000528 "UnaryOpStub_%s_%s_%s",
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000529 op_name,
530 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000531 UnaryOpIC::GetName(operand_type_));
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000532 return name_;
533}
534
535
536// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000537void UnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000538 switch (operand_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000539 case UnaryOpIC::UNINITIALIZED:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000540 GenerateTypeTransition(masm);
541 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000542 case UnaryOpIC::SMI:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000543 GenerateSmiStub(masm);
544 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000545 case UnaryOpIC::HEAP_NUMBER:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000546 GenerateHeapNumberStub(masm);
547 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000548 case UnaryOpIC::GENERIC:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000549 GenerateGenericStub(masm);
550 break;
551 }
552}
553
554
danno@chromium.org40cb8782011-05-25 07:58:50 +0000555void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000556 __ pop(ecx); // Save return address.
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000557
558 __ push(eax); // the operand
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000559 __ push(Immediate(Smi::FromInt(op_)));
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000560 __ push(Immediate(Smi::FromInt(mode_)));
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000561 __ push(Immediate(Smi::FromInt(operand_type_)));
562
563 __ push(ecx); // Push return address.
564
565 // Patch the caller to an appropriate specialized stub and return the
566 // operation result to the caller of the stub.
567 __ TailCallExternalReference(
ricow@chromium.org4f693d62011-07-04 14:01:31 +0000568 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000569}
570
571
572// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000573void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000574 switch (op_) {
575 case Token::SUB:
576 GenerateSmiStubSub(masm);
577 break;
578 case Token::BIT_NOT:
579 GenerateSmiStubBitNot(masm);
580 break;
581 default:
582 UNREACHABLE();
583 }
584}
585
586
danno@chromium.org40cb8782011-05-25 07:58:50 +0000587void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000588 Label non_smi, undo, slow;
589 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
590 Label::kNear, Label::kNear, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000591 __ bind(&undo);
592 GenerateSmiCodeUndo(masm);
593 __ bind(&non_smi);
594 __ bind(&slow);
595 GenerateTypeTransition(masm);
596}
597
598
danno@chromium.org40cb8782011-05-25 07:58:50 +0000599void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000600 Label non_smi;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000601 GenerateSmiCodeBitNot(masm, &non_smi);
602 __ bind(&non_smi);
603 GenerateTypeTransition(masm);
604}
605
606
danno@chromium.org40cb8782011-05-25 07:58:50 +0000607void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
608 Label* non_smi,
609 Label* undo,
610 Label* slow,
611 Label::Distance non_smi_near,
612 Label::Distance undo_near,
613 Label::Distance slow_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000614 // Check whether the value is a smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000615 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000616
617 // We can't handle -0 with smis, so use a type transition for that case.
618 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000619 __ j(zero, slow, slow_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000620
621 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
622 __ mov(edx, Operand(eax));
623 __ Set(eax, Immediate(0));
624 __ sub(eax, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000625 __ j(overflow, undo, undo_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000626 __ ret(0);
627}
628
629
danno@chromium.org40cb8782011-05-25 07:58:50 +0000630void UnaryOpStub::GenerateSmiCodeBitNot(
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000631 MacroAssembler* masm,
632 Label* non_smi,
633 Label::Distance non_smi_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000634 // Check whether the value is a smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000635 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000636
637 // Flip bits and revert inverted smi-tag.
638 __ not_(eax);
639 __ and_(eax, ~kSmiTagMask);
640 __ ret(0);
641}
642
643
danno@chromium.org40cb8782011-05-25 07:58:50 +0000644void UnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000645 __ mov(eax, Operand(edx));
646}
647
648
649// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000650void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000651 switch (op_) {
652 case Token::SUB:
653 GenerateHeapNumberStubSub(masm);
654 break;
655 case Token::BIT_NOT:
656 GenerateHeapNumberStubBitNot(masm);
657 break;
658 default:
659 UNREACHABLE();
660 }
661}
662
663
danno@chromium.org40cb8782011-05-25 07:58:50 +0000664void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000665 Label non_smi, undo, slow, call_builtin;
666 GenerateSmiCodeSub(masm, &non_smi, &undo, &call_builtin, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000667 __ bind(&non_smi);
668 GenerateHeapNumberCodeSub(masm, &slow);
669 __ bind(&undo);
670 GenerateSmiCodeUndo(masm);
671 __ bind(&slow);
672 GenerateTypeTransition(masm);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000673 __ bind(&call_builtin);
674 GenerateGenericCodeFallback(masm);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000675}
676
677
danno@chromium.org40cb8782011-05-25 07:58:50 +0000678void UnaryOpStub::GenerateHeapNumberStubBitNot(
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000679 MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000680 Label non_smi, slow;
681 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000682 __ bind(&non_smi);
683 GenerateHeapNumberCodeBitNot(masm, &slow);
684 __ bind(&slow);
685 GenerateTypeTransition(masm);
686}
687
688
danno@chromium.org40cb8782011-05-25 07:58:50 +0000689void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
690 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000691 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
692 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
693 __ j(not_equal, slow);
694
695 if (mode_ == UNARY_OVERWRITE) {
696 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
697 Immediate(HeapNumber::kSignMask)); // Flip sign.
698 } else {
699 __ mov(edx, Operand(eax));
700 // edx: operand
701
702 Label slow_allocate_heapnumber, heapnumber_allocated;
703 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
704 __ jmp(&heapnumber_allocated);
705
706 __ bind(&slow_allocate_heapnumber);
707 __ EnterInternalFrame();
708 __ push(edx);
709 __ CallRuntime(Runtime::kNumberAlloc, 0);
710 __ pop(edx);
711 __ LeaveInternalFrame();
712
713 __ bind(&heapnumber_allocated);
714 // eax: allocated 'empty' number
715 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
716 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
717 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
718 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
719 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
720 }
721 __ ret(0);
722}
723
724
danno@chromium.org40cb8782011-05-25 07:58:50 +0000725void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
726 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000727 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
728 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
729 __ j(not_equal, slow);
730
731 // Convert the heap number in eax to an untagged integer in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000732 IntegerConvert(masm, eax, CpuFeatures::IsSupported(SSE3), slow);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000733
734 // Do the bitwise operation and check if the result fits in a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000735 Label try_float;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000736 __ not_(ecx);
737 __ cmp(ecx, 0xc0000000);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000738 __ j(sign, &try_float, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000739
740 // Tag the result as a smi and we're done.
741 STATIC_ASSERT(kSmiTagSize == 1);
742 __ lea(eax, Operand(ecx, times_2, kSmiTag));
743 __ ret(0);
744
745 // Try to store the result in a heap number.
746 __ bind(&try_float);
747 if (mode_ == UNARY_NO_OVERWRITE) {
748 Label slow_allocate_heapnumber, heapnumber_allocated;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000749 __ mov(ebx, eax);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000750 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
751 __ jmp(&heapnumber_allocated);
752
753 __ bind(&slow_allocate_heapnumber);
754 __ EnterInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000755 // Push the original HeapNumber on the stack. The integer value can't
756 // be stored since it's untagged and not in the smi range (so we can't
757 // smi-tag it). We'll recalculate the value after the GC instead.
758 __ push(ebx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000759 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000760 // New HeapNumber is in eax.
761 __ pop(edx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000762 __ LeaveInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000763 // IntegerConvert uses ebx and edi as scratch registers.
764 // This conversion won't go slow-case.
765 IntegerConvert(masm, edx, CpuFeatures::IsSupported(SSE3), slow);
766 __ not_(ecx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000767
768 __ bind(&heapnumber_allocated);
769 }
770 if (CpuFeatures::IsSupported(SSE2)) {
771 CpuFeatures::Scope use_sse2(SSE2);
772 __ cvtsi2sd(xmm0, Operand(ecx));
773 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
774 } else {
775 __ push(ecx);
776 __ fild_s(Operand(esp, 0));
777 __ pop(ecx);
778 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
779 }
780 __ ret(0);
781}
782
783
784// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000785void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000786 switch (op_) {
787 case Token::SUB:
788 GenerateGenericStubSub(masm);
789 break;
790 case Token::BIT_NOT:
791 GenerateGenericStubBitNot(masm);
792 break;
793 default:
794 UNREACHABLE();
795 }
796}
797
798
danno@chromium.org40cb8782011-05-25 07:58:50 +0000799void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000800 Label non_smi, undo, slow;
801 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000802 __ bind(&non_smi);
803 GenerateHeapNumberCodeSub(masm, &slow);
804 __ bind(&undo);
805 GenerateSmiCodeUndo(masm);
806 __ bind(&slow);
807 GenerateGenericCodeFallback(masm);
808}
809
810
danno@chromium.org40cb8782011-05-25 07:58:50 +0000811void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000812 Label non_smi, slow;
813 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000814 __ bind(&non_smi);
815 GenerateHeapNumberCodeBitNot(masm, &slow);
816 __ bind(&slow);
817 GenerateGenericCodeFallback(masm);
818}
819
820
danno@chromium.org40cb8782011-05-25 07:58:50 +0000821void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000822 // Handle the slow case by jumping to the corresponding JavaScript builtin.
823 __ pop(ecx); // pop return address.
824 __ push(eax);
825 __ push(ecx); // push return address
826 switch (op_) {
827 case Token::SUB:
828 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
829 break;
830 case Token::BIT_NOT:
831 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
832 break;
833 default:
834 UNREACHABLE();
835 }
836}
837
838
danno@chromium.org40cb8782011-05-25 07:58:50 +0000839void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000840 __ pop(ecx); // Save return address.
841 __ push(edx);
842 __ push(eax);
843 // Left and right arguments are now on top.
844 // Push this stub's key. Although the operation and the type info are
845 // encoded into the key, the encoding is opaque, so push them too.
846 __ push(Immediate(Smi::FromInt(MinorKey())));
847 __ push(Immediate(Smi::FromInt(op_)));
848 __ push(Immediate(Smi::FromInt(operands_type_)));
849
850 __ push(ecx); // Push return address.
851
852 // Patch the caller to an appropriate specialized stub and return the
853 // operation result to the caller of the stub.
854 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000855 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000856 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000857 5,
858 1);
859}
860
861
862// Prepare for a type transition runtime call when the args are already on
863// the stack, under the return address.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000864void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000865 __ pop(ecx); // Save return address.
866 // Left and right arguments are already on top of the stack.
867 // Push this stub's key. Although the operation and the type info are
868 // encoded into the key, the encoding is opaque, so push them too.
869 __ push(Immediate(Smi::FromInt(MinorKey())));
870 __ push(Immediate(Smi::FromInt(op_)));
871 __ push(Immediate(Smi::FromInt(operands_type_)));
872
873 __ push(ecx); // Push return address.
874
875 // Patch the caller to an appropriate specialized stub and return the
876 // operation result to the caller of the stub.
877 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000878 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000879 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000880 5,
881 1);
882}
883
884
danno@chromium.org40cb8782011-05-25 07:58:50 +0000885void BinaryOpStub::Generate(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000886 switch (operands_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000887 case BinaryOpIC::UNINITIALIZED:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000888 GenerateTypeTransition(masm);
889 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000890 case BinaryOpIC::SMI:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000891 GenerateSmiStub(masm);
892 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000893 case BinaryOpIC::INT32:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000894 GenerateInt32Stub(masm);
895 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000896 case BinaryOpIC::HEAP_NUMBER:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000897 GenerateHeapNumberStub(masm);
898 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000899 case BinaryOpIC::ODDBALL:
lrn@chromium.org7516f052011-03-30 08:52:27 +0000900 GenerateOddballStub(masm);
901 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000902 case BinaryOpIC::BOTH_STRING:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000903 GenerateBothStringStub(masm);
904 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000905 case BinaryOpIC::STRING:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000906 GenerateStringStub(masm);
907 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000908 case BinaryOpIC::GENERIC:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000909 GenerateGeneric(masm);
910 break;
911 default:
912 UNREACHABLE();
913 }
914}
915
916
danno@chromium.org40cb8782011-05-25 07:58:50 +0000917const char* BinaryOpStub::GetName() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000918 if (name_ != NULL) return name_;
919 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000920 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
921 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000922 if (name_ == NULL) return "OOM";
923 const char* op_name = Token::Name(op_);
924 const char* overwrite_name;
925 switch (mode_) {
926 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
927 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
928 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
929 default: overwrite_name = "UnknownOverwrite"; break;
930 }
931
932 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000933 "BinaryOpStub_%s_%s_%s",
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000934 op_name,
935 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000936 BinaryOpIC::GetName(operands_type_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000937 return name_;
938}
939
940
danno@chromium.org40cb8782011-05-25 07:58:50 +0000941void BinaryOpStub::GenerateSmiCode(
942 MacroAssembler* masm,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000943 Label* slow,
944 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
945 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
946 // dividend in eax and edx free for the division. Use eax, ebx for those.
947 Comment load_comment(masm, "-- Load arguments");
948 Register left = edx;
949 Register right = eax;
950 if (op_ == Token::DIV || op_ == Token::MOD) {
951 left = eax;
952 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000953 __ mov(ebx, eax);
954 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000955 }
956
957
958 // 2. Prepare the smi check of both operands by oring them together.
959 Comment smi_check_comment(masm, "-- Smi check arguments");
960 Label not_smis;
961 Register combined = ecx;
962 ASSERT(!left.is(combined) && !right.is(combined));
963 switch (op_) {
964 case Token::BIT_OR:
965 // Perform the operation into eax and smi check the result. Preserve
966 // eax in case the result is not a smi.
967 ASSERT(!left.is(ecx) && !right.is(ecx));
968 __ mov(ecx, right);
969 __ or_(right, Operand(left)); // Bitwise or is commutative.
970 combined = right;
971 break;
972
973 case Token::BIT_XOR:
974 case Token::BIT_AND:
975 case Token::ADD:
976 case Token::SUB:
977 case Token::MUL:
978 case Token::DIV:
979 case Token::MOD:
980 __ mov(combined, right);
981 __ or_(combined, Operand(left));
982 break;
983
984 case Token::SHL:
985 case Token::SAR:
986 case Token::SHR:
987 // Move the right operand into ecx for the shift operation, use eax
988 // for the smi check register.
989 ASSERT(!left.is(ecx) && !right.is(ecx));
990 __ mov(ecx, right);
991 __ or_(right, Operand(left));
992 combined = right;
993 break;
994
995 default:
996 break;
997 }
998
999 // 3. Perform the smi check of the operands.
1000 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001001 __ JumpIfNotSmi(combined, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002
1003 // 4. Operands are both smis, perform the operation leaving the result in
1004 // eax and check the result if necessary.
1005 Comment perform_smi(masm, "-- Perform smi operation");
1006 Label use_fp_on_smis;
1007 switch (op_) {
1008 case Token::BIT_OR:
1009 // Nothing to do.
1010 break;
1011
1012 case Token::BIT_XOR:
1013 ASSERT(right.is(eax));
1014 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1015 break;
1016
1017 case Token::BIT_AND:
1018 ASSERT(right.is(eax));
1019 __ and_(right, Operand(left)); // Bitwise and is commutative.
1020 break;
1021
1022 case Token::SHL:
1023 // Remove tags from operands (but keep sign).
1024 __ SmiUntag(left);
1025 __ SmiUntag(ecx);
1026 // Perform the operation.
1027 __ shl_cl(left);
1028 // Check that the *signed* result fits in a smi.
1029 __ cmp(left, 0xc0000000);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001030 __ j(sign, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001031 // Tag the result and store it in register eax.
1032 __ SmiTag(left);
1033 __ mov(eax, left);
1034 break;
1035
1036 case Token::SAR:
1037 // Remove tags from operands (but keep sign).
1038 __ SmiUntag(left);
1039 __ SmiUntag(ecx);
1040 // Perform the operation.
1041 __ sar_cl(left);
1042 // Tag the result and store it in register eax.
1043 __ SmiTag(left);
1044 __ mov(eax, left);
1045 break;
1046
1047 case Token::SHR:
1048 // Remove tags from operands (but keep sign).
1049 __ SmiUntag(left);
1050 __ SmiUntag(ecx);
1051 // Perform the operation.
1052 __ shr_cl(left);
1053 // Check that the *unsigned* result fits in a smi.
1054 // Neither of the two high-order bits can be set:
1055 // - 0x80000000: high bit would be lost when smi tagging.
1056 // - 0x40000000: this number would convert to negative when
1057 // Smi tagging these two cases can only happen with shifts
1058 // by 0 or 1 when handed a valid smi.
1059 __ test(left, Immediate(0xc0000000));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001060 __ j(not_zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001061 // Tag the result and store it in register eax.
1062 __ SmiTag(left);
1063 __ mov(eax, left);
1064 break;
1065
1066 case Token::ADD:
1067 ASSERT(right.is(eax));
1068 __ add(right, Operand(left)); // Addition is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001069 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001070 break;
1071
1072 case Token::SUB:
1073 __ sub(left, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001074 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001075 __ mov(eax, left);
1076 break;
1077
1078 case Token::MUL:
1079 // If the smi tag is 0 we can just leave the tag on one operand.
1080 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1081 // We can't revert the multiplication if the result is not a smi
1082 // so save the right operand.
1083 __ mov(ebx, right);
1084 // Remove tag from one of the operands (but keep sign).
1085 __ SmiUntag(right);
1086 // Do multiplication.
1087 __ imul(right, Operand(left)); // Multiplication is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001088 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001089 // Check for negative zero result. Use combined = left | right.
1090 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1091 break;
1092
1093 case Token::DIV:
1094 // We can't revert the division if the result is not a smi so
1095 // save the left operand.
1096 __ mov(edi, left);
1097 // Check for 0 divisor.
1098 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001099 __ j(zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001100 // Sign extend left into edx:eax.
1101 ASSERT(left.is(eax));
1102 __ cdq();
1103 // Divide edx:eax by right.
1104 __ idiv(right);
1105 // Check for the corner case of dividing the most negative smi by
1106 // -1. We cannot use the overflow flag, since it is not set by idiv
1107 // instruction.
1108 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1109 __ cmp(eax, 0x40000000);
1110 __ j(equal, &use_fp_on_smis);
1111 // Check for negative zero result. Use combined = left | right.
1112 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1113 // Check that the remainder is zero.
1114 __ test(edx, Operand(edx));
1115 __ j(not_zero, &use_fp_on_smis);
1116 // Tag the result and store it in register eax.
1117 __ SmiTag(eax);
1118 break;
1119
1120 case Token::MOD:
1121 // Check for 0 divisor.
1122 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001123 __ j(zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001124
1125 // Sign extend left into edx:eax.
1126 ASSERT(left.is(eax));
1127 __ cdq();
1128 // Divide edx:eax by right.
1129 __ idiv(right);
1130 // Check for negative zero result. Use combined = left | right.
1131 __ NegativeZeroTest(edx, combined, slow);
1132 // Move remainder to register eax.
1133 __ mov(eax, edx);
1134 break;
1135
1136 default:
1137 UNREACHABLE();
1138 }
1139
1140 // 5. Emit return of result in eax. Some operations have registers pushed.
1141 switch (op_) {
1142 case Token::ADD:
1143 case Token::SUB:
1144 case Token::MUL:
1145 case Token::DIV:
1146 __ ret(0);
1147 break;
1148 case Token::MOD:
1149 case Token::BIT_OR:
1150 case Token::BIT_AND:
1151 case Token::BIT_XOR:
1152 case Token::SAR:
1153 case Token::SHL:
1154 case Token::SHR:
1155 __ ret(2 * kPointerSize);
1156 break;
1157 default:
1158 UNREACHABLE();
1159 }
1160
1161 // 6. For some operations emit inline code to perform floating point
1162 // operations on known smis (e.g., if the result of the operation
1163 // overflowed the smi range).
1164 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1165 __ bind(&use_fp_on_smis);
1166 switch (op_) {
1167 // Undo the effects of some operations, and some register moves.
1168 case Token::SHL:
1169 // The arguments are saved on the stack, and only used from there.
1170 break;
1171 case Token::ADD:
1172 // Revert right = right + left.
1173 __ sub(right, Operand(left));
1174 break;
1175 case Token::SUB:
1176 // Revert left = left - right.
1177 __ add(left, Operand(right));
1178 break;
1179 case Token::MUL:
1180 // Right was clobbered but a copy is in ebx.
1181 __ mov(right, ebx);
1182 break;
1183 case Token::DIV:
1184 // Left was clobbered but a copy is in edi. Right is in ebx for
1185 // division. They should be in eax, ebx for jump to not_smi.
1186 __ mov(eax, edi);
1187 break;
1188 default:
1189 // No other operators jump to use_fp_on_smis.
1190 break;
1191 }
1192 __ jmp(&not_smis);
1193 } else {
1194 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1195 switch (op_) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001196 case Token::SHL:
1197 case Token::SHR: {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001198 Comment perform_float(masm, "-- Perform float operation on smis");
1199 __ bind(&use_fp_on_smis);
1200 // Result we want is in left == edx, so we can put the allocated heap
1201 // number in eax.
1202 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1203 // Store the result in the HeapNumber and return.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001204 // It's OK to overwrite the arguments on the stack because we
1205 // are about to return.
1206 if (op_ == Token::SHR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001207 __ mov(Operand(esp, 1 * kPointerSize), left);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001208 __ mov(Operand(esp, 2 * kPointerSize), Immediate(0));
1209 __ fild_d(Operand(esp, 1 * kPointerSize));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001210 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001211 } else {
1212 ASSERT_EQ(Token::SHL, op_);
1213 if (CpuFeatures::IsSupported(SSE2)) {
1214 CpuFeatures::Scope use_sse2(SSE2);
1215 __ cvtsi2sd(xmm0, Operand(left));
1216 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1217 } else {
1218 __ mov(Operand(esp, 1 * kPointerSize), left);
1219 __ fild_s(Operand(esp, 1 * kPointerSize));
1220 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1221 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001222 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001223 __ ret(2 * kPointerSize);
1224 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001225 }
1226
1227 case Token::ADD:
1228 case Token::SUB:
1229 case Token::MUL:
1230 case Token::DIV: {
1231 Comment perform_float(masm, "-- Perform float operation on smis");
1232 __ bind(&use_fp_on_smis);
1233 // Restore arguments to edx, eax.
1234 switch (op_) {
1235 case Token::ADD:
1236 // Revert right = right + left.
1237 __ sub(right, Operand(left));
1238 break;
1239 case Token::SUB:
1240 // Revert left = left - right.
1241 __ add(left, Operand(right));
1242 break;
1243 case Token::MUL:
1244 // Right was clobbered but a copy is in ebx.
1245 __ mov(right, ebx);
1246 break;
1247 case Token::DIV:
1248 // Left was clobbered but a copy is in edi. Right is in ebx for
1249 // division.
1250 __ mov(edx, edi);
1251 __ mov(eax, right);
1252 break;
1253 default: UNREACHABLE();
1254 break;
1255 }
1256 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001257 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001258 CpuFeatures::Scope use_sse2(SSE2);
1259 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1260 switch (op_) {
1261 case Token::ADD: __ addsd(xmm0, xmm1); break;
1262 case Token::SUB: __ subsd(xmm0, xmm1); break;
1263 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1264 case Token::DIV: __ divsd(xmm0, xmm1); break;
1265 default: UNREACHABLE();
1266 }
1267 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1268 } else { // SSE2 not available, use FPU.
1269 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1270 switch (op_) {
1271 case Token::ADD: __ faddp(1); break;
1272 case Token::SUB: __ fsubp(1); break;
1273 case Token::MUL: __ fmulp(1); break;
1274 case Token::DIV: __ fdivp(1); break;
1275 default: UNREACHABLE();
1276 }
1277 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1278 }
1279 __ mov(eax, ecx);
1280 __ ret(0);
1281 break;
1282 }
1283
1284 default:
1285 break;
1286 }
1287 }
1288
1289 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1290 // edx and eax.
1291 Comment done_comment(masm, "-- Enter non-smi code");
1292 __ bind(&not_smis);
1293 switch (op_) {
1294 case Token::BIT_OR:
1295 case Token::SHL:
1296 case Token::SAR:
1297 case Token::SHR:
1298 // Right operand is saved in ecx and eax was destroyed by the smi
1299 // check.
1300 __ mov(eax, ecx);
1301 break;
1302
1303 case Token::DIV:
1304 case Token::MOD:
1305 // Operands are in eax, ebx at this point.
1306 __ mov(edx, eax);
1307 __ mov(eax, ebx);
1308 break;
1309
1310 default:
1311 break;
1312 }
1313}
1314
1315
danno@chromium.org40cb8782011-05-25 07:58:50 +00001316void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001317 Label call_runtime;
1318
1319 switch (op_) {
1320 case Token::ADD:
1321 case Token::SUB:
1322 case Token::MUL:
1323 case Token::DIV:
1324 break;
1325 case Token::MOD:
1326 case Token::BIT_OR:
1327 case Token::BIT_AND:
1328 case Token::BIT_XOR:
1329 case Token::SAR:
1330 case Token::SHL:
1331 case Token::SHR:
1332 GenerateRegisterArgsPush(masm);
1333 break;
1334 default:
1335 UNREACHABLE();
1336 }
1337
danno@chromium.org40cb8782011-05-25 07:58:50 +00001338 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1339 result_type_ == BinaryOpIC::SMI) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001340 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1341 } else {
1342 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1343 }
1344 __ bind(&call_runtime);
1345 switch (op_) {
1346 case Token::ADD:
1347 case Token::SUB:
1348 case Token::MUL:
1349 case Token::DIV:
1350 GenerateTypeTransition(masm);
1351 break;
1352 case Token::MOD:
1353 case Token::BIT_OR:
1354 case Token::BIT_AND:
1355 case Token::BIT_XOR:
1356 case Token::SAR:
1357 case Token::SHL:
1358 case Token::SHR:
1359 GenerateTypeTransitionWithSavedArgs(masm);
1360 break;
1361 default:
1362 UNREACHABLE();
1363 }
1364}
1365
1366
danno@chromium.org40cb8782011-05-25 07:58:50 +00001367void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1368 ASSERT(operands_type_ == BinaryOpIC::STRING);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001369 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001370 // Try to add arguments as strings, otherwise, transition to the generic
danno@chromium.org40cb8782011-05-25 07:58:50 +00001371 // BinaryOpIC type.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001372 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001373 GenerateTypeTransition(masm);
1374}
1375
1376
danno@chromium.org40cb8782011-05-25 07:58:50 +00001377void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001378 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001379 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001380 ASSERT(op_ == Token::ADD);
1381 // If both arguments are strings, call the string add stub.
1382 // Otherwise, do a transition.
1383
1384 // Registers containing left and right operands respectively.
1385 Register left = edx;
1386 Register right = eax;
1387
1388 // Test if left operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001389 __ JumpIfSmi(left, &call_runtime);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001390 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1391 __ j(above_equal, &call_runtime);
1392
1393 // Test if right operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001394 __ JumpIfSmi(right, &call_runtime);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001395 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1396 __ j(above_equal, &call_runtime);
1397
1398 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1399 GenerateRegisterArgsPush(masm);
1400 __ TailCallStub(&string_add_stub);
1401
1402 __ bind(&call_runtime);
1403 GenerateTypeTransition(masm);
1404}
1405
1406
danno@chromium.org40cb8782011-05-25 07:58:50 +00001407void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001408 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001409 ASSERT(operands_type_ == BinaryOpIC::INT32);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001410
1411 // Floating point case.
1412 switch (op_) {
1413 case Token::ADD:
1414 case Token::SUB:
1415 case Token::MUL:
1416 case Token::DIV: {
1417 Label not_floats;
1418 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001419 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001420 CpuFeatures::Scope use_sse2(SSE2);
1421 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1422 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1423 switch (op_) {
1424 case Token::ADD: __ addsd(xmm0, xmm1); break;
1425 case Token::SUB: __ subsd(xmm0, xmm1); break;
1426 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1427 case Token::DIV: __ divsd(xmm0, xmm1); break;
1428 default: UNREACHABLE();
1429 }
1430 // Check result type if it is currently Int32.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001431 if (result_type_ <= BinaryOpIC::INT32) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001432 __ cvttsd2si(ecx, Operand(xmm0));
1433 __ cvtsi2sd(xmm2, Operand(ecx));
1434 __ ucomisd(xmm0, xmm2);
1435 __ j(not_zero, &not_int32);
1436 __ j(carry, &not_int32);
1437 }
1438 GenerateHeapResultAllocation(masm, &call_runtime);
1439 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1440 __ ret(0);
1441 } else { // SSE2 not available, use FPU.
1442 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1443 FloatingPointHelper::LoadFloatOperands(
1444 masm,
1445 ecx,
1446 FloatingPointHelper::ARGS_IN_REGISTERS);
1447 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1448 switch (op_) {
1449 case Token::ADD: __ faddp(1); break;
1450 case Token::SUB: __ fsubp(1); break;
1451 case Token::MUL: __ fmulp(1); break;
1452 case Token::DIV: __ fdivp(1); break;
1453 default: UNREACHABLE();
1454 }
1455 Label after_alloc_failure;
1456 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1457 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1458 __ ret(0);
1459 __ bind(&after_alloc_failure);
1460 __ ffree();
1461 __ jmp(&call_runtime);
1462 }
1463
1464 __ bind(&not_floats);
1465 __ bind(&not_int32);
1466 GenerateTypeTransition(masm);
1467 break;
1468 }
1469
1470 case Token::MOD: {
1471 // For MOD we go directly to runtime in the non-smi case.
1472 break;
1473 }
1474 case Token::BIT_OR:
1475 case Token::BIT_AND:
1476 case Token::BIT_XOR:
1477 case Token::SAR:
1478 case Token::SHL:
1479 case Token::SHR: {
1480 GenerateRegisterArgsPush(masm);
1481 Label not_floats;
1482 Label not_int32;
1483 Label non_smi_result;
1484 /* {
1485 CpuFeatures::Scope use_sse2(SSE2);
1486 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1487 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1488 }*/
1489 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1490 use_sse3_,
1491 &not_floats);
1492 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1493 &not_int32);
1494 switch (op_) {
1495 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1496 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1497 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1498 case Token::SAR: __ sar_cl(eax); break;
1499 case Token::SHL: __ shl_cl(eax); break;
1500 case Token::SHR: __ shr_cl(eax); break;
1501 default: UNREACHABLE();
1502 }
1503 if (op_ == Token::SHR) {
1504 // Check if result is non-negative and fits in a smi.
1505 __ test(eax, Immediate(0xc0000000));
1506 __ j(not_zero, &call_runtime);
1507 } else {
1508 // Check if result fits in a smi.
1509 __ cmp(eax, 0xc0000000);
1510 __ j(negative, &non_smi_result);
1511 }
1512 // Tag smi result and return.
1513 __ SmiTag(eax);
1514 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1515
1516 // All ops except SHR return a signed int32 that we load in
1517 // a HeapNumber.
1518 if (op_ != Token::SHR) {
1519 __ bind(&non_smi_result);
1520 // Allocate a heap number if needed.
1521 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001522 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001523 switch (mode_) {
1524 case OVERWRITE_LEFT:
1525 case OVERWRITE_RIGHT:
1526 // If the operand was an object, we skip the
1527 // allocation of a heap number.
1528 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1529 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001530 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001531 // Fall through!
1532 case NO_OVERWRITE:
1533 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1534 __ bind(&skip_allocation);
1535 break;
1536 default: UNREACHABLE();
1537 }
1538 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001539 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001540 CpuFeatures::Scope use_sse2(SSE2);
1541 __ cvtsi2sd(xmm0, Operand(ebx));
1542 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1543 } else {
1544 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1545 __ fild_s(Operand(esp, 1 * kPointerSize));
1546 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1547 }
1548 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1549 }
1550
1551 __ bind(&not_floats);
1552 __ bind(&not_int32);
1553 GenerateTypeTransitionWithSavedArgs(masm);
1554 break;
1555 }
1556 default: UNREACHABLE(); break;
1557 }
1558
1559 // If an allocation fails, or SHR or MOD hit a hard case,
1560 // use the runtime system to get the correct result.
1561 __ bind(&call_runtime);
1562
1563 switch (op_) {
1564 case Token::ADD:
1565 GenerateRegisterArgsPush(masm);
1566 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1567 break;
1568 case Token::SUB:
1569 GenerateRegisterArgsPush(masm);
1570 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1571 break;
1572 case Token::MUL:
1573 GenerateRegisterArgsPush(masm);
1574 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1575 break;
1576 case Token::DIV:
1577 GenerateRegisterArgsPush(masm);
1578 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1579 break;
1580 case Token::MOD:
1581 GenerateRegisterArgsPush(masm);
1582 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1583 break;
1584 case Token::BIT_OR:
1585 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1586 break;
1587 case Token::BIT_AND:
1588 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1589 break;
1590 case Token::BIT_XOR:
1591 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1592 break;
1593 case Token::SAR:
1594 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1595 break;
1596 case Token::SHL:
1597 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1598 break;
1599 case Token::SHR:
1600 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1601 break;
1602 default:
1603 UNREACHABLE();
1604 }
1605}
1606
1607
danno@chromium.org40cb8782011-05-25 07:58:50 +00001608void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001609 if (op_ == Token::ADD) {
1610 // Handle string addition here, because it is the only operation
1611 // that does not do a ToNumber conversion on the operands.
1612 GenerateAddStrings(masm);
1613 }
1614
danno@chromium.org160a7b02011-04-18 15:51:38 +00001615 Factory* factory = masm->isolate()->factory();
1616
lrn@chromium.org7516f052011-03-30 08:52:27 +00001617 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001618 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001619 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001620 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001621 if (Token::IsBitOp(op_)) {
1622 __ xor_(edx, Operand(edx));
1623 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001624 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001625 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001626 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001627 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001628 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001629 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001630 if (Token::IsBitOp(op_)) {
1631 __ xor_(eax, Operand(eax));
1632 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001633 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001634 }
1635 __ bind(&done);
1636
1637 GenerateHeapNumberStub(masm);
1638}
1639
1640
danno@chromium.org40cb8782011-05-25 07:58:50 +00001641void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001642 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001643
1644 // Floating point case.
1645 switch (op_) {
1646 case Token::ADD:
1647 case Token::SUB:
1648 case Token::MUL:
1649 case Token::DIV: {
1650 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001651 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001652 CpuFeatures::Scope use_sse2(SSE2);
1653 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1654
1655 switch (op_) {
1656 case Token::ADD: __ addsd(xmm0, xmm1); break;
1657 case Token::SUB: __ subsd(xmm0, xmm1); break;
1658 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1659 case Token::DIV: __ divsd(xmm0, xmm1); break;
1660 default: UNREACHABLE();
1661 }
1662 GenerateHeapResultAllocation(masm, &call_runtime);
1663 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1664 __ ret(0);
1665 } else { // SSE2 not available, use FPU.
1666 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1667 FloatingPointHelper::LoadFloatOperands(
1668 masm,
1669 ecx,
1670 FloatingPointHelper::ARGS_IN_REGISTERS);
1671 switch (op_) {
1672 case Token::ADD: __ faddp(1); break;
1673 case Token::SUB: __ fsubp(1); break;
1674 case Token::MUL: __ fmulp(1); break;
1675 case Token::DIV: __ fdivp(1); break;
1676 default: UNREACHABLE();
1677 }
1678 Label after_alloc_failure;
1679 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1680 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1681 __ ret(0);
1682 __ bind(&after_alloc_failure);
1683 __ ffree();
1684 __ jmp(&call_runtime);
1685 }
1686
1687 __ bind(&not_floats);
1688 GenerateTypeTransition(masm);
1689 break;
1690 }
1691
1692 case Token::MOD: {
1693 // For MOD we go directly to runtime in the non-smi case.
1694 break;
1695 }
1696 case Token::BIT_OR:
1697 case Token::BIT_AND:
1698 case Token::BIT_XOR:
1699 case Token::SAR:
1700 case Token::SHL:
1701 case Token::SHR: {
1702 GenerateRegisterArgsPush(masm);
1703 Label not_floats;
1704 Label non_smi_result;
1705 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1706 use_sse3_,
1707 &not_floats);
1708 switch (op_) {
1709 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1710 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1711 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1712 case Token::SAR: __ sar_cl(eax); break;
1713 case Token::SHL: __ shl_cl(eax); break;
1714 case Token::SHR: __ shr_cl(eax); break;
1715 default: UNREACHABLE();
1716 }
1717 if (op_ == Token::SHR) {
1718 // Check if result is non-negative and fits in a smi.
1719 __ test(eax, Immediate(0xc0000000));
1720 __ j(not_zero, &call_runtime);
1721 } else {
1722 // Check if result fits in a smi.
1723 __ cmp(eax, 0xc0000000);
1724 __ j(negative, &non_smi_result);
1725 }
1726 // Tag smi result and return.
1727 __ SmiTag(eax);
1728 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1729
1730 // All ops except SHR return a signed int32 that we load in
1731 // a HeapNumber.
1732 if (op_ != Token::SHR) {
1733 __ bind(&non_smi_result);
1734 // Allocate a heap number if needed.
1735 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001736 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001737 switch (mode_) {
1738 case OVERWRITE_LEFT:
1739 case OVERWRITE_RIGHT:
1740 // If the operand was an object, we skip the
1741 // allocation of a heap number.
1742 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1743 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001744 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001745 // Fall through!
1746 case NO_OVERWRITE:
1747 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1748 __ bind(&skip_allocation);
1749 break;
1750 default: UNREACHABLE();
1751 }
1752 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001753 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001754 CpuFeatures::Scope use_sse2(SSE2);
1755 __ cvtsi2sd(xmm0, Operand(ebx));
1756 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1757 } else {
1758 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1759 __ fild_s(Operand(esp, 1 * kPointerSize));
1760 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1761 }
1762 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1763 }
1764
1765 __ bind(&not_floats);
1766 GenerateTypeTransitionWithSavedArgs(masm);
1767 break;
1768 }
1769 default: UNREACHABLE(); break;
1770 }
1771
1772 // If an allocation fails, or SHR or MOD hit a hard case,
1773 // use the runtime system to get the correct result.
1774 __ bind(&call_runtime);
1775
1776 switch (op_) {
1777 case Token::ADD:
1778 GenerateRegisterArgsPush(masm);
1779 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1780 break;
1781 case Token::SUB:
1782 GenerateRegisterArgsPush(masm);
1783 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1784 break;
1785 case Token::MUL:
1786 GenerateRegisterArgsPush(masm);
1787 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1788 break;
1789 case Token::DIV:
1790 GenerateRegisterArgsPush(masm);
1791 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1792 break;
1793 case Token::MOD:
1794 GenerateRegisterArgsPush(masm);
1795 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1796 break;
1797 case Token::BIT_OR:
1798 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1799 break;
1800 case Token::BIT_AND:
1801 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1802 break;
1803 case Token::BIT_XOR:
1804 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1805 break;
1806 case Token::SAR:
1807 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1808 break;
1809 case Token::SHL:
1810 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1811 break;
1812 case Token::SHR:
1813 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1814 break;
1815 default:
1816 UNREACHABLE();
1817 }
1818}
1819
1820
danno@chromium.org40cb8782011-05-25 07:58:50 +00001821void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001822 Label call_runtime;
1823
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001824 Counters* counters = masm->isolate()->counters();
1825 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001826
1827 switch (op_) {
1828 case Token::ADD:
1829 case Token::SUB:
1830 case Token::MUL:
1831 case Token::DIV:
1832 break;
1833 case Token::MOD:
1834 case Token::BIT_OR:
1835 case Token::BIT_AND:
1836 case Token::BIT_XOR:
1837 case Token::SAR:
1838 case Token::SHL:
1839 case Token::SHR:
1840 GenerateRegisterArgsPush(masm);
1841 break;
1842 default:
1843 UNREACHABLE();
1844 }
1845
1846 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1847
1848 // Floating point case.
1849 switch (op_) {
1850 case Token::ADD:
1851 case Token::SUB:
1852 case Token::MUL:
1853 case Token::DIV: {
1854 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001855 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001856 CpuFeatures::Scope use_sse2(SSE2);
1857 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1858
1859 switch (op_) {
1860 case Token::ADD: __ addsd(xmm0, xmm1); break;
1861 case Token::SUB: __ subsd(xmm0, xmm1); break;
1862 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1863 case Token::DIV: __ divsd(xmm0, xmm1); break;
1864 default: UNREACHABLE();
1865 }
1866 GenerateHeapResultAllocation(masm, &call_runtime);
1867 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1868 __ ret(0);
1869 } else { // SSE2 not available, use FPU.
1870 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1871 FloatingPointHelper::LoadFloatOperands(
1872 masm,
1873 ecx,
1874 FloatingPointHelper::ARGS_IN_REGISTERS);
1875 switch (op_) {
1876 case Token::ADD: __ faddp(1); break;
1877 case Token::SUB: __ fsubp(1); break;
1878 case Token::MUL: __ fmulp(1); break;
1879 case Token::DIV: __ fdivp(1); break;
1880 default: UNREACHABLE();
1881 }
1882 Label after_alloc_failure;
1883 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1884 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1885 __ ret(0);
1886 __ bind(&after_alloc_failure);
1887 __ ffree();
1888 __ jmp(&call_runtime);
1889 }
1890 __ bind(&not_floats);
1891 break;
1892 }
1893 case Token::MOD: {
1894 // For MOD we go directly to runtime in the non-smi case.
1895 break;
1896 }
1897 case Token::BIT_OR:
1898 case Token::BIT_AND:
1899 case Token::BIT_XOR:
1900 case Token::SAR:
1901 case Token::SHL:
1902 case Token::SHR: {
1903 Label non_smi_result;
1904 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1905 use_sse3_,
1906 &call_runtime);
1907 switch (op_) {
1908 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1909 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1910 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1911 case Token::SAR: __ sar_cl(eax); break;
1912 case Token::SHL: __ shl_cl(eax); break;
1913 case Token::SHR: __ shr_cl(eax); break;
1914 default: UNREACHABLE();
1915 }
1916 if (op_ == Token::SHR) {
1917 // Check if result is non-negative and fits in a smi.
1918 __ test(eax, Immediate(0xc0000000));
1919 __ j(not_zero, &call_runtime);
1920 } else {
1921 // Check if result fits in a smi.
1922 __ cmp(eax, 0xc0000000);
1923 __ j(negative, &non_smi_result);
1924 }
1925 // Tag smi result and return.
1926 __ SmiTag(eax);
1927 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1928
1929 // All ops except SHR return a signed int32 that we load in
1930 // a HeapNumber.
1931 if (op_ != Token::SHR) {
1932 __ bind(&non_smi_result);
1933 // Allocate a heap number if needed.
1934 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001935 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001936 switch (mode_) {
1937 case OVERWRITE_LEFT:
1938 case OVERWRITE_RIGHT:
1939 // If the operand was an object, we skip the
1940 // allocation of a heap number.
1941 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1942 1 * kPointerSize : 2 * kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001943 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001944 // Fall through!
1945 case NO_OVERWRITE:
1946 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1947 __ bind(&skip_allocation);
1948 break;
1949 default: UNREACHABLE();
1950 }
1951 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001952 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001953 CpuFeatures::Scope use_sse2(SSE2);
1954 __ cvtsi2sd(xmm0, Operand(ebx));
1955 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1956 } else {
1957 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1958 __ fild_s(Operand(esp, 1 * kPointerSize));
1959 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1960 }
1961 __ ret(2 * kPointerSize);
1962 }
1963 break;
1964 }
1965 default: UNREACHABLE(); break;
1966 }
1967
1968 // If all else fails, use the runtime system to get the correct
1969 // result.
1970 __ bind(&call_runtime);
1971 switch (op_) {
1972 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001973 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001974 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001975 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1976 break;
1977 }
1978 case Token::SUB:
1979 GenerateRegisterArgsPush(masm);
1980 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1981 break;
1982 case Token::MUL:
1983 GenerateRegisterArgsPush(masm);
1984 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1985 break;
1986 case Token::DIV:
1987 GenerateRegisterArgsPush(masm);
1988 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1989 break;
1990 case Token::MOD:
1991 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1992 break;
1993 case Token::BIT_OR:
1994 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1995 break;
1996 case Token::BIT_AND:
1997 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1998 break;
1999 case Token::BIT_XOR:
2000 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2001 break;
2002 case Token::SAR:
2003 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2004 break;
2005 case Token::SHL:
2006 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2007 break;
2008 case Token::SHR:
2009 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2010 break;
2011 default:
2012 UNREACHABLE();
2013 }
2014}
2015
2016
danno@chromium.org40cb8782011-05-25 07:58:50 +00002017void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002018 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002019 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002020
2021 // Registers containing left and right operands respectively.
2022 Register left = edx;
2023 Register right = eax;
2024
2025 // Test if left operand is a string.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002026 __ JumpIfSmi(left, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002027 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002028 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002029
2030 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2031 GenerateRegisterArgsPush(masm);
2032 __ TailCallStub(&string_add_left_stub);
2033
2034 // Left operand is not a string, test right.
2035 __ bind(&left_not_string);
whesse@chromium.org7b260152011-06-20 15:33:18 +00002036 __ JumpIfSmi(right, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002037 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002038 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002039
2040 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2041 GenerateRegisterArgsPush(masm);
2042 __ TailCallStub(&string_add_right_stub);
2043
2044 // Neither argument is a string.
2045 __ bind(&call_runtime);
2046}
2047
2048
danno@chromium.org40cb8782011-05-25 07:58:50 +00002049void BinaryOpStub::GenerateHeapResultAllocation(
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002050 MacroAssembler* masm,
2051 Label* alloc_failure) {
2052 Label skip_allocation;
2053 OverwriteMode mode = mode_;
2054 switch (mode) {
2055 case OVERWRITE_LEFT: {
2056 // If the argument in edx is already an object, we skip the
2057 // allocation of a heap number.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002058 __ JumpIfNotSmi(edx, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002059 // Allocate a heap number for the result. Keep eax and edx intact
2060 // for the possible runtime call.
2061 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2062 // Now edx can be overwritten losing one of the arguments as we are
2063 // now done and will not need it any more.
2064 __ mov(edx, Operand(ebx));
2065 __ bind(&skip_allocation);
2066 // Use object in edx as a result holder
2067 __ mov(eax, Operand(edx));
2068 break;
2069 }
2070 case OVERWRITE_RIGHT:
2071 // If the argument in eax is already an object, we skip the
2072 // allocation of a heap number.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002073 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002074 // Fall through!
2075 case NO_OVERWRITE:
2076 // Allocate a heap number for the result. Keep eax and edx intact
2077 // for the possible runtime call.
2078 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2079 // Now eax can be overwritten losing one of the arguments as we are
2080 // now done and will not need it any more.
2081 __ mov(eax, ebx);
2082 __ bind(&skip_allocation);
2083 break;
2084 default: UNREACHABLE();
2085 }
2086}
2087
2088
danno@chromium.org40cb8782011-05-25 07:58:50 +00002089void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002090 __ pop(ecx);
2091 __ push(edx);
2092 __ push(eax);
2093 __ push(ecx);
2094}
2095
2096
ricow@chromium.org65fae842010-08-25 15:26:24 +00002097void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002098 // TAGGED case:
2099 // Input:
2100 // esp[4]: tagged number input argument (should be number).
2101 // esp[0]: return address.
2102 // Output:
2103 // eax: tagged double result.
2104 // UNTAGGED case:
2105 // Input::
2106 // esp[0]: return address.
2107 // xmm1: untagged double input argument
2108 // Output:
2109 // xmm1: untagged double result.
2110
ricow@chromium.org65fae842010-08-25 15:26:24 +00002111 Label runtime_call;
2112 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002113 Label skip_cache;
2114 const bool tagged = (argument_type_ == TAGGED);
2115 if (tagged) {
2116 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002117 Label input_not_smi;
2118 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002119 __ mov(eax, Operand(esp, kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002120 __ JumpIfNotSmi(eax, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002121 // Input is a smi. Untag and load it onto the FPU stack.
2122 // Then load the low and high words of the double into ebx, edx.
2123 STATIC_ASSERT(kSmiTagSize == 1);
2124 __ sar(eax, 1);
2125 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2126 __ mov(Operand(esp, 0), eax);
2127 __ fild_s(Operand(esp, 0));
2128 __ fst_d(Operand(esp, 0));
2129 __ pop(edx);
2130 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002131 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002132 __ bind(&input_not_smi);
2133 // Check if input is a HeapNumber.
2134 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002135 Factory* factory = masm->isolate()->factory();
2136 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002137 __ j(not_equal, &runtime_call);
2138 // Input is a HeapNumber. Push it on the FPU stack and load its
2139 // low and high words into ebx, edx.
2140 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2141 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2142 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002143
whesse@chromium.org023421e2010-12-21 12:19:12 +00002144 __ bind(&loaded);
2145 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002146 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002147 CpuFeatures::Scope sse4_scope(SSE4_1);
2148 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2149 } else {
2150 __ pshufd(xmm0, xmm1, 0x1);
2151 __ movd(Operand(edx), xmm0);
2152 }
2153 __ movd(Operand(ebx), xmm1);
2154 }
2155
2156 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002157 // ebx = low 32 bits of double value
2158 // edx = high 32 bits of double value
2159 // Compute hash (the shifts are arithmetic):
2160 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2161 __ mov(ecx, ebx);
2162 __ xor_(ecx, Operand(edx));
2163 __ mov(eax, ecx);
2164 __ sar(eax, 16);
2165 __ xor_(ecx, Operand(eax));
2166 __ mov(eax, ecx);
2167 __ sar(eax, 8);
2168 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002169 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2170 __ and_(Operand(ecx),
2171 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002172
whesse@chromium.org023421e2010-12-21 12:19:12 +00002173 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002174 // ebx = low 32 bits of double value.
2175 // edx = high 32 bits of double value.
2176 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002177 ExternalReference cache_array =
2178 ExternalReference::transcendental_cache_array_address(masm->isolate());
2179 __ mov(eax, Immediate(cache_array));
2180 int cache_array_index =
2181 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2182 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002183 // Eax points to the cache for the type type_.
2184 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2185 __ test(eax, Operand(eax));
2186 __ j(zero, &runtime_call_clear_stack);
2187#ifdef DEBUG
2188 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002189 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002190 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2191 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2192 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2193 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2194 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2195 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2196 CHECK_EQ(0, elem_in0 - elem_start);
2197 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2198 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2199 }
2200#endif
2201 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2202 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2203 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2204 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002205 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002206 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002207 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002208 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002209 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002210 // Cache hit!
2211 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002212 if (tagged) {
2213 __ fstp(0);
2214 __ ret(kPointerSize);
2215 } else { // UNTAGGED.
2216 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2217 __ Ret();
2218 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002219
2220 __ bind(&cache_miss);
2221 // Update cache with new value.
2222 // We are short on registers, so use no_reg as scratch.
2223 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002224 if (tagged) {
2225 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2226 } else { // UNTAGGED.
2227 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2228 __ sub(Operand(esp), Immediate(kDoubleSize));
2229 __ movdbl(Operand(esp, 0), xmm1);
2230 __ fld_d(Operand(esp, 0));
2231 __ add(Operand(esp), Immediate(kDoubleSize));
2232 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002233 GenerateOperation(masm);
2234 __ mov(Operand(ecx, 0), ebx);
2235 __ mov(Operand(ecx, kIntSize), edx);
2236 __ mov(Operand(ecx, 2 * kIntSize), eax);
2237 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002238 if (tagged) {
2239 __ ret(kPointerSize);
2240 } else { // UNTAGGED.
2241 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2242 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002243
whesse@chromium.org023421e2010-12-21 12:19:12 +00002244 // Skip cache and return answer directly, only in untagged case.
2245 __ bind(&skip_cache);
2246 __ sub(Operand(esp), Immediate(kDoubleSize));
2247 __ movdbl(Operand(esp, 0), xmm1);
2248 __ fld_d(Operand(esp, 0));
2249 GenerateOperation(masm);
2250 __ fstp_d(Operand(esp, 0));
2251 __ movdbl(xmm1, Operand(esp, 0));
2252 __ add(Operand(esp), Immediate(kDoubleSize));
2253 // We return the value in xmm1 without adding it to the cache, but
2254 // we cause a scavenging GC so that future allocations will succeed.
2255 __ EnterInternalFrame();
2256 // Allocate an unused object bigger than a HeapNumber.
2257 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2258 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2259 __ LeaveInternalFrame();
2260 __ Ret();
2261 }
2262
2263 // Call runtime, doing whatever allocation and cleanup is necessary.
2264 if (tagged) {
2265 __ bind(&runtime_call_clear_stack);
2266 __ fstp(0);
2267 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002268 ExternalReference runtime =
2269 ExternalReference(RuntimeFunction(), masm->isolate());
2270 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002271 } else { // UNTAGGED.
2272 __ bind(&runtime_call_clear_stack);
2273 __ bind(&runtime_call);
2274 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2275 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2276 __ EnterInternalFrame();
2277 __ push(eax);
2278 __ CallRuntime(RuntimeFunction(), 1);
2279 __ LeaveInternalFrame();
2280 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2281 __ Ret();
2282 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002283}
2284
2285
2286Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2287 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002288 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2289 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002290 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002291 default:
2292 UNIMPLEMENTED();
2293 return Runtime::kAbort;
2294 }
2295}
2296
2297
2298void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2299 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002300 // Input value is on FP stack, and also in ebx/edx.
2301 // Input value is possibly in xmm1.
2302 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002303 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2304 // Both fsin and fcos require arguments in the range +/-2^63 and
2305 // return NaN for infinities and NaN. They can share all code except
2306 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002307 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002308 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2309 // work. We must reduce it to the appropriate range.
2310 __ mov(edi, edx);
2311 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2312 int supported_exponent_limit =
2313 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2314 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002315 __ j(below, &in_range, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002316 // Check for infinity and NaN. Both return NaN for sin.
2317 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002318 Label non_nan_result;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002319 __ j(not_equal, &non_nan_result, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002320 // Input is +/-Infinity or NaN. Result is NaN.
2321 __ fstp(0);
2322 // NaN is represented by 0x7ff8000000000000.
2323 __ push(Immediate(0x7ff80000));
2324 __ push(Immediate(0));
2325 __ fld_d(Operand(esp, 0));
2326 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002327 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002328
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002329 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002330
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002331 // Use fpmod to restrict argument to the range +/-2*PI.
2332 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2333 __ fldpi();
2334 __ fadd(0);
2335 __ fld(1);
2336 // FPU Stack: input, 2*pi, input.
2337 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002338 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002339 __ fwait();
2340 __ fnstsw_ax();
2341 // Clear if Illegal Operand or Zero Division exceptions are set.
2342 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002343 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002344 __ fnclex();
2345 __ bind(&no_exceptions);
2346 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002347
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002348 // Compute st(0) % st(1)
2349 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002350 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002351 __ bind(&partial_remainder_loop);
2352 __ fprem1();
2353 __ fwait();
2354 __ fnstsw_ax();
2355 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2356 // If C2 is set, computation only has partial result. Loop to
2357 // continue computation.
2358 __ j(not_zero, &partial_remainder_loop);
2359 }
2360 // FPU Stack: input, 2*pi, input % 2*pi
2361 __ fstp(2);
2362 __ fstp(0);
2363 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2364
2365 // FPU Stack: input % 2*pi
2366 __ bind(&in_range);
2367 switch (type_) {
2368 case TranscendentalCache::SIN:
2369 __ fsin();
2370 break;
2371 case TranscendentalCache::COS:
2372 __ fcos();
2373 break;
2374 default:
2375 UNREACHABLE();
2376 }
2377 __ bind(&done);
2378 } else {
2379 ASSERT(type_ == TranscendentalCache::LOG);
2380 __ fldln2();
2381 __ fxch();
2382 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002383 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002384}
2385
2386
ricow@chromium.org65fae842010-08-25 15:26:24 +00002387// Input: edx, eax are the left and right objects of a bit op.
2388// Output: eax, ecx are left and right integers for a bit op.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002389void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2390 bool use_sse3,
2391 Label* conversion_failure) {
2392 // Check float operands.
2393 Label arg1_is_object, check_undefined_arg1;
2394 Label arg2_is_object, check_undefined_arg2;
2395 Label load_arg2, done;
2396
2397 // Test if arg1 is a Smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002398 __ JumpIfNotSmi(edx, &arg1_is_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002399
2400 __ SmiUntag(edx);
2401 __ jmp(&load_arg2);
2402
2403 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2404 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002405 Factory* factory = masm->isolate()->factory();
2406 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002407 __ j(not_equal, conversion_failure);
2408 __ mov(edx, Immediate(0));
2409 __ jmp(&load_arg2);
2410
2411 __ bind(&arg1_is_object);
2412 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002413 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002414 __ j(not_equal, &check_undefined_arg1);
2415
2416 // Get the untagged integer version of the edx heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002417 IntegerConvert(masm, edx, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002418 __ mov(edx, ecx);
2419
2420 // Here edx has the untagged integer, eax has a Smi or a heap number.
2421 __ bind(&load_arg2);
2422
2423 // Test if arg2 is a Smi.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002424 __ JumpIfNotSmi(eax, &arg2_is_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002425
2426 __ SmiUntag(eax);
2427 __ mov(ecx, eax);
2428 __ jmp(&done);
2429
2430 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2431 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002432 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002433 __ j(not_equal, conversion_failure);
2434 __ mov(ecx, Immediate(0));
2435 __ jmp(&done);
2436
2437 __ bind(&arg2_is_object);
2438 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002439 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002440 __ j(not_equal, &check_undefined_arg2);
2441
2442 // Get the untagged integer version of the eax heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002443 IntegerConvert(masm, eax, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002444 __ bind(&done);
2445 __ mov(eax, edx);
2446}
2447
2448
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002449void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2450 bool use_sse3,
2451 Label* not_int32) {
2452 return;
2453}
2454
2455
ricow@chromium.org65fae842010-08-25 15:26:24 +00002456void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2457 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002458 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002459
whesse@chromium.org7b260152011-06-20 15:33:18 +00002460 __ JumpIfSmi(number, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002461 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002462 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002463
2464 __ bind(&load_smi);
2465 __ SmiUntag(number);
2466 __ push(number);
2467 __ fild_s(Operand(esp, 0));
2468 __ pop(number);
2469
2470 __ bind(&done);
2471}
2472
2473
2474void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002475 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002476 // Load operand in edx into xmm0.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002477 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002478 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2479
2480 __ bind(&load_eax);
2481 // Load operand in eax into xmm1.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002482 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002483 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002484 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002485
2486 __ bind(&load_smi_edx);
2487 __ SmiUntag(edx); // Untag smi before converting to float.
2488 __ cvtsi2sd(xmm0, Operand(edx));
2489 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2490 __ jmp(&load_eax);
2491
2492 __ bind(&load_smi_eax);
2493 __ SmiUntag(eax); // Untag smi before converting to float.
2494 __ cvtsi2sd(xmm1, Operand(eax));
2495 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2496
2497 __ bind(&done);
2498}
2499
2500
2501void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2502 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002503 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002504 // Load operand in edx into xmm0, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002505 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002506 Factory* factory = masm->isolate()->factory();
2507 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002508 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2509 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2510 __ bind(&load_eax);
2511 // Load operand in eax into xmm1, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002512 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002513 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002514 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002515 __ jmp(not_numbers); // Argument in eax is not a number.
2516 __ bind(&load_smi_edx);
2517 __ SmiUntag(edx); // Untag smi before converting to float.
2518 __ cvtsi2sd(xmm0, Operand(edx));
2519 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2520 __ jmp(&load_eax);
2521 __ bind(&load_smi_eax);
2522 __ SmiUntag(eax); // Untag smi before converting to float.
2523 __ cvtsi2sd(xmm1, Operand(eax));
2524 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002525 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002526 __ bind(&load_float_eax);
2527 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2528 __ bind(&done);
2529}
2530
2531
2532void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2533 Register scratch) {
2534 const Register left = edx;
2535 const Register right = eax;
2536 __ mov(scratch, left);
2537 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2538 __ SmiUntag(scratch);
2539 __ cvtsi2sd(xmm0, Operand(scratch));
2540
2541 __ mov(scratch, right);
2542 __ SmiUntag(scratch);
2543 __ cvtsi2sd(xmm1, Operand(scratch));
2544}
2545
2546
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002547void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2548 Label* non_int32,
2549 Register scratch) {
2550 __ cvttsd2si(scratch, Operand(xmm0));
2551 __ cvtsi2sd(xmm2, Operand(scratch));
2552 __ ucomisd(xmm0, xmm2);
2553 __ j(not_zero, non_int32);
2554 __ j(carry, non_int32);
2555 __ cvttsd2si(scratch, Operand(xmm1));
2556 __ cvtsi2sd(xmm2, Operand(scratch));
2557 __ ucomisd(xmm1, xmm2);
2558 __ j(not_zero, non_int32);
2559 __ j(carry, non_int32);
2560}
2561
2562
ricow@chromium.org65fae842010-08-25 15:26:24 +00002563void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2564 Register scratch,
2565 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002566 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002567 if (arg_location == ARGS_IN_REGISTERS) {
2568 __ mov(scratch, edx);
2569 } else {
2570 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2571 }
whesse@chromium.org7b260152011-06-20 15:33:18 +00002572 __ JumpIfSmi(scratch, &load_smi_1, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002573 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2574 __ bind(&done_load_1);
2575
2576 if (arg_location == ARGS_IN_REGISTERS) {
2577 __ mov(scratch, eax);
2578 } else {
2579 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2580 }
whesse@chromium.org7b260152011-06-20 15:33:18 +00002581 __ JumpIfSmi(scratch, &load_smi_2, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002582 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002583 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002584
2585 __ bind(&load_smi_1);
2586 __ SmiUntag(scratch);
2587 __ push(scratch);
2588 __ fild_s(Operand(esp, 0));
2589 __ pop(scratch);
2590 __ jmp(&done_load_1);
2591
2592 __ bind(&load_smi_2);
2593 __ SmiUntag(scratch);
2594 __ push(scratch);
2595 __ fild_s(Operand(esp, 0));
2596 __ pop(scratch);
2597
2598 __ bind(&done);
2599}
2600
2601
2602void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2603 Register scratch) {
2604 const Register left = edx;
2605 const Register right = eax;
2606 __ mov(scratch, left);
2607 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2608 __ SmiUntag(scratch);
2609 __ push(scratch);
2610 __ fild_s(Operand(esp, 0));
2611
2612 __ mov(scratch, right);
2613 __ SmiUntag(scratch);
2614 __ mov(Operand(esp, 0), scratch);
2615 __ fild_s(Operand(esp, 0));
2616 __ pop(scratch);
2617}
2618
2619
2620void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2621 Label* non_float,
2622 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002623 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002624 // Test if both operands are floats or smi -> scratch=k_is_float;
2625 // Otherwise scratch = k_not_float.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002626 __ JumpIfSmi(edx, &test_other, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002627 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002628 Factory* factory = masm->isolate()->factory();
2629 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002630 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2631
2632 __ bind(&test_other);
whesse@chromium.org7b260152011-06-20 15:33:18 +00002633 __ JumpIfSmi(eax, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002634 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002635 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002636 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2637
2638 // Fall-through: Both operands are numbers.
2639 __ bind(&done);
2640}
2641
2642
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002643void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2644 Label* non_int32) {
2645 return;
2646}
2647
2648
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002649void MathPowStub::Generate(MacroAssembler* masm) {
2650 // Registers are used as follows:
2651 // edx = base
2652 // eax = exponent
2653 // ecx = temporary, result
2654
2655 CpuFeatures::Scope use_sse2(SSE2);
2656 Label allocate_return, call_runtime;
2657
2658 // Load input parameters.
2659 __ mov(edx, Operand(esp, 2 * kPointerSize));
2660 __ mov(eax, Operand(esp, 1 * kPointerSize));
2661
2662 // Save 1 in xmm3 - we need this several times later on.
2663 __ mov(ecx, Immediate(1));
2664 __ cvtsi2sd(xmm3, Operand(ecx));
2665
2666 Label exponent_nonsmi;
2667 Label base_nonsmi;
2668 // If the exponent is a heap number go to that specific case.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002669 __ JumpIfNotSmi(eax, &exponent_nonsmi);
2670 __ JumpIfNotSmi(edx, &base_nonsmi);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002671
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002672 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002673 Label powi;
2674 __ SmiUntag(edx);
2675 __ cvtsi2sd(xmm0, Operand(edx));
2676 __ jmp(&powi);
2677 // exponent is smi and base is a heapnumber.
2678 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002679 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002680 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002681 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002682 __ j(not_equal, &call_runtime);
2683
2684 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2685
2686 // Optimized version of pow if exponent is a smi.
2687 // xmm0 contains the base.
2688 __ bind(&powi);
2689 __ SmiUntag(eax);
2690
2691 // Save exponent in base as we need to check if exponent is negative later.
2692 // We know that base and exponent are in different registers.
2693 __ mov(edx, eax);
2694
2695 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002696 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002697 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002698 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002699 __ neg(eax);
2700 __ bind(&no_neg);
2701
2702 // Load xmm1 with 1.
2703 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002704 Label while_true;
2705 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002706
2707 __ bind(&while_true);
2708 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002709 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002710 __ mulsd(xmm1, xmm0);
2711 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002712 __ mulsd(xmm0, xmm0);
2713 __ j(not_zero, &while_true);
2714
2715 // base has the original value of the exponent - if the exponent is
2716 // negative return 1/result.
2717 __ test(edx, Operand(edx));
2718 __ j(positive, &allocate_return);
2719 // Special case if xmm1 has reached infinity.
2720 __ mov(ecx, Immediate(0x7FB00000));
2721 __ movd(xmm0, Operand(ecx));
2722 __ cvtss2sd(xmm0, xmm0);
2723 __ ucomisd(xmm0, xmm1);
2724 __ j(equal, &call_runtime);
2725 __ divsd(xmm3, xmm1);
2726 __ movsd(xmm1, xmm3);
2727 __ jmp(&allocate_return);
2728
2729 // exponent (or both) is a heapnumber - no matter what we should now work
2730 // on doubles.
2731 __ bind(&exponent_nonsmi);
2732 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002733 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002734 __ j(not_equal, &call_runtime);
2735 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2736 // Test if exponent is nan.
2737 __ ucomisd(xmm1, xmm1);
2738 __ j(parity_even, &call_runtime);
2739
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002740 Label base_not_smi;
2741 Label handle_special_cases;
whesse@chromium.org7b260152011-06-20 15:33:18 +00002742 __ JumpIfNotSmi(edx, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002743 __ SmiUntag(edx);
2744 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002745 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002746
2747 __ bind(&base_not_smi);
2748 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002749 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002750 __ j(not_equal, &call_runtime);
2751 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2752 __ and_(ecx, HeapNumber::kExponentMask);
2753 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2754 // base is NaN or +/-Infinity
2755 __ j(greater_equal, &call_runtime);
2756 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2757
2758 // base is in xmm0 and exponent is in xmm1.
2759 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002760 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002761 // Test for -0.5.
2762 // Load xmm2 with -0.5.
2763 __ mov(ecx, Immediate(0xBF000000));
2764 __ movd(xmm2, Operand(ecx));
2765 __ cvtss2sd(xmm2, xmm2);
2766 // xmm2 now has -0.5.
2767 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002768 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002769
2770 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002771 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002772 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002773 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002774 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002775 __ divsd(xmm3, xmm1);
2776 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002777 __ jmp(&allocate_return);
2778
2779 // Test for 0.5.
2780 __ bind(&not_minus_half);
2781 // Load xmm2 with 0.5.
2782 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2783 __ addsd(xmm2, xmm3);
2784 // xmm2 now has 0.5.
2785 __ ucomisd(xmm2, xmm1);
2786 __ j(not_equal, &call_runtime);
2787 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002788 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002789 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002790 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002791 __ sqrtsd(xmm1, xmm1);
2792
2793 __ bind(&allocate_return);
2794 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2795 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2796 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002797 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002798
2799 __ bind(&call_runtime);
2800 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2801}
2802
2803
ricow@chromium.org65fae842010-08-25 15:26:24 +00002804void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2805 // The key is in edx and the parameter count is in eax.
2806
2807 // The displacement is used for skipping the frame pointer on the
2808 // stack. It is the offset of the last parameter (if any) relative
2809 // to the frame pointer.
2810 static const int kDisplacement = 1 * kPointerSize;
2811
2812 // Check that the key is a smi.
2813 Label slow;
whesse@chromium.org7b260152011-06-20 15:33:18 +00002814 __ JumpIfNotSmi(edx, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002815
2816 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002817 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002818 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2819 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2820 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002821 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002822
2823 // Check index against formal parameters count limit passed in
2824 // through register eax. Use unsigned comparison to get negative
2825 // check for free.
2826 __ cmp(edx, Operand(eax));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002827 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002828
2829 // Read the argument from the stack and return it.
2830 STATIC_ASSERT(kSmiTagSize == 1);
2831 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2832 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2833 __ neg(edx);
2834 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2835 __ ret(0);
2836
2837 // Arguments adaptor case: Check index against actual arguments
2838 // limit found in the arguments adaptor frame. Use unsigned
2839 // comparison to get negative check for free.
2840 __ bind(&adaptor);
2841 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2842 __ cmp(edx, Operand(ecx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002843 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002844
2845 // Read the argument from the stack and return it.
2846 STATIC_ASSERT(kSmiTagSize == 1);
2847 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2848 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2849 __ neg(edx);
2850 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2851 __ ret(0);
2852
2853 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2854 // by calling the runtime system.
2855 __ bind(&slow);
2856 __ pop(ebx); // Return address.
2857 __ push(edx);
2858 __ push(ebx);
2859 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2860}
2861
2862
whesse@chromium.org7b260152011-06-20 15:33:18 +00002863void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002864 // esp[0] : return address
2865 // esp[4] : number of parameters
2866 // esp[8] : receiver displacement
whesse@chromium.org7b260152011-06-20 15:33:18 +00002867 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00002868
whesse@chromium.org7b260152011-06-20 15:33:18 +00002869 // Check if the calling frame is an arguments adaptor frame.
2870 Label runtime;
2871 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2872 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2873 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2874 __ j(not_equal, &runtime, Label::kNear);
2875
2876 // Patch the arguments.length and the parameters pointer.
2877 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2878 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2879 __ lea(edx, Operand(edx, ecx, times_2,
2880 StandardFrameConstants::kCallerSPOffset));
2881 __ mov(Operand(esp, 2 * kPointerSize), edx);
2882
2883 __ bind(&runtime);
2884 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2885}
2886
2887
2888void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
2889 // esp[0] : return address
2890 // esp[4] : number of parameters (tagged)
2891 // esp[8] : receiver displacement
2892 // esp[12] : function
2893
2894 // ebx = parameter count (tagged)
2895 __ mov(ebx, Operand(esp, 1 * kPointerSize));
2896
2897 // Check if the calling frame is an arguments adaptor frame.
2898 // TODO(rossberg): Factor out some of the bits that are shared with the other
2899 // Generate* functions.
2900 Label runtime;
2901 Label adaptor_frame, try_allocate;
2902 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2903 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2904 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2905 __ j(equal, &adaptor_frame, Label::kNear);
2906
2907 // No adaptor, parameter count = argument count.
2908 __ mov(ecx, ebx);
2909 __ jmp(&try_allocate, Label::kNear);
2910
2911 // We have an adaptor frame. Patch the parameters pointer.
2912 __ bind(&adaptor_frame);
2913 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2914 __ lea(edx, Operand(edx, ecx, times_2,
2915 StandardFrameConstants::kCallerSPOffset));
2916 __ mov(Operand(esp, 2 * kPointerSize), edx);
2917
2918 // ebx = parameter count (tagged)
2919 // ecx = argument count (tagged)
2920 // esp[4] = parameter count (tagged)
2921 // esp[8] = address of receiver argument
2922 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
2923 __ cmp(ebx, Operand(ecx));
2924 __ j(less_equal, &try_allocate, Label::kNear);
2925 __ mov(ebx, ecx);
2926
2927 __ bind(&try_allocate);
2928
2929 // Save mapped parameter count.
2930 __ push(ebx);
2931
2932 // Compute the sizes of backing store, parameter map, and arguments object.
2933 // 1. Parameter map, has 2 extra words containing context and backing store.
2934 const int kParameterMapHeaderSize =
2935 FixedArray::kHeaderSize + 2 * kPointerSize;
2936 Label no_parameter_map;
2937 __ test(ebx, Operand(ebx));
2938 __ j(zero, &no_parameter_map, Label::kNear);
2939 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
2940 __ bind(&no_parameter_map);
2941
2942 // 2. Backing store.
2943 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
2944
2945 // 3. Arguments object.
2946 __ add(Operand(ebx), Immediate(Heap::kArgumentsObjectSize));
2947
2948 // Do the allocation of all three objects in one go.
2949 __ AllocateInNewSpace(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
2950
2951 // eax = address of new object(s) (tagged)
2952 // ecx = argument count (tagged)
2953 // esp[0] = mapped parameter count (tagged)
2954 // esp[8] = parameter count (tagged)
2955 // esp[12] = address of receiver argument
2956 // Get the arguments boilerplate from the current (global) context into edi.
2957 Label has_mapped_parameters, copy;
2958 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2959 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
2960 __ mov(ebx, Operand(esp, 0 * kPointerSize));
2961 __ test(ebx, Operand(ebx));
2962 __ j(not_zero, &has_mapped_parameters, Label::kNear);
2963 __ mov(edi, Operand(edi,
2964 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX)));
2965 __ jmp(&copy, Label::kNear);
2966
2967 __ bind(&has_mapped_parameters);
2968 __ mov(edi, Operand(edi,
2969 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX)));
2970 __ bind(&copy);
2971
2972 // eax = address of new object (tagged)
2973 // ebx = mapped parameter count (tagged)
2974 // ecx = argument count (tagged)
2975 // edi = address of boilerplate object (tagged)
2976 // esp[0] = mapped parameter count (tagged)
2977 // esp[8] = parameter count (tagged)
2978 // esp[12] = address of receiver argument
2979 // Copy the JS object part.
2980 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2981 __ mov(edx, FieldOperand(edi, i));
2982 __ mov(FieldOperand(eax, i), edx);
2983 }
2984
2985 // Setup the callee in-object property.
2986 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2987 __ mov(edx, Operand(esp, 4 * kPointerSize));
2988 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2989 Heap::kArgumentsCalleeIndex * kPointerSize),
2990 edx);
2991
2992 // Use the length (smi tagged) and set that as an in-object property too.
2993 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2994 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2995 Heap::kArgumentsLengthIndex * kPointerSize),
2996 ecx);
2997
2998 // Setup the elements pointer in the allocated arguments object.
2999 // If we allocated a parameter map, edi will point there, otherwise to the
3000 // backing store.
3001 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
3002 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3003
3004 // eax = address of new object (tagged)
3005 // ebx = mapped parameter count (tagged)
3006 // ecx = argument count (tagged)
3007 // edi = address of parameter map or backing store (tagged)
3008 // esp[0] = mapped parameter count (tagged)
3009 // esp[8] = parameter count (tagged)
3010 // esp[12] = address of receiver argument
3011 // Free a register.
3012 __ push(eax);
3013
3014 // Initialize parameter map. If there are no mapped arguments, we're done.
3015 Label skip_parameter_map;
3016 __ test(ebx, Operand(ebx));
3017 __ j(zero, &skip_parameter_map);
3018
3019 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3020 Immediate(FACTORY->non_strict_arguments_elements_map()));
3021 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
3022 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
3023 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
3024 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
3025 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
3026
3027 // Copy the parameter slots and the holes in the arguments.
3028 // We need to fill in mapped_parameter_count slots. They index the context,
3029 // where parameters are stored in reverse order, at
3030 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
3031 // The mapped parameter thus need to get indices
3032 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
3033 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
3034 // We loop from right to left.
3035 Label parameters_loop, parameters_test;
3036 __ push(ecx);
3037 __ mov(eax, Operand(esp, 2 * kPointerSize));
3038 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
3039 __ add(ebx, Operand(esp, 4 * kPointerSize));
3040 __ sub(ebx, Operand(eax));
3041 __ mov(ecx, FACTORY->the_hole_value());
3042 __ mov(edx, edi);
3043 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
3044 // eax = loop variable (tagged)
3045 // ebx = mapping index (tagged)
3046 // ecx = the hole value
3047 // edx = address of parameter map (tagged)
3048 // edi = address of backing store (tagged)
3049 // esp[0] = argument count (tagged)
3050 // esp[4] = address of new object (tagged)
3051 // esp[8] = mapped parameter count (tagged)
3052 // esp[16] = parameter count (tagged)
3053 // esp[20] = address of receiver argument
3054 __ jmp(&parameters_test, Label::kNear);
3055
3056 __ bind(&parameters_loop);
3057 __ sub(Operand(eax), Immediate(Smi::FromInt(1)));
3058 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
3059 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
3060 __ add(Operand(ebx), Immediate(Smi::FromInt(1)));
3061 __ bind(&parameters_test);
3062 __ test(eax, Operand(eax));
3063 __ j(not_zero, &parameters_loop, Label::kNear);
3064 __ pop(ecx);
3065
3066 __ bind(&skip_parameter_map);
3067
3068 // ecx = argument count (tagged)
3069 // edi = address of backing store (tagged)
3070 // esp[0] = address of new object (tagged)
3071 // esp[4] = mapped parameter count (tagged)
3072 // esp[12] = parameter count (tagged)
3073 // esp[16] = address of receiver argument
3074 // Copy arguments header and remaining slots (if there are any).
3075 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3076 Immediate(FACTORY->fixed_array_map()));
3077 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3078
3079 Label arguments_loop, arguments_test;
3080 __ mov(ebx, Operand(esp, 1 * kPointerSize));
3081 __ mov(edx, Operand(esp, 4 * kPointerSize));
3082 __ sub(Operand(edx), ebx); // Is there a smarter way to do negative scaling?
3083 __ sub(Operand(edx), ebx);
3084 __ jmp(&arguments_test, Label::kNear);
3085
3086 __ bind(&arguments_loop);
3087 __ sub(Operand(edx), Immediate(kPointerSize));
3088 __ mov(eax, Operand(edx, 0));
3089 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
3090 __ add(Operand(ebx), Immediate(Smi::FromInt(1)));
3091
3092 __ bind(&arguments_test);
3093 __ cmp(ebx, Operand(ecx));
3094 __ j(less, &arguments_loop, Label::kNear);
3095
3096 // Restore.
3097 __ pop(eax); // Address of arguments object.
3098 __ pop(ebx); // Parameter count.
3099
3100 // Return and remove the on-stack parameters.
3101 __ ret(3 * kPointerSize);
3102
3103 // Do the runtime call to allocate the arguments object.
3104 __ bind(&runtime);
3105 __ pop(eax); // Remove saved parameter count.
3106 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
3107 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
3108}
3109
3110
3111void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
3112 // esp[0] : return address
3113 // esp[4] : number of parameters
3114 // esp[8] : receiver displacement
3115 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00003116
3117 // Check if the calling frame is an arguments adaptor frame.
3118 Label adaptor_frame, try_allocate, runtime;
3119 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3120 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
3121 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3122 __ j(equal, &adaptor_frame);
3123
3124 // Get the length from the frame.
3125 __ mov(ecx, Operand(esp, 1 * kPointerSize));
3126 __ jmp(&try_allocate);
3127
3128 // Patch the arguments.length and the parameters pointer.
3129 __ bind(&adaptor_frame);
3130 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3131 __ mov(Operand(esp, 1 * kPointerSize), ecx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003132 __ lea(edx, Operand(edx, ecx, times_2,
3133 StandardFrameConstants::kCallerSPOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003134 __ mov(Operand(esp, 2 * kPointerSize), edx);
3135
3136 // Try the new space allocation. Start out with computing the size of
3137 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003138 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003139 __ bind(&try_allocate);
3140 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003141 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003142 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3143 __ bind(&add_arguments_object);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003144 __ add(Operand(ecx), Immediate(Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003145
3146 // Do the allocation of both objects in one go.
3147 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3148
3149 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003150 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3151 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003152 const int offset =
3153 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
3154 __ mov(edi, Operand(edi, offset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003155
3156 // Copy the JS object part.
3157 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3158 __ mov(ebx, FieldOperand(edi, i));
3159 __ mov(FieldOperand(eax, i), ebx);
3160 }
3161
ricow@chromium.org65fae842010-08-25 15:26:24 +00003162 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003163 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003164 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003165 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
whesse@chromium.org7b260152011-06-20 15:33:18 +00003166 Heap::kArgumentsLengthIndex * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003167 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003168
3169 // If there are no actual arguments, we're done.
3170 Label done;
3171 __ test(ecx, Operand(ecx));
3172 __ j(zero, &done);
3173
3174 // Get the parameters pointer from the stack.
3175 __ mov(edx, Operand(esp, 2 * kPointerSize));
3176
3177 // Setup the elements pointer in the allocated arguments object and
3178 // initialize the header in the elements fixed array.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003179 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003180 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3181 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
whesse@chromium.org7b260152011-06-20 15:33:18 +00003182 Immediate(FACTORY->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003183
ricow@chromium.org65fae842010-08-25 15:26:24 +00003184 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3185 // Untag the length for the loop below.
3186 __ SmiUntag(ecx);
3187
3188 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003189 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003190 __ bind(&loop);
3191 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3192 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3193 __ add(Operand(edi), Immediate(kPointerSize));
3194 __ sub(Operand(edx), Immediate(kPointerSize));
3195 __ dec(ecx);
3196 __ j(not_zero, &loop);
3197
3198 // Return and remove the on-stack parameters.
3199 __ bind(&done);
3200 __ ret(3 * kPointerSize);
3201
3202 // Do the runtime call to allocate the arguments object.
3203 __ bind(&runtime);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003204 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003205}
3206
3207
3208void RegExpExecStub::Generate(MacroAssembler* masm) {
3209 // Just jump directly to runtime if native RegExp is not selected at compile
3210 // time or if regexp entry in generated code is turned off runtime switch or
3211 // at compilation.
3212#ifdef V8_INTERPRETED_REGEXP
3213 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3214#else // V8_INTERPRETED_REGEXP
3215 if (!FLAG_regexp_entry_native) {
3216 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3217 return;
3218 }
3219
3220 // Stack frame on entry.
3221 // esp[0]: return address
3222 // esp[4]: last_match_info (expected JSArray)
3223 // esp[8]: previous index
3224 // esp[12]: subject string
3225 // esp[16]: JSRegExp object
3226
3227 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3228 static const int kPreviousIndexOffset = 2 * kPointerSize;
3229 static const int kSubjectOffset = 3 * kPointerSize;
3230 static const int kJSRegExpOffset = 4 * kPointerSize;
3231
3232 Label runtime, invoke_regexp;
3233
3234 // Ensure that a RegExp stack is allocated.
3235 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003236 ExternalReference::address_of_regexp_stack_memory_address(
3237 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003238 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003239 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003240 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3241 __ test(ebx, Operand(ebx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003242 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003243
3244 // Check that the first argument is a JSRegExp object.
3245 __ mov(eax, Operand(esp, kJSRegExpOffset));
3246 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003247 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003248 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3249 __ j(not_equal, &runtime);
3250 // Check that the RegExp has been compiled (data contains a fixed array).
3251 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3252 if (FLAG_debug_code) {
3253 __ test(ecx, Immediate(kSmiTagMask));
3254 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3255 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3256 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3257 }
3258
3259 // ecx: RegExp data (FixedArray)
3260 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3261 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3262 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3263 __ j(not_equal, &runtime);
3264
3265 // ecx: RegExp data (FixedArray)
3266 // Check that the number of captures fit in the static offsets vector buffer.
3267 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3268 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3269 // uses the asumption that smis are 2 * their untagged value.
3270 STATIC_ASSERT(kSmiTag == 0);
3271 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3272 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3273 // Check that the static offsets vector buffer is large enough.
3274 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3275 __ j(above, &runtime);
3276
3277 // ecx: RegExp data (FixedArray)
3278 // edx: Number of capture registers
3279 // Check that the second argument is a string.
3280 __ mov(eax, Operand(esp, kSubjectOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003281 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003282 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3283 __ j(NegateCondition(is_string), &runtime);
3284 // Get the length of the string to ebx.
3285 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3286
3287 // ebx: Length of subject string as a smi
3288 // ecx: RegExp data (FixedArray)
3289 // edx: Number of capture registers
3290 // Check that the third argument is a positive smi less than the subject
3291 // string length. A negative value will be greater (unsigned comparison).
3292 __ mov(eax, Operand(esp, kPreviousIndexOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003293 __ JumpIfNotSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003294 __ cmp(eax, Operand(ebx));
3295 __ j(above_equal, &runtime);
3296
3297 // ecx: RegExp data (FixedArray)
3298 // edx: Number of capture registers
3299 // Check that the fourth object is a JSArray object.
3300 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003301 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003302 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3303 __ j(not_equal, &runtime);
3304 // Check that the JSArray is in fast case.
3305 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3306 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003307 Factory* factory = masm->isolate()->factory();
3308 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003309 __ j(not_equal, &runtime);
3310 // Check that the last match info has space for the capture registers and the
3311 // additional information.
3312 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3313 __ SmiUntag(eax);
3314 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3315 __ cmp(edx, Operand(eax));
3316 __ j(greater, &runtime);
3317
3318 // ecx: RegExp data (FixedArray)
3319 // Check the representation and encoding of the subject string.
3320 Label seq_ascii_string, seq_two_byte_string, check_code;
3321 __ mov(eax, Operand(esp, kSubjectOffset));
3322 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3323 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3324 // First check for flat two byte string.
3325 __ and_(ebx,
3326 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3327 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3328 __ j(zero, &seq_two_byte_string);
3329 // Any other flat string must be a flat ascii string.
3330 __ test(Operand(ebx),
3331 Immediate(kIsNotStringMask | kStringRepresentationMask));
3332 __ j(zero, &seq_ascii_string);
3333
3334 // Check for flat cons string.
3335 // A flat cons string is a cons string where the second part is the empty
3336 // string. In that case the subject string is just the first part of the cons
3337 // string. Also in this case the first part of the cons string is known to be
3338 // a sequential string or an external string.
3339 STATIC_ASSERT(kExternalStringTag != 0);
3340 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3341 __ test(Operand(ebx),
3342 Immediate(kIsNotStringMask | kExternalStringTag));
3343 __ j(not_zero, &runtime);
3344 // String is a cons string.
3345 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003346 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003347 __ j(not_equal, &runtime);
3348 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3349 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3350 // String is a cons string with empty second part.
3351 // eax: first part of cons string.
3352 // ebx: map of first part of cons string.
3353 // Is first part a flat two byte string?
3354 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3355 kStringRepresentationMask | kStringEncodingMask);
3356 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3357 __ j(zero, &seq_two_byte_string);
3358 // Any other flat string must be ascii.
3359 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3360 kStringRepresentationMask);
3361 __ j(not_zero, &runtime);
3362
3363 __ bind(&seq_ascii_string);
3364 // eax: subject string (flat ascii)
3365 // ecx: RegExp data (FixedArray)
3366 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3367 __ Set(edi, Immediate(1)); // Type is ascii.
3368 __ jmp(&check_code);
3369
3370 __ bind(&seq_two_byte_string);
3371 // eax: subject string (flat two byte)
3372 // ecx: RegExp data (FixedArray)
3373 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3374 __ Set(edi, Immediate(0)); // Type is two byte.
3375
3376 __ bind(&check_code);
3377 // Check that the irregexp code has been generated for the actual string
3378 // encoding. If it has, the field contains a code object otherwise it contains
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00003379 // a smi (code flushing support).
3380 __ JumpIfSmi(edx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003381
3382 // eax: subject string
3383 // edx: code
3384 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3385 // Load used arguments before starting to push arguments for call to native
3386 // RegExp code to avoid handling changing stack height.
3387 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3388 __ SmiUntag(ebx); // Previous index from smi.
3389
3390 // eax: subject string
3391 // ebx: previous index
3392 // edx: code
3393 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3394 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003395 Counters* counters = masm->isolate()->counters();
3396 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003397
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003398 // Isolates: note we add an additional parameter here (isolate pointer).
3399 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003400 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003401
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003402 // Argument 8: Pass current isolate address.
3403 __ mov(Operand(esp, 7 * kPointerSize),
3404 Immediate(ExternalReference::isolate_address()));
3405
ricow@chromium.org65fae842010-08-25 15:26:24 +00003406 // Argument 7: Indicate that this is a direct call from JavaScript.
3407 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3408
3409 // Argument 6: Start (high end) of backtracking stack memory area.
3410 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3411 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3412 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3413
3414 // Argument 5: static offsets vector buffer.
3415 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003416 Immediate(ExternalReference::address_of_static_offsets_vector(
3417 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003418
3419 // Argument 4: End of string data
3420 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003421 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003422 __ test(edi, Operand(edi));
3423 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003424 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003425 __ SmiUntag(edi);
3426 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3427 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3428 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3429 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003430 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003431
3432 __ bind(&setup_two_byte);
3433 STATIC_ASSERT(kSmiTag == 0);
3434 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3435 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3436 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3437 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3438 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3439
3440 __ bind(&setup_rest);
3441
3442 // Argument 2: Previous index.
3443 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3444
3445 // Argument 1: Subject string.
3446 __ mov(Operand(esp, 0 * kPointerSize), eax);
3447
3448 // Locate the code entry and call it.
3449 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003450 __ call(Operand(edx));
3451
3452 // Drop arguments and come back to JS mode.
3453 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003454
3455 // Check the result.
3456 Label success;
3457 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003458 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003459 Label failure;
3460 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003461 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003462 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3463 // If not exception it can only be retry. Handle that in the runtime system.
3464 __ j(not_equal, &runtime);
3465 // Result must now be exception. If there is no pending exception already a
3466 // stack overflow (on the backtrack stack) was detected in RegExp code but
3467 // haven't created the exception yet. Handle that in the runtime system.
3468 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003469 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3470 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003471 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003472 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3473 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003474 __ mov(eax, Operand::StaticVariable(pending_exception));
3475 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003476 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003477 // For exception, throw the exception again.
3478
3479 // Clear the pending exception variable.
3480 __ mov(Operand::StaticVariable(pending_exception), edx);
3481
3482 // Special handling of termination exceptions which are uncatchable
3483 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003484 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003485 Label throw_termination_exception;
3486 __ j(equal, &throw_termination_exception);
3487
3488 // Handle normal exception by following handler chain.
3489 __ Throw(eax);
3490
3491 __ bind(&throw_termination_exception);
3492 __ ThrowUncatchable(TERMINATION, eax);
3493
ricow@chromium.org65fae842010-08-25 15:26:24 +00003494 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003495 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003496 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003497 __ ret(4 * kPointerSize);
3498
3499 // Load RegExp data.
3500 __ bind(&success);
3501 __ mov(eax, Operand(esp, kJSRegExpOffset));
3502 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3503 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3504 // Calculate number of capture registers (number_of_captures + 1) * 2.
3505 STATIC_ASSERT(kSmiTag == 0);
3506 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3507 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3508
3509 // edx: Number of capture registers
3510 // Load last_match_info which is still known to be a fast case JSArray.
3511 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3512 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3513
3514 // ebx: last_match_info backing store (FixedArray)
3515 // edx: number of capture registers
3516 // Store the capture count.
3517 __ SmiTag(edx); // Number of capture registers to smi.
3518 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3519 __ SmiUntag(edx); // Number of capture registers back from smi.
3520 // Store last subject and last input.
3521 __ mov(eax, Operand(esp, kSubjectOffset));
3522 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3523 __ mov(ecx, ebx);
3524 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3525 __ mov(eax, Operand(esp, kSubjectOffset));
3526 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3527 __ mov(ecx, ebx);
3528 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3529
3530 // Get the static offsets vector filled by the native regexp code.
3531 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003532 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003533 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3534
3535 // ebx: last_match_info backing store (FixedArray)
3536 // ecx: offsets vector
3537 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003538 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003539 // Capture register counter starts from number of capture registers and
3540 // counts down until wraping after zero.
3541 __ bind(&next_capture);
3542 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003543 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003544 // Read the value from the static offsets vector buffer.
3545 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3546 __ SmiTag(edi);
3547 // Store the smi value in the last match info.
3548 __ mov(FieldOperand(ebx,
3549 edx,
3550 times_pointer_size,
3551 RegExpImpl::kFirstCaptureOffset),
3552 edi);
3553 __ jmp(&next_capture);
3554 __ bind(&done);
3555
3556 // Return last match info.
3557 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3558 __ ret(4 * kPointerSize);
3559
3560 // Do the runtime call to execute the regexp.
3561 __ bind(&runtime);
3562 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3563#endif // V8_INTERPRETED_REGEXP
3564}
3565
3566
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003567void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3568 const int kMaxInlineLength = 100;
3569 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003570 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003571 __ mov(ebx, Operand(esp, kPointerSize * 3));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003572 __ JumpIfNotSmi(ebx, &slowcase);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003573 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3574 __ j(above, &slowcase);
3575 // Smi-tagging is equivalent to multiplying by 2.
3576 STATIC_ASSERT(kSmiTag == 0);
3577 STATIC_ASSERT(kSmiTagSize == 1);
3578 // Allocate RegExpResult followed by FixedArray with size in ebx.
3579 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3580 // Elements: [Map][Length][..elements..]
3581 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3582 times_half_pointer_size,
3583 ebx, // In: Number of elements (times 2, being a smi)
3584 eax, // Out: Start of allocation (tagged).
3585 ecx, // Out: End of allocation.
3586 edx, // Scratch register
3587 &slowcase,
3588 TAG_OBJECT);
3589 // eax: Start of allocated area, object-tagged.
3590
3591 // Set JSArray map to global.regexp_result_map().
3592 // Set empty properties FixedArray.
3593 // Set elements to point to FixedArray allocated right after the JSArray.
3594 // Interleave operations for better latency.
3595 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003596 Factory* factory = masm->isolate()->factory();
3597 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003598 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3599 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3600 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3601 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3602 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3603 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3604
3605 // Set input, index and length fields from arguments.
3606 __ mov(ecx, Operand(esp, kPointerSize * 1));
3607 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3608 __ mov(ecx, Operand(esp, kPointerSize * 2));
3609 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3610 __ mov(ecx, Operand(esp, kPointerSize * 3));
3611 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3612
3613 // Fill out the elements FixedArray.
3614 // eax: JSArray.
3615 // ebx: FixedArray.
3616 // ecx: Number of elements in array, as smi.
3617
3618 // Set map.
3619 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003620 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003621 // Set length.
3622 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3623 // Fill contents of fixed-array with the-hole.
3624 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003625 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003626 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3627 // Fill fixed array elements with hole.
3628 // eax: JSArray.
3629 // ecx: Number of elements to fill.
3630 // ebx: Start of elements in FixedArray.
3631 // edx: the hole.
3632 Label loop;
3633 __ test(ecx, Operand(ecx));
3634 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003635 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003636 __ sub(Operand(ecx), Immediate(1));
3637 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3638 __ jmp(&loop);
3639
3640 __ bind(&done);
3641 __ ret(3 * kPointerSize);
3642
3643 __ bind(&slowcase);
3644 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3645}
3646
3647
ricow@chromium.org65fae842010-08-25 15:26:24 +00003648void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3649 Register object,
3650 Register result,
3651 Register scratch1,
3652 Register scratch2,
3653 bool object_is_smi,
3654 Label* not_found) {
3655 // Use of registers. Register result is used as a temporary.
3656 Register number_string_cache = result;
3657 Register mask = scratch1;
3658 Register scratch = scratch2;
3659
3660 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003661 ExternalReference roots_address =
3662 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003663 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3664 __ mov(number_string_cache,
3665 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3666 // Make the hash mask from the length of the number string cache. It
3667 // contains two elements (number and string) for each cache entry.
3668 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3669 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3670 __ sub(Operand(mask), Immediate(1)); // Make mask.
3671
3672 // Calculate the entry in the number string cache. The hash value in the
3673 // number string cache for smis is just the smi value, and the hash for
3674 // doubles is the xor of the upper and lower words. See
3675 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003676 Label smi_hash_calculated;
3677 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003678 if (object_is_smi) {
3679 __ mov(scratch, object);
3680 __ SmiUntag(scratch);
3681 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003682 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003683 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003684 __ JumpIfNotSmi(object, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003685 __ mov(scratch, object);
3686 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003687 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003688 __ bind(&not_smi);
3689 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003690 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003691 __ j(not_equal, not_found);
3692 STATIC_ASSERT(8 == kDoubleSize);
3693 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3694 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3695 // Object is heap number and hash is now in scratch. Calculate cache index.
3696 __ and_(scratch, Operand(mask));
3697 Register index = scratch;
3698 Register probe = mask;
3699 __ mov(probe,
3700 FieldOperand(number_string_cache,
3701 index,
3702 times_twice_pointer_size,
3703 FixedArray::kHeaderSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003704 __ JumpIfSmi(probe, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003705 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003706 CpuFeatures::Scope fscope(SSE2);
3707 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3708 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3709 __ ucomisd(xmm0, xmm1);
3710 } else {
3711 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3712 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3713 __ FCmp();
3714 }
3715 __ j(parity_even, not_found); // Bail out if NaN is involved.
3716 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003717 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003718 }
3719
3720 __ bind(&smi_hash_calculated);
3721 // Object is smi and hash is now in scratch. Calculate cache index.
3722 __ and_(scratch, Operand(mask));
3723 Register index = scratch;
3724 // Check if the entry is the smi we are looking for.
3725 __ cmp(object,
3726 FieldOperand(number_string_cache,
3727 index,
3728 times_twice_pointer_size,
3729 FixedArray::kHeaderSize));
3730 __ j(not_equal, not_found);
3731
3732 // Get the result from the cache.
3733 __ bind(&load_result_from_cache);
3734 __ mov(result,
3735 FieldOperand(number_string_cache,
3736 index,
3737 times_twice_pointer_size,
3738 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003739 Counters* counters = masm->isolate()->counters();
3740 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003741}
3742
3743
3744void NumberToStringStub::Generate(MacroAssembler* masm) {
3745 Label runtime;
3746
3747 __ mov(ebx, Operand(esp, kPointerSize));
3748
3749 // Generate code to lookup number in the number string cache.
3750 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3751 __ ret(1 * kPointerSize);
3752
3753 __ bind(&runtime);
3754 // Handle number to string in the runtime system if not found in the cache.
3755 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3756}
3757
3758
3759static int NegativeComparisonResult(Condition cc) {
3760 ASSERT(cc != equal);
3761 ASSERT((cc == less) || (cc == less_equal)
3762 || (cc == greater) || (cc == greater_equal));
3763 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3764}
3765
3766void CompareStub::Generate(MacroAssembler* masm) {
3767 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3768
3769 Label check_unequal_objects, done;
3770
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003771 // Compare two smis if required.
3772 if (include_smi_compare_) {
3773 Label non_smi, smi_done;
3774 __ mov(ecx, Operand(edx));
3775 __ or_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00003776 __ JumpIfNotSmi(ecx, &non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003777 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3778 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003779 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003780 __ bind(&smi_done);
3781 __ mov(eax, edx);
3782 __ ret(0);
3783 __ bind(&non_smi);
3784 } else if (FLAG_debug_code) {
3785 __ mov(ecx, Operand(edx));
3786 __ or_(ecx, Operand(eax));
3787 __ test(ecx, Immediate(kSmiTagMask));
3788 __ Assert(not_zero, "Unexpected smi operands.");
3789 }
3790
ricow@chromium.org65fae842010-08-25 15:26:24 +00003791 // NOTICE! This code is only reached after a smi-fast-case check, so
3792 // it is certain that at least one operand isn't a smi.
3793
3794 // Identical objects can be compared fast, but there are some tricky cases
3795 // for NaN and undefined.
3796 {
3797 Label not_identical;
3798 __ cmp(eax, Operand(edx));
3799 __ j(not_equal, &not_identical);
3800
3801 if (cc_ != equal) {
3802 // Check for undefined. undefined OP undefined is false even though
3803 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003804 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003805 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003806 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003807 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3808 __ ret(0);
3809 __ bind(&check_for_nan);
3810 }
3811
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003812 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003813 // so we do the second best thing - test it ourselves.
3814 // Note: if cc_ != equal, never_nan_nan_ is not used.
3815 if (never_nan_nan_ && (cc_ == equal)) {
3816 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3817 __ ret(0);
3818 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003819 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003820 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003821 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003822 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003823 if (cc_ != equal) {
3824 // Call runtime on identical JSObjects. Otherwise return equal.
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003825 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003826 __ j(above_equal, &not_identical);
3827 }
3828 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3829 __ ret(0);
3830
3831 __ bind(&heap_number);
3832 // It is a heap number, so return non-equal if it's NaN and equal if
3833 // it's not NaN.
3834 // The representation of NaN values has all exponent bits (52..62) set,
3835 // and not all mantissa bits (0..51) clear.
3836 // We only accept QNaNs, which have bit 51 set.
3837 // Read top bits of double representation (second word of value).
3838
3839 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3840 // all bits in the mask are set. We only need to check the word
3841 // that contains the exponent and high bit of the mantissa.
3842 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3843 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003844 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003845 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3846 // bits.
3847 __ add(edx, Operand(edx));
3848 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3849 if (cc_ == equal) {
3850 STATIC_ASSERT(EQUAL != 1);
3851 __ setcc(above_equal, eax);
3852 __ ret(0);
3853 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003854 Label nan;
3855 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003856 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3857 __ ret(0);
3858 __ bind(&nan);
3859 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3860 __ ret(0);
3861 }
3862 }
3863
3864 __ bind(&not_identical);
3865 }
3866
3867 // Strict equality can quickly decide whether objects are equal.
3868 // Non-strict object equality is slower, so it is handled later in the stub.
3869 if (cc_ == equal && strict_) {
3870 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003871 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003872 // If we're doing a strict equality comparison, we don't have to do
3873 // type conversion, so we generate code to do fast comparison for objects
3874 // and oddballs. Non-smi numbers and strings still go through the usual
3875 // slow-case code.
3876 // If either is a Smi (we know that not both are), then they can only
3877 // be equal if the other is a HeapNumber. If so, use the slow case.
3878 STATIC_ASSERT(kSmiTag == 0);
3879 ASSERT_EQ(0, Smi::FromInt(0));
3880 __ mov(ecx, Immediate(kSmiTagMask));
3881 __ and_(ecx, Operand(eax));
3882 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003883 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003884 // One operand is a smi.
3885
3886 // Check whether the non-smi is a heap number.
3887 STATIC_ASSERT(kSmiTagMask == 1);
3888 // ecx still holds eax & kSmiTag, which is either zero or one.
3889 __ sub(Operand(ecx), Immediate(0x01));
3890 __ mov(ebx, edx);
3891 __ xor_(ebx, Operand(eax));
3892 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3893 __ xor_(ebx, Operand(eax));
3894 // if eax was smi, ebx is now edx, else eax.
3895
3896 // Check if the non-smi operand is a heap number.
3897 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003898 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003899 // If heap number, handle it in the slow case.
3900 __ j(equal, &slow);
3901 // Return non-equal (ebx is not zero)
3902 __ mov(eax, ebx);
3903 __ ret(0);
3904
3905 __ bind(&not_smis);
3906 // If either operand is a JSObject or an oddball value, then they are not
3907 // equal since their pointers are different
3908 // There is no test for undetectability in strict equality.
3909
3910 // Get the type of the first operand.
3911 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003912 Label first_non_object;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003913 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
3914 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003915 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003916
3917 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003918 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003919 STATIC_ASSERT(kHeapObjectTag != 0);
3920 __ bind(&return_not_equal);
3921 __ ret(0);
3922
3923 __ bind(&first_non_object);
3924 // Check for oddballs: true, false, null, undefined.
3925 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3926 __ j(equal, &return_not_equal);
3927
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00003928 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003929 __ j(above_equal, &return_not_equal);
3930
3931 // Check for oddballs: true, false, null, undefined.
3932 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3933 __ j(equal, &return_not_equal);
3934
3935 // Fall through to the general case.
3936 __ bind(&slow);
3937 }
3938
3939 // Generate the number comparison code.
3940 if (include_number_compare_) {
3941 Label non_number_comparison;
3942 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003943 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003944 CpuFeatures::Scope use_sse2(SSE2);
3945 CpuFeatures::Scope use_cmov(CMOV);
3946
3947 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3948 __ ucomisd(xmm0, xmm1);
3949
3950 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003951 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003952 // Return a result of -1, 0, or 1, based on EFLAGS.
3953 __ mov(eax, 0); // equal
3954 __ mov(ecx, Immediate(Smi::FromInt(1)));
3955 __ cmov(above, eax, Operand(ecx));
3956 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3957 __ cmov(below, eax, Operand(ecx));
3958 __ ret(0);
3959 } else {
3960 FloatingPointHelper::CheckFloatOperands(
3961 masm, &non_number_comparison, ebx);
3962 FloatingPointHelper::LoadFloatOperand(masm, eax);
3963 FloatingPointHelper::LoadFloatOperand(masm, edx);
3964 __ FCmp();
3965
3966 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003967 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003968
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003969 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003970 // Return a result of -1, 0, or 1, based on EFLAGS.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003971 __ j(below, &below_label);
3972 __ j(above, &above_label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003973
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003974 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003975 __ ret(0);
3976
3977 __ bind(&below_label);
3978 __ mov(eax, Immediate(Smi::FromInt(-1)));
3979 __ ret(0);
3980
3981 __ bind(&above_label);
3982 __ mov(eax, Immediate(Smi::FromInt(1)));
3983 __ ret(0);
3984 }
3985
3986 // If one of the numbers was NaN, then the result is always false.
3987 // The cc is never not-equal.
3988 __ bind(&unordered);
3989 ASSERT(cc_ != not_equal);
3990 if (cc_ == less || cc_ == less_equal) {
3991 __ mov(eax, Immediate(Smi::FromInt(1)));
3992 } else {
3993 __ mov(eax, Immediate(Smi::FromInt(-1)));
3994 }
3995 __ ret(0);
3996
3997 // The number comparison code did not provide a valid result.
3998 __ bind(&non_number_comparison);
3999 }
4000
4001 // Fast negative check for symbol-to-symbol equality.
4002 Label check_for_strings;
4003 if (cc_ == equal) {
4004 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4005 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4006
4007 // We've already checked for object identity, so if both operands
4008 // are symbols they aren't equal. Register eax already holds a
4009 // non-zero value, which indicates not equal, so just return.
4010 __ ret(0);
4011 }
4012
4013 __ bind(&check_for_strings);
4014
4015 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4016 &check_unequal_objects);
4017
4018 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004019 if (cc_ == equal) {
4020 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00004021 edx,
4022 eax,
4023 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00004024 ebx);
4025 } else {
4026 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4027 edx,
4028 eax,
4029 ecx,
4030 ebx,
4031 edi);
4032 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004033#ifdef DEBUG
4034 __ Abort("Unexpected fall-through from string comparison");
4035#endif
4036
4037 __ bind(&check_unequal_objects);
4038 if (cc_ == equal && !strict_) {
4039 // Non-strict equality. Objects are unequal if
4040 // they are both JSObjects and not undetectable,
4041 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004042 Label not_both_objects;
4043 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004044 // At most one is a smi, so we can test for smi by adding the two.
4045 // A smi plus a heap object has the low bit set, a heap object plus
4046 // a heap object has the low bit clear.
4047 STATIC_ASSERT(kSmiTag == 0);
4048 STATIC_ASSERT(kSmiTagMask == 1);
4049 __ lea(ecx, Operand(eax, edx, times_1, 0));
4050 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004051 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004052 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004053 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004054 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004055 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004056 // We do not bail out after this point. Both are JSObjects, and
4057 // they are equal if and only if both are undetectable.
4058 // The and of the undetectable flags is 1 if and only if they are equal.
4059 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4060 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004061 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004062 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4063 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004064 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004065 // The objects are both undetectable, so they both compare as the value
4066 // undefined, and are equal.
4067 __ Set(eax, Immediate(EQUAL));
4068 __ bind(&return_unequal);
4069 // Return non-equal by returning the non-zero object pointer in eax,
4070 // or return equal if we fell through to here.
4071 __ ret(0); // rax, rdx were pushed
4072 __ bind(&not_both_objects);
4073 }
4074
4075 // Push arguments below the return address.
4076 __ pop(ecx);
4077 __ push(edx);
4078 __ push(eax);
4079
4080 // Figure out which native to call and setup the arguments.
4081 Builtins::JavaScript builtin;
4082 if (cc_ == equal) {
4083 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4084 } else {
4085 builtin = Builtins::COMPARE;
4086 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4087 }
4088
4089 // Restore return address on the stack.
4090 __ push(ecx);
4091
4092 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4093 // tagged as a small integer.
4094 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4095}
4096
4097
4098void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4099 Label* label,
4100 Register object,
4101 Register scratch) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004102 __ JumpIfSmi(object, label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004103 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4104 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4105 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4106 __ cmp(scratch, kSymbolTag | kStringTag);
4107 __ j(not_equal, label);
4108}
4109
4110
4111void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00004112 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004113}
4114
4115
4116void CallFunctionStub::Generate(MacroAssembler* masm) {
4117 Label slow;
4118
danno@chromium.org40cb8782011-05-25 07:58:50 +00004119 // The receiver might implicitly be the global object. This is
4120 // indicated by passing the hole as the receiver to the call
4121 // function stub.
4122 if (ReceiverMightBeImplicit()) {
4123 Label call;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004124 // Get the receiver from the stack.
4125 // +1 ~ return address
ricow@chromium.org65fae842010-08-25 15:26:24 +00004126 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004127 // Call as function is indicated with the hole.
4128 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
4129 __ j(not_equal, &call, Label::kNear);
4130 // Patch the receiver on the stack with the global receiver object.
4131 __ mov(ebx, GlobalObjectOperand());
4132 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
4133 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), ebx);
4134 __ bind(&call);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004135 }
4136
4137 // Get the function to call from the stack.
4138 // +2 ~ receiver, return address
4139 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4140
4141 // Check that the function really is a JavaScript function.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004142 __ JumpIfSmi(edi, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004143 // Goto slow case if we do not have a function.
4144 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004145 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004146
4147 // Fast-case: Just invoke the function.
4148 ParameterCount actual(argc_);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004149
4150 if (ReceiverMightBeImplicit()) {
4151 Label call_as_function;
4152 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
4153 __ j(equal, &call_as_function);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00004154 __ InvokeFunction(edi,
4155 actual,
4156 JUMP_FUNCTION,
4157 NullCallWrapper(),
4158 CALL_AS_METHOD);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004159 __ bind(&call_as_function);
4160 }
4161 __ InvokeFunction(edi,
4162 actual,
4163 JUMP_FUNCTION,
4164 NullCallWrapper(),
4165 CALL_AS_FUNCTION);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004166
4167 // Slow-case: Non-function called.
4168 __ bind(&slow);
4169 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4170 // of the original receiver from the call site).
4171 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4172 __ Set(eax, Immediate(argc_));
4173 __ Set(ebx, Immediate(0));
4174 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004175 Handle<Code> adaptor =
4176 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004177 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4178}
4179
4180
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004181bool CEntryStub::NeedsImmovableCode() {
4182 return false;
4183}
4184
4185
ricow@chromium.org65fae842010-08-25 15:26:24 +00004186void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004187 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004188}
4189
4190
ricow@chromium.org65fae842010-08-25 15:26:24 +00004191void CEntryStub::GenerateCore(MacroAssembler* masm,
4192 Label* throw_normal_exception,
4193 Label* throw_termination_exception,
4194 Label* throw_out_of_memory_exception,
4195 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004196 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004197 // eax: result parameter for PerformGC, if any
4198 // ebx: pointer to C function (C callee-saved)
4199 // ebp: frame pointer (restored after C call)
4200 // esp: stack pointer (restored after C call)
4201 // edi: number of arguments including receiver (C callee-saved)
4202 // esi: pointer to the first argument (C callee-saved)
4203
4204 // Result returned in eax, or eax+edx if result_size_ is 2.
4205
4206 // Check stack alignment.
4207 if (FLAG_debug_code) {
4208 __ CheckStackAlignment();
4209 }
4210
4211 if (do_gc) {
4212 // Pass failure code returned from last attempt as first argument to
4213 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4214 // stack alignment is known to be correct. This function takes one argument
4215 // which is passed on the stack, and we know that the stack has been
4216 // prepared to pass at least one argument.
4217 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4218 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4219 }
4220
4221 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004222 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004223 if (always_allocate_scope) {
4224 __ inc(Operand::StaticVariable(scope_depth));
4225 }
4226
4227 // Call C function.
4228 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4229 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004230 __ mov(Operand(esp, 2 * kPointerSize),
4231 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004232 __ call(Operand(ebx));
4233 // Result is in eax or edx:eax - do not destroy these registers!
4234
4235 if (always_allocate_scope) {
4236 __ dec(Operand::StaticVariable(scope_depth));
4237 }
4238
4239 // Make sure we're not trying to return 'the hole' from the runtime
4240 // call as this may lead to crashes in the IC code later.
4241 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004242 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004243 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004244 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004245 __ int3();
4246 __ bind(&okay);
4247 }
4248
4249 // Check for failure result.
4250 Label failure_returned;
4251 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4252 __ lea(ecx, Operand(eax, 1));
4253 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4254 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004255 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004256
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004257 ExternalReference pending_exception_address(
4258 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004259
4260 // Check that there is no pending exception, otherwise we
4261 // should have returned some failure value.
4262 if (FLAG_debug_code) {
4263 __ push(edx);
4264 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004265 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004266 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004267 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4268 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004269 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004270 __ int3();
4271 __ bind(&okay);
4272 __ pop(edx);
4273 }
4274
ricow@chromium.org65fae842010-08-25 15:26:24 +00004275 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004276 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004277 __ ret(0);
4278
4279 // Handling of failure.
4280 __ bind(&failure_returned);
4281
4282 Label retry;
4283 // If the returned exception is RETRY_AFTER_GC continue at retry label
4284 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4285 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004286 __ j(zero, &retry);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004287
4288 // Special handling of out of memory exceptions.
4289 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4290 __ j(equal, throw_out_of_memory_exception);
4291
4292 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004293 ExternalReference the_hole_location =
4294 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004295 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004296 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004297 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4298
4299 // Special handling of termination exceptions which are uncatchable
4300 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004301 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004302 __ j(equal, throw_termination_exception);
4303
4304 // Handle normal exception.
4305 __ jmp(throw_normal_exception);
4306
4307 // Retry.
4308 __ bind(&retry);
4309}
4310
4311
4312void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4313 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004314 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004315}
4316
4317
4318void CEntryStub::Generate(MacroAssembler* masm) {
4319 // eax: number of arguments including receiver
4320 // ebx: pointer to C function (C callee-saved)
4321 // ebp: frame pointer (restored after C call)
4322 // esp: stack pointer (restored after C call)
4323 // esi: current context (C callee-saved)
4324 // edi: JS function of the caller (C callee-saved)
4325
4326 // NOTE: Invocations of builtins may return failure objects instead
4327 // of a proper result. The builtin entry handles this by performing
4328 // a garbage collection and retrying the builtin (twice).
4329
4330 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004331 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004332
4333 // eax: result parameter for PerformGC, if any (setup below)
4334 // ebx: pointer to builtin function (C callee-saved)
4335 // ebp: frame pointer (restored after C call)
4336 // esp: stack pointer (restored after C call)
4337 // edi: number of arguments including receiver (C callee-saved)
4338 // esi: argv pointer (C callee-saved)
4339
4340 Label throw_normal_exception;
4341 Label throw_termination_exception;
4342 Label throw_out_of_memory_exception;
4343
4344 // Call into the runtime system.
4345 GenerateCore(masm,
4346 &throw_normal_exception,
4347 &throw_termination_exception,
4348 &throw_out_of_memory_exception,
4349 false,
4350 false);
4351
4352 // Do space-specific GC and retry runtime call.
4353 GenerateCore(masm,
4354 &throw_normal_exception,
4355 &throw_termination_exception,
4356 &throw_out_of_memory_exception,
4357 true,
4358 false);
4359
4360 // Do full GC and retry runtime call one final time.
4361 Failure* failure = Failure::InternalError();
4362 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4363 GenerateCore(masm,
4364 &throw_normal_exception,
4365 &throw_termination_exception,
4366 &throw_out_of_memory_exception,
4367 true,
4368 true);
4369
4370 __ bind(&throw_out_of_memory_exception);
4371 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4372
4373 __ bind(&throw_termination_exception);
4374 GenerateThrowUncatchable(masm, TERMINATION);
4375
4376 __ bind(&throw_normal_exception);
4377 GenerateThrowTOS(masm);
4378}
4379
4380
4381void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4382 Label invoke, exit;
4383#ifdef ENABLE_LOGGING_AND_PROFILING
4384 Label not_outermost_js, not_outermost_js_2;
4385#endif
4386
4387 // Setup frame.
4388 __ push(ebp);
4389 __ mov(ebp, Operand(esp));
4390
4391 // Push marker in two places.
4392 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4393 __ push(Immediate(Smi::FromInt(marker))); // context slot
4394 __ push(Immediate(Smi::FromInt(marker))); // function slot
4395 // Save callee-saved registers (C calling conventions).
4396 __ push(edi);
4397 __ push(esi);
4398 __ push(ebx);
4399
4400 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004401 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004402 __ push(Operand::StaticVariable(c_entry_fp));
4403
4404#ifdef ENABLE_LOGGING_AND_PROFILING
4405 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004406 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4407 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004408 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4409 __ j(not_equal, &not_outermost_js);
4410 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004411 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4412 Label cont;
4413 __ jmp(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004414 __ bind(&not_outermost_js);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004415 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
4416 __ bind(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004417#endif
4418
4419 // Call a faked try-block that does the invoke.
4420 __ call(&invoke);
4421
4422 // Caught exception: Store result (exception) in the pending
4423 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004424 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4425 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004426 __ mov(Operand::StaticVariable(pending_exception), eax);
4427 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4428 __ jmp(&exit);
4429
4430 // Invoke: Link this frame into the handler chain.
4431 __ bind(&invoke);
4432 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4433
4434 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004435 ExternalReference the_hole_location =
4436 ExternalReference::the_hole_value_location(masm->isolate());
4437 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004438 __ mov(Operand::StaticVariable(pending_exception), edx);
4439
4440 // Fake a receiver (NULL).
4441 __ push(Immediate(0)); // receiver
4442
4443 // Invoke the function by calling through JS entry trampoline
4444 // builtin and pop the faked function when we return. Notice that we
4445 // cannot store a reference to the trampoline code directly in this
4446 // stub, because the builtin stubs may not have been generated yet.
4447 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004448 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004449 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004450 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004451 __ mov(edx, Immediate(construct_entry));
4452 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004453 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004454 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004455 __ mov(edx, Immediate(entry));
4456 }
4457 __ mov(edx, Operand(edx, 0)); // deref address
4458 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4459 __ call(Operand(edx));
4460
4461 // Unlink this frame from the handler chain.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004462 __ PopTryHandler();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004463
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004464 __ bind(&exit);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004465#ifdef ENABLE_LOGGING_AND_PROFILING
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004466 // Check if the current stack frame is marked as the outermost JS frame.
4467 __ pop(ebx);
4468 __ cmp(Operand(ebx),
4469 Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004470 __ j(not_equal, &not_outermost_js_2);
4471 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4472 __ bind(&not_outermost_js_2);
4473#endif
4474
4475 // Restore the top frame descriptor from the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004476 __ pop(Operand::StaticVariable(ExternalReference(
4477 Isolate::k_c_entry_fp_address,
4478 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004479
4480 // Restore callee-saved registers (C calling conventions).
4481 __ pop(ebx);
4482 __ pop(esi);
4483 __ pop(edi);
4484 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4485
4486 // Restore frame pointer and return.
4487 __ pop(ebp);
4488 __ ret(0);
4489}
4490
4491
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004492// Generate stub code for instanceof.
4493// This code can patch a call site inlined cache of the instance of check,
4494// which looks like this.
4495//
4496// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4497// 75 0a jne <some near label>
4498// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4499//
4500// If call site patching is requested the stack will have the delta from the
4501// return address to the cmp instruction just below the return address. This
4502// also means that call site patching can only take place with arguments in
4503// registers. TOS looks like this when call site patching is requested
4504//
4505// esp[0] : return address
4506// esp[4] : delta from return address to cmp instruction
4507//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004508void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004509 // Call site inlining and patching implies arguments in registers.
4510 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4511
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004512 // Fixed register usage throughout the stub.
4513 Register object = eax; // Object (lhs).
4514 Register map = ebx; // Map of the object.
4515 Register function = edx; // Function (rhs).
4516 Register prototype = edi; // Prototype of the function.
4517 Register scratch = ecx;
4518
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004519 // Constants describing the call site code to patch.
4520 static const int kDeltaToCmpImmediate = 2;
4521 static const int kDeltaToMov = 8;
4522 static const int kDeltaToMovImmediate = 9;
4523 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4524 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4525 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4526
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004527 ExternalReference roots_address =
4528 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004529
4530 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4531 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4532
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004533 // Get the object and function - they are always both needed.
4534 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004535 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004536 __ mov(object, Operand(esp, 2 * kPointerSize));
4537 __ mov(function, Operand(esp, 1 * kPointerSize));
4538 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004539
4540 // Check that the left hand is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004541 __ JumpIfSmi(object, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004542 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004543
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004544 // If there is a call site cache don't look in the global cache, but do the
4545 // real lookup and update the call site cache.
4546 if (!HasCallSiteInlineCheck()) {
4547 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004548 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004549 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4550 __ cmp(function,
4551 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004552 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004553 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4554 __ cmp(map, Operand::StaticArray(
4555 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004556 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004557 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4558 __ mov(eax, Operand::StaticArray(
4559 scratch, times_pointer_size, roots_address));
4560 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4561 __ bind(&miss);
4562 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004563
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004564 // Get the prototype of the function.
4565 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004566
4567 // Check that the function prototype is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004568 __ JumpIfSmi(prototype, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004569 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004570
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004571 // Update the global instanceof or call site inlined cache with the current
4572 // map and function. The cached answer will be set when it is known below.
4573 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004574 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4575 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4576 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4577 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4578 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004579 } else {
4580 // The constants for the code patching are based on no push instructions
4581 // at the call site.
4582 ASSERT(HasArgsInRegisters());
4583 // Get return address and delta to inlined map check.
4584 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4585 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4586 if (FLAG_debug_code) {
4587 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4588 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4589 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4590 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4591 }
4592 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4593 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004594
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004595 // Loop through the prototype chain of the object looking for the function
4596 // prototype.
4597 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004598 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004599 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004600 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004601 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004602 Factory* factory = masm->isolate()->factory();
4603 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004604 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004605 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4606 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004607 __ jmp(&loop);
4608
4609 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004610 if (!HasCallSiteInlineCheck()) {
4611 __ Set(eax, Immediate(0));
4612 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4613 __ mov(Operand::StaticArray(scratch,
4614 times_pointer_size, roots_address), eax);
4615 } else {
4616 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004617 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004618 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4619 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4620 if (FLAG_debug_code) {
4621 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4622 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4623 }
4624 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4625 if (!ReturnTrueFalseObject()) {
4626 __ Set(eax, Immediate(0));
4627 }
4628 }
4629 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004630
4631 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004632 if (!HasCallSiteInlineCheck()) {
4633 __ Set(eax, Immediate(Smi::FromInt(1)));
4634 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4635 __ mov(Operand::StaticArray(
4636 scratch, times_pointer_size, roots_address), eax);
4637 } else {
4638 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004639 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004640 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4641 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4642 if (FLAG_debug_code) {
4643 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4644 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4645 }
4646 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4647 if (!ReturnTrueFalseObject()) {
4648 __ Set(eax, Immediate(Smi::FromInt(1)));
4649 }
4650 }
4651 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004652
4653 Label object_not_null, object_not_null_or_smi;
4654 __ bind(&not_js_object);
4655 // Before null, smi and string value checks, check that the rhs is a function
4656 // as for a non-function rhs an exception needs to be thrown.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004657 __ JumpIfSmi(function, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004658 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004659 __ j(not_equal, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004660
4661 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004662 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004663 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004664 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004665 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004666
4667 __ bind(&object_not_null);
4668 // Smi values is not instance of anything.
whesse@chromium.org7b260152011-06-20 15:33:18 +00004669 __ JumpIfNotSmi(object, &object_not_null_or_smi);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004670 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004671 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004672
4673 __ bind(&object_not_null_or_smi);
4674 // String values is not instance of anything.
4675 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4676 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004677 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004678 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004679
4680 // Slow-case: Go through the JavaScript implementation.
4681 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004682 if (!ReturnTrueFalseObject()) {
4683 // Tail call the builtin which returns 0 or 1.
4684 if (HasArgsInRegisters()) {
4685 // Push arguments below return address.
4686 __ pop(scratch);
4687 __ push(object);
4688 __ push(function);
4689 __ push(scratch);
4690 }
4691 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4692 } else {
4693 // Call the builtin and convert 0/1 to true/false.
4694 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004695 __ push(object);
4696 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004697 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4698 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004699 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004700 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004701 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004702 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004703 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004704 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004705 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004706 __ bind(&done);
4707 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004708 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004709}
4710
4711
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004712Register InstanceofStub::left() { return eax; }
4713
4714
4715Register InstanceofStub::right() { return edx; }
4716
4717
ricow@chromium.org65fae842010-08-25 15:26:24 +00004718int CompareStub::MinorKey() {
4719 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4720 // stubs the never NaN NaN condition is only taken into account if the
4721 // condition is equals.
4722 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4723 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4724 return ConditionField::encode(static_cast<unsigned>(cc_))
4725 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4726 | StrictField::encode(strict_)
4727 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004728 | IncludeNumberCompareField::encode(include_number_compare_)
4729 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004730}
4731
4732
4733// Unfortunately you have to run without snapshots to see most of these
4734// names in the profile since most compare stubs end up in the snapshot.
4735const char* CompareStub::GetName() {
4736 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4737
4738 if (name_ != NULL) return name_;
4739 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004740 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4741 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004742 if (name_ == NULL) return "OOM";
4743
4744 const char* cc_name;
4745 switch (cc_) {
4746 case less: cc_name = "LT"; break;
4747 case greater: cc_name = "GT"; break;
4748 case less_equal: cc_name = "LE"; break;
4749 case greater_equal: cc_name = "GE"; break;
4750 case equal: cc_name = "EQ"; break;
4751 case not_equal: cc_name = "NE"; break;
4752 default: cc_name = "UnknownCondition"; break;
4753 }
4754
4755 const char* strict_name = "";
4756 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4757 strict_name = "_STRICT";
4758 }
4759
4760 const char* never_nan_nan_name = "";
4761 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4762 never_nan_nan_name = "_NO_NAN";
4763 }
4764
4765 const char* include_number_compare_name = "";
4766 if (!include_number_compare_) {
4767 include_number_compare_name = "_NO_NUMBER";
4768 }
4769
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004770 const char* include_smi_compare_name = "";
4771 if (!include_smi_compare_) {
4772 include_smi_compare_name = "_NO_SMI";
4773 }
4774
ricow@chromium.org65fae842010-08-25 15:26:24 +00004775 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004776 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004777 cc_name,
4778 strict_name,
4779 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004780 include_number_compare_name,
4781 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004782 return name_;
4783}
4784
4785
4786// -------------------------------------------------------------------------
4787// StringCharCodeAtGenerator
4788
4789void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4790 Label flat_string;
4791 Label ascii_string;
4792 Label got_char_code;
4793
4794 // If the receiver is a smi trigger the non-string case.
4795 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004796 __ JumpIfSmi(object_, receiver_not_string_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004797
4798 // Fetch the instance type of the receiver into result register.
4799 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4800 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4801 // If the receiver is not a string trigger the non-string case.
4802 __ test(result_, Immediate(kIsNotStringMask));
4803 __ j(not_zero, receiver_not_string_);
4804
4805 // If the index is non-smi trigger the non-smi case.
4806 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004807 __ JumpIfNotSmi(index_, &index_not_smi_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004808
4809 // Put smi-tagged index into scratch register.
4810 __ mov(scratch_, index_);
4811 __ bind(&got_smi_index_);
4812
4813 // Check for index out of range.
4814 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4815 __ j(above_equal, index_out_of_range_);
4816
4817 // We need special handling for non-flat strings.
4818 STATIC_ASSERT(kSeqStringTag == 0);
4819 __ test(result_, Immediate(kStringRepresentationMask));
4820 __ j(zero, &flat_string);
4821
4822 // Handle non-flat strings.
4823 __ test(result_, Immediate(kIsConsStringMask));
4824 __ j(zero, &call_runtime_);
4825
4826 // ConsString.
4827 // Check whether the right hand side is the empty string (i.e. if
4828 // this is really a flat string in a cons string). If that is not
4829 // the case we would rather go to the runtime system now to flatten
4830 // the string.
4831 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004832 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004833 __ j(not_equal, &call_runtime_);
4834 // Get the first of the two strings and load its instance type.
4835 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4836 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4837 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4838 // If the first cons component is also non-flat, then go to runtime.
4839 STATIC_ASSERT(kSeqStringTag == 0);
4840 __ test(result_, Immediate(kStringRepresentationMask));
4841 __ j(not_zero, &call_runtime_);
4842
4843 // Check for 1-byte or 2-byte string.
4844 __ bind(&flat_string);
4845 STATIC_ASSERT(kAsciiStringTag != 0);
4846 __ test(result_, Immediate(kStringEncodingMask));
4847 __ j(not_zero, &ascii_string);
4848
4849 // 2-byte string.
4850 // Load the 2-byte character code into the result register.
4851 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4852 __ movzx_w(result_, FieldOperand(object_,
4853 scratch_, times_1, // Scratch is smi-tagged.
4854 SeqTwoByteString::kHeaderSize));
4855 __ jmp(&got_char_code);
4856
4857 // ASCII string.
4858 // Load the byte into the result register.
4859 __ bind(&ascii_string);
4860 __ SmiUntag(scratch_);
4861 __ movzx_b(result_, FieldOperand(object_,
4862 scratch_, times_1,
4863 SeqAsciiString::kHeaderSize));
4864 __ bind(&got_char_code);
4865 __ SmiTag(result_);
4866 __ bind(&exit_);
4867}
4868
4869
4870void StringCharCodeAtGenerator::GenerateSlow(
4871 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4872 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4873
4874 // Index is not a smi.
4875 __ bind(&index_not_smi_);
4876 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004877 __ CheckMap(index_,
4878 masm->isolate()->factory()->heap_number_map(),
4879 index_not_number_,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004880 DONT_DO_SMI_CHECK);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004881 call_helper.BeforeCall(masm);
4882 __ push(object_);
4883 __ push(index_);
4884 __ push(index_); // Consumed by runtime conversion function.
4885 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4886 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4887 } else {
4888 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4889 // NumberToSmi discards numbers that are not exact integers.
4890 __ CallRuntime(Runtime::kNumberToSmi, 1);
4891 }
4892 if (!scratch_.is(eax)) {
4893 // Save the conversion result before the pop instructions below
4894 // have a chance to overwrite it.
4895 __ mov(scratch_, eax);
4896 }
4897 __ pop(index_);
4898 __ pop(object_);
4899 // Reload the instance type.
4900 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4901 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4902 call_helper.AfterCall(masm);
4903 // If index is still not a smi, it must be out of range.
4904 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004905 __ JumpIfNotSmi(scratch_, index_out_of_range_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004906 // Otherwise, return to the fast path.
4907 __ jmp(&got_smi_index_);
4908
4909 // Call runtime. We get here when the receiver is a string and the
4910 // index is a number, but the code of getting the actual character
4911 // is too complex (e.g., when the string needs to be flattened).
4912 __ bind(&call_runtime_);
4913 call_helper.BeforeCall(masm);
4914 __ push(object_);
4915 __ push(index_);
4916 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4917 if (!result_.is(eax)) {
4918 __ mov(result_, eax);
4919 }
4920 call_helper.AfterCall(masm);
4921 __ jmp(&exit_);
4922
4923 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4924}
4925
4926
4927// -------------------------------------------------------------------------
4928// StringCharFromCodeGenerator
4929
4930void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4931 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4932 STATIC_ASSERT(kSmiTag == 0);
4933 STATIC_ASSERT(kSmiShiftSize == 0);
4934 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4935 __ test(code_,
4936 Immediate(kSmiTagMask |
4937 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004938 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004939
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004940 Factory* factory = masm->isolate()->factory();
4941 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004942 STATIC_ASSERT(kSmiTag == 0);
4943 STATIC_ASSERT(kSmiTagSize == 1);
4944 STATIC_ASSERT(kSmiShiftSize == 0);
4945 // At this point code register contains smi tagged ascii char code.
4946 __ mov(result_, FieldOperand(result_,
4947 code_, times_half_pointer_size,
4948 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004949 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004950 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004951 __ bind(&exit_);
4952}
4953
4954
4955void StringCharFromCodeGenerator::GenerateSlow(
4956 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4957 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4958
4959 __ bind(&slow_case_);
4960 call_helper.BeforeCall(masm);
4961 __ push(code_);
4962 __ CallRuntime(Runtime::kCharFromCode, 1);
4963 if (!result_.is(eax)) {
4964 __ mov(result_, eax);
4965 }
4966 call_helper.AfterCall(masm);
4967 __ jmp(&exit_);
4968
4969 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4970}
4971
4972
4973// -------------------------------------------------------------------------
4974// StringCharAtGenerator
4975
4976void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4977 char_code_at_generator_.GenerateFast(masm);
4978 char_from_code_generator_.GenerateFast(masm);
4979}
4980
4981
4982void StringCharAtGenerator::GenerateSlow(
4983 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4984 char_code_at_generator_.GenerateSlow(masm, call_helper);
4985 char_from_code_generator_.GenerateSlow(masm, call_helper);
4986}
4987
4988
4989void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004990 Label string_add_runtime, call_builtin;
4991 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004992
4993 // Load the two arguments.
4994 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4995 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4996
4997 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004998 if (flags_ == NO_STRING_ADD_FLAGS) {
whesse@chromium.org7b260152011-06-20 15:33:18 +00004999 __ JumpIfSmi(eax, &string_add_runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005000 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
5001 __ j(above_equal, &string_add_runtime);
5002
5003 // First argument is a a string, test second.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005004 __ JumpIfSmi(edx, &string_add_runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005005 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
5006 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005007 } else {
5008 // Here at least one of the arguments is definitely a string.
5009 // We convert the one that is not known to be a string.
5010 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5011 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5012 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5013 &call_builtin);
5014 builtin_id = Builtins::STRING_ADD_RIGHT;
5015 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5016 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5017 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5018 &call_builtin);
5019 builtin_id = Builtins::STRING_ADD_LEFT;
5020 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00005021 }
5022
5023 // Both arguments are strings.
5024 // eax: first string
5025 // edx: second string
5026 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005027 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005028 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5029 STATIC_ASSERT(kSmiTag == 0);
5030 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005031 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005032 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005033 Counters* counters = masm->isolate()->counters();
5034 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005035 __ ret(2 * kPointerSize);
5036 __ bind(&second_not_zero_length);
5037 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5038 STATIC_ASSERT(kSmiTag == 0);
5039 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005040 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005041 // First string is empty, result is second string which is in edx.
5042 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005043 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005044 __ ret(2 * kPointerSize);
5045
5046 // Both strings are non-empty.
5047 // eax: first string
5048 // ebx: length of first string as a smi
5049 // ecx: length of second string as a smi
5050 // edx: second string
5051 // Look at the length of the result of adding the two strings.
5052 Label string_add_flat_result, longer_than_two;
5053 __ bind(&both_not_zero_length);
5054 __ add(ebx, Operand(ecx));
5055 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5056 // Handle exceptionally long strings in the runtime system.
5057 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005058 // Use the symbol table when adding two one character strings, as it
5059 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005060 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
5061 __ j(not_equal, &longer_than_two);
5062
5063 // Check that both strings are non-external ascii strings.
5064 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
5065 &string_add_runtime);
5066
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005067 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005068 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5069 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5070
5071 // Try to lookup two character string in symbol table. If it is not found
5072 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005073 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005074 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005075 masm, ebx, ecx, eax, edx, edi,
5076 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005077 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005078 __ ret(2 * kPointerSize);
5079
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005080 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00005081 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005082 // Reload the arguments.
5083 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5084 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5085 // Get the two characters forming the new string.
5086 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5087 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5088 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005089 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005090 __ AllocateAsciiString(eax, // Result.
5091 2, // Length.
5092 edi, // Scratch 1.
5093 edx, // Scratch 2.
5094 &string_add_runtime);
5095 // Pack both characters in ebx.
5096 __ shl(ecx, kBitsPerByte);
5097 __ or_(ebx, Operand(ecx));
5098 // Set the characters in the new string.
5099 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005100 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005101 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005102
5103 __ bind(&longer_than_two);
5104 // Check if resulting string will be flat.
5105 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
5106 __ j(below, &string_add_flat_result);
5107
5108 // If result is not supposed to be flat allocate a cons string object. If both
5109 // strings are ascii the result is an ascii cons string.
5110 Label non_ascii, allocated, ascii_data;
5111 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5112 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5113 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5114 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5115 __ and_(ecx, Operand(edi));
5116 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5117 __ test(ecx, Immediate(kAsciiStringTag));
5118 __ j(zero, &non_ascii);
5119 __ bind(&ascii_data);
5120 // Allocate an acsii cons string.
5121 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5122 __ bind(&allocated);
5123 // Fill the fields of the cons string.
5124 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5125 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5126 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5127 Immediate(String::kEmptyHashField));
5128 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5129 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5130 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005131 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005132 __ ret(2 * kPointerSize);
5133 __ bind(&non_ascii);
5134 // At least one of the strings is two-byte. Check whether it happens
5135 // to contain only ascii characters.
5136 // ecx: first instance type AND second instance type.
5137 // edi: second instance type.
5138 __ test(ecx, Immediate(kAsciiDataHintMask));
5139 __ j(not_zero, &ascii_data);
5140 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5141 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5142 __ xor_(edi, Operand(ecx));
5143 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5144 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5145 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5146 __ j(equal, &ascii_data);
5147 // Allocate a two byte cons string.
5148 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5149 __ jmp(&allocated);
5150
5151 // Handle creating a flat result. First check that both strings are not
5152 // external strings.
5153 // eax: first string
5154 // ebx: length of resulting flat string as a smi
5155 // edx: second string
5156 __ bind(&string_add_flat_result);
5157 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5158 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5159 __ and_(ecx, kStringRepresentationMask);
5160 __ cmp(ecx, kExternalStringTag);
5161 __ j(equal, &string_add_runtime);
5162 __ mov(ecx, FieldOperand(edx, 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 // Now check if both strings are ascii strings.
5168 // eax: first string
5169 // ebx: length of resulting flat string as a smi
5170 // edx: second string
5171 Label non_ascii_string_add_flat_result;
5172 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5173 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5174 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5175 __ j(zero, &non_ascii_string_add_flat_result);
5176 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5177 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5178 __ j(zero, &string_add_runtime);
5179
ricow@chromium.org65fae842010-08-25 15:26:24 +00005180 // Both strings are ascii strings. As they are short they are both flat.
5181 // ebx: length of resulting flat string as a smi
5182 __ SmiUntag(ebx);
5183 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5184 // eax: result string
5185 __ mov(ecx, eax);
5186 // Locate first character of result.
5187 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5188 // Load first argument and locate first character.
5189 __ mov(edx, Operand(esp, 2 * kPointerSize));
5190 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5191 __ SmiUntag(edi);
5192 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5193 // eax: result string
5194 // ecx: first character of result
5195 // edx: first char of first argument
5196 // edi: length of first argument
5197 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5198 // Load second argument and locate first character.
5199 __ mov(edx, Operand(esp, 1 * kPointerSize));
5200 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5201 __ SmiUntag(edi);
5202 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5203 // eax: result string
5204 // ecx: next character of result
5205 // edx: first char of second argument
5206 // edi: length of second argument
5207 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005208 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005209 __ ret(2 * kPointerSize);
5210
5211 // Handle creating a flat two byte result.
5212 // eax: first string - known to be two byte
5213 // ebx: length of resulting flat string as a smi
5214 // edx: second string
5215 __ bind(&non_ascii_string_add_flat_result);
5216 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5217 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5218 __ j(not_zero, &string_add_runtime);
5219 // Both strings are two byte strings. As they are short they are both
5220 // flat.
5221 __ SmiUntag(ebx);
5222 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5223 // eax: result string
5224 __ mov(ecx, eax);
5225 // Locate first character of result.
5226 __ add(Operand(ecx),
5227 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5228 // Load first argument and locate first character.
5229 __ mov(edx, Operand(esp, 2 * kPointerSize));
5230 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5231 __ SmiUntag(edi);
5232 __ add(Operand(edx),
5233 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5234 // eax: result string
5235 // ecx: first character of result
5236 // edx: first char of first argument
5237 // edi: length of first argument
5238 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5239 // Load second argument and locate first character.
5240 __ mov(edx, Operand(esp, 1 * kPointerSize));
5241 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5242 __ SmiUntag(edi);
5243 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5244 // eax: result string
5245 // ecx: next character of result
5246 // edx: first char of second argument
5247 // edi: length of second argument
5248 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005249 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005250 __ ret(2 * kPointerSize);
5251
5252 // Just jump to runtime to add the two strings.
5253 __ bind(&string_add_runtime);
5254 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005255
5256 if (call_builtin.is_linked()) {
5257 __ bind(&call_builtin);
5258 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5259 }
5260}
5261
5262
5263void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5264 int stack_offset,
5265 Register arg,
5266 Register scratch1,
5267 Register scratch2,
5268 Register scratch3,
5269 Label* slow) {
5270 // First check if the argument is already a string.
5271 Label not_string, done;
whesse@chromium.org7b260152011-06-20 15:33:18 +00005272 __ JumpIfSmi(arg, &not_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005273 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5274 __ j(below, &done);
5275
5276 // Check the number to string cache.
5277 Label not_cached;
5278 __ bind(&not_string);
5279 // Puts the cached result into scratch1.
5280 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5281 arg,
5282 scratch1,
5283 scratch2,
5284 scratch3,
5285 false,
5286 &not_cached);
5287 __ mov(arg, scratch1);
5288 __ mov(Operand(esp, stack_offset), arg);
5289 __ jmp(&done);
5290
5291 // Check if the argument is a safe string wrapper.
5292 __ bind(&not_cached);
whesse@chromium.org7b260152011-06-20 15:33:18 +00005293 __ JumpIfSmi(arg, slow);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005294 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5295 __ j(not_equal, slow);
5296 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5297 1 << Map::kStringWrapperSafeForDefaultValueOf);
5298 __ j(zero, slow);
5299 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5300 __ mov(Operand(esp, stack_offset), arg);
5301
5302 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005303}
5304
5305
5306void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5307 Register dest,
5308 Register src,
5309 Register count,
5310 Register scratch,
5311 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005312 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005313 __ bind(&loop);
5314 // This loop just copies one character at a time, as it is only used for very
5315 // short strings.
5316 if (ascii) {
5317 __ mov_b(scratch, Operand(src, 0));
5318 __ mov_b(Operand(dest, 0), scratch);
5319 __ add(Operand(src), Immediate(1));
5320 __ add(Operand(dest), Immediate(1));
5321 } else {
5322 __ mov_w(scratch, Operand(src, 0));
5323 __ mov_w(Operand(dest, 0), scratch);
5324 __ add(Operand(src), Immediate(2));
5325 __ add(Operand(dest), Immediate(2));
5326 }
5327 __ sub(Operand(count), Immediate(1));
5328 __ j(not_zero, &loop);
5329}
5330
5331
5332void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5333 Register dest,
5334 Register src,
5335 Register count,
5336 Register scratch,
5337 bool ascii) {
5338 // Copy characters using rep movs of doublewords.
5339 // The destination is aligned on a 4 byte boundary because we are
5340 // copying to the beginning of a newly allocated string.
5341 ASSERT(dest.is(edi)); // rep movs destination
5342 ASSERT(src.is(esi)); // rep movs source
5343 ASSERT(count.is(ecx)); // rep movs count
5344 ASSERT(!scratch.is(dest));
5345 ASSERT(!scratch.is(src));
5346 ASSERT(!scratch.is(count));
5347
5348 // Nothing to do for zero characters.
5349 Label done;
5350 __ test(count, Operand(count));
5351 __ j(zero, &done);
5352
5353 // Make count the number of bytes to copy.
5354 if (!ascii) {
5355 __ shl(count, 1);
5356 }
5357
5358 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005359 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005360 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005361 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005362
5363 // Copy from edi to esi using rep movs instruction.
5364 __ mov(scratch, count);
5365 __ sar(count, 2); // Number of doublewords to copy.
5366 __ cld();
5367 __ rep_movs();
5368
5369 // Find number of bytes left.
5370 __ mov(count, scratch);
5371 __ and_(count, 3);
5372
5373 // Check if there are more bytes to copy.
5374 __ bind(&last_bytes);
5375 __ test(count, Operand(count));
5376 __ j(zero, &done);
5377
5378 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005379 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005380 __ bind(&loop);
5381 __ mov_b(scratch, Operand(src, 0));
5382 __ mov_b(Operand(dest, 0), scratch);
5383 __ add(Operand(src), Immediate(1));
5384 __ add(Operand(dest), Immediate(1));
5385 __ sub(Operand(count), Immediate(1));
5386 __ j(not_zero, &loop);
5387
5388 __ bind(&done);
5389}
5390
5391
5392void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5393 Register c1,
5394 Register c2,
5395 Register scratch1,
5396 Register scratch2,
5397 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005398 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005399 Label* not_found) {
5400 // Register scratch3 is the general scratch register in this function.
5401 Register scratch = scratch3;
5402
5403 // Make sure that both characters are not digits as such strings has a
5404 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005405 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005406 __ mov(scratch, c1);
5407 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5408 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005409 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005410 __ mov(scratch, c2);
5411 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5412 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005413 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005414
5415 __ bind(&not_array_index);
5416 // Calculate the two character string hash.
5417 Register hash = scratch1;
5418 GenerateHashInit(masm, hash, c1, scratch);
5419 GenerateHashAddCharacter(masm, hash, c2, scratch);
5420 GenerateHashGetHash(masm, hash, scratch);
5421
5422 // Collect the two characters in a register.
5423 Register chars = c1;
5424 __ shl(c2, kBitsPerByte);
5425 __ or_(chars, Operand(c2));
5426
5427 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5428 // hash: hash of two character string.
5429
5430 // Load the symbol table.
5431 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005432 ExternalReference roots_address =
5433 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005434 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5435 __ mov(symbol_table,
5436 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5437
5438 // Calculate capacity mask from the symbol table capacity.
5439 Register mask = scratch2;
5440 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5441 __ SmiUntag(mask);
5442 __ sub(Operand(mask), Immediate(1));
5443
5444 // Registers
5445 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5446 // hash: hash of two character string
5447 // symbol_table: symbol table
5448 // mask: capacity mask
5449 // scratch: -
5450
5451 // Perform a number of probes in the symbol table.
5452 static const int kProbes = 4;
5453 Label found_in_symbol_table;
5454 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5455 for (int i = 0; i < kProbes; i++) {
5456 // Calculate entry in symbol table.
5457 __ mov(scratch, hash);
5458 if (i > 0) {
5459 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5460 }
5461 __ and_(scratch, Operand(mask));
5462
5463 // Load the entry from the symbol table.
5464 Register candidate = scratch; // Scratch register contains candidate.
5465 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5466 __ mov(candidate,
5467 FieldOperand(symbol_table,
5468 scratch,
5469 times_pointer_size,
5470 SymbolTable::kElementsStartOffset));
5471
5472 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005473 Factory* factory = masm->isolate()->factory();
5474 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005475 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005476 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005477 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005478
5479 // If length is not 2 the string is not a candidate.
5480 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5481 Immediate(Smi::FromInt(2)));
5482 __ j(not_equal, &next_probe[i]);
5483
5484 // As we are out of registers save the mask on the stack and use that
5485 // register as a temporary.
5486 __ push(mask);
5487 Register temp = mask;
5488
5489 // Check that the candidate is a non-external ascii string.
5490 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5491 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5492 __ JumpIfInstanceTypeIsNotSequentialAscii(
5493 temp, temp, &next_probe_pop_mask[i]);
5494
5495 // Check if the two characters match.
5496 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5497 __ and_(temp, 0x0000ffff);
5498 __ cmp(chars, Operand(temp));
5499 __ j(equal, &found_in_symbol_table);
5500 __ bind(&next_probe_pop_mask[i]);
5501 __ pop(mask);
5502 __ bind(&next_probe[i]);
5503 }
5504
5505 // No matching 2 character string found by probing.
5506 __ jmp(not_found);
5507
5508 // Scratch register contains result when we fall through to here.
5509 Register result = scratch;
5510 __ bind(&found_in_symbol_table);
5511 __ pop(mask); // Pop saved mask from the stack.
5512 if (!result.is(eax)) {
5513 __ mov(eax, result);
5514 }
5515}
5516
5517
5518void StringHelper::GenerateHashInit(MacroAssembler* masm,
5519 Register hash,
5520 Register character,
5521 Register scratch) {
5522 // hash = character + (character << 10);
5523 __ mov(hash, character);
5524 __ shl(hash, 10);
5525 __ add(hash, Operand(character));
5526 // hash ^= hash >> 6;
5527 __ mov(scratch, hash);
5528 __ sar(scratch, 6);
5529 __ xor_(hash, Operand(scratch));
5530}
5531
5532
5533void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5534 Register hash,
5535 Register character,
5536 Register scratch) {
5537 // hash += character;
5538 __ add(hash, Operand(character));
5539 // hash += hash << 10;
5540 __ mov(scratch, hash);
5541 __ shl(scratch, 10);
5542 __ add(hash, Operand(scratch));
5543 // hash ^= hash >> 6;
5544 __ mov(scratch, hash);
5545 __ sar(scratch, 6);
5546 __ xor_(hash, Operand(scratch));
5547}
5548
5549
5550void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5551 Register hash,
5552 Register scratch) {
5553 // hash += hash << 3;
5554 __ mov(scratch, hash);
5555 __ shl(scratch, 3);
5556 __ add(hash, Operand(scratch));
5557 // hash ^= hash >> 11;
5558 __ mov(scratch, hash);
5559 __ sar(scratch, 11);
5560 __ xor_(hash, Operand(scratch));
5561 // hash += hash << 15;
5562 __ mov(scratch, hash);
5563 __ shl(scratch, 15);
5564 __ add(hash, Operand(scratch));
5565
5566 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005567 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005568 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005569 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005570 __ mov(hash, Immediate(27));
5571 __ bind(&hash_not_zero);
5572}
5573
5574
5575void SubStringStub::Generate(MacroAssembler* masm) {
5576 Label runtime;
5577
5578 // Stack frame on entry.
5579 // esp[0]: return address
5580 // esp[4]: to
5581 // esp[8]: from
5582 // esp[12]: string
5583
5584 // Make sure first argument is a string.
5585 __ mov(eax, Operand(esp, 3 * kPointerSize));
5586 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00005587 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005588 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5589 __ j(NegateCondition(is_string), &runtime);
5590
5591 // eax: string
5592 // ebx: instance type
5593
5594 // Calculate length of sub string using the smi values.
5595 Label result_longer_than_two;
5596 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005597 __ JumpIfNotSmi(ecx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005598 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00005599 __ JumpIfNotSmi(edx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005600 __ sub(ecx, Operand(edx));
5601 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5602 Label return_eax;
5603 __ j(equal, &return_eax);
5604 // Special handling of sub-strings of length 1 and 2. One character strings
5605 // are handled in the runtime system (looked up in the single character
5606 // cache). Two character strings are looked for in the symbol cache.
5607 __ SmiUntag(ecx); // Result length is no longer smi.
5608 __ cmp(ecx, 2);
5609 __ j(greater, &result_longer_than_two);
5610 __ j(less, &runtime);
5611
5612 // Sub string of length 2 requested.
5613 // eax: string
5614 // ebx: instance type
5615 // ecx: sub string length (value is 2)
5616 // edx: from index (smi)
5617 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5618
5619 // Get the two characters forming the sub string.
5620 __ SmiUntag(edx); // From index is no longer smi.
5621 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5622 __ movzx_b(ecx,
5623 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5624
5625 // Try to lookup two character string in symbol table.
5626 Label make_two_character_string;
5627 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005628 masm, ebx, ecx, eax, edx, edi,
5629 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005630 __ ret(3 * kPointerSize);
5631
5632 __ bind(&make_two_character_string);
5633 // Setup registers for allocating the two character string.
5634 __ mov(eax, Operand(esp, 3 * kPointerSize));
5635 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5636 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5637 __ Set(ecx, Immediate(2));
5638
5639 __ bind(&result_longer_than_two);
5640 // eax: string
5641 // ebx: instance type
5642 // ecx: result string length
5643 // Check for flat ascii string
5644 Label non_ascii_flat;
5645 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5646
5647 // Allocate the result.
5648 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5649
5650 // eax: result string
5651 // ecx: result string length
5652 __ mov(edx, esi); // esi used by following code.
5653 // Locate first character of result.
5654 __ mov(edi, eax);
5655 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5656 // Load string argument and locate character of sub string start.
5657 __ mov(esi, Operand(esp, 3 * kPointerSize));
5658 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5659 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5660 __ SmiUntag(ebx);
5661 __ add(esi, Operand(ebx));
5662
5663 // eax: result string
5664 // ecx: result length
5665 // edx: original value of esi
5666 // edi: first character of result
5667 // esi: character of sub string start
5668 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5669 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005670 Counters* counters = masm->isolate()->counters();
5671 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005672 __ ret(3 * kPointerSize);
5673
5674 __ bind(&non_ascii_flat);
5675 // eax: string
5676 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5677 // ecx: result string length
5678 // Check for flat two byte string
5679 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5680 __ j(not_equal, &runtime);
5681
5682 // Allocate the result.
5683 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5684
5685 // eax: result string
5686 // ecx: result string length
5687 __ mov(edx, esi); // esi used by following code.
5688 // Locate first character of result.
5689 __ mov(edi, eax);
5690 __ add(Operand(edi),
5691 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5692 // Load string argument and locate character of sub string start.
5693 __ mov(esi, Operand(esp, 3 * kPointerSize));
5694 __ add(Operand(esi),
5695 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5696 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5697 // As from is a smi it is 2 times the value which matches the size of a two
5698 // byte character.
5699 STATIC_ASSERT(kSmiTag == 0);
5700 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5701 __ add(esi, Operand(ebx));
5702
5703 // eax: result string
5704 // ecx: result length
5705 // edx: original value of esi
5706 // edi: first character of result
5707 // esi: character of sub string start
5708 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5709 __ mov(esi, edx); // Restore esi.
5710
5711 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005712 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005713 __ ret(3 * kPointerSize);
5714
5715 // Just jump to runtime to create the sub string.
5716 __ bind(&runtime);
5717 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5718}
5719
5720
lrn@chromium.org1c092762011-05-09 09:42:16 +00005721void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5722 Register left,
5723 Register right,
5724 Register scratch1,
5725 Register scratch2) {
5726 Register length = scratch1;
5727
5728 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005729 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005730 __ mov(length, FieldOperand(left, String::kLengthOffset));
5731 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005732 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005733 __ bind(&strings_not_equal);
5734 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5735 __ ret(0);
5736
5737 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005738 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005739 __ bind(&check_zero_length);
5740 STATIC_ASSERT(kSmiTag == 0);
5741 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005742 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005743 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5744 __ ret(0);
5745
5746 // Compare characters.
5747 __ bind(&compare_chars);
5748 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005749 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005750
5751 // Characters are equal.
5752 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5753 __ ret(0);
5754}
5755
5756
ricow@chromium.org65fae842010-08-25 15:26:24 +00005757void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5758 Register left,
5759 Register right,
5760 Register scratch1,
5761 Register scratch2,
5762 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005763 Counters* counters = masm->isolate()->counters();
5764 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005765
5766 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005767 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005768 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5769 __ mov(scratch3, scratch1);
5770 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5771
5772 Register length_delta = scratch3;
5773
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005774 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005775 // Right string is shorter. Change scratch1 to be length of right string.
5776 __ sub(scratch1, Operand(length_delta));
5777 __ bind(&left_shorter);
5778
5779 Register min_length = scratch1;
5780
5781 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005782 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005783 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005784 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005785
lrn@chromium.org1c092762011-05-09 09:42:16 +00005786 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005787 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005788 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005789 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005790
5791 // Compare lengths - strings up to min-length are equal.
5792 __ bind(&compare_lengths);
5793 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005794 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005795
5796 // Result is EQUAL.
5797 STATIC_ASSERT(EQUAL == 0);
5798 STATIC_ASSERT(kSmiTag == 0);
5799 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5800 __ ret(0);
5801
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005802 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005803 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005804 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005805
5806 // Result is LESS.
5807 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5808 __ ret(0);
5809
5810 // Result is GREATER.
5811 __ bind(&result_greater);
5812 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5813 __ ret(0);
5814}
5815
5816
lrn@chromium.org1c092762011-05-09 09:42:16 +00005817void StringCompareStub::GenerateAsciiCharsCompareLoop(
5818 MacroAssembler* masm,
5819 Register left,
5820 Register right,
5821 Register length,
5822 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005823 Label* chars_not_equal,
5824 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005825 // Change index to run from -length to -1 by adding length to string
5826 // start. This means that loop ends when index reaches zero, which
5827 // doesn't need an additional compare.
5828 __ SmiUntag(length);
5829 __ lea(left,
5830 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5831 __ lea(right,
5832 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5833 __ neg(length);
5834 Register index = length; // index = -length;
5835
5836 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005837 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005838 __ bind(&loop);
5839 __ mov_b(scratch, Operand(left, index, times_1, 0));
5840 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005841 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005842 __ add(Operand(index), Immediate(1));
5843 __ j(not_zero, &loop);
5844}
5845
5846
ricow@chromium.org65fae842010-08-25 15:26:24 +00005847void StringCompareStub::Generate(MacroAssembler* masm) {
5848 Label runtime;
5849
5850 // Stack frame on entry.
5851 // esp[0]: return address
5852 // esp[4]: right string
5853 // esp[8]: left string
5854
5855 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5856 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5857
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005858 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005859 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005860 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005861 STATIC_ASSERT(EQUAL == 0);
5862 STATIC_ASSERT(kSmiTag == 0);
5863 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005864 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005865 __ ret(2 * kPointerSize);
5866
5867 __ bind(&not_same);
5868
5869 // Check that both objects are sequential ascii strings.
5870 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5871
5872 // Compare flat ascii strings.
5873 // Drop arguments from the stack.
5874 __ pop(ecx);
5875 __ add(Operand(esp), Immediate(2 * kPointerSize));
5876 __ push(ecx);
5877 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5878
5879 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5880 // tagged as a small integer.
5881 __ bind(&runtime);
5882 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5883}
5884
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005885
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005886void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5887 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005888 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005889 __ mov(ecx, Operand(edx));
5890 __ or_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005891 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005892
5893 if (GetCondition() == equal) {
5894 // For equality we do not care about the sign of the result.
5895 __ sub(eax, Operand(edx));
5896 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005897 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005898 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005899 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005900 // Correct sign of result in case of overflow.
5901 __ not_(edx);
5902 __ bind(&done);
5903 __ mov(eax, edx);
5904 }
5905 __ ret(0);
5906
5907 __ bind(&miss);
5908 GenerateMiss(masm);
5909}
5910
5911
5912void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5913 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5914
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005915 Label generic_stub;
5916 Label unordered;
5917 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005918 __ mov(ecx, Operand(edx));
5919 __ and_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005920 __ JumpIfSmi(ecx, &generic_stub, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005921
5922 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005923 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005924 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005925 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005926
5927 // Inlining the double comparison and falling back to the general compare
5928 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005929 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005930 CpuFeatures::Scope scope1(SSE2);
5931 CpuFeatures::Scope scope2(CMOV);
5932
5933 // Load left and right operand
5934 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5935 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5936
5937 // Compare operands
5938 __ ucomisd(xmm0, xmm1);
5939
5940 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005941 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005942
5943 // Return a result of -1, 0, or 1, based on EFLAGS.
5944 // Performing mov, because xor would destroy the flag register.
5945 __ mov(eax, 0); // equal
5946 __ mov(ecx, Immediate(Smi::FromInt(1)));
5947 __ cmov(above, eax, Operand(ecx));
5948 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5949 __ cmov(below, eax, Operand(ecx));
5950 __ ret(0);
5951
5952 __ bind(&unordered);
5953 }
5954
5955 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5956 __ bind(&generic_stub);
5957 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5958
5959 __ bind(&miss);
5960 GenerateMiss(masm);
5961}
5962
5963
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005964void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5965 ASSERT(state_ == CompareIC::SYMBOLS);
5966 ASSERT(GetCondition() == equal);
5967
5968 // Registers containing left and right operands respectively.
5969 Register left = edx;
5970 Register right = eax;
5971 Register tmp1 = ecx;
5972 Register tmp2 = ebx;
5973
5974 // Check that both operands are heap objects.
5975 Label miss;
5976 __ mov(tmp1, Operand(left));
5977 STATIC_ASSERT(kSmiTag == 0);
5978 __ and_(tmp1, Operand(right));
whesse@chromium.org7b260152011-06-20 15:33:18 +00005979 __ JumpIfSmi(tmp1, &miss, Label::kNear);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005980
5981 // Check that both operands are symbols.
5982 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5983 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5984 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5985 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5986 STATIC_ASSERT(kSymbolTag != 0);
5987 __ and_(tmp1, Operand(tmp2));
5988 __ test(tmp1, Immediate(kIsSymbolMask));
5989 __ j(zero, &miss, Label::kNear);
5990
5991 // Symbols are compared by identity.
5992 Label done;
5993 __ cmp(left, Operand(right));
5994 // Make sure eax is non-zero. At this point input operands are
5995 // guaranteed to be non-zero.
5996 ASSERT(right.is(eax));
5997 __ j(not_equal, &done, Label::kNear);
5998 STATIC_ASSERT(EQUAL == 0);
5999 STATIC_ASSERT(kSmiTag == 0);
6000 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6001 __ bind(&done);
6002 __ ret(0);
6003
6004 __ bind(&miss);
6005 GenerateMiss(masm);
6006}
6007
6008
lrn@chromium.org1c092762011-05-09 09:42:16 +00006009void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6010 ASSERT(state_ == CompareIC::STRINGS);
6011 ASSERT(GetCondition() == equal);
6012 Label miss;
6013
6014 // Registers containing left and right operands respectively.
6015 Register left = edx;
6016 Register right = eax;
6017 Register tmp1 = ecx;
6018 Register tmp2 = ebx;
6019 Register tmp3 = edi;
6020
6021 // Check that both operands are heap objects.
6022 __ mov(tmp1, Operand(left));
6023 STATIC_ASSERT(kSmiTag == 0);
6024 __ and_(tmp1, Operand(right));
whesse@chromium.org7b260152011-06-20 15:33:18 +00006025 __ JumpIfSmi(tmp1, &miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006026
6027 // Check that both operands are strings. This leaves the instance
6028 // types loaded in tmp1 and tmp2.
6029 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
6030 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
6031 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
6032 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
6033 __ mov(tmp3, tmp1);
6034 STATIC_ASSERT(kNotStringTag != 0);
6035 __ or_(tmp3, Operand(tmp2));
6036 __ test(tmp3, Immediate(kIsNotStringMask));
6037 __ j(not_zero, &miss);
6038
6039 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006040 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006041 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006042 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006043 STATIC_ASSERT(EQUAL == 0);
6044 STATIC_ASSERT(kSmiTag == 0);
6045 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6046 __ ret(0);
6047
6048 // Handle not identical strings.
6049 __ bind(&not_same);
6050
6051 // Check that both strings are symbols. If they are, we're done
6052 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006053 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006054 STATIC_ASSERT(kSymbolTag != 0);
6055 __ and_(tmp1, Operand(tmp2));
6056 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006057 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006058 // Make sure eax is non-zero. At this point input operands are
6059 // guaranteed to be non-zero.
6060 ASSERT(right.is(eax));
6061 __ ret(0);
6062
6063 // Check that both strings are sequential ASCII.
6064 Label runtime;
6065 __ bind(&do_compare);
6066 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
6067
6068 // Compare flat ASCII strings. Returns when done.
6069 StringCompareStub::GenerateFlatAsciiStringEquals(
6070 masm, left, right, tmp1, tmp2);
6071
6072 // Handle more complex cases in runtime.
6073 __ bind(&runtime);
6074 __ pop(tmp1); // Return address.
6075 __ push(left);
6076 __ push(right);
6077 __ push(tmp1);
6078 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6079
6080 __ bind(&miss);
6081 GenerateMiss(masm);
6082}
6083
6084
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006085void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6086 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006087 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006088 __ mov(ecx, Operand(edx));
6089 __ and_(ecx, Operand(eax));
whesse@chromium.org7b260152011-06-20 15:33:18 +00006090 __ JumpIfSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006091
6092 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006093 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006094 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006095 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006096
6097 ASSERT(GetCondition() == equal);
6098 __ sub(eax, Operand(edx));
6099 __ ret(0);
6100
6101 __ bind(&miss);
6102 GenerateMiss(masm);
6103}
6104
6105
6106void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6107 // Save the registers.
6108 __ pop(ecx);
6109 __ push(edx);
6110 __ push(eax);
6111 __ push(ecx);
6112
6113 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006114 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6115 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006116 __ EnterInternalFrame();
6117 __ push(edx);
6118 __ push(eax);
6119 __ push(Immediate(Smi::FromInt(op_)));
6120 __ CallExternalReference(miss, 3);
6121 __ LeaveInternalFrame();
6122
6123 // Compute the entry point of the rewritten stub.
6124 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6125
6126 // Restore registers.
6127 __ pop(ecx);
6128 __ pop(eax);
6129 __ pop(edx);
6130 __ push(ecx);
6131
6132 // Do a tail call to the rewritten stub.
6133 __ jmp(Operand(edi));
6134}
6135
6136
lrn@chromium.org1c092762011-05-09 09:42:16 +00006137// Helper function used to check that the dictionary doesn't contain
6138// the property. This function may return false negatives, so miss_label
6139// must always call a backup property check that is complete.
6140// This function is safe to call if the receiver has fast properties.
6141// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006142MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
6143 MacroAssembler* masm,
6144 Label* miss,
6145 Label* done,
6146 Register properties,
6147 String* name,
6148 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00006149 ASSERT(name->IsSymbol());
6150
6151 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6152 // not equal to the name and kProbes-th slot is not used (its name is the
6153 // undefined value), it guarantees the hash table doesn't contain the
6154 // property. It's true even if some slots represent deleted properties
6155 // (their names are the null value).
6156 for (int i = 0; i < kInlinedProbes; i++) {
6157 // Compute the masked index: (hash + i + i * i) & mask.
6158 Register index = r0;
6159 // Capacity is smi 2^n.
6160 __ mov(index, FieldOperand(properties, kCapacityOffset));
6161 __ dec(index);
6162 __ and_(Operand(index),
6163 Immediate(Smi::FromInt(name->Hash() +
6164 StringDictionary::GetProbeOffset(i))));
6165
6166 // Scale the index by multiplying by the entry size.
6167 ASSERT(StringDictionary::kEntrySize == 3);
6168 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
6169 Register entity_name = r0;
6170 // Having undefined at this place means the name is not contained.
6171 ASSERT_EQ(kSmiTagSize, 1);
6172 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
6173 kElementsStartOffset - kHeapObjectTag));
6174 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006175 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006176
6177 // Stop if found the property.
6178 __ cmp(entity_name, Handle<String>(name));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006179 __ j(equal, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006180
6181 // Check if the entry name is not a symbol.
6182 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6183 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6184 kIsSymbolMask);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006185 __ j(zero, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006186 }
6187
6188 StringDictionaryLookupStub stub(properties,
6189 r0,
6190 r0,
6191 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6192 __ push(Immediate(Handle<Object>(name)));
6193 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006194 MaybeObject* result = masm->TryCallStub(&stub);
6195 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006196 __ test(r0, Operand(r0));
6197 __ j(not_zero, miss);
6198 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006199 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006200}
6201
6202
6203// Probe the string dictionary in the |elements| register. Jump to the
6204// |done| label if a property with the given name is found leaving the
6205// index into the dictionary in |r0|. Jump to the |miss| label
6206// otherwise.
6207void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6208 Label* miss,
6209 Label* done,
6210 Register elements,
6211 Register name,
6212 Register r0,
6213 Register r1) {
6214 // Assert that name contains a string.
6215 if (FLAG_debug_code) __ AbortIfNotString(name);
6216
6217 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6218 __ shr(r1, kSmiTagSize); // convert smi to int
6219 __ dec(r1);
6220
6221 // Generate an unrolled loop that performs a few probes before
6222 // giving up. Measurements done on Gmail indicate that 2 probes
6223 // cover ~93% of loads from dictionaries.
6224 for (int i = 0; i < kInlinedProbes; i++) {
6225 // Compute the masked index: (hash + i + i * i) & mask.
6226 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6227 __ shr(r0, String::kHashShift);
6228 if (i > 0) {
6229 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6230 }
6231 __ and_(r0, Operand(r1));
6232
6233 // Scale the index by multiplying by the entry size.
6234 ASSERT(StringDictionary::kEntrySize == 3);
6235 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6236
6237 // Check if the key is identical to the name.
6238 __ cmp(name, Operand(elements,
6239 r0,
6240 times_4,
6241 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006242 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006243 }
6244
6245 StringDictionaryLookupStub stub(elements,
6246 r1,
6247 r0,
6248 POSITIVE_LOOKUP);
6249 __ push(name);
6250 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6251 __ shr(r0, String::kHashShift);
6252 __ push(r0);
6253 __ CallStub(&stub);
6254
6255 __ test(r1, Operand(r1));
6256 __ j(zero, miss);
6257 __ jmp(done);
6258}
6259
6260
6261void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6262 // Stack frame on entry:
6263 // esp[0 * kPointerSize]: return address.
6264 // esp[1 * kPointerSize]: key's hash.
6265 // esp[2 * kPointerSize]: key.
6266 // Registers:
6267 // dictionary_: StringDictionary to probe.
6268 // result_: used as scratch.
6269 // index_: will hold an index of entry if lookup is successful.
6270 // might alias with result_.
6271 // Returns:
6272 // result_ is zero if lookup failed, non zero otherwise.
6273
6274 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6275
6276 Register scratch = result_;
6277
6278 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6279 __ dec(scratch);
6280 __ SmiUntag(scratch);
6281 __ push(scratch);
6282
6283 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6284 // not equal to the name and kProbes-th slot is not used (its name is the
6285 // undefined value), it guarantees the hash table doesn't contain the
6286 // property. It's true even if some slots represent deleted properties
6287 // (their names are the null value).
6288 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6289 // Compute the masked index: (hash + i + i * i) & mask.
6290 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6291 if (i > 0) {
6292 __ add(Operand(scratch),
6293 Immediate(StringDictionary::GetProbeOffset(i)));
6294 }
6295 __ and_(scratch, Operand(esp, 0));
6296
6297 // Scale the index by multiplying by the entry size.
6298 ASSERT(StringDictionary::kEntrySize == 3);
6299 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6300
6301 // Having undefined at this place means the name is not contained.
6302 ASSERT_EQ(kSmiTagSize, 1);
6303 __ mov(scratch, Operand(dictionary_,
6304 index_,
6305 times_pointer_size,
6306 kElementsStartOffset - kHeapObjectTag));
6307 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6308 __ j(equal, &not_in_dictionary);
6309
6310 // Stop if found the property.
6311 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6312 __ j(equal, &in_dictionary);
6313
6314 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6315 // If we hit a non symbol key during negative lookup
6316 // we have to bailout as this key might be equal to the
6317 // key we are looking for.
6318
6319 // Check if the entry name is not a symbol.
6320 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6321 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6322 kIsSymbolMask);
6323 __ j(zero, &maybe_in_dictionary);
6324 }
6325 }
6326
6327 __ bind(&maybe_in_dictionary);
6328 // If we are doing negative lookup then probing failure should be
6329 // treated as a lookup success. For positive lookup probing failure
6330 // should be treated as lookup failure.
6331 if (mode_ == POSITIVE_LOOKUP) {
6332 __ mov(result_, Immediate(0));
6333 __ Drop(1);
6334 __ ret(2 * kPointerSize);
6335 }
6336
6337 __ bind(&in_dictionary);
6338 __ mov(result_, Immediate(1));
6339 __ Drop(1);
6340 __ ret(2 * kPointerSize);
6341
6342 __ bind(&not_in_dictionary);
6343 __ mov(result_, Immediate(0));
6344 __ Drop(1);
6345 __ ret(2 * kPointerSize);
6346}
6347
6348
ricow@chromium.org65fae842010-08-25 15:26:24 +00006349#undef __
6350
6351} } // namespace v8::internal
6352
6353#endif // V8_TARGET_ARCH_IA32