• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_ARM
6 
7 #include "src/codegen.h"
8 #include "src/debug/debug.h"
9 #include "src/deoptimizer.h"
10 #include "src/full-codegen/full-codegen.h"
11 #include "src/runtime/runtime.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 
17 #define __ ACCESS_MASM(masm)
18 
19 
Generate_Adaptor(MacroAssembler * masm,CFunctionId id,BuiltinExtraArguments extra_args)20 void Builtins::Generate_Adaptor(MacroAssembler* masm,
21                                 CFunctionId id,
22                                 BuiltinExtraArguments extra_args) {
23   // ----------- S t a t e -------------
24   //  -- r0                 : number of arguments excluding receiver
25   //  -- r1                 : target
26   //  -- r3                 : new.target
27   //  -- sp[0]              : last argument
28   //  -- ...
29   //  -- sp[4 * (argc - 1)] : first argument
30   //  -- sp[4 * argc]       : receiver
31   // -----------------------------------
32   __ AssertFunction(r1);
33 
34   // Make sure we operate in the context of the called function (for example
35   // ConstructStubs implemented in C++ will be run in the context of the caller
36   // instead of the callee, due to the way that [[Construct]] is defined for
37   // ordinary functions).
38   __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
39 
40   // Insert extra arguments.
41   int num_extra_args = 0;
42   switch (extra_args) {
43     case BuiltinExtraArguments::kTarget:
44       __ Push(r1);
45       ++num_extra_args;
46       break;
47     case BuiltinExtraArguments::kNewTarget:
48       __ Push(r3);
49       ++num_extra_args;
50       break;
51     case BuiltinExtraArguments::kTargetAndNewTarget:
52       __ Push(r1, r3);
53       num_extra_args += 2;
54       break;
55     case BuiltinExtraArguments::kNone:
56       break;
57   }
58 
59   // JumpToExternalReference expects r0 to contain the number of arguments
60   // including the receiver and the extra arguments.
61   __ add(r0, r0, Operand(num_extra_args + 1));
62 
63   __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
64 }
65 
66 
67 // Load the built-in InternalArray function from the current context.
GenerateLoadInternalArrayFunction(MacroAssembler * masm,Register result)68 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
69                                               Register result) {
70   // Load the InternalArray function from the current native context.
71   __ LoadNativeContextSlot(Context::INTERNAL_ARRAY_FUNCTION_INDEX, result);
72 }
73 
74 
75 // Load the built-in Array function from the current context.
GenerateLoadArrayFunction(MacroAssembler * masm,Register result)76 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
77   // Load the Array function from the current native context.
78   __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, result);
79 }
80 
81 
Generate_InternalArrayCode(MacroAssembler * masm)82 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
83   // ----------- S t a t e -------------
84   //  -- r0     : number of arguments
85   //  -- lr     : return address
86   //  -- sp[...]: constructor arguments
87   // -----------------------------------
88   Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
89 
90   // Get the InternalArray function.
91   GenerateLoadInternalArrayFunction(masm, r1);
92 
93   if (FLAG_debug_code) {
94     // Initial map for the builtin InternalArray functions should be maps.
95     __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
96     __ SmiTst(r2);
97     __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
98     __ CompareObjectType(r2, r3, r4, MAP_TYPE);
99     __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
100   }
101 
102   // Run the native code for the InternalArray function called as a normal
103   // function.
104   // tail call a stub
105   InternalArrayConstructorStub stub(masm->isolate());
106   __ TailCallStub(&stub);
107 }
108 
109 
Generate_ArrayCode(MacroAssembler * masm)110 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
111   // ----------- S t a t e -------------
112   //  -- r0     : number of arguments
113   //  -- lr     : return address
114   //  -- sp[...]: constructor arguments
115   // -----------------------------------
116   Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
117 
118   // Get the Array function.
119   GenerateLoadArrayFunction(masm, r1);
120 
121   if (FLAG_debug_code) {
122     // Initial map for the builtin Array functions should be maps.
123     __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
124     __ SmiTst(r2);
125     __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
126     __ CompareObjectType(r2, r3, r4, MAP_TYPE);
127     __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
128   }
129 
130   __ mov(r3, r1);
131   // Run the native code for the Array function called as a normal function.
132   // tail call a stub
133   __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
134   ArrayConstructorStub stub(masm->isolate());
135   __ TailCallStub(&stub);
136 }
137 
138 
139 // static
Generate_NumberConstructor(MacroAssembler * masm)140 void Builtins::Generate_NumberConstructor(MacroAssembler* masm) {
141   // ----------- S t a t e -------------
142   //  -- r0                     : number of arguments
143   //  -- r1                     : constructor function
144   //  -- lr                     : return address
145   //  -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
146   //  -- sp[argc * 4]           : receiver
147   // -----------------------------------
148 
149   // 1. Load the first argument into r0 and get rid of the rest (including the
150   // receiver).
151   Label no_arguments;
152   {
153     __ sub(r0, r0, Operand(1), SetCC);
154     __ b(lo, &no_arguments);
155     __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
156     __ Drop(2);
157   }
158 
159   // 2a. Convert the first argument to a number.
160   ToNumberStub stub(masm->isolate());
161   __ TailCallStub(&stub);
162 
163   // 2b. No arguments, return +0.
164   __ bind(&no_arguments);
165   __ Move(r0, Smi::FromInt(0));
166   __ Ret(1);
167 }
168 
169 
170 // static
Generate_NumberConstructor_ConstructStub(MacroAssembler * masm)171 void Builtins::Generate_NumberConstructor_ConstructStub(MacroAssembler* masm) {
172   // ----------- S t a t e -------------
173   //  -- r0                     : number of arguments
174   //  -- r1                     : constructor function
175   //  -- r3                     : new target
176   //  -- lr                     : return address
177   //  -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
178   //  -- sp[argc * 4]           : receiver
179   // -----------------------------------
180 
181   // 1. Make sure we operate in the context of the called function.
182   __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
183 
184   // 2. Load the first argument into r2 and get rid of the rest (including the
185   // receiver).
186   {
187     Label no_arguments, done;
188     __ sub(r0, r0, Operand(1), SetCC);
189     __ b(lo, &no_arguments);
190     __ ldr(r2, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
191     __ Drop(2);
192     __ b(&done);
193     __ bind(&no_arguments);
194     __ Move(r2, Smi::FromInt(0));
195     __ Drop(1);
196     __ bind(&done);
197   }
198 
199   // 3. Make sure r2 is a number.
200   {
201     Label done_convert;
202     __ JumpIfSmi(r2, &done_convert);
203     __ CompareObjectType(r2, r4, r4, HEAP_NUMBER_TYPE);
204     __ b(eq, &done_convert);
205     {
206       FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
207       __ Push(r1, r3);
208       __ Move(r0, r2);
209       ToNumberStub stub(masm->isolate());
210       __ CallStub(&stub);
211       __ Move(r2, r0);
212       __ Pop(r1, r3);
213     }
214     __ bind(&done_convert);
215   }
216 
217   // 4. Check if new target and constructor differ.
218   Label new_object;
219   __ cmp(r1, r3);
220   __ b(ne, &new_object);
221 
222   // 5. Allocate a JSValue wrapper for the number.
223   __ AllocateJSValue(r0, r1, r2, r4, r5, &new_object);
224   __ Ret();
225 
226   // 6. Fallback to the runtime to create new object.
227   __ bind(&new_object);
228   {
229     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
230     __ Push(r2, r1, r3);  // first argument, constructor, new target
231     __ CallRuntime(Runtime::kNewObject);
232     __ Pop(r2);
233   }
234   __ str(r2, FieldMemOperand(r0, JSValue::kValueOffset));
235   __ Ret();
236 }
237 
238 
239 // static
Generate_StringConstructor(MacroAssembler * masm)240 void Builtins::Generate_StringConstructor(MacroAssembler* masm) {
241   // ----------- S t a t e -------------
242   //  -- r0                     : number of arguments
243   //  -- r1                     : constructor function
244   //  -- lr                     : return address
245   //  -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
246   //  -- sp[argc * 4]           : receiver
247   // -----------------------------------
248 
249   // 1. Load the first argument into r0 and get rid of the rest (including the
250   // receiver).
251   Label no_arguments;
252   {
253     __ sub(r0, r0, Operand(1), SetCC);
254     __ b(lo, &no_arguments);
255     __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
256     __ Drop(2);
257   }
258 
259   // 2a. At least one argument, return r0 if it's a string, otherwise
260   // dispatch to appropriate conversion.
261   Label to_string, symbol_descriptive_string;
262   {
263     __ JumpIfSmi(r0, &to_string);
264     STATIC_ASSERT(FIRST_NONSTRING_TYPE == SYMBOL_TYPE);
265     __ CompareObjectType(r0, r1, r1, FIRST_NONSTRING_TYPE);
266     __ b(hi, &to_string);
267     __ b(eq, &symbol_descriptive_string);
268     __ Ret();
269   }
270 
271   // 2b. No arguments, return the empty string (and pop the receiver).
272   __ bind(&no_arguments);
273   {
274     __ LoadRoot(r0, Heap::kempty_stringRootIndex);
275     __ Ret(1);
276   }
277 
278   // 3a. Convert r0 to a string.
279   __ bind(&to_string);
280   {
281     ToStringStub stub(masm->isolate());
282     __ TailCallStub(&stub);
283   }
284 
285   // 3b. Convert symbol in r0 to a string.
286   __ bind(&symbol_descriptive_string);
287   {
288     __ Push(r0);
289     __ TailCallRuntime(Runtime::kSymbolDescriptiveString);
290   }
291 }
292 
293 
294 // static
Generate_StringConstructor_ConstructStub(MacroAssembler * masm)295 void Builtins::Generate_StringConstructor_ConstructStub(MacroAssembler* masm) {
296   // ----------- S t a t e -------------
297   //  -- r0                     : number of arguments
298   //  -- r1                     : constructor function
299   //  -- r3                     : new target
300   //  -- lr                     : return address
301   //  -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
302   //  -- sp[argc * 4]           : receiver
303   // -----------------------------------
304 
305   // 1. Make sure we operate in the context of the called function.
306   __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
307 
308   // 2. Load the first argument into r2 and get rid of the rest (including the
309   // receiver).
310   {
311     Label no_arguments, done;
312     __ sub(r0, r0, Operand(1), SetCC);
313     __ b(lo, &no_arguments);
314     __ ldr(r2, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
315     __ Drop(2);
316     __ b(&done);
317     __ bind(&no_arguments);
318     __ LoadRoot(r2, Heap::kempty_stringRootIndex);
319     __ Drop(1);
320     __ bind(&done);
321   }
322 
323   // 3. Make sure r2 is a string.
324   {
325     Label convert, done_convert;
326     __ JumpIfSmi(r2, &convert);
327     __ CompareObjectType(r2, r4, r4, FIRST_NONSTRING_TYPE);
328     __ b(lo, &done_convert);
329     __ bind(&convert);
330     {
331       FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
332       ToStringStub stub(masm->isolate());
333       __ Push(r1, r3);
334       __ Move(r0, r2);
335       __ CallStub(&stub);
336       __ Move(r2, r0);
337       __ Pop(r1, r3);
338     }
339     __ bind(&done_convert);
340   }
341 
342   // 4. Check if new target and constructor differ.
343   Label new_object;
344   __ cmp(r1, r3);
345   __ b(ne, &new_object);
346 
347   // 5. Allocate a JSValue wrapper for the string.
348   __ AllocateJSValue(r0, r1, r2, r4, r5, &new_object);
349   __ Ret();
350 
351   // 6. Fallback to the runtime to create new object.
352   __ bind(&new_object);
353   {
354     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
355     __ Push(r2, r1, r3);  // first argument, constructor, new target
356     __ CallRuntime(Runtime::kNewObject);
357     __ Pop(r2);
358   }
359   __ str(r2, FieldMemOperand(r0, JSValue::kValueOffset));
360   __ Ret();
361 }
362 
363 
CallRuntimePassFunction(MacroAssembler * masm,Runtime::FunctionId function_id)364 static void CallRuntimePassFunction(
365     MacroAssembler* masm, Runtime::FunctionId function_id) {
366   // ----------- S t a t e -------------
367   //  -- r1 : target function (preserved for callee)
368   //  -- r3 : new target (preserved for callee)
369   // -----------------------------------
370 
371   FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
372   // Push a copy of the target function and the new target.
373   __ push(r1);
374   __ push(r3);
375   // Push function as parameter to the runtime call.
376   __ Push(r1);
377 
378   __ CallRuntime(function_id, 1);
379   // Restore target function and new target.
380   __ pop(r3);
381   __ pop(r1);
382 }
383 
384 
GenerateTailCallToSharedCode(MacroAssembler * masm)385 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
386   __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
387   __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCodeOffset));
388   __ add(r2, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
389   __ Jump(r2);
390 }
391 
392 
GenerateTailCallToReturnedCode(MacroAssembler * masm)393 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
394   __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
395   __ Jump(r0);
396 }
397 
398 
Generate_InOptimizationQueue(MacroAssembler * masm)399 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
400   // Checking whether the queued function is ready for install is optional,
401   // since we come across interrupts and stack checks elsewhere.  However,
402   // not checking may delay installing ready functions, and always checking
403   // would be quite expensive.  A good compromise is to first check against
404   // stack limit as a cue for an interrupt signal.
405   Label ok;
406   __ LoadRoot(ip, Heap::kStackLimitRootIndex);
407   __ cmp(sp, Operand(ip));
408   __ b(hs, &ok);
409 
410   CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
411   GenerateTailCallToReturnedCode(masm);
412 
413   __ bind(&ok);
414   GenerateTailCallToSharedCode(masm);
415 }
416 
417 
Generate_JSConstructStubHelper(MacroAssembler * masm,bool is_api_function,bool create_implicit_receiver)418 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
419                                            bool is_api_function,
420                                            bool create_implicit_receiver) {
421   // ----------- S t a t e -------------
422   //  -- r0     : number of arguments
423   //  -- r1     : constructor function
424   //  -- r2     : allocation site or undefined
425   //  -- r3     : new target
426   //  -- lr     : return address
427   //  -- sp[...]: constructor arguments
428   // -----------------------------------
429 
430   Isolate* isolate = masm->isolate();
431 
432   // Enter a construct frame.
433   {
434     FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
435 
436     // Preserve the incoming parameters on the stack.
437     __ AssertUndefinedOrAllocationSite(r2, r4);
438     __ push(r2);
439     __ SmiTag(r0);
440     __ push(r0);
441 
442     if (create_implicit_receiver) {
443       // Try to allocate the object without transitioning into C code. If any of
444       // the preconditions is not met, the code bails out to the runtime call.
445       Label rt_call, allocated;
446       if (FLAG_inline_new) {
447         // Verify that the new target is a JSFunction.
448         __ CompareObjectType(r3, r5, r4, JS_FUNCTION_TYPE);
449         __ b(ne, &rt_call);
450 
451         // Load the initial map and verify that it is in fact a map.
452         // r3: new target
453         __ ldr(r2,
454                FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
455         __ JumpIfSmi(r2, &rt_call);
456         __ CompareObjectType(r2, r5, r4, MAP_TYPE);
457         __ b(ne, &rt_call);
458 
459         // Fall back to runtime if the expected base constructor and base
460         // constructor differ.
461         __ ldr(r5, FieldMemOperand(r2, Map::kConstructorOrBackPointerOffset));
462         __ cmp(r1, r5);
463         __ b(ne, &rt_call);
464 
465         // Check that the constructor is not constructing a JSFunction (see
466         // comments in Runtime_NewObject in runtime.cc). In which case the
467         // initial map's instance type would be JS_FUNCTION_TYPE.
468         // r1: constructor function
469         // r2: initial map
470         // r3: new target
471         __ CompareInstanceType(r2, r5, JS_FUNCTION_TYPE);
472         __ b(eq, &rt_call);
473 
474         // Now allocate the JSObject on the heap.
475         // r1: constructor function
476         // r2: initial map
477         // r3: new target
478         __ ldrb(r9, FieldMemOperand(r2, Map::kInstanceSizeOffset));
479 
480         __ Allocate(r9, r4, r9, r6, &rt_call, SIZE_IN_WORDS);
481 
482         // Allocated the JSObject, now initialize the fields. Map is set to
483         // initial map and properties and elements are set to empty fixed array.
484         // r1: constructor function
485         // r2: initial map
486         // r3: new target
487         // r4: JSObject (not HeapObject tagged - the actual address).
488         // r9: start of next object
489         __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
490         __ mov(r5, r4);
491         STATIC_ASSERT(0 * kPointerSize == JSObject::kMapOffset);
492         __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
493         STATIC_ASSERT(1 * kPointerSize == JSObject::kPropertiesOffset);
494         __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
495         STATIC_ASSERT(2 * kPointerSize == JSObject::kElementsOffset);
496         __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
497         STATIC_ASSERT(3 * kPointerSize == JSObject::kHeaderSize);
498 
499         // Add the object tag to make the JSObject real, so that we can continue
500         // and jump into the continuation code at any time from now on.
501         __ add(r4, r4, Operand(kHeapObjectTag));
502 
503         // Fill all the in-object properties with the appropriate filler.
504         // r4: JSObject (tagged)
505         // r5: First in-object property of JSObject (not tagged)
506         __ LoadRoot(r6, Heap::kUndefinedValueRootIndex);
507 
508         if (!is_api_function) {
509           Label no_inobject_slack_tracking;
510 
511           // Check if slack tracking is enabled.
512           MemOperand bit_field3 = FieldMemOperand(r2, Map::kBitField3Offset);
513           // Check if slack tracking is enabled.
514           __ ldr(r0, bit_field3);
515           __ DecodeField<Map::ConstructionCounter>(ip, r0);
516           // ip: slack tracking counter
517           __ cmp(ip, Operand(Map::kSlackTrackingCounterEnd));
518           __ b(lt, &no_inobject_slack_tracking);
519           __ push(ip);  // Save allocation count value.
520           // Decrease generous allocation count.
521           __ sub(r0, r0, Operand(1 << Map::ConstructionCounter::kShift));
522           __ str(r0, bit_field3);
523 
524           // Allocate object with a slack.
525           __ ldr(r0, FieldMemOperand(r2, Map::kInstanceAttributesOffset));
526           __ Ubfx(r0, r0, Map::kUnusedPropertyFieldsByte * kBitsPerByte,
527                   kBitsPerByte);
528           __ sub(r0, r9, Operand(r0, LSL, kPointerSizeLog2));
529           // r0: offset of first field after pre-allocated fields
530           if (FLAG_debug_code) {
531             __ cmp(r5, r0);
532             __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
533           }
534           __ InitializeFieldsWithFiller(r5, r0, r6);
535 
536           // To allow truncation fill the remaining fields with one pointer
537           // filler map.
538           __ LoadRoot(r6, Heap::kOnePointerFillerMapRootIndex);
539           __ InitializeFieldsWithFiller(r5, r9, r6);
540 
541           __ pop(r0);  // Restore allocation count value before decreasing.
542           __ cmp(r0, Operand(Map::kSlackTrackingCounterEnd));
543           __ b(ne, &allocated);
544 
545           // Push the constructor, new_target and the object to the stack,
546           // and then the initial map as an argument to the runtime call.
547           __ Push(r1, r3, r4, r2);
548           __ CallRuntime(Runtime::kFinalizeInstanceSize);
549           __ Pop(r1, r3, r4);
550 
551           // Continue with JSObject being successfully allocated
552           // r1: constructor function
553           // r3: new target
554           // r4: JSObject
555           __ jmp(&allocated);
556 
557           __ bind(&no_inobject_slack_tracking);
558         }
559 
560         __ InitializeFieldsWithFiller(r5, r9, r6);
561 
562         // Continue with JSObject being successfully allocated
563         // r1: constructor function
564         // r3: new target
565         // r4: JSObject
566         __ jmp(&allocated);
567       }
568 
569       // Allocate the new receiver object using the runtime call.
570       // r1: constructor function
571       // r3: new target
572       __ bind(&rt_call);
573 
574       // Push the constructor and new_target twice, second pair as arguments
575       // to the runtime call.
576       __ Push(r1, r3);
577       __ Push(r1, r3);  // constructor function, new target
578       __ CallRuntime(Runtime::kNewObject);
579       __ mov(r4, r0);
580       __ Pop(r1, r3);
581 
582       // Receiver for constructor call allocated.
583       // r1: constructor function
584       // r3: new target
585       // r4: JSObject
586       __ bind(&allocated);
587 
588       // Retrieve smi-tagged arguments count from the stack.
589       __ ldr(r0, MemOperand(sp));
590     }
591 
592     __ SmiUntag(r0);
593 
594     if (create_implicit_receiver) {
595       // Push the allocated receiver to the stack. We need two copies
596       // because we may have to return the original one and the calling
597       // conventions dictate that the called function pops the receiver.
598       __ push(r4);
599       __ push(r4);
600     } else {
601       __ PushRoot(Heap::kTheHoleValueRootIndex);
602     }
603 
604     // Set up pointer to last argument.
605     __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
606 
607     // Copy arguments and receiver to the expression stack.
608     // r0: number of arguments
609     // r1: constructor function
610     // r2: address of last argument (caller sp)
611     // r3: new target
612     // r4: number of arguments (smi-tagged)
613     // sp[0]: receiver
614     // sp[1]: receiver
615     // sp[2]: number of arguments (smi-tagged)
616     Label loop, entry;
617     __ SmiTag(r4, r0);
618     __ b(&entry);
619     __ bind(&loop);
620     __ ldr(ip, MemOperand(r2, r4, LSL, kPointerSizeLog2 - 1));
621     __ push(ip);
622     __ bind(&entry);
623     __ sub(r4, r4, Operand(2), SetCC);
624     __ b(ge, &loop);
625 
626     // Call the function.
627     // r0: number of arguments
628     // r1: constructor function
629     // r3: new target
630     if (is_api_function) {
631       __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
632       Handle<Code> code =
633           masm->isolate()->builtins()->HandleApiCallConstruct();
634       __ Call(code, RelocInfo::CODE_TARGET);
635     } else {
636       ParameterCount actual(r0);
637       __ InvokeFunction(r1, r3, actual, CALL_FUNCTION,
638                         CheckDebugStepCallWrapper());
639     }
640 
641     // Store offset of return address for deoptimizer.
642     if (create_implicit_receiver && !is_api_function) {
643       masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
644     }
645 
646     // Restore context from the frame.
647     // r0: result
648     // sp[0]: receiver
649     // sp[1]: number of arguments (smi-tagged)
650     __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
651 
652     if (create_implicit_receiver) {
653       // If the result is an object (in the ECMA sense), we should get rid
654       // of the receiver and use the result; see ECMA-262 section 13.2.2-7
655       // on page 74.
656       Label use_receiver, exit;
657 
658       // If the result is a smi, it is *not* an object in the ECMA sense.
659       // r0: result
660       // sp[0]: receiver
661       // sp[1]: number of arguments (smi-tagged)
662       __ JumpIfSmi(r0, &use_receiver);
663 
664       // If the type of the result (stored in its map) is less than
665       // FIRST_JS_RECEIVER_TYPE, it is not an object in the ECMA sense.
666       __ CompareObjectType(r0, r1, r3, FIRST_JS_RECEIVER_TYPE);
667       __ b(ge, &exit);
668 
669       // Throw away the result of the constructor invocation and use the
670       // on-stack receiver as the result.
671       __ bind(&use_receiver);
672       __ ldr(r0, MemOperand(sp));
673 
674       // Remove receiver from the stack, remove caller arguments, and
675       // return.
676       __ bind(&exit);
677       // r0: result
678       // sp[0]: receiver (newly allocated object)
679       // sp[1]: number of arguments (smi-tagged)
680       __ ldr(r1, MemOperand(sp, 1 * kPointerSize));
681     } else {
682       __ ldr(r1, MemOperand(sp));
683     }
684 
685     // Leave construct frame.
686   }
687 
688   __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
689   __ add(sp, sp, Operand(kPointerSize));
690   if (create_implicit_receiver) {
691     __ IncrementCounter(isolate->counters()->constructed_objects(), 1, r1, r2);
692   }
693   __ Jump(lr);
694 }
695 
696 
Generate_JSConstructStubGeneric(MacroAssembler * masm)697 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
698   Generate_JSConstructStubHelper(masm, false, true);
699 }
700 
701 
Generate_JSConstructStubApi(MacroAssembler * masm)702 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
703   Generate_JSConstructStubHelper(masm, true, true);
704 }
705 
706 
Generate_JSBuiltinsConstructStub(MacroAssembler * masm)707 void Builtins::Generate_JSBuiltinsConstructStub(MacroAssembler* masm) {
708   Generate_JSConstructStubHelper(masm, false, false);
709 }
710 
711 
Generate_ConstructedNonConstructable(MacroAssembler * masm)712 void Builtins::Generate_ConstructedNonConstructable(MacroAssembler* masm) {
713   FrameScope scope(masm, StackFrame::INTERNAL);
714   __ push(r1);
715   __ CallRuntime(Runtime::kThrowConstructedNonConstructable);
716 }
717 
718 
719 enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
720 
721 
722 // Clobbers r2; preserves all other registers.
Generate_CheckStackOverflow(MacroAssembler * masm,Register argc,IsTagged argc_is_tagged)723 static void Generate_CheckStackOverflow(MacroAssembler* masm, Register argc,
724                                         IsTagged argc_is_tagged) {
725   // Check the stack for overflow. We are not trying to catch
726   // interruptions (e.g. debug break and preemption) here, so the "real stack
727   // limit" is checked.
728   Label okay;
729   __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
730   // Make r2 the space we have left. The stack might already be overflowed
731   // here which will cause r2 to become negative.
732   __ sub(r2, sp, r2);
733   // Check if the arguments will overflow the stack.
734   if (argc_is_tagged == kArgcIsSmiTagged) {
735     __ cmp(r2, Operand::PointerOffsetFromSmiKey(argc));
736   } else {
737     DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
738     __ cmp(r2, Operand(argc, LSL, kPointerSizeLog2));
739   }
740   __ b(gt, &okay);  // Signed comparison.
741 
742   // Out of stack space.
743   __ CallRuntime(Runtime::kThrowStackOverflow);
744 
745   __ bind(&okay);
746 }
747 
748 
Generate_JSEntryTrampolineHelper(MacroAssembler * masm,bool is_construct)749 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
750                                              bool is_construct) {
751   // Called from Generate_JS_Entry
752   // r0: new.target
753   // r1: function
754   // r2: receiver
755   // r3: argc
756   // r4: argv
757   // r5-r6, r8 (if !FLAG_enable_embedded_constant_pool) and cp may be clobbered
758   ProfileEntryHookStub::MaybeCallEntryHook(masm);
759 
760   // Clear the context before we push it when entering the internal frame.
761   __ mov(cp, Operand::Zero());
762 
763   // Enter an internal frame.
764   {
765     FrameScope scope(masm, StackFrame::INTERNAL);
766 
767     // Setup the context (we need to use the caller context from the isolate).
768     ExternalReference context_address(Isolate::kContextAddress,
769                                       masm->isolate());
770     __ mov(cp, Operand(context_address));
771     __ ldr(cp, MemOperand(cp));
772 
773     __ InitializeRootRegister();
774 
775     // Push the function and the receiver onto the stack.
776     __ Push(r1, r2);
777 
778     // Check if we have enough stack space to push all arguments.
779     // Clobbers r2.
780     Generate_CheckStackOverflow(masm, r3, kArgcIsUntaggedInt);
781 
782     // Remember new.target.
783     __ mov(r5, r0);
784 
785     // Copy arguments to the stack in a loop.
786     // r1: function
787     // r3: argc
788     // r4: argv, i.e. points to first arg
789     Label loop, entry;
790     __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
791     // r2 points past last arg.
792     __ b(&entry);
793     __ bind(&loop);
794     __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex));  // read next parameter
795     __ ldr(r0, MemOperand(r0));  // dereference handle
796     __ push(r0);  // push parameter
797     __ bind(&entry);
798     __ cmp(r4, r2);
799     __ b(ne, &loop);
800 
801     // Setup new.target and argc.
802     __ mov(r0, Operand(r3));
803     __ mov(r3, Operand(r5));
804 
805     // Initialize all JavaScript callee-saved registers, since they will be seen
806     // by the garbage collector as part of handlers.
807     __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
808     __ mov(r5, Operand(r4));
809     __ mov(r6, Operand(r4));
810     if (!FLAG_enable_embedded_constant_pool) {
811       __ mov(r8, Operand(r4));
812     }
813     if (kR9Available == 1) {
814       __ mov(r9, Operand(r4));
815     }
816 
817     // Invoke the code.
818     Handle<Code> builtin = is_construct
819                                ? masm->isolate()->builtins()->Construct()
820                                : masm->isolate()->builtins()->Call();
821     __ Call(builtin, RelocInfo::CODE_TARGET);
822 
823     // Exit the JS frame and remove the parameters (except function), and
824     // return.
825     // Respect ABI stack constraint.
826   }
827   __ Jump(lr);
828 
829   // r0: result
830 }
831 
832 
Generate_JSEntryTrampoline(MacroAssembler * masm)833 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
834   Generate_JSEntryTrampolineHelper(masm, false);
835 }
836 
837 
Generate_JSConstructEntryTrampoline(MacroAssembler * masm)838 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
839   Generate_JSEntryTrampolineHelper(masm, true);
840 }
841 
842 
843 // Generate code for entering a JS function with the interpreter.
844 // On entry to the function the receiver and arguments have been pushed on the
845 // stack left to right.  The actual argument count matches the formal parameter
846 // count expected by the function.
847 //
848 // The live registers are:
849 //   o r1: the JS function object being called.
850 //   o r3: the new target
851 //   o cp: our context
852 //   o pp: the caller's constant pool pointer (if enabled)
853 //   o fp: the caller's frame pointer
854 //   o sp: stack pointer
855 //   o lr: return address
856 //
857 // The function builds a JS frame.  Please see JavaScriptFrameConstants in
858 // frames-arm.h for its layout.
859 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
860 // frame.
Generate_InterpreterEntryTrampoline(MacroAssembler * masm)861 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
862   // Open a frame scope to indicate that there is a frame on the stack.  The
863   // MANUAL indicates that the scope shouldn't actually generate code to set up
864   // the frame (that is done below).
865   FrameScope frame_scope(masm, StackFrame::MANUAL);
866   __ PushFixedFrame(r1);
867   __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
868   __ push(r3);
869 
870   // Push zero for bytecode array offset.
871   __ mov(r0, Operand(0));
872   __ push(r0);
873 
874   // Get the bytecode array from the function object and load the pointer to the
875   // first entry into kInterpreterBytecodeRegister.
876   __ ldr(r0, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
877   __ ldr(kInterpreterBytecodeArrayRegister,
878          FieldMemOperand(r0, SharedFunctionInfo::kFunctionDataOffset));
879 
880   if (FLAG_debug_code) {
881     // Check function data field is actually a BytecodeArray object.
882     __ SmiTst(kInterpreterBytecodeArrayRegister);
883     __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
884     __ CompareObjectType(kInterpreterBytecodeArrayRegister, r0, no_reg,
885                          BYTECODE_ARRAY_TYPE);
886     __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
887   }
888 
889   // Allocate the local and temporary register file on the stack.
890   {
891     // Load frame size from the BytecodeArray object.
892     __ ldr(r4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
893                                BytecodeArray::kFrameSizeOffset));
894 
895     // Do a stack check to ensure we don't go over the limit.
896     Label ok;
897     __ sub(r9, sp, Operand(r4));
898     __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
899     __ cmp(r9, Operand(r2));
900     __ b(hs, &ok);
901     __ CallRuntime(Runtime::kThrowStackOverflow);
902     __ bind(&ok);
903 
904     // If ok, push undefined as the initial value for all register file entries.
905     Label loop_header;
906     Label loop_check;
907     __ LoadRoot(r9, Heap::kUndefinedValueRootIndex);
908     __ b(&loop_check, al);
909     __ bind(&loop_header);
910     // TODO(rmcilroy): Consider doing more than one push per loop iteration.
911     __ push(r9);
912     // Continue loop if not done.
913     __ bind(&loop_check);
914     __ sub(r4, r4, Operand(kPointerSize), SetCC);
915     __ b(&loop_header, ge);
916   }
917 
918   // TODO(rmcilroy): List of things not currently dealt with here but done in
919   // fullcodegen's prologue:
920   //  - Support profiler (specifically profiling_counter).
921   //  - Call ProfileEntryHookStub when isolate has a function_entry_hook.
922   //  - Allow simulator stop operations if FLAG_stop_at is set.
923   //  - Code aging of the BytecodeArray object.
924 
925   // Perform stack guard check.
926   {
927     Label ok;
928     __ LoadRoot(ip, Heap::kStackLimitRootIndex);
929     __ cmp(sp, Operand(ip));
930     __ b(hs, &ok);
931     __ push(kInterpreterBytecodeArrayRegister);
932     __ CallRuntime(Runtime::kStackGuard);
933     __ pop(kInterpreterBytecodeArrayRegister);
934     __ bind(&ok);
935   }
936 
937   // Load accumulator, register file, bytecode offset, dispatch table into
938   // registers.
939   __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
940   __ add(kInterpreterRegisterFileRegister, fp,
941          Operand(InterpreterFrameConstants::kRegisterFilePointerFromFp));
942   __ mov(kInterpreterBytecodeOffsetRegister,
943          Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
944   __ LoadRoot(kInterpreterDispatchTableRegister,
945               Heap::kInterpreterTableRootIndex);
946   __ add(kInterpreterDispatchTableRegister, kInterpreterDispatchTableRegister,
947          Operand(FixedArray::kHeaderSize - kHeapObjectTag));
948 
949   // Dispatch to the first bytecode handler for the function.
950   __ ldrb(r1, MemOperand(kInterpreterBytecodeArrayRegister,
951                          kInterpreterBytecodeOffsetRegister));
952   __ ldr(ip, MemOperand(kInterpreterDispatchTableRegister, r1, LSL,
953                         kPointerSizeLog2));
954   // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
955   // and header removal.
956   __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
957   __ Call(ip);
958 }
959 
960 
Generate_InterpreterExitTrampoline(MacroAssembler * masm)961 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
962   // TODO(rmcilroy): List of things not currently dealt with here but done in
963   // fullcodegen's EmitReturnSequence.
964   //  - Supporting FLAG_trace for Runtime::TraceExit.
965   //  - Support profiler (specifically decrementing profiling_counter
966   //    appropriately and calling out to HandleInterrupts if necessary).
967 
968   // The return value is in accumulator, which is already in r0.
969 
970   // Leave the frame (also dropping the register file).
971   __ LeaveFrame(StackFrame::JAVA_SCRIPT);
972 
973   // Drop receiver + arguments and return.
974   __ ldr(ip, FieldMemOperand(kInterpreterBytecodeArrayRegister,
975                              BytecodeArray::kParameterSizeOffset));
976   __ add(sp, sp, ip, LeaveCC);
977   __ Jump(lr);
978 }
979 
980 
Generate_InterpreterPushArgs(MacroAssembler * masm,Register index,Register limit,Register scratch)981 static void Generate_InterpreterPushArgs(MacroAssembler* masm, Register index,
982                                          Register limit, Register scratch) {
983   Label loop_header, loop_check;
984   __ b(al, &loop_check);
985   __ bind(&loop_header);
986   __ ldr(scratch, MemOperand(index, -kPointerSize, PostIndex));
987   __ push(scratch);
988   __ bind(&loop_check);
989   __ cmp(index, limit);
990   __ b(gt, &loop_header);
991 }
992 
993 
994 // static
Generate_InterpreterPushArgsAndCall(MacroAssembler * masm)995 void Builtins::Generate_InterpreterPushArgsAndCall(MacroAssembler* masm) {
996   // ----------- S t a t e -------------
997   //  -- r0 : the number of arguments (not including the receiver)
998   //  -- r2 : the address of the first argument to be pushed. Subsequent
999   //          arguments should be consecutive above this, in the same order as
1000   //          they are to be pushed onto the stack.
1001   //  -- r1 : the target to call (can be any Object).
1002   // -----------------------------------
1003 
1004   // Find the address of the last argument.
1005   __ add(r3, r0, Operand(1));  // Add one for receiver.
1006   __ mov(r3, Operand(r3, LSL, kPointerSizeLog2));
1007   __ sub(r3, r2, r3);
1008 
1009   // Push the arguments.
1010   Generate_InterpreterPushArgs(masm, r2, r3, r4);
1011 
1012   // Call the target.
1013   __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1014 }
1015 
1016 
1017 // static
Generate_InterpreterPushArgsAndConstruct(MacroAssembler * masm)1018 void Builtins::Generate_InterpreterPushArgsAndConstruct(MacroAssembler* masm) {
1019   // ----------- S t a t e -------------
1020   // -- r0 : argument count (not including receiver)
1021   // -- r3 : new target
1022   // -- r1 : constructor to call
1023   // -- r2 : address of the first argument
1024   // -----------------------------------
1025 
1026   // Find the address of the last argument.
1027   __ mov(r4, Operand(r0, LSL, kPointerSizeLog2));
1028   __ sub(r4, r2, r4);
1029 
1030   // Push a slot for the receiver to be constructed.
1031   __ mov(ip, Operand::Zero());
1032   __ push(ip);
1033 
1034   // Push the arguments.
1035   Generate_InterpreterPushArgs(masm, r2, r4, r5);
1036 
1037   // Call the constructor with r0, r1, and r3 unmodified.
1038   __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
1039 }
1040 
1041 
Generate_InterpreterNotifyDeoptimizedHelper(MacroAssembler * masm,Deoptimizer::BailoutType type)1042 static void Generate_InterpreterNotifyDeoptimizedHelper(
1043     MacroAssembler* masm, Deoptimizer::BailoutType type) {
1044   // Enter an internal frame.
1045   {
1046     FrameScope scope(masm, StackFrame::INTERNAL);
1047     __ push(kInterpreterAccumulatorRegister);  // Save accumulator register.
1048 
1049     // Pass the deoptimization type to the runtime system.
1050     __ mov(r1, Operand(Smi::FromInt(static_cast<int>(type))));
1051     __ push(r1);
1052     __ CallRuntime(Runtime::kNotifyDeoptimized);
1053 
1054     __ pop(kInterpreterAccumulatorRegister);  // Restore accumulator register.
1055     // Tear down internal frame.
1056   }
1057 
1058   // Drop state (we don't use this for interpreter deopts).
1059   __ Drop(1);
1060 
1061   // Initialize register file register and dispatch table register.
1062   __ add(kInterpreterRegisterFileRegister, fp,
1063          Operand(InterpreterFrameConstants::kRegisterFilePointerFromFp));
1064   __ LoadRoot(kInterpreterDispatchTableRegister,
1065               Heap::kInterpreterTableRootIndex);
1066   __ add(kInterpreterDispatchTableRegister, kInterpreterDispatchTableRegister,
1067          Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1068 
1069   // Get the context from the frame.
1070   // TODO(rmcilroy): Update interpreter frame to expect current context at the
1071   // context slot instead of the function context.
1072   __ ldr(kContextRegister,
1073          MemOperand(kInterpreterRegisterFileRegister,
1074                     InterpreterFrameConstants::kContextFromRegisterPointer));
1075 
1076   // Get the bytecode array pointer from the frame.
1077   __ ldr(r1,
1078          MemOperand(kInterpreterRegisterFileRegister,
1079                     InterpreterFrameConstants::kFunctionFromRegisterPointer));
1080   __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1081   __ ldr(kInterpreterBytecodeArrayRegister,
1082          FieldMemOperand(r1, SharedFunctionInfo::kFunctionDataOffset));
1083 
1084   if (FLAG_debug_code) {
1085     // Check function data field is actually a BytecodeArray object.
1086     __ SmiTst(kInterpreterBytecodeArrayRegister);
1087     __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
1088     __ CompareObjectType(kInterpreterBytecodeArrayRegister, r1, no_reg,
1089                          BYTECODE_ARRAY_TYPE);
1090     __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
1091   }
1092 
1093   // Get the target bytecode offset from the frame.
1094   __ ldr(kInterpreterBytecodeOffsetRegister,
1095          MemOperand(
1096              kInterpreterRegisterFileRegister,
1097              InterpreterFrameConstants::kBytecodeOffsetFromRegisterPointer));
1098   __ SmiUntag(kInterpreterBytecodeOffsetRegister);
1099 
1100   // Dispatch to the target bytecode.
1101   __ ldrb(r1, MemOperand(kInterpreterBytecodeArrayRegister,
1102                          kInterpreterBytecodeOffsetRegister));
1103   __ ldr(ip, MemOperand(kInterpreterDispatchTableRegister, r1, LSL,
1104                         kPointerSizeLog2));
1105   __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
1106   __ mov(pc, ip);
1107 }
1108 
1109 
Generate_InterpreterNotifyDeoptimized(MacroAssembler * masm)1110 void Builtins::Generate_InterpreterNotifyDeoptimized(MacroAssembler* masm) {
1111   Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1112 }
1113 
1114 
Generate_InterpreterNotifySoftDeoptimized(MacroAssembler * masm)1115 void Builtins::Generate_InterpreterNotifySoftDeoptimized(MacroAssembler* masm) {
1116   Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1117 }
1118 
1119 
Generate_InterpreterNotifyLazyDeoptimized(MacroAssembler * masm)1120 void Builtins::Generate_InterpreterNotifyLazyDeoptimized(MacroAssembler* masm) {
1121   Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1122 }
1123 
1124 
Generate_CompileLazy(MacroAssembler * masm)1125 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
1126   CallRuntimePassFunction(masm, Runtime::kCompileLazy);
1127   GenerateTailCallToReturnedCode(masm);
1128 }
1129 
1130 
Generate_CompileOptimized(MacroAssembler * masm)1131 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
1132   CallRuntimePassFunction(masm, Runtime::kCompileOptimized_NotConcurrent);
1133   GenerateTailCallToReturnedCode(masm);
1134 }
1135 
1136 
Generate_CompileOptimizedConcurrent(MacroAssembler * masm)1137 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
1138   CallRuntimePassFunction(masm, Runtime::kCompileOptimized_Concurrent);
1139   GenerateTailCallToReturnedCode(masm);
1140 }
1141 
1142 
GenerateMakeCodeYoungAgainCommon(MacroAssembler * masm)1143 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1144   // For now, we are relying on the fact that make_code_young doesn't do any
1145   // garbage collection which allows us to save/restore the registers without
1146   // worrying about which of them contain pointers. We also don't build an
1147   // internal frame to make the code faster, since we shouldn't have to do stack
1148   // crawls in MakeCodeYoung. This seems a bit fragile.
1149 
1150   // The following registers must be saved and restored when calling through to
1151   // the runtime:
1152   //   r0 - contains return address (beginning of patch sequence)
1153   //   r1 - isolate
1154   //   r3 - new target
1155   FrameScope scope(masm, StackFrame::MANUAL);
1156   __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit() | fp.bit() | lr.bit());
1157   __ PrepareCallCFunction(2, 0, r2);
1158   __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
1159   __ CallCFunction(
1160       ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1161   __ ldm(ia_w, sp, r0.bit() | r1.bit() | r3.bit() | fp.bit() | lr.bit());
1162   __ mov(pc, r0);
1163 }
1164 
1165 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C)                 \
1166 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking(  \
1167     MacroAssembler* masm) {                                  \
1168   GenerateMakeCodeYoungAgainCommon(masm);                    \
1169 }                                                            \
1170 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking(   \
1171     MacroAssembler* masm) {                                  \
1172   GenerateMakeCodeYoungAgainCommon(masm);                    \
1173 }
CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)1174 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1175 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1176 
1177 
1178 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1179   // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1180   // that make_code_young doesn't do any garbage collection which allows us to
1181   // save/restore the registers without worrying about which of them contain
1182   // pointers.
1183 
1184   // The following registers must be saved and restored when calling through to
1185   // the runtime:
1186   //   r0 - contains return address (beginning of patch sequence)
1187   //   r1 - isolate
1188   //   r3 - new target
1189   FrameScope scope(masm, StackFrame::MANUAL);
1190   __ stm(db_w, sp, r0.bit() | r1.bit() | r3.bit() | fp.bit() | lr.bit());
1191   __ PrepareCallCFunction(2, 0, r2);
1192   __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
1193   __ CallCFunction(ExternalReference::get_mark_code_as_executed_function(
1194         masm->isolate()), 2);
1195   __ ldm(ia_w, sp, r0.bit() | r1.bit() | r3.bit() | fp.bit() | lr.bit());
1196 
1197   // Perform prologue operations usually performed by the young code stub.
1198   __ PushFixedFrame(r1);
1199   __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1200 
1201   // Jump to point after the code-age stub.
1202   __ add(r0, r0, Operand(kNoCodeAgeSequenceLength));
1203   __ mov(pc, r0);
1204 }
1205 
1206 
Generate_MarkCodeAsExecutedTwice(MacroAssembler * masm)1207 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1208   GenerateMakeCodeYoungAgainCommon(masm);
1209 }
1210 
1211 
Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler * masm)1212 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1213   Generate_MarkCodeAsExecutedOnce(masm);
1214 }
1215 
1216 
Generate_NotifyStubFailureHelper(MacroAssembler * masm,SaveFPRegsMode save_doubles)1217 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1218                                              SaveFPRegsMode save_doubles) {
1219   {
1220     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1221 
1222     // Preserve registers across notification, this is important for compiled
1223     // stubs that tail call the runtime on deopts passing their parameters in
1224     // registers.
1225     __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved);
1226     // Pass the function and deoptimization type to the runtime system.
1227     __ CallRuntime(Runtime::kNotifyStubFailure, save_doubles);
1228     __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved);
1229   }
1230 
1231   __ add(sp, sp, Operand(kPointerSize));  // Ignore state
1232   __ mov(pc, lr);  // Jump to miss handler
1233 }
1234 
1235 
Generate_NotifyStubFailure(MacroAssembler * masm)1236 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1237   Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1238 }
1239 
1240 
Generate_NotifyStubFailureSaveDoubles(MacroAssembler * masm)1241 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1242   Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1243 }
1244 
1245 
Generate_NotifyDeoptimizedHelper(MacroAssembler * masm,Deoptimizer::BailoutType type)1246 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1247                                              Deoptimizer::BailoutType type) {
1248   {
1249     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1250     // Pass the function and deoptimization type to the runtime system.
1251     __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
1252     __ push(r0);
1253     __ CallRuntime(Runtime::kNotifyDeoptimized);
1254   }
1255 
1256   // Get the full codegen state from the stack and untag it -> r6.
1257   __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
1258   __ SmiUntag(r6);
1259   // Switch on the state.
1260   Label with_tos_register, unknown_state;
1261   __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
1262   __ b(ne, &with_tos_register);
1263   __ add(sp, sp, Operand(1 * kPointerSize));  // Remove state.
1264   __ Ret();
1265 
1266   __ bind(&with_tos_register);
1267   __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
1268   __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
1269   __ b(ne, &unknown_state);
1270   __ add(sp, sp, Operand(2 * kPointerSize));  // Remove state.
1271   __ Ret();
1272 
1273   __ bind(&unknown_state);
1274   __ stop("no cases left");
1275 }
1276 
1277 
Generate_NotifyDeoptimized(MacroAssembler * masm)1278 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1279   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1280 }
1281 
1282 
Generate_NotifySoftDeoptimized(MacroAssembler * masm)1283 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1284   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1285 }
1286 
1287 
Generate_NotifyLazyDeoptimized(MacroAssembler * masm)1288 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1289   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1290 }
1291 
1292 
CompatibleReceiverCheck(MacroAssembler * masm,Register receiver,Register function_template_info,Register scratch0,Register scratch1,Register scratch2,Label * receiver_check_failed)1293 static void CompatibleReceiverCheck(MacroAssembler* masm, Register receiver,
1294                                     Register function_template_info,
1295                                     Register scratch0, Register scratch1,
1296                                     Register scratch2,
1297                                     Label* receiver_check_failed) {
1298   Register signature = scratch0;
1299   Register map = scratch1;
1300   Register constructor = scratch2;
1301 
1302   // If there is no signature, return the holder.
1303   __ ldr(signature, FieldMemOperand(function_template_info,
1304                                     FunctionTemplateInfo::kSignatureOffset));
1305   __ CompareRoot(signature, Heap::kUndefinedValueRootIndex);
1306   Label receiver_check_passed;
1307   __ b(eq, &receiver_check_passed);
1308 
1309   // Walk the prototype chain.
1310   __ ldr(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
1311   Label prototype_loop_start;
1312   __ bind(&prototype_loop_start);
1313 
1314   // Get the constructor, if any.
1315   __ GetMapConstructor(constructor, map, ip, ip);
1316   __ cmp(ip, Operand(JS_FUNCTION_TYPE));
1317   Label next_prototype;
1318   __ b(ne, &next_prototype);
1319   Register type = constructor;
1320   __ ldr(type,
1321          FieldMemOperand(constructor, JSFunction::kSharedFunctionInfoOffset));
1322   __ ldr(type, FieldMemOperand(type, SharedFunctionInfo::kFunctionDataOffset));
1323 
1324   // Loop through the chain of inheriting function templates.
1325   Label function_template_loop;
1326   __ bind(&function_template_loop);
1327 
1328   // If the signatures match, we have a compatible receiver.
1329   __ cmp(signature, type);
1330   __ b(eq, &receiver_check_passed);
1331 
1332   // If the current type is not a FunctionTemplateInfo, load the next prototype
1333   // in the chain.
1334   __ JumpIfSmi(type, &next_prototype);
1335   __ CompareObjectType(type, ip, ip, FUNCTION_TEMPLATE_INFO_TYPE);
1336 
1337   // Otherwise load the parent function template and iterate.
1338   __ ldr(type,
1339          FieldMemOperand(type, FunctionTemplateInfo::kParentTemplateOffset),
1340          eq);
1341   __ b(&function_template_loop, eq);
1342 
1343   // Load the next prototype.
1344   __ bind(&next_prototype);
1345   __ ldr(receiver, FieldMemOperand(map, Map::kPrototypeOffset));
1346   // End if the prototype is null or not hidden.
1347   __ CompareRoot(receiver, Heap::kNullValueRootIndex);
1348   __ b(eq, receiver_check_failed);
1349   __ ldr(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
1350   __ ldr(ip, FieldMemOperand(map, Map::kBitField3Offset));
1351   __ tst(ip, Operand(Map::IsHiddenPrototype::kMask));
1352   __ b(eq, receiver_check_failed);
1353   // Iterate.
1354   __ b(&prototype_loop_start);
1355 
1356   __ bind(&receiver_check_passed);
1357 }
1358 
1359 
Generate_HandleFastApiCall(MacroAssembler * masm)1360 void Builtins::Generate_HandleFastApiCall(MacroAssembler* masm) {
1361   // ----------- S t a t e -------------
1362   //  -- r0                 : number of arguments excluding receiver
1363   //  -- r1                 : callee
1364   //  -- lr                 : return address
1365   //  -- sp[0]              : last argument
1366   //  -- ...
1367   //  -- sp[4 * (argc - 1)] : first argument
1368   //  -- sp[4 * argc]       : receiver
1369   // -----------------------------------
1370 
1371   // Load the FunctionTemplateInfo.
1372   __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1373   __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kFunctionDataOffset));
1374 
1375   // Do the compatible receiver check.
1376   Label receiver_check_failed;
1377   __ ldr(r2, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1378   CompatibleReceiverCheck(masm, r2, r3, r4, r5, r6, &receiver_check_failed);
1379 
1380   // Get the callback offset from the FunctionTemplateInfo, and jump to the
1381   // beginning of the code.
1382   __ ldr(r4, FieldMemOperand(r3, FunctionTemplateInfo::kCallCodeOffset));
1383   __ ldr(r4, FieldMemOperand(r4, CallHandlerInfo::kFastHandlerOffset));
1384   __ add(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
1385   __ Jump(r4);
1386 
1387   // Compatible receiver check failed: throw an Illegal Invocation exception.
1388   __ bind(&receiver_check_failed);
1389   // Drop the arguments (including the receiver)
1390   __ add(r0, r0, Operand(1));
1391   __ add(sp, sp, Operand(r0, LSL, kPointerSizeLog2));
1392   __ TailCallRuntime(Runtime::kThrowIllegalInvocation);
1393 }
1394 
1395 
Generate_OnStackReplacement(MacroAssembler * masm)1396 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1397   // Lookup the function in the JavaScript frame.
1398   __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1399   {
1400     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1401     // Pass function as argument.
1402     __ push(r0);
1403     __ CallRuntime(Runtime::kCompileForOnStackReplacement);
1404   }
1405 
1406   // If the code object is null, just return to the unoptimized code.
1407   Label skip;
1408   __ cmp(r0, Operand(Smi::FromInt(0)));
1409   __ b(ne, &skip);
1410   __ Ret();
1411 
1412   __ bind(&skip);
1413 
1414   // Load deoptimization data from the code object.
1415   // <deopt_data> = <code>[#deoptimization_data_offset]
1416   __ ldr(r1, FieldMemOperand(r0, Code::kDeoptimizationDataOffset));
1417 
1418   { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
1419     __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag));  // Code start
1420 
1421     if (FLAG_enable_embedded_constant_pool) {
1422       __ LoadConstantPoolPointerRegisterFromCodeTargetAddress(r0);
1423     }
1424 
1425     // Load the OSR entrypoint offset from the deoptimization data.
1426     // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1427     __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(
1428         DeoptimizationInputData::kOsrPcOffsetIndex)));
1429 
1430     // Compute the target address = code start + osr_offset
1431     __ add(lr, r0, Operand::SmiUntag(r1));
1432 
1433     // And "return" to the OSR entry point of the function.
1434     __ Ret();
1435   }
1436 }
1437 
1438 
Generate_OsrAfterStackCheck(MacroAssembler * masm)1439 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1440   // We check the stack limit as indicator that recompilation might be done.
1441   Label ok;
1442   __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1443   __ cmp(sp, Operand(ip));
1444   __ b(hs, &ok);
1445   {
1446     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1447     __ CallRuntime(Runtime::kStackGuard);
1448   }
1449   __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1450           RelocInfo::CODE_TARGET);
1451 
1452   __ bind(&ok);
1453   __ Ret();
1454 }
1455 
1456 
1457 // static
Generate_DatePrototype_GetField(MacroAssembler * masm,int field_index)1458 void Builtins::Generate_DatePrototype_GetField(MacroAssembler* masm,
1459                                                int field_index) {
1460   // ----------- S t a t e -------------
1461   //  -- lr    : return address
1462   //  -- sp[0] : receiver
1463   // -----------------------------------
1464 
1465   // 1. Pop receiver into r0 and check that it's actually a JSDate object.
1466   Label receiver_not_date;
1467   {
1468     __ Pop(r0);
1469     __ JumpIfSmi(r0, &receiver_not_date);
1470     __ CompareObjectType(r0, r1, r2, JS_DATE_TYPE);
1471     __ b(ne, &receiver_not_date);
1472   }
1473 
1474   // 2. Load the specified date field, falling back to the runtime as necessary.
1475   if (field_index == JSDate::kDateValue) {
1476     __ ldr(r0, FieldMemOperand(r0, JSDate::kValueOffset));
1477   } else {
1478     if (field_index < JSDate::kFirstUncachedField) {
1479       Label stamp_mismatch;
1480       __ mov(r1, Operand(ExternalReference::date_cache_stamp(masm->isolate())));
1481       __ ldr(r1, MemOperand(r1));
1482       __ ldr(ip, FieldMemOperand(r0, JSDate::kCacheStampOffset));
1483       __ cmp(r1, ip);
1484       __ b(ne, &stamp_mismatch);
1485       __ ldr(r0, FieldMemOperand(
1486                      r0, JSDate::kValueOffset + field_index * kPointerSize));
1487       __ Ret();
1488       __ bind(&stamp_mismatch);
1489     }
1490     FrameScope scope(masm, StackFrame::INTERNAL);
1491     __ PrepareCallCFunction(2, r1);
1492     __ mov(r1, Operand(Smi::FromInt(field_index)));
1493     __ CallCFunction(
1494         ExternalReference::get_date_field_function(masm->isolate()), 2);
1495   }
1496   __ Ret();
1497 
1498   // 3. Raise a TypeError if the receiver is not a date.
1499   __ bind(&receiver_not_date);
1500   __ TailCallRuntime(Runtime::kThrowNotDateError);
1501 }
1502 
1503 
1504 // static
Generate_FunctionPrototypeApply(MacroAssembler * masm)1505 void Builtins::Generate_FunctionPrototypeApply(MacroAssembler* masm) {
1506   // ----------- S t a t e -------------
1507   //  -- r0    : argc
1508   //  -- sp[0] : argArray
1509   //  -- sp[4] : thisArg
1510   //  -- sp[8] : receiver
1511   // -----------------------------------
1512 
1513   // 1. Load receiver into r1, argArray into r0 (if present), remove all
1514   // arguments from the stack (including the receiver), and push thisArg (if
1515   // present) instead.
1516   {
1517     __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1518     __ mov(r3, r2);
1519     __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));  // receiver
1520     __ sub(r4, r0, Operand(1), SetCC);
1521     __ ldr(r2, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // thisArg
1522     __ sub(r4, r4, Operand(1), SetCC, ge);
1523     __ ldr(r3, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // argArray
1524     __ add(sp, sp, Operand(r0, LSL, kPointerSizeLog2));
1525     __ str(r2, MemOperand(sp, 0));
1526     __ mov(r0, r3);
1527   }
1528 
1529   // ----------- S t a t e -------------
1530   //  -- r0    : argArray
1531   //  -- r1    : receiver
1532   //  -- sp[0] : thisArg
1533   // -----------------------------------
1534 
1535   // 2. Make sure the receiver is actually callable.
1536   Label receiver_not_callable;
1537   __ JumpIfSmi(r1, &receiver_not_callable);
1538   __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
1539   __ ldrb(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
1540   __ tst(r4, Operand(1 << Map::kIsCallable));
1541   __ b(eq, &receiver_not_callable);
1542 
1543   // 3. Tail call with no arguments if argArray is null or undefined.
1544   Label no_arguments;
1545   __ JumpIfRoot(r0, Heap::kNullValueRootIndex, &no_arguments);
1546   __ JumpIfRoot(r0, Heap::kUndefinedValueRootIndex, &no_arguments);
1547 
1548   // 4a. Apply the receiver to the given argArray (passing undefined for
1549   // new.target).
1550   __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1551   __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1552 
1553   // 4b. The argArray is either null or undefined, so we tail call without any
1554   // arguments to the receiver.
1555   __ bind(&no_arguments);
1556   {
1557     __ mov(r0, Operand(0));
1558     __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1559   }
1560 
1561   // 4c. The receiver is not callable, throw an appropriate TypeError.
1562   __ bind(&receiver_not_callable);
1563   {
1564     __ str(r1, MemOperand(sp, 0));
1565     __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1566   }
1567 }
1568 
1569 
1570 // static
Generate_FunctionPrototypeCall(MacroAssembler * masm)1571 void Builtins::Generate_FunctionPrototypeCall(MacroAssembler* masm) {
1572   // 1. Make sure we have at least one argument.
1573   // r0: actual number of arguments
1574   {
1575     Label done;
1576     __ cmp(r0, Operand::Zero());
1577     __ b(ne, &done);
1578     __ PushRoot(Heap::kUndefinedValueRootIndex);
1579     __ add(r0, r0, Operand(1));
1580     __ bind(&done);
1581   }
1582 
1583   // 2. Get the callable to call (passed as receiver) from the stack.
1584   // r0: actual number of arguments
1585   __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1586 
1587   // 3. Shift arguments and return address one slot down on the stack
1588   //    (overwriting the original receiver).  Adjust argument count to make
1589   //    the original first argument the new receiver.
1590   // r0: actual number of arguments
1591   // r1: callable
1592   {
1593     Label loop;
1594     // Calculate the copy start address (destination). Copy end address is sp.
1595     __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1596 
1597     __ bind(&loop);
1598     __ ldr(ip, MemOperand(r2, -kPointerSize));
1599     __ str(ip, MemOperand(r2));
1600     __ sub(r2, r2, Operand(kPointerSize));
1601     __ cmp(r2, sp);
1602     __ b(ne, &loop);
1603     // Adjust the actual number of arguments and remove the top element
1604     // (which is a copy of the last argument).
1605     __ sub(r0, r0, Operand(1));
1606     __ pop();
1607   }
1608 
1609   // 4. Call the callable.
1610   __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1611 }
1612 
1613 
Generate_ReflectApply(MacroAssembler * masm)1614 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1615   // ----------- S t a t e -------------
1616   //  -- r0     : argc
1617   //  -- sp[0]  : argumentsList
1618   //  -- sp[4]  : thisArgument
1619   //  -- sp[8]  : target
1620   //  -- sp[12] : receiver
1621   // -----------------------------------
1622 
1623   // 1. Load target into r1 (if present), argumentsList into r0 (if present),
1624   // remove all arguments from the stack (including the receiver), and push
1625   // thisArgument (if present) instead.
1626   {
1627     __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1628     __ mov(r2, r1);
1629     __ mov(r3, r1);
1630     __ sub(r4, r0, Operand(1), SetCC);
1631     __ ldr(r1, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // target
1632     __ sub(r4, r4, Operand(1), SetCC, ge);
1633     __ ldr(r2, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // thisArgument
1634     __ sub(r4, r4, Operand(1), SetCC, ge);
1635     __ ldr(r3, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // argumentsList
1636     __ add(sp, sp, Operand(r0, LSL, kPointerSizeLog2));
1637     __ str(r2, MemOperand(sp, 0));
1638     __ mov(r0, r3);
1639   }
1640 
1641   // ----------- S t a t e -------------
1642   //  -- r0    : argumentsList
1643   //  -- r1    : target
1644   //  -- sp[0] : thisArgument
1645   // -----------------------------------
1646 
1647   // 2. Make sure the target is actually callable.
1648   Label target_not_callable;
1649   __ JumpIfSmi(r1, &target_not_callable);
1650   __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
1651   __ ldrb(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
1652   __ tst(r4, Operand(1 << Map::kIsCallable));
1653   __ b(eq, &target_not_callable);
1654 
1655   // 3a. Apply the target to the given argumentsList (passing undefined for
1656   // new.target).
1657   __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1658   __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1659 
1660   // 3b. The target is not callable, throw an appropriate TypeError.
1661   __ bind(&target_not_callable);
1662   {
1663     __ str(r1, MemOperand(sp, 0));
1664     __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1665   }
1666 }
1667 
1668 
Generate_ReflectConstruct(MacroAssembler * masm)1669 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1670   // ----------- S t a t e -------------
1671   //  -- r0     : argc
1672   //  -- sp[0]  : new.target (optional)
1673   //  -- sp[4]  : argumentsList
1674   //  -- sp[8]  : target
1675   //  -- sp[12] : receiver
1676   // -----------------------------------
1677 
1678   // 1. Load target into r1 (if present), argumentsList into r0 (if present),
1679   // new.target into r3 (if present, otherwise use target), remove all
1680   // arguments from the stack (including the receiver), and push thisArgument
1681   // (if present) instead.
1682   {
1683     __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1684     __ mov(r2, r1);
1685     __ str(r2, MemOperand(sp, r0, LSL, kPointerSizeLog2));  // receiver
1686     __ sub(r4, r0, Operand(1), SetCC);
1687     __ ldr(r1, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // target
1688     __ mov(r3, r1);  // new.target defaults to target
1689     __ sub(r4, r4, Operand(1), SetCC, ge);
1690     __ ldr(r2, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // argumentsList
1691     __ sub(r4, r4, Operand(1), SetCC, ge);
1692     __ ldr(r3, MemOperand(sp, r4, LSL, kPointerSizeLog2), ge);  // new.target
1693     __ add(sp, sp, Operand(r0, LSL, kPointerSizeLog2));
1694     __ mov(r0, r2);
1695   }
1696 
1697   // ----------- S t a t e -------------
1698   //  -- r0    : argumentsList
1699   //  -- r3    : new.target
1700   //  -- r1    : target
1701   //  -- sp[0] : receiver (undefined)
1702   // -----------------------------------
1703 
1704   // 2. Make sure the target is actually a constructor.
1705   Label target_not_constructor;
1706   __ JumpIfSmi(r1, &target_not_constructor);
1707   __ ldr(r4, FieldMemOperand(r1, HeapObject::kMapOffset));
1708   __ ldrb(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
1709   __ tst(r4, Operand(1 << Map::kIsConstructor));
1710   __ b(eq, &target_not_constructor);
1711 
1712   // 3. Make sure the target is actually a constructor.
1713   Label new_target_not_constructor;
1714   __ JumpIfSmi(r3, &new_target_not_constructor);
1715   __ ldr(r4, FieldMemOperand(r3, HeapObject::kMapOffset));
1716   __ ldrb(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
1717   __ tst(r4, Operand(1 << Map::kIsConstructor));
1718   __ b(eq, &new_target_not_constructor);
1719 
1720   // 4a. Construct the target with the given new.target and argumentsList.
1721   __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1722 
1723   // 4b. The target is not a constructor, throw an appropriate TypeError.
1724   __ bind(&target_not_constructor);
1725   {
1726     __ str(r1, MemOperand(sp, 0));
1727     __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1728   }
1729 
1730   // 4c. The new.target is not a constructor, throw an appropriate TypeError.
1731   __ bind(&new_target_not_constructor);
1732   {
1733     __ str(r3, MemOperand(sp, 0));
1734     __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1735   }
1736 }
1737 
1738 
ArgumentAdaptorStackCheck(MacroAssembler * masm,Label * stack_overflow)1739 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1740                                       Label* stack_overflow) {
1741   // ----------- S t a t e -------------
1742   //  -- r0 : actual number of arguments
1743   //  -- r1 : function (passed through to callee)
1744   //  -- r2 : expected number of arguments
1745   //  -- r3 : new target (passed through to callee)
1746   // -----------------------------------
1747   // Check the stack for overflow. We are not trying to catch
1748   // interruptions (e.g. debug break and preemption) here, so the "real stack
1749   // limit" is checked.
1750   __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
1751   // Make r5 the space we have left. The stack might already be overflowed
1752   // here which will cause r5 to become negative.
1753   __ sub(r5, sp, r5);
1754   // Check if the arguments will overflow the stack.
1755   __ cmp(r5, Operand(r2, LSL, kPointerSizeLog2));
1756   __ b(le, stack_overflow);  // Signed comparison.
1757 }
1758 
1759 
EnterArgumentsAdaptorFrame(MacroAssembler * masm)1760 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1761   __ SmiTag(r0);
1762   __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1763   __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() |
1764                        (FLAG_enable_embedded_constant_pool ? pp.bit() : 0) |
1765                        fp.bit() | lr.bit());
1766   __ add(fp, sp,
1767          Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1768 }
1769 
1770 
LeaveArgumentsAdaptorFrame(MacroAssembler * masm)1771 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1772   // ----------- S t a t e -------------
1773   //  -- r0 : result being passed through
1774   // -----------------------------------
1775   // Get the number of arguments passed (as a smi), tear down the frame and
1776   // then tear down the parameters.
1777   __ ldr(r1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1778                               kPointerSize)));
1779 
1780   __ LeaveFrame(StackFrame::ARGUMENTS_ADAPTOR);
1781   __ add(sp, sp, Operand::PointerOffsetFromSmiKey(r1));
1782   __ add(sp, sp, Operand(kPointerSize));  // adjust for receiver
1783 }
1784 
1785 
1786 // static
Generate_Apply(MacroAssembler * masm)1787 void Builtins::Generate_Apply(MacroAssembler* masm) {
1788   // ----------- S t a t e -------------
1789   //  -- r0    : argumentsList
1790   //  -- r1    : target
1791   //  -- r3    : new.target (checked to be constructor or undefined)
1792   //  -- sp[0] : thisArgument
1793   // -----------------------------------
1794 
1795   // Create the list of arguments from the array-like argumentsList.
1796   {
1797     Label create_arguments, create_array, create_runtime, done_create;
1798     __ JumpIfSmi(r0, &create_runtime);
1799 
1800     // Load the map of argumentsList into r2.
1801     __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
1802 
1803     // Load native context into r4.
1804     __ ldr(r4, NativeContextMemOperand());
1805 
1806     // Check if argumentsList is an (unmodified) arguments object.
1807     __ ldr(ip, ContextMemOperand(r4, Context::SLOPPY_ARGUMENTS_MAP_INDEX));
1808     __ cmp(ip, r2);
1809     __ b(eq, &create_arguments);
1810     __ ldr(ip, ContextMemOperand(r4, Context::STRICT_ARGUMENTS_MAP_INDEX));
1811     __ cmp(ip, r2);
1812     __ b(eq, &create_arguments);
1813 
1814     // Check if argumentsList is a fast JSArray.
1815     __ CompareInstanceType(r2, ip, JS_ARRAY_TYPE);
1816     __ b(eq, &create_array);
1817 
1818     // Ask the runtime to create the list (actually a FixedArray).
1819     __ bind(&create_runtime);
1820     {
1821       FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1822       __ Push(r1, r3, r0);
1823       __ CallRuntime(Runtime::kCreateListFromArrayLike);
1824       __ Pop(r1, r3);
1825       __ ldr(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
1826       __ SmiUntag(r2);
1827     }
1828     __ jmp(&done_create);
1829 
1830     // Try to create the list from an arguments object.
1831     __ bind(&create_arguments);
1832     __ ldr(r2,
1833            FieldMemOperand(r0, JSObject::kHeaderSize +
1834                                    Heap::kArgumentsLengthIndex * kPointerSize));
1835     __ ldr(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
1836     __ ldr(ip, FieldMemOperand(r4, FixedArray::kLengthOffset));
1837     __ cmp(r2, ip);
1838     __ b(ne, &create_runtime);
1839     __ SmiUntag(r2);
1840     __ mov(r0, r4);
1841     __ b(&done_create);
1842 
1843     // Try to create the list from a JSArray object.
1844     __ bind(&create_array);
1845     __ ldr(r2, FieldMemOperand(r2, Map::kBitField2Offset));
1846     __ DecodeField<Map::ElementsKindBits>(r2);
1847     STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
1848     STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
1849     STATIC_ASSERT(FAST_ELEMENTS == 2);
1850     __ cmp(r2, Operand(FAST_ELEMENTS));
1851     __ b(hi, &create_runtime);
1852     __ cmp(r2, Operand(FAST_HOLEY_SMI_ELEMENTS));
1853     __ b(eq, &create_runtime);
1854     __ ldr(r2, FieldMemOperand(r0, JSArray::kLengthOffset));
1855     __ ldr(r0, FieldMemOperand(r0, JSArray::kElementsOffset));
1856     __ SmiUntag(r2);
1857 
1858     __ bind(&done_create);
1859   }
1860 
1861   // Check for stack overflow.
1862   {
1863     // Check the stack for overflow. We are not trying to catch interruptions
1864     // (i.e. debug break and preemption) here, so check the "real stack limit".
1865     Label done;
1866     __ LoadRoot(ip, Heap::kRealStackLimitRootIndex);
1867     // Make ip the space we have left. The stack might already be overflowed
1868     // here which will cause ip to become negative.
1869     __ sub(ip, sp, ip);
1870     // Check if the arguments will overflow the stack.
1871     __ cmp(ip, Operand(r2, LSL, kPointerSizeLog2));
1872     __ b(gt, &done);  // Signed comparison.
1873     __ TailCallRuntime(Runtime::kThrowStackOverflow);
1874     __ bind(&done);
1875   }
1876 
1877   // ----------- S t a t e -------------
1878   //  -- r1    : target
1879   //  -- r0    : args (a FixedArray built from argumentsList)
1880   //  -- r2    : len (number of elements to push from args)
1881   //  -- r3    : new.target (checked to be constructor or undefined)
1882   //  -- sp[0] : thisArgument
1883   // -----------------------------------
1884 
1885   // Push arguments onto the stack (thisArgument is already on the stack).
1886   {
1887     __ mov(r4, Operand(0));
1888     Label done, loop;
1889     __ bind(&loop);
1890     __ cmp(r4, r2);
1891     __ b(eq, &done);
1892     __ add(ip, r0, Operand(r4, LSL, kPointerSizeLog2));
1893     __ ldr(ip, FieldMemOperand(ip, FixedArray::kHeaderSize));
1894     __ Push(ip);
1895     __ add(r4, r4, Operand(1));
1896     __ b(&loop);
1897     __ bind(&done);
1898     __ Move(r0, r4);
1899   }
1900 
1901   // Dispatch to Call or Construct depending on whether new.target is undefined.
1902   {
1903     __ CompareRoot(r3, Heap::kUndefinedValueRootIndex);
1904     __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET, eq);
1905     __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
1906   }
1907 }
1908 
1909 
1910 // static
Generate_CallFunction(MacroAssembler * masm,ConvertReceiverMode mode)1911 void Builtins::Generate_CallFunction(MacroAssembler* masm,
1912                                      ConvertReceiverMode mode) {
1913   // ----------- S t a t e -------------
1914   //  -- r0 : the number of arguments (not including the receiver)
1915   //  -- r1 : the function to call (checked to be a JSFunction)
1916   // -----------------------------------
1917   __ AssertFunction(r1);
1918 
1919   // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
1920   // Check that the function is not a "classConstructor".
1921   Label class_constructor;
1922   __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1923   __ ldrb(r3, FieldMemOperand(r2, SharedFunctionInfo::kFunctionKindByteOffset));
1924   __ tst(r3, Operand(SharedFunctionInfo::kClassConstructorBitsWithinByte));
1925   __ b(ne, &class_constructor);
1926 
1927   // Enter the context of the function; ToObject has to run in the function
1928   // context, and we also need to take the global proxy from the function
1929   // context in case of conversion.
1930   STATIC_ASSERT(SharedFunctionInfo::kNativeByteOffset ==
1931                 SharedFunctionInfo::kStrictModeByteOffset);
1932   __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1933   // We need to convert the receiver for non-native sloppy mode functions.
1934   Label done_convert;
1935   __ ldrb(r3, FieldMemOperand(r2, SharedFunctionInfo::kNativeByteOffset));
1936   __ tst(r3, Operand((1 << SharedFunctionInfo::kNativeBitWithinByte) |
1937                      (1 << SharedFunctionInfo::kStrictModeBitWithinByte)));
1938   __ b(ne, &done_convert);
1939   {
1940     // ----------- S t a t e -------------
1941     //  -- r0 : the number of arguments (not including the receiver)
1942     //  -- r1 : the function to call (checked to be a JSFunction)
1943     //  -- r2 : the shared function info.
1944     //  -- cp : the function context.
1945     // -----------------------------------
1946 
1947     if (mode == ConvertReceiverMode::kNullOrUndefined) {
1948       // Patch receiver to global proxy.
1949       __ LoadGlobalProxy(r3);
1950     } else {
1951       Label convert_to_object, convert_receiver;
1952       __ ldr(r3, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1953       __ JumpIfSmi(r3, &convert_to_object);
1954       STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
1955       __ CompareObjectType(r3, r4, r4, FIRST_JS_RECEIVER_TYPE);
1956       __ b(hs, &done_convert);
1957       if (mode != ConvertReceiverMode::kNotNullOrUndefined) {
1958         Label convert_global_proxy;
1959         __ JumpIfRoot(r3, Heap::kUndefinedValueRootIndex,
1960                       &convert_global_proxy);
1961         __ JumpIfNotRoot(r3, Heap::kNullValueRootIndex, &convert_to_object);
1962         __ bind(&convert_global_proxy);
1963         {
1964           // Patch receiver to global proxy.
1965           __ LoadGlobalProxy(r3);
1966         }
1967         __ b(&convert_receiver);
1968       }
1969       __ bind(&convert_to_object);
1970       {
1971         // Convert receiver using ToObject.
1972         // TODO(bmeurer): Inline the allocation here to avoid building the frame
1973         // in the fast case? (fall back to AllocateInNewSpace?)
1974         FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1975         __ SmiTag(r0);
1976         __ Push(r0, r1);
1977         __ mov(r0, r3);
1978         ToObjectStub stub(masm->isolate());
1979         __ CallStub(&stub);
1980         __ mov(r3, r0);
1981         __ Pop(r0, r1);
1982         __ SmiUntag(r0);
1983       }
1984       __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1985       __ bind(&convert_receiver);
1986     }
1987     __ str(r3, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1988   }
1989   __ bind(&done_convert);
1990 
1991   // ----------- S t a t e -------------
1992   //  -- r0 : the number of arguments (not including the receiver)
1993   //  -- r1 : the function to call (checked to be a JSFunction)
1994   //  -- r2 : the shared function info.
1995   //  -- cp : the function context.
1996   // -----------------------------------
1997 
1998   __ ldr(r2,
1999          FieldMemOperand(r2, SharedFunctionInfo::kFormalParameterCountOffset));
2000   __ SmiUntag(r2);
2001   ParameterCount actual(r0);
2002   ParameterCount expected(r2);
2003   __ InvokeFunctionCode(r1, no_reg, expected, actual, JUMP_FUNCTION,
2004                         CheckDebugStepCallWrapper());
2005 
2006   // The function is a "classConstructor", need to raise an exception.
2007   __ bind(&class_constructor);
2008   {
2009     FrameScope frame(masm, StackFrame::INTERNAL);
2010     __ push(r1);
2011     __ CallRuntime(Runtime::kThrowConstructorNonCallableError);
2012   }
2013 }
2014 
2015 
2016 namespace {
2017 
Generate_PushBoundArguments(MacroAssembler * masm)2018 void Generate_PushBoundArguments(MacroAssembler* masm) {
2019   // ----------- S t a t e -------------
2020   //  -- r0 : the number of arguments (not including the receiver)
2021   //  -- r1 : target (checked to be a JSBoundFunction)
2022   //  -- r3 : new.target (only in case of [[Construct]])
2023   // -----------------------------------
2024 
2025   // Load [[BoundArguments]] into r2 and length of that into r4.
2026   Label no_bound_arguments;
2027   __ ldr(r2, FieldMemOperand(r1, JSBoundFunction::kBoundArgumentsOffset));
2028   __ ldr(r4, FieldMemOperand(r2, FixedArray::kLengthOffset));
2029   __ SmiUntag(r4);
2030   __ cmp(r4, Operand(0));
2031   __ b(eq, &no_bound_arguments);
2032   {
2033     // ----------- S t a t e -------------
2034     //  -- r0 : the number of arguments (not including the receiver)
2035     //  -- r1 : target (checked to be a JSBoundFunction)
2036     //  -- r2 : the [[BoundArguments]] (implemented as FixedArray)
2037     //  -- r3 : new.target (only in case of [[Construct]])
2038     //  -- r4 : the number of [[BoundArguments]]
2039     // -----------------------------------
2040 
2041     // Reserve stack space for the [[BoundArguments]].
2042     {
2043       Label done;
2044       __ sub(sp, sp, Operand(r4, LSL, kPointerSizeLog2));
2045       // Check the stack for overflow. We are not trying to catch interruptions
2046       // (i.e. debug break and preemption) here, so check the "real stack
2047       // limit".
2048       __ CompareRoot(sp, Heap::kRealStackLimitRootIndex);
2049       __ b(gt, &done);  // Signed comparison.
2050       // Restore the stack pointer.
2051       __ add(sp, sp, Operand(r4, LSL, kPointerSizeLog2));
2052       {
2053         FrameScope scope(masm, StackFrame::MANUAL);
2054         __ EnterFrame(StackFrame::INTERNAL);
2055         __ CallRuntime(Runtime::kThrowStackOverflow);
2056       }
2057       __ bind(&done);
2058     }
2059 
2060     // Relocate arguments down the stack.
2061     {
2062       Label loop, done_loop;
2063       __ mov(r5, Operand(0));
2064       __ bind(&loop);
2065       __ cmp(r5, r0);
2066       __ b(gt, &done_loop);
2067       __ ldr(ip, MemOperand(sp, r4, LSL, kPointerSizeLog2));
2068       __ str(ip, MemOperand(sp, r5, LSL, kPointerSizeLog2));
2069       __ add(r4, r4, Operand(1));
2070       __ add(r5, r5, Operand(1));
2071       __ b(&loop);
2072       __ bind(&done_loop);
2073     }
2074 
2075     // Copy [[BoundArguments]] to the stack (below the arguments).
2076     {
2077       Label loop;
2078       __ ldr(r4, FieldMemOperand(r2, FixedArray::kLengthOffset));
2079       __ SmiUntag(r4);
2080       __ add(r2, r2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2081       __ bind(&loop);
2082       __ sub(r4, r4, Operand(1), SetCC);
2083       __ ldr(ip, MemOperand(r2, r4, LSL, kPointerSizeLog2));
2084       __ str(ip, MemOperand(sp, r0, LSL, kPointerSizeLog2));
2085       __ add(r0, r0, Operand(1));
2086       __ b(gt, &loop);
2087     }
2088   }
2089   __ bind(&no_bound_arguments);
2090 }
2091 
2092 }  // namespace
2093 
2094 
2095 // static
Generate_CallBoundFunction(MacroAssembler * masm)2096 void Builtins::Generate_CallBoundFunction(MacroAssembler* masm) {
2097   // ----------- S t a t e -------------
2098   //  -- r0 : the number of arguments (not including the receiver)
2099   //  -- r1 : the function to call (checked to be a JSBoundFunction)
2100   // -----------------------------------
2101   __ AssertBoundFunction(r1);
2102 
2103   // Patch the receiver to [[BoundThis]].
2104   __ ldr(ip, FieldMemOperand(r1, JSBoundFunction::kBoundThisOffset));
2105   __ str(ip, MemOperand(sp, r0, LSL, kPointerSizeLog2));
2106 
2107   // Push the [[BoundArguments]] onto the stack.
2108   Generate_PushBoundArguments(masm);
2109 
2110   // Call the [[BoundTargetFunction]] via the Call builtin.
2111   __ ldr(r1, FieldMemOperand(r1, JSBoundFunction::kBoundTargetFunctionOffset));
2112   __ mov(ip, Operand(ExternalReference(Builtins::kCall_ReceiverIsAny,
2113                                        masm->isolate())));
2114   __ ldr(ip, MemOperand(ip));
2115   __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
2116 }
2117 
2118 
2119 // static
Generate_Call(MacroAssembler * masm,ConvertReceiverMode mode)2120 void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
2121   // ----------- S t a t e -------------
2122   //  -- r0 : the number of arguments (not including the receiver)
2123   //  -- r1 : the target to call (can be any Object).
2124   // -----------------------------------
2125 
2126   Label non_callable, non_function, non_smi;
2127   __ JumpIfSmi(r1, &non_callable);
2128   __ bind(&non_smi);
2129   __ CompareObjectType(r1, r4, r5, JS_FUNCTION_TYPE);
2130   __ Jump(masm->isolate()->builtins()->CallFunction(mode),
2131           RelocInfo::CODE_TARGET, eq);
2132   __ cmp(r5, Operand(JS_BOUND_FUNCTION_TYPE));
2133   __ Jump(masm->isolate()->builtins()->CallBoundFunction(),
2134           RelocInfo::CODE_TARGET, eq);
2135   __ cmp(r5, Operand(JS_PROXY_TYPE));
2136   __ b(ne, &non_function);
2137 
2138   // 1. Runtime fallback for Proxy [[Call]].
2139   __ Push(r1);
2140   // Increase the arguments size to include the pushed function and the
2141   // existing receiver on the stack.
2142   __ add(r0, r0, Operand(2));
2143   // Tail-call to the runtime.
2144   __ JumpToExternalReference(
2145       ExternalReference(Runtime::kJSProxyCall, masm->isolate()));
2146 
2147   // 2. Call to something else, which might have a [[Call]] internal method (if
2148   // not we raise an exception).
2149   __ bind(&non_function);
2150   // Check if target has a [[Call]] internal method.
2151   __ ldrb(r4, FieldMemOperand(r4, Map::kBitFieldOffset));
2152   __ tst(r4, Operand(1 << Map::kIsCallable));
2153   __ b(eq, &non_callable);
2154   // Overwrite the original receiver the (original) target.
2155   __ str(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
2156   // Let the "call_as_function_delegate" take care of the rest.
2157   __ LoadNativeContextSlot(Context::CALL_AS_FUNCTION_DELEGATE_INDEX, r1);
2158   __ Jump(masm->isolate()->builtins()->CallFunction(
2159               ConvertReceiverMode::kNotNullOrUndefined),
2160           RelocInfo::CODE_TARGET);
2161 
2162   // 3. Call to something that is not callable.
2163   __ bind(&non_callable);
2164   {
2165     FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
2166     __ Push(r1);
2167     __ CallRuntime(Runtime::kThrowCalledNonCallable);
2168   }
2169 }
2170 
2171 
2172 // static
Generate_ConstructFunction(MacroAssembler * masm)2173 void Builtins::Generate_ConstructFunction(MacroAssembler* masm) {
2174   // ----------- S t a t e -------------
2175   //  -- r0 : the number of arguments (not including the receiver)
2176   //  -- r1 : the constructor to call (checked to be a JSFunction)
2177   //  -- r3 : the new target (checked to be a constructor)
2178   // -----------------------------------
2179   __ AssertFunction(r1);
2180 
2181   // Calling convention for function specific ConstructStubs require
2182   // r2 to contain either an AllocationSite or undefined.
2183   __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
2184 
2185   // Tail call to the function-specific construct stub (still in the caller
2186   // context at this point).
2187   __ ldr(r4, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
2188   __ ldr(r4, FieldMemOperand(r4, SharedFunctionInfo::kConstructStubOffset));
2189   __ add(pc, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
2190 }
2191 
2192 
2193 // static
Generate_ConstructBoundFunction(MacroAssembler * masm)2194 void Builtins::Generate_ConstructBoundFunction(MacroAssembler* masm) {
2195   // ----------- S t a t e -------------
2196   //  -- r0 : the number of arguments (not including the receiver)
2197   //  -- r1 : the function to call (checked to be a JSBoundFunction)
2198   //  -- r3 : the new target (checked to be a constructor)
2199   // -----------------------------------
2200   __ AssertBoundFunction(r1);
2201 
2202   // Push the [[BoundArguments]] onto the stack.
2203   Generate_PushBoundArguments(masm);
2204 
2205   // Patch new.target to [[BoundTargetFunction]] if new.target equals target.
2206   __ cmp(r1, r3);
2207   __ ldr(r3, FieldMemOperand(r1, JSBoundFunction::kBoundTargetFunctionOffset),
2208          eq);
2209 
2210   // Construct the [[BoundTargetFunction]] via the Construct builtin.
2211   __ ldr(r1, FieldMemOperand(r1, JSBoundFunction::kBoundTargetFunctionOffset));
2212   __ mov(ip, Operand(ExternalReference(Builtins::kConstruct, masm->isolate())));
2213   __ ldr(ip, MemOperand(ip));
2214   __ add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
2215 }
2216 
2217 
2218 // static
Generate_ConstructProxy(MacroAssembler * masm)2219 void Builtins::Generate_ConstructProxy(MacroAssembler* masm) {
2220   // ----------- S t a t e -------------
2221   //  -- r0 : the number of arguments (not including the receiver)
2222   //  -- r1 : the constructor to call (checked to be a JSProxy)
2223   //  -- r3 : the new target (either the same as the constructor or
2224   //          the JSFunction on which new was invoked initially)
2225   // -----------------------------------
2226 
2227   // Call into the Runtime for Proxy [[Construct]].
2228   __ Push(r1);
2229   __ Push(r3);
2230   // Include the pushed new_target, constructor and the receiver.
2231   __ add(r0, r0, Operand(3));
2232   // Tail-call to the runtime.
2233   __ JumpToExternalReference(
2234       ExternalReference(Runtime::kJSProxyConstruct, masm->isolate()));
2235 }
2236 
2237 
2238 // static
Generate_Construct(MacroAssembler * masm)2239 void Builtins::Generate_Construct(MacroAssembler* masm) {
2240   // ----------- S t a t e -------------
2241   //  -- r0 : the number of arguments (not including the receiver)
2242   //  -- r1 : the constructor to call (can be any Object)
2243   //  -- r3 : the new target (either the same as the constructor or
2244   //          the JSFunction on which new was invoked initially)
2245   // -----------------------------------
2246 
2247   // Check if target is a Smi.
2248   Label non_constructor;
2249   __ JumpIfSmi(r1, &non_constructor);
2250 
2251   // Dispatch based on instance type.
2252   __ CompareObjectType(r1, r4, r5, JS_FUNCTION_TYPE);
2253   __ Jump(masm->isolate()->builtins()->ConstructFunction(),
2254           RelocInfo::CODE_TARGET, eq);
2255 
2256   // Check if target has a [[Construct]] internal method.
2257   __ ldrb(r2, FieldMemOperand(r4, Map::kBitFieldOffset));
2258   __ tst(r2, Operand(1 << Map::kIsConstructor));
2259   __ b(eq, &non_constructor);
2260 
2261   // Only dispatch to bound functions after checking whether they are
2262   // constructors.
2263   __ cmp(r5, Operand(JS_BOUND_FUNCTION_TYPE));
2264   __ Jump(masm->isolate()->builtins()->ConstructBoundFunction(),
2265           RelocInfo::CODE_TARGET, eq);
2266 
2267   // Only dispatch to proxies after checking whether they are constructors.
2268   __ cmp(r5, Operand(JS_PROXY_TYPE));
2269   __ Jump(masm->isolate()->builtins()->ConstructProxy(), RelocInfo::CODE_TARGET,
2270           eq);
2271 
2272   // Called Construct on an exotic Object with a [[Construct]] internal method.
2273   {
2274     // Overwrite the original receiver with the (original) target.
2275     __ str(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
2276     // Let the "call_as_constructor_delegate" take care of the rest.
2277     __ LoadNativeContextSlot(Context::CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, r1);
2278     __ Jump(masm->isolate()->builtins()->CallFunction(),
2279             RelocInfo::CODE_TARGET);
2280   }
2281 
2282   // Called Construct on an Object that doesn't have a [[Construct]] internal
2283   // method.
2284   __ bind(&non_constructor);
2285   __ Jump(masm->isolate()->builtins()->ConstructedNonConstructable(),
2286           RelocInfo::CODE_TARGET);
2287 }
2288 
2289 
Generate_ArgumentsAdaptorTrampoline(MacroAssembler * masm)2290 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
2291   // ----------- S t a t e -------------
2292   //  -- r0 : actual number of arguments
2293   //  -- r1 : function (passed through to callee)
2294   //  -- r2 : expected number of arguments
2295   //  -- r3 : new target (passed through to callee)
2296   // -----------------------------------
2297 
2298   Label invoke, dont_adapt_arguments, stack_overflow;
2299 
2300   Label enough, too_few;
2301   __ cmp(r0, r2);
2302   __ b(lt, &too_few);
2303   __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
2304   __ b(eq, &dont_adapt_arguments);
2305 
2306   {  // Enough parameters: actual >= expected
2307     __ bind(&enough);
2308     EnterArgumentsAdaptorFrame(masm);
2309     ArgumentAdaptorStackCheck(masm, &stack_overflow);
2310 
2311     // Calculate copy start address into r0 and copy end address into r4.
2312     // r0: actual number of arguments as a smi
2313     // r1: function
2314     // r2: expected number of arguments
2315     // r3: new target (passed through to callee)
2316     __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
2317     // adjust for return address and receiver
2318     __ add(r0, r0, Operand(2 * kPointerSize));
2319     __ sub(r4, r0, Operand(r2, LSL, kPointerSizeLog2));
2320 
2321     // Copy the arguments (including the receiver) to the new stack frame.
2322     // r0: copy start address
2323     // r1: function
2324     // r2: expected number of arguments
2325     // r3: new target (passed through to callee)
2326     // r4: copy end address
2327 
2328     Label copy;
2329     __ bind(&copy);
2330     __ ldr(ip, MemOperand(r0, 0));
2331     __ push(ip);
2332     __ cmp(r0, r4);  // Compare before moving to next argument.
2333     __ sub(r0, r0, Operand(kPointerSize));
2334     __ b(ne, &copy);
2335 
2336     __ b(&invoke);
2337   }
2338 
2339   {  // Too few parameters: Actual < expected
2340     __ bind(&too_few);
2341 
2342     // If the function is strong we need to throw an error.
2343     Label no_strong_error;
2344     __ ldr(r4, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
2345     __ ldr(r5, FieldMemOperand(r4, SharedFunctionInfo::kCompilerHintsOffset));
2346     __ tst(r5, Operand(1 << (SharedFunctionInfo::kStrongModeFunction +
2347                              kSmiTagSize)));
2348     __ b(eq, &no_strong_error);
2349 
2350     // What we really care about is the required number of arguments.
2351     __ ldr(r4, FieldMemOperand(r4, SharedFunctionInfo::kLengthOffset));
2352     __ cmp(r0, Operand::SmiUntag(r4));
2353     __ b(ge, &no_strong_error);
2354 
2355     {
2356       FrameScope frame(masm, StackFrame::MANUAL);
2357       EnterArgumentsAdaptorFrame(masm);
2358       __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments);
2359     }
2360 
2361     __ bind(&no_strong_error);
2362     EnterArgumentsAdaptorFrame(masm);
2363     ArgumentAdaptorStackCheck(masm, &stack_overflow);
2364 
2365     // Calculate copy start address into r0 and copy end address is fp.
2366     // r0: actual number of arguments as a smi
2367     // r1: function
2368     // r2: expected number of arguments
2369     // r3: new target (passed through to callee)
2370     __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
2371 
2372     // Copy the arguments (including the receiver) to the new stack frame.
2373     // r0: copy start address
2374     // r1: function
2375     // r2: expected number of arguments
2376     // r3: new target (passed through to callee)
2377     Label copy;
2378     __ bind(&copy);
2379     // Adjust load for return address and receiver.
2380     __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
2381     __ push(ip);
2382     __ cmp(r0, fp);  // Compare before moving to next argument.
2383     __ sub(r0, r0, Operand(kPointerSize));
2384     __ b(ne, &copy);
2385 
2386     // Fill the remaining expected arguments with undefined.
2387     // r1: function
2388     // r2: expected number of arguments
2389     // r3: new target (passed through to callee)
2390     __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
2391     __ sub(r4, fp, Operand(r2, LSL, kPointerSizeLog2));
2392     // Adjust for frame.
2393     __ sub(r4, r4, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
2394                            2 * kPointerSize));
2395 
2396     Label fill;
2397     __ bind(&fill);
2398     __ push(ip);
2399     __ cmp(sp, r4);
2400     __ b(ne, &fill);
2401   }
2402 
2403   // Call the entry point.
2404   __ bind(&invoke);
2405   __ mov(r0, r2);
2406   // r0 : expected number of arguments
2407   // r1 : function (passed through to callee)
2408   // r3 : new target (passed through to callee)
2409   __ ldr(r4, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
2410   __ Call(r4);
2411 
2412   // Store offset of return address for deoptimizer.
2413   masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
2414 
2415   // Exit frame and return.
2416   LeaveArgumentsAdaptorFrame(masm);
2417   __ Jump(lr);
2418 
2419 
2420   // -------------------------------------------
2421   // Dont adapt arguments.
2422   // -------------------------------------------
2423   __ bind(&dont_adapt_arguments);
2424   __ ldr(r4, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
2425   __ Jump(r4);
2426 
2427   __ bind(&stack_overflow);
2428   {
2429     FrameScope frame(masm, StackFrame::MANUAL);
2430     __ CallRuntime(Runtime::kThrowStackOverflow);
2431     __ bkpt(0);
2432   }
2433 }
2434 
2435 
2436 #undef __
2437 
2438 }  // namespace internal
2439 }  // namespace v8
2440 
2441 #endif  // V8_TARGET_ARCH_ARM
2442