bytecode-node.h 11.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2017 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_INTERPRETER_BYTECODE_NODE_H_
#define V8_INTERPRETER_BYTECODE_NODE_H_

#include <algorithm>

10
#include "src/common/globals.h"
11 12 13 14 15 16 17 18 19 20
#include "src/interpreter/bytecode-source-info.h"
#include "src/interpreter/bytecodes.h"

namespace v8 {
namespace internal {
namespace interpreter {

// A container for a generated bytecode, it's operands, and source information.
class V8_EXPORT_PRIVATE BytecodeNode final {
 public:
21 22
  V8_INLINE BytecodeNode(Bytecode bytecode,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
23 24 25 26 27 28 29
      : bytecode_(bytecode),
        operand_count_(0),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
  }

30 31
  V8_INLINE BytecodeNode(Bytecode bytecode, uint32_t operand0,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
32 33 34 35 36 37 38 39
      : bytecode_(bytecode),
        operand_count_(1),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
    SetOperand(0, operand0);
  }

40 41 42
  V8_INLINE BytecodeNode(Bytecode bytecode, uint32_t operand0,
                         uint32_t operand1,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
43 44 45 46 47 48 49 50 51
      : bytecode_(bytecode),
        operand_count_(2),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
    SetOperand(0, operand0);
    SetOperand(1, operand1);
  }

52 53 54
  V8_INLINE BytecodeNode(Bytecode bytecode, uint32_t operand0,
                         uint32_t operand1, uint32_t operand2,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
55 56 57 58 59 60 61 62 63 64
      : bytecode_(bytecode),
        operand_count_(3),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
    SetOperand(0, operand0);
    SetOperand(1, operand1);
    SetOperand(2, operand2);
  }

65 66 67 68
  V8_INLINE BytecodeNode(Bytecode bytecode, uint32_t operand0,
                         uint32_t operand1, uint32_t operand2,
                         uint32_t operand3,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
69 70 71 72 73 74 75 76 77 78 79
      : bytecode_(bytecode),
        operand_count_(4),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
    SetOperand(0, operand0);
    SetOperand(1, operand1);
    SetOperand(2, operand2);
    SetOperand(3, operand3);
  }

80 81 82 83
  V8_INLINE BytecodeNode(Bytecode bytecode, uint32_t operand0,
                         uint32_t operand1, uint32_t operand2,
                         uint32_t operand3, uint32_t operand4,
                         BytecodeSourceInfo source_info = BytecodeSourceInfo())
84 85 86 87 88 89 90 91 92 93 94 95 96 97
      : bytecode_(bytecode),
        operand_count_(5),
        operand_scale_(OperandScale::kSingle),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count());
    SetOperand(0, operand0);
    SetOperand(1, operand1);
    SetOperand(2, operand2);
    SetOperand(3, operand3);
    SetOperand(4, operand4);
  }

#define DEFINE_BYTECODE_NODE_CREATOR(Name, ...)                              \
  template <typename... Operands>                                            \
98 99
  V8_INLINE static BytecodeNode Name(BytecodeSourceInfo source_info,         \
                                     Operands... operands) {                 \
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    return Create<Bytecode::k##Name, __VA_ARGS__>(source_info, operands...); \
  }
  BYTECODE_LIST(DEFINE_BYTECODE_NODE_CREATOR)
#undef DEFINE_BYTECODE_NODE_CREATOR

  // Print to stream |os|.
  void Print(std::ostream& os) const;

  Bytecode bytecode() const { return bytecode_; }

  uint32_t operand(int i) const {
    DCHECK_LT(i, operand_count());
    return operands_[i];
  }
  const uint32_t* operands() const { return operands_; }

  void update_operand0(uint32_t operand0) { SetOperand(0, operand0); }

  int operand_count() const { return operand_count_; }
  OperandScale operand_scale() const { return operand_scale_; }

  const BytecodeSourceInfo& source_info() const { return source_info_; }
  void set_source_info(BytecodeSourceInfo source_info) {
    source_info_ = source_info;
  }

  bool operator==(const BytecodeNode& other) const;
  bool operator!=(const BytecodeNode& other) const { return !(*this == other); }

 private:
130
  template <Bytecode bytecode, ImplicitRegisterUse implicit_register_use,
131 132 133
            OperandType... operand_types>
  friend class BytecodeNodeBuilder;

134 135 136 137 138
  V8_INLINE BytecodeNode(Bytecode bytecode, int operand_count,
                         OperandScale operand_scale,
                         BytecodeSourceInfo source_info, uint32_t operand0 = 0,
                         uint32_t operand1 = 0, uint32_t operand2 = 0,
                         uint32_t operand3 = 0, uint32_t operand4 = 0)
139 140 141 142 143 144 145 146 147 148 149 150
      : bytecode_(bytecode),
        operand_count_(operand_count),
        operand_scale_(operand_scale),
        source_info_(source_info) {
    DCHECK_EQ(Bytecodes::NumberOfOperands(bytecode), operand_count);
    operands_[0] = operand0;
    operands_[1] = operand1;
    operands_[2] = operand2;
    operands_[3] = operand3;
    operands_[4] = operand4;
  }

151
  template <Bytecode bytecode, ImplicitRegisterUse accum_use>
152
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info) {
153 154 155
    return BytecodeNode(bytecode, 0, OperandScale::kSingle, source_info);
  }

156
  template <Bytecode bytecode, ImplicitRegisterUse accum_use,
157
            OperandType operand0_type>
158 159
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info,
                                       uint32_t operand0) {
160 161 162 163 164 165
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 0), operand0_type);
    OperandScale scale = OperandScale::kSingle;
    scale = std::max(scale, ScaleForOperand<operand0_type>(operand0));
    return BytecodeNode(bytecode, 1, scale, source_info, operand0);
  }

166
  template <Bytecode bytecode, ImplicitRegisterUse accum_use,
167
            OperandType operand0_type, OperandType operand1_type>
168 169
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info,
                                       uint32_t operand0, uint32_t operand1) {
170 171 172 173 174 175 176 177
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 0), operand0_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 1), operand1_type);
    OperandScale scale = OperandScale::kSingle;
    scale = std::max(scale, ScaleForOperand<operand0_type>(operand0));
    scale = std::max(scale, ScaleForOperand<operand1_type>(operand1));
    return BytecodeNode(bytecode, 2, scale, source_info, operand0, operand1);
  }

178
  template <Bytecode bytecode, ImplicitRegisterUse accum_use,
179 180
            OperandType operand0_type, OperandType operand1_type,
            OperandType operand2_type>
181 182 183
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info,
                                       uint32_t operand0, uint32_t operand1,
                                       uint32_t operand2) {
184 185 186 187 188 189 190 191 192 193 194
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 0), operand0_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 1), operand1_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 2), operand2_type);
    OperandScale scale = OperandScale::kSingle;
    scale = std::max(scale, ScaleForOperand<operand0_type>(operand0));
    scale = std::max(scale, ScaleForOperand<operand1_type>(operand1));
    scale = std::max(scale, ScaleForOperand<operand2_type>(operand2));
    return BytecodeNode(bytecode, 3, scale, source_info, operand0, operand1,
                        operand2);
  }

195
  template <Bytecode bytecode, ImplicitRegisterUse accum_use,
196 197
            OperandType operand0_type, OperandType operand1_type,
            OperandType operand2_type, OperandType operand3_type>
198 199 200
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info,
                                       uint32_t operand0, uint32_t operand1,
                                       uint32_t operand2, uint32_t operand3) {
201 202 203 204 205 206 207 208 209 210 211 212 213
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 0), operand0_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 1), operand1_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 2), operand2_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 3), operand3_type);
    OperandScale scale = OperandScale::kSingle;
    scale = std::max(scale, ScaleForOperand<operand0_type>(operand0));
    scale = std::max(scale, ScaleForOperand<operand1_type>(operand1));
    scale = std::max(scale, ScaleForOperand<operand2_type>(operand2));
    scale = std::max(scale, ScaleForOperand<operand3_type>(operand3));
    return BytecodeNode(bytecode, 4, scale, source_info, operand0, operand1,
                        operand2, operand3);
  }

214
  template <Bytecode bytecode, ImplicitRegisterUse accum_use,
215 216 217
            OperandType operand0_type, OperandType operand1_type,
            OperandType operand2_type, OperandType operand3_type,
            OperandType operand4_type>
218 219 220 221
  V8_INLINE static BytecodeNode Create(BytecodeSourceInfo source_info,
                                       uint32_t operand0, uint32_t operand1,
                                       uint32_t operand2, uint32_t operand3,
                                       uint32_t operand4) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 0), operand0_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 1), operand1_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 2), operand2_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 3), operand3_type);
    DCHECK_EQ(Bytecodes::GetOperandType(bytecode, 4), operand4_type);
    OperandScale scale = OperandScale::kSingle;
    scale = std::max(scale, ScaleForOperand<operand0_type>(operand0));
    scale = std::max(scale, ScaleForOperand<operand1_type>(operand1));
    scale = std::max(scale, ScaleForOperand<operand2_type>(operand2));
    scale = std::max(scale, ScaleForOperand<operand3_type>(operand3));
    scale = std::max(scale, ScaleForOperand<operand4_type>(operand4));
    return BytecodeNode(bytecode, 5, scale, source_info, operand0, operand1,
                        operand2, operand3, operand4);
  }

  template <OperandType operand_type>
238
  V8_INLINE static OperandScale ScaleForOperand(uint32_t operand) {
239 240 241 242 243 244 245 246 247
    if (BytecodeOperands::IsScalableUnsignedByte(operand_type)) {
      return Bytecodes::ScaleForUnsignedOperand(operand);
    } else if (BytecodeOperands::IsScalableSignedByte(operand_type)) {
      return Bytecodes::ScaleForSignedOperand(operand);
    } else {
      return OperandScale::kSingle;
    }
  }

248
  V8_INLINE void UpdateScaleForOperand(int operand_index, uint32_t operand) {
249 250 251 252 253 254 255 256 257 258
    if (Bytecodes::OperandIsScalableSignedByte(bytecode(), operand_index)) {
      operand_scale_ =
          std::max(operand_scale_, Bytecodes::ScaleForSignedOperand(operand));
    } else if (Bytecodes::OperandIsScalableUnsignedByte(bytecode(),
                                                        operand_index)) {
      operand_scale_ =
          std::max(operand_scale_, Bytecodes::ScaleForUnsignedOperand(operand));
    }
  }

259
  V8_INLINE void SetOperand(int operand_index, uint32_t operand) {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    operands_[operand_index] = operand;
    UpdateScaleForOperand(operand_index, operand);
  }

  Bytecode bytecode_;
  uint32_t operands_[Bytecodes::kMaxOperands];
  int operand_count_;
  OperandScale operand_scale_;
  BytecodeSourceInfo source_info_;
};

V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
                                           const BytecodeNode& node);

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_BYTECODE_NODE_H_