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:
254 case Runtime::kInlineDeoptimizeNow:
255 case Runtime::kInlineThrowNotDateError:
261 // Most inlined runtime functions (except the ones listed above) can be called
262 // without a FrameState or will be lowered by JSIntrinsicLowering internally.
263 const Runtime::Function* const f = Runtime::FunctionForId(function);
264 if (f->intrinsic_type == Runtime::IntrinsicType::INLINE) return 0;
270 bool CallDescriptor::UsesOnlyRegisters() const {
271 for (size_t i = 0; i < InputCount(); ++i) {
272 if (!GetInputLocation(i).IsRegister()) return false;
274 for (size_t i = 0; i < ReturnCount(); ++i) {
275 if (!GetReturnLocation(i).IsRegister()) return false;
281 CallDescriptor* Linkage::GetRuntimeCallDescriptor(
282 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
283 Operator::Properties properties) {
284 const size_t function_count = 1;
285 const size_t num_args_count = 1;
286 const size_t context_count = 1;
287 const size_t parameter_count = function_count +
288 static_cast<size_t>(js_parameter_count) +
289 num_args_count + context_count;
291 const Runtime::Function* function = Runtime::FunctionForId(function_id);
292 const size_t return_count = static_cast<size_t>(function->result_size);
294 LocationSignature::Builder locations(zone, return_count, parameter_count);
295 MachineSignature::Builder types(zone, return_count, parameter_count);
298 if (locations.return_count_ > 0) {
299 locations.AddReturn(regloc(kReturnRegister0));
301 if (locations.return_count_ > 1) {
302 locations.AddReturn(regloc(kReturnRegister1));
304 for (size_t i = 0; i < return_count; i++) {
305 types.AddReturn(kMachAnyTagged);
308 // All parameters to the runtime call go on the stack.
309 for (int i = 0; i < js_parameter_count; i++) {
311 LinkageLocation::ForCallerFrameSlot(i - js_parameter_count));
312 types.AddParam(kMachAnyTagged);
314 // Add runtime function itself.
315 locations.AddParam(regloc(kRuntimeCallFunctionRegister));
316 types.AddParam(kMachAnyTagged);
318 // Add runtime call argument count.
319 locations.AddParam(regloc(kRuntimeCallArgCountRegister));
320 types.AddParam(kMachPtr);
323 locations.AddParam(regloc(kContextRegister));
324 types.AddParam(kMachAnyTagged);
326 CallDescriptor::Flags flags = Linkage::FrameStateInputCount(function_id) > 0
327 ? CallDescriptor::kNeedsFrameState
328 : CallDescriptor::kNoFlags;
330 // The target for runtime calls is a code object.
331 MachineType target_type = kMachAnyTagged;
332 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
333 return new (zone) CallDescriptor( // --
334 CallDescriptor::kCallCodeObject, // kind
335 target_type, // target MachineType
336 target_loc, // target location
337 types.Build(), // machine_sig
338 locations.Build(), // location_sig
339 js_parameter_count, // stack_parameter_count
340 properties, // properties
341 kNoCalleeSaved, // callee-saved
342 kNoCalleeSaved, // callee-saved fp
344 function->name); // debug name
348 CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
349 int js_parameter_count,
350 CallDescriptor::Flags flags) {
351 const size_t return_count = 1;
352 const size_t context_count = 1;
353 const size_t parameter_count = js_parameter_count + context_count;
355 LocationSignature::Builder locations(zone, return_count, parameter_count);
356 MachineSignature::Builder types(zone, return_count, parameter_count);
358 // All JS calls have exactly one return value.
359 locations.AddReturn(regloc(kReturnRegister0));
360 types.AddReturn(kMachAnyTagged);
362 // All parameters to JS calls go on the stack.
363 for (int i = 0; i < js_parameter_count; i++) {
364 int spill_slot_index = i - js_parameter_count;
365 locations.AddParam(LinkageLocation::ForCallerFrameSlot(spill_slot_index));
366 types.AddParam(kMachAnyTagged);
369 locations.AddParam(regloc(kContextRegister));
370 types.AddParam(kMachAnyTagged);
372 // The target for JS function calls is the JSFunction object.
373 MachineType target_type = kMachAnyTagged;
374 // TODO(titzer): When entering into an OSR function from unoptimized code,
375 // the JSFunction is not in a register, but it is on the stack in an
376 // unaddressable spill slot. We hack this in the OSR prologue. Fix.
377 LinkageLocation target_loc = regloc(kJSFunctionRegister);
378 return new (zone) CallDescriptor( // --
379 CallDescriptor::kCallJSFunction, // kind
380 target_type, // target MachineType
381 target_loc, // target location
382 types.Build(), // machine_sig
383 locations.Build(), // location_sig
384 js_parameter_count, // stack_parameter_count
385 Operator::kNoProperties, // properties
386 kNoCalleeSaved, // callee-saved
387 kNoCalleeSaved, // callee-saved fp
388 CallDescriptor::kCanUseRoots | // flags
394 CallDescriptor* Linkage::GetInterpreterDispatchDescriptor(Zone* zone) {
395 MachineSignature::Builder types(zone, 0, 6);
396 LocationSignature::Builder locations(zone, 0, 6);
398 // Add registers for fixed parameters passed via interpreter dispatch.
399 STATIC_ASSERT(0 == Linkage::kInterpreterAccumulatorParameter);
400 types.AddParam(kMachAnyTagged);
401 locations.AddParam(regloc(kInterpreterAccumulatorRegister));
403 STATIC_ASSERT(1 == Linkage::kInterpreterRegisterFileParameter);
404 types.AddParam(kMachPtr);
405 locations.AddParam(regloc(kInterpreterRegisterFileRegister));
407 STATIC_ASSERT(2 == Linkage::kInterpreterBytecodeOffsetParameter);
408 types.AddParam(kMachIntPtr);
409 locations.AddParam(regloc(kInterpreterBytecodeOffsetRegister));
411 STATIC_ASSERT(3 == Linkage::kInterpreterBytecodeArrayParameter);
412 types.AddParam(kMachAnyTagged);
413 locations.AddParam(regloc(kInterpreterBytecodeArrayRegister));
415 STATIC_ASSERT(4 == Linkage::kInterpreterDispatchTableParameter);
416 types.AddParam(kMachPtr);
417 locations.AddParam(regloc(kInterpreterDispatchTableRegister));
419 STATIC_ASSERT(5 == Linkage::kInterpreterContextParameter);
420 types.AddParam(kMachAnyTagged);
421 #if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_X87)
423 LinkageLocation::ForCallerFrameSlot(kInterpreterContextSpillSlot));
425 locations.AddParam(regloc(kContextRegister));
428 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
429 return new (zone) CallDescriptor( // --
430 CallDescriptor::kCallCodeObject, // kind
431 kMachNone, // target MachineType
432 target_loc, // target location
433 types.Build(), // machine_sig
434 locations.Build(), // location_sig
435 0, // stack_parameter_count
436 Operator::kNoProperties, // properties
437 kNoCalleeSaved, // callee-saved registers
438 kNoCalleeSaved, // callee-saved fp regs
439 CallDescriptor::kSupportsTailCalls | // flags
440 CallDescriptor::kCanUseRoots, // flags
441 "interpreter-dispatch");
445 // TODO(all): Add support for return representations/locations to
446 // CallInterfaceDescriptor.
447 // TODO(turbofan): cache call descriptors for code stub calls.
448 CallDescriptor* Linkage::GetStubCallDescriptor(
449 Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
450 int stack_parameter_count, CallDescriptor::Flags flags,
451 Operator::Properties properties, MachineType return_type) {
452 const int register_parameter_count = descriptor.GetRegisterParameterCount();
453 const int js_parameter_count =
454 register_parameter_count + stack_parameter_count;
455 const int context_count = 1;
456 const size_t return_count = 1;
457 const size_t parameter_count =
458 static_cast<size_t>(js_parameter_count + context_count);
460 LocationSignature::Builder locations(zone, return_count, parameter_count);
461 MachineSignature::Builder types(zone, return_count, parameter_count);
463 // Add return location.
464 locations.AddReturn(regloc(kReturnRegister0));
465 types.AddReturn(return_type);
467 // Add parameters in registers and on the stack.
468 for (int i = 0; i < js_parameter_count; i++) {
469 if (i < register_parameter_count) {
470 // The first parameters go in registers.
471 Register reg = descriptor.GetRegisterParameter(i);
473 RepresentationFromType(descriptor.GetParameterType(i));
474 locations.AddParam(regloc(reg));
475 types.AddParam(reptyp(rep));
477 // The rest of the parameters go on the stack.
478 int stack_slot = i - register_parameter_count - stack_parameter_count;
479 locations.AddParam(LinkageLocation::ForCallerFrameSlot(stack_slot));
480 types.AddParam(kMachAnyTagged);
484 locations.AddParam(regloc(kContextRegister));
485 types.AddParam(kMachAnyTagged);
487 // The target for stub calls is a code object.
488 MachineType target_type = kMachAnyTagged;
489 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
490 return new (zone) CallDescriptor( // --
491 CallDescriptor::kCallCodeObject, // kind
492 target_type, // target MachineType
493 target_loc, // target location
494 types.Build(), // machine_sig
495 locations.Build(), // location_sig
496 stack_parameter_count, // stack_parameter_count
497 properties, // properties
498 kNoCalleeSaved, // callee-saved registers
499 kNoCalleeSaved, // callee-saved fp
501 descriptor.DebugName(isolate));
505 LinkageLocation Linkage::GetOsrValueLocation(int index) const {
506 CHECK(incoming_->IsJSFunctionCall());
507 int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
508 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
510 if (index == kOsrContextSpillSlotIndex) {
511 // Context. Use the parameter location of the context spill slot.
512 // Parameter (arity + 1) is special for the context of the function frame.
513 int context_index = 1 + 1 + parameter_count; // target + receiver + params
514 return incoming_->GetInputLocation(context_index);
515 } else if (index >= first_stack_slot) {
516 // Local variable stored in this (callee) stack.
518 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
519 return LinkageLocation::ForCalleeFrameSlot(spill_index);
521 // Parameter. Use the assigned location from the incoming call descriptor.
522 int parameter_index = 1 + index; // skip index 0, which is the target.
523 return incoming_->GetInputLocation(parameter_index);
526 } // namespace compiler
527 } // namespace internal