blob: 98fd57de784e5aff57a4ec70e01c602ca58a66ff [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Andrei Popescu31002712010-02-23 13:46:05 +00002// 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
29
30#include "v8.h"
31
Leon Clarkef7060e22010-06-03 12:02:55 +010032#if defined(V8_TARGET_ARCH_MIPS)
33
Ben Murdoch257744e2011-11-30 15:57:28 +000034#include "codegen.h"
Andrei Popescu31002712010-02-23 13:46:05 +000035#include "debug.h"
Steve Block44f0eee2011-05-26 01:26:41 +010036#include "deoptimizer.h"
37#include "full-codegen.h"
Andrei Popescu31002712010-02-23 13:46:05 +000038#include "runtime.h"
39
40namespace v8 {
41namespace internal {
42
43
44#define __ ACCESS_MASM(masm)
45
46
47void Builtins::Generate_Adaptor(MacroAssembler* masm,
48 CFunctionId id,
49 BuiltinExtraArguments extra_args) {
Ben Murdoch257744e2011-11-30 15:57:28 +000050 // ----------- S t a t e -------------
51 // -- a0 : number of arguments excluding receiver
52 // -- a1 : called function (only guaranteed when
53 // -- extra_args requires it)
54 // -- cp : context
55 // -- sp[0] : last argument
56 // -- ...
57 // -- sp[4 * (argc - 1)] : first argument
58 // -- sp[4 * agrc] : receiver
59 // -----------------------------------
60
61 // Insert extra arguments.
62 int num_extra_args = 0;
63 if (extra_args == NEEDS_CALLED_FUNCTION) {
64 num_extra_args = 1;
65 __ push(a1);
66 } else {
67 ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
68 }
69
70 // JumpToExternalReference expects a0 to contain the number of arguments
71 // including the receiver and the extra arguments.
72 __ Addu(a0, a0, Operand(num_extra_args + 1));
73 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
74}
75
76
77// Load the built-in Array function from the current context.
78static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
79 // Load the global context.
80
81 __ lw(result, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
82 __ lw(result,
83 FieldMemOperand(result, GlobalObject::kGlobalContextOffset));
84 // Load the Array function from the global context.
85 __ lw(result,
86 MemOperand(result,
87 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
88}
89
90
Ben Murdoch257744e2011-11-30 15:57:28 +000091// Allocate an empty JSArray. The allocated array is put into the result
92// register. An elements backing store is allocated with size initial_capacity
93// and filled with the hole values.
94static void AllocateEmptyJSArray(MacroAssembler* masm,
95 Register array_function,
96 Register result,
97 Register scratch1,
98 Register scratch2,
99 Register scratch3,
Ben Murdoch257744e2011-11-30 15:57:28 +0000100 Label* gc_required) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000101 const int initial_capacity = JSArray::kPreallocatedArrayElements;
102 STATIC_ASSERT(initial_capacity >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000103 // Load the initial map from the array function.
104 __ lw(scratch1, FieldMemOperand(array_function,
105 JSFunction::kPrototypeOrInitialMapOffset));
106
107 // Allocate the JSArray object together with space for a fixed array with the
108 // requested elements.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000109 int size = JSArray::kSize;
110 if (initial_capacity > 0) {
111 size += FixedArray::SizeFor(initial_capacity);
112 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000113 __ AllocateInNewSpace(size,
114 result,
115 scratch2,
116 scratch3,
117 gc_required,
118 TAG_OBJECT);
119 // Allocated the JSArray. Now initialize the fields except for the elements
120 // array.
121 // result: JSObject
122 // scratch1: initial map
123 // scratch2: start of next object
124 __ sw(scratch1, FieldMemOperand(result, JSObject::kMapOffset));
125 __ LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
126 __ sw(scratch1, FieldMemOperand(result, JSArray::kPropertiesOffset));
127 // Field JSArray::kElementsOffset is initialized later.
128 __ mov(scratch3, zero_reg);
129 __ sw(scratch3, FieldMemOperand(result, JSArray::kLengthOffset));
130
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000131 if (initial_capacity == 0) {
132 __ sw(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
133 return;
134 }
135
Ben Murdoch257744e2011-11-30 15:57:28 +0000136 // Calculate the location of the elements array and set elements array member
137 // of the JSArray.
138 // result: JSObject
139 // scratch2: start of next object
140 __ Addu(scratch1, result, Operand(JSArray::kSize));
141 __ sw(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
142
143 // Clear the heap tag on the elements array.
144 __ And(scratch1, scratch1, Operand(~kHeapObjectTagMask));
145
146 // Initialize the FixedArray and fill it with holes. FixedArray length is
147 // stored as a smi.
148 // result: JSObject
149 // scratch1: elements array (untagged)
150 // scratch2: start of next object
151 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000152 STATIC_ASSERT(0 * kPointerSize == FixedArray::kMapOffset);
Ben Murdoch257744e2011-11-30 15:57:28 +0000153 __ sw(scratch3, MemOperand(scratch1));
154 __ Addu(scratch1, scratch1, kPointerSize);
155 __ li(scratch3, Operand(Smi::FromInt(initial_capacity)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000156 STATIC_ASSERT(1 * kPointerSize == FixedArray::kLengthOffset);
Ben Murdoch257744e2011-11-30 15:57:28 +0000157 __ sw(scratch3, MemOperand(scratch1));
158 __ Addu(scratch1, scratch1, kPointerSize);
159
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000160 // Fill the FixedArray with the hole value. Inline the code if short.
161 STATIC_ASSERT(2 * kPointerSize == FixedArray::kHeaderSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000162 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000163 static const int kLoopUnfoldLimit = 4;
164 if (initial_capacity <= kLoopUnfoldLimit) {
165 for (int i = 0; i < initial_capacity; i++) {
166 __ sw(scratch3, MemOperand(scratch1, i * kPointerSize));
167 }
168 } else {
169 Label loop, entry;
170 __ Addu(scratch2, scratch1, Operand(initial_capacity * kPointerSize));
171 __ Branch(&entry);
172 __ bind(&loop);
Ben Murdoch257744e2011-11-30 15:57:28 +0000173 __ sw(scratch3, MemOperand(scratch1));
174 __ Addu(scratch1, scratch1, kPointerSize);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000175 __ bind(&entry);
176 __ Branch(&loop, lt, scratch1, Operand(scratch2));
Ben Murdoch257744e2011-11-30 15:57:28 +0000177 }
178}
179
180
181// Allocate a JSArray with the number of elements stored in a register. The
182// register array_function holds the built-in Array function and the register
183// array_size holds the size of the array as a smi. The allocated array is put
184// into the result register and beginning and end of the FixedArray elements
185// storage is put into registers elements_array_storage and elements_array_end
186// (see below for when that is not the case). If the parameter fill_with_holes
187// is true the allocated elements backing store is filled with the hole values
188// otherwise it is left uninitialized. When the backing store is filled the
189// register elements_array_storage is scratched.
190static void AllocateJSArray(MacroAssembler* masm,
191 Register array_function, // Array function.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000192 Register array_size, // As a smi, cannot be 0.
Ben Murdoch257744e2011-11-30 15:57:28 +0000193 Register result,
194 Register elements_array_storage,
195 Register elements_array_end,
196 Register scratch1,
197 Register scratch2,
198 bool fill_with_hole,
199 Label* gc_required) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000200 // Load the initial map from the array function.
201 __ lw(elements_array_storage,
202 FieldMemOperand(array_function,
203 JSFunction::kPrototypeOrInitialMapOffset));
204
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000205 if (FLAG_debug_code) { // Assert that array size is not zero.
206 __ Assert(
207 ne, "array size is unexpectedly 0", array_size, Operand(zero_reg));
208 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000209
210 // Allocate the JSArray object together with space for a FixedArray with the
211 // requested number of elements.
Ben Murdoch589d6972011-11-30 16:04:58 +0000212 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000213 __ li(elements_array_end,
214 (JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize);
215 __ sra(scratch1, array_size, kSmiTagSize);
216 __ Addu(elements_array_end, elements_array_end, scratch1);
217 __ AllocateInNewSpace(
218 elements_array_end,
219 result,
220 scratch1,
221 scratch2,
222 gc_required,
223 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
224
225 // Allocated the JSArray. Now initialize the fields except for the elements
226 // array.
227 // result: JSObject
228 // elements_array_storage: initial map
229 // array_size: size of array (smi)
Ben Murdoch257744e2011-11-30 15:57:28 +0000230 __ sw(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset));
231 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex);
232 __ sw(elements_array_storage,
233 FieldMemOperand(result, JSArray::kPropertiesOffset));
234 // Field JSArray::kElementsOffset is initialized later.
235 __ sw(array_size, FieldMemOperand(result, JSArray::kLengthOffset));
236
237 // Calculate the location of the elements array and set elements array member
238 // of the JSArray.
239 // result: JSObject
240 // array_size: size of array (smi)
241 __ Addu(elements_array_storage, result, Operand(JSArray::kSize));
242 __ sw(elements_array_storage,
243 FieldMemOperand(result, JSArray::kElementsOffset));
244
245 // Clear the heap tag on the elements array.
246 __ And(elements_array_storage,
247 elements_array_storage,
248 Operand(~kHeapObjectTagMask));
249 // Initialize the fixed array and fill it with holes. FixedArray length is
250 // stored as a smi.
251 // result: JSObject
252 // elements_array_storage: elements array (untagged)
253 // array_size: size of array (smi)
254 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex);
255 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
256 __ sw(scratch1, MemOperand(elements_array_storage));
257 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
258
259 // Length of the FixedArray is the number of pre-allocated elements if
260 // the actual JSArray has length 0 and the size of the JSArray for non-empty
261 // JSArrays. The length of a FixedArray is stored as a smi.
Ben Murdoch589d6972011-11-30 16:04:58 +0000262 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000263
264 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
265 __ sw(array_size, MemOperand(elements_array_storage));
266 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
267
268 // Calculate elements array and elements array end.
269 // result: JSObject
270 // elements_array_storage: elements array element storage
271 // array_size: smi-tagged size of elements array
Ben Murdoch589d6972011-11-30 16:04:58 +0000272 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000273 __ sll(elements_array_end, array_size, kPointerSizeLog2 - kSmiTagSize);
274 __ Addu(elements_array_end, elements_array_storage, elements_array_end);
275
276 // Fill the allocated FixedArray with the hole value if requested.
277 // result: JSObject
278 // elements_array_storage: elements array element storage
279 // elements_array_end: start of next object
280 if (fill_with_hole) {
281 Label loop, entry;
282 __ LoadRoot(scratch1, Heap::kTheHoleValueRootIndex);
283 __ Branch(&entry);
284 __ bind(&loop);
285 __ sw(scratch1, MemOperand(elements_array_storage));
286 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
287
288 __ bind(&entry);
289 __ Branch(&loop, lt, elements_array_storage, Operand(elements_array_end));
290 }
291}
292
293
294// Create a new array for the built-in Array function. This function allocates
295// the JSArray object and the FixedArray elements array and initializes these.
296// If the Array cannot be constructed in native code the runtime is called. This
297// function assumes the following state:
298// a0: argc
299// a1: constructor (built-in Array function)
300// ra: return address
301// sp[0]: last argument
302// This function is used for both construct and normal calls of Array. The only
303// difference between handling a construct call and a normal call is that for a
304// construct call the constructor function in a1 needs to be preserved for
305// entering the generic code. In both cases argc in a0 needs to be preserved.
306// Both registers are preserved by this code so no need to differentiate between
307// construct call and normal call.
308static void ArrayNativeCode(MacroAssembler* masm,
309 Label* call_generic_code) {
310 Counters* counters = masm->isolate()->counters();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000311 Label argc_one_or_more, argc_two_or_more, not_empty_array, empty_array;
Ben Murdoch257744e2011-11-30 15:57:28 +0000312
313 // Check for array construction with zero arguments or one.
314 __ Branch(&argc_one_or_more, ne, a0, Operand(zero_reg));
315 // Handle construction of an empty array.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000316 __ bind(&empty_array);
Ben Murdoch257744e2011-11-30 15:57:28 +0000317 AllocateEmptyJSArray(masm,
318 a1,
319 a2,
320 a3,
321 t0,
322 t1,
Ben Murdoch257744e2011-11-30 15:57:28 +0000323 call_generic_code);
324 __ IncrementCounter(counters->array_function_native(), 1, a3, t0);
325 // Setup return value, remove receiver from stack and return.
326 __ mov(v0, a2);
327 __ Addu(sp, sp, Operand(kPointerSize));
328 __ Ret();
329
330 // Check for one argument. Bail out if argument is not smi or if it is
331 // negative.
332 __ bind(&argc_one_or_more);
333 __ Branch(&argc_two_or_more, ne, a0, Operand(1));
334
Ben Murdoch589d6972011-11-30 16:04:58 +0000335 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000336 __ lw(a2, MemOperand(sp)); // Get the argument from the stack.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000337 __ Branch(&not_empty_array, ne, a2, Operand(zero_reg));
338 __ Drop(1); // Adjust stack.
339 __ mov(a0, zero_reg); // Treat this as a call with argc of zero.
340 __ Branch(&empty_array);
341
342 __ bind(&not_empty_array);
Ben Murdoch257744e2011-11-30 15:57:28 +0000343 __ And(a3, a2, Operand(kIntptrSignBit | kSmiTagMask));
344 __ Branch(call_generic_code, eq, a3, Operand(zero_reg));
345
346 // Handle construction of an empty array of a certain size. Bail out if size
347 // is too large to actually allocate an elements array.
Ben Murdoch589d6972011-11-30 16:04:58 +0000348 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000349 __ Branch(call_generic_code, Ugreater_equal, a2,
Ben Murdoch257744e2011-11-30 15:57:28 +0000350 Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize));
351
352 // a0: argc
353 // a1: constructor
354 // a2: array_size (smi)
355 // sp[0]: argument
356 AllocateJSArray(masm,
357 a1,
358 a2,
359 a3,
360 t0,
361 t1,
362 t2,
363 t3,
364 true,
365 call_generic_code);
366 __ IncrementCounter(counters->array_function_native(), 1, a2, t0);
367
368 // Setup return value, remove receiver and argument from stack and return.
369 __ mov(v0, a3);
370 __ Addu(sp, sp, Operand(2 * kPointerSize));
371 __ Ret();
372
373 // Handle construction of an array from a list of arguments.
374 __ bind(&argc_two_or_more);
375 __ sll(a2, a0, kSmiTagSize); // Convert argc to a smi.
376
377 // a0: argc
378 // a1: constructor
379 // a2: array_size (smi)
380 // sp[0]: last argument
381 AllocateJSArray(masm,
382 a1,
383 a2,
384 a3,
385 t0,
386 t1,
387 t2,
388 t3,
389 false,
390 call_generic_code);
391 __ IncrementCounter(counters->array_function_native(), 1, a2, t2);
392
393 // Fill arguments as array elements. Copy from the top of the stack (last
394 // element) to the array backing store filling it backwards. Note:
395 // elements_array_end points after the backing store.
396 // a0: argc
397 // a3: JSArray
398 // t0: elements_array storage start (untagged)
399 // t1: elements_array_end (untagged)
400 // sp[0]: last argument
401
402 Label loop, entry;
403 __ Branch(&entry);
404 __ bind(&loop);
405 __ pop(a2);
406 __ Addu(t1, t1, -kPointerSize);
407 __ sw(a2, MemOperand(t1));
408 __ bind(&entry);
409 __ Branch(&loop, lt, t0, Operand(t1));
410
411 // Remove caller arguments and receiver from the stack, setup return value and
412 // return.
413 // a0: argc
414 // a3: JSArray
415 // sp[0]: receiver
416 __ Addu(sp, sp, Operand(kPointerSize));
417 __ mov(v0, a3);
418 __ Ret();
Andrei Popescu31002712010-02-23 13:46:05 +0000419}
420
421
422void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000423 // ----------- S t a t e -------------
424 // -- a0 : number of arguments
425 // -- ra : return address
426 // -- sp[...]: constructor arguments
427 // -----------------------------------
428 Label generic_array_code;
429
430 // Get the Array function.
431 GenerateLoadArrayFunction(masm, a1);
432
433 if (FLAG_debug_code) {
434 // Initial map for the builtin Array functions should be maps.
435 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
436 __ And(t0, a2, Operand(kSmiTagMask));
437 __ Assert(ne, "Unexpected initial map for Array function (1)",
438 t0, Operand(zero_reg));
439 __ GetObjectType(a2, a3, t0);
440 __ Assert(eq, "Unexpected initial map for Array function (2)",
441 t0, Operand(MAP_TYPE));
442 }
443
444 // Run the native code for the Array function called as a normal function.
445 ArrayNativeCode(masm, &generic_array_code);
446
447 // Jump to the generic array code if the specialized code cannot handle
448 // the construction.
449 __ bind(&generic_array_code);
450
451 Handle<Code> array_code =
452 masm->isolate()->builtins()->ArrayCodeGeneric();
453 __ Jump(array_code, RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +0000454}
455
456
457void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000458 // ----------- S t a t e -------------
459 // -- a0 : number of arguments
460 // -- a1 : constructor function
461 // -- ra : return address
462 // -- sp[...]: constructor arguments
463 // -----------------------------------
464 Label generic_constructor;
465
466 if (FLAG_debug_code) {
467 // The array construct code is only set for the builtin and internal
468 // Array functions which always have a map.
469 // Initial map for the builtin Array function should be a map.
470 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
471 __ And(t0, a2, Operand(kSmiTagMask));
472 __ Assert(ne, "Unexpected initial map for Array function (3)",
473 t0, Operand(zero_reg));
474 __ GetObjectType(a2, a3, t0);
475 __ Assert(eq, "Unexpected initial map for Array function (4)",
476 t0, Operand(MAP_TYPE));
477 }
478
479 // Run the native code for the Array function called as a constructor.
480 ArrayNativeCode(masm, &generic_constructor);
481
482 // Jump to the generic construct code in case the specialized code cannot
483 // handle the construction.
484 __ bind(&generic_constructor);
485
486 Handle<Code> generic_construct_stub =
487 masm->isolate()->builtins()->JSConstructStubGeneric();
488 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +0000489}
490
491
Steve Block44f0eee2011-05-26 01:26:41 +0100492void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000493 // ----------- S t a t e -------------
494 // -- a0 : number of arguments
495 // -- a1 : constructor function
496 // -- ra : return address
497 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
498 // -- sp[argc * 4] : receiver
499 // -----------------------------------
500 Counters* counters = masm->isolate()->counters();
501 __ IncrementCounter(counters->string_ctor_calls(), 1, a2, a3);
502
503 Register function = a1;
504 if (FLAG_debug_code) {
505 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, a2);
506 __ Assert(eq, "Unexpected String function", function, Operand(a2));
507 }
508
509 // Load the first arguments in a0 and get rid of the rest.
510 Label no_arguments;
511 __ Branch(&no_arguments, eq, a0, Operand(zero_reg));
512 // First args = sp[(argc - 1) * 4].
513 __ Subu(a0, a0, Operand(1));
514 __ sll(a0, a0, kPointerSizeLog2);
515 __ Addu(sp, a0, sp);
516 __ lw(a0, MemOperand(sp));
517 // sp now point to args[0], drop args[0] + receiver.
518 __ Drop(2);
519
520 Register argument = a2;
521 Label not_cached, argument_is_string;
522 NumberToStringStub::GenerateLookupNumberStringCache(
523 masm,
524 a0, // Input.
525 argument, // Result.
526 a3, // Scratch.
527 t0, // Scratch.
528 t1, // Scratch.
529 false, // Is it a Smi?
530 &not_cached);
531 __ IncrementCounter(counters->string_ctor_cached_number(), 1, a3, t0);
532 __ bind(&argument_is_string);
533
534 // ----------- S t a t e -------------
535 // -- a2 : argument converted to string
536 // -- a1 : constructor function
537 // -- ra : return address
538 // -----------------------------------
539
540 Label gc_required;
541 __ AllocateInNewSpace(JSValue::kSize,
542 v0, // Result.
543 a3, // Scratch.
544 t0, // Scratch.
545 &gc_required,
546 TAG_OBJECT);
547
548 // Initialising the String Object.
549 Register map = a3;
550 __ LoadGlobalFunctionInitialMap(function, map, t0);
551 if (FLAG_debug_code) {
552 __ lbu(t0, FieldMemOperand(map, Map::kInstanceSizeOffset));
553 __ Assert(eq, "Unexpected string wrapper instance size",
554 t0, Operand(JSValue::kSize >> kPointerSizeLog2));
555 __ lbu(t0, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
556 __ Assert(eq, "Unexpected unused properties of string wrapper",
557 t0, Operand(zero_reg));
558 }
559 __ sw(map, FieldMemOperand(v0, HeapObject::kMapOffset));
560
561 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
562 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
563 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
564
565 __ sw(argument, FieldMemOperand(v0, JSValue::kValueOffset));
566
567 // Ensure the object is fully initialized.
568 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
569
570 __ Ret();
571
572 // The argument was not found in the number to string cache. Check
573 // if it's a string already before calling the conversion builtin.
574 Label convert_argument;
575 __ bind(&not_cached);
576 __ JumpIfSmi(a0, &convert_argument);
577
578 // Is it a String?
579 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
580 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +0000581 STATIC_ASSERT(kNotStringTag != 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000582 __ And(t0, a3, Operand(kIsNotStringMask));
583 __ Branch(&convert_argument, ne, t0, Operand(zero_reg));
584 __ mov(argument, a0);
585 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
586 __ Branch(&argument_is_string);
587
588 // Invoke the conversion builtin and put the result into a2.
589 __ bind(&convert_argument);
590 __ push(function); // Preserve the function.
591 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000592 {
593 FrameScope scope(masm, StackFrame::INTERNAL);
594 __ push(v0);
595 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
596 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000597 __ pop(function);
598 __ mov(argument, v0);
599 __ Branch(&argument_is_string);
600
601 // Load the empty string into a2, remove the receiver from the
602 // stack, and jump back to the case where the argument is a string.
603 __ bind(&no_arguments);
604 __ LoadRoot(argument, Heap::kEmptyStringRootIndex);
605 __ Drop(1);
606 __ Branch(&argument_is_string);
607
608 // At this point the argument is already a string. Call runtime to
609 // create a string wrapper.
610 __ bind(&gc_required);
611 __ IncrementCounter(counters->string_ctor_gc_required(), 1, a3, t0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000612 {
613 FrameScope scope(masm, StackFrame::INTERNAL);
614 __ push(argument);
615 __ CallRuntime(Runtime::kNewStringWrapper, 1);
616 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000617 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100618}
619
620
Andrei Popescu31002712010-02-23 13:46:05 +0000621void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000622 // ----------- S t a t e -------------
623 // -- a0 : number of arguments
624 // -- a1 : constructor function
625 // -- ra : return address
626 // -- sp[...]: constructor arguments
627 // -----------------------------------
628
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000629 Label slow, non_function_call;
Ben Murdoch257744e2011-11-30 15:57:28 +0000630 // Check that the function is not a smi.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000631 __ JumpIfSmi(a1, &non_function_call);
Ben Murdoch257744e2011-11-30 15:57:28 +0000632 // Check that the function is a JSFunction.
633 __ GetObjectType(a1, a2, a2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000634 __ Branch(&slow, ne, a2, Operand(JS_FUNCTION_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000635
636 // Jump to the function-specific construct stub.
637 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
638 __ lw(a2, FieldMemOperand(a2, SharedFunctionInfo::kConstructStubOffset));
639 __ Addu(t9, a2, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000640 __ Jump(t9);
Ben Murdoch257744e2011-11-30 15:57:28 +0000641
642 // a0: number of arguments
643 // a1: called object
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000644 // a2: object type
645 Label do_call;
646 __ bind(&slow);
647 __ Branch(&non_function_call, ne, a2, Operand(JS_FUNCTION_PROXY_TYPE));
648 __ GetBuiltinEntry(a3, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
649 __ jmp(&do_call);
650
Ben Murdoch257744e2011-11-30 15:57:28 +0000651 __ bind(&non_function_call);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000652 __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
653 __ bind(&do_call);
Ben Murdoch257744e2011-11-30 15:57:28 +0000654 // CALL_NON_FUNCTION expects the non-function constructor as receiver
655 // (instead of the original receiver from the call site). The receiver is
656 // stack element argc.
657 // Set expected number of arguments to zero (not changing a0).
658 __ mov(a2, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +0000659 __ SetCallKind(t1, CALL_AS_METHOD);
660 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
661 RelocInfo::CODE_TARGET);
662}
663
664
665static void Generate_JSConstructStubHelper(MacroAssembler* masm,
666 bool is_api_function,
667 bool count_constructions) {
668 // Should never count constructions for api objects.
669 ASSERT(!is_api_function || !count_constructions);
670
671 Isolate* isolate = masm->isolate();
672
673 // ----------- S t a t e -------------
674 // -- a0 : number of arguments
675 // -- a1 : constructor function
676 // -- ra : return address
677 // -- sp[...]: constructor arguments
678 // -----------------------------------
679
680 // Enter a construct frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000681 {
682 FrameScope scope(masm, StackFrame::CONSTRUCT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000683
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000684 // Preserve the two incoming parameters on the stack.
685 __ sll(a0, a0, kSmiTagSize); // Tag arguments count.
686 __ MultiPushReversed(a0.bit() | a1.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +0000687
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000688 // Use t7 to hold undefined, which is used in several places below.
689 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +0000690
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000691 Label rt_call, allocated;
692 // Try to allocate the object without transitioning into C code. If any of
693 // the preconditions is not met, the code bails out to the runtime call.
694 if (FLAG_inline_new) {
695 Label undo_allocation;
Ben Murdoch257744e2011-11-30 15:57:28 +0000696#ifdef ENABLE_DEBUGGER_SUPPORT
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000697 ExternalReference debug_step_in_fp =
698 ExternalReference::debug_step_in_fp_address(isolate);
699 __ li(a2, Operand(debug_step_in_fp));
700 __ lw(a2, MemOperand(a2));
701 __ Branch(&rt_call, ne, a2, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000702#endif
703
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000704 // Load the initial map and verify that it is in fact a map.
705 // a1: constructor function
706 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
707 __ JumpIfSmi(a2, &rt_call);
708 __ GetObjectType(a2, a3, t4);
709 __ Branch(&rt_call, ne, t4, Operand(MAP_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000710
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000711 // Check that the constructor is not constructing a JSFunction (see
712 // comments in Runtime_NewObject in runtime.cc). In which case the
713 // initial map's instance type would be JS_FUNCTION_TYPE.
714 // a1: constructor function
715 // a2: initial map
716 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
717 __ Branch(&rt_call, eq, a3, Operand(JS_FUNCTION_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000718
Ben Murdoch257744e2011-11-30 15:57:28 +0000719 if (count_constructions) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000720 Label allocate;
721 // Decrease generous allocation count.
722 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
723 MemOperand constructor_count =
724 FieldMemOperand(a3, SharedFunctionInfo::kConstructionCountOffset);
725 __ lbu(t0, constructor_count);
726 __ Subu(t0, t0, Operand(1));
727 __ sb(t0, constructor_count);
728 __ Branch(&allocate, ne, t0, Operand(zero_reg));
729
730 __ Push(a1, a2);
731
732 __ push(a1); // Constructor.
733 // The call will replace the stub, so the countdown is only done once.
734 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
735
736 __ pop(a2);
737 __ pop(a1);
738
739 __ bind(&allocate);
740 }
741
742 // Now allocate the JSObject on the heap.
743 // a1: constructor function
744 // a2: initial map
745 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
746 __ AllocateInNewSpace(a3, t4, t5, t6, &rt_call, SIZE_IN_WORDS);
747
748 // Allocated the JSObject, now initialize the fields. Map is set to
749 // initial map and properties and elements are set to empty fixed array.
750 // a1: constructor function
751 // a2: initial map
752 // a3: object size
753 // t4: JSObject (not tagged)
754 __ LoadRoot(t6, Heap::kEmptyFixedArrayRootIndex);
755 __ mov(t5, t4);
756 __ sw(a2, MemOperand(t5, JSObject::kMapOffset));
757 __ sw(t6, MemOperand(t5, JSObject::kPropertiesOffset));
758 __ sw(t6, MemOperand(t5, JSObject::kElementsOffset));
759 __ Addu(t5, t5, Operand(3*kPointerSize));
760 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
761 ASSERT_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
762 ASSERT_EQ(2 * kPointerSize, JSObject::kElementsOffset);
763
764 // Fill all the in-object properties with appropriate filler.
765 // a1: constructor function
766 // a2: initial map
767 // a3: object size (in words)
768 // t4: JSObject (not tagged)
769 // t5: First in-object property of JSObject (not tagged)
770 __ sll(t0, a3, kPointerSizeLog2);
771 __ addu(t6, t4, t0); // End of object.
772 ASSERT_EQ(3 * kPointerSize, JSObject::kHeaderSize);
773 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
774 if (count_constructions) {
775 __ lw(a0, FieldMemOperand(a2, Map::kInstanceSizesOffset));
776 __ Ext(a0, a0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
777 kBitsPerByte);
778 __ sll(t0, a0, kPointerSizeLog2);
779 __ addu(a0, t5, t0);
780 // a0: offset of first field after pre-allocated fields
781 if (FLAG_debug_code) {
782 __ Assert(le, "Unexpected number of pre-allocated property fields.",
783 a0, Operand(t6));
784 }
785 __ InitializeFieldsWithFiller(t5, a0, t7);
Ben Murdoch257744e2011-11-30 15:57:28 +0000786 // To allow for truncation.
787 __ LoadRoot(t7, Heap::kOnePointerFillerMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +0000788 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000789 __ InitializeFieldsWithFiller(t5, t6, t7);
790
791 // Add the object tag to make the JSObject real, so that we can continue
792 // and jump into the continuation code at any time from now on. Any
793 // failures need to undo the allocation, so that the heap is in a
794 // consistent state and verifiable.
795 __ Addu(t4, t4, Operand(kHeapObjectTag));
796
797 // Check if a non-empty properties array is needed. Continue with
798 // allocated object if not fall through to runtime call if it is.
799 // a1: constructor function
800 // t4: JSObject
801 // t5: start of next object (not tagged)
802 __ lbu(a3, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
803 // The field instance sizes contains both pre-allocated property fields
804 // and in-object properties.
805 __ lw(a0, FieldMemOperand(a2, Map::kInstanceSizesOffset));
806 __ Ext(t6, a0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
807 kBitsPerByte);
808 __ Addu(a3, a3, Operand(t6));
809 __ Ext(t6, a0, Map::kInObjectPropertiesByte * kBitsPerByte,
810 kBitsPerByte);
811 __ subu(a3, a3, t6);
812
813 // Done if no extra properties are to be allocated.
814 __ Branch(&allocated, eq, a3, Operand(zero_reg));
815 __ Assert(greater_equal, "Property allocation count failed.",
816 a3, Operand(zero_reg));
817
818 // Scale the number of elements by pointer size and add the header for
819 // FixedArrays to the start of the next object calculation from above.
820 // a1: constructor
821 // a3: number of elements in properties array
822 // t4: JSObject
823 // t5: start of next object
824 __ Addu(a0, a3, Operand(FixedArray::kHeaderSize / kPointerSize));
825 __ AllocateInNewSpace(
826 a0,
827 t5,
828 t6,
829 a2,
830 &undo_allocation,
831 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
832
833 // Initialize the FixedArray.
834 // a1: constructor
835 // a3: number of elements in properties array (un-tagged)
836 // t4: JSObject
837 // t5: start of next object
838 __ LoadRoot(t6, Heap::kFixedArrayMapRootIndex);
839 __ mov(a2, t5);
840 __ sw(t6, MemOperand(a2, JSObject::kMapOffset));
841 __ sll(a0, a3, kSmiTagSize);
842 __ sw(a0, MemOperand(a2, FixedArray::kLengthOffset));
843 __ Addu(a2, a2, Operand(2 * kPointerSize));
844
845 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
846 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
847
848 // Initialize the fields to undefined.
849 // a1: constructor
850 // a2: First element of FixedArray (not tagged)
851 // a3: number of elements in properties array
852 // t4: JSObject
853 // t5: FixedArray (not tagged)
854 __ sll(t3, a3, kPointerSizeLog2);
855 __ addu(t6, a2, t3); // End of object.
856 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
857 { Label loop, entry;
858 if (count_constructions) {
859 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
860 } else if (FLAG_debug_code) {
861 __ LoadRoot(t8, Heap::kUndefinedValueRootIndex);
862 __ Assert(eq, "Undefined value not loaded.", t7, Operand(t8));
863 }
864 __ jmp(&entry);
865 __ bind(&loop);
866 __ sw(t7, MemOperand(a2));
867 __ addiu(a2, a2, kPointerSize);
868 __ bind(&entry);
869 __ Branch(&loop, less, a2, Operand(t6));
870 }
871
872 // Store the initialized FixedArray into the properties field of
873 // the JSObject.
874 // a1: constructor function
875 // t4: JSObject
876 // t5: FixedArray (not tagged)
877 __ Addu(t5, t5, Operand(kHeapObjectTag)); // Add the heap tag.
878 __ sw(t5, FieldMemOperand(t4, JSObject::kPropertiesOffset));
879
880 // Continue with JSObject being successfully allocated.
881 // a1: constructor function
882 // a4: JSObject
883 __ jmp(&allocated);
884
885 // Undo the setting of the new top so that the heap is verifiable. For
886 // example, the map's unused properties potentially do not match the
887 // allocated objects unused properties.
888 // t4: JSObject (previous new top)
889 __ bind(&undo_allocation);
890 __ UndoAllocationInNewSpace(t4, t5);
Ben Murdoch257744e2011-11-30 15:57:28 +0000891 }
892
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000893 __ bind(&rt_call);
894 // Allocate the new receiver object using the runtime call.
Ben Murdoch257744e2011-11-30 15:57:28 +0000895 // a1: constructor function
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000896 __ push(a1); // Argument for Runtime_NewObject.
897 __ CallRuntime(Runtime::kNewObject, 1);
898 __ mov(t4, v0);
899
900 // Receiver for constructor call allocated.
Ben Murdoch257744e2011-11-30 15:57:28 +0000901 // t4: JSObject
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000902 __ bind(&allocated);
903 __ push(t4);
Ben Murdoch257744e2011-11-30 15:57:28 +0000904
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000905 // Push the function and the allocated receiver from the stack.
906 // sp[0]: receiver (newly allocated object)
907 // sp[1]: constructor function
908 // sp[2]: number of arguments (smi-tagged)
909 __ lw(a1, MemOperand(sp, kPointerSize));
910 __ MultiPushReversed(a1.bit() | t4.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +0000911
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000912 // Reload the number of arguments from the stack.
913 // a1: constructor function
914 // sp[0]: receiver
915 // sp[1]: constructor function
916 // sp[2]: receiver
917 // sp[3]: constructor function
918 // sp[4]: number of arguments (smi-tagged)
919 __ lw(a3, MemOperand(sp, 4 * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +0000920
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000921 // Setup pointer to last argument.
922 __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000923
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000924 // Setup number of arguments for function call below.
925 __ srl(a0, a3, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000926
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000927 // Copy arguments and receiver to the expression stack.
928 // a0: number of arguments
929 // a1: constructor function
930 // a2: address of last argument (caller sp)
931 // a3: number of arguments (smi-tagged)
932 // sp[0]: receiver
933 // sp[1]: constructor function
934 // sp[2]: receiver
935 // sp[3]: constructor function
936 // sp[4]: number of arguments (smi-tagged)
937 Label loop, entry;
938 __ jmp(&entry);
939 __ bind(&loop);
940 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
941 __ Addu(t0, a2, Operand(t0));
942 __ lw(t1, MemOperand(t0));
943 __ push(t1);
944 __ bind(&entry);
945 __ Addu(a3, a3, Operand(-2));
946 __ Branch(&loop, greater_equal, a3, Operand(zero_reg));
947
948 // Call the function.
949 // a0: number of arguments
950 // a1: constructor function
951 if (is_api_function) {
952 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
953 Handle<Code> code =
954 masm->isolate()->builtins()->HandleApiCallConstruct();
955 ParameterCount expected(0);
956 __ InvokeCode(code, expected, expected,
957 RelocInfo::CODE_TARGET, CALL_FUNCTION, CALL_AS_METHOD);
958 } else {
959 ParameterCount actual(a0);
960 __ InvokeFunction(a1, actual, CALL_FUNCTION,
961 NullCallWrapper(), CALL_AS_METHOD);
Ben Murdoch257744e2011-11-30 15:57:28 +0000962 }
963
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000964 // Pop the function from the stack.
965 // v0: result
966 // sp[0]: constructor function
967 // sp[2]: receiver
968 // sp[3]: constructor function
969 // sp[4]: number of arguments (smi-tagged)
970 __ Pop();
Ben Murdoch257744e2011-11-30 15:57:28 +0000971
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000972 // Restore context from the frame.
973 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000974
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000975 // If the result is an object (in the ECMA sense), we should get rid
976 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
977 // on page 74.
978 Label use_receiver, exit;
979
980 // If the result is a smi, it is *not* an object in the ECMA sense.
981 // v0: result
982 // sp[0]: receiver (newly allocated object)
983 // sp[1]: constructor function
984 // sp[2]: number of arguments (smi-tagged)
985 __ JumpIfSmi(v0, &use_receiver);
986
987 // If the type of the result (stored in its map) is less than
988 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
989 __ GetObjectType(v0, a3, a3);
990 __ Branch(&exit, greater_equal, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
991
992 // Throw away the result of the constructor invocation and use the
993 // on-stack receiver as the result.
994 __ bind(&use_receiver);
995 __ lw(v0, MemOperand(sp));
996
997 // Remove receiver from the stack, remove caller arguments, and
998 // return.
999 __ bind(&exit);
1000 // v0: result
1001 // sp[0]: receiver (newly allocated object)
1002 // sp[1]: constructor function
1003 // sp[2]: number of arguments (smi-tagged)
1004 __ lw(a1, MemOperand(sp, 2 * kPointerSize));
1005
1006 // Leave construct frame.
Ben Murdoch257744e2011-11-30 15:57:28 +00001007 }
1008
Ben Murdoch257744e2011-11-30 15:57:28 +00001009 __ sll(t0, a1, kPointerSizeLog2 - 1);
1010 __ Addu(sp, sp, t0);
1011 __ Addu(sp, sp, kPointerSize);
1012 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
1013 __ Ret();
Andrei Popescu31002712010-02-23 13:46:05 +00001014}
1015
1016
Steve Block44f0eee2011-05-26 01:26:41 +01001017void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001018 Generate_JSConstructStubHelper(masm, false, true);
Steve Block44f0eee2011-05-26 01:26:41 +01001019}
1020
1021
Andrei Popescu31002712010-02-23 13:46:05 +00001022void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001023 Generate_JSConstructStubHelper(masm, false, false);
Andrei Popescu31002712010-02-23 13:46:05 +00001024}
1025
1026
1027void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001028 Generate_JSConstructStubHelper(masm, true, false);
1029}
1030
1031
1032static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
1033 bool is_construct) {
1034 // Called from JSEntryStub::GenerateBody
1035
1036 // ----------- S t a t e -------------
1037 // -- a0: code entry
1038 // -- a1: function
1039 // -- a2: reveiver_pointer
1040 // -- a3: argc
1041 // -- s0: argv
1042 // -----------------------------------
1043
1044 // Clear the context before we push it when entering the JS frame.
1045 __ mov(cp, zero_reg);
1046
1047 // Enter an internal frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001048 {
1049 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch257744e2011-11-30 15:57:28 +00001050
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001051 // Set up the context from the function argument.
1052 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001053
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001054 // Set up the roots register.
1055 ExternalReference roots_array_start =
1056 ExternalReference::roots_array_start(masm->isolate());
1057 __ li(s6, Operand(roots_array_start));
Ben Murdoch257744e2011-11-30 15:57:28 +00001058
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001059 // Push the function and the receiver onto the stack.
1060 __ Push(a1, a2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001061
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001062 // Copy arguments to the stack in a loop.
1063 // a3: argc
1064 // s0: argv, ie points to first arg
1065 Label loop, entry;
1066 __ sll(t0, a3, kPointerSizeLog2);
1067 __ addu(t2, s0, t0);
1068 __ b(&entry);
1069 __ nop(); // Branch delay slot nop.
1070 // t2 points past last arg.
1071 __ bind(&loop);
1072 __ lw(t0, MemOperand(s0)); // Read next parameter.
1073 __ addiu(s0, s0, kPointerSize);
1074 __ lw(t0, MemOperand(t0)); // Dereference handle.
1075 __ push(t0); // Push parameter.
1076 __ bind(&entry);
1077 __ Branch(&loop, ne, s0, Operand(t2));
Ben Murdoch257744e2011-11-30 15:57:28 +00001078
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001079 // Initialize all JavaScript callee-saved registers, since they will be seen
1080 // by the garbage collector as part of handlers.
1081 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
1082 __ mov(s1, t0);
1083 __ mov(s2, t0);
1084 __ mov(s3, t0);
1085 __ mov(s4, t0);
1086 __ mov(s5, t0);
1087 // s6 holds the root address. Do not clobber.
1088 // s7 is cp. Do not init.
Ben Murdoch257744e2011-11-30 15:57:28 +00001089
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001090 // Invoke the code and pass argc as a0.
1091 __ mov(a0, a3);
1092 if (is_construct) {
1093 __ Call(masm->isolate()->builtins()->JSConstructCall());
1094 } else {
1095 ParameterCount actual(a0);
1096 __ InvokeFunction(a1, actual, CALL_FUNCTION,
1097 NullCallWrapper(), CALL_AS_METHOD);
1098 }
1099
1100 // Leave internal frame.
Ben Murdoch257744e2011-11-30 15:57:28 +00001101 }
1102
Ben Murdoch257744e2011-11-30 15:57:28 +00001103 __ Jump(ra);
Andrei Popescu31002712010-02-23 13:46:05 +00001104}
1105
1106
Andrei Popescu31002712010-02-23 13:46:05 +00001107void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001108 Generate_JSEntryTrampolineHelper(masm, false);
Andrei Popescu31002712010-02-23 13:46:05 +00001109}
1110
1111
1112void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001113 Generate_JSEntryTrampolineHelper(masm, true);
Steve Block44f0eee2011-05-26 01:26:41 +01001114}
1115
1116
1117void Builtins::Generate_LazyCompile(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001118 // Enter an internal frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001119 {
1120 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch257744e2011-11-30 15:57:28 +00001121
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001122 // Preserve the function.
1123 __ push(a1);
1124 // Push call kind information.
1125 __ push(t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001126
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001127 // Push the function on the stack as the argument to the runtime function.
1128 __ push(a1);
1129 // Call the runtime function.
1130 __ CallRuntime(Runtime::kLazyCompile, 1);
1131 // Calculate the entry point.
1132 __ addiu(t9, v0, Code::kHeaderSize - kHeapObjectTag);
Ben Murdoch257744e2011-11-30 15:57:28 +00001133
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001134 // Restore call kind information.
1135 __ pop(t1);
1136 // Restore saved function.
1137 __ pop(a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001138
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001139 // Tear down temporary frame.
1140 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001141
1142 // Do a tail-call of the compiled function.
1143 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001144}
1145
1146
1147void Builtins::Generate_LazyRecompile(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001148 // Enter an internal frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001149 {
1150 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch257744e2011-11-30 15:57:28 +00001151
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001152 // Preserve the function.
1153 __ push(a1);
1154 // Push call kind information.
1155 __ push(t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001156
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001157 // Push the function on the stack as the argument to the runtime function.
1158 __ push(a1);
1159 __ CallRuntime(Runtime::kLazyRecompile, 1);
1160 // Calculate the entry point.
1161 __ Addu(t9, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00001162
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001163 // Restore call kind information.
1164 __ pop(t1);
1165 // Restore saved function.
1166 __ pop(a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001167
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001168 // Tear down temporary frame.
1169 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001170
1171 // Do a tail-call of the compiled function.
1172 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001173}
1174
1175
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001176static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1177 Deoptimizer::BailoutType type) {
1178 {
1179 FrameScope scope(masm, StackFrame::INTERNAL);
1180 // Pass the function and deoptimization type to the runtime system.
1181 __ li(a0, Operand(Smi::FromInt(static_cast<int>(type))));
1182 __ push(a0);
1183 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1184 }
1185
1186 // Get the full codegen state from the stack and untag it -> t2.
1187 __ lw(t2, MemOperand(sp, 0 * kPointerSize));
1188 __ SmiUntag(t2);
1189 // Switch on the state.
1190 Label with_tos_register, unknown_state;
1191 __ Branch(&with_tos_register,
1192 ne, t2, Operand(FullCodeGenerator::NO_REGISTERS));
1193 __ Addu(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1194 __ Ret();
1195
1196 __ bind(&with_tos_register);
1197 __ lw(v0, MemOperand(sp, 1 * kPointerSize));
1198 __ Branch(&unknown_state, ne, t2, Operand(FullCodeGenerator::TOS_REG));
1199
1200 __ Addu(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1201 __ Ret();
1202
1203 __ bind(&unknown_state);
1204 __ stop("no cases left");
1205}
1206
1207
Steve Block44f0eee2011-05-26 01:26:41 +01001208void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001209 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
Steve Block44f0eee2011-05-26 01:26:41 +01001210}
1211
1212
1213void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001214 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
Steve Block44f0eee2011-05-26 01:26:41 +01001215}
1216
1217
1218void Builtins::Generate_NotifyOSR(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001219 // For now, we are relying on the fact that Runtime::NotifyOSR
1220 // doesn't do any garbage collection which allows us to save/restore
1221 // the registers without worrying about which of them contain
1222 // pointers. This seems a bit fragile.
1223 RegList saved_regs =
1224 (kJSCallerSaved | kCalleeSaved | ra.bit() | fp.bit()) & ~sp.bit();
1225 __ MultiPush(saved_regs);
1226 {
1227 FrameScope scope(masm, StackFrame::INTERNAL);
1228 __ CallRuntime(Runtime::kNotifyOSR, 0);
1229 }
1230 __ MultiPop(saved_regs);
1231 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01001232}
1233
1234
1235void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001236 CpuFeatures::TryForceFeatureScope scope(VFP3);
1237 if (!CpuFeatures::IsSupported(FPU)) {
1238 __ Abort("Unreachable code: Cannot optimize without FPU support.");
1239 return;
1240 }
1241
1242 // Lookup the function in the JavaScript frame and push it as an
1243 // argument to the on-stack replacement function.
1244 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1245 {
1246 FrameScope scope(masm, StackFrame::INTERNAL);
1247 __ push(a0);
1248 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1249 }
1250
1251 // If the result was -1 it means that we couldn't optimize the
1252 // function. Just return and continue in the unoptimized version.
1253 __ Ret(eq, v0, Operand(Smi::FromInt(-1)));
1254
1255 // Untag the AST id and push it on the stack.
1256 __ SmiUntag(v0);
1257 __ push(v0);
1258
1259 // Generate the code for doing the frame-to-frame translation using
1260 // the deoptimizer infrastructure.
1261 Deoptimizer::EntryGenerator generator(masm, Deoptimizer::OSR);
1262 generator.Generate();
Andrei Popescu31002712010-02-23 13:46:05 +00001263}
1264
1265
1266void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001267 // 1. Make sure we have at least one argument.
1268 // a0: actual number of arguments
1269 { Label done;
1270 __ Branch(&done, ne, a0, Operand(zero_reg));
1271 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
1272 __ push(t2);
1273 __ Addu(a0, a0, Operand(1));
1274 __ bind(&done);
1275 }
1276
1277 // 2. Get the function to call (passed as receiver) from the stack, check
1278 // if it is a function.
1279 // a0: actual number of arguments
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001280 Label slow, non_function;
Ben Murdoch257744e2011-11-30 15:57:28 +00001281 __ sll(at, a0, kPointerSizeLog2);
1282 __ addu(at, sp, at);
1283 __ lw(a1, MemOperand(at));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001284 __ JumpIfSmi(a1, &non_function);
Ben Murdoch257744e2011-11-30 15:57:28 +00001285 __ GetObjectType(a1, a2, a2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001286 __ Branch(&slow, ne, a2, Operand(JS_FUNCTION_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001287
1288 // 3a. Patch the first argument if necessary when calling a function.
1289 // a0: actual number of arguments
1290 // a1: function
1291 Label shift_arguments;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001292 __ li(t0, Operand(0, RelocInfo::NONE)); // Indicate regular JS_FUNCTION.
Ben Murdoch257744e2011-11-30 15:57:28 +00001293 { Label convert_to_object, use_global_receiver, patch_receiver;
1294 // Change context eagerly in case we need the global receiver.
1295 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1296
1297 // Do not transform the receiver for strict mode functions.
1298 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1299 __ lw(a3, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001300 __ And(t3, a3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
Ben Murdoch257744e2011-11-30 15:57:28 +00001301 kSmiTagSize)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001302 __ Branch(&shift_arguments, ne, t3, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00001303
1304 // Do not transform the receiver for native (Compilerhints already in a3).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001305 __ And(t3, a3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1306 __ Branch(&shift_arguments, ne, t3, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00001307
1308 // Compute the receiver in non-strict mode.
1309 // Load first argument in a2. a2 = -kPointerSize(sp + n_args << 2).
1310 __ sll(at, a0, kPointerSizeLog2);
1311 __ addu(a2, sp, at);
1312 __ lw(a2, MemOperand(a2, -kPointerSize));
1313 // a0: actual number of arguments
1314 // a1: function
1315 // a2: first argument
1316 __ JumpIfSmi(a2, &convert_to_object, t2);
1317
1318 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1319 __ Branch(&use_global_receiver, eq, a2, Operand(a3));
1320 __ LoadRoot(a3, Heap::kNullValueRootIndex);
1321 __ Branch(&use_global_receiver, eq, a2, Operand(a3));
1322
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001323 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001324 __ GetObjectType(a2, a3, a3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001325 __ Branch(&shift_arguments, ge, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001326
1327 __ bind(&convert_to_object);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001328 // Enter an internal frame in order to preserve argument count.
1329 {
1330 FrameScope scope(masm, StackFrame::INTERNAL);
1331 __ sll(a0, a0, kSmiTagSize); // Smi tagged.
1332 __ push(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001333
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001334 __ push(a2);
1335 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1336 __ mov(a2, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001337
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001338 __ pop(a0);
1339 __ sra(a0, a0, kSmiTagSize); // Un-tag.
1340 // Leave internal frame.
1341 }
1342 // Restore the function to a1, and the flag to t0.
Ben Murdoch257744e2011-11-30 15:57:28 +00001343 __ sll(at, a0, kPointerSizeLog2);
1344 __ addu(at, sp, at);
1345 __ lw(a1, MemOperand(at));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001346 __ li(t0, Operand(0, RelocInfo::NONE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001347 __ Branch(&patch_receiver);
1348
1349 // Use the global receiver object from the called function as the
1350 // receiver.
1351 __ bind(&use_global_receiver);
1352 const int kGlobalIndex =
1353 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1354 __ lw(a2, FieldMemOperand(cp, kGlobalIndex));
1355 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalContextOffset));
1356 __ lw(a2, FieldMemOperand(a2, kGlobalIndex));
1357 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalReceiverOffset));
1358
1359 __ bind(&patch_receiver);
1360 __ sll(at, a0, kPointerSizeLog2);
1361 __ addu(a3, sp, at);
1362 __ sw(a2, MemOperand(a3, -kPointerSize));
1363
1364 __ Branch(&shift_arguments);
1365 }
1366
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001367 // 3b. Check for function proxy.
1368 __ bind(&slow);
1369 __ li(t0, Operand(1, RelocInfo::NONE)); // Indicate function proxy.
1370 __ Branch(&shift_arguments, eq, a2, Operand(JS_FUNCTION_PROXY_TYPE));
1371
1372 __ bind(&non_function);
1373 __ li(t0, Operand(2, RelocInfo::NONE)); // Indicate non-function.
1374
1375 // 3c. Patch the first argument when calling a non-function. The
Ben Murdoch257744e2011-11-30 15:57:28 +00001376 // CALL_NON_FUNCTION builtin expects the non-function callee as
1377 // receiver, so overwrite the first argument which will ultimately
1378 // become the receiver.
1379 // a0: actual number of arguments
1380 // a1: function
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001381 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
Ben Murdoch257744e2011-11-30 15:57:28 +00001382 __ sll(at, a0, kPointerSizeLog2);
1383 __ addu(a2, sp, at);
1384 __ sw(a1, MemOperand(a2, -kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001385
1386 // 4. Shift arguments and return address one slot down on the stack
1387 // (overwriting the original receiver). Adjust argument count to make
1388 // the original first argument the new receiver.
1389 // a0: actual number of arguments
1390 // a1: function
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001391 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
Ben Murdoch257744e2011-11-30 15:57:28 +00001392 __ bind(&shift_arguments);
1393 { Label loop;
1394 // Calculate the copy start address (destination). Copy end address is sp.
1395 __ sll(at, a0, kPointerSizeLog2);
1396 __ addu(a2, sp, at);
1397
1398 __ bind(&loop);
1399 __ lw(at, MemOperand(a2, -kPointerSize));
1400 __ sw(at, MemOperand(a2));
1401 __ Subu(a2, a2, Operand(kPointerSize));
1402 __ Branch(&loop, ne, a2, Operand(sp));
1403 // Adjust the actual number of arguments and remove the top element
1404 // (which is a copy of the last argument).
1405 __ Subu(a0, a0, Operand(1));
1406 __ Pop();
1407 }
1408
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001409 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1410 // or a function proxy via CALL_FUNCTION_PROXY.
Ben Murdoch257744e2011-11-30 15:57:28 +00001411 // a0: actual number of arguments
1412 // a1: function
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001413 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
1414 { Label function, non_proxy;
1415 __ Branch(&function, eq, t0, Operand(zero_reg));
1416 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1417 __ mov(a2, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001418 __ SetCallKind(t1, CALL_AS_METHOD);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001419 __ Branch(&non_proxy, ne, t0, Operand(1));
1420
1421 __ push(a1); // Re-add proxy object as additional argument.
1422 __ Addu(a0, a0, Operand(1));
1423 __ GetBuiltinEntry(a3, Builtins::CALL_FUNCTION_PROXY);
1424 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1425 RelocInfo::CODE_TARGET);
1426
1427 __ bind(&non_proxy);
1428 __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00001429 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1430 RelocInfo::CODE_TARGET);
1431 __ bind(&function);
1432 }
1433
1434 // 5b. Get the code to call from the function and check that the number of
1435 // expected arguments matches what we're providing. If so, jump
1436 // (tail-call) to the code in register edx without checking arguments.
1437 // a0: actual number of arguments
1438 // a1: function
1439 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1440 __ lw(a2,
1441 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
1442 __ sra(a2, a2, kSmiTagSize);
1443 __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1444 __ SetCallKind(t1, CALL_AS_METHOD);
1445 // Check formal and actual parameter counts.
1446 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1447 RelocInfo::CODE_TARGET, ne, a2, Operand(a0));
1448
1449 ParameterCount expected(0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001450 __ InvokeCode(a3, expected, expected, JUMP_FUNCTION,
1451 NullCallWrapper(), CALL_AS_METHOD);
Andrei Popescu31002712010-02-23 13:46:05 +00001452}
1453
1454
1455void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001456 const int kIndexOffset = -5 * kPointerSize;
1457 const int kLimitOffset = -4 * kPointerSize;
1458 const int kArgsOffset = 2 * kPointerSize;
1459 const int kRecvOffset = 3 * kPointerSize;
1460 const int kFunctionOffset = 4 * kPointerSize;
1461
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001462 {
1463 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1464 __ lw(a0, MemOperand(fp, kFunctionOffset)); // Get the function.
1465 __ push(a0);
1466 __ lw(a0, MemOperand(fp, kArgsOffset)); // Get the args array.
1467 __ push(a0);
1468 // Returns (in v0) number of arguments to copy to stack as Smi.
1469 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00001470
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001471 // Check the stack for overflow. We are not trying to catch
1472 // interruptions (e.g. debug break and preemption) here, so the "real stack
1473 // limit" is checked.
1474 Label okay;
1475 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
1476 // Make a2 the space we have left. The stack might already be overflowed
1477 // here which will cause a2 to become negative.
1478 __ subu(a2, sp, a2);
1479 // Check if the arguments will overflow the stack.
1480 __ sll(t3, v0, kPointerSizeLog2 - kSmiTagSize);
1481 __ Branch(&okay, gt, a2, Operand(t3)); // Signed comparison.
Ben Murdoch257744e2011-11-30 15:57:28 +00001482
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001483 // Out of stack space.
1484 __ lw(a1, MemOperand(fp, kFunctionOffset));
1485 __ push(a1);
1486 __ push(v0);
1487 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
1488 // End of stack check.
Ben Murdoch257744e2011-11-30 15:57:28 +00001489
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001490 // Push current limit and index.
1491 __ bind(&okay);
1492 __ push(v0); // Limit.
1493 __ mov(a1, zero_reg); // Initial index.
1494 __ push(a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001495
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001496 // Get the receiver.
1497 __ lw(a0, MemOperand(fp, kRecvOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001498
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001499 // Check that the function is a JS function (otherwise it must be a proxy).
1500 Label push_receiver;
1501 __ lw(a1, MemOperand(fp, kFunctionOffset));
1502 __ GetObjectType(a1, a2, a2);
1503 __ Branch(&push_receiver, ne, a2, Operand(JS_FUNCTION_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001504
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001505 // Change context eagerly to get the right global object if necessary.
1506 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1507 // Load the shared function info while the function is still in a1.
1508 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001509
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001510 // Compute the receiver.
1511 // Do not transform the receiver for strict mode functions.
1512 Label call_to_object, use_global_receiver;
1513 __ lw(a2, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset));
1514 __ And(t3, a2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1515 kSmiTagSize)));
1516 __ Branch(&push_receiver, ne, t3, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00001517
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001518 // Do not transform the receiver for native (Compilerhints already in a2).
1519 __ And(t3, a2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1520 __ Branch(&push_receiver, ne, t3, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00001521
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001522 // Compute the receiver in non-strict mode.
1523 __ JumpIfSmi(a0, &call_to_object);
1524 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1525 __ Branch(&use_global_receiver, eq, a0, Operand(a1));
1526 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1527 __ Branch(&use_global_receiver, eq, a0, Operand(a2));
Ben Murdoch257744e2011-11-30 15:57:28 +00001528
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001529 // Check if the receiver is already a JavaScript object.
1530 // a0: receiver
1531 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1532 __ GetObjectType(a0, a1, a1);
1533 __ Branch(&push_receiver, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001534
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001535 // Convert the receiver to a regular object.
1536 // a0: receiver
1537 __ bind(&call_to_object);
1538 __ push(a0);
1539 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1540 __ mov(a0, v0); // Put object in a0 to match other paths to push_receiver.
1541 __ Branch(&push_receiver);
Ben Murdoch257744e2011-11-30 15:57:28 +00001542
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001543 // Use the current global receiver object as the receiver.
1544 __ bind(&use_global_receiver);
1545 const int kGlobalOffset =
1546 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1547 __ lw(a0, FieldMemOperand(cp, kGlobalOffset));
1548 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
1549 __ lw(a0, FieldMemOperand(a0, kGlobalOffset));
1550 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalReceiverOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001551
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001552 // Push the receiver.
1553 // a0: receiver
1554 __ bind(&push_receiver);
1555 __ push(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001556
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001557 // Copy all arguments from the array to the stack.
1558 Label entry, loop;
1559 __ lw(a0, MemOperand(fp, kIndexOffset));
1560 __ Branch(&entry);
Ben Murdoch257744e2011-11-30 15:57:28 +00001561
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001562 // Load the current argument from the arguments array and push it to the
1563 // stack.
1564 // a0: current argument index
1565 __ bind(&loop);
1566 __ lw(a1, MemOperand(fp, kArgsOffset));
1567 __ push(a1);
1568 __ push(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001569
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001570 // Call the runtime to access the property in the arguments array.
1571 __ CallRuntime(Runtime::kGetProperty, 2);
1572 __ push(v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001573
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001574 // Use inline caching to access the arguments.
1575 __ lw(a0, MemOperand(fp, kIndexOffset));
1576 __ Addu(a0, a0, Operand(1 << kSmiTagSize));
1577 __ sw(a0, MemOperand(fp, kIndexOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001578
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001579 // Test if the copy loop has finished copying all the elements from the
1580 // arguments object.
1581 __ bind(&entry);
1582 __ lw(a1, MemOperand(fp, kLimitOffset));
1583 __ Branch(&loop, ne, a0, Operand(a1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001584
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001585 // Invoke the function.
1586 Label call_proxy;
1587 ParameterCount actual(a0);
1588 __ sra(a0, a0, kSmiTagSize);
1589 __ lw(a1, MemOperand(fp, kFunctionOffset));
1590 __ GetObjectType(a1, a2, a2);
1591 __ Branch(&call_proxy, ne, a2, Operand(JS_FUNCTION_TYPE));
1592
1593 __ InvokeFunction(a1, actual, CALL_FUNCTION,
1594 NullCallWrapper(), CALL_AS_METHOD);
1595
1596 frame_scope.GenerateLeaveFrame();
1597 __ Ret(USE_DELAY_SLOT);
1598 __ Addu(sp, sp, Operand(3 * kPointerSize)); // In delay slot.
1599
1600 // Invoke the function proxy.
1601 __ bind(&call_proxy);
1602 __ push(a1); // Add function proxy as last argument.
1603 __ Addu(a0, a0, Operand(1));
1604 __ li(a2, Operand(0, RelocInfo::NONE));
1605 __ SetCallKind(t1, CALL_AS_METHOD);
1606 __ GetBuiltinEntry(a3, Builtins::CALL_FUNCTION_PROXY);
1607 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1608 RelocInfo::CODE_TARGET);
1609 // Tear down the internal frame and remove function, receiver and args.
1610 }
1611
1612 __ Ret(USE_DELAY_SLOT);
1613 __ Addu(sp, sp, Operand(3 * kPointerSize)); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001614}
1615
1616
1617static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1618 __ sll(a0, a0, kSmiTagSize);
1619 __ li(t0, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1620 __ MultiPush(a0.bit() | a1.bit() | t0.bit() | fp.bit() | ra.bit());
1621 __ Addu(fp, sp, Operand(3 * kPointerSize));
1622}
1623
1624
1625static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1626 // ----------- S t a t e -------------
1627 // -- v0 : result being passed through
1628 // -----------------------------------
1629 // Get the number of arguments passed (as a smi), tear down the frame and
1630 // then tear down the parameters.
1631 __ lw(a1, MemOperand(fp, -3 * kPointerSize));
1632 __ mov(sp, fp);
1633 __ MultiPop(fp.bit() | ra.bit());
1634 __ sll(t0, a1, kPointerSizeLog2 - kSmiTagSize);
1635 __ Addu(sp, sp, t0);
1636 // Adjust for the receiver.
1637 __ Addu(sp, sp, Operand(kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +00001638}
1639
1640
1641void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001642 // State setup as expected by MacroAssembler::InvokePrologue.
1643 // ----------- S t a t e -------------
1644 // -- a0: actual arguments count
1645 // -- a1: function (passed through to callee)
1646 // -- a2: expected arguments count
1647 // -- a3: callee code entry
1648 // -- t1: call kind information
1649 // -----------------------------------
1650
1651 Label invoke, dont_adapt_arguments;
1652
1653 Label enough, too_few;
1654 __ Branch(&dont_adapt_arguments, eq,
1655 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1656 // We use Uless as the number of argument should always be greater than 0.
1657 __ Branch(&too_few, Uless, a0, Operand(a2));
1658
1659 { // Enough parameters: actual >= expected.
1660 // a0: actual number of arguments as a smi
1661 // a1: function
1662 // a2: expected number of arguments
1663 // a3: code entry to call
1664 __ bind(&enough);
1665 EnterArgumentsAdaptorFrame(masm);
1666
1667 // Calculate copy start address into a0 and copy end address into a2.
1668 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1669 __ Addu(a0, fp, a0);
1670 // Adjust for return address and receiver.
1671 __ Addu(a0, a0, Operand(2 * kPointerSize));
1672 // Compute copy end address.
1673 __ sll(a2, a2, kPointerSizeLog2);
1674 __ subu(a2, a0, a2);
1675
1676 // Copy the arguments (including the receiver) to the new stack frame.
1677 // a0: copy start address
1678 // a1: function
1679 // a2: copy end address
1680 // a3: code entry to call
1681
1682 Label copy;
1683 __ bind(&copy);
1684 __ lw(t0, MemOperand(a0));
1685 __ push(t0);
1686 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a2));
1687 __ addiu(a0, a0, -kPointerSize); // In delay slot.
1688
1689 __ jmp(&invoke);
1690 }
1691
1692 { // Too few parameters: Actual < expected.
1693 __ bind(&too_few);
1694 EnterArgumentsAdaptorFrame(masm);
1695
1696 // TODO(MIPS): Optimize these loops.
1697
1698 // Calculate copy start address into a0 and copy end address is fp.
1699 // a0: actual number of arguments as a smi
1700 // a1: function
1701 // a2: expected number of arguments
1702 // a3: code entry to call
1703 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1704 __ Addu(a0, fp, a0);
1705 // Adjust for return address and receiver.
1706 __ Addu(a0, a0, Operand(2 * kPointerSize));
1707 // Compute copy end address. Also adjust for return address.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001708 __ Addu(t3, fp, kPointerSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001709
1710 // Copy the arguments (including the receiver) to the new stack frame.
1711 // a0: copy start address
1712 // a1: function
1713 // a2: expected number of arguments
1714 // a3: code entry to call
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001715 // t3: copy end address
Ben Murdoch257744e2011-11-30 15:57:28 +00001716 Label copy;
1717 __ bind(&copy);
1718 __ lw(t0, MemOperand(a0)); // Adjusted above for return addr and receiver.
1719 __ push(t0);
1720 __ Subu(a0, a0, kPointerSize);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001721 __ Branch(&copy, ne, a0, Operand(t3));
Ben Murdoch257744e2011-11-30 15:57:28 +00001722
1723 // Fill the remaining expected arguments with undefined.
1724 // a1: function
1725 // a2: expected number of arguments
1726 // a3: code entry to call
1727 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
1728 __ sll(t2, a2, kPointerSizeLog2);
1729 __ Subu(a2, fp, Operand(t2));
1730 __ Addu(a2, a2, Operand(-4 * kPointerSize)); // Adjust for frame.
1731
1732 Label fill;
1733 __ bind(&fill);
1734 __ push(t0);
1735 __ Branch(&fill, ne, sp, Operand(a2));
1736 }
1737
1738 // Call the entry point.
1739 __ bind(&invoke);
1740
1741 __ Call(a3);
1742
1743 // Exit frame and return.
1744 LeaveArgumentsAdaptorFrame(masm);
1745 __ Ret();
1746
1747
1748 // -------------------------------------------
1749 // Don't adapt arguments.
1750 // -------------------------------------------
1751 __ bind(&dont_adapt_arguments);
1752 __ Jump(a3);
Andrei Popescu31002712010-02-23 13:46:05 +00001753}
1754
1755
1756#undef __
1757
1758} } // namespace v8::internal
1759
Leon Clarkef7060e22010-06-03 12:02:55 +01001760#endif // V8_TARGET_ARCH_MIPS