blob: ade324b92f9f0dc6d5e6fe4515e6da2b32735232 [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.orga1645e22009-09-09 19:27:10 +0000449 __ TailCallRuntime(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.orga1645e22009-09-09 19:27:10 +0000471 __ TailCallRuntime(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();
552 if (sfi->function_data()->IsUndefined()) return;
553 FunctionTemplateInfo* info =
554 FunctionTemplateInfo::cast(sfi->function_data());
555
556 // Require a C++ callback.
557 if (info->call_code()->IsUndefined()) return;
558 api_call_info_ = CallHandlerInfo::cast(info->call_code());
559
560 // Accept signatures that either have no restrictions at all or
561 // only have restrictions on the receiver.
562 if (!info->signature()->IsUndefined()) {
563 SignatureInfo* signature = SignatureInfo::cast(info->signature());
564 if (!signature->args()->IsUndefined()) return;
565 if (!signature->receiver()->IsUndefined()) {
566 expected_receiver_type_ =
567 FunctionTemplateInfo::cast(signature->receiver());
568 }
569 }
570
571 is_simple_api_call_ = true;
572 }
573
574 JSFunction* constant_function_;
575 bool is_simple_api_call_;
576 FunctionTemplateInfo* expected_receiver_type_;
577 CallHandlerInfo* api_call_info_;
578};
579
580
581// Reserves space for the extra arguments to FastHandleApiCall in the
582// caller's frame.
583//
584// These arguments are set by CheckPrototypes and GenerateFastApiCall.
585static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
586 // ----------- S t a t e -------------
587 // -- esp[0] : return address
588 // -- esp[4] : last argument in the internal frame of the caller
589 // -----------------------------------
590 __ pop(scratch);
591 __ push(Immediate(Smi::FromInt(0)));
592 __ push(Immediate(Smi::FromInt(0)));
593 __ push(Immediate(Smi::FromInt(0)));
594 __ push(Immediate(Smi::FromInt(0)));
595 __ push(scratch);
596}
597
598
599// Undoes the effects of ReserveSpaceForFastApiCall.
600static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
601 // ----------- S t a t e -------------
602 // -- esp[0] : return address
603 // -- esp[4] : last fast api call extra argument
604 // -- ...
605 // -- esp[16] : first fast api call extra argument
606 // -- esp[20] : last argument in the internal frame
607 // -----------------------------------
608 __ pop(scratch);
609 __ add(Operand(esp), Immediate(kPointerSize * 4));
610 __ push(scratch);
611}
612
613
614// Generates call to FastHandleApiCall builtin.
615static void GenerateFastApiCall(MacroAssembler* masm,
616 const CallOptimization& optimization,
617 int argc) {
618 // ----------- S t a t e -------------
619 // -- esp[0] : return address
620 // -- esp[4] : object passing the type check
621 // (last fast api call extra argument,
622 // set by CheckPrototypes)
623 // -- esp[8] : api call data
624 // -- esp[12] : api callback
625 // -- esp[16] : api function
626 // (first fast api call extra argument)
627 // -- esp[20] : last argument
628 // -- ...
629 // -- esp[(argc + 5) * 4] : first argument
630 // -- esp[(argc + 6) * 4] : receiver
631 // -----------------------------------
632
633 // Get the function and setup the context.
634 JSFunction* function = optimization.constant_function();
635 __ mov(edi, Immediate(Handle<JSFunction>(function)));
636 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
637
638 // Pass the additional arguments FastHandleApiCall expects.
639 __ mov(Operand(esp, 4 * kPointerSize), edi);
640 bool info_loaded = false;
641 Object* callback = optimization.api_call_info()->callback();
642 if (Heap::InNewSpace(callback)) {
643 info_loaded = true;
644 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
645 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
646 __ mov(Operand(esp, 3 * kPointerSize), ebx);
647 } else {
648 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
649 }
650 Object* call_data = optimization.api_call_info()->data();
651 if (Heap::InNewSpace(call_data)) {
652 if (!info_loaded) {
653 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
654 }
655 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
656 __ mov(Operand(esp, 2 * kPointerSize), ebx);
657 } else {
658 __ mov(Operand(esp, 2 * kPointerSize),
659 Immediate(Handle<Object>(call_data)));
660 }
661
662 // Set the number of arguments.
663 __ mov(eax, Immediate(argc + 4));
664
665 // Jump to the fast api call builtin (tail call).
666 Handle<Code> code = Handle<Code>(
667 Builtins::builtin(Builtins::FastHandleApiCall));
668 ParameterCount expected(0);
669 __ InvokeCode(code, expected, expected,
670 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
671}
672
673
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000674class CallInterceptorCompiler BASE_EMBEDDED {
675 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000676 CallInterceptorCompiler(StubCompiler* stub_compiler,
677 const ParameterCount& arguments,
678 Register name)
679 : stub_compiler_(stub_compiler),
680 arguments_(arguments),
681 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000682
ager@chromium.org5c838252010-02-19 08:53:10 +0000683 void Compile(MacroAssembler* masm,
684 JSObject* object,
685 JSObject* holder,
686 String* name,
687 LookupResult* lookup,
688 Register receiver,
689 Register scratch1,
690 Register scratch2,
691 Label* miss) {
692 ASSERT(holder->HasNamedInterceptor());
693 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
694
695 // Check that the receiver isn't a smi.
696 __ test(receiver, Immediate(kSmiTagMask));
697 __ j(zero, miss, not_taken);
698
699 CallOptimization optimization(lookup);
700
701 if (optimization.is_constant_call() &&
702 !Top::CanHaveSpecialFunctions(holder)) {
703 CompileCacheable(masm,
704 object,
705 receiver,
706 scratch1,
707 scratch2,
708 holder,
709 lookup,
710 name,
711 optimization,
712 miss);
713 } else {
714 CompileRegular(masm,
715 object,
716 receiver,
717 scratch1,
718 scratch2,
719 name,
720 holder,
721 miss);
722 }
723 }
724
725 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000726 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000727 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000728 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000729 Register scratch1,
730 Register scratch2,
731 JSObject* holder_obj,
732 LookupResult* lookup,
733 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000734 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000735 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000736 ASSERT(optimization.is_constant_call());
737 ASSERT(!lookup->holder()->IsGlobalObject());
738
739 int depth1 = kInvalidProtoDepth;
740 int depth2 = kInvalidProtoDepth;
741 bool can_do_fast_api_call = false;
742 if (optimization.is_simple_api_call() &&
743 !lookup->holder()->IsGlobalObject()) {
744 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
745 if (depth1 == kInvalidProtoDepth) {
746 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
747 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000748 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000749 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
750 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000751 }
752
ager@chromium.org5c838252010-02-19 08:53:10 +0000753 __ IncrementCounter(&Counters::call_const_interceptor, 1);
754
755 if (can_do_fast_api_call) {
756 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
757 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000758 }
759
ager@chromium.org5c838252010-02-19 08:53:10 +0000760 Label miss_cleanup;
761 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
762 Register holder =
763 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
764 scratch1, scratch2, name,
765 depth1, miss);
766
767 Label regular_invoke;
768 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
769
770 // Generate code for the failed interceptor case.
771
772 // Check the lookup is still valid.
773 stub_compiler_->CheckPrototypes(holder_obj, receiver,
774 lookup->holder(),
775 scratch1, scratch2, name,
776 depth2, miss);
777
778 if (can_do_fast_api_call) {
779 GenerateFastApiCall(masm, optimization, arguments_.immediate());
780 } else {
781 __ InvokeFunction(optimization.constant_function(), arguments_,
782 JUMP_FUNCTION);
783 }
784
785 if (can_do_fast_api_call) {
786 __ bind(&miss_cleanup);
787 FreeSpaceForFastApiCall(masm, scratch1);
788 __ jmp(miss_label);
789 }
790
791 __ bind(&regular_invoke);
792 if (can_do_fast_api_call) {
793 FreeSpaceForFastApiCall(masm, scratch1);
794 }
795 }
796
797 void CompileRegular(MacroAssembler* masm,
798 JSObject* object,
799 Register receiver,
800 Register scratch1,
801 Register scratch2,
802 String* name,
803 JSObject* holder_obj,
804 Label* miss_label) {
805 Register holder =
806 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
807 scratch1, scratch2, name,
808 miss_label);
809
810 __ EnterInternalFrame();
811 // Save the name_ register across the call.
812 __ push(name_);
813
814 PushInterceptorArguments(masm,
815 receiver,
816 holder,
817 name_,
818 holder_obj);
819
820 __ CallExternalReference(
821 ExternalReference(
822 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
823 5);
824
825 // Restore the name_ register.
826 __ pop(name_);
827 __ LeaveInternalFrame();
828 }
829
830 void LoadWithInterceptor(MacroAssembler* masm,
831 Register receiver,
832 Register holder,
833 JSObject* holder_obj,
834 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000835 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000836 __ push(holder); // Save the holder.
837 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000838
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000839 CompileCallLoadPropertyWithInterceptor(masm,
840 receiver,
841 holder,
842 name_,
843 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000844
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000845 __ pop(name_); // Restore the name.
846 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000847 __ LeaveInternalFrame();
848
849 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000850 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000851 }
852
ager@chromium.org5c838252010-02-19 08:53:10 +0000853 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000854 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000855 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000856};
857
858
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000859void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
860 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
861 Code* code = NULL;
862 if (kind == Code::LOAD_IC) {
863 code = Builtins::builtin(Builtins::LoadIC_Miss);
864 } else {
865 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
866 }
867
868 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000869 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870}
871
872
ager@chromium.org5c838252010-02-19 08:53:10 +0000873// Both name_reg and receiver_reg are preserved on jumps to miss_label,
874// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000875void StubCompiler::GenerateStoreField(MacroAssembler* masm,
876 JSObject* object,
877 int index,
878 Map* transition,
879 Register receiver_reg,
880 Register name_reg,
881 Register scratch,
882 Label* miss_label) {
883 // Check that the object isn't a smi.
884 __ test(receiver_reg, Immediate(kSmiTagMask));
885 __ j(zero, miss_label, not_taken);
886
887 // Check that the map of the object hasn't changed.
888 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
889 Immediate(Handle<Map>(object->map())));
890 __ j(not_equal, miss_label, not_taken);
891
892 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000893 if (object->IsJSGlobalProxy()) {
894 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000895 }
896
897 // Stub never generated for non-global objects that require access
898 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000899 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000901 // Perform map transition for the receiver if necessary.
902 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
903 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000904 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000905 __ pop(scratch); // Return address.
906 __ push(receiver_reg);
907 __ push(Immediate(Handle<Map>(transition)));
908 __ push(eax);
909 __ push(scratch);
910 __ TailCallRuntime(
911 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000912 return;
913 }
914
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000916 // Update the map of the object; no write barrier updating is
917 // needed because the map is never in new space.
918 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
919 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000920 }
921
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000922 // Adjust for the number of properties stored in the object. Even in the
923 // face of a transition we can use the old map here because the size of the
924 // object and the number of in-object properties is not going to change.
925 index -= object->map()->inobject_properties();
926
ager@chromium.org7c537e22008-10-16 08:43:32 +0000927 if (index < 0) {
928 // Set the property straight into the object.
929 int offset = object->map()->instance_size() + (index * kPointerSize);
930 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931
ager@chromium.org7c537e22008-10-16 08:43:32 +0000932 // Update the write barrier for the array address.
933 // Pass the value being stored in the now unused name_reg.
934 __ mov(name_reg, Operand(eax));
935 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
936 } else {
937 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000938 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000939 // Get the properties array (optimistically).
940 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000941 __ mov(FieldOperand(scratch, offset), eax);
942
943 // Update the write barrier for the array address.
944 // Pass the value being stored in the now unused name_reg.
945 __ mov(name_reg, Operand(eax));
946 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
947 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000948
949 // Return the value (register eax).
950 __ ret(0);
951}
952
953
954#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000955#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956
957
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000958Register StubCompiler::CheckPrototypes(JSObject* object,
959 Register object_reg,
960 JSObject* holder,
961 Register holder_reg,
962 Register scratch,
963 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000964 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000965 Label* miss) {
966 // Check that the maps haven't changed.
967 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000968 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
969 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000970
971 // If we've skipped any global objects, it's not enough to verify
972 // that their maps haven't changed.
973 while (object != holder) {
974 if (object->IsGlobalObject()) {
975 GlobalObject* global = GlobalObject::cast(object);
976 Object* probe = global->EnsurePropertyCell(name);
977 if (probe->IsFailure()) {
978 set_failure(Failure::cast(probe));
979 return result;
980 }
981 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
982 ASSERT(cell->value()->IsTheHole());
983 __ mov(scratch, Immediate(Handle<Object>(cell)));
984 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
985 Immediate(Factory::the_hole_value()));
986 __ j(not_equal, miss, not_taken);
987 }
988 object = JSObject::cast(object->GetPrototype());
989 }
990
ager@chromium.org5c838252010-02-19 08:53:10 +0000991 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000992 return result;
993}
994
995
996void StubCompiler::GenerateLoadField(JSObject* object,
997 JSObject* holder,
998 Register receiver,
999 Register scratch1,
1000 Register scratch2,
1001 int index,
1002 String* name,
1003 Label* miss) {
1004 // Check that the receiver isn't a smi.
1005 __ test(receiver, Immediate(kSmiTagMask));
1006 __ j(zero, miss, not_taken);
1007
1008 // Check the prototype chain.
1009 Register reg =
1010 CheckPrototypes(object, receiver, holder,
1011 scratch1, scratch2, name, miss);
1012
1013 // Get the value from the properties.
1014 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
1015 __ ret(0);
1016}
1017
1018
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001019bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001020 JSObject* holder,
1021 Register receiver,
1022 Register name_reg,
1023 Register scratch1,
1024 Register scratch2,
1025 AccessorInfo* callback,
1026 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001027 Label* miss,
1028 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001029 // Check that the receiver isn't a smi.
1030 __ test(receiver, Immediate(kSmiTagMask));
1031 __ j(zero, miss, not_taken);
1032
1033 // Check that the maps haven't changed.
1034 Register reg =
1035 CheckPrototypes(object, receiver, holder,
1036 scratch1, scratch2, name, miss);
1037
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001038 Handle<AccessorInfo> callback_handle(callback);
1039
1040 Register other = reg.is(scratch1) ? scratch2 : scratch1;
1041 __ EnterInternalFrame();
1042 __ PushHandleScope(other);
1043 // Push the stack address where the list of arguments ends
1044 __ mov(other, esp);
1045 __ sub(Operand(other), Immediate(2 * kPointerSize));
1046 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001047 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001048 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001049 __ mov(other, Immediate(callback_handle));
1050 __ push(other);
1051 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001052 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001053 // Save a pointer to where we pushed the arguments pointer.
1054 // This will be passed as the const Arguments& to the C++ callback.
1055 __ mov(eax, esp);
1056 __ add(Operand(eax), Immediate(5 * kPointerSize));
1057 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001058
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001059 // Do call through the api.
1060 ASSERT_EQ(6, ApiGetterEntryStub::kStackSpace);
1061 Address getter_address = v8::ToCData<Address>(callback->getter());
1062 ApiFunction fun(getter_address);
1063 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001064 // Emitting a stub call may try to allocate (if the code is not
1065 // already generated). Do not allow the assembler to perform a
1066 // garbage collection but instead return the allocation failure
1067 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001068 Object* result = masm()->TryCallStub(&stub);
1069 if (result->IsFailure()) {
1070 *failure = Failure::cast(result);
1071 return false;
1072 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001073
1074 // We need to avoid using eax since that now holds the result.
1075 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +00001076 // Emitting PopHandleScope may try to allocate. Do not allow the
1077 // assembler to perform a garbage collection but instead return a
1078 // failure object.
1079 result = masm()->TryPopHandleScope(eax, tmp);
1080 if (result->IsFailure()) {
1081 *failure = Failure::cast(result);
1082 return false;
1083 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +00001084 __ LeaveInternalFrame();
1085
1086 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001087 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001088}
1089
1090
1091void StubCompiler::GenerateLoadConstant(JSObject* object,
1092 JSObject* holder,
1093 Register receiver,
1094 Register scratch1,
1095 Register scratch2,
1096 Object* value,
1097 String* name,
1098 Label* miss) {
1099 // Check that the receiver isn't a smi.
1100 __ test(receiver, Immediate(kSmiTagMask));
1101 __ j(zero, miss, not_taken);
1102
1103 // Check that the maps haven't changed.
1104 Register reg =
1105 CheckPrototypes(object, receiver, holder,
1106 scratch1, scratch2, name, miss);
1107
1108 // Return the constant value.
1109 __ mov(eax, Handle<Object>(value));
1110 __ ret(0);
1111}
1112
1113
1114void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1115 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001116 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001117 Register receiver,
1118 Register name_reg,
1119 Register scratch1,
1120 Register scratch2,
1121 String* name,
1122 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001123 LoadInterceptorCompiler compiler(name_reg);
1124 CompileLoadInterceptor(&compiler,
1125 this,
1126 masm(),
1127 object,
1128 holder,
1129 name,
1130 lookup,
1131 receiver,
1132 scratch1,
1133 scratch2,
1134 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001135}
1136
1137
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001138// TODO(1241006): Avoid having lazy compile stubs specialized by the
1139// number of arguments. It is not needed anymore.
1140Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001141 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001142 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001143
1144 // Push a copy of the function onto the stack.
1145 __ push(edi);
1146
1147 __ push(edi); // function is also the parameter to the runtime call
1148 __ CallRuntime(Runtime::kLazyCompile, 1);
1149 __ pop(edi);
1150
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001151 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001152 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001153
1154 // Do a tail-call of the compiled function.
1155 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1156 __ jmp(Operand(ecx));
1157
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001158 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001159}
1160
1161
ager@chromium.org5c838252010-02-19 08:53:10 +00001162Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001164 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001165 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001166 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001167 // -- ecx : name
1168 // -- esp[0] : return address
1169 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1170 // -- ...
1171 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001172 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 Label miss;
1174
1175 // Get the receiver from the stack.
1176 const int argc = arguments().immediate();
1177 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1178
1179 // Check that the receiver isn't a smi.
1180 __ test(edx, Immediate(kSmiTagMask));
1181 __ j(zero, &miss, not_taken);
1182
1183 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001184 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001185
ager@chromium.org7c537e22008-10-16 08:43:32 +00001186 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001187
1188 // Check that the function really is a function.
1189 __ test(edi, Immediate(kSmiTagMask));
1190 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001191 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192 __ j(not_equal, &miss, not_taken);
1193
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001194 // Patch the receiver on the stack with the global proxy if
1195 // necessary.
1196 if (object->IsGlobalObject()) {
1197 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1198 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1199 }
1200
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001201 // Invoke the function.
1202 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1203
1204 // Handle call cache miss.
1205 __ bind(&miss);
1206 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001207 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001208
1209 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001210 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001211}
1212
1213
1214Object* CallStubCompiler::CompileCallConstant(Object* object,
1215 JSObject* holder,
1216 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001217 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001218 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001219 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001220 // -- ecx : name
1221 // -- esp[0] : return address
1222 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1223 // -- ...
1224 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001225 // -----------------------------------
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001226 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001227
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 if (check != NUMBER_CHECK) {
1234 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001235 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001236 }
1237
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001238 // Make sure that it's okay not to patch the on stack receiver
1239 // unless we're doing a receiver map check.
1240 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1241
ager@chromium.org5c838252010-02-19 08:53:10 +00001242 CallOptimization optimization(function);
1243 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001244 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001245
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 switch (check) {
1247 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001248 __ IncrementCounter(&Counters::call_const, 1);
1249
1250 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1251 depth = optimization.GetPrototypeDepthOfExpectedType(
1252 JSObject::cast(object), holder);
1253 }
1254
1255 if (depth != kInvalidProtoDepth) {
1256 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1257 ReserveSpaceForFastApiCall(masm(), eax);
1258 }
1259
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001260 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001261 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001262 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001263
1264 // Patch the receiver on the stack with the global proxy if
1265 // necessary.
1266 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001267 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001268 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1269 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1270 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001271 break;
1272
1273 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001274 if (!function->IsBuiltin()) {
1275 // Calling non-builtins with a value as receiver requires boxing.
1276 __ jmp(&miss);
1277 } else {
1278 // Check that the object is a string or a symbol.
1279 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1280 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1281 __ cmp(eax, FIRST_NONSTRING_TYPE);
1282 __ j(above_equal, &miss, not_taken);
1283 // Check that the maps starting from the prototype haven't changed.
1284 GenerateLoadGlobalFunctionPrototype(masm(),
1285 Context::STRING_FUNCTION_INDEX,
1286 eax);
1287 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1288 ebx, edx, name, &miss);
1289 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001290 break;
1291
1292 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001293 if (!function->IsBuiltin()) {
1294 // Calling non-builtins with a value as receiver requires boxing.
1295 __ jmp(&miss);
1296 } else {
1297 Label fast;
1298 // Check that the object is a smi or a heap number.
1299 __ test(edx, Immediate(kSmiTagMask));
1300 __ j(zero, &fast, taken);
1301 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1302 __ j(not_equal, &miss, not_taken);
1303 __ bind(&fast);
1304 // Check that the maps starting from the prototype haven't changed.
1305 GenerateLoadGlobalFunctionPrototype(masm(),
1306 Context::NUMBER_FUNCTION_INDEX,
1307 eax);
1308 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1309 ebx, edx, name, &miss);
1310 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001311 break;
1312 }
1313
1314 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001315 if (!function->IsBuiltin()) {
1316 // Calling non-builtins with a value as receiver requires boxing.
1317 __ jmp(&miss);
1318 } else {
1319 Label fast;
1320 // Check that the object is a boolean.
1321 __ cmp(edx, Factory::true_value());
1322 __ j(equal, &fast, taken);
1323 __ cmp(edx, Factory::false_value());
1324 __ j(not_equal, &miss, not_taken);
1325 __ bind(&fast);
1326 // Check that the maps starting from the prototype haven't changed.
1327 GenerateLoadGlobalFunctionPrototype(masm(),
1328 Context::BOOLEAN_FUNCTION_INDEX,
1329 eax);
1330 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1331 ebx, edx, name, &miss);
1332 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 break;
1334 }
1335
1336 case JSARRAY_HAS_FAST_ELEMENTS_CHECK:
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001337 CheckPrototypes(JSObject::cast(object), edx, holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001338 ebx, eax, name, &miss);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001339 // Make sure object->HasFastElements().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001340 // Get the elements array of the object.
1341 __ mov(ebx, FieldOperand(edx, JSObject::kElementsOffset));
1342 // Check that the object is in fast mode (not dictionary).
1343 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001344 Immediate(Factory::fixed_array_map()));
1345 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001346 break;
1347
1348 default:
1349 UNREACHABLE();
1350 }
1351
ager@chromium.org5c838252010-02-19 08:53:10 +00001352 if (depth != kInvalidProtoDepth) {
1353 GenerateFastApiCall(masm(), optimization, argc);
1354 } else {
1355 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1356 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357
1358 // Handle call cache miss.
1359 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001360 if (depth != kInvalidProtoDepth) {
1361 FreeSpaceForFastApiCall(masm(), eax);
1362 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001363 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001365 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001366
1367 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001368 String* function_name = NULL;
1369 if (function->shared()->name()->IsString()) {
1370 function_name = String::cast(function->shared()->name());
1371 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001372 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373}
1374
1375
ager@chromium.org5c838252010-02-19 08:53:10 +00001376Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001377 JSObject* holder,
1378 String* name) {
1379 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001380 // -- ecx : name
1381 // -- esp[0] : return address
1382 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1383 // -- ...
1384 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001385 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386 Label miss;
1387
1388 // Get the number of arguments.
1389 const int argc = arguments().immediate();
1390
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001391 LookupResult lookup;
1392 LookupPostInterceptor(holder, name, &lookup);
1393
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001394 // Get the receiver from the stack.
1395 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001396
ager@chromium.org5c838252010-02-19 08:53:10 +00001397 CallInterceptorCompiler compiler(this, arguments(), ecx);
1398 compiler.Compile(masm(),
1399 object,
1400 holder,
1401 name,
1402 &lookup,
1403 edx,
1404 ebx,
1405 edi,
1406 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001407
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001408 // Restore receiver.
1409 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001410
1411 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001412 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001413 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001414 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 __ j(not_equal, &miss, not_taken);
1416
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001417 // Patch the receiver on the stack with the global proxy if
1418 // necessary.
1419 if (object->IsGlobalObject()) {
1420 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1421 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1422 }
1423
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001424 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001425 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1427
1428 // Handle load cache miss.
1429 __ bind(&miss);
1430 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001431 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001432
1433 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001434 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001435}
1436
1437
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001438Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1439 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001440 JSGlobalPropertyCell* cell,
1441 JSFunction* function,
1442 String* name) {
1443 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001444 // -- ecx : name
1445 // -- esp[0] : return address
1446 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1447 // -- ...
1448 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001449 // -----------------------------------
1450 Label miss;
1451
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001452 // Get the number of arguments.
1453 const int argc = arguments().immediate();
1454
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001455 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001456 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001457
1458 // If the object is the holder then we know that it's a global
1459 // object which can only happen for contextual calls. In this case,
1460 // the receiver cannot be a smi.
1461 if (object != holder) {
1462 __ test(edx, Immediate(kSmiTagMask));
1463 __ j(zero, &miss, not_taken);
1464 }
1465
1466 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001467 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001468
1469 // Get the value from the cell.
1470 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1471 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1472
1473 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001474 if (Heap::InNewSpace(function)) {
1475 // We can't embed a pointer to a function in new space so we have
1476 // to verify that the shared function info is unchanged. This has
1477 // the nice side effect that multiple closures based on the same
1478 // function can all use this call IC. Before we load through the
1479 // function, we have to verify that it still is a function.
1480 __ test(edi, Immediate(kSmiTagMask));
1481 __ j(zero, &miss, not_taken);
1482 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1483 __ j(not_equal, &miss, not_taken);
1484
1485 // Check the shared function info. Make sure it hasn't changed.
1486 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1487 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1488 __ j(not_equal, &miss, not_taken);
1489 } else {
1490 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1491 __ j(not_equal, &miss, not_taken);
1492 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001493
1494 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001495 if (object->IsGlobalObject()) {
1496 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1497 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1498 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001499
1500 // Setup the context (function already in edi).
1501 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1502
1503 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001504 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001505 ASSERT(function->is_compiled());
1506 Handle<Code> code(function->code());
1507 ParameterCount expected(function->shared()->formal_parameter_count());
1508 __ InvokeCode(code, expected, arguments(),
1509 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1510
1511 // Handle call cache miss.
1512 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001513 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1514 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1515 __ jmp(ic, RelocInfo::CODE_TARGET);
1516
1517 // Return the generated code.
1518 return GetCode(NORMAL, name);
1519}
1520
1521
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001522Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1523 int index,
1524 Map* transition,
1525 String* name) {
1526 // ----------- S t a t e -------------
1527 // -- eax : value
1528 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001529 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001531 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001532 Label miss;
1533
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001534 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001535 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001536 object,
1537 index,
1538 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001539 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001540 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001541
1542 // Handle store cache miss.
1543 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001544 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001545 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001546 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547
1548 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001549 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001550}
1551
1552
1553Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1554 AccessorInfo* callback,
1555 String* name) {
1556 // ----------- S t a t e -------------
1557 // -- eax : value
1558 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001559 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001561 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562 Label miss;
1563
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001564 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001565 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 __ j(zero, &miss, not_taken);
1567
1568 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001569 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001570 Immediate(Handle<Map>(object->map())));
1571 __ j(not_equal, &miss, not_taken);
1572
1573 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001574 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001575 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001576 }
1577
1578 // Stub never generated for non-global objects that require access
1579 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001580 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001581
1582 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001583 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001584 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1585 __ push(ecx); // name
1586 __ push(eax); // value
1587 __ push(ebx); // restore return address
1588
mads.s.ager31e71382008-08-13 09:32:07 +00001589 // Do tail-call to the runtime system.
1590 ExternalReference store_callback_property =
1591 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001592 __ TailCallRuntime(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001593
1594 // Handle store cache miss.
1595 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001596 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001597 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001598
1599 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001600 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601}
1602
1603
1604Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1605 String* name) {
1606 // ----------- S t a t e -------------
1607 // -- eax : value
1608 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001609 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001611 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001612 Label miss;
1613
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001615 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 __ j(zero, &miss, not_taken);
1617
1618 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001619 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001620 Immediate(Handle<Map>(receiver->map())));
1621 __ j(not_equal, &miss, not_taken);
1622
1623 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001624 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001625 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001626 }
1627
1628 // Stub never generated for non-global objects that require access
1629 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001630 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001631
1632 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001633 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001634 __ push(ecx); // name
1635 __ push(eax); // value
1636 __ push(ebx); // restore return address
1637
mads.s.ager31e71382008-08-13 09:32:07 +00001638 // Do tail-call to the runtime system.
1639 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001641 __ TailCallRuntime(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001642
1643 // Handle store cache miss.
1644 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001645 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001646 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001647
1648 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001649 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001650}
1651
1652
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001653Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1654 JSGlobalPropertyCell* cell,
1655 String* name) {
1656 // ----------- S t a t e -------------
1657 // -- eax : value
1658 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001659 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001660 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001661 // -----------------------------------
1662 Label miss;
1663
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001664 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001665 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001666 Immediate(Handle<Map>(object->map())));
1667 __ j(not_equal, &miss, not_taken);
1668
1669 // Store the value in the cell.
1670 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1671 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1672
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001673 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001674 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001675 __ ret(0);
1676
1677 // Handle store cache miss.
1678 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001679 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1680 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1681 __ jmp(ic, RelocInfo::CODE_TARGET);
1682
1683 // Return the generated code.
1684 return GetCode(NORMAL, name);
1685}
1686
1687
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1689 int index,
1690 Map* transition,
1691 String* name) {
1692 // ----------- S t a t e -------------
1693 // -- eax : value
1694 // -- esp[0] : return address
1695 // -- esp[4] : key
1696 // -- esp[8] : receiver
1697 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 Label miss;
1699
1700 __ IncrementCounter(&Counters::keyed_store_field, 1);
1701
1702 // Get the name from the stack.
1703 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1704 // Check that the name has not changed.
1705 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1706 __ j(not_equal, &miss, not_taken);
1707
1708 // Get the object from the stack.
ager@chromium.org5c838252010-02-19 08:53:10 +00001709 __ mov(edx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001710
1711 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001712 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001713 object,
1714 index,
1715 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001716 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001717 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001718
1719 // Handle store cache miss.
1720 __ bind(&miss);
1721 __ DecrementCounter(&Counters::keyed_store_field, 1);
1722 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001723 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001724
1725 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001726 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727}
1728
1729
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001730
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001731Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1732 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001733 int index,
1734 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001736 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 // -- ecx : name
1738 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001739 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 Label miss;
1741
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001742 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001743 __ bind(&miss);
1744 GenerateLoadMiss(masm(), Code::LOAD_IC);
1745
1746 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001747 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001748}
1749
1750
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001751Object* LoadStubCompiler::CompileLoadCallback(String* name,
1752 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001754 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001756 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757 // -- ecx : name
1758 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001760 Label miss;
1761
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001762 Failure* failure = Failure::InternalError();
1763 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1764 callback, name, &miss, &failure);
1765 if (!success) return failure;
1766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 __ bind(&miss);
1768 GenerateLoadMiss(masm(), Code::LOAD_IC);
1769
1770 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001771 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001772}
1773
1774
1775Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1776 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001777 Object* value,
1778 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001780 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781 // -- ecx : name
1782 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 Label miss;
1785
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001786 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 __ bind(&miss);
1788 GenerateLoadMiss(masm(), Code::LOAD_IC);
1789
1790 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001791 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792}
1793
1794
1795Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1796 JSObject* holder,
1797 String* name) {
1798 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001799 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800 // -- ecx : name
1801 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001802 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803 Label miss;
1804
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001805 LookupResult lookup;
1806 LookupPostInterceptor(holder, name, &lookup);
1807
ager@chromium.orge2902be2009-06-08 12:21:35 +00001808 // TODO(368): Compile in the whole chain: all the interceptors in
1809 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001810 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001811 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001812 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001813 eax,
1814 ecx,
1815 edx,
1816 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001817 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001818 &miss);
1819
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001820 __ bind(&miss);
1821 GenerateLoadMiss(masm(), Code::LOAD_IC);
1822
1823 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001824 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001825}
1826
1827
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001828Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
1829 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001830 JSGlobalPropertyCell* cell,
1831 String* name,
1832 bool is_dont_delete) {
1833 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001834 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001835 // -- ecx : name
1836 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001837 // -----------------------------------
1838 Label miss;
1839
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001840 // If the object is the holder then we know that it's a global
1841 // object which can only happen for contextual loads. In this case,
1842 // the receiver cannot be a smi.
1843 if (object != holder) {
1844 __ test(eax, Immediate(kSmiTagMask));
1845 __ j(zero, &miss, not_taken);
1846 }
1847
1848 // Check that the maps haven't changed.
1849 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001850
1851 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00001852 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1853 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001854
1855 // Check for deleted property if property can actually be deleted.
1856 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001857 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001858 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001859 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001860 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001861 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001862 }
1863
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001864 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00001865 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001866 __ ret(0);
1867
1868 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001869 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
1870 GenerateLoadMiss(masm(), Code::LOAD_IC);
1871
1872 // Return the generated code.
1873 return GetCode(NORMAL, name);
1874}
1875
1876
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001877Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
1878 JSObject* receiver,
1879 JSObject* holder,
1880 int index) {
1881 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001882 // -- eax : key
1883 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001884 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001885 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001886 Label miss;
1887
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001888 __ IncrementCounter(&Counters::keyed_load_field, 1);
1889
1890 // Check that the name has not changed.
1891 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1892 __ j(not_equal, &miss, not_taken);
1893
ager@chromium.org5c838252010-02-19 08:53:10 +00001894 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001895
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001896 __ bind(&miss);
1897 __ DecrementCounter(&Counters::keyed_load_field, 1);
1898 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1899
1900 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001901 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001902}
1903
1904
1905Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
1906 JSObject* receiver,
1907 JSObject* holder,
1908 AccessorInfo* callback) {
1909 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001910 // -- eax : key
1911 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001912 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001914 Label miss;
1915
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 __ IncrementCounter(&Counters::keyed_load_callback, 1);
1917
1918 // Check that the name has not changed.
1919 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1920 __ j(not_equal, &miss, not_taken);
1921
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001922 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00001923 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001924 callback, name, &miss, &failure);
1925 if (!success) return failure;
1926
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 __ bind(&miss);
1928 __ DecrementCounter(&Counters::keyed_load_callback, 1);
1929 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1930
1931 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001932 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933}
1934
1935
1936Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
1937 JSObject* receiver,
1938 JSObject* holder,
1939 Object* value) {
1940 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001941 // -- eax : key
1942 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001944 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 Label miss;
1946
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
1948
1949 // Check that the name has not changed.
1950 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1951 __ j(not_equal, &miss, not_taken);
1952
ager@chromium.org5c838252010-02-19 08:53:10 +00001953 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001954 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 __ bind(&miss);
1956 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
1957 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1958
1959 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001960 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001961}
1962
1963
1964Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1965 JSObject* holder,
1966 String* name) {
1967 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001968 // -- eax : key
1969 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001970 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001972 Label miss;
1973
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
1975
1976 // Check that the name has not changed.
1977 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1978 __ j(not_equal, &miss, not_taken);
1979
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001980 LookupResult lookup;
1981 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001982 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001983 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001984 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001985 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00001986 eax,
1987 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001988 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001989 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001990 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001991 __ bind(&miss);
1992 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
1993 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1994
1995 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001996 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001997}
1998
1999
2000
2001
2002Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2003 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002004 // -- eax : key
2005 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002006 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002007 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002008 Label miss;
2009
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2011
2012 // Check that the name has not changed.
2013 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2014 __ j(not_equal, &miss, not_taken);
2015
ager@chromium.org5c838252010-02-19 08:53:10 +00002016 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002017 __ bind(&miss);
2018 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2019 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2020
2021 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002022 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002023}
2024
2025
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002026Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002027 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002028 // -- eax : key
2029 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002030 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002031 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002032 Label miss;
2033
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002034 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2035
2036 // Check that the name has not changed.
2037 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2038 __ j(not_equal, &miss, not_taken);
2039
ager@chromium.org5c838252010-02-19 08:53:10 +00002040 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002041 __ bind(&miss);
2042 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2043 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2044
2045 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002046 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002047}
2048
2049
2050Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2051 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002052 // -- eax : key
2053 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002054 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002055 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002056 Label miss;
2057
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002058 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2059
2060 // Check that the name has not changed.
2061 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2062 __ j(not_equal, &miss, not_taken);
2063
ager@chromium.org5c838252010-02-19 08:53:10 +00002064 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002065 __ bind(&miss);
2066 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2067 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2068
2069 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002070 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002071}
2072
2073
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002074// Specialized stub for constructing objects from functions which only have only
2075// simple assignments of the form this.x = ...; in their body.
2076Object* ConstructStubCompiler::CompileConstructStub(
2077 SharedFunctionInfo* shared) {
2078 // ----------- S t a t e -------------
2079 // -- eax : argc
2080 // -- edi : constructor
2081 // -- esp[0] : return address
2082 // -- esp[4] : last argument
2083 // -----------------------------------
2084 Label generic_stub_call;
2085#ifdef ENABLE_DEBUGGER_SUPPORT
2086 // Check to see whether there are any break points in the function code. If
2087 // there are jump to the generic constructor stub which calls the actual
2088 // code for the function thereby hitting the break points.
2089 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2090 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2091 __ cmp(ebx, Factory::undefined_value());
2092 __ j(not_equal, &generic_stub_call, not_taken);
2093#endif
2094
2095 // Load the initial map and verify that it is in fact a map.
2096 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2097 // Will both indicate a NULL and a Smi.
2098 __ test(ebx, Immediate(kSmiTagMask));
2099 __ j(zero, &generic_stub_call);
2100 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2101 __ j(not_equal, &generic_stub_call);
2102
2103#ifdef DEBUG
2104 // Cannot construct functions this way.
2105 // edi: constructor
2106 // ebx: initial map
2107 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2108 __ Assert(not_equal, "Function constructed by construct stub.");
2109#endif
2110
2111 // Now allocate the JSObject on the heap by moving the new space allocation
2112 // top forward.
2113 // edi: constructor
2114 // ebx: initial map
2115 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2116 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002117 __ AllocateInNewSpace(ecx,
2118 edx,
2119 ecx,
2120 no_reg,
2121 &generic_stub_call,
2122 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002123
2124 // Allocated the JSObject, now initialize the fields and add the heap tag.
2125 // ebx: initial map
2126 // edx: JSObject (untagged)
2127 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2128 __ mov(ebx, Factory::empty_fixed_array());
2129 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2130 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2131
2132 // Push the allocated object to the stack. This is the object that will be
2133 // returned (after it is tagged).
2134 __ push(edx);
2135
2136 // eax: argc
2137 // edx: JSObject (untagged)
2138 // Load the address of the first in-object property into edx.
2139 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2140 // Calculate the location of the first argument. The stack contains the
2141 // allocated object and the return address on top of the argc arguments.
2142 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2143
2144 // Use edi for holding undefined which is used in several places below.
2145 __ mov(edi, Factory::undefined_value());
2146
2147 // eax: argc
2148 // ecx: first argument
2149 // edx: first in-object property of the JSObject
2150 // edi: undefined
2151 // Fill the initialized properties with a constant value or a passed argument
2152 // depending on the this.x = ...; assignment in the function.
2153 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2154 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002155 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002156 // If argument is not passed the property is set to undefined,
2157 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002158 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002159 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002160 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002161 if (CpuFeatures::IsSupported(CMOV)) {
2162 CpuFeatures::Scope use_cmov(CMOV);
2163 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2164 } else {
2165 Label not_passed;
2166 __ j(below_equal, &not_passed);
2167 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2168 __ bind(&not_passed);
2169 }
2170 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002171 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002172 } else {
2173 // Set the property to the constant value.
2174 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2175 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2176 }
2177 }
2178
2179 // Fill the unused in-object property fields with undefined.
2180 for (int i = shared->this_property_assignments_count();
2181 i < shared->CalculateInObjectProperties();
2182 i++) {
2183 __ mov(Operand(edx, i * kPointerSize), edi);
2184 }
2185
2186 // Move argc to ebx and retrieve and tag the JSObject to return.
2187 __ mov(ebx, eax);
2188 __ pop(eax);
2189 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2190
2191 // Remove caller arguments and receiver from the stack and return.
2192 __ pop(ecx);
2193 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2194 __ push(ecx);
2195 __ IncrementCounter(&Counters::constructed_objects, 1);
2196 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2197 __ ret(0);
2198
2199 // Jump to the generic stub in case the specialized code cannot handle the
2200 // construction.
2201 __ bind(&generic_stub_call);
2202 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2203 Handle<Code> generic_construct_stub(code);
2204 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2205
2206 // Return the generated code.
2207 return GetCode();
2208}
2209
2210
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002211#undef __
2212
2213} } // namespace v8::internal