blob: 9f94b1d798386a082b493c81e450d5a5fd36cdde [file] [log] [blame]
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001// Copyright 2011 the V8 project authors. All rights reserved.
ager@chromium.org5c838252010-02-19 08:53:10 +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
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000030#if defined(V8_TARGET_ARCH_MIPS)
31
ager@chromium.org5c838252010-02-19 08:53:10 +000032#include "ic-inl.h"
karlklose@chromium.org83a47282011-05-11 11:54:09 +000033#include "codegen.h"
ager@chromium.org5c838252010-02-19 08:53:10 +000034#include "stub-cache.h"
35
36namespace v8 {
37namespace internal {
38
39#define __ ACCESS_MASM(masm)
40
41
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +000042static void ProbeTable(Isolate* isolate,
43 MacroAssembler* masm,
44 Code::Flags flags,
45 StubCache::Table table,
46 Register name,
47 Register offset,
48 Register scratch,
49 Register scratch2) {
50 ExternalReference key_offset(isolate->stub_cache()->key_reference(table));
51 ExternalReference value_offset(isolate->stub_cache()->value_reference(table));
52
53 uint32_t key_off_addr = reinterpret_cast<uint32_t>(key_offset.address());
54 uint32_t value_off_addr = reinterpret_cast<uint32_t>(value_offset.address());
55
56 // Check the relative positions of the address fields.
57 ASSERT(value_off_addr > key_off_addr);
58 ASSERT((value_off_addr - key_off_addr) % 4 == 0);
59 ASSERT((value_off_addr - key_off_addr) < (256 * 4));
60
61 Label miss;
62 Register offsets_base_addr = scratch;
63
64 // Check that the key in the entry matches the name.
65 __ li(offsets_base_addr, Operand(key_offset));
66 __ sll(scratch2, offset, 1);
67 __ addu(scratch2, offsets_base_addr, scratch2);
68 __ lw(scratch2, MemOperand(scratch2));
69 __ Branch(&miss, ne, name, Operand(scratch2));
70
71 // Get the code entry from the cache.
72 __ Addu(offsets_base_addr, offsets_base_addr,
73 Operand(value_off_addr - key_off_addr));
74 __ sll(scratch2, offset, 1);
75 __ addu(scratch2, offsets_base_addr, scratch2);
76 __ lw(scratch2, MemOperand(scratch2));
77
78 // Check that the flags match what we're looking for.
79 __ lw(scratch2, FieldMemOperand(scratch2, Code::kFlagsOffset));
80 __ And(scratch2, scratch2, Operand(~Code::kFlagsNotUsedInLookup));
81 __ Branch(&miss, ne, scratch2, Operand(flags));
82
83 // Re-load code entry from cache.
84 __ sll(offset, offset, 1);
85 __ addu(offset, offset, offsets_base_addr);
86 __ lw(offset, MemOperand(offset));
87
88 // Jump to the first instruction in the code stub.
89 __ Addu(offset, offset, Operand(Code::kHeaderSize - kHeapObjectTag));
90 __ Jump(offset);
91
92 // Miss: fall through.
93 __ bind(&miss);
94}
95
96
97// Helper function used to check that the dictionary doesn't contain
98// the property. This function may return false negatives, so miss_label
99// must always call a backup property check that is complete.
100// This function is safe to call if the receiver has fast properties.
101// Name must be a symbol and receiver must be a heap object.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000102static void GenerateDictionaryNegativeLookup(MacroAssembler* masm,
103 Label* miss_label,
104 Register receiver,
105 Handle<String> name,
106 Register scratch0,
107 Register scratch1) {
108 ASSERT(name->IsSymbol());
109 Counters* counters = masm->isolate()->counters();
110 __ IncrementCounter(counters->negative_lookups(), 1, scratch0, scratch1);
111 __ IncrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
112
113 Label done;
114
115 const int kInterceptorOrAccessCheckNeededMask =
116 (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
117
118 // Bail out if the receiver has a named interceptor or requires access checks.
119 Register map = scratch1;
120 __ lw(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
121 __ lbu(scratch0, FieldMemOperand(map, Map::kBitFieldOffset));
122 __ And(scratch0, scratch0, Operand(kInterceptorOrAccessCheckNeededMask));
123 __ Branch(miss_label, ne, scratch0, Operand(zero_reg));
124
125 // Check that receiver is a JSObject.
126 __ lbu(scratch0, FieldMemOperand(map, Map::kInstanceTypeOffset));
127 __ Branch(miss_label, lt, scratch0, Operand(FIRST_SPEC_OBJECT_TYPE));
128
129 // Load properties array.
130 Register properties = scratch0;
131 __ lw(properties, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
132 // Check that the properties array is a dictionary.
133 __ lw(map, FieldMemOperand(properties, HeapObject::kMapOffset));
134 Register tmp = properties;
135 __ LoadRoot(tmp, Heap::kHashTableMapRootIndex);
136 __ Branch(miss_label, ne, map, Operand(tmp));
137
138 // Restore the temporarily used register.
139 __ lw(properties, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
140
141
142 StringDictionaryLookupStub::GenerateNegativeLookup(masm,
143 miss_label,
144 &done,
145 receiver,
146 properties,
147 name,
148 scratch1);
149 __ bind(&done);
150 __ DecrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
151}
152
153
154// TODO(kmillikin): Eliminate this function when the stub cache is fully
155// handlified.
156MUST_USE_RESULT static MaybeObject* TryGenerateDictionaryNegativeLookup(
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000157 MacroAssembler* masm,
158 Label* miss_label,
159 Register receiver,
160 String* name,
161 Register scratch0,
162 Register scratch1) {
163 ASSERT(name->IsSymbol());
164 Counters* counters = masm->isolate()->counters();
165 __ IncrementCounter(counters->negative_lookups(), 1, scratch0, scratch1);
166 __ IncrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
167
168 Label done;
169
170 const int kInterceptorOrAccessCheckNeededMask =
171 (1 << Map::kHasNamedInterceptor) | (1 << Map::kIsAccessCheckNeeded);
172
173 // Bail out if the receiver has a named interceptor or requires access checks.
174 Register map = scratch1;
175 __ lw(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
176 __ lbu(scratch0, FieldMemOperand(map, Map::kBitFieldOffset));
177 __ And(at, scratch0, Operand(kInterceptorOrAccessCheckNeededMask));
178 __ Branch(miss_label, ne, at, Operand(zero_reg));
179
180
181 // Check that receiver is a JSObject.
182 __ lbu(scratch0, FieldMemOperand(map, Map::kInstanceTypeOffset));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000183 __ Branch(miss_label, lt, scratch0, Operand(FIRST_SPEC_OBJECT_TYPE));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000184
185 // Load properties array.
186 Register properties = scratch0;
187 __ lw(properties, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
188 // Check that the properties array is a dictionary.
189 __ lw(map, FieldMemOperand(properties, HeapObject::kMapOffset));
190 Register tmp = properties;
191 __ LoadRoot(tmp, Heap::kHashTableMapRootIndex);
192 __ Branch(miss_label, ne, map, Operand(tmp));
193
194 // Restore the temporarily used register.
195 __ lw(properties, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
196
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000197 MaybeObject* result = StringDictionaryLookupStub::TryGenerateNegativeLookup(
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000198 masm,
199 miss_label,
200 &done,
201 receiver,
202 properties,
203 name,
204 scratch1);
205 if (result->IsFailure()) return result;
206
207 __ bind(&done);
208 __ DecrementCounter(counters->negative_lookups_miss(), 1, scratch0, scratch1);
209
210 return result;
211}
212
213
ager@chromium.org5c838252010-02-19 08:53:10 +0000214void StubCache::GenerateProbe(MacroAssembler* masm,
215 Code::Flags flags,
216 Register receiver,
217 Register name,
218 Register scratch,
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000219 Register extra,
220 Register extra2) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000221 Isolate* isolate = masm->isolate();
222 Label miss;
223
224 // Make sure that code is valid. The shifting code relies on the
225 // entry size being 8.
226 ASSERT(sizeof(Entry) == 8);
227
228 // Make sure the flags does not name a specific type.
229 ASSERT(Code::ExtractTypeFromFlags(flags) == 0);
230
231 // Make sure that there are no register conflicts.
232 ASSERT(!scratch.is(receiver));
233 ASSERT(!scratch.is(name));
234 ASSERT(!extra.is(receiver));
235 ASSERT(!extra.is(name));
236 ASSERT(!extra.is(scratch));
237 ASSERT(!extra2.is(receiver));
238 ASSERT(!extra2.is(name));
239 ASSERT(!extra2.is(scratch));
240 ASSERT(!extra2.is(extra));
241
242 // Check scratch, extra and extra2 registers are valid.
243 ASSERT(!scratch.is(no_reg));
244 ASSERT(!extra.is(no_reg));
245 ASSERT(!extra2.is(no_reg));
246
247 // Check that the receiver isn't a smi.
248 __ JumpIfSmi(receiver, &miss, t0);
249
250 // Get the map of the receiver and compute the hash.
251 __ lw(scratch, FieldMemOperand(name, String::kHashFieldOffset));
252 __ lw(t8, FieldMemOperand(receiver, HeapObject::kMapOffset));
253 __ Addu(scratch, scratch, Operand(t8));
254 __ Xor(scratch, scratch, Operand(flags));
255 __ And(scratch,
256 scratch,
257 Operand((kPrimaryTableSize - 1) << kHeapObjectTagSize));
258
259 // Probe the primary table.
260 ProbeTable(isolate, masm, flags, kPrimary, name, scratch, extra, extra2);
261
262 // Primary miss: Compute hash for secondary probe.
263 __ Subu(scratch, scratch, Operand(name));
264 __ Addu(scratch, scratch, Operand(flags));
265 __ And(scratch,
266 scratch,
267 Operand((kSecondaryTableSize - 1) << kHeapObjectTagSize));
268
269 // Probe the secondary table.
270 ProbeTable(isolate, masm, flags, kSecondary, name, scratch, extra, extra2);
271
272 // Cache miss: Fall-through and let caller handle the miss by
273 // entering the runtime system.
274 __ bind(&miss);
ager@chromium.org5c838252010-02-19 08:53:10 +0000275}
276
277
278void StubCompiler::GenerateLoadGlobalFunctionPrototype(MacroAssembler* masm,
279 int index,
280 Register prototype) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000281 // Load the global or builtins object from the current context.
282 __ lw(prototype, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
283 // Load the global context from the global or builtins object.
284 __ lw(prototype,
285 FieldMemOperand(prototype, GlobalObject::kGlobalContextOffset));
286 // Load the function from the global context.
287 __ lw(prototype, MemOperand(prototype, Context::SlotOffset(index)));
288 // Load the initial map. The global functions all have initial maps.
289 __ lw(prototype,
290 FieldMemOperand(prototype, JSFunction::kPrototypeOrInitialMapOffset));
291 // Load the prototype from the initial map.
292 __ lw(prototype, FieldMemOperand(prototype, Map::kPrototypeOffset));
ager@chromium.org5c838252010-02-19 08:53:10 +0000293}
294
295
lrn@chromium.org7516f052011-03-30 08:52:27 +0000296void StubCompiler::GenerateDirectLoadGlobalFunctionPrototype(
297 MacroAssembler* masm, int index, Register prototype, Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000298 Isolate* isolate = masm->isolate();
299 // Check we're still in the same context.
300 __ lw(prototype, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
301 ASSERT(!prototype.is(at));
302 __ li(at, isolate->global());
303 __ Branch(miss, ne, prototype, Operand(at));
304 // Get the global function with the given index.
305 JSFunction* function =
306 JSFunction::cast(isolate->global_context()->get(index));
307 // Load its initial map. The global functions all have initial maps.
308 __ li(prototype, Handle<Map>(function->initial_map()));
309 // Load the prototype from the initial map.
310 __ lw(prototype, FieldMemOperand(prototype, Map::kPrototypeOffset));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000311}
312
313
ager@chromium.org5c838252010-02-19 08:53:10 +0000314// Load a fast property out of a holder object (src). In-object properties
315// are loaded directly otherwise the property is loaded from the properties
316// fixed array.
317void StubCompiler::GenerateFastPropertyLoad(MacroAssembler* masm,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000318 Register dst,
319 Register src,
320 Handle<JSObject> holder,
321 int index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000322 // Adjust for the number of properties stored in the holder.
323 index -= holder->map()->inobject_properties();
324 if (index < 0) {
325 // Get the property straight out of the holder.
326 int offset = holder->map()->instance_size() + (index * kPointerSize);
327 __ lw(dst, FieldMemOperand(src, offset));
328 } else {
329 // Calculate the offset into the properties array.
330 int offset = index * kPointerSize + FixedArray::kHeaderSize;
331 __ lw(dst, FieldMemOperand(src, JSObject::kPropertiesOffset));
332 __ lw(dst, FieldMemOperand(dst, offset));
333 }
ager@chromium.org5c838252010-02-19 08:53:10 +0000334}
335
336
337void StubCompiler::GenerateLoadArrayLength(MacroAssembler* masm,
338 Register receiver,
339 Register scratch,
340 Label* miss_label) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000341 // Check that the receiver isn't a smi.
342 __ And(scratch, receiver, Operand(kSmiTagMask));
343 __ Branch(miss_label, eq, scratch, Operand(zero_reg));
344
345 // Check that the object is a JS array.
346 __ GetObjectType(receiver, scratch, scratch);
347 __ Branch(miss_label, ne, scratch, Operand(JS_ARRAY_TYPE));
348
349 // Load length directly from the JS array.
350 __ lw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
351 __ Ret();
352}
353
354
355// Generate code to check if an object is a string. If the object is a
356// heap object, its map's instance type is left in the scratch1 register.
357// If this is not needed, scratch1 and scratch2 may be the same register.
358static void GenerateStringCheck(MacroAssembler* masm,
359 Register receiver,
360 Register scratch1,
361 Register scratch2,
362 Label* smi,
363 Label* non_string_object) {
364 // Check that the receiver isn't a smi.
365 __ JumpIfSmi(receiver, smi, t0);
366
367 // Check that the object is a string.
368 __ lw(scratch1, FieldMemOperand(receiver, HeapObject::kMapOffset));
369 __ lbu(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
370 __ And(scratch2, scratch1, Operand(kIsNotStringMask));
371 // The cast is to resolve the overload for the argument of 0x0.
372 __ Branch(non_string_object,
373 ne,
374 scratch2,
375 Operand(static_cast<int32_t>(kStringTag)));
ager@chromium.org5c838252010-02-19 08:53:10 +0000376}
377
378
lrn@chromium.org7516f052011-03-30 08:52:27 +0000379// Generate code to load the length from a string object and return the length.
380// If the receiver object is not a string or a wrapped string object the
381// execution continues at the miss label. The register containing the
382// receiver is potentially clobbered.
383void StubCompiler::GenerateLoadStringLength(MacroAssembler* masm,
384 Register receiver,
385 Register scratch1,
386 Register scratch2,
387 Label* miss,
388 bool support_wrappers) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000389 Label check_wrapper;
390
391 // Check if the object is a string leaving the instance type in the
392 // scratch1 register.
393 GenerateStringCheck(masm, receiver, scratch1, scratch2, miss,
394 support_wrappers ? &check_wrapper : miss);
395
396 // Load length directly from the string.
397 __ lw(v0, FieldMemOperand(receiver, String::kLengthOffset));
398 __ Ret();
399
400 if (support_wrappers) {
401 // Check if the object is a JSValue wrapper.
402 __ bind(&check_wrapper);
403 __ Branch(miss, ne, scratch1, Operand(JS_VALUE_TYPE));
404
405 // Unwrap the value and check if the wrapped value is a string.
406 __ lw(scratch1, FieldMemOperand(receiver, JSValue::kValueOffset));
407 GenerateStringCheck(masm, scratch1, scratch2, scratch2, miss, miss);
408 __ lw(v0, FieldMemOperand(scratch1, String::kLengthOffset));
409 __ Ret();
410 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000411}
412
413
ager@chromium.org5c838252010-02-19 08:53:10 +0000414void StubCompiler::GenerateLoadFunctionPrototype(MacroAssembler* masm,
415 Register receiver,
416 Register scratch1,
417 Register scratch2,
418 Label* miss_label) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000419 __ TryGetFunctionPrototype(receiver, scratch1, scratch2, miss_label);
420 __ mov(v0, scratch1);
421 __ Ret();
ager@chromium.org5c838252010-02-19 08:53:10 +0000422}
423
424
lrn@chromium.org7516f052011-03-30 08:52:27 +0000425// Generate StoreField code, value is passed in a0 register.
ager@chromium.org5c838252010-02-19 08:53:10 +0000426// After executing generated code, the receiver_reg and name_reg
427// may be clobbered.
428void StubCompiler::GenerateStoreField(MacroAssembler* masm,
ager@chromium.org5c838252010-02-19 08:53:10 +0000429 JSObject* object,
430 int index,
431 Map* transition,
432 Register receiver_reg,
433 Register name_reg,
434 Register scratch,
435 Label* miss_label) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000436 // a0 : value.
437 Label exit;
438
439 // Check that the receiver isn't a smi.
440 __ JumpIfSmi(receiver_reg, miss_label, scratch);
441
442 // Check that the map of the receiver hasn't changed.
443 __ lw(scratch, FieldMemOperand(receiver_reg, HeapObject::kMapOffset));
444 __ Branch(miss_label, ne, scratch, Operand(Handle<Map>(object->map())));
445
446 // Perform global security token check if needed.
447 if (object->IsJSGlobalProxy()) {
448 __ CheckAccessGlobalProxy(receiver_reg, scratch, miss_label);
449 }
450
451 // Stub never generated for non-global objects that require access
452 // checks.
453 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
454
455 // Perform map transition for the receiver if necessary.
456 if ((transition != NULL) && (object->map()->unused_property_fields() == 0)) {
457 // The properties must be extended before we can store the value.
458 // We jump to a runtime call that extends the properties array.
459 __ push(receiver_reg);
460 __ li(a2, Operand(Handle<Map>(transition)));
461 __ Push(a2, a0);
462 __ TailCallExternalReference(
463 ExternalReference(IC_Utility(IC::kSharedStoreIC_ExtendStorage),
464 masm->isolate()),
465 3, 1);
466 return;
467 }
468
469 if (transition != NULL) {
470 // Update the map of the object; no write barrier updating is
471 // needed because the map is never in new space.
472 __ li(t0, Operand(Handle<Map>(transition)));
473 __ sw(t0, FieldMemOperand(receiver_reg, HeapObject::kMapOffset));
474 }
475
476 // Adjust for the number of properties stored in the object. Even in the
477 // face of a transition we can use the old map here because the size of the
478 // object and the number of in-object properties is not going to change.
479 index -= object->map()->inobject_properties();
480
481 if (index < 0) {
482 // Set the property straight into the object.
483 int offset = object->map()->instance_size() + (index * kPointerSize);
484 __ sw(a0, FieldMemOperand(receiver_reg, offset));
485
486 // Skip updating write barrier if storing a smi.
487 __ JumpIfSmi(a0, &exit, scratch);
488
489 // Update the write barrier for the array address.
490 // Pass the now unused name_reg as a scratch register.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000491 __ mov(name_reg, a0);
492 __ RecordWriteField(receiver_reg,
493 offset,
494 name_reg,
495 scratch,
496 kRAHasNotBeenSaved,
497 kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000498 } else {
499 // Write to the properties array.
500 int offset = index * kPointerSize + FixedArray::kHeaderSize;
501 // Get the properties array.
502 __ lw(scratch, FieldMemOperand(receiver_reg, JSObject::kPropertiesOffset));
503 __ sw(a0, FieldMemOperand(scratch, offset));
504
505 // Skip updating write barrier if storing a smi.
506 __ JumpIfSmi(a0, &exit);
507
508 // Update the write barrier for the array address.
509 // Ok to clobber receiver_reg and name_reg, since we return.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000510 __ mov(name_reg, a0);
511 __ RecordWriteField(scratch,
512 offset,
513 name_reg,
514 receiver_reg,
515 kRAHasNotBeenSaved,
516 kDontSaveFPRegs);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000517 }
518
519 // Return the value (register v0).
520 __ bind(&exit);
521 __ mov(v0, a0);
522 __ Ret();
ager@chromium.org5c838252010-02-19 08:53:10 +0000523}
524
525
526void StubCompiler::GenerateLoadMiss(MacroAssembler* masm, Code::Kind kind) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000527 ASSERT(kind == Code::LOAD_IC || kind == Code::KEYED_LOAD_IC);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000528 Handle<Code> code = (kind == Code::LOAD_IC)
529 ? masm->isolate()->builtins()->LoadIC_Miss()
530 : masm->isolate()->builtins()->KeyedLoadIC_Miss();
531 __ Jump(code, RelocInfo::CODE_TARGET);
ager@chromium.org5c838252010-02-19 08:53:10 +0000532}
533
534
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000535static void GenerateCallFunction(MacroAssembler* masm,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000536 Handle<Object> object,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000537 const ParameterCount& arguments,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000538 Label* miss,
539 Code::ExtraICState extra_ic_state) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000540 // ----------- S t a t e -------------
541 // -- a0: receiver
542 // -- a1: function to call
543 // -----------------------------------
544 // Check that the function really is a function.
545 __ JumpIfSmi(a1, miss);
546 __ GetObjectType(a1, a3, a3);
547 __ Branch(miss, ne, a3, Operand(JS_FUNCTION_TYPE));
548
549 // Patch the receiver on the stack with the global proxy if
550 // necessary.
551 if (object->IsGlobalObject()) {
552 __ lw(a3, FieldMemOperand(a0, GlobalObject::kGlobalReceiverOffset));
553 __ sw(a3, MemOperand(sp, arguments.immediate() * kPointerSize));
554 }
555
556 // Invoke the function.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000557 CallKind call_kind = CallICBase::Contextual::decode(extra_ic_state)
558 ? CALL_AS_FUNCTION
559 : CALL_AS_METHOD;
560 __ InvokeFunction(a1, arguments, JUMP_FUNCTION, NullCallWrapper(), call_kind);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000561}
562
563
564static void PushInterceptorArguments(MacroAssembler* masm,
565 Register receiver,
566 Register holder,
567 Register name,
568 JSObject* holder_obj) {
569 __ push(name);
570 InterceptorInfo* interceptor = holder_obj->GetNamedInterceptor();
571 ASSERT(!masm->isolate()->heap()->InNewSpace(interceptor));
572 Register scratch = name;
573 __ li(scratch, Operand(Handle<Object>(interceptor)));
574 __ Push(scratch, receiver, holder);
575 __ lw(scratch, FieldMemOperand(scratch, InterceptorInfo::kDataOffset));
576 __ push(scratch);
577}
578
579
580static void CompileCallLoadPropertyWithInterceptor(MacroAssembler* masm,
581 Register receiver,
582 Register holder,
583 Register name,
584 JSObject* holder_obj) {
585 PushInterceptorArguments(masm, receiver, holder, name, holder_obj);
586
587 ExternalReference ref =
588 ExternalReference(IC_Utility(IC::kLoadPropertyWithInterceptorOnly),
589 masm->isolate());
590 __ li(a0, Operand(5));
591 __ li(a1, Operand(ref));
592
593 CEntryStub stub(1);
594 __ CallStub(&stub);
595}
596
597
598static const int kFastApiCallArguments = 3;
599
600
601// Reserves space for the extra arguments to FastHandleApiCall in the
602// caller's frame.
603//
604// These arguments are set by CheckPrototypes and GenerateFastApiDirectCall.
605static void ReserveSpaceForFastApiCall(MacroAssembler* masm,
606 Register scratch) {
607 ASSERT(Smi::FromInt(0) == 0);
608 for (int i = 0; i < kFastApiCallArguments; i++) {
609 __ push(zero_reg);
610 }
611}
612
613
614// Undoes the effects of ReserveSpaceForFastApiCall.
615static void FreeSpaceForFastApiCall(MacroAssembler* masm) {
616 __ Drop(kFastApiCallArguments);
617}
618
619
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000620static MaybeObject* GenerateFastApiDirectCall(
621 MacroAssembler* masm,
622 const CallOptimization& optimization,
623 int argc) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000624 // ----------- S t a t e -------------
625 // -- sp[0] : holder (set by CheckPrototypes)
626 // -- sp[4] : callee js function
627 // -- sp[8] : call data
628 // -- sp[12] : last js argument
629 // -- ...
630 // -- sp[(argc + 3) * 4] : first js argument
631 // -- sp[(argc + 4) * 4] : receiver
632 // -----------------------------------
633 // Get the function and setup the context.
634 JSFunction* function = optimization.constant_function();
635 __ li(t1, Operand(Handle<JSFunction>(function)));
636 __ lw(cp, FieldMemOperand(t1, JSFunction::kContextOffset));
637
638 // Pass the additional arguments FastHandleApiCall expects.
639 Object* call_data = optimization.api_call_info()->data();
640 Handle<CallHandlerInfo> api_call_info_handle(optimization.api_call_info());
641 if (masm->isolate()->heap()->InNewSpace(call_data)) {
642 __ li(a0, api_call_info_handle);
643 __ lw(t2, FieldMemOperand(a0, CallHandlerInfo::kDataOffset));
644 } else {
645 __ li(t2, Operand(Handle<Object>(call_data)));
646 }
647
648 // Store js function and call data.
649 __ sw(t1, MemOperand(sp, 1 * kPointerSize));
650 __ sw(t2, MemOperand(sp, 2 * kPointerSize));
651
652 // a2 points to call data as expected by Arguments
653 // (refer to layout above).
654 __ Addu(a2, sp, Operand(2 * kPointerSize));
655
656 Object* callback = optimization.api_call_info()->callback();
657 Address api_function_address = v8::ToCData<Address>(callback);
658 ApiFunction fun(api_function_address);
659
660 const int kApiStackSpace = 4;
661
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000662 FrameScope frame_scope(masm, StackFrame::MANUAL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000663 __ EnterExitFrame(false, kApiStackSpace);
664
665 // NOTE: the O32 abi requires a0 to hold a special pointer when returning a
666 // struct from the function (which is currently the case). This means we pass
667 // the first argument in a1 instead of a0. TryCallApiFunctionAndReturn
668 // will handle setting up a0.
669
670 // a1 = v8::Arguments&
671 // Arguments is built at sp + 1 (sp is a reserved spot for ra).
672 __ Addu(a1, sp, kPointerSize);
673
674 // v8::Arguments::implicit_args = data
675 __ sw(a2, MemOperand(a1, 0 * kPointerSize));
676 // v8::Arguments::values = last argument
677 __ Addu(t0, a2, Operand(argc * kPointerSize));
678 __ sw(t0, MemOperand(a1, 1 * kPointerSize));
679 // v8::Arguments::length_ = argc
680 __ li(t0, Operand(argc));
681 __ sw(t0, MemOperand(a1, 2 * kPointerSize));
682 // v8::Arguments::is_construct_call = 0
683 __ sw(zero_reg, MemOperand(a1, 3 * kPointerSize));
684
685 // Emitting a stub call may try to allocate (if the code is not
686 // already generated). Do not allow the assembler to perform a
687 // garbage collection but instead return the allocation failure
688 // object.
689 const int kStackUnwindSpace = argc + kFastApiCallArguments + 1;
690 ExternalReference ref =
691 ExternalReference(&fun,
692 ExternalReference::DIRECT_API_CALL,
693 masm->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000694 AllowExternalCallThatCantCauseGC scope(masm);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000695 return masm->TryCallApiFunctionAndReturn(ref, kStackUnwindSpace);
696}
697
lrn@chromium.org7516f052011-03-30 08:52:27 +0000698class CallInterceptorCompiler BASE_EMBEDDED {
699 public:
700 CallInterceptorCompiler(StubCompiler* stub_compiler,
701 const ParameterCount& arguments,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000702 Register name,
703 Code::ExtraICState extra_ic_state)
lrn@chromium.org7516f052011-03-30 08:52:27 +0000704 : stub_compiler_(stub_compiler),
705 arguments_(arguments),
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000706 name_(name),
707 extra_ic_state_(extra_ic_state) {}
lrn@chromium.org7516f052011-03-30 08:52:27 +0000708
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000709 MaybeObject* Compile(MacroAssembler* masm,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000710 JSObject* object,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000711 JSObject* holder,
712 String* name,
713 LookupResult* lookup,
lrn@chromium.org7516f052011-03-30 08:52:27 +0000714 Register receiver,
715 Register scratch1,
716 Register scratch2,
717 Register scratch3,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000718 Label* miss) {
719 ASSERT(holder->HasNamedInterceptor());
720 ASSERT(!holder->GetNamedInterceptor()->getter()->IsUndefined());
721
722 // Check that the receiver isn't a smi.
723 __ JumpIfSmi(receiver, miss);
724
725 CallOptimization optimization(lookup);
726
727 if (optimization.is_constant_call()) {
728 return CompileCacheable(masm,
729 object,
730 receiver,
731 scratch1,
732 scratch2,
733 scratch3,
734 holder,
735 lookup,
736 name,
737 optimization,
738 miss);
739 } else {
740 CompileRegular(masm,
741 object,
742 receiver,
743 scratch1,
744 scratch2,
745 scratch3,
746 name,
747 holder,
748 miss);
749 return masm->isolate()->heap()->undefined_value();
750 }
751 }
752
753 private:
754 MaybeObject* CompileCacheable(MacroAssembler* masm,
755 JSObject* object,
756 Register receiver,
757 Register scratch1,
758 Register scratch2,
759 Register scratch3,
760 JSObject* interceptor_holder,
761 LookupResult* lookup,
762 String* name,
763 const CallOptimization& optimization,
764 Label* miss_label) {
765 ASSERT(optimization.is_constant_call());
766 ASSERT(!lookup->holder()->IsGlobalObject());
767
768 Counters* counters = masm->isolate()->counters();
769
770 int depth1 = kInvalidProtoDepth;
771 int depth2 = kInvalidProtoDepth;
772 bool can_do_fast_api_call = false;
773 if (optimization.is_simple_api_call() &&
774 !lookup->holder()->IsGlobalObject()) {
775 depth1 =
776 optimization.GetPrototypeDepthOfExpectedType(object,
777 interceptor_holder);
778 if (depth1 == kInvalidProtoDepth) {
779 depth2 =
780 optimization.GetPrototypeDepthOfExpectedType(interceptor_holder,
781 lookup->holder());
782 }
783 can_do_fast_api_call = (depth1 != kInvalidProtoDepth) ||
784 (depth2 != kInvalidProtoDepth);
785 }
786
787 __ IncrementCounter(counters->call_const_interceptor(), 1,
788 scratch1, scratch2);
789
790 if (can_do_fast_api_call) {
791 __ IncrementCounter(counters->call_const_interceptor_fast_api(), 1,
792 scratch1, scratch2);
793 ReserveSpaceForFastApiCall(masm, scratch1);
794 }
795
796 // Check that the maps from receiver to interceptor's holder
797 // haven't changed and thus we can invoke interceptor.
798 Label miss_cleanup;
799 Label* miss = can_do_fast_api_call ? &miss_cleanup : miss_label;
800 Register holder =
801 stub_compiler_->CheckPrototypes(object, receiver,
802 interceptor_holder, scratch1,
803 scratch2, scratch3, name, depth1, miss);
804
805 // Invoke an interceptor and if it provides a value,
806 // branch to |regular_invoke|.
807 Label regular_invoke;
808 LoadWithInterceptor(masm, receiver, holder, interceptor_holder, scratch2,
809 &regular_invoke);
810
811 // Interceptor returned nothing for this property. Try to use cached
812 // constant function.
813
814 // Check that the maps from interceptor's holder to constant function's
815 // holder haven't changed and thus we can use cached constant function.
816 if (interceptor_holder != lookup->holder()) {
817 stub_compiler_->CheckPrototypes(interceptor_holder, receiver,
818 lookup->holder(), scratch1,
819 scratch2, scratch3, name, depth2, miss);
820 } else {
821 // CheckPrototypes has a side effect of fetching a 'holder'
822 // for API (object which is instanceof for the signature). It's
823 // safe to omit it here, as if present, it should be fetched
824 // by the previous CheckPrototypes.
825 ASSERT(depth2 == kInvalidProtoDepth);
826 }
827
828 // Invoke function.
829 if (can_do_fast_api_call) {
830 MaybeObject* result = GenerateFastApiDirectCall(masm,
831 optimization,
832 arguments_.immediate());
833 if (result->IsFailure()) return result;
834 } else {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000835 CallKind call_kind = CallICBase::Contextual::decode(extra_ic_state_)
836 ? CALL_AS_FUNCTION
837 : CALL_AS_METHOD;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000838 __ InvokeFunction(optimization.constant_function(), arguments_,
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000839 JUMP_FUNCTION, call_kind);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000840 }
841
842 // Deferred code for fast API call case---clean preallocated space.
843 if (can_do_fast_api_call) {
844 __ bind(&miss_cleanup);
845 FreeSpaceForFastApiCall(masm);
846 __ Branch(miss_label);
847 }
848
849 // Invoke a regular function.
850 __ bind(&regular_invoke);
851 if (can_do_fast_api_call) {
852 FreeSpaceForFastApiCall(masm);
853 }
854
855 return masm->isolate()->heap()->undefined_value();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000856 }
857
858 void CompileRegular(MacroAssembler* masm,
859 JSObject* object,
860 Register receiver,
861 Register scratch1,
862 Register scratch2,
863 Register scratch3,
864 String* name,
865 JSObject* interceptor_holder,
866 Label* miss_label) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000867 Register holder =
868 stub_compiler_->CheckPrototypes(object, receiver, interceptor_holder,
869 scratch1, scratch2, scratch3, name,
870 miss_label);
871
872 // Call a runtime function to load the interceptor property.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000873 FrameScope scope(masm, StackFrame::INTERNAL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000874 // Save the name_ register across the call.
875 __ push(name_);
876
877 PushInterceptorArguments(masm,
878 receiver,
879 holder,
880 name_,
881 interceptor_holder);
882
883 __ CallExternalReference(
884 ExternalReference(
885 IC_Utility(IC::kLoadPropertyWithInterceptorForCall),
886 masm->isolate()),
887 5);
888
889 // Restore the name_ register.
890 __ pop(name_);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000891
892 // Leave the internal frame.
lrn@chromium.org7516f052011-03-30 08:52:27 +0000893 }
894
895 void LoadWithInterceptor(MacroAssembler* masm,
896 Register receiver,
897 Register holder,
898 JSObject* holder_obj,
899 Register scratch,
900 Label* interceptor_succeeded) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000901 {
902 FrameScope scope(masm, StackFrame::INTERNAL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000903
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000904 __ Push(holder, name_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000905
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000906 CompileCallLoadPropertyWithInterceptor(masm,
907 receiver,
908 holder,
909 name_,
910 holder_obj);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000911
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000912 __ pop(name_); // Restore the name.
913 __ pop(receiver); // Restore the holder.
914 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000915
916 // If interceptor returns no-result sentinel, call the constant function.
917 __ LoadRoot(scratch, Heap::kNoInterceptorResultSentinelRootIndex);
918 __ Branch(interceptor_succeeded, ne, v0, Operand(scratch));
lrn@chromium.org7516f052011-03-30 08:52:27 +0000919 }
920
921 StubCompiler* stub_compiler_;
922 const ParameterCount& arguments_;
923 Register name_;
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +0000924 Code::ExtraICState extra_ic_state_;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000925};
926
927
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000928
929// Generate code to check that a global property cell is empty. Create
930// the property cell at compilation time if no cell exists for the
931// property.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000932static void GenerateCheckPropertyCell(MacroAssembler* masm,
933 Handle<GlobalObject> global,
934 Handle<String> name,
935 Register scratch,
936 Label* miss) {
937 Handle<JSGlobalPropertyCell> cell =
938 GlobalObject::EnsurePropertyCell(global, name);
939 ASSERT(cell->value()->IsTheHole());
940 __ li(scratch, Operand(cell));
941 __ lw(scratch,
942 FieldMemOperand(scratch, JSGlobalPropertyCell::kValueOffset));
943 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
944 __ Branch(miss, ne, scratch, Operand(at));
945}
946
947
948// TODO(kmillikin): Eliminate this function when the stub cache is fully
949// handlified.
950MUST_USE_RESULT static MaybeObject* TryGenerateCheckPropertyCell(
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000951 MacroAssembler* masm,
952 GlobalObject* global,
953 String* name,
954 Register scratch,
955 Label* miss) {
956 Object* probe;
957 { MaybeObject* maybe_probe = global->EnsurePropertyCell(name);
958 if (!maybe_probe->ToObject(&probe)) return maybe_probe;
959 }
960 JSGlobalPropertyCell* cell = JSGlobalPropertyCell::cast(probe);
961 ASSERT(cell->value()->IsTheHole());
962 __ li(scratch, Operand(Handle<Object>(cell)));
963 __ lw(scratch,
964 FieldMemOperand(scratch, JSGlobalPropertyCell::kValueOffset));
965 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
966 __ Branch(miss, ne, scratch, Operand(at));
967 return cell;
968}
969
970
971// Calls GenerateCheckPropertyCell for each global object in the prototype chain
972// from object to (but not including) holder.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000973static void GenerateCheckPropertyCells(MacroAssembler* masm,
974 Handle<JSObject> object,
975 Handle<JSObject> holder,
976 Handle<String> name,
977 Register scratch,
978 Label* miss) {
979 Handle<JSObject> current = object;
980 while (!current.is_identical_to(holder)) {
981 if (current->IsGlobalObject()) {
982 GenerateCheckPropertyCell(masm,
983 Handle<GlobalObject>::cast(current),
984 name,
985 scratch,
986 miss);
987 }
988 current = Handle<JSObject>(JSObject::cast(current->GetPrototype()));
989 }
990}
991
992
993// TODO(kmillikin): Eliminate this function when the stub cache is fully
994// handlified.
995MUST_USE_RESULT static MaybeObject* TryGenerateCheckPropertyCells(
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000996 MacroAssembler* masm,
997 JSObject* object,
998 JSObject* holder,
999 String* name,
1000 Register scratch,
1001 Label* miss) {
1002 JSObject* current = object;
1003 while (current != holder) {
1004 if (current->IsGlobalObject()) {
1005 // Returns a cell or a failure.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001006 MaybeObject* result = TryGenerateCheckPropertyCell(
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001007 masm,
1008 GlobalObject::cast(current),
1009 name,
1010 scratch,
1011 miss);
1012 if (result->IsFailure()) return result;
1013 }
1014 ASSERT(current->IsJSObject());
1015 current = JSObject::cast(current->GetPrototype());
1016 }
1017 return NULL;
1018}
1019
1020
1021// Convert and store int passed in register ival to IEEE 754 single precision
1022// floating point value at memory location (dst + 4 * wordoffset)
1023// If FPU is available use it for conversion.
1024static void StoreIntAsFloat(MacroAssembler* masm,
1025 Register dst,
1026 Register wordoffset,
1027 Register ival,
1028 Register fval,
1029 Register scratch1,
1030 Register scratch2) {
1031 if (CpuFeatures::IsSupported(FPU)) {
1032 CpuFeatures::Scope scope(FPU);
1033 __ mtc1(ival, f0);
1034 __ cvt_s_w(f0, f0);
1035 __ sll(scratch1, wordoffset, 2);
1036 __ addu(scratch1, dst, scratch1);
1037 __ swc1(f0, MemOperand(scratch1, 0));
1038 } else {
1039 // FPU is not available, do manual conversions.
1040
1041 Label not_special, done;
1042 // Move sign bit from source to destination. This works because the sign
1043 // bit in the exponent word of the double has the same position and polarity
1044 // as the 2's complement sign bit in a Smi.
1045 ASSERT(kBinary32SignMask == 0x80000000u);
1046
1047 __ And(fval, ival, Operand(kBinary32SignMask));
1048 // Negate value if it is negative.
1049 __ subu(scratch1, zero_reg, ival);
1050 __ movn(ival, scratch1, fval);
1051
1052 // We have -1, 0 or 1, which we treat specially. Register ival contains
1053 // absolute value: it is either equal to 1 (special case of -1 and 1),
1054 // greater than 1 (not a special case) or less than 1 (special case of 0).
1055 __ Branch(&not_special, gt, ival, Operand(1));
1056
1057 // For 1 or -1 we need to or in the 0 exponent (biased).
1058 static const uint32_t exponent_word_for_1 =
1059 kBinary32ExponentBias << kBinary32ExponentShift;
1060
1061 __ Xor(scratch1, ival, Operand(1));
1062 __ li(scratch2, exponent_word_for_1);
1063 __ or_(scratch2, fval, scratch2);
1064 __ movz(fval, scratch2, scratch1); // Only if ival is equal to 1.
1065 __ Branch(&done);
1066
1067 __ bind(&not_special);
1068 // Count leading zeros.
1069 // Gets the wrong answer for 0, but we already checked for that case above.
1070 Register zeros = scratch2;
1071 __ clz(zeros, ival);
1072
1073 // Compute exponent and or it into the exponent register.
1074 __ li(scratch1, (kBitsPerInt - 1) + kBinary32ExponentBias);
1075 __ subu(scratch1, scratch1, zeros);
1076
1077 __ sll(scratch1, scratch1, kBinary32ExponentShift);
1078 __ or_(fval, fval, scratch1);
1079
1080 // Shift up the source chopping the top bit off.
1081 __ Addu(zeros, zeros, Operand(1));
1082 // This wouldn't work for 1 and -1 as the shift would be 32 which means 0.
1083 __ sllv(ival, ival, zeros);
1084 // And the top (top 20 bits).
1085 __ srl(scratch1, ival, kBitsPerInt - kBinary32MantissaBits);
1086 __ or_(fval, fval, scratch1);
1087
1088 __ bind(&done);
1089
1090 __ sll(scratch1, wordoffset, 2);
1091 __ addu(scratch1, dst, scratch1);
1092 __ sw(fval, MemOperand(scratch1, 0));
1093 }
1094}
1095
1096
1097// Convert unsigned integer with specified number of leading zeroes in binary
1098// representation to IEEE 754 double.
1099// Integer to convert is passed in register hiword.
1100// Resulting double is returned in registers hiword:loword.
1101// This functions does not work correctly for 0.
1102static void GenerateUInt2Double(MacroAssembler* masm,
1103 Register hiword,
1104 Register loword,
1105 Register scratch,
1106 int leading_zeroes) {
1107 const int meaningful_bits = kBitsPerInt - leading_zeroes - 1;
1108 const int biased_exponent = HeapNumber::kExponentBias + meaningful_bits;
1109
1110 const int mantissa_shift_for_hi_word =
1111 meaningful_bits - HeapNumber::kMantissaBitsInTopWord;
1112
1113 const int mantissa_shift_for_lo_word =
1114 kBitsPerInt - mantissa_shift_for_hi_word;
1115
1116 __ li(scratch, biased_exponent << HeapNumber::kExponentShift);
1117 if (mantissa_shift_for_hi_word > 0) {
1118 __ sll(loword, hiword, mantissa_shift_for_lo_word);
1119 __ srl(hiword, hiword, mantissa_shift_for_hi_word);
1120 __ or_(hiword, scratch, hiword);
1121 } else {
1122 __ mov(loword, zero_reg);
1123 __ sll(hiword, hiword, mantissa_shift_for_hi_word);
1124 __ or_(hiword, scratch, hiword);
1125 }
1126
1127 // If least significant bit of biased exponent was not 1 it was corrupted
1128 // by most significant bit of mantissa so we should fix that.
1129 if (!(biased_exponent & 1)) {
1130 __ li(scratch, 1 << HeapNumber::kExponentShift);
1131 __ nor(scratch, scratch, scratch);
1132 __ and_(hiword, hiword, scratch);
1133 }
1134}
1135
1136
ager@chromium.org5c838252010-02-19 08:53:10 +00001137#undef __
1138#define __ ACCESS_MASM(masm())
1139
1140
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001141Register StubCompiler::CheckPrototypes(Handle<JSObject> object,
1142 Register object_reg,
1143 Handle<JSObject> holder,
1144 Register holder_reg,
1145 Register scratch1,
1146 Register scratch2,
1147 Handle<String> name,
1148 int save_at_depth,
1149 Label* miss) {
1150 // Make sure there's no overlap between holder and object registers.
1151 ASSERT(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
1152 ASSERT(!scratch2.is(object_reg) && !scratch2.is(holder_reg)
1153 && !scratch2.is(scratch1));
1154
1155 // Keep track of the current object in register reg.
1156 Register reg = object_reg;
1157 int depth = 0;
1158
1159 if (save_at_depth == depth) {
1160 __ sw(reg, MemOperand(sp));
1161 }
1162
1163 // Check the maps in the prototype chain.
1164 // Traverse the prototype chain from the object and do map checks.
1165 Handle<JSObject> current = object;
1166 while (!current.is_identical_to(holder)) {
1167 ++depth;
1168
1169 // Only global objects and objects that do not require access
1170 // checks are allowed in stubs.
1171 ASSERT(current->IsJSGlobalProxy() || !current->IsAccessCheckNeeded());
1172
1173 Handle<JSObject> prototype(JSObject::cast(current->GetPrototype()));
1174 if (!current->HasFastProperties() &&
1175 !current->IsJSGlobalObject() &&
1176 !current->IsJSGlobalProxy()) {
1177 if (!name->IsSymbol()) {
1178 name = factory()->LookupSymbol(name);
1179 }
1180 ASSERT(current->property_dictionary()->FindEntry(*name) ==
1181 StringDictionary::kNotFound);
1182
1183 GenerateDictionaryNegativeLookup(masm(), miss, reg, name,
1184 scratch1, scratch2);
1185
1186 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1187 reg = holder_reg; // From now on the object will be in holder_reg.
1188 __ lw(reg, FieldMemOperand(scratch1, Map::kPrototypeOffset));
1189 } else {
1190 Handle<Map> current_map(current->map());
1191 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1192 // Branch on the result of the map check.
1193 __ Branch(miss, ne, scratch1, Operand(current_map));
1194 // Check access rights to the global object. This has to happen after
1195 // the map check so that we know that the object is actually a global
1196 // object.
1197 if (current->IsJSGlobalProxy()) {
1198 __ CheckAccessGlobalProxy(reg, scratch2, miss);
1199 }
1200 reg = holder_reg; // From now on the object will be in holder_reg.
1201
1202 if (heap()->InNewSpace(*prototype)) {
1203 // The prototype is in new space; we cannot store a reference to it
1204 // in the code. Load it from the map.
1205 __ lw(reg, FieldMemOperand(scratch1, Map::kPrototypeOffset));
1206 } else {
1207 // The prototype is in old space; load it directly.
1208 __ li(reg, Operand(prototype));
1209 }
1210 }
1211
1212 if (save_at_depth == depth) {
1213 __ sw(reg, MemOperand(sp));
1214 }
1215
1216 // Go to the next object in the prototype chain.
1217 current = prototype;
1218 }
1219
1220 // Log the check depth.
1221 LOG(masm()->isolate(), IntEvent("check-maps-depth", depth + 1));
1222
1223 // Check the holder map.
1224 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1225 __ Branch(miss, ne, scratch1, Operand(Handle<Map>(current->map())));
1226
1227 // Perform security check for access to the global object.
1228 ASSERT(holder->IsJSGlobalProxy() || !holder->IsAccessCheckNeeded());
1229 if (holder->IsJSGlobalProxy()) {
1230 __ CheckAccessGlobalProxy(reg, scratch1, miss);
1231 }
1232
1233 // If we've skipped any global objects, it's not enough to verify that
1234 // their maps haven't changed. We also need to check that the property
1235 // cell for the property is still empty.
1236 GenerateCheckPropertyCells(masm(), object, holder, name, scratch1, miss);
1237
1238 // Return the register containing the holder.
1239 return reg;
1240}
1241
1242
lrn@chromium.org7516f052011-03-30 08:52:27 +00001243Register StubCompiler::CheckPrototypes(JSObject* object,
1244 Register object_reg,
1245 JSObject* holder,
1246 Register holder_reg,
1247 Register scratch1,
1248 Register scratch2,
1249 String* name,
1250 int save_at_depth,
1251 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001252 // Make sure there's no overlap between holder and object registers.
1253 ASSERT(!scratch1.is(object_reg) && !scratch1.is(holder_reg));
1254 ASSERT(!scratch2.is(object_reg) && !scratch2.is(holder_reg)
1255 && !scratch2.is(scratch1));
1256
1257 // Keep track of the current object in register reg.
1258 Register reg = object_reg;
1259 int depth = 0;
1260
1261 if (save_at_depth == depth) {
1262 __ sw(reg, MemOperand(sp));
1263 }
1264
1265 // Check the maps in the prototype chain.
1266 // Traverse the prototype chain from the object and do map checks.
1267 JSObject* current = object;
1268 while (current != holder) {
1269 depth++;
1270
1271 // Only global objects and objects that do not require access
1272 // checks are allowed in stubs.
1273 ASSERT(current->IsJSGlobalProxy() || !current->IsAccessCheckNeeded());
1274
1275 ASSERT(current->GetPrototype()->IsJSObject());
1276 JSObject* prototype = JSObject::cast(current->GetPrototype());
1277 if (!current->HasFastProperties() &&
1278 !current->IsJSGlobalObject() &&
1279 !current->IsJSGlobalProxy()) {
1280 if (!name->IsSymbol()) {
1281 MaybeObject* maybe_lookup_result = heap()->LookupSymbol(name);
1282 Object* lookup_result = NULL; // Initialization to please compiler.
1283 if (!maybe_lookup_result->ToObject(&lookup_result)) {
1284 set_failure(Failure::cast(maybe_lookup_result));
1285 return reg;
1286 }
1287 name = String::cast(lookup_result);
1288 }
1289 ASSERT(current->property_dictionary()->FindEntry(name) ==
1290 StringDictionary::kNotFound);
1291
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001292 MaybeObject* negative_lookup =
1293 TryGenerateDictionaryNegativeLookup(masm(),
1294 miss,
1295 reg,
1296 name,
1297 scratch1,
1298 scratch2);
1299
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001300 if (negative_lookup->IsFailure()) {
1301 set_failure(Failure::cast(negative_lookup));
1302 return reg;
1303 }
1304
1305 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1306 reg = holder_reg; // From now the object is in holder_reg.
1307 __ lw(reg, FieldMemOperand(scratch1, Map::kPrototypeOffset));
1308 } else if (heap()->InNewSpace(prototype)) {
1309 // Get the map of the current object.
1310 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1311
1312 // Branch on the result of the map check.
1313 __ Branch(miss, ne, scratch1, Operand(Handle<Map>(current->map())));
1314
1315 // Check access rights to the global object. This has to happen
1316 // after the map check so that we know that the object is
1317 // actually a global object.
1318 if (current->IsJSGlobalProxy()) {
1319 __ CheckAccessGlobalProxy(reg, scratch1, miss);
1320 // Restore scratch register to be the map of the object. In the
1321 // new space case below, we load the prototype from the map in
1322 // the scratch register.
1323 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1324 }
1325
1326 reg = holder_reg; // From now the object is in holder_reg.
1327 // The prototype is in new space; we cannot store a reference
1328 // to it in the code. Load it from the map.
1329 __ lw(reg, FieldMemOperand(scratch1, Map::kPrototypeOffset));
1330 } else {
1331 // Check the map of the current object.
1332 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1333 // Branch on the result of the map check.
1334 __ Branch(miss, ne, scratch1, Operand(Handle<Map>(current->map())));
1335 // Check access rights to the global object. This has to happen
1336 // after the map check so that we know that the object is
1337 // actually a global object.
1338 if (current->IsJSGlobalProxy()) {
1339 __ CheckAccessGlobalProxy(reg, scratch1, miss);
1340 }
1341 // The prototype is in old space; load it directly.
1342 reg = holder_reg; // From now the object is in holder_reg.
1343 __ li(reg, Operand(Handle<JSObject>(prototype)));
1344 }
1345
1346 if (save_at_depth == depth) {
1347 __ sw(reg, MemOperand(sp));
1348 }
1349
1350 // Go to the next object in the prototype chain.
1351 current = prototype;
1352 }
1353
1354 // Check the holder map.
1355 __ lw(scratch1, FieldMemOperand(reg, HeapObject::kMapOffset));
1356 __ Branch(miss, ne, scratch1, Operand(Handle<Map>(current->map())));
1357
1358 // Log the check depth.
1359 LOG(masm()->isolate(), IntEvent("check-maps-depth", depth + 1));
1360 // Perform security check for access to the global object.
1361 ASSERT(holder->IsJSGlobalProxy() || !holder->IsAccessCheckNeeded());
1362 if (holder->IsJSGlobalProxy()) {
1363 __ CheckAccessGlobalProxy(reg, scratch1, miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001364 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001365
1366 // If we've skipped any global objects, it's not enough to verify
1367 // that their maps haven't changed. We also need to check that the
1368 // property cell for the property is still empty.
1369
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001370 MaybeObject* result = TryGenerateCheckPropertyCells(masm(),
1371 object,
1372 holder,
1373 name,
1374 scratch1,
1375 miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001376 if (result->IsFailure()) set_failure(Failure::cast(result));
1377
1378 // Return the register containing the holder.
1379 return reg;
lrn@chromium.org7516f052011-03-30 08:52:27 +00001380}
1381
1382
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001383void StubCompiler::GenerateLoadField(Handle<JSObject> object,
1384 Handle<JSObject> holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001385 Register receiver,
1386 Register scratch1,
1387 Register scratch2,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001388 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +00001389 int index,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001390 Handle<String> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00001391 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001392 // Check that the receiver isn't a smi.
1393 __ And(scratch1, receiver, Operand(kSmiTagMask));
1394 __ Branch(miss, eq, scratch1, Operand(zero_reg));
1395
1396 // Check that the maps haven't changed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001397 Register reg = CheckPrototypes(
1398 object, receiver, holder, scratch1, scratch2, scratch3, name, miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001399 GenerateFastPropertyLoad(masm(), v0, reg, holder, index);
1400 __ Ret();
ager@chromium.org5c838252010-02-19 08:53:10 +00001401}
1402
1403
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001404void StubCompiler::GenerateLoadConstant(Handle<JSObject> object,
1405 Handle<JSObject> holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001406 Register receiver,
1407 Register scratch1,
1408 Register scratch2,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001409 Register scratch3,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001410 Handle<Object> value,
1411 Handle<String> name,
ager@chromium.org5c838252010-02-19 08:53:10 +00001412 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001413 // Check that the receiver isn't a smi.
1414 __ JumpIfSmi(receiver, miss, scratch1);
1415
1416 // Check that the maps haven't changed.
1417 Register reg =
1418 CheckPrototypes(object, receiver, holder,
1419 scratch1, scratch2, scratch3, name, miss);
1420
1421 // Return the constant value.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001422 __ li(v0, Operand(value));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001423 __ Ret();
ager@chromium.org5c838252010-02-19 08:53:10 +00001424}
1425
1426
lrn@chromium.org7516f052011-03-30 08:52:27 +00001427MaybeObject* StubCompiler::GenerateLoadCallback(JSObject* object,
1428 JSObject* holder,
1429 Register receiver,
1430 Register name_reg,
1431 Register scratch1,
1432 Register scratch2,
1433 Register scratch3,
1434 AccessorInfo* callback,
1435 String* name,
1436 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001437 // Check that the receiver isn't a smi.
1438 __ JumpIfSmi(receiver, miss, scratch1);
1439
1440 // Check that the maps haven't changed.
1441 Register reg =
1442 CheckPrototypes(object, receiver, holder, scratch1, scratch2, scratch3,
1443 name, miss);
1444
1445 // Build AccessorInfo::args_ list on the stack and push property name below
1446 // the exit frame to make GC aware of them and store pointers to them.
1447 __ push(receiver);
1448 __ mov(scratch2, sp); // scratch2 = AccessorInfo::args_
1449 Handle<AccessorInfo> callback_handle(callback);
1450 if (heap()->InNewSpace(callback_handle->data())) {
1451 __ li(scratch3, callback_handle);
1452 __ lw(scratch3, FieldMemOperand(scratch3, AccessorInfo::kDataOffset));
1453 } else {
1454 __ li(scratch3, Handle<Object>(callback_handle->data()));
1455 }
1456 __ Push(reg, scratch3, name_reg);
1457 __ mov(a2, scratch2); // Saved in case scratch2 == a1.
1458 __ mov(a1, sp); // a1 (first argument - see note below) = Handle<String>
1459
1460 Address getter_address = v8::ToCData<Address>(callback->getter());
1461 ApiFunction fun(getter_address);
1462
1463 // NOTE: the O32 abi requires a0 to hold a special pointer when returning a
1464 // struct from the function (which is currently the case). This means we pass
1465 // the arguments in a1-a2 instead of a0-a1. TryCallApiFunctionAndReturn
1466 // will handle setting up a0.
1467
1468 const int kApiStackSpace = 1;
1469
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001470 FrameScope frame_scope(masm(), StackFrame::MANUAL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001471 __ EnterExitFrame(false, kApiStackSpace);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001472
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001473 // Create AccessorInfo instance on the stack above the exit frame with
1474 // scratch2 (internal::Object **args_) as the data.
1475 __ sw(a2, MemOperand(sp, kPointerSize));
1476 // a2 (second argument - see note above) = AccessorInfo&
1477 __ Addu(a2, sp, kPointerSize);
1478
1479 // Emitting a stub call may try to allocate (if the code is not
1480 // already generated). Do not allow the assembler to perform a
1481 // garbage collection but instead return the allocation failure
1482 // object.
1483 ExternalReference ref =
1484 ExternalReference(&fun,
1485 ExternalReference::DIRECT_GETTER_CALL,
1486 masm()->isolate());
1487 // 4 args - will be freed later by LeaveExitFrame.
1488 return masm()->TryCallApiFunctionAndReturn(ref, 4);
ager@chromium.org5c838252010-02-19 08:53:10 +00001489}
1490
1491
1492void StubCompiler::GenerateLoadInterceptor(JSObject* object,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001493 JSObject* interceptor_holder,
ager@chromium.org5c838252010-02-19 08:53:10 +00001494 LookupResult* lookup,
1495 Register receiver,
1496 Register name_reg,
1497 Register scratch1,
1498 Register scratch2,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001499 Register scratch3,
ager@chromium.org5c838252010-02-19 08:53:10 +00001500 String* name,
1501 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001502 ASSERT(interceptor_holder->HasNamedInterceptor());
1503 ASSERT(!interceptor_holder->GetNamedInterceptor()->getter()->IsUndefined());
1504
1505 // Check that the receiver isn't a smi.
1506 __ JumpIfSmi(receiver, miss);
1507
1508 // So far the most popular follow ups for interceptor loads are FIELD
1509 // and CALLBACKS, so inline only them, other cases may be added
1510 // later.
1511 bool compile_followup_inline = false;
1512 if (lookup->IsProperty() && lookup->IsCacheable()) {
1513 if (lookup->type() == FIELD) {
1514 compile_followup_inline = true;
1515 } else if (lookup->type() == CALLBACKS &&
1516 lookup->GetCallbackObject()->IsAccessorInfo() &&
1517 AccessorInfo::cast(lookup->GetCallbackObject())->getter() != NULL) {
1518 compile_followup_inline = true;
1519 }
1520 }
1521
1522 if (compile_followup_inline) {
1523 // Compile the interceptor call, followed by inline code to load the
1524 // property from further up the prototype chain if the call fails.
1525 // Check that the maps haven't changed.
1526 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
1527 scratch1, scratch2, scratch3,
1528 name, miss);
1529 ASSERT(holder_reg.is(receiver) || holder_reg.is(scratch1));
1530
1531 // Save necessary data before invoking an interceptor.
1532 // Requires a frame to make GC aware of pushed pointers.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001533 {
1534 FrameScope frame_scope(masm(), StackFrame::INTERNAL);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001535
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001536 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1537 // CALLBACKS case needs a receiver to be passed into C++ callback.
1538 __ Push(receiver, holder_reg, name_reg);
1539 } else {
1540 __ Push(holder_reg, name_reg);
1541 }
1542
1543 // Invoke an interceptor. Note: map checks from receiver to
1544 // interceptor's holder has been compiled before (see a caller
1545 // of this method).
1546 CompileCallLoadPropertyWithInterceptor(masm(),
1547 receiver,
1548 holder_reg,
1549 name_reg,
1550 interceptor_holder);
1551
1552 // Check if interceptor provided a value for property. If it's
1553 // the case, return immediately.
1554 Label interceptor_failed;
1555 __ LoadRoot(scratch1, Heap::kNoInterceptorResultSentinelRootIndex);
1556 __ Branch(&interceptor_failed, eq, v0, Operand(scratch1));
1557 frame_scope.GenerateLeaveFrame();
1558 __ Ret();
1559
1560 __ bind(&interceptor_failed);
1561 __ pop(name_reg);
1562 __ pop(holder_reg);
1563 if (lookup->type() == CALLBACKS && !receiver.is(holder_reg)) {
1564 __ pop(receiver);
1565 }
1566
1567 // Leave the internal frame.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001568 }
1569
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001570 // Check that the maps from interceptor's holder to lookup's holder
1571 // haven't changed. And load lookup's holder into |holder| register.
1572 if (interceptor_holder != lookup->holder()) {
1573 holder_reg = CheckPrototypes(interceptor_holder,
1574 holder_reg,
1575 lookup->holder(),
1576 scratch1,
1577 scratch2,
1578 scratch3,
1579 name,
1580 miss);
1581 }
1582
1583 if (lookup->type() == FIELD) {
1584 // We found FIELD property in prototype chain of interceptor's holder.
1585 // Retrieve a field from field's holder.
1586 GenerateFastPropertyLoad(masm(), v0, holder_reg,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001587 Handle<JSObject>(lookup->holder()),
1588 lookup->GetFieldIndex());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001589 __ Ret();
1590 } else {
1591 // We found CALLBACKS property in prototype chain of interceptor's
1592 // holder.
1593 ASSERT(lookup->type() == CALLBACKS);
1594 ASSERT(lookup->GetCallbackObject()->IsAccessorInfo());
1595 AccessorInfo* callback = AccessorInfo::cast(lookup->GetCallbackObject());
1596 ASSERT(callback != NULL);
1597 ASSERT(callback->getter() != NULL);
1598
1599 // Tail call to runtime.
1600 // Important invariant in CALLBACKS case: the code above must be
1601 // structured to never clobber |receiver| register.
1602 __ li(scratch2, Handle<AccessorInfo>(callback));
1603 // holder_reg is either receiver or scratch1.
1604 if (!receiver.is(holder_reg)) {
1605 ASSERT(scratch1.is(holder_reg));
1606 __ Push(receiver, holder_reg);
1607 __ lw(scratch3,
1608 FieldMemOperand(scratch2, AccessorInfo::kDataOffset));
1609 __ Push(scratch3, scratch2, name_reg);
1610 } else {
1611 __ push(receiver);
1612 __ lw(scratch3,
1613 FieldMemOperand(scratch2, AccessorInfo::kDataOffset));
1614 __ Push(holder_reg, scratch3, scratch2, name_reg);
1615 }
1616
1617 ExternalReference ref =
1618 ExternalReference(IC_Utility(IC::kLoadCallbackProperty),
1619 masm()->isolate());
1620 __ TailCallExternalReference(ref, 5, 1);
1621 }
1622 } else { // !compile_followup_inline
1623 // Call the runtime system to load the interceptor.
1624 // Check that the maps haven't changed.
1625 Register holder_reg = CheckPrototypes(object, receiver, interceptor_holder,
1626 scratch1, scratch2, scratch3,
1627 name, miss);
1628 PushInterceptorArguments(masm(), receiver, holder_reg,
1629 name_reg, interceptor_holder);
1630
1631 ExternalReference ref = ExternalReference(
1632 IC_Utility(IC::kLoadPropertyWithInterceptorForLoad), masm()->isolate());
1633 __ TailCallExternalReference(ref, 5, 1);
1634 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001635}
1636
1637
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001638void CallStubCompiler::GenerateNameCheck(Handle<String> name, Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001639 if (kind_ == Code::KEYED_CALL_IC) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001640 __ Branch(miss, ne, a2, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001641 }
ager@chromium.org5c838252010-02-19 08:53:10 +00001642}
1643
1644
lrn@chromium.org7516f052011-03-30 08:52:27 +00001645void CallStubCompiler::GenerateGlobalReceiverCheck(JSObject* object,
1646 JSObject* holder,
1647 String* name,
1648 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001649 ASSERT(holder->IsGlobalObject());
1650
1651 // Get the number of arguments.
1652 const int argc = arguments().immediate();
1653
1654 // Get the receiver from the stack.
1655 __ lw(a0, MemOperand(sp, argc * kPointerSize));
1656
1657 // If the object is the holder then we know that it's a global
1658 // object which can only happen for contextual calls. In this case,
1659 // the receiver cannot be a smi.
1660 if (object != holder) {
1661 __ JumpIfSmi(a0, miss);
1662 }
1663
1664 // Check that the maps haven't changed.
1665 CheckPrototypes(object, a0, holder, a3, a1, t0, name, miss);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001666}
1667
1668
lrn@chromium.org7516f052011-03-30 08:52:27 +00001669void CallStubCompiler::GenerateLoadFunctionFromCell(JSGlobalPropertyCell* cell,
1670 JSFunction* function,
1671 Label* miss) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001672 // Get the value from the cell.
1673 __ li(a3, Operand(Handle<JSGlobalPropertyCell>(cell)));
1674 __ lw(a1, FieldMemOperand(a3, JSGlobalPropertyCell::kValueOffset));
1675
1676 // Check that the cell contains the same function.
1677 if (heap()->InNewSpace(function)) {
1678 // We can't embed a pointer to a function in new space so we have
1679 // to verify that the shared function info is unchanged. This has
1680 // the nice side effect that multiple closures based on the same
1681 // function can all use this call IC. Before we load through the
1682 // function, we have to verify that it still is a function.
1683 __ JumpIfSmi(a1, miss);
1684 __ GetObjectType(a1, a3, a3);
1685 __ Branch(miss, ne, a3, Operand(JS_FUNCTION_TYPE));
1686
1687 // Check the shared function info. Make sure it hasn't changed.
1688 __ li(a3, Handle<SharedFunctionInfo>(function->shared()));
1689 __ lw(t0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1690 __ Branch(miss, ne, t0, Operand(a3));
1691 } else {
1692 __ Branch(miss, ne, a1, Operand(Handle<JSFunction>(function)));
1693 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +00001694}
1695
1696
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001697void CallStubCompiler::GenerateMissBranch() {
1698 Handle<Code> code =
danno@chromium.org40cb8782011-05-25 07:58:50 +00001699 isolate()->stub_cache()->ComputeCallMiss(arguments().immediate(),
1700 kind_,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001701 extra_state_);
1702 __ Jump(code, RelocInfo::CODE_TARGET);
1703}
1704
1705
1706// TODO(kmillikin): Eliminate this function when the stub cache is fully
1707// handlified.
1708MaybeObject* CallStubCompiler::TryGenerateMissBranch() {
1709 MaybeObject* maybe_obj =
1710 isolate()->stub_cache()->TryComputeCallMiss(arguments().immediate(),
1711 kind_,
1712 extra_state_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001713 Object* obj;
1714 if (!maybe_obj->ToObject(&obj)) return maybe_obj;
1715 __ Jump(Handle<Code>(Code::cast(obj)), RelocInfo::CODE_TARGET);
1716 return obj;
ager@chromium.org5c838252010-02-19 08:53:10 +00001717}
1718
1719
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001720Handle<Code> CallStubCompiler::CompileCallField(Handle<JSObject> object,
1721 Handle<JSObject> holder,
lrn@chromium.org7516f052011-03-30 08:52:27 +00001722 int index,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001723 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001724 // ----------- S t a t e -------------
1725 // -- a2 : name
1726 // -- ra : return address
1727 // -----------------------------------
1728 Label miss;
1729
1730 GenerateNameCheck(name, &miss);
1731
1732 const int argc = arguments().immediate();
1733
1734 // Get the receiver of the function from the stack into a0.
1735 __ lw(a0, MemOperand(sp, argc * kPointerSize));
1736 // Check that the receiver isn't a smi.
1737 __ JumpIfSmi(a0, &miss, t0);
1738
1739 // Do the right check and compute the holder register.
1740 Register reg = CheckPrototypes(object, a0, holder, a1, a3, t0, name, &miss);
1741 GenerateFastPropertyLoad(masm(), a1, reg, holder, index);
1742
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001743 GenerateCallFunction(masm(), object, arguments(), &miss, extra_state_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001744
1745 // Handle call cache miss.
1746 __ bind(&miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001747 GenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001748
1749 // Return the generated code.
1750 return GetCode(FIELD, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00001751}
1752
1753
lrn@chromium.org7516f052011-03-30 08:52:27 +00001754MaybeObject* CallStubCompiler::CompileArrayPushCall(Object* object,
1755 JSObject* holder,
1756 JSGlobalPropertyCell* cell,
1757 JSFunction* function,
1758 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001759 // ----------- S t a t e -------------
1760 // -- a2 : name
1761 // -- ra : return address
1762 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
1763 // -- ...
1764 // -- sp[argc * 4] : receiver
1765 // -----------------------------------
1766
1767 // If object is not an array, bail out to regular call.
1768 if (!object->IsJSArray() || cell != NULL) return heap()->undefined_value();
1769
1770 Label miss;
1771
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001772 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001773
1774 Register receiver = a1;
1775
1776 // Get the receiver from the stack.
1777 const int argc = arguments().immediate();
1778 __ lw(receiver, MemOperand(sp, argc * kPointerSize));
1779
1780 // Check that the receiver isn't a smi.
1781 __ JumpIfSmi(receiver, &miss);
1782
1783 // Check that the maps haven't changed.
1784 CheckPrototypes(JSObject::cast(object), receiver,
1785 holder, a3, v0, t0, name, &miss);
1786
1787 if (argc == 0) {
1788 // Nothing to do, just return the length.
1789 __ lw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1790 __ Drop(argc + 1);
1791 __ Ret();
1792 } else {
1793 Label call_builtin;
1794
1795 Register elements = a3;
1796 Register end_elements = t1;
1797
1798 // Get the elements array of the object.
1799 __ lw(elements, FieldMemOperand(receiver, JSArray::kElementsOffset));
1800
1801 // Check that the elements are in fast mode and writable.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001802 __ CheckMap(elements,
1803 v0,
1804 Heap::kFixedArrayMapRootIndex,
1805 &call_builtin,
1806 DONT_DO_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001807
1808 if (argc == 1) { // Otherwise fall through to call the builtin.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001809 Label attempt_to_grow_elements;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001810
1811 // Get the array's length into v0 and calculate new length.
1812 __ lw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1813 STATIC_ASSERT(kSmiTagSize == 1);
1814 STATIC_ASSERT(kSmiTag == 0);
1815 __ Addu(v0, v0, Operand(Smi::FromInt(argc)));
1816
1817 // Get the element's length.
1818 __ lw(t0, FieldMemOperand(elements, FixedArray::kLengthOffset));
1819
1820 // Check if we could survive without allocation.
1821 __ Branch(&attempt_to_grow_elements, gt, v0, Operand(t0));
1822
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001823 // Check if value is a smi.
1824 Label with_write_barrier;
1825 __ lw(t0, MemOperand(sp, (argc - 1) * kPointerSize));
1826 __ JumpIfNotSmi(t0, &with_write_barrier);
1827
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001828 // Save new length.
1829 __ sw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1830
1831 // Push the element.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001832 // We may need a register containing the address end_elements below,
1833 // so write back the value in end_elements.
1834 __ sll(end_elements, v0, kPointerSizeLog2 - kSmiTagSize);
1835 __ Addu(end_elements, elements, end_elements);
1836 const int kEndElementsOffset =
1837 FixedArray::kHeaderSize - kHeapObjectTag - argc * kPointerSize;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001838 __ Addu(end_elements, end_elements, kEndElementsOffset);
1839 __ sw(t0, MemOperand(end_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001840
1841 // Check for a smi.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001842 __ Drop(argc + 1);
1843 __ Ret();
1844
1845 __ bind(&with_write_barrier);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001846
1847 __ lw(t2, FieldMemOperand(receiver, HeapObject::kMapOffset));
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001848 __ CheckFastObjectElements(t2, t2, &call_builtin);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001849
1850 // Save new length.
1851 __ sw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1852
1853 // Push the element.
1854 // We may need a register containing the address end_elements below,
1855 // so write back the value in end_elements.
1856 __ sll(end_elements, v0, kPointerSizeLog2 - kSmiTagSize);
1857 __ Addu(end_elements, elements, end_elements);
1858 __ Addu(end_elements, end_elements, kEndElementsOffset);
1859 __ sw(t0, MemOperand(end_elements));
1860
1861 __ RecordWrite(elements,
1862 end_elements,
1863 t0,
1864 kRAHasNotBeenSaved,
1865 kDontSaveFPRegs,
1866 EMIT_REMEMBERED_SET,
1867 OMIT_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001868 __ Drop(argc + 1);
1869 __ Ret();
1870
1871 __ bind(&attempt_to_grow_elements);
1872 // v0: array's length + 1.
1873 // t0: elements' length.
1874
1875 if (!FLAG_inline_new) {
1876 __ Branch(&call_builtin);
1877 }
1878
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001879 __ lw(a2, MemOperand(sp, (argc - 1) * kPointerSize));
1880 // Growing elements that are SMI-only requires special handling in case
1881 // the new element is non-Smi. For now, delegate to the builtin.
1882 Label no_fast_elements_check;
1883 __ JumpIfSmi(a2, &no_fast_elements_check);
1884 __ lw(t3, FieldMemOperand(receiver, HeapObject::kMapOffset));
1885 __ CheckFastObjectElements(t3, t3, &call_builtin);
1886 __ bind(&no_fast_elements_check);
1887
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001888 ExternalReference new_space_allocation_top =
1889 ExternalReference::new_space_allocation_top_address(
1890 masm()->isolate());
1891 ExternalReference new_space_allocation_limit =
1892 ExternalReference::new_space_allocation_limit_address(
1893 masm()->isolate());
1894
1895 const int kAllocationDelta = 4;
1896 // Load top and check if it is the end of elements.
1897 __ sll(end_elements, v0, kPointerSizeLog2 - kSmiTagSize);
1898 __ Addu(end_elements, elements, end_elements);
1899 __ Addu(end_elements, end_elements, Operand(kEndElementsOffset));
1900 __ li(t3, Operand(new_space_allocation_top));
1901 __ lw(t2, MemOperand(t3));
1902 __ Branch(&call_builtin, ne, end_elements, Operand(t2));
1903
1904 __ li(t5, Operand(new_space_allocation_limit));
1905 __ lw(t5, MemOperand(t5));
1906 __ Addu(t2, t2, Operand(kAllocationDelta * kPointerSize));
1907 __ Branch(&call_builtin, hi, t2, Operand(t5));
1908
1909 // We fit and could grow elements.
1910 // Update new_space_allocation_top.
1911 __ sw(t2, MemOperand(t3));
1912 // Push the argument.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001913 __ sw(a2, MemOperand(end_elements));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001914 // Fill the rest with holes.
1915 __ LoadRoot(t2, Heap::kTheHoleValueRootIndex);
1916 for (int i = 1; i < kAllocationDelta; i++) {
1917 __ sw(t2, MemOperand(end_elements, i * kPointerSize));
1918 }
1919
1920 // Update elements' and array's sizes.
1921 __ sw(v0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1922 __ Addu(t0, t0, Operand(Smi::FromInt(kAllocationDelta)));
1923 __ sw(t0, FieldMemOperand(elements, FixedArray::kLengthOffset));
1924
1925 // Elements are in new space, so write barrier is not required.
1926 __ Drop(argc + 1);
1927 __ Ret();
1928 }
1929 __ bind(&call_builtin);
1930 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPush,
1931 masm()->isolate()),
1932 argc + 1,
1933 1);
1934 }
1935
1936 // Handle call cache miss.
1937 __ bind(&miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001938 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001939 if (maybe_result->IsFailure()) return maybe_result;
1940
1941 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001942 return TryGetCode(function);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001943}
1944
1945
1946MaybeObject* CallStubCompiler::CompileArrayPopCall(Object* object,
1947 JSObject* holder,
1948 JSGlobalPropertyCell* cell,
1949 JSFunction* function,
ager@chromium.org5c838252010-02-19 08:53:10 +00001950 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001951 // ----------- S t a t e -------------
1952 // -- a2 : name
1953 // -- ra : return address
1954 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
1955 // -- ...
1956 // -- sp[argc * 4] : receiver
1957 // -----------------------------------
1958
1959 // If object is not an array, bail out to regular call.
1960 if (!object->IsJSArray() || cell != NULL) return heap()->undefined_value();
1961
1962 Label miss, return_undefined, call_builtin;
1963
1964 Register receiver = a1;
1965 Register elements = a3;
1966
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00001967 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001968
1969 // Get the receiver from the stack.
1970 const int argc = arguments().immediate();
1971 __ lw(receiver, MemOperand(sp, argc * kPointerSize));
1972
1973 // Check that the receiver isn't a smi.
1974 __ JumpIfSmi(receiver, &miss);
1975
1976 // Check that the maps haven't changed.
1977 CheckPrototypes(JSObject::cast(object),
1978 receiver, holder, elements, t0, v0, name, &miss);
1979
1980 // Get the elements array of the object.
1981 __ lw(elements, FieldMemOperand(receiver, JSArray::kElementsOffset));
1982
1983 // Check that the elements are in fast mode and writable.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001984 __ CheckMap(elements,
1985 v0,
1986 Heap::kFixedArrayMapRootIndex,
1987 &call_builtin,
1988 DONT_DO_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001989
1990 // Get the array's length into t0 and calculate new length.
1991 __ lw(t0, FieldMemOperand(receiver, JSArray::kLengthOffset));
1992 __ Subu(t0, t0, Operand(Smi::FromInt(1)));
1993 __ Branch(&return_undefined, lt, t0, Operand(zero_reg));
1994
1995 // Get the last element.
1996 __ LoadRoot(t2, Heap::kTheHoleValueRootIndex);
1997 STATIC_ASSERT(kSmiTagSize == 1);
1998 STATIC_ASSERT(kSmiTag == 0);
1999 // We can't address the last element in one operation. Compute the more
2000 // expensive shift first, and use an offset later on.
2001 __ sll(t1, t0, kPointerSizeLog2 - kSmiTagSize);
2002 __ Addu(elements, elements, t1);
2003 __ lw(v0, MemOperand(elements, FixedArray::kHeaderSize - kHeapObjectTag));
2004 __ Branch(&call_builtin, eq, v0, Operand(t2));
2005
2006 // Set the array's length.
2007 __ sw(t0, FieldMemOperand(receiver, JSArray::kLengthOffset));
2008
2009 // Fill with the hole.
2010 __ sw(t2, MemOperand(elements, FixedArray::kHeaderSize - kHeapObjectTag));
2011 __ Drop(argc + 1);
2012 __ Ret();
2013
2014 __ bind(&return_undefined);
2015 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
2016 __ Drop(argc + 1);
2017 __ Ret();
2018
2019 __ bind(&call_builtin);
2020 __ TailCallExternalReference(ExternalReference(Builtins::c_ArrayPop,
2021 masm()->isolate()),
2022 argc + 1,
2023 1);
2024
2025 // Handle call cache miss.
2026 __ bind(&miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002027 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002028 if (maybe_result->IsFailure()) return maybe_result;
2029
2030 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002031 return TryGetCode(function);
ager@chromium.org5c838252010-02-19 08:53:10 +00002032}
2033
2034
lrn@chromium.org7516f052011-03-30 08:52:27 +00002035MaybeObject* CallStubCompiler::CompileStringCharCodeAtCall(
2036 Object* object,
2037 JSObject* holder,
2038 JSGlobalPropertyCell* cell,
2039 JSFunction* function,
2040 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002041 // ----------- S t a t e -------------
2042 // -- a2 : function name
2043 // -- ra : return address
2044 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
2045 // -- ...
2046 // -- sp[argc * 4] : receiver
2047 // -----------------------------------
2048
2049 // If object is not a string, bail out to regular call.
2050 if (!object->IsString() || cell != NULL) return heap()->undefined_value();
2051
2052 const int argc = arguments().immediate();
2053
2054 Label miss;
2055 Label name_miss;
2056 Label index_out_of_range;
2057
2058 Label* index_out_of_range_label = &index_out_of_range;
2059
danno@chromium.org40cb8782011-05-25 07:58:50 +00002060 if (kind_ == Code::CALL_IC &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002061 (CallICBase::StringStubState::decode(extra_state_) ==
danno@chromium.org40cb8782011-05-25 07:58:50 +00002062 DEFAULT_STRING_STUB)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002063 index_out_of_range_label = &miss;
2064 }
2065
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002066 GenerateNameCheck(Handle<String>(name), &name_miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002067
2068 // Check that the maps starting from the prototype haven't changed.
2069 GenerateDirectLoadGlobalFunctionPrototype(masm(),
2070 Context::STRING_FUNCTION_INDEX,
2071 v0,
2072 &miss);
2073 ASSERT(object != holder);
2074 CheckPrototypes(JSObject::cast(object->GetPrototype()), v0, holder,
2075 a1, a3, t0, name, &miss);
2076
2077 Register receiver = a1;
2078 Register index = t1;
2079 Register scratch = a3;
2080 Register result = v0;
2081 __ lw(receiver, MemOperand(sp, argc * kPointerSize));
2082 if (argc > 0) {
2083 __ lw(index, MemOperand(sp, (argc - 1) * kPointerSize));
2084 } else {
2085 __ LoadRoot(index, Heap::kUndefinedValueRootIndex);
2086 }
2087
2088 StringCharCodeAtGenerator char_code_at_generator(receiver,
2089 index,
2090 scratch,
2091 result,
2092 &miss, // When not a string.
2093 &miss, // When not a number.
2094 index_out_of_range_label,
2095 STRING_INDEX_IS_NUMBER);
2096 char_code_at_generator.GenerateFast(masm());
2097 __ Drop(argc + 1);
2098 __ Ret();
2099
2100 StubRuntimeCallHelper call_helper;
2101 char_code_at_generator.GenerateSlow(masm(), call_helper);
2102
2103 if (index_out_of_range.is_linked()) {
2104 __ bind(&index_out_of_range);
2105 __ LoadRoot(v0, Heap::kNanValueRootIndex);
2106 __ Drop(argc + 1);
2107 __ Ret();
2108 }
2109
2110 __ bind(&miss);
2111 // Restore function name in a2.
2112 __ li(a2, Handle<String>(name));
2113 __ bind(&name_miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002114 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002115 if (maybe_result->IsFailure()) return maybe_result;
2116
2117 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002118 return TryGetCode(function);
ager@chromium.org5c838252010-02-19 08:53:10 +00002119}
2120
2121
lrn@chromium.org7516f052011-03-30 08:52:27 +00002122MaybeObject* CallStubCompiler::CompileStringCharAtCall(
2123 Object* object,
2124 JSObject* holder,
2125 JSGlobalPropertyCell* cell,
2126 JSFunction* function,
2127 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002128 // ----------- S t a t e -------------
2129 // -- a2 : function name
2130 // -- ra : return address
2131 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
2132 // -- ...
2133 // -- sp[argc * 4] : receiver
2134 // -----------------------------------
2135
2136 // If object is not a string, bail out to regular call.
2137 if (!object->IsString() || cell != NULL) return heap()->undefined_value();
2138
2139 const int argc = arguments().immediate();
2140
2141 Label miss;
2142 Label name_miss;
2143 Label index_out_of_range;
2144 Label* index_out_of_range_label = &index_out_of_range;
2145
danno@chromium.org40cb8782011-05-25 07:58:50 +00002146 if (kind_ == Code::CALL_IC &&
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002147 (CallICBase::StringStubState::decode(extra_state_) ==
danno@chromium.org40cb8782011-05-25 07:58:50 +00002148 DEFAULT_STRING_STUB)) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002149 index_out_of_range_label = &miss;
2150 }
2151
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002152 GenerateNameCheck(Handle<String>(name), &name_miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002153
2154 // Check that the maps starting from the prototype haven't changed.
2155 GenerateDirectLoadGlobalFunctionPrototype(masm(),
2156 Context::STRING_FUNCTION_INDEX,
2157 v0,
2158 &miss);
2159 ASSERT(object != holder);
2160 CheckPrototypes(JSObject::cast(object->GetPrototype()), v0, holder,
2161 a1, a3, t0, name, &miss);
2162
2163 Register receiver = v0;
2164 Register index = t1;
2165 Register scratch1 = a1;
2166 Register scratch2 = a3;
2167 Register result = v0;
2168 __ lw(receiver, MemOperand(sp, argc * kPointerSize));
2169 if (argc > 0) {
2170 __ lw(index, MemOperand(sp, (argc - 1) * kPointerSize));
2171 } else {
2172 __ LoadRoot(index, Heap::kUndefinedValueRootIndex);
2173 }
2174
2175 StringCharAtGenerator char_at_generator(receiver,
2176 index,
2177 scratch1,
2178 scratch2,
2179 result,
2180 &miss, // When not a string.
2181 &miss, // When not a number.
2182 index_out_of_range_label,
2183 STRING_INDEX_IS_NUMBER);
2184 char_at_generator.GenerateFast(masm());
2185 __ Drop(argc + 1);
2186 __ Ret();
2187
2188 StubRuntimeCallHelper call_helper;
2189 char_at_generator.GenerateSlow(masm(), call_helper);
2190
2191 if (index_out_of_range.is_linked()) {
2192 __ bind(&index_out_of_range);
2193 __ LoadRoot(v0, Heap::kEmptyStringRootIndex);
2194 __ Drop(argc + 1);
2195 __ Ret();
2196 }
2197
2198 __ bind(&miss);
2199 // Restore function name in a2.
2200 __ li(a2, Handle<String>(name));
2201 __ bind(&name_miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002202 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002203 if (maybe_result->IsFailure()) return maybe_result;
2204
2205 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002206 return TryGetCode(function);
ager@chromium.org5c838252010-02-19 08:53:10 +00002207}
2208
2209
lrn@chromium.org7516f052011-03-30 08:52:27 +00002210MaybeObject* CallStubCompiler::CompileStringFromCharCodeCall(
2211 Object* object,
2212 JSObject* holder,
2213 JSGlobalPropertyCell* cell,
2214 JSFunction* function,
2215 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002216 // ----------- S t a t e -------------
2217 // -- a2 : function name
2218 // -- ra : return address
2219 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
2220 // -- ...
2221 // -- sp[argc * 4] : receiver
2222 // -----------------------------------
2223
2224 const int argc = arguments().immediate();
2225
2226 // If the object is not a JSObject or we got an unexpected number of
2227 // arguments, bail out to the regular call.
2228 if (!object->IsJSObject() || argc != 1) return heap()->undefined_value();
2229
2230 Label miss;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002231 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002232
2233 if (cell == NULL) {
2234 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
2235
2236 STATIC_ASSERT(kSmiTag == 0);
2237 __ JumpIfSmi(a1, &miss);
2238
2239 CheckPrototypes(JSObject::cast(object), a1, holder, v0, a3, t0, name,
2240 &miss);
2241 } else {
2242 ASSERT(cell->value() == function);
2243 GenerateGlobalReceiverCheck(JSObject::cast(object), holder, name, &miss);
2244 GenerateLoadFunctionFromCell(cell, function, &miss);
2245 }
2246
2247 // Load the char code argument.
2248 Register code = a1;
2249 __ lw(code, MemOperand(sp, 0 * kPointerSize));
2250
2251 // Check the code is a smi.
2252 Label slow;
2253 STATIC_ASSERT(kSmiTag == 0);
2254 __ JumpIfNotSmi(code, &slow);
2255
2256 // Convert the smi code to uint16.
2257 __ And(code, code, Operand(Smi::FromInt(0xffff)));
2258
2259 StringCharFromCodeGenerator char_from_code_generator(code, v0);
2260 char_from_code_generator.GenerateFast(masm());
2261 __ Drop(argc + 1);
2262 __ Ret();
2263
2264 StubRuntimeCallHelper call_helper;
2265 char_from_code_generator.GenerateSlow(masm(), call_helper);
2266
2267 // Tail call the full function. We do not have to patch the receiver
2268 // because the function makes no use of it.
2269 __ bind(&slow);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002270 __ InvokeFunction(function, arguments(), JUMP_FUNCTION, CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002271
2272 __ bind(&miss);
2273 // a2: function name.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002274 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002275 if (maybe_result->IsFailure()) return maybe_result;
2276
2277 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002278 return (cell == NULL) ? TryGetCode(function) : TryGetCode(NORMAL, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002279}
2280
2281
lrn@chromium.org7516f052011-03-30 08:52:27 +00002282MaybeObject* CallStubCompiler::CompileMathFloorCall(Object* object,
2283 JSObject* holder,
2284 JSGlobalPropertyCell* cell,
2285 JSFunction* function,
2286 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002287 // ----------- S t a t e -------------
2288 // -- a2 : function name
2289 // -- ra : return address
2290 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
2291 // -- ...
2292 // -- sp[argc * 4] : receiver
2293 // -----------------------------------
2294
2295 if (!CpuFeatures::IsSupported(FPU))
2296 return heap()->undefined_value();
2297 CpuFeatures::Scope scope_fpu(FPU);
2298
2299 const int argc = arguments().immediate();
2300
2301 // If the object is not a JSObject or we got an unexpected number of
2302 // arguments, bail out to the regular call.
2303 if (!object->IsJSObject() || argc != 1) return heap()->undefined_value();
2304
2305 Label miss, slow;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002306 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002307
2308 if (cell == NULL) {
2309 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
2310
2311 STATIC_ASSERT(kSmiTag == 0);
2312 __ JumpIfSmi(a1, &miss);
2313
2314 CheckPrototypes(JSObject::cast(object), a1, holder, a0, a3, t0, name,
2315 &miss);
2316 } else {
2317 ASSERT(cell->value() == function);
2318 GenerateGlobalReceiverCheck(JSObject::cast(object), holder, name, &miss);
2319 GenerateLoadFunctionFromCell(cell, function, &miss);
2320 }
2321
2322 // Load the (only) argument into v0.
2323 __ lw(v0, MemOperand(sp, 0 * kPointerSize));
2324
2325 // If the argument is a smi, just return.
2326 STATIC_ASSERT(kSmiTag == 0);
2327 __ And(t0, v0, Operand(kSmiTagMask));
2328 __ Drop(argc + 1, eq, t0, Operand(zero_reg));
2329 __ Ret(eq, t0, Operand(zero_reg));
2330
danno@chromium.org40cb8782011-05-25 07:58:50 +00002331 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, &slow, DONT_DO_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002332
2333 Label wont_fit_smi, no_fpu_error, restore_fcsr_and_return;
2334
2335 // If fpu is enabled, we use the floor instruction.
2336
2337 // Load the HeapNumber value.
2338 __ ldc1(f0, FieldMemOperand(v0, HeapNumber::kValueOffset));
2339
2340 // Backup FCSR.
2341 __ cfc1(a3, FCSR);
2342 // Clearing FCSR clears the exception mask with no side-effects.
2343 __ ctc1(zero_reg, FCSR);
2344 // Convert the argument to an integer.
2345 __ floor_w_d(f0, f0);
2346
2347 // Start checking for special cases.
2348 // Get the argument exponent and clear the sign bit.
2349 __ lw(t1, FieldMemOperand(v0, HeapNumber::kValueOffset + kPointerSize));
2350 __ And(t2, t1, Operand(~HeapNumber::kSignMask));
2351 __ srl(t2, t2, HeapNumber::kMantissaBitsInTopWord);
2352
2353 // Retrieve FCSR and check for fpu errors.
2354 __ cfc1(t5, FCSR);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002355 __ And(t5, t5, Operand(kFCSRExceptionFlagMask));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002356 __ Branch(&no_fpu_error, eq, t5, Operand(zero_reg));
2357
2358 // Check for NaN, Infinity, and -Infinity.
2359 // They are invariant through a Math.Floor call, so just
2360 // return the original argument.
2361 __ Subu(t3, t2, Operand(HeapNumber::kExponentMask
2362 >> HeapNumber::kMantissaBitsInTopWord));
2363 __ Branch(&restore_fcsr_and_return, eq, t3, Operand(zero_reg));
2364 // We had an overflow or underflow in the conversion. Check if we
2365 // have a big exponent.
2366 // If greater or equal, the argument is already round and in v0.
2367 __ Branch(&restore_fcsr_and_return, ge, t3,
2368 Operand(HeapNumber::kMantissaBits));
2369 __ Branch(&wont_fit_smi);
2370
2371 __ bind(&no_fpu_error);
2372 // Move the result back to v0.
2373 __ mfc1(v0, f0);
2374 // Check if the result fits into a smi.
2375 __ Addu(a1, v0, Operand(0x40000000));
2376 __ Branch(&wont_fit_smi, lt, a1, Operand(zero_reg));
2377 // Tag the result.
2378 STATIC_ASSERT(kSmiTag == 0);
2379 __ sll(v0, v0, kSmiTagSize);
2380
2381 // Check for -0.
2382 __ Branch(&restore_fcsr_and_return, ne, v0, Operand(zero_reg));
2383 // t1 already holds the HeapNumber exponent.
2384 __ And(t0, t1, Operand(HeapNumber::kSignMask));
2385 // If our HeapNumber is negative it was -0, so load its address and return.
2386 // Else v0 is loaded with 0, so we can also just return.
2387 __ Branch(&restore_fcsr_and_return, eq, t0, Operand(zero_reg));
2388 __ lw(v0, MemOperand(sp, 0 * kPointerSize));
2389
2390 __ bind(&restore_fcsr_and_return);
2391 // Restore FCSR and return.
2392 __ ctc1(a3, FCSR);
2393
2394 __ Drop(argc + 1);
2395 __ Ret();
2396
2397 __ bind(&wont_fit_smi);
2398 // Restore FCSR and fall to slow case.
2399 __ ctc1(a3, FCSR);
2400
2401 __ bind(&slow);
2402 // Tail call the full function. We do not have to patch the receiver
2403 // because the function makes no use of it.
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002404 __ InvokeFunction(function, arguments(), JUMP_FUNCTION, CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002405
2406 __ bind(&miss);
2407 // a2: function name.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002408 MaybeObject* obj = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002409 if (obj->IsFailure()) return obj;
2410
2411 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002412 return (cell == NULL) ? TryGetCode(function) : TryGetCode(NORMAL, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002413}
2414
2415
lrn@chromium.org7516f052011-03-30 08:52:27 +00002416MaybeObject* CallStubCompiler::CompileMathAbsCall(Object* object,
2417 JSObject* holder,
2418 JSGlobalPropertyCell* cell,
2419 JSFunction* function,
2420 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002421 // ----------- S t a t e -------------
2422 // -- a2 : function name
2423 // -- ra : return address
2424 // -- sp[(argc - n - 1) * 4] : arg[n] (zero-based)
2425 // -- ...
2426 // -- sp[argc * 4] : receiver
2427 // -----------------------------------
2428
2429 const int argc = arguments().immediate();
2430
2431 // If the object is not a JSObject or we got an unexpected number of
2432 // arguments, bail out to the regular call.
2433 if (!object->IsJSObject() || argc != 1) return heap()->undefined_value();
2434
2435 Label miss;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002436 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002437
2438 if (cell == NULL) {
2439 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
2440
2441 STATIC_ASSERT(kSmiTag == 0);
2442 __ JumpIfSmi(a1, &miss);
2443
2444 CheckPrototypes(JSObject::cast(object), a1, holder, v0, a3, t0, name,
2445 &miss);
2446 } else {
2447 ASSERT(cell->value() == function);
2448 GenerateGlobalReceiverCheck(JSObject::cast(object), holder, name, &miss);
2449 GenerateLoadFunctionFromCell(cell, function, &miss);
2450 }
2451
2452 // Load the (only) argument into v0.
2453 __ lw(v0, MemOperand(sp, 0 * kPointerSize));
2454
2455 // Check if the argument is a smi.
2456 Label not_smi;
2457 STATIC_ASSERT(kSmiTag == 0);
2458 __ JumpIfNotSmi(v0, &not_smi);
2459
2460 // Do bitwise not or do nothing depending on the sign of the
2461 // argument.
2462 __ sra(t0, v0, kBitsPerInt - 1);
2463 __ Xor(a1, v0, t0);
2464
2465 // Add 1 or do nothing depending on the sign of the argument.
2466 __ Subu(v0, a1, t0);
2467
2468 // If the result is still negative, go to the slow case.
2469 // This only happens for the most negative smi.
2470 Label slow;
2471 __ Branch(&slow, lt, v0, Operand(zero_reg));
2472
2473 // Smi case done.
2474 __ Drop(argc + 1);
2475 __ Ret();
2476
2477 // Check if the argument is a heap number and load its exponent and
2478 // sign.
2479 __ bind(&not_smi);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002480 __ CheckMap(v0, a1, Heap::kHeapNumberMapRootIndex, &slow, DONT_DO_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002481 __ lw(a1, FieldMemOperand(v0, HeapNumber::kExponentOffset));
2482
2483 // Check the sign of the argument. If the argument is positive,
2484 // just return it.
2485 Label negative_sign;
2486 __ And(t0, a1, Operand(HeapNumber::kSignMask));
2487 __ Branch(&negative_sign, ne, t0, Operand(zero_reg));
2488 __ Drop(argc + 1);
2489 __ Ret();
2490
2491 // If the argument is negative, clear the sign, and return a new
2492 // number.
2493 __ bind(&negative_sign);
2494 __ Xor(a1, a1, Operand(HeapNumber::kSignMask));
2495 __ lw(a3, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
2496 __ LoadRoot(t2, Heap::kHeapNumberMapRootIndex);
2497 __ AllocateHeapNumber(v0, t0, t1, t2, &slow);
2498 __ sw(a1, FieldMemOperand(v0, HeapNumber::kExponentOffset));
2499 __ sw(a3, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
2500 __ Drop(argc + 1);
2501 __ Ret();
2502
2503 // Tail call the full function. We do not have to patch the receiver
2504 // because the function makes no use of it.
2505 __ bind(&slow);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002506 __ InvokeFunction(function, arguments(), JUMP_FUNCTION, CALL_AS_METHOD);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002507
2508 __ bind(&miss);
2509 // a2: function name.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002510 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002511 if (maybe_result->IsFailure()) return maybe_result;
2512
2513 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002514 return (cell == NULL) ? TryGetCode(function) : TryGetCode(NORMAL, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002515}
2516
2517
lrn@chromium.org7516f052011-03-30 08:52:27 +00002518MaybeObject* CallStubCompiler::CompileFastApiCall(
2519 const CallOptimization& optimization,
2520 Object* object,
2521 JSObject* holder,
2522 JSGlobalPropertyCell* cell,
2523 JSFunction* function,
2524 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002525
danno@chromium.org40cb8782011-05-25 07:58:50 +00002526 Counters* counters = isolate()->counters();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002527
2528 ASSERT(optimization.is_simple_api_call());
2529 // Bail out if object is a global object as we don't want to
2530 // repatch it to global receiver.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002531 if (object->IsGlobalObject()) return heap()->undefined_value();
2532 if (cell != NULL) return heap()->undefined_value();
ager@chromium.orgea91cc52011-05-23 06:06:11 +00002533 if (!object->IsJSObject()) return heap()->undefined_value();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002534 int depth = optimization.GetPrototypeDepthOfExpectedType(
2535 JSObject::cast(object), holder);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002536 if (depth == kInvalidProtoDepth) return heap()->undefined_value();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002537
2538 Label miss, miss_before_stack_reserved;
2539
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002540 GenerateNameCheck(Handle<String>(name), &miss_before_stack_reserved);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002541
2542 // Get the receiver from the stack.
2543 const int argc = arguments().immediate();
2544 __ lw(a1, MemOperand(sp, argc * kPointerSize));
2545
2546 // Check that the receiver isn't a smi.
2547 __ JumpIfSmi(a1, &miss_before_stack_reserved);
2548
2549 __ IncrementCounter(counters->call_const(), 1, a0, a3);
2550 __ IncrementCounter(counters->call_const_fast_api(), 1, a0, a3);
2551
2552 ReserveSpaceForFastApiCall(masm(), a0);
2553
2554 // Check that the maps haven't changed and find a Holder as a side effect.
2555 CheckPrototypes(JSObject::cast(object), a1, holder, a0, a3, t0, name,
2556 depth, &miss);
2557
2558 MaybeObject* result = GenerateFastApiDirectCall(masm(), optimization, argc);
2559 if (result->IsFailure()) return result;
2560
2561 __ bind(&miss);
2562 FreeSpaceForFastApiCall(masm());
2563
2564 __ bind(&miss_before_stack_reserved);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002565 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002566 if (maybe_result->IsFailure()) return maybe_result;
2567
2568 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002569 return TryGetCode(function);
ager@chromium.org5c838252010-02-19 08:53:10 +00002570}
2571
2572
lrn@chromium.org7516f052011-03-30 08:52:27 +00002573MaybeObject* CallStubCompiler::CompileCallConstant(Object* object,
ager@chromium.org5c838252010-02-19 08:53:10 +00002574 JSObject* holder,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002575 JSFunction* function,
2576 String* name,
2577 CheckType check) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002578 // ----------- S t a t e -------------
2579 // -- a2 : name
2580 // -- ra : return address
2581 // -----------------------------------
2582 if (HasCustomCallGenerator(function)) {
2583 MaybeObject* maybe_result = CompileCustomCall(
2584 object, holder, NULL, function, name);
2585 Object* result;
2586 if (!maybe_result->ToObject(&result)) return maybe_result;
2587 // Undefined means bail out to regular compiler.
2588 if (!result->IsUndefined()) return result;
2589 }
2590
2591 Label miss;
2592
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002593 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002594
2595 // Get the receiver from the stack.
2596 const int argc = arguments().immediate();
2597 __ lw(a1, MemOperand(sp, argc * kPointerSize));
2598
2599 // Check that the receiver isn't a smi.
2600 if (check != NUMBER_CHECK) {
2601 __ And(t1, a1, Operand(kSmiTagMask));
2602 __ Branch(&miss, eq, t1, Operand(zero_reg));
2603 }
2604
2605 // Make sure that it's okay not to patch the on stack receiver
2606 // unless we're doing a receiver map check.
2607 ASSERT(!object->IsGlobalObject() || check == RECEIVER_MAP_CHECK);
2608
2609 SharedFunctionInfo* function_info = function->shared();
2610 switch (check) {
2611 case RECEIVER_MAP_CHECK:
2612 __ IncrementCounter(masm()->isolate()->counters()->call_const(),
2613 1, a0, a3);
2614
2615 // Check that the maps haven't changed.
2616 CheckPrototypes(JSObject::cast(object), a1, holder, a0, a3, t0, name,
2617 &miss);
2618
2619 // Patch the receiver on the stack with the global proxy if
2620 // necessary.
2621 if (object->IsGlobalObject()) {
2622 __ lw(a3, FieldMemOperand(a1, GlobalObject::kGlobalReceiverOffset));
2623 __ sw(a3, MemOperand(sp, argc * kPointerSize));
2624 }
2625 break;
2626
2627 case STRING_CHECK:
2628 if (!function->IsBuiltin() && !function_info->strict_mode()) {
2629 // Calling non-strict non-builtins with a value as the receiver
2630 // requires boxing.
2631 __ jmp(&miss);
2632 } else {
2633 // Check that the object is a two-byte string or a symbol.
2634 __ GetObjectType(a1, a3, a3);
2635 __ Branch(&miss, Ugreater_equal, a3, Operand(FIRST_NONSTRING_TYPE));
2636 // Check that the maps starting from the prototype haven't changed.
2637 GenerateDirectLoadGlobalFunctionPrototype(
2638 masm(), Context::STRING_FUNCTION_INDEX, a0, &miss);
2639 CheckPrototypes(JSObject::cast(object->GetPrototype()), a0, holder, a3,
2640 a1, t0, name, &miss);
2641 }
2642 break;
2643
2644 case NUMBER_CHECK: {
2645 if (!function->IsBuiltin() && !function_info->strict_mode()) {
2646 // Calling non-strict non-builtins with a value as the receiver
2647 // requires boxing.
2648 __ jmp(&miss);
2649 } else {
2650 Label fast;
2651 // Check that the object is a smi or a heap number.
2652 __ And(t1, a1, Operand(kSmiTagMask));
2653 __ Branch(&fast, eq, t1, Operand(zero_reg));
2654 __ GetObjectType(a1, a0, a0);
2655 __ Branch(&miss, ne, a0, Operand(HEAP_NUMBER_TYPE));
2656 __ bind(&fast);
2657 // Check that the maps starting from the prototype haven't changed.
2658 GenerateDirectLoadGlobalFunctionPrototype(
2659 masm(), Context::NUMBER_FUNCTION_INDEX, a0, &miss);
2660 CheckPrototypes(JSObject::cast(object->GetPrototype()), a0, holder, a3,
2661 a1, t0, name, &miss);
2662 }
2663 break;
2664 }
2665
2666 case BOOLEAN_CHECK: {
2667 if (!function->IsBuiltin() && !function_info->strict_mode()) {
2668 // Calling non-strict non-builtins with a value as the receiver
2669 // requires boxing.
2670 __ jmp(&miss);
2671 } else {
2672 Label fast;
2673 // Check that the object is a boolean.
2674 __ LoadRoot(t0, Heap::kTrueValueRootIndex);
2675 __ Branch(&fast, eq, a1, Operand(t0));
2676 __ LoadRoot(t0, Heap::kFalseValueRootIndex);
2677 __ Branch(&miss, ne, a1, Operand(t0));
2678 __ bind(&fast);
2679 // Check that the maps starting from the prototype haven't changed.
2680 GenerateDirectLoadGlobalFunctionPrototype(
2681 masm(), Context::BOOLEAN_FUNCTION_INDEX, a0, &miss);
2682 CheckPrototypes(JSObject::cast(object->GetPrototype()), a0, holder, a3,
2683 a1, t0, name, &miss);
2684 }
2685 break;
2686 }
2687
2688 default:
2689 UNREACHABLE();
2690 }
2691
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002692 CallKind call_kind = CallICBase::Contextual::decode(extra_state_)
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002693 ? CALL_AS_FUNCTION
2694 : CALL_AS_METHOD;
2695 __ InvokeFunction(function, arguments(), JUMP_FUNCTION, call_kind);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002696
2697 // Handle call cache miss.
2698 __ bind(&miss);
2699
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002700 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002701 if (maybe_result->IsFailure()) return maybe_result;
2702
2703 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002704 return TryGetCode(function);
ager@chromium.org5c838252010-02-19 08:53:10 +00002705}
2706
2707
lrn@chromium.org7516f052011-03-30 08:52:27 +00002708MaybeObject* CallStubCompiler::CompileCallInterceptor(JSObject* object,
ager@chromium.org5c838252010-02-19 08:53:10 +00002709 JSObject* holder,
2710 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002711 // ----------- S t a t e -------------
2712 // -- a2 : name
2713 // -- ra : return address
2714 // -----------------------------------
2715
2716 Label miss;
2717
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002718 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002719
2720 // Get the number of arguments.
2721 const int argc = arguments().immediate();
2722
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002723 LookupResult lookup(isolate());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002724 LookupPostInterceptor(holder, name, &lookup);
2725
2726 // Get the receiver from the stack.
2727 __ lw(a1, MemOperand(sp, argc * kPointerSize));
2728
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002729 CallInterceptorCompiler compiler(this, arguments(), a2, extra_state_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002730 MaybeObject* result = compiler.Compile(masm(),
2731 object,
2732 holder,
2733 name,
2734 &lookup,
2735 a1,
2736 a3,
2737 t0,
2738 a0,
2739 &miss);
2740 if (result->IsFailure()) {
2741 return result;
2742 }
2743
2744 // Move returned value, the function to call, to a1.
2745 __ mov(a1, v0);
2746 // Restore receiver.
2747 __ lw(a0, MemOperand(sp, argc * kPointerSize));
2748
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002749 GenerateCallFunction(masm(), Handle<Object>(object), arguments(), &miss,
2750 extra_state_);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002751
2752 // Handle call cache miss.
2753 __ bind(&miss);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002754 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002755 if (maybe_result->IsFailure()) return maybe_result;
2756
2757 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002758 return TryGetCode(INTERCEPTOR, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002759}
2760
2761
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002762MaybeObject* CallStubCompiler::CompileCallGlobal(JSObject* object,
2763 GlobalObject* holder,
2764 JSGlobalPropertyCell* cell,
2765 JSFunction* function,
2766 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002767 // ----------- S t a t e -------------
2768 // -- a2 : name
2769 // -- ra : return address
2770 // -----------------------------------
2771
2772 if (HasCustomCallGenerator(function)) {
2773 MaybeObject* maybe_result = CompileCustomCall(
2774 object, holder, cell, function, name);
2775 Object* result;
2776 if (!maybe_result->ToObject(&result)) return maybe_result;
2777 // Undefined means bail out to regular compiler.
2778 if (!result->IsUndefined()) return result;
2779 }
2780
2781 Label miss;
2782
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002783 GenerateNameCheck(Handle<String>(name), &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002784
2785 // Get the number of arguments.
2786 const int argc = arguments().immediate();
2787
2788 GenerateGlobalReceiverCheck(object, holder, name, &miss);
2789 GenerateLoadFunctionFromCell(cell, function, &miss);
2790
2791 // Patch the receiver on the stack with the global proxy if
2792 // necessary.
2793 if (object->IsGlobalObject()) {
2794 __ lw(a3, FieldMemOperand(a0, GlobalObject::kGlobalReceiverOffset));
2795 __ sw(a3, MemOperand(sp, argc * kPointerSize));
2796 }
2797
2798 // Setup the context (function already in r1).
2799 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
2800
2801 // Jump to the cached code (tail call).
2802 Counters* counters = masm()->isolate()->counters();
2803 __ IncrementCounter(counters->call_global_inline(), 1, a3, t0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002804 Handle<Code> code(function->code());
2805 ParameterCount expected(function->shared()->formal_parameter_count());
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002806 CallKind call_kind = CallICBase::Contextual::decode(extra_state_)
danno@chromium.org40cb8782011-05-25 07:58:50 +00002807 ? CALL_AS_FUNCTION
2808 : CALL_AS_METHOD;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002809 // We call indirectly through the code field in the function to
2810 // allow recompilation to take effect without changing any of the
2811 // call sites.
2812 __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
2813 __ InvokeCode(a3, expected, arguments(), JUMP_FUNCTION,
2814 NullCallWrapper(), call_kind);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002815
2816 // Handle call cache miss.
2817 __ bind(&miss);
2818 __ IncrementCounter(counters->call_global_inline_miss(), 1, a1, a3);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002819 MaybeObject* maybe_result = TryGenerateMissBranch();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002820 if (maybe_result->IsFailure()) return maybe_result;
2821
2822 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002823 return TryGetCode(NORMAL, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002824}
2825
2826
lrn@chromium.org7516f052011-03-30 08:52:27 +00002827MaybeObject* StoreStubCompiler::CompileStoreField(JSObject* object,
ager@chromium.org5c838252010-02-19 08:53:10 +00002828 int index,
2829 Map* transition,
2830 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002831 // ----------- S t a t e -------------
2832 // -- a0 : value
2833 // -- a1 : receiver
2834 // -- a2 : name
2835 // -- ra : return address
2836 // -----------------------------------
2837 Label miss;
2838
2839 // Name register might be clobbered.
2840 GenerateStoreField(masm(),
2841 object,
2842 index,
2843 transition,
2844 a1, a2, a3,
2845 &miss);
2846 __ bind(&miss);
2847 __ li(a2, Operand(Handle<String>(name))); // Restore name.
2848 Handle<Code> ic = masm()->isolate()->builtins()->Builtins::StoreIC_Miss();
2849 __ Jump(ic, RelocInfo::CODE_TARGET);
2850
2851 // Return the generated code.
2852 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002853}
2854
2855
lrn@chromium.org7516f052011-03-30 08:52:27 +00002856MaybeObject* StoreStubCompiler::CompileStoreCallback(JSObject* object,
2857 AccessorInfo* callback,
2858 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002859 // ----------- S t a t e -------------
2860 // -- a0 : value
2861 // -- a1 : receiver
2862 // -- a2 : name
2863 // -- ra : return address
2864 // -----------------------------------
2865 Label miss;
2866
2867 // Check that the object isn't a smi.
2868 __ JumpIfSmi(a1, &miss);
2869
2870 // Check that the map of the object hasn't changed.
2871 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
2872 __ Branch(&miss, ne, a3, Operand(Handle<Map>(object->map())));
2873
2874 // Perform global security token check if needed.
2875 if (object->IsJSGlobalProxy()) {
2876 __ CheckAccessGlobalProxy(a1, a3, &miss);
2877 }
2878
2879 // Stub never generated for non-global objects that require access
2880 // checks.
2881 ASSERT(object->IsJSGlobalProxy() || !object->IsAccessCheckNeeded());
2882
2883 __ push(a1); // Receiver.
2884 __ li(a3, Operand(Handle<AccessorInfo>(callback))); // Callback info.
2885 __ Push(a3, a2, a0);
2886
2887 // Do tail-call to the runtime system.
2888 ExternalReference store_callback_property =
2889 ExternalReference(IC_Utility(IC::kStoreCallbackProperty),
2890 masm()->isolate());
2891 __ TailCallExternalReference(store_callback_property, 4, 1);
2892
2893 // Handle store cache miss.
2894 __ bind(&miss);
2895 Handle<Code> ic = masm()->isolate()->builtins()->StoreIC_Miss();
2896 __ Jump(ic, RelocInfo::CODE_TARGET);
2897
2898 // Return the generated code.
2899 return GetCode(CALLBACKS, name);
ager@chromium.org5c838252010-02-19 08:53:10 +00002900}
2901
2902
lrn@chromium.org7516f052011-03-30 08:52:27 +00002903MaybeObject* StoreStubCompiler::CompileStoreInterceptor(JSObject* receiver,
2904 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002905 // ----------- S t a t e -------------
2906 // -- a0 : value
2907 // -- a1 : receiver
2908 // -- a2 : name
2909 // -- ra : return address
2910 // -----------------------------------
2911 Label miss;
2912
2913 // Check that the object isn't a smi.
2914 __ JumpIfSmi(a1, &miss);
2915
2916 // Check that the map of the object hasn't changed.
2917 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
2918 __ Branch(&miss, ne, a3, Operand(Handle<Map>(receiver->map())));
2919
2920 // Perform global security token check if needed.
2921 if (receiver->IsJSGlobalProxy()) {
2922 __ CheckAccessGlobalProxy(a1, a3, &miss);
2923 }
2924
2925 // Stub is never generated for non-global objects that require access
2926 // checks.
2927 ASSERT(receiver->IsJSGlobalProxy() || !receiver->IsAccessCheckNeeded());
2928
2929 __ Push(a1, a2, a0); // Receiver, name, value.
2930
2931 __ li(a0, Operand(Smi::FromInt(strict_mode_)));
2932 __ push(a0); // Strict mode.
2933
2934 // Do tail-call to the runtime system.
2935 ExternalReference store_ic_property =
2936 ExternalReference(IC_Utility(IC::kStoreInterceptorProperty),
2937 masm()->isolate());
2938 __ TailCallExternalReference(store_ic_property, 4, 1);
2939
2940 // Handle store cache miss.
2941 __ bind(&miss);
2942 Handle<Code> ic = masm()->isolate()->builtins()->Builtins::StoreIC_Miss();
2943 __ Jump(ic, RelocInfo::CODE_TARGET);
2944
2945 // Return the generated code.
2946 return GetCode(INTERCEPTOR, name);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00002947}
2948
2949
lrn@chromium.org7516f052011-03-30 08:52:27 +00002950MaybeObject* StoreStubCompiler::CompileStoreGlobal(GlobalObject* object,
2951 JSGlobalPropertyCell* cell,
2952 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002953 // ----------- S t a t e -------------
2954 // -- a0 : value
2955 // -- a1 : receiver
2956 // -- a2 : name
2957 // -- ra : return address
2958 // -----------------------------------
2959 Label miss;
2960
2961 // Check that the map of the global has not changed.
2962 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
2963 __ Branch(&miss, ne, a3, Operand(Handle<Map>(object->map())));
2964
2965 // Check that the value in the cell is not the hole. If it is, this
2966 // cell could have been deleted and reintroducing the global needs
2967 // to update the property details in the property dictionary of the
2968 // global object. We bail out to the runtime system to do that.
2969 __ li(t0, Operand(Handle<JSGlobalPropertyCell>(cell)));
2970 __ LoadRoot(t1, Heap::kTheHoleValueRootIndex);
2971 __ lw(t2, FieldMemOperand(t0, JSGlobalPropertyCell::kValueOffset));
2972 __ Branch(&miss, eq, t1, Operand(t2));
2973
2974 // Store the value in the cell.
2975 __ sw(a0, FieldMemOperand(t0, JSGlobalPropertyCell::kValueOffset));
2976 __ mov(v0, a0); // Stored value must be returned in v0.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002977
2978 // This trashes a0 but the value is returned in v0 anyway.
2979 __ RecordWriteField(t0,
2980 JSGlobalPropertyCell::kValueOffset,
2981 a0,
2982 a2,
2983 kRAHasNotBeenSaved,
2984 kDontSaveFPRegs,
2985 OMIT_REMEMBERED_SET);
2986
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00002987 Counters* counters = masm()->isolate()->counters();
2988 __ IncrementCounter(counters->named_store_global_inline(), 1, a1, a3);
2989 __ Ret();
2990
2991 // Handle store cache miss.
2992 __ bind(&miss);
2993 __ IncrementCounter(counters->named_store_global_inline_miss(), 1, a1, a3);
2994 Handle<Code> ic = masm()->isolate()->builtins()->StoreIC_Miss();
2995 __ Jump(ic, RelocInfo::CODE_TARGET);
2996
2997 // Return the generated code.
2998 return GetCode(NORMAL, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002999}
3000
3001
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003002Handle<Code> LoadStubCompiler::CompileLoadNonexistent(Handle<String> name,
3003 Handle<JSObject> object,
3004 Handle<JSObject> last) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003005 // ----------- S t a t e -------------
3006 // -- a0 : receiver
3007 // -- ra : return address
3008 // -----------------------------------
3009 Label miss;
3010
3011 // Check that the receiver is not a smi.
3012 __ JumpIfSmi(a0, &miss);
3013
3014 // Check the maps of the full prototype chain.
3015 CheckPrototypes(object, a0, last, a3, a1, t0, name, &miss);
3016
3017 // If the last object in the prototype chain is a global object,
3018 // check that the global property cell is empty.
3019 if (last->IsGlobalObject()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003020 GenerateCheckPropertyCell(
3021 masm(), Handle<GlobalObject>::cast(last), name, a1, &miss);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003022 }
3023
3024 // Return undefined if maps of the full prototype chain is still the same.
3025 __ LoadRoot(v0, Heap::kUndefinedValueRootIndex);
3026 __ Ret();
3027
3028 __ bind(&miss);
3029 GenerateLoadMiss(masm(), Code::LOAD_IC);
3030
3031 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003032 return GetCode(NONEXISTENT, factory()->empty_string());
lrn@chromium.org7516f052011-03-30 08:52:27 +00003033}
3034
3035
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003036Handle<Code> LoadStubCompiler::CompileLoadField(Handle<JSObject> object,
3037 Handle<JSObject> holder,
lrn@chromium.org7516f052011-03-30 08:52:27 +00003038 int index,
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003039 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003040 // ----------- S t a t e -------------
3041 // -- a0 : receiver
3042 // -- a2 : name
3043 // -- ra : return address
3044 // -----------------------------------
3045 Label miss;
3046
3047 __ mov(v0, a0);
3048
3049 GenerateLoadField(object, holder, v0, a3, a1, t0, index, name, &miss);
3050 __ bind(&miss);
3051 GenerateLoadMiss(masm(), Code::LOAD_IC);
3052
3053 // Return the generated code.
3054 return GetCode(FIELD, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003055}
3056
3057
3058MaybeObject* LoadStubCompiler::CompileLoadCallback(String* name,
3059 JSObject* object,
3060 JSObject* holder,
3061 AccessorInfo* callback) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003062 // ----------- S t a t e -------------
3063 // -- a0 : receiver
3064 // -- a2 : name
3065 // -- ra : return address
3066 // -----------------------------------
3067 Label miss;
3068
3069 MaybeObject* result = GenerateLoadCallback(object, holder, a0, a2, a3, a1, t0,
3070 callback, name, &miss);
3071 if (result->IsFailure()) {
3072 miss.Unuse();
3073 return result;
3074 }
3075
3076 __ bind(&miss);
3077 GenerateLoadMiss(masm(), Code::LOAD_IC);
3078
3079 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003080 return TryGetCode(CALLBACKS, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003081}
3082
3083
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003084Handle<Code> LoadStubCompiler::CompileLoadConstant(Handle<JSObject> object,
3085 Handle<JSObject> holder,
3086 Handle<Object> value,
3087 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003088 // ----------- S t a t e -------------
3089 // -- a0 : receiver
3090 // -- a2 : name
3091 // -- ra : return address
3092 // -----------------------------------
3093 Label miss;
3094
3095 GenerateLoadConstant(object, holder, a0, a3, a1, t0, value, name, &miss);
3096 __ bind(&miss);
3097 GenerateLoadMiss(masm(), Code::LOAD_IC);
3098
3099 // Return the generated code.
3100 return GetCode(CONSTANT_FUNCTION, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003101}
3102
3103
3104MaybeObject* LoadStubCompiler::CompileLoadInterceptor(JSObject* object,
3105 JSObject* holder,
3106 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003107 // ----------- S t a t e -------------
3108 // -- a0 : receiver
3109 // -- a2 : name
3110 // -- ra : return address
3111 // -- [sp] : receiver
3112 // -----------------------------------
3113 Label miss;
3114
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003115 LookupResult lookup(isolate());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003116 LookupPostInterceptor(holder, name, &lookup);
3117 GenerateLoadInterceptor(object,
3118 holder,
3119 &lookup,
3120 a0,
3121 a2,
3122 a3,
3123 a1,
3124 t0,
3125 name,
3126 &miss);
3127 __ bind(&miss);
3128 GenerateLoadMiss(masm(), Code::LOAD_IC);
3129
3130 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003131 return TryGetCode(INTERCEPTOR, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003132}
3133
3134
3135MaybeObject* LoadStubCompiler::CompileLoadGlobal(JSObject* object,
3136 GlobalObject* holder,
3137 JSGlobalPropertyCell* cell,
3138 String* name,
3139 bool is_dont_delete) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003140 // ----------- S t a t e -------------
3141 // -- a0 : receiver
3142 // -- a2 : name
3143 // -- ra : return address
3144 // -----------------------------------
3145 Label miss;
3146
3147 // If the object is the holder then we know that it's a global
3148 // object which can only happen for contextual calls. In this case,
3149 // the receiver cannot be a smi.
3150 if (object != holder) {
3151 __ And(t0, a0, Operand(kSmiTagMask));
3152 __ Branch(&miss, eq, t0, Operand(zero_reg));
3153 }
3154
3155 // Check that the map of the global has not changed.
3156 CheckPrototypes(object, a0, holder, a3, t0, a1, name, &miss);
3157
3158 // Get the value from the cell.
3159 __ li(a3, Operand(Handle<JSGlobalPropertyCell>(cell)));
3160 __ lw(t0, FieldMemOperand(a3, JSGlobalPropertyCell::kValueOffset));
3161
3162 // Check for deleted property if property can actually be deleted.
3163 if (!is_dont_delete) {
3164 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
3165 __ Branch(&miss, eq, t0, Operand(at));
3166 }
3167
3168 __ mov(v0, t0);
3169 Counters* counters = masm()->isolate()->counters();
3170 __ IncrementCounter(counters->named_load_global_stub(), 1, a1, a3);
3171 __ Ret();
3172
3173 __ bind(&miss);
3174 __ IncrementCounter(counters->named_load_global_stub_miss(), 1, a1, a3);
3175 GenerateLoadMiss(masm(), Code::LOAD_IC);
3176
3177 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003178 return TryGetCode(NORMAL, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003179}
3180
3181
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003182Handle<Code> KeyedLoadStubCompiler::CompileLoadField(Handle<String> name,
3183 Handle<JSObject> receiver,
3184 Handle<JSObject> holder,
lrn@chromium.org7516f052011-03-30 08:52:27 +00003185 int index) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003186 // ----------- S t a t e -------------
3187 // -- ra : return address
3188 // -- a0 : key
3189 // -- a1 : receiver
3190 // -----------------------------------
3191 Label miss;
3192
3193 // Check the key is the cached one.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003194 __ Branch(&miss, ne, a0, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003195
3196 GenerateLoadField(receiver, holder, a1, a2, a3, t0, index, name, &miss);
3197 __ bind(&miss);
3198 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3199
3200 return GetCode(FIELD, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003201}
3202
3203
3204MaybeObject* KeyedLoadStubCompiler::CompileLoadCallback(
3205 String* name,
3206 JSObject* receiver,
3207 JSObject* holder,
3208 AccessorInfo* callback) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003209 // ----------- S t a t e -------------
3210 // -- ra : return address
3211 // -- a0 : key
3212 // -- a1 : receiver
3213 // -----------------------------------
3214 Label miss;
3215
3216 // Check the key is the cached one.
3217 __ Branch(&miss, ne, a0, Operand(Handle<String>(name)));
3218
3219 MaybeObject* result = GenerateLoadCallback(receiver, holder, a1, a0, a2, a3,
3220 t0, callback, name, &miss);
3221 if (result->IsFailure()) {
3222 miss.Unuse();
3223 return result;
3224 }
3225
3226 __ bind(&miss);
3227 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3228
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003229 return TryGetCode(CALLBACKS, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003230}
3231
3232
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003233Handle<Code> KeyedLoadStubCompiler::CompileLoadConstant(
3234 Handle<String> name,
3235 Handle<JSObject> receiver,
3236 Handle<JSObject> holder,
3237 Handle<Object> value) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003238 // ----------- S t a t e -------------
3239 // -- ra : return address
3240 // -- a0 : key
3241 // -- a1 : receiver
3242 // -----------------------------------
3243 Label miss;
3244
3245 // Check the key is the cached one.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003246 __ Branch(&miss, ne, a0, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003247
3248 GenerateLoadConstant(receiver, holder, a1, a2, a3, t0, value, name, &miss);
3249 __ bind(&miss);
3250 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3251
3252 // Return the generated code.
3253 return GetCode(CONSTANT_FUNCTION, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003254}
3255
3256
3257MaybeObject* KeyedLoadStubCompiler::CompileLoadInterceptor(JSObject* receiver,
3258 JSObject* holder,
3259 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003260 // ----------- S t a t e -------------
3261 // -- ra : return address
3262 // -- a0 : key
3263 // -- a1 : receiver
3264 // -----------------------------------
3265 Label miss;
3266
3267 // Check the key is the cached one.
3268 __ Branch(&miss, ne, a0, Operand(Handle<String>(name)));
3269
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003270 LookupResult lookup(isolate());
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003271 LookupPostInterceptor(holder, name, &lookup);
3272 GenerateLoadInterceptor(receiver,
3273 holder,
3274 &lookup,
3275 a1,
3276 a0,
3277 a2,
3278 a3,
3279 t0,
3280 name,
3281 &miss);
3282 __ bind(&miss);
3283 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3284
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003285 return TryGetCode(INTERCEPTOR, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003286}
3287
3288
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003289Handle<Code> KeyedLoadStubCompiler::CompileLoadArrayLength(
3290 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003291 // ----------- S t a t e -------------
3292 // -- ra : return address
3293 // -- a0 : key
3294 // -- a1 : receiver
3295 // -----------------------------------
3296 Label miss;
3297
3298 // Check the key is the cached one.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003299 __ Branch(&miss, ne, a0, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003300
3301 GenerateLoadArrayLength(masm(), a1, a2, &miss);
3302 __ bind(&miss);
3303 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3304
3305 return GetCode(CALLBACKS, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003306}
3307
3308
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003309Handle<Code> KeyedLoadStubCompiler::CompileLoadStringLength(
3310 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003311 // ----------- S t a t e -------------
3312 // -- ra : return address
3313 // -- a0 : key
3314 // -- a1 : receiver
3315 // -----------------------------------
3316 Label miss;
3317
3318 Counters* counters = masm()->isolate()->counters();
3319 __ IncrementCounter(counters->keyed_load_string_length(), 1, a2, a3);
3320
3321 // Check the key is the cached one.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003322 __ Branch(&miss, ne, a0, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003323
3324 GenerateLoadStringLength(masm(), a1, a2, a3, &miss, true);
3325 __ bind(&miss);
3326 __ DecrementCounter(counters->keyed_load_string_length(), 1, a2, a3);
3327
3328 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3329
3330 return GetCode(CALLBACKS, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003331}
3332
3333
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003334Handle<Code> KeyedLoadStubCompiler::CompileLoadFunctionPrototype(
3335 Handle<String> name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003336 // ----------- S t a t e -------------
3337 // -- ra : return address
3338 // -- a0 : key
3339 // -- a1 : receiver
3340 // -----------------------------------
3341 Label miss;
3342
3343 Counters* counters = masm()->isolate()->counters();
3344 __ IncrementCounter(counters->keyed_load_function_prototype(), 1, a2, a3);
3345
3346 // Check the name hasn't changed.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003347 __ Branch(&miss, ne, a0, Operand(name));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003348
3349 GenerateLoadFunctionPrototype(masm(), a1, a2, a3, &miss);
3350 __ bind(&miss);
3351 __ DecrementCounter(counters->keyed_load_function_prototype(), 1, a2, a3);
3352 GenerateLoadMiss(masm(), Code::KEYED_LOAD_IC);
3353
3354 return GetCode(CALLBACKS, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003355}
3356
3357
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003358MaybeObject* KeyedLoadStubCompiler::CompileLoadElement(Map* receiver_map) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00003359 // ----------- S t a t e -------------
3360 // -- ra : return address
3361 // -- a0 : key
3362 // -- a1 : receiver
3363 // -----------------------------------
danno@chromium.org40cb8782011-05-25 07:58:50 +00003364 Code* stub;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003365 ElementsKind elements_kind = receiver_map->elements_kind();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003366 MaybeObject* maybe_stub = KeyedLoadElementStub(elements_kind).TryGetCode();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003367 if (!maybe_stub->To(&stub)) return maybe_stub;
3368 __ DispatchMap(a1,
3369 a2,
3370 Handle<Map>(receiver_map),
3371 Handle<Code>(stub),
3372 DO_SMI_CHECK);
3373
3374 Handle<Code> ic = isolate()->builtins()->KeyedLoadIC_Miss();
3375 __ Jump(ic, RelocInfo::CODE_TARGET);
3376
3377 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003378 return TryGetCode(NORMAL, NULL);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003379}
3380
3381
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003382MaybeObject* KeyedLoadStubCompiler::CompileLoadPolymorphic(
danno@chromium.org40cb8782011-05-25 07:58:50 +00003383 MapList* receiver_maps,
3384 CodeList* handler_ics) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003385 // ----------- S t a t e -------------
3386 // -- ra : return address
3387 // -- a0 : key
3388 // -- a1 : receiver
3389 // -----------------------------------
3390 Label miss;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003391 __ JumpIfSmi(a1, &miss);
3392
danno@chromium.org40cb8782011-05-25 07:58:50 +00003393 int receiver_count = receiver_maps->length();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003394 __ lw(a2, FieldMemOperand(a1, HeapObject::kMapOffset));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003395 for (int current = 0; current < receiver_count; ++current) {
3396 Handle<Map> map(receiver_maps->at(current));
3397 Handle<Code> code(handler_ics->at(current));
3398 __ Jump(code, RelocInfo::CODE_TARGET, eq, a2, Operand(map));
3399 }
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003400
3401 __ bind(&miss);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003402 Handle<Code> miss_ic = isolate()->builtins()->KeyedLoadIC_Miss();
3403 __ Jump(miss_ic, RelocInfo::CODE_TARGET);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003404
3405 // Return the generated code.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003406 return TryGetCode(NORMAL, NULL, MEGAMORPHIC);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003407}
3408
3409
3410MaybeObject* KeyedStoreStubCompiler::CompileStoreField(JSObject* object,
3411 int index,
3412 Map* transition,
3413 String* name) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003414 // ----------- S t a t e -------------
3415 // -- a0 : value
3416 // -- a1 : key
3417 // -- a2 : receiver
3418 // -- ra : return address
3419 // -----------------------------------
3420
3421 Label miss;
3422
3423 Counters* counters = masm()->isolate()->counters();
3424 __ IncrementCounter(counters->keyed_store_field(), 1, a3, t0);
3425
3426 // Check that the name has not changed.
3427 __ Branch(&miss, ne, a1, Operand(Handle<String>(name)));
3428
3429 // a3 is used as scratch register. a1 and a2 keep their values if a jump to
3430 // the miss label is generated.
3431 GenerateStoreField(masm(),
3432 object,
3433 index,
3434 transition,
3435 a2, a1, a3,
3436 &miss);
3437 __ bind(&miss);
3438
3439 __ DecrementCounter(counters->keyed_store_field(), 1, a3, t0);
3440 Handle<Code> ic = masm()->isolate()->builtins()->KeyedStoreIC_Miss();
3441 __ Jump(ic, RelocInfo::CODE_TARGET);
3442
3443 // Return the generated code.
3444 return GetCode(transition == NULL ? FIELD : MAP_TRANSITION, name);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003445}
3446
3447
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003448MaybeObject* KeyedStoreStubCompiler::CompileStoreElement(Map* receiver_map) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003449 // ----------- S t a t e -------------
3450 // -- a0 : value
3451 // -- a1 : key
3452 // -- a2 : receiver
3453 // -- ra : return address
3454 // -- a3 : scratch
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003455 // -----------------------------------
danno@chromium.org40cb8782011-05-25 07:58:50 +00003456 Code* stub;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003457 ElementsKind elements_kind = receiver_map->elements_kind();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003458 bool is_js_array = receiver_map->instance_type() == JS_ARRAY_TYPE;
3459 MaybeObject* maybe_stub =
3460 KeyedStoreElementStub(is_js_array, elements_kind).TryGetCode();
danno@chromium.org40cb8782011-05-25 07:58:50 +00003461 if (!maybe_stub->To(&stub)) return maybe_stub;
3462 __ DispatchMap(a2,
3463 a3,
3464 Handle<Map>(receiver_map),
3465 Handle<Code>(stub),
3466 DO_SMI_CHECK);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003467
danno@chromium.org40cb8782011-05-25 07:58:50 +00003468 Handle<Code> ic = isolate()->builtins()->KeyedStoreIC_Miss();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003469 __ Jump(ic, RelocInfo::CODE_TARGET);
3470
3471 // Return the generated code.
3472 return GetCode(NORMAL, NULL);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003473}
3474
3475
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003476MaybeObject* KeyedStoreStubCompiler::CompileStorePolymorphic(
danno@chromium.org40cb8782011-05-25 07:58:50 +00003477 MapList* receiver_maps,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003478 CodeList* handler_stubs,
3479 MapList* transitioned_maps) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00003480 // ----------- S t a t e -------------
3481 // -- a0 : value
3482 // -- a1 : key
3483 // -- a2 : receiver
3484 // -- ra : return address
3485 // -- a3 : scratch
3486 // -----------------------------------
3487 Label miss;
3488 __ JumpIfSmi(a2, &miss);
3489
3490 int receiver_count = receiver_maps->length();
3491 __ lw(a3, FieldMemOperand(a2, HeapObject::kMapOffset));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003492 for (int i = 0; i < receiver_count; ++i) {
3493 Handle<Map> map(receiver_maps->at(i));
3494 Handle<Code> code(handler_stubs->at(i));
3495 if (transitioned_maps->at(i) == NULL) {
3496 __ Jump(code, RelocInfo::CODE_TARGET, eq, a3, Operand(map));
3497 } else {
3498 Label next_map;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003499 __ Branch(&next_map, ne, a3, Operand(map));
3500 __ li(a3, Operand(Handle<Map>(transitioned_maps->at(i))));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003501 __ Jump(code, RelocInfo::CODE_TARGET);
3502 __ bind(&next_map);
3503 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00003504 }
3505
3506 __ bind(&miss);
3507 Handle<Code> miss_ic = isolate()->builtins()->KeyedStoreIC_Miss();
3508 __ Jump(miss_ic, RelocInfo::CODE_TARGET);
3509
3510 // Return the generated code.
3511 return GetCode(NORMAL, NULL, MEGAMORPHIC);
3512}
3513
3514
lrn@chromium.org7516f052011-03-30 08:52:27 +00003515MaybeObject* ConstructStubCompiler::CompileConstructStub(JSFunction* function) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003516 // a0 : argc
3517 // a1 : constructor
3518 // ra : return address
3519 // [sp] : last argument
3520 Label generic_stub_call;
3521
3522 // Use t7 for holding undefined which is used in several places below.
3523 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
3524
3525#ifdef ENABLE_DEBUGGER_SUPPORT
3526 // Check to see whether there are any break points in the function code. If
3527 // there are jump to the generic constructor stub which calls the actual
3528 // code for the function thereby hitting the break points.
3529 __ lw(t5, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
3530 __ lw(a2, FieldMemOperand(t5, SharedFunctionInfo::kDebugInfoOffset));
3531 __ Branch(&generic_stub_call, ne, a2, Operand(t7));
3532#endif
3533
3534 // Load the initial map and verify that it is in fact a map.
3535 // a1: constructor function
3536 // t7: undefined
3537 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
3538 __ And(t0, a2, Operand(kSmiTagMask));
3539 __ Branch(&generic_stub_call, eq, t0, Operand(zero_reg));
3540 __ GetObjectType(a2, a3, t0);
3541 __ Branch(&generic_stub_call, ne, t0, Operand(MAP_TYPE));
3542
3543#ifdef DEBUG
3544 // Cannot construct functions this way.
3545 // a0: argc
3546 // a1: constructor function
3547 // a2: initial map
3548 // t7: undefined
3549 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
3550 __ Check(ne, "Function constructed by construct stub.",
3551 a3, Operand(JS_FUNCTION_TYPE));
3552#endif
3553
3554 // Now allocate the JSObject in new space.
3555 // a0: argc
3556 // a1: constructor function
3557 // a2: initial map
3558 // t7: undefined
3559 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
3560 __ AllocateInNewSpace(a3,
3561 t4,
3562 t5,
3563 t6,
3564 &generic_stub_call,
3565 SIZE_IN_WORDS);
3566
3567 // Allocated the JSObject, now initialize the fields. Map is set to initial
3568 // map and properties and elements are set to empty fixed array.
3569 // a0: argc
3570 // a1: constructor function
3571 // a2: initial map
3572 // a3: object size (in words)
3573 // t4: JSObject (not tagged)
3574 // t7: undefined
3575 __ LoadRoot(t6, Heap::kEmptyFixedArrayRootIndex);
3576 __ mov(t5, t4);
3577 __ sw(a2, MemOperand(t5, JSObject::kMapOffset));
3578 __ sw(t6, MemOperand(t5, JSObject::kPropertiesOffset));
3579 __ sw(t6, MemOperand(t5, JSObject::kElementsOffset));
3580 __ Addu(t5, t5, Operand(3 * kPointerSize));
3581 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
3582 ASSERT_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
3583 ASSERT_EQ(2 * kPointerSize, JSObject::kElementsOffset);
3584
3585
3586 // Calculate the location of the first argument. The stack contains only the
3587 // argc arguments.
3588 __ sll(a1, a0, kPointerSizeLog2);
3589 __ Addu(a1, a1, sp);
3590
3591 // Fill all the in-object properties with undefined.
3592 // a0: argc
3593 // a1: first argument
3594 // a3: object size (in words)
3595 // t4: JSObject (not tagged)
3596 // t5: First in-object property of JSObject (not tagged)
3597 // t7: undefined
3598 // Fill the initialized properties with a constant value or a passed argument
3599 // depending on the this.x = ...; assignment in the function.
3600 SharedFunctionInfo* shared = function->shared();
3601 for (int i = 0; i < shared->this_property_assignments_count(); i++) {
3602 if (shared->IsThisPropertyAssignmentArgument(i)) {
3603 Label not_passed, next;
3604 // Check if the argument assigned to the property is actually passed.
3605 int arg_number = shared->GetThisPropertyAssignmentArgument(i);
3606 __ Branch(&not_passed, less_equal, a0, Operand(arg_number));
3607 // Argument passed - find it on the stack.
3608 __ lw(a2, MemOperand(a1, (arg_number + 1) * -kPointerSize));
3609 __ sw(a2, MemOperand(t5));
3610 __ Addu(t5, t5, kPointerSize);
3611 __ jmp(&next);
3612 __ bind(&not_passed);
3613 // Set the property to undefined.
3614 __ sw(t7, MemOperand(t5));
3615 __ Addu(t5, t5, Operand(kPointerSize));
3616 __ bind(&next);
3617 } else {
3618 // Set the property to the constant value.
3619 Handle<Object> constant(shared->GetThisPropertyAssignmentConstant(i));
3620 __ li(a2, Operand(constant));
3621 __ sw(a2, MemOperand(t5));
3622 __ Addu(t5, t5, kPointerSize);
3623 }
3624 }
3625
3626 // Fill the unused in-object property fields with undefined.
3627 ASSERT(function->has_initial_map());
3628 for (int i = shared->this_property_assignments_count();
3629 i < function->initial_map()->inobject_properties();
3630 i++) {
3631 __ sw(t7, MemOperand(t5));
3632 __ Addu(t5, t5, kPointerSize);
3633 }
3634
3635 // a0: argc
3636 // t4: JSObject (not tagged)
3637 // Move argc to a1 and the JSObject to return to v0 and tag it.
3638 __ mov(a1, a0);
3639 __ mov(v0, t4);
3640 __ Or(v0, v0, Operand(kHeapObjectTag));
3641
3642 // v0: JSObject
3643 // a1: argc
3644 // Remove caller arguments and receiver from the stack and return.
3645 __ sll(t0, a1, kPointerSizeLog2);
3646 __ Addu(sp, sp, t0);
3647 __ Addu(sp, sp, Operand(kPointerSize));
3648 Counters* counters = masm()->isolate()->counters();
3649 __ IncrementCounter(counters->constructed_objects(), 1, a1, a2);
3650 __ IncrementCounter(counters->constructed_objects_stub(), 1, a1, a2);
3651 __ Ret();
3652
3653 // Jump to the generic stub in case the specialized code cannot handle the
3654 // construction.
3655 __ bind(&generic_stub_call);
3656 Handle<Code> generic_construct_stub =
3657 masm()->isolate()->builtins()->JSConstructStubGeneric();
3658 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
3659
3660 // Return the generated code.
3661 return GetCode();
3662}
3663
3664
danno@chromium.org40cb8782011-05-25 07:58:50 +00003665#undef __
3666#define __ ACCESS_MASM(masm)
3667
3668
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00003669void KeyedLoadStubCompiler::GenerateLoadDictionaryElement(
3670 MacroAssembler* masm) {
3671 // ---------- S t a t e --------------
3672 // -- ra : return address
3673 // -- a0 : key
3674 // -- a1 : receiver
3675 // -----------------------------------
3676 Label slow, miss_force_generic;
3677
3678 Register key = a0;
3679 Register receiver = a1;
3680
3681 __ JumpIfNotSmi(key, &miss_force_generic);
3682 __ lw(t0, FieldMemOperand(receiver, JSObject::kElementsOffset));
3683 __ sra(a2, a0, kSmiTagSize);
3684 __ LoadFromNumberDictionary(&slow, t0, a0, v0, a2, a3, t1);
3685 __ Ret();
3686
3687 // Slow case, key and receiver still in a0 and a1.
3688 __ bind(&slow);
3689 __ IncrementCounter(
3690 masm->isolate()->counters()->keyed_load_external_array_slow(),
3691 1, a2, a3);
3692 // Entry registers are intact.
3693 // ---------- S t a t e --------------
3694 // -- ra : return address
3695 // -- a0 : key
3696 // -- a1 : receiver
3697 // -----------------------------------
3698 Handle<Code> slow_ic =
3699 masm->isolate()->builtins()->KeyedLoadIC_Slow();
3700 __ Jump(slow_ic, RelocInfo::CODE_TARGET);
3701
3702 // Miss case, call the runtime.
3703 __ bind(&miss_force_generic);
3704
3705 // ---------- S t a t e --------------
3706 // -- ra : return address
3707 // -- a0 : key
3708 // -- a1 : receiver
3709 // -----------------------------------
3710
3711 Handle<Code> miss_ic =
3712 masm->isolate()->builtins()->KeyedLoadIC_MissForceGeneric();
3713 __ Jump(miss_ic, RelocInfo::CODE_TARGET);
3714}
3715
3716
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003717static bool IsElementTypeSigned(ElementsKind elements_kind) {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003718 switch (elements_kind) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003719 case EXTERNAL_BYTE_ELEMENTS:
3720 case EXTERNAL_SHORT_ELEMENTS:
3721 case EXTERNAL_INT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003722 return true;
3723
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003724 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
3725 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
3726 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
3727 case EXTERNAL_PIXEL_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003728 return false;
3729
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003730 case EXTERNAL_FLOAT_ELEMENTS:
3731 case EXTERNAL_DOUBLE_ELEMENTS:
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003732 case FAST_SMI_ONLY_ELEMENTS:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003733 case FAST_ELEMENTS:
3734 case FAST_DOUBLE_ELEMENTS:
3735 case DICTIONARY_ELEMENTS:
3736 case NON_STRICT_ARGUMENTS_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003737 UNREACHABLE();
3738 return false;
3739 }
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003740 return false;
lrn@chromium.org7516f052011-03-30 08:52:27 +00003741}
3742
3743
danno@chromium.org40cb8782011-05-25 07:58:50 +00003744void KeyedLoadStubCompiler::GenerateLoadExternalArray(
3745 MacroAssembler* masm,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003746 ElementsKind elements_kind) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003747 // ---------- S t a t e --------------
3748 // -- ra : return address
3749 // -- a0 : key
3750 // -- a1 : receiver
3751 // -----------------------------------
danno@chromium.org40cb8782011-05-25 07:58:50 +00003752 Label miss_force_generic, slow, failed_allocation;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003753
3754 Register key = a0;
3755 Register receiver = a1;
3756
danno@chromium.org40cb8782011-05-25 07:58:50 +00003757 // This stub is meant to be tail-jumped to, the receiver must already
3758 // have been verified by the caller to not be a smi.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003759
3760 // Check that the key is a smi.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003761 __ JumpIfNotSmi(key, &miss_force_generic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003762
3763 __ lw(a3, FieldMemOperand(receiver, JSObject::kElementsOffset));
3764 // a3: elements array
3765
3766 // Check that the index is in range.
3767 __ lw(t1, FieldMemOperand(a3, ExternalArray::kLengthOffset));
3768 __ sra(t2, key, kSmiTagSize);
3769 // Unsigned comparison catches both negative and too-large values.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00003770 __ Branch(&miss_force_generic, Ugreater_equal, key, Operand(t1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003771
3772 __ lw(a3, FieldMemOperand(a3, ExternalArray::kExternalPointerOffset));
3773 // a3: base pointer of external storage
3774
3775 // We are not untagging smi key and instead work with it
3776 // as if it was premultiplied by 2.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003777 STATIC_ASSERT((kSmiTag == 0) && (kSmiTagSize == 1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003778
3779 Register value = a2;
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00003780 switch (elements_kind) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003781 case EXTERNAL_BYTE_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003782 __ srl(t2, key, 1);
3783 __ addu(t3, a3, t2);
3784 __ lb(value, MemOperand(t3, 0));
3785 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003786 case EXTERNAL_PIXEL_ELEMENTS:
3787 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003788 __ srl(t2, key, 1);
3789 __ addu(t3, a3, t2);
3790 __ lbu(value, MemOperand(t3, 0));
3791 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003792 case EXTERNAL_SHORT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003793 __ addu(t3, a3, key);
3794 __ lh(value, MemOperand(t3, 0));
3795 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003796 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003797 __ addu(t3, a3, key);
3798 __ lhu(value, MemOperand(t3, 0));
3799 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003800 case EXTERNAL_INT_ELEMENTS:
3801 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003802 __ sll(t2, key, 1);
3803 __ addu(t3, a3, t2);
3804 __ lw(value, MemOperand(t3, 0));
3805 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003806 case EXTERNAL_FLOAT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003807 __ sll(t3, t2, 2);
3808 __ addu(t3, a3, t3);
3809 if (CpuFeatures::IsSupported(FPU)) {
3810 CpuFeatures::Scope scope(FPU);
3811 __ lwc1(f0, MemOperand(t3, 0));
3812 } else {
3813 __ lw(value, MemOperand(t3, 0));
3814 }
3815 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003816 case EXTERNAL_DOUBLE_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003817 __ sll(t2, key, 2);
3818 __ addu(t3, a3, t2);
3819 if (CpuFeatures::IsSupported(FPU)) {
3820 CpuFeatures::Scope scope(FPU);
3821 __ ldc1(f0, MemOperand(t3, 0));
3822 } else {
3823 // t3: pointer to the beginning of the double we want to load.
3824 __ lw(a2, MemOperand(t3, 0));
3825 __ lw(a3, MemOperand(t3, Register::kSizeInBytes));
3826 }
3827 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003828 case FAST_ELEMENTS:
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003829 case FAST_SMI_ONLY_ELEMENTS:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003830 case FAST_DOUBLE_ELEMENTS:
3831 case DICTIONARY_ELEMENTS:
3832 case NON_STRICT_ARGUMENTS_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003833 UNREACHABLE();
3834 break;
3835 }
3836
3837 // For integer array types:
3838 // a2: value
3839 // For float array type:
3840 // f0: value (if FPU is supported)
3841 // a2: value (if FPU is not supported)
3842 // For double array type:
3843 // f0: value (if FPU is supported)
3844 // a2/a3: value (if FPU is not supported)
3845
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003846 if (elements_kind == EXTERNAL_INT_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003847 // For the Int and UnsignedInt array types, we need to see whether
3848 // the value can be represented in a Smi. If not, we need to convert
3849 // it to a HeapNumber.
3850 Label box_int;
3851 __ Subu(t3, value, Operand(0xC0000000)); // Non-smi value gives neg result.
3852 __ Branch(&box_int, lt, t3, Operand(zero_reg));
3853 // Tag integer as smi and return it.
3854 __ sll(v0, value, kSmiTagSize);
3855 __ Ret();
3856
3857 __ bind(&box_int);
3858 // Allocate a HeapNumber for the result and perform int-to-double
3859 // conversion.
3860 // The arm version uses a temporary here to save r0, but we don't need to
3861 // (a0 is not modified).
3862 __ LoadRoot(t1, Heap::kHeapNumberMapRootIndex);
3863 __ AllocateHeapNumber(v0, a3, t0, t1, &slow);
3864
3865 if (CpuFeatures::IsSupported(FPU)) {
3866 CpuFeatures::Scope scope(FPU);
3867 __ mtc1(value, f0);
3868 __ cvt_d_w(f0, f0);
3869 __ sdc1(f0, MemOperand(v0, HeapNumber::kValueOffset - kHeapObjectTag));
3870 __ Ret();
3871 } else {
danno@chromium.org40cb8782011-05-25 07:58:50 +00003872 Register dst1 = t2;
3873 Register dst2 = t3;
3874 FloatingPointHelper::Destination dest =
3875 FloatingPointHelper::kCoreRegisters;
3876 FloatingPointHelper::ConvertIntToDouble(masm,
3877 value,
3878 dest,
3879 f0,
3880 dst1,
3881 dst2,
3882 t1,
3883 f2);
3884 __ sw(dst1, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
3885 __ sw(dst2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
3886 __ Ret();
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003887 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003888 } else if (elements_kind == EXTERNAL_UNSIGNED_INT_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003889 // The test is different for unsigned int values. Since we need
3890 // the value to be in the range of a positive smi, we can't
3891 // handle either of the top two bits being set in the value.
3892 if (CpuFeatures::IsSupported(FPU)) {
3893 CpuFeatures::Scope scope(FPU);
3894 Label pl_box_int;
3895 __ And(t2, value, Operand(0xC0000000));
3896 __ Branch(&pl_box_int, ne, t2, Operand(zero_reg));
3897
3898 // It can fit in an Smi.
3899 // Tag integer as smi and return it.
3900 __ sll(v0, value, kSmiTagSize);
3901 __ Ret();
3902
3903 __ bind(&pl_box_int);
3904 // Allocate a HeapNumber for the result and perform int-to-double
3905 // conversion. Don't use a0 and a1 as AllocateHeapNumber clobbers all
3906 // registers - also when jumping due to exhausted young space.
3907 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3908 __ AllocateHeapNumber(v0, t2, t3, t6, &slow);
3909
3910 // This is replaced by a macro:
3911 // __ mtc1(value, f0); // LS 32-bits.
3912 // __ mtc1(zero_reg, f1); // MS 32-bits are all zero.
3913 // __ cvt_d_l(f0, f0); // Use 64 bit conv to get correct unsigned 32-bit.
3914
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003915 __ Cvt_d_uw(f0, value, f22);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003916
3917 __ sdc1(f0, MemOperand(v0, HeapNumber::kValueOffset - kHeapObjectTag));
3918
3919 __ Ret();
3920 } else {
3921 // Check whether unsigned integer fits into smi.
3922 Label box_int_0, box_int_1, done;
3923 __ And(t2, value, Operand(0x80000000));
3924 __ Branch(&box_int_0, ne, t2, Operand(zero_reg));
3925 __ And(t2, value, Operand(0x40000000));
3926 __ Branch(&box_int_1, ne, t2, Operand(zero_reg));
3927
3928 // Tag integer as smi and return it.
3929 __ sll(v0, value, kSmiTagSize);
3930 __ Ret();
3931
3932 Register hiword = value; // a2.
3933 Register loword = a3;
3934
3935 __ bind(&box_int_0);
3936 // Integer does not have leading zeros.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003937 GenerateUInt2Double(masm, hiword, loword, t0, 0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003938 __ Branch(&done);
3939
3940 __ bind(&box_int_1);
3941 // Integer has one leading zero.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003942 GenerateUInt2Double(masm, hiword, loword, t0, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003943
3944
3945 __ bind(&done);
3946 // Integer was converted to double in registers hiword:loword.
3947 // Wrap it into a HeapNumber. Don't use a0 and a1 as AllocateHeapNumber
3948 // clobbers all registers - also when jumping due to exhausted young
3949 // space.
3950 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3951 __ AllocateHeapNumber(t2, t3, t5, t6, &slow);
3952
3953 __ sw(hiword, FieldMemOperand(t2, HeapNumber::kExponentOffset));
3954 __ sw(loword, FieldMemOperand(t2, HeapNumber::kMantissaOffset));
3955
3956 __ mov(v0, t2);
3957 __ Ret();
3958 }
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003959 } else if (elements_kind == EXTERNAL_FLOAT_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00003960 // For the floating-point array type, we need to always allocate a
3961 // HeapNumber.
3962 if (CpuFeatures::IsSupported(FPU)) {
3963 CpuFeatures::Scope scope(FPU);
3964 // Allocate a HeapNumber for the result. Don't use a0 and a1 as
3965 // AllocateHeapNumber clobbers all registers - also when jumping due to
3966 // exhausted young space.
3967 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3968 __ AllocateHeapNumber(v0, t3, t5, t6, &slow);
3969 // The float (single) value is already in fpu reg f0 (if we use float).
3970 __ cvt_d_s(f0, f0);
3971 __ sdc1(f0, MemOperand(v0, HeapNumber::kValueOffset - kHeapObjectTag));
3972 __ Ret();
3973 } else {
3974 // Allocate a HeapNumber for the result. Don't use a0 and a1 as
3975 // AllocateHeapNumber clobbers all registers - also when jumping due to
3976 // exhausted young space.
3977 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
3978 __ AllocateHeapNumber(v0, t3, t5, t6, &slow);
3979 // FPU is not available, do manual single to double conversion.
3980
3981 // a2: floating point value (binary32).
3982 // v0: heap number for result
3983
3984 // Extract mantissa to t4.
3985 __ And(t4, value, Operand(kBinary32MantissaMask));
3986
3987 // Extract exponent to t5.
3988 __ srl(t5, value, kBinary32MantissaBits);
3989 __ And(t5, t5, Operand(kBinary32ExponentMask >> kBinary32MantissaBits));
3990
3991 Label exponent_rebiased;
3992 __ Branch(&exponent_rebiased, eq, t5, Operand(zero_reg));
3993
3994 __ li(t0, 0x7ff);
3995 __ Xor(t1, t5, Operand(0xFF));
3996 __ movz(t5, t0, t1); // Set t5 to 0x7ff only if t5 is equal to 0xff.
3997 __ Branch(&exponent_rebiased, eq, t0, Operand(0xff));
3998
3999 // Rebias exponent.
4000 __ Addu(t5,
4001 t5,
4002 Operand(-kBinary32ExponentBias + HeapNumber::kExponentBias));
4003
4004 __ bind(&exponent_rebiased);
4005 __ And(a2, value, Operand(kBinary32SignMask));
4006 value = no_reg;
4007 __ sll(t0, t5, HeapNumber::kMantissaBitsInTopWord);
4008 __ or_(a2, a2, t0);
4009
4010 // Shift mantissa.
4011 static const int kMantissaShiftForHiWord =
4012 kBinary32MantissaBits - HeapNumber::kMantissaBitsInTopWord;
4013
4014 static const int kMantissaShiftForLoWord =
4015 kBitsPerInt - kMantissaShiftForHiWord;
4016
4017 __ srl(t0, t4, kMantissaShiftForHiWord);
4018 __ or_(a2, a2, t0);
4019 __ sll(a0, t4, kMantissaShiftForLoWord);
4020
4021 __ sw(a2, FieldMemOperand(v0, HeapNumber::kExponentOffset));
4022 __ sw(a0, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
4023 __ Ret();
4024 }
4025
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004026 } else if (elements_kind == EXTERNAL_DOUBLE_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004027 if (CpuFeatures::IsSupported(FPU)) {
4028 CpuFeatures::Scope scope(FPU);
4029 // Allocate a HeapNumber for the result. Don't use a0 and a1 as
4030 // AllocateHeapNumber clobbers all registers - also when jumping due to
4031 // exhausted young space.
4032 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
4033 __ AllocateHeapNumber(v0, t3, t5, t6, &slow);
4034 // The double value is already in f0
4035 __ sdc1(f0, FieldMemOperand(v0, HeapNumber::kValueOffset));
4036 __ Ret();
4037 } else {
4038 // Allocate a HeapNumber for the result. Don't use a0 and a1 as
4039 // AllocateHeapNumber clobbers all registers - also when jumping due to
4040 // exhausted young space.
4041 __ LoadRoot(t6, Heap::kHeapNumberMapRootIndex);
4042 __ AllocateHeapNumber(v0, t3, t5, t6, &slow);
4043
4044 __ sw(a2, FieldMemOperand(v0, HeapNumber::kMantissaOffset));
4045 __ sw(a3, FieldMemOperand(v0, HeapNumber::kExponentOffset));
4046 __ Ret();
4047 }
4048
4049 } else {
4050 // Tag integer as smi and return it.
4051 __ sll(v0, value, kSmiTagSize);
4052 __ Ret();
4053 }
4054
4055 // Slow case, key and receiver still in a0 and a1.
4056 __ bind(&slow);
4057 __ IncrementCounter(
danno@chromium.org40cb8782011-05-25 07:58:50 +00004058 masm->isolate()->counters()->keyed_load_external_array_slow(),
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004059 1, a2, a3);
4060
4061 // ---------- S t a t e --------------
4062 // -- ra : return address
4063 // -- a0 : key
4064 // -- a1 : receiver
4065 // -----------------------------------
4066
4067 __ Push(a1, a0);
4068
4069 __ TailCallRuntime(Runtime::kKeyedGetProperty, 2, 1);
4070
danno@chromium.org40cb8782011-05-25 07:58:50 +00004071 __ bind(&miss_force_generic);
4072 Code* stub = masm->isolate()->builtins()->builtin(
4073 Builtins::kKeyedLoadIC_MissForceGeneric);
4074 __ Jump(Handle<Code>(stub), RelocInfo::CODE_TARGET);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004075}
4076
4077
danno@chromium.org40cb8782011-05-25 07:58:50 +00004078void KeyedStoreStubCompiler::GenerateStoreExternalArray(
4079 MacroAssembler* masm,
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004080 ElementsKind elements_kind) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004081 // ---------- S t a t e --------------
4082 // -- a0 : value
4083 // -- a1 : key
4084 // -- a2 : receiver
4085 // -- ra : return address
4086 // -----------------------------------
4087
danno@chromium.org40cb8782011-05-25 07:58:50 +00004088 Label slow, check_heap_number, miss_force_generic;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004089
4090 // Register usage.
4091 Register value = a0;
4092 Register key = a1;
4093 Register receiver = a2;
4094 // a3 mostly holds the elements array or the destination external array.
4095
danno@chromium.org40cb8782011-05-25 07:58:50 +00004096 // This stub is meant to be tail-jumped to, the receiver must already
4097 // have been verified by the caller to not be a smi.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004098
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004099 // Check that the key is a smi.
danno@chromium.org40cb8782011-05-25 07:58:50 +00004100 __ JumpIfNotSmi(key, &miss_force_generic);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004101
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004102 __ lw(a3, FieldMemOperand(receiver, JSObject::kElementsOffset));
4103
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004104 // Check that the index is in range.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004105 __ lw(t1, FieldMemOperand(a3, ExternalArray::kLengthOffset));
4106 // Unsigned comparison catches both negative and too-large values.
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00004107 __ Branch(&miss_force_generic, Ugreater_equal, key, Operand(t1));
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004108
4109 // Handle both smis and HeapNumbers in the fast path. Go to the
4110 // runtime for all other kinds of values.
4111 // a3: external array.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004112
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004113 if (elements_kind == EXTERNAL_PIXEL_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004114 // Double to pixel conversion is only implemented in the runtime for now.
4115 __ JumpIfNotSmi(value, &slow);
4116 } else {
4117 __ JumpIfNotSmi(value, &check_heap_number);
4118 }
4119 __ SmiUntag(t1, value);
4120 __ lw(a3, FieldMemOperand(a3, ExternalArray::kExternalPointerOffset));
4121
4122 // a3: base pointer of external storage.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004123 // t1: value (integer).
4124
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004125 switch (elements_kind) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004126 case EXTERNAL_PIXEL_ELEMENTS: {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004127 // Clamp the value to [0..255].
4128 // v0 is used as a scratch register here.
4129 Label done;
4130 __ li(v0, Operand(255));
4131 // Normal branch: nop in delay slot.
4132 __ Branch(&done, gt, t1, Operand(v0));
4133 // Use delay slot in this branch.
4134 __ Branch(USE_DELAY_SLOT, &done, lt, t1, Operand(zero_reg));
4135 __ mov(v0, zero_reg); // In delay slot.
4136 __ mov(v0, t1); // Value is in range 0..255.
4137 __ bind(&done);
4138 __ mov(t1, v0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004139
4140 __ srl(t8, key, 1);
4141 __ addu(t8, a3, t8);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004142 __ sb(t1, MemOperand(t8, 0));
4143 }
4144 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004145 case EXTERNAL_BYTE_ELEMENTS:
4146 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004147 __ srl(t8, key, 1);
4148 __ addu(t8, a3, t8);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004149 __ sb(t1, MemOperand(t8, 0));
4150 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004151 case EXTERNAL_SHORT_ELEMENTS:
4152 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004153 __ addu(t8, a3, key);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004154 __ sh(t1, MemOperand(t8, 0));
4155 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004156 case EXTERNAL_INT_ELEMENTS:
4157 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004158 __ sll(t8, key, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004159 __ addu(t8, a3, t8);
4160 __ sw(t1, MemOperand(t8, 0));
4161 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004162 case EXTERNAL_FLOAT_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004163 // Perform int-to-float conversion and store to memory.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004164 __ SmiUntag(t0, key);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004165 StoreIntAsFloat(masm, a3, t0, t1, t2, t3, t4);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004166 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004167 case EXTERNAL_DOUBLE_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004168 __ sll(t8, key, 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004169 __ addu(a3, a3, t8);
4170 // a3: effective address of the double element
4171 FloatingPointHelper::Destination destination;
4172 if (CpuFeatures::IsSupported(FPU)) {
4173 destination = FloatingPointHelper::kFPURegisters;
4174 } else {
4175 destination = FloatingPointHelper::kCoreRegisters;
4176 }
4177 FloatingPointHelper::ConvertIntToDouble(
danno@chromium.org40cb8782011-05-25 07:58:50 +00004178 masm, t1, destination,
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004179 f0, t2, t3, // These are: double_dst, dst1, dst2.
4180 t0, f2); // These are: scratch2, single_scratch.
4181 if (destination == FloatingPointHelper::kFPURegisters) {
4182 CpuFeatures::Scope scope(FPU);
4183 __ sdc1(f0, MemOperand(a3, 0));
4184 } else {
4185 __ sw(t2, MemOperand(a3, 0));
4186 __ sw(t3, MemOperand(a3, Register::kSizeInBytes));
4187 }
4188 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004189 case FAST_ELEMENTS:
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004190 case FAST_SMI_ONLY_ELEMENTS:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004191 case FAST_DOUBLE_ELEMENTS:
4192 case DICTIONARY_ELEMENTS:
4193 case NON_STRICT_ARGUMENTS_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004194 UNREACHABLE();
4195 break;
4196 }
4197
4198 // Entry registers are intact, a0 holds the value which is the return value.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004199 __ mov(v0, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004200 __ Ret();
4201
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004202 if (elements_kind != EXTERNAL_PIXEL_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004203 // a3: external array.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004204 __ bind(&check_heap_number);
4205 __ GetObjectType(value, t1, t2);
4206 __ Branch(&slow, ne, t2, Operand(HEAP_NUMBER_TYPE));
4207
4208 __ lw(a3, FieldMemOperand(a3, ExternalArray::kExternalPointerOffset));
4209
4210 // a3: base pointer of external storage.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004211
4212 // The WebGL specification leaves the behavior of storing NaN and
4213 // +/-Infinity into integer arrays basically undefined. For more
4214 // reproducible behavior, convert these to zero.
4215
4216 if (CpuFeatures::IsSupported(FPU)) {
4217 CpuFeatures::Scope scope(FPU);
4218
4219 __ ldc1(f0, FieldMemOperand(a0, HeapNumber::kValueOffset));
4220
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004221 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004222 __ cvt_s_d(f0, f0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004223 __ sll(t8, key, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004224 __ addu(t8, a3, t8);
4225 __ swc1(f0, MemOperand(t8, 0));
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004226 } else if (elements_kind == EXTERNAL_DOUBLE_ELEMENTS) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004227 __ sll(t8, key, 2);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004228 __ addu(t8, a3, t8);
4229 __ sdc1(f0, MemOperand(t8, 0));
4230 } else {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00004231 __ EmitECMATruncate(t3, f0, f2, t2, t1, t5);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004232
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004233 switch (elements_kind) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004234 case EXTERNAL_BYTE_ELEMENTS:
4235 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004236 __ srl(t8, key, 1);
4237 __ addu(t8, a3, t8);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004238 __ sb(t3, MemOperand(t8, 0));
4239 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004240 case EXTERNAL_SHORT_ELEMENTS:
4241 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004242 __ addu(t8, a3, key);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004243 __ sh(t3, MemOperand(t8, 0));
4244 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004245 case EXTERNAL_INT_ELEMENTS:
4246 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004247 __ sll(t8, key, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004248 __ addu(t8, a3, t8);
4249 __ sw(t3, MemOperand(t8, 0));
4250 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004251 case EXTERNAL_PIXEL_ELEMENTS:
4252 case EXTERNAL_FLOAT_ELEMENTS:
4253 case EXTERNAL_DOUBLE_ELEMENTS:
4254 case FAST_ELEMENTS:
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004255 case FAST_SMI_ONLY_ELEMENTS:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004256 case FAST_DOUBLE_ELEMENTS:
4257 case DICTIONARY_ELEMENTS:
4258 case NON_STRICT_ARGUMENTS_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004259 UNREACHABLE();
4260 break;
4261 }
4262 }
4263
4264 // Entry registers are intact, a0 holds the value
4265 // which is the return value.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004266 __ mov(v0, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004267 __ Ret();
4268 } else {
4269 // FPU is not available, do manual conversions.
4270
4271 __ lw(t3, FieldMemOperand(value, HeapNumber::kExponentOffset));
4272 __ lw(t4, FieldMemOperand(value, HeapNumber::kMantissaOffset));
4273
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004274 if (elements_kind == EXTERNAL_FLOAT_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004275 Label done, nan_or_infinity_or_zero;
4276 static const int kMantissaInHiWordShift =
4277 kBinary32MantissaBits - HeapNumber::kMantissaBitsInTopWord;
4278
4279 static const int kMantissaInLoWordShift =
4280 kBitsPerInt - kMantissaInHiWordShift;
4281
4282 // Test for all special exponent values: zeros, subnormal numbers, NaNs
4283 // and infinities. All these should be converted to 0.
4284 __ li(t5, HeapNumber::kExponentMask);
4285 __ and_(t6, t3, t5);
4286 __ Branch(&nan_or_infinity_or_zero, eq, t6, Operand(zero_reg));
4287
4288 __ xor_(t1, t6, t5);
4289 __ li(t2, kBinary32ExponentMask);
4290 __ movz(t6, t2, t1); // Only if t6 is equal to t5.
4291 __ Branch(&nan_or_infinity_or_zero, eq, t6, Operand(t5));
4292
4293 // Rebias exponent.
4294 __ srl(t6, t6, HeapNumber::kExponentShift);
4295 __ Addu(t6,
4296 t6,
4297 Operand(kBinary32ExponentBias - HeapNumber::kExponentBias));
4298
4299 __ li(t1, Operand(kBinary32MaxExponent));
4300 __ Slt(t1, t1, t6);
4301 __ And(t2, t3, Operand(HeapNumber::kSignMask));
4302 __ Or(t2, t2, Operand(kBinary32ExponentMask));
4303 __ movn(t3, t2, t1); // Only if t6 is gt kBinary32MaxExponent.
4304 __ Branch(&done, gt, t6, Operand(kBinary32MaxExponent));
4305
4306 __ Slt(t1, t6, Operand(kBinary32MinExponent));
4307 __ And(t2, t3, Operand(HeapNumber::kSignMask));
4308 __ movn(t3, t2, t1); // Only if t6 is lt kBinary32MinExponent.
4309 __ Branch(&done, lt, t6, Operand(kBinary32MinExponent));
4310
4311 __ And(t7, t3, Operand(HeapNumber::kSignMask));
4312 __ And(t3, t3, Operand(HeapNumber::kMantissaMask));
4313 __ sll(t3, t3, kMantissaInHiWordShift);
4314 __ or_(t7, t7, t3);
4315 __ srl(t4, t4, kMantissaInLoWordShift);
4316 __ or_(t7, t7, t4);
4317 __ sll(t6, t6, kBinary32ExponentShift);
4318 __ or_(t3, t7, t6);
4319
4320 __ bind(&done);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004321 __ sll(t9, key, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004322 __ addu(t9, a2, t9);
4323 __ sw(t3, MemOperand(t9, 0));
4324
4325 // Entry registers are intact, a0 holds the value which is the return
4326 // value.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004327 __ mov(v0, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004328 __ Ret();
4329
4330 __ bind(&nan_or_infinity_or_zero);
4331 __ And(t7, t3, Operand(HeapNumber::kSignMask));
4332 __ And(t3, t3, Operand(HeapNumber::kMantissaMask));
4333 __ or_(t6, t6, t7);
4334 __ sll(t3, t3, kMantissaInHiWordShift);
4335 __ or_(t6, t6, t3);
4336 __ srl(t4, t4, kMantissaInLoWordShift);
4337 __ or_(t3, t6, t4);
4338 __ Branch(&done);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004339 } else if (elements_kind == EXTERNAL_DOUBLE_ELEMENTS) {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004340 __ sll(t8, t0, 3);
4341 __ addu(t8, a3, t8);
4342 // t8: effective address of destination element.
4343 __ sw(t4, MemOperand(t8, 0));
4344 __ sw(t3, MemOperand(t8, Register::kSizeInBytes));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004345 __ mov(v0, a0);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004346 __ Ret();
4347 } else {
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004348 bool is_signed_type = IsElementTypeSigned(elements_kind);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004349 int meaningfull_bits = is_signed_type ? (kBitsPerInt - 1) : kBitsPerInt;
4350 int32_t min_value = is_signed_type ? 0x80000000 : 0x00000000;
4351
4352 Label done, sign;
4353
4354 // Test for all special exponent values: zeros, subnormal numbers, NaNs
4355 // and infinities. All these should be converted to 0.
4356 __ li(t5, HeapNumber::kExponentMask);
4357 __ and_(t6, t3, t5);
4358 __ movz(t3, zero_reg, t6); // Only if t6 is equal to zero.
4359 __ Branch(&done, eq, t6, Operand(zero_reg));
4360
4361 __ xor_(t2, t6, t5);
4362 __ movz(t3, zero_reg, t2); // Only if t6 is equal to t5.
4363 __ Branch(&done, eq, t6, Operand(t5));
4364
4365 // Unbias exponent.
4366 __ srl(t6, t6, HeapNumber::kExponentShift);
4367 __ Subu(t6, t6, Operand(HeapNumber::kExponentBias));
4368 // If exponent is negative then result is 0.
4369 __ slt(t2, t6, zero_reg);
4370 __ movn(t3, zero_reg, t2); // Only if exponent is negative.
4371 __ Branch(&done, lt, t6, Operand(zero_reg));
4372
4373 // If exponent is too big then result is minimal value.
4374 __ slti(t1, t6, meaningfull_bits - 1);
4375 __ li(t2, min_value);
4376 __ movz(t3, t2, t1); // Only if t6 is ge meaningfull_bits - 1.
4377 __ Branch(&done, ge, t6, Operand(meaningfull_bits - 1));
4378
4379 __ And(t5, t3, Operand(HeapNumber::kSignMask));
4380 __ And(t3, t3, Operand(HeapNumber::kMantissaMask));
4381 __ Or(t3, t3, Operand(1u << HeapNumber::kMantissaBitsInTopWord));
4382
4383 __ li(t9, HeapNumber::kMantissaBitsInTopWord);
4384 __ subu(t6, t9, t6);
4385 __ slt(t1, t6, zero_reg);
4386 __ srlv(t2, t3, t6);
4387 __ movz(t3, t2, t1); // Only if t6 is positive.
4388 __ Branch(&sign, ge, t6, Operand(zero_reg));
4389
4390 __ subu(t6, zero_reg, t6);
4391 __ sllv(t3, t3, t6);
4392 __ li(t9, meaningfull_bits);
4393 __ subu(t6, t9, t6);
4394 __ srlv(t4, t4, t6);
4395 __ or_(t3, t3, t4);
4396
4397 __ bind(&sign);
4398 __ subu(t2, t3, zero_reg);
4399 __ movz(t3, t2, t5); // Only if t5 is zero.
4400
4401 __ bind(&done);
4402
4403 // Result is in t3.
4404 // This switch block should be exactly the same as above (FPU mode).
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00004405 switch (elements_kind) {
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004406 case EXTERNAL_BYTE_ELEMENTS:
4407 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004408 __ srl(t8, key, 1);
4409 __ addu(t8, a3, t8);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004410 __ sb(t3, MemOperand(t8, 0));
4411 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004412 case EXTERNAL_SHORT_ELEMENTS:
4413 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004414 __ addu(t8, a3, key);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004415 __ sh(t3, MemOperand(t8, 0));
4416 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004417 case EXTERNAL_INT_ELEMENTS:
4418 case EXTERNAL_UNSIGNED_INT_ELEMENTS:
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004419 __ sll(t8, key, 1);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004420 __ addu(t8, a3, t8);
4421 __ sw(t3, MemOperand(t8, 0));
4422 break;
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004423 case EXTERNAL_PIXEL_ELEMENTS:
4424 case EXTERNAL_FLOAT_ELEMENTS:
4425 case EXTERNAL_DOUBLE_ELEMENTS:
4426 case FAST_ELEMENTS:
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004427 case FAST_SMI_ONLY_ELEMENTS:
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004428 case FAST_DOUBLE_ELEMENTS:
4429 case DICTIONARY_ELEMENTS:
4430 case NON_STRICT_ARGUMENTS_ELEMENTS:
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004431 UNREACHABLE();
4432 break;
4433 }
4434 }
4435 }
4436 }
4437
danno@chromium.org40cb8782011-05-25 07:58:50 +00004438 // Slow case, key and receiver still in a0 and a1.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004439 __ bind(&slow);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004440 __ IncrementCounter(
4441 masm->isolate()->counters()->keyed_load_external_array_slow(),
4442 1, a2, a3);
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004443 // Entry registers are intact.
4444 // ---------- S t a t e --------------
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004445 // -- ra : return address
danno@chromium.org40cb8782011-05-25 07:58:50 +00004446 // -- a0 : key
4447 // -- a1 : receiver
4448 // -----------------------------------
4449 Handle<Code> slow_ic =
4450 masm->isolate()->builtins()->KeyedStoreIC_Slow();
4451 __ Jump(slow_ic, RelocInfo::CODE_TARGET);
4452
4453 // Miss case, call the runtime.
4454 __ bind(&miss_force_generic);
4455
4456 // ---------- S t a t e --------------
4457 // -- ra : return address
4458 // -- a0 : key
4459 // -- a1 : receiver
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004460 // -----------------------------------
4461
danno@chromium.org40cb8782011-05-25 07:58:50 +00004462 Handle<Code> miss_ic =
4463 masm->isolate()->builtins()->KeyedStoreIC_MissForceGeneric();
4464 __ Jump(miss_ic, RelocInfo::CODE_TARGET);
4465}
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004466
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004467
danno@chromium.org40cb8782011-05-25 07:58:50 +00004468void KeyedLoadStubCompiler::GenerateLoadFastElement(MacroAssembler* masm) {
4469 // ----------- S t a t e -------------
4470 // -- ra : return address
4471 // -- a0 : key
4472 // -- a1 : receiver
4473 // -----------------------------------
4474 Label miss_force_generic;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00004475
danno@chromium.org40cb8782011-05-25 07:58:50 +00004476 // This stub is meant to be tail-jumped to, the receiver must already
4477 // have been verified by the caller to not be a smi.
4478
4479 // Check that the key is a smi.
4480 __ JumpIfNotSmi(a0, &miss_force_generic);
4481
4482 // Get the elements array.
4483 __ lw(a2, FieldMemOperand(a1, JSObject::kElementsOffset));
4484 __ AssertFastElements(a2);
4485
4486 // Check that the key is within bounds.
4487 __ lw(a3, FieldMemOperand(a2, FixedArray::kLengthOffset));
4488 __ Branch(&miss_force_generic, hs, a0, Operand(a3));
4489
4490 // Load the result and make sure it's not the hole.
4491 __ Addu(a3, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004492 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004493 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
4494 __ Addu(t0, t0, a3);
4495 __ lw(t0, MemOperand(t0));
4496 __ LoadRoot(t1, Heap::kTheHoleValueRootIndex);
4497 __ Branch(&miss_force_generic, eq, t0, Operand(t1));
4498 __ mov(v0, t0);
4499 __ Ret();
4500
4501 __ bind(&miss_force_generic);
4502 Code* stub = masm->isolate()->builtins()->builtin(
4503 Builtins::kKeyedLoadIC_MissForceGeneric);
4504 __ Jump(Handle<Code>(stub), RelocInfo::CODE_TARGET);
4505}
4506
4507
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004508void KeyedLoadStubCompiler::GenerateLoadFastDoubleElement(
4509 MacroAssembler* masm) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004510 // ----------- S t a t e -------------
4511 // -- ra : return address
4512 // -- a0 : key
4513 // -- a1 : receiver
4514 // -----------------------------------
4515 Label miss_force_generic, slow_allocate_heapnumber;
4516
4517 Register key_reg = a0;
4518 Register receiver_reg = a1;
4519 Register elements_reg = a2;
4520 Register heap_number_reg = a2;
4521 Register indexed_double_offset = a3;
4522 Register scratch = t0;
4523 Register scratch2 = t1;
4524 Register scratch3 = t2;
4525 Register heap_number_map = t3;
4526
4527 // This stub is meant to be tail-jumped to, the receiver must already
4528 // have been verified by the caller to not be a smi.
4529
4530 // Check that the key is a smi.
4531 __ JumpIfNotSmi(key_reg, &miss_force_generic);
4532
4533 // Get the elements array.
4534 __ lw(elements_reg,
4535 FieldMemOperand(receiver_reg, JSObject::kElementsOffset));
4536
4537 // Check that the key is within bounds.
4538 __ lw(scratch, FieldMemOperand(elements_reg, FixedArray::kLengthOffset));
4539 __ Branch(&miss_force_generic, hs, key_reg, Operand(scratch));
4540
4541 // Load the upper word of the double in the fixed array and test for NaN.
4542 __ sll(scratch2, key_reg, kDoubleSizeLog2 - kSmiTagSize);
4543 __ Addu(indexed_double_offset, elements_reg, Operand(scratch2));
4544 uint32_t upper_32_offset = FixedArray::kHeaderSize + sizeof(kHoleNanLower32);
4545 __ lw(scratch, FieldMemOperand(indexed_double_offset, upper_32_offset));
4546 __ Branch(&miss_force_generic, eq, scratch, Operand(kHoleNanUpper32));
4547
4548 // Non-NaN. Allocate a new heap number and copy the double value into it.
4549 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4550 __ AllocateHeapNumber(heap_number_reg, scratch2, scratch3,
4551 heap_number_map, &slow_allocate_heapnumber);
4552
4553 // Don't need to reload the upper 32 bits of the double, it's already in
4554 // scratch.
4555 __ sw(scratch, FieldMemOperand(heap_number_reg,
4556 HeapNumber::kExponentOffset));
4557 __ lw(scratch, FieldMemOperand(indexed_double_offset,
4558 FixedArray::kHeaderSize));
4559 __ sw(scratch, FieldMemOperand(heap_number_reg,
4560 HeapNumber::kMantissaOffset));
4561
4562 __ mov(v0, heap_number_reg);
4563 __ Ret();
4564
4565 __ bind(&slow_allocate_heapnumber);
4566 Handle<Code> slow_ic =
4567 masm->isolate()->builtins()->KeyedLoadIC_Slow();
4568 __ Jump(slow_ic, RelocInfo::CODE_TARGET);
4569
4570 __ bind(&miss_force_generic);
4571 Handle<Code> miss_ic =
4572 masm->isolate()->builtins()->KeyedLoadIC_MissForceGeneric();
4573 __ Jump(miss_ic, RelocInfo::CODE_TARGET);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004574}
4575
4576
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004577void KeyedStoreStubCompiler::GenerateStoreFastElement(
4578 MacroAssembler* masm,
4579 bool is_js_array,
4580 ElementsKind elements_kind) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00004581 // ----------- S t a t e -------------
4582 // -- a0 : value
4583 // -- a1 : key
4584 // -- a2 : receiver
4585 // -- ra : return address
4586 // -- a3 : scratch
4587 // -- a4 : scratch (elements)
4588 // -----------------------------------
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004589 Label miss_force_generic, transition_elements_kind;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004590
4591 Register value_reg = a0;
4592 Register key_reg = a1;
4593 Register receiver_reg = a2;
4594 Register scratch = a3;
4595 Register elements_reg = t0;
4596 Register scratch2 = t1;
4597 Register scratch3 = t2;
4598
4599 // This stub is meant to be tail-jumped to, the receiver must already
4600 // have been verified by the caller to not be a smi.
4601
4602 // Check that the key is a smi.
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004603 __ JumpIfNotSmi(key_reg, &miss_force_generic);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004604
4605 // Get the elements array and make sure it is a fast element array, not 'cow'.
4606 __ lw(elements_reg,
4607 FieldMemOperand(receiver_reg, JSObject::kElementsOffset));
4608 __ CheckMap(elements_reg,
4609 scratch,
4610 Heap::kFixedArrayMapRootIndex,
4611 &miss_force_generic,
4612 DONT_DO_SMI_CHECK);
4613
4614 // Check that the key is within bounds.
4615 if (is_js_array) {
4616 __ lw(scratch, FieldMemOperand(receiver_reg, JSArray::kLengthOffset));
4617 } else {
4618 __ lw(scratch, FieldMemOperand(elements_reg, FixedArray::kLengthOffset));
4619 }
4620 // Compare smis.
4621 __ Branch(&miss_force_generic, hs, key_reg, Operand(scratch));
4622
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004623 if (elements_kind == FAST_SMI_ONLY_ELEMENTS) {
4624 __ JumpIfNotSmi(value_reg, &transition_elements_kind);
4625 __ Addu(scratch,
4626 elements_reg,
4627 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4628 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
4629 __ sll(scratch2, key_reg, kPointerSizeLog2 - kSmiTagSize);
4630 __ Addu(scratch, scratch, scratch2);
4631 __ sw(value_reg, MemOperand(scratch));
4632 } else {
4633 ASSERT(elements_kind == FAST_ELEMENTS);
4634 __ Addu(scratch,
4635 elements_reg,
4636 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4637 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
4638 __ sll(scratch2, key_reg, kPointerSizeLog2 - kSmiTagSize);
4639 __ Addu(scratch, scratch, scratch2);
4640 __ sw(value_reg, MemOperand(scratch));
4641 __ mov(receiver_reg, value_reg);
4642 ASSERT(elements_kind == FAST_ELEMENTS);
4643 __ RecordWrite(elements_reg, // Object.
4644 scratch, // Address.
4645 receiver_reg, // Value.
4646 kRAHasNotBeenSaved,
4647 kDontSaveFPRegs);
4648 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004649 // value_reg (a0) is preserved.
4650 // Done.
4651 __ Ret();
4652
4653 __ bind(&miss_force_generic);
4654 Handle<Code> ic =
4655 masm->isolate()->builtins()->KeyedStoreIC_MissForceGeneric();
4656 __ Jump(ic, RelocInfo::CODE_TARGET);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004657
4658 __ bind(&transition_elements_kind);
4659 Handle<Code> ic_miss = masm->isolate()->builtins()->KeyedStoreIC_Miss();
4660 __ Jump(ic_miss, RelocInfo::CODE_TARGET);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00004661}
4662
4663
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004664void KeyedStoreStubCompiler::GenerateStoreFastDoubleElement(
4665 MacroAssembler* masm,
4666 bool is_js_array) {
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004667 // ----------- S t a t e -------------
4668 // -- a0 : value
4669 // -- a1 : key
4670 // -- a2 : receiver
4671 // -- ra : return address
4672 // -- a3 : scratch
4673 // -- t0 : scratch (elements_reg)
4674 // -- t1 : scratch (mantissa_reg)
4675 // -- t2 : scratch (exponent_reg)
4676 // -- t3 : scratch4
4677 // -----------------------------------
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004678 Label miss_force_generic, transition_elements_kind;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004679
4680 Register value_reg = a0;
4681 Register key_reg = a1;
4682 Register receiver_reg = a2;
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004683 Register elements_reg = a3;
4684 Register scratch1 = t0;
4685 Register scratch2 = t1;
4686 Register scratch3 = t2;
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004687 Register scratch4 = t3;
4688
4689 // This stub is meant to be tail-jumped to, the receiver must already
4690 // have been verified by the caller to not be a smi.
4691 __ JumpIfNotSmi(key_reg, &miss_force_generic);
4692
4693 __ lw(elements_reg,
4694 FieldMemOperand(receiver_reg, JSObject::kElementsOffset));
4695
4696 // Check that the key is within bounds.
4697 if (is_js_array) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004698 __ lw(scratch1, FieldMemOperand(receiver_reg, JSArray::kLengthOffset));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004699 } else {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004700 __ lw(scratch1,
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004701 FieldMemOperand(elements_reg, FixedArray::kLengthOffset));
4702 }
4703 // Compare smis, unsigned compare catches both negative and out-of-bound
4704 // indexes.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004705 __ Branch(&miss_force_generic, hs, key_reg, Operand(scratch1));
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004706
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004707 __ StoreNumberToDoubleElements(value_reg,
4708 key_reg,
4709 receiver_reg,
4710 elements_reg,
4711 scratch1,
4712 scratch2,
4713 scratch3,
4714 scratch4,
4715 &transition_elements_kind);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004716
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004717 __ Ret(USE_DELAY_SLOT);
4718 __ mov(v0, value_reg); // In delay slot.
rossberg@chromium.org717967f2011-07-20 13:44:42 +00004719
4720 // Handle store cache miss, replacing the ic with the generic stub.
4721 __ bind(&miss_force_generic);
4722 Handle<Code> ic =
4723 masm->isolate()->builtins()->KeyedStoreIC_MissForceGeneric();
4724 __ Jump(ic, RelocInfo::CODE_TARGET);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004725
4726 __ bind(&transition_elements_kind);
4727 Handle<Code> ic_miss = masm->isolate()->builtins()->KeyedStoreIC_Miss();
4728 __ Jump(ic_miss, RelocInfo::CODE_TARGET);
svenpanne@chromium.org84bcc552011-07-18 09:50:57 +00004729}
4730
4731
ager@chromium.org5c838252010-02-19 08:53:10 +00004732#undef __
4733
4734} } // namespace v8::internal
4735
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +00004736#endif // V8_TARGET_ARCH_MIPS