blob: e22259d71abbf1693582323b93eec0ae963fa41e [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
91// This constant has the same value as JSArray::kPreallocatedArrayElements and
92// if JSArray::kPreallocatedArrayElements is changed handling of loop unfolding
93// below should be reconsidered.
94static const int kLoopUnfoldLimit = 4;
95
96
97// Allocate an empty JSArray. The allocated array is put into the result
98// register. An elements backing store is allocated with size initial_capacity
99// and filled with the hole values.
100static void AllocateEmptyJSArray(MacroAssembler* masm,
101 Register array_function,
102 Register result,
103 Register scratch1,
104 Register scratch2,
105 Register scratch3,
106 int initial_capacity,
107 Label* gc_required) {
108 ASSERT(initial_capacity > 0);
109 // Load the initial map from the array function.
110 __ lw(scratch1, FieldMemOperand(array_function,
111 JSFunction::kPrototypeOrInitialMapOffset));
112
113 // Allocate the JSArray object together with space for a fixed array with the
114 // requested elements.
115 int size = JSArray::kSize + FixedArray::SizeFor(initial_capacity);
116 __ AllocateInNewSpace(size,
117 result,
118 scratch2,
119 scratch3,
120 gc_required,
121 TAG_OBJECT);
122 // Allocated the JSArray. Now initialize the fields except for the elements
123 // array.
124 // result: JSObject
125 // scratch1: initial map
126 // scratch2: start of next object
127 __ sw(scratch1, FieldMemOperand(result, JSObject::kMapOffset));
128 __ LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
129 __ sw(scratch1, FieldMemOperand(result, JSArray::kPropertiesOffset));
130 // Field JSArray::kElementsOffset is initialized later.
131 __ mov(scratch3, zero_reg);
132 __ sw(scratch3, FieldMemOperand(result, JSArray::kLengthOffset));
133
134 // Calculate the location of the elements array and set elements array member
135 // of the JSArray.
136 // result: JSObject
137 // scratch2: start of next object
138 __ Addu(scratch1, result, Operand(JSArray::kSize));
139 __ sw(scratch1, FieldMemOperand(result, JSArray::kElementsOffset));
140
141 // Clear the heap tag on the elements array.
142 __ And(scratch1, scratch1, Operand(~kHeapObjectTagMask));
143
144 // Initialize the FixedArray and fill it with holes. FixedArray length is
145 // stored as a smi.
146 // result: JSObject
147 // scratch1: elements array (untagged)
148 // scratch2: start of next object
149 __ LoadRoot(scratch3, Heap::kFixedArrayMapRootIndex);
150 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
151 __ sw(scratch3, MemOperand(scratch1));
152 __ Addu(scratch1, scratch1, kPointerSize);
153 __ li(scratch3, Operand(Smi::FromInt(initial_capacity)));
154 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
155 __ sw(scratch3, MemOperand(scratch1));
156 __ Addu(scratch1, scratch1, kPointerSize);
157
158 // Fill the FixedArray with the hole value.
159 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
160 ASSERT(initial_capacity <= kLoopUnfoldLimit);
161 __ LoadRoot(scratch3, Heap::kTheHoleValueRootIndex);
162 for (int i = 0; i < initial_capacity; i++) {
163 __ sw(scratch3, MemOperand(scratch1));
164 __ Addu(scratch1, scratch1, kPointerSize);
165 }
166}
167
168
169// Allocate a JSArray with the number of elements stored in a register. The
170// register array_function holds the built-in Array function and the register
171// array_size holds the size of the array as a smi. The allocated array is put
172// into the result register and beginning and end of the FixedArray elements
173// storage is put into registers elements_array_storage and elements_array_end
174// (see below for when that is not the case). If the parameter fill_with_holes
175// is true the allocated elements backing store is filled with the hole values
176// otherwise it is left uninitialized. When the backing store is filled the
177// register elements_array_storage is scratched.
178static void AllocateJSArray(MacroAssembler* masm,
179 Register array_function, // Array function.
180 Register array_size, // As a smi.
181 Register result,
182 Register elements_array_storage,
183 Register elements_array_end,
184 Register scratch1,
185 Register scratch2,
186 bool fill_with_hole,
187 Label* gc_required) {
188 Label not_empty, allocated;
189
190 // Load the initial map from the array function.
191 __ lw(elements_array_storage,
192 FieldMemOperand(array_function,
193 JSFunction::kPrototypeOrInitialMapOffset));
194
195 // Check whether an empty sized array is requested.
196 __ Branch(&not_empty, ne, array_size, Operand(zero_reg));
197
198 // If an empty array is requested allocate a small elements array anyway. This
199 // keeps the code below free of special casing for the empty array.
200 int size = JSArray::kSize +
201 FixedArray::SizeFor(JSArray::kPreallocatedArrayElements);
202 __ AllocateInNewSpace(size,
203 result,
204 elements_array_end,
205 scratch1,
206 gc_required,
207 TAG_OBJECT);
208 __ Branch(&allocated);
209
210 // Allocate the JSArray object together with space for a FixedArray with the
211 // requested number of elements.
212 __ bind(&not_empty);
213 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
214 __ li(elements_array_end,
215 (JSArray::kSize + FixedArray::kHeaderSize) / kPointerSize);
216 __ sra(scratch1, array_size, kSmiTagSize);
217 __ Addu(elements_array_end, elements_array_end, scratch1);
218 __ AllocateInNewSpace(
219 elements_array_end,
220 result,
221 scratch1,
222 scratch2,
223 gc_required,
224 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
225
226 // Allocated the JSArray. Now initialize the fields except for the elements
227 // array.
228 // result: JSObject
229 // elements_array_storage: initial map
230 // array_size: size of array (smi)
231 __ bind(&allocated);
232 __ sw(elements_array_storage, FieldMemOperand(result, JSObject::kMapOffset));
233 __ LoadRoot(elements_array_storage, Heap::kEmptyFixedArrayRootIndex);
234 __ sw(elements_array_storage,
235 FieldMemOperand(result, JSArray::kPropertiesOffset));
236 // Field JSArray::kElementsOffset is initialized later.
237 __ sw(array_size, FieldMemOperand(result, JSArray::kLengthOffset));
238
239 // Calculate the location of the elements array and set elements array member
240 // of the JSArray.
241 // result: JSObject
242 // array_size: size of array (smi)
243 __ Addu(elements_array_storage, result, Operand(JSArray::kSize));
244 __ sw(elements_array_storage,
245 FieldMemOperand(result, JSArray::kElementsOffset));
246
247 // Clear the heap tag on the elements array.
248 __ And(elements_array_storage,
249 elements_array_storage,
250 Operand(~kHeapObjectTagMask));
251 // Initialize the fixed array and fill it with holes. FixedArray length is
252 // stored as a smi.
253 // result: JSObject
254 // elements_array_storage: elements array (untagged)
255 // array_size: size of array (smi)
256 __ LoadRoot(scratch1, Heap::kFixedArrayMapRootIndex);
257 ASSERT_EQ(0 * kPointerSize, FixedArray::kMapOffset);
258 __ sw(scratch1, MemOperand(elements_array_storage));
259 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
260
261 // Length of the FixedArray is the number of pre-allocated elements if
262 // the actual JSArray has length 0 and the size of the JSArray for non-empty
263 // JSArrays. The length of a FixedArray is stored as a smi.
264 ASSERT(kSmiTag == 0);
265 __ li(at, Operand(Smi::FromInt(JSArray::kPreallocatedArrayElements)));
266 __ movz(array_size, at, array_size);
267
268 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
269 __ sw(array_size, MemOperand(elements_array_storage));
270 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
271
272 // Calculate elements array and elements array end.
273 // result: JSObject
274 // elements_array_storage: elements array element storage
275 // array_size: smi-tagged size of elements array
276 ASSERT(kSmiTag == 0 && kSmiTagSize < kPointerSizeLog2);
277 __ sll(elements_array_end, array_size, kPointerSizeLog2 - kSmiTagSize);
278 __ Addu(elements_array_end, elements_array_storage, elements_array_end);
279
280 // Fill the allocated FixedArray with the hole value if requested.
281 // result: JSObject
282 // elements_array_storage: elements array element storage
283 // elements_array_end: start of next object
284 if (fill_with_hole) {
285 Label loop, entry;
286 __ LoadRoot(scratch1, Heap::kTheHoleValueRootIndex);
287 __ Branch(&entry);
288 __ bind(&loop);
289 __ sw(scratch1, MemOperand(elements_array_storage));
290 __ Addu(elements_array_storage, elements_array_storage, kPointerSize);
291
292 __ bind(&entry);
293 __ Branch(&loop, lt, elements_array_storage, Operand(elements_array_end));
294 }
295}
296
297
298// Create a new array for the built-in Array function. This function allocates
299// the JSArray object and the FixedArray elements array and initializes these.
300// If the Array cannot be constructed in native code the runtime is called. This
301// function assumes the following state:
302// a0: argc
303// a1: constructor (built-in Array function)
304// ra: return address
305// sp[0]: last argument
306// This function is used for both construct and normal calls of Array. The only
307// difference between handling a construct call and a normal call is that for a
308// construct call the constructor function in a1 needs to be preserved for
309// entering the generic code. In both cases argc in a0 needs to be preserved.
310// Both registers are preserved by this code so no need to differentiate between
311// construct call and normal call.
312static void ArrayNativeCode(MacroAssembler* masm,
313 Label* call_generic_code) {
314 Counters* counters = masm->isolate()->counters();
315 Label argc_one_or_more, argc_two_or_more;
316
317 // Check for array construction with zero arguments or one.
318 __ Branch(&argc_one_or_more, ne, a0, Operand(zero_reg));
319 // Handle construction of an empty array.
320 AllocateEmptyJSArray(masm,
321 a1,
322 a2,
323 a3,
324 t0,
325 t1,
326 JSArray::kPreallocatedArrayElements,
327 call_generic_code);
328 __ IncrementCounter(counters->array_function_native(), 1, a3, t0);
329 // Setup return value, remove receiver from stack and return.
330 __ mov(v0, a2);
331 __ Addu(sp, sp, Operand(kPointerSize));
332 __ Ret();
333
334 // Check for one argument. Bail out if argument is not smi or if it is
335 // negative.
336 __ bind(&argc_one_or_more);
337 __ Branch(&argc_two_or_more, ne, a0, Operand(1));
338
339 ASSERT(kSmiTag == 0);
340 __ lw(a2, MemOperand(sp)); // Get the argument from the stack.
341 __ And(a3, a2, Operand(kIntptrSignBit | kSmiTagMask));
342 __ Branch(call_generic_code, eq, a3, Operand(zero_reg));
343
344 // Handle construction of an empty array of a certain size. Bail out if size
345 // is too large to actually allocate an elements array.
346 ASSERT(kSmiTag == 0);
347 __ Branch(call_generic_code, ge, a2,
348 Operand(JSObject::kInitialMaxFastElementArray << kSmiTagSize));
349
350 // a0: argc
351 // a1: constructor
352 // a2: array_size (smi)
353 // sp[0]: argument
354 AllocateJSArray(masm,
355 a1,
356 a2,
357 a3,
358 t0,
359 t1,
360 t2,
361 t3,
362 true,
363 call_generic_code);
364 __ IncrementCounter(counters->array_function_native(), 1, a2, t0);
365
366 // Setup return value, remove receiver and argument from stack and return.
367 __ mov(v0, a3);
368 __ Addu(sp, sp, Operand(2 * kPointerSize));
369 __ Ret();
370
371 // Handle construction of an array from a list of arguments.
372 __ bind(&argc_two_or_more);
373 __ sll(a2, a0, kSmiTagSize); // Convert argc to a smi.
374
375 // a0: argc
376 // a1: constructor
377 // a2: array_size (smi)
378 // sp[0]: last argument
379 AllocateJSArray(masm,
380 a1,
381 a2,
382 a3,
383 t0,
384 t1,
385 t2,
386 t3,
387 false,
388 call_generic_code);
389 __ IncrementCounter(counters->array_function_native(), 1, a2, t2);
390
391 // Fill arguments as array elements. Copy from the top of the stack (last
392 // element) to the array backing store filling it backwards. Note:
393 // elements_array_end points after the backing store.
394 // a0: argc
395 // a3: JSArray
396 // t0: elements_array storage start (untagged)
397 // t1: elements_array_end (untagged)
398 // sp[0]: last argument
399
400 Label loop, entry;
401 __ Branch(&entry);
402 __ bind(&loop);
403 __ pop(a2);
404 __ Addu(t1, t1, -kPointerSize);
405 __ sw(a2, MemOperand(t1));
406 __ bind(&entry);
407 __ Branch(&loop, lt, t0, Operand(t1));
408
409 // Remove caller arguments and receiver from the stack, setup return value and
410 // return.
411 // a0: argc
412 // a3: JSArray
413 // sp[0]: receiver
414 __ Addu(sp, sp, Operand(kPointerSize));
415 __ mov(v0, a3);
416 __ Ret();
Andrei Popescu31002712010-02-23 13:46:05 +0000417}
418
419
420void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000421 // ----------- S t a t e -------------
422 // -- a0 : number of arguments
423 // -- ra : return address
424 // -- sp[...]: constructor arguments
425 // -----------------------------------
426 Label generic_array_code;
427
428 // Get the Array function.
429 GenerateLoadArrayFunction(masm, a1);
430
431 if (FLAG_debug_code) {
432 // Initial map for the builtin Array functions should be maps.
433 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
434 __ And(t0, a2, Operand(kSmiTagMask));
435 __ Assert(ne, "Unexpected initial map for Array function (1)",
436 t0, Operand(zero_reg));
437 __ GetObjectType(a2, a3, t0);
438 __ Assert(eq, "Unexpected initial map for Array function (2)",
439 t0, Operand(MAP_TYPE));
440 }
441
442 // Run the native code for the Array function called as a normal function.
443 ArrayNativeCode(masm, &generic_array_code);
444
445 // Jump to the generic array code if the specialized code cannot handle
446 // the construction.
447 __ bind(&generic_array_code);
448
449 Handle<Code> array_code =
450 masm->isolate()->builtins()->ArrayCodeGeneric();
451 __ Jump(array_code, RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +0000452}
453
454
455void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000456 // ----------- S t a t e -------------
457 // -- a0 : number of arguments
458 // -- a1 : constructor function
459 // -- ra : return address
460 // -- sp[...]: constructor arguments
461 // -----------------------------------
462 Label generic_constructor;
463
464 if (FLAG_debug_code) {
465 // The array construct code is only set for the builtin and internal
466 // Array functions which always have a map.
467 // Initial map for the builtin Array function should be a map.
468 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
469 __ And(t0, a2, Operand(kSmiTagMask));
470 __ Assert(ne, "Unexpected initial map for Array function (3)",
471 t0, Operand(zero_reg));
472 __ GetObjectType(a2, a3, t0);
473 __ Assert(eq, "Unexpected initial map for Array function (4)",
474 t0, Operand(MAP_TYPE));
475 }
476
477 // Run the native code for the Array function called as a constructor.
478 ArrayNativeCode(masm, &generic_constructor);
479
480 // Jump to the generic construct code in case the specialized code cannot
481 // handle the construction.
482 __ bind(&generic_constructor);
483
484 Handle<Code> generic_construct_stub =
485 masm->isolate()->builtins()->JSConstructStubGeneric();
486 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
Andrei Popescu31002712010-02-23 13:46:05 +0000487}
488
489
Steve Block44f0eee2011-05-26 01:26:41 +0100490void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000491 // ----------- S t a t e -------------
492 // -- a0 : number of arguments
493 // -- a1 : constructor function
494 // -- ra : return address
495 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
496 // -- sp[argc * 4] : receiver
497 // -----------------------------------
498 Counters* counters = masm->isolate()->counters();
499 __ IncrementCounter(counters->string_ctor_calls(), 1, a2, a3);
500
501 Register function = a1;
502 if (FLAG_debug_code) {
503 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, a2);
504 __ Assert(eq, "Unexpected String function", function, Operand(a2));
505 }
506
507 // Load the first arguments in a0 and get rid of the rest.
508 Label no_arguments;
509 __ Branch(&no_arguments, eq, a0, Operand(zero_reg));
510 // First args = sp[(argc - 1) * 4].
511 __ Subu(a0, a0, Operand(1));
512 __ sll(a0, a0, kPointerSizeLog2);
513 __ Addu(sp, a0, sp);
514 __ lw(a0, MemOperand(sp));
515 // sp now point to args[0], drop args[0] + receiver.
516 __ Drop(2);
517
518 Register argument = a2;
519 Label not_cached, argument_is_string;
520 NumberToStringStub::GenerateLookupNumberStringCache(
521 masm,
522 a0, // Input.
523 argument, // Result.
524 a3, // Scratch.
525 t0, // Scratch.
526 t1, // Scratch.
527 false, // Is it a Smi?
528 &not_cached);
529 __ IncrementCounter(counters->string_ctor_cached_number(), 1, a3, t0);
530 __ bind(&argument_is_string);
531
532 // ----------- S t a t e -------------
533 // -- a2 : argument converted to string
534 // -- a1 : constructor function
535 // -- ra : return address
536 // -----------------------------------
537
538 Label gc_required;
539 __ AllocateInNewSpace(JSValue::kSize,
540 v0, // Result.
541 a3, // Scratch.
542 t0, // Scratch.
543 &gc_required,
544 TAG_OBJECT);
545
546 // Initialising the String Object.
547 Register map = a3;
548 __ LoadGlobalFunctionInitialMap(function, map, t0);
549 if (FLAG_debug_code) {
550 __ lbu(t0, FieldMemOperand(map, Map::kInstanceSizeOffset));
551 __ Assert(eq, "Unexpected string wrapper instance size",
552 t0, Operand(JSValue::kSize >> kPointerSizeLog2));
553 __ lbu(t0, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
554 __ Assert(eq, "Unexpected unused properties of string wrapper",
555 t0, Operand(zero_reg));
556 }
557 __ sw(map, FieldMemOperand(v0, HeapObject::kMapOffset));
558
559 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
560 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
561 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
562
563 __ sw(argument, FieldMemOperand(v0, JSValue::kValueOffset));
564
565 // Ensure the object is fully initialized.
566 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
567
568 __ Ret();
569
570 // The argument was not found in the number to string cache. Check
571 // if it's a string already before calling the conversion builtin.
572 Label convert_argument;
573 __ bind(&not_cached);
574 __ JumpIfSmi(a0, &convert_argument);
575
576 // Is it a String?
577 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
578 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
579 ASSERT(kNotStringTag != 0);
580 __ And(t0, a3, Operand(kIsNotStringMask));
581 __ Branch(&convert_argument, ne, t0, Operand(zero_reg));
582 __ mov(argument, a0);
583 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
584 __ Branch(&argument_is_string);
585
586 // Invoke the conversion builtin and put the result into a2.
587 __ bind(&convert_argument);
588 __ push(function); // Preserve the function.
589 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
590 __ EnterInternalFrame();
591 __ push(v0);
592 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
593 __ LeaveInternalFrame();
594 __ pop(function);
595 __ mov(argument, v0);
596 __ Branch(&argument_is_string);
597
598 // Load the empty string into a2, remove the receiver from the
599 // stack, and jump back to the case where the argument is a string.
600 __ bind(&no_arguments);
601 __ LoadRoot(argument, Heap::kEmptyStringRootIndex);
602 __ Drop(1);
603 __ Branch(&argument_is_string);
604
605 // At this point the argument is already a string. Call runtime to
606 // create a string wrapper.
607 __ bind(&gc_required);
608 __ IncrementCounter(counters->string_ctor_gc_required(), 1, a3, t0);
609 __ EnterInternalFrame();
610 __ push(argument);
611 __ CallRuntime(Runtime::kNewStringWrapper, 1);
612 __ LeaveInternalFrame();
613 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100614}
615
616
Andrei Popescu31002712010-02-23 13:46:05 +0000617void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000618 // ----------- S t a t e -------------
619 // -- a0 : number of arguments
620 // -- a1 : constructor function
621 // -- ra : return address
622 // -- sp[...]: constructor arguments
623 // -----------------------------------
624
625 Label non_function_call;
626 // Check that the function is not a smi.
627 __ And(t0, a1, Operand(kSmiTagMask));
628 __ Branch(&non_function_call, eq, t0, Operand(zero_reg));
629 // Check that the function is a JSFunction.
630 __ GetObjectType(a1, a2, a2);
631 __ Branch(&non_function_call, ne, a2, Operand(JS_FUNCTION_TYPE));
632
633 // Jump to the function-specific construct stub.
634 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
635 __ lw(a2, FieldMemOperand(a2, SharedFunctionInfo::kConstructStubOffset));
636 __ Addu(t9, a2, Operand(Code::kHeaderSize - kHeapObjectTag));
637 __ Jump(Operand(t9));
638
639 // a0: number of arguments
640 // a1: called object
641 __ bind(&non_function_call);
642 // CALL_NON_FUNCTION expects the non-function constructor as receiver
643 // (instead of the original receiver from the call site). The receiver is
644 // stack element argc.
645 // Set expected number of arguments to zero (not changing a0).
646 __ mov(a2, zero_reg);
647 __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
648 __ SetCallKind(t1, CALL_AS_METHOD);
649 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
650 RelocInfo::CODE_TARGET);
651}
652
653
654static void Generate_JSConstructStubHelper(MacroAssembler* masm,
655 bool is_api_function,
656 bool count_constructions) {
657 // Should never count constructions for api objects.
658 ASSERT(!is_api_function || !count_constructions);
659
660 Isolate* isolate = masm->isolate();
661
662 // ----------- S t a t e -------------
663 // -- a0 : number of arguments
664 // -- a1 : constructor function
665 // -- ra : return address
666 // -- sp[...]: constructor arguments
667 // -----------------------------------
668
669 // Enter a construct frame.
670 __ EnterConstructFrame();
671
672 // Preserve the two incoming parameters on the stack.
673 __ sll(a0, a0, kSmiTagSize); // Tag arguments count.
674 __ MultiPushReversed(a0.bit() | a1.bit());
675
676 // Use t7 to hold undefined, which is used in several places below.
677 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
678
679 Label rt_call, allocated;
680 // Try to allocate the object without transitioning into C code. If any of the
681 // preconditions is not met, the code bails out to the runtime call.
682 if (FLAG_inline_new) {
683 Label undo_allocation;
684#ifdef ENABLE_DEBUGGER_SUPPORT
685 ExternalReference debug_step_in_fp =
686 ExternalReference::debug_step_in_fp_address(isolate);
687 __ li(a2, Operand(debug_step_in_fp));
688 __ lw(a2, MemOperand(a2));
689 __ Branch(&rt_call, ne, a2, Operand(zero_reg));
690#endif
691
692 // Load the initial map and verify that it is in fact a map.
693 // a1: constructor function
694 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
695 __ And(t0, a2, Operand(kSmiTagMask));
696 __ Branch(&rt_call, eq, t0, Operand(zero_reg));
697 __ GetObjectType(a2, a3, t4);
698 __ Branch(&rt_call, ne, t4, Operand(MAP_TYPE));
699
700 // Check that the constructor is not constructing a JSFunction (see comments
701 // in Runtime_NewObject in runtime.cc). In which case the initial map's
702 // instance type would be JS_FUNCTION_TYPE.
703 // a1: constructor function
704 // a2: initial map
705 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
706 __ Branch(&rt_call, eq, a3, Operand(JS_FUNCTION_TYPE));
707
708 if (count_constructions) {
709 Label allocate;
710 // Decrease generous allocation count.
711 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
712 MemOperand constructor_count =
713 FieldMemOperand(a3, SharedFunctionInfo::kConstructionCountOffset);
714 __ lbu(t0, constructor_count);
715 __ Subu(t0, t0, Operand(1));
716 __ sb(t0, constructor_count);
717 __ Branch(&allocate, ne, t0, Operand(zero_reg));
718
719 __ Push(a1, a2);
720
721 __ push(a1); // Constructor.
722 // The call will replace the stub, so the countdown is only done once.
723 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
724
725 __ pop(a2);
726 __ pop(a1);
727
728 __ bind(&allocate);
729 }
730
731 // Now allocate the JSObject on the heap.
732 // a1: constructor function
733 // a2: initial map
734 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
735 __ AllocateInNewSpace(a3, t4, t5, t6, &rt_call, SIZE_IN_WORDS);
736
737 // Allocated the JSObject, now initialize the fields. Map is set to initial
738 // map and properties and elements are set to empty fixed array.
739 // a1: constructor function
740 // a2: initial map
741 // a3: object size
742 // t4: JSObject (not tagged)
743 __ LoadRoot(t6, Heap::kEmptyFixedArrayRootIndex);
744 __ mov(t5, t4);
745 __ sw(a2, MemOperand(t5, JSObject::kMapOffset));
746 __ sw(t6, MemOperand(t5, JSObject::kPropertiesOffset));
747 __ sw(t6, MemOperand(t5, JSObject::kElementsOffset));
748 __ Addu(t5, t5, Operand(3*kPointerSize));
749 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
750 ASSERT_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
751 ASSERT_EQ(2 * kPointerSize, JSObject::kElementsOffset);
752
753 // Fill all the in-object properties with appropriate filler.
754 // a1: constructor function
755 // a2: initial map
756 // a3: object size (in words)
757 // t4: JSObject (not tagged)
758 // t5: First in-object property of JSObject (not tagged)
759 __ sll(t0, a3, kPointerSizeLog2);
760 __ addu(t6, t4, t0); // End of object.
761 ASSERT_EQ(3 * kPointerSize, JSObject::kHeaderSize);
762 { Label loop, entry;
763 if (count_constructions) {
764 // To allow for truncation.
765 __ LoadRoot(t7, Heap::kOnePointerFillerMapRootIndex);
766 } else {
767 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
768 }
769 __ jmp(&entry);
770 __ bind(&loop);
771 __ sw(t7, MemOperand(t5, 0));
772 __ addiu(t5, t5, kPointerSize);
773 __ bind(&entry);
774 __ Branch(&loop, Uless, t5, Operand(t6));
775 }
776
777 // Add the object tag to make the JSObject real, so that we can continue and
778 // jump into the continuation code at any time from now on. Any failures
779 // need to undo the allocation, so that the heap is in a consistent state
780 // and verifiable.
781 __ Addu(t4, t4, Operand(kHeapObjectTag));
782
783 // Check if a non-empty properties array is needed. Continue with allocated
784 // object if not fall through to runtime call if it is.
785 // a1: constructor function
786 // t4: JSObject
787 // t5: start of next object (not tagged)
788 __ lbu(a3, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
789 // The field instance sizes contains both pre-allocated property fields and
790 // in-object properties.
791 __ lw(a0, FieldMemOperand(a2, Map::kInstanceSizesOffset));
792 __ And(t6,
793 a0,
794 Operand(0x000000FF << Map::kPreAllocatedPropertyFieldsByte * 8));
795 __ srl(t0, t6, Map::kPreAllocatedPropertyFieldsByte * 8);
796 __ Addu(a3, a3, Operand(t0));
797 __ And(t6, a0, Operand(0x000000FF << Map::kInObjectPropertiesByte * 8));
798 __ srl(t0, t6, Map::kInObjectPropertiesByte * 8);
799 __ subu(a3, a3, t0);
800
801 // Done if no extra properties are to be allocated.
802 __ Branch(&allocated, eq, a3, Operand(zero_reg));
803 __ Assert(greater_equal, "Property allocation count failed.",
804 a3, Operand(zero_reg));
805
806 // Scale the number of elements by pointer size and add the header for
807 // FixedArrays to the start of the next object calculation from above.
808 // a1: constructor
809 // a3: number of elements in properties array
810 // t4: JSObject
811 // t5: start of next object
812 __ Addu(a0, a3, Operand(FixedArray::kHeaderSize / kPointerSize));
813 __ AllocateInNewSpace(
814 a0,
815 t5,
816 t6,
817 a2,
818 &undo_allocation,
819 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
820
821 // Initialize the FixedArray.
822 // a1: constructor
823 // a3: number of elements in properties array (un-tagged)
824 // t4: JSObject
825 // t5: start of next object
826 __ LoadRoot(t6, Heap::kFixedArrayMapRootIndex);
827 __ mov(a2, t5);
828 __ sw(t6, MemOperand(a2, JSObject::kMapOffset));
829 __ sll(a0, a3, kSmiTagSize);
830 __ sw(a0, MemOperand(a2, FixedArray::kLengthOffset));
831 __ Addu(a2, a2, Operand(2 * kPointerSize));
832
833 ASSERT_EQ(0 * kPointerSize, JSObject::kMapOffset);
834 ASSERT_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
835
836 // Initialize the fields to undefined.
837 // a1: constructor
838 // a2: First element of FixedArray (not tagged)
839 // a3: number of elements in properties array
840 // t4: JSObject
841 // t5: FixedArray (not tagged)
842 __ sll(t3, a3, kPointerSizeLog2);
843 __ addu(t6, a2, t3); // End of object.
844 ASSERT_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
845 { Label loop, entry;
846 if (count_constructions) {
847 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
848 } else if (FLAG_debug_code) {
849 __ LoadRoot(t8, Heap::kUndefinedValueRootIndex);
850 __ Assert(eq, "Undefined value not loaded.", t7, Operand(t8));
851 }
852 __ jmp(&entry);
853 __ bind(&loop);
854 __ sw(t7, MemOperand(a2));
855 __ addiu(a2, a2, kPointerSize);
856 __ bind(&entry);
857 __ Branch(&loop, less, a2, Operand(t6));
858 }
859
860 // Store the initialized FixedArray into the properties field of
861 // the JSObject.
862 // a1: constructor function
863 // t4: JSObject
864 // t5: FixedArray (not tagged)
865 __ Addu(t5, t5, Operand(kHeapObjectTag)); // Add the heap tag.
866 __ sw(t5, FieldMemOperand(t4, JSObject::kPropertiesOffset));
867
868 // Continue with JSObject being successfully allocated.
869 // a1: constructor function
870 // a4: JSObject
871 __ jmp(&allocated);
872
873 // Undo the setting of the new top so that the heap is verifiable. For
874 // example, the map's unused properties potentially do not match the
875 // allocated objects unused properties.
876 // t4: JSObject (previous new top)
877 __ bind(&undo_allocation);
878 __ UndoAllocationInNewSpace(t4, t5);
879 }
880
881 __ bind(&rt_call);
882 // Allocate the new receiver object using the runtime call.
883 // a1: constructor function
884 __ push(a1); // Argument for Runtime_NewObject.
885 __ CallRuntime(Runtime::kNewObject, 1);
886 __ mov(t4, v0);
887
888 // Receiver for constructor call allocated.
889 // t4: JSObject
890 __ bind(&allocated);
891 __ push(t4);
892
893 // Push the function and the allocated receiver from the stack.
894 // sp[0]: receiver (newly allocated object)
895 // sp[1]: constructor function
896 // sp[2]: number of arguments (smi-tagged)
897 __ lw(a1, MemOperand(sp, kPointerSize));
898 __ MultiPushReversed(a1.bit() | t4.bit());
899
900 // Reload the number of arguments from the stack.
901 // a1: constructor function
902 // sp[0]: receiver
903 // sp[1]: constructor function
904 // sp[2]: receiver
905 // sp[3]: constructor function
906 // sp[4]: number of arguments (smi-tagged)
907 __ lw(a3, MemOperand(sp, 4 * kPointerSize));
908
909 // Setup pointer to last argument.
910 __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
911
912 // Setup number of arguments for function call below.
913 __ srl(a0, a3, kSmiTagSize);
914
915 // Copy arguments and receiver to the expression stack.
916 // a0: number of arguments
917 // a1: constructor function
918 // a2: address of last argument (caller sp)
919 // a3: number of arguments (smi-tagged)
920 // sp[0]: receiver
921 // sp[1]: constructor function
922 // sp[2]: receiver
923 // sp[3]: constructor function
924 // sp[4]: number of arguments (smi-tagged)
925 Label loop, entry;
926 __ jmp(&entry);
927 __ bind(&loop);
928 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
929 __ Addu(t0, a2, Operand(t0));
930 __ lw(t1, MemOperand(t0));
931 __ push(t1);
932 __ bind(&entry);
933 __ Addu(a3, a3, Operand(-2));
934 __ Branch(&loop, greater_equal, a3, Operand(zero_reg));
935
936 // Call the function.
937 // a0: number of arguments
938 // a1: constructor function
939 if (is_api_function) {
940 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
941 Handle<Code> code =
942 masm->isolate()->builtins()->HandleApiCallConstruct();
943 ParameterCount expected(0);
944 __ InvokeCode(code, expected, expected,
945 RelocInfo::CODE_TARGET, CALL_FUNCTION);
946 } else {
947 ParameterCount actual(a0);
948 __ InvokeFunction(a1, actual, CALL_FUNCTION);
949 }
950
951 // Pop the function from the stack.
952 // v0: result
953 // sp[0]: constructor function
954 // sp[2]: receiver
955 // sp[3]: constructor function
956 // sp[4]: number of arguments (smi-tagged)
957 __ Pop();
958
959 // Restore context from the frame.
960 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
961
962 // If the result is an object (in the ECMA sense), we should get rid
963 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
964 // on page 74.
965 Label use_receiver, exit;
966
967 // If the result is a smi, it is *not* an object in the ECMA sense.
968 // v0: result
969 // sp[0]: receiver (newly allocated object)
970 // sp[1]: constructor function
971 // sp[2]: number of arguments (smi-tagged)
972 __ And(t0, v0, Operand(kSmiTagMask));
973 __ Branch(&use_receiver, eq, t0, Operand(zero_reg));
974
975 // If the type of the result (stored in its map) is less than
976 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
977 __ GetObjectType(v0, a3, a3);
978 __ Branch(&exit, greater_equal, a3, Operand(FIRST_JS_OBJECT_TYPE));
979
980 // Throw away the result of the constructor invocation and use the
981 // on-stack receiver as the result.
982 __ bind(&use_receiver);
983 __ lw(v0, MemOperand(sp));
984
985 // Remove receiver from the stack, remove caller arguments, and
986 // return.
987 __ bind(&exit);
988 // v0: result
989 // sp[0]: receiver (newly allocated object)
990 // sp[1]: constructor function
991 // sp[2]: number of arguments (smi-tagged)
992 __ lw(a1, MemOperand(sp, 2 * kPointerSize));
993 __ LeaveConstructFrame();
994 __ sll(t0, a1, kPointerSizeLog2 - 1);
995 __ Addu(sp, sp, t0);
996 __ Addu(sp, sp, kPointerSize);
997 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
998 __ Ret();
Andrei Popescu31002712010-02-23 13:46:05 +0000999}
1000
1001
Steve Block44f0eee2011-05-26 01:26:41 +01001002void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001003 Generate_JSConstructStubHelper(masm, false, true);
Steve Block44f0eee2011-05-26 01:26:41 +01001004}
1005
1006
Andrei Popescu31002712010-02-23 13:46:05 +00001007void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001008 Generate_JSConstructStubHelper(masm, false, false);
Andrei Popescu31002712010-02-23 13:46:05 +00001009}
1010
1011
1012void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001013 Generate_JSConstructStubHelper(masm, true, false);
1014}
1015
1016
1017static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
1018 bool is_construct) {
1019 // Called from JSEntryStub::GenerateBody
1020
1021 // ----------- S t a t e -------------
1022 // -- a0: code entry
1023 // -- a1: function
1024 // -- a2: reveiver_pointer
1025 // -- a3: argc
1026 // -- s0: argv
1027 // -----------------------------------
1028
1029 // Clear the context before we push it when entering the JS frame.
1030 __ mov(cp, zero_reg);
1031
1032 // Enter an internal frame.
1033 __ EnterInternalFrame();
1034
1035 // Set up the context from the function argument.
1036 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1037
1038 // Set up the roots register.
1039 ExternalReference roots_address =
1040 ExternalReference::roots_address(masm->isolate());
1041 __ li(s6, Operand(roots_address));
1042
1043 // Push the function and the receiver onto the stack.
1044 __ Push(a1, a2);
1045
1046 // Copy arguments to the stack in a loop.
1047 // a3: argc
1048 // s0: argv, ie points to first arg
1049 Label loop, entry;
1050 __ sll(t0, a3, kPointerSizeLog2);
1051 __ addu(t2, s0, t0);
1052 __ b(&entry);
1053 __ nop(); // Branch delay slot nop.
1054 // t2 points past last arg.
1055 __ bind(&loop);
1056 __ lw(t0, MemOperand(s0)); // Read next parameter.
1057 __ addiu(s0, s0, kPointerSize);
1058 __ lw(t0, MemOperand(t0)); // Dereference handle.
1059 __ push(t0); // Push parameter.
1060 __ bind(&entry);
1061 __ Branch(&loop, ne, s0, Operand(t2));
1062
1063 // Initialize all JavaScript callee-saved registers, since they will be seen
1064 // by the garbage collector as part of handlers.
1065 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
1066 __ mov(s1, t0);
1067 __ mov(s2, t0);
1068 __ mov(s3, t0);
1069 __ mov(s4, t0);
1070 __ mov(s5, t0);
1071 // s6 holds the root address. Do not clobber.
1072 // s7 is cp. Do not init.
1073
1074 // Invoke the code and pass argc as a0.
1075 __ mov(a0, a3);
1076 if (is_construct) {
1077 __ Call(masm->isolate()->builtins()->JSConstructCall(),
1078 RelocInfo::CODE_TARGET);
1079 } else {
1080 ParameterCount actual(a0);
1081 __ InvokeFunction(a1, actual, CALL_FUNCTION);
1082 }
1083
1084 __ LeaveInternalFrame();
1085
1086 __ Jump(ra);
Andrei Popescu31002712010-02-23 13:46:05 +00001087}
1088
1089
Andrei Popescu31002712010-02-23 13:46:05 +00001090void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001091 Generate_JSEntryTrampolineHelper(masm, false);
Andrei Popescu31002712010-02-23 13:46:05 +00001092}
1093
1094
1095void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001096 Generate_JSEntryTrampolineHelper(masm, true);
Steve Block44f0eee2011-05-26 01:26:41 +01001097}
1098
1099
1100void Builtins::Generate_LazyCompile(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001101 // Enter an internal frame.
1102 __ EnterInternalFrame();
1103
1104 // Preserve the function.
1105 __ push(a1);
1106 // Push call kind information.
1107 __ push(t1);
1108
1109 // Push the function on the stack as the argument to the runtime function.
1110 __ push(a1);
1111 // Call the runtime function.
1112 __ CallRuntime(Runtime::kLazyCompile, 1);
1113 // Calculate the entry point.
1114 __ addiu(t9, v0, Code::kHeaderSize - kHeapObjectTag);
1115
1116 // Restore call kind information.
1117 __ pop(t1);
1118 // Restore saved function.
1119 __ pop(a1);
1120
1121 // Tear down temporary frame.
1122 __ LeaveInternalFrame();
1123
1124 // Do a tail-call of the compiled function.
1125 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001126}
1127
1128
1129void Builtins::Generate_LazyRecompile(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001130 // Enter an internal frame.
1131 __ EnterInternalFrame();
1132
1133 // Preserve the function.
1134 __ push(a1);
1135 // Push call kind information.
1136 __ push(t1);
1137
1138 // Push the function on the stack as the argument to the runtime function.
1139 __ push(a1);
1140 __ CallRuntime(Runtime::kLazyRecompile, 1);
1141 // Calculate the entry point.
1142 __ Addu(t9, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
1143
1144 // Restore call kind information.
1145 __ pop(t1);
1146 // Restore saved function.
1147 __ pop(a1);
1148
1149 // Tear down temporary frame.
1150 __ LeaveInternalFrame();
1151
1152 // Do a tail-call of the compiled function.
1153 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001154}
1155
1156
Ben Murdoch257744e2011-11-30 15:57:28 +00001157// These functions are called from C++ but cannot be used in live code.
Steve Block44f0eee2011-05-26 01:26:41 +01001158void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001159 __ Abort("Call to unimplemented function in builtins-mips.cc");
Steve Block44f0eee2011-05-26 01:26:41 +01001160}
1161
1162
1163void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001164 __ Abort("Call to unimplemented function in builtins-mips.cc");
Steve Block44f0eee2011-05-26 01:26:41 +01001165}
1166
1167
1168void Builtins::Generate_NotifyOSR(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001169 __ Abort("Call to unimplemented function in builtins-mips.cc");
Steve Block44f0eee2011-05-26 01:26:41 +01001170}
1171
1172
1173void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001174 __ Abort("Call to unimplemented function in builtins-mips.cc");
Andrei Popescu31002712010-02-23 13:46:05 +00001175}
1176
1177
1178void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001179 // 1. Make sure we have at least one argument.
1180 // a0: actual number of arguments
1181 { Label done;
1182 __ Branch(&done, ne, a0, Operand(zero_reg));
1183 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
1184 __ push(t2);
1185 __ Addu(a0, a0, Operand(1));
1186 __ bind(&done);
1187 }
1188
1189 // 2. Get the function to call (passed as receiver) from the stack, check
1190 // if it is a function.
1191 // a0: actual number of arguments
1192 Label non_function;
1193 __ sll(at, a0, kPointerSizeLog2);
1194 __ addu(at, sp, at);
1195 __ lw(a1, MemOperand(at));
1196 __ And(at, a1, Operand(kSmiTagMask));
1197 __ Branch(&non_function, eq, at, Operand(zero_reg));
1198 __ GetObjectType(a1, a2, a2);
1199 __ Branch(&non_function, ne, a2, Operand(JS_FUNCTION_TYPE));
1200
1201 // 3a. Patch the first argument if necessary when calling a function.
1202 // a0: actual number of arguments
1203 // a1: function
1204 Label shift_arguments;
1205 { Label convert_to_object, use_global_receiver, patch_receiver;
1206 // Change context eagerly in case we need the global receiver.
1207 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1208
1209 // Do not transform the receiver for strict mode functions.
1210 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1211 __ lw(a3, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset));
1212 __ And(t0, a3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1213 kSmiTagSize)));
1214 __ Branch(&shift_arguments, ne, t0, Operand(zero_reg));
1215
1216 // Do not transform the receiver for native (Compilerhints already in a3).
1217 __ And(t0, a3, Operand(1 << (SharedFunctionInfo::kES5Native +
1218 kSmiTagSize)));
1219 __ Branch(&shift_arguments, ne, t0, Operand(zero_reg));
1220
1221 // Compute the receiver in non-strict mode.
1222 // Load first argument in a2. a2 = -kPointerSize(sp + n_args << 2).
1223 __ sll(at, a0, kPointerSizeLog2);
1224 __ addu(a2, sp, at);
1225 __ lw(a2, MemOperand(a2, -kPointerSize));
1226 // a0: actual number of arguments
1227 // a1: function
1228 // a2: first argument
1229 __ JumpIfSmi(a2, &convert_to_object, t2);
1230
1231 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1232 __ Branch(&use_global_receiver, eq, a2, Operand(a3));
1233 __ LoadRoot(a3, Heap::kNullValueRootIndex);
1234 __ Branch(&use_global_receiver, eq, a2, Operand(a3));
1235
1236 STATIC_ASSERT(LAST_JS_OBJECT_TYPE + 1 == LAST_TYPE);
1237 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1238 __ GetObjectType(a2, a3, a3);
1239 __ Branch(&shift_arguments, ge, a3, Operand(FIRST_JS_OBJECT_TYPE));
1240
1241 __ bind(&convert_to_object);
1242 __ EnterInternalFrame(); // In order to preserve argument count.
1243 __ sll(a0, a0, kSmiTagSize); // Smi tagged.
1244 __ push(a0);
1245
1246 __ push(a2);
1247 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1248 __ mov(a2, v0);
1249
1250 __ pop(a0);
1251 __ sra(a0, a0, kSmiTagSize); // Un-tag.
1252 __ LeaveInternalFrame();
1253 // Restore the function to a1.
1254 __ sll(at, a0, kPointerSizeLog2);
1255 __ addu(at, sp, at);
1256 __ lw(a1, MemOperand(at));
1257 __ Branch(&patch_receiver);
1258
1259 // Use the global receiver object from the called function as the
1260 // receiver.
1261 __ bind(&use_global_receiver);
1262 const int kGlobalIndex =
1263 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1264 __ lw(a2, FieldMemOperand(cp, kGlobalIndex));
1265 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalContextOffset));
1266 __ lw(a2, FieldMemOperand(a2, kGlobalIndex));
1267 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalReceiverOffset));
1268
1269 __ bind(&patch_receiver);
1270 __ sll(at, a0, kPointerSizeLog2);
1271 __ addu(a3, sp, at);
1272 __ sw(a2, MemOperand(a3, -kPointerSize));
1273
1274 __ Branch(&shift_arguments);
1275 }
1276
1277 // 3b. Patch the first argument when calling a non-function. The
1278 // CALL_NON_FUNCTION builtin expects the non-function callee as
1279 // receiver, so overwrite the first argument which will ultimately
1280 // become the receiver.
1281 // a0: actual number of arguments
1282 // a1: function
1283 __ bind(&non_function);
1284 // Restore the function in case it has been modified.
1285 __ sll(at, a0, kPointerSizeLog2);
1286 __ addu(a2, sp, at);
1287 __ sw(a1, MemOperand(a2, -kPointerSize));
1288 // Clear a1 to indicate a non-function being called.
1289 __ mov(a1, zero_reg);
1290
1291 // 4. Shift arguments and return address one slot down on the stack
1292 // (overwriting the original receiver). Adjust argument count to make
1293 // the original first argument the new receiver.
1294 // a0: actual number of arguments
1295 // a1: function
1296 __ bind(&shift_arguments);
1297 { Label loop;
1298 // Calculate the copy start address (destination). Copy end address is sp.
1299 __ sll(at, a0, kPointerSizeLog2);
1300 __ addu(a2, sp, at);
1301
1302 __ bind(&loop);
1303 __ lw(at, MemOperand(a2, -kPointerSize));
1304 __ sw(at, MemOperand(a2));
1305 __ Subu(a2, a2, Operand(kPointerSize));
1306 __ Branch(&loop, ne, a2, Operand(sp));
1307 // Adjust the actual number of arguments and remove the top element
1308 // (which is a copy of the last argument).
1309 __ Subu(a0, a0, Operand(1));
1310 __ Pop();
1311 }
1312
1313 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin.
1314 // a0: actual number of arguments
1315 // a1: function
1316 { Label function;
1317 __ Branch(&function, ne, a1, Operand(zero_reg));
1318 __ mov(a2, zero_reg); // expected arguments is 0 for CALL_NON_FUNCTION
1319 __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION);
1320 __ SetCallKind(t1, CALL_AS_METHOD);
1321 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1322 RelocInfo::CODE_TARGET);
1323 __ bind(&function);
1324 }
1325
1326 // 5b. Get the code to call from the function and check that the number of
1327 // expected arguments matches what we're providing. If so, jump
1328 // (tail-call) to the code in register edx without checking arguments.
1329 // a0: actual number of arguments
1330 // a1: function
1331 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1332 __ lw(a2,
1333 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
1334 __ sra(a2, a2, kSmiTagSize);
1335 __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1336 __ SetCallKind(t1, CALL_AS_METHOD);
1337 // Check formal and actual parameter counts.
1338 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1339 RelocInfo::CODE_TARGET, ne, a2, Operand(a0));
1340
1341 ParameterCount expected(0);
1342 __ InvokeCode(a3, expected, expected, JUMP_FUNCTION);
Andrei Popescu31002712010-02-23 13:46:05 +00001343}
1344
1345
1346void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001347 const int kIndexOffset = -5 * kPointerSize;
1348 const int kLimitOffset = -4 * kPointerSize;
1349 const int kArgsOffset = 2 * kPointerSize;
1350 const int kRecvOffset = 3 * kPointerSize;
1351 const int kFunctionOffset = 4 * kPointerSize;
1352
1353 __ EnterInternalFrame();
1354
1355 __ lw(a0, MemOperand(fp, kFunctionOffset)); // Get the function.
1356 __ push(a0);
1357 __ lw(a0, MemOperand(fp, kArgsOffset)); // Get the args array.
1358 __ push(a0);
1359 // Returns (in v0) number of arguments to copy to stack as Smi.
1360 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1361
1362 // Check the stack for overflow. We are not trying need to catch
1363 // interruptions (e.g. debug break and preemption) here, so the "real stack
1364 // limit" is checked.
1365 Label okay;
1366 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
1367 // Make a2 the space we have left. The stack might already be overflowed
1368 // here which will cause a2 to become negative.
1369 __ subu(a2, sp, a2);
1370 // Check if the arguments will overflow the stack.
1371 __ sll(t0, v0, kPointerSizeLog2 - kSmiTagSize);
1372 __ Branch(&okay, gt, a2, Operand(t0)); // Signed comparison.
1373
1374 // Out of stack space.
1375 __ lw(a1, MemOperand(fp, kFunctionOffset));
1376 __ push(a1);
1377 __ push(v0);
1378 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
1379 // End of stack check.
1380
1381 // Push current limit and index.
1382 __ bind(&okay);
1383 __ push(v0); // Limit.
1384 __ mov(a1, zero_reg); // Initial index.
1385 __ push(a1);
1386
1387 // Change context eagerly to get the right global object if necessary.
1388 __ lw(a0, MemOperand(fp, kFunctionOffset));
1389 __ lw(cp, FieldMemOperand(a0, JSFunction::kContextOffset));
1390 // Load the shared function info while the function is still in a0.
1391 __ lw(a1, FieldMemOperand(a0, JSFunction::kSharedFunctionInfoOffset));
1392
1393 // Compute the receiver.
1394 Label call_to_object, use_global_receiver, push_receiver;
1395 __ lw(a0, MemOperand(fp, kRecvOffset));
1396
1397 // Do not transform the receiver for strict mode functions.
1398 __ lw(a2, FieldMemOperand(a1, SharedFunctionInfo::kCompilerHintsOffset));
1399 __ And(t0, a2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1400 kSmiTagSize)));
1401 __ Branch(&push_receiver, ne, t0, Operand(zero_reg));
1402
1403 // Do not transform the receiver for native (Compilerhints already in a2).
1404 __ And(t0, a2, Operand(1 << (SharedFunctionInfo::kES5Native +
1405 kSmiTagSize)));
1406 __ Branch(&push_receiver, ne, t0, Operand(zero_reg));
1407
1408 // Compute the receiver in non-strict mode.
1409 __ And(t0, a0, Operand(kSmiTagMask));
1410 __ Branch(&call_to_object, eq, t0, Operand(zero_reg));
1411 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1412 __ Branch(&use_global_receiver, eq, a0, Operand(a1));
1413 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1414 __ Branch(&use_global_receiver, eq, a0, Operand(a2));
1415
1416 // Check if the receiver is already a JavaScript object.
1417 // a0: receiver
1418 STATIC_ASSERT(LAST_JS_OBJECT_TYPE + 1 == LAST_TYPE);
1419 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1420 __ GetObjectType(a0, a1, a1);
1421 __ Branch(&push_receiver, ge, a1, Operand(FIRST_JS_OBJECT_TYPE));
1422
1423 // Convert the receiver to a regular object.
1424 // a0: receiver
1425 __ bind(&call_to_object);
1426 __ push(a0);
1427 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1428 __ mov(a0, v0); // Put object in a0 to match other paths to push_receiver.
1429 __ Branch(&push_receiver);
1430
1431 // Use the current global receiver object as the receiver.
1432 __ bind(&use_global_receiver);
1433 const int kGlobalOffset =
1434 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
1435 __ lw(a0, FieldMemOperand(cp, kGlobalOffset));
1436 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset));
1437 __ lw(a0, FieldMemOperand(a0, kGlobalOffset));
1438 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalReceiverOffset));
1439
1440 // Push the receiver.
1441 // a0: receiver
1442 __ bind(&push_receiver);
1443 __ push(a0);
1444
1445 // Copy all arguments from the array to the stack.
1446 Label entry, loop;
1447 __ lw(a0, MemOperand(fp, kIndexOffset));
1448 __ Branch(&entry);
1449
1450 // Load the current argument from the arguments array and push it to the
1451 // stack.
1452 // a0: current argument index
1453 __ bind(&loop);
1454 __ lw(a1, MemOperand(fp, kArgsOffset));
1455 __ push(a1);
1456 __ push(a0);
1457
1458 // Call the runtime to access the property in the arguments array.
1459 __ CallRuntime(Runtime::kGetProperty, 2);
1460 __ push(v0);
1461
1462 // Use inline caching to access the arguments.
1463 __ lw(a0, MemOperand(fp, kIndexOffset));
1464 __ Addu(a0, a0, Operand(1 << kSmiTagSize));
1465 __ sw(a0, MemOperand(fp, kIndexOffset));
1466
1467 // Test if the copy loop has finished copying all the elements from the
1468 // arguments object.
1469 __ bind(&entry);
1470 __ lw(a1, MemOperand(fp, kLimitOffset));
1471 __ Branch(&loop, ne, a0, Operand(a1));
1472 // Invoke the function.
1473 ParameterCount actual(a0);
1474 __ sra(a0, a0, kSmiTagSize);
1475 __ lw(a1, MemOperand(fp, kFunctionOffset));
1476 __ InvokeFunction(a1, actual, CALL_FUNCTION);
1477
1478 // Tear down the internal frame and remove function, receiver and args.
1479 __ LeaveInternalFrame();
1480 __ Addu(sp, sp, Operand(3 * kPointerSize));
1481 __ Ret();
1482}
1483
1484
1485static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1486 __ sll(a0, a0, kSmiTagSize);
1487 __ li(t0, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1488 __ MultiPush(a0.bit() | a1.bit() | t0.bit() | fp.bit() | ra.bit());
1489 __ Addu(fp, sp, Operand(3 * kPointerSize));
1490}
1491
1492
1493static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1494 // ----------- S t a t e -------------
1495 // -- v0 : result being passed through
1496 // -----------------------------------
1497 // Get the number of arguments passed (as a smi), tear down the frame and
1498 // then tear down the parameters.
1499 __ lw(a1, MemOperand(fp, -3 * kPointerSize));
1500 __ mov(sp, fp);
1501 __ MultiPop(fp.bit() | ra.bit());
1502 __ sll(t0, a1, kPointerSizeLog2 - kSmiTagSize);
1503 __ Addu(sp, sp, t0);
1504 // Adjust for the receiver.
1505 __ Addu(sp, sp, Operand(kPointerSize));
Andrei Popescu31002712010-02-23 13:46:05 +00001506}
1507
1508
1509void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001510 // State setup as expected by MacroAssembler::InvokePrologue.
1511 // ----------- S t a t e -------------
1512 // -- a0: actual arguments count
1513 // -- a1: function (passed through to callee)
1514 // -- a2: expected arguments count
1515 // -- a3: callee code entry
1516 // -- t1: call kind information
1517 // -----------------------------------
1518
1519 Label invoke, dont_adapt_arguments;
1520
1521 Label enough, too_few;
1522 __ Branch(&dont_adapt_arguments, eq,
1523 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1524 // We use Uless as the number of argument should always be greater than 0.
1525 __ Branch(&too_few, Uless, a0, Operand(a2));
1526
1527 { // Enough parameters: actual >= expected.
1528 // a0: actual number of arguments as a smi
1529 // a1: function
1530 // a2: expected number of arguments
1531 // a3: code entry to call
1532 __ bind(&enough);
1533 EnterArgumentsAdaptorFrame(masm);
1534
1535 // Calculate copy start address into a0 and copy end address into a2.
1536 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1537 __ Addu(a0, fp, a0);
1538 // Adjust for return address and receiver.
1539 __ Addu(a0, a0, Operand(2 * kPointerSize));
1540 // Compute copy end address.
1541 __ sll(a2, a2, kPointerSizeLog2);
1542 __ subu(a2, a0, a2);
1543
1544 // Copy the arguments (including the receiver) to the new stack frame.
1545 // a0: copy start address
1546 // a1: function
1547 // a2: copy end address
1548 // a3: code entry to call
1549
1550 Label copy;
1551 __ bind(&copy);
1552 __ lw(t0, MemOperand(a0));
1553 __ push(t0);
1554 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a2));
1555 __ addiu(a0, a0, -kPointerSize); // In delay slot.
1556
1557 __ jmp(&invoke);
1558 }
1559
1560 { // Too few parameters: Actual < expected.
1561 __ bind(&too_few);
1562 EnterArgumentsAdaptorFrame(masm);
1563
1564 // TODO(MIPS): Optimize these loops.
1565
1566 // Calculate copy start address into a0 and copy end address is fp.
1567 // a0: actual number of arguments as a smi
1568 // a1: function
1569 // a2: expected number of arguments
1570 // a3: code entry to call
1571 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1572 __ Addu(a0, fp, a0);
1573 // Adjust for return address and receiver.
1574 __ Addu(a0, a0, Operand(2 * kPointerSize));
1575 // Compute copy end address. Also adjust for return address.
1576 __ Addu(t1, fp, kPointerSize);
1577
1578 // Copy the arguments (including the receiver) to the new stack frame.
1579 // a0: copy start address
1580 // a1: function
1581 // a2: expected number of arguments
1582 // a3: code entry to call
1583 // t1: copy end address
1584 Label copy;
1585 __ bind(&copy);
1586 __ lw(t0, MemOperand(a0)); // Adjusted above for return addr and receiver.
1587 __ push(t0);
1588 __ Subu(a0, a0, kPointerSize);
1589 __ Branch(&copy, ne, a0, Operand(t1));
1590
1591 // Fill the remaining expected arguments with undefined.
1592 // a1: function
1593 // a2: expected number of arguments
1594 // a3: code entry to call
1595 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
1596 __ sll(t2, a2, kPointerSizeLog2);
1597 __ Subu(a2, fp, Operand(t2));
1598 __ Addu(a2, a2, Operand(-4 * kPointerSize)); // Adjust for frame.
1599
1600 Label fill;
1601 __ bind(&fill);
1602 __ push(t0);
1603 __ Branch(&fill, ne, sp, Operand(a2));
1604 }
1605
1606 // Call the entry point.
1607 __ bind(&invoke);
1608
1609 __ Call(a3);
1610
1611 // Exit frame and return.
1612 LeaveArgumentsAdaptorFrame(masm);
1613 __ Ret();
1614
1615
1616 // -------------------------------------------
1617 // Don't adapt arguments.
1618 // -------------------------------------------
1619 __ bind(&dont_adapt_arguments);
1620 __ Jump(a3);
Andrei Popescu31002712010-02-23 13:46:05 +00001621}
1622
1623
1624#undef __
1625
1626} } // namespace v8::internal
1627
Leon Clarkef7060e22010-06-03 12:02:55 +01001628#endif // V8_TARGET_ARCH_MIPS