linkage.cc 24.4 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/compiler/linkage.h"

7 8 9
#include "src/codegen/assembler-inl.h"
#include "src/codegen/macro-assembler.h"
#include "src/codegen/optimized-compilation-info.h"
10
#include "src/compiler/frame.h"
11
#include "src/compiler/osr.h"
12 13 14 15 16 17
#include "src/compiler/pipeline.h"

namespace v8 {
namespace internal {
namespace compiler {

18 19
namespace {

20 21 22 23
// Offsets from callee to caller frame, in slots.
constexpr int kFirstCallerSlotOffset = 1;
constexpr int kNoCallerSlotOffset = 0;

24
inline LinkageLocation regloc(Register reg, MachineType type) {
25 26
  return LinkageLocation::ForRegister(reg.code(), type);
}
27

28 29 30 31
inline LinkageLocation regloc(DoubleRegister reg, MachineType type) {
  return LinkageLocation::ForRegister(reg.code(), type);
}

32 33
}  // namespace

34

35
std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
36 37 38 39 40 41 42 43 44 45
  switch (k) {
    case CallDescriptor::kCallCodeObject:
      os << "Code";
      break;
    case CallDescriptor::kCallJSFunction:
      os << "JS";
      break;
    case CallDescriptor::kCallAddress:
      os << "Addr";
      break;
46
#if V8_ENABLE_WEBASSEMBLY
47 48 49
    case CallDescriptor::kCallWasmCapiFunction:
      os << "WasmExit";
      break;
50
    case CallDescriptor::kCallWasmFunction:
51 52 53 54
      os << "WasmFunction";
      break;
    case CallDescriptor::kCallWasmImportWrapper:
      os << "WasmImportWrapper";
55
      break;
56
#endif  // V8_ENABLE_WEBASSEMBLY
57 58 59
    case CallDescriptor::kCallBuiltinPointer:
      os << "BuiltinPointer";
      break;
60 61 62 63 64
  }
  return os;
}


65
std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
66 67
  // TODO(svenpanne) Output properties etc. and be less cryptic.
  return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
68
            << "s" << d.ParameterSlotCount() << "i" << d.InputCount() << "f"
69
            << d.FrameStateCount();
svenpanne's avatar
svenpanne committed
70 71
}

72 73 74
MachineSignature* CallDescriptor::GetMachineSignature(Zone* zone) const {
  size_t param_count = ParameterCount();
  size_t return_count = ReturnCount();
75
  MachineType* types = zone->NewArray<MachineType>(param_count + return_count);
76 77 78 79 80 81 82
  int current = 0;
  for (size_t i = 0; i < return_count; ++i) {
    types[current++] = GetReturnType(i);
  }
  for (size_t i = 0; i < param_count; ++i) {
    types[current++] = GetParameterType(i);
  }
83
  return zone->New<MachineSignature>(return_count, param_count, types);
84
}
svenpanne's avatar
svenpanne committed
85

86 87
int CallDescriptor::GetStackParameterDelta(
    CallDescriptor const* tail_caller) const {
88 89 90 91 92 93
  // In the IsTailCallForTierUp case, the callee has
  // identical linkage and runtime arguments to the caller, thus the stack
  // parameter delta is 0. We don't explicitly pass the runtime arguments as
  // inputs to the TailCall node, since they already exist on the stack.
  if (IsTailCallForTierUp()) return 0;

94 95 96 97
  // Add padding if necessary before computing the stack parameter delta.
  int callee_slots_above_sp = AddArgumentPaddingSlots(GetOffsetToReturns());
  int tail_caller_slots_above_sp =
      AddArgumentPaddingSlots(tail_caller->GetOffsetToReturns());
98
  int stack_param_delta = callee_slots_above_sp - tail_caller_slots_above_sp;
99
  DCHECK(!ShouldPadArguments(stack_param_delta));
100
  return stack_param_delta;
101 102
}

103 104
int CallDescriptor::GetOffsetToFirstUnusedStackSlot() const {
  int offset = kFirstCallerSlotOffset;
105 106 107
  for (size_t i = 0; i < InputCount(); ++i) {
    LinkageLocation operand = GetInputLocation(i);
    if (!operand.IsRegister()) {
108 109 110
      DCHECK(operand.IsCallerFrameSlot());
      int slot_offset = -operand.GetLocation();
      offset = std::max(offset, slot_offset + operand.GetSizeInPointers());
111 112
    }
  }
113
  return offset;
114 115
}

116
int CallDescriptor::GetOffsetToReturns() const {
117 118
  // Find the return slot with the least offset relative to the callee.
  int offset = kNoCallerSlotOffset;
119 120 121
  for (size_t i = 0; i < ReturnCount(); ++i) {
    LinkageLocation operand = GetReturnLocation(i);
    if (!operand.IsRegister()) {
122 123 124
      DCHECK(operand.IsCallerFrameSlot());
      int slot_offset = -operand.GetLocation();
      offset = std::min(offset, slot_offset);
125 126
    }
  }
127 128 129 130
  // If there was a return slot, return the offset minus 1 slot.
  if (offset != kNoCallerSlotOffset) {
    return offset - 1;
  }
131

132 133 134 135
  // Otherwise, return the first slot after the parameters area, including
  // optional padding slots.
  int last_argument_slot = GetOffsetToFirstUnusedStackSlot() - 1;
  offset = AddArgumentPaddingSlots(last_argument_slot);
136

137 138
  DCHECK_IMPLIES(offset == 0, ParameterSlotCount() == 0);
  return offset;
139 140
}

141 142 143 144 145 146 147 148 149 150 151
int CallDescriptor::GetTaggedParameterSlots() const {
  int result = 0;
  for (size_t i = 0; i < InputCount(); ++i) {
    LinkageLocation operand = GetInputLocation(i);
    if (!operand.IsRegister() && operand.GetType().IsTagged()) {
      ++result;
    }
  }
  return result;
}

152
bool CallDescriptor::CanTailCall(const CallDescriptor* callee) const {
153
  if (ReturnCount() != callee->ReturnCount()) return false;
154 155
  const int stack_returns_delta =
      GetOffsetToReturns() - callee->GetOffsetToReturns();
156
  for (size_t i = 0; i < ReturnCount(); ++i) {
157 158
    if (GetReturnLocation(i).IsCallerFrameSlot() &&
        callee->GetReturnLocation(i).IsCallerFrameSlot()) {
159
      if (GetReturnLocation(i).AsCallerFrameSlot() + stack_returns_delta !=
160 161 162 163 164
          callee->GetReturnLocation(i).AsCallerFrameSlot()) {
        return false;
      }
    } else if (!LinkageLocation::IsSameLocation(GetReturnLocation(i),
                                                callee->GetReturnLocation(i))) {
165
      return false;
166
    }
167 168
  }
  return true;
169 170
}

171 172 173
// TODO(jkummerow, sigurds): Arguably frame size calculation should be
// keyed on code/frame type, not on CallDescriptor kind. Think about a
// good way to organize this logic.
174
int CallDescriptor::CalculateFixedFrameSize(CodeKind code_kind) const {
175 176
  switch (kind_) {
    case kCallJSFunction:
177
      return StandardFrameConstants::kFixedSlotCount;
178
    case kCallAddress:
179
#if V8_ENABLE_WEBASSEMBLY
180
      if (code_kind == CodeKind::C_WASM_ENTRY) {
181 182
        return CWasmEntryFrameConstants::kFixedSlotCount;
      }
183
#endif  // V8_ENABLE_WEBASSEMBLY
184 185 186
      return CommonFrameConstants::kFixedSlotCountAboveFp +
             CommonFrameConstants::kCPSlotCount;
    case kCallCodeObject:
187
    case kCallBuiltinPointer:
188
      return TypedFrameConstants::kFixedSlotCount;
189
#if V8_ENABLE_WEBASSEMBLY
190
    case kCallWasmFunction:
191
    case kCallWasmImportWrapper:
192
      return WasmFrameConstants::kFixedSlotCount;
193
    case kCallWasmCapiFunction:
194
      return WasmExitFrameConstants::kFixedSlotCount;
195
#endif  // V8_ENABLE_WEBASSEMBLY
196 197 198
  }
  UNREACHABLE();
}
199

200 201
CallDescriptor* Linkage::ComputeIncoming(Zone* zone,
                                         OptimizedCompilationInfo* info) {
202
#if V8_ENABLE_WEBASSEMBLY
203
  DCHECK(info->IsOptimizing() || info->IsWasm());
204 205 206
#else
  DCHECK(info->IsOptimizing());
#endif  // V8_ENABLE_WEBASSEMBLY
207
  if (!info->closure().is_null()) {
208 209
    // If we are compiling a JS function, use a JS call descriptor,
    // plus the receiver.
210
    SharedFunctionInfo shared = info->closure()->shared();
211
    return GetJSCallDescriptor(zone, info->is_osr(),
212
                               1 + shared.internal_formal_parameter_count(),
213
                               CallDescriptor::kCanUseRoots);
214
  }
215
  return nullptr;  // TODO(titzer): ?
216 217 218
}


219
// static
220
bool Linkage::NeedsFrameStateInput(Runtime::FunctionId function) {
221
  switch (function) {
222
    // Most runtime functions need a FrameState. A few chosen ones that we know
223
    // not to call into arbitrary JavaScript, not to throw, and not to lazily
Dan Elphick's avatar
Dan Elphick committed
224
    // deoptimize are allowlisted here and can be called without a FrameState.
225
    case Runtime::kAbort:
226
    case Runtime::kAllocateInOldGeneration:
227
    case Runtime::kCreateIterResultObject:
228
    case Runtime::kIncBlockCounter:
229
    case Runtime::kIsFunction:
230
    case Runtime::kNewClosure:
231
    case Runtime::kNewClosure_Tenured:
232 233 234 235
    case Runtime::kNewFunctionContext:
    case Runtime::kPushBlockContext:
    case Runtime::kPushCatchContext:
    case Runtime::kReThrow:
236
    case Runtime::kStringEqual:
237 238 239 240
    case Runtime::kStringLessThan:
    case Runtime::kStringLessThanOrEqual:
    case Runtime::kStringGreaterThan:
    case Runtime::kStringGreaterThanOrEqual:
241
    case Runtime::kToFastProperties:  // TODO(conradw): Is it safe?
242 243
    case Runtime::kTraceEnter:
    case Runtime::kTraceExit:
244
      return false;
245 246 247

    // Some inline intrinsics are also safe to call without a FrameState.
    case Runtime::kInlineCreateIterResultObject:
248
    case Runtime::kInlineIncBlockCounter:
249 250
    case Runtime::kInlineGeneratorClose:
    case Runtime::kInlineGeneratorGetResumeMode:
251
    case Runtime::kInlineCreateJSGeneratorObject:
252 253 254 255 256 257
    case Runtime::kInlineIsArray:
    case Runtime::kInlineIsJSReceiver:
    case Runtime::kInlineIsRegExp:
    case Runtime::kInlineIsSmi:
      return false;

258
    default:
259
      break;
260
  }
261

Dan Elphick's avatar
Dan Elphick committed
262
  // For safety, default to needing a FrameState unless allowlisted.
263
  return true;
264 265 266
}


svenpanne's avatar
svenpanne committed
267 268
bool CallDescriptor::UsesOnlyRegisters() const {
  for (size_t i = 0; i < InputCount(); ++i) {
269
    if (!GetInputLocation(i).IsRegister()) return false;
svenpanne's avatar
svenpanne committed
270 271
  }
  for (size_t i = 0; i < ReturnCount(); ++i) {
272
    if (!GetReturnLocation(i).IsRegister()) return false;
svenpanne's avatar
svenpanne committed
273 274 275 276 277
  }
  return true;
}


278 279
CallDescriptor* Linkage::GetRuntimeCallDescriptor(
    Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
280
    Operator::Properties properties, CallDescriptor::Flags flags) {
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  const Runtime::Function* function = Runtime::FunctionForId(function_id);
  const int return_count = function->result_size;
  const char* debug_name = function->name;

  if (!Linkage::NeedsFrameStateInput(function_id)) {
    flags = static_cast<CallDescriptor::Flags>(
        flags & ~CallDescriptor::kNeedsFrameState);
  }

  return GetCEntryStubCallDescriptor(zone, return_count, js_parameter_count,
                                     debug_name, properties, flags);
}

CallDescriptor* Linkage::GetCEntryStubCallDescriptor(
    Zone* zone, int return_count, int js_parameter_count,
    const char* debug_name, Operator::Properties properties,
297
    CallDescriptor::Flags flags, StackArgumentOrder stack_order) {
298 299 300 301 302 303 304
  const size_t function_count = 1;
  const size_t num_args_count = 1;
  const size_t context_count = 1;
  const size_t parameter_count = function_count +
                                 static_cast<size_t>(js_parameter_count) +
                                 num_args_count + context_count;

305 306
  LocationSignature::Builder locations(zone, static_cast<size_t>(return_count),
                                       static_cast<size_t>(parameter_count));
307 308 309

  // Add returns.
  if (locations.return_count_ > 0) {
310
    locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
311 312
  }
  if (locations.return_count_ > 1) {
313
    locations.AddReturn(regloc(kReturnRegister1, MachineType::AnyTagged()));
314
  }
315
  if (locations.return_count_ > 2) {
316
    locations.AddReturn(regloc(kReturnRegister2, MachineType::AnyTagged()));
317 318 319 320
  }

  // All parameters to the runtime call go on the stack.
  for (int i = 0; i < js_parameter_count; i++) {
321 322
    locations.AddParam(LinkageLocation::ForCallerFrameSlot(
        i - js_parameter_count, MachineType::AnyTagged()));
323 324
  }
  // Add runtime function itself.
325
  locations.AddParam(
326
      regloc(kRuntimeCallFunctionRegister, MachineType::Pointer()));
327 328

  // Add runtime call argument count.
329
  locations.AddParam(
330
      regloc(kRuntimeCallArgCountRegister, MachineType::Int32()));
331 332

  // Add context.
333
  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
334 335

  // The target for runtime calls is a code object.
336
  MachineType target_type = MachineType::AnyTagged();
337 338
  LinkageLocation target_loc =
      LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
339
  return zone->New<CallDescriptor>(     // --
340 341 342 343 344 345 346 347 348
      CallDescriptor::kCallCodeObject,  // kind
      target_type,                      // target MachineType
      target_loc,                       // target location
      locations.Build(),                // location_sig
      js_parameter_count,               // stack_parameter_count
      properties,                       // properties
      kNoCalleeSaved,                   // callee-saved
      kNoCalleeSaved,                   // callee-saved fp
      flags,                            // flags
349 350
      debug_name,                       // debug name
      stack_order);                     // stack order
351 352
}

353
CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
354
                                             int js_parameter_count,
355
                                             CallDescriptor::Flags flags) {
356 357
  const size_t return_count = 1;
  const size_t context_count = 1;
358
  const size_t new_target_count = 1;
359 360
  const size_t num_args_count = 1;
  const size_t parameter_count =
361
      js_parameter_count + new_target_count + num_args_count + context_count;
362

363
  LocationSignature::Builder locations(zone, return_count, parameter_count);
364

365
  // All JS calls have exactly one return value.
366
  locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
367 368 369

  // All parameters to JS calls go on the stack.
  for (int i = 0; i < js_parameter_count; i++) {
370
    int spill_slot_index = -i - 1;
371 372
    locations.AddParam(LinkageLocation::ForCallerFrameSlot(
        spill_slot_index, MachineType::AnyTagged()));
373
  }
374

375
  // Add JavaScript call new target value.
376 377
  locations.AddParam(
      regloc(kJavaScriptCallNewTargetRegister, MachineType::AnyTagged()));
378

379
  // Add JavaScript call argument count.
380 381
  locations.AddParam(
      regloc(kJavaScriptCallArgCountRegister, MachineType::Int32()));
382

383
  // Add context.
384
  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
385 386

  // The target for JS function calls is the JSFunction object.
387
  MachineType target_type = MachineType::AnyTagged();
388 389
  // When entering into an OSR function from unoptimized code the JSFunction
  // is not in a register, but it is on the stack in the marker spill slot.
390 391 392
  LinkageLocation target_loc =
      is_osr ? LinkageLocation::ForSavedCallerFunction()
             : regloc(kJSFunctionRegister, MachineType::AnyTagged());
393
  return zone->New<CallDescriptor>(     // --
394 395 396 397 398 399 400 401
      CallDescriptor::kCallJSFunction,  // kind
      target_type,                      // target MachineType
      target_loc,                       // target location
      locations.Build(),                // location_sig
      js_parameter_count,               // stack_parameter_count
      Operator::kNoProperties,          // properties
      kNoCalleeSaved,                   // callee-saved
      kNoCalleeSaved,                   // callee-saved fp
402
      flags,                            // flags
403
      "js-call");                       // debug name
404 405
}

406
// TODO(turbofan): cache call descriptors for code stub calls.
407 408 409 410 411
// TODO(jgruber): Clean up stack parameter count handling. The descriptor
// already knows the formal stack parameter count and ideally only additional
// stack parameters should be passed into this method. All call-sites should
// be audited for correctness (e.g. many used to assume a stack parameter count
// of 0).
412
CallDescriptor* Linkage::GetStubCallDescriptor(
413
    Zone* zone, const CallInterfaceDescriptor& descriptor,
414
    int stack_parameter_count, CallDescriptor::Flags flags,
415
    Operator::Properties properties, StubCallMode stub_mode) {
416 417 418
  const int register_parameter_count = descriptor.GetRegisterParameterCount();
  const int js_parameter_count =
      register_parameter_count + stack_parameter_count;
419
  const int context_count = descriptor.HasContextParameter() ? 1 : 0;
420 421 422
  const size_t parameter_count =
      static_cast<size_t>(js_parameter_count + context_count);

423 424
  DCHECK_GE(stack_parameter_count, descriptor.GetStackParameterCount());

425
  size_t return_count = descriptor.GetReturnCount();
426 427
  LocationSignature::Builder locations(zone, return_count, parameter_count);

428
  // Add returns.
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
  static constexpr Register return_registers[] = {
      kReturnRegister0, kReturnRegister1, kReturnRegister2};
  size_t num_returns = 0;
  size_t num_fp_returns = 0;
  for (size_t i = 0; i < locations.return_count_; i++) {
    MachineType type = descriptor.GetReturnType(static_cast<int>(i));
    if (IsFloatingPoint(type.representation())) {
      DCHECK_LT(num_fp_returns, 1);  // Only 1 FP return is supported.
      locations.AddReturn(regloc(kFPReturnRegister0, type));
      num_fp_returns++;
    } else {
      DCHECK_LT(num_returns, arraysize(return_registers));
      locations.AddReturn(regloc(return_registers[num_returns], type));
      num_returns++;
    }
444
  }
445 446 447 448 449

  // Add parameters in registers and on the stack.
  for (int i = 0; i < js_parameter_count; i++) {
    if (i < register_parameter_count) {
      // The first parameters go in registers.
450 451 452 453 454 455
      // TODO(bbudge) Add floating point registers to the InterfaceDescriptor
      // and use them for FP types. Currently, this works because on most
      // platforms, all FP registers are available for use. On ia32, xmm0 is
      // not allocatable and so we must work around that with platform-specific
      // descriptors, adjusting the GP register set to avoid eax, which has
      // register code 0.
456
      Register reg = descriptor.GetRegisterParameter(i);
457
      MachineType type = descriptor.GetParameterType(i);
458
      locations.AddParam(regloc(reg, type));
459 460 461
    } else {
      // The rest of the parameters go on the stack.
      int stack_slot = i - register_parameter_count - stack_parameter_count;
462
      locations.AddParam(LinkageLocation::ForCallerFrameSlot(
463 464 465
          stack_slot, i < descriptor.GetParameterCount()
                          ? descriptor.GetParameterType(i)
                          : MachineType::AnyTagged()));
466 467 468
    }
  }
  // Add context.
469 470 471
  if (context_count) {
    locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
  }
472

473
  // The target for stub calls depends on the requested mode.
474 475 476
  CallDescriptor::Kind kind;
  MachineType target_type;
  switch (stub_mode) {
477
    case StubCallMode::kCallCodeObject:
478 479 480
      kind = CallDescriptor::kCallCodeObject;
      target_type = MachineType::AnyTagged();
      break;
481
#if V8_ENABLE_WEBASSEMBLY
482 483 484 485
    case StubCallMode::kCallWasmRuntimeStub:
      kind = CallDescriptor::kCallWasmFunction;
      target_type = MachineType::Pointer();
      break;
486
#endif  // V8_ENABLE_WEBASSEMBLY
487 488 489 490 491 492
    case StubCallMode::kCallBuiltinPointer:
      kind = CallDescriptor::kCallBuiltinPointer;
      target_type = MachineType::AnyTagged();
      break;
  }

493
  LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
494
  return zone->New<CallDescriptor>(          // --
495 496 497 498 499 500 501 502 503 504
      kind,                                  // kind
      target_type,                           // target MachineType
      target_loc,                            // target location
      locations.Build(),                     // location_sig
      stack_parameter_count,                 // stack_parameter_count
      properties,                            // properties
      kNoCalleeSaved,                        // callee-saved registers
      kNoCalleeSaved,                        // callee-saved fp
      CallDescriptor::kCanUseRoots | flags,  // flags
      descriptor.DebugName(),                // debug name
505
      descriptor.GetStackArgumentOrder(),    // stack order
506
      descriptor.allocatable_registers());
507 508
}

509
// static
510
CallDescriptor* Linkage::GetBytecodeDispatchCallDescriptor(
511
    Zone* zone, const CallInterfaceDescriptor& descriptor,
512 513 514 515
    int stack_parameter_count) {
  const int register_parameter_count = descriptor.GetRegisterParameterCount();
  const int parameter_count = register_parameter_count + stack_parameter_count;

516 517 518 519
  DCHECK_EQ(descriptor.GetReturnCount(), 1);
  LocationSignature::Builder locations(zone, 1, parameter_count);

  locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
520 521 522 523 524 525

  // Add parameters in registers and on the stack.
  for (int i = 0; i < parameter_count; i++) {
    if (i < register_parameter_count) {
      // The first parameters go in registers.
      Register reg = descriptor.GetRegisterParameter(i);
526
      MachineType type = descriptor.GetParameterType(i);
527
      locations.AddParam(regloc(reg, type));
528 529 530
    } else {
      // The rest of the parameters go on the stack.
      int stack_slot = i - register_parameter_count - stack_parameter_count;
531 532
      locations.AddParam(LinkageLocation::ForCallerFrameSlot(
          stack_slot, MachineType::AnyTagged()));
533 534 535 536 537
    }
  }

  // The target for interpreter dispatches is a code entry address.
  MachineType target_type = MachineType::Pointer();
538
  LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
539 540
  const CallDescriptor::Flags kFlags =
      CallDescriptor::kCanUseRoots | CallDescriptor::kFixedTargetRegister;
541
  return zone->New<CallDescriptor>(  // --
542 543 544 545 546 547 548 549
      CallDescriptor::kCallAddress,  // kind
      target_type,                   // target MachineType
      target_loc,                    // target location
      locations.Build(),             // location_sig
      stack_parameter_count,         // stack_parameter_count
      Operator::kNoProperties,       // properties
      kNoCalleeSaved,                // callee-saved registers
      kNoCalleeSaved,                // callee-saved fp
550
      kFlags,                        // flags
551
      descriptor.DebugName());
552
}
553

554 555 556 557 558 559 560
LinkageLocation Linkage::GetOsrValueLocation(int index) const {
  CHECK(incoming_->IsJSFunctionCall());
  int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
  int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);

  if (index == kOsrContextSpillSlotIndex) {
    // Context. Use the parameter location of the context spill slot.
561
    // Parameter (arity + 2) is special for the context of the function frame.
562 563
    // >> context_index = target + receiver + params + new_target + #args
    int context_index = 1 + 1 + parameter_count + 1 + 1;
564 565 566
    return incoming_->GetInputLocation(context_index);
  } else if (index >= first_stack_slot) {
    // Local variable stored in this (callee) stack.
567 568
    int spill_index =
        index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
569 570
    return LinkageLocation::ForCalleeFrameSlot(spill_index,
                                               MachineType::AnyTagged());
571 572 573 574 575 576
  } else {
    // Parameter. Use the assigned location from the incoming call descriptor.
    int parameter_index = 1 + index;  // skip index 0, which is the target.
    return incoming_->GetInputLocation(parameter_index);
  }
}
577

578
namespace {
579
inline bool IsTaggedReg(const LinkageLocation& loc, Register reg) {
580
  return loc.IsRegister() && loc.AsRegister() == reg.code() &&
581 582
         loc.GetType().representation() ==
             MachineRepresentation::kTaggedPointer;
583 584
}
}  // namespace
585 586

bool Linkage::ParameterHasSecondaryLocation(int index) const {
587 588 589
  // TODO(titzer): this should be configurable, not call-type specific.
  if (incoming_->IsJSFunctionCall()) {
    LinkageLocation loc = GetParameterLocation(index);
590 591
    return IsTaggedReg(loc, kJSFunctionRegister) ||
           IsTaggedReg(loc, kContextRegister);
592
  }
593
#if V8_ENABLE_WEBASSEMBLY
594 595
  if (incoming_->IsWasmFunctionCall()) {
    LinkageLocation loc = GetParameterLocation(index);
596
    return IsTaggedReg(loc, kWasmInstanceRegister);
597
  }
598
#endif  // V8_ENABLE_WEBASSEMBLY
599
  return false;
600 601 602
}

LinkageLocation Linkage::GetParameterSecondaryLocation(int index) const {
603 604 605 606
  // TODO(titzer): these constants are necessary due to offset/slot# mismatch
  static const int kJSContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
  static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;

607 608 609
  DCHECK(ParameterHasSecondaryLocation(index));
  LinkageLocation loc = GetParameterLocation(index);

610 611
  // TODO(titzer): this should be configurable, not call-type specific.
  if (incoming_->IsJSFunctionCall()) {
612
    if (IsTaggedReg(loc, kJSFunctionRegister)) {
613 614 615
      return LinkageLocation::ForCalleeFrameSlot(kJSFunctionSlot,
                                                 MachineType::AnyTagged());
    } else {
616
      DCHECK(IsTaggedReg(loc, kContextRegister));
617 618 619 620
      return LinkageLocation::ForCalleeFrameSlot(kJSContextSlot,
                                                 MachineType::AnyTagged());
    }
  }
621 622
#if V8_ENABLE_WEBASSEMBLY
  static const int kWasmInstanceSlot = 3 + StandardFrameConstants::kCPSlotCount;
623
  if (incoming_->IsWasmFunctionCall()) {
624
    DCHECK(IsTaggedReg(loc, kWasmInstanceRegister));
625
    return LinkageLocation::ForCalleeFrameSlot(kWasmInstanceSlot,
626
                                               MachineType::AnyTagged());
627
  }
628
#endif  // V8_ENABLE_WEBASSEMBLY
629 630
  UNREACHABLE();
  return LinkageLocation::ForCalleeFrameSlot(0, MachineType::AnyTagged());
631 632 633
}


634 635 636
}  // namespace compiler
}  // namespace internal
}  // namespace v8