blob: f444d2cf85f5ca2d56dd14e531d7dd0fabfe85c9 [file] [log] [blame]
ager@chromium.org5ec48922009-05-05 07:25:34 +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
kasperl@chromium.org71affb52009-05-26 05:44:31 +000028#include "v8.h"
29#include "codegen-inl.h"
ager@chromium.orgeadaf222009-06-16 09:43:10 +000030#include "macro-assembler.h"
kasperl@chromium.org71affb52009-05-26 05:44:31 +000031
32namespace v8 {
33namespace internal {
34
ager@chromium.orgeadaf222009-06-16 09:43:10 +000035#define __ ACCESS_MASM(masm)
36
ager@chromium.org3e875802009-06-29 08:26:34 +000037void Builtins::Generate_Adaptor(MacroAssembler* masm, CFunctionId id) {
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000038 // TODO(428): Don't pass the function in a static variable.
ager@chromium.org3e875802009-06-29 08:26:34 +000039 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
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000044 // rax, but JumpToRuntime expects rax to contain the number of
ager@chromium.org3e875802009-06-29 08:26:34 +000045 // arguments including the receiver.
46 __ incq(rax);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +000047 __ JumpToRuntime(ExternalReference(id), 1);
kasperl@chromium.org71affb52009-05-26 05:44:31 +000048}
49
ager@chromium.org3e875802009-06-29 08:26:34 +000050
ager@chromium.orgeadaf222009-06-16 09:43:10 +000051static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
52 __ push(rbp);
53 __ movq(rbp, rsp);
54
55 // Store the arguments adaptor context sentinel.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000056 __ Push(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
ager@chromium.orgeadaf222009-06-16 09:43:10 +000057
58 // Push the function on the stack.
59 __ push(rdi);
60
61 // Preserve the number of arguments on the stack. Must preserve both
ager@chromium.org3e875802009-06-29 08:26:34 +000062 // rax and rbx because these registers are used when copying the
ager@chromium.orgeadaf222009-06-16 09:43:10 +000063 // arguments and the receiver.
ager@chromium.org4af710e2009-09-15 12:20:11 +000064 __ Integer32ToSmi(rcx, rax);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000065 __ push(rcx);
kasperl@chromium.org71affb52009-05-26 05:44:31 +000066}
67
ager@chromium.orgeadaf222009-06-16 09:43:10 +000068
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.
ager@chromium.orgeadaf222009-06-16 09:43:10 +000078 __ pop(rcx);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +000079 SmiIndex index = masm->SmiToIndex(rbx, rbx, kPointerSizeLog2);
80 __ lea(rsp, Operand(rsp, index.reg, index.scale, 1 * kPointerSize));
ager@chromium.orgeadaf222009-06-16 09:43:10 +000081 __ push(rcx);
82}
83
84
85void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
86 // ----------- S t a t e -------------
87 // -- rax : actual number of arguments
88 // -- rbx : expected number of arguments
89 // -- rdx : code entry to call
90 // -----------------------------------
91
92 Label invoke, dont_adapt_arguments;
93 __ IncrementCounter(&Counters::arguments_adaptors, 1);
94
95 Label enough, too_few;
96 __ cmpq(rax, rbx);
97 __ j(less, &too_few);
98 __ cmpq(rbx, Immediate(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
99 __ j(equal, &dont_adapt_arguments);
100
101 { // Enough parameters: Actual >= expected.
102 __ bind(&enough);
103 EnterArgumentsAdaptorFrame(masm);
104
105 // Copy receiver and all expected arguments.
106 const int offset = StandardFrameConstants::kCallerSPOffset;
ager@chromium.org3e875802009-06-29 08:26:34 +0000107 __ lea(rax, Operand(rbp, rax, times_pointer_size, offset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000108 __ movq(rcx, Immediate(-1)); // account for receiver
109
110 Label copy;
111 __ bind(&copy);
112 __ incq(rcx);
113 __ push(Operand(rax, 0));
114 __ subq(rax, Immediate(kPointerSize));
115 __ cmpq(rcx, rbx);
116 __ j(less, &copy);
117 __ jmp(&invoke);
118 }
119
120 { // Too few parameters: Actual < expected.
121 __ bind(&too_few);
122 EnterArgumentsAdaptorFrame(masm);
123
124 // Copy receiver and all actual arguments.
125 const int offset = StandardFrameConstants::kCallerSPOffset;
ager@chromium.org3e875802009-06-29 08:26:34 +0000126 __ lea(rdi, Operand(rbp, rax, times_pointer_size, offset));
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000127 __ movq(rcx, Immediate(-1)); // account for receiver
128
129 Label copy;
130 __ bind(&copy);
131 __ incq(rcx);
132 __ push(Operand(rdi, 0));
133 __ subq(rdi, Immediate(kPointerSize));
134 __ cmpq(rcx, rax);
135 __ j(less, &copy);
136
137 // Fill remaining expected arguments with undefined values.
138 Label fill;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000139 __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex);
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000140 __ bind(&fill);
141 __ incq(rcx);
142 __ push(kScratchRegister);
143 __ cmpq(rcx, rbx);
144 __ j(less, &fill);
145
146 // Restore function pointer.
147 __ movq(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
148 }
149
150 // Call the entry point.
151 __ bind(&invoke);
152 __ call(rdx);
153
154 // Leave frame and return.
155 LeaveArgumentsAdaptorFrame(masm);
156 __ ret(0);
157
158 // -------------------------------------------
159 // Dont adapt arguments.
160 // -------------------------------------------
161 __ bind(&dont_adapt_arguments);
162 __ jmp(rdx);
163}
164
165
ager@chromium.org3e875802009-06-29 08:26:34 +0000166void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
167 // Stack Layout:
168 // rsp: return address
169 // +1: Argument n
170 // +2: Argument n-1
171 // ...
172 // +n: Argument 1 = receiver
173 // +n+1: Argument 0 = function to call
174 //
175 // rax contains the number of arguments, n, not counting the function.
176 //
177 // 1. Make sure we have at least one argument.
178 { Label done;
179 __ testq(rax, rax);
180 __ j(not_zero, &done);
181 __ pop(rbx);
182 __ Push(Factory::undefined_value());
183 __ push(rbx);
184 __ incq(rax);
185 __ bind(&done);
186 }
187
188 // 2. Get the function to call from the stack.
189 { Label done, non_function, function;
190 // The function to call is at position n+1 on the stack.
191 __ movq(rdi, Operand(rsp, rax, times_pointer_size, +1 * kPointerSize));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000192 __ JumpIfSmi(rdi, &non_function);
ager@chromium.org3e875802009-06-29 08:26:34 +0000193 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
194 __ j(equal, &function);
195
196 // Non-function called: Clear the function to force exception.
197 __ bind(&non_function);
198 __ xor_(rdi, rdi);
199 __ jmp(&done);
200
201 // Function called: Change context eagerly to get the right global object.
202 __ bind(&function);
203 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
204
205 __ bind(&done);
206 }
207
208 // 3. Make sure first argument is an object; convert if necessary.
209 { Label call_to_object, use_global_receiver, patch_receiver, done;
210 __ movq(rbx, Operand(rsp, rax, times_pointer_size, 0));
211
ager@chromium.org4af710e2009-09-15 12:20:11 +0000212 __ JumpIfSmi(rbx, &call_to_object);
ager@chromium.org3e875802009-06-29 08:26:34 +0000213
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000214 __ CompareRoot(rbx, Heap::kNullValueRootIndex);
ager@chromium.org3e875802009-06-29 08:26:34 +0000215 __ j(equal, &use_global_receiver);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000216 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
ager@chromium.org3e875802009-06-29 08:26:34 +0000217 __ j(equal, &use_global_receiver);
218
219 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, rcx);
220 __ j(below, &call_to_object);
221 __ CmpInstanceType(rcx, LAST_JS_OBJECT_TYPE);
222 __ j(below_equal, &done);
223
224 __ bind(&call_to_object);
225 __ EnterInternalFrame(); // preserves rax, rbx, rdi
226
227 // Store the arguments count on the stack (smi tagged).
ager@chromium.org4af710e2009-09-15 12:20:11 +0000228 __ Integer32ToSmi(rax, rax);
ager@chromium.org3e875802009-06-29 08:26:34 +0000229 __ push(rax);
230
231 __ push(rdi); // save edi across the call
232 __ push(rbx);
233 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
234 __ movq(rbx, rax);
235 __ pop(rdi); // restore edi after the call
236
237 // Get the arguments count and untag it.
238 __ pop(rax);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000239 __ SmiToInteger32(rax, rax);
ager@chromium.org3e875802009-06-29 08:26:34 +0000240
241 __ LeaveInternalFrame();
242 __ jmp(&patch_receiver);
243
244 // Use the global receiver object from the called function as the receiver.
245 __ bind(&use_global_receiver);
246 const int kGlobalIndex =
247 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
248 __ movq(rbx, FieldOperand(rsi, kGlobalIndex));
ager@chromium.org3811b432009-10-28 14:53:37 +0000249 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalContextOffset));
250 __ movq(rbx, FieldOperand(rbx, kGlobalIndex));
ager@chromium.org3e875802009-06-29 08:26:34 +0000251 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
252
253 __ bind(&patch_receiver);
254 __ movq(Operand(rsp, rax, times_pointer_size, 0), rbx);
255
256 __ bind(&done);
257 }
258
259 // 4. Shift stuff one slot down the stack.
260 { Label loop;
261 __ lea(rcx, Operand(rax, +1)); // +1 ~ copy receiver too
262 __ bind(&loop);
263 __ movq(rbx, Operand(rsp, rcx, times_pointer_size, 0));
264 __ movq(Operand(rsp, rcx, times_pointer_size, 1 * kPointerSize), rbx);
265 __ decq(rcx);
266 __ j(not_zero, &loop);
267 }
268
269 // 5. Remove TOS (copy of last arguments), but keep return address.
270 __ pop(rbx);
271 __ pop(rcx);
272 __ push(rbx);
273 __ decq(rax);
274
275 // 6. Check that function really was a function and get the code to
276 // call from the function and check that the number of expected
277 // arguments matches what we're providing.
278 { Label invoke, trampoline;
279 __ testq(rdi, rdi);
280 __ j(not_zero, &invoke);
281 __ xor_(rbx, rbx);
282 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
283 __ bind(&trampoline);
284 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
285 RelocInfo::CODE_TARGET);
286
287 __ bind(&invoke);
288 __ movq(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
289 __ movsxlq(rbx,
290 FieldOperand(rdx, SharedFunctionInfo::kFormalParameterCountOffset));
291 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
292 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
293 __ cmpq(rax, rbx);
294 __ j(not_equal, &trampoline);
295 }
296
297 // 7. Jump (tail-call) to the code in register edx without checking arguments.
298 ParameterCount expected(0);
299 __ InvokeCode(rdx, expected, expected, JUMP_FUNCTION);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000300}
301
ager@chromium.org3e875802009-06-29 08:26:34 +0000302
303void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
304 // Stack at entry:
305 // rsp: return address
306 // rsp+8: arguments
307 // rsp+16: receiver ("this")
308 // rsp+24: function
309 __ EnterInternalFrame();
310 // Stack frame:
311 // rbp: Old base pointer
312 // rbp[1]: return address
313 // rbp[2]: function arguments
314 // rbp[3]: receiver
315 // rbp[4]: function
316 static const int kArgumentsOffset = 2 * kPointerSize;
317 static const int kReceiverOffset = 3 * kPointerSize;
318 static const int kFunctionOffset = 4 * kPointerSize;
319 __ push(Operand(rbp, kFunctionOffset));
320 __ push(Operand(rbp, kArgumentsOffset));
321 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
322
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000323 // Check the stack for overflow. We are not trying need to catch
324 // interruptions (e.g. debug break and preemption) here, so the "real stack
325 // limit" is checked.
ager@chromium.org3811b432009-10-28 14:53:37 +0000326 Label okay;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000327 __ LoadRoot(kScratchRegister, Heap::kRealStackLimitRootIndex);
328 __ movq(rcx, rsp);
329 // Make rcx the space we have left. The stack might already be overflowed
330 // here which will cause rcx to become negative.
331 __ subq(rcx, kScratchRegister);
ager@chromium.org3811b432009-10-28 14:53:37 +0000332 // Make rdx the space we need for the array when it is unrolled onto the
333 // stack.
334 __ PositiveSmiTimesPowerOfTwoToInteger64(rdx, rax, kPointerSizeLog2);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000335 // Check if the arguments will overflow the stack.
ager@chromium.org3811b432009-10-28 14:53:37 +0000336 __ cmpq(rcx, rdx);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000337 __ j(greater, &okay); // Signed comparison.
ager@chromium.org3e875802009-06-29 08:26:34 +0000338
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000339 // Out of stack space.
ager@chromium.org3811b432009-10-28 14:53:37 +0000340 __ push(Operand(rbp, kFunctionOffset));
341 __ push(rax);
342 __ InvokeBuiltin(Builtins::APPLY_OVERFLOW, CALL_FUNCTION);
343 __ bind(&okay);
344 // End of stack check.
ager@chromium.org3e875802009-06-29 08:26:34 +0000345
346 // Push current index and limit.
347 const int kLimitOffset =
348 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
349 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
350 __ push(rax); // limit
351 __ push(Immediate(0)); // index
352
353 // Change context eagerly to get the right global object if
354 // necessary.
355 __ movq(rdi, Operand(rbp, kFunctionOffset));
356 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
357
358 // Compute the receiver.
359 Label call_to_object, use_global_receiver, push_receiver;
360 __ movq(rbx, Operand(rbp, kReceiverOffset));
ager@chromium.org4af710e2009-09-15 12:20:11 +0000361 __ JumpIfSmi(rbx, &call_to_object);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000362 __ CompareRoot(rbx, Heap::kNullValueRootIndex);
ager@chromium.org3e875802009-06-29 08:26:34 +0000363 __ j(equal, &use_global_receiver);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000364 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
ager@chromium.org3e875802009-06-29 08:26:34 +0000365 __ j(equal, &use_global_receiver);
366
367 // If given receiver is already a JavaScript object then there's no
368 // reason for converting it.
369 __ CmpObjectType(rbx, FIRST_JS_OBJECT_TYPE, rcx);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000370 __ j(below, &call_to_object);
ager@chromium.org3e875802009-06-29 08:26:34 +0000371 __ CmpInstanceType(rcx, LAST_JS_OBJECT_TYPE);
kasperl@chromium.orge959c182009-07-27 08:59:04 +0000372 __ j(below_equal, &push_receiver);
ager@chromium.org3e875802009-06-29 08:26:34 +0000373
374 // Convert the receiver to an object.
375 __ bind(&call_to_object);
376 __ push(rbx);
377 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
378 __ movq(rbx, rax);
379 __ jmp(&push_receiver);
380
381 // Use the current global receiver object as the receiver.
382 __ bind(&use_global_receiver);
383 const int kGlobalOffset =
384 Context::kHeaderSize + Context::GLOBAL_INDEX * kPointerSize;
385 __ movq(rbx, FieldOperand(rsi, kGlobalOffset));
ager@chromium.org3811b432009-10-28 14:53:37 +0000386 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalContextOffset));
387 __ movq(rbx, FieldOperand(rbx, kGlobalOffset));
ager@chromium.org3e875802009-06-29 08:26:34 +0000388 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
389
390 // Push the receiver.
391 __ bind(&push_receiver);
392 __ push(rbx);
393
394 // Copy all arguments from the array to the stack.
395 Label entry, loop;
396 __ movq(rax, Operand(rbp, kIndexOffset));
397 __ jmp(&entry);
398 __ bind(&loop);
399 __ movq(rcx, Operand(rbp, kArgumentsOffset)); // load arguments
400 __ push(rcx);
401 __ push(rax);
402
403 // Use inline caching to speed up access to arguments.
404 Handle<Code> ic(Builtins::builtin(Builtins::KeyedLoadIC_Initialize));
405 __ Call(ic, RelocInfo::CODE_TARGET);
406 // It is important that we do not have a test instruction after the
407 // call. A test instruction after the call is used to indicate that
408 // we have generated an inline version of the keyed load. In this
409 // case, we know that we are not generating a test instruction next.
410
411 // Remove IC arguments from the stack and push the nth argument.
412 __ addq(rsp, Immediate(2 * kPointerSize));
413 __ push(rax);
414
415 // Update the index on the stack and in register rax.
416 __ movq(rax, Operand(rbp, kIndexOffset));
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000417 __ SmiAddConstant(rax, rax, Smi::FromInt(1));
ager@chromium.org3e875802009-06-29 08:26:34 +0000418 __ movq(Operand(rbp, kIndexOffset), rax);
419
420 __ bind(&entry);
421 __ cmpq(rax, Operand(rbp, kLimitOffset));
422 __ j(not_equal, &loop);
423
424 // Invoke the function.
425 ParameterCount actual(rax);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000426 __ SmiToInteger32(rax, rax);
ager@chromium.org3e875802009-06-29 08:26:34 +0000427 __ movq(rdi, Operand(rbp, kFunctionOffset));
428 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
429
430 __ LeaveInternalFrame();
431 __ ret(3 * kPointerSize); // remove function, receiver, and arguments
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000432}
433
ager@chromium.org3e875802009-06-29 08:26:34 +0000434
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000435// Load the built-in Array function from the current context.
436static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
437 // Load the global context.
438 __ movq(result, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
439 __ movq(result, FieldOperand(result, GlobalObject::kGlobalContextOffset));
440 // Load the Array function from the global context.
441 __ movq(result,
442 Operand(result, Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
443}
444
445
446// Number of empty elements to allocate for an empty array.
447static const int kPreallocatedArrayElements = 4;
448
449
450// Allocate an empty JSArray. The allocated array is put into the result
451// register. If the parameter initial_capacity is larger than zero an elements
452// backing store is allocated with this size and filled with the hole values.
453// Otherwise the elements backing store is set to the empty FixedArray.
454static void AllocateEmptyJSArray(MacroAssembler* masm,
455 Register array_function,
456 Register result,
457 Register scratch1,
458 Register scratch2,
459 Register scratch3,
460 int initial_capacity,
461 Label* gc_required) {
462 ASSERT(initial_capacity >= 0);
463
464 // Load the initial map from the array function.
465 __ movq(scratch1, FieldOperand(array_function,
466 JSFunction::kPrototypeOrInitialMapOffset));
467
468 // Allocate the JSArray object together with space for a fixed array with the
469 // requested elements.
470 int size = JSArray::kSize;
471 if (initial_capacity > 0) {
472 size += FixedArray::SizeFor(initial_capacity);
473 }
474 __ AllocateInNewSpace(size,
475 result,
476 scratch2,
477 scratch3,
478 gc_required,
479 TAG_OBJECT);
480
481 // Allocated the JSArray. Now initialize the fields except for the elements
482 // array.
483 // result: JSObject
484 // scratch1: initial map
485 // scratch2: start of next object
486 __ movq(FieldOperand(result, JSObject::kMapOffset), scratch1);
487 __ Move(FieldOperand(result, JSArray::kPropertiesOffset),
488 Factory::empty_fixed_array());
489 // Field JSArray::kElementsOffset is initialized later.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000490 __ Move(FieldOperand(result, JSArray::kLengthOffset), Smi::FromInt(0));
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000491
492 // If no storage is requested for the elements array just set the empty
493 // fixed array.
494 if (initial_capacity == 0) {
495 __ Move(FieldOperand(result, JSArray::kElementsOffset),
496 Factory::empty_fixed_array());
497 return;
498 }
499
500 // Calculate the location of the elements array and set elements array member
501 // of the JSArray.
502 // result: JSObject
503 // scratch2: start of next object
504 __ lea(scratch1, Operand(result, JSArray::kSize));
505 __ movq(FieldOperand(result, JSArray::kElementsOffset), scratch1);
506
507 // Initialize the FixedArray and fill it with holes. FixedArray length is not
508 // stored as a smi.
509 // result: JSObject
510 // scratch1: elements array
511 // scratch2: start of next object
512 __ Move(FieldOperand(scratch1, JSObject::kMapOffset),
513 Factory::fixed_array_map());
514 __ movq(FieldOperand(scratch1, Array::kLengthOffset),
515 Immediate(initial_capacity));
516
517 // Fill the FixedArray with the hole value. Inline the code if short.
518 // Reconsider loop unfolding if kPreallocatedArrayElements gets changed.
519 static const int kLoopUnfoldLimit = 4;
520 ASSERT(kPreallocatedArrayElements <= kLoopUnfoldLimit);
521 __ Move(scratch3, Factory::the_hole_value());
522 if (initial_capacity <= kLoopUnfoldLimit) {
523 // Use a scratch register here to have only one reloc info when unfolding
524 // the loop.
525 for (int i = 0; i < initial_capacity; i++) {
526 __ movq(FieldOperand(scratch1,
527 FixedArray::kHeaderSize + i * kPointerSize),
528 scratch3);
529 }
530 } else {
531 Label loop, entry;
532 __ jmp(&entry);
533 __ bind(&loop);
534 __ movq(Operand(scratch1, 0), scratch3);
535 __ addq(scratch1, Immediate(kPointerSize));
536 __ bind(&entry);
537 __ cmpq(scratch1, scratch2);
538 __ j(below, &loop);
539 }
540}
541
542
543// Allocate a JSArray with the number of elements stored in a register. The
544// register array_function holds the built-in Array function and the register
545// array_size holds the size of the array as a smi. The allocated array is put
546// into the result register and beginning and end of the FixedArray elements
547// storage is put into registers elements_array and elements_array_end (see
548// below for when that is not the case). If the parameter fill_with_holes is
549// true the allocated elements backing store is filled with the hole values
550// otherwise it is left uninitialized. When the backing store is filled the
551// register elements_array is scratched.
552static void AllocateJSArray(MacroAssembler* masm,
553 Register array_function, // Array function.
554 Register array_size, // As a smi.
555 Register result,
556 Register elements_array,
557 Register elements_array_end,
558 Register scratch,
559 bool fill_with_hole,
560 Label* gc_required) {
561 Label not_empty, allocated;
562
563 // Load the initial map from the array function.
564 __ movq(elements_array,
565 FieldOperand(array_function,
566 JSFunction::kPrototypeOrInitialMapOffset));
567
568 // Check whether an empty sized array is requested.
569 __ testq(array_size, array_size);
570 __ j(not_zero, &not_empty);
571
572 // If an empty array is requested allocate a small elements array anyway. This
573 // keeps the code below free of special casing for the empty array.
574 int size = JSArray::kSize + FixedArray::SizeFor(kPreallocatedArrayElements);
575 __ AllocateInNewSpace(size,
576 result,
577 elements_array_end,
578 scratch,
579 gc_required,
580 TAG_OBJECT);
581 __ jmp(&allocated);
582
583 // Allocate the JSArray object together with space for a FixedArray with the
584 // requested elements.
585 __ bind(&not_empty);
586 ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
587 __ AllocateInNewSpace(JSArray::kSize + FixedArray::kHeaderSize,
588 times_half_pointer_size, // array_size is a smi.
589 array_size,
590 result,
591 elements_array_end,
592 scratch,
593 gc_required,
594 TAG_OBJECT);
595
596 // Allocated the JSArray. Now initialize the fields except for the elements
597 // array.
598 // result: JSObject
599 // elements_array: initial map
600 // elements_array_end: start of next object
601 // array_size: size of array (smi)
602 __ bind(&allocated);
603 __ movq(FieldOperand(result, JSObject::kMapOffset), elements_array);
604 __ Move(elements_array, Factory::empty_fixed_array());
605 __ movq(FieldOperand(result, JSArray::kPropertiesOffset), elements_array);
606 // Field JSArray::kElementsOffset is initialized later.
607 __ movq(FieldOperand(result, JSArray::kLengthOffset), array_size);
608
609 // Calculate the location of the elements array and set elements array member
610 // of the JSArray.
611 // result: JSObject
612 // elements_array_end: start of next object
613 // array_size: size of array (smi)
614 __ lea(elements_array, Operand(result, JSArray::kSize));
615 __ movq(FieldOperand(result, JSArray::kElementsOffset), elements_array);
616
617 // Initialize the fixed array. FixedArray length is not stored as a smi.
618 // result: JSObject
619 // elements_array: elements array
620 // elements_array_end: start of next object
621 // array_size: size of array (smi)
622 ASSERT(kSmiTag == 0);
623 __ SmiToInteger64(array_size, array_size);
624 __ Move(FieldOperand(elements_array, JSObject::kMapOffset),
625 Factory::fixed_array_map());
626 Label not_empty_2, fill_array;
627 __ testq(array_size, array_size);
628 __ j(not_zero, &not_empty_2);
629 // Length of the FixedArray is the number of pre-allocated elements even
630 // though the actual JSArray has length 0.
631 __ movq(FieldOperand(elements_array, Array::kLengthOffset),
632 Immediate(kPreallocatedArrayElements));
633 __ jmp(&fill_array);
634 __ bind(&not_empty_2);
635 // For non-empty JSArrays the length of the FixedArray and the JSArray is the
636 // same.
637 __ movq(FieldOperand(elements_array, Array::kLengthOffset), array_size);
638
639 // Fill the allocated FixedArray with the hole value if requested.
640 // result: JSObject
641 // elements_array: elements array
642 // elements_array_end: start of next object
643 __ bind(&fill_array);
644 if (fill_with_hole) {
645 Label loop, entry;
646 __ Move(scratch, Factory::the_hole_value());
647 __ lea(elements_array, Operand(elements_array,
648 FixedArray::kHeaderSize - kHeapObjectTag));
649 __ jmp(&entry);
650 __ bind(&loop);
651 __ movq(Operand(elements_array, 0), scratch);
652 __ addq(elements_array, Immediate(kPointerSize));
653 __ bind(&entry);
654 __ cmpq(elements_array, elements_array_end);
655 __ j(below, &loop);
656 }
657}
658
659
660// Create a new array for the built-in Array function. This function allocates
661// the JSArray object and the FixedArray elements array and initializes these.
662// If the Array cannot be constructed in native code the runtime is called. This
663// function assumes the following state:
664// rdi: constructor (built-in Array function)
665// rax: argc
666// rsp[0]: return address
667// rsp[8]: last argument
668// This function is used for both construct and normal calls of Array. The only
669// difference between handling a construct call and a normal call is that for a
670// construct call the constructor function in rdi needs to be preserved for
671// entering the generic code. In both cases argc in rax needs to be preserved.
672// Both registers are preserved by this code so no need to differentiate between
673// a construct call and a normal call.
674static void ArrayNativeCode(MacroAssembler* masm,
675 Label *call_generic_code) {
676 Label argc_one_or_more, argc_two_or_more;
677
678 // Check for array construction with zero arguments.
679 __ testq(rax, rax);
680 __ j(not_zero, &argc_one_or_more);
681
682 // Handle construction of an empty array.
683 AllocateEmptyJSArray(masm,
684 rdi,
685 rbx,
686 rcx,
687 rdx,
688 r8,
689 kPreallocatedArrayElements,
690 call_generic_code);
691 __ IncrementCounter(&Counters::array_function_native, 1);
692 __ movq(rax, rbx);
693 __ ret(kPointerSize);
694
695 // Check for one argument. Bail out if argument is not smi or if it is
696 // negative.
697 __ bind(&argc_one_or_more);
698 __ cmpq(rax, Immediate(1));
699 __ j(not_equal, &argc_two_or_more);
700 __ movq(rdx, Operand(rsp, kPointerSize)); // Get the argument from the stack.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000701 __ JumpIfNotPositiveSmi(rdx, call_generic_code);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000702
703 // Handle construction of an empty array of a certain size. Bail out if size
704 // is to large to actually allocate an elements array.
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000705 __ SmiCompare(rdx, Smi::FromInt(JSObject::kInitialMaxFastElementArray));
706 __ j(greater_equal, call_generic_code);
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000707
708 // rax: argc
709 // rdx: array_size (smi)
710 // rdi: constructor
711 // esp[0]: return address
712 // esp[8]: argument
713 AllocateJSArray(masm,
714 rdi,
715 rdx,
716 rbx,
717 rcx,
718 r8,
719 r9,
720 true,
721 call_generic_code);
722 __ IncrementCounter(&Counters::array_function_native, 1);
723 __ movq(rax, rbx);
724 __ ret(2 * kPointerSize);
725
726 // Handle construction of an array from a list of arguments.
727 __ bind(&argc_two_or_more);
728 __ movq(rdx, rax);
729 __ Integer32ToSmi(rdx, rdx); // Convet argc to a smi.
730 // rax: argc
731 // rdx: array_size (smi)
732 // rdi: constructor
733 // esp[0] : return address
734 // esp[8] : last argument
735 AllocateJSArray(masm,
736 rdi,
737 rdx,
738 rbx,
739 rcx,
740 r8,
741 r9,
742 false,
743 call_generic_code);
744 __ IncrementCounter(&Counters::array_function_native, 1);
745
746 // rax: argc
747 // rbx: JSArray
748 // rcx: elements_array
749 // r8: elements_array_end (untagged)
750 // esp[0]: return address
751 // esp[8]: last argument
752
753 // Location of the last argument
754 __ lea(r9, Operand(rsp, kPointerSize));
755
756 // Location of the first array element (Parameter fill_with_holes to
757 // AllocateJSArrayis false, so the FixedArray is returned in rcx).
758 __ lea(rdx, Operand(rcx, FixedArray::kHeaderSize - kHeapObjectTag));
759
760 // rax: argc
761 // rbx: JSArray
762 // rdx: location of the first array element
763 // r9: location of the last argument
764 // esp[0]: return address
765 // esp[8]: last argument
766 Label loop, entry;
767 __ movq(rcx, rax);
768 __ jmp(&entry);
769 __ bind(&loop);
770 __ movq(kScratchRegister, Operand(r9, rcx, times_pointer_size, 0));
771 __ movq(Operand(rdx, 0), kScratchRegister);
772 __ addq(rdx, Immediate(kPointerSize));
773 __ bind(&entry);
774 __ decq(rcx);
775 __ j(greater_equal, &loop);
776
777 // Remove caller arguments from the stack and return.
778 // rax: argc
779 // rbx: JSArray
780 // esp[0]: return address
781 // esp[8]: last argument
782 __ pop(rcx);
783 __ lea(rsp, Operand(rsp, rax, times_pointer_size, 1 * kPointerSize));
784 __ push(rcx);
785 __ movq(rax, rbx);
786 __ ret(0);
787}
788
789
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000790void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000791 // ----------- S t a t e -------------
792 // -- rax : argc
793 // -- rsp[0] : return address
794 // -- rsp[8] : last argument
795 // -----------------------------------
796 Label generic_array_code;
797
798 // Get the Array function.
799 GenerateLoadArrayFunction(masm, rdi);
800
801 if (FLAG_debug_code) {
802 // Initial map for the builtin Array function shoud be a map.
803 __ movq(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
804 // Will both indicate a NULL and a Smi.
805 ASSERT(kSmiTag == 0);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000806 Condition not_smi = NegateCondition(masm->CheckSmi(rbx));
807 __ Check(not_smi, "Unexpected initial map for Array function");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000808 __ CmpObjectType(rbx, MAP_TYPE, rcx);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000809 __ Check(equal, "Unexpected initial map for Array function");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000810 }
811
812 // Run the native code for the Array function called as a normal function.
813 ArrayNativeCode(masm, &generic_array_code);
814
815 // Jump to the generic array code in case the specialized code cannot handle
816 // the construction.
817 __ bind(&generic_array_code);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000818 Code* code = Builtins::builtin(Builtins::ArrayCodeGeneric);
819 Handle<Code> array_code(code);
820 __ Jump(array_code, RelocInfo::CODE_TARGET);
821}
822
823
824void Builtins::Generate_ArrayConstructCode(MacroAssembler* masm) {
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000825 // ----------- S t a t e -------------
826 // -- rax : argc
827 // -- rdi : constructor
828 // -- rsp[0] : return address
829 // -- rsp[8] : last argument
830 // -----------------------------------
831 Label generic_constructor;
832
833 if (FLAG_debug_code) {
834 // The array construct code is only set for the builtin Array function which
835 // does always have a map.
836 GenerateLoadArrayFunction(masm, rbx);
837 __ cmpq(rdi, rbx);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000838 __ Check(equal, "Unexpected Array function");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000839 // Initial map for the builtin Array function should be a map.
840 __ movq(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
841 // Will both indicate a NULL and a Smi.
842 ASSERT(kSmiTag == 0);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000843 Condition not_smi = NegateCondition(masm->CheckSmi(rbx));
844 __ Check(not_smi, "Unexpected initial map for Array function");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000845 __ CmpObjectType(rbx, MAP_TYPE, rcx);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +0000846 __ Check(equal, "Unexpected initial map for Array function");
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000847 }
848
849 // Run the native code for the Array function called as constructor.
850 ArrayNativeCode(masm, &generic_constructor);
851
852 // Jump to the generic construct code in case the specialized code cannot
853 // handle the construction.
854 __ bind(&generic_constructor);
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000855 Code* code = Builtins::builtin(Builtins::JSConstructStubGeneric);
856 Handle<Code> generic_construct_stub(code);
857 __ Jump(generic_construct_stub, RelocInfo::CODE_TARGET);
858}
859
860
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000861void Builtins::Generate_JSConstructCall(MacroAssembler* masm) {
ager@chromium.org3e875802009-06-29 08:26:34 +0000862 // ----------- S t a t e -------------
863 // -- rax: number of arguments
864 // -- rdi: constructor function
865 // -----------------------------------
866
867 Label non_function_call;
868 // Check that function is not a smi.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000869 __ JumpIfSmi(rdi, &non_function_call);
ager@chromium.org3e875802009-06-29 08:26:34 +0000870 // Check that function is a JSFunction.
871 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
872 __ j(not_equal, &non_function_call);
873
874 // Jump to the function-specific construct stub.
875 __ movq(rbx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
876 __ movq(rbx, FieldOperand(rbx, SharedFunctionInfo::kConstructStubOffset));
877 __ lea(rbx, FieldOperand(rbx, Code::kHeaderSize));
878 __ jmp(rbx);
879
880 // edi: called object
881 // eax: number of arguments
882 __ bind(&non_function_call);
ager@chromium.org3e875802009-06-29 08:26:34 +0000883 // Set expected number of arguments to zero (not changing eax).
884 __ movq(rbx, Immediate(0));
885 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
886 __ Jump(Handle<Code>(builtin(ArgumentsAdaptorTrampoline)),
887 RelocInfo::CODE_TARGET);
kasperl@chromium.org71affb52009-05-26 05:44:31 +0000888}
889
ager@chromium.org3e875802009-06-29 08:26:34 +0000890
ager@chromium.org5aa501c2009-06-23 07:57:28 +0000891void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
ager@chromium.org3e875802009-06-29 08:26:34 +0000892 // Enter a construct frame.
893 __ EnterConstructFrame();
894
895 // Store a smi-tagged arguments count on the stack.
ager@chromium.org4af710e2009-09-15 12:20:11 +0000896 __ Integer32ToSmi(rax, rax);
ager@chromium.org3e875802009-06-29 08:26:34 +0000897 __ push(rax);
898
899 // Push the function to invoke on the stack.
900 __ push(rdi);
901
902 // Try to allocate the object without transitioning into C code. If any of the
903 // preconditions is not met, the code bails out to the runtime call.
904 Label rt_call, allocated;
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000905 if (FLAG_inline_new) {
906 Label undo_allocation;
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000907
908#ifdef ENABLE_DEBUGGER_SUPPORT
909 ExternalReference debug_step_in_fp =
910 ExternalReference::debug_step_in_fp_address();
911 __ movq(kScratchRegister, debug_step_in_fp);
912 __ cmpq(Operand(kScratchRegister, 0), Immediate(0));
913 __ j(not_equal, &rt_call);
914#endif
ager@chromium.org3e875802009-06-29 08:26:34 +0000915
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000916 // Verified that the constructor is a JSFunction.
917 // Load the initial map and verify that it is in fact a map.
918 // rdi: constructor
919 __ movq(rax, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
920 // Will both indicate a NULL and a Smi
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000921 ASSERT(kSmiTag == 0);
ager@chromium.org4af710e2009-09-15 12:20:11 +0000922 __ JumpIfSmi(rax, &rt_call);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000923 // rdi: constructor
924 // rax: initial map (if proven valid below)
925 __ CmpObjectType(rax, MAP_TYPE, rbx);
926 __ j(not_equal, &rt_call);
927
928 // Check that the constructor is not constructing a JSFunction (see comments
929 // in Runtime_NewObject in runtime.cc). In which case the initial map's
930 // instance type would be JS_FUNCTION_TYPE.
931 // rdi: constructor
932 // rax: initial map
933 __ CmpInstanceType(rax, JS_FUNCTION_TYPE);
934 __ j(equal, &rt_call);
935
936 // Now allocate the JSObject on the heap.
937 __ movzxbq(rdi, FieldOperand(rax, Map::kInstanceSizeOffset));
938 __ shl(rdi, Immediate(kPointerSizeLog2));
939 // rdi: size of new object
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +0000940 __ AllocateInNewSpace(rdi,
941 rbx,
942 rdi,
943 no_reg,
944 &rt_call,
945 NO_ALLOCATION_FLAGS);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000946 // Allocated the JSObject, now initialize the fields.
947 // rax: initial map
948 // rbx: JSObject (not HeapObject tagged - the actual address).
949 // rdi: start of next object
950 __ movq(Operand(rbx, JSObject::kMapOffset), rax);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000951 __ LoadRoot(rcx, Heap::kEmptyFixedArrayRootIndex);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000952 __ movq(Operand(rbx, JSObject::kPropertiesOffset), rcx);
953 __ movq(Operand(rbx, JSObject::kElementsOffset), rcx);
954 // Set extra fields in the newly allocated object.
955 // rax: initial map
956 // rbx: JSObject
957 // rdi: start of next object
958 { Label loop, entry;
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000959 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000960 __ lea(rcx, Operand(rbx, JSObject::kHeaderSize));
961 __ jmp(&entry);
962 __ bind(&loop);
963 __ movq(Operand(rcx, 0), rdx);
964 __ addq(rcx, Immediate(kPointerSize));
965 __ bind(&entry);
966 __ cmpq(rcx, rdi);
967 __ j(less, &loop);
968 }
969
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000970 // Add the object tag to make the JSObject real, so that we can continue and
971 // jump into the continuation code at any time from now on. Any failures
972 // need to undo the allocation, so that the heap is in a consistent state
973 // and verifiable.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000974 // rax: initial map
975 // rbx: JSObject
976 // rdi: start of next object
977 __ or_(rbx, Immediate(kHeapObjectTag));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000978
979 // Check if a non-empty properties array is needed.
980 // Allocate and initialize a FixedArray if it is.
981 // rax: initial map
982 // rbx: JSObject
983 // rdi: start of next object
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000984 // Calculate total properties described map.
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000985 __ movzxbq(rdx, FieldOperand(rax, Map::kUnusedPropertyFieldsOffset));
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000986 __ movzxbq(rcx, FieldOperand(rax, Map::kPreAllocatedPropertyFieldsOffset));
987 __ addq(rdx, rcx);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000988 // Calculate unused properties past the end of the in-object properties.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000989 __ movzxbq(rcx, FieldOperand(rax, Map::kInObjectPropertiesOffset));
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000990 __ subq(rdx, rcx);
991 // Done if no extra properties are to be allocated.
992 __ j(zero, &allocated);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000993 __ Assert(positive, "Property allocation count failed.");
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +0000994
995 // Scale the number of elements by pointer size and add the header for
996 // FixedArrays to the start of the next object calculation from above.
997 // rbx: JSObject
998 // rdi: start of next object (will be start of FixedArray)
999 // rdx: number of elements in properties array
sgjesse@chromium.orgc5145742009-10-07 09:00:33 +00001000 __ AllocateInNewSpace(FixedArray::kHeaderSize,
1001 times_pointer_size,
1002 rdx,
1003 rdi,
1004 rax,
1005 no_reg,
1006 &undo_allocation,
1007 RESULT_CONTAINS_TOP);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001008
1009 // Initialize the FixedArray.
1010 // rbx: JSObject
1011 // rdi: FixedArray
1012 // rdx: number of elements
1013 // rax: start of next object
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001014 __ LoadRoot(rcx, Heap::kFixedArrayMapRootIndex);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001015 __ movq(Operand(rdi, JSObject::kMapOffset), rcx); // setup the map
1016 __ movl(Operand(rdi, FixedArray::kLengthOffset), rdx); // and length
1017
1018 // Initialize the fields to undefined.
1019 // rbx: JSObject
1020 // rdi: FixedArray
1021 // rax: start of next object
1022 // rdx: number of elements
1023 { Label loop, entry;
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001024 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001025 __ lea(rcx, Operand(rdi, FixedArray::kHeaderSize));
1026 __ jmp(&entry);
1027 __ bind(&loop);
1028 __ movq(Operand(rcx, 0), rdx);
1029 __ addq(rcx, Immediate(kPointerSize));
1030 __ bind(&entry);
1031 __ cmpq(rcx, rax);
1032 __ j(below, &loop);
1033 }
1034
1035 // Store the initialized FixedArray into the properties field of
1036 // the JSObject
1037 // rbx: JSObject
1038 // rdi: FixedArray
1039 __ or_(rdi, Immediate(kHeapObjectTag)); // add the heap tag
1040 __ movq(FieldOperand(rbx, JSObject::kPropertiesOffset), rdi);
1041
1042
1043 // Continue with JSObject being successfully allocated
1044 // rbx: JSObject
1045 __ jmp(&allocated);
1046
1047 // Undo the setting of the new top so that the heap is verifiable. For
1048 // example, the map's unused properties potentially do not match the
1049 // allocated objects unused properties.
1050 // rbx: JSObject (previous new top)
1051 __ bind(&undo_allocation);
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001052 __ UndoAllocationInNewSpace(rbx);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001053 }
ager@chromium.org3e875802009-06-29 08:26:34 +00001054
1055 // Allocate the new receiver object using the runtime call.
1056 // rdi: function (constructor)
1057 __ bind(&rt_call);
christian.plesner.hansen@gmail.com5a6af922009-08-12 14:20:51 +00001058 // Must restore rdi (constructor) before calling runtime.
ager@chromium.org3e875802009-06-29 08:26:34 +00001059 __ movq(rdi, Operand(rsp, 0));
1060 __ push(rdi);
1061 __ CallRuntime(Runtime::kNewObject, 1);
1062 __ movq(rbx, rax); // store result in rbx
1063
1064 // New object allocated.
1065 // rbx: newly allocated object
1066 __ bind(&allocated);
1067 // Retrieve the function from the stack.
1068 __ pop(rdi);
1069
1070 // Retrieve smi-tagged arguments count from the stack.
1071 __ movq(rax, Operand(rsp, 0));
ager@chromium.org4af710e2009-09-15 12:20:11 +00001072 __ SmiToInteger32(rax, rax);
ager@chromium.org3e875802009-06-29 08:26:34 +00001073
1074 // Push the allocated receiver to the stack. We need two copies
1075 // because we may have to return the original one and the calling
1076 // conventions dictate that the called function pops the receiver.
1077 __ push(rbx);
1078 __ push(rbx);
1079
1080 // Setup pointer to last argument.
1081 __ lea(rbx, Operand(rbp, StandardFrameConstants::kCallerSPOffset));
1082
1083 // Copy arguments and receiver to the expression stack.
1084 Label loop, entry;
1085 __ movq(rcx, rax);
1086 __ jmp(&entry);
1087 __ bind(&loop);
1088 __ push(Operand(rbx, rcx, times_pointer_size, 0));
1089 __ bind(&entry);
1090 __ decq(rcx);
1091 __ j(greater_equal, &loop);
1092
1093 // Call the function.
1094 ParameterCount actual(rax);
1095 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
1096
1097 // Restore context from the frame.
1098 __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
1099
1100 // If the result is an object (in the ECMA sense), we should get rid
1101 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
1102 // on page 74.
1103 Label use_receiver, exit;
1104 // If the result is a smi, it is *not* an object in the ECMA sense.
ager@chromium.org4af710e2009-09-15 12:20:11 +00001105 __ JumpIfSmi(rax, &use_receiver);
ager@chromium.org3e875802009-06-29 08:26:34 +00001106
1107 // If the type of the result (stored in its map) is less than
1108 // FIRST_JS_OBJECT_TYPE, it is not an object in the ECMA sense.
1109 __ CmpObjectType(rax, FIRST_JS_OBJECT_TYPE, rcx);
kasperl@chromium.orge959c182009-07-27 08:59:04 +00001110 __ j(above_equal, &exit);
ager@chromium.org3e875802009-06-29 08:26:34 +00001111
1112 // Throw away the result of the constructor invocation and use the
1113 // on-stack receiver as the result.
1114 __ bind(&use_receiver);
1115 __ movq(rax, Operand(rsp, 0));
1116
1117 // Restore the arguments count and leave the construct frame.
1118 __ bind(&exit);
1119 __ movq(rbx, Operand(rsp, kPointerSize)); // get arguments count
1120 __ LeaveConstructFrame();
1121
1122 // Remove caller arguments from the stack and return.
ager@chromium.org3e875802009-06-29 08:26:34 +00001123 __ pop(rcx);
christian.plesner.hansen@gmail.com9d58c2b2009-10-16 11:48:38 +00001124 SmiIndex index = masm->SmiToIndex(rbx, rbx, kPointerSizeLog2);
1125 __ lea(rsp, Operand(rsp, index.reg, index.scale, 1 * kPointerSize));
ager@chromium.org3e875802009-06-29 08:26:34 +00001126 __ push(rcx);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001127 __ IncrementCounter(&Counters::constructed_objects, 1);
ager@chromium.org3e875802009-06-29 08:26:34 +00001128 __ ret(0);
ager@chromium.org5aa501c2009-06-23 07:57:28 +00001129}
1130
ager@chromium.org3e875802009-06-29 08:26:34 +00001131
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001132static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
1133 bool is_construct) {
1134 // Expects five C++ function parameters.
1135 // - Address entry (ignored)
1136 // - JSFunction* function (
1137 // - Object* receiver
1138 // - int argc
1139 // - Object*** argv
1140 // (see Handle::Invoke in execution.cc).
1141
1142 // Platform specific argument handling. After this, the stack contains
1143 // an internal frame and the pushed function and receiver, and
1144 // register rax and rbx holds the argument count and argument array,
1145 // while rdi holds the function pointer and rsi the context.
ager@chromium.org96c75b52009-08-26 09:13:16 +00001146#ifdef _WIN64
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001147 // MSVC parameters in:
1148 // rcx : entry (ignored)
1149 // rdx : function
1150 // r8 : receiver
1151 // r9 : argc
1152 // [rsp+0x20] : argv
1153
1154 // Clear the context before we push it when entering the JS frame.
1155 __ xor_(rsi, rsi);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001156 __ EnterInternalFrame();
1157
1158 // Load the function context into rsi.
1159 __ movq(rsi, FieldOperand(rdx, JSFunction::kContextOffset));
1160
1161 // Push the function and the receiver onto the stack.
1162 __ push(rdx);
1163 __ push(r8);
1164
1165 // Load the number of arguments and setup pointer to the arguments.
1166 __ movq(rax, r9);
1167 // Load the previous frame pointer to access C argument on stack
1168 __ movq(kScratchRegister, Operand(rbp, 0));
1169 __ movq(rbx, Operand(kScratchRegister, EntryFrameConstants::kArgvOffset));
1170 // Load the function pointer into rdi.
1171 __ movq(rdi, rdx);
ager@chromium.org96c75b52009-08-26 09:13:16 +00001172#else // !defined(_WIN64)
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001173 // GCC parameters in:
1174 // rdi : entry (ignored)
1175 // rsi : function
1176 // rdx : receiver
1177 // rcx : argc
1178 // r8 : argv
1179
1180 __ movq(rdi, rsi);
1181 // rdi : function
1182
1183 // Clear the context before we push it when entering the JS frame.
1184 __ xor_(rsi, rsi);
1185 // Enter an internal frame.
1186 __ EnterInternalFrame();
1187
1188 // Push the function and receiver and setup the context.
1189 __ push(rdi);
1190 __ push(rdx);
1191 __ movq(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
1192
1193 // Load the number of arguments and setup pointer to the arguments.
1194 __ movq(rax, rcx);
1195 __ movq(rbx, r8);
ager@chromium.org96c75b52009-08-26 09:13:16 +00001196#endif // _WIN64
ager@chromium.org18ad94b2009-09-02 08:22:29 +00001197
1198 // Set up the roots register.
1199 ExternalReference roots_address = ExternalReference::roots_address();
1200 __ movq(r13, roots_address);
1201
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001202 // Current stack contents:
1203 // [rsp + 2 * kPointerSize ... ]: Internal frame
1204 // [rsp + kPointerSize] : function
1205 // [rsp] : receiver
1206 // Current register contents:
1207 // rax : argc
1208 // rbx : argv
1209 // rsi : context
1210 // rdi : function
1211
1212 // Copy arguments to the stack in a loop.
1213 // Register rbx points to array of pointers to handle locations.
1214 // Push the values of these handles.
1215 Label loop, entry;
1216 __ xor_(rcx, rcx); // Set loop variable to 0.
1217 __ jmp(&entry);
1218 __ bind(&loop);
ager@chromium.org3e875802009-06-29 08:26:34 +00001219 __ movq(kScratchRegister, Operand(rbx, rcx, times_pointer_size, 0));
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001220 __ push(Operand(kScratchRegister, 0)); // dereference handle
1221 __ addq(rcx, Immediate(1));
1222 __ bind(&entry);
1223 __ cmpq(rcx, rax);
1224 __ j(not_equal, &loop);
1225
1226 // Invoke the code.
1227 if (is_construct) {
1228 // Expects rdi to hold function pointer.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +00001229 __ Call(Handle<Code>(Builtins::builtin(Builtins::JSConstructCall)),
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001230 RelocInfo::CODE_TARGET);
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001231 } else {
1232 ParameterCount actual(rax);
1233 // Function must be in rdi.
1234 __ InvokeFunction(rdi, actual, CALL_FUNCTION);
1235 }
1236
1237 // Exit the JS frame. Notice that this also removes the empty
1238 // context and the function left on the stack by the code
1239 // invocation.
1240 __ LeaveInternalFrame();
1241 // TODO(X64): Is argument correct? Is there a receiver to remove?
1242 __ ret(1 * kPointerSize); // remove receiver
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001243}
1244
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001245
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001246void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +00001247 Generate_JSEntryTrampolineHelper(masm, false);
1248}
1249
1250
1251void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
1252 Generate_JSEntryTrampolineHelper(masm, true);
kasperl@chromium.org71affb52009-05-26 05:44:31 +00001253}
1254
1255} } // namespace v8::internal