blob: d7b05cf40bf9965f43194c9f18dcf42ed321a8e2 [file] [log] [blame]
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_IA32)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "ic-inl.h"
33#include "codegen-inl.h"
34#include "stub-cache.h"
35
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
ager@chromium.org65dad4b2009-04-23 08:48:43 +000039#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42static void ProbeTable(MacroAssembler* masm,
43 Code::Flags flags,
44 StubCache::Table table,
45 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000046 Register offset,
47 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048 ExternalReference key_offset(SCTableReference::keyReference(table));
49 ExternalReference value_offset(SCTableReference::valueReference(table));
50
51 Label miss;
52
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000053 if (extra.is_valid()) {
54 // Get the code entry from the cache.
55 __ mov(extra, Operand::StaticArray(offset, times_2, value_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000057 // Check that the key in the entry matches the name.
58 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
59 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000061 // Check that the flags match what we're looking for.
62 __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
63 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
64 __ cmp(offset, flags);
65 __ j(not_equal, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000067 // Jump to the first instruction in the code stub.
68 __ add(Operand(extra), Immediate(Code::kHeaderSize - kHeapObjectTag));
69 __ jmp(Operand(extra));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000071 __ bind(&miss);
72 } else {
73 // Save the offset on the stack.
74 __ push(offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000076 // Check that the key in the entry matches the name.
77 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
78 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000080 // Get the code entry from the cache.
81 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
82
83 // Check that the flags match what we're looking for.
84 __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
85 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
86 __ cmp(offset, flags);
87 __ j(not_equal, &miss);
88
89 // Restore offset and re-load code entry from cache.
90 __ pop(offset);
91 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
92
93 // Jump to the first instruction in the code stub.
94 __ add(Operand(offset), Immediate(Code::kHeaderSize - kHeapObjectTag));
95 __ jmp(Operand(offset));
96
97 // Pop at miss.
98 __ bind(&miss);
99 __ pop(offset);
100 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101}
102
103
104void StubCache::GenerateProbe(MacroAssembler* masm,
105 Code::Flags flags,
106 Register receiver,
107 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000108 Register scratch,
109 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 Label miss;
111
112 // Make sure that code is valid. The shifting code relies on the
113 // entry size being 8.
114 ASSERT(sizeof(Entry) == 8);
115
116 // Make sure the flags does not name a specific type.
117 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
118
119 // Make sure that there are no register conflicts.
120 ASSERT(!scratch.is(receiver));
121 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000122 ASSERT(!extra.is(receiver));
123 ASSERT(!extra.is(name));
124 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
126 // Check that the receiver isn't a smi.
127 __ test(receiver, Immediate(kSmiTagMask));
128 __ j(zero, &miss, not_taken);
129
130 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000131 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000132 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 __ xor_(scratch, flags);
134 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
135
136 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000137 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000140 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000141 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
142 __ xor_(scratch, flags);
143 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 __ sub(scratch, Operand(name));
145 __ add(Operand(scratch), Immediate(flags));
146 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
147
148 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000149 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
151 // Cache miss: Fall-through and let caller handle the miss by
152 // entering the runtime system.
153 __ bind(&miss);
154}
155
156
157void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
158 int index,
159 Register prototype) {
160 // Load the global or builtins object from the current context.
161 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
162 // Load the global context from the global or builtins object.
163 __ mov(prototype,
164 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
165 // Load the function from the global context.
166 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
167 // Load the initial map. The global functions all have initial maps.
168 __ mov(prototype,
169 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
170 // Load the prototype from the initial map.
171 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
172}
173
174
175void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
176 Register receiver,
177 Register scratch,
178 Label* miss_label) {
179 // Check that the receiver isn't a smi.
180 __ test(receiver, Immediate(kSmiTagMask));
181 __ j(zero, miss_label, not_taken);
182
183 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 __ j(not_equal, miss_label, not_taken);
186
187 // Load length directly from the JS array.
188 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
189 __ ret(0);
190}
191
192
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000193// Generate code to check if an object is a string. If the object is
194// a string, the map's instance type is left in the scratch register.
195static void GenerateStringCheck(MacroAssembler* masm,
196 Register receiver,
197 Register scratch,
198 Label* smi,
199 Label* non_string_object) {
200 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000202 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000204 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
206 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000207 ASSERT(kNotStringTag != 0);
208 __ test(scratch, Immediate(kNotStringTag));
209 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210}
211
212
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000213void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
214 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000215 Register scratch1,
216 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000217 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000218 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000220 // Check if the object is a string leaving the instance type in the
221 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000222 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000224 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000227
228 // Check if the object is a JSValue wrapper.
229 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000230 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000231 __ j(not_equal, miss, not_taken);
232
233 // Check if the wrapped value is a string and load the length
234 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000235 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
236 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
237 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000238 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
243 Register receiver,
244 Register scratch1,
245 Register scratch2,
246 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000247 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
ager@chromium.org7c537e22008-10-16 08:43:32 +0000252
253// Load a fast property out of a holder object (src). In-object properties
254// are loaded directly otherwise the property is loaded from the properties
255// fixed array.
256void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000257 Register dst, Register src,
258 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Adjust for the number of properties stored in the holder.
260 index -= holder->map()->inobject_properties();
261 if (index < 0) {
262 // Get the property straight out of the holder.
263 int offset = holder->map()->instance_size() + (index * kPointerSize);
264 __ mov(dst, FieldOperand(src, offset));
265 } else {
266 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000267 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
269 __ mov(dst, FieldOperand(dst, offset));
270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271}
272
273
ager@chromium.org5c838252010-02-19 08:53:10 +0000274static void PushInterceptorArguments(MacroAssembler* masm,
275 Register receiver,
276 Register holder,
277 Register name,
278 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 __ push(name);
280 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
281 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000282 Register scratch = name;
283 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
284 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000285 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000286 __ push(holder);
287 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000288}
289
290
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000291static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
292 Register receiver,
293 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000294 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000295 JSObject* holder_obj) {
296 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000297 __ CallExternalReference(
298 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
299 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000300}
301
302
303template <class Compiler>
304static void CompileLoadInterceptor(Compiler* compiler,
305 StubCompiler* stub_compiler,
306 MacroAssembler* masm,
307 JSObject* object,
308 JSObject* holder,
309 String* name,
310 LookupResult* lookup,
311 Register receiver,
312 Register scratch1,
313 Register scratch2,
314 Label* miss) {
315 ASSERT(holder->HasNamedInterceptor());
316 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
317
318 // Check that the receiver isn't a smi.
319 __ test(receiver, Immediate(kSmiTagMask));
320 __ j(zero, miss, not_taken);
321
322 // Check that the maps haven't changed.
323 Register reg =
324 stub_compiler->CheckPrototypes(object, receiver, holder,
325 scratch1, scratch2, name, miss);
326
ager@chromium.org5c838252010-02-19 08:53:10 +0000327 if (lookup->IsProperty() && lookup->IsCacheable()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000328 compiler->CompileCacheable(masm,
329 stub_compiler,
330 receiver,
331 reg,
332 scratch1,
333 scratch2,
334 holder,
335 lookup,
336 name,
337 miss);
338 } else {
339 compiler->CompileRegular(masm,
340 receiver,
341 reg,
342 scratch2,
343 holder,
344 miss);
345 }
346}
347
348
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000349class LoadInterceptorCompiler BASE_EMBEDDED {
350 public:
351 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
352
353 void CompileCacheable(MacroAssembler* masm,
354 StubCompiler* stub_compiler,
355 Register receiver,
356 Register holder,
357 Register scratch1,
358 Register scratch2,
359 JSObject* holder_obj,
360 LookupResult* lookup,
361 String* name,
362 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000363 AccessorInfo* callback = NULL;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000364 bool optimize = false;
365 // So far the most popular follow ups for interceptor loads are FIELD
366 // and CALLBACKS, so inline only them, other cases may be added
367 // later.
368 if (lookup->type() == FIELD) {
369 optimize = true;
370 } else if (lookup->type() == CALLBACKS) {
371 Object* callback_object = lookup->GetCallbackObject();
372 if (callback_object->IsAccessorInfo()) {
373 callback = AccessorInfo::cast(callback_object);
374 optimize = callback->getter() != NULL;
375 }
376 }
377
378 if (!optimize) {
379 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
380 return;
381 }
382
383 // Note: starting a frame here makes GC aware of pointers pushed below.
384 __ EnterInternalFrame();
385
386 if (lookup->type() == CALLBACKS) {
387 __ push(receiver);
388 }
389 __ push(holder);
390 __ push(name_);
391
392 CompileCallLoadPropertyWithInterceptor(masm,
393 receiver,
394 holder,
395 name_,
396 holder_obj);
397
398 Label interceptor_failed;
399 __ cmp(eax, Factory::no_interceptor_result_sentinel());
400 __ j(equal, &interceptor_failed);
401 __ LeaveInternalFrame();
402 __ ret(0);
403
404 __ bind(&interceptor_failed);
405 __ pop(name_);
406 __ pop(holder);
407 if (lookup->type() == CALLBACKS) {
408 __ pop(receiver);
409 }
410
411 __ LeaveInternalFrame();
412
413 if (lookup->type() == FIELD) {
414 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
415 lookup->holder(), scratch1,
416 scratch2,
417 name,
418 miss_label);
419 stub_compiler->GenerateFastPropertyLoad(masm, eax,
420 holder, lookup->holder(),
421 lookup->GetFieldIndex());
422 __ ret(0);
423 } else {
424 ASSERT(lookup->type() == CALLBACKS);
425 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
426 ASSERT(callback != NULL);
427 ASSERT(callback->getter() != NULL);
428
429 Label cleanup;
430 __ pop(scratch2);
431 __ push(receiver);
432 __ push(scratch2);
433
434 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
435 lookup->holder(), scratch1,
436 scratch2,
437 name,
438 &cleanup);
439
440 __ pop(scratch2); // save old return address
441 __ push(holder);
442 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
443 __ push(holder);
444 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
445 __ push(name_);
446 __ push(scratch2); // restore old return address
447
448 ExternalReference ref =
449 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000450 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000451
452 __ bind(&cleanup);
453 __ pop(scratch1);
454 __ pop(scratch2);
455 __ push(scratch1);
456 }
457 }
458
459
460 void CompileRegular(MacroAssembler* masm,
461 Register receiver,
462 Register holder,
463 Register scratch,
464 JSObject* holder_obj,
465 Label* miss_label) {
466 __ pop(scratch); // save old return address
467 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
468 __ push(scratch); // restore old return address
469
470 ExternalReference ref = ExternalReference(
471 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000472 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000473 }
474
475 private:
476 Register name_;
477};
478
479
ager@chromium.org5c838252010-02-19 08:53:10 +0000480// Reserves space for the extra arguments to FastHandleApiCall in the
481// caller's frame.
482//
483// These arguments are set by CheckPrototypes and GenerateFastApiCall.
484static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
485 // ----------- S t a t e -------------
486 // -- esp[0] : return address
487 // -- esp[4] : last argument in the internal frame of the caller
488 // -----------------------------------
489 __ pop(scratch);
490 __ push(Immediate(Smi::FromInt(0)));
491 __ push(Immediate(Smi::FromInt(0)));
492 __ push(Immediate(Smi::FromInt(0)));
493 __ push(Immediate(Smi::FromInt(0)));
494 __ push(scratch);
495}
496
497
498// Undoes the effects of ReserveSpaceForFastApiCall.
499static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
500 // ----------- S t a t e -------------
501 // -- esp[0] : return address
502 // -- esp[4] : last fast api call extra argument
503 // -- ...
504 // -- esp[16] : first fast api call extra argument
505 // -- esp[20] : last argument in the internal frame
506 // -----------------------------------
507 __ pop(scratch);
508 __ add(Operand(esp), Immediate(kPointerSize * 4));
509 __ push(scratch);
510}
511
512
513// Generates call to FastHandleApiCall builtin.
514static void GenerateFastApiCall(MacroAssembler* masm,
515 const CallOptimization& optimization,
516 int argc) {
517 // ----------- S t a t e -------------
518 // -- esp[0] : return address
519 // -- esp[4] : object passing the type check
520 // (last fast api call extra argument,
521 // set by CheckPrototypes)
522 // -- esp[8] : api call data
523 // -- esp[12] : api callback
524 // -- esp[16] : api function
525 // (first fast api call extra argument)
526 // -- esp[20] : last argument
527 // -- ...
528 // -- esp[(argc + 5) * 4] : first argument
529 // -- esp[(argc + 6) * 4] : receiver
530 // -----------------------------------
531
532 // Get the function and setup the context.
533 JSFunction* function = optimization.constant_function();
534 __ mov(edi, Immediate(Handle<JSFunction>(function)));
535 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
536
537 // Pass the additional arguments FastHandleApiCall expects.
538 __ mov(Operand(esp, 4 * kPointerSize), edi);
539 bool info_loaded = false;
540 Object* callback = optimization.api_call_info()->callback();
541 if (Heap::InNewSpace(callback)) {
542 info_loaded = true;
543 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
544 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
545 __ mov(Operand(esp, 3 * kPointerSize), ebx);
546 } else {
547 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
548 }
549 Object* call_data = optimization.api_call_info()->data();
550 if (Heap::InNewSpace(call_data)) {
551 if (!info_loaded) {
552 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
553 }
554 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
555 __ mov(Operand(esp, 2 * kPointerSize), ebx);
556 } else {
557 __ mov(Operand(esp, 2 * kPointerSize),
558 Immediate(Handle<Object>(call_data)));
559 }
560
561 // Set the number of arguments.
562 __ mov(eax, Immediate(argc + 4));
563
564 // Jump to the fast api call builtin (tail call).
565 Handle<Code> code = Handle<Code>(
566 Builtins::builtin(Builtins::FastHandleApiCall));
567 ParameterCount expected(0);
568 __ InvokeCode(code, expected, expected,
569 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
570}
571
572
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000573class CallInterceptorCompiler BASE_EMBEDDED {
574 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000575 CallInterceptorCompiler(StubCompiler* stub_compiler,
576 const ParameterCount& arguments,
577 Register name)
578 : stub_compiler_(stub_compiler),
579 arguments_(arguments),
580 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000581
ager@chromium.org5c838252010-02-19 08:53:10 +0000582 void Compile(MacroAssembler* masm,
583 JSObject* object,
584 JSObject* holder,
585 String* name,
586 LookupResult* lookup,
587 Register receiver,
588 Register scratch1,
589 Register scratch2,
590 Label* miss) {
591 ASSERT(holder->HasNamedInterceptor());
592 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
593
594 // Check that the receiver isn't a smi.
595 __ test(receiver, Immediate(kSmiTagMask));
596 __ j(zero, miss, not_taken);
597
598 CallOptimization optimization(lookup);
599
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000600 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 CompileCacheable(masm,
602 object,
603 receiver,
604 scratch1,
605 scratch2,
606 holder,
607 lookup,
608 name,
609 optimization,
610 miss);
611 } else {
612 CompileRegular(masm,
613 object,
614 receiver,
615 scratch1,
616 scratch2,
617 name,
618 holder,
619 miss);
620 }
621 }
622
623 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000624 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000625 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000626 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000627 Register scratch1,
628 Register scratch2,
629 JSObject* holder_obj,
630 LookupResult* lookup,
631 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000632 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000633 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000634 ASSERT(optimization.is_constant_call());
635 ASSERT(!lookup->holder()->IsGlobalObject());
636
637 int depth1 = kInvalidProtoDepth;
638 int depth2 = kInvalidProtoDepth;
639 bool can_do_fast_api_call = false;
640 if (optimization.is_simple_api_call() &&
641 !lookup->holder()->IsGlobalObject()) {
642 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
643 if (depth1 == kInvalidProtoDepth) {
644 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
645 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000646 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000647 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
648 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000649 }
650
ager@chromium.org5c838252010-02-19 08:53:10 +0000651 __ IncrementCounter(&Counters::call_const_interceptor, 1);
652
653 if (can_do_fast_api_call) {
654 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
655 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000656 }
657
ager@chromium.org5c838252010-02-19 08:53:10 +0000658 Label miss_cleanup;
659 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
660 Register holder =
661 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
662 scratch1, scratch2, name,
663 depth1, miss);
664
665 Label regular_invoke;
666 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
667
668 // Generate code for the failed interceptor case.
669
670 // Check the lookup is still valid.
671 stub_compiler_->CheckPrototypes(holder_obj, receiver,
672 lookup->holder(),
673 scratch1, scratch2, name,
674 depth2, miss);
675
676 if (can_do_fast_api_call) {
677 GenerateFastApiCall(masm, optimization, arguments_.immediate());
678 } else {
679 __ InvokeFunction(optimization.constant_function(), arguments_,
680 JUMP_FUNCTION);
681 }
682
683 if (can_do_fast_api_call) {
684 __ bind(&miss_cleanup);
685 FreeSpaceForFastApiCall(masm, scratch1);
686 __ jmp(miss_label);
687 }
688
689 __ bind(&regular_invoke);
690 if (can_do_fast_api_call) {
691 FreeSpaceForFastApiCall(masm, scratch1);
692 }
693 }
694
695 void CompileRegular(MacroAssembler* masm,
696 JSObject* object,
697 Register receiver,
698 Register scratch1,
699 Register scratch2,
700 String* name,
701 JSObject* holder_obj,
702 Label* miss_label) {
703 Register holder =
704 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
705 scratch1, scratch2, name,
706 miss_label);
707
708 __ EnterInternalFrame();
709 // Save the name_ register across the call.
710 __ push(name_);
711
712 PushInterceptorArguments(masm,
713 receiver,
714 holder,
715 name_,
716 holder_obj);
717
718 __ CallExternalReference(
719 ExternalReference(
720 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
721 5);
722
723 // Restore the name_ register.
724 __ pop(name_);
725 __ LeaveInternalFrame();
726 }
727
728 void LoadWithInterceptor(MacroAssembler* masm,
729 Register receiver,
730 Register holder,
731 JSObject* holder_obj,
732 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000733 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000734 __ push(holder); // Save the holder.
735 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000736
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000737 CompileCallLoadPropertyWithInterceptor(masm,
738 receiver,
739 holder,
740 name_,
741 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000742
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000743 __ pop(name_); // Restore the name.
744 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000745 __ LeaveInternalFrame();
746
747 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000748 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000749 }
750
ager@chromium.org5c838252010-02-19 08:53:10 +0000751 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000752 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000753 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000754};
755
756
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
758 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
759 Code* code = NULL;
760 if (kind == Code::LOAD_IC) {
761 code = Builtins::builtin(Builtins::LoadIC_Miss);
762 } else {
763 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
764 }
765
766 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000767 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768}
769
770
ager@chromium.org5c838252010-02-19 08:53:10 +0000771// Both name_reg and receiver_reg are preserved on jumps to miss_label,
772// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000773void StubCompiler::GenerateStoreField(MacroAssembler* masm,
774 JSObject* object,
775 int index,
776 Map* transition,
777 Register receiver_reg,
778 Register name_reg,
779 Register scratch,
780 Label* miss_label) {
781 // Check that the object isn't a smi.
782 __ test(receiver_reg, Immediate(kSmiTagMask));
783 __ j(zero, miss_label, not_taken);
784
785 // Check that the map of the object hasn't changed.
786 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
787 Immediate(Handle<Map>(object->map())));
788 __ j(not_equal, miss_label, not_taken);
789
790 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000791 if (object->IsJSGlobalProxy()) {
792 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000793 }
794
795 // Stub never generated for non-global objects that require access
796 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000797 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000798
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000799 // Perform map transition for the receiver if necessary.
800 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
801 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000802 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000803 __ pop(scratch); // Return address.
804 __ push(receiver_reg);
805 __ push(Immediate(Handle<Map>(transition)));
806 __ push(eax);
807 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000808 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000809 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000810 return;
811 }
812
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000813 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000814 // Update the map of the object; no write barrier updating is
815 // needed because the map is never in new space.
816 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
817 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000818 }
819
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000820 // Adjust for the number of properties stored in the object. Even in the
821 // face of a transition we can use the old map here because the size of the
822 // object and the number of in-object properties is not going to change.
823 index -= object->map()->inobject_properties();
824
ager@chromium.org7c537e22008-10-16 08:43:32 +0000825 if (index < 0) {
826 // Set the property straight into the object.
827 int offset = object->map()->instance_size() + (index * kPointerSize);
828 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000829
ager@chromium.org7c537e22008-10-16 08:43:32 +0000830 // Update the write barrier for the array address.
831 // Pass the value being stored in the now unused name_reg.
832 __ mov(name_reg, Operand(eax));
833 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
834 } else {
835 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000836 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000837 // Get the properties array (optimistically).
838 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000839 __ mov(FieldOperand(scratch, offset), eax);
840
841 // Update the write barrier for the array address.
842 // Pass the value being stored in the now unused name_reg.
843 __ mov(name_reg, Operand(eax));
844 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
845 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846
847 // Return the value (register eax).
848 __ ret(0);
849}
850
851
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000852// Generate code to check that a global property cell is empty. Create
853// the property cell at compilation time if no cell exists for the
854// property.
855static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
856 GlobalObject* global,
857 String* name,
858 Register scratch,
859 Label* miss) {
860 Object* probe = global->EnsurePropertyCell(name);
861 if (probe->IsFailure()) return probe;
862 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
863 ASSERT(cell->value()->IsTheHole());
864 __ mov(scratch, Immediate(Handle<Object>(cell)));
865 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
866 Immediate(Factory::the_hole_value()));
867 __ j(not_equal, miss, not_taken);
868 return cell;
869}
870
871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000873#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874
875
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000876Register StubCompiler::CheckPrototypes(JSObject* object,
877 Register object_reg,
878 JSObject* holder,
879 Register holder_reg,
880 Register scratch,
881 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000882 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000883 Label* miss) {
884 // Check that the maps haven't changed.
885 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000886 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
887 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000888
889 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000890 // that their maps haven't changed. We also need to check that the
891 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000892 while (object != holder) {
893 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000894 Object* cell = GenerateCheckPropertyCell(masm(),
895 GlobalObject::cast(object),
896 name,
897 scratch,
898 miss);
899 if (cell->IsFailure()) {
900 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000901 return result;
902 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000903 }
904 object = JSObject::cast(object->GetPrototype());
905 }
906
ager@chromium.org5c838252010-02-19 08:53:10 +0000907 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000908 return result;
909}
910
911
912void StubCompiler::GenerateLoadField(JSObject* object,
913 JSObject* holder,
914 Register receiver,
915 Register scratch1,
916 Register scratch2,
917 int index,
918 String* name,
919 Label* miss) {
920 // Check that the receiver isn't a smi.
921 __ test(receiver, Immediate(kSmiTagMask));
922 __ j(zero, miss, not_taken);
923
924 // Check the prototype chain.
925 Register reg =
926 CheckPrototypes(object, receiver, holder,
927 scratch1, scratch2, name, miss);
928
929 // Get the value from the properties.
930 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
931 __ ret(0);
932}
933
934
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000935bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000936 JSObject* holder,
937 Register receiver,
938 Register name_reg,
939 Register scratch1,
940 Register scratch2,
941 AccessorInfo* callback,
942 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000943 Label* miss,
944 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000945 // Check that the receiver isn't a smi.
946 __ test(receiver, Immediate(kSmiTagMask));
947 __ j(zero, miss, not_taken);
948
949 // Check that the maps haven't changed.
950 Register reg =
951 CheckPrototypes(object, receiver, holder,
952 scratch1, scratch2, name, miss);
953
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000954 Handle<AccessorInfo> callback_handle(callback);
955
956 Register other = reg.is(scratch1) ? scratch2 : scratch1;
957 __ EnterInternalFrame();
958 __ PushHandleScope(other);
959 // Push the stack address where the list of arguments ends
960 __ mov(other, esp);
961 __ sub(Operand(other), Immediate(2 * kPointerSize));
962 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000963 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000964 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000965 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000966 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000967 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000968 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000969 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000970 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000971 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000972 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000973
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000974 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000975 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000976 Address getter_address = v8::ToCData<Address>(callback->getter());
977 ApiFunction fun(getter_address);
978 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000979 // Emitting a stub call may try to allocate (if the code is not
980 // already generated). Do not allow the assembler to perform a
981 // garbage collection but instead return the allocation failure
982 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000983 Object* result = masm()->TryCallStub(&stub);
984 if (result->IsFailure()) {
985 *failure = Failure::cast(result);
986 return false;
987 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000988
989 // We need to avoid using eax since that now holds the result.
990 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000991 // Emitting PopHandleScope may try to allocate. Do not allow the
992 // assembler to perform a garbage collection but instead return a
993 // failure object.
994 result = masm()->TryPopHandleScope(eax, tmp);
995 if (result->IsFailure()) {
996 *failure = Failure::cast(result);
997 return false;
998 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000999 __ LeaveInternalFrame();
1000
1001 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001002 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001003}
1004
1005
1006void StubCompiler::GenerateLoadConstant(JSObject* object,
1007 JSObject* holder,
1008 Register receiver,
1009 Register scratch1,
1010 Register scratch2,
1011 Object* value,
1012 String* name,
1013 Label* miss) {
1014 // Check that the receiver isn't a smi.
1015 __ test(receiver, Immediate(kSmiTagMask));
1016 __ j(zero, miss, not_taken);
1017
1018 // Check that the maps haven't changed.
1019 Register reg =
1020 CheckPrototypes(object, receiver, holder,
1021 scratch1, scratch2, name, miss);
1022
1023 // Return the constant value.
1024 __ mov(eax, Handle<Object>(value));
1025 __ ret(0);
1026}
1027
1028
1029void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1030 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001031 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001032 Register receiver,
1033 Register name_reg,
1034 Register scratch1,
1035 Register scratch2,
1036 String* name,
1037 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001038 LoadInterceptorCompiler compiler(name_reg);
1039 CompileLoadInterceptor(&compiler,
1040 this,
1041 masm(),
1042 object,
1043 holder,
1044 name,
1045 lookup,
1046 receiver,
1047 scratch1,
1048 scratch2,
1049 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001050}
1051
1052
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053// TODO(1241006): Avoid having lazy compile stubs specialized by the
1054// number of arguments. It is not needed anymore.
1055Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001057 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058
1059 // Push a copy of the function onto the stack.
1060 __ push(edi);
1061
1062 __ push(edi); // function is also the parameter to the runtime call
1063 __ CallRuntime(Runtime::kLazyCompile, 1);
1064 __ pop(edi);
1065
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001066 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001067 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068
1069 // Do a tail-call of the compiled function.
1070 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1071 __ jmp(Operand(ecx));
1072
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001073 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001074}
1075
1076
ager@chromium.org5c838252010-02-19 08:53:10 +00001077Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001079 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001080 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001082 // -- ecx : name
1083 // -- esp[0] : return address
1084 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1085 // -- ...
1086 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001088 Label miss;
1089
1090 // Get the receiver from the stack.
1091 const int argc = arguments().immediate();
1092 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1093
1094 // Check that the receiver isn't a smi.
1095 __ test(edx, Immediate(kSmiTagMask));
1096 __ j(zero, &miss, not_taken);
1097
1098 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001099 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
ager@chromium.org7c537e22008-10-16 08:43:32 +00001101 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001102
1103 // Check that the function really is a function.
1104 __ test(edi, Immediate(kSmiTagMask));
1105 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001106 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107 __ j(not_equal, &miss, not_taken);
1108
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001109 // Patch the receiver on the stack with the global proxy if
1110 // necessary.
1111 if (object->IsGlobalObject()) {
1112 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1113 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1114 }
1115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001116 // Invoke the function.
1117 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1118
1119 // Handle call cache miss.
1120 __ bind(&miss);
1121 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001122 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
1124 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001125 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126}
1127
1128
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001129Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1130 JSObject* holder,
1131 JSFunction* function,
1132 String* name,
1133 CheckType check) {
1134 // ----------- S t a t e -------------
1135 // -- ecx : name
1136 // -- esp[0] : return address
1137 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1138 // -- ...
1139 // -- esp[(argc + 1) * 4] : receiver
1140 // -----------------------------------
1141 ASSERT(check == RECEIVER_MAP_CHECK);
1142
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001143 // If object is not an array, bail out to regular call.
1144 if (!object->IsJSArray()) {
1145 return Heap::undefined_value();
1146 }
1147
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001148 Label miss;
1149
1150 // Get the receiver from the stack.
1151 const int argc = arguments().immediate();
1152 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1153
1154 // Check that the receiver isn't a smi.
1155 __ test(edx, Immediate(kSmiTagMask));
1156 __ j(zero, &miss);
1157
1158 CheckPrototypes(JSObject::cast(object), edx,
1159 holder, ebx,
1160 eax, name, &miss);
1161
1162 if (argc == 0) {
1163 // Noop, return the length.
1164 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1165 __ ret((argc + 1) * kPointerSize);
1166 } else {
1167 // Get the elements array of the object.
1168 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1169
1170 // Check that the elements are in fast mode (not dictionary).
1171 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1172 Immediate(Factory::fixed_array_map()));
1173 __ j(not_equal, &miss);
1174
1175 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001176 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001177
1178 // Get the array's length into eax and calculate new length.
1179 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1180 STATIC_ASSERT(kSmiTagSize == 1);
1181 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001182 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001183
1184 // Get the element's length into ecx.
1185 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1186 __ SmiTag(ecx);
1187
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001188 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001189 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001190 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001191
1192 // Save new length.
1193 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1194
1195 // Push the element.
1196 __ lea(edx, FieldOperand(ebx,
1197 eax, times_half_pointer_size,
1198 FixedArray::kHeaderSize - argc * kPointerSize));
1199 __ mov(ecx, Operand(esp, argc * kPointerSize));
1200 __ mov(Operand(edx, 0), ecx);
1201
1202 // Check if value is a smi.
1203 __ test(ecx, Immediate(kSmiTagMask));
1204 __ j(not_zero, &with_rset_update);
1205
1206 __ bind(&exit);
1207 __ ret((argc + 1) * kPointerSize);
1208
1209 __ bind(&with_rset_update);
1210
1211 __ InNewSpace(ebx, ecx, equal, &exit);
1212
1213 RecordWriteStub stub(ebx, edx, ecx);
1214 __ CallStub(&stub);
1215 __ ret((argc + 1) * kPointerSize);
1216
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001217 __ bind(&attempt_to_grow_elements);
1218 ExternalReference new_space_allocation_top =
1219 ExternalReference::new_space_allocation_top_address();
1220 ExternalReference new_space_allocation_limit =
1221 ExternalReference::new_space_allocation_limit_address();
1222
1223 const int kAllocationDelta = 4;
1224 // Load top.
1225 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1226
1227 // Check if it's the end of elements.
1228 __ lea(edx, FieldOperand(ebx,
1229 eax, times_half_pointer_size,
1230 FixedArray::kHeaderSize - argc * kPointerSize));
1231 __ cmp(edx, Operand(ecx));
1232 __ j(not_equal, &call_builtin);
1233 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1234 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001235 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001236
1237 // We fit and could grow elements.
1238 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1239 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001240
1241 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001242 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001243 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001244 for (int i = 1; i < kAllocationDelta; i++) {
1245 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001246 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001247 }
1248
1249 // Restore receiver to edx as finish sequence assumes it's here.
1250 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1251
1252 // Increment element's and array's sizes.
1253 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1254 Immediate(kAllocationDelta));
1255 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1256
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001257 // Elements are in new space, so no remembered set updates are necessary.
1258 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001259
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001260 __ bind(&call_builtin);
1261 }
1262
1263 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1264 argc + 1,
1265 1);
1266 }
1267
1268 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001269 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1270 __ jmp(ic, RelocInfo::CODE_TARGET);
1271
1272 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001273 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001274}
1275
1276
1277Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1278 JSObject* holder,
1279 JSFunction* function,
1280 String* name,
1281 CheckType check) {
1282 // ----------- S t a t e -------------
1283 // -- ecx : name
1284 // -- esp[0] : return address
1285 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1286 // -- ...
1287 // -- esp[(argc + 1) * 4] : receiver
1288 // -----------------------------------
1289 ASSERT(check == RECEIVER_MAP_CHECK);
1290
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001291 // If object is not an array, bail out to regular call.
1292 if (!object->IsJSArray()) {
1293 return Heap::undefined_value();
1294 }
1295
ager@chromium.orgac091b72010-05-05 07:34:42 +00001296 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001297
1298 // Get the receiver from the stack.
1299 const int argc = arguments().immediate();
1300 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1301
1302 // Check that the receiver isn't a smi.
1303 __ test(edx, Immediate(kSmiTagMask));
1304 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001305 CheckPrototypes(JSObject::cast(object), edx,
1306 holder, ebx,
1307 eax, name, &miss);
1308
1309 // Get the elements array of the object.
1310 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1311
1312 // Check that the elements are in fast mode (not dictionary).
1313 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1314 Immediate(Factory::fixed_array_map()));
1315 __ j(not_equal, &miss);
1316
1317 // Get the array's length into ecx and calculate new length.
1318 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1319 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001320 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001321
1322 // Get the last element.
1323 STATIC_ASSERT(kSmiTagSize == 1);
1324 STATIC_ASSERT(kSmiTag == 0);
1325 __ mov(eax, FieldOperand(ebx,
1326 ecx, times_half_pointer_size,
1327 FixedArray::kHeaderSize));
1328 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1329 __ j(equal, &call_builtin);
1330
1331 // Set the array's length.
1332 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1333
1334 // Fill with the hole.
1335 __ mov(FieldOperand(ebx,
1336 ecx, times_half_pointer_size,
1337 FixedArray::kHeaderSize),
1338 Immediate(Factory::the_hole_value()));
1339 __ ret((argc + 1) * kPointerSize);
1340
ager@chromium.orgac091b72010-05-05 07:34:42 +00001341 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001342 __ mov(eax, Immediate(Factory::undefined_value()));
1343 __ ret((argc + 1) * kPointerSize);
1344
1345 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001346 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1347 argc + 1,
1348 1);
1349
1350 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001351 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1352 __ jmp(ic, RelocInfo::CODE_TARGET);
1353
1354 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001355 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001356}
1357
1358
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359Object* CallStubCompiler::CompileCallConstant(Object* object,
1360 JSObject* holder,
1361 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001362 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001363 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001365 // -- ecx : name
1366 // -- esp[0] : return address
1367 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1368 // -- ...
1369 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001370 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001371
1372 SharedFunctionInfo* function_info = function->shared();
1373 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001374 const int id = function_info->custom_call_generator_id();
1375 Object* result =
1376 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001377 // undefined means bail out to regular compiler.
1378 if (!result->IsUndefined()) {
1379 return result;
1380 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001381 }
1382
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001383 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001384
1385 // Get the receiver from the stack.
1386 const int argc = arguments().immediate();
1387 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1388
1389 // Check that the receiver isn't a smi.
1390 if (check != NUMBER_CHECK) {
1391 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001392 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001393 }
1394
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001395 // Make sure that it's okay not to patch the on stack receiver
1396 // unless we're doing a receiver map check.
1397 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1398
ager@chromium.org5c838252010-02-19 08:53:10 +00001399 CallOptimization optimization(function);
1400 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001401 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001402
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 switch (check) {
1404 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001405 __ IncrementCounter(&Counters::call_const, 1);
1406
1407 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1408 depth = optimization.GetPrototypeDepthOfExpectedType(
1409 JSObject::cast(object), holder);
1410 }
1411
1412 if (depth != kInvalidProtoDepth) {
1413 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1414 ReserveSpaceForFastApiCall(masm(), eax);
1415 }
1416
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001418 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001419 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001420
1421 // Patch the receiver on the stack with the global proxy if
1422 // necessary.
1423 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001424 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001425 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1426 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1427 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001428 break;
1429
1430 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001431 if (!function->IsBuiltin()) {
1432 // Calling non-builtins with a value as receiver requires boxing.
1433 __ jmp(&miss);
1434 } else {
1435 // Check that the object is a string or a symbol.
1436 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1437 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1438 __ cmp(eax, FIRST_NONSTRING_TYPE);
1439 __ j(above_equal, &miss, not_taken);
1440 // Check that the maps starting from the prototype haven't changed.
1441 GenerateLoadGlobalFunctionPrototype(masm(),
1442 Context::STRING_FUNCTION_INDEX,
1443 eax);
1444 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1445 ebx, edx, name, &miss);
1446 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447 break;
1448
1449 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001450 if (!function->IsBuiltin()) {
1451 // Calling non-builtins with a value as receiver requires boxing.
1452 __ jmp(&miss);
1453 } else {
1454 Label fast;
1455 // Check that the object is a smi or a heap number.
1456 __ test(edx, Immediate(kSmiTagMask));
1457 __ j(zero, &fast, taken);
1458 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1459 __ j(not_equal, &miss, not_taken);
1460 __ bind(&fast);
1461 // Check that the maps starting from the prototype haven't changed.
1462 GenerateLoadGlobalFunctionPrototype(masm(),
1463 Context::NUMBER_FUNCTION_INDEX,
1464 eax);
1465 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1466 ebx, edx, name, &miss);
1467 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001468 break;
1469 }
1470
1471 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001472 if (!function->IsBuiltin()) {
1473 // Calling non-builtins with a value as receiver requires boxing.
1474 __ jmp(&miss);
1475 } else {
1476 Label fast;
1477 // Check that the object is a boolean.
1478 __ cmp(edx, Factory::true_value());
1479 __ j(equal, &fast, taken);
1480 __ cmp(edx, Factory::false_value());
1481 __ j(not_equal, &miss, not_taken);
1482 __ bind(&fast);
1483 // Check that the maps starting from the prototype haven't changed.
1484 GenerateLoadGlobalFunctionPrototype(masm(),
1485 Context::BOOLEAN_FUNCTION_INDEX,
1486 eax);
1487 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1488 ebx, edx, name, &miss);
1489 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 break;
1491 }
1492
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001493 default:
1494 UNREACHABLE();
1495 }
1496
ager@chromium.org5c838252010-02-19 08:53:10 +00001497 if (depth != kInvalidProtoDepth) {
1498 GenerateFastApiCall(masm(), optimization, argc);
1499 } else {
1500 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1501 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502
1503 // Handle call cache miss.
1504 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001505 if (depth != kInvalidProtoDepth) {
1506 FreeSpaceForFastApiCall(masm(), eax);
1507 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001508 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001510 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001511
1512 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001513 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514}
1515
1516
ager@chromium.org5c838252010-02-19 08:53:10 +00001517Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 JSObject* holder,
1519 String* name) {
1520 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001521 // -- ecx : name
1522 // -- esp[0] : return address
1523 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1524 // -- ...
1525 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 Label miss;
1528
1529 // Get the number of arguments.
1530 const int argc = arguments().immediate();
1531
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001532 LookupResult lookup;
1533 LookupPostInterceptor(holder, name, &lookup);
1534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535 // Get the receiver from the stack.
1536 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001537
ager@chromium.org5c838252010-02-19 08:53:10 +00001538 CallInterceptorCompiler compiler(this, arguments(), ecx);
1539 compiler.Compile(masm(),
1540 object,
1541 holder,
1542 name,
1543 &lookup,
1544 edx,
1545 ebx,
1546 edi,
1547 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001549 // Restore receiver.
1550 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551
1552 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001553 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001555 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 __ j(not_equal, &miss, not_taken);
1557
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001558 // Patch the receiver on the stack with the global proxy if
1559 // necessary.
1560 if (object->IsGlobalObject()) {
1561 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1562 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1563 }
1564
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001566 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1568
1569 // Handle load cache miss.
1570 __ bind(&miss);
1571 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001572 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573
1574 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001575 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576}
1577
1578
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001579Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1580 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001581 JSGlobalPropertyCell* cell,
1582 JSFunction* function,
1583 String* name) {
1584 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001585 // -- ecx : name
1586 // -- esp[0] : return address
1587 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1588 // -- ...
1589 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001590 // -----------------------------------
1591 Label miss;
1592
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001593 // Get the number of arguments.
1594 const int argc = arguments().immediate();
1595
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001596 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001597 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001598
1599 // If the object is the holder then we know that it's a global
1600 // object which can only happen for contextual calls. In this case,
1601 // the receiver cannot be a smi.
1602 if (object != holder) {
1603 __ test(edx, Immediate(kSmiTagMask));
1604 __ j(zero, &miss, not_taken);
1605 }
1606
1607 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001608 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001609
1610 // Get the value from the cell.
1611 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1612 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1613
1614 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001615 if (Heap::InNewSpace(function)) {
1616 // We can't embed a pointer to a function in new space so we have
1617 // to verify that the shared function info is unchanged. This has
1618 // the nice side effect that multiple closures based on the same
1619 // function can all use this call IC. Before we load through the
1620 // function, we have to verify that it still is a function.
1621 __ test(edi, Immediate(kSmiTagMask));
1622 __ j(zero, &miss, not_taken);
1623 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1624 __ j(not_equal, &miss, not_taken);
1625
1626 // Check the shared function info. Make sure it hasn't changed.
1627 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1628 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1629 __ j(not_equal, &miss, not_taken);
1630 } else {
1631 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1632 __ j(not_equal, &miss, not_taken);
1633 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001634
1635 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001636 if (object->IsGlobalObject()) {
1637 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1638 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1639 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001640
1641 // Setup the context (function already in edi).
1642 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1643
1644 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001645 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001646 ASSERT(function->is_compiled());
1647 Handle<Code> code(function->code());
1648 ParameterCount expected(function->shared()->formal_parameter_count());
1649 __ InvokeCode(code, expected, arguments(),
1650 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1651
1652 // Handle call cache miss.
1653 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001654 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1655 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1656 __ jmp(ic, RelocInfo::CODE_TARGET);
1657
1658 // Return the generated code.
1659 return GetCode(NORMAL, name);
1660}
1661
1662
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1664 int index,
1665 Map* transition,
1666 String* name) {
1667 // ----------- S t a t e -------------
1668 // -- eax : value
1669 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001670 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 Label miss;
1674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001676 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001677 object,
1678 index,
1679 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001680 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001681 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682
1683 // Handle store cache miss.
1684 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001685 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001687 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
1689 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691}
1692
1693
1694Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1695 AccessorInfo* callback,
1696 String* name) {
1697 // ----------- S t a t e -------------
1698 // -- eax : value
1699 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001700 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 Label miss;
1704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001706 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001707 __ j(zero, &miss, not_taken);
1708
1709 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001710 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001711 Immediate(Handle<Map>(object->map())));
1712 __ j(not_equal, &miss, not_taken);
1713
1714 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001715 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001716 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 }
1718
1719 // Stub never generated for non-global objects that require access
1720 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001721 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001722
1723 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001724 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001725 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1726 __ push(ecx); // name
1727 __ push(eax); // value
1728 __ push(ebx); // restore return address
1729
mads.s.ager31e71382008-08-13 09:32:07 +00001730 // Do tail-call to the runtime system.
1731 ExternalReference store_callback_property =
1732 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001733 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
1735 // Handle store cache miss.
1736 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001738 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739
1740 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001741 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001742}
1743
1744
1745Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1746 String* name) {
1747 // ----------- S t a t e -------------
1748 // -- eax : value
1749 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001750 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001752 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 Label miss;
1754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001756 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 __ j(zero, &miss, not_taken);
1758
1759 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001760 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 Immediate(Handle<Map>(receiver->map())));
1762 __ j(not_equal, &miss, not_taken);
1763
1764 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001765 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001766 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 }
1768
1769 // Stub never generated for non-global objects that require access
1770 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001771 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772
1773 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001774 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 __ push(ecx); // name
1776 __ push(eax); // value
1777 __ push(ebx); // restore return address
1778
mads.s.ager31e71382008-08-13 09:32:07 +00001779 // Do tail-call to the runtime system.
1780 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001782 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783
1784 // Handle store cache miss.
1785 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001787 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788
1789 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001790 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791}
1792
1793
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001794Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1795 JSGlobalPropertyCell* cell,
1796 String* name) {
1797 // ----------- S t a t e -------------
1798 // -- eax : value
1799 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001800 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001801 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001802 // -----------------------------------
1803 Label miss;
1804
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001805 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001806 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001807 Immediate(Handle<Map>(object->map())));
1808 __ j(not_equal, &miss, not_taken);
1809
1810 // Store the value in the cell.
1811 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1812 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1813
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001814 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001815 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001816 __ ret(0);
1817
1818 // Handle store cache miss.
1819 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001820 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1821 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1822 __ jmp(ic, RelocInfo::CODE_TARGET);
1823
1824 // Return the generated code.
1825 return GetCode(NORMAL, name);
1826}
1827
1828
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1830 int index,
1831 Map* transition,
1832 String* name) {
1833 // ----------- S t a t e -------------
1834 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001835 // -- ecx : key
1836 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 Label miss;
1840
1841 __ IncrementCounter(&Counters::keyed_store_field, 1);
1842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001843 // Check that the name has not changed.
1844 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1845 __ j(not_equal, &miss, not_taken);
1846
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001847 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001848 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001849 object,
1850 index,
1851 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001852 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001853 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
1855 // Handle store cache miss.
1856 __ bind(&miss);
1857 __ DecrementCounter(&Counters::keyed_store_field, 1);
1858 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001859 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860
1861 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001862 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863}
1864
1865
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001866Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1867 JSObject* object,
1868 JSObject* last) {
1869 // ----------- S t a t e -------------
1870 // -- eax : receiver
1871 // -- ecx : name
1872 // -- esp[0] : return address
1873 // -----------------------------------
1874 Label miss;
1875
1876 // Check that the receiver isn't a smi.
1877 __ test(eax, Immediate(kSmiTagMask));
1878 __ j(zero, &miss, not_taken);
1879
1880 // Check the maps of the full prototype chain. Also check that
1881 // global property cells up to (but not including) the last object
1882 // in the prototype chain are empty.
1883 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1884
1885 // If the last object in the prototype chain is a global object,
1886 // check that the global property cell is empty.
1887 if (last->IsGlobalObject()) {
1888 Object* cell = GenerateCheckPropertyCell(masm(),
1889 GlobalObject::cast(last),
1890 name,
1891 edx,
1892 &miss);
1893 if (cell->IsFailure()) return cell;
1894 }
1895
1896 // Return undefined if maps of the full prototype chain are still the
1897 // same and no global property with this name contains a value.
1898 __ mov(eax, Factory::undefined_value());
1899 __ ret(0);
1900
1901 __ bind(&miss);
1902 GenerateLoadMiss(masm(), Code::LOAD_IC);
1903
1904 // Return the generated code.
1905 return GetCode(NONEXISTENT, Heap::empty_string());
1906}
1907
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1910 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 int index,
1912 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001914 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 // -- ecx : name
1916 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 Label miss;
1919
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001920 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 __ bind(&miss);
1922 GenerateLoadMiss(masm(), Code::LOAD_IC);
1923
1924 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926}
1927
1928
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001929Object* LoadStubCompiler::CompileLoadCallback(String* name,
1930 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001932 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001934 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 // -- ecx : name
1936 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 Label miss;
1939
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001940 Failure* failure = Failure::InternalError();
1941 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1942 callback, name, &miss, &failure);
1943 if (!success) return failure;
1944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 __ bind(&miss);
1946 GenerateLoadMiss(masm(), Code::LOAD_IC);
1947
1948 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950}
1951
1952
1953Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1954 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 Object* value,
1956 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001958 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 // -- ecx : name
1960 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 Label miss;
1963
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001964 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 __ bind(&miss);
1966 GenerateLoadMiss(masm(), Code::LOAD_IC);
1967
1968 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970}
1971
1972
1973Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1974 JSObject* holder,
1975 String* name) {
1976 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001977 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 // -- ecx : name
1979 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 Label miss;
1982
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001983 LookupResult lookup;
1984 LookupPostInterceptor(holder, name, &lookup);
1985
ager@chromium.orge2902be2009-06-08 12:21:35 +00001986 // TODO(368): Compile in the whole chain: all the interceptors in
1987 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001988 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001989 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001990 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001991 eax,
1992 ecx,
1993 edx,
1994 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001995 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001996 &miss);
1997
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 __ bind(&miss);
1999 GenerateLoadMiss(masm(), Code::LOAD_IC);
2000
2001 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002002 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003}
2004
2005
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002006Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2007 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002008 JSGlobalPropertyCell* cell,
2009 String* name,
2010 bool is_dont_delete) {
2011 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002012 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002013 // -- ecx : name
2014 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002015 // -----------------------------------
2016 Label miss;
2017
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002018 // If the object is the holder then we know that it's a global
2019 // object which can only happen for contextual loads. In this case,
2020 // the receiver cannot be a smi.
2021 if (object != holder) {
2022 __ test(eax, Immediate(kSmiTagMask));
2023 __ j(zero, &miss, not_taken);
2024 }
2025
2026 // Check that the maps haven't changed.
2027 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002028
2029 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002030 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2031 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002032
2033 // Check for deleted property if property can actually be deleted.
2034 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002035 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002036 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002037 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002038 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002039 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002040 }
2041
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002042 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002043 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002044 __ ret(0);
2045
2046 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002047 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2048 GenerateLoadMiss(masm(), Code::LOAD_IC);
2049
2050 // Return the generated code.
2051 return GetCode(NORMAL, name);
2052}
2053
2054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2056 JSObject* receiver,
2057 JSObject* holder,
2058 int index) {
2059 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002060 // -- eax : key
2061 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 Label miss;
2065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 __ IncrementCounter(&Counters::keyed_load_field, 1);
2067
2068 // Check that the name has not changed.
2069 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2070 __ j(not_equal, &miss, not_taken);
2071
ager@chromium.org5c838252010-02-19 08:53:10 +00002072 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002073
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 __ bind(&miss);
2075 __ DecrementCounter(&Counters::keyed_load_field, 1);
2076 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2077
2078 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080}
2081
2082
2083Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2084 JSObject* receiver,
2085 JSObject* holder,
2086 AccessorInfo* callback) {
2087 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002088 // -- eax : key
2089 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 Label miss;
2093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2095
2096 // Check that the name has not changed.
2097 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2098 __ j(not_equal, &miss, not_taken);
2099
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002100 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002101 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002102 callback, name, &miss, &failure);
2103 if (!success) return failure;
2104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 __ bind(&miss);
2106 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2107 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2108
2109 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111}
2112
2113
2114Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2115 JSObject* receiver,
2116 JSObject* holder,
2117 Object* value) {
2118 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002119 // -- eax : key
2120 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 Label miss;
2124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2126
2127 // Check that the name has not changed.
2128 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2129 __ j(not_equal, &miss, not_taken);
2130
ager@chromium.org5c838252010-02-19 08:53:10 +00002131 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002132 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 __ bind(&miss);
2134 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2135 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2136
2137 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139}
2140
2141
2142Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2143 JSObject* holder,
2144 String* name) {
2145 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 // -- eax : key
2147 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 Label miss;
2151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2153
2154 // Check that the name has not changed.
2155 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2156 __ j(not_equal, &miss, not_taken);
2157
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002158 LookupResult lookup;
2159 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002160 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002161 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002162 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002163 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002164 eax,
2165 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002166 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002167 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002168 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 __ bind(&miss);
2170 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2171 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2172
2173 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175}
2176
2177
2178
2179
2180Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2181 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002182 // -- eax : key
2183 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 Label miss;
2187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2189
2190 // Check that the name has not changed.
2191 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2192 __ j(not_equal, &miss, not_taken);
2193
ager@chromium.org5c838252010-02-19 08:53:10 +00002194 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 __ bind(&miss);
2196 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2197 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2198
2199 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201}
2202
2203
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002204Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002206 // -- eax : key
2207 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 Label miss;
2211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2213
2214 // Check that the name has not changed.
2215 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2216 __ j(not_equal, &miss, not_taken);
2217
ager@chromium.org5c838252010-02-19 08:53:10 +00002218 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 __ bind(&miss);
2220 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2221 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2222
2223 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002224 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225}
2226
2227
2228Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2229 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002230 // -- eax : key
2231 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 Label miss;
2235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2237
2238 // Check that the name has not changed.
2239 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2240 __ j(not_equal, &miss, not_taken);
2241
ager@chromium.org5c838252010-02-19 08:53:10 +00002242 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 __ bind(&miss);
2244 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2245 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2246
2247 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249}
2250
2251
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002252// Specialized stub for constructing objects from functions which only have only
2253// simple assignments of the form this.x = ...; in their body.
2254Object* ConstructStubCompiler::CompileConstructStub(
2255 SharedFunctionInfo* shared) {
2256 // ----------- S t a t e -------------
2257 // -- eax : argc
2258 // -- edi : constructor
2259 // -- esp[0] : return address
2260 // -- esp[4] : last argument
2261 // -----------------------------------
2262 Label generic_stub_call;
2263#ifdef ENABLE_DEBUGGER_SUPPORT
2264 // Check to see whether there are any break points in the function code. If
2265 // there are jump to the generic constructor stub which calls the actual
2266 // code for the function thereby hitting the break points.
2267 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2268 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2269 __ cmp(ebx, Factory::undefined_value());
2270 __ j(not_equal, &generic_stub_call, not_taken);
2271#endif
2272
2273 // Load the initial map and verify that it is in fact a map.
2274 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2275 // Will both indicate a NULL and a Smi.
2276 __ test(ebx, Immediate(kSmiTagMask));
2277 __ j(zero, &generic_stub_call);
2278 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2279 __ j(not_equal, &generic_stub_call);
2280
2281#ifdef DEBUG
2282 // Cannot construct functions this way.
2283 // edi: constructor
2284 // ebx: initial map
2285 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2286 __ Assert(not_equal, "Function constructed by construct stub.");
2287#endif
2288
2289 // Now allocate the JSObject on the heap by moving the new space allocation
2290 // top forward.
2291 // edi: constructor
2292 // ebx: initial map
2293 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2294 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002295 __ AllocateInNewSpace(ecx,
2296 edx,
2297 ecx,
2298 no_reg,
2299 &generic_stub_call,
2300 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002301
2302 // Allocated the JSObject, now initialize the fields and add the heap tag.
2303 // ebx: initial map
2304 // edx: JSObject (untagged)
2305 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2306 __ mov(ebx, Factory::empty_fixed_array());
2307 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2308 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2309
2310 // Push the allocated object to the stack. This is the object that will be
2311 // returned (after it is tagged).
2312 __ push(edx);
2313
2314 // eax: argc
2315 // edx: JSObject (untagged)
2316 // Load the address of the first in-object property into edx.
2317 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2318 // Calculate the location of the first argument. The stack contains the
2319 // allocated object and the return address on top of the argc arguments.
2320 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2321
2322 // Use edi for holding undefined which is used in several places below.
2323 __ mov(edi, Factory::undefined_value());
2324
2325 // eax: argc
2326 // ecx: first argument
2327 // edx: first in-object property of the JSObject
2328 // edi: undefined
2329 // Fill the initialized properties with a constant value or a passed argument
2330 // depending on the this.x = ...; assignment in the function.
2331 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2332 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002333 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002334 // If argument is not passed the property is set to undefined,
2335 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002336 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002337 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002338 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002339 if (CpuFeatures::IsSupported(CMOV)) {
2340 CpuFeatures::Scope use_cmov(CMOV);
2341 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2342 } else {
2343 Label not_passed;
2344 __ j(below_equal, &not_passed);
2345 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2346 __ bind(&not_passed);
2347 }
2348 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002349 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002350 } else {
2351 // Set the property to the constant value.
2352 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2353 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2354 }
2355 }
2356
2357 // Fill the unused in-object property fields with undefined.
2358 for (int i = shared->this_property_assignments_count();
2359 i < shared->CalculateInObjectProperties();
2360 i++) {
2361 __ mov(Operand(edx, i * kPointerSize), edi);
2362 }
2363
2364 // Move argc to ebx and retrieve and tag the JSObject to return.
2365 __ mov(ebx, eax);
2366 __ pop(eax);
2367 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2368
2369 // Remove caller arguments and receiver from the stack and return.
2370 __ pop(ecx);
2371 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2372 __ push(ecx);
2373 __ IncrementCounter(&Counters::constructed_objects, 1);
2374 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2375 __ ret(0);
2376
2377 // Jump to the generic stub in case the specialized code cannot handle the
2378 // construction.
2379 __ bind(&generic_stub_call);
2380 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2381 Handle<Code> generic_construct_stub(code);
2382 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2383
2384 // Return the generated code.
2385 return GetCode();
2386}
2387
2388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389#undef __
2390
2391} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002392
2393#endif // V8_TARGET_ARCH_IA32