blob: c6c65f076e236da7480c73692139ce7b54d04717 [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) {
260 // Load the global or builtins object from the current context.
261 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
262 // Load the global context from the global or builtins object.
263 __ mov(prototype,
264 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
265 // Load the function from the global context.
266 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
267 // Load the initial map. The global functions all have initial maps.
268 __ mov(prototype,
269 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
270 // Load the prototype from the initial map.
271 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
272}
273
274
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000275void StubCompiler::GenerateDirectLoadGlobalFunctionPrototype(
276 MacroAssembler* masm, int index, Register prototype) {
277 // Get the global function with the given index.
278 JSFunction* function = JSFunction::cast(Top::global_context()->get(index));
279 // Load its initial map. The global functions all have initial maps.
280 __ Set(prototype, Immediate(Handle<Map>(function->initial_map())));
281 // Load the prototype from the initial map.
282 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
283}
284
285
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
287 Register receiver,
288 Register scratch,
289 Label* miss_label) {
290 // Check that the receiver isn't a smi.
291 __ test(receiver, Immediate(kSmiTagMask));
292 __ j(zero, miss_label, not_taken);
293
294 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000295 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000296 __ j(not_equal, miss_label, not_taken);
297
298 // Load length directly from the JS array.
299 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
300 __ ret(0);
301}
302
303
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000304// Generate code to check if an object is a string. If the object is
305// a string, the map's instance type is left in the scratch register.
306static void GenerateStringCheck(MacroAssembler* masm,
307 Register receiver,
308 Register scratch,
309 Label* smi,
310 Label* non_string_object) {
311 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000312 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000313 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000314
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000315 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000316 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
317 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000318 ASSERT(kNotStringTag != 0);
319 __ test(scratch, Immediate(kNotStringTag));
320 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000321}
322
323
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000324void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
325 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000326 Register scratch1,
327 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000328 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000329 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000330
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000331 // Check if the object is a string leaving the instance type in the
332 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000333 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000334
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000335 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000336 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000337 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000338
339 // Check if the object is a JSValue wrapper.
340 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000341 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000342 __ j(not_equal, miss, not_taken);
343
344 // Check if the wrapped value is a string and load the length
345 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000346 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
347 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
348 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000349 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000350}
351
352
353void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
354 Register receiver,
355 Register scratch1,
356 Register scratch2,
357 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000358 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000359 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000360 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000361}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362
ager@chromium.org7c537e22008-10-16 08:43:32 +0000363
364// Load a fast property out of a holder object (src). In-object properties
365// are loaded directly otherwise the property is loaded from the properties
366// fixed array.
367void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000368 Register dst, Register src,
369 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000370 // Adjust for the number of properties stored in the holder.
371 index -= holder->map()->inobject_properties();
372 if (index < 0) {
373 // Get the property straight out of the holder.
374 int offset = holder->map()->instance_size() + (index * kPointerSize);
375 __ mov(dst, FieldOperand(src, offset));
376 } else {
377 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000378 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000379 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
380 __ mov(dst, FieldOperand(dst, offset));
381 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000382}
383
384
ager@chromium.org5c838252010-02-19 08:53:10 +0000385static void PushInterceptorArguments(MacroAssembler* masm,
386 Register receiver,
387 Register holder,
388 Register name,
389 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000390 __ push(name);
391 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
392 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000393 Register scratch = name;
394 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
395 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000396 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000397 __ push(holder);
398 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000399}
400
401
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000402static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
403 Register receiver,
404 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000405 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000406 JSObject* holder_obj) {
407 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000408 __ CallExternalReference(
409 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
410 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000411}
412
413
ager@chromium.org5c838252010-02-19 08:53:10 +0000414// Reserves space for the extra arguments to FastHandleApiCall in the
415// caller's frame.
416//
417// These arguments are set by CheckPrototypes and GenerateFastApiCall.
418static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
419 // ----------- S t a t e -------------
420 // -- esp[0] : return address
421 // -- esp[4] : last argument in the internal frame of the caller
422 // -----------------------------------
423 __ pop(scratch);
424 __ push(Immediate(Smi::FromInt(0)));
425 __ push(Immediate(Smi::FromInt(0)));
426 __ push(Immediate(Smi::FromInt(0)));
427 __ push(Immediate(Smi::FromInt(0)));
428 __ push(scratch);
429}
430
431
432// Undoes the effects of ReserveSpaceForFastApiCall.
433static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
434 // ----------- S t a t e -------------
435 // -- esp[0] : return address
436 // -- esp[4] : last fast api call extra argument
437 // -- ...
438 // -- esp[16] : first fast api call extra argument
439 // -- esp[20] : last argument in the internal frame
440 // -----------------------------------
441 __ pop(scratch);
442 __ add(Operand(esp), Immediate(kPointerSize * 4));
443 __ push(scratch);
444}
445
446
447// Generates call to FastHandleApiCall builtin.
448static void GenerateFastApiCall(MacroAssembler* masm,
449 const CallOptimization& optimization,
450 int argc) {
451 // ----------- S t a t e -------------
452 // -- esp[0] : return address
453 // -- esp[4] : object passing the type check
454 // (last fast api call extra argument,
455 // set by CheckPrototypes)
456 // -- esp[8] : api call data
457 // -- esp[12] : api callback
458 // -- esp[16] : api function
459 // (first fast api call extra argument)
460 // -- esp[20] : last argument
461 // -- ...
462 // -- esp[(argc + 5) * 4] : first argument
463 // -- esp[(argc + 6) * 4] : receiver
464 // -----------------------------------
465
466 // Get the function and setup the context.
467 JSFunction* function = optimization.constant_function();
468 __ mov(edi, Immediate(Handle<JSFunction>(function)));
469 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
470
471 // Pass the additional arguments FastHandleApiCall expects.
472 __ mov(Operand(esp, 4 * kPointerSize), edi);
473 bool info_loaded = false;
474 Object* callback = optimization.api_call_info()->callback();
475 if (Heap::InNewSpace(callback)) {
476 info_loaded = true;
477 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
478 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
479 __ mov(Operand(esp, 3 * kPointerSize), ebx);
480 } else {
481 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
482 }
483 Object* call_data = optimization.api_call_info()->data();
484 if (Heap::InNewSpace(call_data)) {
485 if (!info_loaded) {
486 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
487 }
488 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
489 __ mov(Operand(esp, 2 * kPointerSize), ebx);
490 } else {
491 __ mov(Operand(esp, 2 * kPointerSize),
492 Immediate(Handle<Object>(call_data)));
493 }
494
495 // Set the number of arguments.
496 __ mov(eax, Immediate(argc + 4));
497
498 // Jump to the fast api call builtin (tail call).
499 Handle<Code> code = Handle<Code>(
500 Builtins::builtin(Builtins::FastHandleApiCall));
501 ParameterCount expected(0);
502 __ InvokeCode(code, expected, expected,
503 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
504}
505
506
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000507class CallInterceptorCompiler BASE_EMBEDDED {
508 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000509 CallInterceptorCompiler(StubCompiler* stub_compiler,
510 const ParameterCount& arguments,
511 Register name)
512 : stub_compiler_(stub_compiler),
513 arguments_(arguments),
514 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000515
ager@chromium.org5c838252010-02-19 08:53:10 +0000516 void Compile(MacroAssembler* masm,
517 JSObject* object,
518 JSObject* holder,
519 String* name,
520 LookupResult* lookup,
521 Register receiver,
522 Register scratch1,
523 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000524 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000525 Label* miss) {
526 ASSERT(holder->HasNamedInterceptor());
527 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
528
529 // Check that the receiver isn't a smi.
530 __ test(receiver, Immediate(kSmiTagMask));
531 __ j(zero, miss, not_taken);
532
533 CallOptimization optimization(lookup);
534
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000535 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000536 CompileCacheable(masm,
537 object,
538 receiver,
539 scratch1,
540 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000541 scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000542 holder,
543 lookup,
544 name,
545 optimization,
546 miss);
547 } else {
548 CompileRegular(masm,
549 object,
550 receiver,
551 scratch1,
552 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000553 scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000554 name,
555 holder,
556 miss);
557 }
558 }
559
560 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000561 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000562 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000563 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000564 Register scratch1,
565 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000566 Register scratch3,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000567 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000568 LookupResult* lookup,
569 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000570 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000571 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000572 ASSERT(optimization.is_constant_call());
573 ASSERT(!lookup->holder()->IsGlobalObject());
574
575 int depth1 = kInvalidProtoDepth;
576 int depth2 = kInvalidProtoDepth;
577 bool can_do_fast_api_call = false;
578 if (optimization.is_simple_api_call() &&
579 !lookup->holder()->IsGlobalObject()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000580 depth1 =
581 optimization.GetPrototypeDepthOfExpectedType(object,
582 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000583 if (depth1 == kInvalidProtoDepth) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000584 depth2 =
585 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
586 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000587 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000588 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
589 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000590 }
591
ager@chromium.org5c838252010-02-19 08:53:10 +0000592 __ IncrementCounter(&Counters::call_const_interceptor, 1);
593
594 if (can_do_fast_api_call) {
595 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
596 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000597 }
598
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000599 // Check that the maps from receiver to interceptor's holder
600 // haven't changed and thus we can invoke interceptor.
ager@chromium.org5c838252010-02-19 08:53:10 +0000601 Label miss_cleanup;
602 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
603 Register holder =
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000604 stub_compiler_->CheckPrototypes(object, receiver,
605 interceptor_holder, scratch1,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000606 scratch2, scratch3, name, depth1, miss);
ager@chromium.org5c838252010-02-19 08:53:10 +0000607
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000608 // Invoke an interceptor and if it provides a value,
609 // branch to |regular_invoke|.
ager@chromium.org5c838252010-02-19 08:53:10 +0000610 Label regular_invoke;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000611 LoadWithInterceptor(masm, receiver, holder, interceptor_holder,
612 &regular_invoke);
ager@chromium.org5c838252010-02-19 08:53:10 +0000613
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000614 // Interceptor returned nothing for this property. Try to use cached
615 // constant function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000616
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000617 // Check that the maps from interceptor's holder to constant function's
618 // holder haven't changed and thus we can use cached constant function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000619 if (interceptor_holder != lookup->holder()) {
620 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
621 lookup->holder(), scratch1,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000622 scratch2, scratch3, name, depth2, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000623 } else {
624 // CheckPrototypes has a side effect of fetching a 'holder'
625 // for API (object which is instanceof for the signature). It's
626 // safe to omit it here, as if present, it should be fetched
627 // by the previous CheckPrototypes.
628 ASSERT(depth2 == kInvalidProtoDepth);
629 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000630
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000631 // Invoke function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000632 if (can_do_fast_api_call) {
633 GenerateFastApiCall(masm, optimization, arguments_.immediate());
634 } else {
635 __ InvokeFunction(optimization.constant_function(), arguments_,
636 JUMP_FUNCTION);
637 }
638
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000639 // Deferred code for fast API call case---clean preallocated space.
ager@chromium.org5c838252010-02-19 08:53:10 +0000640 if (can_do_fast_api_call) {
641 __ bind(&miss_cleanup);
642 FreeSpaceForFastApiCall(masm, scratch1);
643 __ jmp(miss_label);
644 }
645
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000646 // Invoke a regular function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000647 __ bind(&regular_invoke);
648 if (can_do_fast_api_call) {
649 FreeSpaceForFastApiCall(masm, scratch1);
650 }
651 }
652
653 void CompileRegular(MacroAssembler* masm,
654 JSObject* object,
655 Register receiver,
656 Register scratch1,
657 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000658 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +0000659 String* name,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000660 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000661 Label* miss_label) {
662 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000663 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000664 scratch1, scratch2, scratch3, name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000665 miss_label);
666
667 __ EnterInternalFrame();
668 // Save the name_ register across the call.
669 __ push(name_);
670
671 PushInterceptorArguments(masm,
672 receiver,
673 holder,
674 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000675 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000676
677 __ CallExternalReference(
678 ExternalReference(
679 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
680 5);
681
682 // Restore the name_ register.
683 __ pop(name_);
684 __ LeaveInternalFrame();
685 }
686
687 void LoadWithInterceptor(MacroAssembler* masm,
688 Register receiver,
689 Register holder,
690 JSObject* holder_obj,
691 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000692 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000693 __ push(holder); // Save the holder.
694 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000695
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000696 CompileCallLoadPropertyWithInterceptor(masm,
697 receiver,
698 holder,
699 name_,
700 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000701
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000702 __ pop(name_); // Restore the name.
703 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000704 __ LeaveInternalFrame();
705
706 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000707 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000708 }
709
ager@chromium.org5c838252010-02-19 08:53:10 +0000710 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000711 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000712 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000713};
714
715
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000716void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
717 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
718 Code* code = NULL;
719 if (kind == Code::LOAD_IC) {
720 code = Builtins::builtin(Builtins::LoadIC_Miss);
721 } else {
722 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
723 }
724
725 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000726 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000727}
728
729
ager@chromium.org5c838252010-02-19 08:53:10 +0000730// Both name_reg and receiver_reg are preserved on jumps to miss_label,
731// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000732void StubCompiler::GenerateStoreField(MacroAssembler* masm,
733 JSObject* object,
734 int index,
735 Map* transition,
736 Register receiver_reg,
737 Register name_reg,
738 Register scratch,
739 Label* miss_label) {
740 // Check that the object isn't a smi.
741 __ test(receiver_reg, Immediate(kSmiTagMask));
742 __ j(zero, miss_label, not_taken);
743
744 // Check that the map of the object hasn't changed.
745 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
746 Immediate(Handle<Map>(object->map())));
747 __ j(not_equal, miss_label, not_taken);
748
749 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000750 if (object->IsJSGlobalProxy()) {
751 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000752 }
753
754 // Stub never generated for non-global objects that require access
755 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000756 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000757
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000758 // Perform map transition for the receiver if necessary.
759 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
760 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000761 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000762 __ pop(scratch); // Return address.
763 __ push(receiver_reg);
764 __ push(Immediate(Handle<Map>(transition)));
765 __ push(eax);
766 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000767 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000768 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000769 return;
770 }
771
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000772 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000773 // Update the map of the object; no write barrier updating is
774 // needed because the map is never in new space.
775 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
776 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000777 }
778
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000779 // Adjust for the number of properties stored in the object. Even in the
780 // face of a transition we can use the old map here because the size of the
781 // object and the number of in-object properties is not going to change.
782 index -= object->map()->inobject_properties();
783
ager@chromium.org7c537e22008-10-16 08:43:32 +0000784 if (index < 0) {
785 // Set the property straight into the object.
786 int offset = object->map()->instance_size() + (index * kPointerSize);
787 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000788
ager@chromium.org7c537e22008-10-16 08:43:32 +0000789 // Update the write barrier for the array address.
790 // Pass the value being stored in the now unused name_reg.
791 __ mov(name_reg, Operand(eax));
792 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
793 } else {
794 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000795 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000796 // Get the properties array (optimistically).
797 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000798 __ mov(FieldOperand(scratch, offset), eax);
799
800 // Update the write barrier for the array address.
801 // Pass the value being stored in the now unused name_reg.
802 __ mov(name_reg, Operand(eax));
803 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
804 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000805
806 // Return the value (register eax).
807 __ ret(0);
808}
809
810
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000811// Generate code to check that a global property cell is empty. Create
812// the property cell at compilation time if no cell exists for the
813// property.
814static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
815 GlobalObject* global,
816 String* name,
817 Register scratch,
818 Label* miss) {
819 Object* probe = global->EnsurePropertyCell(name);
820 if (probe->IsFailure()) return probe;
821 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
822 ASSERT(cell->value()->IsTheHole());
823 __ mov(scratch, Immediate(Handle<Object>(cell)));
824 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
825 Immediate(Factory::the_hole_value()));
826 __ j(not_equal, miss, not_taken);
827 return cell;
828}
829
830
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000831// Calls GenerateCheckPropertyCell for each global object in the prototype chain
832// from object to (but not including) holder.
833static Object* GenerateCheckPropertyCells(MacroAssembler* masm,
834 JSObject* object,
835 JSObject* holder,
836 String* name,
837 Register scratch,
838 Label* miss) {
839 JSObject* current = object;
840 while (current != holder) {
841 if (current->IsGlobalObject()) {
842 Object* cell = GenerateCheckPropertyCell(masm,
843 GlobalObject::cast(current),
844 name,
845 scratch,
846 miss);
847 if (cell->IsFailure()) {
848 return cell;
849 }
850 }
851 ASSERT(current->IsJSObject());
852 current = JSObject::cast(current->GetPrototype());
853 }
854 return NULL;
855}
856
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000859#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000860
861
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000862Register StubCompiler::CheckPrototypes(JSObject* object,
863 Register object_reg,
864 JSObject* holder,
865 Register holder_reg,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000866 Register scratch1,
867 Register scratch2,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000868 String* name,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000869 int save_at_depth,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000870 Label* miss) {
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000871 // Make sure there's no overlap between holder and object registers.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000872 ASSERT(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
873 ASSERT(!scratch2.is(object_reg) && !scratch2.is(holder_reg)
874 && !scratch2.is(scratch1));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000875 // Keep track of the current object in register reg.
876 Register reg = object_reg;
877 JSObject* current = object;
878 int depth = 0;
879
880 if (save_at_depth == depth) {
881 __ mov(Operand(esp, kPointerSize), reg);
882 }
883
884 // Traverse the prototype chain and check the maps in the prototype chain for
885 // fast and global objects or do negative lookup for normal objects.
886 while (current != holder) {
887 depth++;
888
889 // Only global objects and objects that do not require access
890 // checks are allowed in stubs.
891 ASSERT(current->IsJSGlobalProxy() || !current->IsAccessCheckNeeded());
892
893 ASSERT(current->GetPrototype()->IsJSObject());
894 JSObject* prototype = JSObject::cast(current->GetPrototype());
895 if (!current->HasFastProperties() &&
896 !current->IsJSGlobalObject() &&
897 !current->IsJSGlobalProxy()) {
898 if (!name->IsSymbol()) {
899 Object* lookup_result = Heap::LookupSymbol(name);
900 if (lookup_result->IsFailure()) {
901 set_failure(Failure::cast(lookup_result));
902 return reg;
903 } else {
904 name = String::cast(lookup_result);
905 }
906 }
907 ASSERT(current->property_dictionary()->FindEntry(name) ==
908 StringDictionary::kNotFound);
909
910 GenerateDictionaryNegativeLookup(masm(),
911 miss,
912 reg,
913 name,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000914 scratch1,
915 scratch2);
916 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000917 reg = holder_reg; // from now the object is in holder_reg
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000918 __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000919 } else if (Heap::InNewSpace(prototype)) {
920 // Get the map of the current object.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000921 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
922 __ cmp(Operand(scratch1), Immediate(Handle<Map>(current->map())));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000923 // Branch on the result of the map check.
924 __ j(not_equal, miss, not_taken);
925 // Check access rights to the global object. This has to happen
926 // after the map check so that we know that the object is
927 // actually a global object.
928 if (current->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000929 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000930
931 // Restore scratch register to be the map of the object.
932 // We load the prototype from the map in the scratch register.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000933 __ mov(scratch1, FieldOperand(reg, HeapObject::kMapOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000934 }
935 // The prototype is in new space; we cannot store a reference
936 // to it in the code. Load it from the map.
937 reg = holder_reg; // from now the object is in holder_reg
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000938 __ mov(reg, FieldOperand(scratch1, Map::kPrototypeOffset));
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000939 } else {
940 // Check the map of the current object.
941 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
942 Immediate(Handle<Map>(current->map())));
943 // Branch on the result of the map check.
944 __ j(not_equal, miss, not_taken);
945 // Check access rights to the global object. This has to happen
946 // after the map check so that we know that the object is
947 // actually a global object.
948 if (current->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000949 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000950 }
951 // The prototype is in old space; load it directly.
952 reg = holder_reg; // from now the object is in holder_reg
953 __ mov(reg, Handle<JSObject>(prototype));
954 }
955
956 if (save_at_depth == depth) {
957 __ mov(Operand(esp, kPointerSize), reg);
958 }
959
960 // Go to the next object in the prototype chain.
961 current = prototype;
962 }
963 ASSERT(current == holder);
964
965 // Log the check depth.
966 LOG(IntEvent("check-maps-depth", depth + 1));
967
968 // Check the holder map.
969 __ cmp(FieldOperand(reg, HeapObject::kMapOffset),
970 Immediate(Handle<Map>(holder->map())));
971 __ j(not_equal, miss, not_taken);
972
973 // Perform security check for access to the global object.
974 ASSERT(holder->IsJSGlobalProxy() || !holder->IsAccessCheckNeeded());
975 if (holder->IsJSGlobalProxy()) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000976 __ CheckAccessGlobalProxy(reg, scratch1, miss);
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000977 };
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000978
979 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000980 // that their maps haven't changed. We also need to check that the
981 // property cell for the property is still empty.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000982 Object* result = GenerateCheckPropertyCells(masm(),
983 object,
984 holder,
985 name,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +0000986 scratch1,
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000987 miss);
988 if (result->IsFailure()) set_failure(Failure::cast(result));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000989
ager@chromium.org5c838252010-02-19 08:53:10 +0000990 // Return the register containing the holder.
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +0000991 return reg;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000992}
993
994
995void StubCompiler::GenerateLoadField(JSObject* object,
996 JSObject* holder,
997 Register receiver,
998 Register scratch1,
999 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001000 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001001 int index,
1002 String* name,
1003 Label* miss) {
1004 // Check that the receiver isn't a smi.
1005 __ test(receiver, Immediate(kSmiTagMask));
1006 __ j(zero, miss, not_taken);
1007
1008 // Check the prototype chain.
1009 Register reg =
1010 CheckPrototypes(object, receiver, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001011 scratch1, scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001012
1013 // Get the value from the properties.
1014 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1015 __ ret(0);
1016}
1017
1018
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001019bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001020 JSObject* holder,
1021 Register receiver,
1022 Register name_reg,
1023 Register scratch1,
1024 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001025 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001026 AccessorInfo* callback,
1027 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001028 Label* miss,
1029 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001030 // Check that the receiver isn't a smi.
1031 __ test(receiver, Immediate(kSmiTagMask));
1032 __ j(zero, miss, not_taken);
1033
1034 // Check that the maps haven't changed.
1035 Register reg =
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001036 CheckPrototypes(object, receiver, holder, scratch1,
1037 scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001038
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001039 Handle<AccessorInfo> callback_handle(callback);
1040
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001041 __ EnterInternalFrame();
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001042 __ PushHandleScope(scratch2);
1043 // Push the stack address where the list of arguments ends.
1044 __ mov(scratch2, esp);
1045 __ sub(Operand(scratch2), Immediate(2 * kPointerSize));
1046 __ push(scratch2);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001047 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001048 __ push(reg); // holder
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001049 // Push data from AccessorInfo.
1050 if (Heap::InNewSpace(callback_handle->data())) {
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001051 __ mov(scratch2, Immediate(callback_handle));
1052 __ push(FieldOperand(scratch2, AccessorInfo::kDataOffset));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +00001053 } else {
1054 __ push(Immediate(Handle<Object>(callback_handle->data())));
1055 }
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001056 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001057 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001058 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001059 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001060 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001061 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001062
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001063 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001064 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001065 Address getter_address = v8::ToCData<Address>(callback->getter());
1066 ApiFunction fun(getter_address);
1067 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001068 // Emitting a stub call may try to allocate (if the code is not
1069 // already generated). Do not allow the assembler to perform a
1070 // garbage collection but instead return the allocation failure
1071 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001072 Object* result = masm()->TryCallStub(&stub);
1073 if (result->IsFailure()) {
1074 *failure = Failure::cast(result);
1075 return false;
1076 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001077
1078 // We need to avoid using eax since that now holds the result.
whesse@chromium.orge90029b2010-08-02 11:52:17 +00001079 Register tmp = scratch2.is(eax) ? reg : scratch2;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001080 // Emitting PopHandleScope may try to allocate. Do not allow the
1081 // assembler to perform a garbage collection but instead return a
1082 // failure object.
1083 result = masm()->TryPopHandleScope(eax, tmp);
1084 if (result->IsFailure()) {
1085 *failure = Failure::cast(result);
1086 return false;
1087 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001088 __ LeaveInternalFrame();
1089
1090 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001091 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001092}
1093
1094
1095void StubCompiler::GenerateLoadConstant(JSObject* object,
1096 JSObject* holder,
1097 Register receiver,
1098 Register scratch1,
1099 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001100 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001101 Object* value,
1102 String* name,
1103 Label* miss) {
1104 // Check that the receiver isn't a smi.
1105 __ test(receiver, Immediate(kSmiTagMask));
1106 __ j(zero, miss, not_taken);
1107
1108 // Check that the maps haven't changed.
1109 Register reg =
1110 CheckPrototypes(object, receiver, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001111 scratch1, scratch2, scratch3, name, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001112
1113 // Return the constant value.
1114 __ mov(eax, Handle<Object>(value));
1115 __ ret(0);
1116}
1117
1118
1119void StubCompiler::GenerateLoadInterceptor(JSObject* object,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001120 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001121 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001122 Register receiver,
1123 Register name_reg,
1124 Register scratch1,
1125 Register scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001126 Register scratch3,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001127 String* name,
1128 Label* miss) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001129 ASSERT(interceptor_holder->HasNamedInterceptor());
1130 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
1131
1132 // Check that the receiver isn't a smi.
1133 __ test(receiver, Immediate(kSmiTagMask));
1134 __ j(zero, miss, not_taken);
1135
1136 // So far the most popular follow ups for interceptor loads are FIELD
1137 // and CALLBACKS, so inline only them, other cases may be added
1138 // later.
1139 bool compile_followup_inline = false;
1140 if (lookup->IsProperty() && lookup->IsCacheable()) {
1141 if (lookup->type() == FIELD) {
1142 compile_followup_inline = true;
1143 } else if (lookup->type() == CALLBACKS &&
1144 lookup->GetCallbackObject()->IsAccessorInfo() &&
1145 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
1146 compile_followup_inline = true;
1147 }
1148 }
1149
1150 if (compile_followup_inline) {
1151 // Compile the interceptor call, followed by inline code to load the
1152 // property from further up the prototype chain if the call fails.
1153 // Check that the maps haven't changed.
1154 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001155 scratch1, scratch2, scratch3,
1156 name, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001157 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
1158
1159 // Save necessary data before invoking an interceptor.
1160 // Requires a frame to make GC aware of pushed pointers.
1161 __ EnterInternalFrame();
1162
1163 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1164 // CALLBACKS case needs a receiver to be passed into C++ callback.
1165 __ push(receiver);
1166 }
1167 __ push(holder_reg);
1168 __ push(name_reg);
1169
1170 // Invoke an interceptor. Note: map checks from receiver to
1171 // interceptor's holder has been compiled before (see a caller
1172 // of this method.)
1173 CompileCallLoadPropertyWithInterceptor(masm(),
1174 receiver,
1175 holder_reg,
1176 name_reg,
1177 interceptor_holder);
1178
1179 // Check if interceptor provided a value for property. If it's
1180 // the case, return immediately.
1181 Label interceptor_failed;
1182 __ cmp(eax, Factory::no_interceptor_result_sentinel());
1183 __ j(equal, &interceptor_failed);
1184 __ LeaveInternalFrame();
1185 __ ret(0);
1186
1187 __ bind(&interceptor_failed);
1188 __ pop(name_reg);
1189 __ pop(holder_reg);
1190 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1191 __ pop(receiver);
1192 }
1193
1194 __ LeaveInternalFrame();
1195
1196 // Check that the maps from interceptor's holder to lookup's holder
1197 // haven't changed. And load lookup's holder into holder_reg.
1198 if (interceptor_holder != lookup->holder()) {
1199 holder_reg = CheckPrototypes(interceptor_holder,
1200 holder_reg,
1201 lookup->holder(),
1202 scratch1,
1203 scratch2,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001204 scratch3,
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001205 name,
1206 miss);
1207 }
1208
1209 if (lookup->type() == FIELD) {
1210 // We found FIELD property in prototype chain of interceptor's holder.
1211 // Retrieve a field from field's holder.
1212 GenerateFastPropertyLoad(masm(), eax, holder_reg,
1213 lookup->holder(), lookup->GetFieldIndex());
1214 __ ret(0);
1215 } else {
1216 // We found CALLBACKS property in prototype chain of interceptor's
1217 // holder.
1218 ASSERT(lookup->type() == CALLBACKS);
1219 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
1220 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
1221 ASSERT(callback != NULL);
1222 ASSERT(callback->getter() != NULL);
1223
1224 // Tail call to runtime.
1225 // Important invariant in CALLBACKS case: the code above must be
1226 // structured to never clobber |receiver| register.
1227 __ pop(scratch2); // return address
1228 __ push(receiver);
1229 __ push(holder_reg);
1230 __ mov(holder_reg, Immediate(Handle<AccessorInfo>(callback)));
1231 __ push(holder_reg);
1232 __ push(FieldOperand(holder_reg, AccessorInfo::kDataOffset));
1233 __ push(name_reg);
1234 __ push(scratch2); // restore return address
1235
1236 ExternalReference ref =
1237 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
1238 __ TailCallExternalReference(ref, 5, 1);
1239 }
1240 } else { // !compile_followup_inline
1241 // Call the runtime system to load the interceptor.
1242 // Check that the maps haven't changed.
1243 Register holder_reg =
1244 CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001245 scratch1, scratch2, scratch3, name, miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001246 __ pop(scratch2); // save old return address
1247 PushInterceptorArguments(masm(), receiver, holder_reg,
1248 name_reg, interceptor_holder);
1249 __ push(scratch2); // restore old return address
1250
1251 ExternalReference ref = ExternalReference(
1252 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
1253 __ TailCallExternalReference(ref, 5, 1);
1254 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001255}
1256
1257
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001258void CallStubCompiler::GenerateNameCheck(String* name, Label* miss) {
1259 if (kind_ == Code::KEYED_CALL_IC) {
1260 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1261 __ j(not_equal, miss, not_taken);
1262 }
1263}
1264
1265
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001266Object* CallStubCompiler::GenerateMissBranch() {
1267 Object* obj = StubCache::ComputeCallMiss(arguments().immediate(), kind_);
1268 if (obj->IsFailure()) return obj;
1269 __ jmp(Handle<Code>(Code::cast(obj)), RelocInfo::CODE_TARGET);
1270 return obj;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001271}
1272
1273
ager@chromium.org5c838252010-02-19 08:53:10 +00001274Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001275 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001276 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001277 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001279 // -- ecx : name
1280 // -- esp[0] : return address
1281 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1282 // -- ...
1283 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001284 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001285 Label miss;
1286
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001287 GenerateNameCheck(name, &miss);
1288
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001289 // Get the receiver from the stack.
1290 const int argc = arguments().immediate();
1291 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1292
1293 // Check that the receiver isn't a smi.
1294 __ test(edx, Immediate(kSmiTagMask));
1295 __ j(zero, &miss, not_taken);
1296
1297 // Do the right check and compute the holder register.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001298 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, edi,
1299 name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300
ager@chromium.org7c537e22008-10-16 08:43:32 +00001301 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001302
1303 // Check that the function really is a function.
1304 __ test(edi, Immediate(kSmiTagMask));
1305 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001306 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001307 __ j(not_equal, &miss, not_taken);
1308
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001309 // Patch the receiver on the stack with the global proxy if
1310 // necessary.
1311 if (object->IsGlobalObject()) {
1312 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1313 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1314 }
1315
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001316 // Invoke the function.
1317 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1318
1319 // Handle call cache miss.
1320 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001321 Object* obj = GenerateMissBranch();
1322 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001323
1324 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001325 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001326}
1327
1328
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001329Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1330 JSObject* holder,
1331 JSFunction* function,
1332 String* name,
1333 CheckType check) {
1334 // ----------- S t a t e -------------
1335 // -- ecx : name
1336 // -- esp[0] : return address
1337 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1338 // -- ...
1339 // -- esp[(argc + 1) * 4] : receiver
1340 // -----------------------------------
1341 ASSERT(check == RECEIVER_MAP_CHECK);
1342
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001343 // If object is not an array, bail out to regular call.
1344 if (!object->IsJSArray()) {
1345 return Heap::undefined_value();
1346 }
1347
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001348 Label miss;
1349
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001350 GenerateNameCheck(name, &miss);
1351
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001352 // Get the receiver from the stack.
1353 const int argc = arguments().immediate();
1354 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1355
1356 // Check that the receiver isn't a smi.
1357 __ test(edx, Immediate(kSmiTagMask));
1358 __ j(zero, &miss);
1359
1360 CheckPrototypes(JSObject::cast(object), edx,
1361 holder, ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001362 eax, edi, name, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001363
1364 if (argc == 0) {
1365 // Noop, return the length.
1366 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1367 __ ret((argc + 1) * kPointerSize);
1368 } else {
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001369 Label call_builtin;
1370
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001371 // Get the elements array of the object.
1372 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1373
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001374 // Check that the elements are in fast mode and writable.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001375 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1376 Immediate(Factory::fixed_array_map()));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001377 __ j(not_equal, &call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001378
1379 if (argc == 1) { // Otherwise fall through to call builtin.
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001380 Label exit, with_write_barrier, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001381
1382 // Get the array's length into eax and calculate new length.
1383 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1384 STATIC_ASSERT(kSmiTagSize == 1);
1385 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001386 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001387
1388 // Get the element's length into ecx.
1389 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001390
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001391 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001392 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001393 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001394
1395 // Save new length.
1396 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1397
1398 // Push the element.
1399 __ lea(edx, FieldOperand(ebx,
1400 eax, times_half_pointer_size,
1401 FixedArray::kHeaderSize - argc * kPointerSize));
1402 __ mov(ecx, Operand(esp, argc * kPointerSize));
1403 __ mov(Operand(edx, 0), ecx);
1404
1405 // Check if value is a smi.
1406 __ test(ecx, Immediate(kSmiTagMask));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001407 __ j(not_zero, &with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001408
1409 __ bind(&exit);
1410 __ ret((argc + 1) * kPointerSize);
1411
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001412 __ bind(&with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001413
1414 __ InNewSpace(ebx, ecx, equal, &exit);
1415
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001416 __ RecordWriteHelper(ebx, edx, ecx);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001417 __ ret((argc + 1) * kPointerSize);
1418
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001419 __ bind(&attempt_to_grow_elements);
1420 ExternalReference new_space_allocation_top =
1421 ExternalReference::new_space_allocation_top_address();
1422 ExternalReference new_space_allocation_limit =
1423 ExternalReference::new_space_allocation_limit_address();
1424
1425 const int kAllocationDelta = 4;
1426 // Load top.
1427 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1428
1429 // Check if it's the end of elements.
1430 __ lea(edx, FieldOperand(ebx,
1431 eax, times_half_pointer_size,
1432 FixedArray::kHeaderSize - argc * kPointerSize));
1433 __ cmp(edx, Operand(ecx));
1434 __ j(not_equal, &call_builtin);
1435 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1436 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001437 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001438
1439 // We fit and could grow elements.
1440 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1441 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001442
1443 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001444 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001445 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001446 for (int i = 1; i < kAllocationDelta; i++) {
1447 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001448 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001449 }
1450
1451 // Restore receiver to edx as finish sequence assumes it's here.
1452 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1453
1454 // Increment element's and array's sizes.
1455 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001456 Immediate(Smi::FromInt(kAllocationDelta)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001457 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1458
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001459 // Elements are in new space, so write barrier is not required.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001460 __ ret((argc + 1) * kPointerSize);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001461 }
1462
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001463 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001464 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1465 argc + 1,
1466 1);
1467 }
1468
1469 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001470 Object* obj = GenerateMissBranch();
1471 if (obj->IsFailure()) return obj;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001472
1473 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001474 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001475}
1476
1477
1478Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1479 JSObject* holder,
1480 JSFunction* function,
1481 String* name,
1482 CheckType check) {
1483 // ----------- S t a t e -------------
1484 // -- ecx : name
1485 // -- esp[0] : return address
1486 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1487 // -- ...
1488 // -- esp[(argc + 1) * 4] : receiver
1489 // -----------------------------------
1490 ASSERT(check == RECEIVER_MAP_CHECK);
1491
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001492 // If object is not an array, bail out to regular call.
1493 if (!object->IsJSArray()) {
1494 return Heap::undefined_value();
1495 }
1496
ager@chromium.orgac091b72010-05-05 07:34:42 +00001497 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001498
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001499 GenerateNameCheck(name, &miss);
1500
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001501 // Get the receiver from the stack.
1502 const int argc = arguments().immediate();
1503 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1504
1505 // Check that the receiver isn't a smi.
1506 __ test(edx, Immediate(kSmiTagMask));
1507 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001508 CheckPrototypes(JSObject::cast(object), edx,
1509 holder, ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001510 eax, edi, name, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001511
1512 // Get the elements array of the object.
1513 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1514
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001515 // Check that the elements are in fast mode and writable.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001516 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1517 Immediate(Factory::fixed_array_map()));
ricow@chromium.org0b9f8502010-08-18 07:45:01 +00001518 __ j(not_equal, &call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001519
1520 // Get the array's length into ecx and calculate new length.
1521 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1522 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001523 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001524
1525 // Get the last element.
1526 STATIC_ASSERT(kSmiTagSize == 1);
1527 STATIC_ASSERT(kSmiTag == 0);
1528 __ mov(eax, FieldOperand(ebx,
1529 ecx, times_half_pointer_size,
1530 FixedArray::kHeaderSize));
1531 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1532 __ j(equal, &call_builtin);
1533
1534 // Set the array's length.
1535 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1536
1537 // Fill with the hole.
1538 __ mov(FieldOperand(ebx,
1539 ecx, times_half_pointer_size,
1540 FixedArray::kHeaderSize),
1541 Immediate(Factory::the_hole_value()));
1542 __ ret((argc + 1) * kPointerSize);
1543
ager@chromium.orgac091b72010-05-05 07:34:42 +00001544 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001545 __ mov(eax, Immediate(Factory::undefined_value()));
1546 __ ret((argc + 1) * kPointerSize);
1547
1548 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001549 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1550 argc + 1,
1551 1);
1552
1553 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001554 Object* obj = GenerateMissBranch();
1555 if (obj->IsFailure()) return obj;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001556
1557 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001558 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001559}
1560
1561
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001562Object* CallStubCompiler::CompileStringCharCodeAtCall(Object* object,
1563 JSObject* holder,
1564 JSFunction* function,
1565 String* name,
1566 CheckType check) {
1567 // ----------- S t a t e -------------
1568 // -- ecx : function name
1569 // -- esp[0] : return address
1570 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1571 // -- ...
1572 // -- esp[(argc + 1) * 4] : receiver
1573 // -----------------------------------
1574
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001575 // If object is not a string, bail out to regular call.
1576 if (!object->IsString()) return Heap::undefined_value();
1577
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001578 const int argc = arguments().immediate();
1579
1580 Label miss;
1581 Label index_out_of_range;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001582 GenerateNameCheck(name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001583
1584 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001585 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1586 Context::STRING_FUNCTION_INDEX,
1587 eax);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001588 ASSERT(object != holder);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001589 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001590 ebx, edx, edi, name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001591
1592 Register receiver = ebx;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001593 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001594 Register scratch = edx;
1595 Register result = eax;
1596 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1597 if (argc > 0) {
1598 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1599 } else {
1600 __ Set(index, Immediate(Factory::undefined_value()));
1601 }
1602
1603 StringCharCodeAtGenerator char_code_at_generator(receiver,
1604 index,
1605 scratch,
1606 result,
1607 &miss, // When not a string.
1608 &miss, // When not a number.
1609 &index_out_of_range,
1610 STRING_INDEX_IS_NUMBER);
1611 char_code_at_generator.GenerateFast(masm());
1612 __ ret((argc + 1) * kPointerSize);
1613
1614 ICRuntimeCallHelper call_helper;
1615 char_code_at_generator.GenerateSlow(masm(), call_helper);
1616
1617 __ bind(&index_out_of_range);
1618 __ Set(eax, Immediate(Factory::nan_value()));
1619 __ ret((argc + 1) * kPointerSize);
1620
1621 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001622 Object* obj = GenerateMissBranch();
1623 if (obj->IsFailure()) return obj;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001624
1625 // Return the generated code.
1626 return GetCode(function);
1627}
1628
1629
1630Object* CallStubCompiler::CompileStringCharAtCall(Object* object,
1631 JSObject* holder,
1632 JSFunction* function,
1633 String* name,
1634 CheckType check) {
1635 // ----------- S t a t e -------------
1636 // -- ecx : function name
1637 // -- esp[0] : return address
1638 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1639 // -- ...
1640 // -- esp[(argc + 1) * 4] : receiver
1641 // -----------------------------------
1642
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001643 // If object is not a string, bail out to regular call.
1644 if (!object->IsString()) return Heap::undefined_value();
1645
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001646 const int argc = arguments().immediate();
1647
1648 Label miss;
1649 Label index_out_of_range;
1650
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001651 GenerateNameCheck(name, &miss);
1652
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001653 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001654 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1655 Context::STRING_FUNCTION_INDEX,
1656 eax);
ager@chromium.orgea4f62e2010-08-16 16:28:43 +00001657 ASSERT(object != holder);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001658 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001659 ebx, edx, edi, name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001660
1661 Register receiver = eax;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001662 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001663 Register scratch1 = ebx;
1664 Register scratch2 = edx;
1665 Register result = eax;
1666 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1667 if (argc > 0) {
1668 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1669 } else {
1670 __ Set(index, Immediate(Factory::undefined_value()));
1671 }
1672
1673 StringCharAtGenerator char_at_generator(receiver,
1674 index,
1675 scratch1,
1676 scratch2,
1677 result,
1678 &miss, // When not a string.
1679 &miss, // When not a number.
1680 &index_out_of_range,
1681 STRING_INDEX_IS_NUMBER);
1682 char_at_generator.GenerateFast(masm());
1683 __ ret((argc + 1) * kPointerSize);
1684
1685 ICRuntimeCallHelper call_helper;
1686 char_at_generator.GenerateSlow(masm(), call_helper);
1687
1688 __ bind(&index_out_of_range);
1689 __ Set(eax, Immediate(Factory::empty_string()));
1690 __ ret((argc + 1) * kPointerSize);
1691
1692 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001693 Object* obj = GenerateMissBranch();
1694 if (obj->IsFailure()) return obj;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001695
1696 // Return the generated code.
1697 return GetCode(function);
1698}
1699
1700
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701Object* CallStubCompiler::CompileCallConstant(Object* object,
1702 JSObject* holder,
1703 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001704 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001705 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001707 // -- ecx : name
1708 // -- esp[0] : return address
1709 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1710 // -- ...
1711 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001712 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001713
1714 SharedFunctionInfo* function_info = function->shared();
1715 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001716 const int id = function_info->custom_call_generator_id();
1717 Object* result =
1718 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001719 // undefined means bail out to regular compiler.
1720 if (!result->IsUndefined()) {
1721 return result;
1722 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001723 }
1724
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001725 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001726
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001727 GenerateNameCheck(name, &miss_in_smi_check);
1728
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 // Get the receiver from the stack.
1730 const int argc = arguments().immediate();
1731 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1732
1733 // Check that the receiver isn't a smi.
1734 if (check != NUMBER_CHECK) {
1735 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001736 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 }
1738
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001739 // Make sure that it's okay not to patch the on stack receiver
1740 // unless we're doing a receiver map check.
1741 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1742
ager@chromium.org5c838252010-02-19 08:53:10 +00001743 CallOptimization optimization(function);
1744 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001745 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 switch (check) {
1748 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001749 __ IncrementCounter(&Counters::call_const, 1);
1750
1751 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1752 depth = optimization.GetPrototypeDepthOfExpectedType(
1753 JSObject::cast(object), holder);
1754 }
1755
1756 if (depth != kInvalidProtoDepth) {
1757 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1758 ReserveSpaceForFastApiCall(masm(), eax);
1759 }
1760
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001762 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001763 ebx, eax, edi, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001764
1765 // Patch the receiver on the stack with the global proxy if
1766 // necessary.
1767 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001768 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001769 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1770 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1771 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772 break;
1773
1774 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001775 if (!function->IsBuiltin()) {
1776 // Calling non-builtins with a value as receiver requires boxing.
1777 __ jmp(&miss);
1778 } else {
1779 // Check that the object is a string or a symbol.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001780 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001781 __ j(above_equal, &miss, not_taken);
1782 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001783 GenerateDirectLoadGlobalFunctionPrototype(
1784 masm(), Context::STRING_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001785 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001786 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001787 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 break;
1789
1790 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 if (!function->IsBuiltin()) {
1792 // Calling non-builtins with a value as receiver requires boxing.
1793 __ jmp(&miss);
1794 } else {
1795 Label fast;
1796 // Check that the object is a smi or a heap number.
1797 __ test(edx, Immediate(kSmiTagMask));
1798 __ j(zero, &fast, taken);
1799 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1800 __ j(not_equal, &miss, not_taken);
1801 __ bind(&fast);
1802 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001803 GenerateDirectLoadGlobalFunctionPrototype(
1804 masm(), Context::NUMBER_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001805 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001806 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001807 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 break;
1809 }
1810
1811 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001812 if (!function->IsBuiltin()) {
1813 // Calling non-builtins with a value as receiver requires boxing.
1814 __ jmp(&miss);
1815 } else {
1816 Label fast;
1817 // Check that the object is a boolean.
1818 __ cmp(edx, Factory::true_value());
1819 __ j(equal, &fast, taken);
1820 __ cmp(edx, Factory::false_value());
1821 __ j(not_equal, &miss, not_taken);
1822 __ bind(&fast);
1823 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001824 GenerateDirectLoadGlobalFunctionPrototype(
1825 masm(), Context::BOOLEAN_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001826 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001827 ebx, edx, edi, name, &miss);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001828 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829 break;
1830 }
1831
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 default:
1833 UNREACHABLE();
1834 }
1835
ager@chromium.org5c838252010-02-19 08:53:10 +00001836 if (depth != kInvalidProtoDepth) {
1837 GenerateFastApiCall(masm(), optimization, argc);
1838 } else {
1839 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1840 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841
1842 // Handle call cache miss.
1843 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001844 if (depth != kInvalidProtoDepth) {
1845 FreeSpaceForFastApiCall(masm(), eax);
1846 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001847 __ bind(&miss_in_smi_check);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001848 Object* obj = GenerateMissBranch();
1849 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001852 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853}
1854
1855
ager@chromium.org5c838252010-02-19 08:53:10 +00001856Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857 JSObject* holder,
1858 String* name) {
1859 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001860 // -- ecx : name
1861 // -- esp[0] : return address
1862 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1863 // -- ...
1864 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 Label miss;
1867
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001868 GenerateNameCheck(name, &miss);
1869
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 // Get the number of arguments.
1871 const int argc = arguments().immediate();
1872
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001873 LookupResult lookup;
1874 LookupPostInterceptor(holder, name, &lookup);
1875
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 // Get the receiver from the stack.
1877 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001878
ager@chromium.org5c838252010-02-19 08:53:10 +00001879 CallInterceptorCompiler compiler(this, arguments(), ecx);
1880 compiler.Compile(masm(),
1881 object,
1882 holder,
1883 name,
1884 &lookup,
1885 edx,
1886 ebx,
1887 edi,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001888 eax,
ager@chromium.org5c838252010-02-19 08:53:10 +00001889 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001891 // Restore receiver.
1892 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001893
1894 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001895 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001897 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 __ j(not_equal, &miss, not_taken);
1899
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001900 // Patch the receiver on the stack with the global proxy if
1901 // necessary.
1902 if (object->IsGlobalObject()) {
1903 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1904 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1905 }
1906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001908 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1910
1911 // Handle load cache miss.
1912 __ bind(&miss);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001913 Object* obj = GenerateMissBranch();
1914 if (obj->IsFailure()) return obj;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915
1916 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001917 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918}
1919
1920
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001921Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1922 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001923 JSGlobalPropertyCell* cell,
1924 JSFunction* function,
1925 String* name) {
1926 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001927 // -- ecx : name
1928 // -- esp[0] : return address
1929 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1930 // -- ...
1931 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001932 // -----------------------------------
1933 Label miss;
1934
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001935 GenerateNameCheck(name, &miss);
1936
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001937 // Get the number of arguments.
1938 const int argc = arguments().immediate();
1939
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001940 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001941 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001942
1943 // If the object is the holder then we know that it's a global
1944 // object which can only happen for contextual calls. In this case,
1945 // the receiver cannot be a smi.
1946 if (object != holder) {
1947 __ test(edx, Immediate(kSmiTagMask));
1948 __ j(zero, &miss, not_taken);
1949 }
1950
1951 // Check that the maps haven't changed.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00001952 CheckPrototypes(object, edx, holder, ebx, eax, edi, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001953
1954 // Get the value from the cell.
1955 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1956 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1957
1958 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001959 if (Heap::InNewSpace(function)) {
1960 // We can't embed a pointer to a function in new space so we have
1961 // to verify that the shared function info is unchanged. This has
1962 // the nice side effect that multiple closures based on the same
1963 // function can all use this call IC. Before we load through the
1964 // function, we have to verify that it still is a function.
1965 __ test(edi, Immediate(kSmiTagMask));
1966 __ j(zero, &miss, not_taken);
1967 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1968 __ j(not_equal, &miss, not_taken);
1969
1970 // Check the shared function info. Make sure it hasn't changed.
1971 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1972 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1973 __ j(not_equal, &miss, not_taken);
1974 } else {
1975 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1976 __ j(not_equal, &miss, not_taken);
1977 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001978
1979 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001980 if (object->IsGlobalObject()) {
1981 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1982 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1983 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001984
1985 // Setup the context (function already in edi).
1986 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1987
1988 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001989 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001990 ASSERT(function->is_compiled());
1991 Handle<Code> code(function->code());
1992 ParameterCount expected(function->shared()->formal_parameter_count());
1993 __ InvokeCode(code, expected, arguments(),
1994 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1995
1996 // Handle call cache miss.
1997 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001998 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
lrn@chromium.orgc4e51ac2010-08-09 09:47:21 +00001999 Object* obj = GenerateMissBranch();
2000 if (obj->IsFailure()) return obj;
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002001
2002 // Return the generated code.
2003 return GetCode(NORMAL, name);
2004}
2005
2006
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007Object* StoreStubCompiler::CompileStoreField(JSObject* object,
2008 int index,
2009 Map* transition,
2010 String* name) {
2011 // ----------- S t a t e -------------
2012 // -- eax : value
2013 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002014 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 Label miss;
2018
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002019 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002020 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002021 object,
2022 index,
2023 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002024 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002025 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026
2027 // Handle store cache miss.
2028 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00002029 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002031 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032
2033 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002034 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035}
2036
2037
2038Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
2039 AccessorInfo* callback,
2040 String* name) {
2041 // ----------- S t a t e -------------
2042 // -- eax : value
2043 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002044 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 Label miss;
2048
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002050 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 __ j(zero, &miss, not_taken);
2052
2053 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002054 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 Immediate(Handle<Map>(object->map())));
2056 __ j(not_equal, &miss, not_taken);
2057
2058 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002059 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002060 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 }
2062
2063 // Stub never generated for non-global objects that require access
2064 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002065 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066
2067 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002068 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002069 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
2070 __ push(ecx); // name
2071 __ push(eax); // value
2072 __ push(ebx); // restore return address
2073
mads.s.ager31e71382008-08-13 09:32:07 +00002074 // Do tail-call to the runtime system.
2075 ExternalReference store_callback_property =
2076 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002077 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078
2079 // Handle store cache miss.
2080 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002082 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083
2084 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086}
2087
2088
2089Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
2090 String* name) {
2091 // ----------- S t a t e -------------
2092 // -- eax : value
2093 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002094 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 Label miss;
2098
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002099 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002100 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 __ j(zero, &miss, not_taken);
2102
2103 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002104 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 Immediate(Handle<Map>(receiver->map())));
2106 __ j(not_equal, &miss, not_taken);
2107
2108 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002109 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002110 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111 }
2112
2113 // Stub never generated for non-global objects that require access
2114 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00002115 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116
2117 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002118 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 __ push(ecx); // name
2120 __ push(eax); // value
2121 __ push(ebx); // restore return address
2122
mads.s.ager31e71382008-08-13 09:32:07 +00002123 // Do tail-call to the runtime system.
2124 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002126 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002127
2128 // Handle store cache miss.
2129 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002130 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002131 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132
2133 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002134 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135}
2136
2137
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002138Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
2139 JSGlobalPropertyCell* cell,
2140 String* name) {
2141 // ----------- S t a t e -------------
2142 // -- eax : value
2143 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002144 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002145 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002146 // -----------------------------------
2147 Label miss;
2148
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002149 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00002150 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002151 Immediate(Handle<Map>(object->map())));
2152 __ j(not_equal, &miss, not_taken);
2153
2154 // Store the value in the cell.
2155 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2156 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
2157
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002158 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002159 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002160 __ ret(0);
2161
2162 // Handle store cache miss.
2163 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002164 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
2165 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
2166 __ jmp(ic, RelocInfo::CODE_TARGET);
2167
2168 // Return the generated code.
2169 return GetCode(NORMAL, name);
2170}
2171
2172
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
2174 int index,
2175 Map* transition,
2176 String* name) {
2177 // ----------- S t a t e -------------
2178 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00002179 // -- ecx : key
2180 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 Label miss;
2184
2185 __ IncrementCounter(&Counters::keyed_store_field, 1);
2186
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187 // Check that the name has not changed.
2188 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
2189 __ j(not_equal, &miss, not_taken);
2190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002192 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002193 object,
2194 index,
2195 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00002196 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00002197 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198
2199 // Handle store cache miss.
2200 __ bind(&miss);
2201 __ DecrementCounter(&Counters::keyed_store_field, 1);
2202 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00002203 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002204
2205 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002206 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207}
2208
2209
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002210Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
2211 JSObject* object,
2212 JSObject* last) {
2213 // ----------- S t a t e -------------
2214 // -- eax : receiver
2215 // -- ecx : name
2216 // -- esp[0] : return address
2217 // -----------------------------------
2218 Label miss;
2219
2220 // Check that the receiver isn't a smi.
2221 __ test(eax, Immediate(kSmiTagMask));
2222 __ j(zero, &miss, not_taken);
2223
kmillikin@chromium.org69ea3962010-07-05 11:01:40 +00002224 ASSERT(last->IsGlobalObject() || last->HasFastProperties());
2225
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002226 // Check the maps of the full prototype chain. Also check that
2227 // global property cells up to (but not including) the last object
2228 // in the prototype chain are empty.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002229 CheckPrototypes(object, eax, last, ebx, edx, edi, name, &miss);
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00002230
2231 // If the last object in the prototype chain is a global object,
2232 // check that the global property cell is empty.
2233 if (last->IsGlobalObject()) {
2234 Object* cell = GenerateCheckPropertyCell(masm(),
2235 GlobalObject::cast(last),
2236 name,
2237 edx,
2238 &miss);
2239 if (cell->IsFailure()) return cell;
2240 }
2241
2242 // Return undefined if maps of the full prototype chain are still the
2243 // same and no global property with this name contains a value.
2244 __ mov(eax, Factory::undefined_value());
2245 __ ret(0);
2246
2247 __ bind(&miss);
2248 GenerateLoadMiss(masm(), Code::LOAD_IC);
2249
2250 // Return the generated code.
2251 return GetCode(NONEXISTENT, Heap::empty_string());
2252}
2253
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2256 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002257 int index,
2258 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002260 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 // -- ecx : name
2262 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 Label miss;
2265
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002266 GenerateLoadField(object, holder, eax, ebx, edx, edi, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 __ bind(&miss);
2268 GenerateLoadMiss(masm(), Code::LOAD_IC);
2269
2270 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002271 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272}
2273
2274
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002275Object* LoadStubCompiler::CompileLoadCallback(String* name,
2276 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002278 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002280 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002281 // -- ecx : name
2282 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002283 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 Label miss;
2285
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002286 Failure* failure = Failure::InternalError();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002287 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx, edi,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002288 callback, name, &miss, &failure);
2289 if (!success) return failure;
2290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 __ bind(&miss);
2292 GenerateLoadMiss(masm(), Code::LOAD_IC);
2293
2294 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002295 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296}
2297
2298
2299Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2300 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002301 Object* value,
2302 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002304 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 // -- ecx : name
2306 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002307 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 Label miss;
2309
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002310 GenerateLoadConstant(object, holder, eax, ebx, edx, edi, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 __ bind(&miss);
2312 GenerateLoadMiss(masm(), Code::LOAD_IC);
2313
2314 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316}
2317
2318
2319Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2320 JSObject* holder,
2321 String* name) {
2322 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002323 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 // -- ecx : name
2325 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 Label miss;
2328
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002329 LookupResult lookup;
2330 LookupPostInterceptor(holder, name, &lookup);
2331
ager@chromium.orge2902be2009-06-08 12:21:35 +00002332 // TODO(368): Compile in the whole chain: all the interceptors in
2333 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002334 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002335 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002336 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002337 eax,
2338 ecx,
2339 edx,
2340 ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002341 edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002342 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002343 &miss);
2344
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 __ bind(&miss);
2346 GenerateLoadMiss(masm(), Code::LOAD_IC);
2347
2348 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002349 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350}
2351
2352
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002353Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2354 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002355 JSGlobalPropertyCell* cell,
2356 String* name,
2357 bool is_dont_delete) {
2358 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002359 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002360 // -- ecx : name
2361 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002362 // -----------------------------------
2363 Label miss;
2364
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002365 // If the object is the holder then we know that it's a global
2366 // object which can only happen for contextual loads. In this case,
2367 // the receiver cannot be a smi.
2368 if (object != holder) {
2369 __ test(eax, Immediate(kSmiTagMask));
2370 __ j(zero, &miss, not_taken);
2371 }
2372
2373 // Check that the maps haven't changed.
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002374 CheckPrototypes(object, eax, holder, ebx, edx, edi, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002375
2376 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002377 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2378 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002379
2380 // Check for deleted property if property can actually be deleted.
2381 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002382 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002383 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002384 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002385 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002386 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002387 }
2388
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002389 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002390 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002391 __ ret(0);
2392
2393 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002394 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2395 GenerateLoadMiss(masm(), Code::LOAD_IC);
2396
2397 // Return the generated code.
2398 return GetCode(NORMAL, name);
2399}
2400
2401
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002402Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2403 JSObject* receiver,
2404 JSObject* holder,
2405 int index) {
2406 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002407 // -- eax : key
2408 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002409 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002410 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002411 Label miss;
2412
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002413 __ IncrementCounter(&Counters::keyed_load_field, 1);
2414
2415 // Check that the name has not changed.
2416 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2417 __ j(not_equal, &miss, not_taken);
2418
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002419 GenerateLoadField(receiver, holder, edx, ebx, ecx, edi, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002420
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002421 __ bind(&miss);
2422 __ DecrementCounter(&Counters::keyed_load_field, 1);
2423 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2424
2425 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002426 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002427}
2428
2429
2430Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2431 JSObject* receiver,
2432 JSObject* holder,
2433 AccessorInfo* callback) {
2434 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002435 // -- eax : key
2436 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002437 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002438 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002439 Label miss;
2440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002441 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2442
2443 // Check that the name has not changed.
2444 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2445 __ j(not_equal, &miss, not_taken);
2446
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002447 Failure* failure = Failure::InternalError();
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002448 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx, edi,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002449 callback, name, &miss, &failure);
2450 if (!success) return failure;
2451
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002452 __ bind(&miss);
2453 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2454 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2455
2456 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002457 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002458}
2459
2460
2461Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2462 JSObject* receiver,
2463 JSObject* holder,
2464 Object* value) {
2465 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002466 // -- eax : key
2467 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002468 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002469 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002470 Label miss;
2471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002472 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2473
2474 // Check that the name has not changed.
2475 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2476 __ j(not_equal, &miss, not_taken);
2477
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002478 GenerateLoadConstant(receiver, holder, edx, ebx, ecx, edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002479 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002480 __ bind(&miss);
2481 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2482 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2483
2484 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002485 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002486}
2487
2488
2489Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2490 JSObject* holder,
2491 String* name) {
2492 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002493 // -- eax : key
2494 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002495 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002496 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002497 Label miss;
2498
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002499 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2500
2501 // Check that the name has not changed.
2502 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2503 __ j(not_equal, &miss, not_taken);
2504
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002505 LookupResult lookup;
2506 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002507 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002508 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002509 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002510 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002511 eax,
2512 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002513 ebx,
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002514 edi,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002515 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002516 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002517 __ bind(&miss);
2518 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2519 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2520
2521 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002522 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002523}
2524
2525
2526
2527
2528Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2529 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002530 // -- eax : key
2531 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002532 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002533 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002534 Label miss;
2535
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002536 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2537
2538 // Check that the name has not changed.
2539 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2540 __ j(not_equal, &miss, not_taken);
2541
ager@chromium.org5c838252010-02-19 08:53:10 +00002542 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002543 __ bind(&miss);
2544 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2545 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2546
2547 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002548 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002549}
2550
2551
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002552Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002553 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002554 // -- eax : key
2555 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002556 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002557 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002558 Label miss;
2559
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002560 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2561
2562 // Check that the name has not changed.
2563 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2564 __ j(not_equal, &miss, not_taken);
2565
ager@chromium.org5c838252010-02-19 08:53:10 +00002566 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002567 __ bind(&miss);
2568 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2569 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2570
2571 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002572 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002573}
2574
2575
2576Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2577 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002578 // -- eax : key
2579 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002580 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002581 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002582 Label miss;
2583
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002584 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2585
2586 // Check that the name has not changed.
2587 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2588 __ j(not_equal, &miss, not_taken);
2589
ager@chromium.org5c838252010-02-19 08:53:10 +00002590 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002591 __ bind(&miss);
2592 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2593 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2594
2595 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002596 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002597}
2598
2599
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002600// Specialized stub for constructing objects from functions which only have only
2601// simple assignments of the form this.x = ...; in their body.
2602Object* ConstructStubCompiler::CompileConstructStub(
2603 SharedFunctionInfo* shared) {
2604 // ----------- S t a t e -------------
2605 // -- eax : argc
2606 // -- edi : constructor
2607 // -- esp[0] : return address
2608 // -- esp[4] : last argument
2609 // -----------------------------------
2610 Label generic_stub_call;
2611#ifdef ENABLE_DEBUGGER_SUPPORT
2612 // Check to see whether there are any break points in the function code. If
2613 // there are jump to the generic constructor stub which calls the actual
2614 // code for the function thereby hitting the break points.
2615 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2616 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2617 __ cmp(ebx, Factory::undefined_value());
2618 __ j(not_equal, &generic_stub_call, not_taken);
2619#endif
2620
2621 // Load the initial map and verify that it is in fact a map.
2622 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2623 // Will both indicate a NULL and a Smi.
2624 __ test(ebx, Immediate(kSmiTagMask));
2625 __ j(zero, &generic_stub_call);
2626 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2627 __ j(not_equal, &generic_stub_call);
2628
2629#ifdef DEBUG
2630 // Cannot construct functions this way.
2631 // edi: constructor
2632 // ebx: initial map
2633 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2634 __ Assert(not_equal, "Function constructed by construct stub.");
2635#endif
2636
2637 // Now allocate the JSObject on the heap by moving the new space allocation
2638 // top forward.
2639 // edi: constructor
2640 // ebx: initial map
2641 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2642 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002643 __ AllocateInNewSpace(ecx,
2644 edx,
2645 ecx,
2646 no_reg,
2647 &generic_stub_call,
2648 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002649
2650 // Allocated the JSObject, now initialize the fields and add the heap tag.
2651 // ebx: initial map
2652 // edx: JSObject (untagged)
2653 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2654 __ mov(ebx, Factory::empty_fixed_array());
2655 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2656 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2657
2658 // Push the allocated object to the stack. This is the object that will be
2659 // returned (after it is tagged).
2660 __ push(edx);
2661
2662 // eax: argc
2663 // edx: JSObject (untagged)
2664 // Load the address of the first in-object property into edx.
2665 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2666 // Calculate the location of the first argument. The stack contains the
2667 // allocated object and the return address on top of the argc arguments.
2668 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2669
2670 // Use edi for holding undefined which is used in several places below.
2671 __ mov(edi, Factory::undefined_value());
2672
2673 // eax: argc
2674 // ecx: first argument
2675 // edx: first in-object property of the JSObject
2676 // edi: undefined
2677 // Fill the initialized properties with a constant value or a passed argument
2678 // depending on the this.x = ...; assignment in the function.
2679 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2680 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002681 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002682 // If argument is not passed the property is set to undefined,
2683 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002684 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002685 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002686 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002687 if (CpuFeatures::IsSupported(CMOV)) {
2688 CpuFeatures::Scope use_cmov(CMOV);
2689 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2690 } else {
2691 Label not_passed;
2692 __ j(below_equal, &not_passed);
2693 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2694 __ bind(&not_passed);
2695 }
2696 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002697 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002698 } else {
2699 // Set the property to the constant value.
2700 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2701 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2702 }
2703 }
2704
2705 // Fill the unused in-object property fields with undefined.
2706 for (int i = shared->this_property_assignments_count();
2707 i < shared->CalculateInObjectProperties();
2708 i++) {
2709 __ mov(Operand(edx, i * kPointerSize), edi);
2710 }
2711
2712 // Move argc to ebx and retrieve and tag the JSObject to return.
2713 __ mov(ebx, eax);
2714 __ pop(eax);
2715 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2716
2717 // Remove caller arguments and receiver from the stack and return.
2718 __ pop(ecx);
2719 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2720 __ push(ecx);
2721 __ IncrementCounter(&Counters::constructed_objects, 1);
2722 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2723 __ ret(0);
2724
2725 // Jump to the generic stub in case the specialized code cannot handle the
2726 // construction.
2727 __ bind(&generic_stub_call);
2728 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2729 Handle<Code> generic_construct_stub(code);
2730 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2731
2732 // Return the generated code.
2733 return GetCode();
2734}
2735
2736
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002737#undef __
2738
2739} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002740
2741#endif // V8_TARGET_ARCH_IA32