5 #include "src/compiler/linkage.h" 7 #include "src/assembler-inl.h" 8 #include "src/code-stubs.h" 9 #include "src/compiler/common-operator.h" 10 #include "src/compiler/frame.h" 11 #include "src/compiler/node.h" 12 #include "src/compiler/osr.h" 13 #include "src/compiler/pipeline.h" 14 #include "src/macro-assembler.h" 15 #include "src/optimized-compilation-info.h" 23 inline LinkageLocation regloc(Register reg, MachineType type) {
24 return LinkageLocation::ForRegister(reg.code(), type);
30 std::ostream& operator<<(std::ostream& os,
const CallDescriptor::Kind& k) {
32 case CallDescriptor::kCallCodeObject:
35 case CallDescriptor::kCallJSFunction:
38 case CallDescriptor::kCallAddress:
41 case CallDescriptor::kCallWasmFunction:
44 case CallDescriptor::kCallWasmImportWrapper:
45 os <<
"WasmImportWrapper";
52 std::ostream& operator<<(std::ostream& os,
const CallDescriptor& d) {
54 return os << d.kind() <<
":" << d.debug_name() <<
":r" << d.ReturnCount()
55 <<
"s" << d.StackParameterCount() <<
"i" << d.InputCount() <<
"f" 56 << d.FrameStateCount();
59 MachineSignature* CallDescriptor::GetMachineSignature(Zone* zone)
const {
60 size_t param_count = ParameterCount();
61 size_t return_count = ReturnCount();
62 MachineType* types = zone->NewArray<MachineType>(param_count + return_count);
64 for (
size_t i = 0;
i < return_count; ++
i) {
65 types[current++] = GetReturnType(
i);
67 for (
size_t i = 0;
i < param_count; ++
i) {
68 types[current++] = GetParameterType(
i);
70 return new (zone) MachineSignature(return_count, param_count, types);
73 bool CallDescriptor::HasSameReturnLocationsAs(
74 const CallDescriptor* other)
const {
75 if (ReturnCount() != other->ReturnCount())
return false;
76 for (
size_t i = 0;
i < ReturnCount(); ++
i) {
77 if (GetReturnLocation(
i) != other->GetReturnLocation(
i))
return false;
82 int CallDescriptor::GetFirstUnusedStackSlot()
const {
83 int slots_above_sp = 0;
84 for (
size_t i = 0;
i < InputCount(); ++
i) {
85 LinkageLocation operand = GetInputLocation(
i);
86 if (!operand.IsRegister()) {
88 -operand.GetLocation() + operand.GetSizeInPointers() - 1;
89 if (new_candidate > slots_above_sp) {
90 slots_above_sp = new_candidate;
94 return slots_above_sp;
97 int CallDescriptor::GetStackParameterDelta(
98 CallDescriptor
const* tail_caller)
const {
99 int callee_slots_above_sp = GetFirstUnusedStackSlot();
100 int tail_caller_slots_above_sp = tail_caller->GetFirstUnusedStackSlot();
101 int stack_param_delta = callee_slots_above_sp - tail_caller_slots_above_sp;
104 if (stack_param_delta % 2 != 0) {
105 if (callee_slots_above_sp % 2 != 0) {
117 return stack_param_delta;
120 bool CallDescriptor::CanTailCall(
const Node* node)
const {
121 return HasSameReturnLocationsAs(CallDescriptorOf(node->op()));
124 int CallDescriptor::CalculateFixedFrameSize()
const {
126 case kCallJSFunction:
127 return PushArgumentCount()
128 ? OptimizedBuiltinFrameConstants::kFixedSlotCount
129 : StandardFrameConstants::kFixedSlotCount;
131 return CommonFrameConstants::kFixedSlotCountAboveFp +
132 CommonFrameConstants::kCPSlotCount;
133 case kCallCodeObject:
134 return TypedFrameConstants::kFixedSlotCount;
135 case kCallWasmFunction:
136 case kCallWasmImportWrapper:
137 return WasmCompiledFrameConstants::kFixedSlotCount;
142 CallDescriptor* Linkage::ComputeIncoming(Zone* zone,
143 OptimizedCompilationInfo* info) {
144 DCHECK(!info->IsStub());
145 if (!info->closure().is_null()) {
148 SharedFunctionInfo* shared = info->closure()->shared();
149 return GetJSCallDescriptor(zone, info->is_osr(),
150 1 + shared->internal_formal_parameter_count(),
151 CallDescriptor::kCanUseRoots);
158 bool Linkage::NeedsFrameStateInput(Runtime::FunctionId
function) {
163 case Runtime::kAbort:
164 case Runtime::kAllocateInTargetSpace:
165 case Runtime::kCreateIterResultObject:
166 case Runtime::kIncBlockCounter:
167 case Runtime::kIsFunction:
168 case Runtime::kNewClosure:
169 case Runtime::kNewClosure_Tenured:
170 case Runtime::kNewFunctionContext:
171 case Runtime::kPushBlockContext:
172 case Runtime::kPushCatchContext:
173 case Runtime::kReThrow:
174 case Runtime::kStringEqual:
175 case Runtime::kStringLessThan:
176 case Runtime::kStringLessThanOrEqual:
177 case Runtime::kStringGreaterThan:
178 case Runtime::kStringGreaterThanOrEqual:
179 case Runtime::kToFastProperties:
180 case Runtime::kTraceEnter:
181 case Runtime::kTraceExit:
185 case Runtime::kInlineCreateIterResultObject:
186 case Runtime::kInlineGeneratorClose:
187 case Runtime::kInlineGeneratorGetResumeMode:
188 case Runtime::kInlineCreateJSGeneratorObject:
189 case Runtime::kInlineIsArray:
190 case Runtime::kInlineIsJSReceiver:
191 case Runtime::kInlineIsRegExp:
192 case Runtime::kInlineIsSmi:
193 case Runtime::kInlineIsTypedArray:
205 bool CallDescriptor::UsesOnlyRegisters()
const {
206 for (
size_t i = 0;
i < InputCount(); ++
i) {
207 if (!GetInputLocation(
i).IsRegister())
return false;
209 for (
size_t i = 0;
i < ReturnCount(); ++
i) {
210 if (!GetReturnLocation(
i).IsRegister())
return false;
216 CallDescriptor* Linkage::GetRuntimeCallDescriptor(
217 Zone* zone, Runtime::FunctionId function_id,
int js_parameter_count,
218 Operator::Properties properties, CallDescriptor::Flags flags) {
219 const Runtime::Function*
function = Runtime::FunctionForId(function_id);
220 const int return_count =
function->result_size;
221 const char* debug_name =
function->name;
223 if (!Linkage::NeedsFrameStateInput(function_id)) {
224 flags =
static_cast<CallDescriptor::Flags
>(
225 flags & ~CallDescriptor::kNeedsFrameState);
228 return GetCEntryStubCallDescriptor(zone, return_count, js_parameter_count,
229 debug_name, properties, flags);
232 CallDescriptor* Linkage::GetCEntryStubCallDescriptor(
233 Zone* zone,
int return_count,
int js_parameter_count,
234 const char* debug_name, Operator::Properties properties,
235 CallDescriptor::Flags flags) {
236 const size_t function_count = 1;
237 const size_t num_args_count = 1;
238 const size_t context_count = 1;
239 const size_t parameter_count = function_count +
240 static_cast<size_t>(js_parameter_count) +
241 num_args_count + context_count;
243 LocationSignature::Builder locations(zone, static_cast<size_t>(return_count),
244 static_cast<size_t>(parameter_count));
247 if (locations.return_count_ > 0) {
248 locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
250 if (locations.return_count_ > 1) {
251 locations.AddReturn(regloc(kReturnRegister1, MachineType::AnyTagged()));
253 if (locations.return_count_ > 2) {
254 locations.AddReturn(regloc(kReturnRegister2, MachineType::AnyTagged()));
258 for (
int i = 0;
i < js_parameter_count;
i++) {
259 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
260 i - js_parameter_count, MachineType::AnyTagged()));
264 regloc(kRuntimeCallFunctionRegister, MachineType::Pointer()));
268 regloc(kRuntimeCallArgCountRegister, MachineType::Int32()));
271 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
274 MachineType target_type = MachineType::AnyTagged();
275 LinkageLocation target_loc =
276 LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
277 return new (zone) CallDescriptor(
278 CallDescriptor::kCallCodeObject,
290 CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone,
bool is_osr,
291 int js_parameter_count,
292 CallDescriptor::Flags flags) {
293 const size_t return_count = 1;
294 const size_t context_count = 1;
295 const size_t new_target_count = 1;
296 const size_t num_args_count = 1;
297 const size_t parameter_count =
298 js_parameter_count + new_target_count + num_args_count + context_count;
300 LocationSignature::Builder locations(zone, return_count, parameter_count);
303 locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
306 for (
int i = 0;
i < js_parameter_count;
i++) {
307 int spill_slot_index =
i - js_parameter_count;
308 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
309 spill_slot_index, MachineType::AnyTagged()));
314 regloc(kJavaScriptCallNewTargetRegister, MachineType::AnyTagged()));
318 regloc(kJavaScriptCallArgCountRegister, MachineType::Int32()));
321 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
324 MachineType target_type = MachineType::AnyTagged();
327 LinkageLocation target_loc =
328 is_osr ? LinkageLocation::ForSavedCallerFunction()
329 : regloc(kJSFunctionRegister, MachineType::AnyTagged());
330 return new (zone) CallDescriptor(
331 CallDescriptor::kCallJSFunction,
336 Operator::kNoProperties,
349 CallDescriptor* Linkage::GetStubCallDescriptor(
350 Zone* zone,
const CallInterfaceDescriptor& descriptor,
351 int stack_parameter_count, CallDescriptor::Flags flags,
352 Operator::Properties properties, StubCallMode stub_mode) {
353 const int register_parameter_count = descriptor.GetRegisterParameterCount();
354 const int js_parameter_count =
355 register_parameter_count + stack_parameter_count;
356 const int context_count = descriptor.HasContextParameter() ? 1 : 0;
357 const size_t parameter_count =
358 static_cast<size_t>(js_parameter_count + context_count);
360 DCHECK_GE(stack_parameter_count, descriptor.GetStackParameterCount());
362 size_t return_count = descriptor.GetReturnCount();
363 LocationSignature::Builder locations(zone, return_count, parameter_count);
366 if (locations.return_count_ > 0) {
367 locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
369 if (locations.return_count_ > 1) {
370 locations.AddReturn(regloc(kReturnRegister1, descriptor.GetReturnType(1)));
372 if (locations.return_count_ > 2) {
373 locations.AddReturn(regloc(kReturnRegister2, descriptor.GetReturnType(2)));
377 for (
int i = 0;
i < js_parameter_count;
i++) {
378 if (
i < register_parameter_count) {
380 Register reg = descriptor.GetRegisterParameter(
i);
381 MachineType type = descriptor.GetParameterType(
i);
382 locations.AddParam(regloc(reg, type));
385 int stack_slot =
i - register_parameter_count - stack_parameter_count;
386 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
387 stack_slot, MachineType::AnyTagged()));
392 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
396 CallDescriptor::Kind kind = stub_mode == StubCallMode::kCallWasmRuntimeStub
397 ? CallDescriptor::kCallWasmFunction
398 : CallDescriptor::kCallCodeObject;
399 MachineType target_type = stub_mode == StubCallMode::kCallWasmRuntimeStub
400 ? MachineType::Pointer()
401 : MachineType::AnyTagged();
402 LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
403 return new (zone) CallDescriptor(
408 stack_parameter_count,
412 CallDescriptor::kCanUseRoots | flags,
413 descriptor.DebugName(),
414 descriptor.allocatable_registers());
418 CallDescriptor* Linkage::GetBytecodeDispatchCallDescriptor(
419 Zone* zone,
const CallInterfaceDescriptor& descriptor,
420 int stack_parameter_count) {
421 const int register_parameter_count = descriptor.GetRegisterParameterCount();
422 const int parameter_count = register_parameter_count + stack_parameter_count;
424 DCHECK_EQ(descriptor.GetReturnCount(), 1);
425 LocationSignature::Builder locations(zone, 1, parameter_count);
427 locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
430 for (
int i = 0;
i < parameter_count;
i++) {
431 if (
i < register_parameter_count) {
433 Register reg = descriptor.GetRegisterParameter(
i);
434 MachineType type = descriptor.GetParameterType(
i);
435 locations.AddParam(regloc(reg, type));
438 int stack_slot =
i - register_parameter_count - stack_parameter_count;
439 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
440 stack_slot, MachineType::AnyTagged()));
445 MachineType target_type = MachineType::Pointer();
446 LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
447 const CallDescriptor::Flags kFlags =
448 CallDescriptor::kCanUseRoots | CallDescriptor::kFixedTargetRegister;
449 return new (zone) CallDescriptor(
450 CallDescriptor::kCallAddress,
454 stack_parameter_count,
455 Operator::kNoProperties,
459 descriptor.DebugName());
462 LinkageLocation Linkage::GetOsrValueLocation(
int index)
const {
463 CHECK(incoming_->IsJSFunctionCall());
464 int parameter_count =
static_cast<int>(incoming_->JSParameterCount() - 1);
465 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
467 if (index == kOsrContextSpillSlotIndex) {
471 int context_index = 1 + 1 + parameter_count + 1 + 1;
472 return incoming_->GetInputLocation(context_index);
473 }
else if (index >= first_stack_slot) {
476 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
477 return LinkageLocation::ForCalleeFrameSlot(spill_index,
478 MachineType::AnyTagged());
481 int parameter_index = 1 + index;
482 return incoming_->GetInputLocation(parameter_index);
487 inline bool IsTaggedReg(
const LinkageLocation& loc, Register reg) {
488 return loc.IsRegister() && loc.AsRegister() == reg.code() &&
489 loc.GetType().representation() ==
490 MachineRepresentation::kTaggedPointer;
494 bool Linkage::ParameterHasSecondaryLocation(
int index)
const {
496 if (incoming_->IsJSFunctionCall()) {
497 LinkageLocation loc = GetParameterLocation(index);
498 return IsTaggedReg(loc, kJSFunctionRegister) ||
499 IsTaggedReg(loc, kContextRegister);
501 if (incoming_->IsWasmFunctionCall()) {
502 LinkageLocation loc = GetParameterLocation(index);
503 return IsTaggedReg(loc, kWasmInstanceRegister);
508 LinkageLocation Linkage::GetParameterSecondaryLocation(
int index)
const {
510 static const int kJSContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
511 static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;
512 static const int kWasmInstanceSlot = 3 + StandardFrameConstants::kCPSlotCount;
514 DCHECK(ParameterHasSecondaryLocation(index));
515 LinkageLocation loc = GetParameterLocation(index);
518 if (incoming_->IsJSFunctionCall()) {
519 if (IsTaggedReg(loc, kJSFunctionRegister)) {
520 return LinkageLocation::ForCalleeFrameSlot(kJSFunctionSlot,
521 MachineType::AnyTagged());
523 DCHECK(IsTaggedReg(loc, kContextRegister));
524 return LinkageLocation::ForCalleeFrameSlot(kJSContextSlot,
525 MachineType::AnyTagged());
528 if (incoming_->IsWasmFunctionCall()) {
529 DCHECK(IsTaggedReg(loc, kWasmInstanceRegister));
530 return LinkageLocation::ForCalleeFrameSlot(kWasmInstanceSlot,
531 MachineType::AnyTagged());
534 return LinkageLocation::ForCalleeFrameSlot(0, MachineType::AnyTagged());