blob: 5767489660dd57e6d3fc572ca1b1d250dde53722 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_IA32
Leon Clarkef7060e22010-06-03 12:02:55 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/code-factory.h"
10#include "src/codegen.h"
11#include "src/deoptimizer.h"
12#include "src/full-codegen.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000013
14namespace v8 {
15namespace internal {
16
17
18#define __ ACCESS_MASM(masm)
19
20
Leon Clarkee46be812010-01-19 14:06:41 +000021void Builtins::Generate_Adaptor(MacroAssembler* masm,
22 CFunctionId id,
23 BuiltinExtraArguments extra_args) {
24 // ----------- S t a t e -------------
25 // -- eax : number of arguments excluding receiver
26 // -- edi : called function (only guaranteed when
27 // extra_args requires it)
28 // -- esi : context
29 // -- esp[0] : return address
30 // -- esp[4] : last argument
31 // -- ...
32 // -- esp[4 * argc] : first argument (argc == eax)
33 // -- esp[4 * (argc +1)] : receiver
34 // -----------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000035
Leon Clarkee46be812010-01-19 14:06:41 +000036 // Insert extra arguments.
37 int num_extra_args = 0;
38 if (extra_args == NEEDS_CALLED_FUNCTION) {
39 num_extra_args = 1;
40 Register scratch = ebx;
41 __ pop(scratch); // Save return address.
42 __ push(edi);
43 __ push(scratch); // Restore return address.
44 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
Leon Clarkee46be812010-01-19 14:06:41 +000046 }
47
Steve Block6ded16b2010-05-10 14:33:55 +010048 // JumpToExternalReference expects eax to contain the number of arguments
Leon Clarkee46be812010-01-19 14:06:41 +000049 // including the receiver and the extra arguments.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050 __ add(eax, Immediate(num_extra_args + 1));
Steve Block44f0eee2011-05-26 01:26:41 +010051 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
Steve Blocka7e24c12009-10-30 11:49:00 +000052}
53
54
Ben Murdochb8a8cc12014-11-26 15:28:44 +000055static void CallRuntimePassFunction(
56 MacroAssembler* masm, Runtime::FunctionId function_id) {
57 FrameScope scope(masm, StackFrame::INTERNAL);
58 // Push a copy of the function.
59 __ push(edi);
60 // Function is also the parameter to the runtime call.
61 __ push(edi);
62
63 __ CallRuntime(function_id, 1);
64 // Restore receiver.
65 __ pop(edi);
66}
67
68
69static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
70 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
71 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kCodeOffset));
72 __ lea(eax, FieldOperand(eax, Code::kHeaderSize));
73 __ jmp(eax);
74}
75
76
77static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
78 __ lea(eax, FieldOperand(eax, Code::kHeaderSize));
79 __ jmp(eax);
80}
81
82
83void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
84 // Checking whether the queued function is ready for install is optional,
85 // since we come across interrupts and stack checks elsewhere. However,
86 // not checking may delay installing ready functions, and always checking
87 // would be quite expensive. A good compromise is to first check against
88 // stack limit as a cue for an interrupt signal.
89 Label ok;
90 ExternalReference stack_limit =
91 ExternalReference::address_of_stack_limit(masm->isolate());
92 __ cmp(esp, Operand::StaticVariable(stack_limit));
93 __ j(above_equal, &ok, Label::kNear);
94
95 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
96 GenerateTailCallToReturnedCode(masm);
97
98 __ bind(&ok);
99 GenerateTailCallToSharedCode(masm);
100}
101
102
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100103static void Generate_JSConstructStubHelper(MacroAssembler* masm,
104 bool is_api_function,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 bool create_memento) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106 // ----------- S t a t e -------------
107 // -- eax: number of arguments
108 // -- edi: constructor function
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 // -- ebx: allocation site or undefined
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100110 // -----------------------------------
111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 // Should never create mementos for api functions.
113 DCHECK(!is_api_function || !create_memento);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100114
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 // Enter a construct frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100116 {
117 FrameScope scope(masm, StackFrame::CONSTRUCT);
Steve Blocka7e24c12009-10-30 11:49:00 +0000118
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 if (create_memento) {
120 __ AssertUndefinedOrAllocationSite(ebx);
121 __ push(ebx);
122 }
123
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100124 // Store a smi-tagged arguments count on the stack.
125 __ SmiTag(eax);
126 __ push(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000127
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100128 // Push the function to invoke on the stack.
129 __ push(edi);
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131 // Try to allocate the object without transitioning into C code. If any of
132 // the preconditions is not met, the code bails out to the runtime call.
133 Label rt_call, allocated;
134 if (FLAG_inline_new) {
135 Label undo_allocation;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100136 ExternalReference debug_step_in_fp =
137 ExternalReference::debug_step_in_fp_address(masm->isolate());
138 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
139 __ j(not_equal, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000140
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100141 // Verified that the constructor is a JSFunction.
142 // Load the initial map and verify that it is in fact a map.
143 // edi: constructor
144 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
145 // Will both indicate a NULL and a Smi
146 __ JumpIfSmi(eax, &rt_call);
147 // edi: constructor
148 // eax: initial map (if proven valid below)
149 __ CmpObjectType(eax, MAP_TYPE, ebx);
150 __ j(not_equal, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000151
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100152 // Check that the constructor is not constructing a JSFunction (see
153 // comments in Runtime_NewObject in runtime.cc). In which case the
154 // initial map's instance type would be JS_FUNCTION_TYPE.
155 // edi: constructor
156 // eax: initial map
157 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
158 __ j(equal, &rt_call);
Steve Blocka7e24c12009-10-30 11:49:00 +0000159
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 if (!is_api_function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100161 Label allocate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 // The code below relies on these assumptions.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163 STATIC_ASSERT(Map::Counter::kShift + Map::Counter::kSize == 32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 // Check if slack tracking is enabled.
165 __ mov(esi, FieldOperand(eax, Map::kBitField3Offset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400166 __ shr(esi, Map::Counter::kShift);
167 __ cmp(esi, Map::kSlackTrackingCounterEnd);
168 __ j(less, &allocate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100169 // Decrease generous allocation count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 __ sub(FieldOperand(eax, Map::kBitField3Offset),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400171 Immediate(1 << Map::Counter::kShift));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000172
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400173 __ cmp(esi, Map::kSlackTrackingCounterEnd);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000174 __ j(not_equal, &allocate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100175
176 __ push(eax);
177 __ push(edi);
178
179 __ push(edi); // constructor
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100180 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
181
182 __ pop(edi);
183 __ pop(eax);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400184 __ mov(esi, Map::kSlackTrackingCounterEnd - 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100185
186 __ bind(&allocate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000187 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100188
189 // Now allocate the JSObject on the heap.
190 // edi: constructor
191 // eax: initial map
192 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
193 __ shl(edi, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000194 if (create_memento) {
195 __ add(edi, Immediate(AllocationMemento::kSize));
196 }
197
198 __ Allocate(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS);
199
200 Factory* factory = masm->isolate()->factory();
201
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100202 // Allocated the JSObject, now initialize the fields.
203 // eax: initial map
204 // ebx: JSObject
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 // edi: start of next object (including memento if create_memento)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100206 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100207 __ mov(ecx, factory->empty_fixed_array());
208 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
209 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
210 // Set extra fields in the newly allocated object.
211 // eax: initial map
212 // ebx: JSObject
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 // edi: start of next object (including memento if create_memento)
214 // esi: slack tracking counter (non-API function case)
Ben Murdoch85b71792012-04-11 18:30:58 +0100215 __ mov(edx, factory->undefined_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
217 if (!is_api_function) {
218 Label no_inobject_slack_tracking;
219
220 // Check if slack tracking is enabled.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400221 __ cmp(esi, Map::kSlackTrackingCounterEnd);
222 __ j(less, &no_inobject_slack_tracking);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223
224 // Allocate object with a slack.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100225 __ movzx_b(esi,
226 FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset));
227 __ lea(esi,
228 Operand(ebx, esi, times_pointer_size, JSObject::kHeaderSize));
229 // esi: offset of first field after pre-allocated fields
230 if (FLAG_debug_code) {
231 __ cmp(esi, edi);
232 __ Assert(less_equal,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 kUnexpectedNumberOfPreAllocatedPropertyFields);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100234 }
235 __ InitializeFieldsWithFiller(ecx, esi, edx);
236 __ mov(edx, factory->one_pointer_filler_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237 // Fill the remaining fields with one pointer filler map.
238
239 __ bind(&no_inobject_slack_tracking);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241
242 if (create_memento) {
243 __ lea(esi, Operand(edi, -AllocationMemento::kSize));
244 __ InitializeFieldsWithFiller(ecx, esi, edx);
245
246 // Fill in memento fields if necessary.
247 // esi: points to the allocated but uninitialized memento.
248 __ mov(Operand(esi, AllocationMemento::kMapOffset),
249 factory->allocation_memento_map());
250 // Get the cell or undefined.
251 __ mov(edx, Operand(esp, kPointerSize*2));
252 __ mov(Operand(esi, AllocationMemento::kAllocationSiteOffset),
253 edx);
254 } else {
255 __ InitializeFieldsWithFiller(ecx, edi, edx);
256 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100257
258 // Add the object tag to make the JSObject real, so that we can continue
259 // and jump into the continuation code at any time from now on. Any
260 // failures need to undo the allocation, so that the heap is in a
261 // consistent state and verifiable.
262 // eax: initial map
263 // ebx: JSObject
264 // edi: start of next object
265 __ or_(ebx, Immediate(kHeapObjectTag));
266
267 // Check if a non-empty properties array is needed.
268 // Allocate and initialize a FixedArray if it is.
269 // eax: initial map
270 // ebx: JSObject
271 // edi: start of next object
272 // Calculate the total number of properties described by the map.
273 __ movzx_b(edx, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
274 __ movzx_b(ecx,
275 FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset));
276 __ add(edx, ecx);
277 // Calculate unused properties past the end of the in-object properties.
278 __ movzx_b(ecx, FieldOperand(eax, Map::kInObjectPropertiesOffset));
279 __ sub(edx, ecx);
280 // Done if no extra properties are to be allocated.
281 __ j(zero, &allocated);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 __ Assert(positive, kPropertyAllocationCountFailed);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100283
284 // Scale the number of elements by pointer size and add the header for
285 // FixedArrays to the start of the next object calculation from above.
286 // ebx: JSObject
287 // edi: start of next object (will be start of FixedArray)
288 // edx: number of elements in properties array
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 __ Allocate(FixedArray::kHeaderSize,
290 times_pointer_size,
291 edx,
292 REGISTER_VALUE_IS_INT32,
293 edi,
294 ecx,
295 no_reg,
296 &undo_allocation,
297 RESULT_CONTAINS_TOP);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100298
299 // Initialize the FixedArray.
300 // ebx: JSObject
301 // edi: FixedArray
302 // edx: number of elements
303 // ecx: start of next object
304 __ mov(eax, factory->fixed_array_map());
305 __ mov(Operand(edi, FixedArray::kMapOffset), eax); // setup the map
306 __ SmiTag(edx);
307 __ mov(Operand(edi, FixedArray::kLengthOffset), edx); // and length
308
309 // Initialize the fields to undefined.
310 // ebx: JSObject
311 // edi: FixedArray
312 // ecx: start of next object
313 { Label loop, entry;
314 __ mov(edx, factory->undefined_value());
315 __ lea(eax, Operand(edi, FixedArray::kHeaderSize));
316 __ jmp(&entry);
317 __ bind(&loop);
318 __ mov(Operand(eax, 0), edx);
319 __ add(eax, Immediate(kPointerSize));
320 __ bind(&entry);
321 __ cmp(eax, ecx);
322 __ j(below, &loop);
323 }
324
325 // Store the initialized FixedArray into the properties field of
326 // the JSObject
327 // ebx: JSObject
328 // edi: FixedArray
329 __ or_(edi, Immediate(kHeapObjectTag)); // add the heap tag
330 __ mov(FieldOperand(ebx, JSObject::kPropertiesOffset), edi);
331
332
333 // Continue with JSObject being successfully allocated
334 // ebx: JSObject
335 __ jmp(&allocated);
336
337 // Undo the setting of the new top so that the heap is verifiable. For
338 // example, the map's unused properties potentially do not match the
339 // allocated objects unused properties.
340 // ebx: JSObject (previous new top)
341 __ bind(&undo_allocation);
342 __ UndoAllocationInNewSpace(ebx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000343 }
344
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100345 // Allocate the new receiver object using the runtime call.
346 __ bind(&rt_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 int offset = 0;
348 if (create_memento) {
349 // Get the cell or allocation site.
350 __ mov(edi, Operand(esp, kPointerSize * 2));
351 __ push(edi);
352 offset = kPointerSize;
353 }
354
355 // Must restore esi (context) and edi (constructor) before calling runtime.
356 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
357 __ mov(edi, Operand(esp, offset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100358 // edi: function (constructor)
359 __ push(edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 if (create_memento) {
361 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 2);
362 } else {
363 __ CallRuntime(Runtime::kNewObject, 1);
364 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100365 __ mov(ebx, eax); // store result in ebx
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +0100366
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 // If we ended up using the runtime, and we want a memento, then the
368 // runtime call made it for us, and we shouldn't do create count
369 // increment.
370 Label count_incremented;
371 if (create_memento) {
372 __ jmp(&count_incremented);
373 }
374
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100375 // New object allocated.
376 // ebx: newly allocated object
377 __ bind(&allocated);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378
379 if (create_memento) {
380 __ mov(ecx, Operand(esp, kPointerSize * 2));
381 __ cmp(ecx, masm->isolate()->factory()->undefined_value());
382 __ j(equal, &count_incremented);
383 // ecx is an AllocationSite. We are creating a memento from it, so we
384 // need to increment the memento create count.
385 __ add(FieldOperand(ecx, AllocationSite::kPretenureCreateCountOffset),
386 Immediate(Smi::FromInt(1)));
387 __ bind(&count_incremented);
388 }
389
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100390 // Retrieve the function from the stack.
391 __ pop(edi);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000392
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100393 // Retrieve smi-tagged arguments count from the stack.
394 __ mov(eax, Operand(esp, 0));
395 __ SmiUntag(eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000396
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100397 // Push the allocated receiver to the stack. We need two copies
398 // because we may have to return the original one and the calling
399 // conventions dictate that the called function pops the receiver.
400 __ push(ebx);
401 __ push(ebx);
402
403 // Set up pointer to last argument.
404 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
405
406 // Copy arguments and receiver to the expression stack.
407 Label loop, entry;
408 __ mov(ecx, eax);
409 __ jmp(&entry);
410 __ bind(&loop);
411 __ push(Operand(ebx, ecx, times_4, 0));
412 __ bind(&entry);
413 __ dec(ecx);
414 __ j(greater_equal, &loop);
415
416 // Call the function.
417 if (is_api_function) {
418 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
419 Handle<Code> code =
420 masm->isolate()->builtins()->HandleApiCallConstruct();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 __ call(code, RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100422 } else {
423 ParameterCount actual(eax);
424 __ InvokeFunction(edi, actual, CALL_FUNCTION,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000425 NullCallWrapper());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100426 }
427
428 // Store offset of return address for deoptimizer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000429 if (!is_api_function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100430 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
431 }
432
433 // Restore context from the frame.
434 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
435
436 // If the result is an object (in the ECMA sense), we should get rid
437 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
438 // on page 74.
439 Label use_receiver, exit;
440
441 // If the result is a smi, it is *not* an object in the ECMA sense.
442 __ JumpIfSmi(eax, &use_receiver);
443
444 // If the type of the result (stored in its map) is less than
445 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
446 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
447 __ j(above_equal, &exit);
448
449 // Throw away the result of the constructor invocation and use the
450 // on-stack receiver as the result.
451 __ bind(&use_receiver);
452 __ mov(eax, Operand(esp, 0));
453
454 // Restore the arguments count and leave the construct frame.
455 __ bind(&exit);
456 __ mov(ebx, Operand(esp, kPointerSize)); // Get arguments count.
457
458 // Leave construct frame.
Steve Blocka7e24c12009-10-30 11:49:00 +0000459 }
460
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 // Remove caller arguments from the stack and return.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000462 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000463 __ pop(ecx);
464 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
465 __ push(ecx);
Steve Block44f0eee2011-05-26 01:26:41 +0100466 __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 __ ret(0);
468}
469
470
Leon Clarkee46be812010-01-19 14:06:41 +0000471void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
Leon Clarkee46be812010-01-19 14:06:41 +0000473}
474
475
476void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100477 Generate_JSConstructStubHelper(masm, true, false);
Leon Clarkee46be812010-01-19 14:06:41 +0000478}
479
480
Steve Blocka7e24c12009-10-30 11:49:00 +0000481static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
482 bool is_construct) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 ProfileEntryHookStub::MaybeCallEntryHook(masm);
484
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100485 // Clear the context before we push it when entering the internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 __ Move(esi, Immediate(0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000487
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100488 {
489 FrameScope scope(masm, StackFrame::INTERNAL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000490
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100491 // Load the previous frame pointer (ebx) to access C arguments
492 __ mov(ebx, Operand(ebp, 0));
Steve Blocka7e24c12009-10-30 11:49:00 +0000493
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100494 // Get the function from the frame and setup the context.
495 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
496 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000497
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100498 // Push the function and the receiver onto the stack.
499 __ push(ecx);
500 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000501
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100502 // Load the number of arguments and setup pointer to the arguments.
503 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
504 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000505
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100506 // Copy arguments to the stack in a loop.
507 Label loop, entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 __ Move(ecx, Immediate(0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100509 __ jmp(&entry);
510 __ bind(&loop);
511 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
512 __ push(Operand(edx, 0)); // dereference handle
513 __ inc(ecx);
514 __ bind(&entry);
515 __ cmp(ecx, eax);
516 __ j(not_equal, &loop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000517
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100518 // Get the function from the stack and call it.
519 // kPointerSize for the receiver.
520 __ mov(edi, Operand(esp, eax, times_4, kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000521
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100522 // Invoke the code.
523 if (is_construct) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 // No type feedback cell is available
525 __ mov(ebx, masm->isolate()->factory()->undefined_value());
526 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100527 __ CallStub(&stub);
528 } else {
529 ParameterCount actual(eax);
530 __ InvokeFunction(edi, actual, CALL_FUNCTION,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 NullCallWrapper());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100532 }
533
534 // Exit the internal frame. Notice that this also removes the empty.
535 // context and the function left on the stack by the code
536 // invocation.
Steve Blocka7e24c12009-10-30 11:49:00 +0000537 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100538 __ ret(kPointerSize); // Remove receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +0000539}
540
541
542void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
543 Generate_JSEntryTrampolineHelper(masm, false);
544}
545
546
547void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
548 Generate_JSEntryTrampolineHelper(masm, true);
549}
550
551
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
553 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
554 GenerateTailCallToReturnedCode(masm);
Iain Merrick75681382010-08-19 15:07:18 +0100555}
556
557
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558
559static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
560 FrameScope scope(masm, StackFrame::INTERNAL);
561 // Push a copy of the function.
562 __ push(edi);
563 // Function is also the parameter to the runtime call.
564 __ push(edi);
565 // Whether to compile in a background thread.
566 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
567
568 __ CallRuntime(Runtime::kCompileOptimized, 2);
569 // Restore receiver.
570 __ pop(edi);
571}
572
573
574void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
575 CallCompileOptimized(masm, false);
576 GenerateTailCallToReturnedCode(masm);
577}
578
579
580void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
581 CallCompileOptimized(masm, true);
582 GenerateTailCallToReturnedCode(masm);
583}
584
585
586static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
587 // For now, we are relying on the fact that make_code_young doesn't do any
588 // garbage collection which allows us to save/restore the registers without
589 // worrying about which of them contain pointers. We also don't build an
590 // internal frame to make the code faster, since we shouldn't have to do stack
591 // crawls in MakeCodeYoung. This seems a bit fragile.
592
593 // Re-execute the code that was patched back to the young age when
594 // the stub returns.
595 __ sub(Operand(esp, 0), Immediate(5));
596 __ pushad();
597 __ mov(eax, Operand(esp, 8 * kPointerSize));
598 {
599 FrameScope scope(masm, StackFrame::MANUAL);
600 __ PrepareCallCFunction(2, ebx);
601 __ mov(Operand(esp, 1 * kPointerSize),
602 Immediate(ExternalReference::isolate_address(masm->isolate())));
603 __ mov(Operand(esp, 0), eax);
604 __ CallCFunction(
605 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
606 }
607 __ popad();
608 __ ret(0);
609}
610
611#define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
612void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
613 MacroAssembler* masm) { \
614 GenerateMakeCodeYoungAgainCommon(masm); \
615} \
616void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
617 MacroAssembler* masm) { \
618 GenerateMakeCodeYoungAgainCommon(masm); \
619}
620CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
621#undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
622
623
624void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
625 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
626 // that make_code_young doesn't do any garbage collection which allows us to
627 // save/restore the registers without worrying about which of them contain
628 // pointers.
629 __ pushad();
630 __ mov(eax, Operand(esp, 8 * kPointerSize));
631 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
632 { // NOLINT
633 FrameScope scope(masm, StackFrame::MANUAL);
634 __ PrepareCallCFunction(2, ebx);
635 __ mov(Operand(esp, 1 * kPointerSize),
636 Immediate(ExternalReference::isolate_address(masm->isolate())));
637 __ mov(Operand(esp, 0), eax);
638 __ CallCFunction(
639 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
640 2);
641 }
642 __ popad();
643
644 // Perform prologue operations usually performed by the young code stub.
645 __ pop(eax); // Pop return address into scratch register.
646 __ push(ebp); // Caller's frame pointer.
647 __ mov(ebp, esp);
648 __ push(esi); // Callee's context.
649 __ push(edi); // Callee's JS Function.
650 __ push(eax); // Push return address after frame prologue.
651
652 // Jump to point after the code-age stub.
653 __ ret(0);
654}
655
656
657void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
658 GenerateMakeCodeYoungAgainCommon(masm);
659}
660
661
662static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
663 SaveFPRegsMode save_doubles) {
664 // Enter an internal frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100665 {
666 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 // Preserve registers across notification, this is important for compiled
669 // stubs that tail call the runtime on deopts passing their parameters in
670 // registers.
671 __ pushad();
672 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
673 __ popad();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100674 // Tear down internal frame.
675 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100676
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000677 __ pop(MemOperand(esp, 0)); // Ignore state offset
678 __ ret(0); // Return to IC Miss stub, continuation still on stack.
679}
680
681
682void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
683 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
684}
685
686
687void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
688 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100689}
690
691
692static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
693 Deoptimizer::BailoutType type) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100694 {
695 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100696
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100697 // Pass deoptimization type to the runtime system.
698 __ push(Immediate(Smi::FromInt(static_cast<int>(type))));
699 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100700
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100701 // Tear down internal frame.
702 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100703
704 // Get the full codegen state from the stack and untag it.
705 __ mov(ecx, Operand(esp, 1 * kPointerSize));
706 __ SmiUntag(ecx);
707
708 // Switch on the state.
Ben Murdoch257744e2011-11-30 15:57:28 +0000709 Label not_no_registers, not_tos_eax;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100710 __ cmp(ecx, FullCodeGenerator::NO_REGISTERS);
Ben Murdoch257744e2011-11-30 15:57:28 +0000711 __ j(not_equal, &not_no_registers, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100712 __ ret(1 * kPointerSize); // Remove state.
713
714 __ bind(&not_no_registers);
715 __ mov(eax, Operand(esp, 2 * kPointerSize));
716 __ cmp(ecx, FullCodeGenerator::TOS_REG);
Ben Murdoch257744e2011-11-30 15:57:28 +0000717 __ j(not_equal, &not_tos_eax, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100718 __ ret(2 * kPointerSize); // Remove state, eax.
719
720 __ bind(&not_tos_eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 __ Abort(kNoCasesLeft);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100722}
723
724
725void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
726 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
727}
728
729
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
731 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100732}
733
734
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
736 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100737}
738
739
Steve Blocka7e24c12009-10-30 11:49:00 +0000740void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +0100741 Factory* factory = masm->isolate()->factory();
742
Steve Blocka7e24c12009-10-30 11:49:00 +0000743 // 1. Make sure we have at least one argument.
744 { Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100745 __ test(eax, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +0000746 __ j(not_zero, &done);
Steve Blocka7e24c12009-10-30 11:49:00 +0000747 __ pop(ebx);
Steve Block44f0eee2011-05-26 01:26:41 +0100748 __ push(Immediate(factory->undefined_value()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 __ push(ebx);
750 __ inc(eax);
751 __ bind(&done);
752 }
753
Andrei Popescu402d9372010-02-26 13:31:12 +0000754 // 2. Get the function to call (passed as receiver) from the stack, check
755 // if it is a function.
Ben Murdoch589d6972011-11-30 16:04:58 +0000756 Label slow, non_function;
Andrei Popescu402d9372010-02-26 13:31:12 +0000757 // 1 ~ return address.
758 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000759 __ JumpIfSmi(edi, &non_function);
Andrei Popescu402d9372010-02-26 13:31:12 +0000760 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch589d6972011-11-30 16:04:58 +0000761 __ j(not_equal, &slow);
Steve Blocka7e24c12009-10-30 11:49:00 +0000762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763
Andrei Popescu402d9372010-02-26 13:31:12 +0000764 // 3a. Patch the first argument if necessary when calling a function.
765 Label shift_arguments;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000766 __ Move(edx, Immediate(0)); // indicate regular JS_FUNCTION
767 { Label convert_to_object, use_global_proxy, patch_receiver;
Andrei Popescu402d9372010-02-26 13:31:12 +0000768 // Change context eagerly in case we need the global receiver.
Steve Blocka7e24c12009-10-30 11:49:00 +0000769 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
770
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100771 // Do not transform the receiver for strict mode functions.
772 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
773 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kStrictModeByteOffset),
774 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
775 __ j(not_equal, &shift_arguments);
776
Ben Murdoch257744e2011-11-30 15:57:28 +0000777 // Do not transform the receiver for natives (shared already in ebx).
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000778 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kNativeByteOffset),
779 1 << SharedFunctionInfo::kNativeBitWithinByte);
Ben Murdoch257744e2011-11-30 15:57:28 +0000780 __ j(not_equal, &shift_arguments);
781
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 // Compute the receiver in sloppy mode.
Andrei Popescu402d9372010-02-26 13:31:12 +0000783 __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument.
Ben Murdoch257744e2011-11-30 15:57:28 +0000784
785 // Call ToObject on the receiver if it is not an object, or use the
786 // global object if it is null or undefined.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000787 __ JumpIfSmi(ebx, &convert_to_object);
Steve Block44f0eee2011-05-26 01:26:41 +0100788 __ cmp(ebx, factory->null_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 __ j(equal, &use_global_proxy);
Steve Block44f0eee2011-05-26 01:26:41 +0100790 __ cmp(ebx, factory->undefined_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000791 __ j(equal, &use_global_proxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000792 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
793 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000794 __ j(above_equal, &shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000795
Andrei Popescu402d9372010-02-26 13:31:12 +0000796 __ bind(&convert_to_object);
Steve Blocka7e24c12009-10-30 11:49:00 +0000797
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100798 { // In order to preserve argument count.
799 FrameScope scope(masm, StackFrame::INTERNAL);
800 __ SmiTag(eax);
801 __ push(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000802
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100803 __ push(ebx);
804 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
805 __ mov(ebx, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000806 __ Move(edx, Immediate(0)); // restore
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100807
808 __ pop(eax);
809 __ SmiUntag(eax);
810 }
811
Andrei Popescu402d9372010-02-26 13:31:12 +0000812 // Restore the function to edi.
813 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
Steve Blocka7e24c12009-10-30 11:49:00 +0000814 __ jmp(&patch_receiver);
815
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 __ bind(&use_global_proxy);
817 __ mov(ebx,
818 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
819 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000820
821 __ bind(&patch_receiver);
822 __ mov(Operand(esp, eax, times_4, 0), ebx);
823
Andrei Popescu402d9372010-02-26 13:31:12 +0000824 __ jmp(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000825 }
826
Ben Murdoch589d6972011-11-30 16:04:58 +0000827 // 3b. Check for function proxy.
828 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 __ Move(edx, Immediate(1)); // indicate function proxy
Ben Murdoch589d6972011-11-30 16:04:58 +0000830 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
831 __ j(equal, &shift_arguments);
832 __ bind(&non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 __ Move(edx, Immediate(2)); // indicate non-function
Ben Murdoch589d6972011-11-30 16:04:58 +0000834
835 // 3c. Patch the first argument when calling a non-function. The
Andrei Popescu402d9372010-02-26 13:31:12 +0000836 // CALL_NON_FUNCTION builtin expects the non-function callee as
837 // receiver, so overwrite the first argument which will ultimately
838 // become the receiver.
Andrei Popescu402d9372010-02-26 13:31:12 +0000839 __ mov(Operand(esp, eax, times_4, 0), edi);
Leon Clarkee46be812010-01-19 14:06:41 +0000840
Andrei Popescu402d9372010-02-26 13:31:12 +0000841 // 4. Shift arguments and return address one slot down on the stack
842 // (overwriting the original receiver). Adjust argument count to make
843 // the original first argument the new receiver.
844 __ bind(&shift_arguments);
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 { Label loop;
Leon Clarkee46be812010-01-19 14:06:41 +0000846 __ mov(ecx, eax);
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 __ bind(&loop);
848 __ mov(ebx, Operand(esp, ecx, times_4, 0));
849 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
850 __ dec(ecx);
Andrei Popescu402d9372010-02-26 13:31:12 +0000851 __ j(not_sign, &loop); // While non-negative (to copy return address).
Leon Clarkee46be812010-01-19 14:06:41 +0000852 __ pop(ebx); // Discard copy of return address.
853 __ dec(eax); // One fewer argument (first argument is new receiver).
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 }
855
Ben Murdoch589d6972011-11-30 16:04:58 +0000856 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
857 // or a function proxy via CALL_FUNCTION_PROXY.
858 { Label function, non_proxy;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100859 __ test(edx, edx);
Ben Murdoch589d6972011-11-30 16:04:58 +0000860 __ j(zero, &function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861 __ Move(ebx, Immediate(0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100862 __ cmp(edx, Immediate(1));
Ben Murdoch589d6972011-11-30 16:04:58 +0000863 __ j(not_equal, &non_proxy);
864
865 __ pop(edx); // return address
866 __ push(edi); // re-add proxy object as additional argument
867 __ push(edx);
868 __ inc(eax);
869 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
870 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
871 RelocInfo::CODE_TARGET);
872
873 __ bind(&non_proxy);
874 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +0100875 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Andrei Popescu402d9372010-02-26 13:31:12 +0000876 RelocInfo::CODE_TARGET);
877 __ bind(&function);
Steve Blocka7e24c12009-10-30 11:49:00 +0000878 }
879
Andrei Popescu402d9372010-02-26 13:31:12 +0000880 // 5b. Get the code to call from the function and check that the number of
881 // expected arguments matches what we're providing. If so, jump
882 // (tail-call) to the code in register edx without checking arguments.
883 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
884 __ mov(ebx,
885 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
Steve Block791712a2010-08-27 10:21:07 +0100886 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100887 __ SmiUntag(ebx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100888 __ cmp(eax, ebx);
Steve Block44f0eee2011-05-26 01:26:41 +0100889 __ j(not_equal,
890 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline());
Andrei Popescu402d9372010-02-26 13:31:12 +0000891
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 ParameterCount expected(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000893 __ InvokeCode(edx, expected, expected, JUMP_FUNCTION, NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +0000894}
895
896
897void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
Ben Murdoch589d6972011-11-30 16:04:58 +0000898 static const int kArgumentsOffset = 2 * kPointerSize;
899 static const int kReceiverOffset = 3 * kPointerSize;
900 static const int kFunctionOffset = 4 * kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100901 {
902 FrameScope frame_scope(masm, StackFrame::INTERNAL);
Ben Murdoch589d6972011-11-30 16:04:58 +0000903
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100904 __ push(Operand(ebp, kFunctionOffset)); // push this
905 __ push(Operand(ebp, kArgumentsOffset)); // push arguments
906 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
Steve Blocka7e24c12009-10-30 11:49:00 +0000907
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100908 // Check the stack for overflow. We are not trying to catch
909 // interruptions (e.g. debug break and preemption) here, so the "real stack
910 // limit" is checked.
911 Label okay;
912 ExternalReference real_stack_limit =
913 ExternalReference::address_of_real_stack_limit(masm->isolate());
914 __ mov(edi, Operand::StaticVariable(real_stack_limit));
915 // Make ecx the space we have left. The stack might already be overflowed
916 // here which will cause ecx to become negative.
917 __ mov(ecx, esp);
918 __ sub(ecx, edi);
919 // Make edx the space we need for the array when it is unrolled onto the
920 // stack.
921 __ mov(edx, eax);
922 __ shl(edx, kPointerSizeLog2 - kSmiTagSize);
923 // Check if the arguments will overflow the stack.
924 __ cmp(ecx, edx);
925 __ j(greater, &okay); // Signed comparison.
Steve Blocka7e24c12009-10-30 11:49:00 +0000926
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100927 // Out of stack space.
928 __ push(Operand(ebp, 4 * kPointerSize)); // push this
929 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100931 __ bind(&okay);
932 // End of stack check.
Steve Blocka7e24c12009-10-30 11:49:00 +0000933
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100934 // Push current index and limit.
935 const int kLimitOffset =
936 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
937 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
938 __ push(eax); // limit
939 __ push(Immediate(0)); // index
Steve Blocka7e24c12009-10-30 11:49:00 +0000940
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100941 // Get the receiver.
942 __ mov(ebx, Operand(ebp, kReceiverOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000943
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100944 // Check that the function is a JS function (otherwise it must be a proxy).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945 Label push_receiver, use_global_proxy;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100946 __ mov(edi, Operand(ebp, kFunctionOffset));
947 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
948 __ j(not_equal, &push_receiver);
Ben Murdoch589d6972011-11-30 16:04:58 +0000949
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100950 // Change context eagerly to get the right global object if necessary.
951 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +0000952
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100953 // Compute the receiver.
954 // Do not transform the receiver for strict mode functions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955 Label call_to_object;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100956 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
957 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
958 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
959 __ j(not_equal, &push_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000960
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100961 Factory* factory = masm->isolate()->factory();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100962
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100963 // Do not transform the receiver for natives (shared already in ecx).
964 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
965 1 << SharedFunctionInfo::kNativeBitWithinByte);
966 __ j(not_equal, &push_receiver);
Ben Murdoch257744e2011-11-30 15:57:28 +0000967
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968 // Compute the receiver in sloppy mode.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100969 // Call ToObject on the receiver if it is not an object, or use the
970 // global object if it is null or undefined.
971 __ JumpIfSmi(ebx, &call_to_object);
972 __ cmp(ebx, factory->null_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000973 __ j(equal, &use_global_proxy);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100974 __ cmp(ebx, factory->undefined_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000975 __ j(equal, &use_global_proxy);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100976 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
977 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
978 __ j(above_equal, &push_receiver);
Ben Murdoch257744e2011-11-30 15:57:28 +0000979
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100980 __ bind(&call_to_object);
981 __ push(ebx);
982 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
983 __ mov(ebx, eax);
984 __ jmp(&push_receiver);
Steve Blocka7e24c12009-10-30 11:49:00 +0000985
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000986 __ bind(&use_global_proxy);
987 __ mov(ebx,
988 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
989 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
Steve Blocka7e24c12009-10-30 11:49:00 +0000990
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100991 // Push the receiver.
992 __ bind(&push_receiver);
993 __ push(ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +0000994
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100995 // Copy all arguments from the array to the stack.
996 Label entry, loop;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 Register receiver = LoadDescriptor::ReceiverRegister();
998 Register key = LoadDescriptor::NameRegister();
999 __ mov(key, Operand(ebp, kIndexOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001000 __ jmp(&entry);
1001 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 __ mov(receiver, Operand(ebp, kArgumentsOffset)); // load arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001003
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 if (FLAG_vector_ics) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001005 // TODO(mvstanton): Vector-based ics need additional infrastructure to
1006 // be embedded here. For now, just call the runtime.
1007 __ push(receiver);
1008 __ push(key);
1009 __ CallRuntime(Runtime::kGetProperty, 2);
1010 } else {
1011 // Use inline caching to speed up access to arguments.
1012 Handle<Code> ic = CodeFactory::KeyedLoadIC(masm->isolate()).code();
1013 __ call(ic, RelocInfo::CODE_TARGET);
1014 // It is important that we do not have a test instruction after the
1015 // call. A test instruction after the call is used to indicate that
1016 // we have generated an inline version of the keyed load. In this
1017 // case, we know that we are not generating a test instruction next.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001019
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001020 // Push the nth argument.
1021 __ push(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001022
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023 // Update the index on the stack and in register key.
1024 __ mov(key, Operand(ebp, kIndexOffset));
1025 __ add(key, Immediate(1 << kSmiTagSize));
1026 __ mov(Operand(ebp, kIndexOffset), key);
Steve Blocka7e24c12009-10-30 11:49:00 +00001027
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001028 __ bind(&entry);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001029 __ cmp(key, Operand(ebp, kLimitOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001030 __ j(not_equal, &loop);
Steve Blocka7e24c12009-10-30 11:49:00 +00001031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 // Call the function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001033 Label call_proxy;
1034 ParameterCount actual(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035 __ Move(eax, key);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001036 __ SmiUntag(eax);
1037 __ mov(edi, Operand(ebp, kFunctionOffset));
1038 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1039 __ j(not_equal, &call_proxy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001040 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
Steve Blocka7e24c12009-10-30 11:49:00 +00001041
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001042 frame_scope.GenerateLeaveFrame();
1043 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001044
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001045 // Call the function proxy.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001046 __ bind(&call_proxy);
1047 __ push(edi); // add function proxy as last argument
1048 __ inc(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 __ Move(ebx, Immediate(0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001050 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
1051 __ call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1052 RelocInfo::CODE_TARGET);
Ben Murdoch589d6972011-11-30 16:04:58 +00001053
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001054 // Leave internal frame.
1055 }
Ben Murdoch589d6972011-11-30 16:04:58 +00001056 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
Steve Blocka7e24c12009-10-30 11:49:00 +00001057}
1058
1059
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001060void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
1061 // ----------- S t a t e -------------
1062 // -- eax : argc
1063 // -- esp[0] : return address
1064 // -- esp[4] : last argument
1065 // -----------------------------------
1066 Label generic_array_code;
1067
1068 // Get the InternalArray function.
1069 __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, edi);
1070
1071 if (FLAG_debug_code) {
1072 // Initial map for the builtin InternalArray function should be a map.
1073 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1074 // Will both indicate a NULL and a Smi.
1075 __ test(ebx, Immediate(kSmiTagMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001076 __ Assert(not_zero, kUnexpectedInitialMapForInternalArrayFunction);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001077 __ CmpObjectType(ebx, MAP_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078 __ Assert(equal, kUnexpectedInitialMapForInternalArrayFunction);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001079 }
1080
1081 // Run the native code for the InternalArray function called as a normal
1082 // function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083 // tail call a stub
1084 InternalArrayConstructorStub stub(masm->isolate());
1085 __ TailCallStub(&stub);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001086}
1087
1088
Steve Blocka7e24c12009-10-30 11:49:00 +00001089void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1090 // ----------- S t a t e -------------
1091 // -- eax : argc
1092 // -- esp[0] : return address
1093 // -- esp[4] : last argument
1094 // -----------------------------------
Kristian Monsen25f61362010-05-21 11:50:48 +01001095 Label generic_array_code;
Steve Blocka7e24c12009-10-30 11:49:00 +00001096
1097 // Get the Array function.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001098 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, edi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001099
1100 if (FLAG_debug_code) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001101 // Initial map for the builtin Array function should be a map.
Steve Blocka7e24c12009-10-30 11:49:00 +00001102 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1103 // Will both indicate a NULL and a Smi.
1104 __ test(ebx, Immediate(kSmiTagMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001105 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +00001106 __ CmpObjectType(ebx, MAP_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
Steve Blocka7e24c12009-10-30 11:49:00 +00001108 }
1109
1110 // Run the native code for the Array function called as a normal function.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111 // tail call a stub
1112 __ mov(ebx, masm->isolate()->factory()->undefined_value());
1113 ArrayConstructorStub stub(masm->isolate());
1114 __ TailCallStub(&stub);
Steve Blocka7e24c12009-10-30 11:49:00 +00001115}
1116
1117
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001118void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
1119 // ----------- S t a t e -------------
1120 // -- eax : number of arguments
1121 // -- edi : constructor function
1122 // -- esp[0] : return address
1123 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1124 // -- esp[(argc + 1) * 4] : receiver
1125 // -----------------------------------
Steve Block44f0eee2011-05-26 01:26:41 +01001126 Counters* counters = masm->isolate()->counters();
1127 __ IncrementCounter(counters->string_ctor_calls(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001128
1129 if (FLAG_debug_code) {
1130 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, ecx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001131 __ cmp(edi, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132 __ Assert(equal, kUnexpectedStringFunction);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001133 }
1134
1135 // Load the first argument into eax and get rid of the rest
1136 // (including the receiver).
1137 Label no_arguments;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001138 __ test(eax, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001139 __ j(zero, &no_arguments);
1140 __ mov(ebx, Operand(esp, eax, times_pointer_size, 0));
1141 __ pop(ecx);
1142 __ lea(esp, Operand(esp, eax, times_pointer_size, kPointerSize));
1143 __ push(ecx);
1144 __ mov(eax, ebx);
1145
1146 // Lookup the argument in the number to string cache.
1147 Label not_cached, argument_is_string;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001148 __ LookupNumberStringCache(eax, // Input.
1149 ebx, // Result.
1150 ecx, // Scratch 1.
1151 edx, // Scratch 2.
1152 &not_cached);
Steve Block44f0eee2011-05-26 01:26:41 +01001153 __ IncrementCounter(counters->string_ctor_cached_number(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001154 __ bind(&argument_is_string);
1155 // ----------- S t a t e -------------
1156 // -- ebx : argument converted to string
1157 // -- edi : constructor function
1158 // -- esp[0] : return address
1159 // -----------------------------------
1160
1161 // Allocate a JSValue and put the tagged pointer into eax.
1162 Label gc_required;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001163 __ Allocate(JSValue::kSize,
1164 eax, // Result.
1165 ecx, // New allocation top (we ignore it).
1166 no_reg,
1167 &gc_required,
1168 TAG_OBJECT);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001169
1170 // Set the map.
1171 __ LoadGlobalFunctionInitialMap(edi, ecx);
1172 if (FLAG_debug_code) {
1173 __ cmpb(FieldOperand(ecx, Map::kInstanceSizeOffset),
1174 JSValue::kSize >> kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001175 __ Assert(equal, kUnexpectedStringWrapperInstanceSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001176 __ cmpb(FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset), 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001177 __ Assert(equal, kUnexpectedUnusedPropertiesOfStringWrapper);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001178 }
1179 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ecx);
1180
1181 // Set properties and elements.
Steve Block44f0eee2011-05-26 01:26:41 +01001182 Factory* factory = masm->isolate()->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001183 __ Move(ecx, Immediate(factory->empty_fixed_array()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001184 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
1185 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ecx);
1186
1187 // Set the value.
1188 __ mov(FieldOperand(eax, JSValue::kValueOffset), ebx);
1189
1190 // Ensure the object is fully initialized.
1191 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1192
1193 // We're done. Return.
1194 __ ret(0);
1195
1196 // The argument was not found in the number to string cache. Check
1197 // if it's a string already before calling the conversion builtin.
1198 Label convert_argument;
1199 __ bind(&not_cached);
1200 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001201 __ JumpIfSmi(eax, &convert_argument);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001202 Condition is_string = masm->IsObjectStringType(eax, ebx, ecx);
1203 __ j(NegateCondition(is_string), &convert_argument);
1204 __ mov(ebx, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001205 __ IncrementCounter(counters->string_ctor_string_value(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001206 __ jmp(&argument_is_string);
1207
1208 // Invoke the conversion builtin and put the result into ebx.
1209 __ bind(&convert_argument);
Steve Block44f0eee2011-05-26 01:26:41 +01001210 __ IncrementCounter(counters->string_ctor_conversions(), 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001211 {
1212 FrameScope scope(masm, StackFrame::INTERNAL);
1213 __ push(edi); // Preserve the function.
1214 __ push(eax);
1215 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
1216 __ pop(edi);
1217 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001218 __ mov(ebx, eax);
1219 __ jmp(&argument_is_string);
1220
1221 // Load the empty string into ebx, remove the receiver from the
1222 // stack, and jump back to the case where the argument is a string.
1223 __ bind(&no_arguments);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 __ Move(ebx, Immediate(factory->empty_string()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001225 __ pop(ecx);
1226 __ lea(esp, Operand(esp, kPointerSize));
1227 __ push(ecx);
1228 __ jmp(&argument_is_string);
1229
1230 // At this point the argument is already a string. Call runtime to
1231 // create a string wrapper.
1232 __ bind(&gc_required);
Steve Block44f0eee2011-05-26 01:26:41 +01001233 __ IncrementCounter(counters->string_ctor_gc_required(), 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001234 {
1235 FrameScope scope(masm, StackFrame::INTERNAL);
1236 __ push(ebx);
1237 __ CallRuntime(Runtime::kNewStringWrapper, 1);
1238 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001239 __ ret(0);
1240}
1241
1242
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243static void ArgumentsAdaptorStackCheck(MacroAssembler* masm,
1244 Label* stack_overflow) {
1245 // ----------- S t a t e -------------
1246 // -- eax : actual number of arguments
1247 // -- ebx : expected number of arguments
1248 // -- edi : function (passed through to callee)
1249 // -----------------------------------
1250 // Check the stack for overflow. We are not trying to catch
1251 // interruptions (e.g. debug break and preemption) here, so the "real stack
1252 // limit" is checked.
1253 ExternalReference real_stack_limit =
1254 ExternalReference::address_of_real_stack_limit(masm->isolate());
1255 __ mov(edx, Operand::StaticVariable(real_stack_limit));
1256 // Make ecx the space we have left. The stack might already be overflowed
1257 // here which will cause ecx to become negative.
1258 __ mov(ecx, esp);
1259 __ sub(ecx, edx);
1260 // Make edx the space we need for the array when it is unrolled onto the
1261 // stack.
1262 __ mov(edx, ebx);
1263 __ shl(edx, kPointerSizeLog2);
1264 // Check if the arguments will overflow the stack.
1265 __ cmp(ecx, edx);
1266 __ j(less_equal, stack_overflow); // Signed comparison.
1267}
1268
1269
Steve Blocka7e24c12009-10-30 11:49:00 +00001270static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1271 __ push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001272 __ mov(ebp, esp);
Steve Blocka7e24c12009-10-30 11:49:00 +00001273
1274 // Store the arguments adaptor context sentinel.
1275 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1276
1277 // Push the function on the stack.
1278 __ push(edi);
1279
Ben Murdoch257744e2011-11-30 15:57:28 +00001280 // Preserve the number of arguments on the stack. Must preserve eax,
1281 // ebx and ecx because these registers are used when copying the
Steve Blocka7e24c12009-10-30 11:49:00 +00001282 // arguments and the receiver.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001283 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001284 __ lea(edi, Operand(eax, eax, times_1, kSmiTag));
1285 __ push(edi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001286}
1287
1288
1289static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1290 // Retrieve the number of arguments from the stack.
1291 __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1292
1293 // Leave the frame.
1294 __ leave();
1295
1296 // Remove caller arguments from the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001297 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Steve Blocka7e24c12009-10-30 11:49:00 +00001298 __ pop(ecx);
1299 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
1300 __ push(ecx);
1301}
1302
1303
1304void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1305 // ----------- S t a t e -------------
1306 // -- eax : actual number of arguments
1307 // -- ebx : expected number of arguments
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308 // -- edi : function (passed through to callee)
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 // -----------------------------------
1310
1311 Label invoke, dont_adapt_arguments;
Steve Block44f0eee2011-05-26 01:26:41 +01001312 __ IncrementCounter(masm->isolate()->counters()->arguments_adaptors(), 1);
Steve Blocka7e24c12009-10-30 11:49:00 +00001313
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314 Label stack_overflow;
1315 ArgumentsAdaptorStackCheck(masm, &stack_overflow);
1316
Steve Blocka7e24c12009-10-30 11:49:00 +00001317 Label enough, too_few;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001319 __ cmp(eax, ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001320 __ j(less, &too_few);
1321 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1322 __ j(equal, &dont_adapt_arguments);
1323
1324 { // Enough parameters: Actual >= expected.
1325 __ bind(&enough);
1326 EnterArgumentsAdaptorFrame(masm);
1327
1328 // Copy receiver and all expected arguments.
1329 const int offset = StandardFrameConstants::kCallerSPOffset;
1330 __ lea(eax, Operand(ebp, eax, times_4, offset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001331 __ mov(edi, -1); // account for receiver
Steve Blocka7e24c12009-10-30 11:49:00 +00001332
1333 Label copy;
1334 __ bind(&copy);
Ben Murdoch257744e2011-11-30 15:57:28 +00001335 __ inc(edi);
Steve Blocka7e24c12009-10-30 11:49:00 +00001336 __ push(Operand(eax, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001337 __ sub(eax, Immediate(kPointerSize));
1338 __ cmp(edi, ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001339 __ j(less, &copy);
1340 __ jmp(&invoke);
1341 }
1342
1343 { // Too few parameters: Actual < expected.
1344 __ bind(&too_few);
1345 EnterArgumentsAdaptorFrame(masm);
1346
1347 // Copy receiver and all actual arguments.
1348 const int offset = StandardFrameConstants::kCallerSPOffset;
1349 __ lea(edi, Operand(ebp, eax, times_4, offset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001350 // ebx = expected - actual.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001351 __ sub(ebx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00001352 // eax = -actual - 1
1353 __ neg(eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001354 __ sub(eax, Immediate(1));
Steve Blocka7e24c12009-10-30 11:49:00 +00001355
1356 Label copy;
1357 __ bind(&copy);
Ben Murdoch257744e2011-11-30 15:57:28 +00001358 __ inc(eax);
Steve Blocka7e24c12009-10-30 11:49:00 +00001359 __ push(Operand(edi, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001360 __ sub(edi, Immediate(kPointerSize));
1361 __ test(eax, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00001362 __ j(not_zero, &copy);
Steve Blocka7e24c12009-10-30 11:49:00 +00001363
1364 // Fill remaining expected arguments with undefined values.
1365 Label fill;
1366 __ bind(&fill);
Ben Murdoch257744e2011-11-30 15:57:28 +00001367 __ inc(eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001368 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001369 __ cmp(eax, ebx);
Steve Blocka7e24c12009-10-30 11:49:00 +00001370 __ j(less, &fill);
Steve Blocka7e24c12009-10-30 11:49:00 +00001371 }
1372
1373 // Call the entry point.
1374 __ bind(&invoke);
Ben Murdoch257744e2011-11-30 15:57:28 +00001375 // Restore function pointer.
1376 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001377 __ call(edx);
1378
1379 // Store offset of return address for deoptimizer.
1380 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01001381
Steve Blocka7e24c12009-10-30 11:49:00 +00001382 // Leave frame and return.
1383 LeaveArgumentsAdaptorFrame(masm);
1384 __ ret(0);
1385
1386 // -------------------------------------------
1387 // Dont adapt arguments.
1388 // -------------------------------------------
1389 __ bind(&dont_adapt_arguments);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001390 __ jmp(edx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391
1392 __ bind(&stack_overflow);
1393 {
1394 FrameScope frame(masm, StackFrame::MANUAL);
1395 EnterArgumentsAdaptorFrame(masm);
1396 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1397 __ int3();
1398 }
Steve Blocka7e24c12009-10-30 11:49:00 +00001399}
1400
1401
Ben Murdochb0fe1622011-05-05 13:52:32 +01001402void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 // Lookup the function in the JavaScript frame.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001404 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001405 {
1406 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 // Pass function as argument.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001408 __ push(eax);
1409 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1410 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001411
Ben Murdoch257744e2011-11-30 15:57:28 +00001412 Label skip;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413 // If the code object is null, just return to the unoptimized code.
1414 __ cmp(eax, Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001415 __ j(not_equal, &skip, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001416 __ ret(0);
1417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 __ bind(&skip);
1419
1420 // Load deoptimization data from the code object.
1421 __ mov(ebx, Operand(eax, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1422
1423 // Load the OSR entrypoint offset from the deoptimization data.
1424 __ mov(ebx, Operand(ebx, FixedArray::OffsetOfElementAt(
1425 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1426 __ SmiUntag(ebx);
1427
1428 // Compute the target address = code_obj + header_size + osr_offset
1429 __ lea(eax, Operand(eax, ebx, times_1, Code::kHeaderSize - kHeapObjectTag));
1430
1431 // Overwrite the return address on the stack.
1432 __ mov(Operand(esp, 0), eax);
1433
1434 // And "return" to the OSR entry point of the function.
1435 __ ret(0);
1436}
1437
1438
1439void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1440 // We check the stack limit as indicator that recompilation might be done.
Ben Murdoch257744e2011-11-30 15:57:28 +00001441 Label ok;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001442 ExternalReference stack_limit =
Steve Block44f0eee2011-05-26 01:26:41 +01001443 ExternalReference::address_of_stack_limit(masm->isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001444 __ cmp(esp, Operand::StaticVariable(stack_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00001445 __ j(above_equal, &ok, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001446 {
1447 FrameScope scope(masm, StackFrame::INTERNAL);
1448 __ CallRuntime(Runtime::kStackGuard, 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001449 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 __ jmp(masm->isolate()->builtins()->OnStackReplacement(),
1451 RelocInfo::CODE_TARGET);
1452
Ben Murdochb0fe1622011-05-05 13:52:32 +01001453 __ bind(&ok);
1454 __ ret(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001455}
1456
Steve Blocka7e24c12009-10-30 11:49:00 +00001457#undef __
Steve Block44f0eee2011-05-26 01:26:41 +01001458}
1459} // namespace v8::internal
Leon Clarkef7060e22010-06-03 12:02:55 +01001460
1461#endif // V8_TARGET_ARCH_IA32