blob: 029e394c643af12afe0d44ffc1b5acb18ce47f03 [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));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000225
226 // Check if the object is a JSValue wrapper.
227 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000228 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000229 __ j(not_equal, miss, not_taken);
230
231 // Check if the wrapped value is a string and load the length
232 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000233 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
234 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
235 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000236 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237}
238
239
240void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
241 Register receiver,
242 Register scratch1,
243 Register scratch2,
244 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000245 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000248}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250
251// Load a fast property out of a holder object (src). In-object properties
252// are loaded directly otherwise the property is loaded from the properties
253// fixed array.
254void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000255 Register dst, Register src,
256 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000257 // Adjust for the number of properties stored in the holder.
258 index -= holder->map()->inobject_properties();
259 if (index < 0) {
260 // Get the property straight out of the holder.
261 int offset = holder->map()->instance_size() + (index * kPointerSize);
262 __ mov(dst, FieldOperand(src, offset));
263 } else {
264 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000265 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000266 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
267 __ mov(dst, FieldOperand(dst, offset));
268 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269}
270
271
ager@chromium.org5c838252010-02-19 08:53:10 +0000272static void PushInterceptorArguments(MacroAssembler* masm,
273 Register receiver,
274 Register holder,
275 Register name,
276 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000277 __ push(name);
278 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
279 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000280 Register scratch = name;
281 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
282 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000283 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000284 __ push(holder);
285 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000286}
287
288
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000289static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
290 Register receiver,
291 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000292 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000293 JSObject* holder_obj) {
294 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 __ CallExternalReference(
296 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
297 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000298}
299
300
301template <class Compiler>
302static void CompileLoadInterceptor(Compiler* compiler,
303 StubCompiler* stub_compiler,
304 MacroAssembler* masm,
305 JSObject* object,
306 JSObject* holder,
307 String* name,
308 LookupResult* lookup,
309 Register receiver,
310 Register scratch1,
311 Register scratch2,
312 Label* miss) {
313 ASSERT(holder->HasNamedInterceptor());
314 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
315
316 // Check that the receiver isn't a smi.
317 __ test(receiver, Immediate(kSmiTagMask));
318 __ j(zero, miss, not_taken);
319
320 // Check that the maps haven't changed.
321 Register reg =
322 stub_compiler->CheckPrototypes(object, receiver, holder,
323 scratch1, scratch2, name, miss);
324
ager@chromium.org5c838252010-02-19 08:53:10 +0000325 if (lookup->IsProperty() && lookup->IsCacheable()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000326 compiler->CompileCacheable(masm,
327 stub_compiler,
328 receiver,
329 reg,
330 scratch1,
331 scratch2,
332 holder,
333 lookup,
334 name,
335 miss);
336 } else {
337 compiler->CompileRegular(masm,
338 receiver,
339 reg,
340 scratch2,
341 holder,
342 miss);
343 }
344}
345
346
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000347class LoadInterceptorCompiler BASE_EMBEDDED {
348 public:
349 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
350
351 void CompileCacheable(MacroAssembler* masm,
352 StubCompiler* stub_compiler,
353 Register receiver,
354 Register holder,
355 Register scratch1,
356 Register scratch2,
357 JSObject* holder_obj,
358 LookupResult* lookup,
359 String* name,
360 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000361 AccessorInfo* callback = NULL;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000362 bool optimize = false;
363 // So far the most popular follow ups for interceptor loads are FIELD
364 // and CALLBACKS, so inline only them, other cases may be added
365 // later.
366 if (lookup->type() == FIELD) {
367 optimize = true;
368 } else if (lookup->type() == CALLBACKS) {
369 Object* callback_object = lookup->GetCallbackObject();
370 if (callback_object->IsAccessorInfo()) {
371 callback = AccessorInfo::cast(callback_object);
372 optimize = callback->getter() != NULL;
373 }
374 }
375
376 if (!optimize) {
377 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
378 return;
379 }
380
381 // Note: starting a frame here makes GC aware of pointers pushed below.
382 __ EnterInternalFrame();
383
384 if (lookup->type() == CALLBACKS) {
385 __ push(receiver);
386 }
387 __ push(holder);
388 __ push(name_);
389
390 CompileCallLoadPropertyWithInterceptor(masm,
391 receiver,
392 holder,
393 name_,
394 holder_obj);
395
396 Label interceptor_failed;
397 __ cmp(eax, Factory::no_interceptor_result_sentinel());
398 __ j(equal, &interceptor_failed);
399 __ LeaveInternalFrame();
400 __ ret(0);
401
402 __ bind(&interceptor_failed);
403 __ pop(name_);
404 __ pop(holder);
405 if (lookup->type() == CALLBACKS) {
406 __ pop(receiver);
407 }
408
409 __ LeaveInternalFrame();
410
411 if (lookup->type() == FIELD) {
412 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
413 lookup->holder(), scratch1,
414 scratch2,
415 name,
416 miss_label);
417 stub_compiler->GenerateFastPropertyLoad(masm, eax,
418 holder, lookup->holder(),
419 lookup->GetFieldIndex());
420 __ ret(0);
421 } else {
422 ASSERT(lookup->type() == CALLBACKS);
423 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
424 ASSERT(callback != NULL);
425 ASSERT(callback->getter() != NULL);
426
427 Label cleanup;
428 __ pop(scratch2);
429 __ push(receiver);
430 __ push(scratch2);
431
432 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
433 lookup->holder(), scratch1,
434 scratch2,
435 name,
436 &cleanup);
437
438 __ pop(scratch2); // save old return address
439 __ push(holder);
440 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
441 __ push(holder);
442 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
443 __ push(name_);
444 __ push(scratch2); // restore old return address
445
446 ExternalReference ref =
447 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000448 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000449
450 __ bind(&cleanup);
451 __ pop(scratch1);
452 __ pop(scratch2);
453 __ push(scratch1);
454 }
455 }
456
457
458 void CompileRegular(MacroAssembler* masm,
459 Register receiver,
460 Register holder,
461 Register scratch,
462 JSObject* holder_obj,
463 Label* miss_label) {
464 __ pop(scratch); // save old return address
465 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
466 __ push(scratch); // restore old return address
467
468 ExternalReference ref = ExternalReference(
469 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000470 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000471 }
472
473 private:
474 Register name_;
475};
476
477
ager@chromium.org5c838252010-02-19 08:53:10 +0000478// Holds information about possible function call optimizations.
479class CallOptimization BASE_EMBEDDED {
480 public:
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000481 explicit CallOptimization(LookupResult* lookup) {
482 if (!lookup->IsProperty() || !lookup->IsCacheable() ||
483 lookup->type() != CONSTANT_FUNCTION) {
484 Initialize(NULL);
485 } else {
486 // We only optimize constant function calls.
487 Initialize(lookup->GetConstantFunction());
488 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000489 }
490
491 explicit CallOptimization(JSFunction* function) {
492 Initialize(function);
493 }
494
495 bool is_constant_call() const {
496 return constant_function_ != NULL;
497 }
498
499 JSFunction* constant_function() const {
500 ASSERT(constant_function_ != NULL);
501 return constant_function_;
502 }
503
504 bool is_simple_api_call() const {
505 return is_simple_api_call_;
506 }
507
508 FunctionTemplateInfo* expected_receiver_type() const {
509 ASSERT(is_simple_api_call_);
510 return expected_receiver_type_;
511 }
512
513 CallHandlerInfo* api_call_info() const {
514 ASSERT(is_simple_api_call_);
515 return api_call_info_;
516 }
517
518 // Returns the depth of the object having the expected type in the
519 // prototype chain between the two arguments.
520 int GetPrototypeDepthOfExpectedType(JSObject* object,
521 JSObject* holder) const {
522 ASSERT(is_simple_api_call_);
523 if (expected_receiver_type_ == NULL) return 0;
524 int depth = 0;
525 while (object != holder) {
526 if (object->IsInstanceOf(expected_receiver_type_)) return depth;
527 object = JSObject::cast(object->GetPrototype());
528 ++depth;
529 }
530 if (holder->IsInstanceOf(expected_receiver_type_)) return depth;
531 return kInvalidProtoDepth;
532 }
533
534 private:
535 void Initialize(JSFunction* function) {
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000536 constant_function_ = NULL;
537 is_simple_api_call_ = false;
538 expected_receiver_type_ = NULL;
539 api_call_info_ = NULL;
540
541 if (function == NULL || !function->is_compiled()) return;
ager@chromium.org5c838252010-02-19 08:53:10 +0000542
543 constant_function_ = function;
ager@chromium.org5c838252010-02-19 08:53:10 +0000544 AnalyzePossibleApiFunction(function);
545 }
546
547 // Determines whether the given function can be called using the
548 // fast api call builtin.
549 void AnalyzePossibleApiFunction(JSFunction* function) {
550 SharedFunctionInfo* sfi = function->shared();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000551 if (!sfi->IsApiFunction()) return;
552 FunctionTemplateInfo* info = sfi->get_api_func_data();
ager@chromium.org5c838252010-02-19 08:53:10 +0000553
554 // Require a C++ callback.
555 if (info->call_code()->IsUndefined()) return;
556 api_call_info_ = CallHandlerInfo::cast(info->call_code());
557
558 // Accept signatures that either have no restrictions at all or
559 // only have restrictions on the receiver.
560 if (!info->signature()->IsUndefined()) {
561 SignatureInfo* signature = SignatureInfo::cast(info->signature());
562 if (!signature->args()->IsUndefined()) return;
563 if (!signature->receiver()->IsUndefined()) {
564 expected_receiver_type_ =
565 FunctionTemplateInfo::cast(signature->receiver());
566 }
567 }
568
569 is_simple_api_call_ = true;
570 }
571
572 JSFunction* constant_function_;
573 bool is_simple_api_call_;
574 FunctionTemplateInfo* expected_receiver_type_;
575 CallHandlerInfo* api_call_info_;
576};
577
578
579// Reserves space for the extra arguments to FastHandleApiCall in the
580// caller's frame.
581//
582// These arguments are set by CheckPrototypes and GenerateFastApiCall.
583static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
584 // ----------- S t a t e -------------
585 // -- esp[0] : return address
586 // -- esp[4] : last argument in the internal frame of the caller
587 // -----------------------------------
588 __ pop(scratch);
589 __ push(Immediate(Smi::FromInt(0)));
590 __ push(Immediate(Smi::FromInt(0)));
591 __ push(Immediate(Smi::FromInt(0)));
592 __ push(Immediate(Smi::FromInt(0)));
593 __ push(scratch);
594}
595
596
597// Undoes the effects of ReserveSpaceForFastApiCall.
598static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
599 // ----------- S t a t e -------------
600 // -- esp[0] : return address
601 // -- esp[4] : last fast api call extra argument
602 // -- ...
603 // -- esp[16] : first fast api call extra argument
604 // -- esp[20] : last argument in the internal frame
605 // -----------------------------------
606 __ pop(scratch);
607 __ add(Operand(esp), Immediate(kPointerSize * 4));
608 __ push(scratch);
609}
610
611
612// Generates call to FastHandleApiCall builtin.
613static void GenerateFastApiCall(MacroAssembler* masm,
614 const CallOptimization& optimization,
615 int argc) {
616 // ----------- S t a t e -------------
617 // -- esp[0] : return address
618 // -- esp[4] : object passing the type check
619 // (last fast api call extra argument,
620 // set by CheckPrototypes)
621 // -- esp[8] : api call data
622 // -- esp[12] : api callback
623 // -- esp[16] : api function
624 // (first fast api call extra argument)
625 // -- esp[20] : last argument
626 // -- ...
627 // -- esp[(argc + 5) * 4] : first argument
628 // -- esp[(argc + 6) * 4] : receiver
629 // -----------------------------------
630
631 // Get the function and setup the context.
632 JSFunction* function = optimization.constant_function();
633 __ mov(edi, Immediate(Handle<JSFunction>(function)));
634 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
635
636 // Pass the additional arguments FastHandleApiCall expects.
637 __ mov(Operand(esp, 4 * kPointerSize), edi);
638 bool info_loaded = false;
639 Object* callback = optimization.api_call_info()->callback();
640 if (Heap::InNewSpace(callback)) {
641 info_loaded = true;
642 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
643 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
644 __ mov(Operand(esp, 3 * kPointerSize), ebx);
645 } else {
646 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
647 }
648 Object* call_data = optimization.api_call_info()->data();
649 if (Heap::InNewSpace(call_data)) {
650 if (!info_loaded) {
651 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
652 }
653 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
654 __ mov(Operand(esp, 2 * kPointerSize), ebx);
655 } else {
656 __ mov(Operand(esp, 2 * kPointerSize),
657 Immediate(Handle<Object>(call_data)));
658 }
659
660 // Set the number of arguments.
661 __ mov(eax, Immediate(argc + 4));
662
663 // Jump to the fast api call builtin (tail call).
664 Handle<Code> code = Handle<Code>(
665 Builtins::builtin(Builtins::FastHandleApiCall));
666 ParameterCount expected(0);
667 __ InvokeCode(code, expected, expected,
668 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
669}
670
671
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000672class CallInterceptorCompiler BASE_EMBEDDED {
673 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000674 CallInterceptorCompiler(StubCompiler* stub_compiler,
675 const ParameterCount& arguments,
676 Register name)
677 : stub_compiler_(stub_compiler),
678 arguments_(arguments),
679 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000680
ager@chromium.org5c838252010-02-19 08:53:10 +0000681 void Compile(MacroAssembler* masm,
682 JSObject* object,
683 JSObject* holder,
684 String* name,
685 LookupResult* lookup,
686 Register receiver,
687 Register scratch1,
688 Register scratch2,
689 Label* miss) {
690 ASSERT(holder->HasNamedInterceptor());
691 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
692
693 // Check that the receiver isn't a smi.
694 __ test(receiver, Immediate(kSmiTagMask));
695 __ j(zero, miss, not_taken);
696
697 CallOptimization optimization(lookup);
698
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000699 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000700 CompileCacheable(masm,
701 object,
702 receiver,
703 scratch1,
704 scratch2,
705 holder,
706 lookup,
707 name,
708 optimization,
709 miss);
710 } else {
711 CompileRegular(masm,
712 object,
713 receiver,
714 scratch1,
715 scratch2,
716 name,
717 holder,
718 miss);
719 }
720 }
721
722 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000723 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000724 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000725 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000726 Register scratch1,
727 Register scratch2,
728 JSObject* holder_obj,
729 LookupResult* lookup,
730 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000731 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000732 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000733 ASSERT(optimization.is_constant_call());
734 ASSERT(!lookup->holder()->IsGlobalObject());
735
736 int depth1 = kInvalidProtoDepth;
737 int depth2 = kInvalidProtoDepth;
738 bool can_do_fast_api_call = false;
739 if (optimization.is_simple_api_call() &&
740 !lookup->holder()->IsGlobalObject()) {
741 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
742 if (depth1 == kInvalidProtoDepth) {
743 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
744 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000745 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000746 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
747 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000748 }
749
ager@chromium.org5c838252010-02-19 08:53:10 +0000750 __ IncrementCounter(&Counters::call_const_interceptor, 1);
751
752 if (can_do_fast_api_call) {
753 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
754 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000755 }
756
ager@chromium.org5c838252010-02-19 08:53:10 +0000757 Label miss_cleanup;
758 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
759 Register holder =
760 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
761 scratch1, scratch2, name,
762 depth1, miss);
763
764 Label regular_invoke;
765 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
766
767 // Generate code for the failed interceptor case.
768
769 // Check the lookup is still valid.
770 stub_compiler_->CheckPrototypes(holder_obj, receiver,
771 lookup->holder(),
772 scratch1, scratch2, name,
773 depth2, miss);
774
775 if (can_do_fast_api_call) {
776 GenerateFastApiCall(masm, optimization, arguments_.immediate());
777 } else {
778 __ InvokeFunction(optimization.constant_function(), arguments_,
779 JUMP_FUNCTION);
780 }
781
782 if (can_do_fast_api_call) {
783 __ bind(&miss_cleanup);
784 FreeSpaceForFastApiCall(masm, scratch1);
785 __ jmp(miss_label);
786 }
787
788 __ bind(&regular_invoke);
789 if (can_do_fast_api_call) {
790 FreeSpaceForFastApiCall(masm, scratch1);
791 }
792 }
793
794 void CompileRegular(MacroAssembler* masm,
795 JSObject* object,
796 Register receiver,
797 Register scratch1,
798 Register scratch2,
799 String* name,
800 JSObject* holder_obj,
801 Label* miss_label) {
802 Register holder =
803 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
804 scratch1, scratch2, name,
805 miss_label);
806
807 __ EnterInternalFrame();
808 // Save the name_ register across the call.
809 __ push(name_);
810
811 PushInterceptorArguments(masm,
812 receiver,
813 holder,
814 name_,
815 holder_obj);
816
817 __ CallExternalReference(
818 ExternalReference(
819 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
820 5);
821
822 // Restore the name_ register.
823 __ pop(name_);
824 __ LeaveInternalFrame();
825 }
826
827 void LoadWithInterceptor(MacroAssembler* masm,
828 Register receiver,
829 Register holder,
830 JSObject* holder_obj,
831 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000832 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000833 __ push(holder); // Save the holder.
834 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000835
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000836 CompileCallLoadPropertyWithInterceptor(masm,
837 receiver,
838 holder,
839 name_,
840 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000841
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000842 __ pop(name_); // Restore the name.
843 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000844 __ LeaveInternalFrame();
845
846 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000847 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000848 }
849
ager@chromium.org5c838252010-02-19 08:53:10 +0000850 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000851 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000852 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000853};
854
855
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000856void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
857 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
858 Code* code = NULL;
859 if (kind == Code::LOAD_IC) {
860 code = Builtins::builtin(Builtins::LoadIC_Miss);
861 } else {
862 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
863 }
864
865 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000866 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000867}
868
869
ager@chromium.org5c838252010-02-19 08:53:10 +0000870// Both name_reg and receiver_reg are preserved on jumps to miss_label,
871// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872void StubCompiler::GenerateStoreField(MacroAssembler* masm,
873 JSObject* object,
874 int index,
875 Map* transition,
876 Register receiver_reg,
877 Register name_reg,
878 Register scratch,
879 Label* miss_label) {
880 // Check that the object isn't a smi.
881 __ test(receiver_reg, Immediate(kSmiTagMask));
882 __ j(zero, miss_label, not_taken);
883
884 // Check that the map of the object hasn't changed.
885 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
886 Immediate(Handle<Map>(object->map())));
887 __ j(not_equal, miss_label, not_taken);
888
889 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000890 if (object->IsJSGlobalProxy()) {
891 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000892 }
893
894 // Stub never generated for non-global objects that require access
895 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000896 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000897
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000898 // Perform map transition for the receiver if necessary.
899 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
900 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000901 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000902 __ pop(scratch); // Return address.
903 __ push(receiver_reg);
904 __ push(Immediate(Handle<Map>(transition)));
905 __ push(eax);
906 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000907 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000908 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000909 return;
910 }
911
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000912 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000913 // Update the map of the object; no write barrier updating is
914 // needed because the map is never in new space.
915 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
916 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917 }
918
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000919 // Adjust for the number of properties stored in the object. Even in the
920 // face of a transition we can use the old map here because the size of the
921 // object and the number of in-object properties is not going to change.
922 index -= object->map()->inobject_properties();
923
ager@chromium.org7c537e22008-10-16 08:43:32 +0000924 if (index < 0) {
925 // Set the property straight into the object.
926 int offset = object->map()->instance_size() + (index * kPointerSize);
927 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000928
ager@chromium.org7c537e22008-10-16 08:43:32 +0000929 // Update the write barrier for the array address.
930 // Pass the value being stored in the now unused name_reg.
931 __ mov(name_reg, Operand(eax));
932 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
933 } else {
934 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000935 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000936 // Get the properties array (optimistically).
937 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000938 __ mov(FieldOperand(scratch, offset), eax);
939
940 // Update the write barrier for the array address.
941 // Pass the value being stored in the now unused name_reg.
942 __ mov(name_reg, Operand(eax));
943 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
944 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000945
946 // Return the value (register eax).
947 __ ret(0);
948}
949
950
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000951// Generate code to check that a global property cell is empty. Create
952// the property cell at compilation time if no cell exists for the
953// property.
954static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
955 GlobalObject* global,
956 String* name,
957 Register scratch,
958 Label* miss) {
959 Object* probe = global->EnsurePropertyCell(name);
960 if (probe->IsFailure()) return probe;
961 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
962 ASSERT(cell->value()->IsTheHole());
963 __ mov(scratch, Immediate(Handle<Object>(cell)));
964 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
965 Immediate(Factory::the_hole_value()));
966 __ j(not_equal, miss, not_taken);
967 return cell;
968}
969
970
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000971#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000972#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973
974
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000975Register StubCompiler::CheckPrototypes(JSObject* object,
976 Register object_reg,
977 JSObject* holder,
978 Register holder_reg,
979 Register scratch,
980 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000981 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000982 Label* miss) {
983 // Check that the maps haven't changed.
984 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000985 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
986 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000987
988 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000989 // that their maps haven't changed. We also need to check that the
990 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000991 while (object != holder) {
992 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000993 Object* cell = GenerateCheckPropertyCell(masm(),
994 GlobalObject::cast(object),
995 name,
996 scratch,
997 miss);
998 if (cell->IsFailure()) {
999 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001000 return result;
1001 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001002 }
1003 object = JSObject::cast(object->GetPrototype());
1004 }
1005
ager@chromium.org5c838252010-02-19 08:53:10 +00001006 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001007 return result;
1008}
1009
1010
1011void StubCompiler::GenerateLoadField(JSObject* object,
1012 JSObject* holder,
1013 Register receiver,
1014 Register scratch1,
1015 Register scratch2,
1016 int index,
1017 String* name,
1018 Label* miss) {
1019 // Check that the receiver isn't a smi.
1020 __ test(receiver, Immediate(kSmiTagMask));
1021 __ j(zero, miss, not_taken);
1022
1023 // Check the prototype chain.
1024 Register reg =
1025 CheckPrototypes(object, receiver, holder,
1026 scratch1, scratch2, name, miss);
1027
1028 // Get the value from the properties.
1029 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1030 __ ret(0);
1031}
1032
1033
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001034bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001035 JSObject* holder,
1036 Register receiver,
1037 Register name_reg,
1038 Register scratch1,
1039 Register scratch2,
1040 AccessorInfo* callback,
1041 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001042 Label* miss,
1043 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001044 // Check that the receiver isn't a smi.
1045 __ test(receiver, Immediate(kSmiTagMask));
1046 __ j(zero, miss, not_taken);
1047
1048 // Check that the maps haven't changed.
1049 Register reg =
1050 CheckPrototypes(object, receiver, holder,
1051 scratch1, scratch2, name, miss);
1052
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001053 Handle<AccessorInfo> callback_handle(callback);
1054
1055 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1056 __ EnterInternalFrame();
1057 __ PushHandleScope(other);
1058 // Push the stack address where the list of arguments ends
1059 __ mov(other, esp);
1060 __ sub(Operand(other), Immediate(2 * kPointerSize));
1061 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001062 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001063 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001064 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001065 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001066 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001067 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001068 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001069 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001070 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001071 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001072
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001073 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001074 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001075 Address getter_address = v8::ToCData<Address>(callback->getter());
1076 ApiFunction fun(getter_address);
1077 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001078 // Emitting a stub call may try to allocate (if the code is not
1079 // already generated). Do not allow the assembler to perform a
1080 // garbage collection but instead return the allocation failure
1081 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001082 Object* result = masm()->TryCallStub(&stub);
1083 if (result->IsFailure()) {
1084 *failure = Failure::cast(result);
1085 return false;
1086 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001087
1088 // We need to avoid using eax since that now holds the result.
1089 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001090 // Emitting PopHandleScope may try to allocate. Do not allow the
1091 // assembler to perform a garbage collection but instead return a
1092 // failure object.
1093 result = masm()->TryPopHandleScope(eax, tmp);
1094 if (result->IsFailure()) {
1095 *failure = Failure::cast(result);
1096 return false;
1097 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001098 __ LeaveInternalFrame();
1099
1100 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001101 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001102}
1103
1104
1105void StubCompiler::GenerateLoadConstant(JSObject* object,
1106 JSObject* holder,
1107 Register receiver,
1108 Register scratch1,
1109 Register scratch2,
1110 Object* value,
1111 String* name,
1112 Label* miss) {
1113 // Check that the receiver isn't a smi.
1114 __ test(receiver, Immediate(kSmiTagMask));
1115 __ j(zero, miss, not_taken);
1116
1117 // Check that the maps haven't changed.
1118 Register reg =
1119 CheckPrototypes(object, receiver, holder,
1120 scratch1, scratch2, name, miss);
1121
1122 // Return the constant value.
1123 __ mov(eax, Handle<Object>(value));
1124 __ ret(0);
1125}
1126
1127
1128void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1129 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001130 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001131 Register receiver,
1132 Register name_reg,
1133 Register scratch1,
1134 Register scratch2,
1135 String* name,
1136 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001137 LoadInterceptorCompiler compiler(name_reg);
1138 CompileLoadInterceptor(&compiler,
1139 this,
1140 masm(),
1141 object,
1142 holder,
1143 name,
1144 lookup,
1145 receiver,
1146 scratch1,
1147 scratch2,
1148 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001149}
1150
1151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001152// TODO(1241006): Avoid having lazy compile stubs specialized by the
1153// number of arguments. It is not needed anymore.
1154Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001155 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001156 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157
1158 // Push a copy of the function onto the stack.
1159 __ push(edi);
1160
1161 __ push(edi); // function is also the parameter to the runtime call
1162 __ CallRuntime(Runtime::kLazyCompile, 1);
1163 __ pop(edi);
1164
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001165 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001166 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001167
1168 // Do a tail-call of the compiled function.
1169 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1170 __ jmp(Operand(ecx));
1171
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001172 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173}
1174
1175
ager@chromium.org5c838252010-02-19 08:53:10 +00001176Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001177 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001178 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001179 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001180 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001181 // -- ecx : name
1182 // -- esp[0] : return address
1183 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1184 // -- ...
1185 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187 Label miss;
1188
1189 // Get the receiver from the stack.
1190 const int argc = arguments().immediate();
1191 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1192
1193 // Check that the receiver isn't a smi.
1194 __ test(edx, Immediate(kSmiTagMask));
1195 __ j(zero, &miss, not_taken);
1196
1197 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001198 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199
ager@chromium.org7c537e22008-10-16 08:43:32 +00001200 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201
1202 // Check that the function really is a function.
1203 __ test(edi, Immediate(kSmiTagMask));
1204 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001205 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206 __ j(not_equal, &miss, not_taken);
1207
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001208 // Patch the receiver on the stack with the global proxy if
1209 // necessary.
1210 if (object->IsGlobalObject()) {
1211 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1212 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1213 }
1214
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001215 // Invoke the function.
1216 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1217
1218 // Handle call cache miss.
1219 __ bind(&miss);
1220 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001221 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001222
1223 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001224 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225}
1226
1227
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001228Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1229 JSObject* holder,
1230 JSFunction* function,
1231 String* name,
1232 CheckType check) {
1233 // ----------- S t a t e -------------
1234 // -- ecx : name
1235 // -- esp[0] : return address
1236 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1237 // -- ...
1238 // -- esp[(argc + 1) * 4] : receiver
1239 // -----------------------------------
1240 ASSERT(check == RECEIVER_MAP_CHECK);
1241
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001242 // If object is not an array, bail out to regular call.
1243 if (!object->IsJSArray()) {
1244 return Heap::undefined_value();
1245 }
1246
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001247 Label miss;
1248
1249 // Get the receiver from the stack.
1250 const int argc = arguments().immediate();
1251 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1252
1253 // Check that the receiver isn't a smi.
1254 __ test(edx, Immediate(kSmiTagMask));
1255 __ j(zero, &miss);
1256
1257 CheckPrototypes(JSObject::cast(object), edx,
1258 holder, ebx,
1259 eax, name, &miss);
1260
1261 if (argc == 0) {
1262 // Noop, return the length.
1263 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1264 __ ret((argc + 1) * kPointerSize);
1265 } else {
1266 // Get the elements array of the object.
1267 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1268
1269 // Check that the elements are in fast mode (not dictionary).
1270 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1271 Immediate(Factory::fixed_array_map()));
1272 __ j(not_equal, &miss);
1273
1274 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001275 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001276
1277 // Get the array's length into eax and calculate new length.
1278 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1279 STATIC_ASSERT(kSmiTagSize == 1);
1280 STATIC_ASSERT(kSmiTag == 0);
1281 __ add(Operand(eax), Immediate(argc << 1));
1282
1283 // Get the element's length into ecx.
1284 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1285 __ SmiTag(ecx);
1286
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001287 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001288 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001289 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001290
1291 // Save new length.
1292 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1293
1294 // Push the element.
1295 __ lea(edx, FieldOperand(ebx,
1296 eax, times_half_pointer_size,
1297 FixedArray::kHeaderSize - argc * kPointerSize));
1298 __ mov(ecx, Operand(esp, argc * kPointerSize));
1299 __ mov(Operand(edx, 0), ecx);
1300
1301 // Check if value is a smi.
1302 __ test(ecx, Immediate(kSmiTagMask));
1303 __ j(not_zero, &with_rset_update);
1304
1305 __ bind(&exit);
1306 __ ret((argc + 1) * kPointerSize);
1307
1308 __ bind(&with_rset_update);
1309
1310 __ InNewSpace(ebx, ecx, equal, &exit);
1311
1312 RecordWriteStub stub(ebx, edx, ecx);
1313 __ CallStub(&stub);
1314 __ ret((argc + 1) * kPointerSize);
1315
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001316 __ bind(&attempt_to_grow_elements);
1317 ExternalReference new_space_allocation_top =
1318 ExternalReference::new_space_allocation_top_address();
1319 ExternalReference new_space_allocation_limit =
1320 ExternalReference::new_space_allocation_limit_address();
1321
1322 const int kAllocationDelta = 4;
1323 // Load top.
1324 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1325
1326 // Check if it's the end of elements.
1327 __ lea(edx, FieldOperand(ebx,
1328 eax, times_half_pointer_size,
1329 FixedArray::kHeaderSize - argc * kPointerSize));
1330 __ cmp(edx, Operand(ecx));
1331 __ j(not_equal, &call_builtin);
1332 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1333 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
1334 __ j(greater, &call_builtin);
1335
1336 // We fit and could grow elements.
1337 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1338 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001339
1340 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001341 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001342 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001343 for (int i = 1; i < kAllocationDelta; i++) {
1344 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001345 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001346 }
1347
1348 // Restore receiver to edx as finish sequence assumes it's here.
1349 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1350
1351 // Increment element's and array's sizes.
1352 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1353 Immediate(kAllocationDelta));
1354 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1355
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001356 // Elements are in new space, so no remembered set updates are necessary.
1357 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001358
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001359 __ bind(&call_builtin);
1360 }
1361
1362 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1363 argc + 1,
1364 1);
1365 }
1366
1367 __ bind(&miss);
1368
1369 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1370 __ jmp(ic, RelocInfo::CODE_TARGET);
1371
1372 // Return the generated code.
1373 String* function_name = NULL;
1374 if (function->shared()->name()->IsString()) {
1375 function_name = String::cast(function->shared()->name());
1376 }
1377 return GetCode(CONSTANT_FUNCTION, function_name);
1378}
1379
1380
1381Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1382 JSObject* holder,
1383 JSFunction* function,
1384 String* name,
1385 CheckType check) {
1386 // ----------- S t a t e -------------
1387 // -- ecx : name
1388 // -- esp[0] : return address
1389 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1390 // -- ...
1391 // -- esp[(argc + 1) * 4] : receiver
1392 // -----------------------------------
1393 ASSERT(check == RECEIVER_MAP_CHECK);
1394
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001395 // If object is not an array, bail out to regular call.
1396 if (!object->IsJSArray()) {
1397 return Heap::undefined_value();
1398 }
1399
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001400 Label miss, empty_array, call_builtin;
1401
1402 // Get the receiver from the stack.
1403 const int argc = arguments().immediate();
1404 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1405
1406 // Check that the receiver isn't a smi.
1407 __ test(edx, Immediate(kSmiTagMask));
1408 __ j(zero, &miss);
1409
1410 CheckPrototypes(JSObject::cast(object), edx,
1411 holder, ebx,
1412 eax, name, &miss);
1413
1414 // Get the elements array of the object.
1415 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1416
1417 // Check that the elements are in fast mode (not dictionary).
1418 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1419 Immediate(Factory::fixed_array_map()));
1420 __ j(not_equal, &miss);
1421
1422 // Get the array's length into ecx and calculate new length.
1423 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1424 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1425 __ j(negative, &empty_array);
1426
1427 // Get the last element.
1428 STATIC_ASSERT(kSmiTagSize == 1);
1429 STATIC_ASSERT(kSmiTag == 0);
1430 __ mov(eax, FieldOperand(ebx,
1431 ecx, times_half_pointer_size,
1432 FixedArray::kHeaderSize));
1433 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1434 __ j(equal, &call_builtin);
1435
1436 // Set the array's length.
1437 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1438
1439 // Fill with the hole.
1440 __ mov(FieldOperand(ebx,
1441 ecx, times_half_pointer_size,
1442 FixedArray::kHeaderSize),
1443 Immediate(Factory::the_hole_value()));
1444 __ ret((argc + 1) * kPointerSize);
1445
1446 __ bind(&empty_array);
1447 __ mov(eax, Immediate(Factory::undefined_value()));
1448 __ ret((argc + 1) * kPointerSize);
1449
1450 __ bind(&call_builtin);
1451
1452 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1453 argc + 1,
1454 1);
1455
1456 __ bind(&miss);
1457
1458 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1459 __ jmp(ic, RelocInfo::CODE_TARGET);
1460
1461 // Return the generated code.
1462 String* function_name = NULL;
1463 if (function->shared()->name()->IsString()) {
1464 function_name = String::cast(function->shared()->name());
1465 }
1466 return GetCode(CONSTANT_FUNCTION, function_name);
1467}
1468
1469
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001470Object* CallStubCompiler::CompileCallConstant(Object* object,
1471 JSObject* holder,
1472 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001473 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001474 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001475 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001476 // -- ecx : name
1477 // -- esp[0] : return address
1478 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1479 // -- ...
1480 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001482
1483 SharedFunctionInfo* function_info = function->shared();
1484 if (function_info->HasCustomCallGenerator()) {
1485 CustomCallGenerator generator =
1486 ToCData<CustomCallGenerator>(function_info->function_data());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001487 Object* result = generator(this, object, holder, function, name, check);
1488 // undefined means bail out to regular compiler.
1489 if (!result->IsUndefined()) {
1490 return result;
1491 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001492 }
1493
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001494 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001495
1496 // Get the receiver from the stack.
1497 const int argc = arguments().immediate();
1498 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1499
1500 // Check that the receiver isn't a smi.
1501 if (check != NUMBER_CHECK) {
1502 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001503 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 }
1505
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001506 // Make sure that it's okay not to patch the on stack receiver
1507 // unless we're doing a receiver map check.
1508 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1509
ager@chromium.org5c838252010-02-19 08:53:10 +00001510 CallOptimization optimization(function);
1511 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001512 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001514 switch (check) {
1515 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001516 __ IncrementCounter(&Counters::call_const, 1);
1517
1518 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1519 depth = optimization.GetPrototypeDepthOfExpectedType(
1520 JSObject::cast(object), holder);
1521 }
1522
1523 if (depth != kInvalidProtoDepth) {
1524 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1525 ReserveSpaceForFastApiCall(masm(), eax);
1526 }
1527
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001529 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001530 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001531
1532 // Patch the receiver on the stack with the global proxy if
1533 // necessary.
1534 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001535 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001536 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1537 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1538 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539 break;
1540
1541 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001542 if (!function->IsBuiltin()) {
1543 // Calling non-builtins with a value as receiver requires boxing.
1544 __ jmp(&miss);
1545 } else {
1546 // Check that the object is a string or a symbol.
1547 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1548 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1549 __ cmp(eax, FIRST_NONSTRING_TYPE);
1550 __ j(above_equal, &miss, not_taken);
1551 // Check that the maps starting from the prototype haven't changed.
1552 GenerateLoadGlobalFunctionPrototype(masm(),
1553 Context::STRING_FUNCTION_INDEX,
1554 eax);
1555 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1556 ebx, edx, name, &miss);
1557 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001558 break;
1559
1560 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001561 if (!function->IsBuiltin()) {
1562 // Calling non-builtins with a value as receiver requires boxing.
1563 __ jmp(&miss);
1564 } else {
1565 Label fast;
1566 // Check that the object is a smi or a heap number.
1567 __ test(edx, Immediate(kSmiTagMask));
1568 __ j(zero, &fast, taken);
1569 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1570 __ j(not_equal, &miss, not_taken);
1571 __ bind(&fast);
1572 // Check that the maps starting from the prototype haven't changed.
1573 GenerateLoadGlobalFunctionPrototype(masm(),
1574 Context::NUMBER_FUNCTION_INDEX,
1575 eax);
1576 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1577 ebx, edx, name, &miss);
1578 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 break;
1580 }
1581
1582 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001583 if (!function->IsBuiltin()) {
1584 // Calling non-builtins with a value as receiver requires boxing.
1585 __ jmp(&miss);
1586 } else {
1587 Label fast;
1588 // Check that the object is a boolean.
1589 __ cmp(edx, Factory::true_value());
1590 __ j(equal, &fast, taken);
1591 __ cmp(edx, Factory::false_value());
1592 __ j(not_equal, &miss, not_taken);
1593 __ bind(&fast);
1594 // Check that the maps starting from the prototype haven't changed.
1595 GenerateLoadGlobalFunctionPrototype(masm(),
1596 Context::BOOLEAN_FUNCTION_INDEX,
1597 eax);
1598 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1599 ebx, edx, name, &miss);
1600 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601 break;
1602 }
1603
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 default:
1605 UNREACHABLE();
1606 }
1607
ager@chromium.org5c838252010-02-19 08:53:10 +00001608 if (depth != kInvalidProtoDepth) {
1609 GenerateFastApiCall(masm(), optimization, argc);
1610 } else {
1611 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1612 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001613
1614 // Handle call cache miss.
1615 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001616 if (depth != kInvalidProtoDepth) {
1617 FreeSpaceForFastApiCall(masm(), eax);
1618 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001619 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001621 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622
1623 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001624 String* function_name = NULL;
1625 if (function->shared()->name()->IsString()) {
1626 function_name = String::cast(function->shared()->name());
1627 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001628 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001629}
1630
1631
ager@chromium.org5c838252010-02-19 08:53:10 +00001632Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 JSObject* holder,
1634 String* name) {
1635 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001636 // -- ecx : name
1637 // -- esp[0] : return address
1638 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1639 // -- ...
1640 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001641 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642 Label miss;
1643
1644 // Get the number of arguments.
1645 const int argc = arguments().immediate();
1646
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001647 LookupResult lookup;
1648 LookupPostInterceptor(holder, name, &lookup);
1649
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650 // Get the receiver from the stack.
1651 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001652
ager@chromium.org5c838252010-02-19 08:53:10 +00001653 CallInterceptorCompiler compiler(this, arguments(), ecx);
1654 compiler.Compile(masm(),
1655 object,
1656 holder,
1657 name,
1658 &lookup,
1659 edx,
1660 ebx,
1661 edi,
1662 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001663
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001664 // Restore receiver.
1665 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001666
1667 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001668 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001670 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 __ j(not_equal, &miss, not_taken);
1672
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001673 // Patch the receiver on the stack with the global proxy if
1674 // necessary.
1675 if (object->IsGlobalObject()) {
1676 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1677 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1678 }
1679
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001681 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001682 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1683
1684 // Handle load cache miss.
1685 __ bind(&miss);
1686 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001687 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688
1689 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691}
1692
1693
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001694Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1695 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001696 JSGlobalPropertyCell* cell,
1697 JSFunction* function,
1698 String* name) {
1699 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001700 // -- ecx : name
1701 // -- esp[0] : return address
1702 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1703 // -- ...
1704 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001705 // -----------------------------------
1706 Label miss;
1707
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001708 // Get the number of arguments.
1709 const int argc = arguments().immediate();
1710
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001711 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001712 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001713
1714 // If the object is the holder then we know that it's a global
1715 // object which can only happen for contextual calls. In this case,
1716 // the receiver cannot be a smi.
1717 if (object != holder) {
1718 __ test(edx, Immediate(kSmiTagMask));
1719 __ j(zero, &miss, not_taken);
1720 }
1721
1722 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001723 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001724
1725 // Get the value from the cell.
1726 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1727 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1728
1729 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001730 if (Heap::InNewSpace(function)) {
1731 // We can't embed a pointer to a function in new space so we have
1732 // to verify that the shared function info is unchanged. This has
1733 // the nice side effect that multiple closures based on the same
1734 // function can all use this call IC. Before we load through the
1735 // function, we have to verify that it still is a function.
1736 __ test(edi, Immediate(kSmiTagMask));
1737 __ j(zero, &miss, not_taken);
1738 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1739 __ j(not_equal, &miss, not_taken);
1740
1741 // Check the shared function info. Make sure it hasn't changed.
1742 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1743 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1744 __ j(not_equal, &miss, not_taken);
1745 } else {
1746 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1747 __ j(not_equal, &miss, not_taken);
1748 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001749
1750 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001751 if (object->IsGlobalObject()) {
1752 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1753 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1754 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001755
1756 // Setup the context (function already in edi).
1757 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1758
1759 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001760 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001761 ASSERT(function->is_compiled());
1762 Handle<Code> code(function->code());
1763 ParameterCount expected(function->shared()->formal_parameter_count());
1764 __ InvokeCode(code, expected, arguments(),
1765 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1766
1767 // Handle call cache miss.
1768 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001769 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1770 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1771 __ jmp(ic, RelocInfo::CODE_TARGET);
1772
1773 // Return the generated code.
1774 return GetCode(NORMAL, name);
1775}
1776
1777
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001778Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1779 int index,
1780 Map* transition,
1781 String* name) {
1782 // ----------- S t a t e -------------
1783 // -- eax : value
1784 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001785 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001788 Label miss;
1789
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001790 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001791 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001792 object,
1793 index,
1794 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001795 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001796 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797
1798 // Handle store cache miss.
1799 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001800 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001802 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803
1804 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001805 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806}
1807
1808
1809Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1810 AccessorInfo* callback,
1811 String* name) {
1812 // ----------- S t a t e -------------
1813 // -- eax : value
1814 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001815 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001816 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818 Label miss;
1819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001821 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001822 __ j(zero, &miss, not_taken);
1823
1824 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001825 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826 Immediate(Handle<Map>(object->map())));
1827 __ j(not_equal, &miss, not_taken);
1828
1829 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001830 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001831 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001832 }
1833
1834 // Stub never generated for non-global objects that require access
1835 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001836 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837
1838 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001839 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001840 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1841 __ push(ecx); // name
1842 __ push(eax); // value
1843 __ push(ebx); // restore return address
1844
mads.s.ager31e71382008-08-13 09:32:07 +00001845 // Do tail-call to the runtime system.
1846 ExternalReference store_callback_property =
1847 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001848 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849
1850 // Handle store cache miss.
1851 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001853 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001854
1855 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001856 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001857}
1858
1859
1860Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1861 String* name) {
1862 // ----------- S t a t e -------------
1863 // -- eax : value
1864 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001865 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001868 Label miss;
1869
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001871 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872 __ j(zero, &miss, not_taken);
1873
1874 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001875 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001876 Immediate(Handle<Map>(receiver->map())));
1877 __ j(not_equal, &miss, not_taken);
1878
1879 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001880 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001881 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001882 }
1883
1884 // Stub never generated for non-global objects that require access
1885 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001886 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001887
1888 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001889 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001890 __ push(ecx); // name
1891 __ push(eax); // value
1892 __ push(ebx); // restore return address
1893
mads.s.ager31e71382008-08-13 09:32:07 +00001894 // Do tail-call to the runtime system.
1895 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001897 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001898
1899 // Handle store cache miss.
1900 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001901 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001902 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001903
1904 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001905 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001906}
1907
1908
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001909Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1910 JSGlobalPropertyCell* cell,
1911 String* name) {
1912 // ----------- S t a t e -------------
1913 // -- eax : value
1914 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001915 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001916 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001917 // -----------------------------------
1918 Label miss;
1919
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001920 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001921 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001922 Immediate(Handle<Map>(object->map())));
1923 __ j(not_equal, &miss, not_taken);
1924
1925 // Store the value in the cell.
1926 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1927 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1928
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001929 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001930 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001931 __ ret(0);
1932
1933 // Handle store cache miss.
1934 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001935 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1936 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1937 __ jmp(ic, RelocInfo::CODE_TARGET);
1938
1939 // Return the generated code.
1940 return GetCode(NORMAL, name);
1941}
1942
1943
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1945 int index,
1946 Map* transition,
1947 String* name) {
1948 // ----------- S t a t e -------------
1949 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001950 // -- ecx : key
1951 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001952 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001953 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001954 Label miss;
1955
1956 __ IncrementCounter(&Counters::keyed_store_field, 1);
1957
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001958 // Check that the name has not changed.
1959 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1960 __ j(not_equal, &miss, not_taken);
1961
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001963 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001964 object,
1965 index,
1966 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001967 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001968 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969
1970 // Handle store cache miss.
1971 __ bind(&miss);
1972 __ DecrementCounter(&Counters::keyed_store_field, 1);
1973 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001974 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975
1976 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001977 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978}
1979
1980
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001981Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1982 JSObject* object,
1983 JSObject* last) {
1984 // ----------- S t a t e -------------
1985 // -- eax : receiver
1986 // -- ecx : name
1987 // -- esp[0] : return address
1988 // -----------------------------------
1989 Label miss;
1990
1991 // Check that the receiver isn't a smi.
1992 __ test(eax, Immediate(kSmiTagMask));
1993 __ j(zero, &miss, not_taken);
1994
1995 // Check the maps of the full prototype chain. Also check that
1996 // global property cells up to (but not including) the last object
1997 // in the prototype chain are empty.
1998 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1999
2000 // If the last object in the prototype chain is a global object,
2001 // check that the global property cell is empty.
2002 if (last->IsGlobalObject()) {
2003 Object* cell = GenerateCheckPropertyCell(masm(),
2004 GlobalObject::cast(last),
2005 name,
2006 edx,
2007 &miss);
2008 if (cell->IsFailure()) return cell;
2009 }
2010
2011 // Return undefined if maps of the full prototype chain are still the
2012 // same and no global property with this name contains a value.
2013 __ mov(eax, Factory::undefined_value());
2014 __ ret(0);
2015
2016 __ bind(&miss);
2017 GenerateLoadMiss(masm(), Code::LOAD_IC);
2018
2019 // Return the generated code.
2020 return GetCode(NONEXISTENT, Heap::empty_string());
2021}
2022
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002023
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002024Object* LoadStubCompiler::CompileLoadField(JSObject* object,
2025 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002026 int index,
2027 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002028 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002029 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 // -- ecx : name
2031 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002033 Label miss;
2034
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002035 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002036 __ bind(&miss);
2037 GenerateLoadMiss(masm(), Code::LOAD_IC);
2038
2039 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002040 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041}
2042
2043
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002044Object* LoadStubCompiler::CompileLoadCallback(String* name,
2045 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002046 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002047 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002048 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002049 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002050 // -- ecx : name
2051 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002052 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053 Label miss;
2054
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002055 Failure* failure = Failure::InternalError();
2056 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
2057 callback, name, &miss, &failure);
2058 if (!success) return failure;
2059
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 __ bind(&miss);
2061 GenerateLoadMiss(masm(), Code::LOAD_IC);
2062
2063 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002064 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065}
2066
2067
2068Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
2069 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002070 Object* value,
2071 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002073 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 // -- ecx : name
2075 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002077 Label miss;
2078
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002079 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080 __ bind(&miss);
2081 GenerateLoadMiss(masm(), Code::LOAD_IC);
2082
2083 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002084 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002085}
2086
2087
2088Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2089 JSObject* holder,
2090 String* name) {
2091 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002092 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002093 // -- ecx : name
2094 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002095 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002096 Label miss;
2097
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002098 LookupResult lookup;
2099 LookupPostInterceptor(holder, name, &lookup);
2100
ager@chromium.orge2902be2009-06-08 12:21:35 +00002101 // TODO(368): Compile in the whole chain: all the interceptors in
2102 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002103 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002104 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002105 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002106 eax,
2107 ecx,
2108 edx,
2109 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002110 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002111 &miss);
2112
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002113 __ bind(&miss);
2114 GenerateLoadMiss(masm(), Code::LOAD_IC);
2115
2116 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002117 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002118}
2119
2120
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002121Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2122 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002123 JSGlobalPropertyCell* cell,
2124 String* name,
2125 bool is_dont_delete) {
2126 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002127 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002128 // -- ecx : name
2129 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002130 // -----------------------------------
2131 Label miss;
2132
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002133 // If the object is the holder then we know that it's a global
2134 // object which can only happen for contextual loads. In this case,
2135 // the receiver cannot be a smi.
2136 if (object != holder) {
2137 __ test(eax, Immediate(kSmiTagMask));
2138 __ j(zero, &miss, not_taken);
2139 }
2140
2141 // Check that the maps haven't changed.
2142 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002143
2144 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002145 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2146 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002147
2148 // Check for deleted property if property can actually be deleted.
2149 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002150 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002151 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002152 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002153 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002154 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002155 }
2156
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002157 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002158 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002159 __ ret(0);
2160
2161 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002162 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2163 GenerateLoadMiss(masm(), Code::LOAD_IC);
2164
2165 // Return the generated code.
2166 return GetCode(NORMAL, name);
2167}
2168
2169
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002170Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2171 JSObject* receiver,
2172 JSObject* holder,
2173 int index) {
2174 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002175 // -- eax : key
2176 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002177 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002178 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002179 Label miss;
2180
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 __ IncrementCounter(&Counters::keyed_load_field, 1);
2182
2183 // Check that the name has not changed.
2184 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2185 __ j(not_equal, &miss, not_taken);
2186
ager@chromium.org5c838252010-02-19 08:53:10 +00002187 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002188
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002189 __ bind(&miss);
2190 __ DecrementCounter(&Counters::keyed_load_field, 1);
2191 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2192
2193 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002194 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195}
2196
2197
2198Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2199 JSObject* receiver,
2200 JSObject* holder,
2201 AccessorInfo* callback) {
2202 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002203 // -- eax : key
2204 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 Label miss;
2208
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2210
2211 // Check that the name has not changed.
2212 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2213 __ j(not_equal, &miss, not_taken);
2214
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002215 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002216 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002217 callback, name, &miss, &failure);
2218 if (!success) return failure;
2219
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 __ bind(&miss);
2221 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2222 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2223
2224 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002225 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226}
2227
2228
2229Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2230 JSObject* receiver,
2231 JSObject* holder,
2232 Object* value) {
2233 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002234 // -- eax : key
2235 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002238 Label miss;
2239
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002240 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2241
2242 // Check that the name has not changed.
2243 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2244 __ j(not_equal, &miss, not_taken);
2245
ager@chromium.org5c838252010-02-19 08:53:10 +00002246 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002247 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 __ bind(&miss);
2249 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2250 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2251
2252 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002253 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254}
2255
2256
2257Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2258 JSObject* holder,
2259 String* name) {
2260 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002261 // -- eax : key
2262 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002263 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002264 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002265 Label miss;
2266
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2268
2269 // Check that the name has not changed.
2270 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2271 __ j(not_equal, &miss, not_taken);
2272
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002273 LookupResult lookup;
2274 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002275 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002276 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002277 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002278 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002279 eax,
2280 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002281 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002282 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002283 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002284 __ bind(&miss);
2285 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2286 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2287
2288 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002289 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002290}
2291
2292
2293
2294
2295Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2296 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002297 // -- eax : key
2298 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002299 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002300 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002301 Label miss;
2302
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002303 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2304
2305 // Check that the name has not changed.
2306 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2307 __ j(not_equal, &miss, not_taken);
2308
ager@chromium.org5c838252010-02-19 08:53:10 +00002309 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002310 __ bind(&miss);
2311 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2312 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2313
2314 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002315 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002316}
2317
2318
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002319Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002320 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002321 // -- eax : key
2322 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002323 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002324 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002325 Label miss;
2326
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002327 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2328
2329 // Check that the name has not changed.
2330 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2331 __ j(not_equal, &miss, not_taken);
2332
ager@chromium.org5c838252010-02-19 08:53:10 +00002333 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002334 __ bind(&miss);
2335 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2336 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2337
2338 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002339 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002340}
2341
2342
2343Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2344 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002345 // -- eax : key
2346 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002347 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002348 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002349 Label miss;
2350
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002351 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2352
2353 // Check that the name has not changed.
2354 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2355 __ j(not_equal, &miss, not_taken);
2356
ager@chromium.org5c838252010-02-19 08:53:10 +00002357 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002358 __ bind(&miss);
2359 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2360 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2361
2362 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002363 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002364}
2365
2366
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002367// Specialized stub for constructing objects from functions which only have only
2368// simple assignments of the form this.x = ...; in their body.
2369Object* ConstructStubCompiler::CompileConstructStub(
2370 SharedFunctionInfo* shared) {
2371 // ----------- S t a t e -------------
2372 // -- eax : argc
2373 // -- edi : constructor
2374 // -- esp[0] : return address
2375 // -- esp[4] : last argument
2376 // -----------------------------------
2377 Label generic_stub_call;
2378#ifdef ENABLE_DEBUGGER_SUPPORT
2379 // Check to see whether there are any break points in the function code. If
2380 // there are jump to the generic constructor stub which calls the actual
2381 // code for the function thereby hitting the break points.
2382 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2383 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2384 __ cmp(ebx, Factory::undefined_value());
2385 __ j(not_equal, &generic_stub_call, not_taken);
2386#endif
2387
2388 // Load the initial map and verify that it is in fact a map.
2389 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2390 // Will both indicate a NULL and a Smi.
2391 __ test(ebx, Immediate(kSmiTagMask));
2392 __ j(zero, &generic_stub_call);
2393 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2394 __ j(not_equal, &generic_stub_call);
2395
2396#ifdef DEBUG
2397 // Cannot construct functions this way.
2398 // edi: constructor
2399 // ebx: initial map
2400 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2401 __ Assert(not_equal, "Function constructed by construct stub.");
2402#endif
2403
2404 // Now allocate the JSObject on the heap by moving the new space allocation
2405 // top forward.
2406 // edi: constructor
2407 // ebx: initial map
2408 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2409 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002410 __ AllocateInNewSpace(ecx,
2411 edx,
2412 ecx,
2413 no_reg,
2414 &generic_stub_call,
2415 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002416
2417 // Allocated the JSObject, now initialize the fields and add the heap tag.
2418 // ebx: initial map
2419 // edx: JSObject (untagged)
2420 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2421 __ mov(ebx, Factory::empty_fixed_array());
2422 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2423 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2424
2425 // Push the allocated object to the stack. This is the object that will be
2426 // returned (after it is tagged).
2427 __ push(edx);
2428
2429 // eax: argc
2430 // edx: JSObject (untagged)
2431 // Load the address of the first in-object property into edx.
2432 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2433 // Calculate the location of the first argument. The stack contains the
2434 // allocated object and the return address on top of the argc arguments.
2435 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2436
2437 // Use edi for holding undefined which is used in several places below.
2438 __ mov(edi, Factory::undefined_value());
2439
2440 // eax: argc
2441 // ecx: first argument
2442 // edx: first in-object property of the JSObject
2443 // edi: undefined
2444 // Fill the initialized properties with a constant value or a passed argument
2445 // depending on the this.x = ...; assignment in the function.
2446 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2447 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002448 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002449 // If argument is not passed the property is set to undefined,
2450 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002451 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002452 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002453 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002454 if (CpuFeatures::IsSupported(CMOV)) {
2455 CpuFeatures::Scope use_cmov(CMOV);
2456 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2457 } else {
2458 Label not_passed;
2459 __ j(below_equal, &not_passed);
2460 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2461 __ bind(&not_passed);
2462 }
2463 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002464 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002465 } else {
2466 // Set the property to the constant value.
2467 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2468 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2469 }
2470 }
2471
2472 // Fill the unused in-object property fields with undefined.
2473 for (int i = shared->this_property_assignments_count();
2474 i < shared->CalculateInObjectProperties();
2475 i++) {
2476 __ mov(Operand(edx, i * kPointerSize), edi);
2477 }
2478
2479 // Move argc to ebx and retrieve and tag the JSObject to return.
2480 __ mov(ebx, eax);
2481 __ pop(eax);
2482 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2483
2484 // Remove caller arguments and receiver from the stack and return.
2485 __ pop(ecx);
2486 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2487 __ push(ecx);
2488 __ IncrementCounter(&Counters::constructed_objects, 1);
2489 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2490 __ ret(0);
2491
2492 // Jump to the generic stub in case the specialized code cannot handle the
2493 // construction.
2494 __ bind(&generic_stub_call);
2495 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2496 Handle<Code> generic_construct_stub(code);
2497 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2498
2499 // Return the generated code.
2500 return GetCode();
2501}
2502
2503
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002504#undef __
2505
2506} } // namespace v8::internal