summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/bytecodes.cc
blob: 8232b657e78e0d1de05c86db3754b287779a1ee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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/interpreter/bytecodes.h"

#include "src/interpreter/bytecode-array-builder.h"

namespace v8 {
namespace internal {
namespace interpreter {

// Maximum number of operands a bytecode may have.
static const int kMaxOperands = 3;

// kBytecodeTable relies on kNone being the same as zero to detect length.
STATIC_ASSERT(static_cast<int>(OperandType::kNone) == 0);

static const OperandType kBytecodeTable[][kMaxOperands] = {
#define DECLARE_OPERAND(_, ...) \
  { __VA_ARGS__ }               \
  ,
    BYTECODE_LIST(DECLARE_OPERAND)
#undef DECLARE_OPERAND
};


// static
const char* Bytecodes::ToString(Bytecode bytecode) {
  switch (bytecode) {
#define CASE(Name, ...)   \
  case Bytecode::k##Name: \
    return #Name;
    BYTECODE_LIST(CASE)
#undef CASE
  }
  UNREACHABLE();
  return "";
}


// static
const char* Bytecodes::OperandTypeToString(OperandType operand_type) {
  switch (operand_type) {
#define CASE(Name)           \
  case OperandType::k##Name: \
    return #Name;
    OPERAND_TYPE_LIST(CASE)
#undef CASE
  }
  UNREACHABLE();
  return "";
}


// static
uint8_t Bytecodes::ToByte(Bytecode bytecode) {
  return static_cast<uint8_t>(bytecode);
}


// static
Bytecode Bytecodes::FromByte(uint8_t value) {
  Bytecode bytecode = static_cast<Bytecode>(value);
  DCHECK(bytecode <= Bytecode::kLast);
  return bytecode;
}


// static
int Bytecodes::NumberOfOperands(Bytecode bytecode) {
  DCHECK(bytecode <= Bytecode::kLast);
  int count;
  uint8_t row = ToByte(bytecode);
  for (count = 0; count < kMaxOperands; count++) {
    if (kBytecodeTable[row][count] == OperandType::kNone) {
      break;
    }
  }
  return count;
}


// static
OperandType Bytecodes::GetOperandType(Bytecode bytecode, int i) {
  DCHECK(bytecode <= Bytecode::kLast && i < NumberOfOperands(bytecode));
  return kBytecodeTable[ToByte(bytecode)][i];
}


// static
int Bytecodes::Size(Bytecode bytecode) {
  return 1 + NumberOfOperands(bytecode);
}


// static
int Bytecodes::MaximumNumberOfOperands() { return kMaxOperands; }


// static
int Bytecodes::MaximumSize() { return 1 + kMaxOperands; }


// static
std::ostream& Bytecodes::Decode(std::ostream& os,
                                const uint8_t* bytecode_start) {
  Vector<char> buf = Vector<char>::New(50);

  Bytecode bytecode = Bytecodes::FromByte(bytecode_start[0]);
  int bytecode_size = Bytecodes::Size(bytecode);

  for (int i = 0; i < bytecode_size; i++) {
    SNPrintF(buf, "%02x ", bytecode_start[i]);
    os << buf.start();
  }
  for (int i = bytecode_size; i < Bytecodes::MaximumSize(); i++) {
    os << "   ";
  }

  os << bytecode << " ";

  const uint8_t* operands_start = bytecode_start + 1;
  int operands_size = bytecode_size - 1;
  for (int i = 0; i < operands_size; i++) {
    OperandType op_type = GetOperandType(bytecode, i);
    uint8_t operand = operands_start[i];
    switch (op_type) {
      case interpreter::OperandType::kImm8:
        os << "#" << static_cast<int>(operand);
        break;
      case interpreter::OperandType::kReg:
        os << "r" << Register::FromOperand(operand).index();
        break;
      case interpreter::OperandType::kNone:
        UNREACHABLE();
        break;
    }
    if (i != operands_size - 1) {
      os << ", ";
    }
  }
  return os;
}


std::ostream& operator<<(std::ostream& os, const Bytecode& bytecode) {
  return os << Bytecodes::ToString(bytecode);
}


std::ostream& operator<<(std::ostream& os, const OperandType& operand_type) {
  return os << Bytecodes::OperandTypeToString(operand_type);
}

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