blob: 809228c7ec5332aa4c863ac405f8e7864f4d25ab [file] [log] [blame]
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "ic-inl.h"
31#include "codegen-inl.h"
32#include "stub-cache.h"
33
kasperl@chromium.org71affb52009-05-26 05:44:31 +000034namespace v8 {
35namespace internal {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
ager@chromium.org65dad4b2009-04-23 08:48:43 +000037#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000038
39
40static void ProbeTable(MacroAssembler* masm,
41 Code::Flags flags,
42 StubCache::Table table,
43 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000044 Register offset,
45 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000046 ExternalReference key_offset(SCTableReference::keyReference(table));
47 ExternalReference value_offset(SCTableReference::valueReference(table));
48
49 Label miss;
50
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000051 if (extra.is_valid()) {
52 // Get the code entry from the cache.
53 __ mov(extra, Operand::StaticArray(offset, times_2, value_offset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000054
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000055 // Check that the key in the entry matches the name.
56 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
57 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000058
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000059 // Check that the flags match what we're looking for.
60 __ mov(offset, FieldOperand(extra, Code::kFlagsOffset));
61 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
62 __ cmp(offset, flags);
63 __ j(not_equal, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000064
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000065 // Jump to the first instruction in the code stub.
66 __ add(Operand(extra), Immediate(Code::kHeaderSize - kHeapObjectTag));
67 __ jmp(Operand(extra));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000069 __ bind(&miss);
70 } else {
71 // Save the offset on the stack.
72 __ push(offset);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000073
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000074 // Check that the key in the entry matches the name.
75 __ cmp(name, Operand::StaticArray(offset, times_2, key_offset));
76 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
kasperl@chromium.org86f77b72009-07-06 08:21:57 +000078 // Get the code entry from the cache.
79 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
80
81 // Check that the flags match what we're looking for.
82 __ mov(offset, FieldOperand(offset, Code::kFlagsOffset));
83 __ and_(offset, ~Code::kFlagsNotUsedInLookup);
84 __ cmp(offset, flags);
85 __ j(not_equal, &miss);
86
87 // Restore offset and re-load code entry from cache.
88 __ pop(offset);
89 __ mov(offset, Operand::StaticArray(offset, times_2, value_offset));
90
91 // Jump to the first instruction in the code stub.
92 __ add(Operand(offset), Immediate(Code::kHeaderSize - kHeapObjectTag));
93 __ jmp(Operand(offset));
94
95 // Pop at miss.
96 __ bind(&miss);
97 __ pop(offset);
98 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099}
100
101
102void StubCache::GenerateProbe(MacroAssembler* masm,
103 Code::Flags flags,
104 Register receiver,
105 Register name,
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000106 Register scratch,
107 Register extra) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000108 Label miss;
109
110 // Make sure that code is valid. The shifting code relies on the
111 // entry size being 8.
112 ASSERT(sizeof(Entry) == 8);
113
114 // Make sure the flags does not name a specific type.
115 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
116
117 // Make sure that there are no register conflicts.
118 ASSERT(!scratch.is(receiver));
119 ASSERT(!scratch.is(name));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000120 ASSERT(!extra.is(receiver));
121 ASSERT(!extra.is(name));
122 ASSERT(!extra.is(scratch));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000123
124 // Check that the receiver isn't a smi.
125 __ test(receiver, Immediate(kSmiTagMask));
126 __ j(zero, &miss, not_taken);
127
128 // Get the map of the receiver and compute the hash.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000129 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000130 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131 __ xor_(scratch, flags);
132 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
133
134 // Probe the primary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000135 ProbeTable(masm, flags, kPrimary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000136
137 // Primary miss: Compute hash for secondary probe.
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000138 __ mov(scratch, FieldOperand(name, String::kHashFieldOffset));
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000139 __ add(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
140 __ xor_(scratch, flags);
141 __ and_(scratch, (kPrimaryTableSize - 1) << kHeapObjectTagSize);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000142 __ sub(scratch, Operand(name));
143 __ add(Operand(scratch), Immediate(flags));
144 __ and_(scratch, (kSecondaryTableSize - 1) << kHeapObjectTagSize);
145
146 // Probe the secondary table.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000147 ProbeTable(masm, flags, kSecondary, name, scratch, extra);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000148
149 // Cache miss: Fall-through and let caller handle the miss by
150 // entering the runtime system.
151 __ bind(&miss);
152}
153
154
155void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
156 int index,
157 Register prototype) {
158 // Load the global or builtins object from the current context.
159 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
160 // Load the global context from the global or builtins object.
161 __ mov(prototype,
162 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
163 // Load the function from the global context.
164 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
165 // Load the initial map. The global functions all have initial maps.
166 __ mov(prototype,
167 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
168 // Load the prototype from the initial map.
169 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
170}
171
172
173void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
174 Register receiver,
175 Register scratch,
176 Label* miss_label) {
177 // Check that the receiver isn't a smi.
178 __ test(receiver, Immediate(kSmiTagMask));
179 __ j(zero, miss_label, not_taken);
180
181 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000182 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183 __ j(not_equal, miss_label, not_taken);
184
185 // Load length directly from the JS array.
186 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
187 __ ret(0);
188}
189
190
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000191// Generate code to check if an object is a string. If the object is
192// a string, the map's instance type is left in the scratch register.
193static void GenerateStringCheck(MacroAssembler* masm,
194 Register receiver,
195 Register scratch,
196 Label* smi,
197 Label* non_string_object) {
198 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000200 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000201
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000202 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000203 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
204 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000205 ASSERT(kNotStringTag != 0);
206 __ test(scratch, Immediate(kNotStringTag));
207 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000208}
209
210
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000211void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
212 Register receiver,
ager@chromium.org5c838252010-02-19 08:53:10 +0000213 Register scratch1,
214 Register scratch2,
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000215 Label* miss) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000216 Label check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000218 // Check if the object is a string leaving the instance type in the
219 // scratch register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000220 GenerateStringCheck(masm, receiver, scratch1, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000221
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000222 // Load length from the string and convert to a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000223 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000225
226 // Check if the object is a JSValue wrapper.
227 __ bind(&check_wrapper);
ager@chromium.org5c838252010-02-19 08:53:10 +0000228 __ cmp(scratch1, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000229 __ j(not_equal, miss, not_taken);
230
231 // Check if the wrapped value is a string and load the length
232 // directly if it is.
ager@chromium.org5c838252010-02-19 08:53:10 +0000233 __ mov(scratch2, FieldOperand(receiver, JSValue::kValueOffset));
234 GenerateStringCheck(masm, scratch2, scratch1, miss, miss);
235 __ mov(eax, FieldOperand(scratch2, String::kLengthOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000236 __ ret(0);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000237}
238
239
240void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
241 Register receiver,
242 Register scratch1,
243 Register scratch2,
244 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000245 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000247 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000248}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000249
ager@chromium.org7c537e22008-10-16 08:43:32 +0000250
251// Load a fast property out of a holder object (src). In-object properties
252// are loaded directly otherwise the property is loaded from the properties
253// fixed array.
254void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000255 Register dst, Register src,
256 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000257 // Adjust for the number of properties stored in the holder.
258 index -= holder->map()->inobject_properties();
259 if (index < 0) {
260 // Get the property straight out of the holder.
261 int offset = holder->map()->instance_size() + (index * kPointerSize);
262 __ mov(dst, FieldOperand(src, offset));
263 } else {
264 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000265 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000266 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
267 __ mov(dst, FieldOperand(dst, offset));
268 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269}
270
271
ager@chromium.org5c838252010-02-19 08:53:10 +0000272static void PushInterceptorArguments(MacroAssembler* masm,
273 Register receiver,
274 Register holder,
275 Register name,
276 JSObject* holder_obj) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000277 __ push(name);
278 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
279 ASSERT(!Heap::InNewSpace(interceptor));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000280 Register scratch = name;
281 __ mov(scratch, Immediate(Handle<Object>(interceptor)));
282 __ push(scratch);
ager@chromium.org5c838252010-02-19 08:53:10 +0000283 __ push(receiver);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000284 __ push(holder);
285 __ push(FieldOperand(scratch, InterceptorInfo::kDataOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000286}
287
288
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000289static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
290 Register receiver,
291 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000292 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000293 JSObject* holder_obj) {
294 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
ager@chromium.org5c838252010-02-19 08:53:10 +0000295 __ CallExternalReference(
296 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly)),
297 5);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000298}
299
300
301template <class Compiler>
302static void CompileLoadInterceptor(Compiler* compiler,
303 StubCompiler* stub_compiler,
304 MacroAssembler* masm,
305 JSObject* object,
306 JSObject* holder,
307 String* name,
308 LookupResult* lookup,
309 Register receiver,
310 Register scratch1,
311 Register scratch2,
312 Label* miss) {
313 ASSERT(holder->HasNamedInterceptor());
314 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
315
316 // Check that the receiver isn't a smi.
317 __ test(receiver, Immediate(kSmiTagMask));
318 __ j(zero, miss, not_taken);
319
320 // Check that the maps haven't changed.
321 Register reg =
322 stub_compiler->CheckPrototypes(object, receiver, holder,
323 scratch1, scratch2, name, miss);
324
ager@chromium.org5c838252010-02-19 08:53:10 +0000325 if (lookup->IsProperty() && lookup->IsCacheable()) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000326 compiler->CompileCacheable(masm,
327 stub_compiler,
328 receiver,
329 reg,
330 scratch1,
331 scratch2,
332 holder,
333 lookup,
334 name,
335 miss);
336 } else {
337 compiler->CompileRegular(masm,
338 receiver,
339 reg,
340 scratch2,
341 holder,
342 miss);
343 }
344}
345
346
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000347class LoadInterceptorCompiler BASE_EMBEDDED {
348 public:
349 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
350
351 void CompileCacheable(MacroAssembler* masm,
352 StubCompiler* stub_compiler,
353 Register receiver,
354 Register holder,
355 Register scratch1,
356 Register scratch2,
357 JSObject* holder_obj,
358 LookupResult* lookup,
359 String* name,
360 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000361 AccessorInfo* callback = NULL;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000362 bool optimize = false;
363 // So far the most popular follow ups for interceptor loads are FIELD
364 // and CALLBACKS, so inline only them, other cases may be added
365 // later.
366 if (lookup->type() == FIELD) {
367 optimize = true;
368 } else if (lookup->type() == CALLBACKS) {
369 Object* callback_object = lookup->GetCallbackObject();
370 if (callback_object->IsAccessorInfo()) {
371 callback = AccessorInfo::cast(callback_object);
372 optimize = callback->getter() != NULL;
373 }
374 }
375
376 if (!optimize) {
377 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
378 return;
379 }
380
381 // Note: starting a frame here makes GC aware of pointers pushed below.
382 __ EnterInternalFrame();
383
384 if (lookup->type() == CALLBACKS) {
385 __ push(receiver);
386 }
387 __ push(holder);
388 __ push(name_);
389
390 CompileCallLoadPropertyWithInterceptor(masm,
391 receiver,
392 holder,
393 name_,
394 holder_obj);
395
396 Label interceptor_failed;
397 __ cmp(eax, Factory::no_interceptor_result_sentinel());
398 __ j(equal, &interceptor_failed);
399 __ LeaveInternalFrame();
400 __ ret(0);
401
402 __ bind(&interceptor_failed);
403 __ pop(name_);
404 __ pop(holder);
405 if (lookup->type() == CALLBACKS) {
406 __ pop(receiver);
407 }
408
409 __ LeaveInternalFrame();
410
411 if (lookup->type() == FIELD) {
412 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
413 lookup->holder(), scratch1,
414 scratch2,
415 name,
416 miss_label);
417 stub_compiler->GenerateFastPropertyLoad(masm, eax,
418 holder, lookup->holder(),
419 lookup->GetFieldIndex());
420 __ ret(0);
421 } else {
422 ASSERT(lookup->type() == CALLBACKS);
423 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
424 ASSERT(callback != NULL);
425 ASSERT(callback->getter() != NULL);
426
427 Label cleanup;
428 __ pop(scratch2);
429 __ push(receiver);
430 __ push(scratch2);
431
432 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
433 lookup->holder(), scratch1,
434 scratch2,
435 name,
436 &cleanup);
437
438 __ pop(scratch2); // save old return address
439 __ push(holder);
440 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
441 __ push(holder);
442 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
443 __ push(name_);
444 __ push(scratch2); // restore old return address
445
446 ExternalReference ref =
447 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000448 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000449
450 __ bind(&cleanup);
451 __ pop(scratch1);
452 __ pop(scratch2);
453 __ push(scratch1);
454 }
455 }
456
457
458 void CompileRegular(MacroAssembler* masm,
459 Register receiver,
460 Register holder,
461 Register scratch,
462 JSObject* holder_obj,
463 Label* miss_label) {
464 __ pop(scratch); // save old return address
465 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
466 __ push(scratch); // restore old return address
467
468 ExternalReference ref = ExternalReference(
469 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000470 __ TailCallExternalReference(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000471 }
472
473 private:
474 Register name_;
475};
476
477
ager@chromium.org5c838252010-02-19 08:53:10 +0000478// Reserves space for the extra arguments to FastHandleApiCall in the
479// caller's frame.
480//
481// These arguments are set by CheckPrototypes and GenerateFastApiCall.
482static void ReserveSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
483 // ----------- S t a t e -------------
484 // -- esp[0] : return address
485 // -- esp[4] : last argument in the internal frame of the caller
486 // -----------------------------------
487 __ pop(scratch);
488 __ push(Immediate(Smi::FromInt(0)));
489 __ push(Immediate(Smi::FromInt(0)));
490 __ push(Immediate(Smi::FromInt(0)));
491 __ push(Immediate(Smi::FromInt(0)));
492 __ push(scratch);
493}
494
495
496// Undoes the effects of ReserveSpaceForFastApiCall.
497static void FreeSpaceForFastApiCall(MacroAssembler* masm, Register scratch) {
498 // ----------- S t a t e -------------
499 // -- esp[0] : return address
500 // -- esp[4] : last fast api call extra argument
501 // -- ...
502 // -- esp[16] : first fast api call extra argument
503 // -- esp[20] : last argument in the internal frame
504 // -----------------------------------
505 __ pop(scratch);
506 __ add(Operand(esp), Immediate(kPointerSize * 4));
507 __ push(scratch);
508}
509
510
511// Generates call to FastHandleApiCall builtin.
512static void GenerateFastApiCall(MacroAssembler* masm,
513 const CallOptimization& optimization,
514 int argc) {
515 // ----------- S t a t e -------------
516 // -- esp[0] : return address
517 // -- esp[4] : object passing the type check
518 // (last fast api call extra argument,
519 // set by CheckPrototypes)
520 // -- esp[8] : api call data
521 // -- esp[12] : api callback
522 // -- esp[16] : api function
523 // (first fast api call extra argument)
524 // -- esp[20] : last argument
525 // -- ...
526 // -- esp[(argc + 5) * 4] : first argument
527 // -- esp[(argc + 6) * 4] : receiver
528 // -----------------------------------
529
530 // Get the function and setup the context.
531 JSFunction* function = optimization.constant_function();
532 __ mov(edi, Immediate(Handle<JSFunction>(function)));
533 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
534
535 // Pass the additional arguments FastHandleApiCall expects.
536 __ mov(Operand(esp, 4 * kPointerSize), edi);
537 bool info_loaded = false;
538 Object* callback = optimization.api_call_info()->callback();
539 if (Heap::InNewSpace(callback)) {
540 info_loaded = true;
541 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
542 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kCallbackOffset));
543 __ mov(Operand(esp, 3 * kPointerSize), ebx);
544 } else {
545 __ mov(Operand(esp, 3 * kPointerSize), Immediate(Handle<Object>(callback)));
546 }
547 Object* call_data = optimization.api_call_info()->data();
548 if (Heap::InNewSpace(call_data)) {
549 if (!info_loaded) {
550 __ mov(ecx, Handle<CallHandlerInfo>(optimization.api_call_info()));
551 }
552 __ mov(ebx, FieldOperand(ecx, CallHandlerInfo::kDataOffset));
553 __ mov(Operand(esp, 2 * kPointerSize), ebx);
554 } else {
555 __ mov(Operand(esp, 2 * kPointerSize),
556 Immediate(Handle<Object>(call_data)));
557 }
558
559 // Set the number of arguments.
560 __ mov(eax, Immediate(argc + 4));
561
562 // Jump to the fast api call builtin (tail call).
563 Handle<Code> code = Handle<Code>(
564 Builtins::builtin(Builtins::FastHandleApiCall));
565 ParameterCount expected(0);
566 __ InvokeCode(code, expected, expected,
567 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
568}
569
570
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000571class CallInterceptorCompiler BASE_EMBEDDED {
572 public:
ager@chromium.org5c838252010-02-19 08:53:10 +0000573 CallInterceptorCompiler(StubCompiler* stub_compiler,
574 const ParameterCount& arguments,
575 Register name)
576 : stub_compiler_(stub_compiler),
577 arguments_(arguments),
578 name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000579
ager@chromium.org5c838252010-02-19 08:53:10 +0000580 void Compile(MacroAssembler* masm,
581 JSObject* object,
582 JSObject* holder,
583 String* name,
584 LookupResult* lookup,
585 Register receiver,
586 Register scratch1,
587 Register scratch2,
588 Label* miss) {
589 ASSERT(holder->HasNamedInterceptor());
590 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
591
592 // Check that the receiver isn't a smi.
593 __ test(receiver, Immediate(kSmiTagMask));
594 __ j(zero, miss, not_taken);
595
596 CallOptimization optimization(lookup);
597
vegorov@chromium.orgf8372902010-03-15 10:26:20 +0000598 if (optimization.is_constant_call()) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000599 CompileCacheable(masm,
600 object,
601 receiver,
602 scratch1,
603 scratch2,
604 holder,
605 lookup,
606 name,
607 optimization,
608 miss);
609 } else {
610 CompileRegular(masm,
611 object,
612 receiver,
613 scratch1,
614 scratch2,
615 name,
616 holder,
617 miss);
618 }
619 }
620
621 private:
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000622 void CompileCacheable(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000623 JSObject* object,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000624 Register receiver,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000625 Register scratch1,
626 Register scratch2,
627 JSObject* holder_obj,
628 LookupResult* lookup,
629 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000630 const CallOptimization& optimization,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000631 Label* miss_label) {
ager@chromium.org5c838252010-02-19 08:53:10 +0000632 ASSERT(optimization.is_constant_call());
633 ASSERT(!lookup->holder()->IsGlobalObject());
634
635 int depth1 = kInvalidProtoDepth;
636 int depth2 = kInvalidProtoDepth;
637 bool can_do_fast_api_call = false;
638 if (optimization.is_simple_api_call() &&
639 !lookup->holder()->IsGlobalObject()) {
640 depth1 = optimization.GetPrototypeDepthOfExpectedType(object, holder_obj);
641 if (depth1 == kInvalidProtoDepth) {
642 depth2 = optimization.GetPrototypeDepthOfExpectedType(holder_obj,
643 lookup->holder());
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000644 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000645 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
646 (depth2 != kInvalidProtoDepth);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000647 }
648
ager@chromium.org5c838252010-02-19 08:53:10 +0000649 __ IncrementCounter(&Counters::call_const_interceptor, 1);
650
651 if (can_do_fast_api_call) {
652 __ IncrementCounter(&Counters::call_const_interceptor_fast_api, 1);
653 ReserveSpaceForFastApiCall(masm, scratch1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000654 }
655
ager@chromium.org5c838252010-02-19 08:53:10 +0000656 Label miss_cleanup;
657 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
658 Register holder =
659 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
660 scratch1, scratch2, name,
661 depth1, miss);
662
663 Label regular_invoke;
664 LoadWithInterceptor(masm, receiver, holder, holder_obj, &regular_invoke);
665
666 // Generate code for the failed interceptor case.
667
668 // Check the lookup is still valid.
669 stub_compiler_->CheckPrototypes(holder_obj, receiver,
670 lookup->holder(),
671 scratch1, scratch2, name,
672 depth2, miss);
673
674 if (can_do_fast_api_call) {
675 GenerateFastApiCall(masm, optimization, arguments_.immediate());
676 } else {
677 __ InvokeFunction(optimization.constant_function(), arguments_,
678 JUMP_FUNCTION);
679 }
680
681 if (can_do_fast_api_call) {
682 __ bind(&miss_cleanup);
683 FreeSpaceForFastApiCall(masm, scratch1);
684 __ jmp(miss_label);
685 }
686
687 __ bind(&regular_invoke);
688 if (can_do_fast_api_call) {
689 FreeSpaceForFastApiCall(masm, scratch1);
690 }
691 }
692
693 void CompileRegular(MacroAssembler* masm,
694 JSObject* object,
695 Register receiver,
696 Register scratch1,
697 Register scratch2,
698 String* name,
699 JSObject* holder_obj,
700 Label* miss_label) {
701 Register holder =
702 stub_compiler_->CheckPrototypes(object, receiver, holder_obj,
703 scratch1, scratch2, name,
704 miss_label);
705
706 __ EnterInternalFrame();
707 // Save the name_ register across the call.
708 __ push(name_);
709
710 PushInterceptorArguments(masm,
711 receiver,
712 holder,
713 name_,
714 holder_obj);
715
716 __ CallExternalReference(
717 ExternalReference(
718 IC_Utility(IC::kLoadPropertyWithInterceptorForCall)),
719 5);
720
721 // Restore the name_ register.
722 __ pop(name_);
723 __ LeaveInternalFrame();
724 }
725
726 void LoadWithInterceptor(MacroAssembler* masm,
727 Register receiver,
728 Register holder,
729 JSObject* holder_obj,
730 Label* interceptor_succeeded) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000731 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000732 __ push(holder); // Save the holder.
733 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000734
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000735 CompileCallLoadPropertyWithInterceptor(masm,
736 receiver,
737 holder,
738 name_,
739 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000740
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000741 __ pop(name_); // Restore the name.
742 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000743 __ LeaveInternalFrame();
744
745 __ cmp(eax, Factory::no_interceptor_result_sentinel());
ager@chromium.org5c838252010-02-19 08:53:10 +0000746 __ j(not_equal, interceptor_succeeded);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000747 }
748
ager@chromium.org5c838252010-02-19 08:53:10 +0000749 StubCompiler* stub_compiler_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000750 const ParameterCount& arguments_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000751 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000752};
753
754
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
756 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
757 Code* code = NULL;
758 if (kind == Code::LOAD_IC) {
759 code = Builtins::builtin(Builtins::LoadIC_Miss);
760 } else {
761 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
762 }
763
764 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000765 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000766}
767
768
ager@chromium.org5c838252010-02-19 08:53:10 +0000769// Both name_reg and receiver_reg are preserved on jumps to miss_label,
770// but may be destroyed if store is successful.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000771void StubCompiler::GenerateStoreField(MacroAssembler* masm,
772 JSObject* object,
773 int index,
774 Map* transition,
775 Register receiver_reg,
776 Register name_reg,
777 Register scratch,
778 Label* miss_label) {
779 // Check that the object isn't a smi.
780 __ test(receiver_reg, Immediate(kSmiTagMask));
781 __ j(zero, miss_label, not_taken);
782
783 // Check that the map of the object hasn't changed.
784 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
785 Immediate(Handle<Map>(object->map())));
786 __ j(not_equal, miss_label, not_taken);
787
788 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000789 if (object->IsJSGlobalProxy()) {
790 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000791 }
792
793 // Stub never generated for non-global objects that require access
794 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000795 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000796
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000797 // Perform map transition for the receiver if necessary.
798 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
799 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000800 // We jump to a runtime call that extends the properties array.
ager@chromium.org5c838252010-02-19 08:53:10 +0000801 __ pop(scratch); // Return address.
802 __ push(receiver_reg);
803 __ push(Immediate(Handle<Map>(transition)));
804 __ push(eax);
805 __ push(scratch);
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000806 __ TailCallExternalReference(
ager@chromium.org5c838252010-02-19 08:53:10 +0000807 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage)), 3, 1);
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000808 return;
809 }
810
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000811 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000812 // Update the map of the object; no write barrier updating is
813 // needed because the map is never in new space.
814 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
815 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000816 }
817
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000818 // Adjust for the number of properties stored in the object. Even in the
819 // face of a transition we can use the old map here because the size of the
820 // object and the number of in-object properties is not going to change.
821 index -= object->map()->inobject_properties();
822
ager@chromium.org7c537e22008-10-16 08:43:32 +0000823 if (index < 0) {
824 // Set the property straight into the object.
825 int offset = object->map()->instance_size() + (index * kPointerSize);
826 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000827
ager@chromium.org7c537e22008-10-16 08:43:32 +0000828 // Update the write barrier for the array address.
829 // Pass the value being stored in the now unused name_reg.
830 __ mov(name_reg, Operand(eax));
831 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
832 } else {
833 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000834 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000835 // Get the properties array (optimistically).
836 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000837 __ mov(FieldOperand(scratch, offset), eax);
838
839 // Update the write barrier for the array address.
840 // Pass the value being stored in the now unused name_reg.
841 __ mov(name_reg, Operand(eax));
842 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
843 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000844
845 // Return the value (register eax).
846 __ ret(0);
847}
848
849
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000850// Generate code to check that a global property cell is empty. Create
851// the property cell at compilation time if no cell exists for the
852// property.
853static Object* GenerateCheckPropertyCell(MacroAssembler* masm,
854 GlobalObject* global,
855 String* name,
856 Register scratch,
857 Label* miss) {
858 Object* probe = global->EnsurePropertyCell(name);
859 if (probe->IsFailure()) return probe;
860 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
861 ASSERT(cell->value()->IsTheHole());
862 __ mov(scratch, Immediate(Handle<Object>(cell)));
863 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
864 Immediate(Factory::the_hole_value()));
865 __ j(not_equal, miss, not_taken);
866 return cell;
867}
868
869
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000870#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000871#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000872
873
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000874Register StubCompiler::CheckPrototypes(JSObject* object,
875 Register object_reg,
876 JSObject* holder,
877 Register holder_reg,
878 Register scratch,
879 String* name,
ager@chromium.org5c838252010-02-19 08:53:10 +0000880 int push_at_depth,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000881 Label* miss) {
882 // Check that the maps haven't changed.
883 Register result =
ager@chromium.org5c838252010-02-19 08:53:10 +0000884 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch,
885 push_at_depth, miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000886
887 // If we've skipped any global objects, it's not enough to verify
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000888 // that their maps haven't changed. We also need to check that the
889 // property cell for the property is still empty.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000890 while (object != holder) {
891 if (object->IsGlobalObject()) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000892 Object* cell = GenerateCheckPropertyCell(masm(),
893 GlobalObject::cast(object),
894 name,
895 scratch,
896 miss);
897 if (cell->IsFailure()) {
898 set_failure(Failure::cast(cell));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000899 return result;
900 }
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000901 }
902 object = JSObject::cast(object->GetPrototype());
903 }
904
ager@chromium.org5c838252010-02-19 08:53:10 +0000905 // Return the register containing the holder.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000906 return result;
907}
908
909
910void StubCompiler::GenerateLoadField(JSObject* object,
911 JSObject* holder,
912 Register receiver,
913 Register scratch1,
914 Register scratch2,
915 int index,
916 String* name,
917 Label* miss) {
918 // Check that the receiver isn't a smi.
919 __ test(receiver, Immediate(kSmiTagMask));
920 __ j(zero, miss, not_taken);
921
922 // Check the prototype chain.
923 Register reg =
924 CheckPrototypes(object, receiver, holder,
925 scratch1, scratch2, name, miss);
926
927 // Get the value from the properties.
928 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
929 __ ret(0);
930}
931
932
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000933bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000934 JSObject* holder,
935 Register receiver,
936 Register name_reg,
937 Register scratch1,
938 Register scratch2,
939 AccessorInfo* callback,
940 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000941 Label* miss,
942 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000943 // Check that the receiver isn't a smi.
944 __ test(receiver, Immediate(kSmiTagMask));
945 __ j(zero, miss, not_taken);
946
947 // Check that the maps haven't changed.
948 Register reg =
949 CheckPrototypes(object, receiver, holder,
950 scratch1, scratch2, name, miss);
951
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000952 Handle<AccessorInfo> callback_handle(callback);
953
954 Register other = reg.is(scratch1) ? scratch2 : scratch1;
955 __ EnterInternalFrame();
956 __ PushHandleScope(other);
957 // Push the stack address where the list of arguments ends
958 __ mov(other, esp);
959 __ sub(Operand(other), Immediate(2 * kPointerSize));
960 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000961 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000962 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000963 __ mov(other, Immediate(callback_handle));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000964 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000965 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000966 // Save a pointer to where we pushed the arguments pointer.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000967 // This will be passed as the const AccessorInfo& to the C++ callback.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000968 __ mov(eax, esp);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000969 __ add(Operand(eax), Immediate(4 * kPointerSize));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000970 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000971
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000972 // Do call through the api.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +0000973 ASSERT_EQ(5, ApiGetterEntryStub::kStackSpace);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000974 Address getter_address = v8::ToCData<Address>(callback->getter());
975 ApiFunction fun(getter_address);
976 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000977 // Emitting a stub call may try to allocate (if the code is not
978 // already generated). Do not allow the assembler to perform a
979 // garbage collection but instead return the allocation failure
980 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000981 Object* result = masm()->TryCallStub(&stub);
982 if (result->IsFailure()) {
983 *failure = Failure::cast(result);
984 return false;
985 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000986
987 // We need to avoid using eax since that now holds the result.
988 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000989 // Emitting PopHandleScope may try to allocate. Do not allow the
990 // assembler to perform a garbage collection but instead return a
991 // failure object.
992 result = masm()->TryPopHandleScope(eax, tmp);
993 if (result->IsFailure()) {
994 *failure = Failure::cast(result);
995 return false;
996 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000997 __ LeaveInternalFrame();
998
999 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001000 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001001}
1002
1003
1004void StubCompiler::GenerateLoadConstant(JSObject* object,
1005 JSObject* holder,
1006 Register receiver,
1007 Register scratch1,
1008 Register scratch2,
1009 Object* value,
1010 String* name,
1011 Label* miss) {
1012 // Check that the receiver isn't a smi.
1013 __ test(receiver, Immediate(kSmiTagMask));
1014 __ j(zero, miss, not_taken);
1015
1016 // Check that the maps haven't changed.
1017 Register reg =
1018 CheckPrototypes(object, receiver, holder,
1019 scratch1, scratch2, name, miss);
1020
1021 // Return the constant value.
1022 __ mov(eax, Handle<Object>(value));
1023 __ ret(0);
1024}
1025
1026
1027void StubCompiler::GenerateLoadInterceptor(JSObject* object,
1028 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001029 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001030 Register receiver,
1031 Register name_reg,
1032 Register scratch1,
1033 Register scratch2,
1034 String* name,
1035 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001036 LoadInterceptorCompiler compiler(name_reg);
1037 CompileLoadInterceptor(&compiler,
1038 this,
1039 masm(),
1040 object,
1041 holder,
1042 name,
1043 lookup,
1044 receiver,
1045 scratch1,
1046 scratch2,
1047 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001048}
1049
1050
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001051// TODO(1241006): Avoid having lazy compile stubs specialized by the
1052// number of arguments. It is not needed anymore.
1053Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001054 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +00001055 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001056
1057 // Push a copy of the function onto the stack.
1058 __ push(edi);
1059
1060 __ push(edi); // function is also the parameter to the runtime call
1061 __ CallRuntime(Runtime::kLazyCompile, 1);
1062 __ pop(edi);
1063
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001064 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +00001065 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001066
1067 // Do a tail-call of the compiled function.
1068 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
1069 __ jmp(Operand(ecx));
1070
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001071 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001072}
1073
1074
ager@chromium.org5c838252010-02-19 08:53:10 +00001075Object* CallStubCompiler::CompileCallField(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001077 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001078 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001079 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001080 // -- ecx : name
1081 // -- esp[0] : return address
1082 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1083 // -- ...
1084 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001085 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001086 Label miss;
1087
1088 // Get the receiver from the stack.
1089 const int argc = arguments().immediate();
1090 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1091
1092 // Check that the receiver isn't a smi.
1093 __ test(edx, Immediate(kSmiTagMask));
1094 __ j(zero, &miss, not_taken);
1095
1096 // Do the right check and compute the holder register.
ager@chromium.org5c838252010-02-19 08:53:10 +00001097 Register reg = CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098
ager@chromium.org7c537e22008-10-16 08:43:32 +00001099 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100
1101 // Check that the function really is a function.
1102 __ test(edi, Immediate(kSmiTagMask));
1103 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001104 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001105 __ j(not_equal, &miss, not_taken);
1106
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001107 // Patch the receiver on the stack with the global proxy if
1108 // necessary.
1109 if (object->IsGlobalObject()) {
1110 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1111 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1112 }
1113
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001114 // Invoke the function.
1115 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1116
1117 // Handle call cache miss.
1118 __ bind(&miss);
1119 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001120 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001121
1122 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001123 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001124}
1125
1126
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001127Object* CallStubCompiler::CompileArrayPushCall(Object* object,
1128 JSObject* holder,
1129 JSFunction* function,
1130 String* name,
1131 CheckType check) {
1132 // ----------- S t a t e -------------
1133 // -- ecx : name
1134 // -- esp[0] : return address
1135 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1136 // -- ...
1137 // -- esp[(argc + 1) * 4] : receiver
1138 // -----------------------------------
1139 ASSERT(check == RECEIVER_MAP_CHECK);
1140
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001141 // If object is not an array, bail out to regular call.
1142 if (!object->IsJSArray()) {
1143 return Heap::undefined_value();
1144 }
1145
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001146 Label miss;
1147
1148 // Get the receiver from the stack.
1149 const int argc = arguments().immediate();
1150 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1151
1152 // Check that the receiver isn't a smi.
1153 __ test(edx, Immediate(kSmiTagMask));
1154 __ j(zero, &miss);
1155
1156 CheckPrototypes(JSObject::cast(object), edx,
1157 holder, ebx,
1158 eax, name, &miss);
1159
1160 if (argc == 0) {
1161 // Noop, return the length.
1162 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1163 __ ret((argc + 1) * kPointerSize);
1164 } else {
1165 // Get the elements array of the object.
1166 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1167
1168 // Check that the elements are in fast mode (not dictionary).
1169 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1170 Immediate(Factory::fixed_array_map()));
1171 __ j(not_equal, &miss);
1172
1173 if (argc == 1) { // Otherwise fall through to call builtin.
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001174 Label call_builtin, exit, with_rset_update, attempt_to_grow_elements;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001175
1176 // Get the array's length into eax and calculate new length.
1177 __ mov(eax, FieldOperand(edx, JSArray::kLengthOffset));
1178 STATIC_ASSERT(kSmiTagSize == 1);
1179 STATIC_ASSERT(kSmiTag == 0);
ager@chromium.orgac091b72010-05-05 07:34:42 +00001180 __ add(Operand(eax), Immediate(Smi::FromInt(argc)));
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001181
1182 // Get the element's length into ecx.
1183 __ mov(ecx, FieldOperand(ebx, FixedArray::kLengthOffset));
1184 __ SmiTag(ecx);
1185
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001186 // Check if we could survive without allocation.
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001187 __ cmp(eax, Operand(ecx));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001188 __ j(greater, &attempt_to_grow_elements);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001189
1190 // Save new length.
1191 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1192
1193 // Push the element.
1194 __ lea(edx, FieldOperand(ebx,
1195 eax, times_half_pointer_size,
1196 FixedArray::kHeaderSize - argc * kPointerSize));
1197 __ mov(ecx, Operand(esp, argc * kPointerSize));
1198 __ mov(Operand(edx, 0), ecx);
1199
1200 // Check if value is a smi.
1201 __ test(ecx, Immediate(kSmiTagMask));
1202 __ j(not_zero, &with_rset_update);
1203
1204 __ bind(&exit);
1205 __ ret((argc + 1) * kPointerSize);
1206
1207 __ bind(&with_rset_update);
1208
1209 __ InNewSpace(ebx, ecx, equal, &exit);
1210
1211 RecordWriteStub stub(ebx, edx, ecx);
1212 __ CallStub(&stub);
1213 __ ret((argc + 1) * kPointerSize);
1214
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001215 __ bind(&attempt_to_grow_elements);
1216 ExternalReference new_space_allocation_top =
1217 ExternalReference::new_space_allocation_top_address();
1218 ExternalReference new_space_allocation_limit =
1219 ExternalReference::new_space_allocation_limit_address();
1220
1221 const int kAllocationDelta = 4;
1222 // Load top.
1223 __ mov(ecx, Operand::StaticVariable(new_space_allocation_top));
1224
1225 // Check if it's the end of elements.
1226 __ lea(edx, FieldOperand(ebx,
1227 eax, times_half_pointer_size,
1228 FixedArray::kHeaderSize - argc * kPointerSize));
1229 __ cmp(edx, Operand(ecx));
1230 __ j(not_equal, &call_builtin);
1231 __ add(Operand(ecx), Immediate(kAllocationDelta * kPointerSize));
1232 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001233 __ j(above, &call_builtin);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001234
1235 // We fit and could grow elements.
1236 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
1237 __ mov(ecx, Operand(esp, argc * kPointerSize));
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001238
1239 // Push the argument...
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001240 __ mov(Operand(edx, 0), ecx);
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001241 // ... and fill the rest with holes.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001242 for (int i = 1; i < kAllocationDelta; i++) {
1243 __ mov(Operand(edx, i * kPointerSize),
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001244 Immediate(Factory::the_hole_value()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001245 }
1246
1247 // Restore receiver to edx as finish sequence assumes it's here.
1248 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1249
1250 // Increment element's and array's sizes.
1251 __ add(FieldOperand(ebx, FixedArray::kLengthOffset),
1252 Immediate(kAllocationDelta));
1253 __ mov(FieldOperand(edx, JSArray::kLengthOffset), eax);
1254
ager@chromium.orgb26c50a2010-03-26 09:27:16 +00001255 // Elements are in new space, so no remembered set updates are necessary.
1256 __ ret((argc + 1) * kPointerSize);
fschneider@chromium.org086aac62010-03-17 13:18:24 +00001257
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001258 __ bind(&call_builtin);
1259 }
1260
1261 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush),
1262 argc + 1,
1263 1);
1264 }
1265
1266 __ bind(&miss);
1267
1268 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1269 __ jmp(ic, RelocInfo::CODE_TARGET);
1270
1271 // Return the generated code.
1272 String* function_name = NULL;
1273 if (function->shared()->name()->IsString()) {
1274 function_name = String::cast(function->shared()->name());
1275 }
1276 return GetCode(CONSTANT_FUNCTION, function_name);
1277}
1278
1279
1280Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1281 JSObject* holder,
1282 JSFunction* function,
1283 String* name,
1284 CheckType check) {
1285 // ----------- S t a t e -------------
1286 // -- ecx : name
1287 // -- esp[0] : return address
1288 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1289 // -- ...
1290 // -- esp[(argc + 1) * 4] : receiver
1291 // -----------------------------------
1292 ASSERT(check == RECEIVER_MAP_CHECK);
1293
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001294 // If object is not an array, bail out to regular call.
1295 if (!object->IsJSArray()) {
1296 return Heap::undefined_value();
1297 }
1298
ager@chromium.orgac091b72010-05-05 07:34:42 +00001299 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001300
1301 // Get the receiver from the stack.
1302 const int argc = arguments().immediate();
1303 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1304
1305 // Check that the receiver isn't a smi.
1306 __ test(edx, Immediate(kSmiTagMask));
1307 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001308 CheckPrototypes(JSObject::cast(object), edx,
1309 holder, ebx,
1310 eax, name, &miss);
1311
1312 // Get the elements array of the object.
1313 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1314
1315 // Check that the elements are in fast mode (not dictionary).
1316 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1317 Immediate(Factory::fixed_array_map()));
1318 __ j(not_equal, &miss);
1319
1320 // Get the array's length into ecx and calculate new length.
1321 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1322 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001323 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001324
1325 // Get the last element.
1326 STATIC_ASSERT(kSmiTagSize == 1);
1327 STATIC_ASSERT(kSmiTag == 0);
1328 __ mov(eax, FieldOperand(ebx,
1329 ecx, times_half_pointer_size,
1330 FixedArray::kHeaderSize));
1331 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1332 __ j(equal, &call_builtin);
1333
1334 // Set the array's length.
1335 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1336
1337 // Fill with the hole.
1338 __ mov(FieldOperand(ebx,
1339 ecx, times_half_pointer_size,
1340 FixedArray::kHeaderSize),
1341 Immediate(Factory::the_hole_value()));
1342 __ ret((argc + 1) * kPointerSize);
1343
ager@chromium.orgac091b72010-05-05 07:34:42 +00001344 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001345 __ mov(eax, Immediate(Factory::undefined_value()));
1346 __ ret((argc + 1) * kPointerSize);
1347
1348 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001349 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1350 argc + 1,
1351 1);
1352
1353 __ bind(&miss);
1354
1355 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1356 __ jmp(ic, RelocInfo::CODE_TARGET);
1357
1358 // Return the generated code.
1359 String* function_name = NULL;
1360 if (function->shared()->name()->IsString()) {
1361 function_name = String::cast(function->shared()->name());
1362 }
1363 return GetCode(CONSTANT_FUNCTION, function_name);
1364}
1365
1366
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367Object* CallStubCompiler::CompileCallConstant(Object* object,
1368 JSObject* holder,
1369 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001370 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001371 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001372 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001373 // -- ecx : name
1374 // -- esp[0] : return address
1375 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1376 // -- ...
1377 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001378 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001379
1380 SharedFunctionInfo* function_info = function->shared();
1381 if (function_info->HasCustomCallGenerator()) {
1382 CustomCallGenerator generator =
1383 ToCData<CustomCallGenerator>(function_info->function_data());
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001384 Object* result = generator(this, object, holder, function, name, check);
1385 // undefined means bail out to regular compiler.
1386 if (!result->IsUndefined()) {
1387 return result;
1388 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001389 }
1390
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001391 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001392
1393 // Get the receiver from the stack.
1394 const int argc = arguments().immediate();
1395 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1396
1397 // Check that the receiver isn't a smi.
1398 if (check != NUMBER_CHECK) {
1399 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001400 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 }
1402
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001403 // Make sure that it's okay not to patch the on stack receiver
1404 // unless we're doing a receiver map check.
1405 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1406
ager@chromium.org5c838252010-02-19 08:53:10 +00001407 CallOptimization optimization(function);
1408 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001409 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001410
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 switch (check) {
1412 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001413 __ IncrementCounter(&Counters::call_const, 1);
1414
1415 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1416 depth = optimization.GetPrototypeDepthOfExpectedType(
1417 JSObject::cast(object), holder);
1418 }
1419
1420 if (depth != kInvalidProtoDepth) {
1421 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1422 ReserveSpaceForFastApiCall(masm(), eax);
1423 }
1424
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001425 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001426 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001427 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001428
1429 // Patch the receiver on the stack with the global proxy if
1430 // necessary.
1431 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001432 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001433 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1434 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1435 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436 break;
1437
1438 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001439 if (!function->IsBuiltin()) {
1440 // Calling non-builtins with a value as receiver requires boxing.
1441 __ jmp(&miss);
1442 } else {
1443 // Check that the object is a string or a symbol.
1444 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1445 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1446 __ cmp(eax, FIRST_NONSTRING_TYPE);
1447 __ j(above_equal, &miss, not_taken);
1448 // Check that the maps starting from the prototype haven't changed.
1449 GenerateLoadGlobalFunctionPrototype(masm(),
1450 Context::STRING_FUNCTION_INDEX,
1451 eax);
1452 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1453 ebx, edx, name, &miss);
1454 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001455 break;
1456
1457 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001458 if (!function->IsBuiltin()) {
1459 // Calling non-builtins with a value as receiver requires boxing.
1460 __ jmp(&miss);
1461 } else {
1462 Label fast;
1463 // Check that the object is a smi or a heap number.
1464 __ test(edx, Immediate(kSmiTagMask));
1465 __ j(zero, &fast, taken);
1466 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1467 __ j(not_equal, &miss, not_taken);
1468 __ bind(&fast);
1469 // Check that the maps starting from the prototype haven't changed.
1470 GenerateLoadGlobalFunctionPrototype(masm(),
1471 Context::NUMBER_FUNCTION_INDEX,
1472 eax);
1473 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1474 ebx, edx, name, &miss);
1475 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 break;
1477 }
1478
1479 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001480 if (!function->IsBuiltin()) {
1481 // Calling non-builtins with a value as receiver requires boxing.
1482 __ jmp(&miss);
1483 } else {
1484 Label fast;
1485 // Check that the object is a boolean.
1486 __ cmp(edx, Factory::true_value());
1487 __ j(equal, &fast, taken);
1488 __ cmp(edx, Factory::false_value());
1489 __ j(not_equal, &miss, not_taken);
1490 __ bind(&fast);
1491 // Check that the maps starting from the prototype haven't changed.
1492 GenerateLoadGlobalFunctionPrototype(masm(),
1493 Context::BOOLEAN_FUNCTION_INDEX,
1494 eax);
1495 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1496 ebx, edx, name, &miss);
1497 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001498 break;
1499 }
1500
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001501 default:
1502 UNREACHABLE();
1503 }
1504
ager@chromium.org5c838252010-02-19 08:53:10 +00001505 if (depth != kInvalidProtoDepth) {
1506 GenerateFastApiCall(masm(), optimization, argc);
1507 } else {
1508 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1509 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001510
1511 // Handle call cache miss.
1512 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001513 if (depth != kInvalidProtoDepth) {
1514 FreeSpaceForFastApiCall(masm(), eax);
1515 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001516 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001517 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001518 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001519
1520 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001521 String* function_name = NULL;
1522 if (function->shared()->name()->IsString()) {
1523 function_name = String::cast(function->shared()->name());
1524 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001525 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001526}
1527
1528
ager@chromium.org5c838252010-02-19 08:53:10 +00001529Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001530 JSObject* holder,
1531 String* name) {
1532 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001533 // -- ecx : name
1534 // -- esp[0] : return address
1535 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1536 // -- ...
1537 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001538 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001539 Label miss;
1540
1541 // Get the number of arguments.
1542 const int argc = arguments().immediate();
1543
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001544 LookupResult lookup;
1545 LookupPostInterceptor(holder, name, &lookup);
1546
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547 // Get the receiver from the stack.
1548 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001549
ager@chromium.org5c838252010-02-19 08:53:10 +00001550 CallInterceptorCompiler compiler(this, arguments(), ecx);
1551 compiler.Compile(masm(),
1552 object,
1553 holder,
1554 name,
1555 &lookup,
1556 edx,
1557 ebx,
1558 edi,
1559 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001560
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001561 // Restore receiver.
1562 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563
1564 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001565 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001566 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001567 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001568 __ j(not_equal, &miss, not_taken);
1569
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001570 // Patch the receiver on the stack with the global proxy if
1571 // necessary.
1572 if (object->IsGlobalObject()) {
1573 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1574 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1575 }
1576
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001577 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001578 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001579 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1580
1581 // Handle load cache miss.
1582 __ bind(&miss);
1583 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001584 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001585
1586 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001587 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001588}
1589
1590
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001591Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1592 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001593 JSGlobalPropertyCell* cell,
1594 JSFunction* function,
1595 String* name) {
1596 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001597 // -- ecx : name
1598 // -- esp[0] : return address
1599 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1600 // -- ...
1601 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001602 // -----------------------------------
1603 Label miss;
1604
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001605 // Get the number of arguments.
1606 const int argc = arguments().immediate();
1607
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001608 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001609 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001610
1611 // If the object is the holder then we know that it's a global
1612 // object which can only happen for contextual calls. In this case,
1613 // the receiver cannot be a smi.
1614 if (object != holder) {
1615 __ test(edx, Immediate(kSmiTagMask));
1616 __ j(zero, &miss, not_taken);
1617 }
1618
1619 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001620 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001621
1622 // Get the value from the cell.
1623 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1624 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1625
1626 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001627 if (Heap::InNewSpace(function)) {
1628 // We can't embed a pointer to a function in new space so we have
1629 // to verify that the shared function info is unchanged. This has
1630 // the nice side effect that multiple closures based on the same
1631 // function can all use this call IC. Before we load through the
1632 // function, we have to verify that it still is a function.
1633 __ test(edi, Immediate(kSmiTagMask));
1634 __ j(zero, &miss, not_taken);
1635 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1636 __ j(not_equal, &miss, not_taken);
1637
1638 // Check the shared function info. Make sure it hasn't changed.
1639 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1640 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1641 __ j(not_equal, &miss, not_taken);
1642 } else {
1643 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1644 __ j(not_equal, &miss, not_taken);
1645 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001646
1647 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001648 if (object->IsGlobalObject()) {
1649 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1650 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1651 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001652
1653 // Setup the context (function already in edi).
1654 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1655
1656 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001657 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001658 ASSERT(function->is_compiled());
1659 Handle<Code> code(function->code());
1660 ParameterCount expected(function->shared()->formal_parameter_count());
1661 __ InvokeCode(code, expected, arguments(),
1662 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1663
1664 // Handle call cache miss.
1665 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001666 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1667 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1668 __ jmp(ic, RelocInfo::CODE_TARGET);
1669
1670 // Return the generated code.
1671 return GetCode(NORMAL, name);
1672}
1673
1674
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1676 int index,
1677 Map* transition,
1678 String* name) {
1679 // ----------- S t a t e -------------
1680 // -- eax : value
1681 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001682 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001683 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 Label miss;
1686
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001687 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001688 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001689 object,
1690 index,
1691 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001692 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001693 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001694
1695 // Handle store cache miss.
1696 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001697 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001698 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001699 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700
1701 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001702 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703}
1704
1705
1706Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1707 AccessorInfo* callback,
1708 String* name) {
1709 // ----------- S t a t e -------------
1710 // -- eax : value
1711 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001712 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001713 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 Label miss;
1716
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001717 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001718 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001719 __ j(zero, &miss, not_taken);
1720
1721 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 Immediate(Handle<Map>(object->map())));
1724 __ j(not_equal, &miss, not_taken);
1725
1726 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001727 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001728 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729 }
1730
1731 // Stub never generated for non-global objects that require access
1732 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001733 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001734
1735 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001736 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1738 __ push(ecx); // name
1739 __ push(eax); // value
1740 __ push(ebx); // restore return address
1741
mads.s.ager31e71382008-08-13 09:32:07 +00001742 // Do tail-call to the runtime system.
1743 ExternalReference store_callback_property =
1744 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001745 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001746
1747 // Handle store cache miss.
1748 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001750 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751
1752 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001753 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001754}
1755
1756
1757Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1758 String* name) {
1759 // ----------- S t a t e -------------
1760 // -- eax : value
1761 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001762 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001763 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001764 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 Label miss;
1766
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001767 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001768 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001769 __ j(zero, &miss, not_taken);
1770
1771 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 Immediate(Handle<Map>(receiver->map())));
1774 __ j(not_equal, &miss, not_taken);
1775
1776 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001777 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001778 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 }
1780
1781 // Stub never generated for non-global objects that require access
1782 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001783 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784
1785 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001786 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001787 __ push(ecx); // name
1788 __ push(eax); // value
1789 __ push(ebx); // restore return address
1790
mads.s.ager31e71382008-08-13 09:32:07 +00001791 // Do tail-call to the runtime system.
1792 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001793 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001794 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001795
1796 // Handle store cache miss.
1797 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001798 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001799 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001800
1801 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001802 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001803}
1804
1805
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001806Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1807 JSGlobalPropertyCell* cell,
1808 String* name) {
1809 // ----------- S t a t e -------------
1810 // -- eax : value
1811 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001812 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001813 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001814 // -----------------------------------
1815 Label miss;
1816
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001817 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001818 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001819 Immediate(Handle<Map>(object->map())));
1820 __ j(not_equal, &miss, not_taken);
1821
1822 // Store the value in the cell.
1823 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1824 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1825
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001826 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001827 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001828 __ ret(0);
1829
1830 // Handle store cache miss.
1831 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001832 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1833 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1834 __ jmp(ic, RelocInfo::CODE_TARGET);
1835
1836 // Return the generated code.
1837 return GetCode(NORMAL, name);
1838}
1839
1840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1842 int index,
1843 Map* transition,
1844 String* name) {
1845 // ----------- S t a t e -------------
1846 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001847 // -- ecx : key
1848 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001849 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001850 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001851 Label miss;
1852
1853 __ IncrementCounter(&Counters::keyed_store_field, 1);
1854
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001855 // Check that the name has not changed.
1856 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1857 __ j(not_equal, &miss, not_taken);
1858
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001859 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001860 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001861 object,
1862 index,
1863 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001864 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001865 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001866
1867 // Handle store cache miss.
1868 __ bind(&miss);
1869 __ DecrementCounter(&Counters::keyed_store_field, 1);
1870 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001871 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872
1873 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001874 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001875}
1876
1877
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001878Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1879 JSObject* object,
1880 JSObject* last) {
1881 // ----------- S t a t e -------------
1882 // -- eax : receiver
1883 // -- ecx : name
1884 // -- esp[0] : return address
1885 // -----------------------------------
1886 Label miss;
1887
1888 // Check that the receiver isn't a smi.
1889 __ test(eax, Immediate(kSmiTagMask));
1890 __ j(zero, &miss, not_taken);
1891
1892 // Check the maps of the full prototype chain. Also check that
1893 // global property cells up to (but not including) the last object
1894 // in the prototype chain are empty.
1895 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1896
1897 // If the last object in the prototype chain is a global object,
1898 // check that the global property cell is empty.
1899 if (last->IsGlobalObject()) {
1900 Object* cell = GenerateCheckPropertyCell(masm(),
1901 GlobalObject::cast(last),
1902 name,
1903 edx,
1904 &miss);
1905 if (cell->IsFailure()) return cell;
1906 }
1907
1908 // Return undefined if maps of the full prototype chain are still the
1909 // same and no global property with this name contains a value.
1910 __ mov(eax, Factory::undefined_value());
1911 __ ret(0);
1912
1913 __ bind(&miss);
1914 GenerateLoadMiss(masm(), Code::LOAD_IC);
1915
1916 // Return the generated code.
1917 return GetCode(NONEXISTENT, Heap::empty_string());
1918}
1919
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001920
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001921Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1922 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 int index,
1924 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001925 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001926 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001927 // -- ecx : name
1928 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001930 Label miss;
1931
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001932 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 __ bind(&miss);
1934 GenerateLoadMiss(masm(), Code::LOAD_IC);
1935
1936 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001937 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001938}
1939
1940
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001941Object* LoadStubCompiler::CompileLoadCallback(String* name,
1942 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001944 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001945 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001946 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001947 // -- ecx : name
1948 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001950 Label miss;
1951
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001952 Failure* failure = Failure::InternalError();
1953 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1954 callback, name, &miss, &failure);
1955 if (!success) return failure;
1956
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 __ bind(&miss);
1958 GenerateLoadMiss(masm(), Code::LOAD_IC);
1959
1960 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001961 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001962}
1963
1964
1965Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1966 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 Object* value,
1968 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001969 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001970 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001971 // -- ecx : name
1972 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001973 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001974 Label miss;
1975
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001976 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001977 __ bind(&miss);
1978 GenerateLoadMiss(masm(), Code::LOAD_IC);
1979
1980 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001981 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001982}
1983
1984
1985Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1986 JSObject* holder,
1987 String* name) {
1988 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001989 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001990 // -- ecx : name
1991 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001992 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001993 Label miss;
1994
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001995 LookupResult lookup;
1996 LookupPostInterceptor(holder, name, &lookup);
1997
ager@chromium.orge2902be2009-06-08 12:21:35 +00001998 // TODO(368): Compile in the whole chain: all the interceptors in
1999 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002000 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002001 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002002 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002003 eax,
2004 ecx,
2005 edx,
2006 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002007 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002008 &miss);
2009
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002010 __ bind(&miss);
2011 GenerateLoadMiss(masm(), Code::LOAD_IC);
2012
2013 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002014 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002015}
2016
2017
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002018Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2019 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002020 JSGlobalPropertyCell* cell,
2021 String* name,
2022 bool is_dont_delete) {
2023 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002024 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002025 // -- ecx : name
2026 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002027 // -----------------------------------
2028 Label miss;
2029
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002030 // If the object is the holder then we know that it's a global
2031 // object which can only happen for contextual loads. In this case,
2032 // the receiver cannot be a smi.
2033 if (object != holder) {
2034 __ test(eax, Immediate(kSmiTagMask));
2035 __ j(zero, &miss, not_taken);
2036 }
2037
2038 // Check that the maps haven't changed.
2039 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002040
2041 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002042 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2043 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002044
2045 // Check for deleted property if property can actually be deleted.
2046 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002047 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002048 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002049 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002050 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002051 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002052 }
2053
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002054 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002055 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002056 __ ret(0);
2057
2058 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002059 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2060 GenerateLoadMiss(masm(), Code::LOAD_IC);
2061
2062 // Return the generated code.
2063 return GetCode(NORMAL, name);
2064}
2065
2066
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002067Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2068 JSObject* receiver,
2069 JSObject* holder,
2070 int index) {
2071 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002072 // -- eax : key
2073 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002074 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002075 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002076 Label miss;
2077
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078 __ IncrementCounter(&Counters::keyed_load_field, 1);
2079
2080 // Check that the name has not changed.
2081 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2082 __ j(not_equal, &miss, not_taken);
2083
ager@chromium.org5c838252010-02-19 08:53:10 +00002084 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002085
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002086 __ bind(&miss);
2087 __ DecrementCounter(&Counters::keyed_load_field, 1);
2088 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2089
2090 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002091 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092}
2093
2094
2095Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2096 JSObject* receiver,
2097 JSObject* holder,
2098 AccessorInfo* callback) {
2099 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002100 // -- eax : key
2101 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002102 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002104 Label miss;
2105
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002106 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2107
2108 // Check that the name has not changed.
2109 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2110 __ j(not_equal, &miss, not_taken);
2111
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002112 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002113 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002114 callback, name, &miss, &failure);
2115 if (!success) return failure;
2116
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002117 __ bind(&miss);
2118 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2119 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2120
2121 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002122 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123}
2124
2125
2126Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2127 JSObject* receiver,
2128 JSObject* holder,
2129 Object* value) {
2130 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002131 // -- eax : key
2132 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002133 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002134 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002135 Label miss;
2136
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2138
2139 // Check that the name has not changed.
2140 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2141 __ j(not_equal, &miss, not_taken);
2142
ager@chromium.org5c838252010-02-19 08:53:10 +00002143 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002144 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002145 __ bind(&miss);
2146 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2147 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2148
2149 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002150 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002151}
2152
2153
2154Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2155 JSObject* holder,
2156 String* name) {
2157 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002158 // -- eax : key
2159 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002160 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002161 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002162 Label miss;
2163
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002164 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2165
2166 // Check that the name has not changed.
2167 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2168 __ j(not_equal, &miss, not_taken);
2169
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002170 LookupResult lookup;
2171 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002172 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002173 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002174 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002175 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002176 eax,
2177 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002178 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002179 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002180 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002181 __ bind(&miss);
2182 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2183 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2184
2185 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002186 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002187}
2188
2189
2190
2191
2192Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2193 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002194 // -- eax : key
2195 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002196 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002197 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002198 Label miss;
2199
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002200 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2201
2202 // Check that the name has not changed.
2203 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2204 __ j(not_equal, &miss, not_taken);
2205
ager@chromium.org5c838252010-02-19 08:53:10 +00002206 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 __ bind(&miss);
2208 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2209 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2210
2211 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002212 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002213}
2214
2215
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002216Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002218 // -- eax : key
2219 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002220 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002221 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002222 Label miss;
2223
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002224 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2225
2226 // Check that the name has not changed.
2227 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2228 __ j(not_equal, &miss, not_taken);
2229
ager@chromium.org5c838252010-02-19 08:53:10 +00002230 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 __ bind(&miss);
2232 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2233 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2234
2235 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002236 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002237}
2238
2239
2240Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2241 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002242 // -- eax : key
2243 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002244 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002245 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002246 Label miss;
2247
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002248 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2249
2250 // Check that the name has not changed.
2251 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2252 __ j(not_equal, &miss, not_taken);
2253
ager@chromium.org5c838252010-02-19 08:53:10 +00002254 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002255 __ bind(&miss);
2256 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2257 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2258
2259 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002260 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002261}
2262
2263
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002264// Specialized stub for constructing objects from functions which only have only
2265// simple assignments of the form this.x = ...; in their body.
2266Object* ConstructStubCompiler::CompileConstructStub(
2267 SharedFunctionInfo* shared) {
2268 // ----------- S t a t e -------------
2269 // -- eax : argc
2270 // -- edi : constructor
2271 // -- esp[0] : return address
2272 // -- esp[4] : last argument
2273 // -----------------------------------
2274 Label generic_stub_call;
2275#ifdef ENABLE_DEBUGGER_SUPPORT
2276 // Check to see whether there are any break points in the function code. If
2277 // there are jump to the generic constructor stub which calls the actual
2278 // code for the function thereby hitting the break points.
2279 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2280 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2281 __ cmp(ebx, Factory::undefined_value());
2282 __ j(not_equal, &generic_stub_call, not_taken);
2283#endif
2284
2285 // Load the initial map and verify that it is in fact a map.
2286 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2287 // Will both indicate a NULL and a Smi.
2288 __ test(ebx, Immediate(kSmiTagMask));
2289 __ j(zero, &generic_stub_call);
2290 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2291 __ j(not_equal, &generic_stub_call);
2292
2293#ifdef DEBUG
2294 // Cannot construct functions this way.
2295 // edi: constructor
2296 // ebx: initial map
2297 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2298 __ Assert(not_equal, "Function constructed by construct stub.");
2299#endif
2300
2301 // Now allocate the JSObject on the heap by moving the new space allocation
2302 // top forward.
2303 // edi: constructor
2304 // ebx: initial map
2305 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2306 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002307 __ AllocateInNewSpace(ecx,
2308 edx,
2309 ecx,
2310 no_reg,
2311 &generic_stub_call,
2312 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002313
2314 // Allocated the JSObject, now initialize the fields and add the heap tag.
2315 // ebx: initial map
2316 // edx: JSObject (untagged)
2317 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2318 __ mov(ebx, Factory::empty_fixed_array());
2319 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2320 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2321
2322 // Push the allocated object to the stack. This is the object that will be
2323 // returned (after it is tagged).
2324 __ push(edx);
2325
2326 // eax: argc
2327 // edx: JSObject (untagged)
2328 // Load the address of the first in-object property into edx.
2329 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2330 // Calculate the location of the first argument. The stack contains the
2331 // allocated object and the return address on top of the argc arguments.
2332 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2333
2334 // Use edi for holding undefined which is used in several places below.
2335 __ mov(edi, Factory::undefined_value());
2336
2337 // eax: argc
2338 // ecx: first argument
2339 // edx: first in-object property of the JSObject
2340 // edi: undefined
2341 // Fill the initialized properties with a constant value or a passed argument
2342 // depending on the this.x = ...; assignment in the function.
2343 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2344 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002345 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002346 // If argument is not passed the property is set to undefined,
2347 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002348 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002349 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002350 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002351 if (CpuFeatures::IsSupported(CMOV)) {
2352 CpuFeatures::Scope use_cmov(CMOV);
2353 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2354 } else {
2355 Label not_passed;
2356 __ j(below_equal, &not_passed);
2357 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2358 __ bind(&not_passed);
2359 }
2360 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002361 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002362 } else {
2363 // Set the property to the constant value.
2364 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2365 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2366 }
2367 }
2368
2369 // Fill the unused in-object property fields with undefined.
2370 for (int i = shared->this_property_assignments_count();
2371 i < shared->CalculateInObjectProperties();
2372 i++) {
2373 __ mov(Operand(edx, i * kPointerSize), edi);
2374 }
2375
2376 // Move argc to ebx and retrieve and tag the JSObject to return.
2377 __ mov(ebx, eax);
2378 __ pop(eax);
2379 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2380
2381 // Remove caller arguments and receiver from the stack and return.
2382 __ pop(ecx);
2383 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2384 __ push(ecx);
2385 __ IncrementCounter(&Counters::constructed_objects, 1);
2386 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2387 __ ret(0);
2388
2389 // Jump to the generic stub in case the specialized code cannot handle the
2390 // construction.
2391 __ bind(&generic_stub_call);
2392 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2393 Handle<Code> generic_construct_stub(code);
2394 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2395
2396 // Return the generated code.
2397 return GetCode();
2398}
2399
2400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002401#undef __
2402
2403} } // namespace v8::internal