Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: src/a64/builtins-a64.cc

Issue 148293020: Merge experimental/a64 to bleeding_edge. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Remove ARM from OWNERS Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/a64/assembler-a64-inl.h ('k') | src/a64/code-stubs-a64.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #if V8_TARGET_ARCH_A64
31
32 #include "codegen.h"
33 #include "debug.h"
34 #include "deoptimizer.h"
35 #include "full-codegen.h"
36 #include "runtime.h"
37 #include "stub-cache.h"
38
39 namespace v8 {
40 namespace internal {
41
42
43 #define __ ACCESS_MASM(masm)
44
45
46 // Load the built-in Array function from the current context.
47 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
48 // Load the native context.
49 __ Ldr(result, GlobalObjectMemOperand());
50 __ Ldr(result,
51 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
52 // Load the InternalArray function from the native context.
53 __ Ldr(result,
54 MemOperand(result,
55 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
56 }
57
58
59 // Load the built-in InternalArray function from the current context.
60 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
61 Register result) {
62 // Load the native context.
63 __ Ldr(result, GlobalObjectMemOperand());
64 __ Ldr(result,
65 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
66 // Load the InternalArray function from the native context.
67 __ Ldr(result, ContextMemOperand(result,
68 Context::INTERNAL_ARRAY_FUNCTION_INDEX));
69 }
70
71
72 void Builtins::Generate_Adaptor(MacroAssembler* masm,
73 CFunctionId id,
74 BuiltinExtraArguments extra_args) {
75 // ----------- S t a t e -------------
76 // -- x0 : number of arguments excluding receiver
77 // -- x1 : called function (only guaranteed when
78 // extra_args requires it)
79 // -- cp : context
80 // -- sp[0] : last argument
81 // -- ...
82 // -- sp[4 * (argc - 1)] : first argument (argc == x0)
83 // -- sp[4 * argc] : receiver
84 // -----------------------------------
85
86 // Insert extra arguments.
87 int num_extra_args = 0;
88 if (extra_args == NEEDS_CALLED_FUNCTION) {
89 num_extra_args = 1;
90 __ Push(x1);
91 } else {
92 ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
93 }
94
95 // JumpToExternalReference expects x0 to contain the number of arguments
96 // including the receiver and the extra arguments.
97 __ Add(x0, x0, num_extra_args + 1);
98 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
99 }
100
101
102 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
103 // ----------- S t a t e -------------
104 // -- x0 : number of arguments
105 // -- lr : return address
106 // -- sp[...]: constructor arguments
107 // -----------------------------------
108 ASM_LOCATION("Builtins::Generate_InternalArrayCode");
109 Label generic_array_code;
110
111 // Get the InternalArray function.
112 GenerateLoadInternalArrayFunction(masm, x1);
113
114 if (FLAG_debug_code) {
115 // Initial map for the builtin InternalArray functions should be maps.
116 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kPrototypeOrInitialMapOffset));
117 __ Tst(x10, kSmiTagMask);
118 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
119 __ CompareObjectType(x10, x11, x12, MAP_TYPE);
120 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
121 }
122
123 // Run the native code for the InternalArray function called as a normal
124 // function.
125 InternalArrayConstructorStub stub(masm->isolate());
126 __ TailCallStub(&stub);
127 }
128
129
130 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
131 // ----------- S t a t e -------------
132 // -- x0 : number of arguments
133 // -- lr : return address
134 // -- sp[...]: constructor arguments
135 // -----------------------------------
136 ASM_LOCATION("Builtins::Generate_ArrayCode");
137 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
138
139 // Get the Array function.
140 GenerateLoadArrayFunction(masm, x1);
141
142 if (FLAG_debug_code) {
143 // Initial map for the builtin Array functions should be maps.
144 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kPrototypeOrInitialMapOffset));
145 __ Tst(x10, kSmiTagMask);
146 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
147 __ CompareObjectType(x10, x11, x12, MAP_TYPE);
148 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
149 }
150
151 // Run the native code for the Array function called as a normal function.
152 Handle<Object> undefined_sentinel(
153 masm->isolate()->heap()->undefined_value(),
154 masm->isolate());
155 __ Mov(x2, Operand(undefined_sentinel));
156 ArrayConstructorStub stub(masm->isolate());
157 __ TailCallStub(&stub);
158 }
159
160
161 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
162 // ----------- S t a t e -------------
163 // -- x0 : number of arguments
164 // -- x1 : constructor function
165 // -- lr : return address
166 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
167 // -- sp[argc * 8] : receiver
168 // -----------------------------------
169 ASM_LOCATION("Builtins::Generate_StringConstructCode");
170 Counters* counters = masm->isolate()->counters();
171 __ IncrementCounter(counters->string_ctor_calls(), 1, x10, x11);
172
173 Register argc = x0;
174 Register function = x1;
175 if (FLAG_debug_code) {
176 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, x10);
177 __ Cmp(function, x10);
178 __ Assert(eq, kUnexpectedStringFunction);
179 }
180
181 // Load the first arguments in x0 and get rid of the rest.
182 Label no_arguments;
183 __ Cbz(argc, &no_arguments);
184 // First args = sp[(argc - 1) * 8].
185 __ Sub(argc, argc, 1);
186 __ Claim(argc, kXRegSizeInBytes);
187 // jssp now point to args[0], load and drop args[0] + receiver.
188 // TODO(jbramley): Consider adding ClaimAndPoke.
189 __ Ldr(argc, MemOperand(jssp, 2 * kPointerSize, PostIndex));
190
191 Register argument = x2;
192 Label not_cached, argument_is_string;
193 __ LookupNumberStringCache(argc, // Input.
194 argument, // Result.
195 x10, // Scratch.
196 x11, // Scratch.
197 x12, // Scratch.
198 &not_cached);
199 __ IncrementCounter(counters->string_ctor_cached_number(), 1, x10, x11);
200 __ Bind(&argument_is_string);
201
202 // ----------- S t a t e -------------
203 // -- x2 : argument converted to string
204 // -- x1 : constructor function
205 // -- lr : return address
206 // -----------------------------------
207
208 Label gc_required;
209 Register new_obj = x0;
210 __ Allocate(JSValue::kSize, new_obj, x10, x11, &gc_required, TAG_OBJECT);
211
212 // Initialize the String object.
213 Register map = x3;
214 __ LoadGlobalFunctionInitialMap(function, map, x10);
215 if (FLAG_debug_code) {
216 __ Ldrb(x4, FieldMemOperand(map, Map::kInstanceSizeOffset));
217 __ Cmp(x4, JSValue::kSize >> kPointerSizeLog2);
218 __ Assert(eq, kUnexpectedStringWrapperInstanceSize);
219 __ Ldrb(x4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
220 __ Cmp(x4, 0);
221 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper);
222 }
223 __ Str(map, FieldMemOperand(new_obj, HeapObject::kMapOffset));
224
225 Register empty = x3;
226 __ LoadRoot(empty, Heap::kEmptyFixedArrayRootIndex);
227 __ Str(empty, FieldMemOperand(new_obj, JSObject::kPropertiesOffset));
228 __ Str(empty, FieldMemOperand(new_obj, JSObject::kElementsOffset));
229
230 __ Str(argument, FieldMemOperand(new_obj, JSValue::kValueOffset));
231
232 // Ensure the object is fully initialized.
233 STATIC_ASSERT(JSValue::kSize == (4 * kPointerSize));
234
235 __ Ret();
236
237 // The argument was not found in the number to string cache. Check
238 // if it's a string already before calling the conversion builtin.
239 Label convert_argument;
240 __ Bind(&not_cached);
241 __ JumpIfSmi(argc, &convert_argument);
242
243 // Is it a String?
244 __ Ldr(x10, FieldMemOperand(x0, HeapObject::kMapOffset));
245 __ Ldrb(x11, FieldMemOperand(x10, Map::kInstanceTypeOffset));
246 __ Tbnz(x11, MaskToBit(kIsNotStringMask), &convert_argument);
247 __ Mov(argument, argc);
248 __ IncrementCounter(counters->string_ctor_string_value(), 1, x10, x11);
249 __ B(&argument_is_string);
250
251 // Invoke the conversion builtin and put the result into x2.
252 __ Bind(&convert_argument);
253 __ Push(function); // Preserve the function.
254 __ IncrementCounter(counters->string_ctor_conversions(), 1, x10, x11);
255 {
256 FrameScope scope(masm, StackFrame::INTERNAL);
257 __ Push(argc);
258 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
259 }
260 __ Pop(function);
261 __ Mov(argument, x0);
262 __ B(&argument_is_string);
263
264 // Load the empty string into x2, remove the receiver from the
265 // stack, and jump back to the case where the argument is a string.
266 __ Bind(&no_arguments);
267 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
268 __ Drop(1);
269 __ B(&argument_is_string);
270
271 // At this point the argument is already a string. Call runtime to create a
272 // string wrapper.
273 __ Bind(&gc_required);
274 __ IncrementCounter(counters->string_ctor_gc_required(), 1, x10, x11);
275 {
276 FrameScope scope(masm, StackFrame::INTERNAL);
277 __ Push(argument);
278 __ CallRuntime(Runtime::kNewStringWrapper, 1);
279 }
280 __ Ret();
281 }
282
283
284 static void CallRuntimePassFunction(MacroAssembler* masm,
285 Runtime::FunctionId function_id) {
286 FrameScope scope(masm, StackFrame::INTERNAL);
287 // - Push a copy of the function onto the stack.
288 // - Push another copy as a parameter to the runtime call.
289 __ Push(x1, x1);
290
291 __ CallRuntime(function_id, 1);
292
293 // - Restore receiver.
294 __ Pop(x1);
295 }
296
297
298 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
299 __ Ldr(x2, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
300 __ Ldr(x2, FieldMemOperand(x2, SharedFunctionInfo::kCodeOffset));
301 __ Add(x2, x2, Code::kHeaderSize - kHeapObjectTag);
302 __ Br(x2);
303 }
304
305
306 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
307 __ Add(x0, x0, Code::kHeaderSize - kHeapObjectTag);
308 __ Br(x0);
309 }
310
311
312 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
313 // Checking whether the queued function is ready for install is optional,
314 // since we come across interrupts and stack checks elsewhere. However, not
315 // checking may delay installing ready functions, and always checking would be
316 // quite expensive. A good compromise is to first check against stack limit as
317 // a cue for an interrupt signal.
318 Label ok;
319 __ CompareRoot(masm->StackPointer(), Heap::kStackLimitRootIndex);
320 __ B(hs, &ok);
321
322 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
323 GenerateTailCallToReturnedCode(masm);
324
325 __ Bind(&ok);
326 GenerateTailCallToSharedCode(masm);
327 }
328
329
330 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
331 bool is_api_function,
332 bool count_constructions) {
333 // ----------- S t a t e -------------
334 // -- x0 : number of arguments
335 // -- x1 : constructor function
336 // -- lr : return address
337 // -- sp[...]: constructor arguments
338 // -----------------------------------
339
340 ASM_LOCATION("Builtins::Generate_JSConstructStubHelper");
341 // Should never count constructions for api objects.
342 ASSERT(!is_api_function || !count_constructions);
343
344 Isolate* isolate = masm->isolate();
345
346 // Enter a construct frame.
347 {
348 FrameScope scope(masm, StackFrame::CONSTRUCT);
349
350 // Preserve the two incoming parameters on the stack.
351 Register argc = x0;
352 Register constructor = x1;
353 // x1: constructor function
354 __ SmiTag(argc);
355 __ Push(argc, constructor);
356 // sp[0] : Constructor function.
357 // sp[1]: number of arguments (smi-tagged)
358
359 // Try to allocate the object without transitioning into C code. If any of
360 // the preconditions is not met, the code bails out to the runtime call.
361 Label rt_call, allocated;
362 if (FLAG_inline_new) {
363 Label undo_allocation;
364 #if ENABLE_DEBUGGER_SUPPORT
365 ExternalReference debug_step_in_fp =
366 ExternalReference::debug_step_in_fp_address(isolate);
367 __ Mov(x2, Operand(debug_step_in_fp));
368 __ Ldr(x2, MemOperand(x2));
369 __ Cbnz(x2, &rt_call);
370 #endif
371 // Load the initial map and verify that it is in fact a map.
372 Register init_map = x2;
373 __ Ldr(init_map,
374 FieldMemOperand(constructor,
375 JSFunction::kPrototypeOrInitialMapOffset));
376 __ JumpIfSmi(init_map, &rt_call);
377 __ JumpIfNotObjectType(init_map, x10, x11, MAP_TYPE, &rt_call);
378
379 // Check that the constructor is not constructing a JSFunction (see
380 // comments in Runtime_NewObject in runtime.cc). In which case the initial
381 // map's instance type would be JS_FUNCTION_TYPE.
382 __ CompareInstanceType(init_map, x10, JS_FUNCTION_TYPE);
383 __ B(eq, &rt_call);
384
385 if (count_constructions) {
386 Label allocate;
387 // Decrease generous allocation count.
388 __ Ldr(x3, FieldMemOperand(constructor,
389 JSFunction::kSharedFunctionInfoOffset));
390 MemOperand constructor_count =
391 FieldMemOperand(x3, SharedFunctionInfo::kConstructionCountOffset);
392 __ Ldrb(x4, constructor_count);
393 __ Subs(x4, x4, 1);
394 __ Strb(x4, constructor_count);
395 __ B(ne, &allocate);
396
397 // Push the constructor and map to the stack, and the constructor again
398 // as argument to the runtime call.
399 __ Push(constructor, init_map, constructor);
400 // The call will replace the stub, so the countdown is only done once.
401 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
402 __ Pop(init_map, constructor);
403 __ Bind(&allocate);
404 }
405
406 // Now allocate the JSObject on the heap.
407 Register obj_size = x3;
408 Register new_obj = x4;
409 __ Ldrb(obj_size, FieldMemOperand(init_map, Map::kInstanceSizeOffset));
410 __ Allocate(obj_size, new_obj, x10, x11, &rt_call, SIZE_IN_WORDS);
411
412 // Allocated the JSObject, now initialize the fields. Map is set to
413 // initial map and properties and elements are set to empty fixed array.
414 // NB. the object pointer is not tagged, so MemOperand is used.
415 Register empty = x5;
416 __ LoadRoot(empty, Heap::kEmptyFixedArrayRootIndex);
417 __ Str(init_map, MemOperand(new_obj, JSObject::kMapOffset));
418 __ Str(empty, MemOperand(new_obj, JSObject::kPropertiesOffset));
419 __ Str(empty, MemOperand(new_obj, JSObject::kElementsOffset));
420
421 Register first_prop = x5;
422 __ Add(first_prop, new_obj, JSObject::kHeaderSize);
423
424 // Fill all of the in-object properties with the appropriate filler.
425 Register obj_end = x6;
426 __ Add(obj_end, new_obj, Operand(obj_size, LSL, kPointerSizeLog2));
427 Register undef = x7;
428 __ LoadRoot(undef, Heap::kUndefinedValueRootIndex);
429
430 // Obtain number of pre-allocated property fields and in-object
431 // properties.
432 Register prealloc_fields = x10;
433 Register inobject_props = x11;
434 Register inst_sizes = x11;
435 __ Ldr(inst_sizes, FieldMemOperand(init_map, Map::kInstanceSizesOffset));
436 __ Ubfx(prealloc_fields, inst_sizes,
437 Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
438 kBitsPerByte);
439 __ Ubfx(inobject_props, inst_sizes,
440 Map::kInObjectPropertiesByte * kBitsPerByte, kBitsPerByte);
441
442 if (count_constructions) {
443 // Register first_non_prealloc is the offset of the first field after
444 // pre-allocated fields.
445 Register first_non_prealloc = x12;
446 __ Add(first_non_prealloc, first_prop,
447 Operand(prealloc_fields, LSL, kPointerSizeLog2));
448
449 if (FLAG_debug_code) {
450 __ Cmp(first_non_prealloc, obj_end);
451 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
452 }
453 __ InitializeFieldsWithFiller(first_prop, first_non_prealloc, undef);
454 // To allow for truncation.
455 __ LoadRoot(x12, Heap::kOnePointerFillerMapRootIndex);
456 __ InitializeFieldsWithFiller(first_prop, obj_end, x12);
457 } else {
458 __ InitializeFieldsWithFiller(first_prop, obj_end, undef);
459 }
460
461 // Add the object tag to make the JSObject real, so that we can continue
462 // and jump into the continuation code at any time from now on. Any
463 // failures need to undo the allocation, so that the heap is in a
464 // consistent state and verifiable.
465 __ Add(new_obj, new_obj, kHeapObjectTag);
466
467 // Check if a non-empty properties array is needed. Continue with
468 // allocated object if not, or fall through to runtime call if it is.
469 Register element_count = x3;
470 __ Ldrb(x3, FieldMemOperand(init_map, Map::kUnusedPropertyFieldsOffset));
471 // The field instance sizes contains both pre-allocated property fields
472 // and in-object properties.
473 __ Add(x3, x3, prealloc_fields);
474 __ Subs(element_count, x3, inobject_props);
475
476 // Done if no extra properties are to be allocated.
477 __ B(eq, &allocated);
478 __ Assert(pl, kPropertyAllocationCountFailed);
479
480 // Scale the number of elements by pointer size and add the header for
481 // FixedArrays to the start of the next object calculation from above.
482 Register new_array = x5;
483 Register array_size = x6;
484 __ Add(array_size, element_count, FixedArray::kHeaderSize / kPointerSize);
485 __ Allocate(array_size, new_array, x11, x12, &undo_allocation,
486 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP |
487 SIZE_IN_WORDS));
488
489 Register array_map = x10;
490 __ LoadRoot(array_map, Heap::kFixedArrayMapRootIndex);
491 __ Str(array_map, MemOperand(new_array, FixedArray::kMapOffset));
492 __ SmiTag(x0, element_count);
493 __ Str(x0, MemOperand(new_array, FixedArray::kLengthOffset));
494
495 // Initialize the fields to undefined.
496 Register elements = x10;
497 Register elements_end = x11;
498 __ Add(elements, new_array, FixedArray::kHeaderSize);
499 __ Add(elements_end, elements,
500 Operand(element_count, LSL, kPointerSizeLog2));
501 __ InitializeFieldsWithFiller(elements, elements_end, undef);
502
503 // Store the initialized FixedArray into the properties field of the
504 // JSObject.
505 __ Add(new_array, new_array, kHeapObjectTag);
506 __ Str(new_array, FieldMemOperand(new_obj, JSObject::kPropertiesOffset));
507
508 // Continue with JSObject being successfully allocated.
509 __ B(&allocated);
510
511 // Undo the setting of the new top so that the heap is verifiable. For
512 // example, the map's unused properties potentially do not match the
513 // allocated objects unused properties.
514 __ Bind(&undo_allocation);
515 __ UndoAllocationInNewSpace(new_obj, x14);
516 }
517
518 // Allocate the new receiver object using the runtime call.
519 __ Bind(&rt_call);
520 __ Push(constructor); // Argument for Runtime_NewObject.
521 __ CallRuntime(Runtime::kNewObject, 1);
522 __ Mov(x4, x0);
523
524 // Receiver for constructor call allocated.
525 // x4: JSObject
526 __ Bind(&allocated);
527 __ Push(x4, x4);
528
529 // Reload the number of arguments from the stack.
530 // Set it up in x0 for the function call below.
531 // jssp[0]: receiver
532 // jssp[1]: receiver
533 // jssp[2]: constructor function
534 // jssp[3]: number of arguments (smi-tagged)
535 __ Peek(constructor, 2 * kXRegSizeInBytes); // Load constructor.
536 __ Peek(argc, 3 * kXRegSizeInBytes); // Load number of arguments.
537 __ SmiUntag(argc);
538
539 // Set up pointer to last argument.
540 __ Add(x2, fp, StandardFrameConstants::kCallerSPOffset);
541
542 // Copy arguments and receiver to the expression stack.
543 // Copy 2 values every loop to use ldp/stp.
544 // x0: number of arguments
545 // x1: constructor function
546 // x2: address of last argument (caller sp)
547 // jssp[0]: receiver
548 // jssp[1]: receiver
549 // jssp[2]: constructor function
550 // jssp[3]: number of arguments (smi-tagged)
551 // Compute the start address of the copy in x3.
552 __ Add(x3, x2, Operand(argc, LSL, kPointerSizeLog2));
553 Label loop, entry, done_copying_arguments;
554 __ B(&entry);
555 __ Bind(&loop);
556 __ Ldp(x10, x11, MemOperand(x3, -2 * kPointerSize, PreIndex));
557 __ Push(x11, x10);
558 __ Bind(&entry);
559 __ Cmp(x3, x2);
560 __ B(gt, &loop);
561 // Because we copied values 2 by 2 we may have copied one extra value.
562 // Drop it if that is the case.
563 __ B(eq, &done_copying_arguments);
564 __ Drop(1);
565 __ Bind(&done_copying_arguments);
566
567 // Call the function.
568 // x0: number of arguments
569 // x1: constructor function
570 if (is_api_function) {
571 __ Ldr(cp, FieldMemOperand(constructor, JSFunction::kContextOffset));
572 Handle<Code> code =
573 masm->isolate()->builtins()->HandleApiCallConstruct();
574 __ Call(code, RelocInfo::CODE_TARGET);
575 } else {
576 ParameterCount actual(argc);
577 __ InvokeFunction(constructor, actual, CALL_FUNCTION, NullCallWrapper());
578 }
579
580 // Store offset of return address for deoptimizer.
581 if (!is_api_function && !count_constructions) {
582 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
583 }
584
585 // Restore the context from the frame.
586 // x0: result
587 // jssp[0]: receiver
588 // jssp[1]: constructor function
589 // jssp[2]: number of arguments (smi-tagged)
590 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
591
592 // If the result is an object (in the ECMA sense), we should get rid
593 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
594 // on page 74.
595 Label use_receiver, exit;
596
597 // If the result is a smi, it is *not* an object in the ECMA sense.
598 // x0: result
599 // jssp[0]: receiver (newly allocated object)
600 // jssp[1]: constructor function
601 // jssp[2]: number of arguments (smi-tagged)
602 __ JumpIfSmi(x0, &use_receiver);
603
604 // If the type of the result (stored in its map) is less than
605 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
606 __ JumpIfObjectType(x0, x1, x3, FIRST_SPEC_OBJECT_TYPE, &exit, ge);
607
608 // Throw away the result of the constructor invocation and use the
609 // on-stack receiver as the result.
610 __ Bind(&use_receiver);
611 __ Peek(x0, 0);
612
613 // Remove the receiver from the stack, remove caller arguments, and
614 // return.
615 __ Bind(&exit);
616 // x0: result
617 // jssp[0]: receiver (newly allocated object)
618 // jssp[1]: constructor function
619 // jssp[2]: number of arguments (smi-tagged)
620 __ Peek(x1, 2 * kXRegSizeInBytes);
621
622 // Leave construct frame.
623 }
624
625 __ DropBySMI(x1);
626 __ Drop(1);
627 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, x1, x2);
628 __ Ret();
629 }
630
631
632 void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) {
633 Generate_JSConstructStubHelper(masm, false, true);
634 }
635
636
637 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
638 Generate_JSConstructStubHelper(masm, false, false);
639 }
640
641
642 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
643 Generate_JSConstructStubHelper(masm, true, false);
644 }
645
646
647 // Input:
648 // x0: code entry.
649 // x1: function.
650 // x2: receiver.
651 // x3: argc.
652 // x4: argv.
653 // Output:
654 // x0: result.
655 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
656 bool is_construct) {
657 // Called from JSEntryStub::GenerateBody().
658 Register function = x1;
659 Register receiver = x2;
660 Register argc = x3;
661 Register argv = x4;
662
663 ProfileEntryHookStub::MaybeCallEntryHook(masm);
664
665 // Clear the context before we push it when entering the internal frame.
666 __ Mov(cp, 0);
667
668 {
669 // Enter an internal frame.
670 FrameScope scope(masm, StackFrame::INTERNAL);
671
672 // Set up the context from the function argument.
673 __ Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
674
675 __ InitializeRootRegister();
676
677 // Push the function and the receiver onto the stack.
678 __ Push(function, receiver);
679
680 // Copy arguments to the stack in a loop, in reverse order.
681 // x3: argc.
682 // x4: argv.
683 Label loop, entry;
684 // Compute the copy end address.
685 __ Add(x10, argv, Operand(argc, LSL, kPointerSizeLog2));
686
687 // TODO(all): This can potentially be optimized with ldp/stp to speed up
688 // arguments passing from C++ to JS.
689 __ B(&entry);
690 __ Bind(&loop);
691 __ Ldr(x11, MemOperand(argv, kPointerSize, PostIndex));
692 __ Ldr(x12, MemOperand(x11)); // Dereference the handle.
693 __ Push(x12); // Push the argument.
694 __ Bind(&entry);
695 __ Cmp(x10, argv);
696 __ B(ne, &loop);
697
698 // Initialize all JavaScript callee-saved registers, since they will be seen
699 // by the garbage collector as part of handlers.
700 // The original values have been saved in JSEntryStub::GenerateBody().
701 __ LoadRoot(x19, Heap::kUndefinedValueRootIndex);
702 __ Mov(x20, x19);
703 __ Mov(x21, x19);
704 __ Mov(x22, x19);
705 __ Mov(x23, x19);
706 __ Mov(x24, x19);
707 __ Mov(x25, x19);
708 // Don't initialize the reserved registers.
709 // x26 : root register (root).
710 // x27 : context pointer (cp).
711 // x28 : JS stack pointer (jssp).
712 // x29 : frame pointer (fp).
713
714 // TODO(alexandre): Revisit the MAsm function invocation mechanisms.
715 // Currently there is a mix of statically and dynamically allocated
716 // registers.
717 __ Mov(x0, argc);
718 if (is_construct) {
719 // No type feedback cell is available.
720 Handle<Object> undefined_sentinel(
721 masm->isolate()->heap()->undefined_value(), masm->isolate());
722 __ Mov(x2, Operand(undefined_sentinel));
723
724 CallConstructStub stub(NO_CALL_FUNCTION_FLAGS);
725 __ CallStub(&stub);
726 } else {
727 ParameterCount actual(x0);
728 __ InvokeFunction(function, actual, CALL_FUNCTION, NullCallWrapper());
729 }
730 // Exit the JS internal frame and remove the parameters (except function),
731 // and return.
732 }
733
734 // Result is in x0. Return.
735 __ Ret();
736 }
737
738
739 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
740 Generate_JSEntryTrampolineHelper(masm, false);
741 }
742
743
744 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
745 Generate_JSEntryTrampolineHelper(masm, true);
746 }
747
748
749 void Builtins::Generate_CompileUnoptimized(MacroAssembler* masm) {
750 CallRuntimePassFunction(masm, Runtime::kCompileUnoptimized);
751 GenerateTailCallToReturnedCode(masm);
752 }
753
754
755 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
756 FrameScope scope(masm, StackFrame::INTERNAL);
757 Register function = x1;
758
759 // Preserve function. At the same time, push arguments for
760 // kCompileOptimized.
761 __ LoadObject(x10, masm->isolate()->factory()->ToBoolean(concurrent));
762 __ Push(function, function, x10);
763
764 __ CallRuntime(Runtime::kCompileOptimized, 2);
765
766 // Restore receiver.
767 __ Pop(function);
768 }
769
770
771 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
772 CallCompileOptimized(masm, false);
773 GenerateTailCallToReturnedCode(masm);
774 }
775
776
777 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
778 CallCompileOptimized(masm, true);
779 GenerateTailCallToReturnedCode(masm);
780 }
781
782
783 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
784 // For now, we are relying on the fact that make_code_young doesn't do any
785 // garbage collection which allows us to save/restore the registers without
786 // worrying about which of them contain pointers. We also don't build an
787 // internal frame to make the code fast, since we shouldn't have to do stack
788 // crawls in MakeCodeYoung. This seems a bit fragile.
789
790 // The following caller-saved registers must be saved and restored when
791 // calling through to the runtime:
792 // x0 - The address from which to resume execution.
793 // x1 - isolate
794 // lr - The return address for the JSFunction itself. It has not yet been
795 // preserved on the stack because the frame setup code was replaced
796 // with a call to this stub, to handle code ageing.
797 {
798 FrameScope scope(masm, StackFrame::MANUAL);
799 __ Push(x0, x1, fp, lr);
800 __ Mov(x1, Operand(ExternalReference::isolate_address(masm->isolate())));
801 __ CallCFunction(
802 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
803 __ Pop(lr, fp, x1, x0);
804 }
805
806 // The calling function has been made young again, so return to execute the
807 // real frame set-up code.
808 __ Br(x0);
809 }
810
811 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
812 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
813 MacroAssembler* masm) { \
814 GenerateMakeCodeYoungAgainCommon(masm); \
815 } \
816 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
817 MacroAssembler* masm) { \
818 GenerateMakeCodeYoungAgainCommon(masm); \
819 }
820 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
821 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
822
823
824 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
825 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
826 // that make_code_young doesn't do any garbage collection which allows us to
827 // save/restore the registers without worrying about which of them contain
828 // pointers.
829
830 // The following caller-saved registers must be saved and restored when
831 // calling through to the runtime:
832 // x0 - The address from which to resume execution.
833 // x1 - isolate
834 // lr - The return address for the JSFunction itself. It has not yet been
835 // preserved on the stack because the frame setup code was replaced
836 // with a call to this stub, to handle code ageing.
837 {
838 FrameScope scope(masm, StackFrame::MANUAL);
839 __ Push(x0, x1, fp, lr);
840 __ Mov(x1, Operand(ExternalReference::isolate_address(masm->isolate())));
841 __ CallCFunction(
842 ExternalReference::get_mark_code_as_executed_function(
843 masm->isolate()), 2);
844 __ Pop(lr, fp, x1, x0);
845
846 // Perform prologue operations usually performed by the young code stub.
847 __ EmitFrameSetupForCodeAgePatching(masm);
848 }
849
850 // Jump to point after the code-age stub.
851 __ Add(x0, x0, kCodeAgeSequenceSize);
852 __ Br(x0);
853 }
854
855
856 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
857 GenerateMakeCodeYoungAgainCommon(masm);
858 }
859
860
861 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
862 SaveFPRegsMode save_doubles) {
863 {
864 FrameScope scope(masm, StackFrame::INTERNAL);
865
866 // Preserve registers across notification, this is important for compiled
867 // stubs that tail call the runtime on deopts passing their parameters in
868 // registers.
869 // TODO(jbramley): Is it correct (and appropriate) to use safepoint
870 // registers here? According to the comment above, we should only need to
871 // preserve the registers with parameters.
872 __ PushXRegList(kSafepointSavedRegisters);
873 // Pass the function and deoptimization type to the runtime system.
874 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
875 __ PopXRegList(kSafepointSavedRegisters);
876 }
877
878 // Ignore state (pushed by Deoptimizer::EntryGenerator::Generate).
879 __ Drop(1);
880
881 // Jump to the miss handler. Deoptimizer::EntryGenerator::Generate loads this
882 // into lr before it jumps here.
883 __ Br(lr);
884 }
885
886
887 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
888 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
889 }
890
891
892 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
893 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
894 }
895
896
897 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
898 Deoptimizer::BailoutType type) {
899 {
900 FrameScope scope(masm, StackFrame::INTERNAL);
901 // Pass the deoptimization type to the runtime system.
902 __ Mov(x0, Operand(Smi::FromInt(static_cast<int>(type))));
903 __ Push(x0);
904 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
905 }
906
907 // Get the full codegen state from the stack and untag it.
908 Register state = x6;
909 __ Peek(state, 0);
910 __ SmiUntag(state);
911
912 // Switch on the state.
913 Label with_tos_register, unknown_state;
914 __ CompareAndBranch(
915 state, FullCodeGenerator::NO_REGISTERS, ne, &with_tos_register);
916 __ Drop(1); // Remove state.
917 __ Ret();
918
919 __ Bind(&with_tos_register);
920 // Reload TOS register.
921 __ Peek(x0, kPointerSize);
922 __ CompareAndBranch(state, FullCodeGenerator::TOS_REG, ne, &unknown_state);
923 __ Drop(2); // Remove state and TOS.
924 __ Ret();
925
926 __ Bind(&unknown_state);
927 __ Abort(kInvalidFullCodegenState);
928 }
929
930
931 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
932 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
933 }
934
935
936 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
937 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
938 }
939
940
941 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
942 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
943 }
944
945
946 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
947 // Lookup the function in the JavaScript frame.
948 __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
949 {
950 FrameScope scope(masm, StackFrame::INTERNAL);
951 // Pass function as argument.
952 __ Push(x0);
953 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
954 }
955
956 // If the code object is null, just return to the unoptimized code.
957 Label skip;
958 __ CompareAndBranch(x0, Operand(Smi::FromInt(0)), ne, &skip);
959 __ Ret();
960
961 __ Bind(&skip);
962
963 // Load deoptimization data from the code object.
964 // <deopt_data> = <code>[#deoptimization_data_offset]
965 __ Ldr(x1, MemOperand(x0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
966
967 // Load the OSR entrypoint offset from the deoptimization data.
968 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
969 __ Ldrsw(w1, UntagSmiFieldMemOperand(x1, FixedArray::OffsetOfElementAt(
970 DeoptimizationInputData::kOsrPcOffsetIndex)));
971
972 // Compute the target address = code_obj + header_size + osr_offset
973 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
974 __ Add(x0, x0, x1);
975 __ Add(lr, x0, Code::kHeaderSize - kHeapObjectTag);
976
977 // And "return" to the OSR entry point of the function.
978 __ Ret();
979 }
980
981
982 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
983 // We check the stack limit as indicator that recompilation might be done.
984 Label ok;
985 __ CompareRoot(jssp, Heap::kStackLimitRootIndex);
986 __ B(hs, &ok);
987 {
988 FrameScope scope(masm, StackFrame::INTERNAL);
989 __ CallRuntime(Runtime::kStackGuard, 0);
990 }
991 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
992 RelocInfo::CODE_TARGET);
993
994 __ Bind(&ok);
995 __ Ret();
996 }
997
998
999 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1000 Register receiver_type = x13;
1001
1002 ASM_LOCATION("Builtins::Generate_FunctionCall");
1003 // TODO(all/rames): Optimize and use named registers.
1004 // 1. Make sure we have at least one argument.
1005 // x0: actual number of arguments
1006 { Label done;
1007 __ Cbnz(x0, &done);
1008 __ LoadRoot(x10, Heap::kUndefinedValueRootIndex);
1009 __ Push(x10);
1010 __ Mov(x0, 1);
1011 __ Bind(&done);
1012 }
1013
1014 // 2. Get the function to call (passed as receiver) from the stack, check
1015 // if it is a function.
1016 // x0: actual number of arguments
1017 Label slow, non_function;
1018 // TODO(jbramley): Consider giving Peek a unit_size parameter, like Claim and
1019 // Drop. This usage pattern is very common.
1020 __ Peek(x1, Operand(x0, LSL, kXRegSizeInBytesLog2));
1021 __ JumpIfSmi(x1, &non_function);
1022 __ JumpIfNotObjectType(x1, x10, receiver_type, JS_FUNCTION_TYPE, &slow);
1023
1024 // 3a. Patch the first argument if necessary when calling a function.
1025 // x0: actual number of arguments
1026 // x1: function
1027 Label shift_arguments;
1028 __ Mov(x4, 0); // Indicates a regular JS_FUNCTION.
1029 { Label convert_to_object, use_global_receiver, patch_receiver;
1030 // Change context eagerly in case we need the global receiver.
1031 __ Ldr(cp, FieldMemOperand(x1, JSFunction::kContextOffset));
1032
1033 // Do not transform the receiver for strict mode functions.
1034 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
1035 __ Ldr(w11, FieldMemOperand(x10, SharedFunctionInfo::kCompilerHintsOffset));
1036 __ Tbnz(x11, SharedFunctionInfo::kStrictModeFunction, &shift_arguments);
1037
1038 // TODO(all): Shoudld we insert space to avoid BTAC collisions?
1039 // Do not transform the receiver for native (Compilerhints already in x3).
1040 __ Tbnz(x11, SharedFunctionInfo::kNative, &shift_arguments);
1041
1042 // Compute the receiver in non-strict mode.
1043 __ Sub(x10, x0, 1);
1044 __ Peek(x2, Operand(x10, LSL, kXRegSizeInBytesLog2));
1045 // x0: actual number of arguments
1046 // x1: function
1047 // x2: first argument
1048 __ JumpIfSmi(x2, &convert_to_object);
1049
1050 // TODO(all): We could potentially work to optimize loads of root values.
1051 // TODO(all): If the indexes are successive we can use 'ldp'.
1052 __ JumpIfRoot(x2, Heap::kUndefinedValueRootIndex, &use_global_receiver);
1053 __ JumpIfRoot(x2, Heap::kNullValueRootIndex, &use_global_receiver);
1054
1055 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1056 __ JumpIfObjectType(x2, x10, x11, FIRST_SPEC_OBJECT_TYPE, &shift_arguments,
1057 ge);
1058
1059 __ Bind(&convert_to_object);
1060
1061 {
1062 // Enter an internal frame in order to preserve argument count.
1063 FrameScope scope(masm, StackFrame::INTERNAL);
1064 __ SmiTag(x0);
1065
1066 __ Push(x0, x2);
1067 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1068 __ Mov(x2, x0);
1069
1070 __ Pop(x0);
1071 __ SmiUntag(x0);
1072
1073 // Exit the internal frame.
1074 }
1075
1076 // Restore the function to x1, and the flag to x4.
1077 __ Peek(x1, Operand(x0, LSL, kXRegSizeInBytesLog2));
1078 __ Mov(x4, 0);
1079 __ B(&patch_receiver);
1080
1081 __ Bind(&use_global_receiver);
1082 __ Ldr(x2, GlobalObjectMemOperand());
1083 __ Ldr(x2, FieldMemOperand(x2, GlobalObject::kGlobalReceiverOffset));
1084
1085 __ Bind(&patch_receiver);
1086 __ Sub(x10, x0, 1);
1087 __ Poke(x2, Operand(x10, LSL, kXRegSizeInBytesLog2));
1088
1089 __ B(&shift_arguments);
1090 }
1091
1092 // 3b. Check for function proxy.
1093 __ Bind(&slow);
1094 __ Mov(x4, 1); // Indicate function proxy.
1095 __ Cmp(receiver_type, JS_FUNCTION_PROXY_TYPE);
1096 __ B(eq, &shift_arguments);
1097 __ Bind(&non_function);
1098 __ Mov(x4, 2); // Indicate non-function.
1099
1100 // 3c. Patch the first argument when calling a non-function. The
1101 // CALL_NON_FUNCTION builtin expects the non-function callee as
1102 // receiver, so overwrite the first argument which will ultimately
1103 // become the receiver.
1104 // x0: actual number of arguments
1105 // x1: function
1106 // x4: call type (0: JS function, 1: function proxy, 2: non-function)
1107 __ Sub(x10, x0, 1);
1108 __ Poke(x1, Operand(x10, LSL, kXRegSizeInBytesLog2));
1109
1110 // 4. Shift arguments and return address one slot down on the stack
1111 // (overwriting the original receiver). Adjust argument count to make
1112 // the original first argument the new receiver.
1113 // x0: actual number of arguments
1114 // x1: function
1115 // x4: call type (0: JS function, 1: function proxy, 2: non-function)
1116 __ Bind(&shift_arguments);
1117 { Label loop;
1118 // Calculate the copy start address (destination). Copy end address is jssp.
1119 __ Add(x11, jssp, Operand(x0, LSL, kPointerSizeLog2));
1120 __ Sub(x10, x11, kPointerSize);
1121
1122 // TODO(all): Optimize to copy values 2 by 2?
1123 __ Bind(&loop);
1124 __ Ldr(x12, MemOperand(x10, -kPointerSize, PostIndex));
1125 __ Str(x12, MemOperand(x11, -kPointerSize, PostIndex));
1126 __ Cmp(x10, jssp);
1127 __ B(ge, &loop);
1128 // Adjust the actual number of arguments and remove the top element
1129 // (which is a copy of the last argument).
1130 __ Sub(x0, x0, 1);
1131 __ Drop(1);
1132 }
1133
1134 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1135 // or a function proxy via CALL_FUNCTION_PROXY.
1136 // x0: actual number of arguments
1137 // x1: function
1138 // x4: call type (0: JS function, 1: function proxy, 2: non-function)
1139 { Label function, non_proxy;
1140 __ Cbz(x4, &function);
1141 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1142 __ Mov(x2, 0);
1143 __ Cmp(x4, 1);
1144 __ B(ne, &non_proxy);
1145
1146 __ Push(x1); // Re-add proxy object as additional argument.
1147 __ Add(x0, x0, 1);
1148 __ GetBuiltinFunction(x1, Builtins::CALL_FUNCTION_PROXY);
1149 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1150 RelocInfo::CODE_TARGET);
1151
1152 __ Bind(&non_proxy);
1153 __ GetBuiltinFunction(x1, Builtins::CALL_NON_FUNCTION);
1154 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1155 RelocInfo::CODE_TARGET);
1156 __ Bind(&function);
1157 }
1158
1159 // 5b. Get the code to call from the function and check that the number of
1160 // expected arguments matches what we're providing. If so, jump
1161 // (tail-call) to the code in register edx without checking arguments.
1162 // x0: actual number of arguments
1163 // x1: function
1164 __ Ldr(x3, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
1165 __ Ldrsw(x2,
1166 FieldMemOperand(x3,
1167 SharedFunctionInfo::kFormalParameterCountOffset));
1168 Label dont_adapt_args;
1169 __ Cmp(x2, x0); // Check formal and actual parameter counts.
1170 __ B(eq, &dont_adapt_args);
1171 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1172 RelocInfo::CODE_TARGET);
1173 __ Bind(&dont_adapt_args);
1174
1175 __ Ldr(x3, FieldMemOperand(x1, JSFunction::kCodeEntryOffset));
1176 ParameterCount expected(0);
1177 __ InvokeCode(x3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1178 }
1179
1180
1181 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1182 ASM_LOCATION("Builtins::Generate_FunctionApply");
1183 const int kIndexOffset =
1184 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1185 const int kLimitOffset =
1186 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1187 const int kArgsOffset = 2 * kPointerSize;
1188 const int kReceiverOffset = 3 * kPointerSize;
1189 const int kFunctionOffset = 4 * kPointerSize;
1190
1191 {
1192 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1193
1194 Register args = x12;
1195 Register receiver = x14;
1196 Register function = x15;
1197
1198 // Get the length of the arguments via a builtin call.
1199 __ Ldr(function, MemOperand(fp, kFunctionOffset));
1200 __ Ldr(args, MemOperand(fp, kArgsOffset));
1201 __ Push(function, args);
1202 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1203 Register argc = x0;
1204
1205 // Check the stack for overflow.
1206 // We are not trying to catch interruptions (e.g. debug break and
1207 // preemption) here, so the "real stack limit" is checked.
1208 Label enough_stack_space;
1209 __ LoadRoot(x10, Heap::kRealStackLimitRootIndex);
1210 __ Ldr(function, MemOperand(fp, kFunctionOffset));
1211 // Make x10 the space we have left. The stack might already be overflowed
1212 // here which will cause x10 to become negative.
1213 // TODO(jbramley): Check that the stack usage here is safe.
1214 __ Sub(x10, jssp, x10);
1215 // Check if the arguments will overflow the stack.
1216 __ Cmp(x10, Operand(argc, LSR, kSmiShift - kPointerSizeLog2));
1217 __ B(gt, &enough_stack_space);
1218 // There is not enough stack space, so use a builtin to throw an appropriate
1219 // error.
1220 __ Push(function, argc);
1221 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
1222 // We should never return from the APPLY_OVERFLOW builtin.
1223 if (__ emit_debug_code()) {
1224 __ Unreachable();
1225 }
1226
1227 __ Bind(&enough_stack_space);
1228 // Push current limit and index.
1229 __ Mov(x1, 0); // Initial index.
1230 __ Push(argc, x1);
1231
1232 Label push_receiver;
1233 __ Ldr(receiver, MemOperand(fp, kReceiverOffset));
1234
1235 // Check that the function is a JS function. Otherwise it must be a proxy.
1236 // When it is not the function proxy will be invoked later.
1237 __ JumpIfNotObjectType(function, x10, x11, JS_FUNCTION_TYPE,
1238 &push_receiver);
1239
1240 // Change context eagerly to get the right global object if necessary.
1241 __ Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
1242 // Load the shared function info.
1243 __ Ldr(x2, FieldMemOperand(function,
1244 JSFunction::kSharedFunctionInfoOffset));
1245
1246 // Compute and push the receiver.
1247 // Do not transform the receiver for strict mode functions.
1248 Label convert_receiver_to_object, use_global_receiver;
1249 __ Ldr(w10, FieldMemOperand(x2, SharedFunctionInfo::kCompilerHintsOffset));
1250 __ Tbnz(x10, SharedFunctionInfo::kStrictModeFunction, &push_receiver);
1251 // Do not transform the receiver for native functions.
1252 __ Tbnz(x10, SharedFunctionInfo::kNative, &push_receiver);
1253
1254 // Compute the receiver in non-strict mode.
1255 __ JumpIfSmi(receiver, &convert_receiver_to_object);
1256 __ JumpIfRoot(receiver, Heap::kNullValueRootIndex, &use_global_receiver);
1257 __ JumpIfRoot(receiver, Heap::kUndefinedValueRootIndex,
1258 &use_global_receiver);
1259
1260 // Check if the receiver is already a JavaScript object.
1261 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1262 __ JumpIfObjectType(receiver, x10, x11, FIRST_SPEC_OBJECT_TYPE,
1263 &push_receiver, ge);
1264
1265 // Call a builtin to convert the receiver to a regular object.
1266 __ Bind(&convert_receiver_to_object);
1267 __ Push(receiver);
1268 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1269 __ Mov(receiver, x0);
1270 __ B(&push_receiver);
1271
1272 __ Bind(&use_global_receiver);
1273 __ Ldr(x10, GlobalObjectMemOperand());
1274 __ Ldr(receiver, FieldMemOperand(x10, GlobalObject::kGlobalReceiverOffset));
1275
1276 // Push the receiver
1277 __ Bind(&push_receiver);
1278 __ Push(receiver);
1279
1280 // Copy all arguments from the array to the stack.
1281 Label entry, loop;
1282 Register current = x0;
1283 __ Ldr(current, MemOperand(fp, kIndexOffset));
1284 __ B(&entry);
1285
1286 __ Bind(&loop);
1287 // Load the current argument from the arguments array and push it.
1288 // TODO(all): Couldn't we optimize this for JS arrays?
1289
1290 __ Ldr(x1, MemOperand(fp, kArgsOffset));
1291 __ Push(x1, current);
1292
1293 // Call the runtime to access the property in the arguments array.
1294 __ CallRuntime(Runtime::kGetProperty, 2);
1295 __ Push(x0);
1296
1297 // Use inline caching to access the arguments.
1298 __ Ldr(current, MemOperand(fp, kIndexOffset));
1299 __ Add(current, current, Operand(Smi::FromInt(1)));
1300 __ Str(current, MemOperand(fp, kIndexOffset));
1301
1302 // Test if the copy loop has finished copying all the elements from the
1303 // arguments object.
1304 __ Bind(&entry);
1305 __ Ldr(x1, MemOperand(fp, kLimitOffset));
1306 __ Cmp(current, x1);
1307 __ B(ne, &loop);
1308
1309 // At the end of the loop, the number of arguments is stored in 'current',
1310 // represented as a smi.
1311
1312 function = x1; // From now on we want the function to be kept in x1;
1313 __ Ldr(function, MemOperand(fp, kFunctionOffset));
1314
1315 // Call the function.
1316 Label call_proxy;
1317 ParameterCount actual(current);
1318 __ SmiUntag(current);
1319 __ JumpIfNotObjectType(function, x10, x11, JS_FUNCTION_TYPE, &call_proxy);
1320 __ InvokeFunction(function, actual, CALL_FUNCTION, NullCallWrapper());
1321 frame_scope.GenerateLeaveFrame();
1322 __ Drop(3);
1323 __ Ret();
1324
1325 // Call the function proxy.
1326 __ Bind(&call_proxy);
1327 // x0 : argc
1328 // x1 : function
1329 __ Push(function); // Add function proxy as last argument.
1330 __ Add(x0, x0, 1);
1331 __ Mov(x2, 0);
1332 __ GetBuiltinFunction(x1, Builtins::CALL_FUNCTION_PROXY);
1333 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1334 RelocInfo::CODE_TARGET);
1335 }
1336 __ Drop(3);
1337 __ Ret();
1338 }
1339
1340
1341 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1342 __ SmiTag(x10, x0);
1343 __ Mov(x11, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1344 __ Push(lr, fp);
1345 __ Push(x11, x1, x10);
1346 __ Add(fp, jssp,
1347 StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize);
1348 }
1349
1350
1351 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1352 // ----------- S t a t e -------------
1353 // -- x0 : result being passed through
1354 // -----------------------------------
1355 // Get the number of arguments passed (as a smi), tear down the frame and
1356 // then drop the parameters and the receiver.
1357 __ Ldr(x10, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1358 kPointerSize)));
1359 __ Mov(jssp, fp);
1360 __ Pop(fp, lr);
1361 __ DropBySMI(x10, kXRegSizeInBytes);
1362 __ Drop(1);
1363 }
1364
1365
1366 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1367 ASM_LOCATION("Builtins::Generate_ArgumentsAdaptorTrampoline");
1368 // ----------- S t a t e -------------
1369 // -- x0 : actual number of arguments
1370 // -- x1 : function (passed through to callee)
1371 // -- x2 : expected number of arguments
1372 // -----------------------------------
1373
1374 Label invoke, dont_adapt_arguments;
1375
1376 Label enough, too_few;
1377 __ Ldr(x3, FieldMemOperand(x1, JSFunction::kCodeEntryOffset));
1378 __ Cmp(x0, x2);
1379 __ B(lt, &too_few);
1380 __ Cmp(x2, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1381 __ B(eq, &dont_adapt_arguments);
1382
1383 { // Enough parameters: actual >= expected
1384 EnterArgumentsAdaptorFrame(masm);
1385
1386 // Calculate copy start address into x10 and end address into x11.
1387 // x0: actual number of arguments
1388 // x1: function
1389 // x2: expected number of arguments
1390 // x3: code entry to call
1391 __ Add(x10, fp, Operand(x0, LSL, kPointerSizeLog2));
1392 // Adjust for return address and receiver
1393 __ Add(x10, x10, 2 * kPointerSize);
1394 __ Sub(x11, x10, Operand(x2, LSL, kPointerSizeLog2));
1395
1396 // Copy the arguments (including the receiver) to the new stack frame.
1397 // x0: actual number of arguments
1398 // x1: function
1399 // x2: expected number of arguments
1400 // x3: code entry to call
1401 // x10: copy start address
1402 // x11: copy end address
1403
1404 // TODO(all): Should we push values 2 by 2?
1405 Label copy;
1406 __ Bind(&copy);
1407 __ Cmp(x10, x11);
1408 __ Ldr(x12, MemOperand(x10, -kPointerSize, PostIndex));
1409 __ Push(x12);
1410 __ B(gt, &copy);
1411
1412 __ B(&invoke);
1413 }
1414
1415 { // Too few parameters: Actual < expected
1416 __ Bind(&too_few);
1417 EnterArgumentsAdaptorFrame(masm);
1418
1419 // Calculate copy start address into x10 and copy end address into x11.
1420 // x0: actual number of arguments
1421 // x1: function
1422 // x2: expected number of arguments
1423 // x3: code entry to call
1424 // Adjust for return address.
1425 __ Add(x11, fp, 1 * kPointerSize);
1426 __ Add(x10, x11, Operand(x0, LSL, kPointerSizeLog2));
1427 __ Add(x10, x10, 1 * kPointerSize);
1428
1429 // Copy the arguments (including the receiver) to the new stack frame.
1430 // x0: actual number of arguments
1431 // x1: function
1432 // x2: expected number of arguments
1433 // x3: code entry to call
1434 // x10: copy start address
1435 // x11: copy end address
1436 Label copy;
1437 __ Bind(&copy);
1438 __ Ldr(x12, MemOperand(x10, -kPointerSize, PostIndex));
1439 __ Push(x12);
1440 __ Cmp(x10, x11); // Compare before moving to next argument.
1441 __ B(ne, &copy);
1442
1443 // Fill the remaining expected arguments with undefined.
1444 // x0: actual number of arguments
1445 // x1: function
1446 // x2: expected number of arguments
1447 // x3: code entry to call
1448 __ LoadRoot(x10, Heap::kUndefinedValueRootIndex);
1449 __ Sub(x11, fp, Operand(x2, LSL, kPointerSizeLog2));
1450 // Adjust for the arguments adaptor frame and already pushed receiver.
1451 __ Sub(x11, x11,
1452 StandardFrameConstants::kFixedFrameSizeFromFp + (2 * kPointerSize));
1453
1454 // TODO(all): Optimize this to use ldp?
1455 Label fill;
1456 __ Bind(&fill);
1457 __ Push(x10);
1458 __ Cmp(jssp, x11);
1459 __ B(ne, &fill);
1460 }
1461
1462 // Arguments have been adapted. Now call the entry point.
1463 __ Bind(&invoke);
1464 __ Call(x3);
1465
1466 // Store offset of return address for deoptimizer.
1467 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1468
1469 // Exit frame and return.
1470 LeaveArgumentsAdaptorFrame(masm);
1471 __ Ret();
1472
1473 // Call the entry point without adapting the arguments.
1474 __ Bind(&dont_adapt_arguments);
1475 __ Jump(x3);
1476 }
1477
1478
1479 #undef __
1480
1481 } } // namespace v8::internal
1482
1483 #endif // V8_TARGET_ARCH_ARM
OLDNEW
« no previous file with comments | « src/a64/assembler-a64-inl.h ('k') | src/a64/code-stubs-a64.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698