blob: 7b289d0cf342167fa798ce216bd79bb48b1dfd9d [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));
Maja Gagic6ea651f2015-02-24 16:55:04 +010046static const uint32_t kSpecial0Mask = (0x3f << kOpcodeShift);
Andreas Gampe57b34292015-01-14 15:45:59 -080047static 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", },
Maja Gagic6ea651f2015-02-24 16:55:04 +010061 { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", }, // rd = 31 is implicit.
62 { kRTypeMask | (0x1f << 11), 9, "jr", "S", }, // rd = 0 is implicit.
Andreas Gampe57b34292015-01-14 15:45:59 -080063 { kRTypeMask, 9, "jalr", "DS", }, // General case.
Andreas Gampe57b34292015-01-14 15:45:59 -080064 { kRTypeMask, 12, "syscall", "", }, // TODO: code
65 { kRTypeMask, 13, "break", "", }, // TODO: code
66 { kRTypeMask, 15, "sync", "", }, // TODO: type
Maja Gagic6ea651f2015-02-24 16:55:04 +010067 { kRTypeMask, 20, "dsllv", "DTS", },
68 { kRTypeMask, 22, "dsrlv", "DTS", },
69 { kRTypeMask, 23, "dsrav", "DTS", },
Andreas Gampe57b34292015-01-14 15:45:59 -080070 { kRTypeMask, 33, "addu", "DST", },
71 { kRTypeMask, 34, "sub", "DST", },
72 { kRTypeMask, 35, "subu", "DST", },
73 { kRTypeMask, 36, "and", "DST", },
74 { kRTypeMask, 37, "or", "DST", },
75 { kRTypeMask, 38, "xor", "DST", },
76 { kRTypeMask, 39, "nor", "DST", },
77 { kRTypeMask, 42, "slt", "DST", },
78 { kRTypeMask, 43, "sltu", "DST", },
Andreas Gampe57b34292015-01-14 15:45:59 -080079 { kRTypeMask, 45, "daddu", "DST", },
80 { kRTypeMask, 46, "dsub", "DST", },
81 { kRTypeMask, 47, "dsubu", "DST", },
Maja Gagic6ea651f2015-02-24 16:55:04 +010082 // TODO: seleqz, selnez
83 { kRTypeMask, 56, "dsll", "DTA", },
84 { kRTypeMask, 58, "dsrl", "DTA", },
85 { kRTypeMask, 59, "dsra", "DTA", },
86 { kRTypeMask, 60, "dsll32", "DTA", },
87 { kRTypeMask | (0x1f << 21), 62 | (1 << 21), "drotr32", "DTA", },
88 { kRTypeMask, 62, "dsrl32", "DTA", },
89 { kRTypeMask, 63, "dsra32", "DTA", },
Andreas Gampe57b34292015-01-14 15:45:59 -080090
Maja Gagic6ea651f2015-02-24 16:55:04 +010091 // SPECIAL0
92 { kSpecial0Mask | 0x7ff, (2 << 6) | 24, "mul", "DST" },
93 { kSpecial0Mask | 0x7ff, (3 << 6) | 24, "muh", "DST" },
94 { kSpecial0Mask | 0x7ff, (2 << 6) | 25, "mulu", "DST" },
95 { kSpecial0Mask | 0x7ff, (3 << 6) | 25, "muhu", "DST" },
96 { kSpecial0Mask | 0x7ff, (2 << 6) | 26, "div", "DST" },
97 { kSpecial0Mask | 0x7ff, (3 << 6) | 26, "mod", "DST" },
98 { kSpecial0Mask | 0x7ff, (2 << 6) | 27, "divu", "DST" },
99 { kSpecial0Mask | 0x7ff, (3 << 6) | 27, "modu", "DST" },
100 { kSpecial0Mask | 0x7ff, (2 << 6) | 28, "dmul", "DST" },
101 { kSpecial0Mask | 0x7ff, (3 << 6) | 28, "dmuh", "DST" },
102 { kSpecial0Mask | 0x7ff, (2 << 6) | 29, "dmulu", "DST" },
103 { kSpecial0Mask | 0x7ff, (3 << 6) | 29, "dmuhu", "DST" },
104 { kSpecial0Mask | 0x7ff, (2 << 6) | 30, "ddiv", "DST" },
105 { kSpecial0Mask | 0x7ff, (3 << 6) | 30, "dmod", "DST" },
106 { kSpecial0Mask | 0x7ff, (2 << 6) | 31, "ddivu", "DST" },
107 { kSpecial0Mask | 0x7ff, (3 << 6) | 31, "dmodu", "DST" },
108 // TODO: [d]clz, [d]clo
109 // TODO: sdbbp
Andreas Gampe57b34292015-01-14 15:45:59 -0800110
111 // J-type instructions.
112 { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
113 { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
114
115 // I-type instructions.
116 { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
117 { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
118 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
119 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
Andreas Gampe57b34292015-01-14 15:45:59 -0800120 { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
121 { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
Maja Gagic6ea651f2015-02-24 16:55:04 +0100122 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (6 << 16), "dahi", "Si", },
123 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (30 << 16), "dati", "Si", },
Andreas Gampe57b34292015-01-14 15:45:59 -0800124
125 { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
126 { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
127
Andreas Gampe57b34292015-01-14 15:45:59 -0800128 { 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", },
Maja Gagic6ea651f2015-02-24 16:55:04 +0100133 { kITypeMask, 14 << kOpcodeShift, "xori", "TSi", },
134 { kITypeMask | (0x1f << 21), 15 << kOpcodeShift, "lui", "TI", },
135 { kITypeMask, 15 << kOpcodeShift, "aui", "TSI", },
Andreas Gampe57b34292015-01-14 15:45:59 -0800136 { kITypeMask, 25 << kOpcodeShift, "daddiu", "TSi", },
Maja Gagic6ea651f2015-02-24 16:55:04 +0100137 { kITypeMask, 29 << kOpcodeShift, "daui", "TSi", },
Andreas Gampe57b34292015-01-14 15:45:59 -0800138
139 { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
140 { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
141 { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
142 { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
143 { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
Maja Gagic6ea651f2015-02-24 16:55:04 +0100144 { kITypeMask, 39u << kOpcodeShift, "lwu", "TO", },
Andreas Gampe57b34292015-01-14 15:45:59 -0800145 { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
146 { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
147 { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
148 { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
149 { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
150 { kITypeMask, 55u << kOpcodeShift, "ld", "TO", },
151 { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
152 { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
153 { kITypeMask, 63u << kOpcodeShift, "sd", "TO", },
154
155 // Floating point.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100156 { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21), "mfc1", "Td" },
157 { kFpMask | (0x1f << 21), kCop1 | (0x01 << 21), "dmfc1", "Td" },
158 { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21), "mtc1", "Td" },
159 { kFpMask | (0x1f << 21), kCop1 | (0x05 << 21), "dmtc1", "Td" },
160 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" },
161 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" },
162 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" },
163 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" },
164 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" },
165 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" },
166 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" },
167 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" },
168 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" },
169 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" },
170 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" },
171 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" },
172 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" },
173 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" },
174 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" },
175 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" },
176 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" },
177 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" },
178 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" },
179 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" },
180 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" },
Andreas Gampe57b34292015-01-14 15:45:59 -0800181};
182
183static uint32_t ReadU32(const uint8_t* ptr) {
184 // We only support little-endian MIPS64.
185 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
186}
187
188static void DumpMips64(std::ostream& os, const uint8_t* instr_ptr) {
189 uint32_t instruction = ReadU32(instr_ptr);
190
191 uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type.
192 uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type.
193 uint32_t rd = (instruction >> 11) & 0x1f; // R-type.
194 uint32_t sa = (instruction >> 6) & 0x1f; // R-type.
195
196 std::string opcode;
197 std::ostringstream args;
198
199 // TODO: remove this!
200 uint32_t op = (instruction >> 26) & 0x3f;
201 uint32_t function = (instruction & 0x3f); // R-type.
202 opcode = StringPrintf("op=%d fn=%d", op, function);
203
204 for (size_t i = 0; i < arraysize(gMips64Instructions); ++i) {
205 if (gMips64Instructions[i].Matches(instruction)) {
206 opcode = gMips64Instructions[i].name;
207 for (const char* args_fmt = gMips64Instructions[i].args_fmt; *args_fmt; ++args_fmt) {
208 switch (*args_fmt) {
209 case 'A': // sa (shift amount).
210 args << sa;
211 break;
212 case 'B': // Branch offset.
213 {
214 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
215 offset <<= 2;
216 offset += 4; // Delay slot.
217 args << StringPrintf("%p ; %+d", instr_ptr + offset, offset);
218 }
219 break;
220 case 'D': args << 'r' << rd; break;
221 case 'd': args << 'f' << rd; break;
Maja Gagic6ea651f2015-02-24 16:55:04 +0100222 case 'a': args << 'f' << sa; break;
Andreas Gampe57b34292015-01-14 15:45:59 -0800223 case 'f': // Floating point "fmt".
224 {
225 size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts?
226 switch (fmt) {
227 case 0: opcode += ".s"; break;
228 case 1: opcode += ".d"; break;
229 case 4: opcode += ".w"; break;
230 case 5: opcode += ".l"; break;
231 case 6: opcode += ".ps"; break;
232 default: opcode += ".?"; break;
233 }
234 continue; // No ", ".
235 }
236 break;
237 case 'I': // Upper 16-bit immediate.
238 args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
239 break;
240 case 'i': // Sign-extended lower 16-bit immediate.
241 args << static_cast<int16_t>(instruction & 0xffff);
242 break;
243 case 'L': // Jump label.
244 {
245 // TODO: is this right?
246 uint32_t instr_index = (instruction & 0x1ffffff);
247 uint32_t target = (instr_index << 2);
248 target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4)
249 & 0xf0000000);
250 args << reinterpret_cast<void*>(target);
251 }
252 break;
253 case 'O': // +x(rs)
254 {
255 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
256 args << StringPrintf("%+d(r%d)", offset, rs);
257 if (rs == 17) {
258 args << " ; ";
259 Thread::DumpThreadOffset<8>(args, offset);
260 }
261 }
262 break;
263 case 'S': args << 'r' << rs; break;
264 case 's': args << 'f' << rs; break;
265 case 'T': args << 'r' << rt; break;
266 case 't': args << 'f' << rt; break;
267 }
268 if (*(args_fmt + 1)) {
269 args << ", ";
270 }
271 }
272 break;
273 }
274 }
275
276 os << StringPrintf("%p: %08x\t%-7s ", instr_ptr, instruction, opcode.c_str())
277 << args.str() << '\n';
278}
279
280size_t DisassemblerMips64::Dump(std::ostream& os, const uint8_t* begin) {
281 DumpMips64(os, begin);
282 return 4;
283}
284
285void DisassemblerMips64::Dump(std::ostream& os, const uint8_t* begin,
286 const uint8_t* end) {
287 for (const uint8_t* cur = begin; cur < end; cur += 4) {
288 DumpMips64(os, cur);
289 }
290}
291
292} // namespace mips64
293} // namespace art