blob: 189c0e4d16c668e70e8094ba745c62e3be78a2a8 [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);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001267 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1268 __ jmp(ic, RelocInfo::CODE_TARGET);
1269
1270 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001271 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001272}
1273
1274
1275Object* CallStubCompiler::CompileArrayPopCall(Object* object,
1276 JSObject* holder,
1277 JSFunction* function,
1278 String* name,
1279 CheckType check) {
1280 // ----------- S t a t e -------------
1281 // -- ecx : name
1282 // -- esp[0] : return address
1283 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1284 // -- ...
1285 // -- esp[(argc + 1) * 4] : receiver
1286 // -----------------------------------
1287 ASSERT(check == RECEIVER_MAP_CHECK);
1288
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001289 // If object is not an array, bail out to regular call.
1290 if (!object->IsJSArray()) {
1291 return Heap::undefined_value();
1292 }
1293
ager@chromium.orgac091b72010-05-05 07:34:42 +00001294 Label miss, return_undefined, call_builtin;
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001295
1296 // Get the receiver from the stack.
1297 const int argc = arguments().immediate();
1298 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1299
1300 // Check that the receiver isn't a smi.
1301 __ test(edx, Immediate(kSmiTagMask));
1302 __ j(zero, &miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001303 CheckPrototypes(JSObject::cast(object), edx,
1304 holder, ebx,
1305 eax, name, &miss);
1306
1307 // Get the elements array of the object.
1308 __ mov(ebx, FieldOperand(edx, JSArray::kElementsOffset));
1309
1310 // Check that the elements are in fast mode (not dictionary).
1311 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1312 Immediate(Factory::fixed_array_map()));
1313 __ j(not_equal, &miss);
1314
1315 // Get the array's length into ecx and calculate new length.
1316 __ mov(ecx, FieldOperand(edx, JSArray::kLengthOffset));
1317 __ sub(Operand(ecx), Immediate(Smi::FromInt(1)));
ager@chromium.orgac091b72010-05-05 07:34:42 +00001318 __ j(negative, &return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001319
1320 // Get the last element.
1321 STATIC_ASSERT(kSmiTagSize == 1);
1322 STATIC_ASSERT(kSmiTag == 0);
1323 __ mov(eax, FieldOperand(ebx,
1324 ecx, times_half_pointer_size,
1325 FixedArray::kHeaderSize));
1326 __ cmp(Operand(eax), Immediate(Factory::the_hole_value()));
1327 __ j(equal, &call_builtin);
1328
1329 // Set the array's length.
1330 __ mov(FieldOperand(edx, JSArray::kLengthOffset), ecx);
1331
1332 // Fill with the hole.
1333 __ mov(FieldOperand(ebx,
1334 ecx, times_half_pointer_size,
1335 FixedArray::kHeaderSize),
1336 Immediate(Factory::the_hole_value()));
1337 __ ret((argc + 1) * kPointerSize);
1338
ager@chromium.orgac091b72010-05-05 07:34:42 +00001339 __ bind(&return_undefined);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001340 __ mov(eax, Immediate(Factory::undefined_value()));
1341 __ ret((argc + 1) * kPointerSize);
1342
1343 __ bind(&call_builtin);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001344 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop),
1345 argc + 1,
1346 1);
1347
1348 __ bind(&miss);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001349 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1350 __ jmp(ic, RelocInfo::CODE_TARGET);
1351
1352 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001353 return GetCode(function);
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001354}
1355
1356
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357Object* CallStubCompiler::CompileCallConstant(Object* object,
1358 JSObject* holder,
1359 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001360 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001361 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001363 // -- ecx : name
1364 // -- esp[0] : return address
1365 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1366 // -- ...
1367 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001368 // -----------------------------------
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001369
1370 SharedFunctionInfo* function_info = function->shared();
1371 if (function_info->HasCustomCallGenerator()) {
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001372 const int id = function_info->custom_call_generator_id();
1373 Object* result =
1374 CompileCustomCall(id, object, holder, function, name, check);
lrn@chromium.orgc34f5802010-04-28 12:53:43 +00001375 // undefined means bail out to regular compiler.
1376 if (!result->IsUndefined()) {
1377 return result;
1378 }
vegorov@chromium.orgf8372902010-03-15 10:26:20 +00001379 }
1380
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001381 Label miss_in_smi_check;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382
1383 // Get the receiver from the stack.
1384 const int argc = arguments().immediate();
1385 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
1386
1387 // Check that the receiver isn't a smi.
1388 if (check != NUMBER_CHECK) {
1389 __ test(edx, Immediate(kSmiTagMask));
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001390 __ j(zero, &miss_in_smi_check, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001391 }
1392
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001393 // Make sure that it's okay not to patch the on stack receiver
1394 // unless we're doing a receiver map check.
1395 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
1396
ager@chromium.org5c838252010-02-19 08:53:10 +00001397 CallOptimization optimization(function);
1398 int depth = kInvalidProtoDepth;
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001399 Label miss;
ager@chromium.org5c838252010-02-19 08:53:10 +00001400
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001401 switch (check) {
1402 case RECEIVER_MAP_CHECK:
ager@chromium.org5c838252010-02-19 08:53:10 +00001403 __ IncrementCounter(&Counters::call_const, 1);
1404
1405 if (optimization.is_simple_api_call() && !object->IsGlobalObject()) {
1406 depth = optimization.GetPrototypeDepthOfExpectedType(
1407 JSObject::cast(object), holder);
1408 }
1409
1410 if (depth != kInvalidProtoDepth) {
1411 __ IncrementCounter(&Counters::call_const_fast_api, 1);
1412 ReserveSpaceForFastApiCall(masm(), eax);
1413 }
1414
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001416 CheckPrototypes(JSObject::cast(object), edx, holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001417 ebx, eax, name, depth, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001418
1419 // Patch the receiver on the stack with the global proxy if
1420 // necessary.
1421 if (object->IsGlobalObject()) {
ager@chromium.org5c838252010-02-19 08:53:10 +00001422 ASSERT(depth == kInvalidProtoDepth);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001423 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1424 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1425 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426 break;
1427
1428 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001429 if (!function->IsBuiltin()) {
1430 // Calling non-builtins with a value as receiver requires boxing.
1431 __ jmp(&miss);
1432 } else {
1433 // Check that the object is a string or a symbol.
1434 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
1435 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
1436 __ cmp(eax, FIRST_NONSTRING_TYPE);
1437 __ j(above_equal, &miss, not_taken);
1438 // Check that the maps starting from the prototype haven't changed.
1439 GenerateLoadGlobalFunctionPrototype(masm(),
1440 Context::STRING_FUNCTION_INDEX,
1441 eax);
1442 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1443 ebx, edx, name, &miss);
1444 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445 break;
1446
1447 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001448 if (!function->IsBuiltin()) {
1449 // Calling non-builtins with a value as receiver requires boxing.
1450 __ jmp(&miss);
1451 } else {
1452 Label fast;
1453 // Check that the object is a smi or a heap number.
1454 __ test(edx, Immediate(kSmiTagMask));
1455 __ j(zero, &fast, taken);
1456 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1457 __ j(not_equal, &miss, not_taken);
1458 __ bind(&fast);
1459 // Check that the maps starting from the prototype haven't changed.
1460 GenerateLoadGlobalFunctionPrototype(masm(),
1461 Context::NUMBER_FUNCTION_INDEX,
1462 eax);
1463 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1464 ebx, edx, name, &miss);
1465 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001466 break;
1467 }
1468
1469 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001470 if (!function->IsBuiltin()) {
1471 // Calling non-builtins with a value as receiver requires boxing.
1472 __ jmp(&miss);
1473 } else {
1474 Label fast;
1475 // Check that the object is a boolean.
1476 __ cmp(edx, Factory::true_value());
1477 __ j(equal, &fast, taken);
1478 __ cmp(edx, Factory::false_value());
1479 __ j(not_equal, &miss, not_taken);
1480 __ bind(&fast);
1481 // Check that the maps starting from the prototype haven't changed.
1482 GenerateLoadGlobalFunctionPrototype(masm(),
1483 Context::BOOLEAN_FUNCTION_INDEX,
1484 eax);
1485 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1486 ebx, edx, name, &miss);
1487 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001488 break;
1489 }
1490
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001491 default:
1492 UNREACHABLE();
1493 }
1494
ager@chromium.org5c838252010-02-19 08:53:10 +00001495 if (depth != kInvalidProtoDepth) {
1496 GenerateFastApiCall(masm(), optimization, argc);
1497 } else {
1498 __ InvokeFunction(function, arguments(), JUMP_FUNCTION);
1499 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001500
1501 // Handle call cache miss.
1502 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +00001503 if (depth != kInvalidProtoDepth) {
1504 FreeSpaceForFastApiCall(masm(), eax);
1505 }
sgjesse@chromium.org534ae572010-02-24 22:09:39 +00001506 __ bind(&miss_in_smi_check);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001507 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001508 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001509
1510 // Return the generated code.
sgjesse@chromium.org720dc0b2010-05-10 09:25:39 +00001511 return GetCode(function);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001512}
1513
1514
ager@chromium.org5c838252010-02-19 08:53:10 +00001515Object* CallStubCompiler::CompileCallInterceptor(JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001516 JSObject* holder,
1517 String* name) {
1518 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001519 // -- ecx : name
1520 // -- esp[0] : return address
1521 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1522 // -- ...
1523 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001525 Label miss;
1526
1527 // Get the number of arguments.
1528 const int argc = arguments().immediate();
1529
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001530 LookupResult lookup;
1531 LookupPostInterceptor(holder, name, &lookup);
1532
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001533 // Get the receiver from the stack.
1534 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001535
ager@chromium.org5c838252010-02-19 08:53:10 +00001536 CallInterceptorCompiler compiler(this, arguments(), ecx);
1537 compiler.Compile(masm(),
1538 object,
1539 holder,
1540 name,
1541 &lookup,
1542 edx,
1543 ebx,
1544 edi,
1545 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001546
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001547 // Restore receiver.
1548 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001549
1550 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001551 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001552 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001553 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001554 __ j(not_equal, &miss, not_taken);
1555
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001556 // Patch the receiver on the stack with the global proxy if
1557 // necessary.
1558 if (object->IsGlobalObject()) {
1559 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1560 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1561 }
1562
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001563 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001564 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001565 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1566
1567 // Handle load cache miss.
1568 __ bind(&miss);
1569 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001570 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001571
1572 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001573 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574}
1575
1576
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001577Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1578 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001579 JSGlobalPropertyCell* cell,
1580 JSFunction* function,
1581 String* name) {
1582 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001583 // -- ecx : name
1584 // -- esp[0] : return address
1585 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1586 // -- ...
1587 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001588 // -----------------------------------
1589 Label miss;
1590
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001591 // Get the number of arguments.
1592 const int argc = arguments().immediate();
1593
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001594 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001595 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001596
1597 // If the object is the holder then we know that it's a global
1598 // object which can only happen for contextual calls. In this case,
1599 // the receiver cannot be a smi.
1600 if (object != holder) {
1601 __ test(edx, Immediate(kSmiTagMask));
1602 __ j(zero, &miss, not_taken);
1603 }
1604
1605 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001606 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001607
1608 // Get the value from the cell.
1609 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1610 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1611
1612 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001613 if (Heap::InNewSpace(function)) {
1614 // We can't embed a pointer to a function in new space so we have
1615 // to verify that the shared function info is unchanged. This has
1616 // the nice side effect that multiple closures based on the same
1617 // function can all use this call IC. Before we load through the
1618 // function, we have to verify that it still is a function.
1619 __ test(edi, Immediate(kSmiTagMask));
1620 __ j(zero, &miss, not_taken);
1621 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1622 __ j(not_equal, &miss, not_taken);
1623
1624 // Check the shared function info. Make sure it hasn't changed.
1625 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1626 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1627 __ j(not_equal, &miss, not_taken);
1628 } else {
1629 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1630 __ j(not_equal, &miss, not_taken);
1631 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001632
1633 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001634 if (object->IsGlobalObject()) {
1635 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1636 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1637 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001638
1639 // Setup the context (function already in edi).
1640 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1641
1642 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001643 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001644 ASSERT(function->is_compiled());
1645 Handle<Code> code(function->code());
1646 ParameterCount expected(function->shared()->formal_parameter_count());
1647 __ InvokeCode(code, expected, arguments(),
1648 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1649
1650 // Handle call cache miss.
1651 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001652 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1653 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1654 __ jmp(ic, RelocInfo::CODE_TARGET);
1655
1656 // Return the generated code.
1657 return GetCode(NORMAL, name);
1658}
1659
1660
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1662 int index,
1663 Map* transition,
1664 String* name) {
1665 // ----------- S t a t e -------------
1666 // -- eax : value
1667 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001668 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001669 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001670 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 Label miss;
1672
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001674 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001675 object,
1676 index,
1677 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001678 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001679 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001680
1681 // Handle store cache miss.
1682 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001683 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001684 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001685 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001686
1687 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001688 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001689}
1690
1691
1692Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1693 AccessorInfo* callback,
1694 String* name) {
1695 // ----------- S t a t e -------------
1696 // -- eax : value
1697 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001698 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001699 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001700 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 Label miss;
1702
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001703 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001704 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001705 __ j(zero, &miss, not_taken);
1706
1707 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001708 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001709 Immediate(Handle<Map>(object->map())));
1710 __ j(not_equal, &miss, not_taken);
1711
1712 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001713 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001714 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001715 }
1716
1717 // Stub never generated for non-global objects that require access
1718 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001719 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001720
1721 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001722 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1724 __ push(ecx); // name
1725 __ push(eax); // value
1726 __ push(ebx); // restore return address
1727
mads.s.ager31e71382008-08-13 09:32:07 +00001728 // Do tail-call to the runtime system.
1729 ExternalReference store_callback_property =
1730 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001731 __ TailCallExternalReference(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001732
1733 // Handle store cache miss.
1734 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001735 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001736 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001737
1738 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001739 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740}
1741
1742
1743Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1744 String* name) {
1745 // ----------- S t a t e -------------
1746 // -- eax : value
1747 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001748 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001749 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001750 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001751 Label miss;
1752
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001753 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001754 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001755 __ j(zero, &miss, not_taken);
1756
1757 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001758 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001759 Immediate(Handle<Map>(receiver->map())));
1760 __ j(not_equal, &miss, not_taken);
1761
1762 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001763 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001764 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001765 }
1766
1767 // Stub never generated for non-global objects that require access
1768 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001769 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770
1771 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001772 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001773 __ push(ecx); // name
1774 __ push(eax); // value
1775 __ push(ebx); // restore return address
1776
mads.s.ager31e71382008-08-13 09:32:07 +00001777 // Do tail-call to the runtime system.
1778 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001779 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001780 __ TailCallExternalReference(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001781
1782 // Handle store cache miss.
1783 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001784 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001785 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001786
1787 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001788 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001789}
1790
1791
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001792Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1793 JSGlobalPropertyCell* cell,
1794 String* name) {
1795 // ----------- S t a t e -------------
1796 // -- eax : value
1797 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001798 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001799 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001800 // -----------------------------------
1801 Label miss;
1802
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001803 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001804 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001805 Immediate(Handle<Map>(object->map())));
1806 __ j(not_equal, &miss, not_taken);
1807
1808 // Store the value in the cell.
1809 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1810 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1811
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001812 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001813 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001814 __ ret(0);
1815
1816 // Handle store cache miss.
1817 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001818 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1819 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1820 __ jmp(ic, RelocInfo::CODE_TARGET);
1821
1822 // Return the generated code.
1823 return GetCode(NORMAL, name);
1824}
1825
1826
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001827Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1828 int index,
1829 Map* transition,
1830 String* name) {
1831 // ----------- S t a t e -------------
1832 // -- eax : value
ager@chromium.orgce5e87b2010-03-10 10:24:18 +00001833 // -- ecx : key
1834 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001835 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001836 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001837 Label miss;
1838
1839 __ IncrementCounter(&Counters::keyed_store_field, 1);
1840
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001841 // Check that the name has not changed.
1842 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1843 __ j(not_equal, &miss, not_taken);
1844
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001845 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001846 GenerateStoreField(masm(),
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001847 object,
1848 index,
1849 transition,
ager@chromium.org5c838252010-02-19 08:53:10 +00001850 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001851 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001852
1853 // Handle store cache miss.
1854 __ bind(&miss);
1855 __ DecrementCounter(&Counters::keyed_store_field, 1);
1856 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001857 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001858
1859 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001860 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001861}
1862
1863
ricow@chromium.orgc9c80822010-04-21 08:22:37 +00001864Object* LoadStubCompiler::CompileLoadNonexistent(String* name,
1865 JSObject* object,
1866 JSObject* last) {
1867 // ----------- S t a t e -------------
1868 // -- eax : receiver
1869 // -- ecx : name
1870 // -- esp[0] : return address
1871 // -----------------------------------
1872 Label miss;
1873
1874 // Check that the receiver isn't a smi.
1875 __ test(eax, Immediate(kSmiTagMask));
1876 __ j(zero, &miss, not_taken);
1877
1878 // Check the maps of the full prototype chain. Also check that
1879 // global property cells up to (but not including) the last object
1880 // in the prototype chain are empty.
1881 CheckPrototypes(object, eax, last, ebx, edx, name, &miss);
1882
1883 // If the last object in the prototype chain is a global object,
1884 // check that the global property cell is empty.
1885 if (last->IsGlobalObject()) {
1886 Object* cell = GenerateCheckPropertyCell(masm(),
1887 GlobalObject::cast(last),
1888 name,
1889 edx,
1890 &miss);
1891 if (cell->IsFailure()) return cell;
1892 }
1893
1894 // Return undefined if maps of the full prototype chain are still the
1895 // same and no global property with this name contains a value.
1896 __ mov(eax, Factory::undefined_value());
1897 __ ret(0);
1898
1899 __ bind(&miss);
1900 GenerateLoadMiss(masm(), Code::LOAD_IC);
1901
1902 // Return the generated code.
1903 return GetCode(NONEXISTENT, Heap::empty_string());
1904}
1905
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001906
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001907Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1908 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001909 int index,
1910 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001911 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001912 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001913 // -- ecx : name
1914 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001915 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001916 Label miss;
1917
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001918 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001919 __ bind(&miss);
1920 GenerateLoadMiss(masm(), Code::LOAD_IC);
1921
1922 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001923 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001924}
1925
1926
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001927Object* LoadStubCompiler::CompileLoadCallback(String* name,
1928 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001929 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001930 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001931 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001932 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001933 // -- ecx : name
1934 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001935 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001936 Label miss;
1937
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001938 Failure* failure = Failure::InternalError();
1939 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1940 callback, name, &miss, &failure);
1941 if (!success) return failure;
1942
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001943 __ bind(&miss);
1944 GenerateLoadMiss(masm(), Code::LOAD_IC);
1945
1946 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001947 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001948}
1949
1950
1951Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1952 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001953 Object* value,
1954 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001955 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001956 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001957 // -- ecx : name
1958 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001959 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001960 Label miss;
1961
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001962 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001963 __ bind(&miss);
1964 GenerateLoadMiss(masm(), Code::LOAD_IC);
1965
1966 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001967 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001968}
1969
1970
1971Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1972 JSObject* holder,
1973 String* name) {
1974 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00001975 // -- eax : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001976 // -- ecx : name
1977 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001978 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001979 Label miss;
1980
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001981 LookupResult lookup;
1982 LookupPostInterceptor(holder, name, &lookup);
1983
ager@chromium.orge2902be2009-06-08 12:21:35 +00001984 // TODO(368): Compile in the whole chain: all the interceptors in
1985 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001986 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001987 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001988 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001989 eax,
1990 ecx,
1991 edx,
1992 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001993 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001994 &miss);
1995
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001996 __ bind(&miss);
1997 GenerateLoadMiss(masm(), Code::LOAD_IC);
1998
1999 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002000 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002001}
2002
2003
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002004Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
2005 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002006 JSGlobalPropertyCell* cell,
2007 String* name,
2008 bool is_dont_delete) {
2009 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002010 // -- eax : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002011 // -- ecx : name
2012 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002013 // -----------------------------------
2014 Label miss;
2015
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002016 // If the object is the holder then we know that it's a global
2017 // object which can only happen for contextual loads. In this case,
2018 // the receiver cannot be a smi.
2019 if (object != holder) {
2020 __ test(eax, Immediate(kSmiTagMask));
2021 __ j(zero, &miss, not_taken);
2022 }
2023
2024 // Check that the maps haven't changed.
2025 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002026
2027 // Get the value from the cell.
ager@chromium.org5c838252010-02-19 08:53:10 +00002028 __ mov(ebx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
2029 __ mov(ebx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002030
2031 // Check for deleted property if property can actually be deleted.
2032 if (!is_dont_delete) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002033 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002034 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002035 } else if (FLAG_debug_code) {
ager@chromium.org5c838252010-02-19 08:53:10 +00002036 __ cmp(ebx, Factory::the_hole_value());
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00002037 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002038 }
2039
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002040 __ IncrementCounter(&Counters::named_load_global_inline, 1);
ager@chromium.org5c838252010-02-19 08:53:10 +00002041 __ mov(eax, ebx);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002042 __ ret(0);
2043
2044 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00002045 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
2046 GenerateLoadMiss(masm(), Code::LOAD_IC);
2047
2048 // Return the generated code.
2049 return GetCode(NORMAL, name);
2050}
2051
2052
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002053Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
2054 JSObject* receiver,
2055 JSObject* holder,
2056 int index) {
2057 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002058 // -- eax : key
2059 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002060 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002061 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002062 Label miss;
2063
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002064 __ IncrementCounter(&Counters::keyed_load_field, 1);
2065
2066 // Check that the name has not changed.
2067 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2068 __ j(not_equal, &miss, not_taken);
2069
ager@chromium.org5c838252010-02-19 08:53:10 +00002070 GenerateLoadField(receiver, holder, edx, ebx, ecx, index, name, &miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002071
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002072 __ bind(&miss);
2073 __ DecrementCounter(&Counters::keyed_load_field, 1);
2074 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2075
2076 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002077 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002078}
2079
2080
2081Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
2082 JSObject* receiver,
2083 JSObject* holder,
2084 AccessorInfo* callback) {
2085 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002086 // -- eax : key
2087 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002088 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002089 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002090 Label miss;
2091
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002092 __ IncrementCounter(&Counters::keyed_load_callback, 1);
2093
2094 // Check that the name has not changed.
2095 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2096 __ j(not_equal, &miss, not_taken);
2097
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002098 Failure* failure = Failure::InternalError();
ager@chromium.org5c838252010-02-19 08:53:10 +00002099 bool success = GenerateLoadCallback(receiver, holder, edx, eax, ebx, ecx,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00002100 callback, name, &miss, &failure);
2101 if (!success) return failure;
2102
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002103 __ bind(&miss);
2104 __ DecrementCounter(&Counters::keyed_load_callback, 1);
2105 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2106
2107 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002108 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002109}
2110
2111
2112Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
2113 JSObject* receiver,
2114 JSObject* holder,
2115 Object* value) {
2116 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002117 // -- eax : key
2118 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002119 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002120 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002121 Label miss;
2122
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002123 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
2124
2125 // Check that the name has not changed.
2126 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2127 __ j(not_equal, &miss, not_taken);
2128
ager@chromium.org5c838252010-02-19 08:53:10 +00002129 GenerateLoadConstant(receiver, holder, edx, ebx, ecx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002130 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002131 __ bind(&miss);
2132 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
2133 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2134
2135 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002136 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002137}
2138
2139
2140Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
2141 JSObject* holder,
2142 String* name) {
2143 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002144 // -- eax : key
2145 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002146 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002147 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002148 Label miss;
2149
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002150 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
2151
2152 // Check that the name has not changed.
2153 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2154 __ j(not_equal, &miss, not_taken);
2155
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002156 LookupResult lookup;
2157 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002158 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002159 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00002160 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002161 edx,
ager@chromium.org5c838252010-02-19 08:53:10 +00002162 eax,
2163 ecx,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002164 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00002165 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00002166 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002167 __ bind(&miss);
2168 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
2169 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2170
2171 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002172 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002173}
2174
2175
2176
2177
2178Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
2179 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002180 // -- eax : key
2181 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002182 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002183 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002184 Label miss;
2185
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002186 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
2187
2188 // Check that the name has not changed.
2189 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2190 __ j(not_equal, &miss, not_taken);
2191
ager@chromium.org5c838252010-02-19 08:53:10 +00002192 GenerateLoadArrayLength(masm(), edx, ecx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002193 __ bind(&miss);
2194 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
2195 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2196
2197 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002198 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002199}
2200
2201
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00002202Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002203 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002204 // -- eax : key
2205 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002206 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002207 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002208 Label miss;
2209
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002210 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
2211
2212 // Check that the name has not changed.
2213 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2214 __ j(not_equal, &miss, not_taken);
2215
ager@chromium.org5c838252010-02-19 08:53:10 +00002216 GenerateLoadStringLength(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002217 __ bind(&miss);
2218 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
2219 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2220
2221 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002222 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002223}
2224
2225
2226Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
2227 // ----------- S t a t e -------------
ager@chromium.org5c838252010-02-19 08:53:10 +00002228 // -- eax : key
2229 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002230 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002231 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002232 Label miss;
2233
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002234 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
2235
2236 // Check that the name has not changed.
2237 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
2238 __ j(not_equal, &miss, not_taken);
2239
ager@chromium.org5c838252010-02-19 08:53:10 +00002240 GenerateLoadFunctionPrototype(masm(), edx, ecx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002241 __ bind(&miss);
2242 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
2243 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
2244
2245 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00002246 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002247}
2248
2249
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002250// Specialized stub for constructing objects from functions which only have only
2251// simple assignments of the form this.x = ...; in their body.
2252Object* ConstructStubCompiler::CompileConstructStub(
2253 SharedFunctionInfo* shared) {
2254 // ----------- S t a t e -------------
2255 // -- eax : argc
2256 // -- edi : constructor
2257 // -- esp[0] : return address
2258 // -- esp[4] : last argument
2259 // -----------------------------------
2260 Label generic_stub_call;
2261#ifdef ENABLE_DEBUGGER_SUPPORT
2262 // Check to see whether there are any break points in the function code. If
2263 // there are jump to the generic constructor stub which calls the actual
2264 // code for the function thereby hitting the break points.
2265 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2266 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
2267 __ cmp(ebx, Factory::undefined_value());
2268 __ j(not_equal, &generic_stub_call, not_taken);
2269#endif
2270
2271 // Load the initial map and verify that it is in fact a map.
2272 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
2273 // Will both indicate a NULL and a Smi.
2274 __ test(ebx, Immediate(kSmiTagMask));
2275 __ j(zero, &generic_stub_call);
2276 __ CmpObjectType(ebx, MAP_TYPE, ecx);
2277 __ j(not_equal, &generic_stub_call);
2278
2279#ifdef DEBUG
2280 // Cannot construct functions this way.
2281 // edi: constructor
2282 // ebx: initial map
2283 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
2284 __ Assert(not_equal, "Function constructed by construct stub.");
2285#endif
2286
2287 // Now allocate the JSObject on the heap by moving the new space allocation
2288 // top forward.
2289 // edi: constructor
2290 // ebx: initial map
2291 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
2292 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00002293 __ AllocateInNewSpace(ecx,
2294 edx,
2295 ecx,
2296 no_reg,
2297 &generic_stub_call,
2298 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002299
2300 // Allocated the JSObject, now initialize the fields and add the heap tag.
2301 // ebx: initial map
2302 // edx: JSObject (untagged)
2303 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
2304 __ mov(ebx, Factory::empty_fixed_array());
2305 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
2306 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
2307
2308 // Push the allocated object to the stack. This is the object that will be
2309 // returned (after it is tagged).
2310 __ push(edx);
2311
2312 // eax: argc
2313 // edx: JSObject (untagged)
2314 // Load the address of the first in-object property into edx.
2315 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
2316 // Calculate the location of the first argument. The stack contains the
2317 // allocated object and the return address on top of the argc arguments.
2318 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
2319
2320 // Use edi for holding undefined which is used in several places below.
2321 __ mov(edi, Factory::undefined_value());
2322
2323 // eax: argc
2324 // ecx: first argument
2325 // edx: first in-object property of the JSObject
2326 // edi: undefined
2327 // Fill the initialized properties with a constant value or a passed argument
2328 // depending on the this.x = ...; assignment in the function.
2329 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
2330 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002331 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002332 // If argument is not passed the property is set to undefined,
2333 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002334 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002335 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002336 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00002337 if (CpuFeatures::IsSupported(CMOV)) {
2338 CpuFeatures::Scope use_cmov(CMOV);
2339 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
2340 } else {
2341 Label not_passed;
2342 __ j(below_equal, &not_passed);
2343 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
2344 __ bind(&not_passed);
2345 }
2346 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002347 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00002348 } else {
2349 // Set the property to the constant value.
2350 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
2351 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
2352 }
2353 }
2354
2355 // Fill the unused in-object property fields with undefined.
2356 for (int i = shared->this_property_assignments_count();
2357 i < shared->CalculateInObjectProperties();
2358 i++) {
2359 __ mov(Operand(edx, i * kPointerSize), edi);
2360 }
2361
2362 // Move argc to ebx and retrieve and tag the JSObject to return.
2363 __ mov(ebx, eax);
2364 __ pop(eax);
2365 __ or_(Operand(eax), Immediate(kHeapObjectTag));
2366
2367 // Remove caller arguments and receiver from the stack and return.
2368 __ pop(ecx);
2369 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
2370 __ push(ecx);
2371 __ IncrementCounter(&Counters::constructed_objects, 1);
2372 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
2373 __ ret(0);
2374
2375 // Jump to the generic stub in case the specialized code cannot handle the
2376 // construction.
2377 __ bind(&generic_stub_call);
2378 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
2379 Handle<Code> generic_construct_stub(code);
2380 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
2381
2382 // Return the generated code.
2383 return GetCode();
2384}
2385
2386
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002387#undef __
2388
2389} } // namespace v8::internal