summaryrefslogtreecommitdiff
path: root/deps/v8/src/interpreter/bytecode-array-iterator.cc
blob: 0fea985efee2911cb141fdc4857e2d021d42a407 (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
159
160
161
162
163
164
165
166
// 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/bytecode-array-iterator.h"

#include "src/objects-inl.h"

namespace v8 {
namespace internal {
namespace interpreter {

BytecodeArrayIterator::BytecodeArrayIterator(
    Handle<BytecodeArray> bytecode_array)
    : bytecode_array_(bytecode_array), bytecode_offset_(0) {}


void BytecodeArrayIterator::Advance() {
  bytecode_offset_ += Bytecodes::Size(current_bytecode());
}


bool BytecodeArrayIterator::done() const {
  return bytecode_offset_ >= bytecode_array()->length();
}


Bytecode BytecodeArrayIterator::current_bytecode() const {
  DCHECK(!done());
  uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
  return interpreter::Bytecodes::FromByte(current_byte);
}


int BytecodeArrayIterator::current_bytecode_size() const {
  return Bytecodes::Size(current_bytecode());
}


uint32_t BytecodeArrayIterator::GetRawOperand(int operand_index,
                                              OperandType operand_type) const {
  DCHECK_GE(operand_index, 0);
  DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
  DCHECK_EQ(operand_type,
            Bytecodes::GetOperandType(current_bytecode(), operand_index));
  uint8_t* operand_start =
      bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
      Bytecodes::GetOperandOffset(current_bytecode(), operand_index);
  switch (Bytecodes::SizeOfOperand(operand_type)) {
    case OperandSize::kByte:
      return static_cast<uint32_t>(*operand_start);
    case OperandSize::kShort:
      return ReadUnalignedUInt16(operand_start);
    case OperandSize::kNone:
      UNREACHABLE();
  }
  return 0;
}


int8_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const {
  uint32_t operand = GetRawOperand(operand_index, OperandType::kImm8);
  return static_cast<int8_t>(operand);
}

int BytecodeArrayIterator::GetRegisterCountOperand(int operand_index) const {
  OperandSize size =
      Bytecodes::GetOperandSize(current_bytecode(), operand_index);
  OperandType type = (size == OperandSize::kByte) ? OperandType::kRegCount8
                                                  : OperandType::kRegCount16;
  uint32_t operand = GetRawOperand(operand_index, type);
  return static_cast<int>(operand);
}


int BytecodeArrayIterator::GetIndexOperand(int operand_index) const {
  OperandType operand_type =
      Bytecodes::GetOperandType(current_bytecode(), operand_index);
  DCHECK(operand_type == OperandType::kIdx8 ||
         operand_type == OperandType::kIdx16);
  uint32_t operand = GetRawOperand(operand_index, operand_type);
  return static_cast<int>(operand);
}


Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const {
  OperandType operand_type =
      Bytecodes::GetOperandType(current_bytecode(), operand_index);
  DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
  uint32_t operand = GetRawOperand(operand_index, operand_type);
  Register reg;
  switch (Bytecodes::GetOperandSize(current_bytecode(), operand_index)) {
    case OperandSize::kByte:
      reg = Register::FromOperand(static_cast<uint8_t>(operand));
      break;
    case OperandSize::kShort:
      reg = Register::FromWideOperand(static_cast<uint16_t>(operand));
      break;
    case OperandSize::kNone:
      UNREACHABLE();
      reg = Register::invalid_value();
      break;
  }
  DCHECK_GE(reg.index(),
            Register::FromParameterIndex(0, bytecode_array()->parameter_count())
                .index());
  DCHECK(reg.index() < bytecode_array()->register_count() ||
         (reg.index() == 0 &&
          Bytecodes::IsMaybeRegisterOperandType(
              Bytecodes::GetOperandType(current_bytecode(), operand_index))));
  return reg;
}

int BytecodeArrayIterator::GetRegisterOperandRange(int operand_index) const {
  interpreter::OperandType operand_type =
      Bytecodes::GetOperandType(current_bytecode(), operand_index);
  DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
  switch (operand_type) {
    case OperandType::kRegPair8:
    case OperandType::kRegPair16:
    case OperandType::kRegOutPair8:
    case OperandType::kRegOutPair16:
      return 2;
    case OperandType::kRegOutTriple8:
    case OperandType::kRegOutTriple16:
      return 3;
    default: {
      if (operand_index + 1 !=
          Bytecodes::NumberOfOperands(current_bytecode())) {
        OperandType next_operand_type =
            Bytecodes::GetOperandType(current_bytecode(), operand_index + 1);
        if (Bytecodes::IsRegisterCountOperandType(next_operand_type)) {
          return GetRegisterCountOperand(operand_index + 1);
        }
      }
      return 1;
    }
  }
}

Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand(
    int operand_index) const {
  return FixedArray::get(bytecode_array()->constant_pool(),
                         GetIndexOperand(operand_index),
                         bytecode_array()->GetIsolate());
}


int BytecodeArrayIterator::GetJumpTargetOffset() const {
  Bytecode bytecode = current_bytecode();
  if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
    int relative_offset = GetImmediateOperand(0);
    return current_offset() + relative_offset;
  } else if (interpreter::Bytecodes::IsJumpConstant(bytecode) ||
             interpreter::Bytecodes::IsJumpConstantWide(bytecode)) {
    Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
    return current_offset() + smi->value();
  } else {
    UNREACHABLE();
    return kMinInt;
  }
}

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