v8  3.11.10(node0.8.26)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
builtins-arm.cc
Go to the documentation of this file.
1 // Copyright 2012 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 
30 #if defined(V8_TARGET_ARCH_ARM)
31 
32 #include "codegen.h"
33 #include "debug.h"
34 #include "deoptimizer.h"
35 #include "full-codegen.h"
36 #include "runtime.h"
37 
38 namespace v8 {
39 namespace internal {
40 
41 
42 #define __ ACCESS_MASM(masm)
43 
44 
45 void 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  // -----------------------------------
58 
59  // 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 
68  // JumpToExternalReference expects r0 to contain the number of arguments
69  // including the receiver and the extra arguments.
70  __ add(r0, r0, Operand(num_extra_args + 1));
71  __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
72 }
73 
74 
75 // Load the built-in InternalArray function from the current context.
76 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
77  Register result) {
78  // Load the global context.
79 
81  __ ldr(result,
83  // Load the InternalArray function from the global context.
84  __ ldr(result,
85  MemOperand(result,
88 }
89 
90 
91 // Load the built-in Array function from the current context.
92 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
93  // Load the global context.
94 
96  __ ldr(result,
98  // Load the Array function from the global context.
99  __ ldr(result,
100  MemOperand(result,
102 }
103 
104 
105 // Allocate an empty JSArray. The allocated array is put into the result
106 // register. An elements backing store is allocated with size initial_capacity
107 // and filled with the hole values.
108 static void AllocateEmptyJSArray(MacroAssembler* masm,
109  Register array_function,
110  Register result,
111  Register scratch1,
112  Register scratch2,
113  Register scratch3,
114  Label* gc_required) {
115  const int initial_capacity = JSArray::kPreallocatedArrayElements;
116  STATIC_ASSERT(initial_capacity >= 0);
117  __ LoadInitialArrayMap(array_function, scratch2, scratch1, false);
118 
119  // Allocate the JSArray object together with space for a fixed array with the
120  // requested elements.
121  int size = JSArray::kSize;
122  if (initial_capacity > 0) {
123  size += FixedArray::SizeFor(initial_capacity);
124  }
125  __ AllocateInNewSpace(size,
126  result,
127  scratch2,
128  scratch3,
129  gc_required,
130  TAG_OBJECT);
131 
132  // Allocated the JSArray. Now initialize the fields except for the elements
133  // array.
134  // result: JSObject
135  // scratch1: initial map
136  // scratch2: start of next object
137  __ str(scratch1, FieldMemOperand(result, JSObject::kMapOffset));
138  __ LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
139  __ str(scratch1, FieldMemOperand(result, JSArray::kPropertiesOffset));
140  // Field JSArray::kElementsOffset is initialized later.
141  __ mov(scratch3, Operand(0, RelocInfo::NONE));
142  __ str(scratch3, FieldMemOperand(result, JSArray::kLengthOffset));
143 
144  if (initial_capacity == 0) {
145  __ str(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
146  return;
147  }
148 
149  // Calculate the location of the elements array and set elements array member
150  // of the JSArray.
151  // result: JSObject
152  // scratch2: start of next object
153  __ add(scratch1, result, Operand(JSArray::kSize));
154  __ str(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
155 
156  // Clear the heap tag on the elements array.
157  __ sub(scratch1, scratch1, Operand(kHeapObjectTag));
158 
159  // Initialize the FixedArray and fill it with holes. FixedArray length is
160  // stored as a smi.
161  // result: JSObject
162  // scratch1: elements array (untagged)
163  // scratch2: start of next object
164  __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex);
166  __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
167  __ mov(scratch3, Operand(Smi::FromInt(initial_capacity)));
169  __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
170 
171  // Fill the FixedArray with the hole value. Inline the code if short.
173  __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex);
174  static const int kLoopUnfoldLimit = 4;
175  if (initial_capacity <= kLoopUnfoldLimit) {
176  for (int i = 0; i < initial_capacity; i++) {
177  __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
178  }
179  } else {
180  Label loop, entry;
181  __ add(scratch2, scratch1, Operand(initial_capacity * kPointerSize));
182  __ b(&entry);
183  __ bind(&loop);
184  __ str(scratch3, MemOperand(scratch1, kPointerSize, PostIndex));
185  __ bind(&entry);
186  __ cmp(scratch1, scratch2);
187  __ b(lt, &loop);
188  }
189 }
190 
191 // Allocate a JSArray with the number of elements stored in a register. The
192 // register array_function holds the built-in Array function and the register
193 // array_size holds the size of the array as a smi. The allocated array is put
194 // into the result register and beginning and end of the FixedArray elements
195 // storage is put into registers elements_array_storage and elements_array_end
196 // (see below for when that is not the case). If the parameter fill_with_holes
197 // is true the allocated elements backing store is filled with the hole values
198 // otherwise it is left uninitialized. When the backing store is filled the
199 // register elements_array_storage is scratched.
200 static void AllocateJSArray(MacroAssembler* masm,
201  Register array_function, // Array function.
202  Register array_size, // As a smi, cannot be 0.
203  Register result,
204  Register elements_array_storage,
205  Register elements_array_end,
206  Register scratch1,
207  Register scratch2,
208  bool fill_with_hole,
209  Label* gc_required) {
210  // Load the initial map from the array function.
211  __ LoadInitialArrayMap(array_function, scratch2,
212  elements_array_storage, fill_with_hole);
213 
214  if (FLAG_debug_code) { // Assert that array size is not zero.
215  __ tst(array_size, array_size);
216  __ Assert(ne, "array size is unexpectedly 0");
217  }
218 
219  // Allocate the JSArray object together with space for a FixedArray with the
220  // requested number of elements.
221  STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
222  __ mov(elements_array_end,
224  __ add(elements_array_end,
225  elements_array_end,
226  Operand(array_size, ASR, kSmiTagSize));
227  __ AllocateInNewSpace(
228  elements_array_end,
229  result,
230  scratch1,
231  scratch2,
232  gc_required,
233  static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
234 
235  // Allocated the JSArray. Now initialize the fields except for the elements
236  // array.
237  // result: JSObject
238  // elements_array_storage: initial map
239  // array_size: size of array (smi)
240  __ str(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset));
241  __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex);
242  __ str(elements_array_storage,
244  // Field JSArray::kElementsOffset is initialized later.
245  __ str(array_size, FieldMemOperand(result, JSArray::kLengthOffset));
246 
247  // Calculate the location of the elements array and set elements array member
248  // of the JSArray.
249  // result: JSObject
250  // array_size: size of array (smi)
251  __ add(elements_array_storage, result, Operand(JSArray::kSize));
252  __ str(elements_array_storage,
254 
255  // Clear the heap tag on the elements array.
256  STATIC_ASSERT(kSmiTag == 0);
257  __ sub(elements_array_storage,
258  elements_array_storage,
259  Operand(kHeapObjectTag));
260  // Initialize the fixed array and fill it with holes. FixedArray length is
261  // stored as a smi.
262  // result: JSObject
263  // elements_array_storage: elements array (untagged)
264  // array_size: size of array (smi)
265  __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex);
267  __ str(scratch1, MemOperand(elements_array_storage, kPointerSize, PostIndex));
268  STATIC_ASSERT(kSmiTag == 0);
270  __ str(array_size,
271  MemOperand(elements_array_storage, kPointerSize, PostIndex));
272 
273  // Calculate elements array and elements array end.
274  // result: JSObject
275  // elements_array_storage: elements array element storage
276  // array_size: smi-tagged size of elements array
278  __ add(elements_array_end,
279  elements_array_storage,
280  Operand(array_size, LSL, kPointerSizeLog2 - kSmiTagSize));
281 
282  // Fill the allocated FixedArray with the hole value if requested.
283  // result: JSObject
284  // elements_array_storage: elements array element storage
285  // elements_array_end: start of next object
286  if (fill_with_hole) {
287  Label loop, entry;
288  __ LoadRoot(scratch1, Heap::kTheHoleValueRootIndex);
289  __ jmp(&entry);
290  __ bind(&loop);
291  __ str(scratch1,
292  MemOperand(elements_array_storage, kPointerSize, PostIndex));
293  __ bind(&entry);
294  __ cmp(elements_array_storage, elements_array_end);
295  __ b(lt, &loop);
296  }
297 }
298 
299 // Create a new array for the built-in Array function. This function allocates
300 // the JSArray object and the FixedArray elements array and initializes these.
301 // If the Array cannot be constructed in native code the runtime is called. This
302 // function assumes the following state:
303 // r0: argc
304 // r1: constructor (built-in Array function)
305 // lr: return address
306 // sp[0]: last argument
307 // This function is used for both construct and normal calls of Array. The only
308 // difference between handling a construct call and a normal call is that for a
309 // construct call the constructor function in r1 needs to be preserved for
310 // entering the generic code. In both cases argc in r0 needs to be preserved.
311 // Both registers are preserved by this code so no need to differentiate between
312 // construct call and normal call.
313 static void ArrayNativeCode(MacroAssembler* masm,
314  Label* call_generic_code) {
315  Counters* counters = masm->isolate()->counters();
316  Label argc_one_or_more, argc_two_or_more, not_empty_array, empty_array,
317  has_non_smi_element, finish, cant_transition_map, not_double;
318 
319  // Check for array construction with zero arguments or one.
320  __ cmp(r0, Operand(0, RelocInfo::NONE));
321  __ b(ne, &argc_one_or_more);
322 
323  // Handle construction of an empty array.
324  __ bind(&empty_array);
325  AllocateEmptyJSArray(masm,
326  r1,
327  r2,
328  r3,
329  r4,
330  r5,
331  call_generic_code);
332  __ IncrementCounter(counters->array_function_native(), 1, r3, r4);
333  // Set up return value, remove receiver from stack and return.
334  __ mov(r0, r2);
335  __ add(sp, sp, Operand(kPointerSize));
336  __ Jump(lr);
337 
338  // Check for one argument. Bail out if argument is not smi or if it is
339  // negative.
340  __ bind(&argc_one_or_more);
341  __ cmp(r0, Operand(1));
342  __ b(ne, &argc_two_or_more);
343  STATIC_ASSERT(kSmiTag == 0);
344  __ ldr(r2, MemOperand(sp)); // Get the argument from the stack.
345  __ tst(r2, r2);
346  __ b(ne, &not_empty_array);
347  __ Drop(1); // Adjust stack.
348  __ mov(r0, Operand(0)); // Treat this as a call with argc of zero.
349  __ b(&empty_array);
350 
351  __ bind(&not_empty_array);
352  __ and_(r3, r2, Operand(kIntptrSignBit | kSmiTagMask), SetCC);
353  __ b(ne, call_generic_code);
354 
355  // Handle construction of an empty array of a certain size. Bail out if size
356  // is too large to actually allocate an elements array.
357  STATIC_ASSERT(kSmiTag == 0);
359  __ b(ge, call_generic_code);
360 
361  // r0: argc
362  // r1: constructor
363  // r2: array_size (smi)
364  // sp[0]: argument
365  AllocateJSArray(masm,
366  r1,
367  r2,
368  r3,
369  r4,
370  r5,
371  r6,
372  r7,
373  true,
374  call_generic_code);
375  __ IncrementCounter(counters->array_function_native(), 1, r2, r4);
376  // Set up return value, remove receiver and argument from stack and return.
377  __ mov(r0, r3);
378  __ add(sp, sp, Operand(2 * kPointerSize));
379  __ Jump(lr);
380 
381  // Handle construction of an array from a list of arguments.
382  __ bind(&argc_two_or_more);
383  __ mov(r2, Operand(r0, LSL, kSmiTagSize)); // Convet argc to a smi.
384 
385  // r0: argc
386  // r1: constructor
387  // r2: array_size (smi)
388  // sp[0]: last argument
389  AllocateJSArray(masm,
390  r1,
391  r2,
392  r3,
393  r4,
394  r5,
395  r6,
396  r7,
397  false,
398  call_generic_code);
399  __ IncrementCounter(counters->array_function_native(), 1, r2, r6);
400 
401  // Fill arguments as array elements. Copy from the top of the stack (last
402  // element) to the array backing store filling it backwards. Note:
403  // elements_array_end points after the backing store therefore PreIndex is
404  // used when filling the backing store.
405  // r0: argc
406  // r3: JSArray
407  // r4: elements_array storage start (untagged)
408  // r5: elements_array_end (untagged)
409  // sp[0]: last argument
410  Label loop, entry;
411  __ mov(r7, sp);
412  __ jmp(&entry);
413  __ bind(&loop);
415  if (FLAG_smi_only_arrays) {
416  __ JumpIfNotSmi(r2, &has_non_smi_element);
417  }
419  __ bind(&entry);
420  __ cmp(r4, r5);
421  __ b(lt, &loop);
422 
423  __ bind(&finish);
424  __ mov(sp, r7);
425 
426  // Remove caller arguments and receiver from the stack, setup return value and
427  // return.
428  // r0: argc
429  // r3: JSArray
430  // sp[0]: receiver
431  __ add(sp, sp, Operand(kPointerSize));
432  __ mov(r0, r3);
433  __ Jump(lr);
434 
435  __ bind(&has_non_smi_element);
436  // Double values are handled by the runtime.
437  __ CheckMap(
438  r2, r9, Heap::kHeapNumberMapRootIndex, &not_double, DONT_DO_SMI_CHECK);
439  __ bind(&cant_transition_map);
440  __ UndoAllocationInNewSpace(r3, r4);
441  __ b(call_generic_code);
442 
443  __ bind(&not_double);
444  // Transition FAST_SMI_ELEMENTS to FAST_ELEMENTS.
445  // r3: JSArray
447  __ LoadTransitionedArrayMapConditional(FAST_SMI_ELEMENTS,
449  r2,
450  r9,
451  &cant_transition_map);
453  __ RecordWriteField(r3,
455  r2,
456  r9,
461  Label loop2;
462  __ sub(r7, r7, Operand(kPointerSize));
463  __ bind(&loop2);
466  __ cmp(r4, r5);
467  __ b(lt, &loop2);
468  __ b(&finish);
469 }
470 
471 
472 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
473  // ----------- S t a t e -------------
474  // -- r0 : number of arguments
475  // -- lr : return address
476  // -- sp[...]: constructor arguments
477  // -----------------------------------
478  Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
479 
480  // Get the InternalArray function.
481  GenerateLoadInternalArrayFunction(masm, r1);
482 
483  if (FLAG_debug_code) {
484  // Initial map for the builtin InternalArray functions should be maps.
486  __ tst(r2, Operand(kSmiTagMask));
487  __ Assert(ne, "Unexpected initial map for InternalArray function");
488  __ CompareObjectType(r2, r3, r4, MAP_TYPE);
489  __ Assert(eq, "Unexpected initial map for InternalArray function");
490  }
491 
492  // Run the native code for the InternalArray function called as a normal
493  // function.
494  ArrayNativeCode(masm, &generic_array_code);
495 
496  // Jump to the generic array code if the specialized code cannot handle the
497  // construction.
498  __ bind(&generic_array_code);
499 
500  Handle<Code> array_code =
501  masm->isolate()->builtins()->InternalArrayCodeGeneric();
502  __ Jump(array_code, RelocInfo::CODE_TARGET);
503 }
504 
505 
506 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
507  // ----------- S t a t e -------------
508  // -- r0 : number of arguments
509  // -- lr : return address
510  // -- sp[...]: constructor arguments
511  // -----------------------------------
512  Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
513 
514  // Get the Array function.
515  GenerateLoadArrayFunction(masm, r1);
516 
517  if (FLAG_debug_code) {
518  // Initial map for the builtin Array functions should be maps.
520  __ tst(r2, Operand(kSmiTagMask));
521  __ Assert(ne, "Unexpected initial map for Array function");
522  __ CompareObjectType(r2, r3, r4, MAP_TYPE);
523  __ Assert(eq, "Unexpected initial map for Array function");
524  }
525 
526  // Run the native code for the Array function called as a normal function.
527  ArrayNativeCode(masm, &generic_array_code);
528 
529  // Jump to the generic array code if the specialized code cannot handle
530  // the construction.
531  __ bind(&generic_array_code);
532 
533  Handle<Code> array_code =
534  masm->isolate()->builtins()->ArrayCodeGeneric();
535  __ Jump(array_code, RelocInfo::CODE_TARGET);
536 }
537 
538 
539 void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
540  // ----------- S t a t e -------------
541  // -- r0 : number of arguments
542  // -- r1 : constructor function
543  // -- lr : return address
544  // -- sp[...]: constructor arguments
545  // -----------------------------------
546  Label generic_constructor;
547 
548  if (FLAG_debug_code) {
549  // The array construct code is only set for the builtin and internal
550  // Array functions which always have a map.
551  // Initial map for the builtin Array function should be a map.
553  __ tst(r2, Operand(kSmiTagMask));
554  __ Assert(ne, "Unexpected initial map for Array function");
555  __ CompareObjectType(r2, r3, r4, MAP_TYPE);
556  __ Assert(eq, "Unexpected initial map for Array function");
557  }
558 
559  // Run the native code for the Array function called as a constructor.
560  ArrayNativeCode(masm, &generic_constructor);
561 
562  // Jump to the generic construct code in case the specialized code cannot
563  // handle the construction.
564  __ bind(&generic_constructor);
565  Handle<Code> generic_construct_stub =
566  masm->isolate()->builtins()->JSConstructStubGeneric();
567  __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
568 }
569 
570 
571 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
572  // ----------- S t a t e -------------
573  // -- r0 : number of arguments
574  // -- r1 : constructor function
575  // -- lr : return address
576  // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
577  // -- sp[argc * 4] : receiver
578  // -----------------------------------
579  Counters* counters = masm->isolate()->counters();
580  __ IncrementCounter(counters->string_ctor_calls(), 1, r2, r3);
581 
582  Register function = r1;
583  if (FLAG_debug_code) {
584  __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, r2);
585  __ cmp(function, Operand(r2));
586  __ Assert(eq, "Unexpected String function");
587  }
588 
589  // Load the first arguments in r0 and get rid of the rest.
590  Label no_arguments;
591  __ cmp(r0, Operand(0, RelocInfo::NONE));
592  __ b(eq, &no_arguments);
593  // First args = sp[(argc - 1) * 4].
594  __ sub(r0, r0, Operand(1));
596  // sp now point to args[0], drop args[0] + receiver.
597  __ Drop(2);
598 
599  Register argument = r2;
600  Label not_cached, argument_is_string;
602  masm,
603  r0, // Input.
604  argument, // Result.
605  r3, // Scratch.
606  r4, // Scratch.
607  r5, // Scratch.
608  false, // Is it a Smi?
609  &not_cached);
610  __ IncrementCounter(counters->string_ctor_cached_number(), 1, r3, r4);
611  __ bind(&argument_is_string);
612 
613  // ----------- S t a t e -------------
614  // -- r2 : argument converted to string
615  // -- r1 : constructor function
616  // -- lr : return address
617  // -----------------------------------
618 
619  Label gc_required;
620  __ AllocateInNewSpace(JSValue::kSize,
621  r0, // Result.
622  r3, // Scratch.
623  r4, // Scratch.
624  &gc_required,
625  TAG_OBJECT);
626 
627  // Initialising the String Object.
628  Register map = r3;
629  __ LoadGlobalFunctionInitialMap(function, map, r4);
630  if (FLAG_debug_code) {
632  __ cmp(r4, Operand(JSValue::kSize >> kPointerSizeLog2));
633  __ Assert(eq, "Unexpected string wrapper instance size");
635  __ cmp(r4, Operand(0, RelocInfo::NONE));
636  __ Assert(eq, "Unexpected unused properties of string wrapper");
637  }
639 
640  __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
643 
644  __ str(argument, FieldMemOperand(r0, JSValue::kValueOffset));
645 
646  // Ensure the object is fully initialized.
648 
649  __ Ret();
650 
651  // The argument was not found in the number to string cache. Check
652  // if it's a string already before calling the conversion builtin.
653  Label convert_argument;
654  __ bind(&not_cached);
655  __ JumpIfSmi(r0, &convert_argument);
656 
657  // Is it a String?
661  __ tst(r3, Operand(kIsNotStringMask));
662  __ b(ne, &convert_argument);
663  __ mov(argument, r0);
664  __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
665  __ b(&argument_is_string);
666 
667  // Invoke the conversion builtin and put the result into r2.
668  __ bind(&convert_argument);
669  __ push(function); // Preserve the function.
670  __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
671  {
672  FrameScope scope(masm, StackFrame::INTERNAL);
673  __ push(r0);
674  __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
675  }
676  __ pop(function);
677  __ mov(argument, r0);
678  __ b(&argument_is_string);
679 
680  // Load the empty string into r2, remove the receiver from the
681  // stack, and jump back to the case where the argument is a string.
682  __ bind(&no_arguments);
683  __ LoadRoot(argument, Heap::kEmptyStringRootIndex);
684  __ Drop(1);
685  __ b(&argument_is_string);
686 
687  // At this point the argument is already a string. Call runtime to
688  // create a string wrapper.
689  __ bind(&gc_required);
690  __ IncrementCounter(counters->string_ctor_gc_required(), 1, r3, r4);
691  {
692  FrameScope scope(masm, StackFrame::INTERNAL);
693  __ push(argument);
694  __ CallRuntime(Runtime::kNewStringWrapper, 1);
695  }
696  __ Ret();
697 }
698 
699 
700 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
701  bool is_api_function,
702  bool count_constructions) {
703  // ----------- S t a t e -------------
704  // -- r0 : number of arguments
705  // -- r1 : constructor function
706  // -- lr : return address
707  // -- sp[...]: constructor arguments
708  // -----------------------------------
709 
710  // Should never count constructions for api objects.
711  ASSERT(!is_api_function || !count_constructions);
712 
713  Isolate* isolate = masm->isolate();
714 
715  // Enter a construct frame.
716  {
717  FrameScope scope(masm, StackFrame::CONSTRUCT);
718 
719  // Preserve the two incoming parameters on the stack.
720  __ mov(r0, Operand(r0, LSL, kSmiTagSize));
721  __ push(r0); // Smi-tagged arguments count.
722  __ push(r1); // Constructor function.
723 
724  // Try to allocate the object without transitioning into C code. If any of
725  // the preconditions is not met, the code bails out to the runtime call.
726  Label rt_call, allocated;
727  if (FLAG_inline_new) {
728  Label undo_allocation;
729 #ifdef ENABLE_DEBUGGER_SUPPORT
730  ExternalReference debug_step_in_fp =
731  ExternalReference::debug_step_in_fp_address(isolate);
732  __ mov(r2, Operand(debug_step_in_fp));
733  __ ldr(r2, MemOperand(r2));
734  __ tst(r2, r2);
735  __ b(ne, &rt_call);
736 #endif
737 
738  // Load the initial map and verify that it is in fact a map.
739  // r1: constructor function
741  __ JumpIfSmi(r2, &rt_call);
742  __ CompareObjectType(r2, r3, r4, MAP_TYPE);
743  __ b(ne, &rt_call);
744 
745  // Check that the constructor is not constructing a JSFunction (see
746  // comments in Runtime_NewObject in runtime.cc). In which case the
747  // initial map's instance type would be JS_FUNCTION_TYPE.
748  // r1: constructor function
749  // r2: initial map
750  __ CompareInstanceType(r2, r3, JS_FUNCTION_TYPE);
751  __ b(eq, &rt_call);
752 
753  if (count_constructions) {
754  Label allocate;
755  // Decrease generous allocation count.
757  MemOperand constructor_count =
759  __ ldrb(r4, constructor_count);
760  __ sub(r4, r4, Operand(1), SetCC);
761  __ strb(r4, constructor_count);
762  __ b(ne, &allocate);
763 
764  __ Push(r1, r2);
765 
766  __ push(r1); // constructor
767  // The call will replace the stub, so the countdown is only done once.
768  __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
769 
770  __ pop(r2);
771  __ pop(r1);
772 
773  __ bind(&allocate);
774  }
775 
776  // Now allocate the JSObject on the heap.
777  // r1: constructor function
778  // r2: initial map
780  __ AllocateInNewSpace(r3, r4, r5, r6, &rt_call, SIZE_IN_WORDS);
781 
782  // Allocated the JSObject, now initialize the fields. Map is set to
783  // initial map and properties and elements are set to empty fixed array.
784  // r1: constructor function
785  // r2: initial map
786  // r3: object size
787  // r4: JSObject (not tagged)
788  __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
789  __ mov(r5, r4);
796 
797  // Fill all the in-object properties with the appropriate filler.
798  // r1: constructor function
799  // r2: initial map
800  // r3: object size (in words)
801  // r4: JSObject (not tagged)
802  // r5: First in-object property of JSObject (not tagged)
803  __ add(r6, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
805  __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
806  if (count_constructions) {
809  kBitsPerByte);
810  __ add(r0, r5, Operand(r0, LSL, kPointerSizeLog2));
811  // r0: offset of first field after pre-allocated fields
812  if (FLAG_debug_code) {
813  __ cmp(r0, r6);
814  __ Assert(le, "Unexpected number of pre-allocated property fields.");
815  }
816  __ InitializeFieldsWithFiller(r5, r0, r7);
817  // To allow for truncation.
818  __ LoadRoot(r7, Heap::kOnePointerFillerMapRootIndex);
819  }
820  __ InitializeFieldsWithFiller(r5, r6, r7);
821 
822  // Add the object tag to make the JSObject real, so that we can continue
823  // and jump into the continuation code at any time from now on. Any
824  // failures need to undo the allocation, so that the heap is in a
825  // consistent state and verifiable.
826  __ add(r4, r4, Operand(kHeapObjectTag));
827 
828  // Check if a non-empty properties array is needed. Continue with
829  // allocated object if not fall through to runtime call if it is.
830  // r1: constructor function
831  // r4: JSObject
832  // r5: start of next object (not tagged)
834  // The field instance sizes contains both pre-allocated property fields
835  // and in-object properties.
838  kBitsPerByte);
839  __ add(r3, r3, Operand(r6));
841  kBitsPerByte);
842  __ sub(r3, r3, Operand(r6), SetCC);
843 
844  // Done if no extra properties are to be allocated.
845  __ b(eq, &allocated);
846  __ Assert(pl, "Property allocation count failed.");
847 
848  // Scale the number of elements by pointer size and add the header for
849  // FixedArrays to the start of the next object calculation from above.
850  // r1: constructor
851  // r3: number of elements in properties array
852  // r4: JSObject
853  // r5: start of next object
854  __ add(r0, r3, Operand(FixedArray::kHeaderSize / kPointerSize));
855  __ AllocateInNewSpace(
856  r0,
857  r5,
858  r6,
859  r2,
860  &undo_allocation,
861  static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
862 
863  // Initialize the FixedArray.
864  // r1: constructor
865  // r3: number of elements in properties array
866  // r4: JSObject
867  // r5: FixedArray (not tagged)
868  __ LoadRoot(r6, Heap::kFixedArrayMapRootIndex);
869  __ mov(r2, r5);
873  __ mov(r0, Operand(r3, LSL, kSmiTagSize));
875 
876  // Initialize the fields to undefined.
877  // r1: constructor function
878  // r2: First element of FixedArray (not tagged)
879  // r3: number of elements in properties array
880  // r4: JSObject
881  // r5: FixedArray (not tagged)
882  __ add(r6, r2, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
884  { Label loop, entry;
885  if (count_constructions) {
886  __ LoadRoot(r7, Heap::kUndefinedValueRootIndex);
887  } else if (FLAG_debug_code) {
888  __ LoadRoot(r8, Heap::kUndefinedValueRootIndex);
889  __ cmp(r7, r8);
890  __ Assert(eq, "Undefined value not loaded.");
891  }
892  __ b(&entry);
893  __ bind(&loop);
895  __ bind(&entry);
896  __ cmp(r2, r6);
897  __ b(lt, &loop);
898  }
899 
900  // Store the initialized FixedArray into the properties field of
901  // the JSObject
902  // r1: constructor function
903  // r4: JSObject
904  // r5: FixedArray (not tagged)
905  __ add(r5, r5, Operand(kHeapObjectTag)); // Add the heap tag.
907 
908  // Continue with JSObject being successfully allocated
909  // r1: constructor function
910  // r4: JSObject
911  __ jmp(&allocated);
912 
913  // Undo the setting of the new top so that the heap is verifiable. For
914  // example, the map's unused properties potentially do not match the
915  // allocated objects unused properties.
916  // r4: JSObject (previous new top)
917  __ bind(&undo_allocation);
918  __ UndoAllocationInNewSpace(r4, r5);
919  }
920 
921  // Allocate the new receiver object using the runtime call.
922  // r1: constructor function
923  __ bind(&rt_call);
924  __ push(r1); // argument for Runtime_NewObject
925  __ CallRuntime(Runtime::kNewObject, 1);
926  __ mov(r4, r0);
927 
928  // Receiver for constructor call allocated.
929  // r4: JSObject
930  __ bind(&allocated);
931  __ push(r4);
932  __ push(r4);
933 
934  // Reload the number of arguments and the constructor from the stack.
935  // sp[0]: receiver
936  // sp[1]: receiver
937  // sp[2]: constructor function
938  // sp[3]: number of arguments (smi-tagged)
939  __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
940  __ ldr(r3, MemOperand(sp, 3 * kPointerSize));
941 
942  // Set up pointer to last argument.
944 
945  // Set up number of arguments for function call below
946  __ mov(r0, Operand(r3, LSR, kSmiTagSize));
947 
948  // Copy arguments and receiver to the expression stack.
949  // r0: number of arguments
950  // r1: constructor function
951  // r2: address of last argument (caller sp)
952  // r3: number of arguments (smi-tagged)
953  // sp[0]: receiver
954  // sp[1]: receiver
955  // sp[2]: constructor function
956  // sp[3]: number of arguments (smi-tagged)
957  Label loop, entry;
958  __ b(&entry);
959  __ bind(&loop);
960  __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
961  __ push(ip);
962  __ bind(&entry);
963  __ sub(r3, r3, Operand(2), SetCC);
964  __ b(ge, &loop);
965 
966  // Call the function.
967  // r0: number of arguments
968  // r1: constructor function
969  if (is_api_function) {
971  Handle<Code> code =
972  masm->isolate()->builtins()->HandleApiCallConstruct();
973  ParameterCount expected(0);
974  __ InvokeCode(code, expected, expected,
975  RelocInfo::CODE_TARGET, CALL_FUNCTION, CALL_AS_METHOD);
976  } else {
977  ParameterCount actual(r0);
978  __ InvokeFunction(r1, actual, CALL_FUNCTION,
979  NullCallWrapper(), CALL_AS_METHOD);
980  }
981 
982  // Store offset of return address for deoptimizer.
983  if (!is_api_function && !count_constructions) {
984  masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
985  }
986 
987  // Restore context from the frame.
988  // r0: result
989  // sp[0]: receiver
990  // sp[1]: constructor function
991  // sp[2]: number of arguments (smi-tagged)
993 
994  // If the result is an object (in the ECMA sense), we should get rid
995  // of the receiver and use the result; see ECMA-262 section 13.2.2-7
996  // on page 74.
997  Label use_receiver, exit;
998 
999  // If the result is a smi, it is *not* an object in the ECMA sense.
1000  // r0: result
1001  // sp[0]: receiver (newly allocated object)
1002  // sp[1]: constructor function
1003  // sp[2]: number of arguments (smi-tagged)
1004  __ JumpIfSmi(r0, &use_receiver);
1005 
1006  // If the type of the result (stored in its map) is less than
1007  // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
1008  __ CompareObjectType(r0, r3, r3, FIRST_SPEC_OBJECT_TYPE);
1009  __ b(ge, &exit);
1010 
1011  // Throw away the result of the constructor invocation and use the
1012  // on-stack receiver as the result.
1013  __ bind(&use_receiver);
1014  __ ldr(r0, MemOperand(sp));
1015 
1016  // Remove receiver from the stack, remove caller arguments, and
1017  // return.
1018  __ bind(&exit);
1019  // r0: result
1020  // sp[0]: receiver (newly allocated object)
1021  // sp[1]: constructor function
1022  // sp[2]: number of arguments (smi-tagged)
1023  __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
1024 
1025  // Leave construct frame.
1026  }
1027 
1028  __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
1029  __ add(sp, sp, Operand(kPointerSize));
1030  __ IncrementCounter(isolate->counters()->constructed_objects(), 1, r1, r2);
1031  __ Jump(lr);
1032 }
1033 
1034 
1035 void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) {
1036  Generate_JSConstructStubHelper(masm, false, true);
1037 }
1038 
1039 
1040 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
1041  Generate_JSConstructStubHelper(masm, false, false);
1042 }
1043 
1044 
1045 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
1046  Generate_JSConstructStubHelper(masm, true, false);
1047 }
1048 
1049 
1050 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
1051  bool is_construct) {
1052  // Called from Generate_JS_Entry
1053  // r0: code entry
1054  // r1: function
1055  // r2: receiver
1056  // r3: argc
1057  // r4: argv
1058  // r5-r7, cp may be clobbered
1059 
1060  // Clear the context before we push it when entering the internal frame.
1061  __ mov(cp, Operand(0, RelocInfo::NONE));
1062 
1063  // Enter an internal frame.
1064  {
1065  FrameScope scope(masm, StackFrame::INTERNAL);
1066 
1067  // Set up the context from the function argument.
1069 
1070  __ InitializeRootRegister();
1071 
1072  // Push the function and the receiver onto the stack.
1073  __ push(r1);
1074  __ push(r2);
1075 
1076  // Copy arguments to the stack in a loop.
1077  // r1: function
1078  // r3: argc
1079  // r4: argv, i.e. points to first arg
1080  Label loop, entry;
1081  __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
1082  // r2 points past last arg.
1083  __ b(&entry);
1084  __ bind(&loop);
1085  __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
1086  __ ldr(r0, MemOperand(r0)); // dereference handle
1087  __ push(r0); // push parameter
1088  __ bind(&entry);
1089  __ cmp(r4, r2);
1090  __ b(ne, &loop);
1091 
1092  // Initialize all JavaScript callee-saved registers, since they will be seen
1093  // by the garbage collector as part of handlers.
1094  __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
1095  __ mov(r5, Operand(r4));
1096  __ mov(r6, Operand(r4));
1097  __ mov(r7, Operand(r4));
1098  if (kR9Available == 1) {
1099  __ mov(r9, Operand(r4));
1100  }
1101 
1102  // Invoke the code and pass argc as r0.
1103  __ mov(r0, Operand(r3));
1104  if (is_construct) {
1105  CallConstructStub stub(NO_CALL_FUNCTION_FLAGS);
1106  __ CallStub(&stub);
1107  } else {
1108  ParameterCount actual(r0);
1109  __ InvokeFunction(r1, actual, CALL_FUNCTION,
1110  NullCallWrapper(), CALL_AS_METHOD);
1111  }
1112  // Exit the JS frame and remove the parameters (except function), and
1113  // return.
1114  // Respect ABI stack constraint.
1115  }
1116  __ Jump(lr);
1117 
1118  // r0: result
1119 }
1120 
1121 
1122 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
1123  Generate_JSEntryTrampolineHelper(masm, false);
1124 }
1125 
1126 
1127 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
1128  Generate_JSEntryTrampolineHelper(masm, true);
1129 }
1130 
1131 
1132 void Builtins::Generate_LazyCompile(MacroAssembler* masm) {
1133  // Enter an internal frame.
1134  {
1135  FrameScope scope(masm, StackFrame::INTERNAL);
1136 
1137  // Preserve the function.
1138  __ push(r1);
1139  // Push call kind information.
1140  __ push(r5);
1141 
1142  // Push the function on the stack as the argument to the runtime function.
1143  __ push(r1);
1144  __ CallRuntime(Runtime::kLazyCompile, 1);
1145  // Calculate the entry point.
1146  __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1147 
1148  // Restore call kind information.
1149  __ pop(r5);
1150  // Restore saved function.
1151  __ pop(r1);
1152 
1153  // Tear down internal frame.
1154  }
1155 
1156  // Do a tail-call of the compiled function.
1157  __ Jump(r2);
1158 }
1159 
1160 
1161 void Builtins::Generate_LazyRecompile(MacroAssembler* masm) {
1162  // Enter an internal frame.
1163  {
1164  FrameScope scope(masm, StackFrame::INTERNAL);
1165 
1166  // Preserve the function.
1167  __ push(r1);
1168  // Push call kind information.
1169  __ push(r5);
1170 
1171  // Push the function on the stack as the argument to the runtime function.
1172  __ push(r1);
1173  __ CallRuntime(Runtime::kLazyRecompile, 1);
1174  // Calculate the entry point.
1175  __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1176 
1177  // Restore call kind information.
1178  __ pop(r5);
1179  // Restore saved function.
1180  __ pop(r1);
1181 
1182  // Tear down internal frame.
1183  }
1184 
1185  // Do a tail-call of the compiled function.
1186  __ Jump(r2);
1187 }
1188 
1189 
1190 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1192  {
1193  FrameScope scope(masm, StackFrame::INTERNAL);
1194  // Pass the function and deoptimization type to the runtime system.
1195  __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
1196  __ push(r0);
1197  __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1198  }
1199 
1200  // Get the full codegen state from the stack and untag it -> r6.
1201  __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
1202  __ SmiUntag(r6);
1203  // Switch on the state.
1204  Label with_tos_register, unknown_state;
1205  __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
1206  __ b(ne, &with_tos_register);
1207  __ add(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1208  __ Ret();
1209 
1210  __ bind(&with_tos_register);
1211  __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
1212  __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
1213  __ b(ne, &unknown_state);
1214  __ add(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1215  __ Ret();
1216 
1217  __ bind(&unknown_state);
1218  __ stop("no cases left");
1219 }
1220 
1221 
1222 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1223  Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1224 }
1225 
1226 
1227 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1228  Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1229 }
1230 
1231 
1232 void Builtins::Generate_NotifyOSR(MacroAssembler* masm) {
1233  // For now, we are relying on the fact that Runtime::NotifyOSR
1234  // doesn't do any garbage collection which allows us to save/restore
1235  // the registers without worrying about which of them contain
1236  // pointers. This seems a bit fragile.
1237  __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved | lr.bit() | fp.bit());
1238  {
1239  FrameScope scope(masm, StackFrame::INTERNAL);
1240  __ CallRuntime(Runtime::kNotifyOSR, 0);
1241  }
1242  __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved | lr.bit() | fp.bit());
1243  __ Ret();
1244 }
1245 
1246 
1247 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1248  CpuFeatures::TryForceFeatureScope scope(VFP3);
1250  __ Abort("Unreachable code: Cannot optimize without VFP3 support.");
1251  return;
1252  }
1253 
1254  // Lookup the function in the JavaScript frame and push it as an
1255  // argument to the on-stack replacement function.
1257  {
1258  FrameScope scope(masm, StackFrame::INTERNAL);
1259  __ push(r0);
1260  __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1261  }
1262 
1263  // If the result was -1 it means that we couldn't optimize the
1264  // function. Just return and continue in the unoptimized version.
1265  Label skip;
1266  __ cmp(r0, Operand(Smi::FromInt(-1)));
1267  __ b(ne, &skip);
1268  __ Ret();
1269 
1270  __ bind(&skip);
1271  // Untag the AST id and push it on the stack.
1272  __ SmiUntag(r0);
1273  __ push(r0);
1274 
1275  // Generate the code for doing the frame-to-frame translation using
1276  // the deoptimizer infrastructure.
1277  Deoptimizer::EntryGenerator generator(masm, Deoptimizer::OSR);
1278  generator.Generate();
1279 }
1280 
1281 
1282 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1283  // 1. Make sure we have at least one argument.
1284  // r0: actual number of arguments
1285  { Label done;
1286  __ cmp(r0, Operand(0));
1287  __ b(ne, &done);
1288  __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1289  __ push(r2);
1290  __ add(r0, r0, Operand(1));
1291  __ bind(&done);
1292  }
1293 
1294  // 2. Get the function to call (passed as receiver) from the stack, check
1295  // if it is a function.
1296  // r0: actual number of arguments
1297  Label slow, non_function;
1298  __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1299  __ JumpIfSmi(r1, &non_function);
1300  __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1301  __ b(ne, &slow);
1302 
1303  // 3a. Patch the first argument if necessary when calling a function.
1304  // r0: actual number of arguments
1305  // r1: function
1306  Label shift_arguments;
1307  __ mov(r4, Operand(0, RelocInfo::NONE)); // indicate regular JS_FUNCTION
1308  { Label convert_to_object, use_global_receiver, patch_receiver;
1309  // Change context eagerly in case we need the global receiver.
1311 
1312  // Do not transform the receiver for strict mode functions.
1315  __ tst(r3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1316  kSmiTagSize)));
1317  __ b(ne, &shift_arguments);
1318 
1319  // Do not transform the receiver for native (Compilerhints already in r3).
1320  __ tst(r3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1321  __ b(ne, &shift_arguments);
1322 
1323  // Compute the receiver in non-strict mode.
1324  __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1325  __ ldr(r2, MemOperand(r2, -kPointerSize));
1326  // r0: actual number of arguments
1327  // r1: function
1328  // r2: first argument
1329  __ JumpIfSmi(r2, &convert_to_object);
1330 
1331  __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1332  __ cmp(r2, r3);
1333  __ b(eq, &use_global_receiver);
1334  __ LoadRoot(r3, Heap::kNullValueRootIndex);
1335  __ cmp(r2, r3);
1336  __ b(eq, &use_global_receiver);
1337 
1339  __ CompareObjectType(r2, r3, r3, FIRST_SPEC_OBJECT_TYPE);
1340  __ b(ge, &shift_arguments);
1341 
1342  __ bind(&convert_to_object);
1343 
1344  {
1345  // Enter an internal frame in order to preserve argument count.
1346  FrameScope scope(masm, StackFrame::INTERNAL);
1347  __ mov(r0, Operand(r0, LSL, kSmiTagSize)); // Smi-tagged.
1348  __ push(r0);
1349 
1350  __ push(r2);
1351  __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1352  __ mov(r2, r0);
1353 
1354  __ pop(r0);
1355  __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1356 
1357  // Exit the internal frame.
1358  }
1359 
1360  // Restore the function to r1, and the flag to r4.
1361  __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1362  __ mov(r4, Operand(0, RelocInfo::NONE));
1363  __ jmp(&patch_receiver);
1364 
1365  // Use the global receiver object from the called function as the
1366  // receiver.
1367  __ bind(&use_global_receiver);
1368  const int kGlobalIndex =
1370  __ ldr(r2, FieldMemOperand(cp, kGlobalIndex));
1372  __ ldr(r2, FieldMemOperand(r2, kGlobalIndex));
1374 
1375  __ bind(&patch_receiver);
1376  __ add(r3, sp, Operand(r0, LSL, kPointerSizeLog2));
1377  __ str(r2, MemOperand(r3, -kPointerSize));
1378 
1379  __ jmp(&shift_arguments);
1380  }
1381 
1382  // 3b. Check for function proxy.
1383  __ bind(&slow);
1384  __ mov(r4, Operand(1, RelocInfo::NONE)); // indicate function proxy
1385  __ cmp(r2, Operand(JS_FUNCTION_PROXY_TYPE));
1386  __ b(eq, &shift_arguments);
1387  __ bind(&non_function);
1388  __ mov(r4, Operand(2, RelocInfo::NONE)); // indicate non-function
1389 
1390  // 3c. Patch the first argument when calling a non-function. The
1391  // CALL_NON_FUNCTION builtin expects the non-function callee as
1392  // receiver, so overwrite the first argument which will ultimately
1393  // become the receiver.
1394  // r0: actual number of arguments
1395  // r1: function
1396  // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1397  __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1398  __ str(r1, MemOperand(r2, -kPointerSize));
1399 
1400  // 4. Shift arguments and return address one slot down on the stack
1401  // (overwriting the original receiver). Adjust argument count to make
1402  // the original first argument the new receiver.
1403  // r0: actual number of arguments
1404  // r1: function
1405  // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1406  __ bind(&shift_arguments);
1407  { Label loop;
1408  // Calculate the copy start address (destination). Copy end address is sp.
1409  __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1410 
1411  __ bind(&loop);
1412  __ ldr(ip, MemOperand(r2, -kPointerSize));
1413  __ str(ip, MemOperand(r2));
1414  __ sub(r2, r2, Operand(kPointerSize));
1415  __ cmp(r2, sp);
1416  __ b(ne, &loop);
1417  // Adjust the actual number of arguments and remove the top element
1418  // (which is a copy of the last argument).
1419  __ sub(r0, r0, Operand(1));
1420  __ pop();
1421  }
1422 
1423  // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1424  // or a function proxy via CALL_FUNCTION_PROXY.
1425  // r0: actual number of arguments
1426  // r1: function
1427  // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1428  { Label function, non_proxy;
1429  __ tst(r4, r4);
1430  __ b(eq, &function);
1431  // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1432  __ mov(r2, Operand(0, RelocInfo::NONE));
1433  __ SetCallKind(r5, CALL_AS_METHOD);
1434  __ cmp(r4, Operand(1));
1435  __ b(ne, &non_proxy);
1436 
1437  __ push(r1); // re-add proxy object as additional argument
1438  __ add(r0, r0, Operand(1));
1439  __ GetBuiltinEntry(r3, Builtins::CALL_FUNCTION_PROXY);
1440  __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1441  RelocInfo::CODE_TARGET);
1442 
1443  __ bind(&non_proxy);
1444  __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
1445  __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1446  RelocInfo::CODE_TARGET);
1447  __ bind(&function);
1448  }
1449 
1450  // 5b. Get the code to call from the function and check that the number of
1451  // expected arguments matches what we're providing. If so, jump
1452  // (tail-call) to the code in register edx without checking arguments.
1453  // r0: actual number of arguments
1454  // r1: function
1456  __ ldr(r2,
1458  __ mov(r2, Operand(r2, ASR, kSmiTagSize));
1460  __ SetCallKind(r5, CALL_AS_METHOD);
1461  __ cmp(r2, r0); // Check formal and actual parameter counts.
1462  __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1463  RelocInfo::CODE_TARGET,
1464  ne);
1465 
1466  ParameterCount expected(0);
1467  __ InvokeCode(r3, expected, expected, JUMP_FUNCTION,
1468  NullCallWrapper(), CALL_AS_METHOD);
1469 }
1470 
1471 
1472 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1473  const int kIndexOffset = -5 * kPointerSize;
1474  const int kLimitOffset = -4 * kPointerSize;
1475  const int kArgsOffset = 2 * kPointerSize;
1476  const int kRecvOffset = 3 * kPointerSize;
1477  const int kFunctionOffset = 4 * kPointerSize;
1478 
1479  {
1480  FrameScope frame_scope(masm, StackFrame::INTERNAL);
1481 
1482  __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1483  __ push(r0);
1484  __ ldr(r0, MemOperand(fp, kArgsOffset)); // get the args array
1485  __ push(r0);
1486  __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1487 
1488  // Check the stack for overflow. We are not trying to catch
1489  // interruptions (e.g. debug break and preemption) here, so the "real stack
1490  // limit" is checked.
1491  Label okay;
1492  __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
1493  // Make r2 the space we have left. The stack might already be overflowed
1494  // here which will cause r2 to become negative.
1495  __ sub(r2, sp, r2);
1496  // Check if the arguments will overflow the stack.
1497  __ cmp(r2, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1498  __ b(gt, &okay); // Signed comparison.
1499 
1500  // Out of stack space.
1501  __ ldr(r1, MemOperand(fp, kFunctionOffset));
1502  __ push(r1);
1503  __ push(r0);
1504  __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
1505  // End of stack check.
1506 
1507  // Push current limit and index.
1508  __ bind(&okay);
1509  __ push(r0); // limit
1510  __ mov(r1, Operand(0, RelocInfo::NONE)); // initial index
1511  __ push(r1);
1512 
1513  // Get the receiver.
1514  __ ldr(r0, MemOperand(fp, kRecvOffset));
1515 
1516  // Check that the function is a JS function (otherwise it must be a proxy).
1517  Label push_receiver;
1518  __ ldr(r1, MemOperand(fp, kFunctionOffset));
1519  __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1520  __ b(ne, &push_receiver);
1521 
1522  // Change context eagerly to get the right global object if necessary.
1524  // Load the shared function info while the function is still in r1.
1526 
1527  // Compute the receiver.
1528  // Do not transform the receiver for strict mode functions.
1529  Label call_to_object, use_global_receiver;
1531  __ tst(r2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1532  kSmiTagSize)));
1533  __ b(ne, &push_receiver);
1534 
1535  // Do not transform the receiver for strict mode functions.
1536  __ tst(r2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1537  __ b(ne, &push_receiver);
1538 
1539  // Compute the receiver in non-strict mode.
1540  __ JumpIfSmi(r0, &call_to_object);
1541  __ LoadRoot(r1, Heap::kNullValueRootIndex);
1542  __ cmp(r0, r1);
1543  __ b(eq, &use_global_receiver);
1544  __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1545  __ cmp(r0, r1);
1546  __ b(eq, &use_global_receiver);
1547 
1548  // Check if the receiver is already a JavaScript object.
1549  // r0: receiver
1551  __ CompareObjectType(r0, r1, r1, FIRST_SPEC_OBJECT_TYPE);
1552  __ b(ge, &push_receiver);
1553 
1554  // Convert the receiver to a regular object.
1555  // r0: receiver
1556  __ bind(&call_to_object);
1557  __ push(r0);
1558  __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1559  __ b(&push_receiver);
1560 
1561  // Use the current global receiver object as the receiver.
1562  __ bind(&use_global_receiver);
1563  const int kGlobalOffset =
1565  __ ldr(r0, FieldMemOperand(cp, kGlobalOffset));
1567  __ ldr(r0, FieldMemOperand(r0, kGlobalOffset));
1569 
1570  // Push the receiver.
1571  // r0: receiver
1572  __ bind(&push_receiver);
1573  __ push(r0);
1574 
1575  // Copy all arguments from the array to the stack.
1576  Label entry, loop;
1577  __ ldr(r0, MemOperand(fp, kIndexOffset));
1578  __ b(&entry);
1579 
1580  // Load the current argument from the arguments array and push it to the
1581  // stack.
1582  // r0: current argument index
1583  __ bind(&loop);
1584  __ ldr(r1, MemOperand(fp, kArgsOffset));
1585  __ push(r1);
1586  __ push(r0);
1587 
1588  // Call the runtime to access the property in the arguments array.
1589  __ CallRuntime(Runtime::kGetProperty, 2);
1590  __ push(r0);
1591 
1592  // Use inline caching to access the arguments.
1593  __ ldr(r0, MemOperand(fp, kIndexOffset));
1594  __ add(r0, r0, Operand(1 << kSmiTagSize));
1595  __ str(r0, MemOperand(fp, kIndexOffset));
1596 
1597  // Test if the copy loop has finished copying all the elements from the
1598  // arguments object.
1599  __ bind(&entry);
1600  __ ldr(r1, MemOperand(fp, kLimitOffset));
1601  __ cmp(r0, r1);
1602  __ b(ne, &loop);
1603 
1604  // Invoke the function.
1605  Label call_proxy;
1606  ParameterCount actual(r0);
1607  __ mov(r0, Operand(r0, ASR, kSmiTagSize));
1608  __ ldr(r1, MemOperand(fp, kFunctionOffset));
1609  __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1610  __ b(ne, &call_proxy);
1611  __ InvokeFunction(r1, actual, CALL_FUNCTION,
1612  NullCallWrapper(), CALL_AS_METHOD);
1613 
1614  frame_scope.GenerateLeaveFrame();
1615  __ add(sp, sp, Operand(3 * kPointerSize));
1616  __ Jump(lr);
1617 
1618  // Invoke the function proxy.
1619  __ bind(&call_proxy);
1620  __ push(r1); // add function proxy as last argument
1621  __ add(r0, r0, Operand(1));
1622  __ mov(r2, Operand(0, RelocInfo::NONE));
1623  __ SetCallKind(r5, CALL_AS_METHOD);
1624  __ GetBuiltinEntry(r3, Builtins::CALL_FUNCTION_PROXY);
1625  __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1626  RelocInfo::CODE_TARGET);
1627 
1628  // Tear down the internal frame and remove function, receiver and args.
1629  }
1630  __ add(sp, sp, Operand(3 * kPointerSize));
1631  __ Jump(lr);
1632 }
1633 
1634 
1635 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1636  __ mov(r0, Operand(r0, LSL, kSmiTagSize));
1638  __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() | fp.bit() | lr.bit());
1639  __ add(fp, sp, Operand(3 * kPointerSize));
1640 }
1641 
1642 
1643 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1644  // ----------- S t a t e -------------
1645  // -- r0 : result being passed through
1646  // -----------------------------------
1647  // Get the number of arguments passed (as a smi), tear down the frame and
1648  // then tear down the parameters.
1649  __ ldr(r1, MemOperand(fp, -3 * kPointerSize));
1650  __ mov(sp, fp);
1651  __ ldm(ia_w, sp, fp.bit() | lr.bit());
1652  __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
1653  __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1654 }
1655 
1656 
1657 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1658  // ----------- S t a t e -------------
1659  // -- r0 : actual number of arguments
1660  // -- r1 : function (passed through to callee)
1661  // -- r2 : expected number of arguments
1662  // -- r3 : code entry to call
1663  // -- r5 : call kind information
1664  // -----------------------------------
1665 
1666  Label invoke, dont_adapt_arguments;
1667 
1668  Label enough, too_few;
1669  __ cmp(r0, r2);
1670  __ b(lt, &too_few);
1672  __ b(eq, &dont_adapt_arguments);
1673 
1674  { // Enough parameters: actual >= expected
1675  __ bind(&enough);
1676  EnterArgumentsAdaptorFrame(masm);
1677 
1678  // Calculate copy start address into r0 and copy end address into r2.
1679  // r0: actual number of arguments as a smi
1680  // r1: function
1681  // r2: expected number of arguments
1682  // r3: code entry to call
1683  __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1684  // adjust for return address and receiver
1685  __ add(r0, r0, Operand(2 * kPointerSize));
1686  __ sub(r2, r0, Operand(r2, LSL, kPointerSizeLog2));
1687 
1688  // Copy the arguments (including the receiver) to the new stack frame.
1689  // r0: copy start address
1690  // r1: function
1691  // r2: copy end address
1692  // r3: code entry to call
1693 
1694  Label copy;
1695  __ bind(&copy);
1696  __ ldr(ip, MemOperand(r0, 0));
1697  __ push(ip);
1698  __ cmp(r0, r2); // Compare before moving to next argument.
1699  __ sub(r0, r0, Operand(kPointerSize));
1700  __ b(ne, &copy);
1701 
1702  __ b(&invoke);
1703  }
1704 
1705  { // Too few parameters: Actual < expected
1706  __ bind(&too_few);
1707  EnterArgumentsAdaptorFrame(masm);
1708 
1709  // Calculate copy start address into r0 and copy end address is fp.
1710  // r0: actual number of arguments as a smi
1711  // r1: function
1712  // r2: expected number of arguments
1713  // r3: code entry to call
1714  __ add(r0, fp, Operand(r0, LSL, kPointerSizeLog2 - kSmiTagSize));
1715 
1716  // Copy the arguments (including the receiver) to the new stack frame.
1717  // r0: copy start address
1718  // r1: function
1719  // r2: expected number of arguments
1720  // r3: code entry to call
1721  Label copy;
1722  __ bind(&copy);
1723  // Adjust load for return address and receiver.
1724  __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1725  __ push(ip);
1726  __ cmp(r0, fp); // Compare before moving to next argument.
1727  __ sub(r0, r0, Operand(kPointerSize));
1728  __ b(ne, &copy);
1729 
1730  // Fill the remaining expected arguments with undefined.
1731  // r1: function
1732  // r2: expected number of arguments
1733  // r3: code entry to call
1734  __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1735  __ sub(r2, fp, Operand(r2, LSL, kPointerSizeLog2));
1736  __ sub(r2, r2, Operand(4 * kPointerSize)); // Adjust for frame.
1737 
1738  Label fill;
1739  __ bind(&fill);
1740  __ push(ip);
1741  __ cmp(sp, r2);
1742  __ b(ne, &fill);
1743  }
1744 
1745  // Call the entry point.
1746  __ bind(&invoke);
1747  __ Call(r3);
1748 
1749  // Store offset of return address for deoptimizer.
1750  masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1751 
1752  // Exit frame and return.
1753  LeaveArgumentsAdaptorFrame(masm);
1754  __ Jump(lr);
1755 
1756 
1757  // -------------------------------------------
1758  // Dont adapt arguments.
1759  // -------------------------------------------
1760  __ bind(&dont_adapt_arguments);
1761  __ Jump(r3);
1762 }
1763 
1764 
1765 #undef __
1766 
1767 } } // namespace v8::internal
1768 
1769 #endif // V8_TARGET_ARCH_ARM
const Register cp
const intptr_t kSmiTagMask
Definition: v8.h:3855
static const int kCodeEntryOffset
Definition: objects.h:5981
static const int kPrototypeOrInitialMapOffset
Definition: objects.h:5982
static int SlotOffset(int index)
Definition: contexts.h:408
const Register r3
static Smi * FromInt(int value)
Definition: objects-inl.h:973
const intptr_t kIntptrSignBit
Definition: globals.h:247
static const int kGlobalReceiverOffset
Definition: objects.h:6085
static const int kConstructionCountOffset
Definition: objects.h:5697
const Register r6
static bool IsSupported(CpuFeature f)
#define ASSERT(condition)
Definition: checks.h:270
const RegList kJSCallerSaved
Definition: frames-arm.h:47
const int kPointerSizeLog2
Definition: globals.h:246
static const int kInstanceSizeOffset
Definition: objects.h:4981
const Register r2
static const int kUnusedPropertyFieldsOffset
Definition: objects.h:4993
static const int kInstanceSizesOffset
Definition: objects.h:4951
static const int kGlobalContextOffset
Definition: objects.h:6084
static const int kContextOffset
Definition: objects.h:5986
static const int kSize
Definition: objects.h:8112
static const int kInObjectPropertiesByte
Definition: objects.h:4982
const uint32_t kNotStringTag
Definition: objects.h:438
const Register sp
STATIC_ASSERT((FixedDoubleArray::kHeaderSize &kDoubleAlignmentMask)==0)
BuiltinExtraArguments
Definition: builtins.h:35
static const int kDontAdaptArgumentsSentinel
Definition: objects.h:5601
const Register ip
const Register r9
const int kPointerSize
Definition: globals.h:234
const int kHeapObjectTag
Definition: v8.h:3848
const RegList kCalleeSaved
Definition: frames-arm.h:63
#define __
static const int kPropertiesOffset
Definition: objects.h:2113
const int kBitsPerByte
Definition: globals.h:251
const Register r0
static const int kElementsOffset
Definition: objects.h:2114
static const int kLengthOffset
Definition: objects.h:8111
static int SizeFor(int length)
Definition: objects.h:2288
static const int kHeaderSize
Definition: objects.h:2233
const Register lr
static const int kSize
Definition: objects.h:6189
static const int kMapOffset
Definition: objects.h:1219
const uint32_t kIsNotStringMask
Definition: objects.h:436
const Register r1
static const int kLengthOffset
Definition: objects.h:2232
MemOperand FieldMemOperand(Register object, int offset)
static const int kFormalParameterCountOffset
Definition: objects.h:5662
const int kSmiTagSize
Definition: v8.h:3854
static const int kHeaderSize
Definition: objects.h:4513
const Register r8
#define ASSERT_EQ(v1, v2)
Definition: checks.h:271
const int kSmiTag
Definition: v8.h:3853
static const int kHeaderSize
Definition: objects.h:2115
static void GenerateLookupNumberStringCache(MacroAssembler *masm, Register object, Register result, Register scratch1, Register scratch2, Register scratch3, bool object_is_smi, Label *not_found)
const int kR9Available
Definition: frames-arm.h:38
static const int kPreAllocatedPropertyFieldsByte
Definition: objects.h:4985
static const int kPreallocatedArrayElements
Definition: objects.h:8108
static const int kValueOffset
Definition: objects.h:6188
const Register fp
static const int kCompilerHintsOffset
Definition: objects.h:5677
static const int kSharedFunctionInfoOffset
Definition: objects.h:5984
static const int kInitialMaxFastElementArray
Definition: objects.h:2103
FlagType type() const
Definition: flags.cc:1358
const Register r5
static const int kInstanceTypeOffset
Definition: objects.h:4992
const Register r4
const Register r7