blob: eb555d705d10a1b5ab48f89e708661bdaa7c11cd [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,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000359 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000360 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) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000379 CompileRegular(masm, receiver, holder, scratch2, interceptor_holder,
380 miss_label);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000381 return;
382 }
383
384 // Note: starting a frame here makes GC aware of pointers pushed below.
385 __ EnterInternalFrame();
386
387 if (lookup->type() == CALLBACKS) {
388 __ push(receiver);
389 }
390 __ push(holder);
391 __ push(name_);
392
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000393 // Invoke an interceptor. Note: map checks from receiver to
394 // interceptor's holder has been compiled before (see a caller
395 // of this method.)
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000396 CompileCallLoadPropertyWithInterceptor(masm,
397 receiver,
398 holder,
399 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000400 interceptor_holder);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000401
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000402 // Check if interceptor provided a value for property. If it's
403 // the case, return immediately.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000404 Label interceptor_failed;
405 __ cmp(eax, Factory::no_interceptor_result_sentinel());
406 __ j(equal, &interceptor_failed);
407 __ LeaveInternalFrame();
408 __ ret(0);
409
410 __ bind(&interceptor_failed);
411 __ pop(name_);
412 __ pop(holder);
413 if (lookup->type() == CALLBACKS) {
414 __ pop(receiver);
415 }
416
417 __ LeaveInternalFrame();
418
419 if (lookup->type() == FIELD) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000420 // We found FIELD property in prototype chain of interceptor's holder.
421 // Check that the maps from interceptor's holder to field's holder
422 // haven't changed...
423 holder = stub_compiler->CheckPrototypes(interceptor_holder, holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000424 lookup->holder(), scratch1,
425 scratch2,
426 name,
427 miss_label);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000428 // ... and retrieve a field from field's holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000429 stub_compiler->GenerateFastPropertyLoad(masm, eax,
430 holder, lookup->holder(),
431 lookup->GetFieldIndex());
432 __ ret(0);
433 } else {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000434 // We found CALLBACKS property in prototype chain of interceptor's
435 // holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000436 ASSERT(lookup->type() == CALLBACKS);
437 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
438 ASSERT(callback != NULL);
439 ASSERT(callback->getter() != NULL);
440
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000441 // Prepare for tail call: push receiver to stack after return address.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000442 Label cleanup;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000443 __ pop(scratch2); // return address
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000444 __ push(receiver);
445 __ push(scratch2);
446
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000447 // Check that the maps from interceptor's holder to callback's holder
448 // haven't changed.
449 holder = stub_compiler->CheckPrototypes(interceptor_holder, holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000450 lookup->holder(), scratch1,
451 scratch2,
452 name,
453 &cleanup);
454
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000455 // Continue tail call preparation: push remaining parameters after
456 // return address.
457 __ pop(scratch2); // return address
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000458 __ push(holder);
459 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
460 __ push(holder);
461 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
462 __ push(name_);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000463 __ push(scratch2); // restore return address
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000464
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000465 // Tail call to runtime.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000466 ExternalReference ref =
467 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000468 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000469
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000470 // Clean up code: we pushed receiver after return address and
471 // need to remove it from there.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000472 __ bind(&cleanup);
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000473 __ pop(scratch1); // return address.
474 __ pop(scratch2); // receiver.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000475 __ push(scratch1);
476 }
477 }
478
479
480 void CompileRegular(MacroAssembler* masm,
481 Register receiver,
482 Register holder,
483 Register scratch,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000484 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000485 Label* miss_label) {
486 __ pop(scratch); // save old return address
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000487 PushInterceptorArguments(masm, receiver, holder, name_, interceptor_holder);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000488 __ push(scratch); // restore old return address
489
490 ExternalReference ref = ExternalReference(
491 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000492 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000493 }
494
495 private:
496 Register name_;
497};
498
499
ager@chromium.org5c838252010-02-19 08:53:10 +0000500// Reserves space for the extra arguments to FastHandleApiCall in the
501// caller's frame.
502//
503// These arguments are set by CheckPrototypes and GenerateFastApiCall.
504static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
505 // ----------- S t a t e -------------
506 // -- esp[0] : return address
507 // -- esp[4] : last argument in the internal frame of the caller
508 // -----------------------------------
509 __ pop(scratch);
510 __ push(Immediate(Smi::FromInt(0)));
511 __ push(Immediate(Smi::FromInt(0)));
512 __ push(Immediate(Smi::FromInt(0)));
513 __ push(Immediate(Smi::FromInt(0)));
514 __ push(scratch);
515}
516
517
518// Undoes the effects of ReserveSpaceForFastApiCall.
519static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
520 // ----------- S t a t e -------------
521 // -- esp[0] : return address
522 // -- esp[4] : last fast api call extra argument
523 // -- ...
524 // -- esp[16] : first fast api call extra argument
525 // -- esp[20] : last argument in the internal frame
526 // -----------------------------------
527 __ pop(scratch);
528 __ add(Operand(esp), Immediate(kPointerSize * 4));
529 __ push(scratch);
530}
531
532
533// Generates call to FastHandleApiCall builtin.
534static void GenerateFastApiCall(MacroAssembler* masm,
535 const CallOptimization& optimization,
536 int argc) {
537 // ----------- S t a t e -------------
538 // -- esp[0] : return address
539 // -- esp[4] : object passing the type check
540 // (last fast api call extra argument,
541 // set by CheckPrototypes)
542 // -- esp[8] : api call data
543 // -- esp[12] : api callback
544 // -- esp[16] : api function
545 // (first fast api call extra argument)
546 // -- esp[20] : last argument
547 // -- ...
548 // -- esp[(argc + 5) * 4] : first argument
549 // -- esp[(argc + 6) * 4] : receiver
550 // -----------------------------------
551
552 // Get the function and setup the context.
553 JSFunction* function = optimization.constant_function();
554 __ mov(edi, Immediate(Handle<JSFunction>(function)));
555 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
556
557 // Pass the additional arguments FastHandleApiCall expects.
558 __ mov(Operand(esp, 4 * kPointerSize), edi);
559 bool info_loaded = false;
560 Object* callback = optimization.api_call_info()->callback();
561 if (Heap::InNewSpace(callback)) {
562 info_loaded = true;
563 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
564 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
565 __ mov(Operand(esp, 3 * kPointerSize), ebx);
566 } else {
567 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
568 }
569 Object* call_data = optimization.api_call_info()->data();
570 if (Heap::InNewSpace(call_data)) {
571 if (!info_loaded) {
572 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
573 }
574 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
575 __ mov(Operand(esp, 2 * kPointerSize), ebx);
576 } else {
577 __ mov(Operand(esp, 2 * kPointerSize),
578 Immediate(Handle<Object>(call_data)));
579 }
580
581 // Set the number of arguments.
582 __ mov(eax, Immediate(argc + 4));
583
584 // Jump to the fast api call builtin (tail call).
585 Handle<Code> code = Handle<Code>(
586 Builtins::builtin(Builtins::FastHandleApiCall));
587 ParameterCount expected(0);
588 __ InvokeCode(code, expected, expected,
589 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
590}
591
592
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000593class CallInterceptorCompiler BASE_EMBEDDED {
594 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000595 CallInterceptorCompiler(StubCompiler* stub_compiler,
596 const ParameterCount& arguments,
597 Register name)
598 : stub_compiler_(stub_compiler),
599 arguments_(arguments),
600 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000601
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 void Compile(MacroAssembler* masm,
603 JSObject* object,
604 JSObject* holder,
605 String* name,
606 LookupResult* lookup,
607 Register receiver,
608 Register scratch1,
609 Register scratch2,
610 Label* miss) {
611 ASSERT(holder->HasNamedInterceptor());
612 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
613
614 // Check that the receiver isn't a smi.
615 __ test(receiver, Immediate(kSmiTagMask));
616 __ j(zero, miss, not_taken);
617
618 CallOptimization optimization(lookup);
619
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000620 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000621 CompileCacheable(masm,
622 object,
623 receiver,
624 scratch1,
625 scratch2,
626 holder,
627 lookup,
628 name,
629 optimization,
630 miss);
631 } else {
632 CompileRegular(masm,
633 object,
634 receiver,
635 scratch1,
636 scratch2,
637 name,
638 holder,
639 miss);
640 }
641 }
642
643 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000644 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000646 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000647 Register scratch1,
648 Register scratch2,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000649 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000650 LookupResult* lookup,
651 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000652 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000653 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000654 ASSERT(optimization.is_constant_call());
655 ASSERT(!lookup->holder()->IsGlobalObject());
656
657 int depth1 = kInvalidProtoDepth;
658 int depth2 = kInvalidProtoDepth;
659 bool can_do_fast_api_call = false;
660 if (optimization.is_simple_api_call() &&
661 !lookup->holder()->IsGlobalObject()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000662 depth1 =
663 optimization.GetPrototypeDepthOfExpectedType(object,
664 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000665 if (depth1 == kInvalidProtoDepth) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000666 depth2 =
667 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
668 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000669 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000670 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
671 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000672 }
673
ager@chromium.org5c838252010-02-19 08:53:10 +0000674 __ IncrementCounter(&Counters::call_const_interceptor, 1);
675
676 if (can_do_fast_api_call) {
677 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
678 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000679 }
680
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000681 // Check that the maps from receiver to interceptor's holder
682 // haven't changed and thus we can invoke interceptor.
ager@chromium.org5c838252010-02-19 08:53:10 +0000683 Label miss_cleanup;
684 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
685 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000686 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000687 scratch1, scratch2, name,
688 depth1, miss);
689
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000690 // Invoke an interceptor and if it provides a value,
691 // branch to |regular_invoke|.
ager@chromium.org5c838252010-02-19 08:53:10 +0000692 Label regular_invoke;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000693 LoadWithInterceptor(masm, receiver, holder, interceptor_holder,
694 &regular_invoke);
ager@chromium.org5c838252010-02-19 08:53:10 +0000695
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000696 // Interceptor returned nothing for this property. Try to use cached
697 // constant function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000698
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000699 // Check that the maps from interceptor's holder to constant function's
700 // holder haven't changed and thus we can use cached constant function.
701 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000702 lookup->holder(),
703 scratch1, scratch2, name,
704 depth2, miss);
705
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000706 // Invoke function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000707 if (can_do_fast_api_call) {
708 GenerateFastApiCall(masm, optimization, arguments_.immediate());
709 } else {
710 __ InvokeFunction(optimization.constant_function(), arguments_,
711 JUMP_FUNCTION);
712 }
713
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000714 // Deferred code for fast API call case---clean preallocated space.
ager@chromium.org5c838252010-02-19 08:53:10 +0000715 if (can_do_fast_api_call) {
716 __ bind(&miss_cleanup);
717 FreeSpaceForFastApiCall(masm, scratch1);
718 __ jmp(miss_label);
719 }
720
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000721 // Invoke a regular function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000722 __ bind(&regular_invoke);
723 if (can_do_fast_api_call) {
724 FreeSpaceForFastApiCall(masm, scratch1);
725 }
726 }
727
728 void CompileRegular(MacroAssembler* masm,
729 JSObject* object,
730 Register receiver,
731 Register scratch1,
732 Register scratch2,
733 String* name,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000734 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000735 Label* miss_label) {
736 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000737 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000738 scratch1, scratch2, name,
739 miss_label);
740
741 __ EnterInternalFrame();
742 // Save the name_ register across the call.
743 __ push(name_);
744
745 PushInterceptorArguments(masm,
746 receiver,
747 holder,
748 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000749 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000750
751 __ CallExternalReference(
752 ExternalReference(
753 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
754 5);
755
756 // Restore the name_ register.
757 __ pop(name_);
758 __ LeaveInternalFrame();
759 }
760
761 void LoadWithInterceptor(MacroAssembler* masm,
762 Register receiver,
763 Register holder,
764 JSObject* holder_obj,
765 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000766 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000767 __ push(holder); // Save the holder.
768 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000769
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000770 CompileCallLoadPropertyWithInterceptor(masm,
771 receiver,
772 holder,
773 name_,
774 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000775
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000776 __ pop(name_); // Restore the name.
777 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000778 __ LeaveInternalFrame();
779
780 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000781 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000782 }
783
ager@chromium.org5c838252010-02-19 08:53:10 +0000784 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000785 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000786 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000787};
788
789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000790void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
791 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
792 Code* code = NULL;
793 if (kind == Code::LOAD_IC) {
794 code = Builtins::builtin(Builtins::LoadIC_Miss);
795 } else {
796 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
797 }
798
799 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000800 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000801}
802
803
ager@chromium.org5c838252010-02-19 08:53:10 +0000804// Both name_reg and receiver_reg are preserved on jumps to miss_label,
805// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000806void StubCompiler::GenerateStoreField(MacroAssembler* masm,
807 JSObject* object,
808 int index,
809 Map* transition,
810 Register receiver_reg,
811 Register name_reg,
812 Register scratch,
813 Label* miss_label) {
814 // Check that the object isn't a smi.
815 __ test(receiver_reg, Immediate(kSmiTagMask));
816 __ j(zero, miss_label, not_taken);
817
818 // Check that the map of the object hasn't changed.
819 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
820 Immediate(Handle<Map>(object->map())));
821 __ j(not_equal, miss_label, not_taken);
822
823 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000824 if (object->IsJSGlobalProxy()) {
825 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000826 }
827
828 // Stub never generated for non-global objects that require access
829 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000830 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000831
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000832 // Perform map transition for the receiver if necessary.
833 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
834 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000835 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000836 __ pop(scratch); // Return address.
837 __ push(receiver_reg);
838 __ push(Immediate(Handle<Map>(transition)));
839 __ push(eax);
840 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000841 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000842 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000843 return;
844 }
845
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000847 // Update the map of the object; no write barrier updating is
848 // needed because the map is never in new space.
849 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
850 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000851 }
852
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000853 // Adjust for the number of properties stored in the object. Even in the
854 // face of a transition we can use the old map here because the size of the
855 // object and the number of in-object properties is not going to change.
856 index -= object->map()->inobject_properties();
857
ager@chromium.org7c537e22008-10-16 08:43:32 +0000858 if (index < 0) {
859 // Set the property straight into the object.
860 int offset = object->map()->instance_size() + (index * kPointerSize);
861 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000862
ager@chromium.org7c537e22008-10-16 08:43:32 +0000863 // Update the write barrier for the array address.
864 // Pass the value being stored in the now unused name_reg.
865 __ mov(name_reg, Operand(eax));
866 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
867 } else {
868 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000869 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000870 // Get the properties array (optimistically).
871 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000872 __ mov(FieldOperand(scratch, offset), eax);
873
874 // Update the write barrier for the array address.
875 // Pass the value being stored in the now unused name_reg.
876 __ mov(name_reg, Operand(eax));
877 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
878 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000879
880 // Return the value (register eax).
881 __ ret(0);
882}
883
884
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000885// Generate code to check that a global property cell is empty. Create
886// the property cell at compilation time if no cell exists for the
887// property.
888static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
889 GlobalObject* global,
890 String* name,
891 Register scratch,
892 Label* miss) {
893 Object* probe = global->EnsurePropertyCell(name);
894 if (probe->IsFailure()) return probe;
895 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
896 ASSERT(cell->value()->IsTheHole());
897 __ mov(scratch, Immediate(Handle<Object>(cell)));
898 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
899 Immediate(Factory::the_hole_value()));
900 __ j(not_equal, miss, not_taken);
901 return cell;
902}
903
904
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000905#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000906#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000907
908
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000909Register StubCompiler::CheckPrototypes(JSObject* object,
910 Register object_reg,
911 JSObject* holder,
912 Register holder_reg,
913 Register scratch,
914 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000915 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000916 Label* miss) {
917 // Check that the maps haven't changed.
918 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000919 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
920 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000921
922 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000923 // that their maps haven't changed. We also need to check that the
924 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000925 while (object != holder) {
926 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000927 Object* cell = GenerateCheckPropertyCell(masm(),
928 GlobalObject::cast(object),
929 name,
930 scratch,
931 miss);
932 if (cell->IsFailure()) {
933 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000934 return result;
935 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000936 }
937 object = JSObject::cast(object->GetPrototype());
938 }
939
ager@chromium.org5c838252010-02-19 08:53:10 +0000940 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000941 return result;
942}
943
944
945void StubCompiler::GenerateLoadField(JSObject* object,
946 JSObject* holder,
947 Register receiver,
948 Register scratch1,
949 Register scratch2,
950 int index,
951 String* name,
952 Label* miss) {
953 // Check that the receiver isn't a smi.
954 __ test(receiver, Immediate(kSmiTagMask));
955 __ j(zero, miss, not_taken);
956
957 // Check the prototype chain.
958 Register reg =
959 CheckPrototypes(object, receiver, holder,
960 scratch1, scratch2, name, miss);
961
962 // Get the value from the properties.
963 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
964 __ ret(0);
965}
966
967
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000968bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000969 JSObject* holder,
970 Register receiver,
971 Register name_reg,
972 Register scratch1,
973 Register scratch2,
974 AccessorInfo* callback,
975 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000976 Label* miss,
977 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000978 // Check that the receiver isn't a smi.
979 __ test(receiver, Immediate(kSmiTagMask));
980 __ j(zero, miss, not_taken);
981
982 // Check that the maps haven't changed.
983 Register reg =
984 CheckPrototypes(object, receiver, holder,
985 scratch1, scratch2, name, miss);
986
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000987 Handle<AccessorInfo> callback_handle(callback);
988
989 Register other = reg.is(scratch1) ? scratch2 : scratch1;
990 __ EnterInternalFrame();
991 __ PushHandleScope(other);
992 // Push the stack address where the list of arguments ends
993 __ mov(other, esp);
994 __ sub(Operand(other), Immediate(2 * kPointerSize));
995 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000996 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000997 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000998 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000999 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001000 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001001 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001002 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001003 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001004 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001005 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001006
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001007 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001008 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001009 Address getter_address = v8::ToCData<Address>(callback->getter());
1010 ApiFunction fun(getter_address);
1011 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001012 // Emitting a stub call may try to allocate (if the code is not
1013 // already generated). Do not allow the assembler to perform a
1014 // garbage collection but instead return the allocation failure
1015 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001016 Object* result = masm()->TryCallStub(&stub);
1017 if (result->IsFailure()) {
1018 *failure = Failure::cast(result);
1019 return false;
1020 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001021
1022 // We need to avoid using eax since that now holds the result.
1023 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001024 // Emitting PopHandleScope may try to allocate. Do not allow the
1025 // assembler to perform a garbage collection but instead return a
1026 // failure object.
1027 result = masm()->TryPopHandleScope(eax, tmp);
1028 if (result->IsFailure()) {
1029 *failure = Failure::cast(result);
1030 return false;
1031 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001032 __ LeaveInternalFrame();
1033
1034 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001035 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001036}
1037
1038
1039void StubCompiler::GenerateLoadConstant(JSObject* object,
1040 JSObject* holder,
1041 Register receiver,
1042 Register scratch1,
1043 Register scratch2,
1044 Object* value,
1045 String* name,
1046 Label* miss) {
1047 // Check that the receiver isn't a smi.
1048 __ test(receiver, Immediate(kSmiTagMask));
1049 __ j(zero, miss, not_taken);
1050
1051 // Check that the maps haven't changed.
1052 Register reg =
1053 CheckPrototypes(object, receiver, holder,
1054 scratch1, scratch2, name, miss);
1055
1056 // Return the constant value.
1057 __ mov(eax, Handle<Object>(value));
1058 __ ret(0);
1059}
1060
1061
1062void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1063 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001064 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001065 Register receiver,
1066 Register name_reg,
1067 Register scratch1,
1068 Register scratch2,
1069 String* name,
1070 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001071 LoadInterceptorCompiler compiler(name_reg);
1072 CompileLoadInterceptor(&compiler,
1073 this,
1074 masm(),
1075 object,
1076 holder,
1077 name,
1078 lookup,
1079 receiver,
1080 scratch1,
1081 scratch2,
1082 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001083}
1084
1085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086// TODO(1241006): Avoid having lazy compile stubs specialized by the
1087// number of arguments. It is not needed anymore.
1088Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001090 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001091
1092 // Push a copy of the function onto the stack.
1093 __ push(edi);
1094
1095 __ push(edi); // function is also the parameter to the runtime call
1096 __ CallRuntime(Runtime::kLazyCompile, 1);
1097 __ pop(edi);
1098
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001099 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001100 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001101
1102 // Do a tail-call of the compiled function.
1103 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1104 __ jmp(Operand(ecx));
1105
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001106 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107}
1108
1109
ager@chromium.org5c838252010-02-19 08:53:10 +00001110Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001111 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001112 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001113 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001115 // -- ecx : name
1116 // -- esp[0] : return address
1117 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1118 // -- ...
1119 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001120 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121 Label miss;
1122
1123 // Get the receiver from the stack.
1124 const int argc = arguments().immediate();
1125 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1126
1127 // Check that the receiver isn't a smi.
1128 __ test(edx, Immediate(kSmiTagMask));
1129 __ j(zero, &miss, not_taken);
1130
1131 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001132 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001133
ager@chromium.org7c537e22008-10-16 08:43:32 +00001134 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001135
1136 // Check that the function really is a function.
1137 __ test(edi, Immediate(kSmiTagMask));
1138 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001139 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 __ j(not_equal, &miss, not_taken);
1141
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001142 // Patch the receiver on the stack with the global proxy if
1143 // necessary.
1144 if (object->IsGlobalObject()) {
1145 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1146 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1147 }
1148
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001149 // Invoke the function.
1150 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1151
1152 // Handle call cache miss.
1153 __ bind(&miss);
1154 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001155 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001156
1157 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001158 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159}
1160
1161
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001162Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1163 JSObject* holder,
1164 JSFunction* function,
1165 String* name,
1166 CheckType check) {
1167 // ----------- S t a t e -------------
1168 // -- ecx : name
1169 // -- esp[0] : return address
1170 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1171 // -- ...
1172 // -- esp[(argc + 1) * 4] : receiver
1173 // -----------------------------------
1174 ASSERT(check == RECEIVER_MAP_CHECK);
1175
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001176 // If object is not an array, bail out to regular call.
1177 if (!object->IsJSArray()) {
1178 return Heap::undefined_value();
1179 }
1180
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001181 Label miss;
1182
1183 // Get the receiver from the stack.
1184 const int argc = arguments().immediate();
1185 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1186
1187 // Check that the receiver isn't a smi.
1188 __ test(edx, Immediate(kSmiTagMask));
1189 __ j(zero, &miss);
1190
1191 CheckPrototypes(JSObject::cast(object), edx,
1192 holder, ebx,
1193 eax, name, &miss);
1194
1195 if (argc == 0) {
1196 // Noop, return the length.
1197 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1198 __ ret((argc + 1) * kPointerSize);
1199 } else {
1200 // Get the elements array of the object.
1201 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1202
1203 // Check that the elements are in fast mode (not dictionary).
1204 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1205 Immediate(Factory::fixed_array_map()));
1206 __ j(not_equal, &miss);
1207
1208 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001209 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001210
1211 // Get the array's length into eax and calculate new length.
1212 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1213 STATIC_ASSERT(kSmiTagSize == 1);
1214 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001215 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001216
1217 // Get the element's length into ecx.
1218 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1219 __ SmiTag(ecx);
1220
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001221 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001222 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001223 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001224
1225 // Save new length.
1226 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1227
1228 // Push the element.
1229 __ lea(edx, FieldOperand(ebx,
1230 eax, times_half_pointer_size,
1231 FixedArray::kHeaderSize - argc * kPointerSize));
1232 __ mov(ecx, Operand(esp, argc * kPointerSize));
1233 __ mov(Operand(edx, 0), ecx);
1234
1235 // Check if value is a smi.
1236 __ test(ecx, Immediate(kSmiTagMask));
1237 __ j(not_zero, &with_rset_update);
1238
1239 __ bind(&exit);
1240 __ ret((argc + 1) * kPointerSize);
1241
1242 __ bind(&with_rset_update);
1243
1244 __ InNewSpace(ebx, ecx, equal, &exit);
1245
1246 RecordWriteStub stub(ebx, edx, ecx);
1247 __ CallStub(&stub);
1248 __ ret((argc + 1) * kPointerSize);
1249
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001250 __ bind(&attempt_to_grow_elements);
1251 ExternalReference new_space_allocation_top =
1252 ExternalReference::new_space_allocation_top_address();
1253 ExternalReference new_space_allocation_limit =
1254 ExternalReference::new_space_allocation_limit_address();
1255
1256 const int kAllocationDelta = 4;
1257 // Load top.
1258 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1259
1260 // Check if it's the end of elements.
1261 __ lea(edx, FieldOperand(ebx,
1262 eax, times_half_pointer_size,
1263 FixedArray::kHeaderSize - argc * kPointerSize));
1264 __ cmp(edx, Operand(ecx));
1265 __ j(not_equal, &call_builtin);
1266 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1267 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001268 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001269
1270 // We fit and could grow elements.
1271 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1272 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001273
1274 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001275 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001276 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001277 for (int i = 1; i < kAllocationDelta; i++) {
1278 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001279 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001280 }
1281
1282 // Restore receiver to edx as finish sequence assumes it's here.
1283 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1284
1285 // Increment element's and array's sizes.
1286 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1287 Immediate(kAllocationDelta));
1288 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1289
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001290 // Elements are in new space, so no remembered set updates are necessary.
1291 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001292
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001293 __ bind(&call_builtin);
1294 }
1295
1296 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1297 argc + 1,
1298 1);
1299 }
1300
1301 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001302 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1303 __ jmp(ic, RelocInfo::CODE_TARGET);
1304
1305 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001306 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001307}
1308
1309
1310Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1311 JSObject* holder,
1312 JSFunction* function,
1313 String* name,
1314 CheckType check) {
1315 // ----------- S t a t e -------------
1316 // -- ecx : name
1317 // -- esp[0] : return address
1318 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1319 // -- ...
1320 // -- esp[(argc + 1) * 4] : receiver
1321 // -----------------------------------
1322 ASSERT(check == RECEIVER_MAP_CHECK);
1323
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001324 // If object is not an array, bail out to regular call.
1325 if (!object->IsJSArray()) {
1326 return Heap::undefined_value();
1327 }
1328
ager@chromium.orgac091b72010-05-05 07:34:42 +00001329 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001330
1331 // Get the receiver from the stack.
1332 const int argc = arguments().immediate();
1333 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1334
1335 // Check that the receiver isn't a smi.
1336 __ test(edx, Immediate(kSmiTagMask));
1337 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001338 CheckPrototypes(JSObject::cast(object), edx,
1339 holder, ebx,
1340 eax, name, &miss);
1341
1342 // Get the elements array of the object.
1343 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1344
1345 // Check that the elements are in fast mode (not dictionary).
1346 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1347 Immediate(Factory::fixed_array_map()));
1348 __ j(not_equal, &miss);
1349
1350 // Get the array's length into ecx and calculate new length.
1351 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1352 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001353 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001354
1355 // Get the last element.
1356 STATIC_ASSERT(kSmiTagSize == 1);
1357 STATIC_ASSERT(kSmiTag == 0);
1358 __ mov(eax, FieldOperand(ebx,
1359 ecx, times_half_pointer_size,
1360 FixedArray::kHeaderSize));
1361 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1362 __ j(equal, &call_builtin);
1363
1364 // Set the array's length.
1365 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1366
1367 // Fill with the hole.
1368 __ mov(FieldOperand(ebx,
1369 ecx, times_half_pointer_size,
1370 FixedArray::kHeaderSize),
1371 Immediate(Factory::the_hole_value()));
1372 __ ret((argc + 1) * kPointerSize);
1373
ager@chromium.orgac091b72010-05-05 07:34:42 +00001374 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001375 __ mov(eax, Immediate(Factory::undefined_value()));
1376 __ ret((argc + 1) * kPointerSize);
1377
1378 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001379 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1380 argc + 1,
1381 1);
1382
1383 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001384 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1385 __ jmp(ic, RelocInfo::CODE_TARGET);
1386
1387 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001388 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001389}
1390
1391
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392Object* CallStubCompiler::CompileCallConstant(Object* object,
1393 JSObject* holder,
1394 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001395 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001396 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001397 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001398 // -- ecx : name
1399 // -- esp[0] : return address
1400 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1401 // -- ...
1402 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001403 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001404
1405 SharedFunctionInfo* function_info = function->shared();
1406 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001407 const int id = function_info->custom_call_generator_id();
1408 Object* result =
1409 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001410 // undefined means bail out to regular compiler.
1411 if (!result->IsUndefined()) {
1412 return result;
1413 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001414 }
1415
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001416 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001417
1418 // Get the receiver from the stack.
1419 const int argc = arguments().immediate();
1420 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1421
1422 // Check that the receiver isn't a smi.
1423 if (check != NUMBER_CHECK) {
1424 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001425 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 }
1427
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001428 // Make sure that it's okay not to patch the on stack receiver
1429 // unless we're doing a receiver map check.
1430 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1431
ager@chromium.org5c838252010-02-19 08:53:10 +00001432 CallOptimization optimization(function);
1433 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001434 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001435
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 switch (check) {
1437 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001438 __ IncrementCounter(&Counters::call_const, 1);
1439
1440 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1441 depth = optimization.GetPrototypeDepthOfExpectedType(
1442 JSObject::cast(object), holder);
1443 }
1444
1445 if (depth != kInvalidProtoDepth) {
1446 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1447 ReserveSpaceForFastApiCall(masm(), eax);
1448 }
1449
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001450 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001451 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001452 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001453
1454 // Patch the receiver on the stack with the global proxy if
1455 // necessary.
1456 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001457 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001458 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1459 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1460 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001461 break;
1462
1463 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001464 if (!function->IsBuiltin()) {
1465 // Calling non-builtins with a value as receiver requires boxing.
1466 __ jmp(&miss);
1467 } else {
1468 // Check that the object is a string or a symbol.
1469 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1470 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1471 __ cmp(eax, FIRST_NONSTRING_TYPE);
1472 __ j(above_equal, &miss, not_taken);
1473 // Check that the maps starting from the prototype haven't changed.
1474 GenerateLoadGlobalFunctionPrototype(masm(),
1475 Context::STRING_FUNCTION_INDEX,
1476 eax);
1477 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1478 ebx, edx, name, &miss);
1479 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 break;
1481
1482 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001483 if (!function->IsBuiltin()) {
1484 // Calling non-builtins with a value as receiver requires boxing.
1485 __ jmp(&miss);
1486 } else {
1487 Label fast;
1488 // Check that the object is a smi or a heap number.
1489 __ test(edx, Immediate(kSmiTagMask));
1490 __ j(zero, &fast, taken);
1491 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1492 __ j(not_equal, &miss, not_taken);
1493 __ bind(&fast);
1494 // Check that the maps starting from the prototype haven't changed.
1495 GenerateLoadGlobalFunctionPrototype(masm(),
1496 Context::NUMBER_FUNCTION_INDEX,
1497 eax);
1498 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1499 ebx, edx, name, &miss);
1500 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 break;
1502 }
1503
1504 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001505 if (!function->IsBuiltin()) {
1506 // Calling non-builtins with a value as receiver requires boxing.
1507 __ jmp(&miss);
1508 } else {
1509 Label fast;
1510 // Check that the object is a boolean.
1511 __ cmp(edx, Factory::true_value());
1512 __ j(equal, &fast, taken);
1513 __ cmp(edx, Factory::false_value());
1514 __ j(not_equal, &miss, not_taken);
1515 __ bind(&fast);
1516 // Check that the maps starting from the prototype haven't changed.
1517 GenerateLoadGlobalFunctionPrototype(masm(),
1518 Context::BOOLEAN_FUNCTION_INDEX,
1519 eax);
1520 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1521 ebx, edx, name, &miss);
1522 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 break;
1524 }
1525
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526 default:
1527 UNREACHABLE();
1528 }
1529
ager@chromium.org5c838252010-02-19 08:53:10 +00001530 if (depth != kInvalidProtoDepth) {
1531 GenerateFastApiCall(masm(), optimization, argc);
1532 } else {
1533 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1534 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535
1536 // Handle call cache miss.
1537 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001538 if (depth != kInvalidProtoDepth) {
1539 FreeSpaceForFastApiCall(masm(), eax);
1540 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001541 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001543 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544
1545 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001546 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547}
1548
1549
ager@chromium.org5c838252010-02-19 08:53:10 +00001550Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001551 JSObject* holder,
1552 String* name) {
1553 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001554 // -- ecx : name
1555 // -- esp[0] : return address
1556 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1557 // -- ...
1558 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 Label miss;
1561
1562 // Get the number of arguments.
1563 const int argc = arguments().immediate();
1564
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001565 LookupResult lookup;
1566 LookupPostInterceptor(holder, name, &lookup);
1567
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 // Get the receiver from the stack.
1569 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001570
ager@chromium.org5c838252010-02-19 08:53:10 +00001571 CallInterceptorCompiler compiler(this, arguments(), ecx);
1572 compiler.Compile(masm(),
1573 object,
1574 holder,
1575 name,
1576 &lookup,
1577 edx,
1578 ebx,
1579 edi,
1580 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001582 // Restore receiver.
1583 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584
1585 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001586 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001587 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001588 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 __ j(not_equal, &miss, not_taken);
1590
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001591 // Patch the receiver on the stack with the global proxy if
1592 // necessary.
1593 if (object->IsGlobalObject()) {
1594 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1595 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1596 }
1597
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001599 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1601
1602 // Handle load cache miss.
1603 __ bind(&miss);
1604 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001605 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606
1607 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001608 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609}
1610
1611
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001612Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1613 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001614 JSGlobalPropertyCell* cell,
1615 JSFunction* function,
1616 String* name) {
1617 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001618 // -- ecx : name
1619 // -- esp[0] : return address
1620 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1621 // -- ...
1622 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001623 // -----------------------------------
1624 Label miss;
1625
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001626 // Get the number of arguments.
1627 const int argc = arguments().immediate();
1628
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001629 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001630 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001631
1632 // If the object is the holder then we know that it's a global
1633 // object which can only happen for contextual calls. In this case,
1634 // the receiver cannot be a smi.
1635 if (object != holder) {
1636 __ test(edx, Immediate(kSmiTagMask));
1637 __ j(zero, &miss, not_taken);
1638 }
1639
1640 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001641 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001642
1643 // Get the value from the cell.
1644 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1645 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1646
1647 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001648 if (Heap::InNewSpace(function)) {
1649 // We can't embed a pointer to a function in new space so we have
1650 // to verify that the shared function info is unchanged. This has
1651 // the nice side effect that multiple closures based on the same
1652 // function can all use this call IC. Before we load through the
1653 // function, we have to verify that it still is a function.
1654 __ test(edi, Immediate(kSmiTagMask));
1655 __ j(zero, &miss, not_taken);
1656 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1657 __ j(not_equal, &miss, not_taken);
1658
1659 // Check the shared function info. Make sure it hasn't changed.
1660 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1661 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1662 __ j(not_equal, &miss, not_taken);
1663 } else {
1664 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1665 __ j(not_equal, &miss, not_taken);
1666 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001667
1668 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001669 if (object->IsGlobalObject()) {
1670 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1671 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1672 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001673
1674 // Setup the context (function already in edi).
1675 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1676
1677 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001678 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001679 ASSERT(function->is_compiled());
1680 Handle<Code> code(function->code());
1681 ParameterCount expected(function->shared()->formal_parameter_count());
1682 __ InvokeCode(code, expected, arguments(),
1683 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1684
1685 // Handle call cache miss.
1686 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001687 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1688 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1689 __ jmp(ic, RelocInfo::CODE_TARGET);
1690
1691 // Return the generated code.
1692 return GetCode(NORMAL, name);
1693}
1694
1695
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001696Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1697 int index,
1698 Map* transition,
1699 String* name) {
1700 // ----------- S t a t e -------------
1701 // -- eax : value
1702 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001703 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 Label miss;
1707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001709 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001710 object,
1711 index,
1712 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001713 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001714 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715
1716 // Handle store cache miss.
1717 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001718 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001720 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721
1722 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001723 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724}
1725
1726
1727Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1728 AccessorInfo* callback,
1729 String* name) {
1730 // ----------- S t a t e -------------
1731 // -- eax : value
1732 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001733 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001736 Label miss;
1737
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001738 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001739 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 __ j(zero, &miss, not_taken);
1741
1742 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001743 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 Immediate(Handle<Map>(object->map())));
1745 __ j(not_equal, &miss, not_taken);
1746
1747 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001748 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001749 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 }
1751
1752 // Stub never generated for non-global objects that require access
1753 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001754 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755
1756 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001757 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1759 __ push(ecx); // name
1760 __ push(eax); // value
1761 __ push(ebx); // restore return address
1762
mads.s.ager31e71382008-08-13 09:32:07 +00001763 // Do tail-call to the runtime system.
1764 ExternalReference store_callback_property =
1765 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001766 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767
1768 // Handle store cache miss.
1769 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001771 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772
1773 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001774 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775}
1776
1777
1778Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1779 String* name) {
1780 // ----------- S t a t e -------------
1781 // -- eax : value
1782 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001783 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 Label miss;
1787
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001789 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 __ j(zero, &miss, not_taken);
1791
1792 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001793 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 Immediate(Handle<Map>(receiver->map())));
1795 __ j(not_equal, &miss, not_taken);
1796
1797 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001798 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001799 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 }
1801
1802 // Stub never generated for non-global objects that require access
1803 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001804 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805
1806 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001807 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 __ push(ecx); // name
1809 __ push(eax); // value
1810 __ push(ebx); // restore return address
1811
mads.s.ager31e71382008-08-13 09:32:07 +00001812 // Do tail-call to the runtime system.
1813 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001815 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816
1817 // Handle store cache miss.
1818 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001820 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821
1822 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001823 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824}
1825
1826
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001827Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1828 JSGlobalPropertyCell* cell,
1829 String* name) {
1830 // ----------- S t a t e -------------
1831 // -- eax : value
1832 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001833 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001834 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001835 // -----------------------------------
1836 Label miss;
1837
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001838 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001839 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001840 Immediate(Handle<Map>(object->map())));
1841 __ j(not_equal, &miss, not_taken);
1842
1843 // Store the value in the cell.
1844 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1845 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1846
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001847 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001848 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001849 __ ret(0);
1850
1851 // Handle store cache miss.
1852 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001853 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1854 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1855 __ jmp(ic, RelocInfo::CODE_TARGET);
1856
1857 // Return the generated code.
1858 return GetCode(NORMAL, name);
1859}
1860
1861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1863 int index,
1864 Map* transition,
1865 String* name) {
1866 // ----------- S t a t e -------------
1867 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001868 // -- ecx : key
1869 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 Label miss;
1873
1874 __ IncrementCounter(&Counters::keyed_store_field, 1);
1875
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 // Check that the name has not changed.
1877 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1878 __ j(not_equal, &miss, not_taken);
1879
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001881 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001882 object,
1883 index,
1884 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001885 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001886 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887
1888 // Handle store cache miss.
1889 __ bind(&miss);
1890 __ DecrementCounter(&Counters::keyed_store_field, 1);
1891 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001892 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893
1894 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001895 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896}
1897
1898
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001899Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1900 JSObject* object,
1901 JSObject* last) {
1902 // ----------- S t a t e -------------
1903 // -- eax : receiver
1904 // -- ecx : name
1905 // -- esp[0] : return address
1906 // -----------------------------------
1907 Label miss;
1908
1909 // Check that the receiver isn't a smi.
1910 __ test(eax, Immediate(kSmiTagMask));
1911 __ j(zero, &miss, not_taken);
1912
1913 // Check the maps of the full prototype chain. Also check that
1914 // global property cells up to (but not including) the last object
1915 // in the prototype chain are empty.
1916 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1917
1918 // If the last object in the prototype chain is a global object,
1919 // check that the global property cell is empty.
1920 if (last->IsGlobalObject()) {
1921 Object* cell = GenerateCheckPropertyCell(masm(),
1922 GlobalObject::cast(last),
1923 name,
1924 edx,
1925 &miss);
1926 if (cell->IsFailure()) return cell;
1927 }
1928
1929 // Return undefined if maps of the full prototype chain are still the
1930 // same and no global property with this name contains a value.
1931 __ mov(eax, Factory::undefined_value());
1932 __ ret(0);
1933
1934 __ bind(&miss);
1935 GenerateLoadMiss(masm(), Code::LOAD_IC);
1936
1937 // Return the generated code.
1938 return GetCode(NONEXISTENT, Heap::empty_string());
1939}
1940
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001941
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1943 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001944 int index,
1945 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001947 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 // -- ecx : name
1949 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951 Label miss;
1952
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001953 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 __ bind(&miss);
1955 GenerateLoadMiss(masm(), Code::LOAD_IC);
1956
1957 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001958 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959}
1960
1961
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001962Object* LoadStubCompiler::CompileLoadCallback(String* name,
1963 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001965 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001967 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968 // -- ecx : name
1969 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 Label miss;
1972
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001973 Failure* failure = Failure::InternalError();
1974 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1975 callback, name, &miss, &failure);
1976 if (!success) return failure;
1977
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 __ bind(&miss);
1979 GenerateLoadMiss(masm(), Code::LOAD_IC);
1980
1981 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001982 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001983}
1984
1985
1986Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1987 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001988 Object* value,
1989 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001991 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 // -- ecx : name
1993 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001994 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001995 Label miss;
1996
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001997 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
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(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003}
2004
2005
2006Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2007 JSObject* holder,
2008 String* name) {
2009 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002010 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 // -- ecx : name
2012 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002014 Label miss;
2015
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002016 LookupResult lookup;
2017 LookupPostInterceptor(holder, name, &lookup);
2018
ager@chromium.orge2902be2009-06-08 12:21:35 +00002019 // TODO(368): Compile in the whole chain: all the interceptors in
2020 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002021 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002022 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002023 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002024 eax,
2025 ecx,
2026 edx,
2027 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002028 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002029 &miss);
2030
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 __ bind(&miss);
2032 GenerateLoadMiss(masm(), Code::LOAD_IC);
2033
2034 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002035 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036}
2037
2038
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002039Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2040 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002041 JSGlobalPropertyCell* cell,
2042 String* name,
2043 bool is_dont_delete) {
2044 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002045 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002046 // -- ecx : name
2047 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002048 // -----------------------------------
2049 Label miss;
2050
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002051 // If the object is the holder then we know that it's a global
2052 // object which can only happen for contextual loads. In this case,
2053 // the receiver cannot be a smi.
2054 if (object != holder) {
2055 __ test(eax, Immediate(kSmiTagMask));
2056 __ j(zero, &miss, not_taken);
2057 }
2058
2059 // Check that the maps haven't changed.
2060 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002061
2062 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002063 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2064 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002065
2066 // Check for deleted property if property can actually be deleted.
2067 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002068 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002069 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002070 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002071 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002072 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002073 }
2074
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002075 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002076 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002077 __ ret(0);
2078
2079 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002080 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2081 GenerateLoadMiss(masm(), Code::LOAD_IC);
2082
2083 // Return the generated code.
2084 return GetCode(NORMAL, name);
2085}
2086
2087
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2089 JSObject* receiver,
2090 JSObject* holder,
2091 int index) {
2092 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002093 // -- eax : key
2094 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 Label miss;
2098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 __ IncrementCounter(&Counters::keyed_load_field, 1);
2100
2101 // Check that the name has not changed.
2102 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2103 __ j(not_equal, &miss, not_taken);
2104
ager@chromium.org5c838252010-02-19 08:53:10 +00002105 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002106
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 __ bind(&miss);
2108 __ DecrementCounter(&Counters::keyed_load_field, 1);
2109 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2110
2111 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002112 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113}
2114
2115
2116Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2117 JSObject* receiver,
2118 JSObject* holder,
2119 AccessorInfo* callback) {
2120 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002121 // -- eax : key
2122 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 Label miss;
2126
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2128
2129 // Check that the name has not changed.
2130 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2131 __ j(not_equal, &miss, not_taken);
2132
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002133 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002134 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002135 callback, name, &miss, &failure);
2136 if (!success) return failure;
2137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002138 __ bind(&miss);
2139 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2140 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2141
2142 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002143 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002144}
2145
2146
2147Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2148 JSObject* receiver,
2149 JSObject* holder,
2150 Object* value) {
2151 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002152 // -- eax : key
2153 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002155 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002156 Label miss;
2157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2159
2160 // Check that the name has not changed.
2161 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2162 __ j(not_equal, &miss, not_taken);
2163
ager@chromium.org5c838252010-02-19 08:53:10 +00002164 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002165 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 __ bind(&miss);
2167 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2168 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2169
2170 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002171 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172}
2173
2174
2175Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2176 JSObject* holder,
2177 String* name) {
2178 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002179 // -- eax : key
2180 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 Label miss;
2184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2186
2187 // Check that the name has not changed.
2188 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2189 __ j(not_equal, &miss, not_taken);
2190
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002191 LookupResult lookup;
2192 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002193 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002194 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002195 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002196 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002197 eax,
2198 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002199 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002200 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002201 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 __ bind(&miss);
2203 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2204 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2205
2206 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002207 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208}
2209
2210
2211
2212
2213Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2214 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002215 // -- eax : key
2216 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 Label miss;
2220
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2222
2223 // Check that the name has not changed.
2224 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2225 __ j(not_equal, &miss, not_taken);
2226
ager@chromium.org5c838252010-02-19 08:53:10 +00002227 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 __ bind(&miss);
2229 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2230 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2231
2232 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002233 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234}
2235
2236
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002237Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002239 // -- eax : key
2240 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 Label miss;
2244
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2246
2247 // Check that the name has not changed.
2248 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2249 __ j(not_equal, &miss, not_taken);
2250
ager@chromium.org5c838252010-02-19 08:53:10 +00002251 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 __ bind(&miss);
2253 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2254 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2255
2256 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002258}
2259
2260
2261Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2262 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002263 // -- eax : key
2264 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 Label miss;
2268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2270
2271 // Check that the name has not changed.
2272 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2273 __ j(not_equal, &miss, not_taken);
2274
ager@chromium.org5c838252010-02-19 08:53:10 +00002275 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 __ bind(&miss);
2277 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2278 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2279
2280 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002281 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282}
2283
2284
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002285// Specialized stub for constructing objects from functions which only have only
2286// simple assignments of the form this.x = ...; in their body.
2287Object* ConstructStubCompiler::CompileConstructStub(
2288 SharedFunctionInfo* shared) {
2289 // ----------- S t a t e -------------
2290 // -- eax : argc
2291 // -- edi : constructor
2292 // -- esp[0] : return address
2293 // -- esp[4] : last argument
2294 // -----------------------------------
2295 Label generic_stub_call;
2296#ifdef ENABLE_DEBUGGER_SUPPORT
2297 // Check to see whether there are any break points in the function code. If
2298 // there are jump to the generic constructor stub which calls the actual
2299 // code for the function thereby hitting the break points.
2300 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2301 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2302 __ cmp(ebx, Factory::undefined_value());
2303 __ j(not_equal, &generic_stub_call, not_taken);
2304#endif
2305
2306 // Load the initial map and verify that it is in fact a map.
2307 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2308 // Will both indicate a NULL and a Smi.
2309 __ test(ebx, Immediate(kSmiTagMask));
2310 __ j(zero, &generic_stub_call);
2311 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2312 __ j(not_equal, &generic_stub_call);
2313
2314#ifdef DEBUG
2315 // Cannot construct functions this way.
2316 // edi: constructor
2317 // ebx: initial map
2318 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2319 __ Assert(not_equal, "Function constructed by construct stub.");
2320#endif
2321
2322 // Now allocate the JSObject on the heap by moving the new space allocation
2323 // top forward.
2324 // edi: constructor
2325 // ebx: initial map
2326 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2327 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002328 __ AllocateInNewSpace(ecx,
2329 edx,
2330 ecx,
2331 no_reg,
2332 &generic_stub_call,
2333 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002334
2335 // Allocated the JSObject, now initialize the fields and add the heap tag.
2336 // ebx: initial map
2337 // edx: JSObject (untagged)
2338 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2339 __ mov(ebx, Factory::empty_fixed_array());
2340 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2341 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2342
2343 // Push the allocated object to the stack. This is the object that will be
2344 // returned (after it is tagged).
2345 __ push(edx);
2346
2347 // eax: argc
2348 // edx: JSObject (untagged)
2349 // Load the address of the first in-object property into edx.
2350 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2351 // Calculate the location of the first argument. The stack contains the
2352 // allocated object and the return address on top of the argc arguments.
2353 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2354
2355 // Use edi for holding undefined which is used in several places below.
2356 __ mov(edi, Factory::undefined_value());
2357
2358 // eax: argc
2359 // ecx: first argument
2360 // edx: first in-object property of the JSObject
2361 // edi: undefined
2362 // Fill the initialized properties with a constant value or a passed argument
2363 // depending on the this.x = ...; assignment in the function.
2364 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2365 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002366 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002367 // If argument is not passed the property is set to undefined,
2368 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002369 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002370 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002371 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002372 if (CpuFeatures::IsSupported(CMOV)) {
2373 CpuFeatures::Scope use_cmov(CMOV);
2374 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2375 } else {
2376 Label not_passed;
2377 __ j(below_equal, &not_passed);
2378 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2379 __ bind(&not_passed);
2380 }
2381 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002382 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002383 } else {
2384 // Set the property to the constant value.
2385 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2386 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2387 }
2388 }
2389
2390 // Fill the unused in-object property fields with undefined.
2391 for (int i = shared->this_property_assignments_count();
2392 i < shared->CalculateInObjectProperties();
2393 i++) {
2394 __ mov(Operand(edx, i * kPointerSize), edi);
2395 }
2396
2397 // Move argc to ebx and retrieve and tag the JSObject to return.
2398 __ mov(ebx, eax);
2399 __ pop(eax);
2400 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2401
2402 // Remove caller arguments and receiver from the stack and return.
2403 __ pop(ecx);
2404 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2405 __ push(ecx);
2406 __ IncrementCounter(&Counters::constructed_objects, 1);
2407 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2408 __ ret(0);
2409
2410 // Jump to the generic stub in case the specialized code cannot handle the
2411 // construction.
2412 __ bind(&generic_stub_call);
2413 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2414 Handle<Code> generic_construct_stub(code);
2415 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2416
2417 // Return the generated code.
2418 return GetCode();
2419}
2420
2421
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002422#undef __
2423
2424} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002425
2426#endif // V8_TARGET_ARCH_IA32