blob: 11c4ac73a01fef598e481b09f83ae75b10dbbbbc [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
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000953// Generate code to check that a global property cell is empty. Create
954// the property cell at compilation time if no cell exists for the
955// property.
956static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
957 GlobalObject* global,
958 String* name,
959 Register scratch,
960 Label* miss) {
961 Object* probe = global->EnsurePropertyCell(name);
962 if (probe->IsFailure()) return probe;
963 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
964 ASSERT(cell->value()->IsTheHole());
965 __ mov(scratch, Immediate(Handle<Object>(cell)));
966 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
967 Immediate(Factory::the_hole_value()));
968 __ j(not_equal, miss, not_taken);
969 return cell;
970}
971
972
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000974#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000975
976
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000977Register StubCompiler::CheckPrototypes(JSObject* object,
978 Register object_reg,
979 JSObject* holder,
980 Register holder_reg,
981 Register scratch,
982 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000983 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000984 Label* miss) {
985 // Check that the maps haven't changed.
986 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000987 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
988 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000989
990 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000991 // that their maps haven't changed. We also need to check that the
992 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000993 while (object != holder) {
994 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000995 Object* cell = GenerateCheckPropertyCell(masm(),
996 GlobalObject::cast(object),
997 name,
998 scratch,
999 miss);
1000 if (cell->IsFailure()) {
1001 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001002 return result;
1003 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001004 }
1005 object = JSObject::cast(object->GetPrototype());
1006 }
1007
ager@chromium.org5c838252010-02-19 08:53:10 +00001008 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001009 return result;
1010}
1011
1012
1013void StubCompiler::GenerateLoadField(JSObject* object,
1014 JSObject* holder,
1015 Register receiver,
1016 Register scratch1,
1017 Register scratch2,
1018 int index,
1019 String* name,
1020 Label* miss) {
1021 // Check that the receiver isn't a smi.
1022 __ test(receiver, Immediate(kSmiTagMask));
1023 __ j(zero, miss, not_taken);
1024
1025 // Check the prototype chain.
1026 Register reg =
1027 CheckPrototypes(object, receiver, holder,
1028 scratch1, scratch2, name, miss);
1029
1030 // Get the value from the properties.
1031 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1032 __ ret(0);
1033}
1034
1035
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001036bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001037 JSObject* holder,
1038 Register receiver,
1039 Register name_reg,
1040 Register scratch1,
1041 Register scratch2,
1042 AccessorInfo* callback,
1043 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001044 Label* miss,
1045 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001046 // Check that the receiver isn't a smi.
1047 __ test(receiver, Immediate(kSmiTagMask));
1048 __ j(zero, miss, not_taken);
1049
1050 // Check that the maps haven't changed.
1051 Register reg =
1052 CheckPrototypes(object, receiver, holder,
1053 scratch1, scratch2, name, miss);
1054
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001055 Handle<AccessorInfo> callback_handle(callback);
1056
1057 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1058 __ EnterInternalFrame();
1059 __ PushHandleScope(other);
1060 // Push the stack address where the list of arguments ends
1061 __ mov(other, esp);
1062 __ sub(Operand(other), Immediate(2 * kPointerSize));
1063 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001064 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001065 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001066 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001067 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001068 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001069 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001070 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001071 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001072 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001073 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001074
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001075 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001076 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001077 Address getter_address = v8::ToCData<Address>(callback->getter());
1078 ApiFunction fun(getter_address);
1079 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001080 // Emitting a stub call may try to allocate (if the code is not
1081 // already generated). Do not allow the assembler to perform a
1082 // garbage collection but instead return the allocation failure
1083 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001084 Object* result = masm()->TryCallStub(&stub);
1085 if (result->IsFailure()) {
1086 *failure = Failure::cast(result);
1087 return false;
1088 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001089
1090 // We need to avoid using eax since that now holds the result.
1091 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001092 // Emitting PopHandleScope may try to allocate. Do not allow the
1093 // assembler to perform a garbage collection but instead return a
1094 // failure object.
1095 result = masm()->TryPopHandleScope(eax, tmp);
1096 if (result->IsFailure()) {
1097 *failure = Failure::cast(result);
1098 return false;
1099 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001100 __ LeaveInternalFrame();
1101
1102 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001103 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001104}
1105
1106
1107void StubCompiler::GenerateLoadConstant(JSObject* object,
1108 JSObject* holder,
1109 Register receiver,
1110 Register scratch1,
1111 Register scratch2,
1112 Object* value,
1113 String* name,
1114 Label* miss) {
1115 // Check that the receiver isn't a smi.
1116 __ test(receiver, Immediate(kSmiTagMask));
1117 __ j(zero, miss, not_taken);
1118
1119 // Check that the maps haven't changed.
1120 Register reg =
1121 CheckPrototypes(object, receiver, holder,
1122 scratch1, scratch2, name, miss);
1123
1124 // Return the constant value.
1125 __ mov(eax, Handle<Object>(value));
1126 __ ret(0);
1127}
1128
1129
1130void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1131 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001132 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133 Register receiver,
1134 Register name_reg,
1135 Register scratch1,
1136 Register scratch2,
1137 String* name,
1138 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001139 LoadInterceptorCompiler compiler(name_reg);
1140 CompileLoadInterceptor(&compiler,
1141 this,
1142 masm(),
1143 object,
1144 holder,
1145 name,
1146 lookup,
1147 receiver,
1148 scratch1,
1149 scratch2,
1150 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001151}
1152
1153
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001154// TODO(1241006): Avoid having lazy compile stubs specialized by the
1155// number of arguments. It is not needed anymore.
1156Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001158 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159
1160 // Push a copy of the function onto the stack.
1161 __ push(edi);
1162
1163 __ push(edi); // function is also the parameter to the runtime call
1164 __ CallRuntime(Runtime::kLazyCompile, 1);
1165 __ pop(edi);
1166
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001167 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001168 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001169
1170 // Do a tail-call of the compiled function.
1171 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1172 __ jmp(Operand(ecx));
1173
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001174 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001175}
1176
1177
ager@chromium.org5c838252010-02-19 08:53:10 +00001178Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001179 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001180 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001181 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001182 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001183 // -- ecx : name
1184 // -- esp[0] : return address
1185 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1186 // -- ...
1187 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001188 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001189 Label miss;
1190
1191 // Get the receiver from the stack.
1192 const int argc = arguments().immediate();
1193 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1194
1195 // Check that the receiver isn't a smi.
1196 __ test(edx, Immediate(kSmiTagMask));
1197 __ j(zero, &miss, not_taken);
1198
1199 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001200 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
ager@chromium.org7c537e22008-10-16 08:43:32 +00001202 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001203
1204 // Check that the function really is a function.
1205 __ test(edi, Immediate(kSmiTagMask));
1206 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001207 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208 __ j(not_equal, &miss, not_taken);
1209
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001210 // Patch the receiver on the stack with the global proxy if
1211 // necessary.
1212 if (object->IsGlobalObject()) {
1213 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1214 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1215 }
1216
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001217 // Invoke the function.
1218 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1219
1220 // Handle call cache miss.
1221 __ bind(&miss);
1222 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001223 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224
1225 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001226 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227}
1228
1229
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001230Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1231 JSObject* holder,
1232 JSFunction* function,
1233 String* name,
1234 CheckType check) {
1235 // ----------- S t a t e -------------
1236 // -- ecx : name
1237 // -- esp[0] : return address
1238 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1239 // -- ...
1240 // -- esp[(argc + 1) * 4] : receiver
1241 // -----------------------------------
1242 ASSERT(check == RECEIVER_MAP_CHECK);
1243
1244 Label miss;
1245
1246 // Get the receiver from the stack.
1247 const int argc = arguments().immediate();
1248 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1249
1250 // Check that the receiver isn't a smi.
1251 __ test(edx, Immediate(kSmiTagMask));
1252 __ j(zero, &miss);
1253
1254 CheckPrototypes(JSObject::cast(object), edx,
1255 holder, ebx,
1256 eax, name, &miss);
1257
1258 if (argc == 0) {
1259 // Noop, return the length.
1260 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1261 __ ret((argc + 1) * kPointerSize);
1262 } else {
1263 // Get the elements array of the object.
1264 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1265
1266 // Check that the elements are in fast mode (not dictionary).
1267 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1268 Immediate(Factory::fixed_array_map()));
1269 __ j(not_equal, &miss);
1270
1271 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001272 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001273
1274 // Get the array's length into eax and calculate new length.
1275 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1276 STATIC_ASSERT(kSmiTagSize == 1);
1277 STATIC_ASSERT(kSmiTag == 0);
1278 __ add(Operand(eax), Immediate(argc << 1));
1279
1280 // Get the element's length into ecx.
1281 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1282 __ SmiTag(ecx);
1283
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001284 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001285 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001286 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001287
1288 // Save new length.
1289 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1290
1291 // Push the element.
1292 __ lea(edx, FieldOperand(ebx,
1293 eax, times_half_pointer_size,
1294 FixedArray::kHeaderSize - argc * kPointerSize));
1295 __ mov(ecx, Operand(esp, argc * kPointerSize));
1296 __ mov(Operand(edx, 0), ecx);
1297
1298 // Check if value is a smi.
1299 __ test(ecx, Immediate(kSmiTagMask));
1300 __ j(not_zero, &with_rset_update);
1301
1302 __ bind(&exit);
1303 __ ret((argc + 1) * kPointerSize);
1304
1305 __ bind(&with_rset_update);
1306
1307 __ InNewSpace(ebx, ecx, equal, &exit);
1308
1309 RecordWriteStub stub(ebx, edx, ecx);
1310 __ CallStub(&stub);
1311 __ ret((argc + 1) * kPointerSize);
1312
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001313 __ bind(&attempt_to_grow_elements);
1314 ExternalReference new_space_allocation_top =
1315 ExternalReference::new_space_allocation_top_address();
1316 ExternalReference new_space_allocation_limit =
1317 ExternalReference::new_space_allocation_limit_address();
1318
1319 const int kAllocationDelta = 4;
1320 // Load top.
1321 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1322
1323 // Check if it's the end of elements.
1324 __ lea(edx, FieldOperand(ebx,
1325 eax, times_half_pointer_size,
1326 FixedArray::kHeaderSize - argc * kPointerSize));
1327 __ cmp(edx, Operand(ecx));
1328 __ j(not_equal, &call_builtin);
1329 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1330 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
1331 __ j(greater, &call_builtin);
1332
1333 // We fit and could grow elements.
1334 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1335 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001336
1337 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001338 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001339 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001340 for (int i = 1; i < kAllocationDelta; i++) {
1341 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001342 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001343 }
1344
1345 // Restore receiver to edx as finish sequence assumes it's here.
1346 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1347
1348 // Increment element's and array's sizes.
1349 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1350 Immediate(kAllocationDelta));
1351 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1352
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001353 // Elements are in new space, so no remembered set updates are necessary.
1354 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001355
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001356 __ bind(&call_builtin);
1357 }
1358
1359 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1360 argc + 1,
1361 1);
1362 }
1363
1364 __ bind(&miss);
1365
1366 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1367 __ jmp(ic, RelocInfo::CODE_TARGET);
1368
1369 // Return the generated code.
1370 String* function_name = NULL;
1371 if (function->shared()->name()->IsString()) {
1372 function_name = String::cast(function->shared()->name());
1373 }
1374 return GetCode(CONSTANT_FUNCTION, function_name);
1375}
1376
1377
1378Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1379 JSObject* holder,
1380 JSFunction* function,
1381 String* name,
1382 CheckType check) {
1383 // ----------- S t a t e -------------
1384 // -- ecx : name
1385 // -- esp[0] : return address
1386 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1387 // -- ...
1388 // -- esp[(argc + 1) * 4] : receiver
1389 // -----------------------------------
1390 ASSERT(check == RECEIVER_MAP_CHECK);
1391
1392 Label miss, empty_array, call_builtin;
1393
1394 // Get the receiver from the stack.
1395 const int argc = arguments().immediate();
1396 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1397
1398 // Check that the receiver isn't a smi.
1399 __ test(edx, Immediate(kSmiTagMask));
1400 __ j(zero, &miss);
1401
1402 CheckPrototypes(JSObject::cast(object), edx,
1403 holder, ebx,
1404 eax, name, &miss);
1405
1406 // Get the elements array of the object.
1407 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1408
1409 // Check that the elements are in fast mode (not dictionary).
1410 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1411 Immediate(Factory::fixed_array_map()));
1412 __ j(not_equal, &miss);
1413
1414 // Get the array's length into ecx and calculate new length.
1415 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1416 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1417 __ j(negative, &empty_array);
1418
1419 // Get the last element.
1420 STATIC_ASSERT(kSmiTagSize == 1);
1421 STATIC_ASSERT(kSmiTag == 0);
1422 __ mov(eax, FieldOperand(ebx,
1423 ecx, times_half_pointer_size,
1424 FixedArray::kHeaderSize));
1425 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1426 __ j(equal, &call_builtin);
1427
1428 // Set the array's length.
1429 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1430
1431 // Fill with the hole.
1432 __ mov(FieldOperand(ebx,
1433 ecx, times_half_pointer_size,
1434 FixedArray::kHeaderSize),
1435 Immediate(Factory::the_hole_value()));
1436 __ ret((argc + 1) * kPointerSize);
1437
1438 __ bind(&empty_array);
1439 __ mov(eax, Immediate(Factory::undefined_value()));
1440 __ ret((argc + 1) * kPointerSize);
1441
1442 __ bind(&call_builtin);
1443
1444 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1445 argc + 1,
1446 1);
1447
1448 __ bind(&miss);
1449
1450 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1451 __ jmp(ic, RelocInfo::CODE_TARGET);
1452
1453 // Return the generated code.
1454 String* function_name = NULL;
1455 if (function->shared()->name()->IsString()) {
1456 function_name = String::cast(function->shared()->name());
1457 }
1458 return GetCode(CONSTANT_FUNCTION, function_name);
1459}
1460
1461
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462Object* CallStubCompiler::CompileCallConstant(Object* object,
1463 JSObject* holder,
1464 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001465 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001466 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001468 // -- ecx : name
1469 // -- esp[0] : return address
1470 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1471 // -- ...
1472 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001474
1475 SharedFunctionInfo* function_info = function->shared();
1476 if (function_info->HasCustomCallGenerator()) {
1477 CustomCallGenerator generator =
1478 ToCData<CustomCallGenerator>(function_info->function_data());
1479 return generator(this, object, holder, function, name, check);
1480 }
1481
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001482 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483
1484 // Get the receiver from the stack.
1485 const int argc = arguments().immediate();
1486 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1487
1488 // Check that the receiver isn't a smi.
1489 if (check != NUMBER_CHECK) {
1490 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001491 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492 }
1493
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001494 // Make sure that it's okay not to patch the on stack receiver
1495 // unless we're doing a receiver map check.
1496 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1497
ager@chromium.org5c838252010-02-19 08:53:10 +00001498 CallOptimization optimization(function);
1499 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001500 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001501
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001502 switch (check) {
1503 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001504 __ IncrementCounter(&Counters::call_const, 1);
1505
1506 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1507 depth = optimization.GetPrototypeDepthOfExpectedType(
1508 JSObject::cast(object), holder);
1509 }
1510
1511 if (depth != kInvalidProtoDepth) {
1512 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1513 ReserveSpaceForFastApiCall(masm(), eax);
1514 }
1515
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001517 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001518 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001519
1520 // Patch the receiver on the stack with the global proxy if
1521 // necessary.
1522 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001523 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001524 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1525 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1526 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 break;
1528
1529 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530 if (!function->IsBuiltin()) {
1531 // Calling non-builtins with a value as receiver requires boxing.
1532 __ jmp(&miss);
1533 } else {
1534 // Check that the object is a string or a symbol.
1535 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1536 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1537 __ cmp(eax, FIRST_NONSTRING_TYPE);
1538 __ j(above_equal, &miss, not_taken);
1539 // Check that the maps starting from the prototype haven't changed.
1540 GenerateLoadGlobalFunctionPrototype(masm(),
1541 Context::STRING_FUNCTION_INDEX,
1542 eax);
1543 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1544 ebx, edx, name, &miss);
1545 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546 break;
1547
1548 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001549 if (!function->IsBuiltin()) {
1550 // Calling non-builtins with a value as receiver requires boxing.
1551 __ jmp(&miss);
1552 } else {
1553 Label fast;
1554 // Check that the object is a smi or a heap number.
1555 __ test(edx, Immediate(kSmiTagMask));
1556 __ j(zero, &fast, taken);
1557 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1558 __ j(not_equal, &miss, not_taken);
1559 __ bind(&fast);
1560 // Check that the maps starting from the prototype haven't changed.
1561 GenerateLoadGlobalFunctionPrototype(masm(),
1562 Context::NUMBER_FUNCTION_INDEX,
1563 eax);
1564 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1565 ebx, edx, name, &miss);
1566 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001567 break;
1568 }
1569
1570 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001571 if (!function->IsBuiltin()) {
1572 // Calling non-builtins with a value as receiver requires boxing.
1573 __ jmp(&miss);
1574 } else {
1575 Label fast;
1576 // Check that the object is a boolean.
1577 __ cmp(edx, Factory::true_value());
1578 __ j(equal, &fast, taken);
1579 __ cmp(edx, Factory::false_value());
1580 __ j(not_equal, &miss, not_taken);
1581 __ bind(&fast);
1582 // Check that the maps starting from the prototype haven't changed.
1583 GenerateLoadGlobalFunctionPrototype(masm(),
1584 Context::BOOLEAN_FUNCTION_INDEX,
1585 eax);
1586 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1587 ebx, edx, name, &miss);
1588 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 break;
1590 }
1591
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592 default:
1593 UNREACHABLE();
1594 }
1595
ager@chromium.org5c838252010-02-19 08:53:10 +00001596 if (depth != kInvalidProtoDepth) {
1597 GenerateFastApiCall(masm(), optimization, argc);
1598 } else {
1599 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601
1602 // Handle call cache miss.
1603 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001604 if (depth != kInvalidProtoDepth) {
1605 FreeSpaceForFastApiCall(masm(), eax);
1606 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001607 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001609 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610
1611 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001612 String* function_name = NULL;
1613 if (function->shared()->name()->IsString()) {
1614 function_name = String::cast(function->shared()->name());
1615 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001616 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001617}
1618
1619
ager@chromium.org5c838252010-02-19 08:53:10 +00001620Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001621 JSObject* holder,
1622 String* name) {
1623 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001624 // -- ecx : name
1625 // -- esp[0] : return address
1626 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1627 // -- ...
1628 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001630 Label miss;
1631
1632 // Get the number of arguments.
1633 const int argc = arguments().immediate();
1634
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001635 LookupResult lookup;
1636 LookupPostInterceptor(holder, name, &lookup);
1637
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638 // Get the receiver from the stack.
1639 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001640
ager@chromium.org5c838252010-02-19 08:53:10 +00001641 CallInterceptorCompiler compiler(this, arguments(), ecx);
1642 compiler.Compile(masm(),
1643 object,
1644 holder,
1645 name,
1646 &lookup,
1647 edx,
1648 ebx,
1649 edi,
1650 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001652 // Restore receiver.
1653 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654
1655 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001656 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001658 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001659 __ j(not_equal, &miss, not_taken);
1660
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001661 // Patch the receiver on the stack with the global proxy if
1662 // necessary.
1663 if (object->IsGlobalObject()) {
1664 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1665 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1666 }
1667
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001668 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001669 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1671
1672 // Handle load cache miss.
1673 __ bind(&miss);
1674 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001675 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001676
1677 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001678 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001679}
1680
1681
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001682Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1683 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001684 JSGlobalPropertyCell* cell,
1685 JSFunction* function,
1686 String* name) {
1687 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001688 // -- ecx : name
1689 // -- esp[0] : return address
1690 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1691 // -- ...
1692 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001693 // -----------------------------------
1694 Label miss;
1695
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001696 // Get the number of arguments.
1697 const int argc = arguments().immediate();
1698
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001699 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001700 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001701
1702 // If the object is the holder then we know that it's a global
1703 // object which can only happen for contextual calls. In this case,
1704 // the receiver cannot be a smi.
1705 if (object != holder) {
1706 __ test(edx, Immediate(kSmiTagMask));
1707 __ j(zero, &miss, not_taken);
1708 }
1709
1710 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001711 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001712
1713 // Get the value from the cell.
1714 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1715 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1716
1717 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001718 if (Heap::InNewSpace(function)) {
1719 // We can't embed a pointer to a function in new space so we have
1720 // to verify that the shared function info is unchanged. This has
1721 // the nice side effect that multiple closures based on the same
1722 // function can all use this call IC. Before we load through the
1723 // function, we have to verify that it still is a function.
1724 __ test(edi, Immediate(kSmiTagMask));
1725 __ j(zero, &miss, not_taken);
1726 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1727 __ j(not_equal, &miss, not_taken);
1728
1729 // Check the shared function info. Make sure it hasn't changed.
1730 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1731 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1732 __ j(not_equal, &miss, not_taken);
1733 } else {
1734 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1735 __ j(not_equal, &miss, not_taken);
1736 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001737
1738 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001739 if (object->IsGlobalObject()) {
1740 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1741 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1742 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001743
1744 // Setup the context (function already in edi).
1745 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1746
1747 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001748 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001749 ASSERT(function->is_compiled());
1750 Handle<Code> code(function->code());
1751 ParameterCount expected(function->shared()->formal_parameter_count());
1752 __ InvokeCode(code, expected, arguments(),
1753 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1754
1755 // Handle call cache miss.
1756 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001757 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1758 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1759 __ jmp(ic, RelocInfo::CODE_TARGET);
1760
1761 // Return the generated code.
1762 return GetCode(NORMAL, name);
1763}
1764
1765
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1767 int index,
1768 Map* transition,
1769 String* name) {
1770 // ----------- S t a t e -------------
1771 // -- eax : value
1772 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001773 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001774 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776 Label miss;
1777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001779 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001780 object,
1781 index,
1782 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001783 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001784 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785
1786 // Handle store cache miss.
1787 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001788 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001790 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791
1792 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001793 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794}
1795
1796
1797Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1798 AccessorInfo* callback,
1799 String* name) {
1800 // ----------- S t a t e -------------
1801 // -- eax : value
1802 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001803 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001804 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001805 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806 Label miss;
1807
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001808 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001809 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001810 __ j(zero, &miss, not_taken);
1811
1812 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001813 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814 Immediate(Handle<Map>(object->map())));
1815 __ j(not_equal, &miss, not_taken);
1816
1817 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001818 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001819 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 }
1821
1822 // Stub never generated for non-global objects that require access
1823 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001824 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825
1826 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001827 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1829 __ push(ecx); // name
1830 __ push(eax); // value
1831 __ push(ebx); // restore return address
1832
mads.s.ager31e71382008-08-13 09:32:07 +00001833 // Do tail-call to the runtime system.
1834 ExternalReference store_callback_property =
1835 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001836 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837
1838 // Handle store cache miss.
1839 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001841 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001842
1843 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001844 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845}
1846
1847
1848Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1849 String* name) {
1850 // ----------- S t a t e -------------
1851 // -- eax : value
1852 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001853 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856 Label miss;
1857
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001859 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001860 __ j(zero, &miss, not_taken);
1861
1862 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001863 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001864 Immediate(Handle<Map>(receiver->map())));
1865 __ j(not_equal, &miss, not_taken);
1866
1867 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001868 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001869 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 }
1871
1872 // Stub never generated for non-global objects that require access
1873 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001874 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875
1876 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001877 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001878 __ push(ecx); // name
1879 __ push(eax); // value
1880 __ push(ebx); // restore return address
1881
mads.s.ager31e71382008-08-13 09:32:07 +00001882 // Do tail-call to the runtime system.
1883 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001885 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886
1887 // Handle store cache miss.
1888 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001890 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001891
1892 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001893 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001894}
1895
1896
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001897Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1898 JSGlobalPropertyCell* cell,
1899 String* name) {
1900 // ----------- S t a t e -------------
1901 // -- eax : value
1902 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001903 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001904 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001905 // -----------------------------------
1906 Label miss;
1907
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001908 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001909 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001910 Immediate(Handle<Map>(object->map())));
1911 __ j(not_equal, &miss, not_taken);
1912
1913 // Store the value in the cell.
1914 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1915 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1916
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001917 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001918 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001919 __ ret(0);
1920
1921 // Handle store cache miss.
1922 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001923 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1924 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1925 __ jmp(ic, RelocInfo::CODE_TARGET);
1926
1927 // Return the generated code.
1928 return GetCode(NORMAL, name);
1929}
1930
1931
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001932Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1933 int index,
1934 Map* transition,
1935 String* name) {
1936 // ----------- S t a t e -------------
1937 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001938 // -- ecx : key
1939 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001940 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001941 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001942 Label miss;
1943
1944 __ IncrementCounter(&Counters::keyed_store_field, 1);
1945
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001946 // Check that the name has not changed.
1947 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1948 __ j(not_equal, &miss, not_taken);
1949
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001951 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001952 object,
1953 index,
1954 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001955 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001956 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957
1958 // Handle store cache miss.
1959 __ bind(&miss);
1960 __ DecrementCounter(&Counters::keyed_store_field, 1);
1961 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001962 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963
1964 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001965 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001966}
1967
1968
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001969Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1970 JSObject* object,
1971 JSObject* last) {
1972 // ----------- S t a t e -------------
1973 // -- eax : receiver
1974 // -- ecx : name
1975 // -- esp[0] : return address
1976 // -----------------------------------
1977 Label miss;
1978
1979 // Check that the receiver isn't a smi.
1980 __ test(eax, Immediate(kSmiTagMask));
1981 __ j(zero, &miss, not_taken);
1982
1983 // Check the maps of the full prototype chain. Also check that
1984 // global property cells up to (but not including) the last object
1985 // in the prototype chain are empty.
1986 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1987
1988 // If the last object in the prototype chain is a global object,
1989 // check that the global property cell is empty.
1990 if (last->IsGlobalObject()) {
1991 Object* cell = GenerateCheckPropertyCell(masm(),
1992 GlobalObject::cast(last),
1993 name,
1994 edx,
1995 &miss);
1996 if (cell->IsFailure()) return cell;
1997 }
1998
1999 // Return undefined if maps of the full prototype chain are still the
2000 // same and no global property with this name contains a value.
2001 __ mov(eax, Factory::undefined_value());
2002 __ ret(0);
2003
2004 __ bind(&miss);
2005 GenerateLoadMiss(masm(), Code::LOAD_IC);
2006
2007 // Return the generated code.
2008 return GetCode(NONEXISTENT, Heap::empty_string());
2009}
2010
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002011
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2013 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002014 int index,
2015 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002016 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002017 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002018 // -- ecx : name
2019 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002021 Label miss;
2022
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002023 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024 __ bind(&miss);
2025 GenerateLoadMiss(masm(), Code::LOAD_IC);
2026
2027 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002028 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002029}
2030
2031
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002032Object* LoadStubCompiler::CompileLoadCallback(String* name,
2033 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002035 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002037 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002038 // -- ecx : name
2039 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 Label miss;
2042
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002043 Failure* failure = Failure::InternalError();
2044 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2045 callback, name, &miss, &failure);
2046 if (!success) return failure;
2047
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 __ bind(&miss);
2049 GenerateLoadMiss(masm(), Code::LOAD_IC);
2050
2051 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002052 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053}
2054
2055
2056Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2057 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002058 Object* value,
2059 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002061 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 // -- ecx : name
2063 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 Label miss;
2066
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002067 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002068 __ bind(&miss);
2069 GenerateLoadMiss(masm(), Code::LOAD_IC);
2070
2071 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002072 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002073}
2074
2075
2076Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2077 JSObject* holder,
2078 String* name) {
2079 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002080 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002081 // -- ecx : name
2082 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002083 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002084 Label miss;
2085
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002086 LookupResult lookup;
2087 LookupPostInterceptor(holder, name, &lookup);
2088
ager@chromium.orge2902be2009-06-08 12:21:35 +00002089 // TODO(368): Compile in the whole chain: all the interceptors in
2090 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002091 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002092 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002093 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002094 eax,
2095 ecx,
2096 edx,
2097 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002098 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002099 &miss);
2100
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002101 __ bind(&miss);
2102 GenerateLoadMiss(masm(), Code::LOAD_IC);
2103
2104 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002105 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106}
2107
2108
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002109Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2110 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002111 JSGlobalPropertyCell* cell,
2112 String* name,
2113 bool is_dont_delete) {
2114 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002115 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002116 // -- ecx : name
2117 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002118 // -----------------------------------
2119 Label miss;
2120
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002121 // If the object is the holder then we know that it's a global
2122 // object which can only happen for contextual loads. In this case,
2123 // the receiver cannot be a smi.
2124 if (object != holder) {
2125 __ test(eax, Immediate(kSmiTagMask));
2126 __ j(zero, &miss, not_taken);
2127 }
2128
2129 // Check that the maps haven't changed.
2130 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002131
2132 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002133 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2134 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002135
2136 // Check for deleted property if property can actually be deleted.
2137 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002138 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002139 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002140 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002141 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002142 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002143 }
2144
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002145 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002147 __ ret(0);
2148
2149 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002150 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2151 GenerateLoadMiss(masm(), Code::LOAD_IC);
2152
2153 // Return the generated code.
2154 return GetCode(NORMAL, name);
2155}
2156
2157
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002158Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2159 JSObject* receiver,
2160 JSObject* holder,
2161 int index) {
2162 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002163 // -- eax : key
2164 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002166 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 Label miss;
2168
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 __ IncrementCounter(&Counters::keyed_load_field, 1);
2170
2171 // Check that the name has not changed.
2172 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2173 __ j(not_equal, &miss, not_taken);
2174
ager@chromium.org5c838252010-02-19 08:53:10 +00002175 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002176
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 __ bind(&miss);
2178 __ DecrementCounter(&Counters::keyed_load_field, 1);
2179 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2180
2181 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002182 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183}
2184
2185
2186Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2187 JSObject* receiver,
2188 JSObject* holder,
2189 AccessorInfo* callback) {
2190 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002191 // -- eax : key
2192 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 Label miss;
2196
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2198
2199 // Check that the name has not changed.
2200 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2201 __ j(not_equal, &miss, not_taken);
2202
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002203 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002204 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002205 callback, name, &miss, &failure);
2206 if (!success) return failure;
2207
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 __ bind(&miss);
2209 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2210 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2211
2212 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002213 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002214}
2215
2216
2217Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2218 JSObject* receiver,
2219 JSObject* holder,
2220 Object* value) {
2221 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002222 // -- eax : key
2223 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 Label miss;
2227
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2229
2230 // Check that the name has not changed.
2231 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2232 __ j(not_equal, &miss, not_taken);
2233
ager@chromium.org5c838252010-02-19 08:53:10 +00002234 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002235 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 __ bind(&miss);
2237 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2238 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2239
2240 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002241 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002242}
2243
2244
2245Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2246 JSObject* holder,
2247 String* name) {
2248 // ----------- 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_interceptor, 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
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002261 LookupResult lookup;
2262 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002263 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002264 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002265 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002266 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002267 eax,
2268 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002269 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002270 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002271 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002272 __ bind(&miss);
2273 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2274 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2275
2276 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002277 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278}
2279
2280
2281
2282
2283Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2284 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002285 // -- eax : key
2286 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002287 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002288 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002289 Label miss;
2290
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2292
2293 // Check that the name has not changed.
2294 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2295 __ j(not_equal, &miss, not_taken);
2296
ager@chromium.org5c838252010-02-19 08:53:10 +00002297 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002298 __ bind(&miss);
2299 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2300 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2301
2302 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002303 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002304}
2305
2306
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002307Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002308 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002309 // -- eax : key
2310 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002311 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002312 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002313 Label miss;
2314
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002315 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2316
2317 // Check that the name has not changed.
2318 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2319 __ j(not_equal, &miss, not_taken);
2320
ager@chromium.org5c838252010-02-19 08:53:10 +00002321 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002322 __ bind(&miss);
2323 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2324 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2325
2326 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002327 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002328}
2329
2330
2331Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2332 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002333 // -- eax : key
2334 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002335 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002336 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002337 Label miss;
2338
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002339 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2340
2341 // Check that the name has not changed.
2342 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2343 __ j(not_equal, &miss, not_taken);
2344
ager@chromium.org5c838252010-02-19 08:53:10 +00002345 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002346 __ bind(&miss);
2347 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2348 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2349
2350 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002351 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002352}
2353
2354
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002355// Specialized stub for constructing objects from functions which only have only
2356// simple assignments of the form this.x = ...; in their body.
2357Object* ConstructStubCompiler::CompileConstructStub(
2358 SharedFunctionInfo* shared) {
2359 // ----------- S t a t e -------------
2360 // -- eax : argc
2361 // -- edi : constructor
2362 // -- esp[0] : return address
2363 // -- esp[4] : last argument
2364 // -----------------------------------
2365 Label generic_stub_call;
2366#ifdef ENABLE_DEBUGGER_SUPPORT
2367 // Check to see whether there are any break points in the function code. If
2368 // there are jump to the generic constructor stub which calls the actual
2369 // code for the function thereby hitting the break points.
2370 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2371 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2372 __ cmp(ebx, Factory::undefined_value());
2373 __ j(not_equal, &generic_stub_call, not_taken);
2374#endif
2375
2376 // Load the initial map and verify that it is in fact a map.
2377 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2378 // Will both indicate a NULL and a Smi.
2379 __ test(ebx, Immediate(kSmiTagMask));
2380 __ j(zero, &generic_stub_call);
2381 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2382 __ j(not_equal, &generic_stub_call);
2383
2384#ifdef DEBUG
2385 // Cannot construct functions this way.
2386 // edi: constructor
2387 // ebx: initial map
2388 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2389 __ Assert(not_equal, "Function constructed by construct stub.");
2390#endif
2391
2392 // Now allocate the JSObject on the heap by moving the new space allocation
2393 // top forward.
2394 // edi: constructor
2395 // ebx: initial map
2396 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2397 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002398 __ AllocateInNewSpace(ecx,
2399 edx,
2400 ecx,
2401 no_reg,
2402 &generic_stub_call,
2403 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002404
2405 // Allocated the JSObject, now initialize the fields and add the heap tag.
2406 // ebx: initial map
2407 // edx: JSObject (untagged)
2408 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2409 __ mov(ebx, Factory::empty_fixed_array());
2410 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2411 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2412
2413 // Push the allocated object to the stack. This is the object that will be
2414 // returned (after it is tagged).
2415 __ push(edx);
2416
2417 // eax: argc
2418 // edx: JSObject (untagged)
2419 // Load the address of the first in-object property into edx.
2420 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2421 // Calculate the location of the first argument. The stack contains the
2422 // allocated object and the return address on top of the argc arguments.
2423 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2424
2425 // Use edi for holding undefined which is used in several places below.
2426 __ mov(edi, Factory::undefined_value());
2427
2428 // eax: argc
2429 // ecx: first argument
2430 // edx: first in-object property of the JSObject
2431 // edi: undefined
2432 // Fill the initialized properties with a constant value or a passed argument
2433 // depending on the this.x = ...; assignment in the function.
2434 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2435 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002436 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002437 // If argument is not passed the property is set to undefined,
2438 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002439 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002440 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002441 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002442 if (CpuFeatures::IsSupported(CMOV)) {
2443 CpuFeatures::Scope use_cmov(CMOV);
2444 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2445 } else {
2446 Label not_passed;
2447 __ j(below_equal, &not_passed);
2448 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2449 __ bind(&not_passed);
2450 }
2451 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002452 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002453 } else {
2454 // Set the property to the constant value.
2455 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2456 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2457 }
2458 }
2459
2460 // Fill the unused in-object property fields with undefined.
2461 for (int i = shared->this_property_assignments_count();
2462 i < shared->CalculateInObjectProperties();
2463 i++) {
2464 __ mov(Operand(edx, i * kPointerSize), edi);
2465 }
2466
2467 // Move argc to ebx and retrieve and tag the JSObject to return.
2468 __ mov(ebx, eax);
2469 __ pop(eax);
2470 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2471
2472 // Remove caller arguments and receiver from the stack and return.
2473 __ pop(ecx);
2474 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2475 __ push(ecx);
2476 __ IncrementCounter(&Counters::constructed_objects, 1);
2477 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2478 __ ret(0);
2479
2480 // Jump to the generic stub in case the specialized code cannot handle the
2481 // construction.
2482 __ bind(&generic_stub_call);
2483 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2484 Handle<Code> generic_construct_stub(code);
2485 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2486
2487 // Return the generated code.
2488 return GetCode();
2489}
2490
2491
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002492#undef __
2493
2494} } // namespace v8::internal