OLD | NEW |
(Empty) | |
| 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-decoder.h" |
| 6 |
| 7 #include <iomanip> |
| 8 |
| 9 #include "src/utils.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 namespace interpreter { |
| 14 |
| 15 // static |
| 16 Register BytecodeDecoder::DecodeRegisterOperand(const uint8_t* operand_start, |
| 17 OperandType operand_type, |
| 18 OperandScale operand_scale) { |
| 19 DCHECK(Bytecodes::IsRegisterOperandType(operand_type)); |
| 20 int32_t operand = |
| 21 DecodeSignedOperand(operand_start, operand_type, operand_scale); |
| 22 return Register::FromOperand(operand); |
| 23 } |
| 24 |
| 25 // static |
| 26 int32_t BytecodeDecoder::DecodeSignedOperand(const uint8_t* operand_start, |
| 27 OperandType operand_type, |
| 28 OperandScale operand_scale) { |
| 29 DCHECK(!Bytecodes::IsUnsignedOperandType(operand_type)); |
| 30 switch (Bytecodes::SizeOfOperand(operand_type, operand_scale)) { |
| 31 case OperandSize::kByte: |
| 32 return static_cast<int8_t>(*operand_start); |
| 33 case OperandSize::kShort: |
| 34 return static_cast<int16_t>(ReadUnalignedUInt16(operand_start)); |
| 35 case OperandSize::kQuad: |
| 36 return static_cast<int32_t>(ReadUnalignedUInt32(operand_start)); |
| 37 case OperandSize::kNone: |
| 38 UNREACHABLE(); |
| 39 } |
| 40 return 0; |
| 41 } |
| 42 |
| 43 // static |
| 44 uint32_t BytecodeDecoder::DecodeUnsignedOperand(const uint8_t* operand_start, |
| 45 OperandType operand_type, |
| 46 OperandScale operand_scale) { |
| 47 DCHECK(Bytecodes::IsUnsignedOperandType(operand_type)); |
| 48 switch (Bytecodes::SizeOfOperand(operand_type, operand_scale)) { |
| 49 case OperandSize::kByte: |
| 50 return *operand_start; |
| 51 case OperandSize::kShort: |
| 52 return ReadUnalignedUInt16(operand_start); |
| 53 case OperandSize::kQuad: |
| 54 return ReadUnalignedUInt32(operand_start); |
| 55 case OperandSize::kNone: |
| 56 UNREACHABLE(); |
| 57 } |
| 58 return 0; |
| 59 } |
| 60 |
| 61 // static |
| 62 std::ostream& BytecodeDecoder::Decode(std::ostream& os, |
| 63 const uint8_t* bytecode_start, |
| 64 int parameter_count) { |
| 65 Bytecode bytecode = Bytecodes::FromByte(bytecode_start[0]); |
| 66 int prefix_offset = 0; |
| 67 OperandScale operand_scale = OperandScale::kSingle; |
| 68 if (Bytecodes::IsPrefixScalingBytecode(bytecode)) { |
| 69 prefix_offset = 1; |
| 70 operand_scale = Bytecodes::PrefixBytecodeToOperandScale(bytecode); |
| 71 bytecode = Bytecodes::FromByte(bytecode_start[1]); |
| 72 } |
| 73 |
| 74 // Prepare to print bytecode and operands as hex digits. |
| 75 std::ios saved_format(nullptr); |
| 76 saved_format.copyfmt(saved_format); |
| 77 os.fill('0'); |
| 78 os.flags(std::ios::hex); |
| 79 |
| 80 int bytecode_size = Bytecodes::Size(bytecode, operand_scale); |
| 81 for (int i = 0; i < prefix_offset + bytecode_size; i++) { |
| 82 os << std::setw(2) << static_cast<uint32_t>(bytecode_start[i]) << ' '; |
| 83 } |
| 84 os.copyfmt(saved_format); |
| 85 |
| 86 const int kBytecodeColumnSize = 6; |
| 87 for (int i = prefix_offset + bytecode_size; i < kBytecodeColumnSize; i++) { |
| 88 os << " "; |
| 89 } |
| 90 |
| 91 os << Bytecodes::ToString(bytecode, operand_scale) << " "; |
| 92 |
| 93 // Operands for the debug break are from the original instruction. |
| 94 if (Bytecodes::IsDebugBreak(bytecode)) return os; |
| 95 |
| 96 int number_of_operands = Bytecodes::NumberOfOperands(bytecode); |
| 97 int range = 0; |
| 98 for (int i = 0; i < number_of_operands; i++) { |
| 99 OperandType op_type = Bytecodes::GetOperandType(bytecode, i); |
| 100 int operand_offset = |
| 101 Bytecodes::GetOperandOffset(bytecode, i, operand_scale); |
| 102 const uint8_t* operand_start = |
| 103 &bytecode_start[prefix_offset + operand_offset]; |
| 104 switch (op_type) { |
| 105 case interpreter::OperandType::kRegCount: |
| 106 os << "#" |
| 107 << DecodeUnsignedOperand(operand_start, op_type, operand_scale); |
| 108 break; |
| 109 case interpreter::OperandType::kIdx: |
| 110 case interpreter::OperandType::kRuntimeId: |
| 111 case interpreter::OperandType::kIntrinsicId: |
| 112 os << "[" |
| 113 << DecodeUnsignedOperand(operand_start, op_type, operand_scale) |
| 114 << "]"; |
| 115 break; |
| 116 case interpreter::OperandType::kImm: |
| 117 os << "[" << DecodeSignedOperand(operand_start, op_type, operand_scale) |
| 118 << "]"; |
| 119 break; |
| 120 case interpreter::OperandType::kFlag8: |
| 121 os << "#" |
| 122 << DecodeUnsignedOperand(operand_start, op_type, operand_scale); |
| 123 break; |
| 124 case interpreter::OperandType::kMaybeReg: |
| 125 case interpreter::OperandType::kReg: |
| 126 case interpreter::OperandType::kRegOut: { |
| 127 Register reg = |
| 128 DecodeRegisterOperand(operand_start, op_type, operand_scale); |
| 129 os << reg.ToString(parameter_count); |
| 130 break; |
| 131 } |
| 132 case interpreter::OperandType::kRegOutTriple: |
| 133 range += 1; |
| 134 case interpreter::OperandType::kRegOutPair: |
| 135 case interpreter::OperandType::kRegPair: { |
| 136 range += 1; |
| 137 Register first_reg = |
| 138 DecodeRegisterOperand(operand_start, op_type, operand_scale); |
| 139 Register last_reg = Register(first_reg.index() + range); |
| 140 os << first_reg.ToString(parameter_count) << "-" |
| 141 << last_reg.ToString(parameter_count); |
| 142 break; |
| 143 } |
| 144 case interpreter::OperandType::kNone: |
| 145 UNREACHABLE(); |
| 146 break; |
| 147 } |
| 148 if (i != number_of_operands - 1) { |
| 149 os << ", "; |
| 150 } |
| 151 } |
| 152 return os; |
| 153 } |
| 154 |
| 155 } // namespace interpreter |
| 156 } // namespace internal |
| 157 } // namespace v8 |
OLD | NEW |