linkage.cc 19.6 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
#include "src/ast/scopes.h"
8
#include "src/code-stubs.h"
9
#include "src/compilation-info.h"
10
#include "src/compiler/common-operator.h"
11
#include "src/compiler/frame.h"
12
#include "src/compiler/node.h"
13
#include "src/compiler/osr.h"
14
#include "src/compiler/pipeline.h"
15
#include "src/objects-inl.h"
16 17 18 19 20

namespace v8 {
namespace internal {
namespace compiler {

21 22
namespace {

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

}  // namespace

29

30
std::ostream& operator<<(std::ostream& os, const CallDescriptor::Kind& k) {
31 32 33 34 35 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;
  }
  return os;
}


46
std::ostream& operator<<(std::ostream& os, const CallDescriptor& d) {
47 48
  // TODO(svenpanne) Output properties etc. and be less cryptic.
  return os << d.kind() << ":" << d.debug_name() << ":r" << d.ReturnCount()
49
            << "s" << d.StackParameterCount() << "i" << d.InputCount() << "f"
svenpanne's avatar
svenpanne committed
50 51 52
            << d.FrameStateCount() << "t" << d.SupportsTailCalls();
}

53 54 55
MachineSignature* CallDescriptor::GetMachineSignature(Zone* zone) const {
  size_t param_count = ParameterCount();
  size_t return_count = ReturnCount();
56
  MachineType* types = zone->NewArray<MachineType>(param_count + return_count);
57 58 59 60 61 62 63 64 65
  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);
  }
  return new (zone) MachineSignature(return_count, param_count, types);
}
svenpanne's avatar
svenpanne committed
66 67 68 69 70 71 72 73

bool CallDescriptor::HasSameReturnLocationsAs(
    const CallDescriptor* other) const {
  if (ReturnCount() != other->ReturnCount()) return false;
  for (size_t i = 0; i < ReturnCount(); ++i) {
    if (GetReturnLocation(i) != other->GetReturnLocation(i)) return false;
  }
  return true;
74 75
}

76 77 78 79 80 81 82 83 84 85
int CallDescriptor::GetStackParameterDelta(
    CallDescriptor const* tail_caller) const {
  int callee_slots_above_sp = 0;
  for (size_t i = 0; i < InputCount(); ++i) {
    LinkageLocation operand = GetInputLocation(i);
    if (!operand.IsRegister()) {
      int new_candidate =
          -operand.GetLocation() + operand.GetSizeInPointers() - 1;
      if (new_candidate > callee_slots_above_sp) {
        callee_slots_above_sp = new_candidate;
86 87
      }
    }
88 89 90 91 92 93 94 95 96 97 98
  }
  int tail_caller_slots_above_sp = 0;
  if (tail_caller != nullptr) {
    for (size_t i = 0; i < tail_caller->InputCount(); ++i) {
      LinkageLocation operand = tail_caller->GetInputLocation(i);
      if (!operand.IsRegister()) {
        int new_candidate =
            -operand.GetLocation() + operand.GetSizeInPointers() - 1;
        if (new_candidate > tail_caller_slots_above_sp) {
          tail_caller_slots_above_sp = new_candidate;
        }
99 100 101
      }
    }
  }
102 103 104 105
  return callee_slots_above_sp - tail_caller_slots_above_sp;
}

bool CallDescriptor::CanTailCall(const Node* node) const {
106
  return HasSameReturnLocationsAs(CallDescriptorOf(node->op()));
107 108
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
int CallDescriptor::CalculateFixedFrameSize() const {
  switch (kind_) {
    case kCallJSFunction:
      return PushArgumentCount()
                 ? OptimizedBuiltinFrameConstants::kFixedSlotCount
                 : StandardFrameConstants::kFixedSlotCount;
      break;
    case kCallAddress:
      return CommonFrameConstants::kFixedSlotCountAboveFp +
             CommonFrameConstants::kCPSlotCount;
      break;
    case kCallCodeObject:
      return TypedFrameConstants::kFixedSlotCount;
  }
  UNREACHABLE();
  return 0;
}
126

127
CallDescriptor* Linkage::ComputeIncoming(Zone* zone, CompilationInfo* info) {
128
  DCHECK(!info->IsStub());
129
  if (!info->closure().is_null()) {
130 131 132
    // If we are compiling a JS function, use a JS call descriptor,
    // plus the receiver.
    SharedFunctionInfo* shared = info->closure()->shared();
133
    return GetJSCallDescriptor(zone, info->is_osr(),
134
                               1 + shared->internal_formal_parameter_count(),
135
                               CallDescriptor::kNoFlags);
136
  }
137
  return nullptr;  // TODO(titzer): ?
138 139 140
}


141
// static
142
bool Linkage::NeedsFrameStateInput(Runtime::FunctionId function) {
143
  switch (function) {
144
    // Most runtime functions need a FrameState. A few chosen ones that we know
145 146
    // not to call into arbitrary JavaScript, not to throw, and not to lazily
    // deoptimize are whitelisted here and can be called without a FrameState.
147
    case Runtime::kAbort:
148
    case Runtime::kAllocateInTargetSpace:
149
    case Runtime::kConvertReceiver:
150
    case Runtime::kCreateIterResultObject:
151 152
    case Runtime::kDefineGetterPropertyUnchecked:  // TODO(jarin): Is it safe?
    case Runtime::kDefineSetterPropertyUnchecked:  // TODO(jarin): Is it safe?
153
    case Runtime::kGeneratorGetContinuation:
154
    case Runtime::kIsFunction:
155
    case Runtime::kNewClosure:
156
    case Runtime::kNewClosure_Tenured:
157 158 159 160
    case Runtime::kNewFunctionContext:
    case Runtime::kPushBlockContext:
    case Runtime::kPushCatchContext:
    case Runtime::kReThrow:
161
    case Runtime::kStringCompare:
162
    case Runtime::kStringEqual:
163 164 165 166 167
    case Runtime::kStringNotEqual:
    case Runtime::kStringLessThan:
    case Runtime::kStringLessThanOrEqual:
    case Runtime::kStringGreaterThan:
    case Runtime::kStringGreaterThanOrEqual:
168
    case Runtime::kToFastProperties:  // TODO(conradw): Is it safe?
169 170
    case Runtime::kTraceEnter:
    case Runtime::kTraceExit:
171
      return false;
172 173

    // Some inline intrinsics are also safe to call without a FrameState.
174
    case Runtime::kInlineClassOf:
175 176 177 178 179 180 181 182 183 184 185 186 187
    case Runtime::kInlineCreateIterResultObject:
    case Runtime::kInlineFixedArrayGet:
    case Runtime::kInlineFixedArraySet:
    case Runtime::kInlineGeneratorClose:
    case Runtime::kInlineGeneratorGetInputOrDebugPos:
    case Runtime::kInlineGeneratorGetResumeMode:
    case Runtime::kInlineIsArray:
    case Runtime::kInlineIsJSReceiver:
    case Runtime::kInlineIsRegExp:
    case Runtime::kInlineIsSmi:
    case Runtime::kInlineIsTypedArray:
      return false;

188
    default:
189
      break;
190
  }
191

192
  // For safety, default to needing a FrameState unless whitelisted.
193
  return true;
194 195 196
}


svenpanne's avatar
svenpanne committed
197 198
bool CallDescriptor::UsesOnlyRegisters() const {
  for (size_t i = 0; i < InputCount(); ++i) {
199
    if (!GetInputLocation(i).IsRegister()) return false;
svenpanne's avatar
svenpanne committed
200 201
  }
  for (size_t i = 0; i < ReturnCount(); ++i) {
202
    if (!GetReturnLocation(i).IsRegister()) return false;
svenpanne's avatar
svenpanne committed
203 204 205 206 207
  }
  return true;
}


208 209
CallDescriptor* Linkage::GetRuntimeCallDescriptor(
    Zone* zone, Runtime::FunctionId function_id, int js_parameter_count,
210
    Operator::Properties properties, CallDescriptor::Flags flags) {
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  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,
    CallDescriptor::Flags flags) {
228 229 230 231 232 233 234
  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;

235 236
  LocationSignature::Builder locations(zone, static_cast<size_t>(return_count),
                                       static_cast<size_t>(parameter_count));
237 238 239

  // Add returns.
  if (locations.return_count_ > 0) {
240
    locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
241 242
  }
  if (locations.return_count_ > 1) {
243
    locations.AddReturn(regloc(kReturnRegister1, MachineType::AnyTagged()));
244
  }
245
  if (locations.return_count_ > 2) {
246
    locations.AddReturn(regloc(kReturnRegister2, MachineType::AnyTagged()));
247 248 249 250
  }

  // All parameters to the runtime call go on the stack.
  for (int i = 0; i < js_parameter_count; i++) {
251 252
    locations.AddParam(LinkageLocation::ForCallerFrameSlot(
        i - js_parameter_count, MachineType::AnyTagged()));
253 254
  }
  // Add runtime function itself.
255
  locations.AddParam(
256
      regloc(kRuntimeCallFunctionRegister, MachineType::Pointer()));
257 258

  // Add runtime call argument count.
259
  locations.AddParam(
260
      regloc(kRuntimeCallArgCountRegister, MachineType::Int32()));
261 262

  // Add context.
263
  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
264 265

  // The target for runtime calls is a code object.
266
  MachineType target_type = MachineType::AnyTagged();
267 268
  LinkageLocation target_loc =
      LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
269 270 271 272 273 274 275 276 277 278
  return new (zone) CallDescriptor(     // --
      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
279
      debug_name);                      // debug name
280 281
}

282
CallDescriptor* Linkage::GetJSCallDescriptor(Zone* zone, bool is_osr,
283
                                             int js_parameter_count,
284
                                             CallDescriptor::Flags flags) {
285 286
  const size_t return_count = 1;
  const size_t context_count = 1;
287
  const size_t new_target_count = 1;
288 289
  const size_t num_args_count = 1;
  const size_t parameter_count =
290
      js_parameter_count + new_target_count + num_args_count + context_count;
291

292
  LocationSignature::Builder locations(zone, return_count, parameter_count);
293

294
  // All JS calls have exactly one return value.
295
  locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
296 297 298 299

  // All parameters to JS calls go on the stack.
  for (int i = 0; i < js_parameter_count; i++) {
    int spill_slot_index = i - js_parameter_count;
300 301
    locations.AddParam(LinkageLocation::ForCallerFrameSlot(
        spill_slot_index, MachineType::AnyTagged()));
302
  }
303

304
  // Add JavaScript call new target value.
305 306
  locations.AddParam(
      regloc(kJavaScriptCallNewTargetRegister, MachineType::AnyTagged()));
307

308
  // Add JavaScript call argument count.
309 310
  locations.AddParam(
      regloc(kJavaScriptCallArgCountRegister, MachineType::Int32()));
311

312
  // Add context.
313
  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
314 315

  // The target for JS function calls is the JSFunction object.
316
  MachineType target_type = MachineType::AnyTagged();
317 318
  // 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.
319 320 321
  LinkageLocation target_loc =
      is_osr ? LinkageLocation::ForSavedCallerFunction()
             : regloc(kJSFunctionRegister, MachineType::AnyTagged());
322 323 324 325 326 327 328 329 330
  return new (zone) CallDescriptor(     // --
      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
331 332
      CallDescriptor::kCanUseRoots |    // flags
          flags,                        // flags
333
      "js-call");
334 335
}

336 337 338
// TODO(all): Add support for return representations/locations to
// CallInterfaceDescriptor.
// TODO(turbofan): cache call descriptors for code stub calls.
339
CallDescriptor* Linkage::GetStubCallDescriptor(
340 341
    Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
    int stack_parameter_count, CallDescriptor::Flags flags,
342 343
    Operator::Properties properties, MachineType return_type,
    size_t return_count) {
344 345 346 347 348 349 350 351 352
  const int register_parameter_count = descriptor.GetRegisterParameterCount();
  const int js_parameter_count =
      register_parameter_count + stack_parameter_count;
  const int context_count = 1;
  const size_t parameter_count =
      static_cast<size_t>(js_parameter_count + context_count);

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

353 354
  // Add returns.
  if (locations.return_count_ > 0) {
355
    locations.AddReturn(regloc(kReturnRegister0, return_type));
356 357
  }
  if (locations.return_count_ > 1) {
358
    locations.AddReturn(regloc(kReturnRegister1, return_type));
359
  }
360
  if (locations.return_count_ > 2) {
361
    locations.AddReturn(regloc(kReturnRegister2, return_type));
362
  }
363 364 365 366 367 368

  // 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.
      Register reg = descriptor.GetRegisterParameter(i);
369
      MachineType type = descriptor.GetParameterType(i);
370
      locations.AddParam(regloc(reg, type));
371 372 373
    } else {
      // The rest of the parameters go on the stack.
      int stack_slot = i - register_parameter_count - stack_parameter_count;
374 375
      locations.AddParam(LinkageLocation::ForCallerFrameSlot(
          stack_slot, MachineType::AnyTagged()));
376 377 378
    }
  }
  // Add context.
379
  locations.AddParam(regloc(kContextRegister, MachineType::AnyTagged()));
380 381

  // The target for stub calls is a code object.
382
  MachineType target_type = MachineType::AnyTagged();
383 384
  LinkageLocation target_loc =
      LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
385 386 387 388 389 390 391 392 393
  return new (zone) CallDescriptor(     // --
      CallDescriptor::kCallCodeObject,  // 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
394 395
      CallDescriptor::kCanUseRoots |    // flags
          flags,                        // flags
396
      descriptor.DebugName(isolate));
397 398
}

399 400 401 402
// static
CallDescriptor* Linkage::GetAllocateCallDescriptor(Zone* zone) {
  LocationSignature::Builder locations(zone, 1, 1);

403
  locations.AddParam(regloc(kAllocateSizeRegister, MachineType::Int32()));
404

405
  locations.AddReturn(regloc(kReturnRegister0, MachineType::AnyTagged()));
406 407 408

  // The target for allocate calls is a code object.
  MachineType target_type = MachineType::AnyTagged();
409 410
  LinkageLocation target_loc =
      LinkageLocation::ForAnyRegister(MachineType::AnyTagged());
411 412 413 414 415 416 417 418 419 420 421 422 423 424
  return new (zone) CallDescriptor(     // --
      CallDescriptor::kCallCodeObject,  // kind
      target_type,                      // target MachineType
      target_loc,                       // target location
      locations.Build(),                // location_sig
      0,                                // stack_parameter_count
      Operator::kNoThrow,               // properties
      kNoCalleeSaved,                   // callee-saved registers
      kNoCalleeSaved,                   // callee-saved fp
      CallDescriptor::kCanUseRoots,     // flags
      "Allocate");
}

// static
425 426 427 428 429 430
CallDescriptor* Linkage::GetBytecodeDispatchCallDescriptor(
    Isolate* isolate, Zone* zone, const CallInterfaceDescriptor& descriptor,
    int stack_parameter_count) {
  const int register_parameter_count = descriptor.GetRegisterParameterCount();
  const int parameter_count = register_parameter_count + stack_parameter_count;

431
  LocationSignature::Builder locations(zone, 0, parameter_count);
432 433 434 435 436 437

  // 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);
438
      MachineType type = descriptor.GetParameterType(i);
439
      locations.AddParam(regloc(reg, type));
440 441 442
    } else {
      // The rest of the parameters go on the stack.
      int stack_slot = i - register_parameter_count - stack_parameter_count;
443 444
      locations.AddParam(LinkageLocation::ForCallerFrameSlot(
          stack_slot, MachineType::AnyTagged()));
445 446 447 448 449
    }
  }

  // The target for interpreter dispatches is a code entry address.
  MachineType target_type = MachineType::Pointer();
450
  LinkageLocation target_loc = LinkageLocation::ForAnyRegister(target_type);
451 452 453 454 455 456 457 458 459 460 461 462 463
  return new (zone) CallDescriptor(            // --
      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
      CallDescriptor::kCanUseRoots |           // flags
          CallDescriptor::kSupportsTailCalls,  // flags
      descriptor.DebugName(isolate));
}
464

465 466 467 468 469 470 471
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.
472
    // Parameter (arity + 2) is special for the context of the function frame.
473 474
    // >> context_index = target + receiver + params + new_target + #args
    int context_index = 1 + 1 + parameter_count + 1 + 1;
475 476 477
    return incoming_->GetInputLocation(context_index);
  } else if (index >= first_stack_slot) {
    // Local variable stored in this (callee) stack.
478 479
    int spill_index =
        index - first_stack_slot + StandardFrameConstants::kFixedSlotCount;
480 481
    return LinkageLocation::ForCalleeFrameSlot(spill_index,
                                               MachineType::AnyTagged());
482 483 484 485 486 487
  } 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);
  }
}
488 489 490


bool Linkage::ParameterHasSecondaryLocation(int index) const {
491
  if (!incoming_->IsJSFunctionCall()) return false;
492
  LinkageLocation loc = GetParameterLocation(index);
493 494
  return (loc == regloc(kJSFunctionRegister, MachineType::AnyTagged()) ||
          loc == regloc(kContextRegister, MachineType::AnyTagged()));
495 496 497 498 499 500
}

LinkageLocation Linkage::GetParameterSecondaryLocation(int index) const {
  DCHECK(ParameterHasSecondaryLocation(index));
  LinkageLocation loc = GetParameterLocation(index);

501 502 503
  if (loc == regloc(kJSFunctionRegister, MachineType::AnyTagged())) {
    return LinkageLocation::ForCalleeFrameSlot(Frame::kJSFunctionSlot,
                                               MachineType::AnyTagged());
504
  } else {
505 506 507
    DCHECK(loc == regloc(kContextRegister, MachineType::AnyTagged()));
    return LinkageLocation::ForCalleeFrameSlot(Frame::kContextSlot,
                                               MachineType::AnyTagged());
508 509 510 511
  }
}


512 513 514
}  // namespace compiler
}  // namespace internal
}  // namespace v8