blob: 48d9e674549c08b21db7a3b0fbf454409d00bc7b [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
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +0000175void StubCompiler::GenerateDirectLoadGlobalFunctionPrototype(
176 MacroAssembler* masm, int index, Register prototype) {
177 // Get the global function with the given index.
178 JSFunction* function = JSFunction::cast(Top::global_context()->get(index));
179 // Load its initial map. The global functions all have initial maps.
180 __ Set(prototype, Immediate(Handle<Map>(function->initial_map())));
181 // Load the prototype from the initial map.
182 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
183}
184
185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000186void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
187 Register receiver,
188 Register scratch,
189 Label* miss_label) {
190 // Check that the receiver isn't a smi.
191 __ test(receiver, Immediate(kSmiTagMask));
192 __ j(zero, miss_label, not_taken);
193
194 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000195 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000196 __ j(not_equal, miss_label, not_taken);
197
198 // Load length directly from the JS array.
199 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
200 __ ret(0);
201}
202
203
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000204// Generate code to check if an object is a string. If the object is
205// a string, the map's instance type is left in the scratch register.
206static void GenerateStringCheck(MacroAssembler* masm,
207 Register receiver,
208 Register scratch,
209 Label* smi,
210 Label* non_string_object) {
211 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000212 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000213 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000214
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000215 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000216 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
217 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000218 ASSERT(kNotStringTag != 0);
219 __ test(scratch, Immediate(kNotStringTag));
220 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221}
222
223
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000224void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
225 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000226 Register scratch1,
227 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000228 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000229 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000230
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000231 // Check if the object is a string leaving the instance type in the
232 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000233 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000234
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000235 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000238
239 // Check if the object is a JSValue wrapper.
240 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000241 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000242 __ j(not_equal, miss, not_taken);
243
244 // Check if the wrapped value is a string and load the length
245 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000246 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
247 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
248 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000249 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000250}
251
252
253void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
254 Register receiver,
255 Register scratch1,
256 Register scratch2,
257 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000258 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000259 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000260 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000261}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262
ager@chromium.org7c537e22008-10-16 08:43:32 +0000263
264// Load a fast property out of a holder object (src). In-object properties
265// are loaded directly otherwise the property is loaded from the properties
266// fixed array.
267void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000268 Register dst, Register src,
269 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000270 // Adjust for the number of properties stored in the holder.
271 index -= holder->map()->inobject_properties();
272 if (index < 0) {
273 // Get the property straight out of the holder.
274 int offset = holder->map()->instance_size() + (index * kPointerSize);
275 __ mov(dst, FieldOperand(src, offset));
276 } else {
277 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000278 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000279 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
280 __ mov(dst, FieldOperand(dst, offset));
281 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000282}
283
284
ager@chromium.org5c838252010-02-19 08:53:10 +0000285static void PushInterceptorArguments(MacroAssembler* masm,
286 Register receiver,
287 Register holder,
288 Register name,
289 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000290 __ push(name);
291 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
292 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000293 Register scratch = name;
294 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
295 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000297 __ push(holder);
298 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000299}
300
301
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000302static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
303 Register receiver,
304 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000305 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000306 JSObject* holder_obj) {
307 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000308 __ CallExternalReference(
309 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
310 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000311}
312
313
ager@chromium.org5c838252010-02-19 08:53:10 +0000314// Reserves space for the extra arguments to FastHandleApiCall in the
315// caller's frame.
316//
317// These arguments are set by CheckPrototypes and GenerateFastApiCall.
318static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
319 // ----------- S t a t e -------------
320 // -- esp[0] : return address
321 // -- esp[4] : last argument in the internal frame of the caller
322 // -----------------------------------
323 __ pop(scratch);
324 __ push(Immediate(Smi::FromInt(0)));
325 __ push(Immediate(Smi::FromInt(0)));
326 __ push(Immediate(Smi::FromInt(0)));
327 __ push(Immediate(Smi::FromInt(0)));
328 __ push(scratch);
329}
330
331
332// Undoes the effects of ReserveSpaceForFastApiCall.
333static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
334 // ----------- S t a t e -------------
335 // -- esp[0] : return address
336 // -- esp[4] : last fast api call extra argument
337 // -- ...
338 // -- esp[16] : first fast api call extra argument
339 // -- esp[20] : last argument in the internal frame
340 // -----------------------------------
341 __ pop(scratch);
342 __ add(Operand(esp), Immediate(kPointerSize * 4));
343 __ push(scratch);
344}
345
346
347// Generates call to FastHandleApiCall builtin.
348static void GenerateFastApiCall(MacroAssembler* masm,
349 const CallOptimization& optimization,
350 int argc) {
351 // ----------- S t a t e -------------
352 // -- esp[0] : return address
353 // -- esp[4] : object passing the type check
354 // (last fast api call extra argument,
355 // set by CheckPrototypes)
356 // -- esp[8] : api call data
357 // -- esp[12] : api callback
358 // -- esp[16] : api function
359 // (first fast api call extra argument)
360 // -- esp[20] : last argument
361 // -- ...
362 // -- esp[(argc + 5) * 4] : first argument
363 // -- esp[(argc + 6) * 4] : receiver
364 // -----------------------------------
365
366 // Get the function and setup the context.
367 JSFunction* function = optimization.constant_function();
368 __ mov(edi, Immediate(Handle<JSFunction>(function)));
369 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
370
371 // Pass the additional arguments FastHandleApiCall expects.
372 __ mov(Operand(esp, 4 * kPointerSize), edi);
373 bool info_loaded = false;
374 Object* callback = optimization.api_call_info()->callback();
375 if (Heap::InNewSpace(callback)) {
376 info_loaded = true;
377 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
378 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
379 __ mov(Operand(esp, 3 * kPointerSize), ebx);
380 } else {
381 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
382 }
383 Object* call_data = optimization.api_call_info()->data();
384 if (Heap::InNewSpace(call_data)) {
385 if (!info_loaded) {
386 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
387 }
388 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
389 __ mov(Operand(esp, 2 * kPointerSize), ebx);
390 } else {
391 __ mov(Operand(esp, 2 * kPointerSize),
392 Immediate(Handle<Object>(call_data)));
393 }
394
395 // Set the number of arguments.
396 __ mov(eax, Immediate(argc + 4));
397
398 // Jump to the fast api call builtin (tail call).
399 Handle<Code> code = Handle<Code>(
400 Builtins::builtin(Builtins::FastHandleApiCall));
401 ParameterCount expected(0);
402 __ InvokeCode(code, expected, expected,
403 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
404}
405
406
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000407class CallInterceptorCompiler BASE_EMBEDDED {
408 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000409 CallInterceptorCompiler(StubCompiler* stub_compiler,
410 const ParameterCount& arguments,
411 Register name)
412 : stub_compiler_(stub_compiler),
413 arguments_(arguments),
414 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000415
ager@chromium.org5c838252010-02-19 08:53:10 +0000416 void Compile(MacroAssembler* masm,
417 JSObject* object,
418 JSObject* holder,
419 String* name,
420 LookupResult* lookup,
421 Register receiver,
422 Register scratch1,
423 Register scratch2,
424 Label* miss) {
425 ASSERT(holder->HasNamedInterceptor());
426 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
427
428 // Check that the receiver isn't a smi.
429 __ test(receiver, Immediate(kSmiTagMask));
430 __ j(zero, miss, not_taken);
431
432 CallOptimization optimization(lookup);
433
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000434 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000435 CompileCacheable(masm,
436 object,
437 receiver,
438 scratch1,
439 scratch2,
440 holder,
441 lookup,
442 name,
443 optimization,
444 miss);
445 } else {
446 CompileRegular(masm,
447 object,
448 receiver,
449 scratch1,
450 scratch2,
451 name,
452 holder,
453 miss);
454 }
455 }
456
457 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000458 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000459 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000460 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000461 Register scratch1,
462 Register scratch2,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000463 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000464 LookupResult* lookup,
465 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000466 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000467 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000468 ASSERT(optimization.is_constant_call());
469 ASSERT(!lookup->holder()->IsGlobalObject());
470
471 int depth1 = kInvalidProtoDepth;
472 int depth2 = kInvalidProtoDepth;
473 bool can_do_fast_api_call = false;
474 if (optimization.is_simple_api_call() &&
475 !lookup->holder()->IsGlobalObject()) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000476 depth1 =
477 optimization.GetPrototypeDepthOfExpectedType(object,
478 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000479 if (depth1 == kInvalidProtoDepth) {
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000480 depth2 =
481 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
482 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000483 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000484 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
485 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000486 }
487
ager@chromium.org5c838252010-02-19 08:53:10 +0000488 __ IncrementCounter(&Counters::call_const_interceptor, 1);
489
490 if (can_do_fast_api_call) {
491 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
492 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000493 }
494
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000495 // Check that the maps from receiver to interceptor's holder
496 // haven't changed and thus we can invoke interceptor.
ager@chromium.org5c838252010-02-19 08:53:10 +0000497 Label miss_cleanup;
498 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
499 Register holder =
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000500 stub_compiler_->CheckPrototypes(object, receiver,
501 interceptor_holder, scratch1,
502 scratch2, name, depth1, miss);
ager@chromium.org5c838252010-02-19 08:53:10 +0000503
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000504 // Invoke an interceptor and if it provides a value,
505 // branch to |regular_invoke|.
ager@chromium.org5c838252010-02-19 08:53:10 +0000506 Label regular_invoke;
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000507 LoadWithInterceptor(masm, receiver, holder, interceptor_holder,
508 &regular_invoke);
ager@chromium.org5c838252010-02-19 08:53:10 +0000509
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000510 // Interceptor returned nothing for this property. Try to use cached
511 // constant function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000512
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000513 // Check that the maps from interceptor's holder to constant function's
514 // holder haven't changed and thus we can use cached constant function.
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000515 if (interceptor_holder != lookup->holder()) {
516 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
517 lookup->holder(), scratch1,
518 scratch2, name, depth2, miss);
519 } else {
520 // CheckPrototypes has a side effect of fetching a 'holder'
521 // for API (object which is instanceof for the signature). It's
522 // safe to omit it here, as if present, it should be fetched
523 // by the previous CheckPrototypes.
524 ASSERT(depth2 == kInvalidProtoDepth);
525 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000526
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000527 // Invoke function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000528 if (can_do_fast_api_call) {
529 GenerateFastApiCall(masm, optimization, arguments_.immediate());
530 } else {
531 __ InvokeFunction(optimization.constant_function(), arguments_,
532 JUMP_FUNCTION);
533 }
534
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000535 // Deferred code for fast API call case---clean preallocated space.
ager@chromium.org5c838252010-02-19 08:53:10 +0000536 if (can_do_fast_api_call) {
537 __ bind(&miss_cleanup);
538 FreeSpaceForFastApiCall(masm, scratch1);
539 __ jmp(miss_label);
540 }
541
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000542 // Invoke a regular function.
ager@chromium.org5c838252010-02-19 08:53:10 +0000543 __ bind(&regular_invoke);
544 if (can_do_fast_api_call) {
545 FreeSpaceForFastApiCall(masm, scratch1);
546 }
547 }
548
549 void CompileRegular(MacroAssembler* masm,
550 JSObject* object,
551 Register receiver,
552 Register scratch1,
553 Register scratch2,
554 String* name,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000555 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000556 Label* miss_label) {
557 Register holder =
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000558 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +0000559 scratch1, scratch2, name,
560 miss_label);
561
562 __ EnterInternalFrame();
563 // Save the name_ register across the call.
564 __ push(name_);
565
566 PushInterceptorArguments(masm,
567 receiver,
568 holder,
569 name_,
kmillikin@chromium.org9155e252010-05-26 13:27:57 +0000570 interceptor_holder);
ager@chromium.org5c838252010-02-19 08:53:10 +0000571
572 __ CallExternalReference(
573 ExternalReference(
574 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
575 5);
576
577 // Restore the name_ register.
578 __ pop(name_);
579 __ LeaveInternalFrame();
580 }
581
582 void LoadWithInterceptor(MacroAssembler* masm,
583 Register receiver,
584 Register holder,
585 JSObject* holder_obj,
586 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000587 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000588 __ push(holder); // Save the holder.
589 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000590
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000591 CompileCallLoadPropertyWithInterceptor(masm,
592 receiver,
593 holder,
594 name_,
595 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000596
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000597 __ pop(name_); // Restore the name.
598 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000599 __ LeaveInternalFrame();
600
601 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000602 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000603 }
604
ager@chromium.org5c838252010-02-19 08:53:10 +0000605 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000606 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000607 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000608};
609
610
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000611void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
612 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
613 Code* code = NULL;
614 if (kind == Code::LOAD_IC) {
615 code = Builtins::builtin(Builtins::LoadIC_Miss);
616 } else {
617 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
618 }
619
620 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000621 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000622}
623
624
ager@chromium.org5c838252010-02-19 08:53:10 +0000625// Both name_reg and receiver_reg are preserved on jumps to miss_label,
626// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000627void StubCompiler::GenerateStoreField(MacroAssembler* masm,
628 JSObject* object,
629 int index,
630 Map* transition,
631 Register receiver_reg,
632 Register name_reg,
633 Register scratch,
634 Label* miss_label) {
635 // Check that the object isn't a smi.
636 __ test(receiver_reg, Immediate(kSmiTagMask));
637 __ j(zero, miss_label, not_taken);
638
639 // Check that the map of the object hasn't changed.
640 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
641 Immediate(Handle<Map>(object->map())));
642 __ j(not_equal, miss_label, not_taken);
643
644 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000645 if (object->IsJSGlobalProxy()) {
646 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000647 }
648
649 // Stub never generated for non-global objects that require access
650 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000651 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000652
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000653 // Perform map transition for the receiver if necessary.
654 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
655 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000656 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000657 __ pop(scratch); // Return address.
658 __ push(receiver_reg);
659 __ push(Immediate(Handle<Map>(transition)));
660 __ push(eax);
661 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000662 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000663 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000664 return;
665 }
666
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000667 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000668 // Update the map of the object; no write barrier updating is
669 // needed because the map is never in new space.
670 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
671 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000672 }
673
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000674 // Adjust for the number of properties stored in the object. Even in the
675 // face of a transition we can use the old map here because the size of the
676 // object and the number of in-object properties is not going to change.
677 index -= object->map()->inobject_properties();
678
ager@chromium.org7c537e22008-10-16 08:43:32 +0000679 if (index < 0) {
680 // Set the property straight into the object.
681 int offset = object->map()->instance_size() + (index * kPointerSize);
682 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000683
ager@chromium.org7c537e22008-10-16 08:43:32 +0000684 // 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(receiver_reg, offset, name_reg, scratch);
688 } else {
689 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000690 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000691 // Get the properties array (optimistically).
692 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000693 __ mov(FieldOperand(scratch, offset), eax);
694
695 // Update the write barrier for the array address.
696 // Pass the value being stored in the now unused name_reg.
697 __ mov(name_reg, Operand(eax));
698 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
699 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
701 // Return the value (register eax).
702 __ ret(0);
703}
704
705
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000706// Generate code to check that a global property cell is empty. Create
707// the property cell at compilation time if no cell exists for the
708// property.
709static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
710 GlobalObject* global,
711 String* name,
712 Register scratch,
713 Label* miss) {
714 Object* probe = global->EnsurePropertyCell(name);
715 if (probe->IsFailure()) return probe;
716 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
717 ASSERT(cell->value()->IsTheHole());
718 __ mov(scratch, Immediate(Handle<Object>(cell)));
719 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
720 Immediate(Factory::the_hole_value()));
721 __ j(not_equal, miss, not_taken);
722 return cell;
723}
724
725
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000726#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000727#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000728
729
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000730Register StubCompiler::CheckPrototypes(JSObject* object,
731 Register object_reg,
732 JSObject* holder,
733 Register holder_reg,
734 Register scratch,
735 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000736 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000737 Label* miss) {
738 // Check that the maps haven't changed.
739 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000740 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
741 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000742
743 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000744 // that their maps haven't changed. We also need to check that the
745 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000746 while (object != holder) {
747 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000748 Object* cell = GenerateCheckPropertyCell(masm(),
749 GlobalObject::cast(object),
750 name,
751 scratch,
752 miss);
753 if (cell->IsFailure()) {
754 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000755 return result;
756 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000757 }
758 object = JSObject::cast(object->GetPrototype());
759 }
760
ager@chromium.org5c838252010-02-19 08:53:10 +0000761 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000762 return result;
763}
764
765
766void StubCompiler::GenerateLoadField(JSObject* object,
767 JSObject* holder,
768 Register receiver,
769 Register scratch1,
770 Register scratch2,
771 int index,
772 String* name,
773 Label* miss) {
774 // Check that the receiver isn't a smi.
775 __ test(receiver, Immediate(kSmiTagMask));
776 __ j(zero, miss, not_taken);
777
778 // Check the prototype chain.
779 Register reg =
780 CheckPrototypes(object, receiver, holder,
781 scratch1, scratch2, name, miss);
782
783 // Get the value from the properties.
784 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
785 __ ret(0);
786}
787
788
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000789bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000790 JSObject* holder,
791 Register receiver,
792 Register name_reg,
793 Register scratch1,
794 Register scratch2,
795 AccessorInfo* callback,
796 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000797 Label* miss,
798 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000799 // Check that the receiver isn't a smi.
800 __ test(receiver, Immediate(kSmiTagMask));
801 __ j(zero, miss, not_taken);
802
803 // Check that the maps haven't changed.
804 Register reg =
805 CheckPrototypes(object, receiver, holder,
806 scratch1, scratch2, name, miss);
807
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000808 Handle<AccessorInfo> callback_handle(callback);
809
810 Register other = reg.is(scratch1) ? scratch2 : scratch1;
811 __ EnterInternalFrame();
812 __ PushHandleScope(other);
813 // Push the stack address where the list of arguments ends
814 __ mov(other, esp);
815 __ sub(Operand(other), Immediate(2 * kPointerSize));
816 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000817 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000818 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000819 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000820 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000821 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000822 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000823 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000824 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000825 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000826 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000827
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000828 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000829 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000830 Address getter_address = v8::ToCData<Address>(callback->getter());
831 ApiFunction fun(getter_address);
832 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000833 // Emitting a stub call may try to allocate (if the code is not
834 // already generated). Do not allow the assembler to perform a
835 // garbage collection but instead return the allocation failure
836 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000837 Object* result = masm()->TryCallStub(&stub);
838 if (result->IsFailure()) {
839 *failure = Failure::cast(result);
840 return false;
841 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000842
843 // We need to avoid using eax since that now holds the result.
844 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000845 // Emitting PopHandleScope may try to allocate. Do not allow the
846 // assembler to perform a garbage collection but instead return a
847 // failure object.
848 result = masm()->TryPopHandleScope(eax, tmp);
849 if (result->IsFailure()) {
850 *failure = Failure::cast(result);
851 return false;
852 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000853 __ LeaveInternalFrame();
854
855 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000856 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000857}
858
859
860void StubCompiler::GenerateLoadConstant(JSObject* object,
861 JSObject* holder,
862 Register receiver,
863 Register scratch1,
864 Register scratch2,
865 Object* value,
866 String* name,
867 Label* miss) {
868 // Check that the receiver isn't a smi.
869 __ test(receiver, Immediate(kSmiTagMask));
870 __ j(zero, miss, not_taken);
871
872 // Check that the maps haven't changed.
873 Register reg =
874 CheckPrototypes(object, receiver, holder,
875 scratch1, scratch2, name, miss);
876
877 // Return the constant value.
878 __ mov(eax, Handle<Object>(value));
879 __ ret(0);
880}
881
882
883void StubCompiler::GenerateLoadInterceptor(JSObject* object,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000884 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000885 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000886 Register receiver,
887 Register name_reg,
888 Register scratch1,
889 Register scratch2,
890 String* name,
891 Label* miss) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000892 ASSERT(interceptor_holder->HasNamedInterceptor());
893 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
894
895 // Check that the receiver isn't a smi.
896 __ test(receiver, Immediate(kSmiTagMask));
897 __ j(zero, miss, not_taken);
898
899 // So far the most popular follow ups for interceptor loads are FIELD
900 // and CALLBACKS, so inline only them, other cases may be added
901 // later.
902 bool compile_followup_inline = false;
903 if (lookup->IsProperty() && lookup->IsCacheable()) {
904 if (lookup->type() == FIELD) {
905 compile_followup_inline = true;
906 } else if (lookup->type() == CALLBACKS &&
907 lookup->GetCallbackObject()->IsAccessorInfo() &&
908 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
909 compile_followup_inline = true;
910 }
911 }
912
913 if (compile_followup_inline) {
914 // Compile the interceptor call, followed by inline code to load the
915 // property from further up the prototype chain if the call fails.
916 // Check that the maps haven't changed.
917 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
918 scratch1, scratch2, name, miss);
919 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
920
921 // Save necessary data before invoking an interceptor.
922 // Requires a frame to make GC aware of pushed pointers.
923 __ EnterInternalFrame();
924
925 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
926 // CALLBACKS case needs a receiver to be passed into C++ callback.
927 __ push(receiver);
928 }
929 __ push(holder_reg);
930 __ push(name_reg);
931
932 // Invoke an interceptor. Note: map checks from receiver to
933 // interceptor's holder has been compiled before (see a caller
934 // of this method.)
935 CompileCallLoadPropertyWithInterceptor(masm(),
936 receiver,
937 holder_reg,
938 name_reg,
939 interceptor_holder);
940
941 // Check if interceptor provided a value for property. If it's
942 // the case, return immediately.
943 Label interceptor_failed;
944 __ cmp(eax, Factory::no_interceptor_result_sentinel());
945 __ j(equal, &interceptor_failed);
946 __ LeaveInternalFrame();
947 __ ret(0);
948
949 __ bind(&interceptor_failed);
950 __ pop(name_reg);
951 __ pop(holder_reg);
952 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
953 __ pop(receiver);
954 }
955
956 __ LeaveInternalFrame();
957
958 // Check that the maps from interceptor's holder to lookup's holder
959 // haven't changed. And load lookup's holder into holder_reg.
960 if (interceptor_holder != lookup->holder()) {
961 holder_reg = CheckPrototypes(interceptor_holder,
962 holder_reg,
963 lookup->holder(),
964 scratch1,
965 scratch2,
966 name,
967 miss);
968 }
969
970 if (lookup->type() == FIELD) {
971 // We found FIELD property in prototype chain of interceptor's holder.
972 // Retrieve a field from field's holder.
973 GenerateFastPropertyLoad(masm(), eax, holder_reg,
974 lookup->holder(), lookup->GetFieldIndex());
975 __ ret(0);
976 } else {
977 // We found CALLBACKS property in prototype chain of interceptor's
978 // holder.
979 ASSERT(lookup->type() == CALLBACKS);
980 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
981 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
982 ASSERT(callback != NULL);
983 ASSERT(callback->getter() != NULL);
984
985 // Tail call to runtime.
986 // Important invariant in CALLBACKS case: the code above must be
987 // structured to never clobber |receiver| register.
988 __ pop(scratch2); // return address
989 __ push(receiver);
990 __ push(holder_reg);
991 __ mov(holder_reg, Immediate(Handle<AccessorInfo>(callback)));
992 __ push(holder_reg);
993 __ push(FieldOperand(holder_reg, AccessorInfo::kDataOffset));
994 __ push(name_reg);
995 __ push(scratch2); // restore return address
996
997 ExternalReference ref =
998 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
999 __ TailCallExternalReference(ref, 5, 1);
1000 }
1001 } else { // !compile_followup_inline
1002 // Call the runtime system to load the interceptor.
1003 // Check that the maps haven't changed.
1004 Register holder_reg =
1005 CheckPrototypes(object, receiver, interceptor_holder,
1006 scratch1, scratch2, name, miss);
1007 __ pop(scratch2); // save old return address
1008 PushInterceptorArguments(masm(), receiver, holder_reg,
1009 name_reg, interceptor_holder);
1010 __ push(scratch2); // restore old return address
1011
1012 ExternalReference ref = ExternalReference(
1013 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
1014 __ TailCallExternalReference(ref, 5, 1);
1015 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001016}
1017
1018
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019// TODO(1241006): Avoid having lazy compile stubs specialized by the
1020// number of arguments. It is not needed anymore.
1021Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001022 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001023 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024
1025 // Push a copy of the function onto the stack.
1026 __ push(edi);
1027
1028 __ push(edi); // function is also the parameter to the runtime call
1029 __ CallRuntime(Runtime::kLazyCompile, 1);
1030 __ pop(edi);
1031
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001032 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001033 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001034
1035 // Do a tail-call of the compiled function.
1036 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1037 __ jmp(Operand(ecx));
1038
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001039 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001040}
1041
1042
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001043void CallStubCompiler::GenerateNameCheck(String* name, Label* miss) {
1044 if (kind_ == Code::KEYED_CALL_IC) {
1045 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1046 __ j(not_equal, miss, not_taken);
1047 }
1048}
1049
1050
1051void CallStubCompiler::GenerateMissBranch() {
1052 Handle<Code> ic = ComputeCallMiss(arguments().immediate(), kind_);
1053 __ jmp(ic, RelocInfo::CODE_TARGET);
1054}
1055
1056
ager@chromium.org5c838252010-02-19 08:53:10 +00001057Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001058 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001059 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001060 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001061 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001062 // -- ecx : name
1063 // -- esp[0] : return address
1064 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1065 // -- ...
1066 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001068 Label miss;
1069
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001070 GenerateNameCheck(name, &miss);
1071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 // Get the receiver from the stack.
1073 const int argc = arguments().immediate();
1074 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1075
1076 // Check that the receiver isn't a smi.
1077 __ test(edx, Immediate(kSmiTagMask));
1078 __ j(zero, &miss, not_taken);
1079
1080 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001081 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001082
ager@chromium.org7c537e22008-10-16 08:43:32 +00001083 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001084
1085 // Check that the function really is a function.
1086 __ test(edi, Immediate(kSmiTagMask));
1087 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001088 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 __ j(not_equal, &miss, not_taken);
1090
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001091 // Patch the receiver on the stack with the global proxy if
1092 // necessary.
1093 if (object->IsGlobalObject()) {
1094 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1095 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1096 }
1097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098 // Invoke the function.
1099 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1100
1101 // Handle call cache miss.
1102 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001103 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001104
1105 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001106 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001107}
1108
1109
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001110Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1111 JSObject* holder,
1112 JSFunction* function,
1113 String* name,
1114 CheckType check) {
1115 // ----------- S t a t e -------------
1116 // -- ecx : name
1117 // -- esp[0] : return address
1118 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1119 // -- ...
1120 // -- esp[(argc + 1) * 4] : receiver
1121 // -----------------------------------
1122 ASSERT(check == RECEIVER_MAP_CHECK);
1123
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001124 // If object is not an array, bail out to regular call.
1125 if (!object->IsJSArray()) {
1126 return Heap::undefined_value();
1127 }
1128
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001129 Label miss;
1130
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001131 GenerateNameCheck(name, &miss);
1132
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001133 // Get the receiver from the stack.
1134 const int argc = arguments().immediate();
1135 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1136
1137 // Check that the receiver isn't a smi.
1138 __ test(edx, Immediate(kSmiTagMask));
1139 __ j(zero, &miss);
1140
1141 CheckPrototypes(JSObject::cast(object), edx,
1142 holder, ebx,
1143 eax, name, &miss);
1144
1145 if (argc == 0) {
1146 // Noop, return the length.
1147 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1148 __ ret((argc + 1) * kPointerSize);
1149 } else {
1150 // Get the elements array of the object.
1151 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1152
1153 // Check that the elements are in fast mode (not dictionary).
1154 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1155 Immediate(Factory::fixed_array_map()));
1156 __ j(not_equal, &miss);
1157
1158 if (argc == 1) { // Otherwise fall through to call builtin.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001159 Label call_builtin, exit, with_write_barrier, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001160
1161 // Get the array's length into eax and calculate new length.
1162 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1163 STATIC_ASSERT(kSmiTagSize == 1);
1164 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001165 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001166
1167 // Get the element's length into ecx.
1168 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001169
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001170 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001171 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001172 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001173
1174 // Save new length.
1175 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1176
1177 // Push the element.
1178 __ lea(edx, FieldOperand(ebx,
1179 eax, times_half_pointer_size,
1180 FixedArray::kHeaderSize - argc * kPointerSize));
1181 __ mov(ecx, Operand(esp, argc * kPointerSize));
1182 __ mov(Operand(edx, 0), ecx);
1183
1184 // Check if value is a smi.
1185 __ test(ecx, Immediate(kSmiTagMask));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001186 __ j(not_zero, &with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001187
1188 __ bind(&exit);
1189 __ ret((argc + 1) * kPointerSize);
1190
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001191 __ bind(&with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001192
1193 __ InNewSpace(ebx, ecx, equal, &exit);
1194
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001195 __ RecordWriteHelper(ebx, edx, ecx);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001196 __ ret((argc + 1) * kPointerSize);
1197
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001198 __ bind(&attempt_to_grow_elements);
1199 ExternalReference new_space_allocation_top =
1200 ExternalReference::new_space_allocation_top_address();
1201 ExternalReference new_space_allocation_limit =
1202 ExternalReference::new_space_allocation_limit_address();
1203
1204 const int kAllocationDelta = 4;
1205 // Load top.
1206 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1207
1208 // Check if it's the end of elements.
1209 __ lea(edx, FieldOperand(ebx,
1210 eax, times_half_pointer_size,
1211 FixedArray::kHeaderSize - argc * kPointerSize));
1212 __ cmp(edx, Operand(ecx));
1213 __ j(not_equal, &call_builtin);
1214 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1215 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001216 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001217
1218 // We fit and could grow elements.
1219 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1220 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001221
1222 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001223 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001224 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001225 for (int i = 1; i < kAllocationDelta; i++) {
1226 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001227 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001228 }
1229
1230 // Restore receiver to edx as finish sequence assumes it's here.
1231 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1232
1233 // Increment element's and array's sizes.
1234 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001235 Immediate(Smi::FromInt(kAllocationDelta)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001236 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1237
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001238 // Elements are in new space, so write barrier is not required.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001239 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001240
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001241 __ bind(&call_builtin);
1242 }
1243
1244 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1245 argc + 1,
1246 1);
1247 }
1248
1249 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001250 GenerateMissBranch();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001251
1252 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001253 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001254}
1255
1256
1257Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1258 JSObject* holder,
1259 JSFunction* function,
1260 String* name,
1261 CheckType check) {
1262 // ----------- S t a t e -------------
1263 // -- ecx : name
1264 // -- esp[0] : return address
1265 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1266 // -- ...
1267 // -- esp[(argc + 1) * 4] : receiver
1268 // -----------------------------------
1269 ASSERT(check == RECEIVER_MAP_CHECK);
1270
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001271 // If object is not an array, bail out to regular call.
1272 if (!object->IsJSArray()) {
1273 return Heap::undefined_value();
1274 }
1275
ager@chromium.orgac091b72010-05-05 07:34:42 +00001276 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001277
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001278 GenerateNameCheck(name, &miss);
1279
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001280 // Get the receiver from the stack.
1281 const int argc = arguments().immediate();
1282 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1283
1284 // Check that the receiver isn't a smi.
1285 __ test(edx, Immediate(kSmiTagMask));
1286 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001287 CheckPrototypes(JSObject::cast(object), edx,
1288 holder, ebx,
1289 eax, name, &miss);
1290
1291 // Get the elements array of the object.
1292 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1293
1294 // Check that the elements are in fast mode (not dictionary).
1295 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1296 Immediate(Factory::fixed_array_map()));
1297 __ j(not_equal, &miss);
1298
1299 // Get the array's length into ecx and calculate new length.
1300 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1301 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001302 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001303
1304 // Get the last element.
1305 STATIC_ASSERT(kSmiTagSize == 1);
1306 STATIC_ASSERT(kSmiTag == 0);
1307 __ mov(eax, FieldOperand(ebx,
1308 ecx, times_half_pointer_size,
1309 FixedArray::kHeaderSize));
1310 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1311 __ j(equal, &call_builtin);
1312
1313 // Set the array's length.
1314 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1315
1316 // Fill with the hole.
1317 __ mov(FieldOperand(ebx,
1318 ecx, times_half_pointer_size,
1319 FixedArray::kHeaderSize),
1320 Immediate(Factory::the_hole_value()));
1321 __ ret((argc + 1) * kPointerSize);
1322
ager@chromium.orgac091b72010-05-05 07:34:42 +00001323 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001324 __ mov(eax, Immediate(Factory::undefined_value()));
1325 __ ret((argc + 1) * kPointerSize);
1326
1327 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001328 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1329 argc + 1,
1330 1);
1331
1332 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001333 GenerateMissBranch();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001334
1335 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001336 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001337}
1338
1339
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001340Object* CallStubCompiler::CompileStringCharCodeAtCall(Object* object,
1341 JSObject* holder,
1342 JSFunction* function,
1343 String* name,
1344 CheckType check) {
1345 // ----------- S t a t e -------------
1346 // -- ecx : function name
1347 // -- esp[0] : return address
1348 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1349 // -- ...
1350 // -- esp[(argc + 1) * 4] : receiver
1351 // -----------------------------------
1352
1353 const int argc = arguments().immediate();
1354
1355 Label miss;
1356 Label index_out_of_range;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001357 GenerateNameCheck(name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001358
1359 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001360 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1361 Context::STRING_FUNCTION_INDEX,
1362 eax);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001363 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1364 ebx, edx, name, &miss);
1365
1366 Register receiver = ebx;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001367 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001368 Register scratch = edx;
1369 Register result = eax;
1370 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1371 if (argc > 0) {
1372 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1373 } else {
1374 __ Set(index, Immediate(Factory::undefined_value()));
1375 }
1376
1377 StringCharCodeAtGenerator char_code_at_generator(receiver,
1378 index,
1379 scratch,
1380 result,
1381 &miss, // When not a string.
1382 &miss, // When not a number.
1383 &index_out_of_range,
1384 STRING_INDEX_IS_NUMBER);
1385 char_code_at_generator.GenerateFast(masm());
1386 __ ret((argc + 1) * kPointerSize);
1387
1388 ICRuntimeCallHelper call_helper;
1389 char_code_at_generator.GenerateSlow(masm(), call_helper);
1390
1391 __ bind(&index_out_of_range);
1392 __ Set(eax, Immediate(Factory::nan_value()));
1393 __ ret((argc + 1) * kPointerSize);
1394
1395 __ bind(&miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001396
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001397 GenerateMissBranch();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001398
1399 // Return the generated code.
1400 return GetCode(function);
1401}
1402
1403
1404Object* CallStubCompiler::CompileStringCharAtCall(Object* object,
1405 JSObject* holder,
1406 JSFunction* function,
1407 String* name,
1408 CheckType check) {
1409 // ----------- S t a t e -------------
1410 // -- ecx : function name
1411 // -- esp[0] : return address
1412 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1413 // -- ...
1414 // -- esp[(argc + 1) * 4] : receiver
1415 // -----------------------------------
1416
1417 const int argc = arguments().immediate();
1418
1419 Label miss;
1420 Label index_out_of_range;
1421
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001422 GenerateNameCheck(name, &miss);
1423
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001424 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001425 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1426 Context::STRING_FUNCTION_INDEX,
1427 eax);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001428 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1429 ebx, edx, name, &miss);
1430
1431 Register receiver = eax;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001432 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001433 Register scratch1 = ebx;
1434 Register scratch2 = edx;
1435 Register result = eax;
1436 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1437 if (argc > 0) {
1438 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1439 } else {
1440 __ Set(index, Immediate(Factory::undefined_value()));
1441 }
1442
1443 StringCharAtGenerator char_at_generator(receiver,
1444 index,
1445 scratch1,
1446 scratch2,
1447 result,
1448 &miss, // When not a string.
1449 &miss, // When not a number.
1450 &index_out_of_range,
1451 STRING_INDEX_IS_NUMBER);
1452 char_at_generator.GenerateFast(masm());
1453 __ ret((argc + 1) * kPointerSize);
1454
1455 ICRuntimeCallHelper call_helper;
1456 char_at_generator.GenerateSlow(masm(), call_helper);
1457
1458 __ bind(&index_out_of_range);
1459 __ Set(eax, Immediate(Factory::empty_string()));
1460 __ ret((argc + 1) * kPointerSize);
1461
1462 __ bind(&miss);
1463 // Restore function name in ecx.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001464
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001465 GenerateMissBranch();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001466
1467 // Return the generated code.
1468 return GetCode(function);
1469}
1470
1471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472Object* CallStubCompiler::CompileCallConstant(Object* object,
1473 JSObject* holder,
1474 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001475 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001476 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001478 // -- ecx : name
1479 // -- esp[0] : return address
1480 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1481 // -- ...
1482 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001484
1485 SharedFunctionInfo* function_info = function->shared();
1486 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001487 const int id = function_info->custom_call_generator_id();
1488 Object* result =
1489 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001490 // undefined means bail out to regular compiler.
1491 if (!result->IsUndefined()) {
1492 return result;
1493 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001494 }
1495
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001496 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001498 GenerateNameCheck(name, &miss_in_smi_check);
1499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500 // Get the receiver from the stack.
1501 const int argc = arguments().immediate();
1502 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1503
1504 // Check that the receiver isn't a smi.
1505 if (check != NUMBER_CHECK) {
1506 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001507 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 }
1509
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001510 // Make sure that it's okay not to patch the on stack receiver
1511 // unless we're doing a receiver map check.
1512 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1513
ager@chromium.org5c838252010-02-19 08:53:10 +00001514 CallOptimization optimization(function);
1515 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001516 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001517
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001518 switch (check) {
1519 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001520 __ IncrementCounter(&Counters::call_const, 1);
1521
1522 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1523 depth = optimization.GetPrototypeDepthOfExpectedType(
1524 JSObject::cast(object), holder);
1525 }
1526
1527 if (depth != kInvalidProtoDepth) {
1528 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1529 ReserveSpaceForFastApiCall(masm(), eax);
1530 }
1531
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001533 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001534 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001535
1536 // Patch the receiver on the stack with the global proxy if
1537 // necessary.
1538 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001539 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001540 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1541 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1542 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001543 break;
1544
1545 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001546 if (!function->IsBuiltin()) {
1547 // Calling non-builtins with a value as receiver requires boxing.
1548 __ jmp(&miss);
1549 } else {
1550 // Check that the object is a string or a symbol.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001551 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552 __ j(above_equal, &miss, not_taken);
1553 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001554 GenerateDirectLoadGlobalFunctionPrototype(
1555 masm(), Context::STRING_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001556 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1557 ebx, edx, name, &miss);
1558 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001559 break;
1560
1561 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001562 if (!function->IsBuiltin()) {
1563 // Calling non-builtins with a value as receiver requires boxing.
1564 __ jmp(&miss);
1565 } else {
1566 Label fast;
1567 // Check that the object is a smi or a heap number.
1568 __ test(edx, Immediate(kSmiTagMask));
1569 __ j(zero, &fast, taken);
1570 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1571 __ j(not_equal, &miss, not_taken);
1572 __ bind(&fast);
1573 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001574 GenerateDirectLoadGlobalFunctionPrototype(
1575 masm(), Context::NUMBER_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001576 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1577 ebx, edx, name, &miss);
1578 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 break;
1580 }
1581
1582 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001583 if (!function->IsBuiltin()) {
1584 // Calling non-builtins with a value as receiver requires boxing.
1585 __ jmp(&miss);
1586 } else {
1587 Label fast;
1588 // Check that the object is a boolean.
1589 __ cmp(edx, Factory::true_value());
1590 __ j(equal, &fast, taken);
1591 __ cmp(edx, Factory::false_value());
1592 __ j(not_equal, &miss, not_taken);
1593 __ bind(&fast);
1594 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001595 GenerateDirectLoadGlobalFunctionPrototype(
1596 masm(), Context::BOOLEAN_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001597 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1598 ebx, edx, name, &miss);
1599 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001600 break;
1601 }
1602
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 default:
1604 UNREACHABLE();
1605 }
1606
ager@chromium.org5c838252010-02-19 08:53:10 +00001607 if (depth != kInvalidProtoDepth) {
1608 GenerateFastApiCall(masm(), optimization, argc);
1609 } else {
1610 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1611 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612
1613 // Handle call cache miss.
1614 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001615 if (depth != kInvalidProtoDepth) {
1616 FreeSpaceForFastApiCall(masm(), eax);
1617 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001618 __ bind(&miss_in_smi_check);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001619 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620
1621 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001622 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001623}
1624
1625
ager@chromium.org5c838252010-02-19 08:53:10 +00001626Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001627 JSObject* holder,
1628 String* name) {
1629 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001630 // -- ecx : name
1631 // -- esp[0] : return address
1632 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1633 // -- ...
1634 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636 Label miss;
1637
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001638 GenerateNameCheck(name, &miss);
1639
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 // Get the number of arguments.
1641 const int argc = arguments().immediate();
1642
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001643 LookupResult lookup;
1644 LookupPostInterceptor(holder, name, &lookup);
1645
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001646 // Get the receiver from the stack.
1647 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001648
ager@chromium.org5c838252010-02-19 08:53:10 +00001649 CallInterceptorCompiler compiler(this, arguments(), ecx);
1650 compiler.Compile(masm(),
1651 object,
1652 holder,
1653 name,
1654 &lookup,
1655 edx,
1656 ebx,
1657 edi,
1658 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001660 // Restore receiver.
1661 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001662
1663 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001664 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001666 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667 __ j(not_equal, &miss, not_taken);
1668
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001669 // Patch the receiver on the stack with the global proxy if
1670 // necessary.
1671 if (object->IsGlobalObject()) {
1672 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1673 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1674 }
1675
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001677 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001678 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1679
1680 // Handle load cache miss.
1681 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001682 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683
1684 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001685 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686}
1687
1688
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001689Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1690 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001691 JSGlobalPropertyCell* cell,
1692 JSFunction* function,
1693 String* name) {
1694 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001695 // -- ecx : name
1696 // -- esp[0] : return address
1697 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1698 // -- ...
1699 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001700 // -----------------------------------
1701 Label miss;
1702
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001703 GenerateNameCheck(name, &miss);
1704
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001705 // Get the number of arguments.
1706 const int argc = arguments().immediate();
1707
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001708 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001709 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001710
1711 // If the object is the holder then we know that it's a global
1712 // object which can only happen for contextual calls. In this case,
1713 // the receiver cannot be a smi.
1714 if (object != holder) {
1715 __ test(edx, Immediate(kSmiTagMask));
1716 __ j(zero, &miss, not_taken);
1717 }
1718
1719 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001720 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001721
1722 // Get the value from the cell.
1723 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1724 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1725
1726 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001727 if (Heap::InNewSpace(function)) {
1728 // We can't embed a pointer to a function in new space so we have
1729 // to verify that the shared function info is unchanged. This has
1730 // the nice side effect that multiple closures based on the same
1731 // function can all use this call IC. Before we load through the
1732 // function, we have to verify that it still is a function.
1733 __ test(edi, Immediate(kSmiTagMask));
1734 __ j(zero, &miss, not_taken);
1735 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1736 __ j(not_equal, &miss, not_taken);
1737
1738 // Check the shared function info. Make sure it hasn't changed.
1739 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1740 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1741 __ j(not_equal, &miss, not_taken);
1742 } else {
1743 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1744 __ j(not_equal, &miss, not_taken);
1745 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001746
1747 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001748 if (object->IsGlobalObject()) {
1749 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1750 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1751 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001752
1753 // Setup the context (function already in edi).
1754 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1755
1756 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001757 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001758 ASSERT(function->is_compiled());
1759 Handle<Code> code(function->code());
1760 ParameterCount expected(function->shared()->formal_parameter_count());
1761 __ InvokeCode(code, expected, arguments(),
1762 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1763
1764 // Handle call cache miss.
1765 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001766 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001767 GenerateMissBranch();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001768
1769 // Return the generated code.
1770 return GetCode(NORMAL, name);
1771}
1772
1773
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1775 int index,
1776 Map* transition,
1777 String* name) {
1778 // ----------- S t a t e -------------
1779 // -- eax : value
1780 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001781 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001782 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 Label miss;
1785
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001787 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001788 object,
1789 index,
1790 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001792 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793
1794 // Handle store cache miss.
1795 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001796 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001798 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799
1800 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001801 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802}
1803
1804
1805Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1806 AccessorInfo* callback,
1807 String* name) {
1808 // ----------- S t a t e -------------
1809 // -- eax : value
1810 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001811 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001812 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001813 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 Label miss;
1815
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001817 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 __ j(zero, &miss, not_taken);
1819
1820 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001821 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 Immediate(Handle<Map>(object->map())));
1823 __ j(not_equal, &miss, not_taken);
1824
1825 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001826 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001827 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 }
1829
1830 // Stub never generated for non-global objects that require access
1831 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001832 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833
1834 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001835 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1837 __ push(ecx); // name
1838 __ push(eax); // value
1839 __ push(ebx); // restore return address
1840
mads.s.ager31e71382008-08-13 09:32:07 +00001841 // Do tail-call to the runtime system.
1842 ExternalReference store_callback_property =
1843 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001844 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845
1846 // Handle store cache miss.
1847 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001848 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001849 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001852 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853}
1854
1855
1856Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1857 String* name) {
1858 // ----------- S t a t e -------------
1859 // -- eax : value
1860 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001861 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001862 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001863 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 Label miss;
1865
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001867 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 __ j(zero, &miss, not_taken);
1869
1870 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001871 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 Immediate(Handle<Map>(receiver->map())));
1873 __ j(not_equal, &miss, not_taken);
1874
1875 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001876 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001877 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 }
1879
1880 // Stub never generated for non-global objects that require access
1881 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001882 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883
1884 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001885 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 __ push(ecx); // name
1887 __ push(eax); // value
1888 __ push(ebx); // restore return address
1889
mads.s.ager31e71382008-08-13 09:32:07 +00001890 // Do tail-call to the runtime system.
1891 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001893 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894
1895 // Handle store cache miss.
1896 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001898 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
1900 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001901 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902}
1903
1904
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001905Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1906 JSGlobalPropertyCell* cell,
1907 String* name) {
1908 // ----------- S t a t e -------------
1909 // -- eax : value
1910 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001911 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001912 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001913 // -----------------------------------
1914 Label miss;
1915
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001916 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001917 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001918 Immediate(Handle<Map>(object->map())));
1919 __ j(not_equal, &miss, not_taken);
1920
1921 // Store the value in the cell.
1922 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1923 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1924
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001925 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001926 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001927 __ ret(0);
1928
1929 // Handle store cache miss.
1930 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001931 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1932 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1933 __ jmp(ic, RelocInfo::CODE_TARGET);
1934
1935 // Return the generated code.
1936 return GetCode(NORMAL, name);
1937}
1938
1939
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1941 int index,
1942 Map* transition,
1943 String* name) {
1944 // ----------- S t a t e -------------
1945 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001946 // -- ecx : key
1947 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 Label miss;
1951
1952 __ IncrementCounter(&Counters::keyed_store_field, 1);
1953
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 // Check that the name has not changed.
1955 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1956 __ j(not_equal, &miss, not_taken);
1957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001959 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001960 object,
1961 index,
1962 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001963 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001964 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965
1966 // Handle store cache miss.
1967 __ bind(&miss);
1968 __ DecrementCounter(&Counters::keyed_store_field, 1);
1969 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001970 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971
1972 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001973 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974}
1975
1976
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001977Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1978 JSObject* object,
1979 JSObject* last) {
1980 // ----------- S t a t e -------------
1981 // -- eax : receiver
1982 // -- ecx : name
1983 // -- esp[0] : return address
1984 // -----------------------------------
1985 Label miss;
1986
1987 // Check that the receiver isn't a smi.
1988 __ test(eax, Immediate(kSmiTagMask));
1989 __ j(zero, &miss, not_taken);
1990
1991 // Check the maps of the full prototype chain. Also check that
1992 // global property cells up to (but not including) the last object
1993 // in the prototype chain are empty.
1994 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1995
1996 // If the last object in the prototype chain is a global object,
1997 // check that the global property cell is empty.
1998 if (last->IsGlobalObject()) {
1999 Object* cell = GenerateCheckPropertyCell(masm(),
2000 GlobalObject::cast(last),
2001 name,
2002 edx,
2003 &miss);
2004 if (cell->IsFailure()) return cell;
2005 }
2006
2007 // Return undefined if maps of the full prototype chain are still the
2008 // same and no global property with this name contains a value.
2009 __ mov(eax, Factory::undefined_value());
2010 __ ret(0);
2011
2012 __ bind(&miss);
2013 GenerateLoadMiss(masm(), Code::LOAD_IC);
2014
2015 // Return the generated code.
2016 return GetCode(NONEXISTENT, Heap::empty_string());
2017}
2018
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002019
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2021 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 int index,
2023 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002025 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026 // -- ecx : name
2027 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 Label miss;
2030
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002031 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
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(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037}
2038
2039
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002040Object* LoadStubCompiler::CompileLoadCallback(String* name,
2041 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002043 AccessorInfo* callback) {
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
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002051 Failure* failure = Failure::InternalError();
2052 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2053 callback, name, &miss, &failure);
2054 if (!success) return failure;
2055
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 __ bind(&miss);
2057 GenerateLoadMiss(masm(), Code::LOAD_IC);
2058
2059 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002060 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061}
2062
2063
2064Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2065 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002066 Object* value,
2067 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002069 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002070 // -- ecx : name
2071 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 Label miss;
2074
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002075 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 __ bind(&miss);
2077 GenerateLoadMiss(masm(), Code::LOAD_IC);
2078
2079 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002080 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081}
2082
2083
2084Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2085 JSObject* holder,
2086 String* name) {
2087 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002088 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 // -- ecx : name
2090 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 Label miss;
2093
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002094 LookupResult lookup;
2095 LookupPostInterceptor(holder, name, &lookup);
2096
ager@chromium.orge2902be2009-06-08 12:21:35 +00002097 // TODO(368): Compile in the whole chain: all the interceptors in
2098 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002099 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002100 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002101 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002102 eax,
2103 ecx,
2104 edx,
2105 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002106 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002107 &miss);
2108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 __ bind(&miss);
2110 GenerateLoadMiss(masm(), Code::LOAD_IC);
2111
2112 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002113 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114}
2115
2116
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002117Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2118 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002119 JSGlobalPropertyCell* cell,
2120 String* name,
2121 bool is_dont_delete) {
2122 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002123 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002124 // -- ecx : name
2125 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002126 // -----------------------------------
2127 Label miss;
2128
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002129 // If the object is the holder then we know that it's a global
2130 // object which can only happen for contextual loads. In this case,
2131 // the receiver cannot be a smi.
2132 if (object != holder) {
2133 __ test(eax, Immediate(kSmiTagMask));
2134 __ j(zero, &miss, not_taken);
2135 }
2136
2137 // Check that the maps haven't changed.
2138 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002139
2140 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002141 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2142 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002143
2144 // Check for deleted property if property can actually be deleted.
2145 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002147 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002148 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002149 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002150 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002151 }
2152
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002153 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002154 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002155 __ ret(0);
2156
2157 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002158 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2159 GenerateLoadMiss(masm(), Code::LOAD_IC);
2160
2161 // Return the generated code.
2162 return GetCode(NORMAL, name);
2163}
2164
2165
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2167 JSObject* receiver,
2168 JSObject* holder,
2169 int index) {
2170 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002171 // -- eax : key
2172 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002174 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 Label miss;
2176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 __ IncrementCounter(&Counters::keyed_load_field, 1);
2178
2179 // Check that the name has not changed.
2180 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2181 __ j(not_equal, &miss, not_taken);
2182
ager@chromium.org5c838252010-02-19 08:53:10 +00002183 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002184
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 __ bind(&miss);
2186 __ DecrementCounter(&Counters::keyed_load_field, 1);
2187 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2188
2189 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002190 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191}
2192
2193
2194Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2195 JSObject* receiver,
2196 JSObject* holder,
2197 AccessorInfo* callback) {
2198 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002199 // -- eax : key
2200 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002202 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 Label miss;
2204
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2206
2207 // Check that the name has not changed.
2208 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2209 __ j(not_equal, &miss, not_taken);
2210
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002211 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002212 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002213 callback, name, &miss, &failure);
2214 if (!success) return failure;
2215
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002216 __ bind(&miss);
2217 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2218 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2219
2220 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002221 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222}
2223
2224
2225Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2226 JSObject* receiver,
2227 JSObject* holder,
2228 Object* value) {
2229 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002230 // -- eax : key
2231 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 Label miss;
2235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2237
2238 // Check that the name has not changed.
2239 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2240 __ j(not_equal, &miss, not_taken);
2241
ager@chromium.org5c838252010-02-19 08:53:10 +00002242 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002243 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 __ bind(&miss);
2245 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2246 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2247
2248 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002249 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250}
2251
2252
2253Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2254 JSObject* holder,
2255 String* name) {
2256 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002257 // -- eax : key
2258 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002259 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002260 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 Label miss;
2262
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2264
2265 // Check that the name has not changed.
2266 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2267 __ j(not_equal, &miss, not_taken);
2268
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002269 LookupResult lookup;
2270 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002271 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002272 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002273 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002274 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002275 eax,
2276 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002277 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002278 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002279 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002280 __ bind(&miss);
2281 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2282 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2283
2284 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002285 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286}
2287
2288
2289
2290
2291Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2292 // ----------- 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_array_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 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002306 __ bind(&miss);
2307 __ DecrementCounter(&Counters::keyed_load_array_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
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002315Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316 // ----------- 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_string_length, 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 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002330 __ bind(&miss);
2331 __ DecrementCounter(&Counters::keyed_load_string_length, 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
2339Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2340 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002341 // -- eax : key
2342 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002343 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002344 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002345 Label miss;
2346
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2348
2349 // Check that the name has not changed.
2350 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2351 __ j(not_equal, &miss, not_taken);
2352
ager@chromium.org5c838252010-02-19 08:53:10 +00002353 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002354 __ bind(&miss);
2355 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2356 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2357
2358 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002359 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360}
2361
2362
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002363// Specialized stub for constructing objects from functions which only have only
2364// simple assignments of the form this.x = ...; in their body.
2365Object* ConstructStubCompiler::CompileConstructStub(
2366 SharedFunctionInfo* shared) {
2367 // ----------- S t a t e -------------
2368 // -- eax : argc
2369 // -- edi : constructor
2370 // -- esp[0] : return address
2371 // -- esp[4] : last argument
2372 // -----------------------------------
2373 Label generic_stub_call;
2374#ifdef ENABLE_DEBUGGER_SUPPORT
2375 // Check to see whether there are any break points in the function code. If
2376 // there are jump to the generic constructor stub which calls the actual
2377 // code for the function thereby hitting the break points.
2378 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2379 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2380 __ cmp(ebx, Factory::undefined_value());
2381 __ j(not_equal, &generic_stub_call, not_taken);
2382#endif
2383
2384 // Load the initial map and verify that it is in fact a map.
2385 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2386 // Will both indicate a NULL and a Smi.
2387 __ test(ebx, Immediate(kSmiTagMask));
2388 __ j(zero, &generic_stub_call);
2389 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2390 __ j(not_equal, &generic_stub_call);
2391
2392#ifdef DEBUG
2393 // Cannot construct functions this way.
2394 // edi: constructor
2395 // ebx: initial map
2396 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2397 __ Assert(not_equal, "Function constructed by construct stub.");
2398#endif
2399
2400 // Now allocate the JSObject on the heap by moving the new space allocation
2401 // top forward.
2402 // edi: constructor
2403 // ebx: initial map
2404 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2405 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002406 __ AllocateInNewSpace(ecx,
2407 edx,
2408 ecx,
2409 no_reg,
2410 &generic_stub_call,
2411 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002412
2413 // Allocated the JSObject, now initialize the fields and add the heap tag.
2414 // ebx: initial map
2415 // edx: JSObject (untagged)
2416 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2417 __ mov(ebx, Factory::empty_fixed_array());
2418 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2419 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2420
2421 // Push the allocated object to the stack. This is the object that will be
2422 // returned (after it is tagged).
2423 __ push(edx);
2424
2425 // eax: argc
2426 // edx: JSObject (untagged)
2427 // Load the address of the first in-object property into edx.
2428 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2429 // Calculate the location of the first argument. The stack contains the
2430 // allocated object and the return address on top of the argc arguments.
2431 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2432
2433 // Use edi for holding undefined which is used in several places below.
2434 __ mov(edi, Factory::undefined_value());
2435
2436 // eax: argc
2437 // ecx: first argument
2438 // edx: first in-object property of the JSObject
2439 // edi: undefined
2440 // Fill the initialized properties with a constant value or a passed argument
2441 // depending on the this.x = ...; assignment in the function.
2442 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2443 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002444 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002445 // If argument is not passed the property is set to undefined,
2446 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002447 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002448 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002449 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002450 if (CpuFeatures::IsSupported(CMOV)) {
2451 CpuFeatures::Scope use_cmov(CMOV);
2452 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2453 } else {
2454 Label not_passed;
2455 __ j(below_equal, &not_passed);
2456 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2457 __ bind(&not_passed);
2458 }
2459 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002460 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002461 } else {
2462 // Set the property to the constant value.
2463 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2464 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2465 }
2466 }
2467
2468 // Fill the unused in-object property fields with undefined.
2469 for (int i = shared->this_property_assignments_count();
2470 i < shared->CalculateInObjectProperties();
2471 i++) {
2472 __ mov(Operand(edx, i * kPointerSize), edi);
2473 }
2474
2475 // Move argc to ebx and retrieve and tag the JSObject to return.
2476 __ mov(ebx, eax);
2477 __ pop(eax);
2478 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2479
2480 // Remove caller arguments and receiver from the stack and return.
2481 __ pop(ecx);
2482 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2483 __ push(ecx);
2484 __ IncrementCounter(&Counters::constructed_objects, 1);
2485 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2486 __ ret(0);
2487
2488 // Jump to the generic stub in case the specialized code cannot handle the
2489 // construction.
2490 __ bind(&generic_stub_call);
2491 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2492 Handle<Code> generic_construct_stub(code);
2493 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2494
2495 // Return the generated code.
2496 return GetCode();
2497}
2498
2499
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002500#undef __
2501
2502} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002503
2504#endif // V8_TARGET_ARCH_IA32