blob: b27b555265962022601be8f69be8e9b0a70fae17 [file] [log] [blame]
Elliott Hughes60454e82012-04-25 12:56:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "disassembler_mips.h"
18
Ian Rogerscf7f1912014-10-22 22:06:39 -070019#include <ostream>
Ian Rogersc7dd2952014-10-21 23:31:19 -070020#include <sstream>
Elliott Hughes60454e82012-04-25 12:56:03 -070021
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringprintf.h"
Elliott Hughes60454e82012-04-25 12:56:03 -070024#include "thread.h"
25
26namespace art {
27namespace mips {
28
29struct MipsInstruction {
30 uint32_t mask;
31 uint32_t value;
32 const char* name;
33 const char* args_fmt;
34
35 bool Matches(uint32_t instruction) const {
36 return (instruction & mask) == value;
37 }
38};
39
40static const uint32_t kOpcodeShift = 26;
41
42static const uint32_t kCop1 = (17 << kOpcodeShift);
43
44static const uint32_t kITypeMask = (0x3f << kOpcodeShift);
45static const uint32_t kJTypeMask = (0x3f << kOpcodeShift);
46static const uint32_t kRTypeMask = ((0x3f << kOpcodeShift) | (0x3f));
47static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift);
48static const uint32_t kFpMask = kRTypeMask;
49
50static const MipsInstruction gMipsInstructions[] = {
51 // "sll r0, r0, 0" is the canonical "nop", used in delay slots.
52 { 0xffffffff, 0, "nop", "" },
53
54 // R-type instructions.
55 { kRTypeMask, 0, "sll", "DTA", },
56 // 0, 1, movci
57 { kRTypeMask, 2, "srl", "DTA", },
58 { kRTypeMask, 3, "sra", "DTA", },
59 { kRTypeMask, 4, "sllv", "DTS", },
60 { kRTypeMask, 6, "srlv", "DTS", },
61 { kRTypeMask, 7, "srav", "DTS", },
62 { kRTypeMask, 8, "jr", "S", },
Brian Carlstrom7934ac22013-07-26 10:54:15 -070063 { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", }, // rd = 31 is implicit.
64 { kRTypeMask, 9, "jalr", "DS", }, // General case.
Elliott Hughes60454e82012-04-25 12:56:03 -070065 { kRTypeMask | (0x1f << 6), 10, "movz", "DST", },
66 { kRTypeMask | (0x1f << 6), 11, "movn", "DST", },
Brian Carlstrom7934ac22013-07-26 10:54:15 -070067 { kRTypeMask, 12, "syscall", "", }, // TODO: code
68 { kRTypeMask, 13, "break", "", }, // TODO: code
69 { kRTypeMask, 15, "sync", "", }, // TODO: type
Elliott Hughes60454e82012-04-25 12:56:03 -070070 { kRTypeMask, 16, "mfhi", "D", },
71 { kRTypeMask, 17, "mthi", "S", },
72 { kRTypeMask, 18, "mflo", "D", },
73 { kRTypeMask, 19, "mtlo", "S", },
Douglas Leung027f0ff2015-02-27 19:05:03 -080074 { kRTypeMask | (0x1f << 6), 24, "mult", "ST", },
75 { kRTypeMask | (0x1f << 6), 25, "multu", "ST", },
76 { kRTypeMask | (0x1f << 6), 26, "div", "ST", },
77 { kRTypeMask | (0x1f << 6), 27, "divu", "ST", },
78 { kRTypeMask | (0x1f << 6), 24 + (2 << 6), "mul", "DST", },
79 { kRTypeMask | (0x1f << 6), 24 + (3 << 6), "muh", "DST", },
80 { kRTypeMask | (0x1f << 6), 26 + (2 << 6), "div", "DST", },
81 { kRTypeMask | (0x1f << 6), 26 + (3 << 6), "mod", "DST", },
Elliott Hughes60454e82012-04-25 12:56:03 -070082 { kRTypeMask, 32, "add", "DST", },
83 { kRTypeMask, 33, "addu", "DST", },
84 { kRTypeMask, 34, "sub", "DST", },
85 { kRTypeMask, 35, "subu", "DST", },
86 { kRTypeMask, 36, "and", "DST", },
87 { kRTypeMask, 37, "or", "DST", },
88 { kRTypeMask, 38, "xor", "DST", },
89 { kRTypeMask, 39, "nor", "DST", },
90 { kRTypeMask, 42, "slt", "DST", },
91 { kRTypeMask, 43, "sltu", "DST", },
92 // 0, 48, tge
93 // 0, 49, tgeu
94 // 0, 50, tlt
95 // 0, 51, tltu
96 // 0, 52, teq
97 // 0, 54, tne
98
99 // SPECIAL2
100 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" },
101 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" },
102 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" },
103 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" },
104 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" },
105 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" },
106 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" },
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700107 { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" }, // TODO: code
Elliott Hughes60454e82012-04-25 12:56:03 -0700108
109 // J-type instructions.
110 { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
111 { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
112
113 // I-type instructions.
114 { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
115 { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
116 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
117 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
118 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" },
119 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" },
120 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" },
121 { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
122 { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
123
124 { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
125 { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
126
127 { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", },
128 { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", },
129 { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", },
130 { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", },
131 { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", },
132 { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", },
133 { kITypeMask, 14 << kOpcodeShift, "ori", "TSi", },
134 { kITypeMask, 15 << kOpcodeShift, "lui", "TI", },
135
Brian Carlstromd74e41b2013-03-24 23:47:01 -0700136 { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
137 { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
138 { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
139 { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
140 { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
141 { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
142 { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
143 { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
144 { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
Andreas Gampe8d365912015-01-13 11:32:32 -0800145 { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
Brian Carlstromd74e41b2013-03-24 23:47:01 -0700146 { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
Andreas Gampe8d365912015-01-13 11:32:32 -0800147 { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
Elliott Hughes60454e82012-04-25 12:56:03 -0700148
149 // Floating point.
Douglas Leung1cd27902015-02-13 16:55:57 -0800150 { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21) | 0, "mfc1", "Td" },
151 { kFpMask | (0x1f << 21), kCop1 | (0x03 << 21) | 0, "mfhc1", "Td" },
152 { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21) | 0, "mtc1", "Td" },
153 { kFpMask | (0x1f << 21), kCop1 | (0x07 << 21) | 0, "mthc1", "Td" },
154 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" },
155 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" },
156 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" },
157 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" },
158 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" },
159 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" },
160 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" },
161 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" },
162 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" },
163 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" },
164 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" },
165 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" },
166 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" },
167 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" },
168 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" },
169 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" },
170 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" },
171 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" },
172 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" },
173 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" },
174 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" },
Elliott Hughes60454e82012-04-25 12:56:03 -0700175};
176
177static uint32_t ReadU32(const uint8_t* ptr) {
jeffhao4f8f04a2012-10-02 18:10:35 -0700178 // We only support little-endian MIPS.
179 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
Elliott Hughes60454e82012-04-25 12:56:03 -0700180}
181
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700182size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes60454e82012-04-25 12:56:03 -0700183 uint32_t instruction = ReadU32(instr_ptr);
184
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700185 uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type.
186 uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type.
187 uint32_t rd = (instruction >> 11) & 0x1f; // R-type.
188 uint32_t sa = (instruction >> 6) & 0x1f; // R-type.
Elliott Hughes60454e82012-04-25 12:56:03 -0700189
190 std::string opcode;
191 std::ostringstream args;
192
193 // TODO: remove this!
194 uint32_t op = (instruction >> 26) & 0x3f;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700195 uint32_t function = (instruction & 0x3f); // R-type.
Elliott Hughes60454e82012-04-25 12:56:03 -0700196 opcode = StringPrintf("op=%d fn=%d", op, function);
197
198 for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) {
199 if (gMipsInstructions[i].Matches(instruction)) {
200 opcode = gMipsInstructions[i].name;
201 for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) {
202 switch (*args_fmt) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700203 case 'A': // sa (shift amount).
Elliott Hughes60454e82012-04-25 12:56:03 -0700204 args << sa;
205 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700206 case 'B': // Branch offset.
Elliott Hughes60454e82012-04-25 12:56:03 -0700207 {
208 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
209 offset <<= 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700210 offset += 4; // Delay slot.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700211 args << FormatInstructionPointer(instr_ptr + offset)
212 << StringPrintf(" ; %+d", offset);
Elliott Hughes60454e82012-04-25 12:56:03 -0700213 }
214 break;
215 case 'D': args << 'r' << rd; break;
216 case 'd': args << 'f' << rd; break;
Douglas Leung1cd27902015-02-13 16:55:57 -0800217 case 'a': args << 'f' << sa; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700218 case 'f': // Floating point "fmt".
Elliott Hughes60454e82012-04-25 12:56:03 -0700219 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700220 size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts?
Elliott Hughes60454e82012-04-25 12:56:03 -0700221 switch (fmt) {
222 case 0: opcode += ".s"; break;
223 case 1: opcode += ".d"; break;
224 case 4: opcode += ".w"; break;
225 case 5: opcode += ".l"; break;
226 case 6: opcode += ".ps"; break;
227 default: opcode += ".?"; break;
228 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700229 continue; // No ", ".
Elliott Hughes60454e82012-04-25 12:56:03 -0700230 }
231 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700232 case 'I': // Upper 16-bit immediate.
Elliott Hughes60454e82012-04-25 12:56:03 -0700233 args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
234 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700235 case 'i': // Sign-extended lower 16-bit immediate.
Elliott Hughes60454e82012-04-25 12:56:03 -0700236 args << static_cast<int16_t>(instruction & 0xffff);
237 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700238 case 'L': // Jump label.
Elliott Hughes60454e82012-04-25 12:56:03 -0700239 {
240 // TODO: is this right?
241 uint32_t instr_index = (instruction & 0x1ffffff);
242 uint32_t target = (instr_index << 2);
243 target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000);
244 args << reinterpret_cast<void*>(target);
245 }
246 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700247 case 'O': // +x(rs)
Elliott Hughes60454e82012-04-25 12:56:03 -0700248 {
249 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
250 args << StringPrintf("%+d(r%d)", offset, rs);
251 if (rs == 17) {
252 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700253 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes60454e82012-04-25 12:56:03 -0700254 }
255 }
256 break;
257 case 'S': args << 'r' << rs; break;
258 case 's': args << 'f' << rs; break;
259 case 'T': args << 'r' << rt; break;
260 case 't': args << 'f' << rt; break;
261 }
262 if (*(args_fmt + 1)) {
263 args << ", ";
264 }
265 }
266 break;
267 }
268 }
269
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700270 os << FormatInstructionPointer(instr_ptr)
271 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
272 << args.str() << '\n';
Ian Rogersb23a7722012-10-09 16:54:26 -0700273 return 4;
274}
275
Elliott Hughes60454e82012-04-25 12:56:03 -0700276void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
277 for (const uint8_t* cur = begin; cur < end; cur += 4) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700278 Dump(os, cur);
Elliott Hughes60454e82012-04-25 12:56:03 -0700279 }
280}
281
282} // namespace mips
283} // namespace art