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