blob: e554a31c7166b3bd8055ac8253c48f213ae6a9d9 [file] [log] [blame]
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "ic-inl.h"
31#include "codegen-inl.h"
32#include "stub-cache.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.org65dad4b2009-04-23 08:48:43 +000037#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39
40static void ProbeTable(MacroAssembler* masm,
41 Code::Flags flags,
42 StubCache::Table table,
43 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000044 Register offset,
45 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046 ExternalReference key_offset(SCTableReference::keyReference(table));
47 ExternalReference value_offset(SCTableReference::valueReference(table));
48
49 Label miss;
50
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000051 if (extra.is_valid()) {
52 // Get the code entry from the cache.
53 __ mov(extra, Operand::StaticArray(offset, times_2, value_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055 // Check that the key in the entry matches the name.
56 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
57 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000059 // Check that the flags match what we're looking for.
60 __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
61 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
62 __ cmp(offset, flags);
63 __ j(not_equal, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000065 // Jump to the first instruction in the code stub.
66 __ add(Operand(extra), Immediate(Code::kHeaderSize - kHeapObjectTag));
67 __ jmp(Operand(extra));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000069 __ bind(&miss);
70 } else {
71 // Save the offset on the stack.
72 __ push(offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000074 // Check that the key in the entry matches the name.
75 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
76 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000078 // Get the code entry from the cache.
79 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
80
81 // Check that the flags match what we're looking for.
82 __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
83 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
84 __ cmp(offset, flags);
85 __ j(not_equal, &miss);
86
87 // Restore offset and re-load code entry from cache.
88 __ pop(offset);
89 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
90
91 // Jump to the first instruction in the code stub.
92 __ add(Operand(offset), Immediate(Code::kHeaderSize - kHeapObjectTag));
93 __ jmp(Operand(offset));
94
95 // Pop at miss.
96 __ bind(&miss);
97 __ pop(offset);
98 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099}
100
101
102void StubCache::GenerateProbe(MacroAssembler* masm,
103 Code::Flags flags,
104 Register receiver,
105 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000106 Register scratch,
107 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 Label miss;
109
110 // Make sure that code is valid. The shifting code relies on the
111 // entry size being 8.
112 ASSERT(sizeof(Entry) == 8);
113
114 // Make sure the flags does not name a specific type.
115 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
116
117 // Make sure that there are no register conflicts.
118 ASSERT(!scratch.is(receiver));
119 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000120 ASSERT(!extra.is(receiver));
121 ASSERT(!extra.is(name));
122 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
124 // Check that the receiver isn't a smi.
125 __ test(receiver, Immediate(kSmiTagMask));
126 __ j(zero, &miss, not_taken);
127
128 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000129 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000130 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 __ xor_(scratch, flags);
132 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
133
134 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000135 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000138 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000139 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
140 __ xor_(scratch, flags);
141 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 __ sub(scratch, Operand(name));
143 __ add(Operand(scratch), Immediate(flags));
144 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
145
146 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000147 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149 // Cache miss: Fall-through and let caller handle the miss by
150 // entering the runtime system.
151 __ bind(&miss);
152}
153
154
155void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
156 int index,
157 Register prototype) {
158 // Load the global or builtins object from the current context.
159 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
160 // Load the global context from the global or builtins object.
161 __ mov(prototype,
162 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
163 // Load the function from the global context.
164 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
165 // Load the initial map. The global functions all have initial maps.
166 __ mov(prototype,
167 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
168 // Load the prototype from the initial map.
169 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
170}
171
172
173void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
174 Register receiver,
175 Register scratch,
176 Label* miss_label) {
177 // Check that the receiver isn't a smi.
178 __ test(receiver, Immediate(kSmiTagMask));
179 __ j(zero, miss_label, not_taken);
180
181 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 __ j(not_equal, miss_label, not_taken);
184
185 // Load length directly from the JS array.
186 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
187 __ ret(0);
188}
189
190
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000191// Generate code to check if an object is a string. If the object is
192// a string, the map's instance type is left in the scratch register.
193static void GenerateStringCheck(MacroAssembler* masm,
194 Register receiver,
195 Register scratch,
196 Label* smi,
197 Label* non_string_object) {
198 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000200 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000202 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
204 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000205 ASSERT(kNotStringTag != 0);
206 __ test(scratch, Immediate(kNotStringTag));
207 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208}
209
210
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000211void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
212 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000213 Register scratch1,
214 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000215 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000216 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000218 // Check if the object is a string leaving the instance type in the
219 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000220 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000222 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000224 __ SmiTag(eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000225 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000226
227 // Check if the object is a JSValue wrapper.
228 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000229 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000230 __ j(not_equal, miss, not_taken);
231
232 // Check if the wrapped value is a string and load the length
233 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000234 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
235 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
236 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
237 __ SmiTag(eax);
238 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239}
240
241
242void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
243 Register receiver,
244 Register scratch1,
245 Register scratch2,
246 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000247 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000248 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000251
ager@chromium.org7c537e22008-10-16 08:43:32 +0000252
253// Load a fast property out of a holder object (src). In-object properties
254// are loaded directly otherwise the property is loaded from the properties
255// fixed array.
256void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000257 Register dst, Register src,
258 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000259 // Adjust for the number of properties stored in the holder.
260 index -= holder->map()->inobject_properties();
261 if (index < 0) {
262 // Get the property straight out of the holder.
263 int offset = holder->map()->instance_size() + (index * kPointerSize);
264 __ mov(dst, FieldOperand(src, offset));
265 } else {
266 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000267 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
269 __ mov(dst, FieldOperand(dst, offset));
270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271}
272
273
ager@chromium.org5c838252010-02-19 08:53:10 +0000274static void PushInterceptorArguments(MacroAssembler* masm,
275 Register receiver,
276 Register holder,
277 Register name,
278 JSObject* holder_obj) {
279 __ push(receiver);
280 __ push(holder);
281 __ push(name);
282 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
283 ASSERT(!Heap::InNewSpace(interceptor));
284 __ mov(receiver, Immediate(Handle<Object>(interceptor)));
285 __ push(receiver);
286 __ push(FieldOperand(receiver, InterceptorInfo::kDataOffset));
287}
288
289
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000290static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
291 Register receiver,
292 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000293 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000294 JSObject* holder_obj) {
295 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000296 __ CallExternalReference(
297 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
298 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000299}
300
301
302template <class Compiler>
303static void CompileLoadInterceptor(Compiler* compiler,
304 StubCompiler* stub_compiler,
305 MacroAssembler* masm,
306 JSObject* object,
307 JSObject* holder,
308 String* name,
309 LookupResult* lookup,
310 Register receiver,
311 Register scratch1,
312 Register scratch2,
313 Label* miss) {
314 ASSERT(holder->HasNamedInterceptor());
315 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
316
317 // Check that the receiver isn't a smi.
318 __ test(receiver, Immediate(kSmiTagMask));
319 __ j(zero, miss, not_taken);
320
321 // Check that the maps haven't changed.
322 Register reg =
323 stub_compiler->CheckPrototypes(object, receiver, holder,
324 scratch1, scratch2, name, miss);
325
ager@chromium.org5c838252010-02-19 08:53:10 +0000326 if (lookup->IsProperty() && lookup->IsCacheable()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000327 compiler->CompileCacheable(masm,
328 stub_compiler,
329 receiver,
330 reg,
331 scratch1,
332 scratch2,
333 holder,
334 lookup,
335 name,
336 miss);
337 } else {
338 compiler->CompileRegular(masm,
339 receiver,
340 reg,
341 scratch2,
342 holder,
343 miss);
344 }
345}
346
347
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000348class LoadInterceptorCompiler BASE_EMBEDDED {
349 public:
350 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
351
352 void CompileCacheable(MacroAssembler* masm,
353 StubCompiler* stub_compiler,
354 Register receiver,
355 Register holder,
356 Register scratch1,
357 Register scratch2,
358 JSObject* holder_obj,
359 LookupResult* lookup,
360 String* name,
361 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000362 AccessorInfo* callback = NULL;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000363 bool optimize = false;
364 // So far the most popular follow ups for interceptor loads are FIELD
365 // and CALLBACKS, so inline only them, other cases may be added
366 // later.
367 if (lookup->type() == FIELD) {
368 optimize = true;
369 } else if (lookup->type() == CALLBACKS) {
370 Object* callback_object = lookup->GetCallbackObject();
371 if (callback_object->IsAccessorInfo()) {
372 callback = AccessorInfo::cast(callback_object);
373 optimize = callback->getter() != NULL;
374 }
375 }
376
377 if (!optimize) {
378 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
379 return;
380 }
381
382 // Note: starting a frame here makes GC aware of pointers pushed below.
383 __ EnterInternalFrame();
384
385 if (lookup->type() == CALLBACKS) {
386 __ push(receiver);
387 }
388 __ push(holder);
389 __ push(name_);
390
391 CompileCallLoadPropertyWithInterceptor(masm,
392 receiver,
393 holder,
394 name_,
395 holder_obj);
396
397 Label interceptor_failed;
398 __ cmp(eax, Factory::no_interceptor_result_sentinel());
399 __ j(equal, &interceptor_failed);
400 __ LeaveInternalFrame();
401 __ ret(0);
402
403 __ bind(&interceptor_failed);
404 __ pop(name_);
405 __ pop(holder);
406 if (lookup->type() == CALLBACKS) {
407 __ pop(receiver);
408 }
409
410 __ LeaveInternalFrame();
411
412 if (lookup->type() == FIELD) {
413 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
414 lookup->holder(), scratch1,
415 scratch2,
416 name,
417 miss_label);
418 stub_compiler->GenerateFastPropertyLoad(masm, eax,
419 holder, lookup->holder(),
420 lookup->GetFieldIndex());
421 __ ret(0);
422 } else {
423 ASSERT(lookup->type() == CALLBACKS);
424 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
425 ASSERT(callback != NULL);
426 ASSERT(callback->getter() != NULL);
427
428 Label cleanup;
429 __ pop(scratch2);
430 __ push(receiver);
431 __ push(scratch2);
432
433 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
434 lookup->holder(), scratch1,
435 scratch2,
436 name,
437 &cleanup);
438
439 __ pop(scratch2); // save old return address
440 __ push(holder);
441 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
442 __ push(holder);
443 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
444 __ push(name_);
445 __ push(scratch2); // restore old return address
446
447 ExternalReference ref =
448 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000449 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000450
451 __ bind(&cleanup);
452 __ pop(scratch1);
453 __ pop(scratch2);
454 __ push(scratch1);
455 }
456 }
457
458
459 void CompileRegular(MacroAssembler* masm,
460 Register receiver,
461 Register holder,
462 Register scratch,
463 JSObject* holder_obj,
464 Label* miss_label) {
465 __ pop(scratch); // save old return address
466 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
467 __ push(scratch); // restore old return address
468
469 ExternalReference ref = ExternalReference(
470 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000471 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000472 }
473
474 private:
475 Register name_;
476};
477
478
ager@chromium.org5c838252010-02-19 08:53:10 +0000479// Holds information about possible function call optimizations.
480class CallOptimization BASE_EMBEDDED {
481 public:
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000482 explicit CallOptimization(LookupResult* lookup) {
483 if (!lookup->IsProperty() || !lookup->IsCacheable() ||
484 lookup->type() != CONSTANT_FUNCTION) {
485 Initialize(NULL);
486 } else {
487 // We only optimize constant function calls.
488 Initialize(lookup->GetConstantFunction());
489 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000490 }
491
492 explicit CallOptimization(JSFunction* function) {
493 Initialize(function);
494 }
495
496 bool is_constant_call() const {
497 return constant_function_ != NULL;
498 }
499
500 JSFunction* constant_function() const {
501 ASSERT(constant_function_ != NULL);
502 return constant_function_;
503 }
504
505 bool is_simple_api_call() const {
506 return is_simple_api_call_;
507 }
508
509 FunctionTemplateInfo* expected_receiver_type() const {
510 ASSERT(is_simple_api_call_);
511 return expected_receiver_type_;
512 }
513
514 CallHandlerInfo* api_call_info() const {
515 ASSERT(is_simple_api_call_);
516 return api_call_info_;
517 }
518
519 // Returns the depth of the object having the expected type in the
520 // prototype chain between the two arguments.
521 int GetPrototypeDepthOfExpectedType(JSObject* object,
522 JSObject* holder) const {
523 ASSERT(is_simple_api_call_);
524 if (expected_receiver_type_ == NULL) return 0;
525 int depth = 0;
526 while (object != holder) {
527 if (object->IsInstanceOf(expected_receiver_type_)) return depth;
528 object = JSObject::cast(object->GetPrototype());
529 ++depth;
530 }
531 if (holder->IsInstanceOf(expected_receiver_type_)) return depth;
532 return kInvalidProtoDepth;
533 }
534
535 private:
536 void Initialize(JSFunction* function) {
sgjesse@chromium.org534ae572010-02-24 22:09:39 +0000537 constant_function_ = NULL;
538 is_simple_api_call_ = false;
539 expected_receiver_type_ = NULL;
540 api_call_info_ = NULL;
541
542 if (function == NULL || !function->is_compiled()) return;
ager@chromium.org5c838252010-02-19 08:53:10 +0000543
544 constant_function_ = function;
ager@chromium.org5c838252010-02-19 08:53:10 +0000545 AnalyzePossibleApiFunction(function);
546 }
547
548 // Determines whether the given function can be called using the
549 // fast api call builtin.
550 void AnalyzePossibleApiFunction(JSFunction* function) {
551 SharedFunctionInfo* sfi = function->shared();
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000552 if (!sfi->IsApiFunction()) return;
553 FunctionTemplateInfo* info = sfi->get_api_func_data();
ager@chromium.org5c838252010-02-19 08:53:10 +0000554
555 // Require a C++ callback.
556 if (info->call_code()->IsUndefined()) return;
557 api_call_info_ = CallHandlerInfo::cast(info->call_code());
558
559 // Accept signatures that either have no restrictions at all or
560 // only have restrictions on the receiver.
561 if (!info->signature()->IsUndefined()) {
562 SignatureInfo* signature = SignatureInfo::cast(info->signature());
563 if (!signature->args()->IsUndefined()) return;
564 if (!signature->receiver()->IsUndefined()) {
565 expected_receiver_type_ =
566 FunctionTemplateInfo::cast(signature->receiver());
567 }
568 }
569
570 is_simple_api_call_ = true;
571 }
572
573 JSFunction* constant_function_;
574 bool is_simple_api_call_;
575 FunctionTemplateInfo* expected_receiver_type_;
576 CallHandlerInfo* api_call_info_;
577};
578
579
580// Reserves space for the extra arguments to FastHandleApiCall in the
581// caller's frame.
582//
583// These arguments are set by CheckPrototypes and GenerateFastApiCall.
584static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
585 // ----------- S t a t e -------------
586 // -- esp[0] : return address
587 // -- esp[4] : last argument in the internal frame of the caller
588 // -----------------------------------
589 __ pop(scratch);
590 __ push(Immediate(Smi::FromInt(0)));
591 __ push(Immediate(Smi::FromInt(0)));
592 __ push(Immediate(Smi::FromInt(0)));
593 __ push(Immediate(Smi::FromInt(0)));
594 __ push(scratch);
595}
596
597
598// Undoes the effects of ReserveSpaceForFastApiCall.
599static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
600 // ----------- S t a t e -------------
601 // -- esp[0] : return address
602 // -- esp[4] : last fast api call extra argument
603 // -- ...
604 // -- esp[16] : first fast api call extra argument
605 // -- esp[20] : last argument in the internal frame
606 // -----------------------------------
607 __ pop(scratch);
608 __ add(Operand(esp), Immediate(kPointerSize * 4));
609 __ push(scratch);
610}
611
612
613// Generates call to FastHandleApiCall builtin.
614static void GenerateFastApiCall(MacroAssembler* masm,
615 const CallOptimization& optimization,
616 int argc) {
617 // ----------- S t a t e -------------
618 // -- esp[0] : return address
619 // -- esp[4] : object passing the type check
620 // (last fast api call extra argument,
621 // set by CheckPrototypes)
622 // -- esp[8] : api call data
623 // -- esp[12] : api callback
624 // -- esp[16] : api function
625 // (first fast api call extra argument)
626 // -- esp[20] : last argument
627 // -- ...
628 // -- esp[(argc + 5) * 4] : first argument
629 // -- esp[(argc + 6) * 4] : receiver
630 // -----------------------------------
631
632 // Get the function and setup the context.
633 JSFunction* function = optimization.constant_function();
634 __ mov(edi, Immediate(Handle<JSFunction>(function)));
635 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
636
637 // Pass the additional arguments FastHandleApiCall expects.
638 __ mov(Operand(esp, 4 * kPointerSize), edi);
639 bool info_loaded = false;
640 Object* callback = optimization.api_call_info()->callback();
641 if (Heap::InNewSpace(callback)) {
642 info_loaded = true;
643 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
644 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
645 __ mov(Operand(esp, 3 * kPointerSize), ebx);
646 } else {
647 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
648 }
649 Object* call_data = optimization.api_call_info()->data();
650 if (Heap::InNewSpace(call_data)) {
651 if (!info_loaded) {
652 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
653 }
654 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
655 __ mov(Operand(esp, 2 * kPointerSize), ebx);
656 } else {
657 __ mov(Operand(esp, 2 * kPointerSize),
658 Immediate(Handle<Object>(call_data)));
659 }
660
661 // Set the number of arguments.
662 __ mov(eax, Immediate(argc + 4));
663
664 // Jump to the fast api call builtin (tail call).
665 Handle<Code> code = Handle<Code>(
666 Builtins::builtin(Builtins::FastHandleApiCall));
667 ParameterCount expected(0);
668 __ InvokeCode(code, expected, expected,
669 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
670}
671
672
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000673class CallInterceptorCompiler BASE_EMBEDDED {
674 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000675 CallInterceptorCompiler(StubCompiler* stub_compiler,
676 const ParameterCount& arguments,
677 Register name)
678 : stub_compiler_(stub_compiler),
679 arguments_(arguments),
680 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000681
ager@chromium.org5c838252010-02-19 08:53:10 +0000682 void Compile(MacroAssembler* masm,
683 JSObject* object,
684 JSObject* holder,
685 String* name,
686 LookupResult* lookup,
687 Register receiver,
688 Register scratch1,
689 Register scratch2,
690 Label* miss) {
691 ASSERT(holder->HasNamedInterceptor());
692 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
693
694 // Check that the receiver isn't a smi.
695 __ test(receiver, Immediate(kSmiTagMask));
696 __ j(zero, miss, not_taken);
697
698 CallOptimization optimization(lookup);
699
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000700 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000701 CompileCacheable(masm,
702 object,
703 receiver,
704 scratch1,
705 scratch2,
706 holder,
707 lookup,
708 name,
709 optimization,
710 miss);
711 } else {
712 CompileRegular(masm,
713 object,
714 receiver,
715 scratch1,
716 scratch2,
717 name,
718 holder,
719 miss);
720 }
721 }
722
723 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000724 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000725 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000726 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000727 Register scratch1,
728 Register scratch2,
729 JSObject* holder_obj,
730 LookupResult* lookup,
731 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000732 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000733 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000734 ASSERT(optimization.is_constant_call());
735 ASSERT(!lookup->holder()->IsGlobalObject());
736
737 int depth1 = kInvalidProtoDepth;
738 int depth2 = kInvalidProtoDepth;
739 bool can_do_fast_api_call = false;
740 if (optimization.is_simple_api_call() &&
741 !lookup->holder()->IsGlobalObject()) {
742 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
743 if (depth1 == kInvalidProtoDepth) {
744 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
745 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000746 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000747 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
748 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000749 }
750
ager@chromium.org5c838252010-02-19 08:53:10 +0000751 __ IncrementCounter(&Counters::call_const_interceptor, 1);
752
753 if (can_do_fast_api_call) {
754 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
755 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000756 }
757
ager@chromium.org5c838252010-02-19 08:53:10 +0000758 Label miss_cleanup;
759 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
760 Register holder =
761 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
762 scratch1, scratch2, name,
763 depth1, miss);
764
765 Label regular_invoke;
766 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
767
768 // Generate code for the failed interceptor case.
769
770 // Check the lookup is still valid.
771 stub_compiler_->CheckPrototypes(holder_obj, receiver,
772 lookup->holder(),
773 scratch1, scratch2, name,
774 depth2, miss);
775
776 if (can_do_fast_api_call) {
777 GenerateFastApiCall(masm, optimization, arguments_.immediate());
778 } else {
779 __ InvokeFunction(optimization.constant_function(), arguments_,
780 JUMP_FUNCTION);
781 }
782
783 if (can_do_fast_api_call) {
784 __ bind(&miss_cleanup);
785 FreeSpaceForFastApiCall(masm, scratch1);
786 __ jmp(miss_label);
787 }
788
789 __ bind(&regular_invoke);
790 if (can_do_fast_api_call) {
791 FreeSpaceForFastApiCall(masm, scratch1);
792 }
793 }
794
795 void CompileRegular(MacroAssembler* masm,
796 JSObject* object,
797 Register receiver,
798 Register scratch1,
799 Register scratch2,
800 String* name,
801 JSObject* holder_obj,
802 Label* miss_label) {
803 Register holder =
804 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
805 scratch1, scratch2, name,
806 miss_label);
807
808 __ EnterInternalFrame();
809 // Save the name_ register across the call.
810 __ push(name_);
811
812 PushInterceptorArguments(masm,
813 receiver,
814 holder,
815 name_,
816 holder_obj);
817
818 __ CallExternalReference(
819 ExternalReference(
820 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
821 5);
822
823 // Restore the name_ register.
824 __ pop(name_);
825 __ LeaveInternalFrame();
826 }
827
828 void LoadWithInterceptor(MacroAssembler* masm,
829 Register receiver,
830 Register holder,
831 JSObject* holder_obj,
832 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000833 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000834 __ push(holder); // Save the holder.
835 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000836
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000837 CompileCallLoadPropertyWithInterceptor(masm,
838 receiver,
839 holder,
840 name_,
841 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000842
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000843 __ pop(name_); // Restore the name.
844 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000845 __ LeaveInternalFrame();
846
847 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000848 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000849 }
850
ager@chromium.org5c838252010-02-19 08:53:10 +0000851 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000852 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000853 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000854};
855
856
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000857void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
858 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
859 Code* code = NULL;
860 if (kind == Code::LOAD_IC) {
861 code = Builtins::builtin(Builtins::LoadIC_Miss);
862 } else {
863 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
864 }
865
866 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000867 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000868}
869
870
ager@chromium.org5c838252010-02-19 08:53:10 +0000871// Both name_reg and receiver_reg are preserved on jumps to miss_label,
872// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873void StubCompiler::GenerateStoreField(MacroAssembler* masm,
874 JSObject* object,
875 int index,
876 Map* transition,
877 Register receiver_reg,
878 Register name_reg,
879 Register scratch,
880 Label* miss_label) {
881 // Check that the object isn't a smi.
882 __ test(receiver_reg, Immediate(kSmiTagMask));
883 __ j(zero, miss_label, not_taken);
884
885 // Check that the map of the object hasn't changed.
886 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
887 Immediate(Handle<Map>(object->map())));
888 __ j(not_equal, miss_label, not_taken);
889
890 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000891 if (object->IsJSGlobalProxy()) {
892 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000893 }
894
895 // Stub never generated for non-global objects that require access
896 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000897 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000898
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000899 // Perform map transition for the receiver if necessary.
900 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
901 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000902 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000903 __ pop(scratch); // Return address.
904 __ push(receiver_reg);
905 __ push(Immediate(Handle<Map>(transition)));
906 __ push(eax);
907 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000908 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000909 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000910 return;
911 }
912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000914 // Update the map of the object; no write barrier updating is
915 // needed because the map is never in new space.
916 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
917 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000918 }
919
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000920 // Adjust for the number of properties stored in the object. Even in the
921 // face of a transition we can use the old map here because the size of the
922 // object and the number of in-object properties is not going to change.
923 index -= object->map()->inobject_properties();
924
ager@chromium.org7c537e22008-10-16 08:43:32 +0000925 if (index < 0) {
926 // Set the property straight into the object.
927 int offset = object->map()->instance_size() + (index * kPointerSize);
928 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000929
ager@chromium.org7c537e22008-10-16 08:43:32 +0000930 // Update the write barrier for the array address.
931 // Pass the value being stored in the now unused name_reg.
932 __ mov(name_reg, Operand(eax));
933 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
934 } else {
935 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000936 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000937 // Get the properties array (optimistically).
938 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000939 __ mov(FieldOperand(scratch, offset), eax);
940
941 // Update the write barrier for the array address.
942 // Pass the value being stored in the now unused name_reg.
943 __ mov(name_reg, Operand(eax));
944 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
945 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000946
947 // Return the value (register eax).
948 __ ret(0);
949}
950
951
952#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000953#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000954
955
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000956Register StubCompiler::CheckPrototypes(JSObject* object,
957 Register object_reg,
958 JSObject* holder,
959 Register holder_reg,
960 Register scratch,
961 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000962 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000963 Label* miss) {
964 // Check that the maps haven't changed.
965 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000966 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
967 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000968
969 // If we've skipped any global objects, it's not enough to verify
970 // that their maps haven't changed.
971 while (object != holder) {
972 if (object->IsGlobalObject()) {
973 GlobalObject* global = GlobalObject::cast(object);
974 Object* probe = global->EnsurePropertyCell(name);
975 if (probe->IsFailure()) {
976 set_failure(Failure::cast(probe));
977 return result;
978 }
979 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
980 ASSERT(cell->value()->IsTheHole());
981 __ mov(scratch, Immediate(Handle<Object>(cell)));
982 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
983 Immediate(Factory::the_hole_value()));
984 __ j(not_equal, miss, not_taken);
985 }
986 object = JSObject::cast(object->GetPrototype());
987 }
988
ager@chromium.org5c838252010-02-19 08:53:10 +0000989 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000990 return result;
991}
992
993
994void StubCompiler::GenerateLoadField(JSObject* object,
995 JSObject* holder,
996 Register receiver,
997 Register scratch1,
998 Register scratch2,
999 int index,
1000 String* name,
1001 Label* miss) {
1002 // Check that the receiver isn't a smi.
1003 __ test(receiver, Immediate(kSmiTagMask));
1004 __ j(zero, miss, not_taken);
1005
1006 // Check the prototype chain.
1007 Register reg =
1008 CheckPrototypes(object, receiver, holder,
1009 scratch1, scratch2, name, miss);
1010
1011 // Get the value from the properties.
1012 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1013 __ ret(0);
1014}
1015
1016
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001017bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001018 JSObject* holder,
1019 Register receiver,
1020 Register name_reg,
1021 Register scratch1,
1022 Register scratch2,
1023 AccessorInfo* callback,
1024 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001025 Label* miss,
1026 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001027 // Check that the receiver isn't a smi.
1028 __ test(receiver, Immediate(kSmiTagMask));
1029 __ j(zero, miss, not_taken);
1030
1031 // Check that the maps haven't changed.
1032 Register reg =
1033 CheckPrototypes(object, receiver, holder,
1034 scratch1, scratch2, name, miss);
1035
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001036 Handle<AccessorInfo> callback_handle(callback);
1037
1038 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1039 __ EnterInternalFrame();
1040 __ PushHandleScope(other);
1041 // Push the stack address where the list of arguments ends
1042 __ mov(other, esp);
1043 __ sub(Operand(other), Immediate(2 * kPointerSize));
1044 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001045 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001046 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001047 __ mov(other, Immediate(callback_handle));
1048 __ push(other);
1049 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001050 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001051 // Save a pointer to where we pushed the arguments pointer.
1052 // This will be passed as the const Arguments& to the C++ callback.
1053 __ mov(eax, esp);
1054 __ add(Operand(eax), Immediate(5 * kPointerSize));
1055 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001056
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001057 // Do call through the api.
1058 ASSERT_EQ(6, ApiGetterEntryStub::kStackSpace);
1059 Address getter_address = v8::ToCData<Address>(callback->getter());
1060 ApiFunction fun(getter_address);
1061 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001062 // Emitting a stub call may try to allocate (if the code is not
1063 // already generated). Do not allow the assembler to perform a
1064 // garbage collection but instead return the allocation failure
1065 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001066 Object* result = masm()->TryCallStub(&stub);
1067 if (result->IsFailure()) {
1068 *failure = Failure::cast(result);
1069 return false;
1070 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001071
1072 // We need to avoid using eax since that now holds the result.
1073 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001074 // Emitting PopHandleScope may try to allocate. Do not allow the
1075 // assembler to perform a garbage collection but instead return a
1076 // failure object.
1077 result = masm()->TryPopHandleScope(eax, tmp);
1078 if (result->IsFailure()) {
1079 *failure = Failure::cast(result);
1080 return false;
1081 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001082 __ LeaveInternalFrame();
1083
1084 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001085 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001086}
1087
1088
1089void StubCompiler::GenerateLoadConstant(JSObject* object,
1090 JSObject* holder,
1091 Register receiver,
1092 Register scratch1,
1093 Register scratch2,
1094 Object* value,
1095 String* name,
1096 Label* miss) {
1097 // Check that the receiver isn't a smi.
1098 __ test(receiver, Immediate(kSmiTagMask));
1099 __ j(zero, miss, not_taken);
1100
1101 // Check that the maps haven't changed.
1102 Register reg =
1103 CheckPrototypes(object, receiver, holder,
1104 scratch1, scratch2, name, miss);
1105
1106 // Return the constant value.
1107 __ mov(eax, Handle<Object>(value));
1108 __ ret(0);
1109}
1110
1111
1112void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1113 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001114 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001115 Register receiver,
1116 Register name_reg,
1117 Register scratch1,
1118 Register scratch2,
1119 String* name,
1120 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001121 LoadInterceptorCompiler compiler(name_reg);
1122 CompileLoadInterceptor(&compiler,
1123 this,
1124 masm(),
1125 object,
1126 holder,
1127 name,
1128 lookup,
1129 receiver,
1130 scratch1,
1131 scratch2,
1132 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001133}
1134
1135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001136// TODO(1241006): Avoid having lazy compile stubs specialized by the
1137// number of arguments. It is not needed anymore.
1138Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001139 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001140 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141
1142 // Push a copy of the function onto the stack.
1143 __ push(edi);
1144
1145 __ push(edi); // function is also the parameter to the runtime call
1146 __ CallRuntime(Runtime::kLazyCompile, 1);
1147 __ pop(edi);
1148
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001149 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001150 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151
1152 // Do a tail-call of the compiled function.
1153 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1154 __ jmp(Operand(ecx));
1155
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001156 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001157}
1158
1159
ager@chromium.org5c838252010-02-19 08:53:10 +00001160Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001161 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001162 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001163 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001164 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001165 // -- ecx : name
1166 // -- esp[0] : return address
1167 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1168 // -- ...
1169 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001170 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001171 Label miss;
1172
1173 // Get the receiver from the stack.
1174 const int argc = arguments().immediate();
1175 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1176
1177 // Check that the receiver isn't a smi.
1178 __ test(edx, Immediate(kSmiTagMask));
1179 __ j(zero, &miss, not_taken);
1180
1181 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001182 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001183
ager@chromium.org7c537e22008-10-16 08:43:32 +00001184 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
1186 // Check that the function really is a function.
1187 __ test(edi, Immediate(kSmiTagMask));
1188 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001189 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 __ j(not_equal, &miss, not_taken);
1191
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001192 // Patch the receiver on the stack with the global proxy if
1193 // necessary.
1194 if (object->IsGlobalObject()) {
1195 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1196 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1197 }
1198
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001199 // Invoke the function.
1200 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1201
1202 // Handle call cache miss.
1203 __ bind(&miss);
1204 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001205 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001206
1207 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001208 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001209}
1210
1211
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001212Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1213 JSObject* holder,
1214 JSFunction* function,
1215 String* name,
1216 CheckType check) {
1217 // ----------- S t a t e -------------
1218 // -- ecx : name
1219 // -- esp[0] : return address
1220 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1221 // -- ...
1222 // -- esp[(argc + 1) * 4] : receiver
1223 // -----------------------------------
1224 ASSERT(check == RECEIVER_MAP_CHECK);
1225
1226 Label miss;
1227
1228 // Get the receiver from the stack.
1229 const int argc = arguments().immediate();
1230 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1231
1232 // Check that the receiver isn't a smi.
1233 __ test(edx, Immediate(kSmiTagMask));
1234 __ j(zero, &miss);
1235
1236 CheckPrototypes(JSObject::cast(object), edx,
1237 holder, ebx,
1238 eax, name, &miss);
1239
1240 if (argc == 0) {
1241 // Noop, return the length.
1242 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1243 __ ret((argc + 1) * kPointerSize);
1244 } else {
1245 // Get the elements array of the object.
1246 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1247
1248 // Check that the elements are in fast mode (not dictionary).
1249 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1250 Immediate(Factory::fixed_array_map()));
1251 __ j(not_equal, &miss);
1252
1253 if (argc == 1) { // Otherwise fall through to call builtin.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001254 Label call_builtin, exit, with_rset_update,
1255 attempt_to_grow_elements, finish_push;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001256
1257 // Get the array's length into eax and calculate new length.
1258 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1259 STATIC_ASSERT(kSmiTagSize == 1);
1260 STATIC_ASSERT(kSmiTag == 0);
1261 __ add(Operand(eax), Immediate(argc << 1));
1262
1263 // Get the element's length into ecx.
1264 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1265 __ SmiTag(ecx);
1266
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001267 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001268 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001269 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001270
1271 // Save new length.
1272 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1273
1274 // Push the element.
1275 __ lea(edx, FieldOperand(ebx,
1276 eax, times_half_pointer_size,
1277 FixedArray::kHeaderSize - argc * kPointerSize));
1278 __ mov(ecx, Operand(esp, argc * kPointerSize));
1279 __ mov(Operand(edx, 0), ecx);
1280
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001281 __ bind(&finish_push);
1282
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001283 // Check if value is a smi.
1284 __ test(ecx, Immediate(kSmiTagMask));
1285 __ j(not_zero, &with_rset_update);
1286
1287 __ bind(&exit);
1288 __ ret((argc + 1) * kPointerSize);
1289
1290 __ bind(&with_rset_update);
1291
1292 __ InNewSpace(ebx, ecx, equal, &exit);
1293
1294 RecordWriteStub stub(ebx, edx, ecx);
1295 __ CallStub(&stub);
1296 __ ret((argc + 1) * kPointerSize);
1297
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001298 __ bind(&attempt_to_grow_elements);
1299 ExternalReference new_space_allocation_top =
1300 ExternalReference::new_space_allocation_top_address();
1301 ExternalReference new_space_allocation_limit =
1302 ExternalReference::new_space_allocation_limit_address();
1303
1304 const int kAllocationDelta = 4;
1305 // Load top.
1306 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1307
1308 // Check if it's the end of elements.
1309 __ lea(edx, FieldOperand(ebx,
1310 eax, times_half_pointer_size,
1311 FixedArray::kHeaderSize - argc * kPointerSize));
1312 __ cmp(edx, Operand(ecx));
1313 __ j(not_equal, &call_builtin);
1314 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1315 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
1316 __ j(greater, &call_builtin);
1317
1318 // We fit and could grow elements.
1319 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1320 __ mov(ecx, Operand(esp, argc * kPointerSize));
1321 __ mov(Operand(edx, 0), ecx);
1322 for (int i = 1; i < kAllocationDelta; i++) {
1323 __ mov(Operand(edx, i * kPointerSize),
1324 Immediate(Factory::undefined_value()));
1325 }
1326
1327 // Restore receiver to edx as finish sequence assumes it's here.
1328 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1329
1330 // Increment element's and array's sizes.
1331 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1332 Immediate(kAllocationDelta));
1333 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1334
1335 __ jmp(&finish_push);
1336
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001337 __ bind(&call_builtin);
1338 }
1339
1340 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1341 argc + 1,
1342 1);
1343 }
1344
1345 __ bind(&miss);
1346
1347 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1348 __ jmp(ic, RelocInfo::CODE_TARGET);
1349
1350 // Return the generated code.
1351 String* function_name = NULL;
1352 if (function->shared()->name()->IsString()) {
1353 function_name = String::cast(function->shared()->name());
1354 }
1355 return GetCode(CONSTANT_FUNCTION, function_name);
1356}
1357
1358
1359Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1360 JSObject* holder,
1361 JSFunction* function,
1362 String* name,
1363 CheckType check) {
1364 // ----------- S t a t e -------------
1365 // -- ecx : name
1366 // -- esp[0] : return address
1367 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1368 // -- ...
1369 // -- esp[(argc + 1) * 4] : receiver
1370 // -----------------------------------
1371 ASSERT(check == RECEIVER_MAP_CHECK);
1372
1373 Label miss, empty_array, call_builtin;
1374
1375 // Get the receiver from the stack.
1376 const int argc = arguments().immediate();
1377 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1378
1379 // Check that the receiver isn't a smi.
1380 __ test(edx, Immediate(kSmiTagMask));
1381 __ j(zero, &miss);
1382
1383 CheckPrototypes(JSObject::cast(object), edx,
1384 holder, ebx,
1385 eax, name, &miss);
1386
1387 // Get the elements array of the object.
1388 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1389
1390 // Check that the elements are in fast mode (not dictionary).
1391 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1392 Immediate(Factory::fixed_array_map()));
1393 __ j(not_equal, &miss);
1394
1395 // Get the array's length into ecx and calculate new length.
1396 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1397 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1398 __ j(negative, &empty_array);
1399
1400 // Get the last element.
1401 STATIC_ASSERT(kSmiTagSize == 1);
1402 STATIC_ASSERT(kSmiTag == 0);
1403 __ mov(eax, FieldOperand(ebx,
1404 ecx, times_half_pointer_size,
1405 FixedArray::kHeaderSize));
1406 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1407 __ j(equal, &call_builtin);
1408
1409 // Set the array's length.
1410 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1411
1412 // Fill with the hole.
1413 __ mov(FieldOperand(ebx,
1414 ecx, times_half_pointer_size,
1415 FixedArray::kHeaderSize),
1416 Immediate(Factory::the_hole_value()));
1417 __ ret((argc + 1) * kPointerSize);
1418
1419 __ bind(&empty_array);
1420 __ mov(eax, Immediate(Factory::undefined_value()));
1421 __ ret((argc + 1) * kPointerSize);
1422
1423 __ bind(&call_builtin);
1424
1425 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1426 argc + 1,
1427 1);
1428
1429 __ bind(&miss);
1430
1431 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1432 __ jmp(ic, RelocInfo::CODE_TARGET);
1433
1434 // Return the generated code.
1435 String* function_name = NULL;
1436 if (function->shared()->name()->IsString()) {
1437 function_name = String::cast(function->shared()->name());
1438 }
1439 return GetCode(CONSTANT_FUNCTION, function_name);
1440}
1441
1442
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001443Object* CallStubCompiler::CompileCallConstant(Object* object,
1444 JSObject* holder,
1445 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001446 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001447 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001448 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001449 // -- ecx : name
1450 // -- esp[0] : return address
1451 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1452 // -- ...
1453 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001454 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001455
1456 SharedFunctionInfo* function_info = function->shared();
1457 if (function_info->HasCustomCallGenerator()) {
1458 CustomCallGenerator generator =
1459 ToCData<CustomCallGenerator>(function_info->function_data());
1460 return generator(this, object, holder, function, name, check);
1461 }
1462
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001463 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001464
1465 // Get the receiver from the stack.
1466 const int argc = arguments().immediate();
1467 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1468
1469 // Check that the receiver isn't a smi.
1470 if (check != NUMBER_CHECK) {
1471 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001472 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001473 }
1474
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001475 // Make sure that it's okay not to patch the on stack receiver
1476 // unless we're doing a receiver map check.
1477 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1478
ager@chromium.org5c838252010-02-19 08:53:10 +00001479 CallOptimization optimization(function);
1480 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001481 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001482
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001483 switch (check) {
1484 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001485 __ IncrementCounter(&Counters::call_const, 1);
1486
1487 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1488 depth = optimization.GetPrototypeDepthOfExpectedType(
1489 JSObject::cast(object), holder);
1490 }
1491
1492 if (depth != kInvalidProtoDepth) {
1493 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1494 ReserveSpaceForFastApiCall(masm(), eax);
1495 }
1496
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001497 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001498 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001499 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001500
1501 // Patch the receiver on the stack with the global proxy if
1502 // necessary.
1503 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001504 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001505 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1506 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1507 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 break;
1509
1510 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001511 if (!function->IsBuiltin()) {
1512 // Calling non-builtins with a value as receiver requires boxing.
1513 __ jmp(&miss);
1514 } else {
1515 // Check that the object is a string or a symbol.
1516 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1517 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1518 __ cmp(eax, FIRST_NONSTRING_TYPE);
1519 __ j(above_equal, &miss, not_taken);
1520 // Check that the maps starting from the prototype haven't changed.
1521 GenerateLoadGlobalFunctionPrototype(masm(),
1522 Context::STRING_FUNCTION_INDEX,
1523 eax);
1524 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1525 ebx, edx, name, &miss);
1526 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001527 break;
1528
1529 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001530 if (!function->IsBuiltin()) {
1531 // Calling non-builtins with a value as receiver requires boxing.
1532 __ jmp(&miss);
1533 } else {
1534 Label fast;
1535 // Check that the object is a smi or a heap number.
1536 __ test(edx, Immediate(kSmiTagMask));
1537 __ j(zero, &fast, taken);
1538 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1539 __ j(not_equal, &miss, not_taken);
1540 __ bind(&fast);
1541 // Check that the maps starting from the prototype haven't changed.
1542 GenerateLoadGlobalFunctionPrototype(masm(),
1543 Context::NUMBER_FUNCTION_INDEX,
1544 eax);
1545 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1546 ebx, edx, name, &miss);
1547 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 break;
1549 }
1550
1551 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001552 if (!function->IsBuiltin()) {
1553 // Calling non-builtins with a value as receiver requires boxing.
1554 __ jmp(&miss);
1555 } else {
1556 Label fast;
1557 // Check that the object is a boolean.
1558 __ cmp(edx, Factory::true_value());
1559 __ j(equal, &fast, taken);
1560 __ cmp(edx, Factory::false_value());
1561 __ j(not_equal, &miss, not_taken);
1562 __ bind(&fast);
1563 // Check that the maps starting from the prototype haven't changed.
1564 GenerateLoadGlobalFunctionPrototype(masm(),
1565 Context::BOOLEAN_FUNCTION_INDEX,
1566 eax);
1567 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1568 ebx, edx, name, &miss);
1569 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 break;
1571 }
1572
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001573 default:
1574 UNREACHABLE();
1575 }
1576
ager@chromium.org5c838252010-02-19 08:53:10 +00001577 if (depth != kInvalidProtoDepth) {
1578 GenerateFastApiCall(masm(), optimization, argc);
1579 } else {
1580 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1581 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001582
1583 // Handle call cache miss.
1584 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001585 if (depth != kInvalidProtoDepth) {
1586 FreeSpaceForFastApiCall(masm(), eax);
1587 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001588 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001589 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001590 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001591
1592 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001593 String* function_name = NULL;
1594 if (function->shared()->name()->IsString()) {
1595 function_name = String::cast(function->shared()->name());
1596 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001597 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598}
1599
1600
ager@chromium.org5c838252010-02-19 08:53:10 +00001601Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001602 JSObject* holder,
1603 String* name) {
1604 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001605 // -- ecx : name
1606 // -- esp[0] : return address
1607 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1608 // -- ...
1609 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 Label miss;
1612
1613 // Get the number of arguments.
1614 const int argc = arguments().immediate();
1615
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001616 LookupResult lookup;
1617 LookupPostInterceptor(holder, name, &lookup);
1618
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001619 // Get the receiver from the stack.
1620 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001621
ager@chromium.org5c838252010-02-19 08:53:10 +00001622 CallInterceptorCompiler compiler(this, arguments(), ecx);
1623 compiler.Compile(masm(),
1624 object,
1625 holder,
1626 name,
1627 &lookup,
1628 edx,
1629 ebx,
1630 edi,
1631 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001632
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001633 // Restore receiver.
1634 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001635
1636 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001637 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001638 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001639 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 __ j(not_equal, &miss, not_taken);
1641
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001642 // Patch the receiver on the stack with the global proxy if
1643 // necessary.
1644 if (object->IsGlobalObject()) {
1645 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1646 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1647 }
1648
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001649 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001650 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001651 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1652
1653 // Handle load cache miss.
1654 __ bind(&miss);
1655 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001656 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001657
1658 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001659 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660}
1661
1662
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001663Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1664 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001665 JSGlobalPropertyCell* cell,
1666 JSFunction* function,
1667 String* name) {
1668 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001669 // -- ecx : name
1670 // -- esp[0] : return address
1671 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1672 // -- ...
1673 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001674 // -----------------------------------
1675 Label miss;
1676
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001677 // Get the number of arguments.
1678 const int argc = arguments().immediate();
1679
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001680 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001681 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001682
1683 // If the object is the holder then we know that it's a global
1684 // object which can only happen for contextual calls. In this case,
1685 // the receiver cannot be a smi.
1686 if (object != holder) {
1687 __ test(edx, Immediate(kSmiTagMask));
1688 __ j(zero, &miss, not_taken);
1689 }
1690
1691 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001692 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001693
1694 // Get the value from the cell.
1695 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1696 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1697
1698 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001699 if (Heap::InNewSpace(function)) {
1700 // We can't embed a pointer to a function in new space so we have
1701 // to verify that the shared function info is unchanged. This has
1702 // the nice side effect that multiple closures based on the same
1703 // function can all use this call IC. Before we load through the
1704 // function, we have to verify that it still is a function.
1705 __ test(edi, Immediate(kSmiTagMask));
1706 __ j(zero, &miss, not_taken);
1707 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1708 __ j(not_equal, &miss, not_taken);
1709
1710 // Check the shared function info. Make sure it hasn't changed.
1711 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1712 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1713 __ j(not_equal, &miss, not_taken);
1714 } else {
1715 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1716 __ j(not_equal, &miss, not_taken);
1717 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001718
1719 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001720 if (object->IsGlobalObject()) {
1721 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1722 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1723 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001724
1725 // Setup the context (function already in edi).
1726 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1727
1728 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001729 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001730 ASSERT(function->is_compiled());
1731 Handle<Code> code(function->code());
1732 ParameterCount expected(function->shared()->formal_parameter_count());
1733 __ InvokeCode(code, expected, arguments(),
1734 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1735
1736 // Handle call cache miss.
1737 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001738 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1739 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1740 __ jmp(ic, RelocInfo::CODE_TARGET);
1741
1742 // Return the generated code.
1743 return GetCode(NORMAL, name);
1744}
1745
1746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1748 int index,
1749 Map* transition,
1750 String* name) {
1751 // ----------- S t a t e -------------
1752 // -- eax : value
1753 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001754 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001756 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 Label miss;
1758
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001760 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001761 object,
1762 index,
1763 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001764 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001765 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766
1767 // Handle store cache miss.
1768 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001769 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001771 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772
1773 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001774 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001775}
1776
1777
1778Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1779 AccessorInfo* callback,
1780 String* name) {
1781 // ----------- S t a t e -------------
1782 // -- eax : value
1783 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001784 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001785 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 Label miss;
1788
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001790 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001791 __ j(zero, &miss, not_taken);
1792
1793 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001794 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 Immediate(Handle<Map>(object->map())));
1796 __ j(not_equal, &miss, not_taken);
1797
1798 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001799 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001800 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001801 }
1802
1803 // Stub never generated for non-global objects that require access
1804 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001805 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001806
1807 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001808 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1810 __ push(ecx); // name
1811 __ push(eax); // value
1812 __ push(ebx); // restore return address
1813
mads.s.ager31e71382008-08-13 09:32:07 +00001814 // Do tail-call to the runtime system.
1815 ExternalReference store_callback_property =
1816 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001817 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001818
1819 // Handle store cache miss.
1820 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001821 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001822 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823
1824 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001825 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001826}
1827
1828
1829Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1830 String* name) {
1831 // ----------- S t a t e -------------
1832 // -- eax : value
1833 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001834 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 Label miss;
1838
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001839 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001840 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 __ j(zero, &miss, not_taken);
1842
1843 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001844 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 Immediate(Handle<Map>(receiver->map())));
1846 __ j(not_equal, &miss, not_taken);
1847
1848 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001849 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001850 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 }
1852
1853 // Stub never generated for non-global objects that require access
1854 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001855 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001856
1857 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001858 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 __ push(ecx); // name
1860 __ push(eax); // value
1861 __ push(ebx); // restore return address
1862
mads.s.ager31e71382008-08-13 09:32:07 +00001863 // Do tail-call to the runtime system.
1864 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001865 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001866 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001867
1868 // Handle store cache miss.
1869 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001870 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001871 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872
1873 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875}
1876
1877
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001878Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1879 JSGlobalPropertyCell* cell,
1880 String* name) {
1881 // ----------- S t a t e -------------
1882 // -- eax : value
1883 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001884 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001885 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001886 // -----------------------------------
1887 Label miss;
1888
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001889 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001890 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001891 Immediate(Handle<Map>(object->map())));
1892 __ j(not_equal, &miss, not_taken);
1893
1894 // Store the value in the cell.
1895 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1896 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1897
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001898 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001899 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001900 __ ret(0);
1901
1902 // Handle store cache miss.
1903 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001904 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1905 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1906 __ jmp(ic, RelocInfo::CODE_TARGET);
1907
1908 // Return the generated code.
1909 return GetCode(NORMAL, name);
1910}
1911
1912
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1914 int index,
1915 Map* transition,
1916 String* name) {
1917 // ----------- S t a t e -------------
1918 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001919 // -- ecx : key
1920 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001922 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001923 Label miss;
1924
1925 __ IncrementCounter(&Counters::keyed_store_field, 1);
1926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 // Check that the name has not changed.
1928 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1929 __ j(not_equal, &miss, not_taken);
1930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001932 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001933 object,
1934 index,
1935 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001936 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001937 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938
1939 // Handle store cache miss.
1940 __ bind(&miss);
1941 __ DecrementCounter(&Counters::keyed_store_field, 1);
1942 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001943 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944
1945 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001946 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947}
1948
1949
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001950
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001951Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1952 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 int index,
1954 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001956 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 // -- ecx : name
1958 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 Label miss;
1961
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001962 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 __ bind(&miss);
1964 GenerateLoadMiss(masm(), Code::LOAD_IC);
1965
1966 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968}
1969
1970
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001971Object* LoadStubCompiler::CompileLoadCallback(String* name,
1972 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001974 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001975 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001976 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 // -- ecx : name
1978 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 Label miss;
1981
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001982 Failure* failure = Failure::InternalError();
1983 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1984 callback, name, &miss, &failure);
1985 if (!success) return failure;
1986
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001987 __ bind(&miss);
1988 GenerateLoadMiss(masm(), Code::LOAD_IC);
1989
1990 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001991 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992}
1993
1994
1995Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1996 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001997 Object* value,
1998 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001999 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002000 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001 // -- ecx : name
2002 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002004 Label miss;
2005
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002006 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 __ bind(&miss);
2008 GenerateLoadMiss(masm(), Code::LOAD_IC);
2009
2010 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002011 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002012}
2013
2014
2015Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2016 JSObject* holder,
2017 String* name) {
2018 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002019 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002020 // -- ecx : name
2021 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002022 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023 Label miss;
2024
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002025 LookupResult lookup;
2026 LookupPostInterceptor(holder, name, &lookup);
2027
ager@chromium.orge2902be2009-06-08 12:21:35 +00002028 // TODO(368): Compile in the whole chain: all the interceptors in
2029 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002030 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002031 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002032 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002033 eax,
2034 ecx,
2035 edx,
2036 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002037 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002038 &miss);
2039
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002040 __ bind(&miss);
2041 GenerateLoadMiss(masm(), Code::LOAD_IC);
2042
2043 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002044 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002045}
2046
2047
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002048Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2049 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002050 JSGlobalPropertyCell* cell,
2051 String* name,
2052 bool is_dont_delete) {
2053 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002054 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002055 // -- ecx : name
2056 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002057 // -----------------------------------
2058 Label miss;
2059
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002060 // If the object is the holder then we know that it's a global
2061 // object which can only happen for contextual loads. In this case,
2062 // the receiver cannot be a smi.
2063 if (object != holder) {
2064 __ test(eax, Immediate(kSmiTagMask));
2065 __ j(zero, &miss, not_taken);
2066 }
2067
2068 // Check that the maps haven't changed.
2069 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002070
2071 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002072 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2073 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002074
2075 // Check for deleted property if property can actually be deleted.
2076 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002077 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002078 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002079 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002080 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002081 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002082 }
2083
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002084 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002085 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002086 __ ret(0);
2087
2088 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002089 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2090 GenerateLoadMiss(masm(), Code::LOAD_IC);
2091
2092 // Return the generated code.
2093 return GetCode(NORMAL, name);
2094}
2095
2096
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002097Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2098 JSObject* receiver,
2099 JSObject* holder,
2100 int index) {
2101 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002102 // -- eax : key
2103 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 Label miss;
2107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002108 __ IncrementCounter(&Counters::keyed_load_field, 1);
2109
2110 // Check that the name has not changed.
2111 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2112 __ j(not_equal, &miss, not_taken);
2113
ager@chromium.org5c838252010-02-19 08:53:10 +00002114 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002115
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002116 __ bind(&miss);
2117 __ DecrementCounter(&Counters::keyed_load_field, 1);
2118 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2119
2120 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002121 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122}
2123
2124
2125Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2126 JSObject* receiver,
2127 JSObject* holder,
2128 AccessorInfo* callback) {
2129 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002130 // -- eax : key
2131 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002132 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 Label miss;
2135
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002136 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2137
2138 // Check that the name has not changed.
2139 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2140 __ j(not_equal, &miss, not_taken);
2141
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002142 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002143 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002144 callback, name, &miss, &failure);
2145 if (!success) return failure;
2146
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 __ bind(&miss);
2148 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2149 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2150
2151 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002152 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002153}
2154
2155
2156Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2157 JSObject* receiver,
2158 JSObject* holder,
2159 Object* value) {
2160 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002161 // -- eax : key
2162 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002163 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002165 Label miss;
2166
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2168
2169 // Check that the name has not changed.
2170 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2171 __ j(not_equal, &miss, not_taken);
2172
ager@chromium.org5c838252010-02-19 08:53:10 +00002173 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002174 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175 __ bind(&miss);
2176 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2177 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2178
2179 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002180 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181}
2182
2183
2184Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2185 JSObject* holder,
2186 String* name) {
2187 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002188 // -- eax : key
2189 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002190 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002191 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002192 Label miss;
2193
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002194 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2195
2196 // Check that the name has not changed.
2197 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2198 __ j(not_equal, &miss, not_taken);
2199
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002200 LookupResult lookup;
2201 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002202 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002203 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002204 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002205 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002206 eax,
2207 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002208 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002209 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002210 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211 __ bind(&miss);
2212 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2213 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2214
2215 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002216 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217}
2218
2219
2220
2221
2222Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2223 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002224 // -- eax : key
2225 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002226 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002227 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002228 Label miss;
2229
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2231
2232 // Check that the name has not changed.
2233 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2234 __ j(not_equal, &miss, not_taken);
2235
ager@chromium.org5c838252010-02-19 08:53:10 +00002236 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237 __ bind(&miss);
2238 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2239 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2240
2241 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002242 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243}
2244
2245
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002246Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002248 // -- eax : key
2249 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002250 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002251 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002252 Label miss;
2253
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002254 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2255
2256 // Check that the name has not changed.
2257 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2258 __ j(not_equal, &miss, not_taken);
2259
ager@chromium.org5c838252010-02-19 08:53:10 +00002260 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261 __ bind(&miss);
2262 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2263 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2264
2265 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002266 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002267}
2268
2269
2270Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2271 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002272 // -- eax : key
2273 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002274 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002275 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002276 Label miss;
2277
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002278 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2279
2280 // Check that the name has not changed.
2281 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2282 __ j(not_equal, &miss, not_taken);
2283
ager@chromium.org5c838252010-02-19 08:53:10 +00002284 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002285 __ bind(&miss);
2286 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2287 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2288
2289 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002290 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002291}
2292
2293
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002294// Specialized stub for constructing objects from functions which only have only
2295// simple assignments of the form this.x = ...; in their body.
2296Object* ConstructStubCompiler::CompileConstructStub(
2297 SharedFunctionInfo* shared) {
2298 // ----------- S t a t e -------------
2299 // -- eax : argc
2300 // -- edi : constructor
2301 // -- esp[0] : return address
2302 // -- esp[4] : last argument
2303 // -----------------------------------
2304 Label generic_stub_call;
2305#ifdef ENABLE_DEBUGGER_SUPPORT
2306 // Check to see whether there are any break points in the function code. If
2307 // there are jump to the generic constructor stub which calls the actual
2308 // code for the function thereby hitting the break points.
2309 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2310 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2311 __ cmp(ebx, Factory::undefined_value());
2312 __ j(not_equal, &generic_stub_call, not_taken);
2313#endif
2314
2315 // Load the initial map and verify that it is in fact a map.
2316 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2317 // Will both indicate a NULL and a Smi.
2318 __ test(ebx, Immediate(kSmiTagMask));
2319 __ j(zero, &generic_stub_call);
2320 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2321 __ j(not_equal, &generic_stub_call);
2322
2323#ifdef DEBUG
2324 // Cannot construct functions this way.
2325 // edi: constructor
2326 // ebx: initial map
2327 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2328 __ Assert(not_equal, "Function constructed by construct stub.");
2329#endif
2330
2331 // Now allocate the JSObject on the heap by moving the new space allocation
2332 // top forward.
2333 // edi: constructor
2334 // ebx: initial map
2335 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2336 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002337 __ AllocateInNewSpace(ecx,
2338 edx,
2339 ecx,
2340 no_reg,
2341 &generic_stub_call,
2342 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002343
2344 // Allocated the JSObject, now initialize the fields and add the heap tag.
2345 // ebx: initial map
2346 // edx: JSObject (untagged)
2347 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2348 __ mov(ebx, Factory::empty_fixed_array());
2349 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2350 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2351
2352 // Push the allocated object to the stack. This is the object that will be
2353 // returned (after it is tagged).
2354 __ push(edx);
2355
2356 // eax: argc
2357 // edx: JSObject (untagged)
2358 // Load the address of the first in-object property into edx.
2359 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2360 // Calculate the location of the first argument. The stack contains the
2361 // allocated object and the return address on top of the argc arguments.
2362 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2363
2364 // Use edi for holding undefined which is used in several places below.
2365 __ mov(edi, Factory::undefined_value());
2366
2367 // eax: argc
2368 // ecx: first argument
2369 // edx: first in-object property of the JSObject
2370 // edi: undefined
2371 // Fill the initialized properties with a constant value or a passed argument
2372 // depending on the this.x = ...; assignment in the function.
2373 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2374 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002375 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002376 // If argument is not passed the property is set to undefined,
2377 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002378 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002379 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002380 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002381 if (CpuFeatures::IsSupported(CMOV)) {
2382 CpuFeatures::Scope use_cmov(CMOV);
2383 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2384 } else {
2385 Label not_passed;
2386 __ j(below_equal, &not_passed);
2387 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2388 __ bind(&not_passed);
2389 }
2390 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002391 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002392 } else {
2393 // Set the property to the constant value.
2394 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2395 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2396 }
2397 }
2398
2399 // Fill the unused in-object property fields with undefined.
2400 for (int i = shared->this_property_assignments_count();
2401 i < shared->CalculateInObjectProperties();
2402 i++) {
2403 __ mov(Operand(edx, i * kPointerSize), edi);
2404 }
2405
2406 // Move argc to ebx and retrieve and tag the JSObject to return.
2407 __ mov(ebx, eax);
2408 __ pop(eax);
2409 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2410
2411 // Remove caller arguments and receiver from the stack and return.
2412 __ pop(ecx);
2413 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2414 __ push(ecx);
2415 __ IncrementCounter(&Counters::constructed_objects, 1);
2416 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2417 __ ret(0);
2418
2419 // Jump to the generic stub in case the specialized code cannot handle the
2420 // construction.
2421 __ bind(&generic_stub_call);
2422 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2423 Handle<Code> generic_construct_stub(code);
2424 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2425
2426 // Return the generated code.
2427 return GetCode();
2428}
2429
2430
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002431#undef __
2432
2433} } // namespace v8::internal