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.
4
5 #include "src/compiler/linkage.h"
6
7 #include "src/codegen/assembler-inl.h"
8 #include "src/codegen/macro-assembler.h"
9 #include "src/codegen/optimized-compilation-info.h"
10 #include "src/compiler/frame.h"
11 #include "src/compiler/osr.h"
12 #include "src/compiler/pipeline.h"
13
14 namespace v8 {
15 namespace internal {
16 namespace compiler {
17
18 namespace {
19
20 // Offsets from callee to caller frame, in slots.
21 constexpr int kFirstCallerSlotOffset = 1;
22 constexpr int kNoCallerSlotOffset = 0;
23
regloc(Register reg,MachineType type)24 inline LinkageLocation regloc(Register reg, MachineType type) {
25 return LinkageLocation::ForRegister(reg.code(), type);
26 }
27
regloc(DoubleRegister reg,MachineType type)28 inline LinkageLocation regloc(DoubleRegister reg, MachineType type) {
29 return LinkageLocation::ForRegister(reg.code(), type);
30 }
31
32 } // namespace
33
34
operator <<(std::ostream & os,const CallDescriptor::Kind & k)35 std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
36 switch (k) {
37 case CallDescriptor::kCallCodeObject:
38 os << "Code";
39 break;
40 case CallDescriptor::kCallJSFunction:
41 os << "JS";
42 break;
43 case CallDescriptor::kCallAddress:
44 os << "Addr";
45 break;
46 #if V8_ENABLE_WEBASSEMBLY
47 case CallDescriptor::kCallWasmCapiFunction:
48 os << "WasmExit";
49 break;
50 case CallDescriptor::kCallWasmFunction:
51 os << "WasmFunction";
52 break;
53 case CallDescriptor::kCallWasmImportWrapper:
54 os << "WasmImportWrapper";
55 break;
56 #endif // V8_ENABLE_WEBASSEMBLY
57 case CallDescriptor::kCallBuiltinPointer:
58 os << "BuiltinPointer";
59 break;
60 }
61 return os;
62 }
63
64
operator <<(std::ostream & os,const CallDescriptor & d)65 std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
66 // TODO(svenpanne) Output properties etc. and be less cryptic.
67 return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
68 << "s" << d.ParameterSlotCount() << "i" << d.InputCount() << "f"
69 << d.FrameStateCount();
70 }
71
GetMachineSignature(Zone * zone) const72 MachineSignature* CallDescriptor::GetMachineSignature(Zone* zone) const {
73 size_t param_count = ParameterCount();
74 size_t return_count = ReturnCount();
75 MachineType* types = zone->NewArray<MachineType>(param_count + return_count);
76 int current = 0;
77 for (size_t i = 0; i < return_count; ++i) {
78 types[current++] = GetReturnType(i);
79 }
80 for (size_t i = 0; i < param_count; ++i) {
81 types[current++] = GetParameterType(i);
82 }
83 return zone->New<MachineSignature>(return_count, param_count, types);
84 }
85
GetStackParameterDelta(CallDescriptor const * tail_caller) const86 int CallDescriptor::GetStackParameterDelta(
87 CallDescriptor const* tail_caller) const {
88 // In the IsTailCallForTierUp case, the callee has
89 // identical linkage and runtime arguments to the caller, thus the stack
90 // parameter delta is 0. We don't explicitly pass the runtime arguments as
91 // inputs to the TailCall node, since they already exist on the stack.
92 if (IsTailCallForTierUp()) return 0;
93
94 // Add padding if necessary before computing the stack parameter delta.
95 int callee_slots_above_sp = AddArgumentPaddingSlots(GetOffsetToReturns());
96 int tail_caller_slots_above_sp =
97 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
GetOffsetToFirstUnusedStackSlot() const103 int CallDescriptor::GetOffsetToFirstUnusedStackSlot() const {
104 int offset = kFirstCallerSlotOffset;
105 for (size_t i = 0; i < InputCount(); ++i) {
106 LinkageLocation operand = GetInputLocation(i);
107 if (!operand.IsRegister()) {
108 DCHECK(operand.IsCallerFrameSlot());
109 int slot_offset = -operand.GetLocation();
110 offset = std::max(offset, slot_offset + operand.GetSizeInPointers());
111 }
112 }
113 return offset;
114 }
115
GetOffsetToReturns() const116 int CallDescriptor::GetOffsetToReturns() const {
117 // Find the return slot with the least offset relative to the callee.
118 int offset = kNoCallerSlotOffset;
119 for (size_t i = 0; i < ReturnCount(); ++i) {
120 LinkageLocation operand = GetReturnLocation(i);
121 if (!operand.IsRegister()) {
122 DCHECK(operand.IsCallerFrameSlot());
123 int slot_offset = -operand.GetLocation();
124 offset = std::min(offset, slot_offset);
125 }
126 }
127 // If there was a return slot, return the offset minus 1 slot.
128 if (offset != kNoCallerSlotOffset) {
129 return offset - 1;
130 }
131
132 // Otherwise, return the first slot after the parameters area, including
133 // optional padding slots.
134 int last_argument_slot = GetOffsetToFirstUnusedStackSlot() - 1;
135 offset = AddArgumentPaddingSlots(last_argument_slot);
136
137 DCHECK_IMPLIES(offset == 0, ParameterSlotCount() == 0);
138 return offset;
139 }
140
GetTaggedParameterSlots() const141 uint32_t CallDescriptor::GetTaggedParameterSlots() const {
142 uint32_t count = 0;
143 uint32_t first_offset = kMaxInt;
144 for (size_t i = 0; i < InputCount(); ++i) {
145 LinkageLocation operand = GetInputLocation(i);
146 if (!operand.IsRegister() && operand.GetType().IsTagged()) {
147 ++count;
148 // Caller frame slots have negative indices and start at -1. Flip it
149 // back to a positive offset (to be added to the frame's SP to find the
150 // slot).
151 int slot_offset = -operand.GetLocation() - 1;
152 DCHECK_GE(slot_offset, 0);
153 first_offset = std::min(first_offset, static_cast<uint32_t>(slot_offset));
154 }
155 }
156 if (count > 0) {
157 DCHECK(first_offset != kMaxInt);
158 return (first_offset << 16) | (count & 0xFFFFu);
159 }
160 return 0;
161 }
162
CanTailCall(const CallDescriptor * callee) const163 bool CallDescriptor::CanTailCall(const CallDescriptor* callee) const {
164 if (ReturnCount() != callee->ReturnCount()) return false;
165 const int stack_returns_delta =
166 GetOffsetToReturns() - callee->GetOffsetToReturns();
167 for (size_t i = 0; i < ReturnCount(); ++i) {
168 if (GetReturnLocation(i).IsCallerFrameSlot() &&
169 callee->GetReturnLocation(i).IsCallerFrameSlot()) {
170 if (GetReturnLocation(i).AsCallerFrameSlot() + stack_returns_delta !=
171 callee->GetReturnLocation(i).AsCallerFrameSlot()) {
172 return false;
173 }
174 } else if (!LinkageLocation::IsSameLocation(GetReturnLocation(i),
175 callee->GetReturnLocation(i))) {
176 return false;
177 }
178 }
179 return true;
180 }
181
182 // TODO(jkummerow, sigurds): Arguably frame size calculation should be
183 // keyed on code/frame type, not on CallDescriptor kind. Think about a
184 // good way to organize this logic.
CalculateFixedFrameSize(CodeKind code_kind) const185 int CallDescriptor::CalculateFixedFrameSize(CodeKind code_kind) const {
186 switch (kind_) {
187 case kCallJSFunction:
188 return StandardFrameConstants::kFixedSlotCount;
189 case kCallAddress:
190 #if V8_ENABLE_WEBASSEMBLY
191 if (code_kind == CodeKind::C_WASM_ENTRY) {
192 return CWasmEntryFrameConstants::kFixedSlotCount;
193 }
194 #endif // V8_ENABLE_WEBASSEMBLY
195 return CommonFrameConstants::kFixedSlotCountAboveFp +
196 CommonFrameConstants::kCPSlotCount;
197 case kCallCodeObject:
198 case kCallBuiltinPointer:
199 return TypedFrameConstants::kFixedSlotCount;
200 #if V8_ENABLE_WEBASSEMBLY
201 case kCallWasmFunction:
202 case kCallWasmImportWrapper:
203 return WasmFrameConstants::kFixedSlotCount;
204 case kCallWasmCapiFunction:
205 return WasmExitFrameConstants::kFixedSlotCount;
206 #endif // V8_ENABLE_WEBASSEMBLY
207 }
208 UNREACHABLE();
209 }
210
ToEncodedCSignature() const211 EncodedCSignature CallDescriptor::ToEncodedCSignature() const {
212 int parameter_count = static_cast<int>(ParameterCount());
213 EncodedCSignature sig(parameter_count);
214 CHECK_LT(parameter_count, EncodedCSignature::kInvalidParamCount);
215
216 for (int i = 0; i < parameter_count; ++i) {
217 if (IsFloatingPoint(GetParameterType(i).representation())) {
218 sig.SetFloat(i);
219 }
220 }
221 if (ReturnCount() > 0) {
222 DCHECK_EQ(1, ReturnCount());
223 if (IsFloatingPoint(GetReturnType(0).representation())) {
224 sig.SetFloat(EncodedCSignature::kReturnIndex);
225 }
226 }
227 return sig;
228 }
229
ComputeParamCounts() const230 void CallDescriptor::ComputeParamCounts() const {
231 gp_param_count_ = 0;
232 fp_param_count_ = 0;
233 for (size_t i = 0; i < ParameterCount(); ++i) {
234 if (IsFloatingPoint(GetParameterType(i).representation())) {
235 ++fp_param_count_.value();
236 } else {
237 ++gp_param_count_.value();
238 }
239 }
240 }
241
ComputeIncoming(Zone * zone,OptimizedCompilationInfo * info)242 CallDescriptor* Linkage::ComputeIncoming(Zone* zone,
243 OptimizedCompilationInfo* info) {
244 #if V8_ENABLE_WEBASSEMBLY
245 DCHECK(info->IsOptimizing() || info->IsWasm());
246 #else
247 DCHECK(info->IsOptimizing());
248 #endif // V8_ENABLE_WEBASSEMBLY
249 if (!info->closure().is_null()) {
250 // If we are compiling a JS function, use a JS call descriptor,
251 // plus the receiver.
252 SharedFunctionInfo shared = info->closure()->shared();
253 return GetJSCallDescriptor(
254 zone, info->is_osr(),
255 shared.internal_formal_parameter_count_with_receiver(),
256 CallDescriptor::kCanUseRoots);
257 }
258 return nullptr; // TODO(titzer): ?
259 }
260
261
262 // static
NeedsFrameStateInput(Runtime::FunctionId function)263 bool Linkage::NeedsFrameStateInput(Runtime::FunctionId function) {
264 switch (function) {
265 // Most runtime functions need a FrameState. A few chosen ones that we know
266 // not to call into arbitrary JavaScript, not to throw, and not to lazily
267 // deoptimize are allowlisted here and can be called without a FrameState.
268 case Runtime::kAbort:
269 case Runtime::kAllocateInOldGeneration:
270 case Runtime::kCreateIterResultObject:
271 case Runtime::kIncBlockCounter:
272 case Runtime::kIsFunction:
273 case Runtime::kNewClosure:
274 case Runtime::kNewClosure_Tenured:
275 case Runtime::kNewFunctionContext:
276 case Runtime::kPushBlockContext:
277 case Runtime::kPushCatchContext:
278 case Runtime::kReThrow:
279 case Runtime::kReThrowWithMessage:
280 case Runtime::kStringEqual:
281 case Runtime::kStringLessThan:
282 case Runtime::kStringLessThanOrEqual:
283 case Runtime::kStringGreaterThan:
284 case Runtime::kStringGreaterThanOrEqual:
285 case Runtime::kToFastProperties: // TODO(conradw): Is it safe?
286 case Runtime::kTraceEnter:
287 case Runtime::kTraceExit:
288 return false;
289
290 // Some inline intrinsics are also safe to call without a FrameState.
291 case Runtime::kInlineCreateIterResultObject:
292 case Runtime::kInlineIncBlockCounter:
293 case Runtime::kInlineGeneratorClose:
294 case Runtime::kInlineGeneratorGetResumeMode:
295 case Runtime::kInlineCreateJSGeneratorObject:
296 return false;
297
298 default:
299 break;
300 }
301
302 // For safety, default to needing a FrameState unless allowlisted.
303 return true;
304 }
305
GetRuntimeCallDescriptor(Zone * zone,Runtime::FunctionId function_id,int js_parameter_count,Operator::Properties properties,CallDescriptor::Flags flags)306 CallDescriptor* Linkage::GetRuntimeCallDescriptor(
307 Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
308 Operator::Properties properties, CallDescriptor::Flags flags) {
309 const Runtime::Function* function = Runtime::FunctionForId(function_id);
310 const int return_count = function->result_size;
311 const char* debug_name = function->name;
312
313 if (!Linkage::NeedsFrameStateInput(function_id)) {
314 flags = static_cast<CallDescriptor::Flags>(
315 flags & ~CallDescriptor::kNeedsFrameState);
316 }
317
318 return GetCEntryStubCallDescriptor(zone, return_count, js_parameter_count,
319 debug_name, properties, flags);
320 }
321
GetCEntryStubCallDescriptor(Zone * zone,int return_count,int js_parameter_count,const char * debug_name,Operator::Properties properties,CallDescriptor::Flags flags,StackArgumentOrder stack_order)322 CallDescriptor* Linkage::GetCEntryStubCallDescriptor(
323 Zone* zone, int return_count, int js_parameter_count,
324 const char* debug_name, Operator::Properties properties,
325 CallDescriptor::Flags flags, StackArgumentOrder stack_order) {
326 const size_t function_count = 1;
327 const size_t num_args_count = 1;
328 const size_t context_count = 1;
329 const size_t parameter_count = function_count +
330 static_cast<size_t>(js_parameter_count) +
331 num_args_count + context_count;
332
333 LocationSignature::Builder locations(zone, static_cast<size_t>(return_count),
334 static_cast<size_t>(parameter_count));
335
336 // Add returns.
337 if (locations.return_count_ > 0) {
338 locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
339 }
340 if (locations.return_count_ > 1) {
341 locations.AddReturn(regloc(kReturnRegister1, MachineType::AnyTagged()));
342 }
343 if (locations.return_count_ > 2) {
344 locations.AddReturn(regloc(kReturnRegister2, MachineType::AnyTagged()));
345 }
346
347 // All parameters to the runtime call go on the stack.
348 for (int i = 0; i < js_parameter_count; i++) {
349 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
350 i - js_parameter_count, MachineType::AnyTagged()));
351 }
352 // Add runtime function itself.
353 locations.AddParam(
354 regloc(kRuntimeCallFunctionRegister, MachineType::Pointer()));
355
356 // Add runtime call argument count.
357 locations.AddParam(
358 regloc(kRuntimeCallArgCountRegister, MachineType::Int32()));
359
360 // Add context.
361 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
362
363 // The target for runtime calls is a code object.
364 MachineType target_type = MachineType::AnyTagged();
365 LinkageLocation target_loc =
366 LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
367 return zone->New<CallDescriptor>( // --
368 CallDescriptor::kCallCodeObject, // kind
369 target_type, // target MachineType
370 target_loc, // target location
371 locations.Build(), // location_sig
372 js_parameter_count, // stack_parameter_count
373 properties, // properties
374 kNoCalleeSaved, // callee-saved
375 kNoCalleeSavedFp, // callee-saved fp
376 flags, // flags
377 debug_name, // debug name
378 stack_order); // stack order
379 }
380
GetJSCallDescriptor(Zone * zone,bool is_osr,int js_parameter_count,CallDescriptor::Flags flags)381 CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
382 int js_parameter_count,
383 CallDescriptor::Flags flags) {
384 const size_t return_count = 1;
385 const size_t context_count = 1;
386 const size_t new_target_count = 1;
387 const size_t num_args_count = 1;
388 const size_t parameter_count =
389 js_parameter_count + new_target_count + num_args_count + context_count;
390
391 LocationSignature::Builder locations(zone, return_count, parameter_count);
392
393 // All JS calls have exactly one return value.
394 locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
395
396 // All parameters to JS calls go on the stack.
397 for (int i = 0; i < js_parameter_count; i++) {
398 int spill_slot_index = -i - 1;
399 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
400 spill_slot_index, MachineType::AnyTagged()));
401 }
402
403 // Add JavaScript call new target value.
404 locations.AddParam(
405 regloc(kJavaScriptCallNewTargetRegister, MachineType::AnyTagged()));
406
407 // Add JavaScript call argument count.
408 locations.AddParam(
409 regloc(kJavaScriptCallArgCountRegister, MachineType::Int32()));
410
411 // Add context.
412 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
413
414 // The target for JS function calls is the JSFunction object.
415 MachineType target_type = MachineType::AnyTagged();
416 // When entering into an OSR function from unoptimized code the JSFunction
417 // is not in a register, but it is on the stack in the marker spill slot.
418 LinkageLocation target_loc =
419 is_osr ? LinkageLocation::ForSavedCallerFunction()
420 : regloc(kJSFunctionRegister, MachineType::AnyTagged());
421 return zone->New<CallDescriptor>( // --
422 CallDescriptor::kCallJSFunction, // kind
423 target_type, // target MachineType
424 target_loc, // target location
425 locations.Build(), // location_sig
426 js_parameter_count, // stack_parameter_count
427 Operator::kNoProperties, // properties
428 kNoCalleeSaved, // callee-saved
429 kNoCalleeSavedFp, // callee-saved fp
430 flags, // flags
431 "js-call"); // debug name
432 }
433
434 // TODO(turbofan): cache call descriptors for code stub calls.
435 // TODO(jgruber): Clean up stack parameter count handling. The descriptor
436 // already knows the formal stack parameter count and ideally only additional
437 // stack parameters should be passed into this method. All call-sites should
438 // be audited for correctness (e.g. many used to assume a stack parameter count
439 // of 0).
GetStubCallDescriptor(Zone * zone,const CallInterfaceDescriptor & descriptor,int stack_parameter_count,CallDescriptor::Flags flags,Operator::Properties properties,StubCallMode stub_mode)440 CallDescriptor* Linkage::GetStubCallDescriptor(
441 Zone* zone, const CallInterfaceDescriptor& descriptor,
442 int stack_parameter_count, CallDescriptor::Flags flags,
443 Operator::Properties properties, StubCallMode stub_mode) {
444 const int register_parameter_count = descriptor.GetRegisterParameterCount();
445 const int js_parameter_count =
446 register_parameter_count + stack_parameter_count;
447 const int context_count = descriptor.HasContextParameter() ? 1 : 0;
448 const size_t parameter_count =
449 static_cast<size_t>(js_parameter_count + context_count);
450
451 DCHECK_GE(stack_parameter_count, descriptor.GetStackParameterCount());
452
453 size_t return_count = descriptor.GetReturnCount();
454 LocationSignature::Builder locations(zone, return_count, parameter_count);
455
456 // Add returns.
457 static constexpr Register return_registers[] = {
458 kReturnRegister0, kReturnRegister1, kReturnRegister2};
459 size_t num_returns = 0;
460 size_t num_fp_returns = 0;
461 for (size_t i = 0; i < locations.return_count_; i++) {
462 MachineType type = descriptor.GetReturnType(static_cast<int>(i));
463 if (IsFloatingPoint(type.representation())) {
464 DCHECK_LT(num_fp_returns, 1); // Only 1 FP return is supported.
465 locations.AddReturn(regloc(kFPReturnRegister0, type));
466 num_fp_returns++;
467 } else {
468 DCHECK_LT(num_returns, arraysize(return_registers));
469 locations.AddReturn(regloc(return_registers[num_returns], type));
470 num_returns++;
471 }
472 }
473 USE(num_fp_returns);
474
475 // Add parameters in registers and on the stack.
476 for (int i = 0; i < js_parameter_count; i++) {
477 if (i < register_parameter_count) {
478 // The first parameters go in registers.
479 // TODO(bbudge) Add floating point registers to the InterfaceDescriptor
480 // and use them for FP types. Currently, this works because on most
481 // platforms, all FP registers are available for use. On ia32, xmm0 is
482 // not allocatable and so we must work around that with platform-specific
483 // descriptors, adjusting the GP register set to avoid eax, which has
484 // register code 0.
485 Register reg = descriptor.GetRegisterParameter(i);
486 MachineType type = descriptor.GetParameterType(i);
487 locations.AddParam(regloc(reg, type));
488 } else {
489 // The rest of the parameters go on the stack.
490 int stack_slot = i - register_parameter_count - stack_parameter_count;
491 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
492 stack_slot, i < descriptor.GetParameterCount()
493 ? descriptor.GetParameterType(i)
494 : MachineType::AnyTagged()));
495 }
496 }
497 // Add context.
498 if (context_count) {
499 locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
500 }
501
502 // The target for stub calls depends on the requested mode.
503 CallDescriptor::Kind kind;
504 MachineType target_type;
505 switch (stub_mode) {
506 case StubCallMode::kCallCodeObject:
507 kind = CallDescriptor::kCallCodeObject;
508 target_type = MachineType::AnyTagged();
509 break;
510 #if V8_ENABLE_WEBASSEMBLY
511 case StubCallMode::kCallWasmRuntimeStub:
512 kind = CallDescriptor::kCallWasmFunction;
513 target_type = MachineType::Pointer();
514 break;
515 #endif // V8_ENABLE_WEBASSEMBLY
516 case StubCallMode::kCallBuiltinPointer:
517 kind = CallDescriptor::kCallBuiltinPointer;
518 target_type = MachineType::AnyTagged();
519 break;
520 }
521
522 RegList allocatable_registers = descriptor.allocatable_registers();
523 RegList callee_saved_registers = kNoCalleeSaved;
524 if (descriptor.CalleeSaveRegisters()) {
525 callee_saved_registers = allocatable_registers;
526 DCHECK(!callee_saved_registers.is_empty());
527 }
528 LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
529 return zone->New<CallDescriptor>( // --
530 kind, // kind
531 target_type, // target MachineType
532 target_loc, // target location
533 locations.Build(), // location_sig
534 stack_parameter_count, // stack_parameter_count
535 properties, // properties
536 callee_saved_registers, // callee-saved registers
537 kNoCalleeSavedFp, // callee-saved fp
538 CallDescriptor::kCanUseRoots | flags, // flags
539 descriptor.DebugName(), // debug name
540 descriptor.GetStackArgumentOrder(), // stack order
541 #if V8_ENABLE_WEBASSEMBLY
542 nullptr, // wasm function signature
543 #endif
544 allocatable_registers);
545 }
546
547 // static
GetBytecodeDispatchCallDescriptor(Zone * zone,const CallInterfaceDescriptor & descriptor,int stack_parameter_count)548 CallDescriptor* Linkage::GetBytecodeDispatchCallDescriptor(
549 Zone* zone, const CallInterfaceDescriptor& descriptor,
550 int stack_parameter_count) {
551 const int register_parameter_count = descriptor.GetRegisterParameterCount();
552 const int parameter_count = register_parameter_count + stack_parameter_count;
553
554 DCHECK_EQ(descriptor.GetReturnCount(), 1);
555 LocationSignature::Builder locations(zone, 1, parameter_count);
556
557 locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
558
559 // Add parameters in registers and on the stack.
560 for (int i = 0; i < parameter_count; i++) {
561 if (i < register_parameter_count) {
562 // The first parameters go in registers.
563 Register reg = descriptor.GetRegisterParameter(i);
564 MachineType type = descriptor.GetParameterType(i);
565 locations.AddParam(regloc(reg, type));
566 } else {
567 // The rest of the parameters go on the stack.
568 int stack_slot = i - register_parameter_count - stack_parameter_count;
569 locations.AddParam(LinkageLocation::ForCallerFrameSlot(
570 stack_slot, MachineType::AnyTagged()));
571 }
572 }
573
574 // The target for interpreter dispatches is a code entry address.
575 MachineType target_type = MachineType::Pointer();
576 LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
577 const CallDescriptor::Flags kFlags =
578 CallDescriptor::kCanUseRoots | CallDescriptor::kFixedTargetRegister;
579 return zone->New<CallDescriptor>( // --
580 CallDescriptor::kCallAddress, // kind
581 target_type, // target MachineType
582 target_loc, // target location
583 locations.Build(), // location_sig
584 stack_parameter_count, // stack_parameter_count
585 Operator::kNoProperties, // properties
586 kNoCalleeSaved, // callee-saved registers
587 kNoCalleeSavedFp, // callee-saved fp
588 kFlags, // flags
589 descriptor.DebugName());
590 }
591
GetOsrValueLocation(int index) const592 LinkageLocation Linkage::GetOsrValueLocation(int index) const {
593 CHECK(incoming_->IsJSFunctionCall());
594 int parameter_count = static_cast<int>(incoming_->JSParameterCount() - 1);
595 int first_stack_slot = OsrHelper::FirstStackSlotIndex(parameter_count);
596
597 if (index == kOsrContextSpillSlotIndex) {
598 // Context. Use the parameter location of the context spill slot.
599 // Parameter (arity + 2) is special for the context of the function frame.
600 // >> context_index = target + receiver + params + new_target + #args
601 int context_index = 1 + 1 + parameter_count + 1 + 1;
602 return incoming_->GetInputLocation(context_index);
603 } else if (index >= first_stack_slot) {
604 // Local variable stored in this (callee) stack.
605 int spill_index =
606 index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
607 return LinkageLocation::ForCalleeFrameSlot(spill_index,
608 MachineType::AnyTagged());
609 } else {
610 // Parameter. Use the assigned location from the incoming call descriptor.
611 int parameter_index = 1 + index; // skip index 0, which is the target.
612 return incoming_->GetInputLocation(parameter_index);
613 }
614 }
615
616 namespace {
IsTaggedReg(const LinkageLocation & loc,Register reg)617 inline bool IsTaggedReg(const LinkageLocation& loc, Register reg) {
618 return loc.IsRegister() && loc.AsRegister() == reg.code() &&
619 loc.GetType().representation() ==
620 MachineRepresentation::kTaggedPointer;
621 }
622 } // namespace
623
ParameterHasSecondaryLocation(int index) const624 bool Linkage::ParameterHasSecondaryLocation(int index) const {
625 // TODO(titzer): this should be configurable, not call-type specific.
626 if (incoming_->IsJSFunctionCall()) {
627 LinkageLocation loc = GetParameterLocation(index);
628 return IsTaggedReg(loc, kJSFunctionRegister) ||
629 IsTaggedReg(loc, kContextRegister);
630 }
631 #if V8_ENABLE_WEBASSEMBLY
632 if (incoming_->IsWasmFunctionCall()) {
633 LinkageLocation loc = GetParameterLocation(index);
634 return IsTaggedReg(loc, kWasmInstanceRegister);
635 }
636 #endif // V8_ENABLE_WEBASSEMBLY
637 return false;
638 }
639
GetParameterSecondaryLocation(int index) const640 LinkageLocation Linkage::GetParameterSecondaryLocation(int index) const {
641 // TODO(titzer): these constants are necessary due to offset/slot# mismatch
642 static const int kJSContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
643 static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;
644
645 DCHECK(ParameterHasSecondaryLocation(index));
646 LinkageLocation loc = GetParameterLocation(index);
647
648 // TODO(titzer): this should be configurable, not call-type specific.
649 if (incoming_->IsJSFunctionCall()) {
650 if (IsTaggedReg(loc, kJSFunctionRegister)) {
651 return LinkageLocation::ForCalleeFrameSlot(kJSFunctionSlot,
652 MachineType::AnyTagged());
653 } else {
654 DCHECK(IsTaggedReg(loc, kContextRegister));
655 return LinkageLocation::ForCalleeFrameSlot(kJSContextSlot,
656 MachineType::AnyTagged());
657 }
658 }
659 #if V8_ENABLE_WEBASSEMBLY
660 static const int kWasmInstanceSlot = 3 + StandardFrameConstants::kCPSlotCount;
661 if (incoming_->IsWasmFunctionCall()) {
662 DCHECK(IsTaggedReg(loc, kWasmInstanceRegister));
663 return LinkageLocation::ForCalleeFrameSlot(kWasmInstanceSlot,
664 MachineType::AnyTagged());
665 }
666 #endif // V8_ENABLE_WEBASSEMBLY
667 UNREACHABLE();
668 }
669
670
671 } // namespace compiler
672 } // namespace internal
673 } // namespace v8
674