blob: 5bb5be617f375bfa1078959efddd4ca86dbfd59d [file] [log] [blame]
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_IA32)
31
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000032#include "ic-inl.h"
33#include "codegen-inl.h"
34#include "stub-cache.h"
35
kasperl@chromium.org71affb52009-05-26 05:44:31 +000036namespace v8 {
37namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
ager@chromium.org65dad4b2009-04-23 08:48:43 +000039#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000040
41
42static void ProbeTable(MacroAssembler* masm,
43 Code::Flags flags,
44 StubCache::Table table,
45 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000046 Register offset,
47 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000048 ExternalReference key_offset(SCTableReference::keyReference(table));
49 ExternalReference value_offset(SCTableReference::valueReference(table));
50
51 Label miss;
52
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000053 if (extra.is_valid()) {
54 // Get the code entry from the cache.
55 __ mov(extra, Operand::StaticArray(offset, times_2, value_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000057 // Check that the key in the entry matches the name.
58 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
59 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000060
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000061 // Check that the flags match what we're looking for.
62 __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
63 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
64 __ cmp(offset, flags);
65 __ j(not_equal, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000066
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000067 // Jump to the first instruction in the code stub.
68 __ add(Operand(extra), Immediate(Code::kHeaderSize - kHeapObjectTag));
69 __ jmp(Operand(extra));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000070
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000071 __ bind(&miss);
72 } else {
73 // Save the offset on the stack.
74 __ push(offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000075
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000076 // Check that the key in the entry matches the name.
77 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
78 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000079
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000080 // Get the code entry from the cache.
81 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
82
83 // Check that the flags match what we're looking for.
84 __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
85 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
86 __ cmp(offset, flags);
87 __ j(not_equal, &miss);
88
89 // Restore offset and re-load code entry from cache.
90 __ pop(offset);
91 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
92
93 // Jump to the first instruction in the code stub.
94 __ add(Operand(offset), Immediate(Code::kHeaderSize - kHeapObjectTag));
95 __ jmp(Operand(offset));
96
97 // Pop at miss.
98 __ bind(&miss);
99 __ pop(offset);
100 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000101}
102
103
104void StubCache::GenerateProbe(MacroAssembler* masm,
105 Code::Flags flags,
106 Register receiver,
107 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000108 Register scratch,
109 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000110 Label miss;
111
112 // Make sure that code is valid. The shifting code relies on the
113 // entry size being 8.
114 ASSERT(sizeof(Entry) == 8);
115
116 // Make sure the flags does not name a specific type.
117 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
118
119 // Make sure that there are no register conflicts.
120 ASSERT(!scratch.is(receiver));
121 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000122 ASSERT(!extra.is(receiver));
123 ASSERT(!extra.is(name));
124 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
126 // Check that the receiver isn't a smi.
127 __ test(receiver, Immediate(kSmiTagMask));
128 __ j(zero, &miss, not_taken);
129
130 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000131 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000132 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000133 __ xor_(scratch, flags);
134 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
135
136 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000137 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138
139 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000140 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000141 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
142 __ xor_(scratch, flags);
143 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000144 __ sub(scratch, Operand(name));
145 __ add(Operand(scratch), Immediate(flags));
146 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
147
148 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000149 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000150
151 // Cache miss: Fall-through and let caller handle the miss by
152 // entering the runtime system.
153 __ bind(&miss);
154}
155
156
157void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
158 int index,
159 Register prototype) {
160 // Load the global or builtins object from the current context.
161 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
162 // Load the global context from the global or builtins object.
163 __ mov(prototype,
164 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
165 // Load the function from the global context.
166 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
167 // Load the initial map. The global functions all have initial maps.
168 __ mov(prototype,
169 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
170 // Load the prototype from the initial map.
171 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
172}
173
174
175void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
176 Register receiver,
177 Register scratch,
178 Label* miss_label) {
179 // Check that the receiver isn't a smi.
180 __ test(receiver, Immediate(kSmiTagMask));
181 __ j(zero, miss_label, not_taken);
182
183 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000184 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000185 __ j(not_equal, miss_label, not_taken);
186
187 // Load length directly from the JS array.
188 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
189 __ ret(0);
190}
191
192
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000193// Generate code to check if an object is a string. If the object is
194// a string, the map's instance type is left in the scratch register.
195static void GenerateStringCheck(MacroAssembler* masm,
196 Register receiver,
197 Register scratch,
198 Label* smi,
199 Label* non_string_object) {
200 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000202 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000204 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000205 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
206 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000207 ASSERT(kNotStringTag != 0);
208 __ test(scratch, Immediate(kNotStringTag));
209 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000210}
211
212
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000213void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
214 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000215 Register scratch1,
216 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000217 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000218 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000220 // Check if the object is a string leaving the instance type in the
221 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000222 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000224 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000226 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000227
228 // Check if the object is a JSValue wrapper.
229 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000230 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000231 __ j(not_equal, miss, not_taken);
232
233 // Check if the wrapped value is a string and load the length
234 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000235 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
236 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
237 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000238 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
243 Register receiver,
244 Register scratch1,
245 Register scratch2,
246 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000247 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
ager@chromium.org7c537e22008-10-16 08:43:32 +0000252
253// Load a fast property out of a holder object (src). In-object properties
254// are loaded directly otherwise the property is loaded from the properties
255// fixed array.
256void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000257 Register dst, Register src,
258 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Adjust for the number of properties stored in the holder.
260 index -= holder->map()->inobject_properties();
261 if (index < 0) {
262 // Get the property straight out of the holder.
263 int offset = holder->map()->instance_size() + (index * kPointerSize);
264 __ mov(dst, FieldOperand(src, offset));
265 } else {
266 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000267 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
269 __ mov(dst, FieldOperand(dst, offset));
270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271}
272
273
ager@chromium.org5c838252010-02-19 08:53:10 +0000274static void PushInterceptorArguments(MacroAssembler* masm,
275 Register receiver,
276 Register holder,
277 Register name,
278 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 __ push(name);
280 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
281 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000282 Register scratch = name;
283 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
284 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000285 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000286 __ push(holder);
287 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000288}
289
290
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000291static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
292 Register receiver,
293 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000294 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000295 JSObject* holder_obj) {
296 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000297 __ CallExternalReference(
298 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
299 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000300}
301
302
ager@chromium.org5c838252010-02-19 08:53:10 +0000303// Reserves space for the extra arguments to FastHandleApiCall in the
304// caller's frame.
305//
306// These arguments are set by CheckPrototypes and GenerateFastApiCall.
307static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
308 // ----------- S t a t e -------------
309 // -- esp[0] : return address
310 // -- esp[4] : last argument in the internal frame of the caller
311 // -----------------------------------
312 __ pop(scratch);
313 __ push(Immediate(Smi::FromInt(0)));
314 __ push(Immediate(Smi::FromInt(0)));
315 __ push(Immediate(Smi::FromInt(0)));
316 __ push(Immediate(Smi::FromInt(0)));
317 __ push(scratch);
318}
319
320
321// Undoes the effects of ReserveSpaceForFastApiCall.
322static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
323 // ----------- S t a t e -------------
324 // -- esp[0] : return address
325 // -- esp[4] : last fast api call extra argument
326 // -- ...
327 // -- esp[16] : first fast api call extra argument
328 // -- esp[20] : last argument in the internal frame
329 // -----------------------------------
330 __ pop(scratch);
331 __ add(Operand(esp), Immediate(kPointerSize * 4));
332 __ push(scratch);
333}
334
335
336// Generates call to FastHandleApiCall builtin.
337static void GenerateFastApiCall(MacroAssembler* masm,
338 const CallOptimization& optimization,
339 int argc) {
340 // ----------- S t a t e -------------
341 // -- esp[0] : return address
342 // -- esp[4] : object passing the type check
343 // (last fast api call extra argument,
344 // set by CheckPrototypes)
345 // -- esp[8] : api call data
346 // -- esp[12] : api callback
347 // -- esp[16] : api function
348 // (first fast api call extra argument)
349 // -- esp[20] : last argument
350 // -- ...
351 // -- esp[(argc + 5) * 4] : first argument
352 // -- esp[(argc + 6) * 4] : receiver
353 // -----------------------------------
354
355 // Get the function and setup the context.
356 JSFunction* function = optimization.constant_function();
357 __ mov(edi, Immediate(Handle<JSFunction>(function)));
358 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
359
360 // Pass the additional arguments FastHandleApiCall expects.
361 __ mov(Operand(esp, 4 * kPointerSize), edi);
362 bool info_loaded = false;
363 Object* callback = optimization.api_call_info()->callback();
364 if (Heap::InNewSpace(callback)) {
365 info_loaded = true;
366 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
367 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
368 __ mov(Operand(esp, 3 * kPointerSize), ebx);
369 } else {
370 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
371 }
372 Object* call_data = optimization.api_call_info()->data();
373 if (Heap::InNewSpace(call_data)) {
374 if (!info_loaded) {
375 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
376 }
377 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
378 __ mov(Operand(esp, 2 * kPointerSize), ebx);
379 } else {
380 __ mov(Operand(esp, 2 * kPointerSize),
381 Immediate(Handle<Object>(call_data)));
382 }
383
384 // Set the number of arguments.
385 __ mov(eax, Immediate(argc + 4));
386
387 // Jump to the fast api call builtin (tail call).
388 Handle<Code> code = Handle<Code>(
389 Builtins::builtin(Builtins::FastHandleApiCall));
390 ParameterCount expected(0);
391 __ InvokeCode(code, expected, expected,
392 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
393}
394
395
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000396class CallInterceptorCompiler BASE_EMBEDDED {
397 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000398 CallInterceptorCompiler(StubCompiler* stub_compiler,
399 const ParameterCount& arguments,
400 Register name)
401 : stub_compiler_(stub_compiler),
402 arguments_(arguments),
403 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000404
ager@chromium.org5c838252010-02-19 08:53:10 +0000405 void Compile(MacroAssembler* masm,
406 JSObject* object,
407 JSObject* holder,
408 String* name,
409 LookupResult* lookup,
410 Register receiver,
411 Register scratch1,
412 Register scratch2,
413 Label* miss) {
414 ASSERT(holder->HasNamedInterceptor());
415 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
416
417 // Check that the receiver isn't a smi.
418 __ test(receiver, Immediate(kSmiTagMask));
419 __ j(zero, miss, not_taken);
420
421 CallOptimization optimization(lookup);
422
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000423 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000424 CompileCacheable(masm,
425 object,
426 receiver,
427 scratch1,
428 scratch2,
429 holder,
430 lookup,
431 name,
432 optimization,
433 miss);
434 } else {
435 CompileRegular(masm,
436 object,
437 receiver,
438 scratch1,
439 scratch2,
440 name,
441 holder,
442 miss);
443 }
444 }
445
446 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000447 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000448 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000449 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000450 Register scratch1,
451 Register scratch2,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000452 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000453 LookupResult* lookup,
454 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000455 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000456 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000457 ASSERT(optimization.is_constant_call());
458 ASSERT(!lookup->holder()->IsGlobalObject());
459
460 int depth1 = kInvalidProtoDepth;
461 int depth2 = kInvalidProtoDepth;
462 bool can_do_fast_api_call = false;
463 if (optimization.is_simple_api_call() &&
464 !lookup->holder()->IsGlobalObject()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000465 depth1 =
466 optimization.GetPrototypeDepthOfExpectedType(object,
467 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000468 if (depth1 == kInvalidProtoDepth) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000469 depth2 =
470 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
471 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000472 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000473 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
474 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000475 }
476
ager@chromium.org5c838252010-02-19 08:53:10 +0000477 __ IncrementCounter(&Counters::call_const_interceptor, 1);
478
479 if (can_do_fast_api_call) {
480 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
481 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000482 }
483
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000484 // Check that the maps from receiver to interceptor's holder
485 // haven't changed and thus we can invoke interceptor.
ager@chromium.org5c838252010-02-19 08:53:10 +0000486 Label miss_cleanup;
487 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
488 Register holder =
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000489 stub_compiler_->CheckPrototypes(object, receiver,
490 interceptor_holder, scratch1,
491 scratch2, name, depth1, miss);
ager@chromium.org5c838252010-02-19 08:53:10 +0000492
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000493 // Invoke an interceptor and if it provides a value,
494 // branch to |regular_invoke|.
ager@chromium.org5c838252010-02-19 08:53:10 +0000495 Label regular_invoke;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000496 LoadWithInterceptor(masm, receiver, holder, interceptor_holder,
497 &regular_invoke);
ager@chromium.org5c838252010-02-19 08:53:10 +0000498
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000499 // Interceptor returned nothing for this property. Try to use cached
500 // constant function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000501
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000502 // Check that the maps from interceptor's holder to constant function's
503 // holder haven't changed and thus we can use cached constant function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000504 if (interceptor_holder != lookup->holder()) {
505 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
506 lookup->holder(), scratch1,
507 scratch2, name, depth2, miss);
508 } else {
509 // CheckPrototypes has a side effect of fetching a 'holder'
510 // for API (object which is instanceof for the signature). It's
511 // safe to omit it here, as if present, it should be fetched
512 // by the previous CheckPrototypes.
513 ASSERT(depth2 == kInvalidProtoDepth);
514 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000515
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000516 // Invoke function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000517 if (can_do_fast_api_call) {
518 GenerateFastApiCall(masm, optimization, arguments_.immediate());
519 } else {
520 __ InvokeFunction(optimization.constant_function(), arguments_,
521 JUMP_FUNCTION);
522 }
523
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000524 // Deferred code for fast API call case---clean preallocated space.
ager@chromium.org5c838252010-02-19 08:53:10 +0000525 if (can_do_fast_api_call) {
526 __ bind(&miss_cleanup);
527 FreeSpaceForFastApiCall(masm, scratch1);
528 __ jmp(miss_label);
529 }
530
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000531 // Invoke a regular function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000532 __ bind(&regular_invoke);
533 if (can_do_fast_api_call) {
534 FreeSpaceForFastApiCall(masm, scratch1);
535 }
536 }
537
538 void CompileRegular(MacroAssembler* masm,
539 JSObject* object,
540 Register receiver,
541 Register scratch1,
542 Register scratch2,
543 String* name,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000544 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000545 Label* miss_label) {
546 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000547 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000548 scratch1, scratch2, name,
549 miss_label);
550
551 __ EnterInternalFrame();
552 // Save the name_ register across the call.
553 __ push(name_);
554
555 PushInterceptorArguments(masm,
556 receiver,
557 holder,
558 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000559 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000560
561 __ CallExternalReference(
562 ExternalReference(
563 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
564 5);
565
566 // Restore the name_ register.
567 __ pop(name_);
568 __ LeaveInternalFrame();
569 }
570
571 void LoadWithInterceptor(MacroAssembler* masm,
572 Register receiver,
573 Register holder,
574 JSObject* holder_obj,
575 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000576 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000577 __ push(holder); // Save the holder.
578 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000579
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000580 CompileCallLoadPropertyWithInterceptor(masm,
581 receiver,
582 holder,
583 name_,
584 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000585
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000586 __ pop(name_); // Restore the name.
587 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000588 __ LeaveInternalFrame();
589
590 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000591 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000592 }
593
ager@chromium.org5c838252010-02-19 08:53:10 +0000594 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000595 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000596 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000597};
598
599
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000600void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
601 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
602 Code* code = NULL;
603 if (kind == Code::LOAD_IC) {
604 code = Builtins::builtin(Builtins::LoadIC_Miss);
605 } else {
606 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
607 }
608
609 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000610 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611}
612
613
ager@chromium.org5c838252010-02-19 08:53:10 +0000614// Both name_reg and receiver_reg are preserved on jumps to miss_label,
615// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000616void StubCompiler::GenerateStoreField(MacroAssembler* masm,
617 JSObject* object,
618 int index,
619 Map* transition,
620 Register receiver_reg,
621 Register name_reg,
622 Register scratch,
623 Label* miss_label) {
624 // Check that the object isn't a smi.
625 __ test(receiver_reg, Immediate(kSmiTagMask));
626 __ j(zero, miss_label, not_taken);
627
628 // Check that the map of the object hasn't changed.
629 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
630 Immediate(Handle<Map>(object->map())));
631 __ j(not_equal, miss_label, not_taken);
632
633 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000634 if (object->IsJSGlobalProxy()) {
635 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000636 }
637
638 // Stub never generated for non-global objects that require access
639 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000640 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000641
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000642 // Perform map transition for the receiver if necessary.
643 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
644 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000645 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000646 __ pop(scratch); // Return address.
647 __ push(receiver_reg);
648 __ push(Immediate(Handle<Map>(transition)));
649 __ push(eax);
650 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000651 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000652 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000653 return;
654 }
655
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000656 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000657 // Update the map of the object; no write barrier updating is
658 // needed because the map is never in new space.
659 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
660 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661 }
662
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000663 // Adjust for the number of properties stored in the object. Even in the
664 // face of a transition we can use the old map here because the size of the
665 // object and the number of in-object properties is not going to change.
666 index -= object->map()->inobject_properties();
667
ager@chromium.org7c537e22008-10-16 08:43:32 +0000668 if (index < 0) {
669 // Set the property straight into the object.
670 int offset = object->map()->instance_size() + (index * kPointerSize);
671 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672
ager@chromium.org7c537e22008-10-16 08:43:32 +0000673 // Update the write barrier for the array address.
674 // Pass the value being stored in the now unused name_reg.
675 __ mov(name_reg, Operand(eax));
676 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
677 } else {
678 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000679 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000680 // Get the properties array (optimistically).
681 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000682 __ mov(FieldOperand(scratch, offset), eax);
683
684 // Update the write barrier for the array address.
685 // Pass the value being stored in the now unused name_reg.
686 __ mov(name_reg, Operand(eax));
687 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
688 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000689
690 // Return the value (register eax).
691 __ ret(0);
692}
693
694
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000695// Generate code to check that a global property cell is empty. Create
696// the property cell at compilation time if no cell exists for the
697// property.
698static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
699 GlobalObject* global,
700 String* name,
701 Register scratch,
702 Label* miss) {
703 Object* probe = global->EnsurePropertyCell(name);
704 if (probe->IsFailure()) return probe;
705 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
706 ASSERT(cell->value()->IsTheHole());
707 __ mov(scratch, Immediate(Handle<Object>(cell)));
708 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
709 Immediate(Factory::the_hole_value()));
710 __ j(not_equal, miss, not_taken);
711 return cell;
712}
713
714
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000715#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000716#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000717
718
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000719Register StubCompiler::CheckPrototypes(JSObject* object,
720 Register object_reg,
721 JSObject* holder,
722 Register holder_reg,
723 Register scratch,
724 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000725 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000726 Label* miss) {
727 // Check that the maps haven't changed.
728 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000729 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
730 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000731
732 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000733 // that their maps haven't changed. We also need to check that the
734 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000735 while (object != holder) {
736 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000737 Object* cell = GenerateCheckPropertyCell(masm(),
738 GlobalObject::cast(object),
739 name,
740 scratch,
741 miss);
742 if (cell->IsFailure()) {
743 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000744 return result;
745 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000746 }
747 object = JSObject::cast(object->GetPrototype());
748 }
749
ager@chromium.org5c838252010-02-19 08:53:10 +0000750 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000751 return result;
752}
753
754
755void StubCompiler::GenerateLoadField(JSObject* object,
756 JSObject* holder,
757 Register receiver,
758 Register scratch1,
759 Register scratch2,
760 int index,
761 String* name,
762 Label* miss) {
763 // Check that the receiver isn't a smi.
764 __ test(receiver, Immediate(kSmiTagMask));
765 __ j(zero, miss, not_taken);
766
767 // Check the prototype chain.
768 Register reg =
769 CheckPrototypes(object, receiver, holder,
770 scratch1, scratch2, name, miss);
771
772 // Get the value from the properties.
773 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
774 __ ret(0);
775}
776
777
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000778bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000779 JSObject* holder,
780 Register receiver,
781 Register name_reg,
782 Register scratch1,
783 Register scratch2,
784 AccessorInfo* callback,
785 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000786 Label* miss,
787 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000788 // Check that the receiver isn't a smi.
789 __ test(receiver, Immediate(kSmiTagMask));
790 __ j(zero, miss, not_taken);
791
792 // Check that the maps haven't changed.
793 Register reg =
794 CheckPrototypes(object, receiver, holder,
795 scratch1, scratch2, name, miss);
796
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000797 Handle<AccessorInfo> callback_handle(callback);
798
799 Register other = reg.is(scratch1) ? scratch2 : scratch1;
800 __ EnterInternalFrame();
801 __ PushHandleScope(other);
802 // Push the stack address where the list of arguments ends
803 __ mov(other, esp);
804 __ sub(Operand(other), Immediate(2 * kPointerSize));
805 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000806 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000807 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000808 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000809 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000810 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000811 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000812 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000813 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000814 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000815 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000816
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000817 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000818 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000819 Address getter_address = v8::ToCData<Address>(callback->getter());
820 ApiFunction fun(getter_address);
821 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000822 // Emitting a stub call may try to allocate (if the code is not
823 // already generated). Do not allow the assembler to perform a
824 // garbage collection but instead return the allocation failure
825 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000826 Object* result = masm()->TryCallStub(&stub);
827 if (result->IsFailure()) {
828 *failure = Failure::cast(result);
829 return false;
830 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000831
832 // We need to avoid using eax since that now holds the result.
833 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000834 // Emitting PopHandleScope may try to allocate. Do not allow the
835 // assembler to perform a garbage collection but instead return a
836 // failure object.
837 result = masm()->TryPopHandleScope(eax, tmp);
838 if (result->IsFailure()) {
839 *failure = Failure::cast(result);
840 return false;
841 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000842 __ LeaveInternalFrame();
843
844 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000845 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000846}
847
848
849void StubCompiler::GenerateLoadConstant(JSObject* object,
850 JSObject* holder,
851 Register receiver,
852 Register scratch1,
853 Register scratch2,
854 Object* value,
855 String* name,
856 Label* miss) {
857 // Check that the receiver isn't a smi.
858 __ test(receiver, Immediate(kSmiTagMask));
859 __ j(zero, miss, not_taken);
860
861 // Check that the maps haven't changed.
862 Register reg =
863 CheckPrototypes(object, receiver, holder,
864 scratch1, scratch2, name, miss);
865
866 // Return the constant value.
867 __ mov(eax, Handle<Object>(value));
868 __ ret(0);
869}
870
871
872void StubCompiler::GenerateLoadInterceptor(JSObject* object,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000873 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000874 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000875 Register receiver,
876 Register name_reg,
877 Register scratch1,
878 Register scratch2,
879 String* name,
880 Label* miss) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000881 ASSERT(interceptor_holder->HasNamedInterceptor());
882 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
883
884 // Check that the receiver isn't a smi.
885 __ test(receiver, Immediate(kSmiTagMask));
886 __ j(zero, miss, not_taken);
887
888 // So far the most popular follow ups for interceptor loads are FIELD
889 // and CALLBACKS, so inline only them, other cases may be added
890 // later.
891 bool compile_followup_inline = false;
892 if (lookup->IsProperty() && lookup->IsCacheable()) {
893 if (lookup->type() == FIELD) {
894 compile_followup_inline = true;
895 } else if (lookup->type() == CALLBACKS &&
896 lookup->GetCallbackObject()->IsAccessorInfo() &&
897 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
898 compile_followup_inline = true;
899 }
900 }
901
902 if (compile_followup_inline) {
903 // Compile the interceptor call, followed by inline code to load the
904 // property from further up the prototype chain if the call fails.
905 // Check that the maps haven't changed.
906 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
907 scratch1, scratch2, name, miss);
908 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
909
910 // Save necessary data before invoking an interceptor.
911 // Requires a frame to make GC aware of pushed pointers.
912 __ EnterInternalFrame();
913
914 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
915 // CALLBACKS case needs a receiver to be passed into C++ callback.
916 __ push(receiver);
917 }
918 __ push(holder_reg);
919 __ push(name_reg);
920
921 // Invoke an interceptor. Note: map checks from receiver to
922 // interceptor's holder has been compiled before (see a caller
923 // of this method.)
924 CompileCallLoadPropertyWithInterceptor(masm(),
925 receiver,
926 holder_reg,
927 name_reg,
928 interceptor_holder);
929
930 // Check if interceptor provided a value for property. If it's
931 // the case, return immediately.
932 Label interceptor_failed;
933 __ cmp(eax, Factory::no_interceptor_result_sentinel());
934 __ j(equal, &interceptor_failed);
935 __ LeaveInternalFrame();
936 __ ret(0);
937
938 __ bind(&interceptor_failed);
939 __ pop(name_reg);
940 __ pop(holder_reg);
941 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
942 __ pop(receiver);
943 }
944
945 __ LeaveInternalFrame();
946
947 // Check that the maps from interceptor's holder to lookup's holder
948 // haven't changed. And load lookup's holder into holder_reg.
949 if (interceptor_holder != lookup->holder()) {
950 holder_reg = CheckPrototypes(interceptor_holder,
951 holder_reg,
952 lookup->holder(),
953 scratch1,
954 scratch2,
955 name,
956 miss);
957 }
958
959 if (lookup->type() == FIELD) {
960 // We found FIELD property in prototype chain of interceptor's holder.
961 // Retrieve a field from field's holder.
962 GenerateFastPropertyLoad(masm(), eax, holder_reg,
963 lookup->holder(), lookup->GetFieldIndex());
964 __ ret(0);
965 } else {
966 // We found CALLBACKS property in prototype chain of interceptor's
967 // holder.
968 ASSERT(lookup->type() == CALLBACKS);
969 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
970 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
971 ASSERT(callback != NULL);
972 ASSERT(callback->getter() != NULL);
973
974 // Tail call to runtime.
975 // Important invariant in CALLBACKS case: the code above must be
976 // structured to never clobber |receiver| register.
977 __ pop(scratch2); // return address
978 __ push(receiver);
979 __ push(holder_reg);
980 __ mov(holder_reg, Immediate(Handle<AccessorInfo>(callback)));
981 __ push(holder_reg);
982 __ push(FieldOperand(holder_reg, AccessorInfo::kDataOffset));
983 __ push(name_reg);
984 __ push(scratch2); // restore return address
985
986 ExternalReference ref =
987 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
988 __ TailCallExternalReference(ref, 5, 1);
989 }
990 } else { // !compile_followup_inline
991 // Call the runtime system to load the interceptor.
992 // Check that the maps haven't changed.
993 Register holder_reg =
994 CheckPrototypes(object, receiver, interceptor_holder,
995 scratch1, scratch2, name, miss);
996 __ pop(scratch2); // save old return address
997 PushInterceptorArguments(masm(), receiver, holder_reg,
998 name_reg, interceptor_holder);
999 __ push(scratch2); // restore old return address
1000
1001 ExternalReference ref = ExternalReference(
1002 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
1003 __ TailCallExternalReference(ref, 5, 1);
1004 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001005}
1006
1007
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001008// TODO(1241006): Avoid having lazy compile stubs specialized by the
1009// number of arguments. It is not needed anymore.
1010Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001012 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001013
1014 // Push a copy of the function onto the stack.
1015 __ push(edi);
1016
1017 __ push(edi); // function is also the parameter to the runtime call
1018 __ CallRuntime(Runtime::kLazyCompile, 1);
1019 __ pop(edi);
1020
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001021 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001022 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023
1024 // Do a tail-call of the compiled function.
1025 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1026 __ jmp(Operand(ecx));
1027
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001028 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029}
1030
1031
ager@chromium.org5c838252010-02-19 08:53:10 +00001032Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001033 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001034 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001035 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001036 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001037 // -- ecx : name
1038 // -- esp[0] : return address
1039 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1040 // -- ...
1041 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001042 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001043 Label miss;
1044
1045 // Get the receiver from the stack.
1046 const int argc = arguments().immediate();
1047 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1048
1049 // Check that the receiver isn't a smi.
1050 __ test(edx, Immediate(kSmiTagMask));
1051 __ j(zero, &miss, not_taken);
1052
1053 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001054 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055
ager@chromium.org7c537e22008-10-16 08:43:32 +00001056 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001057
1058 // Check that the function really is a function.
1059 __ test(edi, Immediate(kSmiTagMask));
1060 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001061 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001062 __ j(not_equal, &miss, not_taken);
1063
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001064 // Patch the receiver on the stack with the global proxy if
1065 // necessary.
1066 if (object->IsGlobalObject()) {
1067 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1068 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1069 }
1070
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 // Invoke the function.
1072 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1073
1074 // Handle call cache miss.
1075 __ bind(&miss);
1076 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001077 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078
1079 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001080 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001081}
1082
1083
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001084Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1085 JSObject* holder,
1086 JSFunction* function,
1087 String* name,
1088 CheckType check) {
1089 // ----------- S t a t e -------------
1090 // -- ecx : name
1091 // -- esp[0] : return address
1092 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1093 // -- ...
1094 // -- esp[(argc + 1) * 4] : receiver
1095 // -----------------------------------
1096 ASSERT(check == RECEIVER_MAP_CHECK);
1097
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001098 // If object is not an array, bail out to regular call.
1099 if (!object->IsJSArray()) {
1100 return Heap::undefined_value();
1101 }
1102
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001103 Label miss;
1104
1105 // Get the receiver from the stack.
1106 const int argc = arguments().immediate();
1107 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1108
1109 // Check that the receiver isn't a smi.
1110 __ test(edx, Immediate(kSmiTagMask));
1111 __ j(zero, &miss);
1112
1113 CheckPrototypes(JSObject::cast(object), edx,
1114 holder, ebx,
1115 eax, name, &miss);
1116
1117 if (argc == 0) {
1118 // Noop, return the length.
1119 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1120 __ ret((argc + 1) * kPointerSize);
1121 } else {
1122 // Get the elements array of the object.
1123 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1124
1125 // Check that the elements are in fast mode (not dictionary).
1126 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1127 Immediate(Factory::fixed_array_map()));
1128 __ j(not_equal, &miss);
1129
1130 if (argc == 1) { // Otherwise fall through to call builtin.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001131 Label call_builtin, exit, with_write_barrier, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001132
1133 // Get the array's length into eax and calculate new length.
1134 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1135 STATIC_ASSERT(kSmiTagSize == 1);
1136 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001137 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001138
1139 // Get the element's length into ecx.
1140 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001141
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001142 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001143 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001144 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001145
1146 // Save new length.
1147 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1148
1149 // Push the element.
1150 __ lea(edx, FieldOperand(ebx,
1151 eax, times_half_pointer_size,
1152 FixedArray::kHeaderSize - argc * kPointerSize));
1153 __ mov(ecx, Operand(esp, argc * kPointerSize));
1154 __ mov(Operand(edx, 0), ecx);
1155
1156 // Check if value is a smi.
1157 __ test(ecx, Immediate(kSmiTagMask));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001158 __ j(not_zero, &with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001159
1160 __ bind(&exit);
1161 __ ret((argc + 1) * kPointerSize);
1162
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001163 __ bind(&with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001164
1165 __ InNewSpace(ebx, ecx, equal, &exit);
1166
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001167 __ RecordWriteHelper(ebx, edx, ecx);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001168 __ ret((argc + 1) * kPointerSize);
1169
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001170 __ bind(&attempt_to_grow_elements);
1171 ExternalReference new_space_allocation_top =
1172 ExternalReference::new_space_allocation_top_address();
1173 ExternalReference new_space_allocation_limit =
1174 ExternalReference::new_space_allocation_limit_address();
1175
1176 const int kAllocationDelta = 4;
1177 // Load top.
1178 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1179
1180 // Check if it's the end of elements.
1181 __ lea(edx, FieldOperand(ebx,
1182 eax, times_half_pointer_size,
1183 FixedArray::kHeaderSize - argc * kPointerSize));
1184 __ cmp(edx, Operand(ecx));
1185 __ j(not_equal, &call_builtin);
1186 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1187 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001188 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001189
1190 // We fit and could grow elements.
1191 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1192 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001193
1194 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001195 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001196 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001197 for (int i = 1; i < kAllocationDelta; i++) {
1198 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001199 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001200 }
1201
1202 // Restore receiver to edx as finish sequence assumes it's here.
1203 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1204
1205 // Increment element's and array's sizes.
1206 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001207 Immediate(Smi::FromInt(kAllocationDelta)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001208 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1209
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001210 // Elements are in new space, so write barrier is not required.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001211 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001212
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001213 __ bind(&call_builtin);
1214 }
1215
1216 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1217 argc + 1,
1218 1);
1219 }
1220
1221 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001222 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1223 __ jmp(ic, RelocInfo::CODE_TARGET);
1224
1225 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001226 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001227}
1228
1229
1230Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1231 JSObject* holder,
1232 JSFunction* function,
1233 String* name,
1234 CheckType check) {
1235 // ----------- S t a t e -------------
1236 // -- ecx : name
1237 // -- esp[0] : return address
1238 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1239 // -- ...
1240 // -- esp[(argc + 1) * 4] : receiver
1241 // -----------------------------------
1242 ASSERT(check == RECEIVER_MAP_CHECK);
1243
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001244 // If object is not an array, bail out to regular call.
1245 if (!object->IsJSArray()) {
1246 return Heap::undefined_value();
1247 }
1248
ager@chromium.orgac091b72010-05-05 07:34:42 +00001249 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001250
1251 // Get the receiver from the stack.
1252 const int argc = arguments().immediate();
1253 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1254
1255 // Check that the receiver isn't a smi.
1256 __ test(edx, Immediate(kSmiTagMask));
1257 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001258 CheckPrototypes(JSObject::cast(object), edx,
1259 holder, ebx,
1260 eax, name, &miss);
1261
1262 // Get the elements array of the object.
1263 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1264
1265 // Check that the elements are in fast mode (not dictionary).
1266 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1267 Immediate(Factory::fixed_array_map()));
1268 __ j(not_equal, &miss);
1269
1270 // Get the array's length into ecx and calculate new length.
1271 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1272 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001273 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001274
1275 // Get the last element.
1276 STATIC_ASSERT(kSmiTagSize == 1);
1277 STATIC_ASSERT(kSmiTag == 0);
1278 __ mov(eax, FieldOperand(ebx,
1279 ecx, times_half_pointer_size,
1280 FixedArray::kHeaderSize));
1281 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1282 __ j(equal, &call_builtin);
1283
1284 // Set the array's length.
1285 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1286
1287 // Fill with the hole.
1288 __ mov(FieldOperand(ebx,
1289 ecx, times_half_pointer_size,
1290 FixedArray::kHeaderSize),
1291 Immediate(Factory::the_hole_value()));
1292 __ ret((argc + 1) * kPointerSize);
1293
ager@chromium.orgac091b72010-05-05 07:34:42 +00001294 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001295 __ mov(eax, Immediate(Factory::undefined_value()));
1296 __ ret((argc + 1) * kPointerSize);
1297
1298 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001299 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1300 argc + 1,
1301 1);
1302
1303 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001304 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1305 __ jmp(ic, RelocInfo::CODE_TARGET);
1306
1307 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001308 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001309}
1310
1311
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001312Object* CallStubCompiler::CompileStringCharCodeAtCall(Object* object,
1313 JSObject* holder,
1314 JSFunction* function,
1315 String* name,
1316 CheckType check) {
1317 // ----------- S t a t e -------------
1318 // -- ecx : function name
1319 // -- esp[0] : return address
1320 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1321 // -- ...
1322 // -- esp[(argc + 1) * 4] : receiver
1323 // -----------------------------------
1324
1325 const int argc = arguments().immediate();
1326
1327 Label miss;
1328 Label index_out_of_range;
1329
1330 // Check that the maps starting from the prototype haven't changed.
1331 GenerateLoadGlobalFunctionPrototype(masm(),
1332 Context::STRING_FUNCTION_INDEX,
1333 eax);
1334 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1335 ebx, edx, name, &miss);
1336
1337 Register receiver = ebx;
1338 Register index = ecx;
1339 Register scratch = edx;
1340 Register result = eax;
1341 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1342 if (argc > 0) {
1343 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1344 } else {
1345 __ Set(index, Immediate(Factory::undefined_value()));
1346 }
1347
1348 StringCharCodeAtGenerator char_code_at_generator(receiver,
1349 index,
1350 scratch,
1351 result,
1352 &miss, // When not a string.
1353 &miss, // When not a number.
1354 &index_out_of_range,
1355 STRING_INDEX_IS_NUMBER);
1356 char_code_at_generator.GenerateFast(masm());
1357 __ ret((argc + 1) * kPointerSize);
1358
1359 ICRuntimeCallHelper call_helper;
1360 char_code_at_generator.GenerateSlow(masm(), call_helper);
1361
1362 __ bind(&index_out_of_range);
1363 __ Set(eax, Immediate(Factory::nan_value()));
1364 __ ret((argc + 1) * kPointerSize);
1365
1366 __ bind(&miss);
1367 // Restore function name in ecx.
1368 __ Set(ecx, Immediate(Handle<String>(name)));
1369
1370 Handle<Code> ic = ComputeCallMiss(argc);
1371 __ jmp(ic, RelocInfo::CODE_TARGET);
1372
1373 // Return the generated code.
1374 return GetCode(function);
1375}
1376
1377
1378Object* CallStubCompiler::CompileStringCharAtCall(Object* object,
1379 JSObject* holder,
1380 JSFunction* function,
1381 String* name,
1382 CheckType check) {
1383 // ----------- S t a t e -------------
1384 // -- ecx : function name
1385 // -- esp[0] : return address
1386 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1387 // -- ...
1388 // -- esp[(argc + 1) * 4] : receiver
1389 // -----------------------------------
1390
1391 const int argc = arguments().immediate();
1392
1393 Label miss;
1394 Label index_out_of_range;
1395
1396 // Check that the maps starting from the prototype haven't changed.
1397 GenerateLoadGlobalFunctionPrototype(masm(),
1398 Context::STRING_FUNCTION_INDEX,
1399 eax);
1400 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1401 ebx, edx, name, &miss);
1402
1403 Register receiver = eax;
1404 Register index = ecx;
1405 Register scratch1 = ebx;
1406 Register scratch2 = edx;
1407 Register result = eax;
1408 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1409 if (argc > 0) {
1410 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1411 } else {
1412 __ Set(index, Immediate(Factory::undefined_value()));
1413 }
1414
1415 StringCharAtGenerator char_at_generator(receiver,
1416 index,
1417 scratch1,
1418 scratch2,
1419 result,
1420 &miss, // When not a string.
1421 &miss, // When not a number.
1422 &index_out_of_range,
1423 STRING_INDEX_IS_NUMBER);
1424 char_at_generator.GenerateFast(masm());
1425 __ ret((argc + 1) * kPointerSize);
1426
1427 ICRuntimeCallHelper call_helper;
1428 char_at_generator.GenerateSlow(masm(), call_helper);
1429
1430 __ bind(&index_out_of_range);
1431 __ Set(eax, Immediate(Factory::empty_string()));
1432 __ ret((argc + 1) * kPointerSize);
1433
1434 __ bind(&miss);
1435 // Restore function name in ecx.
1436 __ Set(ecx, Immediate(Handle<String>(name)));
1437
1438 Handle<Code> ic = ComputeCallMiss(argc);
1439 __ jmp(ic, RelocInfo::CODE_TARGET);
1440
1441 // Return the generated code.
1442 return GetCode(function);
1443}
1444
1445
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001446Object* CallStubCompiler::CompileCallConstant(Object* object,
1447 JSObject* holder,
1448 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001449 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001450 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001451 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001452 // -- ecx : name
1453 // -- esp[0] : return address
1454 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1455 // -- ...
1456 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001457 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001458
1459 SharedFunctionInfo* function_info = function->shared();
1460 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001461 const int id = function_info->custom_call_generator_id();
1462 Object* result =
1463 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001464 // undefined means bail out to regular compiler.
1465 if (!result->IsUndefined()) {
1466 return result;
1467 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001468 }
1469
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001470 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001471
1472 // Get the receiver from the stack.
1473 const int argc = arguments().immediate();
1474 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1475
1476 // Check that the receiver isn't a smi.
1477 if (check != NUMBER_CHECK) {
1478 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001479 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001480 }
1481
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001482 // Make sure that it's okay not to patch the on stack receiver
1483 // unless we're doing a receiver map check.
1484 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1485
ager@chromium.org5c838252010-02-19 08:53:10 +00001486 CallOptimization optimization(function);
1487 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001488 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001489
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001490 switch (check) {
1491 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001492 __ IncrementCounter(&Counters::call_const, 1);
1493
1494 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1495 depth = optimization.GetPrototypeDepthOfExpectedType(
1496 JSObject::cast(object), holder);
1497 }
1498
1499 if (depth != kInvalidProtoDepth) {
1500 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1501 ReserveSpaceForFastApiCall(masm(), eax);
1502 }
1503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001505 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001506 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001507
1508 // Patch the receiver on the stack with the global proxy if
1509 // necessary.
1510 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001511 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001512 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1513 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1514 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001515 break;
1516
1517 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001518 if (!function->IsBuiltin()) {
1519 // Calling non-builtins with a value as receiver requires boxing.
1520 __ jmp(&miss);
1521 } else {
1522 // Check that the object is a string or a symbol.
1523 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1524 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1525 __ cmp(eax, FIRST_NONSTRING_TYPE);
1526 __ j(above_equal, &miss, not_taken);
1527 // Check that the maps starting from the prototype haven't changed.
1528 GenerateLoadGlobalFunctionPrototype(masm(),
1529 Context::STRING_FUNCTION_INDEX,
1530 eax);
1531 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1532 ebx, edx, name, &miss);
1533 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 break;
1535
1536 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001537 if (!function->IsBuiltin()) {
1538 // Calling non-builtins with a value as receiver requires boxing.
1539 __ jmp(&miss);
1540 } else {
1541 Label fast;
1542 // Check that the object is a smi or a heap number.
1543 __ test(edx, Immediate(kSmiTagMask));
1544 __ j(zero, &fast, taken);
1545 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1546 __ j(not_equal, &miss, not_taken);
1547 __ bind(&fast);
1548 // Check that the maps starting from the prototype haven't changed.
1549 GenerateLoadGlobalFunctionPrototype(masm(),
1550 Context::NUMBER_FUNCTION_INDEX,
1551 eax);
1552 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1553 ebx, edx, name, &miss);
1554 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001555 break;
1556 }
1557
1558 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 if (!function->IsBuiltin()) {
1560 // Calling non-builtins with a value as receiver requires boxing.
1561 __ jmp(&miss);
1562 } else {
1563 Label fast;
1564 // Check that the object is a boolean.
1565 __ cmp(edx, Factory::true_value());
1566 __ j(equal, &fast, taken);
1567 __ cmp(edx, Factory::false_value());
1568 __ j(not_equal, &miss, not_taken);
1569 __ bind(&fast);
1570 // Check that the maps starting from the prototype haven't changed.
1571 GenerateLoadGlobalFunctionPrototype(masm(),
1572 Context::BOOLEAN_FUNCTION_INDEX,
1573 eax);
1574 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1575 ebx, edx, name, &miss);
1576 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 break;
1578 }
1579
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001580 default:
1581 UNREACHABLE();
1582 }
1583
ager@chromium.org5c838252010-02-19 08:53:10 +00001584 if (depth != kInvalidProtoDepth) {
1585 GenerateFastApiCall(masm(), optimization, argc);
1586 } else {
1587 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1588 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589
1590 // Handle call cache miss.
1591 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001592 if (depth != kInvalidProtoDepth) {
1593 FreeSpaceForFastApiCall(masm(), eax);
1594 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001595 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001597 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
1599 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001600 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601}
1602
1603
ager@chromium.org5c838252010-02-19 08:53:10 +00001604Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 JSObject* holder,
1606 String* name) {
1607 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001608 // -- ecx : name
1609 // -- esp[0] : return address
1610 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1611 // -- ...
1612 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 Label miss;
1615
1616 // Get the number of arguments.
1617 const int argc = arguments().immediate();
1618
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001619 LookupResult lookup;
1620 LookupPostInterceptor(holder, name, &lookup);
1621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 // Get the receiver from the stack.
1623 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001624
ager@chromium.org5c838252010-02-19 08:53:10 +00001625 CallInterceptorCompiler compiler(this, arguments(), ecx);
1626 compiler.Compile(masm(),
1627 object,
1628 holder,
1629 name,
1630 &lookup,
1631 edx,
1632 ebx,
1633 edi,
1634 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001636 // Restore receiver.
1637 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638
1639 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001640 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001642 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 __ j(not_equal, &miss, not_taken);
1644
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001645 // Patch the receiver on the stack with the global proxy if
1646 // necessary.
1647 if (object->IsGlobalObject()) {
1648 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1649 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1650 }
1651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001653 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1655
1656 // Handle load cache miss.
1657 __ bind(&miss);
1658 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001659 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660
1661 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001662 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663}
1664
1665
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001666Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1667 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001668 JSGlobalPropertyCell* cell,
1669 JSFunction* function,
1670 String* name) {
1671 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001672 // -- ecx : name
1673 // -- esp[0] : return address
1674 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1675 // -- ...
1676 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001677 // -----------------------------------
1678 Label miss;
1679
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001680 // Get the number of arguments.
1681 const int argc = arguments().immediate();
1682
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001683 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001684 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001685
1686 // If the object is the holder then we know that it's a global
1687 // object which can only happen for contextual calls. In this case,
1688 // the receiver cannot be a smi.
1689 if (object != holder) {
1690 __ test(edx, Immediate(kSmiTagMask));
1691 __ j(zero, &miss, not_taken);
1692 }
1693
1694 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001695 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001696
1697 // Get the value from the cell.
1698 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1699 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1700
1701 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001702 if (Heap::InNewSpace(function)) {
1703 // We can't embed a pointer to a function in new space so we have
1704 // to verify that the shared function info is unchanged. This has
1705 // the nice side effect that multiple closures based on the same
1706 // function can all use this call IC. Before we load through the
1707 // function, we have to verify that it still is a function.
1708 __ test(edi, Immediate(kSmiTagMask));
1709 __ j(zero, &miss, not_taken);
1710 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1711 __ j(not_equal, &miss, not_taken);
1712
1713 // Check the shared function info. Make sure it hasn't changed.
1714 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1715 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1716 __ j(not_equal, &miss, not_taken);
1717 } else {
1718 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1719 __ j(not_equal, &miss, not_taken);
1720 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001721
1722 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001723 if (object->IsGlobalObject()) {
1724 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1725 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1726 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001727
1728 // Setup the context (function already in edi).
1729 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1730
1731 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001732 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001733 ASSERT(function->is_compiled());
1734 Handle<Code> code(function->code());
1735 ParameterCount expected(function->shared()->formal_parameter_count());
1736 __ InvokeCode(code, expected, arguments(),
1737 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1738
1739 // Handle call cache miss.
1740 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001741 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1742 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1743 __ jmp(ic, RelocInfo::CODE_TARGET);
1744
1745 // Return the generated code.
1746 return GetCode(NORMAL, name);
1747}
1748
1749
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1751 int index,
1752 Map* transition,
1753 String* name) {
1754 // ----------- S t a t e -------------
1755 // -- eax : value
1756 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001757 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 Label miss;
1761
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001762 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001763 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001764 object,
1765 index,
1766 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001767 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001768 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769
1770 // Handle store cache miss.
1771 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001772 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001774 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775
1776 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778}
1779
1780
1781Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1782 AccessorInfo* callback,
1783 String* name) {
1784 // ----------- S t a t e -------------
1785 // -- eax : value
1786 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001787 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 Label miss;
1791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001793 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 __ j(zero, &miss, not_taken);
1795
1796 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001797 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 Immediate(Handle<Map>(object->map())));
1799 __ j(not_equal, &miss, not_taken);
1800
1801 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001802 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001803 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 }
1805
1806 // Stub never generated for non-global objects that require access
1807 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001808 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809
1810 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001811 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1813 __ push(ecx); // name
1814 __ push(eax); // value
1815 __ push(ebx); // restore return address
1816
mads.s.ager31e71382008-08-13 09:32:07 +00001817 // Do tail-call to the runtime system.
1818 ExternalReference store_callback_property =
1819 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001820 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821
1822 // Handle store cache miss.
1823 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001825 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826
1827 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001828 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001829}
1830
1831
1832Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1833 String* name) {
1834 // ----------- S t a t e -------------
1835 // -- eax : value
1836 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001837 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 Label miss;
1841
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001843 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001844 __ j(zero, &miss, not_taken);
1845
1846 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001847 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 Immediate(Handle<Map>(receiver->map())));
1849 __ j(not_equal, &miss, not_taken);
1850
1851 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001852 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001853 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 }
1855
1856 // Stub never generated for non-global objects that require access
1857 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001858 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859
1860 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001861 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 __ push(ecx); // name
1863 __ push(eax); // value
1864 __ push(ebx); // restore return address
1865
mads.s.ager31e71382008-08-13 09:32:07 +00001866 // Do tail-call to the runtime system.
1867 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001869 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870
1871 // Handle store cache miss.
1872 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001874 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875
1876 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001877 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878}
1879
1880
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001881Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1882 JSGlobalPropertyCell* cell,
1883 String* name) {
1884 // ----------- S t a t e -------------
1885 // -- eax : value
1886 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001887 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001888 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001889 // -----------------------------------
1890 Label miss;
1891
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001892 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001893 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001894 Immediate(Handle<Map>(object->map())));
1895 __ j(not_equal, &miss, not_taken);
1896
1897 // Store the value in the cell.
1898 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1899 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1900
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001901 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001902 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001903 __ ret(0);
1904
1905 // Handle store cache miss.
1906 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001907 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1908 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1909 __ jmp(ic, RelocInfo::CODE_TARGET);
1910
1911 // Return the generated code.
1912 return GetCode(NORMAL, name);
1913}
1914
1915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1917 int index,
1918 Map* transition,
1919 String* name) {
1920 // ----------- S t a t e -------------
1921 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001922 // -- ecx : key
1923 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926 Label miss;
1927
1928 __ IncrementCounter(&Counters::keyed_store_field, 1);
1929
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 // Check that the name has not changed.
1931 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1932 __ j(not_equal, &miss, not_taken);
1933
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001934 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001935 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001936 object,
1937 index,
1938 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001939 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001940 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941
1942 // Handle store cache miss.
1943 __ bind(&miss);
1944 __ DecrementCounter(&Counters::keyed_store_field, 1);
1945 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001946 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947
1948 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950}
1951
1952
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001953Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1954 JSObject* object,
1955 JSObject* last) {
1956 // ----------- S t a t e -------------
1957 // -- eax : receiver
1958 // -- ecx : name
1959 // -- esp[0] : return address
1960 // -----------------------------------
1961 Label miss;
1962
1963 // Check that the receiver isn't a smi.
1964 __ test(eax, Immediate(kSmiTagMask));
1965 __ j(zero, &miss, not_taken);
1966
1967 // Check the maps of the full prototype chain. Also check that
1968 // global property cells up to (but not including) the last object
1969 // in the prototype chain are empty.
1970 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1971
1972 // If the last object in the prototype chain is a global object,
1973 // check that the global property cell is empty.
1974 if (last->IsGlobalObject()) {
1975 Object* cell = GenerateCheckPropertyCell(masm(),
1976 GlobalObject::cast(last),
1977 name,
1978 edx,
1979 &miss);
1980 if (cell->IsFailure()) return cell;
1981 }
1982
1983 // Return undefined if maps of the full prototype chain are still the
1984 // same and no global property with this name contains a value.
1985 __ mov(eax, Factory::undefined_value());
1986 __ ret(0);
1987
1988 __ bind(&miss);
1989 GenerateLoadMiss(masm(), Code::LOAD_IC);
1990
1991 // Return the generated code.
1992 return GetCode(NONEXISTENT, Heap::empty_string());
1993}
1994
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001995
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1997 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 int index,
1999 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002001 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002 // -- ecx : name
2003 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 Label miss;
2006
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002007 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 __ bind(&miss);
2009 GenerateLoadMiss(masm(), Code::LOAD_IC);
2010
2011 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013}
2014
2015
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002016Object* LoadStubCompiler::CompileLoadCallback(String* name,
2017 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002019 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002021 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 // -- ecx : name
2023 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025 Label miss;
2026
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002027 Failure* failure = Failure::InternalError();
2028 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2029 callback, name, &miss, &failure);
2030 if (!success) return failure;
2031
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 __ bind(&miss);
2033 GenerateLoadMiss(masm(), Code::LOAD_IC);
2034
2035 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002036 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037}
2038
2039
2040Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2041 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 Object* value,
2043 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002044 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002045 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 // -- ecx : name
2047 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 Label miss;
2050
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002051 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 __ bind(&miss);
2053 GenerateLoadMiss(masm(), Code::LOAD_IC);
2054
2055 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002056 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002057}
2058
2059
2060Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2061 JSObject* holder,
2062 String* name) {
2063 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002064 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 // -- ecx : name
2066 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 Label miss;
2069
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002070 LookupResult lookup;
2071 LookupPostInterceptor(holder, name, &lookup);
2072
ager@chromium.orge2902be2009-06-08 12:21:35 +00002073 // TODO(368): Compile in the whole chain: all the interceptors in
2074 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002075 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002076 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002077 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002078 eax,
2079 ecx,
2080 edx,
2081 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002082 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002083 &miss);
2084
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085 __ bind(&miss);
2086 GenerateLoadMiss(masm(), Code::LOAD_IC);
2087
2088 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002089 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090}
2091
2092
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002093Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2094 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002095 JSGlobalPropertyCell* cell,
2096 String* name,
2097 bool is_dont_delete) {
2098 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002099 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002100 // -- ecx : name
2101 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002102 // -----------------------------------
2103 Label miss;
2104
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002105 // If the object is the holder then we know that it's a global
2106 // object which can only happen for contextual loads. In this case,
2107 // the receiver cannot be a smi.
2108 if (object != holder) {
2109 __ test(eax, Immediate(kSmiTagMask));
2110 __ j(zero, &miss, not_taken);
2111 }
2112
2113 // Check that the maps haven't changed.
2114 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002115
2116 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002117 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2118 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002119
2120 // Check for deleted property if property can actually be deleted.
2121 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002122 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002123 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002124 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002125 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002126 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002127 }
2128
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002129 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002130 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002131 __ ret(0);
2132
2133 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002134 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2135 GenerateLoadMiss(masm(), Code::LOAD_IC);
2136
2137 // Return the generated code.
2138 return GetCode(NORMAL, name);
2139}
2140
2141
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002142Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2143 JSObject* receiver,
2144 JSObject* holder,
2145 int index) {
2146 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002147 // -- eax : key
2148 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151 Label miss;
2152
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153 __ IncrementCounter(&Counters::keyed_load_field, 1);
2154
2155 // Check that the name has not changed.
2156 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2157 __ j(not_equal, &miss, not_taken);
2158
ager@chromium.org5c838252010-02-19 08:53:10 +00002159 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002160
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 __ bind(&miss);
2162 __ DecrementCounter(&Counters::keyed_load_field, 1);
2163 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2164
2165 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002166 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167}
2168
2169
2170Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2171 JSObject* receiver,
2172 JSObject* holder,
2173 AccessorInfo* callback) {
2174 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002175 // -- eax : key
2176 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 Label miss;
2180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2182
2183 // Check that the name has not changed.
2184 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2185 __ j(not_equal, &miss, not_taken);
2186
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002187 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002188 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002189 callback, name, &miss, &failure);
2190 if (!success) return failure;
2191
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 __ bind(&miss);
2193 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2194 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2195
2196 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002197 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198}
2199
2200
2201Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2202 JSObject* receiver,
2203 JSObject* holder,
2204 Object* value) {
2205 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002206 // -- eax : key
2207 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 Label miss;
2211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2213
2214 // Check that the name has not changed.
2215 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2216 __ j(not_equal, &miss, not_taken);
2217
ager@chromium.org5c838252010-02-19 08:53:10 +00002218 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002219 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 __ bind(&miss);
2221 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2222 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2223
2224 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226}
2227
2228
2229Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2230 JSObject* holder,
2231 String* name) {
2232 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002233 // -- eax : key
2234 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002235 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 Label miss;
2238
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2240
2241 // Check that the name has not changed.
2242 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2243 __ j(not_equal, &miss, not_taken);
2244
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002245 LookupResult lookup;
2246 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002247 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002248 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002249 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002250 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002251 eax,
2252 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002253 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002254 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002255 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256 __ bind(&miss);
2257 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2258 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2259
2260 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002261 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262}
2263
2264
2265
2266
2267Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2268 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002269 // -- eax : key
2270 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002271 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002273 Label miss;
2274
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2276
2277 // Check that the name has not changed.
2278 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2279 __ j(not_equal, &miss, not_taken);
2280
ager@chromium.org5c838252010-02-19 08:53:10 +00002281 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002282 __ bind(&miss);
2283 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2284 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2285
2286 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002287 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288}
2289
2290
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002291Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002293 // -- eax : key
2294 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002295 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002296 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002297 Label miss;
2298
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2300
2301 // Check that the name has not changed.
2302 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2303 __ j(not_equal, &miss, not_taken);
2304
ager@chromium.org5c838252010-02-19 08:53:10 +00002305 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 __ bind(&miss);
2307 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2308 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2309
2310 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002311 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312}
2313
2314
2315Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2316 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002317 // -- eax : key
2318 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002319 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 Label miss;
2322
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2324
2325 // Check that the name has not changed.
2326 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2327 __ j(not_equal, &miss, not_taken);
2328
ager@chromium.org5c838252010-02-19 08:53:10 +00002329 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 __ bind(&miss);
2331 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2332 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2333
2334 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002335 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336}
2337
2338
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002339// Specialized stub for constructing objects from functions which only have only
2340// simple assignments of the form this.x = ...; in their body.
2341Object* ConstructStubCompiler::CompileConstructStub(
2342 SharedFunctionInfo* shared) {
2343 // ----------- S t a t e -------------
2344 // -- eax : argc
2345 // -- edi : constructor
2346 // -- esp[0] : return address
2347 // -- esp[4] : last argument
2348 // -----------------------------------
2349 Label generic_stub_call;
2350#ifdef ENABLE_DEBUGGER_SUPPORT
2351 // Check to see whether there are any break points in the function code. If
2352 // there are jump to the generic constructor stub which calls the actual
2353 // code for the function thereby hitting the break points.
2354 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2355 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2356 __ cmp(ebx, Factory::undefined_value());
2357 __ j(not_equal, &generic_stub_call, not_taken);
2358#endif
2359
2360 // Load the initial map and verify that it is in fact a map.
2361 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2362 // Will both indicate a NULL and a Smi.
2363 __ test(ebx, Immediate(kSmiTagMask));
2364 __ j(zero, &generic_stub_call);
2365 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2366 __ j(not_equal, &generic_stub_call);
2367
2368#ifdef DEBUG
2369 // Cannot construct functions this way.
2370 // edi: constructor
2371 // ebx: initial map
2372 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2373 __ Assert(not_equal, "Function constructed by construct stub.");
2374#endif
2375
2376 // Now allocate the JSObject on the heap by moving the new space allocation
2377 // top forward.
2378 // edi: constructor
2379 // ebx: initial map
2380 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2381 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002382 __ AllocateInNewSpace(ecx,
2383 edx,
2384 ecx,
2385 no_reg,
2386 &generic_stub_call,
2387 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002388
2389 // Allocated the JSObject, now initialize the fields and add the heap tag.
2390 // ebx: initial map
2391 // edx: JSObject (untagged)
2392 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2393 __ mov(ebx, Factory::empty_fixed_array());
2394 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2395 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2396
2397 // Push the allocated object to the stack. This is the object that will be
2398 // returned (after it is tagged).
2399 __ push(edx);
2400
2401 // eax: argc
2402 // edx: JSObject (untagged)
2403 // Load the address of the first in-object property into edx.
2404 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2405 // Calculate the location of the first argument. The stack contains the
2406 // allocated object and the return address on top of the argc arguments.
2407 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2408
2409 // Use edi for holding undefined which is used in several places below.
2410 __ mov(edi, Factory::undefined_value());
2411
2412 // eax: argc
2413 // ecx: first argument
2414 // edx: first in-object property of the JSObject
2415 // edi: undefined
2416 // Fill the initialized properties with a constant value or a passed argument
2417 // depending on the this.x = ...; assignment in the function.
2418 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2419 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002420 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002421 // If argument is not passed the property is set to undefined,
2422 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002423 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002424 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002425 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002426 if (CpuFeatures::IsSupported(CMOV)) {
2427 CpuFeatures::Scope use_cmov(CMOV);
2428 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2429 } else {
2430 Label not_passed;
2431 __ j(below_equal, &not_passed);
2432 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2433 __ bind(&not_passed);
2434 }
2435 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002436 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002437 } else {
2438 // Set the property to the constant value.
2439 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2440 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2441 }
2442 }
2443
2444 // Fill the unused in-object property fields with undefined.
2445 for (int i = shared->this_property_assignments_count();
2446 i < shared->CalculateInObjectProperties();
2447 i++) {
2448 __ mov(Operand(edx, i * kPointerSize), edi);
2449 }
2450
2451 // Move argc to ebx and retrieve and tag the JSObject to return.
2452 __ mov(ebx, eax);
2453 __ pop(eax);
2454 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2455
2456 // Remove caller arguments and receiver from the stack and return.
2457 __ pop(ecx);
2458 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2459 __ push(ecx);
2460 __ IncrementCounter(&Counters::constructed_objects, 1);
2461 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2462 __ ret(0);
2463
2464 // Jump to the generic stub in case the specialized code cannot handle the
2465 // construction.
2466 __ bind(&generic_stub_call);
2467 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2468 Handle<Code> generic_construct_stub(code);
2469 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2470
2471 // Return the generated code.
2472 return GetCode();
2473}
2474
2475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002476#undef __
2477
2478} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002479
2480#endif // V8_TARGET_ARCH_IA32