frame-states.cc 7.34 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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.

#include "src/compiler/frame-states.h"
6 7

#include "src/base/functional.h"
8 9 10 11
#include "src/callable.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node.h"
12
#include "src/handles-inl.h"
13
#include "src/objects-inl.h"
14 15 16 17 18 19

namespace v8 {
namespace internal {
namespace compiler {

size_t hash_value(OutputFrameStateCombine const& sc) {
20
  return base::hash_value(sc.parameter_);
21 22 23 24
}


std::ostream& operator<<(std::ostream& os, OutputFrameStateCombine const& sc) {
25 26 27
  if (sc.parameter_ == OutputFrameStateCombine::kInvalidIndex)
    return os << "Ignore";
  return os << "PokeAt(" << sc.parameter_ << ")";
28 29 30
}


31
bool operator==(FrameStateInfo const& lhs, FrameStateInfo const& rhs) {
32
  return lhs.type() == rhs.type() && lhs.bailout_id() == rhs.bailout_id() &&
33 34
         lhs.state_combine() == rhs.state_combine() &&
         lhs.function_info() == rhs.function_info();
35 36 37
}


38
bool operator!=(FrameStateInfo const& lhs, FrameStateInfo const& rhs) {
39 40 41 42
  return !(lhs == rhs);
}


43 44
size_t hash_value(FrameStateInfo const& info) {
  return base::hash_combine(static_cast<int>(info.type()), info.bailout_id(),
45 46 47 48
                            info.state_combine());
}


49 50
std::ostream& operator<<(std::ostream& os, FrameStateType type) {
  switch (type) {
51 52 53
    case FrameStateType::kInterpretedFunction:
      os << "INTERPRETED_FRAME";
      break;
54 55 56
    case FrameStateType::kArgumentsAdaptor:
      os << "ARGUMENTS_ADAPTOR";
      break;
57 58 59
    case FrameStateType::kConstructStub:
      os << "CONSTRUCT_STUB";
      break;
60 61 62 63 64 65
    case FrameStateType::kBuiltinContinuation:
      os << "BUILTIN_CONTINUATION_FRAME";
      break;
    case FrameStateType::kJavaScriptBuiltinContinuation:
      os << "JAVA_SCRIPT_BUILTIN_CONTINUATION_FRAME";
      break;
66 67 68 69 70 71
    case FrameStateType::kGetterStub:
      os << "GETTER_STUB";
      break;
    case FrameStateType::kSetterStub:
      os << "SETTER_STUB";
      break;
72 73 74 75 76 77
  }
  return os;
}


std::ostream& operator<<(std::ostream& os, FrameStateInfo const& info) {
78 79 80 81 82 83 84
  os << info.type() << ", " << info.bailout_id() << ", "
     << info.state_combine();
  Handle<SharedFunctionInfo> shared_info;
  if (info.shared_info().ToHandle(&shared_info)) {
    os << ", " << Brief(*shared_info);
  }
  return os;
85
}
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
namespace {
Node* CreateBuiltinContinuationFrameStateCommon(
    JSGraph* js_graph, Builtins::Name name, Node* context, Node** parameters,
    int parameter_count, Node* outer_frame_state, Handle<JSFunction> function) {
  Isolate* isolate = js_graph->isolate();
  Graph* graph = js_graph->graph();
  CommonOperatorBuilder* common = js_graph->common();

  BailoutId bailout_id = Builtins::GetContinuationBailoutId(name);
  Callable callable = Builtins::CallableFor(isolate, name);

  const Operator* op_param =
      common->StateValues(parameter_count, SparseInputMask::Dense());
  Node* params_node = graph->NewNode(op_param, parameter_count, parameters);

  FrameStateType frame_type =
      function.is_null() ? FrameStateType::kBuiltinContinuation
                         : FrameStateType::kJavaScriptBuiltinContinuation;
  const FrameStateFunctionInfo* state_info =
106 107 108 109
      common->CreateFrameStateFunctionInfo(
          frame_type, parameter_count, 0,
          function.is_null() ? Handle<SharedFunctionInfo>()
                             : Handle<SharedFunctionInfo>(function->shared()));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  const Operator* op = common->FrameState(
      bailout_id, OutputFrameStateCombine::Ignore(), state_info);

  Node* function_node = function.is_null() ? js_graph->UndefinedConstant()
                                           : js_graph->HeapConstant(function);

  Node* frame_state = graph->NewNode(
      op, params_node, js_graph->EmptyStateValues(),
      js_graph->EmptyStateValues(), context, function_node, outer_frame_state);

  return frame_state;
}
}  // namespace

Node* CreateStubBuiltinContinuationFrameState(JSGraph* js_graph,
                                              Builtins::Name name,
                                              Node* context, Node** parameters,
                                              int parameter_count,
                                              Node* outer_frame_state,
                                              ContinuationFrameStateMode mode) {
  Isolate* isolate = js_graph->isolate();
  Callable callable = Builtins::CallableFor(isolate, name);
  CallInterfaceDescriptor descriptor = callable.descriptor();

  std::vector<Node*> actual_parameters;
135 136 137 138 139 140
  // Stack parameters first. If the deoptimization is LAZY, the final parameter
  // is added by the deoptimizer and isn't explicitly passed in the frame state.
  int stack_parameter_count =
      descriptor.GetRegisterParameterCount() -
      (mode == ContinuationFrameStateMode::LAZY ? 1 : 0);
  for (int i = 0; i < stack_parameter_count; ++i) {
141 142 143 144 145 146 147 148 149 150
    actual_parameters.push_back(
        parameters[descriptor.GetRegisterParameterCount() + i]);
  }
  // Register parameters follow, context will be added by instruction selector
  // during FrameState translation.
  for (int i = 0; i < descriptor.GetRegisterParameterCount(); ++i) {
    actual_parameters.push_back(parameters[i]);
  }

  return CreateBuiltinContinuationFrameStateCommon(
151
      js_graph, name, context, actual_parameters.data(),
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
      static_cast<int>(actual_parameters.size()), outer_frame_state,
      Handle<JSFunction>());
}

Node* CreateJavaScriptBuiltinContinuationFrameState(
    JSGraph* js_graph, Handle<JSFunction> function, Builtins::Name name,
    Node* target, Node* context, Node** stack_parameters,
    int stack_parameter_count, Node* outer_frame_state,
    ContinuationFrameStateMode mode) {
  Isolate* isolate = js_graph->isolate();
  Callable callable = Builtins::CallableFor(isolate, name);

  // Lazy deopt points where the frame state is assocated with a call get an
  // additional parameter for the return result from the call that's added by
  // the deoptimizer and not explicitly specified in the frame state. Check that
  // there is not a mismatch between the number of frame state parameters and
  // the stack parameters required by the builtin taking this into account.
  DCHECK_EQ(
      Builtins::GetStackParameterCount(isolate, name) + 1,  // add receiver
      stack_parameter_count +
          (mode == ContinuationFrameStateMode::EAGER ? 0 : 1));

  Node* argc =
      js_graph->Constant(stack_parameter_count -
                         (mode == ContinuationFrameStateMode::EAGER ? 1 : 0));

  // Stack parameters first. They must be first because the receiver is expected
  // to be the second value in the translation when creating stack crawls
  // (e.g. Error.stack) of optimized JavaScript frames.
  std::vector<Node*> actual_parameters;
  for (int i = 0; i < stack_parameter_count; ++i) {
    actual_parameters.push_back(stack_parameters[i]);
  }

  // Register parameters follow stack paraemters. The context will be added by
  // instruction selector during FrameState translation.
  actual_parameters.push_back(target);
  actual_parameters.push_back(js_graph->UndefinedConstant());
  actual_parameters.push_back(argc);

  return CreateBuiltinContinuationFrameStateCommon(
      js_graph, name, context, &actual_parameters[0],
      static_cast<int>(actual_parameters.size()), outer_frame_state, function);
}

197 198 199
}  // namespace compiler
}  // namespace internal
}  // namespace v8