blob: 049c57e4f16a4b97d8d59db7cb030e82832ebd95 [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.
ager@chromium.org7c537e22008-10-16 08:43:32 +0000129 __ mov(scratch, FieldOperand(name, String::kLengthOffset));
130 __ 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.
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000138 __ mov(scratch, FieldOperand(name, String::kLengthOffset));
139 __ 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
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000155template <typename Pushable>
156static void PushInterceptorArguments(MacroAssembler* masm,
157 Register receiver,
158 Register holder,
159 Pushable name,
160 JSObject* holder_obj) {
161 __ push(receiver);
162 __ push(holder);
163 __ push(name);
164 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
165 __ mov(receiver, Immediate(Handle<Object>(interceptor)));
166 __ push(receiver);
167 __ push(FieldOperand(receiver, InterceptorInfo::kDataOffset));
168}
169
170
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000171void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
172 int index,
173 Register prototype) {
174 // Load the global or builtins object from the current context.
175 __ mov(prototype, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
176 // Load the global context from the global or builtins object.
177 __ mov(prototype,
178 FieldOperand(prototype, GlobalObject::kGlobalContextOffset));
179 // Load the function from the global context.
180 __ mov(prototype, Operand(prototype, Context::SlotOffset(index)));
181 // Load the initial map. The global functions all have initial maps.
182 __ mov(prototype,
183 FieldOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
184 // Load the prototype from the initial map.
185 __ mov(prototype, FieldOperand(prototype, Map::kPrototypeOffset));
186}
187
188
189void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
190 Register receiver,
191 Register scratch,
192 Label* miss_label) {
193 // Check that the receiver isn't a smi.
194 __ test(receiver, Immediate(kSmiTagMask));
195 __ j(zero, miss_label, not_taken);
196
197 // Check that the object is a JS array.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000198 __ CmpObjectType(receiver, JS_ARRAY_TYPE, scratch);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 __ j(not_equal, miss_label, not_taken);
200
201 // Load length directly from the JS array.
202 __ mov(eax, FieldOperand(receiver, JSArray::kLengthOffset));
203 __ ret(0);
204}
205
206
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000207// Generate code to check if an object is a string. If the object is
208// a string, the map's instance type is left in the scratch register.
209static void GenerateStringCheck(MacroAssembler* masm,
210 Register receiver,
211 Register scratch,
212 Label* smi,
213 Label* non_string_object) {
214 // Check that the object isn't a smi.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000215 __ test(receiver, Immediate(kSmiTagMask));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000216 __ j(zero, smi, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000217
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000218 // Check that the object is a string.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000219 __ mov(scratch, FieldOperand(receiver, HeapObject::kMapOffset));
220 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000221 ASSERT(kNotStringTag != 0);
222 __ test(scratch, Immediate(kNotStringTag));
223 __ j(not_zero, non_string_object, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000224}
225
226
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000227void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
228 Register receiver,
229 Register scratch,
230 Label* miss) {
231 Label load_length, check_wrapper;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000232
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000233 // Check if the object is a string leaving the instance type in the
234 // scratch register.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000235 GenerateStringCheck(masm, receiver, scratch, miss, &check_wrapper);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000236
237 // Load length directly from the string.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000238 __ bind(&load_length);
239 __ and_(scratch, kStringSizeMask);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000240 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000241 // ecx is also the receiver.
242 __ lea(ecx, Operand(scratch, String::kLongLengthShift));
243 __ shr(eax); // ecx is implicit shift register.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000244 __ shl(eax, kSmiTagSize);
245 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000246
247 // Check if the object is a JSValue wrapper.
248 __ bind(&check_wrapper);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000249 __ cmp(scratch, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000250 __ j(not_equal, miss, not_taken);
251
252 // Check if the wrapped value is a string and load the length
253 // directly if it is.
254 __ mov(receiver, FieldOperand(receiver, JSValue::kValueOffset));
255 GenerateStringCheck(masm, receiver, scratch, miss, miss);
256 __ jmp(&load_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000257}
258
259
260void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
261 Register receiver,
262 Register scratch1,
263 Register scratch2,
264 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000265 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000266 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000267 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000268}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269
ager@chromium.org7c537e22008-10-16 08:43:32 +0000270
271// Load a fast property out of a holder object (src). In-object properties
272// are loaded directly otherwise the property is loaded from the properties
273// fixed array.
274void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000275 Register dst, Register src,
276 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000277 // Adjust for the number of properties stored in the holder.
278 index -= holder->map()->inobject_properties();
279 if (index < 0) {
280 // Get the property straight out of the holder.
281 int offset = holder->map()->instance_size() + (index * kPointerSize);
282 __ mov(dst, FieldOperand(src, offset));
283 } else {
284 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000285 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000286 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
287 __ mov(dst, FieldOperand(dst, offset));
288 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289}
290
291
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000292template <class Pushable>
293static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
294 Register receiver,
295 Register holder,
296 Pushable name,
297 JSObject* holder_obj) {
298 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
299
300 ExternalReference ref =
301 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly));
302 __ mov(eax, Immediate(5));
303 __ mov(ebx, Immediate(ref));
304
305 CEntryStub stub;
306 __ CallStub(&stub);
307}
308
309
310template <class Compiler>
311static void CompileLoadInterceptor(Compiler* compiler,
312 StubCompiler* stub_compiler,
313 MacroAssembler* masm,
314 JSObject* object,
315 JSObject* holder,
316 String* name,
317 LookupResult* lookup,
318 Register receiver,
319 Register scratch1,
320 Register scratch2,
321 Label* miss) {
322 ASSERT(holder->HasNamedInterceptor());
323 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
324
325 // Check that the receiver isn't a smi.
326 __ test(receiver, Immediate(kSmiTagMask));
327 __ j(zero, miss, not_taken);
328
329 // Check that the maps haven't changed.
330 Register reg =
331 stub_compiler->CheckPrototypes(object, receiver, holder,
332 scratch1, scratch2, name, miss);
333
334 if (lookup->IsValid() && lookup->IsCacheable()) {
335 compiler->CompileCacheable(masm,
336 stub_compiler,
337 receiver,
338 reg,
339 scratch1,
340 scratch2,
341 holder,
342 lookup,
343 name,
344 miss);
345 } else {
346 compiler->CompileRegular(masm,
347 receiver,
348 reg,
349 scratch2,
350 holder,
351 miss);
352 }
353}
354
355
356static void LookupPostInterceptor(JSObject* holder,
357 String* name,
358 LookupResult* lookup) {
359 holder->LocalLookupRealNamedProperty(name, lookup);
360 if (lookup->IsNotFound()) {
361 Object* proto = holder->GetPrototype();
362 if (proto != Heap::null_value()) {
363 proto->Lookup(name, lookup);
364 }
365 }
366}
367
368
369class LoadInterceptorCompiler BASE_EMBEDDED {
370 public:
371 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
372
373 void CompileCacheable(MacroAssembler* masm,
374 StubCompiler* stub_compiler,
375 Register receiver,
376 Register holder,
377 Register scratch1,
378 Register scratch2,
379 JSObject* holder_obj,
380 LookupResult* lookup,
381 String* name,
382 Label* miss_label) {
383 AccessorInfo* callback = 0;
384 bool optimize = false;
385 // So far the most popular follow ups for interceptor loads are FIELD
386 // and CALLBACKS, so inline only them, other cases may be added
387 // later.
388 if (lookup->type() == FIELD) {
389 optimize = true;
390 } else if (lookup->type() == CALLBACKS) {
391 Object* callback_object = lookup->GetCallbackObject();
392 if (callback_object->IsAccessorInfo()) {
393 callback = AccessorInfo::cast(callback_object);
394 optimize = callback->getter() != NULL;
395 }
396 }
397
398 if (!optimize) {
399 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
400 return;
401 }
402
403 // Note: starting a frame here makes GC aware of pointers pushed below.
404 __ EnterInternalFrame();
405
406 if (lookup->type() == CALLBACKS) {
407 __ push(receiver);
408 }
409 __ push(holder);
410 __ push(name_);
411
412 CompileCallLoadPropertyWithInterceptor(masm,
413 receiver,
414 holder,
415 name_,
416 holder_obj);
417
418 Label interceptor_failed;
419 __ cmp(eax, Factory::no_interceptor_result_sentinel());
420 __ j(equal, &interceptor_failed);
421 __ LeaveInternalFrame();
422 __ ret(0);
423
424 __ bind(&interceptor_failed);
425 __ pop(name_);
426 __ pop(holder);
427 if (lookup->type() == CALLBACKS) {
428 __ pop(receiver);
429 }
430
431 __ LeaveInternalFrame();
432
433 if (lookup->type() == FIELD) {
434 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
435 lookup->holder(), scratch1,
436 scratch2,
437 name,
438 miss_label);
439 stub_compiler->GenerateFastPropertyLoad(masm, eax,
440 holder, lookup->holder(),
441 lookup->GetFieldIndex());
442 __ ret(0);
443 } else {
444 ASSERT(lookup->type() == CALLBACKS);
445 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
446 ASSERT(callback != NULL);
447 ASSERT(callback->getter() != NULL);
448
449 Label cleanup;
450 __ pop(scratch2);
451 __ push(receiver);
452 __ push(scratch2);
453
454 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
455 lookup->holder(), scratch1,
456 scratch2,
457 name,
458 &cleanup);
459
460 __ pop(scratch2); // save old return address
461 __ push(holder);
462 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
463 __ push(holder);
464 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
465 __ push(name_);
466 __ push(scratch2); // restore old return address
467
468 ExternalReference ref =
469 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
470 __ TailCallRuntime(ref, 5);
471
472 __ bind(&cleanup);
473 __ pop(scratch1);
474 __ pop(scratch2);
475 __ push(scratch1);
476 }
477 }
478
479
480 void CompileRegular(MacroAssembler* masm,
481 Register receiver,
482 Register holder,
483 Register scratch,
484 JSObject* holder_obj,
485 Label* miss_label) {
486 __ pop(scratch); // save old return address
487 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
488 __ push(scratch); // restore old return address
489
490 ExternalReference ref = ExternalReference(
491 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
492 __ TailCallRuntime(ref, 5);
493 }
494
495 private:
496 Register name_;
497};
498
499
500class CallInterceptorCompiler BASE_EMBEDDED {
501 public:
502 explicit CallInterceptorCompiler(const ParameterCount& arguments)
503 : arguments_(arguments), argc_(arguments.immediate()) {}
504
505 void CompileCacheable(MacroAssembler* masm,
506 StubCompiler* stub_compiler,
507 Register receiver,
508 Register holder,
509 Register scratch1,
510 Register scratch2,
511 JSObject* holder_obj,
512 LookupResult* lookup,
513 String* name,
514 Label* miss_label) {
515 JSFunction* function = 0;
516 bool optimize = false;
517 // So far the most popular case for failed interceptor is
518 // CONSTANT_FUNCTION sitting below.
519 if (lookup->type() == CONSTANT_FUNCTION) {
520 function = lookup->GetConstantFunction();
521 // JSArray holder is a special case for call constant function
522 // (see the corresponding code).
523 if (function->is_compiled() && !holder_obj->IsJSArray()) {
524 optimize = true;
525 }
526 }
527
528 if (!optimize) {
529 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
530 return;
531 }
532
533 __ EnterInternalFrame();
534 __ push(holder); // save the holder
535
536 CompileCallLoadPropertyWithInterceptor(
537 masm,
538 receiver,
539 holder,
540 // Under EnterInternalFrame this refers to name.
541 Operand(ebp, (argc_ + 3) * kPointerSize),
542 holder_obj);
543
544 __ pop(receiver); // restore holder
545 __ LeaveInternalFrame();
546
547 __ cmp(eax, Factory::no_interceptor_result_sentinel());
548 Label invoke;
549 __ j(not_equal, &invoke);
550
551 stub_compiler->CheckPrototypes(holder_obj, receiver,
552 lookup->holder(), scratch1,
553 scratch2,
554 name,
555 miss_label);
556 if (lookup->holder()->IsGlobalObject()) {
557 __ mov(edx, Operand(esp, (argc_ + 1) * kPointerSize));
558 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
559 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edx);
560 }
561
562 ASSERT(function->is_compiled());
563 // Get the function and setup the context.
564 __ mov(edi, Immediate(Handle<JSFunction>(function)));
565 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
566
567 // Jump to the cached code (tail call).
568 ASSERT(function->is_compiled());
569 Handle<Code> code(function->code());
570 ParameterCount expected(function->shared()->formal_parameter_count());
571 __ InvokeCode(code, expected, arguments_,
572 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
573
574 __ bind(&invoke);
575 }
576
577 void CompileRegular(MacroAssembler* masm,
578 Register receiver,
579 Register holder,
580 Register scratch,
581 JSObject* holder_obj,
582 Label* miss_label) {
583 __ EnterInternalFrame();
584
585 PushInterceptorArguments(masm,
586 receiver,
587 holder,
588 Operand(ebp, (argc_ + 3) * kPointerSize),
589 holder_obj);
590
591 ExternalReference ref = ExternalReference(
592 IC_Utility(IC::kLoadPropertyWithInterceptorForCall));
593 __ mov(eax, Immediate(5));
594 __ mov(ebx, Immediate(ref));
595
596 CEntryStub stub;
597 __ CallStub(&stub);
598
599 __ LeaveInternalFrame();
600 }
601
602 private:
603 const ParameterCount& arguments_;
604 int argc_;
605};
606
607
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
609 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
610 Code* code = NULL;
611 if (kind == Code::LOAD_IC) {
612 code = Builtins::builtin(Builtins::LoadIC_Miss);
613 } else {
614 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
615 }
616
617 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000618 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000619}
620
621
622void StubCompiler::GenerateStoreField(MacroAssembler* masm,
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000623 Builtins::Name storage_extend,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000624 JSObject* object,
625 int index,
626 Map* transition,
627 Register receiver_reg,
628 Register name_reg,
629 Register scratch,
630 Label* miss_label) {
631 // Check that the object isn't a smi.
632 __ test(receiver_reg, Immediate(kSmiTagMask));
633 __ j(zero, miss_label, not_taken);
634
635 // Check that the map of the object hasn't changed.
636 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
637 Immediate(Handle<Map>(object->map())));
638 __ j(not_equal, miss_label, not_taken);
639
640 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000641 if (object->IsJSGlobalProxy()) {
642 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000643 }
644
645 // Stub never generated for non-global objects that require access
646 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000647 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000648
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000649 // Perform map transition for the receiver if necessary.
650 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
651 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000652 // We jump to a runtime call that extends the properties array.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000653 __ mov(ecx, Immediate(Handle<Map>(transition)));
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000654 Handle<Code> ic(Builtins::builtin(storage_extend));
655 __ jmp(ic, RelocInfo::CODE_TARGET);
656 return;
657 }
658
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000659 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000660 // Update the map of the object; no write barrier updating is
661 // needed because the map is never in new space.
662 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
663 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000664 }
665
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000666 // Adjust for the number of properties stored in the object. Even in the
667 // face of a transition we can use the old map here because the size of the
668 // object and the number of in-object properties is not going to change.
669 index -= object->map()->inobject_properties();
670
ager@chromium.org7c537e22008-10-16 08:43:32 +0000671 if (index < 0) {
672 // Set the property straight into the object.
673 int offset = object->map()->instance_size() + (index * kPointerSize);
674 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000675
ager@chromium.org7c537e22008-10-16 08:43:32 +0000676 // Update the write barrier for the array address.
677 // Pass the value being stored in the now unused name_reg.
678 __ mov(name_reg, Operand(eax));
679 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
680 } else {
681 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000682 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000683 // Get the properties array (optimistically).
684 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000685 __ mov(FieldOperand(scratch, offset), eax);
686
687 // Update the write barrier for the array address.
688 // Pass the value being stored in the now unused name_reg.
689 __ mov(name_reg, Operand(eax));
690 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
691 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000692
693 // Return the value (register eax).
694 __ ret(0);
695}
696
697
698#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000699#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000700
701
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000702Register StubCompiler::CheckPrototypes(JSObject* object,
703 Register object_reg,
704 JSObject* holder,
705 Register holder_reg,
706 Register scratch,
707 String* name,
708 Label* miss) {
709 // Check that the maps haven't changed.
710 Register result =
711 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch, miss);
712
713 // If we've skipped any global objects, it's not enough to verify
714 // that their maps haven't changed.
715 while (object != holder) {
716 if (object->IsGlobalObject()) {
717 GlobalObject* global = GlobalObject::cast(object);
718 Object* probe = global->EnsurePropertyCell(name);
719 if (probe->IsFailure()) {
720 set_failure(Failure::cast(probe));
721 return result;
722 }
723 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
724 ASSERT(cell->value()->IsTheHole());
725 __ mov(scratch, Immediate(Handle<Object>(cell)));
726 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
727 Immediate(Factory::the_hole_value()));
728 __ j(not_equal, miss, not_taken);
729 }
730 object = JSObject::cast(object->GetPrototype());
731 }
732
733 // Return the register containin the holder.
734 return result;
735}
736
737
738void StubCompiler::GenerateLoadField(JSObject* object,
739 JSObject* holder,
740 Register receiver,
741 Register scratch1,
742 Register scratch2,
743 int index,
744 String* name,
745 Label* miss) {
746 // Check that the receiver isn't a smi.
747 __ test(receiver, Immediate(kSmiTagMask));
748 __ j(zero, miss, not_taken);
749
750 // Check the prototype chain.
751 Register reg =
752 CheckPrototypes(object, receiver, holder,
753 scratch1, scratch2, name, miss);
754
755 // Get the value from the properties.
756 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
757 __ ret(0);
758}
759
760
761void StubCompiler::GenerateLoadCallback(JSObject* object,
762 JSObject* holder,
763 Register receiver,
764 Register name_reg,
765 Register scratch1,
766 Register scratch2,
767 AccessorInfo* callback,
768 String* name,
769 Label* miss) {
770 // Check that the receiver isn't a smi.
771 __ test(receiver, Immediate(kSmiTagMask));
772 __ j(zero, miss, not_taken);
773
774 // Check that the maps haven't changed.
775 Register reg =
776 CheckPrototypes(object, receiver, holder,
777 scratch1, scratch2, name, miss);
778
779 // Push the arguments on the JS stack of the caller.
780 __ pop(scratch2); // remove return address
781 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000782 __ push(reg); // holder
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000783 __ mov(reg, Immediate(Handle<AccessorInfo>(callback))); // callback data
784 __ push(reg);
785 __ push(FieldOperand(reg, AccessorInfo::kDataOffset));
786 __ push(name_reg); // name
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000787 __ push(scratch2); // restore return address
788
789 // Do tail-call to the runtime system.
790 ExternalReference load_callback_property =
791 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000792 __ TailCallRuntime(load_callback_property, 5);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000793}
794
795
796void StubCompiler::GenerateLoadConstant(JSObject* object,
797 JSObject* holder,
798 Register receiver,
799 Register scratch1,
800 Register scratch2,
801 Object* value,
802 String* name,
803 Label* miss) {
804 // Check that the receiver isn't a smi.
805 __ test(receiver, Immediate(kSmiTagMask));
806 __ j(zero, miss, not_taken);
807
808 // Check that the maps haven't changed.
809 Register reg =
810 CheckPrototypes(object, receiver, holder,
811 scratch1, scratch2, name, miss);
812
813 // Return the constant value.
814 __ mov(eax, Handle<Object>(value));
815 __ ret(0);
816}
817
818
819void StubCompiler::GenerateLoadInterceptor(JSObject* object,
820 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000821 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000822 Register receiver,
823 Register name_reg,
824 Register scratch1,
825 Register scratch2,
826 String* name,
827 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000828 LoadInterceptorCompiler compiler(name_reg);
829 CompileLoadInterceptor(&compiler,
830 this,
831 masm(),
832 object,
833 holder,
834 name,
835 lookup,
836 receiver,
837 scratch1,
838 scratch2,
839 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000840}
841
842
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000843// TODO(1241006): Avoid having lazy compile stubs specialized by the
844// number of arguments. It is not needed anymore.
845Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000846 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000847 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000848
849 // Push a copy of the function onto the stack.
850 __ push(edi);
851
852 __ push(edi); // function is also the parameter to the runtime call
853 __ CallRuntime(Runtime::kLazyCompile, 1);
854 __ pop(edi);
855
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000856 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +0000857 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000858
859 // Do a tail-call of the compiled function.
860 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
861 __ jmp(Operand(ecx));
862
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000863 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000864}
865
866
867Object* CallStubCompiler::CompileCallField(Object* object,
868 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000869 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000870 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871 // ----------- S t a t e -------------
872 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000873 Label miss;
874
875 // Get the receiver from the stack.
876 const int argc = arguments().immediate();
877 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
878
879 // Check that the receiver isn't a smi.
880 __ test(edx, Immediate(kSmiTagMask));
881 __ j(zero, &miss, not_taken);
882
883 // Do the right check and compute the holder register.
884 Register reg =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000885 CheckPrototypes(JSObject::cast(object), edx, holder,
886 ebx, ecx, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887
ager@chromium.org7c537e22008-10-16 08:43:32 +0000888 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000889
890 // Check that the function really is a function.
891 __ test(edi, Immediate(kSmiTagMask));
892 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000893 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 __ j(not_equal, &miss, not_taken);
895
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000896 // Patch the receiver on the stack with the global proxy if
897 // necessary.
898 if (object->IsGlobalObject()) {
899 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
900 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
901 }
902
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000903 // Invoke the function.
904 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
905
906 // Handle call cache miss.
907 __ bind(&miss);
908 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +0000909 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000910
911 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000912 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000913}
914
915
916Object* CallStubCompiler::CompileCallConstant(Object* object,
917 JSObject* holder,
918 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000919 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000920 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000921 // ----------- S t a t e -------------
922 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000923 Label miss;
924
925 // Get the receiver from the stack.
926 const int argc = arguments().immediate();
927 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
928
929 // Check that the receiver isn't a smi.
930 if (check != NUMBER_CHECK) {
931 __ test(edx, Immediate(kSmiTagMask));
932 __ j(zero, &miss, not_taken);
933 }
934
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000935 // Make sure that it's okay not to patch the on stack receiver
936 // unless we're doing a receiver map check.
937 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
938
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000939 switch (check) {
940 case RECEIVER_MAP_CHECK:
941 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000942 CheckPrototypes(JSObject::cast(object), edx, holder,
943 ebx, ecx, name, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000944
945 // Patch the receiver on the stack with the global proxy if
946 // necessary.
947 if (object->IsGlobalObject()) {
948 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
949 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
950 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000951 break;
952
953 case STRING_CHECK:
954 // Check that the object is a two-byte string or a symbol.
955 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
956 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
957 __ cmp(ecx, FIRST_NONSTRING_TYPE);
958 __ j(above_equal, &miss, not_taken);
959 // Check that the maps starting from the prototype haven't changed.
960 GenerateLoadGlobalFunctionPrototype(masm(),
961 Context::STRING_FUNCTION_INDEX,
962 ecx);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000963 CheckPrototypes(JSObject::cast(object->GetPrototype()), ecx, holder,
964 ebx, edx, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000965 break;
966
967 case NUMBER_CHECK: {
968 Label fast;
969 // Check that the object is a smi or a heap number.
970 __ test(edx, Immediate(kSmiTagMask));
971 __ j(zero, &fast, taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000972 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000973 __ j(not_equal, &miss, not_taken);
974 __ bind(&fast);
975 // Check that the maps starting from the prototype haven't changed.
976 GenerateLoadGlobalFunctionPrototype(masm(),
977 Context::NUMBER_FUNCTION_INDEX,
978 ecx);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000979 CheckPrototypes(JSObject::cast(object->GetPrototype()), ecx, holder,
980 ebx, edx, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000981 break;
982 }
983
984 case BOOLEAN_CHECK: {
985 Label fast;
986 // Check that the object is a boolean.
987 __ cmp(edx, Factory::true_value());
988 __ j(equal, &fast, taken);
989 __ cmp(edx, Factory::false_value());
990 __ j(not_equal, &miss, not_taken);
991 __ bind(&fast);
992 // Check that the maps starting from the prototype haven't changed.
993 GenerateLoadGlobalFunctionPrototype(masm(),
994 Context::BOOLEAN_FUNCTION_INDEX,
995 ecx);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000996 CheckPrototypes(JSObject::cast(object->GetPrototype()), ecx, holder,
997 ebx, edx, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000998 break;
999 }
1000
1001 case JSARRAY_HAS_FAST_ELEMENTS_CHECK:
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001002 CheckPrototypes(JSObject::cast(object), edx, holder,
1003 ebx, ecx, name, &miss);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001004 // Make sure object->HasFastElements().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001005 // Get the elements array of the object.
1006 __ mov(ebx, FieldOperand(edx, JSObject::kElementsOffset));
1007 // Check that the object is in fast mode (not dictionary).
1008 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001009 Immediate(Factory::fixed_array_map()));
1010 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001011 break;
1012
1013 default:
1014 UNREACHABLE();
1015 }
1016
1017 // Get the function and setup the context.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001018 __ mov(edi, Immediate(Handle<JSFunction>(function)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001019 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1020
1021 // Jump to the cached code (tail call).
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001022 ASSERT(function->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001023 Handle<Code> code(function->code());
1024 ParameterCount expected(function->shared()->formal_parameter_count());
ager@chromium.org236ad962008-09-25 09:45:57 +00001025 __ InvokeCode(code, expected, arguments(),
1026 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001027
1028 // Handle call cache miss.
1029 __ bind(&miss);
1030 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001031 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001032
1033 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001034 String* function_name = NULL;
1035 if (function->shared()->name()->IsString()) {
1036 function_name = String::cast(function->shared()->name());
1037 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001038 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001039}
1040
1041
1042Object* CallStubCompiler::CompileCallInterceptor(Object* object,
1043 JSObject* holder,
1044 String* name) {
1045 // ----------- S t a t e -------------
1046 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001047 Label miss;
1048
1049 // Get the number of arguments.
1050 const int argc = arguments().immediate();
1051
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001052 LookupResult lookup;
1053 LookupPostInterceptor(holder, name, &lookup);
1054
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001055 // Get the receiver from the stack.
1056 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001057
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001058 CallInterceptorCompiler compiler(arguments());
1059 CompileLoadInterceptor(&compiler,
1060 this,
1061 masm(),
1062 JSObject::cast(object),
1063 holder,
1064 name,
1065 &lookup,
1066 edx,
1067 ebx,
1068 ecx,
1069 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001070
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001071 // Restore receiver.
1072 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001073
1074 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001075 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001076 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001077 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001078 __ j(not_equal, &miss, not_taken);
1079
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001080 // Patch the receiver on the stack with the global proxy if
1081 // necessary.
1082 if (object->IsGlobalObject()) {
1083 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1084 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1085 }
1086
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001088 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001089 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1090
1091 // Handle load cache miss.
1092 __ bind(&miss);
1093 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001094 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001095
1096 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001097 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001098}
1099
1100
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001101Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1102 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001103 JSGlobalPropertyCell* cell,
1104 JSFunction* function,
1105 String* name) {
1106 // ----------- S t a t e -------------
1107 // -----------------------------------
1108 Label miss;
1109
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001110 // Get the number of arguments.
1111 const int argc = arguments().immediate();
1112
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001113 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001114 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001115
1116 // If the object is the holder then we know that it's a global
1117 // object which can only happen for contextual calls. In this case,
1118 // the receiver cannot be a smi.
1119 if (object != holder) {
1120 __ test(edx, Immediate(kSmiTagMask));
1121 __ j(zero, &miss, not_taken);
1122 }
1123
1124 // Check that the maps haven't changed.
1125 CheckPrototypes(object, edx, holder, ebx, ecx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001126
1127 // Get the value from the cell.
1128 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1129 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1130
1131 // Check that the cell contains the same function.
1132 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1133 __ j(not_equal, &miss, not_taken);
1134
1135 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001136 if (object->IsGlobalObject()) {
1137 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1138 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1139 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001140
1141 // Setup the context (function already in edi).
1142 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1143
1144 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001145 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001146 ASSERT(function->is_compiled());
1147 Handle<Code> code(function->code());
1148 ParameterCount expected(function->shared()->formal_parameter_count());
1149 __ InvokeCode(code, expected, arguments(),
1150 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1151
1152 // Handle call cache miss.
1153 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001154 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1155 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1156 __ jmp(ic, RelocInfo::CODE_TARGET);
1157
1158 // Return the generated code.
1159 return GetCode(NORMAL, name);
1160}
1161
1162
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001163Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1164 int index,
1165 Map* transition,
1166 String* name) {
1167 // ----------- S t a t e -------------
1168 // -- eax : value
1169 // -- ecx : name
1170 // -- esp[0] : return address
1171 // -- esp[4] : receiver
1172 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001173 Label miss;
1174
1175 // Get the object from the stack.
1176 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1177
1178 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001179 GenerateStoreField(masm(),
1180 Builtins::StoreIC_ExtendStorage,
1181 object,
1182 index,
1183 transition,
1184 ebx, ecx, edx,
1185 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001186
1187 // Handle store cache miss.
1188 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001189 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001190 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001191 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001192
1193 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001194 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001195}
1196
1197
1198Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1199 AccessorInfo* callback,
1200 String* name) {
1201 // ----------- S t a t e -------------
1202 // -- eax : value
1203 // -- ecx : name
1204 // -- esp[0] : return address
1205 // -- esp[4] : receiver
1206 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001207 Label miss;
1208
1209 // Get the object from the stack.
1210 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1211
1212 // Check that the object isn't a smi.
1213 __ test(ebx, Immediate(kSmiTagMask));
1214 __ j(zero, &miss, not_taken);
1215
1216 // Check that the map of the object hasn't changed.
1217 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1218 Immediate(Handle<Map>(object->map())));
1219 __ j(not_equal, &miss, not_taken);
1220
1221 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001222 if (object->IsJSGlobalProxy()) {
1223 __ CheckAccessGlobalProxy(ebx, edx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001224 }
1225
1226 // Stub never generated for non-global objects that require access
1227 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001228 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001229
1230 __ pop(ebx); // remove the return address
1231 __ push(Operand(esp, 0)); // receiver
1232 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1233 __ push(ecx); // name
1234 __ push(eax); // value
1235 __ push(ebx); // restore return address
1236
mads.s.ager31e71382008-08-13 09:32:07 +00001237 // Do tail-call to the runtime system.
1238 ExternalReference store_callback_property =
1239 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
1240 __ TailCallRuntime(store_callback_property, 4);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001241
1242 // Handle store cache miss.
1243 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001244 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001245 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001246 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247
1248 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001249 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250}
1251
1252
1253Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1254 String* name) {
1255 // ----------- S t a t e -------------
1256 // -- eax : value
1257 // -- ecx : name
1258 // -- esp[0] : return address
1259 // -- esp[4] : receiver
1260 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001261 Label miss;
1262
1263 // Get the object from the stack.
1264 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1265
1266 // Check that the object isn't a smi.
1267 __ test(ebx, Immediate(kSmiTagMask));
1268 __ j(zero, &miss, not_taken);
1269
1270 // Check that the map of the object hasn't changed.
1271 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1272 Immediate(Handle<Map>(receiver->map())));
1273 __ j(not_equal, &miss, not_taken);
1274
1275 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001276 if (receiver->IsJSGlobalProxy()) {
1277 __ CheckAccessGlobalProxy(ebx, edx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 }
1279
1280 // Stub never generated for non-global objects that require access
1281 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001282 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283
1284 __ pop(ebx); // remove the return address
1285 __ push(Operand(esp, 0)); // receiver
1286 __ push(ecx); // name
1287 __ push(eax); // value
1288 __ push(ebx); // restore return address
1289
mads.s.ager31e71382008-08-13 09:32:07 +00001290 // Do tail-call to the runtime system.
1291 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001292 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
mads.s.ager31e71382008-08-13 09:32:07 +00001293 __ TailCallRuntime(store_ic_property, 3);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001294
1295 // Handle store cache miss.
1296 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001297 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001299 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001300
1301 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001302 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001303}
1304
1305
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001306Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1307 JSGlobalPropertyCell* cell,
1308 String* name) {
1309 // ----------- S t a t e -------------
1310 // -- eax : value
1311 // -- ecx : name
1312 // -- esp[0] : return address
1313 // -- esp[4] : receiver
1314 // -----------------------------------
1315 Label miss;
1316
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001317 // Check that the map of the global has not changed.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001318 __ mov(ebx, Operand(esp, kPointerSize));
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001319 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1320 Immediate(Handle<Map>(object->map())));
1321 __ j(not_equal, &miss, not_taken);
1322
1323 // Store the value in the cell.
1324 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1325 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1326
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001327 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001328 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001329 __ ret(0);
1330
1331 // Handle store cache miss.
1332 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001333 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1334 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1335 __ jmp(ic, RelocInfo::CODE_TARGET);
1336
1337 // Return the generated code.
1338 return GetCode(NORMAL, name);
1339}
1340
1341
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001342Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1343 int index,
1344 Map* transition,
1345 String* name) {
1346 // ----------- S t a t e -------------
1347 // -- eax : value
1348 // -- esp[0] : return address
1349 // -- esp[4] : key
1350 // -- esp[8] : receiver
1351 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001352 Label miss;
1353
1354 __ IncrementCounter(&Counters::keyed_store_field, 1);
1355
1356 // Get the name from the stack.
1357 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1358 // Check that the name has not changed.
1359 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1360 __ j(not_equal, &miss, not_taken);
1361
1362 // Get the object from the stack.
1363 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1364
1365 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001366 GenerateStoreField(masm(),
1367 Builtins::KeyedStoreIC_ExtendStorage,
1368 object,
1369 index,
1370 transition,
1371 ebx, ecx, edx,
1372 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001373
1374 // Handle store cache miss.
1375 __ bind(&miss);
1376 __ DecrementCounter(&Counters::keyed_store_field, 1);
1377 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001378 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001379
1380 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001381 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001382}
1383
1384
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001385
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001386Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1387 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001388 int index,
1389 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001390 // ----------- S t a t e -------------
1391 // -- ecx : name
1392 // -- esp[0] : return address
1393 // -- esp[4] : receiver
1394 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001395 Label miss;
1396
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001397 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001398 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001399 __ bind(&miss);
1400 GenerateLoadMiss(masm(), Code::LOAD_IC);
1401
1402 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001403 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001404}
1405
1406
1407Object* LoadStubCompiler::CompileLoadCallback(JSObject* object,
1408 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001409 AccessorInfo* callback,
1410 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001411 // ----------- S t a t e -------------
1412 // -- ecx : name
1413 // -- esp[0] : return address
1414 // -- esp[4] : receiver
1415 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001416 Label miss;
1417
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001418 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001419 GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1420 callback, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001421 __ bind(&miss);
1422 GenerateLoadMiss(masm(), Code::LOAD_IC);
1423
1424 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001425 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001426}
1427
1428
1429Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1430 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001431 Object* value,
1432 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001433 // ----------- S t a t e -------------
1434 // -- ecx : name
1435 // -- esp[0] : return address
1436 // -- esp[4] : receiver
1437 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001438 Label miss;
1439
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001440 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001441 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442 __ bind(&miss);
1443 GenerateLoadMiss(masm(), Code::LOAD_IC);
1444
1445 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001446 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001447}
1448
1449
1450Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1451 JSObject* holder,
1452 String* name) {
1453 // ----------- S t a t e -------------
1454 // -- ecx : name
1455 // -- esp[0] : return address
1456 // -- esp[4] : receiver
1457 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001458 Label miss;
1459
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001460 LookupResult lookup;
1461 LookupPostInterceptor(holder, name, &lookup);
1462
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001463 __ mov(eax, Operand(esp, kPointerSize));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001464 // TODO(368): Compile in the whole chain: all the interceptors in
1465 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001466 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001467 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001468 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001469 eax,
1470 ecx,
1471 edx,
1472 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001473 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001474 &miss);
1475
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001476 __ bind(&miss);
1477 GenerateLoadMiss(masm(), Code::LOAD_IC);
1478
1479 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001480 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001481}
1482
1483
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001484Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
1485 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001486 JSGlobalPropertyCell* cell,
1487 String* name,
1488 bool is_dont_delete) {
1489 // ----------- S t a t e -------------
1490 // -- ecx : name
1491 // -- esp[0] : return address
1492 // -- esp[4] : receiver
1493 // -----------------------------------
1494 Label miss;
1495
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001496 // Get the receiver from the stack.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001497 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001498
1499 // If the object is the holder then we know that it's a global
1500 // object which can only happen for contextual loads. In this case,
1501 // the receiver cannot be a smi.
1502 if (object != holder) {
1503 __ test(eax, Immediate(kSmiTagMask));
1504 __ j(zero, &miss, not_taken);
1505 }
1506
1507 // Check that the maps haven't changed.
1508 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001509
1510 // Get the value from the cell.
1511 __ mov(eax, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1512 __ mov(eax, FieldOperand(eax, JSGlobalPropertyCell::kValueOffset));
1513
1514 // Check for deleted property if property can actually be deleted.
1515 if (!is_dont_delete) {
1516 __ cmp(eax, Factory::the_hole_value());
1517 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001518 } else if (FLAG_debug_code) {
1519 __ cmp(eax, Factory::the_hole_value());
1520 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001521 }
1522
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001523 __ IncrementCounter(&Counters::named_load_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001524 __ ret(0);
1525
1526 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001527 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
1528 GenerateLoadMiss(masm(), Code::LOAD_IC);
1529
1530 // Return the generated code.
1531 return GetCode(NORMAL, name);
1532}
1533
1534
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001535Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
1536 JSObject* receiver,
1537 JSObject* holder,
1538 int index) {
1539 // ----------- S t a t e -------------
1540 // -- esp[0] : return address
1541 // -- esp[4] : name
1542 // -- esp[8] : receiver
1543 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001544 Label miss;
1545
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001546 __ mov(eax, Operand(esp, kPointerSize));
1547 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001548 __ IncrementCounter(&Counters::keyed_load_field, 1);
1549
1550 // Check that the name has not changed.
1551 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1552 __ j(not_equal, &miss, not_taken);
1553
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001554 GenerateLoadField(receiver, holder, ecx, ebx, edx, index, name, &miss);
1555
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001556 __ bind(&miss);
1557 __ DecrementCounter(&Counters::keyed_load_field, 1);
1558 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1559
1560 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001561 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001562}
1563
1564
1565Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
1566 JSObject* receiver,
1567 JSObject* holder,
1568 AccessorInfo* callback) {
1569 // ----------- S t a t e -------------
1570 // -- esp[0] : return address
1571 // -- esp[4] : name
1572 // -- esp[8] : receiver
1573 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001574 Label miss;
1575
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001576 __ mov(eax, Operand(esp, kPointerSize));
1577 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001578 __ IncrementCounter(&Counters::keyed_load_callback, 1);
1579
1580 // Check that the name has not changed.
1581 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1582 __ j(not_equal, &miss, not_taken);
1583
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001584 GenerateLoadCallback(receiver, holder, ecx, eax, ebx, edx,
1585 callback, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001586 __ bind(&miss);
1587 __ DecrementCounter(&Counters::keyed_load_callback, 1);
1588 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1589
1590 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001591 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001592}
1593
1594
1595Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
1596 JSObject* receiver,
1597 JSObject* holder,
1598 Object* value) {
1599 // ----------- S t a t e -------------
1600 // -- esp[0] : return address
1601 // -- esp[4] : name
1602 // -- esp[8] : receiver
1603 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001604 Label miss;
1605
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001606 __ mov(eax, Operand(esp, kPointerSize));
1607 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001608 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
1609
1610 // Check that the name has not changed.
1611 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1612 __ j(not_equal, &miss, not_taken);
1613
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001614 GenerateLoadConstant(receiver, holder, ecx, ebx, edx,
1615 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001616 __ bind(&miss);
1617 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
1618 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1619
1620 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001621 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622}
1623
1624
1625Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1626 JSObject* holder,
1627 String* name) {
1628 // ----------- S t a t e -------------
1629 // -- esp[0] : return address
1630 // -- esp[4] : name
1631 // -- esp[8] : receiver
1632 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001633 Label miss;
1634
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001635 __ mov(eax, Operand(esp, kPointerSize));
1636 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001637 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
1638
1639 // Check that the name has not changed.
1640 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1641 __ j(not_equal, &miss, not_taken);
1642
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001643 LookupResult lookup;
1644 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001645 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001646 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001647 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001648 ecx,
1649 eax,
1650 edx,
1651 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001652 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001653 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001654 __ bind(&miss);
1655 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
1656 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1657
1658 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001659 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001660}
1661
1662
1663
1664
1665Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
1666 // ----------- S t a t e -------------
1667 // -- esp[0] : return address
1668 // -- esp[4] : name
1669 // -- esp[8] : receiver
1670 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001671 Label miss;
1672
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001673 __ mov(eax, Operand(esp, kPointerSize));
1674 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001675 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
1676
1677 // Check that the name has not changed.
1678 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1679 __ j(not_equal, &miss, not_taken);
1680
1681 GenerateLoadArrayLength(masm(), ecx, edx, &miss);
1682 __ bind(&miss);
1683 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
1684 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1685
1686 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001687 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001688}
1689
1690
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001691Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001692 // ----------- S t a t e -------------
1693 // -- esp[0] : return address
1694 // -- esp[4] : name
1695 // -- esp[8] : receiver
1696 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001697 Label miss;
1698
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001699 __ mov(eax, Operand(esp, kPointerSize));
1700 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001701 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
1702
1703 // Check that the name has not changed.
1704 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1705 __ j(not_equal, &miss, not_taken);
1706
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001707 GenerateLoadStringLength(masm(), ecx, edx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001708 __ bind(&miss);
1709 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
1710 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1711
1712 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001713 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001714}
1715
1716
1717Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
1718 // ----------- S t a t e -------------
1719 // -- esp[0] : return address
1720 // -- esp[4] : name
1721 // -- esp[8] : receiver
1722 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 Label miss;
1724
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001725 __ mov(eax, Operand(esp, kPointerSize));
1726 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001727 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
1728
1729 // Check that the name has not changed.
1730 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1731 __ j(not_equal, &miss, not_taken);
1732
1733 GenerateLoadFunctionPrototype(masm(), ecx, edx, ebx, &miss);
1734 __ bind(&miss);
1735 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
1736 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1737
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
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001743// Specialized stub for constructing objects from functions which only have only
1744// simple assignments of the form this.x = ...; in their body.
1745Object* ConstructStubCompiler::CompileConstructStub(
1746 SharedFunctionInfo* shared) {
1747 // ----------- S t a t e -------------
1748 // -- eax : argc
1749 // -- edi : constructor
1750 // -- esp[0] : return address
1751 // -- esp[4] : last argument
1752 // -----------------------------------
1753 Label generic_stub_call;
1754#ifdef ENABLE_DEBUGGER_SUPPORT
1755 // Check to see whether there are any break points in the function code. If
1756 // there are jump to the generic constructor stub which calls the actual
1757 // code for the function thereby hitting the break points.
1758 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1759 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
1760 __ cmp(ebx, Factory::undefined_value());
1761 __ j(not_equal, &generic_stub_call, not_taken);
1762#endif
1763
1764 // Load the initial map and verify that it is in fact a map.
1765 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1766 // Will both indicate a NULL and a Smi.
1767 __ test(ebx, Immediate(kSmiTagMask));
1768 __ j(zero, &generic_stub_call);
1769 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1770 __ j(not_equal, &generic_stub_call);
1771
1772#ifdef DEBUG
1773 // Cannot construct functions this way.
1774 // edi: constructor
1775 // ebx: initial map
1776 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
1777 __ Assert(not_equal, "Function constructed by construct stub.");
1778#endif
1779
1780 // Now allocate the JSObject on the heap by moving the new space allocation
1781 // top forward.
1782 // edi: constructor
1783 // ebx: initial map
1784 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
1785 __ shl(ecx, kPointerSizeLog2);
1786 // Make sure that the maximum heap object size will never cause us
1787 // problems here.
1788 ASSERT(Heap::MaxObjectSizeInPagedSpace() >= JSObject::kMaxInstanceSize);
1789 __ AllocateObjectInNewSpace(ecx, edx, ecx, no_reg, &generic_stub_call, false);
1790
1791 // Allocated the JSObject, now initialize the fields and add the heap tag.
1792 // ebx: initial map
1793 // edx: JSObject (untagged)
1794 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
1795 __ mov(ebx, Factory::empty_fixed_array());
1796 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
1797 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
1798
1799 // Push the allocated object to the stack. This is the object that will be
1800 // returned (after it is tagged).
1801 __ push(edx);
1802
1803 // eax: argc
1804 // edx: JSObject (untagged)
1805 // Load the address of the first in-object property into edx.
1806 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
1807 // Calculate the location of the first argument. The stack contains the
1808 // allocated object and the return address on top of the argc arguments.
1809 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
1810
1811 // Use edi for holding undefined which is used in several places below.
1812 __ mov(edi, Factory::undefined_value());
1813
1814 // eax: argc
1815 // ecx: first argument
1816 // edx: first in-object property of the JSObject
1817 // edi: undefined
1818 // Fill the initialized properties with a constant value or a passed argument
1819 // depending on the this.x = ...; assignment in the function.
1820 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
1821 if (shared->IsThisPropertyAssignmentArgument(i)) {
1822 Label not_passed;
1823 // Set the property to undefined.
1824 __ mov(Operand(edx, i * kPointerSize), edi);
1825 // Check if the argument assigned to the property is actually passed.
1826 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
1827 __ cmp(eax, arg_number);
1828 __ j(below_equal, &not_passed);
1829 // Argument passed - find it on the stack.
1830 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
1831 __ mov(Operand(edx, i * kPointerSize), ebx);
1832 __ bind(&not_passed);
1833 } else {
1834 // Set the property to the constant value.
1835 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
1836 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
1837 }
1838 }
1839
1840 // Fill the unused in-object property fields with undefined.
1841 for (int i = shared->this_property_assignments_count();
1842 i < shared->CalculateInObjectProperties();
1843 i++) {
1844 __ mov(Operand(edx, i * kPointerSize), edi);
1845 }
1846
1847 // Move argc to ebx and retrieve and tag the JSObject to return.
1848 __ mov(ebx, eax);
1849 __ pop(eax);
1850 __ or_(Operand(eax), Immediate(kHeapObjectTag));
1851
1852 // Remove caller arguments and receiver from the stack and return.
1853 __ pop(ecx);
1854 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
1855 __ push(ecx);
1856 __ IncrementCounter(&Counters::constructed_objects, 1);
1857 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
1858 __ ret(0);
1859
1860 // Jump to the generic stub in case the specialized code cannot handle the
1861 // construction.
1862 __ bind(&generic_stub_call);
1863 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
1864 Handle<Code> generic_construct_stub(code);
1865 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
1866
1867 // Return the generated code.
1868 return GetCode();
1869}
1870
1871
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001872#undef __
1873
1874} } // namespace v8::internal