blob: 777924b105472ff9ec3575fdf14952d0eec0396f [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_IA32)
31
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000033#include "bootstrapper.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000034#include "jsregexp.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000035#include "isolate.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
37
38namespace v8 {
39namespace internal {
40
41#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +000042
43void ToNumberStub::Generate(MacroAssembler* masm) {
44 // The ToNumber stub takes one argument in eax.
45 NearLabel check_heap_number, call_builtin;
46 __ test(eax, Immediate(kSmiTagMask));
47 __ j(not_zero, &check_heap_number);
48 __ ret(0);
49
50 __ bind(&check_heap_number);
51 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000052 Factory* factory = masm->isolate()->factory();
53 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org7a392b32011-01-31 11:30:36 +000054 __ j(not_equal, &call_builtin);
55 __ 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) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +0000245 NearLabel false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000246 __ mov(eax, Operand(esp, 1 * kPointerSize));
247
248 // 'null' => false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000249 Factory* factory = masm->isolate()->factory();
250 __ cmp(eax, factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000251 __ j(equal, &false_result);
252
253 // Get the map and type of the heap object.
254 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
255 __ movzx_b(ecx, FieldOperand(edx, Map::kInstanceTypeOffset));
256
257 // Undetectable => false.
258 __ test_b(FieldOperand(edx, Map::kBitFieldOffset),
259 1 << Map::kIsUndetectable);
260 __ j(not_zero, &false_result);
261
262 // JavaScript object => true.
263 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
264 __ j(above_equal, &true_result);
265
266 // String value => false iff empty.
267 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
268 __ j(above_equal, &not_string);
269 STATIC_ASSERT(kSmiTag == 0);
270 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
271 __ j(zero, &false_result);
272 __ jmp(&true_result);
273
274 __ bind(&not_string);
275 // HeapNumber => false iff +0, -0, or NaN.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000276 __ cmp(edx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000277 __ j(not_equal, &true_result);
278 __ fldz();
279 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
280 __ FCmp();
281 __ j(zero, &false_result);
282 // Fall through to |true_result|.
283
284 // Return 1/0 for true/false in eax.
285 __ bind(&true_result);
286 __ mov(eax, 1);
287 __ ret(1 * kPointerSize);
288 __ bind(&false_result);
289 __ mov(eax, 0);
290 __ ret(1 * kPointerSize);
291}
292
293
ricow@chromium.org65fae842010-08-25 15:26:24 +0000294class FloatingPointHelper : public AllStatic {
295 public:
296
297 enum ArgLocation {
298 ARGS_ON_STACK,
299 ARGS_IN_REGISTERS
300 };
301
302 // Code pattern for loading a floating point value. Input value must
303 // be either a smi or a heap number object (fp value). Requirements:
304 // operand in register number. Returns operand as floating point number
305 // on FPU stack.
306 static void LoadFloatOperand(MacroAssembler* masm, Register number);
307
308 // Code pattern for loading floating point values. Input values must
309 // be either smi or heap number objects (fp values). Requirements:
310 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
311 // Returns operands as floating point numbers on FPU stack.
312 static void LoadFloatOperands(MacroAssembler* masm,
313 Register scratch,
314 ArgLocation arg_location = ARGS_ON_STACK);
315
316 // Similar to LoadFloatOperand but assumes that both operands are smis.
317 // Expects operands in edx, eax.
318 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
319
320 // Test if operands are smi or number objects (fp). Requirements:
321 // operand_1 in eax, operand_2 in edx; falls through on float
322 // operands, jumps to the non_float label otherwise.
323 static void CheckFloatOperands(MacroAssembler* masm,
324 Label* non_float,
325 Register scratch);
326
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000327 // Checks that the two floating point numbers on top of the FPU stack
328 // have int32 values.
329 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
330 Label* non_int32);
331
ricow@chromium.org65fae842010-08-25 15:26:24 +0000332 // Takes the operands in edx and eax and loads them as integers in eax
333 // and ecx.
334 static void LoadAsIntegers(MacroAssembler* masm,
335 TypeInfo type_info,
336 bool use_sse3,
337 Label* operand_conversion_failure);
338 static void LoadNumbersAsIntegers(MacroAssembler* masm,
339 TypeInfo type_info,
340 bool use_sse3,
341 Label* operand_conversion_failure);
342 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
343 bool use_sse3,
344 Label* operand_conversion_failure);
345
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
347 // operands are pushed on the stack, and that their conversions to int32
348 // are in eax and ecx. Checks that the original numbers were in the int32
349 // range.
350 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
351 bool use_sse3,
352 Label* not_int32);
353
354 // Assumes that operands are smis or heap numbers and loads them
355 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000356 // Leaves operands unchanged.
357 static void LoadSSE2Operands(MacroAssembler* masm);
358
359 // Test if operands are numbers (smi or HeapNumber objects), and load
360 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
361 // either operand is not a number. Operands are in edx and eax.
362 // Leaves operands unchanged.
363 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
364
365 // Similar to LoadSSE2Operands but assumes that both operands are smis.
366 // Expects operands in edx, eax.
367 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000368
369 // Checks that the two floating point numbers loaded into xmm0 and xmm1
370 // have int32 values.
371 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
372 Label* non_int32,
373 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000374};
375
376
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000377// Get the integer part of a heap number. Surprisingly, all this bit twiddling
378// is faster than using the built-in instructions on floating point registers.
379// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
380// trashed registers.
381static void IntegerConvert(MacroAssembler* masm,
382 Register source,
383 TypeInfo type_info,
384 bool use_sse3,
385 Label* conversion_failure) {
386 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
387 Label done, right_exponent, normal_exponent;
388 Register scratch = ebx;
389 Register scratch2 = edi;
390 if (type_info.IsInteger32() && CpuFeatures::IsSupported(SSE2)) {
391 CpuFeatures::Scope scope(SSE2);
392 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
393 return;
394 }
395 if (!type_info.IsInteger32() || !use_sse3) {
396 // Get exponent word.
397 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
398 // Get exponent alone in scratch2.
399 __ mov(scratch2, scratch);
400 __ and_(scratch2, HeapNumber::kExponentMask);
401 }
402 if (use_sse3) {
403 CpuFeatures::Scope scope(SSE3);
404 if (!type_info.IsInteger32()) {
405 // Check whether the exponent is too big for a 64 bit signed integer.
406 static const uint32_t kTooBigExponent =
407 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
408 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
409 __ j(greater_equal, conversion_failure);
410 }
411 // Load x87 register with heap number.
412 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
413 // Reserve space for 64 bit answer.
414 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
415 // Do conversion, which cannot fail because we checked the exponent.
416 __ fisttp_d(Operand(esp, 0));
417 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
418 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
419 } else {
420 // Load ecx with zero. We use this either for the final shift or
421 // for the answer.
422 __ xor_(ecx, Operand(ecx));
423 // Check whether the exponent matches a 32 bit signed int that cannot be
424 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
425 // exponent is 30 (biased). This is the exponent that we are fastest at and
426 // also the highest exponent we can handle here.
427 const uint32_t non_smi_exponent =
428 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
429 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
430 // If we have a match of the int32-but-not-Smi exponent then skip some
431 // logic.
432 __ j(equal, &right_exponent);
433 // If the exponent is higher than that then go to slow case. This catches
434 // numbers that don't fit in a signed int32, infinities and NaNs.
435 __ j(less, &normal_exponent);
436
437 {
438 // Handle a big exponent. The only reason we have this code is that the
439 // >>> operator has a tendency to generate numbers with an exponent of 31.
440 const uint32_t big_non_smi_exponent =
441 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
442 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
443 __ j(not_equal, conversion_failure);
444 // We have the big exponent, typically from >>>. This means the number is
445 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
446 __ mov(scratch2, scratch);
447 __ and_(scratch2, HeapNumber::kMantissaMask);
448 // Put back the implicit 1.
449 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
450 // Shift up the mantissa bits to take up the space the exponent used to
451 // take. We just orred in the implicit bit so that took care of one and
452 // we want to use the full unsigned range so we subtract 1 bit from the
453 // shift distance.
454 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
455 __ shl(scratch2, big_shift_distance);
456 // Get the second half of the double.
457 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
458 // Shift down 21 bits to get the most significant 11 bits or the low
459 // mantissa word.
460 __ shr(ecx, 32 - big_shift_distance);
461 __ or_(ecx, Operand(scratch2));
462 // We have the answer in ecx, but we may need to negate it.
463 __ test(scratch, Operand(scratch));
464 __ j(positive, &done);
465 __ neg(ecx);
466 __ jmp(&done);
467 }
468
469 __ bind(&normal_exponent);
470 // Exponent word in scratch, exponent part of exponent word in scratch2.
471 // Zero in ecx.
472 // We know the exponent is smaller than 30 (biased). If it is less than
473 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
474 // it rounds to zero.
475 const uint32_t zero_exponent =
476 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
477 __ sub(Operand(scratch2), Immediate(zero_exponent));
478 // ecx already has a Smi zero.
479 __ j(less, &done);
480
481 // We have a shifted exponent between 0 and 30 in scratch2.
482 __ shr(scratch2, HeapNumber::kExponentShift);
483 __ mov(ecx, Immediate(30));
484 __ sub(ecx, Operand(scratch2));
485
486 __ bind(&right_exponent);
487 // Here ecx is the shift, scratch is the exponent word.
488 // Get the top bits of the mantissa.
489 __ and_(scratch, HeapNumber::kMantissaMask);
490 // Put back the implicit 1.
491 __ or_(scratch, 1 << HeapNumber::kExponentShift);
492 // Shift up the mantissa bits to take up the space the exponent used to
493 // take. We have kExponentShift + 1 significant bits int he low end of the
494 // word. Shift them to the top bits.
495 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
496 __ shl(scratch, shift_distance);
497 // Get the second half of the double. For some exponents we don't
498 // actually need this because the bits get shifted out again, but
499 // it's probably slower to test than just to do it.
500 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
501 // Shift down 22 bits to get the most significant 10 bits or the low
502 // mantissa word.
503 __ shr(scratch2, 32 - shift_distance);
504 __ or_(scratch2, Operand(scratch));
505 // Move down according to the exponent.
506 __ shr_cl(scratch2);
507 // Now the unsigned answer is in scratch2. We need to move it to ecx and
508 // we may need to fix the sign.
509 NearLabel negative;
510 __ xor_(ecx, Operand(ecx));
511 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
512 __ j(greater, &negative);
513 __ mov(ecx, scratch2);
514 __ jmp(&done);
515 __ bind(&negative);
516 __ sub(ecx, Operand(scratch2));
517 __ bind(&done);
518 }
519}
520
521
522Handle<Code> GetTypeRecordingUnaryOpStub(int key,
523 TRUnaryOpIC::TypeInfo type_info) {
524 TypeRecordingUnaryOpStub stub(key, type_info);
525 return stub.GetCode();
526}
527
528
529const char* TypeRecordingUnaryOpStub::GetName() {
530 if (name_ != NULL) return name_;
531 const int kMaxNameLength = 100;
532 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
533 kMaxNameLength);
534 if (name_ == NULL) return "OOM";
535 const char* op_name = Token::Name(op_);
536 const char* overwrite_name = NULL; // Make g++ happy.
537 switch (mode_) {
538 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
539 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
540 }
541
542 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
543 "TypeRecordingUnaryOpStub_%s_%s_%s",
544 op_name,
545 overwrite_name,
546 TRUnaryOpIC::GetName(operand_type_));
547 return name_;
548}
549
550
551// TODO(svenpanne): Use virtual functions instead of switch.
552void TypeRecordingUnaryOpStub::Generate(MacroAssembler* masm) {
553 switch (operand_type_) {
554 case TRUnaryOpIC::UNINITIALIZED:
555 GenerateTypeTransition(masm);
556 break;
557 case TRUnaryOpIC::SMI:
558 GenerateSmiStub(masm);
559 break;
560 case TRUnaryOpIC::HEAP_NUMBER:
561 GenerateHeapNumberStub(masm);
562 break;
563 case TRUnaryOpIC::GENERIC:
564 GenerateGenericStub(masm);
565 break;
566 }
567}
568
569
570void TypeRecordingUnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
571 __ pop(ecx); // Save return address.
572 __ push(eax);
573 // the argument is now on top.
574 // Push this stub's key. Although the operation and the type info are
575 // encoded into the key, the encoding is opaque, so push them too.
576 __ push(Immediate(Smi::FromInt(MinorKey())));
577 __ push(Immediate(Smi::FromInt(op_)));
578 __ push(Immediate(Smi::FromInt(operand_type_)));
579
580 __ push(ecx); // Push return address.
581
582 // Patch the caller to an appropriate specialized stub and return the
583 // operation result to the caller of the stub.
584 __ TailCallExternalReference(
585 ExternalReference(IC_Utility(IC::kTypeRecordingUnaryOp_Patch),
586 masm->isolate()),
587 4,
588 1);
589}
590
591
592// TODO(svenpanne): Use virtual functions instead of switch.
593void TypeRecordingUnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
594 switch (op_) {
595 case Token::SUB:
596 GenerateSmiStubSub(masm);
597 break;
598 case Token::BIT_NOT:
599 GenerateSmiStubBitNot(masm);
600 break;
601 default:
602 UNREACHABLE();
603 }
604}
605
606
607void TypeRecordingUnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
608 NearLabel non_smi;
609 Label undo, slow;
610 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow);
611 __ bind(&undo);
612 GenerateSmiCodeUndo(masm);
613 __ bind(&non_smi);
614 __ bind(&slow);
615 GenerateTypeTransition(masm);
616}
617
618
619void TypeRecordingUnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
620 NearLabel non_smi;
621 GenerateSmiCodeBitNot(masm, &non_smi);
622 __ bind(&non_smi);
623 GenerateTypeTransition(masm);
624}
625
626
627void TypeRecordingUnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
628 NearLabel* non_smi,
629 Label* undo,
630 Label* slow) {
631 // Check whether the value is a smi.
632 __ test(eax, Immediate(kSmiTagMask));
633 __ j(not_zero, non_smi);
634
635 // We can't handle -0 with smis, so use a type transition for that case.
636 __ test(eax, Operand(eax));
637 __ j(zero, slow);
638
639 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
640 __ mov(edx, Operand(eax));
641 __ Set(eax, Immediate(0));
642 __ sub(eax, Operand(edx));
643 __ j(overflow, undo);
644 __ ret(0);
645}
646
647
648void TypeRecordingUnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
649 NearLabel* non_smi) {
650 // Check whether the value is a smi.
651 __ test(eax, Immediate(kSmiTagMask));
652 __ j(not_zero, non_smi);
653
654 // Flip bits and revert inverted smi-tag.
655 __ not_(eax);
656 __ and_(eax, ~kSmiTagMask);
657 __ ret(0);
658}
659
660
661void TypeRecordingUnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
662 __ mov(eax, Operand(edx));
663}
664
665
666// TODO(svenpanne): Use virtual functions instead of switch.
667void TypeRecordingUnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
668 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
681void TypeRecordingUnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
682 NearLabel non_smi;
683 Label undo, slow;
684 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow);
685 __ bind(&non_smi);
686 GenerateHeapNumberCodeSub(masm, &slow);
687 __ bind(&undo);
688 GenerateSmiCodeUndo(masm);
689 __ bind(&slow);
690 GenerateTypeTransition(masm);
691}
692
693
694void TypeRecordingUnaryOpStub::GenerateHeapNumberStubBitNot(
695 MacroAssembler* masm) {
696 NearLabel non_smi;
697 Label slow;
698 GenerateSmiCodeBitNot(masm, &non_smi);
699 __ bind(&non_smi);
700 GenerateHeapNumberCodeBitNot(masm, &slow);
701 __ bind(&slow);
702 GenerateTypeTransition(masm);
703}
704
705
706void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
707 Label* slow) {
708 __ 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
742void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeBitNot(
743 MacroAssembler* masm,
744 Label* slow) {
745 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
746 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
747 __ j(not_equal, slow);
748
749 // Convert the heap number in eax to an untagged integer in ecx.
750 IntegerConvert(masm, eax, TypeInfo::Unknown(), CpuFeatures::IsSupported(SSE3),
751 slow);
752
753 // Do the bitwise operation and check if the result fits in a smi.
754 NearLabel try_float;
755 __ not_(ecx);
756 __ cmp(ecx, 0xc0000000);
757 __ j(sign, &try_float);
758
759 // Tag the result as a smi and we're done.
760 STATIC_ASSERT(kSmiTagSize == 1);
761 __ lea(eax, Operand(ecx, times_2, kSmiTag));
762 __ ret(0);
763
764 // Try to store the result in a heap number.
765 __ bind(&try_float);
766 if (mode_ == UNARY_NO_OVERWRITE) {
767 Label slow_allocate_heapnumber, heapnumber_allocated;
768 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
769 __ jmp(&heapnumber_allocated);
770
771 __ bind(&slow_allocate_heapnumber);
772 __ EnterInternalFrame();
773 __ push(ecx);
774 __ CallRuntime(Runtime::kNumberAlloc, 0);
775 __ pop(ecx);
776 __ LeaveInternalFrame();
777
778 __ bind(&heapnumber_allocated);
779 }
780 if (CpuFeatures::IsSupported(SSE2)) {
781 CpuFeatures::Scope use_sse2(SSE2);
782 __ cvtsi2sd(xmm0, Operand(ecx));
783 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
784 } else {
785 __ push(ecx);
786 __ fild_s(Operand(esp, 0));
787 __ pop(ecx);
788 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
789 }
790 __ ret(0);
791}
792
793
794// TODO(svenpanne): Use virtual functions instead of switch.
795void TypeRecordingUnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
796 switch (op_) {
797 case Token::SUB:
798 GenerateGenericStubSub(masm);
799 break;
800 case Token::BIT_NOT:
801 GenerateGenericStubBitNot(masm);
802 break;
803 default:
804 UNREACHABLE();
805 }
806}
807
808
809void TypeRecordingUnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
810 NearLabel non_smi;
811 Label undo, slow;
812 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow);
813 __ bind(&non_smi);
814 GenerateHeapNumberCodeSub(masm, &slow);
815 __ bind(&undo);
816 GenerateSmiCodeUndo(masm);
817 __ bind(&slow);
818 GenerateGenericCodeFallback(masm);
819}
820
821
822void TypeRecordingUnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
823 NearLabel non_smi;
824 Label slow;
825 GenerateSmiCodeBitNot(masm, &non_smi);
826 __ bind(&non_smi);
827 GenerateHeapNumberCodeBitNot(masm, &slow);
828 __ bind(&slow);
829 GenerateGenericCodeFallback(masm);
830}
831
832
833void TypeRecordingUnaryOpStub::GenerateGenericCodeFallback(
834 MacroAssembler* masm) {
835 // Handle the slow case by jumping to the corresponding JavaScript builtin.
836 __ pop(ecx); // pop return address.
837 __ push(eax);
838 __ push(ecx); // push return address
839 switch (op_) {
840 case Token::SUB:
841 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
842 break;
843 case Token::BIT_NOT:
844 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
845 break;
846 default:
847 UNREACHABLE();
848 }
849}
850
851
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000852Handle<Code> GetTypeRecordingBinaryOpStub(int key,
853 TRBinaryOpIC::TypeInfo type_info,
854 TRBinaryOpIC::TypeInfo result_type_info) {
855 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
856 return stub.GetCode();
857}
858
859
860void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
861 __ pop(ecx); // Save return address.
862 __ push(edx);
863 __ push(eax);
864 // Left and right arguments are now on top.
865 // Push this stub's key. Although the operation and the type info are
866 // encoded into the key, the encoding is opaque, so push them too.
867 __ push(Immediate(Smi::FromInt(MinorKey())));
868 __ push(Immediate(Smi::FromInt(op_)));
869 __ push(Immediate(Smi::FromInt(operands_type_)));
870
871 __ push(ecx); // Push return address.
872
873 // Patch the caller to an appropriate specialized stub and return the
874 // operation result to the caller of the stub.
875 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000876 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
877 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000878 5,
879 1);
880}
881
882
883// Prepare for a type transition runtime call when the args are already on
884// the stack, under the return address.
885void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
886 MacroAssembler* masm) {
887 __ pop(ecx); // Save return address.
888 // Left and right arguments are already on top of the stack.
889 // Push this stub's key. Although the operation and the type info are
890 // encoded into the key, the encoding is opaque, so push them too.
891 __ push(Immediate(Smi::FromInt(MinorKey())));
892 __ push(Immediate(Smi::FromInt(op_)));
893 __ push(Immediate(Smi::FromInt(operands_type_)));
894
895 __ push(ecx); // Push return address.
896
897 // Patch the caller to an appropriate specialized stub and return the
898 // operation result to the caller of the stub.
899 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000900 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
901 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000902 5,
903 1);
904}
905
906
907void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
908 switch (operands_type_) {
909 case TRBinaryOpIC::UNINITIALIZED:
910 GenerateTypeTransition(masm);
911 break;
912 case TRBinaryOpIC::SMI:
913 GenerateSmiStub(masm);
914 break;
915 case TRBinaryOpIC::INT32:
916 GenerateInt32Stub(masm);
917 break;
918 case TRBinaryOpIC::HEAP_NUMBER:
919 GenerateHeapNumberStub(masm);
920 break;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000921 case TRBinaryOpIC::ODDBALL:
922 GenerateOddballStub(masm);
923 break;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000924 case TRBinaryOpIC::BOTH_STRING:
925 GenerateBothStringStub(masm);
926 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000927 case TRBinaryOpIC::STRING:
928 GenerateStringStub(masm);
929 break;
930 case TRBinaryOpIC::GENERIC:
931 GenerateGeneric(masm);
932 break;
933 default:
934 UNREACHABLE();
935 }
936}
937
938
939const char* TypeRecordingBinaryOpStub::GetName() {
940 if (name_ != NULL) return name_;
941 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000942 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
943 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000944 if (name_ == NULL) return "OOM";
945 const char* op_name = Token::Name(op_);
946 const char* overwrite_name;
947 switch (mode_) {
948 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
949 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
950 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
951 default: overwrite_name = "UnknownOverwrite"; break;
952 }
953
954 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
955 "TypeRecordingBinaryOpStub_%s_%s_%s",
956 op_name,
957 overwrite_name,
958 TRBinaryOpIC::GetName(operands_type_));
959 return name_;
960}
961
962
963void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
964 Label* slow,
965 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
966 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
967 // dividend in eax and edx free for the division. Use eax, ebx for those.
968 Comment load_comment(masm, "-- Load arguments");
969 Register left = edx;
970 Register right = eax;
971 if (op_ == Token::DIV || op_ == Token::MOD) {
972 left = eax;
973 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000974 __ mov(ebx, eax);
975 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000976 }
977
978
979 // 2. Prepare the smi check of both operands by oring them together.
980 Comment smi_check_comment(masm, "-- Smi check arguments");
981 Label not_smis;
982 Register combined = ecx;
983 ASSERT(!left.is(combined) && !right.is(combined));
984 switch (op_) {
985 case Token::BIT_OR:
986 // Perform the operation into eax and smi check the result. Preserve
987 // eax in case the result is not a smi.
988 ASSERT(!left.is(ecx) && !right.is(ecx));
989 __ mov(ecx, right);
990 __ or_(right, Operand(left)); // Bitwise or is commutative.
991 combined = right;
992 break;
993
994 case Token::BIT_XOR:
995 case Token::BIT_AND:
996 case Token::ADD:
997 case Token::SUB:
998 case Token::MUL:
999 case Token::DIV:
1000 case Token::MOD:
1001 __ mov(combined, right);
1002 __ or_(combined, Operand(left));
1003 break;
1004
1005 case Token::SHL:
1006 case Token::SAR:
1007 case Token::SHR:
1008 // Move the right operand into ecx for the shift operation, use eax
1009 // for the smi check register.
1010 ASSERT(!left.is(ecx) && !right.is(ecx));
1011 __ mov(ecx, right);
1012 __ or_(right, Operand(left));
1013 combined = right;
1014 break;
1015
1016 default:
1017 break;
1018 }
1019
1020 // 3. Perform the smi check of the operands.
1021 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1022 __ test(combined, Immediate(kSmiTagMask));
1023 __ j(not_zero, &not_smis, not_taken);
1024
1025 // 4. Operands are both smis, perform the operation leaving the result in
1026 // eax and check the result if necessary.
1027 Comment perform_smi(masm, "-- Perform smi operation");
1028 Label use_fp_on_smis;
1029 switch (op_) {
1030 case Token::BIT_OR:
1031 // Nothing to do.
1032 break;
1033
1034 case Token::BIT_XOR:
1035 ASSERT(right.is(eax));
1036 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1037 break;
1038
1039 case Token::BIT_AND:
1040 ASSERT(right.is(eax));
1041 __ and_(right, Operand(left)); // Bitwise and is commutative.
1042 break;
1043
1044 case Token::SHL:
1045 // Remove tags from operands (but keep sign).
1046 __ SmiUntag(left);
1047 __ SmiUntag(ecx);
1048 // Perform the operation.
1049 __ shl_cl(left);
1050 // Check that the *signed* result fits in a smi.
1051 __ cmp(left, 0xc0000000);
1052 __ j(sign, &use_fp_on_smis, not_taken);
1053 // Tag the result and store it in register eax.
1054 __ SmiTag(left);
1055 __ mov(eax, left);
1056 break;
1057
1058 case Token::SAR:
1059 // Remove tags from operands (but keep sign).
1060 __ SmiUntag(left);
1061 __ SmiUntag(ecx);
1062 // Perform the operation.
1063 __ sar_cl(left);
1064 // Tag the result and store it in register eax.
1065 __ SmiTag(left);
1066 __ mov(eax, left);
1067 break;
1068
1069 case Token::SHR:
1070 // Remove tags from operands (but keep sign).
1071 __ SmiUntag(left);
1072 __ SmiUntag(ecx);
1073 // Perform the operation.
1074 __ shr_cl(left);
1075 // Check that the *unsigned* result fits in a smi.
1076 // Neither of the two high-order bits can be set:
1077 // - 0x80000000: high bit would be lost when smi tagging.
1078 // - 0x40000000: this number would convert to negative when
1079 // Smi tagging these two cases can only happen with shifts
1080 // by 0 or 1 when handed a valid smi.
1081 __ test(left, Immediate(0xc0000000));
1082 __ j(not_zero, slow, not_taken);
1083 // Tag the result and store it in register eax.
1084 __ SmiTag(left);
1085 __ mov(eax, left);
1086 break;
1087
1088 case Token::ADD:
1089 ASSERT(right.is(eax));
1090 __ add(right, Operand(left)); // Addition is commutative.
1091 __ j(overflow, &use_fp_on_smis, not_taken);
1092 break;
1093
1094 case Token::SUB:
1095 __ sub(left, Operand(right));
1096 __ j(overflow, &use_fp_on_smis, not_taken);
1097 __ mov(eax, left);
1098 break;
1099
1100 case Token::MUL:
1101 // If the smi tag is 0 we can just leave the tag on one operand.
1102 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1103 // We can't revert the multiplication if the result is not a smi
1104 // so save the right operand.
1105 __ mov(ebx, right);
1106 // Remove tag from one of the operands (but keep sign).
1107 __ SmiUntag(right);
1108 // Do multiplication.
1109 __ imul(right, Operand(left)); // Multiplication is commutative.
1110 __ j(overflow, &use_fp_on_smis, not_taken);
1111 // Check for negative zero result. Use combined = left | right.
1112 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1113 break;
1114
1115 case Token::DIV:
1116 // We can't revert the division if the result is not a smi so
1117 // save the left operand.
1118 __ mov(edi, left);
1119 // Check for 0 divisor.
1120 __ test(right, Operand(right));
1121 __ j(zero, &use_fp_on_smis, not_taken);
1122 // Sign extend left into edx:eax.
1123 ASSERT(left.is(eax));
1124 __ cdq();
1125 // Divide edx:eax by right.
1126 __ idiv(right);
1127 // Check for the corner case of dividing the most negative smi by
1128 // -1. We cannot use the overflow flag, since it is not set by idiv
1129 // instruction.
1130 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1131 __ cmp(eax, 0x40000000);
1132 __ j(equal, &use_fp_on_smis);
1133 // Check for negative zero result. Use combined = left | right.
1134 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1135 // Check that the remainder is zero.
1136 __ test(edx, Operand(edx));
1137 __ j(not_zero, &use_fp_on_smis);
1138 // Tag the result and store it in register eax.
1139 __ SmiTag(eax);
1140 break;
1141
1142 case Token::MOD:
1143 // Check for 0 divisor.
1144 __ test(right, Operand(right));
1145 __ j(zero, &not_smis, not_taken);
1146
1147 // Sign extend left into edx:eax.
1148 ASSERT(left.is(eax));
1149 __ cdq();
1150 // Divide edx:eax by right.
1151 __ idiv(right);
1152 // Check for negative zero result. Use combined = left | right.
1153 __ NegativeZeroTest(edx, combined, slow);
1154 // Move remainder to register eax.
1155 __ mov(eax, edx);
1156 break;
1157
1158 default:
1159 UNREACHABLE();
1160 }
1161
1162 // 5. Emit return of result in eax. Some operations have registers pushed.
1163 switch (op_) {
1164 case Token::ADD:
1165 case Token::SUB:
1166 case Token::MUL:
1167 case Token::DIV:
1168 __ ret(0);
1169 break;
1170 case Token::MOD:
1171 case Token::BIT_OR:
1172 case Token::BIT_AND:
1173 case Token::BIT_XOR:
1174 case Token::SAR:
1175 case Token::SHL:
1176 case Token::SHR:
1177 __ ret(2 * kPointerSize);
1178 break;
1179 default:
1180 UNREACHABLE();
1181 }
1182
1183 // 6. For some operations emit inline code to perform floating point
1184 // operations on known smis (e.g., if the result of the operation
1185 // overflowed the smi range).
1186 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1187 __ bind(&use_fp_on_smis);
1188 switch (op_) {
1189 // Undo the effects of some operations, and some register moves.
1190 case Token::SHL:
1191 // The arguments are saved on the stack, and only used from there.
1192 break;
1193 case Token::ADD:
1194 // Revert right = right + left.
1195 __ sub(right, Operand(left));
1196 break;
1197 case Token::SUB:
1198 // Revert left = left - right.
1199 __ add(left, Operand(right));
1200 break;
1201 case Token::MUL:
1202 // Right was clobbered but a copy is in ebx.
1203 __ mov(right, ebx);
1204 break;
1205 case Token::DIV:
1206 // Left was clobbered but a copy is in edi. Right is in ebx for
1207 // division. They should be in eax, ebx for jump to not_smi.
1208 __ mov(eax, edi);
1209 break;
1210 default:
1211 // No other operators jump to use_fp_on_smis.
1212 break;
1213 }
1214 __ jmp(&not_smis);
1215 } else {
1216 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1217 switch (op_) {
1218 case Token::SHL: {
1219 Comment perform_float(masm, "-- Perform float operation on smis");
1220 __ bind(&use_fp_on_smis);
1221 // Result we want is in left == edx, so we can put the allocated heap
1222 // number in eax.
1223 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1224 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001225 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001226 CpuFeatures::Scope use_sse2(SSE2);
1227 __ cvtsi2sd(xmm0, Operand(left));
1228 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1229 } else {
1230 // It's OK to overwrite the right argument on the stack because we
1231 // are about to return.
1232 __ mov(Operand(esp, 1 * kPointerSize), left);
1233 __ fild_s(Operand(esp, 1 * kPointerSize));
1234 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1235 }
1236 __ ret(2 * kPointerSize);
1237 break;
1238 }
1239
1240 case Token::ADD:
1241 case Token::SUB:
1242 case Token::MUL:
1243 case Token::DIV: {
1244 Comment perform_float(masm, "-- Perform float operation on smis");
1245 __ bind(&use_fp_on_smis);
1246 // Restore arguments to edx, eax.
1247 switch (op_) {
1248 case Token::ADD:
1249 // Revert right = right + left.
1250 __ sub(right, Operand(left));
1251 break;
1252 case Token::SUB:
1253 // Revert left = left - right.
1254 __ add(left, Operand(right));
1255 break;
1256 case Token::MUL:
1257 // Right was clobbered but a copy is in ebx.
1258 __ mov(right, ebx);
1259 break;
1260 case Token::DIV:
1261 // Left was clobbered but a copy is in edi. Right is in ebx for
1262 // division.
1263 __ mov(edx, edi);
1264 __ mov(eax, right);
1265 break;
1266 default: UNREACHABLE();
1267 break;
1268 }
1269 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001270 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001271 CpuFeatures::Scope use_sse2(SSE2);
1272 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1273 switch (op_) {
1274 case Token::ADD: __ addsd(xmm0, xmm1); break;
1275 case Token::SUB: __ subsd(xmm0, xmm1); break;
1276 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1277 case Token::DIV: __ divsd(xmm0, xmm1); break;
1278 default: UNREACHABLE();
1279 }
1280 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1281 } else { // SSE2 not available, use FPU.
1282 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1283 switch (op_) {
1284 case Token::ADD: __ faddp(1); break;
1285 case Token::SUB: __ fsubp(1); break;
1286 case Token::MUL: __ fmulp(1); break;
1287 case Token::DIV: __ fdivp(1); break;
1288 default: UNREACHABLE();
1289 }
1290 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1291 }
1292 __ mov(eax, ecx);
1293 __ ret(0);
1294 break;
1295 }
1296
1297 default:
1298 break;
1299 }
1300 }
1301
1302 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1303 // edx and eax.
1304 Comment done_comment(masm, "-- Enter non-smi code");
1305 __ bind(&not_smis);
1306 switch (op_) {
1307 case Token::BIT_OR:
1308 case Token::SHL:
1309 case Token::SAR:
1310 case Token::SHR:
1311 // Right operand is saved in ecx and eax was destroyed by the smi
1312 // check.
1313 __ mov(eax, ecx);
1314 break;
1315
1316 case Token::DIV:
1317 case Token::MOD:
1318 // Operands are in eax, ebx at this point.
1319 __ mov(edx, eax);
1320 __ mov(eax, ebx);
1321 break;
1322
1323 default:
1324 break;
1325 }
1326}
1327
1328
1329void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1330 Label call_runtime;
1331
1332 switch (op_) {
1333 case Token::ADD:
1334 case Token::SUB:
1335 case Token::MUL:
1336 case Token::DIV:
1337 break;
1338 case Token::MOD:
1339 case Token::BIT_OR:
1340 case Token::BIT_AND:
1341 case Token::BIT_XOR:
1342 case Token::SAR:
1343 case Token::SHL:
1344 case Token::SHR:
1345 GenerateRegisterArgsPush(masm);
1346 break;
1347 default:
1348 UNREACHABLE();
1349 }
1350
1351 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1352 result_type_ == TRBinaryOpIC::SMI) {
1353 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1354 } else {
1355 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1356 }
1357 __ bind(&call_runtime);
1358 switch (op_) {
1359 case Token::ADD:
1360 case Token::SUB:
1361 case Token::MUL:
1362 case Token::DIV:
1363 GenerateTypeTransition(masm);
1364 break;
1365 case Token::MOD:
1366 case Token::BIT_OR:
1367 case Token::BIT_AND:
1368 case Token::BIT_XOR:
1369 case Token::SAR:
1370 case Token::SHL:
1371 case Token::SHR:
1372 GenerateTypeTransitionWithSavedArgs(masm);
1373 break;
1374 default:
1375 UNREACHABLE();
1376 }
1377}
1378
1379
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001380void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001381 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1382 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001383 // Try to add arguments as strings, otherwise, transition to the generic
1384 // TRBinaryOpIC type.
1385 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001386 GenerateTypeTransition(masm);
1387}
1388
1389
danno@chromium.org160a7b02011-04-18 15:51:38 +00001390void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1391 Label call_runtime;
1392 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
1393 ASSERT(op_ == Token::ADD);
1394 // If both arguments are strings, call the string add stub.
1395 // Otherwise, do a transition.
1396
1397 // Registers containing left and right operands respectively.
1398 Register left = edx;
1399 Register right = eax;
1400
1401 // Test if left operand is a string.
1402 __ test(left, Immediate(kSmiTagMask));
1403 __ j(zero, &call_runtime);
1404 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1405 __ j(above_equal, &call_runtime);
1406
1407 // Test if right operand is a string.
1408 __ test(right, Immediate(kSmiTagMask));
1409 __ j(zero, &call_runtime);
1410 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1411 __ j(above_equal, &call_runtime);
1412
1413 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1414 GenerateRegisterArgsPush(masm);
1415 __ TailCallStub(&string_add_stub);
1416
1417 __ bind(&call_runtime);
1418 GenerateTypeTransition(masm);
1419}
1420
1421
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001422void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1423 Label call_runtime;
1424 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1425
1426 // Floating point case.
1427 switch (op_) {
1428 case Token::ADD:
1429 case Token::SUB:
1430 case Token::MUL:
1431 case Token::DIV: {
1432 Label not_floats;
1433 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001434 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001435 CpuFeatures::Scope use_sse2(SSE2);
1436 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1437 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1438 switch (op_) {
1439 case Token::ADD: __ addsd(xmm0, xmm1); break;
1440 case Token::SUB: __ subsd(xmm0, xmm1); break;
1441 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1442 case Token::DIV: __ divsd(xmm0, xmm1); break;
1443 default: UNREACHABLE();
1444 }
1445 // Check result type if it is currently Int32.
1446 if (result_type_ <= TRBinaryOpIC::INT32) {
1447 __ cvttsd2si(ecx, Operand(xmm0));
1448 __ cvtsi2sd(xmm2, Operand(ecx));
1449 __ ucomisd(xmm0, xmm2);
1450 __ j(not_zero, &not_int32);
1451 __ j(carry, &not_int32);
1452 }
1453 GenerateHeapResultAllocation(masm, &call_runtime);
1454 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1455 __ ret(0);
1456 } else { // SSE2 not available, use FPU.
1457 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1458 FloatingPointHelper::LoadFloatOperands(
1459 masm,
1460 ecx,
1461 FloatingPointHelper::ARGS_IN_REGISTERS);
1462 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1463 switch (op_) {
1464 case Token::ADD: __ faddp(1); break;
1465 case Token::SUB: __ fsubp(1); break;
1466 case Token::MUL: __ fmulp(1); break;
1467 case Token::DIV: __ fdivp(1); break;
1468 default: UNREACHABLE();
1469 }
1470 Label after_alloc_failure;
1471 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1472 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1473 __ ret(0);
1474 __ bind(&after_alloc_failure);
1475 __ ffree();
1476 __ jmp(&call_runtime);
1477 }
1478
1479 __ bind(&not_floats);
1480 __ bind(&not_int32);
1481 GenerateTypeTransition(masm);
1482 break;
1483 }
1484
1485 case Token::MOD: {
1486 // For MOD we go directly to runtime in the non-smi case.
1487 break;
1488 }
1489 case Token::BIT_OR:
1490 case Token::BIT_AND:
1491 case Token::BIT_XOR:
1492 case Token::SAR:
1493 case Token::SHL:
1494 case Token::SHR: {
1495 GenerateRegisterArgsPush(masm);
1496 Label not_floats;
1497 Label not_int32;
1498 Label non_smi_result;
1499 /* {
1500 CpuFeatures::Scope use_sse2(SSE2);
1501 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1502 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1503 }*/
1504 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1505 use_sse3_,
1506 &not_floats);
1507 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1508 &not_int32);
1509 switch (op_) {
1510 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1511 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1512 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1513 case Token::SAR: __ sar_cl(eax); break;
1514 case Token::SHL: __ shl_cl(eax); break;
1515 case Token::SHR: __ shr_cl(eax); break;
1516 default: UNREACHABLE();
1517 }
1518 if (op_ == Token::SHR) {
1519 // Check if result is non-negative and fits in a smi.
1520 __ test(eax, Immediate(0xc0000000));
1521 __ j(not_zero, &call_runtime);
1522 } else {
1523 // Check if result fits in a smi.
1524 __ cmp(eax, 0xc0000000);
1525 __ j(negative, &non_smi_result);
1526 }
1527 // Tag smi result and return.
1528 __ SmiTag(eax);
1529 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1530
1531 // All ops except SHR return a signed int32 that we load in
1532 // a HeapNumber.
1533 if (op_ != Token::SHR) {
1534 __ bind(&non_smi_result);
1535 // Allocate a heap number if needed.
1536 __ mov(ebx, Operand(eax)); // ebx: result
1537 NearLabel skip_allocation;
1538 switch (mode_) {
1539 case OVERWRITE_LEFT:
1540 case OVERWRITE_RIGHT:
1541 // If the operand was an object, we skip the
1542 // allocation of a heap number.
1543 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1544 1 * kPointerSize : 2 * kPointerSize));
1545 __ test(eax, Immediate(kSmiTagMask));
1546 __ j(not_zero, &skip_allocation, not_taken);
1547 // Fall through!
1548 case NO_OVERWRITE:
1549 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1550 __ bind(&skip_allocation);
1551 break;
1552 default: UNREACHABLE();
1553 }
1554 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001555 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001556 CpuFeatures::Scope use_sse2(SSE2);
1557 __ cvtsi2sd(xmm0, Operand(ebx));
1558 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1559 } else {
1560 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1561 __ fild_s(Operand(esp, 1 * kPointerSize));
1562 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1563 }
1564 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1565 }
1566
1567 __ bind(&not_floats);
1568 __ bind(&not_int32);
1569 GenerateTypeTransitionWithSavedArgs(masm);
1570 break;
1571 }
1572 default: UNREACHABLE(); break;
1573 }
1574
1575 // If an allocation fails, or SHR or MOD hit a hard case,
1576 // use the runtime system to get the correct result.
1577 __ bind(&call_runtime);
1578
1579 switch (op_) {
1580 case Token::ADD:
1581 GenerateRegisterArgsPush(masm);
1582 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1583 break;
1584 case Token::SUB:
1585 GenerateRegisterArgsPush(masm);
1586 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1587 break;
1588 case Token::MUL:
1589 GenerateRegisterArgsPush(masm);
1590 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1591 break;
1592 case Token::DIV:
1593 GenerateRegisterArgsPush(masm);
1594 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1595 break;
1596 case Token::MOD:
1597 GenerateRegisterArgsPush(masm);
1598 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1599 break;
1600 case Token::BIT_OR:
1601 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1602 break;
1603 case Token::BIT_AND:
1604 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1605 break;
1606 case Token::BIT_XOR:
1607 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1608 break;
1609 case Token::SAR:
1610 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1611 break;
1612 case Token::SHL:
1613 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1614 break;
1615 case Token::SHR:
1616 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1617 break;
1618 default:
1619 UNREACHABLE();
1620 }
1621}
1622
1623
lrn@chromium.org7516f052011-03-30 08:52:27 +00001624void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
1625 Label call_runtime;
1626
1627 if (op_ == Token::ADD) {
1628 // Handle string addition here, because it is the only operation
1629 // that does not do a ToNumber conversion on the operands.
1630 GenerateAddStrings(masm);
1631 }
1632
danno@chromium.org160a7b02011-04-18 15:51:38 +00001633 Factory* factory = masm->isolate()->factory();
1634
lrn@chromium.org7516f052011-03-30 08:52:27 +00001635 // Convert odd ball arguments to numbers.
1636 NearLabel check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001637 __ cmp(edx, factory->undefined_value());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001638 __ j(not_equal, &check);
1639 if (Token::IsBitOp(op_)) {
1640 __ xor_(edx, Operand(edx));
1641 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001642 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001643 }
1644 __ jmp(&done);
1645 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001646 __ cmp(eax, factory->undefined_value());
lrn@chromium.org7516f052011-03-30 08:52:27 +00001647 __ j(not_equal, &done);
1648 if (Token::IsBitOp(op_)) {
1649 __ xor_(eax, Operand(eax));
1650 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001651 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001652 }
1653 __ bind(&done);
1654
1655 GenerateHeapNumberStub(masm);
1656}
1657
1658
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001659void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1660 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001661
1662 // Floating point case.
1663 switch (op_) {
1664 case Token::ADD:
1665 case Token::SUB:
1666 case Token::MUL:
1667 case Token::DIV: {
1668 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001669 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001670 CpuFeatures::Scope use_sse2(SSE2);
1671 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1672
1673 switch (op_) {
1674 case Token::ADD: __ addsd(xmm0, xmm1); break;
1675 case Token::SUB: __ subsd(xmm0, xmm1); break;
1676 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1677 case Token::DIV: __ divsd(xmm0, xmm1); break;
1678 default: UNREACHABLE();
1679 }
1680 GenerateHeapResultAllocation(masm, &call_runtime);
1681 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1682 __ ret(0);
1683 } else { // SSE2 not available, use FPU.
1684 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1685 FloatingPointHelper::LoadFloatOperands(
1686 masm,
1687 ecx,
1688 FloatingPointHelper::ARGS_IN_REGISTERS);
1689 switch (op_) {
1690 case Token::ADD: __ faddp(1); break;
1691 case Token::SUB: __ fsubp(1); break;
1692 case Token::MUL: __ fmulp(1); break;
1693 case Token::DIV: __ fdivp(1); break;
1694 default: UNREACHABLE();
1695 }
1696 Label after_alloc_failure;
1697 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1698 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1699 __ ret(0);
1700 __ bind(&after_alloc_failure);
1701 __ ffree();
1702 __ jmp(&call_runtime);
1703 }
1704
1705 __ bind(&not_floats);
1706 GenerateTypeTransition(masm);
1707 break;
1708 }
1709
1710 case Token::MOD: {
1711 // For MOD we go directly to runtime in the non-smi case.
1712 break;
1713 }
1714 case Token::BIT_OR:
1715 case Token::BIT_AND:
1716 case Token::BIT_XOR:
1717 case Token::SAR:
1718 case Token::SHL:
1719 case Token::SHR: {
1720 GenerateRegisterArgsPush(masm);
1721 Label not_floats;
1722 Label non_smi_result;
1723 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1724 use_sse3_,
1725 &not_floats);
1726 switch (op_) {
1727 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1728 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1729 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1730 case Token::SAR: __ sar_cl(eax); break;
1731 case Token::SHL: __ shl_cl(eax); break;
1732 case Token::SHR: __ shr_cl(eax); break;
1733 default: UNREACHABLE();
1734 }
1735 if (op_ == Token::SHR) {
1736 // Check if result is non-negative and fits in a smi.
1737 __ test(eax, Immediate(0xc0000000));
1738 __ j(not_zero, &call_runtime);
1739 } else {
1740 // Check if result fits in a smi.
1741 __ cmp(eax, 0xc0000000);
1742 __ j(negative, &non_smi_result);
1743 }
1744 // Tag smi result and return.
1745 __ SmiTag(eax);
1746 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1747
1748 // All ops except SHR return a signed int32 that we load in
1749 // a HeapNumber.
1750 if (op_ != Token::SHR) {
1751 __ bind(&non_smi_result);
1752 // Allocate a heap number if needed.
1753 __ mov(ebx, Operand(eax)); // ebx: result
1754 NearLabel skip_allocation;
1755 switch (mode_) {
1756 case OVERWRITE_LEFT:
1757 case OVERWRITE_RIGHT:
1758 // If the operand was an object, we skip the
1759 // allocation of a heap number.
1760 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1761 1 * kPointerSize : 2 * kPointerSize));
1762 __ test(eax, Immediate(kSmiTagMask));
1763 __ j(not_zero, &skip_allocation, not_taken);
1764 // Fall through!
1765 case NO_OVERWRITE:
1766 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1767 __ bind(&skip_allocation);
1768 break;
1769 default: UNREACHABLE();
1770 }
1771 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001772 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001773 CpuFeatures::Scope use_sse2(SSE2);
1774 __ cvtsi2sd(xmm0, Operand(ebx));
1775 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1776 } else {
1777 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1778 __ fild_s(Operand(esp, 1 * kPointerSize));
1779 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1780 }
1781 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1782 }
1783
1784 __ bind(&not_floats);
1785 GenerateTypeTransitionWithSavedArgs(masm);
1786 break;
1787 }
1788 default: UNREACHABLE(); break;
1789 }
1790
1791 // If an allocation fails, or SHR or MOD hit a hard case,
1792 // use the runtime system to get the correct result.
1793 __ bind(&call_runtime);
1794
1795 switch (op_) {
1796 case Token::ADD:
1797 GenerateRegisterArgsPush(masm);
1798 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1799 break;
1800 case Token::SUB:
1801 GenerateRegisterArgsPush(masm);
1802 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1803 break;
1804 case Token::MUL:
1805 GenerateRegisterArgsPush(masm);
1806 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1807 break;
1808 case Token::DIV:
1809 GenerateRegisterArgsPush(masm);
1810 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1811 break;
1812 case Token::MOD:
1813 GenerateRegisterArgsPush(masm);
1814 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1815 break;
1816 case Token::BIT_OR:
1817 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1818 break;
1819 case Token::BIT_AND:
1820 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1821 break;
1822 case Token::BIT_XOR:
1823 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1824 break;
1825 case Token::SAR:
1826 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1827 break;
1828 case Token::SHL:
1829 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1830 break;
1831 case Token::SHR:
1832 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1833 break;
1834 default:
1835 UNREACHABLE();
1836 }
1837}
1838
1839
1840void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
1841 Label call_runtime;
1842
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001843 Counters* counters = masm->isolate()->counters();
1844 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001845
1846 switch (op_) {
1847 case Token::ADD:
1848 case Token::SUB:
1849 case Token::MUL:
1850 case Token::DIV:
1851 break;
1852 case Token::MOD:
1853 case Token::BIT_OR:
1854 case Token::BIT_AND:
1855 case Token::BIT_XOR:
1856 case Token::SAR:
1857 case Token::SHL:
1858 case Token::SHR:
1859 GenerateRegisterArgsPush(masm);
1860 break;
1861 default:
1862 UNREACHABLE();
1863 }
1864
1865 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1866
1867 // Floating point case.
1868 switch (op_) {
1869 case Token::ADD:
1870 case Token::SUB:
1871 case Token::MUL:
1872 case Token::DIV: {
1873 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001874 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001875 CpuFeatures::Scope use_sse2(SSE2);
1876 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1877
1878 switch (op_) {
1879 case Token::ADD: __ addsd(xmm0, xmm1); break;
1880 case Token::SUB: __ subsd(xmm0, xmm1); break;
1881 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1882 case Token::DIV: __ divsd(xmm0, xmm1); break;
1883 default: UNREACHABLE();
1884 }
1885 GenerateHeapResultAllocation(masm, &call_runtime);
1886 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1887 __ ret(0);
1888 } else { // SSE2 not available, use FPU.
1889 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1890 FloatingPointHelper::LoadFloatOperands(
1891 masm,
1892 ecx,
1893 FloatingPointHelper::ARGS_IN_REGISTERS);
1894 switch (op_) {
1895 case Token::ADD: __ faddp(1); break;
1896 case Token::SUB: __ fsubp(1); break;
1897 case Token::MUL: __ fmulp(1); break;
1898 case Token::DIV: __ fdivp(1); break;
1899 default: UNREACHABLE();
1900 }
1901 Label after_alloc_failure;
1902 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1903 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1904 __ ret(0);
1905 __ bind(&after_alloc_failure);
1906 __ ffree();
1907 __ jmp(&call_runtime);
1908 }
1909 __ bind(&not_floats);
1910 break;
1911 }
1912 case Token::MOD: {
1913 // For MOD we go directly to runtime in the non-smi case.
1914 break;
1915 }
1916 case Token::BIT_OR:
1917 case Token::BIT_AND:
1918 case Token::BIT_XOR:
1919 case Token::SAR:
1920 case Token::SHL:
1921 case Token::SHR: {
1922 Label non_smi_result;
1923 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1924 use_sse3_,
1925 &call_runtime);
1926 switch (op_) {
1927 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1928 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1929 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1930 case Token::SAR: __ sar_cl(eax); break;
1931 case Token::SHL: __ shl_cl(eax); break;
1932 case Token::SHR: __ shr_cl(eax); break;
1933 default: UNREACHABLE();
1934 }
1935 if (op_ == Token::SHR) {
1936 // Check if result is non-negative and fits in a smi.
1937 __ test(eax, Immediate(0xc0000000));
1938 __ j(not_zero, &call_runtime);
1939 } else {
1940 // Check if result fits in a smi.
1941 __ cmp(eax, 0xc0000000);
1942 __ j(negative, &non_smi_result);
1943 }
1944 // Tag smi result and return.
1945 __ SmiTag(eax);
1946 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1947
1948 // All ops except SHR return a signed int32 that we load in
1949 // a HeapNumber.
1950 if (op_ != Token::SHR) {
1951 __ bind(&non_smi_result);
1952 // Allocate a heap number if needed.
1953 __ mov(ebx, Operand(eax)); // ebx: result
1954 NearLabel skip_allocation;
1955 switch (mode_) {
1956 case OVERWRITE_LEFT:
1957 case OVERWRITE_RIGHT:
1958 // If the operand was an object, we skip the
1959 // allocation of a heap number.
1960 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1961 1 * kPointerSize : 2 * kPointerSize));
1962 __ test(eax, Immediate(kSmiTagMask));
1963 __ j(not_zero, &skip_allocation, not_taken);
1964 // Fall through!
1965 case NO_OVERWRITE:
1966 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1967 __ bind(&skip_allocation);
1968 break;
1969 default: UNREACHABLE();
1970 }
1971 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001972 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001973 CpuFeatures::Scope use_sse2(SSE2);
1974 __ cvtsi2sd(xmm0, Operand(ebx));
1975 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1976 } else {
1977 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1978 __ fild_s(Operand(esp, 1 * kPointerSize));
1979 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1980 }
1981 __ ret(2 * kPointerSize);
1982 }
1983 break;
1984 }
1985 default: UNREACHABLE(); break;
1986 }
1987
1988 // If all else fails, use the runtime system to get the correct
1989 // result.
1990 __ bind(&call_runtime);
1991 switch (op_) {
1992 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001993 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001994 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001995 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1996 break;
1997 }
1998 case Token::SUB:
1999 GenerateRegisterArgsPush(masm);
2000 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2001 break;
2002 case Token::MUL:
2003 GenerateRegisterArgsPush(masm);
2004 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2005 break;
2006 case Token::DIV:
2007 GenerateRegisterArgsPush(masm);
2008 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2009 break;
2010 case Token::MOD:
2011 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2012 break;
2013 case Token::BIT_OR:
2014 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2015 break;
2016 case Token::BIT_AND:
2017 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2018 break;
2019 case Token::BIT_XOR:
2020 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2021 break;
2022 case Token::SAR:
2023 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2024 break;
2025 case Token::SHL:
2026 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2027 break;
2028 case Token::SHR:
2029 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2030 break;
2031 default:
2032 UNREACHABLE();
2033 }
2034}
2035
2036
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002037void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002038 ASSERT(op_ == Token::ADD);
2039 NearLabel left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002040
2041 // Registers containing left and right operands respectively.
2042 Register left = edx;
2043 Register right = eax;
2044
2045 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002046 __ test(left, Immediate(kSmiTagMask));
2047 __ j(zero, &left_not_string);
2048 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
2049 __ j(above_equal, &left_not_string);
2050
2051 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2052 GenerateRegisterArgsPush(masm);
2053 __ TailCallStub(&string_add_left_stub);
2054
2055 // Left operand is not a string, test right.
2056 __ bind(&left_not_string);
2057 __ test(right, Immediate(kSmiTagMask));
2058 __ j(zero, &call_runtime);
2059 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
2060 __ j(above_equal, &call_runtime);
2061
2062 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2063 GenerateRegisterArgsPush(masm);
2064 __ TailCallStub(&string_add_right_stub);
2065
2066 // Neither argument is a string.
2067 __ bind(&call_runtime);
2068}
2069
2070
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002071void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2072 MacroAssembler* masm,
2073 Label* alloc_failure) {
2074 Label skip_allocation;
2075 OverwriteMode mode = mode_;
2076 switch (mode) {
2077 case OVERWRITE_LEFT: {
2078 // If the argument in edx is already an object, we skip the
2079 // allocation of a heap number.
2080 __ test(edx, Immediate(kSmiTagMask));
2081 __ j(not_zero, &skip_allocation, not_taken);
2082 // Allocate a heap number for the result. Keep eax and edx intact
2083 // for the possible runtime call.
2084 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2085 // Now edx can be overwritten losing one of the arguments as we are
2086 // now done and will not need it any more.
2087 __ mov(edx, Operand(ebx));
2088 __ bind(&skip_allocation);
2089 // Use object in edx as a result holder
2090 __ mov(eax, Operand(edx));
2091 break;
2092 }
2093 case OVERWRITE_RIGHT:
2094 // If the argument in eax is already an object, we skip the
2095 // allocation of a heap number.
2096 __ test(eax, Immediate(kSmiTagMask));
2097 __ j(not_zero, &skip_allocation, not_taken);
2098 // Fall through!
2099 case NO_OVERWRITE:
2100 // Allocate a heap number for the result. Keep eax and edx intact
2101 // for the possible runtime call.
2102 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2103 // Now eax can be overwritten losing one of the arguments as we are
2104 // now done and will not need it any more.
2105 __ mov(eax, ebx);
2106 __ bind(&skip_allocation);
2107 break;
2108 default: UNREACHABLE();
2109 }
2110}
2111
2112
2113void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2114 __ pop(ecx);
2115 __ push(edx);
2116 __ push(eax);
2117 __ push(ecx);
2118}
2119
2120
ricow@chromium.org65fae842010-08-25 15:26:24 +00002121void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002122 // TAGGED case:
2123 // Input:
2124 // esp[4]: tagged number input argument (should be number).
2125 // esp[0]: return address.
2126 // Output:
2127 // eax: tagged double result.
2128 // UNTAGGED case:
2129 // Input::
2130 // esp[0]: return address.
2131 // xmm1: untagged double input argument
2132 // Output:
2133 // xmm1: untagged double result.
2134
ricow@chromium.org65fae842010-08-25 15:26:24 +00002135 Label runtime_call;
2136 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002137 Label skip_cache;
2138 const bool tagged = (argument_type_ == TAGGED);
2139 if (tagged) {
2140 // Test that eax is a number.
2141 NearLabel input_not_smi;
2142 NearLabel loaded;
2143 __ mov(eax, Operand(esp, kPointerSize));
2144 __ test(eax, Immediate(kSmiTagMask));
2145 __ j(not_zero, &input_not_smi);
2146 // Input is a smi. Untag and load it onto the FPU stack.
2147 // Then load the low and high words of the double into ebx, edx.
2148 STATIC_ASSERT(kSmiTagSize == 1);
2149 __ sar(eax, 1);
2150 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2151 __ mov(Operand(esp, 0), eax);
2152 __ fild_s(Operand(esp, 0));
2153 __ fst_d(Operand(esp, 0));
2154 __ pop(edx);
2155 __ pop(ebx);
2156 __ jmp(&loaded);
2157 __ bind(&input_not_smi);
2158 // Check if input is a HeapNumber.
2159 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002160 Factory* factory = masm->isolate()->factory();
2161 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002162 __ j(not_equal, &runtime_call);
2163 // Input is a HeapNumber. Push it on the FPU stack and load its
2164 // low and high words into ebx, edx.
2165 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2166 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2167 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002168
whesse@chromium.org023421e2010-12-21 12:19:12 +00002169 __ bind(&loaded);
2170 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002171 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002172 CpuFeatures::Scope sse4_scope(SSE4_1);
2173 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2174 } else {
2175 __ pshufd(xmm0, xmm1, 0x1);
2176 __ movd(Operand(edx), xmm0);
2177 }
2178 __ movd(Operand(ebx), xmm1);
2179 }
2180
2181 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002182 // ebx = low 32 bits of double value
2183 // edx = high 32 bits of double value
2184 // Compute hash (the shifts are arithmetic):
2185 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2186 __ mov(ecx, ebx);
2187 __ xor_(ecx, Operand(edx));
2188 __ mov(eax, ecx);
2189 __ sar(eax, 16);
2190 __ xor_(ecx, Operand(eax));
2191 __ mov(eax, ecx);
2192 __ sar(eax, 8);
2193 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002194 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2195 __ and_(Operand(ecx),
2196 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002197
whesse@chromium.org023421e2010-12-21 12:19:12 +00002198 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002199 // ebx = low 32 bits of double value.
2200 // edx = high 32 bits of double value.
2201 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002202 ExternalReference cache_array =
2203 ExternalReference::transcendental_cache_array_address(masm->isolate());
2204 __ mov(eax, Immediate(cache_array));
2205 int cache_array_index =
2206 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2207 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002208 // Eax points to the cache for the type type_.
2209 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2210 __ test(eax, Operand(eax));
2211 __ j(zero, &runtime_call_clear_stack);
2212#ifdef DEBUG
2213 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002214 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002215 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2216 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2217 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2218 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2219 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2220 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2221 CHECK_EQ(0, elem_in0 - elem_start);
2222 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2223 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2224 }
2225#endif
2226 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2227 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2228 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2229 // Check if cache matches: Double value is stored in uint32_t[2] array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002230 NearLabel cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002231 __ cmp(ebx, Operand(ecx, 0));
2232 __ j(not_equal, &cache_miss);
2233 __ cmp(edx, Operand(ecx, kIntSize));
2234 __ j(not_equal, &cache_miss);
2235 // Cache hit!
2236 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002237 if (tagged) {
2238 __ fstp(0);
2239 __ ret(kPointerSize);
2240 } else { // UNTAGGED.
2241 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2242 __ Ret();
2243 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002244
2245 __ bind(&cache_miss);
2246 // Update cache with new value.
2247 // We are short on registers, so use no_reg as scratch.
2248 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002249 if (tagged) {
2250 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2251 } else { // UNTAGGED.
2252 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2253 __ sub(Operand(esp), Immediate(kDoubleSize));
2254 __ movdbl(Operand(esp, 0), xmm1);
2255 __ fld_d(Operand(esp, 0));
2256 __ add(Operand(esp), Immediate(kDoubleSize));
2257 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002258 GenerateOperation(masm);
2259 __ mov(Operand(ecx, 0), ebx);
2260 __ mov(Operand(ecx, kIntSize), edx);
2261 __ mov(Operand(ecx, 2 * kIntSize), eax);
2262 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002263 if (tagged) {
2264 __ ret(kPointerSize);
2265 } else { // UNTAGGED.
2266 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2267 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002268
whesse@chromium.org023421e2010-12-21 12:19:12 +00002269 // Skip cache and return answer directly, only in untagged case.
2270 __ bind(&skip_cache);
2271 __ sub(Operand(esp), Immediate(kDoubleSize));
2272 __ movdbl(Operand(esp, 0), xmm1);
2273 __ fld_d(Operand(esp, 0));
2274 GenerateOperation(masm);
2275 __ fstp_d(Operand(esp, 0));
2276 __ movdbl(xmm1, Operand(esp, 0));
2277 __ add(Operand(esp), Immediate(kDoubleSize));
2278 // We return the value in xmm1 without adding it to the cache, but
2279 // we cause a scavenging GC so that future allocations will succeed.
2280 __ EnterInternalFrame();
2281 // Allocate an unused object bigger than a HeapNumber.
2282 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2283 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2284 __ LeaveInternalFrame();
2285 __ Ret();
2286 }
2287
2288 // Call runtime, doing whatever allocation and cleanup is necessary.
2289 if (tagged) {
2290 __ bind(&runtime_call_clear_stack);
2291 __ fstp(0);
2292 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002293 ExternalReference runtime =
2294 ExternalReference(RuntimeFunction(), masm->isolate());
2295 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002296 } else { // UNTAGGED.
2297 __ bind(&runtime_call_clear_stack);
2298 __ bind(&runtime_call);
2299 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2300 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2301 __ EnterInternalFrame();
2302 __ push(eax);
2303 __ CallRuntime(RuntimeFunction(), 1);
2304 __ LeaveInternalFrame();
2305 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2306 __ Ret();
2307 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002308}
2309
2310
2311Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2312 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002313 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2314 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002315 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002316 default:
2317 UNIMPLEMENTED();
2318 return Runtime::kAbort;
2319 }
2320}
2321
2322
2323void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2324 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002325 // Input value is on FP stack, and also in ebx/edx.
2326 // Input value is possibly in xmm1.
2327 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002328 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2329 // Both fsin and fcos require arguments in the range +/-2^63 and
2330 // return NaN for infinities and NaN. They can share all code except
2331 // the actual fsin/fcos operation.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002332 NearLabel in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002333 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2334 // work. We must reduce it to the appropriate range.
2335 __ mov(edi, edx);
2336 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2337 int supported_exponent_limit =
2338 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2339 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
2340 __ j(below, &in_range, taken);
2341 // Check for infinity and NaN. Both return NaN for sin.
2342 __ cmp(Operand(edi), Immediate(0x7ff00000));
2343 NearLabel non_nan_result;
2344 __ j(not_equal, &non_nan_result, taken);
2345 // Input is +/-Infinity or NaN. Result is NaN.
2346 __ fstp(0);
2347 // NaN is represented by 0x7ff8000000000000.
2348 __ push(Immediate(0x7ff80000));
2349 __ push(Immediate(0));
2350 __ fld_d(Operand(esp, 0));
2351 __ add(Operand(esp), Immediate(2 * kPointerSize));
2352 __ jmp(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002353
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002354 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002355
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002356 // Use fpmod to restrict argument to the range +/-2*PI.
2357 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2358 __ fldpi();
2359 __ fadd(0);
2360 __ fld(1);
2361 // FPU Stack: input, 2*pi, input.
2362 {
2363 NearLabel no_exceptions;
2364 __ fwait();
2365 __ fnstsw_ax();
2366 // Clear if Illegal Operand or Zero Division exceptions are set.
2367 __ test(Operand(eax), Immediate(5));
2368 __ j(zero, &no_exceptions);
2369 __ fnclex();
2370 __ bind(&no_exceptions);
2371 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002372
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002373 // Compute st(0) % st(1)
2374 {
2375 NearLabel partial_remainder_loop;
2376 __ bind(&partial_remainder_loop);
2377 __ fprem1();
2378 __ fwait();
2379 __ fnstsw_ax();
2380 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2381 // If C2 is set, computation only has partial result. Loop to
2382 // continue computation.
2383 __ j(not_zero, &partial_remainder_loop);
2384 }
2385 // FPU Stack: input, 2*pi, input % 2*pi
2386 __ fstp(2);
2387 __ fstp(0);
2388 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2389
2390 // FPU Stack: input % 2*pi
2391 __ bind(&in_range);
2392 switch (type_) {
2393 case TranscendentalCache::SIN:
2394 __ fsin();
2395 break;
2396 case TranscendentalCache::COS:
2397 __ fcos();
2398 break;
2399 default:
2400 UNREACHABLE();
2401 }
2402 __ bind(&done);
2403 } else {
2404 ASSERT(type_ == TranscendentalCache::LOG);
2405 __ fldln2();
2406 __ fxch();
2407 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002408 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002409}
2410
2411
ricow@chromium.org65fae842010-08-25 15:26:24 +00002412// Input: edx, eax are the left and right objects of a bit op.
2413// Output: eax, ecx are left and right integers for a bit op.
2414void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
2415 TypeInfo type_info,
2416 bool use_sse3,
2417 Label* conversion_failure) {
2418 // Check float operands.
2419 Label arg1_is_object, check_undefined_arg1;
2420 Label arg2_is_object, check_undefined_arg2;
2421 Label load_arg2, done;
2422
2423 if (!type_info.IsDouble()) {
2424 if (!type_info.IsSmi()) {
2425 __ test(edx, Immediate(kSmiTagMask));
2426 __ j(not_zero, &arg1_is_object);
2427 } else {
2428 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
2429 }
2430 __ SmiUntag(edx);
2431 __ jmp(&load_arg2);
2432 }
2433
2434 __ bind(&arg1_is_object);
2435
2436 // Get the untagged integer version of the edx heap number in ecx.
2437 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
2438 __ mov(edx, ecx);
2439
2440 // Here edx has the untagged integer, eax has a Smi or a heap number.
2441 __ bind(&load_arg2);
2442 if (!type_info.IsDouble()) {
2443 // Test if arg2 is a Smi.
2444 if (!type_info.IsSmi()) {
2445 __ test(eax, Immediate(kSmiTagMask));
2446 __ j(not_zero, &arg2_is_object);
2447 } else {
2448 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
2449 }
2450 __ SmiUntag(eax);
2451 __ mov(ecx, eax);
2452 __ jmp(&done);
2453 }
2454
2455 __ bind(&arg2_is_object);
2456
2457 // Get the untagged integer version of the eax heap number in ecx.
2458 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
2459 __ bind(&done);
2460 __ mov(eax, edx);
2461}
2462
2463
2464// Input: edx, eax are the left and right objects of a bit op.
2465// Output: eax, ecx are left and right integers for a bit op.
2466void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2467 bool use_sse3,
2468 Label* conversion_failure) {
2469 // Check float operands.
2470 Label arg1_is_object, check_undefined_arg1;
2471 Label arg2_is_object, check_undefined_arg2;
2472 Label load_arg2, done;
2473
2474 // Test if arg1 is a Smi.
2475 __ test(edx, Immediate(kSmiTagMask));
2476 __ j(not_zero, &arg1_is_object);
2477
2478 __ SmiUntag(edx);
2479 __ jmp(&load_arg2);
2480
2481 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2482 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002483 Factory* factory = masm->isolate()->factory();
2484 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002485 __ j(not_equal, conversion_failure);
2486 __ mov(edx, Immediate(0));
2487 __ jmp(&load_arg2);
2488
2489 __ bind(&arg1_is_object);
2490 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002491 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002492 __ j(not_equal, &check_undefined_arg1);
2493
2494 // Get the untagged integer version of the edx heap number in ecx.
2495 IntegerConvert(masm,
2496 edx,
2497 TypeInfo::Unknown(),
2498 use_sse3,
2499 conversion_failure);
2500 __ mov(edx, ecx);
2501
2502 // Here edx has the untagged integer, eax has a Smi or a heap number.
2503 __ bind(&load_arg2);
2504
2505 // Test if arg2 is a Smi.
2506 __ test(eax, Immediate(kSmiTagMask));
2507 __ j(not_zero, &arg2_is_object);
2508
2509 __ SmiUntag(eax);
2510 __ mov(ecx, eax);
2511 __ jmp(&done);
2512
2513 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2514 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002515 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002516 __ j(not_equal, conversion_failure);
2517 __ mov(ecx, Immediate(0));
2518 __ jmp(&done);
2519
2520 __ bind(&arg2_is_object);
2521 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002522 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002523 __ j(not_equal, &check_undefined_arg2);
2524
2525 // Get the untagged integer version of the eax heap number in ecx.
2526 IntegerConvert(masm,
2527 eax,
2528 TypeInfo::Unknown(),
2529 use_sse3,
2530 conversion_failure);
2531 __ bind(&done);
2532 __ mov(eax, edx);
2533}
2534
2535
2536void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
2537 TypeInfo type_info,
2538 bool use_sse3,
2539 Label* conversion_failure) {
2540 if (type_info.IsNumber()) {
2541 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
2542 } else {
2543 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
2544 }
2545}
2546
2547
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002548void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2549 bool use_sse3,
2550 Label* not_int32) {
2551 return;
2552}
2553
2554
ricow@chromium.org65fae842010-08-25 15:26:24 +00002555void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2556 Register number) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002557 NearLabel load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002558
2559 __ test(number, Immediate(kSmiTagMask));
2560 __ j(zero, &load_smi, not_taken);
2561 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
2562 __ jmp(&done);
2563
2564 __ bind(&load_smi);
2565 __ SmiUntag(number);
2566 __ push(number);
2567 __ fild_s(Operand(esp, 0));
2568 __ pop(number);
2569
2570 __ bind(&done);
2571}
2572
2573
2574void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002575 NearLabel load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002576 // Load operand in edx into xmm0.
2577 __ test(edx, Immediate(kSmiTagMask));
2578 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
2579 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2580
2581 __ bind(&load_eax);
2582 // Load operand in eax into xmm1.
2583 __ test(eax, Immediate(kSmiTagMask));
2584 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
2585 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2586 __ jmp(&done);
2587
2588 __ bind(&load_smi_edx);
2589 __ SmiUntag(edx); // Untag smi before converting to float.
2590 __ cvtsi2sd(xmm0, Operand(edx));
2591 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2592 __ jmp(&load_eax);
2593
2594 __ bind(&load_smi_eax);
2595 __ SmiUntag(eax); // Untag smi before converting to float.
2596 __ cvtsi2sd(xmm1, Operand(eax));
2597 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2598
2599 __ bind(&done);
2600}
2601
2602
2603void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2604 Label* not_numbers) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002605 NearLabel load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002606 // Load operand in edx into xmm0, or branch to not_numbers.
2607 __ test(edx, Immediate(kSmiTagMask));
2608 __ j(zero, &load_smi_edx, not_taken); // Argument in edx is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002609 Factory* factory = masm->isolate()->factory();
2610 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002611 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2612 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2613 __ bind(&load_eax);
2614 // Load operand in eax into xmm1, or branch to not_numbers.
2615 __ test(eax, Immediate(kSmiTagMask));
2616 __ j(zero, &load_smi_eax, not_taken); // Argument in eax is a smi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002617 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002618 __ j(equal, &load_float_eax);
2619 __ jmp(not_numbers); // Argument in eax is not a number.
2620 __ bind(&load_smi_edx);
2621 __ SmiUntag(edx); // Untag smi before converting to float.
2622 __ cvtsi2sd(xmm0, Operand(edx));
2623 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2624 __ jmp(&load_eax);
2625 __ bind(&load_smi_eax);
2626 __ SmiUntag(eax); // Untag smi before converting to float.
2627 __ cvtsi2sd(xmm1, Operand(eax));
2628 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2629 __ jmp(&done);
2630 __ bind(&load_float_eax);
2631 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2632 __ bind(&done);
2633}
2634
2635
2636void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2637 Register scratch) {
2638 const Register left = edx;
2639 const Register right = eax;
2640 __ mov(scratch, left);
2641 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2642 __ SmiUntag(scratch);
2643 __ cvtsi2sd(xmm0, Operand(scratch));
2644
2645 __ mov(scratch, right);
2646 __ SmiUntag(scratch);
2647 __ cvtsi2sd(xmm1, Operand(scratch));
2648}
2649
2650
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002651void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2652 Label* non_int32,
2653 Register scratch) {
2654 __ cvttsd2si(scratch, Operand(xmm0));
2655 __ cvtsi2sd(xmm2, Operand(scratch));
2656 __ ucomisd(xmm0, xmm2);
2657 __ j(not_zero, non_int32);
2658 __ j(carry, non_int32);
2659 __ cvttsd2si(scratch, Operand(xmm1));
2660 __ cvtsi2sd(xmm2, Operand(scratch));
2661 __ ucomisd(xmm1, xmm2);
2662 __ j(not_zero, non_int32);
2663 __ j(carry, non_int32);
2664}
2665
2666
ricow@chromium.org65fae842010-08-25 15:26:24 +00002667void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2668 Register scratch,
2669 ArgLocation arg_location) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002670 NearLabel load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002671 if (arg_location == ARGS_IN_REGISTERS) {
2672 __ mov(scratch, edx);
2673 } else {
2674 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2675 }
2676 __ test(scratch, Immediate(kSmiTagMask));
2677 __ j(zero, &load_smi_1, not_taken);
2678 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2679 __ bind(&done_load_1);
2680
2681 if (arg_location == ARGS_IN_REGISTERS) {
2682 __ mov(scratch, eax);
2683 } else {
2684 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2685 }
2686 __ test(scratch, Immediate(kSmiTagMask));
2687 __ j(zero, &load_smi_2, not_taken);
2688 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2689 __ jmp(&done);
2690
2691 __ bind(&load_smi_1);
2692 __ SmiUntag(scratch);
2693 __ push(scratch);
2694 __ fild_s(Operand(esp, 0));
2695 __ pop(scratch);
2696 __ jmp(&done_load_1);
2697
2698 __ bind(&load_smi_2);
2699 __ SmiUntag(scratch);
2700 __ push(scratch);
2701 __ fild_s(Operand(esp, 0));
2702 __ pop(scratch);
2703
2704 __ bind(&done);
2705}
2706
2707
2708void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2709 Register scratch) {
2710 const Register left = edx;
2711 const Register right = eax;
2712 __ mov(scratch, left);
2713 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2714 __ SmiUntag(scratch);
2715 __ push(scratch);
2716 __ fild_s(Operand(esp, 0));
2717
2718 __ mov(scratch, right);
2719 __ SmiUntag(scratch);
2720 __ mov(Operand(esp, 0), scratch);
2721 __ fild_s(Operand(esp, 0));
2722 __ pop(scratch);
2723}
2724
2725
2726void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2727 Label* non_float,
2728 Register scratch) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002729 NearLabel test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002730 // Test if both operands are floats or smi -> scratch=k_is_float;
2731 // Otherwise scratch = k_not_float.
2732 __ test(edx, Immediate(kSmiTagMask));
2733 __ j(zero, &test_other, not_taken); // argument in edx is OK
2734 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002735 Factory* factory = masm->isolate()->factory();
2736 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002737 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2738
2739 __ bind(&test_other);
2740 __ test(eax, Immediate(kSmiTagMask));
2741 __ j(zero, &done); // argument in eax is OK
2742 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002743 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002744 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2745
2746 // Fall-through: Both operands are numbers.
2747 __ bind(&done);
2748}
2749
2750
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002751void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2752 Label* non_int32) {
2753 return;
2754}
2755
2756
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002757void MathPowStub::Generate(MacroAssembler* masm) {
2758 // Registers are used as follows:
2759 // edx = base
2760 // eax = exponent
2761 // ecx = temporary, result
2762
2763 CpuFeatures::Scope use_sse2(SSE2);
2764 Label allocate_return, call_runtime;
2765
2766 // Load input parameters.
2767 __ mov(edx, Operand(esp, 2 * kPointerSize));
2768 __ mov(eax, Operand(esp, 1 * kPointerSize));
2769
2770 // Save 1 in xmm3 - we need this several times later on.
2771 __ mov(ecx, Immediate(1));
2772 __ cvtsi2sd(xmm3, Operand(ecx));
2773
2774 Label exponent_nonsmi;
2775 Label base_nonsmi;
2776 // If the exponent is a heap number go to that specific case.
2777 __ test(eax, Immediate(kSmiTagMask));
2778 __ j(not_zero, &exponent_nonsmi);
2779 __ test(edx, Immediate(kSmiTagMask));
2780 __ j(not_zero, &base_nonsmi);
2781
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002782 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002783 Label powi;
2784 __ SmiUntag(edx);
2785 __ cvtsi2sd(xmm0, Operand(edx));
2786 __ jmp(&powi);
2787 // exponent is smi and base is a heapnumber.
2788 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002789 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002790 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002791 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002792 __ j(not_equal, &call_runtime);
2793
2794 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2795
2796 // Optimized version of pow if exponent is a smi.
2797 // xmm0 contains the base.
2798 __ bind(&powi);
2799 __ SmiUntag(eax);
2800
2801 // Save exponent in base as we need to check if exponent is negative later.
2802 // We know that base and exponent are in different registers.
2803 __ mov(edx, eax);
2804
2805 // Get absolute value of exponent.
2806 NearLabel no_neg;
2807 __ cmp(eax, 0);
2808 __ j(greater_equal, &no_neg);
2809 __ neg(eax);
2810 __ bind(&no_neg);
2811
2812 // Load xmm1 with 1.
2813 __ movsd(xmm1, xmm3);
2814 NearLabel while_true;
2815 NearLabel no_multiply;
2816
2817 __ bind(&while_true);
2818 __ shr(eax, 1);
2819 __ j(not_carry, &no_multiply);
2820 __ mulsd(xmm1, xmm0);
2821 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002822 __ mulsd(xmm0, xmm0);
2823 __ j(not_zero, &while_true);
2824
2825 // base has the original value of the exponent - if the exponent is
2826 // negative return 1/result.
2827 __ test(edx, Operand(edx));
2828 __ j(positive, &allocate_return);
2829 // Special case if xmm1 has reached infinity.
2830 __ mov(ecx, Immediate(0x7FB00000));
2831 __ movd(xmm0, Operand(ecx));
2832 __ cvtss2sd(xmm0, xmm0);
2833 __ ucomisd(xmm0, xmm1);
2834 __ j(equal, &call_runtime);
2835 __ divsd(xmm3, xmm1);
2836 __ movsd(xmm1, xmm3);
2837 __ jmp(&allocate_return);
2838
2839 // exponent (or both) is a heapnumber - no matter what we should now work
2840 // on doubles.
2841 __ bind(&exponent_nonsmi);
2842 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002843 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002844 __ j(not_equal, &call_runtime);
2845 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2846 // Test if exponent is nan.
2847 __ ucomisd(xmm1, xmm1);
2848 __ j(parity_even, &call_runtime);
2849
2850 NearLabel base_not_smi;
2851 NearLabel handle_special_cases;
2852 __ test(edx, Immediate(kSmiTagMask));
2853 __ j(not_zero, &base_not_smi);
2854 __ SmiUntag(edx);
2855 __ cvtsi2sd(xmm0, Operand(edx));
2856 __ jmp(&handle_special_cases);
2857
2858 __ bind(&base_not_smi);
2859 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002860 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002861 __ j(not_equal, &call_runtime);
2862 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2863 __ and_(ecx, HeapNumber::kExponentMask);
2864 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2865 // base is NaN or +/-Infinity
2866 __ j(greater_equal, &call_runtime);
2867 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2868
2869 // base is in xmm0 and exponent is in xmm1.
2870 __ bind(&handle_special_cases);
2871 NearLabel not_minus_half;
2872 // Test for -0.5.
2873 // Load xmm2 with -0.5.
2874 __ mov(ecx, Immediate(0xBF000000));
2875 __ movd(xmm2, Operand(ecx));
2876 __ cvtss2sd(xmm2, xmm2);
2877 // xmm2 now has -0.5.
2878 __ ucomisd(xmm2, xmm1);
2879 __ j(not_equal, &not_minus_half);
2880
2881 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002882 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
2883 __ xorpd(xmm1, xmm1);
2884 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002885 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002886 __ divsd(xmm3, xmm1);
2887 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002888 __ jmp(&allocate_return);
2889
2890 // Test for 0.5.
2891 __ bind(&not_minus_half);
2892 // Load xmm2 with 0.5.
2893 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2894 __ addsd(xmm2, xmm3);
2895 // xmm2 now has 0.5.
2896 __ ucomisd(xmm2, xmm1);
2897 __ j(not_equal, &call_runtime);
2898 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002899 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
2900 __ xorpd(xmm1, xmm1);
2901 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002902 __ sqrtsd(xmm1, xmm1);
2903
2904 __ bind(&allocate_return);
2905 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2906 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2907 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002908 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002909
2910 __ bind(&call_runtime);
2911 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2912}
2913
2914
ricow@chromium.org65fae842010-08-25 15:26:24 +00002915void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2916 // The key is in edx and the parameter count is in eax.
2917
2918 // The displacement is used for skipping the frame pointer on the
2919 // stack. It is the offset of the last parameter (if any) relative
2920 // to the frame pointer.
2921 static const int kDisplacement = 1 * kPointerSize;
2922
2923 // Check that the key is a smi.
2924 Label slow;
2925 __ test(edx, Immediate(kSmiTagMask));
2926 __ j(not_zero, &slow, not_taken);
2927
2928 // Check if the calling frame is an arguments adaptor frame.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00002929 NearLabel adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002930 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2931 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2932 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2933 __ j(equal, &adaptor);
2934
2935 // Check index against formal parameters count limit passed in
2936 // through register eax. Use unsigned comparison to get negative
2937 // check for free.
2938 __ cmp(edx, Operand(eax));
2939 __ j(above_equal, &slow, not_taken);
2940
2941 // Read the argument from the stack and return it.
2942 STATIC_ASSERT(kSmiTagSize == 1);
2943 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2944 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2945 __ neg(edx);
2946 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2947 __ ret(0);
2948
2949 // Arguments adaptor case: Check index against actual arguments
2950 // limit found in the arguments adaptor frame. Use unsigned
2951 // comparison to get negative check for free.
2952 __ bind(&adaptor);
2953 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2954 __ cmp(edx, Operand(ecx));
2955 __ j(above_equal, &slow, not_taken);
2956
2957 // Read the argument from the stack and return it.
2958 STATIC_ASSERT(kSmiTagSize == 1);
2959 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2960 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2961 __ neg(edx);
2962 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2963 __ ret(0);
2964
2965 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2966 // by calling the runtime system.
2967 __ bind(&slow);
2968 __ pop(ebx); // Return address.
2969 __ push(edx);
2970 __ push(ebx);
2971 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2972}
2973
2974
2975void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2976 // esp[0] : return address
2977 // esp[4] : number of parameters
2978 // esp[8] : receiver displacement
2979 // esp[16] : function
2980
2981 // The displacement is used for skipping the return address and the
2982 // frame pointer on the stack. It is the offset of the last
2983 // parameter (if any) relative to the frame pointer.
2984 static const int kDisplacement = 2 * kPointerSize;
2985
2986 // Check if the calling frame is an arguments adaptor frame.
2987 Label adaptor_frame, try_allocate, runtime;
2988 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2989 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2990 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2991 __ j(equal, &adaptor_frame);
2992
2993 // Get the length from the frame.
2994 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2995 __ jmp(&try_allocate);
2996
2997 // Patch the arguments.length and the parameters pointer.
2998 __ bind(&adaptor_frame);
2999 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3000 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3001 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
3002 __ mov(Operand(esp, 2 * kPointerSize), edx);
3003
3004 // Try the new space allocation. Start out with computing the size of
3005 // the arguments object and the elements array.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003006 NearLabel add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003007 __ bind(&try_allocate);
3008 __ test(ecx, Operand(ecx));
3009 __ j(zero, &add_arguments_object);
3010 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3011 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003012 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003013
3014 // Do the allocation of both objects in one go.
3015 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3016
3017 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003018 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3019 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003020 __ mov(edi, Operand(edi,
3021 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003022
3023 // Copy the JS object part.
3024 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3025 __ mov(ebx, FieldOperand(edi, i));
3026 __ mov(FieldOperand(eax, i), ebx);
3027 }
3028
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003029 if (type_ == NEW_NON_STRICT) {
3030 // Setup the callee in-object property.
3031 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3032 __ mov(ebx, Operand(esp, 3 * kPointerSize));
3033 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3034 Heap::kArgumentsCalleeIndex * kPointerSize),
3035 ebx);
3036 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003037
3038 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003039 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003040 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003041 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3042 Heap::kArgumentsLengthIndex * kPointerSize),
3043 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003044
3045 // If there are no actual arguments, we're done.
3046 Label done;
3047 __ test(ecx, Operand(ecx));
3048 __ j(zero, &done);
3049
3050 // Get the parameters pointer from the stack.
3051 __ mov(edx, Operand(esp, 2 * kPointerSize));
3052
3053 // Setup the elements pointer in the allocated arguments object and
3054 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003055 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003056 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3057 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003058 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003059
ricow@chromium.org65fae842010-08-25 15:26:24 +00003060 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3061 // Untag the length for the loop below.
3062 __ SmiUntag(ecx);
3063
3064 // Copy the fixed array slots.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003065 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003066 __ bind(&loop);
3067 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3068 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3069 __ add(Operand(edi), Immediate(kPointerSize));
3070 __ sub(Operand(edx), Immediate(kPointerSize));
3071 __ dec(ecx);
3072 __ j(not_zero, &loop);
3073
3074 // Return and remove the on-stack parameters.
3075 __ bind(&done);
3076 __ ret(3 * kPointerSize);
3077
3078 // Do the runtime call to allocate the arguments object.
3079 __ bind(&runtime);
3080 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3081}
3082
3083
3084void RegExpExecStub::Generate(MacroAssembler* masm) {
3085 // Just jump directly to runtime if native RegExp is not selected at compile
3086 // time or if regexp entry in generated code is turned off runtime switch or
3087 // at compilation.
3088#ifdef V8_INTERPRETED_REGEXP
3089 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3090#else // V8_INTERPRETED_REGEXP
3091 if (!FLAG_regexp_entry_native) {
3092 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3093 return;
3094 }
3095
3096 // Stack frame on entry.
3097 // esp[0]: return address
3098 // esp[4]: last_match_info (expected JSArray)
3099 // esp[8]: previous index
3100 // esp[12]: subject string
3101 // esp[16]: JSRegExp object
3102
3103 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3104 static const int kPreviousIndexOffset = 2 * kPointerSize;
3105 static const int kSubjectOffset = 3 * kPointerSize;
3106 static const int kJSRegExpOffset = 4 * kPointerSize;
3107
3108 Label runtime, invoke_regexp;
3109
3110 // Ensure that a RegExp stack is allocated.
3111 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003112 ExternalReference::address_of_regexp_stack_memory_address(
3113 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003114 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003115 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003116 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3117 __ test(ebx, Operand(ebx));
3118 __ j(zero, &runtime, not_taken);
3119
3120 // Check that the first argument is a JSRegExp object.
3121 __ mov(eax, Operand(esp, kJSRegExpOffset));
3122 STATIC_ASSERT(kSmiTag == 0);
3123 __ test(eax, Immediate(kSmiTagMask));
3124 __ j(zero, &runtime);
3125 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3126 __ j(not_equal, &runtime);
3127 // Check that the RegExp has been compiled (data contains a fixed array).
3128 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3129 if (FLAG_debug_code) {
3130 __ test(ecx, Immediate(kSmiTagMask));
3131 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3132 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3133 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3134 }
3135
3136 // ecx: RegExp data (FixedArray)
3137 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3138 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3139 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3140 __ j(not_equal, &runtime);
3141
3142 // ecx: RegExp data (FixedArray)
3143 // Check that the number of captures fit in the static offsets vector buffer.
3144 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3145 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3146 // uses the asumption that smis are 2 * their untagged value.
3147 STATIC_ASSERT(kSmiTag == 0);
3148 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3149 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3150 // Check that the static offsets vector buffer is large enough.
3151 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3152 __ j(above, &runtime);
3153
3154 // ecx: RegExp data (FixedArray)
3155 // edx: Number of capture registers
3156 // Check that the second argument is a string.
3157 __ mov(eax, Operand(esp, kSubjectOffset));
3158 __ test(eax, Immediate(kSmiTagMask));
3159 __ j(zero, &runtime);
3160 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3161 __ j(NegateCondition(is_string), &runtime);
3162 // Get the length of the string to ebx.
3163 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3164
3165 // ebx: Length of subject string as a smi
3166 // ecx: RegExp data (FixedArray)
3167 // edx: Number of capture registers
3168 // Check that the third argument is a positive smi less than the subject
3169 // string length. A negative value will be greater (unsigned comparison).
3170 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3171 __ test(eax, Immediate(kSmiTagMask));
3172 __ j(not_zero, &runtime);
3173 __ cmp(eax, Operand(ebx));
3174 __ j(above_equal, &runtime);
3175
3176 // ecx: RegExp data (FixedArray)
3177 // edx: Number of capture registers
3178 // Check that the fourth object is a JSArray object.
3179 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3180 __ test(eax, Immediate(kSmiTagMask));
3181 __ j(zero, &runtime);
3182 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3183 __ j(not_equal, &runtime);
3184 // Check that the JSArray is in fast case.
3185 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3186 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003187 Factory* factory = masm->isolate()->factory();
3188 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003189 __ j(not_equal, &runtime);
3190 // Check that the last match info has space for the capture registers and the
3191 // additional information.
3192 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3193 __ SmiUntag(eax);
3194 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3195 __ cmp(edx, Operand(eax));
3196 __ j(greater, &runtime);
3197
3198 // ecx: RegExp data (FixedArray)
3199 // Check the representation and encoding of the subject string.
3200 Label seq_ascii_string, seq_two_byte_string, check_code;
3201 __ mov(eax, Operand(esp, kSubjectOffset));
3202 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3203 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3204 // First check for flat two byte string.
3205 __ and_(ebx,
3206 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3207 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3208 __ j(zero, &seq_two_byte_string);
3209 // Any other flat string must be a flat ascii string.
3210 __ test(Operand(ebx),
3211 Immediate(kIsNotStringMask | kStringRepresentationMask));
3212 __ j(zero, &seq_ascii_string);
3213
3214 // Check for flat cons string.
3215 // A flat cons string is a cons string where the second part is the empty
3216 // string. In that case the subject string is just the first part of the cons
3217 // string. Also in this case the first part of the cons string is known to be
3218 // a sequential string or an external string.
3219 STATIC_ASSERT(kExternalStringTag != 0);
3220 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3221 __ test(Operand(ebx),
3222 Immediate(kIsNotStringMask | kExternalStringTag));
3223 __ j(not_zero, &runtime);
3224 // String is a cons string.
3225 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003226 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003227 __ j(not_equal, &runtime);
3228 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3229 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3230 // String is a cons string with empty second part.
3231 // eax: first part of cons string.
3232 // ebx: map of first part of cons string.
3233 // Is first part a flat two byte string?
3234 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3235 kStringRepresentationMask | kStringEncodingMask);
3236 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3237 __ j(zero, &seq_two_byte_string);
3238 // Any other flat string must be ascii.
3239 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3240 kStringRepresentationMask);
3241 __ j(not_zero, &runtime);
3242
3243 __ bind(&seq_ascii_string);
3244 // eax: subject string (flat ascii)
3245 // ecx: RegExp data (FixedArray)
3246 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3247 __ Set(edi, Immediate(1)); // Type is ascii.
3248 __ jmp(&check_code);
3249
3250 __ bind(&seq_two_byte_string);
3251 // eax: subject string (flat two byte)
3252 // ecx: RegExp data (FixedArray)
3253 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3254 __ Set(edi, Immediate(0)); // Type is two byte.
3255
3256 __ bind(&check_code);
3257 // Check that the irregexp code has been generated for the actual string
3258 // encoding. If it has, the field contains a code object otherwise it contains
3259 // the hole.
3260 __ CmpObjectType(edx, CODE_TYPE, ebx);
3261 __ j(not_equal, &runtime);
3262
3263 // eax: subject string
3264 // edx: code
3265 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3266 // Load used arguments before starting to push arguments for call to native
3267 // RegExp code to avoid handling changing stack height.
3268 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3269 __ SmiUntag(ebx); // Previous index from smi.
3270
3271 // eax: subject string
3272 // ebx: previous index
3273 // edx: code
3274 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3275 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003276 Counters* counters = masm->isolate()->counters();
3277 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003278
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003279 // Isolates: note we add an additional parameter here (isolate pointer).
3280 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003281 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003282
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003283 // Argument 8: Pass current isolate address.
3284 __ mov(Operand(esp, 7 * kPointerSize),
3285 Immediate(ExternalReference::isolate_address()));
3286
ricow@chromium.org65fae842010-08-25 15:26:24 +00003287 // Argument 7: Indicate that this is a direct call from JavaScript.
3288 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3289
3290 // Argument 6: Start (high end) of backtracking stack memory area.
3291 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3292 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3293 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3294
3295 // Argument 5: static offsets vector buffer.
3296 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003297 Immediate(ExternalReference::address_of_static_offsets_vector(
3298 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003299
3300 // Argument 4: End of string data
3301 // Argument 3: Start of string data
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003302 NearLabel setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003303 __ test(edi, Operand(edi));
3304 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
3305 __ j(zero, &setup_two_byte);
3306 __ SmiUntag(edi);
3307 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3308 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3309 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3310 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3311 __ jmp(&setup_rest);
3312
3313 __ bind(&setup_two_byte);
3314 STATIC_ASSERT(kSmiTag == 0);
3315 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3316 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3317 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3318 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3319 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3320
3321 __ bind(&setup_rest);
3322
3323 // Argument 2: Previous index.
3324 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3325
3326 // Argument 1: Subject string.
3327 __ mov(Operand(esp, 0 * kPointerSize), eax);
3328
3329 // Locate the code entry and call it.
3330 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003331 __ call(Operand(edx));
3332
3333 // Drop arguments and come back to JS mode.
3334 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003335
3336 // Check the result.
3337 Label success;
3338 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
3339 __ j(equal, &success, taken);
3340 Label failure;
3341 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
3342 __ j(equal, &failure, taken);
3343 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3344 // If not exception it can only be retry. Handle that in the runtime system.
3345 __ j(not_equal, &runtime);
3346 // Result must now be exception. If there is no pending exception already a
3347 // stack overflow (on the backtrack stack) was detected in RegExp code but
3348 // haven't created the exception yet. Handle that in the runtime system.
3349 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003350 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3351 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003352 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003353 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3354 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003355 __ mov(eax, Operand::StaticVariable(pending_exception));
3356 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003357 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003358 // For exception, throw the exception again.
3359
3360 // Clear the pending exception variable.
3361 __ mov(Operand::StaticVariable(pending_exception), edx);
3362
3363 // Special handling of termination exceptions which are uncatchable
3364 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003365 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003366 Label throw_termination_exception;
3367 __ j(equal, &throw_termination_exception);
3368
3369 // Handle normal exception by following handler chain.
3370 __ Throw(eax);
3371
3372 __ bind(&throw_termination_exception);
3373 __ ThrowUncatchable(TERMINATION, eax);
3374
ricow@chromium.org65fae842010-08-25 15:26:24 +00003375 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003376 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003377 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003378 __ ret(4 * kPointerSize);
3379
3380 // Load RegExp data.
3381 __ bind(&success);
3382 __ mov(eax, Operand(esp, kJSRegExpOffset));
3383 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3384 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3385 // Calculate number of capture registers (number_of_captures + 1) * 2.
3386 STATIC_ASSERT(kSmiTag == 0);
3387 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3388 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3389
3390 // edx: Number of capture registers
3391 // Load last_match_info which is still known to be a fast case JSArray.
3392 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3393 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3394
3395 // ebx: last_match_info backing store (FixedArray)
3396 // edx: number of capture registers
3397 // Store the capture count.
3398 __ SmiTag(edx); // Number of capture registers to smi.
3399 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3400 __ SmiUntag(edx); // Number of capture registers back from smi.
3401 // Store last subject and last input.
3402 __ mov(eax, Operand(esp, kSubjectOffset));
3403 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3404 __ mov(ecx, ebx);
3405 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3406 __ mov(eax, Operand(esp, kSubjectOffset));
3407 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3408 __ mov(ecx, ebx);
3409 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3410
3411 // Get the static offsets vector filled by the native regexp code.
3412 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003413 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003414 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3415
3416 // ebx: last_match_info backing store (FixedArray)
3417 // ecx: offsets vector
3418 // edx: number of capture registers
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003419 NearLabel next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003420 // Capture register counter starts from number of capture registers and
3421 // counts down until wraping after zero.
3422 __ bind(&next_capture);
3423 __ sub(Operand(edx), Immediate(1));
3424 __ j(negative, &done);
3425 // Read the value from the static offsets vector buffer.
3426 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3427 __ SmiTag(edi);
3428 // Store the smi value in the last match info.
3429 __ mov(FieldOperand(ebx,
3430 edx,
3431 times_pointer_size,
3432 RegExpImpl::kFirstCaptureOffset),
3433 edi);
3434 __ jmp(&next_capture);
3435 __ bind(&done);
3436
3437 // Return last match info.
3438 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3439 __ ret(4 * kPointerSize);
3440
3441 // Do the runtime call to execute the regexp.
3442 __ bind(&runtime);
3443 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3444#endif // V8_INTERPRETED_REGEXP
3445}
3446
3447
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003448void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3449 const int kMaxInlineLength = 100;
3450 Label slowcase;
3451 NearLabel done;
3452 __ mov(ebx, Operand(esp, kPointerSize * 3));
3453 __ test(ebx, Immediate(kSmiTagMask));
3454 __ j(not_zero, &slowcase);
3455 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3456 __ j(above, &slowcase);
3457 // Smi-tagging is equivalent to multiplying by 2.
3458 STATIC_ASSERT(kSmiTag == 0);
3459 STATIC_ASSERT(kSmiTagSize == 1);
3460 // Allocate RegExpResult followed by FixedArray with size in ebx.
3461 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3462 // Elements: [Map][Length][..elements..]
3463 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3464 times_half_pointer_size,
3465 ebx, // In: Number of elements (times 2, being a smi)
3466 eax, // Out: Start of allocation (tagged).
3467 ecx, // Out: End of allocation.
3468 edx, // Scratch register
3469 &slowcase,
3470 TAG_OBJECT);
3471 // eax: Start of allocated area, object-tagged.
3472
3473 // Set JSArray map to global.regexp_result_map().
3474 // Set empty properties FixedArray.
3475 // Set elements to point to FixedArray allocated right after the JSArray.
3476 // Interleave operations for better latency.
3477 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003478 Factory* factory = masm->isolate()->factory();
3479 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003480 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3481 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3482 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3483 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3484 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3485 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3486
3487 // Set input, index and length fields from arguments.
3488 __ mov(ecx, Operand(esp, kPointerSize * 1));
3489 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3490 __ mov(ecx, Operand(esp, kPointerSize * 2));
3491 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3492 __ mov(ecx, Operand(esp, kPointerSize * 3));
3493 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3494
3495 // Fill out the elements FixedArray.
3496 // eax: JSArray.
3497 // ebx: FixedArray.
3498 // ecx: Number of elements in array, as smi.
3499
3500 // Set map.
3501 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003502 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003503 // Set length.
3504 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3505 // Fill contents of fixed-array with the-hole.
3506 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003507 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003508 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3509 // Fill fixed array elements with hole.
3510 // eax: JSArray.
3511 // ecx: Number of elements to fill.
3512 // ebx: Start of elements in FixedArray.
3513 // edx: the hole.
3514 Label loop;
3515 __ test(ecx, Operand(ecx));
3516 __ bind(&loop);
3517 __ j(less_equal, &done); // Jump if ecx is negative or zero.
3518 __ sub(Operand(ecx), Immediate(1));
3519 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3520 __ jmp(&loop);
3521
3522 __ bind(&done);
3523 __ ret(3 * kPointerSize);
3524
3525 __ bind(&slowcase);
3526 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3527}
3528
3529
ricow@chromium.org65fae842010-08-25 15:26:24 +00003530void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3531 Register object,
3532 Register result,
3533 Register scratch1,
3534 Register scratch2,
3535 bool object_is_smi,
3536 Label* not_found) {
3537 // Use of registers. Register result is used as a temporary.
3538 Register number_string_cache = result;
3539 Register mask = scratch1;
3540 Register scratch = scratch2;
3541
3542 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003543 ExternalReference roots_address =
3544 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003545 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3546 __ mov(number_string_cache,
3547 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3548 // Make the hash mask from the length of the number string cache. It
3549 // contains two elements (number and string) for each cache entry.
3550 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3551 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3552 __ sub(Operand(mask), Immediate(1)); // Make mask.
3553
3554 // Calculate the entry in the number string cache. The hash value in the
3555 // number string cache for smis is just the smi value, and the hash for
3556 // doubles is the xor of the upper and lower words. See
3557 // Heap::GetNumberStringCache.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003558 NearLabel smi_hash_calculated;
3559 NearLabel load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003560 if (object_is_smi) {
3561 __ mov(scratch, object);
3562 __ SmiUntag(scratch);
3563 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003564 NearLabel not_smi, hash_calculated;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003565 STATIC_ASSERT(kSmiTag == 0);
3566 __ test(object, Immediate(kSmiTagMask));
3567 __ j(not_zero, &not_smi);
3568 __ mov(scratch, object);
3569 __ SmiUntag(scratch);
3570 __ jmp(&smi_hash_calculated);
3571 __ bind(&not_smi);
3572 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003573 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003574 __ j(not_equal, not_found);
3575 STATIC_ASSERT(8 == kDoubleSize);
3576 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3577 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3578 // Object is heap number and hash is now in scratch. Calculate cache index.
3579 __ and_(scratch, Operand(mask));
3580 Register index = scratch;
3581 Register probe = mask;
3582 __ mov(probe,
3583 FieldOperand(number_string_cache,
3584 index,
3585 times_twice_pointer_size,
3586 FixedArray::kHeaderSize));
3587 __ test(probe, Immediate(kSmiTagMask));
3588 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003589 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003590 CpuFeatures::Scope fscope(SSE2);
3591 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3592 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3593 __ ucomisd(xmm0, xmm1);
3594 } else {
3595 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3596 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3597 __ FCmp();
3598 }
3599 __ j(parity_even, not_found); // Bail out if NaN is involved.
3600 __ j(not_equal, not_found); // The cache did not contain this value.
3601 __ jmp(&load_result_from_cache);
3602 }
3603
3604 __ bind(&smi_hash_calculated);
3605 // Object is smi and hash is now in scratch. Calculate cache index.
3606 __ and_(scratch, Operand(mask));
3607 Register index = scratch;
3608 // Check if the entry is the smi we are looking for.
3609 __ cmp(object,
3610 FieldOperand(number_string_cache,
3611 index,
3612 times_twice_pointer_size,
3613 FixedArray::kHeaderSize));
3614 __ j(not_equal, not_found);
3615
3616 // Get the result from the cache.
3617 __ bind(&load_result_from_cache);
3618 __ mov(result,
3619 FieldOperand(number_string_cache,
3620 index,
3621 times_twice_pointer_size,
3622 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003623 Counters* counters = masm->isolate()->counters();
3624 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003625}
3626
3627
3628void NumberToStringStub::Generate(MacroAssembler* masm) {
3629 Label runtime;
3630
3631 __ mov(ebx, Operand(esp, kPointerSize));
3632
3633 // Generate code to lookup number in the number string cache.
3634 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3635 __ ret(1 * kPointerSize);
3636
3637 __ bind(&runtime);
3638 // Handle number to string in the runtime system if not found in the cache.
3639 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3640}
3641
3642
3643static int NegativeComparisonResult(Condition cc) {
3644 ASSERT(cc != equal);
3645 ASSERT((cc == less) || (cc == less_equal)
3646 || (cc == greater) || (cc == greater_equal));
3647 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3648}
3649
3650void CompareStub::Generate(MacroAssembler* masm) {
3651 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3652
3653 Label check_unequal_objects, done;
3654
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003655 // Compare two smis if required.
3656 if (include_smi_compare_) {
3657 Label non_smi, smi_done;
3658 __ mov(ecx, Operand(edx));
3659 __ or_(ecx, Operand(eax));
3660 __ test(ecx, Immediate(kSmiTagMask));
3661 __ j(not_zero, &non_smi, not_taken);
3662 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3663 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003664 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003665 __ bind(&smi_done);
3666 __ mov(eax, edx);
3667 __ ret(0);
3668 __ bind(&non_smi);
3669 } else if (FLAG_debug_code) {
3670 __ mov(ecx, Operand(edx));
3671 __ or_(ecx, Operand(eax));
3672 __ test(ecx, Immediate(kSmiTagMask));
3673 __ Assert(not_zero, "Unexpected smi operands.");
3674 }
3675
ricow@chromium.org65fae842010-08-25 15:26:24 +00003676 // NOTICE! This code is only reached after a smi-fast-case check, so
3677 // it is certain that at least one operand isn't a smi.
3678
3679 // Identical objects can be compared fast, but there are some tricky cases
3680 // for NaN and undefined.
3681 {
3682 Label not_identical;
3683 __ cmp(eax, Operand(edx));
3684 __ j(not_equal, &not_identical);
3685
3686 if (cc_ != equal) {
3687 // Check for undefined. undefined OP undefined is false even though
3688 // undefined == undefined.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003689 NearLabel check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003690 __ cmp(edx, masm->isolate()->factory()->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003691 __ j(not_equal, &check_for_nan);
3692 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3693 __ ret(0);
3694 __ bind(&check_for_nan);
3695 }
3696
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003697 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003698 // so we do the second best thing - test it ourselves.
3699 // Note: if cc_ != equal, never_nan_nan_ is not used.
3700 if (never_nan_nan_ && (cc_ == equal)) {
3701 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3702 __ ret(0);
3703 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003704 NearLabel heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003705 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003706 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003707 __ j(equal, &heap_number);
3708 if (cc_ != equal) {
3709 // Call runtime on identical JSObjects. Otherwise return equal.
3710 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3711 __ j(above_equal, &not_identical);
3712 }
3713 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3714 __ ret(0);
3715
3716 __ bind(&heap_number);
3717 // It is a heap number, so return non-equal if it's NaN and equal if
3718 // it's not NaN.
3719 // The representation of NaN values has all exponent bits (52..62) set,
3720 // and not all mantissa bits (0..51) clear.
3721 // We only accept QNaNs, which have bit 51 set.
3722 // Read top bits of double representation (second word of value).
3723
3724 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3725 // all bits in the mask are set. We only need to check the word
3726 // that contains the exponent and high bit of the mantissa.
3727 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3728 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003729 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003730 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3731 // bits.
3732 __ add(edx, Operand(edx));
3733 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3734 if (cc_ == equal) {
3735 STATIC_ASSERT(EQUAL != 1);
3736 __ setcc(above_equal, eax);
3737 __ ret(0);
3738 } else {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003739 NearLabel nan;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003740 __ j(above_equal, &nan);
3741 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3742 __ ret(0);
3743 __ bind(&nan);
3744 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3745 __ ret(0);
3746 }
3747 }
3748
3749 __ bind(&not_identical);
3750 }
3751
3752 // Strict equality can quickly decide whether objects are equal.
3753 // Non-strict object equality is slower, so it is handled later in the stub.
3754 if (cc_ == equal && strict_) {
3755 Label slow; // Fallthrough label.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003756 NearLabel not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003757 // If we're doing a strict equality comparison, we don't have to do
3758 // type conversion, so we generate code to do fast comparison for objects
3759 // and oddballs. Non-smi numbers and strings still go through the usual
3760 // slow-case code.
3761 // If either is a Smi (we know that not both are), then they can only
3762 // be equal if the other is a HeapNumber. If so, use the slow case.
3763 STATIC_ASSERT(kSmiTag == 0);
3764 ASSERT_EQ(0, Smi::FromInt(0));
3765 __ mov(ecx, Immediate(kSmiTagMask));
3766 __ and_(ecx, Operand(eax));
3767 __ test(ecx, Operand(edx));
3768 __ j(not_zero, &not_smis);
3769 // One operand is a smi.
3770
3771 // Check whether the non-smi is a heap number.
3772 STATIC_ASSERT(kSmiTagMask == 1);
3773 // ecx still holds eax & kSmiTag, which is either zero or one.
3774 __ sub(Operand(ecx), Immediate(0x01));
3775 __ mov(ebx, edx);
3776 __ xor_(ebx, Operand(eax));
3777 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3778 __ xor_(ebx, Operand(eax));
3779 // if eax was smi, ebx is now edx, else eax.
3780
3781 // Check if the non-smi operand is a heap number.
3782 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003783 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003784 // If heap number, handle it in the slow case.
3785 __ j(equal, &slow);
3786 // Return non-equal (ebx is not zero)
3787 __ mov(eax, ebx);
3788 __ ret(0);
3789
3790 __ bind(&not_smis);
3791 // If either operand is a JSObject or an oddball value, then they are not
3792 // equal since their pointers are different
3793 // There is no test for undetectability in strict equality.
3794
3795 // Get the type of the first operand.
3796 // If the first object is a JS object, we have done pointer comparison.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003797 NearLabel first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003798 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3799 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3800 __ j(below, &first_non_object);
3801
3802 // Return non-zero (eax is not zero)
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003803 NearLabel return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003804 STATIC_ASSERT(kHeapObjectTag != 0);
3805 __ bind(&return_not_equal);
3806 __ ret(0);
3807
3808 __ bind(&first_non_object);
3809 // Check for oddballs: true, false, null, undefined.
3810 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3811 __ j(equal, &return_not_equal);
3812
3813 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
3814 __ j(above_equal, &return_not_equal);
3815
3816 // Check for oddballs: true, false, null, undefined.
3817 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3818 __ j(equal, &return_not_equal);
3819
3820 // Fall through to the general case.
3821 __ bind(&slow);
3822 }
3823
3824 // Generate the number comparison code.
3825 if (include_number_compare_) {
3826 Label non_number_comparison;
3827 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003828 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003829 CpuFeatures::Scope use_sse2(SSE2);
3830 CpuFeatures::Scope use_cmov(CMOV);
3831
3832 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3833 __ ucomisd(xmm0, xmm1);
3834
3835 // Don't base result on EFLAGS when a NaN is involved.
3836 __ j(parity_even, &unordered, not_taken);
3837 // Return a result of -1, 0, or 1, based on EFLAGS.
3838 __ mov(eax, 0); // equal
3839 __ mov(ecx, Immediate(Smi::FromInt(1)));
3840 __ cmov(above, eax, Operand(ecx));
3841 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3842 __ cmov(below, eax, Operand(ecx));
3843 __ ret(0);
3844 } else {
3845 FloatingPointHelper::CheckFloatOperands(
3846 masm, &non_number_comparison, ebx);
3847 FloatingPointHelper::LoadFloatOperand(masm, eax);
3848 FloatingPointHelper::LoadFloatOperand(masm, edx);
3849 __ FCmp();
3850
3851 // Don't base result on EFLAGS when a NaN is involved.
3852 __ j(parity_even, &unordered, not_taken);
3853
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003854 NearLabel below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003855 // Return a result of -1, 0, or 1, based on EFLAGS.
3856 __ j(below, &below_label, not_taken);
3857 __ j(above, &above_label, not_taken);
3858
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003859 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003860 __ ret(0);
3861
3862 __ bind(&below_label);
3863 __ mov(eax, Immediate(Smi::FromInt(-1)));
3864 __ ret(0);
3865
3866 __ bind(&above_label);
3867 __ mov(eax, Immediate(Smi::FromInt(1)));
3868 __ ret(0);
3869 }
3870
3871 // If one of the numbers was NaN, then the result is always false.
3872 // The cc is never not-equal.
3873 __ bind(&unordered);
3874 ASSERT(cc_ != not_equal);
3875 if (cc_ == less || cc_ == less_equal) {
3876 __ mov(eax, Immediate(Smi::FromInt(1)));
3877 } else {
3878 __ mov(eax, Immediate(Smi::FromInt(-1)));
3879 }
3880 __ ret(0);
3881
3882 // The number comparison code did not provide a valid result.
3883 __ bind(&non_number_comparison);
3884 }
3885
3886 // Fast negative check for symbol-to-symbol equality.
3887 Label check_for_strings;
3888 if (cc_ == equal) {
3889 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
3890 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
3891
3892 // We've already checked for object identity, so if both operands
3893 // are symbols they aren't equal. Register eax already holds a
3894 // non-zero value, which indicates not equal, so just return.
3895 __ ret(0);
3896 }
3897
3898 __ bind(&check_for_strings);
3899
3900 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
3901 &check_unequal_objects);
3902
3903 // Inline comparison of ascii strings.
3904 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3905 edx,
3906 eax,
3907 ecx,
3908 ebx,
3909 edi);
3910#ifdef DEBUG
3911 __ Abort("Unexpected fall-through from string comparison");
3912#endif
3913
3914 __ bind(&check_unequal_objects);
3915 if (cc_ == equal && !strict_) {
3916 // Non-strict equality. Objects are unequal if
3917 // they are both JSObjects and not undetectable,
3918 // and their pointers are different.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00003919 NearLabel not_both_objects;
3920 NearLabel return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003921 // At most one is a smi, so we can test for smi by adding the two.
3922 // A smi plus a heap object has the low bit set, a heap object plus
3923 // a heap object has the low bit clear.
3924 STATIC_ASSERT(kSmiTag == 0);
3925 STATIC_ASSERT(kSmiTagMask == 1);
3926 __ lea(ecx, Operand(eax, edx, times_1, 0));
3927 __ test(ecx, Immediate(kSmiTagMask));
3928 __ j(not_zero, &not_both_objects);
3929 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3930 __ j(below, &not_both_objects);
3931 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
3932 __ j(below, &not_both_objects);
3933 // We do not bail out after this point. Both are JSObjects, and
3934 // they are equal if and only if both are undetectable.
3935 // The and of the undetectable flags is 1 if and only if they are equal.
3936 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3937 1 << Map::kIsUndetectable);
3938 __ j(zero, &return_unequal);
3939 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
3940 1 << Map::kIsUndetectable);
3941 __ j(zero, &return_unequal);
3942 // The objects are both undetectable, so they both compare as the value
3943 // undefined, and are equal.
3944 __ Set(eax, Immediate(EQUAL));
3945 __ bind(&return_unequal);
3946 // Return non-equal by returning the non-zero object pointer in eax,
3947 // or return equal if we fell through to here.
3948 __ ret(0); // rax, rdx were pushed
3949 __ bind(&not_both_objects);
3950 }
3951
3952 // Push arguments below the return address.
3953 __ pop(ecx);
3954 __ push(edx);
3955 __ push(eax);
3956
3957 // Figure out which native to call and setup the arguments.
3958 Builtins::JavaScript builtin;
3959 if (cc_ == equal) {
3960 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3961 } else {
3962 builtin = Builtins::COMPARE;
3963 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3964 }
3965
3966 // Restore return address on the stack.
3967 __ push(ecx);
3968
3969 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3970 // tagged as a small integer.
3971 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3972}
3973
3974
3975void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3976 Label* label,
3977 Register object,
3978 Register scratch) {
3979 __ test(object, Immediate(kSmiTagMask));
3980 __ j(zero, label);
3981 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
3982 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3983 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
3984 __ cmp(scratch, kSymbolTag | kStringTag);
3985 __ j(not_equal, label);
3986}
3987
3988
3989void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003990 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003991}
3992
3993
3994void CallFunctionStub::Generate(MacroAssembler* masm) {
3995 Label slow;
3996
3997 // If the receiver might be a value (string, number or boolean) check for this
3998 // and box it if it is.
3999 if (ReceiverMightBeValue()) {
4000 // Get the receiver from the stack.
4001 // +1 ~ return address
4002 Label receiver_is_value, receiver_is_js_object;
4003 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
4004
4005 // Check if receiver is a smi (which is a number value).
4006 __ test(eax, Immediate(kSmiTagMask));
4007 __ j(zero, &receiver_is_value, not_taken);
4008
4009 // Check if the receiver is a valid JS object.
4010 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
4011 __ j(above_equal, &receiver_is_js_object);
4012
4013 // Call the runtime to box the value.
4014 __ bind(&receiver_is_value);
4015 __ EnterInternalFrame();
4016 __ push(eax);
4017 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
4018 __ LeaveInternalFrame();
4019 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
4020
4021 __ bind(&receiver_is_js_object);
4022 }
4023
4024 // Get the function to call from the stack.
4025 // +2 ~ receiver, return address
4026 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4027
4028 // Check that the function really is a JavaScript function.
4029 __ test(edi, Immediate(kSmiTagMask));
4030 __ j(zero, &slow, not_taken);
4031 // Goto slow case if we do not have a function.
4032 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
4033 __ j(not_equal, &slow, not_taken);
4034
4035 // Fast-case: Just invoke the function.
4036 ParameterCount actual(argc_);
4037 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
4038
4039 // Slow-case: Non-function called.
4040 __ bind(&slow);
4041 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4042 // of the original receiver from the call site).
4043 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4044 __ Set(eax, Immediate(argc_));
4045 __ Set(ebx, Immediate(0));
4046 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004047 Handle<Code> adaptor =
4048 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004049 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4050}
4051
4052
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004053bool CEntryStub::NeedsImmovableCode() {
4054 return false;
4055}
4056
4057
ricow@chromium.org65fae842010-08-25 15:26:24 +00004058void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004059 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004060}
4061
4062
ricow@chromium.org65fae842010-08-25 15:26:24 +00004063void CEntryStub::GenerateCore(MacroAssembler* masm,
4064 Label* throw_normal_exception,
4065 Label* throw_termination_exception,
4066 Label* throw_out_of_memory_exception,
4067 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004068 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004069 // eax: result parameter for PerformGC, if any
4070 // ebx: pointer to C function (C callee-saved)
4071 // ebp: frame pointer (restored after C call)
4072 // esp: stack pointer (restored after C call)
4073 // edi: number of arguments including receiver (C callee-saved)
4074 // esi: pointer to the first argument (C callee-saved)
4075
4076 // Result returned in eax, or eax+edx if result_size_ is 2.
4077
4078 // Check stack alignment.
4079 if (FLAG_debug_code) {
4080 __ CheckStackAlignment();
4081 }
4082
4083 if (do_gc) {
4084 // Pass failure code returned from last attempt as first argument to
4085 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4086 // stack alignment is known to be correct. This function takes one argument
4087 // which is passed on the stack, and we know that the stack has been
4088 // prepared to pass at least one argument.
4089 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4090 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4091 }
4092
4093 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004094 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004095 if (always_allocate_scope) {
4096 __ inc(Operand::StaticVariable(scope_depth));
4097 }
4098
4099 // Call C function.
4100 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4101 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004102 __ mov(Operand(esp, 2 * kPointerSize),
4103 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004104 __ call(Operand(ebx));
4105 // Result is in eax or edx:eax - do not destroy these registers!
4106
4107 if (always_allocate_scope) {
4108 __ dec(Operand::StaticVariable(scope_depth));
4109 }
4110
4111 // Make sure we're not trying to return 'the hole' from the runtime
4112 // call as this may lead to crashes in the IC code later.
4113 if (FLAG_debug_code) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004114 NearLabel okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004115 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004116 __ j(not_equal, &okay);
4117 __ int3();
4118 __ bind(&okay);
4119 }
4120
4121 // Check for failure result.
4122 Label failure_returned;
4123 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4124 __ lea(ecx, Operand(eax, 1));
4125 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4126 __ test(ecx, Immediate(kFailureTagMask));
4127 __ j(zero, &failure_returned, not_taken);
4128
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004129 ExternalReference pending_exception_address(
4130 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004131
4132 // Check that there is no pending exception, otherwise we
4133 // should have returned some failure value.
4134 if (FLAG_debug_code) {
4135 __ push(edx);
4136 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004137 ExternalReference::the_hole_value_location(masm->isolate())));
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004138 NearLabel okay;
4139 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4140 // Cannot use check here as it attempts to generate call into runtime.
4141 __ j(equal, &okay);
4142 __ int3();
4143 __ bind(&okay);
4144 __ pop(edx);
4145 }
4146
ricow@chromium.org65fae842010-08-25 15:26:24 +00004147 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004148 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004149 __ ret(0);
4150
4151 // Handling of failure.
4152 __ bind(&failure_returned);
4153
4154 Label retry;
4155 // If the returned exception is RETRY_AFTER_GC continue at retry label
4156 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4157 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4158 __ j(zero, &retry, taken);
4159
4160 // Special handling of out of memory exceptions.
4161 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4162 __ j(equal, throw_out_of_memory_exception);
4163
4164 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004165 ExternalReference the_hole_location =
4166 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004167 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004168 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004169 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4170
4171 // Special handling of termination exceptions which are uncatchable
4172 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004173 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004174 __ j(equal, throw_termination_exception);
4175
4176 // Handle normal exception.
4177 __ jmp(throw_normal_exception);
4178
4179 // Retry.
4180 __ bind(&retry);
4181}
4182
4183
4184void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4185 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004186 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004187}
4188
4189
4190void CEntryStub::Generate(MacroAssembler* masm) {
4191 // eax: number of arguments including receiver
4192 // ebx: pointer to C function (C callee-saved)
4193 // ebp: frame pointer (restored after C call)
4194 // esp: stack pointer (restored after C call)
4195 // esi: current context (C callee-saved)
4196 // edi: JS function of the caller (C callee-saved)
4197
4198 // NOTE: Invocations of builtins may return failure objects instead
4199 // of a proper result. The builtin entry handles this by performing
4200 // a garbage collection and retrying the builtin (twice).
4201
4202 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004203 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004204
4205 // eax: result parameter for PerformGC, if any (setup below)
4206 // ebx: pointer to builtin function (C callee-saved)
4207 // ebp: frame pointer (restored after C call)
4208 // esp: stack pointer (restored after C call)
4209 // edi: number of arguments including receiver (C callee-saved)
4210 // esi: argv pointer (C callee-saved)
4211
4212 Label throw_normal_exception;
4213 Label throw_termination_exception;
4214 Label throw_out_of_memory_exception;
4215
4216 // Call into the runtime system.
4217 GenerateCore(masm,
4218 &throw_normal_exception,
4219 &throw_termination_exception,
4220 &throw_out_of_memory_exception,
4221 false,
4222 false);
4223
4224 // Do space-specific GC and retry runtime call.
4225 GenerateCore(masm,
4226 &throw_normal_exception,
4227 &throw_termination_exception,
4228 &throw_out_of_memory_exception,
4229 true,
4230 false);
4231
4232 // Do full GC and retry runtime call one final time.
4233 Failure* failure = Failure::InternalError();
4234 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4235 GenerateCore(masm,
4236 &throw_normal_exception,
4237 &throw_termination_exception,
4238 &throw_out_of_memory_exception,
4239 true,
4240 true);
4241
4242 __ bind(&throw_out_of_memory_exception);
4243 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4244
4245 __ bind(&throw_termination_exception);
4246 GenerateThrowUncatchable(masm, TERMINATION);
4247
4248 __ bind(&throw_normal_exception);
4249 GenerateThrowTOS(masm);
4250}
4251
4252
4253void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4254 Label invoke, exit;
4255#ifdef ENABLE_LOGGING_AND_PROFILING
4256 Label not_outermost_js, not_outermost_js_2;
4257#endif
4258
4259 // Setup frame.
4260 __ push(ebp);
4261 __ mov(ebp, Operand(esp));
4262
4263 // Push marker in two places.
4264 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4265 __ push(Immediate(Smi::FromInt(marker))); // context slot
4266 __ push(Immediate(Smi::FromInt(marker))); // function slot
4267 // Save callee-saved registers (C calling conventions).
4268 __ push(edi);
4269 __ push(esi);
4270 __ push(ebx);
4271
4272 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004273 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004274 __ push(Operand::StaticVariable(c_entry_fp));
4275
4276#ifdef ENABLE_LOGGING_AND_PROFILING
4277 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004278 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4279 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004280 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4281 __ j(not_equal, &not_outermost_js);
4282 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4283 __ bind(&not_outermost_js);
4284#endif
4285
4286 // Call a faked try-block that does the invoke.
4287 __ call(&invoke);
4288
4289 // Caught exception: Store result (exception) in the pending
4290 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004291 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4292 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004293 __ mov(Operand::StaticVariable(pending_exception), eax);
4294 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4295 __ jmp(&exit);
4296
4297 // Invoke: Link this frame into the handler chain.
4298 __ bind(&invoke);
4299 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4300
4301 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004302 ExternalReference the_hole_location =
4303 ExternalReference::the_hole_value_location(masm->isolate());
4304 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004305 __ mov(Operand::StaticVariable(pending_exception), edx);
4306
4307 // Fake a receiver (NULL).
4308 __ push(Immediate(0)); // receiver
4309
4310 // Invoke the function by calling through JS entry trampoline
4311 // builtin and pop the faked function when we return. Notice that we
4312 // cannot store a reference to the trampoline code directly in this
4313 // stub, because the builtin stubs may not have been generated yet.
4314 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004315 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004316 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004317 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004318 __ mov(edx, Immediate(construct_entry));
4319 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004320 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004321 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004322 __ mov(edx, Immediate(entry));
4323 }
4324 __ mov(edx, Operand(edx, 0)); // deref address
4325 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4326 __ call(Operand(edx));
4327
4328 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004329 __ pop(Operand::StaticVariable(ExternalReference(
4330 Isolate::k_handler_address,
4331 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004332 // Pop next_sp.
4333 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
4334
4335#ifdef ENABLE_LOGGING_AND_PROFILING
4336 // If current EBP value is the same as js_entry_sp value, it means that
4337 // the current function is the outermost.
4338 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
4339 __ j(not_equal, &not_outermost_js_2);
4340 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4341 __ bind(&not_outermost_js_2);
4342#endif
4343
4344 // Restore the top frame descriptor from the stack.
4345 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004346 __ pop(Operand::StaticVariable(ExternalReference(
4347 Isolate::k_c_entry_fp_address,
4348 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004349
4350 // Restore callee-saved registers (C calling conventions).
4351 __ pop(ebx);
4352 __ pop(esi);
4353 __ pop(edi);
4354 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4355
4356 // Restore frame pointer and return.
4357 __ pop(ebp);
4358 __ ret(0);
4359}
4360
4361
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004362// Generate stub code for instanceof.
4363// This code can patch a call site inlined cache of the instance of check,
4364// which looks like this.
4365//
4366// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4367// 75 0a jne <some near label>
4368// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4369//
4370// If call site patching is requested the stack will have the delta from the
4371// return address to the cmp instruction just below the return address. This
4372// also means that call site patching can only take place with arguments in
4373// registers. TOS looks like this when call site patching is requested
4374//
4375// esp[0] : return address
4376// esp[4] : delta from return address to cmp instruction
4377//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004378void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004379 // Call site inlining and patching implies arguments in registers.
4380 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4381
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004382 // Fixed register usage throughout the stub.
4383 Register object = eax; // Object (lhs).
4384 Register map = ebx; // Map of the object.
4385 Register function = edx; // Function (rhs).
4386 Register prototype = edi; // Prototype of the function.
4387 Register scratch = ecx;
4388
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004389 // Constants describing the call site code to patch.
4390 static const int kDeltaToCmpImmediate = 2;
4391 static const int kDeltaToMov = 8;
4392 static const int kDeltaToMovImmediate = 9;
4393 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4394 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4395 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4396
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004397 ExternalReference roots_address =
4398 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004399
4400 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4401 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4402
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004403 // Get the object and function - they are always both needed.
4404 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004405 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004406 __ mov(object, Operand(esp, 2 * kPointerSize));
4407 __ mov(function, Operand(esp, 1 * kPointerSize));
4408 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004409
4410 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004411 __ test(object, Immediate(kSmiTagMask));
4412 __ j(zero, &not_js_object, not_taken);
4413 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004414
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004415 // If there is a call site cache don't look in the global cache, but do the
4416 // real lookup and update the call site cache.
4417 if (!HasCallSiteInlineCheck()) {
4418 // Look up the function and the map in the instanceof cache.
4419 NearLabel miss;
4420 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4421 __ cmp(function,
4422 Operand::StaticArray(scratch, times_pointer_size, roots_address));
4423 __ j(not_equal, &miss);
4424 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4425 __ cmp(map, Operand::StaticArray(
4426 scratch, times_pointer_size, roots_address));
4427 __ j(not_equal, &miss);
4428 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4429 __ mov(eax, Operand::StaticArray(
4430 scratch, times_pointer_size, roots_address));
4431 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4432 __ bind(&miss);
4433 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004434
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004435 // Get the prototype of the function.
4436 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004437
4438 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004439 __ test(prototype, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004440 __ j(zero, &slow, not_taken);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004441 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004442
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004443 // Update the global instanceof or call site inlined cache with the current
4444 // map and function. The cached answer will be set when it is known below.
4445 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004446 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4447 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4448 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4449 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4450 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004451 } else {
4452 // The constants for the code patching are based on no push instructions
4453 // at the call site.
4454 ASSERT(HasArgsInRegisters());
4455 // Get return address and delta to inlined map check.
4456 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4457 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4458 if (FLAG_debug_code) {
4459 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4460 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4461 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4462 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4463 }
4464 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4465 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004466
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004467 // Loop through the prototype chain of the object looking for the function
4468 // prototype.
4469 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004470 NearLabel loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004471 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004472 __ cmp(scratch, Operand(prototype));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004473 __ j(equal, &is_instance);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004474 Factory* factory = masm->isolate()->factory();
4475 __ cmp(Operand(scratch), Immediate(factory->null_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004476 __ j(equal, &is_not_instance);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004477 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4478 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004479 __ jmp(&loop);
4480
4481 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004482 if (!HasCallSiteInlineCheck()) {
4483 __ Set(eax, Immediate(0));
4484 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4485 __ mov(Operand::StaticArray(scratch,
4486 times_pointer_size, roots_address), eax);
4487 } else {
4488 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004489 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004490 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4491 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4492 if (FLAG_debug_code) {
4493 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4494 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4495 }
4496 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4497 if (!ReturnTrueFalseObject()) {
4498 __ Set(eax, Immediate(0));
4499 }
4500 }
4501 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004502
4503 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004504 if (!HasCallSiteInlineCheck()) {
4505 __ Set(eax, Immediate(Smi::FromInt(1)));
4506 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4507 __ mov(Operand::StaticArray(
4508 scratch, times_pointer_size, roots_address), eax);
4509 } else {
4510 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004511 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004512 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4513 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4514 if (FLAG_debug_code) {
4515 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4516 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4517 }
4518 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4519 if (!ReturnTrueFalseObject()) {
4520 __ Set(eax, Immediate(Smi::FromInt(1)));
4521 }
4522 }
4523 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004524
4525 Label object_not_null, object_not_null_or_smi;
4526 __ bind(&not_js_object);
4527 // Before null, smi and string value checks, check that the rhs is a function
4528 // as for a non-function rhs an exception needs to be thrown.
4529 __ test(function, Immediate(kSmiTagMask));
4530 __ j(zero, &slow, not_taken);
4531 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
4532 __ j(not_equal, &slow, not_taken);
4533
4534 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004535 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004536 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004537 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004538 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004539
4540 __ bind(&object_not_null);
4541 // Smi values is not instance of anything.
4542 __ test(object, Immediate(kSmiTagMask));
4543 __ j(not_zero, &object_not_null_or_smi, not_taken);
4544 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004545 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004546
4547 __ bind(&object_not_null_or_smi);
4548 // String values is not instance of anything.
4549 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4550 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004551 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004552 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004553
4554 // Slow-case: Go through the JavaScript implementation.
4555 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004556 if (!ReturnTrueFalseObject()) {
4557 // Tail call the builtin which returns 0 or 1.
4558 if (HasArgsInRegisters()) {
4559 // Push arguments below return address.
4560 __ pop(scratch);
4561 __ push(object);
4562 __ push(function);
4563 __ push(scratch);
4564 }
4565 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4566 } else {
4567 // Call the builtin and convert 0/1 to true/false.
4568 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004569 __ push(object);
4570 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004571 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4572 __ LeaveInternalFrame();
4573 NearLabel true_value, done;
4574 __ test(eax, Operand(eax));
4575 __ j(zero, &true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004576 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004577 __ jmp(&done);
4578 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004579 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004580 __ bind(&done);
4581 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004582 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004583}
4584
4585
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004586Register InstanceofStub::left() { return eax; }
4587
4588
4589Register InstanceofStub::right() { return edx; }
4590
4591
ricow@chromium.org65fae842010-08-25 15:26:24 +00004592int CompareStub::MinorKey() {
4593 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4594 // stubs the never NaN NaN condition is only taken into account if the
4595 // condition is equals.
4596 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4597 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4598 return ConditionField::encode(static_cast<unsigned>(cc_))
4599 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4600 | StrictField::encode(strict_)
4601 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004602 | IncludeNumberCompareField::encode(include_number_compare_)
4603 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004604}
4605
4606
4607// Unfortunately you have to run without snapshots to see most of these
4608// names in the profile since most compare stubs end up in the snapshot.
4609const char* CompareStub::GetName() {
4610 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4611
4612 if (name_ != NULL) return name_;
4613 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004614 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4615 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004616 if (name_ == NULL) return "OOM";
4617
4618 const char* cc_name;
4619 switch (cc_) {
4620 case less: cc_name = "LT"; break;
4621 case greater: cc_name = "GT"; break;
4622 case less_equal: cc_name = "LE"; break;
4623 case greater_equal: cc_name = "GE"; break;
4624 case equal: cc_name = "EQ"; break;
4625 case not_equal: cc_name = "NE"; break;
4626 default: cc_name = "UnknownCondition"; break;
4627 }
4628
4629 const char* strict_name = "";
4630 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4631 strict_name = "_STRICT";
4632 }
4633
4634 const char* never_nan_nan_name = "";
4635 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4636 never_nan_nan_name = "_NO_NAN";
4637 }
4638
4639 const char* include_number_compare_name = "";
4640 if (!include_number_compare_) {
4641 include_number_compare_name = "_NO_NUMBER";
4642 }
4643
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004644 const char* include_smi_compare_name = "";
4645 if (!include_smi_compare_) {
4646 include_smi_compare_name = "_NO_SMI";
4647 }
4648
ricow@chromium.org65fae842010-08-25 15:26:24 +00004649 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004650 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004651 cc_name,
4652 strict_name,
4653 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004654 include_number_compare_name,
4655 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004656 return name_;
4657}
4658
4659
4660// -------------------------------------------------------------------------
4661// StringCharCodeAtGenerator
4662
4663void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4664 Label flat_string;
4665 Label ascii_string;
4666 Label got_char_code;
4667
4668 // If the receiver is a smi trigger the non-string case.
4669 STATIC_ASSERT(kSmiTag == 0);
4670 __ test(object_, Immediate(kSmiTagMask));
4671 __ j(zero, receiver_not_string_);
4672
4673 // Fetch the instance type of the receiver into result register.
4674 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4675 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4676 // If the receiver is not a string trigger the non-string case.
4677 __ test(result_, Immediate(kIsNotStringMask));
4678 __ j(not_zero, receiver_not_string_);
4679
4680 // If the index is non-smi trigger the non-smi case.
4681 STATIC_ASSERT(kSmiTag == 0);
4682 __ test(index_, Immediate(kSmiTagMask));
4683 __ j(not_zero, &index_not_smi_);
4684
4685 // Put smi-tagged index into scratch register.
4686 __ mov(scratch_, index_);
4687 __ bind(&got_smi_index_);
4688
4689 // Check for index out of range.
4690 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4691 __ j(above_equal, index_out_of_range_);
4692
4693 // We need special handling for non-flat strings.
4694 STATIC_ASSERT(kSeqStringTag == 0);
4695 __ test(result_, Immediate(kStringRepresentationMask));
4696 __ j(zero, &flat_string);
4697
4698 // Handle non-flat strings.
4699 __ test(result_, Immediate(kIsConsStringMask));
4700 __ j(zero, &call_runtime_);
4701
4702 // ConsString.
4703 // Check whether the right hand side is the empty string (i.e. if
4704 // this is really a flat string in a cons string). If that is not
4705 // the case we would rather go to the runtime system now to flatten
4706 // the string.
4707 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004708 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004709 __ j(not_equal, &call_runtime_);
4710 // Get the first of the two strings and load its instance type.
4711 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4712 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4713 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4714 // If the first cons component is also non-flat, then go to runtime.
4715 STATIC_ASSERT(kSeqStringTag == 0);
4716 __ test(result_, Immediate(kStringRepresentationMask));
4717 __ j(not_zero, &call_runtime_);
4718
4719 // Check for 1-byte or 2-byte string.
4720 __ bind(&flat_string);
4721 STATIC_ASSERT(kAsciiStringTag != 0);
4722 __ test(result_, Immediate(kStringEncodingMask));
4723 __ j(not_zero, &ascii_string);
4724
4725 // 2-byte string.
4726 // Load the 2-byte character code into the result register.
4727 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4728 __ movzx_w(result_, FieldOperand(object_,
4729 scratch_, times_1, // Scratch is smi-tagged.
4730 SeqTwoByteString::kHeaderSize));
4731 __ jmp(&got_char_code);
4732
4733 // ASCII string.
4734 // Load the byte into the result register.
4735 __ bind(&ascii_string);
4736 __ SmiUntag(scratch_);
4737 __ movzx_b(result_, FieldOperand(object_,
4738 scratch_, times_1,
4739 SeqAsciiString::kHeaderSize));
4740 __ bind(&got_char_code);
4741 __ SmiTag(result_);
4742 __ bind(&exit_);
4743}
4744
4745
4746void StringCharCodeAtGenerator::GenerateSlow(
4747 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4748 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4749
4750 // Index is not a smi.
4751 __ bind(&index_not_smi_);
4752 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004753 __ CheckMap(index_,
4754 masm->isolate()->factory()->heap_number_map(),
4755 index_not_number_,
4756 true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004757 call_helper.BeforeCall(masm);
4758 __ push(object_);
4759 __ push(index_);
4760 __ push(index_); // Consumed by runtime conversion function.
4761 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4762 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4763 } else {
4764 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4765 // NumberToSmi discards numbers that are not exact integers.
4766 __ CallRuntime(Runtime::kNumberToSmi, 1);
4767 }
4768 if (!scratch_.is(eax)) {
4769 // Save the conversion result before the pop instructions below
4770 // have a chance to overwrite it.
4771 __ mov(scratch_, eax);
4772 }
4773 __ pop(index_);
4774 __ pop(object_);
4775 // Reload the instance type.
4776 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4777 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4778 call_helper.AfterCall(masm);
4779 // If index is still not a smi, it must be out of range.
4780 STATIC_ASSERT(kSmiTag == 0);
4781 __ test(scratch_, Immediate(kSmiTagMask));
4782 __ j(not_zero, index_out_of_range_);
4783 // Otherwise, return to the fast path.
4784 __ jmp(&got_smi_index_);
4785
4786 // Call runtime. We get here when the receiver is a string and the
4787 // index is a number, but the code of getting the actual character
4788 // is too complex (e.g., when the string needs to be flattened).
4789 __ bind(&call_runtime_);
4790 call_helper.BeforeCall(masm);
4791 __ push(object_);
4792 __ push(index_);
4793 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4794 if (!result_.is(eax)) {
4795 __ mov(result_, eax);
4796 }
4797 call_helper.AfterCall(masm);
4798 __ jmp(&exit_);
4799
4800 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4801}
4802
4803
4804// -------------------------------------------------------------------------
4805// StringCharFromCodeGenerator
4806
4807void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4808 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4809 STATIC_ASSERT(kSmiTag == 0);
4810 STATIC_ASSERT(kSmiShiftSize == 0);
4811 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4812 __ test(code_,
4813 Immediate(kSmiTagMask |
4814 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
4815 __ j(not_zero, &slow_case_, not_taken);
4816
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004817 Factory* factory = masm->isolate()->factory();
4818 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004819 STATIC_ASSERT(kSmiTag == 0);
4820 STATIC_ASSERT(kSmiTagSize == 1);
4821 STATIC_ASSERT(kSmiShiftSize == 0);
4822 // At this point code register contains smi tagged ascii char code.
4823 __ mov(result_, FieldOperand(result_,
4824 code_, times_half_pointer_size,
4825 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004826 __ cmp(result_, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004827 __ j(equal, &slow_case_, not_taken);
4828 __ bind(&exit_);
4829}
4830
4831
4832void StringCharFromCodeGenerator::GenerateSlow(
4833 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4834 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4835
4836 __ bind(&slow_case_);
4837 call_helper.BeforeCall(masm);
4838 __ push(code_);
4839 __ CallRuntime(Runtime::kCharFromCode, 1);
4840 if (!result_.is(eax)) {
4841 __ mov(result_, eax);
4842 }
4843 call_helper.AfterCall(masm);
4844 __ jmp(&exit_);
4845
4846 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4847}
4848
4849
4850// -------------------------------------------------------------------------
4851// StringCharAtGenerator
4852
4853void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4854 char_code_at_generator_.GenerateFast(masm);
4855 char_from_code_generator_.GenerateFast(masm);
4856}
4857
4858
4859void StringCharAtGenerator::GenerateSlow(
4860 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4861 char_code_at_generator_.GenerateSlow(masm, call_helper);
4862 char_from_code_generator_.GenerateSlow(masm, call_helper);
4863}
4864
4865
4866void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004867 Label string_add_runtime, call_builtin;
4868 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004869
4870 // Load the two arguments.
4871 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4872 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4873
4874 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004875 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004876 __ test(eax, Immediate(kSmiTagMask));
4877 __ j(zero, &string_add_runtime);
4878 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
4879 __ j(above_equal, &string_add_runtime);
4880
4881 // First argument is a a string, test second.
4882 __ test(edx, Immediate(kSmiTagMask));
4883 __ j(zero, &string_add_runtime);
4884 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
4885 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004886 } else {
4887 // Here at least one of the arguments is definitely a string.
4888 // We convert the one that is not known to be a string.
4889 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4890 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4891 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
4892 &call_builtin);
4893 builtin_id = Builtins::STRING_ADD_RIGHT;
4894 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4895 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4896 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
4897 &call_builtin);
4898 builtin_id = Builtins::STRING_ADD_LEFT;
4899 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004900 }
4901
4902 // Both arguments are strings.
4903 // eax: first string
4904 // edx: second string
4905 // Check if either of the strings are empty. In that case return the other.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00004906 NearLabel second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004907 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
4908 STATIC_ASSERT(kSmiTag == 0);
4909 __ test(ecx, Operand(ecx));
4910 __ j(not_zero, &second_not_zero_length);
4911 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004912 Counters* counters = masm->isolate()->counters();
4913 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004914 __ ret(2 * kPointerSize);
4915 __ bind(&second_not_zero_length);
4916 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
4917 STATIC_ASSERT(kSmiTag == 0);
4918 __ test(ebx, Operand(ebx));
4919 __ j(not_zero, &both_not_zero_length);
4920 // First string is empty, result is second string which is in edx.
4921 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004922 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004923 __ ret(2 * kPointerSize);
4924
4925 // Both strings are non-empty.
4926 // eax: first string
4927 // ebx: length of first string as a smi
4928 // ecx: length of second string as a smi
4929 // edx: second string
4930 // Look at the length of the result of adding the two strings.
4931 Label string_add_flat_result, longer_than_two;
4932 __ bind(&both_not_zero_length);
4933 __ add(ebx, Operand(ecx));
4934 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
4935 // Handle exceptionally long strings in the runtime system.
4936 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004937 // Use the symbol table when adding two one character strings, as it
4938 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004939 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
4940 __ j(not_equal, &longer_than_two);
4941
4942 // Check that both strings are non-external ascii strings.
4943 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
4944 &string_add_runtime);
4945
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004946 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004947 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4948 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4949
4950 // Try to lookup two character string in symbol table. If it is not found
4951 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004952 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004953 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004954 masm, ebx, ecx, eax, edx, edi,
4955 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004956 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004957 __ ret(2 * kPointerSize);
4958
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004959 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004960 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004961 // Reload the arguments.
4962 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4963 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4964 // Get the two characters forming the new string.
4965 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4966 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4967 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004968 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004969 __ AllocateAsciiString(eax, // Result.
4970 2, // Length.
4971 edi, // Scratch 1.
4972 edx, // Scratch 2.
4973 &string_add_runtime);
4974 // Pack both characters in ebx.
4975 __ shl(ecx, kBitsPerByte);
4976 __ or_(ebx, Operand(ecx));
4977 // Set the characters in the new string.
4978 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004979 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004980 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004981
4982 __ bind(&longer_than_two);
4983 // Check if resulting string will be flat.
4984 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
4985 __ j(below, &string_add_flat_result);
4986
4987 // If result is not supposed to be flat allocate a cons string object. If both
4988 // strings are ascii the result is an ascii cons string.
4989 Label non_ascii, allocated, ascii_data;
4990 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
4991 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
4992 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
4993 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
4994 __ and_(ecx, Operand(edi));
4995 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4996 __ test(ecx, Immediate(kAsciiStringTag));
4997 __ j(zero, &non_ascii);
4998 __ bind(&ascii_data);
4999 // Allocate an acsii cons string.
5000 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5001 __ bind(&allocated);
5002 // Fill the fields of the cons string.
5003 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5004 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5005 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5006 Immediate(String::kEmptyHashField));
5007 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5008 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5009 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005010 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005011 __ ret(2 * kPointerSize);
5012 __ bind(&non_ascii);
5013 // At least one of the strings is two-byte. Check whether it happens
5014 // to contain only ascii characters.
5015 // ecx: first instance type AND second instance type.
5016 // edi: second instance type.
5017 __ test(ecx, Immediate(kAsciiDataHintMask));
5018 __ j(not_zero, &ascii_data);
5019 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5020 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5021 __ xor_(edi, Operand(ecx));
5022 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5023 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5024 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5025 __ j(equal, &ascii_data);
5026 // Allocate a two byte cons string.
5027 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5028 __ jmp(&allocated);
5029
5030 // Handle creating a flat result. First check that both strings are not
5031 // external strings.
5032 // eax: first string
5033 // ebx: length of resulting flat string as a smi
5034 // edx: second string
5035 __ bind(&string_add_flat_result);
5036 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5037 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5038 __ and_(ecx, kStringRepresentationMask);
5039 __ cmp(ecx, kExternalStringTag);
5040 __ j(equal, &string_add_runtime);
5041 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5042 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5043 __ and_(ecx, kStringRepresentationMask);
5044 __ cmp(ecx, kExternalStringTag);
5045 __ j(equal, &string_add_runtime);
5046 // Now check if both strings are ascii strings.
5047 // eax: first string
5048 // ebx: length of resulting flat string as a smi
5049 // edx: second string
5050 Label non_ascii_string_add_flat_result;
5051 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5052 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5053 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5054 __ j(zero, &non_ascii_string_add_flat_result);
5055 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5056 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5057 __ j(zero, &string_add_runtime);
5058
ricow@chromium.org65fae842010-08-25 15:26:24 +00005059 // Both strings are ascii strings. As they are short they are both flat.
5060 // ebx: length of resulting flat string as a smi
5061 __ SmiUntag(ebx);
5062 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5063 // eax: result string
5064 __ mov(ecx, eax);
5065 // Locate first character of result.
5066 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5067 // Load first argument and locate first character.
5068 __ mov(edx, Operand(esp, 2 * kPointerSize));
5069 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5070 __ SmiUntag(edi);
5071 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5072 // eax: result string
5073 // ecx: first character of result
5074 // edx: first char of first argument
5075 // edi: length of first argument
5076 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5077 // Load second argument and locate first character.
5078 __ mov(edx, Operand(esp, 1 * kPointerSize));
5079 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5080 __ SmiUntag(edi);
5081 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5082 // eax: result string
5083 // ecx: next character of result
5084 // edx: first char of second argument
5085 // edi: length of second argument
5086 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005087 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005088 __ ret(2 * kPointerSize);
5089
5090 // Handle creating a flat two byte result.
5091 // eax: first string - known to be two byte
5092 // ebx: length of resulting flat string as a smi
5093 // edx: second string
5094 __ bind(&non_ascii_string_add_flat_result);
5095 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5096 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5097 __ j(not_zero, &string_add_runtime);
5098 // Both strings are two byte strings. As they are short they are both
5099 // flat.
5100 __ SmiUntag(ebx);
5101 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5102 // eax: result string
5103 __ mov(ecx, eax);
5104 // Locate first character of result.
5105 __ add(Operand(ecx),
5106 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5107 // Load first argument and locate first character.
5108 __ mov(edx, Operand(esp, 2 * kPointerSize));
5109 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5110 __ SmiUntag(edi);
5111 __ add(Operand(edx),
5112 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5113 // eax: result string
5114 // ecx: first character of result
5115 // edx: first char of first argument
5116 // edi: length of first argument
5117 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5118 // Load second argument and locate first character.
5119 __ mov(edx, Operand(esp, 1 * kPointerSize));
5120 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5121 __ SmiUntag(edi);
5122 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5123 // eax: result string
5124 // ecx: next character of result
5125 // edx: first char of second argument
5126 // edi: length of second argument
5127 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005128 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005129 __ ret(2 * kPointerSize);
5130
5131 // Just jump to runtime to add the two strings.
5132 __ bind(&string_add_runtime);
5133 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005134
5135 if (call_builtin.is_linked()) {
5136 __ bind(&call_builtin);
5137 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5138 }
5139}
5140
5141
5142void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5143 int stack_offset,
5144 Register arg,
5145 Register scratch1,
5146 Register scratch2,
5147 Register scratch3,
5148 Label* slow) {
5149 // First check if the argument is already a string.
5150 Label not_string, done;
5151 __ test(arg, Immediate(kSmiTagMask));
5152 __ j(zero, &not_string);
5153 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5154 __ j(below, &done);
5155
5156 // Check the number to string cache.
5157 Label not_cached;
5158 __ bind(&not_string);
5159 // Puts the cached result into scratch1.
5160 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5161 arg,
5162 scratch1,
5163 scratch2,
5164 scratch3,
5165 false,
5166 &not_cached);
5167 __ mov(arg, scratch1);
5168 __ mov(Operand(esp, stack_offset), arg);
5169 __ jmp(&done);
5170
5171 // Check if the argument is a safe string wrapper.
5172 __ bind(&not_cached);
5173 __ test(arg, Immediate(kSmiTagMask));
5174 __ j(zero, slow);
5175 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5176 __ j(not_equal, slow);
5177 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5178 1 << Map::kStringWrapperSafeForDefaultValueOf);
5179 __ j(zero, slow);
5180 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5181 __ mov(Operand(esp, stack_offset), arg);
5182
5183 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005184}
5185
5186
5187void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5188 Register dest,
5189 Register src,
5190 Register count,
5191 Register scratch,
5192 bool ascii) {
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005193 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005194 __ bind(&loop);
5195 // This loop just copies one character at a time, as it is only used for very
5196 // short strings.
5197 if (ascii) {
5198 __ mov_b(scratch, Operand(src, 0));
5199 __ mov_b(Operand(dest, 0), scratch);
5200 __ add(Operand(src), Immediate(1));
5201 __ add(Operand(dest), Immediate(1));
5202 } else {
5203 __ mov_w(scratch, Operand(src, 0));
5204 __ mov_w(Operand(dest, 0), scratch);
5205 __ add(Operand(src), Immediate(2));
5206 __ add(Operand(dest), Immediate(2));
5207 }
5208 __ sub(Operand(count), Immediate(1));
5209 __ j(not_zero, &loop);
5210}
5211
5212
5213void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5214 Register dest,
5215 Register src,
5216 Register count,
5217 Register scratch,
5218 bool ascii) {
5219 // Copy characters using rep movs of doublewords.
5220 // The destination is aligned on a 4 byte boundary because we are
5221 // copying to the beginning of a newly allocated string.
5222 ASSERT(dest.is(edi)); // rep movs destination
5223 ASSERT(src.is(esi)); // rep movs source
5224 ASSERT(count.is(ecx)); // rep movs count
5225 ASSERT(!scratch.is(dest));
5226 ASSERT(!scratch.is(src));
5227 ASSERT(!scratch.is(count));
5228
5229 // Nothing to do for zero characters.
5230 Label done;
5231 __ test(count, Operand(count));
5232 __ j(zero, &done);
5233
5234 // Make count the number of bytes to copy.
5235 if (!ascii) {
5236 __ shl(count, 1);
5237 }
5238
5239 // Don't enter the rep movs if there are less than 4 bytes to copy.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005240 NearLabel last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005241 __ test(count, Immediate(~3));
5242 __ j(zero, &last_bytes);
5243
5244 // Copy from edi to esi using rep movs instruction.
5245 __ mov(scratch, count);
5246 __ sar(count, 2); // Number of doublewords to copy.
5247 __ cld();
5248 __ rep_movs();
5249
5250 // Find number of bytes left.
5251 __ mov(count, scratch);
5252 __ and_(count, 3);
5253
5254 // Check if there are more bytes to copy.
5255 __ bind(&last_bytes);
5256 __ test(count, Operand(count));
5257 __ j(zero, &done);
5258
5259 // Copy remaining characters.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005260 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005261 __ bind(&loop);
5262 __ mov_b(scratch, Operand(src, 0));
5263 __ mov_b(Operand(dest, 0), scratch);
5264 __ add(Operand(src), Immediate(1));
5265 __ add(Operand(dest), Immediate(1));
5266 __ sub(Operand(count), Immediate(1));
5267 __ j(not_zero, &loop);
5268
5269 __ bind(&done);
5270}
5271
5272
5273void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5274 Register c1,
5275 Register c2,
5276 Register scratch1,
5277 Register scratch2,
5278 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005279 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005280 Label* not_found) {
5281 // Register scratch3 is the general scratch register in this function.
5282 Register scratch = scratch3;
5283
5284 // Make sure that both characters are not digits as such strings has a
5285 // different hash algorithm. Don't try to look for these in the symbol table.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005286 NearLabel not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005287 __ mov(scratch, c1);
5288 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5289 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
5290 __ j(above, &not_array_index);
5291 __ mov(scratch, c2);
5292 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5293 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005294 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005295
5296 __ bind(&not_array_index);
5297 // Calculate the two character string hash.
5298 Register hash = scratch1;
5299 GenerateHashInit(masm, hash, c1, scratch);
5300 GenerateHashAddCharacter(masm, hash, c2, scratch);
5301 GenerateHashGetHash(masm, hash, scratch);
5302
5303 // Collect the two characters in a register.
5304 Register chars = c1;
5305 __ shl(c2, kBitsPerByte);
5306 __ or_(chars, Operand(c2));
5307
5308 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5309 // hash: hash of two character string.
5310
5311 // Load the symbol table.
5312 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005313 ExternalReference roots_address =
5314 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005315 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5316 __ mov(symbol_table,
5317 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5318
5319 // Calculate capacity mask from the symbol table capacity.
5320 Register mask = scratch2;
5321 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5322 __ SmiUntag(mask);
5323 __ sub(Operand(mask), Immediate(1));
5324
5325 // Registers
5326 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5327 // hash: hash of two character string
5328 // symbol_table: symbol table
5329 // mask: capacity mask
5330 // scratch: -
5331
5332 // Perform a number of probes in the symbol table.
5333 static const int kProbes = 4;
5334 Label found_in_symbol_table;
5335 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5336 for (int i = 0; i < kProbes; i++) {
5337 // Calculate entry in symbol table.
5338 __ mov(scratch, hash);
5339 if (i > 0) {
5340 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5341 }
5342 __ and_(scratch, Operand(mask));
5343
5344 // Load the entry from the symbol table.
5345 Register candidate = scratch; // Scratch register contains candidate.
5346 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5347 __ mov(candidate,
5348 FieldOperand(symbol_table,
5349 scratch,
5350 times_pointer_size,
5351 SymbolTable::kElementsStartOffset));
5352
5353 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005354 Factory* factory = masm->isolate()->factory();
5355 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005356 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005357 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005358 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005359
5360 // If length is not 2 the string is not a candidate.
5361 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5362 Immediate(Smi::FromInt(2)));
5363 __ j(not_equal, &next_probe[i]);
5364
5365 // As we are out of registers save the mask on the stack and use that
5366 // register as a temporary.
5367 __ push(mask);
5368 Register temp = mask;
5369
5370 // Check that the candidate is a non-external ascii string.
5371 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5372 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5373 __ JumpIfInstanceTypeIsNotSequentialAscii(
5374 temp, temp, &next_probe_pop_mask[i]);
5375
5376 // Check if the two characters match.
5377 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5378 __ and_(temp, 0x0000ffff);
5379 __ cmp(chars, Operand(temp));
5380 __ j(equal, &found_in_symbol_table);
5381 __ bind(&next_probe_pop_mask[i]);
5382 __ pop(mask);
5383 __ bind(&next_probe[i]);
5384 }
5385
5386 // No matching 2 character string found by probing.
5387 __ jmp(not_found);
5388
5389 // Scratch register contains result when we fall through to here.
5390 Register result = scratch;
5391 __ bind(&found_in_symbol_table);
5392 __ pop(mask); // Pop saved mask from the stack.
5393 if (!result.is(eax)) {
5394 __ mov(eax, result);
5395 }
5396}
5397
5398
5399void StringHelper::GenerateHashInit(MacroAssembler* masm,
5400 Register hash,
5401 Register character,
5402 Register scratch) {
5403 // hash = character + (character << 10);
5404 __ mov(hash, character);
5405 __ shl(hash, 10);
5406 __ add(hash, Operand(character));
5407 // hash ^= hash >> 6;
5408 __ mov(scratch, hash);
5409 __ sar(scratch, 6);
5410 __ xor_(hash, Operand(scratch));
5411}
5412
5413
5414void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5415 Register hash,
5416 Register character,
5417 Register scratch) {
5418 // hash += character;
5419 __ add(hash, Operand(character));
5420 // hash += hash << 10;
5421 __ mov(scratch, hash);
5422 __ shl(scratch, 10);
5423 __ add(hash, Operand(scratch));
5424 // hash ^= hash >> 6;
5425 __ mov(scratch, hash);
5426 __ sar(scratch, 6);
5427 __ xor_(hash, Operand(scratch));
5428}
5429
5430
5431void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5432 Register hash,
5433 Register scratch) {
5434 // hash += hash << 3;
5435 __ mov(scratch, hash);
5436 __ shl(scratch, 3);
5437 __ add(hash, Operand(scratch));
5438 // hash ^= hash >> 11;
5439 __ mov(scratch, hash);
5440 __ sar(scratch, 11);
5441 __ xor_(hash, Operand(scratch));
5442 // hash += hash << 15;
5443 __ mov(scratch, hash);
5444 __ shl(scratch, 15);
5445 __ add(hash, Operand(scratch));
5446
5447 // if (hash == 0) hash = 27;
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005448 NearLabel hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005449 __ test(hash, Operand(hash));
5450 __ j(not_zero, &hash_not_zero);
5451 __ mov(hash, Immediate(27));
5452 __ bind(&hash_not_zero);
5453}
5454
5455
5456void SubStringStub::Generate(MacroAssembler* masm) {
5457 Label runtime;
5458
5459 // Stack frame on entry.
5460 // esp[0]: return address
5461 // esp[4]: to
5462 // esp[8]: from
5463 // esp[12]: string
5464
5465 // Make sure first argument is a string.
5466 __ mov(eax, Operand(esp, 3 * kPointerSize));
5467 STATIC_ASSERT(kSmiTag == 0);
5468 __ test(eax, Immediate(kSmiTagMask));
5469 __ j(zero, &runtime);
5470 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5471 __ j(NegateCondition(is_string), &runtime);
5472
5473 // eax: string
5474 // ebx: instance type
5475
5476 // Calculate length of sub string using the smi values.
5477 Label result_longer_than_two;
5478 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
5479 __ test(ecx, Immediate(kSmiTagMask));
5480 __ j(not_zero, &runtime);
5481 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
5482 __ test(edx, Immediate(kSmiTagMask));
5483 __ j(not_zero, &runtime);
5484 __ sub(ecx, Operand(edx));
5485 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5486 Label return_eax;
5487 __ j(equal, &return_eax);
5488 // Special handling of sub-strings of length 1 and 2. One character strings
5489 // are handled in the runtime system (looked up in the single character
5490 // cache). Two character strings are looked for in the symbol cache.
5491 __ SmiUntag(ecx); // Result length is no longer smi.
5492 __ cmp(ecx, 2);
5493 __ j(greater, &result_longer_than_two);
5494 __ j(less, &runtime);
5495
5496 // Sub string of length 2 requested.
5497 // eax: string
5498 // ebx: instance type
5499 // ecx: sub string length (value is 2)
5500 // edx: from index (smi)
5501 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5502
5503 // Get the two characters forming the sub string.
5504 __ SmiUntag(edx); // From index is no longer smi.
5505 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5506 __ movzx_b(ecx,
5507 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5508
5509 // Try to lookup two character string in symbol table.
5510 Label make_two_character_string;
5511 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005512 masm, ebx, ecx, eax, edx, edi,
5513 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005514 __ ret(3 * kPointerSize);
5515
5516 __ bind(&make_two_character_string);
5517 // Setup registers for allocating the two character string.
5518 __ mov(eax, Operand(esp, 3 * kPointerSize));
5519 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5520 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5521 __ Set(ecx, Immediate(2));
5522
5523 __ bind(&result_longer_than_two);
5524 // eax: string
5525 // ebx: instance type
5526 // ecx: result string length
5527 // Check for flat ascii string
5528 Label non_ascii_flat;
5529 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5530
5531 // Allocate the result.
5532 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5533
5534 // eax: result string
5535 // ecx: result string length
5536 __ mov(edx, esi); // esi used by following code.
5537 // Locate first character of result.
5538 __ mov(edi, eax);
5539 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5540 // Load string argument and locate character of sub string start.
5541 __ mov(esi, Operand(esp, 3 * kPointerSize));
5542 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5543 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5544 __ SmiUntag(ebx);
5545 __ add(esi, Operand(ebx));
5546
5547 // eax: result string
5548 // ecx: result length
5549 // edx: original value of esi
5550 // edi: first character of result
5551 // esi: character of sub string start
5552 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5553 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005554 Counters* counters = masm->isolate()->counters();
5555 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005556 __ ret(3 * kPointerSize);
5557
5558 __ bind(&non_ascii_flat);
5559 // eax: string
5560 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5561 // ecx: result string length
5562 // Check for flat two byte string
5563 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5564 __ j(not_equal, &runtime);
5565
5566 // Allocate the result.
5567 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5568
5569 // eax: result string
5570 // ecx: result string length
5571 __ mov(edx, esi); // esi used by following code.
5572 // Locate first character of result.
5573 __ mov(edi, eax);
5574 __ add(Operand(edi),
5575 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5576 // Load string argument and locate character of sub string start.
5577 __ mov(esi, Operand(esp, 3 * kPointerSize));
5578 __ add(Operand(esi),
5579 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5580 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5581 // As from is a smi it is 2 times the value which matches the size of a two
5582 // byte character.
5583 STATIC_ASSERT(kSmiTag == 0);
5584 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5585 __ add(esi, Operand(ebx));
5586
5587 // eax: result string
5588 // ecx: result length
5589 // edx: original value of esi
5590 // edi: first character of result
5591 // esi: character of sub string start
5592 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5593 __ mov(esi, edx); // Restore esi.
5594
5595 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005596 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005597 __ ret(3 * kPointerSize);
5598
5599 // Just jump to runtime to create the sub string.
5600 __ bind(&runtime);
5601 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5602}
5603
5604
5605void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5606 Register left,
5607 Register right,
5608 Register scratch1,
5609 Register scratch2,
5610 Register scratch3) {
5611 Label result_not_equal;
5612 Label result_greater;
5613 Label compare_lengths;
5614
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005615 Counters* counters = masm->isolate()->counters();
5616 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005617
5618 // Find minimum length.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005619 NearLabel left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005620 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5621 __ mov(scratch3, scratch1);
5622 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5623
5624 Register length_delta = scratch3;
5625
5626 __ j(less_equal, &left_shorter);
5627 // Right string is shorter. Change scratch1 to be length of right string.
5628 __ sub(scratch1, Operand(length_delta));
5629 __ bind(&left_shorter);
5630
5631 Register min_length = scratch1;
5632
5633 // If either length is zero, just compare lengths.
5634 __ test(min_length, Operand(min_length));
5635 __ j(zero, &compare_lengths);
5636
5637 // Change index to run from -min_length to -1 by adding min_length
5638 // to string start. This means that loop ends when index reaches zero,
5639 // which doesn't need an additional compare.
5640 __ SmiUntag(min_length);
5641 __ lea(left,
5642 FieldOperand(left,
5643 min_length, times_1,
5644 SeqAsciiString::kHeaderSize));
5645 __ lea(right,
5646 FieldOperand(right,
5647 min_length, times_1,
5648 SeqAsciiString::kHeaderSize));
5649 __ neg(min_length);
5650
5651 Register index = min_length; // index = -min_length;
5652
5653 {
5654 // Compare loop.
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005655 NearLabel loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005656 __ bind(&loop);
5657 // Compare characters.
5658 __ mov_b(scratch2, Operand(left, index, times_1, 0));
5659 __ cmpb(scratch2, Operand(right, index, times_1, 0));
5660 __ j(not_equal, &result_not_equal);
5661 __ add(Operand(index), Immediate(1));
5662 __ j(not_zero, &loop);
5663 }
5664
5665 // Compare lengths - strings up to min-length are equal.
5666 __ bind(&compare_lengths);
5667 __ test(length_delta, Operand(length_delta));
5668 __ j(not_zero, &result_not_equal);
5669
5670 // Result is EQUAL.
5671 STATIC_ASSERT(EQUAL == 0);
5672 STATIC_ASSERT(kSmiTag == 0);
5673 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5674 __ ret(0);
5675
5676 __ bind(&result_not_equal);
5677 __ j(greater, &result_greater);
5678
5679 // Result is LESS.
5680 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5681 __ ret(0);
5682
5683 // Result is GREATER.
5684 __ bind(&result_greater);
5685 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5686 __ ret(0);
5687}
5688
5689
5690void StringCompareStub::Generate(MacroAssembler* masm) {
5691 Label runtime;
5692
5693 // Stack frame on entry.
5694 // esp[0]: return address
5695 // esp[4]: right string
5696 // esp[8]: left string
5697
5698 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5699 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5700
whesse@chromium.org4a1fe7d2010-09-27 12:32:04 +00005701 NearLabel not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005702 __ cmp(edx, Operand(eax));
5703 __ j(not_equal, &not_same);
5704 STATIC_ASSERT(EQUAL == 0);
5705 STATIC_ASSERT(kSmiTag == 0);
5706 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005707 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005708 __ ret(2 * kPointerSize);
5709
5710 __ bind(&not_same);
5711
5712 // Check that both objects are sequential ascii strings.
5713 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5714
5715 // Compare flat ascii strings.
5716 // Drop arguments from the stack.
5717 __ pop(ecx);
5718 __ add(Operand(esp), Immediate(2 * kPointerSize));
5719 __ push(ecx);
5720 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5721
5722 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5723 // tagged as a small integer.
5724 __ bind(&runtime);
5725 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5726}
5727
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005728
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005729void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5730 ASSERT(state_ == CompareIC::SMIS);
5731 NearLabel miss;
5732 __ mov(ecx, Operand(edx));
5733 __ or_(ecx, Operand(eax));
5734 __ test(ecx, Immediate(kSmiTagMask));
5735 __ j(not_zero, &miss, not_taken);
5736
5737 if (GetCondition() == equal) {
5738 // For equality we do not care about the sign of the result.
5739 __ sub(eax, Operand(edx));
5740 } else {
5741 NearLabel done;
5742 __ sub(edx, Operand(eax));
5743 __ j(no_overflow, &done);
5744 // Correct sign of result in case of overflow.
5745 __ not_(edx);
5746 __ bind(&done);
5747 __ mov(eax, edx);
5748 }
5749 __ ret(0);
5750
5751 __ bind(&miss);
5752 GenerateMiss(masm);
5753}
5754
5755
5756void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5757 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5758
5759 NearLabel generic_stub;
5760 NearLabel unordered;
5761 NearLabel miss;
5762 __ mov(ecx, Operand(edx));
5763 __ and_(ecx, Operand(eax));
5764 __ test(ecx, Immediate(kSmiTagMask));
5765 __ j(zero, &generic_stub, not_taken);
5766
5767 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
5768 __ j(not_equal, &miss, not_taken);
5769 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
5770 __ j(not_equal, &miss, not_taken);
5771
5772 // Inlining the double comparison and falling back to the general compare
5773 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005774 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005775 CpuFeatures::Scope scope1(SSE2);
5776 CpuFeatures::Scope scope2(CMOV);
5777
5778 // Load left and right operand
5779 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5780 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5781
5782 // Compare operands
5783 __ ucomisd(xmm0, xmm1);
5784
5785 // Don't base result on EFLAGS when a NaN is involved.
5786 __ j(parity_even, &unordered, not_taken);
5787
5788 // Return a result of -1, 0, or 1, based on EFLAGS.
5789 // Performing mov, because xor would destroy the flag register.
5790 __ mov(eax, 0); // equal
5791 __ mov(ecx, Immediate(Smi::FromInt(1)));
5792 __ cmov(above, eax, Operand(ecx));
5793 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5794 __ cmov(below, eax, Operand(ecx));
5795 __ ret(0);
5796
5797 __ bind(&unordered);
5798 }
5799
5800 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5801 __ bind(&generic_stub);
5802 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5803
5804 __ bind(&miss);
5805 GenerateMiss(masm);
5806}
5807
5808
5809void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5810 ASSERT(state_ == CompareIC::OBJECTS);
5811 NearLabel miss;
5812 __ mov(ecx, Operand(edx));
5813 __ and_(ecx, Operand(eax));
5814 __ test(ecx, Immediate(kSmiTagMask));
5815 __ j(zero, &miss, not_taken);
5816
5817 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
5818 __ j(not_equal, &miss, not_taken);
5819 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
5820 __ j(not_equal, &miss, not_taken);
5821
5822 ASSERT(GetCondition() == equal);
5823 __ sub(eax, Operand(edx));
5824 __ ret(0);
5825
5826 __ bind(&miss);
5827 GenerateMiss(masm);
5828}
5829
5830
5831void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
5832 // Save the registers.
5833 __ pop(ecx);
5834 __ push(edx);
5835 __ push(eax);
5836 __ push(ecx);
5837
5838 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005839 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
5840 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005841 __ EnterInternalFrame();
5842 __ push(edx);
5843 __ push(eax);
5844 __ push(Immediate(Smi::FromInt(op_)));
5845 __ CallExternalReference(miss, 3);
5846 __ LeaveInternalFrame();
5847
5848 // Compute the entry point of the rewritten stub.
5849 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
5850
5851 // Restore registers.
5852 __ pop(ecx);
5853 __ pop(eax);
5854 __ pop(edx);
5855 __ push(ecx);
5856
5857 // Do a tail call to the rewritten stub.
5858 __ jmp(Operand(edi));
5859}
5860
5861
ricow@chromium.org65fae842010-08-25 15:26:24 +00005862#undef __
5863
5864} } // namespace v8::internal
5865
5866#endif // V8_TARGET_ARCH_IA32