blob: 534c18753b270f4d98e77d1d62cce01b19212d9a [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_IA32)
31
32#include "bootstrapper.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000033#include "code-stubs.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000034#include "isolate.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000035#include "jsregexp.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
37
38namespace v8 {
39namespace internal {
40
41#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +000042
43void ToNumberStub::Generate(MacroAssembler* masm) {
44 // The ToNumber stub takes one argument in eax.
karlklose@chromium.org83a47282011-05-11 11:54:09 +000045 Label check_heap_number, call_builtin;
whesse@chromium.org7a392b32011-01-31 11:30:36 +000046 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000047 __ j(not_zero, &check_heap_number, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000048 __ ret(0);
49
50 __ bind(&check_heap_number);
51 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000052 Factory* factory = masm->isolate()->factory();
53 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +000054 __ j(not_equal, &call_builtin, Label::kNear);
whesse@chromium.org7a392b32011-01-31 11:30:36 +000055 __ ret(0);
56
57 __ bind(&call_builtin);
58 __ pop(ecx); // Pop return address.
59 __ push(eax);
60 __ push(ecx); // Push return address.
61 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
62}
63
64
ricow@chromium.org65fae842010-08-25 15:26:24 +000065void FastNewClosureStub::Generate(MacroAssembler* masm) {
66 // Create a new closure from the given function info in new
67 // space. Set the context to the current context in esi.
68 Label gc;
69 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
70
71 // Get the function info from the stack.
72 __ mov(edx, Operand(esp, 1 * kPointerSize));
73
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 int map_index = strict_mode_ == kStrictMode
75 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
76 : Context::FUNCTION_MAP_INDEX;
77
ricow@chromium.org65fae842010-08-25 15:26:24 +000078 // Compute the function map in the current global context and set that
79 // as the map of the allocated object.
80 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
81 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 __ mov(ecx, Operand(ecx, Context::SlotOffset(map_index)));
ricow@chromium.org65fae842010-08-25 15:26:24 +000083 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
84
85 // Initialize the rest of the function. We don't have to update the
86 // write barrier because the allocated object is in new space.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000087 Factory* factory = masm->isolate()->factory();
88 __ mov(ebx, Immediate(factory->empty_fixed_array()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000089 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
90 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
91 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000092 Immediate(factory->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000093 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
94 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
95 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +000096 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +000097 Immediate(factory->undefined_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +000098
99 // Initialize the code pointer in the function to be the one
100 // found in the shared function info object.
101 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
102 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
103 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
104
105 // Return and remove the on-stack parameter.
106 __ ret(1 * kPointerSize);
107
108 // Create a new closure through the slower runtime call.
109 __ bind(&gc);
110 __ pop(ecx); // Temporarily remove return address.
111 __ pop(edx);
112 __ push(esi);
113 __ push(edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000114 __ push(Immediate(factory->false_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000115 __ push(ecx); // Restore return address.
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000116 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000117}
118
119
120void FastNewContextStub::Generate(MacroAssembler* masm) {
121 // Try to allocate the context in new space.
122 Label gc;
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000123 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
124 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
ricow@chromium.org65fae842010-08-25 15:26:24 +0000125 eax, ebx, ecx, &gc, TAG_OBJECT);
126
127 // Get the function from the stack.
128 __ mov(ecx, Operand(esp, 1 * kPointerSize));
129
130 // Setup the object header.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000131 Factory* factory = masm->isolate()->factory();
132 __ mov(FieldOperand(eax, HeapObject::kMapOffset), factory->context_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000133 __ mov(FieldOperand(eax, Context::kLengthOffset),
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000134 Immediate(Smi::FromInt(length)));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000135
136 // Setup the fixed slots.
lrn@chromium.org5d00b602011-01-05 09:51:43 +0000137 __ Set(ebx, Immediate(0)); // Set to NULL.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000138 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
139 __ mov(Operand(eax, Context::SlotOffset(Context::FCONTEXT_INDEX)), eax);
140 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), ebx);
141 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
142
143 // Copy the global object from the surrounding context. We go through the
144 // context in the function (ecx) to match the allocation behavior we have
145 // in the runtime system (see Heap::AllocateFunctionContext).
146 __ mov(ebx, FieldOperand(ecx, JSFunction::kContextOffset));
147 __ mov(ebx, Operand(ebx, Context::SlotOffset(Context::GLOBAL_INDEX)));
148 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
149
150 // Initialize the rest of the slots to undefined.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000151 __ mov(ebx, factory->undefined_value());
ager@chromium.org0ee099b2011-01-25 14:06:47 +0000152 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
ricow@chromium.org65fae842010-08-25 15:26:24 +0000153 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
154 }
155
156 // Return and remove the on-stack parameter.
157 __ mov(esi, Operand(eax));
158 __ ret(1 * kPointerSize);
159
160 // Need to collect. Call into runtime system.
161 __ bind(&gc);
162 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
163}
164
165
166void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
167 // Stack layout on entry:
168 //
169 // [esp + kPointerSize]: constant elements.
170 // [esp + (2 * kPointerSize)]: literal index.
171 // [esp + (3 * kPointerSize)]: literals array.
172
173 // All sizes here are multiples of kPointerSize.
174 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
175 int size = JSArray::kSize + elements_size;
176
177 // Load boilerplate object into ecx and check if we need to create a
178 // boilerplate.
179 Label slow_case;
180 __ mov(ecx, Operand(esp, 3 * kPointerSize));
181 __ mov(eax, Operand(esp, 2 * kPointerSize));
182 STATIC_ASSERT(kPointerSize == 4);
183 STATIC_ASSERT(kSmiTagSize == 1);
184 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000185 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
186 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000187 Factory* factory = masm->isolate()->factory();
188 __ cmp(ecx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000189 __ j(equal, &slow_case);
190
191 if (FLAG_debug_code) {
192 const char* message;
193 Handle<Map> expected_map;
194 if (mode_ == CLONE_ELEMENTS) {
195 message = "Expected (writable) fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000196 expected_map = factory->fixed_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000197 } else {
198 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
199 message = "Expected copy-on-write fixed array";
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000200 expected_map = factory->fixed_cow_array_map();
ricow@chromium.org65fae842010-08-25 15:26:24 +0000201 }
202 __ push(ecx);
203 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
204 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
205 __ Assert(equal, message);
206 __ pop(ecx);
207 }
208
209 // Allocate both the JS array and the elements array in one big
210 // allocation. This avoids multiple limit checks.
211 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
212
213 // Copy the JS array part.
214 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
215 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
216 __ mov(ebx, FieldOperand(ecx, i));
217 __ mov(FieldOperand(eax, i), ebx);
218 }
219 }
220
221 if (length_ > 0) {
222 // Get hold of the elements array of the boilerplate and setup the
223 // elements pointer in the resulting object.
224 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
225 __ lea(edx, Operand(eax, JSArray::kSize));
226 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
227
228 // Copy the elements array.
229 for (int i = 0; i < elements_size; i += kPointerSize) {
230 __ mov(ebx, FieldOperand(ecx, i));
231 __ mov(FieldOperand(edx, i), ebx);
232 }
233 }
234
235 // Return and remove the on-stack parameters.
236 __ ret(3 * kPointerSize);
237
238 __ bind(&slow_case);
239 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
240}
241
242
243// NOTE: The stub does not handle the inlined cases (Smis, Booleans, undefined).
244void ToBooleanStub::Generate(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000245 Label false_result, true_result, not_string;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000246 __ mov(eax, Operand(esp, 1 * kPointerSize));
247
248 // 'null' => false.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000249 Factory* factory = masm->isolate()->factory();
250 __ cmp(eax, factory->null_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000251 __ j(equal, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000252
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);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000260 __ j(not_zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000261
262 // JavaScript object => true.
263 __ CmpInstanceType(edx, FIRST_JS_OBJECT_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000264 __ j(above_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000265
266 // String value => false iff empty.
267 __ CmpInstanceType(edx, FIRST_NONSTRING_TYPE);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000268 __ j(above_equal, &not_string, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000269 STATIC_ASSERT(kSmiTag == 0);
270 __ cmp(FieldOperand(eax, String::kLengthOffset), Immediate(0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000271 __ j(zero, &false_result, Label::kNear);
272 __ jmp(&true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000273
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());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000277 __ j(not_equal, &true_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000278 __ fldz();
279 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
280 __ FCmp();
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000281 __ j(zero, &false_result, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000282 // 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.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000334 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
335 bool use_sse3,
336 Label* operand_conversion_failure);
337
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000338 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
339 // operands are pushed on the stack, and that their conversions to int32
340 // are in eax and ecx. Checks that the original numbers were in the int32
341 // range.
342 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
343 bool use_sse3,
344 Label* not_int32);
345
346 // Assumes that operands are smis or heap numbers and loads them
347 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000348 // Leaves operands unchanged.
349 static void LoadSSE2Operands(MacroAssembler* masm);
350
351 // Test if operands are numbers (smi or HeapNumber objects), and load
352 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
353 // either operand is not a number. Operands are in edx and eax.
354 // Leaves operands unchanged.
355 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
356
357 // Similar to LoadSSE2Operands but assumes that both operands are smis.
358 // Expects operands in edx, eax.
359 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000360
361 // Checks that the two floating point numbers loaded into xmm0 and xmm1
362 // have int32 values.
363 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
364 Label* non_int32,
365 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000366};
367
368
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000369// Get the integer part of a heap number. Surprisingly, all this bit twiddling
370// is faster than using the built-in instructions on floating point registers.
371// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
372// trashed registers.
373static void IntegerConvert(MacroAssembler* masm,
374 Register source,
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000375 bool use_sse3,
376 Label* conversion_failure) {
377 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
378 Label done, right_exponent, normal_exponent;
379 Register scratch = ebx;
380 Register scratch2 = edi;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000381 // Get exponent word.
382 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
383 // Get exponent alone in scratch2.
384 __ mov(scratch2, scratch);
385 __ and_(scratch2, HeapNumber::kExponentMask);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000386 if (use_sse3) {
387 CpuFeatures::Scope scope(SSE3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000388 // Check whether the exponent is too big for a 64 bit signed integer.
389 static const uint32_t kTooBigExponent =
390 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
391 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
392 __ j(greater_equal, conversion_failure);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000393 // Load x87 register with heap number.
394 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
395 // Reserve space for 64 bit answer.
396 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
397 // Do conversion, which cannot fail because we checked the exponent.
398 __ fisttp_d(Operand(esp, 0));
399 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
400 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
401 } else {
402 // Load ecx with zero. We use this either for the final shift or
403 // for the answer.
404 __ xor_(ecx, Operand(ecx));
405 // Check whether the exponent matches a 32 bit signed int that cannot be
406 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
407 // exponent is 30 (biased). This is the exponent that we are fastest at and
408 // also the highest exponent we can handle here.
409 const uint32_t non_smi_exponent =
410 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
411 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
412 // If we have a match of the int32-but-not-Smi exponent then skip some
413 // logic.
414 __ j(equal, &right_exponent);
415 // If the exponent is higher than that then go to slow case. This catches
416 // numbers that don't fit in a signed int32, infinities and NaNs.
417 __ j(less, &normal_exponent);
418
419 {
420 // Handle a big exponent. The only reason we have this code is that the
421 // >>> operator has a tendency to generate numbers with an exponent of 31.
422 const uint32_t big_non_smi_exponent =
423 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
424 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
425 __ j(not_equal, conversion_failure);
426 // We have the big exponent, typically from >>>. This means the number is
427 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
428 __ mov(scratch2, scratch);
429 __ and_(scratch2, HeapNumber::kMantissaMask);
430 // Put back the implicit 1.
431 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
432 // Shift up the mantissa bits to take up the space the exponent used to
433 // take. We just orred in the implicit bit so that took care of one and
434 // we want to use the full unsigned range so we subtract 1 bit from the
435 // shift distance.
436 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
437 __ shl(scratch2, big_shift_distance);
438 // Get the second half of the double.
439 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
440 // Shift down 21 bits to get the most significant 11 bits or the low
441 // mantissa word.
442 __ shr(ecx, 32 - big_shift_distance);
443 __ or_(ecx, Operand(scratch2));
444 // We have the answer in ecx, but we may need to negate it.
445 __ test(scratch, Operand(scratch));
446 __ j(positive, &done);
447 __ neg(ecx);
448 __ jmp(&done);
449 }
450
451 __ bind(&normal_exponent);
452 // Exponent word in scratch, exponent part of exponent word in scratch2.
453 // Zero in ecx.
454 // We know the exponent is smaller than 30 (biased). If it is less than
455 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
456 // it rounds to zero.
457 const uint32_t zero_exponent =
458 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
459 __ sub(Operand(scratch2), Immediate(zero_exponent));
460 // ecx already has a Smi zero.
461 __ j(less, &done);
462
463 // We have a shifted exponent between 0 and 30 in scratch2.
464 __ shr(scratch2, HeapNumber::kExponentShift);
465 __ mov(ecx, Immediate(30));
466 __ sub(ecx, Operand(scratch2));
467
468 __ bind(&right_exponent);
469 // Here ecx is the shift, scratch is the exponent word.
470 // Get the top bits of the mantissa.
471 __ and_(scratch, HeapNumber::kMantissaMask);
472 // Put back the implicit 1.
473 __ or_(scratch, 1 << HeapNumber::kExponentShift);
474 // Shift up the mantissa bits to take up the space the exponent used to
475 // take. We have kExponentShift + 1 significant bits int he low end of the
476 // word. Shift them to the top bits.
477 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
478 __ shl(scratch, shift_distance);
479 // Get the second half of the double. For some exponents we don't
480 // actually need this because the bits get shifted out again, but
481 // it's probably slower to test than just to do it.
482 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
483 // Shift down 22 bits to get the most significant 10 bits or the low
484 // mantissa word.
485 __ shr(scratch2, 32 - shift_distance);
486 __ or_(scratch2, Operand(scratch));
487 // Move down according to the exponent.
488 __ shr_cl(scratch2);
489 // Now the unsigned answer is in scratch2. We need to move it to ecx and
490 // we may need to fix the sign.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000491 Label negative;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000492 __ xor_(ecx, Operand(ecx));
493 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000494 __ j(greater, &negative, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000495 __ mov(ecx, scratch2);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000496 __ jmp(&done, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000497 __ bind(&negative);
498 __ sub(ecx, Operand(scratch2));
499 __ bind(&done);
500 }
501}
502
503
504Handle<Code> GetTypeRecordingUnaryOpStub(int key,
505 TRUnaryOpIC::TypeInfo type_info) {
506 TypeRecordingUnaryOpStub stub(key, type_info);
507 return stub.GetCode();
508}
509
510
511const char* TypeRecordingUnaryOpStub::GetName() {
512 if (name_ != NULL) return name_;
513 const int kMaxNameLength = 100;
514 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
515 kMaxNameLength);
516 if (name_ == NULL) return "OOM";
517 const char* op_name = Token::Name(op_);
518 const char* overwrite_name = NULL; // Make g++ happy.
519 switch (mode_) {
520 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
521 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
522 }
523
524 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
525 "TypeRecordingUnaryOpStub_%s_%s_%s",
526 op_name,
527 overwrite_name,
528 TRUnaryOpIC::GetName(operand_type_));
529 return name_;
530}
531
532
533// TODO(svenpanne): Use virtual functions instead of switch.
534void TypeRecordingUnaryOpStub::Generate(MacroAssembler* masm) {
535 switch (operand_type_) {
536 case TRUnaryOpIC::UNINITIALIZED:
537 GenerateTypeTransition(masm);
538 break;
539 case TRUnaryOpIC::SMI:
540 GenerateSmiStub(masm);
541 break;
542 case TRUnaryOpIC::HEAP_NUMBER:
543 GenerateHeapNumberStub(masm);
544 break;
545 case TRUnaryOpIC::GENERIC:
546 GenerateGenericStub(masm);
547 break;
548 }
549}
550
551
552void TypeRecordingUnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
553 __ pop(ecx); // Save return address.
554 __ push(eax);
555 // the argument is now on top.
556 // Push this stub's key. Although the operation and the type info are
557 // encoded into the key, the encoding is opaque, so push them too.
558 __ push(Immediate(Smi::FromInt(MinorKey())));
559 __ push(Immediate(Smi::FromInt(op_)));
560 __ push(Immediate(Smi::FromInt(operand_type_)));
561
562 __ push(ecx); // Push return address.
563
564 // Patch the caller to an appropriate specialized stub and return the
565 // operation result to the caller of the stub.
566 __ TailCallExternalReference(
567 ExternalReference(IC_Utility(IC::kTypeRecordingUnaryOp_Patch),
568 masm->isolate()),
569 4,
570 1);
571}
572
573
574// TODO(svenpanne): Use virtual functions instead of switch.
575void TypeRecordingUnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
576 switch (op_) {
577 case Token::SUB:
578 GenerateSmiStubSub(masm);
579 break;
580 case Token::BIT_NOT:
581 GenerateSmiStubBitNot(masm);
582 break;
583 default:
584 UNREACHABLE();
585 }
586}
587
588
589void TypeRecordingUnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000590 Label non_smi, undo, slow;
591 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
592 Label::kNear, Label::kNear, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000593 __ bind(&undo);
594 GenerateSmiCodeUndo(masm);
595 __ bind(&non_smi);
596 __ bind(&slow);
597 GenerateTypeTransition(masm);
598}
599
600
601void TypeRecordingUnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000602 Label non_smi;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000603 GenerateSmiCodeBitNot(masm, &non_smi);
604 __ bind(&non_smi);
605 GenerateTypeTransition(masm);
606}
607
608
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000609void TypeRecordingUnaryOpStub::GenerateSmiCodeSub(
610 MacroAssembler* masm, Label* non_smi, Label* undo, Label* slow,
611 Label::Distance non_smi_near, Label::Distance undo_near,
612 Label::Distance slow_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000613 // Check whether the value is a smi.
614 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000615 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000616
617 // We can't handle -0 with smis, so use a type transition for that case.
618 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000619 __ j(zero, slow, slow_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000620
621 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
622 __ mov(edx, Operand(eax));
623 __ Set(eax, Immediate(0));
624 __ sub(eax, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000625 __ j(overflow, undo, undo_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000626 __ ret(0);
627}
628
629
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000630void TypeRecordingUnaryOpStub::GenerateSmiCodeBitNot(
631 MacroAssembler* masm,
632 Label* non_smi,
633 Label::Distance non_smi_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000634 // Check whether the value is a smi.
635 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000636 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000637
638 // Flip bits and revert inverted smi-tag.
639 __ not_(eax);
640 __ and_(eax, ~kSmiTagMask);
641 __ ret(0);
642}
643
644
645void TypeRecordingUnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
646 __ mov(eax, Operand(edx));
647}
648
649
650// TODO(svenpanne): Use virtual functions instead of switch.
651void TypeRecordingUnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
652 switch (op_) {
653 case Token::SUB:
654 GenerateHeapNumberStubSub(masm);
655 break;
656 case Token::BIT_NOT:
657 GenerateHeapNumberStubBitNot(masm);
658 break;
659 default:
660 UNREACHABLE();
661 }
662}
663
664
665void TypeRecordingUnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000666 Label non_smi, undo, slow;
667 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000668 __ bind(&non_smi);
669 GenerateHeapNumberCodeSub(masm, &slow);
670 __ bind(&undo);
671 GenerateSmiCodeUndo(masm);
672 __ bind(&slow);
673 GenerateTypeTransition(masm);
674}
675
676
677void TypeRecordingUnaryOpStub::GenerateHeapNumberStubBitNot(
678 MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000679 Label non_smi, slow;
680 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000681 __ bind(&non_smi);
682 GenerateHeapNumberCodeBitNot(masm, &slow);
683 __ bind(&slow);
684 GenerateTypeTransition(masm);
685}
686
687
688void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
689 Label* slow) {
690 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
691 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
692 __ j(not_equal, slow);
693
694 if (mode_ == UNARY_OVERWRITE) {
695 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
696 Immediate(HeapNumber::kSignMask)); // Flip sign.
697 } else {
698 __ mov(edx, Operand(eax));
699 // edx: operand
700
701 Label slow_allocate_heapnumber, heapnumber_allocated;
702 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
703 __ jmp(&heapnumber_allocated);
704
705 __ bind(&slow_allocate_heapnumber);
706 __ EnterInternalFrame();
707 __ push(edx);
708 __ CallRuntime(Runtime::kNumberAlloc, 0);
709 __ pop(edx);
710 __ LeaveInternalFrame();
711
712 __ bind(&heapnumber_allocated);
713 // eax: allocated 'empty' number
714 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
715 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
716 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
717 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
718 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
719 }
720 __ ret(0);
721}
722
723
724void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeBitNot(
725 MacroAssembler* masm,
726 Label* slow) {
727 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
728 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
729 __ j(not_equal, slow);
730
731 // Convert the heap number in eax to an untagged integer in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000732 IntegerConvert(masm, eax, CpuFeatures::IsSupported(SSE3), slow);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000733
734 // Do the bitwise operation and check if the result fits in a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000735 Label try_float;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000736 __ not_(ecx);
737 __ cmp(ecx, 0xc0000000);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000738 __ j(sign, &try_float, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000739
740 // Tag the result as a smi and we're done.
741 STATIC_ASSERT(kSmiTagSize == 1);
742 __ lea(eax, Operand(ecx, times_2, kSmiTag));
743 __ ret(0);
744
745 // Try to store the result in a heap number.
746 __ bind(&try_float);
747 if (mode_ == UNARY_NO_OVERWRITE) {
748 Label slow_allocate_heapnumber, heapnumber_allocated;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000749 __ mov(ebx, eax);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000750 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
751 __ jmp(&heapnumber_allocated);
752
753 __ bind(&slow_allocate_heapnumber);
754 __ EnterInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000755 // Push the original HeapNumber on the stack. The integer value can't
756 // be stored since it's untagged and not in the smi range (so we can't
757 // smi-tag it). We'll recalculate the value after the GC instead.
758 __ push(ebx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000759 __ CallRuntime(Runtime::kNumberAlloc, 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000760 // New HeapNumber is in eax.
761 __ pop(edx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000762 __ LeaveInternalFrame();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000763 // IntegerConvert uses ebx and edi as scratch registers.
764 // This conversion won't go slow-case.
765 IntegerConvert(masm, edx, CpuFeatures::IsSupported(SSE3), slow);
766 __ not_(ecx);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000767
768 __ bind(&heapnumber_allocated);
769 }
770 if (CpuFeatures::IsSupported(SSE2)) {
771 CpuFeatures::Scope use_sse2(SSE2);
772 __ cvtsi2sd(xmm0, Operand(ecx));
773 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
774 } else {
775 __ push(ecx);
776 __ fild_s(Operand(esp, 0));
777 __ pop(ecx);
778 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
779 }
780 __ ret(0);
781}
782
783
784// TODO(svenpanne): Use virtual functions instead of switch.
785void TypeRecordingUnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
786 switch (op_) {
787 case Token::SUB:
788 GenerateGenericStubSub(masm);
789 break;
790 case Token::BIT_NOT:
791 GenerateGenericStubBitNot(masm);
792 break;
793 default:
794 UNREACHABLE();
795 }
796}
797
798
799void TypeRecordingUnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000800 Label non_smi, undo, slow;
801 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000802 __ bind(&non_smi);
803 GenerateHeapNumberCodeSub(masm, &slow);
804 __ bind(&undo);
805 GenerateSmiCodeUndo(masm);
806 __ bind(&slow);
807 GenerateGenericCodeFallback(masm);
808}
809
810
811void TypeRecordingUnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000812 Label non_smi, slow;
813 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000814 __ bind(&non_smi);
815 GenerateHeapNumberCodeBitNot(masm, &slow);
816 __ bind(&slow);
817 GenerateGenericCodeFallback(masm);
818}
819
820
821void TypeRecordingUnaryOpStub::GenerateGenericCodeFallback(
822 MacroAssembler* masm) {
823 // Handle the slow case by jumping to the corresponding JavaScript builtin.
824 __ pop(ecx); // pop return address.
825 __ push(eax);
826 __ push(ecx); // push return address
827 switch (op_) {
828 case Token::SUB:
829 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
830 break;
831 case Token::BIT_NOT:
832 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
833 break;
834 default:
835 UNREACHABLE();
836 }
837}
838
839
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000840Handle<Code> GetTypeRecordingBinaryOpStub(int key,
841 TRBinaryOpIC::TypeInfo type_info,
842 TRBinaryOpIC::TypeInfo result_type_info) {
843 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
844 return stub.GetCode();
845}
846
847
848void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
849 __ pop(ecx); // Save return address.
850 __ push(edx);
851 __ push(eax);
852 // Left and right arguments are now on top.
853 // Push this stub's key. Although the operation and the type info are
854 // encoded into the key, the encoding is opaque, so push them too.
855 __ push(Immediate(Smi::FromInt(MinorKey())));
856 __ push(Immediate(Smi::FromInt(op_)));
857 __ push(Immediate(Smi::FromInt(operands_type_)));
858
859 __ push(ecx); // Push return address.
860
861 // Patch the caller to an appropriate specialized stub and return the
862 // operation result to the caller of the stub.
863 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000864 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
865 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000866 5,
867 1);
868}
869
870
871// Prepare for a type transition runtime call when the args are already on
872// the stack, under the return address.
873void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
874 MacroAssembler* masm) {
875 __ pop(ecx); // Save return address.
876 // Left and right arguments are already on top of the stack.
877 // Push this stub's key. Although the operation and the type info are
878 // encoded into the key, the encoding is opaque, so push them too.
879 __ push(Immediate(Smi::FromInt(MinorKey())));
880 __ push(Immediate(Smi::FromInt(op_)));
881 __ push(Immediate(Smi::FromInt(operands_type_)));
882
883 __ push(ecx); // Push return address.
884
885 // Patch the caller to an appropriate specialized stub and return the
886 // operation result to the caller of the stub.
887 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000888 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
889 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000890 5,
891 1);
892}
893
894
895void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
896 switch (operands_type_) {
897 case TRBinaryOpIC::UNINITIALIZED:
898 GenerateTypeTransition(masm);
899 break;
900 case TRBinaryOpIC::SMI:
901 GenerateSmiStub(masm);
902 break;
903 case TRBinaryOpIC::INT32:
904 GenerateInt32Stub(masm);
905 break;
906 case TRBinaryOpIC::HEAP_NUMBER:
907 GenerateHeapNumberStub(masm);
908 break;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000909 case TRBinaryOpIC::ODDBALL:
910 GenerateOddballStub(masm);
911 break;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000912 case TRBinaryOpIC::BOTH_STRING:
913 GenerateBothStringStub(masm);
914 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000915 case TRBinaryOpIC::STRING:
916 GenerateStringStub(masm);
917 break;
918 case TRBinaryOpIC::GENERIC:
919 GenerateGeneric(masm);
920 break;
921 default:
922 UNREACHABLE();
923 }
924}
925
926
927const char* TypeRecordingBinaryOpStub::GetName() {
928 if (name_ != NULL) return name_;
929 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000930 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
931 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000932 if (name_ == NULL) return "OOM";
933 const char* op_name = Token::Name(op_);
934 const char* overwrite_name;
935 switch (mode_) {
936 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
937 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
938 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
939 default: overwrite_name = "UnknownOverwrite"; break;
940 }
941
942 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
943 "TypeRecordingBinaryOpStub_%s_%s_%s",
944 op_name,
945 overwrite_name,
946 TRBinaryOpIC::GetName(operands_type_));
947 return name_;
948}
949
950
951void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
952 Label* slow,
953 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
954 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
955 // dividend in eax and edx free for the division. Use eax, ebx for those.
956 Comment load_comment(masm, "-- Load arguments");
957 Register left = edx;
958 Register right = eax;
959 if (op_ == Token::DIV || op_ == Token::MOD) {
960 left = eax;
961 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000962 __ mov(ebx, eax);
963 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000964 }
965
966
967 // 2. Prepare the smi check of both operands by oring them together.
968 Comment smi_check_comment(masm, "-- Smi check arguments");
969 Label not_smis;
970 Register combined = ecx;
971 ASSERT(!left.is(combined) && !right.is(combined));
972 switch (op_) {
973 case Token::BIT_OR:
974 // Perform the operation into eax and smi check the result. Preserve
975 // eax in case the result is not a smi.
976 ASSERT(!left.is(ecx) && !right.is(ecx));
977 __ mov(ecx, right);
978 __ or_(right, Operand(left)); // Bitwise or is commutative.
979 combined = right;
980 break;
981
982 case Token::BIT_XOR:
983 case Token::BIT_AND:
984 case Token::ADD:
985 case Token::SUB:
986 case Token::MUL:
987 case Token::DIV:
988 case Token::MOD:
989 __ mov(combined, right);
990 __ or_(combined, Operand(left));
991 break;
992
993 case Token::SHL:
994 case Token::SAR:
995 case Token::SHR:
996 // Move the right operand into ecx for the shift operation, use eax
997 // for the smi check register.
998 ASSERT(!left.is(ecx) && !right.is(ecx));
999 __ mov(ecx, right);
1000 __ or_(right, Operand(left));
1001 combined = right;
1002 break;
1003
1004 default:
1005 break;
1006 }
1007
1008 // 3. Perform the smi check of the operands.
1009 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1010 __ test(combined, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001011 __ j(not_zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001012
1013 // 4. Operands are both smis, perform the operation leaving the result in
1014 // eax and check the result if necessary.
1015 Comment perform_smi(masm, "-- Perform smi operation");
1016 Label use_fp_on_smis;
1017 switch (op_) {
1018 case Token::BIT_OR:
1019 // Nothing to do.
1020 break;
1021
1022 case Token::BIT_XOR:
1023 ASSERT(right.is(eax));
1024 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1025 break;
1026
1027 case Token::BIT_AND:
1028 ASSERT(right.is(eax));
1029 __ and_(right, Operand(left)); // Bitwise and is commutative.
1030 break;
1031
1032 case Token::SHL:
1033 // Remove tags from operands (but keep sign).
1034 __ SmiUntag(left);
1035 __ SmiUntag(ecx);
1036 // Perform the operation.
1037 __ shl_cl(left);
1038 // Check that the *signed* result fits in a smi.
1039 __ cmp(left, 0xc0000000);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001040 __ j(sign, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041 // Tag the result and store it in register eax.
1042 __ SmiTag(left);
1043 __ mov(eax, left);
1044 break;
1045
1046 case Token::SAR:
1047 // Remove tags from operands (but keep sign).
1048 __ SmiUntag(left);
1049 __ SmiUntag(ecx);
1050 // Perform the operation.
1051 __ sar_cl(left);
1052 // Tag the result and store it in register eax.
1053 __ SmiTag(left);
1054 __ mov(eax, left);
1055 break;
1056
1057 case Token::SHR:
1058 // Remove tags from operands (but keep sign).
1059 __ SmiUntag(left);
1060 __ SmiUntag(ecx);
1061 // Perform the operation.
1062 __ shr_cl(left);
1063 // Check that the *unsigned* result fits in a smi.
1064 // Neither of the two high-order bits can be set:
1065 // - 0x80000000: high bit would be lost when smi tagging.
1066 // - 0x40000000: this number would convert to negative when
1067 // Smi tagging these two cases can only happen with shifts
1068 // by 0 or 1 when handed a valid smi.
1069 __ test(left, Immediate(0xc0000000));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001070 __ j(not_zero, slow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 // Tag the result and store it in register eax.
1072 __ SmiTag(left);
1073 __ mov(eax, left);
1074 break;
1075
1076 case Token::ADD:
1077 ASSERT(right.is(eax));
1078 __ add(right, Operand(left)); // Addition is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001079 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001080 break;
1081
1082 case Token::SUB:
1083 __ sub(left, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001084 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001085 __ mov(eax, left);
1086 break;
1087
1088 case Token::MUL:
1089 // If the smi tag is 0 we can just leave the tag on one operand.
1090 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1091 // We can't revert the multiplication if the result is not a smi
1092 // so save the right operand.
1093 __ mov(ebx, right);
1094 // Remove tag from one of the operands (but keep sign).
1095 __ SmiUntag(right);
1096 // Do multiplication.
1097 __ imul(right, Operand(left)); // Multiplication is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001098 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001099 // Check for negative zero result. Use combined = left | right.
1100 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1101 break;
1102
1103 case Token::DIV:
1104 // We can't revert the division if the result is not a smi so
1105 // save the left operand.
1106 __ mov(edi, left);
1107 // Check for 0 divisor.
1108 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001109 __ j(zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001110 // Sign extend left into edx:eax.
1111 ASSERT(left.is(eax));
1112 __ cdq();
1113 // Divide edx:eax by right.
1114 __ idiv(right);
1115 // Check for the corner case of dividing the most negative smi by
1116 // -1. We cannot use the overflow flag, since it is not set by idiv
1117 // instruction.
1118 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1119 __ cmp(eax, 0x40000000);
1120 __ j(equal, &use_fp_on_smis);
1121 // Check for negative zero result. Use combined = left | right.
1122 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1123 // Check that the remainder is zero.
1124 __ test(edx, Operand(edx));
1125 __ j(not_zero, &use_fp_on_smis);
1126 // Tag the result and store it in register eax.
1127 __ SmiTag(eax);
1128 break;
1129
1130 case Token::MOD:
1131 // Check for 0 divisor.
1132 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001133 __ j(zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001134
1135 // Sign extend left into edx:eax.
1136 ASSERT(left.is(eax));
1137 __ cdq();
1138 // Divide edx:eax by right.
1139 __ idiv(right);
1140 // Check for negative zero result. Use combined = left | right.
1141 __ NegativeZeroTest(edx, combined, slow);
1142 // Move remainder to register eax.
1143 __ mov(eax, edx);
1144 break;
1145
1146 default:
1147 UNREACHABLE();
1148 }
1149
1150 // 5. Emit return of result in eax. Some operations have registers pushed.
1151 switch (op_) {
1152 case Token::ADD:
1153 case Token::SUB:
1154 case Token::MUL:
1155 case Token::DIV:
1156 __ ret(0);
1157 break;
1158 case Token::MOD:
1159 case Token::BIT_OR:
1160 case Token::BIT_AND:
1161 case Token::BIT_XOR:
1162 case Token::SAR:
1163 case Token::SHL:
1164 case Token::SHR:
1165 __ ret(2 * kPointerSize);
1166 break;
1167 default:
1168 UNREACHABLE();
1169 }
1170
1171 // 6. For some operations emit inline code to perform floating point
1172 // operations on known smis (e.g., if the result of the operation
1173 // overflowed the smi range).
1174 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1175 __ bind(&use_fp_on_smis);
1176 switch (op_) {
1177 // Undo the effects of some operations, and some register moves.
1178 case Token::SHL:
1179 // The arguments are saved on the stack, and only used from there.
1180 break;
1181 case Token::ADD:
1182 // Revert right = right + left.
1183 __ sub(right, Operand(left));
1184 break;
1185 case Token::SUB:
1186 // Revert left = left - right.
1187 __ add(left, Operand(right));
1188 break;
1189 case Token::MUL:
1190 // Right was clobbered but a copy is in ebx.
1191 __ mov(right, ebx);
1192 break;
1193 case Token::DIV:
1194 // Left was clobbered but a copy is in edi. Right is in ebx for
1195 // division. They should be in eax, ebx for jump to not_smi.
1196 __ mov(eax, edi);
1197 break;
1198 default:
1199 // No other operators jump to use_fp_on_smis.
1200 break;
1201 }
1202 __ jmp(&not_smis);
1203 } else {
1204 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1205 switch (op_) {
1206 case Token::SHL: {
1207 Comment perform_float(masm, "-- Perform float operation on smis");
1208 __ bind(&use_fp_on_smis);
1209 // Result we want is in left == edx, so we can put the allocated heap
1210 // number in eax.
1211 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1212 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001213 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001214 CpuFeatures::Scope use_sse2(SSE2);
1215 __ cvtsi2sd(xmm0, Operand(left));
1216 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1217 } else {
1218 // It's OK to overwrite the right argument on the stack because we
1219 // are about to return.
1220 __ mov(Operand(esp, 1 * kPointerSize), left);
1221 __ fild_s(Operand(esp, 1 * kPointerSize));
1222 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1223 }
1224 __ ret(2 * kPointerSize);
1225 break;
1226 }
1227
1228 case Token::ADD:
1229 case Token::SUB:
1230 case Token::MUL:
1231 case Token::DIV: {
1232 Comment perform_float(masm, "-- Perform float operation on smis");
1233 __ bind(&use_fp_on_smis);
1234 // Restore arguments to edx, eax.
1235 switch (op_) {
1236 case Token::ADD:
1237 // Revert right = right + left.
1238 __ sub(right, Operand(left));
1239 break;
1240 case Token::SUB:
1241 // Revert left = left - right.
1242 __ add(left, Operand(right));
1243 break;
1244 case Token::MUL:
1245 // Right was clobbered but a copy is in ebx.
1246 __ mov(right, ebx);
1247 break;
1248 case Token::DIV:
1249 // Left was clobbered but a copy is in edi. Right is in ebx for
1250 // division.
1251 __ mov(edx, edi);
1252 __ mov(eax, right);
1253 break;
1254 default: UNREACHABLE();
1255 break;
1256 }
1257 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001258 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001259 CpuFeatures::Scope use_sse2(SSE2);
1260 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1261 switch (op_) {
1262 case Token::ADD: __ addsd(xmm0, xmm1); break;
1263 case Token::SUB: __ subsd(xmm0, xmm1); break;
1264 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1265 case Token::DIV: __ divsd(xmm0, xmm1); break;
1266 default: UNREACHABLE();
1267 }
1268 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1269 } else { // SSE2 not available, use FPU.
1270 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1271 switch (op_) {
1272 case Token::ADD: __ faddp(1); break;
1273 case Token::SUB: __ fsubp(1); break;
1274 case Token::MUL: __ fmulp(1); break;
1275 case Token::DIV: __ fdivp(1); break;
1276 default: UNREACHABLE();
1277 }
1278 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1279 }
1280 __ mov(eax, ecx);
1281 __ ret(0);
1282 break;
1283 }
1284
1285 default:
1286 break;
1287 }
1288 }
1289
1290 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1291 // edx and eax.
1292 Comment done_comment(masm, "-- Enter non-smi code");
1293 __ bind(&not_smis);
1294 switch (op_) {
1295 case Token::BIT_OR:
1296 case Token::SHL:
1297 case Token::SAR:
1298 case Token::SHR:
1299 // Right operand is saved in ecx and eax was destroyed by the smi
1300 // check.
1301 __ mov(eax, ecx);
1302 break;
1303
1304 case Token::DIV:
1305 case Token::MOD:
1306 // Operands are in eax, ebx at this point.
1307 __ mov(edx, eax);
1308 __ mov(eax, ebx);
1309 break;
1310
1311 default:
1312 break;
1313 }
1314}
1315
1316
1317void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1318 Label call_runtime;
1319
1320 switch (op_) {
1321 case Token::ADD:
1322 case Token::SUB:
1323 case Token::MUL:
1324 case Token::DIV:
1325 break;
1326 case Token::MOD:
1327 case Token::BIT_OR:
1328 case Token::BIT_AND:
1329 case Token::BIT_XOR:
1330 case Token::SAR:
1331 case Token::SHL:
1332 case Token::SHR:
1333 GenerateRegisterArgsPush(masm);
1334 break;
1335 default:
1336 UNREACHABLE();
1337 }
1338
1339 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1340 result_type_ == TRBinaryOpIC::SMI) {
1341 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1342 } else {
1343 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1344 }
1345 __ bind(&call_runtime);
1346 switch (op_) {
1347 case Token::ADD:
1348 case Token::SUB:
1349 case Token::MUL:
1350 case Token::DIV:
1351 GenerateTypeTransition(masm);
1352 break;
1353 case Token::MOD:
1354 case Token::BIT_OR:
1355 case Token::BIT_AND:
1356 case Token::BIT_XOR:
1357 case Token::SAR:
1358 case Token::SHL:
1359 case Token::SHR:
1360 GenerateTypeTransitionWithSavedArgs(masm);
1361 break;
1362 default:
1363 UNREACHABLE();
1364 }
1365}
1366
1367
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001368void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001369 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1370 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001371 // Try to add arguments as strings, otherwise, transition to the generic
1372 // TRBinaryOpIC type.
1373 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001374 GenerateTypeTransition(masm);
1375}
1376
1377
danno@chromium.org160a7b02011-04-18 15:51:38 +00001378void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1379 Label call_runtime;
1380 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
1381 ASSERT(op_ == Token::ADD);
1382 // If both arguments are strings, call the string add stub.
1383 // Otherwise, do a transition.
1384
1385 // Registers containing left and right operands respectively.
1386 Register left = edx;
1387 Register right = eax;
1388
1389 // Test if left operand is a string.
1390 __ test(left, Immediate(kSmiTagMask));
1391 __ j(zero, &call_runtime);
1392 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1393 __ j(above_equal, &call_runtime);
1394
1395 // Test if right operand is a string.
1396 __ test(right, Immediate(kSmiTagMask));
1397 __ j(zero, &call_runtime);
1398 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1399 __ j(above_equal, &call_runtime);
1400
1401 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1402 GenerateRegisterArgsPush(masm);
1403 __ TailCallStub(&string_add_stub);
1404
1405 __ bind(&call_runtime);
1406 GenerateTypeTransition(masm);
1407}
1408
1409
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001410void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1411 Label call_runtime;
1412 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1413
1414 // Floating point case.
1415 switch (op_) {
1416 case Token::ADD:
1417 case Token::SUB:
1418 case Token::MUL:
1419 case Token::DIV: {
1420 Label not_floats;
1421 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001422 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001423 CpuFeatures::Scope use_sse2(SSE2);
1424 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1425 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1426 switch (op_) {
1427 case Token::ADD: __ addsd(xmm0, xmm1); break;
1428 case Token::SUB: __ subsd(xmm0, xmm1); break;
1429 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1430 case Token::DIV: __ divsd(xmm0, xmm1); break;
1431 default: UNREACHABLE();
1432 }
1433 // Check result type if it is currently Int32.
1434 if (result_type_ <= TRBinaryOpIC::INT32) {
1435 __ cvttsd2si(ecx, Operand(xmm0));
1436 __ cvtsi2sd(xmm2, Operand(ecx));
1437 __ ucomisd(xmm0, xmm2);
1438 __ j(not_zero, &not_int32);
1439 __ j(carry, &not_int32);
1440 }
1441 GenerateHeapResultAllocation(masm, &call_runtime);
1442 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1443 __ ret(0);
1444 } else { // SSE2 not available, use FPU.
1445 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1446 FloatingPointHelper::LoadFloatOperands(
1447 masm,
1448 ecx,
1449 FloatingPointHelper::ARGS_IN_REGISTERS);
1450 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1451 switch (op_) {
1452 case Token::ADD: __ faddp(1); break;
1453 case Token::SUB: __ fsubp(1); break;
1454 case Token::MUL: __ fmulp(1); break;
1455 case Token::DIV: __ fdivp(1); break;
1456 default: UNREACHABLE();
1457 }
1458 Label after_alloc_failure;
1459 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1460 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1461 __ ret(0);
1462 __ bind(&after_alloc_failure);
1463 __ ffree();
1464 __ jmp(&call_runtime);
1465 }
1466
1467 __ bind(&not_floats);
1468 __ bind(&not_int32);
1469 GenerateTypeTransition(masm);
1470 break;
1471 }
1472
1473 case Token::MOD: {
1474 // For MOD we go directly to runtime in the non-smi case.
1475 break;
1476 }
1477 case Token::BIT_OR:
1478 case Token::BIT_AND:
1479 case Token::BIT_XOR:
1480 case Token::SAR:
1481 case Token::SHL:
1482 case Token::SHR: {
1483 GenerateRegisterArgsPush(masm);
1484 Label not_floats;
1485 Label not_int32;
1486 Label non_smi_result;
1487 /* {
1488 CpuFeatures::Scope use_sse2(SSE2);
1489 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1490 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1491 }*/
1492 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1493 use_sse3_,
1494 &not_floats);
1495 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1496 &not_int32);
1497 switch (op_) {
1498 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1499 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1500 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1501 case Token::SAR: __ sar_cl(eax); break;
1502 case Token::SHL: __ shl_cl(eax); break;
1503 case Token::SHR: __ shr_cl(eax); break;
1504 default: UNREACHABLE();
1505 }
1506 if (op_ == Token::SHR) {
1507 // Check if result is non-negative and fits in a smi.
1508 __ test(eax, Immediate(0xc0000000));
1509 __ j(not_zero, &call_runtime);
1510 } else {
1511 // Check if result fits in a smi.
1512 __ cmp(eax, 0xc0000000);
1513 __ j(negative, &non_smi_result);
1514 }
1515 // Tag smi result and return.
1516 __ SmiTag(eax);
1517 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1518
1519 // All ops except SHR return a signed int32 that we load in
1520 // a HeapNumber.
1521 if (op_ != Token::SHR) {
1522 __ bind(&non_smi_result);
1523 // Allocate a heap number if needed.
1524 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001525 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001526 switch (mode_) {
1527 case OVERWRITE_LEFT:
1528 case OVERWRITE_RIGHT:
1529 // If the operand was an object, we skip the
1530 // allocation of a heap number.
1531 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1532 1 * kPointerSize : 2 * kPointerSize));
1533 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001534 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001535 // Fall through!
1536 case NO_OVERWRITE:
1537 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1538 __ bind(&skip_allocation);
1539 break;
1540 default: UNREACHABLE();
1541 }
1542 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001543 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001544 CpuFeatures::Scope use_sse2(SSE2);
1545 __ cvtsi2sd(xmm0, Operand(ebx));
1546 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1547 } else {
1548 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1549 __ fild_s(Operand(esp, 1 * kPointerSize));
1550 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1551 }
1552 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1553 }
1554
1555 __ bind(&not_floats);
1556 __ bind(&not_int32);
1557 GenerateTypeTransitionWithSavedArgs(masm);
1558 break;
1559 }
1560 default: UNREACHABLE(); break;
1561 }
1562
1563 // If an allocation fails, or SHR or MOD hit a hard case,
1564 // use the runtime system to get the correct result.
1565 __ bind(&call_runtime);
1566
1567 switch (op_) {
1568 case Token::ADD:
1569 GenerateRegisterArgsPush(masm);
1570 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1571 break;
1572 case Token::SUB:
1573 GenerateRegisterArgsPush(masm);
1574 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1575 break;
1576 case Token::MUL:
1577 GenerateRegisterArgsPush(masm);
1578 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1579 break;
1580 case Token::DIV:
1581 GenerateRegisterArgsPush(masm);
1582 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1583 break;
1584 case Token::MOD:
1585 GenerateRegisterArgsPush(masm);
1586 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1587 break;
1588 case Token::BIT_OR:
1589 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1590 break;
1591 case Token::BIT_AND:
1592 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1593 break;
1594 case Token::BIT_XOR:
1595 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1596 break;
1597 case Token::SAR:
1598 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1599 break;
1600 case Token::SHL:
1601 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1602 break;
1603 case Token::SHR:
1604 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1605 break;
1606 default:
1607 UNREACHABLE();
1608 }
1609}
1610
1611
lrn@chromium.org7516f052011-03-30 08:52:27 +00001612void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001613 if (op_ == Token::ADD) {
1614 // Handle string addition here, because it is the only operation
1615 // that does not do a ToNumber conversion on the operands.
1616 GenerateAddStrings(masm);
1617 }
1618
danno@chromium.org160a7b02011-04-18 15:51:38 +00001619 Factory* factory = masm->isolate()->factory();
1620
lrn@chromium.org7516f052011-03-30 08:52:27 +00001621 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001622 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001623 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001624 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001625 if (Token::IsBitOp(op_)) {
1626 __ xor_(edx, Operand(edx));
1627 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001628 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001629 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001630 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001631 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001632 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001633 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001634 if (Token::IsBitOp(op_)) {
1635 __ xor_(eax, Operand(eax));
1636 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001637 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001638 }
1639 __ bind(&done);
1640
1641 GenerateHeapNumberStub(masm);
1642}
1643
1644
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001645void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1646 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001647
1648 // Floating point case.
1649 switch (op_) {
1650 case Token::ADD:
1651 case Token::SUB:
1652 case Token::MUL:
1653 case Token::DIV: {
1654 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001655 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001656 CpuFeatures::Scope use_sse2(SSE2);
1657 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1658
1659 switch (op_) {
1660 case Token::ADD: __ addsd(xmm0, xmm1); break;
1661 case Token::SUB: __ subsd(xmm0, xmm1); break;
1662 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1663 case Token::DIV: __ divsd(xmm0, xmm1); break;
1664 default: UNREACHABLE();
1665 }
1666 GenerateHeapResultAllocation(masm, &call_runtime);
1667 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1668 __ ret(0);
1669 } else { // SSE2 not available, use FPU.
1670 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1671 FloatingPointHelper::LoadFloatOperands(
1672 masm,
1673 ecx,
1674 FloatingPointHelper::ARGS_IN_REGISTERS);
1675 switch (op_) {
1676 case Token::ADD: __ faddp(1); break;
1677 case Token::SUB: __ fsubp(1); break;
1678 case Token::MUL: __ fmulp(1); break;
1679 case Token::DIV: __ fdivp(1); break;
1680 default: UNREACHABLE();
1681 }
1682 Label after_alloc_failure;
1683 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1684 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1685 __ ret(0);
1686 __ bind(&after_alloc_failure);
1687 __ ffree();
1688 __ jmp(&call_runtime);
1689 }
1690
1691 __ bind(&not_floats);
1692 GenerateTypeTransition(masm);
1693 break;
1694 }
1695
1696 case Token::MOD: {
1697 // For MOD we go directly to runtime in the non-smi case.
1698 break;
1699 }
1700 case Token::BIT_OR:
1701 case Token::BIT_AND:
1702 case Token::BIT_XOR:
1703 case Token::SAR:
1704 case Token::SHL:
1705 case Token::SHR: {
1706 GenerateRegisterArgsPush(masm);
1707 Label not_floats;
1708 Label non_smi_result;
1709 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1710 use_sse3_,
1711 &not_floats);
1712 switch (op_) {
1713 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1714 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1715 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1716 case Token::SAR: __ sar_cl(eax); break;
1717 case Token::SHL: __ shl_cl(eax); break;
1718 case Token::SHR: __ shr_cl(eax); break;
1719 default: UNREACHABLE();
1720 }
1721 if (op_ == Token::SHR) {
1722 // Check if result is non-negative and fits in a smi.
1723 __ test(eax, Immediate(0xc0000000));
1724 __ j(not_zero, &call_runtime);
1725 } else {
1726 // Check if result fits in a smi.
1727 __ cmp(eax, 0xc0000000);
1728 __ j(negative, &non_smi_result);
1729 }
1730 // Tag smi result and return.
1731 __ SmiTag(eax);
1732 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1733
1734 // All ops except SHR return a signed int32 that we load in
1735 // a HeapNumber.
1736 if (op_ != Token::SHR) {
1737 __ bind(&non_smi_result);
1738 // Allocate a heap number if needed.
1739 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001740 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001741 switch (mode_) {
1742 case OVERWRITE_LEFT:
1743 case OVERWRITE_RIGHT:
1744 // If the operand was an object, we skip the
1745 // allocation of a heap number.
1746 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1747 1 * kPointerSize : 2 * kPointerSize));
1748 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001749 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001750 // Fall through!
1751 case NO_OVERWRITE:
1752 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1753 __ bind(&skip_allocation);
1754 break;
1755 default: UNREACHABLE();
1756 }
1757 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001758 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001759 CpuFeatures::Scope use_sse2(SSE2);
1760 __ cvtsi2sd(xmm0, Operand(ebx));
1761 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1762 } else {
1763 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1764 __ fild_s(Operand(esp, 1 * kPointerSize));
1765 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1766 }
1767 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1768 }
1769
1770 __ bind(&not_floats);
1771 GenerateTypeTransitionWithSavedArgs(masm);
1772 break;
1773 }
1774 default: UNREACHABLE(); break;
1775 }
1776
1777 // If an allocation fails, or SHR or MOD hit a hard case,
1778 // use the runtime system to get the correct result.
1779 __ bind(&call_runtime);
1780
1781 switch (op_) {
1782 case Token::ADD:
1783 GenerateRegisterArgsPush(masm);
1784 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1785 break;
1786 case Token::SUB:
1787 GenerateRegisterArgsPush(masm);
1788 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1789 break;
1790 case Token::MUL:
1791 GenerateRegisterArgsPush(masm);
1792 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1793 break;
1794 case Token::DIV:
1795 GenerateRegisterArgsPush(masm);
1796 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1797 break;
1798 case Token::MOD:
1799 GenerateRegisterArgsPush(masm);
1800 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1801 break;
1802 case Token::BIT_OR:
1803 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1804 break;
1805 case Token::BIT_AND:
1806 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1807 break;
1808 case Token::BIT_XOR:
1809 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1810 break;
1811 case Token::SAR:
1812 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1813 break;
1814 case Token::SHL:
1815 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1816 break;
1817 case Token::SHR:
1818 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1819 break;
1820 default:
1821 UNREACHABLE();
1822 }
1823}
1824
1825
1826void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
1827 Label call_runtime;
1828
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001829 Counters* counters = masm->isolate()->counters();
1830 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001831
1832 switch (op_) {
1833 case Token::ADD:
1834 case Token::SUB:
1835 case Token::MUL:
1836 case Token::DIV:
1837 break;
1838 case Token::MOD:
1839 case Token::BIT_OR:
1840 case Token::BIT_AND:
1841 case Token::BIT_XOR:
1842 case Token::SAR:
1843 case Token::SHL:
1844 case Token::SHR:
1845 GenerateRegisterArgsPush(masm);
1846 break;
1847 default:
1848 UNREACHABLE();
1849 }
1850
1851 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1852
1853 // Floating point case.
1854 switch (op_) {
1855 case Token::ADD:
1856 case Token::SUB:
1857 case Token::MUL:
1858 case Token::DIV: {
1859 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001860 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001861 CpuFeatures::Scope use_sse2(SSE2);
1862 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1863
1864 switch (op_) {
1865 case Token::ADD: __ addsd(xmm0, xmm1); break;
1866 case Token::SUB: __ subsd(xmm0, xmm1); break;
1867 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1868 case Token::DIV: __ divsd(xmm0, xmm1); break;
1869 default: UNREACHABLE();
1870 }
1871 GenerateHeapResultAllocation(masm, &call_runtime);
1872 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1873 __ ret(0);
1874 } else { // SSE2 not available, use FPU.
1875 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1876 FloatingPointHelper::LoadFloatOperands(
1877 masm,
1878 ecx,
1879 FloatingPointHelper::ARGS_IN_REGISTERS);
1880 switch (op_) {
1881 case Token::ADD: __ faddp(1); break;
1882 case Token::SUB: __ fsubp(1); break;
1883 case Token::MUL: __ fmulp(1); break;
1884 case Token::DIV: __ fdivp(1); break;
1885 default: UNREACHABLE();
1886 }
1887 Label after_alloc_failure;
1888 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1889 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1890 __ ret(0);
1891 __ bind(&after_alloc_failure);
1892 __ ffree();
1893 __ jmp(&call_runtime);
1894 }
1895 __ bind(&not_floats);
1896 break;
1897 }
1898 case Token::MOD: {
1899 // For MOD we go directly to runtime in the non-smi case.
1900 break;
1901 }
1902 case Token::BIT_OR:
1903 case Token::BIT_AND:
1904 case Token::BIT_XOR:
1905 case Token::SAR:
1906 case Token::SHL:
1907 case Token::SHR: {
1908 Label non_smi_result;
1909 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1910 use_sse3_,
1911 &call_runtime);
1912 switch (op_) {
1913 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1914 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1915 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1916 case Token::SAR: __ sar_cl(eax); break;
1917 case Token::SHL: __ shl_cl(eax); break;
1918 case Token::SHR: __ shr_cl(eax); break;
1919 default: UNREACHABLE();
1920 }
1921 if (op_ == Token::SHR) {
1922 // Check if result is non-negative and fits in a smi.
1923 __ test(eax, Immediate(0xc0000000));
1924 __ j(not_zero, &call_runtime);
1925 } else {
1926 // Check if result fits in a smi.
1927 __ cmp(eax, 0xc0000000);
1928 __ j(negative, &non_smi_result);
1929 }
1930 // Tag smi result and return.
1931 __ SmiTag(eax);
1932 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1933
1934 // All ops except SHR return a signed int32 that we load in
1935 // a HeapNumber.
1936 if (op_ != Token::SHR) {
1937 __ bind(&non_smi_result);
1938 // Allocate a heap number if needed.
1939 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001940 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001941 switch (mode_) {
1942 case OVERWRITE_LEFT:
1943 case OVERWRITE_RIGHT:
1944 // If the operand was an object, we skip the
1945 // allocation of a heap number.
1946 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1947 1 * kPointerSize : 2 * kPointerSize));
1948 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001949 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001950 // Fall through!
1951 case NO_OVERWRITE:
1952 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1953 __ bind(&skip_allocation);
1954 break;
1955 default: UNREACHABLE();
1956 }
1957 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001958 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001959 CpuFeatures::Scope use_sse2(SSE2);
1960 __ cvtsi2sd(xmm0, Operand(ebx));
1961 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1962 } else {
1963 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1964 __ fild_s(Operand(esp, 1 * kPointerSize));
1965 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1966 }
1967 __ ret(2 * kPointerSize);
1968 }
1969 break;
1970 }
1971 default: UNREACHABLE(); break;
1972 }
1973
1974 // If all else fails, use the runtime system to get the correct
1975 // result.
1976 __ bind(&call_runtime);
1977 switch (op_) {
1978 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001979 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001980 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001981 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1982 break;
1983 }
1984 case Token::SUB:
1985 GenerateRegisterArgsPush(masm);
1986 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1987 break;
1988 case Token::MUL:
1989 GenerateRegisterArgsPush(masm);
1990 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1991 break;
1992 case Token::DIV:
1993 GenerateRegisterArgsPush(masm);
1994 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1995 break;
1996 case Token::MOD:
1997 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1998 break;
1999 case Token::BIT_OR:
2000 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2001 break;
2002 case Token::BIT_AND:
2003 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2004 break;
2005 case Token::BIT_XOR:
2006 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2007 break;
2008 case Token::SAR:
2009 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2010 break;
2011 case Token::SHL:
2012 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2013 break;
2014 case Token::SHR:
2015 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2016 break;
2017 default:
2018 UNREACHABLE();
2019 }
2020}
2021
2022
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002023void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002024 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002025 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002026
2027 // Registers containing left and right operands respectively.
2028 Register left = edx;
2029 Register right = eax;
2030
2031 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002032 __ test(left, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002033 __ j(zero, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002034 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002035 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002036
2037 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2038 GenerateRegisterArgsPush(masm);
2039 __ TailCallStub(&string_add_left_stub);
2040
2041 // Left operand is not a string, test right.
2042 __ bind(&left_not_string);
2043 __ test(right, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002044 __ j(zero, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002045 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002046 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002047
2048 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2049 GenerateRegisterArgsPush(masm);
2050 __ TailCallStub(&string_add_right_stub);
2051
2052 // Neither argument is a string.
2053 __ bind(&call_runtime);
2054}
2055
2056
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002057void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2058 MacroAssembler* masm,
2059 Label* alloc_failure) {
2060 Label skip_allocation;
2061 OverwriteMode mode = mode_;
2062 switch (mode) {
2063 case OVERWRITE_LEFT: {
2064 // If the argument in edx is already an object, we skip the
2065 // allocation of a heap number.
2066 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002067 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002068 // Allocate a heap number for the result. Keep eax and edx intact
2069 // for the possible runtime call.
2070 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2071 // Now edx can be overwritten losing one of the arguments as we are
2072 // now done and will not need it any more.
2073 __ mov(edx, Operand(ebx));
2074 __ bind(&skip_allocation);
2075 // Use object in edx as a result holder
2076 __ mov(eax, Operand(edx));
2077 break;
2078 }
2079 case OVERWRITE_RIGHT:
2080 // If the argument in eax is already an object, we skip the
2081 // allocation of a heap number.
2082 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002083 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002084 // Fall through!
2085 case NO_OVERWRITE:
2086 // Allocate a heap number for the result. Keep eax and edx intact
2087 // for the possible runtime call.
2088 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2089 // Now eax can be overwritten losing one of the arguments as we are
2090 // now done and will not need it any more.
2091 __ mov(eax, ebx);
2092 __ bind(&skip_allocation);
2093 break;
2094 default: UNREACHABLE();
2095 }
2096}
2097
2098
2099void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2100 __ pop(ecx);
2101 __ push(edx);
2102 __ push(eax);
2103 __ push(ecx);
2104}
2105
2106
ricow@chromium.org65fae842010-08-25 15:26:24 +00002107void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002108 // TAGGED case:
2109 // Input:
2110 // esp[4]: tagged number input argument (should be number).
2111 // esp[0]: return address.
2112 // Output:
2113 // eax: tagged double result.
2114 // UNTAGGED case:
2115 // Input::
2116 // esp[0]: return address.
2117 // xmm1: untagged double input argument
2118 // Output:
2119 // xmm1: untagged double result.
2120
ricow@chromium.org65fae842010-08-25 15:26:24 +00002121 Label runtime_call;
2122 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002123 Label skip_cache;
2124 const bool tagged = (argument_type_ == TAGGED);
2125 if (tagged) {
2126 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002127 Label input_not_smi;
2128 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002129 __ mov(eax, Operand(esp, kPointerSize));
2130 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002131 __ j(not_zero, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002132 // Input is a smi. Untag and load it onto the FPU stack.
2133 // Then load the low and high words of the double into ebx, edx.
2134 STATIC_ASSERT(kSmiTagSize == 1);
2135 __ sar(eax, 1);
2136 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2137 __ mov(Operand(esp, 0), eax);
2138 __ fild_s(Operand(esp, 0));
2139 __ fst_d(Operand(esp, 0));
2140 __ pop(edx);
2141 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002142 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002143 __ bind(&input_not_smi);
2144 // Check if input is a HeapNumber.
2145 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002146 Factory* factory = masm->isolate()->factory();
2147 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002148 __ j(not_equal, &runtime_call);
2149 // Input is a HeapNumber. Push it on the FPU stack and load its
2150 // low and high words into ebx, edx.
2151 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2152 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2153 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002154
whesse@chromium.org023421e2010-12-21 12:19:12 +00002155 __ bind(&loaded);
2156 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002157 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002158 CpuFeatures::Scope sse4_scope(SSE4_1);
2159 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2160 } else {
2161 __ pshufd(xmm0, xmm1, 0x1);
2162 __ movd(Operand(edx), xmm0);
2163 }
2164 __ movd(Operand(ebx), xmm1);
2165 }
2166
2167 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002168 // ebx = low 32 bits of double value
2169 // edx = high 32 bits of double value
2170 // Compute hash (the shifts are arithmetic):
2171 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2172 __ mov(ecx, ebx);
2173 __ xor_(ecx, Operand(edx));
2174 __ mov(eax, ecx);
2175 __ sar(eax, 16);
2176 __ xor_(ecx, Operand(eax));
2177 __ mov(eax, ecx);
2178 __ sar(eax, 8);
2179 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002180 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2181 __ and_(Operand(ecx),
2182 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002183
whesse@chromium.org023421e2010-12-21 12:19:12 +00002184 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002185 // ebx = low 32 bits of double value.
2186 // edx = high 32 bits of double value.
2187 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002188 ExternalReference cache_array =
2189 ExternalReference::transcendental_cache_array_address(masm->isolate());
2190 __ mov(eax, Immediate(cache_array));
2191 int cache_array_index =
2192 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2193 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002194 // Eax points to the cache for the type type_.
2195 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2196 __ test(eax, Operand(eax));
2197 __ j(zero, &runtime_call_clear_stack);
2198#ifdef DEBUG
2199 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002200 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002201 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2202 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2203 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2204 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2205 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2206 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2207 CHECK_EQ(0, elem_in0 - elem_start);
2208 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2209 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2210 }
2211#endif
2212 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2213 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2214 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2215 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002216 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002217 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002218 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002219 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002220 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002221 // Cache hit!
2222 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002223 if (tagged) {
2224 __ fstp(0);
2225 __ ret(kPointerSize);
2226 } else { // UNTAGGED.
2227 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2228 __ Ret();
2229 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002230
2231 __ bind(&cache_miss);
2232 // Update cache with new value.
2233 // We are short on registers, so use no_reg as scratch.
2234 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002235 if (tagged) {
2236 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2237 } else { // UNTAGGED.
2238 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2239 __ sub(Operand(esp), Immediate(kDoubleSize));
2240 __ movdbl(Operand(esp, 0), xmm1);
2241 __ fld_d(Operand(esp, 0));
2242 __ add(Operand(esp), Immediate(kDoubleSize));
2243 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002244 GenerateOperation(masm);
2245 __ mov(Operand(ecx, 0), ebx);
2246 __ mov(Operand(ecx, kIntSize), edx);
2247 __ mov(Operand(ecx, 2 * kIntSize), eax);
2248 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002249 if (tagged) {
2250 __ ret(kPointerSize);
2251 } else { // UNTAGGED.
2252 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2253 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002254
whesse@chromium.org023421e2010-12-21 12:19:12 +00002255 // Skip cache and return answer directly, only in untagged case.
2256 __ bind(&skip_cache);
2257 __ sub(Operand(esp), Immediate(kDoubleSize));
2258 __ movdbl(Operand(esp, 0), xmm1);
2259 __ fld_d(Operand(esp, 0));
2260 GenerateOperation(masm);
2261 __ fstp_d(Operand(esp, 0));
2262 __ movdbl(xmm1, Operand(esp, 0));
2263 __ add(Operand(esp), Immediate(kDoubleSize));
2264 // We return the value in xmm1 without adding it to the cache, but
2265 // we cause a scavenging GC so that future allocations will succeed.
2266 __ EnterInternalFrame();
2267 // Allocate an unused object bigger than a HeapNumber.
2268 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2269 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2270 __ LeaveInternalFrame();
2271 __ Ret();
2272 }
2273
2274 // Call runtime, doing whatever allocation and cleanup is necessary.
2275 if (tagged) {
2276 __ bind(&runtime_call_clear_stack);
2277 __ fstp(0);
2278 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002279 ExternalReference runtime =
2280 ExternalReference(RuntimeFunction(), masm->isolate());
2281 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002282 } else { // UNTAGGED.
2283 __ bind(&runtime_call_clear_stack);
2284 __ bind(&runtime_call);
2285 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2286 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2287 __ EnterInternalFrame();
2288 __ push(eax);
2289 __ CallRuntime(RuntimeFunction(), 1);
2290 __ LeaveInternalFrame();
2291 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2292 __ Ret();
2293 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002294}
2295
2296
2297Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2298 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002299 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2300 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002301 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002302 default:
2303 UNIMPLEMENTED();
2304 return Runtime::kAbort;
2305 }
2306}
2307
2308
2309void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2310 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002311 // Input value is on FP stack, and also in ebx/edx.
2312 // Input value is possibly in xmm1.
2313 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002314 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2315 // Both fsin and fcos require arguments in the range +/-2^63 and
2316 // return NaN for infinities and NaN. They can share all code except
2317 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002318 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002319 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2320 // work. We must reduce it to the appropriate range.
2321 __ mov(edi, edx);
2322 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2323 int supported_exponent_limit =
2324 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2325 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002326 __ j(below, &in_range, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002327 // Check for infinity and NaN. Both return NaN for sin.
2328 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002329 Label non_nan_result;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002330 __ j(not_equal, &non_nan_result, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002331 // Input is +/-Infinity or NaN. Result is NaN.
2332 __ fstp(0);
2333 // NaN is represented by 0x7ff8000000000000.
2334 __ push(Immediate(0x7ff80000));
2335 __ push(Immediate(0));
2336 __ fld_d(Operand(esp, 0));
2337 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002338 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002339
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002340 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002341
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002342 // Use fpmod to restrict argument to the range +/-2*PI.
2343 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2344 __ fldpi();
2345 __ fadd(0);
2346 __ fld(1);
2347 // FPU Stack: input, 2*pi, input.
2348 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002349 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002350 __ fwait();
2351 __ fnstsw_ax();
2352 // Clear if Illegal Operand or Zero Division exceptions are set.
2353 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002354 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002355 __ fnclex();
2356 __ bind(&no_exceptions);
2357 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002358
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002359 // Compute st(0) % st(1)
2360 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002361 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002362 __ bind(&partial_remainder_loop);
2363 __ fprem1();
2364 __ fwait();
2365 __ fnstsw_ax();
2366 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2367 // If C2 is set, computation only has partial result. Loop to
2368 // continue computation.
2369 __ j(not_zero, &partial_remainder_loop);
2370 }
2371 // FPU Stack: input, 2*pi, input % 2*pi
2372 __ fstp(2);
2373 __ fstp(0);
2374 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2375
2376 // FPU Stack: input % 2*pi
2377 __ bind(&in_range);
2378 switch (type_) {
2379 case TranscendentalCache::SIN:
2380 __ fsin();
2381 break;
2382 case TranscendentalCache::COS:
2383 __ fcos();
2384 break;
2385 default:
2386 UNREACHABLE();
2387 }
2388 __ bind(&done);
2389 } else {
2390 ASSERT(type_ == TranscendentalCache::LOG);
2391 __ fldln2();
2392 __ fxch();
2393 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002394 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002395}
2396
2397
ricow@chromium.org65fae842010-08-25 15:26:24 +00002398// Input: edx, eax are the left and right objects of a bit op.
2399// Output: eax, ecx are left and right integers for a bit op.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002400void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2401 bool use_sse3,
2402 Label* conversion_failure) {
2403 // Check float operands.
2404 Label arg1_is_object, check_undefined_arg1;
2405 Label arg2_is_object, check_undefined_arg2;
2406 Label load_arg2, done;
2407
2408 // Test if arg1 is a Smi.
2409 __ test(edx, Immediate(kSmiTagMask));
2410 __ j(not_zero, &arg1_is_object);
2411
2412 __ SmiUntag(edx);
2413 __ jmp(&load_arg2);
2414
2415 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2416 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002417 Factory* factory = masm->isolate()->factory();
2418 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002419 __ j(not_equal, conversion_failure);
2420 __ mov(edx, Immediate(0));
2421 __ jmp(&load_arg2);
2422
2423 __ bind(&arg1_is_object);
2424 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002425 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002426 __ j(not_equal, &check_undefined_arg1);
2427
2428 // Get the untagged integer version of the edx heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002429 IntegerConvert(masm, edx, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002430 __ mov(edx, ecx);
2431
2432 // Here edx has the untagged integer, eax has a Smi or a heap number.
2433 __ bind(&load_arg2);
2434
2435 // Test if arg2 is a Smi.
2436 __ test(eax, Immediate(kSmiTagMask));
2437 __ j(not_zero, &arg2_is_object);
2438
2439 __ SmiUntag(eax);
2440 __ mov(ecx, eax);
2441 __ jmp(&done);
2442
2443 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2444 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002445 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002446 __ j(not_equal, conversion_failure);
2447 __ mov(ecx, Immediate(0));
2448 __ jmp(&done);
2449
2450 __ bind(&arg2_is_object);
2451 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002452 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002453 __ j(not_equal, &check_undefined_arg2);
2454
2455 // Get the untagged integer version of the eax heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002456 IntegerConvert(masm, eax, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002457 __ bind(&done);
2458 __ mov(eax, edx);
2459}
2460
2461
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002462void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2463 bool use_sse3,
2464 Label* not_int32) {
2465 return;
2466}
2467
2468
ricow@chromium.org65fae842010-08-25 15:26:24 +00002469void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2470 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002471 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002472
2473 __ test(number, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002474 __ j(zero, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002475 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002476 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002477
2478 __ bind(&load_smi);
2479 __ SmiUntag(number);
2480 __ push(number);
2481 __ fild_s(Operand(esp, 0));
2482 __ pop(number);
2483
2484 __ bind(&done);
2485}
2486
2487
2488void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002489 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002490 // Load operand in edx into xmm0.
2491 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002492 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002493 __ j(zero, &load_smi_edx, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002494 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2495
2496 __ bind(&load_eax);
2497 // Load operand in eax into xmm1.
2498 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002499 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002500 __ j(zero, &load_smi_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002501 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002502 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002503
2504 __ bind(&load_smi_edx);
2505 __ SmiUntag(edx); // Untag smi before converting to float.
2506 __ cvtsi2sd(xmm0, Operand(edx));
2507 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2508 __ jmp(&load_eax);
2509
2510 __ bind(&load_smi_eax);
2511 __ SmiUntag(eax); // Untag smi before converting to float.
2512 __ cvtsi2sd(xmm1, Operand(eax));
2513 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2514
2515 __ bind(&done);
2516}
2517
2518
2519void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2520 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002521 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002522 // Load operand in edx into xmm0, or branch to not_numbers.
2523 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002524 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002525 __ j(zero, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002526 Factory* factory = masm->isolate()->factory();
2527 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002528 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2529 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2530 __ bind(&load_eax);
2531 // Load operand in eax into xmm1, or branch to not_numbers.
2532 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002533 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002534 __ j(zero, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002535 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002536 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002537 __ jmp(not_numbers); // Argument in eax is not a number.
2538 __ bind(&load_smi_edx);
2539 __ SmiUntag(edx); // Untag smi before converting to float.
2540 __ cvtsi2sd(xmm0, Operand(edx));
2541 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2542 __ jmp(&load_eax);
2543 __ bind(&load_smi_eax);
2544 __ SmiUntag(eax); // Untag smi before converting to float.
2545 __ cvtsi2sd(xmm1, Operand(eax));
2546 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002547 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002548 __ bind(&load_float_eax);
2549 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2550 __ bind(&done);
2551}
2552
2553
2554void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2555 Register scratch) {
2556 const Register left = edx;
2557 const Register right = eax;
2558 __ mov(scratch, left);
2559 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2560 __ SmiUntag(scratch);
2561 __ cvtsi2sd(xmm0, Operand(scratch));
2562
2563 __ mov(scratch, right);
2564 __ SmiUntag(scratch);
2565 __ cvtsi2sd(xmm1, Operand(scratch));
2566}
2567
2568
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002569void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2570 Label* non_int32,
2571 Register scratch) {
2572 __ cvttsd2si(scratch, Operand(xmm0));
2573 __ cvtsi2sd(xmm2, Operand(scratch));
2574 __ ucomisd(xmm0, xmm2);
2575 __ j(not_zero, non_int32);
2576 __ j(carry, non_int32);
2577 __ cvttsd2si(scratch, Operand(xmm1));
2578 __ cvtsi2sd(xmm2, Operand(scratch));
2579 __ ucomisd(xmm1, xmm2);
2580 __ j(not_zero, non_int32);
2581 __ j(carry, non_int32);
2582}
2583
2584
ricow@chromium.org65fae842010-08-25 15:26:24 +00002585void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2586 Register scratch,
2587 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002588 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002589 if (arg_location == ARGS_IN_REGISTERS) {
2590 __ mov(scratch, edx);
2591 } else {
2592 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2593 }
2594 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002595 __ j(zero, &load_smi_1, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002596 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2597 __ bind(&done_load_1);
2598
2599 if (arg_location == ARGS_IN_REGISTERS) {
2600 __ mov(scratch, eax);
2601 } else {
2602 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2603 }
2604 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002605 __ j(zero, &load_smi_2, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002606 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002607 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002608
2609 __ bind(&load_smi_1);
2610 __ SmiUntag(scratch);
2611 __ push(scratch);
2612 __ fild_s(Operand(esp, 0));
2613 __ pop(scratch);
2614 __ jmp(&done_load_1);
2615
2616 __ bind(&load_smi_2);
2617 __ SmiUntag(scratch);
2618 __ push(scratch);
2619 __ fild_s(Operand(esp, 0));
2620 __ pop(scratch);
2621
2622 __ bind(&done);
2623}
2624
2625
2626void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2627 Register scratch) {
2628 const Register left = edx;
2629 const Register right = eax;
2630 __ mov(scratch, left);
2631 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2632 __ SmiUntag(scratch);
2633 __ push(scratch);
2634 __ fild_s(Operand(esp, 0));
2635
2636 __ mov(scratch, right);
2637 __ SmiUntag(scratch);
2638 __ mov(Operand(esp, 0), scratch);
2639 __ fild_s(Operand(esp, 0));
2640 __ pop(scratch);
2641}
2642
2643
2644void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2645 Label* non_float,
2646 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002647 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002648 // Test if both operands are floats or smi -> scratch=k_is_float;
2649 // Otherwise scratch = k_not_float.
2650 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002651 __ j(zero, &test_other, Label::kNear); // argument in edx is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002652 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002653 Factory* factory = masm->isolate()->factory();
2654 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002655 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2656
2657 __ bind(&test_other);
2658 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002659 __ j(zero, &done, Label::kNear); // argument in eax is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002660 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002661 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002662 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2663
2664 // Fall-through: Both operands are numbers.
2665 __ bind(&done);
2666}
2667
2668
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002669void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2670 Label* non_int32) {
2671 return;
2672}
2673
2674
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002675void MathPowStub::Generate(MacroAssembler* masm) {
2676 // Registers are used as follows:
2677 // edx = base
2678 // eax = exponent
2679 // ecx = temporary, result
2680
2681 CpuFeatures::Scope use_sse2(SSE2);
2682 Label allocate_return, call_runtime;
2683
2684 // Load input parameters.
2685 __ mov(edx, Operand(esp, 2 * kPointerSize));
2686 __ mov(eax, Operand(esp, 1 * kPointerSize));
2687
2688 // Save 1 in xmm3 - we need this several times later on.
2689 __ mov(ecx, Immediate(1));
2690 __ cvtsi2sd(xmm3, Operand(ecx));
2691
2692 Label exponent_nonsmi;
2693 Label base_nonsmi;
2694 // If the exponent is a heap number go to that specific case.
2695 __ test(eax, Immediate(kSmiTagMask));
2696 __ j(not_zero, &exponent_nonsmi);
2697 __ test(edx, Immediate(kSmiTagMask));
2698 __ j(not_zero, &base_nonsmi);
2699
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002700 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002701 Label powi;
2702 __ SmiUntag(edx);
2703 __ cvtsi2sd(xmm0, Operand(edx));
2704 __ jmp(&powi);
2705 // exponent is smi and base is a heapnumber.
2706 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002707 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002708 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002709 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002710 __ j(not_equal, &call_runtime);
2711
2712 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2713
2714 // Optimized version of pow if exponent is a smi.
2715 // xmm0 contains the base.
2716 __ bind(&powi);
2717 __ SmiUntag(eax);
2718
2719 // Save exponent in base as we need to check if exponent is negative later.
2720 // We know that base and exponent are in different registers.
2721 __ mov(edx, eax);
2722
2723 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002724 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002725 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002726 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002727 __ neg(eax);
2728 __ bind(&no_neg);
2729
2730 // Load xmm1 with 1.
2731 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002732 Label while_true;
2733 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002734
2735 __ bind(&while_true);
2736 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002737 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002738 __ mulsd(xmm1, xmm0);
2739 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002740 __ mulsd(xmm0, xmm0);
2741 __ j(not_zero, &while_true);
2742
2743 // base has the original value of the exponent - if the exponent is
2744 // negative return 1/result.
2745 __ test(edx, Operand(edx));
2746 __ j(positive, &allocate_return);
2747 // Special case if xmm1 has reached infinity.
2748 __ mov(ecx, Immediate(0x7FB00000));
2749 __ movd(xmm0, Operand(ecx));
2750 __ cvtss2sd(xmm0, xmm0);
2751 __ ucomisd(xmm0, xmm1);
2752 __ j(equal, &call_runtime);
2753 __ divsd(xmm3, xmm1);
2754 __ movsd(xmm1, xmm3);
2755 __ jmp(&allocate_return);
2756
2757 // exponent (or both) is a heapnumber - no matter what we should now work
2758 // on doubles.
2759 __ bind(&exponent_nonsmi);
2760 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002761 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002762 __ j(not_equal, &call_runtime);
2763 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2764 // Test if exponent is nan.
2765 __ ucomisd(xmm1, xmm1);
2766 __ j(parity_even, &call_runtime);
2767
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002768 Label base_not_smi;
2769 Label handle_special_cases;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002770 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002771 __ j(not_zero, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002772 __ SmiUntag(edx);
2773 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002774 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002775
2776 __ bind(&base_not_smi);
2777 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002778 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002779 __ j(not_equal, &call_runtime);
2780 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2781 __ and_(ecx, HeapNumber::kExponentMask);
2782 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2783 // base is NaN or +/-Infinity
2784 __ j(greater_equal, &call_runtime);
2785 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2786
2787 // base is in xmm0 and exponent is in xmm1.
2788 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002789 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002790 // Test for -0.5.
2791 // Load xmm2 with -0.5.
2792 __ mov(ecx, Immediate(0xBF000000));
2793 __ movd(xmm2, Operand(ecx));
2794 __ cvtss2sd(xmm2, xmm2);
2795 // xmm2 now has -0.5.
2796 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002797 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002798
2799 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002800 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002801 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002802 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002803 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002804 __ divsd(xmm3, xmm1);
2805 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002806 __ jmp(&allocate_return);
2807
2808 // Test for 0.5.
2809 __ bind(&not_minus_half);
2810 // Load xmm2 with 0.5.
2811 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2812 __ addsd(xmm2, xmm3);
2813 // xmm2 now has 0.5.
2814 __ ucomisd(xmm2, xmm1);
2815 __ j(not_equal, &call_runtime);
2816 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002817 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002818 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002819 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002820 __ sqrtsd(xmm1, xmm1);
2821
2822 __ bind(&allocate_return);
2823 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2824 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2825 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002826 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002827
2828 __ bind(&call_runtime);
2829 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2830}
2831
2832
ricow@chromium.org65fae842010-08-25 15:26:24 +00002833void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2834 // The key is in edx and the parameter count is in eax.
2835
2836 // The displacement is used for skipping the frame pointer on the
2837 // stack. It is the offset of the last parameter (if any) relative
2838 // to the frame pointer.
2839 static const int kDisplacement = 1 * kPointerSize;
2840
2841 // Check that the key is a smi.
2842 Label slow;
2843 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002844 __ j(not_zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002845
2846 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002847 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002848 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2849 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2850 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002851 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002852
2853 // Check index against formal parameters count limit passed in
2854 // through register eax. Use unsigned comparison to get negative
2855 // check for free.
2856 __ cmp(edx, Operand(eax));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002857 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002858
2859 // Read the argument from the stack and return it.
2860 STATIC_ASSERT(kSmiTagSize == 1);
2861 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2862 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2863 __ neg(edx);
2864 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2865 __ ret(0);
2866
2867 // Arguments adaptor case: Check index against actual arguments
2868 // limit found in the arguments adaptor frame. Use unsigned
2869 // comparison to get negative check for free.
2870 __ bind(&adaptor);
2871 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2872 __ cmp(edx, Operand(ecx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002873 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002874
2875 // Read the argument from the stack and return it.
2876 STATIC_ASSERT(kSmiTagSize == 1);
2877 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2878 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2879 __ neg(edx);
2880 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2881 __ ret(0);
2882
2883 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2884 // by calling the runtime system.
2885 __ bind(&slow);
2886 __ pop(ebx); // Return address.
2887 __ push(edx);
2888 __ push(ebx);
2889 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2890}
2891
2892
2893void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2894 // esp[0] : return address
2895 // esp[4] : number of parameters
2896 // esp[8] : receiver displacement
2897 // esp[16] : function
2898
2899 // The displacement is used for skipping the return address and the
2900 // frame pointer on the stack. It is the offset of the last
2901 // parameter (if any) relative to the frame pointer.
2902 static const int kDisplacement = 2 * kPointerSize;
2903
2904 // Check if the calling frame is an arguments adaptor frame.
2905 Label adaptor_frame, try_allocate, runtime;
2906 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2907 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2908 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2909 __ j(equal, &adaptor_frame);
2910
2911 // Get the length from the frame.
2912 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2913 __ jmp(&try_allocate);
2914
2915 // Patch the arguments.length and the parameters pointer.
2916 __ bind(&adaptor_frame);
2917 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2918 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2919 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
2920 __ mov(Operand(esp, 2 * kPointerSize), edx);
2921
2922 // Try the new space allocation. Start out with computing the size of
2923 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002924 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002925 __ bind(&try_allocate);
2926 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002927 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002928 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
2929 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002930 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002931
2932 // Do the allocation of both objects in one go.
2933 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
2934
2935 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002936 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2937 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002938 __ mov(edi, Operand(edi,
2939 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002940
2941 // Copy the JS object part.
2942 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2943 __ mov(ebx, FieldOperand(edi, i));
2944 __ mov(FieldOperand(eax, i), ebx);
2945 }
2946
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002947 if (type_ == NEW_NON_STRICT) {
2948 // Setup the callee in-object property.
2949 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2950 __ mov(ebx, Operand(esp, 3 * kPointerSize));
2951 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2952 Heap::kArgumentsCalleeIndex * kPointerSize),
2953 ebx);
2954 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002955
2956 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002957 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002958 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002959 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2960 Heap::kArgumentsLengthIndex * kPointerSize),
2961 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002962
2963 // If there are no actual arguments, we're done.
2964 Label done;
2965 __ test(ecx, Operand(ecx));
2966 __ j(zero, &done);
2967
2968 // Get the parameters pointer from the stack.
2969 __ mov(edx, Operand(esp, 2 * kPointerSize));
2970
2971 // Setup the elements pointer in the allocated arguments object and
2972 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002973 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002974 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
2975 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002976 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002977
ricow@chromium.org65fae842010-08-25 15:26:24 +00002978 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
2979 // Untag the length for the loop below.
2980 __ SmiUntag(ecx);
2981
2982 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002983 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002984 __ bind(&loop);
2985 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
2986 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
2987 __ add(Operand(edi), Immediate(kPointerSize));
2988 __ sub(Operand(edx), Immediate(kPointerSize));
2989 __ dec(ecx);
2990 __ j(not_zero, &loop);
2991
2992 // Return and remove the on-stack parameters.
2993 __ bind(&done);
2994 __ ret(3 * kPointerSize);
2995
2996 // Do the runtime call to allocate the arguments object.
2997 __ bind(&runtime);
2998 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2999}
3000
3001
3002void RegExpExecStub::Generate(MacroAssembler* masm) {
3003 // Just jump directly to runtime if native RegExp is not selected at compile
3004 // time or if regexp entry in generated code is turned off runtime switch or
3005 // at compilation.
3006#ifdef V8_INTERPRETED_REGEXP
3007 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3008#else // V8_INTERPRETED_REGEXP
3009 if (!FLAG_regexp_entry_native) {
3010 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3011 return;
3012 }
3013
3014 // Stack frame on entry.
3015 // esp[0]: return address
3016 // esp[4]: last_match_info (expected JSArray)
3017 // esp[8]: previous index
3018 // esp[12]: subject string
3019 // esp[16]: JSRegExp object
3020
3021 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3022 static const int kPreviousIndexOffset = 2 * kPointerSize;
3023 static const int kSubjectOffset = 3 * kPointerSize;
3024 static const int kJSRegExpOffset = 4 * kPointerSize;
3025
3026 Label runtime, invoke_regexp;
3027
3028 // Ensure that a RegExp stack is allocated.
3029 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003030 ExternalReference::address_of_regexp_stack_memory_address(
3031 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003032 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003033 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003034 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3035 __ test(ebx, Operand(ebx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003036 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003037
3038 // Check that the first argument is a JSRegExp object.
3039 __ mov(eax, Operand(esp, kJSRegExpOffset));
3040 STATIC_ASSERT(kSmiTag == 0);
3041 __ test(eax, Immediate(kSmiTagMask));
3042 __ j(zero, &runtime);
3043 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3044 __ j(not_equal, &runtime);
3045 // Check that the RegExp has been compiled (data contains a fixed array).
3046 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3047 if (FLAG_debug_code) {
3048 __ test(ecx, Immediate(kSmiTagMask));
3049 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3050 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3051 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3052 }
3053
3054 // ecx: RegExp data (FixedArray)
3055 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3056 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3057 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3058 __ j(not_equal, &runtime);
3059
3060 // ecx: RegExp data (FixedArray)
3061 // Check that the number of captures fit in the static offsets vector buffer.
3062 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3063 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3064 // uses the asumption that smis are 2 * their untagged value.
3065 STATIC_ASSERT(kSmiTag == 0);
3066 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3067 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3068 // Check that the static offsets vector buffer is large enough.
3069 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3070 __ j(above, &runtime);
3071
3072 // ecx: RegExp data (FixedArray)
3073 // edx: Number of capture registers
3074 // Check that the second argument is a string.
3075 __ mov(eax, Operand(esp, kSubjectOffset));
3076 __ test(eax, Immediate(kSmiTagMask));
3077 __ j(zero, &runtime);
3078 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3079 __ j(NegateCondition(is_string), &runtime);
3080 // Get the length of the string to ebx.
3081 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3082
3083 // ebx: Length of subject string as a smi
3084 // ecx: RegExp data (FixedArray)
3085 // edx: Number of capture registers
3086 // Check that the third argument is a positive smi less than the subject
3087 // string length. A negative value will be greater (unsigned comparison).
3088 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3089 __ test(eax, Immediate(kSmiTagMask));
3090 __ j(not_zero, &runtime);
3091 __ cmp(eax, Operand(ebx));
3092 __ j(above_equal, &runtime);
3093
3094 // ecx: RegExp data (FixedArray)
3095 // edx: Number of capture registers
3096 // Check that the fourth object is a JSArray object.
3097 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3098 __ test(eax, Immediate(kSmiTagMask));
3099 __ j(zero, &runtime);
3100 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3101 __ j(not_equal, &runtime);
3102 // Check that the JSArray is in fast case.
3103 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3104 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003105 Factory* factory = masm->isolate()->factory();
3106 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003107 __ j(not_equal, &runtime);
3108 // Check that the last match info has space for the capture registers and the
3109 // additional information.
3110 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3111 __ SmiUntag(eax);
3112 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3113 __ cmp(edx, Operand(eax));
3114 __ j(greater, &runtime);
3115
3116 // ecx: RegExp data (FixedArray)
3117 // Check the representation and encoding of the subject string.
3118 Label seq_ascii_string, seq_two_byte_string, check_code;
3119 __ mov(eax, Operand(esp, kSubjectOffset));
3120 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3121 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3122 // First check for flat two byte string.
3123 __ and_(ebx,
3124 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3125 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3126 __ j(zero, &seq_two_byte_string);
3127 // Any other flat string must be a flat ascii string.
3128 __ test(Operand(ebx),
3129 Immediate(kIsNotStringMask | kStringRepresentationMask));
3130 __ j(zero, &seq_ascii_string);
3131
3132 // Check for flat cons string.
3133 // A flat cons string is a cons string where the second part is the empty
3134 // string. In that case the subject string is just the first part of the cons
3135 // string. Also in this case the first part of the cons string is known to be
3136 // a sequential string or an external string.
3137 STATIC_ASSERT(kExternalStringTag != 0);
3138 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3139 __ test(Operand(ebx),
3140 Immediate(kIsNotStringMask | kExternalStringTag));
3141 __ j(not_zero, &runtime);
3142 // String is a cons string.
3143 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003144 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003145 __ j(not_equal, &runtime);
3146 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3147 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3148 // String is a cons string with empty second part.
3149 // eax: first part of cons string.
3150 // ebx: map of first part of cons string.
3151 // Is first part a flat two byte string?
3152 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3153 kStringRepresentationMask | kStringEncodingMask);
3154 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3155 __ j(zero, &seq_two_byte_string);
3156 // Any other flat string must be ascii.
3157 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3158 kStringRepresentationMask);
3159 __ j(not_zero, &runtime);
3160
3161 __ bind(&seq_ascii_string);
3162 // eax: subject string (flat ascii)
3163 // ecx: RegExp data (FixedArray)
3164 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3165 __ Set(edi, Immediate(1)); // Type is ascii.
3166 __ jmp(&check_code);
3167
3168 __ bind(&seq_two_byte_string);
3169 // eax: subject string (flat two byte)
3170 // ecx: RegExp data (FixedArray)
3171 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3172 __ Set(edi, Immediate(0)); // Type is two byte.
3173
3174 __ bind(&check_code);
3175 // Check that the irregexp code has been generated for the actual string
3176 // encoding. If it has, the field contains a code object otherwise it contains
3177 // the hole.
3178 __ CmpObjectType(edx, CODE_TYPE, ebx);
3179 __ j(not_equal, &runtime);
3180
3181 // eax: subject string
3182 // edx: code
3183 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3184 // Load used arguments before starting to push arguments for call to native
3185 // RegExp code to avoid handling changing stack height.
3186 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3187 __ SmiUntag(ebx); // Previous index from smi.
3188
3189 // eax: subject string
3190 // ebx: previous index
3191 // edx: code
3192 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3193 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003194 Counters* counters = masm->isolate()->counters();
3195 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003196
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003197 // Isolates: note we add an additional parameter here (isolate pointer).
3198 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003199 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003200
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003201 // Argument 8: Pass current isolate address.
3202 __ mov(Operand(esp, 7 * kPointerSize),
3203 Immediate(ExternalReference::isolate_address()));
3204
ricow@chromium.org65fae842010-08-25 15:26:24 +00003205 // Argument 7: Indicate that this is a direct call from JavaScript.
3206 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3207
3208 // Argument 6: Start (high end) of backtracking stack memory area.
3209 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3210 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3211 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3212
3213 // Argument 5: static offsets vector buffer.
3214 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003215 Immediate(ExternalReference::address_of_static_offsets_vector(
3216 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003217
3218 // Argument 4: End of string data
3219 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003220 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003221 __ test(edi, Operand(edi));
3222 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003223 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003224 __ SmiUntag(edi);
3225 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3226 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3227 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3228 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003229 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003230
3231 __ bind(&setup_two_byte);
3232 STATIC_ASSERT(kSmiTag == 0);
3233 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3234 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3235 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3236 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3237 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3238
3239 __ bind(&setup_rest);
3240
3241 // Argument 2: Previous index.
3242 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3243
3244 // Argument 1: Subject string.
3245 __ mov(Operand(esp, 0 * kPointerSize), eax);
3246
3247 // Locate the code entry and call it.
3248 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003249 __ call(Operand(edx));
3250
3251 // Drop arguments and come back to JS mode.
3252 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003253
3254 // Check the result.
3255 Label success;
3256 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003257 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003258 Label failure;
3259 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003260 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003261 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3262 // If not exception it can only be retry. Handle that in the runtime system.
3263 __ j(not_equal, &runtime);
3264 // Result must now be exception. If there is no pending exception already a
3265 // stack overflow (on the backtrack stack) was detected in RegExp code but
3266 // haven't created the exception yet. Handle that in the runtime system.
3267 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003268 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3269 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003270 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003271 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3272 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003273 __ mov(eax, Operand::StaticVariable(pending_exception));
3274 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003275 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003276 // For exception, throw the exception again.
3277
3278 // Clear the pending exception variable.
3279 __ mov(Operand::StaticVariable(pending_exception), edx);
3280
3281 // Special handling of termination exceptions which are uncatchable
3282 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003283 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003284 Label throw_termination_exception;
3285 __ j(equal, &throw_termination_exception);
3286
3287 // Handle normal exception by following handler chain.
3288 __ Throw(eax);
3289
3290 __ bind(&throw_termination_exception);
3291 __ ThrowUncatchable(TERMINATION, eax);
3292
ricow@chromium.org65fae842010-08-25 15:26:24 +00003293 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003294 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003295 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003296 __ ret(4 * kPointerSize);
3297
3298 // Load RegExp data.
3299 __ bind(&success);
3300 __ mov(eax, Operand(esp, kJSRegExpOffset));
3301 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3302 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3303 // Calculate number of capture registers (number_of_captures + 1) * 2.
3304 STATIC_ASSERT(kSmiTag == 0);
3305 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3306 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3307
3308 // edx: Number of capture registers
3309 // Load last_match_info which is still known to be a fast case JSArray.
3310 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3311 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3312
3313 // ebx: last_match_info backing store (FixedArray)
3314 // edx: number of capture registers
3315 // Store the capture count.
3316 __ SmiTag(edx); // Number of capture registers to smi.
3317 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3318 __ SmiUntag(edx); // Number of capture registers back from smi.
3319 // Store last subject and last input.
3320 __ mov(eax, Operand(esp, kSubjectOffset));
3321 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3322 __ mov(ecx, ebx);
3323 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3324 __ mov(eax, Operand(esp, kSubjectOffset));
3325 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3326 __ mov(ecx, ebx);
3327 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3328
3329 // Get the static offsets vector filled by the native regexp code.
3330 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003331 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003332 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3333
3334 // ebx: last_match_info backing store (FixedArray)
3335 // ecx: offsets vector
3336 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003337 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003338 // Capture register counter starts from number of capture registers and
3339 // counts down until wraping after zero.
3340 __ bind(&next_capture);
3341 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003342 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003343 // Read the value from the static offsets vector buffer.
3344 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3345 __ SmiTag(edi);
3346 // Store the smi value in the last match info.
3347 __ mov(FieldOperand(ebx,
3348 edx,
3349 times_pointer_size,
3350 RegExpImpl::kFirstCaptureOffset),
3351 edi);
3352 __ jmp(&next_capture);
3353 __ bind(&done);
3354
3355 // Return last match info.
3356 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3357 __ ret(4 * kPointerSize);
3358
3359 // Do the runtime call to execute the regexp.
3360 __ bind(&runtime);
3361 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3362#endif // V8_INTERPRETED_REGEXP
3363}
3364
3365
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003366void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3367 const int kMaxInlineLength = 100;
3368 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003369 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003370 __ mov(ebx, Operand(esp, kPointerSize * 3));
3371 __ test(ebx, Immediate(kSmiTagMask));
3372 __ j(not_zero, &slowcase);
3373 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3374 __ j(above, &slowcase);
3375 // Smi-tagging is equivalent to multiplying by 2.
3376 STATIC_ASSERT(kSmiTag == 0);
3377 STATIC_ASSERT(kSmiTagSize == 1);
3378 // Allocate RegExpResult followed by FixedArray with size in ebx.
3379 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3380 // Elements: [Map][Length][..elements..]
3381 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3382 times_half_pointer_size,
3383 ebx, // In: Number of elements (times 2, being a smi)
3384 eax, // Out: Start of allocation (tagged).
3385 ecx, // Out: End of allocation.
3386 edx, // Scratch register
3387 &slowcase,
3388 TAG_OBJECT);
3389 // eax: Start of allocated area, object-tagged.
3390
3391 // Set JSArray map to global.regexp_result_map().
3392 // Set empty properties FixedArray.
3393 // Set elements to point to FixedArray allocated right after the JSArray.
3394 // Interleave operations for better latency.
3395 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003396 Factory* factory = masm->isolate()->factory();
3397 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003398 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3399 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3400 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3401 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3402 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3403 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3404
3405 // Set input, index and length fields from arguments.
3406 __ mov(ecx, Operand(esp, kPointerSize * 1));
3407 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3408 __ mov(ecx, Operand(esp, kPointerSize * 2));
3409 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3410 __ mov(ecx, Operand(esp, kPointerSize * 3));
3411 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3412
3413 // Fill out the elements FixedArray.
3414 // eax: JSArray.
3415 // ebx: FixedArray.
3416 // ecx: Number of elements in array, as smi.
3417
3418 // Set map.
3419 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003420 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003421 // Set length.
3422 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3423 // Fill contents of fixed-array with the-hole.
3424 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003425 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003426 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3427 // Fill fixed array elements with hole.
3428 // eax: JSArray.
3429 // ecx: Number of elements to fill.
3430 // ebx: Start of elements in FixedArray.
3431 // edx: the hole.
3432 Label loop;
3433 __ test(ecx, Operand(ecx));
3434 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003435 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003436 __ sub(Operand(ecx), Immediate(1));
3437 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3438 __ jmp(&loop);
3439
3440 __ bind(&done);
3441 __ ret(3 * kPointerSize);
3442
3443 __ bind(&slowcase);
3444 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3445}
3446
3447
ricow@chromium.org65fae842010-08-25 15:26:24 +00003448void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3449 Register object,
3450 Register result,
3451 Register scratch1,
3452 Register scratch2,
3453 bool object_is_smi,
3454 Label* not_found) {
3455 // Use of registers. Register result is used as a temporary.
3456 Register number_string_cache = result;
3457 Register mask = scratch1;
3458 Register scratch = scratch2;
3459
3460 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003461 ExternalReference roots_address =
3462 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003463 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3464 __ mov(number_string_cache,
3465 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3466 // Make the hash mask from the length of the number string cache. It
3467 // contains two elements (number and string) for each cache entry.
3468 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3469 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3470 __ sub(Operand(mask), Immediate(1)); // Make mask.
3471
3472 // Calculate the entry in the number string cache. The hash value in the
3473 // number string cache for smis is just the smi value, and the hash for
3474 // doubles is the xor of the upper and lower words. See
3475 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003476 Label smi_hash_calculated;
3477 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003478 if (object_is_smi) {
3479 __ mov(scratch, object);
3480 __ SmiUntag(scratch);
3481 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003482 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003483 STATIC_ASSERT(kSmiTag == 0);
3484 __ test(object, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003485 __ j(not_zero, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003486 __ mov(scratch, object);
3487 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003488 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003489 __ bind(&not_smi);
3490 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003491 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003492 __ j(not_equal, not_found);
3493 STATIC_ASSERT(8 == kDoubleSize);
3494 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3495 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3496 // Object is heap number and hash is now in scratch. Calculate cache index.
3497 __ and_(scratch, Operand(mask));
3498 Register index = scratch;
3499 Register probe = mask;
3500 __ mov(probe,
3501 FieldOperand(number_string_cache,
3502 index,
3503 times_twice_pointer_size,
3504 FixedArray::kHeaderSize));
3505 __ test(probe, Immediate(kSmiTagMask));
3506 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003507 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003508 CpuFeatures::Scope fscope(SSE2);
3509 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3510 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3511 __ ucomisd(xmm0, xmm1);
3512 } else {
3513 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3514 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3515 __ FCmp();
3516 }
3517 __ j(parity_even, not_found); // Bail out if NaN is involved.
3518 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003519 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003520 }
3521
3522 __ bind(&smi_hash_calculated);
3523 // Object is smi and hash is now in scratch. Calculate cache index.
3524 __ and_(scratch, Operand(mask));
3525 Register index = scratch;
3526 // Check if the entry is the smi we are looking for.
3527 __ cmp(object,
3528 FieldOperand(number_string_cache,
3529 index,
3530 times_twice_pointer_size,
3531 FixedArray::kHeaderSize));
3532 __ j(not_equal, not_found);
3533
3534 // Get the result from the cache.
3535 __ bind(&load_result_from_cache);
3536 __ mov(result,
3537 FieldOperand(number_string_cache,
3538 index,
3539 times_twice_pointer_size,
3540 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003541 Counters* counters = masm->isolate()->counters();
3542 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003543}
3544
3545
3546void NumberToStringStub::Generate(MacroAssembler* masm) {
3547 Label runtime;
3548
3549 __ mov(ebx, Operand(esp, kPointerSize));
3550
3551 // Generate code to lookup number in the number string cache.
3552 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3553 __ ret(1 * kPointerSize);
3554
3555 __ bind(&runtime);
3556 // Handle number to string in the runtime system if not found in the cache.
3557 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3558}
3559
3560
3561static int NegativeComparisonResult(Condition cc) {
3562 ASSERT(cc != equal);
3563 ASSERT((cc == less) || (cc == less_equal)
3564 || (cc == greater) || (cc == greater_equal));
3565 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3566}
3567
3568void CompareStub::Generate(MacroAssembler* masm) {
3569 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3570
3571 Label check_unequal_objects, done;
3572
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003573 // Compare two smis if required.
3574 if (include_smi_compare_) {
3575 Label non_smi, smi_done;
3576 __ mov(ecx, Operand(edx));
3577 __ or_(ecx, Operand(eax));
3578 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003579 __ j(not_zero, &non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003580 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3581 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003582 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003583 __ bind(&smi_done);
3584 __ mov(eax, edx);
3585 __ ret(0);
3586 __ bind(&non_smi);
3587 } else if (FLAG_debug_code) {
3588 __ mov(ecx, Operand(edx));
3589 __ or_(ecx, Operand(eax));
3590 __ test(ecx, Immediate(kSmiTagMask));
3591 __ Assert(not_zero, "Unexpected smi operands.");
3592 }
3593
ricow@chromium.org65fae842010-08-25 15:26:24 +00003594 // NOTICE! This code is only reached after a smi-fast-case check, so
3595 // it is certain that at least one operand isn't a smi.
3596
3597 // Identical objects can be compared fast, but there are some tricky cases
3598 // for NaN and undefined.
3599 {
3600 Label not_identical;
3601 __ cmp(eax, Operand(edx));
3602 __ j(not_equal, &not_identical);
3603
3604 if (cc_ != equal) {
3605 // Check for undefined. undefined OP undefined is false even though
3606 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003607 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003608 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003609 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003610 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3611 __ ret(0);
3612 __ bind(&check_for_nan);
3613 }
3614
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003615 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003616 // so we do the second best thing - test it ourselves.
3617 // Note: if cc_ != equal, never_nan_nan_ is not used.
3618 if (never_nan_nan_ && (cc_ == equal)) {
3619 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3620 __ ret(0);
3621 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003622 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003623 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003624 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003625 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003626 if (cc_ != equal) {
3627 // Call runtime on identical JSObjects. Otherwise return equal.
3628 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3629 __ j(above_equal, &not_identical);
3630 }
3631 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3632 __ ret(0);
3633
3634 __ bind(&heap_number);
3635 // It is a heap number, so return non-equal if it's NaN and equal if
3636 // it's not NaN.
3637 // The representation of NaN values has all exponent bits (52..62) set,
3638 // and not all mantissa bits (0..51) clear.
3639 // We only accept QNaNs, which have bit 51 set.
3640 // Read top bits of double representation (second word of value).
3641
3642 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3643 // all bits in the mask are set. We only need to check the word
3644 // that contains the exponent and high bit of the mantissa.
3645 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3646 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003647 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003648 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3649 // bits.
3650 __ add(edx, Operand(edx));
3651 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3652 if (cc_ == equal) {
3653 STATIC_ASSERT(EQUAL != 1);
3654 __ setcc(above_equal, eax);
3655 __ ret(0);
3656 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003657 Label nan;
3658 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003659 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3660 __ ret(0);
3661 __ bind(&nan);
3662 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3663 __ ret(0);
3664 }
3665 }
3666
3667 __ bind(&not_identical);
3668 }
3669
3670 // Strict equality can quickly decide whether objects are equal.
3671 // Non-strict object equality is slower, so it is handled later in the stub.
3672 if (cc_ == equal && strict_) {
3673 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003674 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003675 // If we're doing a strict equality comparison, we don't have to do
3676 // type conversion, so we generate code to do fast comparison for objects
3677 // and oddballs. Non-smi numbers and strings still go through the usual
3678 // slow-case code.
3679 // If either is a Smi (we know that not both are), then they can only
3680 // be equal if the other is a HeapNumber. If so, use the slow case.
3681 STATIC_ASSERT(kSmiTag == 0);
3682 ASSERT_EQ(0, Smi::FromInt(0));
3683 __ mov(ecx, Immediate(kSmiTagMask));
3684 __ and_(ecx, Operand(eax));
3685 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003686 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003687 // One operand is a smi.
3688
3689 // Check whether the non-smi is a heap number.
3690 STATIC_ASSERT(kSmiTagMask == 1);
3691 // ecx still holds eax & kSmiTag, which is either zero or one.
3692 __ sub(Operand(ecx), Immediate(0x01));
3693 __ mov(ebx, edx);
3694 __ xor_(ebx, Operand(eax));
3695 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3696 __ xor_(ebx, Operand(eax));
3697 // if eax was smi, ebx is now edx, else eax.
3698
3699 // Check if the non-smi operand is a heap number.
3700 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003701 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003702 // If heap number, handle it in the slow case.
3703 __ j(equal, &slow);
3704 // Return non-equal (ebx is not zero)
3705 __ mov(eax, ebx);
3706 __ ret(0);
3707
3708 __ bind(&not_smis);
3709 // If either operand is a JSObject or an oddball value, then they are not
3710 // equal since their pointers are different
3711 // There is no test for undetectability in strict equality.
3712
3713 // Get the type of the first operand.
3714 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003715 Label first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003716 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3717 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003718 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003719
3720 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003721 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003722 STATIC_ASSERT(kHeapObjectTag != 0);
3723 __ bind(&return_not_equal);
3724 __ ret(0);
3725
3726 __ bind(&first_non_object);
3727 // Check for oddballs: true, false, null, undefined.
3728 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3729 __ j(equal, &return_not_equal);
3730
3731 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
3732 __ j(above_equal, &return_not_equal);
3733
3734 // Check for oddballs: true, false, null, undefined.
3735 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3736 __ j(equal, &return_not_equal);
3737
3738 // Fall through to the general case.
3739 __ bind(&slow);
3740 }
3741
3742 // Generate the number comparison code.
3743 if (include_number_compare_) {
3744 Label non_number_comparison;
3745 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003746 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003747 CpuFeatures::Scope use_sse2(SSE2);
3748 CpuFeatures::Scope use_cmov(CMOV);
3749
3750 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3751 __ ucomisd(xmm0, xmm1);
3752
3753 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003754 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003755 // Return a result of -1, 0, or 1, based on EFLAGS.
3756 __ mov(eax, 0); // equal
3757 __ mov(ecx, Immediate(Smi::FromInt(1)));
3758 __ cmov(above, eax, Operand(ecx));
3759 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3760 __ cmov(below, eax, Operand(ecx));
3761 __ ret(0);
3762 } else {
3763 FloatingPointHelper::CheckFloatOperands(
3764 masm, &non_number_comparison, ebx);
3765 FloatingPointHelper::LoadFloatOperand(masm, eax);
3766 FloatingPointHelper::LoadFloatOperand(masm, edx);
3767 __ FCmp();
3768
3769 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003770 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003771
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003772 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003773 // Return a result of -1, 0, or 1, based on EFLAGS.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003774 __ j(below, &below_label);
3775 __ j(above, &above_label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003776
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003777 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003778 __ ret(0);
3779
3780 __ bind(&below_label);
3781 __ mov(eax, Immediate(Smi::FromInt(-1)));
3782 __ ret(0);
3783
3784 __ bind(&above_label);
3785 __ mov(eax, Immediate(Smi::FromInt(1)));
3786 __ ret(0);
3787 }
3788
3789 // If one of the numbers was NaN, then the result is always false.
3790 // The cc is never not-equal.
3791 __ bind(&unordered);
3792 ASSERT(cc_ != not_equal);
3793 if (cc_ == less || cc_ == less_equal) {
3794 __ mov(eax, Immediate(Smi::FromInt(1)));
3795 } else {
3796 __ mov(eax, Immediate(Smi::FromInt(-1)));
3797 }
3798 __ ret(0);
3799
3800 // The number comparison code did not provide a valid result.
3801 __ bind(&non_number_comparison);
3802 }
3803
3804 // Fast negative check for symbol-to-symbol equality.
3805 Label check_for_strings;
3806 if (cc_ == equal) {
3807 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
3808 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
3809
3810 // We've already checked for object identity, so if both operands
3811 // are symbols they aren't equal. Register eax already holds a
3812 // non-zero value, which indicates not equal, so just return.
3813 __ ret(0);
3814 }
3815
3816 __ bind(&check_for_strings);
3817
3818 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
3819 &check_unequal_objects);
3820
3821 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003822 if (cc_ == equal) {
3823 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00003824 edx,
3825 eax,
3826 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00003827 ebx);
3828 } else {
3829 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3830 edx,
3831 eax,
3832 ecx,
3833 ebx,
3834 edi);
3835 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003836#ifdef DEBUG
3837 __ Abort("Unexpected fall-through from string comparison");
3838#endif
3839
3840 __ bind(&check_unequal_objects);
3841 if (cc_ == equal && !strict_) {
3842 // Non-strict equality. Objects are unequal if
3843 // they are both JSObjects and not undetectable,
3844 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003845 Label not_both_objects;
3846 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003847 // At most one is a smi, so we can test for smi by adding the two.
3848 // A smi plus a heap object has the low bit set, a heap object plus
3849 // a heap object has the low bit clear.
3850 STATIC_ASSERT(kSmiTag == 0);
3851 STATIC_ASSERT(kSmiTagMask == 1);
3852 __ lea(ecx, Operand(eax, edx, times_1, 0));
3853 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003854 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003855 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003856 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003857 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003858 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003859 // We do not bail out after this point. Both are JSObjects, and
3860 // they are equal if and only if both are undetectable.
3861 // The and of the undetectable flags is 1 if and only if they are equal.
3862 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3863 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003864 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003865 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
3866 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003867 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003868 // The objects are both undetectable, so they both compare as the value
3869 // undefined, and are equal.
3870 __ Set(eax, Immediate(EQUAL));
3871 __ bind(&return_unequal);
3872 // Return non-equal by returning the non-zero object pointer in eax,
3873 // or return equal if we fell through to here.
3874 __ ret(0); // rax, rdx were pushed
3875 __ bind(&not_both_objects);
3876 }
3877
3878 // Push arguments below the return address.
3879 __ pop(ecx);
3880 __ push(edx);
3881 __ push(eax);
3882
3883 // Figure out which native to call and setup the arguments.
3884 Builtins::JavaScript builtin;
3885 if (cc_ == equal) {
3886 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3887 } else {
3888 builtin = Builtins::COMPARE;
3889 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3890 }
3891
3892 // Restore return address on the stack.
3893 __ push(ecx);
3894
3895 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3896 // tagged as a small integer.
3897 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3898}
3899
3900
3901void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3902 Label* label,
3903 Register object,
3904 Register scratch) {
3905 __ test(object, Immediate(kSmiTagMask));
3906 __ j(zero, label);
3907 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
3908 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3909 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
3910 __ cmp(scratch, kSymbolTag | kStringTag);
3911 __ j(not_equal, label);
3912}
3913
3914
3915void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003916 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003917}
3918
3919
3920void CallFunctionStub::Generate(MacroAssembler* masm) {
3921 Label slow;
3922
3923 // If the receiver might be a value (string, number or boolean) check for this
3924 // and box it if it is.
3925 if (ReceiverMightBeValue()) {
3926 // Get the receiver from the stack.
3927 // +1 ~ return address
3928 Label receiver_is_value, receiver_is_js_object;
3929 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
3930
3931 // Check if receiver is a smi (which is a number value).
3932 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003933 __ j(zero, &receiver_is_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003934
3935 // Check if the receiver is a valid JS object.
3936 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
3937 __ j(above_equal, &receiver_is_js_object);
3938
3939 // Call the runtime to box the value.
3940 __ bind(&receiver_is_value);
3941 __ EnterInternalFrame();
3942 __ push(eax);
3943 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
3944 __ LeaveInternalFrame();
3945 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
3946
3947 __ bind(&receiver_is_js_object);
3948 }
3949
3950 // Get the function to call from the stack.
3951 // +2 ~ receiver, return address
3952 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
3953
3954 // Check that the function really is a JavaScript function.
3955 __ test(edi, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003956 __ j(zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003957 // Goto slow case if we do not have a function.
3958 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003959 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003960
3961 // Fast-case: Just invoke the function.
3962 ParameterCount actual(argc_);
3963 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
3964
3965 // Slow-case: Non-function called.
3966 __ bind(&slow);
3967 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3968 // of the original receiver from the call site).
3969 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
3970 __ Set(eax, Immediate(argc_));
3971 __ Set(ebx, Immediate(0));
3972 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003973 Handle<Code> adaptor =
3974 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003975 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3976}
3977
3978
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003979bool CEntryStub::NeedsImmovableCode() {
3980 return false;
3981}
3982
3983
ricow@chromium.org65fae842010-08-25 15:26:24 +00003984void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003985 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003986}
3987
3988
ricow@chromium.org65fae842010-08-25 15:26:24 +00003989void CEntryStub::GenerateCore(MacroAssembler* masm,
3990 Label* throw_normal_exception,
3991 Label* throw_termination_exception,
3992 Label* throw_out_of_memory_exception,
3993 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00003994 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003995 // eax: result parameter for PerformGC, if any
3996 // ebx: pointer to C function (C callee-saved)
3997 // ebp: frame pointer (restored after C call)
3998 // esp: stack pointer (restored after C call)
3999 // edi: number of arguments including receiver (C callee-saved)
4000 // esi: pointer to the first argument (C callee-saved)
4001
4002 // Result returned in eax, or eax+edx if result_size_ is 2.
4003
4004 // Check stack alignment.
4005 if (FLAG_debug_code) {
4006 __ CheckStackAlignment();
4007 }
4008
4009 if (do_gc) {
4010 // Pass failure code returned from last attempt as first argument to
4011 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4012 // stack alignment is known to be correct. This function takes one argument
4013 // which is passed on the stack, and we know that the stack has been
4014 // prepared to pass at least one argument.
4015 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4016 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4017 }
4018
4019 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004020 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004021 if (always_allocate_scope) {
4022 __ inc(Operand::StaticVariable(scope_depth));
4023 }
4024
4025 // Call C function.
4026 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4027 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004028 __ mov(Operand(esp, 2 * kPointerSize),
4029 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004030 __ call(Operand(ebx));
4031 // Result is in eax or edx:eax - do not destroy these registers!
4032
4033 if (always_allocate_scope) {
4034 __ dec(Operand::StaticVariable(scope_depth));
4035 }
4036
4037 // Make sure we're not trying to return 'the hole' from the runtime
4038 // call as this may lead to crashes in the IC code later.
4039 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004040 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004041 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004042 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004043 __ int3();
4044 __ bind(&okay);
4045 }
4046
4047 // Check for failure result.
4048 Label failure_returned;
4049 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4050 __ lea(ecx, Operand(eax, 1));
4051 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4052 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004053 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004054
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004055 ExternalReference pending_exception_address(
4056 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004057
4058 // Check that there is no pending exception, otherwise we
4059 // should have returned some failure value.
4060 if (FLAG_debug_code) {
4061 __ push(edx);
4062 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004063 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004064 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004065 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4066 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004067 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004068 __ int3();
4069 __ bind(&okay);
4070 __ pop(edx);
4071 }
4072
ricow@chromium.org65fae842010-08-25 15:26:24 +00004073 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004074 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004075 __ ret(0);
4076
4077 // Handling of failure.
4078 __ bind(&failure_returned);
4079
4080 Label retry;
4081 // If the returned exception is RETRY_AFTER_GC continue at retry label
4082 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4083 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004084 __ j(zero, &retry);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004085
4086 // Special handling of out of memory exceptions.
4087 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4088 __ j(equal, throw_out_of_memory_exception);
4089
4090 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004091 ExternalReference the_hole_location =
4092 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004093 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004094 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004095 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4096
4097 // Special handling of termination exceptions which are uncatchable
4098 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004099 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004100 __ j(equal, throw_termination_exception);
4101
4102 // Handle normal exception.
4103 __ jmp(throw_normal_exception);
4104
4105 // Retry.
4106 __ bind(&retry);
4107}
4108
4109
4110void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4111 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004112 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004113}
4114
4115
4116void CEntryStub::Generate(MacroAssembler* masm) {
4117 // eax: number of arguments including receiver
4118 // ebx: pointer to C function (C callee-saved)
4119 // ebp: frame pointer (restored after C call)
4120 // esp: stack pointer (restored after C call)
4121 // esi: current context (C callee-saved)
4122 // edi: JS function of the caller (C callee-saved)
4123
4124 // NOTE: Invocations of builtins may return failure objects instead
4125 // of a proper result. The builtin entry handles this by performing
4126 // a garbage collection and retrying the builtin (twice).
4127
4128 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004129 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004130
4131 // eax: result parameter for PerformGC, if any (setup below)
4132 // ebx: pointer to builtin function (C callee-saved)
4133 // ebp: frame pointer (restored after C call)
4134 // esp: stack pointer (restored after C call)
4135 // edi: number of arguments including receiver (C callee-saved)
4136 // esi: argv pointer (C callee-saved)
4137
4138 Label throw_normal_exception;
4139 Label throw_termination_exception;
4140 Label throw_out_of_memory_exception;
4141
4142 // Call into the runtime system.
4143 GenerateCore(masm,
4144 &throw_normal_exception,
4145 &throw_termination_exception,
4146 &throw_out_of_memory_exception,
4147 false,
4148 false);
4149
4150 // Do space-specific GC and retry runtime call.
4151 GenerateCore(masm,
4152 &throw_normal_exception,
4153 &throw_termination_exception,
4154 &throw_out_of_memory_exception,
4155 true,
4156 false);
4157
4158 // Do full GC and retry runtime call one final time.
4159 Failure* failure = Failure::InternalError();
4160 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4161 GenerateCore(masm,
4162 &throw_normal_exception,
4163 &throw_termination_exception,
4164 &throw_out_of_memory_exception,
4165 true,
4166 true);
4167
4168 __ bind(&throw_out_of_memory_exception);
4169 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4170
4171 __ bind(&throw_termination_exception);
4172 GenerateThrowUncatchable(masm, TERMINATION);
4173
4174 __ bind(&throw_normal_exception);
4175 GenerateThrowTOS(masm);
4176}
4177
4178
4179void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4180 Label invoke, exit;
4181#ifdef ENABLE_LOGGING_AND_PROFILING
4182 Label not_outermost_js, not_outermost_js_2;
4183#endif
4184
4185 // Setup frame.
4186 __ push(ebp);
4187 __ mov(ebp, Operand(esp));
4188
4189 // Push marker in two places.
4190 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4191 __ push(Immediate(Smi::FromInt(marker))); // context slot
4192 __ push(Immediate(Smi::FromInt(marker))); // function slot
4193 // Save callee-saved registers (C calling conventions).
4194 __ push(edi);
4195 __ push(esi);
4196 __ push(ebx);
4197
4198 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004199 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004200 __ push(Operand::StaticVariable(c_entry_fp));
4201
4202#ifdef ENABLE_LOGGING_AND_PROFILING
4203 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004204 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4205 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004206 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4207 __ j(not_equal, &not_outermost_js);
4208 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004209 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4210 Label cont;
4211 __ jmp(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004212 __ bind(&not_outermost_js);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004213 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
4214 __ bind(&cont);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004215#endif
4216
4217 // Call a faked try-block that does the invoke.
4218 __ call(&invoke);
4219
4220 // Caught exception: Store result (exception) in the pending
4221 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004222 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4223 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004224 __ mov(Operand::StaticVariable(pending_exception), eax);
4225 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4226 __ jmp(&exit);
4227
4228 // Invoke: Link this frame into the handler chain.
4229 __ bind(&invoke);
4230 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4231
4232 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004233 ExternalReference the_hole_location =
4234 ExternalReference::the_hole_value_location(masm->isolate());
4235 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004236 __ mov(Operand::StaticVariable(pending_exception), edx);
4237
4238 // Fake a receiver (NULL).
4239 __ push(Immediate(0)); // receiver
4240
4241 // Invoke the function by calling through JS entry trampoline
4242 // builtin and pop the faked function when we return. Notice that we
4243 // cannot store a reference to the trampoline code directly in this
4244 // stub, because the builtin stubs may not have been generated yet.
4245 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004246 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004247 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004248 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004249 __ mov(edx, Immediate(construct_entry));
4250 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004251 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004252 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004253 __ mov(edx, Immediate(entry));
4254 }
4255 __ mov(edx, Operand(edx, 0)); // deref address
4256 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4257 __ call(Operand(edx));
4258
4259 // Unlink this frame from the handler chain.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004260 __ PopTryHandler();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004261
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004262 __ bind(&exit);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004263#ifdef ENABLE_LOGGING_AND_PROFILING
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004264 // Check if the current stack frame is marked as the outermost JS frame.
4265 __ pop(ebx);
4266 __ cmp(Operand(ebx),
4267 Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004268 __ j(not_equal, &not_outermost_js_2);
4269 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4270 __ bind(&not_outermost_js_2);
4271#endif
4272
4273 // Restore the top frame descriptor from the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004274 __ pop(Operand::StaticVariable(ExternalReference(
4275 Isolate::k_c_entry_fp_address,
4276 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004277
4278 // Restore callee-saved registers (C calling conventions).
4279 __ pop(ebx);
4280 __ pop(esi);
4281 __ pop(edi);
4282 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4283
4284 // Restore frame pointer and return.
4285 __ pop(ebp);
4286 __ ret(0);
4287}
4288
4289
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004290// Generate stub code for instanceof.
4291// This code can patch a call site inlined cache of the instance of check,
4292// which looks like this.
4293//
4294// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4295// 75 0a jne <some near label>
4296// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4297//
4298// If call site patching is requested the stack will have the delta from the
4299// return address to the cmp instruction just below the return address. This
4300// also means that call site patching can only take place with arguments in
4301// registers. TOS looks like this when call site patching is requested
4302//
4303// esp[0] : return address
4304// esp[4] : delta from return address to cmp instruction
4305//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004306void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004307 // Call site inlining and patching implies arguments in registers.
4308 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4309
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004310 // Fixed register usage throughout the stub.
4311 Register object = eax; // Object (lhs).
4312 Register map = ebx; // Map of the object.
4313 Register function = edx; // Function (rhs).
4314 Register prototype = edi; // Prototype of the function.
4315 Register scratch = ecx;
4316
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004317 // Constants describing the call site code to patch.
4318 static const int kDeltaToCmpImmediate = 2;
4319 static const int kDeltaToMov = 8;
4320 static const int kDeltaToMovImmediate = 9;
4321 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4322 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4323 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4324
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004325 ExternalReference roots_address =
4326 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004327
4328 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4329 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4330
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004331 // Get the object and function - they are always both needed.
4332 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004333 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004334 __ mov(object, Operand(esp, 2 * kPointerSize));
4335 __ mov(function, Operand(esp, 1 * kPointerSize));
4336 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004337
4338 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004339 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004340 __ j(zero, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004341 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004342
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004343 // If there is a call site cache don't look in the global cache, but do the
4344 // real lookup and update the call site cache.
4345 if (!HasCallSiteInlineCheck()) {
4346 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004347 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004348 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4349 __ cmp(function,
4350 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004351 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004352 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4353 __ cmp(map, Operand::StaticArray(
4354 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004355 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004356 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4357 __ mov(eax, Operand::StaticArray(
4358 scratch, times_pointer_size, roots_address));
4359 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4360 __ bind(&miss);
4361 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004362
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004363 // Get the prototype of the function.
4364 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004365
4366 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004367 __ test(prototype, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004368 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004369 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004370
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004371 // Update the global instanceof or call site inlined cache with the current
4372 // map and function. The cached answer will be set when it is known below.
4373 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004374 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4375 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4376 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4377 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4378 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004379 } else {
4380 // The constants for the code patching are based on no push instructions
4381 // at the call site.
4382 ASSERT(HasArgsInRegisters());
4383 // Get return address and delta to inlined map check.
4384 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4385 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4386 if (FLAG_debug_code) {
4387 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4388 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4389 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4390 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4391 }
4392 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4393 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004394
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004395 // Loop through the prototype chain of the object looking for the function
4396 // prototype.
4397 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004398 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004399 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004400 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004401 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004402 Factory* factory = masm->isolate()->factory();
4403 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004404 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004405 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4406 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004407 __ jmp(&loop);
4408
4409 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004410 if (!HasCallSiteInlineCheck()) {
4411 __ Set(eax, Immediate(0));
4412 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4413 __ mov(Operand::StaticArray(scratch,
4414 times_pointer_size, roots_address), eax);
4415 } else {
4416 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004417 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004418 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4419 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4420 if (FLAG_debug_code) {
4421 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4422 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4423 }
4424 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4425 if (!ReturnTrueFalseObject()) {
4426 __ Set(eax, Immediate(0));
4427 }
4428 }
4429 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004430
4431 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004432 if (!HasCallSiteInlineCheck()) {
4433 __ Set(eax, Immediate(Smi::FromInt(1)));
4434 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4435 __ mov(Operand::StaticArray(
4436 scratch, times_pointer_size, roots_address), eax);
4437 } else {
4438 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004439 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004440 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4441 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4442 if (FLAG_debug_code) {
4443 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4444 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4445 }
4446 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4447 if (!ReturnTrueFalseObject()) {
4448 __ Set(eax, Immediate(Smi::FromInt(1)));
4449 }
4450 }
4451 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004452
4453 Label object_not_null, object_not_null_or_smi;
4454 __ bind(&not_js_object);
4455 // Before null, smi and string value checks, check that the rhs is a function
4456 // as for a non-function rhs an exception needs to be thrown.
4457 __ test(function, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004458 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004459 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004460 __ j(not_equal, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004461
4462 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004463 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004464 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004465 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004466 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004467
4468 __ bind(&object_not_null);
4469 // Smi values is not instance of anything.
4470 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004471 __ j(not_zero, &object_not_null_or_smi);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004472 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004473 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004474
4475 __ bind(&object_not_null_or_smi);
4476 // String values is not instance of anything.
4477 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4478 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004479 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004480 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004481
4482 // Slow-case: Go through the JavaScript implementation.
4483 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004484 if (!ReturnTrueFalseObject()) {
4485 // Tail call the builtin which returns 0 or 1.
4486 if (HasArgsInRegisters()) {
4487 // Push arguments below return address.
4488 __ pop(scratch);
4489 __ push(object);
4490 __ push(function);
4491 __ push(scratch);
4492 }
4493 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4494 } else {
4495 // Call the builtin and convert 0/1 to true/false.
4496 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004497 __ push(object);
4498 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004499 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4500 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004501 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004502 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004503 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004504 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004505 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004506 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004507 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004508 __ bind(&done);
4509 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004510 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004511}
4512
4513
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004514Register InstanceofStub::left() { return eax; }
4515
4516
4517Register InstanceofStub::right() { return edx; }
4518
4519
ricow@chromium.org65fae842010-08-25 15:26:24 +00004520int CompareStub::MinorKey() {
4521 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4522 // stubs the never NaN NaN condition is only taken into account if the
4523 // condition is equals.
4524 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4525 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4526 return ConditionField::encode(static_cast<unsigned>(cc_))
4527 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4528 | StrictField::encode(strict_)
4529 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004530 | IncludeNumberCompareField::encode(include_number_compare_)
4531 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004532}
4533
4534
4535// Unfortunately you have to run without snapshots to see most of these
4536// names in the profile since most compare stubs end up in the snapshot.
4537const char* CompareStub::GetName() {
4538 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4539
4540 if (name_ != NULL) return name_;
4541 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004542 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4543 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004544 if (name_ == NULL) return "OOM";
4545
4546 const char* cc_name;
4547 switch (cc_) {
4548 case less: cc_name = "LT"; break;
4549 case greater: cc_name = "GT"; break;
4550 case less_equal: cc_name = "LE"; break;
4551 case greater_equal: cc_name = "GE"; break;
4552 case equal: cc_name = "EQ"; break;
4553 case not_equal: cc_name = "NE"; break;
4554 default: cc_name = "UnknownCondition"; break;
4555 }
4556
4557 const char* strict_name = "";
4558 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4559 strict_name = "_STRICT";
4560 }
4561
4562 const char* never_nan_nan_name = "";
4563 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4564 never_nan_nan_name = "_NO_NAN";
4565 }
4566
4567 const char* include_number_compare_name = "";
4568 if (!include_number_compare_) {
4569 include_number_compare_name = "_NO_NUMBER";
4570 }
4571
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004572 const char* include_smi_compare_name = "";
4573 if (!include_smi_compare_) {
4574 include_smi_compare_name = "_NO_SMI";
4575 }
4576
ricow@chromium.org65fae842010-08-25 15:26:24 +00004577 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004578 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004579 cc_name,
4580 strict_name,
4581 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004582 include_number_compare_name,
4583 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004584 return name_;
4585}
4586
4587
4588// -------------------------------------------------------------------------
4589// StringCharCodeAtGenerator
4590
4591void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4592 Label flat_string;
4593 Label ascii_string;
4594 Label got_char_code;
4595
4596 // If the receiver is a smi trigger the non-string case.
4597 STATIC_ASSERT(kSmiTag == 0);
4598 __ test(object_, Immediate(kSmiTagMask));
4599 __ j(zero, receiver_not_string_);
4600
4601 // Fetch the instance type of the receiver into result register.
4602 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4603 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4604 // If the receiver is not a string trigger the non-string case.
4605 __ test(result_, Immediate(kIsNotStringMask));
4606 __ j(not_zero, receiver_not_string_);
4607
4608 // If the index is non-smi trigger the non-smi case.
4609 STATIC_ASSERT(kSmiTag == 0);
4610 __ test(index_, Immediate(kSmiTagMask));
4611 __ j(not_zero, &index_not_smi_);
4612
4613 // Put smi-tagged index into scratch register.
4614 __ mov(scratch_, index_);
4615 __ bind(&got_smi_index_);
4616
4617 // Check for index out of range.
4618 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4619 __ j(above_equal, index_out_of_range_);
4620
4621 // We need special handling for non-flat strings.
4622 STATIC_ASSERT(kSeqStringTag == 0);
4623 __ test(result_, Immediate(kStringRepresentationMask));
4624 __ j(zero, &flat_string);
4625
4626 // Handle non-flat strings.
4627 __ test(result_, Immediate(kIsConsStringMask));
4628 __ j(zero, &call_runtime_);
4629
4630 // ConsString.
4631 // Check whether the right hand side is the empty string (i.e. if
4632 // this is really a flat string in a cons string). If that is not
4633 // the case we would rather go to the runtime system now to flatten
4634 // the string.
4635 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004636 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004637 __ j(not_equal, &call_runtime_);
4638 // Get the first of the two strings and load its instance type.
4639 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4640 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4641 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4642 // If the first cons component is also non-flat, then go to runtime.
4643 STATIC_ASSERT(kSeqStringTag == 0);
4644 __ test(result_, Immediate(kStringRepresentationMask));
4645 __ j(not_zero, &call_runtime_);
4646
4647 // Check for 1-byte or 2-byte string.
4648 __ bind(&flat_string);
4649 STATIC_ASSERT(kAsciiStringTag != 0);
4650 __ test(result_, Immediate(kStringEncodingMask));
4651 __ j(not_zero, &ascii_string);
4652
4653 // 2-byte string.
4654 // Load the 2-byte character code into the result register.
4655 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4656 __ movzx_w(result_, FieldOperand(object_,
4657 scratch_, times_1, // Scratch is smi-tagged.
4658 SeqTwoByteString::kHeaderSize));
4659 __ jmp(&got_char_code);
4660
4661 // ASCII string.
4662 // Load the byte into the result register.
4663 __ bind(&ascii_string);
4664 __ SmiUntag(scratch_);
4665 __ movzx_b(result_, FieldOperand(object_,
4666 scratch_, times_1,
4667 SeqAsciiString::kHeaderSize));
4668 __ bind(&got_char_code);
4669 __ SmiTag(result_);
4670 __ bind(&exit_);
4671}
4672
4673
4674void StringCharCodeAtGenerator::GenerateSlow(
4675 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4676 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4677
4678 // Index is not a smi.
4679 __ bind(&index_not_smi_);
4680 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004681 __ CheckMap(index_,
4682 masm->isolate()->factory()->heap_number_map(),
4683 index_not_number_,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004684 DONT_DO_SMI_CHECK);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004685 call_helper.BeforeCall(masm);
4686 __ push(object_);
4687 __ push(index_);
4688 __ push(index_); // Consumed by runtime conversion function.
4689 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4690 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4691 } else {
4692 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4693 // NumberToSmi discards numbers that are not exact integers.
4694 __ CallRuntime(Runtime::kNumberToSmi, 1);
4695 }
4696 if (!scratch_.is(eax)) {
4697 // Save the conversion result before the pop instructions below
4698 // have a chance to overwrite it.
4699 __ mov(scratch_, eax);
4700 }
4701 __ pop(index_);
4702 __ pop(object_);
4703 // Reload the instance type.
4704 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4705 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4706 call_helper.AfterCall(masm);
4707 // If index is still not a smi, it must be out of range.
4708 STATIC_ASSERT(kSmiTag == 0);
4709 __ test(scratch_, Immediate(kSmiTagMask));
4710 __ j(not_zero, index_out_of_range_);
4711 // Otherwise, return to the fast path.
4712 __ jmp(&got_smi_index_);
4713
4714 // Call runtime. We get here when the receiver is a string and the
4715 // index is a number, but the code of getting the actual character
4716 // is too complex (e.g., when the string needs to be flattened).
4717 __ bind(&call_runtime_);
4718 call_helper.BeforeCall(masm);
4719 __ push(object_);
4720 __ push(index_);
4721 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4722 if (!result_.is(eax)) {
4723 __ mov(result_, eax);
4724 }
4725 call_helper.AfterCall(masm);
4726 __ jmp(&exit_);
4727
4728 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4729}
4730
4731
4732// -------------------------------------------------------------------------
4733// StringCharFromCodeGenerator
4734
4735void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4736 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4737 STATIC_ASSERT(kSmiTag == 0);
4738 STATIC_ASSERT(kSmiShiftSize == 0);
4739 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4740 __ test(code_,
4741 Immediate(kSmiTagMask |
4742 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004743 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004744
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004745 Factory* factory = masm->isolate()->factory();
4746 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004747 STATIC_ASSERT(kSmiTag == 0);
4748 STATIC_ASSERT(kSmiTagSize == 1);
4749 STATIC_ASSERT(kSmiShiftSize == 0);
4750 // At this point code register contains smi tagged ascii char code.
4751 __ mov(result_, FieldOperand(result_,
4752 code_, times_half_pointer_size,
4753 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004754 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004755 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004756 __ bind(&exit_);
4757}
4758
4759
4760void StringCharFromCodeGenerator::GenerateSlow(
4761 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4762 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4763
4764 __ bind(&slow_case_);
4765 call_helper.BeforeCall(masm);
4766 __ push(code_);
4767 __ CallRuntime(Runtime::kCharFromCode, 1);
4768 if (!result_.is(eax)) {
4769 __ mov(result_, eax);
4770 }
4771 call_helper.AfterCall(masm);
4772 __ jmp(&exit_);
4773
4774 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4775}
4776
4777
4778// -------------------------------------------------------------------------
4779// StringCharAtGenerator
4780
4781void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4782 char_code_at_generator_.GenerateFast(masm);
4783 char_from_code_generator_.GenerateFast(masm);
4784}
4785
4786
4787void StringCharAtGenerator::GenerateSlow(
4788 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4789 char_code_at_generator_.GenerateSlow(masm, call_helper);
4790 char_from_code_generator_.GenerateSlow(masm, call_helper);
4791}
4792
4793
4794void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004795 Label string_add_runtime, call_builtin;
4796 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004797
4798 // Load the two arguments.
4799 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4800 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4801
4802 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004803 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004804 __ test(eax, Immediate(kSmiTagMask));
4805 __ j(zero, &string_add_runtime);
4806 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
4807 __ j(above_equal, &string_add_runtime);
4808
4809 // First argument is a a string, test second.
4810 __ test(edx, Immediate(kSmiTagMask));
4811 __ j(zero, &string_add_runtime);
4812 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
4813 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004814 } else {
4815 // Here at least one of the arguments is definitely a string.
4816 // We convert the one that is not known to be a string.
4817 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4818 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4819 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
4820 &call_builtin);
4821 builtin_id = Builtins::STRING_ADD_RIGHT;
4822 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4823 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4824 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
4825 &call_builtin);
4826 builtin_id = Builtins::STRING_ADD_LEFT;
4827 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004828 }
4829
4830 // Both arguments are strings.
4831 // eax: first string
4832 // edx: second string
4833 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004834 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004835 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
4836 STATIC_ASSERT(kSmiTag == 0);
4837 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004838 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004839 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004840 Counters* counters = masm->isolate()->counters();
4841 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004842 __ ret(2 * kPointerSize);
4843 __ bind(&second_not_zero_length);
4844 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
4845 STATIC_ASSERT(kSmiTag == 0);
4846 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004847 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004848 // First string is empty, result is second string which is in edx.
4849 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004850 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004851 __ ret(2 * kPointerSize);
4852
4853 // Both strings are non-empty.
4854 // eax: first string
4855 // ebx: length of first string as a smi
4856 // ecx: length of second string as a smi
4857 // edx: second string
4858 // Look at the length of the result of adding the two strings.
4859 Label string_add_flat_result, longer_than_two;
4860 __ bind(&both_not_zero_length);
4861 __ add(ebx, Operand(ecx));
4862 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
4863 // Handle exceptionally long strings in the runtime system.
4864 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004865 // Use the symbol table when adding two one character strings, as it
4866 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004867 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
4868 __ j(not_equal, &longer_than_two);
4869
4870 // Check that both strings are non-external ascii strings.
4871 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
4872 &string_add_runtime);
4873
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004874 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004875 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4876 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4877
4878 // Try to lookup two character string in symbol table. If it is not found
4879 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004880 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004881 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004882 masm, ebx, ecx, eax, edx, edi,
4883 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004884 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004885 __ ret(2 * kPointerSize);
4886
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004887 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004888 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004889 // Reload the arguments.
4890 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4891 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4892 // Get the two characters forming the new string.
4893 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4894 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4895 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004896 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004897 __ AllocateAsciiString(eax, // Result.
4898 2, // Length.
4899 edi, // Scratch 1.
4900 edx, // Scratch 2.
4901 &string_add_runtime);
4902 // Pack both characters in ebx.
4903 __ shl(ecx, kBitsPerByte);
4904 __ or_(ebx, Operand(ecx));
4905 // Set the characters in the new string.
4906 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004907 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004908 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004909
4910 __ bind(&longer_than_two);
4911 // Check if resulting string will be flat.
4912 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
4913 __ j(below, &string_add_flat_result);
4914
4915 // If result is not supposed to be flat allocate a cons string object. If both
4916 // strings are ascii the result is an ascii cons string.
4917 Label non_ascii, allocated, ascii_data;
4918 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
4919 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
4920 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
4921 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
4922 __ and_(ecx, Operand(edi));
4923 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4924 __ test(ecx, Immediate(kAsciiStringTag));
4925 __ j(zero, &non_ascii);
4926 __ bind(&ascii_data);
4927 // Allocate an acsii cons string.
4928 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
4929 __ bind(&allocated);
4930 // Fill the fields of the cons string.
4931 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
4932 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
4933 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
4934 Immediate(String::kEmptyHashField));
4935 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
4936 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
4937 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004938 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004939 __ ret(2 * kPointerSize);
4940 __ bind(&non_ascii);
4941 // At least one of the strings is two-byte. Check whether it happens
4942 // to contain only ascii characters.
4943 // ecx: first instance type AND second instance type.
4944 // edi: second instance type.
4945 __ test(ecx, Immediate(kAsciiDataHintMask));
4946 __ j(not_zero, &ascii_data);
4947 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4948 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4949 __ xor_(edi, Operand(ecx));
4950 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4951 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
4952 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
4953 __ j(equal, &ascii_data);
4954 // Allocate a two byte cons string.
4955 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
4956 __ jmp(&allocated);
4957
4958 // Handle creating a flat result. First check that both strings are not
4959 // external strings.
4960 // eax: first string
4961 // ebx: length of resulting flat string as a smi
4962 // edx: second string
4963 __ bind(&string_add_flat_result);
4964 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4965 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4966 __ and_(ecx, kStringRepresentationMask);
4967 __ cmp(ecx, kExternalStringTag);
4968 __ j(equal, &string_add_runtime);
4969 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4970 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4971 __ and_(ecx, kStringRepresentationMask);
4972 __ cmp(ecx, kExternalStringTag);
4973 __ j(equal, &string_add_runtime);
4974 // Now check if both strings are ascii strings.
4975 // eax: first string
4976 // ebx: length of resulting flat string as a smi
4977 // edx: second string
4978 Label non_ascii_string_add_flat_result;
4979 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4980 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4981 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
4982 __ j(zero, &non_ascii_string_add_flat_result);
4983 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4984 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
4985 __ j(zero, &string_add_runtime);
4986
ricow@chromium.org65fae842010-08-25 15:26:24 +00004987 // Both strings are ascii strings. As they are short they are both flat.
4988 // ebx: length of resulting flat string as a smi
4989 __ SmiUntag(ebx);
4990 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
4991 // eax: result string
4992 __ mov(ecx, eax);
4993 // Locate first character of result.
4994 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4995 // Load first argument and locate first character.
4996 __ mov(edx, Operand(esp, 2 * kPointerSize));
4997 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4998 __ SmiUntag(edi);
4999 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5000 // eax: result string
5001 // ecx: first character of result
5002 // edx: first char of first argument
5003 // edi: length of first argument
5004 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5005 // Load second argument and locate first character.
5006 __ mov(edx, Operand(esp, 1 * kPointerSize));
5007 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5008 __ SmiUntag(edi);
5009 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5010 // eax: result string
5011 // ecx: next character of result
5012 // edx: first char of second argument
5013 // edi: length of second argument
5014 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005015 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005016 __ ret(2 * kPointerSize);
5017
5018 // Handle creating a flat two byte result.
5019 // eax: first string - known to be two byte
5020 // ebx: length of resulting flat string as a smi
5021 // edx: second string
5022 __ bind(&non_ascii_string_add_flat_result);
5023 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5024 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5025 __ j(not_zero, &string_add_runtime);
5026 // Both strings are two byte strings. As they are short they are both
5027 // flat.
5028 __ SmiUntag(ebx);
5029 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5030 // eax: result string
5031 __ mov(ecx, eax);
5032 // Locate first character of result.
5033 __ add(Operand(ecx),
5034 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5035 // Load first argument and locate first character.
5036 __ mov(edx, Operand(esp, 2 * kPointerSize));
5037 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5038 __ SmiUntag(edi);
5039 __ add(Operand(edx),
5040 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5041 // eax: result string
5042 // ecx: first character of result
5043 // edx: first char of first argument
5044 // edi: length of first argument
5045 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5046 // Load second argument and locate first character.
5047 __ mov(edx, Operand(esp, 1 * kPointerSize));
5048 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5049 __ SmiUntag(edi);
5050 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5051 // eax: result string
5052 // ecx: next character of result
5053 // edx: first char of second argument
5054 // edi: length of second argument
5055 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005056 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005057 __ ret(2 * kPointerSize);
5058
5059 // Just jump to runtime to add the two strings.
5060 __ bind(&string_add_runtime);
5061 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005062
5063 if (call_builtin.is_linked()) {
5064 __ bind(&call_builtin);
5065 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5066 }
5067}
5068
5069
5070void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5071 int stack_offset,
5072 Register arg,
5073 Register scratch1,
5074 Register scratch2,
5075 Register scratch3,
5076 Label* slow) {
5077 // First check if the argument is already a string.
5078 Label not_string, done;
5079 __ test(arg, Immediate(kSmiTagMask));
5080 __ j(zero, &not_string);
5081 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5082 __ j(below, &done);
5083
5084 // Check the number to string cache.
5085 Label not_cached;
5086 __ bind(&not_string);
5087 // Puts the cached result into scratch1.
5088 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5089 arg,
5090 scratch1,
5091 scratch2,
5092 scratch3,
5093 false,
5094 &not_cached);
5095 __ mov(arg, scratch1);
5096 __ mov(Operand(esp, stack_offset), arg);
5097 __ jmp(&done);
5098
5099 // Check if the argument is a safe string wrapper.
5100 __ bind(&not_cached);
5101 __ test(arg, Immediate(kSmiTagMask));
5102 __ j(zero, slow);
5103 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5104 __ j(not_equal, slow);
5105 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5106 1 << Map::kStringWrapperSafeForDefaultValueOf);
5107 __ j(zero, slow);
5108 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5109 __ mov(Operand(esp, stack_offset), arg);
5110
5111 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005112}
5113
5114
5115void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5116 Register dest,
5117 Register src,
5118 Register count,
5119 Register scratch,
5120 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005121 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005122 __ bind(&loop);
5123 // This loop just copies one character at a time, as it is only used for very
5124 // short strings.
5125 if (ascii) {
5126 __ mov_b(scratch, Operand(src, 0));
5127 __ mov_b(Operand(dest, 0), scratch);
5128 __ add(Operand(src), Immediate(1));
5129 __ add(Operand(dest), Immediate(1));
5130 } else {
5131 __ mov_w(scratch, Operand(src, 0));
5132 __ mov_w(Operand(dest, 0), scratch);
5133 __ add(Operand(src), Immediate(2));
5134 __ add(Operand(dest), Immediate(2));
5135 }
5136 __ sub(Operand(count), Immediate(1));
5137 __ j(not_zero, &loop);
5138}
5139
5140
5141void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5142 Register dest,
5143 Register src,
5144 Register count,
5145 Register scratch,
5146 bool ascii) {
5147 // Copy characters using rep movs of doublewords.
5148 // The destination is aligned on a 4 byte boundary because we are
5149 // copying to the beginning of a newly allocated string.
5150 ASSERT(dest.is(edi)); // rep movs destination
5151 ASSERT(src.is(esi)); // rep movs source
5152 ASSERT(count.is(ecx)); // rep movs count
5153 ASSERT(!scratch.is(dest));
5154 ASSERT(!scratch.is(src));
5155 ASSERT(!scratch.is(count));
5156
5157 // Nothing to do for zero characters.
5158 Label done;
5159 __ test(count, Operand(count));
5160 __ j(zero, &done);
5161
5162 // Make count the number of bytes to copy.
5163 if (!ascii) {
5164 __ shl(count, 1);
5165 }
5166
5167 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005168 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005169 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005170 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005171
5172 // Copy from edi to esi using rep movs instruction.
5173 __ mov(scratch, count);
5174 __ sar(count, 2); // Number of doublewords to copy.
5175 __ cld();
5176 __ rep_movs();
5177
5178 // Find number of bytes left.
5179 __ mov(count, scratch);
5180 __ and_(count, 3);
5181
5182 // Check if there are more bytes to copy.
5183 __ bind(&last_bytes);
5184 __ test(count, Operand(count));
5185 __ j(zero, &done);
5186
5187 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005188 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005189 __ bind(&loop);
5190 __ mov_b(scratch, Operand(src, 0));
5191 __ mov_b(Operand(dest, 0), scratch);
5192 __ add(Operand(src), Immediate(1));
5193 __ add(Operand(dest), Immediate(1));
5194 __ sub(Operand(count), Immediate(1));
5195 __ j(not_zero, &loop);
5196
5197 __ bind(&done);
5198}
5199
5200
5201void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5202 Register c1,
5203 Register c2,
5204 Register scratch1,
5205 Register scratch2,
5206 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005207 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005208 Label* not_found) {
5209 // Register scratch3 is the general scratch register in this function.
5210 Register scratch = scratch3;
5211
5212 // Make sure that both characters are not digits as such strings has a
5213 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005214 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005215 __ mov(scratch, c1);
5216 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5217 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005218 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005219 __ mov(scratch, c2);
5220 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5221 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005222 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005223
5224 __ bind(&not_array_index);
5225 // Calculate the two character string hash.
5226 Register hash = scratch1;
5227 GenerateHashInit(masm, hash, c1, scratch);
5228 GenerateHashAddCharacter(masm, hash, c2, scratch);
5229 GenerateHashGetHash(masm, hash, scratch);
5230
5231 // Collect the two characters in a register.
5232 Register chars = c1;
5233 __ shl(c2, kBitsPerByte);
5234 __ or_(chars, Operand(c2));
5235
5236 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5237 // hash: hash of two character string.
5238
5239 // Load the symbol table.
5240 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005241 ExternalReference roots_address =
5242 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005243 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5244 __ mov(symbol_table,
5245 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5246
5247 // Calculate capacity mask from the symbol table capacity.
5248 Register mask = scratch2;
5249 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5250 __ SmiUntag(mask);
5251 __ sub(Operand(mask), Immediate(1));
5252
5253 // Registers
5254 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5255 // hash: hash of two character string
5256 // symbol_table: symbol table
5257 // mask: capacity mask
5258 // scratch: -
5259
5260 // Perform a number of probes in the symbol table.
5261 static const int kProbes = 4;
5262 Label found_in_symbol_table;
5263 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5264 for (int i = 0; i < kProbes; i++) {
5265 // Calculate entry in symbol table.
5266 __ mov(scratch, hash);
5267 if (i > 0) {
5268 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5269 }
5270 __ and_(scratch, Operand(mask));
5271
5272 // Load the entry from the symbol table.
5273 Register candidate = scratch; // Scratch register contains candidate.
5274 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5275 __ mov(candidate,
5276 FieldOperand(symbol_table,
5277 scratch,
5278 times_pointer_size,
5279 SymbolTable::kElementsStartOffset));
5280
5281 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005282 Factory* factory = masm->isolate()->factory();
5283 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005284 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005285 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005286 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005287
5288 // If length is not 2 the string is not a candidate.
5289 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5290 Immediate(Smi::FromInt(2)));
5291 __ j(not_equal, &next_probe[i]);
5292
5293 // As we are out of registers save the mask on the stack and use that
5294 // register as a temporary.
5295 __ push(mask);
5296 Register temp = mask;
5297
5298 // Check that the candidate is a non-external ascii string.
5299 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5300 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5301 __ JumpIfInstanceTypeIsNotSequentialAscii(
5302 temp, temp, &next_probe_pop_mask[i]);
5303
5304 // Check if the two characters match.
5305 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5306 __ and_(temp, 0x0000ffff);
5307 __ cmp(chars, Operand(temp));
5308 __ j(equal, &found_in_symbol_table);
5309 __ bind(&next_probe_pop_mask[i]);
5310 __ pop(mask);
5311 __ bind(&next_probe[i]);
5312 }
5313
5314 // No matching 2 character string found by probing.
5315 __ jmp(not_found);
5316
5317 // Scratch register contains result when we fall through to here.
5318 Register result = scratch;
5319 __ bind(&found_in_symbol_table);
5320 __ pop(mask); // Pop saved mask from the stack.
5321 if (!result.is(eax)) {
5322 __ mov(eax, result);
5323 }
5324}
5325
5326
5327void StringHelper::GenerateHashInit(MacroAssembler* masm,
5328 Register hash,
5329 Register character,
5330 Register scratch) {
5331 // hash = character + (character << 10);
5332 __ mov(hash, character);
5333 __ shl(hash, 10);
5334 __ add(hash, Operand(character));
5335 // hash ^= hash >> 6;
5336 __ mov(scratch, hash);
5337 __ sar(scratch, 6);
5338 __ xor_(hash, Operand(scratch));
5339}
5340
5341
5342void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5343 Register hash,
5344 Register character,
5345 Register scratch) {
5346 // hash += character;
5347 __ add(hash, Operand(character));
5348 // hash += hash << 10;
5349 __ mov(scratch, hash);
5350 __ shl(scratch, 10);
5351 __ add(hash, Operand(scratch));
5352 // hash ^= hash >> 6;
5353 __ mov(scratch, hash);
5354 __ sar(scratch, 6);
5355 __ xor_(hash, Operand(scratch));
5356}
5357
5358
5359void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5360 Register hash,
5361 Register scratch) {
5362 // hash += hash << 3;
5363 __ mov(scratch, hash);
5364 __ shl(scratch, 3);
5365 __ add(hash, Operand(scratch));
5366 // hash ^= hash >> 11;
5367 __ mov(scratch, hash);
5368 __ sar(scratch, 11);
5369 __ xor_(hash, Operand(scratch));
5370 // hash += hash << 15;
5371 __ mov(scratch, hash);
5372 __ shl(scratch, 15);
5373 __ add(hash, Operand(scratch));
5374
5375 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005376 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005377 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005378 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005379 __ mov(hash, Immediate(27));
5380 __ bind(&hash_not_zero);
5381}
5382
5383
5384void SubStringStub::Generate(MacroAssembler* masm) {
5385 Label runtime;
5386
5387 // Stack frame on entry.
5388 // esp[0]: return address
5389 // esp[4]: to
5390 // esp[8]: from
5391 // esp[12]: string
5392
5393 // Make sure first argument is a string.
5394 __ mov(eax, Operand(esp, 3 * kPointerSize));
5395 STATIC_ASSERT(kSmiTag == 0);
5396 __ test(eax, Immediate(kSmiTagMask));
5397 __ j(zero, &runtime);
5398 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5399 __ j(NegateCondition(is_string), &runtime);
5400
5401 // eax: string
5402 // ebx: instance type
5403
5404 // Calculate length of sub string using the smi values.
5405 Label result_longer_than_two;
5406 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
5407 __ test(ecx, Immediate(kSmiTagMask));
5408 __ j(not_zero, &runtime);
5409 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
5410 __ test(edx, Immediate(kSmiTagMask));
5411 __ j(not_zero, &runtime);
5412 __ sub(ecx, Operand(edx));
5413 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5414 Label return_eax;
5415 __ j(equal, &return_eax);
5416 // Special handling of sub-strings of length 1 and 2. One character strings
5417 // are handled in the runtime system (looked up in the single character
5418 // cache). Two character strings are looked for in the symbol cache.
5419 __ SmiUntag(ecx); // Result length is no longer smi.
5420 __ cmp(ecx, 2);
5421 __ j(greater, &result_longer_than_two);
5422 __ j(less, &runtime);
5423
5424 // Sub string of length 2 requested.
5425 // eax: string
5426 // ebx: instance type
5427 // ecx: sub string length (value is 2)
5428 // edx: from index (smi)
5429 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5430
5431 // Get the two characters forming the sub string.
5432 __ SmiUntag(edx); // From index is no longer smi.
5433 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5434 __ movzx_b(ecx,
5435 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5436
5437 // Try to lookup two character string in symbol table.
5438 Label make_two_character_string;
5439 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005440 masm, ebx, ecx, eax, edx, edi,
5441 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005442 __ ret(3 * kPointerSize);
5443
5444 __ bind(&make_two_character_string);
5445 // Setup registers for allocating the two character string.
5446 __ mov(eax, Operand(esp, 3 * kPointerSize));
5447 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5448 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5449 __ Set(ecx, Immediate(2));
5450
5451 __ bind(&result_longer_than_two);
5452 // eax: string
5453 // ebx: instance type
5454 // ecx: result string length
5455 // Check for flat ascii string
5456 Label non_ascii_flat;
5457 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5458
5459 // Allocate the result.
5460 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5461
5462 // eax: result string
5463 // ecx: result string length
5464 __ mov(edx, esi); // esi used by following code.
5465 // Locate first character of result.
5466 __ mov(edi, eax);
5467 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5468 // Load string argument and locate character of sub string start.
5469 __ mov(esi, Operand(esp, 3 * kPointerSize));
5470 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5471 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5472 __ SmiUntag(ebx);
5473 __ add(esi, Operand(ebx));
5474
5475 // eax: result string
5476 // ecx: result length
5477 // edx: original value of esi
5478 // edi: first character of result
5479 // esi: character of sub string start
5480 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5481 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005482 Counters* counters = masm->isolate()->counters();
5483 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005484 __ ret(3 * kPointerSize);
5485
5486 __ bind(&non_ascii_flat);
5487 // eax: string
5488 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5489 // ecx: result string length
5490 // Check for flat two byte string
5491 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5492 __ j(not_equal, &runtime);
5493
5494 // Allocate the result.
5495 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5496
5497 // eax: result string
5498 // ecx: result string length
5499 __ mov(edx, esi); // esi used by following code.
5500 // Locate first character of result.
5501 __ mov(edi, eax);
5502 __ add(Operand(edi),
5503 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5504 // Load string argument and locate character of sub string start.
5505 __ mov(esi, Operand(esp, 3 * kPointerSize));
5506 __ add(Operand(esi),
5507 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5508 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5509 // As from is a smi it is 2 times the value which matches the size of a two
5510 // byte character.
5511 STATIC_ASSERT(kSmiTag == 0);
5512 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5513 __ add(esi, Operand(ebx));
5514
5515 // eax: result string
5516 // ecx: result length
5517 // edx: original value of esi
5518 // edi: first character of result
5519 // esi: character of sub string start
5520 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5521 __ mov(esi, edx); // Restore esi.
5522
5523 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005524 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005525 __ ret(3 * kPointerSize);
5526
5527 // Just jump to runtime to create the sub string.
5528 __ bind(&runtime);
5529 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5530}
5531
5532
lrn@chromium.org1c092762011-05-09 09:42:16 +00005533void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5534 Register left,
5535 Register right,
5536 Register scratch1,
5537 Register scratch2) {
5538 Register length = scratch1;
5539
5540 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005541 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005542 __ mov(length, FieldOperand(left, String::kLengthOffset));
5543 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005544 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005545 __ bind(&strings_not_equal);
5546 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5547 __ ret(0);
5548
5549 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005550 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005551 __ bind(&check_zero_length);
5552 STATIC_ASSERT(kSmiTag == 0);
5553 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005554 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005555 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5556 __ ret(0);
5557
5558 // Compare characters.
5559 __ bind(&compare_chars);
5560 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005561 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005562
5563 // Characters are equal.
5564 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5565 __ ret(0);
5566}
5567
5568
ricow@chromium.org65fae842010-08-25 15:26:24 +00005569void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5570 Register left,
5571 Register right,
5572 Register scratch1,
5573 Register scratch2,
5574 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005575 Counters* counters = masm->isolate()->counters();
5576 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005577
5578 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005579 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005580 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5581 __ mov(scratch3, scratch1);
5582 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5583
5584 Register length_delta = scratch3;
5585
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005586 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005587 // Right string is shorter. Change scratch1 to be length of right string.
5588 __ sub(scratch1, Operand(length_delta));
5589 __ bind(&left_shorter);
5590
5591 Register min_length = scratch1;
5592
5593 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005594 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005595 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005596 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005597
lrn@chromium.org1c092762011-05-09 09:42:16 +00005598 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005599 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005600 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005601 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005602
5603 // Compare lengths - strings up to min-length are equal.
5604 __ bind(&compare_lengths);
5605 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005606 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005607
5608 // Result is EQUAL.
5609 STATIC_ASSERT(EQUAL == 0);
5610 STATIC_ASSERT(kSmiTag == 0);
5611 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5612 __ ret(0);
5613
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005614 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005615 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005616 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005617
5618 // Result is LESS.
5619 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5620 __ ret(0);
5621
5622 // Result is GREATER.
5623 __ bind(&result_greater);
5624 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5625 __ ret(0);
5626}
5627
5628
lrn@chromium.org1c092762011-05-09 09:42:16 +00005629void StringCompareStub::GenerateAsciiCharsCompareLoop(
5630 MacroAssembler* masm,
5631 Register left,
5632 Register right,
5633 Register length,
5634 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005635 Label* chars_not_equal,
5636 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005637 // Change index to run from -length to -1 by adding length to string
5638 // start. This means that loop ends when index reaches zero, which
5639 // doesn't need an additional compare.
5640 __ SmiUntag(length);
5641 __ lea(left,
5642 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5643 __ lea(right,
5644 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5645 __ neg(length);
5646 Register index = length; // index = -length;
5647
5648 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005649 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005650 __ bind(&loop);
5651 __ mov_b(scratch, Operand(left, index, times_1, 0));
5652 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005653 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005654 __ add(Operand(index), Immediate(1));
5655 __ j(not_zero, &loop);
5656}
5657
5658
ricow@chromium.org65fae842010-08-25 15:26:24 +00005659void StringCompareStub::Generate(MacroAssembler* masm) {
5660 Label runtime;
5661
5662 // Stack frame on entry.
5663 // esp[0]: return address
5664 // esp[4]: right string
5665 // esp[8]: left string
5666
5667 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5668 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5669
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005670 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005671 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005672 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005673 STATIC_ASSERT(EQUAL == 0);
5674 STATIC_ASSERT(kSmiTag == 0);
5675 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005676 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005677 __ ret(2 * kPointerSize);
5678
5679 __ bind(&not_same);
5680
5681 // Check that both objects are sequential ascii strings.
5682 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5683
5684 // Compare flat ascii strings.
5685 // Drop arguments from the stack.
5686 __ pop(ecx);
5687 __ add(Operand(esp), Immediate(2 * kPointerSize));
5688 __ push(ecx);
5689 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5690
5691 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5692 // tagged as a small integer.
5693 __ bind(&runtime);
5694 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5695}
5696
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005697
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005698void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5699 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005700 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005701 __ mov(ecx, Operand(edx));
5702 __ or_(ecx, Operand(eax));
5703 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005704 __ j(not_zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005705
5706 if (GetCondition() == equal) {
5707 // For equality we do not care about the sign of the result.
5708 __ sub(eax, Operand(edx));
5709 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005710 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005711 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005712 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005713 // Correct sign of result in case of overflow.
5714 __ not_(edx);
5715 __ bind(&done);
5716 __ mov(eax, edx);
5717 }
5718 __ ret(0);
5719
5720 __ bind(&miss);
5721 GenerateMiss(masm);
5722}
5723
5724
5725void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5726 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5727
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005728 Label generic_stub;
5729 Label unordered;
5730 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005731 __ mov(ecx, Operand(edx));
5732 __ and_(ecx, Operand(eax));
5733 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005734 __ j(zero, &generic_stub, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005735
5736 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005737 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005738 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005739 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005740
5741 // Inlining the double comparison and falling back to the general compare
5742 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005743 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005744 CpuFeatures::Scope scope1(SSE2);
5745 CpuFeatures::Scope scope2(CMOV);
5746
5747 // Load left and right operand
5748 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5749 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5750
5751 // Compare operands
5752 __ ucomisd(xmm0, xmm1);
5753
5754 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005755 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005756
5757 // Return a result of -1, 0, or 1, based on EFLAGS.
5758 // Performing mov, because xor would destroy the flag register.
5759 __ mov(eax, 0); // equal
5760 __ mov(ecx, Immediate(Smi::FromInt(1)));
5761 __ cmov(above, eax, Operand(ecx));
5762 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5763 __ cmov(below, eax, Operand(ecx));
5764 __ ret(0);
5765
5766 __ bind(&unordered);
5767 }
5768
5769 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5770 __ bind(&generic_stub);
5771 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5772
5773 __ bind(&miss);
5774 GenerateMiss(masm);
5775}
5776
5777
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005778void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5779 ASSERT(state_ == CompareIC::SYMBOLS);
5780 ASSERT(GetCondition() == equal);
5781
5782 // Registers containing left and right operands respectively.
5783 Register left = edx;
5784 Register right = eax;
5785 Register tmp1 = ecx;
5786 Register tmp2 = ebx;
5787
5788 // Check that both operands are heap objects.
5789 Label miss;
5790 __ mov(tmp1, Operand(left));
5791 STATIC_ASSERT(kSmiTag == 0);
5792 __ and_(tmp1, Operand(right));
5793 __ test(tmp1, Immediate(kSmiTagMask));
5794 __ j(zero, &miss, Label::kNear);
5795
5796 // Check that both operands are symbols.
5797 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5798 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5799 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5800 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5801 STATIC_ASSERT(kSymbolTag != 0);
5802 __ and_(tmp1, Operand(tmp2));
5803 __ test(tmp1, Immediate(kIsSymbolMask));
5804 __ j(zero, &miss, Label::kNear);
5805
5806 // Symbols are compared by identity.
5807 Label done;
5808 __ cmp(left, Operand(right));
5809 // Make sure eax is non-zero. At this point input operands are
5810 // guaranteed to be non-zero.
5811 ASSERT(right.is(eax));
5812 __ j(not_equal, &done, Label::kNear);
5813 STATIC_ASSERT(EQUAL == 0);
5814 STATIC_ASSERT(kSmiTag == 0);
5815 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5816 __ bind(&done);
5817 __ ret(0);
5818
5819 __ bind(&miss);
5820 GenerateMiss(masm);
5821}
5822
5823
lrn@chromium.org1c092762011-05-09 09:42:16 +00005824void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5825 ASSERT(state_ == CompareIC::STRINGS);
5826 ASSERT(GetCondition() == equal);
5827 Label miss;
5828
5829 // Registers containing left and right operands respectively.
5830 Register left = edx;
5831 Register right = eax;
5832 Register tmp1 = ecx;
5833 Register tmp2 = ebx;
5834 Register tmp3 = edi;
5835
5836 // Check that both operands are heap objects.
5837 __ mov(tmp1, Operand(left));
5838 STATIC_ASSERT(kSmiTag == 0);
5839 __ and_(tmp1, Operand(right));
5840 __ test(tmp1, Immediate(kSmiTagMask));
5841 __ j(zero, &miss);
5842
5843 // Check that both operands are strings. This leaves the instance
5844 // types loaded in tmp1 and tmp2.
5845 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5846 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5847 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5848 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5849 __ mov(tmp3, tmp1);
5850 STATIC_ASSERT(kNotStringTag != 0);
5851 __ or_(tmp3, Operand(tmp2));
5852 __ test(tmp3, Immediate(kIsNotStringMask));
5853 __ j(not_zero, &miss);
5854
5855 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005856 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005857 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005858 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005859 STATIC_ASSERT(EQUAL == 0);
5860 STATIC_ASSERT(kSmiTag == 0);
5861 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5862 __ ret(0);
5863
5864 // Handle not identical strings.
5865 __ bind(&not_same);
5866
5867 // Check that both strings are symbols. If they are, we're done
5868 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005869 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005870 STATIC_ASSERT(kSymbolTag != 0);
5871 __ and_(tmp1, Operand(tmp2));
5872 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005873 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005874 // Make sure eax is non-zero. At this point input operands are
5875 // guaranteed to be non-zero.
5876 ASSERT(right.is(eax));
5877 __ ret(0);
5878
5879 // Check that both strings are sequential ASCII.
5880 Label runtime;
5881 __ bind(&do_compare);
5882 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5883
5884 // Compare flat ASCII strings. Returns when done.
5885 StringCompareStub::GenerateFlatAsciiStringEquals(
5886 masm, left, right, tmp1, tmp2);
5887
5888 // Handle more complex cases in runtime.
5889 __ bind(&runtime);
5890 __ pop(tmp1); // Return address.
5891 __ push(left);
5892 __ push(right);
5893 __ push(tmp1);
5894 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5895
5896 __ bind(&miss);
5897 GenerateMiss(masm);
5898}
5899
5900
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005901void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5902 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005903 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005904 __ mov(ecx, Operand(edx));
5905 __ and_(ecx, Operand(eax));
5906 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005907 __ j(zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005908
5909 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005910 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005911 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005912 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005913
5914 ASSERT(GetCondition() == equal);
5915 __ sub(eax, Operand(edx));
5916 __ ret(0);
5917
5918 __ bind(&miss);
5919 GenerateMiss(masm);
5920}
5921
5922
5923void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
5924 // Save the registers.
5925 __ pop(ecx);
5926 __ push(edx);
5927 __ push(eax);
5928 __ push(ecx);
5929
5930 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005931 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
5932 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005933 __ EnterInternalFrame();
5934 __ push(edx);
5935 __ push(eax);
5936 __ push(Immediate(Smi::FromInt(op_)));
5937 __ CallExternalReference(miss, 3);
5938 __ LeaveInternalFrame();
5939
5940 // Compute the entry point of the rewritten stub.
5941 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
5942
5943 // Restore registers.
5944 __ pop(ecx);
5945 __ pop(eax);
5946 __ pop(edx);
5947 __ push(ecx);
5948
5949 // Do a tail call to the rewritten stub.
5950 __ jmp(Operand(edi));
5951}
5952
5953
lrn@chromium.org1c092762011-05-09 09:42:16 +00005954// Helper function used to check that the dictionary doesn't contain
5955// the property. This function may return false negatives, so miss_label
5956// must always call a backup property check that is complete.
5957// This function is safe to call if the receiver has fast properties.
5958// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005959MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
5960 MacroAssembler* masm,
5961 Label* miss,
5962 Label* done,
5963 Register properties,
5964 String* name,
5965 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005966 ASSERT(name->IsSymbol());
5967
5968 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5969 // not equal to the name and kProbes-th slot is not used (its name is the
5970 // undefined value), it guarantees the hash table doesn't contain the
5971 // property. It's true even if some slots represent deleted properties
5972 // (their names are the null value).
5973 for (int i = 0; i < kInlinedProbes; i++) {
5974 // Compute the masked index: (hash + i + i * i) & mask.
5975 Register index = r0;
5976 // Capacity is smi 2^n.
5977 __ mov(index, FieldOperand(properties, kCapacityOffset));
5978 __ dec(index);
5979 __ and_(Operand(index),
5980 Immediate(Smi::FromInt(name->Hash() +
5981 StringDictionary::GetProbeOffset(i))));
5982
5983 // Scale the index by multiplying by the entry size.
5984 ASSERT(StringDictionary::kEntrySize == 3);
5985 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
5986 Register entity_name = r0;
5987 // Having undefined at this place means the name is not contained.
5988 ASSERT_EQ(kSmiTagSize, 1);
5989 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
5990 kElementsStartOffset - kHeapObjectTag));
5991 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005992 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005993
5994 // Stop if found the property.
5995 __ cmp(entity_name, Handle<String>(name));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005996 __ j(equal, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005997
5998 // Check if the entry name is not a symbol.
5999 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6000 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6001 kIsSymbolMask);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006002 __ j(zero, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006003 }
6004
6005 StringDictionaryLookupStub stub(properties,
6006 r0,
6007 r0,
6008 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6009 __ push(Immediate(Handle<Object>(name)));
6010 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006011 MaybeObject* result = masm->TryCallStub(&stub);
6012 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006013 __ test(r0, Operand(r0));
6014 __ j(not_zero, miss);
6015 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006016 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006017}
6018
6019
6020// Probe the string dictionary in the |elements| register. Jump to the
6021// |done| label if a property with the given name is found leaving the
6022// index into the dictionary in |r0|. Jump to the |miss| label
6023// otherwise.
6024void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6025 Label* miss,
6026 Label* done,
6027 Register elements,
6028 Register name,
6029 Register r0,
6030 Register r1) {
6031 // Assert that name contains a string.
6032 if (FLAG_debug_code) __ AbortIfNotString(name);
6033
6034 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6035 __ shr(r1, kSmiTagSize); // convert smi to int
6036 __ dec(r1);
6037
6038 // Generate an unrolled loop that performs a few probes before
6039 // giving up. Measurements done on Gmail indicate that 2 probes
6040 // cover ~93% of loads from dictionaries.
6041 for (int i = 0; i < kInlinedProbes; i++) {
6042 // Compute the masked index: (hash + i + i * i) & mask.
6043 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6044 __ shr(r0, String::kHashShift);
6045 if (i > 0) {
6046 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6047 }
6048 __ and_(r0, Operand(r1));
6049
6050 // Scale the index by multiplying by the entry size.
6051 ASSERT(StringDictionary::kEntrySize == 3);
6052 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6053
6054 // Check if the key is identical to the name.
6055 __ cmp(name, Operand(elements,
6056 r0,
6057 times_4,
6058 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006059 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006060 }
6061
6062 StringDictionaryLookupStub stub(elements,
6063 r1,
6064 r0,
6065 POSITIVE_LOOKUP);
6066 __ push(name);
6067 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6068 __ shr(r0, String::kHashShift);
6069 __ push(r0);
6070 __ CallStub(&stub);
6071
6072 __ test(r1, Operand(r1));
6073 __ j(zero, miss);
6074 __ jmp(done);
6075}
6076
6077
6078void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6079 // Stack frame on entry:
6080 // esp[0 * kPointerSize]: return address.
6081 // esp[1 * kPointerSize]: key's hash.
6082 // esp[2 * kPointerSize]: key.
6083 // Registers:
6084 // dictionary_: StringDictionary to probe.
6085 // result_: used as scratch.
6086 // index_: will hold an index of entry if lookup is successful.
6087 // might alias with result_.
6088 // Returns:
6089 // result_ is zero if lookup failed, non zero otherwise.
6090
6091 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6092
6093 Register scratch = result_;
6094
6095 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6096 __ dec(scratch);
6097 __ SmiUntag(scratch);
6098 __ push(scratch);
6099
6100 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6101 // not equal to the name and kProbes-th slot is not used (its name is the
6102 // undefined value), it guarantees the hash table doesn't contain the
6103 // property. It's true even if some slots represent deleted properties
6104 // (their names are the null value).
6105 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6106 // Compute the masked index: (hash + i + i * i) & mask.
6107 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6108 if (i > 0) {
6109 __ add(Operand(scratch),
6110 Immediate(StringDictionary::GetProbeOffset(i)));
6111 }
6112 __ and_(scratch, Operand(esp, 0));
6113
6114 // Scale the index by multiplying by the entry size.
6115 ASSERT(StringDictionary::kEntrySize == 3);
6116 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6117
6118 // Having undefined at this place means the name is not contained.
6119 ASSERT_EQ(kSmiTagSize, 1);
6120 __ mov(scratch, Operand(dictionary_,
6121 index_,
6122 times_pointer_size,
6123 kElementsStartOffset - kHeapObjectTag));
6124 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6125 __ j(equal, &not_in_dictionary);
6126
6127 // Stop if found the property.
6128 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6129 __ j(equal, &in_dictionary);
6130
6131 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6132 // If we hit a non symbol key during negative lookup
6133 // we have to bailout as this key might be equal to the
6134 // key we are looking for.
6135
6136 // Check if the entry name is not a symbol.
6137 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6138 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6139 kIsSymbolMask);
6140 __ j(zero, &maybe_in_dictionary);
6141 }
6142 }
6143
6144 __ bind(&maybe_in_dictionary);
6145 // If we are doing negative lookup then probing failure should be
6146 // treated as a lookup success. For positive lookup probing failure
6147 // should be treated as lookup failure.
6148 if (mode_ == POSITIVE_LOOKUP) {
6149 __ mov(result_, Immediate(0));
6150 __ Drop(1);
6151 __ ret(2 * kPointerSize);
6152 }
6153
6154 __ bind(&in_dictionary);
6155 __ mov(result_, Immediate(1));
6156 __ Drop(1);
6157 __ ret(2 * kPointerSize);
6158
6159 __ bind(&not_in_dictionary);
6160 __ mov(result_, Immediate(0));
6161 __ Drop(1);
6162 __ ret(2 * kPointerSize);
6163}
6164
6165
ricow@chromium.org65fae842010-08-25 15:26:24 +00006166#undef __
6167
6168} } // namespace v8::internal
6169
6170#endif // V8_TARGET_ARCH_IA32