blob: bab0435f38968a24e0f5d090a32ac428788ac316 [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
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000819 // Push data from AccessorInfo.
820 if (Heap::InNewSpace(callback_handle->data())) {
821 __ mov(other, Immediate(callback_handle));
822 __ push(FieldOperand(other, AccessorInfo::kDataOffset));
823 } else {
824 __ push(Immediate(Handle<Object>(callback_handle->data())));
825 }
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000826 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000827 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000828 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000829 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000830 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000831 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000832
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000833 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000834 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000835 Address getter_address = v8::ToCData<Address>(callback->getter());
836 ApiFunction fun(getter_address);
837 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000838 // Emitting a stub call may try to allocate (if the code is not
839 // already generated). Do not allow the assembler to perform a
840 // garbage collection but instead return the allocation failure
841 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000842 Object* result = masm()->TryCallStub(&stub);
843 if (result->IsFailure()) {
844 *failure = Failure::cast(result);
845 return false;
846 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000847
848 // We need to avoid using eax since that now holds the result.
849 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000850 // Emitting PopHandleScope may try to allocate. Do not allow the
851 // assembler to perform a garbage collection but instead return a
852 // failure object.
853 result = masm()->TryPopHandleScope(eax, tmp);
854 if (result->IsFailure()) {
855 *failure = Failure::cast(result);
856 return false;
857 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000858 __ LeaveInternalFrame();
859
860 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000861 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000862}
863
864
865void StubCompiler::GenerateLoadConstant(JSObject* object,
866 JSObject* holder,
867 Register receiver,
868 Register scratch1,
869 Register scratch2,
870 Object* value,
871 String* name,
872 Label* miss) {
873 // Check that the receiver isn't a smi.
874 __ test(receiver, Immediate(kSmiTagMask));
875 __ j(zero, miss, not_taken);
876
877 // Check that the maps haven't changed.
878 Register reg =
879 CheckPrototypes(object, receiver, holder,
880 scratch1, scratch2, name, miss);
881
882 // Return the constant value.
883 __ mov(eax, Handle<Object>(value));
884 __ ret(0);
885}
886
887
888void StubCompiler::GenerateLoadInterceptor(JSObject* object,
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000889 JSObject* interceptor_holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000890 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000891 Register receiver,
892 Register name_reg,
893 Register scratch1,
894 Register scratch2,
895 String* name,
896 Label* miss) {
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000897 ASSERT(interceptor_holder->HasNamedInterceptor());
898 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
899
900 // Check that the receiver isn't a smi.
901 __ test(receiver, Immediate(kSmiTagMask));
902 __ j(zero, miss, not_taken);
903
904 // So far the most popular follow ups for interceptor loads are FIELD
905 // and CALLBACKS, so inline only them, other cases may be added
906 // later.
907 bool compile_followup_inline = false;
908 if (lookup->IsProperty() && lookup->IsCacheable()) {
909 if (lookup->type() == FIELD) {
910 compile_followup_inline = true;
911 } else if (lookup->type() == CALLBACKS &&
912 lookup->GetCallbackObject()->IsAccessorInfo() &&
913 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
914 compile_followup_inline = true;
915 }
916 }
917
918 if (compile_followup_inline) {
919 // Compile the interceptor call, followed by inline code to load the
920 // property from further up the prototype chain if the call fails.
921 // Check that the maps haven't changed.
922 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
923 scratch1, scratch2, name, miss);
924 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
925
926 // Save necessary data before invoking an interceptor.
927 // Requires a frame to make GC aware of pushed pointers.
928 __ EnterInternalFrame();
929
930 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
931 // CALLBACKS case needs a receiver to be passed into C++ callback.
932 __ push(receiver);
933 }
934 __ push(holder_reg);
935 __ push(name_reg);
936
937 // Invoke an interceptor. Note: map checks from receiver to
938 // interceptor's holder has been compiled before (see a caller
939 // of this method.)
940 CompileCallLoadPropertyWithInterceptor(masm(),
941 receiver,
942 holder_reg,
943 name_reg,
944 interceptor_holder);
945
946 // Check if interceptor provided a value for property. If it's
947 // the case, return immediately.
948 Label interceptor_failed;
949 __ cmp(eax, Factory::no_interceptor_result_sentinel());
950 __ j(equal, &interceptor_failed);
951 __ LeaveInternalFrame();
952 __ ret(0);
953
954 __ bind(&interceptor_failed);
955 __ pop(name_reg);
956 __ pop(holder_reg);
957 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
958 __ pop(receiver);
959 }
960
961 __ LeaveInternalFrame();
962
963 // Check that the maps from interceptor's holder to lookup's holder
964 // haven't changed. And load lookup's holder into holder_reg.
965 if (interceptor_holder != lookup->holder()) {
966 holder_reg = CheckPrototypes(interceptor_holder,
967 holder_reg,
968 lookup->holder(),
969 scratch1,
970 scratch2,
971 name,
972 miss);
973 }
974
975 if (lookup->type() == FIELD) {
976 // We found FIELD property in prototype chain of interceptor's holder.
977 // Retrieve a field from field's holder.
978 GenerateFastPropertyLoad(masm(), eax, holder_reg,
979 lookup->holder(), lookup->GetFieldIndex());
980 __ ret(0);
981 } else {
982 // We found CALLBACKS property in prototype chain of interceptor's
983 // holder.
984 ASSERT(lookup->type() == CALLBACKS);
985 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
986 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
987 ASSERT(callback != NULL);
988 ASSERT(callback->getter() != NULL);
989
990 // Tail call to runtime.
991 // Important invariant in CALLBACKS case: the code above must be
992 // structured to never clobber |receiver| register.
993 __ pop(scratch2); // return address
994 __ push(receiver);
995 __ push(holder_reg);
996 __ mov(holder_reg, Immediate(Handle<AccessorInfo>(callback)));
997 __ push(holder_reg);
998 __ push(FieldOperand(holder_reg, AccessorInfo::kDataOffset));
999 __ push(name_reg);
1000 __ push(scratch2); // restore return address
1001
1002 ExternalReference ref =
1003 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
1004 __ TailCallExternalReference(ref, 5, 1);
1005 }
1006 } else { // !compile_followup_inline
1007 // Call the runtime system to load the interceptor.
1008 // Check that the maps haven't changed.
1009 Register holder_reg =
1010 CheckPrototypes(object, receiver, interceptor_holder,
1011 scratch1, scratch2, name, miss);
1012 __ pop(scratch2); // save old return address
1013 PushInterceptorArguments(masm(), receiver, holder_reg,
1014 name_reg, interceptor_holder);
1015 __ push(scratch2); // restore old return address
1016
1017 ExternalReference ref = ExternalReference(
1018 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
1019 __ TailCallExternalReference(ref, 5, 1);
1020 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001021}
1022
1023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024// TODO(1241006): Avoid having lazy compile stubs specialized by the
1025// number of arguments. It is not needed anymore.
1026Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001028 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001029
1030 // Push a copy of the function onto the stack.
1031 __ push(edi);
1032
1033 __ push(edi); // function is also the parameter to the runtime call
1034 __ CallRuntime(Runtime::kLazyCompile, 1);
1035 __ pop(edi);
1036
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001037 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001038 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039
1040 // Do a tail-call of the compiled function.
1041 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1042 __ jmp(Operand(ecx));
1043
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001044 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001045}
1046
1047
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001048void CallStubCompiler::GenerateNameCheck(String* name, Label* miss) {
1049 if (kind_ == Code::KEYED_CALL_IC) {
1050 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1051 __ j(not_equal, miss, not_taken);
1052 }
1053}
1054
1055
1056void CallStubCompiler::GenerateMissBranch() {
1057 Handle<Code> ic = ComputeCallMiss(arguments().immediate(), kind_);
1058 __ jmp(ic, RelocInfo::CODE_TARGET);
1059}
1060
1061
ager@chromium.org5c838252010-02-19 08:53:10 +00001062Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001063 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001064 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001065 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001067 // -- ecx : name
1068 // -- esp[0] : return address
1069 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1070 // -- ...
1071 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073 Label miss;
1074
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001075 GenerateNameCheck(name, &miss);
1076
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001077 // Get the receiver from the stack.
1078 const int argc = arguments().immediate();
1079 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1080
1081 // Check that the receiver isn't a smi.
1082 __ test(edx, Immediate(kSmiTagMask));
1083 __ j(zero, &miss, not_taken);
1084
1085 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001086 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087
ager@chromium.org7c537e22008-10-16 08:43:32 +00001088 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089
1090 // Check that the function really is a function.
1091 __ test(edi, Immediate(kSmiTagMask));
1092 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001093 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001094 __ j(not_equal, &miss, not_taken);
1095
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001096 // Patch the receiver on the stack with the global proxy if
1097 // necessary.
1098 if (object->IsGlobalObject()) {
1099 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1100 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1101 }
1102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001103 // Invoke the function.
1104 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1105
1106 // Handle call cache miss.
1107 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001108 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001109
1110 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001111 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001112}
1113
1114
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001115Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1116 JSObject* holder,
1117 JSFunction* function,
1118 String* name,
1119 CheckType check) {
1120 // ----------- S t a t e -------------
1121 // -- ecx : name
1122 // -- esp[0] : return address
1123 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1124 // -- ...
1125 // -- esp[(argc + 1) * 4] : receiver
1126 // -----------------------------------
1127 ASSERT(check == RECEIVER_MAP_CHECK);
1128
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001129 // If object is not an array, bail out to regular call.
1130 if (!object->IsJSArray()) {
1131 return Heap::undefined_value();
1132 }
1133
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001134 Label miss;
1135
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001136 GenerateNameCheck(name, &miss);
1137
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001138 // Get the receiver from the stack.
1139 const int argc = arguments().immediate();
1140 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1141
1142 // Check that the receiver isn't a smi.
1143 __ test(edx, Immediate(kSmiTagMask));
1144 __ j(zero, &miss);
1145
1146 CheckPrototypes(JSObject::cast(object), edx,
1147 holder, ebx,
1148 eax, name, &miss);
1149
1150 if (argc == 0) {
1151 // Noop, return the length.
1152 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1153 __ ret((argc + 1) * kPointerSize);
1154 } else {
1155 // Get the elements array of the object.
1156 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1157
1158 // Check that the elements are in fast mode (not dictionary).
1159 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1160 Immediate(Factory::fixed_array_map()));
1161 __ j(not_equal, &miss);
1162
1163 if (argc == 1) { // Otherwise fall through to call builtin.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001164 Label call_builtin, exit, with_write_barrier, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001165
1166 // Get the array's length into eax and calculate new length.
1167 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1168 STATIC_ASSERT(kSmiTagSize == 1);
1169 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001170 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001171
1172 // Get the element's length into ecx.
1173 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001174
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001175 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001176 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001177 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001178
1179 // Save new length.
1180 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1181
1182 // Push the element.
1183 __ lea(edx, FieldOperand(ebx,
1184 eax, times_half_pointer_size,
1185 FixedArray::kHeaderSize - argc * kPointerSize));
1186 __ mov(ecx, Operand(esp, argc * kPointerSize));
1187 __ mov(Operand(edx, 0), ecx);
1188
1189 // Check if value is a smi.
1190 __ test(ecx, Immediate(kSmiTagMask));
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001191 __ j(not_zero, &with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001192
1193 __ bind(&exit);
1194 __ ret((argc + 1) * kPointerSize);
1195
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001196 __ bind(&with_write_barrier);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001197
1198 __ InNewSpace(ebx, ecx, equal, &exit);
1199
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001200 __ RecordWriteHelper(ebx, edx, ecx);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001201 __ ret((argc + 1) * kPointerSize);
1202
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001203 __ bind(&attempt_to_grow_elements);
1204 ExternalReference new_space_allocation_top =
1205 ExternalReference::new_space_allocation_top_address();
1206 ExternalReference new_space_allocation_limit =
1207 ExternalReference::new_space_allocation_limit_address();
1208
1209 const int kAllocationDelta = 4;
1210 // Load top.
1211 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1212
1213 // Check if it's the end of elements.
1214 __ lea(edx, FieldOperand(ebx,
1215 eax, times_half_pointer_size,
1216 FixedArray::kHeaderSize - argc * kPointerSize));
1217 __ cmp(edx, Operand(ecx));
1218 __ j(not_equal, &call_builtin);
1219 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1220 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001221 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001222
1223 // We fit and could grow elements.
1224 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1225 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001226
1227 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001228 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001229 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001230 for (int i = 1; i < kAllocationDelta; i++) {
1231 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001232 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001233 }
1234
1235 // Restore receiver to edx as finish sequence assumes it's here.
1236 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1237
1238 // Increment element's and array's sizes.
1239 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001240 Immediate(Smi::FromInt(kAllocationDelta)));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001241 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1242
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001243 // Elements are in new space, so write barrier is not required.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001244 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001245
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001246 __ bind(&call_builtin);
1247 }
1248
1249 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1250 argc + 1,
1251 1);
1252 }
1253
1254 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001255 GenerateMissBranch();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001256
1257 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001258 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001259}
1260
1261
1262Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1263 JSObject* holder,
1264 JSFunction* function,
1265 String* name,
1266 CheckType check) {
1267 // ----------- S t a t e -------------
1268 // -- ecx : name
1269 // -- esp[0] : return address
1270 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1271 // -- ...
1272 // -- esp[(argc + 1) * 4] : receiver
1273 // -----------------------------------
1274 ASSERT(check == RECEIVER_MAP_CHECK);
1275
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001276 // If object is not an array, bail out to regular call.
1277 if (!object->IsJSArray()) {
1278 return Heap::undefined_value();
1279 }
1280
ager@chromium.orgac091b72010-05-05 07:34:42 +00001281 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001282
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001283 GenerateNameCheck(name, &miss);
1284
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001285 // Get the receiver from the stack.
1286 const int argc = arguments().immediate();
1287 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1288
1289 // Check that the receiver isn't a smi.
1290 __ test(edx, Immediate(kSmiTagMask));
1291 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001292 CheckPrototypes(JSObject::cast(object), edx,
1293 holder, ebx,
1294 eax, name, &miss);
1295
1296 // Get the elements array of the object.
1297 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1298
1299 // Check that the elements are in fast mode (not dictionary).
1300 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1301 Immediate(Factory::fixed_array_map()));
1302 __ j(not_equal, &miss);
1303
1304 // Get the array's length into ecx and calculate new length.
1305 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1306 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001307 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001308
1309 // Get the last element.
1310 STATIC_ASSERT(kSmiTagSize == 1);
1311 STATIC_ASSERT(kSmiTag == 0);
1312 __ mov(eax, FieldOperand(ebx,
1313 ecx, times_half_pointer_size,
1314 FixedArray::kHeaderSize));
1315 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1316 __ j(equal, &call_builtin);
1317
1318 // Set the array's length.
1319 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1320
1321 // Fill with the hole.
1322 __ mov(FieldOperand(ebx,
1323 ecx, times_half_pointer_size,
1324 FixedArray::kHeaderSize),
1325 Immediate(Factory::the_hole_value()));
1326 __ ret((argc + 1) * kPointerSize);
1327
ager@chromium.orgac091b72010-05-05 07:34:42 +00001328 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001329 __ mov(eax, Immediate(Factory::undefined_value()));
1330 __ ret((argc + 1) * kPointerSize);
1331
1332 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001333 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1334 argc + 1,
1335 1);
1336
1337 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001338 GenerateMissBranch();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001339
1340 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001341 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001342}
1343
1344
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001345Object* CallStubCompiler::CompileStringCharCodeAtCall(Object* object,
1346 JSObject* holder,
1347 JSFunction* function,
1348 String* name,
1349 CheckType check) {
1350 // ----------- S t a t e -------------
1351 // -- ecx : function name
1352 // -- esp[0] : return address
1353 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1354 // -- ...
1355 // -- esp[(argc + 1) * 4] : receiver
1356 // -----------------------------------
1357
1358 const int argc = arguments().immediate();
1359
1360 Label miss;
1361 Label index_out_of_range;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001362 GenerateNameCheck(name, &miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001363
1364 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001365 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1366 Context::STRING_FUNCTION_INDEX,
1367 eax);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001368 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1369 ebx, edx, name, &miss);
1370
1371 Register receiver = ebx;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001372 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001373 Register scratch = edx;
1374 Register result = eax;
1375 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1376 if (argc > 0) {
1377 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1378 } else {
1379 __ Set(index, Immediate(Factory::undefined_value()));
1380 }
1381
1382 StringCharCodeAtGenerator char_code_at_generator(receiver,
1383 index,
1384 scratch,
1385 result,
1386 &miss, // When not a string.
1387 &miss, // When not a number.
1388 &index_out_of_range,
1389 STRING_INDEX_IS_NUMBER);
1390 char_code_at_generator.GenerateFast(masm());
1391 __ ret((argc + 1) * kPointerSize);
1392
1393 ICRuntimeCallHelper call_helper;
1394 char_code_at_generator.GenerateSlow(masm(), call_helper);
1395
1396 __ bind(&index_out_of_range);
1397 __ Set(eax, Immediate(Factory::nan_value()));
1398 __ ret((argc + 1) * kPointerSize);
1399
1400 __ bind(&miss);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001401
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001402 GenerateMissBranch();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001403
1404 // Return the generated code.
1405 return GetCode(function);
1406}
1407
1408
1409Object* CallStubCompiler::CompileStringCharAtCall(Object* object,
1410 JSObject* holder,
1411 JSFunction* function,
1412 String* name,
1413 CheckType check) {
1414 // ----------- S t a t e -------------
1415 // -- ecx : function name
1416 // -- esp[0] : return address
1417 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1418 // -- ...
1419 // -- esp[(argc + 1) * 4] : receiver
1420 // -----------------------------------
1421
1422 const int argc = arguments().immediate();
1423
1424 Label miss;
1425 Label index_out_of_range;
1426
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001427 GenerateNameCheck(name, &miss);
1428
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001429 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001430 GenerateDirectLoadGlobalFunctionPrototype(masm(),
1431 Context::STRING_FUNCTION_INDEX,
1432 eax);
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001433 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1434 ebx, edx, name, &miss);
1435
1436 Register receiver = eax;
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001437 Register index = edi;
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001438 Register scratch1 = ebx;
1439 Register scratch2 = edx;
1440 Register result = eax;
1441 __ mov(receiver, Operand(esp, (argc + 1) * kPointerSize));
1442 if (argc > 0) {
1443 __ mov(index, Operand(esp, (argc - 0) * kPointerSize));
1444 } else {
1445 __ Set(index, Immediate(Factory::undefined_value()));
1446 }
1447
1448 StringCharAtGenerator char_at_generator(receiver,
1449 index,
1450 scratch1,
1451 scratch2,
1452 result,
1453 &miss, // When not a string.
1454 &miss, // When not a number.
1455 &index_out_of_range,
1456 STRING_INDEX_IS_NUMBER);
1457 char_at_generator.GenerateFast(masm());
1458 __ ret((argc + 1) * kPointerSize);
1459
1460 ICRuntimeCallHelper call_helper;
1461 char_at_generator.GenerateSlow(masm(), call_helper);
1462
1463 __ bind(&index_out_of_range);
1464 __ Set(eax, Immediate(Factory::empty_string()));
1465 __ ret((argc + 1) * kPointerSize);
1466
1467 __ bind(&miss);
1468 // Restore function name in ecx.
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001469
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001470 GenerateMissBranch();
ricow@chromium.org30ce4112010-05-31 10:38:25 +00001471
1472 // Return the generated code.
1473 return GetCode(function);
1474}
1475
1476
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477Object* CallStubCompiler::CompileCallConstant(Object* object,
1478 JSObject* holder,
1479 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001480 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001481 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001482 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001483 // -- ecx : name
1484 // -- esp[0] : return address
1485 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1486 // -- ...
1487 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001489
1490 SharedFunctionInfo* function_info = function->shared();
1491 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001492 const int id = function_info->custom_call_generator_id();
1493 Object* result =
1494 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001495 // undefined means bail out to regular compiler.
1496 if (!result->IsUndefined()) {
1497 return result;
1498 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001499 }
1500
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001501 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001503 GenerateNameCheck(name, &miss_in_smi_check);
1504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001505 // Get the receiver from the stack.
1506 const int argc = arguments().immediate();
1507 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1508
1509 // Check that the receiver isn't a smi.
1510 if (check != NUMBER_CHECK) {
1511 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001512 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513 }
1514
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001515 // Make sure that it's okay not to patch the on stack receiver
1516 // unless we're doing a receiver map check.
1517 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1518
ager@chromium.org5c838252010-02-19 08:53:10 +00001519 CallOptimization optimization(function);
1520 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001521 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001522
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001523 switch (check) {
1524 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001525 __ IncrementCounter(&Counters::call_const, 1);
1526
1527 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1528 depth = optimization.GetPrototypeDepthOfExpectedType(
1529 JSObject::cast(object), holder);
1530 }
1531
1532 if (depth != kInvalidProtoDepth) {
1533 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1534 ReserveSpaceForFastApiCall(masm(), eax);
1535 }
1536
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001537 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001538 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001539 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001540
1541 // Patch the receiver on the stack with the global proxy if
1542 // necessary.
1543 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001544 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001545 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1546 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1547 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 break;
1549
1550 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001551 if (!function->IsBuiltin()) {
1552 // Calling non-builtins with a value as receiver requires boxing.
1553 __ jmp(&miss);
1554 } else {
1555 // Check that the object is a string or a symbol.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001556 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001557 __ j(above_equal, &miss, not_taken);
1558 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001559 GenerateDirectLoadGlobalFunctionPrototype(
1560 masm(), Context::STRING_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001561 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1562 ebx, edx, name, &miss);
1563 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 break;
1565
1566 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001567 if (!function->IsBuiltin()) {
1568 // Calling non-builtins with a value as receiver requires boxing.
1569 __ jmp(&miss);
1570 } else {
1571 Label fast;
1572 // Check that the object is a smi or a heap number.
1573 __ test(edx, Immediate(kSmiTagMask));
1574 __ j(zero, &fast, taken);
1575 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1576 __ j(not_equal, &miss, not_taken);
1577 __ bind(&fast);
1578 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001579 GenerateDirectLoadGlobalFunctionPrototype(
1580 masm(), Context::NUMBER_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001581 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1582 ebx, edx, name, &miss);
1583 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 break;
1585 }
1586
1587 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001588 if (!function->IsBuiltin()) {
1589 // Calling non-builtins with a value as receiver requires boxing.
1590 __ jmp(&miss);
1591 } else {
1592 Label fast;
1593 // Check that the object is a boolean.
1594 __ cmp(edx, Factory::true_value());
1595 __ j(equal, &fast, taken);
1596 __ cmp(edx, Factory::false_value());
1597 __ j(not_equal, &miss, not_taken);
1598 __ bind(&fast);
1599 // Check that the maps starting from the prototype haven't changed.
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001600 GenerateDirectLoadGlobalFunctionPrototype(
1601 masm(), Context::BOOLEAN_FUNCTION_INDEX, eax);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001602 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1603 ebx, edx, name, &miss);
1604 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001605 break;
1606 }
1607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 default:
1609 UNREACHABLE();
1610 }
1611
ager@chromium.org5c838252010-02-19 08:53:10 +00001612 if (depth != kInvalidProtoDepth) {
1613 GenerateFastApiCall(masm(), optimization, argc);
1614 } else {
1615 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1616 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617
1618 // Handle call cache miss.
1619 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001620 if (depth != kInvalidProtoDepth) {
1621 FreeSpaceForFastApiCall(masm(), eax);
1622 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001623 __ bind(&miss_in_smi_check);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001624 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001625
1626 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001627 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628}
1629
1630
ager@chromium.org5c838252010-02-19 08:53:10 +00001631Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632 JSObject* holder,
1633 String* name) {
1634 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001635 // -- ecx : name
1636 // -- esp[0] : return address
1637 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1638 // -- ...
1639 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 Label miss;
1642
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001643 GenerateNameCheck(name, &miss);
1644
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 // Get the number of arguments.
1646 const int argc = arguments().immediate();
1647
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001648 LookupResult lookup;
1649 LookupPostInterceptor(holder, name, &lookup);
1650
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 // Get the receiver from the stack.
1652 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001653
ager@chromium.org5c838252010-02-19 08:53:10 +00001654 CallInterceptorCompiler compiler(this, arguments(), ecx);
1655 compiler.Compile(masm(),
1656 object,
1657 holder,
1658 name,
1659 &lookup,
1660 edx,
1661 ebx,
1662 edi,
1663 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001664
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001665 // Restore receiver.
1666 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001667
1668 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001669 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001671 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001672 __ j(not_equal, &miss, not_taken);
1673
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001674 // Patch the receiver on the stack with the global proxy if
1675 // necessary.
1676 if (object->IsGlobalObject()) {
1677 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1678 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1679 }
1680
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001681 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001682 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1684
1685 // Handle load cache miss.
1686 __ bind(&miss);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001687 GenerateMissBranch();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
1689 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691}
1692
1693
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001694Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1695 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001696 JSGlobalPropertyCell* cell,
1697 JSFunction* function,
1698 String* name) {
1699 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001700 // -- ecx : name
1701 // -- esp[0] : return address
1702 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1703 // -- ...
1704 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001705 // -----------------------------------
1706 Label miss;
1707
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001708 GenerateNameCheck(name, &miss);
1709
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001710 // Get the number of arguments.
1711 const int argc = arguments().immediate();
1712
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001713 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001714 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001715
1716 // If the object is the holder then we know that it's a global
1717 // object which can only happen for contextual calls. In this case,
1718 // the receiver cannot be a smi.
1719 if (object != holder) {
1720 __ test(edx, Immediate(kSmiTagMask));
1721 __ j(zero, &miss, not_taken);
1722 }
1723
1724 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001725 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001726
1727 // Get the value from the cell.
1728 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1729 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1730
1731 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001732 if (Heap::InNewSpace(function)) {
1733 // We can't embed a pointer to a function in new space so we have
1734 // to verify that the shared function info is unchanged. This has
1735 // the nice side effect that multiple closures based on the same
1736 // function can all use this call IC. Before we load through the
1737 // function, we have to verify that it still is a function.
1738 __ test(edi, Immediate(kSmiTagMask));
1739 __ j(zero, &miss, not_taken);
1740 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1741 __ j(not_equal, &miss, not_taken);
1742
1743 // Check the shared function info. Make sure it hasn't changed.
1744 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1745 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1746 __ j(not_equal, &miss, not_taken);
1747 } else {
1748 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1749 __ j(not_equal, &miss, not_taken);
1750 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001751
1752 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001753 if (object->IsGlobalObject()) {
1754 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1755 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1756 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001757
1758 // Setup the context (function already in edi).
1759 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1760
1761 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001762 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001763 ASSERT(function->is_compiled());
1764 Handle<Code> code(function->code());
1765 ParameterCount expected(function->shared()->formal_parameter_count());
1766 __ InvokeCode(code, expected, arguments(),
1767 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1768
1769 // Handle call cache miss.
1770 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001771 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
lrn@chromium.org1af7e1b2010-06-07 11:12:01 +00001772 GenerateMissBranch();
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001773
1774 // Return the generated code.
1775 return GetCode(NORMAL, name);
1776}
1777
1778
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1780 int index,
1781 Map* transition,
1782 String* name) {
1783 // ----------- S t a t e -------------
1784 // -- eax : value
1785 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001786 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 Label miss;
1790
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001792 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001793 object,
1794 index,
1795 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001796 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001797 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798
1799 // Handle store cache miss.
1800 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001801 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001803 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804
1805 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001806 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807}
1808
1809
1810Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1811 AccessorInfo* callback,
1812 String* name) {
1813 // ----------- S t a t e -------------
1814 // -- eax : value
1815 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001816 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 Label miss;
1820
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001822 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 __ j(zero, &miss, not_taken);
1824
1825 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001826 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827 Immediate(Handle<Map>(object->map())));
1828 __ j(not_equal, &miss, not_taken);
1829
1830 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001831 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001832 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833 }
1834
1835 // Stub never generated for non-global objects that require access
1836 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001837 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838
1839 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001840 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1842 __ push(ecx); // name
1843 __ push(eax); // value
1844 __ push(ebx); // restore return address
1845
mads.s.ager31e71382008-08-13 09:32:07 +00001846 // Do tail-call to the runtime system.
1847 ExternalReference store_callback_property =
1848 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001849 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850
1851 // Handle store cache miss.
1852 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001853 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001854 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855
1856 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001857 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858}
1859
1860
1861Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1862 String* name) {
1863 // ----------- S t a t e -------------
1864 // -- eax : value
1865 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001866 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 Label miss;
1870
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001872 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873 __ j(zero, &miss, not_taken);
1874
1875 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001876 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877 Immediate(Handle<Map>(receiver->map())));
1878 __ j(not_equal, &miss, not_taken);
1879
1880 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001881 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001882 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001883 }
1884
1885 // Stub never generated for non-global objects that require access
1886 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001887 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888
1889 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001890 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891 __ push(ecx); // name
1892 __ push(eax); // value
1893 __ push(ebx); // restore return address
1894
mads.s.ager31e71382008-08-13 09:32:07 +00001895 // Do tail-call to the runtime system.
1896 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001897 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001898 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001899
1900 // Handle store cache miss.
1901 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001903 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001904
1905 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001906 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907}
1908
1909
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001910Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1911 JSGlobalPropertyCell* cell,
1912 String* name) {
1913 // ----------- S t a t e -------------
1914 // -- eax : value
1915 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001916 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001917 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001918 // -----------------------------------
1919 Label miss;
1920
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001921 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001922 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001923 Immediate(Handle<Map>(object->map())));
1924 __ j(not_equal, &miss, not_taken);
1925
1926 // Store the value in the cell.
1927 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1928 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1929
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001930 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001931 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001932 __ ret(0);
1933
1934 // Handle store cache miss.
1935 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001936 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1937 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1938 __ jmp(ic, RelocInfo::CODE_TARGET);
1939
1940 // Return the generated code.
1941 return GetCode(NORMAL, name);
1942}
1943
1944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1946 int index,
1947 Map* transition,
1948 String* name) {
1949 // ----------- S t a t e -------------
1950 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001951 // -- ecx : key
1952 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 Label miss;
1956
1957 __ IncrementCounter(&Counters::keyed_store_field, 1);
1958
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 // Check that the name has not changed.
1960 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1961 __ j(not_equal, &miss, not_taken);
1962
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001964 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001965 object,
1966 index,
1967 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001968 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001969 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970
1971 // Handle store cache miss.
1972 __ bind(&miss);
1973 __ DecrementCounter(&Counters::keyed_store_field, 1);
1974 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001975 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976
1977 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001978 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979}
1980
1981
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001982Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1983 JSObject* object,
1984 JSObject* last) {
1985 // ----------- S t a t e -------------
1986 // -- eax : receiver
1987 // -- ecx : name
1988 // -- esp[0] : return address
1989 // -----------------------------------
1990 Label miss;
1991
1992 // Check that the receiver isn't a smi.
1993 __ test(eax, Immediate(kSmiTagMask));
1994 __ j(zero, &miss, not_taken);
1995
1996 // Check the maps of the full prototype chain. Also check that
1997 // global property cells up to (but not including) the last object
1998 // in the prototype chain are empty.
1999 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
2000
2001 // If the last object in the prototype chain is a global object,
2002 // check that the global property cell is empty.
2003 if (last->IsGlobalObject()) {
2004 Object* cell = GenerateCheckPropertyCell(masm(),
2005 GlobalObject::cast(last),
2006 name,
2007 edx,
2008 &miss);
2009 if (cell->IsFailure()) return cell;
2010 }
2011
2012 // Return undefined if maps of the full prototype chain are still the
2013 // same and no global property with this name contains a value.
2014 __ mov(eax, Factory::undefined_value());
2015 __ ret(0);
2016
2017 __ bind(&miss);
2018 GenerateLoadMiss(masm(), Code::LOAD_IC);
2019
2020 // Return the generated code.
2021 return GetCode(NONEXISTENT, Heap::empty_string());
2022}
2023
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002024
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002025Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2026 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002027 int index,
2028 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002030 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 // -- ecx : name
2032 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 Label miss;
2035
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002036 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002037 __ bind(&miss);
2038 GenerateLoadMiss(masm(), Code::LOAD_IC);
2039
2040 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002041 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002042}
2043
2044
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002045Object* LoadStubCompiler::CompileLoadCallback(String* name,
2046 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002048 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002049 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002050 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002051 // -- ecx : name
2052 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 Label miss;
2055
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002056 Failure* failure = Failure::InternalError();
2057 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2058 callback, name, &miss, &failure);
2059 if (!success) return failure;
2060
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 __ bind(&miss);
2062 GenerateLoadMiss(masm(), Code::LOAD_IC);
2063
2064 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002065 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066}
2067
2068
2069Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2070 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002071 Object* value,
2072 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002074 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 // -- ecx : name
2076 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 Label miss;
2079
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002080 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 __ bind(&miss);
2082 GenerateLoadMiss(masm(), Code::LOAD_IC);
2083
2084 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002085 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086}
2087
2088
2089Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2090 JSObject* holder,
2091 String* name) {
2092 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002093 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 // -- ecx : name
2095 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 Label miss;
2098
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002099 LookupResult lookup;
2100 LookupPostInterceptor(holder, name, &lookup);
2101
ager@chromium.orge2902be2009-06-08 12:21:35 +00002102 // TODO(368): Compile in the whole chain: all the interceptors in
2103 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002104 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002105 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002106 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002107 eax,
2108 ecx,
2109 edx,
2110 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002111 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002112 &miss);
2113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002114 __ bind(&miss);
2115 GenerateLoadMiss(masm(), Code::LOAD_IC);
2116
2117 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002118 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119}
2120
2121
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002122Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2123 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002124 JSGlobalPropertyCell* cell,
2125 String* name,
2126 bool is_dont_delete) {
2127 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002128 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002129 // -- ecx : name
2130 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002131 // -----------------------------------
2132 Label miss;
2133
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002134 // If the object is the holder then we know that it's a global
2135 // object which can only happen for contextual loads. In this case,
2136 // the receiver cannot be a smi.
2137 if (object != holder) {
2138 __ test(eax, Immediate(kSmiTagMask));
2139 __ j(zero, &miss, not_taken);
2140 }
2141
2142 // Check that the maps haven't changed.
2143 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002144
2145 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2147 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002148
2149 // Check for deleted property if property can actually be deleted.
2150 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002151 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002152 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002153 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002154 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002155 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002156 }
2157
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002158 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002159 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002160 __ ret(0);
2161
2162 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002163 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2164 GenerateLoadMiss(masm(), Code::LOAD_IC);
2165
2166 // Return the generated code.
2167 return GetCode(NORMAL, name);
2168}
2169
2170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002171Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2172 JSObject* receiver,
2173 JSObject* holder,
2174 int index) {
2175 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002176 // -- eax : key
2177 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 Label miss;
2181
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 __ IncrementCounter(&Counters::keyed_load_field, 1);
2183
2184 // Check that the name has not changed.
2185 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2186 __ j(not_equal, &miss, not_taken);
2187
ager@chromium.org5c838252010-02-19 08:53:10 +00002188 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002189
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190 __ bind(&miss);
2191 __ DecrementCounter(&Counters::keyed_load_field, 1);
2192 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2193
2194 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002195 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196}
2197
2198
2199Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2200 JSObject* receiver,
2201 JSObject* holder,
2202 AccessorInfo* callback) {
2203 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002204 // -- eax : key
2205 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 Label miss;
2209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2211
2212 // Check that the name has not changed.
2213 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2214 __ j(not_equal, &miss, not_taken);
2215
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002216 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002217 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002218 callback, name, &miss, &failure);
2219 if (!success) return failure;
2220
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 __ bind(&miss);
2222 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2223 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2224
2225 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002226 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227}
2228
2229
2230Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2231 JSObject* receiver,
2232 JSObject* holder,
2233 Object* value) {
2234 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002235 // -- eax : key
2236 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 Label miss;
2240
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2242
2243 // Check that the name has not changed.
2244 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2245 __ j(not_equal, &miss, not_taken);
2246
ager@chromium.org5c838252010-02-19 08:53:10 +00002247 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002248 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249 __ bind(&miss);
2250 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2251 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2252
2253 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002254 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255}
2256
2257
2258Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2259 JSObject* holder,
2260 String* name) {
2261 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002262 // -- eax : key
2263 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 Label miss;
2267
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2269
2270 // Check that the name has not changed.
2271 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2272 __ j(not_equal, &miss, not_taken);
2273
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002274 LookupResult lookup;
2275 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002276 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002277 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002278 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002279 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002280 eax,
2281 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002282 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002283 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002284 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 __ bind(&miss);
2286 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2287 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2288
2289 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291}
2292
2293
2294
2295
2296Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2297 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002298 // -- eax : key
2299 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 Label miss;
2303
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2305
2306 // Check that the name has not changed.
2307 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2308 __ j(not_equal, &miss, not_taken);
2309
ager@chromium.org5c838252010-02-19 08:53:10 +00002310 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 __ bind(&miss);
2312 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2313 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2314
2315 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002316 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002317}
2318
2319
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002320Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002321 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002322 // -- eax : key
2323 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 Label miss;
2327
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2329
2330 // Check that the name has not changed.
2331 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2332 __ j(not_equal, &miss, not_taken);
2333
ager@chromium.org5c838252010-02-19 08:53:10 +00002334 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 __ bind(&miss);
2336 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2337 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2338
2339 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002340 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002341}
2342
2343
2344Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2345 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002346 // -- eax : key
2347 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 Label miss;
2351
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2353
2354 // Check that the name has not changed.
2355 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2356 __ j(not_equal, &miss, not_taken);
2357
ager@chromium.org5c838252010-02-19 08:53:10 +00002358 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002359 __ bind(&miss);
2360 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2361 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2362
2363 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002364 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002365}
2366
2367
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002368// Specialized stub for constructing objects from functions which only have only
2369// simple assignments of the form this.x = ...; in their body.
2370Object* ConstructStubCompiler::CompileConstructStub(
2371 SharedFunctionInfo* shared) {
2372 // ----------- S t a t e -------------
2373 // -- eax : argc
2374 // -- edi : constructor
2375 // -- esp[0] : return address
2376 // -- esp[4] : last argument
2377 // -----------------------------------
2378 Label generic_stub_call;
2379#ifdef ENABLE_DEBUGGER_SUPPORT
2380 // Check to see whether there are any break points in the function code. If
2381 // there are jump to the generic constructor stub which calls the actual
2382 // code for the function thereby hitting the break points.
2383 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2384 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2385 __ cmp(ebx, Factory::undefined_value());
2386 __ j(not_equal, &generic_stub_call, not_taken);
2387#endif
2388
2389 // Load the initial map and verify that it is in fact a map.
2390 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2391 // Will both indicate a NULL and a Smi.
2392 __ test(ebx, Immediate(kSmiTagMask));
2393 __ j(zero, &generic_stub_call);
2394 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2395 __ j(not_equal, &generic_stub_call);
2396
2397#ifdef DEBUG
2398 // Cannot construct functions this way.
2399 // edi: constructor
2400 // ebx: initial map
2401 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2402 __ Assert(not_equal, "Function constructed by construct stub.");
2403#endif
2404
2405 // Now allocate the JSObject on the heap by moving the new space allocation
2406 // top forward.
2407 // edi: constructor
2408 // ebx: initial map
2409 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2410 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002411 __ AllocateInNewSpace(ecx,
2412 edx,
2413 ecx,
2414 no_reg,
2415 &generic_stub_call,
2416 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002417
2418 // Allocated the JSObject, now initialize the fields and add the heap tag.
2419 // ebx: initial map
2420 // edx: JSObject (untagged)
2421 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2422 __ mov(ebx, Factory::empty_fixed_array());
2423 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2424 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2425
2426 // Push the allocated object to the stack. This is the object that will be
2427 // returned (after it is tagged).
2428 __ push(edx);
2429
2430 // eax: argc
2431 // edx: JSObject (untagged)
2432 // Load the address of the first in-object property into edx.
2433 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2434 // Calculate the location of the first argument. The stack contains the
2435 // allocated object and the return address on top of the argc arguments.
2436 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2437
2438 // Use edi for holding undefined which is used in several places below.
2439 __ mov(edi, Factory::undefined_value());
2440
2441 // eax: argc
2442 // ecx: first argument
2443 // edx: first in-object property of the JSObject
2444 // edi: undefined
2445 // Fill the initialized properties with a constant value or a passed argument
2446 // depending on the this.x = ...; assignment in the function.
2447 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2448 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002449 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002450 // If argument is not passed the property is set to undefined,
2451 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002452 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002453 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002454 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002455 if (CpuFeatures::IsSupported(CMOV)) {
2456 CpuFeatures::Scope use_cmov(CMOV);
2457 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2458 } else {
2459 Label not_passed;
2460 __ j(below_equal, &not_passed);
2461 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2462 __ bind(&not_passed);
2463 }
2464 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002465 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002466 } else {
2467 // Set the property to the constant value.
2468 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2469 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2470 }
2471 }
2472
2473 // Fill the unused in-object property fields with undefined.
2474 for (int i = shared->this_property_assignments_count();
2475 i < shared->CalculateInObjectProperties();
2476 i++) {
2477 __ mov(Operand(edx, i * kPointerSize), edi);
2478 }
2479
2480 // Move argc to ebx and retrieve and tag the JSObject to return.
2481 __ mov(ebx, eax);
2482 __ pop(eax);
2483 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2484
2485 // Remove caller arguments and receiver from the stack and return.
2486 __ pop(ecx);
2487 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2488 __ push(ecx);
2489 __ IncrementCounter(&Counters::constructed_objects, 1);
2490 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2491 __ ret(0);
2492
2493 // Jump to the generic stub in case the specialized code cannot handle the
2494 // construction.
2495 __ bind(&generic_stub_call);
2496 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2497 Handle<Code> generic_construct_stub(code);
2498 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2499
2500 // Return the generated code.
2501 return GetCode();
2502}
2503
2504
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002505#undef __
2506
2507} } // namespace v8::internal
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00002508
2509#endif // V8_TARGET_ARCH_IA32