blob: 315600bfaeaebe63dc106d9181e152e6e51106f9 [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));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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));
237 __ SmiTag(eax);
238 __ 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
953#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000954#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955
956
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000957Register StubCompiler::CheckPrototypes(JSObject* object,
958 Register object_reg,
959 JSObject* holder,
960 Register holder_reg,
961 Register scratch,
962 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000963 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000964 Label* miss) {
965 // Check that the maps haven't changed.
966 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000967 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
968 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000969
970 // If we've skipped any global objects, it's not enough to verify
971 // that their maps haven't changed.
972 while (object != holder) {
973 if (object->IsGlobalObject()) {
974 GlobalObject* global = GlobalObject::cast(object);
975 Object* probe = global->EnsurePropertyCell(name);
976 if (probe->IsFailure()) {
977 set_failure(Failure::cast(probe));
978 return result;
979 }
980 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
981 ASSERT(cell->value()->IsTheHole());
982 __ mov(scratch, Immediate(Handle<Object>(cell)));
983 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
984 Immediate(Factory::the_hole_value()));
985 __ j(not_equal, miss, not_taken);
986 }
987 object = JSObject::cast(object->GetPrototype());
988 }
989
ager@chromium.org5c838252010-02-19 08:53:10 +0000990 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000991 return result;
992}
993
994
995void StubCompiler::GenerateLoadField(JSObject* object,
996 JSObject* holder,
997 Register receiver,
998 Register scratch1,
999 Register scratch2,
1000 int index,
1001 String* name,
1002 Label* miss) {
1003 // Check that the receiver isn't a smi.
1004 __ test(receiver, Immediate(kSmiTagMask));
1005 __ j(zero, miss, not_taken);
1006
1007 // Check the prototype chain.
1008 Register reg =
1009 CheckPrototypes(object, receiver, holder,
1010 scratch1, scratch2, name, miss);
1011
1012 // Get the value from the properties.
1013 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1014 __ ret(0);
1015}
1016
1017
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001018bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001019 JSObject* holder,
1020 Register receiver,
1021 Register name_reg,
1022 Register scratch1,
1023 Register scratch2,
1024 AccessorInfo* callback,
1025 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001026 Label* miss,
1027 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001028 // Check that the receiver isn't a smi.
1029 __ test(receiver, Immediate(kSmiTagMask));
1030 __ j(zero, miss, not_taken);
1031
1032 // Check that the maps haven't changed.
1033 Register reg =
1034 CheckPrototypes(object, receiver, holder,
1035 scratch1, scratch2, name, miss);
1036
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001037 Handle<AccessorInfo> callback_handle(callback);
1038
1039 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1040 __ EnterInternalFrame();
1041 __ PushHandleScope(other);
1042 // Push the stack address where the list of arguments ends
1043 __ mov(other, esp);
1044 __ sub(Operand(other), Immediate(2 * kPointerSize));
1045 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001046 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001047 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001048 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001049 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001050 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001051 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001052 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001053 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001054 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001055 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001056
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001057 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001058 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001059 Address getter_address = v8::ToCData<Address>(callback->getter());
1060 ApiFunction fun(getter_address);
1061 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001062 // Emitting a stub call may try to allocate (if the code is not
1063 // already generated). Do not allow the assembler to perform a
1064 // garbage collection but instead return the allocation failure
1065 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001066 Object* result = masm()->TryCallStub(&stub);
1067 if (result->IsFailure()) {
1068 *failure = Failure::cast(result);
1069 return false;
1070 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001071
1072 // We need to avoid using eax since that now holds the result.
1073 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001074 // Emitting PopHandleScope may try to allocate. Do not allow the
1075 // assembler to perform a garbage collection but instead return a
1076 // failure object.
1077 result = masm()->TryPopHandleScope(eax, tmp);
1078 if (result->IsFailure()) {
1079 *failure = Failure::cast(result);
1080 return false;
1081 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001082 __ LeaveInternalFrame();
1083
1084 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001085 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001086}
1087
1088
1089void StubCompiler::GenerateLoadConstant(JSObject* object,
1090 JSObject* holder,
1091 Register receiver,
1092 Register scratch1,
1093 Register scratch2,
1094 Object* value,
1095 String* name,
1096 Label* miss) {
1097 // Check that the receiver isn't a smi.
1098 __ test(receiver, Immediate(kSmiTagMask));
1099 __ j(zero, miss, not_taken);
1100
1101 // Check that the maps haven't changed.
1102 Register reg =
1103 CheckPrototypes(object, receiver, holder,
1104 scratch1, scratch2, name, miss);
1105
1106 // Return the constant value.
1107 __ mov(eax, Handle<Object>(value));
1108 __ ret(0);
1109}
1110
1111
1112void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1113 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001114 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001115 Register receiver,
1116 Register name_reg,
1117 Register scratch1,
1118 Register scratch2,
1119 String* name,
1120 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001121 LoadInterceptorCompiler compiler(name_reg);
1122 CompileLoadInterceptor(&compiler,
1123 this,
1124 masm(),
1125 object,
1126 holder,
1127 name,
1128 lookup,
1129 receiver,
1130 scratch1,
1131 scratch2,
1132 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133}
1134
1135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136// TODO(1241006): Avoid having lazy compile stubs specialized by the
1137// number of arguments. It is not needed anymore.
1138Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001140 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141
1142 // Push a copy of the function onto the stack.
1143 __ push(edi);
1144
1145 __ push(edi); // function is also the parameter to the runtime call
1146 __ CallRuntime(Runtime::kLazyCompile, 1);
1147 __ pop(edi);
1148
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001149 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001150 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Do a tail-call of the compiled function.
1153 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1154 __ jmp(Operand(ecx));
1155
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001156 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157}
1158
1159
ager@chromium.org5c838252010-02-19 08:53:10 +00001160Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001163 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001165 // -- ecx : name
1166 // -- esp[0] : return address
1167 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1168 // -- ...
1169 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 Label miss;
1172
1173 // Get the receiver from the stack.
1174 const int argc = arguments().immediate();
1175 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1176
1177 // Check that the receiver isn't a smi.
1178 __ test(edx, Immediate(kSmiTagMask));
1179 __ j(zero, &miss, not_taken);
1180
1181 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001182 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183
ager@chromium.org7c537e22008-10-16 08:43:32 +00001184 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
1186 // Check that the function really is a function.
1187 __ test(edi, Immediate(kSmiTagMask));
1188 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001189 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 __ j(not_equal, &miss, not_taken);
1191
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001192 // Patch the receiver on the stack with the global proxy if
1193 // necessary.
1194 if (object->IsGlobalObject()) {
1195 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1196 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1197 }
1198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 // Invoke the function.
1200 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1201
1202 // Handle call cache miss.
1203 __ bind(&miss);
1204 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001205 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001208 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209}
1210
1211
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001212Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1213 JSObject* holder,
1214 JSFunction* function,
1215 String* name,
1216 CheckType check) {
1217 // ----------- S t a t e -------------
1218 // -- ecx : name
1219 // -- esp[0] : return address
1220 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1221 // -- ...
1222 // -- esp[(argc + 1) * 4] : receiver
1223 // -----------------------------------
1224 ASSERT(check == RECEIVER_MAP_CHECK);
1225
1226 Label miss;
1227
1228 // Get the receiver from the stack.
1229 const int argc = arguments().immediate();
1230 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1231
1232 // Check that the receiver isn't a smi.
1233 __ test(edx, Immediate(kSmiTagMask));
1234 __ j(zero, &miss);
1235
1236 CheckPrototypes(JSObject::cast(object), edx,
1237 holder, ebx,
1238 eax, name, &miss);
1239
1240 if (argc == 0) {
1241 // Noop, return the length.
1242 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1243 __ ret((argc + 1) * kPointerSize);
1244 } else {
1245 // Get the elements array of the object.
1246 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1247
1248 // Check that the elements are in fast mode (not dictionary).
1249 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1250 Immediate(Factory::fixed_array_map()));
1251 __ j(not_equal, &miss);
1252
1253 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001254 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001255
1256 // Get the array's length into eax and calculate new length.
1257 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1258 STATIC_ASSERT(kSmiTagSize == 1);
1259 STATIC_ASSERT(kSmiTag == 0);
1260 __ add(Operand(eax), Immediate(argc << 1));
1261
1262 // Get the element's length into ecx.
1263 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1264 __ SmiTag(ecx);
1265
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001266 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001267 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001268 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001269
1270 // Save new length.
1271 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1272
1273 // Push the element.
1274 __ lea(edx, FieldOperand(ebx,
1275 eax, times_half_pointer_size,
1276 FixedArray::kHeaderSize - argc * kPointerSize));
1277 __ mov(ecx, Operand(esp, argc * kPointerSize));
1278 __ mov(Operand(edx, 0), ecx);
1279
1280 // Check if value is a smi.
1281 __ test(ecx, Immediate(kSmiTagMask));
1282 __ j(not_zero, &with_rset_update);
1283
1284 __ bind(&exit);
1285 __ ret((argc + 1) * kPointerSize);
1286
1287 __ bind(&with_rset_update);
1288
1289 __ InNewSpace(ebx, ecx, equal, &exit);
1290
1291 RecordWriteStub stub(ebx, edx, ecx);
1292 __ CallStub(&stub);
1293 __ ret((argc + 1) * kPointerSize);
1294
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001295 __ bind(&attempt_to_grow_elements);
1296 ExternalReference new_space_allocation_top =
1297 ExternalReference::new_space_allocation_top_address();
1298 ExternalReference new_space_allocation_limit =
1299 ExternalReference::new_space_allocation_limit_address();
1300
1301 const int kAllocationDelta = 4;
1302 // Load top.
1303 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1304
1305 // Check if it's the end of elements.
1306 __ lea(edx, FieldOperand(ebx,
1307 eax, times_half_pointer_size,
1308 FixedArray::kHeaderSize - argc * kPointerSize));
1309 __ cmp(edx, Operand(ecx));
1310 __ j(not_equal, &call_builtin);
1311 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1312 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
1313 __ j(greater, &call_builtin);
1314
1315 // We fit and could grow elements.
1316 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1317 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001318
1319 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001320 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001321 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001322 for (int i = 1; i < kAllocationDelta; i++) {
1323 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001324 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001325 }
1326
1327 // Restore receiver to edx as finish sequence assumes it's here.
1328 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1329
1330 // Increment element's and array's sizes.
1331 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1332 Immediate(kAllocationDelta));
1333 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1334
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001335 // Elements are in new space, so no remembered set updates are necessary.
1336 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001337
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001338 __ bind(&call_builtin);
1339 }
1340
1341 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1342 argc + 1,
1343 1);
1344 }
1345
1346 __ bind(&miss);
1347
1348 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1349 __ jmp(ic, RelocInfo::CODE_TARGET);
1350
1351 // Return the generated code.
1352 String* function_name = NULL;
1353 if (function->shared()->name()->IsString()) {
1354 function_name = String::cast(function->shared()->name());
1355 }
1356 return GetCode(CONSTANT_FUNCTION, function_name);
1357}
1358
1359
1360Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1361 JSObject* holder,
1362 JSFunction* function,
1363 String* name,
1364 CheckType check) {
1365 // ----------- S t a t e -------------
1366 // -- ecx : name
1367 // -- esp[0] : return address
1368 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1369 // -- ...
1370 // -- esp[(argc + 1) * 4] : receiver
1371 // -----------------------------------
1372 ASSERT(check == RECEIVER_MAP_CHECK);
1373
1374 Label miss, empty_array, call_builtin;
1375
1376 // Get the receiver from the stack.
1377 const int argc = arguments().immediate();
1378 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1379
1380 // Check that the receiver isn't a smi.
1381 __ test(edx, Immediate(kSmiTagMask));
1382 __ j(zero, &miss);
1383
1384 CheckPrototypes(JSObject::cast(object), edx,
1385 holder, ebx,
1386 eax, name, &miss);
1387
1388 // Get the elements array of the object.
1389 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1390
1391 // Check that the elements are in fast mode (not dictionary).
1392 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1393 Immediate(Factory::fixed_array_map()));
1394 __ j(not_equal, &miss);
1395
1396 // Get the array's length into ecx and calculate new length.
1397 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1398 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1399 __ j(negative, &empty_array);
1400
1401 // Get the last element.
1402 STATIC_ASSERT(kSmiTagSize == 1);
1403 STATIC_ASSERT(kSmiTag == 0);
1404 __ mov(eax, FieldOperand(ebx,
1405 ecx, times_half_pointer_size,
1406 FixedArray::kHeaderSize));
1407 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1408 __ j(equal, &call_builtin);
1409
1410 // Set the array's length.
1411 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1412
1413 // Fill with the hole.
1414 __ mov(FieldOperand(ebx,
1415 ecx, times_half_pointer_size,
1416 FixedArray::kHeaderSize),
1417 Immediate(Factory::the_hole_value()));
1418 __ ret((argc + 1) * kPointerSize);
1419
1420 __ bind(&empty_array);
1421 __ mov(eax, Immediate(Factory::undefined_value()));
1422 __ ret((argc + 1) * kPointerSize);
1423
1424 __ bind(&call_builtin);
1425
1426 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1427 argc + 1,
1428 1);
1429
1430 __ bind(&miss);
1431
1432 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1433 __ jmp(ic, RelocInfo::CODE_TARGET);
1434
1435 // Return the generated code.
1436 String* function_name = NULL;
1437 if (function->shared()->name()->IsString()) {
1438 function_name = String::cast(function->shared()->name());
1439 }
1440 return GetCode(CONSTANT_FUNCTION, function_name);
1441}
1442
1443
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001444Object* CallStubCompiler::CompileCallConstant(Object* object,
1445 JSObject* holder,
1446 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001447 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001448 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001450 // -- ecx : name
1451 // -- esp[0] : return address
1452 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1453 // -- ...
1454 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001456
1457 SharedFunctionInfo* function_info = function->shared();
1458 if (function_info->HasCustomCallGenerator()) {
1459 CustomCallGenerator generator =
1460 ToCData<CustomCallGenerator>(function_info->function_data());
1461 return generator(this, object, holder, function, name, check);
1462 }
1463
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001464 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001465
1466 // Get the receiver from the stack.
1467 const int argc = arguments().immediate();
1468 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1469
1470 // Check that the receiver isn't a smi.
1471 if (check != NUMBER_CHECK) {
1472 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001473 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 }
1475
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001476 // Make sure that it's okay not to patch the on stack receiver
1477 // unless we're doing a receiver map check.
1478 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1479
ager@chromium.org5c838252010-02-19 08:53:10 +00001480 CallOptimization optimization(function);
1481 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001482 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001483
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001484 switch (check) {
1485 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001486 __ IncrementCounter(&Counters::call_const, 1);
1487
1488 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1489 depth = optimization.GetPrototypeDepthOfExpectedType(
1490 JSObject::cast(object), holder);
1491 }
1492
1493 if (depth != kInvalidProtoDepth) {
1494 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1495 ReserveSpaceForFastApiCall(masm(), eax);
1496 }
1497
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001499 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001500 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001501
1502 // Patch the receiver on the stack with the global proxy if
1503 // necessary.
1504 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001505 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001506 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1507 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1508 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509 break;
1510
1511 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001512 if (!function->IsBuiltin()) {
1513 // Calling non-builtins with a value as receiver requires boxing.
1514 __ jmp(&miss);
1515 } else {
1516 // Check that the object is a string or a symbol.
1517 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1518 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1519 __ cmp(eax, FIRST_NONSTRING_TYPE);
1520 __ j(above_equal, &miss, not_taken);
1521 // Check that the maps starting from the prototype haven't changed.
1522 GenerateLoadGlobalFunctionPrototype(masm(),
1523 Context::STRING_FUNCTION_INDEX,
1524 eax);
1525 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1526 ebx, edx, name, &miss);
1527 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 break;
1529
1530 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001531 if (!function->IsBuiltin()) {
1532 // Calling non-builtins with a value as receiver requires boxing.
1533 __ jmp(&miss);
1534 } else {
1535 Label fast;
1536 // Check that the object is a smi or a heap number.
1537 __ test(edx, Immediate(kSmiTagMask));
1538 __ j(zero, &fast, taken);
1539 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1540 __ j(not_equal, &miss, not_taken);
1541 __ bind(&fast);
1542 // Check that the maps starting from the prototype haven't changed.
1543 GenerateLoadGlobalFunctionPrototype(masm(),
1544 Context::NUMBER_FUNCTION_INDEX,
1545 eax);
1546 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1547 ebx, edx, name, &miss);
1548 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549 break;
1550 }
1551
1552 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001553 if (!function->IsBuiltin()) {
1554 // Calling non-builtins with a value as receiver requires boxing.
1555 __ jmp(&miss);
1556 } else {
1557 Label fast;
1558 // Check that the object is a boolean.
1559 __ cmp(edx, Factory::true_value());
1560 __ j(equal, &fast, taken);
1561 __ cmp(edx, Factory::false_value());
1562 __ j(not_equal, &miss, not_taken);
1563 __ bind(&fast);
1564 // Check that the maps starting from the prototype haven't changed.
1565 GenerateLoadGlobalFunctionPrototype(masm(),
1566 Context::BOOLEAN_FUNCTION_INDEX,
1567 eax);
1568 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1569 ebx, edx, name, &miss);
1570 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571 break;
1572 }
1573
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 default:
1575 UNREACHABLE();
1576 }
1577
ager@chromium.org5c838252010-02-19 08:53:10 +00001578 if (depth != kInvalidProtoDepth) {
1579 GenerateFastApiCall(masm(), optimization, argc);
1580 } else {
1581 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1582 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001583
1584 // Handle call cache miss.
1585 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001586 if (depth != kInvalidProtoDepth) {
1587 FreeSpaceForFastApiCall(masm(), eax);
1588 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001589 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001591 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592
1593 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001594 String* function_name = NULL;
1595 if (function->shared()->name()->IsString()) {
1596 function_name = String::cast(function->shared()->name());
1597 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001598 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001599}
1600
1601
ager@chromium.org5c838252010-02-19 08:53:10 +00001602Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001603 JSObject* holder,
1604 String* name) {
1605 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001606 // -- ecx : name
1607 // -- esp[0] : return address
1608 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1609 // -- ...
1610 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 Label miss;
1613
1614 // Get the number of arguments.
1615 const int argc = arguments().immediate();
1616
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001617 LookupResult lookup;
1618 LookupPostInterceptor(holder, name, &lookup);
1619
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 // Get the receiver from the stack.
1621 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001622
ager@chromium.org5c838252010-02-19 08:53:10 +00001623 CallInterceptorCompiler compiler(this, arguments(), ecx);
1624 compiler.Compile(masm(),
1625 object,
1626 holder,
1627 name,
1628 &lookup,
1629 edx,
1630 ebx,
1631 edi,
1632 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001634 // Restore receiver.
1635 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001636
1637 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001638 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001639 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001640 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 __ j(not_equal, &miss, not_taken);
1642
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001643 // Patch the receiver on the stack with the global proxy if
1644 // necessary.
1645 if (object->IsGlobalObject()) {
1646 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1647 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1648 }
1649
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001651 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001652 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1653
1654 // Handle load cache miss.
1655 __ bind(&miss);
1656 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001657 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001658
1659 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001660 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661}
1662
1663
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001664Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1665 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001666 JSGlobalPropertyCell* cell,
1667 JSFunction* function,
1668 String* name) {
1669 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001670 // -- ecx : name
1671 // -- esp[0] : return address
1672 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1673 // -- ...
1674 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001675 // -----------------------------------
1676 Label miss;
1677
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001678 // Get the number of arguments.
1679 const int argc = arguments().immediate();
1680
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001681 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001682 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001683
1684 // If the object is the holder then we know that it's a global
1685 // object which can only happen for contextual calls. In this case,
1686 // the receiver cannot be a smi.
1687 if (object != holder) {
1688 __ test(edx, Immediate(kSmiTagMask));
1689 __ j(zero, &miss, not_taken);
1690 }
1691
1692 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001693 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001694
1695 // Get the value from the cell.
1696 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1697 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1698
1699 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001700 if (Heap::InNewSpace(function)) {
1701 // We can't embed a pointer to a function in new space so we have
1702 // to verify that the shared function info is unchanged. This has
1703 // the nice side effect that multiple closures based on the same
1704 // function can all use this call IC. Before we load through the
1705 // function, we have to verify that it still is a function.
1706 __ test(edi, Immediate(kSmiTagMask));
1707 __ j(zero, &miss, not_taken);
1708 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1709 __ j(not_equal, &miss, not_taken);
1710
1711 // Check the shared function info. Make sure it hasn't changed.
1712 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1713 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1714 __ j(not_equal, &miss, not_taken);
1715 } else {
1716 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1717 __ j(not_equal, &miss, not_taken);
1718 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001719
1720 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001721 if (object->IsGlobalObject()) {
1722 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1723 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1724 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001725
1726 // Setup the context (function already in edi).
1727 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1728
1729 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001730 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001731 ASSERT(function->is_compiled());
1732 Handle<Code> code(function->code());
1733 ParameterCount expected(function->shared()->formal_parameter_count());
1734 __ InvokeCode(code, expected, arguments(),
1735 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1736
1737 // Handle call cache miss.
1738 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001739 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1740 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1741 __ jmp(ic, RelocInfo::CODE_TARGET);
1742
1743 // Return the generated code.
1744 return GetCode(NORMAL, name);
1745}
1746
1747
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1749 int index,
1750 Map* transition,
1751 String* name) {
1752 // ----------- S t a t e -------------
1753 // -- eax : value
1754 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001755 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001758 Label miss;
1759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001761 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001762 object,
1763 index,
1764 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001765 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001766 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767
1768 // Handle store cache miss.
1769 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001770 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001771 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001772 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773
1774 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001775 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776}
1777
1778
1779Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1780 AccessorInfo* callback,
1781 String* name) {
1782 // ----------- S t a t e -------------
1783 // -- eax : value
1784 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001785 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 Label miss;
1789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001791 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 __ j(zero, &miss, not_taken);
1793
1794 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001795 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 Immediate(Handle<Map>(object->map())));
1797 __ j(not_equal, &miss, not_taken);
1798
1799 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001800 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001801 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 }
1803
1804 // Stub never generated for non-global objects that require access
1805 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001806 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001807
1808 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001809 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1811 __ push(ecx); // name
1812 __ push(eax); // value
1813 __ push(ebx); // restore return address
1814
mads.s.ager31e71382008-08-13 09:32:07 +00001815 // Do tail-call to the runtime system.
1816 ExternalReference store_callback_property =
1817 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001818 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001819
1820 // Handle store cache miss.
1821 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001823 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001824
1825 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001826 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827}
1828
1829
1830Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1831 String* name) {
1832 // ----------- S t a t e -------------
1833 // -- eax : value
1834 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001835 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001838 Label miss;
1839
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001841 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842 __ j(zero, &miss, not_taken);
1843
1844 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001845 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001846 Immediate(Handle<Map>(receiver->map())));
1847 __ j(not_equal, &miss, not_taken);
1848
1849 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001850 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001851 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 }
1853
1854 // Stub never generated for non-global objects that require access
1855 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001856 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857
1858 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001859 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ push(ecx); // name
1861 __ push(eax); // value
1862 __ push(ebx); // restore return address
1863
mads.s.ager31e71382008-08-13 09:32:07 +00001864 // Do tail-call to the runtime system.
1865 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001867 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868
1869 // Handle store cache miss.
1870 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001872 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001873
1874 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001875 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876}
1877
1878
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001879Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1880 JSGlobalPropertyCell* cell,
1881 String* name) {
1882 // ----------- S t a t e -------------
1883 // -- eax : value
1884 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001885 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001886 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001887 // -----------------------------------
1888 Label miss;
1889
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001890 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001891 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001892 Immediate(Handle<Map>(object->map())));
1893 __ j(not_equal, &miss, not_taken);
1894
1895 // Store the value in the cell.
1896 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1897 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1898
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001899 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001900 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001901 __ ret(0);
1902
1903 // Handle store cache miss.
1904 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001905 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1906 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1907 __ jmp(ic, RelocInfo::CODE_TARGET);
1908
1909 // Return the generated code.
1910 return GetCode(NORMAL, name);
1911}
1912
1913
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1915 int index,
1916 Map* transition,
1917 String* name) {
1918 // ----------- S t a t e -------------
1919 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001920 // -- ecx : key
1921 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924 Label miss;
1925
1926 __ IncrementCounter(&Counters::keyed_store_field, 1);
1927
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001928 // Check that the name has not changed.
1929 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1930 __ j(not_equal, &miss, not_taken);
1931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001933 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001934 object,
1935 index,
1936 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001937 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001938 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001939
1940 // Handle store cache miss.
1941 __ bind(&miss);
1942 __ DecrementCounter(&Counters::keyed_store_field, 1);
1943 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001944 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945
1946 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948}
1949
1950
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001951
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1953 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001954 int index,
1955 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001956 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001957 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 // -- ecx : name
1959 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 Label miss;
1962
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001963 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001964 __ bind(&miss);
1965 GenerateLoadMiss(masm(), Code::LOAD_IC);
1966
1967 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001968 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969}
1970
1971
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001972Object* LoadStubCompiler::CompileLoadCallback(String* name,
1973 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001975 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001977 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 // -- ecx : name
1979 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 Label miss;
1982
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001983 Failure* failure = Failure::InternalError();
1984 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1985 callback, name, &miss, &failure);
1986 if (!success) return failure;
1987
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001988 __ bind(&miss);
1989 GenerateLoadMiss(masm(), Code::LOAD_IC);
1990
1991 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001992 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993}
1994
1995
1996Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1997 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001998 Object* value,
1999 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002000 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002001 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002002 // -- ecx : name
2003 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002005 Label miss;
2006
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002007 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 __ bind(&miss);
2009 GenerateLoadMiss(masm(), Code::LOAD_IC);
2010
2011 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002012 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002013}
2014
2015
2016Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2017 JSObject* holder,
2018 String* name) {
2019 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002020 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 // -- ecx : name
2022 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 Label miss;
2025
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002026 LookupResult lookup;
2027 LookupPostInterceptor(holder, name, &lookup);
2028
ager@chromium.orge2902be2009-06-08 12:21:35 +00002029 // TODO(368): Compile in the whole chain: all the interceptors in
2030 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002031 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002032 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002033 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002034 eax,
2035 ecx,
2036 edx,
2037 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002038 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002039 &miss);
2040
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 __ bind(&miss);
2042 GenerateLoadMiss(masm(), Code::LOAD_IC);
2043
2044 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002045 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046}
2047
2048
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002049Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2050 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002051 JSGlobalPropertyCell* cell,
2052 String* name,
2053 bool is_dont_delete) {
2054 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002055 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002056 // -- ecx : name
2057 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002058 // -----------------------------------
2059 Label miss;
2060
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002061 // If the object is the holder then we know that it's a global
2062 // object which can only happen for contextual loads. In this case,
2063 // the receiver cannot be a smi.
2064 if (object != holder) {
2065 __ test(eax, Immediate(kSmiTagMask));
2066 __ j(zero, &miss, not_taken);
2067 }
2068
2069 // Check that the maps haven't changed.
2070 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002071
2072 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002073 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2074 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002075
2076 // Check for deleted property if property can actually be deleted.
2077 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002078 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002079 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002080 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002081 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002082 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002083 }
2084
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002085 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002086 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002087 __ ret(0);
2088
2089 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002090 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2091 GenerateLoadMiss(masm(), Code::LOAD_IC);
2092
2093 // Return the generated code.
2094 return GetCode(NORMAL, name);
2095}
2096
2097
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002098Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2099 JSObject* receiver,
2100 JSObject* holder,
2101 int index) {
2102 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002103 // -- eax : key
2104 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002107 Label miss;
2108
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109 __ IncrementCounter(&Counters::keyed_load_field, 1);
2110
2111 // Check that the name has not changed.
2112 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2113 __ j(not_equal, &miss, not_taken);
2114
ager@chromium.org5c838252010-02-19 08:53:10 +00002115 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 __ bind(&miss);
2118 __ DecrementCounter(&Counters::keyed_load_field, 1);
2119 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2120
2121 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123}
2124
2125
2126Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2127 JSObject* receiver,
2128 JSObject* holder,
2129 AccessorInfo* callback) {
2130 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002131 // -- eax : key
2132 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 Label miss;
2136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2138
2139 // Check that the name has not changed.
2140 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2141 __ j(not_equal, &miss, not_taken);
2142
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002143 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002144 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002145 callback, name, &miss, &failure);
2146 if (!success) return failure;
2147
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 __ bind(&miss);
2149 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2150 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2151
2152 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002153 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002154}
2155
2156
2157Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2158 JSObject* receiver,
2159 JSObject* holder,
2160 Object* value) {
2161 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002162 // -- eax : key
2163 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 Label miss;
2167
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002168 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2169
2170 // Check that the name has not changed.
2171 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2172 __ j(not_equal, &miss, not_taken);
2173
ager@chromium.org5c838252010-02-19 08:53:10 +00002174 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002175 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002176 __ bind(&miss);
2177 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2178 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2179
2180 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002181 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182}
2183
2184
2185Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2186 JSObject* holder,
2187 String* name) {
2188 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002189 // -- eax : key
2190 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 Label miss;
2194
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2196
2197 // Check that the name has not changed.
2198 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2199 __ j(not_equal, &miss, not_taken);
2200
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002201 LookupResult lookup;
2202 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002203 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002204 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002205 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002206 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002207 eax,
2208 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002209 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002210 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002211 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 __ bind(&miss);
2213 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2214 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2215
2216 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002217 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002218}
2219
2220
2221
2222
2223Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2224 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002225 // -- eax : key
2226 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002229 Label miss;
2230
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2232
2233 // Check that the name has not changed.
2234 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2235 __ j(not_equal, &miss, not_taken);
2236
ager@chromium.org5c838252010-02-19 08:53:10 +00002237 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 __ bind(&miss);
2239 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2240 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2241
2242 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002243 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244}
2245
2246
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002247Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002249 // -- eax : key
2250 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002253 Label miss;
2254
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2256
2257 // Check that the name has not changed.
2258 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2259 __ j(not_equal, &miss, not_taken);
2260
ager@chromium.org5c838252010-02-19 08:53:10 +00002261 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002262 __ bind(&miss);
2263 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2264 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2265
2266 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002267 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002268}
2269
2270
2271Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2272 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002273 // -- eax : key
2274 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002277 Label miss;
2278
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002279 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2280
2281 // Check that the name has not changed.
2282 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2283 __ j(not_equal, &miss, not_taken);
2284
ager@chromium.org5c838252010-02-19 08:53:10 +00002285 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002286 __ bind(&miss);
2287 __ DecrementCounter(&Counters::keyed_load_function_prototype, 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(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002292}
2293
2294
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002295// Specialized stub for constructing objects from functions which only have only
2296// simple assignments of the form this.x = ...; in their body.
2297Object* ConstructStubCompiler::CompileConstructStub(
2298 SharedFunctionInfo* shared) {
2299 // ----------- S t a t e -------------
2300 // -- eax : argc
2301 // -- edi : constructor
2302 // -- esp[0] : return address
2303 // -- esp[4] : last argument
2304 // -----------------------------------
2305 Label generic_stub_call;
2306#ifdef ENABLE_DEBUGGER_SUPPORT
2307 // Check to see whether there are any break points in the function code. If
2308 // there are jump to the generic constructor stub which calls the actual
2309 // code for the function thereby hitting the break points.
2310 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2311 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2312 __ cmp(ebx, Factory::undefined_value());
2313 __ j(not_equal, &generic_stub_call, not_taken);
2314#endif
2315
2316 // Load the initial map and verify that it is in fact a map.
2317 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2318 // Will both indicate a NULL and a Smi.
2319 __ test(ebx, Immediate(kSmiTagMask));
2320 __ j(zero, &generic_stub_call);
2321 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2322 __ j(not_equal, &generic_stub_call);
2323
2324#ifdef DEBUG
2325 // Cannot construct functions this way.
2326 // edi: constructor
2327 // ebx: initial map
2328 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2329 __ Assert(not_equal, "Function constructed by construct stub.");
2330#endif
2331
2332 // Now allocate the JSObject on the heap by moving the new space allocation
2333 // top forward.
2334 // edi: constructor
2335 // ebx: initial map
2336 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2337 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002338 __ AllocateInNewSpace(ecx,
2339 edx,
2340 ecx,
2341 no_reg,
2342 &generic_stub_call,
2343 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002344
2345 // Allocated the JSObject, now initialize the fields and add the heap tag.
2346 // ebx: initial map
2347 // edx: JSObject (untagged)
2348 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2349 __ mov(ebx, Factory::empty_fixed_array());
2350 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2351 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2352
2353 // Push the allocated object to the stack. This is the object that will be
2354 // returned (after it is tagged).
2355 __ push(edx);
2356
2357 // eax: argc
2358 // edx: JSObject (untagged)
2359 // Load the address of the first in-object property into edx.
2360 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2361 // Calculate the location of the first argument. The stack contains the
2362 // allocated object and the return address on top of the argc arguments.
2363 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2364
2365 // Use edi for holding undefined which is used in several places below.
2366 __ mov(edi, Factory::undefined_value());
2367
2368 // eax: argc
2369 // ecx: first argument
2370 // edx: first in-object property of the JSObject
2371 // edi: undefined
2372 // Fill the initialized properties with a constant value or a passed argument
2373 // depending on the this.x = ...; assignment in the function.
2374 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2375 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002376 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002377 // If argument is not passed the property is set to undefined,
2378 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002379 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002380 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002381 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002382 if (CpuFeatures::IsSupported(CMOV)) {
2383 CpuFeatures::Scope use_cmov(CMOV);
2384 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2385 } else {
2386 Label not_passed;
2387 __ j(below_equal, &not_passed);
2388 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2389 __ bind(&not_passed);
2390 }
2391 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002392 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002393 } else {
2394 // Set the property to the constant value.
2395 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2396 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2397 }
2398 }
2399
2400 // Fill the unused in-object property fields with undefined.
2401 for (int i = shared->this_property_assignments_count();
2402 i < shared->CalculateInObjectProperties();
2403 i++) {
2404 __ mov(Operand(edx, i * kPointerSize), edi);
2405 }
2406
2407 // Move argc to ebx and retrieve and tag the JSObject to return.
2408 __ mov(ebx, eax);
2409 __ pop(eax);
2410 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2411
2412 // Remove caller arguments and receiver from the stack and return.
2413 __ pop(ecx);
2414 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2415 __ push(ecx);
2416 __ IncrementCounter(&Counters::constructed_objects, 1);
2417 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2418 __ ret(0);
2419
2420 // Jump to the generic stub in case the specialized code cannot handle the
2421 // construction.
2422 __ bind(&generic_stub_call);
2423 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2424 Handle<Code> generic_construct_stub(code);
2425 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2426
2427 // Return the generated code.
2428 return GetCode();
2429}
2430
2431
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002432#undef __
2433
2434} } // namespace v8::internal