blob: 5c7ba8e1bac8367bcced34a439d600723772c436 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#include "codegen-inl.h"
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000031
32namespace v8 { namespace internal {
33
34
ager@chromium.org65dad4b2009-04-23 08:48:43 +000035#define __ ACCESS_MASM(masm)
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000038void Builtins::Generate_Adaptor(MacroAssembler* masm, CFunctionId id) {
39 // TODO(1238487): Don't pass the function in a static variable.
40 ExternalReference passed = ExternalReference::builtin_passed_function();
41 __ mov(Operand::StaticVariable(passed), edi);
42
43 // The actual argument count has already been loaded into register
44 // eax, but JumpToBuiltin expects eax to contain the number of
45 // arguments including the receiver.
46 __ inc(eax);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000047 __ JumpToBuiltin(ExternalReference(id));
48}
49
50
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +000052 // ----------- S t a t e -------------
53 // -- eax: number of arguments
54 // -- edi: constructor function
55 // -----------------------------------
56
ager@chromium.org5ec48922009-05-05 07:25:34 +000057 Label non_function_call;
58 // Check that function is not a smi.
59 __ test(edi, Immediate(kSmiTagMask));
60 __ j(zero, &non_function_call);
61 // Check that function is a JSFunction.
62 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
63 __ j(not_equal, &non_function_call);
64
ager@chromium.org7c537e22008-10-16 08:43:32 +000065 // Enter a construct frame.
66 __ EnterConstructFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000067
68 // Store a smi-tagged arguments count on the stack.
69 __ shl(eax, kSmiTagSize);
70 __ push(eax);
71
72 // Push the function to invoke on the stack.
73 __ push(edi);
74
75 // Try to allocate the object without transitioning into C code. If any of the
76 // preconditions is not met, the code bails out to the runtime call.
77 Label rt_call, allocated;
78 if (FLAG_inline_new) {
79 Label undo_allocation;
ager@chromium.org65dad4b2009-04-23 08:48:43 +000080#ifdef ENABLE_DEBUGGER_SUPPORT
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000081 ExternalReference debug_step_in_fp =
82 ExternalReference::debug_step_in_fp_address();
83 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
84 __ j(not_equal, &rt_call);
ager@chromium.org65dad4b2009-04-23 08:48:43 +000085#endif
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000086
87 // Verified that the constructor is a JSFunction.
88 // Load the initial map and verify that it is in fact a map.
89 // edi: constructor
90 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
91 // Will both indicate a NULL and a Smi
92 __ test(eax, Immediate(kSmiTagMask));
93 __ j(zero, &rt_call);
94 // edi: constructor
95 // eax: initial map (if proven valid below)
kasperl@chromium.org7be3c992009-03-12 07:19:55 +000096 __ CmpObjectType(eax, MAP_TYPE, ebx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000097 __ j(not_equal, &rt_call);
98
99 // Check that the constructor is not constructing a JSFunction (see comments
100 // in Runtime_NewObject in runtime.cc). In which case the initial map's
101 // instance type would be JS_FUNCTION_TYPE.
102 // edi: constructor
103 // eax: initial map
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000104 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 __ j(equal, &rt_call);
106
107 // Now allocate the JSObject on the heap.
108 // edi: constructor
109 // eax: initial map
110 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000111 __ shl(edi, kPointerSizeLog2);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112 // Make sure that the maximum heap object size will never cause us
113 // problem here, because it is always greater than the maximum
114 // instance size that can be represented in a byte.
115 ASSERT(Heap::MaxHeapObjectSize() >= (1 << kBitsPerByte));
116 ExternalReference new_space_allocation_top =
117 ExternalReference::new_space_allocation_top_address();
118 __ mov(ebx, Operand::StaticVariable(new_space_allocation_top));
119 __ add(edi, Operand(ebx)); // Calculate new top
120 ExternalReference new_space_allocation_limit =
121 ExternalReference::new_space_allocation_limit_address();
122 __ cmp(edi, Operand::StaticVariable(new_space_allocation_limit));
123 __ j(greater_equal, &rt_call);
124 // Allocated the JSObject, now initialize the fields.
125 // eax: initial map
126 // ebx: JSObject
127 // edi: start of next object
128 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000129 __ mov(ecx, Factory::empty_fixed_array());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000130 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
131 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
132 // Set extra fields in the newly allocated object.
133 // eax: initial map
134 // ebx: JSObject
135 // edi: start of next object
136 { Label loop, entry;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000137 __ mov(edx, Factory::undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000138 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
139 __ jmp(&entry);
140 __ bind(&loop);
141 __ mov(Operand(ecx, 0), edx);
142 __ add(Operand(ecx), Immediate(kPointerSize));
143 __ bind(&entry);
144 __ cmp(ecx, Operand(edi));
145 __ j(less, &loop);
146 }
147
148 // Mostly done with the JSObject. Add the heap tag and store the new top, so
149 // that we can continue and jump into the continuation code at any time from
150 // now on. Any failures need to undo the setting of the new top, so that the
151 // heap is in a consistent state and verifiable.
152 // eax: initial map
153 // ebx: JSObject
154 // edi: start of next object
155 __ or_(Operand(ebx), Immediate(kHeapObjectTag));
156 __ mov(Operand::StaticVariable(new_space_allocation_top), edi);
157
158 // Check if a properties array should be setup and allocate one if needed.
159 // Otherwise initialize the properties to the empty_fixed_array as well.
160 // eax: initial map
161 // ebx: JSObject
162 // edi: start of next object
163 __ movzx_b(edx, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000164 __ movzx_b(ecx, FieldOperand(eax, Map::kInObjectPropertiesOffset));
165 // Calculate unused properties past the end of the in-object properties.
166 __ sub(edx, Operand(ecx));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000167 __ test(edx, Operand(edx));
ager@chromium.org7c537e22008-10-16 08:43:32 +0000168 // Done if no extra properties are to be allocated.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000169 __ j(zero, &allocated);
170
171 // Scale the number of elements by pointer size and add the header for
172 // FixedArrays to the start of the next object calculation from above.
173 // eax: initial map
174 // ebx: JSObject
175 // edi: start of next object (will be start of FixedArray)
176 // edx: number of elements in properties array
177 ASSERT(Heap::MaxHeapObjectSize() >
178 (FixedArray::kHeaderSize + 255*kPointerSize));
179 __ lea(ecx, Operand(edi, edx, times_4, FixedArray::kHeaderSize));
180 __ cmp(ecx, Operand::StaticVariable(new_space_allocation_limit));
181 __ j(greater_equal, &undo_allocation);
182 __ mov(Operand::StaticVariable(new_space_allocation_top), ecx);
183
184 // Initialize the FixedArray.
185 // ebx: JSObject
186 // edi: FixedArray
187 // edx: number of elements
188 // ecx: start of next object
189 __ mov(eax, Factory::fixed_array_map());
190 __ mov(Operand(edi, JSObject::kMapOffset), eax); // setup the map
191 __ mov(Operand(edi, Array::kLengthOffset), edx); // and length
192
193 // Initialize the fields to undefined.
194 // ebx: JSObject
195 // edi: FixedArray
196 // ecx: start of next object
197 { Label loop, entry;
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000198 __ mov(edx, Factory::undefined_value());
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000199 __ lea(eax, Operand(edi, FixedArray::kHeaderSize));
200 __ jmp(&entry);
201 __ bind(&loop);
202 __ mov(Operand(eax, 0), edx);
203 __ add(Operand(eax), Immediate(kPointerSize));
204 __ bind(&entry);
205 __ cmp(eax, Operand(ecx));
206 __ j(less, &loop);
207 }
208
209 // Store the initialized FixedArray into the properties field of
210 // the JSObject
211 // ebx: JSObject
212 // edi: FixedArray
213 __ or_(Operand(edi), Immediate(kHeapObjectTag)); // add the heap tag
214 __ mov(FieldOperand(ebx, JSObject::kPropertiesOffset), edi);
215
216
217 // Continue with JSObject being successfully allocated
218 // ebx: JSObject
219 __ jmp(&allocated);
220
221 // Undo the setting of the new top so that the heap is verifiable. For
222 // example, the map's unused properties potentially do not match the
223 // allocated objects unused properties.
224 // ebx: JSObject (previous new top)
225 __ bind(&undo_allocation);
226 __ xor_(Operand(ebx), Immediate(kHeapObjectTag)); // clear the heap tag
227 __ mov(Operand::StaticVariable(new_space_allocation_top), ebx);
228 }
229
230 // Allocate the new receiver object using the runtime call.
231 // edi: function (constructor)
232 __ bind(&rt_call);
233 // Must restore edi (constructor) before calling runtime.
234 __ mov(edi, Operand(esp, 0));
235 __ push(edi);
236 __ CallRuntime(Runtime::kNewObject, 1);
237 __ mov(ebx, Operand(eax)); // store result in ebx
238
239 // New object allocated.
240 // ebx: newly allocated object
241 __ bind(&allocated);
242 // Retrieve the function from the stack.
243 __ pop(edi);
244
245 // Retrieve smi-tagged arguments count from the stack.
246 __ mov(eax, Operand(esp, 0));
247 __ shr(eax, kSmiTagSize);
248
249 // Push the allocated receiver to the stack. We need two copies
250 // because we may have to return the original one and the calling
251 // conventions dictate that the called function pops the receiver.
252 __ push(ebx);
253 __ push(ebx);
254
255 // Setup pointer to last argument.
256 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
257
258 // Copy arguments and receiver to the expression stack.
259 Label loop, entry;
260 __ mov(ecx, Operand(eax));
261 __ jmp(&entry);
262 __ bind(&loop);
263 __ push(Operand(ebx, ecx, times_4, 0));
264 __ bind(&entry);
265 __ dec(ecx);
266 __ j(greater_equal, &loop);
267
268 // Call the function.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000269 ParameterCount actual(eax);
270 __ InvokeFunction(edi, actual, CALL_FUNCTION);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000271
272 // Restore context from the frame.
273 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
274
275 // If the result is an object (in the ECMA sense), we should get rid
276 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
277 // on page 74.
278 Label use_receiver, exit;
279
280 // If the result is a smi, it is *not* an object in the ECMA sense.
281 __ test(eax, Immediate(kSmiTagMask));
282 __ j(zero, &use_receiver, not_taken);
283
284 // If the type of the result (stored in its map) is less than
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000285 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000286 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
287 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
ager@chromium.orgc27e4e72008-09-04 13:52:27 +0000288 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000289 __ j(greater_equal, &exit, not_taken);
290
291 // Throw away the result of the constructor invocation and use the
292 // on-stack receiver as the result.
293 __ bind(&use_receiver);
294 __ mov(eax, Operand(esp, 0));
295
ager@chromium.org7c537e22008-10-16 08:43:32 +0000296 // Restore the arguments count and leave the construct frame.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000297 __ bind(&exit);
298 __ mov(ebx, Operand(esp, kPointerSize)); // get arguments count
ager@chromium.org7c537e22008-10-16 08:43:32 +0000299 __ LeaveConstructFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000300
301 // Remove caller arguments from the stack and return.
302 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
303 __ pop(ecx);
304 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
305 __ push(ecx);
306 __ ret(0);
ager@chromium.org5ec48922009-05-05 07:25:34 +0000307
308 // edi: called object
309 // eax: number of arguments
310 __ bind(&non_function_call);
311
312 // Set expected number of arguments to zero (not changing eax).
313 __ Set(ebx, Immediate(0));
314 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
315 __ jmp(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
316 RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000317}
318
319
320static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
321 bool is_construct) {
322 // Clear the context before we push it when entering the JS frame.
323 __ xor_(esi, Operand(esi)); // clear esi
324
325 // Enter an internal frame.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000326 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000327
328 // Load the previous frame pointer (ebx) to access C arguments
329 __ mov(ebx, Operand(ebp, 0));
330
331 // Get the function from the frame and setup the context.
332 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
333 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
334
335 // Push the function and the receiver onto the stack.
336 __ push(ecx);
337 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
338
339 // Load the number of arguments and setup pointer to the arguments.
340 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
341 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
342
343 // Copy arguments to the stack in a loop.
344 Label loop, entry;
345 __ xor_(ecx, Operand(ecx)); // clear ecx
346 __ jmp(&entry);
347 __ bind(&loop);
348 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
349 __ push(Operand(edx, 0)); // dereference handle
350 __ inc(Operand(ecx));
351 __ bind(&entry);
352 __ cmp(ecx, Operand(eax));
353 __ j(not_equal, &loop);
354
355 // Get the function from the stack and call it.
356 __ mov(edi, Operand(esp, eax, times_4, +1 * kPointerSize)); // +1 ~ receiver
357
358 // Invoke the code.
359 if (is_construct) {
360 __ call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.org236ad962008-09-25 09:45:57 +0000361 RelocInfo::CODE_TARGET);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000362 } else {
363 ParameterCount actual(eax);
364 __ InvokeFunction(edi, actual, CALL_FUNCTION);
365 }
366
367 // Exit the JS frame. Notice that this also removes the empty
368 // context and the function left on the stack by the code
369 // invocation.
ager@chromium.org236ad962008-09-25 09:45:57 +0000370 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000371 __ ret(1 * kPointerSize); // remove receiver
372}
373
374
375void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
376 Generate_JSEntryTrampolineHelper(masm, false);
377}
378
379
380void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
381 Generate_JSEntryTrampolineHelper(masm, true);
382}
383
384
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000385void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
386 // 1. Make sure we have at least one argument.
387 { Label done;
388 __ test(eax, Operand(eax));
389 __ j(not_zero, &done, taken);
390 __ pop(ebx);
391 __ push(Immediate(Factory::undefined_value()));
392 __ push(ebx);
393 __ inc(eax);
394 __ bind(&done);
395 }
396
397 // 2. Get the function to call from the stack.
398 { Label done, non_function, function;
399 // +1 ~ return address.
400 __ mov(edi, Operand(esp, eax, times_4, +1 * kPointerSize));
401 __ test(edi, Immediate(kSmiTagMask));
402 __ j(zero, &non_function, not_taken);
kasperl@chromium.org7be3c992009-03-12 07:19:55 +0000403 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000404 __ j(equal, &function, taken);
405
406 // Non-function called: Clear the function to force exception.
407 __ bind(&non_function);
408 __ xor_(edi, Operand(edi));
409 __ jmp(&done);
410
411 // Function called: Change context eagerly to get the right global object.
412 __ bind(&function);
413 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
414
415 __ bind(&done);
416 }
417
418 // 3. Make sure first argument is an object; convert if necessary.
419 { Label call_to_object, use_global_receiver, patch_receiver, done;
420 __ mov(ebx, Operand(esp, eax, times_4, 0));
421
422 __ test(ebx, Immediate(kSmiTagMask));
423 __ j(zero, &call_to_object);
424
425 __ cmp(ebx, Factory::null_value());
426 __ j(equal, &use_global_receiver);
427 __ cmp(ebx, Factory::undefined_value());
428 __ j(equal, &use_global_receiver);
429
430 __ mov(ecx, FieldOperand(ebx, HeapObject::kMapOffset));
431 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
432 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
433 __ j(less, &call_to_object);
434 __ cmp(ecx, LAST_JS_OBJECT_TYPE);
435 __ j(less_equal, &done);
436
437 __ bind(&call_to_object);
438 __ EnterInternalFrame(); // preserves eax, ebx, edi
439
440 // Store the arguments count on the stack (smi tagged).
441 ASSERT(kSmiTag == 0);
442 __ shl(eax, kSmiTagSize);
443 __ push(eax);
444
445 __ push(edi); // save edi across the call
446 __ push(ebx);
447 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000448 __ mov(ebx, eax);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000449 __ pop(edi); // restore edi after the call
450
451 // Get the arguments count and untag it.
452 __ pop(eax);
453 __ shr(eax, kSmiTagSize);
454
ager@chromium.org236ad962008-09-25 09:45:57 +0000455 __ LeaveInternalFrame();
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000456 __ jmp(&patch_receiver);
457
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000458 // Use the global receiver object from the called function as the receiver.
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000459 __ bind(&use_global_receiver);
460 const int kGlobalIndex =
461 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
462 __ mov(ebx, FieldOperand(esi, kGlobalIndex));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000463 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000464
465 __ bind(&patch_receiver);
466 __ mov(Operand(esp, eax, times_4, 0), ebx);
467
468 __ bind(&done);
469 }
470
471 // 4. Shift stuff one slot down the stack.
472 { Label loop;
473 __ lea(ecx, Operand(eax, +1)); // +1 ~ copy receiver too
474 __ bind(&loop);
475 __ mov(ebx, Operand(esp, ecx, times_4, 0));
476 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
477 __ dec(ecx);
478 __ j(not_zero, &loop);
479 }
480
481 // 5. Remove TOS (copy of last arguments), but keep return address.
482 __ pop(ebx);
483 __ pop(ecx);
484 __ push(ebx);
485 __ dec(eax);
486
487 // 6. Check that function really was a function and get the code to
488 // call from the function and check that the number of expected
489 // arguments matches what we're providing.
490 { Label invoke;
491 __ test(edi, Operand(edi));
492 __ j(not_zero, &invoke, taken);
493 __ xor_(ebx, Operand(ebx));
494 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
ager@chromium.org236ad962008-09-25 09:45:57 +0000495 __ jmp(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
496 RelocInfo::CODE_TARGET);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000497
498 __ bind(&invoke);
499 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
500 __ mov(ebx,
501 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
502 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
503 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
504 __ cmp(eax, Operand(ebx));
505 __ j(not_equal, Handle<Code>(builtin(ArgumentsAdaptorTrampoline)));
506 }
507
508 // 7. Jump (tail-call) to the code in register edx without checking arguments.
509 ParameterCount expected(0);
510 __ InvokeCode(Operand(edx), expected, expected, JUMP_FUNCTION);
511}
512
513
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000514void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000515 __ EnterInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000516
517 __ push(Operand(ebp, 4 * kPointerSize)); // push this
518 __ push(Operand(ebp, 2 * kPointerSize)); // push arguments
519 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
520
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000521 if (FLAG_check_stack) {
522 // We need to catch preemptions right here, otherwise an unlucky preemption
523 // could show up as a failed apply.
524 ExternalReference stack_guard_limit =
525 ExternalReference::address_of_stack_guard_limit();
526 Label retry_preemption;
527 Label no_preemption;
528 __ bind(&retry_preemption);
529 __ mov(edi, Operand::StaticVariable(stack_guard_limit));
530 __ cmp(esp, Operand(edi));
531 __ j(above, &no_preemption, taken);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000532
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000533 // Preemption!
534 // Because builtins always remove the receiver from the stack, we
535 // have to fake one to avoid underflowing the stack.
536 __ push(eax);
537 __ push(Immediate(Smi::FromInt(0)));
538
539 // Do call to runtime routine.
540 __ CallRuntime(Runtime::kStackGuard, 1);
541 __ pop(eax);
542 __ jmp(&retry_preemption);
543
544 __ bind(&no_preemption);
545
546 Label okay;
547 // Make ecx the space we have left.
548 __ mov(ecx, Operand(esp));
549 __ sub(ecx, Operand(edi));
550 // Make edx the space we need for the array when it is unrolled onto the
551 // stack.
552 __ mov(edx, Operand(eax));
553 __ shl(edx, kPointerSizeLog2 - kSmiTagSize);
554 __ cmp(ecx, Operand(edx));
555 __ j(greater, &okay, taken);
556
557 // Too bad: Out of stack space.
558 __ push(Operand(ebp, 4 * kPointerSize)); // push this
559 __ push(eax);
560 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
561 __ bind(&okay);
562 }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000563
564 // Push current index and limit.
565 const int kLimitOffset =
566 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
567 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
568 __ push(eax); // limit
569 __ push(Immediate(0)); // index
570
571 // Change context eagerly to get the right global object if
572 // necessary.
573 __ mov(edi, Operand(ebp, 4 * kPointerSize));
574 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
575
576 // Compute the receiver.
577 Label call_to_object, use_global_receiver, push_receiver;
578 __ mov(ebx, Operand(ebp, 3 * kPointerSize));
579 __ test(ebx, Immediate(kSmiTagMask));
580 __ j(zero, &call_to_object);
581 __ cmp(ebx, Factory::null_value());
582 __ j(equal, &use_global_receiver);
583 __ cmp(ebx, Factory::undefined_value());
584 __ j(equal, &use_global_receiver);
585
586 // If given receiver is already a JavaScript object then there's no
587 // reason for converting it.
588 __ mov(ecx, FieldOperand(ebx, HeapObject::kMapOffset));
589 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
590 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
591 __ j(less, &call_to_object);
592 __ cmp(ecx, LAST_JS_OBJECT_TYPE);
593 __ j(less_equal, &push_receiver);
594
595 // Convert the receiver to an object.
596 __ bind(&call_to_object);
597 __ push(ebx);
598 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
599 __ mov(ebx, Operand(eax));
600 __ jmp(&push_receiver);
601
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000602 // Use the current global receiver object as the receiver.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000603 __ bind(&use_global_receiver);
604 const int kGlobalOffset =
605 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
606 __ mov(ebx, FieldOperand(esi, kGlobalOffset));
kasperl@chromium.org5a8ca6c2008-10-23 13:57:19 +0000607 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000608
609 // Push the receiver.
610 __ bind(&push_receiver);
611 __ push(ebx);
612
613 // Copy all arguments from the array to the stack.
614 Label entry, loop;
615 __ mov(eax, Operand(ebp, kIndexOffset));
616 __ jmp(&entry);
617 __ bind(&loop);
618 __ mov(ecx, Operand(ebp, 2 * kPointerSize)); // load arguments
619 __ push(ecx);
620 __ push(eax);
621
622 // Use inline caching to speed up access to arguments.
623 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
ager@chromium.org236ad962008-09-25 09:45:57 +0000624 __ call(ic, RelocInfo::CODE_TARGET);
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000625 // It is important that we do not have a test instruction after the
626 // call. A test instruction after the call is used to indicate that
627 // we have generated an inline version of the keyed load. In this
628 // case, we know that we are not generating a test instruction next.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000629
630 // Remove IC arguments from the stack and push the nth argument.
631 __ add(Operand(esp), Immediate(2 * kPointerSize));
632 __ push(eax);
633
634 // Update the index on the stack and in register eax.
635 __ mov(eax, Operand(ebp, kIndexOffset));
636 __ add(Operand(eax), Immediate(1 << kSmiTagSize));
637 __ mov(Operand(ebp, kIndexOffset), eax);
638
639 __ bind(&entry);
640 __ cmp(eax, Operand(ebp, kLimitOffset));
641 __ j(not_equal, &loop);
642
643 // Invoke the function.
644 ParameterCount actual(eax);
645 __ shr(eax, kSmiTagSize);
646 __ mov(edi, Operand(ebp, 4 * kPointerSize));
647 __ InvokeFunction(edi, actual, CALL_FUNCTION);
648
ager@chromium.org236ad962008-09-25 09:45:57 +0000649 __ LeaveInternalFrame();
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000650 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
651}
652
653
654static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
655 __ push(ebp);
656 __ mov(ebp, Operand(esp));
657
658 // Store the arguments adaptor context sentinel.
659 __ push(Immediate(ArgumentsAdaptorFrame::SENTINEL));
660
661 // Push the function on the stack.
662 __ push(edi);
663
664 // Preserve the number of arguments on the stack. Must preserve both
665 // eax and ebx because these registers are used when copying the
666 // arguments and the receiver.
667 ASSERT(kSmiTagSize == 1);
668 __ lea(ecx, Operand(eax, eax, times_1, kSmiTag));
ager@chromium.org3bf7b912008-11-17 09:09:45 +0000669 __ push(ecx);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000670}
671
672
ager@chromium.org7c537e22008-10-16 08:43:32 +0000673static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000674 // Retrieve the number of arguments from the stack.
675 __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
676
677 // Leave the frame.
678 __ leave();
679
680 // Remove caller arguments from the stack.
681 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
682 __ pop(ecx);
683 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
684 __ push(ecx);
685}
686
687
688void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
689 // ----------- S t a t e -------------
690 // -- eax : actual number of arguments
691 // -- ebx : expected number of arguments
692 // -- edx : code entry to call
693 // -----------------------------------
694
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000695 Label invoke, dont_adapt_arguments;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000696 __ IncrementCounter(&Counters::arguments_adaptors, 1);
697
698 Label enough, too_few;
699 __ cmp(eax, Operand(ebx));
700 __ j(less, &too_few);
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000701 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
702 __ j(equal, &dont_adapt_arguments);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000703
704 { // Enough parameters: Actual >= expected.
705 __ bind(&enough);
706 EnterArgumentsAdaptorFrame(masm);
707
708 // Copy receiver and all expected arguments.
709 const int offset = StandardFrameConstants::kCallerSPOffset;
710 __ lea(eax, Operand(ebp, eax, times_4, offset));
711 __ mov(ecx, -1); // account for receiver
712
713 Label copy;
714 __ bind(&copy);
715 __ inc(ecx);
716 __ push(Operand(eax, 0));
717 __ sub(Operand(eax), Immediate(kPointerSize));
718 __ cmp(ecx, Operand(ebx));
719 __ j(less, &copy);
720 __ jmp(&invoke);
721 }
722
723 { // Too few parameters: Actual < expected.
724 __ bind(&too_few);
725 EnterArgumentsAdaptorFrame(masm);
726
727 // Copy receiver and all actual arguments.
728 const int offset = StandardFrameConstants::kCallerSPOffset;
729 __ lea(edi, Operand(ebp, eax, times_4, offset));
730 __ mov(ecx, -1); // account for receiver
731
732 Label copy;
733 __ bind(&copy);
734 __ inc(ecx);
735 __ push(Operand(edi, 0));
736 __ sub(Operand(edi), Immediate(kPointerSize));
737 __ cmp(ecx, Operand(eax));
738 __ j(less, &copy);
739
740 // Fill remaining expected arguments with undefined values.
741 Label fill;
742 __ bind(&fill);
743 __ inc(ecx);
744 __ push(Immediate(Factory::undefined_value()));
745 __ cmp(ecx, Operand(ebx));
746 __ j(less, &fill);
747
748 // Restore function pointer.
749 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
750 }
751
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000752 // Call the entry point.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000753 __ bind(&invoke);
754 __ call(Operand(edx));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000755
ager@chromium.org7c537e22008-10-16 08:43:32 +0000756 // Leave frame and return.
757 LeaveArgumentsAdaptorFrame(masm);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000758 __ ret(0);
759
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000760 // -------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000761 // Dont adapt arguments.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000762 // -------------------------------------------
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000763 __ bind(&dont_adapt_arguments);
764 __ jmp(Operand(edx));
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000765}
766
767
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000768#undef __
769
770} } // namespace v8::internal