blob: 35eddc45e30d875edd4ba768e1443149c0ba8acc [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 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#include "codegen-inl.h"
30#include "macro-assembler.h"
31
32namespace v8 {
33namespace internal {
34
35#define __ ACCESS_MASM(masm)
36
37void Builtins::Generate_Adaptor(MacroAssembler* masm, CFunctionId id) {
38 // TODO(428): Don't pass the function in a static variable.
39 ExternalReference passed = ExternalReference::builtin_passed_function();
40 __ movq(kScratchRegister, passed.address(), RelocInfo::EXTERNAL_REFERENCE);
41 __ movq(Operand(kScratchRegister, 0), rdi);
42
43 // The actual argument count has already been loaded into register
44 // rax, but JumpToRuntime expects rax to contain the number of
45 // arguments including the receiver.
46 __ incq(rax);
47 __ JumpToRuntime(ExternalReference(id), 1);
48}
49
50
51static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
52 __ push(rbp);
53 __ movq(rbp, rsp);
54
55 // Store the arguments adaptor context sentinel.
56 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
57
58 // Push the function on the stack.
59 __ push(rdi);
60
61 // Preserve the number of arguments on the stack. Must preserve both
62 // rax and rbx because these registers are used when copying the
63 // arguments and the receiver.
64 __ Integer32ToSmi(rcx, rax);
65 __ push(rcx);
66}
67
68
69static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
70 // Retrieve the number of arguments from the stack. Number is a Smi.
71 __ movq(rbx, Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset));
72
73 // Leave the frame.
74 __ movq(rsp, rbp);
75 __ pop(rbp);
76
77 // Remove caller arguments from the stack.
78 // rbx holds a Smi, so we convery to dword offset by multiplying by 4.
79 // TODO(smi): Find a way to abstract indexing by a smi.
80 ASSERT_EQ(kSmiTagSize, 1 && kSmiTag == 0);
81 ASSERT_EQ(kPointerSize, (1 << kSmiTagSize) * 4);
82 // TODO(smi): Find way to abstract indexing by a smi.
83 __ pop(rcx);
84 // 1 * kPointerSize is offset of receiver.
85 __ lea(rsp, Operand(rsp, rbx, times_half_pointer_size, 1 * kPointerSize));
86 __ push(rcx);
87}
88
89
90void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
91 // ----------- S t a t e -------------
92 // -- rax : actual number of arguments
93 // -- rbx : expected number of arguments
94 // -- rdx : code entry to call
95 // -----------------------------------
96
97 Label invoke, dont_adapt_arguments;
98 __ IncrementCounter(&Counters::arguments_adaptors, 1);
99
100 Label enough, too_few;
101 __ cmpq(rax, rbx);
102 __ j(less, &too_few);
103 __ cmpq(rbx, Immediate(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
104 __ j(equal, &dont_adapt_arguments);
105
106 { // Enough parameters: Actual >= expected.
107 __ bind(&enough);
108 EnterArgumentsAdaptorFrame(masm);
109
110 // Copy receiver and all expected arguments.
111 const int offset = StandardFrameConstants::kCallerSPOffset;
112 __ lea(rax, Operand(rbp, rax, times_pointer_size, offset));
113 __ movq(rcx, Immediate(-1)); // account for receiver
114
115 Label copy;
116 __ bind(&copy);
117 __ incq(rcx);
118 __ push(Operand(rax, 0));
119 __ subq(rax, Immediate(kPointerSize));
120 __ cmpq(rcx, rbx);
121 __ j(less, &copy);
122 __ jmp(&invoke);
123 }
124
125 { // Too few parameters: Actual < expected.
126 __ bind(&too_few);
127 EnterArgumentsAdaptorFrame(masm);
128
129 // Copy receiver and all actual arguments.
130 const int offset = StandardFrameConstants::kCallerSPOffset;
131 __ lea(rdi, Operand(rbp, rax, times_pointer_size, offset));
132 __ movq(rcx, Immediate(-1)); // account for receiver
133
134 Label copy;
135 __ bind(&copy);
136 __ incq(rcx);
137 __ push(Operand(rdi, 0));
138 __ subq(rdi, Immediate(kPointerSize));
139 __ cmpq(rcx, rax);
140 __ j(less, &copy);
141
142 // Fill remaining expected arguments with undefined values.
143 Label fill;
144 __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex);
145 __ bind(&fill);
146 __ incq(rcx);
147 __ push(kScratchRegister);
148 __ cmpq(rcx, rbx);
149 __ j(less, &fill);
150
151 // Restore function pointer.
152 __ movq(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
153 }
154
155 // Call the entry point.
156 __ bind(&invoke);
157 __ call(rdx);
158
159 // Leave frame and return.
160 LeaveArgumentsAdaptorFrame(masm);
161 __ ret(0);
162
163 // -------------------------------------------
164 // Dont adapt arguments.
165 // -------------------------------------------
166 __ bind(&dont_adapt_arguments);
167 __ jmp(rdx);
168}
169
170
171void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
172 // Stack Layout:
173 // rsp: return address
174 // +1: Argument n
175 // +2: Argument n-1
176 // ...
177 // +n: Argument 1 = receiver
178 // +n+1: Argument 0 = function to call
179 //
180 // rax contains the number of arguments, n, not counting the function.
181 //
182 // 1. Make sure we have at least one argument.
183 { Label done;
184 __ testq(rax, rax);
185 __ j(not_zero, &done);
186 __ pop(rbx);
187 __ Push(Factory::undefined_value());
188 __ push(rbx);
189 __ incq(rax);
190 __ bind(&done);
191 }
192
193 // 2. Get the function to call from the stack.
194 { Label done, non_function, function;
195 // The function to call is at position n+1 on the stack.
196 __ movq(rdi, Operand(rsp, rax, times_pointer_size, +1 * kPointerSize));
197 __ JumpIfSmi(rdi, &non_function);
198 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
199 __ j(equal, &function);
200
201 // Non-function called: Clear the function to force exception.
202 __ bind(&non_function);
203 __ xor_(rdi, rdi);
204 __ jmp(&done);
205
206 // Function called: Change context eagerly to get the right global object.
207 __ bind(&function);
208 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
209
210 __ bind(&done);
211 }
212
213 // 3. Make sure first argument is an object; convert if necessary.
214 { Label call_to_object, use_global_receiver, patch_receiver, done;
215 __ movq(rbx, Operand(rsp, rax, times_pointer_size, 0));
216
217 __ JumpIfSmi(rbx, &call_to_object);
218
219 __ CompareRoot(rbx, Heap::kNullValueRootIndex);
220 __ j(equal, &use_global_receiver);
221 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
222 __ j(equal, &use_global_receiver);
223
224 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, rcx);
225 __ j(below, &call_to_object);
226 __ CmpInstanceType(rcx, LAST_JS_OBJECT_TYPE);
227 __ j(below_equal, &done);
228
229 __ bind(&call_to_object);
230 __ EnterInternalFrame(); // preserves rax, rbx, rdi
231
232 // Store the arguments count on the stack (smi tagged).
233 __ Integer32ToSmi(rax, rax);
234 __ push(rax);
235
236 __ push(rdi); // save edi across the call
237 __ push(rbx);
238 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
239 __ movq(rbx, rax);
240 __ pop(rdi); // restore edi after the call
241
242 // Get the arguments count and untag it.
243 __ pop(rax);
244 __ SmiToInteger32(rax, rax);
245
246 __ LeaveInternalFrame();
247 __ jmp(&patch_receiver);
248
249 // Use the global receiver object from the called function as the receiver.
250 __ bind(&use_global_receiver);
251 const int kGlobalIndex =
252 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
253 __ movq(rbx, FieldOperand(rsi, kGlobalIndex));
254 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
255
256 __ bind(&patch_receiver);
257 __ movq(Operand(rsp, rax, times_pointer_size, 0), rbx);
258
259 __ bind(&done);
260 }
261
262 // 4. Shift stuff one slot down the stack.
263 { Label loop;
264 __ lea(rcx, Operand(rax, +1)); // +1 ~ copy receiver too
265 __ bind(&loop);
266 __ movq(rbx, Operand(rsp, rcx, times_pointer_size, 0));
267 __ movq(Operand(rsp, rcx, times_pointer_size, 1 * kPointerSize), rbx);
268 __ decq(rcx);
269 __ j(not_zero, &loop);
270 }
271
272 // 5. Remove TOS (copy of last arguments), but keep return address.
273 __ pop(rbx);
274 __ pop(rcx);
275 __ push(rbx);
276 __ decq(rax);
277
278 // 6. Check that function really was a function and get the code to
279 // call from the function and check that the number of expected
280 // arguments matches what we're providing.
281 { Label invoke, trampoline;
282 __ testq(rdi, rdi);
283 __ j(not_zero, &invoke);
284 __ xor_(rbx, rbx);
285 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
286 __ bind(&trampoline);
287 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
288 RelocInfo::CODE_TARGET);
289
290 __ bind(&invoke);
291 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
292 __ movsxlq(rbx,
293 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
294 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
295 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
296 __ cmpq(rax, rbx);
297 __ j(not_equal, &trampoline);
298 }
299
300 // 7. Jump (tail-call) to the code in register edx without checking arguments.
301 ParameterCount expected(0);
302 __ InvokeCode(rdx, expected, expected, JUMP_FUNCTION);
303}
304
305
306void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
307 // Stack at entry:
308 // rsp: return address
309 // rsp+8: arguments
310 // rsp+16: receiver ("this")
311 // rsp+24: function
312 __ EnterInternalFrame();
313 // Stack frame:
314 // rbp: Old base pointer
315 // rbp[1]: return address
316 // rbp[2]: function arguments
317 // rbp[3]: receiver
318 // rbp[4]: function
319 static const int kArgumentsOffset = 2 * kPointerSize;
320 static const int kReceiverOffset = 3 * kPointerSize;
321 static const int kFunctionOffset = 4 * kPointerSize;
322 __ push(Operand(rbp, kFunctionOffset));
323 __ push(Operand(rbp, kArgumentsOffset));
324 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
325
326 if (FLAG_check_stack) {
327 // We need to catch preemptions right here, otherwise an unlucky preemption
328 // could show up as a failed apply.
329 Label retry_preemption;
330 Label no_preemption;
331 __ bind(&retry_preemption);
332 ExternalReference stack_guard_limit =
333 ExternalReference::address_of_stack_guard_limit();
334 __ movq(kScratchRegister, stack_guard_limit);
335 __ movq(rcx, rsp);
336 __ subq(rcx, Operand(kScratchRegister, 0));
337 // rcx contains the difference between the stack limit and the stack top.
338 // We use it below to check that there is enough room for the arguments.
339 __ j(above, &no_preemption);
340
341 // Preemption!
342 // Because runtime functions always remove the receiver from the stack, we
343 // have to fake one to avoid underflowing the stack.
344 __ push(rax);
345 __ push(Immediate(Smi::FromInt(0)));
346
347 // Do call to runtime routine.
348 __ CallRuntime(Runtime::kStackGuard, 1);
349 __ pop(rax);
350 __ jmp(&retry_preemption);
351
352 __ bind(&no_preemption);
353
354 Label okay;
355 // Make rdx the space we need for the array when it is unrolled onto the
356 // stack.
357 __ PositiveSmiTimesPowerOfTwoToInteger64(rdx, rax, kPointerSizeLog2);
358 __ cmpq(rcx, rdx);
359 __ j(greater, &okay);
360
361 // Too bad: Out of stack space.
362 __ push(Operand(rbp, kFunctionOffset));
363 __ push(rax);
364 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
365 __ bind(&okay);
366 }
367
368 // Push current index and limit.
369 const int kLimitOffset =
370 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
371 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
372 __ push(rax); // limit
373 __ push(Immediate(0)); // index
374
375 // Change context eagerly to get the right global object if
376 // necessary.
377 __ movq(rdi, Operand(rbp, kFunctionOffset));
378 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
379
380 // Compute the receiver.
381 Label call_to_object, use_global_receiver, push_receiver;
382 __ movq(rbx, Operand(rbp, kReceiverOffset));
383 __ JumpIfSmi(rbx, &call_to_object);
384 __ CompareRoot(rbx, Heap::kNullValueRootIndex);
385 __ j(equal, &use_global_receiver);
386 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
387 __ j(equal, &use_global_receiver);
388
389 // If given receiver is already a JavaScript object then there's no
390 // reason for converting it.
391 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, rcx);
392 __ j(below, &call_to_object);
393 __ CmpInstanceType(rcx, LAST_JS_OBJECT_TYPE);
394 __ j(below_equal, &push_receiver);
395
396 // Convert the receiver to an object.
397 __ bind(&call_to_object);
398 __ push(rbx);
399 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
400 __ movq(rbx, rax);
401 __ jmp(&push_receiver);
402
403 // Use the current global receiver object as the receiver.
404 __ bind(&use_global_receiver);
405 const int kGlobalOffset =
406 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
407 __ movq(rbx, FieldOperand(rsi, kGlobalOffset));
408 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
409
410 // Push the receiver.
411 __ bind(&push_receiver);
412 __ push(rbx);
413
414 // Copy all arguments from the array to the stack.
415 Label entry, loop;
416 __ movq(rax, Operand(rbp, kIndexOffset));
417 __ jmp(&entry);
418 __ bind(&loop);
419 __ movq(rcx, Operand(rbp, kArgumentsOffset)); // load arguments
420 __ push(rcx);
421 __ push(rax);
422
423 // Use inline caching to speed up access to arguments.
424 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
425 __ Call(ic, RelocInfo::CODE_TARGET);
426 // It is important that we do not have a test instruction after the
427 // call. A test instruction after the call is used to indicate that
428 // we have generated an inline version of the keyed load. In this
429 // case, we know that we are not generating a test instruction next.
430
431 // Remove IC arguments from the stack and push the nth argument.
432 __ addq(rsp, Immediate(2 * kPointerSize));
433 __ push(rax);
434
435 // Update the index on the stack and in register rax.
436 __ movq(rax, Operand(rbp, kIndexOffset));
437 __ addq(rax, Immediate(Smi::FromInt(1)));
438 __ movq(Operand(rbp, kIndexOffset), rax);
439
440 __ bind(&entry);
441 __ cmpq(rax, Operand(rbp, kLimitOffset));
442 __ j(not_equal, &loop);
443
444 // Invoke the function.
445 ParameterCount actual(rax);
446 __ SmiToInteger32(rax, rax);
447 __ movq(rdi, Operand(rbp, kFunctionOffset));
448 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
449
450 __ LeaveInternalFrame();
451 __ ret(3 * kPointerSize); // remove function, receiver, and arguments
452}
453
454
455// Load the built-in Array function from the current context.
456static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
457 // Load the global context.
458 __ movq(result, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
459 __ movq(result, FieldOperand(result, GlobalObject::kGlobalContextOffset));
460 // Load the Array function from the global context.
461 __ movq(result,
462 Operand(result, Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
463}
464
465
466// Number of empty elements to allocate for an empty array.
467static const int kPreallocatedArrayElements = 4;
468
469
470// Allocate an empty JSArray. The allocated array is put into the result
471// register. If the parameter initial_capacity is larger than zero an elements
472// backing store is allocated with this size and filled with the hole values.
473// Otherwise the elements backing store is set to the empty FixedArray.
474static void AllocateEmptyJSArray(MacroAssembler* masm,
475 Register array_function,
476 Register result,
477 Register scratch1,
478 Register scratch2,
479 Register scratch3,
480 int initial_capacity,
481 Label* gc_required) {
482 ASSERT(initial_capacity >= 0);
483
484 // Load the initial map from the array function.
485 __ movq(scratch1, FieldOperand(array_function,
486 JSFunction::kPrototypeOrInitialMapOffset));
487
488 // Allocate the JSArray object together with space for a fixed array with the
489 // requested elements.
490 int size = JSArray::kSize;
491 if (initial_capacity > 0) {
492 size += FixedArray::SizeFor(initial_capacity);
493 }
494 __ AllocateInNewSpace(size,
495 result,
496 scratch2,
497 scratch3,
498 gc_required,
499 TAG_OBJECT);
500
501 // Allocated the JSArray. Now initialize the fields except for the elements
502 // array.
503 // result: JSObject
504 // scratch1: initial map
505 // scratch2: start of next object
506 __ movq(FieldOperand(result, JSObject::kMapOffset), scratch1);
507 __ Move(FieldOperand(result, JSArray::kPropertiesOffset),
508 Factory::empty_fixed_array());
509 // Field JSArray::kElementsOffset is initialized later.
510 __ movq(FieldOperand(result, JSArray::kLengthOffset), Immediate(0));
511
512 // If no storage is requested for the elements array just set the empty
513 // fixed array.
514 if (initial_capacity == 0) {
515 __ Move(FieldOperand(result, JSArray::kElementsOffset),
516 Factory::empty_fixed_array());
517 return;
518 }
519
520 // Calculate the location of the elements array and set elements array member
521 // of the JSArray.
522 // result: JSObject
523 // scratch2: start of next object
524 __ lea(scratch1, Operand(result, JSArray::kSize));
525 __ movq(FieldOperand(result, JSArray::kElementsOffset), scratch1);
526
527 // Initialize the FixedArray and fill it with holes. FixedArray length is not
528 // stored as a smi.
529 // result: JSObject
530 // scratch1: elements array
531 // scratch2: start of next object
532 __ Move(FieldOperand(scratch1, JSObject::kMapOffset),
533 Factory::fixed_array_map());
534 __ movq(FieldOperand(scratch1, Array::kLengthOffset),
535 Immediate(initial_capacity));
536
537 // Fill the FixedArray with the hole value. Inline the code if short.
538 // Reconsider loop unfolding if kPreallocatedArrayElements gets changed.
539 static const int kLoopUnfoldLimit = 4;
540 ASSERT(kPreallocatedArrayElements <= kLoopUnfoldLimit);
541 __ Move(scratch3, Factory::the_hole_value());
542 if (initial_capacity <= kLoopUnfoldLimit) {
543 // Use a scratch register here to have only one reloc info when unfolding
544 // the loop.
545 for (int i = 0; i < initial_capacity; i++) {
546 __ movq(FieldOperand(scratch1,
547 FixedArray::kHeaderSize + i * kPointerSize),
548 scratch3);
549 }
550 } else {
551 Label loop, entry;
552 __ jmp(&entry);
553 __ bind(&loop);
554 __ movq(Operand(scratch1, 0), scratch3);
555 __ addq(scratch1, Immediate(kPointerSize));
556 __ bind(&entry);
557 __ cmpq(scratch1, scratch2);
558 __ j(below, &loop);
559 }
560}
561
562
563// Allocate a JSArray with the number of elements stored in a register. The
564// register array_function holds the built-in Array function and the register
565// array_size holds the size of the array as a smi. The allocated array is put
566// into the result register and beginning and end of the FixedArray elements
567// storage is put into registers elements_array and elements_array_end (see
568// below for when that is not the case). If the parameter fill_with_holes is
569// true the allocated elements backing store is filled with the hole values
570// otherwise it is left uninitialized. When the backing store is filled the
571// register elements_array is scratched.
572static void AllocateJSArray(MacroAssembler* masm,
573 Register array_function, // Array function.
574 Register array_size, // As a smi.
575 Register result,
576 Register elements_array,
577 Register elements_array_end,
578 Register scratch,
579 bool fill_with_hole,
580 Label* gc_required) {
581 Label not_empty, allocated;
582
583 // Load the initial map from the array function.
584 __ movq(elements_array,
585 FieldOperand(array_function,
586 JSFunction::kPrototypeOrInitialMapOffset));
587
588 // Check whether an empty sized array is requested.
589 __ testq(array_size, array_size);
590 __ j(not_zero, &not_empty);
591
592 // If an empty array is requested allocate a small elements array anyway. This
593 // keeps the code below free of special casing for the empty array.
594 int size = JSArray::kSize + FixedArray::SizeFor(kPreallocatedArrayElements);
595 __ AllocateInNewSpace(size,
596 result,
597 elements_array_end,
598 scratch,
599 gc_required,
600 TAG_OBJECT);
601 __ jmp(&allocated);
602
603 // Allocate the JSArray object together with space for a FixedArray with the
604 // requested elements.
605 __ bind(&not_empty);
606 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
607 __ AllocateInNewSpace(JSArray::kSize + FixedArray::kHeaderSize,
608 times_half_pointer_size, // array_size is a smi.
609 array_size,
610 result,
611 elements_array_end,
612 scratch,
613 gc_required,
614 TAG_OBJECT);
615
616 // Allocated the JSArray. Now initialize the fields except for the elements
617 // array.
618 // result: JSObject
619 // elements_array: initial map
620 // elements_array_end: start of next object
621 // array_size: size of array (smi)
622 __ bind(&allocated);
623 __ movq(FieldOperand(result, JSObject::kMapOffset), elements_array);
624 __ Move(elements_array, Factory::empty_fixed_array());
625 __ movq(FieldOperand(result, JSArray::kPropertiesOffset), elements_array);
626 // Field JSArray::kElementsOffset is initialized later.
627 __ movq(FieldOperand(result, JSArray::kLengthOffset), array_size);
628
629 // Calculate the location of the elements array and set elements array member
630 // of the JSArray.
631 // result: JSObject
632 // elements_array_end: start of next object
633 // array_size: size of array (smi)
634 __ lea(elements_array, Operand(result, JSArray::kSize));
635 __ movq(FieldOperand(result, JSArray::kElementsOffset), elements_array);
636
637 // Initialize the fixed array. FixedArray length is not stored as a smi.
638 // result: JSObject
639 // elements_array: elements array
640 // elements_array_end: start of next object
641 // array_size: size of array (smi)
642 ASSERT(kSmiTag == 0);
643 __ SmiToInteger64(array_size, array_size);
644 __ Move(FieldOperand(elements_array, JSObject::kMapOffset),
645 Factory::fixed_array_map());
646 Label not_empty_2, fill_array;
647 __ testq(array_size, array_size);
648 __ j(not_zero, &not_empty_2);
649 // Length of the FixedArray is the number of pre-allocated elements even
650 // though the actual JSArray has length 0.
651 __ movq(FieldOperand(elements_array, Array::kLengthOffset),
652 Immediate(kPreallocatedArrayElements));
653 __ jmp(&fill_array);
654 __ bind(&not_empty_2);
655 // For non-empty JSArrays the length of the FixedArray and the JSArray is the
656 // same.
657 __ movq(FieldOperand(elements_array, Array::kLengthOffset), array_size);
658
659 // Fill the allocated FixedArray with the hole value if requested.
660 // result: JSObject
661 // elements_array: elements array
662 // elements_array_end: start of next object
663 __ bind(&fill_array);
664 if (fill_with_hole) {
665 Label loop, entry;
666 __ Move(scratch, Factory::the_hole_value());
667 __ lea(elements_array, Operand(elements_array,
668 FixedArray::kHeaderSize - kHeapObjectTag));
669 __ jmp(&entry);
670 __ bind(&loop);
671 __ movq(Operand(elements_array, 0), scratch);
672 __ addq(elements_array, Immediate(kPointerSize));
673 __ bind(&entry);
674 __ cmpq(elements_array, elements_array_end);
675 __ j(below, &loop);
676 }
677}
678
679
680// Create a new array for the built-in Array function. This function allocates
681// the JSArray object and the FixedArray elements array and initializes these.
682// If the Array cannot be constructed in native code the runtime is called. This
683// function assumes the following state:
684// rdi: constructor (built-in Array function)
685// rax: argc
686// rsp[0]: return address
687// rsp[8]: last argument
688// This function is used for both construct and normal calls of Array. The only
689// difference between handling a construct call and a normal call is that for a
690// construct call the constructor function in rdi needs to be preserved for
691// entering the generic code. In both cases argc in rax needs to be preserved.
692// Both registers are preserved by this code so no need to differentiate between
693// a construct call and a normal call.
694static void ArrayNativeCode(MacroAssembler* masm,
695 Label *call_generic_code) {
696 Label argc_one_or_more, argc_two_or_more;
697
698 // Check for array construction with zero arguments.
699 __ testq(rax, rax);
700 __ j(not_zero, &argc_one_or_more);
701
702 // Handle construction of an empty array.
703 AllocateEmptyJSArray(masm,
704 rdi,
705 rbx,
706 rcx,
707 rdx,
708 r8,
709 kPreallocatedArrayElements,
710 call_generic_code);
711 __ IncrementCounter(&Counters::array_function_native, 1);
712 __ movq(rax, rbx);
713 __ ret(kPointerSize);
714
715 // Check for one argument. Bail out if argument is not smi or if it is
716 // negative.
717 __ bind(&argc_one_or_more);
718 __ cmpq(rax, Immediate(1));
719 __ j(not_equal, &argc_two_or_more);
720 __ movq(rdx, Operand(rsp, kPointerSize)); // Get the argument from the stack.
721 Condition not_positive_smi = __ CheckNotPositiveSmi(rdx);
722 __ j(not_positive_smi, call_generic_code);
723
724 // Handle construction of an empty array of a certain size. Bail out if size
725 // is to large to actually allocate an elements array.
726 __ JumpIfSmiGreaterEqualsConstant(rdx,
727 JSObject::kInitialMaxFastElementArray,
728 call_generic_code);
729
730 // rax: argc
731 // rdx: array_size (smi)
732 // rdi: constructor
733 // esp[0]: return address
734 // esp[8]: argument
735 AllocateJSArray(masm,
736 rdi,
737 rdx,
738 rbx,
739 rcx,
740 r8,
741 r9,
742 true,
743 call_generic_code);
744 __ IncrementCounter(&Counters::array_function_native, 1);
745 __ movq(rax, rbx);
746 __ ret(2 * kPointerSize);
747
748 // Handle construction of an array from a list of arguments.
749 __ bind(&argc_two_or_more);
750 __ movq(rdx, rax);
751 __ Integer32ToSmi(rdx, rdx); // Convet argc to a smi.
752 // rax: argc
753 // rdx: array_size (smi)
754 // rdi: constructor
755 // esp[0] : return address
756 // esp[8] : last argument
757 AllocateJSArray(masm,
758 rdi,
759 rdx,
760 rbx,
761 rcx,
762 r8,
763 r9,
764 false,
765 call_generic_code);
766 __ IncrementCounter(&Counters::array_function_native, 1);
767
768 // rax: argc
769 // rbx: JSArray
770 // rcx: elements_array
771 // r8: elements_array_end (untagged)
772 // esp[0]: return address
773 // esp[8]: last argument
774
775 // Location of the last argument
776 __ lea(r9, Operand(rsp, kPointerSize));
777
778 // Location of the first array element (Parameter fill_with_holes to
779 // AllocateJSArrayis false, so the FixedArray is returned in rcx).
780 __ lea(rdx, Operand(rcx, FixedArray::kHeaderSize - kHeapObjectTag));
781
782 // rax: argc
783 // rbx: JSArray
784 // rdx: location of the first array element
785 // r9: location of the last argument
786 // esp[0]: return address
787 // esp[8]: last argument
788 Label loop, entry;
789 __ movq(rcx, rax);
790 __ jmp(&entry);
791 __ bind(&loop);
792 __ movq(kScratchRegister, Operand(r9, rcx, times_pointer_size, 0));
793 __ movq(Operand(rdx, 0), kScratchRegister);
794 __ addq(rdx, Immediate(kPointerSize));
795 __ bind(&entry);
796 __ decq(rcx);
797 __ j(greater_equal, &loop);
798
799 // Remove caller arguments from the stack and return.
800 // rax: argc
801 // rbx: JSArray
802 // esp[0]: return address
803 // esp[8]: last argument
804 __ pop(rcx);
805 __ lea(rsp, Operand(rsp, rax, times_pointer_size, 1 * kPointerSize));
806 __ push(rcx);
807 __ movq(rax, rbx);
808 __ ret(0);
809}
810
811
812void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
813 // ----------- S t a t e -------------
814 // -- rax : argc
815 // -- rsp[0] : return address
816 // -- rsp[8] : last argument
817 // -----------------------------------
818 Label generic_array_code;
819
820 // Get the Array function.
821 GenerateLoadArrayFunction(masm, rdi);
822
823 if (FLAG_debug_code) {
824 // Initial map for the builtin Array function shoud be a map.
825 __ movq(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
826 // Will both indicate a NULL and a Smi.
827 ASSERT(kSmiTag == 0);
828 Condition not_smi = __ CheckNotSmi(rbx);
829 __ Assert(not_smi, "Unexpected initial map for Array function");
830 __ CmpObjectType(rbx, MAP_TYPE, rcx);
831 __ Assert(equal, "Unexpected initial map for Array function");
832 }
833
834 // Run the native code for the Array function called as a normal function.
835 ArrayNativeCode(masm, &generic_array_code);
836
837 // Jump to the generic array code in case the specialized code cannot handle
838 // the construction.
839 __ bind(&generic_array_code);
840 Code* code = Builtins::builtin(Builtins::ArrayCodeGeneric);
841 Handle<Code> array_code(code);
842 __ Jump(array_code, RelocInfo::CODE_TARGET);
843}
844
845
846void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
847 // ----------- S t a t e -------------
848 // -- rax : argc
849 // -- rdi : constructor
850 // -- rsp[0] : return address
851 // -- rsp[8] : last argument
852 // -----------------------------------
853 Label generic_constructor;
854
855 if (FLAG_debug_code) {
856 // The array construct code is only set for the builtin Array function which
857 // does always have a map.
858 GenerateLoadArrayFunction(masm, rbx);
859 __ cmpq(rdi, rbx);
860 __ Assert(equal, "Unexpected Array function");
861 // Initial map for the builtin Array function should be a map.
862 __ movq(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
863 // Will both indicate a NULL and a Smi.
864 ASSERT(kSmiTag == 0);
865 Condition not_smi = __ CheckNotSmi(rbx);
866 __ Assert(not_smi, "Unexpected initial map for Array function");
867 __ CmpObjectType(rbx, MAP_TYPE, rcx);
868 __ Assert(equal, "Unexpected initial map for Array function");
869 }
870
871 // Run the native code for the Array function called as constructor.
872 ArrayNativeCode(masm, &generic_constructor);
873
874 // Jump to the generic construct code in case the specialized code cannot
875 // handle the construction.
876 __ bind(&generic_constructor);
877 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
878 Handle<Code> generic_construct_stub(code);
879 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
880}
881
882
883void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
884 // ----------- S t a t e -------------
885 // -- rax: number of arguments
886 // -- rdi: constructor function
887 // -----------------------------------
888
889 Label non_function_call;
890 // Check that function is not a smi.
891 __ JumpIfSmi(rdi, &non_function_call);
892 // Check that function is a JSFunction.
893 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
894 __ j(not_equal, &non_function_call);
895
896 // Jump to the function-specific construct stub.
897 __ movq(rbx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
898 __ movq(rbx, FieldOperand(rbx, SharedFunctionInfo::kConstructStubOffset));
899 __ lea(rbx, FieldOperand(rbx, Code::kHeaderSize));
900 __ jmp(rbx);
901
902 // edi: called object
903 // eax: number of arguments
904 __ bind(&non_function_call);
905
906 // Set expected number of arguments to zero (not changing eax).
907 __ movq(rbx, Immediate(0));
908 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
909 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
910 RelocInfo::CODE_TARGET);
911}
912
913
914void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
915 // Enter a construct frame.
916 __ EnterConstructFrame();
917
918 // Store a smi-tagged arguments count on the stack.
919 __ Integer32ToSmi(rax, rax);
920 __ push(rax);
921
922 // Push the function to invoke on the stack.
923 __ push(rdi);
924
925 // Try to allocate the object without transitioning into C code. If any of the
926 // preconditions is not met, the code bails out to the runtime call.
927 Label rt_call, allocated;
928 if (FLAG_inline_new) {
929 Label undo_allocation;
930
931#ifdef ENABLE_DEBUGGER_SUPPORT
932 ExternalReference debug_step_in_fp =
933 ExternalReference::debug_step_in_fp_address();
934 __ movq(kScratchRegister, debug_step_in_fp);
935 __ cmpq(Operand(kScratchRegister, 0), Immediate(0));
936 __ j(not_equal, &rt_call);
937#endif
938
939 // Verified that the constructor is a JSFunction.
940 // Load the initial map and verify that it is in fact a map.
941 // rdi: constructor
942 __ movq(rax, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
943 // Will both indicate a NULL and a Smi
944 ASSERT(kSmiTag == 0);
945 __ JumpIfSmi(rax, &rt_call);
946 // rdi: constructor
947 // rax: initial map (if proven valid below)
948 __ CmpObjectType(rax, MAP_TYPE, rbx);
949 __ j(not_equal, &rt_call);
950
951 // Check that the constructor is not constructing a JSFunction (see comments
952 // in Runtime_NewObject in runtime.cc). In which case the initial map's
953 // instance type would be JS_FUNCTION_TYPE.
954 // rdi: constructor
955 // rax: initial map
956 __ CmpInstanceType(rax, JS_FUNCTION_TYPE);
957 __ j(equal, &rt_call);
958
959 // Now allocate the JSObject on the heap.
960 __ movzxbq(rdi, FieldOperand(rax, Map::kInstanceSizeOffset));
961 __ shl(rdi, Immediate(kPointerSizeLog2));
962 // rdi: size of new object
963 __ AllocateInNewSpace(rdi,
964 rbx,
965 rdi,
966 no_reg,
967 &rt_call,
968 NO_ALLOCATION_FLAGS);
969 // Allocated the JSObject, now initialize the fields.
970 // rax: initial map
971 // rbx: JSObject (not HeapObject tagged - the actual address).
972 // rdi: start of next object
973 __ movq(Operand(rbx, JSObject::kMapOffset), rax);
974 __ LoadRoot(rcx, Heap::kEmptyFixedArrayRootIndex);
975 __ movq(Operand(rbx, JSObject::kPropertiesOffset), rcx);
976 __ movq(Operand(rbx, JSObject::kElementsOffset), rcx);
977 // Set extra fields in the newly allocated object.
978 // rax: initial map
979 // rbx: JSObject
980 // rdi: start of next object
981 { Label loop, entry;
982 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
983 __ lea(rcx, Operand(rbx, JSObject::kHeaderSize));
984 __ jmp(&entry);
985 __ bind(&loop);
986 __ movq(Operand(rcx, 0), rdx);
987 __ addq(rcx, Immediate(kPointerSize));
988 __ bind(&entry);
989 __ cmpq(rcx, rdi);
990 __ j(less, &loop);
991 }
992
993 // Add the object tag to make the JSObject real, so that we can continue and
994 // jump into the continuation code at any time from now on. Any failures
995 // need to undo the allocation, so that the heap is in a consistent state
996 // and verifiable.
997 // rax: initial map
998 // rbx: JSObject
999 // rdi: start of next object
1000 __ or_(rbx, Immediate(kHeapObjectTag));
1001
1002 // Check if a non-empty properties array is needed.
1003 // Allocate and initialize a FixedArray if it is.
1004 // rax: initial map
1005 // rbx: JSObject
1006 // rdi: start of next object
1007 // Calculate total properties described map.
1008 __ movzxbq(rdx, FieldOperand(rax, Map::kUnusedPropertyFieldsOffset));
1009 __ movzxbq(rcx, FieldOperand(rax, Map::kPreAllocatedPropertyFieldsOffset));
1010 __ addq(rdx, rcx);
1011 // Calculate unused properties past the end of the in-object properties.
1012 __ movzxbq(rcx, FieldOperand(rax, Map::kInObjectPropertiesOffset));
1013 __ subq(rdx, rcx);
1014 // Done if no extra properties are to be allocated.
1015 __ j(zero, &allocated);
1016 __ Assert(positive, "Property allocation count failed.");
1017
1018 // Scale the number of elements by pointer size and add the header for
1019 // FixedArrays to the start of the next object calculation from above.
1020 // rbx: JSObject
1021 // rdi: start of next object (will be start of FixedArray)
1022 // rdx: number of elements in properties array
1023 __ AllocateInNewSpace(FixedArray::kHeaderSize,
1024 times_pointer_size,
1025 rdx,
1026 rdi,
1027 rax,
1028 no_reg,
1029 &undo_allocation,
1030 RESULT_CONTAINS_TOP);
1031
1032 // Initialize the FixedArray.
1033 // rbx: JSObject
1034 // rdi: FixedArray
1035 // rdx: number of elements
1036 // rax: start of next object
1037 __ LoadRoot(rcx, Heap::kFixedArrayMapRootIndex);
1038 __ movq(Operand(rdi, JSObject::kMapOffset), rcx); // setup the map
1039 __ movl(Operand(rdi, FixedArray::kLengthOffset), rdx); // and length
1040
1041 // Initialize the fields to undefined.
1042 // rbx: JSObject
1043 // rdi: FixedArray
1044 // rax: start of next object
1045 // rdx: number of elements
1046 { Label loop, entry;
1047 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
1048 __ lea(rcx, Operand(rdi, FixedArray::kHeaderSize));
1049 __ jmp(&entry);
1050 __ bind(&loop);
1051 __ movq(Operand(rcx, 0), rdx);
1052 __ addq(rcx, Immediate(kPointerSize));
1053 __ bind(&entry);
1054 __ cmpq(rcx, rax);
1055 __ j(below, &loop);
1056 }
1057
1058 // Store the initialized FixedArray into the properties field of
1059 // the JSObject
1060 // rbx: JSObject
1061 // rdi: FixedArray
1062 __ or_(rdi, Immediate(kHeapObjectTag)); // add the heap tag
1063 __ movq(FieldOperand(rbx, JSObject::kPropertiesOffset), rdi);
1064
1065
1066 // Continue with JSObject being successfully allocated
1067 // rbx: JSObject
1068 __ jmp(&allocated);
1069
1070 // Undo the setting of the new top so that the heap is verifiable. For
1071 // example, the map's unused properties potentially do not match the
1072 // allocated objects unused properties.
1073 // rbx: JSObject (previous new top)
1074 __ bind(&undo_allocation);
1075 __ UndoAllocationInNewSpace(rbx);
1076 }
1077
1078 // Allocate the new receiver object using the runtime call.
1079 // rdi: function (constructor)
1080 __ bind(&rt_call);
1081 // Must restore rdi (constructor) before calling runtime.
1082 __ movq(rdi, Operand(rsp, 0));
1083 __ push(rdi);
1084 __ CallRuntime(Runtime::kNewObject, 1);
1085 __ movq(rbx, rax); // store result in rbx
1086
1087 // New object allocated.
1088 // rbx: newly allocated object
1089 __ bind(&allocated);
1090 // Retrieve the function from the stack.
1091 __ pop(rdi);
1092
1093 // Retrieve smi-tagged arguments count from the stack.
1094 __ movq(rax, Operand(rsp, 0));
1095 __ SmiToInteger32(rax, rax);
1096
1097 // Push the allocated receiver to the stack. We need two copies
1098 // because we may have to return the original one and the calling
1099 // conventions dictate that the called function pops the receiver.
1100 __ push(rbx);
1101 __ push(rbx);
1102
1103 // Setup pointer to last argument.
1104 __ lea(rbx, Operand(rbp, StandardFrameConstants::kCallerSPOffset));
1105
1106 // Copy arguments and receiver to the expression stack.
1107 Label loop, entry;
1108 __ movq(rcx, rax);
1109 __ jmp(&entry);
1110 __ bind(&loop);
1111 __ push(Operand(rbx, rcx, times_pointer_size, 0));
1112 __ bind(&entry);
1113 __ decq(rcx);
1114 __ j(greater_equal, &loop);
1115
1116 // Call the function.
1117 ParameterCount actual(rax);
1118 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
1119
1120 // Restore context from the frame.
1121 __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
1122
1123 // If the result is an object (in the ECMA sense), we should get rid
1124 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
1125 // on page 74.
1126 Label use_receiver, exit;
1127 // If the result is a smi, it is *not* an object in the ECMA sense.
1128 __ JumpIfSmi(rax, &use_receiver);
1129
1130 // If the type of the result (stored in its map) is less than
1131 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
1132 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
1133 __ j(above_equal, &exit);
1134
1135 // Throw away the result of the constructor invocation and use the
1136 // on-stack receiver as the result.
1137 __ bind(&use_receiver);
1138 __ movq(rax, Operand(rsp, 0));
1139
1140 // Restore the arguments count and leave the construct frame.
1141 __ bind(&exit);
1142 __ movq(rbx, Operand(rsp, kPointerSize)); // get arguments count
1143 __ LeaveConstructFrame();
1144
1145 // Remove caller arguments from the stack and return.
1146 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
1147 // TODO(smi): Find a way to abstract indexing by a smi.
1148 __ pop(rcx);
1149 // 1 * kPointerSize is offset of receiver.
1150 __ lea(rsp, Operand(rsp, rbx, times_half_pointer_size, 1 * kPointerSize));
1151 __ push(rcx);
1152 __ IncrementCounter(&Counters::constructed_objects, 1);
1153 __ ret(0);
1154}
1155
1156
1157static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
1158 bool is_construct) {
1159 // Expects five C++ function parameters.
1160 // - Address entry (ignored)
1161 // - JSFunction* function (
1162 // - Object* receiver
1163 // - int argc
1164 // - Object*** argv
1165 // (see Handle::Invoke in execution.cc).
1166
1167 // Platform specific argument handling. After this, the stack contains
1168 // an internal frame and the pushed function and receiver, and
1169 // register rax and rbx holds the argument count and argument array,
1170 // while rdi holds the function pointer and rsi the context.
1171#ifdef _WIN64
1172 // MSVC parameters in:
1173 // rcx : entry (ignored)
1174 // rdx : function
1175 // r8 : receiver
1176 // r9 : argc
1177 // [rsp+0x20] : argv
1178
1179 // Clear the context before we push it when entering the JS frame.
1180 __ xor_(rsi, rsi);
1181 __ EnterInternalFrame();
1182
1183 // Load the function context into rsi.
1184 __ movq(rsi, FieldOperand(rdx, JSFunction::kContextOffset));
1185
1186 // Push the function and the receiver onto the stack.
1187 __ push(rdx);
1188 __ push(r8);
1189
1190 // Load the number of arguments and setup pointer to the arguments.
1191 __ movq(rax, r9);
1192 // Load the previous frame pointer to access C argument on stack
1193 __ movq(kScratchRegister, Operand(rbp, 0));
1194 __ movq(rbx, Operand(kScratchRegister, EntryFrameConstants::kArgvOffset));
1195 // Load the function pointer into rdi.
1196 __ movq(rdi, rdx);
1197#else // !defined(_WIN64)
1198 // GCC parameters in:
1199 // rdi : entry (ignored)
1200 // rsi : function
1201 // rdx : receiver
1202 // rcx : argc
1203 // r8 : argv
1204
1205 __ movq(rdi, rsi);
1206 // rdi : function
1207
1208 // Clear the context before we push it when entering the JS frame.
1209 __ xor_(rsi, rsi);
1210 // Enter an internal frame.
1211 __ EnterInternalFrame();
1212
1213 // Push the function and receiver and setup the context.
1214 __ push(rdi);
1215 __ push(rdx);
1216 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
1217
1218 // Load the number of arguments and setup pointer to the arguments.
1219 __ movq(rax, rcx);
1220 __ movq(rbx, r8);
1221#endif // _WIN64
1222
1223 // Set up the roots register.
1224 ExternalReference roots_address = ExternalReference::roots_address();
1225 __ movq(r13, roots_address);
1226
1227 // Current stack contents:
1228 // [rsp + 2 * kPointerSize ... ]: Internal frame
1229 // [rsp + kPointerSize] : function
1230 // [rsp] : receiver
1231 // Current register contents:
1232 // rax : argc
1233 // rbx : argv
1234 // rsi : context
1235 // rdi : function
1236
1237 // Copy arguments to the stack in a loop.
1238 // Register rbx points to array of pointers to handle locations.
1239 // Push the values of these handles.
1240 Label loop, entry;
1241 __ xor_(rcx, rcx); // Set loop variable to 0.
1242 __ jmp(&entry);
1243 __ bind(&loop);
1244 __ movq(kScratchRegister, Operand(rbx, rcx, times_pointer_size, 0));
1245 __ push(Operand(kScratchRegister, 0)); // dereference handle
1246 __ addq(rcx, Immediate(1));
1247 __ bind(&entry);
1248 __ cmpq(rcx, rax);
1249 __ j(not_equal, &loop);
1250
1251 // Invoke the code.
1252 if (is_construct) {
1253 // Expects rdi to hold function pointer.
1254 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
1255 RelocInfo::CODE_TARGET);
1256 } else {
1257 ParameterCount actual(rax);
1258 // Function must be in rdi.
1259 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
1260 }
1261
1262 // Exit the JS frame. Notice that this also removes the empty
1263 // context and the function left on the stack by the code
1264 // invocation.
1265 __ LeaveInternalFrame();
1266 // TODO(X64): Is argument correct? Is there a receiver to remove?
1267 __ ret(1 * kPointerSize); // remove receiver
1268}
1269
1270
1271void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
1272 Generate_JSEntryTrampolineHelper(masm, false);
1273}
1274
1275
1276void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
1277 Generate_JSEntryTrampolineHelper(masm, true);
1278}
1279
1280} } // namespace v8::internal