1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/code-stubs.h"
6 #include "src/compiler.h"
7 #include "src/compiler/common-operator.h"
8 #include "src/compiler/frame.h"
9 #include "src/compiler/linkage.h"
10 #include "src/compiler/node.h"
11 #include "src/compiler/osr.h"
12 #include "src/compiler/pipeline.h"
13 #include "src/scopes.h"
20 LinkageLocation regloc(Register reg) {
21 return LinkageLocation::ForRegister(Register::ToAllocationIndex(reg));
25 MachineType reptyp(Representation representation) {
26 switch (representation.kind()) {
27 case Representation::kInteger8:
29 case Representation::kUInteger8:
31 case Representation::kInteger16:
33 case Representation::kUInteger16:
35 case Representation::kInteger32:
37 case Representation::kSmi:
38 case Representation::kTagged:
39 case Representation::kHeapObject:
40 return kMachAnyTagged;
41 case Representation::kDouble:
43 case Representation::kExternal:
45 case Representation::kNone:
46 case Representation::kNumRepresentations:
55 std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
57 case CallDescriptor::kCallCodeObject:
60 case CallDescriptor::kCallJSFunction:
63 case CallDescriptor::kCallAddress:
71 std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
72 // TODO(svenpanne) Output properties etc. and be less cryptic.
73 return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
74 << "s" << d.StackParameterCount() << "i" << d.InputCount() << "f"
75 << d.FrameStateCount() << "t" << d.SupportsTailCalls();
79 bool CallDescriptor::HasSameReturnLocationsAs(
80 const CallDescriptor* other) const {
81 if (ReturnCount() != other->ReturnCount()) return false;
82 for (size_t i = 0; i < ReturnCount(); ++i) {
83 if (GetReturnLocation(i) != other->GetReturnLocation(i)) return false;
89 bool CallDescriptor::CanTailCall(const Node* node) const {
90 // Determine the number of stack parameters passed in
91 size_t stack_params = 0;
92 for (size_t i = 0; i < InputCount(); ++i) {
93 if (!GetInputLocation(i).IsRegister()) {
97 // Ensure the input linkage contains the stack parameters in the right order
98 size_t current_stack_param = 0;
99 for (size_t i = 0; i < InputCount(); ++i) {
100 if (!GetInputLocation(i).IsRegister()) {
101 if (GetInputLocation(i) != LinkageLocation::ForCallerFrameSlot(
102 static_cast<int>(current_stack_param) -
103 static_cast<int>(stack_params))) {
106 ++current_stack_param;
109 // Tail calling is currently allowed if return locations match and all
110 // parameters are either in registers or on the stack but match exactly in
111 // number and content.
112 CallDescriptor const* other = OpParameter<CallDescriptor const*>(node);
113 if (!HasSameReturnLocationsAs(other)) return false;
114 size_t current_input = 0;
115 size_t other_input = 0;
117 if (other_input >= other->InputCount()) {
118 while (current_input < InputCount()) {
119 if (!GetInputLocation(current_input).IsRegister()) {
126 if (current_input >= InputCount()) {
127 while (other_input < other->InputCount()) {
128 if (!other->GetInputLocation(other_input).IsRegister()) {
135 if (GetInputLocation(current_input).IsRegister()) {
139 if (other->GetInputLocation(other_input).IsRegister()) {
143 if (GetInputLocation(current_input) !=
144 other->GetInputLocation(other_input)) {
147 Node* input = node->InputAt(static_cast<int>(other_input));
148 if (input->opcode() != IrOpcode::kParameter) {
151 // Make sure that the parameter input passed through to the tail call
152 // corresponds to the correct stack slot.
153 size_t param_index = ParameterIndexOf(input->op());
154 if (param_index != current_input - 1) {
165 CallDescriptor* Linkage::ComputeIncoming(Zone* zone, CompilationInfo* info) {
166 if (info->code_stub() != NULL) {
167 // Use the code stub interface descriptor.
168 CodeStub* stub = info->code_stub();
169 CallInterfaceDescriptor descriptor = stub->GetCallInterfaceDescriptor();
170 return GetStubCallDescriptor(
171 info->isolate(), zone, descriptor, stub->GetStackParameterCount(),
172 CallDescriptor::kNoFlags, Operator::kNoProperties);
174 if (info->has_literal()) {
175 // If we already have the function literal, use the number of parameters
176 // plus the receiver.
177 return GetJSCallDescriptor(zone, info->is_osr(),
178 1 + info->literal()->parameter_count(),
179 CallDescriptor::kNoFlags);
181 if (!info->closure().is_null()) {
182 // If we are compiling a JS function, use a JS call descriptor,
183 // plus the receiver.
184 SharedFunctionInfo* shared = info->closure()->shared();
185 return GetJSCallDescriptor(zone, info->is_osr(),
186 1 + shared->internal_formal_parameter_count(),
187 CallDescriptor::kNoFlags);
189 return NULL; // TODO(titzer): ?
193 FrameOffset Linkage::GetFrameOffset(int spill_slot, Frame* frame) const {
194 bool has_frame = frame->GetSpillSlotCount() > 0 ||
195 incoming_->IsJSFunctionCall() ||
196 incoming_->kind() == CallDescriptor::kCallAddress;
198 (StandardFrameConstants::kFixedSlotCountAboveFp - spill_slot - 1) *
201 return FrameOffset::FromFramePointer(offset);
203 // No frame. Retrieve all parameters relative to stack pointer.
204 DCHECK(spill_slot < 0); // Must be a parameter.
206 kPointerSize * (StandardFrameConstants::kFixedSlotCountAboveFp -
207 frame->GetTotalFrameSlotCount());
208 return FrameOffset::FromStackPointer(offset - offsetSpToFp);
214 int Linkage::FrameStateInputCount(Runtime::FunctionId function) {
215 // Most runtime functions need a FrameState. A few chosen ones that we know
216 // not to call into arbitrary JavaScript, not to throw, and not to deoptimize
217 // are blacklisted here and can be called without a FrameState.
219 case Runtime::kAllocateInTargetSpace:
220 case Runtime::kDateField:
221 case Runtime::kFinalizeClassDefinition: // TODO(conradw): Is it safe?
222 case Runtime::kDefineClassMethod: // TODO(jarin): Is it safe?
223 case Runtime::kDefineGetterPropertyUnchecked: // TODO(jarin): Is it safe?
224 case Runtime::kDefineSetterPropertyUnchecked: // TODO(jarin): Is it safe?
225 case Runtime::kForInDone:
226 case Runtime::kForInStep:
227 case Runtime::kGetOriginalConstructor:
228 case Runtime::kNewArguments:
229 case Runtime::kNewClosure:
230 case Runtime::kNewFunctionContext:
231 case Runtime::kNewRestParamSlow:
232 case Runtime::kPushBlockContext:
233 case Runtime::kPushCatchContext:
234 case Runtime::kReThrow:
235 case Runtime::kStringCompare:
236 case Runtime::kStringEquals:
237 case Runtime::kToFastProperties: // TODO(jarin): Is it safe?
238 case Runtime::kTraceEnter:
239 case Runtime::kTraceExit:
241 case Runtime::kInlineArguments:
242 case Runtime::kInlineCallFunction:
243 case Runtime::kInlineDefaultConstructorCallSuper:
244 case Runtime::kInlineGetCallerJSFunction:
245 case Runtime::kInlineGetPrototype:
246 case Runtime::kInlineRegExpExec:
247 case Runtime::kInlineToObject:
248 case Runtime::kInlineToPrimitive:
249 case Runtime::kInlineToPrimitive_Number:
250 case Runtime::kInlineToPrimitive_String:
251 case Runtime::kInlineOrdinaryToPrimitive:
252 case Runtime::kInlineToNumber:
253 case Runtime::kInlineToString:
254 case Runtime::kInlineToName:
256 case Runtime::kInlineDeoptimizeNow:
257 case Runtime::kInlineThrowNotDateError:
263 // Most inlined runtime functions (except the ones listed above) can be called
264 // without a FrameState or will be lowered by JSIntrinsicLowering internally.
265 const Runtime::Function* const f = Runtime::FunctionForId(function);
266 if (f->intrinsic_type == Runtime::IntrinsicType::INLINE) return 0;
272 bool CallDescriptor::UsesOnlyRegisters() const {
273 for (size_t i = 0; i < InputCount(); ++i) {
274 if (!GetInputLocation(i).IsRegister()) return false;
276 for (size_t i = 0; i < ReturnCount(); ++i) {
277 if (!GetReturnLocation(i).IsRegister()) return false;
283 CallDescriptor* Linkage::GetRuntimeCallDescriptor(
284 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
285 Operator::Properties properties) {
286 const size_t function_count = 1;
287 const size_t num_args_count = 1;
288 const size_t context_count = 1;
289 const size_t parameter_count = function_count +
290 static_cast<size_t>(js_parameter_count) +
291 num_args_count + context_count;
293 const Runtime::Function* function = Runtime::FunctionForId(function_id);
294 const size_t return_count = static_cast<size_t>(function->result_size);
296 LocationSignature::Builder locations(zone, return_count, parameter_count);
297 MachineSignature::Builder types(zone, return_count, parameter_count);
300 if (locations.return_count_ > 0) {
301 locations.AddReturn(regloc(kReturnRegister0));
303 if (locations.return_count_ > 1) {
304 locations.AddReturn(regloc(kReturnRegister1));
306 for (size_t i = 0; i < return_count; i++) {
307 types.AddReturn(kMachAnyTagged);
310 // All parameters to the runtime call go on the stack.
311 for (int i = 0; i < js_parameter_count; i++) {
313 LinkageLocation::ForCallerFrameSlot(i - js_parameter_count));
314 types.AddParam(kMachAnyTagged);
316 // Add runtime function itself.
317 locations.AddParam(regloc(kRuntimeCallFunctionRegister));
318 types.AddParam(kMachAnyTagged);
320 // Add runtime call argument count.
321 locations.AddParam(regloc(kRuntimeCallArgCountRegister));
322 types.AddParam(kMachPtr);
325 locations.AddParam(regloc(kContextRegister));
326 types.AddParam(kMachAnyTagged);
328 CallDescriptor::Flags flags = Linkage::FrameStateInputCount(function_id) > 0
329 ? CallDescriptor::kNeedsFrameState
330 : CallDescriptor::kNoFlags;
332 // The target for runtime calls is a code object.
333 MachineType target_type = kMachAnyTagged;
334 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
335 return new (zone) CallDescriptor( // --
336 CallDescriptor::kCallCodeObject, // kind
337 target_type, // target MachineType
338 target_loc, // target location
339 types.Build(), // machine_sig
340 locations.Build(), // location_sig
341 js_parameter_count, // stack_parameter_count
342 properties, // properties
343 kNoCalleeSaved, // callee-saved
344 kNoCalleeSaved, // callee-saved fp
346 function->name); // debug name
350 CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
351 int js_parameter_count,
352 CallDescriptor::Flags flags) {
353 const size_t return_count = 1;
354 const size_t context_count = 1;
355 const size_t parameter_count = js_parameter_count + context_count;
357 LocationSignature::Builder locations(zone, return_count, parameter_count);
358 MachineSignature::Builder types(zone, return_count, parameter_count);
360 // All JS calls have exactly one return value.
361 locations.AddReturn(regloc(kReturnRegister0));
362 types.AddReturn(kMachAnyTagged);
364 // All parameters to JS calls go on the stack.
365 for (int i = 0; i < js_parameter_count; i++) {
366 int spill_slot_index = i - js_parameter_count;
367 locations.AddParam(LinkageLocation::ForCallerFrameSlot(spill_slot_index));
368 types.AddParam(kMachAnyTagged);
371 locations.AddParam(regloc(kContextRegister));
372 types.AddParam(kMachAnyTagged);
374 // The target for JS function calls is the JSFunction object.
375 MachineType target_type = kMachAnyTagged;
376 // TODO(titzer): When entering into an OSR function from unoptimized code,
377 // the JSFunction is not in a register, but it is on the stack in an
378 // unaddressable spill slot. We hack this in the OSR prologue. Fix.
379 LinkageLocation target_loc = regloc(kJSFunctionRegister);
380 return new (zone) CallDescriptor( // --
381 CallDescriptor::kCallJSFunction, // kind
382 target_type, // target MachineType
383 target_loc, // target location
384 types.Build(), // machine_sig
385 locations.Build(), // location_sig
386 js_parameter_count, // stack_parameter_count
387 Operator::kNoProperties, // properties
388 kNoCalleeSaved, // callee-saved
389 kNoCalleeSaved, // callee-saved fp
390 CallDescriptor::kCanUseRoots | // flags
396 CallDescriptor* Linkage::GetInterpreterDispatchDescriptor(Zone* zone) {
397 MachineSignature::Builder types(zone, 0, 6);
398 LocationSignature::Builder locations(zone, 0, 6);
400 // Add registers for fixed parameters passed via interpreter dispatch.
401 STATIC_ASSERT(0 == Linkage::kInterpreterAccumulatorParameter);
402 types.AddParam(kMachAnyTagged);
403 locations.AddParam(regloc(kInterpreterAccumulatorRegister));
405 STATIC_ASSERT(1 == Linkage::kInterpreterRegisterFileParameter);
406 types.AddParam(kMachPtr);
407 locations.AddParam(regloc(kInterpreterRegisterFileRegister));
409 STATIC_ASSERT(2 == Linkage::kInterpreterBytecodeOffsetParameter);
410 types.AddParam(kMachIntPtr);
411 locations.AddParam(regloc(kInterpreterBytecodeOffsetRegister));
413 STATIC_ASSERT(3 == Linkage::kInterpreterBytecodeArrayParameter);
414 types.AddParam(kMachAnyTagged);
415 locations.AddParam(regloc(kInterpreterBytecodeArrayRegister));
417 STATIC_ASSERT(4 == Linkage::kInterpreterDispatchTableParameter);
418 types.AddParam(kMachPtr);
419 locations.AddParam(regloc(kInterpreterDispatchTableRegister));
421 STATIC_ASSERT(5 == Linkage::kInterpreterContextParameter);
422 types.AddParam(kMachAnyTagged);
423 #if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_X87)
425 LinkageLocation::ForCallerFrameSlot(kInterpreterContextSpillSlot));
427 locations.AddParam(regloc(kContextRegister));
430 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
431 return new (zone) CallDescriptor( // --
432 CallDescriptor::kCallCodeObject, // kind
433 kMachNone, // target MachineType
434 target_loc, // target location
435 types.Build(), // machine_sig
436 locations.Build(), // location_sig
437 0, // stack_parameter_count
438 Operator::kNoProperties, // properties
439 kNoCalleeSaved, // callee-saved registers
440 kNoCalleeSaved, // callee-saved fp regs
441 CallDescriptor::kSupportsTailCalls | // flags
442 CallDescriptor::kCanUseRoots, // flags
443 "interpreter-dispatch");
447 // TODO(all): Add support for return representations/locations to
448 // CallInterfaceDescriptor.
449 // TODO(turbofan): cache call descriptors for code stub calls.
450 CallDescriptor* Linkage::GetStubCallDescriptor(
451 Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
452 int stack_parameter_count, CallDescriptor::Flags flags,
453 Operator::Properties properties, MachineType return_type) {
454 const int register_parameter_count = descriptor.GetRegisterParameterCount();
455 const int js_parameter_count =
456 register_parameter_count + stack_parameter_count;
457 const int context_count = 1;
458 const size_t return_count = 1;
459 const size_t parameter_count =
460 static_cast<size_t>(js_parameter_count + context_count);
462 LocationSignature::Builder locations(zone, return_count, parameter_count);
463 MachineSignature::Builder types(zone, return_count, parameter_count);
465 // Add return location.
466 locations.AddReturn(regloc(kReturnRegister0));
467 types.AddReturn(return_type);
469 // Add parameters in registers and on the stack.
470 for (int i = 0; i < js_parameter_count; i++) {
471 if (i < register_parameter_count) {
472 // The first parameters go in registers.
473 Register reg = descriptor.GetRegisterParameter(i);
475 RepresentationFromType(descriptor.GetParameterType(i));
476 locations.AddParam(regloc(reg));
477 types.AddParam(reptyp(rep));
479 // The rest of the parameters go on the stack.
480 int stack_slot = i - register_parameter_count - stack_parameter_count;
481 locations.AddParam(LinkageLocation::ForCallerFrameSlot(stack_slot));
482 types.AddParam(kMachAnyTagged);
486 locations.AddParam(regloc(kContextRegister));
487 types.AddParam(kMachAnyTagged);
489 // The target for stub calls is a code object.
490 MachineType target_type = kMachAnyTagged;
491 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
492 return new (zone) CallDescriptor( // --
493 CallDescriptor::kCallCodeObject, // kind
494 target_type, // target MachineType
495 target_loc, // target location
496 types.Build(), // machine_sig
497 locations.Build(), // location_sig
498 stack_parameter_count, // stack_parameter_count
499 properties, // properties
500 kNoCalleeSaved, // callee-saved registers
501 kNoCalleeSaved, // callee-saved fp
503 descriptor.DebugName(isolate));
507 LinkageLocation Linkage::GetOsrValueLocation(int index) const {
508 CHECK(incoming_->IsJSFunctionCall());
509 int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
510 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
512 if (index == kOsrContextSpillSlotIndex) {
513 // Context. Use the parameter location of the context spill slot.
514 // Parameter (arity + 1) is special for the context of the function frame.
515 int context_index = 1 + 1 + parameter_count; // target + receiver + params
516 return incoming_->GetInputLocation(context_index);
517 } else if (index >= first_stack_slot) {
518 // Local variable stored in this (callee) stack.
520 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
521 return LinkageLocation::ForCalleeFrameSlot(spill_index);
523 // Parameter. Use the assigned location from the incoming call descriptor.
524 int parameter_index = 1 + index; // skip index 0, which is the target.
525 return incoming_->GetInputLocation(parameter_index);
528 } // namespace compiler
529 } // namespace internal