blob: 2d3239f4f343feac0548e96a00fdea791ea630a4 [file] [log] [blame]
Andreas Gampe57b34292015-01-14 15:45:59 -08001/*
2 * Copyright (C) 2014 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_mips64.h"
18
19#include <ostream>
20#include <sstream>
21
22#include "base/logging.h"
23#include "base/stringprintf.h"
24#include "thread.h"
25
26namespace art {
27namespace mips64 {
28
29
30struct Mips64Instruction {
31 uint32_t mask;
32 uint32_t value;
33 const char* name;
34 const char* args_fmt;
35
36 bool Matches(uint32_t instruction) const {
37 return (instruction & mask) == value;
38 }
39};
40
41static const uint32_t kOpcodeShift = 26;
42static const uint32_t kCop1 = (17 << kOpcodeShift);
43static const uint32_t kITypeMask = (0x3f << kOpcodeShift);
44static const uint32_t kJTypeMask = (0x3f << kOpcodeShift);
45static const uint32_t kRTypeMask = ((0x3f << kOpcodeShift) | (0x3f));
46static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift);
47static const uint32_t kFpMask = kRTypeMask;
48
49static const Mips64Instruction gMips64Instructions[] = {
50 // "sll r0, r0, 0" is the canonical "nop", used in delay slots.
51 { 0xffffffff, 0, "nop", "" },
52
53 // R-type instructions.
54 { kRTypeMask, 0, "sll", "DTA", },
55 // 0, 1, movci
56 { kRTypeMask, 2, "srl", "DTA", },
57 { kRTypeMask, 3, "sra", "DTA", },
58 { kRTypeMask, 4, "sllv", "DTS", },
59 { kRTypeMask, 6, "srlv", "DTS", },
60 { kRTypeMask, 7, "srav", "DTS", },
61 { kRTypeMask, 8, "jr", "S", },
62 // rd = 31 is implicit.
63 { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", },
64 { kRTypeMask, 9, "jalr", "DS", }, // General case.
65 { kRTypeMask | (0x1f << 6), 10, "movz", "DST", },
66 { kRTypeMask | (0x1f << 6), 11, "movn", "DST", },
67 { kRTypeMask, 12, "syscall", "", }, // TODO: code
68 { kRTypeMask, 13, "break", "", }, // TODO: code
69 { kRTypeMask, 15, "sync", "", }, // TODO: type
70 { 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 { kRTypeMask, 44, "dadd", "DST", },
89 { kRTypeMask, 45, "daddu", "DST", },
90 { kRTypeMask, 46, "dsub", "DST", },
91 { kRTypeMask, 47, "dsubu", "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 | 0x7ff, (28 << kOpcodeShift) | 36, "dclz", "DS" },
103 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" },
104 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" },
105 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" },
106 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" },
107 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" },
108 { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" },
109
110 // J-type instructions.
111 { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
112 { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
113
114 // I-type instructions.
115 { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
116 { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
117 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
118 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
119 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" },
120 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" },
121 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" },
122 { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
123 { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
124
125 { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
126 { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
127
128 { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", },
129 { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", },
130 { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", },
131 { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", },
132 { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", },
133 { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", },
134 { kITypeMask, 14 << kOpcodeShift, "ori", "TSi", },
135 { kITypeMask, 15 << kOpcodeShift, "lui", "TI", },
136
137 { kITypeMask, 24 << kOpcodeShift, "daddi", "TSi", },
138 { kITypeMask, 25 << kOpcodeShift, "daddiu", "TSi", },
139
140
141 { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
142 { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
143 { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
144 { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
145 { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
146 { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
147 { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
148 { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
149 { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
150 { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
151 { kITypeMask, 55u << kOpcodeShift, "ld", "TO", },
152 { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
153 { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
154 { kITypeMask, 63u << kOpcodeShift, "sd", "TO", },
155
156 // Floating point.
157 { kFpMask, kCop1 | 0, "add", "fdst" },
158 { kFpMask, kCop1 | 1, "sub", "fdst" },
159 { kFpMask, kCop1 | 2, "mul", "fdst" },
160 { kFpMask, kCop1 | 3, "div", "fdst" },
161 { kFpMask | (0x1f << 16), kCop1 | 4, "sqrt", "fdst" },
162 { kFpMask | (0x1f << 16), kCop1 | 5, "abs", "fds" },
163 { kFpMask | (0x1f << 16), kCop1 | 6, "mov", "fds" },
164 { kFpMask | (0x1f << 16), kCop1 | 7, "neg", "fds" },
165 { kFpMask | (0x1f << 16), kCop1 | 8, "round.l", "fds" },
166 { kFpMask | (0x1f << 16), kCop1 | 9, "trunc.l", "fds" },
167 { kFpMask | (0x1f << 16), kCop1 | 10, "ceil.l", "fds" },
168 { kFpMask | (0x1f << 16), kCop1 | 11, "floor.l", "fds" },
169 { kFpMask | (0x1f << 16), kCop1 | 12, "round.w", "fds" },
170 { kFpMask | (0x1f << 16), kCop1 | 13, "trunc.w", "fds" },
171 { kFpMask | (0x1f << 16), kCop1 | 14, "ceil.w", "fds" },
172 { kFpMask | (0x1f << 16), kCop1 | 15, "floor.w", "fds" },
173 { kFpMask | (0x1f << 16), kCop1 | 32, "cvt.s", "fds" },
174 { kFpMask | (0x1f << 16), kCop1 | 33, "cvt.d", "fds" },
175 { kFpMask | (0x1f << 16), kCop1 | 36, "cvt.w", "fds" },
176 { kFpMask | (0x1f << 16), kCop1 | 37, "cvt.l", "fds" },
177 { kFpMask | (0x1f << 16), kCop1 | 38, "cvt.ps", "fds" },
178};
179
180static uint32_t ReadU32(const uint8_t* ptr) {
181 // We only support little-endian MIPS64.
182 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
183}
184
185static void DumpMips64(std::ostream& os, const uint8_t* instr_ptr) {
186 uint32_t instruction = ReadU32(instr_ptr);
187
188 uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type.
189 uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type.
190 uint32_t rd = (instruction >> 11) & 0x1f; // R-type.
191 uint32_t sa = (instruction >> 6) & 0x1f; // R-type.
192
193 std::string opcode;
194 std::ostringstream args;
195
196 // TODO: remove this!
197 uint32_t op = (instruction >> 26) & 0x3f;
198 uint32_t function = (instruction & 0x3f); // R-type.
199 opcode = StringPrintf("op=%d fn=%d", op, function);
200
201 for (size_t i = 0; i < arraysize(gMips64Instructions); ++i) {
202 if (gMips64Instructions[i].Matches(instruction)) {
203 opcode = gMips64Instructions[i].name;
204 for (const char* args_fmt = gMips64Instructions[i].args_fmt; *args_fmt; ++args_fmt) {
205 switch (*args_fmt) {
206 case 'A': // sa (shift amount).
207 args << sa;
208 break;
209 case 'B': // Branch offset.
210 {
211 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
212 offset <<= 2;
213 offset += 4; // Delay slot.
214 args << StringPrintf("%p ; %+d", instr_ptr + offset, offset);
215 }
216 break;
217 case 'D': args << 'r' << rd; break;
218 case 'd': args << 'f' << rd; break;
219 case 'f': // Floating point "fmt".
220 {
221 size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts?
222 switch (fmt) {
223 case 0: opcode += ".s"; break;
224 case 1: opcode += ".d"; break;
225 case 4: opcode += ".w"; break;
226 case 5: opcode += ".l"; break;
227 case 6: opcode += ".ps"; break;
228 default: opcode += ".?"; break;
229 }
230 continue; // No ", ".
231 }
232 break;
233 case 'I': // Upper 16-bit immediate.
234 args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
235 break;
236 case 'i': // Sign-extended lower 16-bit immediate.
237 args << static_cast<int16_t>(instruction & 0xffff);
238 break;
239 case 'L': // Jump label.
240 {
241 // TODO: is this right?
242 uint32_t instr_index = (instruction & 0x1ffffff);
243 uint32_t target = (instr_index << 2);
244 target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4)
245 & 0xf0000000);
246 args << reinterpret_cast<void*>(target);
247 }
248 break;
249 case 'O': // +x(rs)
250 {
251 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
252 args << StringPrintf("%+d(r%d)", offset, rs);
253 if (rs == 17) {
254 args << " ; ";
255 Thread::DumpThreadOffset<8>(args, offset);
256 }
257 }
258 break;
259 case 'S': args << 'r' << rs; break;
260 case 's': args << 'f' << rs; break;
261 case 'T': args << 'r' << rt; break;
262 case 't': args << 'f' << rt; break;
263 }
264 if (*(args_fmt + 1)) {
265 args << ", ";
266 }
267 }
268 break;
269 }
270 }
271
272 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str())
273 << args.str() << '\n';
274}
275
276size_t DisassemblerMips64::Dump(std::ostream& os, const uint8_t* begin) {
277 DumpMips64(os, begin);
278 return 4;
279}
280
281void DisassemblerMips64::Dump(std::ostream& os, const uint8_t* begin,
282 const uint8_t* end) {
283 for (const uint8_t* cur = begin; cur < end; cur += 4) {
284 DumpMips64(os, cur);
285 }
286}
287
288} // namespace mips64
289} // namespace art