blob: 3d8a56730a86a45ae6b8456a9b4f1f962f48db16 [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", },
74 { kRTypeMask, 24, "mult", "ST", },
75 { kRTypeMask, 25, "multu", "ST", },
76 { kRTypeMask, 26, "div", "ST", },
77 { kRTypeMask, 27, "divu", "ST", },
78 { kRTypeMask, 32, "add", "DST", },
79 { kRTypeMask, 33, "addu", "DST", },
80 { kRTypeMask, 34, "sub", "DST", },
81 { kRTypeMask, 35, "subu", "DST", },
82 { kRTypeMask, 36, "and", "DST", },
83 { kRTypeMask, 37, "or", "DST", },
84 { kRTypeMask, 38, "xor", "DST", },
85 { kRTypeMask, 39, "nor", "DST", },
86 { kRTypeMask, 42, "slt", "DST", },
87 { kRTypeMask, 43, "sltu", "DST", },
88 // 0, 48, tge
89 // 0, 49, tgeu
90 // 0, 50, tlt
91 // 0, 51, tltu
92 // 0, 52, teq
93 // 0, 54, tne
94
95 // SPECIAL2
96 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" },
97 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" },
98 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" },
99 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" },
100 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" },
101 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" },
102 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" },
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700103 { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" }, // TODO: code
Elliott Hughes60454e82012-04-25 12:56:03 -0700104
105 // J-type instructions.
106 { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
107 { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
108
109 // I-type instructions.
110 { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
111 { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
112 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
113 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
114 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" },
115 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" },
116 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" },
117 { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
118 { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
119
120 { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
121 { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
122
123 { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", },
124 { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", },
125 { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", },
126 { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", },
127 { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", },
128 { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", },
129 { kITypeMask, 14 << kOpcodeShift, "ori", "TSi", },
130 { kITypeMask, 15 << kOpcodeShift, "lui", "TI", },
131
Brian Carlstromd74e41b2013-03-24 23:47:01 -0700132 { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
133 { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
134 { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
135 { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
136 { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
137 { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
138 { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
139 { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
140 { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
Andreas Gampe8d365912015-01-13 11:32:32 -0800141 { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
Brian Carlstromd74e41b2013-03-24 23:47:01 -0700142 { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
Andreas Gampe8d365912015-01-13 11:32:32 -0800143 { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
Elliott Hughes60454e82012-04-25 12:56:03 -0700144
145 // Floating point.
Douglas Leung1cd27902015-02-13 16:55:57 -0800146 { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21) | 0, "mfc1", "Td" },
147 { kFpMask | (0x1f << 21), kCop1 | (0x03 << 21) | 0, "mfhc1", "Td" },
148 { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21) | 0, "mtc1", "Td" },
149 { kFpMask | (0x1f << 21), kCop1 | (0x07 << 21) | 0, "mthc1", "Td" },
150 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" },
151 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" },
152 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" },
153 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" },
154 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" },
155 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" },
156 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" },
157 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" },
158 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" },
159 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" },
160 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" },
161 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" },
162 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" },
163 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" },
164 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" },
165 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" },
166 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" },
167 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" },
168 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" },
169 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" },
170 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" },
Elliott Hughes60454e82012-04-25 12:56:03 -0700171};
172
173static uint32_t ReadU32(const uint8_t* ptr) {
jeffhao4f8f04a2012-10-02 18:10:35 -0700174 // We only support little-endian MIPS.
175 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
Elliott Hughes60454e82012-04-25 12:56:03 -0700176}
177
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700178size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) {
Elliott Hughes60454e82012-04-25 12:56:03 -0700179 uint32_t instruction = ReadU32(instr_ptr);
180
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700181 uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type.
182 uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type.
183 uint32_t rd = (instruction >> 11) & 0x1f; // R-type.
184 uint32_t sa = (instruction >> 6) & 0x1f; // R-type.
Elliott Hughes60454e82012-04-25 12:56:03 -0700185
186 std::string opcode;
187 std::ostringstream args;
188
189 // TODO: remove this!
190 uint32_t op = (instruction >> 26) & 0x3f;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700191 uint32_t function = (instruction & 0x3f); // R-type.
Elliott Hughes60454e82012-04-25 12:56:03 -0700192 opcode = StringPrintf("op=%d fn=%d", op, function);
193
194 for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) {
195 if (gMipsInstructions[i].Matches(instruction)) {
196 opcode = gMipsInstructions[i].name;
197 for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) {
198 switch (*args_fmt) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700199 case 'A': // sa (shift amount).
Elliott Hughes60454e82012-04-25 12:56:03 -0700200 args << sa;
201 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700202 case 'B': // Branch offset.
Elliott Hughes60454e82012-04-25 12:56:03 -0700203 {
204 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
205 offset <<= 2;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700206 offset += 4; // Delay slot.
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700207 args << FormatInstructionPointer(instr_ptr + offset)
208 << StringPrintf(" ; %+d", offset);
Elliott Hughes60454e82012-04-25 12:56:03 -0700209 }
210 break;
211 case 'D': args << 'r' << rd; break;
212 case 'd': args << 'f' << rd; break;
Douglas Leung1cd27902015-02-13 16:55:57 -0800213 case 'a': args << 'f' << sa; break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700214 case 'f': // Floating point "fmt".
Elliott Hughes60454e82012-04-25 12:56:03 -0700215 {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700216 size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts?
Elliott Hughes60454e82012-04-25 12:56:03 -0700217 switch (fmt) {
218 case 0: opcode += ".s"; break;
219 case 1: opcode += ".d"; break;
220 case 4: opcode += ".w"; break;
221 case 5: opcode += ".l"; break;
222 case 6: opcode += ".ps"; break;
223 default: opcode += ".?"; break;
224 }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700225 continue; // No ", ".
Elliott Hughes60454e82012-04-25 12:56:03 -0700226 }
227 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700228 case 'I': // Upper 16-bit immediate.
Elliott Hughes60454e82012-04-25 12:56:03 -0700229 args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
230 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700231 case 'i': // Sign-extended lower 16-bit immediate.
Elliott Hughes60454e82012-04-25 12:56:03 -0700232 args << static_cast<int16_t>(instruction & 0xffff);
233 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700234 case 'L': // Jump label.
Elliott Hughes60454e82012-04-25 12:56:03 -0700235 {
236 // TODO: is this right?
237 uint32_t instr_index = (instruction & 0x1ffffff);
238 uint32_t target = (instr_index << 2);
239 target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000);
240 args << reinterpret_cast<void*>(target);
241 }
242 break;
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700243 case 'O': // +x(rs)
Elliott Hughes60454e82012-04-25 12:56:03 -0700244 {
245 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
246 args << StringPrintf("%+d(r%d)", offset, rs);
247 if (rs == 17) {
248 args << " ; ";
Ian Rogersdd7624d2014-03-14 17:43:00 -0700249 Thread::DumpThreadOffset<4>(args, offset);
Elliott Hughes60454e82012-04-25 12:56:03 -0700250 }
251 }
252 break;
253 case 'S': args << 'r' << rs; break;
254 case 's': args << 'f' << rs; break;
255 case 'T': args << 'r' << rt; break;
256 case 't': args << 'f' << rt; break;
257 }
258 if (*(args_fmt + 1)) {
259 args << ", ";
260 }
261 }
262 break;
263 }
264 }
265
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700266 os << FormatInstructionPointer(instr_ptr)
267 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
268 << args.str() << '\n';
Ian Rogersb23a7722012-10-09 16:54:26 -0700269 return 4;
270}
271
Elliott Hughes60454e82012-04-25 12:56:03 -0700272void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
273 for (const uint8_t* cur = begin; cur < end; cur += 4) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700274 Dump(os, cur);
Elliott Hughes60454e82012-04-25 12:56:03 -0700275 }
276}
277
278} // namespace mips
279} // namespace art