blob: fa7d08cf848bf0c5b4fca17981a7fe2d272038b1 [file] [log] [blame]
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_IA32)
31
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000032#include "code-stubs.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000033#include "bootstrapper.h"
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000034#include "jsregexp.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000035#include "isolate.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
37
38namespace v8 {
39namespace internal {
40
41#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +000042
43void ToNumberStub::Generate(MacroAssembler* masm) {
44 // The ToNumber stub takes one argument in eax.
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.
334 static void LoadAsIntegers(MacroAssembler* masm,
335 TypeInfo type_info,
336 bool use_sse3,
337 Label* operand_conversion_failure);
338 static void LoadNumbersAsIntegers(MacroAssembler* masm,
339 TypeInfo type_info,
340 bool use_sse3,
341 Label* operand_conversion_failure);
342 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
343 bool use_sse3,
344 Label* operand_conversion_failure);
345
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000346 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
347 // operands are pushed on the stack, and that their conversions to int32
348 // are in eax and ecx. Checks that the original numbers were in the int32
349 // range.
350 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
351 bool use_sse3,
352 Label* not_int32);
353
354 // Assumes that operands are smis or heap numbers and loads them
355 // into xmm0 and xmm1. Operands are in edx and eax.
ricow@chromium.org65fae842010-08-25 15:26:24 +0000356 // Leaves operands unchanged.
357 static void LoadSSE2Operands(MacroAssembler* masm);
358
359 // Test if operands are numbers (smi or HeapNumber objects), and load
360 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
361 // either operand is not a number. Operands are in edx and eax.
362 // Leaves operands unchanged.
363 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
364
365 // Similar to LoadSSE2Operands but assumes that both operands are smis.
366 // Expects operands in edx, eax.
367 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000368
369 // Checks that the two floating point numbers loaded into xmm0 and xmm1
370 // have int32 values.
371 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
372 Label* non_int32,
373 Register scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000374};
375
376
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000377// Get the integer part of a heap number. Surprisingly, all this bit twiddling
378// is faster than using the built-in instructions on floating point registers.
379// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
380// trashed registers.
381static void IntegerConvert(MacroAssembler* masm,
382 Register source,
383 TypeInfo type_info,
384 bool use_sse3,
385 Label* conversion_failure) {
386 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
387 Label done, right_exponent, normal_exponent;
388 Register scratch = ebx;
389 Register scratch2 = edi;
390 if (type_info.IsInteger32() && CpuFeatures::IsSupported(SSE2)) {
391 CpuFeatures::Scope scope(SSE2);
392 __ cvttsd2si(ecx, FieldOperand(source, HeapNumber::kValueOffset));
393 return;
394 }
395 if (!type_info.IsInteger32() || !use_sse3) {
396 // Get exponent word.
397 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
398 // Get exponent alone in scratch2.
399 __ mov(scratch2, scratch);
400 __ and_(scratch2, HeapNumber::kExponentMask);
401 }
402 if (use_sse3) {
403 CpuFeatures::Scope scope(SSE3);
404 if (!type_info.IsInteger32()) {
405 // Check whether the exponent is too big for a 64 bit signed integer.
406 static const uint32_t kTooBigExponent =
407 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
408 __ cmp(Operand(scratch2), Immediate(kTooBigExponent));
409 __ j(greater_equal, conversion_failure);
410 }
411 // Load x87 register with heap number.
412 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
413 // Reserve space for 64 bit answer.
414 __ sub(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
415 // Do conversion, which cannot fail because we checked the exponent.
416 __ fisttp_d(Operand(esp, 0));
417 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
418 __ add(Operand(esp), Immediate(sizeof(uint64_t))); // Nolint.
419 } else {
420 // Load ecx with zero. We use this either for the final shift or
421 // for the answer.
422 __ xor_(ecx, Operand(ecx));
423 // Check whether the exponent matches a 32 bit signed int that cannot be
424 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
425 // exponent is 30 (biased). This is the exponent that we are fastest at and
426 // also the highest exponent we can handle here.
427 const uint32_t non_smi_exponent =
428 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
429 __ cmp(Operand(scratch2), Immediate(non_smi_exponent));
430 // If we have a match of the int32-but-not-Smi exponent then skip some
431 // logic.
432 __ j(equal, &right_exponent);
433 // If the exponent is higher than that then go to slow case. This catches
434 // numbers that don't fit in a signed int32, infinities and NaNs.
435 __ j(less, &normal_exponent);
436
437 {
438 // Handle a big exponent. The only reason we have this code is that the
439 // >>> operator has a tendency to generate numbers with an exponent of 31.
440 const uint32_t big_non_smi_exponent =
441 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
442 __ cmp(Operand(scratch2), Immediate(big_non_smi_exponent));
443 __ j(not_equal, conversion_failure);
444 // We have the big exponent, typically from >>>. This means the number is
445 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
446 __ mov(scratch2, scratch);
447 __ and_(scratch2, HeapNumber::kMantissaMask);
448 // Put back the implicit 1.
449 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
450 // Shift up the mantissa bits to take up the space the exponent used to
451 // take. We just orred in the implicit bit so that took care of one and
452 // we want to use the full unsigned range so we subtract 1 bit from the
453 // shift distance.
454 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
455 __ shl(scratch2, big_shift_distance);
456 // Get the second half of the double.
457 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
458 // Shift down 21 bits to get the most significant 11 bits or the low
459 // mantissa word.
460 __ shr(ecx, 32 - big_shift_distance);
461 __ or_(ecx, Operand(scratch2));
462 // We have the answer in ecx, but we may need to negate it.
463 __ test(scratch, Operand(scratch));
464 __ j(positive, &done);
465 __ neg(ecx);
466 __ jmp(&done);
467 }
468
469 __ bind(&normal_exponent);
470 // Exponent word in scratch, exponent part of exponent word in scratch2.
471 // Zero in ecx.
472 // We know the exponent is smaller than 30 (biased). If it is less than
473 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
474 // it rounds to zero.
475 const uint32_t zero_exponent =
476 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
477 __ sub(Operand(scratch2), Immediate(zero_exponent));
478 // ecx already has a Smi zero.
479 __ j(less, &done);
480
481 // We have a shifted exponent between 0 and 30 in scratch2.
482 __ shr(scratch2, HeapNumber::kExponentShift);
483 __ mov(ecx, Immediate(30));
484 __ sub(ecx, Operand(scratch2));
485
486 __ bind(&right_exponent);
487 // Here ecx is the shift, scratch is the exponent word.
488 // Get the top bits of the mantissa.
489 __ and_(scratch, HeapNumber::kMantissaMask);
490 // Put back the implicit 1.
491 __ or_(scratch, 1 << HeapNumber::kExponentShift);
492 // Shift up the mantissa bits to take up the space the exponent used to
493 // take. We have kExponentShift + 1 significant bits int he low end of the
494 // word. Shift them to the top bits.
495 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
496 __ shl(scratch, shift_distance);
497 // Get the second half of the double. For some exponents we don't
498 // actually need this because the bits get shifted out again, but
499 // it's probably slower to test than just to do it.
500 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
501 // Shift down 22 bits to get the most significant 10 bits or the low
502 // mantissa word.
503 __ shr(scratch2, 32 - shift_distance);
504 __ or_(scratch2, Operand(scratch));
505 // Move down according to the exponent.
506 __ shr_cl(scratch2);
507 // Now the unsigned answer is in scratch2. We need to move it to ecx and
508 // we may need to fix the sign.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000509 Label negative;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000510 __ xor_(ecx, Operand(ecx));
511 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000512 __ j(greater, &negative, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000513 __ mov(ecx, scratch2);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000514 __ jmp(&done, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000515 __ bind(&negative);
516 __ sub(ecx, Operand(scratch2));
517 __ bind(&done);
518 }
519}
520
521
522Handle<Code> GetTypeRecordingUnaryOpStub(int key,
523 TRUnaryOpIC::TypeInfo type_info) {
524 TypeRecordingUnaryOpStub stub(key, type_info);
525 return stub.GetCode();
526}
527
528
529const char* TypeRecordingUnaryOpStub::GetName() {
530 if (name_ != NULL) return name_;
531 const int kMaxNameLength = 100;
532 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
533 kMaxNameLength);
534 if (name_ == NULL) return "OOM";
535 const char* op_name = Token::Name(op_);
536 const char* overwrite_name = NULL; // Make g++ happy.
537 switch (mode_) {
538 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
539 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
540 }
541
542 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
543 "TypeRecordingUnaryOpStub_%s_%s_%s",
544 op_name,
545 overwrite_name,
546 TRUnaryOpIC::GetName(operand_type_));
547 return name_;
548}
549
550
551// TODO(svenpanne): Use virtual functions instead of switch.
552void TypeRecordingUnaryOpStub::Generate(MacroAssembler* masm) {
553 switch (operand_type_) {
554 case TRUnaryOpIC::UNINITIALIZED:
555 GenerateTypeTransition(masm);
556 break;
557 case TRUnaryOpIC::SMI:
558 GenerateSmiStub(masm);
559 break;
560 case TRUnaryOpIC::HEAP_NUMBER:
561 GenerateHeapNumberStub(masm);
562 break;
563 case TRUnaryOpIC::GENERIC:
564 GenerateGenericStub(masm);
565 break;
566 }
567}
568
569
570void TypeRecordingUnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
571 __ pop(ecx); // Save return address.
572 __ push(eax);
573 // the argument is now on top.
574 // Push this stub's key. Although the operation and the type info are
575 // encoded into the key, the encoding is opaque, so push them too.
576 __ push(Immediate(Smi::FromInt(MinorKey())));
577 __ push(Immediate(Smi::FromInt(op_)));
578 __ push(Immediate(Smi::FromInt(operand_type_)));
579
580 __ push(ecx); // Push return address.
581
582 // Patch the caller to an appropriate specialized stub and return the
583 // operation result to the caller of the stub.
584 __ TailCallExternalReference(
585 ExternalReference(IC_Utility(IC::kTypeRecordingUnaryOp_Patch),
586 masm->isolate()),
587 4,
588 1);
589}
590
591
592// TODO(svenpanne): Use virtual functions instead of switch.
593void TypeRecordingUnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
594 switch (op_) {
595 case Token::SUB:
596 GenerateSmiStubSub(masm);
597 break;
598 case Token::BIT_NOT:
599 GenerateSmiStubBitNot(masm);
600 break;
601 default:
602 UNREACHABLE();
603 }
604}
605
606
607void TypeRecordingUnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000608 Label non_smi, undo, slow;
609 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
610 Label::kNear, Label::kNear, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000611 __ bind(&undo);
612 GenerateSmiCodeUndo(masm);
613 __ bind(&non_smi);
614 __ bind(&slow);
615 GenerateTypeTransition(masm);
616}
617
618
619void TypeRecordingUnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000620 Label non_smi;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000621 GenerateSmiCodeBitNot(masm, &non_smi);
622 __ bind(&non_smi);
623 GenerateTypeTransition(masm);
624}
625
626
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000627void TypeRecordingUnaryOpStub::GenerateSmiCodeSub(
628 MacroAssembler* masm, Label* non_smi, Label* undo, Label* slow,
629 Label::Distance non_smi_near, Label::Distance undo_near,
630 Label::Distance slow_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000631 // Check whether the value is a smi.
632 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000633 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000634
635 // We can't handle -0 with smis, so use a type transition for that case.
636 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000637 __ j(zero, slow, slow_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000638
639 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
640 __ mov(edx, Operand(eax));
641 __ Set(eax, Immediate(0));
642 __ sub(eax, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000643 __ j(overflow, undo, undo_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000644 __ ret(0);
645}
646
647
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000648void TypeRecordingUnaryOpStub::GenerateSmiCodeBitNot(
649 MacroAssembler* masm,
650 Label* non_smi,
651 Label::Distance non_smi_near) {
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000652 // Check whether the value is a smi.
653 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000654 __ j(not_zero, non_smi, non_smi_near);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000655
656 // Flip bits and revert inverted smi-tag.
657 __ not_(eax);
658 __ and_(eax, ~kSmiTagMask);
659 __ ret(0);
660}
661
662
663void TypeRecordingUnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
664 __ mov(eax, Operand(edx));
665}
666
667
668// TODO(svenpanne): Use virtual functions instead of switch.
669void TypeRecordingUnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
670 switch (op_) {
671 case Token::SUB:
672 GenerateHeapNumberStubSub(masm);
673 break;
674 case Token::BIT_NOT:
675 GenerateHeapNumberStubBitNot(masm);
676 break;
677 default:
678 UNREACHABLE();
679 }
680}
681
682
683void TypeRecordingUnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000684 Label non_smi, undo, slow;
685 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000686 __ bind(&non_smi);
687 GenerateHeapNumberCodeSub(masm, &slow);
688 __ bind(&undo);
689 GenerateSmiCodeUndo(masm);
690 __ bind(&slow);
691 GenerateTypeTransition(masm);
692}
693
694
695void TypeRecordingUnaryOpStub::GenerateHeapNumberStubBitNot(
696 MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000697 Label non_smi, slow;
698 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000699 __ bind(&non_smi);
700 GenerateHeapNumberCodeBitNot(masm, &slow);
701 __ bind(&slow);
702 GenerateTypeTransition(masm);
703}
704
705
706void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
707 Label* slow) {
708 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
709 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
710 __ j(not_equal, slow);
711
712 if (mode_ == UNARY_OVERWRITE) {
713 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
714 Immediate(HeapNumber::kSignMask)); // Flip sign.
715 } else {
716 __ mov(edx, Operand(eax));
717 // edx: operand
718
719 Label slow_allocate_heapnumber, heapnumber_allocated;
720 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
721 __ jmp(&heapnumber_allocated);
722
723 __ bind(&slow_allocate_heapnumber);
724 __ EnterInternalFrame();
725 __ push(edx);
726 __ CallRuntime(Runtime::kNumberAlloc, 0);
727 __ pop(edx);
728 __ LeaveInternalFrame();
729
730 __ bind(&heapnumber_allocated);
731 // eax: allocated 'empty' number
732 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
733 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
734 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
735 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
736 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
737 }
738 __ ret(0);
739}
740
741
742void TypeRecordingUnaryOpStub::GenerateHeapNumberCodeBitNot(
743 MacroAssembler* masm,
744 Label* slow) {
745 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
746 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
747 __ j(not_equal, slow);
748
749 // Convert the heap number in eax to an untagged integer in ecx.
750 IntegerConvert(masm, eax, TypeInfo::Unknown(), CpuFeatures::IsSupported(SSE3),
751 slow);
752
753 // Do the bitwise operation and check if the result fits in a smi.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000754 Label try_float;
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000755 __ not_(ecx);
756 __ cmp(ecx, 0xc0000000);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000757 __ j(sign, &try_float, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000758
759 // Tag the result as a smi and we're done.
760 STATIC_ASSERT(kSmiTagSize == 1);
761 __ lea(eax, Operand(ecx, times_2, kSmiTag));
762 __ ret(0);
763
764 // Try to store the result in a heap number.
765 __ bind(&try_float);
766 if (mode_ == UNARY_NO_OVERWRITE) {
767 Label slow_allocate_heapnumber, heapnumber_allocated;
768 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
769 __ jmp(&heapnumber_allocated);
770
771 __ bind(&slow_allocate_heapnumber);
772 __ EnterInternalFrame();
773 __ push(ecx);
774 __ CallRuntime(Runtime::kNumberAlloc, 0);
775 __ pop(ecx);
776 __ LeaveInternalFrame();
777
778 __ bind(&heapnumber_allocated);
779 }
780 if (CpuFeatures::IsSupported(SSE2)) {
781 CpuFeatures::Scope use_sse2(SSE2);
782 __ cvtsi2sd(xmm0, Operand(ecx));
783 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
784 } else {
785 __ push(ecx);
786 __ fild_s(Operand(esp, 0));
787 __ pop(ecx);
788 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
789 }
790 __ ret(0);
791}
792
793
794// TODO(svenpanne): Use virtual functions instead of switch.
795void TypeRecordingUnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
796 switch (op_) {
797 case Token::SUB:
798 GenerateGenericStubSub(masm);
799 break;
800 case Token::BIT_NOT:
801 GenerateGenericStubBitNot(masm);
802 break;
803 default:
804 UNREACHABLE();
805 }
806}
807
808
809void TypeRecordingUnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000810 Label non_smi, undo, slow;
811 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000812 __ bind(&non_smi);
813 GenerateHeapNumberCodeSub(masm, &slow);
814 __ bind(&undo);
815 GenerateSmiCodeUndo(masm);
816 __ bind(&slow);
817 GenerateGenericCodeFallback(masm);
818}
819
820
821void TypeRecordingUnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000822 Label non_smi, slow;
823 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000824 __ bind(&non_smi);
825 GenerateHeapNumberCodeBitNot(masm, &slow);
826 __ bind(&slow);
827 GenerateGenericCodeFallback(masm);
828}
829
830
831void TypeRecordingUnaryOpStub::GenerateGenericCodeFallback(
832 MacroAssembler* masm) {
833 // Handle the slow case by jumping to the corresponding JavaScript builtin.
834 __ pop(ecx); // pop return address.
835 __ push(eax);
836 __ push(ecx); // push return address
837 switch (op_) {
838 case Token::SUB:
839 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
840 break;
841 case Token::BIT_NOT:
842 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
843 break;
844 default:
845 UNREACHABLE();
846 }
847}
848
849
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000850Handle<Code> GetTypeRecordingBinaryOpStub(int key,
851 TRBinaryOpIC::TypeInfo type_info,
852 TRBinaryOpIC::TypeInfo result_type_info) {
853 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
854 return stub.GetCode();
855}
856
857
858void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
859 __ pop(ecx); // Save return address.
860 __ push(edx);
861 __ push(eax);
862 // Left and right arguments are now on top.
863 // Push this stub's key. Although the operation and the type info are
864 // encoded into the key, the encoding is opaque, so push them too.
865 __ push(Immediate(Smi::FromInt(MinorKey())));
866 __ push(Immediate(Smi::FromInt(op_)));
867 __ push(Immediate(Smi::FromInt(operands_type_)));
868
869 __ push(ecx); // Push return address.
870
871 // Patch the caller to an appropriate specialized stub and return the
872 // operation result to the caller of the stub.
873 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000874 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
875 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000876 5,
877 1);
878}
879
880
881// Prepare for a type transition runtime call when the args are already on
882// the stack, under the return address.
883void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
884 MacroAssembler* masm) {
885 __ pop(ecx); // Save return address.
886 // Left and right arguments are already on top of the stack.
887 // Push this stub's key. Although the operation and the type info are
888 // encoded into the key, the encoding is opaque, so push them too.
889 __ push(Immediate(Smi::FromInt(MinorKey())));
890 __ push(Immediate(Smi::FromInt(op_)));
891 __ push(Immediate(Smi::FromInt(operands_type_)));
892
893 __ push(ecx); // Push return address.
894
895 // Patch the caller to an appropriate specialized stub and return the
896 // operation result to the caller of the stub.
897 __ TailCallExternalReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000898 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
899 masm->isolate()),
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000900 5,
901 1);
902}
903
904
905void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
906 switch (operands_type_) {
907 case TRBinaryOpIC::UNINITIALIZED:
908 GenerateTypeTransition(masm);
909 break;
910 case TRBinaryOpIC::SMI:
911 GenerateSmiStub(masm);
912 break;
913 case TRBinaryOpIC::INT32:
914 GenerateInt32Stub(masm);
915 break;
916 case TRBinaryOpIC::HEAP_NUMBER:
917 GenerateHeapNumberStub(masm);
918 break;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000919 case TRBinaryOpIC::ODDBALL:
920 GenerateOddballStub(masm);
921 break;
danno@chromium.org160a7b02011-04-18 15:51:38 +0000922 case TRBinaryOpIC::BOTH_STRING:
923 GenerateBothStringStub(masm);
924 break;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000925 case TRBinaryOpIC::STRING:
926 GenerateStringStub(masm);
927 break;
928 case TRBinaryOpIC::GENERIC:
929 GenerateGeneric(masm);
930 break;
931 default:
932 UNREACHABLE();
933 }
934}
935
936
937const char* TypeRecordingBinaryOpStub::GetName() {
938 if (name_ != NULL) return name_;
939 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000940 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
941 kMaxNameLength);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000942 if (name_ == NULL) return "OOM";
943 const char* op_name = Token::Name(op_);
944 const char* overwrite_name;
945 switch (mode_) {
946 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
947 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
948 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
949 default: overwrite_name = "UnknownOverwrite"; break;
950 }
951
952 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
953 "TypeRecordingBinaryOpStub_%s_%s_%s",
954 op_name,
955 overwrite_name,
956 TRBinaryOpIC::GetName(operands_type_));
957 return name_;
958}
959
960
961void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
962 Label* slow,
963 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
964 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
965 // dividend in eax and edx free for the division. Use eax, ebx for those.
966 Comment load_comment(masm, "-- Load arguments");
967 Register left = edx;
968 Register right = eax;
969 if (op_ == Token::DIV || op_ == Token::MOD) {
970 left = eax;
971 right = ebx;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000972 __ mov(ebx, eax);
973 __ mov(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000974 }
975
976
977 // 2. Prepare the smi check of both operands by oring them together.
978 Comment smi_check_comment(masm, "-- Smi check arguments");
979 Label not_smis;
980 Register combined = ecx;
981 ASSERT(!left.is(combined) && !right.is(combined));
982 switch (op_) {
983 case Token::BIT_OR:
984 // Perform the operation into eax and smi check the result. Preserve
985 // eax in case the result is not a smi.
986 ASSERT(!left.is(ecx) && !right.is(ecx));
987 __ mov(ecx, right);
988 __ or_(right, Operand(left)); // Bitwise or is commutative.
989 combined = right;
990 break;
991
992 case Token::BIT_XOR:
993 case Token::BIT_AND:
994 case Token::ADD:
995 case Token::SUB:
996 case Token::MUL:
997 case Token::DIV:
998 case Token::MOD:
999 __ mov(combined, right);
1000 __ or_(combined, Operand(left));
1001 break;
1002
1003 case Token::SHL:
1004 case Token::SAR:
1005 case Token::SHR:
1006 // Move the right operand into ecx for the shift operation, use eax
1007 // for the smi check register.
1008 ASSERT(!left.is(ecx) && !right.is(ecx));
1009 __ mov(ecx, right);
1010 __ or_(right, Operand(left));
1011 combined = right;
1012 break;
1013
1014 default:
1015 break;
1016 }
1017
1018 // 3. Perform the smi check of the operands.
1019 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
1020 __ test(combined, Immediate(kSmiTagMask));
1021 __ j(not_zero, &not_smis, not_taken);
1022
1023 // 4. Operands are both smis, perform the operation leaving the result in
1024 // eax and check the result if necessary.
1025 Comment perform_smi(masm, "-- Perform smi operation");
1026 Label use_fp_on_smis;
1027 switch (op_) {
1028 case Token::BIT_OR:
1029 // Nothing to do.
1030 break;
1031
1032 case Token::BIT_XOR:
1033 ASSERT(right.is(eax));
1034 __ xor_(right, Operand(left)); // Bitwise xor is commutative.
1035 break;
1036
1037 case Token::BIT_AND:
1038 ASSERT(right.is(eax));
1039 __ and_(right, Operand(left)); // Bitwise and is commutative.
1040 break;
1041
1042 case Token::SHL:
1043 // Remove tags from operands (but keep sign).
1044 __ SmiUntag(left);
1045 __ SmiUntag(ecx);
1046 // Perform the operation.
1047 __ shl_cl(left);
1048 // Check that the *signed* result fits in a smi.
1049 __ cmp(left, 0xc0000000);
1050 __ j(sign, &use_fp_on_smis, not_taken);
1051 // Tag the result and store it in register eax.
1052 __ SmiTag(left);
1053 __ mov(eax, left);
1054 break;
1055
1056 case Token::SAR:
1057 // Remove tags from operands (but keep sign).
1058 __ SmiUntag(left);
1059 __ SmiUntag(ecx);
1060 // Perform the operation.
1061 __ sar_cl(left);
1062 // Tag the result and store it in register eax.
1063 __ SmiTag(left);
1064 __ mov(eax, left);
1065 break;
1066
1067 case Token::SHR:
1068 // Remove tags from operands (but keep sign).
1069 __ SmiUntag(left);
1070 __ SmiUntag(ecx);
1071 // Perform the operation.
1072 __ shr_cl(left);
1073 // Check that the *unsigned* result fits in a smi.
1074 // Neither of the two high-order bits can be set:
1075 // - 0x80000000: high bit would be lost when smi tagging.
1076 // - 0x40000000: this number would convert to negative when
1077 // Smi tagging these two cases can only happen with shifts
1078 // by 0 or 1 when handed a valid smi.
1079 __ test(left, Immediate(0xc0000000));
1080 __ j(not_zero, slow, not_taken);
1081 // Tag the result and store it in register eax.
1082 __ SmiTag(left);
1083 __ mov(eax, left);
1084 break;
1085
1086 case Token::ADD:
1087 ASSERT(right.is(eax));
1088 __ add(right, Operand(left)); // Addition is commutative.
1089 __ j(overflow, &use_fp_on_smis, not_taken);
1090 break;
1091
1092 case Token::SUB:
1093 __ sub(left, Operand(right));
1094 __ j(overflow, &use_fp_on_smis, not_taken);
1095 __ mov(eax, left);
1096 break;
1097
1098 case Token::MUL:
1099 // If the smi tag is 0 we can just leave the tag on one operand.
1100 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1101 // We can't revert the multiplication if the result is not a smi
1102 // so save the right operand.
1103 __ mov(ebx, right);
1104 // Remove tag from one of the operands (but keep sign).
1105 __ SmiUntag(right);
1106 // Do multiplication.
1107 __ imul(right, Operand(left)); // Multiplication is commutative.
1108 __ j(overflow, &use_fp_on_smis, not_taken);
1109 // Check for negative zero result. Use combined = left | right.
1110 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1111 break;
1112
1113 case Token::DIV:
1114 // We can't revert the division if the result is not a smi so
1115 // save the left operand.
1116 __ mov(edi, left);
1117 // Check for 0 divisor.
1118 __ test(right, Operand(right));
1119 __ j(zero, &use_fp_on_smis, not_taken);
1120 // Sign extend left into edx:eax.
1121 ASSERT(left.is(eax));
1122 __ cdq();
1123 // Divide edx:eax by right.
1124 __ idiv(right);
1125 // Check for the corner case of dividing the most negative smi by
1126 // -1. We cannot use the overflow flag, since it is not set by idiv
1127 // instruction.
1128 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1129 __ cmp(eax, 0x40000000);
1130 __ j(equal, &use_fp_on_smis);
1131 // Check for negative zero result. Use combined = left | right.
1132 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1133 // Check that the remainder is zero.
1134 __ test(edx, Operand(edx));
1135 __ j(not_zero, &use_fp_on_smis);
1136 // Tag the result and store it in register eax.
1137 __ SmiTag(eax);
1138 break;
1139
1140 case Token::MOD:
1141 // Check for 0 divisor.
1142 __ test(right, Operand(right));
1143 __ j(zero, &not_smis, not_taken);
1144
1145 // Sign extend left into edx:eax.
1146 ASSERT(left.is(eax));
1147 __ cdq();
1148 // Divide edx:eax by right.
1149 __ idiv(right);
1150 // Check for negative zero result. Use combined = left | right.
1151 __ NegativeZeroTest(edx, combined, slow);
1152 // Move remainder to register eax.
1153 __ mov(eax, edx);
1154 break;
1155
1156 default:
1157 UNREACHABLE();
1158 }
1159
1160 // 5. Emit return of result in eax. Some operations have registers pushed.
1161 switch (op_) {
1162 case Token::ADD:
1163 case Token::SUB:
1164 case Token::MUL:
1165 case Token::DIV:
1166 __ ret(0);
1167 break;
1168 case Token::MOD:
1169 case Token::BIT_OR:
1170 case Token::BIT_AND:
1171 case Token::BIT_XOR:
1172 case Token::SAR:
1173 case Token::SHL:
1174 case Token::SHR:
1175 __ ret(2 * kPointerSize);
1176 break;
1177 default:
1178 UNREACHABLE();
1179 }
1180
1181 // 6. For some operations emit inline code to perform floating point
1182 // operations on known smis (e.g., if the result of the operation
1183 // overflowed the smi range).
1184 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1185 __ bind(&use_fp_on_smis);
1186 switch (op_) {
1187 // Undo the effects of some operations, and some register moves.
1188 case Token::SHL:
1189 // The arguments are saved on the stack, and only used from there.
1190 break;
1191 case Token::ADD:
1192 // Revert right = right + left.
1193 __ sub(right, Operand(left));
1194 break;
1195 case Token::SUB:
1196 // Revert left = left - right.
1197 __ add(left, Operand(right));
1198 break;
1199 case Token::MUL:
1200 // Right was clobbered but a copy is in ebx.
1201 __ mov(right, ebx);
1202 break;
1203 case Token::DIV:
1204 // Left was clobbered but a copy is in edi. Right is in ebx for
1205 // division. They should be in eax, ebx for jump to not_smi.
1206 __ mov(eax, edi);
1207 break;
1208 default:
1209 // No other operators jump to use_fp_on_smis.
1210 break;
1211 }
1212 __ jmp(&not_smis);
1213 } else {
1214 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1215 switch (op_) {
1216 case Token::SHL: {
1217 Comment perform_float(masm, "-- Perform float operation on smis");
1218 __ bind(&use_fp_on_smis);
1219 // Result we want is in left == edx, so we can put the allocated heap
1220 // number in eax.
1221 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1222 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001223 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001224 CpuFeatures::Scope use_sse2(SSE2);
1225 __ cvtsi2sd(xmm0, Operand(left));
1226 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1227 } else {
1228 // It's OK to overwrite the right argument on the stack because we
1229 // are about to return.
1230 __ mov(Operand(esp, 1 * kPointerSize), left);
1231 __ fild_s(Operand(esp, 1 * kPointerSize));
1232 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1233 }
1234 __ ret(2 * kPointerSize);
1235 break;
1236 }
1237
1238 case Token::ADD:
1239 case Token::SUB:
1240 case Token::MUL:
1241 case Token::DIV: {
1242 Comment perform_float(masm, "-- Perform float operation on smis");
1243 __ bind(&use_fp_on_smis);
1244 // Restore arguments to edx, eax.
1245 switch (op_) {
1246 case Token::ADD:
1247 // Revert right = right + left.
1248 __ sub(right, Operand(left));
1249 break;
1250 case Token::SUB:
1251 // Revert left = left - right.
1252 __ add(left, Operand(right));
1253 break;
1254 case Token::MUL:
1255 // Right was clobbered but a copy is in ebx.
1256 __ mov(right, ebx);
1257 break;
1258 case Token::DIV:
1259 // Left was clobbered but a copy is in edi. Right is in ebx for
1260 // division.
1261 __ mov(edx, edi);
1262 __ mov(eax, right);
1263 break;
1264 default: UNREACHABLE();
1265 break;
1266 }
1267 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001268 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001269 CpuFeatures::Scope use_sse2(SSE2);
1270 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1271 switch (op_) {
1272 case Token::ADD: __ addsd(xmm0, xmm1); break;
1273 case Token::SUB: __ subsd(xmm0, xmm1); break;
1274 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1275 case Token::DIV: __ divsd(xmm0, xmm1); break;
1276 default: UNREACHABLE();
1277 }
1278 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1279 } else { // SSE2 not available, use FPU.
1280 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1281 switch (op_) {
1282 case Token::ADD: __ faddp(1); break;
1283 case Token::SUB: __ fsubp(1); break;
1284 case Token::MUL: __ fmulp(1); break;
1285 case Token::DIV: __ fdivp(1); break;
1286 default: UNREACHABLE();
1287 }
1288 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1289 }
1290 __ mov(eax, ecx);
1291 __ ret(0);
1292 break;
1293 }
1294
1295 default:
1296 break;
1297 }
1298 }
1299
1300 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1301 // edx and eax.
1302 Comment done_comment(masm, "-- Enter non-smi code");
1303 __ bind(&not_smis);
1304 switch (op_) {
1305 case Token::BIT_OR:
1306 case Token::SHL:
1307 case Token::SAR:
1308 case Token::SHR:
1309 // Right operand is saved in ecx and eax was destroyed by the smi
1310 // check.
1311 __ mov(eax, ecx);
1312 break;
1313
1314 case Token::DIV:
1315 case Token::MOD:
1316 // Operands are in eax, ebx at this point.
1317 __ mov(edx, eax);
1318 __ mov(eax, ebx);
1319 break;
1320
1321 default:
1322 break;
1323 }
1324}
1325
1326
1327void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1328 Label call_runtime;
1329
1330 switch (op_) {
1331 case Token::ADD:
1332 case Token::SUB:
1333 case Token::MUL:
1334 case Token::DIV:
1335 break;
1336 case Token::MOD:
1337 case Token::BIT_OR:
1338 case Token::BIT_AND:
1339 case Token::BIT_XOR:
1340 case Token::SAR:
1341 case Token::SHL:
1342 case Token::SHR:
1343 GenerateRegisterArgsPush(masm);
1344 break;
1345 default:
1346 UNREACHABLE();
1347 }
1348
1349 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
1350 result_type_ == TRBinaryOpIC::SMI) {
1351 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1352 } else {
1353 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1354 }
1355 __ bind(&call_runtime);
1356 switch (op_) {
1357 case Token::ADD:
1358 case Token::SUB:
1359 case Token::MUL:
1360 case Token::DIV:
1361 GenerateTypeTransition(masm);
1362 break;
1363 case Token::MOD:
1364 case Token::BIT_OR:
1365 case Token::BIT_AND:
1366 case Token::BIT_XOR:
1367 case Token::SAR:
1368 case Token::SHL:
1369 case Token::SHR:
1370 GenerateTypeTransitionWithSavedArgs(masm);
1371 break;
1372 default:
1373 UNREACHABLE();
1374 }
1375}
1376
1377
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001378void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001379 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
1380 ASSERT(op_ == Token::ADD);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001381 // Try to add arguments as strings, otherwise, transition to the generic
1382 // TRBinaryOpIC type.
1383 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001384 GenerateTypeTransition(masm);
1385}
1386
1387
danno@chromium.org160a7b02011-04-18 15:51:38 +00001388void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1389 Label call_runtime;
1390 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
1391 ASSERT(op_ == Token::ADD);
1392 // If both arguments are strings, call the string add stub.
1393 // Otherwise, do a transition.
1394
1395 // Registers containing left and right operands respectively.
1396 Register left = edx;
1397 Register right = eax;
1398
1399 // Test if left operand is a string.
1400 __ test(left, Immediate(kSmiTagMask));
1401 __ j(zero, &call_runtime);
1402 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
1403 __ j(above_equal, &call_runtime);
1404
1405 // Test if right operand is a string.
1406 __ test(right, Immediate(kSmiTagMask));
1407 __ j(zero, &call_runtime);
1408 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
1409 __ j(above_equal, &call_runtime);
1410
1411 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1412 GenerateRegisterArgsPush(masm);
1413 __ TailCallStub(&string_add_stub);
1414
1415 __ bind(&call_runtime);
1416 GenerateTypeTransition(masm);
1417}
1418
1419
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001420void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1421 Label call_runtime;
1422 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
1423
1424 // Floating point case.
1425 switch (op_) {
1426 case Token::ADD:
1427 case Token::SUB:
1428 case Token::MUL:
1429 case Token::DIV: {
1430 Label not_floats;
1431 Label not_int32;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001432 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001433 CpuFeatures::Scope use_sse2(SSE2);
1434 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1435 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1436 switch (op_) {
1437 case Token::ADD: __ addsd(xmm0, xmm1); break;
1438 case Token::SUB: __ subsd(xmm0, xmm1); break;
1439 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1440 case Token::DIV: __ divsd(xmm0, xmm1); break;
1441 default: UNREACHABLE();
1442 }
1443 // Check result type if it is currently Int32.
1444 if (result_type_ <= TRBinaryOpIC::INT32) {
1445 __ cvttsd2si(ecx, Operand(xmm0));
1446 __ cvtsi2sd(xmm2, Operand(ecx));
1447 __ ucomisd(xmm0, xmm2);
1448 __ j(not_zero, &not_int32);
1449 __ j(carry, &not_int32);
1450 }
1451 GenerateHeapResultAllocation(masm, &call_runtime);
1452 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1453 __ ret(0);
1454 } else { // SSE2 not available, use FPU.
1455 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1456 FloatingPointHelper::LoadFloatOperands(
1457 masm,
1458 ecx,
1459 FloatingPointHelper::ARGS_IN_REGISTERS);
1460 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1461 switch (op_) {
1462 case Token::ADD: __ faddp(1); break;
1463 case Token::SUB: __ fsubp(1); break;
1464 case Token::MUL: __ fmulp(1); break;
1465 case Token::DIV: __ fdivp(1); break;
1466 default: UNREACHABLE();
1467 }
1468 Label after_alloc_failure;
1469 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1470 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1471 __ ret(0);
1472 __ bind(&after_alloc_failure);
1473 __ ffree();
1474 __ jmp(&call_runtime);
1475 }
1476
1477 __ bind(&not_floats);
1478 __ bind(&not_int32);
1479 GenerateTypeTransition(masm);
1480 break;
1481 }
1482
1483 case Token::MOD: {
1484 // For MOD we go directly to runtime in the non-smi case.
1485 break;
1486 }
1487 case Token::BIT_OR:
1488 case Token::BIT_AND:
1489 case Token::BIT_XOR:
1490 case Token::SAR:
1491 case Token::SHL:
1492 case Token::SHR: {
1493 GenerateRegisterArgsPush(masm);
1494 Label not_floats;
1495 Label not_int32;
1496 Label non_smi_result;
1497 /* {
1498 CpuFeatures::Scope use_sse2(SSE2);
1499 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1500 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1501 }*/
1502 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1503 use_sse3_,
1504 &not_floats);
1505 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1506 &not_int32);
1507 switch (op_) {
1508 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1509 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1510 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1511 case Token::SAR: __ sar_cl(eax); break;
1512 case Token::SHL: __ shl_cl(eax); break;
1513 case Token::SHR: __ shr_cl(eax); break;
1514 default: UNREACHABLE();
1515 }
1516 if (op_ == Token::SHR) {
1517 // Check if result is non-negative and fits in a smi.
1518 __ test(eax, Immediate(0xc0000000));
1519 __ j(not_zero, &call_runtime);
1520 } else {
1521 // Check if result fits in a smi.
1522 __ cmp(eax, 0xc0000000);
1523 __ j(negative, &non_smi_result);
1524 }
1525 // Tag smi result and return.
1526 __ SmiTag(eax);
1527 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1528
1529 // All ops except SHR return a signed int32 that we load in
1530 // a HeapNumber.
1531 if (op_ != Token::SHR) {
1532 __ bind(&non_smi_result);
1533 // Allocate a heap number if needed.
1534 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001535 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001536 switch (mode_) {
1537 case OVERWRITE_LEFT:
1538 case OVERWRITE_RIGHT:
1539 // If the operand was an object, we skip the
1540 // allocation of a heap number.
1541 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1542 1 * kPointerSize : 2 * kPointerSize));
1543 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001544 __ j(not_zero, &skip_allocation, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001545 // Fall through!
1546 case NO_OVERWRITE:
1547 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1548 __ bind(&skip_allocation);
1549 break;
1550 default: UNREACHABLE();
1551 }
1552 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001553 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001554 CpuFeatures::Scope use_sse2(SSE2);
1555 __ cvtsi2sd(xmm0, Operand(ebx));
1556 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1557 } else {
1558 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1559 __ fild_s(Operand(esp, 1 * kPointerSize));
1560 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1561 }
1562 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1563 }
1564
1565 __ bind(&not_floats);
1566 __ bind(&not_int32);
1567 GenerateTypeTransitionWithSavedArgs(masm);
1568 break;
1569 }
1570 default: UNREACHABLE(); break;
1571 }
1572
1573 // If an allocation fails, or SHR or MOD hit a hard case,
1574 // use the runtime system to get the correct result.
1575 __ bind(&call_runtime);
1576
1577 switch (op_) {
1578 case Token::ADD:
1579 GenerateRegisterArgsPush(masm);
1580 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1581 break;
1582 case Token::SUB:
1583 GenerateRegisterArgsPush(masm);
1584 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1585 break;
1586 case Token::MUL:
1587 GenerateRegisterArgsPush(masm);
1588 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1589 break;
1590 case Token::DIV:
1591 GenerateRegisterArgsPush(masm);
1592 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1593 break;
1594 case Token::MOD:
1595 GenerateRegisterArgsPush(masm);
1596 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1597 break;
1598 case Token::BIT_OR:
1599 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1600 break;
1601 case Token::BIT_AND:
1602 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1603 break;
1604 case Token::BIT_XOR:
1605 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1606 break;
1607 case Token::SAR:
1608 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1609 break;
1610 case Token::SHL:
1611 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1612 break;
1613 case Token::SHR:
1614 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1615 break;
1616 default:
1617 UNREACHABLE();
1618 }
1619}
1620
1621
lrn@chromium.org7516f052011-03-30 08:52:27 +00001622void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
lrn@chromium.org7516f052011-03-30 08:52:27 +00001623 if (op_ == Token::ADD) {
1624 // Handle string addition here, because it is the only operation
1625 // that does not do a ToNumber conversion on the operands.
1626 GenerateAddStrings(masm);
1627 }
1628
danno@chromium.org160a7b02011-04-18 15:51:38 +00001629 Factory* factory = masm->isolate()->factory();
1630
lrn@chromium.org7516f052011-03-30 08:52:27 +00001631 // Convert odd ball arguments to numbers.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001632 Label check, done;
danno@chromium.org160a7b02011-04-18 15:51:38 +00001633 __ cmp(edx, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001634 __ j(not_equal, &check, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001635 if (Token::IsBitOp(op_)) {
1636 __ xor_(edx, Operand(edx));
1637 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001638 __ mov(edx, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001639 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001640 __ jmp(&done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001641 __ bind(&check);
danno@chromium.org160a7b02011-04-18 15:51:38 +00001642 __ cmp(eax, factory->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001643 __ j(not_equal, &done, Label::kNear);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001644 if (Token::IsBitOp(op_)) {
1645 __ xor_(eax, Operand(eax));
1646 } else {
danno@chromium.org160a7b02011-04-18 15:51:38 +00001647 __ mov(eax, Immediate(factory->nan_value()));
lrn@chromium.org7516f052011-03-30 08:52:27 +00001648 }
1649 __ bind(&done);
1650
1651 GenerateHeapNumberStub(masm);
1652}
1653
1654
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001655void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1656 Label call_runtime;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001657
1658 // Floating point case.
1659 switch (op_) {
1660 case Token::ADD:
1661 case Token::SUB:
1662 case Token::MUL:
1663 case Token::DIV: {
1664 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001665 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001666 CpuFeatures::Scope use_sse2(SSE2);
1667 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1668
1669 switch (op_) {
1670 case Token::ADD: __ addsd(xmm0, xmm1); break;
1671 case Token::SUB: __ subsd(xmm0, xmm1); break;
1672 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1673 case Token::DIV: __ divsd(xmm0, xmm1); break;
1674 default: UNREACHABLE();
1675 }
1676 GenerateHeapResultAllocation(masm, &call_runtime);
1677 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1678 __ ret(0);
1679 } else { // SSE2 not available, use FPU.
1680 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1681 FloatingPointHelper::LoadFloatOperands(
1682 masm,
1683 ecx,
1684 FloatingPointHelper::ARGS_IN_REGISTERS);
1685 switch (op_) {
1686 case Token::ADD: __ faddp(1); break;
1687 case Token::SUB: __ fsubp(1); break;
1688 case Token::MUL: __ fmulp(1); break;
1689 case Token::DIV: __ fdivp(1); break;
1690 default: UNREACHABLE();
1691 }
1692 Label after_alloc_failure;
1693 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1694 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1695 __ ret(0);
1696 __ bind(&after_alloc_failure);
1697 __ ffree();
1698 __ jmp(&call_runtime);
1699 }
1700
1701 __ bind(&not_floats);
1702 GenerateTypeTransition(masm);
1703 break;
1704 }
1705
1706 case Token::MOD: {
1707 // For MOD we go directly to runtime in the non-smi case.
1708 break;
1709 }
1710 case Token::BIT_OR:
1711 case Token::BIT_AND:
1712 case Token::BIT_XOR:
1713 case Token::SAR:
1714 case Token::SHL:
1715 case Token::SHR: {
1716 GenerateRegisterArgsPush(masm);
1717 Label not_floats;
1718 Label non_smi_result;
1719 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1720 use_sse3_,
1721 &not_floats);
1722 switch (op_) {
1723 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1724 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1725 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1726 case Token::SAR: __ sar_cl(eax); break;
1727 case Token::SHL: __ shl_cl(eax); break;
1728 case Token::SHR: __ shr_cl(eax); break;
1729 default: UNREACHABLE();
1730 }
1731 if (op_ == Token::SHR) {
1732 // Check if result is non-negative and fits in a smi.
1733 __ test(eax, Immediate(0xc0000000));
1734 __ j(not_zero, &call_runtime);
1735 } else {
1736 // Check if result fits in a smi.
1737 __ cmp(eax, 0xc0000000);
1738 __ j(negative, &non_smi_result);
1739 }
1740 // Tag smi result and return.
1741 __ SmiTag(eax);
1742 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1743
1744 // All ops except SHR return a signed int32 that we load in
1745 // a HeapNumber.
1746 if (op_ != Token::SHR) {
1747 __ bind(&non_smi_result);
1748 // Allocate a heap number if needed.
1749 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001750 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001751 switch (mode_) {
1752 case OVERWRITE_LEFT:
1753 case OVERWRITE_RIGHT:
1754 // If the operand was an object, we skip the
1755 // allocation of a heap number.
1756 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1757 1 * kPointerSize : 2 * kPointerSize));
1758 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001759 __ j(not_zero, &skip_allocation, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001760 // Fall through!
1761 case NO_OVERWRITE:
1762 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1763 __ bind(&skip_allocation);
1764 break;
1765 default: UNREACHABLE();
1766 }
1767 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001768 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001769 CpuFeatures::Scope use_sse2(SSE2);
1770 __ cvtsi2sd(xmm0, Operand(ebx));
1771 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1772 } else {
1773 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1774 __ fild_s(Operand(esp, 1 * kPointerSize));
1775 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1776 }
1777 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1778 }
1779
1780 __ bind(&not_floats);
1781 GenerateTypeTransitionWithSavedArgs(masm);
1782 break;
1783 }
1784 default: UNREACHABLE(); break;
1785 }
1786
1787 // If an allocation fails, or SHR or MOD hit a hard case,
1788 // use the runtime system to get the correct result.
1789 __ bind(&call_runtime);
1790
1791 switch (op_) {
1792 case Token::ADD:
1793 GenerateRegisterArgsPush(masm);
1794 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1795 break;
1796 case Token::SUB:
1797 GenerateRegisterArgsPush(masm);
1798 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1799 break;
1800 case Token::MUL:
1801 GenerateRegisterArgsPush(masm);
1802 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1803 break;
1804 case Token::DIV:
1805 GenerateRegisterArgsPush(masm);
1806 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1807 break;
1808 case Token::MOD:
1809 GenerateRegisterArgsPush(masm);
1810 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1811 break;
1812 case Token::BIT_OR:
1813 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1814 break;
1815 case Token::BIT_AND:
1816 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1817 break;
1818 case Token::BIT_XOR:
1819 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1820 break;
1821 case Token::SAR:
1822 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1823 break;
1824 case Token::SHL:
1825 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1826 break;
1827 case Token::SHR:
1828 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1829 break;
1830 default:
1831 UNREACHABLE();
1832 }
1833}
1834
1835
1836void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
1837 Label call_runtime;
1838
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001839 Counters* counters = masm->isolate()->counters();
1840 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001841
1842 switch (op_) {
1843 case Token::ADD:
1844 case Token::SUB:
1845 case Token::MUL:
1846 case Token::DIV:
1847 break;
1848 case Token::MOD:
1849 case Token::BIT_OR:
1850 case Token::BIT_AND:
1851 case Token::BIT_XOR:
1852 case Token::SAR:
1853 case Token::SHL:
1854 case Token::SHR:
1855 GenerateRegisterArgsPush(masm);
1856 break;
1857 default:
1858 UNREACHABLE();
1859 }
1860
1861 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1862
1863 // Floating point case.
1864 switch (op_) {
1865 case Token::ADD:
1866 case Token::SUB:
1867 case Token::MUL:
1868 case Token::DIV: {
1869 Label not_floats;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001870 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001871 CpuFeatures::Scope use_sse2(SSE2);
1872 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1873
1874 switch (op_) {
1875 case Token::ADD: __ addsd(xmm0, xmm1); break;
1876 case Token::SUB: __ subsd(xmm0, xmm1); break;
1877 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1878 case Token::DIV: __ divsd(xmm0, xmm1); break;
1879 default: UNREACHABLE();
1880 }
1881 GenerateHeapResultAllocation(masm, &call_runtime);
1882 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1883 __ ret(0);
1884 } else { // SSE2 not available, use FPU.
1885 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1886 FloatingPointHelper::LoadFloatOperands(
1887 masm,
1888 ecx,
1889 FloatingPointHelper::ARGS_IN_REGISTERS);
1890 switch (op_) {
1891 case Token::ADD: __ faddp(1); break;
1892 case Token::SUB: __ fsubp(1); break;
1893 case Token::MUL: __ fmulp(1); break;
1894 case Token::DIV: __ fdivp(1); break;
1895 default: UNREACHABLE();
1896 }
1897 Label after_alloc_failure;
1898 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1899 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1900 __ ret(0);
1901 __ bind(&after_alloc_failure);
1902 __ ffree();
1903 __ jmp(&call_runtime);
1904 }
1905 __ bind(&not_floats);
1906 break;
1907 }
1908 case Token::MOD: {
1909 // For MOD we go directly to runtime in the non-smi case.
1910 break;
1911 }
1912 case Token::BIT_OR:
1913 case Token::BIT_AND:
1914 case Token::BIT_XOR:
1915 case Token::SAR:
1916 case Token::SHL:
1917 case Token::SHR: {
1918 Label non_smi_result;
1919 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1920 use_sse3_,
1921 &call_runtime);
1922 switch (op_) {
1923 case Token::BIT_OR: __ or_(eax, Operand(ecx)); break;
1924 case Token::BIT_AND: __ and_(eax, Operand(ecx)); break;
1925 case Token::BIT_XOR: __ xor_(eax, Operand(ecx)); break;
1926 case Token::SAR: __ sar_cl(eax); break;
1927 case Token::SHL: __ shl_cl(eax); break;
1928 case Token::SHR: __ shr_cl(eax); break;
1929 default: UNREACHABLE();
1930 }
1931 if (op_ == Token::SHR) {
1932 // Check if result is non-negative and fits in a smi.
1933 __ test(eax, Immediate(0xc0000000));
1934 __ j(not_zero, &call_runtime);
1935 } else {
1936 // Check if result fits in a smi.
1937 __ cmp(eax, 0xc0000000);
1938 __ j(negative, &non_smi_result);
1939 }
1940 // Tag smi result and return.
1941 __ SmiTag(eax);
1942 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
1943
1944 // All ops except SHR return a signed int32 that we load in
1945 // a HeapNumber.
1946 if (op_ != Token::SHR) {
1947 __ bind(&non_smi_result);
1948 // Allocate a heap number if needed.
1949 __ mov(ebx, Operand(eax)); // ebx: result
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001950 Label skip_allocation;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001951 switch (mode_) {
1952 case OVERWRITE_LEFT:
1953 case OVERWRITE_RIGHT:
1954 // If the operand was an object, we skip the
1955 // allocation of a heap number.
1956 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1957 1 * kPointerSize : 2 * kPointerSize));
1958 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001959 __ j(not_zero, &skip_allocation, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001960 // Fall through!
1961 case NO_OVERWRITE:
1962 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1963 __ bind(&skip_allocation);
1964 break;
1965 default: UNREACHABLE();
1966 }
1967 // Store the result in the HeapNumber and return.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001968 if (CpuFeatures::IsSupported(SSE2)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001969 CpuFeatures::Scope use_sse2(SSE2);
1970 __ cvtsi2sd(xmm0, Operand(ebx));
1971 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1972 } else {
1973 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1974 __ fild_s(Operand(esp, 1 * kPointerSize));
1975 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1976 }
1977 __ ret(2 * kPointerSize);
1978 }
1979 break;
1980 }
1981 default: UNREACHABLE(); break;
1982 }
1983
1984 // If all else fails, use the runtime system to get the correct
1985 // result.
1986 __ bind(&call_runtime);
1987 switch (op_) {
1988 case Token::ADD: {
ager@chromium.org0ee099b2011-01-25 14:06:47 +00001989 GenerateAddStrings(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001990 GenerateRegisterArgsPush(masm);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001991 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1992 break;
1993 }
1994 case Token::SUB:
1995 GenerateRegisterArgsPush(masm);
1996 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1997 break;
1998 case Token::MUL:
1999 GenerateRegisterArgsPush(masm);
2000 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2001 break;
2002 case Token::DIV:
2003 GenerateRegisterArgsPush(masm);
2004 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2005 break;
2006 case Token::MOD:
2007 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2008 break;
2009 case Token::BIT_OR:
2010 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2011 break;
2012 case Token::BIT_AND:
2013 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2014 break;
2015 case Token::BIT_XOR:
2016 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2017 break;
2018 case Token::SAR:
2019 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2020 break;
2021 case Token::SHL:
2022 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2023 break;
2024 case Token::SHR:
2025 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2026 break;
2027 default:
2028 UNREACHABLE();
2029 }
2030}
2031
2032
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002033void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002034 ASSERT(op_ == Token::ADD);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002035 Label left_not_string, call_runtime;
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002036
2037 // Registers containing left and right operands respectively.
2038 Register left = edx;
2039 Register right = eax;
2040
2041 // Test if left operand is a string.
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002042 __ test(left, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002043 __ j(zero, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002044 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002045 __ j(above_equal, &left_not_string, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002046
2047 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2048 GenerateRegisterArgsPush(masm);
2049 __ TailCallStub(&string_add_left_stub);
2050
2051 // Left operand is not a string, test right.
2052 __ bind(&left_not_string);
2053 __ test(right, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002054 __ j(zero, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002055 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002056 __ j(above_equal, &call_runtime, Label::kNear);
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002057
2058 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2059 GenerateRegisterArgsPush(masm);
2060 __ TailCallStub(&string_add_right_stub);
2061
2062 // Neither argument is a string.
2063 __ bind(&call_runtime);
2064}
2065
2066
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002067void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2068 MacroAssembler* masm,
2069 Label* alloc_failure) {
2070 Label skip_allocation;
2071 OverwriteMode mode = mode_;
2072 switch (mode) {
2073 case OVERWRITE_LEFT: {
2074 // If the argument in edx is already an object, we skip the
2075 // allocation of a heap number.
2076 __ test(edx, Immediate(kSmiTagMask));
2077 __ j(not_zero, &skip_allocation, not_taken);
2078 // Allocate a heap number for the result. Keep eax and edx intact
2079 // for the possible runtime call.
2080 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2081 // Now edx can be overwritten losing one of the arguments as we are
2082 // now done and will not need it any more.
2083 __ mov(edx, Operand(ebx));
2084 __ bind(&skip_allocation);
2085 // Use object in edx as a result holder
2086 __ mov(eax, Operand(edx));
2087 break;
2088 }
2089 case OVERWRITE_RIGHT:
2090 // If the argument in eax is already an object, we skip the
2091 // allocation of a heap number.
2092 __ test(eax, Immediate(kSmiTagMask));
2093 __ j(not_zero, &skip_allocation, not_taken);
2094 // Fall through!
2095 case NO_OVERWRITE:
2096 // Allocate a heap number for the result. Keep eax and edx intact
2097 // for the possible runtime call.
2098 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2099 // Now eax can be overwritten losing one of the arguments as we are
2100 // now done and will not need it any more.
2101 __ mov(eax, ebx);
2102 __ bind(&skip_allocation);
2103 break;
2104 default: UNREACHABLE();
2105 }
2106}
2107
2108
2109void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2110 __ pop(ecx);
2111 __ push(edx);
2112 __ push(eax);
2113 __ push(ecx);
2114}
2115
2116
ricow@chromium.org65fae842010-08-25 15:26:24 +00002117void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002118 // TAGGED case:
2119 // Input:
2120 // esp[4]: tagged number input argument (should be number).
2121 // esp[0]: return address.
2122 // Output:
2123 // eax: tagged double result.
2124 // UNTAGGED case:
2125 // Input::
2126 // esp[0]: return address.
2127 // xmm1: untagged double input argument
2128 // Output:
2129 // xmm1: untagged double result.
2130
ricow@chromium.org65fae842010-08-25 15:26:24 +00002131 Label runtime_call;
2132 Label runtime_call_clear_stack;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002133 Label skip_cache;
2134 const bool tagged = (argument_type_ == TAGGED);
2135 if (tagged) {
2136 // Test that eax is a number.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002137 Label input_not_smi;
2138 Label loaded;
whesse@chromium.org023421e2010-12-21 12:19:12 +00002139 __ mov(eax, Operand(esp, kPointerSize));
2140 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002141 __ j(not_zero, &input_not_smi, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002142 // Input is a smi. Untag and load it onto the FPU stack.
2143 // Then load the low and high words of the double into ebx, edx.
2144 STATIC_ASSERT(kSmiTagSize == 1);
2145 __ sar(eax, 1);
2146 __ sub(Operand(esp), Immediate(2 * kPointerSize));
2147 __ mov(Operand(esp, 0), eax);
2148 __ fild_s(Operand(esp, 0));
2149 __ fst_d(Operand(esp, 0));
2150 __ pop(edx);
2151 __ pop(ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002152 __ jmp(&loaded, Label::kNear);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002153 __ bind(&input_not_smi);
2154 // Check if input is a HeapNumber.
2155 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002156 Factory* factory = masm->isolate()->factory();
2157 __ cmp(Operand(ebx), Immediate(factory->heap_number_map()));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002158 __ j(not_equal, &runtime_call);
2159 // Input is a HeapNumber. Push it on the FPU stack and load its
2160 // low and high words into ebx, edx.
2161 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2162 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2163 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002164
whesse@chromium.org023421e2010-12-21 12:19:12 +00002165 __ bind(&loaded);
2166 } else { // UNTAGGED.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002167 if (CpuFeatures::IsSupported(SSE4_1)) {
whesse@chromium.org023421e2010-12-21 12:19:12 +00002168 CpuFeatures::Scope sse4_scope(SSE4_1);
2169 __ pextrd(Operand(edx), xmm1, 0x1); // copy xmm1[63..32] to edx.
2170 } else {
2171 __ pshufd(xmm0, xmm1, 0x1);
2172 __ movd(Operand(edx), xmm0);
2173 }
2174 __ movd(Operand(ebx), xmm1);
2175 }
2176
2177 // ST[0] or xmm1 == double value
ricow@chromium.org65fae842010-08-25 15:26:24 +00002178 // ebx = low 32 bits of double value
2179 // edx = high 32 bits of double value
2180 // Compute hash (the shifts are arithmetic):
2181 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2182 __ mov(ecx, ebx);
2183 __ xor_(ecx, Operand(edx));
2184 __ mov(eax, ecx);
2185 __ sar(eax, 16);
2186 __ xor_(ecx, Operand(eax));
2187 __ mov(eax, ecx);
2188 __ sar(eax, 8);
2189 __ xor_(ecx, Operand(eax));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002190 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2191 __ and_(Operand(ecx),
2192 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002193
whesse@chromium.org023421e2010-12-21 12:19:12 +00002194 // ST[0] or xmm1 == double value.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002195 // ebx = low 32 bits of double value.
2196 // edx = high 32 bits of double value.
2197 // ecx = TranscendentalCache::hash(double value).
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002198 ExternalReference cache_array =
2199 ExternalReference::transcendental_cache_array_address(masm->isolate());
2200 __ mov(eax, Immediate(cache_array));
2201 int cache_array_index =
2202 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2203 __ mov(eax, Operand(eax, cache_array_index));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002204 // Eax points to the cache for the type type_.
2205 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2206 __ test(eax, Operand(eax));
2207 __ j(zero, &runtime_call_clear_stack);
2208#ifdef DEBUG
2209 // Check that the layout of cache elements match expectations.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002210 { TranscendentalCache::SubCache::Element test_elem[2];
ricow@chromium.org65fae842010-08-25 15:26:24 +00002211 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2212 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2213 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2214 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2215 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2216 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2217 CHECK_EQ(0, elem_in0 - elem_start);
2218 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2219 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2220 }
2221#endif
2222 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2223 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2224 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2225 // Check if cache matches: Double value is stored in uint32_t[2] array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002226 Label cache_miss;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002227 __ cmp(ebx, Operand(ecx, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002228 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002229 __ cmp(edx, Operand(ecx, kIntSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002230 __ j(not_equal, &cache_miss, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002231 // Cache hit!
2232 __ mov(eax, Operand(ecx, 2 * kIntSize));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002233 if (tagged) {
2234 __ fstp(0);
2235 __ ret(kPointerSize);
2236 } else { // UNTAGGED.
2237 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2238 __ Ret();
2239 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002240
2241 __ bind(&cache_miss);
2242 // Update cache with new value.
2243 // We are short on registers, so use no_reg as scratch.
2244 // This gives slightly larger code.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002245 if (tagged) {
2246 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2247 } else { // UNTAGGED.
2248 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2249 __ sub(Operand(esp), Immediate(kDoubleSize));
2250 __ movdbl(Operand(esp, 0), xmm1);
2251 __ fld_d(Operand(esp, 0));
2252 __ add(Operand(esp), Immediate(kDoubleSize));
2253 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002254 GenerateOperation(masm);
2255 __ mov(Operand(ecx, 0), ebx);
2256 __ mov(Operand(ecx, kIntSize), edx);
2257 __ mov(Operand(ecx, 2 * kIntSize), eax);
2258 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
whesse@chromium.org023421e2010-12-21 12:19:12 +00002259 if (tagged) {
2260 __ ret(kPointerSize);
2261 } else { // UNTAGGED.
2262 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2263 __ Ret();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002264
whesse@chromium.org023421e2010-12-21 12:19:12 +00002265 // Skip cache and return answer directly, only in untagged case.
2266 __ bind(&skip_cache);
2267 __ sub(Operand(esp), Immediate(kDoubleSize));
2268 __ movdbl(Operand(esp, 0), xmm1);
2269 __ fld_d(Operand(esp, 0));
2270 GenerateOperation(masm);
2271 __ fstp_d(Operand(esp, 0));
2272 __ movdbl(xmm1, Operand(esp, 0));
2273 __ add(Operand(esp), Immediate(kDoubleSize));
2274 // We return the value in xmm1 without adding it to the cache, but
2275 // we cause a scavenging GC so that future allocations will succeed.
2276 __ EnterInternalFrame();
2277 // Allocate an unused object bigger than a HeapNumber.
2278 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2279 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2280 __ LeaveInternalFrame();
2281 __ Ret();
2282 }
2283
2284 // Call runtime, doing whatever allocation and cleanup is necessary.
2285 if (tagged) {
2286 __ bind(&runtime_call_clear_stack);
2287 __ fstp(0);
2288 __ bind(&runtime_call);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002289 ExternalReference runtime =
2290 ExternalReference(RuntimeFunction(), masm->isolate());
2291 __ TailCallExternalReference(runtime, 1, 1);
whesse@chromium.org023421e2010-12-21 12:19:12 +00002292 } else { // UNTAGGED.
2293 __ bind(&runtime_call_clear_stack);
2294 __ bind(&runtime_call);
2295 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2296 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
2297 __ EnterInternalFrame();
2298 __ push(eax);
2299 __ CallRuntime(RuntimeFunction(), 1);
2300 __ LeaveInternalFrame();
2301 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2302 __ Ret();
2303 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002304}
2305
2306
2307Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2308 switch (type_) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002309 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2310 case TranscendentalCache::COS: return Runtime::kMath_cos;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002311 case TranscendentalCache::LOG: return Runtime::kMath_log;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002312 default:
2313 UNIMPLEMENTED();
2314 return Runtime::kAbort;
2315 }
2316}
2317
2318
2319void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2320 // Only free register is edi.
whesse@chromium.org023421e2010-12-21 12:19:12 +00002321 // Input value is on FP stack, and also in ebx/edx.
2322 // Input value is possibly in xmm1.
2323 // Address of result (a newly allocated HeapNumber) may be in eax.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002324 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
2325 // Both fsin and fcos require arguments in the range +/-2^63 and
2326 // return NaN for infinities and NaN. They can share all code except
2327 // the actual fsin/fcos operation.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002328 Label in_range, done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002329 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2330 // work. We must reduce it to the appropriate range.
2331 __ mov(edi, edx);
2332 __ and_(Operand(edi), Immediate(0x7ff00000)); // Exponent only.
2333 int supported_exponent_limit =
2334 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
2335 __ cmp(Operand(edi), Immediate(supported_exponent_limit));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002336 __ j(below, &in_range, taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002337 // Check for infinity and NaN. Both return NaN for sin.
2338 __ cmp(Operand(edi), Immediate(0x7ff00000));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002339 Label non_nan_result;
2340 __ j(not_equal, &non_nan_result, taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002341 // Input is +/-Infinity or NaN. Result is NaN.
2342 __ fstp(0);
2343 // NaN is represented by 0x7ff8000000000000.
2344 __ push(Immediate(0x7ff80000));
2345 __ push(Immediate(0));
2346 __ fld_d(Operand(esp, 0));
2347 __ add(Operand(esp), Immediate(2 * kPointerSize));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002348 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002349
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002350 __ bind(&non_nan_result);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002351
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002352 // Use fpmod to restrict argument to the range +/-2*PI.
2353 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2354 __ fldpi();
2355 __ fadd(0);
2356 __ fld(1);
2357 // FPU Stack: input, 2*pi, input.
2358 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002359 Label no_exceptions;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002360 __ fwait();
2361 __ fnstsw_ax();
2362 // Clear if Illegal Operand or Zero Division exceptions are set.
2363 __ test(Operand(eax), Immediate(5));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002364 __ j(zero, &no_exceptions, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002365 __ fnclex();
2366 __ bind(&no_exceptions);
2367 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002368
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002369 // Compute st(0) % st(1)
2370 {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002371 Label partial_remainder_loop;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002372 __ bind(&partial_remainder_loop);
2373 __ fprem1();
2374 __ fwait();
2375 __ fnstsw_ax();
2376 __ test(Operand(eax), Immediate(0x400 /* C2 */));
2377 // If C2 is set, computation only has partial result. Loop to
2378 // continue computation.
2379 __ j(not_zero, &partial_remainder_loop);
2380 }
2381 // FPU Stack: input, 2*pi, input % 2*pi
2382 __ fstp(2);
2383 __ fstp(0);
2384 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2385
2386 // FPU Stack: input % 2*pi
2387 __ bind(&in_range);
2388 switch (type_) {
2389 case TranscendentalCache::SIN:
2390 __ fsin();
2391 break;
2392 case TranscendentalCache::COS:
2393 __ fcos();
2394 break;
2395 default:
2396 UNREACHABLE();
2397 }
2398 __ bind(&done);
2399 } else {
2400 ASSERT(type_ == TranscendentalCache::LOG);
2401 __ fldln2();
2402 __ fxch();
2403 __ fyl2x();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002404 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002405}
2406
2407
ricow@chromium.org65fae842010-08-25 15:26:24 +00002408// Input: edx, eax are the left and right objects of a bit op.
2409// Output: eax, ecx are left and right integers for a bit op.
2410void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm,
2411 TypeInfo type_info,
2412 bool use_sse3,
2413 Label* conversion_failure) {
2414 // Check float operands.
2415 Label arg1_is_object, check_undefined_arg1;
2416 Label arg2_is_object, check_undefined_arg2;
2417 Label load_arg2, done;
2418
2419 if (!type_info.IsDouble()) {
2420 if (!type_info.IsSmi()) {
2421 __ test(edx, Immediate(kSmiTagMask));
2422 __ j(not_zero, &arg1_is_object);
2423 } else {
2424 if (FLAG_debug_code) __ AbortIfNotSmi(edx);
2425 }
2426 __ SmiUntag(edx);
2427 __ jmp(&load_arg2);
2428 }
2429
2430 __ bind(&arg1_is_object);
2431
2432 // Get the untagged integer version of the edx heap number in ecx.
2433 IntegerConvert(masm, edx, type_info, use_sse3, conversion_failure);
2434 __ mov(edx, ecx);
2435
2436 // Here edx has the untagged integer, eax has a Smi or a heap number.
2437 __ bind(&load_arg2);
2438 if (!type_info.IsDouble()) {
2439 // Test if arg2 is a Smi.
2440 if (!type_info.IsSmi()) {
2441 __ test(eax, Immediate(kSmiTagMask));
2442 __ j(not_zero, &arg2_is_object);
2443 } else {
2444 if (FLAG_debug_code) __ AbortIfNotSmi(eax);
2445 }
2446 __ SmiUntag(eax);
2447 __ mov(ecx, eax);
2448 __ jmp(&done);
2449 }
2450
2451 __ bind(&arg2_is_object);
2452
2453 // Get the untagged integer version of the eax heap number in ecx.
2454 IntegerConvert(masm, eax, type_info, use_sse3, conversion_failure);
2455 __ bind(&done);
2456 __ mov(eax, edx);
2457}
2458
2459
2460// Input: edx, eax are the left and right objects of a bit op.
2461// Output: eax, ecx are left and right integers for a bit op.
2462void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2463 bool use_sse3,
2464 Label* conversion_failure) {
2465 // Check float operands.
2466 Label arg1_is_object, check_undefined_arg1;
2467 Label arg2_is_object, check_undefined_arg2;
2468 Label load_arg2, done;
2469
2470 // Test if arg1 is a Smi.
2471 __ test(edx, Immediate(kSmiTagMask));
2472 __ j(not_zero, &arg1_is_object);
2473
2474 __ SmiUntag(edx);
2475 __ jmp(&load_arg2);
2476
2477 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2478 __ bind(&check_undefined_arg1);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002479 Factory* factory = masm->isolate()->factory();
2480 __ cmp(edx, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002481 __ j(not_equal, conversion_failure);
2482 __ mov(edx, Immediate(0));
2483 __ jmp(&load_arg2);
2484
2485 __ bind(&arg1_is_object);
2486 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002487 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002488 __ j(not_equal, &check_undefined_arg1);
2489
2490 // Get the untagged integer version of the edx heap number in ecx.
2491 IntegerConvert(masm,
2492 edx,
2493 TypeInfo::Unknown(),
2494 use_sse3,
2495 conversion_failure);
2496 __ mov(edx, ecx);
2497
2498 // Here edx has the untagged integer, eax has a Smi or a heap number.
2499 __ bind(&load_arg2);
2500
2501 // Test if arg2 is a Smi.
2502 __ test(eax, Immediate(kSmiTagMask));
2503 __ j(not_zero, &arg2_is_object);
2504
2505 __ SmiUntag(eax);
2506 __ mov(ecx, eax);
2507 __ jmp(&done);
2508
2509 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2510 __ bind(&check_undefined_arg2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002511 __ cmp(eax, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002512 __ j(not_equal, conversion_failure);
2513 __ mov(ecx, Immediate(0));
2514 __ jmp(&done);
2515
2516 __ bind(&arg2_is_object);
2517 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002518 __ cmp(ebx, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002519 __ j(not_equal, &check_undefined_arg2);
2520
2521 // Get the untagged integer version of the eax heap number in ecx.
2522 IntegerConvert(masm,
2523 eax,
2524 TypeInfo::Unknown(),
2525 use_sse3,
2526 conversion_failure);
2527 __ bind(&done);
2528 __ mov(eax, edx);
2529}
2530
2531
2532void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
2533 TypeInfo type_info,
2534 bool use_sse3,
2535 Label* conversion_failure) {
2536 if (type_info.IsNumber()) {
2537 LoadNumbersAsIntegers(masm, type_info, use_sse3, conversion_failure);
2538 } else {
2539 LoadUnknownsAsIntegers(masm, use_sse3, conversion_failure);
2540 }
2541}
2542
2543
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002544void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2545 bool use_sse3,
2546 Label* not_int32) {
2547 return;
2548}
2549
2550
ricow@chromium.org65fae842010-08-25 15:26:24 +00002551void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2552 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002553 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002554
2555 __ test(number, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002556 __ j(zero, &load_smi, not_taken, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002557 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002558 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002559
2560 __ bind(&load_smi);
2561 __ SmiUntag(number);
2562 __ push(number);
2563 __ fild_s(Operand(esp, 0));
2564 __ pop(number);
2565
2566 __ bind(&done);
2567}
2568
2569
2570void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002571 Label load_smi_edx, load_eax, load_smi_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002572 // Load operand in edx into xmm0.
2573 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002574 // Argument in edx is a smi.
2575 __ j(zero, &load_smi_edx, not_taken, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002576 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2577
2578 __ bind(&load_eax);
2579 // Load operand in eax into xmm1.
2580 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002581 // Argument in eax is a smi.
2582 __ j(zero, &load_smi_eax, not_taken, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002583 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002584 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002585
2586 __ bind(&load_smi_edx);
2587 __ SmiUntag(edx); // Untag smi before converting to float.
2588 __ cvtsi2sd(xmm0, Operand(edx));
2589 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2590 __ jmp(&load_eax);
2591
2592 __ bind(&load_smi_eax);
2593 __ SmiUntag(eax); // Untag smi before converting to float.
2594 __ cvtsi2sd(xmm1, Operand(eax));
2595 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2596
2597 __ bind(&done);
2598}
2599
2600
2601void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2602 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002603 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002604 // Load operand in edx into xmm0, or branch to not_numbers.
2605 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002606 // Argument in edx is a smi.
2607 __ j(zero, &load_smi_edx, not_taken, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002608 Factory* factory = masm->isolate()->factory();
2609 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002610 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2611 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2612 __ bind(&load_eax);
2613 // Load operand in eax into xmm1, or branch to not_numbers.
2614 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002615 // Argument in eax is a smi.
2616 __ j(zero, &load_smi_eax, not_taken, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002617 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002618 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002619 __ jmp(not_numbers); // Argument in eax is not a number.
2620 __ bind(&load_smi_edx);
2621 __ SmiUntag(edx); // Untag smi before converting to float.
2622 __ cvtsi2sd(xmm0, Operand(edx));
2623 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2624 __ jmp(&load_eax);
2625 __ bind(&load_smi_eax);
2626 __ SmiUntag(eax); // Untag smi before converting to float.
2627 __ cvtsi2sd(xmm1, Operand(eax));
2628 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002629 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002630 __ bind(&load_float_eax);
2631 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2632 __ bind(&done);
2633}
2634
2635
2636void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2637 Register scratch) {
2638 const Register left = edx;
2639 const Register right = eax;
2640 __ mov(scratch, left);
2641 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2642 __ SmiUntag(scratch);
2643 __ cvtsi2sd(xmm0, Operand(scratch));
2644
2645 __ mov(scratch, right);
2646 __ SmiUntag(scratch);
2647 __ cvtsi2sd(xmm1, Operand(scratch));
2648}
2649
2650
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002651void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2652 Label* non_int32,
2653 Register scratch) {
2654 __ cvttsd2si(scratch, Operand(xmm0));
2655 __ cvtsi2sd(xmm2, Operand(scratch));
2656 __ ucomisd(xmm0, xmm2);
2657 __ j(not_zero, non_int32);
2658 __ j(carry, non_int32);
2659 __ cvttsd2si(scratch, Operand(xmm1));
2660 __ cvtsi2sd(xmm2, Operand(scratch));
2661 __ ucomisd(xmm1, xmm2);
2662 __ j(not_zero, non_int32);
2663 __ j(carry, non_int32);
2664}
2665
2666
ricow@chromium.org65fae842010-08-25 15:26:24 +00002667void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2668 Register scratch,
2669 ArgLocation arg_location) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002670 Label load_smi_1, load_smi_2, done_load_1, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002671 if (arg_location == ARGS_IN_REGISTERS) {
2672 __ mov(scratch, edx);
2673 } else {
2674 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2675 }
2676 __ test(scratch, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002677 __ j(zero, &load_smi_1, not_taken, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002678 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2679 __ bind(&done_load_1);
2680
2681 if (arg_location == ARGS_IN_REGISTERS) {
2682 __ mov(scratch, eax);
2683 } else {
2684 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2685 }
2686 __ test(scratch, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002687 __ j(zero, &load_smi_2, not_taken, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002688 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002689 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002690
2691 __ bind(&load_smi_1);
2692 __ SmiUntag(scratch);
2693 __ push(scratch);
2694 __ fild_s(Operand(esp, 0));
2695 __ pop(scratch);
2696 __ jmp(&done_load_1);
2697
2698 __ bind(&load_smi_2);
2699 __ SmiUntag(scratch);
2700 __ push(scratch);
2701 __ fild_s(Operand(esp, 0));
2702 __ pop(scratch);
2703
2704 __ bind(&done);
2705}
2706
2707
2708void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2709 Register scratch) {
2710 const Register left = edx;
2711 const Register right = eax;
2712 __ mov(scratch, left);
2713 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2714 __ SmiUntag(scratch);
2715 __ push(scratch);
2716 __ fild_s(Operand(esp, 0));
2717
2718 __ mov(scratch, right);
2719 __ SmiUntag(scratch);
2720 __ mov(Operand(esp, 0), scratch);
2721 __ fild_s(Operand(esp, 0));
2722 __ pop(scratch);
2723}
2724
2725
2726void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2727 Label* non_float,
2728 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002729 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002730 // Test if both operands are floats or smi -> scratch=k_is_float;
2731 // Otherwise scratch = k_not_float.
2732 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002733 __ j(zero, &test_other, not_taken, Label::kNear); // argument in edx is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002734 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002735 Factory* factory = masm->isolate()->factory();
2736 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002737 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2738
2739 __ bind(&test_other);
2740 __ test(eax, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002741 __ j(zero, &done, Label::kNear); // argument in eax is OK
ricow@chromium.org65fae842010-08-25 15:26:24 +00002742 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002743 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002744 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2745
2746 // Fall-through: Both operands are numbers.
2747 __ bind(&done);
2748}
2749
2750
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002751void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2752 Label* non_int32) {
2753 return;
2754}
2755
2756
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002757void MathPowStub::Generate(MacroAssembler* masm) {
2758 // Registers are used as follows:
2759 // edx = base
2760 // eax = exponent
2761 // ecx = temporary, result
2762
2763 CpuFeatures::Scope use_sse2(SSE2);
2764 Label allocate_return, call_runtime;
2765
2766 // Load input parameters.
2767 __ mov(edx, Operand(esp, 2 * kPointerSize));
2768 __ mov(eax, Operand(esp, 1 * kPointerSize));
2769
2770 // Save 1 in xmm3 - we need this several times later on.
2771 __ mov(ecx, Immediate(1));
2772 __ cvtsi2sd(xmm3, Operand(ecx));
2773
2774 Label exponent_nonsmi;
2775 Label base_nonsmi;
2776 // If the exponent is a heap number go to that specific case.
2777 __ test(eax, Immediate(kSmiTagMask));
2778 __ j(not_zero, &exponent_nonsmi);
2779 __ test(edx, Immediate(kSmiTagMask));
2780 __ j(not_zero, &base_nonsmi);
2781
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002782 // Optimized version when both exponent and base are smis.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002783 Label powi;
2784 __ SmiUntag(edx);
2785 __ cvtsi2sd(xmm0, Operand(edx));
2786 __ jmp(&powi);
2787 // exponent is smi and base is a heapnumber.
2788 __ bind(&base_nonsmi);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002789 Factory* factory = masm->isolate()->factory();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002790 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002791 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002792 __ j(not_equal, &call_runtime);
2793
2794 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2795
2796 // Optimized version of pow if exponent is a smi.
2797 // xmm0 contains the base.
2798 __ bind(&powi);
2799 __ SmiUntag(eax);
2800
2801 // Save exponent in base as we need to check if exponent is negative later.
2802 // We know that base and exponent are in different registers.
2803 __ mov(edx, eax);
2804
2805 // Get absolute value of exponent.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002806 Label no_neg;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002807 __ cmp(eax, 0);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002808 __ j(greater_equal, &no_neg, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002809 __ neg(eax);
2810 __ bind(&no_neg);
2811
2812 // Load xmm1 with 1.
2813 __ movsd(xmm1, xmm3);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002814 Label while_true;
2815 Label no_multiply;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002816
2817 __ bind(&while_true);
2818 __ shr(eax, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002819 __ j(not_carry, &no_multiply, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002820 __ mulsd(xmm1, xmm0);
2821 __ bind(&no_multiply);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002822 __ mulsd(xmm0, xmm0);
2823 __ j(not_zero, &while_true);
2824
2825 // base has the original value of the exponent - if the exponent is
2826 // negative return 1/result.
2827 __ test(edx, Operand(edx));
2828 __ j(positive, &allocate_return);
2829 // Special case if xmm1 has reached infinity.
2830 __ mov(ecx, Immediate(0x7FB00000));
2831 __ movd(xmm0, Operand(ecx));
2832 __ cvtss2sd(xmm0, xmm0);
2833 __ ucomisd(xmm0, xmm1);
2834 __ j(equal, &call_runtime);
2835 __ divsd(xmm3, xmm1);
2836 __ movsd(xmm1, xmm3);
2837 __ jmp(&allocate_return);
2838
2839 // exponent (or both) is a heapnumber - no matter what we should now work
2840 // on doubles.
2841 __ bind(&exponent_nonsmi);
2842 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002843 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002844 __ j(not_equal, &call_runtime);
2845 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2846 // Test if exponent is nan.
2847 __ ucomisd(xmm1, xmm1);
2848 __ j(parity_even, &call_runtime);
2849
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002850 Label base_not_smi;
2851 Label handle_special_cases;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002852 __ test(edx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002853 __ j(not_zero, &base_not_smi, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002854 __ SmiUntag(edx);
2855 __ cvtsi2sd(xmm0, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002856 __ jmp(&handle_special_cases, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002857
2858 __ bind(&base_not_smi);
2859 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002860 factory->heap_number_map());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002861 __ j(not_equal, &call_runtime);
2862 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
2863 __ and_(ecx, HeapNumber::kExponentMask);
2864 __ cmp(Operand(ecx), Immediate(HeapNumber::kExponentMask));
2865 // base is NaN or +/-Infinity
2866 __ j(greater_equal, &call_runtime);
2867 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2868
2869 // base is in xmm0 and exponent is in xmm1.
2870 __ bind(&handle_special_cases);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002871 Label not_minus_half;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002872 // Test for -0.5.
2873 // Load xmm2 with -0.5.
2874 __ mov(ecx, Immediate(0xBF000000));
2875 __ movd(xmm2, Operand(ecx));
2876 __ cvtss2sd(xmm2, xmm2);
2877 // xmm2 now has -0.5.
2878 __ ucomisd(xmm2, xmm1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002879 __ j(not_equal, &not_minus_half, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002880
2881 // Calculates reciprocal of square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002882 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002883 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002884 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002885 __ sqrtsd(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002886 __ divsd(xmm3, xmm1);
2887 __ movsd(xmm1, xmm3);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002888 __ jmp(&allocate_return);
2889
2890 // Test for 0.5.
2891 __ bind(&not_minus_half);
2892 // Load xmm2 with 0.5.
2893 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2894 __ addsd(xmm2, xmm3);
2895 // xmm2 now has 0.5.
2896 __ ucomisd(xmm2, xmm1);
2897 __ j(not_equal, &call_runtime);
2898 // Calculates square root.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002899 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00002900 __ xorps(xmm1, xmm1);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00002901 __ addsd(xmm1, xmm0);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002902 __ sqrtsd(xmm1, xmm1);
2903
2904 __ bind(&allocate_return);
2905 __ AllocateHeapNumber(ecx, eax, edx, &call_runtime);
2906 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm1);
2907 __ mov(eax, ecx);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002908 __ ret(2 * kPointerSize);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002909
2910 __ bind(&call_runtime);
2911 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2912}
2913
2914
ricow@chromium.org65fae842010-08-25 15:26:24 +00002915void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2916 // The key is in edx and the parameter count is in eax.
2917
2918 // The displacement is used for skipping the frame pointer on the
2919 // stack. It is the offset of the last parameter (if any) relative
2920 // to the frame pointer.
2921 static const int kDisplacement = 1 * kPointerSize;
2922
2923 // Check that the key is a smi.
2924 Label slow;
2925 __ test(edx, Immediate(kSmiTagMask));
2926 __ j(not_zero, &slow, not_taken);
2927
2928 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002929 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002930 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2931 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
2932 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002933 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002934
2935 // Check index against formal parameters count limit passed in
2936 // through register eax. Use unsigned comparison to get negative
2937 // check for free.
2938 __ cmp(edx, Operand(eax));
2939 __ j(above_equal, &slow, not_taken);
2940
2941 // Read the argument from the stack and return it.
2942 STATIC_ASSERT(kSmiTagSize == 1);
2943 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2944 __ lea(ebx, Operand(ebp, eax, times_2, 0));
2945 __ neg(edx);
2946 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2947 __ ret(0);
2948
2949 // Arguments adaptor case: Check index against actual arguments
2950 // limit found in the arguments adaptor frame. Use unsigned
2951 // comparison to get negative check for free.
2952 __ bind(&adaptor);
2953 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2954 __ cmp(edx, Operand(ecx));
2955 __ j(above_equal, &slow, not_taken);
2956
2957 // Read the argument from the stack and return it.
2958 STATIC_ASSERT(kSmiTagSize == 1);
2959 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
2960 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
2961 __ neg(edx);
2962 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
2963 __ ret(0);
2964
2965 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2966 // by calling the runtime system.
2967 __ bind(&slow);
2968 __ pop(ebx); // Return address.
2969 __ push(edx);
2970 __ push(ebx);
2971 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2972}
2973
2974
2975void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
2976 // esp[0] : return address
2977 // esp[4] : number of parameters
2978 // esp[8] : receiver displacement
2979 // esp[16] : function
2980
2981 // The displacement is used for skipping the return address and the
2982 // frame pointer on the stack. It is the offset of the last
2983 // parameter (if any) relative to the frame pointer.
2984 static const int kDisplacement = 2 * kPointerSize;
2985
2986 // Check if the calling frame is an arguments adaptor frame.
2987 Label adaptor_frame, try_allocate, runtime;
2988 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
2989 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
2990 __ cmp(Operand(ecx), Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2991 __ j(equal, &adaptor_frame);
2992
2993 // Get the length from the frame.
2994 __ mov(ecx, Operand(esp, 1 * kPointerSize));
2995 __ jmp(&try_allocate);
2996
2997 // Patch the arguments.length and the parameters pointer.
2998 __ bind(&adaptor_frame);
2999 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3000 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3001 __ lea(edx, Operand(edx, ecx, times_2, kDisplacement));
3002 __ mov(Operand(esp, 2 * kPointerSize), edx);
3003
3004 // Try the new space allocation. Start out with computing the size of
3005 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003006 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003007 __ bind(&try_allocate);
3008 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003009 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003010 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3011 __ bind(&add_arguments_object);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003012 __ add(Operand(ecx), Immediate(GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003013
3014 // Do the allocation of both objects in one go.
3015 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3016
3017 // Get the arguments boilerplate from the current (global) context.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003018 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3019 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003020 __ mov(edi, Operand(edi,
3021 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003022
3023 // Copy the JS object part.
3024 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3025 __ mov(ebx, FieldOperand(edi, i));
3026 __ mov(FieldOperand(eax, i), ebx);
3027 }
3028
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003029 if (type_ == NEW_NON_STRICT) {
3030 // Setup the callee in-object property.
3031 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3032 __ mov(ebx, Operand(esp, 3 * kPointerSize));
3033 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3034 Heap::kArgumentsCalleeIndex * kPointerSize),
3035 ebx);
3036 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003037
3038 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003039 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003040 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003041 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3042 Heap::kArgumentsLengthIndex * kPointerSize),
3043 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003044
3045 // If there are no actual arguments, we're done.
3046 Label done;
3047 __ test(ecx, Operand(ecx));
3048 __ j(zero, &done);
3049
3050 // Get the parameters pointer from the stack.
3051 __ mov(edx, Operand(esp, 2 * kPointerSize));
3052
3053 // Setup the elements pointer in the allocated arguments object and
3054 // initialize the header in the elements fixed array.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003055 __ lea(edi, Operand(eax, GetArgumentsObjectSize()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003056 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3057 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003058 Immediate(masm->isolate()->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003059
ricow@chromium.org65fae842010-08-25 15:26:24 +00003060 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3061 // Untag the length for the loop below.
3062 __ SmiUntag(ecx);
3063
3064 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003065 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003066 __ bind(&loop);
3067 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3068 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
3069 __ add(Operand(edi), Immediate(kPointerSize));
3070 __ sub(Operand(edx), Immediate(kPointerSize));
3071 __ dec(ecx);
3072 __ j(not_zero, &loop);
3073
3074 // Return and remove the on-stack parameters.
3075 __ bind(&done);
3076 __ ret(3 * kPointerSize);
3077
3078 // Do the runtime call to allocate the arguments object.
3079 __ bind(&runtime);
3080 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3081}
3082
3083
3084void RegExpExecStub::Generate(MacroAssembler* masm) {
3085 // Just jump directly to runtime if native RegExp is not selected at compile
3086 // time or if regexp entry in generated code is turned off runtime switch or
3087 // at compilation.
3088#ifdef V8_INTERPRETED_REGEXP
3089 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3090#else // V8_INTERPRETED_REGEXP
3091 if (!FLAG_regexp_entry_native) {
3092 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3093 return;
3094 }
3095
3096 // Stack frame on entry.
3097 // esp[0]: return address
3098 // esp[4]: last_match_info (expected JSArray)
3099 // esp[8]: previous index
3100 // esp[12]: subject string
3101 // esp[16]: JSRegExp object
3102
3103 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3104 static const int kPreviousIndexOffset = 2 * kPointerSize;
3105 static const int kSubjectOffset = 3 * kPointerSize;
3106 static const int kJSRegExpOffset = 4 * kPointerSize;
3107
3108 Label runtime, invoke_regexp;
3109
3110 // Ensure that a RegExp stack is allocated.
3111 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003112 ExternalReference::address_of_regexp_stack_memory_address(
3113 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003114 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003115 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003116 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3117 __ test(ebx, Operand(ebx));
3118 __ j(zero, &runtime, not_taken);
3119
3120 // Check that the first argument is a JSRegExp object.
3121 __ mov(eax, Operand(esp, kJSRegExpOffset));
3122 STATIC_ASSERT(kSmiTag == 0);
3123 __ test(eax, Immediate(kSmiTagMask));
3124 __ j(zero, &runtime);
3125 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3126 __ j(not_equal, &runtime);
3127 // Check that the RegExp has been compiled (data contains a fixed array).
3128 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3129 if (FLAG_debug_code) {
3130 __ test(ecx, Immediate(kSmiTagMask));
3131 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3132 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3133 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3134 }
3135
3136 // ecx: RegExp data (FixedArray)
3137 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3138 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
3139 __ cmp(Operand(ebx), Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
3140 __ j(not_equal, &runtime);
3141
3142 // ecx: RegExp data (FixedArray)
3143 // Check that the number of captures fit in the static offsets vector buffer.
3144 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3145 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3146 // uses the asumption that smis are 2 * their untagged value.
3147 STATIC_ASSERT(kSmiTag == 0);
3148 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3149 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3150 // Check that the static offsets vector buffer is large enough.
3151 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3152 __ j(above, &runtime);
3153
3154 // ecx: RegExp data (FixedArray)
3155 // edx: Number of capture registers
3156 // Check that the second argument is a string.
3157 __ mov(eax, Operand(esp, kSubjectOffset));
3158 __ test(eax, Immediate(kSmiTagMask));
3159 __ j(zero, &runtime);
3160 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3161 __ j(NegateCondition(is_string), &runtime);
3162 // Get the length of the string to ebx.
3163 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3164
3165 // ebx: Length of subject string as a smi
3166 // ecx: RegExp data (FixedArray)
3167 // edx: Number of capture registers
3168 // Check that the third argument is a positive smi less than the subject
3169 // string length. A negative value will be greater (unsigned comparison).
3170 __ mov(eax, Operand(esp, kPreviousIndexOffset));
3171 __ test(eax, Immediate(kSmiTagMask));
3172 __ j(not_zero, &runtime);
3173 __ cmp(eax, Operand(ebx));
3174 __ j(above_equal, &runtime);
3175
3176 // ecx: RegExp data (FixedArray)
3177 // edx: Number of capture registers
3178 // Check that the fourth object is a JSArray object.
3179 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3180 __ test(eax, Immediate(kSmiTagMask));
3181 __ j(zero, &runtime);
3182 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3183 __ j(not_equal, &runtime);
3184 // Check that the JSArray is in fast case.
3185 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3186 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003187 Factory* factory = masm->isolate()->factory();
3188 __ cmp(eax, factory->fixed_array_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003189 __ j(not_equal, &runtime);
3190 // Check that the last match info has space for the capture registers and the
3191 // additional information.
3192 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3193 __ SmiUntag(eax);
3194 __ add(Operand(edx), Immediate(RegExpImpl::kLastMatchOverhead));
3195 __ cmp(edx, Operand(eax));
3196 __ j(greater, &runtime);
3197
3198 // ecx: RegExp data (FixedArray)
3199 // Check the representation and encoding of the subject string.
3200 Label seq_ascii_string, seq_two_byte_string, check_code;
3201 __ mov(eax, Operand(esp, kSubjectOffset));
3202 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3203 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3204 // First check for flat two byte string.
3205 __ and_(ebx,
3206 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask);
3207 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
3208 __ j(zero, &seq_two_byte_string);
3209 // Any other flat string must be a flat ascii string.
3210 __ test(Operand(ebx),
3211 Immediate(kIsNotStringMask | kStringRepresentationMask));
3212 __ j(zero, &seq_ascii_string);
3213
3214 // Check for flat cons string.
3215 // A flat cons string is a cons string where the second part is the empty
3216 // string. In that case the subject string is just the first part of the cons
3217 // string. Also in this case the first part of the cons string is known to be
3218 // a sequential string or an external string.
3219 STATIC_ASSERT(kExternalStringTag != 0);
3220 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3221 __ test(Operand(ebx),
3222 Immediate(kIsNotStringMask | kExternalStringTag));
3223 __ j(not_zero, &runtime);
3224 // String is a cons string.
3225 __ mov(edx, FieldOperand(eax, ConsString::kSecondOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003226 __ cmp(Operand(edx), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003227 __ j(not_equal, &runtime);
3228 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
3229 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3230 // String is a cons string with empty second part.
3231 // eax: first part of cons string.
3232 // ebx: map of first part of cons string.
3233 // Is first part a flat two byte string?
3234 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3235 kStringRepresentationMask | kStringEncodingMask);
3236 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
3237 __ j(zero, &seq_two_byte_string);
3238 // Any other flat string must be ascii.
3239 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3240 kStringRepresentationMask);
3241 __ j(not_zero, &runtime);
3242
3243 __ bind(&seq_ascii_string);
3244 // eax: subject string (flat ascii)
3245 // ecx: RegExp data (FixedArray)
3246 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
3247 __ Set(edi, Immediate(1)); // Type is ascii.
3248 __ jmp(&check_code);
3249
3250 __ bind(&seq_two_byte_string);
3251 // eax: subject string (flat two byte)
3252 // ecx: RegExp data (FixedArray)
3253 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
3254 __ Set(edi, Immediate(0)); // Type is two byte.
3255
3256 __ bind(&check_code);
3257 // Check that the irregexp code has been generated for the actual string
3258 // encoding. If it has, the field contains a code object otherwise it contains
3259 // the hole.
3260 __ CmpObjectType(edx, CODE_TYPE, ebx);
3261 __ j(not_equal, &runtime);
3262
3263 // eax: subject string
3264 // edx: code
3265 // edi: encoding of subject string (1 if ascii, 0 if two_byte);
3266 // Load used arguments before starting to push arguments for call to native
3267 // RegExp code to avoid handling changing stack height.
3268 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3269 __ SmiUntag(ebx); // Previous index from smi.
3270
3271 // eax: subject string
3272 // ebx: previous index
3273 // edx: code
3274 // edi: encoding of subject string (1 if ascii 0 if two_byte);
3275 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003276 Counters* counters = masm->isolate()->counters();
3277 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003278
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003279 // Isolates: note we add an additional parameter here (isolate pointer).
3280 static const int kRegExpExecuteArguments = 8;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003281 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003282
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003283 // Argument 8: Pass current isolate address.
3284 __ mov(Operand(esp, 7 * kPointerSize),
3285 Immediate(ExternalReference::isolate_address()));
3286
ricow@chromium.org65fae842010-08-25 15:26:24 +00003287 // Argument 7: Indicate that this is a direct call from JavaScript.
3288 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3289
3290 // Argument 6: Start (high end) of backtracking stack memory area.
3291 __ mov(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3292 __ add(ecx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3293 __ mov(Operand(esp, 5 * kPointerSize), ecx);
3294
3295 // Argument 5: static offsets vector buffer.
3296 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003297 Immediate(ExternalReference::address_of_static_offsets_vector(
3298 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003299
3300 // Argument 4: End of string data
3301 // Argument 3: Start of string data
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003302 Label setup_two_byte, setup_rest;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003303 __ test(edi, Operand(edi));
3304 __ mov(edi, FieldOperand(eax, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003305 __ j(zero, &setup_two_byte, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003306 __ SmiUntag(edi);
3307 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqAsciiString::kHeaderSize));
3308 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3309 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3310 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003311 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003312
3313 __ bind(&setup_two_byte);
3314 STATIC_ASSERT(kSmiTag == 0);
3315 STATIC_ASSERT(kSmiTagSize == 1); // edi is smi (powered by 2).
3316 __ lea(ecx, FieldOperand(eax, edi, times_1, SeqTwoByteString::kHeaderSize));
3317 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3318 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3319 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3320
3321 __ bind(&setup_rest);
3322
3323 // Argument 2: Previous index.
3324 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3325
3326 // Argument 1: Subject string.
3327 __ mov(Operand(esp, 0 * kPointerSize), eax);
3328
3329 // Locate the code entry and call it.
3330 __ add(Operand(edx), Immediate(Code::kHeaderSize - kHeapObjectTag));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003331 __ call(Operand(edx));
3332
3333 // Drop arguments and come back to JS mode.
3334 __ LeaveApiExitFrame();
ricow@chromium.org65fae842010-08-25 15:26:24 +00003335
3336 // Check the result.
3337 Label success;
3338 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
3339 __ j(equal, &success, taken);
3340 Label failure;
3341 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
3342 __ j(equal, &failure, taken);
3343 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3344 // If not exception it can only be retry. Handle that in the runtime system.
3345 __ j(not_equal, &runtime);
3346 // Result must now be exception. If there is no pending exception already a
3347 // stack overflow (on the backtrack stack) was detected in RegExp code but
3348 // haven't created the exception yet. Handle that in the runtime system.
3349 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003350 ExternalReference pending_exception(Isolate::k_pending_exception_address,
3351 masm->isolate());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003352 __ mov(edx,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003353 Operand::StaticVariable(ExternalReference::the_hole_value_location(
3354 masm->isolate())));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003355 __ mov(eax, Operand::StaticVariable(pending_exception));
3356 __ cmp(edx, Operand(eax));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003357 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003358 // For exception, throw the exception again.
3359
3360 // Clear the pending exception variable.
3361 __ mov(Operand::StaticVariable(pending_exception), edx);
3362
3363 // Special handling of termination exceptions which are uncatchable
3364 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003365 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003366 Label throw_termination_exception;
3367 __ j(equal, &throw_termination_exception);
3368
3369 // Handle normal exception by following handler chain.
3370 __ Throw(eax);
3371
3372 __ bind(&throw_termination_exception);
3373 __ ThrowUncatchable(TERMINATION, eax);
3374
ricow@chromium.org65fae842010-08-25 15:26:24 +00003375 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00003376 // For failure to match, return null.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003377 __ mov(Operand(eax), factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003378 __ ret(4 * kPointerSize);
3379
3380 // Load RegExp data.
3381 __ bind(&success);
3382 __ mov(eax, Operand(esp, kJSRegExpOffset));
3383 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3384 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3385 // Calculate number of capture registers (number_of_captures + 1) * 2.
3386 STATIC_ASSERT(kSmiTag == 0);
3387 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3388 __ add(Operand(edx), Immediate(2)); // edx was a smi.
3389
3390 // edx: Number of capture registers
3391 // Load last_match_info which is still known to be a fast case JSArray.
3392 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3393 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3394
3395 // ebx: last_match_info backing store (FixedArray)
3396 // edx: number of capture registers
3397 // Store the capture count.
3398 __ SmiTag(edx); // Number of capture registers to smi.
3399 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3400 __ SmiUntag(edx); // Number of capture registers back from smi.
3401 // Store last subject and last input.
3402 __ mov(eax, Operand(esp, kSubjectOffset));
3403 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
3404 __ mov(ecx, ebx);
3405 __ RecordWrite(ecx, RegExpImpl::kLastSubjectOffset, eax, edi);
3406 __ mov(eax, Operand(esp, kSubjectOffset));
3407 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
3408 __ mov(ecx, ebx);
3409 __ RecordWrite(ecx, RegExpImpl::kLastInputOffset, eax, edi);
3410
3411 // Get the static offsets vector filled by the native regexp code.
3412 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003413 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003414 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3415
3416 // ebx: last_match_info backing store (FixedArray)
3417 // ecx: offsets vector
3418 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003419 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003420 // Capture register counter starts from number of capture registers and
3421 // counts down until wraping after zero.
3422 __ bind(&next_capture);
3423 __ sub(Operand(edx), Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003424 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003425 // Read the value from the static offsets vector buffer.
3426 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3427 __ SmiTag(edi);
3428 // Store the smi value in the last match info.
3429 __ mov(FieldOperand(ebx,
3430 edx,
3431 times_pointer_size,
3432 RegExpImpl::kFirstCaptureOffset),
3433 edi);
3434 __ jmp(&next_capture);
3435 __ bind(&done);
3436
3437 // Return last match info.
3438 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3439 __ ret(4 * kPointerSize);
3440
3441 // Do the runtime call to execute the regexp.
3442 __ bind(&runtime);
3443 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3444#endif // V8_INTERPRETED_REGEXP
3445}
3446
3447
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003448void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3449 const int kMaxInlineLength = 100;
3450 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003451 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003452 __ mov(ebx, Operand(esp, kPointerSize * 3));
3453 __ test(ebx, Immediate(kSmiTagMask));
3454 __ j(not_zero, &slowcase);
3455 __ cmp(Operand(ebx), Immediate(Smi::FromInt(kMaxInlineLength)));
3456 __ j(above, &slowcase);
3457 // Smi-tagging is equivalent to multiplying by 2.
3458 STATIC_ASSERT(kSmiTag == 0);
3459 STATIC_ASSERT(kSmiTagSize == 1);
3460 // Allocate RegExpResult followed by FixedArray with size in ebx.
3461 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3462 // Elements: [Map][Length][..elements..]
3463 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3464 times_half_pointer_size,
3465 ebx, // In: Number of elements (times 2, being a smi)
3466 eax, // Out: Start of allocation (tagged).
3467 ecx, // Out: End of allocation.
3468 edx, // Scratch register
3469 &slowcase,
3470 TAG_OBJECT);
3471 // eax: Start of allocated area, object-tagged.
3472
3473 // Set JSArray map to global.regexp_result_map().
3474 // Set empty properties FixedArray.
3475 // Set elements to point to FixedArray allocated right after the JSArray.
3476 // Interleave operations for better latency.
3477 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003478 Factory* factory = masm->isolate()->factory();
3479 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003480 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
3481 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
3482 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
3483 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
3484 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
3485 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
3486
3487 // Set input, index and length fields from arguments.
3488 __ mov(ecx, Operand(esp, kPointerSize * 1));
3489 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
3490 __ mov(ecx, Operand(esp, kPointerSize * 2));
3491 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
3492 __ mov(ecx, Operand(esp, kPointerSize * 3));
3493 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
3494
3495 // Fill out the elements FixedArray.
3496 // eax: JSArray.
3497 // ebx: FixedArray.
3498 // ecx: Number of elements in array, as smi.
3499
3500 // Set map.
3501 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003502 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003503 // Set length.
3504 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
3505 // Fill contents of fixed-array with the-hole.
3506 __ SmiUntag(ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003507 __ mov(edx, Immediate(factory->the_hole_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003508 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
3509 // Fill fixed array elements with hole.
3510 // eax: JSArray.
3511 // ecx: Number of elements to fill.
3512 // ebx: Start of elements in FixedArray.
3513 // edx: the hole.
3514 Label loop;
3515 __ test(ecx, Operand(ecx));
3516 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003517 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003518 __ sub(Operand(ecx), Immediate(1));
3519 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
3520 __ jmp(&loop);
3521
3522 __ bind(&done);
3523 __ ret(3 * kPointerSize);
3524
3525 __ bind(&slowcase);
3526 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3527}
3528
3529
ricow@chromium.org65fae842010-08-25 15:26:24 +00003530void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3531 Register object,
3532 Register result,
3533 Register scratch1,
3534 Register scratch2,
3535 bool object_is_smi,
3536 Label* not_found) {
3537 // Use of registers. Register result is used as a temporary.
3538 Register number_string_cache = result;
3539 Register mask = scratch1;
3540 Register scratch = scratch2;
3541
3542 // Load the number string cache.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003543 ExternalReference roots_address =
3544 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003545 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
3546 __ mov(number_string_cache,
3547 Operand::StaticArray(scratch, times_pointer_size, roots_address));
3548 // Make the hash mask from the length of the number string cache. It
3549 // contains two elements (number and string) for each cache entry.
3550 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3551 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
3552 __ sub(Operand(mask), Immediate(1)); // Make mask.
3553
3554 // Calculate the entry in the number string cache. The hash value in the
3555 // number string cache for smis is just the smi value, and the hash for
3556 // doubles is the xor of the upper and lower words. See
3557 // Heap::GetNumberStringCache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003558 Label smi_hash_calculated;
3559 Label load_result_from_cache;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003560 if (object_is_smi) {
3561 __ mov(scratch, object);
3562 __ SmiUntag(scratch);
3563 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003564 Label not_smi;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003565 STATIC_ASSERT(kSmiTag == 0);
3566 __ test(object, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003567 __ j(not_zero, &not_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003568 __ mov(scratch, object);
3569 __ SmiUntag(scratch);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003570 __ jmp(&smi_hash_calculated, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003571 __ bind(&not_smi);
3572 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003573 masm->isolate()->factory()->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +00003574 __ j(not_equal, not_found);
3575 STATIC_ASSERT(8 == kDoubleSize);
3576 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3577 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3578 // Object is heap number and hash is now in scratch. Calculate cache index.
3579 __ and_(scratch, Operand(mask));
3580 Register index = scratch;
3581 Register probe = mask;
3582 __ mov(probe,
3583 FieldOperand(number_string_cache,
3584 index,
3585 times_twice_pointer_size,
3586 FixedArray::kHeaderSize));
3587 __ test(probe, Immediate(kSmiTagMask));
3588 __ j(zero, not_found);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003589 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003590 CpuFeatures::Scope fscope(SSE2);
3591 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3592 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3593 __ ucomisd(xmm0, xmm1);
3594 } else {
3595 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
3596 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
3597 __ FCmp();
3598 }
3599 __ j(parity_even, not_found); // Bail out if NaN is involved.
3600 __ j(not_equal, not_found); // The cache did not contain this value.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003601 __ jmp(&load_result_from_cache, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003602 }
3603
3604 __ bind(&smi_hash_calculated);
3605 // Object is smi and hash is now in scratch. Calculate cache index.
3606 __ and_(scratch, Operand(mask));
3607 Register index = scratch;
3608 // Check if the entry is the smi we are looking for.
3609 __ cmp(object,
3610 FieldOperand(number_string_cache,
3611 index,
3612 times_twice_pointer_size,
3613 FixedArray::kHeaderSize));
3614 __ j(not_equal, not_found);
3615
3616 // Get the result from the cache.
3617 __ bind(&load_result_from_cache);
3618 __ mov(result,
3619 FieldOperand(number_string_cache,
3620 index,
3621 times_twice_pointer_size,
3622 FixedArray::kHeaderSize + kPointerSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003623 Counters* counters = masm->isolate()->counters();
3624 __ IncrementCounter(counters->number_to_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003625}
3626
3627
3628void NumberToStringStub::Generate(MacroAssembler* masm) {
3629 Label runtime;
3630
3631 __ mov(ebx, Operand(esp, kPointerSize));
3632
3633 // Generate code to lookup number in the number string cache.
3634 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
3635 __ ret(1 * kPointerSize);
3636
3637 __ bind(&runtime);
3638 // Handle number to string in the runtime system if not found in the cache.
3639 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3640}
3641
3642
3643static int NegativeComparisonResult(Condition cc) {
3644 ASSERT(cc != equal);
3645 ASSERT((cc == less) || (cc == less_equal)
3646 || (cc == greater) || (cc == greater_equal));
3647 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3648}
3649
3650void CompareStub::Generate(MacroAssembler* masm) {
3651 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3652
3653 Label check_unequal_objects, done;
3654
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003655 // Compare two smis if required.
3656 if (include_smi_compare_) {
3657 Label non_smi, smi_done;
3658 __ mov(ecx, Operand(edx));
3659 __ or_(ecx, Operand(eax));
3660 __ test(ecx, Immediate(kSmiTagMask));
3661 __ j(not_zero, &non_smi, not_taken);
3662 __ sub(edx, Operand(eax)); // Return on the result of the subtraction.
3663 __ j(no_overflow, &smi_done);
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003664 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003665 __ bind(&smi_done);
3666 __ mov(eax, edx);
3667 __ ret(0);
3668 __ bind(&non_smi);
3669 } else if (FLAG_debug_code) {
3670 __ mov(ecx, Operand(edx));
3671 __ or_(ecx, Operand(eax));
3672 __ test(ecx, Immediate(kSmiTagMask));
3673 __ Assert(not_zero, "Unexpected smi operands.");
3674 }
3675
ricow@chromium.org65fae842010-08-25 15:26:24 +00003676 // NOTICE! This code is only reached after a smi-fast-case check, so
3677 // it is certain that at least one operand isn't a smi.
3678
3679 // Identical objects can be compared fast, but there are some tricky cases
3680 // for NaN and undefined.
3681 {
3682 Label not_identical;
3683 __ cmp(eax, Operand(edx));
3684 __ j(not_equal, &not_identical);
3685
3686 if (cc_ != equal) {
3687 // Check for undefined. undefined OP undefined is false even though
3688 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003689 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003690 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003691 __ j(not_equal, &check_for_nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003692 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3693 __ ret(0);
3694 __ bind(&check_for_nan);
3695 }
3696
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003697 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
ricow@chromium.org65fae842010-08-25 15:26:24 +00003698 // so we do the second best thing - test it ourselves.
3699 // Note: if cc_ != equal, never_nan_nan_ is not used.
3700 if (never_nan_nan_ && (cc_ == equal)) {
3701 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3702 __ ret(0);
3703 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003704 Label heap_number;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003705 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003706 Immediate(masm->isolate()->factory()->heap_number_map()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003707 __ j(equal, &heap_number, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003708 if (cc_ != equal) {
3709 // Call runtime on identical JSObjects. Otherwise return equal.
3710 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
3711 __ j(above_equal, &not_identical);
3712 }
3713 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3714 __ ret(0);
3715
3716 __ bind(&heap_number);
3717 // It is a heap number, so return non-equal if it's NaN and equal if
3718 // it's not NaN.
3719 // The representation of NaN values has all exponent bits (52..62) set,
3720 // and not all mantissa bits (0..51) clear.
3721 // We only accept QNaNs, which have bit 51 set.
3722 // Read top bits of double representation (second word of value).
3723
3724 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
3725 // all bits in the mask are set. We only need to check the word
3726 // that contains the exponent and high bit of the mantissa.
3727 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
3728 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003729 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003730 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
3731 // bits.
3732 __ add(edx, Operand(edx));
3733 __ cmp(edx, kQuietNaNHighBitsMask << 1);
3734 if (cc_ == equal) {
3735 STATIC_ASSERT(EQUAL != 1);
3736 __ setcc(above_equal, eax);
3737 __ ret(0);
3738 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003739 Label nan;
3740 __ j(above_equal, &nan, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003741 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3742 __ ret(0);
3743 __ bind(&nan);
3744 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3745 __ ret(0);
3746 }
3747 }
3748
3749 __ bind(&not_identical);
3750 }
3751
3752 // Strict equality can quickly decide whether objects are equal.
3753 // Non-strict object equality is slower, so it is handled later in the stub.
3754 if (cc_ == equal && strict_) {
3755 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003756 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003757 // If we're doing a strict equality comparison, we don't have to do
3758 // type conversion, so we generate code to do fast comparison for objects
3759 // and oddballs. Non-smi numbers and strings still go through the usual
3760 // slow-case code.
3761 // If either is a Smi (we know that not both are), then they can only
3762 // be equal if the other is a HeapNumber. If so, use the slow case.
3763 STATIC_ASSERT(kSmiTag == 0);
3764 ASSERT_EQ(0, Smi::FromInt(0));
3765 __ mov(ecx, Immediate(kSmiTagMask));
3766 __ and_(ecx, Operand(eax));
3767 __ test(ecx, Operand(edx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003768 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003769 // One operand is a smi.
3770
3771 // Check whether the non-smi is a heap number.
3772 STATIC_ASSERT(kSmiTagMask == 1);
3773 // ecx still holds eax & kSmiTag, which is either zero or one.
3774 __ sub(Operand(ecx), Immediate(0x01));
3775 __ mov(ebx, edx);
3776 __ xor_(ebx, Operand(eax));
3777 __ and_(ebx, Operand(ecx)); // ebx holds either 0 or eax ^ edx.
3778 __ xor_(ebx, Operand(eax));
3779 // if eax was smi, ebx is now edx, else eax.
3780
3781 // Check if the non-smi operand is a heap number.
3782 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003783 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003784 // If heap number, handle it in the slow case.
3785 __ j(equal, &slow);
3786 // Return non-equal (ebx is not zero)
3787 __ mov(eax, ebx);
3788 __ ret(0);
3789
3790 __ bind(&not_smis);
3791 // If either operand is a JSObject or an oddball value, then they are not
3792 // equal since their pointers are different
3793 // There is no test for undetectability in strict equality.
3794
3795 // Get the type of the first operand.
3796 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003797 Label first_non_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003798 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
3799 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003800 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003801
3802 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003803 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003804 STATIC_ASSERT(kHeapObjectTag != 0);
3805 __ bind(&return_not_equal);
3806 __ ret(0);
3807
3808 __ bind(&first_non_object);
3809 // Check for oddballs: true, false, null, undefined.
3810 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3811 __ j(equal, &return_not_equal);
3812
3813 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ecx);
3814 __ j(above_equal, &return_not_equal);
3815
3816 // Check for oddballs: true, false, null, undefined.
3817 __ CmpInstanceType(ecx, ODDBALL_TYPE);
3818 __ j(equal, &return_not_equal);
3819
3820 // Fall through to the general case.
3821 __ bind(&slow);
3822 }
3823
3824 // Generate the number comparison code.
3825 if (include_number_compare_) {
3826 Label non_number_comparison;
3827 Label unordered;
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003828 if (CpuFeatures::IsSupported(SSE2)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003829 CpuFeatures::Scope use_sse2(SSE2);
3830 CpuFeatures::Scope use_cmov(CMOV);
3831
3832 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
3833 __ ucomisd(xmm0, xmm1);
3834
3835 // Don't base result on EFLAGS when a NaN is involved.
3836 __ j(parity_even, &unordered, not_taken);
3837 // Return a result of -1, 0, or 1, based on EFLAGS.
3838 __ mov(eax, 0); // equal
3839 __ mov(ecx, Immediate(Smi::FromInt(1)));
3840 __ cmov(above, eax, Operand(ecx));
3841 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3842 __ cmov(below, eax, Operand(ecx));
3843 __ ret(0);
3844 } else {
3845 FloatingPointHelper::CheckFloatOperands(
3846 masm, &non_number_comparison, ebx);
3847 FloatingPointHelper::LoadFloatOperand(masm, eax);
3848 FloatingPointHelper::LoadFloatOperand(masm, edx);
3849 __ FCmp();
3850
3851 // Don't base result on EFLAGS when a NaN is involved.
3852 __ j(parity_even, &unordered, not_taken);
3853
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003854 Label below_label, above_label;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003855 // Return a result of -1, 0, or 1, based on EFLAGS.
3856 __ j(below, &below_label, not_taken);
3857 __ j(above, &above_label, not_taken);
3858
lrn@chromium.org5d00b602011-01-05 09:51:43 +00003859 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003860 __ ret(0);
3861
3862 __ bind(&below_label);
3863 __ mov(eax, Immediate(Smi::FromInt(-1)));
3864 __ ret(0);
3865
3866 __ bind(&above_label);
3867 __ mov(eax, Immediate(Smi::FromInt(1)));
3868 __ ret(0);
3869 }
3870
3871 // If one of the numbers was NaN, then the result is always false.
3872 // The cc is never not-equal.
3873 __ bind(&unordered);
3874 ASSERT(cc_ != not_equal);
3875 if (cc_ == less || cc_ == less_equal) {
3876 __ mov(eax, Immediate(Smi::FromInt(1)));
3877 } else {
3878 __ mov(eax, Immediate(Smi::FromInt(-1)));
3879 }
3880 __ ret(0);
3881
3882 // The number comparison code did not provide a valid result.
3883 __ bind(&non_number_comparison);
3884 }
3885
3886 // Fast negative check for symbol-to-symbol equality.
3887 Label check_for_strings;
3888 if (cc_ == equal) {
3889 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
3890 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
3891
3892 // We've already checked for object identity, so if both operands
3893 // are symbols they aren't equal. Register eax already holds a
3894 // non-zero value, which indicates not equal, so just return.
3895 __ ret(0);
3896 }
3897
3898 __ bind(&check_for_strings);
3899
3900 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
3901 &check_unequal_objects);
3902
3903 // Inline comparison of ascii strings.
lrn@chromium.org1c092762011-05-09 09:42:16 +00003904 if (cc_ == equal) {
3905 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00003906 edx,
3907 eax,
3908 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00003909 ebx);
3910 } else {
3911 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3912 edx,
3913 eax,
3914 ecx,
3915 ebx,
3916 edi);
3917 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003918#ifdef DEBUG
3919 __ Abort("Unexpected fall-through from string comparison");
3920#endif
3921
3922 __ bind(&check_unequal_objects);
3923 if (cc_ == equal && !strict_) {
3924 // Non-strict equality. Objects are unequal if
3925 // they are both JSObjects and not undetectable,
3926 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003927 Label not_both_objects;
3928 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003929 // At most one is a smi, so we can test for smi by adding the two.
3930 // A smi plus a heap object has the low bit set, a heap object plus
3931 // a heap object has the low bit clear.
3932 STATIC_ASSERT(kSmiTag == 0);
3933 STATIC_ASSERT(kSmiTagMask == 1);
3934 __ lea(ecx, Operand(eax, edx, times_1, 0));
3935 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003936 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003937 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003938 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003939 __ CmpObjectType(edx, FIRST_JS_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003940 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003941 // We do not bail out after this point. Both are JSObjects, and
3942 // they are equal if and only if both are undetectable.
3943 // The and of the undetectable flags is 1 if and only if they are equal.
3944 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
3945 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003946 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003947 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
3948 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003949 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003950 // The objects are both undetectable, so they both compare as the value
3951 // undefined, and are equal.
3952 __ Set(eax, Immediate(EQUAL));
3953 __ bind(&return_unequal);
3954 // Return non-equal by returning the non-zero object pointer in eax,
3955 // or return equal if we fell through to here.
3956 __ ret(0); // rax, rdx were pushed
3957 __ bind(&not_both_objects);
3958 }
3959
3960 // Push arguments below the return address.
3961 __ pop(ecx);
3962 __ push(edx);
3963 __ push(eax);
3964
3965 // Figure out which native to call and setup the arguments.
3966 Builtins::JavaScript builtin;
3967 if (cc_ == equal) {
3968 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3969 } else {
3970 builtin = Builtins::COMPARE;
3971 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
3972 }
3973
3974 // Restore return address on the stack.
3975 __ push(ecx);
3976
3977 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3978 // tagged as a small integer.
3979 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3980}
3981
3982
3983void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3984 Label* label,
3985 Register object,
3986 Register scratch) {
3987 __ test(object, Immediate(kSmiTagMask));
3988 __ j(zero, label);
3989 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
3990 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
3991 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
3992 __ cmp(scratch, kSymbolTag | kStringTag);
3993 __ j(not_equal, label);
3994}
3995
3996
3997void StackCheckStub::Generate(MacroAssembler* masm) {
whesse@chromium.org4a5224e2010-10-20 12:37:07 +00003998 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003999}
4000
4001
4002void CallFunctionStub::Generate(MacroAssembler* masm) {
4003 Label slow;
4004
4005 // If the receiver might be a value (string, number or boolean) check for this
4006 // and box it if it is.
4007 if (ReceiverMightBeValue()) {
4008 // Get the receiver from the stack.
4009 // +1 ~ return address
4010 Label receiver_is_value, receiver_is_js_object;
4011 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
4012
4013 // Check if receiver is a smi (which is a number value).
4014 __ test(eax, Immediate(kSmiTagMask));
4015 __ j(zero, &receiver_is_value, not_taken);
4016
4017 // Check if the receiver is a valid JS object.
4018 __ CmpObjectType(eax, FIRST_JS_OBJECT_TYPE, edi);
4019 __ j(above_equal, &receiver_is_js_object);
4020
4021 // Call the runtime to box the value.
4022 __ bind(&receiver_is_value);
4023 __ EnterInternalFrame();
4024 __ push(eax);
4025 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
4026 __ LeaveInternalFrame();
4027 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), eax);
4028
4029 __ bind(&receiver_is_js_object);
4030 }
4031
4032 // Get the function to call from the stack.
4033 // +2 ~ receiver, return address
4034 __ mov(edi, Operand(esp, (argc_ + 2) * kPointerSize));
4035
4036 // Check that the function really is a JavaScript function.
4037 __ test(edi, Immediate(kSmiTagMask));
4038 __ j(zero, &slow, not_taken);
4039 // Goto slow case if we do not have a function.
4040 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
4041 __ j(not_equal, &slow, not_taken);
4042
4043 // Fast-case: Just invoke the function.
4044 ParameterCount actual(argc_);
4045 __ InvokeFunction(edi, actual, JUMP_FUNCTION);
4046
4047 // Slow-case: Non-function called.
4048 __ bind(&slow);
4049 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4050 // of the original receiver from the call site).
4051 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4052 __ Set(eax, Immediate(argc_));
4053 __ Set(ebx, Immediate(0));
4054 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004055 Handle<Code> adaptor =
4056 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00004057 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4058}
4059
4060
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00004061bool CEntryStub::NeedsImmovableCode() {
4062 return false;
4063}
4064
4065
ricow@chromium.org65fae842010-08-25 15:26:24 +00004066void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004067 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004068}
4069
4070
ricow@chromium.org65fae842010-08-25 15:26:24 +00004071void CEntryStub::GenerateCore(MacroAssembler* masm,
4072 Label* throw_normal_exception,
4073 Label* throw_termination_exception,
4074 Label* throw_out_of_memory_exception,
4075 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00004076 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004077 // eax: result parameter for PerformGC, if any
4078 // ebx: pointer to C function (C callee-saved)
4079 // ebp: frame pointer (restored after C call)
4080 // esp: stack pointer (restored after C call)
4081 // edi: number of arguments including receiver (C callee-saved)
4082 // esi: pointer to the first argument (C callee-saved)
4083
4084 // Result returned in eax, or eax+edx if result_size_ is 2.
4085
4086 // Check stack alignment.
4087 if (FLAG_debug_code) {
4088 __ CheckStackAlignment();
4089 }
4090
4091 if (do_gc) {
4092 // Pass failure code returned from last attempt as first argument to
4093 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4094 // stack alignment is known to be correct. This function takes one argument
4095 // which is passed on the stack, and we know that the stack has been
4096 // prepared to pass at least one argument.
4097 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4098 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4099 }
4100
4101 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004102 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004103 if (always_allocate_scope) {
4104 __ inc(Operand::StaticVariable(scope_depth));
4105 }
4106
4107 // Call C function.
4108 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4109 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004110 __ mov(Operand(esp, 2 * kPointerSize),
4111 Immediate(ExternalReference::isolate_address()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004112 __ call(Operand(ebx));
4113 // Result is in eax or edx:eax - do not destroy these registers!
4114
4115 if (always_allocate_scope) {
4116 __ dec(Operand::StaticVariable(scope_depth));
4117 }
4118
4119 // Make sure we're not trying to return 'the hole' from the runtime
4120 // call as this may lead to crashes in the IC code later.
4121 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004122 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004123 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004124 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004125 __ int3();
4126 __ bind(&okay);
4127 }
4128
4129 // Check for failure result.
4130 Label failure_returned;
4131 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4132 __ lea(ecx, Operand(eax, 1));
4133 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4134 __ test(ecx, Immediate(kFailureTagMask));
4135 __ j(zero, &failure_returned, not_taken);
4136
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004137 ExternalReference pending_exception_address(
4138 Isolate::k_pending_exception_address, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004139
4140 // Check that there is no pending exception, otherwise we
4141 // should have returned some failure value.
4142 if (FLAG_debug_code) {
4143 __ push(edx);
4144 __ mov(edx, Operand::StaticVariable(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004145 ExternalReference::the_hole_value_location(masm->isolate())));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004146 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004147 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4148 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004149 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00004150 __ int3();
4151 __ bind(&okay);
4152 __ pop(edx);
4153 }
4154
ricow@chromium.org65fae842010-08-25 15:26:24 +00004155 // Exit the JavaScript to C++ exit frame.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004156 __ LeaveExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004157 __ ret(0);
4158
4159 // Handling of failure.
4160 __ bind(&failure_returned);
4161
4162 Label retry;
4163 // If the returned exception is RETRY_AFTER_GC continue at retry label
4164 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4165 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4166 __ j(zero, &retry, taken);
4167
4168 // Special handling of out of memory exceptions.
4169 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4170 __ j(equal, throw_out_of_memory_exception);
4171
4172 // Retrieve the pending exception and clear the variable.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004173 ExternalReference the_hole_location =
4174 ExternalReference::the_hole_value_location(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004175 __ mov(eax, Operand::StaticVariable(pending_exception_address));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004176 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004177 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4178
4179 // Special handling of termination exceptions which are uncatchable
4180 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004181 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004182 __ j(equal, throw_termination_exception);
4183
4184 // Handle normal exception.
4185 __ jmp(throw_normal_exception);
4186
4187 // Retry.
4188 __ bind(&retry);
4189}
4190
4191
4192void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4193 UncatchableExceptionType type) {
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00004194 __ ThrowUncatchable(type, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004195}
4196
4197
4198void CEntryStub::Generate(MacroAssembler* masm) {
4199 // eax: number of arguments including receiver
4200 // ebx: pointer to C function (C callee-saved)
4201 // ebp: frame pointer (restored after C call)
4202 // esp: stack pointer (restored after C call)
4203 // esi: current context (C callee-saved)
4204 // edi: JS function of the caller (C callee-saved)
4205
4206 // NOTE: Invocations of builtins may return failure objects instead
4207 // of a proper result. The builtin entry handles this by performing
4208 // a garbage collection and retrying the builtin (twice).
4209
4210 // Enter the exit frame that transitions from JavaScript to C++.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004211 __ EnterExitFrame(save_doubles_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004212
4213 // eax: result parameter for PerformGC, if any (setup below)
4214 // ebx: pointer to builtin function (C callee-saved)
4215 // ebp: frame pointer (restored after C call)
4216 // esp: stack pointer (restored after C call)
4217 // edi: number of arguments including receiver (C callee-saved)
4218 // esi: argv pointer (C callee-saved)
4219
4220 Label throw_normal_exception;
4221 Label throw_termination_exception;
4222 Label throw_out_of_memory_exception;
4223
4224 // Call into the runtime system.
4225 GenerateCore(masm,
4226 &throw_normal_exception,
4227 &throw_termination_exception,
4228 &throw_out_of_memory_exception,
4229 false,
4230 false);
4231
4232 // Do space-specific GC and retry runtime call.
4233 GenerateCore(masm,
4234 &throw_normal_exception,
4235 &throw_termination_exception,
4236 &throw_out_of_memory_exception,
4237 true,
4238 false);
4239
4240 // Do full GC and retry runtime call one final time.
4241 Failure* failure = Failure::InternalError();
4242 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4243 GenerateCore(masm,
4244 &throw_normal_exception,
4245 &throw_termination_exception,
4246 &throw_out_of_memory_exception,
4247 true,
4248 true);
4249
4250 __ bind(&throw_out_of_memory_exception);
4251 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4252
4253 __ bind(&throw_termination_exception);
4254 GenerateThrowUncatchable(masm, TERMINATION);
4255
4256 __ bind(&throw_normal_exception);
4257 GenerateThrowTOS(masm);
4258}
4259
4260
4261void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4262 Label invoke, exit;
4263#ifdef ENABLE_LOGGING_AND_PROFILING
4264 Label not_outermost_js, not_outermost_js_2;
4265#endif
4266
4267 // Setup frame.
4268 __ push(ebp);
4269 __ mov(ebp, Operand(esp));
4270
4271 // Push marker in two places.
4272 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4273 __ push(Immediate(Smi::FromInt(marker))); // context slot
4274 __ push(Immediate(Smi::FromInt(marker))); // function slot
4275 // Save callee-saved registers (C calling conventions).
4276 __ push(edi);
4277 __ push(esi);
4278 __ push(ebx);
4279
4280 // Save copies of the top frame descriptor on the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004281 ExternalReference c_entry_fp(Isolate::k_c_entry_fp_address, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004282 __ push(Operand::StaticVariable(c_entry_fp));
4283
4284#ifdef ENABLE_LOGGING_AND_PROFILING
4285 // If this is the outermost JS call, set js_entry_sp value.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004286 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address,
4287 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004288 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
4289 __ j(not_equal, &not_outermost_js);
4290 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
4291 __ bind(&not_outermost_js);
4292#endif
4293
4294 // Call a faked try-block that does the invoke.
4295 __ call(&invoke);
4296
4297 // Caught exception: Store result (exception) in the pending
4298 // exception field in the JSEnv and return a failure sentinel.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004299 ExternalReference pending_exception(Isolate::k_pending_exception_address,
4300 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004301 __ mov(Operand::StaticVariable(pending_exception), eax);
4302 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4303 __ jmp(&exit);
4304
4305 // Invoke: Link this frame into the handler chain.
4306 __ bind(&invoke);
4307 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4308
4309 // Clear any pending exceptions.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004310 ExternalReference the_hole_location =
4311 ExternalReference::the_hole_value_location(masm->isolate());
4312 __ mov(edx, Operand::StaticVariable(the_hole_location));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004313 __ mov(Operand::StaticVariable(pending_exception), edx);
4314
4315 // Fake a receiver (NULL).
4316 __ push(Immediate(0)); // receiver
4317
4318 // Invoke the function by calling through JS entry trampoline
4319 // builtin and pop the faked function when we return. Notice that we
4320 // cannot store a reference to the trampoline code directly in this
4321 // stub, because the builtin stubs may not have been generated yet.
4322 if (is_construct) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004323 ExternalReference construct_entry(
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004324 Builtins::kJSConstructEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004325 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004326 __ mov(edx, Immediate(construct_entry));
4327 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004328 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004329 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004330 __ mov(edx, Immediate(entry));
4331 }
4332 __ mov(edx, Operand(edx, 0)); // deref address
4333 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
4334 __ call(Operand(edx));
4335
4336 // Unlink this frame from the handler chain.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004337 __ pop(Operand::StaticVariable(ExternalReference(
4338 Isolate::k_handler_address,
4339 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004340 // Pop next_sp.
4341 __ add(Operand(esp), Immediate(StackHandlerConstants::kSize - kPointerSize));
4342
4343#ifdef ENABLE_LOGGING_AND_PROFILING
4344 // If current EBP value is the same as js_entry_sp value, it means that
4345 // the current function is the outermost.
4346 __ cmp(ebp, Operand::StaticVariable(js_entry_sp));
4347 __ j(not_equal, &not_outermost_js_2);
4348 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
4349 __ bind(&not_outermost_js_2);
4350#endif
4351
4352 // Restore the top frame descriptor from the stack.
4353 __ bind(&exit);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004354 __ pop(Operand::StaticVariable(ExternalReference(
4355 Isolate::k_c_entry_fp_address,
4356 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004357
4358 // Restore callee-saved registers (C calling conventions).
4359 __ pop(ebx);
4360 __ pop(esi);
4361 __ pop(edi);
4362 __ add(Operand(esp), Immediate(2 * kPointerSize)); // remove markers
4363
4364 // Restore frame pointer and return.
4365 __ pop(ebp);
4366 __ ret(0);
4367}
4368
4369
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004370// Generate stub code for instanceof.
4371// This code can patch a call site inlined cache of the instance of check,
4372// which looks like this.
4373//
4374// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
4375// 75 0a jne <some near label>
4376// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
4377//
4378// If call site patching is requested the stack will have the delta from the
4379// return address to the cmp instruction just below the return address. This
4380// also means that call site patching can only take place with arguments in
4381// registers. TOS looks like this when call site patching is requested
4382//
4383// esp[0] : return address
4384// esp[4] : delta from return address to cmp instruction
4385//
ricow@chromium.org65fae842010-08-25 15:26:24 +00004386void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004387 // Call site inlining and patching implies arguments in registers.
4388 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4389
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004390 // Fixed register usage throughout the stub.
4391 Register object = eax; // Object (lhs).
4392 Register map = ebx; // Map of the object.
4393 Register function = edx; // Function (rhs).
4394 Register prototype = edi; // Prototype of the function.
4395 Register scratch = ecx;
4396
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004397 // Constants describing the call site code to patch.
4398 static const int kDeltaToCmpImmediate = 2;
4399 static const int kDeltaToMov = 8;
4400 static const int kDeltaToMovImmediate = 9;
4401 static const int8_t kCmpEdiImmediateByte1 = BitCast<int8_t, uint8_t>(0x81);
4402 static const int8_t kCmpEdiImmediateByte2 = BitCast<int8_t, uint8_t>(0xff);
4403 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
4404
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004405 ExternalReference roots_address =
4406 ExternalReference::roots_address(masm->isolate());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004407
4408 ASSERT_EQ(object.code(), InstanceofStub::left().code());
4409 ASSERT_EQ(function.code(), InstanceofStub::right().code());
4410
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004411 // Get the object and function - they are always both needed.
4412 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004413 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004414 __ mov(object, Operand(esp, 2 * kPointerSize));
4415 __ mov(function, Operand(esp, 1 * kPointerSize));
4416 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004417
4418 // Check that the left hand is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004419 __ test(object, Immediate(kSmiTagMask));
4420 __ j(zero, &not_js_object, not_taken);
4421 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004422
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004423 // If there is a call site cache don't look in the global cache, but do the
4424 // real lookup and update the call site cache.
4425 if (!HasCallSiteInlineCheck()) {
4426 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004427 Label miss;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004428 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4429 __ cmp(function,
4430 Operand::StaticArray(scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004431 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004432 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4433 __ cmp(map, Operand::StaticArray(
4434 scratch, times_pointer_size, roots_address));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004435 __ j(not_equal, &miss, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004436 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4437 __ mov(eax, Operand::StaticArray(
4438 scratch, times_pointer_size, roots_address));
4439 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
4440 __ bind(&miss);
4441 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004442
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004443 // Get the prototype of the function.
4444 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004445
4446 // Check that the function prototype is a JS object.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004447 __ test(prototype, Immediate(kSmiTagMask));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004448 __ j(zero, &slow, not_taken);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004449 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004450
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004451 // Update the global instanceof or call site inlined cache with the current
4452 // map and function. The cached answer will be set when it is known below.
4453 if (!HasCallSiteInlineCheck()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004454 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
4455 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address), map);
4456 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
4457 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_address),
4458 function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004459 } else {
4460 // The constants for the code patching are based on no push instructions
4461 // at the call site.
4462 ASSERT(HasArgsInRegisters());
4463 // Get return address and delta to inlined map check.
4464 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4465 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4466 if (FLAG_debug_code) {
4467 __ cmpb(Operand(scratch, 0), kCmpEdiImmediateByte1);
4468 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
4469 __ cmpb(Operand(scratch, 1), kCmpEdiImmediateByte2);
4470 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
4471 }
4472 __ mov(Operand(scratch, kDeltaToCmpImmediate), map);
4473 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004474
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004475 // Loop through the prototype chain of the object looking for the function
4476 // prototype.
4477 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004478 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004479 __ bind(&loop);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004480 __ cmp(scratch, Operand(prototype));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004481 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004482 Factory* factory = masm->isolate()->factory();
4483 __ cmp(Operand(scratch), Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004484 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004485 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4486 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004487 __ jmp(&loop);
4488
4489 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004490 if (!HasCallSiteInlineCheck()) {
4491 __ Set(eax, Immediate(0));
4492 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4493 __ mov(Operand::StaticArray(scratch,
4494 times_pointer_size, roots_address), eax);
4495 } else {
4496 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004497 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004498 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4499 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4500 if (FLAG_debug_code) {
4501 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4502 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4503 }
4504 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4505 if (!ReturnTrueFalseObject()) {
4506 __ Set(eax, Immediate(0));
4507 }
4508 }
4509 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004510
4511 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004512 if (!HasCallSiteInlineCheck()) {
4513 __ Set(eax, Immediate(Smi::FromInt(1)));
4514 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
4515 __ mov(Operand::StaticArray(
4516 scratch, times_pointer_size, roots_address), eax);
4517 } else {
4518 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004519 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004520 __ mov(scratch, Operand(esp, 0 * kPointerSize));
4521 __ sub(scratch, Operand(esp, 1 * kPointerSize));
4522 if (FLAG_debug_code) {
4523 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
4524 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4525 }
4526 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
4527 if (!ReturnTrueFalseObject()) {
4528 __ Set(eax, Immediate(Smi::FromInt(1)));
4529 }
4530 }
4531 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004532
4533 Label object_not_null, object_not_null_or_smi;
4534 __ bind(&not_js_object);
4535 // Before null, smi and string value checks, check that the rhs is a function
4536 // as for a non-function rhs an exception needs to be thrown.
4537 __ test(function, Immediate(kSmiTagMask));
4538 __ j(zero, &slow, not_taken);
4539 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
4540 __ j(not_equal, &slow, not_taken);
4541
4542 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004543 __ cmp(object, factory->null_value());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004544 __ j(not_equal, &object_not_null);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004545 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004546 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004547
4548 __ bind(&object_not_null);
4549 // Smi values is not instance of anything.
4550 __ test(object, Immediate(kSmiTagMask));
4551 __ j(not_zero, &object_not_null_or_smi, not_taken);
4552 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004553 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004554
4555 __ bind(&object_not_null_or_smi);
4556 // String values is not instance of anything.
4557 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
4558 __ j(NegateCondition(is_string), &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004559 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004560 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004561
4562 // Slow-case: Go through the JavaScript implementation.
4563 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004564 if (!ReturnTrueFalseObject()) {
4565 // Tail call the builtin which returns 0 or 1.
4566 if (HasArgsInRegisters()) {
4567 // Push arguments below return address.
4568 __ pop(scratch);
4569 __ push(object);
4570 __ push(function);
4571 __ push(scratch);
4572 }
4573 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4574 } else {
4575 // Call the builtin and convert 0/1 to true/false.
4576 __ EnterInternalFrame();
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004577 __ push(object);
4578 __ push(function);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004579 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4580 __ LeaveInternalFrame();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004581 Label true_value, done;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004582 __ test(eax, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004583 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004584 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004585 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004586 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004587 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004588 __ bind(&done);
4589 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00004590 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004591}
4592
4593
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00004594Register InstanceofStub::left() { return eax; }
4595
4596
4597Register InstanceofStub::right() { return edx; }
4598
4599
ricow@chromium.org65fae842010-08-25 15:26:24 +00004600int CompareStub::MinorKey() {
4601 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4602 // stubs the never NaN NaN condition is only taken into account if the
4603 // condition is equals.
4604 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4605 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4606 return ConditionField::encode(static_cast<unsigned>(cc_))
4607 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4608 | StrictField::encode(strict_)
4609 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004610 | IncludeNumberCompareField::encode(include_number_compare_)
4611 | IncludeSmiCompareField::encode(include_smi_compare_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004612}
4613
4614
4615// Unfortunately you have to run without snapshots to see most of these
4616// names in the profile since most compare stubs end up in the snapshot.
4617const char* CompareStub::GetName() {
4618 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4619
4620 if (name_ != NULL) return name_;
4621 const int kMaxNameLength = 100;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00004622 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4623 kMaxNameLength);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004624 if (name_ == NULL) return "OOM";
4625
4626 const char* cc_name;
4627 switch (cc_) {
4628 case less: cc_name = "LT"; break;
4629 case greater: cc_name = "GT"; break;
4630 case less_equal: cc_name = "LE"; break;
4631 case greater_equal: cc_name = "GE"; break;
4632 case equal: cc_name = "EQ"; break;
4633 case not_equal: cc_name = "NE"; break;
4634 default: cc_name = "UnknownCondition"; break;
4635 }
4636
4637 const char* strict_name = "";
4638 if (strict_ && (cc_ == equal || cc_ == not_equal)) {
4639 strict_name = "_STRICT";
4640 }
4641
4642 const char* never_nan_nan_name = "";
4643 if (never_nan_nan_ && (cc_ == equal || cc_ == not_equal)) {
4644 never_nan_nan_name = "_NO_NAN";
4645 }
4646
4647 const char* include_number_compare_name = "";
4648 if (!include_number_compare_) {
4649 include_number_compare_name = "_NO_NUMBER";
4650 }
4651
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004652 const char* include_smi_compare_name = "";
4653 if (!include_smi_compare_) {
4654 include_smi_compare_name = "_NO_SMI";
4655 }
4656
ricow@chromium.org65fae842010-08-25 15:26:24 +00004657 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004658 "CompareStub_%s%s%s%s%s",
ricow@chromium.org65fae842010-08-25 15:26:24 +00004659 cc_name,
4660 strict_name,
4661 never_nan_nan_name,
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00004662 include_number_compare_name,
4663 include_smi_compare_name);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004664 return name_;
4665}
4666
4667
4668// -------------------------------------------------------------------------
4669// StringCharCodeAtGenerator
4670
4671void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4672 Label flat_string;
4673 Label ascii_string;
4674 Label got_char_code;
4675
4676 // If the receiver is a smi trigger the non-string case.
4677 STATIC_ASSERT(kSmiTag == 0);
4678 __ test(object_, Immediate(kSmiTagMask));
4679 __ j(zero, receiver_not_string_);
4680
4681 // Fetch the instance type of the receiver into result register.
4682 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4683 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4684 // If the receiver is not a string trigger the non-string case.
4685 __ test(result_, Immediate(kIsNotStringMask));
4686 __ j(not_zero, receiver_not_string_);
4687
4688 // If the index is non-smi trigger the non-smi case.
4689 STATIC_ASSERT(kSmiTag == 0);
4690 __ test(index_, Immediate(kSmiTagMask));
4691 __ j(not_zero, &index_not_smi_);
4692
4693 // Put smi-tagged index into scratch register.
4694 __ mov(scratch_, index_);
4695 __ bind(&got_smi_index_);
4696
4697 // Check for index out of range.
4698 __ cmp(scratch_, FieldOperand(object_, String::kLengthOffset));
4699 __ j(above_equal, index_out_of_range_);
4700
4701 // We need special handling for non-flat strings.
4702 STATIC_ASSERT(kSeqStringTag == 0);
4703 __ test(result_, Immediate(kStringRepresentationMask));
4704 __ j(zero, &flat_string);
4705
4706 // Handle non-flat strings.
4707 __ test(result_, Immediate(kIsConsStringMask));
4708 __ j(zero, &call_runtime_);
4709
4710 // ConsString.
4711 // Check whether the right hand side is the empty string (i.e. if
4712 // this is really a flat string in a cons string). If that is not
4713 // the case we would rather go to the runtime system now to flatten
4714 // the string.
4715 __ cmp(FieldOperand(object_, ConsString::kSecondOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004716 Immediate(masm->isolate()->factory()->empty_string()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004717 __ j(not_equal, &call_runtime_);
4718 // Get the first of the two strings and load its instance type.
4719 __ mov(object_, FieldOperand(object_, ConsString::kFirstOffset));
4720 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4721 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4722 // If the first cons component is also non-flat, then go to runtime.
4723 STATIC_ASSERT(kSeqStringTag == 0);
4724 __ test(result_, Immediate(kStringRepresentationMask));
4725 __ j(not_zero, &call_runtime_);
4726
4727 // Check for 1-byte or 2-byte string.
4728 __ bind(&flat_string);
4729 STATIC_ASSERT(kAsciiStringTag != 0);
4730 __ test(result_, Immediate(kStringEncodingMask));
4731 __ j(not_zero, &ascii_string);
4732
4733 // 2-byte string.
4734 // Load the 2-byte character code into the result register.
4735 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
4736 __ movzx_w(result_, FieldOperand(object_,
4737 scratch_, times_1, // Scratch is smi-tagged.
4738 SeqTwoByteString::kHeaderSize));
4739 __ jmp(&got_char_code);
4740
4741 // ASCII string.
4742 // Load the byte into the result register.
4743 __ bind(&ascii_string);
4744 __ SmiUntag(scratch_);
4745 __ movzx_b(result_, FieldOperand(object_,
4746 scratch_, times_1,
4747 SeqAsciiString::kHeaderSize));
4748 __ bind(&got_char_code);
4749 __ SmiTag(result_);
4750 __ bind(&exit_);
4751}
4752
4753
4754void StringCharCodeAtGenerator::GenerateSlow(
4755 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4756 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4757
4758 // Index is not a smi.
4759 __ bind(&index_not_smi_);
4760 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004761 __ CheckMap(index_,
4762 masm->isolate()->factory()->heap_number_map(),
4763 index_not_number_,
4764 true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004765 call_helper.BeforeCall(masm);
4766 __ push(object_);
4767 __ push(index_);
4768 __ push(index_); // Consumed by runtime conversion function.
4769 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4770 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4771 } else {
4772 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4773 // NumberToSmi discards numbers that are not exact integers.
4774 __ CallRuntime(Runtime::kNumberToSmi, 1);
4775 }
4776 if (!scratch_.is(eax)) {
4777 // Save the conversion result before the pop instructions below
4778 // have a chance to overwrite it.
4779 __ mov(scratch_, eax);
4780 }
4781 __ pop(index_);
4782 __ pop(object_);
4783 // Reload the instance type.
4784 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
4785 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4786 call_helper.AfterCall(masm);
4787 // If index is still not a smi, it must be out of range.
4788 STATIC_ASSERT(kSmiTag == 0);
4789 __ test(scratch_, Immediate(kSmiTagMask));
4790 __ j(not_zero, index_out_of_range_);
4791 // Otherwise, return to the fast path.
4792 __ jmp(&got_smi_index_);
4793
4794 // Call runtime. We get here when the receiver is a string and the
4795 // index is a number, but the code of getting the actual character
4796 // is too complex (e.g., when the string needs to be flattened).
4797 __ bind(&call_runtime_);
4798 call_helper.BeforeCall(masm);
4799 __ push(object_);
4800 __ push(index_);
4801 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4802 if (!result_.is(eax)) {
4803 __ mov(result_, eax);
4804 }
4805 call_helper.AfterCall(masm);
4806 __ jmp(&exit_);
4807
4808 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4809}
4810
4811
4812// -------------------------------------------------------------------------
4813// StringCharFromCodeGenerator
4814
4815void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4816 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4817 STATIC_ASSERT(kSmiTag == 0);
4818 STATIC_ASSERT(kSmiShiftSize == 0);
4819 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4820 __ test(code_,
4821 Immediate(kSmiTagMask |
4822 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
4823 __ j(not_zero, &slow_case_, not_taken);
4824
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004825 Factory* factory = masm->isolate()->factory();
4826 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00004827 STATIC_ASSERT(kSmiTag == 0);
4828 STATIC_ASSERT(kSmiTagSize == 1);
4829 STATIC_ASSERT(kSmiShiftSize == 0);
4830 // At this point code register contains smi tagged ascii char code.
4831 __ mov(result_, FieldOperand(result_,
4832 code_, times_half_pointer_size,
4833 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004834 __ cmp(result_, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00004835 __ j(equal, &slow_case_, not_taken);
4836 __ bind(&exit_);
4837}
4838
4839
4840void StringCharFromCodeGenerator::GenerateSlow(
4841 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4842 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4843
4844 __ bind(&slow_case_);
4845 call_helper.BeforeCall(masm);
4846 __ push(code_);
4847 __ CallRuntime(Runtime::kCharFromCode, 1);
4848 if (!result_.is(eax)) {
4849 __ mov(result_, eax);
4850 }
4851 call_helper.AfterCall(masm);
4852 __ jmp(&exit_);
4853
4854 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4855}
4856
4857
4858// -------------------------------------------------------------------------
4859// StringCharAtGenerator
4860
4861void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4862 char_code_at_generator_.GenerateFast(masm);
4863 char_from_code_generator_.GenerateFast(masm);
4864}
4865
4866
4867void StringCharAtGenerator::GenerateSlow(
4868 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4869 char_code_at_generator_.GenerateSlow(masm, call_helper);
4870 char_from_code_generator_.GenerateSlow(masm, call_helper);
4871}
4872
4873
4874void StringAddStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004875 Label string_add_runtime, call_builtin;
4876 Builtins::JavaScript builtin_id = Builtins::ADD;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004877
4878 // Load the two arguments.
4879 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4880 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4881
4882 // Make sure that both arguments are strings if not known in advance.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004883 if (flags_ == NO_STRING_ADD_FLAGS) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00004884 __ test(eax, Immediate(kSmiTagMask));
4885 __ j(zero, &string_add_runtime);
4886 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
4887 __ j(above_equal, &string_add_runtime);
4888
4889 // First argument is a a string, test second.
4890 __ test(edx, Immediate(kSmiTagMask));
4891 __ j(zero, &string_add_runtime);
4892 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
4893 __ j(above_equal, &string_add_runtime);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004894 } else {
4895 // Here at least one of the arguments is definitely a string.
4896 // We convert the one that is not known to be a string.
4897 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4898 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4899 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
4900 &call_builtin);
4901 builtin_id = Builtins::STRING_ADD_RIGHT;
4902 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4903 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4904 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
4905 &call_builtin);
4906 builtin_id = Builtins::STRING_ADD_LEFT;
4907 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00004908 }
4909
4910 // Both arguments are strings.
4911 // eax: first string
4912 // edx: second string
4913 // Check if either of the strings are empty. In that case return the other.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004914 Label second_not_zero_length, both_not_zero_length;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004915 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
4916 STATIC_ASSERT(kSmiTag == 0);
4917 __ test(ecx, Operand(ecx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004918 __ j(not_zero, &second_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004919 // Second string is empty, result is first string which is already in eax.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004920 Counters* counters = masm->isolate()->counters();
4921 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004922 __ ret(2 * kPointerSize);
4923 __ bind(&second_not_zero_length);
4924 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
4925 STATIC_ASSERT(kSmiTag == 0);
4926 __ test(ebx, Operand(ebx));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004927 __ j(not_zero, &both_not_zero_length, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004928 // First string is empty, result is second string which is in edx.
4929 __ mov(eax, edx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004930 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004931 __ ret(2 * kPointerSize);
4932
4933 // Both strings are non-empty.
4934 // eax: first string
4935 // ebx: length of first string as a smi
4936 // ecx: length of second string as a smi
4937 // edx: second string
4938 // Look at the length of the result of adding the two strings.
4939 Label string_add_flat_result, longer_than_two;
4940 __ bind(&both_not_zero_length);
4941 __ add(ebx, Operand(ecx));
4942 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
4943 // Handle exceptionally long strings in the runtime system.
4944 __ j(overflow, &string_add_runtime);
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00004945 // Use the symbol table when adding two one character strings, as it
4946 // helps later optimizations to return a symbol here.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004947 __ cmp(Operand(ebx), Immediate(Smi::FromInt(2)));
4948 __ j(not_equal, &longer_than_two);
4949
4950 // Check that both strings are non-external ascii strings.
4951 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx,
4952 &string_add_runtime);
4953
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004954 // Get the two characters forming the new string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004955 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4956 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4957
4958 // Try to lookup two character string in symbol table. If it is not found
4959 // just allocate a new one.
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004960 Label make_two_character_string, make_two_character_string_no_reload;
ricow@chromium.org65fae842010-08-25 15:26:24 +00004961 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004962 masm, ebx, ecx, eax, edx, edi,
4963 &make_two_character_string_no_reload, &make_two_character_string);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004964 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004965 __ ret(2 * kPointerSize);
4966
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004967 // Allocate a two character string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00004968 __ bind(&make_two_character_string);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004969 // Reload the arguments.
4970 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
4971 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
4972 // Get the two characters forming the new string.
4973 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
4974 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
4975 __ bind(&make_two_character_string_no_reload);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004976 __ IncrementCounter(counters->string_add_make_two_char(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004977 __ AllocateAsciiString(eax, // Result.
4978 2, // Length.
4979 edi, // Scratch 1.
4980 edx, // Scratch 2.
4981 &string_add_runtime);
4982 // Pack both characters in ebx.
4983 __ shl(ecx, kBitsPerByte);
4984 __ or_(ebx, Operand(ecx));
4985 // Set the characters in the new string.
4986 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00004987 __ IncrementCounter(counters->string_add_native(), 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00004988 __ ret(2 * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00004989
4990 __ bind(&longer_than_two);
4991 // Check if resulting string will be flat.
4992 __ cmp(Operand(ebx), Immediate(Smi::FromInt(String::kMinNonFlatLength)));
4993 __ j(below, &string_add_flat_result);
4994
4995 // If result is not supposed to be flat allocate a cons string object. If both
4996 // strings are ascii the result is an ascii cons string.
4997 Label non_ascii, allocated, ascii_data;
4998 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
4999 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5000 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5001 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5002 __ and_(ecx, Operand(edi));
5003 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5004 __ test(ecx, Immediate(kAsciiStringTag));
5005 __ j(zero, &non_ascii);
5006 __ bind(&ascii_data);
5007 // Allocate an acsii cons string.
5008 __ AllocateAsciiConsString(ecx, edi, no_reg, &string_add_runtime);
5009 __ bind(&allocated);
5010 // Fill the fields of the cons string.
5011 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5012 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5013 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5014 Immediate(String::kEmptyHashField));
5015 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5016 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5017 __ mov(eax, ecx);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005018 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005019 __ ret(2 * kPointerSize);
5020 __ bind(&non_ascii);
5021 // At least one of the strings is two-byte. Check whether it happens
5022 // to contain only ascii characters.
5023 // ecx: first instance type AND second instance type.
5024 // edi: second instance type.
5025 __ test(ecx, Immediate(kAsciiDataHintMask));
5026 __ j(not_zero, &ascii_data);
5027 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5028 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5029 __ xor_(edi, Operand(ecx));
5030 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5031 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5032 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5033 __ j(equal, &ascii_data);
5034 // Allocate a two byte cons string.
5035 __ AllocateConsString(ecx, edi, no_reg, &string_add_runtime);
5036 __ jmp(&allocated);
5037
5038 // Handle creating a flat result. First check that both strings are not
5039 // external strings.
5040 // eax: first string
5041 // ebx: length of resulting flat string as a smi
5042 // edx: second string
5043 __ bind(&string_add_flat_result);
5044 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5045 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5046 __ and_(ecx, kStringRepresentationMask);
5047 __ cmp(ecx, kExternalStringTag);
5048 __ j(equal, &string_add_runtime);
5049 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5050 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
5051 __ and_(ecx, kStringRepresentationMask);
5052 __ cmp(ecx, kExternalStringTag);
5053 __ j(equal, &string_add_runtime);
5054 // Now check if both strings are ascii strings.
5055 // eax: first string
5056 // ebx: length of resulting flat string as a smi
5057 // edx: second string
5058 Label non_ascii_string_add_flat_result;
5059 STATIC_ASSERT(kStringEncodingMask == kAsciiStringTag);
5060 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5061 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5062 __ j(zero, &non_ascii_string_add_flat_result);
5063 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5064 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5065 __ j(zero, &string_add_runtime);
5066
ricow@chromium.org65fae842010-08-25 15:26:24 +00005067 // Both strings are ascii strings. As they are short they are both flat.
5068 // ebx: length of resulting flat string as a smi
5069 __ SmiUntag(ebx);
5070 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5071 // eax: result string
5072 __ mov(ecx, eax);
5073 // Locate first character of result.
5074 __ add(Operand(ecx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5075 // Load first argument and locate first character.
5076 __ mov(edx, Operand(esp, 2 * kPointerSize));
5077 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5078 __ SmiUntag(edi);
5079 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5080 // eax: result string
5081 // ecx: first character of result
5082 // edx: first char of first argument
5083 // edi: length of first argument
5084 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
5085 // Load second argument and locate first character.
5086 __ mov(edx, Operand(esp, 1 * kPointerSize));
5087 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5088 __ SmiUntag(edi);
5089 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5090 // eax: result string
5091 // ecx: next character of result
5092 // edx: first char of second argument
5093 // edi: length of second argument
5094 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005095 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005096 __ ret(2 * kPointerSize);
5097
5098 // Handle creating a flat two byte result.
5099 // eax: first string - known to be two byte
5100 // ebx: length of resulting flat string as a smi
5101 // edx: second string
5102 __ bind(&non_ascii_string_add_flat_result);
5103 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
5104 __ test_b(FieldOperand(ecx, Map::kInstanceTypeOffset), kAsciiStringTag);
5105 __ j(not_zero, &string_add_runtime);
5106 // Both strings are two byte strings. As they are short they are both
5107 // flat.
5108 __ SmiUntag(ebx);
5109 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &string_add_runtime);
5110 // eax: result string
5111 __ mov(ecx, eax);
5112 // Locate first character of result.
5113 __ add(Operand(ecx),
5114 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5115 // Load first argument and locate first character.
5116 __ mov(edx, Operand(esp, 2 * kPointerSize));
5117 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5118 __ SmiUntag(edi);
5119 __ add(Operand(edx),
5120 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5121 // eax: result string
5122 // ecx: first character of result
5123 // edx: first char of first argument
5124 // edi: length of first argument
5125 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
5126 // Load second argument and locate first character.
5127 __ mov(edx, Operand(esp, 1 * kPointerSize));
5128 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5129 __ SmiUntag(edi);
5130 __ add(Operand(edx), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5131 // eax: result string
5132 // ecx: next character of result
5133 // edx: first char of second argument
5134 // edi: length of second argument
5135 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005136 __ IncrementCounter(counters->string_add_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005137 __ ret(2 * kPointerSize);
5138
5139 // Just jump to runtime to add the two strings.
5140 __ bind(&string_add_runtime);
5141 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005142
5143 if (call_builtin.is_linked()) {
5144 __ bind(&call_builtin);
5145 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5146 }
5147}
5148
5149
5150void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5151 int stack_offset,
5152 Register arg,
5153 Register scratch1,
5154 Register scratch2,
5155 Register scratch3,
5156 Label* slow) {
5157 // First check if the argument is already a string.
5158 Label not_string, done;
5159 __ test(arg, Immediate(kSmiTagMask));
5160 __ j(zero, &not_string);
5161 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5162 __ j(below, &done);
5163
5164 // Check the number to string cache.
5165 Label not_cached;
5166 __ bind(&not_string);
5167 // Puts the cached result into scratch1.
5168 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5169 arg,
5170 scratch1,
5171 scratch2,
5172 scratch3,
5173 false,
5174 &not_cached);
5175 __ mov(arg, scratch1);
5176 __ mov(Operand(esp, stack_offset), arg);
5177 __ jmp(&done);
5178
5179 // Check if the argument is a safe string wrapper.
5180 __ bind(&not_cached);
5181 __ test(arg, Immediate(kSmiTagMask));
5182 __ j(zero, slow);
5183 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5184 __ j(not_equal, slow);
5185 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5186 1 << Map::kStringWrapperSafeForDefaultValueOf);
5187 __ j(zero, slow);
5188 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5189 __ mov(Operand(esp, stack_offset), arg);
5190
5191 __ bind(&done);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005192}
5193
5194
5195void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5196 Register dest,
5197 Register src,
5198 Register count,
5199 Register scratch,
5200 bool ascii) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005201 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005202 __ bind(&loop);
5203 // This loop just copies one character at a time, as it is only used for very
5204 // short strings.
5205 if (ascii) {
5206 __ mov_b(scratch, Operand(src, 0));
5207 __ mov_b(Operand(dest, 0), scratch);
5208 __ add(Operand(src), Immediate(1));
5209 __ add(Operand(dest), Immediate(1));
5210 } else {
5211 __ mov_w(scratch, Operand(src, 0));
5212 __ mov_w(Operand(dest, 0), scratch);
5213 __ add(Operand(src), Immediate(2));
5214 __ add(Operand(dest), Immediate(2));
5215 }
5216 __ sub(Operand(count), Immediate(1));
5217 __ j(not_zero, &loop);
5218}
5219
5220
5221void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5222 Register dest,
5223 Register src,
5224 Register count,
5225 Register scratch,
5226 bool ascii) {
5227 // Copy characters using rep movs of doublewords.
5228 // The destination is aligned on a 4 byte boundary because we are
5229 // copying to the beginning of a newly allocated string.
5230 ASSERT(dest.is(edi)); // rep movs destination
5231 ASSERT(src.is(esi)); // rep movs source
5232 ASSERT(count.is(ecx)); // rep movs count
5233 ASSERT(!scratch.is(dest));
5234 ASSERT(!scratch.is(src));
5235 ASSERT(!scratch.is(count));
5236
5237 // Nothing to do for zero characters.
5238 Label done;
5239 __ test(count, Operand(count));
5240 __ j(zero, &done);
5241
5242 // Make count the number of bytes to copy.
5243 if (!ascii) {
5244 __ shl(count, 1);
5245 }
5246
5247 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005248 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005249 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005250 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005251
5252 // Copy from edi to esi using rep movs instruction.
5253 __ mov(scratch, count);
5254 __ sar(count, 2); // Number of doublewords to copy.
5255 __ cld();
5256 __ rep_movs();
5257
5258 // Find number of bytes left.
5259 __ mov(count, scratch);
5260 __ and_(count, 3);
5261
5262 // Check if there are more bytes to copy.
5263 __ bind(&last_bytes);
5264 __ test(count, Operand(count));
5265 __ j(zero, &done);
5266
5267 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005268 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005269 __ bind(&loop);
5270 __ mov_b(scratch, Operand(src, 0));
5271 __ mov_b(Operand(dest, 0), scratch);
5272 __ add(Operand(src), Immediate(1));
5273 __ add(Operand(dest), Immediate(1));
5274 __ sub(Operand(count), Immediate(1));
5275 __ j(not_zero, &loop);
5276
5277 __ bind(&done);
5278}
5279
5280
5281void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5282 Register c1,
5283 Register c2,
5284 Register scratch1,
5285 Register scratch2,
5286 Register scratch3,
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005287 Label* not_probed,
ricow@chromium.org65fae842010-08-25 15:26:24 +00005288 Label* not_found) {
5289 // Register scratch3 is the general scratch register in this function.
5290 Register scratch = scratch3;
5291
5292 // Make sure that both characters are not digits as such strings has a
5293 // different hash algorithm. Don't try to look for these in the symbol table.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005294 Label not_array_index;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005295 __ mov(scratch, c1);
5296 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5297 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005298 __ j(above, &not_array_index, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005299 __ mov(scratch, c2);
5300 __ sub(Operand(scratch), Immediate(static_cast<int>('0')));
5301 __ cmp(Operand(scratch), Immediate(static_cast<int>('9' - '0')));
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005302 __ j(below_equal, not_probed);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005303
5304 __ bind(&not_array_index);
5305 // Calculate the two character string hash.
5306 Register hash = scratch1;
5307 GenerateHashInit(masm, hash, c1, scratch);
5308 GenerateHashAddCharacter(masm, hash, c2, scratch);
5309 GenerateHashGetHash(masm, hash, scratch);
5310
5311 // Collect the two characters in a register.
5312 Register chars = c1;
5313 __ shl(c2, kBitsPerByte);
5314 __ or_(chars, Operand(c2));
5315
5316 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5317 // hash: hash of two character string.
5318
5319 // Load the symbol table.
5320 Register symbol_table = c2;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00005321 ExternalReference roots_address =
5322 ExternalReference::roots_address(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005323 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5324 __ mov(symbol_table,
5325 Operand::StaticArray(scratch, times_pointer_size, roots_address));
5326
5327 // Calculate capacity mask from the symbol table capacity.
5328 Register mask = scratch2;
5329 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5330 __ SmiUntag(mask);
5331 __ sub(Operand(mask), Immediate(1));
5332
5333 // Registers
5334 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5335 // hash: hash of two character string
5336 // symbol_table: symbol table
5337 // mask: capacity mask
5338 // scratch: -
5339
5340 // Perform a number of probes in the symbol table.
5341 static const int kProbes = 4;
5342 Label found_in_symbol_table;
5343 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
5344 for (int i = 0; i < kProbes; i++) {
5345 // Calculate entry in symbol table.
5346 __ mov(scratch, hash);
5347 if (i > 0) {
5348 __ add(Operand(scratch), Immediate(SymbolTable::GetProbeOffset(i)));
5349 }
5350 __ and_(scratch, Operand(mask));
5351
5352 // Load the entry from the symbol table.
5353 Register candidate = scratch; // Scratch register contains candidate.
5354 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5355 __ mov(candidate,
5356 FieldOperand(symbol_table,
5357 scratch,
5358 times_pointer_size,
5359 SymbolTable::kElementsStartOffset));
5360
5361 // If entry is undefined no string with this hash can be found.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005362 Factory* factory = masm->isolate()->factory();
5363 __ cmp(candidate, factory->undefined_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00005364 __ j(equal, not_found);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005365 __ cmp(candidate, factory->null_value());
ricow@chromium.orgbadaffc2011-03-17 12:15:27 +00005366 __ j(equal, &next_probe[i]);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005367
5368 // If length is not 2 the string is not a candidate.
5369 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5370 Immediate(Smi::FromInt(2)));
5371 __ j(not_equal, &next_probe[i]);
5372
5373 // As we are out of registers save the mask on the stack and use that
5374 // register as a temporary.
5375 __ push(mask);
5376 Register temp = mask;
5377
5378 // Check that the candidate is a non-external ascii string.
5379 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
5380 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
5381 __ JumpIfInstanceTypeIsNotSequentialAscii(
5382 temp, temp, &next_probe_pop_mask[i]);
5383
5384 // Check if the two characters match.
5385 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
5386 __ and_(temp, 0x0000ffff);
5387 __ cmp(chars, Operand(temp));
5388 __ j(equal, &found_in_symbol_table);
5389 __ bind(&next_probe_pop_mask[i]);
5390 __ pop(mask);
5391 __ bind(&next_probe[i]);
5392 }
5393
5394 // No matching 2 character string found by probing.
5395 __ jmp(not_found);
5396
5397 // Scratch register contains result when we fall through to here.
5398 Register result = scratch;
5399 __ bind(&found_in_symbol_table);
5400 __ pop(mask); // Pop saved mask from the stack.
5401 if (!result.is(eax)) {
5402 __ mov(eax, result);
5403 }
5404}
5405
5406
5407void StringHelper::GenerateHashInit(MacroAssembler* masm,
5408 Register hash,
5409 Register character,
5410 Register scratch) {
5411 // hash = character + (character << 10);
5412 __ mov(hash, character);
5413 __ shl(hash, 10);
5414 __ add(hash, Operand(character));
5415 // hash ^= hash >> 6;
5416 __ mov(scratch, hash);
5417 __ sar(scratch, 6);
5418 __ xor_(hash, Operand(scratch));
5419}
5420
5421
5422void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5423 Register hash,
5424 Register character,
5425 Register scratch) {
5426 // hash += character;
5427 __ add(hash, Operand(character));
5428 // hash += hash << 10;
5429 __ mov(scratch, hash);
5430 __ shl(scratch, 10);
5431 __ add(hash, Operand(scratch));
5432 // hash ^= hash >> 6;
5433 __ mov(scratch, hash);
5434 __ sar(scratch, 6);
5435 __ xor_(hash, Operand(scratch));
5436}
5437
5438
5439void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5440 Register hash,
5441 Register scratch) {
5442 // hash += hash << 3;
5443 __ mov(scratch, hash);
5444 __ shl(scratch, 3);
5445 __ add(hash, Operand(scratch));
5446 // hash ^= hash >> 11;
5447 __ mov(scratch, hash);
5448 __ sar(scratch, 11);
5449 __ xor_(hash, Operand(scratch));
5450 // hash += hash << 15;
5451 __ mov(scratch, hash);
5452 __ shl(scratch, 15);
5453 __ add(hash, Operand(scratch));
5454
5455 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005456 Label hash_not_zero;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005457 __ test(hash, Operand(hash));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005458 __ j(not_zero, &hash_not_zero, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005459 __ mov(hash, Immediate(27));
5460 __ bind(&hash_not_zero);
5461}
5462
5463
5464void SubStringStub::Generate(MacroAssembler* masm) {
5465 Label runtime;
5466
5467 // Stack frame on entry.
5468 // esp[0]: return address
5469 // esp[4]: to
5470 // esp[8]: from
5471 // esp[12]: string
5472
5473 // Make sure first argument is a string.
5474 __ mov(eax, Operand(esp, 3 * kPointerSize));
5475 STATIC_ASSERT(kSmiTag == 0);
5476 __ test(eax, Immediate(kSmiTagMask));
5477 __ j(zero, &runtime);
5478 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
5479 __ j(NegateCondition(is_string), &runtime);
5480
5481 // eax: string
5482 // ebx: instance type
5483
5484 // Calculate length of sub string using the smi values.
5485 Label result_longer_than_two;
5486 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
5487 __ test(ecx, Immediate(kSmiTagMask));
5488 __ j(not_zero, &runtime);
5489 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
5490 __ test(edx, Immediate(kSmiTagMask));
5491 __ j(not_zero, &runtime);
5492 __ sub(ecx, Operand(edx));
5493 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
5494 Label return_eax;
5495 __ j(equal, &return_eax);
5496 // Special handling of sub-strings of length 1 and 2. One character strings
5497 // are handled in the runtime system (looked up in the single character
5498 // cache). Two character strings are looked for in the symbol cache.
5499 __ SmiUntag(ecx); // Result length is no longer smi.
5500 __ cmp(ecx, 2);
5501 __ j(greater, &result_longer_than_two);
5502 __ j(less, &runtime);
5503
5504 // Sub string of length 2 requested.
5505 // eax: string
5506 // ebx: instance type
5507 // ecx: sub string length (value is 2)
5508 // edx: from index (smi)
5509 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
5510
5511 // Get the two characters forming the sub string.
5512 __ SmiUntag(edx); // From index is no longer smi.
5513 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
5514 __ movzx_b(ecx,
5515 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
5516
5517 // Try to lookup two character string in symbol table.
5518 Label make_two_character_string;
5519 StringHelper::GenerateTwoCharacterSymbolTableProbe(
kmillikin@chromium.org3cdd9e12010-09-06 11:39:48 +00005520 masm, ebx, ecx, eax, edx, edi,
5521 &make_two_character_string, &make_two_character_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005522 __ ret(3 * kPointerSize);
5523
5524 __ bind(&make_two_character_string);
5525 // Setup registers for allocating the two character string.
5526 __ mov(eax, Operand(esp, 3 * kPointerSize));
5527 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
5528 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
5529 __ Set(ecx, Immediate(2));
5530
5531 __ bind(&result_longer_than_two);
5532 // eax: string
5533 // ebx: instance type
5534 // ecx: result string length
5535 // Check for flat ascii string
5536 Label non_ascii_flat;
5537 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &non_ascii_flat);
5538
5539 // Allocate the result.
5540 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime);
5541
5542 // eax: result string
5543 // ecx: result string length
5544 __ mov(edx, esi); // esi used by following code.
5545 // Locate first character of result.
5546 __ mov(edi, eax);
5547 __ add(Operand(edi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5548 // Load string argument and locate character of sub string start.
5549 __ mov(esi, Operand(esp, 3 * kPointerSize));
5550 __ add(Operand(esi), Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5551 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5552 __ SmiUntag(ebx);
5553 __ add(esi, Operand(ebx));
5554
5555 // eax: result string
5556 // ecx: result length
5557 // edx: original value of esi
5558 // edi: first character of result
5559 // esi: character of sub string start
5560 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
5561 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005562 Counters* counters = masm->isolate()->counters();
5563 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005564 __ ret(3 * kPointerSize);
5565
5566 __ bind(&non_ascii_flat);
5567 // eax: string
5568 // ebx: instance type & kStringRepresentationMask | kStringEncodingMask
5569 // ecx: result string length
5570 // Check for flat two byte string
5571 __ cmp(ebx, kSeqStringTag | kTwoByteStringTag);
5572 __ j(not_equal, &runtime);
5573
5574 // Allocate the result.
5575 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime);
5576
5577 // eax: result string
5578 // ecx: result string length
5579 __ mov(edx, esi); // esi used by following code.
5580 // Locate first character of result.
5581 __ mov(edi, eax);
5582 __ add(Operand(edi),
5583 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5584 // Load string argument and locate character of sub string start.
5585 __ mov(esi, Operand(esp, 3 * kPointerSize));
5586 __ add(Operand(esi),
5587 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5588 __ mov(ebx, Operand(esp, 2 * kPointerSize)); // from
5589 // As from is a smi it is 2 times the value which matches the size of a two
5590 // byte character.
5591 STATIC_ASSERT(kSmiTag == 0);
5592 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5593 __ add(esi, Operand(ebx));
5594
5595 // eax: result string
5596 // ecx: result length
5597 // edx: original value of esi
5598 // edi: first character of result
5599 // esi: character of sub string start
5600 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
5601 __ mov(esi, edx); // Restore esi.
5602
5603 __ bind(&return_eax);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005604 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005605 __ ret(3 * kPointerSize);
5606
5607 // Just jump to runtime to create the sub string.
5608 __ bind(&runtime);
5609 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5610}
5611
5612
lrn@chromium.org1c092762011-05-09 09:42:16 +00005613void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5614 Register left,
5615 Register right,
5616 Register scratch1,
5617 Register scratch2) {
5618 Register length = scratch1;
5619
5620 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005621 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005622 __ mov(length, FieldOperand(left, String::kLengthOffset));
5623 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005624 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005625 __ bind(&strings_not_equal);
5626 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
5627 __ ret(0);
5628
5629 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005630 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005631 __ bind(&check_zero_length);
5632 STATIC_ASSERT(kSmiTag == 0);
5633 __ test(length, Operand(length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005634 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005635 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5636 __ ret(0);
5637
5638 // Compare characters.
5639 __ bind(&compare_chars);
5640 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005641 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005642
5643 // Characters are equal.
5644 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5645 __ ret(0);
5646}
5647
5648
ricow@chromium.org65fae842010-08-25 15:26:24 +00005649void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5650 Register left,
5651 Register right,
5652 Register scratch1,
5653 Register scratch2,
5654 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005655 Counters* counters = masm->isolate()->counters();
5656 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005657
5658 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005659 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005660 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
5661 __ mov(scratch3, scratch1);
5662 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
5663
5664 Register length_delta = scratch3;
5665
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005666 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005667 // Right string is shorter. Change scratch1 to be length of right string.
5668 __ sub(scratch1, Operand(length_delta));
5669 __ bind(&left_shorter);
5670
5671 Register min_length = scratch1;
5672
5673 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005674 Label compare_lengths;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005675 __ test(min_length, Operand(min_length));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005676 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005677
lrn@chromium.org1c092762011-05-09 09:42:16 +00005678 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005679 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005680 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005681 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005682
5683 // Compare lengths - strings up to min-length are equal.
5684 __ bind(&compare_lengths);
5685 __ test(length_delta, Operand(length_delta));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005686 __ j(not_zero, &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005687
5688 // Result is EQUAL.
5689 STATIC_ASSERT(EQUAL == 0);
5690 STATIC_ASSERT(kSmiTag == 0);
5691 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5692 __ ret(0);
5693
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005694 Label result_greater;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005695 __ bind(&result_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005696 __ j(greater, &result_greater, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005697
5698 // Result is LESS.
5699 __ Set(eax, Immediate(Smi::FromInt(LESS)));
5700 __ ret(0);
5701
5702 // Result is GREATER.
5703 __ bind(&result_greater);
5704 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
5705 __ ret(0);
5706}
5707
5708
lrn@chromium.org1c092762011-05-09 09:42:16 +00005709void StringCompareStub::GenerateAsciiCharsCompareLoop(
5710 MacroAssembler* masm,
5711 Register left,
5712 Register right,
5713 Register length,
5714 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005715 Label* chars_not_equal,
5716 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00005717 // Change index to run from -length to -1 by adding length to string
5718 // start. This means that loop ends when index reaches zero, which
5719 // doesn't need an additional compare.
5720 __ SmiUntag(length);
5721 __ lea(left,
5722 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5723 __ lea(right,
5724 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5725 __ neg(length);
5726 Register index = length; // index = -length;
5727
5728 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005729 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005730 __ bind(&loop);
5731 __ mov_b(scratch, Operand(left, index, times_1, 0));
5732 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005733 __ j(not_equal, chars_not_equal, chars_not_equal_near);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005734 __ add(Operand(index), Immediate(1));
5735 __ j(not_zero, &loop);
5736}
5737
5738
ricow@chromium.org65fae842010-08-25 15:26:24 +00005739void StringCompareStub::Generate(MacroAssembler* masm) {
5740 Label runtime;
5741
5742 // Stack frame on entry.
5743 // esp[0]: return address
5744 // esp[4]: right string
5745 // esp[8]: left string
5746
5747 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
5748 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
5749
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005750 Label not_same;
ricow@chromium.org65fae842010-08-25 15:26:24 +00005751 __ cmp(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005752 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005753 STATIC_ASSERT(EQUAL == 0);
5754 STATIC_ASSERT(kSmiTag == 0);
5755 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00005756 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00005757 __ ret(2 * kPointerSize);
5758
5759 __ bind(&not_same);
5760
5761 // Check that both objects are sequential ascii strings.
5762 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
5763
5764 // Compare flat ascii strings.
5765 // Drop arguments from the stack.
5766 __ pop(ecx);
5767 __ add(Operand(esp), Immediate(2 * kPointerSize));
5768 __ push(ecx);
5769 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
5770
5771 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5772 // tagged as a small integer.
5773 __ bind(&runtime);
5774 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5775}
5776
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005777
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005778void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5779 ASSERT(state_ == CompareIC::SMIS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005780 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005781 __ mov(ecx, Operand(edx));
5782 __ or_(ecx, Operand(eax));
5783 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005784 __ j(not_zero, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005785
5786 if (GetCondition() == equal) {
5787 // For equality we do not care about the sign of the result.
5788 __ sub(eax, Operand(edx));
5789 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005790 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005791 __ sub(edx, Operand(eax));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005792 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005793 // Correct sign of result in case of overflow.
5794 __ not_(edx);
5795 __ bind(&done);
5796 __ mov(eax, edx);
5797 }
5798 __ ret(0);
5799
5800 __ bind(&miss);
5801 GenerateMiss(masm);
5802}
5803
5804
5805void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5806 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5807
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005808 Label generic_stub;
5809 Label unordered;
5810 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005811 __ mov(ecx, Operand(edx));
5812 __ and_(ecx, Operand(eax));
5813 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005814 __ j(zero, &generic_stub, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005815
5816 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005817 __ j(not_equal, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005818 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005819 __ j(not_equal, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005820
5821 // Inlining the double comparison and falling back to the general compare
5822 // stub if NaN is involved or SS2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00005823 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005824 CpuFeatures::Scope scope1(SSE2);
5825 CpuFeatures::Scope scope2(CMOV);
5826
5827 // Load left and right operand
5828 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
5829 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
5830
5831 // Compare operands
5832 __ ucomisd(xmm0, xmm1);
5833
5834 // Don't base result on EFLAGS when a NaN is involved.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005835 __ j(parity_even, &unordered, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005836
5837 // Return a result of -1, 0, or 1, based on EFLAGS.
5838 // Performing mov, because xor would destroy the flag register.
5839 __ mov(eax, 0); // equal
5840 __ mov(ecx, Immediate(Smi::FromInt(1)));
5841 __ cmov(above, eax, Operand(ecx));
5842 __ mov(ecx, Immediate(Smi::FromInt(-1)));
5843 __ cmov(below, eax, Operand(ecx));
5844 __ ret(0);
5845
5846 __ bind(&unordered);
5847 }
5848
5849 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5850 __ bind(&generic_stub);
5851 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5852
5853 __ bind(&miss);
5854 GenerateMiss(masm);
5855}
5856
5857
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005858void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5859 ASSERT(state_ == CompareIC::SYMBOLS);
5860 ASSERT(GetCondition() == equal);
5861
5862 // Registers containing left and right operands respectively.
5863 Register left = edx;
5864 Register right = eax;
5865 Register tmp1 = ecx;
5866 Register tmp2 = ebx;
5867
5868 // Check that both operands are heap objects.
5869 Label miss;
5870 __ mov(tmp1, Operand(left));
5871 STATIC_ASSERT(kSmiTag == 0);
5872 __ and_(tmp1, Operand(right));
5873 __ test(tmp1, Immediate(kSmiTagMask));
5874 __ j(zero, &miss, Label::kNear);
5875
5876 // Check that both operands are symbols.
5877 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5878 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5879 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5880 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5881 STATIC_ASSERT(kSymbolTag != 0);
5882 __ and_(tmp1, Operand(tmp2));
5883 __ test(tmp1, Immediate(kIsSymbolMask));
5884 __ j(zero, &miss, Label::kNear);
5885
5886 // Symbols are compared by identity.
5887 Label done;
5888 __ cmp(left, Operand(right));
5889 // Make sure eax is non-zero. At this point input operands are
5890 // guaranteed to be non-zero.
5891 ASSERT(right.is(eax));
5892 __ j(not_equal, &done, Label::kNear);
5893 STATIC_ASSERT(EQUAL == 0);
5894 STATIC_ASSERT(kSmiTag == 0);
5895 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5896 __ bind(&done);
5897 __ ret(0);
5898
5899 __ bind(&miss);
5900 GenerateMiss(masm);
5901}
5902
5903
lrn@chromium.org1c092762011-05-09 09:42:16 +00005904void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5905 ASSERT(state_ == CompareIC::STRINGS);
5906 ASSERT(GetCondition() == equal);
5907 Label miss;
5908
5909 // Registers containing left and right operands respectively.
5910 Register left = edx;
5911 Register right = eax;
5912 Register tmp1 = ecx;
5913 Register tmp2 = ebx;
5914 Register tmp3 = edi;
5915
5916 // Check that both operands are heap objects.
5917 __ mov(tmp1, Operand(left));
5918 STATIC_ASSERT(kSmiTag == 0);
5919 __ and_(tmp1, Operand(right));
5920 __ test(tmp1, Immediate(kSmiTagMask));
5921 __ j(zero, &miss);
5922
5923 // Check that both operands are strings. This leaves the instance
5924 // types loaded in tmp1 and tmp2.
5925 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5926 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5927 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5928 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5929 __ mov(tmp3, tmp1);
5930 STATIC_ASSERT(kNotStringTag != 0);
5931 __ or_(tmp3, Operand(tmp2));
5932 __ test(tmp3, Immediate(kIsNotStringMask));
5933 __ j(not_zero, &miss);
5934
5935 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005936 Label not_same;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005937 __ cmp(left, Operand(right));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005938 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005939 STATIC_ASSERT(EQUAL == 0);
5940 STATIC_ASSERT(kSmiTag == 0);
5941 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
5942 __ ret(0);
5943
5944 // Handle not identical strings.
5945 __ bind(&not_same);
5946
5947 // Check that both strings are symbols. If they are, we're done
5948 // because we already know they are not identical.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005949 Label do_compare;
lrn@chromium.org1c092762011-05-09 09:42:16 +00005950 STATIC_ASSERT(kSymbolTag != 0);
5951 __ and_(tmp1, Operand(tmp2));
5952 __ test(tmp1, Immediate(kIsSymbolMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005953 __ j(zero, &do_compare, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00005954 // Make sure eax is non-zero. At this point input operands are
5955 // guaranteed to be non-zero.
5956 ASSERT(right.is(eax));
5957 __ ret(0);
5958
5959 // Check that both strings are sequential ASCII.
5960 Label runtime;
5961 __ bind(&do_compare);
5962 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5963
5964 // Compare flat ASCII strings. Returns when done.
5965 StringCompareStub::GenerateFlatAsciiStringEquals(
5966 masm, left, right, tmp1, tmp2);
5967
5968 // Handle more complex cases in runtime.
5969 __ bind(&runtime);
5970 __ pop(tmp1); // Return address.
5971 __ push(left);
5972 __ push(right);
5973 __ push(tmp1);
5974 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5975
5976 __ bind(&miss);
5977 GenerateMiss(masm);
5978}
5979
5980
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005981void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5982 ASSERT(state_ == CompareIC::OBJECTS);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005983 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005984 __ mov(ecx, Operand(edx));
5985 __ and_(ecx, Operand(eax));
5986 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005987 __ j(zero, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005988
5989 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005990 __ j(not_equal, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005991 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005992 __ j(not_equal, &miss, not_taken, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00005993
5994 ASSERT(GetCondition() == equal);
5995 __ sub(eax, Operand(edx));
5996 __ ret(0);
5997
5998 __ bind(&miss);
5999 GenerateMiss(masm);
6000}
6001
6002
6003void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6004 // Save the registers.
6005 __ pop(ecx);
6006 __ push(edx);
6007 __ push(eax);
6008 __ push(ecx);
6009
6010 // Call the runtime system in a fresh internal frame.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00006011 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6012 masm->isolate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006013 __ EnterInternalFrame();
6014 __ push(edx);
6015 __ push(eax);
6016 __ push(Immediate(Smi::FromInt(op_)));
6017 __ CallExternalReference(miss, 3);
6018 __ LeaveInternalFrame();
6019
6020 // Compute the entry point of the rewritten stub.
6021 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6022
6023 // Restore registers.
6024 __ pop(ecx);
6025 __ pop(eax);
6026 __ pop(edx);
6027 __ push(ecx);
6028
6029 // Do a tail call to the rewritten stub.
6030 __ jmp(Operand(edi));
6031}
6032
6033
lrn@chromium.org1c092762011-05-09 09:42:16 +00006034// Helper function used to check that the dictionary doesn't contain
6035// the property. This function may return false negatives, so miss_label
6036// must always call a backup property check that is complete.
6037// This function is safe to call if the receiver has fast properties.
6038// Name must be a symbol and receiver must be a heap object.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006039MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
6040 MacroAssembler* masm,
6041 Label* miss,
6042 Label* done,
6043 Register properties,
6044 String* name,
6045 Register r0) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00006046 ASSERT(name->IsSymbol());
6047
6048 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6049 // not equal to the name and kProbes-th slot is not used (its name is the
6050 // undefined value), it guarantees the hash table doesn't contain the
6051 // property. It's true even if some slots represent deleted properties
6052 // (their names are the null value).
6053 for (int i = 0; i < kInlinedProbes; i++) {
6054 // Compute the masked index: (hash + i + i * i) & mask.
6055 Register index = r0;
6056 // Capacity is smi 2^n.
6057 __ mov(index, FieldOperand(properties, kCapacityOffset));
6058 __ dec(index);
6059 __ and_(Operand(index),
6060 Immediate(Smi::FromInt(name->Hash() +
6061 StringDictionary::GetProbeOffset(i))));
6062
6063 // Scale the index by multiplying by the entry size.
6064 ASSERT(StringDictionary::kEntrySize == 3);
6065 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
6066 Register entity_name = r0;
6067 // Having undefined at this place means the name is not contained.
6068 ASSERT_EQ(kSmiTagSize, 1);
6069 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
6070 kElementsStartOffset - kHeapObjectTag));
6071 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
6072 __ j(equal, done, taken);
6073
6074 // Stop if found the property.
6075 __ cmp(entity_name, Handle<String>(name));
6076 __ j(equal, miss, not_taken);
6077
6078 // Check if the entry name is not a symbol.
6079 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6080 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6081 kIsSymbolMask);
6082 __ j(zero, miss, not_taken);
6083 }
6084
6085 StringDictionaryLookupStub stub(properties,
6086 r0,
6087 r0,
6088 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6089 __ push(Immediate(Handle<Object>(name)));
6090 __ push(Immediate(name->Hash()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006091 MaybeObject* result = masm->TryCallStub(&stub);
6092 if (result->IsFailure()) return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006093 __ test(r0, Operand(r0));
6094 __ j(not_zero, miss);
6095 __ jmp(done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006096 return result;
lrn@chromium.org1c092762011-05-09 09:42:16 +00006097}
6098
6099
6100// Probe the string dictionary in the |elements| register. Jump to the
6101// |done| label if a property with the given name is found leaving the
6102// index into the dictionary in |r0|. Jump to the |miss| label
6103// otherwise.
6104void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6105 Label* miss,
6106 Label* done,
6107 Register elements,
6108 Register name,
6109 Register r0,
6110 Register r1) {
6111 // Assert that name contains a string.
6112 if (FLAG_debug_code) __ AbortIfNotString(name);
6113
6114 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6115 __ shr(r1, kSmiTagSize); // convert smi to int
6116 __ dec(r1);
6117
6118 // Generate an unrolled loop that performs a few probes before
6119 // giving up. Measurements done on Gmail indicate that 2 probes
6120 // cover ~93% of loads from dictionaries.
6121 for (int i = 0; i < kInlinedProbes; i++) {
6122 // Compute the masked index: (hash + i + i * i) & mask.
6123 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6124 __ shr(r0, String::kHashShift);
6125 if (i > 0) {
6126 __ add(Operand(r0), Immediate(StringDictionary::GetProbeOffset(i)));
6127 }
6128 __ and_(r0, Operand(r1));
6129
6130 // Scale the index by multiplying by the entry size.
6131 ASSERT(StringDictionary::kEntrySize == 3);
6132 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6133
6134 // Check if the key is identical to the name.
6135 __ cmp(name, Operand(elements,
6136 r0,
6137 times_4,
6138 kElementsStartOffset - kHeapObjectTag));
6139 __ j(equal, done, taken);
6140 }
6141
6142 StringDictionaryLookupStub stub(elements,
6143 r1,
6144 r0,
6145 POSITIVE_LOOKUP);
6146 __ push(name);
6147 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6148 __ shr(r0, String::kHashShift);
6149 __ push(r0);
6150 __ CallStub(&stub);
6151
6152 __ test(r1, Operand(r1));
6153 __ j(zero, miss);
6154 __ jmp(done);
6155}
6156
6157
6158void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6159 // Stack frame on entry:
6160 // esp[0 * kPointerSize]: return address.
6161 // esp[1 * kPointerSize]: key's hash.
6162 // esp[2 * kPointerSize]: key.
6163 // Registers:
6164 // dictionary_: StringDictionary to probe.
6165 // result_: used as scratch.
6166 // index_: will hold an index of entry if lookup is successful.
6167 // might alias with result_.
6168 // Returns:
6169 // result_ is zero if lookup failed, non zero otherwise.
6170
6171 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6172
6173 Register scratch = result_;
6174
6175 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6176 __ dec(scratch);
6177 __ SmiUntag(scratch);
6178 __ push(scratch);
6179
6180 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6181 // not equal to the name and kProbes-th slot is not used (its name is the
6182 // undefined value), it guarantees the hash table doesn't contain the
6183 // property. It's true even if some slots represent deleted properties
6184 // (their names are the null value).
6185 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6186 // Compute the masked index: (hash + i + i * i) & mask.
6187 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6188 if (i > 0) {
6189 __ add(Operand(scratch),
6190 Immediate(StringDictionary::GetProbeOffset(i)));
6191 }
6192 __ and_(scratch, Operand(esp, 0));
6193
6194 // Scale the index by multiplying by the entry size.
6195 ASSERT(StringDictionary::kEntrySize == 3);
6196 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6197
6198 // Having undefined at this place means the name is not contained.
6199 ASSERT_EQ(kSmiTagSize, 1);
6200 __ mov(scratch, Operand(dictionary_,
6201 index_,
6202 times_pointer_size,
6203 kElementsStartOffset - kHeapObjectTag));
6204 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6205 __ j(equal, &not_in_dictionary);
6206
6207 // Stop if found the property.
6208 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6209 __ j(equal, &in_dictionary);
6210
6211 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6212 // If we hit a non symbol key during negative lookup
6213 // we have to bailout as this key might be equal to the
6214 // key we are looking for.
6215
6216 // Check if the entry name is not a symbol.
6217 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6218 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6219 kIsSymbolMask);
6220 __ j(zero, &maybe_in_dictionary);
6221 }
6222 }
6223
6224 __ bind(&maybe_in_dictionary);
6225 // If we are doing negative lookup then probing failure should be
6226 // treated as a lookup success. For positive lookup probing failure
6227 // should be treated as lookup failure.
6228 if (mode_ == POSITIVE_LOOKUP) {
6229 __ mov(result_, Immediate(0));
6230 __ Drop(1);
6231 __ ret(2 * kPointerSize);
6232 }
6233
6234 __ bind(&in_dictionary);
6235 __ mov(result_, Immediate(1));
6236 __ Drop(1);
6237 __ ret(2 * kPointerSize);
6238
6239 __ bind(&not_in_dictionary);
6240 __ mov(result_, Immediate(0));
6241 __ Drop(1);
6242 __ ret(2 * kPointerSize);
6243}
6244
6245
ricow@chromium.org65fae842010-08-25 15:26:24 +00006246#undef __
6247
6248} } // namespace v8::internal
6249
6250#endif // V8_TARGET_ARCH_IA32