blob: 961d3ce5b981e830d95bbc9c35e53e66d4edfde9 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_ARM)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "codegen-inl.h"
33#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010034#include "deoptimizer.h"
35#include "full-codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036#include "runtime.h"
37
38namespace v8 {
39namespace internal {
40
41
42#define __ ACCESS_MASM(masm)
43
44
Leon Clarkee46be812010-01-19 14:06:41 +000045void Builtins::Generate_Adaptor(MacroAssembler* masm,
46 CFunctionId id,
47 BuiltinExtraArguments extra_args) {
48 // ----------- S t a t e -------------
49 // -- r0 : number of arguments excluding receiver
50 // -- r1 : called function (only guaranteed when
51 // extra_args requires it)
52 // -- cp : context
53 // -- sp[0] : last argument
54 // -- ...
55 // -- sp[4 * (argc - 1)] : first argument (argc == r0)
56 // -- sp[4 * argc] : receiver
57 // -----------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000058
Leon Clarkee46be812010-01-19 14:06:41 +000059 // Insert extra arguments.
60 int num_extra_args = 0;
61 if (extra_args == NEEDS_CALLED_FUNCTION) {
62 num_extra_args = 1;
63 __ push(r1);
64 } else {
65 ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
66 }
67
Steve Block6ded16b2010-05-10 14:33:55 +010068 // JumpToExternalReference expects r0 to contain the number of arguments
Leon Clarkee46be812010-01-19 14:06:41 +000069 // including the receiver and the extra arguments.
70 __ add(r0, r0, Operand(num_extra_args + 1));
Steve Block6ded16b2010-05-10 14:33:55 +010071 __ JumpToExternalReference(ExternalReference(id));
Steve Blocka7e24c12009-10-30 11:49:00 +000072}
73
74
75// Load the built-in Array function from the current context.
76static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
77 // Load the global context.
78
79 __ ldr(result, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
80 __ ldr(result,
81 FieldMemOperand(result, GlobalObject::kGlobalContextOffset));
82 // Load the Array function from the global context.
83 __ ldr(result,
84 MemOperand(result,
85 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
86}
87
88
89// This constant has the same value as JSArray::kPreallocatedArrayElements and
90// if JSArray::kPreallocatedArrayElements is changed handling of loop unfolding
91// below should be reconsidered.
92static const int kLoopUnfoldLimit = 4;
93
94
95// Allocate an empty JSArray. The allocated array is put into the result
96// register. An elements backing store is allocated with size initial_capacity
97// and filled with the hole values.
98static void AllocateEmptyJSArray(MacroAssembler* masm,
99 Register array_function,
100 Register result,
101 Register scratch1,
102 Register scratch2,
103 Register scratch3,
104 int initial_capacity,
105 Label* gc_required) {
106 ASSERT(initial_capacity > 0);
107 // Load the initial map from the array function.
108 __ ldr(scratch1, FieldMemOperand(array_function,
109 JSFunction::kPrototypeOrInitialMapOffset));
110
111 // Allocate the JSArray object together with space for a fixed array with the
112 // requested elements.
113 int size = JSArray::kSize + FixedArray::SizeFor(initial_capacity);
Kristian Monsen25f61362010-05-21 11:50:48 +0100114 __ AllocateInNewSpace(size,
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 result,
116 scratch2,
117 scratch3,
118 gc_required,
119 TAG_OBJECT);
120
121 // Allocated the JSArray. Now initialize the fields except for the elements
122 // array.
123 // result: JSObject
124 // scratch1: initial map
125 // scratch2: start of next object
126 __ str(scratch1, FieldMemOperand(result, JSObject::kMapOffset));
127 __ LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
128 __ str(scratch1, FieldMemOperand(result, JSArray::kPropertiesOffset));
129 // Field JSArray::kElementsOffset is initialized later.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100130 __ mov(scratch3, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000131 __ str(scratch3, FieldMemOperand(result, JSArray::kLengthOffset));
132
133 // Calculate the location of the elements array and set elements array member
134 // of the JSArray.
135 // result: JSObject
136 // scratch2: start of next object
Leon Clarkef7060e22010-06-03 12:02:55 +0100137 __ add(scratch1, result, Operand(JSArray::kSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000138 __ str(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
139
140 // Clear the heap tag on the elements array.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100141 ASSERT(kSmiTag == 0);
142 __ sub(scratch1, scratch1, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000143
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100144 // Initialize the FixedArray and fill it with holes. FixedArray length is
Steve Blocka7e24c12009-10-30 11:49:00 +0000145 // stored as a smi.
146 // result: JSObject
147 // scratch1: elements array (untagged)
148 // scratch2: start of next object
149 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex);
150 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
151 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100152 __ mov(scratch3, Operand(Smi::FromInt(initial_capacity)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000153 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
154 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
155
156 // Fill the FixedArray with the hole value.
157 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
158 ASSERT(initial_capacity <= kLoopUnfoldLimit);
159 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex);
160 for (int i = 0; i < initial_capacity; i++) {
161 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
162 }
163}
164
165// Allocate a JSArray with the number of elements stored in a register. The
166// register array_function holds the built-in Array function and the register
167// array_size holds the size of the array as a smi. The allocated array is put
168// into the result register and beginning and end of the FixedArray elements
169// storage is put into registers elements_array_storage and elements_array_end
170// (see below for when that is not the case). If the parameter fill_with_holes
171// is true the allocated elements backing store is filled with the hole values
172// otherwise it is left uninitialized. When the backing store is filled the
173// register elements_array_storage is scratched.
174static void AllocateJSArray(MacroAssembler* masm,
175 Register array_function, // Array function.
176 Register array_size, // As a smi.
177 Register result,
178 Register elements_array_storage,
179 Register elements_array_end,
180 Register scratch1,
181 Register scratch2,
182 bool fill_with_hole,
183 Label* gc_required) {
184 Label not_empty, allocated;
185
186 // Load the initial map from the array function.
187 __ ldr(elements_array_storage,
188 FieldMemOperand(array_function,
189 JSFunction::kPrototypeOrInitialMapOffset));
190
191 // Check whether an empty sized array is requested.
192 __ tst(array_size, array_size);
Steve Block1e0659c2011-05-24 12:43:12 +0100193 __ b(ne, &not_empty);
Steve Blocka7e24c12009-10-30 11:49:00 +0000194
195 // If an empty array is requested allocate a small elements array anyway. This
196 // keeps the code below free of special casing for the empty array.
197 int size = JSArray::kSize +
198 FixedArray::SizeFor(JSArray::kPreallocatedArrayElements);
Kristian Monsen25f61362010-05-21 11:50:48 +0100199 __ AllocateInNewSpace(size,
Steve Blocka7e24c12009-10-30 11:49:00 +0000200 result,
201 elements_array_end,
202 scratch1,
203 gc_required,
204 TAG_OBJECT);
205 __ jmp(&allocated);
206
207 // Allocate the JSArray object together with space for a FixedArray with the
208 // requested number of elements.
209 __ bind(&not_empty);
210 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
211 __ mov(elements_array_end,
212 Operand((JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize));
213 __ add(elements_array_end,
214 elements_array_end,
215 Operand(array_size, ASR, kSmiTagSize));
Kristian Monsen25f61362010-05-21 11:50:48 +0100216 __ AllocateInNewSpace(
217 elements_array_end,
218 result,
219 scratch1,
220 scratch2,
221 gc_required,
222 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
Steve Blocka7e24c12009-10-30 11:49:00 +0000223
224 // Allocated the JSArray. Now initialize the fields except for the elements
225 // array.
226 // result: JSObject
227 // elements_array_storage: initial map
228 // array_size: size of array (smi)
229 __ bind(&allocated);
230 __ str(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset));
231 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex);
232 __ str(elements_array_storage,
233 FieldMemOperand(result, JSArray::kPropertiesOffset));
234 // Field JSArray::kElementsOffset is initialized later.
235 __ str(array_size, FieldMemOperand(result, JSArray::kLengthOffset));
236
237 // Calculate the location of the elements array and set elements array member
238 // of the JSArray.
239 // result: JSObject
240 // array_size: size of array (smi)
241 __ add(elements_array_storage, result, Operand(JSArray::kSize));
242 __ str(elements_array_storage,
243 FieldMemOperand(result, JSArray::kElementsOffset));
244
245 // Clear the heap tag on the elements array.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100246 ASSERT(kSmiTag == 0);
247 __ sub(elements_array_storage,
248 elements_array_storage,
249 Operand(kHeapObjectTag));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100250 // Initialize the fixed array and fill it with holes. FixedArray length is
Steve Blocka7e24c12009-10-30 11:49:00 +0000251 // stored as a smi.
252 // result: JSObject
253 // elements_array_storage: elements array (untagged)
254 // array_size: size of array (smi)
Steve Blocka7e24c12009-10-30 11:49:00 +0000255 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex);
256 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
257 __ str(scratch1, MemOperand(elements_array_storage, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100258 ASSERT(kSmiTag == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 __ tst(array_size, array_size);
260 // Length of the FixedArray is the number of pre-allocated elements if
261 // the actual JSArray has length 0 and the size of the JSArray for non-empty
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100262 // JSArrays. The length of a FixedArray is stored as a smi.
263 __ mov(array_size,
264 Operand(Smi::FromInt(JSArray::kPreallocatedArrayElements)),
265 LeaveCC,
266 eq);
Steve Blocka7e24c12009-10-30 11:49:00 +0000267 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
268 __ str(array_size,
269 MemOperand(elements_array_storage, kPointerSize, PostIndex));
270
271 // Calculate elements array and elements array end.
272 // result: JSObject
273 // elements_array_storage: elements array element storage
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100274 // array_size: smi-tagged size of elements array
275 ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000276 __ add(elements_array_end,
277 elements_array_storage,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100278 Operand(array_size, LSL, kPointerSizeLog2 - kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000279
280 // Fill the allocated FixedArray with the hole value if requested.
281 // result: JSObject
282 // elements_array_storage: elements array element storage
283 // elements_array_end: start of next object
284 if (fill_with_hole) {
285 Label loop, entry;
286 __ LoadRoot(scratch1, Heap::kTheHoleValueRootIndex);
287 __ jmp(&entry);
288 __ bind(&loop);
289 __ str(scratch1,
290 MemOperand(elements_array_storage, kPointerSize, PostIndex));
291 __ bind(&entry);
292 __ cmp(elements_array_storage, elements_array_end);
293 __ b(lt, &loop);
294 }
295}
296
297// Create a new array for the built-in Array function. This function allocates
298// the JSArray object and the FixedArray elements array and initializes these.
299// If the Array cannot be constructed in native code the runtime is called. This
300// function assumes the following state:
301// r0: argc
302// r1: constructor (built-in Array function)
303// lr: return address
304// sp[0]: last argument
305// This function is used for both construct and normal calls of Array. The only
306// difference between handling a construct call and a normal call is that for a
307// construct call the constructor function in r1 needs to be preserved for
308// entering the generic code. In both cases argc in r0 needs to be preserved.
309// Both registers are preserved by this code so no need to differentiate between
310// construct call and normal call.
311static void ArrayNativeCode(MacroAssembler* masm,
Steve Blockd0582a62009-12-15 09:54:21 +0000312 Label* call_generic_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 Label argc_one_or_more, argc_two_or_more;
314
315 // Check for array construction with zero arguments or one.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100316 __ cmp(r0, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 __ b(ne, &argc_one_or_more);
318
319 // Handle construction of an empty array.
320 AllocateEmptyJSArray(masm,
321 r1,
322 r2,
323 r3,
324 r4,
325 r5,
326 JSArray::kPreallocatedArrayElements,
327 call_generic_code);
328 __ IncrementCounter(&Counters::array_function_native, 1, r3, r4);
329 // Setup return value, remove receiver from stack and return.
330 __ mov(r0, r2);
331 __ add(sp, sp, Operand(kPointerSize));
332 __ Jump(lr);
333
334 // Check for one argument. Bail out if argument is not smi or if it is
335 // negative.
336 __ bind(&argc_one_or_more);
337 __ cmp(r0, Operand(1));
338 __ b(ne, &argc_two_or_more);
339 ASSERT(kSmiTag == 0);
340 __ ldr(r2, MemOperand(sp)); // Get the argument from the stack.
341 __ and_(r3, r2, Operand(kIntptrSignBit | kSmiTagMask), SetCC);
342 __ b(ne, call_generic_code);
343
344 // Handle construction of an empty array of a certain size. Bail out if size
345 // is too large to actually allocate an elements array.
346 ASSERT(kSmiTag == 0);
347 __ cmp(r2, Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize));
348 __ b(ge, call_generic_code);
349
350 // r0: argc
351 // r1: constructor
352 // r2: array_size (smi)
353 // sp[0]: argument
354 AllocateJSArray(masm,
355 r1,
356 r2,
357 r3,
358 r4,
359 r5,
360 r6,
361 r7,
362 true,
363 call_generic_code);
364 __ IncrementCounter(&Counters::array_function_native, 1, r2, r4);
365 // Setup return value, remove receiver and argument from stack and return.
366 __ mov(r0, r3);
367 __ add(sp, sp, Operand(2 * kPointerSize));
368 __ Jump(lr);
369
370 // Handle construction of an array from a list of arguments.
371 __ bind(&argc_two_or_more);
372 __ mov(r2, Operand(r0, LSL, kSmiTagSize)); // Convet argc to a smi.
373
374 // r0: argc
375 // r1: constructor
376 // r2: array_size (smi)
377 // sp[0]: last argument
378 AllocateJSArray(masm,
379 r1,
380 r2,
381 r3,
382 r4,
383 r5,
384 r6,
385 r7,
386 false,
387 call_generic_code);
388 __ IncrementCounter(&Counters::array_function_native, 1, r2, r6);
389
390 // Fill arguments as array elements. Copy from the top of the stack (last
391 // element) to the array backing store filling it backwards. Note:
392 // elements_array_end points after the backing store therefore PreIndex is
393 // used when filling the backing store.
394 // r0: argc
395 // r3: JSArray
396 // r4: elements_array storage start (untagged)
397 // r5: elements_array_end (untagged)
398 // sp[0]: last argument
399 Label loop, entry;
400 __ jmp(&entry);
401 __ bind(&loop);
402 __ ldr(r2, MemOperand(sp, kPointerSize, PostIndex));
403 __ str(r2, MemOperand(r5, -kPointerSize, PreIndex));
404 __ bind(&entry);
405 __ cmp(r4, r5);
406 __ b(lt, &loop);
407
408 // Remove caller arguments and receiver from the stack, setup return value and
409 // return.
410 // r0: argc
411 // r3: JSArray
412 // sp[0]: receiver
413 __ add(sp, sp, Operand(kPointerSize));
414 __ mov(r0, r3);
415 __ Jump(lr);
416}
417
418
419void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
420 // ----------- S t a t e -------------
421 // -- r0 : number of arguments
422 // -- lr : return address
423 // -- sp[...]: constructor arguments
424 // -----------------------------------
425 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
426
427 // Get the Array function.
428 GenerateLoadArrayFunction(masm, r1);
429
430 if (FLAG_debug_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100431 // Initial map for the builtin Array functions should be maps.
Steve Blocka7e24c12009-10-30 11:49:00 +0000432 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
433 __ tst(r2, Operand(kSmiTagMask));
434 __ Assert(ne, "Unexpected initial map for Array function");
435 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
436 __ Assert(eq, "Unexpected initial map for Array function");
437 }
438
439 // Run the native code for the Array function called as a normal function.
440 ArrayNativeCode(masm, &generic_array_code);
441
442 // Jump to the generic array code if the specialized code cannot handle
443 // the construction.
444 __ bind(&generic_array_code);
445 Code* code = Builtins::builtin(Builtins::ArrayCodeGeneric);
446 Handle<Code> array_code(code);
447 __ Jump(array_code, RelocInfo::CODE_TARGET);
448}
449
450
451void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
452 // ----------- S t a t e -------------
453 // -- r0 : number of arguments
454 // -- r1 : constructor function
455 // -- lr : return address
456 // -- sp[...]: constructor arguments
457 // -----------------------------------
458 Label generic_constructor;
459
460 if (FLAG_debug_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100461 // The array construct code is only set for the builtin and internal
462 // Array functions which always have a map.
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 // Initial map for the builtin Array function should be a map.
464 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
465 __ tst(r2, Operand(kSmiTagMask));
466 __ Assert(ne, "Unexpected initial map for Array function");
467 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
468 __ Assert(eq, "Unexpected initial map for Array function");
469 }
470
471 // Run the native code for the Array function called as a constructor.
472 ArrayNativeCode(masm, &generic_constructor);
473
474 // Jump to the generic construct code in case the specialized code cannot
475 // handle the construction.
476 __ bind(&generic_constructor);
477 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
478 Handle<Code> generic_construct_stub(code);
479 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
480}
481
482
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100483void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800484 // ----------- S t a t e -------------
485 // -- r0 : number of arguments
486 // -- r1 : constructor function
487 // -- lr : return address
488 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
489 // -- sp[argc * 4] : receiver
490 // -----------------------------------
491 __ IncrementCounter(&Counters::string_ctor_calls, 1, r2, r3);
492
493 Register function = r1;
494 if (FLAG_debug_code) {
495 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, r2);
496 __ cmp(function, Operand(r2));
497 __ Assert(eq, "Unexpected String function");
498 }
499
500 // Load the first arguments in r0 and get rid of the rest.
501 Label no_arguments;
Ben Murdochb8e0da22011-05-16 14:20:40 +0100502 __ cmp(r0, Operand(0, RelocInfo::NONE));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800503 __ b(eq, &no_arguments);
504 // First args = sp[(argc - 1) * 4].
505 __ sub(r0, r0, Operand(1));
506 __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
507 // sp now point to args[0], drop args[0] + receiver.
508 __ Drop(2);
509
510 Register argument = r2;
511 Label not_cached, argument_is_string;
512 NumberToStringStub::GenerateLookupNumberStringCache(
513 masm,
514 r0, // Input.
515 argument, // Result.
516 r3, // Scratch.
517 r4, // Scratch.
518 r5, // Scratch.
519 false, // Is it a Smi?
520 &not_cached);
521 __ IncrementCounter(&Counters::string_ctor_cached_number, 1, r3, r4);
522 __ bind(&argument_is_string);
523
524 // ----------- S t a t e -------------
525 // -- r2 : argument converted to string
526 // -- r1 : constructor function
527 // -- lr : return address
528 // -----------------------------------
529
530 Label gc_required;
531 __ AllocateInNewSpace(JSValue::kSize,
532 r0, // Result.
533 r3, // Scratch.
534 r4, // Scratch.
535 &gc_required,
536 TAG_OBJECT);
537
538 // Initialising the String Object.
539 Register map = r3;
540 __ LoadGlobalFunctionInitialMap(function, map, r4);
541 if (FLAG_debug_code) {
542 __ ldrb(r4, FieldMemOperand(map, Map::kInstanceSizeOffset));
543 __ cmp(r4, Operand(JSValue::kSize >> kPointerSizeLog2));
544 __ Assert(eq, "Unexpected string wrapper instance size");
545 __ ldrb(r4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100546 __ cmp(r4, Operand(0, RelocInfo::NONE));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800547 __ Assert(eq, "Unexpected unused properties of string wrapper");
548 }
549 __ str(map, FieldMemOperand(r0, HeapObject::kMapOffset));
550
551 __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
552 __ str(r3, FieldMemOperand(r0, JSObject::kPropertiesOffset));
553 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
554
555 __ str(argument, FieldMemOperand(r0, JSValue::kValueOffset));
556
557 // Ensure the object is fully initialized.
558 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
559
560 __ Ret();
561
562 // The argument was not found in the number to string cache. Check
563 // if it's a string already before calling the conversion builtin.
564 Label convert_argument;
565 __ bind(&not_cached);
Steve Block1e0659c2011-05-24 12:43:12 +0100566 __ JumpIfSmi(r0, &convert_argument);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800567
568 // Is it a String?
569 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
570 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
571 ASSERT(kNotStringTag != 0);
572 __ tst(r3, Operand(kIsNotStringMask));
573 __ b(ne, &convert_argument);
574 __ mov(argument, r0);
575 __ IncrementCounter(&Counters::string_ctor_conversions, 1, r3, r4);
576 __ b(&argument_is_string);
577
578 // Invoke the conversion builtin and put the result into r2.
579 __ bind(&convert_argument);
580 __ push(function); // Preserve the function.
581 __ IncrementCounter(&Counters::string_ctor_conversions, 1, r3, r4);
582 __ EnterInternalFrame();
583 __ push(r0);
584 __ InvokeBuiltin(Builtins::TO_STRING, CALL_JS);
585 __ LeaveInternalFrame();
586 __ pop(function);
587 __ mov(argument, r0);
588 __ b(&argument_is_string);
589
590 // Load the empty string into r2, remove the receiver from the
591 // stack, and jump back to the case where the argument is a string.
592 __ bind(&no_arguments);
593 __ LoadRoot(argument, Heap::kEmptyStringRootIndex);
594 __ Drop(1);
595 __ b(&argument_is_string);
596
597 // At this point the argument is already a string. Call runtime to
598 // create a string wrapper.
599 __ bind(&gc_required);
600 __ IncrementCounter(&Counters::string_ctor_gc_required, 1, r3, r4);
601 __ EnterInternalFrame();
602 __ push(argument);
603 __ CallRuntime(Runtime::kNewStringWrapper, 1);
604 __ LeaveInternalFrame();
605 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100606}
607
608
Steve Blocka7e24c12009-10-30 11:49:00 +0000609void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
610 // ----------- S t a t e -------------
611 // -- r0 : number of arguments
612 // -- r1 : constructor function
613 // -- lr : return address
614 // -- sp[...]: constructor arguments
615 // -----------------------------------
616
617 Label non_function_call;
618 // Check that the function is not a smi.
619 __ tst(r1, Operand(kSmiTagMask));
620 __ b(eq, &non_function_call);
621 // Check that the function is a JSFunction.
622 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
623 __ b(ne, &non_function_call);
624
625 // Jump to the function-specific construct stub.
626 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
627 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kConstructStubOffset));
628 __ add(pc, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
629
630 // r0: number of arguments
631 // r1: called object
632 __ bind(&non_function_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000633 // Set expected number of arguments to zero (not changing r0).
Iain Merrick9ac36c92010-09-13 15:29:50 +0100634 __ mov(r2, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
636 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
637 RelocInfo::CODE_TARGET);
638}
639
640
Leon Clarkee46be812010-01-19 14:06:41 +0000641static void Generate_JSConstructStubHelper(MacroAssembler* masm,
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100642 bool is_api_function,
643 bool count_constructions) {
644 // Should never count constructions for api objects.
645 ASSERT(!is_api_function || !count_constructions);
646
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 // Enter a construct frame.
648 __ EnterConstructFrame();
649
650 // Preserve the two incoming parameters on the stack.
651 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
652 __ push(r0); // Smi-tagged arguments count.
653 __ push(r1); // Constructor function.
654
Steve Blocka7e24c12009-10-30 11:49:00 +0000655 // Try to allocate the object without transitioning into C code. If any of the
656 // preconditions is not met, the code bails out to the runtime call.
657 Label rt_call, allocated;
658 if (FLAG_inline_new) {
659 Label undo_allocation;
660#ifdef ENABLE_DEBUGGER_SUPPORT
661 ExternalReference debug_step_in_fp =
662 ExternalReference::debug_step_in_fp_address();
663 __ mov(r2, Operand(debug_step_in_fp));
664 __ ldr(r2, MemOperand(r2));
665 __ tst(r2, r2);
Steve Block1e0659c2011-05-24 12:43:12 +0100666 __ b(ne, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000667#endif
668
669 // Load the initial map and verify that it is in fact a map.
670 // r1: constructor function
Steve Blocka7e24c12009-10-30 11:49:00 +0000671 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
672 __ tst(r2, Operand(kSmiTagMask));
673 __ b(eq, &rt_call);
674 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
675 __ b(ne, &rt_call);
676
677 // Check that the constructor is not constructing a JSFunction (see comments
678 // in Runtime_NewObject in runtime.cc). In which case the initial map's
679 // instance type would be JS_FUNCTION_TYPE.
680 // r1: constructor function
681 // r2: initial map
Steve Blocka7e24c12009-10-30 11:49:00 +0000682 __ CompareInstanceType(r2, r3, JS_FUNCTION_TYPE);
683 __ b(eq, &rt_call);
684
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100685 if (count_constructions) {
686 Label allocate;
687 // Decrease generous allocation count.
688 __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
689 MemOperand constructor_count =
690 FieldMemOperand(r3, SharedFunctionInfo::kConstructionCountOffset);
691 __ ldrb(r4, constructor_count);
692 __ sub(r4, r4, Operand(1), SetCC);
693 __ strb(r4, constructor_count);
694 __ b(ne, &allocate);
695
696 __ Push(r1, r2);
697
698 __ push(r1); // constructor
699 // The call will replace the stub, so the countdown is only done once.
700 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
701
702 __ pop(r2);
703 __ pop(r1);
704
705 __ bind(&allocate);
706 }
707
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 // Now allocate the JSObject on the heap.
709 // r1: constructor function
710 // r2: initial map
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceSizeOffset));
Kristian Monsen25f61362010-05-21 11:50:48 +0100712 __ AllocateInNewSpace(r3, r4, r5, r6, &rt_call, SIZE_IN_WORDS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000713
714 // Allocated the JSObject, now initialize the fields. Map is set to initial
715 // map and properties and elements are set to empty fixed array.
716 // r1: constructor function
717 // r2: initial map
718 // r3: object size
719 // r4: JSObject (not tagged)
Steve Blocka7e24c12009-10-30 11:49:00 +0000720 __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
721 __ mov(r5, r4);
722 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
723 __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
724 ASSERT_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
725 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
726 ASSERT_EQ(2 * kPointerSize, JSObject::kElementsOffset);
727 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
728
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100729 // Fill all the in-object properties with the appropriate filler.
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 // r1: constructor function
731 // r2: initial map
732 // r3: object size (in words)
733 // r4: JSObject (not tagged)
734 // r5: First in-object property of JSObject (not tagged)
Steve Blocka7e24c12009-10-30 11:49:00 +0000735 __ add(r6, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
736 ASSERT_EQ(3 * kPointerSize, JSObject::kHeaderSize);
737 { Label loop, entry;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100738 if (count_constructions) {
739 // To allow for truncation.
740 __ LoadRoot(r7, Heap::kOnePointerFillerMapRootIndex);
741 } else {
742 __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
743 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000744 __ b(&entry);
745 __ bind(&loop);
746 __ str(r7, MemOperand(r5, kPointerSize, PostIndex));
747 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100748 __ cmp(r5, r6);
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 __ b(lt, &loop);
750 }
751
752 // Add the object tag to make the JSObject real, so that we can continue and
753 // jump into the continuation code at any time from now on. Any failures
754 // need to undo the allocation, so that the heap is in a consistent state
755 // and verifiable.
756 __ add(r4, r4, Operand(kHeapObjectTag));
757
758 // Check if a non-empty properties array is needed. Continue with allocated
759 // object if not fall through to runtime call if it is.
760 // r1: constructor function
761 // r4: JSObject
762 // r5: start of next object (not tagged)
Steve Blocka7e24c12009-10-30 11:49:00 +0000763 __ ldrb(r3, FieldMemOperand(r2, Map::kUnusedPropertyFieldsOffset));
764 // The field instance sizes contains both pre-allocated property fields and
765 // in-object properties.
766 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100767 __ Ubfx(r6, r0, Map::kPreAllocatedPropertyFieldsByte * 8, 8);
768 __ add(r3, r3, Operand(r6));
769 __ Ubfx(r6, r0, Map::kInObjectPropertiesByte * 8, 8);
770 __ sub(r3, r3, Operand(r6), SetCC);
Steve Blocka7e24c12009-10-30 11:49:00 +0000771
772 // Done if no extra properties are to be allocated.
773 __ b(eq, &allocated);
774 __ Assert(pl, "Property allocation count failed.");
775
776 // Scale the number of elements by pointer size and add the header for
777 // FixedArrays to the start of the next object calculation from above.
778 // r1: constructor
779 // r3: number of elements in properties array
780 // r4: JSObject
781 // r5: start of next object
Steve Blocka7e24c12009-10-30 11:49:00 +0000782 __ add(r0, r3, Operand(FixedArray::kHeaderSize / kPointerSize));
Kristian Monsen25f61362010-05-21 11:50:48 +0100783 __ AllocateInNewSpace(
784 r0,
785 r5,
786 r6,
787 r2,
788 &undo_allocation,
789 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
Steve Blocka7e24c12009-10-30 11:49:00 +0000790
791 // Initialize the FixedArray.
792 // r1: constructor
793 // r3: number of elements in properties array
794 // r4: JSObject
795 // r5: FixedArray (not tagged)
Steve Blocka7e24c12009-10-30 11:49:00 +0000796 __ LoadRoot(r6, Heap::kFixedArrayMapRootIndex);
797 __ mov(r2, r5);
798 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
799 __ str(r6, MemOperand(r2, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100800 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
801 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
802 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +0000803
804 // Initialize the fields to undefined.
805 // r1: constructor function
806 // r2: First element of FixedArray (not tagged)
807 // r3: number of elements in properties array
808 // r4: JSObject
809 // r5: FixedArray (not tagged)
Steve Blocka7e24c12009-10-30 11:49:00 +0000810 __ add(r6, r2, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
811 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
812 { Label loop, entry;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100813 if (count_constructions) {
814 __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
815 } else if (FLAG_debug_code) {
816 __ LoadRoot(r8, Heap::kUndefinedValueRootIndex);
817 __ cmp(r7, r8);
818 __ Assert(eq, "Undefined value not loaded.");
819 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 __ b(&entry);
821 __ bind(&loop);
822 __ str(r7, MemOperand(r2, kPointerSize, PostIndex));
823 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100824 __ cmp(r2, r6);
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 __ b(lt, &loop);
826 }
827
828 // Store the initialized FixedArray into the properties field of
829 // the JSObject
830 // r1: constructor function
831 // r4: JSObject
832 // r5: FixedArray (not tagged)
833 __ add(r5, r5, Operand(kHeapObjectTag)); // Add the heap tag.
834 __ str(r5, FieldMemOperand(r4, JSObject::kPropertiesOffset));
835
836 // Continue with JSObject being successfully allocated
837 // r1: constructor function
838 // r4: JSObject
839 __ jmp(&allocated);
840
841 // Undo the setting of the new top so that the heap is verifiable. For
842 // example, the map's unused properties potentially do not match the
843 // allocated objects unused properties.
844 // r4: JSObject (previous new top)
845 __ bind(&undo_allocation);
846 __ UndoAllocationInNewSpace(r4, r5);
847 }
848
849 // Allocate the new receiver object using the runtime call.
850 // r1: constructor function
851 __ bind(&rt_call);
852 __ push(r1); // argument for Runtime_NewObject
853 __ CallRuntime(Runtime::kNewObject, 1);
854 __ mov(r4, r0);
855
856 // Receiver for constructor call allocated.
857 // r4: JSObject
858 __ bind(&allocated);
859 __ push(r4);
860
861 // Push the function and the allocated receiver from the stack.
862 // sp[0]: receiver (newly allocated object)
863 // sp[1]: constructor function
864 // sp[2]: number of arguments (smi-tagged)
865 __ ldr(r1, MemOperand(sp, kPointerSize));
866 __ push(r1); // Constructor function.
867 __ push(r4); // Receiver.
868
869 // Reload the number of arguments from the stack.
870 // r1: constructor function
871 // sp[0]: receiver
872 // sp[1]: constructor function
873 // sp[2]: receiver
874 // sp[3]: constructor function
875 // sp[4]: number of arguments (smi-tagged)
876 __ ldr(r3, MemOperand(sp, 4 * kPointerSize));
877
878 // Setup pointer to last argument.
879 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
880
881 // Setup number of arguments for function call below
882 __ mov(r0, Operand(r3, LSR, kSmiTagSize));
883
884 // Copy arguments and receiver to the expression stack.
885 // r0: number of arguments
886 // r2: address of last argument (caller sp)
887 // r1: constructor function
888 // r3: number of arguments (smi-tagged)
889 // sp[0]: receiver
890 // sp[1]: constructor function
891 // sp[2]: receiver
892 // sp[3]: constructor function
893 // sp[4]: number of arguments (smi-tagged)
894 Label loop, entry;
895 __ b(&entry);
896 __ bind(&loop);
897 __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
898 __ push(ip);
899 __ bind(&entry);
900 __ sub(r3, r3, Operand(2), SetCC);
901 __ b(ge, &loop);
902
903 // Call the function.
904 // r0: number of arguments
905 // r1: constructor function
Leon Clarkee46be812010-01-19 14:06:41 +0000906 if (is_api_function) {
907 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
908 Handle<Code> code = Handle<Code>(
909 Builtins::builtin(Builtins::HandleApiCallConstruct));
910 ParameterCount expected(0);
911 __ InvokeCode(code, expected, expected,
912 RelocInfo::CODE_TARGET, CALL_FUNCTION);
913 } else {
914 ParameterCount actual(r0);
915 __ InvokeFunction(r1, actual, CALL_FUNCTION);
916 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000917
918 // Pop the function from the stack.
919 // sp[0]: constructor function
920 // sp[2]: receiver
921 // sp[3]: constructor function
922 // sp[4]: number of arguments (smi-tagged)
923 __ pop();
924
925 // Restore context from the frame.
926 // r0: result
927 // sp[0]: receiver
928 // sp[1]: constructor function
929 // sp[2]: number of arguments (smi-tagged)
930 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
931
932 // If the result is an object (in the ECMA sense), we should get rid
933 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
934 // on page 74.
935 Label use_receiver, exit;
936
937 // If the result is a smi, it is *not* an object in the ECMA sense.
938 // r0: result
939 // sp[0]: receiver (newly allocated object)
940 // sp[1]: constructor function
941 // sp[2]: number of arguments (smi-tagged)
942 __ tst(r0, Operand(kSmiTagMask));
943 __ b(eq, &use_receiver);
944
945 // If the type of the result (stored in its map) is less than
946 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
947 __ CompareObjectType(r0, r3, r3, FIRST_JS_OBJECT_TYPE);
948 __ b(ge, &exit);
949
950 // Throw away the result of the constructor invocation and use the
951 // on-stack receiver as the result.
952 __ bind(&use_receiver);
953 __ ldr(r0, MemOperand(sp));
954
955 // Remove receiver from the stack, remove caller arguments, and
956 // return.
957 __ bind(&exit);
958 // r0: result
959 // sp[0]: receiver (newly allocated object)
960 // sp[1]: constructor function
961 // sp[2]: number of arguments (smi-tagged)
962 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
963 __ LeaveConstructFrame();
964 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
965 __ add(sp, sp, Operand(kPointerSize));
966 __ IncrementCounter(&Counters::constructed_objects, 1, r1, r2);
967 __ Jump(lr);
968}
969
970
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100971void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) {
972 Generate_JSConstructStubHelper(masm, false, true);
973}
974
975
Leon Clarkee46be812010-01-19 14:06:41 +0000976void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100977 Generate_JSConstructStubHelper(masm, false, false);
Leon Clarkee46be812010-01-19 14:06:41 +0000978}
979
980
981void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100982 Generate_JSConstructStubHelper(masm, true, false);
Leon Clarkee46be812010-01-19 14:06:41 +0000983}
984
985
Steve Blocka7e24c12009-10-30 11:49:00 +0000986static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
987 bool is_construct) {
988 // Called from Generate_JS_Entry
989 // r0: code entry
990 // r1: function
991 // r2: receiver
992 // r3: argc
993 // r4: argv
994 // r5-r7, cp may be clobbered
995
996 // Clear the context before we push it when entering the JS frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100997 __ mov(cp, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +0000998
999 // Enter an internal frame.
1000 __ EnterInternalFrame();
1001
1002 // Set up the context from the function argument.
1003 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1004
1005 // Set up the roots register.
1006 ExternalReference roots_address = ExternalReference::roots_address();
1007 __ mov(r10, Operand(roots_address));
1008
1009 // Push the function and the receiver onto the stack.
1010 __ push(r1);
1011 __ push(r2);
1012
1013 // Copy arguments to the stack in a loop.
1014 // r1: function
1015 // r3: argc
1016 // r4: argv, i.e. points to first arg
1017 Label loop, entry;
1018 __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
1019 // r2 points past last arg.
1020 __ b(&entry);
1021 __ bind(&loop);
1022 __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
1023 __ ldr(r0, MemOperand(r0)); // dereference handle
1024 __ push(r0); // push parameter
1025 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +01001026 __ cmp(r4, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001027 __ b(ne, &loop);
1028
1029 // Initialize all JavaScript callee-saved registers, since they will be seen
1030 // by the garbage collector as part of handlers.
1031 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
1032 __ mov(r5, Operand(r4));
1033 __ mov(r6, Operand(r4));
1034 __ mov(r7, Operand(r4));
1035 if (kR9Available == 1) {
1036 __ mov(r9, Operand(r4));
1037 }
1038
1039 // Invoke the code and pass argc as r0.
1040 __ mov(r0, Operand(r3));
1041 if (is_construct) {
1042 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
1043 RelocInfo::CODE_TARGET);
1044 } else {
1045 ParameterCount actual(r0);
1046 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1047 }
1048
1049 // Exit the JS frame and remove the parameters (except function), and return.
1050 // Respect ABI stack constraint.
1051 __ LeaveInternalFrame();
1052 __ Jump(lr);
1053
1054 // r0: result
1055}
1056
1057
1058void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
1059 Generate_JSEntryTrampolineHelper(masm, false);
1060}
1061
1062
1063void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
1064 Generate_JSEntryTrampolineHelper(masm, true);
1065}
1066
1067
Iain Merrick75681382010-08-19 15:07:18 +01001068void Builtins::Generate_LazyCompile(MacroAssembler* masm) {
1069 // Enter an internal frame.
1070 __ EnterInternalFrame();
1071
1072 // Preserve the function.
1073 __ push(r1);
1074
1075 // Push the function on the stack as the argument to the runtime function.
1076 __ push(r1);
1077 __ CallRuntime(Runtime::kLazyCompile, 1);
1078 // Calculate the entry point.
1079 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1080 // Restore saved function.
1081 __ pop(r1);
1082
1083 // Tear down temporary frame.
1084 __ LeaveInternalFrame();
1085
1086 // Do a tail-call of the compiled function.
1087 __ Jump(r2);
1088}
1089
1090
Ben Murdochb0fe1622011-05-05 13:52:32 +01001091void Builtins::Generate_LazyRecompile(MacroAssembler* masm) {
1092 // Enter an internal frame.
1093 __ EnterInternalFrame();
1094
1095 // Preserve the function.
1096 __ push(r1);
1097
1098 // Push the function on the stack as the argument to the runtime function.
1099 __ push(r1);
1100 __ CallRuntime(Runtime::kLazyRecompile, 1);
1101 // Calculate the entry point.
1102 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1103 // Restore saved function.
1104 __ pop(r1);
1105
1106 // Tear down temporary frame.
1107 __ LeaveInternalFrame();
1108
1109 // Do a tail-call of the compiled function.
1110 __ Jump(r2);
1111}
1112
1113
1114static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1115 Deoptimizer::BailoutType type) {
1116 __ EnterInternalFrame();
1117 // Pass the function and deoptimization type to the runtime system.
1118 __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
1119 __ push(r0);
1120 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1121 __ LeaveInternalFrame();
1122
1123 // Get the full codegen state from the stack and untag it -> r6.
1124 __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
1125 __ SmiUntag(r6);
1126 // Switch on the state.
1127 Label with_tos_register, unknown_state;
1128 __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
1129 __ b(ne, &with_tos_register);
1130 __ add(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1131 __ Ret();
1132
1133 __ bind(&with_tos_register);
1134 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
1135 __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
1136 __ b(ne, &unknown_state);
1137 __ add(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1138 __ Ret();
1139
1140 __ bind(&unknown_state);
1141 __ stop("no cases left");
1142}
1143
1144
1145void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1146 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1147}
1148
1149
1150void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1151 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1152}
1153
1154
1155void Builtins::Generate_NotifyOSR(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001156 // For now, we are relying on the fact that Runtime::NotifyOSR
1157 // doesn't do any garbage collection which allows us to save/restore
1158 // the registers without worrying about which of them contain
1159 // pointers. This seems a bit fragile.
1160 __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved | lr.bit() | fp.bit());
1161 __ EnterInternalFrame();
1162 __ CallRuntime(Runtime::kNotifyOSR, 0);
1163 __ LeaveInternalFrame();
1164 __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved | lr.bit() | fp.bit());
1165 __ Ret();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001166}
1167
1168
1169void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001170 // Probe the CPU to set the supported features, because this builtin
1171 // may be called before the initialization performs CPU setup.
1172 CpuFeatures::Probe(false);
1173
1174 // Lookup the function in the JavaScript frame and push it as an
1175 // argument to the on-stack replacement function.
1176 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1177 __ EnterInternalFrame();
1178 __ push(r0);
1179 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1180 __ LeaveInternalFrame();
1181
1182 // If the result was -1 it means that we couldn't optimize the
1183 // function. Just return and continue in the unoptimized version.
1184 Label skip;
1185 __ cmp(r0, Operand(Smi::FromInt(-1)));
1186 __ b(ne, &skip);
1187 __ Ret();
1188
1189 __ bind(&skip);
1190 // Untag the AST id and push it on the stack.
1191 __ SmiUntag(r0);
1192 __ push(r0);
1193
1194 // Generate the code for doing the frame-to-frame translation using
1195 // the deoptimizer infrastructure.
1196 Deoptimizer::EntryGenerator generator(masm, Deoptimizer::OSR);
1197 generator.Generate();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001198}
1199
1200
Steve Blocka7e24c12009-10-30 11:49:00 +00001201void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1202 // 1. Make sure we have at least one argument.
Andrei Popescu402d9372010-02-26 13:31:12 +00001203 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001204 { Label done;
1205 __ tst(r0, Operand(r0));
1206 __ b(ne, &done);
1207 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1208 __ push(r2);
1209 __ add(r0, r0, Operand(1));
1210 __ bind(&done);
1211 }
1212
Andrei Popescu402d9372010-02-26 13:31:12 +00001213 // 2. Get the function to call (passed as receiver) from the stack, check
1214 // if it is a function.
1215 // r0: actual number of arguments
1216 Label non_function;
1217 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1218 __ tst(r1, Operand(kSmiTagMask));
1219 __ b(eq, &non_function);
1220 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1221 __ b(ne, &non_function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001222
Andrei Popescu402d9372010-02-26 13:31:12 +00001223 // 3a. Patch the first argument if necessary when calling a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00001224 // r0: actual number of arguments
1225 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001226 Label shift_arguments;
1227 { Label convert_to_object, use_global_receiver, patch_receiver;
1228 // Change context eagerly in case we need the global receiver.
1229 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1230
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001231 // Do not transform the receiver for strict mode functions.
1232 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1233 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCompilerHintsOffset));
1234 __ tst(r2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1235 kSmiTagSize)));
1236 __ b(ne, &shift_arguments);
1237
1238 // Compute the receiver in non-strict mode.
Steve Blocka7e24c12009-10-30 11:49:00 +00001239 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1240 __ ldr(r2, MemOperand(r2, -kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001241 // r0: actual number of arguments
1242 // r1: function
1243 // r2: first argument
1244 __ tst(r2, Operand(kSmiTagMask));
Andrei Popescu402d9372010-02-26 13:31:12 +00001245 __ b(eq, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001246
1247 __ LoadRoot(r3, Heap::kNullValueRootIndex);
1248 __ cmp(r2, r3);
1249 __ b(eq, &use_global_receiver);
1250 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1251 __ cmp(r2, r3);
1252 __ b(eq, &use_global_receiver);
1253
1254 __ CompareObjectType(r2, r3, r3, FIRST_JS_OBJECT_TYPE);
Andrei Popescu402d9372010-02-26 13:31:12 +00001255 __ b(lt, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001256 __ cmp(r3, Operand(LAST_JS_OBJECT_TYPE));
Andrei Popescu402d9372010-02-26 13:31:12 +00001257 __ b(le, &shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001258
Andrei Popescu402d9372010-02-26 13:31:12 +00001259 __ bind(&convert_to_object);
1260 __ EnterInternalFrame(); // In order to preserve argument count.
1261 __ mov(r0, Operand(r0, LSL, kSmiTagSize)); // Smi-tagged.
Steve Blocka7e24c12009-10-30 11:49:00 +00001262 __ push(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001263
1264 __ push(r2);
1265 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
1266 __ mov(r2, r0);
1267
Steve Blocka7e24c12009-10-30 11:49:00 +00001268 __ pop(r0);
1269 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001270 __ LeaveInternalFrame();
Andrei Popescu402d9372010-02-26 13:31:12 +00001271 // Restore the function to r1.
1272 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1273 __ jmp(&patch_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001274
Andrei Popescu402d9372010-02-26 13:31:12 +00001275 // Use the global receiver object from the called function as the
1276 // receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +00001277 __ bind(&use_global_receiver);
1278 const int kGlobalIndex =
1279 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1280 __ ldr(r2, FieldMemOperand(cp, kGlobalIndex));
Steve Blockd0582a62009-12-15 09:54:21 +00001281 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
1282 __ ldr(r2, FieldMemOperand(r2, kGlobalIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +00001283 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalReceiverOffset));
1284
1285 __ bind(&patch_receiver);
1286 __ add(r3, sp, Operand(r0, LSL, kPointerSizeLog2));
1287 __ str(r2, MemOperand(r3, -kPointerSize));
1288
Andrei Popescu402d9372010-02-26 13:31:12 +00001289 __ jmp(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001290 }
1291
Andrei Popescu402d9372010-02-26 13:31:12 +00001292 // 3b. Patch the first argument when calling a non-function. The
1293 // CALL_NON_FUNCTION builtin expects the non-function callee as
1294 // receiver, so overwrite the first argument which will ultimately
1295 // become the receiver.
1296 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001297 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001298 __ bind(&non_function);
1299 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1300 __ str(r1, MemOperand(r2, -kPointerSize));
1301 // Clear r1 to indicate a non-function being called.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001302 __ mov(r1, Operand(0, RelocInfo::NONE));
Andrei Popescu402d9372010-02-26 13:31:12 +00001303
1304 // 4. Shift arguments and return address one slot down on the stack
1305 // (overwriting the original receiver). Adjust argument count to make
1306 // the original first argument the new receiver.
1307 // r0: actual number of arguments
1308 // r1: function
1309 __ bind(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001310 { Label loop;
1311 // Calculate the copy start address (destination). Copy end address is sp.
1312 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001313
1314 __ bind(&loop);
1315 __ ldr(ip, MemOperand(r2, -kPointerSize));
1316 __ str(ip, MemOperand(r2));
1317 __ sub(r2, r2, Operand(kPointerSize));
1318 __ cmp(r2, sp);
1319 __ b(ne, &loop);
Andrei Popescu402d9372010-02-26 13:31:12 +00001320 // Adjust the actual number of arguments and remove the top element
1321 // (which is a copy of the last argument).
1322 __ sub(r0, r0, Operand(1));
1323 __ pop();
Steve Blocka7e24c12009-10-30 11:49:00 +00001324 }
1325
Andrei Popescu402d9372010-02-26 13:31:12 +00001326 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin.
Steve Blocka7e24c12009-10-30 11:49:00 +00001327 // r0: actual number of arguments
1328 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001329 { Label function;
Steve Blocka7e24c12009-10-30 11:49:00 +00001330 __ tst(r1, r1);
Andrei Popescu402d9372010-02-26 13:31:12 +00001331 __ b(ne, &function);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001332 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1333 __ mov(r2, Operand(0, RelocInfo::NONE));
Steve Blocka7e24c12009-10-30 11:49:00 +00001334 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
1335 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
1336 RelocInfo::CODE_TARGET);
Andrei Popescu402d9372010-02-26 13:31:12 +00001337 __ bind(&function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001338 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001339
1340 // 5b. Get the code to call from the function and check that the number of
1341 // expected arguments matches what we're providing. If so, jump
1342 // (tail-call) to the code in register edx without checking arguments.
1343 // r0: actual number of arguments
1344 // r1: function
1345 __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1346 __ ldr(r2,
1347 FieldMemOperand(r3, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001348 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
Steve Block791712a2010-08-27 10:21:07 +01001349 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001350 __ cmp(r2, r0); // Check formal and actual parameter counts.
1351 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
1352 RelocInfo::CODE_TARGET, ne);
1353
1354 ParameterCount expected(0);
1355 __ InvokeCode(r3, expected, expected, JUMP_FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +00001356}
1357
1358
1359void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1360 const int kIndexOffset = -5 * kPointerSize;
1361 const int kLimitOffset = -4 * kPointerSize;
1362 const int kArgsOffset = 2 * kPointerSize;
1363 const int kRecvOffset = 3 * kPointerSize;
1364 const int kFunctionOffset = 4 * kPointerSize;
1365
1366 __ EnterInternalFrame();
1367
1368 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1369 __ push(r0);
1370 __ ldr(r0, MemOperand(fp, kArgsOffset)); // get the args array
1371 __ push(r0);
1372 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_JS);
1373
Steve Blockd0582a62009-12-15 09:54:21 +00001374 // Check the stack for overflow. We are not trying need to catch
1375 // interruptions (e.g. debug break and preemption) here, so the "real stack
1376 // limit" is checked.
Steve Blocka7e24c12009-10-30 11:49:00 +00001377 Label okay;
Steve Blockd0582a62009-12-15 09:54:21 +00001378 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
1379 // Make r2 the space we have left. The stack might already be overflowed
1380 // here which will cause r2 to become negative.
Steve Blocka7e24c12009-10-30 11:49:00 +00001381 __ sub(r2, sp, r2);
Steve Blockd0582a62009-12-15 09:54:21 +00001382 // Check if the arguments will overflow the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +00001383 __ cmp(r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
Steve Blockd0582a62009-12-15 09:54:21 +00001384 __ b(gt, &okay); // Signed comparison.
Steve Blocka7e24c12009-10-30 11:49:00 +00001385
1386 // Out of stack space.
1387 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1388 __ push(r1);
1389 __ push(r0);
1390 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_JS);
Steve Blockd0582a62009-12-15 09:54:21 +00001391 // End of stack check.
Steve Blocka7e24c12009-10-30 11:49:00 +00001392
1393 // Push current limit and index.
1394 __ bind(&okay);
1395 __ push(r0); // limit
Iain Merrick9ac36c92010-09-13 15:29:50 +01001396 __ mov(r1, Operand(0, RelocInfo::NONE)); // initial index
Steve Blocka7e24c12009-10-30 11:49:00 +00001397 __ push(r1);
1398
1399 // Change context eagerly to get the right global object if necessary.
1400 __ ldr(r0, MemOperand(fp, kFunctionOffset));
1401 __ ldr(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001402 // Load the shared function info while the function is still in r0.
1403 __ ldr(r1, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001404
1405 // Compute the receiver.
1406 Label call_to_object, use_global_receiver, push_receiver;
1407 __ ldr(r0, MemOperand(fp, kRecvOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001408
1409 // Do not transform the receiver for strict mode functions.
1410 __ ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kCompilerHintsOffset));
1411 __ tst(r1, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1412 kSmiTagSize)));
1413 __ b(ne, &push_receiver);
1414
1415 // Compute the receiver in non-strict mode.
Steve Blocka7e24c12009-10-30 11:49:00 +00001416 __ tst(r0, Operand(kSmiTagMask));
1417 __ b(eq, &call_to_object);
1418 __ LoadRoot(r1, Heap::kNullValueRootIndex);
1419 __ cmp(r0, r1);
1420 __ b(eq, &use_global_receiver);
1421 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1422 __ cmp(r0, r1);
1423 __ b(eq, &use_global_receiver);
1424
1425 // Check if the receiver is already a JavaScript object.
1426 // r0: receiver
1427 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
1428 __ b(lt, &call_to_object);
1429 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1430 __ b(le, &push_receiver);
1431
1432 // Convert the receiver to a regular object.
1433 // r0: receiver
1434 __ bind(&call_to_object);
1435 __ push(r0);
1436 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
1437 __ b(&push_receiver);
1438
1439 // Use the current global receiver object as the receiver.
1440 __ bind(&use_global_receiver);
1441 const int kGlobalOffset =
1442 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1443 __ ldr(r0, FieldMemOperand(cp, kGlobalOffset));
Steve Blockd0582a62009-12-15 09:54:21 +00001444 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalContextOffset));
1445 __ ldr(r0, FieldMemOperand(r0, kGlobalOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001446 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalReceiverOffset));
1447
1448 // Push the receiver.
1449 // r0: receiver
1450 __ bind(&push_receiver);
1451 __ push(r0);
1452
1453 // Copy all arguments from the array to the stack.
1454 Label entry, loop;
1455 __ ldr(r0, MemOperand(fp, kIndexOffset));
1456 __ b(&entry);
1457
1458 // Load the current argument from the arguments array and push it to the
1459 // stack.
1460 // r0: current argument index
1461 __ bind(&loop);
1462 __ ldr(r1, MemOperand(fp, kArgsOffset));
1463 __ push(r1);
1464 __ push(r0);
1465
1466 // Call the runtime to access the property in the arguments array.
1467 __ CallRuntime(Runtime::kGetProperty, 2);
1468 __ push(r0);
1469
1470 // Use inline caching to access the arguments.
1471 __ ldr(r0, MemOperand(fp, kIndexOffset));
1472 __ add(r0, r0, Operand(1 << kSmiTagSize));
1473 __ str(r0, MemOperand(fp, kIndexOffset));
1474
1475 // Test if the copy loop has finished copying all the elements from the
1476 // arguments object.
1477 __ bind(&entry);
1478 __ ldr(r1, MemOperand(fp, kLimitOffset));
1479 __ cmp(r0, r1);
1480 __ b(ne, &loop);
1481
1482 // Invoke the function.
1483 ParameterCount actual(r0);
1484 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1485 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1486 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1487
1488 // Tear down the internal frame and remove function, receiver and args.
1489 __ LeaveInternalFrame();
1490 __ add(sp, sp, Operand(3 * kPointerSize));
1491 __ Jump(lr);
1492}
1493
1494
1495static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1496 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
1497 __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1498 __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() | fp.bit() | lr.bit());
1499 __ add(fp, sp, Operand(3 * kPointerSize));
1500}
1501
1502
1503static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1504 // ----------- S t a t e -------------
1505 // -- r0 : result being passed through
1506 // -----------------------------------
1507 // Get the number of arguments passed (as a smi), tear down the frame and
1508 // then tear down the parameters.
1509 __ ldr(r1, MemOperand(fp, -3 * kPointerSize));
1510 __ mov(sp, fp);
1511 __ ldm(ia_w, sp, fp.bit() | lr.bit());
1512 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1513 __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1514}
1515
1516
1517void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1518 // ----------- S t a t e -------------
1519 // -- r0 : actual number of arguments
1520 // -- r1 : function (passed through to callee)
1521 // -- r2 : expected number of arguments
1522 // -- r3 : code entry to call
1523 // -----------------------------------
1524
1525 Label invoke, dont_adapt_arguments;
1526
1527 Label enough, too_few;
Steve Block6ded16b2010-05-10 14:33:55 +01001528 __ cmp(r0, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001529 __ b(lt, &too_few);
1530 __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1531 __ b(eq, &dont_adapt_arguments);
1532
1533 { // Enough parameters: actual >= expected
1534 __ bind(&enough);
1535 EnterArgumentsAdaptorFrame(masm);
1536
1537 // Calculate copy start address into r0 and copy end address into r2.
1538 // r0: actual number of arguments as a smi
1539 // r1: function
1540 // r2: expected number of arguments
1541 // r3: code entry to call
1542 __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1543 // adjust for return address and receiver
1544 __ add(r0, r0, Operand(2 * kPointerSize));
1545 __ sub(r2, r0, Operand(r2, LSL, kPointerSizeLog2));
1546
1547 // Copy the arguments (including the receiver) to the new stack frame.
1548 // r0: copy start address
1549 // r1: function
1550 // r2: copy end address
1551 // r3: code entry to call
1552
1553 Label copy;
1554 __ bind(&copy);
1555 __ ldr(ip, MemOperand(r0, 0));
1556 __ push(ip);
1557 __ cmp(r0, r2); // Compare before moving to next argument.
1558 __ sub(r0, r0, Operand(kPointerSize));
1559 __ b(ne, &copy);
1560
1561 __ b(&invoke);
1562 }
1563
1564 { // Too few parameters: Actual < expected
1565 __ bind(&too_few);
1566 EnterArgumentsAdaptorFrame(masm);
1567
1568 // Calculate copy start address into r0 and copy end address is fp.
1569 // r0: actual number of arguments as a smi
1570 // r1: function
1571 // r2: expected number of arguments
1572 // r3: code entry to call
1573 __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1574
1575 // Copy the arguments (including the receiver) to the new stack frame.
1576 // r0: copy start address
1577 // r1: function
1578 // r2: expected number of arguments
1579 // r3: code entry to call
1580 Label copy;
1581 __ bind(&copy);
1582 // Adjust load for return address and receiver.
1583 __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1584 __ push(ip);
1585 __ cmp(r0, fp); // Compare before moving to next argument.
1586 __ sub(r0, r0, Operand(kPointerSize));
1587 __ b(ne, &copy);
1588
1589 // Fill the remaining expected arguments with undefined.
1590 // r1: function
1591 // r2: expected number of arguments
1592 // r3: code entry to call
1593 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1594 __ sub(r2, fp, Operand(r2, LSL, kPointerSizeLog2));
1595 __ sub(r2, r2, Operand(4 * kPointerSize)); // Adjust for frame.
1596
1597 Label fill;
1598 __ bind(&fill);
1599 __ push(ip);
1600 __ cmp(sp, r2);
1601 __ b(ne, &fill);
1602 }
1603
1604 // Call the entry point.
1605 __ bind(&invoke);
1606 __ Call(r3);
1607
1608 // Exit frame and return.
1609 LeaveArgumentsAdaptorFrame(masm);
1610 __ Jump(lr);
1611
1612
1613 // -------------------------------------------
1614 // Dont adapt arguments.
1615 // -------------------------------------------
1616 __ bind(&dont_adapt_arguments);
1617 __ Jump(r3);
1618}
1619
1620
1621#undef __
1622
1623} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001624
1625#endif // V8_TARGET_ARCH_ARM