| 101 | std::string _text; |
| 102 | |
| 103 | std::string adjust_text(const Bytes &instr_bytes, const std::string &instr_text) { |
| 104 | if ((instr_text.rfind("jal\t", 0) == 0) || instr_text.rfind("j\t", 0) == 0) { |
| 105 | std::stringstream stream; |
| 106 | bool neg; |
| 107 | uint32_t offset; |
| 108 | if (instr_bytes.size() == 2) { |
| 109 | uint16_t instr16 = ((uint16_t)instr_bytes[1]) << 8 | instr_bytes[0]; |
| 110 | /* |
| 111 | * 15 13 12 2 1 0 |
| 112 | * [funct3] [imm] [op] |
| 113 | */ |
| 114 | uint16_t imm = (instr16 >> 2) & 0x7FFU; |
| 115 | neg = (imm & (1U << 10)) != 0; |
| 116 | offset = (((imm >> 1) & 0b111) << 1) |
| 117 | | (((imm >> 9) & 0b1 ) << 4) |
| 118 | | (((imm >> 0) & 0b1 ) << 5) |
| 119 | | (((imm >> 5) & 0b1 ) << 6) |
| 120 | | (((imm >> 4) & 0b1 ) << 7) |
| 121 | | (((imm >> 7) & 0b11 ) << 8) |
| 122 | | (((imm >> 6) & 0b1 ) << 10); |
| 123 | } else if (instr_bytes.size() == 4) { |
| 124 | uint32_t instr32 = (((uint32_t)instr_bytes[3]) << 24) |
| 125 | | (((uint32_t)instr_bytes[2]) << 16) |
| 126 | | (((uint32_t)instr_bytes[1]) << 8) |
| 127 | | (((uint32_t)instr_bytes[0]) << 0); |
| 128 | /* |
| 129 | * 31 30 21 20 19 12 11 7 6 0 |
| 130 | * imm[20] imm[10:1] imm[11] imm[19:12] rd opcode |
| 131 | */ |
| 132 | neg = (instr32 & (1U << 31)) != 0; |
| 133 | offset = (((instr32 >> 21) & 0x3FF) << 1) |
| 134 | | (((instr32 >> 20) & 0b1 ) << 11) |
| 135 | | (((instr32 >> 12) & 0xFF ) << 12); |
| 136 | if (neg) { |
| 137 | offset = (1U << 20) - offset; |
| 138 | } |
| 139 | } else { |
| 140 | assert(0); |
| 141 | } |
| 142 | stream << instr_text.substr(0, instr_text.find("0x")) |
| 143 | << (neg ? "-" : "+") << std::hex << "0x" << offset; |
| 144 | return stream.str(); |
| 145 | } |
| 146 | if ((instr_text.rfind("bnez\t", 0) == 0) || instr_text.rfind("beqz\t", 0) == 0) { |
| 147 | std::stringstream stream; |
| 148 | bool neg; |
| 149 | uint32_t offset; |
| 150 | if (instr_bytes.size() == 2) { |
| 151 | uint16_t instr16 = ((uint16_t)instr_bytes[1]) << 8 | instr_bytes[0]; |
| 152 | neg = (instr16 & (1U << 12)) != 0; |
| 153 | offset = (((instr16 >> 3) & 0b11) << 1) |
| 154 | | (((instr16 >> 10) & 0b11) << 3) |
| 155 | | (((instr16 >> 2) & 0b1 ) << 5) |
| 156 | | (((instr16 >> 5) & 0b11) << 6); |
| 157 | if (neg) { |
| 158 | offset = (1U << 8) - offset; |
| 159 | } |
| 160 | } else if (instr_bytes.size() == 4) { |