blob: 902a5b81065a0ecfef42f537c49b9b951f110475 [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.
1254 Label call_builtin, exit, with_rset_update;
1255
1256 // Get the array's length into eax and calculate new length.
1257 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1258 STATIC_ASSERT(kSmiTagSize == 1);
1259 STATIC_ASSERT(kSmiTag == 0);
1260 __ add(Operand(eax), Immediate(argc << 1));
1261
1262 // Get the element's length into ecx.
1263 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1264 __ SmiTag(ecx);
1265
1266 // Check if we could survive without allocation, go to builtin otherwise.
1267 __ cmp(eax, Operand(ecx));
1268 __ j(greater, &call_builtin);
1269
1270 // Save new length.
1271 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1272
1273 // Push the element.
1274 __ lea(edx, FieldOperand(ebx,
1275 eax, times_half_pointer_size,
1276 FixedArray::kHeaderSize - argc * kPointerSize));
1277 __ mov(ecx, Operand(esp, argc * kPointerSize));
1278 __ mov(Operand(edx, 0), ecx);
1279
1280 // Check if value is a smi.
1281 __ test(ecx, Immediate(kSmiTagMask));
1282 __ j(not_zero, &with_rset_update);
1283
1284 __ bind(&exit);
1285 __ ret((argc + 1) * kPointerSize);
1286
1287 __ bind(&with_rset_update);
1288
1289 __ InNewSpace(ebx, ecx, equal, &exit);
1290
1291 RecordWriteStub stub(ebx, edx, ecx);
1292 __ CallStub(&stub);
1293 __ ret((argc + 1) * kPointerSize);
1294
1295 __ bind(&call_builtin);
1296 }
1297
1298 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1299 argc + 1,
1300 1);
1301 }
1302
1303 __ bind(&miss);
1304
1305 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1306 __ jmp(ic, RelocInfo::CODE_TARGET);
1307
1308 // Return the generated code.
1309 String* function_name = NULL;
1310 if (function->shared()->name()->IsString()) {
1311 function_name = String::cast(function->shared()->name());
1312 }
1313 return GetCode(CONSTANT_FUNCTION, function_name);
1314}
1315
1316
1317Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1318 JSObject* holder,
1319 JSFunction* function,
1320 String* name,
1321 CheckType check) {
1322 // ----------- S t a t e -------------
1323 // -- ecx : name
1324 // -- esp[0] : return address
1325 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1326 // -- ...
1327 // -- esp[(argc + 1) * 4] : receiver
1328 // -----------------------------------
1329 ASSERT(check == RECEIVER_MAP_CHECK);
1330
1331 Label miss, empty_array, call_builtin;
1332
1333 // Get the receiver from the stack.
1334 const int argc = arguments().immediate();
1335 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1336
1337 // Check that the receiver isn't a smi.
1338 __ test(edx, Immediate(kSmiTagMask));
1339 __ j(zero, &miss);
1340
1341 CheckPrototypes(JSObject::cast(object), edx,
1342 holder, ebx,
1343 eax, name, &miss);
1344
1345 // Get the elements array of the object.
1346 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1347
1348 // Check that the elements are in fast mode (not dictionary).
1349 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1350 Immediate(Factory::fixed_array_map()));
1351 __ j(not_equal, &miss);
1352
1353 // Get the array's length into ecx and calculate new length.
1354 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1355 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
1356 __ j(negative, &empty_array);
1357
1358 // Get the last element.
1359 STATIC_ASSERT(kSmiTagSize == 1);
1360 STATIC_ASSERT(kSmiTag == 0);
1361 __ mov(eax, FieldOperand(ebx,
1362 ecx, times_half_pointer_size,
1363 FixedArray::kHeaderSize));
1364 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1365 __ j(equal, &call_builtin);
1366
1367 // Set the array's length.
1368 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1369
1370 // Fill with the hole.
1371 __ mov(FieldOperand(ebx,
1372 ecx, times_half_pointer_size,
1373 FixedArray::kHeaderSize),
1374 Immediate(Factory::the_hole_value()));
1375 __ ret((argc + 1) * kPointerSize);
1376
1377 __ bind(&empty_array);
1378 __ mov(eax, Immediate(Factory::undefined_value()));
1379 __ ret((argc + 1) * kPointerSize);
1380
1381 __ bind(&call_builtin);
1382
1383 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1384 argc + 1,
1385 1);
1386
1387 __ bind(&miss);
1388
1389 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1390 __ jmp(ic, RelocInfo::CODE_TARGET);
1391
1392 // Return the generated code.
1393 String* function_name = NULL;
1394 if (function->shared()->name()->IsString()) {
1395 function_name = String::cast(function->shared()->name());
1396 }
1397 return GetCode(CONSTANT_FUNCTION, function_name);
1398}
1399
1400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401Object* CallStubCompiler::CompileCallConstant(Object* object,
1402 JSObject* holder,
1403 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001404 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001405 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001406 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001407 // -- ecx : name
1408 // -- esp[0] : return address
1409 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1410 // -- ...
1411 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001412 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001413
1414 SharedFunctionInfo* function_info = function->shared();
1415 if (function_info->HasCustomCallGenerator()) {
1416 CustomCallGenerator generator =
1417 ToCData<CustomCallGenerator>(function_info->function_data());
1418 return generator(this, object, holder, function, name, check);
1419 }
1420
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001421 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001422
1423 // Get the receiver from the stack.
1424 const int argc = arguments().immediate();
1425 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1426
1427 // Check that the receiver isn't a smi.
1428 if (check != NUMBER_CHECK) {
1429 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001430 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001431 }
1432
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001433 // Make sure that it's okay not to patch the on stack receiver
1434 // unless we're doing a receiver map check.
1435 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1436
ager@chromium.org5c838252010-02-19 08:53:10 +00001437 CallOptimization optimization(function);
1438 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001439 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001440
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001441 switch (check) {
1442 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001443 __ IncrementCounter(&Counters::call_const, 1);
1444
1445 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1446 depth = optimization.GetPrototypeDepthOfExpectedType(
1447 JSObject::cast(object), holder);
1448 }
1449
1450 if (depth != kInvalidProtoDepth) {
1451 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1452 ReserveSpaceForFastApiCall(masm(), eax);
1453 }
1454
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001456 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001457 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001458
1459 // Patch the receiver on the stack with the global proxy if
1460 // necessary.
1461 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001462 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001463 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1464 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1465 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 break;
1467
1468 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001469 if (!function->IsBuiltin()) {
1470 // Calling non-builtins with a value as receiver requires boxing.
1471 __ jmp(&miss);
1472 } else {
1473 // Check that the object is a string or a symbol.
1474 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1475 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1476 __ cmp(eax, FIRST_NONSTRING_TYPE);
1477 __ j(above_equal, &miss, not_taken);
1478 // Check that the maps starting from the prototype haven't changed.
1479 GenerateLoadGlobalFunctionPrototype(masm(),
1480 Context::STRING_FUNCTION_INDEX,
1481 eax);
1482 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1483 ebx, edx, name, &miss);
1484 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001485 break;
1486
1487 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001488 if (!function->IsBuiltin()) {
1489 // Calling non-builtins with a value as receiver requires boxing.
1490 __ jmp(&miss);
1491 } else {
1492 Label fast;
1493 // Check that the object is a smi or a heap number.
1494 __ test(edx, Immediate(kSmiTagMask));
1495 __ j(zero, &fast, taken);
1496 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1497 __ j(not_equal, &miss, not_taken);
1498 __ bind(&fast);
1499 // Check that the maps starting from the prototype haven't changed.
1500 GenerateLoadGlobalFunctionPrototype(masm(),
1501 Context::NUMBER_FUNCTION_INDEX,
1502 eax);
1503 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1504 ebx, edx, name, &miss);
1505 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001506 break;
1507 }
1508
1509 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001510 if (!function->IsBuiltin()) {
1511 // Calling non-builtins with a value as receiver requires boxing.
1512 __ jmp(&miss);
1513 } else {
1514 Label fast;
1515 // Check that the object is a boolean.
1516 __ cmp(edx, Factory::true_value());
1517 __ j(equal, &fast, taken);
1518 __ cmp(edx, Factory::false_value());
1519 __ j(not_equal, &miss, not_taken);
1520 __ bind(&fast);
1521 // Check that the maps starting from the prototype haven't changed.
1522 GenerateLoadGlobalFunctionPrototype(masm(),
1523 Context::BOOLEAN_FUNCTION_INDEX,
1524 eax);
1525 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1526 ebx, edx, name, &miss);
1527 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001528 break;
1529 }
1530
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 default:
1532 UNREACHABLE();
1533 }
1534
ager@chromium.org5c838252010-02-19 08:53:10 +00001535 if (depth != kInvalidProtoDepth) {
1536 GenerateFastApiCall(masm(), optimization, argc);
1537 } else {
1538 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1539 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001540
1541 // Handle call cache miss.
1542 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001543 if (depth != kInvalidProtoDepth) {
1544 FreeSpaceForFastApiCall(masm(), eax);
1545 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001546 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001548 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
1550 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001551 String* function_name = NULL;
1552 if (function->shared()->name()->IsString()) {
1553 function_name = String::cast(function->shared()->name());
1554 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001555 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556}
1557
1558
ager@chromium.org5c838252010-02-19 08:53:10 +00001559Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 JSObject* holder,
1561 String* name) {
1562 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001563 // -- ecx : name
1564 // -- esp[0] : return address
1565 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1566 // -- ...
1567 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001569 Label miss;
1570
1571 // Get the number of arguments.
1572 const int argc = arguments().immediate();
1573
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001574 LookupResult lookup;
1575 LookupPostInterceptor(holder, name, &lookup);
1576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 // Get the receiver from the stack.
1578 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001579
ager@chromium.org5c838252010-02-19 08:53:10 +00001580 CallInterceptorCompiler compiler(this, arguments(), ecx);
1581 compiler.Compile(masm(),
1582 object,
1583 holder,
1584 name,
1585 &lookup,
1586 edx,
1587 ebx,
1588 edi,
1589 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001590
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001591 // Restore receiver.
1592 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
1594 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001595 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001597 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598 __ j(not_equal, &miss, not_taken);
1599
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001600 // Patch the receiver on the stack with the global proxy if
1601 // necessary.
1602 if (object->IsGlobalObject()) {
1603 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1604 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1605 }
1606
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001607 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001608 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001609 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1610
1611 // Handle load cache miss.
1612 __ bind(&miss);
1613 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001614 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001615
1616 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001617 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001618}
1619
1620
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001621Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1622 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001623 JSGlobalPropertyCell* cell,
1624 JSFunction* function,
1625 String* name) {
1626 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001627 // -- ecx : name
1628 // -- esp[0] : return address
1629 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1630 // -- ...
1631 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001632 // -----------------------------------
1633 Label miss;
1634
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001635 // Get the number of arguments.
1636 const int argc = arguments().immediate();
1637
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001638 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001639 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001640
1641 // If the object is the holder then we know that it's a global
1642 // object which can only happen for contextual calls. In this case,
1643 // the receiver cannot be a smi.
1644 if (object != holder) {
1645 __ test(edx, Immediate(kSmiTagMask));
1646 __ j(zero, &miss, not_taken);
1647 }
1648
1649 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001650 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001651
1652 // Get the value from the cell.
1653 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1654 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1655
1656 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001657 if (Heap::InNewSpace(function)) {
1658 // We can't embed a pointer to a function in new space so we have
1659 // to verify that the shared function info is unchanged. This has
1660 // the nice side effect that multiple closures based on the same
1661 // function can all use this call IC. Before we load through the
1662 // function, we have to verify that it still is a function.
1663 __ test(edi, Immediate(kSmiTagMask));
1664 __ j(zero, &miss, not_taken);
1665 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1666 __ j(not_equal, &miss, not_taken);
1667
1668 // Check the shared function info. Make sure it hasn't changed.
1669 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1670 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1671 __ j(not_equal, &miss, not_taken);
1672 } else {
1673 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1674 __ j(not_equal, &miss, not_taken);
1675 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001676
1677 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001678 if (object->IsGlobalObject()) {
1679 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1680 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1681 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001682
1683 // Setup the context (function already in edi).
1684 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1685
1686 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001687 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001688 ASSERT(function->is_compiled());
1689 Handle<Code> code(function->code());
1690 ParameterCount expected(function->shared()->formal_parameter_count());
1691 __ InvokeCode(code, expected, arguments(),
1692 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1693
1694 // Handle call cache miss.
1695 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001696 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1697 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1698 __ jmp(ic, RelocInfo::CODE_TARGET);
1699
1700 // Return the generated code.
1701 return GetCode(NORMAL, name);
1702}
1703
1704
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1706 int index,
1707 Map* transition,
1708 String* name) {
1709 // ----------- S t a t e -------------
1710 // -- eax : value
1711 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001712 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 Label miss;
1716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001718 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001719 object,
1720 index,
1721 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001723 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
1725 // Handle store cache miss.
1726 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001727 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001728 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001729 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001730
1731 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001732 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001733}
1734
1735
1736Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1737 AccessorInfo* callback,
1738 String* name) {
1739 // ----------- S t a t e -------------
1740 // -- eax : value
1741 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001742 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001745 Label miss;
1746
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001747 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001748 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 __ j(zero, &miss, not_taken);
1750
1751 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001752 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 Immediate(Handle<Map>(object->map())));
1754 __ j(not_equal, &miss, not_taken);
1755
1756 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001757 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001758 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 }
1760
1761 // Stub never generated for non-global objects that require access
1762 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001763 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764
1765 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001766 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1768 __ push(ecx); // name
1769 __ push(eax); // value
1770 __ push(ebx); // restore return address
1771
mads.s.ager31e71382008-08-13 09:32:07 +00001772 // Do tail-call to the runtime system.
1773 ExternalReference store_callback_property =
1774 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001775 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001776
1777 // Handle store cache miss.
1778 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001780 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781
1782 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001783 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784}
1785
1786
1787Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1788 String* name) {
1789 // ----------- S t a t e -------------
1790 // -- eax : value
1791 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001792 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001794 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795 Label miss;
1796
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001797 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001798 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001799 __ j(zero, &miss, not_taken);
1800
1801 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001802 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 Immediate(Handle<Map>(receiver->map())));
1804 __ j(not_equal, &miss, not_taken);
1805
1806 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001807 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001808 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809 }
1810
1811 // Stub never generated for non-global objects that require access
1812 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001813 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001814
1815 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001816 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001817 __ push(ecx); // name
1818 __ push(eax); // value
1819 __ push(ebx); // restore return address
1820
mads.s.ager31e71382008-08-13 09:32:07 +00001821 // Do tail-call to the runtime system.
1822 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001823 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001824 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825
1826 // Handle store cache miss.
1827 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001828 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001829 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001830
1831 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001832 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001833}
1834
1835
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001836Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1837 JSGlobalPropertyCell* cell,
1838 String* name) {
1839 // ----------- S t a t e -------------
1840 // -- eax : value
1841 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001842 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001843 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001844 // -----------------------------------
1845 Label miss;
1846
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001847 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001848 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001849 Immediate(Handle<Map>(object->map())));
1850 __ j(not_equal, &miss, not_taken);
1851
1852 // Store the value in the cell.
1853 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1854 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1855
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001856 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001857 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001858 __ ret(0);
1859
1860 // Handle store cache miss.
1861 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001862 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1863 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1864 __ jmp(ic, RelocInfo::CODE_TARGET);
1865
1866 // Return the generated code.
1867 return GetCode(NORMAL, name);
1868}
1869
1870
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001871Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1872 int index,
1873 Map* transition,
1874 String* name) {
1875 // ----------- S t a t e -------------
1876 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001877 // -- ecx : key
1878 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001879 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001880 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001881 Label miss;
1882
1883 __ IncrementCounter(&Counters::keyed_store_field, 1);
1884
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 // Check that the name has not changed.
1886 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1887 __ j(not_equal, &miss, not_taken);
1888
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001889 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001890 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001891 object,
1892 index,
1893 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001894 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001895 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896
1897 // Handle store cache miss.
1898 __ bind(&miss);
1899 __ DecrementCounter(&Counters::keyed_store_field, 1);
1900 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001901 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902
1903 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001904 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001905}
1906
1907
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001908
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001909Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1910 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001911 int index,
1912 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001914 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 // -- ecx : name
1916 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001917 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001918 Label miss;
1919
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001920 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921 __ bind(&miss);
1922 GenerateLoadMiss(masm(), Code::LOAD_IC);
1923
1924 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001925 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001926}
1927
1928
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001929Object* LoadStubCompiler::CompileLoadCallback(String* name,
1930 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001932 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001934 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 // -- ecx : name
1936 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001937 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938 Label miss;
1939
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001940 Failure* failure = Failure::InternalError();
1941 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1942 callback, name, &miss, &failure);
1943 if (!success) return failure;
1944
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 __ bind(&miss);
1946 GenerateLoadMiss(masm(), Code::LOAD_IC);
1947
1948 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001949 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950}
1951
1952
1953Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1954 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001955 Object* value,
1956 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001958 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 // -- ecx : name
1960 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962 Label miss;
1963
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001964 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001965 __ bind(&miss);
1966 GenerateLoadMiss(masm(), Code::LOAD_IC);
1967
1968 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001969 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970}
1971
1972
1973Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1974 JSObject* holder,
1975 String* name) {
1976 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001977 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 // -- ecx : name
1979 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001980 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001981 Label miss;
1982
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001983 LookupResult lookup;
1984 LookupPostInterceptor(holder, name, &lookup);
1985
ager@chromium.orge2902be2009-06-08 12:21:35 +00001986 // TODO(368): Compile in the whole chain: all the interceptors in
1987 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001988 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001989 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001990 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001991 eax,
1992 ecx,
1993 edx,
1994 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001995 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001996 &miss);
1997
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001998 __ bind(&miss);
1999 GenerateLoadMiss(masm(), Code::LOAD_IC);
2000
2001 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002002 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002003}
2004
2005
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002006Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2007 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002008 JSGlobalPropertyCell* cell,
2009 String* name,
2010 bool is_dont_delete) {
2011 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002012 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002013 // -- ecx : name
2014 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002015 // -----------------------------------
2016 Label miss;
2017
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002018 // If the object is the holder then we know that it's a global
2019 // object which can only happen for contextual loads. In this case,
2020 // the receiver cannot be a smi.
2021 if (object != holder) {
2022 __ test(eax, Immediate(kSmiTagMask));
2023 __ j(zero, &miss, not_taken);
2024 }
2025
2026 // Check that the maps haven't changed.
2027 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002028
2029 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002030 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2031 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002032
2033 // Check for deleted property if property can actually be deleted.
2034 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002035 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002036 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002037 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002038 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002039 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002040 }
2041
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002042 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002043 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002044 __ ret(0);
2045
2046 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002047 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2048 GenerateLoadMiss(masm(), Code::LOAD_IC);
2049
2050 // Return the generated code.
2051 return GetCode(NORMAL, name);
2052}
2053
2054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2056 JSObject* receiver,
2057 JSObject* holder,
2058 int index) {
2059 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002060 // -- eax : key
2061 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002063 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 Label miss;
2065
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002066 __ IncrementCounter(&Counters::keyed_load_field, 1);
2067
2068 // Check that the name has not changed.
2069 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2070 __ j(not_equal, &miss, not_taken);
2071
ager@chromium.org5c838252010-02-19 08:53:10 +00002072 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002073
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 __ bind(&miss);
2075 __ DecrementCounter(&Counters::keyed_load_field, 1);
2076 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2077
2078 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002079 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002080}
2081
2082
2083Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2084 JSObject* receiver,
2085 JSObject* holder,
2086 AccessorInfo* callback) {
2087 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002088 // -- eax : key
2089 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002091 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 Label miss;
2093
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002094 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2095
2096 // Check that the name has not changed.
2097 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2098 __ j(not_equal, &miss, not_taken);
2099
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002100 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002101 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002102 callback, name, &miss, &failure);
2103 if (!success) return failure;
2104
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002105 __ bind(&miss);
2106 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2107 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2108
2109 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002110 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002111}
2112
2113
2114Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2115 JSObject* receiver,
2116 JSObject* holder,
2117 Object* value) {
2118 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002119 // -- eax : key
2120 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002122 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 Label miss;
2124
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002125 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2126
2127 // Check that the name has not changed.
2128 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2129 __ j(not_equal, &miss, not_taken);
2130
ager@chromium.org5c838252010-02-19 08:53:10 +00002131 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002132 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 __ bind(&miss);
2134 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2135 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2136
2137 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002138 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002139}
2140
2141
2142Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2143 JSObject* holder,
2144 String* name) {
2145 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002146 // -- eax : key
2147 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002149 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 Label miss;
2151
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002152 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2153
2154 // Check that the name has not changed.
2155 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2156 __ j(not_equal, &miss, not_taken);
2157
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002158 LookupResult lookup;
2159 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002160 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002161 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002162 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002163 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002164 eax,
2165 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002166 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002167 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002168 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002169 __ bind(&miss);
2170 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2171 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2172
2173 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002174 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002175}
2176
2177
2178
2179
2180Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2181 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002182 // -- eax : key
2183 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002185 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 Label miss;
2187
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002188 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2189
2190 // Check that the name has not changed.
2191 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2192 __ j(not_equal, &miss, not_taken);
2193
ager@chromium.org5c838252010-02-19 08:53:10 +00002194 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002195 __ bind(&miss);
2196 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2197 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2198
2199 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002200 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002201}
2202
2203
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002204Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002205 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002206 // -- eax : key
2207 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002209 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 Label miss;
2211
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002212 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2213
2214 // Check that the name has not changed.
2215 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2216 __ j(not_equal, &miss, not_taken);
2217
ager@chromium.org5c838252010-02-19 08:53:10 +00002218 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002219 __ bind(&miss);
2220 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2221 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2222
2223 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002224 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002225}
2226
2227
2228Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2229 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002230 // -- eax : key
2231 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002233 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 Label miss;
2235
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002236 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2237
2238 // Check that the name has not changed.
2239 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2240 __ j(not_equal, &miss, not_taken);
2241
ager@chromium.org5c838252010-02-19 08:53:10 +00002242 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002243 __ bind(&miss);
2244 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2245 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2246
2247 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002248 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002249}
2250
2251
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002252// Specialized stub for constructing objects from functions which only have only
2253// simple assignments of the form this.x = ...; in their body.
2254Object* ConstructStubCompiler::CompileConstructStub(
2255 SharedFunctionInfo* shared) {
2256 // ----------- S t a t e -------------
2257 // -- eax : argc
2258 // -- edi : constructor
2259 // -- esp[0] : return address
2260 // -- esp[4] : last argument
2261 // -----------------------------------
2262 Label generic_stub_call;
2263#ifdef ENABLE_DEBUGGER_SUPPORT
2264 // Check to see whether there are any break points in the function code. If
2265 // there are jump to the generic constructor stub which calls the actual
2266 // code for the function thereby hitting the break points.
2267 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2268 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2269 __ cmp(ebx, Factory::undefined_value());
2270 __ j(not_equal, &generic_stub_call, not_taken);
2271#endif
2272
2273 // Load the initial map and verify that it is in fact a map.
2274 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2275 // Will both indicate a NULL and a Smi.
2276 __ test(ebx, Immediate(kSmiTagMask));
2277 __ j(zero, &generic_stub_call);
2278 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2279 __ j(not_equal, &generic_stub_call);
2280
2281#ifdef DEBUG
2282 // Cannot construct functions this way.
2283 // edi: constructor
2284 // ebx: initial map
2285 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2286 __ Assert(not_equal, "Function constructed by construct stub.");
2287#endif
2288
2289 // Now allocate the JSObject on the heap by moving the new space allocation
2290 // top forward.
2291 // edi: constructor
2292 // ebx: initial map
2293 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2294 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002295 __ AllocateInNewSpace(ecx,
2296 edx,
2297 ecx,
2298 no_reg,
2299 &generic_stub_call,
2300 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002301
2302 // Allocated the JSObject, now initialize the fields and add the heap tag.
2303 // ebx: initial map
2304 // edx: JSObject (untagged)
2305 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2306 __ mov(ebx, Factory::empty_fixed_array());
2307 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2308 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2309
2310 // Push the allocated object to the stack. This is the object that will be
2311 // returned (after it is tagged).
2312 __ push(edx);
2313
2314 // eax: argc
2315 // edx: JSObject (untagged)
2316 // Load the address of the first in-object property into edx.
2317 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2318 // Calculate the location of the first argument. The stack contains the
2319 // allocated object and the return address on top of the argc arguments.
2320 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2321
2322 // Use edi for holding undefined which is used in several places below.
2323 __ mov(edi, Factory::undefined_value());
2324
2325 // eax: argc
2326 // ecx: first argument
2327 // edx: first in-object property of the JSObject
2328 // edi: undefined
2329 // Fill the initialized properties with a constant value or a passed argument
2330 // depending on the this.x = ...; assignment in the function.
2331 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2332 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002333 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002334 // If argument is not passed the property is set to undefined,
2335 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002336 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002337 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002338 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002339 if (CpuFeatures::IsSupported(CMOV)) {
2340 CpuFeatures::Scope use_cmov(CMOV);
2341 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2342 } else {
2343 Label not_passed;
2344 __ j(below_equal, &not_passed);
2345 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2346 __ bind(&not_passed);
2347 }
2348 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002349 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002350 } else {
2351 // Set the property to the constant value.
2352 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2353 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2354 }
2355 }
2356
2357 // Fill the unused in-object property fields with undefined.
2358 for (int i = shared->this_property_assignments_count();
2359 i < shared->CalculateInObjectProperties();
2360 i++) {
2361 __ mov(Operand(edx, i * kPointerSize), edi);
2362 }
2363
2364 // Move argc to ebx and retrieve and tag the JSObject to return.
2365 __ mov(ebx, eax);
2366 __ pop(eax);
2367 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2368
2369 // Remove caller arguments and receiver from the stack and return.
2370 __ pop(ecx);
2371 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2372 __ push(ecx);
2373 __ IncrementCounter(&Counters::constructed_objects, 1);
2374 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2375 __ ret(0);
2376
2377 // Jump to the generic stub in case the specialized code cannot handle the
2378 // construction.
2379 __ bind(&generic_stub_call);
2380 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2381 Handle<Code> generic_construct_stub(code);
2382 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2383
2384 // Return the generated code.
2385 return GetCode();
2386}
2387
2388
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002389#undef __
2390
2391} } // namespace v8::internal