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