blob: 0992e0072c152dbbb41affac54221ba2a1e83755 [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.org7a392b32011-01-31 11:30:36 +000046 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000047 __ j(not_zero, &check_heap_number, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000048 __ ret(0);
49
50 __ bind(&check_heap_number);
51 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000052 Factory* factory = masm->isolate()->factory();
53 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000054 __ j(not_equal, &call_builtin, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000055 __ ret(0);
56
57 __ bind(&call_builtin);
58 __ pop(ecx); // Pop return address.
59 __ push(eax);
60 __ push(ecx); // Push return address.
61 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
62}
63
64
ricow@chromium.org65fae842010-08-25 15:26:24 +000065void FastNewClosureStub::Generate(MacroAssembler* masm) {
66 // Create a new closure from the given function info in new
67 // space. Set the context to the current context in esi.
68 Label gc;
69 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
70
71 // Get the function info from the stack.
72 __ mov(edx, Operand(esp, 1 * kPointerSize));
73
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 int map_index = strict_mode_ == kStrictMode
75 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
76 : Context::FUNCTION_MAP_INDEX;
77
ricow@chromium.org65fae842010-08-25 15:26:24 +000078 // Compute the function map in the current global context and set that
79 // as the map of the allocated object.
80 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
81 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 __ mov(ecx, Operand(ecx, Context::SlotOffset(map_index)));
ricow@chromium.org65fae842010-08-25 15:26:24 +000083 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
84
85 // Initialize the rest of the function. We don't have to update the
86 // write barrier because the allocated object is in new space.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000087 Factory* factory = masm->isolate()->factory();
88 __ mov(ebx, Immediate(factory->empty_fixed_array()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000089 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
90 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
91 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000092 Immediate(factory->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000093 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
94 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
95 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000096 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000097 Immediate(factory->undefined_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000098
99 // Initialize the code pointer in the function to be the one
100 // found in the shared function info object.
101 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
102 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
103 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
104
105 // Return and remove the on-stack parameter.
106 __ ret(1 * kPointerSize);
107
108 // Create a new closure through the slower runtime call.
109 __ bind(&gc);
110 __ pop(ecx); // Temporarily remove return address.
111 __ pop(edx);
112 __ push(esi);
113 __ push(edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000114 __ push(Immediate(factory->false_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000115 __ push(ecx); // Restore return address.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000116 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000117}
118
119
120void FastNewContextStub::Generate(MacroAssembler* masm) {
121 // Try to allocate the context in new space.
122 Label gc;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000123 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
124 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000125 eax, ebx, ecx, &gc, TAG_OBJECT);
126
127 // Get the function from the stack.
128 __ mov(ecx, Operand(esp, 1 * kPointerSize));
129
130 // Setup the object header.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000131 Factory* factory = masm->isolate()->factory();
132 __ mov(FieldOperand(eax, HeapObject::kMapOffset), factory->context_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000133 __ mov(FieldOperand(eax, Context::kLengthOffset),
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000134 Immediate(Smi::FromInt(length)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135
136 // Setup the fixed slots.
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000137 __ Set(ebx, Immediate(0)); // Set to NULL.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000138 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
139 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
140 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
141 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
142
143 // Copy the global object from the surrounding context. We go through the
144 // context in the function (ecx) to match the allocation behavior we have
145 // in the runtime system (see Heap::AllocateFunctionContext).
146 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
147 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
148 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
149
150 // Initialize the rest of the slots to undefined.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000151 __ mov(ebx, factory->undefined_value());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000152 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000153 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
154 }
155
156 // Return and remove the on-stack parameter.
157 __ mov(esi, Operand(eax));
158 __ ret(1 * kPointerSize);
159
160 // Need to collect. Call into runtime system.
161 __ bind(&gc);
162 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
163}
164
165
166void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
167 // Stack layout on entry:
168 //
169 // [esp + kPointerSize]: constant elements.
170 // [esp + (2 * kPointerSize)]: literal index.
171 // [esp + (3 * kPointerSize)]: literals array.
172
173 // All sizes here are multiples of kPointerSize.
174 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
175 int size = JSArray::kSize + elements_size;
176
177 // Load boilerplate object into ecx and check if we need to create a
178 // boilerplate.
179 Label slow_case;
180 __ mov(ecx, Operand(esp, 3 * kPointerSize));
181 __ mov(eax, Operand(esp, 2 * kPointerSize));
182 STATIC_ASSERT(kPointerSize == 4);
183 STATIC_ASSERT(kSmiTagSize == 1);
184 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000185 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
186 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000187 Factory* factory = masm->isolate()->factory();
188 __ cmp(ecx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000189 __ j(equal, &slow_case);
190
191 if (FLAG_debug_code) {
192 const char* message;
193 Handle<Map> expected_map;
194 if (mode_ == CLONE_ELEMENTS) {
195 message = "Expected (writable) fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000196 expected_map = factory->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000197 } else {
198 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
199 message = "Expected copy-on-write fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000200 expected_map = factory->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000201 }
202 __ push(ecx);
203 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
204 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
205 __ Assert(equal, message);
206 __ pop(ecx);
207 }
208
209 // Allocate both the JS array and the elements array in one big
210 // allocation. This avoids multiple limit checks.
211 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
212
213 // Copy the JS array part.
214 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
215 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
216 __ mov(ebx, FieldOperand(ecx, i));
217 __ mov(FieldOperand(eax, i), ebx);
218 }
219 }
220
221 if (length_ > 0) {
222 // Get hold of the elements array of the boilerplate and setup the
223 // elements pointer in the resulting object.
224 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
225 __ lea(edx, Operand(eax, JSArray::kSize));
226 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
227
228 // Copy the elements array.
229 for (int i = 0; i < elements_size; i += kPointerSize) {
230 __ mov(ebx, FieldOperand(ecx, i));
231 __ mov(FieldOperand(edx, i), ebx);
232 }
233 }
234
235 // Return and remove the on-stack parameters.
236 __ ret(3 * kPointerSize);
237
238 __ bind(&slow_case);
239 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
240}
241
242
243// NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
244void ToBooleanStub::Generate(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000245 Label false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000246 __ mov(eax, Operand(esp, 1 * kPointerSize));
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000247 Factory* factory = masm->isolate()->factory();
248
249 // undefined -> false
250 __ cmp(eax, factory->undefined_value());
251 __ j(equal, &false_result);
252
253 // Boolean -> its value
254 __ cmp(eax, factory->true_value());
255 __ j(equal, &true_result);
256 __ cmp(eax, factory->false_value());
257 __ j(equal, &false_result);
258
259 // Smis: 0 -> false, all other -> true
260 __ test(eax, Operand(eax));
261 __ j(zero, &false_result);
262 __ test(eax, Immediate(kSmiTagMask));
263 __ j(zero, &true_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000264
265 // 'null' => false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000266 __ cmp(eax, factory->null_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000267 __ j(equal, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000268
269 // Get the map and type of the heap object.
270 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
271 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
272
273 // Undetectable => false.
274 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
275 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000276 __ j(not_zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000277
278 // JavaScript object => true.
279 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000280 __ j(above_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000281
282 // String value => false iff empty.
283 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000284 __ j(above_equal, &not_string, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000285 STATIC_ASSERT(kSmiTag == 0);
286 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000287 __ j(zero, &false_result, Label::kNear);
288 __ jmp(&true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000289
290 __ bind(&not_string);
291 // HeapNumber => false iff +0, -0, or NaN.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000292 __ cmp(edx, factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000293 __ j(not_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000294 __ fldz();
295 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
296 __ FCmp();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000297 __ j(zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000298 // Fall through to |true_result|.
299
300 // Return 1/0 for true/false in eax.
301 __ bind(&true_result);
302 __ mov(eax, 1);
303 __ ret(1 * kPointerSize);
304 __ bind(&false_result);
305 __ mov(eax, 0);
306 __ ret(1 * kPointerSize);
307}
308
309
ricow@chromium.org65fae842010-08-25 15:26:24 +0000310class FloatingPointHelper : public AllStatic {
311 public:
312
313 enum ArgLocation {
314 ARGS_ON_STACK,
315 ARGS_IN_REGISTERS
316 };
317
318 // Code pattern for loading a floating point value. Input value must
319 // be either a smi or a heap number object (fp value). Requirements:
320 // operand in register number. Returns operand as floating point number
321 // on FPU stack.
322 static void LoadFloatOperand(MacroAssembler* masm, Register number);
323
324 // Code pattern for loading floating point values. Input values must
325 // be either smi or heap number objects (fp values). Requirements:
326 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
327 // Returns operands as floating point numbers on FPU stack.
328 static void LoadFloatOperands(MacroAssembler* masm,
329 Register scratch,
330 ArgLocation arg_location = ARGS_ON_STACK);
331
332 // Similar to LoadFloatOperand but assumes that both operands are smis.
333 // Expects operands in edx, eax.
334 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
335
336 // Test if operands are smi or number objects (fp). Requirements:
337 // operand_1 in eax, operand_2 in edx; falls through on float
338 // operands, jumps to the non_float label otherwise.
339 static void CheckFloatOperands(MacroAssembler* masm,
340 Label* non_float,
341 Register scratch);
342
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000343 // Checks that the two floating point numbers on top of the FPU stack
344 // have int32 values.
345 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
346 Label* non_int32);
347
ricow@chromium.org65fae842010-08-25 15:26:24 +0000348 // Takes the operands in edx and eax and loads them as integers in eax
349 // and ecx.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000350 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
351 bool use_sse3,
352 Label* operand_conversion_failure);
353
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000354 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
355 // operands are pushed on the stack, and that their conversions to int32
356 // are in eax and ecx. Checks that the original numbers were in the int32
357 // range.
358 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
359 bool use_sse3,
360 Label* not_int32);
361
362 // Assumes that operands are smis or heap numbers and loads them
363 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000364 // Leaves operands unchanged.
365 static void LoadSSE2Operands(MacroAssembler* masm);
366
367 // Test if operands are numbers (smi or HeapNumber objects), and load
368 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
369 // either operand is not a number. Operands are in edx and eax.
370 // Leaves operands unchanged.
371 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
372
373 // Similar to LoadSSE2Operands but assumes that both operands are smis.
374 // Expects operands in edx, eax.
375 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000376
377 // Checks that the two floating point numbers loaded into xmm0 and xmm1
378 // have int32 values.
379 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
380 Label* non_int32,
381 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000382};
383
384
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000385// Get the integer part of a heap number. Surprisingly, all this bit twiddling
386// is faster than using the built-in instructions on floating point registers.
387// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
388// trashed registers.
389static void IntegerConvert(MacroAssembler* masm,
390 Register source,
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000391 bool use_sse3,
392 Label* conversion_failure) {
393 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
394 Label done, right_exponent, normal_exponent;
395 Register scratch = ebx;
396 Register scratch2 = edi;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000397 // Get exponent word.
398 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
399 // Get exponent alone in scratch2.
400 __ mov(scratch2, scratch);
401 __ and_(scratch2, HeapNumber::kExponentMask);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000402 if (use_sse3) {
403 CpuFeatures::Scope scope(SSE3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000404 // Check whether the exponent is too big for a 64 bit signed integer.
405 static const uint32_t kTooBigExponent =
406 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
407 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
408 __ j(greater_equal, conversion_failure);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000409 // Load x87 register with heap number.
410 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
411 // Reserve space for 64 bit answer.
412 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
413 // Do conversion, which cannot fail because we checked the exponent.
414 __ fisttp_d(Operand(esp, 0));
415 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
416 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
417 } else {
418 // Load ecx with zero. We use this either for the final shift or
419 // for the answer.
420 __ xor_(ecx, Operand(ecx));
421 // Check whether the exponent matches a 32 bit signed int that cannot be
422 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
423 // exponent is 30 (biased). This is the exponent that we are fastest at and
424 // also the highest exponent we can handle here.
425 const uint32_t non_smi_exponent =
426 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
427 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
428 // If we have a match of the int32-but-not-Smi exponent then skip some
429 // logic.
430 __ j(equal, &right_exponent);
431 // If the exponent is higher than that then go to slow case. This catches
432 // numbers that don't fit in a signed int32, infinities and NaNs.
433 __ j(less, &normal_exponent);
434
435 {
436 // Handle a big exponent. The only reason we have this code is that the
437 // >>> operator has a tendency to generate numbers with an exponent of 31.
438 const uint32_t big_non_smi_exponent =
439 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
440 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
441 __ j(not_equal, conversion_failure);
442 // We have the big exponent, typically from >>>. This means the number is
443 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
444 __ mov(scratch2, scratch);
445 __ and_(scratch2, HeapNumber::kMantissaMask);
446 // Put back the implicit 1.
447 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
448 // Shift up the mantissa bits to take up the space the exponent used to
449 // take. We just orred in the implicit bit so that took care of one and
450 // we want to use the full unsigned range so we subtract 1 bit from the
451 // shift distance.
452 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
453 __ shl(scratch2, big_shift_distance);
454 // Get the second half of the double.
455 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
456 // Shift down 21 bits to get the most significant 11 bits or the low
457 // mantissa word.
458 __ shr(ecx, 32 - big_shift_distance);
459 __ or_(ecx, Operand(scratch2));
460 // We have the answer in ecx, but we may need to negate it.
461 __ test(scratch, Operand(scratch));
462 __ j(positive, &done);
463 __ neg(ecx);
464 __ jmp(&done);
465 }
466
467 __ bind(&normal_exponent);
468 // Exponent word in scratch, exponent part of exponent word in scratch2.
469 // Zero in ecx.
470 // We know the exponent is smaller than 30 (biased). If it is less than
471 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
472 // it rounds to zero.
473 const uint32_t zero_exponent =
474 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
475 __ sub(Operand(scratch2), Immediate(zero_exponent));
476 // ecx already has a Smi zero.
477 __ j(less, &done);
478
479 // We have a shifted exponent between 0 and 30 in scratch2.
480 __ shr(scratch2, HeapNumber::kExponentShift);
481 __ mov(ecx, Immediate(30));
482 __ sub(ecx, Operand(scratch2));
483
484 __ bind(&right_exponent);
485 // Here ecx is the shift, scratch is the exponent word.
486 // Get the top bits of the mantissa.
487 __ and_(scratch, HeapNumber::kMantissaMask);
488 // Put back the implicit 1.
489 __ or_(scratch, 1 << HeapNumber::kExponentShift);
490 // Shift up the mantissa bits to take up the space the exponent used to
491 // take. We have kExponentShift + 1 significant bits int he low end of the
492 // word. Shift them to the top bits.
493 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
494 __ shl(scratch, shift_distance);
495 // Get the second half of the double. For some exponents we don't
496 // actually need this because the bits get shifted out again, but
497 // it's probably slower to test than just to do it.
498 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
499 // Shift down 22 bits to get the most significant 10 bits or the low
500 // mantissa word.
501 __ shr(scratch2, 32 - shift_distance);
502 __ or_(scratch2, Operand(scratch));
503 // Move down according to the exponent.
504 __ shr_cl(scratch2);
505 // Now the unsigned answer is in scratch2. We need to move it to ecx and
506 // we may need to fix the sign.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000507 Label negative;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000508 __ xor_(ecx, Operand(ecx));
509 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000510 __ j(greater, &negative, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000511 __ mov(ecx, scratch2);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000512 __ jmp(&done, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000513 __ bind(&negative);
514 __ sub(ecx, Operand(scratch2));
515 __ bind(&done);
516 }
517}
518
519
danno@chromium.org40cb8782011-05-25 07:58:50 +0000520Handle<Code> GetUnaryOpStub(int key, UnaryOpIC::TypeInfo type_info) {
521 UnaryOpStub stub(key, type_info);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000522 return stub.GetCode();
523}
524
525
danno@chromium.org40cb8782011-05-25 07:58:50 +0000526const char* UnaryOpStub::GetName() {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000527 if (name_ != NULL) return name_;
528 const int kMaxNameLength = 100;
529 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
530 kMaxNameLength);
531 if (name_ == NULL) return "OOM";
532 const char* op_name = Token::Name(op_);
533 const char* overwrite_name = NULL; // Make g++ happy.
534 switch (mode_) {
535 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
536 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
537 }
538
539 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000540 "UnaryOpStub_%s_%s_%s",
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000541 op_name,
542 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000543 UnaryOpIC::GetName(operand_type_));
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000544 return name_;
545}
546
547
548// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000549void UnaryOpStub::Generate(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000550 switch (operand_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000551 case UnaryOpIC::UNINITIALIZED:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000552 GenerateTypeTransition(masm);
553 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000554 case UnaryOpIC::SMI:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000555 GenerateSmiStub(masm);
556 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000557 case UnaryOpIC::HEAP_NUMBER:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000558 GenerateHeapNumberStub(masm);
559 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000560 case UnaryOpIC::GENERIC:
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000561 GenerateGenericStub(masm);
562 break;
563 }
564}
565
566
danno@chromium.org40cb8782011-05-25 07:58:50 +0000567void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000568 __ pop(ecx); // Save return address.
569 __ push(eax);
570 // the argument is now on top.
571 // Push this stub's key. Although the operation and the type info are
572 // encoded into the key, the encoding is opaque, so push them too.
573 __ push(Immediate(Smi::FromInt(MinorKey())));
574 __ push(Immediate(Smi::FromInt(op_)));
575 __ push(Immediate(Smi::FromInt(operand_type_)));
576
577 __ push(ecx); // Push return address.
578
579 // Patch the caller to an appropriate specialized stub and return the
580 // operation result to the caller of the stub.
581 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000582 ExternalReference(IC_Utility(IC::kUnaryOp_Patch),
583 masm->isolate()), 4, 1);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000584}
585
586
587// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000588void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000589 switch (op_) {
590 case Token::SUB:
591 GenerateSmiStubSub(masm);
592 break;
593 case Token::BIT_NOT:
594 GenerateSmiStubBitNot(masm);
595 break;
596 default:
597 UNREACHABLE();
598 }
599}
600
601
danno@chromium.org40cb8782011-05-25 07:58:50 +0000602void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000603 Label non_smi, undo, slow;
604 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
605 Label::kNear, Label::kNear, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000606 __ bind(&undo);
607 GenerateSmiCodeUndo(masm);
608 __ bind(&non_smi);
609 __ bind(&slow);
610 GenerateTypeTransition(masm);
611}
612
613
danno@chromium.org40cb8782011-05-25 07:58:50 +0000614void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000615 Label non_smi;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000616 GenerateSmiCodeBitNot(masm, &non_smi);
617 __ bind(&non_smi);
618 GenerateTypeTransition(masm);
619}
620
621
danno@chromium.org40cb8782011-05-25 07:58:50 +0000622void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
623 Label* non_smi,
624 Label* undo,
625 Label* slow,
626 Label::Distance non_smi_near,
627 Label::Distance undo_near,
628 Label::Distance slow_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000629 // Check whether the value is a smi.
630 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000631 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000632
633 // We can't handle -0 with smis, so use a type transition for that case.
634 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000635 __ j(zero, slow, slow_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000636
637 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
638 __ mov(edx, Operand(eax));
639 __ Set(eax, Immediate(0));
640 __ sub(eax, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000641 __ j(overflow, undo, undo_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000642 __ ret(0);
643}
644
645
danno@chromium.org40cb8782011-05-25 07:58:50 +0000646void UnaryOpStub::GenerateSmiCodeBitNot(
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000647 MacroAssembler* masm,
648 Label* non_smi,
649 Label::Distance non_smi_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000650 // Check whether the value is a smi.
651 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000652 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000653
654 // Flip bits and revert inverted smi-tag.
655 __ not_(eax);
656 __ and_(eax, ~kSmiTagMask);
657 __ ret(0);
658}
659
660
danno@chromium.org40cb8782011-05-25 07:58:50 +0000661void UnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000662 __ mov(eax, Operand(edx));
663}
664
665
666// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000667void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000668 switch (op_) {
669 case Token::SUB:
670 GenerateHeapNumberStubSub(masm);
671 break;
672 case Token::BIT_NOT:
673 GenerateHeapNumberStubBitNot(masm);
674 break;
675 default:
676 UNREACHABLE();
677 }
678}
679
680
danno@chromium.org40cb8782011-05-25 07:58:50 +0000681void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000682 Label non_smi, undo, slow, call_builtin;
683 GenerateSmiCodeSub(masm, &non_smi, &undo, &call_builtin, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000684 __ bind(&non_smi);
685 GenerateHeapNumberCodeSub(masm, &slow);
686 __ bind(&undo);
687 GenerateSmiCodeUndo(masm);
688 __ bind(&slow);
689 GenerateTypeTransition(masm);
ager@chromium.orgea91cc52011-05-23 06:06:11 +0000690 __ bind(&call_builtin);
691 GenerateGenericCodeFallback(masm);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000692}
693
694
danno@chromium.org40cb8782011-05-25 07:58:50 +0000695void UnaryOpStub::GenerateHeapNumberStubBitNot(
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000696 MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000697 Label non_smi, slow;
698 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000699 __ bind(&non_smi);
700 GenerateHeapNumberCodeBitNot(masm, &slow);
701 __ bind(&slow);
702 GenerateTypeTransition(masm);
703}
704
705
danno@chromium.org40cb8782011-05-25 07:58:50 +0000706void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
707 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000708 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
709 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
710 __ j(not_equal, slow);
711
712 if (mode_ == UNARY_OVERWRITE) {
713 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
714 Immediate(HeapNumber::kSignMask)); // Flip sign.
715 } else {
716 __ mov(edx, Operand(eax));
717 // edx: operand
718
719 Label slow_allocate_heapnumber, heapnumber_allocated;
720 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
721 __ jmp(&heapnumber_allocated);
722
723 __ bind(&slow_allocate_heapnumber);
724 __ EnterInternalFrame();
725 __ push(edx);
726 __ CallRuntime(Runtime::kNumberAlloc, 0);
727 __ pop(edx);
728 __ LeaveInternalFrame();
729
730 __ bind(&heapnumber_allocated);
731 // eax: allocated 'empty' number
732 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
733 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
734 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
735 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
736 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
737 }
738 __ ret(0);
739}
740
741
danno@chromium.org40cb8782011-05-25 07:58:50 +0000742void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
743 Label* slow) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000744 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
745 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
746 __ j(not_equal, slow);
747
748 // Convert the heap number in eax to an untagged integer in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000749 IntegerConvert(masm, eax, CpuFeatures::IsSupported(SSE3), slow);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000750
751 // Do the bitwise operation and check if the result fits in a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000752 Label try_float;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000753 __ not_(ecx);
754 __ cmp(ecx, 0xc0000000);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000755 __ j(sign, &try_float, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000756
757 // Tag the result as a smi and we're done.
758 STATIC_ASSERT(kSmiTagSize == 1);
759 __ lea(eax, Operand(ecx, times_2, kSmiTag));
760 __ ret(0);
761
762 // Try to store the result in a heap number.
763 __ bind(&try_float);
764 if (mode_ == UNARY_NO_OVERWRITE) {
765 Label slow_allocate_heapnumber, heapnumber_allocated;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000766 __ mov(ebx, eax);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000767 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
768 __ jmp(&heapnumber_allocated);
769
770 __ bind(&slow_allocate_heapnumber);
771 __ EnterInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000772 // Push the original HeapNumber on the stack. The integer value can't
773 // be stored since it's untagged and not in the smi range (so we can't
774 // smi-tag it). We'll recalculate the value after the GC instead.
775 __ push(ebx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000776 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000777 // New HeapNumber is in eax.
778 __ pop(edx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000779 __ LeaveInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000780 // IntegerConvert uses ebx and edi as scratch registers.
781 // This conversion won't go slow-case.
782 IntegerConvert(masm, edx, CpuFeatures::IsSupported(SSE3), slow);
783 __ not_(ecx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000784
785 __ bind(&heapnumber_allocated);
786 }
787 if (CpuFeatures::IsSupported(SSE2)) {
788 CpuFeatures::Scope use_sse2(SSE2);
789 __ cvtsi2sd(xmm0, Operand(ecx));
790 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
791 } else {
792 __ push(ecx);
793 __ fild_s(Operand(esp, 0));
794 __ pop(ecx);
795 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
796 }
797 __ ret(0);
798}
799
800
801// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000802void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000803 switch (op_) {
804 case Token::SUB:
805 GenerateGenericStubSub(masm);
806 break;
807 case Token::BIT_NOT:
808 GenerateGenericStubBitNot(masm);
809 break;
810 default:
811 UNREACHABLE();
812 }
813}
814
815
danno@chromium.org40cb8782011-05-25 07:58:50 +0000816void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000817 Label non_smi, undo, slow;
818 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000819 __ bind(&non_smi);
820 GenerateHeapNumberCodeSub(masm, &slow);
821 __ bind(&undo);
822 GenerateSmiCodeUndo(masm);
823 __ bind(&slow);
824 GenerateGenericCodeFallback(masm);
825}
826
827
danno@chromium.org40cb8782011-05-25 07:58:50 +0000828void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000829 Label non_smi, slow;
830 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000831 __ bind(&non_smi);
832 GenerateHeapNumberCodeBitNot(masm, &slow);
833 __ bind(&slow);
834 GenerateGenericCodeFallback(masm);
835}
836
837
danno@chromium.org40cb8782011-05-25 07:58:50 +0000838void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000839 // Handle the slow case by jumping to the corresponding JavaScript builtin.
840 __ pop(ecx); // pop return address.
841 __ push(eax);
842 __ push(ecx); // push return address
843 switch (op_) {
844 case Token::SUB:
845 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
846 break;
847 case Token::BIT_NOT:
848 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
849 break;
850 default:
851 UNREACHABLE();
852 }
853}
854
855
danno@chromium.org40cb8782011-05-25 07:58:50 +0000856Handle<Code> GetBinaryOpStub(int key,
857 BinaryOpIC::TypeInfo type_info,
858 BinaryOpIC::TypeInfo result_type_info) {
859 BinaryOpStub stub(key, type_info, result_type_info);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000860 return stub.GetCode();
861}
862
863
danno@chromium.org40cb8782011-05-25 07:58:50 +0000864void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000865 __ pop(ecx); // Save return address.
866 __ push(edx);
867 __ push(eax);
868 // Left and right arguments are now on top.
869 // Push this stub's key. Although the operation and the type info are
870 // encoded into the key, the encoding is opaque, so push them too.
871 __ push(Immediate(Smi::FromInt(MinorKey())));
872 __ push(Immediate(Smi::FromInt(op_)));
873 __ push(Immediate(Smi::FromInt(operands_type_)));
874
875 __ push(ecx); // Push return address.
876
877 // Patch the caller to an appropriate specialized stub and return the
878 // operation result to the caller of the stub.
879 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000880 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000881 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000882 5,
883 1);
884}
885
886
887// Prepare for a type transition runtime call when the args are already on
888// the stack, under the return address.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000889void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000890 __ pop(ecx); // Save return address.
891 // Left and right arguments are already on top of the stack.
892 // Push this stub's key. Although the operation and the type info are
893 // encoded into the key, the encoding is opaque, so push them too.
894 __ push(Immediate(Smi::FromInt(MinorKey())));
895 __ push(Immediate(Smi::FromInt(op_)));
896 __ push(Immediate(Smi::FromInt(operands_type_)));
897
898 __ push(ecx); // Push return address.
899
900 // Patch the caller to an appropriate specialized stub and return the
901 // operation result to the caller of the stub.
902 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +0000903 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000904 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000905 5,
906 1);
907}
908
909
danno@chromium.org40cb8782011-05-25 07:58:50 +0000910void BinaryOpStub::Generate(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000911 switch (operands_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000912 case BinaryOpIC::UNINITIALIZED:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000913 GenerateTypeTransition(masm);
914 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000915 case BinaryOpIC::SMI:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000916 GenerateSmiStub(masm);
917 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000918 case BinaryOpIC::INT32:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000919 GenerateInt32Stub(masm);
920 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000921 case BinaryOpIC::HEAP_NUMBER:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000922 GenerateHeapNumberStub(masm);
923 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000924 case BinaryOpIC::ODDBALL:
lrn@chromium.org7516f052011-03-30 08:52:27 +0000925 GenerateOddballStub(masm);
926 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000927 case BinaryOpIC::BOTH_STRING:
danno@chromium.org160a7b02011-04-18 15:51:38 +0000928 GenerateBothStringStub(masm);
929 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000930 case BinaryOpIC::STRING:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000931 GenerateStringStub(masm);
932 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +0000933 case BinaryOpIC::GENERIC:
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000934 GenerateGeneric(masm);
935 break;
936 default:
937 UNREACHABLE();
938 }
939}
940
941
danno@chromium.org40cb8782011-05-25 07:58:50 +0000942const char* BinaryOpStub::GetName() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000943 if (name_ != NULL) return name_;
944 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000945 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
946 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000947 if (name_ == NULL) return "OOM";
948 const char* op_name = Token::Name(op_);
949 const char* overwrite_name;
950 switch (mode_) {
951 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
952 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
953 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
954 default: overwrite_name = "UnknownOverwrite"; break;
955 }
956
957 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
danno@chromium.org40cb8782011-05-25 07:58:50 +0000958 "BinaryOpStub_%s_%s_%s",
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000959 op_name,
960 overwrite_name,
danno@chromium.org40cb8782011-05-25 07:58:50 +0000961 BinaryOpIC::GetName(operands_type_));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000962 return name_;
963}
964
965
danno@chromium.org40cb8782011-05-25 07:58:50 +0000966void BinaryOpStub::GenerateSmiCode(
967 MacroAssembler* masm,
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000968 Label* slow,
969 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
970 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
971 // dividend in eax and edx free for the division. Use eax, ebx for those.
972 Comment load_comment(masm, "-- Load arguments");
973 Register left = edx;
974 Register right = eax;
975 if (op_ == Token::DIV || op_ == Token::MOD) {
976 left = eax;
977 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000978 __ mov(ebx, eax);
979 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000980 }
981
982
983 // 2. Prepare the smi check of both operands by oring them together.
984 Comment smi_check_comment(masm, "-- Smi check arguments");
985 Label not_smis;
986 Register combined = ecx;
987 ASSERT(!left.is(combined) && !right.is(combined));
988 switch (op_) {
989 case Token::BIT_OR:
990 // Perform the operation into eax and smi check the result. Preserve
991 // eax in case the result is not a smi.
992 ASSERT(!left.is(ecx) && !right.is(ecx));
993 __ mov(ecx, right);
994 __ or_(right, Operand(left)); // Bitwise or is commutative.
995 combined = right;
996 break;
997
998 case Token::BIT_XOR:
999 case Token::BIT_AND:
1000 case Token::ADD:
1001 case Token::SUB:
1002 case Token::MUL:
1003 case Token::DIV:
1004 case Token::MOD:
1005 __ mov(combined, right);
1006 __ or_(combined, Operand(left));
1007 break;
1008
1009 case Token::SHL:
1010 case Token::SAR:
1011 case Token::SHR:
1012 // Move the right operand into ecx for the shift operation, use eax
1013 // for the smi check register.
1014 ASSERT(!left.is(ecx) && !right.is(ecx));
1015 __ mov(ecx, right);
1016 __ or_(right, Operand(left));
1017 combined = right;
1018 break;
1019
1020 default:
1021 break;
1022 }
1023
1024 // 3. Perform the smi check of the operands.
1025 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1026 __ test(combined, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001027 __ j(not_zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001028
1029 // 4. Operands are both smis, perform the operation leaving the result in
1030 // eax and check the result if necessary.
1031 Comment perform_smi(masm, "-- Perform smi operation");
1032 Label use_fp_on_smis;
1033 switch (op_) {
1034 case Token::BIT_OR:
1035 // Nothing to do.
1036 break;
1037
1038 case Token::BIT_XOR:
1039 ASSERT(right.is(eax));
1040 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1041 break;
1042
1043 case Token::BIT_AND:
1044 ASSERT(right.is(eax));
1045 __ and_(right, Operand(left)); // Bitwise and is commutative.
1046 break;
1047
1048 case Token::SHL:
1049 // Remove tags from operands (but keep sign).
1050 __ SmiUntag(left);
1051 __ SmiUntag(ecx);
1052 // Perform the operation.
1053 __ shl_cl(left);
1054 // Check that the *signed* result fits in a smi.
1055 __ cmp(left, 0xc0000000);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001056 __ j(sign, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001057 // Tag the result and store it in register eax.
1058 __ SmiTag(left);
1059 __ mov(eax, left);
1060 break;
1061
1062 case Token::SAR:
1063 // Remove tags from operands (but keep sign).
1064 __ SmiUntag(left);
1065 __ SmiUntag(ecx);
1066 // Perform the operation.
1067 __ sar_cl(left);
1068 // Tag the result and store it in register eax.
1069 __ SmiTag(left);
1070 __ mov(eax, left);
1071 break;
1072
1073 case Token::SHR:
1074 // Remove tags from operands (but keep sign).
1075 __ SmiUntag(left);
1076 __ SmiUntag(ecx);
1077 // Perform the operation.
1078 __ shr_cl(left);
1079 // Check that the *unsigned* result fits in a smi.
1080 // Neither of the two high-order bits can be set:
1081 // - 0x80000000: high bit would be lost when smi tagging.
1082 // - 0x40000000: this number would convert to negative when
1083 // Smi tagging these two cases can only happen with shifts
1084 // by 0 or 1 when handed a valid smi.
1085 __ test(left, Immediate(0xc0000000));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001086 __ j(not_zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001087 // Tag the result and store it in register eax.
1088 __ SmiTag(left);
1089 __ mov(eax, left);
1090 break;
1091
1092 case Token::ADD:
1093 ASSERT(right.is(eax));
1094 __ add(right, Operand(left)); // Addition is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001095 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001096 break;
1097
1098 case Token::SUB:
1099 __ sub(left, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001100 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001101 __ mov(eax, left);
1102 break;
1103
1104 case Token::MUL:
1105 // If the smi tag is 0 we can just leave the tag on one operand.
1106 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1107 // We can't revert the multiplication if the result is not a smi
1108 // so save the right operand.
1109 __ mov(ebx, right);
1110 // Remove tag from one of the operands (but keep sign).
1111 __ SmiUntag(right);
1112 // Do multiplication.
1113 __ imul(right, Operand(left)); // Multiplication is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001114 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001115 // Check for negative zero result. Use combined = left | right.
1116 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1117 break;
1118
1119 case Token::DIV:
1120 // We can't revert the division if the result is not a smi so
1121 // save the left operand.
1122 __ mov(edi, left);
1123 // Check for 0 divisor.
1124 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001125 __ j(zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001126 // Sign extend left into edx:eax.
1127 ASSERT(left.is(eax));
1128 __ cdq();
1129 // Divide edx:eax by right.
1130 __ idiv(right);
1131 // Check for the corner case of dividing the most negative smi by
1132 // -1. We cannot use the overflow flag, since it is not set by idiv
1133 // instruction.
1134 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1135 __ cmp(eax, 0x40000000);
1136 __ j(equal, &use_fp_on_smis);
1137 // Check for negative zero result. Use combined = left | right.
1138 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1139 // Check that the remainder is zero.
1140 __ test(edx, Operand(edx));
1141 __ j(not_zero, &use_fp_on_smis);
1142 // Tag the result and store it in register eax.
1143 __ SmiTag(eax);
1144 break;
1145
1146 case Token::MOD:
1147 // Check for 0 divisor.
1148 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001149 __ j(zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001150
1151 // Sign extend left into edx:eax.
1152 ASSERT(left.is(eax));
1153 __ cdq();
1154 // Divide edx:eax by right.
1155 __ idiv(right);
1156 // Check for negative zero result. Use combined = left | right.
1157 __ NegativeZeroTest(edx, combined, slow);
1158 // Move remainder to register eax.
1159 __ mov(eax, edx);
1160 break;
1161
1162 default:
1163 UNREACHABLE();
1164 }
1165
1166 // 5. Emit return of result in eax. Some operations have registers pushed.
1167 switch (op_) {
1168 case Token::ADD:
1169 case Token::SUB:
1170 case Token::MUL:
1171 case Token::DIV:
1172 __ ret(0);
1173 break;
1174 case Token::MOD:
1175 case Token::BIT_OR:
1176 case Token::BIT_AND:
1177 case Token::BIT_XOR:
1178 case Token::SAR:
1179 case Token::SHL:
1180 case Token::SHR:
1181 __ ret(2 * kPointerSize);
1182 break;
1183 default:
1184 UNREACHABLE();
1185 }
1186
1187 // 6. For some operations emit inline code to perform floating point
1188 // operations on known smis (e.g., if the result of the operation
1189 // overflowed the smi range).
1190 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1191 __ bind(&use_fp_on_smis);
1192 switch (op_) {
1193 // Undo the effects of some operations, and some register moves.
1194 case Token::SHL:
1195 // The arguments are saved on the stack, and only used from there.
1196 break;
1197 case Token::ADD:
1198 // Revert right = right + left.
1199 __ sub(right, Operand(left));
1200 break;
1201 case Token::SUB:
1202 // Revert left = left - right.
1203 __ add(left, Operand(right));
1204 break;
1205 case Token::MUL:
1206 // Right was clobbered but a copy is in ebx.
1207 __ mov(right, ebx);
1208 break;
1209 case Token::DIV:
1210 // Left was clobbered but a copy is in edi. Right is in ebx for
1211 // division. They should be in eax, ebx for jump to not_smi.
1212 __ mov(eax, edi);
1213 break;
1214 default:
1215 // No other operators jump to use_fp_on_smis.
1216 break;
1217 }
1218 __ jmp(&not_smis);
1219 } else {
1220 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1221 switch (op_) {
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001222 case Token::SHL:
1223 case Token::SHR: {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001224 Comment perform_float(masm, "-- Perform float operation on smis");
1225 __ bind(&use_fp_on_smis);
1226 // Result we want is in left == edx, so we can put the allocated heap
1227 // number in eax.
1228 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1229 // Store the result in the HeapNumber and return.
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001230 // It's OK to overwrite the arguments on the stack because we
1231 // are about to return.
1232 if (op_ == Token::SHR) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001233 __ mov(Operand(esp, 1 * kPointerSize), left);
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001234 __ mov(Operand(esp, 2 * kPointerSize), Immediate(0));
1235 __ fild_d(Operand(esp, 1 * kPointerSize));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001236 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001237 } else {
1238 ASSERT_EQ(Token::SHL, op_);
1239 if (CpuFeatures::IsSupported(SSE2)) {
1240 CpuFeatures::Scope use_sse2(SSE2);
1241 __ cvtsi2sd(xmm0, Operand(left));
1242 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1243 } else {
1244 __ mov(Operand(esp, 1 * kPointerSize), left);
1245 __ fild_s(Operand(esp, 1 * kPointerSize));
1246 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1247 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001248 }
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001249 __ ret(2 * kPointerSize);
1250 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001251 }
1252
1253 case Token::ADD:
1254 case Token::SUB:
1255 case Token::MUL:
1256 case Token::DIV: {
1257 Comment perform_float(masm, "-- Perform float operation on smis");
1258 __ bind(&use_fp_on_smis);
1259 // Restore arguments to edx, eax.
1260 switch (op_) {
1261 case Token::ADD:
1262 // Revert right = right + left.
1263 __ sub(right, Operand(left));
1264 break;
1265 case Token::SUB:
1266 // Revert left = left - right.
1267 __ add(left, Operand(right));
1268 break;
1269 case Token::MUL:
1270 // Right was clobbered but a copy is in ebx.
1271 __ mov(right, ebx);
1272 break;
1273 case Token::DIV:
1274 // Left was clobbered but a copy is in edi. Right is in ebx for
1275 // division.
1276 __ mov(edx, edi);
1277 __ mov(eax, right);
1278 break;
1279 default: UNREACHABLE();
1280 break;
1281 }
1282 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001283 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001284 CpuFeatures::Scope use_sse2(SSE2);
1285 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1286 switch (op_) {
1287 case Token::ADD: __ addsd(xmm0, xmm1); break;
1288 case Token::SUB: __ subsd(xmm0, xmm1); break;
1289 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1290 case Token::DIV: __ divsd(xmm0, xmm1); break;
1291 default: UNREACHABLE();
1292 }
1293 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1294 } else { // SSE2 not available, use FPU.
1295 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1296 switch (op_) {
1297 case Token::ADD: __ faddp(1); break;
1298 case Token::SUB: __ fsubp(1); break;
1299 case Token::MUL: __ fmulp(1); break;
1300 case Token::DIV: __ fdivp(1); break;
1301 default: UNREACHABLE();
1302 }
1303 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1304 }
1305 __ mov(eax, ecx);
1306 __ ret(0);
1307 break;
1308 }
1309
1310 default:
1311 break;
1312 }
1313 }
1314
1315 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1316 // edx and eax.
1317 Comment done_comment(masm, "-- Enter non-smi code");
1318 __ bind(&not_smis);
1319 switch (op_) {
1320 case Token::BIT_OR:
1321 case Token::SHL:
1322 case Token::SAR:
1323 case Token::SHR:
1324 // Right operand is saved in ecx and eax was destroyed by the smi
1325 // check.
1326 __ mov(eax, ecx);
1327 break;
1328
1329 case Token::DIV:
1330 case Token::MOD:
1331 // Operands are in eax, ebx at this point.
1332 __ mov(edx, eax);
1333 __ mov(eax, ebx);
1334 break;
1335
1336 default:
1337 break;
1338 }
1339}
1340
1341
danno@chromium.org40cb8782011-05-25 07:58:50 +00001342void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001343 Label call_runtime;
1344
1345 switch (op_) {
1346 case Token::ADD:
1347 case Token::SUB:
1348 case Token::MUL:
1349 case Token::DIV:
1350 break;
1351 case Token::MOD:
1352 case Token::BIT_OR:
1353 case Token::BIT_AND:
1354 case Token::BIT_XOR:
1355 case Token::SAR:
1356 case Token::SHL:
1357 case Token::SHR:
1358 GenerateRegisterArgsPush(masm);
1359 break;
1360 default:
1361 UNREACHABLE();
1362 }
1363
danno@chromium.org40cb8782011-05-25 07:58:50 +00001364 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1365 result_type_ == BinaryOpIC::SMI) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001366 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1367 } else {
1368 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1369 }
1370 __ bind(&call_runtime);
1371 switch (op_) {
1372 case Token::ADD:
1373 case Token::SUB:
1374 case Token::MUL:
1375 case Token::DIV:
1376 GenerateTypeTransition(masm);
1377 break;
1378 case Token::MOD:
1379 case Token::BIT_OR:
1380 case Token::BIT_AND:
1381 case Token::BIT_XOR:
1382 case Token::SAR:
1383 case Token::SHL:
1384 case Token::SHR:
1385 GenerateTypeTransitionWithSavedArgs(masm);
1386 break;
1387 default:
1388 UNREACHABLE();
1389 }
1390}
1391
1392
danno@chromium.org40cb8782011-05-25 07:58:50 +00001393void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1394 ASSERT(operands_type_ == BinaryOpIC::STRING);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001395 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001396 // Try to add arguments as strings, otherwise, transition to the generic
danno@chromium.org40cb8782011-05-25 07:58:50 +00001397 // BinaryOpIC type.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001398 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001399 GenerateTypeTransition(masm);
1400}
1401
1402
danno@chromium.org40cb8782011-05-25 07:58:50 +00001403void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001404 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001405 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001406 ASSERT(op_ == Token::ADD);
1407 // If both arguments are strings, call the string add stub.
1408 // Otherwise, do a transition.
1409
1410 // Registers containing left and right operands respectively.
1411 Register left = edx;
1412 Register right = eax;
1413
1414 // Test if left operand is a string.
1415 __ test(left, Immediate(kSmiTagMask));
1416 __ j(zero, &call_runtime);
1417 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1418 __ j(above_equal, &call_runtime);
1419
1420 // Test if right operand is a string.
1421 __ test(right, Immediate(kSmiTagMask));
1422 __ j(zero, &call_runtime);
1423 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1424 __ j(above_equal, &call_runtime);
1425
1426 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1427 GenerateRegisterArgsPush(masm);
1428 __ TailCallStub(&string_add_stub);
1429
1430 __ bind(&call_runtime);
1431 GenerateTypeTransition(masm);
1432}
1433
1434
danno@chromium.org40cb8782011-05-25 07:58:50 +00001435void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001436 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001437 ASSERT(operands_type_ == BinaryOpIC::INT32);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001438
1439 // Floating point case.
1440 switch (op_) {
1441 case Token::ADD:
1442 case Token::SUB:
1443 case Token::MUL:
1444 case Token::DIV: {
1445 Label not_floats;
1446 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001447 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001448 CpuFeatures::Scope use_sse2(SSE2);
1449 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1450 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1451 switch (op_) {
1452 case Token::ADD: __ addsd(xmm0, xmm1); break;
1453 case Token::SUB: __ subsd(xmm0, xmm1); break;
1454 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1455 case Token::DIV: __ divsd(xmm0, xmm1); break;
1456 default: UNREACHABLE();
1457 }
1458 // Check result type if it is currently Int32.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001459 if (result_type_ <= BinaryOpIC::INT32) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001460 __ cvttsd2si(ecx, Operand(xmm0));
1461 __ cvtsi2sd(xmm2, Operand(ecx));
1462 __ ucomisd(xmm0, xmm2);
1463 __ j(not_zero, &not_int32);
1464 __ j(carry, &not_int32);
1465 }
1466 GenerateHeapResultAllocation(masm, &call_runtime);
1467 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1468 __ ret(0);
1469 } else { // SSE2 not available, use FPU.
1470 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1471 FloatingPointHelper::LoadFloatOperands(
1472 masm,
1473 ecx,
1474 FloatingPointHelper::ARGS_IN_REGISTERS);
1475 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1476 switch (op_) {
1477 case Token::ADD: __ faddp(1); break;
1478 case Token::SUB: __ fsubp(1); break;
1479 case Token::MUL: __ fmulp(1); break;
1480 case Token::DIV: __ fdivp(1); break;
1481 default: UNREACHABLE();
1482 }
1483 Label after_alloc_failure;
1484 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1485 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1486 __ ret(0);
1487 __ bind(&after_alloc_failure);
1488 __ ffree();
1489 __ jmp(&call_runtime);
1490 }
1491
1492 __ bind(&not_floats);
1493 __ bind(&not_int32);
1494 GenerateTypeTransition(masm);
1495 break;
1496 }
1497
1498 case Token::MOD: {
1499 // For MOD we go directly to runtime in the non-smi case.
1500 break;
1501 }
1502 case Token::BIT_OR:
1503 case Token::BIT_AND:
1504 case Token::BIT_XOR:
1505 case Token::SAR:
1506 case Token::SHL:
1507 case Token::SHR: {
1508 GenerateRegisterArgsPush(masm);
1509 Label not_floats;
1510 Label not_int32;
1511 Label non_smi_result;
1512 /* {
1513 CpuFeatures::Scope use_sse2(SSE2);
1514 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1515 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1516 }*/
1517 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1518 use_sse3_,
1519 &not_floats);
1520 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1521 &not_int32);
1522 switch (op_) {
1523 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1524 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1525 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1526 case Token::SAR: __ sar_cl(eax); break;
1527 case Token::SHL: __ shl_cl(eax); break;
1528 case Token::SHR: __ shr_cl(eax); break;
1529 default: UNREACHABLE();
1530 }
1531 if (op_ == Token::SHR) {
1532 // Check if result is non-negative and fits in a smi.
1533 __ test(eax, Immediate(0xc0000000));
1534 __ j(not_zero, &call_runtime);
1535 } else {
1536 // Check if result fits in a smi.
1537 __ cmp(eax, 0xc0000000);
1538 __ j(negative, &non_smi_result);
1539 }
1540 // Tag smi result and return.
1541 __ SmiTag(eax);
1542 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1543
1544 // All ops except SHR return a signed int32 that we load in
1545 // a HeapNumber.
1546 if (op_ != Token::SHR) {
1547 __ bind(&non_smi_result);
1548 // Allocate a heap number if needed.
1549 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001550 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001551 switch (mode_) {
1552 case OVERWRITE_LEFT:
1553 case OVERWRITE_RIGHT:
1554 // If the operand was an object, we skip the
1555 // allocation of a heap number.
1556 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1557 1 * kPointerSize : 2 * kPointerSize));
1558 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001559 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001560 // Fall through!
1561 case NO_OVERWRITE:
1562 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1563 __ bind(&skip_allocation);
1564 break;
1565 default: UNREACHABLE();
1566 }
1567 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001568 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001569 CpuFeatures::Scope use_sse2(SSE2);
1570 __ cvtsi2sd(xmm0, Operand(ebx));
1571 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1572 } else {
1573 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1574 __ fild_s(Operand(esp, 1 * kPointerSize));
1575 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1576 }
1577 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1578 }
1579
1580 __ bind(&not_floats);
1581 __ bind(&not_int32);
1582 GenerateTypeTransitionWithSavedArgs(masm);
1583 break;
1584 }
1585 default: UNREACHABLE(); break;
1586 }
1587
1588 // If an allocation fails, or SHR or MOD hit a hard case,
1589 // use the runtime system to get the correct result.
1590 __ bind(&call_runtime);
1591
1592 switch (op_) {
1593 case Token::ADD:
1594 GenerateRegisterArgsPush(masm);
1595 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1596 break;
1597 case Token::SUB:
1598 GenerateRegisterArgsPush(masm);
1599 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1600 break;
1601 case Token::MUL:
1602 GenerateRegisterArgsPush(masm);
1603 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1604 break;
1605 case Token::DIV:
1606 GenerateRegisterArgsPush(masm);
1607 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1608 break;
1609 case Token::MOD:
1610 GenerateRegisterArgsPush(masm);
1611 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1612 break;
1613 case Token::BIT_OR:
1614 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1615 break;
1616 case Token::BIT_AND:
1617 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1618 break;
1619 case Token::BIT_XOR:
1620 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1621 break;
1622 case Token::SAR:
1623 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1624 break;
1625 case Token::SHL:
1626 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1627 break;
1628 case Token::SHR:
1629 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1630 break;
1631 default:
1632 UNREACHABLE();
1633 }
1634}
1635
1636
danno@chromium.org40cb8782011-05-25 07:58:50 +00001637void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001638 if (op_ == Token::ADD) {
1639 // Handle string addition here, because it is the only operation
1640 // that does not do a ToNumber conversion on the operands.
1641 GenerateAddStrings(masm);
1642 }
1643
danno@chromium.org160a7b02011-04-18 15:51:38 +00001644 Factory* factory = masm->isolate()->factory();
1645
lrn@chromium.org7516f052011-03-30 08:52:27 +00001646 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001647 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001648 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001649 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001650 if (Token::IsBitOp(op_)) {
1651 __ xor_(edx, Operand(edx));
1652 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001653 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001654 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001655 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001656 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001657 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001658 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001659 if (Token::IsBitOp(op_)) {
1660 __ xor_(eax, Operand(eax));
1661 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001662 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001663 }
1664 __ bind(&done);
1665
1666 GenerateHeapNumberStub(masm);
1667}
1668
1669
danno@chromium.org40cb8782011-05-25 07:58:50 +00001670void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001671 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001672
1673 // Floating point case.
1674 switch (op_) {
1675 case Token::ADD:
1676 case Token::SUB:
1677 case Token::MUL:
1678 case Token::DIV: {
1679 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001680 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001681 CpuFeatures::Scope use_sse2(SSE2);
1682 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1683
1684 switch (op_) {
1685 case Token::ADD: __ addsd(xmm0, xmm1); break;
1686 case Token::SUB: __ subsd(xmm0, xmm1); break;
1687 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1688 case Token::DIV: __ divsd(xmm0, xmm1); break;
1689 default: UNREACHABLE();
1690 }
1691 GenerateHeapResultAllocation(masm, &call_runtime);
1692 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1693 __ ret(0);
1694 } else { // SSE2 not available, use FPU.
1695 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1696 FloatingPointHelper::LoadFloatOperands(
1697 masm,
1698 ecx,
1699 FloatingPointHelper::ARGS_IN_REGISTERS);
1700 switch (op_) {
1701 case Token::ADD: __ faddp(1); break;
1702 case Token::SUB: __ fsubp(1); break;
1703 case Token::MUL: __ fmulp(1); break;
1704 case Token::DIV: __ fdivp(1); break;
1705 default: UNREACHABLE();
1706 }
1707 Label after_alloc_failure;
1708 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1709 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1710 __ ret(0);
1711 __ bind(&after_alloc_failure);
1712 __ ffree();
1713 __ jmp(&call_runtime);
1714 }
1715
1716 __ bind(&not_floats);
1717 GenerateTypeTransition(masm);
1718 break;
1719 }
1720
1721 case Token::MOD: {
1722 // For MOD we go directly to runtime in the non-smi case.
1723 break;
1724 }
1725 case Token::BIT_OR:
1726 case Token::BIT_AND:
1727 case Token::BIT_XOR:
1728 case Token::SAR:
1729 case Token::SHL:
1730 case Token::SHR: {
1731 GenerateRegisterArgsPush(masm);
1732 Label not_floats;
1733 Label non_smi_result;
1734 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1735 use_sse3_,
1736 &not_floats);
1737 switch (op_) {
1738 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1739 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1740 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1741 case Token::SAR: __ sar_cl(eax); break;
1742 case Token::SHL: __ shl_cl(eax); break;
1743 case Token::SHR: __ shr_cl(eax); break;
1744 default: UNREACHABLE();
1745 }
1746 if (op_ == Token::SHR) {
1747 // Check if result is non-negative and fits in a smi.
1748 __ test(eax, Immediate(0xc0000000));
1749 __ j(not_zero, &call_runtime);
1750 } else {
1751 // Check if result fits in a smi.
1752 __ cmp(eax, 0xc0000000);
1753 __ j(negative, &non_smi_result);
1754 }
1755 // Tag smi result and return.
1756 __ SmiTag(eax);
1757 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1758
1759 // All ops except SHR return a signed int32 that we load in
1760 // a HeapNumber.
1761 if (op_ != Token::SHR) {
1762 __ bind(&non_smi_result);
1763 // Allocate a heap number if needed.
1764 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001765 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001766 switch (mode_) {
1767 case OVERWRITE_LEFT:
1768 case OVERWRITE_RIGHT:
1769 // If the operand was an object, we skip the
1770 // allocation of a heap number.
1771 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1772 1 * kPointerSize : 2 * kPointerSize));
1773 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001774 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001775 // Fall through!
1776 case NO_OVERWRITE:
1777 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1778 __ bind(&skip_allocation);
1779 break;
1780 default: UNREACHABLE();
1781 }
1782 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001783 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001784 CpuFeatures::Scope use_sse2(SSE2);
1785 __ cvtsi2sd(xmm0, Operand(ebx));
1786 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1787 } else {
1788 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1789 __ fild_s(Operand(esp, 1 * kPointerSize));
1790 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1791 }
1792 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1793 }
1794
1795 __ bind(&not_floats);
1796 GenerateTypeTransitionWithSavedArgs(masm);
1797 break;
1798 }
1799 default: UNREACHABLE(); break;
1800 }
1801
1802 // If an allocation fails, or SHR or MOD hit a hard case,
1803 // use the runtime system to get the correct result.
1804 __ bind(&call_runtime);
1805
1806 switch (op_) {
1807 case Token::ADD:
1808 GenerateRegisterArgsPush(masm);
1809 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1810 break;
1811 case Token::SUB:
1812 GenerateRegisterArgsPush(masm);
1813 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1814 break;
1815 case Token::MUL:
1816 GenerateRegisterArgsPush(masm);
1817 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1818 break;
1819 case Token::DIV:
1820 GenerateRegisterArgsPush(masm);
1821 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1822 break;
1823 case Token::MOD:
1824 GenerateRegisterArgsPush(masm);
1825 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1826 break;
1827 case Token::BIT_OR:
1828 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1829 break;
1830 case Token::BIT_AND:
1831 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1832 break;
1833 case Token::BIT_XOR:
1834 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1835 break;
1836 case Token::SAR:
1837 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1838 break;
1839 case Token::SHL:
1840 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1841 break;
1842 case Token::SHR:
1843 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1844 break;
1845 default:
1846 UNREACHABLE();
1847 }
1848}
1849
1850
danno@chromium.org40cb8782011-05-25 07:58:50 +00001851void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001852 Label call_runtime;
1853
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001854 Counters* counters = masm->isolate()->counters();
1855 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001856
1857 switch (op_) {
1858 case Token::ADD:
1859 case Token::SUB:
1860 case Token::MUL:
1861 case Token::DIV:
1862 break;
1863 case Token::MOD:
1864 case Token::BIT_OR:
1865 case Token::BIT_AND:
1866 case Token::BIT_XOR:
1867 case Token::SAR:
1868 case Token::SHL:
1869 case Token::SHR:
1870 GenerateRegisterArgsPush(masm);
1871 break;
1872 default:
1873 UNREACHABLE();
1874 }
1875
1876 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1877
1878 // Floating point case.
1879 switch (op_) {
1880 case Token::ADD:
1881 case Token::SUB:
1882 case Token::MUL:
1883 case Token::DIV: {
1884 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001885 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001886 CpuFeatures::Scope use_sse2(SSE2);
1887 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1888
1889 switch (op_) {
1890 case Token::ADD: __ addsd(xmm0, xmm1); break;
1891 case Token::SUB: __ subsd(xmm0, xmm1); break;
1892 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1893 case Token::DIV: __ divsd(xmm0, xmm1); break;
1894 default: UNREACHABLE();
1895 }
1896 GenerateHeapResultAllocation(masm, &call_runtime);
1897 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1898 __ ret(0);
1899 } else { // SSE2 not available, use FPU.
1900 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1901 FloatingPointHelper::LoadFloatOperands(
1902 masm,
1903 ecx,
1904 FloatingPointHelper::ARGS_IN_REGISTERS);
1905 switch (op_) {
1906 case Token::ADD: __ faddp(1); break;
1907 case Token::SUB: __ fsubp(1); break;
1908 case Token::MUL: __ fmulp(1); break;
1909 case Token::DIV: __ fdivp(1); break;
1910 default: UNREACHABLE();
1911 }
1912 Label after_alloc_failure;
1913 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1914 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1915 __ ret(0);
1916 __ bind(&after_alloc_failure);
1917 __ ffree();
1918 __ jmp(&call_runtime);
1919 }
1920 __ bind(&not_floats);
1921 break;
1922 }
1923 case Token::MOD: {
1924 // For MOD we go directly to runtime in the non-smi case.
1925 break;
1926 }
1927 case Token::BIT_OR:
1928 case Token::BIT_AND:
1929 case Token::BIT_XOR:
1930 case Token::SAR:
1931 case Token::SHL:
1932 case Token::SHR: {
1933 Label non_smi_result;
1934 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1935 use_sse3_,
1936 &call_runtime);
1937 switch (op_) {
1938 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1939 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1940 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1941 case Token::SAR: __ sar_cl(eax); break;
1942 case Token::SHL: __ shl_cl(eax); break;
1943 case Token::SHR: __ shr_cl(eax); break;
1944 default: UNREACHABLE();
1945 }
1946 if (op_ == Token::SHR) {
1947 // Check if result is non-negative and fits in a smi.
1948 __ test(eax, Immediate(0xc0000000));
1949 __ j(not_zero, &call_runtime);
1950 } else {
1951 // Check if result fits in a smi.
1952 __ cmp(eax, 0xc0000000);
1953 __ j(negative, &non_smi_result);
1954 }
1955 // Tag smi result and return.
1956 __ SmiTag(eax);
1957 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1958
1959 // All ops except SHR return a signed int32 that we load in
1960 // a HeapNumber.
1961 if (op_ != Token::SHR) {
1962 __ bind(&non_smi_result);
1963 // Allocate a heap number if needed.
1964 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001965 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001966 switch (mode_) {
1967 case OVERWRITE_LEFT:
1968 case OVERWRITE_RIGHT:
1969 // If the operand was an object, we skip the
1970 // allocation of a heap number.
1971 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1972 1 * kPointerSize : 2 * kPointerSize));
1973 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001974 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001975 // Fall through!
1976 case NO_OVERWRITE:
1977 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1978 __ bind(&skip_allocation);
1979 break;
1980 default: UNREACHABLE();
1981 }
1982 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001983 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001984 CpuFeatures::Scope use_sse2(SSE2);
1985 __ cvtsi2sd(xmm0, Operand(ebx));
1986 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1987 } else {
1988 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1989 __ fild_s(Operand(esp, 1 * kPointerSize));
1990 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1991 }
1992 __ ret(2 * kPointerSize);
1993 }
1994 break;
1995 }
1996 default: UNREACHABLE(); break;
1997 }
1998
1999 // If all else fails, use the runtime system to get the correct
2000 // result.
2001 __ bind(&call_runtime);
2002 switch (op_) {
2003 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002004 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002005 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002006 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2007 break;
2008 }
2009 case Token::SUB:
2010 GenerateRegisterArgsPush(masm);
2011 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2012 break;
2013 case Token::MUL:
2014 GenerateRegisterArgsPush(masm);
2015 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2016 break;
2017 case Token::DIV:
2018 GenerateRegisterArgsPush(masm);
2019 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2020 break;
2021 case Token::MOD:
2022 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2023 break;
2024 case Token::BIT_OR:
2025 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2026 break;
2027 case Token::BIT_AND:
2028 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2029 break;
2030 case Token::BIT_XOR:
2031 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2032 break;
2033 case Token::SAR:
2034 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2035 break;
2036 case Token::SHL:
2037 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2038 break;
2039 case Token::SHR:
2040 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2041 break;
2042 default:
2043 UNREACHABLE();
2044 }
2045}
2046
2047
danno@chromium.org40cb8782011-05-25 07:58:50 +00002048void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002049 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002050 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002051
2052 // Registers containing left and right operands respectively.
2053 Register left = edx;
2054 Register right = eax;
2055
2056 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002057 __ test(left, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002058 __ j(zero, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002059 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002060 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002061
2062 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2063 GenerateRegisterArgsPush(masm);
2064 __ TailCallStub(&string_add_left_stub);
2065
2066 // Left operand is not a string, test right.
2067 __ bind(&left_not_string);
2068 __ test(right, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002069 __ j(zero, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002070 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002071 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002072
2073 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2074 GenerateRegisterArgsPush(masm);
2075 __ TailCallStub(&string_add_right_stub);
2076
2077 // Neither argument is a string.
2078 __ bind(&call_runtime);
2079}
2080
2081
danno@chromium.org40cb8782011-05-25 07:58:50 +00002082void BinaryOpStub::GenerateHeapResultAllocation(
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002083 MacroAssembler* masm,
2084 Label* alloc_failure) {
2085 Label skip_allocation;
2086 OverwriteMode mode = mode_;
2087 switch (mode) {
2088 case OVERWRITE_LEFT: {
2089 // If the argument in edx is already an object, we skip the
2090 // allocation of a heap number.
2091 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002092 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002093 // Allocate a heap number for the result. Keep eax and edx intact
2094 // for the possible runtime call.
2095 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2096 // Now edx can be overwritten losing one of the arguments as we are
2097 // now done and will not need it any more.
2098 __ mov(edx, Operand(ebx));
2099 __ bind(&skip_allocation);
2100 // Use object in edx as a result holder
2101 __ mov(eax, Operand(edx));
2102 break;
2103 }
2104 case OVERWRITE_RIGHT:
2105 // If the argument in eax is already an object, we skip the
2106 // allocation of a heap number.
2107 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002108 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002109 // Fall through!
2110 case NO_OVERWRITE:
2111 // Allocate a heap number for the result. Keep eax and edx intact
2112 // for the possible runtime call.
2113 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2114 // Now eax can be overwritten losing one of the arguments as we are
2115 // now done and will not need it any more.
2116 __ mov(eax, ebx);
2117 __ bind(&skip_allocation);
2118 break;
2119 default: UNREACHABLE();
2120 }
2121}
2122
2123
danno@chromium.org40cb8782011-05-25 07:58:50 +00002124void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002125 __ pop(ecx);
2126 __ push(edx);
2127 __ push(eax);
2128 __ push(ecx);
2129}
2130
2131
ricow@chromium.org65fae842010-08-25 15:26:24 +00002132void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002133 // TAGGED case:
2134 // Input:
2135 // esp[4]: tagged number input argument (should be number).
2136 // esp[0]: return address.
2137 // Output:
2138 // eax: tagged double result.
2139 // UNTAGGED case:
2140 // Input::
2141 // esp[0]: return address.
2142 // xmm1: untagged double input argument
2143 // Output:
2144 // xmm1: untagged double result.
2145
ricow@chromium.org65fae842010-08-25 15:26:24 +00002146 Label runtime_call;
2147 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002148 Label skip_cache;
2149 const bool tagged = (argument_type_ == TAGGED);
2150 if (tagged) {
2151 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002152 Label input_not_smi;
2153 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002154 __ mov(eax, Operand(esp, kPointerSize));
2155 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002156 __ j(not_zero, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002157 // Input is a smi. Untag and load it onto the FPU stack.
2158 // Then load the low and high words of the double into ebx, edx.
2159 STATIC_ASSERT(kSmiTagSize == 1);
2160 __ sar(eax, 1);
2161 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2162 __ mov(Operand(esp, 0), eax);
2163 __ fild_s(Operand(esp, 0));
2164 __ fst_d(Operand(esp, 0));
2165 __ pop(edx);
2166 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002167 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002168 __ bind(&input_not_smi);
2169 // Check if input is a HeapNumber.
2170 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002171 Factory* factory = masm->isolate()->factory();
2172 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002173 __ j(not_equal, &runtime_call);
2174 // Input is a HeapNumber. Push it on the FPU stack and load its
2175 // low and high words into ebx, edx.
2176 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2177 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2178 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002179
whesse@chromium.org023421e2010-12-21 12:19:12 +00002180 __ bind(&loaded);
2181 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002182 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002183 CpuFeatures::Scope sse4_scope(SSE4_1);
2184 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2185 } else {
2186 __ pshufd(xmm0, xmm1, 0x1);
2187 __ movd(Operand(edx), xmm0);
2188 }
2189 __ movd(Operand(ebx), xmm1);
2190 }
2191
2192 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002193 // ebx = low 32 bits of double value
2194 // edx = high 32 bits of double value
2195 // Compute hash (the shifts are arithmetic):
2196 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2197 __ mov(ecx, ebx);
2198 __ xor_(ecx, Operand(edx));
2199 __ mov(eax, ecx);
2200 __ sar(eax, 16);
2201 __ xor_(ecx, Operand(eax));
2202 __ mov(eax, ecx);
2203 __ sar(eax, 8);
2204 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002205 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2206 __ and_(Operand(ecx),
2207 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002208
whesse@chromium.org023421e2010-12-21 12:19:12 +00002209 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002210 // ebx = low 32 bits of double value.
2211 // edx = high 32 bits of double value.
2212 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002213 ExternalReference cache_array =
2214 ExternalReference::transcendental_cache_array_address(masm->isolate());
2215 __ mov(eax, Immediate(cache_array));
2216 int cache_array_index =
2217 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2218 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002219 // Eax points to the cache for the type type_.
2220 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2221 __ test(eax, Operand(eax));
2222 __ j(zero, &runtime_call_clear_stack);
2223#ifdef DEBUG
2224 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002225 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002226 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2227 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2228 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2229 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2230 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2231 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2232 CHECK_EQ(0, elem_in0 - elem_start);
2233 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2234 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2235 }
2236#endif
2237 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2238 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2239 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2240 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002241 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002242 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002243 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002244 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002245 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002246 // Cache hit!
2247 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002248 if (tagged) {
2249 __ fstp(0);
2250 __ ret(kPointerSize);
2251 } else { // UNTAGGED.
2252 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2253 __ Ret();
2254 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002255
2256 __ bind(&cache_miss);
2257 // Update cache with new value.
2258 // We are short on registers, so use no_reg as scratch.
2259 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002260 if (tagged) {
2261 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2262 } else { // UNTAGGED.
2263 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2264 __ sub(Operand(esp), Immediate(kDoubleSize));
2265 __ movdbl(Operand(esp, 0), xmm1);
2266 __ fld_d(Operand(esp, 0));
2267 __ add(Operand(esp), Immediate(kDoubleSize));
2268 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002269 GenerateOperation(masm);
2270 __ mov(Operand(ecx, 0), ebx);
2271 __ mov(Operand(ecx, kIntSize), edx);
2272 __ mov(Operand(ecx, 2 * kIntSize), eax);
2273 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002274 if (tagged) {
2275 __ ret(kPointerSize);
2276 } else { // UNTAGGED.
2277 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2278 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002279
whesse@chromium.org023421e2010-12-21 12:19:12 +00002280 // Skip cache and return answer directly, only in untagged case.
2281 __ bind(&skip_cache);
2282 __ sub(Operand(esp), Immediate(kDoubleSize));
2283 __ movdbl(Operand(esp, 0), xmm1);
2284 __ fld_d(Operand(esp, 0));
2285 GenerateOperation(masm);
2286 __ fstp_d(Operand(esp, 0));
2287 __ movdbl(xmm1, Operand(esp, 0));
2288 __ add(Operand(esp), Immediate(kDoubleSize));
2289 // We return the value in xmm1 without adding it to the cache, but
2290 // we cause a scavenging GC so that future allocations will succeed.
2291 __ EnterInternalFrame();
2292 // Allocate an unused object bigger than a HeapNumber.
2293 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2294 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2295 __ LeaveInternalFrame();
2296 __ Ret();
2297 }
2298
2299 // Call runtime, doing whatever allocation and cleanup is necessary.
2300 if (tagged) {
2301 __ bind(&runtime_call_clear_stack);
2302 __ fstp(0);
2303 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002304 ExternalReference runtime =
2305 ExternalReference(RuntimeFunction(), masm->isolate());
2306 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002307 } else { // UNTAGGED.
2308 __ bind(&runtime_call_clear_stack);
2309 __ bind(&runtime_call);
2310 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2311 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2312 __ EnterInternalFrame();
2313 __ push(eax);
2314 __ CallRuntime(RuntimeFunction(), 1);
2315 __ LeaveInternalFrame();
2316 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2317 __ Ret();
2318 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002319}
2320
2321
2322Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2323 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002324 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2325 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002326 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002327 default:
2328 UNIMPLEMENTED();
2329 return Runtime::kAbort;
2330 }
2331}
2332
2333
2334void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2335 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002336 // Input value is on FP stack, and also in ebx/edx.
2337 // Input value is possibly in xmm1.
2338 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002339 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2340 // Both fsin and fcos require arguments in the range +/-2^63 and
2341 // return NaN for infinities and NaN. They can share all code except
2342 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002343 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002344 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2345 // work. We must reduce it to the appropriate range.
2346 __ mov(edi, edx);
2347 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2348 int supported_exponent_limit =
2349 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2350 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002351 __ j(below, &in_range, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002352 // Check for infinity and NaN. Both return NaN for sin.
2353 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002354 Label non_nan_result;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002355 __ j(not_equal, &non_nan_result, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002356 // Input is +/-Infinity or NaN. Result is NaN.
2357 __ fstp(0);
2358 // NaN is represented by 0x7ff8000000000000.
2359 __ push(Immediate(0x7ff80000));
2360 __ push(Immediate(0));
2361 __ fld_d(Operand(esp, 0));
2362 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002363 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002364
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002365 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002366
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002367 // Use fpmod to restrict argument to the range +/-2*PI.
2368 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2369 __ fldpi();
2370 __ fadd(0);
2371 __ fld(1);
2372 // FPU Stack: input, 2*pi, input.
2373 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002374 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002375 __ fwait();
2376 __ fnstsw_ax();
2377 // Clear if Illegal Operand or Zero Division exceptions are set.
2378 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002379 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002380 __ fnclex();
2381 __ bind(&no_exceptions);
2382 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002383
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002384 // Compute st(0) % st(1)
2385 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002386 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002387 __ bind(&partial_remainder_loop);
2388 __ fprem1();
2389 __ fwait();
2390 __ fnstsw_ax();
2391 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2392 // If C2 is set, computation only has partial result. Loop to
2393 // continue computation.
2394 __ j(not_zero, &partial_remainder_loop);
2395 }
2396 // FPU Stack: input, 2*pi, input % 2*pi
2397 __ fstp(2);
2398 __ fstp(0);
2399 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2400
2401 // FPU Stack: input % 2*pi
2402 __ bind(&in_range);
2403 switch (type_) {
2404 case TranscendentalCache::SIN:
2405 __ fsin();
2406 break;
2407 case TranscendentalCache::COS:
2408 __ fcos();
2409 break;
2410 default:
2411 UNREACHABLE();
2412 }
2413 __ bind(&done);
2414 } else {
2415 ASSERT(type_ == TranscendentalCache::LOG);
2416 __ fldln2();
2417 __ fxch();
2418 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002419 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002420}
2421
2422
ricow@chromium.org65fae842010-08-25 15:26:24 +00002423// Input: edx, eax are the left and right objects of a bit op.
2424// Output: eax, ecx are left and right integers for a bit op.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002425void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2426 bool use_sse3,
2427 Label* conversion_failure) {
2428 // Check float operands.
2429 Label arg1_is_object, check_undefined_arg1;
2430 Label arg2_is_object, check_undefined_arg2;
2431 Label load_arg2, done;
2432
2433 // Test if arg1 is a Smi.
2434 __ test(edx, Immediate(kSmiTagMask));
2435 __ j(not_zero, &arg1_is_object);
2436
2437 __ SmiUntag(edx);
2438 __ jmp(&load_arg2);
2439
2440 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2441 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002442 Factory* factory = masm->isolate()->factory();
2443 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002444 __ j(not_equal, conversion_failure);
2445 __ mov(edx, Immediate(0));
2446 __ jmp(&load_arg2);
2447
2448 __ bind(&arg1_is_object);
2449 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002450 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002451 __ j(not_equal, &check_undefined_arg1);
2452
2453 // Get the untagged integer version of the edx heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002454 IntegerConvert(masm, edx, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002455 __ mov(edx, ecx);
2456
2457 // Here edx has the untagged integer, eax has a Smi or a heap number.
2458 __ bind(&load_arg2);
2459
2460 // Test if arg2 is a Smi.
2461 __ test(eax, Immediate(kSmiTagMask));
2462 __ j(not_zero, &arg2_is_object);
2463
2464 __ SmiUntag(eax);
2465 __ mov(ecx, eax);
2466 __ jmp(&done);
2467
2468 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2469 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002470 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002471 __ j(not_equal, conversion_failure);
2472 __ mov(ecx, Immediate(0));
2473 __ jmp(&done);
2474
2475 __ bind(&arg2_is_object);
2476 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002477 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002478 __ j(not_equal, &check_undefined_arg2);
2479
2480 // Get the untagged integer version of the eax heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002481 IntegerConvert(masm, eax, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002482 __ bind(&done);
2483 __ mov(eax, edx);
2484}
2485
2486
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002487void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2488 bool use_sse3,
2489 Label* not_int32) {
2490 return;
2491}
2492
2493
ricow@chromium.org65fae842010-08-25 15:26:24 +00002494void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2495 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002496 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002497
2498 __ test(number, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002499 __ j(zero, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002500 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002501 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002502
2503 __ bind(&load_smi);
2504 __ SmiUntag(number);
2505 __ push(number);
2506 __ fild_s(Operand(esp, 0));
2507 __ pop(number);
2508
2509 __ bind(&done);
2510}
2511
2512
2513void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002514 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002515 // Load operand in edx into xmm0.
2516 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002517 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002518 __ j(zero, &load_smi_edx, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002519 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2520
2521 __ bind(&load_eax);
2522 // Load operand in eax into xmm1.
2523 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002524 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002525 __ j(zero, &load_smi_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002526 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002527 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002528
2529 __ bind(&load_smi_edx);
2530 __ SmiUntag(edx); // Untag smi before converting to float.
2531 __ cvtsi2sd(xmm0, Operand(edx));
2532 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2533 __ jmp(&load_eax);
2534
2535 __ bind(&load_smi_eax);
2536 __ SmiUntag(eax); // Untag smi before converting to float.
2537 __ cvtsi2sd(xmm1, Operand(eax));
2538 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2539
2540 __ bind(&done);
2541}
2542
2543
2544void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2545 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002546 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002547 // Load operand in edx into xmm0, or branch to not_numbers.
2548 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002549 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002550 __ j(zero, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002551 Factory* factory = masm->isolate()->factory();
2552 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002553 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2554 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2555 __ bind(&load_eax);
2556 // Load operand in eax into xmm1, or branch to not_numbers.
2557 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002558 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002559 __ j(zero, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002560 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002561 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002562 __ jmp(not_numbers); // Argument in eax is not a number.
2563 __ bind(&load_smi_edx);
2564 __ SmiUntag(edx); // Untag smi before converting to float.
2565 __ cvtsi2sd(xmm0, Operand(edx));
2566 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2567 __ jmp(&load_eax);
2568 __ bind(&load_smi_eax);
2569 __ SmiUntag(eax); // Untag smi before converting to float.
2570 __ cvtsi2sd(xmm1, Operand(eax));
2571 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002572 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002573 __ bind(&load_float_eax);
2574 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2575 __ bind(&done);
2576}
2577
2578
2579void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2580 Register scratch) {
2581 const Register left = edx;
2582 const Register right = eax;
2583 __ mov(scratch, left);
2584 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2585 __ SmiUntag(scratch);
2586 __ cvtsi2sd(xmm0, Operand(scratch));
2587
2588 __ mov(scratch, right);
2589 __ SmiUntag(scratch);
2590 __ cvtsi2sd(xmm1, Operand(scratch));
2591}
2592
2593
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002594void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2595 Label* non_int32,
2596 Register scratch) {
2597 __ cvttsd2si(scratch, Operand(xmm0));
2598 __ cvtsi2sd(xmm2, Operand(scratch));
2599 __ ucomisd(xmm0, xmm2);
2600 __ j(not_zero, non_int32);
2601 __ j(carry, non_int32);
2602 __ cvttsd2si(scratch, Operand(xmm1));
2603 __ cvtsi2sd(xmm2, Operand(scratch));
2604 __ ucomisd(xmm1, xmm2);
2605 __ j(not_zero, non_int32);
2606 __ j(carry, non_int32);
2607}
2608
2609
ricow@chromium.org65fae842010-08-25 15:26:24 +00002610void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2611 Register scratch,
2612 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002613 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002614 if (arg_location == ARGS_IN_REGISTERS) {
2615 __ mov(scratch, edx);
2616 } else {
2617 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2618 }
2619 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002620 __ j(zero, &load_smi_1, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002621 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2622 __ bind(&done_load_1);
2623
2624 if (arg_location == ARGS_IN_REGISTERS) {
2625 __ mov(scratch, eax);
2626 } else {
2627 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2628 }
2629 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002630 __ j(zero, &load_smi_2, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002631 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002632 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002633
2634 __ bind(&load_smi_1);
2635 __ SmiUntag(scratch);
2636 __ push(scratch);
2637 __ fild_s(Operand(esp, 0));
2638 __ pop(scratch);
2639 __ jmp(&done_load_1);
2640
2641 __ bind(&load_smi_2);
2642 __ SmiUntag(scratch);
2643 __ push(scratch);
2644 __ fild_s(Operand(esp, 0));
2645 __ pop(scratch);
2646
2647 __ bind(&done);
2648}
2649
2650
2651void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2652 Register scratch) {
2653 const Register left = edx;
2654 const Register right = eax;
2655 __ mov(scratch, left);
2656 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2657 __ SmiUntag(scratch);
2658 __ push(scratch);
2659 __ fild_s(Operand(esp, 0));
2660
2661 __ mov(scratch, right);
2662 __ SmiUntag(scratch);
2663 __ mov(Operand(esp, 0), scratch);
2664 __ fild_s(Operand(esp, 0));
2665 __ pop(scratch);
2666}
2667
2668
2669void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2670 Label* non_float,
2671 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002672 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002673 // Test if both operands are floats or smi -> scratch=k_is_float;
2674 // Otherwise scratch = k_not_float.
2675 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002676 __ j(zero, &test_other, Label::kNear); // argument in edx is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002677 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002678 Factory* factory = masm->isolate()->factory();
2679 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002680 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2681
2682 __ bind(&test_other);
2683 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002684 __ j(zero, &done, Label::kNear); // argument in eax is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002685 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002686 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002687 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2688
2689 // Fall-through: Both operands are numbers.
2690 __ bind(&done);
2691}
2692
2693
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002694void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2695 Label* non_int32) {
2696 return;
2697}
2698
2699
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002700void MathPowStub::Generate(MacroAssembler* masm) {
2701 // Registers are used as follows:
2702 // edx = base
2703 // eax = exponent
2704 // ecx = temporary, result
2705
2706 CpuFeatures::Scope use_sse2(SSE2);
2707 Label allocate_return, call_runtime;
2708
2709 // Load input parameters.
2710 __ mov(edx, Operand(esp, 2 * kPointerSize));
2711 __ mov(eax, Operand(esp, 1 * kPointerSize));
2712
2713 // Save 1 in xmm3 - we need this several times later on.
2714 __ mov(ecx, Immediate(1));
2715 __ cvtsi2sd(xmm3, Operand(ecx));
2716
2717 Label exponent_nonsmi;
2718 Label base_nonsmi;
2719 // If the exponent is a heap number go to that specific case.
2720 __ test(eax, Immediate(kSmiTagMask));
2721 __ j(not_zero, &exponent_nonsmi);
2722 __ test(edx, Immediate(kSmiTagMask));
2723 __ j(not_zero, &base_nonsmi);
2724
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002725 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002726 Label powi;
2727 __ SmiUntag(edx);
2728 __ cvtsi2sd(xmm0, Operand(edx));
2729 __ jmp(&powi);
2730 // exponent is smi and base is a heapnumber.
2731 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002732 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002733 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002734 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002735 __ j(not_equal, &call_runtime);
2736
2737 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2738
2739 // Optimized version of pow if exponent is a smi.
2740 // xmm0 contains the base.
2741 __ bind(&powi);
2742 __ SmiUntag(eax);
2743
2744 // Save exponent in base as we need to check if exponent is negative later.
2745 // We know that base and exponent are in different registers.
2746 __ mov(edx, eax);
2747
2748 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002749 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002750 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002751 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002752 __ neg(eax);
2753 __ bind(&no_neg);
2754
2755 // Load xmm1 with 1.
2756 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002757 Label while_true;
2758 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002759
2760 __ bind(&while_true);
2761 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002762 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002763 __ mulsd(xmm1, xmm0);
2764 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002765 __ mulsd(xmm0, xmm0);
2766 __ j(not_zero, &while_true);
2767
2768 // base has the original value of the exponent - if the exponent is
2769 // negative return 1/result.
2770 __ test(edx, Operand(edx));
2771 __ j(positive, &allocate_return);
2772 // Special case if xmm1 has reached infinity.
2773 __ mov(ecx, Immediate(0x7FB00000));
2774 __ movd(xmm0, Operand(ecx));
2775 __ cvtss2sd(xmm0, xmm0);
2776 __ ucomisd(xmm0, xmm1);
2777 __ j(equal, &call_runtime);
2778 __ divsd(xmm3, xmm1);
2779 __ movsd(xmm1, xmm3);
2780 __ jmp(&allocate_return);
2781
2782 // exponent (or both) is a heapnumber - no matter what we should now work
2783 // on doubles.
2784 __ bind(&exponent_nonsmi);
2785 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002786 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002787 __ j(not_equal, &call_runtime);
2788 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2789 // Test if exponent is nan.
2790 __ ucomisd(xmm1, xmm1);
2791 __ j(parity_even, &call_runtime);
2792
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002793 Label base_not_smi;
2794 Label handle_special_cases;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002795 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002796 __ j(not_zero, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002797 __ SmiUntag(edx);
2798 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002799 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002800
2801 __ bind(&base_not_smi);
2802 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002803 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002804 __ j(not_equal, &call_runtime);
2805 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2806 __ and_(ecx, HeapNumber::kExponentMask);
2807 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2808 // base is NaN or +/-Infinity
2809 __ j(greater_equal, &call_runtime);
2810 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2811
2812 // base is in xmm0 and exponent is in xmm1.
2813 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002814 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002815 // Test for -0.5.
2816 // Load xmm2 with -0.5.
2817 __ mov(ecx, Immediate(0xBF000000));
2818 __ movd(xmm2, Operand(ecx));
2819 __ cvtss2sd(xmm2, xmm2);
2820 // xmm2 now has -0.5.
2821 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002822 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002823
2824 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002825 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002826 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002827 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002828 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002829 __ divsd(xmm3, xmm1);
2830 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002831 __ jmp(&allocate_return);
2832
2833 // Test for 0.5.
2834 __ bind(&not_minus_half);
2835 // Load xmm2 with 0.5.
2836 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2837 __ addsd(xmm2, xmm3);
2838 // xmm2 now has 0.5.
2839 __ ucomisd(xmm2, xmm1);
2840 __ j(not_equal, &call_runtime);
2841 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002842 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002843 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002844 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002845 __ sqrtsd(xmm1, xmm1);
2846
2847 __ bind(&allocate_return);
2848 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2849 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2850 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002851 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002852
2853 __ bind(&call_runtime);
2854 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2855}
2856
2857
ricow@chromium.org65fae842010-08-25 15:26:24 +00002858void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2859 // The key is in edx and the parameter count is in eax.
2860
2861 // The displacement is used for skipping the frame pointer on the
2862 // stack. It is the offset of the last parameter (if any) relative
2863 // to the frame pointer.
2864 static const int kDisplacement = 1 * kPointerSize;
2865
2866 // Check that the key is a smi.
2867 Label slow;
2868 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002869 __ j(not_zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002870
2871 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002872 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002873 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2874 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2875 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002876 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002877
2878 // Check index against formal parameters count limit passed in
2879 // through register eax. Use unsigned comparison to get negative
2880 // check for free.
2881 __ cmp(edx, Operand(eax));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002882 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002883
2884 // Read the argument from the stack and return it.
2885 STATIC_ASSERT(kSmiTagSize == 1);
2886 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2887 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2888 __ neg(edx);
2889 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2890 __ ret(0);
2891
2892 // Arguments adaptor case: Check index against actual arguments
2893 // limit found in the arguments adaptor frame. Use unsigned
2894 // comparison to get negative check for free.
2895 __ bind(&adaptor);
2896 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2897 __ cmp(edx, Operand(ecx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002898 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002899
2900 // Read the argument from the stack and return it.
2901 STATIC_ASSERT(kSmiTagSize == 1);
2902 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2903 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2904 __ neg(edx);
2905 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2906 __ ret(0);
2907
2908 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2909 // by calling the runtime system.
2910 __ bind(&slow);
2911 __ pop(ebx); // Return address.
2912 __ push(edx);
2913 __ push(ebx);
2914 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2915}
2916
2917
2918void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2919 // esp[0] : return address
2920 // esp[4] : number of parameters
2921 // esp[8] : receiver displacement
2922 // esp[16] : function
2923
2924 // The displacement is used for skipping the return address and the
2925 // frame pointer on the stack. It is the offset of the last
2926 // parameter (if any) relative to the frame pointer.
2927 static const int kDisplacement = 2 * kPointerSize;
2928
2929 // Check if the calling frame is an arguments adaptor frame.
2930 Label adaptor_frame, try_allocate, runtime;
2931 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2932 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2933 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2934 __ j(equal, &adaptor_frame);
2935
2936 // Get the length from the frame.
2937 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2938 __ jmp(&try_allocate);
2939
2940 // Patch the arguments.length and the parameters pointer.
2941 __ bind(&adaptor_frame);
2942 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2943 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2944 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
2945 __ mov(Operand(esp, 2 * kPointerSize), edx);
2946
2947 // Try the new space allocation. Start out with computing the size of
2948 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002949 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002950 __ bind(&try_allocate);
2951 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002952 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002953 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
2954 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002955 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002956
2957 // Do the allocation of both objects in one go.
2958 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
2959
2960 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002961 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2962 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002963 __ mov(edi, Operand(edi,
2964 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002965
2966 // Copy the JS object part.
2967 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2968 __ mov(ebx, FieldOperand(edi, i));
2969 __ mov(FieldOperand(eax, i), ebx);
2970 }
2971
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002972 if (type_ == NEW_NON_STRICT) {
2973 // Setup the callee in-object property.
2974 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2975 __ mov(ebx, Operand(esp, 3 * kPointerSize));
2976 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2977 Heap::kArgumentsCalleeIndex * kPointerSize),
2978 ebx);
2979 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002980
2981 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002982 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002983 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002984 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2985 Heap::kArgumentsLengthIndex * kPointerSize),
2986 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002987
2988 // If there are no actual arguments, we're done.
2989 Label done;
2990 __ test(ecx, Operand(ecx));
2991 __ j(zero, &done);
2992
2993 // Get the parameters pointer from the stack.
2994 __ mov(edx, Operand(esp, 2 * kPointerSize));
2995
2996 // Setup the elements pointer in the allocated arguments object and
2997 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002998 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002999 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3000 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003001 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003002
ricow@chromium.org65fae842010-08-25 15:26:24 +00003003 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3004 // Untag the length for the loop below.
3005 __ SmiUntag(ecx);
3006
3007 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003008 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003009 __ bind(&loop);
3010 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3011 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3012 __ add(Operand(edi), Immediate(kPointerSize));
3013 __ sub(Operand(edx), Immediate(kPointerSize));
3014 __ dec(ecx);
3015 __ j(not_zero, &loop);
3016
3017 // Return and remove the on-stack parameters.
3018 __ bind(&done);
3019 __ ret(3 * kPointerSize);
3020
3021 // Do the runtime call to allocate the arguments object.
3022 __ bind(&runtime);
3023 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3024}
3025
3026
3027void RegExpExecStub::Generate(MacroAssembler* masm) {
3028 // Just jump directly to runtime if native RegExp is not selected at compile
3029 // time or if regexp entry in generated code is turned off runtime switch or
3030 // at compilation.
3031#ifdef V8_INTERPRETED_REGEXP
3032 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3033#else // V8_INTERPRETED_REGEXP
3034 if (!FLAG_regexp_entry_native) {
3035 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3036 return;
3037 }
3038
3039 // Stack frame on entry.
3040 // esp[0]: return address
3041 // esp[4]: last_match_info (expected JSArray)
3042 // esp[8]: previous index
3043 // esp[12]: subject string
3044 // esp[16]: JSRegExp object
3045
3046 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3047 static const int kPreviousIndexOffset = 2 * kPointerSize;
3048 static const int kSubjectOffset = 3 * kPointerSize;
3049 static const int kJSRegExpOffset = 4 * kPointerSize;
3050
3051 Label runtime, invoke_regexp;
3052
3053 // Ensure that a RegExp stack is allocated.
3054 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003055 ExternalReference::address_of_regexp_stack_memory_address(
3056 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003057 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003058 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003059 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3060 __ test(ebx, Operand(ebx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003061 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003062
3063 // Check that the first argument is a JSRegExp object.
3064 __ mov(eax, Operand(esp, kJSRegExpOffset));
3065 STATIC_ASSERT(kSmiTag == 0);
3066 __ test(eax, Immediate(kSmiTagMask));
3067 __ j(zero, &runtime);
3068 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3069 __ j(not_equal, &runtime);
3070 // Check that the RegExp has been compiled (data contains a fixed array).
3071 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3072 if (FLAG_debug_code) {
3073 __ test(ecx, Immediate(kSmiTagMask));
3074 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3075 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3076 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3077 }
3078
3079 // ecx: RegExp data (FixedArray)
3080 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3081 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3082 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3083 __ j(not_equal, &runtime);
3084
3085 // ecx: RegExp data (FixedArray)
3086 // Check that the number of captures fit in the static offsets vector buffer.
3087 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3088 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3089 // uses the asumption that smis are 2 * their untagged value.
3090 STATIC_ASSERT(kSmiTag == 0);
3091 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3092 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3093 // Check that the static offsets vector buffer is large enough.
3094 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3095 __ j(above, &runtime);
3096
3097 // ecx: RegExp data (FixedArray)
3098 // edx: Number of capture registers
3099 // Check that the second argument is a string.
3100 __ mov(eax, Operand(esp, kSubjectOffset));
3101 __ test(eax, Immediate(kSmiTagMask));
3102 __ j(zero, &runtime);
3103 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3104 __ j(NegateCondition(is_string), &runtime);
3105 // Get the length of the string to ebx.
3106 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3107
3108 // ebx: Length of subject string as a smi
3109 // ecx: RegExp data (FixedArray)
3110 // edx: Number of capture registers
3111 // Check that the third argument is a positive smi less than the subject
3112 // string length. A negative value will be greater (unsigned comparison).
3113 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3114 __ test(eax, Immediate(kSmiTagMask));
3115 __ j(not_zero, &runtime);
3116 __ cmp(eax, Operand(ebx));
3117 __ j(above_equal, &runtime);
3118
3119 // ecx: RegExp data (FixedArray)
3120 // edx: Number of capture registers
3121 // Check that the fourth object is a JSArray object.
3122 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3123 __ test(eax, Immediate(kSmiTagMask));
3124 __ j(zero, &runtime);
3125 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3126 __ j(not_equal, &runtime);
3127 // Check that the JSArray is in fast case.
3128 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3129 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003130 Factory* factory = masm->isolate()->factory();
3131 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003132 __ j(not_equal, &runtime);
3133 // Check that the last match info has space for the capture registers and the
3134 // additional information.
3135 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3136 __ SmiUntag(eax);
3137 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3138 __ cmp(edx, Operand(eax));
3139 __ j(greater, &runtime);
3140
3141 // ecx: RegExp data (FixedArray)
3142 // Check the representation and encoding of the subject string.
3143 Label seq_ascii_string, seq_two_byte_string, check_code;
3144 __ mov(eax, Operand(esp, kSubjectOffset));
3145 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3146 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3147 // First check for flat two byte string.
3148 __ and_(ebx,
3149 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3150 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3151 __ j(zero, &seq_two_byte_string);
3152 // Any other flat string must be a flat ascii string.
3153 __ test(Operand(ebx),
3154 Immediate(kIsNotStringMask | kStringRepresentationMask));
3155 __ j(zero, &seq_ascii_string);
3156
3157 // Check for flat cons string.
3158 // A flat cons string is a cons string where the second part is the empty
3159 // string. In that case the subject string is just the first part of the cons
3160 // string. Also in this case the first part of the cons string is known to be
3161 // a sequential string or an external string.
3162 STATIC_ASSERT(kExternalStringTag != 0);
3163 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3164 __ test(Operand(ebx),
3165 Immediate(kIsNotStringMask | kExternalStringTag));
3166 __ j(not_zero, &runtime);
3167 // String is a cons string.
3168 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003169 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003170 __ j(not_equal, &runtime);
3171 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3172 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3173 // String is a cons string with empty second part.
3174 // eax: first part of cons string.
3175 // ebx: map of first part of cons string.
3176 // Is first part a flat two byte string?
3177 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3178 kStringRepresentationMask | kStringEncodingMask);
3179 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3180 __ j(zero, &seq_two_byte_string);
3181 // Any other flat string must be ascii.
3182 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3183 kStringRepresentationMask);
3184 __ j(not_zero, &runtime);
3185
3186 __ bind(&seq_ascii_string);
3187 // eax: subject string (flat ascii)
3188 // ecx: RegExp data (FixedArray)
3189 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3190 __ Set(edi, Immediate(1)); // Type is ascii.
3191 __ jmp(&check_code);
3192
3193 __ bind(&seq_two_byte_string);
3194 // eax: subject string (flat two byte)
3195 // ecx: RegExp data (FixedArray)
3196 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3197 __ Set(edi, Immediate(0)); // Type is two byte.
3198
3199 __ bind(&check_code);
3200 // Check that the irregexp code has been generated for the actual string
3201 // encoding. If it has, the field contains a code object otherwise it contains
3202 // the hole.
3203 __ CmpObjectType(edx, CODE_TYPE, ebx);
3204 __ j(not_equal, &runtime);
3205
3206 // eax: subject string
3207 // edx: code
3208 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3209 // Load used arguments before starting to push arguments for call to native
3210 // RegExp code to avoid handling changing stack height.
3211 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3212 __ SmiUntag(ebx); // Previous index from smi.
3213
3214 // eax: subject string
3215 // ebx: previous index
3216 // edx: code
3217 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3218 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003219 Counters* counters = masm->isolate()->counters();
3220 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003221
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003222 // Isolates: note we add an additional parameter here (isolate pointer).
3223 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003224 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003225
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003226 // Argument 8: Pass current isolate address.
3227 __ mov(Operand(esp, 7 * kPointerSize),
3228 Immediate(ExternalReference::isolate_address()));
3229
ricow@chromium.org65fae842010-08-25 15:26:24 +00003230 // Argument 7: Indicate that this is a direct call from JavaScript.
3231 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3232
3233 // Argument 6: Start (high end) of backtracking stack memory area.
3234 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3235 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3236 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3237
3238 // Argument 5: static offsets vector buffer.
3239 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003240 Immediate(ExternalReference::address_of_static_offsets_vector(
3241 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003242
3243 // Argument 4: End of string data
3244 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003245 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003246 __ test(edi, Operand(edi));
3247 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003248 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003249 __ SmiUntag(edi);
3250 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3251 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3252 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3253 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003254 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003255
3256 __ bind(&setup_two_byte);
3257 STATIC_ASSERT(kSmiTag == 0);
3258 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3259 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3260 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3261 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3262 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3263
3264 __ bind(&setup_rest);
3265
3266 // Argument 2: Previous index.
3267 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3268
3269 // Argument 1: Subject string.
3270 __ mov(Operand(esp, 0 * kPointerSize), eax);
3271
3272 // Locate the code entry and call it.
3273 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003274 __ call(Operand(edx));
3275
3276 // Drop arguments and come back to JS mode.
3277 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003278
3279 // Check the result.
3280 Label success;
3281 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003282 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003283 Label failure;
3284 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003285 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003286 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3287 // If not exception it can only be retry. Handle that in the runtime system.
3288 __ j(not_equal, &runtime);
3289 // Result must now be exception. If there is no pending exception already a
3290 // stack overflow (on the backtrack stack) was detected in RegExp code but
3291 // haven't created the exception yet. Handle that in the runtime system.
3292 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003293 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3294 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003295 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003296 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3297 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003298 __ mov(eax, Operand::StaticVariable(pending_exception));
3299 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003300 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003301 // For exception, throw the exception again.
3302
3303 // Clear the pending exception variable.
3304 __ mov(Operand::StaticVariable(pending_exception), edx);
3305
3306 // Special handling of termination exceptions which are uncatchable
3307 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003308 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003309 Label throw_termination_exception;
3310 __ j(equal, &throw_termination_exception);
3311
3312 // Handle normal exception by following handler chain.
3313 __ Throw(eax);
3314
3315 __ bind(&throw_termination_exception);
3316 __ ThrowUncatchable(TERMINATION, eax);
3317
ricow@chromium.org65fae842010-08-25 15:26:24 +00003318 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003319 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003320 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003321 __ ret(4 * kPointerSize);
3322
3323 // Load RegExp data.
3324 __ bind(&success);
3325 __ mov(eax, Operand(esp, kJSRegExpOffset));
3326 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3327 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3328 // Calculate number of capture registers (number_of_captures + 1) * 2.
3329 STATIC_ASSERT(kSmiTag == 0);
3330 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3331 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3332
3333 // edx: Number of capture registers
3334 // Load last_match_info which is still known to be a fast case JSArray.
3335 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3336 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3337
3338 // ebx: last_match_info backing store (FixedArray)
3339 // edx: number of capture registers
3340 // Store the capture count.
3341 __ SmiTag(edx); // Number of capture registers to smi.
3342 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3343 __ SmiUntag(edx); // Number of capture registers back from smi.
3344 // Store last subject and last input.
3345 __ mov(eax, Operand(esp, kSubjectOffset));
3346 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3347 __ mov(ecx, ebx);
3348 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3349 __ mov(eax, Operand(esp, kSubjectOffset));
3350 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3351 __ mov(ecx, ebx);
3352 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3353
3354 // Get the static offsets vector filled by the native regexp code.
3355 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003356 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003357 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3358
3359 // ebx: last_match_info backing store (FixedArray)
3360 // ecx: offsets vector
3361 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003362 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003363 // Capture register counter starts from number of capture registers and
3364 // counts down until wraping after zero.
3365 __ bind(&next_capture);
3366 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003367 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003368 // Read the value from the static offsets vector buffer.
3369 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3370 __ SmiTag(edi);
3371 // Store the smi value in the last match info.
3372 __ mov(FieldOperand(ebx,
3373 edx,
3374 times_pointer_size,
3375 RegExpImpl::kFirstCaptureOffset),
3376 edi);
3377 __ jmp(&next_capture);
3378 __ bind(&done);
3379
3380 // Return last match info.
3381 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3382 __ ret(4 * kPointerSize);
3383
3384 // Do the runtime call to execute the regexp.
3385 __ bind(&runtime);
3386 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3387#endif // V8_INTERPRETED_REGEXP
3388}
3389
3390
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003391void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3392 const int kMaxInlineLength = 100;
3393 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003394 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003395 __ mov(ebx, Operand(esp, kPointerSize * 3));
3396 __ test(ebx, Immediate(kSmiTagMask));
3397 __ j(not_zero, &slowcase);
3398 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3399 __ j(above, &slowcase);
3400 // Smi-tagging is equivalent to multiplying by 2.
3401 STATIC_ASSERT(kSmiTag == 0);
3402 STATIC_ASSERT(kSmiTagSize == 1);
3403 // Allocate RegExpResult followed by FixedArray with size in ebx.
3404 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3405 // Elements: [Map][Length][..elements..]
3406 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3407 times_half_pointer_size,
3408 ebx, // In: Number of elements (times 2, being a smi)
3409 eax, // Out: Start of allocation (tagged).
3410 ecx, // Out: End of allocation.
3411 edx, // Scratch register
3412 &slowcase,
3413 TAG_OBJECT);
3414 // eax: Start of allocated area, object-tagged.
3415
3416 // Set JSArray map to global.regexp_result_map().
3417 // Set empty properties FixedArray.
3418 // Set elements to point to FixedArray allocated right after the JSArray.
3419 // Interleave operations for better latency.
3420 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003421 Factory* factory = masm->isolate()->factory();
3422 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003423 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3424 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3425 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3426 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3427 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3428 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3429
3430 // Set input, index and length fields from arguments.
3431 __ mov(ecx, Operand(esp, kPointerSize * 1));
3432 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3433 __ mov(ecx, Operand(esp, kPointerSize * 2));
3434 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3435 __ mov(ecx, Operand(esp, kPointerSize * 3));
3436 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3437
3438 // Fill out the elements FixedArray.
3439 // eax: JSArray.
3440 // ebx: FixedArray.
3441 // ecx: Number of elements in array, as smi.
3442
3443 // Set map.
3444 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003445 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003446 // Set length.
3447 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3448 // Fill contents of fixed-array with the-hole.
3449 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003450 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003451 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3452 // Fill fixed array elements with hole.
3453 // eax: JSArray.
3454 // ecx: Number of elements to fill.
3455 // ebx: Start of elements in FixedArray.
3456 // edx: the hole.
3457 Label loop;
3458 __ test(ecx, Operand(ecx));
3459 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003460 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003461 __ sub(Operand(ecx), Immediate(1));
3462 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3463 __ jmp(&loop);
3464
3465 __ bind(&done);
3466 __ ret(3 * kPointerSize);
3467
3468 __ bind(&slowcase);
3469 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3470}
3471
3472
ricow@chromium.org65fae842010-08-25 15:26:24 +00003473void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3474 Register object,
3475 Register result,
3476 Register scratch1,
3477 Register scratch2,
3478 bool object_is_smi,
3479 Label* not_found) {
3480 // Use of registers. Register result is used as a temporary.
3481 Register number_string_cache = result;
3482 Register mask = scratch1;
3483 Register scratch = scratch2;
3484
3485 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003486 ExternalReference roots_address =
3487 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003488 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3489 __ mov(number_string_cache,
3490 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3491 // Make the hash mask from the length of the number string cache. It
3492 // contains two elements (number and string) for each cache entry.
3493 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3494 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3495 __ sub(Operand(mask), Immediate(1)); // Make mask.
3496
3497 // Calculate the entry in the number string cache. The hash value in the
3498 // number string cache for smis is just the smi value, and the hash for
3499 // doubles is the xor of the upper and lower words. See
3500 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003501 Label smi_hash_calculated;
3502 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003503 if (object_is_smi) {
3504 __ mov(scratch, object);
3505 __ SmiUntag(scratch);
3506 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003507 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003508 STATIC_ASSERT(kSmiTag == 0);
3509 __ test(object, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003510 __ j(not_zero, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003511 __ mov(scratch, object);
3512 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003513 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003514 __ bind(&not_smi);
3515 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003516 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003517 __ j(not_equal, not_found);
3518 STATIC_ASSERT(8 == kDoubleSize);
3519 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3520 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3521 // Object is heap number and hash is now in scratch. Calculate cache index.
3522 __ and_(scratch, Operand(mask));
3523 Register index = scratch;
3524 Register probe = mask;
3525 __ mov(probe,
3526 FieldOperand(number_string_cache,
3527 index,
3528 times_twice_pointer_size,
3529 FixedArray::kHeaderSize));
3530 __ test(probe, Immediate(kSmiTagMask));
3531 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003532 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003533 CpuFeatures::Scope fscope(SSE2);
3534 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3535 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3536 __ ucomisd(xmm0, xmm1);
3537 } else {
3538 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3539 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3540 __ FCmp();
3541 }
3542 __ j(parity_even, not_found); // Bail out if NaN is involved.
3543 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003544 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003545 }
3546
3547 __ bind(&smi_hash_calculated);
3548 // Object is smi and hash is now in scratch. Calculate cache index.
3549 __ and_(scratch, Operand(mask));
3550 Register index = scratch;
3551 // Check if the entry is the smi we are looking for.
3552 __ cmp(object,
3553 FieldOperand(number_string_cache,
3554 index,
3555 times_twice_pointer_size,
3556 FixedArray::kHeaderSize));
3557 __ j(not_equal, not_found);
3558
3559 // Get the result from the cache.
3560 __ bind(&load_result_from_cache);
3561 __ mov(result,
3562 FieldOperand(number_string_cache,
3563 index,
3564 times_twice_pointer_size,
3565 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003566 Counters* counters = masm->isolate()->counters();
3567 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003568}
3569
3570
3571void NumberToStringStub::Generate(MacroAssembler* masm) {
3572 Label runtime;
3573
3574 __ mov(ebx, Operand(esp, kPointerSize));
3575
3576 // Generate code to lookup number in the number string cache.
3577 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3578 __ ret(1 * kPointerSize);
3579
3580 __ bind(&runtime);
3581 // Handle number to string in the runtime system if not found in the cache.
3582 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3583}
3584
3585
3586static int NegativeComparisonResult(Condition cc) {
3587 ASSERT(cc != equal);
3588 ASSERT((cc == less) || (cc == less_equal)
3589 || (cc == greater) || (cc == greater_equal));
3590 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3591}
3592
3593void CompareStub::Generate(MacroAssembler* masm) {
3594 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3595
3596 Label check_unequal_objects, done;
3597
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003598 // Compare two smis if required.
3599 if (include_smi_compare_) {
3600 Label non_smi, smi_done;
3601 __ mov(ecx, Operand(edx));
3602 __ or_(ecx, Operand(eax));
3603 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003604 __ j(not_zero, &non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003605 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3606 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003607 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003608 __ bind(&smi_done);
3609 __ mov(eax, edx);
3610 __ ret(0);
3611 __ bind(&non_smi);
3612 } else if (FLAG_debug_code) {
3613 __ mov(ecx, Operand(edx));
3614 __ or_(ecx, Operand(eax));
3615 __ test(ecx, Immediate(kSmiTagMask));
3616 __ Assert(not_zero, "Unexpected smi operands.");
3617 }
3618
ricow@chromium.org65fae842010-08-25 15:26:24 +00003619 // NOTICE! This code is only reached after a smi-fast-case check, so
3620 // it is certain that at least one operand isn't a smi.
3621
3622 // Identical objects can be compared fast, but there are some tricky cases
3623 // for NaN and undefined.
3624 {
3625 Label not_identical;
3626 __ cmp(eax, Operand(edx));
3627 __ j(not_equal, &not_identical);
3628
3629 if (cc_ != equal) {
3630 // Check for undefined. undefined OP undefined is false even though
3631 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003632 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003633 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003634 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003635 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3636 __ ret(0);
3637 __ bind(&check_for_nan);
3638 }
3639
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003640 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003641 // so we do the second best thing - test it ourselves.
3642 // Note: if cc_ != equal, never_nan_nan_ is not used.
3643 if (never_nan_nan_ && (cc_ == equal)) {
3644 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3645 __ ret(0);
3646 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003647 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003648 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003649 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003650 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003651 if (cc_ != equal) {
3652 // Call runtime on identical JSObjects. Otherwise return equal.
3653 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3654 __ j(above_equal, &not_identical);
3655 }
3656 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3657 __ ret(0);
3658
3659 __ bind(&heap_number);
3660 // It is a heap number, so return non-equal if it's NaN and equal if
3661 // it's not NaN.
3662 // The representation of NaN values has all exponent bits (52..62) set,
3663 // and not all mantissa bits (0..51) clear.
3664 // We only accept QNaNs, which have bit 51 set.
3665 // Read top bits of double representation (second word of value).
3666
3667 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3668 // all bits in the mask are set. We only need to check the word
3669 // that contains the exponent and high bit of the mantissa.
3670 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3671 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003672 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003673 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3674 // bits.
3675 __ add(edx, Operand(edx));
3676 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3677 if (cc_ == equal) {
3678 STATIC_ASSERT(EQUAL != 1);
3679 __ setcc(above_equal, eax);
3680 __ ret(0);
3681 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003682 Label nan;
3683 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003684 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3685 __ ret(0);
3686 __ bind(&nan);
3687 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3688 __ ret(0);
3689 }
3690 }
3691
3692 __ bind(&not_identical);
3693 }
3694
3695 // Strict equality can quickly decide whether objects are equal.
3696 // Non-strict object equality is slower, so it is handled later in the stub.
3697 if (cc_ == equal && strict_) {
3698 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003699 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003700 // If we're doing a strict equality comparison, we don't have to do
3701 // type conversion, so we generate code to do fast comparison for objects
3702 // and oddballs. Non-smi numbers and strings still go through the usual
3703 // slow-case code.
3704 // If either is a Smi (we know that not both are), then they can only
3705 // be equal if the other is a HeapNumber. If so, use the slow case.
3706 STATIC_ASSERT(kSmiTag == 0);
3707 ASSERT_EQ(0, Smi::FromInt(0));
3708 __ mov(ecx, Immediate(kSmiTagMask));
3709 __ and_(ecx, Operand(eax));
3710 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003711 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003712 // One operand is a smi.
3713
3714 // Check whether the non-smi is a heap number.
3715 STATIC_ASSERT(kSmiTagMask == 1);
3716 // ecx still holds eax & kSmiTag, which is either zero or one.
3717 __ sub(Operand(ecx), Immediate(0x01));
3718 __ mov(ebx, edx);
3719 __ xor_(ebx, Operand(eax));
3720 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3721 __ xor_(ebx, Operand(eax));
3722 // if eax was smi, ebx is now edx, else eax.
3723
3724 // Check if the non-smi operand is a heap number.
3725 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003726 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003727 // If heap number, handle it in the slow case.
3728 __ j(equal, &slow);
3729 // Return non-equal (ebx is not zero)
3730 __ mov(eax, ebx);
3731 __ ret(0);
3732
3733 __ bind(&not_smis);
3734 // If either operand is a JSObject or an oddball value, then they are not
3735 // equal since their pointers are different
3736 // There is no test for undetectability in strict equality.
3737
3738 // Get the type of the first operand.
3739 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003740 Label first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003741 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3742 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003743 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003744
3745 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003746 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003747 STATIC_ASSERT(kHeapObjectTag != 0);
3748 __ bind(&return_not_equal);
3749 __ ret(0);
3750
3751 __ bind(&first_non_object);
3752 // Check for oddballs: true, false, null, undefined.
3753 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3754 __ j(equal, &return_not_equal);
3755
3756 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
3757 __ j(above_equal, &return_not_equal);
3758
3759 // Check for oddballs: true, false, null, undefined.
3760 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3761 __ j(equal, &return_not_equal);
3762
3763 // Fall through to the general case.
3764 __ bind(&slow);
3765 }
3766
3767 // Generate the number comparison code.
3768 if (include_number_compare_) {
3769 Label non_number_comparison;
3770 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003771 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003772 CpuFeatures::Scope use_sse2(SSE2);
3773 CpuFeatures::Scope use_cmov(CMOV);
3774
3775 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3776 __ ucomisd(xmm0, xmm1);
3777
3778 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003779 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003780 // Return a result of -1, 0, or 1, based on EFLAGS.
3781 __ mov(eax, 0); // equal
3782 __ mov(ecx, Immediate(Smi::FromInt(1)));
3783 __ cmov(above, eax, Operand(ecx));
3784 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3785 __ cmov(below, eax, Operand(ecx));
3786 __ ret(0);
3787 } else {
3788 FloatingPointHelper::CheckFloatOperands(
3789 masm, &non_number_comparison, ebx);
3790 FloatingPointHelper::LoadFloatOperand(masm, eax);
3791 FloatingPointHelper::LoadFloatOperand(masm, edx);
3792 __ FCmp();
3793
3794 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003795 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003796
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003797 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003798 // Return a result of -1, 0, or 1, based on EFLAGS.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003799 __ j(below, &below_label);
3800 __ j(above, &above_label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003801
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003802 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003803 __ ret(0);
3804
3805 __ bind(&below_label);
3806 __ mov(eax, Immediate(Smi::FromInt(-1)));
3807 __ ret(0);
3808
3809 __ bind(&above_label);
3810 __ mov(eax, Immediate(Smi::FromInt(1)));
3811 __ ret(0);
3812 }
3813
3814 // If one of the numbers was NaN, then the result is always false.
3815 // The cc is never not-equal.
3816 __ bind(&unordered);
3817 ASSERT(cc_ != not_equal);
3818 if (cc_ == less || cc_ == less_equal) {
3819 __ mov(eax, Immediate(Smi::FromInt(1)));
3820 } else {
3821 __ mov(eax, Immediate(Smi::FromInt(-1)));
3822 }
3823 __ ret(0);
3824
3825 // The number comparison code did not provide a valid result.
3826 __ bind(&non_number_comparison);
3827 }
3828
3829 // Fast negative check for symbol-to-symbol equality.
3830 Label check_for_strings;
3831 if (cc_ == equal) {
3832 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
3833 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
3834
3835 // We've already checked for object identity, so if both operands
3836 // are symbols they aren't equal. Register eax already holds a
3837 // non-zero value, which indicates not equal, so just return.
3838 __ ret(0);
3839 }
3840
3841 __ bind(&check_for_strings);
3842
3843 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
3844 &check_unequal_objects);
3845
3846 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003847 if (cc_ == equal) {
3848 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00003849 edx,
3850 eax,
3851 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00003852 ebx);
3853 } else {
3854 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3855 edx,
3856 eax,
3857 ecx,
3858 ebx,
3859 edi);
3860 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003861#ifdef DEBUG
3862 __ Abort("Unexpected fall-through from string comparison");
3863#endif
3864
3865 __ bind(&check_unequal_objects);
3866 if (cc_ == equal && !strict_) {
3867 // Non-strict equality. Objects are unequal if
3868 // they are both JSObjects and not undetectable,
3869 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003870 Label not_both_objects;
3871 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003872 // At most one is a smi, so we can test for smi by adding the two.
3873 // A smi plus a heap object has the low bit set, a heap object plus
3874 // a heap object has the low bit clear.
3875 STATIC_ASSERT(kSmiTag == 0);
3876 STATIC_ASSERT(kSmiTagMask == 1);
3877 __ lea(ecx, Operand(eax, edx, times_1, 0));
3878 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003879 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003880 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003881 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003882 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003883 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003884 // We do not bail out after this point. Both are JSObjects, and
3885 // they are equal if and only if both are undetectable.
3886 // The and of the undetectable flags is 1 if and only if they are equal.
3887 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3888 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003889 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003890 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
3891 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003892 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003893 // The objects are both undetectable, so they both compare as the value
3894 // undefined, and are equal.
3895 __ Set(eax, Immediate(EQUAL));
3896 __ bind(&return_unequal);
3897 // Return non-equal by returning the non-zero object pointer in eax,
3898 // or return equal if we fell through to here.
3899 __ ret(0); // rax, rdx were pushed
3900 __ bind(&not_both_objects);
3901 }
3902
3903 // Push arguments below the return address.
3904 __ pop(ecx);
3905 __ push(edx);
3906 __ push(eax);
3907
3908 // Figure out which native to call and setup the arguments.
3909 Builtins::JavaScript builtin;
3910 if (cc_ == equal) {
3911 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3912 } else {
3913 builtin = Builtins::COMPARE;
3914 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3915 }
3916
3917 // Restore return address on the stack.
3918 __ push(ecx);
3919
3920 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3921 // tagged as a small integer.
3922 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3923}
3924
3925
3926void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3927 Label* label,
3928 Register object,
3929 Register scratch) {
3930 __ test(object, Immediate(kSmiTagMask));
3931 __ j(zero, label);
3932 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
3933 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3934 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
3935 __ cmp(scratch, kSymbolTag | kStringTag);
3936 __ j(not_equal, label);
3937}
3938
3939
3940void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003941 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003942}
3943
3944
3945void CallFunctionStub::Generate(MacroAssembler* masm) {
3946 Label slow;
3947
danno@chromium.org40cb8782011-05-25 07:58:50 +00003948 // The receiver might implicitly be the global object. This is
3949 // indicated by passing the hole as the receiver to the call
3950 // function stub.
3951 if (ReceiverMightBeImplicit()) {
3952 Label call;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003953 // Get the receiver from the stack.
3954 // +1 ~ return address
ricow@chromium.org65fae842010-08-25 15:26:24 +00003955 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003956 // Call as function is indicated with the hole.
3957 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
3958 __ j(not_equal, &call, Label::kNear);
3959 // Patch the receiver on the stack with the global receiver object.
3960 __ mov(ebx, GlobalObjectOperand());
3961 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
3962 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), ebx);
3963 __ bind(&call);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003964 }
3965
3966 // Get the function to call from the stack.
3967 // +2 ~ receiver, return address
3968 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
3969
3970 // Check that the function really is a JavaScript function.
3971 __ test(edi, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003972 __ j(zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003973 // Goto slow case if we do not have a function.
3974 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003975 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003976
3977 // Fast-case: Just invoke the function.
3978 ParameterCount actual(argc_);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003979
3980 if (ReceiverMightBeImplicit()) {
3981 Label call_as_function;
3982 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
3983 __ j(equal, &call_as_function);
3984 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
3985 __ bind(&call_as_function);
3986 }
3987 __ InvokeFunction(edi,
3988 actual,
3989 JUMP_FUNCTION,
3990 NullCallWrapper(),
3991 CALL_AS_FUNCTION);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003992
3993 // Slow-case: Non-function called.
3994 __ bind(&slow);
3995 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3996 // of the original receiver from the call site).
3997 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
3998 __ Set(eax, Immediate(argc_));
3999 __ Set(ebx, Immediate(0));
4000 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004001 Handle<Code> adaptor =
4002 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004003 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4004}
4005
4006
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004007bool CEntryStub::NeedsImmovableCode() {
4008 return false;
4009}
4010
4011
ricow@chromium.org65fae842010-08-25 15:26:24 +00004012void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004013 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004014}
4015
4016
ricow@chromium.org65fae842010-08-25 15:26:24 +00004017void CEntryStub::GenerateCore(MacroAssembler* masm,
4018 Label* throw_normal_exception,
4019 Label* throw_termination_exception,
4020 Label* throw_out_of_memory_exception,
4021 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004022 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004023 // eax: result parameter for PerformGC, if any
4024 // ebx: pointer to C function (C callee-saved)
4025 // ebp: frame pointer (restored after C call)
4026 // esp: stack pointer (restored after C call)
4027 // edi: number of arguments including receiver (C callee-saved)
4028 // esi: pointer to the first argument (C callee-saved)
4029
4030 // Result returned in eax, or eax+edx if result_size_ is 2.
4031
4032 // Check stack alignment.
4033 if (FLAG_debug_code) {
4034 __ CheckStackAlignment();
4035 }
4036
4037 if (do_gc) {
4038 // Pass failure code returned from last attempt as first argument to
4039 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4040 // stack alignment is known to be correct. This function takes one argument
4041 // which is passed on the stack, and we know that the stack has been
4042 // prepared to pass at least one argument.
4043 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4044 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4045 }
4046
4047 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004048 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004049 if (always_allocate_scope) {
4050 __ inc(Operand::StaticVariable(scope_depth));
4051 }
4052
4053 // Call C function.
4054 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4055 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004056 __ mov(Operand(esp, 2 * kPointerSize),
4057 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004058 __ call(Operand(ebx));
4059 // Result is in eax or edx:eax - do not destroy these registers!
4060
4061 if (always_allocate_scope) {
4062 __ dec(Operand::StaticVariable(scope_depth));
4063 }
4064
4065 // Make sure we're not trying to return 'the hole' from the runtime
4066 // call as this may lead to crashes in the IC code later.
4067 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004068 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004069 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004070 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004071 __ int3();
4072 __ bind(&okay);
4073 }
4074
4075 // Check for failure result.
4076 Label failure_returned;
4077 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4078 __ lea(ecx, Operand(eax, 1));
4079 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4080 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004081 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004082
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004083 ExternalReference pending_exception_address(
4084 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004085
4086 // Check that there is no pending exception, otherwise we
4087 // should have returned some failure value.
4088 if (FLAG_debug_code) {
4089 __ push(edx);
4090 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004091 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004092 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004093 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4094 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004095 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004096 __ int3();
4097 __ bind(&okay);
4098 __ pop(edx);
4099 }
4100
ricow@chromium.org65fae842010-08-25 15:26:24 +00004101 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004102 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004103 __ ret(0);
4104
4105 // Handling of failure.
4106 __ bind(&failure_returned);
4107
4108 Label retry;
4109 // If the returned exception is RETRY_AFTER_GC continue at retry label
4110 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4111 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004112 __ j(zero, &retry);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004113
4114 // Special handling of out of memory exceptions.
4115 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4116 __ j(equal, throw_out_of_memory_exception);
4117
4118 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004119 ExternalReference the_hole_location =
4120 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004121 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004122 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004123 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4124
4125 // Special handling of termination exceptions which are uncatchable
4126 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004127 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004128 __ j(equal, throw_termination_exception);
4129
4130 // Handle normal exception.
4131 __ jmp(throw_normal_exception);
4132
4133 // Retry.
4134 __ bind(&retry);
4135}
4136
4137
4138void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4139 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004140 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004141}
4142
4143
4144void CEntryStub::Generate(MacroAssembler* masm) {
4145 // eax: number of arguments including receiver
4146 // ebx: pointer to C function (C callee-saved)
4147 // ebp: frame pointer (restored after C call)
4148 // esp: stack pointer (restored after C call)
4149 // esi: current context (C callee-saved)
4150 // edi: JS function of the caller (C callee-saved)
4151
4152 // NOTE: Invocations of builtins may return failure objects instead
4153 // of a proper result. The builtin entry handles this by performing
4154 // a garbage collection and retrying the builtin (twice).
4155
4156 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004157 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004158
4159 // eax: result parameter for PerformGC, if any (setup below)
4160 // ebx: pointer to builtin function (C callee-saved)
4161 // ebp: frame pointer (restored after C call)
4162 // esp: stack pointer (restored after C call)
4163 // edi: number of arguments including receiver (C callee-saved)
4164 // esi: argv pointer (C callee-saved)
4165
4166 Label throw_normal_exception;
4167 Label throw_termination_exception;
4168 Label throw_out_of_memory_exception;
4169
4170 // Call into the runtime system.
4171 GenerateCore(masm,
4172 &throw_normal_exception,
4173 &throw_termination_exception,
4174 &throw_out_of_memory_exception,
4175 false,
4176 false);
4177
4178 // Do space-specific GC and retry runtime call.
4179 GenerateCore(masm,
4180 &throw_normal_exception,
4181 &throw_termination_exception,
4182 &throw_out_of_memory_exception,
4183 true,
4184 false);
4185
4186 // Do full GC and retry runtime call one final time.
4187 Failure* failure = Failure::InternalError();
4188 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4189 GenerateCore(masm,
4190 &throw_normal_exception,
4191 &throw_termination_exception,
4192 &throw_out_of_memory_exception,
4193 true,
4194 true);
4195
4196 __ bind(&throw_out_of_memory_exception);
4197 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4198
4199 __ bind(&throw_termination_exception);
4200 GenerateThrowUncatchable(masm, TERMINATION);
4201
4202 __ bind(&throw_normal_exception);
4203 GenerateThrowTOS(masm);
4204}
4205
4206
4207void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4208 Label invoke, exit;
4209#ifdef ENABLE_LOGGING_AND_PROFILING
4210 Label not_outermost_js, not_outermost_js_2;
4211#endif
4212
4213 // Setup frame.
4214 __ push(ebp);
4215 __ mov(ebp, Operand(esp));
4216
4217 // Push marker in two places.
4218 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4219 __ push(Immediate(Smi::FromInt(marker))); // context slot
4220 __ push(Immediate(Smi::FromInt(marker))); // function slot
4221 // Save callee-saved registers (C calling conventions).
4222 __ push(edi);
4223 __ push(esi);
4224 __ push(ebx);
4225
4226 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004227 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004228 __ push(Operand::StaticVariable(c_entry_fp));
4229
4230#ifdef ENABLE_LOGGING_AND_PROFILING
4231 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004232 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4233 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004234 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4235 __ j(not_equal, &not_outermost_js);
4236 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004237 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4238 Label cont;
4239 __ jmp(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004240 __ bind(&not_outermost_js);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004241 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
4242 __ bind(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004243#endif
4244
4245 // Call a faked try-block that does the invoke.
4246 __ call(&invoke);
4247
4248 // Caught exception: Store result (exception) in the pending
4249 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004250 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4251 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004252 __ mov(Operand::StaticVariable(pending_exception), eax);
4253 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4254 __ jmp(&exit);
4255
4256 // Invoke: Link this frame into the handler chain.
4257 __ bind(&invoke);
4258 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4259
4260 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004261 ExternalReference the_hole_location =
4262 ExternalReference::the_hole_value_location(masm->isolate());
4263 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004264 __ mov(Operand::StaticVariable(pending_exception), edx);
4265
4266 // Fake a receiver (NULL).
4267 __ push(Immediate(0)); // receiver
4268
4269 // Invoke the function by calling through JS entry trampoline
4270 // builtin and pop the faked function when we return. Notice that we
4271 // cannot store a reference to the trampoline code directly in this
4272 // stub, because the builtin stubs may not have been generated yet.
4273 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004274 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004275 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004276 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004277 __ mov(edx, Immediate(construct_entry));
4278 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004279 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004280 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004281 __ mov(edx, Immediate(entry));
4282 }
4283 __ mov(edx, Operand(edx, 0)); // deref address
4284 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4285 __ call(Operand(edx));
4286
4287 // Unlink this frame from the handler chain.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004288 __ PopTryHandler();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004289
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004290 __ bind(&exit);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004291#ifdef ENABLE_LOGGING_AND_PROFILING
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004292 // Check if the current stack frame is marked as the outermost JS frame.
4293 __ pop(ebx);
4294 __ cmp(Operand(ebx),
4295 Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004296 __ j(not_equal, &not_outermost_js_2);
4297 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4298 __ bind(&not_outermost_js_2);
4299#endif
4300
4301 // Restore the top frame descriptor from the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004302 __ pop(Operand::StaticVariable(ExternalReference(
4303 Isolate::k_c_entry_fp_address,
4304 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004305
4306 // Restore callee-saved registers (C calling conventions).
4307 __ pop(ebx);
4308 __ pop(esi);
4309 __ pop(edi);
4310 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4311
4312 // Restore frame pointer and return.
4313 __ pop(ebp);
4314 __ ret(0);
4315}
4316
4317
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004318// Generate stub code for instanceof.
4319// This code can patch a call site inlined cache of the instance of check,
4320// which looks like this.
4321//
4322// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4323// 75 0a jne <some near label>
4324// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4325//
4326// If call site patching is requested the stack will have the delta from the
4327// return address to the cmp instruction just below the return address. This
4328// also means that call site patching can only take place with arguments in
4329// registers. TOS looks like this when call site patching is requested
4330//
4331// esp[0] : return address
4332// esp[4] : delta from return address to cmp instruction
4333//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004334void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004335 // Call site inlining and patching implies arguments in registers.
4336 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4337
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004338 // Fixed register usage throughout the stub.
4339 Register object = eax; // Object (lhs).
4340 Register map = ebx; // Map of the object.
4341 Register function = edx; // Function (rhs).
4342 Register prototype = edi; // Prototype of the function.
4343 Register scratch = ecx;
4344
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004345 // Constants describing the call site code to patch.
4346 static const int kDeltaToCmpImmediate = 2;
4347 static const int kDeltaToMov = 8;
4348 static const int kDeltaToMovImmediate = 9;
4349 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4350 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4351 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4352
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004353 ExternalReference roots_address =
4354 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004355
4356 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4357 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4358
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004359 // Get the object and function - they are always both needed.
4360 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004361 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004362 __ mov(object, Operand(esp, 2 * kPointerSize));
4363 __ mov(function, Operand(esp, 1 * kPointerSize));
4364 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004365
4366 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004367 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004368 __ j(zero, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004369 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004370
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004371 // If there is a call site cache don't look in the global cache, but do the
4372 // real lookup and update the call site cache.
4373 if (!HasCallSiteInlineCheck()) {
4374 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004375 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004376 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4377 __ cmp(function,
4378 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004379 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004380 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4381 __ cmp(map, Operand::StaticArray(
4382 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004383 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004384 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4385 __ mov(eax, Operand::StaticArray(
4386 scratch, times_pointer_size, roots_address));
4387 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4388 __ bind(&miss);
4389 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004390
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004391 // Get the prototype of the function.
4392 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004393
4394 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004395 __ test(prototype, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004396 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004397 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004398
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004399 // Update the global instanceof or call site inlined cache with the current
4400 // map and function. The cached answer will be set when it is known below.
4401 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004402 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4403 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4404 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4405 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4406 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004407 } else {
4408 // The constants for the code patching are based on no push instructions
4409 // at the call site.
4410 ASSERT(HasArgsInRegisters());
4411 // Get return address and delta to inlined map check.
4412 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4413 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4414 if (FLAG_debug_code) {
4415 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4416 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4417 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4418 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4419 }
4420 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4421 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004422
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004423 // Loop through the prototype chain of the object looking for the function
4424 // prototype.
4425 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004426 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004427 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004428 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004429 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004430 Factory* factory = masm->isolate()->factory();
4431 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004432 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004433 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4434 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004435 __ jmp(&loop);
4436
4437 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004438 if (!HasCallSiteInlineCheck()) {
4439 __ Set(eax, Immediate(0));
4440 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4441 __ mov(Operand::StaticArray(scratch,
4442 times_pointer_size, roots_address), eax);
4443 } else {
4444 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004445 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004446 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4447 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4448 if (FLAG_debug_code) {
4449 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4450 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4451 }
4452 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4453 if (!ReturnTrueFalseObject()) {
4454 __ Set(eax, Immediate(0));
4455 }
4456 }
4457 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004458
4459 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004460 if (!HasCallSiteInlineCheck()) {
4461 __ Set(eax, Immediate(Smi::FromInt(1)));
4462 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4463 __ mov(Operand::StaticArray(
4464 scratch, times_pointer_size, roots_address), eax);
4465 } else {
4466 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004467 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004468 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4469 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4470 if (FLAG_debug_code) {
4471 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4472 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4473 }
4474 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4475 if (!ReturnTrueFalseObject()) {
4476 __ Set(eax, Immediate(Smi::FromInt(1)));
4477 }
4478 }
4479 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004480
4481 Label object_not_null, object_not_null_or_smi;
4482 __ bind(&not_js_object);
4483 // Before null, smi and string value checks, check that the rhs is a function
4484 // as for a non-function rhs an exception needs to be thrown.
4485 __ test(function, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004486 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004487 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004488 __ j(not_equal, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004489
4490 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004491 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004492 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004493 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004494 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004495
4496 __ bind(&object_not_null);
4497 // Smi values is not instance of anything.
4498 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004499 __ j(not_zero, &object_not_null_or_smi);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004500 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004501 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004502
4503 __ bind(&object_not_null_or_smi);
4504 // String values is not instance of anything.
4505 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4506 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004507 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004508 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004509
4510 // Slow-case: Go through the JavaScript implementation.
4511 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004512 if (!ReturnTrueFalseObject()) {
4513 // Tail call the builtin which returns 0 or 1.
4514 if (HasArgsInRegisters()) {
4515 // Push arguments below return address.
4516 __ pop(scratch);
4517 __ push(object);
4518 __ push(function);
4519 __ push(scratch);
4520 }
4521 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4522 } else {
4523 // Call the builtin and convert 0/1 to true/false.
4524 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004525 __ push(object);
4526 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004527 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4528 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004529 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004530 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004531 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004532 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004533 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004534 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004535 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004536 __ bind(&done);
4537 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004538 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004539}
4540
4541
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004542Register InstanceofStub::left() { return eax; }
4543
4544
4545Register InstanceofStub::right() { return edx; }
4546
4547
ricow@chromium.org65fae842010-08-25 15:26:24 +00004548int CompareStub::MinorKey() {
4549 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4550 // stubs the never NaN NaN condition is only taken into account if the
4551 // condition is equals.
4552 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4553 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4554 return ConditionField::encode(static_cast<unsigned>(cc_))
4555 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4556 | StrictField::encode(strict_)
4557 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004558 | IncludeNumberCompareField::encode(include_number_compare_)
4559 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004560}
4561
4562
4563// Unfortunately you have to run without snapshots to see most of these
4564// names in the profile since most compare stubs end up in the snapshot.
4565const char* CompareStub::GetName() {
4566 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4567
4568 if (name_ != NULL) return name_;
4569 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004570 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4571 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004572 if (name_ == NULL) return "OOM";
4573
4574 const char* cc_name;
4575 switch (cc_) {
4576 case less: cc_name = "LT"; break;
4577 case greater: cc_name = "GT"; break;
4578 case less_equal: cc_name = "LE"; break;
4579 case greater_equal: cc_name = "GE"; break;
4580 case equal: cc_name = "EQ"; break;
4581 case not_equal: cc_name = "NE"; break;
4582 default: cc_name = "UnknownCondition"; break;
4583 }
4584
4585 const char* strict_name = "";
4586 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4587 strict_name = "_STRICT";
4588 }
4589
4590 const char* never_nan_nan_name = "";
4591 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4592 never_nan_nan_name = "_NO_NAN";
4593 }
4594
4595 const char* include_number_compare_name = "";
4596 if (!include_number_compare_) {
4597 include_number_compare_name = "_NO_NUMBER";
4598 }
4599
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004600 const char* include_smi_compare_name = "";
4601 if (!include_smi_compare_) {
4602 include_smi_compare_name = "_NO_SMI";
4603 }
4604
ricow@chromium.org65fae842010-08-25 15:26:24 +00004605 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004606 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004607 cc_name,
4608 strict_name,
4609 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004610 include_number_compare_name,
4611 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004612 return name_;
4613}
4614
4615
4616// -------------------------------------------------------------------------
4617// StringCharCodeAtGenerator
4618
4619void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4620 Label flat_string;
4621 Label ascii_string;
4622 Label got_char_code;
4623
4624 // If the receiver is a smi trigger the non-string case.
4625 STATIC_ASSERT(kSmiTag == 0);
4626 __ test(object_, Immediate(kSmiTagMask));
4627 __ j(zero, receiver_not_string_);
4628
4629 // Fetch the instance type of the receiver into result register.
4630 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4631 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4632 // If the receiver is not a string trigger the non-string case.
4633 __ test(result_, Immediate(kIsNotStringMask));
4634 __ j(not_zero, receiver_not_string_);
4635
4636 // If the index is non-smi trigger the non-smi case.
4637 STATIC_ASSERT(kSmiTag == 0);
4638 __ test(index_, Immediate(kSmiTagMask));
4639 __ j(not_zero, &index_not_smi_);
4640
4641 // Put smi-tagged index into scratch register.
4642 __ mov(scratch_, index_);
4643 __ bind(&got_smi_index_);
4644
4645 // Check for index out of range.
4646 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4647 __ j(above_equal, index_out_of_range_);
4648
4649 // We need special handling for non-flat strings.
4650 STATIC_ASSERT(kSeqStringTag == 0);
4651 __ test(result_, Immediate(kStringRepresentationMask));
4652 __ j(zero, &flat_string);
4653
4654 // Handle non-flat strings.
4655 __ test(result_, Immediate(kIsConsStringMask));
4656 __ j(zero, &call_runtime_);
4657
4658 // ConsString.
4659 // Check whether the right hand side is the empty string (i.e. if
4660 // this is really a flat string in a cons string). If that is not
4661 // the case we would rather go to the runtime system now to flatten
4662 // the string.
4663 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004664 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004665 __ j(not_equal, &call_runtime_);
4666 // Get the first of the two strings and load its instance type.
4667 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4668 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4669 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4670 // If the first cons component is also non-flat, then go to runtime.
4671 STATIC_ASSERT(kSeqStringTag == 0);
4672 __ test(result_, Immediate(kStringRepresentationMask));
4673 __ j(not_zero, &call_runtime_);
4674
4675 // Check for 1-byte or 2-byte string.
4676 __ bind(&flat_string);
4677 STATIC_ASSERT(kAsciiStringTag != 0);
4678 __ test(result_, Immediate(kStringEncodingMask));
4679 __ j(not_zero, &ascii_string);
4680
4681 // 2-byte string.
4682 // Load the 2-byte character code into the result register.
4683 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4684 __ movzx_w(result_, FieldOperand(object_,
4685 scratch_, times_1, // Scratch is smi-tagged.
4686 SeqTwoByteString::kHeaderSize));
4687 __ jmp(&got_char_code);
4688
4689 // ASCII string.
4690 // Load the byte into the result register.
4691 __ bind(&ascii_string);
4692 __ SmiUntag(scratch_);
4693 __ movzx_b(result_, FieldOperand(object_,
4694 scratch_, times_1,
4695 SeqAsciiString::kHeaderSize));
4696 __ bind(&got_char_code);
4697 __ SmiTag(result_);
4698 __ bind(&exit_);
4699}
4700
4701
4702void StringCharCodeAtGenerator::GenerateSlow(
4703 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4704 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4705
4706 // Index is not a smi.
4707 __ bind(&index_not_smi_);
4708 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004709 __ CheckMap(index_,
4710 masm->isolate()->factory()->heap_number_map(),
4711 index_not_number_,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004712 DONT_DO_SMI_CHECK);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004713 call_helper.BeforeCall(masm);
4714 __ push(object_);
4715 __ push(index_);
4716 __ push(index_); // Consumed by runtime conversion function.
4717 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4718 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4719 } else {
4720 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4721 // NumberToSmi discards numbers that are not exact integers.
4722 __ CallRuntime(Runtime::kNumberToSmi, 1);
4723 }
4724 if (!scratch_.is(eax)) {
4725 // Save the conversion result before the pop instructions below
4726 // have a chance to overwrite it.
4727 __ mov(scratch_, eax);
4728 }
4729 __ pop(index_);
4730 __ pop(object_);
4731 // Reload the instance type.
4732 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4733 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4734 call_helper.AfterCall(masm);
4735 // If index is still not a smi, it must be out of range.
4736 STATIC_ASSERT(kSmiTag == 0);
4737 __ test(scratch_, Immediate(kSmiTagMask));
4738 __ j(not_zero, index_out_of_range_);
4739 // Otherwise, return to the fast path.
4740 __ jmp(&got_smi_index_);
4741
4742 // Call runtime. We get here when the receiver is a string and the
4743 // index is a number, but the code of getting the actual character
4744 // is too complex (e.g., when the string needs to be flattened).
4745 __ bind(&call_runtime_);
4746 call_helper.BeforeCall(masm);
4747 __ push(object_);
4748 __ push(index_);
4749 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4750 if (!result_.is(eax)) {
4751 __ mov(result_, eax);
4752 }
4753 call_helper.AfterCall(masm);
4754 __ jmp(&exit_);
4755
4756 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4757}
4758
4759
4760// -------------------------------------------------------------------------
4761// StringCharFromCodeGenerator
4762
4763void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4764 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4765 STATIC_ASSERT(kSmiTag == 0);
4766 STATIC_ASSERT(kSmiShiftSize == 0);
4767 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4768 __ test(code_,
4769 Immediate(kSmiTagMask |
4770 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004771 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004772
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004773 Factory* factory = masm->isolate()->factory();
4774 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004775 STATIC_ASSERT(kSmiTag == 0);
4776 STATIC_ASSERT(kSmiTagSize == 1);
4777 STATIC_ASSERT(kSmiShiftSize == 0);
4778 // At this point code register contains smi tagged ascii char code.
4779 __ mov(result_, FieldOperand(result_,
4780 code_, times_half_pointer_size,
4781 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004782 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004783 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004784 __ bind(&exit_);
4785}
4786
4787
4788void StringCharFromCodeGenerator::GenerateSlow(
4789 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4790 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4791
4792 __ bind(&slow_case_);
4793 call_helper.BeforeCall(masm);
4794 __ push(code_);
4795 __ CallRuntime(Runtime::kCharFromCode, 1);
4796 if (!result_.is(eax)) {
4797 __ mov(result_, eax);
4798 }
4799 call_helper.AfterCall(masm);
4800 __ jmp(&exit_);
4801
4802 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4803}
4804
4805
4806// -------------------------------------------------------------------------
4807// StringCharAtGenerator
4808
4809void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4810 char_code_at_generator_.GenerateFast(masm);
4811 char_from_code_generator_.GenerateFast(masm);
4812}
4813
4814
4815void StringCharAtGenerator::GenerateSlow(
4816 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4817 char_code_at_generator_.GenerateSlow(masm, call_helper);
4818 char_from_code_generator_.GenerateSlow(masm, call_helper);
4819}
4820
4821
4822void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004823 Label string_add_runtime, call_builtin;
4824 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004825
4826 // Load the two arguments.
4827 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4828 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4829
4830 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004831 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004832 __ test(eax, Immediate(kSmiTagMask));
4833 __ j(zero, &string_add_runtime);
4834 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
4835 __ j(above_equal, &string_add_runtime);
4836
4837 // First argument is a a string, test second.
4838 __ test(edx, Immediate(kSmiTagMask));
4839 __ j(zero, &string_add_runtime);
4840 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
4841 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004842 } else {
4843 // Here at least one of the arguments is definitely a string.
4844 // We convert the one that is not known to be a string.
4845 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4846 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4847 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
4848 &call_builtin);
4849 builtin_id = Builtins::STRING_ADD_RIGHT;
4850 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4851 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4852 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
4853 &call_builtin);
4854 builtin_id = Builtins::STRING_ADD_LEFT;
4855 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004856 }
4857
4858 // Both arguments are strings.
4859 // eax: first string
4860 // edx: second string
4861 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004862 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004863 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
4864 STATIC_ASSERT(kSmiTag == 0);
4865 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004866 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004867 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004868 Counters* counters = masm->isolate()->counters();
4869 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004870 __ ret(2 * kPointerSize);
4871 __ bind(&second_not_zero_length);
4872 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
4873 STATIC_ASSERT(kSmiTag == 0);
4874 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004875 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004876 // First string is empty, result is second string which is in edx.
4877 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004878 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004879 __ ret(2 * kPointerSize);
4880
4881 // Both strings are non-empty.
4882 // eax: first string
4883 // ebx: length of first string as a smi
4884 // ecx: length of second string as a smi
4885 // edx: second string
4886 // Look at the length of the result of adding the two strings.
4887 Label string_add_flat_result, longer_than_two;
4888 __ bind(&both_not_zero_length);
4889 __ add(ebx, Operand(ecx));
4890 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
4891 // Handle exceptionally long strings in the runtime system.
4892 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004893 // Use the symbol table when adding two one character strings, as it
4894 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004895 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
4896 __ j(not_equal, &longer_than_two);
4897
4898 // Check that both strings are non-external ascii strings.
4899 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
4900 &string_add_runtime);
4901
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004902 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004903 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4904 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4905
4906 // Try to lookup two character string in symbol table. If it is not found
4907 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004908 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004909 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004910 masm, ebx, ecx, eax, edx, edi,
4911 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004912 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004913 __ ret(2 * kPointerSize);
4914
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004915 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004916 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004917 // Reload the arguments.
4918 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4919 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4920 // Get the two characters forming the new string.
4921 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4922 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4923 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004924 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004925 __ AllocateAsciiString(eax, // Result.
4926 2, // Length.
4927 edi, // Scratch 1.
4928 edx, // Scratch 2.
4929 &string_add_runtime);
4930 // Pack both characters in ebx.
4931 __ shl(ecx, kBitsPerByte);
4932 __ or_(ebx, Operand(ecx));
4933 // Set the characters in the new string.
4934 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004935 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004936 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004937
4938 __ bind(&longer_than_two);
4939 // Check if resulting string will be flat.
4940 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
4941 __ j(below, &string_add_flat_result);
4942
4943 // If result is not supposed to be flat allocate a cons string object. If both
4944 // strings are ascii the result is an ascii cons string.
4945 Label non_ascii, allocated, ascii_data;
4946 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
4947 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
4948 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
4949 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
4950 __ and_(ecx, Operand(edi));
4951 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4952 __ test(ecx, Immediate(kAsciiStringTag));
4953 __ j(zero, &non_ascii);
4954 __ bind(&ascii_data);
4955 // Allocate an acsii cons string.
4956 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
4957 __ bind(&allocated);
4958 // Fill the fields of the cons string.
4959 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
4960 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
4961 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
4962 Immediate(String::kEmptyHashField));
4963 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
4964 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
4965 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004966 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004967 __ ret(2 * kPointerSize);
4968 __ bind(&non_ascii);
4969 // At least one of the strings is two-byte. Check whether it happens
4970 // to contain only ascii characters.
4971 // ecx: first instance type AND second instance type.
4972 // edi: second instance type.
4973 __ test(ecx, Immediate(kAsciiDataHintMask));
4974 __ j(not_zero, &ascii_data);
4975 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4976 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4977 __ xor_(edi, Operand(ecx));
4978 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4979 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
4980 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
4981 __ j(equal, &ascii_data);
4982 // Allocate a two byte cons string.
4983 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
4984 __ jmp(&allocated);
4985
4986 // Handle creating a flat result. First check that both strings are not
4987 // external strings.
4988 // eax: first string
4989 // ebx: length of resulting flat string as a smi
4990 // edx: second string
4991 __ bind(&string_add_flat_result);
4992 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4993 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4994 __ and_(ecx, kStringRepresentationMask);
4995 __ cmp(ecx, kExternalStringTag);
4996 __ j(equal, &string_add_runtime);
4997 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4998 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4999 __ and_(ecx, kStringRepresentationMask);
5000 __ cmp(ecx, kExternalStringTag);
5001 __ j(equal, &string_add_runtime);
5002 // Now check if both strings are ascii strings.
5003 // eax: first string
5004 // ebx: length of resulting flat string as a smi
5005 // edx: second string
5006 Label non_ascii_string_add_flat_result;
5007 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5008 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5009 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5010 __ j(zero, &non_ascii_string_add_flat_result);
5011 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5012 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5013 __ j(zero, &string_add_runtime);
5014
ricow@chromium.org65fae842010-08-25 15:26:24 +00005015 // Both strings are ascii strings. As they are short they are both flat.
5016 // ebx: length of resulting flat string as a smi
5017 __ SmiUntag(ebx);
5018 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5019 // eax: result string
5020 __ mov(ecx, eax);
5021 // Locate first character of result.
5022 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5023 // Load first argument and locate first character.
5024 __ mov(edx, Operand(esp, 2 * kPointerSize));
5025 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5026 __ SmiUntag(edi);
5027 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5028 // eax: result string
5029 // ecx: first character of result
5030 // edx: first char of first argument
5031 // edi: length of first argument
5032 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5033 // Load second argument and locate first character.
5034 __ mov(edx, Operand(esp, 1 * kPointerSize));
5035 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5036 __ SmiUntag(edi);
5037 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5038 // eax: result string
5039 // ecx: next character of result
5040 // edx: first char of second argument
5041 // edi: length of second argument
5042 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
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 // Handle creating a flat two byte result.
5047 // eax: first string - known to be two byte
5048 // ebx: length of resulting flat string as a smi
5049 // edx: second string
5050 __ bind(&non_ascii_string_add_flat_result);
5051 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5052 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5053 __ j(not_zero, &string_add_runtime);
5054 // Both strings are two byte strings. As they are short they are both
5055 // flat.
5056 __ SmiUntag(ebx);
5057 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5058 // eax: result string
5059 __ mov(ecx, eax);
5060 // Locate first character of result.
5061 __ add(Operand(ecx),
5062 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5063 // Load first argument and locate first character.
5064 __ mov(edx, Operand(esp, 2 * kPointerSize));
5065 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5066 __ SmiUntag(edi);
5067 __ add(Operand(edx),
5068 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5069 // eax: result string
5070 // ecx: first character of result
5071 // edx: first char of first argument
5072 // edi: length of first argument
5073 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5074 // Load second argument and locate first character.
5075 __ mov(edx, Operand(esp, 1 * kPointerSize));
5076 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5077 __ SmiUntag(edi);
5078 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5079 // eax: result string
5080 // ecx: next character of result
5081 // edx: first char of second argument
5082 // edi: length of second argument
5083 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005084 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005085 __ ret(2 * kPointerSize);
5086
5087 // Just jump to runtime to add the two strings.
5088 __ bind(&string_add_runtime);
5089 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005090
5091 if (call_builtin.is_linked()) {
5092 __ bind(&call_builtin);
5093 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5094 }
5095}
5096
5097
5098void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5099 int stack_offset,
5100 Register arg,
5101 Register scratch1,
5102 Register scratch2,
5103 Register scratch3,
5104 Label* slow) {
5105 // First check if the argument is already a string.
5106 Label not_string, done;
5107 __ test(arg, Immediate(kSmiTagMask));
5108 __ j(zero, &not_string);
5109 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5110 __ j(below, &done);
5111
5112 // Check the number to string cache.
5113 Label not_cached;
5114 __ bind(&not_string);
5115 // Puts the cached result into scratch1.
5116 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5117 arg,
5118 scratch1,
5119 scratch2,
5120 scratch3,
5121 false,
5122 &not_cached);
5123 __ mov(arg, scratch1);
5124 __ mov(Operand(esp, stack_offset), arg);
5125 __ jmp(&done);
5126
5127 // Check if the argument is a safe string wrapper.
5128 __ bind(&not_cached);
5129 __ test(arg, Immediate(kSmiTagMask));
5130 __ j(zero, slow);
5131 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5132 __ j(not_equal, slow);
5133 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5134 1 << Map::kStringWrapperSafeForDefaultValueOf);
5135 __ j(zero, slow);
5136 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5137 __ mov(Operand(esp, stack_offset), arg);
5138
5139 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005140}
5141
5142
5143void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5144 Register dest,
5145 Register src,
5146 Register count,
5147 Register scratch,
5148 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005149 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005150 __ bind(&loop);
5151 // This loop just copies one character at a time, as it is only used for very
5152 // short strings.
5153 if (ascii) {
5154 __ mov_b(scratch, Operand(src, 0));
5155 __ mov_b(Operand(dest, 0), scratch);
5156 __ add(Operand(src), Immediate(1));
5157 __ add(Operand(dest), Immediate(1));
5158 } else {
5159 __ mov_w(scratch, Operand(src, 0));
5160 __ mov_w(Operand(dest, 0), scratch);
5161 __ add(Operand(src), Immediate(2));
5162 __ add(Operand(dest), Immediate(2));
5163 }
5164 __ sub(Operand(count), Immediate(1));
5165 __ j(not_zero, &loop);
5166}
5167
5168
5169void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5170 Register dest,
5171 Register src,
5172 Register count,
5173 Register scratch,
5174 bool ascii) {
5175 // Copy characters using rep movs of doublewords.
5176 // The destination is aligned on a 4 byte boundary because we are
5177 // copying to the beginning of a newly allocated string.
5178 ASSERT(dest.is(edi)); // rep movs destination
5179 ASSERT(src.is(esi)); // rep movs source
5180 ASSERT(count.is(ecx)); // rep movs count
5181 ASSERT(!scratch.is(dest));
5182 ASSERT(!scratch.is(src));
5183 ASSERT(!scratch.is(count));
5184
5185 // Nothing to do for zero characters.
5186 Label done;
5187 __ test(count, Operand(count));
5188 __ j(zero, &done);
5189
5190 // Make count the number of bytes to copy.
5191 if (!ascii) {
5192 __ shl(count, 1);
5193 }
5194
5195 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005196 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005197 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005198 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005199
5200 // Copy from edi to esi using rep movs instruction.
5201 __ mov(scratch, count);
5202 __ sar(count, 2); // Number of doublewords to copy.
5203 __ cld();
5204 __ rep_movs();
5205
5206 // Find number of bytes left.
5207 __ mov(count, scratch);
5208 __ and_(count, 3);
5209
5210 // Check if there are more bytes to copy.
5211 __ bind(&last_bytes);
5212 __ test(count, Operand(count));
5213 __ j(zero, &done);
5214
5215 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005216 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005217 __ bind(&loop);
5218 __ mov_b(scratch, Operand(src, 0));
5219 __ mov_b(Operand(dest, 0), scratch);
5220 __ add(Operand(src), Immediate(1));
5221 __ add(Operand(dest), Immediate(1));
5222 __ sub(Operand(count), Immediate(1));
5223 __ j(not_zero, &loop);
5224
5225 __ bind(&done);
5226}
5227
5228
5229void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5230 Register c1,
5231 Register c2,
5232 Register scratch1,
5233 Register scratch2,
5234 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005235 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005236 Label* not_found) {
5237 // Register scratch3 is the general scratch register in this function.
5238 Register scratch = scratch3;
5239
5240 // Make sure that both characters are not digits as such strings has a
5241 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005242 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005243 __ mov(scratch, c1);
5244 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5245 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005246 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005247 __ mov(scratch, c2);
5248 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5249 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005250 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005251
5252 __ bind(&not_array_index);
5253 // Calculate the two character string hash.
5254 Register hash = scratch1;
5255 GenerateHashInit(masm, hash, c1, scratch);
5256 GenerateHashAddCharacter(masm, hash, c2, scratch);
5257 GenerateHashGetHash(masm, hash, scratch);
5258
5259 // Collect the two characters in a register.
5260 Register chars = c1;
5261 __ shl(c2, kBitsPerByte);
5262 __ or_(chars, Operand(c2));
5263
5264 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5265 // hash: hash of two character string.
5266
5267 // Load the symbol table.
5268 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005269 ExternalReference roots_address =
5270 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005271 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5272 __ mov(symbol_table,
5273 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5274
5275 // Calculate capacity mask from the symbol table capacity.
5276 Register mask = scratch2;
5277 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5278 __ SmiUntag(mask);
5279 __ sub(Operand(mask), Immediate(1));
5280
5281 // Registers
5282 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5283 // hash: hash of two character string
5284 // symbol_table: symbol table
5285 // mask: capacity mask
5286 // scratch: -
5287
5288 // Perform a number of probes in the symbol table.
5289 static const int kProbes = 4;
5290 Label found_in_symbol_table;
5291 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5292 for (int i = 0; i < kProbes; i++) {
5293 // Calculate entry in symbol table.
5294 __ mov(scratch, hash);
5295 if (i > 0) {
5296 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5297 }
5298 __ and_(scratch, Operand(mask));
5299
5300 // Load the entry from the symbol table.
5301 Register candidate = scratch; // Scratch register contains candidate.
5302 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5303 __ mov(candidate,
5304 FieldOperand(symbol_table,
5305 scratch,
5306 times_pointer_size,
5307 SymbolTable::kElementsStartOffset));
5308
5309 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005310 Factory* factory = masm->isolate()->factory();
5311 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005312 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005313 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005314 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005315
5316 // If length is not 2 the string is not a candidate.
5317 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5318 Immediate(Smi::FromInt(2)));
5319 __ j(not_equal, &next_probe[i]);
5320
5321 // As we are out of registers save the mask on the stack and use that
5322 // register as a temporary.
5323 __ push(mask);
5324 Register temp = mask;
5325
5326 // Check that the candidate is a non-external ascii string.
5327 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5328 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5329 __ JumpIfInstanceTypeIsNotSequentialAscii(
5330 temp, temp, &next_probe_pop_mask[i]);
5331
5332 // Check if the two characters match.
5333 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5334 __ and_(temp, 0x0000ffff);
5335 __ cmp(chars, Operand(temp));
5336 __ j(equal, &found_in_symbol_table);
5337 __ bind(&next_probe_pop_mask[i]);
5338 __ pop(mask);
5339 __ bind(&next_probe[i]);
5340 }
5341
5342 // No matching 2 character string found by probing.
5343 __ jmp(not_found);
5344
5345 // Scratch register contains result when we fall through to here.
5346 Register result = scratch;
5347 __ bind(&found_in_symbol_table);
5348 __ pop(mask); // Pop saved mask from the stack.
5349 if (!result.is(eax)) {
5350 __ mov(eax, result);
5351 }
5352}
5353
5354
5355void StringHelper::GenerateHashInit(MacroAssembler* masm,
5356 Register hash,
5357 Register character,
5358 Register scratch) {
5359 // hash = character + (character << 10);
5360 __ mov(hash, character);
5361 __ shl(hash, 10);
5362 __ add(hash, Operand(character));
5363 // hash ^= hash >> 6;
5364 __ mov(scratch, hash);
5365 __ sar(scratch, 6);
5366 __ xor_(hash, Operand(scratch));
5367}
5368
5369
5370void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5371 Register hash,
5372 Register character,
5373 Register scratch) {
5374 // hash += character;
5375 __ add(hash, Operand(character));
5376 // hash += hash << 10;
5377 __ mov(scratch, hash);
5378 __ shl(scratch, 10);
5379 __ add(hash, Operand(scratch));
5380 // hash ^= hash >> 6;
5381 __ mov(scratch, hash);
5382 __ sar(scratch, 6);
5383 __ xor_(hash, Operand(scratch));
5384}
5385
5386
5387void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5388 Register hash,
5389 Register scratch) {
5390 // hash += hash << 3;
5391 __ mov(scratch, hash);
5392 __ shl(scratch, 3);
5393 __ add(hash, Operand(scratch));
5394 // hash ^= hash >> 11;
5395 __ mov(scratch, hash);
5396 __ sar(scratch, 11);
5397 __ xor_(hash, Operand(scratch));
5398 // hash += hash << 15;
5399 __ mov(scratch, hash);
5400 __ shl(scratch, 15);
5401 __ add(hash, Operand(scratch));
5402
5403 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005404 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005405 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005406 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005407 __ mov(hash, Immediate(27));
5408 __ bind(&hash_not_zero);
5409}
5410
5411
5412void SubStringStub::Generate(MacroAssembler* masm) {
5413 Label runtime;
5414
5415 // Stack frame on entry.
5416 // esp[0]: return address
5417 // esp[4]: to
5418 // esp[8]: from
5419 // esp[12]: string
5420
5421 // Make sure first argument is a string.
5422 __ mov(eax, Operand(esp, 3 * kPointerSize));
5423 STATIC_ASSERT(kSmiTag == 0);
5424 __ test(eax, Immediate(kSmiTagMask));
5425 __ j(zero, &runtime);
5426 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5427 __ j(NegateCondition(is_string), &runtime);
5428
5429 // eax: string
5430 // ebx: instance type
5431
5432 // Calculate length of sub string using the smi values.
5433 Label result_longer_than_two;
5434 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
5435 __ test(ecx, Immediate(kSmiTagMask));
5436 __ j(not_zero, &runtime);
5437 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
5438 __ test(edx, Immediate(kSmiTagMask));
5439 __ j(not_zero, &runtime);
5440 __ sub(ecx, Operand(edx));
5441 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5442 Label return_eax;
5443 __ j(equal, &return_eax);
5444 // Special handling of sub-strings of length 1 and 2. One character strings
5445 // are handled in the runtime system (looked up in the single character
5446 // cache). Two character strings are looked for in the symbol cache.
5447 __ SmiUntag(ecx); // Result length is no longer smi.
5448 __ cmp(ecx, 2);
5449 __ j(greater, &result_longer_than_two);
5450 __ j(less, &runtime);
5451
5452 // Sub string of length 2 requested.
5453 // eax: string
5454 // ebx: instance type
5455 // ecx: sub string length (value is 2)
5456 // edx: from index (smi)
5457 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5458
5459 // Get the two characters forming the sub string.
5460 __ SmiUntag(edx); // From index is no longer smi.
5461 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5462 __ movzx_b(ecx,
5463 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5464
5465 // Try to lookup two character string in symbol table.
5466 Label make_two_character_string;
5467 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005468 masm, ebx, ecx, eax, edx, edi,
5469 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005470 __ ret(3 * kPointerSize);
5471
5472 __ bind(&make_two_character_string);
5473 // Setup registers for allocating the two character string.
5474 __ mov(eax, Operand(esp, 3 * kPointerSize));
5475 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5476 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5477 __ Set(ecx, Immediate(2));
5478
5479 __ bind(&result_longer_than_two);
5480 // eax: string
5481 // ebx: instance type
5482 // ecx: result string length
5483 // Check for flat ascii string
5484 Label non_ascii_flat;
5485 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5486
5487 // Allocate the result.
5488 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5489
5490 // eax: result string
5491 // ecx: result string length
5492 __ mov(edx, esi); // esi used by following code.
5493 // Locate first character of result.
5494 __ mov(edi, eax);
5495 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5496 // Load string argument and locate character of sub string start.
5497 __ mov(esi, Operand(esp, 3 * kPointerSize));
5498 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5499 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5500 __ SmiUntag(ebx);
5501 __ add(esi, Operand(ebx));
5502
5503 // eax: result string
5504 // ecx: result length
5505 // edx: original value of esi
5506 // edi: first character of result
5507 // esi: character of sub string start
5508 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5509 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005510 Counters* counters = masm->isolate()->counters();
5511 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005512 __ ret(3 * kPointerSize);
5513
5514 __ bind(&non_ascii_flat);
5515 // eax: string
5516 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5517 // ecx: result string length
5518 // Check for flat two byte string
5519 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5520 __ j(not_equal, &runtime);
5521
5522 // Allocate the result.
5523 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5524
5525 // eax: result string
5526 // ecx: result string length
5527 __ mov(edx, esi); // esi used by following code.
5528 // Locate first character of result.
5529 __ mov(edi, eax);
5530 __ add(Operand(edi),
5531 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5532 // Load string argument and locate character of sub string start.
5533 __ mov(esi, Operand(esp, 3 * kPointerSize));
5534 __ add(Operand(esi),
5535 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5536 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5537 // As from is a smi it is 2 times the value which matches the size of a two
5538 // byte character.
5539 STATIC_ASSERT(kSmiTag == 0);
5540 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5541 __ add(esi, Operand(ebx));
5542
5543 // eax: result string
5544 // ecx: result length
5545 // edx: original value of esi
5546 // edi: first character of result
5547 // esi: character of sub string start
5548 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5549 __ mov(esi, edx); // Restore esi.
5550
5551 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005552 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005553 __ ret(3 * kPointerSize);
5554
5555 // Just jump to runtime to create the sub string.
5556 __ bind(&runtime);
5557 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5558}
5559
5560
lrn@chromium.org1c092762011-05-09 09:42:16 +00005561void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5562 Register left,
5563 Register right,
5564 Register scratch1,
5565 Register scratch2) {
5566 Register length = scratch1;
5567
5568 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005569 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005570 __ mov(length, FieldOperand(left, String::kLengthOffset));
5571 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005572 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005573 __ bind(&strings_not_equal);
5574 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5575 __ ret(0);
5576
5577 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005578 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005579 __ bind(&check_zero_length);
5580 STATIC_ASSERT(kSmiTag == 0);
5581 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005582 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005583 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5584 __ ret(0);
5585
5586 // Compare characters.
5587 __ bind(&compare_chars);
5588 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005589 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005590
5591 // Characters are equal.
5592 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5593 __ ret(0);
5594}
5595
5596
ricow@chromium.org65fae842010-08-25 15:26:24 +00005597void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5598 Register left,
5599 Register right,
5600 Register scratch1,
5601 Register scratch2,
5602 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005603 Counters* counters = masm->isolate()->counters();
5604 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005605
5606 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005607 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005608 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5609 __ mov(scratch3, scratch1);
5610 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5611
5612 Register length_delta = scratch3;
5613
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005614 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005615 // Right string is shorter. Change scratch1 to be length of right string.
5616 __ sub(scratch1, Operand(length_delta));
5617 __ bind(&left_shorter);
5618
5619 Register min_length = scratch1;
5620
5621 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005622 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005623 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005624 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005625
lrn@chromium.org1c092762011-05-09 09:42:16 +00005626 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005627 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005628 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005629 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005630
5631 // Compare lengths - strings up to min-length are equal.
5632 __ bind(&compare_lengths);
5633 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005634 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005635
5636 // Result is EQUAL.
5637 STATIC_ASSERT(EQUAL == 0);
5638 STATIC_ASSERT(kSmiTag == 0);
5639 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5640 __ ret(0);
5641
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005642 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005643 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005644 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005645
5646 // Result is LESS.
5647 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5648 __ ret(0);
5649
5650 // Result is GREATER.
5651 __ bind(&result_greater);
5652 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5653 __ ret(0);
5654}
5655
5656
lrn@chromium.org1c092762011-05-09 09:42:16 +00005657void StringCompareStub::GenerateAsciiCharsCompareLoop(
5658 MacroAssembler* masm,
5659 Register left,
5660 Register right,
5661 Register length,
5662 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005663 Label* chars_not_equal,
5664 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005665 // Change index to run from -length to -1 by adding length to string
5666 // start. This means that loop ends when index reaches zero, which
5667 // doesn't need an additional compare.
5668 __ SmiUntag(length);
5669 __ lea(left,
5670 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5671 __ lea(right,
5672 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5673 __ neg(length);
5674 Register index = length; // index = -length;
5675
5676 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005677 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005678 __ bind(&loop);
5679 __ mov_b(scratch, Operand(left, index, times_1, 0));
5680 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005681 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005682 __ add(Operand(index), Immediate(1));
5683 __ j(not_zero, &loop);
5684}
5685
5686
ricow@chromium.org65fae842010-08-25 15:26:24 +00005687void StringCompareStub::Generate(MacroAssembler* masm) {
5688 Label runtime;
5689
5690 // Stack frame on entry.
5691 // esp[0]: return address
5692 // esp[4]: right string
5693 // esp[8]: left string
5694
5695 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5696 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5697
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005698 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005699 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005700 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005701 STATIC_ASSERT(EQUAL == 0);
5702 STATIC_ASSERT(kSmiTag == 0);
5703 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005704 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005705 __ ret(2 * kPointerSize);
5706
5707 __ bind(&not_same);
5708
5709 // Check that both objects are sequential ascii strings.
5710 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5711
5712 // Compare flat ascii strings.
5713 // Drop arguments from the stack.
5714 __ pop(ecx);
5715 __ add(Operand(esp), Immediate(2 * kPointerSize));
5716 __ push(ecx);
5717 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5718
5719 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5720 // tagged as a small integer.
5721 __ bind(&runtime);
5722 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5723}
5724
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005725
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005726void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5727 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005728 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005729 __ mov(ecx, Operand(edx));
5730 __ or_(ecx, Operand(eax));
5731 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005732 __ j(not_zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005733
5734 if (GetCondition() == equal) {
5735 // For equality we do not care about the sign of the result.
5736 __ sub(eax, Operand(edx));
5737 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005738 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005739 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005740 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005741 // Correct sign of result in case of overflow.
5742 __ not_(edx);
5743 __ bind(&done);
5744 __ mov(eax, edx);
5745 }
5746 __ ret(0);
5747
5748 __ bind(&miss);
5749 GenerateMiss(masm);
5750}
5751
5752
5753void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5754 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5755
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005756 Label generic_stub;
5757 Label unordered;
5758 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005759 __ mov(ecx, Operand(edx));
5760 __ and_(ecx, Operand(eax));
5761 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005762 __ j(zero, &generic_stub, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005763
5764 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005765 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005766 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005767 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005768
5769 // Inlining the double comparison and falling back to the general compare
5770 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005771 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005772 CpuFeatures::Scope scope1(SSE2);
5773 CpuFeatures::Scope scope2(CMOV);
5774
5775 // Load left and right operand
5776 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5777 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5778
5779 // Compare operands
5780 __ ucomisd(xmm0, xmm1);
5781
5782 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005783 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005784
5785 // Return a result of -1, 0, or 1, based on EFLAGS.
5786 // Performing mov, because xor would destroy the flag register.
5787 __ mov(eax, 0); // equal
5788 __ mov(ecx, Immediate(Smi::FromInt(1)));
5789 __ cmov(above, eax, Operand(ecx));
5790 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5791 __ cmov(below, eax, Operand(ecx));
5792 __ ret(0);
5793
5794 __ bind(&unordered);
5795 }
5796
5797 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5798 __ bind(&generic_stub);
5799 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5800
5801 __ bind(&miss);
5802 GenerateMiss(masm);
5803}
5804
5805
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005806void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5807 ASSERT(state_ == CompareIC::SYMBOLS);
5808 ASSERT(GetCondition() == equal);
5809
5810 // Registers containing left and right operands respectively.
5811 Register left = edx;
5812 Register right = eax;
5813 Register tmp1 = ecx;
5814 Register tmp2 = ebx;
5815
5816 // Check that both operands are heap objects.
5817 Label miss;
5818 __ mov(tmp1, Operand(left));
5819 STATIC_ASSERT(kSmiTag == 0);
5820 __ and_(tmp1, Operand(right));
5821 __ test(tmp1, Immediate(kSmiTagMask));
5822 __ j(zero, &miss, Label::kNear);
5823
5824 // Check that both operands are symbols.
5825 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5826 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5827 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5828 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5829 STATIC_ASSERT(kSymbolTag != 0);
5830 __ and_(tmp1, Operand(tmp2));
5831 __ test(tmp1, Immediate(kIsSymbolMask));
5832 __ j(zero, &miss, Label::kNear);
5833
5834 // Symbols are compared by identity.
5835 Label done;
5836 __ cmp(left, Operand(right));
5837 // Make sure eax is non-zero. At this point input operands are
5838 // guaranteed to be non-zero.
5839 ASSERT(right.is(eax));
5840 __ j(not_equal, &done, Label::kNear);
5841 STATIC_ASSERT(EQUAL == 0);
5842 STATIC_ASSERT(kSmiTag == 0);
5843 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5844 __ bind(&done);
5845 __ ret(0);
5846
5847 __ bind(&miss);
5848 GenerateMiss(masm);
5849}
5850
5851
lrn@chromium.org1c092762011-05-09 09:42:16 +00005852void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5853 ASSERT(state_ == CompareIC::STRINGS);
5854 ASSERT(GetCondition() == equal);
5855 Label miss;
5856
5857 // Registers containing left and right operands respectively.
5858 Register left = edx;
5859 Register right = eax;
5860 Register tmp1 = ecx;
5861 Register tmp2 = ebx;
5862 Register tmp3 = edi;
5863
5864 // Check that both operands are heap objects.
5865 __ mov(tmp1, Operand(left));
5866 STATIC_ASSERT(kSmiTag == 0);
5867 __ and_(tmp1, Operand(right));
5868 __ test(tmp1, Immediate(kSmiTagMask));
5869 __ j(zero, &miss);
5870
5871 // Check that both operands are strings. This leaves the instance
5872 // types loaded in tmp1 and tmp2.
5873 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5874 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5875 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5876 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5877 __ mov(tmp3, tmp1);
5878 STATIC_ASSERT(kNotStringTag != 0);
5879 __ or_(tmp3, Operand(tmp2));
5880 __ test(tmp3, Immediate(kIsNotStringMask));
5881 __ j(not_zero, &miss);
5882
5883 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005884 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005885 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005886 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005887 STATIC_ASSERT(EQUAL == 0);
5888 STATIC_ASSERT(kSmiTag == 0);
5889 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5890 __ ret(0);
5891
5892 // Handle not identical strings.
5893 __ bind(&not_same);
5894
5895 // Check that both strings are symbols. If they are, we're done
5896 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005897 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005898 STATIC_ASSERT(kSymbolTag != 0);
5899 __ and_(tmp1, Operand(tmp2));
5900 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005901 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005902 // Make sure eax is non-zero. At this point input operands are
5903 // guaranteed to be non-zero.
5904 ASSERT(right.is(eax));
5905 __ ret(0);
5906
5907 // Check that both strings are sequential ASCII.
5908 Label runtime;
5909 __ bind(&do_compare);
5910 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5911
5912 // Compare flat ASCII strings. Returns when done.
5913 StringCompareStub::GenerateFlatAsciiStringEquals(
5914 masm, left, right, tmp1, tmp2);
5915
5916 // Handle more complex cases in runtime.
5917 __ bind(&runtime);
5918 __ pop(tmp1); // Return address.
5919 __ push(left);
5920 __ push(right);
5921 __ push(tmp1);
5922 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5923
5924 __ bind(&miss);
5925 GenerateMiss(masm);
5926}
5927
5928
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005929void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5930 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005931 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005932 __ mov(ecx, Operand(edx));
5933 __ and_(ecx, Operand(eax));
5934 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005935 __ j(zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005936
5937 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005938 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005939 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005940 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005941
5942 ASSERT(GetCondition() == equal);
5943 __ sub(eax, Operand(edx));
5944 __ ret(0);
5945
5946 __ bind(&miss);
5947 GenerateMiss(masm);
5948}
5949
5950
5951void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
5952 // Save the registers.
5953 __ pop(ecx);
5954 __ push(edx);
5955 __ push(eax);
5956 __ push(ecx);
5957
5958 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005959 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
5960 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005961 __ EnterInternalFrame();
5962 __ push(edx);
5963 __ push(eax);
5964 __ push(Immediate(Smi::FromInt(op_)));
5965 __ CallExternalReference(miss, 3);
5966 __ LeaveInternalFrame();
5967
5968 // Compute the entry point of the rewritten stub.
5969 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
5970
5971 // Restore registers.
5972 __ pop(ecx);
5973 __ pop(eax);
5974 __ pop(edx);
5975 __ push(ecx);
5976
5977 // Do a tail call to the rewritten stub.
5978 __ jmp(Operand(edi));
5979}
5980
5981
lrn@chromium.org1c092762011-05-09 09:42:16 +00005982// Helper function used to check that the dictionary doesn't contain
5983// the property. This function may return false negatives, so miss_label
5984// must always call a backup property check that is complete.
5985// This function is safe to call if the receiver has fast properties.
5986// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005987MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
5988 MacroAssembler* masm,
5989 Label* miss,
5990 Label* done,
5991 Register properties,
5992 String* name,
5993 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005994 ASSERT(name->IsSymbol());
5995
5996 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5997 // not equal to the name and kProbes-th slot is not used (its name is the
5998 // undefined value), it guarantees the hash table doesn't contain the
5999 // property. It's true even if some slots represent deleted properties
6000 // (their names are the null value).
6001 for (int i = 0; i < kInlinedProbes; i++) {
6002 // Compute the masked index: (hash + i + i * i) & mask.
6003 Register index = r0;
6004 // Capacity is smi 2^n.
6005 __ mov(index, FieldOperand(properties, kCapacityOffset));
6006 __ dec(index);
6007 __ and_(Operand(index),
6008 Immediate(Smi::FromInt(name->Hash() +
6009 StringDictionary::GetProbeOffset(i))));
6010
6011 // Scale the index by multiplying by the entry size.
6012 ASSERT(StringDictionary::kEntrySize == 3);
6013 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
6014 Register entity_name = r0;
6015 // Having undefined at this place means the name is not contained.
6016 ASSERT_EQ(kSmiTagSize, 1);
6017 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
6018 kElementsStartOffset - kHeapObjectTag));
6019 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006020 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006021
6022 // Stop if found the property.
6023 __ cmp(entity_name, Handle<String>(name));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006024 __ j(equal, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006025
6026 // Check if the entry name is not a symbol.
6027 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6028 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6029 kIsSymbolMask);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006030 __ j(zero, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006031 }
6032
6033 StringDictionaryLookupStub stub(properties,
6034 r0,
6035 r0,
6036 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6037 __ push(Immediate(Handle<Object>(name)));
6038 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006039 MaybeObject* result = masm->TryCallStub(&stub);
6040 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006041 __ test(r0, Operand(r0));
6042 __ j(not_zero, miss);
6043 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006044 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006045}
6046
6047
6048// Probe the string dictionary in the |elements| register. Jump to the
6049// |done| label if a property with the given name is found leaving the
6050// index into the dictionary in |r0|. Jump to the |miss| label
6051// otherwise.
6052void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6053 Label* miss,
6054 Label* done,
6055 Register elements,
6056 Register name,
6057 Register r0,
6058 Register r1) {
6059 // Assert that name contains a string.
6060 if (FLAG_debug_code) __ AbortIfNotString(name);
6061
6062 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6063 __ shr(r1, kSmiTagSize); // convert smi to int
6064 __ dec(r1);
6065
6066 // Generate an unrolled loop that performs a few probes before
6067 // giving up. Measurements done on Gmail indicate that 2 probes
6068 // cover ~93% of loads from dictionaries.
6069 for (int i = 0; i < kInlinedProbes; i++) {
6070 // Compute the masked index: (hash + i + i * i) & mask.
6071 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6072 __ shr(r0, String::kHashShift);
6073 if (i > 0) {
6074 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6075 }
6076 __ and_(r0, Operand(r1));
6077
6078 // Scale the index by multiplying by the entry size.
6079 ASSERT(StringDictionary::kEntrySize == 3);
6080 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6081
6082 // Check if the key is identical to the name.
6083 __ cmp(name, Operand(elements,
6084 r0,
6085 times_4,
6086 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006087 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006088 }
6089
6090 StringDictionaryLookupStub stub(elements,
6091 r1,
6092 r0,
6093 POSITIVE_LOOKUP);
6094 __ push(name);
6095 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6096 __ shr(r0, String::kHashShift);
6097 __ push(r0);
6098 __ CallStub(&stub);
6099
6100 __ test(r1, Operand(r1));
6101 __ j(zero, miss);
6102 __ jmp(done);
6103}
6104
6105
6106void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6107 // Stack frame on entry:
6108 // esp[0 * kPointerSize]: return address.
6109 // esp[1 * kPointerSize]: key's hash.
6110 // esp[2 * kPointerSize]: key.
6111 // Registers:
6112 // dictionary_: StringDictionary to probe.
6113 // result_: used as scratch.
6114 // index_: will hold an index of entry if lookup is successful.
6115 // might alias with result_.
6116 // Returns:
6117 // result_ is zero if lookup failed, non zero otherwise.
6118
6119 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6120
6121 Register scratch = result_;
6122
6123 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6124 __ dec(scratch);
6125 __ SmiUntag(scratch);
6126 __ push(scratch);
6127
6128 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6129 // not equal to the name and kProbes-th slot is not used (its name is the
6130 // undefined value), it guarantees the hash table doesn't contain the
6131 // property. It's true even if some slots represent deleted properties
6132 // (their names are the null value).
6133 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6134 // Compute the masked index: (hash + i + i * i) & mask.
6135 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6136 if (i > 0) {
6137 __ add(Operand(scratch),
6138 Immediate(StringDictionary::GetProbeOffset(i)));
6139 }
6140 __ and_(scratch, Operand(esp, 0));
6141
6142 // Scale the index by multiplying by the entry size.
6143 ASSERT(StringDictionary::kEntrySize == 3);
6144 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6145
6146 // Having undefined at this place means the name is not contained.
6147 ASSERT_EQ(kSmiTagSize, 1);
6148 __ mov(scratch, Operand(dictionary_,
6149 index_,
6150 times_pointer_size,
6151 kElementsStartOffset - kHeapObjectTag));
6152 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6153 __ j(equal, &not_in_dictionary);
6154
6155 // Stop if found the property.
6156 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6157 __ j(equal, &in_dictionary);
6158
6159 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6160 // If we hit a non symbol key during negative lookup
6161 // we have to bailout as this key might be equal to the
6162 // key we are looking for.
6163
6164 // Check if the entry name is not a symbol.
6165 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6166 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6167 kIsSymbolMask);
6168 __ j(zero, &maybe_in_dictionary);
6169 }
6170 }
6171
6172 __ bind(&maybe_in_dictionary);
6173 // If we are doing negative lookup then probing failure should be
6174 // treated as a lookup success. For positive lookup probing failure
6175 // should be treated as lookup failure.
6176 if (mode_ == POSITIVE_LOOKUP) {
6177 __ mov(result_, Immediate(0));
6178 __ Drop(1);
6179 __ ret(2 * kPointerSize);
6180 }
6181
6182 __ bind(&in_dictionary);
6183 __ mov(result_, Immediate(1));
6184 __ Drop(1);
6185 __ ret(2 * kPointerSize);
6186
6187 __ bind(&not_in_dictionary);
6188 __ mov(result_, Immediate(0));
6189 __ Drop(1);
6190 __ ret(2 * kPointerSize);
6191}
6192
6193
ricow@chromium.org65fae842010-08-25 15:26:24 +00006194#undef __
6195
6196} } // namespace v8::internal
6197
6198#endif // V8_TARGET_ARCH_IA32