Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // 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 | |
| 9 | namespace v8 { |
| 10 | namespace internal { |
| 11 | namespace interpreter { |
| 12 | |
| 13 | BytecodeArrayIterator::BytecodeArrayIterator( |
| 14 | Handle<BytecodeArray> bytecode_array) |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 15 | : bytecode_array_(bytecode_array), |
| 16 | bytecode_offset_(0), |
| 17 | operand_scale_(OperandScale::kSingle), |
| 18 | prefix_offset_(0) { |
| 19 | UpdateOperandScale(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 22 | void BytecodeArrayIterator::Advance() { |
| 23 | bytecode_offset_ += current_bytecode_size(); |
| 24 | UpdateOperandScale(); |
| 25 | } |
| 26 | |
| 27 | void 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | |
| 42 | bool BytecodeArrayIterator::done() const { |
| 43 | return bytecode_offset_ >= bytecode_array()->length(); |
| 44 | } |
| 45 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 46 | Bytecode BytecodeArrayIterator::current_bytecode() const { |
| 47 | DCHECK(!done()); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 48 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 55 | int BytecodeArrayIterator::current_bytecode_size() const { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 56 | return current_prefix_offset() + |
| 57 | Bytecodes::Size(current_bytecode(), current_operand_scale()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 60 | uint32_t BytecodeArrayIterator::GetUnsignedOperand( |
| 61 | int operand_index, OperandType operand_type) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 62 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 66 | DCHECK(Bytecodes::IsUnsignedOperandType(operand_type)); |
| 67 | const uint8_t* operand_start = |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | bytecode_array()->GetFirstBytecodeAddress() + bytecode_offset_ + |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 69 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 76 | int32_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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 92 | uint32_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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 98 | int32_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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 103 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 104 | uint32_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 | |
| 111 | uint32_t BytecodeArrayIterator::GetIndexOperand(int operand_index) const { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 112 | OperandType operand_type = |
| 113 | Bytecodes::GetOperandType(current_bytecode(), operand_index); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 114 | DCHECK_EQ(operand_type, OperandType::kIdx); |
| 115 | return GetUnsignedOperand(operand_index, operand_type); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 118 | Register BytecodeArrayIterator::GetRegisterOperand(int operand_index) const { |
| 119 | OperandType operand_type = |
| 120 | Bytecodes::GetOperandType(current_bytecode(), operand_index); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 121 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 130 | int 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 135 | case OperandType::kRegPair: |
| 136 | case OperandType::kRegOutPair: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 137 | return 2; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 138 | case OperandType::kRegOutTriple: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 139 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 145 | if (OperandType::kRegCount == next_operand_type) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 146 | return GetRegisterCountOperand(operand_index + 1); |
| 147 | } |
| 148 | } |
| 149 | return 1; |
| 150 | } |
| 151 | } |
| 152 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 153 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 154 | uint32_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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 161 | Handle<Object> BytecodeArrayIterator::GetConstantForIndexOperand( |
| 162 | int operand_index) const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 163 | return FixedArray::get(bytecode_array()->constant_pool(), |
| 164 | GetIndexOperand(operand_index), |
| 165 | bytecode_array()->GetIsolate()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
| 169 | int BytecodeArrayIterator::GetJumpTargetOffset() const { |
| 170 | Bytecode bytecode = current_bytecode(); |
| 171 | if (interpreter::Bytecodes::IsJumpImmediate(bytecode)) { |
| 172 | int relative_offset = GetImmediateOperand(0); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 173 | return current_offset() + relative_offset + current_prefix_offset(); |
| 174 | } else if (interpreter::Bytecodes::IsJumpConstant(bytecode)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 175 | Smi* smi = Smi::cast(*GetConstantForIndexOperand(0)); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 176 | return current_offset() + smi->value() + current_prefix_offset(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 177 | } else { |
| 178 | UNREACHABLE(); |
| 179 | return kMinInt; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | } // namespace interpreter |
| 184 | } // namespace internal |
| 185 | } // namespace v8 |