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::kDefineClassMethod: // TODO(jarin): Is it safe?
222 case Runtime::kDefineGetterPropertyUnchecked: // TODO(jarin): Is it safe?
223 case Runtime::kDefineSetterPropertyUnchecked: // TODO(jarin): Is it safe?
224 case Runtime::kFinalizeClassDefinition: // TODO(conradw): 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::kNewClosure_Tenured:
231 case Runtime::kNewFunctionContext:
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::kInlineArgumentsLength:
243 case Runtime::kInlineCall:
244 case Runtime::kInlineCallFunction:
245 case Runtime::kInlineDefaultConstructorCallSuper:
246 case Runtime::kInlineGetCallerJSFunction:
247 case Runtime::kInlineGetPrototype:
248 case Runtime::kInlineRegExpExec:
249 case Runtime::kInlineSubString:
250 case Runtime::kInlineToName:
251 case Runtime::kInlineToNumber:
252 case Runtime::kInlineToObject:
253 case Runtime::kInlineToPrimitive_Number:
254 case Runtime::kInlineToPrimitive_String:
255 case Runtime::kInlineToPrimitive:
256 case Runtime::kInlineToString:
258 case Runtime::kInlineDeoptimizeNow:
259 case Runtime::kInlineThrowNotDateError:
265 // Most inlined runtime functions (except the ones listed above) can be called
266 // without a FrameState or will be lowered by JSIntrinsicLowering internally.
267 const Runtime::Function* const f = Runtime::FunctionForId(function);
268 if (f->intrinsic_type == Runtime::IntrinsicType::INLINE) return 0;
274 bool CallDescriptor::UsesOnlyRegisters() const {
275 for (size_t i = 0; i < InputCount(); ++i) {
276 if (!GetInputLocation(i).IsRegister()) return false;
278 for (size_t i = 0; i < ReturnCount(); ++i) {
279 if (!GetReturnLocation(i).IsRegister()) return false;
285 CallDescriptor* Linkage::GetRuntimeCallDescriptor(
286 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
287 Operator::Properties properties) {
288 const size_t function_count = 1;
289 const size_t num_args_count = 1;
290 const size_t context_count = 1;
291 const size_t parameter_count = function_count +
292 static_cast<size_t>(js_parameter_count) +
293 num_args_count + context_count;
295 const Runtime::Function* function = Runtime::FunctionForId(function_id);
296 const size_t return_count = static_cast<size_t>(function->result_size);
298 LocationSignature::Builder locations(zone, return_count, parameter_count);
299 MachineSignature::Builder types(zone, return_count, parameter_count);
302 if (locations.return_count_ > 0) {
303 locations.AddReturn(regloc(kReturnRegister0));
305 if (locations.return_count_ > 1) {
306 locations.AddReturn(regloc(kReturnRegister1));
308 for (size_t i = 0; i < return_count; i++) {
309 types.AddReturn(kMachAnyTagged);
312 // All parameters to the runtime call go on the stack.
313 for (int i = 0; i < js_parameter_count; i++) {
315 LinkageLocation::ForCallerFrameSlot(i - js_parameter_count));
316 types.AddParam(kMachAnyTagged);
318 // Add runtime function itself.
319 locations.AddParam(regloc(kRuntimeCallFunctionRegister));
320 types.AddParam(kMachAnyTagged);
322 // Add runtime call argument count.
323 locations.AddParam(regloc(kRuntimeCallArgCountRegister));
324 types.AddParam(kMachPtr);
327 locations.AddParam(regloc(kContextRegister));
328 types.AddParam(kMachAnyTagged);
330 CallDescriptor::Flags flags = Linkage::FrameStateInputCount(function_id) > 0
331 ? CallDescriptor::kNeedsFrameState
332 : CallDescriptor::kNoFlags;
334 // The target for runtime calls is a code object.
335 MachineType target_type = kMachAnyTagged;
336 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
337 return new (zone) CallDescriptor( // --
338 CallDescriptor::kCallCodeObject, // kind
339 target_type, // target MachineType
340 target_loc, // target location
341 types.Build(), // machine_sig
342 locations.Build(), // location_sig
343 js_parameter_count, // stack_parameter_count
344 properties, // properties
345 kNoCalleeSaved, // callee-saved
346 kNoCalleeSaved, // callee-saved fp
348 function->name); // debug name
352 CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
353 int js_parameter_count,
354 CallDescriptor::Flags flags) {
355 const size_t return_count = 1;
356 const size_t context_count = 1;
357 const size_t parameter_count = js_parameter_count + context_count;
359 LocationSignature::Builder locations(zone, return_count, parameter_count);
360 MachineSignature::Builder types(zone, return_count, parameter_count);
362 // All JS calls have exactly one return value.
363 locations.AddReturn(regloc(kReturnRegister0));
364 types.AddReturn(kMachAnyTagged);
366 // All parameters to JS calls go on the stack.
367 for (int i = 0; i < js_parameter_count; i++) {
368 int spill_slot_index = i - js_parameter_count;
369 locations.AddParam(LinkageLocation::ForCallerFrameSlot(spill_slot_index));
370 types.AddParam(kMachAnyTagged);
373 locations.AddParam(regloc(kContextRegister));
374 types.AddParam(kMachAnyTagged);
376 // The target for JS function calls is the JSFunction object.
377 MachineType target_type = kMachAnyTagged;
378 // TODO(titzer): When entering into an OSR function from unoptimized code,
379 // the JSFunction is not in a register, but it is on the stack in an
380 // unaddressable spill slot. We hack this in the OSR prologue. Fix.
381 LinkageLocation target_loc = regloc(kJSFunctionRegister);
382 return new (zone) CallDescriptor( // --
383 CallDescriptor::kCallJSFunction, // kind
384 target_type, // target MachineType
385 target_loc, // target location
386 types.Build(), // machine_sig
387 locations.Build(), // location_sig
388 js_parameter_count, // stack_parameter_count
389 Operator::kNoProperties, // properties
390 kNoCalleeSaved, // callee-saved
391 kNoCalleeSaved, // callee-saved fp
392 CallDescriptor::kCanUseRoots | // flags
398 CallDescriptor* Linkage::GetInterpreterDispatchDescriptor(Zone* zone) {
399 MachineSignature::Builder types(zone, 0, 6);
400 LocationSignature::Builder locations(zone, 0, 6);
402 // Add registers for fixed parameters passed via interpreter dispatch.
403 STATIC_ASSERT(0 == Linkage::kInterpreterAccumulatorParameter);
404 types.AddParam(kMachAnyTagged);
405 locations.AddParam(regloc(kInterpreterAccumulatorRegister));
407 STATIC_ASSERT(1 == Linkage::kInterpreterRegisterFileParameter);
408 types.AddParam(kMachPtr);
409 locations.AddParam(regloc(kInterpreterRegisterFileRegister));
411 STATIC_ASSERT(2 == Linkage::kInterpreterBytecodeOffsetParameter);
412 types.AddParam(kMachIntPtr);
413 locations.AddParam(regloc(kInterpreterBytecodeOffsetRegister));
415 STATIC_ASSERT(3 == Linkage::kInterpreterBytecodeArrayParameter);
416 types.AddParam(kMachAnyTagged);
417 locations.AddParam(regloc(kInterpreterBytecodeArrayRegister));
419 STATIC_ASSERT(4 == Linkage::kInterpreterDispatchTableParameter);
420 types.AddParam(kMachPtr);
421 locations.AddParam(regloc(kInterpreterDispatchTableRegister));
423 STATIC_ASSERT(5 == Linkage::kInterpreterContextParameter);
424 types.AddParam(kMachAnyTagged);
425 #if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_X87)
427 LinkageLocation::ForCallerFrameSlot(kInterpreterContextSpillSlot));
429 locations.AddParam(regloc(kContextRegister));
432 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
433 return new (zone) CallDescriptor( // --
434 CallDescriptor::kCallCodeObject, // kind
435 kMachNone, // target MachineType
436 target_loc, // target location
437 types.Build(), // machine_sig
438 locations.Build(), // location_sig
439 0, // stack_parameter_count
440 Operator::kNoProperties, // properties
441 kNoCalleeSaved, // callee-saved registers
442 kNoCalleeSaved, // callee-saved fp regs
443 CallDescriptor::kSupportsTailCalls | // flags
444 CallDescriptor::kCanUseRoots, // flags
445 "interpreter-dispatch");
449 // TODO(all): Add support for return representations/locations to
450 // CallInterfaceDescriptor.
451 // TODO(turbofan): cache call descriptors for code stub calls.
452 CallDescriptor* Linkage::GetStubCallDescriptor(
453 Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
454 int stack_parameter_count, CallDescriptor::Flags flags,
455 Operator::Properties properties, MachineType return_type) {
456 const int register_parameter_count = descriptor.GetRegisterParameterCount();
457 const int js_parameter_count =
458 register_parameter_count + stack_parameter_count;
459 const int context_count = 1;
460 const size_t return_count = 1;
461 const size_t parameter_count =
462 static_cast<size_t>(js_parameter_count + context_count);
464 LocationSignature::Builder locations(zone, return_count, parameter_count);
465 MachineSignature::Builder types(zone, return_count, parameter_count);
467 // Add return location.
468 locations.AddReturn(regloc(kReturnRegister0));
469 types.AddReturn(return_type);
471 // Add parameters in registers and on the stack.
472 for (int i = 0; i < js_parameter_count; i++) {
473 if (i < register_parameter_count) {
474 // The first parameters go in registers.
475 Register reg = descriptor.GetRegisterParameter(i);
477 RepresentationFromType(descriptor.GetParameterType(i));
478 locations.AddParam(regloc(reg));
479 types.AddParam(reptyp(rep));
481 // The rest of the parameters go on the stack.
482 int stack_slot = i - register_parameter_count - stack_parameter_count;
483 locations.AddParam(LinkageLocation::ForCallerFrameSlot(stack_slot));
484 types.AddParam(kMachAnyTagged);
488 locations.AddParam(regloc(kContextRegister));
489 types.AddParam(kMachAnyTagged);
491 // The target for stub calls is a code object.
492 MachineType target_type = kMachAnyTagged;
493 LinkageLocation target_loc = LinkageLocation::ForAnyRegister();
494 return new (zone) CallDescriptor( // --
495 CallDescriptor::kCallCodeObject, // kind
496 target_type, // target MachineType
497 target_loc, // target location
498 types.Build(), // machine_sig
499 locations.Build(), // location_sig
500 stack_parameter_count, // stack_parameter_count
501 properties, // properties
502 kNoCalleeSaved, // callee-saved registers
503 kNoCalleeSaved, // callee-saved fp
505 descriptor.DebugName(isolate));
509 LinkageLocation Linkage::GetOsrValueLocation(int index) const {
510 CHECK(incoming_->IsJSFunctionCall());
511 int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
512 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
514 if (index == kOsrContextSpillSlotIndex) {
515 // Context. Use the parameter location of the context spill slot.
516 // Parameter (arity + 1) is special for the context of the function frame.
517 int context_index = 1 + 1 + parameter_count; // target + receiver + params
518 return incoming_->GetInputLocation(context_index);
519 } else if (index >= first_stack_slot) {
520 // Local variable stored in this (callee) stack.
522 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
523 return LinkageLocation::ForCalleeFrameSlot(spill_index);
525 // Parameter. Use the assigned location from the incoming call descriptor.
526 int parameter_index = 1 + index; // skip index 0, which is the target.
527 return incoming_->GetInputLocation(parameter_index);
530 } // namespace compiler
531 } // namespace internal