linkage.h 16.8 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#ifndef V8_COMPILER_LINKAGE_H_
#define V8_COMPILER_LINKAGE_H_

8
#include "src/base/compiler-specific.h"
9
#include "src/base/flags.h"
10 11
#include "src/compiler/frame.h"
#include "src/compiler/operator.h"
12
#include "src/globals.h"
13
#include "src/interface-descriptors.h"
14
#include "src/machine-type.h"
15
#include "src/reglist.h"
16
#include "src/runtime/runtime.h"
17
#include "src/zone/zone.h"
18 19 20

namespace v8 {
namespace internal {
21 22

class CallInterfaceDescriptor;
23
class CompilationInfo;
24

25 26
namespace compiler {

27 28
const RegList kNoCalleeSaved = 0;

29
class Node;
30 31
class OsrHelper;

32 33 34
// Describes the location for a parameter or a return value to a call.
class LinkageLocation {
 public:
35 36 37
  bool operator==(const LinkageLocation& other) const {
    return bit_field_ == other.bit_field_;
  }
38

39 40
  bool operator!=(const LinkageLocation& other) const {
    return !(*this == other);
svenpanne's avatar
svenpanne committed
41 42
  }

43 44 45
  static LinkageLocation ForAnyRegister(
      MachineType type = MachineType::None()) {
    return LinkageLocation(REGISTER, ANY_REGISTER, type);
46
  }
47

48 49
  static LinkageLocation ForRegister(int32_t reg,
                                     MachineType type = MachineType::None()) {
50
    DCHECK_LE(0, reg);
51
    return LinkageLocation(REGISTER, reg, type);
52
  }
53

54
  static LinkageLocation ForCallerFrameSlot(int32_t slot, MachineType type) {
55
    DCHECK_GT(0, slot);
56
    return LinkageLocation(STACK_SLOT, slot, type);
svenpanne's avatar
svenpanne committed
57 58
  }

59
  static LinkageLocation ForCalleeFrameSlot(int32_t slot, MachineType type) {
60 61
    // TODO(titzer): bailout instead of crashing here.
    DCHECK(slot >= 0 && slot < LinkageLocation::MAX_STACK_SLOT);
62
    return LinkageLocation(STACK_SLOT, slot, type);
svenpanne's avatar
svenpanne committed
63 64
  }

65 66 67
  static LinkageLocation ForSavedCallerReturnAddress() {
    return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
                               StandardFrameConstants::kCallerPCOffset) /
68 69
                                  kPointerSize,
                              MachineType::Pointer());
70 71 72 73 74
  }

  static LinkageLocation ForSavedCallerFramePtr() {
    return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
                               StandardFrameConstants::kCallerFPOffset) /
75 76
                                  kPointerSize,
                              MachineType::Pointer());
77 78 79 80 81 82
  }

  static LinkageLocation ForSavedCallerConstantPool() {
    DCHECK(V8_EMBEDDED_CONSTANT_POOL);
    return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
                               StandardFrameConstants::kConstantPoolOffset) /
83 84
                                  kPointerSize,
                              MachineType::AnyTagged());
85 86
  }

87
  static LinkageLocation ForSavedCallerFunction() {
88
    return ForCalleeFrameSlot((StandardFrameConstants::kCallerPCOffset -
89
                               StandardFrameConstants::kFunctionOffset) /
90 91
                                  kPointerSize,
                              MachineType::AnyTagged());
92 93
  }

94 95 96 97
  static LinkageLocation ConvertToTailCallerLocation(
      LinkageLocation caller_location, int stack_param_delta) {
    if (!caller_location.IsRegister()) {
      return LinkageLocation(STACK_SLOT,
98 99
                             caller_location.GetLocation() + stack_param_delta,
                             caller_location.GetType());
100 101 102 103
    }
    return caller_location;
  }

104 105 106 107 108 109 110 111 112
  MachineType GetType() const { return machine_type_; }

  int GetSize() const {
    return 1 << ElementSizeLog2Of(GetType().representation());
  }

  int GetSizeInPointers() const {
    // Round up
    return (GetSize() + kPointerSize - 1) / kPointerSize;
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  }

  int32_t GetLocation() const {
    return static_cast<int32_t>(bit_field_ & LocationField::kMask) >>
           LocationField::kShift;
  }

  bool IsRegister() const { return TypeField::decode(bit_field_) == REGISTER; }
  bool IsAnyRegister() const {
    return IsRegister() && GetLocation() == ANY_REGISTER;
  }
  bool IsCallerFrameSlot() const { return !IsRegister() && GetLocation() < 0; }
  bool IsCalleeFrameSlot() const { return !IsRegister() && GetLocation() >= 0; }

  int32_t AsRegister() const {
    DCHECK(IsRegister());
    return GetLocation();
  }
  int32_t AsCallerFrameSlot() const {
    DCHECK(IsCallerFrameSlot());
    return GetLocation();
  }
  int32_t AsCalleeFrameSlot() const {
    DCHECK(IsCalleeFrameSlot());
    return GetLocation();
  }

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
 private:
  enum LocationType { REGISTER, STACK_SLOT };

  class TypeField : public BitField<LocationType, 0, 1> {};
  class LocationField : public BitField<int32_t, TypeField::kNext, 31> {};

  static constexpr int32_t ANY_REGISTER = -1;
  static constexpr int32_t MAX_STACK_SLOT = 32767;

  LinkageLocation(LocationType type, int32_t location,
                  MachineType machine_type) {
    bit_field_ = TypeField::encode(type) |
                 ((location << LocationField::kShift) & LocationField::kMask);
    machine_type_ = machine_type;
  }

156
  int32_t bit_field_;
157
  MachineType machine_type_;
158 159
};

160
typedef Signature<LinkageLocation> LocationSignature;
161

162 163
// Describes a call to various parts of the compiler. Every call has the notion
// of a "target", which is the first input to the call.
164 165
class V8_EXPORT_PRIVATE CallDescriptor final
    : public NON_EXPORTED_BASE(ZoneObject) {
166
 public:
167 168
  // Describes the kind of this call, which determines the target.
  enum Kind {
169 170 171 172
    kCallCodeObject,   // target is a Code object
    kCallJSFunction,   // target is a JSFunction object
    kCallAddress,      // target is a machine pointer
    kCallWasmFunction  // target is a wasm function
173
  };
174

175 176
  enum Flag {
    kNoFlags = 0u,
177
    kNeedsFrameState = 1u << 0,
178
    kHasExceptionHandler = 1u << 1,
179
    kCanUseRoots = 1u << 2,
180
    // Causes the code generator to initialize the root register.
181
    kInitializeRootRegister = 1u << 3,
182
    // Does not ever try to allocate space on our heap.
183
    kNoAllocate = 1u << 4,
184
    // Push argument count as part of function prologue.
185 186 187
    kPushArgumentCount = 1u << 5,
    // Use retpoline for this call if indirect.
    kRetpoline = 1u << 6
188
  };
189
  typedef base::Flags<Flag> Flags;
190

191
  CallDescriptor(Kind kind, MachineType target_type, LinkageLocation target_loc,
192
                 LocationSignature* location_sig, size_t stack_param_count,
193
                 Operator::Properties properties,
194 195
                 RegList callee_saved_registers,
                 RegList callee_saved_fp_registers, Flags flags,
196
                 const char* debug_name = "",
197 198
                 const RegList allocatable_registers = 0,
                 size_t stack_return_count = 0)
199
      : kind_(kind),
200 201 202
        target_type_(target_type),
        target_loc_(target_loc),
        location_sig_(location_sig),
203
        stack_param_count_(stack_param_count),
204
        stack_return_count_(stack_return_count),
205 206
        properties_(properties),
        callee_saved_registers_(callee_saved_registers),
207
        callee_saved_fp_registers_(callee_saved_fp_registers),
208
        allocatable_registers_(allocatable_registers),
209
        flags_(flags),
210
        debug_name_(debug_name) {}
211

212 213 214
  // Returns the kind of this call.
  Kind kind() const { return kind_; }

215 216 217
  // Returns {true} if this descriptor is a call to a C function.
  bool IsCFunctionCall() const { return kind_ == kCallAddress; }

218 219 220
  // Returns {true} if this descriptor is a call to a JSFunction.
  bool IsJSFunctionCall() const { return kind_ == kCallJSFunction; }

221 222 223 224
  bool RequiresFrameAsIncoming() const {
    return IsCFunctionCall() || IsJSFunctionCall();
  }

225
  // The number of return values from this call.
226
  size_t ReturnCount() const { return location_sig_->return_count(); }
227

228
  // The number of C parameters to this call.
229
  size_t ParameterCount() const { return location_sig_->parameter_count(); }
230

231 232 233
  // The number of stack parameters to the call.
  size_t StackParameterCount() const { return stack_param_count_; }

234 235 236
  // The number of stack return values from the call.
  size_t StackReturnCount() const { return stack_return_count_; }

237 238 239 240 241
  // The number of parameters to the JS function call.
  size_t JSParameterCount() const {
    DCHECK(IsJSFunctionCall());
    return stack_param_count_;
  }
242

243 244 245
  // The total number of inputs to this call, which includes the target,
  // receiver, context, etc.
  // TODO(titzer): this should input the framestate input too.
246
  size_t InputCount() const { return 1 + location_sig_->parameter_count(); }
247

248
  size_t FrameStateCount() const { return NeedsFrameState() ? 1 : 0; }
249

250
  Flags flags() const { return flags_; }
251

252
  bool NeedsFrameState() const { return flags() & kNeedsFrameState; }
253
  bool PushArgumentCount() const { return flags() & kPushArgumentCount; }
254 255 256
  bool InitializeRootRegister() const {
    return flags() & kInitializeRootRegister;
  }
257

258 259
  LinkageLocation GetReturnLocation(size_t index) const {
    return location_sig_->GetReturn(index);
260 261
  }

262 263 264 265 266
  LinkageLocation GetInputLocation(size_t index) const {
    if (index == 0) return target_loc_;
    return location_sig_->GetParam(index - 1);
  }

267
  MachineSignature* GetMachineSignature(Zone* zone) const;
268 269

  MachineType GetReturnType(size_t index) const {
270
    return location_sig_->GetReturn(index).GetType();
271 272 273 274
  }

  MachineType GetInputType(size_t index) const {
    if (index == 0) return target_type_;
275 276 277 278 279
    return location_sig_->GetParam(index - 1).GetType();
  }

  MachineType GetParameterType(size_t index) const {
    return location_sig_->GetParam(index).GetType();
280 281 282
  }

  // Operator properties describe how this call can be optimized, if at all.
283
  Operator::Properties properties() const { return properties_; }
284 285

  // Get the callee-saved registers, if any, across this call.
286
  RegList CalleeSavedRegisters() const { return callee_saved_registers_; }
287

288 289 290
  // Get the callee-saved FP registers, if any, across this call.
  RegList CalleeSavedFPRegisters() const { return callee_saved_fp_registers_; }

291 292
  const char* debug_name() const { return debug_name_; }

svenpanne's avatar
svenpanne committed
293 294 295 296
  bool UsesOnlyRegisters() const;

  bool HasSameReturnLocationsAs(const CallDescriptor* other) const;

297 298 299
  // Returns the first stack slot that is not used by the stack parameters.
  int GetFirstUnusedStackSlot() const;

300
  int GetStackParameterDelta(const CallDescriptor* tail_caller) const;
301 302

  bool CanTailCall(const Node* call) const;
303 304

  int CalculateFixedFrameSize() const;
305

306 307 308 309 310 311
  RegList AllocatableRegisters() const { return allocatable_registers_; }

  bool HasRestrictedAllocatableRegisters() const {
    return allocatable_registers_ != 0;
  }

312 313 314 315
  void set_save_fp_mode(SaveFPRegsMode mode) { save_fp_mode_ = mode; }

  SaveFPRegsMode get_save_fp_mode() const { return save_fp_mode_; }

316 317
 private:
  friend class Linkage;
318
  SaveFPRegsMode save_fp_mode_ = kSaveFPRegs;
319

320 321 322 323
  const Kind kind_;
  const MachineType target_type_;
  const LinkageLocation target_loc_;
  const LocationSignature* const location_sig_;
324
  const size_t stack_param_count_;
325
  const size_t stack_return_count_;
326 327
  const Operator::Properties properties_;
  const RegList callee_saved_registers_;
328
  const RegList callee_saved_fp_registers_;
329 330 331
  // Non-zero value means restricting the set of allocatable registers for
  // register allocator to use.
  const RegList allocatable_registers_;
332 333 334 335
  const Flags flags_;
  const char* const debug_name_;

  DISALLOW_COPY_AND_ASSIGN(CallDescriptor);
336 337
};

338 339
DEFINE_OPERATORS_FOR_FLAGS(CallDescriptor::Flags)

340
std::ostream& operator<<(std::ostream& os, const CallDescriptor& d);
341 342
V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const CallDescriptor::Kind& k);
343 344 345 346 347 348 349 350

// Defines the linkage for a compilation, including the calling conventions
// for incoming parameters and return value(s) as well as the outgoing calling
// convention for any kind of call. Linkage is generally architecture-specific.
//
// Can be used to translate {arg_index} (i.e. index of the call node input) as
// well as {param_index} (i.e. as stored in parameter nodes) into an operator
// representing the architecture-specific location. The following call node
351
// layouts are supported (where {n} is the number of value inputs):
352
//
353 354 355 356
//                        #0          #1     #2     [...]             #n
// Call[CodeStub]         code,       arg 1, arg 2, [...],            context
// Call[JSFunction]       function,   rcvr,  arg 1, [...], new, #arg, context
// Call[Runtime]          CEntryStub, arg 1, arg 2, [...], fun, #arg, context
357
// Call[BytecodeDispatch] address,    arg 1, arg 2, [...]
358
class V8_EXPORT_PRIVATE Linkage : public NON_EXPORTED_BASE(ZoneObject) {
359
 public:
360 361
  enum ContextSpecification { kNoContext, kPassContext };

362
  explicit Linkage(CallDescriptor* incoming) : incoming_(incoming) {}
363 364

  static CallDescriptor* ComputeIncoming(Zone* zone, CompilationInfo* info);
365 366 367

  // The call descriptor for this compilation unit describes the locations
  // of incoming parameters and the outgoing return value(s).
368
  CallDescriptor* GetIncomingDescriptor() const { return incoming_; }
369 370
  static CallDescriptor* GetJSCallDescriptor(Zone* zone, bool is_osr,
                                             int parameter_count,
371
                                             CallDescriptor::Flags flags);
372

373
  static CallDescriptor* GetRuntimeCallDescriptor(
374
      Zone* zone, Runtime::FunctionId function, int js_parameter_count,
375
      Operator::Properties properties, CallDescriptor::Flags flags);
376

377 378 379 380 381
  static CallDescriptor* GetCEntryStubCallDescriptor(
      Zone* zone, int return_count, int js_parameter_count,
      const char* debug_name, Operator::Properties properties,
      CallDescriptor::Flags flags);

382
  static CallDescriptor* GetStubCallDescriptor(
383 384
      Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
      int stack_parameter_count, CallDescriptor::Flags flags,
385
      Operator::Properties properties = Operator::kNoProperties,
386
      MachineType return_type = MachineType::AnyTagged(),
387 388
      size_t return_count = 1,
      ContextSpecification context_spec = kPassContext);
389

390
  static CallDescriptor* GetAllocateCallDescriptor(Zone* zone);
391 392 393 394
  static CallDescriptor* GetBytecodeDispatchCallDescriptor(
      Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
      int stack_parameter_count);

395 396 397 398
  // Creates a call descriptor for simplified C calls that is appropriate
  // for the host platform. This simplified calling convention only supports
  // integers and pointers of one word size each, i.e. no floating point,
  // structs, pointers to members, etc.
399 400 401
  static CallDescriptor* GetSimplifiedCDescriptor(
      Zone* zone, const MachineSignature* sig,
      bool set_initialize_root_flag = false);
402 403

  // Get the location of an (incoming) parameter to this function.
404
  LinkageLocation GetParameterLocation(int index) const {
405 406 407 408
    return incoming_->GetInputLocation(index + 1);  // + 1 to skip target.
  }

  // Get the machine type of an (incoming) parameter to this function.
409
  MachineType GetParameterType(int index) const {
410
    return incoming_->GetInputType(index + 1);  // + 1 to skip target.
411 412 413
  }

  // Get the location where this function should place its return value.
414 415
  LinkageLocation GetReturnLocation(size_t index = 0) const {
    return incoming_->GetReturnLocation(index);
416 417
  }

418
  // Get the machine type of this function's return value.
419 420 421
  MachineType GetReturnType(size_t index = 0) const {
    return incoming_->GetReturnType(index);
  }
422

423 424 425
  bool ParameterHasSecondaryLocation(int index) const;
  LinkageLocation GetParameterSecondaryLocation(int index) const;

426
  static bool NeedsFrameStateInput(Runtime::FunctionId function);
427

428 429 430
  // Get the location where an incoming OSR value is stored.
  LinkageLocation GetOsrValueLocation(int index) const;

431 432 433 434 435
  // A special {Parameter} index for Stub Calls that represents context.
  static int GetStubCallContextParamIndex(int parameter_count) {
    return parameter_count + 0;  // Parameter (arity + 0) is special.
  }

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  // A special {Parameter} index for JSCalls that represents the new target.
  static int GetJSCallNewTargetParamIndex(int parameter_count) {
    return parameter_count + 0;  // Parameter (arity + 0) is special.
  }

  // A special {Parameter} index for JSCalls that represents the argument count.
  static int GetJSCallArgCountParamIndex(int parameter_count) {
    return parameter_count + 1;  // Parameter (arity + 1) is special.
  }

  // A special {Parameter} index for JSCalls that represents the context.
  static int GetJSCallContextParamIndex(int parameter_count) {
    return parameter_count + 2;  // Parameter (arity + 2) is special.
  }

  // A special {Parameter} index for JSCalls that represents the closure.
  static const int kJSCallClosureParamIndex = -1;
453

454 455 456
  // A special {OsrValue} index to indicate the context spill slot.
  static const int kOsrContextSpillSlotIndex = -1;

457 458 459
  // A special {OsrValue} index to indicate the accumulator register.
  static const int kOsrAccumulatorRegisterIndex = -1;

460
 private:
461 462 463
  CallDescriptor* const incoming_;

  DISALLOW_COPY_AND_ASSIGN(Linkage);
464
};
465 466 467 468

}  // namespace compiler
}  // namespace internal
}  // namespace v8
469 470

#endif  // V8_COMPILER_LINKAGE_H_