blob: a902fc21d69aa6b97594de3db6ad7f2d3635b197 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// 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"
34#include "runtime.h"
35
36namespace v8 {
37namespace internal {
38
39
40#define __ ACCESS_MASM(masm)
41
42
Leon Clarkee46be812010-01-19 14:06:41 +000043void Builtins::Generate_Adaptor(MacroAssembler* masm,
44 CFunctionId id,
45 BuiltinExtraArguments extra_args) {
46 // ----------- S t a t e -------------
47 // -- r0 : number of arguments excluding receiver
48 // -- r1 : called function (only guaranteed when
49 // extra_args requires it)
50 // -- cp : context
51 // -- sp[0] : last argument
52 // -- ...
53 // -- sp[4 * (argc - 1)] : first argument (argc == r0)
54 // -- sp[4 * argc] : receiver
55 // -----------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000056
Leon Clarkee46be812010-01-19 14:06:41 +000057 // Insert extra arguments.
58 int num_extra_args = 0;
59 if (extra_args == NEEDS_CALLED_FUNCTION) {
60 num_extra_args = 1;
61 __ push(r1);
62 } else {
63 ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
64 }
65
Steve Block6ded16b2010-05-10 14:33:55 +010066 // JumpToExternalReference expects r0 to contain the number of arguments
Leon Clarkee46be812010-01-19 14:06:41 +000067 // including the receiver and the extra arguments.
68 __ add(r0, r0, Operand(num_extra_args + 1));
Steve Block6ded16b2010-05-10 14:33:55 +010069 __ JumpToExternalReference(ExternalReference(id));
Steve Blocka7e24c12009-10-30 11:49:00 +000070}
71
72
73// Load the built-in Array function from the current context.
74static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
75 // Load the global context.
76
77 __ ldr(result, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
78 __ ldr(result,
79 FieldMemOperand(result, GlobalObject::kGlobalContextOffset));
80 // Load the Array function from the global context.
81 __ ldr(result,
82 MemOperand(result,
83 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
84}
85
86
87// This constant has the same value as JSArray::kPreallocatedArrayElements and
88// if JSArray::kPreallocatedArrayElements is changed handling of loop unfolding
89// below should be reconsidered.
90static const int kLoopUnfoldLimit = 4;
91
92
93// Allocate an empty JSArray. The allocated array is put into the result
94// register. An elements backing store is allocated with size initial_capacity
95// and filled with the hole values.
96static void AllocateEmptyJSArray(MacroAssembler* masm,
97 Register array_function,
98 Register result,
99 Register scratch1,
100 Register scratch2,
101 Register scratch3,
102 int initial_capacity,
103 Label* gc_required) {
104 ASSERT(initial_capacity > 0);
105 // Load the initial map from the array function.
106 __ ldr(scratch1, FieldMemOperand(array_function,
107 JSFunction::kPrototypeOrInitialMapOffset));
108
109 // Allocate the JSArray object together with space for a fixed array with the
110 // requested elements.
111 int size = JSArray::kSize + FixedArray::SizeFor(initial_capacity);
Kristian Monsen25f61362010-05-21 11:50:48 +0100112 __ AllocateInNewSpace(size,
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 result,
114 scratch2,
115 scratch3,
116 gc_required,
117 TAG_OBJECT);
118
119 // Allocated the JSArray. Now initialize the fields except for the elements
120 // array.
121 // result: JSObject
122 // scratch1: initial map
123 // scratch2: start of next object
124 __ str(scratch1, FieldMemOperand(result, JSObject::kMapOffset));
125 __ LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
126 __ str(scratch1, FieldMemOperand(result, JSArray::kPropertiesOffset));
127 // Field JSArray::kElementsOffset is initialized later.
128 __ mov(scratch3, Operand(0));
129 __ str(scratch3, FieldMemOperand(result, JSArray::kLengthOffset));
130
131 // Calculate the location of the elements array and set elements array member
132 // of the JSArray.
133 // result: JSObject
134 // scratch2: start of next object
Leon Clarkef7060e22010-06-03 12:02:55 +0100135 __ add(scratch1, result, Operand(JSArray::kSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 __ str(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
137
138 // Clear the heap tag on the elements array.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100139 ASSERT(kSmiTag == 0);
140 __ sub(scratch1, scratch1, Operand(kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +0000141
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100142 // Initialize the FixedArray and fill it with holes. FixedArray length is
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 // stored as a smi.
144 // result: JSObject
145 // scratch1: elements array (untagged)
146 // scratch2: start of next object
147 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex);
148 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
149 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100150 __ mov(scratch3, Operand(Smi::FromInt(initial_capacity)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
152 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
153
154 // Fill the FixedArray with the hole value.
155 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
156 ASSERT(initial_capacity <= kLoopUnfoldLimit);
157 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex);
158 for (int i = 0; i < initial_capacity; i++) {
159 __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
160 }
161}
162
163// Allocate a JSArray with the number of elements stored in a register. The
164// register array_function holds the built-in Array function and the register
165// array_size holds the size of the array as a smi. The allocated array is put
166// into the result register and beginning and end of the FixedArray elements
167// storage is put into registers elements_array_storage and elements_array_end
168// (see below for when that is not the case). If the parameter fill_with_holes
169// is true the allocated elements backing store is filled with the hole values
170// otherwise it is left uninitialized. When the backing store is filled the
171// register elements_array_storage is scratched.
172static void AllocateJSArray(MacroAssembler* masm,
173 Register array_function, // Array function.
174 Register array_size, // As a smi.
175 Register result,
176 Register elements_array_storage,
177 Register elements_array_end,
178 Register scratch1,
179 Register scratch2,
180 bool fill_with_hole,
181 Label* gc_required) {
182 Label not_empty, allocated;
183
184 // Load the initial map from the array function.
185 __ ldr(elements_array_storage,
186 FieldMemOperand(array_function,
187 JSFunction::kPrototypeOrInitialMapOffset));
188
189 // Check whether an empty sized array is requested.
190 __ tst(array_size, array_size);
191 __ b(nz, &not_empty);
192
193 // If an empty array is requested allocate a small elements array anyway. This
194 // keeps the code below free of special casing for the empty array.
195 int size = JSArray::kSize +
196 FixedArray::SizeFor(JSArray::kPreallocatedArrayElements);
Kristian Monsen25f61362010-05-21 11:50:48 +0100197 __ AllocateInNewSpace(size,
Steve Blocka7e24c12009-10-30 11:49:00 +0000198 result,
199 elements_array_end,
200 scratch1,
201 gc_required,
202 TAG_OBJECT);
203 __ jmp(&allocated);
204
205 // Allocate the JSArray object together with space for a FixedArray with the
206 // requested number of elements.
207 __ bind(&not_empty);
208 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
209 __ mov(elements_array_end,
210 Operand((JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize));
211 __ add(elements_array_end,
212 elements_array_end,
213 Operand(array_size, ASR, kSmiTagSize));
Kristian Monsen25f61362010-05-21 11:50:48 +0100214 __ AllocateInNewSpace(
215 elements_array_end,
216 result,
217 scratch1,
218 scratch2,
219 gc_required,
220 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
Steve Blocka7e24c12009-10-30 11:49:00 +0000221
222 // Allocated the JSArray. Now initialize the fields except for the elements
223 // array.
224 // result: JSObject
225 // elements_array_storage: initial map
226 // array_size: size of array (smi)
227 __ bind(&allocated);
228 __ str(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset));
229 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex);
230 __ str(elements_array_storage,
231 FieldMemOperand(result, JSArray::kPropertiesOffset));
232 // Field JSArray::kElementsOffset is initialized later.
233 __ str(array_size, FieldMemOperand(result, JSArray::kLengthOffset));
234
235 // Calculate the location of the elements array and set elements array member
236 // of the JSArray.
237 // result: JSObject
238 // array_size: size of array (smi)
239 __ add(elements_array_storage, result, Operand(JSArray::kSize));
240 __ str(elements_array_storage,
241 FieldMemOperand(result, JSArray::kElementsOffset));
242
243 // Clear the heap tag on the elements array.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100244 ASSERT(kSmiTag == 0);
245 __ sub(elements_array_storage,
246 elements_array_storage,
247 Operand(kHeapObjectTag));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100248 // Initialize the fixed array and fill it with holes. FixedArray length is
Steve Blocka7e24c12009-10-30 11:49:00 +0000249 // stored as a smi.
250 // result: JSObject
251 // elements_array_storage: elements array (untagged)
252 // array_size: size of array (smi)
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex);
254 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
255 __ str(scratch1, MemOperand(elements_array_storage, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100256 ASSERT(kSmiTag == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000257 __ tst(array_size, array_size);
258 // Length of the FixedArray is the number of pre-allocated elements if
259 // the actual JSArray has length 0 and the size of the JSArray for non-empty
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100260 // JSArrays. The length of a FixedArray is stored as a smi.
261 __ mov(array_size,
262 Operand(Smi::FromInt(JSArray::kPreallocatedArrayElements)),
263 LeaveCC,
264 eq);
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
266 __ str(array_size,
267 MemOperand(elements_array_storage, kPointerSize, PostIndex));
268
269 // Calculate elements array and elements array end.
270 // result: JSObject
271 // elements_array_storage: elements array element storage
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100272 // array_size: smi-tagged size of elements array
273 ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 __ add(elements_array_end,
275 elements_array_storage,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100276 Operand(array_size, LSL, kPointerSizeLog2 - kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000277
278 // Fill the allocated FixedArray with the hole value if requested.
279 // result: JSObject
280 // elements_array_storage: elements array element storage
281 // elements_array_end: start of next object
282 if (fill_with_hole) {
283 Label loop, entry;
284 __ LoadRoot(scratch1, Heap::kTheHoleValueRootIndex);
285 __ jmp(&entry);
286 __ bind(&loop);
287 __ str(scratch1,
288 MemOperand(elements_array_storage, kPointerSize, PostIndex));
289 __ bind(&entry);
290 __ cmp(elements_array_storage, elements_array_end);
291 __ b(lt, &loop);
292 }
293}
294
295// Create a new array for the built-in Array function. This function allocates
296// the JSArray object and the FixedArray elements array and initializes these.
297// If the Array cannot be constructed in native code the runtime is called. This
298// function assumes the following state:
299// r0: argc
300// r1: constructor (built-in Array function)
301// lr: return address
302// sp[0]: last argument
303// This function is used for both construct and normal calls of Array. The only
304// difference between handling a construct call and a normal call is that for a
305// construct call the constructor function in r1 needs to be preserved for
306// entering the generic code. In both cases argc in r0 needs to be preserved.
307// Both registers are preserved by this code so no need to differentiate between
308// construct call and normal call.
309static void ArrayNativeCode(MacroAssembler* masm,
Steve Blockd0582a62009-12-15 09:54:21 +0000310 Label* call_generic_code) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000311 Label argc_one_or_more, argc_two_or_more;
312
313 // Check for array construction with zero arguments or one.
314 __ cmp(r0, Operand(0));
315 __ b(ne, &argc_one_or_more);
316
317 // Handle construction of an empty array.
318 AllocateEmptyJSArray(masm,
319 r1,
320 r2,
321 r3,
322 r4,
323 r5,
324 JSArray::kPreallocatedArrayElements,
325 call_generic_code);
326 __ IncrementCounter(&Counters::array_function_native, 1, r3, r4);
327 // Setup return value, remove receiver from stack and return.
328 __ mov(r0, r2);
329 __ add(sp, sp, Operand(kPointerSize));
330 __ Jump(lr);
331
332 // Check for one argument. Bail out if argument is not smi or if it is
333 // negative.
334 __ bind(&argc_one_or_more);
335 __ cmp(r0, Operand(1));
336 __ b(ne, &argc_two_or_more);
337 ASSERT(kSmiTag == 0);
338 __ ldr(r2, MemOperand(sp)); // Get the argument from the stack.
339 __ and_(r3, r2, Operand(kIntptrSignBit | kSmiTagMask), SetCC);
340 __ b(ne, call_generic_code);
341
342 // Handle construction of an empty array of a certain size. Bail out if size
343 // is too large to actually allocate an elements array.
344 ASSERT(kSmiTag == 0);
345 __ cmp(r2, Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize));
346 __ b(ge, call_generic_code);
347
348 // r0: argc
349 // r1: constructor
350 // r2: array_size (smi)
351 // sp[0]: argument
352 AllocateJSArray(masm,
353 r1,
354 r2,
355 r3,
356 r4,
357 r5,
358 r6,
359 r7,
360 true,
361 call_generic_code);
362 __ IncrementCounter(&Counters::array_function_native, 1, r2, r4);
363 // Setup return value, remove receiver and argument from stack and return.
364 __ mov(r0, r3);
365 __ add(sp, sp, Operand(2 * kPointerSize));
366 __ Jump(lr);
367
368 // Handle construction of an array from a list of arguments.
369 __ bind(&argc_two_or_more);
370 __ mov(r2, Operand(r0, LSL, kSmiTagSize)); // Convet argc to a smi.
371
372 // r0: argc
373 // r1: constructor
374 // r2: array_size (smi)
375 // sp[0]: last argument
376 AllocateJSArray(masm,
377 r1,
378 r2,
379 r3,
380 r4,
381 r5,
382 r6,
383 r7,
384 false,
385 call_generic_code);
386 __ IncrementCounter(&Counters::array_function_native, 1, r2, r6);
387
388 // Fill arguments as array elements. Copy from the top of the stack (last
389 // element) to the array backing store filling it backwards. Note:
390 // elements_array_end points after the backing store therefore PreIndex is
391 // used when filling the backing store.
392 // r0: argc
393 // r3: JSArray
394 // r4: elements_array storage start (untagged)
395 // r5: elements_array_end (untagged)
396 // sp[0]: last argument
397 Label loop, entry;
398 __ jmp(&entry);
399 __ bind(&loop);
400 __ ldr(r2, MemOperand(sp, kPointerSize, PostIndex));
401 __ str(r2, MemOperand(r5, -kPointerSize, PreIndex));
402 __ bind(&entry);
403 __ cmp(r4, r5);
404 __ b(lt, &loop);
405
406 // Remove caller arguments and receiver from the stack, setup return value and
407 // return.
408 // r0: argc
409 // r3: JSArray
410 // sp[0]: receiver
411 __ add(sp, sp, Operand(kPointerSize));
412 __ mov(r0, r3);
413 __ Jump(lr);
414}
415
416
417void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
418 // ----------- S t a t e -------------
419 // -- r0 : number of arguments
420 // -- lr : return address
421 // -- sp[...]: constructor arguments
422 // -----------------------------------
423 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
424
425 // Get the Array function.
426 GenerateLoadArrayFunction(masm, r1);
427
428 if (FLAG_debug_code) {
429 // Initial map for the builtin Array function shoud be a map.
430 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
431 __ tst(r2, Operand(kSmiTagMask));
432 __ Assert(ne, "Unexpected initial map for Array function");
433 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
434 __ Assert(eq, "Unexpected initial map for Array function");
435 }
436
437 // Run the native code for the Array function called as a normal function.
438 ArrayNativeCode(masm, &generic_array_code);
439
440 // Jump to the generic array code if the specialized code cannot handle
441 // the construction.
442 __ bind(&generic_array_code);
443 Code* code = Builtins::builtin(Builtins::ArrayCodeGeneric);
444 Handle<Code> array_code(code);
445 __ Jump(array_code, RelocInfo::CODE_TARGET);
446}
447
448
449void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
450 // ----------- S t a t e -------------
451 // -- r0 : number of arguments
452 // -- r1 : constructor function
453 // -- lr : return address
454 // -- sp[...]: constructor arguments
455 // -----------------------------------
456 Label generic_constructor;
457
458 if (FLAG_debug_code) {
459 // The array construct code is only set for the builtin Array function which
460 // always have a map.
461 GenerateLoadArrayFunction(masm, r2);
462 __ cmp(r1, r2);
463 __ Assert(eq, "Unexpected Array function");
464 // Initial map for the builtin Array function should be a map.
465 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
466 __ tst(r2, Operand(kSmiTagMask));
467 __ Assert(ne, "Unexpected initial map for Array function");
468 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
469 __ Assert(eq, "Unexpected initial map for Array function");
470 }
471
472 // Run the native code for the Array function called as a constructor.
473 ArrayNativeCode(masm, &generic_constructor);
474
475 // Jump to the generic construct code in case the specialized code cannot
476 // handle the construction.
477 __ bind(&generic_constructor);
478 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
479 Handle<Code> generic_construct_stub(code);
480 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
481}
482
483
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100484void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
485 // TODO(849): implement custom construct stub.
486 // Generate a copy of the generic stub for now.
487 Generate_JSConstructStubGeneric(masm);
488}
489
490
Steve Blocka7e24c12009-10-30 11:49:00 +0000491void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
492 // ----------- S t a t e -------------
493 // -- r0 : number of arguments
494 // -- r1 : constructor function
495 // -- lr : return address
496 // -- sp[...]: constructor arguments
497 // -----------------------------------
498
499 Label non_function_call;
500 // Check that the function is not a smi.
501 __ tst(r1, Operand(kSmiTagMask));
502 __ b(eq, &non_function_call);
503 // Check that the function is a JSFunction.
504 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
505 __ b(ne, &non_function_call);
506
507 // Jump to the function-specific construct stub.
508 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
509 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kConstructStubOffset));
510 __ add(pc, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
511
512 // r0: number of arguments
513 // r1: called object
514 __ bind(&non_function_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 // Set expected number of arguments to zero (not changing r0).
516 __ mov(r2, Operand(0));
517 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
518 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
519 RelocInfo::CODE_TARGET);
520}
521
522
Leon Clarkee46be812010-01-19 14:06:41 +0000523static void Generate_JSConstructStubHelper(MacroAssembler* masm,
524 bool is_api_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 // Enter a construct frame.
526 __ EnterConstructFrame();
527
528 // Preserve the two incoming parameters on the stack.
529 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
530 __ push(r0); // Smi-tagged arguments count.
531 __ push(r1); // Constructor function.
532
533 // Use r7 for holding undefined which is used in several places below.
534 __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
535
536 // Try to allocate the object without transitioning into C code. If any of the
537 // preconditions is not met, the code bails out to the runtime call.
538 Label rt_call, allocated;
539 if (FLAG_inline_new) {
540 Label undo_allocation;
541#ifdef ENABLE_DEBUGGER_SUPPORT
542 ExternalReference debug_step_in_fp =
543 ExternalReference::debug_step_in_fp_address();
544 __ mov(r2, Operand(debug_step_in_fp));
545 __ ldr(r2, MemOperand(r2));
546 __ tst(r2, r2);
547 __ b(nz, &rt_call);
548#endif
549
550 // Load the initial map and verify that it is in fact a map.
551 // r1: constructor function
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100552 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000553 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
554 __ tst(r2, Operand(kSmiTagMask));
555 __ b(eq, &rt_call);
556 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
557 __ b(ne, &rt_call);
558
559 // Check that the constructor is not constructing a JSFunction (see comments
560 // in Runtime_NewObject in runtime.cc). In which case the initial map's
561 // instance type would be JS_FUNCTION_TYPE.
562 // r1: constructor function
563 // r2: initial map
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100564 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000565 __ CompareInstanceType(r2, r3, JS_FUNCTION_TYPE);
566 __ b(eq, &rt_call);
567
568 // Now allocate the JSObject on the heap.
569 // r1: constructor function
570 // r2: initial map
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100571 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000572 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceSizeOffset));
Kristian Monsen25f61362010-05-21 11:50:48 +0100573 __ AllocateInNewSpace(r3, r4, r5, r6, &rt_call, SIZE_IN_WORDS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000574
575 // Allocated the JSObject, now initialize the fields. Map is set to initial
576 // map and properties and elements are set to empty fixed array.
577 // r1: constructor function
578 // r2: initial map
579 // r3: object size
580 // r4: JSObject (not tagged)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100581 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000582 __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
583 __ mov(r5, r4);
584 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
585 __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
586 ASSERT_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
587 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
588 ASSERT_EQ(2 * kPointerSize, JSObject::kElementsOffset);
589 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
590
591 // Fill all the in-object properties with undefined.
592 // r1: constructor function
593 // r2: initial map
594 // r3: object size (in words)
595 // r4: JSObject (not tagged)
596 // r5: First in-object property of JSObject (not tagged)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100597 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000598 __ add(r6, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
599 ASSERT_EQ(3 * kPointerSize, JSObject::kHeaderSize);
600 { Label loop, entry;
601 __ b(&entry);
602 __ bind(&loop);
603 __ str(r7, MemOperand(r5, kPointerSize, PostIndex));
604 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100605 __ cmp(r5, r6);
Steve Blocka7e24c12009-10-30 11:49:00 +0000606 __ b(lt, &loop);
607 }
608
609 // Add the object tag to make the JSObject real, so that we can continue and
610 // jump into the continuation code at any time from now on. Any failures
611 // need to undo the allocation, so that the heap is in a consistent state
612 // and verifiable.
613 __ add(r4, r4, Operand(kHeapObjectTag));
614
615 // Check if a non-empty properties array is needed. Continue with allocated
616 // object if not fall through to runtime call if it is.
617 // r1: constructor function
618 // r4: JSObject
619 // r5: start of next object (not tagged)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100620 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000621 __ ldrb(r3, FieldMemOperand(r2, Map::kUnusedPropertyFieldsOffset));
622 // The field instance sizes contains both pre-allocated property fields and
623 // in-object properties.
624 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100625 __ Ubfx(r6, r0, Map::kPreAllocatedPropertyFieldsByte * 8, 8);
626 __ add(r3, r3, Operand(r6));
627 __ Ubfx(r6, r0, Map::kInObjectPropertiesByte * 8, 8);
628 __ sub(r3, r3, Operand(r6), SetCC);
Steve Blocka7e24c12009-10-30 11:49:00 +0000629
630 // Done if no extra properties are to be allocated.
631 __ b(eq, &allocated);
632 __ Assert(pl, "Property allocation count failed.");
633
634 // Scale the number of elements by pointer size and add the header for
635 // FixedArrays to the start of the next object calculation from above.
636 // r1: constructor
637 // r3: number of elements in properties array
638 // r4: JSObject
639 // r5: start of next object
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100640 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000641 __ add(r0, r3, Operand(FixedArray::kHeaderSize / kPointerSize));
Kristian Monsen25f61362010-05-21 11:50:48 +0100642 __ AllocateInNewSpace(
643 r0,
644 r5,
645 r6,
646 r2,
647 &undo_allocation,
648 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
Steve Blocka7e24c12009-10-30 11:49:00 +0000649
650 // Initialize the FixedArray.
651 // r1: constructor
652 // r3: number of elements in properties array
653 // r4: JSObject
654 // r5: FixedArray (not tagged)
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100655 // r7: undefined value
Steve Blocka7e24c12009-10-30 11:49:00 +0000656 __ LoadRoot(r6, Heap::kFixedArrayMapRootIndex);
657 __ mov(r2, r5);
658 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
659 __ str(r6, MemOperand(r2, kPointerSize, PostIndex));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100660 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
661 __ mov(r0, Operand(r3, LSL, kSmiTagSize));
662 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +0000663
664 // Initialize the fields to undefined.
665 // r1: constructor function
666 // r2: First element of FixedArray (not tagged)
667 // r3: number of elements in properties array
668 // r4: JSObject
669 // r5: FixedArray (not tagged)
670 // r7: undefined
671 __ add(r6, r2, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
672 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
673 { Label loop, entry;
674 __ b(&entry);
675 __ bind(&loop);
676 __ str(r7, MemOperand(r2, kPointerSize, PostIndex));
677 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100678 __ cmp(r2, r6);
Steve Blocka7e24c12009-10-30 11:49:00 +0000679 __ b(lt, &loop);
680 }
681
682 // Store the initialized FixedArray into the properties field of
683 // the JSObject
684 // r1: constructor function
685 // r4: JSObject
686 // r5: FixedArray (not tagged)
687 __ add(r5, r5, Operand(kHeapObjectTag)); // Add the heap tag.
688 __ str(r5, FieldMemOperand(r4, JSObject::kPropertiesOffset));
689
690 // Continue with JSObject being successfully allocated
691 // r1: constructor function
692 // r4: JSObject
693 __ jmp(&allocated);
694
695 // Undo the setting of the new top so that the heap is verifiable. For
696 // example, the map's unused properties potentially do not match the
697 // allocated objects unused properties.
698 // r4: JSObject (previous new top)
699 __ bind(&undo_allocation);
700 __ UndoAllocationInNewSpace(r4, r5);
701 }
702
703 // Allocate the new receiver object using the runtime call.
704 // r1: constructor function
705 __ bind(&rt_call);
706 __ push(r1); // argument for Runtime_NewObject
707 __ CallRuntime(Runtime::kNewObject, 1);
708 __ mov(r4, r0);
709
710 // Receiver for constructor call allocated.
711 // r4: JSObject
712 __ bind(&allocated);
713 __ push(r4);
714
715 // Push the function and the allocated receiver from the stack.
716 // sp[0]: receiver (newly allocated object)
717 // sp[1]: constructor function
718 // sp[2]: number of arguments (smi-tagged)
719 __ ldr(r1, MemOperand(sp, kPointerSize));
720 __ push(r1); // Constructor function.
721 __ push(r4); // Receiver.
722
723 // Reload the number of arguments from the stack.
724 // r1: constructor function
725 // sp[0]: receiver
726 // sp[1]: constructor function
727 // sp[2]: receiver
728 // sp[3]: constructor function
729 // sp[4]: number of arguments (smi-tagged)
730 __ ldr(r3, MemOperand(sp, 4 * kPointerSize));
731
732 // Setup pointer to last argument.
733 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
734
735 // Setup number of arguments for function call below
736 __ mov(r0, Operand(r3, LSR, kSmiTagSize));
737
738 // Copy arguments and receiver to the expression stack.
739 // r0: number of arguments
740 // r2: address of last argument (caller sp)
741 // r1: constructor function
742 // r3: number of arguments (smi-tagged)
743 // sp[0]: receiver
744 // sp[1]: constructor function
745 // sp[2]: receiver
746 // sp[3]: constructor function
747 // sp[4]: number of arguments (smi-tagged)
748 Label loop, entry;
749 __ b(&entry);
750 __ bind(&loop);
751 __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
752 __ push(ip);
753 __ bind(&entry);
754 __ sub(r3, r3, Operand(2), SetCC);
755 __ b(ge, &loop);
756
757 // Call the function.
758 // r0: number of arguments
759 // r1: constructor function
Leon Clarkee46be812010-01-19 14:06:41 +0000760 if (is_api_function) {
761 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
762 Handle<Code> code = Handle<Code>(
763 Builtins::builtin(Builtins::HandleApiCallConstruct));
764 ParameterCount expected(0);
765 __ InvokeCode(code, expected, expected,
766 RelocInfo::CODE_TARGET, CALL_FUNCTION);
767 } else {
768 ParameterCount actual(r0);
769 __ InvokeFunction(r1, actual, CALL_FUNCTION);
770 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000771
772 // Pop the function from the stack.
773 // sp[0]: constructor function
774 // sp[2]: receiver
775 // sp[3]: constructor function
776 // sp[4]: number of arguments (smi-tagged)
777 __ pop();
778
779 // Restore context from the frame.
780 // r0: result
781 // sp[0]: receiver
782 // sp[1]: constructor function
783 // sp[2]: number of arguments (smi-tagged)
784 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
785
786 // If the result is an object (in the ECMA sense), we should get rid
787 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
788 // on page 74.
789 Label use_receiver, exit;
790
791 // If the result is a smi, it is *not* an object in the ECMA sense.
792 // r0: result
793 // sp[0]: receiver (newly allocated object)
794 // sp[1]: constructor function
795 // sp[2]: number of arguments (smi-tagged)
796 __ tst(r0, Operand(kSmiTagMask));
797 __ b(eq, &use_receiver);
798
799 // If the type of the result (stored in its map) is less than
800 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
801 __ CompareObjectType(r0, r3, r3, FIRST_JS_OBJECT_TYPE);
802 __ b(ge, &exit);
803
804 // Throw away the result of the constructor invocation and use the
805 // on-stack receiver as the result.
806 __ bind(&use_receiver);
807 __ ldr(r0, MemOperand(sp));
808
809 // Remove receiver from the stack, remove caller arguments, and
810 // return.
811 __ bind(&exit);
812 // r0: result
813 // sp[0]: receiver (newly allocated object)
814 // sp[1]: constructor function
815 // sp[2]: number of arguments (smi-tagged)
816 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
817 __ LeaveConstructFrame();
818 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
819 __ add(sp, sp, Operand(kPointerSize));
820 __ IncrementCounter(&Counters::constructed_objects, 1, r1, r2);
821 __ Jump(lr);
822}
823
824
Leon Clarkee46be812010-01-19 14:06:41 +0000825void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
826 Generate_JSConstructStubHelper(masm, false);
827}
828
829
830void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
831 Generate_JSConstructStubHelper(masm, true);
832}
833
834
Steve Blocka7e24c12009-10-30 11:49:00 +0000835static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
836 bool is_construct) {
837 // Called from Generate_JS_Entry
838 // r0: code entry
839 // r1: function
840 // r2: receiver
841 // r3: argc
842 // r4: argv
843 // r5-r7, cp may be clobbered
844
845 // Clear the context before we push it when entering the JS frame.
846 __ mov(cp, Operand(0));
847
848 // Enter an internal frame.
849 __ EnterInternalFrame();
850
851 // Set up the context from the function argument.
852 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
853
854 // Set up the roots register.
855 ExternalReference roots_address = ExternalReference::roots_address();
856 __ mov(r10, Operand(roots_address));
857
858 // Push the function and the receiver onto the stack.
859 __ push(r1);
860 __ push(r2);
861
862 // Copy arguments to the stack in a loop.
863 // r1: function
864 // r3: argc
865 // r4: argv, i.e. points to first arg
866 Label loop, entry;
867 __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
868 // r2 points past last arg.
869 __ b(&entry);
870 __ bind(&loop);
871 __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
872 __ ldr(r0, MemOperand(r0)); // dereference handle
873 __ push(r0); // push parameter
874 __ bind(&entry);
Steve Block6ded16b2010-05-10 14:33:55 +0100875 __ cmp(r4, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000876 __ b(ne, &loop);
877
878 // Initialize all JavaScript callee-saved registers, since they will be seen
879 // by the garbage collector as part of handlers.
880 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
881 __ mov(r5, Operand(r4));
882 __ mov(r6, Operand(r4));
883 __ mov(r7, Operand(r4));
884 if (kR9Available == 1) {
885 __ mov(r9, Operand(r4));
886 }
887
888 // Invoke the code and pass argc as r0.
889 __ mov(r0, Operand(r3));
890 if (is_construct) {
891 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
892 RelocInfo::CODE_TARGET);
893 } else {
894 ParameterCount actual(r0);
895 __ InvokeFunction(r1, actual, CALL_FUNCTION);
896 }
897
898 // Exit the JS frame and remove the parameters (except function), and return.
899 // Respect ABI stack constraint.
900 __ LeaveInternalFrame();
901 __ Jump(lr);
902
903 // r0: result
904}
905
906
907void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
908 Generate_JSEntryTrampolineHelper(masm, false);
909}
910
911
912void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
913 Generate_JSEntryTrampolineHelper(masm, true);
914}
915
916
Iain Merrick75681382010-08-19 15:07:18 +0100917void Builtins::Generate_LazyCompile(MacroAssembler* masm) {
918 // Enter an internal frame.
919 __ EnterInternalFrame();
920
921 // Preserve the function.
922 __ push(r1);
923
924 // Push the function on the stack as the argument to the runtime function.
925 __ push(r1);
926 __ CallRuntime(Runtime::kLazyCompile, 1);
927 // Calculate the entry point.
928 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
929 // Restore saved function.
930 __ pop(r1);
931
932 // Tear down temporary frame.
933 __ LeaveInternalFrame();
934
935 // Do a tail-call of the compiled function.
936 __ Jump(r2);
937}
938
939
Steve Blocka7e24c12009-10-30 11:49:00 +0000940void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
941 // 1. Make sure we have at least one argument.
Andrei Popescu402d9372010-02-26 13:31:12 +0000942 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 { Label done;
944 __ tst(r0, Operand(r0));
945 __ b(ne, &done);
946 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
947 __ push(r2);
948 __ add(r0, r0, Operand(1));
949 __ bind(&done);
950 }
951
Andrei Popescu402d9372010-02-26 13:31:12 +0000952 // 2. Get the function to call (passed as receiver) from the stack, check
953 // if it is a function.
954 // r0: actual number of arguments
955 Label non_function;
956 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
957 __ tst(r1, Operand(kSmiTagMask));
958 __ b(eq, &non_function);
959 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
960 __ b(ne, &non_function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000961
Andrei Popescu402d9372010-02-26 13:31:12 +0000962 // 3a. Patch the first argument if necessary when calling a function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000963 // r0: actual number of arguments
964 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +0000965 Label shift_arguments;
966 { Label convert_to_object, use_global_receiver, patch_receiver;
967 // Change context eagerly in case we need the global receiver.
968 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
969
Steve Blocka7e24c12009-10-30 11:49:00 +0000970 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
971 __ ldr(r2, MemOperand(r2, -kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 // r0: actual number of arguments
973 // r1: function
974 // r2: first argument
975 __ tst(r2, Operand(kSmiTagMask));
Andrei Popescu402d9372010-02-26 13:31:12 +0000976 __ b(eq, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000977
978 __ LoadRoot(r3, Heap::kNullValueRootIndex);
979 __ cmp(r2, r3);
980 __ b(eq, &use_global_receiver);
981 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
982 __ cmp(r2, r3);
983 __ b(eq, &use_global_receiver);
984
985 __ CompareObjectType(r2, r3, r3, FIRST_JS_OBJECT_TYPE);
Andrei Popescu402d9372010-02-26 13:31:12 +0000986 __ b(lt, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000987 __ cmp(r3, Operand(LAST_JS_OBJECT_TYPE));
Andrei Popescu402d9372010-02-26 13:31:12 +0000988 __ b(le, &shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000989
Andrei Popescu402d9372010-02-26 13:31:12 +0000990 __ bind(&convert_to_object);
991 __ EnterInternalFrame(); // In order to preserve argument count.
992 __ mov(r0, Operand(r0, LSL, kSmiTagSize)); // Smi-tagged.
Steve Blocka7e24c12009-10-30 11:49:00 +0000993 __ push(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000994
995 __ push(r2);
996 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
997 __ mov(r2, r0);
998
Steve Blocka7e24c12009-10-30 11:49:00 +0000999 __ pop(r0);
1000 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 __ LeaveInternalFrame();
Andrei Popescu402d9372010-02-26 13:31:12 +00001002 // Restore the function to r1.
1003 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1004 __ jmp(&patch_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001005
Andrei Popescu402d9372010-02-26 13:31:12 +00001006 // Use the global receiver object from the called function as the
1007 // receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +00001008 __ bind(&use_global_receiver);
1009 const int kGlobalIndex =
1010 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1011 __ ldr(r2, FieldMemOperand(cp, kGlobalIndex));
Steve Blockd0582a62009-12-15 09:54:21 +00001012 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
1013 __ ldr(r2, FieldMemOperand(r2, kGlobalIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +00001014 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalReceiverOffset));
1015
1016 __ bind(&patch_receiver);
1017 __ add(r3, sp, Operand(r0, LSL, kPointerSizeLog2));
1018 __ str(r2, MemOperand(r3, -kPointerSize));
1019
Andrei Popescu402d9372010-02-26 13:31:12 +00001020 __ jmp(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001021 }
1022
Andrei Popescu402d9372010-02-26 13:31:12 +00001023 // 3b. Patch the first argument when calling a non-function. The
1024 // CALL_NON_FUNCTION builtin expects the non-function callee as
1025 // receiver, so overwrite the first argument which will ultimately
1026 // become the receiver.
1027 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001029 __ bind(&non_function);
1030 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1031 __ str(r1, MemOperand(r2, -kPointerSize));
1032 // Clear r1 to indicate a non-function being called.
1033 __ mov(r1, Operand(0));
1034
1035 // 4. Shift arguments and return address one slot down on the stack
1036 // (overwriting the original receiver). Adjust argument count to make
1037 // the original first argument the new receiver.
1038 // r0: actual number of arguments
1039 // r1: function
1040 __ bind(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001041 { Label loop;
1042 // Calculate the copy start address (destination). Copy end address is sp.
1043 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001044
1045 __ bind(&loop);
1046 __ ldr(ip, MemOperand(r2, -kPointerSize));
1047 __ str(ip, MemOperand(r2));
1048 __ sub(r2, r2, Operand(kPointerSize));
1049 __ cmp(r2, sp);
1050 __ b(ne, &loop);
Andrei Popescu402d9372010-02-26 13:31:12 +00001051 // Adjust the actual number of arguments and remove the top element
1052 // (which is a copy of the last argument).
1053 __ sub(r0, r0, Operand(1));
1054 __ pop();
Steve Blocka7e24c12009-10-30 11:49:00 +00001055 }
1056
Andrei Popescu402d9372010-02-26 13:31:12 +00001057 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin.
Steve Blocka7e24c12009-10-30 11:49:00 +00001058 // r0: actual number of arguments
1059 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001060 { Label function;
Steve Blocka7e24c12009-10-30 11:49:00 +00001061 __ tst(r1, r1);
Andrei Popescu402d9372010-02-26 13:31:12 +00001062 __ b(ne, &function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001063 __ mov(r2, Operand(0)); // expected arguments is 0 for CALL_NON_FUNCTION
1064 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
1065 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
1066 RelocInfo::CODE_TARGET);
Andrei Popescu402d9372010-02-26 13:31:12 +00001067 __ bind(&function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001068 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001069
1070 // 5b. Get the code to call from the function and check that the number of
1071 // expected arguments matches what we're providing. If so, jump
1072 // (tail-call) to the code in register edx without checking arguments.
1073 // r0: actual number of arguments
1074 // r1: function
1075 __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1076 __ ldr(r2,
1077 FieldMemOperand(r3, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001078 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
Steve Block791712a2010-08-27 10:21:07 +01001079 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001080 __ cmp(r2, r0); // Check formal and actual parameter counts.
1081 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
1082 RelocInfo::CODE_TARGET, ne);
1083
1084 ParameterCount expected(0);
1085 __ InvokeCode(r3, expected, expected, JUMP_FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +00001086}
1087
1088
1089void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1090 const int kIndexOffset = -5 * kPointerSize;
1091 const int kLimitOffset = -4 * kPointerSize;
1092 const int kArgsOffset = 2 * kPointerSize;
1093 const int kRecvOffset = 3 * kPointerSize;
1094 const int kFunctionOffset = 4 * kPointerSize;
1095
1096 __ EnterInternalFrame();
1097
1098 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1099 __ push(r0);
1100 __ ldr(r0, MemOperand(fp, kArgsOffset)); // get the args array
1101 __ push(r0);
1102 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_JS);
1103
Steve Blockd0582a62009-12-15 09:54:21 +00001104 // Check the stack for overflow. We are not trying need to catch
1105 // interruptions (e.g. debug break and preemption) here, so the "real stack
1106 // limit" is checked.
Steve Blocka7e24c12009-10-30 11:49:00 +00001107 Label okay;
Steve Blockd0582a62009-12-15 09:54:21 +00001108 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
1109 // Make r2 the space we have left. The stack might already be overflowed
1110 // here which will cause r2 to become negative.
Steve Blocka7e24c12009-10-30 11:49:00 +00001111 __ sub(r2, sp, r2);
Steve Blockd0582a62009-12-15 09:54:21 +00001112 // Check if the arguments will overflow the stack.
Steve Blocka7e24c12009-10-30 11:49:00 +00001113 __ cmp(r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
Steve Blockd0582a62009-12-15 09:54:21 +00001114 __ b(gt, &okay); // Signed comparison.
Steve Blocka7e24c12009-10-30 11:49:00 +00001115
1116 // Out of stack space.
1117 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1118 __ push(r1);
1119 __ push(r0);
1120 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_JS);
Steve Blockd0582a62009-12-15 09:54:21 +00001121 // End of stack check.
Steve Blocka7e24c12009-10-30 11:49:00 +00001122
1123 // Push current limit and index.
1124 __ bind(&okay);
1125 __ push(r0); // limit
1126 __ mov(r1, Operand(0)); // initial index
1127 __ push(r1);
1128
1129 // Change context eagerly to get the right global object if necessary.
1130 __ ldr(r0, MemOperand(fp, kFunctionOffset));
1131 __ ldr(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
1132
1133 // Compute the receiver.
1134 Label call_to_object, use_global_receiver, push_receiver;
1135 __ ldr(r0, MemOperand(fp, kRecvOffset));
1136 __ tst(r0, Operand(kSmiTagMask));
1137 __ b(eq, &call_to_object);
1138 __ LoadRoot(r1, Heap::kNullValueRootIndex);
1139 __ cmp(r0, r1);
1140 __ b(eq, &use_global_receiver);
1141 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1142 __ cmp(r0, r1);
1143 __ b(eq, &use_global_receiver);
1144
1145 // Check if the receiver is already a JavaScript object.
1146 // r0: receiver
1147 __ CompareObjectType(r0, r1, r1, FIRST_JS_OBJECT_TYPE);
1148 __ b(lt, &call_to_object);
1149 __ cmp(r1, Operand(LAST_JS_OBJECT_TYPE));
1150 __ b(le, &push_receiver);
1151
1152 // Convert the receiver to a regular object.
1153 // r0: receiver
1154 __ bind(&call_to_object);
1155 __ push(r0);
1156 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
1157 __ b(&push_receiver);
1158
1159 // Use the current global receiver object as the receiver.
1160 __ bind(&use_global_receiver);
1161 const int kGlobalOffset =
1162 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1163 __ ldr(r0, FieldMemOperand(cp, kGlobalOffset));
Steve Blockd0582a62009-12-15 09:54:21 +00001164 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalContextOffset));
1165 __ ldr(r0, FieldMemOperand(r0, kGlobalOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001166 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalReceiverOffset));
1167
1168 // Push the receiver.
1169 // r0: receiver
1170 __ bind(&push_receiver);
1171 __ push(r0);
1172
1173 // Copy all arguments from the array to the stack.
1174 Label entry, loop;
1175 __ ldr(r0, MemOperand(fp, kIndexOffset));
1176 __ b(&entry);
1177
1178 // Load the current argument from the arguments array and push it to the
1179 // stack.
1180 // r0: current argument index
1181 __ bind(&loop);
1182 __ ldr(r1, MemOperand(fp, kArgsOffset));
1183 __ push(r1);
1184 __ push(r0);
1185
1186 // Call the runtime to access the property in the arguments array.
1187 __ CallRuntime(Runtime::kGetProperty, 2);
1188 __ push(r0);
1189
1190 // Use inline caching to access the arguments.
1191 __ ldr(r0, MemOperand(fp, kIndexOffset));
1192 __ add(r0, r0, Operand(1 << kSmiTagSize));
1193 __ str(r0, MemOperand(fp, kIndexOffset));
1194
1195 // Test if the copy loop has finished copying all the elements from the
1196 // arguments object.
1197 __ bind(&entry);
1198 __ ldr(r1, MemOperand(fp, kLimitOffset));
1199 __ cmp(r0, r1);
1200 __ b(ne, &loop);
1201
1202 // Invoke the function.
1203 ParameterCount actual(r0);
1204 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1205 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1206 __ InvokeFunction(r1, actual, CALL_FUNCTION);
1207
1208 // Tear down the internal frame and remove function, receiver and args.
1209 __ LeaveInternalFrame();
1210 __ add(sp, sp, Operand(3 * kPointerSize));
1211 __ Jump(lr);
1212}
1213
1214
1215static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1216 __ mov(r0, Operand(r0, LSL, kSmiTagSize));
1217 __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1218 __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() | fp.bit() | lr.bit());
1219 __ add(fp, sp, Operand(3 * kPointerSize));
1220}
1221
1222
1223static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1224 // ----------- S t a t e -------------
1225 // -- r0 : result being passed through
1226 // -----------------------------------
1227 // Get the number of arguments passed (as a smi), tear down the frame and
1228 // then tear down the parameters.
1229 __ ldr(r1, MemOperand(fp, -3 * kPointerSize));
1230 __ mov(sp, fp);
1231 __ ldm(ia_w, sp, fp.bit() | lr.bit());
1232 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1233 __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1234}
1235
1236
1237void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1238 // ----------- S t a t e -------------
1239 // -- r0 : actual number of arguments
1240 // -- r1 : function (passed through to callee)
1241 // -- r2 : expected number of arguments
1242 // -- r3 : code entry to call
1243 // -----------------------------------
1244
1245 Label invoke, dont_adapt_arguments;
1246
1247 Label enough, too_few;
Steve Block6ded16b2010-05-10 14:33:55 +01001248 __ cmp(r0, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001249 __ b(lt, &too_few);
1250 __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1251 __ b(eq, &dont_adapt_arguments);
1252
1253 { // Enough parameters: actual >= expected
1254 __ bind(&enough);
1255 EnterArgumentsAdaptorFrame(masm);
1256
1257 // Calculate copy start address into r0 and copy end address into r2.
1258 // r0: actual number of arguments as a smi
1259 // r1: function
1260 // r2: expected number of arguments
1261 // r3: code entry to call
1262 __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1263 // adjust for return address and receiver
1264 __ add(r0, r0, Operand(2 * kPointerSize));
1265 __ sub(r2, r0, Operand(r2, LSL, kPointerSizeLog2));
1266
1267 // Copy the arguments (including the receiver) to the new stack frame.
1268 // r0: copy start address
1269 // r1: function
1270 // r2: copy end address
1271 // r3: code entry to call
1272
1273 Label copy;
1274 __ bind(&copy);
1275 __ ldr(ip, MemOperand(r0, 0));
1276 __ push(ip);
1277 __ cmp(r0, r2); // Compare before moving to next argument.
1278 __ sub(r0, r0, Operand(kPointerSize));
1279 __ b(ne, &copy);
1280
1281 __ b(&invoke);
1282 }
1283
1284 { // Too few parameters: Actual < expected
1285 __ bind(&too_few);
1286 EnterArgumentsAdaptorFrame(masm);
1287
1288 // Calculate copy start address into r0 and copy end address is fp.
1289 // r0: actual number of arguments as a smi
1290 // r1: function
1291 // r2: expected number of arguments
1292 // r3: code entry to call
1293 __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1294
1295 // Copy the arguments (including the receiver) to the new stack frame.
1296 // r0: copy start address
1297 // r1: function
1298 // r2: expected number of arguments
1299 // r3: code entry to call
1300 Label copy;
1301 __ bind(&copy);
1302 // Adjust load for return address and receiver.
1303 __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1304 __ push(ip);
1305 __ cmp(r0, fp); // Compare before moving to next argument.
1306 __ sub(r0, r0, Operand(kPointerSize));
1307 __ b(ne, &copy);
1308
1309 // Fill the remaining expected arguments with undefined.
1310 // r1: function
1311 // r2: expected number of arguments
1312 // r3: code entry to call
1313 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1314 __ sub(r2, fp, Operand(r2, LSL, kPointerSizeLog2));
1315 __ sub(r2, r2, Operand(4 * kPointerSize)); // Adjust for frame.
1316
1317 Label fill;
1318 __ bind(&fill);
1319 __ push(ip);
1320 __ cmp(sp, r2);
1321 __ b(ne, &fill);
1322 }
1323
1324 // Call the entry point.
1325 __ bind(&invoke);
1326 __ Call(r3);
1327
1328 // Exit frame and return.
1329 LeaveArgumentsAdaptorFrame(masm);
1330 __ Jump(lr);
1331
1332
1333 // -------------------------------------------
1334 // Dont adapt arguments.
1335 // -------------------------------------------
1336 __ bind(&dont_adapt_arguments);
1337 __ Jump(r3);
1338}
1339
1340
1341#undef __
1342
1343} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001344
1345#endif // V8_TARGET_ARCH_ARM