blob: 48b59996e9350e1a6dec91aad205d62f2f3edb76 [file] [log] [blame]
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001// Copyright 2012 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
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +000030#if V8_TARGET_ARCH_IA32
ricow@chromium.org65fae842010-08-25 15:26:24 +000031
32#include "bootstrapper.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000033#include "code-stubs.h"
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000034#include "isolate.h"
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000035#include "jsregexp.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000036#include "regexp-macro-assembler.h"
danno@chromium.org94b0d6f2013-02-04 13:33:20 +000037#include "runtime.h"
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +000038#include "stub-cache.h"
erikcorry0ad885c2011-11-21 13:51:57 +000039#include "codegen.h"
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +000040#include "runtime.h"
ricow@chromium.org65fae842010-08-25 15:26:24 +000041
42namespace v8 {
43namespace internal {
44
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000045
verwaest@chromium.org662436e2013-08-28 08:41:27 +000046void FastNewClosureStub::InitializeInterfaceDescriptor(
47 Isolate* isolate,
48 CodeStubInterfaceDescriptor* descriptor) {
49 static Register registers[] = { ebx };
50 descriptor->register_param_count_ = 1;
51 descriptor->register_params_ = registers;
52 descriptor->deoptimization_handler_ =
53 Runtime::FunctionForId(Runtime::kNewClosureFromStubFailure)->entry;
54}
55
56
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +000057void FastNewContextStub::InitializeInterfaceDescriptor(
58 Isolate* isolate,
59 CodeStubInterfaceDescriptor* descriptor) {
60 static Register registers[] = { edi };
61 descriptor->register_param_count_ = 1;
62 descriptor->register_params_ = registers;
63 descriptor->deoptimization_handler_ = NULL;
64}
65
66
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +000067void ToNumberStub::InitializeInterfaceDescriptor(
68 Isolate* isolate,
69 CodeStubInterfaceDescriptor* descriptor) {
70 static Register registers[] = { eax };
71 descriptor->register_param_count_ = 1;
72 descriptor->register_params_ = registers;
73 descriptor->deoptimization_handler_ = NULL;
74}
75
76
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +000077void NumberToStringStub::InitializeInterfaceDescriptor(
78 Isolate* isolate,
79 CodeStubInterfaceDescriptor* descriptor) {
80 static Register registers[] = { eax };
81 descriptor->register_param_count_ = 1;
82 descriptor->register_params_ = registers;
bmeurer@chromium.orge7a07452013-10-21 13:27:29 +000083 descriptor->deoptimization_handler_ =
84 Runtime::FunctionForId(Runtime::kNumberToString)->entry;
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +000085}
86
87
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000088void FastCloneShallowArrayStub::InitializeInterfaceDescriptor(
89 Isolate* isolate,
90 CodeStubInterfaceDescriptor* descriptor) {
91 static Register registers[] = { eax, ebx, ecx };
92 descriptor->register_param_count_ = 3;
93 descriptor->register_params_ = registers;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000094 descriptor->deoptimization_handler_ =
machenbach@chromium.org37be4082013-11-26 13:50:38 +000095 Runtime::FunctionForId(Runtime::kCreateArrayLiteralStubBailout)->entry;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000096}
97
98
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +000099void FastCloneShallowObjectStub::InitializeInterfaceDescriptor(
100 Isolate* isolate,
101 CodeStubInterfaceDescriptor* descriptor) {
102 static Register registers[] = { eax, ebx, ecx, edx };
103 descriptor->register_param_count_ = 4;
104 descriptor->register_params_ = registers;
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000105 descriptor->deoptimization_handler_ =
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000106 Runtime::FunctionForId(Runtime::kCreateObjectLiteral)->entry;
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +0000107}
108
109
danno@chromium.orgbee51992013-07-10 14:57:15 +0000110void CreateAllocationSiteStub::InitializeInterfaceDescriptor(
111 Isolate* isolate,
112 CodeStubInterfaceDescriptor* descriptor) {
113 static Register registers[] = { ebx };
114 descriptor->register_param_count_ = 1;
115 descriptor->register_params_ = registers;
116 descriptor->deoptimization_handler_ = NULL;
117}
118
119
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000120void KeyedLoadFastElementStub::InitializeInterfaceDescriptor(
121 Isolate* isolate,
122 CodeStubInterfaceDescriptor* descriptor) {
123 static Register registers[] = { edx, ecx };
124 descriptor->register_param_count_ = 2;
125 descriptor->register_params_ = registers;
126 descriptor->deoptimization_handler_ =
mstarzinger@chromium.orge3b8d0f2013-02-01 09:06:41 +0000127 FUNCTION_ADDR(KeyedLoadIC_MissFromStubFailure);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000128}
129
130
machenbach@chromium.orgea468882013-11-18 08:53:19 +0000131void KeyedLoadDictionaryElementStub::InitializeInterfaceDescriptor(
132 Isolate* isolate,
133 CodeStubInterfaceDescriptor* descriptor) {
134 static Register registers[] = { edx, ecx };
135 descriptor->register_param_count_ = 2;
136 descriptor->register_params_ = registers;
137 descriptor->deoptimization_handler_ =
138 FUNCTION_ADDR(KeyedLoadIC_MissFromStubFailure);
139}
140
141
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000142void LoadFieldStub::InitializeInterfaceDescriptor(
143 Isolate* isolate,
144 CodeStubInterfaceDescriptor* descriptor) {
145 static Register registers[] = { edx };
146 descriptor->register_param_count_ = 1;
147 descriptor->register_params_ = registers;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000148 descriptor->deoptimization_handler_ = NULL;
149}
150
151
152void KeyedLoadFieldStub::InitializeInterfaceDescriptor(
153 Isolate* isolate,
154 CodeStubInterfaceDescriptor* descriptor) {
155 static Register registers[] = { edx };
156 descriptor->register_param_count_ = 1;
157 descriptor->register_params_ = registers;
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000158 descriptor->deoptimization_handler_ = NULL;
159}
160
161
machenbach@chromium.orgea468882013-11-18 08:53:19 +0000162void KeyedArrayCallStub::InitializeInterfaceDescriptor(
163 Isolate* isolate,
164 CodeStubInterfaceDescriptor* descriptor) {
165 static Register registers[] = { ecx };
166 descriptor->register_param_count_ = 1;
167 descriptor->register_params_ = registers;
168 descriptor->continuation_type_ = TAIL_CALL_CONTINUATION;
169 descriptor->handler_arguments_mode_ = PASS_ARGUMENTS;
170 descriptor->deoptimization_handler_ =
171 FUNCTION_ADDR(KeyedCallIC_MissFromStubFailure);
172}
173
174
jkummerow@chromium.org7bd87f02013-03-20 18:06:29 +0000175void KeyedStoreFastElementStub::InitializeInterfaceDescriptor(
176 Isolate* isolate,
177 CodeStubInterfaceDescriptor* descriptor) {
178 static Register registers[] = { edx, ecx, eax };
179 descriptor->register_param_count_ = 3;
180 descriptor->register_params_ = registers;
181 descriptor->deoptimization_handler_ =
182 FUNCTION_ADDR(KeyedStoreIC_MissFromStubFailure);
183}
184
185
danno@chromium.org94b0d6f2013-02-04 13:33:20 +0000186void TransitionElementsKindStub::InitializeInterfaceDescriptor(
187 Isolate* isolate,
188 CodeStubInterfaceDescriptor* descriptor) {
189 static Register registers[] = { eax, ebx };
190 descriptor->register_param_count_ = 2;
191 descriptor->register_params_ = registers;
192 descriptor->deoptimization_handler_ =
193 Runtime::FunctionForId(Runtime::kTransitionElementsKind)->entry;
194}
195
196
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000197static void InitializeArrayConstructorDescriptor(
198 Isolate* isolate,
199 CodeStubInterfaceDescriptor* descriptor,
200 int constant_stack_parameter_count) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000201 // register state
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000202 // eax -- number of arguments
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000203 // edi -- function
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +0000204 // ebx -- allocation site with elements kind
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000205 static Register registers_variable_args[] = { edi, ebx, eax };
206 static Register registers_no_args[] = { edi, ebx };
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000207
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000208 if (constant_stack_parameter_count == 0) {
209 descriptor->register_param_count_ = 2;
210 descriptor->register_params_ = registers_no_args;
211 } else {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000212 // stack param count needs (constructor pointer, and single argument)
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000213 descriptor->handler_arguments_mode_ = PASS_ARGUMENTS;
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000214 descriptor->stack_parameter_count_ = eax;
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000215 descriptor->register_param_count_ = 3;
216 descriptor->register_params_ = registers_variable_args;
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000217 }
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000218
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000219 descriptor->hint_stack_parameter_count_ = constant_stack_parameter_count;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000220 descriptor->function_mode_ = JS_FUNCTION_STUB_MODE;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000221 descriptor->deoptimization_handler_ =
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000222 Runtime::FunctionForId(Runtime::kArrayConstructor)->entry;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000223}
224
225
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000226static void InitializeInternalArrayConstructorDescriptor(
227 Isolate* isolate,
228 CodeStubInterfaceDescriptor* descriptor,
229 int constant_stack_parameter_count) {
230 // register state
231 // eax -- number of arguments
232 // edi -- constructor function
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000233 static Register registers_variable_args[] = { edi, eax };
234 static Register registers_no_args[] = { edi };
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000235
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000236 if (constant_stack_parameter_count == 0) {
237 descriptor->register_param_count_ = 1;
238 descriptor->register_params_ = registers_no_args;
239 } else {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000240 // stack param count needs (constructor pointer, and single argument)
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000241 descriptor->handler_arguments_mode_ = PASS_ARGUMENTS;
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000242 descriptor->stack_parameter_count_ = eax;
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000243 descriptor->register_param_count_ = 2;
244 descriptor->register_params_ = registers_variable_args;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000245 }
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000246
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000247 descriptor->hint_stack_parameter_count_ = constant_stack_parameter_count;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000248 descriptor->function_mode_ = JS_FUNCTION_STUB_MODE;
249 descriptor->deoptimization_handler_ =
ulan@chromium.orgdfe53072013-06-06 14:14:51 +0000250 Runtime::FunctionForId(Runtime::kInternalArrayConstructor)->entry;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000251}
252
253
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000254void ArrayNoArgumentConstructorStub::InitializeInterfaceDescriptor(
255 Isolate* isolate,
256 CodeStubInterfaceDescriptor* descriptor) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000257 InitializeArrayConstructorDescriptor(isolate, descriptor, 0);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000258}
259
260
261void ArraySingleArgumentConstructorStub::InitializeInterfaceDescriptor(
262 Isolate* isolate,
263 CodeStubInterfaceDescriptor* descriptor) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000264 InitializeArrayConstructorDescriptor(isolate, descriptor, 1);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000265}
266
267
268void ArrayNArgumentsConstructorStub::InitializeInterfaceDescriptor(
269 Isolate* isolate,
270 CodeStubInterfaceDescriptor* descriptor) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000271 InitializeArrayConstructorDescriptor(isolate, descriptor, -1);
272}
273
274
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000275void InternalArrayNoArgumentConstructorStub::InitializeInterfaceDescriptor(
276 Isolate* isolate,
277 CodeStubInterfaceDescriptor* descriptor) {
278 InitializeInternalArrayConstructorDescriptor(isolate, descriptor, 0);
279}
280
281
282void InternalArraySingleArgumentConstructorStub::InitializeInterfaceDescriptor(
283 Isolate* isolate,
284 CodeStubInterfaceDescriptor* descriptor) {
285 InitializeInternalArrayConstructorDescriptor(isolate, descriptor, 1);
286}
287
288
289void InternalArrayNArgumentsConstructorStub::InitializeInterfaceDescriptor(
290 Isolate* isolate,
291 CodeStubInterfaceDescriptor* descriptor) {
292 InitializeInternalArrayConstructorDescriptor(isolate, descriptor, -1);
293}
294
295
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000296void CompareNilICStub::InitializeInterfaceDescriptor(
297 Isolate* isolate,
298 CodeStubInterfaceDescriptor* descriptor) {
299 static Register registers[] = { eax };
300 descriptor->register_param_count_ = 1;
301 descriptor->register_params_ = registers;
302 descriptor->deoptimization_handler_ =
303 FUNCTION_ADDR(CompareNilIC_Miss);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000304 descriptor->SetMissHandler(
305 ExternalReference(IC_Utility(IC::kCompareNilIC_Miss), isolate));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000306}
307
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +0000308void ToBooleanStub::InitializeInterfaceDescriptor(
309 Isolate* isolate,
310 CodeStubInterfaceDescriptor* descriptor) {
311 static Register registers[] = { eax };
312 descriptor->register_param_count_ = 1;
313 descriptor->register_params_ = registers;
314 descriptor->deoptimization_handler_ =
315 FUNCTION_ADDR(ToBooleanIC_Miss);
316 descriptor->SetMissHandler(
317 ExternalReference(IC_Utility(IC::kToBooleanIC_Miss), isolate));
318}
319
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000320
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000321void StoreGlobalStub::InitializeInterfaceDescriptor(
322 Isolate* isolate,
323 CodeStubInterfaceDescriptor* descriptor) {
324 static Register registers[] = { edx, ecx, eax };
325 descriptor->register_param_count_ = 3;
326 descriptor->register_params_ = registers;
327 descriptor->deoptimization_handler_ =
328 FUNCTION_ADDR(StoreIC_MissFromStubFailure);
329}
330
331
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000332void ElementsTransitionAndStoreStub::InitializeInterfaceDescriptor(
333 Isolate* isolate,
334 CodeStubInterfaceDescriptor* descriptor) {
335 static Register registers[] = { eax, ebx, ecx, edx };
336 descriptor->register_param_count_ = 4;
337 descriptor->register_params_ = registers;
338 descriptor->deoptimization_handler_ =
339 FUNCTION_ADDR(ElementsTransitionAndStoreIC_Miss);
340}
341
342
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +0000343void BinaryOpICStub::InitializeInterfaceDescriptor(
jkummerow@chromium.org25b0e212013-10-04 15:38:52 +0000344 Isolate* isolate,
345 CodeStubInterfaceDescriptor* descriptor) {
346 static Register registers[] = { edx, eax };
347 descriptor->register_param_count_ = 2;
348 descriptor->register_params_ = registers;
349 descriptor->deoptimization_handler_ = FUNCTION_ADDR(BinaryOpIC_Miss);
350 descriptor->SetMissHandler(
351 ExternalReference(IC_Utility(IC::kBinaryOpIC_Miss), isolate));
352}
353
354
ulan@chromium.org0f13e742014-01-03 15:51:11 +0000355void BinaryOpWithAllocationSiteStub::InitializeInterfaceDescriptor(
356 Isolate* isolate,
357 CodeStubInterfaceDescriptor* descriptor) {
358 static Register registers[] = { ecx, edx, eax };
359 descriptor->register_param_count_ = 3;
360 descriptor->register_params_ = registers;
361 descriptor->deoptimization_handler_ =
362 FUNCTION_ADDR(BinaryOpIC_MissWithAllocationSite);
363}
364
365
machenbach@chromium.orgbbbda922014-01-23 09:38:20 +0000366void StringAddStub::InitializeInterfaceDescriptor(
machenbach@chromium.org0cc09502013-11-13 12:20:55 +0000367 Isolate* isolate,
368 CodeStubInterfaceDescriptor* descriptor) {
369 static Register registers[] = { edx, eax };
370 descriptor->register_param_count_ = 2;
371 descriptor->register_params_ = registers;
372 descriptor->deoptimization_handler_ =
373 Runtime::FunctionForId(Runtime::kStringAdd)->entry;
374}
375
376
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +0000377void CallDescriptors::InitializeForIsolate(Isolate* isolate) {
378 {
379 CallInterfaceDescriptor* descriptor =
380 isolate->call_descriptor(Isolate::ArgumentAdaptorCall);
381 static Register registers[] = { edi, // JSFunction
382 esi, // context
383 eax, // actual number of arguments
384 ebx, // expected number of arguments
385 };
386 static Representation representations[] = {
387 Representation::Tagged(), // JSFunction
388 Representation::Tagged(), // context
389 Representation::Integer32(), // actual number of arguments
390 Representation::Integer32(), // expected number of arguments
391 };
392 descriptor->register_param_count_ = 4;
393 descriptor->register_params_ = registers;
394 descriptor->param_representations_ = representations;
395 }
396 {
397 CallInterfaceDescriptor* descriptor =
398 isolate->call_descriptor(Isolate::KeyedCall);
399 static Register registers[] = { esi, // context
400 ecx, // key
401 };
402 static Representation representations[] = {
403 Representation::Tagged(), // context
404 Representation::Tagged(), // key
405 };
406 descriptor->register_param_count_ = 2;
407 descriptor->register_params_ = registers;
408 descriptor->param_representations_ = representations;
409 }
410 {
411 CallInterfaceDescriptor* descriptor =
412 isolate->call_descriptor(Isolate::NamedCall);
413 static Register registers[] = { esi, // context
414 ecx, // name
415 };
416 static Representation representations[] = {
417 Representation::Tagged(), // context
418 Representation::Tagged(), // name
419 };
420 descriptor->register_param_count_ = 2;
421 descriptor->register_params_ = registers;
422 descriptor->param_representations_ = representations;
423 }
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +0000424 {
425 CallInterfaceDescriptor* descriptor =
426 isolate->call_descriptor(Isolate::CallHandler);
427 static Register registers[] = { esi, // context
428 edx, // receiver
429 };
430 static Representation representations[] = {
431 Representation::Tagged(), // context
432 Representation::Tagged(), // receiver
433 };
434 descriptor->register_param_count_ = 2;
435 descriptor->register_params_ = registers;
436 descriptor->param_representations_ = representations;
437 }
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +0000438}
439
440
ricow@chromium.org65fae842010-08-25 15:26:24 +0000441#define __ ACCESS_MASM(masm)
whesse@chromium.org7a392b32011-01-31 11:30:36 +0000442
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000443
444void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm) {
445 // Update the static counter each time a new code stub is generated.
446 Isolate* isolate = masm->isolate();
447 isolate->counters()->code_stubs()->Increment();
448
449 CodeStubInterfaceDescriptor* descriptor = GetInterfaceDescriptor(isolate);
450 int param_count = descriptor->register_param_count_;
451 {
452 // Call the runtime system in a fresh internal frame.
453 FrameScope scope(masm, StackFrame::INTERNAL);
454 ASSERT(descriptor->register_param_count_ == 0 ||
455 eax.is(descriptor->register_params_[param_count - 1]));
456 // Push arguments
457 for (int i = 0; i < param_count; ++i) {
458 __ push(descriptor->register_params_[i]);
459 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +0000460 ExternalReference miss = descriptor->miss_handler();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +0000461 __ CallExternalReference(miss, descriptor->register_param_count_);
462 }
463
464 __ ret(0);
465}
466
467
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000468void FastNewBlockContextStub::Generate(MacroAssembler* masm) {
469 // Stack layout on entry:
470 //
471 // [esp + (1 * kPointerSize)]: function
472 // [esp + (2 * kPointerSize)]: serialized scope info
473
474 // Try to allocate the context in new space.
475 Label gc;
476 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000477 __ Allocate(FixedArray::SizeFor(length), eax, ebx, ecx, &gc, TAG_OBJECT);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000478
479 // Get the function or sentinel from the stack.
480 __ mov(ecx, Operand(esp, 1 * kPointerSize));
481
482 // Get the serialized scope info from the stack.
483 __ mov(ebx, Operand(esp, 2 * kPointerSize));
484
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000485 // Set up the object header.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000486 Factory* factory = masm->isolate()->factory();
487 __ mov(FieldOperand(eax, HeapObject::kMapOffset),
488 factory->block_context_map());
489 __ mov(FieldOperand(eax, Context::kLengthOffset),
490 Immediate(Smi::FromInt(length)));
491
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000492 // If this block context is nested in the native context we get a smi
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000493 // sentinel instead of a function. The block context should get the
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000494 // canonical empty function of the native context as its closure which
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000495 // we still have to look up.
496 Label after_sentinel;
497 __ JumpIfNotSmi(ecx, &after_sentinel, Label::kNear);
498 if (FLAG_debug_code) {
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000499 __ cmp(ecx, 0);
danno@chromium.org59400602013-08-13 17:09:37 +0000500 __ Assert(equal, kExpected0AsASmiSentinel);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000501 }
502 __ mov(ecx, GlobalObjectOperand());
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000503 __ mov(ecx, FieldOperand(ecx, GlobalObject::kNativeContextOffset));
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000504 __ mov(ecx, ContextOperand(ecx, Context::CLOSURE_INDEX));
505 __ bind(&after_sentinel);
506
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000507 // Set up the fixed slots.
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000508 __ mov(ContextOperand(eax, Context::CLOSURE_INDEX), ecx);
509 __ mov(ContextOperand(eax, Context::PREVIOUS_INDEX), esi);
510 __ mov(ContextOperand(eax, Context::EXTENSION_INDEX), ebx);
511
512 // Copy the global object from the previous context.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000513 __ mov(ebx, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
514 __ mov(ContextOperand(eax, Context::GLOBAL_OBJECT_INDEX), ebx);
svenpanne@chromium.orga8bb4d92011-10-10 13:20:40 +0000515
516 // Initialize the rest of the slots to the hole value.
517 if (slots_ == 1) {
518 __ mov(ContextOperand(eax, Context::MIN_CONTEXT_SLOTS),
519 factory->the_hole_value());
520 } else {
521 __ mov(ebx, factory->the_hole_value());
522 for (int i = 0; i < slots_; i++) {
523 __ mov(ContextOperand(eax, i + Context::MIN_CONTEXT_SLOTS), ebx);
524 }
525 }
526
527 // Return and remove the on-stack parameters.
528 __ mov(esi, eax);
529 __ ret(2 * kPointerSize);
530
531 // Need to collect. Call into runtime system.
532 __ bind(&gc);
533 __ TailCallRuntime(Runtime::kPushBlockContext, 2, 1);
534}
535
536
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000537void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
538 // We don't allow a GC during a store buffer overflow so there is no need to
539 // store the registers in any particular way, but we do have to store and
540 // restore them.
541 __ pushad();
542 if (save_doubles_ == kSaveFPRegs) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000543 CpuFeatureScope scope(masm, SSE2);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000544 __ sub(esp, Immediate(kDoubleSize * XMMRegister::kNumRegisters));
545 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
546 XMMRegister reg = XMMRegister::from_code(i);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000547 __ movsd(Operand(esp, i * kDoubleSize), reg);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000548 }
549 }
550 const int argument_count = 1;
551
552 AllowExternalCallThatCantCauseGC scope(masm);
553 __ PrepareCallCFunction(argument_count, ecx);
554 __ mov(Operand(esp, 0 * kPointerSize),
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000555 Immediate(ExternalReference::isolate_address(masm->isolate())));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000556 __ CallCFunction(
557 ExternalReference::store_buffer_overflow_function(masm->isolate()),
558 argument_count);
559 if (save_doubles_ == kSaveFPRegs) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000560 CpuFeatureScope scope(masm, SSE2);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000561 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
562 XMMRegister reg = XMMRegister::from_code(i);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000563 __ movsd(reg, Operand(esp, i * kDoubleSize));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000564 }
565 __ add(esp, Immediate(kDoubleSize * XMMRegister::kNumRegisters));
566 }
567 __ popad();
568 __ ret(0);
569}
570
571
ricow@chromium.org65fae842010-08-25 15:26:24 +0000572class FloatingPointHelper : public AllStatic {
573 public:
ricow@chromium.org65fae842010-08-25 15:26:24 +0000574 enum ArgLocation {
575 ARGS_ON_STACK,
576 ARGS_IN_REGISTERS
577 };
578
579 // Code pattern for loading a floating point value. Input value must
580 // be either a smi or a heap number object (fp value). Requirements:
581 // operand in register number. Returns operand as floating point number
582 // on FPU stack.
583 static void LoadFloatOperand(MacroAssembler* masm, Register number);
584
ricow@chromium.org65fae842010-08-25 15:26:24 +0000585 // Test if operands are smi or number objects (fp). Requirements:
586 // operand_1 in eax, operand_2 in edx; falls through on float
587 // operands, jumps to the non_float label otherwise.
588 static void CheckFloatOperands(MacroAssembler* masm,
589 Label* non_float,
590 Register scratch);
591
ricow@chromium.org65fae842010-08-25 15:26:24 +0000592 // Test if operands are numbers (smi or HeapNumber objects), and load
593 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
594 // either operand is not a number. Operands are in edx and eax.
595 // Leaves operands unchanged.
596 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000597};
598
599
danno@chromium.org169691d2013-07-15 08:01:13 +0000600void DoubleToIStub::Generate(MacroAssembler* masm) {
601 Register input_reg = this->source();
602 Register final_result_reg = this->destination();
603 ASSERT(is_truncating());
604
605 Label check_negative, process_64_bits, done, done_no_stash;
606
607 int double_offset = offset();
608
609 // Account for return address and saved regs if input is esp.
610 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
611
612 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
613 MemOperand exponent_operand(MemOperand(input_reg,
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +0000614 double_offset + kDoubleSize / 2));
danno@chromium.org169691d2013-07-15 08:01:13 +0000615
616 Register scratch1;
617 {
618 Register scratch_candidates[3] = { ebx, edx, edi };
619 for (int i = 0; i < 3; i++) {
620 scratch1 = scratch_candidates[i];
621 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
622 }
623 }
624 // Since we must use ecx for shifts below, use some other register (eax)
625 // to calculate the result if ecx is the requested return register.
626 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
627 // Save ecx if it isn't the return register and therefore volatile, or if it
628 // is the return register, then save the temp register we use in its stead for
629 // the result.
630 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
631 __ push(scratch1);
632 __ push(save_reg);
633
634 bool stash_exponent_copy = !input_reg.is(esp);
635 __ mov(scratch1, mantissa_operand);
636 if (CpuFeatures::IsSupported(SSE3)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000637 CpuFeatureScope scope(masm, SSE3);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000638 // Load x87 register with heap number.
danno@chromium.org169691d2013-07-15 08:01:13 +0000639 __ fld_d(mantissa_operand);
640 }
641 __ mov(ecx, exponent_operand);
642 if (stash_exponent_copy) __ push(ecx);
643
644 __ and_(ecx, HeapNumber::kExponentMask);
645 __ shr(ecx, HeapNumber::kExponentShift);
646 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
647 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
648 __ j(below, &process_64_bits);
649
650 // Result is entirely in lower 32-bits of mantissa
651 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
652 if (CpuFeatures::IsSupported(SSE3)) {
653 __ fstp(0);
654 }
655 __ sub(ecx, Immediate(delta));
656 __ xor_(result_reg, result_reg);
657 __ cmp(ecx, Immediate(31));
658 __ j(above, &done);
659 __ shl_cl(scratch1);
660 __ jmp(&check_negative);
661
662 __ bind(&process_64_bits);
663 if (CpuFeatures::IsSupported(SSE3)) {
664 CpuFeatureScope scope(masm, SSE3);
665 if (stash_exponent_copy) {
666 // Already a copy of the exponent on the stack, overwrite it.
667 STATIC_ASSERT(kDoubleSize == 2 * kPointerSize);
668 __ sub(esp, Immediate(kDoubleSize / 2));
669 } else {
670 // Reserve space for 64 bit answer.
671 __ sub(esp, Immediate(kDoubleSize)); // Nolint.
672 }
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000673 // Do conversion, which cannot fail because we checked the exponent.
674 __ fisttp_d(Operand(esp, 0));
danno@chromium.org169691d2013-07-15 08:01:13 +0000675 __ mov(result_reg, Operand(esp, 0)); // Load low word of answer as result
676 __ add(esp, Immediate(kDoubleSize));
677 __ jmp(&done_no_stash);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000678 } else {
danno@chromium.org169691d2013-07-15 08:01:13 +0000679 // Result must be extracted from shifted 32-bit mantissa
680 __ sub(ecx, Immediate(delta));
681 __ neg(ecx);
682 if (stash_exponent_copy) {
683 __ mov(result_reg, MemOperand(esp, 0));
684 } else {
685 __ mov(result_reg, exponent_operand);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000686 }
danno@chromium.org169691d2013-07-15 08:01:13 +0000687 __ and_(result_reg,
688 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
689 __ add(result_reg,
690 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
691 __ shrd(result_reg, scratch1);
692 __ shr_cl(result_reg);
693 __ test(ecx, Immediate(32));
694 if (CpuFeatures::IsSupported(CMOV)) {
695 CpuFeatureScope use_cmov(masm, CMOV);
696 __ cmov(not_equal, scratch1, result_reg);
697 } else {
698 Label skip_mov;
699 __ j(equal, &skip_mov, Label::kNear);
700 __ mov(scratch1, result_reg);
701 __ bind(&skip_mov);
702 }
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000703 }
danno@chromium.org169691d2013-07-15 08:01:13 +0000704
705 // If the double was negative, negate the integer result.
706 __ bind(&check_negative);
707 __ mov(result_reg, scratch1);
708 __ neg(result_reg);
709 if (stash_exponent_copy) {
710 __ cmp(MemOperand(esp, 0), Immediate(0));
711 } else {
712 __ cmp(exponent_operand, Immediate(0));
713 }
714 if (CpuFeatures::IsSupported(CMOV)) {
715 CpuFeatureScope use_cmov(masm, CMOV);
716 __ cmov(greater, result_reg, scratch1);
717 } else {
718 Label skip_mov;
719 __ j(less_equal, &skip_mov, Label::kNear);
720 __ mov(result_reg, scratch1);
721 __ bind(&skip_mov);
722 }
723
724 // Restore registers
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000725 __ bind(&done);
danno@chromium.org169691d2013-07-15 08:01:13 +0000726 if (stash_exponent_copy) {
727 __ add(esp, Immediate(kDoubleSize / 2));
728 }
729 __ bind(&done_no_stash);
730 if (!final_result_reg.is(result_reg)) {
731 ASSERT(final_result_reg.is(ecx));
732 __ mov(final_result_reg, result_reg);
733 }
734 __ pop(save_reg);
735 __ pop(scratch1);
736 __ ret(0);
sgjesse@chromium.org8e8294a2011-05-02 14:30:53 +0000737}
738
739
ricow@chromium.org65fae842010-08-25 15:26:24 +0000740void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
741 Register number) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000742 Label load_smi, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000743
whesse@chromium.org7b260152011-06-20 15:33:18 +0000744 __ JumpIfSmi(number, &load_smi, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000745 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000746 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000747
748 __ bind(&load_smi);
749 __ SmiUntag(number);
750 __ push(number);
751 __ fild_s(Operand(esp, 0));
752 __ pop(number);
753
754 __ bind(&done);
755}
756
757
ricow@chromium.org65fae842010-08-25 15:26:24 +0000758void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
759 Label* not_numbers) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000760 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000761 // Load operand in edx into xmm0, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000762 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000763 Factory* factory = masm->isolate()->factory();
764 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000765 __ j(not_equal, not_numbers); // Argument in edx is not a number.
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000766 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000767 __ bind(&load_eax);
768 // Load operand in eax into xmm1, or branch to not_numbers.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000769 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000770 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000771 __ j(equal, &load_float_eax, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000772 __ jmp(not_numbers); // Argument in eax is not a number.
773 __ bind(&load_smi_edx);
774 __ SmiUntag(edx); // Untag smi before converting to float.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000775 __ Cvtsi2sd(xmm0, edx);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000776 __ SmiTag(edx); // Retag smi for heap number overwriting test.
777 __ jmp(&load_eax);
778 __ bind(&load_smi_eax);
779 __ SmiUntag(eax); // Untag smi before converting to float.
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000780 __ Cvtsi2sd(xmm1, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000781 __ SmiTag(eax); // Retag smi for heap number overwriting test.
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000782 __ jmp(&done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000783 __ bind(&load_float_eax);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000784 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +0000785 __ bind(&done);
786}
787
788
ricow@chromium.org65fae842010-08-25 15:26:24 +0000789void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
790 Label* non_float,
791 Register scratch) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000792 Label test_other, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +0000793 // Test if both operands are floats or smi -> scratch=k_is_float;
794 // Otherwise scratch = k_not_float.
whesse@chromium.org7b260152011-06-20 15:33:18 +0000795 __ JumpIfSmi(edx, &test_other, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000796 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000797 Factory* factory = masm->isolate()->factory();
798 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000799 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
800
801 __ bind(&test_other);
whesse@chromium.org7b260152011-06-20 15:33:18 +0000802 __ JumpIfSmi(eax, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +0000803 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000804 __ cmp(scratch, factory->heap_number_map());
ricow@chromium.org65fae842010-08-25 15:26:24 +0000805 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
806
807 // Fall-through: Both operands are numbers.
808 __ bind(&done);
809}
810
811
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000812void MathPowStub::Generate(MacroAssembler* masm) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000813 CpuFeatureScope use_sse2(masm, SSE2);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000814 Factory* factory = masm->isolate()->factory();
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000815 const Register exponent = eax;
816 const Register base = edx;
817 const Register scratch = ecx;
818 const XMMRegister double_result = xmm3;
819 const XMMRegister double_base = xmm2;
820 const XMMRegister double_exponent = xmm1;
821 const XMMRegister double_scratch = xmm4;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000822
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000823 Label call_runtime, done, exponent_not_smi, int_exponent;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000824
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000825 // Save 1 in double_result - we need this several times later on.
826 __ mov(scratch, Immediate(1));
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000827 __ Cvtsi2sd(double_result, scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000828
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000829 if (exponent_type_ == ON_STACK) {
830 Label base_is_smi, unpack_exponent;
831 // The exponent and base are supplied as arguments on the stack.
832 // This can only happen if the stub is called from non-optimized code.
833 // Load input parameters from stack.
834 __ mov(base, Operand(esp, 2 * kPointerSize));
835 __ mov(exponent, Operand(esp, 1 * kPointerSize));
836
837 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
838 __ cmp(FieldOperand(base, HeapObject::kMapOffset),
839 factory->heap_number_map());
840 __ j(not_equal, &call_runtime);
841
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000842 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000843 __ jmp(&unpack_exponent, Label::kNear);
844
845 __ bind(&base_is_smi);
846 __ SmiUntag(base);
machenbach@chromium.org528ce022013-09-23 14:09:36 +0000847 __ Cvtsi2sd(double_base, base);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000848
849 __ bind(&unpack_exponent);
850 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
851 __ SmiUntag(exponent);
852 __ jmp(&int_exponent);
853
854 __ bind(&exponent_not_smi);
855 __ cmp(FieldOperand(exponent, HeapObject::kMapOffset),
856 factory->heap_number_map());
857 __ j(not_equal, &call_runtime);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000858 __ movsd(double_exponent,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000859 FieldOperand(exponent, HeapNumber::kValueOffset));
860 } else if (exponent_type_ == TAGGED) {
861 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
862 __ SmiUntag(exponent);
863 __ jmp(&int_exponent);
864
865 __ bind(&exponent_not_smi);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000866 __ movsd(double_exponent,
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000867 FieldOperand(exponent, HeapNumber::kValueOffset));
868 }
869
870 if (exponent_type_ != INTEGER) {
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000871 Label fast_power, try_arithmetic_simplification;
872 __ DoubleToI(exponent, double_exponent, double_scratch,
873 TREAT_MINUS_ZERO_AS_ZERO, &try_arithmetic_simplification);
874 __ jmp(&int_exponent);
875
876 __ bind(&try_arithmetic_simplification);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000877 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
jkummerow@chromium.org8fa5bd92013-09-02 11:45:09 +0000878 __ cvttsd2si(exponent, Operand(double_exponent));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000879 __ cmp(exponent, Immediate(0x80000000u));
880 __ j(equal, &call_runtime);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000881
882 if (exponent_type_ == ON_STACK) {
883 // Detect square root case. Crankshaft detects constant +/-0.5 at
884 // compile time and uses DoMathPowHalf instead. We then skip this check
885 // for non-constant cases of +/-0.5 as these hardly occur.
886 Label continue_sqrt, continue_rsqrt, not_plus_half;
887 // Test for 0.5.
888 // Load double_scratch with 0.5.
889 __ mov(scratch, Immediate(0x3F000000u));
890 __ movd(double_scratch, scratch);
891 __ cvtss2sd(double_scratch, double_scratch);
892 // Already ruled out NaNs for exponent.
893 __ ucomisd(double_scratch, double_exponent);
894 __ j(not_equal, &not_plus_half, Label::kNear);
895
896 // Calculates square root of base. Check for the special case of
897 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
898 // According to IEEE-754, single-precision -Infinity has the highest
899 // 9 bits set and the lowest 23 bits cleared.
900 __ mov(scratch, 0xFF800000u);
901 __ movd(double_scratch, scratch);
902 __ cvtss2sd(double_scratch, double_scratch);
903 __ ucomisd(double_base, double_scratch);
904 // Comparing -Infinity with NaN results in "unordered", which sets the
905 // zero flag as if both were equal. However, it also sets the carry flag.
906 __ j(not_equal, &continue_sqrt, Label::kNear);
907 __ j(carry, &continue_sqrt, Label::kNear);
908
909 // Set result to Infinity in the special case.
910 __ xorps(double_result, double_result);
911 __ subsd(double_result, double_scratch);
912 __ jmp(&done);
913
914 __ bind(&continue_sqrt);
915 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
916 __ xorps(double_scratch, double_scratch);
917 __ addsd(double_scratch, double_base); // Convert -0 to +0.
918 __ sqrtsd(double_result, double_scratch);
919 __ jmp(&done);
920
921 // Test for -0.5.
922 __ bind(&not_plus_half);
923 // Load double_exponent with -0.5 by substracting 1.
924 __ subsd(double_scratch, double_result);
925 // Already ruled out NaNs for exponent.
926 __ ucomisd(double_scratch, double_exponent);
927 __ j(not_equal, &fast_power, Label::kNear);
928
929 // Calculates reciprocal of square root of base. Check for the special
930 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
931 // According to IEEE-754, single-precision -Infinity has the highest
932 // 9 bits set and the lowest 23 bits cleared.
933 __ mov(scratch, 0xFF800000u);
934 __ movd(double_scratch, scratch);
935 __ cvtss2sd(double_scratch, double_scratch);
936 __ ucomisd(double_base, double_scratch);
937 // Comparing -Infinity with NaN results in "unordered", which sets the
938 // zero flag as if both were equal. However, it also sets the carry flag.
939 __ j(not_equal, &continue_rsqrt, Label::kNear);
940 __ j(carry, &continue_rsqrt, Label::kNear);
941
942 // Set result to 0 in the special case.
943 __ xorps(double_result, double_result);
944 __ jmp(&done);
945
946 __ bind(&continue_rsqrt);
947 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
948 __ xorps(double_exponent, double_exponent);
949 __ addsd(double_exponent, double_base); // Convert -0 to +0.
950 __ sqrtsd(double_exponent, double_exponent);
951 __ divsd(double_result, double_exponent);
952 __ jmp(&done);
953 }
954
955 // Using FPU instructions to calculate power.
956 Label fast_power_failed;
957 __ bind(&fast_power);
958 __ fnclex(); // Clear flags to catch exceptions later.
959 // Transfer (B)ase and (E)xponent onto the FPU register stack.
960 __ sub(esp, Immediate(kDoubleSize));
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000961 __ movsd(Operand(esp, 0), double_exponent);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000962 __ fld_d(Operand(esp, 0)); // E
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000963 __ movsd(Operand(esp, 0), double_base);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000964 __ fld_d(Operand(esp, 0)); // B, E
965
966 // Exponent is in st(1) and base is in st(0)
967 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
968 // FYL2X calculates st(1) * log2(st(0))
969 __ fyl2x(); // X
970 __ fld(0); // X, X
971 __ frndint(); // rnd(X), X
972 __ fsub(1); // rnd(X), X-rnd(X)
973 __ fxch(1); // X - rnd(X), rnd(X)
974 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
975 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
976 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000977 __ faddp(1); // 2^(X-rnd(X)), rnd(X)
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000978 // FSCALE calculates st(0) * 2^st(1)
979 __ fscale(); // 2^X, rnd(X)
danno@chromium.org1f34ad32012-11-26 14:53:56 +0000980 __ fstp(1); // 2^X
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000981 // Bail out to runtime in case of exceptions in the status word.
982 __ fnstsw_ax();
983 __ test_b(eax, 0x5F); // We check for all but precision exception.
984 __ j(not_zero, &fast_power_failed, Label::kNear);
985 __ fstp_d(Operand(esp, 0));
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +0000986 __ movsd(double_result, Operand(esp, 0));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +0000987 __ add(esp, Immediate(kDoubleSize));
988 __ jmp(&done);
989
990 __ bind(&fast_power_failed);
991 __ fninit();
992 __ add(esp, Immediate(kDoubleSize));
993 __ jmp(&call_runtime);
994 }
995
996 // Calculate power with integer exponent.
997 __ bind(&int_exponent);
998 const XMMRegister double_scratch2 = double_exponent;
999 __ mov(scratch, exponent); // Back up exponent.
1000 __ movsd(double_scratch, double_base); // Back up base.
1001 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001002
1003 // Get absolute value of exponent.
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00001004 Label no_neg, while_true, while_false;
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001005 __ test(scratch, scratch);
1006 __ j(positive, &no_neg, Label::kNear);
1007 __ neg(scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001008 __ bind(&no_neg);
1009
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001010 __ j(zero, &while_false, Label::kNear);
1011 __ shr(scratch, 1);
1012 // Above condition means CF==0 && ZF==0. This means that the
1013 // bit that has been shifted out is 0 and the result is not 0.
1014 __ j(above, &while_true, Label::kNear);
1015 __ movsd(double_result, double_scratch);
1016 __ j(zero, &while_false, Label::kNear);
1017
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001018 __ bind(&while_true);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001019 __ shr(scratch, 1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001020 __ mulsd(double_scratch, double_scratch);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001021 __ j(above, &while_true, Label::kNear);
1022 __ mulsd(double_result, double_scratch);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001023 __ j(not_zero, &while_true);
1024
ulan@chromium.org56c14af2012-09-20 12:51:09 +00001025 __ bind(&while_false);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001026 // scratch has the original value of the exponent - if the exponent is
1027 // negative, return 1/result.
1028 __ test(exponent, exponent);
1029 __ j(positive, &done);
1030 __ divsd(double_scratch2, double_result);
1031 __ movsd(double_result, double_scratch2);
1032 // Test whether result is zero. Bail out to check for subnormal result.
1033 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
1034 __ xorps(double_scratch2, double_scratch2);
1035 __ ucomisd(double_scratch2, double_result); // Result cannot be NaN.
1036 // double_exponent aliased as double_scratch2 has already been overwritten
1037 // and may not have contained the exponent value in the first place when the
1038 // exponent is a smi. We reset it with exponent value before bailing out.
1039 __ j(not_equal, &done);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001040 __ Cvtsi2sd(double_exponent, exponent);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001041
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001042 // Returning or bailing out.
1043 Counters* counters = masm->isolate()->counters();
1044 if (exponent_type_ == ON_STACK) {
1045 // The arguments are still on the stack.
1046 __ bind(&call_runtime);
1047 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001048
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001049 // The stub is called from non-optimized code, which expects the result
1050 // as heap number in exponent.
1051 __ bind(&done);
1052 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +00001053 __ movsd(FieldOperand(eax, HeapNumber::kValueOffset), double_result);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001054 __ IncrementCounter(counters->math_pow(), 1);
1055 __ ret(2 * kPointerSize);
1056 } else {
1057 __ bind(&call_runtime);
1058 {
1059 AllowExternalCallThatCantCauseGC scope(masm);
1060 __ PrepareCallCFunction(4, scratch);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +00001061 __ movsd(Operand(esp, 0 * kDoubleSize), double_base);
1062 __ movsd(Operand(esp, 1 * kDoubleSize), double_exponent);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001063 __ CallCFunction(
1064 ExternalReference::power_double_double_function(masm->isolate()), 4);
1065 }
1066 // Return value is in st(0) on ia32.
1067 // Store it into the (fixed) result register.
1068 __ sub(esp, Immediate(kDoubleSize));
1069 __ fstp_d(Operand(esp, 0));
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +00001070 __ movsd(double_result, Operand(esp, 0));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001071 __ add(esp, Immediate(kDoubleSize));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001072
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00001073 __ bind(&done);
1074 __ IncrementCounter(counters->math_pow(), 1);
1075 __ ret(0);
1076 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001077}
1078
1079
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001080void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1081 // ----------- S t a t e -------------
1082 // -- ecx : name
1083 // -- edx : receiver
1084 // -- esp[0] : return address
1085 // -----------------------------------
1086 Label miss;
1087
1088 if (kind() == Code::KEYED_LOAD_IC) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001089 __ cmp(ecx, Immediate(masm->isolate()->factory()->prototype_string()));
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001090 __ j(not_equal, &miss);
1091 }
1092
1093 StubCompiler::GenerateLoadFunctionPrototype(masm, edx, eax, ebx, &miss);
1094 __ bind(&miss);
danno@chromium.orgbee51992013-07-10 14:57:15 +00001095 StubCompiler::TailCallBuiltin(
1096 masm, BaseLoadStoreStubCompiler::MissBuiltin(kind()));
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001097}
1098
1099
1100void StringLengthStub::Generate(MacroAssembler* masm) {
1101 // ----------- S t a t e -------------
1102 // -- ecx : name
1103 // -- edx : receiver
1104 // -- esp[0] : return address
1105 // -----------------------------------
1106 Label miss;
1107
1108 if (kind() == Code::KEYED_LOAD_IC) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001109 __ cmp(ecx, Immediate(masm->isolate()->factory()->length_string()));
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001110 __ j(not_equal, &miss);
1111 }
1112
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001113 StubCompiler::GenerateLoadStringLength(masm, edx, eax, ebx, &miss);
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001114 __ bind(&miss);
danno@chromium.orgbee51992013-07-10 14:57:15 +00001115 StubCompiler::TailCallBuiltin(
1116 masm, BaseLoadStoreStubCompiler::MissBuiltin(kind()));
mvstanton@chromium.org6bec0092013-01-23 13:46:53 +00001117}
1118
1119
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00001120void StoreArrayLengthStub::Generate(MacroAssembler* masm) {
1121 // ----------- S t a t e -------------
1122 // -- eax : value
1123 // -- ecx : name
1124 // -- edx : receiver
1125 // -- esp[0] : return address
1126 // -----------------------------------
1127 //
1128 // This accepts as a receiver anything JSArray::SetElementsLength accepts
1129 // (currently anything except for external arrays which means anything with
1130 // elements of FixedArray type). Value must be a number, but only smis are
1131 // accepted as the most common case.
1132
1133 Label miss;
1134
1135 Register receiver = edx;
1136 Register value = eax;
1137 Register scratch = ebx;
1138
ulan@chromium.org750145a2013-03-07 15:14:13 +00001139 if (kind() == Code::KEYED_STORE_IC) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001140 __ cmp(ecx, Immediate(masm->isolate()->factory()->length_string()));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00001141 __ j(not_equal, &miss);
1142 }
1143
1144 // Check that the receiver isn't a smi.
1145 __ JumpIfSmi(receiver, &miss);
1146
1147 // Check that the object is a JS array.
1148 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
1149 __ j(not_equal, &miss);
1150
1151 // Check that elements are FixedArray.
1152 // We rely on StoreIC_ArrayLength below to deal with all types of
1153 // fast elements (including COW).
1154 __ mov(scratch, FieldOperand(receiver, JSArray::kElementsOffset));
1155 __ CmpObjectType(scratch, FIXED_ARRAY_TYPE, scratch);
1156 __ j(not_equal, &miss);
1157
1158 // Check that the array has fast properties, otherwise the length
1159 // property might have been redefined.
1160 __ mov(scratch, FieldOperand(receiver, JSArray::kPropertiesOffset));
1161 __ CompareRoot(FieldOperand(scratch, FixedArray::kMapOffset),
1162 Heap::kHashTableMapRootIndex);
1163 __ j(equal, &miss);
1164
1165 // Check that value is a smi.
1166 __ JumpIfNotSmi(value, &miss);
1167
1168 // Prepare tail call to StoreIC_ArrayLength.
1169 __ pop(scratch);
1170 __ push(receiver);
1171 __ push(value);
1172 __ push(scratch); // return address
1173
1174 ExternalReference ref =
1175 ExternalReference(IC_Utility(IC::kStoreIC_ArrayLength), masm->isolate());
1176 __ TailCallExternalReference(ref, 2, 1);
1177
1178 __ bind(&miss);
1179
danno@chromium.orgbee51992013-07-10 14:57:15 +00001180 StubCompiler::TailCallBuiltin(
1181 masm, BaseLoadStoreStubCompiler::MissBuiltin(kind()));
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +00001182}
1183
1184
ricow@chromium.org65fae842010-08-25 15:26:24 +00001185void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
1186 // The key is in edx and the parameter count is in eax.
1187
1188 // The displacement is used for skipping the frame pointer on the
1189 // stack. It is the offset of the last parameter (if any) relative
1190 // to the frame pointer.
1191 static const int kDisplacement = 1 * kPointerSize;
1192
1193 // Check that the key is a smi.
1194 Label slow;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001195 __ JumpIfNotSmi(edx, &slow, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001196
1197 // Check if the calling frame is an arguments adaptor frame.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001198 Label adaptor;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001199 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1200 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001201 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001202 __ j(equal, &adaptor, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001203
1204 // Check index against formal parameters count limit passed in
1205 // through register eax. Use unsigned comparison to get negative
1206 // check for free.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001207 __ cmp(edx, eax);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001208 __ j(above_equal, &slow, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001209
1210 // Read the argument from the stack and return it.
1211 STATIC_ASSERT(kSmiTagSize == 1);
1212 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
1213 __ lea(ebx, Operand(ebp, eax, times_2, 0));
1214 __ neg(edx);
1215 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
1216 __ ret(0);
1217
1218 // Arguments adaptor case: Check index against actual arguments
1219 // limit found in the arguments adaptor frame. Use unsigned
1220 // comparison to get negative check for free.
1221 __ bind(&adaptor);
1222 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001223 __ cmp(edx, ecx);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001224 __ j(above_equal, &slow, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001225
1226 // Read the argument from the stack and return it.
1227 STATIC_ASSERT(kSmiTagSize == 1);
1228 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
1229 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
1230 __ neg(edx);
1231 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
1232 __ ret(0);
1233
1234 // Slow-case: Handle non-smi or out-of-bounds access to arguments
1235 // by calling the runtime system.
1236 __ bind(&slow);
1237 __ pop(ebx); // Return address.
1238 __ push(edx);
1239 __ push(ebx);
1240 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
1241}
1242
1243
whesse@chromium.org7b260152011-06-20 15:33:18 +00001244void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00001245 // esp[0] : return address
1246 // esp[4] : number of parameters
1247 // esp[8] : receiver displacement
whesse@chromium.org7b260152011-06-20 15:33:18 +00001248 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00001249
whesse@chromium.org7b260152011-06-20 15:33:18 +00001250 // Check if the calling frame is an arguments adaptor frame.
1251 Label runtime;
1252 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1253 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001254 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001255 __ j(not_equal, &runtime, Label::kNear);
1256
1257 // Patch the arguments.length and the parameters pointer.
1258 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1259 __ mov(Operand(esp, 1 * kPointerSize), ecx);
1260 __ lea(edx, Operand(edx, ecx, times_2,
1261 StandardFrameConstants::kCallerSPOffset));
1262 __ mov(Operand(esp, 2 * kPointerSize), edx);
1263
1264 __ bind(&runtime);
1265 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
1266}
1267
1268
1269void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001270 Isolate* isolate = masm->isolate();
1271
whesse@chromium.org7b260152011-06-20 15:33:18 +00001272 // esp[0] : return address
1273 // esp[4] : number of parameters (tagged)
1274 // esp[8] : receiver displacement
1275 // esp[12] : function
1276
1277 // ebx = parameter count (tagged)
1278 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1279
1280 // Check if the calling frame is an arguments adaptor frame.
1281 // TODO(rossberg): Factor out some of the bits that are shared with the other
1282 // Generate* functions.
1283 Label runtime;
1284 Label adaptor_frame, try_allocate;
1285 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1286 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001287 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001288 __ j(equal, &adaptor_frame, Label::kNear);
1289
1290 // No adaptor, parameter count = argument count.
1291 __ mov(ecx, ebx);
1292 __ jmp(&try_allocate, Label::kNear);
1293
1294 // We have an adaptor frame. Patch the parameters pointer.
1295 __ bind(&adaptor_frame);
1296 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1297 __ lea(edx, Operand(edx, ecx, times_2,
1298 StandardFrameConstants::kCallerSPOffset));
1299 __ mov(Operand(esp, 2 * kPointerSize), edx);
1300
1301 // ebx = parameter count (tagged)
1302 // ecx = argument count (tagged)
1303 // esp[4] = parameter count (tagged)
1304 // esp[8] = address of receiver argument
1305 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001306 __ cmp(ebx, ecx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001307 __ j(less_equal, &try_allocate, Label::kNear);
1308 __ mov(ebx, ecx);
1309
1310 __ bind(&try_allocate);
1311
1312 // Save mapped parameter count.
1313 __ push(ebx);
1314
1315 // Compute the sizes of backing store, parameter map, and arguments object.
1316 // 1. Parameter map, has 2 extra words containing context and backing store.
1317 const int kParameterMapHeaderSize =
1318 FixedArray::kHeaderSize + 2 * kPointerSize;
1319 Label no_parameter_map;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001320 __ test(ebx, ebx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001321 __ j(zero, &no_parameter_map, Label::kNear);
1322 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
1323 __ bind(&no_parameter_map);
1324
1325 // 2. Backing store.
1326 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
1327
1328 // 3. Arguments object.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001329 __ add(ebx, Immediate(Heap::kArgumentsObjectSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001330
1331 // Do the allocation of all three objects in one go.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001332 __ Allocate(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001333
1334 // eax = address of new object(s) (tagged)
1335 // ecx = argument count (tagged)
1336 // esp[0] = mapped parameter count (tagged)
1337 // esp[8] = parameter count (tagged)
1338 // esp[12] = address of receiver argument
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001339 // Get the arguments boilerplate from the current native context into edi.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001340 Label has_mapped_parameters, copy;
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001341 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1342 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001343 __ mov(ebx, Operand(esp, 0 * kPointerSize));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001344 __ test(ebx, ebx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001345 __ j(not_zero, &has_mapped_parameters, Label::kNear);
1346 __ mov(edi, Operand(edi,
1347 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX)));
1348 __ jmp(&copy, Label::kNear);
1349
1350 __ bind(&has_mapped_parameters);
1351 __ mov(edi, Operand(edi,
1352 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX)));
1353 __ bind(&copy);
1354
1355 // eax = address of new object (tagged)
1356 // ebx = mapped parameter count (tagged)
1357 // ecx = argument count (tagged)
1358 // edi = address of boilerplate object (tagged)
1359 // esp[0] = mapped parameter count (tagged)
1360 // esp[8] = parameter count (tagged)
1361 // esp[12] = address of receiver argument
1362 // Copy the JS object part.
1363 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
1364 __ mov(edx, FieldOperand(edi, i));
1365 __ mov(FieldOperand(eax, i), edx);
1366 }
1367
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001368 // Set up the callee in-object property.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001369 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
1370 __ mov(edx, Operand(esp, 4 * kPointerSize));
1371 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
1372 Heap::kArgumentsCalleeIndex * kPointerSize),
1373 edx);
1374
1375 // Use the length (smi tagged) and set that as an in-object property too.
1376 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1377 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
1378 Heap::kArgumentsLengthIndex * kPointerSize),
1379 ecx);
1380
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001381 // Set up the elements pointer in the allocated arguments object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001382 // If we allocated a parameter map, edi will point there, otherwise to the
1383 // backing store.
1384 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
1385 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
1386
1387 // eax = address of new object (tagged)
1388 // ebx = mapped parameter count (tagged)
1389 // ecx = argument count (tagged)
1390 // edi = address of parameter map or backing store (tagged)
1391 // esp[0] = mapped parameter count (tagged)
1392 // esp[8] = parameter count (tagged)
1393 // esp[12] = address of receiver argument
1394 // Free a register.
1395 __ push(eax);
1396
1397 // Initialize parameter map. If there are no mapped arguments, we're done.
1398 Label skip_parameter_map;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001399 __ test(ebx, ebx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001400 __ j(zero, &skip_parameter_map);
1401
1402 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001403 Immediate(isolate->factory()->non_strict_arguments_elements_map()));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001404 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
1405 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
1406 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
1407 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
1408 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
1409
1410 // Copy the parameter slots and the holes in the arguments.
1411 // We need to fill in mapped_parameter_count slots. They index the context,
1412 // where parameters are stored in reverse order, at
1413 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
1414 // The mapped parameter thus need to get indices
1415 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
1416 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
1417 // We loop from right to left.
1418 Label parameters_loop, parameters_test;
1419 __ push(ecx);
1420 __ mov(eax, Operand(esp, 2 * kPointerSize));
1421 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
1422 __ add(ebx, Operand(esp, 4 * kPointerSize));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001423 __ sub(ebx, eax);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001424 __ mov(ecx, isolate->factory()->the_hole_value());
whesse@chromium.org7b260152011-06-20 15:33:18 +00001425 __ mov(edx, edi);
1426 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
1427 // eax = loop variable (tagged)
1428 // ebx = mapping index (tagged)
1429 // ecx = the hole value
1430 // edx = address of parameter map (tagged)
1431 // edi = address of backing store (tagged)
1432 // esp[0] = argument count (tagged)
1433 // esp[4] = address of new object (tagged)
1434 // esp[8] = mapped parameter count (tagged)
1435 // esp[16] = parameter count (tagged)
1436 // esp[20] = address of receiver argument
1437 __ jmp(&parameters_test, Label::kNear);
1438
1439 __ bind(&parameters_loop);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001440 __ sub(eax, Immediate(Smi::FromInt(1)));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001441 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
1442 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001443 __ add(ebx, Immediate(Smi::FromInt(1)));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001444 __ bind(&parameters_test);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001445 __ test(eax, eax);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001446 __ j(not_zero, &parameters_loop, Label::kNear);
1447 __ pop(ecx);
1448
1449 __ bind(&skip_parameter_map);
1450
1451 // ecx = argument count (tagged)
1452 // edi = address of backing store (tagged)
1453 // esp[0] = address of new object (tagged)
1454 // esp[4] = mapped parameter count (tagged)
1455 // esp[12] = parameter count (tagged)
1456 // esp[16] = address of receiver argument
1457 // Copy arguments header and remaining slots (if there are any).
1458 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001459 Immediate(isolate->factory()->fixed_array_map()));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001460 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1461
1462 Label arguments_loop, arguments_test;
1463 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1464 __ mov(edx, Operand(esp, 4 * kPointerSize));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001465 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
1466 __ sub(edx, ebx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001467 __ jmp(&arguments_test, Label::kNear);
1468
1469 __ bind(&arguments_loop);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001470 __ sub(edx, Immediate(kPointerSize));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001471 __ mov(eax, Operand(edx, 0));
1472 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001473 __ add(ebx, Immediate(Smi::FromInt(1)));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001474
1475 __ bind(&arguments_test);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001476 __ cmp(ebx, ecx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001477 __ j(less, &arguments_loop, Label::kNear);
1478
1479 // Restore.
1480 __ pop(eax); // Address of arguments object.
1481 __ pop(ebx); // Parameter count.
1482
1483 // Return and remove the on-stack parameters.
1484 __ ret(3 * kPointerSize);
1485
1486 // Do the runtime call to allocate the arguments object.
1487 __ bind(&runtime);
1488 __ pop(eax); // Remove saved parameter count.
1489 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
danno@chromium.org72204d52012-10-31 10:02:10 +00001490 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001491}
1492
1493
1494void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001495 Isolate* isolate = masm->isolate();
1496
whesse@chromium.org7b260152011-06-20 15:33:18 +00001497 // esp[0] : return address
1498 // esp[4] : number of parameters
1499 // esp[8] : receiver displacement
1500 // esp[12] : function
ricow@chromium.org65fae842010-08-25 15:26:24 +00001501
1502 // Check if the calling frame is an arguments adaptor frame.
1503 Label adaptor_frame, try_allocate, runtime;
1504 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1505 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001506 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001507 __ j(equal, &adaptor_frame, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001508
1509 // Get the length from the frame.
1510 __ mov(ecx, Operand(esp, 1 * kPointerSize));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001511 __ jmp(&try_allocate, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001512
1513 // Patch the arguments.length and the parameters pointer.
1514 __ bind(&adaptor_frame);
1515 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1516 __ mov(Operand(esp, 1 * kPointerSize), ecx);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001517 __ lea(edx, Operand(edx, ecx, times_2,
1518 StandardFrameConstants::kCallerSPOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001519 __ mov(Operand(esp, 2 * kPointerSize), edx);
1520
1521 // Try the new space allocation. Start out with computing the size of
1522 // the arguments object and the elements array.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001523 Label add_arguments_object;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001524 __ bind(&try_allocate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001525 __ test(ecx, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001526 __ j(zero, &add_arguments_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001527 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
1528 __ bind(&add_arguments_object);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001529 __ add(ecx, Immediate(Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001530
1531 // Do the allocation of both objects in one go.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001532 __ Allocate(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001533
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001534 // Get the arguments boilerplate from the current native context.
1535 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1536 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
whesse@chromium.org7b260152011-06-20 15:33:18 +00001537 const int offset =
1538 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
1539 __ mov(edi, Operand(edi, offset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001540
1541 // Copy the JS object part.
1542 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
1543 __ mov(ebx, FieldOperand(edi, i));
1544 __ mov(FieldOperand(eax, i), ebx);
1545 }
1546
ricow@chromium.org65fae842010-08-25 15:26:24 +00001547 // Get the length (smi tagged) and set that as an in-object property too.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001548 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001549 __ mov(ecx, Operand(esp, 1 * kPointerSize));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001550 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
whesse@chromium.org7b260152011-06-20 15:33:18 +00001551 Heap::kArgumentsLengthIndex * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001552 ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001553
1554 // If there are no actual arguments, we're done.
1555 Label done;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001556 __ test(ecx, ecx);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001557 __ j(zero, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001558
1559 // Get the parameters pointer from the stack.
1560 __ mov(edx, Operand(esp, 2 * kPointerSize));
1561
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001562 // Set up the elements pointer in the allocated arguments object and
ricow@chromium.org65fae842010-08-25 15:26:24 +00001563 // initialize the header in the elements fixed array.
whesse@chromium.org7b260152011-06-20 15:33:18 +00001564 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSizeStrict));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001565 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
1566 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00001567 Immediate(isolate->factory()->fixed_array_map()));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001568
ricow@chromium.org65fae842010-08-25 15:26:24 +00001569 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1570 // Untag the length for the loop below.
1571 __ SmiUntag(ecx);
1572
1573 // Copy the fixed array slots.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001574 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001575 __ bind(&loop);
1576 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
1577 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001578 __ add(edi, Immediate(kPointerSize));
1579 __ sub(edx, Immediate(kPointerSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001580 __ dec(ecx);
1581 __ j(not_zero, &loop);
1582
1583 // Return and remove the on-stack parameters.
1584 __ bind(&done);
1585 __ ret(3 * kPointerSize);
1586
1587 // Do the runtime call to allocate the arguments object.
1588 __ bind(&runtime);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001589 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001590}
1591
1592
1593void RegExpExecStub::Generate(MacroAssembler* masm) {
1594 // Just jump directly to runtime if native RegExp is not selected at compile
1595 // time or if regexp entry in generated code is turned off runtime switch or
1596 // at compilation.
1597#ifdef V8_INTERPRETED_REGEXP
1598 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1599#else // V8_INTERPRETED_REGEXP
ricow@chromium.org65fae842010-08-25 15:26:24 +00001600
1601 // Stack frame on entry.
1602 // esp[0]: return address
1603 // esp[4]: last_match_info (expected JSArray)
1604 // esp[8]: previous index
1605 // esp[12]: subject string
1606 // esp[16]: JSRegExp object
1607
1608 static const int kLastMatchInfoOffset = 1 * kPointerSize;
1609 static const int kPreviousIndexOffset = 2 * kPointerSize;
1610 static const int kSubjectOffset = 3 * kPointerSize;
1611 static const int kJSRegExpOffset = 4 * kPointerSize;
1612
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001613 Label runtime;
1614 Factory* factory = masm->isolate()->factory();
ricow@chromium.org65fae842010-08-25 15:26:24 +00001615
1616 // Ensure that a RegExp stack is allocated.
1617 ExternalReference address_of_regexp_stack_memory_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001618 ExternalReference::address_of_regexp_stack_memory_address(
1619 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001620 ExternalReference address_of_regexp_stack_memory_size =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001621 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001622 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001623 __ test(ebx, ebx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001624 __ j(zero, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001625
1626 // Check that the first argument is a JSRegExp object.
1627 __ mov(eax, Operand(esp, kJSRegExpOffset));
1628 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00001629 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001630 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
1631 __ j(not_equal, &runtime);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001632
ricow@chromium.org65fae842010-08-25 15:26:24 +00001633 // Check that the RegExp has been compiled (data contains a fixed array).
1634 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1635 if (FLAG_debug_code) {
1636 __ test(ecx, Immediate(kSmiTagMask));
danno@chromium.org59400602013-08-13 17:09:37 +00001637 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001638 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
danno@chromium.org59400602013-08-13 17:09:37 +00001639 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001640 }
1641
1642 // ecx: RegExp data (FixedArray)
1643 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1644 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001645 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001646 __ j(not_equal, &runtime);
1647
1648 // ecx: RegExp data (FixedArray)
1649 // Check that the number of captures fit in the static offsets vector buffer.
1650 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001651 // Check (number_of_captures + 1) * 2 <= offsets vector size
1652 // Or number_of_captures * 2 <= offsets vector size - 2
1653 // Multiplying by 2 comes for free since edx is smi-tagged.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001654 STATIC_ASSERT(kSmiTag == 0);
1655 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001656 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1657 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001658 __ j(above, &runtime);
1659
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001660 // Reset offset for possibly sliced string.
1661 __ Set(edi, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001662 __ mov(eax, Operand(esp, kSubjectOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001663 __ JumpIfSmi(eax, &runtime);
1664 __ mov(edx, eax); // Make a copy of the original subject string.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001665 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1666 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001667
1668 // eax: subject string
1669 // edx: subject string
1670 // ebx: subject string instance type
1671 // ecx: RegExp data (FixedArray)
1672 // Handle subject string according to its encoding and representation:
1673 // (1) Sequential two byte? If yes, go to (9).
1674 // (2) Sequential one byte? If yes, go to (6).
1675 // (3) Anything but sequential or cons? If yes, go to (7).
1676 // (4) Cons string. If the string is flat, replace subject with first string.
1677 // Otherwise bailout.
1678 // (5a) Is subject sequential two byte? If yes, go to (9).
1679 // (5b) Is subject external? If yes, go to (8).
1680 // (6) One byte sequential. Load regexp code for one byte.
1681 // (E) Carry on.
1682 /// [...]
1683
1684 // Deferred code at the end of the stub:
1685 // (7) Not a long external string? If yes, go to (10).
1686 // (8) External string. Make it, offset-wise, look like a sequential string.
1687 // (8a) Is the external string one byte? If yes, go to (6).
1688 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1689 // (10) Short external string or not a string? If yes, bail out to runtime.
1690 // (11) Sliced string. Replace subject with parent. Go to (5a).
1691
1692 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1693 external_string /* 8 */, check_underlying /* 5a */,
1694 not_seq_nor_cons /* 7 */, check_code /* E */,
1695 not_long_external /* 10 */;
1696
1697 // (1) Sequential two byte? If yes, go to (9).
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001698 __ and_(ebx, kIsNotStringMask |
1699 kStringRepresentationMask |
1700 kStringEncodingMask |
1701 kShortExternalStringMask);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001702 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001703 __ j(zero, &seq_two_byte_string); // Go to (9).
1704
1705 // (2) Sequential one byte? If yes, go to (6).
1706 // Any other sequential string must be one byte.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001707 __ and_(ebx, Immediate(kIsNotStringMask |
1708 kStringRepresentationMask |
1709 kShortExternalStringMask));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001710 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
ricow@chromium.org65fae842010-08-25 15:26:24 +00001711
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001712 // (3) Anything but sequential or cons? If yes, go to (7).
1713 // We check whether the subject string is a cons, since sequential strings
1714 // have already been covered.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00001715 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1716 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001717 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1718 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001719 __ cmp(ebx, Immediate(kExternalStringTag));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001720 __ j(greater_equal, &not_seq_nor_cons); // Go to (7).
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001721
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001722 // (4) Cons string. Check that it's flat.
1723 // Replace subject with first string and reload instance type.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001724 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001725 __ j(not_equal, &runtime);
1726 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001727 __ bind(&check_underlying);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001728 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001729 __ mov(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1730
1731 // (5a) Is subject sequential two byte? If yes, go to (9).
1732 __ test_b(ebx, kStringRepresentationMask | kStringEncodingMask);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001733 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001734 __ j(zero, &seq_two_byte_string); // Go to (9).
1735 // (5b) Is subject external? If yes, go to (8).
1736 __ test_b(ebx, kStringRepresentationMask);
1737 // The underlying external string is never a short external string.
1738 STATIC_CHECK(ExternalString::kMaxShortLength < ConsString::kMinLength);
1739 STATIC_CHECK(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1740 __ j(not_zero, &external_string); // Go to (8).
ricow@chromium.org65fae842010-08-25 15:26:24 +00001741
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001742 // eax: sequential subject string (or look-alike, external string)
1743 // edx: original subject string
ricow@chromium.org65fae842010-08-25 15:26:24 +00001744 // ecx: RegExp data (FixedArray)
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001745 // (6) One byte sequential. Load regexp code for one byte.
1746 __ bind(&seq_one_byte_string);
1747 // Load previous index and check range before edx is overwritten. We have
1748 // to use edx instead of eax here because it might have been only made to
1749 // look like a sequential string when it actually is an external string.
1750 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1751 __ JumpIfNotSmi(ebx, &runtime);
1752 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1753 __ j(above_equal, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001754 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001755 __ Set(ecx, Immediate(1)); // Type is one byte.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001756
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001757 // (E) Carry on. String handling is done.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001758 __ bind(&check_code);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001759 // edx: irregexp code
ricow@chromium.org65fae842010-08-25 15:26:24 +00001760 // Check that the irregexp code has been generated for the actual string
1761 // encoding. If it has, the field contains a code object otherwise it contains
jkummerow@chromium.orgddda9e82011-07-06 11:27:02 +00001762 // a smi (code flushing support).
1763 __ JumpIfSmi(edx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001764
1765 // eax: subject string
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001766 // ebx: previous index (smi)
ricow@chromium.org65fae842010-08-25 15:26:24 +00001767 // edx: code
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001768 // ecx: encoding of subject string (1 if ASCII, 0 if two_byte);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001769 // All checks done. Now push arguments for native regexp code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001770 Counters* counters = masm->isolate()->counters();
1771 __ IncrementCounter(counters->regexp_entry_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001772
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001773 // Isolates: note we add an additional parameter here (isolate pointer).
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001774 static const int kRegExpExecuteArguments = 9;
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001775 __ EnterApiExitFrame(kRegExpExecuteArguments);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001776
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001777 // Argument 9: Pass current isolate address.
1778 __ mov(Operand(esp, 8 * kPointerSize),
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00001779 Immediate(ExternalReference::isolate_address(masm->isolate())));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001780
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001781 // Argument 8: Indicate that this is a direct call from JavaScript.
1782 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001783
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001784 // Argument 7: Start (high end) of backtracking stack memory area.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001785 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
1786 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001787 __ mov(Operand(esp, 6 * kPointerSize), esi);
1788
1789 // Argument 6: Set the number of capture registers to zero to force global
1790 // regexps to behave as non-global. This does not affect non-global regexps.
1791 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001792
1793 // Argument 5: static offsets vector buffer.
1794 __ mov(Operand(esp, 4 * kPointerSize),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001795 Immediate(ExternalReference::address_of_static_offsets_vector(
1796 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001797
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001798 // Argument 2: Previous index.
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001799 __ SmiUntag(ebx);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001800 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1801
1802 // Argument 1: Original subject string.
1803 // The original subject is in the previous stack frame. Therefore we have to
1804 // use ebp, which points exactly to one pointer size below the previous esp.
1805 // (Because creating a new stack frame pushes the previous ebp onto the stack
1806 // and thereby moves up esp by one kPointerSize.)
1807 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
1808 __ mov(Operand(esp, 0 * kPointerSize), esi);
1809
1810 // esi: original subject string
1811 // eax: underlying subject string
1812 // ebx: previous index
ulan@chromium.org2efb9002012-01-19 15:36:35 +00001813 // ecx: encoding of subject string (1 if ASCII 0 if two_byte);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001814 // edx: code
ricow@chromium.org65fae842010-08-25 15:26:24 +00001815 // Argument 4: End of string data
1816 // Argument 3: Start of string data
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001817 // Prepare start and end index of the input.
1818 // Load the length from the original sliced string if that is the case.
1819 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001820 __ add(esi, edi); // Calculate input end wrt offset.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001821 __ SmiUntag(edi);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001822 __ add(ebx, edi); // Calculate input start wrt offset.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001823
1824 // ebx: start index of the input string
1825 // esi: end index of the input string
1826 Label setup_two_byte, setup_rest;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001827 __ test(ecx, ecx);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001828 __ j(zero, &setup_two_byte, Label::kNear);
1829 __ SmiUntag(esi);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001830 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001831 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001832 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001833 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001834 __ jmp(&setup_rest, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001835
1836 __ bind(&setup_two_byte);
1837 STATIC_ASSERT(kSmiTag == 0);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001838 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
1839 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00001840 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1841 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
1842 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1843
1844 __ bind(&setup_rest);
1845
ricow@chromium.org65fae842010-08-25 15:26:24 +00001846 // Locate the code entry and call it.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001847 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1848 __ call(edx);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001849
1850 // Drop arguments and come back to JS mode.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00001851 __ LeaveApiExitFrame(true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001852
1853 // Check the result.
1854 Label success;
mstarzinger@chromium.org15613d02012-05-23 12:04:37 +00001855 __ cmp(eax, 1);
1856 // We expect exactly one result since we force the called regexp to behave
1857 // as non-global.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001858 __ j(equal, &success);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001859 Label failure;
1860 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001861 __ j(equal, &failure);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001862 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
1863 // If not exception it can only be retry. Handle that in the runtime system.
1864 __ j(not_equal, &runtime);
1865 // Result must now be exception. If there is no pending exception already a
1866 // stack overflow (on the backtrack stack) was detected in RegExp code but
1867 // haven't created the exception yet. Handle that in the runtime system.
1868 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00001869 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001870 masm->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001871 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001872 __ mov(eax, Operand::StaticVariable(pending_exception));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001873 __ cmp(edx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001874 __ j(equal, &runtime);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001875 // For exception, throw the exception again.
1876
1877 // Clear the pending exception variable.
1878 __ mov(Operand::StaticVariable(pending_exception), edx);
1879
1880 // Special handling of termination exceptions which are uncatchable
1881 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00001882 __ cmp(eax, factory->termination_exception());
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001883 Label throw_termination_exception;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00001884 __ j(equal, &throw_termination_exception, Label::kNear);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001885
1886 // Handle normal exception by following handler chain.
1887 __ Throw(eax);
1888
1889 __ bind(&throw_termination_exception);
ulan@chromium.org65a89c22012-02-14 11:46:07 +00001890 __ ThrowUncatchable(eax);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001891
ricow@chromium.org65fae842010-08-25 15:26:24 +00001892 __ bind(&failure);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +00001893 // For failure to match, return null.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001894 __ mov(eax, factory->null_value());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001895 __ ret(4 * kPointerSize);
1896
1897 // Load RegExp data.
1898 __ bind(&success);
1899 __ mov(eax, Operand(esp, kJSRegExpOffset));
1900 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1901 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1902 // Calculate number of capture registers (number_of_captures + 1) * 2.
1903 STATIC_ASSERT(kSmiTag == 0);
1904 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001905 __ add(edx, Immediate(2)); // edx was a smi.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001906
1907 // edx: Number of capture registers
1908 // Load last_match_info which is still known to be a fast case JSArray.
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001909 // Check that the fourth object is a JSArray object.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001910 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001911 __ JumpIfSmi(eax, &runtime);
1912 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
1913 __ j(not_equal, &runtime);
1914 // Check that the JSArray is in fast case.
ricow@chromium.org65fae842010-08-25 15:26:24 +00001915 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001916 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1917 __ cmp(eax, factory->fixed_array_map());
1918 __ j(not_equal, &runtime);
1919 // Check that the last match info has space for the capture registers and the
1920 // additional information.
1921 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1922 __ SmiUntag(eax);
1923 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1924 __ cmp(edx, eax);
1925 __ j(greater, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001926
1927 // ebx: last_match_info backing store (FixedArray)
1928 // edx: number of capture registers
1929 // Store the capture count.
1930 __ SmiTag(edx); // Number of capture registers to smi.
1931 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1932 __ SmiUntag(edx); // Number of capture registers back from smi.
1933 // Store last subject and last input.
1934 __ mov(eax, Operand(esp, kSubjectOffset));
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001935 __ mov(ecx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001936 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001937 __ RecordWriteField(ebx,
1938 RegExpImpl::kLastSubjectOffset,
1939 eax,
1940 edi,
1941 kDontSaveFPRegs);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001942 __ mov(eax, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001943 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001944 __ RecordWriteField(ebx,
1945 RegExpImpl::kLastInputOffset,
1946 eax,
1947 edi,
1948 kDontSaveFPRegs);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001949
1950 // Get the static offsets vector filled by the native regexp code.
1951 ExternalReference address_of_static_offsets_vector =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001952 ExternalReference::address_of_static_offsets_vector(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00001953 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1954
1955 // ebx: last_match_info backing store (FixedArray)
1956 // ecx: offsets vector
1957 // edx: number of capture registers
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001958 Label next_capture, done;
ricow@chromium.org65fae842010-08-25 15:26:24 +00001959 // Capture register counter starts from number of capture registers and
1960 // counts down until wraping after zero.
1961 __ bind(&next_capture);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001962 __ sub(edx, Immediate(1));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001963 __ j(negative, &done, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00001964 // Read the value from the static offsets vector buffer.
1965 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1966 __ SmiTag(edi);
1967 // Store the smi value in the last match info.
1968 __ mov(FieldOperand(ebx,
1969 edx,
1970 times_pointer_size,
1971 RegExpImpl::kFirstCaptureOffset),
1972 edi);
1973 __ jmp(&next_capture);
1974 __ bind(&done);
1975
1976 // Return last match info.
1977 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1978 __ ret(4 * kPointerSize);
1979
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001980 // Do the runtime call to execute the regexp.
1981 __ bind(&runtime);
1982 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1983
1984 // Deferred code for string handling.
1985 // (7) Not a long external string? If yes, go to (10).
1986 __ bind(&not_seq_nor_cons);
1987 // Compare flags are still set from (3).
1988 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
1989
1990 // (8) External string. Short external strings have been ruled out.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001991 __ bind(&external_string);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00001992 // Reload instance type.
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00001993 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1994 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1995 if (FLAG_debug_code) {
1996 // Assert that we do not have a cons or slice (indirect strings) here.
1997 // Sequential strings have already been ruled out.
1998 __ test_b(ebx, kIsIndirectStringMask);
danno@chromium.org59400602013-08-13 17:09:37 +00001999 __ Assert(zero, kExternalStringExpectedButNotFound);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002000 }
2001 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
2002 // Move the pointer so that offset-wise, it looks like a sequential string.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002003 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002004 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2005 STATIC_ASSERT(kTwoByteStringTag == 0);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00002006 // (8a) Is the external string one byte? If yes, go to (6).
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002007 __ test_b(ebx, kStringEncodingMask);
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00002008 __ j(not_zero, &seq_one_byte_string); // Goto (6).
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +00002009
mstarzinger@chromium.org71fc3462013-02-27 09:34:27 +00002010 // eax: sequential subject string (or look-alike, external string)
2011 // edx: original subject string
2012 // ecx: RegExp data (FixedArray)
2013 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
2014 __ bind(&seq_two_byte_string);
2015 // Load previous index and check range before edx is overwritten. We have
2016 // to use edx instead of eax here because it might have been only made to
2017 // look like a sequential string when it actually is an external string.
2018 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
2019 __ JumpIfNotSmi(ebx, &runtime);
2020 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
2021 __ j(above_equal, &runtime);
2022 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
2023 __ Set(ecx, Immediate(0)); // Type is two byte.
2024 __ jmp(&check_code); // Go to (E).
2025
2026 // (10) Not a string or a short external string? If yes, bail out to runtime.
2027 __ bind(&not_long_external);
2028 // Catch non-string subject or short external string.
2029 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2030 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
2031 __ j(not_zero, &runtime);
2032
2033 // (11) Sliced string. Replace subject with parent. Go to (5a).
2034 // Load offset into edi and replace subject string with parent.
2035 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
2036 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
2037 __ jmp(&check_underlying); // Go to (5a).
ricow@chromium.org65fae842010-08-25 15:26:24 +00002038#endif // V8_INTERPRETED_REGEXP
2039}
2040
2041
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002042void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
2043 const int kMaxInlineLength = 100;
2044 Label slowcase;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002045 Label done;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002046 __ mov(ebx, Operand(esp, kPointerSize * 3));
whesse@chromium.org7b260152011-06-20 15:33:18 +00002047 __ JumpIfNotSmi(ebx, &slowcase);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002048 __ cmp(ebx, Immediate(Smi::FromInt(kMaxInlineLength)));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002049 __ j(above, &slowcase);
2050 // Smi-tagging is equivalent to multiplying by 2.
2051 STATIC_ASSERT(kSmiTag == 0);
2052 STATIC_ASSERT(kSmiTagSize == 1);
2053 // Allocate RegExpResult followed by FixedArray with size in ebx.
2054 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
2055 // Elements: [Map][Length][..elements..]
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002056 __ Allocate(JSRegExpResult::kSize + FixedArray::kHeaderSize,
2057 times_pointer_size,
2058 ebx, // In: Number of elements as a smi
2059 REGISTER_VALUE_IS_SMI,
2060 eax, // Out: Start of allocation (tagged).
2061 ecx, // Out: End of allocation.
2062 edx, // Scratch register
2063 &slowcase,
2064 TAG_OBJECT);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002065 // eax: Start of allocated area, object-tagged.
2066
2067 // Set JSArray map to global.regexp_result_map().
2068 // Set empty properties FixedArray.
2069 // Set elements to point to FixedArray allocated right after the JSArray.
2070 // Interleave operations for better latency.
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002071 __ mov(edx, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002072 Factory* factory = masm->isolate()->factory();
2073 __ mov(ecx, Immediate(factory->empty_fixed_array()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002074 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00002075 __ mov(edx, FieldOperand(edx, GlobalObject::kNativeContextOffset));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002076 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
2077 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
2078 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
2079 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
2080
2081 // Set input, index and length fields from arguments.
2082 __ mov(ecx, Operand(esp, kPointerSize * 1));
2083 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
2084 __ mov(ecx, Operand(esp, kPointerSize * 2));
2085 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
2086 __ mov(ecx, Operand(esp, kPointerSize * 3));
2087 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
2088
2089 // Fill out the elements FixedArray.
2090 // eax: JSArray.
2091 // ebx: FixedArray.
2092 // ecx: Number of elements in array, as smi.
2093
2094 // Set map.
2095 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002096 Immediate(factory->fixed_array_map()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002097 // Set length.
2098 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002099 // Fill contents of fixed-array with undefined.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002100 __ SmiUntag(ecx);
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002101 __ mov(edx, Immediate(factory->undefined_value()));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002102 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002103 // Fill fixed array elements with undefined.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002104 // eax: JSArray.
2105 // ecx: Number of elements to fill.
2106 // ebx: Start of elements in FixedArray.
ulan@chromium.org56c14af2012-09-20 12:51:09 +00002107 // edx: undefined.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002108 Label loop;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002109 __ test(ecx, ecx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002110 __ bind(&loop);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002111 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002112 __ sub(ecx, Immediate(1));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002113 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
2114 __ jmp(&loop);
2115
2116 __ bind(&done);
2117 __ ret(3 * kPointerSize);
2118
2119 __ bind(&slowcase);
2120 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
2121}
2122
2123
ricow@chromium.org65fae842010-08-25 15:26:24 +00002124static int NegativeComparisonResult(Condition cc) {
2125 ASSERT(cc != equal);
2126 ASSERT((cc == less) || (cc == less_equal)
2127 || (cc == greater) || (cc == greater_equal));
2128 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
2129}
2130
ricow@chromium.org65fae842010-08-25 15:26:24 +00002131
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002132static void CheckInputType(MacroAssembler* masm,
2133 Register input,
2134 CompareIC::State expected,
2135 Label* fail) {
2136 Label ok;
2137 if (expected == CompareIC::SMI) {
2138 __ JumpIfNotSmi(input, fail);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002139 } else if (expected == CompareIC::NUMBER) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002140 __ JumpIfSmi(input, &ok);
2141 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
2142 Immediate(masm->isolate()->factory()->heap_number_map()));
2143 __ j(not_equal, fail);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002144 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002145 // We could be strict about internalized/non-internalized here, but as long as
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002146 // hydrogen doesn't care, the stub doesn't have to care either.
2147 __ bind(&ok);
2148}
2149
2150
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002151static void BranchIfNotInternalizedString(MacroAssembler* masm,
2152 Label* label,
2153 Register object,
2154 Register scratch) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002155 __ JumpIfSmi(object, label);
2156 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
2157 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00002158 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2159 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2160 __ j(not_zero, label);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002161}
2162
2163
2164void ICCompareStub::GenerateGeneric(MacroAssembler* masm) {
2165 Label check_unequal_objects;
2166 Condition cc = GetCondition();
2167
2168 Label miss;
2169 CheckInputType(masm, edx, left_, &miss);
2170 CheckInputType(masm, eax, right_, &miss);
2171
2172 // Compare two smis.
2173 Label non_smi, smi_done;
2174 __ mov(ecx, edx);
2175 __ or_(ecx, eax);
2176 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
2177 __ sub(edx, eax); // Return on the result of the subtraction.
2178 __ j(no_overflow, &smi_done, Label::kNear);
2179 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
2180 __ bind(&smi_done);
2181 __ mov(eax, edx);
2182 __ ret(0);
2183 __ bind(&non_smi);
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002184
ricow@chromium.org65fae842010-08-25 15:26:24 +00002185 // NOTICE! This code is only reached after a smi-fast-case check, so
2186 // it is certain that at least one operand isn't a smi.
2187
2188 // Identical objects can be compared fast, but there are some tricky cases
2189 // for NaN and undefined.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002190 Label generic_heap_number_comparison;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002191 {
2192 Label not_identical;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002193 __ cmp(eax, edx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002194 __ j(not_equal, &not_identical);
2195
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002196 if (cc != equal) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002197 // Check for undefined. undefined OP undefined is false even though
2198 // undefined == undefined.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002199 Label check_for_nan;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002200 __ cmp(edx, masm->isolate()->factory()->undefined_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002201 __ j(not_equal, &check_for_nan, Label::kNear);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002202 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002203 __ ret(0);
2204 __ bind(&check_for_nan);
2205 }
2206
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002207 // Test for NaN. Compare heap numbers in a general way,
2208 // to hanlde NaNs correctly.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002209 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2210 Immediate(masm->isolate()->factory()->heap_number_map()));
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002211 __ j(equal, &generic_heap_number_comparison, Label::kNear);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002212 if (cc != equal) {
2213 // Call runtime on identical JSObjects. Otherwise return equal.
2214 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2215 __ j(above_equal, &not_identical);
2216 }
2217 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
2218 __ ret(0);
2219
ricow@chromium.org65fae842010-08-25 15:26:24 +00002220
2221 __ bind(&not_identical);
2222 }
2223
2224 // Strict equality can quickly decide whether objects are equal.
2225 // Non-strict object equality is slower, so it is handled later in the stub.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002226 if (cc == equal && strict()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002227 Label slow; // Fallthrough label.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002228 Label not_smis;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002229 // If we're doing a strict equality comparison, we don't have to do
2230 // type conversion, so we generate code to do fast comparison for objects
2231 // and oddballs. Non-smi numbers and strings still go through the usual
2232 // slow-case code.
2233 // If either is a Smi (we know that not both are), then they can only
2234 // be equal if the other is a HeapNumber. If so, use the slow case.
2235 STATIC_ASSERT(kSmiTag == 0);
2236 ASSERT_EQ(0, Smi::FromInt(0));
2237 __ mov(ecx, Immediate(kSmiTagMask));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002238 __ and_(ecx, eax);
2239 __ test(ecx, edx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002240 __ j(not_zero, &not_smis, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002241 // One operand is a smi.
2242
2243 // Check whether the non-smi is a heap number.
2244 STATIC_ASSERT(kSmiTagMask == 1);
2245 // ecx still holds eax & kSmiTag, which is either zero or one.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002246 __ sub(ecx, Immediate(0x01));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002247 __ mov(ebx, edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002248 __ xor_(ebx, eax);
2249 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
2250 __ xor_(ebx, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002251 // if eax was smi, ebx is now edx, else eax.
2252
2253 // Check if the non-smi operand is a heap number.
2254 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002255 Immediate(masm->isolate()->factory()->heap_number_map()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002256 // If heap number, handle it in the slow case.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002257 __ j(equal, &slow, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002258 // Return non-equal (ebx is not zero)
2259 __ mov(eax, ebx);
2260 __ ret(0);
2261
2262 __ bind(&not_smis);
2263 // If either operand is a JSObject or an oddball value, then they are not
2264 // equal since their pointers are different
2265 // There is no test for undetectability in strict equality.
2266
2267 // Get the type of the first operand.
2268 // If the first object is a JS object, we have done pointer comparison.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002269 Label first_non_object;
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002270 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
2271 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002272 __ j(below, &first_non_object, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002273
2274 // Return non-zero (eax is not zero)
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002275 Label return_not_equal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002276 STATIC_ASSERT(kHeapObjectTag != 0);
2277 __ bind(&return_not_equal);
2278 __ ret(0);
2279
2280 __ bind(&first_non_object);
2281 // Check for oddballs: true, false, null, undefined.
2282 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2283 __ j(equal, &return_not_equal);
2284
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002285 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002286 __ j(above_equal, &return_not_equal);
2287
2288 // Check for oddballs: true, false, null, undefined.
2289 __ CmpInstanceType(ecx, ODDBALL_TYPE);
2290 __ j(equal, &return_not_equal);
2291
2292 // Fall through to the general case.
2293 __ bind(&slow);
2294 }
2295
2296 // Generate the number comparison code.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002297 Label non_number_comparison;
2298 Label unordered;
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00002299 __ bind(&generic_heap_number_comparison);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002300 if (CpuFeatures::IsSupported(SSE2)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00002301 CpuFeatureScope use_sse2(masm, SSE2);
2302 CpuFeatureScope use_cmov(masm, CMOV);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002303
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002304 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
2305 __ ucomisd(xmm0, xmm1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002306
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002307 // Don't base result on EFLAGS when a NaN is involved.
2308 __ j(parity_even, &unordered, Label::kNear);
2309 // Return a result of -1, 0, or 1, based on EFLAGS.
2310 __ mov(eax, 0); // equal
2311 __ mov(ecx, Immediate(Smi::FromInt(1)));
2312 __ cmov(above, eax, ecx);
2313 __ mov(ecx, Immediate(Smi::FromInt(-1)));
2314 __ cmov(below, eax, ecx);
2315 __ ret(0);
2316 } else {
2317 FloatingPointHelper::CheckFloatOperands(
2318 masm, &non_number_comparison, ebx);
2319 FloatingPointHelper::LoadFloatOperand(masm, eax);
2320 FloatingPointHelper::LoadFloatOperand(masm, edx);
2321 __ FCmp();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002322
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002323 // Don't base result on EFLAGS when a NaN is involved.
2324 __ j(parity_even, &unordered, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002325
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002326 Label below_label, above_label;
2327 // Return a result of -1, 0, or 1, based on EFLAGS.
2328 __ j(below, &below_label, Label::kNear);
2329 __ j(above, &above_label, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002330
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002331 __ Set(eax, Immediate(0));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002332 __ ret(0);
2333
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002334 __ bind(&below_label);
2335 __ mov(eax, Immediate(Smi::FromInt(-1)));
2336 __ ret(0);
2337
2338 __ bind(&above_label);
2339 __ mov(eax, Immediate(Smi::FromInt(1)));
2340 __ ret(0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002341 }
2342
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002343 // If one of the numbers was NaN, then the result is always false.
2344 // The cc is never not-equal.
2345 __ bind(&unordered);
2346 ASSERT(cc != not_equal);
2347 if (cc == less || cc == less_equal) {
2348 __ mov(eax, Immediate(Smi::FromInt(1)));
2349 } else {
2350 __ mov(eax, Immediate(Smi::FromInt(-1)));
2351 }
2352 __ ret(0);
2353
2354 // The number comparison code did not provide a valid result.
2355 __ bind(&non_number_comparison);
2356
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002357 // Fast negative check for internalized-to-internalized equality.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002358 Label check_for_strings;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002359 if (cc == equal) {
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002360 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
2361 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002362
2363 // We've already checked for object identity, so if both operands
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002364 // are internalized they aren't equal. Register eax already holds a
ricow@chromium.org65fae842010-08-25 15:26:24 +00002365 // non-zero value, which indicates not equal, so just return.
2366 __ ret(0);
2367 }
2368
2369 __ bind(&check_for_strings);
2370
2371 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
2372 &check_unequal_objects);
2373
ulan@chromium.org2efb9002012-01-19 15:36:35 +00002374 // Inline comparison of ASCII strings.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002375 if (cc == equal) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00002376 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
ricow@chromium.org65fae842010-08-25 15:26:24 +00002377 edx,
2378 eax,
2379 ecx,
lrn@chromium.org1c092762011-05-09 09:42:16 +00002380 ebx);
2381 } else {
2382 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
2383 edx,
2384 eax,
2385 ecx,
2386 ebx,
2387 edi);
2388 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00002389#ifdef DEBUG
danno@chromium.org59400602013-08-13 17:09:37 +00002390 __ Abort(kUnexpectedFallThroughFromStringComparison);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002391#endif
2392
2393 __ bind(&check_unequal_objects);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002394 if (cc == equal && !strict()) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002395 // Non-strict equality. Objects are unequal if
2396 // they are both JSObjects and not undetectable,
2397 // and their pointers are different.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002398 Label not_both_objects;
2399 Label return_unequal;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002400 // At most one is a smi, so we can test for smi by adding the two.
2401 // A smi plus a heap object has the low bit set, a heap object plus
2402 // a heap object has the low bit clear.
2403 STATIC_ASSERT(kSmiTag == 0);
2404 STATIC_ASSERT(kSmiTagMask == 1);
2405 __ lea(ecx, Operand(eax, edx, times_1, 0));
2406 __ test(ecx, Immediate(kSmiTagMask));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002407 __ j(not_zero, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002408 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002409 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002410 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002411 __ j(below, &not_both_objects, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002412 // We do not bail out after this point. Both are JSObjects, and
2413 // they are equal if and only if both are undetectable.
2414 // The and of the undetectable flags is 1 if and only if they are equal.
2415 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
2416 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002417 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002418 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
2419 1 << Map::kIsUndetectable);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002420 __ j(zero, &return_unequal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002421 // The objects are both undetectable, so they both compare as the value
2422 // undefined, and are equal.
2423 __ Set(eax, Immediate(EQUAL));
2424 __ bind(&return_unequal);
2425 // Return non-equal by returning the non-zero object pointer in eax,
2426 // or return equal if we fell through to here.
2427 __ ret(0); // rax, rdx were pushed
2428 __ bind(&not_both_objects);
2429 }
2430
2431 // Push arguments below the return address.
2432 __ pop(ecx);
2433 __ push(edx);
2434 __ push(eax);
2435
2436 // Figure out which native to call and setup the arguments.
2437 Builtins::JavaScript builtin;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002438 if (cc == equal) {
2439 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002440 } else {
2441 builtin = Builtins::COMPARE;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002442 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002443 }
2444
2445 // Restore return address on the stack.
2446 __ push(ecx);
2447
2448 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
2449 // tagged as a small integer.
2450 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002451
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002452 __ bind(&miss);
2453 GenerateMiss(masm);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002454}
2455
2456
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002457static void GenerateRecordCallTarget(MacroAssembler* masm) {
2458 // Cache the called function in a global property cell. Cache states
2459 // are uninitialized, monomorphic (indicated by a JSFunction), and
2460 // megamorphic.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002461 // eax : number of arguments to the construct function
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002462 // ebx : cache cell for call target
2463 // edi : the function to call
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002464 Isolate* isolate = masm->isolate();
2465 Label initialize, done, miss, megamorphic, not_array_function;
2466
2467 // Load the cache state into ecx.
danno@chromium.org41728482013-06-12 22:31:22 +00002468 __ mov(ecx, FieldOperand(ebx, Cell::kValueOffset));
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002469
2470 // A monomorphic cache hit or an already megamorphic state: invoke the
2471 // function without changing the state.
2472 __ cmp(ecx, edi);
2473 __ j(equal, &done);
2474 __ cmp(ecx, Immediate(TypeFeedbackCells::MegamorphicSentinel(isolate)));
2475 __ j(equal, &done);
2476
danno@chromium.orgbee51992013-07-10 14:57:15 +00002477 // If we came here, we need to see if we are the array function.
2478 // If we didn't have a matching function, and we didn't find the megamorph
2479 // sentinel, then we have in the cell either some other function or an
2480 // AllocationSite. Do a map check on the object in ecx.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002481 Handle<Map> allocation_site_map =
2482 masm->isolate()->factory()->allocation_site_map();
danno@chromium.orgbee51992013-07-10 14:57:15 +00002483 __ cmp(FieldOperand(ecx, 0), Immediate(allocation_site_map));
2484 __ j(not_equal, &miss);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002485
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002486 // Load the global or builtins object from the current context
2487 __ LoadGlobalContext(ecx);
2488 // Make sure the function is the Array() function
2489 __ cmp(edi, Operand(ecx,
2490 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
2491 __ j(not_equal, &megamorphic);
2492 __ jmp(&done);
2493
2494 __ bind(&miss);
2495
2496 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2497 // megamorphic.
2498 __ cmp(ecx, Immediate(TypeFeedbackCells::UninitializedSentinel(isolate)));
2499 __ j(equal, &initialize);
2500 // MegamorphicSentinel is an immortal immovable object (undefined) so no
2501 // write-barrier is needed.
2502 __ bind(&megamorphic);
danno@chromium.org41728482013-06-12 22:31:22 +00002503 __ mov(FieldOperand(ebx, Cell::kValueOffset),
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002504 Immediate(TypeFeedbackCells::MegamorphicSentinel(isolate)));
2505 __ jmp(&done, Label::kNear);
2506
2507 // An uninitialized cache is patched with the function or sentinel to
2508 // indicate the ElementsKind if function is the Array constructor.
2509 __ bind(&initialize);
2510 __ LoadGlobalContext(ecx);
2511 // Make sure the function is the Array() function
2512 __ cmp(edi, Operand(ecx,
2513 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
2514 __ j(not_equal, &not_array_function);
2515
danno@chromium.orgbee51992013-07-10 14:57:15 +00002516 // The target function is the Array constructor,
2517 // Create an AllocationSite if we don't already have it, store it in the cell
2518 {
2519 FrameScope scope(masm, StackFrame::INTERNAL);
2520
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002521 // Arguments register must be smi-tagged to call out.
verwaest@chromium.org662436e2013-08-28 08:41:27 +00002522 __ SmiTag(eax);
danno@chromium.orgbee51992013-07-10 14:57:15 +00002523 __ push(eax);
2524 __ push(edi);
2525 __ push(ebx);
2526
2527 CreateAllocationSiteStub create_stub;
2528 __ CallStub(&create_stub);
2529
2530 __ pop(ebx);
2531 __ pop(edi);
2532 __ pop(eax);
verwaest@chromium.org662436e2013-08-28 08:41:27 +00002533 __ SmiUntag(eax);
danno@chromium.orgbee51992013-07-10 14:57:15 +00002534 }
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002535 __ jmp(&done);
2536
2537 __ bind(&not_array_function);
danno@chromium.org41728482013-06-12 22:31:22 +00002538 __ mov(FieldOperand(ebx, Cell::kValueOffset), edi);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002539 // No need for a write barrier here - cells are rescanned.
2540
2541 __ bind(&done);
2542}
2543
2544
ricow@chromium.org65fae842010-08-25 15:26:24 +00002545void CallFunctionStub::Generate(MacroAssembler* masm) {
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002546 // ebx : cache cell for call target
danno@chromium.orgc612e022011-11-10 11:38:15 +00002547 // edi : the function to call
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002548 Isolate* isolate = masm->isolate();
lrn@chromium.org34e60782011-09-15 07:25:40 +00002549 Label slow, non_function;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002550
rossberg@chromium.org9ed27462014-01-07 14:16:41 +00002551 // Check that the function really is a JavaScript function.
2552 __ JumpIfSmi(edi, &non_function);
2553
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002554 // Goto slow case if we do not have a function.
2555 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2556 __ j(not_equal, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002557
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002558 if (RecordCallTarget()) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002559 GenerateRecordCallTarget(masm);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002560 }
2561
ricow@chromium.org65fae842010-08-25 15:26:24 +00002562 // Fast-case: Just invoke the function.
2563 ParameterCount actual(argc_);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002564
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002565 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002566
2567 // Slow-case: Non-function called.
2568 __ bind(&slow);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002569 if (RecordCallTarget()) {
2570 // If there is a call target cache, mark it megamorphic in the
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002571 // non-function case. MegamorphicSentinel is an immortal immovable
2572 // object (undefined) so no write barrier is needed.
danno@chromium.org41728482013-06-12 22:31:22 +00002573 __ mov(FieldOperand(ebx, Cell::kValueOffset),
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002574 Immediate(TypeFeedbackCells::MegamorphicSentinel(isolate)));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002575 }
lrn@chromium.org34e60782011-09-15 07:25:40 +00002576 // Check for function proxy.
2577 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2578 __ j(not_equal, &non_function);
2579 __ pop(ecx);
2580 __ push(edi); // put proxy as additional argument under return address
2581 __ push(ecx);
2582 __ Set(eax, Immediate(argc_ + 1));
2583 __ Set(ebx, Immediate(0));
lrn@chromium.org34e60782011-09-15 07:25:40 +00002584 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
2585 {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002586 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
lrn@chromium.org34e60782011-09-15 07:25:40 +00002587 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2588 }
2589
ricow@chromium.org65fae842010-08-25 15:26:24 +00002590 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2591 // of the original receiver from the call site).
lrn@chromium.org34e60782011-09-15 07:25:40 +00002592 __ bind(&non_function);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002593 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
2594 __ Set(eax, Immediate(argc_));
2595 __ Set(ebx, Immediate(0));
2596 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002597 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002598 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2599}
2600
2601
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002602void CallConstructStub::Generate(MacroAssembler* masm) {
2603 // eax : number of arguments
2604 // ebx : cache cell for call target
2605 // edi : constructor function
2606 Label slow, non_function_call;
2607
2608 // Check that function is not a smi.
2609 __ JumpIfSmi(edi, &non_function_call);
2610 // Check that function is a JSFunction.
2611 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2612 __ j(not_equal, &slow);
2613
2614 if (RecordCallTarget()) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002615 GenerateRecordCallTarget(masm);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002616 }
2617
2618 // Jump to the function-specific construct stub.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002619 Register jmp_reg = ecx;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00002620 __ mov(jmp_reg, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2621 __ mov(jmp_reg, FieldOperand(jmp_reg,
2622 SharedFunctionInfo::kConstructStubOffset));
2623 __ lea(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
2624 __ jmp(jmp_reg);
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002625
2626 // edi: called object
2627 // eax: number of arguments
2628 // ecx: object map
2629 Label do_call;
2630 __ bind(&slow);
2631 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2632 __ j(not_equal, &non_function_call);
2633 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
2634 __ jmp(&do_call);
2635
2636 __ bind(&non_function_call);
2637 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2638 __ bind(&do_call);
2639 // Set expected number of arguments to zero (not changing eax).
2640 __ Set(ebx, Immediate(0));
2641 Handle<Code> arguments_adaptor =
2642 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
danno@chromium.orgfa458e42012-02-01 10:48:36 +00002643 __ jmp(arguments_adaptor, RelocInfo::CODE_TARGET);
2644}
2645
2646
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002647bool CEntryStub::NeedsImmovableCode() {
2648 return false;
2649}
2650
2651
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002652void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2653 CEntryStub::GenerateAheadOfTime(isolate);
2654 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002655 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002656 // It is important that the store buffer overflow stubs are generated first.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002657 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
danno@chromium.orgbee51992013-07-10 14:57:15 +00002658 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
mvstanton@chromium.org182d2db2013-10-10 11:03:05 +00002659 if (Serializer::enabled()) {
2660 PlatformFeatureScope sse2(SSE2);
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00002661 BinaryOpICStub::GenerateAheadOfTime(isolate);
ulan@chromium.org0f13e742014-01-03 15:51:11 +00002662 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
mvstanton@chromium.org182d2db2013-10-10 11:03:05 +00002663 } else {
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00002664 BinaryOpICStub::GenerateAheadOfTime(isolate);
ulan@chromium.org0f13e742014-01-03 15:51:11 +00002665 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
mvstanton@chromium.org182d2db2013-10-10 11:03:05 +00002666 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002667}
2668
2669
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002670void CodeStub::GenerateFPStubs(Isolate* isolate) {
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002671 if (CpuFeatures::IsSupported(SSE2)) {
2672 CEntryStub save_doubles(1, kSaveFPRegs);
2673 // Stubs might already be in the snapshot, detect that and don't regenerate,
2674 // which would lead to code stub initialization state being messed up.
2675 Code* save_doubles_code;
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002676 if (!save_doubles.FindCodeInCache(&save_doubles_code, isolate)) {
2677 save_doubles_code = *(save_doubles.GetCode(isolate));
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002678 }
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002679 isolate->set_fp_stubs_generated(true);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00002680 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002681}
2682
2683
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002684void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002685 CEntryStub stub(1, kDontSaveFPRegs);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00002686 stub.GetCode(isolate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002687}
2688
2689
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00002690static void JumpIfOOM(MacroAssembler* masm,
2691 Register value,
2692 Register scratch,
2693 Label* oom_label) {
2694 __ mov(scratch, value);
2695 STATIC_ASSERT(Failure::OUT_OF_MEMORY_EXCEPTION == 3);
2696 STATIC_ASSERT(kFailureTag == 3);
2697 __ and_(scratch, 0xf);
2698 __ cmp(scratch, 0xf);
2699 __ j(equal, oom_label);
2700}
2701
2702
ricow@chromium.org65fae842010-08-25 15:26:24 +00002703void CEntryStub::GenerateCore(MacroAssembler* masm,
2704 Label* throw_normal_exception,
2705 Label* throw_termination_exception,
2706 Label* throw_out_of_memory_exception,
2707 bool do_gc,
ager@chromium.org0ee099b2011-01-25 14:06:47 +00002708 bool always_allocate_scope) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00002709 // eax: result parameter for PerformGC, if any
2710 // ebx: pointer to C function (C callee-saved)
2711 // ebp: frame pointer (restored after C call)
2712 // esp: stack pointer (restored after C call)
2713 // edi: number of arguments including receiver (C callee-saved)
2714 // esi: pointer to the first argument (C callee-saved)
2715
2716 // Result returned in eax, or eax+edx if result_size_ is 2.
2717
2718 // Check stack alignment.
2719 if (FLAG_debug_code) {
2720 __ CheckStackAlignment();
2721 }
2722
2723 if (do_gc) {
2724 // Pass failure code returned from last attempt as first argument to
2725 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
2726 // stack alignment is known to be correct. This function takes one argument
2727 // which is passed on the stack, and we know that the stack has been
2728 // prepared to pass at least one argument.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002729 __ mov(Operand(esp, 1 * kPointerSize),
2730 Immediate(ExternalReference::isolate_address(masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002731 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
2732 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
2733 }
2734
2735 ExternalReference scope_depth =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002736 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002737 if (always_allocate_scope) {
2738 __ inc(Operand::StaticVariable(scope_depth));
2739 }
2740
2741 // Call C function.
2742 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
2743 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002744 __ mov(Operand(esp, 2 * kPointerSize),
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00002745 Immediate(ExternalReference::isolate_address(masm->isolate())));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002746 __ call(ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002747 // Result is in eax or edx:eax - do not destroy these registers!
2748
2749 if (always_allocate_scope) {
2750 __ dec(Operand::StaticVariable(scope_depth));
2751 }
2752
danno@chromium.orgca29dd82013-04-26 11:59:48 +00002753 // Runtime functions should not return 'the hole'. Allowing it to escape may
2754 // lead to crashes in the IC code later.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002755 if (FLAG_debug_code) {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002756 Label okay;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002757 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002758 __ j(not_equal, &okay, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002759 __ int3();
2760 __ bind(&okay);
2761 }
2762
2763 // Check for failure result.
2764 Label failure_returned;
2765 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
2766 __ lea(ecx, Operand(eax, 1));
2767 // Lower 2 bits of ecx are 0 iff eax has failure tag.
2768 __ test(ecx, Immediate(kFailureTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002769 __ j(zero, &failure_returned);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002770
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002771 ExternalReference pending_exception_address(
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002772 Isolate::kPendingExceptionAddress, masm->isolate());
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002773
2774 // Check that there is no pending exception, otherwise we
2775 // should have returned some failure value.
2776 if (FLAG_debug_code) {
2777 __ push(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002778 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002779 Label okay;
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002780 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
2781 // Cannot use check here as it attempts to generate call into runtime.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002782 __ j(equal, &okay, Label::kNear);
erik.corry@gmail.comd91075f2011-02-10 07:45:38 +00002783 __ int3();
2784 __ bind(&okay);
2785 __ pop(edx);
2786 }
2787
ricow@chromium.org65fae842010-08-25 15:26:24 +00002788 // Exit the JavaScript to C++ exit frame.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002789 __ LeaveExitFrame(save_doubles_ == kSaveFPRegs);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002790 __ ret(0);
2791
2792 // Handling of failure.
2793 __ bind(&failure_returned);
2794
2795 Label retry;
2796 // If the returned exception is RETRY_AFTER_GC continue at retry label
2797 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
2798 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002799 __ j(zero, &retry, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002800
2801 // Special handling of out of memory exceptions.
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00002802 JumpIfOOM(masm, eax, ecx, throw_out_of_memory_exception);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002803
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002804 // Retrieve the pending exception.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002805 __ mov(eax, Operand::StaticVariable(pending_exception_address));
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002806
2807 // See if we just retrieved an OOM exception.
2808 JumpIfOOM(masm, eax, ecx, throw_out_of_memory_exception);
2809
2810 // Clear the pending exception.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002811 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002812 __ mov(Operand::StaticVariable(pending_exception_address), edx);
2813
2814 // Special handling of termination exceptions which are uncatchable
2815 // by javascript code.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002816 __ cmp(eax, masm->isolate()->factory()->termination_exception());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002817 __ j(equal, throw_termination_exception);
2818
2819 // Handle normal exception.
2820 __ jmp(throw_normal_exception);
2821
2822 // Retry.
2823 __ bind(&retry);
2824}
2825
2826
ricow@chromium.org65fae842010-08-25 15:26:24 +00002827void CEntryStub::Generate(MacroAssembler* masm) {
2828 // eax: number of arguments including receiver
2829 // ebx: pointer to C function (C callee-saved)
2830 // ebp: frame pointer (restored after C call)
2831 // esp: stack pointer (restored after C call)
2832 // esi: current context (C callee-saved)
2833 // edi: JS function of the caller (C callee-saved)
2834
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002835 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2836
ricow@chromium.org65fae842010-08-25 15:26:24 +00002837 // NOTE: Invocations of builtins may return failure objects instead
2838 // of a proper result. The builtin entry handles this by performing
2839 // a garbage collection and retrying the builtin (twice).
2840
2841 // Enter the exit frame that transitions from JavaScript to C++.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002842 __ EnterExitFrame(save_doubles_ == kSaveFPRegs);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002843
2844 // eax: result parameter for PerformGC, if any (setup below)
2845 // ebx: pointer to builtin function (C callee-saved)
2846 // ebp: frame pointer (restored after C call)
2847 // esp: stack pointer (restored after C call)
2848 // edi: number of arguments including receiver (C callee-saved)
2849 // esi: argv pointer (C callee-saved)
2850
2851 Label throw_normal_exception;
2852 Label throw_termination_exception;
2853 Label throw_out_of_memory_exception;
2854
2855 // Call into the runtime system.
2856 GenerateCore(masm,
2857 &throw_normal_exception,
2858 &throw_termination_exception,
2859 &throw_out_of_memory_exception,
2860 false,
2861 false);
2862
2863 // Do space-specific GC and retry runtime call.
2864 GenerateCore(masm,
2865 &throw_normal_exception,
2866 &throw_termination_exception,
2867 &throw_out_of_memory_exception,
2868 true,
2869 false);
2870
2871 // Do full GC and retry runtime call one final time.
2872 Failure* failure = Failure::InternalError();
2873 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
2874 GenerateCore(masm,
2875 &throw_normal_exception,
2876 &throw_termination_exception,
2877 &throw_out_of_memory_exception,
2878 true,
2879 true);
2880
2881 __ bind(&throw_out_of_memory_exception);
ulan@chromium.org65a89c22012-02-14 11:46:07 +00002882 // Set external caught exception to false.
2883 Isolate* isolate = masm->isolate();
2884 ExternalReference external_caught(Isolate::kExternalCaughtExceptionAddress,
2885 isolate);
2886 __ mov(Operand::StaticVariable(external_caught), Immediate(false));
2887
2888 // Set pending exception and eax to out of memory exception.
2889 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2890 isolate);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00002891 Label already_have_failure;
2892 JumpIfOOM(masm, eax, ecx, &already_have_failure);
2893 __ mov(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException(0x1)));
2894 __ bind(&already_have_failure);
ulan@chromium.org65a89c22012-02-14 11:46:07 +00002895 __ mov(Operand::StaticVariable(pending_exception), eax);
2896 // Fall through to the next label.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002897
2898 __ bind(&throw_termination_exception);
ulan@chromium.org65a89c22012-02-14 11:46:07 +00002899 __ ThrowUncatchable(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002900
2901 __ bind(&throw_normal_exception);
ulan@chromium.org65a89c22012-02-14 11:46:07 +00002902 __ Throw(eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002903}
2904
2905
2906void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002907 Label invoke, handler_entry, exit;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002908 Label not_outermost_js, not_outermost_js_2;
ricow@chromium.org65fae842010-08-25 15:26:24 +00002909
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002910 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2911
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00002912 // Set up frame.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002913 __ push(ebp);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002914 __ mov(ebp, esp);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002915
2916 // Push marker in two places.
2917 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
2918 __ push(Immediate(Smi::FromInt(marker))); // context slot
2919 __ push(Immediate(Smi::FromInt(marker))); // function slot
2920 // Save callee-saved registers (C calling conventions).
2921 __ push(edi);
2922 __ push(esi);
2923 __ push(ebx);
2924
2925 // Save copies of the top frame descriptor on the stack.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002926 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002927 __ push(Operand::StaticVariable(c_entry_fp));
2928
ricow@chromium.org65fae842010-08-25 15:26:24 +00002929 // If this is the outermost JS call, set js_entry_sp value.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002930 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002931 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002932 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002933 __ j(not_equal, &not_outermost_js, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002934 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002935 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
danno@chromium.org2c26cb12012-05-03 09:06:43 +00002936 __ jmp(&invoke, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002937 __ bind(&not_outermost_js);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002938 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002939
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002940 // Jump to a faked try block that does the invoke, with a faked catch
2941 // block that sets the pending exception.
2942 __ jmp(&invoke);
2943 __ bind(&handler_entry);
2944 handler_offset_ = handler_entry.pos();
2945 // Caught exception: Store result (exception) in the pending exception
2946 // field in the JSEnv and return a failure sentinel.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002947 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002948 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002949 __ mov(Operand::StaticVariable(pending_exception), eax);
2950 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
2951 __ jmp(&exit);
2952
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002953 // Invoke: Link this frame into the handler chain. There's only one
2954 // handler block in this code object, so its index is 0.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002955 __ bind(&invoke);
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00002956 __ PushTryHandler(StackHandler::JS_ENTRY, 0);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002957
2958 // Clear any pending exceptions.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002959 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002960 __ mov(Operand::StaticVariable(pending_exception), edx);
2961
2962 // Fake a receiver (NULL).
2963 __ push(Immediate(0)); // receiver
2964
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002965 // Invoke the function by calling through JS entry trampoline builtin and
2966 // pop the faked function when we return. Notice that we cannot store a
2967 // reference to the trampoline code directly in this stub, because the
2968 // builtin stubs may not have been generated yet.
ricow@chromium.org65fae842010-08-25 15:26:24 +00002969 if (is_construct) {
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00002970 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2971 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002972 __ mov(edx, Immediate(construct_entry));
2973 } else {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00002974 ExternalReference entry(Builtins::kJSEntryTrampoline,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002975 masm->isolate());
ricow@chromium.org65fae842010-08-25 15:26:24 +00002976 __ mov(edx, Immediate(entry));
2977 }
2978 __ mov(edx, Operand(edx, 0)); // deref address
2979 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002980 __ call(edx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002981
2982 // Unlink this frame from the handler chain.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002983 __ PopTryHandler();
ricow@chromium.org65fae842010-08-25 15:26:24 +00002984
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002985 __ bind(&exit);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002986 // Check if the current stack frame is marked as the outermost JS frame.
2987 __ pop(ebx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002988 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002989 __ j(not_equal, &not_outermost_js_2);
2990 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2991 __ bind(&not_outermost_js_2);
ricow@chromium.org65fae842010-08-25 15:26:24 +00002992
2993 // Restore the top frame descriptor from the stack.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002994 __ pop(Operand::StaticVariable(ExternalReference(
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00002995 Isolate::kCEntryFPAddress,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002996 masm->isolate())));
ricow@chromium.org65fae842010-08-25 15:26:24 +00002997
2998 // Restore callee-saved registers (C calling conventions).
2999 __ pop(ebx);
3000 __ pop(esi);
3001 __ pop(edi);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003002 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
ricow@chromium.org65fae842010-08-25 15:26:24 +00003003
3004 // Restore frame pointer and return.
3005 __ pop(ebp);
3006 __ ret(0);
3007}
3008
3009
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003010// Generate stub code for instanceof.
3011// This code can patch a call site inlined cache of the instance of check,
3012// which looks like this.
3013//
3014// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
3015// 75 0a jne <some near label>
3016// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
3017//
3018// If call site patching is requested the stack will have the delta from the
3019// return address to the cmp instruction just below the return address. This
3020// also means that call site patching can only take place with arguments in
3021// registers. TOS looks like this when call site patching is requested
3022//
3023// esp[0] : return address
3024// esp[4] : delta from return address to cmp instruction
3025//
ricow@chromium.org65fae842010-08-25 15:26:24 +00003026void InstanceofStub::Generate(MacroAssembler* masm) {
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003027 // Call site inlining and patching implies arguments in registers.
3028 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
3029
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003030 // Fixed register usage throughout the stub.
3031 Register object = eax; // Object (lhs).
3032 Register map = ebx; // Map of the object.
3033 Register function = edx; // Function (rhs).
3034 Register prototype = edi; // Prototype of the function.
3035 Register scratch = ecx;
3036
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003037 // Constants describing the call site code to patch.
3038 static const int kDeltaToCmpImmediate = 2;
3039 static const int kDeltaToMov = 8;
3040 static const int kDeltaToMovImmediate = 9;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003041 static const int8_t kCmpEdiOperandByte1 = BitCast<int8_t, uint8_t>(0x3b);
3042 static const int8_t kCmpEdiOperandByte2 = BitCast<int8_t, uint8_t>(0x3d);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003043 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
3044
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003045 ASSERT_EQ(object.code(), InstanceofStub::left().code());
3046 ASSERT_EQ(function.code(), InstanceofStub::right().code());
3047
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003048 // Get the object and function - they are always both needed.
3049 Label slow, not_js_object;
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003050 if (!HasArgsInRegisters()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003051 __ mov(object, Operand(esp, 2 * kPointerSize));
3052 __ mov(function, Operand(esp, 1 * kPointerSize));
3053 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003054
3055 // Check that the left hand is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003056 __ JumpIfSmi(object, &not_js_object);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003057 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003058
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003059 // If there is a call site cache don't look in the global cache, but do the
3060 // real lookup and update the call site cache.
3061 if (!HasCallSiteInlineCheck()) {
3062 // Look up the function and the map in the instanceof cache.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003063 Label miss;
danno@chromium.org59400602013-08-13 17:09:37 +00003064 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003065 __ j(not_equal, &miss, Label::kNear);
danno@chromium.org59400602013-08-13 17:09:37 +00003066 __ CompareRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003067 __ j(not_equal, &miss, Label::kNear);
danno@chromium.org59400602013-08-13 17:09:37 +00003068 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003069 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
3070 __ bind(&miss);
3071 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003072
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003073 // Get the prototype of the function.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003074 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003075
3076 // Check that the function prototype is a JS object.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003077 __ JumpIfSmi(prototype, &slow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003078 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003079
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003080 // Update the global instanceof or call site inlined cache with the current
3081 // map and function. The cached answer will be set when it is known below.
3082 if (!HasCallSiteInlineCheck()) {
danno@chromium.org59400602013-08-13 17:09:37 +00003083 __ StoreRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
3084 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003085 } else {
3086 // The constants for the code patching are based on no push instructions
3087 // at the call site.
3088 ASSERT(HasArgsInRegisters());
3089 // Get return address and delta to inlined map check.
3090 __ mov(scratch, Operand(esp, 0 * kPointerSize));
3091 __ sub(scratch, Operand(esp, 1 * kPointerSize));
3092 if (FLAG_debug_code) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003093 __ cmpb(Operand(scratch, 0), kCmpEdiOperandByte1);
danno@chromium.org59400602013-08-13 17:09:37 +00003094 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp1);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003095 __ cmpb(Operand(scratch, 1), kCmpEdiOperandByte2);
danno@chromium.org59400602013-08-13 17:09:37 +00003096 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp2);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003097 }
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003098 __ mov(scratch, Operand(scratch, kDeltaToCmpImmediate));
3099 __ mov(Operand(scratch, 0), map);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003100 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003101
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003102 // Loop through the prototype chain of the object looking for the function
3103 // prototype.
3104 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003105 Label loop, is_instance, is_not_instance;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003106 __ bind(&loop);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003107 __ cmp(scratch, prototype);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003108 __ j(equal, &is_instance, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003109 Factory* factory = masm->isolate()->factory();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003110 __ cmp(scratch, Immediate(factory->null_value()));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003111 __ j(equal, &is_not_instance, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003112 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3113 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003114 __ jmp(&loop);
3115
3116 __ bind(&is_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003117 if (!HasCallSiteInlineCheck()) {
danno@chromium.org59400602013-08-13 17:09:37 +00003118 __ mov(eax, Immediate(0));
3119 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003120 } else {
3121 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003122 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003123 __ mov(scratch, Operand(esp, 0 * kPointerSize));
3124 __ sub(scratch, Operand(esp, 1 * kPointerSize));
3125 if (FLAG_debug_code) {
3126 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
danno@chromium.org59400602013-08-13 17:09:37 +00003127 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003128 }
3129 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
3130 if (!ReturnTrueFalseObject()) {
3131 __ Set(eax, Immediate(0));
3132 }
3133 }
3134 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003135
3136 __ bind(&is_not_instance);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003137 if (!HasCallSiteInlineCheck()) {
danno@chromium.org59400602013-08-13 17:09:37 +00003138 __ mov(eax, Immediate(Smi::FromInt(1)));
3139 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003140 } else {
3141 // Get return address and delta to inlined map check.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003142 __ mov(eax, factory->false_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003143 __ mov(scratch, Operand(esp, 0 * kPointerSize));
3144 __ sub(scratch, Operand(esp, 1 * kPointerSize));
3145 if (FLAG_debug_code) {
3146 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
danno@chromium.org59400602013-08-13 17:09:37 +00003147 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003148 }
3149 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
3150 if (!ReturnTrueFalseObject()) {
3151 __ Set(eax, Immediate(Smi::FromInt(1)));
3152 }
3153 }
3154 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003155
3156 Label object_not_null, object_not_null_or_smi;
3157 __ bind(&not_js_object);
3158 // Before null, smi and string value checks, check that the rhs is a function
3159 // as for a non-function rhs an exception needs to be thrown.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003160 __ JumpIfSmi(function, &slow, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003161 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003162 __ j(not_equal, &slow, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003163
3164 // Null is not instance of anything.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003165 __ cmp(object, factory->null_value());
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003166 __ j(not_equal, &object_not_null, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003167 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003168 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003169
3170 __ bind(&object_not_null);
3171 // Smi values is not instance of anything.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003172 __ JumpIfNotSmi(object, &object_not_null_or_smi, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003173 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003174 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003175
3176 __ bind(&object_not_null_or_smi);
3177 // String values is not instance of anything.
3178 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003179 __ j(NegateCondition(is_string), &slow, Label::kNear);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003180 __ Set(eax, Immediate(Smi::FromInt(1)));
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003181 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003182
3183 // Slow-case: Go through the JavaScript implementation.
3184 __ bind(&slow);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003185 if (!ReturnTrueFalseObject()) {
3186 // Tail call the builtin which returns 0 or 1.
3187 if (HasArgsInRegisters()) {
3188 // Push arguments below return address.
3189 __ pop(scratch);
3190 __ push(object);
3191 __ push(function);
3192 __ push(scratch);
3193 }
3194 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
3195 } else {
3196 // Call the builtin and convert 0/1 to true/false.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003197 {
3198 FrameScope scope(masm, StackFrame::INTERNAL);
3199 __ push(object);
3200 __ push(function);
3201 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
3202 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003203 Label true_value, done;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003204 __ test(eax, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003205 __ j(zero, &true_value, Label::kNear);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003206 __ mov(eax, factory->false_value());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003207 __ jmp(&done, Label::kNear);
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003208 __ bind(&true_value);
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003209 __ mov(eax, factory->true_value());
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003210 __ bind(&done);
3211 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00003212 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003213}
3214
3215
kmillikin@chromium.orgd2c22f02011-01-10 08:15:37 +00003216Register InstanceofStub::left() { return eax; }
3217
3218
3219Register InstanceofStub::right() { return edx; }
3220
3221
ricow@chromium.org65fae842010-08-25 15:26:24 +00003222// -------------------------------------------------------------------------
3223// StringCharCodeAtGenerator
3224
3225void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003226 // If the receiver is a smi trigger the non-string case.
3227 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003228 __ JumpIfSmi(object_, receiver_not_string_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003229
3230 // Fetch the instance type of the receiver into result register.
3231 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3232 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3233 // If the receiver is not a string trigger the non-string case.
3234 __ test(result_, Immediate(kIsNotStringMask));
3235 __ j(not_zero, receiver_not_string_);
3236
3237 // If the index is non-smi trigger the non-smi case.
3238 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003239 __ JumpIfNotSmi(index_, &index_not_smi_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003240 __ bind(&got_smi_index_);
3241
3242 // Check for index out of range.
danno@chromium.orgc612e022011-11-10 11:38:15 +00003243 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003244 __ j(above_equal, index_out_of_range_);
3245
danno@chromium.orgc612e022011-11-10 11:38:15 +00003246 __ SmiUntag(index_);
erikcorry0ad885c2011-11-21 13:51:57 +00003247
3248 Factory* factory = masm->isolate()->factory();
3249 StringCharLoadGenerator::Generate(
3250 masm, factory, object_, index_, result_, &call_runtime_);
3251
ricow@chromium.org65fae842010-08-25 15:26:24 +00003252 __ SmiTag(result_);
3253 __ bind(&exit_);
3254}
3255
3256
3257void StringCharCodeAtGenerator::GenerateSlow(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003258 MacroAssembler* masm,
3259 const RuntimeCallHelper& call_helper) {
danno@chromium.org59400602013-08-13 17:09:37 +00003260 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003261
3262 // Index is not a smi.
3263 __ bind(&index_not_smi_);
3264 // If index is a heap number, try converting it to an integer.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003265 __ CheckMap(index_,
3266 masm->isolate()->factory()->heap_number_map(),
3267 index_not_number_,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003268 DONT_DO_SMI_CHECK);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003269 call_helper.BeforeCall(masm);
3270 __ push(object_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003271 __ push(index_); // Consumed by runtime conversion function.
3272 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3273 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3274 } else {
3275 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3276 // NumberToSmi discards numbers that are not exact integers.
3277 __ CallRuntime(Runtime::kNumberToSmi, 1);
3278 }
danno@chromium.orgc612e022011-11-10 11:38:15 +00003279 if (!index_.is(eax)) {
ricow@chromium.org65fae842010-08-25 15:26:24 +00003280 // Save the conversion result before the pop instructions below
3281 // have a chance to overwrite it.
danno@chromium.orgc612e022011-11-10 11:38:15 +00003282 __ mov(index_, eax);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003283 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003284 __ pop(object_);
3285 // Reload the instance type.
3286 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3287 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3288 call_helper.AfterCall(masm);
3289 // If index is still not a smi, it must be out of range.
3290 STATIC_ASSERT(kSmiTag == 0);
danno@chromium.orgc612e022011-11-10 11:38:15 +00003291 __ JumpIfNotSmi(index_, index_out_of_range_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003292 // Otherwise, return to the fast path.
3293 __ jmp(&got_smi_index_);
3294
3295 // Call runtime. We get here when the receiver is a string and the
3296 // index is a number, but the code of getting the actual character
3297 // is too complex (e.g., when the string needs to be flattened).
3298 __ bind(&call_runtime_);
3299 call_helper.BeforeCall(masm);
3300 __ push(object_);
erikcorry0ad885c2011-11-21 13:51:57 +00003301 __ SmiTag(index_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003302 __ push(index_);
3303 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3304 if (!result_.is(eax)) {
3305 __ mov(result_, eax);
3306 }
3307 call_helper.AfterCall(masm);
3308 __ jmp(&exit_);
3309
danno@chromium.org59400602013-08-13 17:09:37 +00003310 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003311}
3312
3313
3314// -------------------------------------------------------------------------
3315// StringCharFromCodeGenerator
3316
3317void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3318 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3319 STATIC_ASSERT(kSmiTag == 0);
3320 STATIC_ASSERT(kSmiShiftSize == 0);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003321 ASSERT(IsPowerOf2(String::kMaxOneByteCharCode + 1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003322 __ test(code_,
3323 Immediate(kSmiTagMask |
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003324 ((~String::kMaxOneByteCharCode) << kSmiTagSize)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003325 __ j(not_zero, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003326
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003327 Factory* factory = masm->isolate()->factory();
3328 __ Set(result_, Immediate(factory->single_character_string_cache()));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003329 STATIC_ASSERT(kSmiTag == 0);
3330 STATIC_ASSERT(kSmiTagSize == 1);
3331 STATIC_ASSERT(kSmiShiftSize == 0);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003332 // At this point code register contains smi tagged ASCII char code.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003333 __ mov(result_, FieldOperand(result_,
3334 code_, times_half_pointer_size,
3335 FixedArray::kHeaderSize));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003336 __ cmp(result_, factory->undefined_value());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003337 __ j(equal, &slow_case_);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003338 __ bind(&exit_);
3339}
3340
3341
3342void StringCharFromCodeGenerator::GenerateSlow(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003343 MacroAssembler* masm,
3344 const RuntimeCallHelper& call_helper) {
danno@chromium.org59400602013-08-13 17:09:37 +00003345 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003346
3347 __ bind(&slow_case_);
3348 call_helper.BeforeCall(masm);
3349 __ push(code_);
3350 __ CallRuntime(Runtime::kCharFromCode, 1);
3351 if (!result_.is(eax)) {
3352 __ mov(result_, eax);
3353 }
3354 call_helper.AfterCall(masm);
3355 __ jmp(&exit_);
3356
danno@chromium.org59400602013-08-13 17:09:37 +00003357 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003358}
3359
3360
ricow@chromium.org65fae842010-08-25 15:26:24 +00003361void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
3362 Register dest,
3363 Register src,
3364 Register count,
3365 Register scratch,
3366 bool ascii) {
3367 // Copy characters using rep movs of doublewords.
3368 // The destination is aligned on a 4 byte boundary because we are
3369 // copying to the beginning of a newly allocated string.
3370 ASSERT(dest.is(edi)); // rep movs destination
3371 ASSERT(src.is(esi)); // rep movs source
3372 ASSERT(count.is(ecx)); // rep movs count
3373 ASSERT(!scratch.is(dest));
3374 ASSERT(!scratch.is(src));
3375 ASSERT(!scratch.is(count));
3376
3377 // Nothing to do for zero characters.
3378 Label done;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003379 __ test(count, count);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003380 __ j(zero, &done);
3381
3382 // Make count the number of bytes to copy.
3383 if (!ascii) {
3384 __ shl(count, 1);
3385 }
3386
3387 // Don't enter the rep movs if there are less than 4 bytes to copy.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003388 Label last_bytes;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003389 __ test(count, Immediate(~3));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003390 __ j(zero, &last_bytes, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003391
3392 // Copy from edi to esi using rep movs instruction.
3393 __ mov(scratch, count);
3394 __ sar(count, 2); // Number of doublewords to copy.
3395 __ cld();
3396 __ rep_movs();
3397
3398 // Find number of bytes left.
3399 __ mov(count, scratch);
3400 __ and_(count, 3);
3401
3402 // Check if there are more bytes to copy.
3403 __ bind(&last_bytes);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003404 __ test(count, count);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003405 __ j(zero, &done);
3406
3407 // Copy remaining characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003408 Label loop;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003409 __ bind(&loop);
3410 __ mov_b(scratch, Operand(src, 0));
3411 __ mov_b(Operand(dest, 0), scratch);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003412 __ add(src, Immediate(1));
3413 __ add(dest, Immediate(1));
3414 __ sub(count, Immediate(1));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003415 __ j(not_zero, &loop);
3416
3417 __ bind(&done);
3418}
3419
3420
ricow@chromium.org65fae842010-08-25 15:26:24 +00003421void StringHelper::GenerateHashInit(MacroAssembler* masm,
3422 Register hash,
3423 Register character,
3424 Register scratch) {
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003425 // hash = (seed + character) + ((seed + character) << 10);
3426 if (Serializer::enabled()) {
danno@chromium.org59400602013-08-13 17:09:37 +00003427 __ LoadRoot(scratch, Heap::kHashSeedRootIndex);
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003428 __ SmiUntag(scratch);
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003429 __ add(scratch, character);
3430 __ mov(hash, scratch);
3431 __ shl(scratch, 10);
3432 __ add(hash, scratch);
3433 } else {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003434 int32_t seed = masm->isolate()->heap()->HashSeed();
rossberg@chromium.orgfab14982012-01-05 15:02:15 +00003435 __ lea(scratch, Operand(character, seed));
3436 __ shl(scratch, 10);
3437 __ lea(hash, Operand(scratch, character, times_1, seed));
3438 }
ricow@chromium.org65fae842010-08-25 15:26:24 +00003439 // hash ^= hash >> 6;
3440 __ mov(scratch, hash);
danno@chromium.org2c456792011-11-11 12:00:53 +00003441 __ shr(scratch, 6);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003442 __ xor_(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003443}
3444
3445
3446void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
3447 Register hash,
3448 Register character,
3449 Register scratch) {
3450 // hash += character;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003451 __ add(hash, character);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003452 // hash += hash << 10;
3453 __ mov(scratch, hash);
3454 __ shl(scratch, 10);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003455 __ add(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003456 // hash ^= hash >> 6;
3457 __ mov(scratch, hash);
danno@chromium.org2c456792011-11-11 12:00:53 +00003458 __ shr(scratch, 6);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003459 __ xor_(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003460}
3461
3462
3463void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
3464 Register hash,
3465 Register scratch) {
3466 // hash += hash << 3;
3467 __ mov(scratch, hash);
3468 __ shl(scratch, 3);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003469 __ add(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003470 // hash ^= hash >> 11;
3471 __ mov(scratch, hash);
danno@chromium.org2c456792011-11-11 12:00:53 +00003472 __ shr(scratch, 11);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003473 __ xor_(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003474 // hash += hash << 15;
3475 __ mov(scratch, hash);
3476 __ shl(scratch, 15);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003477 __ add(hash, scratch);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003478
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003479 __ and_(hash, String::kHashBitMask);
danno@chromium.org2c456792011-11-11 12:00:53 +00003480
ricow@chromium.org65fae842010-08-25 15:26:24 +00003481 // if (hash == 0) hash = 27;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003482 Label hash_not_zero;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003483 __ j(not_zero, &hash_not_zero, Label::kNear);
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00003484 __ mov(hash, Immediate(StringHasher::kZeroHash));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003485 __ bind(&hash_not_zero);
3486}
3487
3488
3489void SubStringStub::Generate(MacroAssembler* masm) {
3490 Label runtime;
3491
3492 // Stack frame on entry.
3493 // esp[0]: return address
3494 // esp[4]: to
3495 // esp[8]: from
3496 // esp[12]: string
3497
3498 // Make sure first argument is a string.
3499 __ mov(eax, Operand(esp, 3 * kPointerSize));
3500 STATIC_ASSERT(kSmiTag == 0);
whesse@chromium.org7b260152011-06-20 15:33:18 +00003501 __ JumpIfSmi(eax, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003502 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3503 __ j(NegateCondition(is_string), &runtime);
3504
3505 // eax: string
3506 // ebx: instance type
3507
3508 // Calculate length of sub string using the smi values.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003509 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003510 __ JumpIfNotSmi(ecx, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003511 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
whesse@chromium.org7b260152011-06-20 15:33:18 +00003512 __ JumpIfNotSmi(edx, &runtime);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003513 __ sub(ecx, edx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003514 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003515 Label not_original_string;
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00003516 // Shorter than original string's length: an actual substring.
3517 __ j(below, &not_original_string, Label::kNear);
3518 // Longer than original string's length or negative: unsafe arguments.
3519 __ j(above, &runtime);
3520 // Return original string.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003521 Counters* counters = masm->isolate()->counters();
3522 __ IncrementCounter(counters->sub_string_native(), 1);
3523 __ ret(3 * kPointerSize);
3524 __ bind(&not_original_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003525
ulan@chromium.org2e04b582013-02-21 14:06:02 +00003526 Label single_char;
3527 __ cmp(ecx, Immediate(Smi::FromInt(1)));
3528 __ j(equal, &single_char);
3529
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003530 // eax: string
3531 // ebx: instance type
3532 // ecx: sub string length (smi)
3533 // edx: from index (smi)
3534 // Deal with different string types: update the index if necessary
3535 // and put the underlying string into edi.
3536 Label underlying_unpacked, sliced_string, seq_or_external_string;
3537 // If the string is not indirect, it can only be sequential or external.
3538 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3539 STATIC_ASSERT(kIsIndirectStringMask != 0);
3540 __ test(ebx, Immediate(kIsIndirectStringMask));
3541 __ j(zero, &seq_or_external_string, Label::kNear);
3542
3543 Factory* factory = masm->isolate()->factory();
3544 __ test(ebx, Immediate(kSlicedNotConsMask));
3545 __ j(not_zero, &sliced_string, Label::kNear);
3546 // Cons string. Check whether it is flat, then fetch first part.
3547 // Flat cons strings have an empty second part.
3548 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
3549 factory->empty_string());
3550 __ j(not_equal, &runtime);
3551 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
3552 // Update instance type.
3553 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3554 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3555 __ jmp(&underlying_unpacked, Label::kNear);
3556
3557 __ bind(&sliced_string);
3558 // Sliced string. Fetch parent and adjust start index by offset.
3559 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
3560 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
3561 // Update instance type.
3562 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3563 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3564 __ jmp(&underlying_unpacked, Label::kNear);
3565
3566 __ bind(&seq_or_external_string);
3567 // Sequential or external string. Just move string to the expected register.
3568 __ mov(edi, eax);
3569
3570 __ bind(&underlying_unpacked);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003571
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003572 if (FLAG_string_slices) {
3573 Label copy_routine;
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003574 // edi: underlying subject string
ricow@chromium.org7ad65222011-12-19 12:13:11 +00003575 // ebx: instance type of underlying subject string
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003576 // edx: adjusted start index (smi)
3577 // ecx: length (smi)
3578 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
3579 // Short slice. Copy instead of slicing.
3580 __ j(less, &copy_routine);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003581 // Allocate new sliced string. At this point we do not reload the instance
3582 // type including the string encoding because we simply rely on the info
3583 // provided by the original string. It does not matter if the original
3584 // string's encoding is wrong because we always have to recheck encoding of
3585 // the newly created string's parent anyways due to externalized strings.
3586 Label two_byte_slice, set_slice_header;
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003587 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003588 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3589 __ test(ebx, Immediate(kStringEncodingMask));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003590 __ j(zero, &two_byte_slice, Label::kNear);
3591 __ AllocateAsciiSlicedString(eax, ebx, no_reg, &runtime);
3592 __ jmp(&set_slice_header, Label::kNear);
3593 __ bind(&two_byte_slice);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003594 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003595 __ bind(&set_slice_header);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003596 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003597 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
3598 Immediate(String::kEmptyHashField));
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003599 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
3600 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003601 __ IncrementCounter(counters->sub_string_native(), 1);
3602 __ ret(3 * kPointerSize);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003603
3604 __ bind(&copy_routine);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00003605 }
3606
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003607 // edi: underlying subject string
ricow@chromium.org7ad65222011-12-19 12:13:11 +00003608 // ebx: instance type of underlying subject string
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003609 // edx: adjusted start index (smi)
3610 // ecx: length (smi)
3611 // The subject string can only be external or sequential string of either
3612 // encoding at this point.
3613 Label two_byte_sequential, runtime_drop_two, sequential_string;
3614 STATIC_ASSERT(kExternalStringTag != 0);
3615 STATIC_ASSERT(kSeqStringTag == 0);
3616 __ test_b(ebx, kExternalStringTag);
3617 __ j(zero, &sequential_string);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003618
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003619 // Handle external string.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003620 // Rule out short external strings.
3621 STATIC_CHECK(kShortExternalStringTag != 0);
3622 __ test_b(ebx, kShortExternalStringMask);
3623 __ j(not_zero, &runtime);
3624 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
3625 // Move the pointer so that offset-wise, it looks like a sequential string.
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003626 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003627 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3628
3629 __ bind(&sequential_string);
3630 // Stash away (adjusted) index and (underlying) string.
3631 __ push(edx);
3632 __ push(edi);
3633 __ SmiUntag(ecx);
mvstanton@chromium.orge4ac3ef2012-11-12 14:53:34 +00003634 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003635 __ test_b(ebx, kStringEncodingMask);
3636 __ j(zero, &two_byte_sequential);
3637
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003638 // Sequential ASCII string. Allocate the result.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003639 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003640
3641 // eax: result string
3642 // ecx: result string length
3643 __ mov(edx, esi); // esi used by following code.
3644 // Locate first character of result.
3645 __ mov(edi, eax);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003646 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003647 // Load string argument and locate character of sub string start.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003648 __ pop(esi);
3649 __ pop(ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003650 __ SmiUntag(ebx);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003651 __ lea(esi, FieldOperand(esi, ebx, times_1, SeqOneByteString::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003652
3653 // eax: result string
3654 // ecx: result length
3655 // edx: original value of esi
3656 // edi: first character of result
3657 // esi: character of sub string start
3658 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
3659 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003660 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003661 __ ret(3 * kPointerSize);
3662
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003663 __ bind(&two_byte_sequential);
3664 // Sequential two-byte string. Allocate the result.
3665 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003666
3667 // eax: result string
3668 // ecx: result string length
3669 __ mov(edx, esi); // esi used by following code.
3670 // Locate first character of result.
3671 __ mov(edi, eax);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003672 __ add(edi,
ricow@chromium.org65fae842010-08-25 15:26:24 +00003673 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3674 // Load string argument and locate character of sub string start.
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003675 __ pop(esi);
3676 __ pop(ebx);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003677 // As from is a smi it is 2 times the value which matches the size of a two
3678 // byte character.
3679 STATIC_ASSERT(kSmiTag == 0);
3680 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003681 __ lea(esi, FieldOperand(esi, ebx, times_1, SeqTwoByteString::kHeaderSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003682
3683 // eax: result string
3684 // ecx: result length
3685 // edx: original value of esi
3686 // edi: first character of result
3687 // esi: character of sub string start
3688 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
3689 __ mov(esi, edx); // Restore esi.
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003690 __ IncrementCounter(counters->sub_string_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003691 __ ret(3 * kPointerSize);
3692
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00003693 // Drop pushed values on the stack before tail call.
3694 __ bind(&runtime_drop_two);
3695 __ Drop(2);
3696
ricow@chromium.org65fae842010-08-25 15:26:24 +00003697 // Just jump to runtime to create the sub string.
3698 __ bind(&runtime);
3699 __ TailCallRuntime(Runtime::kSubString, 3, 1);
ulan@chromium.org2e04b582013-02-21 14:06:02 +00003700
3701 __ bind(&single_char);
3702 // eax: string
3703 // ebx: instance type
3704 // ecx: sub string length (smi)
3705 // edx: from index (smi)
3706 StringCharAtGenerator generator(
3707 eax, edx, ecx, eax, &runtime, &runtime, &runtime, STRING_INDEX_IS_NUMBER);
3708 generator.GenerateFast(masm);
3709 __ ret(3 * kPointerSize);
3710 generator.SkipSlow(masm, &runtime);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003711}
3712
3713
lrn@chromium.org1c092762011-05-09 09:42:16 +00003714void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
3715 Register left,
3716 Register right,
3717 Register scratch1,
3718 Register scratch2) {
3719 Register length = scratch1;
3720
3721 // Compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003722 Label strings_not_equal, check_zero_length;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003723 __ mov(length, FieldOperand(left, String::kLengthOffset));
3724 __ cmp(length, FieldOperand(right, String::kLengthOffset));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003725 __ j(equal, &check_zero_length, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00003726 __ bind(&strings_not_equal);
3727 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
3728 __ ret(0);
3729
3730 // Check if the length is zero.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003731 Label compare_chars;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003732 __ bind(&check_zero_length);
3733 STATIC_ASSERT(kSmiTag == 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003734 __ test(length, length);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003735 __ j(not_zero, &compare_chars, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00003736 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3737 __ ret(0);
3738
3739 // Compare characters.
3740 __ bind(&compare_chars);
3741 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003742 &strings_not_equal, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00003743
3744 // Characters are equal.
3745 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3746 __ ret(0);
3747}
3748
3749
ricow@chromium.org65fae842010-08-25 15:26:24 +00003750void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
3751 Register left,
3752 Register right,
3753 Register scratch1,
3754 Register scratch2,
3755 Register scratch3) {
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003756 Counters* counters = masm->isolate()->counters();
3757 __ IncrementCounter(counters->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003758
3759 // Find minimum length.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003760 Label left_shorter;
ricow@chromium.org65fae842010-08-25 15:26:24 +00003761 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
3762 __ mov(scratch3, scratch1);
3763 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
3764
3765 Register length_delta = scratch3;
3766
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003767 __ j(less_equal, &left_shorter, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003768 // Right string is shorter. Change scratch1 to be length of right string.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003769 __ sub(scratch1, length_delta);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003770 __ bind(&left_shorter);
3771
3772 Register min_length = scratch1;
3773
3774 // If either length is zero, just compare lengths.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003775 Label compare_lengths;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003776 __ test(min_length, min_length);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003777 __ j(zero, &compare_lengths, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003778
lrn@chromium.org1c092762011-05-09 09:42:16 +00003779 // Compare characters.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003780 Label result_not_equal;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003781 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003782 &result_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003783
3784 // Compare lengths - strings up to min-length are equal.
3785 __ bind(&compare_lengths);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003786 __ test(length_delta, length_delta);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003787 Label length_not_equal;
3788 __ j(not_zero, &length_not_equal, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003789
3790 // Result is EQUAL.
3791 STATIC_ASSERT(EQUAL == 0);
3792 STATIC_ASSERT(kSmiTag == 0);
3793 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
3794 __ ret(0);
3795
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003796 Label result_greater;
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003797 Label result_less;
3798 __ bind(&length_not_equal);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003799 __ j(greater, &result_greater, Label::kNear);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003800 __ jmp(&result_less, Label::kNear);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003801 __ bind(&result_not_equal);
jkummerow@chromium.org59297c72013-01-09 16:32:23 +00003802 __ j(above, &result_greater, Label::kNear);
3803 __ bind(&result_less);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003804
3805 // Result is LESS.
3806 __ Set(eax, Immediate(Smi::FromInt(LESS)));
3807 __ ret(0);
3808
3809 // Result is GREATER.
3810 __ bind(&result_greater);
3811 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
3812 __ ret(0);
3813}
3814
3815
lrn@chromium.org1c092762011-05-09 09:42:16 +00003816void StringCompareStub::GenerateAsciiCharsCompareLoop(
3817 MacroAssembler* masm,
3818 Register left,
3819 Register right,
3820 Register length,
3821 Register scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003822 Label* chars_not_equal,
3823 Label::Distance chars_not_equal_near) {
lrn@chromium.org1c092762011-05-09 09:42:16 +00003824 // Change index to run from -length to -1 by adding length to string
3825 // start. This means that loop ends when index reaches zero, which
3826 // doesn't need an additional compare.
3827 __ SmiUntag(length);
3828 __ lea(left,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003829 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
lrn@chromium.org1c092762011-05-09 09:42:16 +00003830 __ lea(right,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003831 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
lrn@chromium.org1c092762011-05-09 09:42:16 +00003832 __ neg(length);
3833 Register index = length; // index = -length;
3834
3835 // Compare loop.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003836 Label loop;
lrn@chromium.org1c092762011-05-09 09:42:16 +00003837 __ bind(&loop);
3838 __ mov_b(scratch, Operand(left, index, times_1, 0));
3839 __ cmpb(scratch, Operand(right, index, times_1, 0));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003840 __ j(not_equal, chars_not_equal, chars_not_equal_near);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00003841 __ inc(index);
lrn@chromium.org1c092762011-05-09 09:42:16 +00003842 __ j(not_zero, &loop);
3843}
3844
3845
ricow@chromium.org65fae842010-08-25 15:26:24 +00003846void StringCompareStub::Generate(MacroAssembler* masm) {
3847 Label runtime;
3848
3849 // Stack frame on entry.
3850 // esp[0]: return address
3851 // esp[4]: right string
3852 // esp[8]: left string
3853
3854 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
3855 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
3856
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003857 Label not_same;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003858 __ cmp(edx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003859 __ j(not_equal, &not_same, Label::kNear);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003860 STATIC_ASSERT(EQUAL == 0);
3861 STATIC_ASSERT(kSmiTag == 0);
3862 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +00003863 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
ricow@chromium.org65fae842010-08-25 15:26:24 +00003864 __ ret(2 * kPointerSize);
3865
3866 __ bind(&not_same);
3867
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003868 // Check that both objects are sequential ASCII strings.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003869 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
3870
ulan@chromium.org2efb9002012-01-19 15:36:35 +00003871 // Compare flat ASCII strings.
ricow@chromium.org65fae842010-08-25 15:26:24 +00003872 // Drop arguments from the stack.
3873 __ pop(ecx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003874 __ add(esp, Immediate(2 * kPointerSize));
ricow@chromium.org65fae842010-08-25 15:26:24 +00003875 __ push(ecx);
3876 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
3877
3878 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3879 // tagged as a small integer.
3880 __ bind(&runtime);
3881 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3882}
3883
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003884
machenbach@chromium.orgc8cbc432014-01-21 09:01:57 +00003885void ArrayPushStub::Generate(MacroAssembler* masm) {
3886 int argc = arguments_count();
3887
3888 if (argc == 0) {
3889 // Noop, return the length.
3890 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
3891 __ ret((argc + 1) * kPointerSize);
3892 return;
3893 }
3894
3895 Isolate* isolate = masm->isolate();
3896
3897 if (argc != 1) {
3898 __ TailCallExternalReference(
3899 ExternalReference(Builtins::c_ArrayPush, isolate), argc + 1, 1);
3900 return;
3901 }
3902
3903 Label call_builtin, attempt_to_grow_elements, with_write_barrier;
3904
3905 // Get the elements array of the object.
3906 __ mov(edi, FieldOperand(edx, JSArray::kElementsOffset));
3907
3908 if (IsFastSmiOrObjectElementsKind(elements_kind())) {
3909 // Check that the elements are in fast mode and writable.
3910 __ cmp(FieldOperand(edi, HeapObject::kMapOffset),
3911 isolate->factory()->fixed_array_map());
3912 __ j(not_equal, &call_builtin);
3913 }
3914
3915 // Get the array's length into eax and calculate new length.
3916 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
3917 STATIC_ASSERT(kSmiTagSize == 1);
3918 STATIC_ASSERT(kSmiTag == 0);
3919 __ add(eax, Immediate(Smi::FromInt(argc)));
3920
3921 // Get the elements' length into ecx.
3922 __ mov(ecx, FieldOperand(edi, FixedArray::kLengthOffset));
3923
3924 // Check if we could survive without allocation.
3925 __ cmp(eax, ecx);
3926
3927 if (IsFastSmiOrObjectElementsKind(elements_kind())) {
3928 __ j(greater, &attempt_to_grow_elements);
3929
3930 // Check if value is a smi.
3931 __ mov(ecx, Operand(esp, argc * kPointerSize));
3932 __ JumpIfNotSmi(ecx, &with_write_barrier);
3933
3934 // Store the value.
3935 __ mov(FieldOperand(edi, eax, times_half_pointer_size,
3936 FixedArray::kHeaderSize - argc * kPointerSize),
3937 ecx);
3938 } else {
3939 __ j(greater, &call_builtin);
3940
3941 __ mov(ecx, Operand(esp, argc * kPointerSize));
3942 __ StoreNumberToDoubleElements(
3943 ecx, edi, eax, ecx, xmm0, &call_builtin, true, argc * kDoubleSize);
3944 }
3945
3946 // Save new length.
3947 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
3948 __ ret((argc + 1) * kPointerSize);
3949
3950 if (IsFastDoubleElementsKind(elements_kind())) {
3951 __ bind(&call_builtin);
3952 __ TailCallExternalReference(
3953 ExternalReference(Builtins::c_ArrayPush, isolate), argc + 1, 1);
3954 return;
3955 }
3956
3957 __ bind(&with_write_barrier);
3958
3959 if (IsFastSmiElementsKind(elements_kind())) {
3960 if (FLAG_trace_elements_transitions) __ jmp(&call_builtin);
3961
3962 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
3963 isolate->factory()->heap_number_map());
3964 __ j(equal, &call_builtin);
3965
3966 ElementsKind target_kind = IsHoleyElementsKind(elements_kind())
3967 ? FAST_HOLEY_ELEMENTS : FAST_ELEMENTS;
3968 __ mov(ebx, ContextOperand(esi, Context::GLOBAL_OBJECT_INDEX));
3969 __ mov(ebx, FieldOperand(ebx, GlobalObject::kNativeContextOffset));
3970 __ mov(ebx, ContextOperand(ebx, Context::JS_ARRAY_MAPS_INDEX));
3971 const int header_size = FixedArrayBase::kHeaderSize;
3972 // Verify that the object can be transitioned in place.
3973 const int origin_offset = header_size + elements_kind() * kPointerSize;
3974 __ mov(edi, FieldOperand(ebx, origin_offset));
3975 __ cmp(edi, FieldOperand(edx, HeapObject::kMapOffset));
3976 __ j(not_equal, &call_builtin);
3977
3978 const int target_offset = header_size + target_kind * kPointerSize;
3979 __ mov(ebx, FieldOperand(ebx, target_offset));
3980 ElementsTransitionGenerator::GenerateMapChangeElementsTransition(
3981 masm, DONT_TRACK_ALLOCATION_SITE, NULL);
3982 // Restore edi used as a scratch register for the write barrier used while
3983 // setting the map.
3984 __ mov(edi, FieldOperand(edx, JSArray::kElementsOffset));
3985 }
3986
3987 // Save new length.
3988 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
3989
3990 // Store the value.
3991 __ lea(edx, FieldOperand(edi, eax, times_half_pointer_size,
3992 FixedArray::kHeaderSize - argc * kPointerSize));
3993 __ mov(Operand(edx, 0), ecx);
3994
3995 __ RecordWrite(edi, edx, ecx, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
3996 OMIT_SMI_CHECK);
3997
3998 __ ret((argc + 1) * kPointerSize);
3999
4000 __ bind(&attempt_to_grow_elements);
4001 if (!FLAG_inline_new) {
4002 __ bind(&call_builtin);
4003 __ TailCallExternalReference(
4004 ExternalReference(Builtins::c_ArrayPush, isolate), argc + 1, 1);
4005 return;
4006 }
4007
4008 __ mov(ebx, Operand(esp, argc * kPointerSize));
4009 // Growing elements that are SMI-only requires special handling in case the
4010 // new element is non-Smi. For now, delegate to the builtin.
4011 if (IsFastSmiElementsKind(elements_kind())) {
4012 __ JumpIfNotSmi(ebx, &call_builtin);
4013 }
4014
4015 // We could be lucky and the elements array could be at the top of new-space.
4016 // In this case we can just grow it in place by moving the allocation pointer
4017 // up.
4018 ExternalReference new_space_allocation_top =
4019 ExternalReference::new_space_allocation_top_address(isolate);
4020 ExternalReference new_space_allocation_limit =
4021 ExternalReference::new_space_allocation_limit_address(isolate);
4022
4023 const int kAllocationDelta = 4;
4024 ASSERT(kAllocationDelta >= argc);
4025 // Load top.
4026 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
4027
4028 // Check if it's the end of elements.
4029 __ lea(edx, FieldOperand(edi, eax, times_half_pointer_size,
4030 FixedArray::kHeaderSize - argc * kPointerSize));
4031 __ cmp(edx, ecx);
4032 __ j(not_equal, &call_builtin);
4033 __ add(ecx, Immediate(kAllocationDelta * kPointerSize));
4034 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
4035 __ j(above, &call_builtin);
4036
4037 // We fit and could grow elements.
4038 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
4039
4040 // Push the argument...
4041 __ mov(Operand(edx, 0), ebx);
4042 // ... and fill the rest with holes.
4043 for (int i = 1; i < kAllocationDelta; i++) {
4044 __ mov(Operand(edx, i * kPointerSize),
4045 isolate->factory()->the_hole_value());
4046 }
4047
4048 if (IsFastObjectElementsKind(elements_kind())) {
4049 // We know the elements array is in new space so we don't need the
4050 // remembered set, but we just pushed a value onto it so we may have to tell
4051 // the incremental marker to rescan the object that we just grew. We don't
4052 // need to worry about the holes because they are in old space and already
4053 // marked black.
4054 __ RecordWrite(edi, edx, ebx, kDontSaveFPRegs, OMIT_REMEMBERED_SET);
4055 }
4056
4057 // Restore receiver to edx as finish sequence assumes it's here.
4058 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
4059
4060 // Increment element's and array's sizes.
4061 __ add(FieldOperand(edi, FixedArray::kLengthOffset),
4062 Immediate(Smi::FromInt(kAllocationDelta)));
4063
4064 // NOTE: This only happen in new-space, where we don't care about the
4065 // black-byte-count on pages. Otherwise we should update that too if the
4066 // object is black.
4067
4068 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
4069 __ ret((argc + 1) * kPointerSize);
4070
4071 __ bind(&call_builtin);
4072 __ TailCallExternalReference(
4073 ExternalReference(Builtins::c_ArrayPush, isolate), argc + 1, 1);
4074}
4075
4076
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004077void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
4078 // ----------- S t a t e -------------
4079 // -- edx : left
4080 // -- eax : right
4081 // -- esp[0] : return address
4082 // -----------------------------------
4083 Isolate* isolate = masm->isolate();
4084
4085 // Load ecx with the allocation site. We stick an undefined dummy value here
4086 // and replace it with the real allocation site later when we instantiate this
4087 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
4088 __ mov(ecx, handle(isolate->heap()->undefined_value()));
4089
4090 // Make sure that we actually patched the allocation site.
4091 if (FLAG_debug_code) {
4092 __ test(ecx, Immediate(kSmiTagMask));
4093 __ Assert(not_equal, kExpectedAllocationSite);
4094 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
4095 isolate->factory()->allocation_site_map());
4096 __ Assert(equal, kExpectedAllocationSite);
4097 }
4098
4099 // Tail call into the stub that handles binary operations with allocation
4100 // sites.
4101 BinaryOpWithAllocationSiteStub stub(state_);
4102 __ TailCallStub(&stub);
4103}
4104
4105
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004106void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004107 ASSERT(state_ == CompareIC::SMI);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004108 Label miss;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004109 __ mov(ecx, edx);
4110 __ or_(ecx, eax);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004111 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004112
4113 if (GetCondition() == equal) {
4114 // For equality we do not care about the sign of the result.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004115 __ sub(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004116 } else {
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004117 Label done;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004118 __ sub(edx, eax);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004119 __ j(no_overflow, &done, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004120 // Correct sign of result in case of overflow.
4121 __ not_(edx);
4122 __ bind(&done);
4123 __ mov(eax, edx);
4124 }
4125 __ ret(0);
4126
4127 __ bind(&miss);
4128 GenerateMiss(masm);
4129}
4130
4131
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004132void ICCompareStub::GenerateNumbers(MacroAssembler* masm) {
4133 ASSERT(state_ == CompareIC::NUMBER);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004134
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004135 Label generic_stub;
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00004136 Label unordered, maybe_undefined1, maybe_undefined2;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004137 Label miss;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004138
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004139 if (left_ == CompareIC::SMI) {
4140 __ JumpIfNotSmi(edx, &miss);
4141 }
4142 if (right_ == CompareIC::SMI) {
4143 __ JumpIfNotSmi(eax, &miss);
4144 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004145
4146 // Inlining the double comparison and falling back to the general compare
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004147 // stub if NaN is involved or SSE2 or CMOV is unsupported.
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00004148 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00004149 CpuFeatureScope scope1(masm, SSE2);
4150 CpuFeatureScope scope2(masm, CMOV);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004151
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004152 // Load left and right operand.
4153 Label done, left, left_smi, right_smi;
4154 __ JumpIfSmi(eax, &right_smi, Label::kNear);
4155 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
4156 masm->isolate()->factory()->heap_number_map());
4157 __ j(not_equal, &maybe_undefined1, Label::kNear);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +00004158 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004159 __ jmp(&left, Label::kNear);
4160 __ bind(&right_smi);
4161 __ mov(ecx, eax); // Can't clobber eax because we can still jump away.
4162 __ SmiUntag(ecx);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004163 __ Cvtsi2sd(xmm1, ecx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004164
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004165 __ bind(&left);
4166 __ JumpIfSmi(edx, &left_smi, Label::kNear);
4167 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
4168 masm->isolate()->factory()->heap_number_map());
4169 __ j(not_equal, &maybe_undefined2, Label::kNear);
bmeurer@chromium.org0fdb2a62013-10-21 07:19:36 +00004170 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004171 __ jmp(&done);
4172 __ bind(&left_smi);
4173 __ mov(ecx, edx); // Can't clobber edx because we can still jump away.
4174 __ SmiUntag(ecx);
machenbach@chromium.org528ce022013-09-23 14:09:36 +00004175 __ Cvtsi2sd(xmm0, ecx);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004176
4177 __ bind(&done);
4178 // Compare operands.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004179 __ ucomisd(xmm0, xmm1);
4180
4181 // Don't base result on EFLAGS when a NaN is involved.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004182 __ j(parity_even, &unordered, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004183
4184 // Return a result of -1, 0, or 1, based on EFLAGS.
4185 // Performing mov, because xor would destroy the flag register.
4186 __ mov(eax, 0); // equal
4187 __ mov(ecx, Immediate(Smi::FromInt(1)));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004188 __ cmov(above, eax, ecx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004189 __ mov(ecx, Immediate(Smi::FromInt(-1)));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004190 __ cmov(below, eax, ecx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004191 __ ret(0);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004192 } else {
4193 __ mov(ecx, edx);
4194 __ and_(ecx, eax);
4195 __ JumpIfSmi(ecx, &generic_stub, Label::kNear);
4196
4197 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
4198 masm->isolate()->factory()->heap_number_map());
4199 __ j(not_equal, &maybe_undefined1, Label::kNear);
4200 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
4201 masm->isolate()->factory()->heap_number_map());
4202 __ j(not_equal, &maybe_undefined2, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004203 }
4204
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00004205 __ bind(&unordered);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004206 __ bind(&generic_stub);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004207 ICCompareStub stub(op_, CompareIC::GENERIC, CompareIC::GENERIC,
4208 CompareIC::GENERIC);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004209 __ jmp(stub.GetCode(masm->isolate()), RelocInfo::CODE_TARGET);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004210
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00004211 __ bind(&maybe_undefined1);
4212 if (Token::IsOrderedRelationalCompareOp(op_)) {
4213 __ cmp(eax, Immediate(masm->isolate()->factory()->undefined_value()));
4214 __ j(not_equal, &miss);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004215 __ JumpIfSmi(edx, &unordered);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00004216 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
4217 __ j(not_equal, &maybe_undefined2, Label::kNear);
4218 __ jmp(&unordered);
4219 }
4220
4221 __ bind(&maybe_undefined2);
4222 if (Token::IsOrderedRelationalCompareOp(op_)) {
4223 __ cmp(edx, Immediate(masm->isolate()->factory()->undefined_value()));
4224 __ j(equal, &unordered);
4225 }
4226
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004227 __ bind(&miss);
4228 GenerateMiss(masm);
4229}
4230
4231
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004232void ICCompareStub::GenerateInternalizedStrings(MacroAssembler* masm) {
4233 ASSERT(state_ == CompareIC::INTERNALIZED_STRING);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004234 ASSERT(GetCondition() == equal);
4235
4236 // Registers containing left and right operands respectively.
4237 Register left = edx;
4238 Register right = eax;
4239 Register tmp1 = ecx;
4240 Register tmp2 = ebx;
4241
4242 // Check that both operands are heap objects.
4243 Label miss;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004244 __ mov(tmp1, left);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004245 STATIC_ASSERT(kSmiTag == 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004246 __ and_(tmp1, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004247 __ JumpIfSmi(tmp1, &miss, Label::kNear);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004248
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004249 // Check that both operands are internalized strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004250 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
4251 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
4252 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
4253 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00004254 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
4255 __ or_(tmp1, tmp2);
4256 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
4257 __ j(not_zero, &miss, Label::kNear);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004258
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004259 // Internalized strings are compared by identity.
4260 Label done;
4261 __ cmp(left, right);
4262 // Make sure eax is non-zero. At this point input operands are
4263 // guaranteed to be non-zero.
4264 ASSERT(right.is(eax));
4265 __ j(not_equal, &done, Label::kNear);
4266 STATIC_ASSERT(EQUAL == 0);
4267 STATIC_ASSERT(kSmiTag == 0);
4268 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4269 __ bind(&done);
4270 __ ret(0);
4271
4272 __ bind(&miss);
4273 GenerateMiss(masm);
4274}
4275
4276
4277void ICCompareStub::GenerateUniqueNames(MacroAssembler* masm) {
4278 ASSERT(state_ == CompareIC::UNIQUE_NAME);
4279 ASSERT(GetCondition() == equal);
4280
4281 // Registers containing left and right operands respectively.
4282 Register left = edx;
4283 Register right = eax;
4284 Register tmp1 = ecx;
4285 Register tmp2 = ebx;
4286
4287 // Check that both operands are heap objects.
4288 Label miss;
4289 __ mov(tmp1, left);
4290 STATIC_ASSERT(kSmiTag == 0);
4291 __ and_(tmp1, right);
4292 __ JumpIfSmi(tmp1, &miss, Label::kNear);
4293
4294 // Check that both operands are unique names. This leaves the instance
4295 // types loaded in tmp1 and tmp2.
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004296 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
4297 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
4298 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
4299 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
4300
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004301 __ JumpIfNotUniqueName(tmp1, &miss, Label::kNear);
4302 __ JumpIfNotUniqueName(tmp2, &miss, Label::kNear);
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004303
4304 // Unique names are compared by identity.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004305 Label done;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004306 __ cmp(left, right);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004307 // Make sure eax is non-zero. At this point input operands are
4308 // guaranteed to be non-zero.
4309 ASSERT(right.is(eax));
4310 __ j(not_equal, &done, Label::kNear);
4311 STATIC_ASSERT(EQUAL == 0);
4312 STATIC_ASSERT(kSmiTag == 0);
4313 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4314 __ bind(&done);
4315 __ ret(0);
4316
4317 __ bind(&miss);
4318 GenerateMiss(masm);
4319}
4320
4321
lrn@chromium.org1c092762011-05-09 09:42:16 +00004322void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004323 ASSERT(state_ == CompareIC::STRING);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004324 Label miss;
4325
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004326 bool equality = Token::IsEqualityOp(op_);
4327
lrn@chromium.org1c092762011-05-09 09:42:16 +00004328 // Registers containing left and right operands respectively.
4329 Register left = edx;
4330 Register right = eax;
4331 Register tmp1 = ecx;
4332 Register tmp2 = ebx;
4333 Register tmp3 = edi;
4334
4335 // Check that both operands are heap objects.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004336 __ mov(tmp1, left);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004337 STATIC_ASSERT(kSmiTag == 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004338 __ and_(tmp1, right);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004339 __ JumpIfSmi(tmp1, &miss);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004340
4341 // Check that both operands are strings. This leaves the instance
4342 // types loaded in tmp1 and tmp2.
4343 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
4344 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
4345 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
4346 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
4347 __ mov(tmp3, tmp1);
4348 STATIC_ASSERT(kNotStringTag != 0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004349 __ or_(tmp3, tmp2);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004350 __ test(tmp3, Immediate(kIsNotStringMask));
4351 __ j(not_zero, &miss);
4352
4353 // Fast check for identical strings.
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004354 Label not_same;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004355 __ cmp(left, right);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004356 __ j(not_equal, &not_same, Label::kNear);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004357 STATIC_ASSERT(EQUAL == 0);
4358 STATIC_ASSERT(kSmiTag == 0);
4359 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4360 __ ret(0);
4361
4362 // Handle not identical strings.
4363 __ bind(&not_same);
4364
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004365 // Check that both strings are internalized. If they are, we're done
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004366 // because we already know they are not identical. But in the case of
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004367 // non-equality compare, we still need to determine the order. We
4368 // also know they are both strings.
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004369 if (equality) {
4370 Label do_compare;
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00004371 STATIC_ASSERT(kInternalizedTag == 0);
4372 __ or_(tmp1, tmp2);
4373 __ test(tmp1, Immediate(kIsNotInternalizedMask));
4374 __ j(not_zero, &do_compare, Label::kNear);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004375 // Make sure eax is non-zero. At this point input operands are
4376 // guaranteed to be non-zero.
4377 ASSERT(right.is(eax));
4378 __ ret(0);
4379 __ bind(&do_compare);
4380 }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004381
4382 // Check that both strings are sequential ASCII.
4383 Label runtime;
lrn@chromium.org1c092762011-05-09 09:42:16 +00004384 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
4385
4386 // Compare flat ASCII strings. Returns when done.
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004387 if (equality) {
4388 StringCompareStub::GenerateFlatAsciiStringEquals(
4389 masm, left, right, tmp1, tmp2);
4390 } else {
4391 StringCompareStub::GenerateCompareFlatAsciiStrings(
4392 masm, left, right, tmp1, tmp2, tmp3);
4393 }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004394
4395 // Handle more complex cases in runtime.
4396 __ bind(&runtime);
4397 __ pop(tmp1); // Return address.
4398 __ push(left);
4399 __ push(right);
4400 __ push(tmp1);
svenpanne@chromium.org4efbdb12012-03-12 08:18:42 +00004401 if (equality) {
4402 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
4403 } else {
4404 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4405 }
lrn@chromium.org1c092762011-05-09 09:42:16 +00004406
4407 __ bind(&miss);
4408 GenerateMiss(masm);
4409}
4410
4411
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004412void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004413 ASSERT(state_ == CompareIC::OBJECT);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00004414 Label miss;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004415 __ mov(ecx, edx);
4416 __ and_(ecx, eax);
whesse@chromium.org7b260152011-06-20 15:33:18 +00004417 __ JumpIfSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004418
4419 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004420 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004421 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004422 __ j(not_equal, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004423
4424 ASSERT(GetCondition() == equal);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004425 __ sub(eax, edx);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004426 __ ret(0);
4427
4428 __ bind(&miss);
4429 GenerateMiss(masm);
4430}
4431
4432
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004433void ICCompareStub::GenerateKnownObjects(MacroAssembler* masm) {
4434 Label miss;
4435 __ mov(ecx, edx);
4436 __ and_(ecx, eax);
4437 __ JumpIfSmi(ecx, &miss, Label::kNear);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004438
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004439 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
4440 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
4441 __ cmp(ecx, known_map_);
4442 __ j(not_equal, &miss, Label::kNear);
4443 __ cmp(ebx, known_map_);
4444 __ j(not_equal, &miss, Label::kNear);
4445
4446 __ sub(eax, edx);
4447 __ ret(0);
4448
4449 __ bind(&miss);
4450 GenerateMiss(masm);
4451}
4452
4453
4454void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004455 {
4456 // Call the runtime system in a fresh internal frame.
4457 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
4458 masm->isolate());
4459 FrameScope scope(masm, StackFrame::INTERNAL);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004460 __ push(edx); // Preserve edx and eax.
4461 __ push(eax);
4462 __ push(edx); // And also use them as the arguments.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004463 __ push(eax);
4464 __ push(Immediate(Smi::FromInt(op_)));
4465 __ CallExternalReference(miss, 3);
ricow@chromium.org64e3a4b2011-12-13 08:07:27 +00004466 // Compute the entry point of the rewritten stub.
4467 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
4468 __ pop(eax);
4469 __ pop(edx);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004470 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004471
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004472 // Do a tail call to the rewritten stub.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004473 __ jmp(edi);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004474}
4475
4476
lrn@chromium.org1c092762011-05-09 09:42:16 +00004477// Helper function used to check that the dictionary doesn't contain
4478// the property. This function may return false negatives, so miss_label
4479// must always call a backup property check that is complete.
4480// This function is safe to call if the receiver has fast properties.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004481// Name must be a unique name and receiver must be a heap object.
4482void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
4483 Label* miss,
4484 Label* done,
4485 Register properties,
4486 Handle<Name> name,
4487 Register r0) {
4488 ASSERT(name->IsUniqueName());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004489
4490 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4491 // not equal to the name and kProbes-th slot is not used (its name is the
4492 // undefined value), it guarantees the hash table doesn't contain the
4493 // property. It's true even if some slots represent deleted properties
ulan@chromium.org967e2702012-02-28 09:49:15 +00004494 // (their names are the hole value).
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004495 for (int i = 0; i < kInlinedProbes; i++) {
4496 // Compute the masked index: (hash + i + i * i) & mask.
4497 Register index = r0;
4498 // Capacity is smi 2^n.
4499 __ mov(index, FieldOperand(properties, kCapacityOffset));
4500 __ dec(index);
4501 __ and_(index,
4502 Immediate(Smi::FromInt(name->Hash() +
ulan@chromium.org750145a2013-03-07 15:14:13 +00004503 NameDictionary::GetProbeOffset(i))));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004504
4505 // Scale the index by multiplying by the entry size.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004506 ASSERT(NameDictionary::kEntrySize == 3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004507 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
4508 Register entity_name = r0;
4509 // Having undefined at this place means the name is not contained.
4510 ASSERT_EQ(kSmiTagSize, 1);
4511 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
4512 kElementsStartOffset - kHeapObjectTag));
4513 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
4514 __ j(equal, done);
4515
4516 // Stop if found the property.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004517 __ cmp(entity_name, Handle<Name>(name));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004518 __ j(equal, miss);
4519
ulan@chromium.org750145a2013-03-07 15:14:13 +00004520 Label good;
ulan@chromium.org967e2702012-02-28 09:49:15 +00004521 // Check for the hole and skip.
4522 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
ulan@chromium.org750145a2013-03-07 15:14:13 +00004523 __ j(equal, &good, Label::kNear);
ulan@chromium.org967e2702012-02-28 09:49:15 +00004524
ulan@chromium.org750145a2013-03-07 15:14:13 +00004525 // Check if the entry name is not a unique name.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004526 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004527 __ JumpIfNotUniqueName(FieldOperand(entity_name, Map::kInstanceTypeOffset),
4528 miss);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004529 __ bind(&good);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004530 }
4531
ulan@chromium.org750145a2013-03-07 15:14:13 +00004532 NameDictionaryLookupStub stub(properties, r0, r0, NEGATIVE_LOOKUP);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004533 __ push(Immediate(Handle<Object>(name)));
4534 __ push(Immediate(name->Hash()));
4535 __ CallStub(&stub);
4536 __ test(r0, r0);
4537 __ j(not_zero, miss);
4538 __ jmp(done);
4539}
4540
4541
ulan@chromium.org750145a2013-03-07 15:14:13 +00004542// Probe the name dictionary in the |elements| register. Jump to the
lrn@chromium.org1c092762011-05-09 09:42:16 +00004543// |done| label if a property with the given name is found leaving the
4544// index into the dictionary in |r0|. Jump to the |miss| label
4545// otherwise.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004546void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
4547 Label* miss,
4548 Label* done,
4549 Register elements,
4550 Register name,
4551 Register r0,
4552 Register r1) {
erik.corry@gmail.com6e28b562011-10-27 14:20:17 +00004553 ASSERT(!elements.is(r0));
4554 ASSERT(!elements.is(r1));
4555 ASSERT(!name.is(r0));
4556 ASSERT(!name.is(r1));
4557
ulan@chromium.org750145a2013-03-07 15:14:13 +00004558 __ AssertName(name);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004559
4560 __ mov(r1, FieldOperand(elements, kCapacityOffset));
4561 __ shr(r1, kSmiTagSize); // convert smi to int
4562 __ dec(r1);
4563
4564 // Generate an unrolled loop that performs a few probes before
4565 // giving up. Measurements done on Gmail indicate that 2 probes
4566 // cover ~93% of loads from dictionaries.
4567 for (int i = 0; i < kInlinedProbes; i++) {
4568 // Compute the masked index: (hash + i + i * i) & mask.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004569 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4570 __ shr(r0, Name::kHashShift);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004571 if (i > 0) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00004572 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
lrn@chromium.org1c092762011-05-09 09:42:16 +00004573 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004574 __ and_(r0, r1);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004575
4576 // Scale the index by multiplying by the entry size.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004577 ASSERT(NameDictionary::kEntrySize == 3);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004578 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
4579
4580 // Check if the key is identical to the name.
4581 __ cmp(name, Operand(elements,
4582 r0,
4583 times_4,
4584 kElementsStartOffset - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004585 __ j(equal, done);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004586 }
4587
ulan@chromium.org750145a2013-03-07 15:14:13 +00004588 NameDictionaryLookupStub stub(elements, r1, r0, POSITIVE_LOOKUP);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004589 __ push(name);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004590 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4591 __ shr(r0, Name::kHashShift);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004592 __ push(r0);
4593 __ CallStub(&stub);
4594
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004595 __ test(r1, r1);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004596 __ j(zero, miss);
4597 __ jmp(done);
4598}
4599
4600
ulan@chromium.org750145a2013-03-07 15:14:13 +00004601void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004602 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4603 // we cannot call anything that could cause a GC from this stub.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004604 // Stack frame on entry:
4605 // esp[0 * kPointerSize]: return address.
4606 // esp[1 * kPointerSize]: key's hash.
4607 // esp[2 * kPointerSize]: key.
4608 // Registers:
ulan@chromium.org750145a2013-03-07 15:14:13 +00004609 // dictionary_: NameDictionary to probe.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004610 // result_: used as scratch.
4611 // index_: will hold an index of entry if lookup is successful.
4612 // might alias with result_.
4613 // Returns:
4614 // result_ is zero if lookup failed, non zero otherwise.
4615
4616 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4617
4618 Register scratch = result_;
4619
4620 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
4621 __ dec(scratch);
4622 __ SmiUntag(scratch);
4623 __ push(scratch);
4624
4625 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4626 // not equal to the name and kProbes-th slot is not used (its name is the
4627 // undefined value), it guarantees the hash table doesn't contain the
4628 // property. It's true even if some slots represent deleted properties
4629 // (their names are the null value).
4630 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4631 // Compute the masked index: (hash + i + i * i) & mask.
4632 __ mov(scratch, Operand(esp, 2 * kPointerSize));
4633 if (i > 0) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00004634 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
lrn@chromium.org1c092762011-05-09 09:42:16 +00004635 }
4636 __ and_(scratch, Operand(esp, 0));
4637
4638 // Scale the index by multiplying by the entry size.
ulan@chromium.org750145a2013-03-07 15:14:13 +00004639 ASSERT(NameDictionary::kEntrySize == 3);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004640 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
4641
4642 // Having undefined at this place means the name is not contained.
4643 ASSERT_EQ(kSmiTagSize, 1);
4644 __ mov(scratch, Operand(dictionary_,
4645 index_,
4646 times_pointer_size,
4647 kElementsStartOffset - kHeapObjectTag));
4648 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
4649 __ j(equal, &not_in_dictionary);
4650
4651 // Stop if found the property.
4652 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
4653 __ j(equal, &in_dictionary);
4654
4655 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00004656 // If we hit a key that is not a unique name during negative
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00004657 // lookup we have to bailout as this key might be equal to the
lrn@chromium.org1c092762011-05-09 09:42:16 +00004658 // key we are looking for.
4659
ulan@chromium.org750145a2013-03-07 15:14:13 +00004660 // Check if the entry name is not a unique name.
lrn@chromium.org1c092762011-05-09 09:42:16 +00004661 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004662 __ JumpIfNotUniqueName(FieldOperand(scratch, Map::kInstanceTypeOffset),
4663 &maybe_in_dictionary);
lrn@chromium.org1c092762011-05-09 09:42:16 +00004664 }
4665 }
4666
4667 __ bind(&maybe_in_dictionary);
4668 // If we are doing negative lookup then probing failure should be
4669 // treated as a lookup success. For positive lookup probing failure
4670 // should be treated as lookup failure.
4671 if (mode_ == POSITIVE_LOOKUP) {
4672 __ mov(result_, Immediate(0));
4673 __ Drop(1);
4674 __ ret(2 * kPointerSize);
4675 }
4676
4677 __ bind(&in_dictionary);
4678 __ mov(result_, Immediate(1));
4679 __ Drop(1);
4680 __ ret(2 * kPointerSize);
4681
4682 __ bind(&not_in_dictionary);
4683 __ mov(result_, Immediate(0));
4684 __ Drop(1);
4685 __ ret(2 * kPointerSize);
4686}
4687
4688
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004689void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4690 Isolate* isolate) {
ulan@chromium.org750145a2013-03-07 15:14:13 +00004691 StoreBufferOverflowStub stub(kDontSaveFPRegs);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00004692 stub.GetCode(isolate);
ulan@chromium.org750145a2013-03-07 15:14:13 +00004693 if (CpuFeatures::IsSafeForSnapshot(SSE2)) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004694 StoreBufferOverflowStub stub2(kSaveFPRegs);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00004695 stub2.GetCode(isolate);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004696 }
4697}
4698
4699
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00004700bool CodeStub::CanUseFPRegisters() {
4701 return CpuFeatures::IsSupported(SSE2);
4702}
4703
4704
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004705// Takes the input in 3 registers: address_ value_ and object_. A pointer to
4706// the value has just been written into the object, now this stub makes sure
4707// we keep the GC informed. The word in the object where the value has been
4708// written is in the address register.
4709void RecordWriteStub::Generate(MacroAssembler* masm) {
4710 Label skip_to_incremental_noncompacting;
4711 Label skip_to_incremental_compacting;
4712
4713 // The first two instructions are generated with labels so as to get the
4714 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4715 // forth between a compare instructions (a nop in this position) and the
4716 // real branch when we start and stop incremental heap marking.
4717 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4718 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4719
4720 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
4721 __ RememberedSetHelper(object_,
4722 address_,
4723 value_,
4724 save_fp_regs_mode_,
4725 MacroAssembler::kReturnAtEnd);
4726 } else {
4727 __ ret(0);
4728 }
4729
4730 __ bind(&skip_to_incremental_noncompacting);
4731 GenerateIncremental(masm, INCREMENTAL);
4732
4733 __ bind(&skip_to_incremental_compacting);
4734 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4735
4736 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4737 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4738 masm->set_byte_at(0, kTwoByteNopInstruction);
4739 masm->set_byte_at(2, kFiveByteNopInstruction);
4740}
4741
4742
4743void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4744 regs_.Save(masm);
4745
4746 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
4747 Label dont_need_remembered_set;
4748
4749 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4750 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4751 regs_.scratch0(),
4752 &dont_need_remembered_set);
4753
4754 __ CheckPageFlag(regs_.object(),
4755 regs_.scratch0(),
4756 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4757 not_zero,
4758 &dont_need_remembered_set);
4759
4760 // First notify the incremental marker if necessary, then update the
4761 // remembered set.
4762 CheckNeedsToInformIncrementalMarker(
4763 masm,
4764 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
4765 mode);
4766 InformIncrementalMarker(masm, mode);
4767 regs_.Restore(masm);
4768 __ RememberedSetHelper(object_,
4769 address_,
4770 value_,
4771 save_fp_regs_mode_,
4772 MacroAssembler::kReturnAtEnd);
4773
4774 __ bind(&dont_need_remembered_set);
4775 }
4776
4777 CheckNeedsToInformIncrementalMarker(
4778 masm,
4779 kReturnOnNoNeedToInformIncrementalMarker,
4780 mode);
4781 InformIncrementalMarker(masm, mode);
4782 regs_.Restore(masm);
4783 __ ret(0);
4784}
4785
4786
4787void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
4788 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
4789 int argument_count = 3;
4790 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4791 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00004792 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004793 __ mov(Operand(esp, 2 * kPointerSize),
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00004794 Immediate(ExternalReference::isolate_address(masm->isolate())));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004795
4796 AllowExternalCallThatCantCauseGC scope(masm);
4797 if (mode == INCREMENTAL_COMPACTION) {
4798 __ CallCFunction(
4799 ExternalReference::incremental_evacuation_record_write_function(
4800 masm->isolate()),
4801 argument_count);
4802 } else {
4803 ASSERT(mode == INCREMENTAL);
4804 __ CallCFunction(
4805 ExternalReference::incremental_marking_record_write_function(
4806 masm->isolate()),
4807 argument_count);
4808 }
4809 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
4810}
4811
4812
4813void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4814 MacroAssembler* masm,
4815 OnNoNeedToInformIncrementalMarker on_no_need,
4816 Mode mode) {
4817 Label object_is_black, need_incremental, need_incremental_pop_object;
4818
verwaest@chromium.org33e09c82012-10-10 17:07:22 +00004819 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4820 __ and_(regs_.scratch0(), regs_.object());
4821 __ mov(regs_.scratch1(),
4822 Operand(regs_.scratch0(),
4823 MemoryChunk::kWriteBarrierCounterOffset));
4824 __ sub(regs_.scratch1(), Immediate(1));
4825 __ mov(Operand(regs_.scratch0(),
4826 MemoryChunk::kWriteBarrierCounterOffset),
4827 regs_.scratch1());
4828 __ j(negative, &need_incremental);
4829
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004830 // Let's look at the color of the object: If it is not black we don't have
4831 // to inform the incremental marker.
4832 __ JumpIfBlack(regs_.object(),
4833 regs_.scratch0(),
4834 regs_.scratch1(),
4835 &object_is_black,
4836 Label::kNear);
4837
4838 regs_.Restore(masm);
4839 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4840 __ RememberedSetHelper(object_,
4841 address_,
4842 value_,
4843 save_fp_regs_mode_,
4844 MacroAssembler::kReturnAtEnd);
4845 } else {
4846 __ ret(0);
4847 }
4848
4849 __ bind(&object_is_black);
4850
4851 // Get the value from the slot.
4852 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4853
4854 if (mode == INCREMENTAL_COMPACTION) {
4855 Label ensure_not_white;
4856
4857 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4858 regs_.scratch1(), // Scratch.
4859 MemoryChunk::kEvacuationCandidateMask,
4860 zero,
4861 &ensure_not_white,
4862 Label::kNear);
4863
4864 __ CheckPageFlag(regs_.object(),
4865 regs_.scratch1(), // Scratch.
4866 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4867 not_zero,
4868 &ensure_not_white,
4869 Label::kNear);
4870
4871 __ jmp(&need_incremental);
4872
4873 __ bind(&ensure_not_white);
4874 }
4875
4876 // We need an extra register for this, so we push the object register
4877 // temporarily.
4878 __ push(regs_.object());
4879 __ EnsureNotWhite(regs_.scratch0(), // The value.
4880 regs_.scratch1(), // Scratch.
4881 regs_.object(), // Scratch.
4882 &need_incremental_pop_object,
4883 Label::kNear);
4884 __ pop(regs_.object());
4885
4886 regs_.Restore(masm);
4887 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4888 __ RememberedSetHelper(object_,
4889 address_,
4890 value_,
4891 save_fp_regs_mode_,
4892 MacroAssembler::kReturnAtEnd);
4893 } else {
4894 __ ret(0);
4895 }
4896
4897 __ bind(&need_incremental_pop_object);
4898 __ pop(regs_.object());
4899
4900 __ bind(&need_incremental);
4901
4902 // Fall through when we need to inform the incremental marker.
4903}
4904
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004905
4906void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4907 // ----------- S t a t e -------------
4908 // -- eax : element value to store
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004909 // -- ecx : element index as smi
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004910 // -- esp[0] : return address
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004911 // -- esp[4] : array literal index in function
4912 // -- esp[8] : array literal
4913 // clobbers ebx, edx, edi
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004914 // -----------------------------------
4915
4916 Label element_done;
4917 Label double_elements;
4918 Label smi_element;
4919 Label slow_elements;
4920 Label slow_elements_from_double;
4921 Label fast_elements;
4922
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004923 // Get array literal index, array literal and its map.
4924 __ mov(edx, Operand(esp, 1 * kPointerSize));
4925 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4926 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
4927
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004928 __ CheckFastElements(edi, &double_elements);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004929
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00004930 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004931 __ JumpIfSmi(eax, &smi_element);
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00004932 __ CheckFastSmiElements(edi, &fast_elements, Label::kNear);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004933
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004934 // Store into the array literal requires a elements transition. Call into
4935 // the runtime.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004936
4937 __ bind(&slow_elements);
4938 __ pop(edi); // Pop return address and remember to put back later for tail
4939 // call.
4940 __ push(ebx);
4941 __ push(ecx);
4942 __ push(eax);
4943 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4944 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
4945 __ push(edx);
4946 __ push(edi); // Return return address so that tail call returns to right
4947 // place.
4948 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4949
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004950 __ bind(&slow_elements_from_double);
4951 __ pop(edx);
4952 __ jmp(&slow_elements);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004953
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00004954 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004955 __ bind(&fast_elements);
4956 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4957 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
4958 FixedArrayBase::kHeaderSize));
4959 __ mov(Operand(ecx, 0), eax);
4960 // Update the write barrier for the array store.
4961 __ RecordWrite(ebx, ecx, eax,
4962 kDontSaveFPRegs,
4963 EMIT_REMEMBERED_SET,
4964 OMIT_SMI_CHECK);
4965 __ ret(0);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004966
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00004967 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4968 // and value is Smi.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004969 __ bind(&smi_element);
4970 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4971 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
4972 FixedArrayBase::kHeaderSize), eax);
4973 __ ret(0);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004974
svenpanne@chromium.org830d30c2012-05-29 13:20:14 +00004975 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004976 __ bind(&double_elements);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004977
jkummerow@chromium.org04e4f1e2011-11-14 13:36:17 +00004978 __ push(edx);
4979 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
4980 __ StoreNumberToDoubleElements(eax,
4981 edx,
4982 ecx,
4983 edi,
4984 xmm0,
4985 &slow_elements_from_double,
4986 false);
4987 __ pop(edx);
4988 __ ret(0);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004989}
4990
verwaest@chromium.org753aee42012-07-17 16:15:42 +00004991
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00004992void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00004993 CEntryStub ces(1, fp_registers_ ? kSaveFPRegs : kDontSaveFPRegs);
hpayer@chromium.org8432c912013-02-28 15:55:26 +00004994 __ call(ces.GetCode(masm->isolate()), RelocInfo::CODE_TARGET);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00004995 int parameter_count_offset =
4996 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4997 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00004998 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00004999 __ pop(ecx);
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00005000 int additional_offset = function_mode_ == JS_FUNCTION_STUB_MODE
5001 ? kPointerSize
5002 : 0;
5003 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +00005004 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00005005}
5006
5007
machenbach@chromium.orgea468882013-11-18 08:53:19 +00005008void StubFailureTailCallTrampolineStub::Generate(MacroAssembler* masm) {
5009 CEntryStub ces(1, fp_registers_ ? kSaveFPRegs : kDontSaveFPRegs);
5010 __ call(ces.GetCode(masm->isolate()), RelocInfo::CODE_TARGET);
5011 __ mov(edi, eax);
5012 int parameter_count_offset =
5013 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
5014 __ mov(eax, MemOperand(ebp, parameter_count_offset));
5015 // The parameter count above includes the receiver for the arguments passed to
5016 // the deoptimization handler. Subtract the receiver for the parameter count
5017 // for the call.
5018 __ sub(eax, Immediate(1));
5019 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
5020 ParameterCount argument_count(eax);
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00005021 __ InvokeFunction(edi, argument_count, JUMP_FUNCTION, NullCallWrapper());
machenbach@chromium.orgea468882013-11-18 08:53:19 +00005022}
5023
5024
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005025void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005026 if (masm->isolate()->function_entry_hook() != NULL) {
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005027 ProfileEntryHookStub stub;
5028 masm->CallStub(&stub);
5029 }
5030}
5031
5032
5033void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005034 // Save volatile registers.
5035 const int kNumSavedRegisters = 3;
5036 __ push(eax);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005037 __ push(ecx);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005038 __ push(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005039
5040 // Calculate and push the original stack pointer.
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00005041 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005042 __ push(eax);
5043
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00005044 // Retrieve our return address and use it to calculate the calling
5045 // function's address.
5046 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005047 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
5048 __ push(eax);
5049
5050 // Call the entry hook.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005051 ASSERT(masm->isolate()->function_entry_hook() != NULL);
5052 __ call(FUNCTION_ADDR(masm->isolate()->function_entry_hook()),
5053 RelocInfo::RUNTIME_ENTRY);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005054 __ add(esp, Immediate(2 * kPointerSize));
5055
5056 // Restore ecx.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005057 __ pop(edx);
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005058 __ pop(ecx);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005059 __ pop(eax);
5060
verwaest@chromium.org753aee42012-07-17 16:15:42 +00005061 __ ret(0);
5062}
5063
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005064
5065template<class T>
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005066static void CreateArrayDispatch(MacroAssembler* masm,
5067 AllocationSiteOverrideMode mode) {
5068 if (mode == DISABLE_ALLOCATION_SITES) {
5069 T stub(GetInitialFastElementsKind(),
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005070 mode);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005071 __ TailCallStub(&stub);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005072 } else if (mode == DONT_OVERRIDE) {
5073 int last_index = GetSequenceIndexFromFastElementsKind(
5074 TERMINAL_FAST_ELEMENTS_KIND);
5075 for (int i = 0; i <= last_index; ++i) {
5076 Label next;
5077 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
5078 __ cmp(edx, kind);
5079 __ j(not_equal, &next);
5080 T stub(kind);
5081 __ TailCallStub(&stub);
5082 __ bind(&next);
5083 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005084
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005085 // If we reached this point there is a problem.
5086 __ Abort(kUnexpectedElementsKindInArrayConstructor);
5087 } else {
5088 UNREACHABLE();
5089 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005090}
5091
5092
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005093static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
5094 AllocationSiteOverrideMode mode) {
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005095 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005096 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005097 // eax - number of arguments
5098 // edi - constructor?
5099 // esp[0] - return address
5100 // esp[4] - last argument
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005101 Label normal_sequence;
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005102 if (mode == DONT_OVERRIDE) {
5103 ASSERT(FAST_SMI_ELEMENTS == 0);
5104 ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
5105 ASSERT(FAST_ELEMENTS == 2);
5106 ASSERT(FAST_HOLEY_ELEMENTS == 3);
5107 ASSERT(FAST_DOUBLE_ELEMENTS == 4);
5108 ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
5109
5110 // is the low bit set? If so, we are holey and that is good.
5111 __ test_b(edx, 1);
5112 __ j(not_zero, &normal_sequence);
5113 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005114
5115 // look at the first argument
5116 __ mov(ecx, Operand(esp, kPointerSize));
5117 __ test(ecx, ecx);
5118 __ j(zero, &normal_sequence);
5119
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005120 if (mode == DISABLE_ALLOCATION_SITES) {
5121 ElementsKind initial = GetInitialFastElementsKind();
5122 ElementsKind holey_initial = GetHoleyElementsKind(initial);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005123
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005124 ArraySingleArgumentConstructorStub stub_holey(holey_initial,
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005125 DISABLE_ALLOCATION_SITES);
5126 __ TailCallStub(&stub_holey);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005127
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005128 __ bind(&normal_sequence);
5129 ArraySingleArgumentConstructorStub stub(initial,
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005130 DISABLE_ALLOCATION_SITES);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005131 __ TailCallStub(&stub);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005132 } else if (mode == DONT_OVERRIDE) {
5133 // We are going to create a holey array, but our kind is non-holey.
5134 // Fix kind and retry.
5135 __ inc(edx);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005136
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005137 if (FLAG_debug_code) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00005138 Handle<Map> allocation_site_map =
5139 masm->isolate()->factory()->allocation_site_map();
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005140 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
5141 __ Assert(equal, kExpectedAllocationSite);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005142 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005143
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +00005144 // Save the resulting elements kind in type info. We can't just store r3
5145 // in the AllocationSite::transition_info field because elements kind is
5146 // restricted to a portion of the field...upper bits need to be left alone.
5147 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005148 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +00005149 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005150
5151 __ bind(&normal_sequence);
5152 int last_index = GetSequenceIndexFromFastElementsKind(
5153 TERMINAL_FAST_ELEMENTS_KIND);
5154 for (int i = 0; i <= last_index; ++i) {
5155 Label next;
5156 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
5157 __ cmp(edx, kind);
5158 __ j(not_equal, &next);
5159 ArraySingleArgumentConstructorStub stub(kind);
5160 __ TailCallStub(&stub);
5161 __ bind(&next);
5162 }
5163
5164 // If we reached this point there is a problem.
5165 __ Abort(kUnexpectedElementsKindInArrayConstructor);
5166 } else {
5167 UNREACHABLE();
5168 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005169}
5170
5171
5172template<class T>
5173static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
5174 int to_index = GetSequenceIndexFromFastElementsKind(
5175 TERMINAL_FAST_ELEMENTS_KIND);
5176 for (int i = 0; i <= to_index; ++i) {
5177 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005178 T stub(kind);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00005179 stub.GetCode(isolate);
ulan@chromium.org9cbaabd2014-01-08 10:55:36 +00005180 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
machenbach@chromium.org03453962014-01-10 14:16:31 +00005181 T stub1(kind, DISABLE_ALLOCATION_SITES);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00005182 stub1.GetCode(isolate);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005183 }
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005184 }
5185}
5186
5187
5188void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
5189 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
5190 isolate);
5191 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
5192 isolate);
5193 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
5194 isolate);
5195}
5196
5197
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005198void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
5199 Isolate* isolate) {
5200 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
5201 for (int i = 0; i < 2; i++) {
5202 // For internal arrays we only need a few things
5203 InternalArrayNoArgumentConstructorStub stubh1(kinds[i]);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00005204 stubh1.GetCode(isolate);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005205 InternalArraySingleArgumentConstructorStub stubh2(kinds[i]);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00005206 stubh2.GetCode(isolate);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005207 InternalArrayNArgumentsConstructorStub stubh3(kinds[i]);
machenbach@chromium.org8a58f642013-12-02 10:46:30 +00005208 stubh3.GetCode(isolate);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005209 }
5210}
5211
5212
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005213void ArrayConstructorStub::GenerateDispatchToArrayStub(
5214 MacroAssembler* masm,
5215 AllocationSiteOverrideMode mode) {
5216 if (argument_count_ == ANY) {
5217 Label not_zero_case, not_one_case;
5218 __ test(eax, eax);
5219 __ j(not_zero, &not_zero_case);
5220 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5221
5222 __ bind(&not_zero_case);
5223 __ cmp(eax, 1);
5224 __ j(greater, &not_one_case);
5225 CreateArrayDispatchOneArgument(masm, mode);
5226
5227 __ bind(&not_one_case);
5228 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5229 } else if (argument_count_ == NONE) {
5230 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5231 } else if (argument_count_ == ONE) {
5232 CreateArrayDispatchOneArgument(masm, mode);
5233 } else if (argument_count_ == MORE_THAN_ONE) {
5234 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5235 } else {
5236 UNREACHABLE();
5237 }
5238}
5239
5240
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005241void ArrayConstructorStub::Generate(MacroAssembler* masm) {
5242 // ----------- S t a t e -------------
5243 // -- eax : argc (only if argument_count_ == ANY)
5244 // -- ebx : type info cell
5245 // -- edi : constructor
5246 // -- esp[0] : return address
5247 // -- esp[4] : last argument
5248 // -----------------------------------
5249 Handle<Object> undefined_sentinel(
5250 masm->isolate()->heap()->undefined_value(),
5251 masm->isolate());
5252
5253 if (FLAG_debug_code) {
5254 // The array construct code is only set for the global and natives
5255 // builtin Array functions which always have maps.
5256
5257 // Initial map for the builtin Array function should be a map.
5258 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5259 // Will both indicate a NULL and a Smi.
5260 __ test(ecx, Immediate(kSmiTagMask));
danno@chromium.org59400602013-08-13 17:09:37 +00005261 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005262 __ CmpObjectType(ecx, MAP_TYPE, ecx);
danno@chromium.org59400602013-08-13 17:09:37 +00005263 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005264
danno@chromium.org41728482013-06-12 22:31:22 +00005265 // We should either have undefined in ebx or a valid cell
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005266 Label okay_here;
danno@chromium.org41728482013-06-12 22:31:22 +00005267 Handle<Map> cell_map = masm->isolate()->factory()->cell_map();
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005268 __ cmp(ebx, Immediate(undefined_sentinel));
5269 __ j(equal, &okay_here);
danno@chromium.org41728482013-06-12 22:31:22 +00005270 __ cmp(FieldOperand(ebx, 0), Immediate(cell_map));
danno@chromium.org59400602013-08-13 17:09:37 +00005271 __ Assert(equal, kExpectedPropertyCellInRegisterEbx);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005272 __ bind(&okay_here);
5273 }
5274
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005275 Label no_info;
5276 // If the type cell is undefined, or contains anything other than an
5277 // AllocationSite, call an array constructor that doesn't use AllocationSites.
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005278 __ cmp(ebx, Immediate(undefined_sentinel));
5279 __ j(equal, &no_info);
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005280 __ mov(ebx, FieldOperand(ebx, Cell::kValueOffset));
5281 __ cmp(FieldOperand(ebx, 0), Immediate(
machenbach@chromium.org528ce022013-09-23 14:09:36 +00005282 masm->isolate()->factory()->allocation_site_map()));
danno@chromium.orgbee51992013-07-10 14:57:15 +00005283 __ j(not_equal, &no_info);
5284
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +00005285 // Only look at the lower 16 bits of the transition info.
machenbach@chromium.org6d26cbb2014-01-22 10:50:56 +00005286 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005287 __ SmiUntag(edx);
machenbach@chromium.orgb5be0a92013-11-15 10:32:41 +00005288 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
5289 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005290 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
5291
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005292 __ bind(&no_info);
dslomov@chromium.org4a35c5a2013-09-13 07:28:52 +00005293 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
danno@chromium.orgca29dd82013-04-26 11:59:48 +00005294}
5295
5296
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005297void InternalArrayConstructorStub::GenerateCase(
5298 MacroAssembler* masm, ElementsKind kind) {
5299 Label not_zero_case, not_one_case;
5300 Label normal_sequence;
5301
5302 __ test(eax, eax);
5303 __ j(not_zero, &not_zero_case);
5304 InternalArrayNoArgumentConstructorStub stub0(kind);
5305 __ TailCallStub(&stub0);
5306
5307 __ bind(&not_zero_case);
5308 __ cmp(eax, 1);
5309 __ j(greater, &not_one_case);
5310
5311 if (IsFastPackedElementsKind(kind)) {
5312 // We might need to create a holey array
5313 // look at the first argument
5314 __ mov(ecx, Operand(esp, kPointerSize));
5315 __ test(ecx, ecx);
5316 __ j(zero, &normal_sequence);
5317
5318 InternalArraySingleArgumentConstructorStub
5319 stub1_holey(GetHoleyElementsKind(kind));
5320 __ TailCallStub(&stub1_holey);
5321 }
5322
5323 __ bind(&normal_sequence);
5324 InternalArraySingleArgumentConstructorStub stub1(kind);
5325 __ TailCallStub(&stub1);
5326
5327 __ bind(&not_one_case);
5328 InternalArrayNArgumentsConstructorStub stubN(kind);
5329 __ TailCallStub(&stubN);
5330}
5331
5332
5333void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
5334 // ----------- S t a t e -------------
5335 // -- eax : argc
5336 // -- ebx : type info cell
5337 // -- edi : constructor
5338 // -- esp[0] : return address
5339 // -- esp[4] : last argument
5340 // -----------------------------------
5341
5342 if (FLAG_debug_code) {
5343 // The array construct code is only set for the global and natives
5344 // builtin Array functions which always have maps.
5345
5346 // Initial map for the builtin Array function should be a map.
5347 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5348 // Will both indicate a NULL and a Smi.
5349 __ test(ecx, Immediate(kSmiTagMask));
danno@chromium.org59400602013-08-13 17:09:37 +00005350 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005351 __ CmpObjectType(ecx, MAP_TYPE, ecx);
danno@chromium.org59400602013-08-13 17:09:37 +00005352 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005353 }
5354
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005355 // Figure out the right elements kind
5356 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005357
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005358 // Load the map's "bit field 2" into |result|. We only need the first byte,
5359 // but the following masking takes care of that anyway.
5360 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
5361 // Retrieve elements_kind from bit field 2.
5362 __ and_(ecx, Map::kElementsKindMask);
5363 __ shr(ecx, Map::kElementsKindShift);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005364
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005365 if (FLAG_debug_code) {
5366 Label done;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005367 __ cmp(ecx, Immediate(FAST_ELEMENTS));
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005368 __ j(equal, &done);
5369 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
5370 __ Assert(equal,
danno@chromium.org59400602013-08-13 17:09:37 +00005371 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005372 __ bind(&done);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005373 }
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00005374
5375 Label fast_elements_case;
5376 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5377 __ j(equal, &fast_elements_case);
5378 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5379
5380 __ bind(&fast_elements_case);
5381 GenerateCase(masm, FAST_ELEMENTS);
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00005382}
5383
5384
ricow@chromium.org65fae842010-08-25 15:26:24 +00005385#undef __
5386
5387} } // namespace v8::internal
5388
5389#endif // V8_TARGET_ARCH_IA32