blob: eba4e1fd5f2bd3f10b0a9551eeaabdd63646f803 [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
30#include "ic-inl.h"
31#include "codegen-inl.h"
32#include "stub-cache.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.org65dad4b2009-04-23 08:48:43 +000037#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39
40static void ProbeTable(MacroAssembler* masm,
41 Code::Flags flags,
42 StubCache::Table table,
43 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000044 Register offset,
45 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046 ExternalReference key_offset(SCTableReference::keyReference(table));
47 ExternalReference value_offset(SCTableReference::valueReference(table));
48
49 Label miss;
50
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000051 if (extra.is_valid()) {
52 // Get the code entry from the cache.
53 __ mov(extra, Operand::StaticArray(offset, times_2, value_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055 // Check that the key in the entry matches the name.
56 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
57 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000059 // Check that the flags match what we're looking for.
60 __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
61 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
62 __ cmp(offset, flags);
63 __ j(not_equal, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000065 // Jump to the first instruction in the code stub.
66 __ add(Operand(extra), Immediate(Code::kHeaderSize - kHeapObjectTag));
67 __ jmp(Operand(extra));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000069 __ bind(&miss);
70 } else {
71 // Save the offset on the stack.
72 __ push(offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000074 // Check that the key in the entry matches the name.
75 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
76 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000078 // Get the code entry from the cache.
79 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
80
81 // Check that the flags match what we're looking for.
82 __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
83 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
84 __ cmp(offset, flags);
85 __ j(not_equal, &miss);
86
87 // Restore offset and re-load code entry from cache.
88 __ pop(offset);
89 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
90
91 // Jump to the first instruction in the code stub.
92 __ add(Operand(offset), Immediate(Code::kHeaderSize - kHeapObjectTag));
93 __ jmp(Operand(offset));
94
95 // Pop at miss.
96 __ bind(&miss);
97 __ pop(offset);
98 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099}
100
101
102void StubCache::GenerateProbe(MacroAssembler* masm,
103 Code::Flags flags,
104 Register receiver,
105 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000106 Register scratch,
107 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 Label miss;
109
110 // Make sure that code is valid. The shifting code relies on the
111 // entry size being 8.
112 ASSERT(sizeof(Entry) == 8);
113
114 // Make sure the flags does not name a specific type.
115 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
116
117 // Make sure that there are no register conflicts.
118 ASSERT(!scratch.is(receiver));
119 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000120 ASSERT(!extra.is(receiver));
121 ASSERT(!extra.is(name));
122 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
124 // Check that the receiver isn't a smi.
125 __ test(receiver, Immediate(kSmiTagMask));
126 __ j(zero, &miss, not_taken);
127
128 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000129 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000130 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 __ xor_(scratch, flags);
132 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
133
134 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000135 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000138 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000139 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
140 __ xor_(scratch, flags);
141 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 __ sub(scratch, Operand(name));
143 __ add(Operand(scratch), Immediate(flags));
144 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
145
146 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000147 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149 // Cache miss: Fall-through and let caller handle the miss by
150 // entering the runtime system.
151 __ bind(&miss);
152}
153
154
155void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
156 int index,
157 Register prototype) {
158 // Load the global or builtins object from the current context.
159 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
160 // Load the global context from the global or builtins object.
161 __ mov(prototype,
162 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
163 // Load the function from the global context.
164 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
165 // Load the initial map. The global functions all have initial maps.
166 __ mov(prototype,
167 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
168 // Load the prototype from the initial map.
169 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
170}
171
172
173void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
174 Register receiver,
175 Register scratch,
176 Label* miss_label) {
177 // Check that the receiver isn't a smi.
178 __ test(receiver, Immediate(kSmiTagMask));
179 __ j(zero, miss_label, not_taken);
180
181 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 __ j(not_equal, miss_label, not_taken);
184
185 // Load length directly from the JS array.
186 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
187 __ ret(0);
188}
189
190
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000191// Generate code to check if an object is a string. If the object is
192// a string, the map's instance type is left in the scratch register.
193static void GenerateStringCheck(MacroAssembler* masm,
194 Register receiver,
195 Register scratch,
196 Label* smi,
197 Label* non_string_object) {
198 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000200 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000202 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
204 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000205 ASSERT(kNotStringTag != 0);
206 __ test(scratch, Immediate(kNotStringTag));
207 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208}
209
210
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000211void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
212 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000213 Register scratch1,
214 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000215 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000216 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000218 // Check if the object is a string leaving the instance type in the
219 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000220 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000222 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +0000224 __ SmiTag(eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000226
227 // Check if the object is a JSValue wrapper.
228 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000229 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000230 __ j(not_equal, miss, not_taken);
231
232 // Check if the wrapped value is a string and load the length
233 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000234 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
235 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
236 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +0000237 __ SmiTag(eax);
ager@chromium.org5c838252010-02-19 08:53:10 +0000238 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
243 Register receiver,
244 Register scratch1,
245 Register scratch2,
246 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000247 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
ager@chromium.org7c537e22008-10-16 08:43:32 +0000252
253// Load a fast property out of a holder object (src). In-object properties
254// are loaded directly otherwise the property is loaded from the properties
255// fixed array.
256void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000257 Register dst, Register src,
258 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Adjust for the number of properties stored in the holder.
260 index -= holder->map()->inobject_properties();
261 if (index < 0) {
262 // Get the property straight out of the holder.
263 int offset = holder->map()->instance_size() + (index * kPointerSize);
264 __ mov(dst, FieldOperand(src, offset));
265 } else {
266 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000267 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
269 __ mov(dst, FieldOperand(dst, offset));
270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271}
272
273
ager@chromium.org5c838252010-02-19 08:53:10 +0000274static void PushInterceptorArguments(MacroAssembler* masm,
275 Register receiver,
276 Register holder,
277 Register name,
278 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000279 __ push(name);
280 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
281 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000282 Register scratch = name;
283 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
284 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000285 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000286 __ push(holder);
287 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000288}
289
290
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000291static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
292 Register receiver,
293 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000294 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000295 JSObject* holder_obj) {
296 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000297 __ CallExternalReference(
298 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
299 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000300}
301
302
303template <class Compiler>
304static void CompileLoadInterceptor(Compiler* compiler,
305 StubCompiler* stub_compiler,
306 MacroAssembler* masm,
307 JSObject* object,
308 JSObject* holder,
309 String* name,
310 LookupResult* lookup,
311 Register receiver,
312 Register scratch1,
313 Register scratch2,
314 Label* miss) {
315 ASSERT(holder->HasNamedInterceptor());
316 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
317
318 // Check that the receiver isn't a smi.
319 __ test(receiver, Immediate(kSmiTagMask));
320 __ j(zero, miss, not_taken);
321
322 // Check that the maps haven't changed.
323 Register reg =
324 stub_compiler->CheckPrototypes(object, receiver, holder,
325 scratch1, scratch2, name, miss);
326
ager@chromium.org5c838252010-02-19 08:53:10 +0000327 if (lookup->IsProperty() && lookup->IsCacheable()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000328 compiler->CompileCacheable(masm,
329 stub_compiler,
330 receiver,
331 reg,
332 scratch1,
333 scratch2,
334 holder,
335 lookup,
336 name,
337 miss);
338 } else {
339 compiler->CompileRegular(masm,
340 receiver,
341 reg,
342 scratch2,
343 holder,
344 miss);
345 }
346}
347
348
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000349class LoadInterceptorCompiler BASE_EMBEDDED {
350 public:
351 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
352
353 void CompileCacheable(MacroAssembler* masm,
354 StubCompiler* stub_compiler,
355 Register receiver,
356 Register holder,
357 Register scratch1,
358 Register scratch2,
359 JSObject* holder_obj,
360 LookupResult* lookup,
361 String* name,
362 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000363 AccessorInfo* callback = NULL;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000364 bool optimize = false;
365 // So far the most popular follow ups for interceptor loads are FIELD
366 // and CALLBACKS, so inline only them, other cases may be added
367 // later.
368 if (lookup->type() == FIELD) {
369 optimize = true;
370 } else if (lookup->type() == CALLBACKS) {
371 Object* callback_object = lookup->GetCallbackObject();
372 if (callback_object->IsAccessorInfo()) {
373 callback = AccessorInfo::cast(callback_object);
374 optimize = callback->getter() != NULL;
375 }
376 }
377
378 if (!optimize) {
379 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
380 return;
381 }
382
383 // Note: starting a frame here makes GC aware of pointers pushed below.
384 __ EnterInternalFrame();
385
386 if (lookup->type() == CALLBACKS) {
387 __ push(receiver);
388 }
389 __ push(holder);
390 __ push(name_);
391
392 CompileCallLoadPropertyWithInterceptor(masm,
393 receiver,
394 holder,
395 name_,
396 holder_obj);
397
398 Label interceptor_failed;
399 __ cmp(eax, Factory::no_interceptor_result_sentinel());
400 __ j(equal, &interceptor_failed);
401 __ LeaveInternalFrame();
402 __ ret(0);
403
404 __ bind(&interceptor_failed);
405 __ pop(name_);
406 __ pop(holder);
407 if (lookup->type() == CALLBACKS) {
408 __ pop(receiver);
409 }
410
411 __ LeaveInternalFrame();
412
413 if (lookup->type() == FIELD) {
414 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
415 lookup->holder(), scratch1,
416 scratch2,
417 name,
418 miss_label);
419 stub_compiler->GenerateFastPropertyLoad(masm, eax,
420 holder, lookup->holder(),
421 lookup->GetFieldIndex());
422 __ ret(0);
423 } else {
424 ASSERT(lookup->type() == CALLBACKS);
425 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
426 ASSERT(callback != NULL);
427 ASSERT(callback->getter() != NULL);
428
429 Label cleanup;
430 __ pop(scratch2);
431 __ push(receiver);
432 __ push(scratch2);
433
434 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
435 lookup->holder(), scratch1,
436 scratch2,
437 name,
438 &cleanup);
439
440 __ pop(scratch2); // save old return address
441 __ push(holder);
442 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
443 __ push(holder);
444 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
445 __ push(name_);
446 __ push(scratch2); // restore old return address
447
448 ExternalReference ref =
449 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000450 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000451
452 __ bind(&cleanup);
453 __ pop(scratch1);
454 __ pop(scratch2);
455 __ push(scratch1);
456 }
457 }
458
459
460 void CompileRegular(MacroAssembler* masm,
461 Register receiver,
462 Register holder,
463 Register scratch,
464 JSObject* holder_obj,
465 Label* miss_label) {
466 __ pop(scratch); // save old return address
467 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
468 __ push(scratch); // restore old return address
469
470 ExternalReference ref = ExternalReference(
471 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000472 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000473 }
474
475 private:
476 Register name_;
477};
478
479
ager@chromium.org5c838252010-02-19 08:53:10 +0000480// Holds information about possible function call optimizations.
481class CallOptimization BASE_EMBEDDED {
482 public:
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000483 explicit CallOptimization(LookupResult* lookup) {
484 if (!lookup->IsProperty() || !lookup->IsCacheable() ||
485 lookup->type() != CONSTANT_FUNCTION) {
486 Initialize(NULL);
487 } else {
488 // We only optimize constant function calls.
489 Initialize(lookup->GetConstantFunction());
490 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000491 }
492
493 explicit CallOptimization(JSFunction* function) {
494 Initialize(function);
495 }
496
497 bool is_constant_call() const {
498 return constant_function_ != NULL;
499 }
500
501 JSFunction* constant_function() const {
502 ASSERT(constant_function_ != NULL);
503 return constant_function_;
504 }
505
506 bool is_simple_api_call() const {
507 return is_simple_api_call_;
508 }
509
510 FunctionTemplateInfo* expected_receiver_type() const {
511 ASSERT(is_simple_api_call_);
512 return expected_receiver_type_;
513 }
514
515 CallHandlerInfo* api_call_info() const {
516 ASSERT(is_simple_api_call_);
517 return api_call_info_;
518 }
519
520 // Returns the depth of the object having the expected type in the
521 // prototype chain between the two arguments.
522 int GetPrototypeDepthOfExpectedType(JSObject* object,
523 JSObject* holder) const {
524 ASSERT(is_simple_api_call_);
525 if (expected_receiver_type_ == NULL) return 0;
526 int depth = 0;
527 while (object != holder) {
528 if (object->IsInstanceOf(expected_receiver_type_)) return depth;
529 object = JSObject::cast(object->GetPrototype());
530 ++depth;
531 }
532 if (holder->IsInstanceOf(expected_receiver_type_)) return depth;
533 return kInvalidProtoDepth;
534 }
535
536 private:
537 void Initialize(JSFunction* function) {
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000538 constant_function_ = NULL;
539 is_simple_api_call_ = false;
540 expected_receiver_type_ = NULL;
541 api_call_info_ = NULL;
542
543 if (function == NULL || !function->is_compiled()) return;
ager@chromium.org5c838252010-02-19 08:53:10 +0000544
545 constant_function_ = function;
ager@chromium.org5c838252010-02-19 08:53:10 +0000546 AnalyzePossibleApiFunction(function);
547 }
548
549 // Determines whether the given function can be called using the
550 // fast api call builtin.
551 void AnalyzePossibleApiFunction(JSFunction* function) {
552 SharedFunctionInfo* sfi = function->shared();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000553 if (!sfi->IsApiFunction()) return;
554 FunctionTemplateInfo* info = sfi->get_api_func_data();
ager@chromium.org5c838252010-02-19 08:53:10 +0000555
556 // Require a C++ callback.
557 if (info->call_code()->IsUndefined()) return;
558 api_call_info_ = CallHandlerInfo::cast(info->call_code());
559
560 // Accept signatures that either have no restrictions at all or
561 // only have restrictions on the receiver.
562 if (!info->signature()->IsUndefined()) {
563 SignatureInfo* signature = SignatureInfo::cast(info->signature());
564 if (!signature->args()->IsUndefined()) return;
565 if (!signature->receiver()->IsUndefined()) {
566 expected_receiver_type_ =
567 FunctionTemplateInfo::cast(signature->receiver());
568 }
569 }
570
571 is_simple_api_call_ = true;
572 }
573
574 JSFunction* constant_function_;
575 bool is_simple_api_call_;
576 FunctionTemplateInfo* expected_receiver_type_;
577 CallHandlerInfo* api_call_info_;
578};
579
580
581// Reserves space for the extra arguments to FastHandleApiCall in the
582// caller's frame.
583//
584// These arguments are set by CheckPrototypes and GenerateFastApiCall.
585static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
586 // ----------- S t a t e -------------
587 // -- esp[0] : return address
588 // -- esp[4] : last argument in the internal frame of the caller
589 // -----------------------------------
590 __ pop(scratch);
591 __ push(Immediate(Smi::FromInt(0)));
592 __ push(Immediate(Smi::FromInt(0)));
593 __ push(Immediate(Smi::FromInt(0)));
594 __ push(Immediate(Smi::FromInt(0)));
595 __ push(scratch);
596}
597
598
599// Undoes the effects of ReserveSpaceForFastApiCall.
600static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
601 // ----------- S t a t e -------------
602 // -- esp[0] : return address
603 // -- esp[4] : last fast api call extra argument
604 // -- ...
605 // -- esp[16] : first fast api call extra argument
606 // -- esp[20] : last argument in the internal frame
607 // -----------------------------------
608 __ pop(scratch);
609 __ add(Operand(esp), Immediate(kPointerSize * 4));
610 __ push(scratch);
611}
612
613
614// Generates call to FastHandleApiCall builtin.
615static void GenerateFastApiCall(MacroAssembler* masm,
616 const CallOptimization& optimization,
617 int argc) {
618 // ----------- S t a t e -------------
619 // -- esp[0] : return address
620 // -- esp[4] : object passing the type check
621 // (last fast api call extra argument,
622 // set by CheckPrototypes)
623 // -- esp[8] : api call data
624 // -- esp[12] : api callback
625 // -- esp[16] : api function
626 // (first fast api call extra argument)
627 // -- esp[20] : last argument
628 // -- ...
629 // -- esp[(argc + 5) * 4] : first argument
630 // -- esp[(argc + 6) * 4] : receiver
631 // -----------------------------------
632
633 // Get the function and setup the context.
634 JSFunction* function = optimization.constant_function();
635 __ mov(edi, Immediate(Handle<JSFunction>(function)));
636 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
637
638 // Pass the additional arguments FastHandleApiCall expects.
639 __ mov(Operand(esp, 4 * kPointerSize), edi);
640 bool info_loaded = false;
641 Object* callback = optimization.api_call_info()->callback();
642 if (Heap::InNewSpace(callback)) {
643 info_loaded = true;
644 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
645 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
646 __ mov(Operand(esp, 3 * kPointerSize), ebx);
647 } else {
648 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
649 }
650 Object* call_data = optimization.api_call_info()->data();
651 if (Heap::InNewSpace(call_data)) {
652 if (!info_loaded) {
653 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
654 }
655 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
656 __ mov(Operand(esp, 2 * kPointerSize), ebx);
657 } else {
658 __ mov(Operand(esp, 2 * kPointerSize),
659 Immediate(Handle<Object>(call_data)));
660 }
661
662 // Set the number of arguments.
663 __ mov(eax, Immediate(argc + 4));
664
665 // Jump to the fast api call builtin (tail call).
666 Handle<Code> code = Handle<Code>(
667 Builtins::builtin(Builtins::FastHandleApiCall));
668 ParameterCount expected(0);
669 __ InvokeCode(code, expected, expected,
670 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
671}
672
673
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000674class CallInterceptorCompiler BASE_EMBEDDED {
675 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 CallInterceptorCompiler(StubCompiler* stub_compiler,
677 const ParameterCount& arguments,
678 Register name)
679 : stub_compiler_(stub_compiler),
680 arguments_(arguments),
681 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000682
ager@chromium.org5c838252010-02-19 08:53:10 +0000683 void Compile(MacroAssembler* masm,
684 JSObject* object,
685 JSObject* holder,
686 String* name,
687 LookupResult* lookup,
688 Register receiver,
689 Register scratch1,
690 Register scratch2,
691 Label* miss) {
692 ASSERT(holder->HasNamedInterceptor());
693 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
694
695 // Check that the receiver isn't a smi.
696 __ test(receiver, Immediate(kSmiTagMask));
697 __ j(zero, miss, not_taken);
698
699 CallOptimization optimization(lookup);
700
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000701 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000702 CompileCacheable(masm,
703 object,
704 receiver,
705 scratch1,
706 scratch2,
707 holder,
708 lookup,
709 name,
710 optimization,
711 miss);
712 } else {
713 CompileRegular(masm,
714 object,
715 receiver,
716 scratch1,
717 scratch2,
718 name,
719 holder,
720 miss);
721 }
722 }
723
724 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000725 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000726 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000727 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000728 Register scratch1,
729 Register scratch2,
730 JSObject* holder_obj,
731 LookupResult* lookup,
732 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000733 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000734 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000735 ASSERT(optimization.is_constant_call());
736 ASSERT(!lookup->holder()->IsGlobalObject());
737
738 int depth1 = kInvalidProtoDepth;
739 int depth2 = kInvalidProtoDepth;
740 bool can_do_fast_api_call = false;
741 if (optimization.is_simple_api_call() &&
742 !lookup->holder()->IsGlobalObject()) {
743 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
744 if (depth1 == kInvalidProtoDepth) {
745 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
746 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000747 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000748 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
749 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000750 }
751
ager@chromium.org5c838252010-02-19 08:53:10 +0000752 __ IncrementCounter(&Counters::call_const_interceptor, 1);
753
754 if (can_do_fast_api_call) {
755 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
756 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000757 }
758
ager@chromium.org5c838252010-02-19 08:53:10 +0000759 Label miss_cleanup;
760 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
761 Register holder =
762 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
763 scratch1, scratch2, name,
764 depth1, miss);
765
766 Label regular_invoke;
767 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
768
769 // Generate code for the failed interceptor case.
770
771 // Check the lookup is still valid.
772 stub_compiler_->CheckPrototypes(holder_obj, receiver,
773 lookup->holder(),
774 scratch1, scratch2, name,
775 depth2, miss);
776
777 if (can_do_fast_api_call) {
778 GenerateFastApiCall(masm, optimization, arguments_.immediate());
779 } else {
780 __ InvokeFunction(optimization.constant_function(), arguments_,
781 JUMP_FUNCTION);
782 }
783
784 if (can_do_fast_api_call) {
785 __ bind(&miss_cleanup);
786 FreeSpaceForFastApiCall(masm, scratch1);
787 __ jmp(miss_label);
788 }
789
790 __ bind(&regular_invoke);
791 if (can_do_fast_api_call) {
792 FreeSpaceForFastApiCall(masm, scratch1);
793 }
794 }
795
796 void CompileRegular(MacroAssembler* masm,
797 JSObject* object,
798 Register receiver,
799 Register scratch1,
800 Register scratch2,
801 String* name,
802 JSObject* holder_obj,
803 Label* miss_label) {
804 Register holder =
805 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
806 scratch1, scratch2, name,
807 miss_label);
808
809 __ EnterInternalFrame();
810 // Save the name_ register across the call.
811 __ push(name_);
812
813 PushInterceptorArguments(masm,
814 receiver,
815 holder,
816 name_,
817 holder_obj);
818
819 __ CallExternalReference(
820 ExternalReference(
821 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
822 5);
823
824 // Restore the name_ register.
825 __ pop(name_);
826 __ LeaveInternalFrame();
827 }
828
829 void LoadWithInterceptor(MacroAssembler* masm,
830 Register receiver,
831 Register holder,
832 JSObject* holder_obj,
833 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000834 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000835 __ push(holder); // Save the holder.
836 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000837
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000838 CompileCallLoadPropertyWithInterceptor(masm,
839 receiver,
840 holder,
841 name_,
842 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000843
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000844 __ pop(name_); // Restore the name.
845 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000846 __ LeaveInternalFrame();
847
848 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000849 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000850 }
851
ager@chromium.org5c838252010-02-19 08:53:10 +0000852 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000853 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000854 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000855};
856
857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
859 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
860 Code* code = NULL;
861 if (kind == Code::LOAD_IC) {
862 code = Builtins::builtin(Builtins::LoadIC_Miss);
863 } else {
864 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
865 }
866
867 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000868 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869}
870
871
ager@chromium.org5c838252010-02-19 08:53:10 +0000872// Both name_reg and receiver_reg are preserved on jumps to miss_label,
873// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000874void StubCompiler::GenerateStoreField(MacroAssembler* masm,
875 JSObject* object,
876 int index,
877 Map* transition,
878 Register receiver_reg,
879 Register name_reg,
880 Register scratch,
881 Label* miss_label) {
882 // Check that the object isn't a smi.
883 __ test(receiver_reg, Immediate(kSmiTagMask));
884 __ j(zero, miss_label, not_taken);
885
886 // Check that the map of the object hasn't changed.
887 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
888 Immediate(Handle<Map>(object->map())));
889 __ j(not_equal, miss_label, not_taken);
890
891 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000892 if (object->IsJSGlobalProxy()) {
893 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 }
895
896 // Stub never generated for non-global objects that require access
897 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000898 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000899
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000900 // Perform map transition for the receiver if necessary.
901 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
902 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000903 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000904 __ pop(scratch); // Return address.
905 __ push(receiver_reg);
906 __ push(Immediate(Handle<Map>(transition)));
907 __ push(eax);
908 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000909 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000910 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000911 return;
912 }
913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000914 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000915 // Update the map of the object; no write barrier updating is
916 // needed because the map is never in new space.
917 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
918 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000919 }
920
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000921 // Adjust for the number of properties stored in the object. Even in the
922 // face of a transition we can use the old map here because the size of the
923 // object and the number of in-object properties is not going to change.
924 index -= object->map()->inobject_properties();
925
ager@chromium.org7c537e22008-10-16 08:43:32 +0000926 if (index < 0) {
927 // Set the property straight into the object.
928 int offset = object->map()->instance_size() + (index * kPointerSize);
929 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000930
ager@chromium.org7c537e22008-10-16 08:43:32 +0000931 // Update the write barrier for the array address.
932 // Pass the value being stored in the now unused name_reg.
933 __ mov(name_reg, Operand(eax));
934 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
935 } else {
936 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000937 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000938 // Get the properties array (optimistically).
939 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000940 __ mov(FieldOperand(scratch, offset), eax);
941
942 // Update the write barrier for the array address.
943 // Pass the value being stored in the now unused name_reg.
944 __ mov(name_reg, Operand(eax));
945 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
946 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000947
948 // Return the value (register eax).
949 __ ret(0);
950}
951
952
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000953// Generate code to check that a global property cell is empty. Create
954// the property cell at compilation time if no cell exists for the
955// property.
956static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
957 GlobalObject* global,
958 String* name,
959 Register scratch,
960 Label* miss) {
961 Object* probe = global->EnsurePropertyCell(name);
962 if (probe->IsFailure()) return probe;
963 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
964 ASSERT(cell->value()->IsTheHole());
965 __ mov(scratch, Immediate(Handle<Object>(cell)));
966 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
967 Immediate(Factory::the_hole_value()));
968 __ j(not_equal, miss, not_taken);
969 return cell;
970}
971
972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000974#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
976
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000977Register StubCompiler::CheckPrototypes(JSObject* object,
978 Register object_reg,
979 JSObject* holder,
980 Register holder_reg,
981 Register scratch,
982 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000983 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000984 Label* miss) {
985 // Check that the maps haven't changed.
986 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000987 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
988 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000989
990 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000991 // that their maps haven't changed. We also need to check that the
992 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000993 while (object != holder) {
994 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000995 Object* cell = GenerateCheckPropertyCell(masm(),
996 GlobalObject::cast(object),
997 name,
998 scratch,
999 miss);
1000 if (cell->IsFailure()) {
1001 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001002 return result;
1003 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001004 }
1005 object = JSObject::cast(object->GetPrototype());
1006 }
1007
ager@chromium.org5c838252010-02-19 08:53:10 +00001008 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001009 return result;
1010}
1011
1012
1013void StubCompiler::GenerateLoadField(JSObject* object,
1014 JSObject* holder,
1015 Register receiver,
1016 Register scratch1,
1017 Register scratch2,
1018 int index,
1019 String* name,
1020 Label* miss) {
1021 // Check that the receiver isn't a smi.
1022 __ test(receiver, Immediate(kSmiTagMask));
1023 __ j(zero, miss, not_taken);
1024
1025 // Check the prototype chain.
1026 Register reg =
1027 CheckPrototypes(object, receiver, holder,
1028 scratch1, scratch2, name, miss);
1029
1030 // Get the value from the properties.
1031 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1032 __ ret(0);
1033}
1034
1035
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001036bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001037 JSObject* holder,
1038 Register receiver,
1039 Register name_reg,
1040 Register scratch1,
1041 Register scratch2,
1042 AccessorInfo* callback,
1043 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001044 Label* miss,
1045 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001046 // Check that the receiver isn't a smi.
1047 __ test(receiver, Immediate(kSmiTagMask));
1048 __ j(zero, miss, not_taken);
1049
1050 // Check that the maps haven't changed.
1051 Register reg =
1052 CheckPrototypes(object, receiver, holder,
1053 scratch1, scratch2, name, miss);
1054
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001055 Handle<AccessorInfo> callback_handle(callback);
1056
1057 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1058 __ EnterInternalFrame();
1059 __ PushHandleScope(other);
1060 // Push the stack address where the list of arguments ends
1061 __ mov(other, esp);
1062 __ sub(Operand(other), Immediate(2 * kPointerSize));
1063 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001064 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001065 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001066 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001067 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001068 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001069 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001070 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001071 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001072 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001073 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001074
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001075 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001076 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001077 Address getter_address = v8::ToCData<Address>(callback->getter());
1078 ApiFunction fun(getter_address);
1079 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001080 // Emitting a stub call may try to allocate (if the code is not
1081 // already generated). Do not allow the assembler to perform a
1082 // garbage collection but instead return the allocation failure
1083 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001084 Object* result = masm()->TryCallStub(&stub);
1085 if (result->IsFailure()) {
1086 *failure = Failure::cast(result);
1087 return false;
1088 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001089
1090 // We need to avoid using eax since that now holds the result.
1091 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001092 // Emitting PopHandleScope may try to allocate. Do not allow the
1093 // assembler to perform a garbage collection but instead return a
1094 // failure object.
1095 result = masm()->TryPopHandleScope(eax, tmp);
1096 if (result->IsFailure()) {
1097 *failure = Failure::cast(result);
1098 return false;
1099 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001100 __ LeaveInternalFrame();
1101
1102 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001103 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001104}
1105
1106
1107void StubCompiler::GenerateLoadConstant(JSObject* object,
1108 JSObject* holder,
1109 Register receiver,
1110 Register scratch1,
1111 Register scratch2,
1112 Object* value,
1113 String* name,
1114 Label* miss) {
1115 // Check that the receiver isn't a smi.
1116 __ test(receiver, Immediate(kSmiTagMask));
1117 __ j(zero, miss, not_taken);
1118
1119 // Check that the maps haven't changed.
1120 Register reg =
1121 CheckPrototypes(object, receiver, holder,
1122 scratch1, scratch2, name, miss);
1123
1124 // Return the constant value.
1125 __ mov(eax, Handle<Object>(value));
1126 __ ret(0);
1127}
1128
1129
1130void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1131 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001132 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133 Register receiver,
1134 Register name_reg,
1135 Register scratch1,
1136 Register scratch2,
1137 String* name,
1138 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001139 LoadInterceptorCompiler compiler(name_reg);
1140 CompileLoadInterceptor(&compiler,
1141 this,
1142 masm(),
1143 object,
1144 holder,
1145 name,
1146 lookup,
1147 receiver,
1148 scratch1,
1149 scratch2,
1150 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001151}
1152
1153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154// TODO(1241006): Avoid having lazy compile stubs specialized by the
1155// number of arguments. It is not needed anymore.
1156Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001158 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159
1160 // Push a copy of the function onto the stack.
1161 __ push(edi);
1162
1163 __ push(edi); // function is also the parameter to the runtime call
1164 __ CallRuntime(Runtime::kLazyCompile, 1);
1165 __ pop(edi);
1166
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001167 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001168 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169
1170 // Do a tail-call of the compiled function.
1171 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1172 __ jmp(Operand(ecx));
1173
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001174 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175}
1176
1177
ager@chromium.org5c838252010-02-19 08:53:10 +00001178Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001180 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001181 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001183 // -- ecx : name
1184 // -- esp[0] : return address
1185 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1186 // -- ...
1187 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 Label miss;
1190
1191 // Get the receiver from the stack.
1192 const int argc = arguments().immediate();
1193 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1194
1195 // Check that the receiver isn't a smi.
1196 __ test(edx, Immediate(kSmiTagMask));
1197 __ j(zero, &miss, not_taken);
1198
1199 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001200 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
ager@chromium.org7c537e22008-10-16 08:43:32 +00001202 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203
1204 // Check that the function really is a function.
1205 __ test(edi, Immediate(kSmiTagMask));
1206 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001207 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 __ j(not_equal, &miss, not_taken);
1209
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001210 // Patch the receiver on the stack with the global proxy if
1211 // necessary.
1212 if (object->IsGlobalObject()) {
1213 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1214 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1215 }
1216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 // Invoke the function.
1218 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1219
1220 // Handle call cache miss.
1221 __ bind(&miss);
1222 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001223 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224
1225 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001226 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227}
1228
1229
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001230Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1231 JSObject* holder,
1232 JSFunction* function,
1233 String* name,
1234 CheckType check) {
1235 // ----------- S t a t e -------------
1236 // -- ecx : name
1237 // -- esp[0] : return address
1238 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1239 // -- ...
1240 // -- esp[(argc + 1) * 4] : receiver
1241 // -----------------------------------
1242 ASSERT(check == RECEIVER_MAP_CHECK);
1243
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001244 // If object is not an array, bail out to regular call.
1245 if (!object->IsJSArray()) {
1246 return Heap::undefined_value();
1247 }
1248
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001249 Label miss;
1250
1251 // Get the receiver from the stack.
1252 const int argc = arguments().immediate();
1253 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1254
1255 // Check that the receiver isn't a smi.
1256 __ test(edx, Immediate(kSmiTagMask));
1257 __ j(zero, &miss);
1258
1259 CheckPrototypes(JSObject::cast(object), edx,
1260 holder, ebx,
1261 eax, name, &miss);
1262
1263 if (argc == 0) {
1264 // Noop, return the length.
1265 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1266 __ ret((argc + 1) * kPointerSize);
1267 } else {
1268 // Get the elements array of the object.
1269 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1270
1271 // Check that the elements are in fast mode (not dictionary).
1272 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1273 Immediate(Factory::fixed_array_map()));
1274 __ j(not_equal, &miss);
1275
1276 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001277 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001278
1279 // Get the array's length into eax and calculate new length.
1280 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1281 STATIC_ASSERT(kSmiTagSize == 1);
1282 STATIC_ASSERT(kSmiTag == 0);
1283 __ add(Operand(eax), Immediate(argc << 1));
1284
1285 // Get the element's length into ecx.
1286 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1287 __ SmiTag(ecx);
1288
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001289 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001290 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001291 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001292
1293 // Save new length.
1294 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1295
1296 // Push the element.
1297 __ lea(edx, FieldOperand(ebx,
1298 eax, times_half_pointer_size,
1299 FixedArray::kHeaderSize - argc * kPointerSize));
1300 __ mov(ecx, Operand(esp, argc * kPointerSize));
1301 __ mov(Operand(edx, 0), ecx);
1302
1303 // Check if value is a smi.
1304 __ test(ecx, Immediate(kSmiTagMask));
1305 __ j(not_zero, &with_rset_update);
1306
1307 __ bind(&exit);
1308 __ ret((argc + 1) * kPointerSize);
1309
1310 __ bind(&with_rset_update);
1311
1312 __ InNewSpace(ebx, ecx, equal, &exit);
1313
1314 RecordWriteStub stub(ebx, edx, ecx);
1315 __ CallStub(&stub);
1316 __ ret((argc + 1) * kPointerSize);
1317
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001318 __ bind(&attempt_to_grow_elements);
1319 ExternalReference new_space_allocation_top =
1320 ExternalReference::new_space_allocation_top_address();
1321 ExternalReference new_space_allocation_limit =
1322 ExternalReference::new_space_allocation_limit_address();
1323
1324 const int kAllocationDelta = 4;
1325 // Load top.
1326 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1327
1328 // Check if it's the end of elements.
1329 __ lea(edx, FieldOperand(ebx,
1330 eax, times_half_pointer_size,
1331 FixedArray::kHeaderSize - argc * kPointerSize));
1332 __ cmp(edx, Operand(ecx));
1333 __ j(not_equal, &call_builtin);
1334 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1335 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
1336 __ j(greater, &call_builtin);
1337
1338 // We fit and could grow elements.
1339 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1340 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001341
1342 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001343 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001344 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001345 for (int i = 1; i < kAllocationDelta; i++) {
1346 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001347 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001348 }
1349
1350 // Restore receiver to edx as finish sequence assumes it's here.
1351 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1352
1353 // Increment element's and array's sizes.
1354 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1355 Immediate(kAllocationDelta));
1356 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1357
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001358 // Elements are in new space, so no remembered set updates are necessary.
1359 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001360
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001361 __ bind(&call_builtin);
1362 }
1363
1364 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1365 argc + 1,
1366 1);
1367 }
1368
1369 __ bind(&miss);
1370
1371 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1372 __ jmp(ic, RelocInfo::CODE_TARGET);
1373
1374 // Return the generated code.
1375 String* function_name = NULL;
1376 if (function->shared()->name()->IsString()) {
1377 function_name = String::cast(function->shared()->name());
1378 }
1379 return GetCode(CONSTANT_FUNCTION, function_name);
1380}
1381
1382
1383Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1384 JSObject* holder,
1385 JSFunction* function,
1386 String* name,
1387 CheckType check) {
1388 // ----------- S t a t e -------------
1389 // -- ecx : name
1390 // -- esp[0] : return address
1391 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1392 // -- ...
1393 // -- esp[(argc + 1) * 4] : receiver
1394 // -----------------------------------
1395 ASSERT(check == RECEIVER_MAP_CHECK);
1396
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001397 // If object is not an array, bail out to regular call.
1398 if (!object->IsJSArray()) {
1399 return Heap::undefined_value();
1400 }
1401
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001402 Label miss, empty_array, call_builtin;
1403
1404 // Get the receiver from the stack.
1405 const int argc = arguments().immediate();
1406 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1407
1408 // Check that the receiver isn't a smi.
1409 __ test(edx, Immediate(kSmiTagMask));
1410 __ j(zero, &miss);
1411
1412 CheckPrototypes(JSObject::cast(object), edx,
1413 holder, ebx,
1414 eax, name, &miss);
1415
1416 // Get the elements array of the object.
1417 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1418
1419 // Check that the elements are in fast mode (not dictionary).
1420 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1421 Immediate(Factory::fixed_array_map()));
1422 __ j(not_equal, &miss);
1423
1424 // Get the array's length into ecx and calculate new length.
1425 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1426 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1427 __ j(negative, &empty_array);
1428
1429 // Get the last element.
1430 STATIC_ASSERT(kSmiTagSize == 1);
1431 STATIC_ASSERT(kSmiTag == 0);
1432 __ mov(eax, FieldOperand(ebx,
1433 ecx, times_half_pointer_size,
1434 FixedArray::kHeaderSize));
1435 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1436 __ j(equal, &call_builtin);
1437
1438 // Set the array's length.
1439 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1440
1441 // Fill with the hole.
1442 __ mov(FieldOperand(ebx,
1443 ecx, times_half_pointer_size,
1444 FixedArray::kHeaderSize),
1445 Immediate(Factory::the_hole_value()));
1446 __ ret((argc + 1) * kPointerSize);
1447
1448 __ bind(&empty_array);
1449 __ mov(eax, Immediate(Factory::undefined_value()));
1450 __ ret((argc + 1) * kPointerSize);
1451
1452 __ bind(&call_builtin);
1453
1454 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1455 argc + 1,
1456 1);
1457
1458 __ bind(&miss);
1459
1460 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1461 __ jmp(ic, RelocInfo::CODE_TARGET);
1462
1463 // Return the generated code.
1464 String* function_name = NULL;
1465 if (function->shared()->name()->IsString()) {
1466 function_name = String::cast(function->shared()->name());
1467 }
1468 return GetCode(CONSTANT_FUNCTION, function_name);
1469}
1470
1471
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472Object* CallStubCompiler::CompileCallConstant(Object* object,
1473 JSObject* holder,
1474 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001475 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001476 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001477 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001478 // -- ecx : name
1479 // -- esp[0] : return address
1480 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1481 // -- ...
1482 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001484
1485 SharedFunctionInfo* function_info = function->shared();
1486 if (function_info->HasCustomCallGenerator()) {
1487 CustomCallGenerator generator =
1488 ToCData<CustomCallGenerator>(function_info->function_data());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001489 Object* result = generator(this, object, holder, function, name, check);
1490 // undefined means bail out to regular compiler.
1491 if (!result->IsUndefined()) {
1492 return result;
1493 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001494 }
1495
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001496 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497
1498 // Get the receiver from the stack.
1499 const int argc = arguments().immediate();
1500 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1501
1502 // Check that the receiver isn't a smi.
1503 if (check != NUMBER_CHECK) {
1504 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001505 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 }
1507
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001508 // Make sure that it's okay not to patch the on stack receiver
1509 // unless we're doing a receiver map check.
1510 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1511
ager@chromium.org5c838252010-02-19 08:53:10 +00001512 CallOptimization optimization(function);
1513 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001514 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 switch (check) {
1517 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001518 __ IncrementCounter(&Counters::call_const, 1);
1519
1520 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1521 depth = optimization.GetPrototypeDepthOfExpectedType(
1522 JSObject::cast(object), holder);
1523 }
1524
1525 if (depth != kInvalidProtoDepth) {
1526 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1527 ReserveSpaceForFastApiCall(masm(), eax);
1528 }
1529
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001531 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001532 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001533
1534 // Patch the receiver on the stack with the global proxy if
1535 // necessary.
1536 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001537 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001538 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1539 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1540 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541 break;
1542
1543 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001544 if (!function->IsBuiltin()) {
1545 // Calling non-builtins with a value as receiver requires boxing.
1546 __ jmp(&miss);
1547 } else {
1548 // Check that the object is a string or a symbol.
1549 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1550 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1551 __ cmp(eax, FIRST_NONSTRING_TYPE);
1552 __ j(above_equal, &miss, not_taken);
1553 // Check that the maps starting from the prototype haven't changed.
1554 GenerateLoadGlobalFunctionPrototype(masm(),
1555 Context::STRING_FUNCTION_INDEX,
1556 eax);
1557 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1558 ebx, edx, name, &miss);
1559 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 break;
1561
1562 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001563 if (!function->IsBuiltin()) {
1564 // Calling non-builtins with a value as receiver requires boxing.
1565 __ jmp(&miss);
1566 } else {
1567 Label fast;
1568 // Check that the object is a smi or a heap number.
1569 __ test(edx, Immediate(kSmiTagMask));
1570 __ j(zero, &fast, taken);
1571 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1572 __ j(not_equal, &miss, not_taken);
1573 __ bind(&fast);
1574 // Check that the maps starting from the prototype haven't changed.
1575 GenerateLoadGlobalFunctionPrototype(masm(),
1576 Context::NUMBER_FUNCTION_INDEX,
1577 eax);
1578 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1579 ebx, edx, name, &miss);
1580 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581 break;
1582 }
1583
1584 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001585 if (!function->IsBuiltin()) {
1586 // Calling non-builtins with a value as receiver requires boxing.
1587 __ jmp(&miss);
1588 } else {
1589 Label fast;
1590 // Check that the object is a boolean.
1591 __ cmp(edx, Factory::true_value());
1592 __ j(equal, &fast, taken);
1593 __ cmp(edx, Factory::false_value());
1594 __ j(not_equal, &miss, not_taken);
1595 __ bind(&fast);
1596 // Check that the maps starting from the prototype haven't changed.
1597 GenerateLoadGlobalFunctionPrototype(masm(),
1598 Context::BOOLEAN_FUNCTION_INDEX,
1599 eax);
1600 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1601 ebx, edx, name, &miss);
1602 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 break;
1604 }
1605
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001606 default:
1607 UNREACHABLE();
1608 }
1609
ager@chromium.org5c838252010-02-19 08:53:10 +00001610 if (depth != kInvalidProtoDepth) {
1611 GenerateFastApiCall(masm(), optimization, argc);
1612 } else {
1613 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1614 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615
1616 // Handle call cache miss.
1617 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001618 if (depth != kInvalidProtoDepth) {
1619 FreeSpaceForFastApiCall(masm(), eax);
1620 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001621 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001623 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001624
1625 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001626 String* function_name = NULL;
1627 if (function->shared()->name()->IsString()) {
1628 function_name = String::cast(function->shared()->name());
1629 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001630 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631}
1632
1633
ager@chromium.org5c838252010-02-19 08:53:10 +00001634Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635 JSObject* holder,
1636 String* name) {
1637 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001638 // -- ecx : name
1639 // -- esp[0] : return address
1640 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1641 // -- ...
1642 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001643 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 Label miss;
1645
1646 // Get the number of arguments.
1647 const int argc = arguments().immediate();
1648
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001649 LookupResult lookup;
1650 LookupPostInterceptor(holder, name, &lookup);
1651
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 // Get the receiver from the stack.
1653 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001654
ager@chromium.org5c838252010-02-19 08:53:10 +00001655 CallInterceptorCompiler compiler(this, arguments(), ecx);
1656 compiler.Compile(masm(),
1657 object,
1658 holder,
1659 name,
1660 &lookup,
1661 edx,
1662 ebx,
1663 edi,
1664 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001665
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001666 // Restore receiver.
1667 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668
1669 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001670 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001672 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 __ j(not_equal, &miss, not_taken);
1674
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001675 // Patch the receiver on the stack with the global proxy if
1676 // necessary.
1677 if (object->IsGlobalObject()) {
1678 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1679 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1680 }
1681
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001683 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1685
1686 // Handle load cache miss.
1687 __ bind(&miss);
1688 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001689 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001690
1691 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001692 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001693}
1694
1695
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001696Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1697 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001698 JSGlobalPropertyCell* cell,
1699 JSFunction* function,
1700 String* name) {
1701 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001702 // -- ecx : name
1703 // -- esp[0] : return address
1704 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1705 // -- ...
1706 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001707 // -----------------------------------
1708 Label 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);
1772 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1773 __ jmp(ic, RelocInfo::CODE_TARGET);
1774
1775 // Return the generated code.
1776 return GetCode(NORMAL, name);
1777}
1778
1779
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001780Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1781 int index,
1782 Map* transition,
1783 String* name) {
1784 // ----------- S t a t e -------------
1785 // -- eax : value
1786 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001787 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 Label miss;
1791
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001793 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001794 object,
1795 index,
1796 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001797 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001798 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799
1800 // Handle store cache miss.
1801 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001802 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001804 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805
1806 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001807 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808}
1809
1810
1811Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1812 AccessorInfo* callback,
1813 String* name) {
1814 // ----------- S t a t e -------------
1815 // -- eax : value
1816 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001817 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 Label miss;
1821
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001823 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824 __ j(zero, &miss, not_taken);
1825
1826 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001827 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 Immediate(Handle<Map>(object->map())));
1829 __ j(not_equal, &miss, not_taken);
1830
1831 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001832 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001833 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001834 }
1835
1836 // Stub never generated for non-global objects that require access
1837 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001838 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839
1840 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001841 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1843 __ push(ecx); // name
1844 __ push(eax); // value
1845 __ push(ebx); // restore return address
1846
mads.s.ager31e71382008-08-13 09:32:07 +00001847 // Do tail-call to the runtime system.
1848 ExternalReference store_callback_property =
1849 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001850 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851
1852 // Handle store cache miss.
1853 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001855 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856
1857 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001858 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859}
1860
1861
1862Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1863 String* name) {
1864 // ----------- S t a t e -------------
1865 // -- eax : value
1866 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001867 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001869 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 Label miss;
1871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001873 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001874 __ j(zero, &miss, not_taken);
1875
1876 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001877 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 Immediate(Handle<Map>(receiver->map())));
1879 __ j(not_equal, &miss, not_taken);
1880
1881 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001882 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001883 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 }
1885
1886 // Stub never generated for non-global objects that require access
1887 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001888 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889
1890 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001891 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001892 __ push(ecx); // name
1893 __ push(eax); // value
1894 __ push(ebx); // restore return address
1895
mads.s.ager31e71382008-08-13 09:32:07 +00001896 // Do tail-call to the runtime system.
1897 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001899 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001900
1901 // Handle store cache miss.
1902 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001904 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905
1906 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001907 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001908}
1909
1910
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001911Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1912 JSGlobalPropertyCell* cell,
1913 String* name) {
1914 // ----------- S t a t e -------------
1915 // -- eax : value
1916 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001917 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001918 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001919 // -----------------------------------
1920 Label miss;
1921
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001922 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001923 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001924 Immediate(Handle<Map>(object->map())));
1925 __ j(not_equal, &miss, not_taken);
1926
1927 // Store the value in the cell.
1928 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1929 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1930
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001931 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001932 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001933 __ ret(0);
1934
1935 // Handle store cache miss.
1936 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001937 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1938 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1939 __ jmp(ic, RelocInfo::CODE_TARGET);
1940
1941 // Return the generated code.
1942 return GetCode(NORMAL, name);
1943}
1944
1945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1947 int index,
1948 Map* transition,
1949 String* name) {
1950 // ----------- S t a t e -------------
1951 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001952 // -- ecx : key
1953 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 Label miss;
1957
1958 __ IncrementCounter(&Counters::keyed_store_field, 1);
1959
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 // Check that the name has not changed.
1961 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1962 __ j(not_equal, &miss, not_taken);
1963
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001965 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001966 object,
1967 index,
1968 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001969 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001970 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971
1972 // Handle store cache miss.
1973 __ bind(&miss);
1974 __ DecrementCounter(&Counters::keyed_store_field, 1);
1975 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001976 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977
1978 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001979 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980}
1981
1982
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001983Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1984 JSObject* object,
1985 JSObject* last) {
1986 // ----------- S t a t e -------------
1987 // -- eax : receiver
1988 // -- ecx : name
1989 // -- esp[0] : return address
1990 // -----------------------------------
1991 Label miss;
1992
1993 // Check that the receiver isn't a smi.
1994 __ test(eax, Immediate(kSmiTagMask));
1995 __ j(zero, &miss, not_taken);
1996
1997 // Check the maps of the full prototype chain. Also check that
1998 // global property cells up to (but not including) the last object
1999 // in the prototype chain are empty.
2000 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
2001
2002 // If the last object in the prototype chain is a global object,
2003 // check that the global property cell is empty.
2004 if (last->IsGlobalObject()) {
2005 Object* cell = GenerateCheckPropertyCell(masm(),
2006 GlobalObject::cast(last),
2007 name,
2008 edx,
2009 &miss);
2010 if (cell->IsFailure()) return cell;
2011 }
2012
2013 // Return undefined if maps of the full prototype chain are still the
2014 // same and no global property with this name contains a value.
2015 __ mov(eax, Factory::undefined_value());
2016 __ ret(0);
2017
2018 __ bind(&miss);
2019 GenerateLoadMiss(masm(), Code::LOAD_IC);
2020
2021 // Return the generated code.
2022 return GetCode(NONEXISTENT, Heap::empty_string());
2023}
2024
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002025
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002026Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2027 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 int index,
2029 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002031 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 // -- ecx : name
2033 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002035 Label miss;
2036
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002037 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 __ bind(&miss);
2039 GenerateLoadMiss(masm(), Code::LOAD_IC);
2040
2041 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002042 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002043}
2044
2045
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002046Object* LoadStubCompiler::CompileLoadCallback(String* name,
2047 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002049 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002051 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 // -- ecx : name
2053 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 Label miss;
2056
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002057 Failure* failure = Failure::InternalError();
2058 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2059 callback, name, &miss, &failure);
2060 if (!success) return failure;
2061
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 __ bind(&miss);
2063 GenerateLoadMiss(masm(), Code::LOAD_IC);
2064
2065 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002066 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067}
2068
2069
2070Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2071 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 Object* value,
2073 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002075 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 // -- ecx : name
2077 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002079 Label miss;
2080
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002081 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002082 __ bind(&miss);
2083 GenerateLoadMiss(masm(), Code::LOAD_IC);
2084
2085 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002086 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002087}
2088
2089
2090Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2091 JSObject* holder,
2092 String* name) {
2093 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002094 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 // -- ecx : name
2096 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098 Label miss;
2099
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002100 LookupResult lookup;
2101 LookupPostInterceptor(holder, name, &lookup);
2102
ager@chromium.orge2902be2009-06-08 12:21:35 +00002103 // TODO(368): Compile in the whole chain: all the interceptors in
2104 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002105 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002106 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002107 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002108 eax,
2109 ecx,
2110 edx,
2111 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002112 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002113 &miss);
2114
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002115 __ bind(&miss);
2116 GenerateLoadMiss(masm(), Code::LOAD_IC);
2117
2118 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002119 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120}
2121
2122
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002123Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2124 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002125 JSGlobalPropertyCell* cell,
2126 String* name,
2127 bool is_dont_delete) {
2128 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002129 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002130 // -- ecx : name
2131 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002132 // -----------------------------------
2133 Label miss;
2134
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002135 // If the object is the holder then we know that it's a global
2136 // object which can only happen for contextual loads. In this case,
2137 // the receiver cannot be a smi.
2138 if (object != holder) {
2139 __ test(eax, Immediate(kSmiTagMask));
2140 __ j(zero, &miss, not_taken);
2141 }
2142
2143 // Check that the maps haven't changed.
2144 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002145
2146 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002147 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2148 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002149
2150 // Check for deleted property if property can actually be deleted.
2151 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002152 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002153 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002154 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002155 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002156 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002157 }
2158
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002159 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002160 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002161 __ ret(0);
2162
2163 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002164 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2165 GenerateLoadMiss(masm(), Code::LOAD_IC);
2166
2167 // Return the generated code.
2168 return GetCode(NORMAL, name);
2169}
2170
2171
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002172Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2173 JSObject* receiver,
2174 JSObject* holder,
2175 int index) {
2176 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002177 // -- eax : key
2178 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002180 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 Label miss;
2182
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 __ IncrementCounter(&Counters::keyed_load_field, 1);
2184
2185 // Check that the name has not changed.
2186 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2187 __ j(not_equal, &miss, not_taken);
2188
ager@chromium.org5c838252010-02-19 08:53:10 +00002189 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002190
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 __ bind(&miss);
2192 __ DecrementCounter(&Counters::keyed_load_field, 1);
2193 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2194
2195 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002196 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197}
2198
2199
2200Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2201 JSObject* receiver,
2202 JSObject* holder,
2203 AccessorInfo* callback) {
2204 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002205 // -- eax : key
2206 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 Label miss;
2210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2212
2213 // Check that the name has not changed.
2214 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2215 __ j(not_equal, &miss, not_taken);
2216
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002217 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002218 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002219 callback, name, &miss, &failure);
2220 if (!success) return failure;
2221
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 __ bind(&miss);
2223 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2224 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2225
2226 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002227 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228}
2229
2230
2231Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2232 JSObject* receiver,
2233 JSObject* holder,
2234 Object* value) {
2235 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002236 // -- eax : key
2237 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002239 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 Label miss;
2241
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2243
2244 // Check that the name has not changed.
2245 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2246 __ j(not_equal, &miss, not_taken);
2247
ager@chromium.org5c838252010-02-19 08:53:10 +00002248 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002249 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 __ bind(&miss);
2251 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2252 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2253
2254 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002255 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002256}
2257
2258
2259Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2260 JSObject* holder,
2261 String* name) {
2262 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002263 // -- eax : key
2264 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002266 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 Label miss;
2268
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002269 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2270
2271 // Check that the name has not changed.
2272 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2273 __ j(not_equal, &miss, not_taken);
2274
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002275 LookupResult lookup;
2276 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002277 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002278 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002279 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002280 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002281 eax,
2282 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002283 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002284 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002285 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 __ bind(&miss);
2287 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2288 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2289
2290 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002291 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292}
2293
2294
2295
2296
2297Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2298 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002299 // -- eax : key
2300 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002302 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 Label miss;
2304
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002305 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2306
2307 // Check that the name has not changed.
2308 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2309 __ j(not_equal, &miss, not_taken);
2310
ager@chromium.org5c838252010-02-19 08:53:10 +00002311 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 __ bind(&miss);
2313 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2314 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2315
2316 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002317 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002318}
2319
2320
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002321Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002323 // -- eax : key
2324 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002326 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 Label miss;
2328
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002329 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2330
2331 // Check that the name has not changed.
2332 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2333 __ j(not_equal, &miss, not_taken);
2334
ager@chromium.org5c838252010-02-19 08:53:10 +00002335 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 __ bind(&miss);
2337 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2338 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2339
2340 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002341 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002342}
2343
2344
2345Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2346 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002347 // -- eax : key
2348 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002350 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 Label miss;
2352
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002353 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2354
2355 // Check that the name has not changed.
2356 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2357 __ j(not_equal, &miss, not_taken);
2358
ager@chromium.org5c838252010-02-19 08:53:10 +00002359 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002360 __ bind(&miss);
2361 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2362 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2363
2364 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002365 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002366}
2367
2368
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002369// Specialized stub for constructing objects from functions which only have only
2370// simple assignments of the form this.x = ...; in their body.
2371Object* ConstructStubCompiler::CompileConstructStub(
2372 SharedFunctionInfo* shared) {
2373 // ----------- S t a t e -------------
2374 // -- eax : argc
2375 // -- edi : constructor
2376 // -- esp[0] : return address
2377 // -- esp[4] : last argument
2378 // -----------------------------------
2379 Label generic_stub_call;
2380#ifdef ENABLE_DEBUGGER_SUPPORT
2381 // Check to see whether there are any break points in the function code. If
2382 // there are jump to the generic constructor stub which calls the actual
2383 // code for the function thereby hitting the break points.
2384 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2385 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2386 __ cmp(ebx, Factory::undefined_value());
2387 __ j(not_equal, &generic_stub_call, not_taken);
2388#endif
2389
2390 // Load the initial map and verify that it is in fact a map.
2391 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2392 // Will both indicate a NULL and a Smi.
2393 __ test(ebx, Immediate(kSmiTagMask));
2394 __ j(zero, &generic_stub_call);
2395 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2396 __ j(not_equal, &generic_stub_call);
2397
2398#ifdef DEBUG
2399 // Cannot construct functions this way.
2400 // edi: constructor
2401 // ebx: initial map
2402 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2403 __ Assert(not_equal, "Function constructed by construct stub.");
2404#endif
2405
2406 // Now allocate the JSObject on the heap by moving the new space allocation
2407 // top forward.
2408 // edi: constructor
2409 // ebx: initial map
2410 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2411 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002412 __ AllocateInNewSpace(ecx,
2413 edx,
2414 ecx,
2415 no_reg,
2416 &generic_stub_call,
2417 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002418
2419 // Allocated the JSObject, now initialize the fields and add the heap tag.
2420 // ebx: initial map
2421 // edx: JSObject (untagged)
2422 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2423 __ mov(ebx, Factory::empty_fixed_array());
2424 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2425 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2426
2427 // Push the allocated object to the stack. This is the object that will be
2428 // returned (after it is tagged).
2429 __ push(edx);
2430
2431 // eax: argc
2432 // edx: JSObject (untagged)
2433 // Load the address of the first in-object property into edx.
2434 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2435 // Calculate the location of the first argument. The stack contains the
2436 // allocated object and the return address on top of the argc arguments.
2437 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2438
2439 // Use edi for holding undefined which is used in several places below.
2440 __ mov(edi, Factory::undefined_value());
2441
2442 // eax: argc
2443 // ecx: first argument
2444 // edx: first in-object property of the JSObject
2445 // edi: undefined
2446 // Fill the initialized properties with a constant value or a passed argument
2447 // depending on the this.x = ...; assignment in the function.
2448 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2449 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002450 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002451 // If argument is not passed the property is set to undefined,
2452 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002453 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002454 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002455 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002456 if (CpuFeatures::IsSupported(CMOV)) {
2457 CpuFeatures::Scope use_cmov(CMOV);
2458 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2459 } else {
2460 Label not_passed;
2461 __ j(below_equal, &not_passed);
2462 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2463 __ bind(&not_passed);
2464 }
2465 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002466 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002467 } else {
2468 // Set the property to the constant value.
2469 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2470 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2471 }
2472 }
2473
2474 // Fill the unused in-object property fields with undefined.
2475 for (int i = shared->this_property_assignments_count();
2476 i < shared->CalculateInObjectProperties();
2477 i++) {
2478 __ mov(Operand(edx, i * kPointerSize), edi);
2479 }
2480
2481 // Move argc to ebx and retrieve and tag the JSObject to return.
2482 __ mov(ebx, eax);
2483 __ pop(eax);
2484 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2485
2486 // Remove caller arguments and receiver from the stack and return.
2487 __ pop(ecx);
2488 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2489 __ push(ecx);
2490 __ IncrementCounter(&Counters::constructed_objects, 1);
2491 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2492 __ ret(0);
2493
2494 // Jump to the generic stub in case the specialized code cannot handle the
2495 // construction.
2496 __ bind(&generic_stub_call);
2497 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2498 Handle<Code> generic_construct_stub(code);
2499 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2500
2501 // Return the generated code.
2502 return GetCode();
2503}
2504
2505
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002506#undef __
2507
2508} } // namespace v8::internal