Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | |
| 29 | |
| 30 | #include "v8.h" |
| 31 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 32 | #if defined(V8_TARGET_ARCH_MIPS) |
| 33 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 34 | #include "codegen.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 35 | #include "debug.h" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 36 | #include "deoptimizer.h" |
| 37 | #include "full-codegen.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 38 | #include "runtime.h" |
| 39 | |
| 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | |
| 44 | #define __ ACCESS_MASM(masm) |
| 45 | |
| 46 | |
| 47 | void Builtins::Generate_Adaptor(MacroAssembler* masm, |
| 48 | CFunctionId id, |
| 49 | BuiltinExtraArguments extra_args) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 50 | // ----------- 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. |
| 78 | static 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. |
| 94 | static 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. |
| 100 | static 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. |
| 178 | static 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(¬_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(¬_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. |
| 312 | static 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); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 347 | __ Branch(call_generic_code, Ugreater_equal, a2, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 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 Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | |
| 420 | void Builtins::Generate_ArrayCode(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 421 | // ----------- 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 Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | |
| 455 | void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 456 | // ----------- 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 Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 490 | void Builtins::Generate_StringConstructCode(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 491 | // ----------- 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 | ¬_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(¬_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 Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 617 | void Builtins::Generate_JSConstructCall(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 618 | // ----------- 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)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 637 | __ Jump(t9); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 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 | |
| 654 | static 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, |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 945 | RelocInfo::CODE_TARGET, CALL_FUNCTION, CALL_AS_METHOD); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 946 | } else { |
| 947 | ParameterCount actual(a0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 948 | __ InvokeFunction(a1, actual, CALL_FUNCTION, |
| 949 | NullCallWrapper(), CALL_AS_METHOD); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | // Pop the function from the stack. |
| 953 | // v0: result |
| 954 | // sp[0]: constructor function |
| 955 | // sp[2]: receiver |
| 956 | // sp[3]: constructor function |
| 957 | // sp[4]: number of arguments (smi-tagged) |
| 958 | __ Pop(); |
| 959 | |
| 960 | // Restore context from the frame. |
| 961 | __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset)); |
| 962 | |
| 963 | // If the result is an object (in the ECMA sense), we should get rid |
| 964 | // of the receiver and use the result; see ECMA-262 section 13.2.2-7 |
| 965 | // on page 74. |
| 966 | Label use_receiver, exit; |
| 967 | |
| 968 | // If the result is a smi, it is *not* an object in the ECMA sense. |
| 969 | // v0: result |
| 970 | // sp[0]: receiver (newly allocated object) |
| 971 | // sp[1]: constructor function |
| 972 | // sp[2]: number of arguments (smi-tagged) |
| 973 | __ And(t0, v0, Operand(kSmiTagMask)); |
| 974 | __ Branch(&use_receiver, eq, t0, Operand(zero_reg)); |
| 975 | |
| 976 | // If the type of the result (stored in its map) is less than |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 977 | // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 978 | __ GetObjectType(v0, a3, a3); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 979 | __ Branch(&exit, greater_equal, a3, Operand(FIRST_SPEC_OBJECT_TYPE)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 980 | |
| 981 | // Throw away the result of the constructor invocation and use the |
| 982 | // on-stack receiver as the result. |
| 983 | __ bind(&use_receiver); |
| 984 | __ lw(v0, MemOperand(sp)); |
| 985 | |
| 986 | // Remove receiver from the stack, remove caller arguments, and |
| 987 | // return. |
| 988 | __ bind(&exit); |
| 989 | // v0: result |
| 990 | // sp[0]: receiver (newly allocated object) |
| 991 | // sp[1]: constructor function |
| 992 | // sp[2]: number of arguments (smi-tagged) |
| 993 | __ lw(a1, MemOperand(sp, 2 * kPointerSize)); |
| 994 | __ LeaveConstructFrame(); |
| 995 | __ sll(t0, a1, kPointerSizeLog2 - 1); |
| 996 | __ Addu(sp, sp, t0); |
| 997 | __ Addu(sp, sp, kPointerSize); |
| 998 | __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2); |
| 999 | __ Ret(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1003 | void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1004 | Generate_JSConstructStubHelper(masm, false, true); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1008 | void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1009 | Generate_JSConstructStubHelper(masm, false, false); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | |
| 1013 | void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1014 | Generate_JSConstructStubHelper(masm, true, false); |
| 1015 | } |
| 1016 | |
| 1017 | |
| 1018 | static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm, |
| 1019 | bool is_construct) { |
| 1020 | // Called from JSEntryStub::GenerateBody |
| 1021 | |
| 1022 | // ----------- S t a t e ------------- |
| 1023 | // -- a0: code entry |
| 1024 | // -- a1: function |
| 1025 | // -- a2: reveiver_pointer |
| 1026 | // -- a3: argc |
| 1027 | // -- s0: argv |
| 1028 | // ----------------------------------- |
| 1029 | |
| 1030 | // Clear the context before we push it when entering the JS frame. |
| 1031 | __ mov(cp, zero_reg); |
| 1032 | |
| 1033 | // Enter an internal frame. |
| 1034 | __ EnterInternalFrame(); |
| 1035 | |
| 1036 | // Set up the context from the function argument. |
| 1037 | __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset)); |
| 1038 | |
| 1039 | // Set up the roots register. |
| 1040 | ExternalReference roots_address = |
| 1041 | ExternalReference::roots_address(masm->isolate()); |
| 1042 | __ li(s6, Operand(roots_address)); |
| 1043 | |
| 1044 | // Push the function and the receiver onto the stack. |
| 1045 | __ Push(a1, a2); |
| 1046 | |
| 1047 | // Copy arguments to the stack in a loop. |
| 1048 | // a3: argc |
| 1049 | // s0: argv, ie points to first arg |
| 1050 | Label loop, entry; |
| 1051 | __ sll(t0, a3, kPointerSizeLog2); |
| 1052 | __ addu(t2, s0, t0); |
| 1053 | __ b(&entry); |
| 1054 | __ nop(); // Branch delay slot nop. |
| 1055 | // t2 points past last arg. |
| 1056 | __ bind(&loop); |
| 1057 | __ lw(t0, MemOperand(s0)); // Read next parameter. |
| 1058 | __ addiu(s0, s0, kPointerSize); |
| 1059 | __ lw(t0, MemOperand(t0)); // Dereference handle. |
| 1060 | __ push(t0); // Push parameter. |
| 1061 | __ bind(&entry); |
| 1062 | __ Branch(&loop, ne, s0, Operand(t2)); |
| 1063 | |
| 1064 | // Initialize all JavaScript callee-saved registers, since they will be seen |
| 1065 | // by the garbage collector as part of handlers. |
| 1066 | __ LoadRoot(t0, Heap::kUndefinedValueRootIndex); |
| 1067 | __ mov(s1, t0); |
| 1068 | __ mov(s2, t0); |
| 1069 | __ mov(s3, t0); |
| 1070 | __ mov(s4, t0); |
| 1071 | __ mov(s5, t0); |
| 1072 | // s6 holds the root address. Do not clobber. |
| 1073 | // s7 is cp. Do not init. |
| 1074 | |
| 1075 | // Invoke the code and pass argc as a0. |
| 1076 | __ mov(a0, a3); |
| 1077 | if (is_construct) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1078 | __ Call(masm->isolate()->builtins()->JSConstructCall()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1079 | } else { |
| 1080 | ParameterCount actual(a0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1081 | __ InvokeFunction(a1, actual, CALL_FUNCTION, |
| 1082 | NullCallWrapper(), CALL_AS_METHOD); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | __ LeaveInternalFrame(); |
| 1086 | |
| 1087 | __ Jump(ra); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1091 | void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1092 | Generate_JSEntryTrampolineHelper(masm, false); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1097 | Generate_JSEntryTrampolineHelper(masm, true); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | |
| 1101 | void Builtins::Generate_LazyCompile(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1102 | // Enter an internal frame. |
| 1103 | __ EnterInternalFrame(); |
| 1104 | |
| 1105 | // Preserve the function. |
| 1106 | __ push(a1); |
| 1107 | // Push call kind information. |
| 1108 | __ push(t1); |
| 1109 | |
| 1110 | // Push the function on the stack as the argument to the runtime function. |
| 1111 | __ push(a1); |
| 1112 | // Call the runtime function. |
| 1113 | __ CallRuntime(Runtime::kLazyCompile, 1); |
| 1114 | // Calculate the entry point. |
| 1115 | __ addiu(t9, v0, Code::kHeaderSize - kHeapObjectTag); |
| 1116 | |
| 1117 | // Restore call kind information. |
| 1118 | __ pop(t1); |
| 1119 | // Restore saved function. |
| 1120 | __ pop(a1); |
| 1121 | |
| 1122 | // Tear down temporary frame. |
| 1123 | __ LeaveInternalFrame(); |
| 1124 | |
| 1125 | // Do a tail-call of the compiled function. |
| 1126 | __ Jump(t9); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | |
| 1130 | void Builtins::Generate_LazyRecompile(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1131 | // Enter an internal frame. |
| 1132 | __ EnterInternalFrame(); |
| 1133 | |
| 1134 | // Preserve the function. |
| 1135 | __ push(a1); |
| 1136 | // Push call kind information. |
| 1137 | __ push(t1); |
| 1138 | |
| 1139 | // Push the function on the stack as the argument to the runtime function. |
| 1140 | __ push(a1); |
| 1141 | __ CallRuntime(Runtime::kLazyRecompile, 1); |
| 1142 | // Calculate the entry point. |
| 1143 | __ Addu(t9, v0, Operand(Code::kHeaderSize - kHeapObjectTag)); |
| 1144 | |
| 1145 | // Restore call kind information. |
| 1146 | __ pop(t1); |
| 1147 | // Restore saved function. |
| 1148 | __ pop(a1); |
| 1149 | |
| 1150 | // Tear down temporary frame. |
| 1151 | __ LeaveInternalFrame(); |
| 1152 | |
| 1153 | // Do a tail-call of the compiled function. |
| 1154 | __ Jump(t9); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1158 | // These functions are called from C++ but cannot be used in live code. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1159 | void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1160 | __ Abort("Call to unimplemented function in builtins-mips.cc"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | |
| 1164 | void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1165 | __ Abort("Call to unimplemented function in builtins-mips.cc"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | |
| 1169 | void Builtins::Generate_NotifyOSR(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1170 | __ Abort("Call to unimplemented function in builtins-mips.cc"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1175 | __ Abort("Call to unimplemented function in builtins-mips.cc"); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | |
| 1179 | void Builtins::Generate_FunctionCall(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1180 | // 1. Make sure we have at least one argument. |
| 1181 | // a0: actual number of arguments |
| 1182 | { Label done; |
| 1183 | __ Branch(&done, ne, a0, Operand(zero_reg)); |
| 1184 | __ LoadRoot(t2, Heap::kUndefinedValueRootIndex); |
| 1185 | __ push(t2); |
| 1186 | __ Addu(a0, a0, Operand(1)); |
| 1187 | __ bind(&done); |
| 1188 | } |
| 1189 | |
| 1190 | // 2. Get the function to call (passed as receiver) from the stack, check |
| 1191 | // if it is a function. |
| 1192 | // a0: actual number of arguments |
| 1193 | Label non_function; |
| 1194 | __ sll(at, a0, kPointerSizeLog2); |
| 1195 | __ addu(at, sp, at); |
| 1196 | __ lw(a1, MemOperand(at)); |
| 1197 | __ And(at, a1, Operand(kSmiTagMask)); |
| 1198 | __ Branch(&non_function, eq, at, Operand(zero_reg)); |
| 1199 | __ GetObjectType(a1, a2, a2); |
| 1200 | __ Branch(&non_function, ne, a2, Operand(JS_FUNCTION_TYPE)); |
| 1201 | |
| 1202 | // 3a. Patch the first argument if necessary when calling a function. |
| 1203 | // a0: actual number of arguments |
| 1204 | // a1: function |
| 1205 | Label shift_arguments; |
| 1206 | { Label convert_to_object, use_global_receiver, patch_receiver; |
| 1207 | // Change context eagerly in case we need the global receiver. |
| 1208 | __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset)); |
| 1209 | |
| 1210 | // Do not transform the receiver for strict mode functions. |
| 1211 | __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset)); |
| 1212 | __ lw(a3, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset)); |
| 1213 | __ And(t0, a3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction + |
| 1214 | kSmiTagSize))); |
| 1215 | __ Branch(&shift_arguments, ne, t0, Operand(zero_reg)); |
| 1216 | |
| 1217 | // Do not transform the receiver for native (Compilerhints already in a3). |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1218 | __ And(t0, a3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1236 | STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1237 | __ GetObjectType(a2, a3, a3); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1238 | __ Branch(&shift_arguments, ge, a3, Operand(FIRST_SPEC_OBJECT_TYPE)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1239 | |
| 1240 | __ bind(&convert_to_object); |
| 1241 | __ EnterInternalFrame(); // In order to preserve argument count. |
| 1242 | __ sll(a0, a0, kSmiTagSize); // Smi tagged. |
| 1243 | __ push(a0); |
| 1244 | |
| 1245 | __ push(a2); |
| 1246 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION); |
| 1247 | __ mov(a2, v0); |
| 1248 | |
| 1249 | __ pop(a0); |
| 1250 | __ sra(a0, a0, kSmiTagSize); // Un-tag. |
| 1251 | __ LeaveInternalFrame(); |
| 1252 | // Restore the function to a1. |
| 1253 | __ sll(at, a0, kPointerSizeLog2); |
| 1254 | __ addu(at, sp, at); |
| 1255 | __ lw(a1, MemOperand(at)); |
| 1256 | __ Branch(&patch_receiver); |
| 1257 | |
| 1258 | // Use the global receiver object from the called function as the |
| 1259 | // receiver. |
| 1260 | __ bind(&use_global_receiver); |
| 1261 | const int kGlobalIndex = |
| 1262 | Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize; |
| 1263 | __ lw(a2, FieldMemOperand(cp, kGlobalIndex)); |
| 1264 | __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalContextOffset)); |
| 1265 | __ lw(a2, FieldMemOperand(a2, kGlobalIndex)); |
| 1266 | __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalReceiverOffset)); |
| 1267 | |
| 1268 | __ bind(&patch_receiver); |
| 1269 | __ sll(at, a0, kPointerSizeLog2); |
| 1270 | __ addu(a3, sp, at); |
| 1271 | __ sw(a2, MemOperand(a3, -kPointerSize)); |
| 1272 | |
| 1273 | __ Branch(&shift_arguments); |
| 1274 | } |
| 1275 | |
| 1276 | // 3b. Patch the first argument when calling a non-function. The |
| 1277 | // CALL_NON_FUNCTION builtin expects the non-function callee as |
| 1278 | // receiver, so overwrite the first argument which will ultimately |
| 1279 | // become the receiver. |
| 1280 | // a0: actual number of arguments |
| 1281 | // a1: function |
| 1282 | __ bind(&non_function); |
| 1283 | // Restore the function in case it has been modified. |
| 1284 | __ sll(at, a0, kPointerSizeLog2); |
| 1285 | __ addu(a2, sp, at); |
| 1286 | __ sw(a1, MemOperand(a2, -kPointerSize)); |
| 1287 | // Clear a1 to indicate a non-function being called. |
| 1288 | __ mov(a1, zero_reg); |
| 1289 | |
| 1290 | // 4. Shift arguments and return address one slot down on the stack |
| 1291 | // (overwriting the original receiver). Adjust argument count to make |
| 1292 | // the original first argument the new receiver. |
| 1293 | // a0: actual number of arguments |
| 1294 | // a1: function |
| 1295 | __ bind(&shift_arguments); |
| 1296 | { Label loop; |
| 1297 | // Calculate the copy start address (destination). Copy end address is sp. |
| 1298 | __ sll(at, a0, kPointerSizeLog2); |
| 1299 | __ addu(a2, sp, at); |
| 1300 | |
| 1301 | __ bind(&loop); |
| 1302 | __ lw(at, MemOperand(a2, -kPointerSize)); |
| 1303 | __ sw(at, MemOperand(a2)); |
| 1304 | __ Subu(a2, a2, Operand(kPointerSize)); |
| 1305 | __ Branch(&loop, ne, a2, Operand(sp)); |
| 1306 | // Adjust the actual number of arguments and remove the top element |
| 1307 | // (which is a copy of the last argument). |
| 1308 | __ Subu(a0, a0, Operand(1)); |
| 1309 | __ Pop(); |
| 1310 | } |
| 1311 | |
| 1312 | // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin. |
| 1313 | // a0: actual number of arguments |
| 1314 | // a1: function |
| 1315 | { Label function; |
| 1316 | __ Branch(&function, ne, a1, Operand(zero_reg)); |
| 1317 | __ mov(a2, zero_reg); // expected arguments is 0 for CALL_NON_FUNCTION |
| 1318 | __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION); |
| 1319 | __ SetCallKind(t1, CALL_AS_METHOD); |
| 1320 | __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(), |
| 1321 | RelocInfo::CODE_TARGET); |
| 1322 | __ bind(&function); |
| 1323 | } |
| 1324 | |
| 1325 | // 5b. Get the code to call from the function and check that the number of |
| 1326 | // expected arguments matches what we're providing. If so, jump |
| 1327 | // (tail-call) to the code in register edx without checking arguments. |
| 1328 | // a0: actual number of arguments |
| 1329 | // a1: function |
| 1330 | __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset)); |
| 1331 | __ lw(a2, |
| 1332 | FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset)); |
| 1333 | __ sra(a2, a2, kSmiTagSize); |
| 1334 | __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset)); |
| 1335 | __ SetCallKind(t1, CALL_AS_METHOD); |
| 1336 | // Check formal and actual parameter counts. |
| 1337 | __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(), |
| 1338 | RelocInfo::CODE_TARGET, ne, a2, Operand(a0)); |
| 1339 | |
| 1340 | ParameterCount expected(0); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1341 | __ InvokeCode(a3, expected, expected, JUMP_FUNCTION, |
| 1342 | NullCallWrapper(), CALL_AS_METHOD); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
| 1345 | |
| 1346 | void Builtins::Generate_FunctionApply(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1347 | 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). |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1404 | __ And(t0, a2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize))); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1405 | __ Branch(&push_receiver, ne, t0, Operand(zero_reg)); |
| 1406 | |
| 1407 | // Compute the receiver in non-strict mode. |
| 1408 | __ And(t0, a0, Operand(kSmiTagMask)); |
| 1409 | __ Branch(&call_to_object, eq, t0, Operand(zero_reg)); |
| 1410 | __ LoadRoot(a1, Heap::kNullValueRootIndex); |
| 1411 | __ Branch(&use_global_receiver, eq, a0, Operand(a1)); |
| 1412 | __ LoadRoot(a2, Heap::kUndefinedValueRootIndex); |
| 1413 | __ Branch(&use_global_receiver, eq, a0, Operand(a2)); |
| 1414 | |
| 1415 | // Check if the receiver is already a JavaScript object. |
| 1416 | // a0: receiver |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1417 | STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1418 | __ GetObjectType(a0, a1, a1); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1419 | __ Branch(&push_receiver, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1420 | |
| 1421 | // Convert the receiver to a regular object. |
| 1422 | // a0: receiver |
| 1423 | __ bind(&call_to_object); |
| 1424 | __ push(a0); |
| 1425 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION); |
| 1426 | __ mov(a0, v0); // Put object in a0 to match other paths to push_receiver. |
| 1427 | __ Branch(&push_receiver); |
| 1428 | |
| 1429 | // Use the current global receiver object as the receiver. |
| 1430 | __ bind(&use_global_receiver); |
| 1431 | const int kGlobalOffset = |
| 1432 | Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize; |
| 1433 | __ lw(a0, FieldMemOperand(cp, kGlobalOffset)); |
| 1434 | __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalContextOffset)); |
| 1435 | __ lw(a0, FieldMemOperand(a0, kGlobalOffset)); |
| 1436 | __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalReceiverOffset)); |
| 1437 | |
| 1438 | // Push the receiver. |
| 1439 | // a0: receiver |
| 1440 | __ bind(&push_receiver); |
| 1441 | __ push(a0); |
| 1442 | |
| 1443 | // Copy all arguments from the array to the stack. |
| 1444 | Label entry, loop; |
| 1445 | __ lw(a0, MemOperand(fp, kIndexOffset)); |
| 1446 | __ Branch(&entry); |
| 1447 | |
| 1448 | // Load the current argument from the arguments array and push it to the |
| 1449 | // stack. |
| 1450 | // a0: current argument index |
| 1451 | __ bind(&loop); |
| 1452 | __ lw(a1, MemOperand(fp, kArgsOffset)); |
| 1453 | __ push(a1); |
| 1454 | __ push(a0); |
| 1455 | |
| 1456 | // Call the runtime to access the property in the arguments array. |
| 1457 | __ CallRuntime(Runtime::kGetProperty, 2); |
| 1458 | __ push(v0); |
| 1459 | |
| 1460 | // Use inline caching to access the arguments. |
| 1461 | __ lw(a0, MemOperand(fp, kIndexOffset)); |
| 1462 | __ Addu(a0, a0, Operand(1 << kSmiTagSize)); |
| 1463 | __ sw(a0, MemOperand(fp, kIndexOffset)); |
| 1464 | |
| 1465 | // Test if the copy loop has finished copying all the elements from the |
| 1466 | // arguments object. |
| 1467 | __ bind(&entry); |
| 1468 | __ lw(a1, MemOperand(fp, kLimitOffset)); |
| 1469 | __ Branch(&loop, ne, a0, Operand(a1)); |
| 1470 | // Invoke the function. |
| 1471 | ParameterCount actual(a0); |
| 1472 | __ sra(a0, a0, kSmiTagSize); |
| 1473 | __ lw(a1, MemOperand(fp, kFunctionOffset)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1474 | __ InvokeFunction(a1, actual, CALL_FUNCTION, |
| 1475 | NullCallWrapper(), CALL_AS_METHOD); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Tear down the internal frame and remove function, receiver and args. |
| 1478 | __ LeaveInternalFrame(); |
| 1479 | __ Addu(sp, sp, Operand(3 * kPointerSize)); |
| 1480 | __ Ret(); |
| 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) { |
| 1485 | __ sll(a0, a0, kSmiTagSize); |
| 1486 | __ li(t0, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
| 1487 | __ MultiPush(a0.bit() | a1.bit() | t0.bit() | fp.bit() | ra.bit()); |
| 1488 | __ Addu(fp, sp, Operand(3 * kPointerSize)); |
| 1489 | } |
| 1490 | |
| 1491 | |
| 1492 | static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) { |
| 1493 | // ----------- S t a t e ------------- |
| 1494 | // -- v0 : result being passed through |
| 1495 | // ----------------------------------- |
| 1496 | // Get the number of arguments passed (as a smi), tear down the frame and |
| 1497 | // then tear down the parameters. |
| 1498 | __ lw(a1, MemOperand(fp, -3 * kPointerSize)); |
| 1499 | __ mov(sp, fp); |
| 1500 | __ MultiPop(fp.bit() | ra.bit()); |
| 1501 | __ sll(t0, a1, kPointerSizeLog2 - kSmiTagSize); |
| 1502 | __ Addu(sp, sp, t0); |
| 1503 | // Adjust for the receiver. |
| 1504 | __ Addu(sp, sp, Operand(kPointerSize)); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | |
| 1508 | void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1509 | // State setup as expected by MacroAssembler::InvokePrologue. |
| 1510 | // ----------- S t a t e ------------- |
| 1511 | // -- a0: actual arguments count |
| 1512 | // -- a1: function (passed through to callee) |
| 1513 | // -- a2: expected arguments count |
| 1514 | // -- a3: callee code entry |
| 1515 | // -- t1: call kind information |
| 1516 | // ----------------------------------- |
| 1517 | |
| 1518 | Label invoke, dont_adapt_arguments; |
| 1519 | |
| 1520 | Label enough, too_few; |
| 1521 | __ Branch(&dont_adapt_arguments, eq, |
| 1522 | a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel)); |
| 1523 | // We use Uless as the number of argument should always be greater than 0. |
| 1524 | __ Branch(&too_few, Uless, a0, Operand(a2)); |
| 1525 | |
| 1526 | { // Enough parameters: actual >= expected. |
| 1527 | // a0: actual number of arguments as a smi |
| 1528 | // a1: function |
| 1529 | // a2: expected number of arguments |
| 1530 | // a3: code entry to call |
| 1531 | __ bind(&enough); |
| 1532 | EnterArgumentsAdaptorFrame(masm); |
| 1533 | |
| 1534 | // Calculate copy start address into a0 and copy end address into a2. |
| 1535 | __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize); |
| 1536 | __ Addu(a0, fp, a0); |
| 1537 | // Adjust for return address and receiver. |
| 1538 | __ Addu(a0, a0, Operand(2 * kPointerSize)); |
| 1539 | // Compute copy end address. |
| 1540 | __ sll(a2, a2, kPointerSizeLog2); |
| 1541 | __ subu(a2, a0, a2); |
| 1542 | |
| 1543 | // Copy the arguments (including the receiver) to the new stack frame. |
| 1544 | // a0: copy start address |
| 1545 | // a1: function |
| 1546 | // a2: copy end address |
| 1547 | // a3: code entry to call |
| 1548 | |
| 1549 | Label copy; |
| 1550 | __ bind(©); |
| 1551 | __ lw(t0, MemOperand(a0)); |
| 1552 | __ push(t0); |
| 1553 | __ Branch(USE_DELAY_SLOT, ©, ne, a0, Operand(a2)); |
| 1554 | __ addiu(a0, a0, -kPointerSize); // In delay slot. |
| 1555 | |
| 1556 | __ jmp(&invoke); |
| 1557 | } |
| 1558 | |
| 1559 | { // Too few parameters: Actual < expected. |
| 1560 | __ bind(&too_few); |
| 1561 | EnterArgumentsAdaptorFrame(masm); |
| 1562 | |
| 1563 | // TODO(MIPS): Optimize these loops. |
| 1564 | |
| 1565 | // Calculate copy start address into a0 and copy end address is fp. |
| 1566 | // a0: actual number of arguments as a smi |
| 1567 | // a1: function |
| 1568 | // a2: expected number of arguments |
| 1569 | // a3: code entry to call |
| 1570 | __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize); |
| 1571 | __ Addu(a0, fp, a0); |
| 1572 | // Adjust for return address and receiver. |
| 1573 | __ Addu(a0, a0, Operand(2 * kPointerSize)); |
| 1574 | // Compute copy end address. Also adjust for return address. |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1575 | __ Addu(t3, fp, kPointerSize); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1576 | |
| 1577 | // Copy the arguments (including the receiver) to the new stack frame. |
| 1578 | // a0: copy start address |
| 1579 | // a1: function |
| 1580 | // a2: expected number of arguments |
| 1581 | // a3: code entry to call |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1582 | // t3: copy end address |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1583 | Label copy; |
| 1584 | __ bind(©); |
| 1585 | __ lw(t0, MemOperand(a0)); // Adjusted above for return addr and receiver. |
| 1586 | __ push(t0); |
| 1587 | __ Subu(a0, a0, kPointerSize); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 1588 | __ Branch(©, ne, a0, Operand(t3)); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1589 | |
| 1590 | // Fill the remaining expected arguments with undefined. |
| 1591 | // a1: function |
| 1592 | // a2: expected number of arguments |
| 1593 | // a3: code entry to call |
| 1594 | __ LoadRoot(t0, Heap::kUndefinedValueRootIndex); |
| 1595 | __ sll(t2, a2, kPointerSizeLog2); |
| 1596 | __ Subu(a2, fp, Operand(t2)); |
| 1597 | __ Addu(a2, a2, Operand(-4 * kPointerSize)); // Adjust for frame. |
| 1598 | |
| 1599 | Label fill; |
| 1600 | __ bind(&fill); |
| 1601 | __ push(t0); |
| 1602 | __ Branch(&fill, ne, sp, Operand(a2)); |
| 1603 | } |
| 1604 | |
| 1605 | // Call the entry point. |
| 1606 | __ bind(&invoke); |
| 1607 | |
| 1608 | __ Call(a3); |
| 1609 | |
| 1610 | // Exit frame and return. |
| 1611 | LeaveArgumentsAdaptorFrame(masm); |
| 1612 | __ Ret(); |
| 1613 | |
| 1614 | |
| 1615 | // ------------------------------------------- |
| 1616 | // Don't adapt arguments. |
| 1617 | // ------------------------------------------- |
| 1618 | __ bind(&dont_adapt_arguments); |
| 1619 | __ Jump(a3); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | |
| 1623 | #undef __ |
| 1624 | |
| 1625 | } } // namespace v8::internal |
| 1626 | |
Leon Clarke | f7060e2 | 2010-06-03 12:02:55 +0100 | [diff] [blame] | 1627 | #endif // V8_TARGET_ARCH_MIPS |