blob: a17efcb6ca4b5a36c240e32474fe0ef92834e96a [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 {
131 interpreter::OperandType operand_type =
132 Bytecodes::GetOperandType(current_bytecode(), operand_index);
133 DCHECK(Bytecodes::IsRegisterOperandType(operand_type));
134 switch (operand_type) {
Ben Murdochda12d292016-06-02 14:46:10 +0100135 case OperandType::kRegPair:
136 case OperandType::kRegOutPair:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100137 return 2;
Ben Murdochda12d292016-06-02 14:46:10 +0100138 case OperandType::kRegOutTriple:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100139 return 3;
140 default: {
141 if (operand_index + 1 !=
142 Bytecodes::NumberOfOperands(current_bytecode())) {
143 OperandType next_operand_type =
144 Bytecodes::GetOperandType(current_bytecode(), operand_index + 1);
Ben Murdochda12d292016-06-02 14:46:10 +0100145 if (OperandType::kRegCount == next_operand_type) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100146 return GetRegisterCountOperand(operand_index + 1);
147 }
148 }
149 return 1;
150 }
151 }
152}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000153
Ben Murdochda12d292016-06-02 14:46:10 +0100154uint32_t BytecodeArrayIterator::GetRuntimeIdOperand(int operand_index) const {
155 OperandType operand_type =
156 Bytecodes::GetOperandType(current_bytecode(), operand_index);
157 DCHECK(operand_type == OperandType::kRuntimeId);
158 return GetUnsignedOperand(operand_index, operand_type);
159}
160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand(
162 int operand_index) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100163 return FixedArray::get(bytecode_array()->constant_pool(),
164 GetIndexOperand(operand_index),
165 bytecode_array()->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000166}
167
168
169int BytecodeArrayIterator::GetJumpTargetOffset() const {
170 Bytecode bytecode = current_bytecode();
171 if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) {
172 int relative_offset = GetImmediateOperand(0);
Ben Murdochda12d292016-06-02 14:46:10 +0100173 return current_offset() + relative_offset + current_prefix_offset();
174 } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 Smi* smi = Smi::cast(*GetConstantForIndexOperand(0));
Ben Murdochda12d292016-06-02 14:46:10 +0100176 return current_offset() + smi->value() + current_prefix_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000177 } else {
178 UNREACHABLE();
179 return kMinInt;
180 }
181}
182
183} // namespace interpreter
184} // namespace internal
185} // namespace v8