V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
linkage.cc
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/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"
16 
17 namespace v8 {
18 namespace internal {
19 namespace compiler {
20 
21 namespace {
22 
23 inline LinkageLocation regloc(Register reg, MachineType type) {
24  return LinkageLocation::ForRegister(reg.code(), type);
25 }
26 
27 } // namespace
28 
29 
30 std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
31  switch (k) {
32  case CallDescriptor::kCallCodeObject:
33  os << "Code";
34  break;
35  case CallDescriptor::kCallJSFunction:
36  os << "JS";
37  break;
38  case CallDescriptor::kCallAddress:
39  os << "Addr";
40  break;
41  case CallDescriptor::kCallWasmFunction:
42  os << "WasmFunction";
43  break;
44  case CallDescriptor::kCallWasmImportWrapper:
45  os << "WasmImportWrapper";
46  break;
47  }
48  return os;
49 }
50 
51 
52 std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
53  // TODO(svenpanne) Output properties etc. and be less cryptic.
54  return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
55  << "s" << d.StackParameterCount() << "i" << d.InputCount() << "f"
56  << d.FrameStateCount();
57 }
58 
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);
63  int current = 0;
64  for (size_t i = 0; i < return_count; ++i) {
65  types[current++] = GetReturnType(i);
66  }
67  for (size_t i = 0; i < param_count; ++i) {
68  types[current++] = GetParameterType(i);
69  }
70  return new (zone) MachineSignature(return_count, param_count, types);
71 }
72 
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;
78  }
79  return true;
80 }
81 
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()) {
87  int new_candidate =
88  -operand.GetLocation() + operand.GetSizeInPointers() - 1;
89  if (new_candidate > slots_above_sp) {
90  slots_above_sp = new_candidate;
91  }
92  }
93  }
94  return slots_above_sp;
95 }
96 
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;
102  if (kPadArguments) {
103  // Adjust stack delta when it is odd.
104  if (stack_param_delta % 2 != 0) {
105  if (callee_slots_above_sp % 2 != 0) {
106  // The delta is odd due to the callee - we will need to add one slot
107  // of padding.
108  ++stack_param_delta;
109  } else {
110  // The delta is odd because of the caller. We already have one slot of
111  // padding that we can reuse for arguments, so we will need one fewer
112  // slot.
113  --stack_param_delta;
114  }
115  }
116  }
117  return stack_param_delta;
118 }
119 
120 bool CallDescriptor::CanTailCall(const Node* node) const {
121  return HasSameReturnLocationsAs(CallDescriptorOf(node->op()));
122 }
123 
124 int CallDescriptor::CalculateFixedFrameSize() const {
125  switch (kind_) {
126  case kCallJSFunction:
127  return PushArgumentCount()
128  ? OptimizedBuiltinFrameConstants::kFixedSlotCount
129  : StandardFrameConstants::kFixedSlotCount;
130  case kCallAddress:
131  return CommonFrameConstants::kFixedSlotCountAboveFp +
132  CommonFrameConstants::kCPSlotCount;
133  case kCallCodeObject:
134  return TypedFrameConstants::kFixedSlotCount;
135  case kCallWasmFunction:
136  case kCallWasmImportWrapper:
137  return WasmCompiledFrameConstants::kFixedSlotCount;
138  }
139  UNREACHABLE();
140 }
141 
142 CallDescriptor* Linkage::ComputeIncoming(Zone* zone,
143  OptimizedCompilationInfo* info) {
144  DCHECK(!info->IsStub());
145  if (!info->closure().is_null()) {
146  // If we are compiling a JS function, use a JS call descriptor,
147  // plus the receiver.
148  SharedFunctionInfo* shared = info->closure()->shared();
149  return GetJSCallDescriptor(zone, info->is_osr(),
150  1 + shared->internal_formal_parameter_count(),
151  CallDescriptor::kCanUseRoots);
152  }
153  return nullptr; // TODO(titzer): ?
154 }
155 
156 
157 // static
158 bool Linkage::NeedsFrameStateInput(Runtime::FunctionId function) {
159  switch (function) {
160  // Most runtime functions need a FrameState. A few chosen ones that we know
161  // not to call into arbitrary JavaScript, not to throw, and not to lazily
162  // deoptimize are whitelisted here and can be called without a FrameState.
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: // TODO(conradw): Is it safe?
180  case Runtime::kTraceEnter:
181  case Runtime::kTraceExit:
182  return false;
183 
184  // Some inline intrinsics are also safe to call without a FrameState.
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:
194  return false;
195 
196  default:
197  break;
198  }
199 
200  // For safety, default to needing a FrameState unless whitelisted.
201  return true;
202 }
203 
204 
205 bool CallDescriptor::UsesOnlyRegisters() const {
206  for (size_t i = 0; i < InputCount(); ++i) {
207  if (!GetInputLocation(i).IsRegister()) return false;
208  }
209  for (size_t i = 0; i < ReturnCount(); ++i) {
210  if (!GetReturnLocation(i).IsRegister()) return false;
211  }
212  return true;
213 }
214 
215 
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;
222 
223  if (!Linkage::NeedsFrameStateInput(function_id)) {
224  flags = static_cast<CallDescriptor::Flags>(
225  flags & ~CallDescriptor::kNeedsFrameState);
226  }
227 
228  return GetCEntryStubCallDescriptor(zone, return_count, js_parameter_count,
229  debug_name, properties, flags);
230 }
231 
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;
242 
243  LocationSignature::Builder locations(zone, static_cast<size_t>(return_count),
244  static_cast<size_t>(parameter_count));
245 
246  // Add returns.
247  if (locations.return_count_ > 0) {
248  locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
249  }
250  if (locations.return_count_ > 1) {
251  locations.AddReturn(regloc(kReturnRegister1, MachineType::AnyTagged()));
252  }
253  if (locations.return_count_ > 2) {
254  locations.AddReturn(regloc(kReturnRegister2, MachineType::AnyTagged()));
255  }
256 
257  // All parameters to the runtime call go on the stack.
258  for (int i = 0; i < js_parameter_count; i++) {
259  locations.AddParam(LinkageLocation::ForCallerFrameSlot(
260  i - js_parameter_count, MachineType::AnyTagged()));
261  }
262  // Add runtime function itself.
263  locations.AddParam(
264  regloc(kRuntimeCallFunctionRegister, MachineType::Pointer()));
265 
266  // Add runtime call argument count.
267  locations.AddParam(
268  regloc(kRuntimeCallArgCountRegister, MachineType::Int32()));
269 
270  // Add context.
271  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
272 
273  // The target for runtime calls is a code object.
274  MachineType target_type = MachineType::AnyTagged();
275  LinkageLocation target_loc =
276  LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
277  return new (zone) CallDescriptor( // --
278  CallDescriptor::kCallCodeObject, // kind
279  target_type, // target MachineType
280  target_loc, // target location
281  locations.Build(), // location_sig
282  js_parameter_count, // stack_parameter_count
283  properties, // properties
284  kNoCalleeSaved, // callee-saved
285  kNoCalleeSaved, // callee-saved fp
286  flags, // flags
287  debug_name); // debug name
288 }
289 
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;
299 
300  LocationSignature::Builder locations(zone, return_count, parameter_count);
301 
302  // All JS calls have exactly one return value.
303  locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
304 
305  // All parameters to JS calls go on the stack.
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()));
310  }
311 
312  // Add JavaScript call new target value.
313  locations.AddParam(
314  regloc(kJavaScriptCallNewTargetRegister, MachineType::AnyTagged()));
315 
316  // Add JavaScript call argument count.
317  locations.AddParam(
318  regloc(kJavaScriptCallArgCountRegister, MachineType::Int32()));
319 
320  // Add context.
321  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
322 
323  // The target for JS function calls is the JSFunction object.
324  MachineType target_type = MachineType::AnyTagged();
325  // When entering into an OSR function from unoptimized code the JSFunction
326  // is not in a register, but it is on the stack in the marker spill slot.
327  LinkageLocation target_loc =
328  is_osr ? LinkageLocation::ForSavedCallerFunction()
329  : regloc(kJSFunctionRegister, MachineType::AnyTagged());
330  return new (zone) CallDescriptor( // --
331  CallDescriptor::kCallJSFunction, // kind
332  target_type, // target MachineType
333  target_loc, // target location
334  locations.Build(), // location_sig
335  js_parameter_count, // stack_parameter_count
336  Operator::kNoProperties, // properties
337  kNoCalleeSaved, // callee-saved
338  kNoCalleeSaved, // callee-saved fp
339  flags, // flags
340  "js-call");
341 }
342 
343 // TODO(turbofan): cache call descriptors for code stub calls.
344 // TODO(jgruber): Clean up stack parameter count handling. The descriptor
345 // already knows the formal stack parameter count and ideally only additional
346 // stack parameters should be passed into this method. All call-sites should
347 // be audited for correctness (e.g. many used to assume a stack parameter count
348 // of 0).
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);
359 
360  DCHECK_GE(stack_parameter_count, descriptor.GetStackParameterCount());
361 
362  size_t return_count = descriptor.GetReturnCount();
363  LocationSignature::Builder locations(zone, return_count, parameter_count);
364 
365  // Add returns.
366  if (locations.return_count_ > 0) {
367  locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
368  }
369  if (locations.return_count_ > 1) {
370  locations.AddReturn(regloc(kReturnRegister1, descriptor.GetReturnType(1)));
371  }
372  if (locations.return_count_ > 2) {
373  locations.AddReturn(regloc(kReturnRegister2, descriptor.GetReturnType(2)));
374  }
375 
376  // Add parameters in registers and on the stack.
377  for (int i = 0; i < js_parameter_count; i++) {
378  if (i < register_parameter_count) {
379  // The first parameters go in registers.
380  Register reg = descriptor.GetRegisterParameter(i);
381  MachineType type = descriptor.GetParameterType(i);
382  locations.AddParam(regloc(reg, type));
383  } else {
384  // The rest of the parameters go on the stack.
385  int stack_slot = i - register_parameter_count - stack_parameter_count;
386  locations.AddParam(LinkageLocation::ForCallerFrameSlot(
387  stack_slot, MachineType::AnyTagged()));
388  }
389  }
390  // Add context.
391  if (context_count) {
392  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
393  }
394 
395  // The target for stub calls depends on the requested mode.
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( // --
404  kind, // kind
405  target_type, // target MachineType
406  target_loc, // target location
407  locations.Build(), // location_sig
408  stack_parameter_count, // stack_parameter_count
409  properties, // properties
410  kNoCalleeSaved, // callee-saved registers
411  kNoCalleeSaved, // callee-saved fp
412  CallDescriptor::kCanUseRoots | flags, // flags
413  descriptor.DebugName(), // debug name
414  descriptor.allocatable_registers());
415 }
416 
417 // static
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;
423 
424  DCHECK_EQ(descriptor.GetReturnCount(), 1);
425  LocationSignature::Builder locations(zone, 1, parameter_count);
426 
427  locations.AddReturn(regloc(kReturnRegister0, descriptor.GetReturnType(0)));
428 
429  // Add parameters in registers and on the stack.
430  for (int i = 0; i < parameter_count; i++) {
431  if (i < register_parameter_count) {
432  // The first parameters go in registers.
433  Register reg = descriptor.GetRegisterParameter(i);
434  MachineType type = descriptor.GetParameterType(i);
435  locations.AddParam(regloc(reg, type));
436  } else {
437  // The rest of the parameters go on the stack.
438  int stack_slot = i - register_parameter_count - stack_parameter_count;
439  locations.AddParam(LinkageLocation::ForCallerFrameSlot(
440  stack_slot, MachineType::AnyTagged()));
441  }
442  }
443 
444  // The target for interpreter dispatches is a code entry address.
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, // kind
451  target_type, // target MachineType
452  target_loc, // target location
453  locations.Build(), // location_sig
454  stack_parameter_count, // stack_parameter_count
455  Operator::kNoProperties, // properties
456  kNoCalleeSaved, // callee-saved registers
457  kNoCalleeSaved, // callee-saved fp
458  kFlags, // flags
459  descriptor.DebugName());
460 }
461 
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);
466 
467  if (index == kOsrContextSpillSlotIndex) {
468  // Context. Use the parameter location of the context spill slot.
469  // Parameter (arity + 2) is special for the context of the function frame.
470  // >> context_index = target + receiver + params + new_target + #args
471  int context_index = 1 + 1 + parameter_count + 1 + 1;
472  return incoming_->GetInputLocation(context_index);
473  } else if (index >= first_stack_slot) {
474  // Local variable stored in this (callee) stack.
475  int spill_index =
476  index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
477  return LinkageLocation::ForCalleeFrameSlot(spill_index,
478  MachineType::AnyTagged());
479  } else {
480  // Parameter. Use the assigned location from the incoming call descriptor.
481  int parameter_index = 1 + index; // skip index 0, which is the target.
482  return incoming_->GetInputLocation(parameter_index);
483  }
484 }
485 
486 namespace {
487 inline bool IsTaggedReg(const LinkageLocation& loc, Register reg) {
488  return loc.IsRegister() && loc.AsRegister() == reg.code() &&
489  loc.GetType().representation() ==
490  MachineRepresentation::kTaggedPointer;
491 }
492 } // namespace
493 
494 bool Linkage::ParameterHasSecondaryLocation(int index) const {
495  // TODO(titzer): this should be configurable, not call-type specific.
496  if (incoming_->IsJSFunctionCall()) {
497  LinkageLocation loc = GetParameterLocation(index);
498  return IsTaggedReg(loc, kJSFunctionRegister) ||
499  IsTaggedReg(loc, kContextRegister);
500  }
501  if (incoming_->IsWasmFunctionCall()) {
502  LinkageLocation loc = GetParameterLocation(index);
503  return IsTaggedReg(loc, kWasmInstanceRegister);
504  }
505  return false;
506 }
507 
508 LinkageLocation Linkage::GetParameterSecondaryLocation(int index) const {
509  // TODO(titzer): these constants are necessary due to offset/slot# mismatch
510  static const int kJSContextSlot = 2 + StandardFrameConstants::kCPSlotCount;
511  static const int kJSFunctionSlot = 3 + StandardFrameConstants::kCPSlotCount;
512  static const int kWasmInstanceSlot = 3 + StandardFrameConstants::kCPSlotCount;
513 
514  DCHECK(ParameterHasSecondaryLocation(index));
515  LinkageLocation loc = GetParameterLocation(index);
516 
517  // TODO(titzer): this should be configurable, not call-type specific.
518  if (incoming_->IsJSFunctionCall()) {
519  if (IsTaggedReg(loc, kJSFunctionRegister)) {
520  return LinkageLocation::ForCalleeFrameSlot(kJSFunctionSlot,
521  MachineType::AnyTagged());
522  } else {
523  DCHECK(IsTaggedReg(loc, kContextRegister));
524  return LinkageLocation::ForCalleeFrameSlot(kJSContextSlot,
525  MachineType::AnyTagged());
526  }
527  }
528  if (incoming_->IsWasmFunctionCall()) {
529  DCHECK(IsTaggedReg(loc, kWasmInstanceRegister));
530  return LinkageLocation::ForCalleeFrameSlot(kWasmInstanceSlot,
531  MachineType::AnyTagged());
532  }
533  UNREACHABLE();
534  return LinkageLocation::ForCalleeFrameSlot(0, MachineType::AnyTagged());
535 }
536 
537 
538 } // namespace compiler
539 } // namespace internal
540 } // namespace v8
Definition: libplatform.h:13