blob: 608625817a4a1c5931c3bd951f9d44f53fc029e3 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
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
Leon Clarkef7060e22010-06-03 12:02:55 +010030#if defined(V8_TARGET_ARCH_IA32)
31
Steve Blocka7e24c12009-10-30 11:49:00 +000032#include "codegen-inl.h"
33
34namespace v8 {
35namespace internal {
36
37
38#define __ ACCESS_MASM(masm)
39
40
Leon Clarkee46be812010-01-19 14:06:41 +000041void Builtins::Generate_Adaptor(MacroAssembler* masm,
42 CFunctionId id,
43 BuiltinExtraArguments extra_args) {
44 // ----------- S t a t e -------------
45 // -- eax : number of arguments excluding receiver
46 // -- edi : called function (only guaranteed when
47 // extra_args requires it)
48 // -- esi : context
49 // -- esp[0] : return address
50 // -- esp[4] : last argument
51 // -- ...
52 // -- esp[4 * argc] : first argument (argc == eax)
53 // -- esp[4 * (argc +1)] : receiver
54 // -----------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Leon Clarkee46be812010-01-19 14:06:41 +000056 // Insert extra arguments.
57 int num_extra_args = 0;
58 if (extra_args == NEEDS_CALLED_FUNCTION) {
59 num_extra_args = 1;
60 Register scratch = ebx;
61 __ pop(scratch); // Save return address.
62 __ push(edi);
63 __ push(scratch); // Restore return address.
64 } else {
65 ASSERT(extra_args == NO_EXTRA_ARGUMENTS);
66 }
67
Steve Block6ded16b2010-05-10 14:33:55 +010068 // JumpToExternalReference expects eax to contain the number of arguments
Leon Clarkee46be812010-01-19 14:06:41 +000069 // including the receiver and the extra arguments.
70 __ add(Operand(eax), Immediate(num_extra_args + 1));
Steve Block6ded16b2010-05-10 14:33:55 +010071 __ JumpToExternalReference(ExternalReference(id));
Steve Blocka7e24c12009-10-30 11:49:00 +000072}
73
74
75void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
76 // ----------- S t a t e -------------
77 // -- eax: number of arguments
78 // -- edi: constructor function
79 // -----------------------------------
80
81 Label non_function_call;
82 // Check that function is not a smi.
83 __ test(edi, Immediate(kSmiTagMask));
84 __ j(zero, &non_function_call);
85 // Check that function is a JSFunction.
86 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
87 __ j(not_equal, &non_function_call);
88
89 // Jump to the function-specific construct stub.
90 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
91 __ mov(ebx, FieldOperand(ebx, SharedFunctionInfo::kConstructStubOffset));
92 __ lea(ebx, FieldOperand(ebx, Code::kHeaderSize));
93 __ jmp(Operand(ebx));
94
95 // edi: called object
96 // eax: number of arguments
97 __ bind(&non_function_call);
Andrei Popescu402d9372010-02-26 13:31:12 +000098 // CALL_NON_FUNCTION expects the non-function constructor as receiver
99 // (instead of the original receiver from the call site). The receiver is
100 // stack element argc+1.
101 __ mov(Operand(esp, eax, times_4, kPointerSize), edi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000102 // Set expected number of arguments to zero (not changing eax).
103 __ Set(ebx, Immediate(0));
104 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
105 __ jmp(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
106 RelocInfo::CODE_TARGET);
107}
108
109
Leon Clarkee46be812010-01-19 14:06:41 +0000110static void Generate_JSConstructStubHelper(MacroAssembler* masm,
111 bool is_api_function) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000112 // Enter a construct frame.
113 __ EnterConstructFrame();
114
115 // Store a smi-tagged arguments count on the stack.
Leon Clarkee46be812010-01-19 14:06:41 +0000116 __ SmiTag(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 __ push(eax);
118
119 // Push the function to invoke on the stack.
120 __ push(edi);
121
122 // Try to allocate the object without transitioning into C code. If any of the
123 // preconditions is not met, the code bails out to the runtime call.
124 Label rt_call, allocated;
125 if (FLAG_inline_new) {
126 Label undo_allocation;
127#ifdef ENABLE_DEBUGGER_SUPPORT
128 ExternalReference debug_step_in_fp =
129 ExternalReference::debug_step_in_fp_address();
130 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
131 __ j(not_equal, &rt_call);
132#endif
133
134 // Verified that the constructor is a JSFunction.
135 // Load the initial map and verify that it is in fact a map.
136 // edi: constructor
137 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
138 // Will both indicate a NULL and a Smi
139 __ test(eax, Immediate(kSmiTagMask));
140 __ j(zero, &rt_call);
141 // edi: constructor
142 // eax: initial map (if proven valid below)
143 __ CmpObjectType(eax, MAP_TYPE, ebx);
144 __ j(not_equal, &rt_call);
145
146 // Check that the constructor is not constructing a JSFunction (see comments
147 // in Runtime_NewObject in runtime.cc). In which case the initial map's
148 // instance type would be JS_FUNCTION_TYPE.
149 // edi: constructor
150 // eax: initial map
151 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
152 __ j(equal, &rt_call);
153
154 // Now allocate the JSObject on the heap.
155 // edi: constructor
156 // eax: initial map
157 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
158 __ shl(edi, kPointerSizeLog2);
159 __ AllocateInNewSpace(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS);
160 // Allocated the JSObject, now initialize the fields.
161 // eax: initial map
162 // ebx: JSObject
163 // edi: start of next object
164 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
165 __ mov(ecx, Factory::empty_fixed_array());
166 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
167 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
168 // Set extra fields in the newly allocated object.
169 // eax: initial map
170 // ebx: JSObject
171 // edi: start of next object
172 { Label loop, entry;
173 __ mov(edx, Factory::undefined_value());
174 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
175 __ jmp(&entry);
176 __ bind(&loop);
177 __ mov(Operand(ecx, 0), edx);
178 __ add(Operand(ecx), Immediate(kPointerSize));
179 __ bind(&entry);
180 __ cmp(ecx, Operand(edi));
181 __ j(less, &loop);
182 }
183
184 // Add the object tag to make the JSObject real, so that we can continue and
185 // jump into the continuation code at any time from now on. Any failures
186 // need to undo the allocation, so that the heap is in a consistent state
187 // and verifiable.
188 // eax: initial map
189 // ebx: JSObject
190 // edi: start of next object
191 __ or_(Operand(ebx), Immediate(kHeapObjectTag));
192
193 // Check if a non-empty properties array is needed.
194 // Allocate and initialize a FixedArray if it is.
195 // eax: initial map
196 // ebx: JSObject
197 // edi: start of next object
198 // Calculate the total number of properties described by the map.
199 __ movzx_b(edx, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
200 __ movzx_b(ecx, FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset));
201 __ add(edx, Operand(ecx));
202 // Calculate unused properties past the end of the in-object properties.
203 __ movzx_b(ecx, FieldOperand(eax, Map::kInObjectPropertiesOffset));
204 __ sub(edx, Operand(ecx));
205 // Done if no extra properties are to be allocated.
206 __ j(zero, &allocated);
207 __ Assert(positive, "Property allocation count failed.");
208
209 // Scale the number of elements by pointer size and add the header for
210 // FixedArrays to the start of the next object calculation from above.
211 // ebx: JSObject
212 // edi: start of next object (will be start of FixedArray)
213 // edx: number of elements in properties array
214 __ AllocateInNewSpace(FixedArray::kHeaderSize,
215 times_pointer_size,
216 edx,
217 edi,
218 ecx,
219 no_reg,
220 &undo_allocation,
221 RESULT_CONTAINS_TOP);
222
223 // Initialize the FixedArray.
224 // ebx: JSObject
225 // edi: FixedArray
226 // edx: number of elements
227 // ecx: start of next object
228 __ mov(eax, Factory::fixed_array_map());
229 __ mov(Operand(edi, JSObject::kMapOffset), eax); // setup the map
230 __ mov(Operand(edi, Array::kLengthOffset), edx); // and length
231
232 // Initialize the fields to undefined.
233 // ebx: JSObject
234 // edi: FixedArray
235 // ecx: start of next object
236 { Label loop, entry;
237 __ mov(edx, Factory::undefined_value());
238 __ lea(eax, Operand(edi, FixedArray::kHeaderSize));
239 __ jmp(&entry);
240 __ bind(&loop);
241 __ mov(Operand(eax, 0), edx);
242 __ add(Operand(eax), Immediate(kPointerSize));
243 __ bind(&entry);
244 __ cmp(eax, Operand(ecx));
245 __ j(below, &loop);
246 }
247
248 // Store the initialized FixedArray into the properties field of
249 // the JSObject
250 // ebx: JSObject
251 // edi: FixedArray
252 __ or_(Operand(edi), Immediate(kHeapObjectTag)); // add the heap tag
253 __ mov(FieldOperand(ebx, JSObject::kPropertiesOffset), edi);
254
255
256 // Continue with JSObject being successfully allocated
257 // ebx: JSObject
258 __ jmp(&allocated);
259
260 // Undo the setting of the new top so that the heap is verifiable. For
261 // example, the map's unused properties potentially do not match the
262 // allocated objects unused properties.
263 // ebx: JSObject (previous new top)
264 __ bind(&undo_allocation);
265 __ UndoAllocationInNewSpace(ebx);
266 }
267
268 // Allocate the new receiver object using the runtime call.
269 __ bind(&rt_call);
270 // Must restore edi (constructor) before calling runtime.
271 __ mov(edi, Operand(esp, 0));
272 // edi: function (constructor)
273 __ push(edi);
274 __ CallRuntime(Runtime::kNewObject, 1);
275 __ mov(ebx, Operand(eax)); // store result in ebx
276
277 // New object allocated.
278 // ebx: newly allocated object
279 __ bind(&allocated);
280 // Retrieve the function from the stack.
281 __ pop(edi);
282
283 // Retrieve smi-tagged arguments count from the stack.
284 __ mov(eax, Operand(esp, 0));
Leon Clarkee46be812010-01-19 14:06:41 +0000285 __ SmiUntag(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000286
287 // Push the allocated receiver to the stack. We need two copies
288 // because we may have to return the original one and the calling
289 // conventions dictate that the called function pops the receiver.
290 __ push(ebx);
291 __ push(ebx);
292
293 // Setup pointer to last argument.
294 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
295
296 // Copy arguments and receiver to the expression stack.
297 Label loop, entry;
298 __ mov(ecx, Operand(eax));
299 __ jmp(&entry);
300 __ bind(&loop);
301 __ push(Operand(ebx, ecx, times_4, 0));
302 __ bind(&entry);
303 __ dec(ecx);
304 __ j(greater_equal, &loop);
305
306 // Call the function.
Leon Clarkee46be812010-01-19 14:06:41 +0000307 if (is_api_function) {
308 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
309 Handle<Code> code = Handle<Code>(
310 Builtins::builtin(Builtins::HandleApiCallConstruct));
311 ParameterCount expected(0);
312 __ InvokeCode(code, expected, expected,
313 RelocInfo::CODE_TARGET, CALL_FUNCTION);
314 } else {
315 ParameterCount actual(eax);
316 __ InvokeFunction(edi, actual, CALL_FUNCTION);
317 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000318
319 // Restore context from the frame.
320 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
321
322 // If the result is an object (in the ECMA sense), we should get rid
323 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
324 // on page 74.
325 Label use_receiver, exit;
326
327 // If the result is a smi, it is *not* an object in the ECMA sense.
328 __ test(eax, Immediate(kSmiTagMask));
329 __ j(zero, &use_receiver, not_taken);
330
331 // If the type of the result (stored in its map) is less than
332 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
333 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
334 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
335 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
336 __ j(greater_equal, &exit, not_taken);
337
338 // Throw away the result of the constructor invocation and use the
339 // on-stack receiver as the result.
340 __ bind(&use_receiver);
341 __ mov(eax, Operand(esp, 0));
342
343 // Restore the arguments count and leave the construct frame.
344 __ bind(&exit);
345 __ mov(ebx, Operand(esp, kPointerSize)); // get arguments count
346 __ LeaveConstructFrame();
347
348 // Remove caller arguments from the stack and return.
349 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
350 __ pop(ecx);
351 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
352 __ push(ecx);
353 __ IncrementCounter(&Counters::constructed_objects, 1);
354 __ ret(0);
355}
356
357
Leon Clarkee46be812010-01-19 14:06:41 +0000358void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
359 Generate_JSConstructStubHelper(masm, false);
360}
361
362
363void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
364 Generate_JSConstructStubHelper(masm, true);
365}
366
367
Steve Blocka7e24c12009-10-30 11:49:00 +0000368static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
369 bool is_construct) {
370 // Clear the context before we push it when entering the JS frame.
371 __ xor_(esi, Operand(esi)); // clear esi
372
373 // Enter an internal frame.
374 __ EnterInternalFrame();
375
376 // Load the previous frame pointer (ebx) to access C arguments
377 __ mov(ebx, Operand(ebp, 0));
378
379 // Get the function from the frame and setup the context.
380 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
381 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
382
383 // Push the function and the receiver onto the stack.
384 __ push(ecx);
385 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
386
387 // Load the number of arguments and setup pointer to the arguments.
388 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
389 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
390
391 // Copy arguments to the stack in a loop.
392 Label loop, entry;
393 __ xor_(ecx, Operand(ecx)); // clear ecx
394 __ jmp(&entry);
395 __ bind(&loop);
396 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
397 __ push(Operand(edx, 0)); // dereference handle
398 __ inc(Operand(ecx));
399 __ bind(&entry);
400 __ cmp(ecx, Operand(eax));
401 __ j(not_equal, &loop);
402
403 // Get the function from the stack and call it.
404 __ mov(edi, Operand(esp, eax, times_4, +1 * kPointerSize)); // +1 ~ receiver
405
406 // Invoke the code.
407 if (is_construct) {
408 __ call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
409 RelocInfo::CODE_TARGET);
410 } else {
411 ParameterCount actual(eax);
412 __ InvokeFunction(edi, actual, CALL_FUNCTION);
413 }
414
415 // Exit the JS frame. Notice that this also removes the empty
416 // context and the function left on the stack by the code
417 // invocation.
418 __ LeaveInternalFrame();
419 __ ret(1 * kPointerSize); // remove receiver
420}
421
422
423void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
424 Generate_JSEntryTrampolineHelper(masm, false);
425}
426
427
428void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
429 Generate_JSEntryTrampolineHelper(masm, true);
430}
431
432
433void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
434 // 1. Make sure we have at least one argument.
435 { Label done;
436 __ test(eax, Operand(eax));
437 __ j(not_zero, &done, taken);
438 __ pop(ebx);
439 __ push(Immediate(Factory::undefined_value()));
440 __ push(ebx);
441 __ inc(eax);
442 __ bind(&done);
443 }
444
Andrei Popescu402d9372010-02-26 13:31:12 +0000445 // 2. Get the function to call (passed as receiver) from the stack, check
446 // if it is a function.
447 Label non_function;
448 // 1 ~ return address.
449 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
450 __ test(edi, Immediate(kSmiTagMask));
451 __ j(zero, &non_function, not_taken);
452 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
453 __ j(not_equal, &non_function, not_taken);
Steve Blocka7e24c12009-10-30 11:49:00 +0000454
Steve Blocka7e24c12009-10-30 11:49:00 +0000455
Andrei Popescu402d9372010-02-26 13:31:12 +0000456 // 3a. Patch the first argument if necessary when calling a function.
457 Label shift_arguments;
458 { Label convert_to_object, use_global_receiver, patch_receiver;
459 // Change context eagerly in case we need the global receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +0000460 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
461
Andrei Popescu402d9372010-02-26 13:31:12 +0000462 __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument.
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 __ test(ebx, Immediate(kSmiTagMask));
Andrei Popescu402d9372010-02-26 13:31:12 +0000464 __ j(zero, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000465
466 __ cmp(ebx, Factory::null_value());
467 __ j(equal, &use_global_receiver);
468 __ cmp(ebx, Factory::undefined_value());
469 __ j(equal, &use_global_receiver);
470
471 __ mov(ecx, FieldOperand(ebx, HeapObject::kMapOffset));
472 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
473 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
Andrei Popescu402d9372010-02-26 13:31:12 +0000474 __ j(below, &convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 __ cmp(ecx, LAST_JS_OBJECT_TYPE);
Andrei Popescu402d9372010-02-26 13:31:12 +0000476 __ j(below_equal, &shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000477
Andrei Popescu402d9372010-02-26 13:31:12 +0000478 __ bind(&convert_to_object);
479 __ EnterInternalFrame(); // In order to preserve argument count.
Leon Clarkee46be812010-01-19 14:06:41 +0000480 __ SmiTag(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000481 __ push(eax);
482
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 __ push(ebx);
484 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
485 __ mov(ebx, eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000486
Steve Blocka7e24c12009-10-30 11:49:00 +0000487 __ pop(eax);
Leon Clarkee46be812010-01-19 14:06:41 +0000488 __ SmiUntag(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000489 __ LeaveInternalFrame();
Andrei Popescu402d9372010-02-26 13:31:12 +0000490 // Restore the function to edi.
491 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000492 __ jmp(&patch_receiver);
493
Andrei Popescu402d9372010-02-26 13:31:12 +0000494 // Use the global receiver object from the called function as the
495 // receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +0000496 __ bind(&use_global_receiver);
497 const int kGlobalIndex =
498 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
499 __ mov(ebx, FieldOperand(esi, kGlobalIndex));
Steve Blockd0582a62009-12-15 09:54:21 +0000500 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalContextOffset));
501 __ mov(ebx, FieldOperand(ebx, kGlobalIndex));
Steve Blocka7e24c12009-10-30 11:49:00 +0000502 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
503
504 __ bind(&patch_receiver);
505 __ mov(Operand(esp, eax, times_4, 0), ebx);
506
Andrei Popescu402d9372010-02-26 13:31:12 +0000507 __ jmp(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000508 }
509
Andrei Popescu402d9372010-02-26 13:31:12 +0000510 // 3b. Patch the first argument when calling a non-function. The
511 // CALL_NON_FUNCTION builtin expects the non-function callee as
512 // receiver, so overwrite the first argument which will ultimately
513 // become the receiver.
514 __ bind(&non_function);
515 __ mov(Operand(esp, eax, times_4, 0), edi);
516 // Clear edi to indicate a non-function being called.
517 __ xor_(edi, Operand(edi));
Leon Clarkee46be812010-01-19 14:06:41 +0000518
Andrei Popescu402d9372010-02-26 13:31:12 +0000519 // 4. Shift arguments and return address one slot down on the stack
520 // (overwriting the original receiver). Adjust argument count to make
521 // the original first argument the new receiver.
522 __ bind(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 { Label loop;
Leon Clarkee46be812010-01-19 14:06:41 +0000524 __ mov(ecx, eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000525 __ bind(&loop);
526 __ mov(ebx, Operand(esp, ecx, times_4, 0));
527 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
528 __ dec(ecx);
Andrei Popescu402d9372010-02-26 13:31:12 +0000529 __ j(not_sign, &loop); // While non-negative (to copy return address).
Leon Clarkee46be812010-01-19 14:06:41 +0000530 __ pop(ebx); // Discard copy of return address.
531 __ dec(eax); // One fewer argument (first argument is new receiver).
Steve Blocka7e24c12009-10-30 11:49:00 +0000532 }
533
Andrei Popescu402d9372010-02-26 13:31:12 +0000534 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin.
535 { Label function;
536 __ test(edi, Operand(edi));
537 __ j(not_zero, &function, taken);
538 __ xor_(ebx, Operand(ebx));
539 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
540 __ jmp(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
541 RelocInfo::CODE_TARGET);
542 __ bind(&function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000543 }
544
Andrei Popescu402d9372010-02-26 13:31:12 +0000545 // 5b. Get the code to call from the function and check that the number of
546 // expected arguments matches what we're providing. If so, jump
547 // (tail-call) to the code in register edx without checking arguments.
548 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
549 __ mov(ebx,
550 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
551 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
552 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
553 __ cmp(eax, Operand(ebx));
554 __ j(not_equal, Handle<Code>(builtin(ArgumentsAdaptorTrampoline)));
555
Steve Blocka7e24c12009-10-30 11:49:00 +0000556 ParameterCount expected(0);
557 __ InvokeCode(Operand(edx), expected, expected, JUMP_FUNCTION);
558}
559
560
561void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
562 __ EnterInternalFrame();
563
564 __ push(Operand(ebp, 4 * kPointerSize)); // push this
565 __ push(Operand(ebp, 2 * kPointerSize)); // push arguments
566 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
567
Steve Blockd0582a62009-12-15 09:54:21 +0000568 // Check the stack for overflow. We are not trying need to catch
569 // interruptions (e.g. debug break and preemption) here, so the "real stack
570 // limit" is checked.
571 Label okay;
572 ExternalReference real_stack_limit =
573 ExternalReference::address_of_real_stack_limit();
574 __ mov(edi, Operand::StaticVariable(real_stack_limit));
575 // Make ecx the space we have left. The stack might already be overflowed
576 // here which will cause ecx to become negative.
577 __ mov(ecx, Operand(esp));
578 __ sub(ecx, Operand(edi));
579 // Make edx the space we need for the array when it is unrolled onto the
580 // stack.
581 __ mov(edx, Operand(eax));
582 __ shl(edx, kPointerSizeLog2 - kSmiTagSize);
583 // Check if the arguments will overflow the stack.
584 __ cmp(ecx, Operand(edx));
585 __ j(greater, &okay, taken); // Signed comparison.
Steve Blocka7e24c12009-10-30 11:49:00 +0000586
Steve Blockd0582a62009-12-15 09:54:21 +0000587 // Out of stack space.
588 __ push(Operand(ebp, 4 * kPointerSize)); // push this
589 __ push(eax);
590 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
591 __ bind(&okay);
592 // End of stack check.
Steve Blocka7e24c12009-10-30 11:49:00 +0000593
594 // Push current index and limit.
595 const int kLimitOffset =
596 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
597 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
598 __ push(eax); // limit
599 __ push(Immediate(0)); // index
600
601 // Change context eagerly to get the right global object if
602 // necessary.
603 __ mov(edi, Operand(ebp, 4 * kPointerSize));
604 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
605
606 // Compute the receiver.
607 Label call_to_object, use_global_receiver, push_receiver;
608 __ mov(ebx, Operand(ebp, 3 * kPointerSize));
609 __ test(ebx, Immediate(kSmiTagMask));
610 __ j(zero, &call_to_object);
611 __ cmp(ebx, Factory::null_value());
612 __ j(equal, &use_global_receiver);
613 __ cmp(ebx, Factory::undefined_value());
614 __ j(equal, &use_global_receiver);
615
616 // If given receiver is already a JavaScript object then there's no
617 // reason for converting it.
618 __ mov(ecx, FieldOperand(ebx, HeapObject::kMapOffset));
619 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
620 __ cmp(ecx, FIRST_JS_OBJECT_TYPE);
621 __ j(less, &call_to_object);
622 __ cmp(ecx, LAST_JS_OBJECT_TYPE);
623 __ j(less_equal, &push_receiver);
624
625 // Convert the receiver to an object.
626 __ bind(&call_to_object);
627 __ push(ebx);
628 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
629 __ mov(ebx, Operand(eax));
630 __ jmp(&push_receiver);
631
632 // Use the current global receiver object as the receiver.
633 __ bind(&use_global_receiver);
634 const int kGlobalOffset =
635 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
636 __ mov(ebx, FieldOperand(esi, kGlobalOffset));
Steve Blockd0582a62009-12-15 09:54:21 +0000637 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalContextOffset));
638 __ mov(ebx, FieldOperand(ebx, kGlobalOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000639 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
640
641 // Push the receiver.
642 __ bind(&push_receiver);
643 __ push(ebx);
644
645 // Copy all arguments from the array to the stack.
646 Label entry, loop;
647 __ mov(eax, Operand(ebp, kIndexOffset));
648 __ jmp(&entry);
649 __ bind(&loop);
Andrei Popescu402d9372010-02-26 13:31:12 +0000650 __ mov(edx, Operand(ebp, 2 * kPointerSize)); // load arguments
Steve Blocka7e24c12009-10-30 11:49:00 +0000651
652 // Use inline caching to speed up access to arguments.
653 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
654 __ call(ic, RelocInfo::CODE_TARGET);
655 // It is important that we do not have a test instruction after the
656 // call. A test instruction after the call is used to indicate that
657 // we have generated an inline version of the keyed load. In this
658 // case, we know that we are not generating a test instruction next.
659
Andrei Popescu402d9372010-02-26 13:31:12 +0000660 // Push the nth argument.
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 __ push(eax);
662
663 // Update the index on the stack and in register eax.
664 __ mov(eax, Operand(ebp, kIndexOffset));
665 __ add(Operand(eax), Immediate(1 << kSmiTagSize));
666 __ mov(Operand(ebp, kIndexOffset), eax);
667
668 __ bind(&entry);
669 __ cmp(eax, Operand(ebp, kLimitOffset));
670 __ j(not_equal, &loop);
671
672 // Invoke the function.
673 ParameterCount actual(eax);
Leon Clarkee46be812010-01-19 14:06:41 +0000674 __ SmiUntag(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000675 __ mov(edi, Operand(ebp, 4 * kPointerSize));
676 __ InvokeFunction(edi, actual, CALL_FUNCTION);
677
678 __ LeaveInternalFrame();
679 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
680}
681
682
683// Load the built-in Array function from the current context.
684static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
685 // Load the global context.
686 __ mov(result, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
687 __ mov(result, FieldOperand(result, GlobalObject::kGlobalContextOffset));
688 // Load the Array function from the global context.
689 __ mov(result,
690 Operand(result, Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
691}
692
693
694// Number of empty elements to allocate for an empty array.
695static const int kPreallocatedArrayElements = 4;
696
697
698// Allocate an empty JSArray. The allocated array is put into the result
699// register. If the parameter initial_capacity is larger than zero an elements
700// backing store is allocated with this size and filled with the hole values.
701// Otherwise the elements backing store is set to the empty FixedArray.
702static void AllocateEmptyJSArray(MacroAssembler* masm,
703 Register array_function,
704 Register result,
705 Register scratch1,
706 Register scratch2,
707 Register scratch3,
708 int initial_capacity,
709 Label* gc_required) {
710 ASSERT(initial_capacity >= 0);
711
712 // Load the initial map from the array function.
713 __ mov(scratch1, FieldOperand(array_function,
714 JSFunction::kPrototypeOrInitialMapOffset));
715
716 // Allocate the JSArray object together with space for a fixed array with the
717 // requested elements.
718 int size = JSArray::kSize;
719 if (initial_capacity > 0) {
720 size += FixedArray::SizeFor(initial_capacity);
721 }
722 __ AllocateInNewSpace(size,
723 result,
724 scratch2,
725 scratch3,
726 gc_required,
727 TAG_OBJECT);
728
729 // Allocated the JSArray. Now initialize the fields except for the elements
730 // array.
731 // result: JSObject
732 // scratch1: initial map
733 // scratch2: start of next object
734 __ mov(FieldOperand(result, JSObject::kMapOffset), scratch1);
735 __ mov(FieldOperand(result, JSArray::kPropertiesOffset),
736 Factory::empty_fixed_array());
737 // Field JSArray::kElementsOffset is initialized later.
738 __ mov(FieldOperand(result, JSArray::kLengthOffset), Immediate(0));
739
740 // If no storage is requested for the elements array just set the empty
741 // fixed array.
742 if (initial_capacity == 0) {
743 __ mov(FieldOperand(result, JSArray::kElementsOffset),
744 Factory::empty_fixed_array());
745 return;
746 }
747
748 // Calculate the location of the elements array and set elements array member
749 // of the JSArray.
750 // result: JSObject
751 // scratch2: start of next object
752 __ lea(scratch1, Operand(result, JSArray::kSize));
753 __ mov(FieldOperand(result, JSArray::kElementsOffset), scratch1);
754
755 // Initialize the FixedArray and fill it with holes. FixedArray length is not
756 // stored as a smi.
757 // result: JSObject
758 // scratch1: elements array
759 // scratch2: start of next object
760 __ mov(FieldOperand(scratch1, JSObject::kMapOffset),
761 Factory::fixed_array_map());
762 __ mov(FieldOperand(scratch1, Array::kLengthOffset),
763 Immediate(initial_capacity));
764
765 // Fill the FixedArray with the hole value. Inline the code if short.
766 // Reconsider loop unfolding if kPreallocatedArrayElements gets changed.
767 static const int kLoopUnfoldLimit = 4;
768 ASSERT(kPreallocatedArrayElements <= kLoopUnfoldLimit);
769 if (initial_capacity <= kLoopUnfoldLimit) {
770 // Use a scratch register here to have only one reloc info when unfolding
771 // the loop.
772 __ mov(scratch3, Factory::the_hole_value());
773 for (int i = 0; i < initial_capacity; i++) {
774 __ mov(FieldOperand(scratch1,
775 FixedArray::kHeaderSize + i * kPointerSize),
776 scratch3);
777 }
778 } else {
779 Label loop, entry;
780 __ jmp(&entry);
781 __ bind(&loop);
782 __ mov(Operand(scratch1, 0), Factory::the_hole_value());
783 __ add(Operand(scratch1), Immediate(kPointerSize));
784 __ bind(&entry);
785 __ cmp(scratch1, Operand(scratch2));
786 __ j(below, &loop);
787 }
788}
789
790
791// Allocate a JSArray with the number of elements stored in a register. The
792// register array_function holds the built-in Array function and the register
793// array_size holds the size of the array as a smi. The allocated array is put
794// into the result register and beginning and end of the FixedArray elements
795// storage is put into registers elements_array and elements_array_end (see
796// below for when that is not the case). If the parameter fill_with_holes is
797// true the allocated elements backing store is filled with the hole values
798// otherwise it is left uninitialized. When the backing store is filled the
799// register elements_array is scratched.
800static void AllocateJSArray(MacroAssembler* masm,
801 Register array_function, // Array function.
Steve Block6ded16b2010-05-10 14:33:55 +0100802 Register array_size, // As a smi, cannot be 0.
Steve Blocka7e24c12009-10-30 11:49:00 +0000803 Register result,
804 Register elements_array,
805 Register elements_array_end,
806 Register scratch,
807 bool fill_with_hole,
808 Label* gc_required) {
Steve Block6ded16b2010-05-10 14:33:55 +0100809 ASSERT(scratch.is(edi)); // rep stos destination
810 ASSERT(!fill_with_hole || array_size.is(ecx)); // rep stos count
Leon Clarkef7060e22010-06-03 12:02:55 +0100811 ASSERT(!fill_with_hole || !result.is(eax)); // result is never eax
Steve Blocka7e24c12009-10-30 11:49:00 +0000812
813 // Load the initial map from the array function.
814 __ mov(elements_array,
815 FieldOperand(array_function,
816 JSFunction::kPrototypeOrInitialMapOffset));
817
Steve Blocka7e24c12009-10-30 11:49:00 +0000818 // Allocate the JSArray object together with space for a FixedArray with the
819 // requested elements.
Steve Blocka7e24c12009-10-30 11:49:00 +0000820 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
821 __ AllocateInNewSpace(JSArray::kSize + FixedArray::kHeaderSize,
822 times_half_pointer_size, // array_size is a smi.
823 array_size,
824 result,
825 elements_array_end,
826 scratch,
827 gc_required,
828 TAG_OBJECT);
829
830 // Allocated the JSArray. Now initialize the fields except for the elements
831 // array.
832 // result: JSObject
833 // elements_array: initial map
834 // elements_array_end: start of next object
835 // array_size: size of array (smi)
Steve Blocka7e24c12009-10-30 11:49:00 +0000836 __ mov(FieldOperand(result, JSObject::kMapOffset), elements_array);
837 __ mov(elements_array, Factory::empty_fixed_array());
838 __ mov(FieldOperand(result, JSArray::kPropertiesOffset), elements_array);
839 // Field JSArray::kElementsOffset is initialized later.
840 __ mov(FieldOperand(result, JSArray::kLengthOffset), array_size);
841
842 // Calculate the location of the elements array and set elements array member
843 // of the JSArray.
844 // result: JSObject
845 // elements_array_end: start of next object
846 // array_size: size of array (smi)
847 __ lea(elements_array, Operand(result, JSArray::kSize));
848 __ mov(FieldOperand(result, JSArray::kElementsOffset), elements_array);
849
850 // Initialize the fixed array. FixedArray length is not stored as a smi.
851 // result: JSObject
852 // elements_array: elements array
853 // elements_array_end: start of next object
854 // array_size: size of array (smi)
855 ASSERT(kSmiTag == 0);
Leon Clarkee46be812010-01-19 14:06:41 +0000856 __ SmiUntag(array_size); // Convert from smi to value.
Steve Blocka7e24c12009-10-30 11:49:00 +0000857 __ mov(FieldOperand(elements_array, JSObject::kMapOffset),
858 Factory::fixed_array_map());
Steve Blocka7e24c12009-10-30 11:49:00 +0000859 // For non-empty JSArrays the length of the FixedArray and the JSArray is the
860 // same.
861 __ mov(FieldOperand(elements_array, Array::kLengthOffset), array_size);
862
863 // Fill the allocated FixedArray with the hole value if requested.
864 // result: JSObject
865 // elements_array: elements array
Steve Blocka7e24c12009-10-30 11:49:00 +0000866 if (fill_with_hole) {
Steve Block6ded16b2010-05-10 14:33:55 +0100867 __ lea(edi, Operand(elements_array,
868 FixedArray::kHeaderSize - kHeapObjectTag));
Steve Block6ded16b2010-05-10 14:33:55 +0100869 __ mov(eax, Factory::the_hole_value());
Steve Block6ded16b2010-05-10 14:33:55 +0100870 __ cld();
Leon Clarkef7060e22010-06-03 12:02:55 +0100871 // Do not use rep stos when filling less than kRepStosThreshold
872 // words.
873 const int kRepStosThreshold = 16;
874 Label loop, entry, done;
875 __ cmp(ecx, kRepStosThreshold);
876 __ j(below, &loop); // Note: ecx > 0.
Steve Block6ded16b2010-05-10 14:33:55 +0100877 __ rep_stos();
Leon Clarkef7060e22010-06-03 12:02:55 +0100878 __ jmp(&done);
879 __ bind(&loop);
880 __ stos();
881 __ bind(&entry);
882 __ cmp(edi, Operand(elements_array_end));
883 __ j(below, &loop);
884 __ bind(&done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000885 }
886}
887
888
889// Create a new array for the built-in Array function. This function allocates
890// the JSArray object and the FixedArray elements array and initializes these.
891// If the Array cannot be constructed in native code the runtime is called. This
892// function assumes the following state:
893// edi: constructor (built-in Array function)
894// eax: argc
895// esp[0]: return address
896// esp[4]: last argument
897// This function is used for both construct and normal calls of Array. Whether
898// it is a construct call or not is indicated by the construct_call parameter.
899// The only difference between handling a construct call and a normal call is
900// that for a construct call the constructor function in edi needs to be
901// preserved for entering the generic code. In both cases argc in eax needs to
902// be preserved.
903static void ArrayNativeCode(MacroAssembler* masm,
904 bool construct_call,
Steve Blockd0582a62009-12-15 09:54:21 +0000905 Label* call_generic_code) {
Steve Block6ded16b2010-05-10 14:33:55 +0100906 Label argc_one_or_more, argc_two_or_more, prepare_generic_code_call,
907 empty_array, not_empty_array;
Steve Blocka7e24c12009-10-30 11:49:00 +0000908
909 // Push the constructor and argc. No need to tag argc as a smi, as there will
910 // be no garbage collection with this on the stack.
911 int push_count = 0;
912 if (construct_call) {
913 push_count++;
914 __ push(edi);
915 }
916 push_count++;
917 __ push(eax);
918
919 // Check for array construction with zero arguments.
920 __ test(eax, Operand(eax));
921 __ j(not_zero, &argc_one_or_more);
922
Steve Block6ded16b2010-05-10 14:33:55 +0100923 __ bind(&empty_array);
Steve Blocka7e24c12009-10-30 11:49:00 +0000924 // Handle construction of an empty array.
925 AllocateEmptyJSArray(masm,
926 edi,
927 eax,
928 ebx,
929 ecx,
930 edi,
931 kPreallocatedArrayElements,
932 &prepare_generic_code_call);
933 __ IncrementCounter(&Counters::array_function_native, 1);
934 __ pop(ebx);
935 if (construct_call) {
936 __ pop(edi);
937 }
938 __ ret(kPointerSize);
939
940 // Check for one argument. Bail out if argument is not smi or if it is
941 // negative.
942 __ bind(&argc_one_or_more);
943 __ cmp(eax, 1);
944 __ j(not_equal, &argc_two_or_more);
945 ASSERT(kSmiTag == 0);
Steve Block6ded16b2010-05-10 14:33:55 +0100946 __ mov(ecx, Operand(esp, (push_count + 1) * kPointerSize));
947 __ test(ecx, Operand(ecx));
948 __ j(not_zero, &not_empty_array);
949
950 // The single argument passed is zero, so we jump to the code above used to
951 // handle the case of no arguments passed. To adapt the stack for that we move
952 // the return address and the pushed constructor (if pushed) one stack slot up
953 // thereby removing the passed argument. Argc is also on the stack - at the
954 // bottom - and it needs to be changed from 1 to 0 to have the call into the
955 // runtime system work in case a GC is required.
956 for (int i = push_count; i > 0; i--) {
957 __ mov(eax, Operand(esp, i * kPointerSize));
958 __ mov(Operand(esp, (i + 1) * kPointerSize), eax);
959 }
960 __ add(Operand(esp), Immediate(2 * kPointerSize)); // Drop two stack slots.
961 __ push(Immediate(0)); // Treat this as a call with argc of zero.
962 __ jmp(&empty_array);
963
964 __ bind(&not_empty_array);
965 __ test(ecx, Immediate(kIntptrSignBit | kSmiTagMask));
Steve Blocka7e24c12009-10-30 11:49:00 +0000966 __ j(not_zero, &prepare_generic_code_call);
967
968 // Handle construction of an empty array of a certain size. Get the size from
969 // the stack and bail out if size is to large to actually allocate an elements
970 // array.
Steve Block6ded16b2010-05-10 14:33:55 +0100971 __ cmp(ecx, JSObject::kInitialMaxFastElementArray << kSmiTagSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000972 __ j(greater_equal, &prepare_generic_code_call);
973
974 // edx: array_size (smi)
975 // edi: constructor
Steve Block6ded16b2010-05-10 14:33:55 +0100976 // esp[0]: argc (cannot be 0 here)
Steve Blocka7e24c12009-10-30 11:49:00 +0000977 // esp[4]: constructor (only if construct_call)
978 // esp[8]: return address
979 // esp[C]: argument
980 AllocateJSArray(masm,
981 edi,
Steve Block6ded16b2010-05-10 14:33:55 +0100982 ecx,
Steve Blocka7e24c12009-10-30 11:49:00 +0000983 ebx,
Leon Clarkef7060e22010-06-03 12:02:55 +0100984 eax,
Steve Block6ded16b2010-05-10 14:33:55 +0100985 edx,
Steve Blocka7e24c12009-10-30 11:49:00 +0000986 edi,
987 true,
988 &prepare_generic_code_call);
989 __ IncrementCounter(&Counters::array_function_native, 1);
Leon Clarkef7060e22010-06-03 12:02:55 +0100990 __ mov(eax, ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000991 __ pop(ebx);
992 if (construct_call) {
993 __ pop(edi);
994 }
995 __ ret(2 * kPointerSize);
996
997 // Handle construction of an array from a list of arguments.
998 __ bind(&argc_two_or_more);
999 ASSERT(kSmiTag == 0);
Leon Clarkee46be812010-01-19 14:06:41 +00001000 __ SmiTag(eax); // Convet argc to a smi.
Steve Blocka7e24c12009-10-30 11:49:00 +00001001 // eax: array_size (smi)
1002 // edi: constructor
1003 // esp[0] : argc
1004 // esp[4]: constructor (only if construct_call)
1005 // esp[8] : return address
1006 // esp[C] : last argument
1007 AllocateJSArray(masm,
1008 edi,
1009 eax,
1010 ebx,
1011 ecx,
1012 edx,
1013 edi,
1014 false,
1015 &prepare_generic_code_call);
1016 __ IncrementCounter(&Counters::array_function_native, 1);
1017 __ mov(eax, ebx);
1018 __ pop(ebx);
1019 if (construct_call) {
1020 __ pop(edi);
1021 }
1022 __ push(eax);
1023 // eax: JSArray
1024 // ebx: argc
1025 // edx: elements_array_end (untagged)
1026 // esp[0]: JSArray
1027 // esp[4]: return address
1028 // esp[8]: last argument
1029
1030 // Location of the last argument
1031 __ lea(edi, Operand(esp, 2 * kPointerSize));
1032
1033 // Location of the first array element (Parameter fill_with_holes to
1034 // AllocateJSArrayis false, so the FixedArray is returned in ecx).
1035 __ lea(edx, Operand(ecx, FixedArray::kHeaderSize - kHeapObjectTag));
1036
1037 // ebx: argc
1038 // edx: location of the first array element
1039 // edi: location of the last argument
1040 // esp[0]: JSArray
1041 // esp[4]: return address
1042 // esp[8]: last argument
1043 Label loop, entry;
1044 __ mov(ecx, ebx);
1045 __ jmp(&entry);
1046 __ bind(&loop);
1047 __ mov(eax, Operand(edi, ecx, times_pointer_size, 0));
1048 __ mov(Operand(edx, 0), eax);
1049 __ add(Operand(edx), Immediate(kPointerSize));
1050 __ bind(&entry);
1051 __ dec(ecx);
1052 __ j(greater_equal, &loop);
1053
1054 // Remove caller arguments from the stack and return.
1055 // ebx: argc
1056 // esp[0]: JSArray
1057 // esp[4]: return address
1058 // esp[8]: last argument
1059 __ pop(eax);
1060 __ pop(ecx);
1061 __ lea(esp, Operand(esp, ebx, times_pointer_size, 1 * kPointerSize));
1062 __ push(ecx);
1063 __ ret(0);
1064
1065 // Restore argc and constructor before running the generic code.
1066 __ bind(&prepare_generic_code_call);
1067 __ pop(eax);
1068 if (construct_call) {
1069 __ pop(edi);
1070 }
1071 __ jmp(call_generic_code);
1072}
1073
1074
1075void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1076 // ----------- S t a t e -------------
1077 // -- eax : argc
1078 // -- esp[0] : return address
1079 // -- esp[4] : last argument
1080 // -----------------------------------
Kristian Monsen25f61362010-05-21 11:50:48 +01001081 Label generic_array_code;
Steve Blocka7e24c12009-10-30 11:49:00 +00001082
1083 // Get the Array function.
1084 GenerateLoadArrayFunction(masm, edi);
1085
1086 if (FLAG_debug_code) {
1087 // Initial map for the builtin Array function shoud be a map.
1088 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1089 // Will both indicate a NULL and a Smi.
1090 __ test(ebx, Immediate(kSmiTagMask));
1091 __ Assert(not_zero, "Unexpected initial map for Array function");
1092 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1093 __ Assert(equal, "Unexpected initial map for Array function");
1094 }
1095
1096 // Run the native code for the Array function called as a normal function.
1097 ArrayNativeCode(masm, false, &generic_array_code);
1098
1099 // Jump to the generic array code in case the specialized code cannot handle
1100 // the construction.
1101 __ bind(&generic_array_code);
1102 Code* code = Builtins::builtin(Builtins::ArrayCodeGeneric);
1103 Handle<Code> array_code(code);
1104 __ jmp(array_code, RelocInfo::CODE_TARGET);
1105}
1106
1107
1108void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
1109 // ----------- S t a t e -------------
1110 // -- eax : argc
1111 // -- edi : constructor
1112 // -- esp[0] : return address
1113 // -- esp[4] : last argument
1114 // -----------------------------------
1115 Label generic_constructor;
1116
1117 if (FLAG_debug_code) {
1118 // The array construct code is only set for the builtin Array function which
1119 // does always have a map.
1120 GenerateLoadArrayFunction(masm, ebx);
1121 __ cmp(edi, Operand(ebx));
1122 __ Assert(equal, "Unexpected Array function");
1123 // Initial map for the builtin Array function should be a map.
1124 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1125 // Will both indicate a NULL and a Smi.
1126 __ test(ebx, Immediate(kSmiTagMask));
1127 __ Assert(not_zero, "Unexpected initial map for Array function");
1128 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1129 __ Assert(equal, "Unexpected initial map for Array function");
1130 }
1131
1132 // Run the native code for the Array function called as constructor.
1133 ArrayNativeCode(masm, true, &generic_constructor);
1134
1135 // Jump to the generic construct code in case the specialized code cannot
1136 // handle the construction.
1137 __ bind(&generic_constructor);
1138 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
1139 Handle<Code> generic_construct_stub(code);
1140 __ jmp(generic_construct_stub, RelocInfo::CODE_TARGET);
1141}
1142
1143
1144static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1145 __ push(ebp);
1146 __ mov(ebp, Operand(esp));
1147
1148 // Store the arguments adaptor context sentinel.
1149 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1150
1151 // Push the function on the stack.
1152 __ push(edi);
1153
1154 // Preserve the number of arguments on the stack. Must preserve both
1155 // eax and ebx because these registers are used when copying the
1156 // arguments and the receiver.
1157 ASSERT(kSmiTagSize == 1);
1158 __ lea(ecx, Operand(eax, eax, times_1, kSmiTag));
1159 __ push(ecx);
1160}
1161
1162
1163static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
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.
1171 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
1172 __ pop(ecx);
1173 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
1174 __ push(ecx);
1175}
1176
1177
1178void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1179 // ----------- S t a t e -------------
1180 // -- eax : actual number of arguments
1181 // -- ebx : expected number of arguments
1182 // -- edx : code entry to call
1183 // -----------------------------------
1184
1185 Label invoke, dont_adapt_arguments;
1186 __ IncrementCounter(&Counters::arguments_adaptors, 1);
1187
1188 Label enough, too_few;
1189 __ cmp(eax, Operand(ebx));
1190 __ j(less, &too_few);
1191 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1192 __ j(equal, &dont_adapt_arguments);
1193
1194 { // Enough parameters: Actual >= expected.
1195 __ bind(&enough);
1196 EnterArgumentsAdaptorFrame(masm);
1197
1198 // Copy receiver and all expected arguments.
1199 const int offset = StandardFrameConstants::kCallerSPOffset;
1200 __ lea(eax, Operand(ebp, eax, times_4, offset));
1201 __ mov(ecx, -1); // account for receiver
1202
1203 Label copy;
1204 __ bind(&copy);
1205 __ inc(ecx);
1206 __ push(Operand(eax, 0));
1207 __ sub(Operand(eax), Immediate(kPointerSize));
1208 __ cmp(ecx, Operand(ebx));
1209 __ j(less, &copy);
1210 __ jmp(&invoke);
1211 }
1212
1213 { // Too few parameters: Actual < expected.
1214 __ bind(&too_few);
1215 EnterArgumentsAdaptorFrame(masm);
1216
1217 // Copy receiver and all actual arguments.
1218 const int offset = StandardFrameConstants::kCallerSPOffset;
1219 __ lea(edi, Operand(ebp, eax, times_4, offset));
1220 __ mov(ecx, -1); // account for receiver
1221
1222 Label copy;
1223 __ bind(&copy);
1224 __ inc(ecx);
1225 __ push(Operand(edi, 0));
1226 __ sub(Operand(edi), Immediate(kPointerSize));
1227 __ cmp(ecx, Operand(eax));
1228 __ j(less, &copy);
1229
1230 // Fill remaining expected arguments with undefined values.
1231 Label fill;
1232 __ bind(&fill);
1233 __ inc(ecx);
1234 __ push(Immediate(Factory::undefined_value()));
1235 __ cmp(ecx, Operand(ebx));
1236 __ j(less, &fill);
1237
1238 // Restore function pointer.
1239 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1240 }
1241
1242 // Call the entry point.
1243 __ bind(&invoke);
1244 __ call(Operand(edx));
1245
1246 // Leave frame and return.
1247 LeaveArgumentsAdaptorFrame(masm);
1248 __ ret(0);
1249
1250 // -------------------------------------------
1251 // Dont adapt arguments.
1252 // -------------------------------------------
1253 __ bind(&dont_adapt_arguments);
1254 __ jmp(Operand(edx));
1255}
1256
1257
1258#undef __
1259
1260} } // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001261
1262#endif // V8_TARGET_ARCH_IA32