blob: 319d2a07bb7b66a87578d372cf5e8048b15b747d [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/interpreter/bytecode-array-iterator.h"
6
7#include "src/objects-inl.h"
8
9namespace v8 {
10namespace internal {
11namespace interpreter {
12
13BytecodeArrayIterator::BytecodeArrayIterator(
14 Handle<BytecodeArray> bytecode_array)
Ben Murdochda12d292016-06-02 14:46:10 +010015 : bytecode_array_(bytecode_array),
16 bytecode_offset_(0),
17 operand_scale_(OperandScale::kSingle),
18 prefix_offset_(0) {
19 UpdateOperandScale();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020}
21
Ben Murdochda12d292016-06-02 14:46:10 +010022void BytecodeArrayIterator::Advance() {
23 bytecode_offset_ += current_bytecode_size();
24 UpdateOperandScale();
25}
26
27void BytecodeArrayIterator::UpdateOperandScale() {
28 if (!done()) {
29 uint8_t current_byte = bytecode_array()->get(bytecode_offset_);
30 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
31 if (Bytecodes::IsPrefixScalingBytecode(current_bytecode)) {
32 operand_scale_ =
33 Bytecodes::PrefixBytecodeToOperandScale(current_bytecode);
34 prefix_offset_ = 1;
35 } else {
36 operand_scale_ = OperandScale::kSingle;
37 prefix_offset_ = 0;
38 }
39 }
40}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041
42bool BytecodeArrayIterator::done() const {
43 return bytecode_offset_ >= bytecode_array()->length();
44}
45
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000046Bytecode BytecodeArrayIterator::current_bytecode() const {
47 DCHECK(!done());
Ben Murdochda12d292016-06-02 14:46:10 +010048 uint8_t current_byte =
49 bytecode_array()->get(bytecode_offset_ + current_prefix_offset());
50 Bytecode current_bytecode = Bytecodes::FromByte(current_byte);
51 DCHECK(!Bytecodes::IsPrefixScalingBytecode(current_bytecode));
52 return current_bytecode;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053}
54
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055int BytecodeArrayIterator::current_bytecode_size() const {
Ben Murdochda12d292016-06-02 14:46:10 +010056 return current_prefix_offset() +
57 Bytecodes::Size(current_bytecode(), current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058}
59
Ben Murdochda12d292016-06-02 14:46:10 +010060uint32_t BytecodeArrayIterator::GetUnsignedOperand(
61 int operand_index, OperandType operand_type) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062 DCHECK_GE(operand_index, 0);
63 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
64 DCHECK_EQ(operand_type,
65 Bytecodes::GetOperandType(current_bytecode(), operand_index));
Ben Murdochda12d292016-06-02 14:46:10 +010066 DCHECK(Bytecodes::IsUnsignedOperandType(operand_type));
67 const uint8_t* operand_start =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
Ben Murdochda12d292016-06-02 14:46:10 +010069 current_prefix_offset() +
70 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
71 current_operand_scale());
72 return Bytecodes::DecodeUnsignedOperand(operand_start, operand_type,
73 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074}
75
Ben Murdochda12d292016-06-02 14:46:10 +010076int32_t BytecodeArrayIterator::GetSignedOperand(
77 int operand_index, OperandType operand_type) const {
78 DCHECK_GE(operand_index, 0);
79 DCHECK_LT(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
80 DCHECK_EQ(operand_type,
81 Bytecodes::GetOperandType(current_bytecode(), operand_index));
82 DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type));
83 const uint8_t* operand_start =
84 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
85 current_prefix_offset() +
86 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
87 current_operand_scale());
88 return Bytecodes::DecodeSignedOperand(operand_start, operand_type,
89 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090}
91
Ben Murdochda12d292016-06-02 14:46:10 +010092uint32_t BytecodeArrayIterator::GetFlagOperand(int operand_index) const {
93 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
94 OperandType::kFlag8);
95 return GetUnsignedOperand(operand_index, OperandType::kFlag8);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096}
97
Ben Murdochda12d292016-06-02 14:46:10 +010098int32_t BytecodeArrayIterator::GetImmediateOperand(int operand_index) const {
99 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
100 OperandType::kImm);
101 return GetSignedOperand(operand_index, OperandType::kImm);
102}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103
Ben Murdochda12d292016-06-02 14:46:10 +0100104uint32_t BytecodeArrayIterator::GetRegisterCountOperand(
105 int operand_index) const {
106 DCHECK_EQ(Bytecodes::GetOperandType(current_bytecode(), operand_index),
107 OperandType::kRegCount);
108 return GetUnsignedOperand(operand_index, OperandType::kRegCount);
109}
110
111uint32_t BytecodeArrayIterator::GetIndexOperand(int operand_index) const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 OperandType operand_type =
113 Bytecodes::GetOperandType(current_bytecode(), operand_index);
Ben Murdochda12d292016-06-02 14:46:10 +0100114 DCHECK_EQ(operand_type, OperandType::kIdx);
115 return GetUnsignedOperand(operand_index, operand_type);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116}
117
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const {
119 OperandType operand_type =
120 Bytecodes::GetOperandType(current_bytecode(), operand_index);
Ben Murdochda12d292016-06-02 14:46:10 +0100121 const uint8_t* operand_start =
122 bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ +
123 current_prefix_offset() +
124 Bytecodes::GetOperandOffset(current_bytecode(), operand_index,
125 current_operand_scale());
126 return Bytecodes::DecodeRegisterOperand(operand_start, operand_type,
127 current_operand_scale());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000128}
129
Ben Murdoch097c5b22016-05-18 11:27:45 +0100130int BytecodeArrayIterator::GetRegisterOperandRange(int operand_index) const {
Ben Murdochc5610432016-08-08 18:44:38 +0100131 DCHECK_LE(operand_index, Bytecodes::NumberOfOperands(current_bytecode()));
132 const OperandType* operand_types =
133 Bytecodes::GetOperandTypes(current_bytecode());
134 DCHECK(Bytecodes::IsRegisterOperandType(operand_types[operand_index]));
135 if (operand_types[operand_index + 1] == OperandType::kRegCount) {
136 return GetRegisterCountOperand(operand_index + 1);
137 } else {
138 OperandType operand_type = operand_types[operand_index];
139 return Bytecodes::GetNumberOfRegistersRepresentedBy(operand_type);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100140 }
141}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142
Ben Murdochda12d292016-06-02 14:46:10 +0100143uint32_t BytecodeArrayIterator::GetRuntimeIdOperand(int operand_index) const {
144 OperandType operand_type =
145 Bytecodes::GetOperandType(current_bytecode(), operand_index);
146 DCHECK(operand_type == OperandType::kRuntimeId);
147 return GetUnsignedOperand(operand_index, operand_type);
148}
149
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand(
151 int operand_index) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100152 return FixedArray::get(bytecode_array()->constant_pool(),
153 GetIndexOperand(operand_index),
154 bytecode_array()->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000155}
156
157
158int BytecodeArrayIterator::GetJumpTargetOffset() const {
159 Bytecode bytecode = current_bytecode();
160 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
161 int relative_offset = GetImmediateOperand(0);
Ben Murdochda12d292016-06-02 14:46:10 +0100162 return current_offset() + relative_offset + current_prefix_offset();
163 } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000164 Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
Ben Murdochda12d292016-06-02 14:46:10 +0100165 return current_offset() + smi->value() + current_prefix_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166 } else {
167 UNREACHABLE();
168 return kMinInt;
169 }
170}
171
172} // namespace interpreter
173} // namespace internal
174} // namespace v8