blob: 6786d5288f148e0032bbac213211715c81819ca1 [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;
749 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
750 __ jmp(&heapnumber_allocated);
751
752 __ bind(&slow_allocate_heapnumber);
753 __ EnterInternalFrame();
754 __ push(ecx);
755 __ CallRuntime(Runtime::kNumberAlloc, 0);
756 __ pop(ecx);
757 __ LeaveInternalFrame();
758
759 __ bind(&heapnumber_allocated);
760 }
761 if (CpuFeatures::IsSupported(SSE2)) {
762 CpuFeatures::Scope use_sse2(SSE2);
763 __ cvtsi2sd(xmm0, Operand(ecx));
764 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
765 } else {
766 __ push(ecx);
767 __ fild_s(Operand(esp, 0));
768 __ pop(ecx);
769 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
770 }
771 __ ret(0);
772}
773
774
775// TODO(svenpanne): Use virtual functions instead of switch.
776void TypeRecordingUnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
777 switch (op_) {
778 case Token::SUB:
779 GenerateGenericStubSub(masm);
780 break;
781 case Token::BIT_NOT:
782 GenerateGenericStubBitNot(masm);
783 break;
784 default:
785 UNREACHABLE();
786 }
787}
788
789
790void TypeRecordingUnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000791 Label non_smi, undo, slow;
792 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000793 __ bind(&non_smi);
794 GenerateHeapNumberCodeSub(masm, &slow);
795 __ bind(&undo);
796 GenerateSmiCodeUndo(masm);
797 __ bind(&slow);
798 GenerateGenericCodeFallback(masm);
799}
800
801
802void TypeRecordingUnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000803 Label non_smi, slow;
804 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000805 __ bind(&non_smi);
806 GenerateHeapNumberCodeBitNot(masm, &slow);
807 __ bind(&slow);
808 GenerateGenericCodeFallback(masm);
809}
810
811
812void TypeRecordingUnaryOpStub::GenerateGenericCodeFallback(
813 MacroAssembler* masm) {
814 // Handle the slow case by jumping to the corresponding JavaScript builtin.
815 __ pop(ecx); // pop return address.
816 __ push(eax);
817 __ push(ecx); // push return address
818 switch (op_) {
819 case Token::SUB:
820 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
821 break;
822 case Token::BIT_NOT:
823 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
824 break;
825 default:
826 UNREACHABLE();
827 }
828}
829
830
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000831Handle<Code> GetTypeRecordingBinaryOpStub(int key,
832 TRBinaryOpIC::TypeInfo type_info,
833 TRBinaryOpIC::TypeInfo result_type_info) {
834 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
835 return stub.GetCode();
836}
837
838
839void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
840 __ pop(ecx); // Save return address.
841 __ push(edx);
842 __ push(eax);
843 // Left and right arguments are now on top.
844 // Push this stub's key. Although the operation and the type info are
845 // encoded into the key, the encoding is opaque, so push them too.
846 __ push(Immediate(Smi::FromInt(MinorKey())));
847 __ push(Immediate(Smi::FromInt(op_)));
848 __ push(Immediate(Smi::FromInt(operands_type_)));
849
850 __ push(ecx); // Push return address.
851
852 // Patch the caller to an appropriate specialized stub and return the
853 // operation result to the caller of the stub.
854 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000855 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
856 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000857 5,
858 1);
859}
860
861
862// Prepare for a type transition runtime call when the args are already on
863// the stack, under the return address.
864void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
865 MacroAssembler* masm) {
866 __ pop(ecx); // Save return address.
867 // Left and right arguments are already on top of the stack.
868 // Push this stub's key. Although the operation and the type info are
869 // encoded into the key, the encoding is opaque, so push them too.
870 __ push(Immediate(Smi::FromInt(MinorKey())));
871 __ push(Immediate(Smi::FromInt(op_)));
872 __ push(Immediate(Smi::FromInt(operands_type_)));
873
874 __ push(ecx); // Push return address.
875
876 // Patch the caller to an appropriate specialized stub and return the
877 // operation result to the caller of the stub.
878 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000879 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
880 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000881 5,
882 1);
883}
884
885
886void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
887 switch (operands_type_) {
888 case TRBinaryOpIC::UNINITIALIZED:
889 GenerateTypeTransition(masm);
890 break;
891 case TRBinaryOpIC::SMI:
892 GenerateSmiStub(masm);
893 break;
894 case TRBinaryOpIC::INT32:
895 GenerateInt32Stub(masm);
896 break;
897 case TRBinaryOpIC::HEAP_NUMBER:
898 GenerateHeapNumberStub(masm);
899 break;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000900 case TRBinaryOpIC::ODDBALL:
901 GenerateOddballStub(masm);
902 break;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000903 case TRBinaryOpIC::BOTH_STRING:
904 GenerateBothStringStub(masm);
905 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000906 case TRBinaryOpIC::STRING:
907 GenerateStringStub(masm);
908 break;
909 case TRBinaryOpIC::GENERIC:
910 GenerateGeneric(masm);
911 break;
912 default:
913 UNREACHABLE();
914 }
915}
916
917
918const char* TypeRecordingBinaryOpStub::GetName() {
919 if (name_ != NULL) return name_;
920 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000921 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
922 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000923 if (name_ == NULL) return "OOM";
924 const char* op_name = Token::Name(op_);
925 const char* overwrite_name;
926 switch (mode_) {
927 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
928 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
929 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
930 default: overwrite_name = "UnknownOverwrite"; break;
931 }
932
933 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
934 "TypeRecordingBinaryOpStub_%s_%s_%s",
935 op_name,
936 overwrite_name,
937 TRBinaryOpIC::GetName(operands_type_));
938 return name_;
939}
940
941
942void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
943 Label* slow,
944 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
945 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
946 // dividend in eax and edx free for the division. Use eax, ebx for those.
947 Comment load_comment(masm, "-- Load arguments");
948 Register left = edx;
949 Register right = eax;
950 if (op_ == Token::DIV || op_ == Token::MOD) {
951 left = eax;
952 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000953 __ mov(ebx, eax);
954 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000955 }
956
957
958 // 2. Prepare the smi check of both operands by oring them together.
959 Comment smi_check_comment(masm, "-- Smi check arguments");
960 Label not_smis;
961 Register combined = ecx;
962 ASSERT(!left.is(combined) && !right.is(combined));
963 switch (op_) {
964 case Token::BIT_OR:
965 // Perform the operation into eax and smi check the result. Preserve
966 // eax in case the result is not a smi.
967 ASSERT(!left.is(ecx) && !right.is(ecx));
968 __ mov(ecx, right);
969 __ or_(right, Operand(left)); // Bitwise or is commutative.
970 combined = right;
971 break;
972
973 case Token::BIT_XOR:
974 case Token::BIT_AND:
975 case Token::ADD:
976 case Token::SUB:
977 case Token::MUL:
978 case Token::DIV:
979 case Token::MOD:
980 __ mov(combined, right);
981 __ or_(combined, Operand(left));
982 break;
983
984 case Token::SHL:
985 case Token::SAR:
986 case Token::SHR:
987 // Move the right operand into ecx for the shift operation, use eax
988 // for the smi check register.
989 ASSERT(!left.is(ecx) && !right.is(ecx));
990 __ mov(ecx, right);
991 __ or_(right, Operand(left));
992 combined = right;
993 break;
994
995 default:
996 break;
997 }
998
999 // 3. Perform the smi check of the operands.
1000 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1001 __ test(combined, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001002 __ j(not_zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001003
1004 // 4. Operands are both smis, perform the operation leaving the result in
1005 // eax and check the result if necessary.
1006 Comment perform_smi(masm, "-- Perform smi operation");
1007 Label use_fp_on_smis;
1008 switch (op_) {
1009 case Token::BIT_OR:
1010 // Nothing to do.
1011 break;
1012
1013 case Token::BIT_XOR:
1014 ASSERT(right.is(eax));
1015 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1016 break;
1017
1018 case Token::BIT_AND:
1019 ASSERT(right.is(eax));
1020 __ and_(right, Operand(left)); // Bitwise and is commutative.
1021 break;
1022
1023 case Token::SHL:
1024 // Remove tags from operands (but keep sign).
1025 __ SmiUntag(left);
1026 __ SmiUntag(ecx);
1027 // Perform the operation.
1028 __ shl_cl(left);
1029 // Check that the *signed* result fits in a smi.
1030 __ cmp(left, 0xc0000000);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001031 __ j(sign, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001032 // Tag the result and store it in register eax.
1033 __ SmiTag(left);
1034 __ mov(eax, left);
1035 break;
1036
1037 case Token::SAR:
1038 // Remove tags from operands (but keep sign).
1039 __ SmiUntag(left);
1040 __ SmiUntag(ecx);
1041 // Perform the operation.
1042 __ sar_cl(left);
1043 // Tag the result and store it in register eax.
1044 __ SmiTag(left);
1045 __ mov(eax, left);
1046 break;
1047
1048 case Token::SHR:
1049 // Remove tags from operands (but keep sign).
1050 __ SmiUntag(left);
1051 __ SmiUntag(ecx);
1052 // Perform the operation.
1053 __ shr_cl(left);
1054 // Check that the *unsigned* result fits in a smi.
1055 // Neither of the two high-order bits can be set:
1056 // - 0x80000000: high bit would be lost when smi tagging.
1057 // - 0x40000000: this number would convert to negative when
1058 // Smi tagging these two cases can only happen with shifts
1059 // by 0 or 1 when handed a valid smi.
1060 __ test(left, Immediate(0xc0000000));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001061 __ j(not_zero, slow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001062 // Tag the result and store it in register eax.
1063 __ SmiTag(left);
1064 __ mov(eax, left);
1065 break;
1066
1067 case Token::ADD:
1068 ASSERT(right.is(eax));
1069 __ add(right, Operand(left)); // Addition is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001070 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001071 break;
1072
1073 case Token::SUB:
1074 __ sub(left, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001075 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001076 __ mov(eax, left);
1077 break;
1078
1079 case Token::MUL:
1080 // If the smi tag is 0 we can just leave the tag on one operand.
1081 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1082 // We can't revert the multiplication if the result is not a smi
1083 // so save the right operand.
1084 __ mov(ebx, right);
1085 // Remove tag from one of the operands (but keep sign).
1086 __ SmiUntag(right);
1087 // Do multiplication.
1088 __ imul(right, Operand(left)); // Multiplication is commutative.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001089 __ j(overflow, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001090 // Check for negative zero result. Use combined = left | right.
1091 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1092 break;
1093
1094 case Token::DIV:
1095 // We can't revert the division if the result is not a smi so
1096 // save the left operand.
1097 __ mov(edi, left);
1098 // Check for 0 divisor.
1099 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001100 __ j(zero, &use_fp_on_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001101 // Sign extend left into edx:eax.
1102 ASSERT(left.is(eax));
1103 __ cdq();
1104 // Divide edx:eax by right.
1105 __ idiv(right);
1106 // Check for the corner case of dividing the most negative smi by
1107 // -1. We cannot use the overflow flag, since it is not set by idiv
1108 // instruction.
1109 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1110 __ cmp(eax, 0x40000000);
1111 __ j(equal, &use_fp_on_smis);
1112 // Check for negative zero result. Use combined = left | right.
1113 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1114 // Check that the remainder is zero.
1115 __ test(edx, Operand(edx));
1116 __ j(not_zero, &use_fp_on_smis);
1117 // Tag the result and store it in register eax.
1118 __ SmiTag(eax);
1119 break;
1120
1121 case Token::MOD:
1122 // Check for 0 divisor.
1123 __ test(right, Operand(right));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001124 __ j(zero, &not_smis);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001125
1126 // Sign extend left into edx:eax.
1127 ASSERT(left.is(eax));
1128 __ cdq();
1129 // Divide edx:eax by right.
1130 __ idiv(right);
1131 // Check for negative zero result. Use combined = left | right.
1132 __ NegativeZeroTest(edx, combined, slow);
1133 // Move remainder to register eax.
1134 __ mov(eax, edx);
1135 break;
1136
1137 default:
1138 UNREACHABLE();
1139 }
1140
1141 // 5. Emit return of result in eax. Some operations have registers pushed.
1142 switch (op_) {
1143 case Token::ADD:
1144 case Token::SUB:
1145 case Token::MUL:
1146 case Token::DIV:
1147 __ ret(0);
1148 break;
1149 case Token::MOD:
1150 case Token::BIT_OR:
1151 case Token::BIT_AND:
1152 case Token::BIT_XOR:
1153 case Token::SAR:
1154 case Token::SHL:
1155 case Token::SHR:
1156 __ ret(2 * kPointerSize);
1157 break;
1158 default:
1159 UNREACHABLE();
1160 }
1161
1162 // 6. For some operations emit inline code to perform floating point
1163 // operations on known smis (e.g., if the result of the operation
1164 // overflowed the smi range).
1165 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1166 __ bind(&use_fp_on_smis);
1167 switch (op_) {
1168 // Undo the effects of some operations, and some register moves.
1169 case Token::SHL:
1170 // The arguments are saved on the stack, and only used from there.
1171 break;
1172 case Token::ADD:
1173 // Revert right = right + left.
1174 __ sub(right, Operand(left));
1175 break;
1176 case Token::SUB:
1177 // Revert left = left - right.
1178 __ add(left, Operand(right));
1179 break;
1180 case Token::MUL:
1181 // Right was clobbered but a copy is in ebx.
1182 __ mov(right, ebx);
1183 break;
1184 case Token::DIV:
1185 // Left was clobbered but a copy is in edi. Right is in ebx for
1186 // division. They should be in eax, ebx for jump to not_smi.
1187 __ mov(eax, edi);
1188 break;
1189 default:
1190 // No other operators jump to use_fp_on_smis.
1191 break;
1192 }
1193 __ jmp(&not_smis);
1194 } else {
1195 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1196 switch (op_) {
1197 case Token::SHL: {
1198 Comment perform_float(masm, "-- Perform float operation on smis");
1199 __ bind(&use_fp_on_smis);
1200 // Result we want is in left == edx, so we can put the allocated heap
1201 // number in eax.
1202 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1203 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001204 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001205 CpuFeatures::Scope use_sse2(SSE2);
1206 __ cvtsi2sd(xmm0, Operand(left));
1207 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1208 } else {
1209 // It's OK to overwrite the right argument on the stack because we
1210 // are about to return.
1211 __ mov(Operand(esp, 1 * kPointerSize), left);
1212 __ fild_s(Operand(esp, 1 * kPointerSize));
1213 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1214 }
1215 __ ret(2 * kPointerSize);
1216 break;
1217 }
1218
1219 case Token::ADD:
1220 case Token::SUB:
1221 case Token::MUL:
1222 case Token::DIV: {
1223 Comment perform_float(masm, "-- Perform float operation on smis");
1224 __ bind(&use_fp_on_smis);
1225 // Restore arguments to edx, eax.
1226 switch (op_) {
1227 case Token::ADD:
1228 // Revert right = right + left.
1229 __ sub(right, Operand(left));
1230 break;
1231 case Token::SUB:
1232 // Revert left = left - right.
1233 __ add(left, Operand(right));
1234 break;
1235 case Token::MUL:
1236 // Right was clobbered but a copy is in ebx.
1237 __ mov(right, ebx);
1238 break;
1239 case Token::DIV:
1240 // Left was clobbered but a copy is in edi. Right is in ebx for
1241 // division.
1242 __ mov(edx, edi);
1243 __ mov(eax, right);
1244 break;
1245 default: UNREACHABLE();
1246 break;
1247 }
1248 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001249 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001250 CpuFeatures::Scope use_sse2(SSE2);
1251 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1252 switch (op_) {
1253 case Token::ADD: __ addsd(xmm0, xmm1); break;
1254 case Token::SUB: __ subsd(xmm0, xmm1); break;
1255 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1256 case Token::DIV: __ divsd(xmm0, xmm1); break;
1257 default: UNREACHABLE();
1258 }
1259 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1260 } else { // SSE2 not available, use FPU.
1261 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1262 switch (op_) {
1263 case Token::ADD: __ faddp(1); break;
1264 case Token::SUB: __ fsubp(1); break;
1265 case Token::MUL: __ fmulp(1); break;
1266 case Token::DIV: __ fdivp(1); break;
1267 default: UNREACHABLE();
1268 }
1269 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1270 }
1271 __ mov(eax, ecx);
1272 __ ret(0);
1273 break;
1274 }
1275
1276 default:
1277 break;
1278 }
1279 }
1280
1281 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1282 // edx and eax.
1283 Comment done_comment(masm, "-- Enter non-smi code");
1284 __ bind(&not_smis);
1285 switch (op_) {
1286 case Token::BIT_OR:
1287 case Token::SHL:
1288 case Token::SAR:
1289 case Token::SHR:
1290 // Right operand is saved in ecx and eax was destroyed by the smi
1291 // check.
1292 __ mov(eax, ecx);
1293 break;
1294
1295 case Token::DIV:
1296 case Token::MOD:
1297 // Operands are in eax, ebx at this point.
1298 __ mov(edx, eax);
1299 __ mov(eax, ebx);
1300 break;
1301
1302 default:
1303 break;
1304 }
1305}
1306
1307
1308void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1309 Label call_runtime;
1310
1311 switch (op_) {
1312 case Token::ADD:
1313 case Token::SUB:
1314 case Token::MUL:
1315 case Token::DIV:
1316 break;
1317 case Token::MOD:
1318 case Token::BIT_OR:
1319 case Token::BIT_AND:
1320 case Token::BIT_XOR:
1321 case Token::SAR:
1322 case Token::SHL:
1323 case Token::SHR:
1324 GenerateRegisterArgsPush(masm);
1325 break;
1326 default:
1327 UNREACHABLE();
1328 }
1329
1330 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1331 result_type_ == TRBinaryOpIC::SMI) {
1332 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1333 } else {
1334 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1335 }
1336 __ bind(&call_runtime);
1337 switch (op_) {
1338 case Token::ADD:
1339 case Token::SUB:
1340 case Token::MUL:
1341 case Token::DIV:
1342 GenerateTypeTransition(masm);
1343 break;
1344 case Token::MOD:
1345 case Token::BIT_OR:
1346 case Token::BIT_AND:
1347 case Token::BIT_XOR:
1348 case Token::SAR:
1349 case Token::SHL:
1350 case Token::SHR:
1351 GenerateTypeTransitionWithSavedArgs(masm);
1352 break;
1353 default:
1354 UNREACHABLE();
1355 }
1356}
1357
1358
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001359void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001360 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1361 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001362 // Try to add arguments as strings, otherwise, transition to the generic
1363 // TRBinaryOpIC type.
1364 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001365 GenerateTypeTransition(masm);
1366}
1367
1368
danno@chromium.org160a7b02011-04-18 15:51:38 +00001369void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1370 Label call_runtime;
1371 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
1372 ASSERT(op_ == Token::ADD);
1373 // If both arguments are strings, call the string add stub.
1374 // Otherwise, do a transition.
1375
1376 // Registers containing left and right operands respectively.
1377 Register left = edx;
1378 Register right = eax;
1379
1380 // Test if left operand is a string.
1381 __ test(left, Immediate(kSmiTagMask));
1382 __ j(zero, &call_runtime);
1383 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1384 __ j(above_equal, &call_runtime);
1385
1386 // Test if right operand is a string.
1387 __ test(right, Immediate(kSmiTagMask));
1388 __ j(zero, &call_runtime);
1389 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1390 __ j(above_equal, &call_runtime);
1391
1392 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1393 GenerateRegisterArgsPush(masm);
1394 __ TailCallStub(&string_add_stub);
1395
1396 __ bind(&call_runtime);
1397 GenerateTypeTransition(masm);
1398}
1399
1400
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001401void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1402 Label call_runtime;
1403 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1404
1405 // Floating point case.
1406 switch (op_) {
1407 case Token::ADD:
1408 case Token::SUB:
1409 case Token::MUL:
1410 case Token::DIV: {
1411 Label not_floats;
1412 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001413 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001414 CpuFeatures::Scope use_sse2(SSE2);
1415 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1416 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1417 switch (op_) {
1418 case Token::ADD: __ addsd(xmm0, xmm1); break;
1419 case Token::SUB: __ subsd(xmm0, xmm1); break;
1420 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1421 case Token::DIV: __ divsd(xmm0, xmm1); break;
1422 default: UNREACHABLE();
1423 }
1424 // Check result type if it is currently Int32.
1425 if (result_type_ <= TRBinaryOpIC::INT32) {
1426 __ cvttsd2si(ecx, Operand(xmm0));
1427 __ cvtsi2sd(xmm2, Operand(ecx));
1428 __ ucomisd(xmm0, xmm2);
1429 __ j(not_zero, &not_int32);
1430 __ j(carry, &not_int32);
1431 }
1432 GenerateHeapResultAllocation(masm, &call_runtime);
1433 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1434 __ ret(0);
1435 } else { // SSE2 not available, use FPU.
1436 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1437 FloatingPointHelper::LoadFloatOperands(
1438 masm,
1439 ecx,
1440 FloatingPointHelper::ARGS_IN_REGISTERS);
1441 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1442 switch (op_) {
1443 case Token::ADD: __ faddp(1); break;
1444 case Token::SUB: __ fsubp(1); break;
1445 case Token::MUL: __ fmulp(1); break;
1446 case Token::DIV: __ fdivp(1); break;
1447 default: UNREACHABLE();
1448 }
1449 Label after_alloc_failure;
1450 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1451 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1452 __ ret(0);
1453 __ bind(&after_alloc_failure);
1454 __ ffree();
1455 __ jmp(&call_runtime);
1456 }
1457
1458 __ bind(&not_floats);
1459 __ bind(&not_int32);
1460 GenerateTypeTransition(masm);
1461 break;
1462 }
1463
1464 case Token::MOD: {
1465 // For MOD we go directly to runtime in the non-smi case.
1466 break;
1467 }
1468 case Token::BIT_OR:
1469 case Token::BIT_AND:
1470 case Token::BIT_XOR:
1471 case Token::SAR:
1472 case Token::SHL:
1473 case Token::SHR: {
1474 GenerateRegisterArgsPush(masm);
1475 Label not_floats;
1476 Label not_int32;
1477 Label non_smi_result;
1478 /* {
1479 CpuFeatures::Scope use_sse2(SSE2);
1480 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1481 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1482 }*/
1483 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1484 use_sse3_,
1485 &not_floats);
1486 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1487 &not_int32);
1488 switch (op_) {
1489 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1490 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1491 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1492 case Token::SAR: __ sar_cl(eax); break;
1493 case Token::SHL: __ shl_cl(eax); break;
1494 case Token::SHR: __ shr_cl(eax); break;
1495 default: UNREACHABLE();
1496 }
1497 if (op_ == Token::SHR) {
1498 // Check if result is non-negative and fits in a smi.
1499 __ test(eax, Immediate(0xc0000000));
1500 __ j(not_zero, &call_runtime);
1501 } else {
1502 // Check if result fits in a smi.
1503 __ cmp(eax, 0xc0000000);
1504 __ j(negative, &non_smi_result);
1505 }
1506 // Tag smi result and return.
1507 __ SmiTag(eax);
1508 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1509
1510 // All ops except SHR return a signed int32 that we load in
1511 // a HeapNumber.
1512 if (op_ != Token::SHR) {
1513 __ bind(&non_smi_result);
1514 // Allocate a heap number if needed.
1515 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001516 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001517 switch (mode_) {
1518 case OVERWRITE_LEFT:
1519 case OVERWRITE_RIGHT:
1520 // If the operand was an object, we skip the
1521 // allocation of a heap number.
1522 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1523 1 * kPointerSize : 2 * kPointerSize));
1524 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001525 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001526 // Fall through!
1527 case NO_OVERWRITE:
1528 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1529 __ bind(&skip_allocation);
1530 break;
1531 default: UNREACHABLE();
1532 }
1533 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001534 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001535 CpuFeatures::Scope use_sse2(SSE2);
1536 __ cvtsi2sd(xmm0, Operand(ebx));
1537 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1538 } else {
1539 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1540 __ fild_s(Operand(esp, 1 * kPointerSize));
1541 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1542 }
1543 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1544 }
1545
1546 __ bind(&not_floats);
1547 __ bind(&not_int32);
1548 GenerateTypeTransitionWithSavedArgs(masm);
1549 break;
1550 }
1551 default: UNREACHABLE(); break;
1552 }
1553
1554 // If an allocation fails, or SHR or MOD hit a hard case,
1555 // use the runtime system to get the correct result.
1556 __ bind(&call_runtime);
1557
1558 switch (op_) {
1559 case Token::ADD:
1560 GenerateRegisterArgsPush(masm);
1561 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1562 break;
1563 case Token::SUB:
1564 GenerateRegisterArgsPush(masm);
1565 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1566 break;
1567 case Token::MUL:
1568 GenerateRegisterArgsPush(masm);
1569 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1570 break;
1571 case Token::DIV:
1572 GenerateRegisterArgsPush(masm);
1573 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1574 break;
1575 case Token::MOD:
1576 GenerateRegisterArgsPush(masm);
1577 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1578 break;
1579 case Token::BIT_OR:
1580 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1581 break;
1582 case Token::BIT_AND:
1583 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1584 break;
1585 case Token::BIT_XOR:
1586 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1587 break;
1588 case Token::SAR:
1589 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1590 break;
1591 case Token::SHL:
1592 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1593 break;
1594 case Token::SHR:
1595 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1596 break;
1597 default:
1598 UNREACHABLE();
1599 }
1600}
1601
1602
lrn@chromium.org7516f052011-03-30 08:52:27 +00001603void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001604 if (op_ == Token::ADD) {
1605 // Handle string addition here, because it is the only operation
1606 // that does not do a ToNumber conversion on the operands.
1607 GenerateAddStrings(masm);
1608 }
1609
danno@chromium.org160a7b02011-04-18 15:51:38 +00001610 Factory* factory = masm->isolate()->factory();
1611
lrn@chromium.org7516f052011-03-30 08:52:27 +00001612 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001613 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001614 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001615 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001616 if (Token::IsBitOp(op_)) {
1617 __ xor_(edx, Operand(edx));
1618 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001619 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001620 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001621 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001622 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001623 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001624 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001625 if (Token::IsBitOp(op_)) {
1626 __ xor_(eax, Operand(eax));
1627 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001628 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001629 }
1630 __ bind(&done);
1631
1632 GenerateHeapNumberStub(masm);
1633}
1634
1635
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001636void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1637 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001638
1639 // Floating point case.
1640 switch (op_) {
1641 case Token::ADD:
1642 case Token::SUB:
1643 case Token::MUL:
1644 case Token::DIV: {
1645 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001646 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001647 CpuFeatures::Scope use_sse2(SSE2);
1648 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1649
1650 switch (op_) {
1651 case Token::ADD: __ addsd(xmm0, xmm1); break;
1652 case Token::SUB: __ subsd(xmm0, xmm1); break;
1653 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1654 case Token::DIV: __ divsd(xmm0, xmm1); break;
1655 default: UNREACHABLE();
1656 }
1657 GenerateHeapResultAllocation(masm, &call_runtime);
1658 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1659 __ ret(0);
1660 } else { // SSE2 not available, use FPU.
1661 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1662 FloatingPointHelper::LoadFloatOperands(
1663 masm,
1664 ecx,
1665 FloatingPointHelper::ARGS_IN_REGISTERS);
1666 switch (op_) {
1667 case Token::ADD: __ faddp(1); break;
1668 case Token::SUB: __ fsubp(1); break;
1669 case Token::MUL: __ fmulp(1); break;
1670 case Token::DIV: __ fdivp(1); break;
1671 default: UNREACHABLE();
1672 }
1673 Label after_alloc_failure;
1674 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1675 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1676 __ ret(0);
1677 __ bind(&after_alloc_failure);
1678 __ ffree();
1679 __ jmp(&call_runtime);
1680 }
1681
1682 __ bind(&not_floats);
1683 GenerateTypeTransition(masm);
1684 break;
1685 }
1686
1687 case Token::MOD: {
1688 // For MOD we go directly to runtime in the non-smi case.
1689 break;
1690 }
1691 case Token::BIT_OR:
1692 case Token::BIT_AND:
1693 case Token::BIT_XOR:
1694 case Token::SAR:
1695 case Token::SHL:
1696 case Token::SHR: {
1697 GenerateRegisterArgsPush(masm);
1698 Label not_floats;
1699 Label non_smi_result;
1700 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1701 use_sse3_,
1702 &not_floats);
1703 switch (op_) {
1704 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1705 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1706 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1707 case Token::SAR: __ sar_cl(eax); break;
1708 case Token::SHL: __ shl_cl(eax); break;
1709 case Token::SHR: __ shr_cl(eax); break;
1710 default: UNREACHABLE();
1711 }
1712 if (op_ == Token::SHR) {
1713 // Check if result is non-negative and fits in a smi.
1714 __ test(eax, Immediate(0xc0000000));
1715 __ j(not_zero, &call_runtime);
1716 } else {
1717 // Check if result fits in a smi.
1718 __ cmp(eax, 0xc0000000);
1719 __ j(negative, &non_smi_result);
1720 }
1721 // Tag smi result and return.
1722 __ SmiTag(eax);
1723 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1724
1725 // All ops except SHR return a signed int32 that we load in
1726 // a HeapNumber.
1727 if (op_ != Token::SHR) {
1728 __ bind(&non_smi_result);
1729 // Allocate a heap number if needed.
1730 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001731 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001732 switch (mode_) {
1733 case OVERWRITE_LEFT:
1734 case OVERWRITE_RIGHT:
1735 // If the operand was an object, we skip the
1736 // allocation of a heap number.
1737 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1738 1 * kPointerSize : 2 * kPointerSize));
1739 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001740 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001741 // Fall through!
1742 case NO_OVERWRITE:
1743 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1744 __ bind(&skip_allocation);
1745 break;
1746 default: UNREACHABLE();
1747 }
1748 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001749 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001750 CpuFeatures::Scope use_sse2(SSE2);
1751 __ cvtsi2sd(xmm0, Operand(ebx));
1752 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1753 } else {
1754 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1755 __ fild_s(Operand(esp, 1 * kPointerSize));
1756 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1757 }
1758 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1759 }
1760
1761 __ bind(&not_floats);
1762 GenerateTypeTransitionWithSavedArgs(masm);
1763 break;
1764 }
1765 default: UNREACHABLE(); break;
1766 }
1767
1768 // If an allocation fails, or SHR or MOD hit a hard case,
1769 // use the runtime system to get the correct result.
1770 __ bind(&call_runtime);
1771
1772 switch (op_) {
1773 case Token::ADD:
1774 GenerateRegisterArgsPush(masm);
1775 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1776 break;
1777 case Token::SUB:
1778 GenerateRegisterArgsPush(masm);
1779 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1780 break;
1781 case Token::MUL:
1782 GenerateRegisterArgsPush(masm);
1783 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1784 break;
1785 case Token::DIV:
1786 GenerateRegisterArgsPush(masm);
1787 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1788 break;
1789 case Token::MOD:
1790 GenerateRegisterArgsPush(masm);
1791 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1792 break;
1793 case Token::BIT_OR:
1794 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1795 break;
1796 case Token::BIT_AND:
1797 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1798 break;
1799 case Token::BIT_XOR:
1800 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1801 break;
1802 case Token::SAR:
1803 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1804 break;
1805 case Token::SHL:
1806 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1807 break;
1808 case Token::SHR:
1809 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1810 break;
1811 default:
1812 UNREACHABLE();
1813 }
1814}
1815
1816
1817void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
1818 Label call_runtime;
1819
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001820 Counters* counters = masm->isolate()->counters();
1821 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001822
1823 switch (op_) {
1824 case Token::ADD:
1825 case Token::SUB:
1826 case Token::MUL:
1827 case Token::DIV:
1828 break;
1829 case Token::MOD:
1830 case Token::BIT_OR:
1831 case Token::BIT_AND:
1832 case Token::BIT_XOR:
1833 case Token::SAR:
1834 case Token::SHL:
1835 case Token::SHR:
1836 GenerateRegisterArgsPush(masm);
1837 break;
1838 default:
1839 UNREACHABLE();
1840 }
1841
1842 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1843
1844 // Floating point case.
1845 switch (op_) {
1846 case Token::ADD:
1847 case Token::SUB:
1848 case Token::MUL:
1849 case Token::DIV: {
1850 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001851 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001852 CpuFeatures::Scope use_sse2(SSE2);
1853 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1854
1855 switch (op_) {
1856 case Token::ADD: __ addsd(xmm0, xmm1); break;
1857 case Token::SUB: __ subsd(xmm0, xmm1); break;
1858 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1859 case Token::DIV: __ divsd(xmm0, xmm1); break;
1860 default: UNREACHABLE();
1861 }
1862 GenerateHeapResultAllocation(masm, &call_runtime);
1863 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1864 __ ret(0);
1865 } else { // SSE2 not available, use FPU.
1866 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1867 FloatingPointHelper::LoadFloatOperands(
1868 masm,
1869 ecx,
1870 FloatingPointHelper::ARGS_IN_REGISTERS);
1871 switch (op_) {
1872 case Token::ADD: __ faddp(1); break;
1873 case Token::SUB: __ fsubp(1); break;
1874 case Token::MUL: __ fmulp(1); break;
1875 case Token::DIV: __ fdivp(1); break;
1876 default: UNREACHABLE();
1877 }
1878 Label after_alloc_failure;
1879 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1880 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1881 __ ret(0);
1882 __ bind(&after_alloc_failure);
1883 __ ffree();
1884 __ jmp(&call_runtime);
1885 }
1886 __ bind(&not_floats);
1887 break;
1888 }
1889 case Token::MOD: {
1890 // For MOD we go directly to runtime in the non-smi case.
1891 break;
1892 }
1893 case Token::BIT_OR:
1894 case Token::BIT_AND:
1895 case Token::BIT_XOR:
1896 case Token::SAR:
1897 case Token::SHL:
1898 case Token::SHR: {
1899 Label non_smi_result;
1900 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1901 use_sse3_,
1902 &call_runtime);
1903 switch (op_) {
1904 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1905 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1906 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1907 case Token::SAR: __ sar_cl(eax); break;
1908 case Token::SHL: __ shl_cl(eax); break;
1909 case Token::SHR: __ shr_cl(eax); break;
1910 default: UNREACHABLE();
1911 }
1912 if (op_ == Token::SHR) {
1913 // Check if result is non-negative and fits in a smi.
1914 __ test(eax, Immediate(0xc0000000));
1915 __ j(not_zero, &call_runtime);
1916 } else {
1917 // Check if result fits in a smi.
1918 __ cmp(eax, 0xc0000000);
1919 __ j(negative, &non_smi_result);
1920 }
1921 // Tag smi result and return.
1922 __ SmiTag(eax);
1923 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1924
1925 // All ops except SHR return a signed int32 that we load in
1926 // a HeapNumber.
1927 if (op_ != Token::SHR) {
1928 __ bind(&non_smi_result);
1929 // Allocate a heap number if needed.
1930 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001931 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001932 switch (mode_) {
1933 case OVERWRITE_LEFT:
1934 case OVERWRITE_RIGHT:
1935 // If the operand was an object, we skip the
1936 // allocation of a heap number.
1937 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1938 1 * kPointerSize : 2 * kPointerSize));
1939 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001940 __ j(not_zero, &skip_allocation, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001941 // Fall through!
1942 case NO_OVERWRITE:
1943 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1944 __ bind(&skip_allocation);
1945 break;
1946 default: UNREACHABLE();
1947 }
1948 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001949 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001950 CpuFeatures::Scope use_sse2(SSE2);
1951 __ cvtsi2sd(xmm0, Operand(ebx));
1952 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1953 } else {
1954 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1955 __ fild_s(Operand(esp, 1 * kPointerSize));
1956 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1957 }
1958 __ ret(2 * kPointerSize);
1959 }
1960 break;
1961 }
1962 default: UNREACHABLE(); break;
1963 }
1964
1965 // If all else fails, use the runtime system to get the correct
1966 // result.
1967 __ bind(&call_runtime);
1968 switch (op_) {
1969 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001970 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001971 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001972 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1973 break;
1974 }
1975 case Token::SUB:
1976 GenerateRegisterArgsPush(masm);
1977 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1978 break;
1979 case Token::MUL:
1980 GenerateRegisterArgsPush(masm);
1981 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1982 break;
1983 case Token::DIV:
1984 GenerateRegisterArgsPush(masm);
1985 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1986 break;
1987 case Token::MOD:
1988 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1989 break;
1990 case Token::BIT_OR:
1991 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1992 break;
1993 case Token::BIT_AND:
1994 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1995 break;
1996 case Token::BIT_XOR:
1997 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1998 break;
1999 case Token::SAR:
2000 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2001 break;
2002 case Token::SHL:
2003 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2004 break;
2005 case Token::SHR:
2006 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2007 break;
2008 default:
2009 UNREACHABLE();
2010 }
2011}
2012
2013
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002014void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002015 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002016 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002017
2018 // Registers containing left and right operands respectively.
2019 Register left = edx;
2020 Register right = eax;
2021
2022 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002023 __ test(left, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002024 __ j(zero, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002025 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002026 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002027
2028 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2029 GenerateRegisterArgsPush(masm);
2030 __ TailCallStub(&string_add_left_stub);
2031
2032 // Left operand is not a string, test right.
2033 __ bind(&left_not_string);
2034 __ test(right, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002035 __ j(zero, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002036 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002037 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002038
2039 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2040 GenerateRegisterArgsPush(masm);
2041 __ TailCallStub(&string_add_right_stub);
2042
2043 // Neither argument is a string.
2044 __ bind(&call_runtime);
2045}
2046
2047
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002048void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2049 MacroAssembler* masm,
2050 Label* alloc_failure) {
2051 Label skip_allocation;
2052 OverwriteMode mode = mode_;
2053 switch (mode) {
2054 case OVERWRITE_LEFT: {
2055 // If the argument in edx is already an object, we skip the
2056 // allocation of a heap number.
2057 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002058 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002059 // Allocate a heap number for the result. Keep eax and edx intact
2060 // for the possible runtime call.
2061 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2062 // Now edx can be overwritten losing one of the arguments as we are
2063 // now done and will not need it any more.
2064 __ mov(edx, Operand(ebx));
2065 __ bind(&skip_allocation);
2066 // Use object in edx as a result holder
2067 __ mov(eax, Operand(edx));
2068 break;
2069 }
2070 case OVERWRITE_RIGHT:
2071 // If the argument in eax is already an object, we skip the
2072 // allocation of a heap number.
2073 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002074 __ j(not_zero, &skip_allocation);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002075 // Fall through!
2076 case NO_OVERWRITE:
2077 // Allocate a heap number for the result. Keep eax and edx intact
2078 // for the possible runtime call.
2079 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2080 // Now eax can be overwritten losing one of the arguments as we are
2081 // now done and will not need it any more.
2082 __ mov(eax, ebx);
2083 __ bind(&skip_allocation);
2084 break;
2085 default: UNREACHABLE();
2086 }
2087}
2088
2089
2090void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2091 __ pop(ecx);
2092 __ push(edx);
2093 __ push(eax);
2094 __ push(ecx);
2095}
2096
2097
ricow@chromium.org65fae842010-08-25 15:26:24 +00002098void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002099 // TAGGED case:
2100 // Input:
2101 // esp[4]: tagged number input argument (should be number).
2102 // esp[0]: return address.
2103 // Output:
2104 // eax: tagged double result.
2105 // UNTAGGED case:
2106 // Input::
2107 // esp[0]: return address.
2108 // xmm1: untagged double input argument
2109 // Output:
2110 // xmm1: untagged double result.
2111
ricow@chromium.org65fae842010-08-25 15:26:24 +00002112 Label runtime_call;
2113 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002114 Label skip_cache;
2115 const bool tagged = (argument_type_ == TAGGED);
2116 if (tagged) {
2117 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002118 Label input_not_smi;
2119 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002120 __ mov(eax, Operand(esp, kPointerSize));
2121 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002122 __ j(not_zero, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002123 // Input is a smi. Untag and load it onto the FPU stack.
2124 // Then load the low and high words of the double into ebx, edx.
2125 STATIC_ASSERT(kSmiTagSize == 1);
2126 __ sar(eax, 1);
2127 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2128 __ mov(Operand(esp, 0), eax);
2129 __ fild_s(Operand(esp, 0));
2130 __ fst_d(Operand(esp, 0));
2131 __ pop(edx);
2132 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002133 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002134 __ bind(&input_not_smi);
2135 // Check if input is a HeapNumber.
2136 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002137 Factory* factory = masm->isolate()->factory();
2138 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002139 __ j(not_equal, &runtime_call);
2140 // Input is a HeapNumber. Push it on the FPU stack and load its
2141 // low and high words into ebx, edx.
2142 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2143 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2144 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002145
whesse@chromium.org023421e2010-12-21 12:19:12 +00002146 __ bind(&loaded);
2147 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002148 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002149 CpuFeatures::Scope sse4_scope(SSE4_1);
2150 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2151 } else {
2152 __ pshufd(xmm0, xmm1, 0x1);
2153 __ movd(Operand(edx), xmm0);
2154 }
2155 __ movd(Operand(ebx), xmm1);
2156 }
2157
2158 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002159 // ebx = low 32 bits of double value
2160 // edx = high 32 bits of double value
2161 // Compute hash (the shifts are arithmetic):
2162 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2163 __ mov(ecx, ebx);
2164 __ xor_(ecx, Operand(edx));
2165 __ mov(eax, ecx);
2166 __ sar(eax, 16);
2167 __ xor_(ecx, Operand(eax));
2168 __ mov(eax, ecx);
2169 __ sar(eax, 8);
2170 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002171 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2172 __ and_(Operand(ecx),
2173 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002174
whesse@chromium.org023421e2010-12-21 12:19:12 +00002175 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002176 // ebx = low 32 bits of double value.
2177 // edx = high 32 bits of double value.
2178 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002179 ExternalReference cache_array =
2180 ExternalReference::transcendental_cache_array_address(masm->isolate());
2181 __ mov(eax, Immediate(cache_array));
2182 int cache_array_index =
2183 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2184 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002185 // Eax points to the cache for the type type_.
2186 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2187 __ test(eax, Operand(eax));
2188 __ j(zero, &runtime_call_clear_stack);
2189#ifdef DEBUG
2190 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002191 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002192 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2193 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2194 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2195 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2196 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2197 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2198 CHECK_EQ(0, elem_in0 - elem_start);
2199 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2200 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2201 }
2202#endif
2203 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2204 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2205 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2206 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002207 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002208 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002209 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002210 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002211 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002212 // Cache hit!
2213 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002214 if (tagged) {
2215 __ fstp(0);
2216 __ ret(kPointerSize);
2217 } else { // UNTAGGED.
2218 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2219 __ Ret();
2220 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002221
2222 __ bind(&cache_miss);
2223 // Update cache with new value.
2224 // We are short on registers, so use no_reg as scratch.
2225 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002226 if (tagged) {
2227 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2228 } else { // UNTAGGED.
2229 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2230 __ sub(Operand(esp), Immediate(kDoubleSize));
2231 __ movdbl(Operand(esp, 0), xmm1);
2232 __ fld_d(Operand(esp, 0));
2233 __ add(Operand(esp), Immediate(kDoubleSize));
2234 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002235 GenerateOperation(masm);
2236 __ mov(Operand(ecx, 0), ebx);
2237 __ mov(Operand(ecx, kIntSize), edx);
2238 __ mov(Operand(ecx, 2 * kIntSize), eax);
2239 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002240 if (tagged) {
2241 __ ret(kPointerSize);
2242 } else { // UNTAGGED.
2243 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2244 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002245
whesse@chromium.org023421e2010-12-21 12:19:12 +00002246 // Skip cache and return answer directly, only in untagged case.
2247 __ bind(&skip_cache);
2248 __ sub(Operand(esp), Immediate(kDoubleSize));
2249 __ movdbl(Operand(esp, 0), xmm1);
2250 __ fld_d(Operand(esp, 0));
2251 GenerateOperation(masm);
2252 __ fstp_d(Operand(esp, 0));
2253 __ movdbl(xmm1, Operand(esp, 0));
2254 __ add(Operand(esp), Immediate(kDoubleSize));
2255 // We return the value in xmm1 without adding it to the cache, but
2256 // we cause a scavenging GC so that future allocations will succeed.
2257 __ EnterInternalFrame();
2258 // Allocate an unused object bigger than a HeapNumber.
2259 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2260 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2261 __ LeaveInternalFrame();
2262 __ Ret();
2263 }
2264
2265 // Call runtime, doing whatever allocation and cleanup is necessary.
2266 if (tagged) {
2267 __ bind(&runtime_call_clear_stack);
2268 __ fstp(0);
2269 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002270 ExternalReference runtime =
2271 ExternalReference(RuntimeFunction(), masm->isolate());
2272 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002273 } else { // UNTAGGED.
2274 __ bind(&runtime_call_clear_stack);
2275 __ bind(&runtime_call);
2276 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2277 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2278 __ EnterInternalFrame();
2279 __ push(eax);
2280 __ CallRuntime(RuntimeFunction(), 1);
2281 __ LeaveInternalFrame();
2282 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2283 __ Ret();
2284 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002285}
2286
2287
2288Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2289 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002290 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2291 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002292 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002293 default:
2294 UNIMPLEMENTED();
2295 return Runtime::kAbort;
2296 }
2297}
2298
2299
2300void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2301 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002302 // Input value is on FP stack, and also in ebx/edx.
2303 // Input value is possibly in xmm1.
2304 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002305 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2306 // Both fsin and fcos require arguments in the range +/-2^63 and
2307 // return NaN for infinities and NaN. They can share all code except
2308 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002309 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002310 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2311 // work. We must reduce it to the appropriate range.
2312 __ mov(edi, edx);
2313 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2314 int supported_exponent_limit =
2315 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2316 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002317 __ j(below, &in_range, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002318 // Check for infinity and NaN. Both return NaN for sin.
2319 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002320 Label non_nan_result;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002321 __ j(not_equal, &non_nan_result, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002322 // Input is +/-Infinity or NaN. Result is NaN.
2323 __ fstp(0);
2324 // NaN is represented by 0x7ff8000000000000.
2325 __ push(Immediate(0x7ff80000));
2326 __ push(Immediate(0));
2327 __ fld_d(Operand(esp, 0));
2328 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002329 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002330
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002331 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002332
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002333 // Use fpmod to restrict argument to the range +/-2*PI.
2334 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2335 __ fldpi();
2336 __ fadd(0);
2337 __ fld(1);
2338 // FPU Stack: input, 2*pi, input.
2339 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002340 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002341 __ fwait();
2342 __ fnstsw_ax();
2343 // Clear if Illegal Operand or Zero Division exceptions are set.
2344 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002345 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002346 __ fnclex();
2347 __ bind(&no_exceptions);
2348 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002349
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002350 // Compute st(0) % st(1)
2351 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002352 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002353 __ bind(&partial_remainder_loop);
2354 __ fprem1();
2355 __ fwait();
2356 __ fnstsw_ax();
2357 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2358 // If C2 is set, computation only has partial result. Loop to
2359 // continue computation.
2360 __ j(not_zero, &partial_remainder_loop);
2361 }
2362 // FPU Stack: input, 2*pi, input % 2*pi
2363 __ fstp(2);
2364 __ fstp(0);
2365 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2366
2367 // FPU Stack: input % 2*pi
2368 __ bind(&in_range);
2369 switch (type_) {
2370 case TranscendentalCache::SIN:
2371 __ fsin();
2372 break;
2373 case TranscendentalCache::COS:
2374 __ fcos();
2375 break;
2376 default:
2377 UNREACHABLE();
2378 }
2379 __ bind(&done);
2380 } else {
2381 ASSERT(type_ == TranscendentalCache::LOG);
2382 __ fldln2();
2383 __ fxch();
2384 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002385 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002386}
2387
2388
ricow@chromium.org65fae842010-08-25 15:26:24 +00002389// Input: edx, eax are the left and right objects of a bit op.
2390// Output: eax, ecx are left and right integers for a bit op.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002391void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2392 bool use_sse3,
2393 Label* conversion_failure) {
2394 // Check float operands.
2395 Label arg1_is_object, check_undefined_arg1;
2396 Label arg2_is_object, check_undefined_arg2;
2397 Label load_arg2, done;
2398
2399 // Test if arg1 is a Smi.
2400 __ test(edx, Immediate(kSmiTagMask));
2401 __ j(not_zero, &arg1_is_object);
2402
2403 __ SmiUntag(edx);
2404 __ jmp(&load_arg2);
2405
2406 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2407 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002408 Factory* factory = masm->isolate()->factory();
2409 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002410 __ j(not_equal, conversion_failure);
2411 __ mov(edx, Immediate(0));
2412 __ jmp(&load_arg2);
2413
2414 __ bind(&arg1_is_object);
2415 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002416 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002417 __ j(not_equal, &check_undefined_arg1);
2418
2419 // Get the untagged integer version of the edx heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002420 IntegerConvert(masm, edx, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002421 __ mov(edx, ecx);
2422
2423 // Here edx has the untagged integer, eax has a Smi or a heap number.
2424 __ bind(&load_arg2);
2425
2426 // Test if arg2 is a Smi.
2427 __ test(eax, Immediate(kSmiTagMask));
2428 __ j(not_zero, &arg2_is_object);
2429
2430 __ SmiUntag(eax);
2431 __ mov(ecx, eax);
2432 __ jmp(&done);
2433
2434 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2435 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002436 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002437 __ j(not_equal, conversion_failure);
2438 __ mov(ecx, Immediate(0));
2439 __ jmp(&done);
2440
2441 __ bind(&arg2_is_object);
2442 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002443 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002444 __ j(not_equal, &check_undefined_arg2);
2445
2446 // Get the untagged integer version of the eax heap number in ecx.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002447 IntegerConvert(masm, eax, use_sse3, conversion_failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002448 __ bind(&done);
2449 __ mov(eax, edx);
2450}
2451
2452
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002453void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2454 bool use_sse3,
2455 Label* not_int32) {
2456 return;
2457}
2458
2459
ricow@chromium.org65fae842010-08-25 15:26:24 +00002460void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2461 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002462 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002463
2464 __ test(number, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002465 __ j(zero, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002466 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002467 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002468
2469 __ bind(&load_smi);
2470 __ SmiUntag(number);
2471 __ push(number);
2472 __ fild_s(Operand(esp, 0));
2473 __ pop(number);
2474
2475 __ bind(&done);
2476}
2477
2478
2479void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002480 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002481 // Load operand in edx into xmm0.
2482 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002483 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002484 __ j(zero, &load_smi_edx, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002485 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2486
2487 __ bind(&load_eax);
2488 // Load operand in eax into xmm1.
2489 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002490 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002491 __ j(zero, &load_smi_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002492 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002493 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002494
2495 __ bind(&load_smi_edx);
2496 __ SmiUntag(edx); // Untag smi before converting to float.
2497 __ cvtsi2sd(xmm0, Operand(edx));
2498 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2499 __ jmp(&load_eax);
2500
2501 __ bind(&load_smi_eax);
2502 __ SmiUntag(eax); // Untag smi before converting to float.
2503 __ cvtsi2sd(xmm1, Operand(eax));
2504 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2505
2506 __ bind(&done);
2507}
2508
2509
2510void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2511 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002512 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002513 // Load operand in edx into xmm0, or branch to not_numbers.
2514 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002515 // Argument in edx is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002516 __ j(zero, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002517 Factory* factory = masm->isolate()->factory();
2518 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002519 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2520 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2521 __ bind(&load_eax);
2522 // Load operand in eax into xmm1, or branch to not_numbers.
2523 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002524 // Argument in eax is a smi.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002525 __ j(zero, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002526 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002527 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002528 __ jmp(not_numbers); // Argument in eax is not a number.
2529 __ bind(&load_smi_edx);
2530 __ SmiUntag(edx); // Untag smi before converting to float.
2531 __ cvtsi2sd(xmm0, Operand(edx));
2532 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2533 __ jmp(&load_eax);
2534 __ bind(&load_smi_eax);
2535 __ SmiUntag(eax); // Untag smi before converting to float.
2536 __ cvtsi2sd(xmm1, Operand(eax));
2537 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002538 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002539 __ bind(&load_float_eax);
2540 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2541 __ bind(&done);
2542}
2543
2544
2545void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2546 Register scratch) {
2547 const Register left = edx;
2548 const Register right = eax;
2549 __ mov(scratch, left);
2550 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2551 __ SmiUntag(scratch);
2552 __ cvtsi2sd(xmm0, Operand(scratch));
2553
2554 __ mov(scratch, right);
2555 __ SmiUntag(scratch);
2556 __ cvtsi2sd(xmm1, Operand(scratch));
2557}
2558
2559
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002560void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2561 Label* non_int32,
2562 Register scratch) {
2563 __ cvttsd2si(scratch, Operand(xmm0));
2564 __ cvtsi2sd(xmm2, Operand(scratch));
2565 __ ucomisd(xmm0, xmm2);
2566 __ j(not_zero, non_int32);
2567 __ j(carry, non_int32);
2568 __ cvttsd2si(scratch, Operand(xmm1));
2569 __ cvtsi2sd(xmm2, Operand(scratch));
2570 __ ucomisd(xmm1, xmm2);
2571 __ j(not_zero, non_int32);
2572 __ j(carry, non_int32);
2573}
2574
2575
ricow@chromium.org65fae842010-08-25 15:26:24 +00002576void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2577 Register scratch,
2578 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002579 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002580 if (arg_location == ARGS_IN_REGISTERS) {
2581 __ mov(scratch, edx);
2582 } else {
2583 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2584 }
2585 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002586 __ j(zero, &load_smi_1, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002587 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2588 __ bind(&done_load_1);
2589
2590 if (arg_location == ARGS_IN_REGISTERS) {
2591 __ mov(scratch, eax);
2592 } else {
2593 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2594 }
2595 __ test(scratch, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002596 __ j(zero, &load_smi_2, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002597 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002598 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002599
2600 __ bind(&load_smi_1);
2601 __ SmiUntag(scratch);
2602 __ push(scratch);
2603 __ fild_s(Operand(esp, 0));
2604 __ pop(scratch);
2605 __ jmp(&done_load_1);
2606
2607 __ bind(&load_smi_2);
2608 __ SmiUntag(scratch);
2609 __ push(scratch);
2610 __ fild_s(Operand(esp, 0));
2611 __ pop(scratch);
2612
2613 __ bind(&done);
2614}
2615
2616
2617void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2618 Register scratch) {
2619 const Register left = edx;
2620 const Register right = eax;
2621 __ mov(scratch, left);
2622 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2623 __ SmiUntag(scratch);
2624 __ push(scratch);
2625 __ fild_s(Operand(esp, 0));
2626
2627 __ mov(scratch, right);
2628 __ SmiUntag(scratch);
2629 __ mov(Operand(esp, 0), scratch);
2630 __ fild_s(Operand(esp, 0));
2631 __ pop(scratch);
2632}
2633
2634
2635void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2636 Label* non_float,
2637 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002638 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002639 // Test if both operands are floats or smi -> scratch=k_is_float;
2640 // Otherwise scratch = k_not_float.
2641 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002642 __ j(zero, &test_other, Label::kNear); // argument in edx is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002643 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002644 Factory* factory = masm->isolate()->factory();
2645 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002646 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2647
2648 __ bind(&test_other);
2649 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002650 __ j(zero, &done, Label::kNear); // argument in eax is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002651 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002652 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002653 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2654
2655 // Fall-through: Both operands are numbers.
2656 __ bind(&done);
2657}
2658
2659
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002660void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2661 Label* non_int32) {
2662 return;
2663}
2664
2665
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002666void MathPowStub::Generate(MacroAssembler* masm) {
2667 // Registers are used as follows:
2668 // edx = base
2669 // eax = exponent
2670 // ecx = temporary, result
2671
2672 CpuFeatures::Scope use_sse2(SSE2);
2673 Label allocate_return, call_runtime;
2674
2675 // Load input parameters.
2676 __ mov(edx, Operand(esp, 2 * kPointerSize));
2677 __ mov(eax, Operand(esp, 1 * kPointerSize));
2678
2679 // Save 1 in xmm3 - we need this several times later on.
2680 __ mov(ecx, Immediate(1));
2681 __ cvtsi2sd(xmm3, Operand(ecx));
2682
2683 Label exponent_nonsmi;
2684 Label base_nonsmi;
2685 // If the exponent is a heap number go to that specific case.
2686 __ test(eax, Immediate(kSmiTagMask));
2687 __ j(not_zero, &exponent_nonsmi);
2688 __ test(edx, Immediate(kSmiTagMask));
2689 __ j(not_zero, &base_nonsmi);
2690
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002691 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002692 Label powi;
2693 __ SmiUntag(edx);
2694 __ cvtsi2sd(xmm0, Operand(edx));
2695 __ jmp(&powi);
2696 // exponent is smi and base is a heapnumber.
2697 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002698 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002699 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002700 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002701 __ j(not_equal, &call_runtime);
2702
2703 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2704
2705 // Optimized version of pow if exponent is a smi.
2706 // xmm0 contains the base.
2707 __ bind(&powi);
2708 __ SmiUntag(eax);
2709
2710 // Save exponent in base as we need to check if exponent is negative later.
2711 // We know that base and exponent are in different registers.
2712 __ mov(edx, eax);
2713
2714 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002715 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002716 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002717 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002718 __ neg(eax);
2719 __ bind(&no_neg);
2720
2721 // Load xmm1 with 1.
2722 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002723 Label while_true;
2724 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002725
2726 __ bind(&while_true);
2727 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002728 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002729 __ mulsd(xmm1, xmm0);
2730 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002731 __ mulsd(xmm0, xmm0);
2732 __ j(not_zero, &while_true);
2733
2734 // base has the original value of the exponent - if the exponent is
2735 // negative return 1/result.
2736 __ test(edx, Operand(edx));
2737 __ j(positive, &allocate_return);
2738 // Special case if xmm1 has reached infinity.
2739 __ mov(ecx, Immediate(0x7FB00000));
2740 __ movd(xmm0, Operand(ecx));
2741 __ cvtss2sd(xmm0, xmm0);
2742 __ ucomisd(xmm0, xmm1);
2743 __ j(equal, &call_runtime);
2744 __ divsd(xmm3, xmm1);
2745 __ movsd(xmm1, xmm3);
2746 __ jmp(&allocate_return);
2747
2748 // exponent (or both) is a heapnumber - no matter what we should now work
2749 // on doubles.
2750 __ bind(&exponent_nonsmi);
2751 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002752 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002753 __ j(not_equal, &call_runtime);
2754 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2755 // Test if exponent is nan.
2756 __ ucomisd(xmm1, xmm1);
2757 __ j(parity_even, &call_runtime);
2758
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002759 Label base_not_smi;
2760 Label handle_special_cases;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002761 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002762 __ j(not_zero, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002763 __ SmiUntag(edx);
2764 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002765 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002766
2767 __ bind(&base_not_smi);
2768 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002769 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002770 __ j(not_equal, &call_runtime);
2771 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2772 __ and_(ecx, HeapNumber::kExponentMask);
2773 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2774 // base is NaN or +/-Infinity
2775 __ j(greater_equal, &call_runtime);
2776 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2777
2778 // base is in xmm0 and exponent is in xmm1.
2779 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002780 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002781 // Test for -0.5.
2782 // Load xmm2 with -0.5.
2783 __ mov(ecx, Immediate(0xBF000000));
2784 __ movd(xmm2, Operand(ecx));
2785 __ cvtss2sd(xmm2, xmm2);
2786 // xmm2 now has -0.5.
2787 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002788 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002789
2790 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002791 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002792 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002793 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002794 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002795 __ divsd(xmm3, xmm1);
2796 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002797 __ jmp(&allocate_return);
2798
2799 // Test for 0.5.
2800 __ bind(&not_minus_half);
2801 // Load xmm2 with 0.5.
2802 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2803 __ addsd(xmm2, xmm3);
2804 // xmm2 now has 0.5.
2805 __ ucomisd(xmm2, xmm1);
2806 __ j(not_equal, &call_runtime);
2807 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002808 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002809 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002810 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002811 __ sqrtsd(xmm1, xmm1);
2812
2813 __ bind(&allocate_return);
2814 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2815 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2816 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002817 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002818
2819 __ bind(&call_runtime);
2820 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2821}
2822
2823
ricow@chromium.org65fae842010-08-25 15:26:24 +00002824void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2825 // The key is in edx and the parameter count is in eax.
2826
2827 // The displacement is used for skipping the frame pointer on the
2828 // stack. It is the offset of the last parameter (if any) relative
2829 // to the frame pointer.
2830 static const int kDisplacement = 1 * kPointerSize;
2831
2832 // Check that the key is a smi.
2833 Label slow;
2834 __ test(edx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002835 __ j(not_zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002836
2837 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002838 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002839 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2840 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2841 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002842 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002843
2844 // Check index against formal parameters count limit passed in
2845 // through register eax. Use unsigned comparison to get negative
2846 // check for free.
2847 __ cmp(edx, Operand(eax));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002848 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002849
2850 // Read the argument from the stack and return it.
2851 STATIC_ASSERT(kSmiTagSize == 1);
2852 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2853 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2854 __ neg(edx);
2855 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2856 __ ret(0);
2857
2858 // Arguments adaptor case: Check index against actual arguments
2859 // limit found in the arguments adaptor frame. Use unsigned
2860 // comparison to get negative check for free.
2861 __ bind(&adaptor);
2862 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2863 __ cmp(edx, Operand(ecx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002864 __ j(above_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002865
2866 // Read the argument from the stack and return it.
2867 STATIC_ASSERT(kSmiTagSize == 1);
2868 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2869 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2870 __ neg(edx);
2871 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2872 __ ret(0);
2873
2874 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2875 // by calling the runtime system.
2876 __ bind(&slow);
2877 __ pop(ebx); // Return address.
2878 __ push(edx);
2879 __ push(ebx);
2880 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2881}
2882
2883
2884void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2885 // esp[0] : return address
2886 // esp[4] : number of parameters
2887 // esp[8] : receiver displacement
2888 // esp[16] : function
2889
2890 // The displacement is used for skipping the return address and the
2891 // frame pointer on the stack. It is the offset of the last
2892 // parameter (if any) relative to the frame pointer.
2893 static const int kDisplacement = 2 * kPointerSize;
2894
2895 // Check if the calling frame is an arguments adaptor frame.
2896 Label adaptor_frame, try_allocate, runtime;
2897 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2898 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2899 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2900 __ j(equal, &adaptor_frame);
2901
2902 // Get the length from the frame.
2903 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2904 __ jmp(&try_allocate);
2905
2906 // Patch the arguments.length and the parameters pointer.
2907 __ bind(&adaptor_frame);
2908 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2909 __ mov(Operand(esp, 1 * kPointerSize), ecx);
2910 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
2911 __ mov(Operand(esp, 2 * kPointerSize), edx);
2912
2913 // Try the new space allocation. Start out with computing the size of
2914 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002915 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002916 __ bind(&try_allocate);
2917 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002918 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002919 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
2920 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002921 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002922
2923 // Do the allocation of both objects in one go.
2924 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
2925
2926 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002927 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2928 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002929 __ mov(edi, Operand(edi,
2930 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002931
2932 // Copy the JS object part.
2933 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2934 __ mov(ebx, FieldOperand(edi, i));
2935 __ mov(FieldOperand(eax, i), ebx);
2936 }
2937
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002938 if (type_ == NEW_NON_STRICT) {
2939 // Setup the callee in-object property.
2940 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2941 __ mov(ebx, Operand(esp, 3 * kPointerSize));
2942 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2943 Heap::kArgumentsCalleeIndex * kPointerSize),
2944 ebx);
2945 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002946
2947 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002948 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002949 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002950 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
2951 Heap::kArgumentsLengthIndex * kPointerSize),
2952 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002953
2954 // If there are no actual arguments, we're done.
2955 Label done;
2956 __ test(ecx, Operand(ecx));
2957 __ j(zero, &done);
2958
2959 // Get the parameters pointer from the stack.
2960 __ mov(edx, Operand(esp, 2 * kPointerSize));
2961
2962 // Setup the elements pointer in the allocated arguments object and
2963 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002964 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002965 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
2966 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002967 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002968
ricow@chromium.org65fae842010-08-25 15:26:24 +00002969 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
2970 // Untag the length for the loop below.
2971 __ SmiUntag(ecx);
2972
2973 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002974 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002975 __ bind(&loop);
2976 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
2977 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
2978 __ add(Operand(edi), Immediate(kPointerSize));
2979 __ sub(Operand(edx), Immediate(kPointerSize));
2980 __ dec(ecx);
2981 __ j(not_zero, &loop);
2982
2983 // Return and remove the on-stack parameters.
2984 __ bind(&done);
2985 __ ret(3 * kPointerSize);
2986
2987 // Do the runtime call to allocate the arguments object.
2988 __ bind(&runtime);
2989 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2990}
2991
2992
2993void RegExpExecStub::Generate(MacroAssembler* masm) {
2994 // Just jump directly to runtime if native RegExp is not selected at compile
2995 // time or if regexp entry in generated code is turned off runtime switch or
2996 // at compilation.
2997#ifdef V8_INTERPRETED_REGEXP
2998 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2999#else // V8_INTERPRETED_REGEXP
3000 if (!FLAG_regexp_entry_native) {
3001 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3002 return;
3003 }
3004
3005 // Stack frame on entry.
3006 // esp[0]: return address
3007 // esp[4]: last_match_info (expected JSArray)
3008 // esp[8]: previous index
3009 // esp[12]: subject string
3010 // esp[16]: JSRegExp object
3011
3012 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3013 static const int kPreviousIndexOffset = 2 * kPointerSize;
3014 static const int kSubjectOffset = 3 * kPointerSize;
3015 static const int kJSRegExpOffset = 4 * kPointerSize;
3016
3017 Label runtime, invoke_regexp;
3018
3019 // Ensure that a RegExp stack is allocated.
3020 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003021 ExternalReference::address_of_regexp_stack_memory_address(
3022 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003023 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003024 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003025 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3026 __ test(ebx, Operand(ebx));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003027 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003028
3029 // Check that the first argument is a JSRegExp object.
3030 __ mov(eax, Operand(esp, kJSRegExpOffset));
3031 STATIC_ASSERT(kSmiTag == 0);
3032 __ test(eax, Immediate(kSmiTagMask));
3033 __ j(zero, &runtime);
3034 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3035 __ j(not_equal, &runtime);
3036 // Check that the RegExp has been compiled (data contains a fixed array).
3037 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3038 if (FLAG_debug_code) {
3039 __ test(ecx, Immediate(kSmiTagMask));
3040 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3041 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3042 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3043 }
3044
3045 // ecx: RegExp data (FixedArray)
3046 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3047 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3048 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3049 __ j(not_equal, &runtime);
3050
3051 // ecx: RegExp data (FixedArray)
3052 // Check that the number of captures fit in the static offsets vector buffer.
3053 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3054 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3055 // uses the asumption that smis are 2 * their untagged value.
3056 STATIC_ASSERT(kSmiTag == 0);
3057 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3058 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3059 // Check that the static offsets vector buffer is large enough.
3060 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3061 __ j(above, &runtime);
3062
3063 // ecx: RegExp data (FixedArray)
3064 // edx: Number of capture registers
3065 // Check that the second argument is a string.
3066 __ mov(eax, Operand(esp, kSubjectOffset));
3067 __ test(eax, Immediate(kSmiTagMask));
3068 __ j(zero, &runtime);
3069 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3070 __ j(NegateCondition(is_string), &runtime);
3071 // Get the length of the string to ebx.
3072 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3073
3074 // ebx: Length of subject string as a smi
3075 // ecx: RegExp data (FixedArray)
3076 // edx: Number of capture registers
3077 // Check that the third argument is a positive smi less than the subject
3078 // string length. A negative value will be greater (unsigned comparison).
3079 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3080 __ test(eax, Immediate(kSmiTagMask));
3081 __ j(not_zero, &runtime);
3082 __ cmp(eax, Operand(ebx));
3083 __ j(above_equal, &runtime);
3084
3085 // ecx: RegExp data (FixedArray)
3086 // edx: Number of capture registers
3087 // Check that the fourth object is a JSArray object.
3088 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3089 __ test(eax, Immediate(kSmiTagMask));
3090 __ j(zero, &runtime);
3091 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3092 __ j(not_equal, &runtime);
3093 // Check that the JSArray is in fast case.
3094 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3095 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003096 Factory* factory = masm->isolate()->factory();
3097 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003098 __ j(not_equal, &runtime);
3099 // Check that the last match info has space for the capture registers and the
3100 // additional information.
3101 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3102 __ SmiUntag(eax);
3103 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3104 __ cmp(edx, Operand(eax));
3105 __ j(greater, &runtime);
3106
3107 // ecx: RegExp data (FixedArray)
3108 // Check the representation and encoding of the subject string.
3109 Label seq_ascii_string, seq_two_byte_string, check_code;
3110 __ mov(eax, Operand(esp, kSubjectOffset));
3111 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3112 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3113 // First check for flat two byte string.
3114 __ and_(ebx,
3115 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3116 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3117 __ j(zero, &seq_two_byte_string);
3118 // Any other flat string must be a flat ascii string.
3119 __ test(Operand(ebx),
3120 Immediate(kIsNotStringMask | kStringRepresentationMask));
3121 __ j(zero, &seq_ascii_string);
3122
3123 // Check for flat cons string.
3124 // A flat cons string is a cons string where the second part is the empty
3125 // string. In that case the subject string is just the first part of the cons
3126 // string. Also in this case the first part of the cons string is known to be
3127 // a sequential string or an external string.
3128 STATIC_ASSERT(kExternalStringTag != 0);
3129 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3130 __ test(Operand(ebx),
3131 Immediate(kIsNotStringMask | kExternalStringTag));
3132 __ j(not_zero, &runtime);
3133 // String is a cons string.
3134 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003135 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003136 __ j(not_equal, &runtime);
3137 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3138 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3139 // String is a cons string with empty second part.
3140 // eax: first part of cons string.
3141 // ebx: map of first part of cons string.
3142 // Is first part a flat two byte string?
3143 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3144 kStringRepresentationMask | kStringEncodingMask);
3145 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3146 __ j(zero, &seq_two_byte_string);
3147 // Any other flat string must be ascii.
3148 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3149 kStringRepresentationMask);
3150 __ j(not_zero, &runtime);
3151
3152 __ bind(&seq_ascii_string);
3153 // eax: subject string (flat ascii)
3154 // ecx: RegExp data (FixedArray)
3155 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3156 __ Set(edi, Immediate(1)); // Type is ascii.
3157 __ jmp(&check_code);
3158
3159 __ bind(&seq_two_byte_string);
3160 // eax: subject string (flat two byte)
3161 // ecx: RegExp data (FixedArray)
3162 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3163 __ Set(edi, Immediate(0)); // Type is two byte.
3164
3165 __ bind(&check_code);
3166 // Check that the irregexp code has been generated for the actual string
3167 // encoding. If it has, the field contains a code object otherwise it contains
3168 // the hole.
3169 __ CmpObjectType(edx, CODE_TYPE, ebx);
3170 __ j(not_equal, &runtime);
3171
3172 // eax: subject string
3173 // edx: code
3174 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3175 // Load used arguments before starting to push arguments for call to native
3176 // RegExp code to avoid handling changing stack height.
3177 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3178 __ SmiUntag(ebx); // Previous index from smi.
3179
3180 // eax: subject string
3181 // ebx: previous index
3182 // edx: code
3183 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3184 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003185 Counters* counters = masm->isolate()->counters();
3186 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003187
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003188 // Isolates: note we add an additional parameter here (isolate pointer).
3189 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003190 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003191
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003192 // Argument 8: Pass current isolate address.
3193 __ mov(Operand(esp, 7 * kPointerSize),
3194 Immediate(ExternalReference::isolate_address()));
3195
ricow@chromium.org65fae842010-08-25 15:26:24 +00003196 // Argument 7: Indicate that this is a direct call from JavaScript.
3197 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3198
3199 // Argument 6: Start (high end) of backtracking stack memory area.
3200 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3201 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3202 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3203
3204 // Argument 5: static offsets vector buffer.
3205 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003206 Immediate(ExternalReference::address_of_static_offsets_vector(
3207 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003208
3209 // Argument 4: End of string data
3210 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003211 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003212 __ test(edi, Operand(edi));
3213 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003214 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003215 __ SmiUntag(edi);
3216 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3217 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3218 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3219 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003220 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003221
3222 __ bind(&setup_two_byte);
3223 STATIC_ASSERT(kSmiTag == 0);
3224 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3225 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3226 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3227 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3228 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3229
3230 __ bind(&setup_rest);
3231
3232 // Argument 2: Previous index.
3233 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3234
3235 // Argument 1: Subject string.
3236 __ mov(Operand(esp, 0 * kPointerSize), eax);
3237
3238 // Locate the code entry and call it.
3239 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003240 __ call(Operand(edx));
3241
3242 // Drop arguments and come back to JS mode.
3243 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003244
3245 // Check the result.
3246 Label success;
3247 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003248 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003249 Label failure;
3250 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003251 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003252 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3253 // If not exception it can only be retry. Handle that in the runtime system.
3254 __ j(not_equal, &runtime);
3255 // Result must now be exception. If there is no pending exception already a
3256 // stack overflow (on the backtrack stack) was detected in RegExp code but
3257 // haven't created the exception yet. Handle that in the runtime system.
3258 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003259 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3260 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003261 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003262 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3263 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003264 __ mov(eax, Operand::StaticVariable(pending_exception));
3265 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003266 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003267 // For exception, throw the exception again.
3268
3269 // Clear the pending exception variable.
3270 __ mov(Operand::StaticVariable(pending_exception), edx);
3271
3272 // Special handling of termination exceptions which are uncatchable
3273 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003274 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003275 Label throw_termination_exception;
3276 __ j(equal, &throw_termination_exception);
3277
3278 // Handle normal exception by following handler chain.
3279 __ Throw(eax);
3280
3281 __ bind(&throw_termination_exception);
3282 __ ThrowUncatchable(TERMINATION, eax);
3283
ricow@chromium.org65fae842010-08-25 15:26:24 +00003284 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003285 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003286 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003287 __ ret(4 * kPointerSize);
3288
3289 // Load RegExp data.
3290 __ bind(&success);
3291 __ mov(eax, Operand(esp, kJSRegExpOffset));
3292 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3293 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3294 // Calculate number of capture registers (number_of_captures + 1) * 2.
3295 STATIC_ASSERT(kSmiTag == 0);
3296 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3297 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3298
3299 // edx: Number of capture registers
3300 // Load last_match_info which is still known to be a fast case JSArray.
3301 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3302 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3303
3304 // ebx: last_match_info backing store (FixedArray)
3305 // edx: number of capture registers
3306 // Store the capture count.
3307 __ SmiTag(edx); // Number of capture registers to smi.
3308 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3309 __ SmiUntag(edx); // Number of capture registers back from smi.
3310 // Store last subject and last input.
3311 __ mov(eax, Operand(esp, kSubjectOffset));
3312 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3313 __ mov(ecx, ebx);
3314 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3315 __ mov(eax, Operand(esp, kSubjectOffset));
3316 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3317 __ mov(ecx, ebx);
3318 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3319
3320 // Get the static offsets vector filled by the native regexp code.
3321 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003322 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003323 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3324
3325 // ebx: last_match_info backing store (FixedArray)
3326 // ecx: offsets vector
3327 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003328 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003329 // Capture register counter starts from number of capture registers and
3330 // counts down until wraping after zero.
3331 __ bind(&next_capture);
3332 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003333 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003334 // Read the value from the static offsets vector buffer.
3335 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3336 __ SmiTag(edi);
3337 // Store the smi value in the last match info.
3338 __ mov(FieldOperand(ebx,
3339 edx,
3340 times_pointer_size,
3341 RegExpImpl::kFirstCaptureOffset),
3342 edi);
3343 __ jmp(&next_capture);
3344 __ bind(&done);
3345
3346 // Return last match info.
3347 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3348 __ ret(4 * kPointerSize);
3349
3350 // Do the runtime call to execute the regexp.
3351 __ bind(&runtime);
3352 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3353#endif // V8_INTERPRETED_REGEXP
3354}
3355
3356
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003357void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3358 const int kMaxInlineLength = 100;
3359 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003360 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003361 __ mov(ebx, Operand(esp, kPointerSize * 3));
3362 __ test(ebx, Immediate(kSmiTagMask));
3363 __ j(not_zero, &slowcase);
3364 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3365 __ j(above, &slowcase);
3366 // Smi-tagging is equivalent to multiplying by 2.
3367 STATIC_ASSERT(kSmiTag == 0);
3368 STATIC_ASSERT(kSmiTagSize == 1);
3369 // Allocate RegExpResult followed by FixedArray with size in ebx.
3370 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3371 // Elements: [Map][Length][..elements..]
3372 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3373 times_half_pointer_size,
3374 ebx, // In: Number of elements (times 2, being a smi)
3375 eax, // Out: Start of allocation (tagged).
3376 ecx, // Out: End of allocation.
3377 edx, // Scratch register
3378 &slowcase,
3379 TAG_OBJECT);
3380 // eax: Start of allocated area, object-tagged.
3381
3382 // Set JSArray map to global.regexp_result_map().
3383 // Set empty properties FixedArray.
3384 // Set elements to point to FixedArray allocated right after the JSArray.
3385 // Interleave operations for better latency.
3386 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003387 Factory* factory = masm->isolate()->factory();
3388 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003389 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3390 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3391 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3392 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3393 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3394 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3395
3396 // Set input, index and length fields from arguments.
3397 __ mov(ecx, Operand(esp, kPointerSize * 1));
3398 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3399 __ mov(ecx, Operand(esp, kPointerSize * 2));
3400 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3401 __ mov(ecx, Operand(esp, kPointerSize * 3));
3402 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3403
3404 // Fill out the elements FixedArray.
3405 // eax: JSArray.
3406 // ebx: FixedArray.
3407 // ecx: Number of elements in array, as smi.
3408
3409 // Set map.
3410 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003411 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003412 // Set length.
3413 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3414 // Fill contents of fixed-array with the-hole.
3415 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003416 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003417 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3418 // Fill fixed array elements with hole.
3419 // eax: JSArray.
3420 // ecx: Number of elements to fill.
3421 // ebx: Start of elements in FixedArray.
3422 // edx: the hole.
3423 Label loop;
3424 __ test(ecx, Operand(ecx));
3425 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003426 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003427 __ sub(Operand(ecx), Immediate(1));
3428 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3429 __ jmp(&loop);
3430
3431 __ bind(&done);
3432 __ ret(3 * kPointerSize);
3433
3434 __ bind(&slowcase);
3435 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3436}
3437
3438
ricow@chromium.org65fae842010-08-25 15:26:24 +00003439void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3440 Register object,
3441 Register result,
3442 Register scratch1,
3443 Register scratch2,
3444 bool object_is_smi,
3445 Label* not_found) {
3446 // Use of registers. Register result is used as a temporary.
3447 Register number_string_cache = result;
3448 Register mask = scratch1;
3449 Register scratch = scratch2;
3450
3451 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003452 ExternalReference roots_address =
3453 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003454 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3455 __ mov(number_string_cache,
3456 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3457 // Make the hash mask from the length of the number string cache. It
3458 // contains two elements (number and string) for each cache entry.
3459 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3460 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3461 __ sub(Operand(mask), Immediate(1)); // Make mask.
3462
3463 // Calculate the entry in the number string cache. The hash value in the
3464 // number string cache for smis is just the smi value, and the hash for
3465 // doubles is the xor of the upper and lower words. See
3466 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003467 Label smi_hash_calculated;
3468 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003469 if (object_is_smi) {
3470 __ mov(scratch, object);
3471 __ SmiUntag(scratch);
3472 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003473 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003474 STATIC_ASSERT(kSmiTag == 0);
3475 __ test(object, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003476 __ j(not_zero, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003477 __ mov(scratch, object);
3478 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003479 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003480 __ bind(&not_smi);
3481 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003482 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003483 __ j(not_equal, not_found);
3484 STATIC_ASSERT(8 == kDoubleSize);
3485 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3486 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3487 // Object is heap number and hash is now in scratch. Calculate cache index.
3488 __ and_(scratch, Operand(mask));
3489 Register index = scratch;
3490 Register probe = mask;
3491 __ mov(probe,
3492 FieldOperand(number_string_cache,
3493 index,
3494 times_twice_pointer_size,
3495 FixedArray::kHeaderSize));
3496 __ test(probe, Immediate(kSmiTagMask));
3497 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003498 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003499 CpuFeatures::Scope fscope(SSE2);
3500 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3501 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3502 __ ucomisd(xmm0, xmm1);
3503 } else {
3504 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3505 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3506 __ FCmp();
3507 }
3508 __ j(parity_even, not_found); // Bail out if NaN is involved.
3509 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003510 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003511 }
3512
3513 __ bind(&smi_hash_calculated);
3514 // Object is smi and hash is now in scratch. Calculate cache index.
3515 __ and_(scratch, Operand(mask));
3516 Register index = scratch;
3517 // Check if the entry is the smi we are looking for.
3518 __ cmp(object,
3519 FieldOperand(number_string_cache,
3520 index,
3521 times_twice_pointer_size,
3522 FixedArray::kHeaderSize));
3523 __ j(not_equal, not_found);
3524
3525 // Get the result from the cache.
3526 __ bind(&load_result_from_cache);
3527 __ mov(result,
3528 FieldOperand(number_string_cache,
3529 index,
3530 times_twice_pointer_size,
3531 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003532 Counters* counters = masm->isolate()->counters();
3533 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003534}
3535
3536
3537void NumberToStringStub::Generate(MacroAssembler* masm) {
3538 Label runtime;
3539
3540 __ mov(ebx, Operand(esp, kPointerSize));
3541
3542 // Generate code to lookup number in the number string cache.
3543 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3544 __ ret(1 * kPointerSize);
3545
3546 __ bind(&runtime);
3547 // Handle number to string in the runtime system if not found in the cache.
3548 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3549}
3550
3551
3552static int NegativeComparisonResult(Condition cc) {
3553 ASSERT(cc != equal);
3554 ASSERT((cc == less) || (cc == less_equal)
3555 || (cc == greater) || (cc == greater_equal));
3556 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3557}
3558
3559void CompareStub::Generate(MacroAssembler* masm) {
3560 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3561
3562 Label check_unequal_objects, done;
3563
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003564 // Compare two smis if required.
3565 if (include_smi_compare_) {
3566 Label non_smi, smi_done;
3567 __ mov(ecx, Operand(edx));
3568 __ or_(ecx, Operand(eax));
3569 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003570 __ j(not_zero, &non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003571 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3572 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003573 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003574 __ bind(&smi_done);
3575 __ mov(eax, edx);
3576 __ ret(0);
3577 __ bind(&non_smi);
3578 } else if (FLAG_debug_code) {
3579 __ mov(ecx, Operand(edx));
3580 __ or_(ecx, Operand(eax));
3581 __ test(ecx, Immediate(kSmiTagMask));
3582 __ Assert(not_zero, "Unexpected smi operands.");
3583 }
3584
ricow@chromium.org65fae842010-08-25 15:26:24 +00003585 // NOTICE! This code is only reached after a smi-fast-case check, so
3586 // it is certain that at least one operand isn't a smi.
3587
3588 // Identical objects can be compared fast, but there are some tricky cases
3589 // for NaN and undefined.
3590 {
3591 Label not_identical;
3592 __ cmp(eax, Operand(edx));
3593 __ j(not_equal, &not_identical);
3594
3595 if (cc_ != equal) {
3596 // Check for undefined. undefined OP undefined is false even though
3597 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003598 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003599 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003600 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003601 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3602 __ ret(0);
3603 __ bind(&check_for_nan);
3604 }
3605
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003606 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003607 // so we do the second best thing - test it ourselves.
3608 // Note: if cc_ != equal, never_nan_nan_ is not used.
3609 if (never_nan_nan_ && (cc_ == equal)) {
3610 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3611 __ ret(0);
3612 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003613 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003614 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003615 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003616 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003617 if (cc_ != equal) {
3618 // Call runtime on identical JSObjects. Otherwise return equal.
3619 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3620 __ j(above_equal, &not_identical);
3621 }
3622 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3623 __ ret(0);
3624
3625 __ bind(&heap_number);
3626 // It is a heap number, so return non-equal if it's NaN and equal if
3627 // it's not NaN.
3628 // The representation of NaN values has all exponent bits (52..62) set,
3629 // and not all mantissa bits (0..51) clear.
3630 // We only accept QNaNs, which have bit 51 set.
3631 // Read top bits of double representation (second word of value).
3632
3633 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3634 // all bits in the mask are set. We only need to check the word
3635 // that contains the exponent and high bit of the mantissa.
3636 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3637 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003638 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003639 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3640 // bits.
3641 __ add(edx, Operand(edx));
3642 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3643 if (cc_ == equal) {
3644 STATIC_ASSERT(EQUAL != 1);
3645 __ setcc(above_equal, eax);
3646 __ ret(0);
3647 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003648 Label nan;
3649 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003650 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3651 __ ret(0);
3652 __ bind(&nan);
3653 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3654 __ ret(0);
3655 }
3656 }
3657
3658 __ bind(&not_identical);
3659 }
3660
3661 // Strict equality can quickly decide whether objects are equal.
3662 // Non-strict object equality is slower, so it is handled later in the stub.
3663 if (cc_ == equal && strict_) {
3664 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003665 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003666 // If we're doing a strict equality comparison, we don't have to do
3667 // type conversion, so we generate code to do fast comparison for objects
3668 // and oddballs. Non-smi numbers and strings still go through the usual
3669 // slow-case code.
3670 // If either is a Smi (we know that not both are), then they can only
3671 // be equal if the other is a HeapNumber. If so, use the slow case.
3672 STATIC_ASSERT(kSmiTag == 0);
3673 ASSERT_EQ(0, Smi::FromInt(0));
3674 __ mov(ecx, Immediate(kSmiTagMask));
3675 __ and_(ecx, Operand(eax));
3676 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003677 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003678 // One operand is a smi.
3679
3680 // Check whether the non-smi is a heap number.
3681 STATIC_ASSERT(kSmiTagMask == 1);
3682 // ecx still holds eax & kSmiTag, which is either zero or one.
3683 __ sub(Operand(ecx), Immediate(0x01));
3684 __ mov(ebx, edx);
3685 __ xor_(ebx, Operand(eax));
3686 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3687 __ xor_(ebx, Operand(eax));
3688 // if eax was smi, ebx is now edx, else eax.
3689
3690 // Check if the non-smi operand is a heap number.
3691 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003692 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003693 // If heap number, handle it in the slow case.
3694 __ j(equal, &slow);
3695 // Return non-equal (ebx is not zero)
3696 __ mov(eax, ebx);
3697 __ ret(0);
3698
3699 __ bind(&not_smis);
3700 // If either operand is a JSObject or an oddball value, then they are not
3701 // equal since their pointers are different
3702 // There is no test for undetectability in strict equality.
3703
3704 // Get the type of the first operand.
3705 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003706 Label first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003707 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3708 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003709 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003710
3711 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003712 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003713 STATIC_ASSERT(kHeapObjectTag != 0);
3714 __ bind(&return_not_equal);
3715 __ ret(0);
3716
3717 __ bind(&first_non_object);
3718 // Check for oddballs: true, false, null, undefined.
3719 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3720 __ j(equal, &return_not_equal);
3721
3722 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
3723 __ j(above_equal, &return_not_equal);
3724
3725 // Check for oddballs: true, false, null, undefined.
3726 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3727 __ j(equal, &return_not_equal);
3728
3729 // Fall through to the general case.
3730 __ bind(&slow);
3731 }
3732
3733 // Generate the number comparison code.
3734 if (include_number_compare_) {
3735 Label non_number_comparison;
3736 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003737 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003738 CpuFeatures::Scope use_sse2(SSE2);
3739 CpuFeatures::Scope use_cmov(CMOV);
3740
3741 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3742 __ ucomisd(xmm0, xmm1);
3743
3744 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003745 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003746 // Return a result of -1, 0, or 1, based on EFLAGS.
3747 __ mov(eax, 0); // equal
3748 __ mov(ecx, Immediate(Smi::FromInt(1)));
3749 __ cmov(above, eax, Operand(ecx));
3750 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3751 __ cmov(below, eax, Operand(ecx));
3752 __ ret(0);
3753 } else {
3754 FloatingPointHelper::CheckFloatOperands(
3755 masm, &non_number_comparison, ebx);
3756 FloatingPointHelper::LoadFloatOperand(masm, eax);
3757 FloatingPointHelper::LoadFloatOperand(masm, edx);
3758 __ FCmp();
3759
3760 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003761 __ j(parity_even, &unordered);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003762
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003763 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003764 // Return a result of -1, 0, or 1, based on EFLAGS.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003765 __ j(below, &below_label);
3766 __ j(above, &above_label);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003767
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003768 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003769 __ ret(0);
3770
3771 __ bind(&below_label);
3772 __ mov(eax, Immediate(Smi::FromInt(-1)));
3773 __ ret(0);
3774
3775 __ bind(&above_label);
3776 __ mov(eax, Immediate(Smi::FromInt(1)));
3777 __ ret(0);
3778 }
3779
3780 // If one of the numbers was NaN, then the result is always false.
3781 // The cc is never not-equal.
3782 __ bind(&unordered);
3783 ASSERT(cc_ != not_equal);
3784 if (cc_ == less || cc_ == less_equal) {
3785 __ mov(eax, Immediate(Smi::FromInt(1)));
3786 } else {
3787 __ mov(eax, Immediate(Smi::FromInt(-1)));
3788 }
3789 __ ret(0);
3790
3791 // The number comparison code did not provide a valid result.
3792 __ bind(&non_number_comparison);
3793 }
3794
3795 // Fast negative check for symbol-to-symbol equality.
3796 Label check_for_strings;
3797 if (cc_ == equal) {
3798 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
3799 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
3800
3801 // We've already checked for object identity, so if both operands
3802 // are symbols they aren't equal. Register eax already holds a
3803 // non-zero value, which indicates not equal, so just return.
3804 __ ret(0);
3805 }
3806
3807 __ bind(&check_for_strings);
3808
3809 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
3810 &check_unequal_objects);
3811
3812 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003813 if (cc_ == equal) {
3814 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00003815 edx,
3816 eax,
3817 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00003818 ebx);
3819 } else {
3820 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3821 edx,
3822 eax,
3823 ecx,
3824 ebx,
3825 edi);
3826 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003827#ifdef DEBUG
3828 __ Abort("Unexpected fall-through from string comparison");
3829#endif
3830
3831 __ bind(&check_unequal_objects);
3832 if (cc_ == equal && !strict_) {
3833 // Non-strict equality. Objects are unequal if
3834 // they are both JSObjects and not undetectable,
3835 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003836 Label not_both_objects;
3837 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003838 // At most one is a smi, so we can test for smi by adding the two.
3839 // A smi plus a heap object has the low bit set, a heap object plus
3840 // a heap object has the low bit clear.
3841 STATIC_ASSERT(kSmiTag == 0);
3842 STATIC_ASSERT(kSmiTagMask == 1);
3843 __ lea(ecx, Operand(eax, edx, times_1, 0));
3844 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003845 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003846 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003847 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003848 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003849 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003850 // We do not bail out after this point. Both are JSObjects, and
3851 // they are equal if and only if both are undetectable.
3852 // The and of the undetectable flags is 1 if and only if they are equal.
3853 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3854 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003855 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003856 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
3857 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003858 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003859 // The objects are both undetectable, so they both compare as the value
3860 // undefined, and are equal.
3861 __ Set(eax, Immediate(EQUAL));
3862 __ bind(&return_unequal);
3863 // Return non-equal by returning the non-zero object pointer in eax,
3864 // or return equal if we fell through to here.
3865 __ ret(0); // rax, rdx were pushed
3866 __ bind(&not_both_objects);
3867 }
3868
3869 // Push arguments below the return address.
3870 __ pop(ecx);
3871 __ push(edx);
3872 __ push(eax);
3873
3874 // Figure out which native to call and setup the arguments.
3875 Builtins::JavaScript builtin;
3876 if (cc_ == equal) {
3877 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3878 } else {
3879 builtin = Builtins::COMPARE;
3880 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3881 }
3882
3883 // Restore return address on the stack.
3884 __ push(ecx);
3885
3886 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3887 // tagged as a small integer.
3888 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3889}
3890
3891
3892void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3893 Label* label,
3894 Register object,
3895 Register scratch) {
3896 __ test(object, Immediate(kSmiTagMask));
3897 __ j(zero, label);
3898 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
3899 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3900 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
3901 __ cmp(scratch, kSymbolTag | kStringTag);
3902 __ j(not_equal, label);
3903}
3904
3905
3906void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003907 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003908}
3909
3910
3911void CallFunctionStub::Generate(MacroAssembler* masm) {
3912 Label slow;
3913
3914 // If the receiver might be a value (string, number or boolean) check for this
3915 // and box it if it is.
3916 if (ReceiverMightBeValue()) {
3917 // Get the receiver from the stack.
3918 // +1 ~ return address
3919 Label receiver_is_value, receiver_is_js_object;
3920 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
3921
3922 // Check if receiver is a smi (which is a number value).
3923 __ test(eax, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003924 __ j(zero, &receiver_is_value);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003925
3926 // Check if the receiver is a valid JS object.
3927 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
3928 __ j(above_equal, &receiver_is_js_object);
3929
3930 // Call the runtime to box the value.
3931 __ bind(&receiver_is_value);
3932 __ EnterInternalFrame();
3933 __ push(eax);
3934 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
3935 __ LeaveInternalFrame();
3936 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
3937
3938 __ bind(&receiver_is_js_object);
3939 }
3940
3941 // Get the function to call from the stack.
3942 // +2 ~ receiver, return address
3943 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
3944
3945 // Check that the function really is a JavaScript function.
3946 __ test(edi, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003947 __ j(zero, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003948 // Goto slow case if we do not have a function.
3949 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003950 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003951
3952 // Fast-case: Just invoke the function.
3953 ParameterCount actual(argc_);
3954 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
3955
3956 // Slow-case: Non-function called.
3957 __ bind(&slow);
3958 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3959 // of the original receiver from the call site).
3960 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
3961 __ Set(eax, Immediate(argc_));
3962 __ Set(ebx, Immediate(0));
3963 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003964 Handle<Code> adaptor =
3965 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003966 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3967}
3968
3969
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00003970bool CEntryStub::NeedsImmovableCode() {
3971 return false;
3972}
3973
3974
ricow@chromium.org65fae842010-08-25 15:26:24 +00003975void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003976 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003977}
3978
3979
ricow@chromium.org65fae842010-08-25 15:26:24 +00003980void CEntryStub::GenerateCore(MacroAssembler* masm,
3981 Label* throw_normal_exception,
3982 Label* throw_termination_exception,
3983 Label* throw_out_of_memory_exception,
3984 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00003985 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003986 // eax: result parameter for PerformGC, if any
3987 // ebx: pointer to C function (C callee-saved)
3988 // ebp: frame pointer (restored after C call)
3989 // esp: stack pointer (restored after C call)
3990 // edi: number of arguments including receiver (C callee-saved)
3991 // esi: pointer to the first argument (C callee-saved)
3992
3993 // Result returned in eax, or eax+edx if result_size_ is 2.
3994
3995 // Check stack alignment.
3996 if (FLAG_debug_code) {
3997 __ CheckStackAlignment();
3998 }
3999
4000 if (do_gc) {
4001 // Pass failure code returned from last attempt as first argument to
4002 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4003 // stack alignment is known to be correct. This function takes one argument
4004 // which is passed on the stack, and we know that the stack has been
4005 // prepared to pass at least one argument.
4006 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4007 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4008 }
4009
4010 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004011 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004012 if (always_allocate_scope) {
4013 __ inc(Operand::StaticVariable(scope_depth));
4014 }
4015
4016 // Call C function.
4017 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4018 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004019 __ mov(Operand(esp, 2 * kPointerSize),
4020 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004021 __ call(Operand(ebx));
4022 // Result is in eax or edx:eax - do not destroy these registers!
4023
4024 if (always_allocate_scope) {
4025 __ dec(Operand::StaticVariable(scope_depth));
4026 }
4027
4028 // Make sure we're not trying to return 'the hole' from the runtime
4029 // call as this may lead to crashes in the IC code later.
4030 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004031 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004032 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004033 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004034 __ int3();
4035 __ bind(&okay);
4036 }
4037
4038 // Check for failure result.
4039 Label failure_returned;
4040 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4041 __ lea(ecx, Operand(eax, 1));
4042 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4043 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004044 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004045
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004046 ExternalReference pending_exception_address(
4047 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004048
4049 // Check that there is no pending exception, otherwise we
4050 // should have returned some failure value.
4051 if (FLAG_debug_code) {
4052 __ push(edx);
4053 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004054 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004055 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004056 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4057 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004058 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004059 __ int3();
4060 __ bind(&okay);
4061 __ pop(edx);
4062 }
4063
ricow@chromium.org65fae842010-08-25 15:26:24 +00004064 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004065 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004066 __ ret(0);
4067
4068 // Handling of failure.
4069 __ bind(&failure_returned);
4070
4071 Label retry;
4072 // If the returned exception is RETRY_AFTER_GC continue at retry label
4073 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4074 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004075 __ j(zero, &retry);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004076
4077 // Special handling of out of memory exceptions.
4078 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4079 __ j(equal, throw_out_of_memory_exception);
4080
4081 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004082 ExternalReference the_hole_location =
4083 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004084 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004085 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004086 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4087
4088 // Special handling of termination exceptions which are uncatchable
4089 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004090 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004091 __ j(equal, throw_termination_exception);
4092
4093 // Handle normal exception.
4094 __ jmp(throw_normal_exception);
4095
4096 // Retry.
4097 __ bind(&retry);
4098}
4099
4100
4101void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4102 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004103 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004104}
4105
4106
4107void CEntryStub::Generate(MacroAssembler* masm) {
4108 // eax: number of arguments including receiver
4109 // ebx: pointer to C function (C callee-saved)
4110 // ebp: frame pointer (restored after C call)
4111 // esp: stack pointer (restored after C call)
4112 // esi: current context (C callee-saved)
4113 // edi: JS function of the caller (C callee-saved)
4114
4115 // NOTE: Invocations of builtins may return failure objects instead
4116 // of a proper result. The builtin entry handles this by performing
4117 // a garbage collection and retrying the builtin (twice).
4118
4119 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004120 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004121
4122 // eax: result parameter for PerformGC, if any (setup below)
4123 // ebx: pointer to builtin function (C callee-saved)
4124 // ebp: frame pointer (restored after C call)
4125 // esp: stack pointer (restored after C call)
4126 // edi: number of arguments including receiver (C callee-saved)
4127 // esi: argv pointer (C callee-saved)
4128
4129 Label throw_normal_exception;
4130 Label throw_termination_exception;
4131 Label throw_out_of_memory_exception;
4132
4133 // Call into the runtime system.
4134 GenerateCore(masm,
4135 &throw_normal_exception,
4136 &throw_termination_exception,
4137 &throw_out_of_memory_exception,
4138 false,
4139 false);
4140
4141 // Do space-specific GC and retry runtime call.
4142 GenerateCore(masm,
4143 &throw_normal_exception,
4144 &throw_termination_exception,
4145 &throw_out_of_memory_exception,
4146 true,
4147 false);
4148
4149 // Do full GC and retry runtime call one final time.
4150 Failure* failure = Failure::InternalError();
4151 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4152 GenerateCore(masm,
4153 &throw_normal_exception,
4154 &throw_termination_exception,
4155 &throw_out_of_memory_exception,
4156 true,
4157 true);
4158
4159 __ bind(&throw_out_of_memory_exception);
4160 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4161
4162 __ bind(&throw_termination_exception);
4163 GenerateThrowUncatchable(masm, TERMINATION);
4164
4165 __ bind(&throw_normal_exception);
4166 GenerateThrowTOS(masm);
4167}
4168
4169
4170void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4171 Label invoke, exit;
4172#ifdef ENABLE_LOGGING_AND_PROFILING
4173 Label not_outermost_js, not_outermost_js_2;
4174#endif
4175
4176 // Setup frame.
4177 __ push(ebp);
4178 __ mov(ebp, Operand(esp));
4179
4180 // Push marker in two places.
4181 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4182 __ push(Immediate(Smi::FromInt(marker))); // context slot
4183 __ push(Immediate(Smi::FromInt(marker))); // function slot
4184 // Save callee-saved registers (C calling conventions).
4185 __ push(edi);
4186 __ push(esi);
4187 __ push(ebx);
4188
4189 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004190 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004191 __ push(Operand::StaticVariable(c_entry_fp));
4192
4193#ifdef ENABLE_LOGGING_AND_PROFILING
4194 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004195 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4196 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004197 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4198 __ j(not_equal, &not_outermost_js);
4199 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4200 __ bind(&not_outermost_js);
4201#endif
4202
4203 // Call a faked try-block that does the invoke.
4204 __ call(&invoke);
4205
4206 // Caught exception: Store result (exception) in the pending
4207 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004208 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4209 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004210 __ mov(Operand::StaticVariable(pending_exception), eax);
4211 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4212 __ jmp(&exit);
4213
4214 // Invoke: Link this frame into the handler chain.
4215 __ bind(&invoke);
4216 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4217
4218 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004219 ExternalReference the_hole_location =
4220 ExternalReference::the_hole_value_location(masm->isolate());
4221 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004222 __ mov(Operand::StaticVariable(pending_exception), edx);
4223
4224 // Fake a receiver (NULL).
4225 __ push(Immediate(0)); // receiver
4226
4227 // Invoke the function by calling through JS entry trampoline
4228 // builtin and pop the faked function when we return. Notice that we
4229 // cannot store a reference to the trampoline code directly in this
4230 // stub, because the builtin stubs may not have been generated yet.
4231 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004232 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004233 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004234 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004235 __ mov(edx, Immediate(construct_entry));
4236 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004237 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004238 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004239 __ mov(edx, Immediate(entry));
4240 }
4241 __ mov(edx, Operand(edx, 0)); // deref address
4242 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4243 __ call(Operand(edx));
4244
4245 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004246 __ pop(Operand::StaticVariable(ExternalReference(
4247 Isolate::k_handler_address,
4248 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004249 // Pop next_sp.
4250 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
4251
4252#ifdef ENABLE_LOGGING_AND_PROFILING
4253 // If current EBP value is the same as js_entry_sp value, it means that
4254 // the current function is the outermost.
4255 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
4256 __ j(not_equal, &not_outermost_js_2);
4257 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4258 __ bind(&not_outermost_js_2);
4259#endif
4260
4261 // Restore the top frame descriptor from the stack.
4262 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004263 __ pop(Operand::StaticVariable(ExternalReference(
4264 Isolate::k_c_entry_fp_address,
4265 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004266
4267 // Restore callee-saved registers (C calling conventions).
4268 __ pop(ebx);
4269 __ pop(esi);
4270 __ pop(edi);
4271 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4272
4273 // Restore frame pointer and return.
4274 __ pop(ebp);
4275 __ ret(0);
4276}
4277
4278
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004279// Generate stub code for instanceof.
4280// This code can patch a call site inlined cache of the instance of check,
4281// which looks like this.
4282//
4283// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4284// 75 0a jne <some near label>
4285// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4286//
4287// If call site patching is requested the stack will have the delta from the
4288// return address to the cmp instruction just below the return address. This
4289// also means that call site patching can only take place with arguments in
4290// registers. TOS looks like this when call site patching is requested
4291//
4292// esp[0] : return address
4293// esp[4] : delta from return address to cmp instruction
4294//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004295void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004296 // Call site inlining and patching implies arguments in registers.
4297 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4298
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004299 // Fixed register usage throughout the stub.
4300 Register object = eax; // Object (lhs).
4301 Register map = ebx; // Map of the object.
4302 Register function = edx; // Function (rhs).
4303 Register prototype = edi; // Prototype of the function.
4304 Register scratch = ecx;
4305
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004306 // Constants describing the call site code to patch.
4307 static const int kDeltaToCmpImmediate = 2;
4308 static const int kDeltaToMov = 8;
4309 static const int kDeltaToMovImmediate = 9;
4310 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4311 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4312 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4313
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004314 ExternalReference roots_address =
4315 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004316
4317 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4318 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4319
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004320 // Get the object and function - they are always both needed.
4321 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004322 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004323 __ mov(object, Operand(esp, 2 * kPointerSize));
4324 __ mov(function, Operand(esp, 1 * kPointerSize));
4325 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004326
4327 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004328 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004329 __ j(zero, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004330 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004331
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004332 // If there is a call site cache don't look in the global cache, but do the
4333 // real lookup and update the call site cache.
4334 if (!HasCallSiteInlineCheck()) {
4335 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004336 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004337 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4338 __ cmp(function,
4339 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004340 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004341 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4342 __ cmp(map, Operand::StaticArray(
4343 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004344 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004345 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4346 __ mov(eax, Operand::StaticArray(
4347 scratch, times_pointer_size, roots_address));
4348 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4349 __ bind(&miss);
4350 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004351
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004352 // Get the prototype of the function.
4353 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004354
4355 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004356 __ test(prototype, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004357 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004358 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004359
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004360 // Update the global instanceof or call site inlined cache with the current
4361 // map and function. The cached answer will be set when it is known below.
4362 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004363 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4364 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4365 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4366 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4367 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004368 } else {
4369 // The constants for the code patching are based on no push instructions
4370 // at the call site.
4371 ASSERT(HasArgsInRegisters());
4372 // Get return address and delta to inlined map check.
4373 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4374 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4375 if (FLAG_debug_code) {
4376 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4377 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4378 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4379 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4380 }
4381 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4382 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004383
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004384 // Loop through the prototype chain of the object looking for the function
4385 // prototype.
4386 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004387 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004388 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004389 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004390 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004391 Factory* factory = masm->isolate()->factory();
4392 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004393 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004394 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4395 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004396 __ jmp(&loop);
4397
4398 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004399 if (!HasCallSiteInlineCheck()) {
4400 __ Set(eax, Immediate(0));
4401 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4402 __ mov(Operand::StaticArray(scratch,
4403 times_pointer_size, roots_address), eax);
4404 } else {
4405 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004406 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004407 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4408 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4409 if (FLAG_debug_code) {
4410 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4411 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4412 }
4413 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4414 if (!ReturnTrueFalseObject()) {
4415 __ Set(eax, Immediate(0));
4416 }
4417 }
4418 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004419
4420 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004421 if (!HasCallSiteInlineCheck()) {
4422 __ Set(eax, Immediate(Smi::FromInt(1)));
4423 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4424 __ mov(Operand::StaticArray(
4425 scratch, times_pointer_size, roots_address), eax);
4426 } else {
4427 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004428 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004429 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4430 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4431 if (FLAG_debug_code) {
4432 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4433 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4434 }
4435 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4436 if (!ReturnTrueFalseObject()) {
4437 __ Set(eax, Immediate(Smi::FromInt(1)));
4438 }
4439 }
4440 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004441
4442 Label object_not_null, object_not_null_or_smi;
4443 __ bind(&not_js_object);
4444 // Before null, smi and string value checks, check that the rhs is a function
4445 // as for a non-function rhs an exception needs to be thrown.
4446 __ test(function, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004447 __ j(zero, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004448 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004449 __ j(not_equal, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004450
4451 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004452 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004453 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004454 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004455 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004456
4457 __ bind(&object_not_null);
4458 // Smi values is not instance of anything.
4459 __ test(object, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004460 __ j(not_zero, &object_not_null_or_smi);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004461 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004462 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004463
4464 __ bind(&object_not_null_or_smi);
4465 // String values is not instance of anything.
4466 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4467 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004468 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004469 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004470
4471 // Slow-case: Go through the JavaScript implementation.
4472 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004473 if (!ReturnTrueFalseObject()) {
4474 // Tail call the builtin which returns 0 or 1.
4475 if (HasArgsInRegisters()) {
4476 // Push arguments below return address.
4477 __ pop(scratch);
4478 __ push(object);
4479 __ push(function);
4480 __ push(scratch);
4481 }
4482 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4483 } else {
4484 // Call the builtin and convert 0/1 to true/false.
4485 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004486 __ push(object);
4487 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004488 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4489 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004490 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004491 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004492 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004493 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004494 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004495 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004496 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004497 __ bind(&done);
4498 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004499 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004500}
4501
4502
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004503Register InstanceofStub::left() { return eax; }
4504
4505
4506Register InstanceofStub::right() { return edx; }
4507
4508
ricow@chromium.org65fae842010-08-25 15:26:24 +00004509int CompareStub::MinorKey() {
4510 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4511 // stubs the never NaN NaN condition is only taken into account if the
4512 // condition is equals.
4513 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4514 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4515 return ConditionField::encode(static_cast<unsigned>(cc_))
4516 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4517 | StrictField::encode(strict_)
4518 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004519 | IncludeNumberCompareField::encode(include_number_compare_)
4520 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004521}
4522
4523
4524// Unfortunately you have to run without snapshots to see most of these
4525// names in the profile since most compare stubs end up in the snapshot.
4526const char* CompareStub::GetName() {
4527 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4528
4529 if (name_ != NULL) return name_;
4530 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004531 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4532 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004533 if (name_ == NULL) return "OOM";
4534
4535 const char* cc_name;
4536 switch (cc_) {
4537 case less: cc_name = "LT"; break;
4538 case greater: cc_name = "GT"; break;
4539 case less_equal: cc_name = "LE"; break;
4540 case greater_equal: cc_name = "GE"; break;
4541 case equal: cc_name = "EQ"; break;
4542 case not_equal: cc_name = "NE"; break;
4543 default: cc_name = "UnknownCondition"; break;
4544 }
4545
4546 const char* strict_name = "";
4547 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4548 strict_name = "_STRICT";
4549 }
4550
4551 const char* never_nan_nan_name = "";
4552 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4553 never_nan_nan_name = "_NO_NAN";
4554 }
4555
4556 const char* include_number_compare_name = "";
4557 if (!include_number_compare_) {
4558 include_number_compare_name = "_NO_NUMBER";
4559 }
4560
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004561 const char* include_smi_compare_name = "";
4562 if (!include_smi_compare_) {
4563 include_smi_compare_name = "_NO_SMI";
4564 }
4565
ricow@chromium.org65fae842010-08-25 15:26:24 +00004566 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004567 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004568 cc_name,
4569 strict_name,
4570 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004571 include_number_compare_name,
4572 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004573 return name_;
4574}
4575
4576
4577// -------------------------------------------------------------------------
4578// StringCharCodeAtGenerator
4579
4580void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4581 Label flat_string;
4582 Label ascii_string;
4583 Label got_char_code;
4584
4585 // If the receiver is a smi trigger the non-string case.
4586 STATIC_ASSERT(kSmiTag == 0);
4587 __ test(object_, Immediate(kSmiTagMask));
4588 __ j(zero, receiver_not_string_);
4589
4590 // Fetch the instance type of the receiver into result register.
4591 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4592 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4593 // If the receiver is not a string trigger the non-string case.
4594 __ test(result_, Immediate(kIsNotStringMask));
4595 __ j(not_zero, receiver_not_string_);
4596
4597 // If the index is non-smi trigger the non-smi case.
4598 STATIC_ASSERT(kSmiTag == 0);
4599 __ test(index_, Immediate(kSmiTagMask));
4600 __ j(not_zero, &index_not_smi_);
4601
4602 // Put smi-tagged index into scratch register.
4603 __ mov(scratch_, index_);
4604 __ bind(&got_smi_index_);
4605
4606 // Check for index out of range.
4607 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4608 __ j(above_equal, index_out_of_range_);
4609
4610 // We need special handling for non-flat strings.
4611 STATIC_ASSERT(kSeqStringTag == 0);
4612 __ test(result_, Immediate(kStringRepresentationMask));
4613 __ j(zero, &flat_string);
4614
4615 // Handle non-flat strings.
4616 __ test(result_, Immediate(kIsConsStringMask));
4617 __ j(zero, &call_runtime_);
4618
4619 // ConsString.
4620 // Check whether the right hand side is the empty string (i.e. if
4621 // this is really a flat string in a cons string). If that is not
4622 // the case we would rather go to the runtime system now to flatten
4623 // the string.
4624 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004625 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004626 __ j(not_equal, &call_runtime_);
4627 // Get the first of the two strings and load its instance type.
4628 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4629 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4630 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4631 // If the first cons component is also non-flat, then go to runtime.
4632 STATIC_ASSERT(kSeqStringTag == 0);
4633 __ test(result_, Immediate(kStringRepresentationMask));
4634 __ j(not_zero, &call_runtime_);
4635
4636 // Check for 1-byte or 2-byte string.
4637 __ bind(&flat_string);
4638 STATIC_ASSERT(kAsciiStringTag != 0);
4639 __ test(result_, Immediate(kStringEncodingMask));
4640 __ j(not_zero, &ascii_string);
4641
4642 // 2-byte string.
4643 // Load the 2-byte character code into the result register.
4644 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4645 __ movzx_w(result_, FieldOperand(object_,
4646 scratch_, times_1, // Scratch is smi-tagged.
4647 SeqTwoByteString::kHeaderSize));
4648 __ jmp(&got_char_code);
4649
4650 // ASCII string.
4651 // Load the byte into the result register.
4652 __ bind(&ascii_string);
4653 __ SmiUntag(scratch_);
4654 __ movzx_b(result_, FieldOperand(object_,
4655 scratch_, times_1,
4656 SeqAsciiString::kHeaderSize));
4657 __ bind(&got_char_code);
4658 __ SmiTag(result_);
4659 __ bind(&exit_);
4660}
4661
4662
4663void StringCharCodeAtGenerator::GenerateSlow(
4664 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4665 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4666
4667 // Index is not a smi.
4668 __ bind(&index_not_smi_);
4669 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004670 __ CheckMap(index_,
4671 masm->isolate()->factory()->heap_number_map(),
4672 index_not_number_,
4673 true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004674 call_helper.BeforeCall(masm);
4675 __ push(object_);
4676 __ push(index_);
4677 __ push(index_); // Consumed by runtime conversion function.
4678 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4679 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4680 } else {
4681 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4682 // NumberToSmi discards numbers that are not exact integers.
4683 __ CallRuntime(Runtime::kNumberToSmi, 1);
4684 }
4685 if (!scratch_.is(eax)) {
4686 // Save the conversion result before the pop instructions below
4687 // have a chance to overwrite it.
4688 __ mov(scratch_, eax);
4689 }
4690 __ pop(index_);
4691 __ pop(object_);
4692 // Reload the instance type.
4693 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4694 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4695 call_helper.AfterCall(masm);
4696 // If index is still not a smi, it must be out of range.
4697 STATIC_ASSERT(kSmiTag == 0);
4698 __ test(scratch_, Immediate(kSmiTagMask));
4699 __ j(not_zero, index_out_of_range_);
4700 // Otherwise, return to the fast path.
4701 __ jmp(&got_smi_index_);
4702
4703 // Call runtime. We get here when the receiver is a string and the
4704 // index is a number, but the code of getting the actual character
4705 // is too complex (e.g., when the string needs to be flattened).
4706 __ bind(&call_runtime_);
4707 call_helper.BeforeCall(masm);
4708 __ push(object_);
4709 __ push(index_);
4710 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4711 if (!result_.is(eax)) {
4712 __ mov(result_, eax);
4713 }
4714 call_helper.AfterCall(masm);
4715 __ jmp(&exit_);
4716
4717 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4718}
4719
4720
4721// -------------------------------------------------------------------------
4722// StringCharFromCodeGenerator
4723
4724void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4725 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4726 STATIC_ASSERT(kSmiTag == 0);
4727 STATIC_ASSERT(kSmiShiftSize == 0);
4728 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4729 __ test(code_,
4730 Immediate(kSmiTagMask |
4731 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004732 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004733
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004734 Factory* factory = masm->isolate()->factory();
4735 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004736 STATIC_ASSERT(kSmiTag == 0);
4737 STATIC_ASSERT(kSmiTagSize == 1);
4738 STATIC_ASSERT(kSmiShiftSize == 0);
4739 // At this point code register contains smi tagged ascii char code.
4740 __ mov(result_, FieldOperand(result_,
4741 code_, times_half_pointer_size,
4742 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004743 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004744 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004745 __ bind(&exit_);
4746}
4747
4748
4749void StringCharFromCodeGenerator::GenerateSlow(
4750 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4751 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4752
4753 __ bind(&slow_case_);
4754 call_helper.BeforeCall(masm);
4755 __ push(code_);
4756 __ CallRuntime(Runtime::kCharFromCode, 1);
4757 if (!result_.is(eax)) {
4758 __ mov(result_, eax);
4759 }
4760 call_helper.AfterCall(masm);
4761 __ jmp(&exit_);
4762
4763 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4764}
4765
4766
4767// -------------------------------------------------------------------------
4768// StringCharAtGenerator
4769
4770void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4771 char_code_at_generator_.GenerateFast(masm);
4772 char_from_code_generator_.GenerateFast(masm);
4773}
4774
4775
4776void StringCharAtGenerator::GenerateSlow(
4777 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4778 char_code_at_generator_.GenerateSlow(masm, call_helper);
4779 char_from_code_generator_.GenerateSlow(masm, call_helper);
4780}
4781
4782
4783void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004784 Label string_add_runtime, call_builtin;
4785 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004786
4787 // Load the two arguments.
4788 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4789 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4790
4791 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004792 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004793 __ test(eax, Immediate(kSmiTagMask));
4794 __ j(zero, &string_add_runtime);
4795 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
4796 __ j(above_equal, &string_add_runtime);
4797
4798 // First argument is a a string, test second.
4799 __ test(edx, Immediate(kSmiTagMask));
4800 __ j(zero, &string_add_runtime);
4801 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
4802 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004803 } else {
4804 // Here at least one of the arguments is definitely a string.
4805 // We convert the one that is not known to be a string.
4806 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4807 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4808 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
4809 &call_builtin);
4810 builtin_id = Builtins::STRING_ADD_RIGHT;
4811 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4812 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4813 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
4814 &call_builtin);
4815 builtin_id = Builtins::STRING_ADD_LEFT;
4816 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004817 }
4818
4819 // Both arguments are strings.
4820 // eax: first string
4821 // edx: second string
4822 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004823 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004824 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
4825 STATIC_ASSERT(kSmiTag == 0);
4826 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004827 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004828 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004829 Counters* counters = masm->isolate()->counters();
4830 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004831 __ ret(2 * kPointerSize);
4832 __ bind(&second_not_zero_length);
4833 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
4834 STATIC_ASSERT(kSmiTag == 0);
4835 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004836 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004837 // First string is empty, result is second string which is in edx.
4838 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004839 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004840 __ ret(2 * kPointerSize);
4841
4842 // Both strings are non-empty.
4843 // eax: first string
4844 // ebx: length of first string as a smi
4845 // ecx: length of second string as a smi
4846 // edx: second string
4847 // Look at the length of the result of adding the two strings.
4848 Label string_add_flat_result, longer_than_two;
4849 __ bind(&both_not_zero_length);
4850 __ add(ebx, Operand(ecx));
4851 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
4852 // Handle exceptionally long strings in the runtime system.
4853 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004854 // Use the symbol table when adding two one character strings, as it
4855 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004856 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
4857 __ j(not_equal, &longer_than_two);
4858
4859 // Check that both strings are non-external ascii strings.
4860 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
4861 &string_add_runtime);
4862
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004863 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004864 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4865 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4866
4867 // Try to lookup two character string in symbol table. If it is not found
4868 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004869 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004870 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004871 masm, ebx, ecx, eax, edx, edi,
4872 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004873 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004874 __ ret(2 * kPointerSize);
4875
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004876 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004877 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004878 // Reload the arguments.
4879 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4880 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4881 // Get the two characters forming the new string.
4882 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4883 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4884 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004885 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004886 __ AllocateAsciiString(eax, // Result.
4887 2, // Length.
4888 edi, // Scratch 1.
4889 edx, // Scratch 2.
4890 &string_add_runtime);
4891 // Pack both characters in ebx.
4892 __ shl(ecx, kBitsPerByte);
4893 __ or_(ebx, Operand(ecx));
4894 // Set the characters in the new string.
4895 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004896 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004897 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004898
4899 __ bind(&longer_than_two);
4900 // Check if resulting string will be flat.
4901 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
4902 __ j(below, &string_add_flat_result);
4903
4904 // If result is not supposed to be flat allocate a cons string object. If both
4905 // strings are ascii the result is an ascii cons string.
4906 Label non_ascii, allocated, ascii_data;
4907 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
4908 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
4909 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
4910 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
4911 __ and_(ecx, Operand(edi));
4912 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4913 __ test(ecx, Immediate(kAsciiStringTag));
4914 __ j(zero, &non_ascii);
4915 __ bind(&ascii_data);
4916 // Allocate an acsii cons string.
4917 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
4918 __ bind(&allocated);
4919 // Fill the fields of the cons string.
4920 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
4921 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
4922 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
4923 Immediate(String::kEmptyHashField));
4924 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
4925 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
4926 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004927 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004928 __ ret(2 * kPointerSize);
4929 __ bind(&non_ascii);
4930 // At least one of the strings is two-byte. Check whether it happens
4931 // to contain only ascii characters.
4932 // ecx: first instance type AND second instance type.
4933 // edi: second instance type.
4934 __ test(ecx, Immediate(kAsciiDataHintMask));
4935 __ j(not_zero, &ascii_data);
4936 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4937 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4938 __ xor_(edi, Operand(ecx));
4939 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4940 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
4941 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
4942 __ j(equal, &ascii_data);
4943 // Allocate a two byte cons string.
4944 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
4945 __ jmp(&allocated);
4946
4947 // Handle creating a flat result. First check that both strings are not
4948 // external strings.
4949 // eax: first string
4950 // ebx: length of resulting flat string as a smi
4951 // edx: second string
4952 __ bind(&string_add_flat_result);
4953 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4954 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4955 __ and_(ecx, kStringRepresentationMask);
4956 __ cmp(ecx, kExternalStringTag);
4957 __ j(equal, &string_add_runtime);
4958 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4959 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
4960 __ and_(ecx, kStringRepresentationMask);
4961 __ cmp(ecx, kExternalStringTag);
4962 __ j(equal, &string_add_runtime);
4963 // Now check if both strings are ascii strings.
4964 // eax: first string
4965 // ebx: length of resulting flat string as a smi
4966 // edx: second string
4967 Label non_ascii_string_add_flat_result;
4968 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
4969 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4970 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
4971 __ j(zero, &non_ascii_string_add_flat_result);
4972 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
4973 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
4974 __ j(zero, &string_add_runtime);
4975
ricow@chromium.org65fae842010-08-25 15:26:24 +00004976 // Both strings are ascii strings. As they are short they are both flat.
4977 // ebx: length of resulting flat string as a smi
4978 __ SmiUntag(ebx);
4979 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
4980 // eax: result string
4981 __ mov(ecx, eax);
4982 // Locate first character of result.
4983 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4984 // Load first argument and locate first character.
4985 __ mov(edx, Operand(esp, 2 * kPointerSize));
4986 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4987 __ SmiUntag(edi);
4988 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4989 // eax: result string
4990 // ecx: first character of result
4991 // edx: first char of first argument
4992 // edi: length of first argument
4993 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
4994 // Load second argument and locate first character.
4995 __ mov(edx, Operand(esp, 1 * kPointerSize));
4996 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
4997 __ SmiUntag(edi);
4998 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4999 // eax: result string
5000 // ecx: next character of result
5001 // edx: first char of second argument
5002 // edi: length of second argument
5003 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005004 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005005 __ ret(2 * kPointerSize);
5006
5007 // Handle creating a flat two byte result.
5008 // eax: first string - known to be two byte
5009 // ebx: length of resulting flat string as a smi
5010 // edx: second string
5011 __ bind(&non_ascii_string_add_flat_result);
5012 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5013 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5014 __ j(not_zero, &string_add_runtime);
5015 // Both strings are two byte strings. As they are short they are both
5016 // flat.
5017 __ SmiUntag(ebx);
5018 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5019 // eax: result string
5020 __ mov(ecx, eax);
5021 // Locate first character of result.
5022 __ add(Operand(ecx),
5023 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5024 // Load first argument and locate first character.
5025 __ mov(edx, Operand(esp, 2 * kPointerSize));
5026 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5027 __ SmiUntag(edi);
5028 __ add(Operand(edx),
5029 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5030 // eax: result string
5031 // ecx: first character of result
5032 // edx: first char of first argument
5033 // edi: length of first argument
5034 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5035 // Load second argument and locate first character.
5036 __ mov(edx, Operand(esp, 1 * kPointerSize));
5037 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5038 __ SmiUntag(edi);
5039 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5040 // eax: result string
5041 // ecx: next character of result
5042 // edx: first char of second argument
5043 // edi: length of second argument
5044 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005045 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005046 __ ret(2 * kPointerSize);
5047
5048 // Just jump to runtime to add the two strings.
5049 __ bind(&string_add_runtime);
5050 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005051
5052 if (call_builtin.is_linked()) {
5053 __ bind(&call_builtin);
5054 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5055 }
5056}
5057
5058
5059void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5060 int stack_offset,
5061 Register arg,
5062 Register scratch1,
5063 Register scratch2,
5064 Register scratch3,
5065 Label* slow) {
5066 // First check if the argument is already a string.
5067 Label not_string, done;
5068 __ test(arg, Immediate(kSmiTagMask));
5069 __ j(zero, &not_string);
5070 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5071 __ j(below, &done);
5072
5073 // Check the number to string cache.
5074 Label not_cached;
5075 __ bind(&not_string);
5076 // Puts the cached result into scratch1.
5077 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5078 arg,
5079 scratch1,
5080 scratch2,
5081 scratch3,
5082 false,
5083 &not_cached);
5084 __ mov(arg, scratch1);
5085 __ mov(Operand(esp, stack_offset), arg);
5086 __ jmp(&done);
5087
5088 // Check if the argument is a safe string wrapper.
5089 __ bind(&not_cached);
5090 __ test(arg, Immediate(kSmiTagMask));
5091 __ j(zero, slow);
5092 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5093 __ j(not_equal, slow);
5094 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5095 1 << Map::kStringWrapperSafeForDefaultValueOf);
5096 __ j(zero, slow);
5097 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5098 __ mov(Operand(esp, stack_offset), arg);
5099
5100 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005101}
5102
5103
5104void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5105 Register dest,
5106 Register src,
5107 Register count,
5108 Register scratch,
5109 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005110 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005111 __ bind(&loop);
5112 // This loop just copies one character at a time, as it is only used for very
5113 // short strings.
5114 if (ascii) {
5115 __ mov_b(scratch, Operand(src, 0));
5116 __ mov_b(Operand(dest, 0), scratch);
5117 __ add(Operand(src), Immediate(1));
5118 __ add(Operand(dest), Immediate(1));
5119 } else {
5120 __ mov_w(scratch, Operand(src, 0));
5121 __ mov_w(Operand(dest, 0), scratch);
5122 __ add(Operand(src), Immediate(2));
5123 __ add(Operand(dest), Immediate(2));
5124 }
5125 __ sub(Operand(count), Immediate(1));
5126 __ j(not_zero, &loop);
5127}
5128
5129
5130void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5131 Register dest,
5132 Register src,
5133 Register count,
5134 Register scratch,
5135 bool ascii) {
5136 // Copy characters using rep movs of doublewords.
5137 // The destination is aligned on a 4 byte boundary because we are
5138 // copying to the beginning of a newly allocated string.
5139 ASSERT(dest.is(edi)); // rep movs destination
5140 ASSERT(src.is(esi)); // rep movs source
5141 ASSERT(count.is(ecx)); // rep movs count
5142 ASSERT(!scratch.is(dest));
5143 ASSERT(!scratch.is(src));
5144 ASSERT(!scratch.is(count));
5145
5146 // Nothing to do for zero characters.
5147 Label done;
5148 __ test(count, Operand(count));
5149 __ j(zero, &done);
5150
5151 // Make count the number of bytes to copy.
5152 if (!ascii) {
5153 __ shl(count, 1);
5154 }
5155
5156 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005157 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005158 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005159 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005160
5161 // Copy from edi to esi using rep movs instruction.
5162 __ mov(scratch, count);
5163 __ sar(count, 2); // Number of doublewords to copy.
5164 __ cld();
5165 __ rep_movs();
5166
5167 // Find number of bytes left.
5168 __ mov(count, scratch);
5169 __ and_(count, 3);
5170
5171 // Check if there are more bytes to copy.
5172 __ bind(&last_bytes);
5173 __ test(count, Operand(count));
5174 __ j(zero, &done);
5175
5176 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005177 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005178 __ bind(&loop);
5179 __ mov_b(scratch, Operand(src, 0));
5180 __ mov_b(Operand(dest, 0), scratch);
5181 __ add(Operand(src), Immediate(1));
5182 __ add(Operand(dest), Immediate(1));
5183 __ sub(Operand(count), Immediate(1));
5184 __ j(not_zero, &loop);
5185
5186 __ bind(&done);
5187}
5188
5189
5190void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5191 Register c1,
5192 Register c2,
5193 Register scratch1,
5194 Register scratch2,
5195 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005196 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005197 Label* not_found) {
5198 // Register scratch3 is the general scratch register in this function.
5199 Register scratch = scratch3;
5200
5201 // Make sure that both characters are not digits as such strings has a
5202 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005203 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005204 __ mov(scratch, c1);
5205 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5206 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005207 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005208 __ mov(scratch, c2);
5209 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5210 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005211 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005212
5213 __ bind(&not_array_index);
5214 // Calculate the two character string hash.
5215 Register hash = scratch1;
5216 GenerateHashInit(masm, hash, c1, scratch);
5217 GenerateHashAddCharacter(masm, hash, c2, scratch);
5218 GenerateHashGetHash(masm, hash, scratch);
5219
5220 // Collect the two characters in a register.
5221 Register chars = c1;
5222 __ shl(c2, kBitsPerByte);
5223 __ or_(chars, Operand(c2));
5224
5225 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5226 // hash: hash of two character string.
5227
5228 // Load the symbol table.
5229 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005230 ExternalReference roots_address =
5231 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005232 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5233 __ mov(symbol_table,
5234 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5235
5236 // Calculate capacity mask from the symbol table capacity.
5237 Register mask = scratch2;
5238 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5239 __ SmiUntag(mask);
5240 __ sub(Operand(mask), Immediate(1));
5241
5242 // Registers
5243 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5244 // hash: hash of two character string
5245 // symbol_table: symbol table
5246 // mask: capacity mask
5247 // scratch: -
5248
5249 // Perform a number of probes in the symbol table.
5250 static const int kProbes = 4;
5251 Label found_in_symbol_table;
5252 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5253 for (int i = 0; i < kProbes; i++) {
5254 // Calculate entry in symbol table.
5255 __ mov(scratch, hash);
5256 if (i > 0) {
5257 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5258 }
5259 __ and_(scratch, Operand(mask));
5260
5261 // Load the entry from the symbol table.
5262 Register candidate = scratch; // Scratch register contains candidate.
5263 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5264 __ mov(candidate,
5265 FieldOperand(symbol_table,
5266 scratch,
5267 times_pointer_size,
5268 SymbolTable::kElementsStartOffset));
5269
5270 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005271 Factory* factory = masm->isolate()->factory();
5272 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005273 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005274 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005275 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005276
5277 // If length is not 2 the string is not a candidate.
5278 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5279 Immediate(Smi::FromInt(2)));
5280 __ j(not_equal, &next_probe[i]);
5281
5282 // As we are out of registers save the mask on the stack and use that
5283 // register as a temporary.
5284 __ push(mask);
5285 Register temp = mask;
5286
5287 // Check that the candidate is a non-external ascii string.
5288 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5289 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5290 __ JumpIfInstanceTypeIsNotSequentialAscii(
5291 temp, temp, &next_probe_pop_mask[i]);
5292
5293 // Check if the two characters match.
5294 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5295 __ and_(temp, 0x0000ffff);
5296 __ cmp(chars, Operand(temp));
5297 __ j(equal, &found_in_symbol_table);
5298 __ bind(&next_probe_pop_mask[i]);
5299 __ pop(mask);
5300 __ bind(&next_probe[i]);
5301 }
5302
5303 // No matching 2 character string found by probing.
5304 __ jmp(not_found);
5305
5306 // Scratch register contains result when we fall through to here.
5307 Register result = scratch;
5308 __ bind(&found_in_symbol_table);
5309 __ pop(mask); // Pop saved mask from the stack.
5310 if (!result.is(eax)) {
5311 __ mov(eax, result);
5312 }
5313}
5314
5315
5316void StringHelper::GenerateHashInit(MacroAssembler* masm,
5317 Register hash,
5318 Register character,
5319 Register scratch) {
5320 // hash = character + (character << 10);
5321 __ mov(hash, character);
5322 __ shl(hash, 10);
5323 __ add(hash, Operand(character));
5324 // hash ^= hash >> 6;
5325 __ mov(scratch, hash);
5326 __ sar(scratch, 6);
5327 __ xor_(hash, Operand(scratch));
5328}
5329
5330
5331void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5332 Register hash,
5333 Register character,
5334 Register scratch) {
5335 // hash += character;
5336 __ add(hash, Operand(character));
5337 // hash += hash << 10;
5338 __ mov(scratch, hash);
5339 __ shl(scratch, 10);
5340 __ add(hash, Operand(scratch));
5341 // hash ^= hash >> 6;
5342 __ mov(scratch, hash);
5343 __ sar(scratch, 6);
5344 __ xor_(hash, Operand(scratch));
5345}
5346
5347
5348void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5349 Register hash,
5350 Register scratch) {
5351 // hash += hash << 3;
5352 __ mov(scratch, hash);
5353 __ shl(scratch, 3);
5354 __ add(hash, Operand(scratch));
5355 // hash ^= hash >> 11;
5356 __ mov(scratch, hash);
5357 __ sar(scratch, 11);
5358 __ xor_(hash, Operand(scratch));
5359 // hash += hash << 15;
5360 __ mov(scratch, hash);
5361 __ shl(scratch, 15);
5362 __ add(hash, Operand(scratch));
5363
5364 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005365 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005366 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005367 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005368 __ mov(hash, Immediate(27));
5369 __ bind(&hash_not_zero);
5370}
5371
5372
5373void SubStringStub::Generate(MacroAssembler* masm) {
5374 Label runtime;
5375
5376 // Stack frame on entry.
5377 // esp[0]: return address
5378 // esp[4]: to
5379 // esp[8]: from
5380 // esp[12]: string
5381
5382 // Make sure first argument is a string.
5383 __ mov(eax, Operand(esp, 3 * kPointerSize));
5384 STATIC_ASSERT(kSmiTag == 0);
5385 __ test(eax, Immediate(kSmiTagMask));
5386 __ j(zero, &runtime);
5387 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5388 __ j(NegateCondition(is_string), &runtime);
5389
5390 // eax: string
5391 // ebx: instance type
5392
5393 // Calculate length of sub string using the smi values.
5394 Label result_longer_than_two;
5395 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
5396 __ test(ecx, Immediate(kSmiTagMask));
5397 __ j(not_zero, &runtime);
5398 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
5399 __ test(edx, Immediate(kSmiTagMask));
5400 __ j(not_zero, &runtime);
5401 __ sub(ecx, Operand(edx));
5402 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5403 Label return_eax;
5404 __ j(equal, &return_eax);
5405 // Special handling of sub-strings of length 1 and 2. One character strings
5406 // are handled in the runtime system (looked up in the single character
5407 // cache). Two character strings are looked for in the symbol cache.
5408 __ SmiUntag(ecx); // Result length is no longer smi.
5409 __ cmp(ecx, 2);
5410 __ j(greater, &result_longer_than_two);
5411 __ j(less, &runtime);
5412
5413 // Sub string of length 2 requested.
5414 // eax: string
5415 // ebx: instance type
5416 // ecx: sub string length (value is 2)
5417 // edx: from index (smi)
5418 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5419
5420 // Get the two characters forming the sub string.
5421 __ SmiUntag(edx); // From index is no longer smi.
5422 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5423 __ movzx_b(ecx,
5424 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5425
5426 // Try to lookup two character string in symbol table.
5427 Label make_two_character_string;
5428 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005429 masm, ebx, ecx, eax, edx, edi,
5430 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005431 __ ret(3 * kPointerSize);
5432
5433 __ bind(&make_two_character_string);
5434 // Setup registers for allocating the two character string.
5435 __ mov(eax, Operand(esp, 3 * kPointerSize));
5436 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5437 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5438 __ Set(ecx, Immediate(2));
5439
5440 __ bind(&result_longer_than_two);
5441 // eax: string
5442 // ebx: instance type
5443 // ecx: result string length
5444 // Check for flat ascii string
5445 Label non_ascii_flat;
5446 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5447
5448 // Allocate the result.
5449 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5450
5451 // eax: result string
5452 // ecx: result string length
5453 __ mov(edx, esi); // esi used by following code.
5454 // Locate first character of result.
5455 __ mov(edi, eax);
5456 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5457 // Load string argument and locate character of sub string start.
5458 __ mov(esi, Operand(esp, 3 * kPointerSize));
5459 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5460 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5461 __ SmiUntag(ebx);
5462 __ add(esi, Operand(ebx));
5463
5464 // eax: result string
5465 // ecx: result length
5466 // edx: original value of esi
5467 // edi: first character of result
5468 // esi: character of sub string start
5469 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5470 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005471 Counters* counters = masm->isolate()->counters();
5472 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005473 __ ret(3 * kPointerSize);
5474
5475 __ bind(&non_ascii_flat);
5476 // eax: string
5477 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5478 // ecx: result string length
5479 // Check for flat two byte string
5480 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5481 __ j(not_equal, &runtime);
5482
5483 // Allocate the result.
5484 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5485
5486 // eax: result string
5487 // ecx: result string length
5488 __ mov(edx, esi); // esi used by following code.
5489 // Locate first character of result.
5490 __ mov(edi, eax);
5491 __ add(Operand(edi),
5492 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5493 // Load string argument and locate character of sub string start.
5494 __ mov(esi, Operand(esp, 3 * kPointerSize));
5495 __ add(Operand(esi),
5496 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5497 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5498 // As from is a smi it is 2 times the value which matches the size of a two
5499 // byte character.
5500 STATIC_ASSERT(kSmiTag == 0);
5501 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5502 __ add(esi, Operand(ebx));
5503
5504 // eax: result string
5505 // ecx: result length
5506 // edx: original value of esi
5507 // edi: first character of result
5508 // esi: character of sub string start
5509 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5510 __ mov(esi, edx); // Restore esi.
5511
5512 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005513 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005514 __ ret(3 * kPointerSize);
5515
5516 // Just jump to runtime to create the sub string.
5517 __ bind(&runtime);
5518 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5519}
5520
5521
lrn@chromium.org1c092762011-05-09 09:42:16 +00005522void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5523 Register left,
5524 Register right,
5525 Register scratch1,
5526 Register scratch2) {
5527 Register length = scratch1;
5528
5529 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005530 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005531 __ mov(length, FieldOperand(left, String::kLengthOffset));
5532 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005533 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005534 __ bind(&strings_not_equal);
5535 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5536 __ ret(0);
5537
5538 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005539 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005540 __ bind(&check_zero_length);
5541 STATIC_ASSERT(kSmiTag == 0);
5542 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005543 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005544 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5545 __ ret(0);
5546
5547 // Compare characters.
5548 __ bind(&compare_chars);
5549 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005550 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005551
5552 // Characters are equal.
5553 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5554 __ ret(0);
5555}
5556
5557
ricow@chromium.org65fae842010-08-25 15:26:24 +00005558void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5559 Register left,
5560 Register right,
5561 Register scratch1,
5562 Register scratch2,
5563 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005564 Counters* counters = masm->isolate()->counters();
5565 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005566
5567 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005568 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005569 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5570 __ mov(scratch3, scratch1);
5571 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5572
5573 Register length_delta = scratch3;
5574
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005575 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005576 // Right string is shorter. Change scratch1 to be length of right string.
5577 __ sub(scratch1, Operand(length_delta));
5578 __ bind(&left_shorter);
5579
5580 Register min_length = scratch1;
5581
5582 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005583 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005584 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005585 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005586
lrn@chromium.org1c092762011-05-09 09:42:16 +00005587 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005588 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005589 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005590 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005591
5592 // Compare lengths - strings up to min-length are equal.
5593 __ bind(&compare_lengths);
5594 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005595 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005596
5597 // Result is EQUAL.
5598 STATIC_ASSERT(EQUAL == 0);
5599 STATIC_ASSERT(kSmiTag == 0);
5600 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5601 __ ret(0);
5602
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005603 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005604 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005605 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005606
5607 // Result is LESS.
5608 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5609 __ ret(0);
5610
5611 // Result is GREATER.
5612 __ bind(&result_greater);
5613 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5614 __ ret(0);
5615}
5616
5617
lrn@chromium.org1c092762011-05-09 09:42:16 +00005618void StringCompareStub::GenerateAsciiCharsCompareLoop(
5619 MacroAssembler* masm,
5620 Register left,
5621 Register right,
5622 Register length,
5623 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005624 Label* chars_not_equal,
5625 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005626 // Change index to run from -length to -1 by adding length to string
5627 // start. This means that loop ends when index reaches zero, which
5628 // doesn't need an additional compare.
5629 __ SmiUntag(length);
5630 __ lea(left,
5631 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5632 __ lea(right,
5633 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5634 __ neg(length);
5635 Register index = length; // index = -length;
5636
5637 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005638 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005639 __ bind(&loop);
5640 __ mov_b(scratch, Operand(left, index, times_1, 0));
5641 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005642 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005643 __ add(Operand(index), Immediate(1));
5644 __ j(not_zero, &loop);
5645}
5646
5647
ricow@chromium.org65fae842010-08-25 15:26:24 +00005648void StringCompareStub::Generate(MacroAssembler* masm) {
5649 Label runtime;
5650
5651 // Stack frame on entry.
5652 // esp[0]: return address
5653 // esp[4]: right string
5654 // esp[8]: left string
5655
5656 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5657 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5658
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005659 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005660 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005661 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005662 STATIC_ASSERT(EQUAL == 0);
5663 STATIC_ASSERT(kSmiTag == 0);
5664 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005665 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005666 __ ret(2 * kPointerSize);
5667
5668 __ bind(&not_same);
5669
5670 // Check that both objects are sequential ascii strings.
5671 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5672
5673 // Compare flat ascii strings.
5674 // Drop arguments from the stack.
5675 __ pop(ecx);
5676 __ add(Operand(esp), Immediate(2 * kPointerSize));
5677 __ push(ecx);
5678 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5679
5680 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5681 // tagged as a small integer.
5682 __ bind(&runtime);
5683 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5684}
5685
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005686
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005687void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5688 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005689 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005690 __ mov(ecx, Operand(edx));
5691 __ or_(ecx, Operand(eax));
5692 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005693 __ j(not_zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005694
5695 if (GetCondition() == equal) {
5696 // For equality we do not care about the sign of the result.
5697 __ sub(eax, Operand(edx));
5698 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005699 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005700 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005701 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005702 // Correct sign of result in case of overflow.
5703 __ not_(edx);
5704 __ bind(&done);
5705 __ mov(eax, edx);
5706 }
5707 __ ret(0);
5708
5709 __ bind(&miss);
5710 GenerateMiss(masm);
5711}
5712
5713
5714void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5715 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5716
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005717 Label generic_stub;
5718 Label unordered;
5719 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005720 __ mov(ecx, Operand(edx));
5721 __ and_(ecx, Operand(eax));
5722 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005723 __ j(zero, &generic_stub, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005724
5725 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005726 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005727 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005728 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005729
5730 // Inlining the double comparison and falling back to the general compare
5731 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005732 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005733 CpuFeatures::Scope scope1(SSE2);
5734 CpuFeatures::Scope scope2(CMOV);
5735
5736 // Load left and right operand
5737 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5738 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5739
5740 // Compare operands
5741 __ ucomisd(xmm0, xmm1);
5742
5743 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005744 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005745
5746 // Return a result of -1, 0, or 1, based on EFLAGS.
5747 // Performing mov, because xor would destroy the flag register.
5748 __ mov(eax, 0); // equal
5749 __ mov(ecx, Immediate(Smi::FromInt(1)));
5750 __ cmov(above, eax, Operand(ecx));
5751 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5752 __ cmov(below, eax, Operand(ecx));
5753 __ ret(0);
5754
5755 __ bind(&unordered);
5756 }
5757
5758 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5759 __ bind(&generic_stub);
5760 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5761
5762 __ bind(&miss);
5763 GenerateMiss(masm);
5764}
5765
5766
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005767void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5768 ASSERT(state_ == CompareIC::SYMBOLS);
5769 ASSERT(GetCondition() == equal);
5770
5771 // Registers containing left and right operands respectively.
5772 Register left = edx;
5773 Register right = eax;
5774 Register tmp1 = ecx;
5775 Register tmp2 = ebx;
5776
5777 // Check that both operands are heap objects.
5778 Label miss;
5779 __ mov(tmp1, Operand(left));
5780 STATIC_ASSERT(kSmiTag == 0);
5781 __ and_(tmp1, Operand(right));
5782 __ test(tmp1, Immediate(kSmiTagMask));
5783 __ j(zero, &miss, Label::kNear);
5784
5785 // Check that both operands are symbols.
5786 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5787 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5788 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5789 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5790 STATIC_ASSERT(kSymbolTag != 0);
5791 __ and_(tmp1, Operand(tmp2));
5792 __ test(tmp1, Immediate(kIsSymbolMask));
5793 __ j(zero, &miss, Label::kNear);
5794
5795 // Symbols are compared by identity.
5796 Label done;
5797 __ cmp(left, Operand(right));
5798 // Make sure eax is non-zero. At this point input operands are
5799 // guaranteed to be non-zero.
5800 ASSERT(right.is(eax));
5801 __ j(not_equal, &done, Label::kNear);
5802 STATIC_ASSERT(EQUAL == 0);
5803 STATIC_ASSERT(kSmiTag == 0);
5804 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5805 __ bind(&done);
5806 __ ret(0);
5807
5808 __ bind(&miss);
5809 GenerateMiss(masm);
5810}
5811
5812
lrn@chromium.org1c092762011-05-09 09:42:16 +00005813void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5814 ASSERT(state_ == CompareIC::STRINGS);
5815 ASSERT(GetCondition() == equal);
5816 Label miss;
5817
5818 // Registers containing left and right operands respectively.
5819 Register left = edx;
5820 Register right = eax;
5821 Register tmp1 = ecx;
5822 Register tmp2 = ebx;
5823 Register tmp3 = edi;
5824
5825 // Check that both operands are heap objects.
5826 __ mov(tmp1, Operand(left));
5827 STATIC_ASSERT(kSmiTag == 0);
5828 __ and_(tmp1, Operand(right));
5829 __ test(tmp1, Immediate(kSmiTagMask));
5830 __ j(zero, &miss);
5831
5832 // Check that both operands are strings. This leaves the instance
5833 // types loaded in tmp1 and tmp2.
5834 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5835 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5836 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5837 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5838 __ mov(tmp3, tmp1);
5839 STATIC_ASSERT(kNotStringTag != 0);
5840 __ or_(tmp3, Operand(tmp2));
5841 __ test(tmp3, Immediate(kIsNotStringMask));
5842 __ j(not_zero, &miss);
5843
5844 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005845 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005846 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005847 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005848 STATIC_ASSERT(EQUAL == 0);
5849 STATIC_ASSERT(kSmiTag == 0);
5850 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5851 __ ret(0);
5852
5853 // Handle not identical strings.
5854 __ bind(&not_same);
5855
5856 // Check that both strings are symbols. If they are, we're done
5857 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005858 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005859 STATIC_ASSERT(kSymbolTag != 0);
5860 __ and_(tmp1, Operand(tmp2));
5861 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005862 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005863 // Make sure eax is non-zero. At this point input operands are
5864 // guaranteed to be non-zero.
5865 ASSERT(right.is(eax));
5866 __ ret(0);
5867
5868 // Check that both strings are sequential ASCII.
5869 Label runtime;
5870 __ bind(&do_compare);
5871 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5872
5873 // Compare flat ASCII strings. Returns when done.
5874 StringCompareStub::GenerateFlatAsciiStringEquals(
5875 masm, left, right, tmp1, tmp2);
5876
5877 // Handle more complex cases in runtime.
5878 __ bind(&runtime);
5879 __ pop(tmp1); // Return address.
5880 __ push(left);
5881 __ push(right);
5882 __ push(tmp1);
5883 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5884
5885 __ bind(&miss);
5886 GenerateMiss(masm);
5887}
5888
5889
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005890void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5891 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005892 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005893 __ mov(ecx, Operand(edx));
5894 __ and_(ecx, Operand(eax));
5895 __ test(ecx, Immediate(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005896 __ j(zero, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005897
5898 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005899 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005900 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005901 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005902
5903 ASSERT(GetCondition() == equal);
5904 __ sub(eax, Operand(edx));
5905 __ ret(0);
5906
5907 __ bind(&miss);
5908 GenerateMiss(masm);
5909}
5910
5911
5912void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
5913 // Save the registers.
5914 __ pop(ecx);
5915 __ push(edx);
5916 __ push(eax);
5917 __ push(ecx);
5918
5919 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005920 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
5921 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005922 __ EnterInternalFrame();
5923 __ push(edx);
5924 __ push(eax);
5925 __ push(Immediate(Smi::FromInt(op_)));
5926 __ CallExternalReference(miss, 3);
5927 __ LeaveInternalFrame();
5928
5929 // Compute the entry point of the rewritten stub.
5930 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
5931
5932 // Restore registers.
5933 __ pop(ecx);
5934 __ pop(eax);
5935 __ pop(edx);
5936 __ push(ecx);
5937
5938 // Do a tail call to the rewritten stub.
5939 __ jmp(Operand(edi));
5940}
5941
5942
lrn@chromium.org1c092762011-05-09 09:42:16 +00005943// Helper function used to check that the dictionary doesn't contain
5944// the property. This function may return false negatives, so miss_label
5945// must always call a backup property check that is complete.
5946// This function is safe to call if the receiver has fast properties.
5947// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005948MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
5949 MacroAssembler* masm,
5950 Label* miss,
5951 Label* done,
5952 Register properties,
5953 String* name,
5954 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005955 ASSERT(name->IsSymbol());
5956
5957 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5958 // not equal to the name and kProbes-th slot is not used (its name is the
5959 // undefined value), it guarantees the hash table doesn't contain the
5960 // property. It's true even if some slots represent deleted properties
5961 // (their names are the null value).
5962 for (int i = 0; i < kInlinedProbes; i++) {
5963 // Compute the masked index: (hash + i + i * i) & mask.
5964 Register index = r0;
5965 // Capacity is smi 2^n.
5966 __ mov(index, FieldOperand(properties, kCapacityOffset));
5967 __ dec(index);
5968 __ and_(Operand(index),
5969 Immediate(Smi::FromInt(name->Hash() +
5970 StringDictionary::GetProbeOffset(i))));
5971
5972 // Scale the index by multiplying by the entry size.
5973 ASSERT(StringDictionary::kEntrySize == 3);
5974 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
5975 Register entity_name = r0;
5976 // Having undefined at this place means the name is not contained.
5977 ASSERT_EQ(kSmiTagSize, 1);
5978 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
5979 kElementsStartOffset - kHeapObjectTag));
5980 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005981 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005982
5983 // Stop if found the property.
5984 __ cmp(entity_name, Handle<String>(name));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005985 __ j(equal, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005986
5987 // Check if the entry name is not a symbol.
5988 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
5989 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
5990 kIsSymbolMask);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005991 __ j(zero, miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005992 }
5993
5994 StringDictionaryLookupStub stub(properties,
5995 r0,
5996 r0,
5997 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
5998 __ push(Immediate(Handle<Object>(name)));
5999 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006000 MaybeObject* result = masm->TryCallStub(&stub);
6001 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006002 __ test(r0, Operand(r0));
6003 __ j(not_zero, miss);
6004 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006005 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006006}
6007
6008
6009// Probe the string dictionary in the |elements| register. Jump to the
6010// |done| label if a property with the given name is found leaving the
6011// index into the dictionary in |r0|. Jump to the |miss| label
6012// otherwise.
6013void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6014 Label* miss,
6015 Label* done,
6016 Register elements,
6017 Register name,
6018 Register r0,
6019 Register r1) {
6020 // Assert that name contains a string.
6021 if (FLAG_debug_code) __ AbortIfNotString(name);
6022
6023 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6024 __ shr(r1, kSmiTagSize); // convert smi to int
6025 __ dec(r1);
6026
6027 // Generate an unrolled loop that performs a few probes before
6028 // giving up. Measurements done on Gmail indicate that 2 probes
6029 // cover ~93% of loads from dictionaries.
6030 for (int i = 0; i < kInlinedProbes; i++) {
6031 // Compute the masked index: (hash + i + i * i) & mask.
6032 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6033 __ shr(r0, String::kHashShift);
6034 if (i > 0) {
6035 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6036 }
6037 __ and_(r0, Operand(r1));
6038
6039 // Scale the index by multiplying by the entry size.
6040 ASSERT(StringDictionary::kEntrySize == 3);
6041 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6042
6043 // Check if the key is identical to the name.
6044 __ cmp(name, Operand(elements,
6045 r0,
6046 times_4,
6047 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006048 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00006049 }
6050
6051 StringDictionaryLookupStub stub(elements,
6052 r1,
6053 r0,
6054 POSITIVE_LOOKUP);
6055 __ push(name);
6056 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6057 __ shr(r0, String::kHashShift);
6058 __ push(r0);
6059 __ CallStub(&stub);
6060
6061 __ test(r1, Operand(r1));
6062 __ j(zero, miss);
6063 __ jmp(done);
6064}
6065
6066
6067void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6068 // Stack frame on entry:
6069 // esp[0 * kPointerSize]: return address.
6070 // esp[1 * kPointerSize]: key's hash.
6071 // esp[2 * kPointerSize]: key.
6072 // Registers:
6073 // dictionary_: StringDictionary to probe.
6074 // result_: used as scratch.
6075 // index_: will hold an index of entry if lookup is successful.
6076 // might alias with result_.
6077 // Returns:
6078 // result_ is zero if lookup failed, non zero otherwise.
6079
6080 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6081
6082 Register scratch = result_;
6083
6084 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6085 __ dec(scratch);
6086 __ SmiUntag(scratch);
6087 __ push(scratch);
6088
6089 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6090 // not equal to the name and kProbes-th slot is not used (its name is the
6091 // undefined value), it guarantees the hash table doesn't contain the
6092 // property. It's true even if some slots represent deleted properties
6093 // (their names are the null value).
6094 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6095 // Compute the masked index: (hash + i + i * i) & mask.
6096 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6097 if (i > 0) {
6098 __ add(Operand(scratch),
6099 Immediate(StringDictionary::GetProbeOffset(i)));
6100 }
6101 __ and_(scratch, Operand(esp, 0));
6102
6103 // Scale the index by multiplying by the entry size.
6104 ASSERT(StringDictionary::kEntrySize == 3);
6105 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6106
6107 // Having undefined at this place means the name is not contained.
6108 ASSERT_EQ(kSmiTagSize, 1);
6109 __ mov(scratch, Operand(dictionary_,
6110 index_,
6111 times_pointer_size,
6112 kElementsStartOffset - kHeapObjectTag));
6113 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6114 __ j(equal, &not_in_dictionary);
6115
6116 // Stop if found the property.
6117 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6118 __ j(equal, &in_dictionary);
6119
6120 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6121 // If we hit a non symbol key during negative lookup
6122 // we have to bailout as this key might be equal to the
6123 // key we are looking for.
6124
6125 // Check if the entry name is not a symbol.
6126 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6127 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6128 kIsSymbolMask);
6129 __ j(zero, &maybe_in_dictionary);
6130 }
6131 }
6132
6133 __ bind(&maybe_in_dictionary);
6134 // If we are doing negative lookup then probing failure should be
6135 // treated as a lookup success. For positive lookup probing failure
6136 // should be treated as lookup failure.
6137 if (mode_ == POSITIVE_LOOKUP) {
6138 __ mov(result_, Immediate(0));
6139 __ Drop(1);
6140 __ ret(2 * kPointerSize);
6141 }
6142
6143 __ bind(&in_dictionary);
6144 __ mov(result_, Immediate(1));
6145 __ Drop(1);
6146 __ ret(2 * kPointerSize);
6147
6148 __ bind(&not_in_dictionary);
6149 __ mov(result_, Immediate(0));
6150 __ Drop(1);
6151 __ ret(2 * kPointerSize);
6152}
6153
6154
ricow@chromium.org65fae842010-08-25 15:26:24 +00006155#undef __
6156
6157} } // namespace v8::internal
6158
6159#endif // V8_TARGET_ARCH_IA32