blob: 7fc3f8114d16b948ff23147b0abb0df55a92c616 [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
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000104// Helper function used to check that the dictionary doesn't contain
105// the property. This function may return false negatives, so miss_label
106// must always call a backup property check that is complete.
107// This function is safe to call if the receiver has fast properties.
108// Name must be a symbol and receiver must be a heap object.
109static void GenerateDictionaryNegativeLookup(MacroAssembler* masm,
110 Label* miss_label,
111 Register receiver,
112 String* name,
113 Register r0,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000114 Register r1) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000115 ASSERT(name->IsSymbol());
116 __ IncrementCounter(&Counters::negative_lookups, 1);
117 __ IncrementCounter(&Counters::negative_lookups_miss, 1);
118
119 Label done;
120 __ mov(r0, FieldOperand(receiver, HeapObject::kMapOffset));
121
122 const int kInterceptorOrAccessCheckNeededMask =
123 (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000124
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000125 // Bail out if the receiver has a named interceptor or requires access checks.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000126 __ test_b(FieldOperand(r0, Map::kBitFieldOffset),
127 kInterceptorOrAccessCheckNeededMask);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000128 __ j(not_zero, miss_label, not_taken);
129
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000130 // Check that receiver is a JSObject.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000131 __ CmpInstanceType(r0, FIRST_JS_OBJECT_TYPE);
132 __ j(below, miss_label, not_taken);
133
134 // Load properties array.
135 Register properties = r0;
136 __ mov(properties, FieldOperand(receiver, JSObject::kPropertiesOffset));
137
138 // Check that the properties array is a dictionary.
139 __ cmp(FieldOperand(properties, HeapObject::kMapOffset),
140 Immediate(Factory::hash_table_map()));
141 __ j(not_equal, miss_label);
142
143 // Compute the capacity mask.
144 const int kCapacityOffset =
145 StringDictionary::kHeaderSize +
146 StringDictionary::kCapacityIndex * kPointerSize;
147
148 // Generate an unrolled loop that performs a few probes before
149 // giving up.
150 static const int kProbes = 4;
151 const int kElementsStartOffset =
152 StringDictionary::kHeaderSize +
153 StringDictionary::kElementsStartIndex * kPointerSize;
154
155 // If names of slots in range from 1 to kProbes - 1 for the hash value are
156 // not equal to the name and kProbes-th slot is not used (its name is the
157 // undefined value), it guarantees the hash table doesn't contain the
158 // property. It's true even if some slots represent deleted properties
159 // (their names are the null value).
160 for (int i = 0; i < kProbes; i++) {
161 // r0 points to properties hash.
162 // Compute the masked index: (hash + i + i * i) & mask.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000163 Register index = r1;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000164 // Capacity is smi 2^n.
165 __ mov(index, FieldOperand(properties, kCapacityOffset));
166 __ dec(index);
167 __ and_(Operand(index),
168 Immediate(Smi::FromInt(name->Hash() +
169 StringDictionary::GetProbeOffset(i))));
170
171 // Scale the index by multiplying by the entry size.
172 ASSERT(StringDictionary::kEntrySize == 3);
173 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
174
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000175 Register entity_name = r1;
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000176 // Having undefined at this place means the name is not contained.
177 ASSERT_EQ(kSmiTagSize, 1);
178 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
179 kElementsStartOffset - kHeapObjectTag));
180 __ cmp(entity_name, Factory::undefined_value());
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000181 if (i != kProbes - 1) {
182 __ j(equal, &done, taken);
183
184 // Stop if found the property.
185 __ cmp(entity_name, Handle<String>(name));
186 __ j(equal, miss_label, not_taken);
ager@chromium.orgb5737492010-07-15 09:29:43 +0000187
188 // Check if the entry name is not a symbol.
189 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
190 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
191 kIsSymbolMask);
192 __ j(zero, miss_label, not_taken);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000193 } else {
194 // Give up probing if still not found the undefined value.
195 __ j(not_equal, miss_label, not_taken);
196 }
197 }
198
199 __ bind(&done);
200 __ DecrementCounter(&Counters::negative_lookups_miss, 1);
201}
202
203
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000204void StubCache::GenerateProbe(MacroAssembler* masm,
205 Code::Flags flags,
206 Register receiver,
207 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000208 Register scratch,
209 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210 Label miss;
211
212 // Make sure that code is valid. The shifting code relies on the
213 // entry size being 8.
214 ASSERT(sizeof(Entry) == 8);
215
216 // Make sure the flags does not name a specific type.
217 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
218
219 // Make sure that there are no register conflicts.
220 ASSERT(!scratch.is(receiver));
221 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000222 ASSERT(!extra.is(receiver));
223 ASSERT(!extra.is(name));
224 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225
226 // Check that the receiver isn't a smi.
227 __ test(receiver, Immediate(kSmiTagMask));
228 __ j(zero, &miss, not_taken);
229
230 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000231 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000232 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000233 __ xor_(scratch, flags);
234 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
235
236 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000237 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000238
239 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000240 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000241 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
242 __ xor_(scratch, flags);
243 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 __ sub(scratch, Operand(name));
245 __ add(Operand(scratch), Immediate(flags));
246 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
247
248 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000249 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250
251 // Cache miss: Fall-through and let caller handle the miss by
252 // entering the runtime system.
253 __ bind(&miss);
254}
255
256
257void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
258 int index,
259 Register prototype) {
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000260 __ LoadGlobalFunction(index, prototype);
261 __ LoadGlobalFunctionInitialMap(prototype, prototype);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 // Load the prototype from the initial map.
263 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
264}
265
266
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000267void StubCompiler::GenerateDirectLoadGlobalFunctionPrototype(
268 MacroAssembler* masm, int index, Register prototype) {
269 // Get the global function with the given index.
270 JSFunction* function = JSFunction::cast(Top::global_context()->get(index));
271 // Load its initial map. The global functions all have initial maps.
272 __ Set(prototype, Immediate(Handle<Map>(function->initial_map())));
273 // Load the prototype from the initial map.
274 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
275}
276
277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000278void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
279 Register receiver,
280 Register scratch,
281 Label* miss_label) {
282 // Check that the receiver isn't a smi.
283 __ test(receiver, Immediate(kSmiTagMask));
284 __ j(zero, miss_label, not_taken);
285
286 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000287 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000288 __ j(not_equal, miss_label, not_taken);
289
290 // Load length directly from the JS array.
291 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
292 __ ret(0);
293}
294
295
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000296// Generate code to check if an object is a string. If the object is
297// a string, the map's instance type is left in the scratch register.
298static void GenerateStringCheck(MacroAssembler* masm,
299 Register receiver,
300 Register scratch,
301 Label* smi,
302 Label* non_string_object) {
303 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000304 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000305 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000306
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000307 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000308 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
309 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000310 ASSERT(kNotStringTag != 0);
311 __ test(scratch, Immediate(kNotStringTag));
312 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000313}
314
315
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000316void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
317 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000318 Register scratch1,
319 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000320 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000321 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000322
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000323 // Check if the object is a string leaving the instance type in the
324 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000325 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000326
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000327 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000328 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000329 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000330
331 // Check if the object is a JSValue wrapper.
332 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000334 __ j(not_equal, miss, not_taken);
335
336 // Check if the wrapped value is a string and load the length
337 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000338 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
339 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
340 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000341 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000342}
343
344
345void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
346 Register receiver,
347 Register scratch1,
348 Register scratch2,
349 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000350 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000351 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000352 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000353}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000354
ager@chromium.org7c537e22008-10-16 08:43:32 +0000355
356// Load a fast property out of a holder object (src). In-object properties
357// are loaded directly otherwise the property is loaded from the properties
358// fixed array.
359void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000360 Register dst, Register src,
361 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000362 // Adjust for the number of properties stored in the holder.
363 index -= holder->map()->inobject_properties();
364 if (index < 0) {
365 // Get the property straight out of the holder.
366 int offset = holder->map()->instance_size() + (index * kPointerSize);
367 __ mov(dst, FieldOperand(src, offset));
368 } else {
369 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000370 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000371 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
372 __ mov(dst, FieldOperand(dst, offset));
373 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000374}
375
376
ager@chromium.org5c838252010-02-19 08:53:10 +0000377static void PushInterceptorArguments(MacroAssembler* masm,
378 Register receiver,
379 Register holder,
380 Register name,
381 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000382 __ push(name);
383 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
384 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000385 Register scratch = name;
386 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
387 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000388 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000389 __ push(holder);
390 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000391}
392
393
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000394static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
395 Register receiver,
396 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000397 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000398 JSObject* holder_obj) {
399 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000400 __ CallExternalReference(
401 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
402 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000403}
404
405
ager@chromium.org5c838252010-02-19 08:53:10 +0000406// Reserves space for the extra arguments to FastHandleApiCall in the
407// caller's frame.
408//
409// These arguments are set by CheckPrototypes and GenerateFastApiCall.
410static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
411 // ----------- S t a t e -------------
412 // -- esp[0] : return address
413 // -- esp[4] : last argument in the internal frame of the caller
414 // -----------------------------------
415 __ pop(scratch);
416 __ push(Immediate(Smi::FromInt(0)));
417 __ push(Immediate(Smi::FromInt(0)));
418 __ push(Immediate(Smi::FromInt(0)));
419 __ push(Immediate(Smi::FromInt(0)));
420 __ push(scratch);
421}
422
423
424// Undoes the effects of ReserveSpaceForFastApiCall.
425static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
426 // ----------- S t a t e -------------
427 // -- esp[0] : return address
428 // -- esp[4] : last fast api call extra argument
429 // -- ...
430 // -- esp[16] : first fast api call extra argument
431 // -- esp[20] : last argument in the internal frame
432 // -----------------------------------
433 __ pop(scratch);
434 __ add(Operand(esp), Immediate(kPointerSize * 4));
435 __ push(scratch);
436}
437
438
439// Generates call to FastHandleApiCall builtin.
440static void GenerateFastApiCall(MacroAssembler* masm,
441 const CallOptimization& optimization,
442 int argc) {
443 // ----------- S t a t e -------------
444 // -- esp[0] : return address
445 // -- esp[4] : object passing the type check
446 // (last fast api call extra argument,
447 // set by CheckPrototypes)
448 // -- esp[8] : api call data
449 // -- esp[12] : api callback
450 // -- esp[16] : api function
451 // (first fast api call extra argument)
452 // -- esp[20] : last argument
453 // -- ...
454 // -- esp[(argc + 5) * 4] : first argument
455 // -- esp[(argc + 6) * 4] : receiver
456 // -----------------------------------
457
458 // Get the function and setup the context.
459 JSFunction* function = optimization.constant_function();
460 __ mov(edi, Immediate(Handle<JSFunction>(function)));
461 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
462
463 // Pass the additional arguments FastHandleApiCall expects.
464 __ mov(Operand(esp, 4 * kPointerSize), edi);
465 bool info_loaded = false;
466 Object* callback = optimization.api_call_info()->callback();
467 if (Heap::InNewSpace(callback)) {
468 info_loaded = true;
469 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
470 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
471 __ mov(Operand(esp, 3 * kPointerSize), ebx);
472 } else {
473 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
474 }
475 Object* call_data = optimization.api_call_info()->data();
476 if (Heap::InNewSpace(call_data)) {
477 if (!info_loaded) {
478 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
479 }
480 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
481 __ mov(Operand(esp, 2 * kPointerSize), ebx);
482 } else {
483 __ mov(Operand(esp, 2 * kPointerSize),
484 Immediate(Handle<Object>(call_data)));
485 }
486
487 // Set the number of arguments.
488 __ mov(eax, Immediate(argc + 4));
489
490 // Jump to the fast api call builtin (tail call).
491 Handle<Code> code = Handle<Code>(
492 Builtins::builtin(Builtins::FastHandleApiCall));
493 ParameterCount expected(0);
494 __ InvokeCode(code, expected, expected,
495 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
496}
497
498
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000499class CallInterceptorCompiler BASE_EMBEDDED {
500 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000501 CallInterceptorCompiler(StubCompiler* stub_compiler,
502 const ParameterCount& arguments,
503 Register name)
504 : stub_compiler_(stub_compiler),
505 arguments_(arguments),
506 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000507
ager@chromium.org5c838252010-02-19 08:53:10 +0000508 void Compile(MacroAssembler* masm,
509 JSObject* object,
510 JSObject* holder,
511 String* name,
512 LookupResult* lookup,
513 Register receiver,
514 Register scratch1,
515 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000516 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000517 Label* miss) {
518 ASSERT(holder->HasNamedInterceptor());
519 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
520
521 // Check that the receiver isn't a smi.
522 __ test(receiver, Immediate(kSmiTagMask));
523 __ j(zero, miss, not_taken);
524
525 CallOptimization optimization(lookup);
526
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000527 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000528 CompileCacheable(masm,
529 object,
530 receiver,
531 scratch1,
532 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000533 scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000534 holder,
535 lookup,
536 name,
537 optimization,
538 miss);
539 } else {
540 CompileRegular(masm,
541 object,
542 receiver,
543 scratch1,
544 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000545 scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000546 name,
547 holder,
548 miss);
549 }
550 }
551
552 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000553 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000554 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000555 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000556 Register scratch1,
557 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000558 Register scratch3,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000559 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000560 LookupResult* lookup,
561 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000562 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000563 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000564 ASSERT(optimization.is_constant_call());
565 ASSERT(!lookup->holder()->IsGlobalObject());
566
567 int depth1 = kInvalidProtoDepth;
568 int depth2 = kInvalidProtoDepth;
569 bool can_do_fast_api_call = false;
570 if (optimization.is_simple_api_call() &&
571 !lookup->holder()->IsGlobalObject()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000572 depth1 =
573 optimization.GetPrototypeDepthOfExpectedType(object,
574 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000575 if (depth1 == kInvalidProtoDepth) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000576 depth2 =
577 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
578 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000579 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000580 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
581 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000582 }
583
ager@chromium.org5c838252010-02-19 08:53:10 +0000584 __ IncrementCounter(&Counters::call_const_interceptor, 1);
585
586 if (can_do_fast_api_call) {
587 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
588 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000589 }
590
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000591 // Check that the maps from receiver to interceptor's holder
592 // haven't changed and thus we can invoke interceptor.
ager@chromium.org5c838252010-02-19 08:53:10 +0000593 Label miss_cleanup;
594 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
595 Register holder =
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000596 stub_compiler_->CheckPrototypes(object, receiver,
597 interceptor_holder, scratch1,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000598 scratch2, scratch3, name, depth1, miss);
ager@chromium.org5c838252010-02-19 08:53:10 +0000599
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000600 // Invoke an interceptor and if it provides a value,
601 // branch to |regular_invoke|.
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 Label regular_invoke;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000603 LoadWithInterceptor(masm, receiver, holder, interceptor_holder,
604 &regular_invoke);
ager@chromium.org5c838252010-02-19 08:53:10 +0000605
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000606 // Interceptor returned nothing for this property. Try to use cached
607 // constant function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000608
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000609 // Check that the maps from interceptor's holder to constant function's
610 // holder haven't changed and thus we can use cached constant function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000611 if (interceptor_holder != lookup->holder()) {
612 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
613 lookup->holder(), scratch1,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000614 scratch2, scratch3, name, depth2, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000615 } else {
616 // CheckPrototypes has a side effect of fetching a 'holder'
617 // for API (object which is instanceof for the signature). It's
618 // safe to omit it here, as if present, it should be fetched
619 // by the previous CheckPrototypes.
620 ASSERT(depth2 == kInvalidProtoDepth);
621 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000622
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000623 // Invoke function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000624 if (can_do_fast_api_call) {
625 GenerateFastApiCall(masm, optimization, arguments_.immediate());
626 } else {
627 __ InvokeFunction(optimization.constant_function(), arguments_,
628 JUMP_FUNCTION);
629 }
630
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000631 // Deferred code for fast API call case---clean preallocated space.
ager@chromium.org5c838252010-02-19 08:53:10 +0000632 if (can_do_fast_api_call) {
633 __ bind(&miss_cleanup);
634 FreeSpaceForFastApiCall(masm, scratch1);
635 __ jmp(miss_label);
636 }
637
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000638 // Invoke a regular function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000639 __ bind(&regular_invoke);
640 if (can_do_fast_api_call) {
641 FreeSpaceForFastApiCall(masm, scratch1);
642 }
643 }
644
645 void CompileRegular(MacroAssembler* masm,
646 JSObject* object,
647 Register receiver,
648 Register scratch1,
649 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000650 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000651 String* name,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000652 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000653 Label* miss_label) {
654 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000655 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000656 scratch1, scratch2, scratch3, name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000657 miss_label);
658
659 __ EnterInternalFrame();
660 // Save the name_ register across the call.
661 __ push(name_);
662
663 PushInterceptorArguments(masm,
664 receiver,
665 holder,
666 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000667 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000668
669 __ CallExternalReference(
670 ExternalReference(
671 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
672 5);
673
674 // Restore the name_ register.
675 __ pop(name_);
676 __ LeaveInternalFrame();
677 }
678
679 void LoadWithInterceptor(MacroAssembler* masm,
680 Register receiver,
681 Register holder,
682 JSObject* holder_obj,
683 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000684 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000685 __ push(holder); // Save the holder.
686 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000687
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000688 CompileCallLoadPropertyWithInterceptor(masm,
689 receiver,
690 holder,
691 name_,
692 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000693
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000694 __ pop(name_); // Restore the name.
695 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000696 __ LeaveInternalFrame();
697
698 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000699 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000700 }
701
ager@chromium.org5c838252010-02-19 08:53:10 +0000702 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000703 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000704 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000705};
706
707
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000708void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
709 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
710 Code* code = NULL;
711 if (kind == Code::LOAD_IC) {
712 code = Builtins::builtin(Builtins::LoadIC_Miss);
713 } else {
714 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
715 }
716
717 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000718 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000719}
720
721
ager@chromium.org5c838252010-02-19 08:53:10 +0000722// Both name_reg and receiver_reg are preserved on jumps to miss_label,
723// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000724void StubCompiler::GenerateStoreField(MacroAssembler* masm,
725 JSObject* object,
726 int index,
727 Map* transition,
728 Register receiver_reg,
729 Register name_reg,
730 Register scratch,
731 Label* miss_label) {
732 // Check that the object isn't a smi.
733 __ test(receiver_reg, Immediate(kSmiTagMask));
734 __ j(zero, miss_label, not_taken);
735
736 // Check that the map of the object hasn't changed.
737 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
738 Immediate(Handle<Map>(object->map())));
739 __ j(not_equal, miss_label, not_taken);
740
741 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000742 if (object->IsJSGlobalProxy()) {
743 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000744 }
745
746 // Stub never generated for non-global objects that require access
747 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000748 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000749
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000750 // Perform map transition for the receiver if necessary.
751 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
752 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000753 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000754 __ pop(scratch); // Return address.
755 __ push(receiver_reg);
756 __ push(Immediate(Handle<Map>(transition)));
757 __ push(eax);
758 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000759 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000760 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000761 return;
762 }
763
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000764 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000765 // Update the map of the object; no write barrier updating is
766 // needed because the map is never in new space.
767 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
768 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000769 }
770
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000771 // Adjust for the number of properties stored in the object. Even in the
772 // face of a transition we can use the old map here because the size of the
773 // object and the number of in-object properties is not going to change.
774 index -= object->map()->inobject_properties();
775
ager@chromium.org7c537e22008-10-16 08:43:32 +0000776 if (index < 0) {
777 // Set the property straight into the object.
778 int offset = object->map()->instance_size() + (index * kPointerSize);
779 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000780
ager@chromium.org7c537e22008-10-16 08:43:32 +0000781 // Update the write barrier for the array address.
782 // Pass the value being stored in the now unused name_reg.
783 __ mov(name_reg, Operand(eax));
784 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
785 } else {
786 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000787 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000788 // Get the properties array (optimistically).
789 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000790 __ mov(FieldOperand(scratch, offset), eax);
791
792 // Update the write barrier for the array address.
793 // Pass the value being stored in the now unused name_reg.
794 __ mov(name_reg, Operand(eax));
795 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
796 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000797
798 // Return the value (register eax).
799 __ ret(0);
800}
801
802
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000803// Generate code to check that a global property cell is empty. Create
804// the property cell at compilation time if no cell exists for the
805// property.
806static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
807 GlobalObject* global,
808 String* name,
809 Register scratch,
810 Label* miss) {
811 Object* probe = global->EnsurePropertyCell(name);
812 if (probe->IsFailure()) return probe;
813 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
814 ASSERT(cell->value()->IsTheHole());
815 __ mov(scratch, Immediate(Handle<Object>(cell)));
816 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
817 Immediate(Factory::the_hole_value()));
818 __ j(not_equal, miss, not_taken);
819 return cell;
820}
821
822
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000823// Calls GenerateCheckPropertyCell for each global object in the prototype chain
824// from object to (but not including) holder.
825static Object* GenerateCheckPropertyCells(MacroAssembler* masm,
826 JSObject* object,
827 JSObject* holder,
828 String* name,
829 Register scratch,
830 Label* miss) {
831 JSObject* current = object;
832 while (current != holder) {
833 if (current->IsGlobalObject()) {
834 Object* cell = GenerateCheckPropertyCell(masm,
835 GlobalObject::cast(current),
836 name,
837 scratch,
838 miss);
839 if (cell->IsFailure()) {
840 return cell;
841 }
842 }
843 ASSERT(current->IsJSObject());
844 current = JSObject::cast(current->GetPrototype());
845 }
846 return NULL;
847}
848
849
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000850#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000851#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000852
853
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000854Register StubCompiler::CheckPrototypes(JSObject* object,
855 Register object_reg,
856 JSObject* holder,
857 Register holder_reg,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000858 Register scratch1,
859 Register scratch2,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000860 String* name,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000861 int save_at_depth,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000862 Label* miss) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000863 // Make sure there's no overlap between holder and object registers.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000864 ASSERT(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
865 ASSERT(!scratch2.is(object_reg) && !scratch2.is(holder_reg)
866 && !scratch2.is(scratch1));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000867 // Keep track of the current object in register reg.
868 Register reg = object_reg;
869 JSObject* current = object;
870 int depth = 0;
871
872 if (save_at_depth == depth) {
873 __ mov(Operand(esp, kPointerSize), reg);
874 }
875
876 // Traverse the prototype chain and check the maps in the prototype chain for
877 // fast and global objects or do negative lookup for normal objects.
878 while (current != holder) {
879 depth++;
880
881 // Only global objects and objects that do not require access
882 // checks are allowed in stubs.
883 ASSERT(current->IsJSGlobalProxy() || !current->IsAccessCheckNeeded());
884
885 ASSERT(current->GetPrototype()->IsJSObject());
886 JSObject* prototype = JSObject::cast(current->GetPrototype());
887 if (!current->HasFastProperties() &&
888 !current->IsJSGlobalObject() &&
889 !current->IsJSGlobalProxy()) {
890 if (!name->IsSymbol()) {
891 Object* lookup_result = Heap::LookupSymbol(name);
892 if (lookup_result->IsFailure()) {
893 set_failure(Failure::cast(lookup_result));
894 return reg;
895 } else {
896 name = String::cast(lookup_result);
897 }
898 }
899 ASSERT(current->property_dictionary()->FindEntry(name) ==
900 StringDictionary::kNotFound);
901
902 GenerateDictionaryNegativeLookup(masm(),
903 miss,
904 reg,
905 name,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000906 scratch1,
907 scratch2);
908 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000909 reg = holder_reg; // from now the object is in holder_reg
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000910 __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000911 } else if (Heap::InNewSpace(prototype)) {
912 // Get the map of the current object.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000913 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
914 __ cmp(Operand(scratch1), Immediate(Handle<Map>(current->map())));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000915 // Branch on the result of the map check.
916 __ j(not_equal, miss, not_taken);
917 // Check access rights to the global object. This has to happen
918 // after the map check so that we know that the object is
919 // actually a global object.
920 if (current->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000921 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000922
923 // Restore scratch register to be the map of the object.
924 // We load the prototype from the map in the scratch register.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000925 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000926 }
927 // The prototype is in new space; we cannot store a reference
928 // to it in the code. Load it from the map.
929 reg = holder_reg; // from now the object is in holder_reg
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000930 __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000931 } else {
932 // Check the map of the current object.
933 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
934 Immediate(Handle<Map>(current->map())));
935 // Branch on the result of the map check.
936 __ j(not_equal, miss, not_taken);
937 // Check access rights to the global object. This has to happen
938 // after the map check so that we know that the object is
939 // actually a global object.
940 if (current->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000941 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000942 }
943 // The prototype is in old space; load it directly.
944 reg = holder_reg; // from now the object is in holder_reg
945 __ mov(reg, Handle<JSObject>(prototype));
946 }
947
948 if (save_at_depth == depth) {
949 __ mov(Operand(esp, kPointerSize), reg);
950 }
951
952 // Go to the next object in the prototype chain.
953 current = prototype;
954 }
955 ASSERT(current == holder);
956
957 // Log the check depth.
958 LOG(IntEvent("check-maps-depth", depth + 1));
959
960 // Check the holder map.
961 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
962 Immediate(Handle<Map>(holder->map())));
963 __ j(not_equal, miss, not_taken);
964
965 // Perform security check for access to the global object.
966 ASSERT(holder->IsJSGlobalProxy() || !holder->IsAccessCheckNeeded());
967 if (holder->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000968 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000969 };
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000970
971 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000972 // that their maps haven't changed. We also need to check that the
973 // property cell for the property is still empty.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000974 Object* result = GenerateCheckPropertyCells(masm(),
975 object,
976 holder,
977 name,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000978 scratch1,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000979 miss);
980 if (result->IsFailure()) set_failure(Failure::cast(result));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000981
ager@chromium.org5c838252010-02-19 08:53:10 +0000982 // Return the register containing the holder.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000983 return reg;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000984}
985
986
987void StubCompiler::GenerateLoadField(JSObject* object,
988 JSObject* holder,
989 Register receiver,
990 Register scratch1,
991 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000992 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000993 int index,
994 String* name,
995 Label* miss) {
996 // Check that the receiver isn't a smi.
997 __ test(receiver, Immediate(kSmiTagMask));
998 __ j(zero, miss, not_taken);
999
1000 // Check the prototype chain.
1001 Register reg =
1002 CheckPrototypes(object, receiver, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001003 scratch1, scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001004
1005 // Get the value from the properties.
1006 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1007 __ ret(0);
1008}
1009
1010
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001011bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001012 JSObject* holder,
1013 Register receiver,
1014 Register name_reg,
1015 Register scratch1,
1016 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001017 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001018 AccessorInfo* callback,
1019 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001020 Label* miss,
1021 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001022 // Check that the receiver isn't a smi.
1023 __ test(receiver, Immediate(kSmiTagMask));
1024 __ j(zero, miss, not_taken);
1025
1026 // Check that the maps haven't changed.
1027 Register reg =
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001028 CheckPrototypes(object, receiver, holder, scratch1,
1029 scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001030
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001031 Handle<AccessorInfo> callback_handle(callback);
1032
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001033 __ EnterInternalFrame();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001034 __ PushHandleScope(scratch2);
1035 // Push the stack address where the list of arguments ends.
1036 __ mov(scratch2, esp);
1037 __ sub(Operand(scratch2), Immediate(2 * kPointerSize));
1038 __ push(scratch2);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001039 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001040 __ push(reg); // holder
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001041 // Push data from AccessorInfo.
1042 if (Heap::InNewSpace(callback_handle->data())) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001043 __ mov(scratch2, Immediate(callback_handle));
1044 __ push(FieldOperand(scratch2, AccessorInfo::kDataOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001045 } else {
1046 __ push(Immediate(Handle<Object>(callback_handle->data())));
1047 }
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001048 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001049 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001050 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001051 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001052 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001053 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001054
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001055 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001056 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001057 Address getter_address = v8::ToCData<Address>(callback->getter());
1058 ApiFunction fun(getter_address);
1059 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001060 // Emitting a stub call may try to allocate (if the code is not
1061 // already generated). Do not allow the assembler to perform a
1062 // garbage collection but instead return the allocation failure
1063 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001064 Object* result = masm()->TryCallStub(&stub);
1065 if (result->IsFailure()) {
1066 *failure = Failure::cast(result);
1067 return false;
1068 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001069
1070 // We need to avoid using eax since that now holds the result.
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001071 Register tmp = scratch2.is(eax) ? reg : scratch2;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001072 // Emitting PopHandleScope may try to allocate. Do not allow the
1073 // assembler to perform a garbage collection but instead return a
1074 // failure object.
1075 result = masm()->TryPopHandleScope(eax, tmp);
1076 if (result->IsFailure()) {
1077 *failure = Failure::cast(result);
1078 return false;
1079 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001080 __ LeaveInternalFrame();
1081
1082 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001083 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001084}
1085
1086
1087void StubCompiler::GenerateLoadConstant(JSObject* object,
1088 JSObject* holder,
1089 Register receiver,
1090 Register scratch1,
1091 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001092 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001093 Object* value,
1094 String* name,
1095 Label* miss) {
1096 // Check that the receiver isn't a smi.
1097 __ test(receiver, Immediate(kSmiTagMask));
1098 __ j(zero, miss, not_taken);
1099
1100 // Check that the maps haven't changed.
1101 Register reg =
1102 CheckPrototypes(object, receiver, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001103 scratch1, scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001104
1105 // Return the constant value.
1106 __ mov(eax, Handle<Object>(value));
1107 __ ret(0);
1108}
1109
1110
1111void StubCompiler::GenerateLoadInterceptor(JSObject* object,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001112 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001113 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001114 Register receiver,
1115 Register name_reg,
1116 Register scratch1,
1117 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001118 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001119 String* name,
1120 Label* miss) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001121 ASSERT(interceptor_holder->HasNamedInterceptor());
1122 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
1123
1124 // Check that the receiver isn't a smi.
1125 __ test(receiver, Immediate(kSmiTagMask));
1126 __ j(zero, miss, not_taken);
1127
1128 // So far the most popular follow ups for interceptor loads are FIELD
1129 // and CALLBACKS, so inline only them, other cases may be added
1130 // later.
1131 bool compile_followup_inline = false;
1132 if (lookup->IsProperty() && lookup->IsCacheable()) {
1133 if (lookup->type() == FIELD) {
1134 compile_followup_inline = true;
1135 } else if (lookup->type() == CALLBACKS &&
1136 lookup->GetCallbackObject()->IsAccessorInfo() &&
1137 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
1138 compile_followup_inline = true;
1139 }
1140 }
1141
1142 if (compile_followup_inline) {
1143 // Compile the interceptor call, followed by inline code to load the
1144 // property from further up the prototype chain if the call fails.
1145 // Check that the maps haven't changed.
1146 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001147 scratch1, scratch2, scratch3,
1148 name, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001149 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
1150
1151 // Save necessary data before invoking an interceptor.
1152 // Requires a frame to make GC aware of pushed pointers.
1153 __ EnterInternalFrame();
1154
1155 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1156 // CALLBACKS case needs a receiver to be passed into C++ callback.
1157 __ push(receiver);
1158 }
1159 __ push(holder_reg);
1160 __ push(name_reg);
1161
1162 // Invoke an interceptor. Note: map checks from receiver to
1163 // interceptor's holder has been compiled before (see a caller
1164 // of this method.)
1165 CompileCallLoadPropertyWithInterceptor(masm(),
1166 receiver,
1167 holder_reg,
1168 name_reg,
1169 interceptor_holder);
1170
1171 // Check if interceptor provided a value for property. If it's
1172 // the case, return immediately.
1173 Label interceptor_failed;
1174 __ cmp(eax, Factory::no_interceptor_result_sentinel());
1175 __ j(equal, &interceptor_failed);
1176 __ LeaveInternalFrame();
1177 __ ret(0);
1178
1179 __ bind(&interceptor_failed);
1180 __ pop(name_reg);
1181 __ pop(holder_reg);
1182 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1183 __ pop(receiver);
1184 }
1185
1186 __ LeaveInternalFrame();
1187
1188 // Check that the maps from interceptor's holder to lookup's holder
1189 // haven't changed. And load lookup's holder into holder_reg.
1190 if (interceptor_holder != lookup->holder()) {
1191 holder_reg = CheckPrototypes(interceptor_holder,
1192 holder_reg,
1193 lookup->holder(),
1194 scratch1,
1195 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001196 scratch3,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001197 name,
1198 miss);
1199 }
1200
1201 if (lookup->type() == FIELD) {
1202 // We found FIELD property in prototype chain of interceptor's holder.
1203 // Retrieve a field from field's holder.
1204 GenerateFastPropertyLoad(masm(), eax, holder_reg,
1205 lookup->holder(), lookup->GetFieldIndex());
1206 __ ret(0);
1207 } else {
1208 // We found CALLBACKS property in prototype chain of interceptor's
1209 // holder.
1210 ASSERT(lookup->type() == CALLBACKS);
1211 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
1212 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
1213 ASSERT(callback != NULL);
1214 ASSERT(callback->getter() != NULL);
1215
1216 // Tail call to runtime.
1217 // Important invariant in CALLBACKS case: the code above must be
1218 // structured to never clobber |receiver| register.
1219 __ pop(scratch2); // return address
1220 __ push(receiver);
1221 __ push(holder_reg);
1222 __ mov(holder_reg, Immediate(Handle<AccessorInfo>(callback)));
1223 __ push(holder_reg);
1224 __ push(FieldOperand(holder_reg, AccessorInfo::kDataOffset));
1225 __ push(name_reg);
1226 __ push(scratch2); // restore return address
1227
1228 ExternalReference ref =
1229 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
1230 __ TailCallExternalReference(ref, 5, 1);
1231 }
1232 } else { // !compile_followup_inline
1233 // Call the runtime system to load the interceptor.
1234 // Check that the maps haven't changed.
1235 Register holder_reg =
1236 CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001237 scratch1, scratch2, scratch3, name, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001238 __ pop(scratch2); // save old return address
1239 PushInterceptorArguments(masm(), receiver, holder_reg,
1240 name_reg, interceptor_holder);
1241 __ push(scratch2); // restore old return address
1242
1243 ExternalReference ref = ExternalReference(
1244 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
1245 __ TailCallExternalReference(ref, 5, 1);
1246 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001247}
1248
1249
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001250void CallStubCompiler::GenerateNameCheck(String* name, Label* miss) {
1251 if (kind_ == Code::KEYED_CALL_IC) {
1252 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1253 __ j(not_equal, miss, not_taken);
1254 }
1255}
1256
1257
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001258Object* CallStubCompiler::GenerateMissBranch() {
1259 Object* obj = StubCache::ComputeCallMiss(arguments().immediate(), kind_);
1260 if (obj->IsFailure()) return obj;
1261 __ jmp(Handle<Code>(Code::cast(obj)), RelocInfo::CODE_TARGET);
1262 return obj;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001263}
1264
1265
ager@chromium.org5c838252010-02-19 08:53:10 +00001266Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001268 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001269 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001270 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001271 // -- ecx : name
1272 // -- esp[0] : return address
1273 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1274 // -- ...
1275 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001276 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 Label miss;
1278
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001279 GenerateNameCheck(name, &miss);
1280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 // Get the receiver from the stack.
1282 const int argc = arguments().immediate();
1283 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1284
1285 // Check that the receiver isn't a smi.
1286 __ test(edx, Immediate(kSmiTagMask));
1287 __ j(zero, &miss, not_taken);
1288
1289 // Do the right check and compute the holder register.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001290 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, edi,
1291 name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292
ager@chromium.org7c537e22008-10-16 08:43:32 +00001293 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
1295 // Check that the function really is a function.
1296 __ test(edi, Immediate(kSmiTagMask));
1297 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001298 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001299 __ j(not_equal, &miss, not_taken);
1300
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001301 // Patch the receiver on the stack with the global proxy if
1302 // necessary.
1303 if (object->IsGlobalObject()) {
1304 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1305 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1306 }
1307
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001308 // Invoke the function.
1309 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1310
1311 // Handle call cache miss.
1312 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001313 Object* obj = GenerateMissBranch();
1314 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315
1316 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001317 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318}
1319
1320
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001321Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1322 JSObject* holder,
1323 JSFunction* function,
1324 String* name,
1325 CheckType check) {
1326 // ----------- S t a t e -------------
1327 // -- ecx : name
1328 // -- esp[0] : return address
1329 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1330 // -- ...
1331 // -- esp[(argc + 1) * 4] : receiver
1332 // -----------------------------------
1333 ASSERT(check == RECEIVER_MAP_CHECK);
1334
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001335 // If object is not an array, bail out to regular call.
1336 if (!object->IsJSArray()) {
1337 return Heap::undefined_value();
1338 }
1339
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001340 Label miss;
1341
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001342 GenerateNameCheck(name, &miss);
1343
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001344 // Get the receiver from the stack.
1345 const int argc = arguments().immediate();
1346 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1347
1348 // Check that the receiver isn't a smi.
1349 __ test(edx, Immediate(kSmiTagMask));
1350 __ j(zero, &miss);
1351
1352 CheckPrototypes(JSObject::cast(object), edx,
1353 holder, ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001354 eax, edi, name, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001355
1356 if (argc == 0) {
1357 // Noop, return the length.
1358 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1359 __ ret((argc + 1) * kPointerSize);
1360 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001361 Label call_builtin;
1362
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001363 // Get the elements array of the object.
1364 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1365
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001366 // Check that the elements are in fast mode and writable.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001367 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1368 Immediate(Factory::fixed_array_map()));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001369 __ j(not_equal, &call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001370
1371 if (argc == 1) { // Otherwise fall through to call builtin.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001372 Label exit, with_write_barrier, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001373
1374 // Get the array's length into eax and calculate new length.
1375 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1376 STATIC_ASSERT(kSmiTagSize == 1);
1377 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001378 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001379
1380 // Get the element's length into ecx.
1381 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001382
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001383 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001384 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001385 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001386
1387 // Save new length.
1388 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1389
1390 // Push the element.
1391 __ lea(edx, FieldOperand(ebx,
1392 eax, times_half_pointer_size,
1393 FixedArray::kHeaderSize - argc * kPointerSize));
1394 __ mov(ecx, Operand(esp, argc * kPointerSize));
1395 __ mov(Operand(edx, 0), ecx);
1396
1397 // Check if value is a smi.
1398 __ test(ecx, Immediate(kSmiTagMask));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001399 __ j(not_zero, &with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001400
1401 __ bind(&exit);
1402 __ ret((argc + 1) * kPointerSize);
1403
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001404 __ bind(&with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001405
1406 __ InNewSpace(ebx, ecx, equal, &exit);
1407
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001408 __ RecordWriteHelper(ebx, edx, ecx);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001409 __ ret((argc + 1) * kPointerSize);
1410
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001411 __ bind(&attempt_to_grow_elements);
1412 ExternalReference new_space_allocation_top =
1413 ExternalReference::new_space_allocation_top_address();
1414 ExternalReference new_space_allocation_limit =
1415 ExternalReference::new_space_allocation_limit_address();
1416
1417 const int kAllocationDelta = 4;
1418 // Load top.
1419 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1420
1421 // Check if it's the end of elements.
1422 __ lea(edx, FieldOperand(ebx,
1423 eax, times_half_pointer_size,
1424 FixedArray::kHeaderSize - argc * kPointerSize));
1425 __ cmp(edx, Operand(ecx));
1426 __ j(not_equal, &call_builtin);
1427 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1428 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001429 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001430
1431 // We fit and could grow elements.
1432 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1433 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001434
1435 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001436 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001437 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001438 for (int i = 1; i < kAllocationDelta; i++) {
1439 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001440 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001441 }
1442
1443 // Restore receiver to edx as finish sequence assumes it's here.
1444 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1445
1446 // Increment element's and array's sizes.
1447 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001448 Immediate(Smi::FromInt(kAllocationDelta)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001449 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1450
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001451 // Elements are in new space, so write barrier is not required.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001452 __ ret((argc + 1) * kPointerSize);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001453 }
1454
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001455 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001456 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1457 argc + 1,
1458 1);
1459 }
1460
1461 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001462 Object* obj = GenerateMissBranch();
1463 if (obj->IsFailure()) return obj;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001464
1465 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001466 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001467}
1468
1469
1470Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1471 JSObject* holder,
1472 JSFunction* function,
1473 String* name,
1474 CheckType check) {
1475 // ----------- S t a t e -------------
1476 // -- ecx : name
1477 // -- esp[0] : return address
1478 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1479 // -- ...
1480 // -- esp[(argc + 1) * 4] : receiver
1481 // -----------------------------------
1482 ASSERT(check == RECEIVER_MAP_CHECK);
1483
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001484 // If object is not an array, bail out to regular call.
1485 if (!object->IsJSArray()) {
1486 return Heap::undefined_value();
1487 }
1488
ager@chromium.orgac091b72010-05-05 07:34:42 +00001489 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001490
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001491 GenerateNameCheck(name, &miss);
1492
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001493 // Get the receiver from the stack.
1494 const int argc = arguments().immediate();
1495 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1496
1497 // Check that the receiver isn't a smi.
1498 __ test(edx, Immediate(kSmiTagMask));
1499 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001500 CheckPrototypes(JSObject::cast(object), edx,
1501 holder, ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001502 eax, edi, name, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001503
1504 // Get the elements array of the object.
1505 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1506
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001507 // Check that the elements are in fast mode and writable.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001508 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1509 Immediate(Factory::fixed_array_map()));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001510 __ j(not_equal, &call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001511
1512 // Get the array's length into ecx and calculate new length.
1513 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1514 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001515 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001516
1517 // Get the last element.
1518 STATIC_ASSERT(kSmiTagSize == 1);
1519 STATIC_ASSERT(kSmiTag == 0);
1520 __ mov(eax, FieldOperand(ebx,
1521 ecx, times_half_pointer_size,
1522 FixedArray::kHeaderSize));
1523 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1524 __ j(equal, &call_builtin);
1525
1526 // Set the array's length.
1527 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1528
1529 // Fill with the hole.
1530 __ mov(FieldOperand(ebx,
1531 ecx, times_half_pointer_size,
1532 FixedArray::kHeaderSize),
1533 Immediate(Factory::the_hole_value()));
1534 __ ret((argc + 1) * kPointerSize);
1535
ager@chromium.orgac091b72010-05-05 07:34:42 +00001536 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001537 __ mov(eax, Immediate(Factory::undefined_value()));
1538 __ ret((argc + 1) * kPointerSize);
1539
1540 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001541 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1542 argc + 1,
1543 1);
1544
1545 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001546 Object* obj = GenerateMissBranch();
1547 if (obj->IsFailure()) return obj;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001548
1549 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001550 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001551}
1552
1553
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001554Object* CallStubCompiler::CompileStringCharCodeAtCall(Object* object,
1555 JSObject* holder,
1556 JSFunction* function,
1557 String* name,
1558 CheckType check) {
1559 // ----------- S t a t e -------------
1560 // -- ecx : function name
1561 // -- esp[0] : return address
1562 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1563 // -- ...
1564 // -- esp[(argc + 1) * 4] : receiver
1565 // -----------------------------------
1566
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001567 // If object is not a string, bail out to regular call.
1568 if (!object->IsString()) return Heap::undefined_value();
1569
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001570 const int argc = arguments().immediate();
1571
1572 Label miss;
1573 Label index_out_of_range;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001574 GenerateNameCheck(name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001575
1576 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001577 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1578 Context::STRING_FUNCTION_INDEX,
1579 eax);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001580 ASSERT(object != holder);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001581 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001582 ebx, edx, edi, name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001583
1584 Register receiver = ebx;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001585 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001586 Register scratch = edx;
1587 Register result = eax;
1588 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1589 if (argc > 0) {
1590 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1591 } else {
1592 __ Set(index, Immediate(Factory::undefined_value()));
1593 }
1594
1595 StringCharCodeAtGenerator char_code_at_generator(receiver,
1596 index,
1597 scratch,
1598 result,
1599 &miss, // When not a string.
1600 &miss, // When not a number.
1601 &index_out_of_range,
1602 STRING_INDEX_IS_NUMBER);
1603 char_code_at_generator.GenerateFast(masm());
1604 __ ret((argc + 1) * kPointerSize);
1605
1606 ICRuntimeCallHelper call_helper;
1607 char_code_at_generator.GenerateSlow(masm(), call_helper);
1608
1609 __ bind(&index_out_of_range);
1610 __ Set(eax, Immediate(Factory::nan_value()));
1611 __ ret((argc + 1) * kPointerSize);
1612
1613 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001614 Object* obj = GenerateMissBranch();
1615 if (obj->IsFailure()) return obj;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001616
1617 // Return the generated code.
1618 return GetCode(function);
1619}
1620
1621
1622Object* CallStubCompiler::CompileStringCharAtCall(Object* object,
1623 JSObject* holder,
1624 JSFunction* function,
1625 String* name,
1626 CheckType check) {
1627 // ----------- S t a t e -------------
1628 // -- ecx : function name
1629 // -- esp[0] : return address
1630 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1631 // -- ...
1632 // -- esp[(argc + 1) * 4] : receiver
1633 // -----------------------------------
1634
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001635 // If object is not a string, bail out to regular call.
1636 if (!object->IsString()) return Heap::undefined_value();
1637
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001638 const int argc = arguments().immediate();
1639
1640 Label miss;
1641 Label index_out_of_range;
1642
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001643 GenerateNameCheck(name, &miss);
1644
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001645 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001646 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1647 Context::STRING_FUNCTION_INDEX,
1648 eax);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001649 ASSERT(object != holder);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001650 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001651 ebx, edx, edi, name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001652
1653 Register receiver = eax;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001654 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001655 Register scratch1 = ebx;
1656 Register scratch2 = edx;
1657 Register result = eax;
1658 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1659 if (argc > 0) {
1660 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1661 } else {
1662 __ Set(index, Immediate(Factory::undefined_value()));
1663 }
1664
1665 StringCharAtGenerator char_at_generator(receiver,
1666 index,
1667 scratch1,
1668 scratch2,
1669 result,
1670 &miss, // When not a string.
1671 &miss, // When not a number.
1672 &index_out_of_range,
1673 STRING_INDEX_IS_NUMBER);
1674 char_at_generator.GenerateFast(masm());
1675 __ ret((argc + 1) * kPointerSize);
1676
1677 ICRuntimeCallHelper call_helper;
1678 char_at_generator.GenerateSlow(masm(), call_helper);
1679
1680 __ bind(&index_out_of_range);
1681 __ Set(eax, Immediate(Factory::empty_string()));
1682 __ ret((argc + 1) * kPointerSize);
1683
1684 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001685 Object* obj = GenerateMissBranch();
1686 if (obj->IsFailure()) return obj;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001687
1688 // Return the generated code.
1689 return GetCode(function);
1690}
1691
1692
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693Object* CallStubCompiler::CompileCallConstant(Object* object,
1694 JSObject* holder,
1695 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001696 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001697 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001699 // -- ecx : name
1700 // -- esp[0] : return address
1701 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1702 // -- ...
1703 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001704 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001705
1706 SharedFunctionInfo* function_info = function->shared();
1707 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001708 const int id = function_info->custom_call_generator_id();
1709 Object* result =
1710 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001711 // undefined means bail out to regular compiler.
1712 if (!result->IsUndefined()) {
1713 return result;
1714 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001715 }
1716
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001717 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001719 GenerateNameCheck(name, &miss_in_smi_check);
1720
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001721 // Get the receiver from the stack.
1722 const int argc = arguments().immediate();
1723 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1724
1725 // Check that the receiver isn't a smi.
1726 if (check != NUMBER_CHECK) {
1727 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001728 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 }
1730
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001731 // Make sure that it's okay not to patch the on stack receiver
1732 // unless we're doing a receiver map check.
1733 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1734
ager@chromium.org5c838252010-02-19 08:53:10 +00001735 CallOptimization optimization(function);
1736 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001737 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001738
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 switch (check) {
1740 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001741 __ IncrementCounter(&Counters::call_const, 1);
1742
1743 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1744 depth = optimization.GetPrototypeDepthOfExpectedType(
1745 JSObject::cast(object), holder);
1746 }
1747
1748 if (depth != kInvalidProtoDepth) {
1749 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1750 ReserveSpaceForFastApiCall(masm(), eax);
1751 }
1752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001754 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001755 ebx, eax, edi, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001756
1757 // Patch the receiver on the stack with the global proxy if
1758 // necessary.
1759 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001760 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001761 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1762 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1763 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 break;
1765
1766 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001767 if (!function->IsBuiltin()) {
1768 // Calling non-builtins with a value as receiver requires boxing.
1769 __ jmp(&miss);
1770 } else {
1771 // Check that the object is a string or a symbol.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001772 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001773 __ j(above_equal, &miss, not_taken);
1774 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001775 GenerateDirectLoadGlobalFunctionPrototype(
1776 masm(), Context::STRING_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001777 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001778 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001779 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780 break;
1781
1782 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001783 if (!function->IsBuiltin()) {
1784 // Calling non-builtins with a value as receiver requires boxing.
1785 __ jmp(&miss);
1786 } else {
1787 Label fast;
1788 // Check that the object is a smi or a heap number.
1789 __ test(edx, Immediate(kSmiTagMask));
1790 __ j(zero, &fast, taken);
1791 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1792 __ j(not_equal, &miss, not_taken);
1793 __ bind(&fast);
1794 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001795 GenerateDirectLoadGlobalFunctionPrototype(
1796 masm(), Context::NUMBER_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001797 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001798 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001799 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 break;
1801 }
1802
1803 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001804 if (!function->IsBuiltin()) {
1805 // Calling non-builtins with a value as receiver requires boxing.
1806 __ jmp(&miss);
1807 } else {
1808 Label fast;
1809 // Check that the object is a boolean.
1810 __ cmp(edx, Factory::true_value());
1811 __ j(equal, &fast, taken);
1812 __ cmp(edx, Factory::false_value());
1813 __ j(not_equal, &miss, not_taken);
1814 __ bind(&fast);
1815 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001816 GenerateDirectLoadGlobalFunctionPrototype(
1817 masm(), Context::BOOLEAN_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001818 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001819 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001820 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 break;
1822 }
1823
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 default:
1825 UNREACHABLE();
1826 }
1827
ager@chromium.org5c838252010-02-19 08:53:10 +00001828 if (depth != kInvalidProtoDepth) {
1829 GenerateFastApiCall(masm(), optimization, argc);
1830 } else {
1831 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1832 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 // Handle call cache miss.
1835 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001836 if (depth != kInvalidProtoDepth) {
1837 FreeSpaceForFastApiCall(masm(), eax);
1838 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001839 __ bind(&miss_in_smi_check);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001840 Object* obj = GenerateMissBranch();
1841 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842
1843 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001844 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845}
1846
1847
ager@chromium.org5c838252010-02-19 08:53:10 +00001848Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 JSObject* holder,
1850 String* name) {
1851 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001852 // -- ecx : name
1853 // -- esp[0] : return address
1854 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1855 // -- ...
1856 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 Label miss;
1859
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001860 GenerateNameCheck(name, &miss);
1861
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 // Get the number of arguments.
1863 const int argc = arguments().immediate();
1864
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001865 LookupResult lookup;
1866 LookupPostInterceptor(holder, name, &lookup);
1867
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // Get the receiver from the stack.
1869 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001870
ager@chromium.org5c838252010-02-19 08:53:10 +00001871 CallInterceptorCompiler compiler(this, arguments(), ecx);
1872 compiler.Compile(masm(),
1873 object,
1874 holder,
1875 name,
1876 &lookup,
1877 edx,
1878 ebx,
1879 edi,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001880 eax,
ager@chromium.org5c838252010-02-19 08:53:10 +00001881 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001883 // Restore receiver.
1884 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885
1886 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001887 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001889 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 __ j(not_equal, &miss, not_taken);
1891
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001892 // Patch the receiver on the stack with the global proxy if
1893 // necessary.
1894 if (object->IsGlobalObject()) {
1895 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1896 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1897 }
1898
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001900 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1902
1903 // Handle load cache miss.
1904 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001905 Object* obj = GenerateMissBranch();
1906 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907
1908 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001910}
1911
1912
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001913Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1914 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001915 JSGlobalPropertyCell* cell,
1916 JSFunction* function,
1917 String* name) {
1918 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001919 // -- ecx : name
1920 // -- esp[0] : return address
1921 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1922 // -- ...
1923 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001924 // -----------------------------------
1925 Label miss;
1926
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001927 GenerateNameCheck(name, &miss);
1928
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001929 // Get the number of arguments.
1930 const int argc = arguments().immediate();
1931
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001932 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001933 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001934
1935 // If the object is the holder then we know that it's a global
1936 // object which can only happen for contextual calls. In this case,
1937 // the receiver cannot be a smi.
1938 if (object != holder) {
1939 __ test(edx, Immediate(kSmiTagMask));
1940 __ j(zero, &miss, not_taken);
1941 }
1942
1943 // Check that the maps haven't changed.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001944 CheckPrototypes(object, edx, holder, ebx, eax, edi, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001945
1946 // Get the value from the cell.
1947 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1948 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1949
1950 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001951 if (Heap::InNewSpace(function)) {
1952 // We can't embed a pointer to a function in new space so we have
1953 // to verify that the shared function info is unchanged. This has
1954 // the nice side effect that multiple closures based on the same
1955 // function can all use this call IC. Before we load through the
1956 // function, we have to verify that it still is a function.
1957 __ test(edi, Immediate(kSmiTagMask));
1958 __ j(zero, &miss, not_taken);
1959 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1960 __ j(not_equal, &miss, not_taken);
1961
1962 // Check the shared function info. Make sure it hasn't changed.
1963 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1964 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1965 __ j(not_equal, &miss, not_taken);
1966 } else {
1967 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1968 __ j(not_equal, &miss, not_taken);
1969 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001970
1971 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001972 if (object->IsGlobalObject()) {
1973 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1974 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1975 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001976
1977 // Setup the context (function already in edi).
1978 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1979
1980 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001981 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001982 ASSERT(function->is_compiled());
1983 Handle<Code> code(function->code());
1984 ParameterCount expected(function->shared()->formal_parameter_count());
1985 __ InvokeCode(code, expected, arguments(),
1986 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1987
1988 // Handle call cache miss.
1989 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001990 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001991 Object* obj = GenerateMissBranch();
1992 if (obj->IsFailure()) return obj;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001993
1994 // Return the generated code.
1995 return GetCode(NORMAL, name);
1996}
1997
1998
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999Object* StoreStubCompiler::CompileStoreField(JSObject* object,
2000 int index,
2001 Map* transition,
2002 String* name) {
2003 // ----------- S t a t e -------------
2004 // -- eax : value
2005 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002006 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002009 Label miss;
2010
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002011 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002012 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002013 object,
2014 index,
2015 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002016 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002017 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018
2019 // Handle store cache miss.
2020 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002021 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002023 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024
2025 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027}
2028
2029
2030Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
2031 AccessorInfo* callback,
2032 String* name) {
2033 // ----------- S t a t e -------------
2034 // -- eax : value
2035 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002036 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002039 Label miss;
2040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002042 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043 __ j(zero, &miss, not_taken);
2044
2045 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002046 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 Immediate(Handle<Map>(object->map())));
2048 __ j(not_equal, &miss, not_taken);
2049
2050 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002051 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002052 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 }
2054
2055 // Stub never generated for non-global objects that require access
2056 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002057 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058
2059 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002060 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
2062 __ push(ecx); // name
2063 __ push(eax); // value
2064 __ push(ebx); // restore return address
2065
mads.s.ager31e71382008-08-13 09:32:07 +00002066 // Do tail-call to the runtime system.
2067 ExternalReference store_callback_property =
2068 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002069 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070
2071 // Handle store cache miss.
2072 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002074 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075
2076 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078}
2079
2080
2081Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
2082 String* name) {
2083 // ----------- S t a t e -------------
2084 // -- eax : value
2085 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002086 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 Label miss;
2090
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002092 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 __ j(zero, &miss, not_taken);
2094
2095 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002096 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 Immediate(Handle<Map>(receiver->map())));
2098 __ j(not_equal, &miss, not_taken);
2099
2100 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002101 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002102 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 }
2104
2105 // Stub never generated for non-global objects that require access
2106 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002107 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108
2109 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002110 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 __ push(ecx); // name
2112 __ push(eax); // value
2113 __ push(ebx); // restore return address
2114
mads.s.ager31e71382008-08-13 09:32:07 +00002115 // Do tail-call to the runtime system.
2116 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002118 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119
2120 // Handle store cache miss.
2121 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002123 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002124
2125 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002126 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127}
2128
2129
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002130Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
2131 JSGlobalPropertyCell* cell,
2132 String* name) {
2133 // ----------- S t a t e -------------
2134 // -- eax : value
2135 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002136 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002137 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002138 // -----------------------------------
2139 Label miss;
2140
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002141 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002142 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002143 Immediate(Handle<Map>(object->map())));
2144 __ j(not_equal, &miss, not_taken);
2145
2146 // Store the value in the cell.
2147 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2148 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
2149
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002150 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002151 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002152 __ ret(0);
2153
2154 // Handle store cache miss.
2155 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002156 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
2157 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
2158 __ jmp(ic, RelocInfo::CODE_TARGET);
2159
2160 // Return the generated code.
2161 return GetCode(NORMAL, name);
2162}
2163
2164
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
2166 int index,
2167 Map* transition,
2168 String* name) {
2169 // ----------- S t a t e -------------
2170 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002171 // -- ecx : key
2172 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 Label miss;
2176
2177 __ IncrementCounter(&Counters::keyed_store_field, 1);
2178
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 // Check that the name has not changed.
2180 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
2181 __ j(not_equal, &miss, not_taken);
2182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002184 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002185 object,
2186 index,
2187 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00002188 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002189 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190
2191 // Handle store cache miss.
2192 __ bind(&miss);
2193 __ DecrementCounter(&Counters::keyed_store_field, 1);
2194 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002195 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196
2197 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002198 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199}
2200
2201
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002202Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
2203 JSObject* object,
2204 JSObject* last) {
2205 // ----------- S t a t e -------------
2206 // -- eax : receiver
2207 // -- ecx : name
2208 // -- esp[0] : return address
2209 // -----------------------------------
2210 Label miss;
2211
2212 // Check that the receiver isn't a smi.
2213 __ test(eax, Immediate(kSmiTagMask));
2214 __ j(zero, &miss, not_taken);
2215
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002216 ASSERT(last->IsGlobalObject() || last->HasFastProperties());
2217
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002218 // Check the maps of the full prototype chain. Also check that
2219 // global property cells up to (but not including) the last object
2220 // in the prototype chain are empty.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002221 CheckPrototypes(object, eax, last, ebx, edx, edi, name, &miss);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002222
2223 // If the last object in the prototype chain is a global object,
2224 // check that the global property cell is empty.
2225 if (last->IsGlobalObject()) {
2226 Object* cell = GenerateCheckPropertyCell(masm(),
2227 GlobalObject::cast(last),
2228 name,
2229 edx,
2230 &miss);
2231 if (cell->IsFailure()) return cell;
2232 }
2233
2234 // Return undefined if maps of the full prototype chain are still the
2235 // same and no global property with this name contains a value.
2236 __ mov(eax, Factory::undefined_value());
2237 __ ret(0);
2238
2239 __ bind(&miss);
2240 GenerateLoadMiss(masm(), Code::LOAD_IC);
2241
2242 // Return the generated code.
2243 return GetCode(NONEXISTENT, Heap::empty_string());
2244}
2245
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002246
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2248 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 int index,
2250 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002252 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 // -- ecx : name
2254 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 Label miss;
2257
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002258 GenerateLoadField(object, holder, eax, ebx, edx, edi, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 __ bind(&miss);
2260 GenerateLoadMiss(masm(), Code::LOAD_IC);
2261
2262 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002263 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264}
2265
2266
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002267Object* LoadStubCompiler::CompileLoadCallback(String* name,
2268 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002270 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002272 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 // -- ecx : name
2274 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 Label miss;
2277
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002278 Failure* failure = Failure::InternalError();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002279 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx, edi,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002280 callback, name, &miss, &failure);
2281 if (!success) return failure;
2282
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 __ bind(&miss);
2284 GenerateLoadMiss(masm(), Code::LOAD_IC);
2285
2286 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288}
2289
2290
2291Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2292 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002293 Object* value,
2294 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002296 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 // -- ecx : name
2298 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 Label miss;
2301
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002302 GenerateLoadConstant(object, holder, eax, ebx, edx, edi, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 __ bind(&miss);
2304 GenerateLoadMiss(masm(), Code::LOAD_IC);
2305
2306 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002307 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308}
2309
2310
2311Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2312 JSObject* holder,
2313 String* name) {
2314 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002315 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 // -- ecx : name
2317 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 Label miss;
2320
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002321 LookupResult lookup;
2322 LookupPostInterceptor(holder, name, &lookup);
2323
ager@chromium.orge2902be2009-06-08 12:21:35 +00002324 // TODO(368): Compile in the whole chain: all the interceptors in
2325 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002326 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002327 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002328 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002329 eax,
2330 ecx,
2331 edx,
2332 ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002333 edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002334 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002335 &miss);
2336
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 __ bind(&miss);
2338 GenerateLoadMiss(masm(), Code::LOAD_IC);
2339
2340 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342}
2343
2344
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002345Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2346 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002347 JSGlobalPropertyCell* cell,
2348 String* name,
2349 bool is_dont_delete) {
2350 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002351 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002352 // -- ecx : name
2353 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002354 // -----------------------------------
2355 Label miss;
2356
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002357 // If the object is the holder then we know that it's a global
2358 // object which can only happen for contextual loads. In this case,
2359 // the receiver cannot be a smi.
2360 if (object != holder) {
2361 __ test(eax, Immediate(kSmiTagMask));
2362 __ j(zero, &miss, not_taken);
2363 }
2364
2365 // Check that the maps haven't changed.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002366 CheckPrototypes(object, eax, holder, ebx, edx, edi, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002367
2368 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002369 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2370 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002371
2372 // Check for deleted property if property can actually be deleted.
2373 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002374 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002375 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002376 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002377 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002378 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002379 }
2380
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002381 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002382 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002383 __ ret(0);
2384
2385 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002386 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2387 GenerateLoadMiss(masm(), Code::LOAD_IC);
2388
2389 // Return the generated code.
2390 return GetCode(NORMAL, name);
2391}
2392
2393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002394Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2395 JSObject* receiver,
2396 JSObject* holder,
2397 int index) {
2398 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002399 // -- eax : key
2400 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002403 Label miss;
2404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002405 __ IncrementCounter(&Counters::keyed_load_field, 1);
2406
2407 // Check that the name has not changed.
2408 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2409 __ j(not_equal, &miss, not_taken);
2410
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002411 GenerateLoadField(receiver, holder, edx, ebx, ecx, edi, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 __ bind(&miss);
2414 __ DecrementCounter(&Counters::keyed_load_field, 1);
2415 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2416
2417 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002418 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002419}
2420
2421
2422Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2423 JSObject* receiver,
2424 JSObject* holder,
2425 AccessorInfo* callback) {
2426 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002427 // -- eax : key
2428 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002429 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002430 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431 Label miss;
2432
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002433 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2434
2435 // Check that the name has not changed.
2436 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2437 __ j(not_equal, &miss, not_taken);
2438
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002439 Failure* failure = Failure::InternalError();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002440 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx, edi,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002441 callback, name, &miss, &failure);
2442 if (!success) return failure;
2443
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002444 __ bind(&miss);
2445 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2446 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2447
2448 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002449 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002450}
2451
2452
2453Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2454 JSObject* receiver,
2455 JSObject* holder,
2456 Object* value) {
2457 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002458 // -- eax : key
2459 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002460 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002461 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002462 Label miss;
2463
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002464 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2465
2466 // Check that the name has not changed.
2467 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2468 __ j(not_equal, &miss, not_taken);
2469
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002470 GenerateLoadConstant(receiver, holder, edx, ebx, ecx, edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002471 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 __ bind(&miss);
2473 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2474 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2475
2476 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002477 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002478}
2479
2480
2481Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2482 JSObject* holder,
2483 String* name) {
2484 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002485 // -- eax : key
2486 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002487 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002488 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002489 Label miss;
2490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002491 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2492
2493 // Check that the name has not changed.
2494 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2495 __ j(not_equal, &miss, not_taken);
2496
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002497 LookupResult lookup;
2498 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002499 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002500 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002501 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002502 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002503 eax,
2504 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002505 ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002506 edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002507 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002508 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002509 __ bind(&miss);
2510 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2511 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2512
2513 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002514 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002515}
2516
2517
2518
2519
2520Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2521 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002522 // -- eax : key
2523 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002524 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002525 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002526 Label miss;
2527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002528 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2529
2530 // Check that the name has not changed.
2531 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2532 __ j(not_equal, &miss, not_taken);
2533
ager@chromium.org5c838252010-02-19 08:53:10 +00002534 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002535 __ bind(&miss);
2536 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2537 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2538
2539 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002540 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002541}
2542
2543
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002544Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002545 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002546 // -- eax : key
2547 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002548 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002550 Label miss;
2551
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002552 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2553
2554 // Check that the name has not changed.
2555 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2556 __ j(not_equal, &miss, not_taken);
2557
ager@chromium.org5c838252010-02-19 08:53:10 +00002558 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002559 __ bind(&miss);
2560 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2561 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2562
2563 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002564 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002565}
2566
2567
2568Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2569 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002570 // -- eax : key
2571 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002572 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002574 Label miss;
2575
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002576 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2577
2578 // Check that the name has not changed.
2579 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2580 __ j(not_equal, &miss, not_taken);
2581
ager@chromium.org5c838252010-02-19 08:53:10 +00002582 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002583 __ bind(&miss);
2584 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2585 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2586
2587 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002588 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002589}
2590
2591
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002592// Specialized stub for constructing objects from functions which only have only
2593// simple assignments of the form this.x = ...; in their body.
2594Object* ConstructStubCompiler::CompileConstructStub(
2595 SharedFunctionInfo* shared) {
2596 // ----------- S t a t e -------------
2597 // -- eax : argc
2598 // -- edi : constructor
2599 // -- esp[0] : return address
2600 // -- esp[4] : last argument
2601 // -----------------------------------
2602 Label generic_stub_call;
2603#ifdef ENABLE_DEBUGGER_SUPPORT
2604 // Check to see whether there are any break points in the function code. If
2605 // there are jump to the generic constructor stub which calls the actual
2606 // code for the function thereby hitting the break points.
2607 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2608 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2609 __ cmp(ebx, Factory::undefined_value());
2610 __ j(not_equal, &generic_stub_call, not_taken);
2611#endif
2612
2613 // Load the initial map and verify that it is in fact a map.
2614 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2615 // Will both indicate a NULL and a Smi.
2616 __ test(ebx, Immediate(kSmiTagMask));
2617 __ j(zero, &generic_stub_call);
2618 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2619 __ j(not_equal, &generic_stub_call);
2620
2621#ifdef DEBUG
2622 // Cannot construct functions this way.
2623 // edi: constructor
2624 // ebx: initial map
2625 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2626 __ Assert(not_equal, "Function constructed by construct stub.");
2627#endif
2628
2629 // Now allocate the JSObject on the heap by moving the new space allocation
2630 // top forward.
2631 // edi: constructor
2632 // ebx: initial map
2633 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2634 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002635 __ AllocateInNewSpace(ecx,
2636 edx,
2637 ecx,
2638 no_reg,
2639 &generic_stub_call,
2640 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002641
2642 // Allocated the JSObject, now initialize the fields and add the heap tag.
2643 // ebx: initial map
2644 // edx: JSObject (untagged)
2645 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2646 __ mov(ebx, Factory::empty_fixed_array());
2647 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2648 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2649
2650 // Push the allocated object to the stack. This is the object that will be
2651 // returned (after it is tagged).
2652 __ push(edx);
2653
2654 // eax: argc
2655 // edx: JSObject (untagged)
2656 // Load the address of the first in-object property into edx.
2657 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2658 // Calculate the location of the first argument. The stack contains the
2659 // allocated object and the return address on top of the argc arguments.
2660 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2661
2662 // Use edi for holding undefined which is used in several places below.
2663 __ mov(edi, Factory::undefined_value());
2664
2665 // eax: argc
2666 // ecx: first argument
2667 // edx: first in-object property of the JSObject
2668 // edi: undefined
2669 // Fill the initialized properties with a constant value or a passed argument
2670 // depending on the this.x = ...; assignment in the function.
2671 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2672 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002673 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002674 // If argument is not passed the property is set to undefined,
2675 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002676 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002677 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002678 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002679 if (CpuFeatures::IsSupported(CMOV)) {
2680 CpuFeatures::Scope use_cmov(CMOV);
2681 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2682 } else {
2683 Label not_passed;
2684 __ j(below_equal, &not_passed);
2685 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2686 __ bind(&not_passed);
2687 }
2688 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002689 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002690 } else {
2691 // Set the property to the constant value.
2692 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2693 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2694 }
2695 }
2696
2697 // Fill the unused in-object property fields with undefined.
2698 for (int i = shared->this_property_assignments_count();
2699 i < shared->CalculateInObjectProperties();
2700 i++) {
2701 __ mov(Operand(edx, i * kPointerSize), edi);
2702 }
2703
2704 // Move argc to ebx and retrieve and tag the JSObject to return.
2705 __ mov(ebx, eax);
2706 __ pop(eax);
2707 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2708
2709 // Remove caller arguments and receiver from the stack and return.
2710 __ pop(ecx);
2711 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2712 __ push(ecx);
2713 __ IncrementCounter(&Counters::constructed_objects, 1);
2714 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2715 __ ret(0);
2716
2717 // Jump to the generic stub in case the specialized code cannot handle the
2718 // construction.
2719 __ bind(&generic_stub_call);
2720 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2721 Handle<Code> generic_construct_stub(code);
2722 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2723
2724 // Return the generated code.
2725 return GetCode();
2726}
2727
2728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002729#undef __
2730
2731} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002732
2733#endif // V8_TARGET_ARCH_IA32