rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +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 | #include "v8.h" |
| 29 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 30 | #if V8_TARGET_ARCH_IA32 |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 31 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 32 | #include "codegen.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "deoptimizer.h" |
| 34 | #include "full-codegen.h" |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
| 39 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 40 | #define __ ACCESS_MASM(masm) |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | |
| 42 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 43 | void Builtins::Generate_Adaptor(MacroAssembler* masm, |
| 44 | CFunctionId id, |
| 45 | BuiltinExtraArguments extra_args) { |
| 46 | // ----------- S t a t e ------------- |
| 47 | // -- eax : number of arguments excluding receiver |
| 48 | // -- edi : called function (only guaranteed when |
| 49 | // extra_args requires it) |
| 50 | // -- esi : context |
| 51 | // -- esp[0] : return address |
| 52 | // -- esp[4] : last argument |
| 53 | // -- ... |
| 54 | // -- esp[4 * argc] : first argument (argc == eax) |
| 55 | // -- esp[4 * (argc +1)] : receiver |
| 56 | // ----------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 57 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 58 | // Insert extra arguments. |
| 59 | int num_extra_args = 0; |
| 60 | if (extra_args == NEEDS_CALLED_FUNCTION) { |
| 61 | num_extra_args = 1; |
| 62 | Register scratch = ebx; |
| 63 | __ pop(scratch); // Save return address. |
| 64 | __ push(edi); |
| 65 | __ push(scratch); // Restore return address. |
| 66 | } else { |
| 67 | ASSERT(extra_args == NO_EXTRA_ARGUMENTS); |
| 68 | } |
| 69 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 70 | // JumpToExternalReference expects eax to contain the number of arguments |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 71 | // including the receiver and the extra arguments. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 72 | __ add(eax, Immediate(num_extra_args + 1)); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 73 | __ JumpToExternalReference(ExternalReference(id, masm->isolate())); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 77 | static void CallRuntimePassFunction(MacroAssembler* masm, |
| 78 | Runtime::FunctionId function_id) { |
| 79 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 80 | // Push a copy of the function. |
| 81 | __ push(edi); |
| 82 | // Push call kind information. |
| 83 | __ push(ecx); |
| 84 | // Function is also the parameter to the runtime call. |
| 85 | __ push(edi); |
| 86 | |
| 87 | __ CallRuntime(function_id, 1); |
| 88 | // Restore call kind information. |
| 89 | __ pop(ecx); |
| 90 | // Restore receiver. |
| 91 | __ pop(edi); |
| 92 | } |
| 93 | |
| 94 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 95 | static void GenerateTailCallToSharedCode(MacroAssembler* masm) { |
| 96 | __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 97 | __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kCodeOffset)); |
| 98 | __ lea(eax, FieldOperand(eax, Code::kHeaderSize)); |
| 99 | __ jmp(eax); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | void Builtins::Generate_InRecompileQueue(MacroAssembler* masm) { |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 104 | // Checking whether the queued function is ready for install is optional, |
| 105 | // since we come across interrupts and stack checks elsewhere. However, |
| 106 | // not checking may delay installing ready functions, and always checking |
| 107 | // would be quite expensive. A good compromise is to first check against |
| 108 | // stack limit as a cue for an interrupt signal. |
| 109 | Label ok; |
| 110 | ExternalReference stack_limit = |
| 111 | ExternalReference::address_of_stack_limit(masm->isolate()); |
| 112 | __ cmp(esp, Operand::StaticVariable(stack_limit)); |
| 113 | __ j(above_equal, &ok, Label::kNear); |
| 114 | |
| 115 | CallRuntimePassFunction(masm, Runtime::kTryInstallRecompiledCode); |
| 116 | // Tail call to returned code. |
| 117 | __ lea(eax, FieldOperand(eax, Code::kHeaderSize)); |
| 118 | __ jmp(eax); |
| 119 | |
| 120 | __ bind(&ok); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 121 | GenerateTailCallToSharedCode(masm); |
| 122 | } |
| 123 | |
| 124 | |
rossberg@chromium.org | 9259716 | 2013-08-23 13:28:00 +0000 | [diff] [blame] | 125 | void Builtins::Generate_ConcurrentRecompile(MacroAssembler* masm) { |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 126 | CallRuntimePassFunction(masm, Runtime::kConcurrentRecompile); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 127 | GenerateTailCallToSharedCode(masm); |
| 128 | } |
| 129 | |
| 130 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 131 | static void Generate_JSConstructStubHelper(MacroAssembler* masm, |
| 132 | bool is_api_function, |
| 133 | bool count_constructions) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 134 | // ----------- S t a t e ------------- |
| 135 | // -- eax: number of arguments |
| 136 | // -- edi: constructor function |
| 137 | // ----------------------------------- |
| 138 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 139 | // Should never count constructions for api objects. |
| 140 | ASSERT(!is_api_function || !count_constructions); |
| 141 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 142 | // Enter a construct frame. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 143 | { |
| 144 | FrameScope scope(masm, StackFrame::CONSTRUCT); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 145 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 146 | // Store a smi-tagged arguments count on the stack. |
| 147 | __ SmiTag(eax); |
| 148 | __ push(eax); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 149 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 150 | // Push the function to invoke on the stack. |
| 151 | __ push(edi); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 152 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 153 | // Try to allocate the object without transitioning into C code. If any of |
| 154 | // the preconditions is not met, the code bails out to the runtime call. |
| 155 | Label rt_call, allocated; |
| 156 | if (FLAG_inline_new) { |
| 157 | Label undo_allocation; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 158 | #ifdef ENABLE_DEBUGGER_SUPPORT |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 159 | ExternalReference debug_step_in_fp = |
| 160 | ExternalReference::debug_step_in_fp_address(masm->isolate()); |
| 161 | __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0)); |
| 162 | __ j(not_equal, &rt_call); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 163 | #endif |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 165 | // Verified that the constructor is a JSFunction. |
| 166 | // Load the initial map and verify that it is in fact a map. |
| 167 | // edi: constructor |
| 168 | __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset)); |
| 169 | // Will both indicate a NULL and a Smi |
| 170 | __ JumpIfSmi(eax, &rt_call); |
| 171 | // edi: constructor |
| 172 | // eax: initial map (if proven valid below) |
| 173 | __ CmpObjectType(eax, MAP_TYPE, ebx); |
| 174 | __ j(not_equal, &rt_call); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 175 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 176 | // Check that the constructor is not constructing a JSFunction (see |
| 177 | // comments in Runtime_NewObject in runtime.cc). In which case the |
| 178 | // initial map's instance type would be JS_FUNCTION_TYPE. |
| 179 | // edi: constructor |
| 180 | // eax: initial map |
| 181 | __ CmpInstanceType(eax, JS_FUNCTION_TYPE); |
| 182 | __ j(equal, &rt_call); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 183 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 184 | if (count_constructions) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 185 | Label allocate; |
| 186 | // Decrease generous allocation count. |
| 187 | __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 188 | __ dec_b(FieldOperand(ecx, |
| 189 | SharedFunctionInfo::kConstructionCountOffset)); |
| 190 | __ j(not_zero, &allocate); |
| 191 | |
| 192 | __ push(eax); |
| 193 | __ push(edi); |
| 194 | |
| 195 | __ push(edi); // constructor |
| 196 | // The call will replace the stub, so the countdown is only done once. |
| 197 | __ CallRuntime(Runtime::kFinalizeInstanceSize, 1); |
| 198 | |
| 199 | __ pop(edi); |
| 200 | __ pop(eax); |
| 201 | |
| 202 | __ bind(&allocate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 203 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 204 | |
| 205 | // Now allocate the JSObject on the heap. |
| 206 | // edi: constructor |
| 207 | // eax: initial map |
| 208 | __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset)); |
| 209 | __ shl(edi, kPointerSizeLog2); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 210 | __ Allocate(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 211 | // Allocated the JSObject, now initialize the fields. |
| 212 | // eax: initial map |
| 213 | // ebx: JSObject |
| 214 | // edi: start of next object |
| 215 | __ mov(Operand(ebx, JSObject::kMapOffset), eax); |
| 216 | Factory* factory = masm->isolate()->factory(); |
| 217 | __ mov(ecx, factory->empty_fixed_array()); |
| 218 | __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx); |
| 219 | __ mov(Operand(ebx, JSObject::kElementsOffset), ecx); |
| 220 | // Set extra fields in the newly allocated object. |
| 221 | // eax: initial map |
| 222 | // ebx: JSObject |
| 223 | // edi: start of next object |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 224 | __ lea(ecx, Operand(ebx, JSObject::kHeaderSize)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 225 | __ mov(edx, factory->undefined_value()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 226 | if (count_constructions) { |
| 227 | __ movzx_b(esi, |
| 228 | FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset)); |
| 229 | __ lea(esi, |
| 230 | Operand(ebx, esi, times_pointer_size, JSObject::kHeaderSize)); |
| 231 | // esi: offset of first field after pre-allocated fields |
| 232 | if (FLAG_debug_code) { |
| 233 | __ cmp(esi, edi); |
| 234 | __ Assert(less_equal, |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 235 | kUnexpectedNumberOfPreAllocatedPropertyFields); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 236 | } |
| 237 | __ InitializeFieldsWithFiller(ecx, esi, edx); |
| 238 | __ mov(edx, factory->one_pointer_filler_map()); |
| 239 | } |
| 240 | __ InitializeFieldsWithFiller(ecx, edi, edx); |
| 241 | |
| 242 | // Add the object tag to make the JSObject real, so that we can continue |
| 243 | // and jump into the continuation code at any time from now on. Any |
| 244 | // failures need to undo the allocation, so that the heap is in a |
| 245 | // consistent state and verifiable. |
| 246 | // eax: initial map |
| 247 | // ebx: JSObject |
| 248 | // edi: start of next object |
| 249 | __ or_(ebx, Immediate(kHeapObjectTag)); |
| 250 | |
| 251 | // Check if a non-empty properties array is needed. |
| 252 | // Allocate and initialize a FixedArray if it is. |
| 253 | // eax: initial map |
| 254 | // ebx: JSObject |
| 255 | // edi: start of next object |
| 256 | // Calculate the total number of properties described by the map. |
| 257 | __ movzx_b(edx, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset)); |
| 258 | __ movzx_b(ecx, |
| 259 | FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset)); |
| 260 | __ add(edx, ecx); |
| 261 | // Calculate unused properties past the end of the in-object properties. |
| 262 | __ movzx_b(ecx, FieldOperand(eax, Map::kInObjectPropertiesOffset)); |
| 263 | __ sub(edx, ecx); |
| 264 | // Done if no extra properties are to be allocated. |
| 265 | __ j(zero, &allocated); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 266 | __ Assert(positive, kPropertyAllocationCountFailed); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 267 | |
| 268 | // Scale the number of elements by pointer size and add the header for |
| 269 | // FixedArrays to the start of the next object calculation from above. |
| 270 | // ebx: JSObject |
| 271 | // edi: start of next object (will be start of FixedArray) |
| 272 | // edx: number of elements in properties array |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 273 | __ Allocate(FixedArray::kHeaderSize, |
| 274 | times_pointer_size, |
| 275 | edx, |
| 276 | REGISTER_VALUE_IS_INT32, |
| 277 | edi, |
| 278 | ecx, |
| 279 | no_reg, |
| 280 | &undo_allocation, |
| 281 | RESULT_CONTAINS_TOP); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 282 | |
| 283 | // Initialize the FixedArray. |
| 284 | // ebx: JSObject |
| 285 | // edi: FixedArray |
| 286 | // edx: number of elements |
| 287 | // ecx: start of next object |
| 288 | __ mov(eax, factory->fixed_array_map()); |
| 289 | __ mov(Operand(edi, FixedArray::kMapOffset), eax); // setup the map |
| 290 | __ SmiTag(edx); |
| 291 | __ mov(Operand(edi, FixedArray::kLengthOffset), edx); // and length |
| 292 | |
| 293 | // Initialize the fields to undefined. |
| 294 | // ebx: JSObject |
| 295 | // edi: FixedArray |
| 296 | // ecx: start of next object |
| 297 | { Label loop, entry; |
| 298 | __ mov(edx, factory->undefined_value()); |
| 299 | __ lea(eax, Operand(edi, FixedArray::kHeaderSize)); |
| 300 | __ jmp(&entry); |
| 301 | __ bind(&loop); |
| 302 | __ mov(Operand(eax, 0), edx); |
| 303 | __ add(eax, Immediate(kPointerSize)); |
| 304 | __ bind(&entry); |
| 305 | __ cmp(eax, ecx); |
| 306 | __ j(below, &loop); |
| 307 | } |
| 308 | |
| 309 | // Store the initialized FixedArray into the properties field of |
| 310 | // the JSObject |
| 311 | // ebx: JSObject |
| 312 | // edi: FixedArray |
| 313 | __ or_(edi, Immediate(kHeapObjectTag)); // add the heap tag |
| 314 | __ mov(FieldOperand(ebx, JSObject::kPropertiesOffset), edi); |
| 315 | |
| 316 | |
| 317 | // Continue with JSObject being successfully allocated |
| 318 | // ebx: JSObject |
| 319 | __ jmp(&allocated); |
| 320 | |
| 321 | // Undo the setting of the new top so that the heap is verifiable. For |
| 322 | // example, the map's unused properties potentially do not match the |
| 323 | // allocated objects unused properties. |
| 324 | // ebx: JSObject (previous new top) |
| 325 | __ bind(&undo_allocation); |
| 326 | __ UndoAllocationInNewSpace(ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 327 | } |
| 328 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 329 | // Allocate the new receiver object using the runtime call. |
| 330 | __ bind(&rt_call); |
| 331 | // Must restore edi (constructor) before calling runtime. |
| 332 | __ mov(edi, Operand(esp, 0)); |
| 333 | // edi: function (constructor) |
| 334 | __ push(edi); |
| 335 | __ CallRuntime(Runtime::kNewObject, 1); |
| 336 | __ mov(ebx, eax); // store result in ebx |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 337 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 338 | // New object allocated. |
| 339 | // ebx: newly allocated object |
| 340 | __ bind(&allocated); |
| 341 | // Retrieve the function from the stack. |
| 342 | __ pop(edi); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 343 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 344 | // Retrieve smi-tagged arguments count from the stack. |
| 345 | __ mov(eax, Operand(esp, 0)); |
| 346 | __ SmiUntag(eax); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 348 | // Push the allocated receiver to the stack. We need two copies |
| 349 | // because we may have to return the original one and the calling |
| 350 | // conventions dictate that the called function pops the receiver. |
| 351 | __ push(ebx); |
| 352 | __ push(ebx); |
| 353 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 354 | // Set up pointer to last argument. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 355 | __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset)); |
| 356 | |
| 357 | // Copy arguments and receiver to the expression stack. |
| 358 | Label loop, entry; |
| 359 | __ mov(ecx, eax); |
| 360 | __ jmp(&entry); |
| 361 | __ bind(&loop); |
| 362 | __ push(Operand(ebx, ecx, times_4, 0)); |
| 363 | __ bind(&entry); |
| 364 | __ dec(ecx); |
| 365 | __ j(greater_equal, &loop); |
| 366 | |
| 367 | // Call the function. |
| 368 | if (is_api_function) { |
| 369 | __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
| 370 | Handle<Code> code = |
| 371 | masm->isolate()->builtins()->HandleApiCallConstruct(); |
| 372 | ParameterCount expected(0); |
| 373 | __ InvokeCode(code, expected, expected, RelocInfo::CODE_TARGET, |
| 374 | CALL_FUNCTION, NullCallWrapper(), CALL_AS_METHOD); |
| 375 | } else { |
| 376 | ParameterCount actual(eax); |
| 377 | __ InvokeFunction(edi, actual, CALL_FUNCTION, |
| 378 | NullCallWrapper(), CALL_AS_METHOD); |
| 379 | } |
| 380 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 381 | // Store offset of return address for deoptimizer. |
| 382 | if (!is_api_function && !count_constructions) { |
| 383 | masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset()); |
| 384 | } |
| 385 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 386 | // Restore context from the frame. |
| 387 | __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset)); |
| 388 | |
| 389 | // If the result is an object (in the ECMA sense), we should get rid |
| 390 | // of the receiver and use the result; see ECMA-262 section 13.2.2-7 |
| 391 | // on page 74. |
| 392 | Label use_receiver, exit; |
| 393 | |
| 394 | // If the result is a smi, it is *not* an object in the ECMA sense. |
| 395 | __ JumpIfSmi(eax, &use_receiver); |
| 396 | |
| 397 | // If the type of the result (stored in its map) is less than |
| 398 | // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense. |
| 399 | __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx); |
| 400 | __ j(above_equal, &exit); |
| 401 | |
| 402 | // Throw away the result of the constructor invocation and use the |
| 403 | // on-stack receiver as the result. |
| 404 | __ bind(&use_receiver); |
| 405 | __ mov(eax, Operand(esp, 0)); |
| 406 | |
| 407 | // Restore the arguments count and leave the construct frame. |
| 408 | __ bind(&exit); |
| 409 | __ mov(ebx, Operand(esp, kPointerSize)); // Get arguments count. |
| 410 | |
| 411 | // Leave construct frame. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 412 | } |
| 413 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 414 | // Remove caller arguments from the stack and return. |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 415 | STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 416 | __ pop(ecx); |
| 417 | __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver |
| 418 | __ push(ecx); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 419 | __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 420 | __ ret(0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 424 | void Builtins::Generate_JSConstructStubCountdown(MacroAssembler* masm) { |
| 425 | Generate_JSConstructStubHelper(masm, false, true); |
| 426 | } |
| 427 | |
| 428 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 429 | void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 430 | Generate_JSConstructStubHelper(masm, false, false); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | |
| 434 | void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) { |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 435 | Generate_JSConstructStubHelper(masm, true, false); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm, |
| 440 | bool is_construct) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 441 | ProfileEntryHookStub::MaybeCallEntryHook(masm); |
| 442 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 443 | // Clear the context before we push it when entering the internal frame. |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 444 | __ Set(esi, Immediate(0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 445 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 446 | { |
| 447 | FrameScope scope(masm, StackFrame::INTERNAL); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 448 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 449 | // Load the previous frame pointer (ebx) to access C arguments |
| 450 | __ mov(ebx, Operand(ebp, 0)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 451 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 452 | // Get the function from the frame and setup the context. |
| 453 | __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset)); |
| 454 | __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 455 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 456 | // Push the function and the receiver onto the stack. |
| 457 | __ push(ecx); |
| 458 | __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 459 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 460 | // Load the number of arguments and setup pointer to the arguments. |
| 461 | __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset)); |
| 462 | __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 463 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 464 | // Copy arguments to the stack in a loop. |
| 465 | Label loop, entry; |
| 466 | __ Set(ecx, Immediate(0)); |
| 467 | __ jmp(&entry); |
| 468 | __ bind(&loop); |
| 469 | __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv |
| 470 | __ push(Operand(edx, 0)); // dereference handle |
| 471 | __ inc(ecx); |
| 472 | __ bind(&entry); |
| 473 | __ cmp(ecx, eax); |
| 474 | __ j(not_equal, &loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 475 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 476 | // Get the function from the stack and call it. |
| 477 | // kPointerSize for the receiver. |
| 478 | __ mov(edi, Operand(esp, eax, times_4, kPointerSize)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 479 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 480 | // Invoke the code. |
| 481 | if (is_construct) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 482 | // No type feedback cell is available |
| 483 | Handle<Object> undefined_sentinel( |
| 484 | masm->isolate()->heap()->undefined_value(), masm->isolate()); |
| 485 | __ mov(ebx, Immediate(undefined_sentinel)); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 486 | CallConstructStub stub(NO_CALL_FUNCTION_FLAGS); |
| 487 | __ CallStub(&stub); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 488 | } else { |
| 489 | ParameterCount actual(eax); |
| 490 | __ InvokeFunction(edi, actual, CALL_FUNCTION, |
| 491 | NullCallWrapper(), CALL_AS_METHOD); |
| 492 | } |
| 493 | |
| 494 | // Exit the internal frame. Notice that this also removes the empty. |
| 495 | // context and the function left on the stack by the code |
| 496 | // invocation. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 497 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 498 | __ ret(kPointerSize); // Remove receiver. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | |
| 502 | void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) { |
| 503 | Generate_JSEntryTrampolineHelper(masm, false); |
| 504 | } |
| 505 | |
| 506 | |
| 507 | void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) { |
| 508 | Generate_JSEntryTrampolineHelper(masm, true); |
| 509 | } |
| 510 | |
| 511 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 512 | void Builtins::Generate_LazyCompile(MacroAssembler* masm) { |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 513 | CallRuntimePassFunction(masm, Runtime::kLazyCompile); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 514 | // Do a tail-call of the compiled function. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 515 | __ lea(eax, FieldOperand(eax, Code::kHeaderSize)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 516 | __ jmp(eax); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 520 | void Builtins::Generate_LazyRecompile(MacroAssembler* masm) { |
dslomov@chromium.org | 4a35c5a | 2013-09-13 07:28:52 +0000 | [diff] [blame] | 521 | CallRuntimePassFunction(masm, Runtime::kLazyRecompile); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 522 | // Do a tail-call of the compiled function. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 523 | __ lea(eax, FieldOperand(eax, Code::kHeaderSize)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 524 | __ jmp(eax); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 528 | static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) { |
| 529 | // For now, we are relying on the fact that make_code_young doesn't do any |
| 530 | // garbage collection which allows us to save/restore the registers without |
| 531 | // worrying about which of them contain pointers. We also don't build an |
| 532 | // internal frame to make the code faster, since we shouldn't have to do stack |
| 533 | // crawls in MakeCodeYoung. This seems a bit fragile. |
| 534 | |
| 535 | // Re-execute the code that was patched back to the young age when |
| 536 | // the stub returns. |
| 537 | __ sub(Operand(esp, 0), Immediate(5)); |
| 538 | __ pushad(); |
| 539 | __ mov(eax, Operand(esp, 8 * kPointerSize)); |
| 540 | { |
| 541 | FrameScope scope(masm, StackFrame::MANUAL); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 542 | __ PrepareCallCFunction(2, ebx); |
| 543 | __ mov(Operand(esp, 1 * kPointerSize), |
| 544 | Immediate(ExternalReference::isolate_address(masm->isolate()))); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 545 | __ mov(Operand(esp, 0), eax); |
| 546 | __ CallCFunction( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 547 | ExternalReference::get_make_code_young_function(masm->isolate()), 2); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 548 | } |
| 549 | __ popad(); |
| 550 | __ ret(0); |
| 551 | } |
| 552 | |
| 553 | #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \ |
| 554 | void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \ |
| 555 | MacroAssembler* masm) { \ |
| 556 | GenerateMakeCodeYoungAgainCommon(masm); \ |
| 557 | } \ |
| 558 | void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \ |
| 559 | MacroAssembler* masm) { \ |
| 560 | GenerateMakeCodeYoungAgainCommon(masm); \ |
| 561 | } |
| 562 | CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR) |
| 563 | #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR |
| 564 | |
| 565 | |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 566 | void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 567 | // Enter an internal frame. |
| 568 | { |
| 569 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 570 | |
| 571 | // Preserve registers across notification, this is important for compiled |
| 572 | // stubs that tail call the runtime on deopts passing their parameters in |
| 573 | // registers. |
| 574 | __ pushad(); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 575 | __ CallRuntime(Runtime::kNotifyStubFailure, 0); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 576 | __ popad(); |
| 577 | // Tear down internal frame. |
| 578 | } |
| 579 | |
| 580 | __ pop(MemOperand(esp, 0)); // Ignore state offset |
| 581 | __ ret(0); // Return to IC Miss stub, continuation still on stack. |
| 582 | } |
| 583 | |
| 584 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 585 | static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm, |
| 586 | Deoptimizer::BailoutType type) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 587 | { |
| 588 | FrameScope scope(masm, StackFrame::INTERNAL); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 589 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 590 | // Pass deoptimization type to the runtime system. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 591 | __ push(Immediate(Smi::FromInt(static_cast<int>(type)))); |
| 592 | __ CallRuntime(Runtime::kNotifyDeoptimized, 1); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 593 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 594 | // Tear down internal frame. |
| 595 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 596 | |
| 597 | // Get the full codegen state from the stack and untag it. |
| 598 | __ mov(ecx, Operand(esp, 1 * kPointerSize)); |
| 599 | __ SmiUntag(ecx); |
| 600 | |
| 601 | // Switch on the state. |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 602 | Label not_no_registers, not_tos_eax; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 603 | __ cmp(ecx, FullCodeGenerator::NO_REGISTERS); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 604 | __ j(not_equal, ¬_no_registers, Label::kNear); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 605 | __ ret(1 * kPointerSize); // Remove state. |
| 606 | |
| 607 | __ bind(¬_no_registers); |
| 608 | __ mov(eax, Operand(esp, 2 * kPointerSize)); |
| 609 | __ cmp(ecx, FullCodeGenerator::TOS_REG); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 610 | __ j(not_equal, ¬_tos_eax, Label::kNear); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 611 | __ ret(2 * kPointerSize); // Remove state, eax. |
| 612 | |
| 613 | __ bind(¬_tos_eax); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 614 | __ Abort(kNoCasesLeft); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | |
| 618 | void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) { |
| 619 | Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER); |
| 620 | } |
| 621 | |
| 622 | |
danno@chromium.org | aefd607 | 2013-05-14 14:11:47 +0000 | [diff] [blame] | 623 | void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) { |
| 624 | Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT); |
| 625 | } |
| 626 | |
| 627 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 628 | void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) { |
| 629 | Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY); |
| 630 | } |
| 631 | |
| 632 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 633 | void Builtins::Generate_FunctionCall(MacroAssembler* masm) { |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 634 | Factory* factory = masm->isolate()->factory(); |
| 635 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 636 | // 1. Make sure we have at least one argument. |
| 637 | { Label done; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 638 | __ test(eax, eax); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 639 | __ j(not_zero, &done); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 640 | __ pop(ebx); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 641 | __ push(Immediate(factory->undefined_value())); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 642 | __ push(ebx); |
| 643 | __ inc(eax); |
| 644 | __ bind(&done); |
| 645 | } |
| 646 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 647 | // 2. Get the function to call (passed as receiver) from the stack, check |
| 648 | // if it is a function. |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 649 | Label slow, non_function; |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 650 | // 1 ~ return address. |
| 651 | __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize)); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 652 | __ JumpIfSmi(edi, &non_function); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 653 | __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 654 | __ j(not_equal, &slow); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 655 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 656 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 657 | // 3a. Patch the first argument if necessary when calling a function. |
| 658 | Label shift_arguments; |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 659 | __ Set(edx, Immediate(0)); // indicate regular JS_FUNCTION |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 660 | { Label convert_to_object, use_global_receiver, patch_receiver; |
| 661 | // Change context eagerly in case we need the global receiver. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 662 | __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
| 663 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 664 | // Do not transform the receiver for strict mode functions. |
| 665 | __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 666 | __ test_b(FieldOperand(ebx, SharedFunctionInfo::kStrictModeByteOffset), |
| 667 | 1 << SharedFunctionInfo::kStrictModeBitWithinByte); |
| 668 | __ j(not_equal, &shift_arguments); |
| 669 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 670 | // Do not transform the receiver for natives (shared already in ebx). |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 671 | __ test_b(FieldOperand(ebx, SharedFunctionInfo::kNativeByteOffset), |
| 672 | 1 << SharedFunctionInfo::kNativeBitWithinByte); |
ricow@chromium.org | 6fe7a8e | 2011-05-13 07:57:29 +0000 | [diff] [blame] | 673 | __ j(not_equal, &shift_arguments); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 674 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 675 | // Compute the receiver in non-strict mode. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 676 | __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument. |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 677 | |
| 678 | // Call ToObject on the receiver if it is not an object, or use the |
| 679 | // global object if it is null or undefined. |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 680 | __ JumpIfSmi(ebx, &convert_to_object); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 681 | __ cmp(ebx, factory->null_value()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 682 | __ j(equal, &use_global_receiver); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 683 | __ cmp(ebx, factory->undefined_value()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 684 | __ j(equal, &use_global_receiver); |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 685 | STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE); |
| 686 | __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 687 | __ j(above_equal, &shift_arguments); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 688 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 689 | __ bind(&convert_to_object); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 690 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 691 | { // In order to preserve argument count. |
| 692 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 693 | __ SmiTag(eax); |
| 694 | __ push(eax); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 695 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 696 | __ push(ebx); |
| 697 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION); |
| 698 | __ mov(ebx, eax); |
| 699 | __ Set(edx, Immediate(0)); // restore |
| 700 | |
| 701 | __ pop(eax); |
| 702 | __ SmiUntag(eax); |
| 703 | } |
| 704 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 705 | // Restore the function to edi. |
| 706 | __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 707 | __ jmp(&patch_receiver); |
| 708 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 709 | // Use the global receiver object from the called function as the |
| 710 | // receiver. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 711 | __ bind(&use_global_receiver); |
| 712 | const int kGlobalIndex = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 713 | Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize; |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 714 | __ mov(ebx, FieldOperand(esi, kGlobalIndex)); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 715 | __ mov(ebx, FieldOperand(ebx, GlobalObject::kNativeContextOffset)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 716 | __ mov(ebx, FieldOperand(ebx, kGlobalIndex)); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 717 | __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset)); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 718 | |
| 719 | __ bind(&patch_receiver); |
| 720 | __ mov(Operand(esp, eax, times_4, 0), ebx); |
| 721 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 722 | __ jmp(&shift_arguments); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 723 | } |
| 724 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 725 | // 3b. Check for function proxy. |
| 726 | __ bind(&slow); |
| 727 | __ Set(edx, Immediate(1)); // indicate function proxy |
| 728 | __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE); |
| 729 | __ j(equal, &shift_arguments); |
| 730 | __ bind(&non_function); |
| 731 | __ Set(edx, Immediate(2)); // indicate non-function |
| 732 | |
| 733 | // 3c. Patch the first argument when calling a non-function. The |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 734 | // CALL_NON_FUNCTION builtin expects the non-function callee as |
| 735 | // receiver, so overwrite the first argument which will ultimately |
| 736 | // become the receiver. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 737 | __ mov(Operand(esp, eax, times_4, 0), edi); |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 738 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 739 | // 4. Shift arguments and return address one slot down on the stack |
| 740 | // (overwriting the original receiver). Adjust argument count to make |
| 741 | // the original first argument the new receiver. |
| 742 | __ bind(&shift_arguments); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 743 | { Label loop; |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 744 | __ mov(ecx, eax); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 745 | __ bind(&loop); |
| 746 | __ mov(ebx, Operand(esp, ecx, times_4, 0)); |
| 747 | __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx); |
| 748 | __ dec(ecx); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 749 | __ j(not_sign, &loop); // While non-negative (to copy return address). |
sgjesse@chromium.org | 846fb74 | 2009-12-18 08:56:33 +0000 | [diff] [blame] | 750 | __ pop(ebx); // Discard copy of return address. |
| 751 | __ dec(eax); // One fewer argument (first argument is new receiver). |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 752 | } |
| 753 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 754 | // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin, |
| 755 | // or a function proxy via CALL_FUNCTION_PROXY. |
| 756 | { Label function, non_proxy; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 757 | __ test(edx, edx); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 758 | __ j(zero, &function); |
lrn@chromium.org | 5d00b60 | 2011-01-05 09:51:43 +0000 | [diff] [blame] | 759 | __ Set(ebx, Immediate(0)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 760 | __ cmp(edx, Immediate(1)); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 761 | __ j(not_equal, &non_proxy); |
| 762 | |
| 763 | __ pop(edx); // return address |
| 764 | __ push(edi); // re-add proxy object as additional argument |
| 765 | __ push(edx); |
| 766 | __ inc(eax); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 767 | __ SetCallKind(ecx, CALL_AS_FUNCTION); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 768 | __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY); |
| 769 | __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(), |
| 770 | RelocInfo::CODE_TARGET); |
| 771 | |
| 772 | __ bind(&non_proxy); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 773 | __ SetCallKind(ecx, CALL_AS_METHOD); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 774 | __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 775 | __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(), |
| 776 | RelocInfo::CODE_TARGET); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 777 | __ bind(&function); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 778 | } |
| 779 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 780 | // 5b. Get the code to call from the function and check that the number of |
| 781 | // expected arguments matches what we're providing. If so, jump |
| 782 | // (tail-call) to the code in register edx without checking arguments. |
| 783 | __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 784 | __ mov(ebx, |
| 785 | FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset)); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 786 | __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset)); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 787 | __ SmiUntag(ebx); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 788 | __ SetCallKind(ecx, CALL_AS_METHOD); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 789 | __ cmp(eax, ebx); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 790 | __ j(not_equal, |
| 791 | masm->isolate()->builtins()->ArgumentsAdaptorTrampoline()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 792 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 793 | ParameterCount expected(0); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 794 | __ InvokeCode(edx, expected, expected, JUMP_FUNCTION, NullCallWrapper(), |
| 795 | CALL_AS_METHOD); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 799 | void Builtins::Generate_FunctionApply(MacroAssembler* masm) { |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 800 | static const int kArgumentsOffset = 2 * kPointerSize; |
| 801 | static const int kReceiverOffset = 3 * kPointerSize; |
| 802 | static const int kFunctionOffset = 4 * kPointerSize; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 803 | { |
| 804 | FrameScope frame_scope(masm, StackFrame::INTERNAL); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 805 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 806 | __ push(Operand(ebp, kFunctionOffset)); // push this |
| 807 | __ push(Operand(ebp, kArgumentsOffset)); // push arguments |
| 808 | __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 809 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 810 | // Check the stack for overflow. We are not trying to catch |
| 811 | // interruptions (e.g. debug break and preemption) here, so the "real stack |
| 812 | // limit" is checked. |
| 813 | Label okay; |
| 814 | ExternalReference real_stack_limit = |
| 815 | ExternalReference::address_of_real_stack_limit(masm->isolate()); |
| 816 | __ mov(edi, Operand::StaticVariable(real_stack_limit)); |
| 817 | // Make ecx the space we have left. The stack might already be overflowed |
| 818 | // here which will cause ecx to become negative. |
| 819 | __ mov(ecx, esp); |
| 820 | __ sub(ecx, edi); |
| 821 | // Make edx the space we need for the array when it is unrolled onto the |
| 822 | // stack. |
| 823 | __ mov(edx, eax); |
| 824 | __ shl(edx, kPointerSizeLog2 - kSmiTagSize); |
| 825 | // Check if the arguments will overflow the stack. |
| 826 | __ cmp(ecx, edx); |
| 827 | __ j(greater, &okay); // Signed comparison. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 828 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 829 | // Out of stack space. |
| 830 | __ push(Operand(ebp, 4 * kPointerSize)); // push this |
| 831 | __ push(eax); |
| 832 | __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION); |
| 833 | __ bind(&okay); |
| 834 | // End of stack check. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 835 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 836 | // Push current index and limit. |
| 837 | const int kLimitOffset = |
| 838 | StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize; |
| 839 | const int kIndexOffset = kLimitOffset - 1 * kPointerSize; |
| 840 | __ push(eax); // limit |
| 841 | __ push(Immediate(0)); // index |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 842 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 843 | // Get the receiver. |
| 844 | __ mov(ebx, Operand(ebp, kReceiverOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 845 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 846 | // Check that the function is a JS function (otherwise it must be a proxy). |
| 847 | Label push_receiver; |
| 848 | __ mov(edi, Operand(ebp, kFunctionOffset)); |
| 849 | __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx); |
| 850 | __ j(not_equal, &push_receiver); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 851 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 852 | // Change context eagerly to get the right global object if necessary. |
| 853 | __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset)); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 854 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 855 | // Compute the receiver. |
| 856 | // Do not transform the receiver for strict mode functions. |
| 857 | Label call_to_object, use_global_receiver; |
| 858 | __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset)); |
| 859 | __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset), |
| 860 | 1 << SharedFunctionInfo::kStrictModeBitWithinByte); |
| 861 | __ j(not_equal, &push_receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 862 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 863 | Factory* factory = masm->isolate()->factory(); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 864 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 865 | // Do not transform the receiver for natives (shared already in ecx). |
| 866 | __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset), |
| 867 | 1 << SharedFunctionInfo::kNativeBitWithinByte); |
| 868 | __ j(not_equal, &push_receiver); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 869 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 870 | // Compute the receiver in non-strict mode. |
| 871 | // Call ToObject on the receiver if it is not an object, or use the |
| 872 | // global object if it is null or undefined. |
| 873 | __ JumpIfSmi(ebx, &call_to_object); |
| 874 | __ cmp(ebx, factory->null_value()); |
| 875 | __ j(equal, &use_global_receiver); |
| 876 | __ cmp(ebx, factory->undefined_value()); |
| 877 | __ j(equal, &use_global_receiver); |
| 878 | STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE); |
| 879 | __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx); |
| 880 | __ j(above_equal, &push_receiver); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 881 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 882 | __ bind(&call_to_object); |
| 883 | __ push(ebx); |
| 884 | __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION); |
| 885 | __ mov(ebx, eax); |
| 886 | __ jmp(&push_receiver); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 887 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 888 | // Use the current global receiver object as the receiver. |
| 889 | __ bind(&use_global_receiver); |
| 890 | const int kGlobalOffset = |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 891 | Context::kHeaderSize + Context::GLOBAL_OBJECT_INDEX * kPointerSize; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 892 | __ mov(ebx, FieldOperand(esi, kGlobalOffset)); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 893 | __ mov(ebx, FieldOperand(ebx, GlobalObject::kNativeContextOffset)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 894 | __ mov(ebx, FieldOperand(ebx, kGlobalOffset)); |
| 895 | __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 896 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 897 | // Push the receiver. |
| 898 | __ bind(&push_receiver); |
| 899 | __ push(ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 900 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 901 | // Copy all arguments from the array to the stack. |
| 902 | Label entry, loop; |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 903 | __ mov(ecx, Operand(ebp, kIndexOffset)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 904 | __ jmp(&entry); |
| 905 | __ bind(&loop); |
| 906 | __ mov(edx, Operand(ebp, kArgumentsOffset)); // load arguments |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 907 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 908 | // Use inline caching to speed up access to arguments. |
| 909 | Handle<Code> ic = masm->isolate()->builtins()->KeyedLoadIC_Initialize(); |
| 910 | __ call(ic, RelocInfo::CODE_TARGET); |
| 911 | // It is important that we do not have a test instruction after the |
| 912 | // call. A test instruction after the call is used to indicate that |
| 913 | // we have generated an inline version of the keyed load. In this |
| 914 | // case, we know that we are not generating a test instruction next. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 915 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 916 | // Push the nth argument. |
| 917 | __ push(eax); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 918 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 919 | // Update the index on the stack and in register eax. |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 920 | __ mov(ecx, Operand(ebp, kIndexOffset)); |
| 921 | __ add(ecx, Immediate(1 << kSmiTagSize)); |
| 922 | __ mov(Operand(ebp, kIndexOffset), ecx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 923 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 924 | __ bind(&entry); |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 925 | __ cmp(ecx, Operand(ebp, kLimitOffset)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 926 | __ j(not_equal, &loop); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 927 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 928 | // Invoke the function. |
| 929 | Label call_proxy; |
danno@chromium.org | 1044a4d | 2012-04-30 12:34:39 +0000 | [diff] [blame] | 930 | __ mov(eax, ecx); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 931 | ParameterCount actual(eax); |
| 932 | __ SmiUntag(eax); |
| 933 | __ mov(edi, Operand(ebp, kFunctionOffset)); |
| 934 | __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx); |
| 935 | __ j(not_equal, &call_proxy); |
| 936 | __ InvokeFunction(edi, actual, CALL_FUNCTION, |
| 937 | NullCallWrapper(), CALL_AS_METHOD); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 938 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 939 | frame_scope.GenerateLeaveFrame(); |
| 940 | __ ret(3 * kPointerSize); // remove this, receiver, and arguments |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 941 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 942 | // Invoke the function proxy. |
| 943 | __ bind(&call_proxy); |
| 944 | __ push(edi); // add function proxy as last argument |
| 945 | __ inc(eax); |
| 946 | __ Set(ebx, Immediate(0)); |
| 947 | __ SetCallKind(ecx, CALL_AS_METHOD); |
| 948 | __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY); |
| 949 | __ call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(), |
| 950 | RelocInfo::CODE_TARGET); |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 951 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 952 | // Leave internal frame. |
| 953 | } |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 954 | __ ret(3 * kPointerSize); // remove this, receiver, and arguments |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 958 | void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) { |
| 959 | // ----------- S t a t e ------------- |
| 960 | // -- eax : argc |
| 961 | // -- esp[0] : return address |
| 962 | // -- esp[4] : last argument |
| 963 | // ----------------------------------- |
| 964 | Label generic_array_code; |
| 965 | |
| 966 | // Get the InternalArray function. |
| 967 | __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, edi); |
| 968 | |
| 969 | if (FLAG_debug_code) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 970 | // Initial map for the builtin InternalArray function should be a map. |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 971 | __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset)); |
| 972 | // Will both indicate a NULL and a Smi. |
| 973 | __ test(ebx, Immediate(kSmiTagMask)); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 974 | __ Assert(not_zero, kUnexpectedInitialMapForInternalArrayFunction); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 975 | __ CmpObjectType(ebx, MAP_TYPE, ecx); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 976 | __ Assert(equal, kUnexpectedInitialMapForInternalArrayFunction); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | // Run the native code for the InternalArray function called as a normal |
| 980 | // function. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 981 | // tail call a stub |
| 982 | InternalArrayConstructorStub stub(masm->isolate()); |
| 983 | __ TailCallStub(&stub); |
svenpanne@chromium.org | 3c93e77 | 2012-01-02 09:26:59 +0000 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 987 | void Builtins::Generate_ArrayCode(MacroAssembler* masm) { |
| 988 | // ----------- S t a t e ------------- |
| 989 | // -- eax : argc |
| 990 | // -- esp[0] : return address |
| 991 | // -- esp[4] : last argument |
| 992 | // ----------------------------------- |
vegorov@chromium.org | dff694e | 2010-05-17 09:10:26 +0000 | [diff] [blame] | 993 | Label generic_array_code; |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 994 | |
| 995 | // Get the Array function. |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 996 | __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, edi); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 997 | |
| 998 | if (FLAG_debug_code) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 999 | // Initial map for the builtin Array function should be a map. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1000 | __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset)); |
| 1001 | // Will both indicate a NULL and a Smi. |
| 1002 | __ test(ebx, Immediate(kSmiTagMask)); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1003 | __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1004 | __ CmpObjectType(ebx, MAP_TYPE, ecx); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1005 | __ Assert(equal, kUnexpectedInitialMapForArrayFunction); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | // Run the native code for the Array function called as a normal function. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1009 | // tail call a stub |
| 1010 | Handle<Object> undefined_sentinel( |
| 1011 | masm->isolate()->heap()->undefined_value(), |
| 1012 | masm->isolate()); |
| 1013 | __ mov(ebx, Immediate(undefined_sentinel)); |
| 1014 | ArrayConstructorStub stub(masm->isolate()); |
| 1015 | __ TailCallStub(&stub); |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1019 | void Builtins::Generate_StringConstructCode(MacroAssembler* masm) { |
| 1020 | // ----------- S t a t e ------------- |
| 1021 | // -- eax : number of arguments |
| 1022 | // -- edi : constructor function |
| 1023 | // -- esp[0] : return address |
| 1024 | // -- esp[(argc - n) * 4] : arg[n] (zero-based) |
| 1025 | // -- esp[(argc + 1) * 4] : receiver |
| 1026 | // ----------------------------------- |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1027 | Counters* counters = masm->isolate()->counters(); |
| 1028 | __ IncrementCounter(counters->string_ctor_calls(), 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1029 | |
| 1030 | if (FLAG_debug_code) { |
| 1031 | __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, ecx); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1032 | __ cmp(edi, ecx); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1033 | __ Assert(equal, kUnexpectedStringFunction); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | // Load the first argument into eax and get rid of the rest |
| 1037 | // (including the receiver). |
| 1038 | Label no_arguments; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1039 | __ test(eax, eax); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1040 | __ j(zero, &no_arguments); |
| 1041 | __ mov(ebx, Operand(esp, eax, times_pointer_size, 0)); |
| 1042 | __ pop(ecx); |
| 1043 | __ lea(esp, Operand(esp, eax, times_pointer_size, kPointerSize)); |
| 1044 | __ push(ecx); |
| 1045 | __ mov(eax, ebx); |
| 1046 | |
| 1047 | // Lookup the argument in the number to string cache. |
| 1048 | Label not_cached, argument_is_string; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1049 | __ LookupNumberStringCache(eax, // Input. |
| 1050 | ebx, // Result. |
| 1051 | ecx, // Scratch 1. |
| 1052 | edx, // Scratch 2. |
| 1053 | ¬_cached); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1054 | __ IncrementCounter(counters->string_ctor_cached_number(), 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1055 | __ bind(&argument_is_string); |
| 1056 | // ----------- S t a t e ------------- |
| 1057 | // -- ebx : argument converted to string |
| 1058 | // -- edi : constructor function |
| 1059 | // -- esp[0] : return address |
| 1060 | // ----------------------------------- |
| 1061 | |
| 1062 | // Allocate a JSValue and put the tagged pointer into eax. |
| 1063 | Label gc_required; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1064 | __ Allocate(JSValue::kSize, |
| 1065 | eax, // Result. |
| 1066 | ecx, // New allocation top (we ignore it). |
| 1067 | no_reg, |
| 1068 | &gc_required, |
| 1069 | TAG_OBJECT); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Set the map. |
| 1072 | __ LoadGlobalFunctionInitialMap(edi, ecx); |
| 1073 | if (FLAG_debug_code) { |
| 1074 | __ cmpb(FieldOperand(ecx, Map::kInstanceSizeOffset), |
| 1075 | JSValue::kSize >> kPointerSizeLog2); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1076 | __ Assert(equal, kUnexpectedStringWrapperInstanceSize); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1077 | __ cmpb(FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset), 0); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 1078 | __ Assert(equal, kUnexpectedUnusedPropertiesOfStringWrapper); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1079 | } |
| 1080 | __ mov(FieldOperand(eax, HeapObject::kMapOffset), ecx); |
| 1081 | |
| 1082 | // Set properties and elements. |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1083 | Factory* factory = masm->isolate()->factory(); |
| 1084 | __ Set(ecx, Immediate(factory->empty_fixed_array())); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1085 | __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx); |
| 1086 | __ mov(FieldOperand(eax, JSObject::kElementsOffset), ecx); |
| 1087 | |
| 1088 | // Set the value. |
| 1089 | __ mov(FieldOperand(eax, JSValue::kValueOffset), ebx); |
| 1090 | |
| 1091 | // Ensure the object is fully initialized. |
| 1092 | STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize); |
| 1093 | |
| 1094 | // We're done. Return. |
| 1095 | __ ret(0); |
| 1096 | |
| 1097 | // The argument was not found in the number to string cache. Check |
| 1098 | // if it's a string already before calling the conversion builtin. |
| 1099 | Label convert_argument; |
| 1100 | __ bind(¬_cached); |
| 1101 | STATIC_ASSERT(kSmiTag == 0); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 1102 | __ JumpIfSmi(eax, &convert_argument); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1103 | Condition is_string = masm->IsObjectStringType(eax, ebx, ecx); |
| 1104 | __ j(NegateCondition(is_string), &convert_argument); |
| 1105 | __ mov(ebx, eax); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1106 | __ IncrementCounter(counters->string_ctor_string_value(), 1); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1107 | __ jmp(&argument_is_string); |
| 1108 | |
| 1109 | // Invoke the conversion builtin and put the result into ebx. |
| 1110 | __ bind(&convert_argument); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1111 | __ IncrementCounter(counters->string_ctor_conversions(), 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1112 | { |
| 1113 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 1114 | __ push(edi); // Preserve the function. |
| 1115 | __ push(eax); |
| 1116 | __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION); |
| 1117 | __ pop(edi); |
| 1118 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1119 | __ mov(ebx, eax); |
| 1120 | __ jmp(&argument_is_string); |
| 1121 | |
| 1122 | // Load the empty string into ebx, remove the receiver from the |
| 1123 | // stack, and jump back to the case where the argument is a string. |
| 1124 | __ bind(&no_arguments); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1125 | __ Set(ebx, Immediate(factory->empty_string())); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1126 | __ pop(ecx); |
| 1127 | __ lea(esp, Operand(esp, kPointerSize)); |
| 1128 | __ push(ecx); |
| 1129 | __ jmp(&argument_is_string); |
| 1130 | |
| 1131 | // At this point the argument is already a string. Call runtime to |
| 1132 | // create a string wrapper. |
| 1133 | __ bind(&gc_required); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1134 | __ IncrementCounter(counters->string_ctor_gc_required(), 1); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1135 | { |
| 1136 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 1137 | __ push(ebx); |
| 1138 | __ CallRuntime(Runtime::kNewStringWrapper, 1); |
| 1139 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 1140 | __ ret(0); |
| 1141 | } |
| 1142 | |
| 1143 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1144 | static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) { |
| 1145 | __ push(ebp); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1146 | __ mov(ebp, esp); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1147 | |
| 1148 | // Store the arguments adaptor context sentinel. |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 1149 | __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR))); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Push the function on the stack. |
| 1152 | __ push(edi); |
| 1153 | |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1154 | // Preserve the number of arguments on the stack. Must preserve eax, |
| 1155 | // ebx and ecx because these registers are used when copying the |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1156 | // arguments and the receiver. |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1157 | STATIC_ASSERT(kSmiTagSize == 1); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1158 | __ lea(edi, Operand(eax, eax, times_1, kSmiTag)); |
| 1159 | __ push(edi); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1163 | static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1164 | // Retrieve the number of arguments from the stack. |
| 1165 | __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset)); |
| 1166 | |
| 1167 | // Leave the frame. |
| 1168 | __ leave(); |
| 1169 | |
| 1170 | // Remove caller arguments from the stack. |
yangguo@chromium.org | 80c42ed | 2011-08-31 09:03:56 +0000 | [diff] [blame] | 1171 | STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1172 | __ pop(ecx); |
| 1173 | __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver |
| 1174 | __ push(ecx); |
| 1175 | } |
| 1176 | |
| 1177 | |
| 1178 | void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) { |
| 1179 | // ----------- S t a t e ------------- |
| 1180 | // -- eax : actual number of arguments |
| 1181 | // -- ebx : expected number of arguments |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1182 | // -- ecx : call kind information |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1183 | // -- edx : code entry to call |
| 1184 | // ----------------------------------- |
| 1185 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1186 | Label invoke, dont_adapt_arguments; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1187 | __ IncrementCounter(masm->isolate()->counters()->arguments_adaptors(), 1); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1188 | |
| 1189 | Label enough, too_few; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1190 | __ cmp(eax, ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1191 | __ j(less, &too_few); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1192 | __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel); |
| 1193 | __ j(equal, &dont_adapt_arguments); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1194 | |
| 1195 | { // Enough parameters: Actual >= expected. |
| 1196 | __ bind(&enough); |
| 1197 | EnterArgumentsAdaptorFrame(masm); |
| 1198 | |
| 1199 | // Copy receiver and all expected arguments. |
| 1200 | const int offset = StandardFrameConstants::kCallerSPOffset; |
| 1201 | __ lea(eax, Operand(ebp, eax, times_4, offset)); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1202 | __ mov(edi, -1); // account for receiver |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1203 | |
| 1204 | Label copy; |
| 1205 | __ bind(©); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1206 | __ inc(edi); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1207 | __ push(Operand(eax, 0)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1208 | __ sub(eax, Immediate(kPointerSize)); |
| 1209 | __ cmp(edi, ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1210 | __ j(less, ©); |
| 1211 | __ jmp(&invoke); |
| 1212 | } |
| 1213 | |
| 1214 | { // Too few parameters: Actual < expected. |
| 1215 | __ bind(&too_few); |
| 1216 | EnterArgumentsAdaptorFrame(masm); |
| 1217 | |
| 1218 | // Copy receiver and all actual arguments. |
| 1219 | const int offset = StandardFrameConstants::kCallerSPOffset; |
| 1220 | __ lea(edi, Operand(ebp, eax, times_4, offset)); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1221 | // ebx = expected - actual. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1222 | __ sub(ebx, eax); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1223 | // eax = -actual - 1 |
| 1224 | __ neg(eax); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1225 | __ sub(eax, Immediate(1)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1226 | |
| 1227 | Label copy; |
| 1228 | __ bind(©); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1229 | __ inc(eax); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1230 | __ push(Operand(edi, 0)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1231 | __ sub(edi, Immediate(kPointerSize)); |
| 1232 | __ test(eax, eax); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1233 | __ j(not_zero, ©); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1234 | |
| 1235 | // Fill remaining expected arguments with undefined values. |
| 1236 | Label fill; |
| 1237 | __ bind(&fill); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1238 | __ inc(eax); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1239 | __ push(Immediate(masm->isolate()->factory()->undefined_value())); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1240 | __ cmp(eax, ebx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1241 | __ j(less, &fill); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1244 | // Call the entry point. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1245 | __ bind(&invoke); |
danno@chromium.org | 40cb878 | 2011-05-25 07:58:50 +0000 | [diff] [blame] | 1246 | // Restore function pointer. |
| 1247 | __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1248 | __ call(edx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1249 | |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 1250 | // Store offset of return address for deoptimizer. |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 1251 | masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset()); |
ulan@chromium.org | 967e270 | 2012-02-28 09:49:15 +0000 | [diff] [blame] | 1252 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 1253 | // Leave frame and return. |
| 1254 | LeaveArgumentsAdaptorFrame(masm); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1255 | __ ret(0); |
| 1256 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1257 | // ------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1258 | // Dont adapt arguments. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1259 | // ------------------------------------------- |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 1260 | __ bind(&dont_adapt_arguments); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1261 | __ jmp(edx); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1265 | void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) { |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 1266 | // Lookup the function in the JavaScript frame. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1267 | __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset)); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1268 | { |
| 1269 | FrameScope scope(masm, StackFrame::INTERNAL); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 1270 | // Lookup and calculate pc offset. |
| 1271 | __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerPCOffset)); |
| 1272 | __ mov(ebx, FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset)); |
| 1273 | __ sub(edx, Immediate(Code::kHeaderSize - kHeapObjectTag)); |
| 1274 | __ sub(edx, FieldOperand(ebx, SharedFunctionInfo::kCodeOffset)); |
| 1275 | __ SmiTag(edx); |
| 1276 | |
| 1277 | // Pass both function and pc offset as arguments. |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1278 | __ push(eax); |
dslomov@chromium.org | e97852d | 2013-09-12 09:02:59 +0000 | [diff] [blame] | 1279 | __ push(edx); |
| 1280 | __ CallRuntime(Runtime::kCompileForOnStackReplacement, 2); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1281 | } |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1282 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1283 | Label skip; |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 1284 | // If the code object is null, just return to the unoptimized code. |
| 1285 | __ cmp(eax, Immediate(0)); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 1286 | __ j(not_equal, &skip, Label::kNear); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1287 | __ ret(0); |
| 1288 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1289 | __ bind(&skip); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1290 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 1291 | // Load deoptimization data from the code object. |
| 1292 | __ mov(ebx, Operand(eax, Code::kDeoptimizationDataOffset - kHeapObjectTag)); |
| 1293 | |
| 1294 | // Load the OSR entrypoint offset from the deoptimization data. |
| 1295 | __ mov(ebx, Operand(ebx, FixedArray::OffsetOfElementAt( |
| 1296 | DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag)); |
| 1297 | __ SmiUntag(ebx); |
| 1298 | |
| 1299 | // Compute the target address = code_obj + header_size + osr_offset |
| 1300 | __ lea(eax, Operand(eax, ebx, times_1, Code::kHeaderSize - kHeapObjectTag)); |
| 1301 | |
| 1302 | // Overwrite the return address on the stack. |
| 1303 | __ mov(Operand(esp, 0), eax); |
| 1304 | |
| 1305 | // And "return" to the OSR entry point of the function. |
| 1306 | __ ret(0); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | |
machenbach@chromium.org | 8e36b5b | 2013-09-26 07:36:30 +0000 | [diff] [blame] | 1310 | void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) { |
| 1311 | // We check the stack limit as indicator that recompilation might be done. |
| 1312 | Label ok; |
| 1313 | ExternalReference stack_limit = |
| 1314 | ExternalReference::address_of_stack_limit(masm->isolate()); |
| 1315 | __ cmp(esp, Operand::StaticVariable(stack_limit)); |
| 1316 | __ j(above_equal, &ok, Label::kNear); |
| 1317 | { |
| 1318 | FrameScope scope(masm, StackFrame::INTERNAL); |
| 1319 | __ CallRuntime(Runtime::kStackGuard, 0); |
| 1320 | } |
| 1321 | __ jmp(masm->isolate()->builtins()->OnStackReplacement(), |
| 1322 | RelocInfo::CODE_TARGET); |
| 1323 | |
| 1324 | __ bind(&ok); |
| 1325 | __ ret(0); |
| 1326 | } |
| 1327 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 1328 | #undef __ |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 1329 | } |
| 1330 | } // namespace v8::internal |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 1331 | |
| 1332 | #endif // V8_TARGET_ARCH_IA32 |