blob: 8ce57d55447aa84ddeaed0c37e8d1d7fea917734 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_ARM
Leon Clarkef7060e22010-06-03 12:02:55 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/codegen.h"
10#include "src/debug.h"
11#include "src/deoptimizer.h"
12#include "src/full-codegen.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/runtime/runtime.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000014
15namespace v8 {
16namespace internal {
17
18
19#define __ ACCESS_MASM(masm)
20
21
Leon Clarkee46be812010-01-19 14:06:41 +000022void Builtins::Generate_Adaptor(MacroAssembler* masm,
23 CFunctionId id,
24 BuiltinExtraArguments extra_args) {
25 // ----------- S t a t e -------------
26 // -- r0 : number of arguments excluding receiver
27 // -- r1 : called function (only guaranteed when
28 // extra_args requires it)
29 // -- cp : context
30 // -- sp[0] : last argument
31 // -- ...
32 // -- sp[4 * (argc - 1)] : first argument (argc == r0)
33 // -- sp[4 * argc] : receiver
34 // -----------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Leon Clarkee46be812010-01-19 14:06:41 +000036 // Insert extra arguments.
37 int num_extra_args = 0;
38 if (extra_args == NEEDS_CALLED_FUNCTION) {
39 num_extra_args = 1;
40 __ push(r1);
41 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
Leon Clarkee46be812010-01-19 14:06:41 +000043 }
44
Steve Block6ded16b2010-05-10 14:33:55 +010045 // JumpToExternalReference expects r0 to contain the number of arguments
Leon Clarkee46be812010-01-19 14:06:41 +000046 // including the receiver and the extra arguments.
47 __ add(r0, r0, Operand(num_extra_args + 1));
Steve Block44f0eee2011-05-26 01:26:41 +010048 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +000049}
50
51
Ben Murdoch3ef787d2012-04-12 10:51:47 +010052// Load the built-in InternalArray function from the current context.
53static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
54 Register result) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055 // Load the native context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010056
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057 __ ldr(result,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
59 __ ldr(result,
60 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
61 // Load the InternalArray function from the native context.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062 __ ldr(result,
63 MemOperand(result,
64 Context::SlotOffset(
65 Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
66}
67
68
Steve Blocka7e24c12009-10-30 11:49:00 +000069// Load the built-in Array function from the current context.
70static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 // Load the native context.
Steve Blocka7e24c12009-10-30 11:49:00 +000072
Steve Blocka7e24c12009-10-30 11:49:00 +000073 __ ldr(result,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
75 __ ldr(result,
76 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
77 // Load the Array function from the native context.
Steve Blocka7e24c12009-10-30 11:49:00 +000078 __ ldr(result,
79 MemOperand(result,
80 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
81}
82
83
Ben Murdoch3ef787d2012-04-12 10:51:47 +010084void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
85 // ----------- S t a t e -------------
86 // -- r0 : number of arguments
87 // -- lr : return address
88 // -- sp[...]: constructor arguments
89 // -----------------------------------
90 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
91
92 // Get the InternalArray function.
93 GenerateLoadInternalArrayFunction(masm, r1);
94
95 if (FLAG_debug_code) {
96 // Initial map for the builtin InternalArray functions should be maps.
97 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098 __ SmiTst(r2);
99 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100100 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100102 }
103
104 // Run the native code for the InternalArray function called as a normal
105 // function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 // tail call a stub
107 InternalArrayConstructorStub stub(masm->isolate());
108 __ TailCallStub(&stub);
Steve Blocka7e24c12009-10-30 11:49:00 +0000109}
110
111
112void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
113 // ----------- S t a t e -------------
114 // -- r0 : number of arguments
115 // -- lr : return address
116 // -- sp[...]: constructor arguments
117 // -----------------------------------
118 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
119
120 // Get the Array function.
121 GenerateLoadArrayFunction(masm, r1);
122
123 if (FLAG_debug_code) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100124 // Initial map for the builtin Array functions should be maps.
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 __ SmiTst(r2);
127 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130 }
131
132 // Run the native code for the Array function called as a normal function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133 // tail call a stub
134 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
135 ArrayConstructorStub stub(masm->isolate());
136 __ TailCallStub(&stub);
Steve Blocka7e24c12009-10-30 11:49:00 +0000137}
138
139
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100140void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800141 // ----------- 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 // -----------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +0100148 Counters* counters = masm->isolate()->counters();
149 __ IncrementCounter(counters->string_ctor_calls(), 1, r2, r3);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800150
151 Register function = r1;
152 if (FLAG_debug_code) {
153 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, r2);
154 __ cmp(function, Operand(r2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 __ Assert(eq, kUnexpectedStringFunction);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800156 }
157
158 // Load the first arguments in r0 and get rid of the rest.
159 Label no_arguments;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 __ cmp(r0, Operand::Zero());
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800161 __ b(eq, &no_arguments);
162 // First args = sp[(argc - 1) * 4].
163 __ sub(r0, r0, Operand(1));
164 __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
165 // sp now point to args[0], drop args[0] + receiver.
166 __ Drop(2);
167
168 Register argument = r2;
169 Label not_cached, argument_is_string;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 __ LookupNumberStringCache(r0, // Input.
171 argument, // Result.
172 r3, // Scratch.
173 r4, // Scratch.
174 r5, // Scratch.
175 &not_cached);
Steve Block44f0eee2011-05-26 01:26:41 +0100176 __ IncrementCounter(counters->string_ctor_cached_number(), 1, r3, r4);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800177 __ bind(&argument_is_string);
178
179 // ----------- S t a t e -------------
180 // -- r2 : argument converted to string
181 // -- r1 : constructor function
182 // -- lr : return address
183 // -----------------------------------
184
185 Label gc_required;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 __ Allocate(JSValue::kSize,
187 r0, // Result.
188 r3, // Scratch.
189 r4, // Scratch.
190 &gc_required,
191 TAG_OBJECT);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800192
193 // Initialising the String Object.
194 Register map = r3;
195 __ LoadGlobalFunctionInitialMap(function, map, r4);
196 if (FLAG_debug_code) {
197 __ ldrb(r4, FieldMemOperand(map, Map::kInstanceSizeOffset));
198 __ cmp(r4, Operand(JSValue::kSize >> kPointerSizeLog2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000199 __ Assert(eq, kUnexpectedStringWrapperInstanceSize);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800200 __ ldrb(r4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000201 __ cmp(r4, Operand::Zero());
202 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800203 }
204 __ str(map, FieldMemOperand(r0, HeapObject::kMapOffset));
205
206 __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
207 __ str(r3, FieldMemOperand(r0, JSObject::kPropertiesOffset));
208 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
209
210 __ str(argument, FieldMemOperand(r0, JSValue::kValueOffset));
211
212 // Ensure the object is fully initialized.
213 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
214
215 __ Ret();
216
217 // The argument was not found in the number to string cache. Check
218 // if it's a string already before calling the conversion builtin.
219 Label convert_argument;
220 __ bind(&not_cached);
Steve Block1e0659c2011-05-24 12:43:12 +0100221 __ JumpIfSmi(r0, &convert_argument);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800222
223 // Is it a String?
224 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
225 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000226 STATIC_ASSERT(kNotStringTag != 0);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800227 __ tst(r3, Operand(kIsNotStringMask));
228 __ b(ne, &convert_argument);
229 __ mov(argument, r0);
Steve Block44f0eee2011-05-26 01:26:41 +0100230 __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800231 __ b(&argument_is_string);
232
233 // Invoke the conversion builtin and put the result into r2.
234 __ bind(&convert_argument);
235 __ push(function); // Preserve the function.
Steve Block44f0eee2011-05-26 01:26:41 +0100236 __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100237 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100239 __ push(r0);
240 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
241 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800242 __ pop(function);
243 __ mov(argument, r0);
244 __ b(&argument_is_string);
245
246 // Load the empty string into r2, remove the receiver from the
247 // stack, and jump back to the case where the argument is a string.
248 __ bind(&no_arguments);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800250 __ Drop(1);
251 __ b(&argument_is_string);
252
253 // At this point the argument is already a string. Call runtime to
254 // create a string wrapper.
255 __ bind(&gc_required);
Steve Block44f0eee2011-05-26 01:26:41 +0100256 __ IncrementCounter(counters->string_ctor_gc_required(), 1, r3, r4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100257 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100259 __ push(argument);
260 __ CallRuntime(Runtime::kNewStringWrapper, 1);
261 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800262 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100263}
264
265
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266static void CallRuntimePassFunction(
267 MacroAssembler* masm, Runtime::FunctionId function_id) {
268 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
269 // Push a copy of the function onto the stack.
270 __ push(r1);
271 // Push function as parameter to the runtime call.
272 __ Push(r1);
273
274 __ CallRuntime(function_id, 1);
275 // Restore receiver.
276 __ pop(r1);
277}
278
279
280static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
281 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
282 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCodeOffset));
283 __ add(r2, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
284 __ Jump(r2);
285}
286
287
288static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
289 __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
290 __ Jump(r0);
291}
292
293
294void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
295 // Checking whether the queued function is ready for install is optional,
296 // since we come across interrupts and stack checks elsewhere. However,
297 // not checking may delay installing ready functions, and always checking
298 // would be quite expensive. A good compromise is to first check against
299 // stack limit as a cue for an interrupt signal.
300 Label ok;
301 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
302 __ cmp(sp, Operand(ip));
303 __ b(hs, &ok);
304
305 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
306 GenerateTailCallToReturnedCode(masm);
307
308 __ bind(&ok);
309 GenerateTailCallToSharedCode(masm);
310}
311
312
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100313static void Generate_JSConstructStubHelper(MacroAssembler* masm,
314 bool is_api_function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000315 bool create_memento) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 // ----------- S t a t e -------------
317 // -- r0 : number of arguments
318 // -- r1 : constructor function
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 // -- r2 : allocation site or undefined
Steve Blocka7e24c12009-10-30 11:49:00 +0000320 // -- lr : return address
321 // -- sp[...]: constructor arguments
322 // -----------------------------------
323
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 // Should never create mementos for api functions.
325 DCHECK(!is_api_function || !create_memento);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100326
Steve Block44f0eee2011-05-26 01:26:41 +0100327 Isolate* isolate = masm->isolate();
328
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 // Enter a construct frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100330 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331 FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
332
333 if (create_memento) {
334 __ AssertUndefinedOrAllocationSite(r2, r3);
335 __ push(r2);
336 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000337
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100338 // Preserve the two incoming parameters on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 __ SmiTag(r0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100340 __ push(r0); // Smi-tagged arguments count.
341 __ push(r1); // Constructor function.
Steve Blocka7e24c12009-10-30 11:49:00 +0000342
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100343 // Try to allocate the object without transitioning into C code. If any of
344 // the preconditions is not met, the code bails out to the runtime call.
345 Label rt_call, allocated;
346 if (FLAG_inline_new) {
347 Label undo_allocation;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100348 ExternalReference debug_step_in_fp =
349 ExternalReference::debug_step_in_fp_address(isolate);
350 __ mov(r2, Operand(debug_step_in_fp));
351 __ ldr(r2, MemOperand(r2));
352 __ tst(r2, r2);
353 __ b(ne, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000354
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100355 // Load the initial map and verify that it is in fact a map.
356 // r1: constructor function
357 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
358 __ JumpIfSmi(r2, &rt_call);
359 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
360 __ b(ne, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100362 // Check that the constructor is not constructing a JSFunction (see
363 // comments in Runtime_NewObject in runtime.cc). In which case the
364 // initial map's instance type would be JS_FUNCTION_TYPE.
365 // r1: constructor function
366 // r2: initial map
367 __ CompareInstanceType(r2, r3, JS_FUNCTION_TYPE);
368 __ b(eq, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000369
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 if (!is_api_function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100371 Label allocate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 MemOperand bit_field3 = FieldMemOperand(r2, Map::kBitField3Offset);
373 // Check if slack tracking is enabled.
374 __ ldr(r4, bit_field3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400375 __ DecodeField<Map::Counter>(r3, r4);
376 __ cmp(r3, Operand(Map::kSlackTrackingCounterEnd));
377 __ b(lt, &allocate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100378 // Decrease generous allocation count.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400379 __ sub(r4, r4, Operand(1 << Map::Counter::kShift));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 __ str(r4, bit_field3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400381 __ cmp(r3, Operand(Map::kSlackTrackingCounterEnd));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100382 __ b(ne, &allocate);
383
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 __ push(r1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100385
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 __ Push(r2, r1); // r1 = constructor
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100387 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
388
389 __ pop(r2);
390 __ pop(r1);
391
392 __ bind(&allocate);
393 }
394
395 // Now allocate the JSObject on the heap.
396 // r1: constructor function
397 // r2: initial map
398 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceSizeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 if (create_memento) {
400 __ add(r3, r3, Operand(AllocationMemento::kSize / kPointerSize));
401 }
402
403 __ Allocate(r3, r4, r5, r6, &rt_call, SIZE_IN_WORDS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100404
405 // Allocated the JSObject, now initialize the fields. Map is set to
406 // initial map and properties and elements are set to empty fixed array.
407 // r1: constructor function
408 // r2: initial map
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 // r3: object size (not including memento if create_memento)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100410 // r4: JSObject (not tagged)
411 __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
412 __ mov(r5, r4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000413 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100414 __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 DCHECK_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100416 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 DCHECK_EQ(2 * kPointerSize, JSObject::kElementsOffset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
419
420 // Fill all the in-object properties with the appropriate filler.
421 // r1: constructor function
422 // r2: initial map
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 // r3: object size (in words, including memento if create_memento)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100424 // r4: JSObject (not tagged)
425 // r5: First in-object property of JSObject (not tagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000426 DCHECK_EQ(3 * kPointerSize, JSObject::kHeaderSize);
427 __ LoadRoot(r6, Heap::kUndefinedValueRootIndex);
428
429 if (!is_api_function) {
430 Label no_inobject_slack_tracking;
431
432 // Check if slack tracking is enabled.
433 __ ldr(ip, FieldMemOperand(r2, Map::kBitField3Offset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400434 __ DecodeField<Map::Counter>(ip);
435 __ cmp(ip, Operand(Map::kSlackTrackingCounterEnd));
436 __ b(lt, &no_inobject_slack_tracking);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437
438 // Allocate object with a slack.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100439 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
440 __ Ubfx(r0, r0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
441 kBitsPerByte);
442 __ add(r0, r5, Operand(r0, LSL, kPointerSizeLog2));
443 // r0: offset of first field after pre-allocated fields
444 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 __ add(ip, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
446 __ cmp(r0, ip);
447 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000449 __ InitializeFieldsWithFiller(r5, r0, r6);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100450 // To allow for truncation.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000451 __ LoadRoot(r6, Heap::kOnePointerFillerMapRootIndex);
452 // Fill the remaining fields with one pointer filler map.
453
454 __ bind(&no_inobject_slack_tracking);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100455 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000456
457 if (create_memento) {
458 __ sub(ip, r3, Operand(AllocationMemento::kSize / kPointerSize));
459 __ add(r0, r4, Operand(ip, LSL, kPointerSizeLog2)); // End of object.
460 __ InitializeFieldsWithFiller(r5, r0, r6);
461
462 // Fill in memento fields.
463 // r5: points to the allocated but uninitialized memento.
464 __ LoadRoot(r6, Heap::kAllocationMementoMapRootIndex);
465 DCHECK_EQ(0 * kPointerSize, AllocationMemento::kMapOffset);
466 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
467 // Load the AllocationSite
468 __ ldr(r6, MemOperand(sp, 2 * kPointerSize));
469 DCHECK_EQ(1 * kPointerSize, AllocationMemento::kAllocationSiteOffset);
470 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
471 } else {
472 __ add(r0, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
473 __ InitializeFieldsWithFiller(r5, r0, r6);
474 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100475
476 // Add the object tag to make the JSObject real, so that we can continue
477 // and jump into the continuation code at any time from now on. Any
478 // failures need to undo the allocation, so that the heap is in a
479 // consistent state and verifiable.
480 __ add(r4, r4, Operand(kHeapObjectTag));
481
482 // Check if a non-empty properties array is needed. Continue with
483 // allocated object if not fall through to runtime call if it is.
484 // r1: constructor function
485 // r4: JSObject
486 // r5: start of next object (not tagged)
487 __ ldrb(r3, FieldMemOperand(r2, Map::kUnusedPropertyFieldsOffset));
488 // The field instance sizes contains both pre-allocated property fields
489 // and in-object properties.
490 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
491 __ Ubfx(r6, r0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
492 kBitsPerByte);
493 __ add(r3, r3, Operand(r6));
494 __ Ubfx(r6, r0, Map::kInObjectPropertiesByte * kBitsPerByte,
495 kBitsPerByte);
496 __ sub(r3, r3, Operand(r6), SetCC);
497
498 // Done if no extra properties are to be allocated.
499 __ b(eq, &allocated);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 __ Assert(pl, kPropertyAllocationCountFailed);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100501
502 // Scale the number of elements by pointer size and add the header for
503 // FixedArrays to the start of the next object calculation from above.
504 // r1: constructor
505 // r3: number of elements in properties array
506 // r4: JSObject
507 // r5: start of next object
508 __ add(r0, r3, Operand(FixedArray::kHeaderSize / kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 __ Allocate(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100510 r0,
511 r5,
512 r6,
513 r2,
514 &undo_allocation,
515 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
516
517 // Initialize the FixedArray.
518 // r1: constructor
519 // r3: number of elements in properties array
520 // r4: JSObject
521 // r5: FixedArray (not tagged)
522 __ LoadRoot(r6, Heap::kFixedArrayMapRootIndex);
523 __ mov(r2, r5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100525 __ str(r6, MemOperand(r2, kPointerSize, PostIndex));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 DCHECK_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
527 __ SmiTag(r0, r3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100528 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
529
530 // Initialize the fields to undefined.
531 // r1: constructor function
532 // r2: First element of FixedArray (not tagged)
533 // r3: number of elements in properties array
534 // r4: JSObject
535 // r5: FixedArray (not tagged)
536 __ add(r6, r2, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000537 DCHECK_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100538 { Label loop, entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100540 __ b(&entry);
541 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100543 __ bind(&entry);
544 __ cmp(r2, r6);
545 __ b(lt, &loop);
546 }
547
548 // Store the initialized FixedArray into the properties field of
549 // the JSObject
550 // r1: constructor function
551 // r4: JSObject
552 // r5: FixedArray (not tagged)
553 __ add(r5, r5, Operand(kHeapObjectTag)); // Add the heap tag.
554 __ str(r5, FieldMemOperand(r4, JSObject::kPropertiesOffset));
555
556 // Continue with JSObject being successfully allocated
557 // r1: constructor function
558 // r4: JSObject
559 __ jmp(&allocated);
560
561 // Undo the setting of the new top so that the heap is verifiable. For
562 // example, the map's unused properties potentially do not match the
563 // allocated objects unused properties.
564 // r4: JSObject (previous new top)
565 __ bind(&undo_allocation);
566 __ UndoAllocationInNewSpace(r4, r5);
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 }
568
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100569 // Allocate the new receiver object using the runtime call.
Ben Murdoch85b71792012-04-11 18:30:58 +0100570 // r1: constructor function
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100571 __ bind(&rt_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 if (create_memento) {
573 // Get the cell or allocation site.
574 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
575 __ push(r2);
576 }
577
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100578 __ push(r1); // argument for Runtime_NewObject
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579 if (create_memento) {
580 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 2);
581 } else {
582 __ CallRuntime(Runtime::kNewObject, 1);
583 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100584 __ mov(r4, r0);
585
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 // If we ended up using the runtime, and we want a memento, then the
587 // runtime call made it for us, and we shouldn't do create count
588 // increment.
589 Label count_incremented;
590 if (create_memento) {
591 __ jmp(&count_incremented);
592 }
593
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100594 // Receiver for constructor call allocated.
Steve Blocka7e24c12009-10-30 11:49:00 +0000595 // r4: JSObject
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100596 __ bind(&allocated);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597
598 if (create_memento) {
599 __ ldr(r2, MemOperand(sp, kPointerSize * 2));
600 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
601 __ cmp(r2, r5);
602 __ b(eq, &count_incremented);
603 // r2 is an AllocationSite. We are creating a memento from it, so we
604 // need to increment the memento create count.
605 __ ldr(r3, FieldMemOperand(r2,
606 AllocationSite::kPretenureCreateCountOffset));
607 __ add(r3, r3, Operand(Smi::FromInt(1)));
608 __ str(r3, FieldMemOperand(r2,
609 AllocationSite::kPretenureCreateCountOffset));
610 __ bind(&count_incremented);
611 }
612
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100613 __ push(r4);
614 __ push(r4);
Steve Blocka7e24c12009-10-30 11:49:00 +0000615
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100616 // Reload the number of arguments and the constructor from the stack.
617 // sp[0]: receiver
618 // sp[1]: receiver
619 // sp[2]: constructor function
620 // sp[3]: number of arguments (smi-tagged)
621 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
622 __ ldr(r3, MemOperand(sp, 3 * kPointerSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000623
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100624 // Set up pointer to last argument.
625 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000626
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100627 // Set up number of arguments for function call below
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 __ SmiUntag(r0, r3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000629
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100630 // Copy arguments and receiver to the expression stack.
631 // r0: number of arguments
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000632 // r1: constructor function
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100633 // r2: address of last argument (caller sp)
634 // r3: number of arguments (smi-tagged)
635 // sp[0]: receiver
636 // sp[1]: receiver
637 // sp[2]: constructor function
638 // sp[3]: number of arguments (smi-tagged)
639 Label loop, entry;
640 __ b(&entry);
641 __ bind(&loop);
642 __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
643 __ push(ip);
644 __ bind(&entry);
645 __ sub(r3, r3, Operand(2), SetCC);
646 __ b(ge, &loop);
647
648 // Call the function.
649 // r0: number of arguments
650 // r1: constructor function
651 if (is_api_function) {
652 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
653 Handle<Code> code =
654 masm->isolate()->builtins()->HandleApiCallConstruct();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 __ Call(code, RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100656 } else {
657 ParameterCount actual(r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +0000659 }
660
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100661 // Store offset of return address for deoptimizer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 if (!is_api_function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100663 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
664 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000665
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100666 // Restore context from the frame.
667 // r0: result
668 // sp[0]: receiver
669 // sp[1]: constructor function
670 // sp[2]: number of arguments (smi-tagged)
671 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000672
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100673 // If the result is an object (in the ECMA sense), we should get rid
674 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
675 // on page 74.
676 Label use_receiver, exit;
677
678 // If the result is a smi, it is *not* an object in the ECMA sense.
679 // r0: result
680 // sp[0]: receiver (newly allocated object)
681 // sp[1]: constructor function
682 // sp[2]: number of arguments (smi-tagged)
683 __ JumpIfSmi(r0, &use_receiver);
684
685 // If the type of the result (stored in its map) is less than
686 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000687 __ CompareObjectType(r0, r1, r3, FIRST_SPEC_OBJECT_TYPE);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100688 __ b(ge, &exit);
689
690 // Throw away the result of the constructor invocation and use the
691 // on-stack receiver as the result.
692 __ bind(&use_receiver);
693 __ ldr(r0, MemOperand(sp));
694
695 // Remove receiver from the stack, remove caller arguments, and
696 // return.
697 __ bind(&exit);
698 // r0: result
699 // sp[0]: receiver (newly allocated object)
700 // sp[1]: constructor function
701 // sp[2]: number of arguments (smi-tagged)
702 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
703
704 // Leave construct frame.
Steve Blocka7e24c12009-10-30 11:49:00 +0000705 }
706
Steve Blocka7e24c12009-10-30 11:49:00 +0000707 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
708 __ add(sp, sp, Operand(kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100709 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, r1, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 __ Jump(lr);
711}
712
713
Leon Clarkee46be812010-01-19 14:06:41 +0000714void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
Leon Clarkee46be812010-01-19 14:06:41 +0000716}
717
718
719void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100720 Generate_JSConstructStubHelper(masm, true, false);
Leon Clarkee46be812010-01-19 14:06:41 +0000721}
722
723
Steve Blocka7e24c12009-10-30 11:49:00 +0000724static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
725 bool is_construct) {
726 // Called from Generate_JS_Entry
727 // r0: code entry
728 // r1: function
729 // r2: receiver
730 // r3: argc
731 // r4: argv
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000732 // r5-r6, r8 (if not FLAG_enable_ool_constant_pool) and cp may be clobbered
733 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Steve Blocka7e24c12009-10-30 11:49:00 +0000734
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100735 // Clear the context before we push it when entering the internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000736 __ mov(cp, Operand::Zero());
Steve Blocka7e24c12009-10-30 11:49:00 +0000737
738 // Enter an internal frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100739 {
740 FrameScope scope(masm, StackFrame::INTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000741
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100742 // Set up the context from the function argument.
743 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000744
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100745 __ InitializeRootRegister();
Steve Blocka7e24c12009-10-30 11:49:00 +0000746
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100747 // Push the function and the receiver onto the stack.
748 __ push(r1);
749 __ push(r2);
Steve Blocka7e24c12009-10-30 11:49:00 +0000750
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100751 // Copy arguments to the stack in a loop.
752 // r1: function
753 // r3: argc
754 // r4: argv, i.e. points to first arg
755 Label loop, entry;
756 __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
757 // r2 points past last arg.
758 __ b(&entry);
759 __ bind(&loop);
760 __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
761 __ ldr(r0, MemOperand(r0)); // dereference handle
762 __ push(r0); // push parameter
763 __ bind(&entry);
764 __ cmp(r4, r2);
765 __ b(ne, &loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000766
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100767 // Initialize all JavaScript callee-saved registers, since they will be seen
768 // by the garbage collector as part of handlers.
769 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
770 __ mov(r5, Operand(r4));
771 __ mov(r6, Operand(r4));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 if (!FLAG_enable_ool_constant_pool) {
773 __ mov(r8, Operand(r4));
774 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100775 if (kR9Available == 1) {
776 __ mov(r9, Operand(r4));
777 }
778
779 // Invoke the code and pass argc as r0.
780 __ mov(r0, Operand(r3));
781 if (is_construct) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 // No type feedback cell is available
783 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
784 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100785 __ CallStub(&stub);
786 } else {
787 ParameterCount actual(r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100789 }
790 // Exit the JS frame and remove the parameters (except function), and
791 // return.
792 // Respect ABI stack constraint.
Steve Blocka7e24c12009-10-30 11:49:00 +0000793 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000794 __ Jump(lr);
795
796 // r0: result
797}
798
799
800void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
801 Generate_JSEntryTrampolineHelper(masm, false);
802}
803
804
805void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
806 Generate_JSEntryTrampolineHelper(masm, true);
807}
808
809
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000810void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
811 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
812 GenerateTailCallToReturnedCode(masm);
Iain Merrick75681382010-08-19 15:07:18 +0100813}
814
815
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
817 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
818 // Push a copy of the function onto the stack.
819 __ push(r1);
820 // Push function as parameter to the runtime call.
821 __ Push(r1);
822 // Whether to compile in a background thread.
823 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
824
825 __ CallRuntime(Runtime::kCompileOptimized, 2);
826 // Restore receiver.
827 __ pop(r1);
828}
829
830
831void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
832 CallCompileOptimized(masm, false);
833 GenerateTailCallToReturnedCode(masm);
834}
835
836
837void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
838 CallCompileOptimized(masm, true);
839 GenerateTailCallToReturnedCode(masm);
840}
841
842
843static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
844 // For now, we are relying on the fact that make_code_young doesn't do any
845 // garbage collection which allows us to save/restore the registers without
846 // worrying about which of them contain pointers. We also don't build an
847 // internal frame to make the code faster, since we shouldn't have to do stack
848 // crawls in MakeCodeYoung. This seems a bit fragile.
849
850 // The following registers must be saved and restored when calling through to
851 // the runtime:
852 // r0 - contains return address (beginning of patch sequence)
853 // r1 - isolate
854 FrameScope scope(masm, StackFrame::MANUAL);
855 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
856 __ PrepareCallCFunction(2, 0, r2);
857 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
858 __ CallCFunction(
859 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
860 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
861 __ mov(pc, r0);
862}
863
864#define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
865void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
866 MacroAssembler* masm) { \
867 GenerateMakeCodeYoungAgainCommon(masm); \
868} \
869void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
870 MacroAssembler* masm) { \
871 GenerateMakeCodeYoungAgainCommon(masm); \
872}
873CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
874#undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
875
876
877void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
878 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
879 // that make_code_young doesn't do any garbage collection which allows us to
880 // save/restore the registers without worrying about which of them contain
881 // pointers.
882
883 // The following registers must be saved and restored when calling through to
884 // the runtime:
885 // r0 - contains return address (beginning of patch sequence)
886 // r1 - isolate
887 FrameScope scope(masm, StackFrame::MANUAL);
888 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
889 __ PrepareCallCFunction(2, 0, r2);
890 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
891 __ CallCFunction(ExternalReference::get_mark_code_as_executed_function(
892 masm->isolate()), 2);
893 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
894
895 // Perform prologue operations usually performed by the young code stub.
896 __ PushFixedFrame(r1);
897 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
898
899 // Jump to point after the code-age stub.
900 __ add(r0, r0, Operand(kNoCodeAgeSequenceLength));
901 __ mov(pc, r0);
902}
903
904
905void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
906 GenerateMakeCodeYoungAgainCommon(masm);
907}
908
909
910static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
911 SaveFPRegsMode save_doubles) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100912 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000913 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100914
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 // Preserve registers across notification, this is important for compiled
916 // stubs that tail call the runtime on deopts passing their parameters in
917 // registers.
918 __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved);
919 // Pass the function and deoptimization type to the runtime system.
920 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
921 __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100922 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100923
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 __ add(sp, sp, Operand(kPointerSize)); // Ignore state
925 __ mov(pc, lr); // Jump to miss handler
926}
927
928
929void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
930 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
931}
932
933
934void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
935 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100936}
937
938
939static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
940 Deoptimizer::BailoutType type) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100941 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100943 // Pass the function and deoptimization type to the runtime system.
944 __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
945 __ push(r0);
946 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
947 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100948
949 // Get the full codegen state from the stack and untag it -> r6.
950 __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
951 __ SmiUntag(r6);
952 // Switch on the state.
953 Label with_tos_register, unknown_state;
954 __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
955 __ b(ne, &with_tos_register);
956 __ add(sp, sp, Operand(1 * kPointerSize)); // Remove state.
957 __ Ret();
958
959 __ bind(&with_tos_register);
960 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
961 __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
962 __ b(ne, &unknown_state);
963 __ add(sp, sp, Operand(2 * kPointerSize)); // Remove state.
964 __ Ret();
965
966 __ bind(&unknown_state);
967 __ stop("no cases left");
968}
969
970
971void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
972 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
973}
974
975
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000976void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
977 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
978}
979
980
Ben Murdochb0fe1622011-05-05 13:52:32 +0100981void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
982 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
983}
984
985
Ben Murdochb0fe1622011-05-05 13:52:32 +0100986void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000987 // Lookup the function in the JavaScript frame.
Steve Block1e0659c2011-05-24 12:43:12 +0100988 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100989 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
991 // Pass function as argument.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100992 __ push(r0);
993 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
994 }
Steve Block1e0659c2011-05-24 12:43:12 +0100995
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 // If the code object is null, just return to the unoptimized code.
Steve Block1e0659c2011-05-24 12:43:12 +0100997 Label skip;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998 __ cmp(r0, Operand(Smi::FromInt(0)));
Steve Block1e0659c2011-05-24 12:43:12 +0100999 __ b(ne, &skip);
1000 __ Ret();
1001
1002 __ bind(&skip);
Steve Block1e0659c2011-05-24 12:43:12 +01001003
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 // Load deoptimization data from the code object.
1005 // <deopt_data> = <code>[#deoptimization_data_offset]
1006 __ ldr(r1, FieldMemOperand(r0, Code::kDeoptimizationDataOffset));
1007
1008 { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
1009 if (FLAG_enable_ool_constant_pool) {
1010 __ ldr(pp, FieldMemOperand(r0, Code::kConstantPoolOffset));
1011 }
1012
1013 // Load the OSR entrypoint offset from the deoptimization data.
1014 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1015 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(
1016 DeoptimizationInputData::kOsrPcOffsetIndex)));
1017
1018 // Compute the target address = code_obj + header_size + osr_offset
1019 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1020 __ add(r0, r0, Operand::SmiUntag(r1));
1021 __ add(lr, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1022
1023 // And "return" to the OSR entry point of the function.
1024 __ Ret();
1025 }
1026}
1027
1028
1029void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1030 // We check the stack limit as indicator that recompilation might be done.
1031 Label ok;
1032 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1033 __ cmp(sp, Operand(ip));
1034 __ b(hs, &ok);
1035 {
1036 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1037 __ CallRuntime(Runtime::kStackGuard, 0);
1038 }
1039 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1040 RelocInfo::CODE_TARGET);
1041
1042 __ bind(&ok);
1043 __ Ret();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001044}
1045
1046
Steve Blocka7e24c12009-10-30 11:49:00 +00001047void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1048 // 1. Make sure we have at least one argument.
Andrei Popescu402d9372010-02-26 13:31:12 +00001049 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001050 { Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001051 __ cmp(r0, Operand::Zero());
Steve Blocka7e24c12009-10-30 11:49:00 +00001052 __ b(ne, &done);
1053 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1054 __ push(r2);
1055 __ add(r0, r0, Operand(1));
1056 __ bind(&done);
1057 }
1058
Andrei Popescu402d9372010-02-26 13:31:12 +00001059 // 2. Get the function to call (passed as receiver) from the stack, check
1060 // if it is a function.
1061 // r0: actual number of arguments
Ben Murdoch589d6972011-11-30 16:04:58 +00001062 Label slow, non_function;
Andrei Popescu402d9372010-02-26 13:31:12 +00001063 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001064 __ JumpIfSmi(r1, &non_function);
Andrei Popescu402d9372010-02-26 13:31:12 +00001065 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
Ben Murdoch589d6972011-11-30 16:04:58 +00001066 __ b(ne, &slow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001067
Andrei Popescu402d9372010-02-26 13:31:12 +00001068 // 3a. Patch the first argument if necessary when calling a function.
Steve Blocka7e24c12009-10-30 11:49:00 +00001069 // r0: actual number of arguments
1070 // r1: function
Andrei Popescu402d9372010-02-26 13:31:12 +00001071 Label shift_arguments;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 __ mov(r4, Operand::Zero()); // indicate regular JS_FUNCTION
1073 { Label convert_to_object, use_global_proxy, patch_receiver;
Andrei Popescu402d9372010-02-26 13:31:12 +00001074 // Change context eagerly in case we need the global receiver.
1075 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1076
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001077 // Do not transform the receiver for strict mode functions.
1078 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001079 __ ldr(r3, FieldMemOperand(r2, SharedFunctionInfo::kCompilerHintsOffset));
1080 __ tst(r3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1081 kSmiTagSize)));
1082 __ b(ne, &shift_arguments);
1083
1084 // Do not transform the receiver for native (Compilerhints already in r3).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001085 __ tst(r3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001086 __ b(ne, &shift_arguments);
1087
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001088 // Compute the receiver in sloppy mode.
Steve Blocka7e24c12009-10-30 11:49:00 +00001089 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1090 __ ldr(r2, MemOperand(r2, -kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001091 // r0: actual number of arguments
1092 // r1: function
1093 // r2: first argument
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001094 __ JumpIfSmi(r2, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001095
Steve Blocka7e24c12009-10-30 11:49:00 +00001096 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1097 __ cmp(r2, r3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001098 __ b(eq, &use_global_proxy);
Ben Murdoch257744e2011-11-30 15:57:28 +00001099 __ LoadRoot(r3, Heap::kNullValueRootIndex);
1100 __ cmp(r2, r3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001101 __ b(eq, &use_global_proxy);
Steve Blocka7e24c12009-10-30 11:49:00 +00001102
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001103 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1104 __ CompareObjectType(r2, r3, r3, FIRST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001105 __ b(ge, &shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001106
Andrei Popescu402d9372010-02-26 13:31:12 +00001107 __ bind(&convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +00001108
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001109 {
1110 // Enter an internal frame in order to preserve argument count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1112 __ SmiTag(r0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001113 __ push(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001114
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001115 __ push(r2);
1116 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1117 __ mov(r2, r0);
1118
1119 __ pop(r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120 __ SmiUntag(r0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001121
1122 // Exit the internal frame.
1123 }
1124
Ben Murdoch589d6972011-11-30 16:04:58 +00001125 // Restore the function to r1, and the flag to r4.
Andrei Popescu402d9372010-02-26 13:31:12 +00001126 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 __ mov(r4, Operand::Zero());
Andrei Popescu402d9372010-02-26 13:31:12 +00001128 __ jmp(&patch_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001129
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130 __ bind(&use_global_proxy);
1131 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1132 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalProxyOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001133
1134 __ bind(&patch_receiver);
1135 __ add(r3, sp, Operand(r0, LSL, kPointerSizeLog2));
1136 __ str(r2, MemOperand(r3, -kPointerSize));
1137
Andrei Popescu402d9372010-02-26 13:31:12 +00001138 __ jmp(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001139 }
1140
Ben Murdoch589d6972011-11-30 16:04:58 +00001141 // 3b. Check for function proxy.
1142 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143 __ mov(r4, Operand(1, RelocInfo::NONE32)); // indicate function proxy
Ben Murdoch589d6972011-11-30 16:04:58 +00001144 __ cmp(r2, Operand(JS_FUNCTION_PROXY_TYPE));
1145 __ b(eq, &shift_arguments);
1146 __ bind(&non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 __ mov(r4, Operand(2, RelocInfo::NONE32)); // indicate non-function
Ben Murdoch589d6972011-11-30 16:04:58 +00001148
1149 // 3c. Patch the first argument when calling a non-function. The
Andrei Popescu402d9372010-02-26 13:31:12 +00001150 // CALL_NON_FUNCTION builtin expects the non-function callee as
1151 // receiver, so overwrite the first argument which will ultimately
1152 // become the receiver.
1153 // r0: actual number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001154 // r1: function
Ben Murdoch589d6972011-11-30 16:04:58 +00001155 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
Andrei Popescu402d9372010-02-26 13:31:12 +00001156 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1157 __ str(r1, MemOperand(r2, -kPointerSize));
Andrei Popescu402d9372010-02-26 13:31:12 +00001158
1159 // 4. Shift arguments and return address one slot down on the stack
1160 // (overwriting the original receiver). Adjust argument count to make
1161 // the original first argument the new receiver.
1162 // r0: actual number of arguments
1163 // r1: function
Ben Murdoch589d6972011-11-30 16:04:58 +00001164 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
Andrei Popescu402d9372010-02-26 13:31:12 +00001165 __ bind(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +00001166 { Label loop;
1167 // Calculate the copy start address (destination). Copy end address is sp.
1168 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
Steve Blocka7e24c12009-10-30 11:49:00 +00001169
1170 __ bind(&loop);
1171 __ ldr(ip, MemOperand(r2, -kPointerSize));
1172 __ str(ip, MemOperand(r2));
1173 __ sub(r2, r2, Operand(kPointerSize));
1174 __ cmp(r2, sp);
1175 __ b(ne, &loop);
Andrei Popescu402d9372010-02-26 13:31:12 +00001176 // Adjust the actual number of arguments and remove the top element
1177 // (which is a copy of the last argument).
1178 __ sub(r0, r0, Operand(1));
1179 __ pop();
Steve Blocka7e24c12009-10-30 11:49:00 +00001180 }
1181
Ben Murdoch589d6972011-11-30 16:04:58 +00001182 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1183 // or a function proxy via CALL_FUNCTION_PROXY.
Steve Blocka7e24c12009-10-30 11:49:00 +00001184 // r0: actual number of arguments
1185 // r1: function
Ben Murdoch589d6972011-11-30 16:04:58 +00001186 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1187 { Label function, non_proxy;
1188 __ tst(r4, r4);
1189 __ b(eq, &function);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001190 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001191 __ mov(r2, Operand::Zero());
Ben Murdoch589d6972011-11-30 16:04:58 +00001192 __ cmp(r4, Operand(1));
1193 __ b(ne, &non_proxy);
1194
1195 __ push(r1); // re-add proxy object as additional argument
1196 __ add(r0, r0, Operand(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001197 __ GetBuiltinFunction(r1, Builtins::CALL_FUNCTION_PROXY);
Ben Murdoch589d6972011-11-30 16:04:58 +00001198 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1199 RelocInfo::CODE_TARGET);
1200
1201 __ bind(&non_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001202 __ GetBuiltinFunction(r1, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01001203 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1204 RelocInfo::CODE_TARGET);
Andrei Popescu402d9372010-02-26 13:31:12 +00001205 __ bind(&function);
Steve Blocka7e24c12009-10-30 11:49:00 +00001206 }
Andrei Popescu402d9372010-02-26 13:31:12 +00001207
1208 // 5b. Get the code to call from the function and check that the number of
1209 // expected arguments matches what we're providing. If so, jump
1210 // (tail-call) to the code in register edx without checking arguments.
1211 // r0: actual number of arguments
1212 // r1: function
1213 __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1214 __ ldr(r2,
1215 FieldMemOperand(r3, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001216 __ SmiUntag(r2);
Andrei Popescu402d9372010-02-26 13:31:12 +00001217 __ cmp(r2, r0); // Check formal and actual parameter counts.
Steve Block44f0eee2011-05-26 01:26:41 +01001218 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1219 RelocInfo::CODE_TARGET,
1220 ne);
Andrei Popescu402d9372010-02-26 13:31:12 +00001221
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Andrei Popescu402d9372010-02-26 13:31:12 +00001223 ParameterCount expected(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 __ InvokeCode(r3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +00001225}
1226
1227
1228void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229 const int kIndexOffset =
1230 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1231 const int kLimitOffset =
1232 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1233 const int kArgsOffset = 2 * kPointerSize;
1234 const int kRecvOffset = 3 * kPointerSize;
1235 const int kFunctionOffset = 4 * kPointerSize;
Steve Blocka7e24c12009-10-30 11:49:00 +00001236
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001237 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 FrameAndConstantPoolScope frame_scope(masm, StackFrame::INTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +00001239
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001240 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1241 __ push(r0);
1242 __ ldr(r0, MemOperand(fp, kArgsOffset)); // get the args array
1243 __ push(r0);
1244 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +00001245
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001246 // Check the stack for overflow. We are not trying to catch
1247 // interruptions (e.g. debug break and preemption) here, so the "real stack
1248 // limit" is checked.
1249 Label okay;
1250 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
1251 // Make r2 the space we have left. The stack might already be overflowed
1252 // here which will cause r2 to become negative.
1253 __ sub(r2, sp, r2);
1254 // Check if the arguments will overflow the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 __ cmp(r2, Operand::PointerOffsetFromSmiKey(r0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 __ b(gt, &okay); // Signed comparison.
Steve Blocka7e24c12009-10-30 11:49:00 +00001257
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001258 // Out of stack space.
1259 __ ldr(r1, MemOperand(fp, kFunctionOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 __ Push(r1, r0);
1261 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001262 // End of stack check.
Steve Blocka7e24c12009-10-30 11:49:00 +00001263
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001264 // Push current limit and index.
1265 __ bind(&okay);
1266 __ push(r0); // limit
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001267 __ mov(r1, Operand::Zero()); // initial index
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001268 __ push(r1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001269
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001270 // Get the receiver.
1271 __ ldr(r0, MemOperand(fp, kRecvOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001272
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001273 // Check that the function is a JS function (otherwise it must be a proxy).
1274 Label push_receiver;
1275 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1276 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1277 __ b(ne, &push_receiver);
Ben Murdoch589d6972011-11-30 16:04:58 +00001278
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001279 // Change context eagerly to get the right global object if necessary.
1280 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1281 // Load the shared function info while the function is still in r1.
1282 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +00001283
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001284 // Compute the receiver.
1285 // Do not transform the receiver for strict mode functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001286 Label call_to_object, use_global_proxy;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001287 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCompilerHintsOffset));
1288 __ tst(r2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1289 kSmiTagSize)));
1290 __ b(ne, &push_receiver);
Ben Murdoch257744e2011-11-30 15:57:28 +00001291
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001292 // Do not transform the receiver for strict mode functions.
1293 __ tst(r2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1294 __ b(ne, &push_receiver);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001295
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001296 // Compute the receiver in sloppy mode.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001297 __ JumpIfSmi(r0, &call_to_object);
1298 __ LoadRoot(r1, Heap::kNullValueRootIndex);
1299 __ cmp(r0, r1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001300 __ b(eq, &use_global_proxy);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001301 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1302 __ cmp(r0, r1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001303 __ b(eq, &use_global_proxy);
Steve Blocka7e24c12009-10-30 11:49:00 +00001304
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001305 // Check if the receiver is already a JavaScript object.
1306 // r0: receiver
1307 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1308 __ CompareObjectType(r0, r1, r1, FIRST_SPEC_OBJECT_TYPE);
1309 __ b(ge, &push_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001310
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001311 // Convert the receiver to a regular object.
1312 // r0: receiver
1313 __ bind(&call_to_object);
1314 __ push(r0);
1315 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1316 __ b(&push_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +00001317
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 __ bind(&use_global_proxy);
1319 __ ldr(r0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1320 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalProxyOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001321
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001322 // Push the receiver.
1323 // r0: receiver
1324 __ bind(&push_receiver);
1325 __ push(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001326
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001327 // Copy all arguments from the array to the stack.
1328 Label entry, loop;
1329 __ ldr(r0, MemOperand(fp, kIndexOffset));
1330 __ b(&entry);
Steve Blocka7e24c12009-10-30 11:49:00 +00001331
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001332 // Load the current argument from the arguments array and push it to the
1333 // stack.
1334 // r0: current argument index
1335 __ bind(&loop);
1336 __ ldr(r1, MemOperand(fp, kArgsOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337 __ Push(r1, r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001338
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001339 // Call the runtime to access the property in the arguments array.
1340 __ CallRuntime(Runtime::kGetProperty, 2);
1341 __ push(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001342
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001343 // Use inline caching to access the arguments.
1344 __ ldr(r0, MemOperand(fp, kIndexOffset));
1345 __ add(r0, r0, Operand(1 << kSmiTagSize));
1346 __ str(r0, MemOperand(fp, kIndexOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +00001347
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001348 // Test if the copy loop has finished copying all the elements from the
1349 // arguments object.
1350 __ bind(&entry);
1351 __ ldr(r1, MemOperand(fp, kLimitOffset));
1352 __ cmp(r0, r1);
1353 __ b(ne, &loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00001354
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001355 // Call the function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001356 Label call_proxy;
1357 ParameterCount actual(r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358 __ SmiUntag(r0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001359 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1360 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1361 __ b(ne, &call_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +00001363
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001364 frame_scope.GenerateLeaveFrame();
1365 __ add(sp, sp, Operand(3 * kPointerSize));
1366 __ Jump(lr);
Ben Murdoch589d6972011-11-30 16:04:58 +00001367
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001368 // Call the function proxy.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001369 __ bind(&call_proxy);
1370 __ push(r1); // add function proxy as last argument
1371 __ add(r0, r0, Operand(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001372 __ mov(r2, Operand::Zero());
1373 __ GetBuiltinFunction(r1, Builtins::CALL_FUNCTION_PROXY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001374 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1375 RelocInfo::CODE_TARGET);
Ben Murdoch589d6972011-11-30 16:04:58 +00001376
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001377 // Tear down the internal frame and remove function, receiver and args.
1378 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001379 __ add(sp, sp, Operand(3 * kPointerSize));
1380 __ Jump(lr);
Steve Blocka7e24c12009-10-30 11:49:00 +00001381}
1382
1383
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1385 Label* stack_overflow) {
1386 // ----------- S t a t e -------------
1387 // -- r0 : actual number of arguments
1388 // -- r1 : function (passed through to callee)
1389 // -- r2 : expected number of arguments
1390 // -----------------------------------
1391 // Check the stack for overflow. We are not trying to catch
1392 // interruptions (e.g. debug break and preemption) here, so the "real stack
1393 // limit" is checked.
1394 __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
1395 // Make r5 the space we have left. The stack might already be overflowed
1396 // here which will cause r5 to become negative.
1397 __ sub(r5, sp, r5);
1398 // Check if the arguments will overflow the stack.
1399 __ cmp(r5, Operand(r2, LSL, kPointerSizeLog2));
1400 __ b(le, stack_overflow); // Signed comparison.
1401}
1402
1403
Steve Blocka7e24c12009-10-30 11:49:00 +00001404static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 __ SmiTag(r0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001406 __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() |
1408 (FLAG_enable_ool_constant_pool ? pp.bit() : 0) |
1409 fp.bit() | lr.bit());
1410 __ add(fp, sp,
1411 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001412}
1413
1414
1415static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1416 // ----------- S t a t e -------------
1417 // -- r0 : result being passed through
1418 // -----------------------------------
1419 // Get the number of arguments passed (as a smi), tear down the frame and
1420 // then tear down the parameters.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421 __ ldr(r1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1422 kPointerSize)));
1423
1424 __ LeaveFrame(StackFrame::ARGUMENTS_ADAPTOR);
1425 __ add(sp, sp, Operand::PointerOffsetFromSmiKey(r1));
Steve Blocka7e24c12009-10-30 11:49:00 +00001426 __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1427}
1428
1429
1430void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1431 // ----------- S t a t e -------------
1432 // -- r0 : actual number of arguments
1433 // -- r1 : function (passed through to callee)
1434 // -- r2 : expected number of arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001435 // -----------------------------------
1436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 Label stack_overflow;
1438 ArgumentAdaptorStackCheck(masm, &stack_overflow);
Steve Blocka7e24c12009-10-30 11:49:00 +00001439 Label invoke, dont_adapt_arguments;
1440
1441 Label enough, too_few;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001442 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
Steve Block6ded16b2010-05-10 14:33:55 +01001443 __ cmp(r0, r2);
Steve Blocka7e24c12009-10-30 11:49:00 +00001444 __ b(lt, &too_few);
1445 __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1446 __ b(eq, &dont_adapt_arguments);
1447
1448 { // Enough parameters: actual >= expected
1449 __ bind(&enough);
1450 EnterArgumentsAdaptorFrame(masm);
1451
1452 // Calculate copy start address into r0 and copy end address into r2.
1453 // r0: actual number of arguments as a smi
1454 // r1: function
1455 // r2: expected number of arguments
1456 // r3: code entry to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001457 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001458 // adjust for return address and receiver
1459 __ add(r0, r0, Operand(2 * kPointerSize));
1460 __ sub(r2, r0, Operand(r2, LSL, kPointerSizeLog2));
1461
1462 // Copy the arguments (including the receiver) to the new stack frame.
1463 // r0: copy start address
1464 // r1: function
1465 // r2: copy end address
1466 // r3: code entry to call
1467
1468 Label copy;
1469 __ bind(&copy);
1470 __ ldr(ip, MemOperand(r0, 0));
1471 __ push(ip);
1472 __ cmp(r0, r2); // Compare before moving to next argument.
1473 __ sub(r0, r0, Operand(kPointerSize));
1474 __ b(ne, &copy);
1475
1476 __ b(&invoke);
1477 }
1478
1479 { // Too few parameters: Actual < expected
1480 __ bind(&too_few);
1481 EnterArgumentsAdaptorFrame(masm);
1482
1483 // Calculate copy start address into r0 and copy end address is fp.
1484 // r0: actual number of arguments as a smi
1485 // r1: function
1486 // r2: expected number of arguments
1487 // r3: code entry to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
Steve Blocka7e24c12009-10-30 11:49:00 +00001489
1490 // Copy the arguments (including the receiver) to the new stack frame.
1491 // r0: copy start address
1492 // r1: function
1493 // r2: expected number of arguments
1494 // r3: code entry to call
1495 Label copy;
1496 __ bind(&copy);
1497 // Adjust load for return address and receiver.
1498 __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1499 __ push(ip);
1500 __ cmp(r0, fp); // Compare before moving to next argument.
1501 __ sub(r0, r0, Operand(kPointerSize));
1502 __ b(ne, &copy);
1503
1504 // Fill the remaining expected arguments with undefined.
1505 // r1: function
1506 // r2: expected number of arguments
1507 // r3: code entry to call
1508 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1509 __ sub(r2, fp, Operand(r2, LSL, kPointerSizeLog2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001510 // Adjust for frame.
1511 __ sub(r2, r2, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
1512 2 * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +00001513
1514 Label fill;
1515 __ bind(&fill);
1516 __ push(ip);
1517 __ cmp(sp, r2);
1518 __ b(ne, &fill);
1519 }
1520
1521 // Call the entry point.
1522 __ bind(&invoke);
1523 __ Call(r3);
1524
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001525 // Store offset of return address for deoptimizer.
1526 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1527
Steve Blocka7e24c12009-10-30 11:49:00 +00001528 // Exit frame and return.
1529 LeaveArgumentsAdaptorFrame(masm);
1530 __ Jump(lr);
1531
1532
1533 // -------------------------------------------
1534 // Dont adapt arguments.
1535 // -------------------------------------------
1536 __ bind(&dont_adapt_arguments);
1537 __ Jump(r3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001538
1539 __ bind(&stack_overflow);
1540 {
1541 FrameScope frame(masm, StackFrame::MANUAL);
1542 EnterArgumentsAdaptorFrame(masm);
1543 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1544 __ bkpt(0);
1545 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001546}
1547
1548
1549#undef __
1550
1551} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001552
1553#endif // V8_TARGET_ARCH_ARM