blob: 7acf81c9495de8b0e716c518ad08a638088df5c8 [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
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000155static void PushInterceptorArguments(MacroAssembler* masm,
156 Register receiver,
157 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000158 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000159 JSObject* holder_obj) {
160 __ push(receiver);
161 __ push(holder);
162 __ push(name);
163 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000164 ASSERT(!Heap::InNewSpace(interceptor));
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000165 __ 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
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000237 // Load length from the string and convert to a smi.
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000238 __ bind(&load_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000239 __ mov(eax, FieldOperand(receiver, String::kLengthOffset));
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000240 __ SmiTag(eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000241 __ ret(0);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000242
243 // Check if the object is a JSValue wrapper.
244 __ bind(&check_wrapper);
kasperl@chromium.org9bbf9682008-10-30 11:53:07 +0000245 __ cmp(scratch, JS_VALUE_TYPE);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000246 __ j(not_equal, miss, not_taken);
247
248 // Check if the wrapped value is a string and load the length
249 // directly if it is.
250 __ mov(receiver, FieldOperand(receiver, JSValue::kValueOffset));
251 GenerateStringCheck(masm, receiver, scratch, miss, miss);
252 __ jmp(&load_length);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000253}
254
255
256void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
257 Register receiver,
258 Register scratch1,
259 Register scratch2,
260 Label* miss_label) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000261 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000262 __ mov(eax, Operand(scratch1));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000263 __ ret(0);
ager@chromium.org7c537e22008-10-16 08:43:32 +0000264}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000265
ager@chromium.org7c537e22008-10-16 08:43:32 +0000266
267// Load a fast property out of a holder object (src). In-object properties
268// are loaded directly otherwise the property is loaded from the properties
269// fixed array.
270void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000271 Register dst, Register src,
272 JSObject* holder, int index) {
ager@chromium.org7c537e22008-10-16 08:43:32 +0000273 // Adjust for the number of properties stored in the holder.
274 index -= holder->map()->inobject_properties();
275 if (index < 0) {
276 // Get the property straight out of the holder.
277 int offset = holder->map()->instance_size() + (index * kPointerSize);
278 __ mov(dst, FieldOperand(src, offset));
279 } else {
280 // Calculate the offset into the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000281 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.org7c537e22008-10-16 08:43:32 +0000282 __ mov(dst, FieldOperand(src, JSObject::kPropertiesOffset));
283 __ mov(dst, FieldOperand(dst, offset));
284 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000285}
286
287
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000288static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
289 Register receiver,
290 Register holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000291 Register name,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000292 JSObject* holder_obj) {
293 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
294
295 ExternalReference ref =
296 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly));
297 __ mov(eax, Immediate(5));
298 __ mov(ebx, Immediate(ref));
299
ager@chromium.orga1645e22009-09-09 19:27:10 +0000300 CEntryStub stub(1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000301 __ CallStub(&stub);
302}
303
304
305template <class Compiler>
306static void CompileLoadInterceptor(Compiler* compiler,
307 StubCompiler* stub_compiler,
308 MacroAssembler* masm,
309 JSObject* object,
310 JSObject* holder,
311 String* name,
312 LookupResult* lookup,
313 Register receiver,
314 Register scratch1,
315 Register scratch2,
316 Label* miss) {
317 ASSERT(holder->HasNamedInterceptor());
318 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
319
320 // Check that the receiver isn't a smi.
321 __ test(receiver, Immediate(kSmiTagMask));
322 __ j(zero, miss, not_taken);
323
324 // Check that the maps haven't changed.
325 Register reg =
326 stub_compiler->CheckPrototypes(object, receiver, holder,
327 scratch1, scratch2, name, miss);
328
329 if (lookup->IsValid() && lookup->IsCacheable()) {
330 compiler->CompileCacheable(masm,
331 stub_compiler,
332 receiver,
333 reg,
334 scratch1,
335 scratch2,
336 holder,
337 lookup,
338 name,
339 miss);
340 } else {
341 compiler->CompileRegular(masm,
342 receiver,
343 reg,
344 scratch2,
345 holder,
346 miss);
347 }
348}
349
350
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000351class LoadInterceptorCompiler BASE_EMBEDDED {
352 public:
353 explicit LoadInterceptorCompiler(Register name) : name_(name) {}
354
355 void CompileCacheable(MacroAssembler* masm,
356 StubCompiler* stub_compiler,
357 Register receiver,
358 Register holder,
359 Register scratch1,
360 Register scratch2,
361 JSObject* holder_obj,
362 LookupResult* lookup,
363 String* name,
364 Label* miss_label) {
365 AccessorInfo* callback = 0;
366 bool optimize = false;
367 // So far the most popular follow ups for interceptor loads are FIELD
368 // and CALLBACKS, so inline only them, other cases may be added
369 // later.
370 if (lookup->type() == FIELD) {
371 optimize = true;
372 } else if (lookup->type() == CALLBACKS) {
373 Object* callback_object = lookup->GetCallbackObject();
374 if (callback_object->IsAccessorInfo()) {
375 callback = AccessorInfo::cast(callback_object);
376 optimize = callback->getter() != NULL;
377 }
378 }
379
380 if (!optimize) {
381 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
382 return;
383 }
384
385 // Note: starting a frame here makes GC aware of pointers pushed below.
386 __ EnterInternalFrame();
387
388 if (lookup->type() == CALLBACKS) {
389 __ push(receiver);
390 }
391 __ push(holder);
392 __ push(name_);
393
394 CompileCallLoadPropertyWithInterceptor(masm,
395 receiver,
396 holder,
397 name_,
398 holder_obj);
399
400 Label interceptor_failed;
401 __ cmp(eax, Factory::no_interceptor_result_sentinel());
402 __ j(equal, &interceptor_failed);
403 __ LeaveInternalFrame();
404 __ ret(0);
405
406 __ bind(&interceptor_failed);
407 __ pop(name_);
408 __ pop(holder);
409 if (lookup->type() == CALLBACKS) {
410 __ pop(receiver);
411 }
412
413 __ LeaveInternalFrame();
414
415 if (lookup->type() == FIELD) {
416 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
417 lookup->holder(), scratch1,
418 scratch2,
419 name,
420 miss_label);
421 stub_compiler->GenerateFastPropertyLoad(masm, eax,
422 holder, lookup->holder(),
423 lookup->GetFieldIndex());
424 __ ret(0);
425 } else {
426 ASSERT(lookup->type() == CALLBACKS);
427 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
428 ASSERT(callback != NULL);
429 ASSERT(callback->getter() != NULL);
430
431 Label cleanup;
432 __ pop(scratch2);
433 __ push(receiver);
434 __ push(scratch2);
435
436 holder = stub_compiler->CheckPrototypes(holder_obj, holder,
437 lookup->holder(), scratch1,
438 scratch2,
439 name,
440 &cleanup);
441
442 __ pop(scratch2); // save old return address
443 __ push(holder);
444 __ mov(holder, Immediate(Handle<AccessorInfo>(callback)));
445 __ push(holder);
446 __ push(FieldOperand(holder, AccessorInfo::kDataOffset));
447 __ push(name_);
448 __ push(scratch2); // restore old return address
449
450 ExternalReference ref =
451 ExternalReference(IC_Utility(IC::kLoadCallbackProperty));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000452 __ TailCallRuntime(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000453
454 __ bind(&cleanup);
455 __ pop(scratch1);
456 __ pop(scratch2);
457 __ push(scratch1);
458 }
459 }
460
461
462 void CompileRegular(MacroAssembler* masm,
463 Register receiver,
464 Register holder,
465 Register scratch,
466 JSObject* holder_obj,
467 Label* miss_label) {
468 __ pop(scratch); // save old return address
469 PushInterceptorArguments(masm, receiver, holder, name_, holder_obj);
470 __ push(scratch); // restore old return address
471
472 ExternalReference ref = ExternalReference(
473 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad));
ager@chromium.orga1645e22009-09-09 19:27:10 +0000474 __ TailCallRuntime(ref, 5, 1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000475 }
476
477 private:
478 Register name_;
479};
480
481
482class CallInterceptorCompiler BASE_EMBEDDED {
483 public:
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000484 CallInterceptorCompiler(const ParameterCount& arguments, Register name)
485 : arguments_(arguments), argc_(arguments.immediate()), name_(name) {}
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000486
487 void CompileCacheable(MacroAssembler* masm,
488 StubCompiler* stub_compiler,
489 Register receiver,
490 Register holder,
491 Register scratch1,
492 Register scratch2,
493 JSObject* holder_obj,
494 LookupResult* lookup,
495 String* name,
496 Label* miss_label) {
497 JSFunction* function = 0;
498 bool optimize = false;
499 // So far the most popular case for failed interceptor is
500 // CONSTANT_FUNCTION sitting below.
501 if (lookup->type() == CONSTANT_FUNCTION) {
502 function = lookup->GetConstantFunction();
503 // JSArray holder is a special case for call constant function
504 // (see the corresponding code).
505 if (function->is_compiled() && !holder_obj->IsJSArray()) {
506 optimize = true;
507 }
508 }
509
510 if (!optimize) {
511 CompileRegular(masm, receiver, holder, scratch2, holder_obj, miss_label);
512 return;
513 }
514
515 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000516 __ push(holder); // Save the holder.
517 __ push(name_); // Save the name.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000518
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000519 CompileCallLoadPropertyWithInterceptor(masm,
520 receiver,
521 holder,
522 name_,
523 holder_obj);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000524
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000525 __ pop(name_); // Restore the name.
526 __ pop(receiver); // Restore the holder.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000527 __ LeaveInternalFrame();
528
529 __ cmp(eax, Factory::no_interceptor_result_sentinel());
530 Label invoke;
531 __ j(not_equal, &invoke);
532
533 stub_compiler->CheckPrototypes(holder_obj, receiver,
534 lookup->holder(), scratch1,
535 scratch2,
536 name,
537 miss_label);
538 if (lookup->holder()->IsGlobalObject()) {
539 __ mov(edx, Operand(esp, (argc_ + 1) * kPointerSize));
540 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
541 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edx);
542 }
543
544 ASSERT(function->is_compiled());
545 // Get the function and setup the context.
546 __ mov(edi, Immediate(Handle<JSFunction>(function)));
547 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
548
549 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000550 Handle<Code> code(function->code());
551 ParameterCount expected(function->shared()->formal_parameter_count());
552 __ InvokeCode(code, expected, arguments_,
553 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
554
555 __ bind(&invoke);
556 }
557
558 void CompileRegular(MacroAssembler* masm,
559 Register receiver,
560 Register holder,
561 Register scratch,
562 JSObject* holder_obj,
563 Label* miss_label) {
564 __ EnterInternalFrame();
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000565 // Save the name_ register across the call.
566 __ push(name_);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000567
568 PushInterceptorArguments(masm,
569 receiver,
570 holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000571 name_,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000572 holder_obj);
573
574 ExternalReference ref = ExternalReference(
575 IC_Utility(IC::kLoadPropertyWithInterceptorForCall));
576 __ mov(eax, Immediate(5));
577 __ mov(ebx, Immediate(ref));
578
ager@chromium.orga1645e22009-09-09 19:27:10 +0000579 CEntryStub stub(1);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000580 __ CallStub(&stub);
581
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000582 // Restore the name_ register.
583 __ pop(name_);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000584 __ LeaveInternalFrame();
585 }
586
587 private:
588 const ParameterCount& arguments_;
589 int argc_;
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000590 Register name_;
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000591};
592
593
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000594void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
595 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
596 Code* code = NULL;
597 if (kind == Code::LOAD_IC) {
598 code = Builtins::builtin(Builtins::LoadIC_Miss);
599 } else {
600 code = Builtins::builtin(Builtins::KeyedLoadIC_Miss);
601 }
602
603 Handle<Code> ic(code);
ager@chromium.org236ad962008-09-25 09:45:57 +0000604 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000605}
606
607
608void StubCompiler::GenerateStoreField(MacroAssembler* masm,
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000609 Builtins::Name storage_extend,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000610 JSObject* object,
611 int index,
612 Map* transition,
613 Register receiver_reg,
614 Register name_reg,
615 Register scratch,
616 Label* miss_label) {
617 // Check that the object isn't a smi.
618 __ test(receiver_reg, Immediate(kSmiTagMask));
619 __ j(zero, miss_label, not_taken);
620
621 // Check that the map of the object hasn't changed.
622 __ cmp(FieldOperand(receiver_reg, HeapObject::kMapOffset),
623 Immediate(Handle<Map>(object->map())));
624 __ j(not_equal, miss_label, not_taken);
625
626 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000627 if (object->IsJSGlobalProxy()) {
628 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629 }
630
631 // Stub never generated for non-global objects that require access
632 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000633 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000634
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000635 // Perform map transition for the receiver if necessary.
636 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
637 // The properties must be extended before we can store the value.
ager@chromium.org32912102009-01-16 10:38:43 +0000638 // We jump to a runtime call that extends the properties array.
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000639 __ mov(ecx, Immediate(Handle<Map>(transition)));
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000640 Handle<Code> ic(Builtins::builtin(storage_extend));
641 __ jmp(ic, RelocInfo::CODE_TARGET);
642 return;
643 }
644
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000645 if (transition != NULL) {
kasperl@chromium.org1accd572008-10-07 10:57:21 +0000646 // Update the map of the object; no write barrier updating is
647 // needed because the map is never in new space.
648 __ mov(FieldOperand(receiver_reg, HeapObject::kMapOffset),
649 Immediate(Handle<Map>(transition)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 }
651
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000652 // Adjust for the number of properties stored in the object. Even in the
653 // face of a transition we can use the old map here because the size of the
654 // object and the number of in-object properties is not going to change.
655 index -= object->map()->inobject_properties();
656
ager@chromium.org7c537e22008-10-16 08:43:32 +0000657 if (index < 0) {
658 // Set the property straight into the object.
659 int offset = object->map()->instance_size() + (index * kPointerSize);
660 __ mov(FieldOperand(receiver_reg, offset), eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000661
ager@chromium.org7c537e22008-10-16 08:43:32 +0000662 // Update the write barrier for the array address.
663 // Pass the value being stored in the now unused name_reg.
664 __ mov(name_reg, Operand(eax));
665 __ RecordWrite(receiver_reg, offset, name_reg, scratch);
666 } else {
667 // Write to the properties array.
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000668 int offset = index * kPointerSize + FixedArray::kHeaderSize;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000669 // Get the properties array (optimistically).
670 __ mov(scratch, FieldOperand(receiver_reg, JSObject::kPropertiesOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000671 __ mov(FieldOperand(scratch, offset), eax);
672
673 // Update the write barrier for the array address.
674 // Pass the value being stored in the now unused name_reg.
675 __ mov(name_reg, Operand(eax));
676 __ RecordWrite(scratch, offset, name_reg, receiver_reg);
677 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000678
679 // Return the value (register eax).
680 __ ret(0);
681}
682
683
684#undef __
ager@chromium.org65dad4b2009-04-23 08:48:43 +0000685#define __ ACCESS_MASM(masm())
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000686
687
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000688Register StubCompiler::CheckPrototypes(JSObject* object,
689 Register object_reg,
690 JSObject* holder,
691 Register holder_reg,
692 Register scratch,
693 String* name,
694 Label* miss) {
695 // Check that the maps haven't changed.
696 Register result =
697 masm()->CheckMaps(object, object_reg, holder, holder_reg, scratch, miss);
698
699 // If we've skipped any global objects, it's not enough to verify
700 // that their maps haven't changed.
701 while (object != holder) {
702 if (object->IsGlobalObject()) {
703 GlobalObject* global = GlobalObject::cast(object);
704 Object* probe = global->EnsurePropertyCell(name);
705 if (probe->IsFailure()) {
706 set_failure(Failure::cast(probe));
707 return result;
708 }
709 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
710 ASSERT(cell->value()->IsTheHole());
711 __ mov(scratch, Immediate(Handle<Object>(cell)));
712 __ cmp(FieldOperand(scratch, JSGlobalPropertyCell::kValueOffset),
713 Immediate(Factory::the_hole_value()));
714 __ j(not_equal, miss, not_taken);
715 }
716 object = JSObject::cast(object->GetPrototype());
717 }
718
719 // Return the register containin the holder.
720 return result;
721}
722
723
724void StubCompiler::GenerateLoadField(JSObject* object,
725 JSObject* holder,
726 Register receiver,
727 Register scratch1,
728 Register scratch2,
729 int index,
730 String* name,
731 Label* miss) {
732 // Check that the receiver isn't a smi.
733 __ test(receiver, Immediate(kSmiTagMask));
734 __ j(zero, miss, not_taken);
735
736 // Check the prototype chain.
737 Register reg =
738 CheckPrototypes(object, receiver, holder,
739 scratch1, scratch2, name, miss);
740
741 // Get the value from the properties.
742 GenerateFastPropertyLoad(masm(), eax, reg, holder, index);
743 __ ret(0);
744}
745
746
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000747bool StubCompiler::GenerateLoadCallback(JSObject* object,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000748 JSObject* holder,
749 Register receiver,
750 Register name_reg,
751 Register scratch1,
752 Register scratch2,
753 AccessorInfo* callback,
754 String* name,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000755 Label* miss,
756 Failure** failure) {
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000757 // Check that the receiver isn't a smi.
758 __ test(receiver, Immediate(kSmiTagMask));
759 __ j(zero, miss, not_taken);
760
761 // Check that the maps haven't changed.
762 Register reg =
763 CheckPrototypes(object, receiver, holder,
764 scratch1, scratch2, name, miss);
765
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000766 Handle<AccessorInfo> callback_handle(callback);
767
768 Register other = reg.is(scratch1) ? scratch2 : scratch1;
769 __ EnterInternalFrame();
770 __ PushHandleScope(other);
771 // Push the stack address where the list of arguments ends
772 __ mov(other, esp);
773 __ sub(Operand(other), Immediate(2 * kPointerSize));
774 __ push(other);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000775 __ push(receiver); // receiver
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000776 __ push(reg); // holder
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000777 __ mov(other, Immediate(callback_handle));
778 __ push(other);
779 __ push(FieldOperand(other, AccessorInfo::kDataOffset)); // data
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000780 __ push(name_reg); // name
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000781 // Save a pointer to where we pushed the arguments pointer.
782 // This will be passed as the const Arguments& to the C++ callback.
783 __ mov(eax, esp);
784 __ add(Operand(eax), Immediate(5 * kPointerSize));
785 __ mov(ebx, esp);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000786
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000787 // Do call through the api.
788 ASSERT_EQ(6, ApiGetterEntryStub::kStackSpace);
789 Address getter_address = v8::ToCData<Address>(callback->getter());
790 ApiFunction fun(getter_address);
791 ApiGetterEntryStub stub(callback_handle, &fun);
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000792 // Emitting a stub call may try to allocate (if the code is not
793 // already generated). Do not allow the assembler to perform a
794 // garbage collection but instead return the allocation failure
795 // object.
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000796 Object* result = masm()->TryCallStub(&stub);
797 if (result->IsFailure()) {
798 *failure = Failure::cast(result);
799 return false;
800 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000801
802 // We need to avoid using eax since that now holds the result.
803 Register tmp = other.is(eax) ? reg : other;
kmillikin@chromium.org2d5475f2009-12-20 18:15:52 +0000804 // Emitting PopHandleScope may try to allocate. Do not allow the
805 // assembler to perform a garbage collection but instead return a
806 // failure object.
807 result = masm()->TryPopHandleScope(eax, tmp);
808 if (result->IsFailure()) {
809 *failure = Failure::cast(result);
810 return false;
811 }
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000812 __ LeaveInternalFrame();
813
814 __ ret(0);
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +0000815 return true;
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000816}
817
818
819void StubCompiler::GenerateLoadConstant(JSObject* object,
820 JSObject* holder,
821 Register receiver,
822 Register scratch1,
823 Register scratch2,
824 Object* value,
825 String* name,
826 Label* miss) {
827 // Check that the receiver isn't a smi.
828 __ test(receiver, Immediate(kSmiTagMask));
829 __ j(zero, miss, not_taken);
830
831 // Check that the maps haven't changed.
832 Register reg =
833 CheckPrototypes(object, receiver, holder,
834 scratch1, scratch2, name, miss);
835
836 // Return the constant value.
837 __ mov(eax, Handle<Object>(value));
838 __ ret(0);
839}
840
841
842void StubCompiler::GenerateLoadInterceptor(JSObject* object,
843 JSObject* holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000844 LookupResult* lookup,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000845 Register receiver,
846 Register name_reg,
847 Register scratch1,
848 Register scratch2,
849 String* name,
850 Label* miss) {
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +0000851 LoadInterceptorCompiler compiler(name_reg);
852 CompileLoadInterceptor(&compiler,
853 this,
854 masm(),
855 object,
856 holder,
857 name,
858 lookup,
859 receiver,
860 scratch1,
861 scratch2,
862 miss);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000863}
864
865
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000866// TODO(1241006): Avoid having lazy compile stubs specialized by the
867// number of arguments. It is not needed anymore.
868Object* StubCompiler::CompileLazyCompile(Code::Flags flags) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000869 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000870 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000871
872 // Push a copy of the function onto the stack.
873 __ push(edi);
874
875 __ push(edi); // function is also the parameter to the runtime call
876 __ CallRuntime(Runtime::kLazyCompile, 1);
877 __ pop(edi);
878
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000879 // Tear down temporary frame.
ager@chromium.org236ad962008-09-25 09:45:57 +0000880 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000881
882 // Do a tail-call of the compiled function.
883 __ lea(ecx, FieldOperand(eax, Code::kHeaderSize));
884 __ jmp(Operand(ecx));
885
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000886 return GetCodeWithFlags(flags, "LazyCompileStub");
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000887}
888
889
890Object* CallStubCompiler::CompileCallField(Object* object,
891 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000892 int index,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000893 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000894 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000895 // -- ecx : name
896 // -- esp[0] : return address
897 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
898 // -- ...
899 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000900 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000901 Label miss;
902
903 // Get the receiver from the stack.
904 const int argc = arguments().immediate();
905 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
906
907 // Check that the receiver isn't a smi.
908 __ test(edx, Immediate(kSmiTagMask));
909 __ j(zero, &miss, not_taken);
910
911 // Do the right check and compute the holder register.
912 Register reg =
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000913 CheckPrototypes(JSObject::cast(object), edx, holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000914 ebx, eax, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000915
ager@chromium.org7c537e22008-10-16 08:43:32 +0000916 GenerateFastPropertyLoad(masm(), edi, reg, holder, index);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000917
918 // Check that the function really is a function.
919 __ test(edi, Immediate(kSmiTagMask));
920 __ j(zero, &miss, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000921 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000922 __ j(not_equal, &miss, not_taken);
923
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000924 // Patch the receiver on the stack with the global proxy if
925 // necessary.
926 if (object->IsGlobalObject()) {
927 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
928 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
929 }
930
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000931 // Invoke the function.
932 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
933
934 // Handle call cache miss.
935 __ bind(&miss);
936 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +0000937 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000938
939 // Return the generated code.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000940 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000941}
942
943
944Object* CallStubCompiler::CompileCallConstant(Object* object,
945 JSObject* holder,
946 JSFunction* function,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000947 String* name,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +0000948 CheckType check) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000949 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000950 // -- ecx : name
951 // -- esp[0] : return address
952 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
953 // -- ...
954 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000955 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000956 Label miss;
957
958 // Get the receiver from the stack.
959 const int argc = arguments().immediate();
960 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
961
962 // Check that the receiver isn't a smi.
963 if (check != NUMBER_CHECK) {
964 __ test(edx, Immediate(kSmiTagMask));
965 __ j(zero, &miss, not_taken);
966 }
967
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000968 // Make sure that it's okay not to patch the on stack receiver
969 // unless we're doing a receiver map check.
970 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
971
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000972 switch (check) {
973 case RECEIVER_MAP_CHECK:
974 // Check that the maps haven't changed.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +0000975 CheckPrototypes(JSObject::cast(object), edx, holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +0000976 ebx, eax, name, &miss);
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +0000977
978 // Patch the receiver on the stack with the global proxy if
979 // necessary.
980 if (object->IsGlobalObject()) {
981 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
982 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
983 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000984 break;
985
986 case STRING_CHECK:
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000987 if (!function->IsBuiltin()) {
988 // Calling non-builtins with a value as receiver requires boxing.
989 __ jmp(&miss);
990 } else {
991 // Check that the object is a string or a symbol.
992 __ mov(eax, FieldOperand(edx, HeapObject::kMapOffset));
993 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceTypeOffset));
994 __ cmp(eax, FIRST_NONSTRING_TYPE);
995 __ j(above_equal, &miss, not_taken);
996 // Check that the maps starting from the prototype haven't changed.
997 GenerateLoadGlobalFunctionPrototype(masm(),
998 Context::STRING_FUNCTION_INDEX,
999 eax);
1000 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1001 ebx, edx, name, &miss);
1002 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001003 break;
1004
1005 case NUMBER_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001006 if (!function->IsBuiltin()) {
1007 // Calling non-builtins with a value as receiver requires boxing.
1008 __ jmp(&miss);
1009 } else {
1010 Label fast;
1011 // Check that the object is a smi or a heap number.
1012 __ test(edx, Immediate(kSmiTagMask));
1013 __ j(zero, &fast, taken);
1014 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, eax);
1015 __ j(not_equal, &miss, not_taken);
1016 __ bind(&fast);
1017 // Check that the maps starting from the prototype haven't changed.
1018 GenerateLoadGlobalFunctionPrototype(masm(),
1019 Context::NUMBER_FUNCTION_INDEX,
1020 eax);
1021 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1022 ebx, edx, name, &miss);
1023 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001024 break;
1025 }
1026
1027 case BOOLEAN_CHECK: {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001028 if (!function->IsBuiltin()) {
1029 // Calling non-builtins with a value as receiver requires boxing.
1030 __ jmp(&miss);
1031 } else {
1032 Label fast;
1033 // Check that the object is a boolean.
1034 __ cmp(edx, Factory::true_value());
1035 __ j(equal, &fast, taken);
1036 __ cmp(edx, Factory::false_value());
1037 __ j(not_equal, &miss, not_taken);
1038 __ bind(&fast);
1039 // Check that the maps starting from the prototype haven't changed.
1040 GenerateLoadGlobalFunctionPrototype(masm(),
1041 Context::BOOLEAN_FUNCTION_INDEX,
1042 eax);
1043 CheckPrototypes(JSObject::cast(object->GetPrototype()), eax, holder,
1044 ebx, edx, name, &miss);
1045 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001046 break;
1047 }
1048
1049 case JSARRAY_HAS_FAST_ELEMENTS_CHECK:
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001050 CheckPrototypes(JSObject::cast(object), edx, holder,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001051 ebx, eax, name, &miss);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001052 // Make sure object->HasFastElements().
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001053 // Get the elements array of the object.
1054 __ mov(ebx, FieldOperand(edx, JSObject::kElementsOffset));
1055 // Check that the object is in fast mode (not dictionary).
1056 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001057 Immediate(Factory::fixed_array_map()));
1058 __ j(not_equal, &miss, not_taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001059 break;
1060
1061 default:
1062 UNREACHABLE();
1063 }
1064
1065 // Get the function and setup the context.
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001066 __ mov(edi, Immediate(Handle<JSFunction>(function)));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001067 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1068
1069 // Jump to the cached code (tail call).
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001070 ASSERT(function->is_compiled());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001071 Handle<Code> code(function->code());
1072 ParameterCount expected(function->shared()->formal_parameter_count());
ager@chromium.org236ad962008-09-25 09:45:57 +00001073 __ InvokeCode(code, expected, arguments(),
1074 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001075
1076 // Handle call cache miss.
1077 __ bind(&miss);
1078 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
ager@chromium.org236ad962008-09-25 09:45:57 +00001079 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001080
1081 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001082 String* function_name = NULL;
1083 if (function->shared()->name()->IsString()) {
1084 function_name = String::cast(function->shared()->name());
1085 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001086 return GetCode(CONSTANT_FUNCTION, function_name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001087}
1088
1089
1090Object* CallStubCompiler::CompileCallInterceptor(Object* object,
1091 JSObject* holder,
1092 String* name) {
1093 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001094 // -- ecx : name
1095 // -- esp[0] : return address
1096 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1097 // -- ...
1098 // -- esp[(argc + 1) * 4] : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001099 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001100 Label miss;
1101
1102 // Get the number of arguments.
1103 const int argc = arguments().immediate();
1104
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001105 LookupResult lookup;
1106 LookupPostInterceptor(holder, name, &lookup);
1107
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001108 // Get the receiver from the stack.
1109 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001110
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001111 CallInterceptorCompiler compiler(arguments(), ecx);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001112 CompileLoadInterceptor(&compiler,
1113 this,
1114 masm(),
1115 JSObject::cast(object),
1116 holder,
1117 name,
1118 &lookup,
1119 edx,
1120 ebx,
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001121 edi,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001122 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001123
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001124 // Restore receiver.
1125 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001126
1127 // Check that the function really is a function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001128 __ test(eax, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001129 __ j(zero, &miss, not_taken);
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001130 __ CmpObjectType(eax, JS_FUNCTION_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001131 __ j(not_equal, &miss, not_taken);
1132
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001133 // Patch the receiver on the stack with the global proxy if
1134 // necessary.
1135 if (object->IsGlobalObject()) {
1136 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1137 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1138 }
1139
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001140 // Invoke the function.
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001141 __ mov(edi, eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001142 __ InvokeFunction(edi, arguments(), JUMP_FUNCTION);
1143
1144 // Handle load cache miss.
1145 __ bind(&miss);
1146 Handle<Code> ic = ComputeCallMiss(argc);
ager@chromium.org236ad962008-09-25 09:45:57 +00001147 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001148
1149 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001150 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001151}
1152
1153
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001154Object* CallStubCompiler::CompileCallGlobal(JSObject* object,
1155 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001156 JSGlobalPropertyCell* cell,
1157 JSFunction* function,
1158 String* name) {
1159 // ----------- S t a t e -------------
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001160 // -- ecx : name
1161 // -- esp[0] : return address
1162 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1163 // -- ...
1164 // -- esp[(argc + 1) * 4] : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001165 // -----------------------------------
1166 Label miss;
1167
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001168 // Get the number of arguments.
1169 const int argc = arguments().immediate();
1170
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001171 // Get the receiver from the stack.
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001172 __ mov(edx, Operand(esp, (argc + 1) * kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001173
1174 // If the object is the holder then we know that it's a global
1175 // object which can only happen for contextual calls. In this case,
1176 // the receiver cannot be a smi.
1177 if (object != holder) {
1178 __ test(edx, Immediate(kSmiTagMask));
1179 __ j(zero, &miss, not_taken);
1180 }
1181
1182 // Check that the maps haven't changed.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001183 CheckPrototypes(object, edx, holder, ebx, eax, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001184
1185 // Get the value from the cell.
1186 __ mov(edi, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1187 __ mov(edi, FieldOperand(edi, JSGlobalPropertyCell::kValueOffset));
1188
1189 // Check that the cell contains the same function.
sgjesse@chromium.org846fb742009-12-18 08:56:33 +00001190 if (Heap::InNewSpace(function)) {
1191 // We can't embed a pointer to a function in new space so we have
1192 // to verify that the shared function info is unchanged. This has
1193 // the nice side effect that multiple closures based on the same
1194 // function can all use this call IC. Before we load through the
1195 // function, we have to verify that it still is a function.
1196 __ test(edi, Immediate(kSmiTagMask));
1197 __ j(zero, &miss, not_taken);
1198 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ebx);
1199 __ j(not_equal, &miss, not_taken);
1200
1201 // Check the shared function info. Make sure it hasn't changed.
1202 __ cmp(FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset),
1203 Immediate(Handle<SharedFunctionInfo>(function->shared())));
1204 __ j(not_equal, &miss, not_taken);
1205 } else {
1206 __ cmp(Operand(edi), Immediate(Handle<JSFunction>(function)));
1207 __ j(not_equal, &miss, not_taken);
1208 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001209
1210 // Patch the receiver on the stack with the global proxy.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001211 if (object->IsGlobalObject()) {
1212 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalReceiverOffset));
1213 __ mov(Operand(esp, (argc + 1) * kPointerSize), edx);
1214 }
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001215
1216 // Setup the context (function already in edi).
1217 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1218
1219 // Jump to the cached code (tail call).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001220 __ IncrementCounter(&Counters::call_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001221 ASSERT(function->is_compiled());
1222 Handle<Code> code(function->code());
1223 ParameterCount expected(function->shared()->formal_parameter_count());
1224 __ InvokeCode(code, expected, arguments(),
1225 RelocInfo::CODE_TARGET, JUMP_FUNCTION);
1226
1227 // Handle call cache miss.
1228 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001229 __ IncrementCounter(&Counters::call_global_inline_miss, 1);
1230 Handle<Code> ic = ComputeCallMiss(arguments().immediate());
1231 __ jmp(ic, RelocInfo::CODE_TARGET);
1232
1233 // Return the generated code.
1234 return GetCode(NORMAL, name);
1235}
1236
1237
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001238Object* StoreStubCompiler::CompileStoreField(JSObject* object,
1239 int index,
1240 Map* transition,
1241 String* name) {
1242 // ----------- S t a t e -------------
1243 // -- eax : value
1244 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001245 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001246 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001247 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001248 Label miss;
1249
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001250 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001251 GenerateStoreField(masm(),
1252 Builtins::StoreIC_ExtendStorage,
1253 object,
1254 index,
1255 transition,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001256 edx, ecx, ebx,
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001257 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001258
1259 // Handle store cache miss.
1260 __ bind(&miss);
ager@chromium.org3bf7b912008-11-17 09:09:45 +00001261 __ mov(ecx, Immediate(Handle<String>(name))); // restore name
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001262 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001263 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001264
1265 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001266 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001267}
1268
1269
1270Object* StoreStubCompiler::CompileStoreCallback(JSObject* object,
1271 AccessorInfo* callback,
1272 String* name) {
1273 // ----------- S t a t e -------------
1274 // -- eax : value
1275 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001276 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001277 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001278 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001279 Label miss;
1280
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001281 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001282 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001283 __ j(zero, &miss, not_taken);
1284
1285 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001286 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001287 Immediate(Handle<Map>(object->map())));
1288 __ j(not_equal, &miss, not_taken);
1289
1290 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001291 if (object->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001292 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001293 }
1294
1295 // Stub never generated for non-global objects that require access
1296 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001297 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001298
1299 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001300 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001301 __ push(Immediate(Handle<AccessorInfo>(callback))); // callback info
1302 __ push(ecx); // name
1303 __ push(eax); // value
1304 __ push(ebx); // restore return address
1305
mads.s.ager31e71382008-08-13 09:32:07 +00001306 // Do tail-call to the runtime system.
1307 ExternalReference store_callback_property =
1308 ExternalReference(IC_Utility(IC::kStoreCallbackProperty));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001309 __ TailCallRuntime(store_callback_property, 4, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001310
1311 // Handle store cache miss.
1312 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001313 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001314 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001315
1316 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001317 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001318}
1319
1320
1321Object* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
1322 String* name) {
1323 // ----------- S t a t e -------------
1324 // -- eax : value
1325 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001326 // -- edx : receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001327 // -- esp[0] : return address
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001328 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001329 Label miss;
1330
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001331 // Check that the object isn't a smi.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001332 __ test(edx, Immediate(kSmiTagMask));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001333 __ j(zero, &miss, not_taken);
1334
1335 // Check that the map of the object hasn't changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001336 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001337 Immediate(Handle<Map>(receiver->map())));
1338 __ j(not_equal, &miss, not_taken);
1339
1340 // Perform global security token check if needed.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001341 if (receiver->IsJSGlobalProxy()) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001342 __ CheckAccessGlobalProxy(edx, ebx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001343 }
1344
1345 // Stub never generated for non-global objects that require access
1346 // checks.
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +00001347 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001348
1349 __ pop(ebx); // remove the return address
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001350 __ push(edx); // receiver
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001351 __ push(ecx); // name
1352 __ push(eax); // value
1353 __ push(ebx); // restore return address
1354
mads.s.ager31e71382008-08-13 09:32:07 +00001355 // Do tail-call to the runtime system.
1356 ExternalReference store_ic_property =
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001357 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty));
ager@chromium.orga1645e22009-09-09 19:27:10 +00001358 __ TailCallRuntime(store_ic_property, 3, 1);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001359
1360 // Handle store cache miss.
1361 __ bind(&miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001362 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001363 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001364
1365 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001366 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001367}
1368
1369
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001370Object* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
1371 JSGlobalPropertyCell* cell,
1372 String* name) {
1373 // ----------- S t a t e -------------
1374 // -- eax : value
1375 // -- ecx : name
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001376 // -- edx : receiver
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001377 // -- esp[0] : return address
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001378 // -----------------------------------
1379 Label miss;
1380
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001381 // Check that the map of the global has not changed.
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001382 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001383 Immediate(Handle<Map>(object->map())));
1384 __ j(not_equal, &miss, not_taken);
1385
1386 // Store the value in the cell.
1387 __ mov(ecx, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1388 __ mov(FieldOperand(ecx, JSGlobalPropertyCell::kValueOffset), eax);
1389
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001390 // Return the value (register eax).
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001391 __ IncrementCounter(&Counters::named_store_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001392 __ ret(0);
1393
1394 // Handle store cache miss.
1395 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001396 __ IncrementCounter(&Counters::named_store_global_inline_miss, 1);
1397 Handle<Code> ic(Builtins::builtin(Builtins::StoreIC_Miss));
1398 __ jmp(ic, RelocInfo::CODE_TARGET);
1399
1400 // Return the generated code.
1401 return GetCode(NORMAL, name);
1402}
1403
1404
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001405Object* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
1406 int index,
1407 Map* transition,
1408 String* name) {
1409 // ----------- S t a t e -------------
1410 // -- eax : value
1411 // -- esp[0] : return address
1412 // -- esp[4] : key
1413 // -- esp[8] : receiver
1414 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001415 Label miss;
1416
1417 __ IncrementCounter(&Counters::keyed_store_field, 1);
1418
1419 // Get the name from the stack.
1420 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1421 // Check that the name has not changed.
1422 __ cmp(Operand(ecx), Immediate(Handle<String>(name)));
1423 __ j(not_equal, &miss, not_taken);
1424
1425 // Get the object from the stack.
1426 __ mov(ebx, Operand(esp, 2 * kPointerSize));
1427
1428 // Generate store field code. Trashes the name register.
kasperl@chromium.org1accd572008-10-07 10:57:21 +00001429 GenerateStoreField(masm(),
1430 Builtins::KeyedStoreIC_ExtendStorage,
1431 object,
1432 index,
1433 transition,
1434 ebx, ecx, edx,
1435 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001436
1437 // Handle store cache miss.
1438 __ bind(&miss);
1439 __ DecrementCounter(&Counters::keyed_store_field, 1);
1440 Handle<Code> ic(Builtins::builtin(Builtins::KeyedStoreIC_Miss));
ager@chromium.org236ad962008-09-25 09:45:57 +00001441 __ jmp(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001442
1443 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001444 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001445}
1446
1447
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001448
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001449Object* LoadStubCompiler::CompileLoadField(JSObject* object,
1450 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001451 int index,
1452 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001453 // ----------- 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
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001460 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001461 GenerateLoadField(object, holder, eax, ebx, edx, index, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001462 __ bind(&miss);
1463 GenerateLoadMiss(masm(), Code::LOAD_IC);
1464
1465 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001466 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001467}
1468
1469
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001470Object* LoadStubCompiler::CompileLoadCallback(String* name,
1471 JSObject* object,
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001472 JSObject* holder,
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001473 AccessorInfo* callback) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001474 // ----------- S t a t e -------------
1475 // -- ecx : name
1476 // -- esp[0] : return address
1477 // -- esp[4] : receiver
1478 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001479 Label miss;
1480
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001481 __ mov(eax, Operand(esp, kPointerSize));
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001482 Failure* failure = Failure::InternalError();
1483 bool success = GenerateLoadCallback(object, holder, eax, ecx, ebx, edx,
1484 callback, name, &miss, &failure);
1485 if (!success) return failure;
1486
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001487 __ bind(&miss);
1488 GenerateLoadMiss(masm(), Code::LOAD_IC);
1489
1490 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001491 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001492}
1493
1494
1495Object* LoadStubCompiler::CompileLoadConstant(JSObject* object,
1496 JSObject* holder,
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001497 Object* value,
1498 String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001499 // ----------- S t a t e -------------
1500 // -- ecx : name
1501 // -- esp[0] : return address
1502 // -- esp[4] : receiver
1503 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001504 Label miss;
1505
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001506 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001507 GenerateLoadConstant(object, holder, eax, ebx, edx, value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001508 __ bind(&miss);
1509 GenerateLoadMiss(masm(), Code::LOAD_IC);
1510
1511 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001512 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001513}
1514
1515
1516Object* LoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1517 JSObject* holder,
1518 String* name) {
1519 // ----------- S t a t e -------------
1520 // -- ecx : name
1521 // -- esp[0] : return address
1522 // -- esp[4] : receiver
1523 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001524 Label miss;
1525
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001526 LookupResult lookup;
1527 LookupPostInterceptor(holder, name, &lookup);
1528
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001529 __ mov(eax, Operand(esp, kPointerSize));
ager@chromium.orge2902be2009-06-08 12:21:35 +00001530 // TODO(368): Compile in the whole chain: all the interceptors in
1531 // prototypes and ultimate answer.
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001532 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001533 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001534 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001535 eax,
1536 ecx,
1537 edx,
1538 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001539 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001540 &miss);
1541
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001542 __ bind(&miss);
1543 GenerateLoadMiss(masm(), Code::LOAD_IC);
1544
1545 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001546 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001547}
1548
1549
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001550Object* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
1551 GlobalObject* holder,
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001552 JSGlobalPropertyCell* cell,
1553 String* name,
1554 bool is_dont_delete) {
1555 // ----------- S t a t e -------------
1556 // -- ecx : name
1557 // -- esp[0] : return address
1558 // -- esp[4] : receiver
1559 // -----------------------------------
1560 Label miss;
1561
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001562 // Get the receiver from the stack.
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001563 __ mov(eax, Operand(esp, kPointerSize));
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001564
1565 // If the object is the holder then we know that it's a global
1566 // object which can only happen for contextual loads. In this case,
1567 // the receiver cannot be a smi.
1568 if (object != holder) {
1569 __ test(eax, Immediate(kSmiTagMask));
1570 __ j(zero, &miss, not_taken);
1571 }
1572
1573 // Check that the maps haven't changed.
1574 CheckPrototypes(object, eax, holder, ebx, edx, name, &miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001575
1576 // Get the value from the cell.
1577 __ mov(eax, Immediate(Handle<JSGlobalPropertyCell>(cell)));
1578 __ mov(eax, FieldOperand(eax, JSGlobalPropertyCell::kValueOffset));
1579
1580 // Check for deleted property if property can actually be deleted.
1581 if (!is_dont_delete) {
1582 __ cmp(eax, Factory::the_hole_value());
1583 __ j(equal, &miss, not_taken);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +00001584 } else if (FLAG_debug_code) {
1585 __ cmp(eax, Factory::the_hole_value());
1586 __ Check(not_equal, "DontDelete cells can't contain the hole");
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001587 }
1588
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001589 __ IncrementCounter(&Counters::named_load_global_inline, 1);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001590 __ ret(0);
1591
1592 __ bind(&miss);
kasperl@chromium.org2abc4502009-07-02 07:00:29 +00001593 __ IncrementCounter(&Counters::named_load_global_inline_miss, 1);
1594 GenerateLoadMiss(masm(), Code::LOAD_IC);
1595
1596 // Return the generated code.
1597 return GetCode(NORMAL, name);
1598}
1599
1600
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001601Object* KeyedLoadStubCompiler::CompileLoadField(String* name,
1602 JSObject* receiver,
1603 JSObject* holder,
1604 int index) {
1605 // ----------- S t a t e -------------
1606 // -- esp[0] : return address
1607 // -- esp[4] : name
1608 // -- esp[8] : receiver
1609 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001610 Label miss;
1611
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001612 __ mov(eax, Operand(esp, kPointerSize));
1613 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001614 __ IncrementCounter(&Counters::keyed_load_field, 1);
1615
1616 // Check that the name has not changed.
1617 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1618 __ j(not_equal, &miss, not_taken);
1619
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001620 GenerateLoadField(receiver, holder, ecx, ebx, edx, index, name, &miss);
1621
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001622 __ bind(&miss);
1623 __ DecrementCounter(&Counters::keyed_load_field, 1);
1624 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1625
1626 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001627 return GetCode(FIELD, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001628}
1629
1630
1631Object* KeyedLoadStubCompiler::CompileLoadCallback(String* name,
1632 JSObject* receiver,
1633 JSObject* holder,
1634 AccessorInfo* callback) {
1635 // ----------- S t a t e -------------
1636 // -- esp[0] : return address
1637 // -- esp[4] : name
1638 // -- esp[8] : receiver
1639 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001640 Label miss;
1641
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001642 __ mov(eax, Operand(esp, kPointerSize));
1643 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001644 __ IncrementCounter(&Counters::keyed_load_callback, 1);
1645
1646 // Check that the name has not changed.
1647 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1648 __ j(not_equal, &miss, not_taken);
1649
kmillikin@chromium.org13bd2942009-12-16 15:36:05 +00001650 Failure* failure = Failure::InternalError();
1651 bool success = GenerateLoadCallback(receiver, holder, ecx, eax, ebx, edx,
1652 callback, name, &miss, &failure);
1653 if (!success) return failure;
1654
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001655 __ bind(&miss);
1656 __ DecrementCounter(&Counters::keyed_load_callback, 1);
1657 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1658
1659 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001660 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001661}
1662
1663
1664Object* KeyedLoadStubCompiler::CompileLoadConstant(String* name,
1665 JSObject* receiver,
1666 JSObject* holder,
1667 Object* value) {
1668 // ----------- S t a t e -------------
1669 // -- esp[0] : return address
1670 // -- esp[4] : name
1671 // -- esp[8] : receiver
1672 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001673 Label miss;
1674
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001675 __ mov(eax, Operand(esp, kPointerSize));
1676 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001677 __ IncrementCounter(&Counters::keyed_load_constant_function, 1);
1678
1679 // Check that the name has not changed.
1680 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1681 __ j(not_equal, &miss, not_taken);
1682
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001683 GenerateLoadConstant(receiver, holder, ecx, ebx, edx,
1684 value, name, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001685 __ bind(&miss);
1686 __ DecrementCounter(&Counters::keyed_load_constant_function, 1);
1687 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1688
1689 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001690 return GetCode(CONSTANT_FUNCTION, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001691}
1692
1693
1694Object* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
1695 JSObject* holder,
1696 String* name) {
1697 // ----------- S t a t e -------------
1698 // -- esp[0] : return address
1699 // -- esp[4] : name
1700 // -- esp[8] : receiver
1701 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001702 Label miss;
1703
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001704 __ mov(eax, Operand(esp, kPointerSize));
1705 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001706 __ IncrementCounter(&Counters::keyed_load_interceptor, 1);
1707
1708 // Check that the name has not changed.
1709 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1710 __ j(not_equal, &miss, not_taken);
1711
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001712 LookupResult lookup;
1713 LookupPostInterceptor(holder, name, &lookup);
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001714 GenerateLoadInterceptor(receiver,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001715 holder,
sgjesse@chromium.org0b6db592009-07-30 14:48:31 +00001716 &lookup,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001717 ecx,
1718 eax,
1719 edx,
1720 ebx,
kasperl@chromium.orgdefbd102009-07-13 14:04:26 +00001721 name,
ager@chromium.orge2902be2009-06-08 12:21:35 +00001722 &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001723 __ bind(&miss);
1724 __ DecrementCounter(&Counters::keyed_load_interceptor, 1);
1725 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1726
1727 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001728 return GetCode(INTERCEPTOR, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001729}
1730
1731
1732
1733
1734Object* KeyedLoadStubCompiler::CompileLoadArrayLength(String* name) {
1735 // ----------- S t a t e -------------
1736 // -- esp[0] : return address
1737 // -- esp[4] : name
1738 // -- esp[8] : receiver
1739 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001740 Label miss;
1741
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001742 __ mov(eax, Operand(esp, kPointerSize));
1743 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001744 __ IncrementCounter(&Counters::keyed_load_array_length, 1);
1745
1746 // Check that the name has not changed.
1747 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1748 __ j(not_equal, &miss, not_taken);
1749
1750 GenerateLoadArrayLength(masm(), ecx, edx, &miss);
1751 __ bind(&miss);
1752 __ DecrementCounter(&Counters::keyed_load_array_length, 1);
1753 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1754
1755 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001756 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001757}
1758
1759
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001760Object* KeyedLoadStubCompiler::CompileLoadStringLength(String* name) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001761 // ----------- S t a t e -------------
1762 // -- esp[0] : return address
1763 // -- esp[4] : name
1764 // -- esp[8] : receiver
1765 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001766 Label miss;
1767
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001768 __ mov(eax, Operand(esp, kPointerSize));
1769 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001770 __ IncrementCounter(&Counters::keyed_load_string_length, 1);
1771
1772 // Check that the name has not changed.
1773 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1774 __ j(not_equal, &miss, not_taken);
1775
kasperl@chromium.org9fe21c62008-10-28 08:53:51 +00001776 GenerateLoadStringLength(masm(), ecx, edx, &miss);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001777 __ bind(&miss);
1778 __ DecrementCounter(&Counters::keyed_load_string_length, 1);
1779 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1780
1781 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001782 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001783}
1784
1785
1786Object* KeyedLoadStubCompiler::CompileLoadFunctionPrototype(String* name) {
1787 // ----------- S t a t e -------------
1788 // -- esp[0] : return address
1789 // -- esp[4] : name
1790 // -- esp[8] : receiver
1791 // -----------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001792 Label miss;
1793
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001794 __ mov(eax, Operand(esp, kPointerSize));
1795 __ mov(ecx, Operand(esp, 2 * kPointerSize));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001796 __ IncrementCounter(&Counters::keyed_load_function_prototype, 1);
1797
1798 // Check that the name has not changed.
1799 __ cmp(Operand(eax), Immediate(Handle<String>(name)));
1800 __ j(not_equal, &miss, not_taken);
1801
1802 GenerateLoadFunctionPrototype(masm(), ecx, edx, ebx, &miss);
1803 __ bind(&miss);
1804 __ DecrementCounter(&Counters::keyed_load_function_prototype, 1);
1805 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
1806
1807 // Return the generated code.
kasperl@chromium.org7be3c992009-03-12 07:19:55 +00001808 return GetCode(CALLBACKS, name);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001809}
1810
1811
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001812// Specialized stub for constructing objects from functions which only have only
1813// simple assignments of the form this.x = ...; in their body.
1814Object* ConstructStubCompiler::CompileConstructStub(
1815 SharedFunctionInfo* shared) {
1816 // ----------- S t a t e -------------
1817 // -- eax : argc
1818 // -- edi : constructor
1819 // -- esp[0] : return address
1820 // -- esp[4] : last argument
1821 // -----------------------------------
1822 Label generic_stub_call;
1823#ifdef ENABLE_DEBUGGER_SUPPORT
1824 // Check to see whether there are any break points in the function code. If
1825 // there are jump to the generic constructor stub which calls the actual
1826 // code for the function thereby hitting the break points.
1827 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1828 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kDebugInfoOffset));
1829 __ cmp(ebx, Factory::undefined_value());
1830 __ j(not_equal, &generic_stub_call, not_taken);
1831#endif
1832
1833 // Load the initial map and verify that it is in fact a map.
1834 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1835 // Will both indicate a NULL and a Smi.
1836 __ test(ebx, Immediate(kSmiTagMask));
1837 __ j(zero, &generic_stub_call);
1838 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1839 __ j(not_equal, &generic_stub_call);
1840
1841#ifdef DEBUG
1842 // Cannot construct functions this way.
1843 // edi: constructor
1844 // ebx: initial map
1845 __ CmpInstanceType(ebx, JS_FUNCTION_TYPE);
1846 __ Assert(not_equal, "Function constructed by construct stub.");
1847#endif
1848
1849 // Now allocate the JSObject on the heap by moving the new space allocation
1850 // top forward.
1851 // edi: constructor
1852 // ebx: initial map
1853 __ movzx_b(ecx, FieldOperand(ebx, Map::kInstanceSizeOffset));
1854 __ shl(ecx, kPointerSizeLog2);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001855 __ AllocateInNewSpace(ecx,
1856 edx,
1857 ecx,
1858 no_reg,
1859 &generic_stub_call,
1860 NO_ALLOCATION_FLAGS);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001861
1862 // Allocated the JSObject, now initialize the fields and add the heap tag.
1863 // ebx: initial map
1864 // edx: JSObject (untagged)
1865 __ mov(Operand(edx, JSObject::kMapOffset), ebx);
1866 __ mov(ebx, Factory::empty_fixed_array());
1867 __ mov(Operand(edx, JSObject::kPropertiesOffset), ebx);
1868 __ mov(Operand(edx, JSObject::kElementsOffset), ebx);
1869
1870 // Push the allocated object to the stack. This is the object that will be
1871 // returned (after it is tagged).
1872 __ push(edx);
1873
1874 // eax: argc
1875 // edx: JSObject (untagged)
1876 // Load the address of the first in-object property into edx.
1877 __ lea(edx, Operand(edx, JSObject::kHeaderSize));
1878 // Calculate the location of the first argument. The stack contains the
1879 // allocated object and the return address on top of the argc arguments.
1880 __ lea(ecx, Operand(esp, eax, times_4, 1 * kPointerSize));
1881
1882 // Use edi for holding undefined which is used in several places below.
1883 __ mov(edi, Factory::undefined_value());
1884
1885 // eax: argc
1886 // ecx: first argument
1887 // edx: first in-object property of the JSObject
1888 // edi: undefined
1889 // Fill the initialized properties with a constant value or a passed argument
1890 // depending on the this.x = ...; assignment in the function.
1891 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
1892 if (shared->IsThisPropertyAssignmentArgument(i)) {
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001893 // Check if the argument assigned to the property is actually passed.
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001894 // If argument is not passed the property is set to undefined,
1895 // otherwise find it on the stack.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001896 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001897 __ mov(ebx, edi);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001898 __ cmp(eax, arg_number);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +00001899 if (CpuFeatures::IsSupported(CMOV)) {
1900 CpuFeatures::Scope use_cmov(CMOV);
1901 __ cmov(above, ebx, Operand(ecx, arg_number * -kPointerSize));
1902 } else {
1903 Label not_passed;
1904 __ j(below_equal, &not_passed);
1905 __ mov(ebx, Operand(ecx, arg_number * -kPointerSize));
1906 __ bind(&not_passed);
1907 }
1908 // Store value in the property.
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001909 __ mov(Operand(edx, i * kPointerSize), ebx);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001910 } else {
1911 // Set the property to the constant value.
1912 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
1913 __ mov(Operand(edx, i * kPointerSize), Immediate(constant));
1914 }
1915 }
1916
1917 // Fill the unused in-object property fields with undefined.
1918 for (int i = shared->this_property_assignments_count();
1919 i < shared->CalculateInObjectProperties();
1920 i++) {
1921 __ mov(Operand(edx, i * kPointerSize), edi);
1922 }
1923
1924 // Move argc to ebx and retrieve and tag the JSObject to return.
1925 __ mov(ebx, eax);
1926 __ pop(eax);
1927 __ or_(Operand(eax), Immediate(kHeapObjectTag));
1928
1929 // Remove caller arguments and receiver from the stack and return.
1930 __ pop(ecx);
1931 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
1932 __ push(ecx);
1933 __ IncrementCounter(&Counters::constructed_objects, 1);
1934 __ IncrementCounter(&Counters::constructed_objects_stub, 1);
1935 __ ret(0);
1936
1937 // Jump to the generic stub in case the specialized code cannot handle the
1938 // construction.
1939 __ bind(&generic_stub_call);
1940 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
1941 Handle<Code> generic_construct_stub(code);
1942 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
1943
1944 // Return the generated code.
1945 return GetCode();
1946}
1947
1948
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001949#undef __
1950
1951} } // namespace v8::internal