1 /*
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
19 #include <ostream>
20 #include <sstream>
21
22 #include "base/logging.h"
23 #include "base/stringprintf.h"
24 #include "thread.h"
25
26 namespace art {
27 namespace mips {
28
29 struct MipsInstruction {
30 uint32_t mask;
31 uint32_t value;
32 const char* name;
33 const char* args_fmt;
34
Matchesart::mips::MipsInstruction35 bool Matches(uint32_t instruction) const {
36 return (instruction & mask) == value;
37 }
38 };
39
40 static const uint32_t kOpcodeShift = 26;
41
42 static const uint32_t kCop1 = (17 << kOpcodeShift);
43
44 static const uint32_t kITypeMask = (0x3f << kOpcodeShift);
45 static const uint32_t kJTypeMask = (0x3f << kOpcodeShift);
46 static const uint32_t kRTypeMask = ((0x3f << kOpcodeShift) | (0x3f));
47 static const uint32_t kSpecial0Mask = (0x3f << kOpcodeShift);
48 static const uint32_t kSpecial2Mask = (0x3f << kOpcodeShift);
49 static const uint32_t kFpMask = kRTypeMask;
50
51 static const MipsInstruction gMipsInstructions[] = {
52 // "sll r0, r0, 0" is the canonical "nop", used in delay slots.
53 { 0xffffffff, 0, "nop", "" },
54
55 // R-type instructions.
56 { kRTypeMask, 0, "sll", "DTA", },
57 // 0, 1, movci
58 { kRTypeMask, 2, "srl", "DTA", },
59 { kRTypeMask, 3, "sra", "DTA", },
60 { kRTypeMask, 4, "sllv", "DTS", },
61 { kRTypeMask, 6, "srlv", "DTS", },
62 { kRTypeMask, 7, "srav", "DTS", },
63 { kRTypeMask, 8, "jr", "S", },
64 { kRTypeMask | (0x1f << 11), 9 | (31 << 11), "jalr", "S", }, // rd = 31 is implicit.
65 { kRTypeMask | (0x1f << 11), 9, "jr", "S", }, // rd = 0 is implicit.
66 { kRTypeMask, 9, "jalr", "DS", }, // General case.
67 { kRTypeMask | (0x1f << 6), 10, "movz", "DST", },
68 { kRTypeMask | (0x1f << 6), 11, "movn", "DST", },
69 { kRTypeMask, 12, "syscall", "", }, // TODO: code
70 { kRTypeMask, 13, "break", "", }, // TODO: code
71 { kRTypeMask, 15, "sync", "", }, // TODO: type
72 { kRTypeMask, 16, "mfhi", "D", },
73 { kRTypeMask, 17, "mthi", "S", },
74 { kRTypeMask, 18, "mflo", "D", },
75 { kRTypeMask, 19, "mtlo", "S", },
76 { kRTypeMask, 20, "dsllv", "DTS", },
77 { kRTypeMask, 22, "dsrlv", "DTS", },
78 { kRTypeMask, 23, "dsrav", "DTS", },
79 { kRTypeMask | (0x1f << 6), 24, "mult", "ST", },
80 { kRTypeMask | (0x1f << 6), 25, "multu", "ST", },
81 { kRTypeMask | (0x1f << 6), 26, "div", "ST", },
82 { kRTypeMask | (0x1f << 6), 27, "divu", "ST", },
83 { kRTypeMask | (0x1f << 6), 24 + (2 << 6), "mul", "DST", },
84 { kRTypeMask | (0x1f << 6), 24 + (3 << 6), "muh", "DST", },
85 { kRTypeMask | (0x1f << 6), 26 + (2 << 6), "div", "DST", },
86 { kRTypeMask | (0x1f << 6), 26 + (3 << 6), "mod", "DST", },
87 { kRTypeMask, 32, "add", "DST", },
88 { kRTypeMask, 33, "addu", "DST", },
89 { kRTypeMask, 34, "sub", "DST", },
90 { kRTypeMask, 35, "subu", "DST", },
91 { kRTypeMask, 36, "and", "DST", },
92 { kRTypeMask, 37, "or", "DST", },
93 { kRTypeMask, 38, "xor", "DST", },
94 { kRTypeMask, 39, "nor", "DST", },
95 { kRTypeMask, 42, "slt", "DST", },
96 { kRTypeMask, 43, "sltu", "DST", },
97 { kRTypeMask, 45, "daddu", "DST", },
98 { kRTypeMask, 46, "dsub", "DST", },
99 { kRTypeMask, 47, "dsubu", "DST", },
100 // TODO: tge[u], tlt[u], teg, tne
101 // TODO: seleqz, selnez
102 { kRTypeMask, 56, "dsll", "DTA", },
103 { kRTypeMask, 58, "dsrl", "DTA", },
104 { kRTypeMask, 59, "dsra", "DTA", },
105 { kRTypeMask, 60, "dsll32", "DTA", },
106 { kRTypeMask | (0x1f << 21), 62 | (1 << 21), "drotr32", "DTA", },
107 { kRTypeMask, 62, "dsrl32", "DTA", },
108 { kRTypeMask, 63, "dsra32", "DTA", },
109 { kRTypeMask, (31u << kOpcodeShift) | 3, "dext", "TSAZ", },
110 { kRTypeMask | (0x1f << 21) | (0x1f << 6), (31u << 26) | (16 << 6) | 32, "seb", "DT", },
111 { kRTypeMask | (0x1f << 21) | (0x1f << 6), (31u << 26) | (24 << 6) | 32, "seh", "DT", },
112
113 // SPECIAL0
114 { kSpecial0Mask | 0x7ff, (2 << 6) | 24, "mul", "DST" },
115 { kSpecial0Mask | 0x7ff, (3 << 6) | 24, "muh", "DST" },
116 { kSpecial0Mask | 0x7ff, (2 << 6) | 25, "mulu", "DST" },
117 { kSpecial0Mask | 0x7ff, (3 << 6) | 25, "muhu", "DST" },
118 { kSpecial0Mask | 0x7ff, (2 << 6) | 26, "div", "DST" },
119 { kSpecial0Mask | 0x7ff, (3 << 6) | 26, "mod", "DST" },
120 { kSpecial0Mask | 0x7ff, (2 << 6) | 27, "divu", "DST" },
121 { kSpecial0Mask | 0x7ff, (3 << 6) | 27, "modu", "DST" },
122 { kSpecial0Mask | 0x7ff, (2 << 6) | 28, "dmul", "DST" },
123 { kSpecial0Mask | 0x7ff, (3 << 6) | 28, "dmuh", "DST" },
124 { kSpecial0Mask | 0x7ff, (2 << 6) | 29, "dmulu", "DST" },
125 { kSpecial0Mask | 0x7ff, (3 << 6) | 29, "dmuhu", "DST" },
126 { kSpecial0Mask | 0x7ff, (2 << 6) | 30, "ddiv", "DST" },
127 { kSpecial0Mask | 0x7ff, (3 << 6) | 30, "dmod", "DST" },
128 { kSpecial0Mask | 0x7ff, (2 << 6) | 31, "ddivu", "DST" },
129 { kSpecial0Mask | 0x7ff, (3 << 6) | 31, "dmodu", "DST" },
130 // TODO: [d]clz, [d]clo
131 // TODO: sdbbp
132
133 // SPECIAL2
134 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 2, "mul", "DST" },
135 { kSpecial2Mask | 0x7ff, (28 << kOpcodeShift) | 32, "clz", "DS" },
136 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 0, "madd", "ST" },
137 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 1, "maddu", "ST" },
138 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 2, "mul", "DST" },
139 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 4, "msub", "ST" },
140 { kSpecial2Mask | 0xffff, (28 << kOpcodeShift) | 5, "msubu", "ST" },
141 { kSpecial2Mask | 0x3f, (28 << kOpcodeShift) | 0x3f, "sdbbp", "" }, // TODO: code
142
143 // J-type instructions.
144 { kJTypeMask, 2 << kOpcodeShift, "j", "L" },
145 { kJTypeMask, 3 << kOpcodeShift, "jal", "L" },
146
147 // I-type instructions.
148 { kITypeMask, 4 << kOpcodeShift, "beq", "STB" },
149 { kITypeMask, 5 << kOpcodeShift, "bne", "STB" },
150 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (1 << 16), "bgez", "SB" },
151 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (0 << 16), "bltz", "SB" },
152 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (2 << 16), "bltzl", "SB" },
153 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (16 << 16), "bltzal", "SB" },
154 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (18 << 16), "bltzall", "SB" },
155 { kITypeMask | (0x1f << 16), 6 << kOpcodeShift | (0 << 16), "blez", "SB" },
156 { kITypeMask, 6 << kOpcodeShift, "bgeuc", "STB" },
157 { kITypeMask | (0x1f << 16), 7 << kOpcodeShift | (0 << 16), "bgtz", "SB" },
158 { kITypeMask, 7 << kOpcodeShift, "bltuc", "STB" },
159 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (6 << 16), "dahi", "Si", },
160 { kITypeMask | (0x1f << 16), 1 << kOpcodeShift | (30 << 16), "dati", "Si", },
161
162 { 0xffff0000, (4 << kOpcodeShift), "b", "B" },
163 { 0xffff0000, (1 << kOpcodeShift) | (17 << 16), "bal", "B" },
164
165 { kITypeMask, 8 << kOpcodeShift, "beqc", "STB" },
166
167 { kITypeMask, 8 << kOpcodeShift, "addi", "TSi", },
168 { kITypeMask, 9 << kOpcodeShift, "addiu", "TSi", },
169 { kITypeMask, 10 << kOpcodeShift, "slti", "TSi", },
170 { kITypeMask, 11 << kOpcodeShift, "sltiu", "TSi", },
171 { kITypeMask, 12 << kOpcodeShift, "andi", "TSi", },
172 { kITypeMask, 13 << kOpcodeShift, "ori", "TSi", },
173 { kITypeMask, 14 << kOpcodeShift, "xori", "TSi", },
174 { kITypeMask | (0x1f << 21), 15 << kOpcodeShift, "lui", "TI", },
175 { kITypeMask, 15 << kOpcodeShift, "aui", "TSI", },
176
177 { kITypeMask | (0x1f << 21), 22 << kOpcodeShift, "blezc", "TB" },
178
179 // TODO: de-dup
180 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (1 << 21) | (1 << 16), "bgezc", "TB" },
181 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (2 << 21) | (2 << 16), "bgezc", "TB" },
182 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (3 << 21) | (3 << 16), "bgezc", "TB" },
183 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (4 << 21) | (4 << 16), "bgezc", "TB" },
184 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (5 << 21) | (5 << 16), "bgezc", "TB" },
185 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (6 << 21) | (6 << 16), "bgezc", "TB" },
186 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (7 << 21) | (7 << 16), "bgezc", "TB" },
187 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (8 << 21) | (8 << 16), "bgezc", "TB" },
188 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (9 << 21) | (9 << 16), "bgezc", "TB" },
189 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (10 << 21) | (10 << 16), "bgezc", "TB" },
190 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (11 << 21) | (11 << 16), "bgezc", "TB" },
191 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (12 << 21) | (12 << 16), "bgezc", "TB" },
192 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (13 << 21) | (13 << 16), "bgezc", "TB" },
193 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (14 << 21) | (14 << 16), "bgezc", "TB" },
194 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (15 << 21) | (15 << 16), "bgezc", "TB" },
195 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (16 << 21) | (16 << 16), "bgezc", "TB" },
196 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (17 << 21) | (17 << 16), "bgezc", "TB" },
197 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (18 << 21) | (18 << 16), "bgezc", "TB" },
198 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (19 << 21) | (19 << 16), "bgezc", "TB" },
199 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (20 << 21) | (20 << 16), "bgezc", "TB" },
200 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (21 << 21) | (21 << 16), "bgezc", "TB" },
201 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (22 << 21) | (22 << 16), "bgezc", "TB" },
202 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (23 << 21) | (23 << 16), "bgezc", "TB" },
203 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (24 << 21) | (24 << 16), "bgezc", "TB" },
204 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (25 << 21) | (25 << 16), "bgezc", "TB" },
205 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (26 << 21) | (26 << 16), "bgezc", "TB" },
206 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (27 << 21) | (27 << 16), "bgezc", "TB" },
207 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (28 << 21) | (28 << 16), "bgezc", "TB" },
208 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (29 << 21) | (29 << 16), "bgezc", "TB" },
209 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (30 << 21) | (30 << 16), "bgezc", "TB" },
210 { kITypeMask | (0x3ff << 16), (22 << kOpcodeShift) | (31 << 21) | (31 << 16), "bgezc", "TB" },
211
212 { kITypeMask, 22 << kOpcodeShift, "bgec", "STB" },
213
214 { kITypeMask | (0x1f << 21), 23 << kOpcodeShift, "bgtzc", "TB" },
215
216 // TODO: de-dup
217 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (1 << 21) | (1 << 16), "bltzc", "TB" },
218 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (2 << 21) | (2 << 16), "bltzc", "TB" },
219 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (3 << 21) | (3 << 16), "bltzc", "TB" },
220 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (4 << 21) | (4 << 16), "bltzc", "TB" },
221 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (5 << 21) | (5 << 16), "bltzc", "TB" },
222 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (6 << 21) | (6 << 16), "bltzc", "TB" },
223 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (7 << 21) | (7 << 16), "bltzc", "TB" },
224 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (8 << 21) | (8 << 16), "bltzc", "TB" },
225 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (9 << 21) | (9 << 16), "bltzc", "TB" },
226 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (10 << 21) | (10 << 16), "bltzc", "TB" },
227 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (11 << 21) | (11 << 16), "bltzc", "TB" },
228 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (12 << 21) | (12 << 16), "bltzc", "TB" },
229 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (13 << 21) | (13 << 16), "bltzc", "TB" },
230 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (14 << 21) | (14 << 16), "bltzc", "TB" },
231 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (15 << 21) | (15 << 16), "bltzc", "TB" },
232 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (16 << 21) | (16 << 16), "bltzc", "TB" },
233 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (17 << 21) | (17 << 16), "bltzc", "TB" },
234 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (18 << 21) | (18 << 16), "bltzc", "TB" },
235 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (19 << 21) | (19 << 16), "bltzc", "TB" },
236 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (20 << 21) | (20 << 16), "bltzc", "TB" },
237 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (21 << 21) | (21 << 16), "bltzc", "TB" },
238 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (22 << 21) | (22 << 16), "bltzc", "TB" },
239 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (23 << 21) | (23 << 16), "bltzc", "TB" },
240 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (24 << 21) | (24 << 16), "bltzc", "TB" },
241 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (25 << 21) | (25 << 16), "bltzc", "TB" },
242 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (26 << 21) | (26 << 16), "bltzc", "TB" },
243 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (27 << 21) | (27 << 16), "bltzc", "TB" },
244 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (28 << 21) | (28 << 16), "bltzc", "TB" },
245 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (29 << 21) | (29 << 16), "bltzc", "TB" },
246 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (30 << 21) | (30 << 16), "bltzc", "TB" },
247 { kITypeMask | (0x3ff << 16), (23 << kOpcodeShift) | (31 << 21) | (31 << 16), "bltzc", "TB" },
248
249 { kITypeMask, 23 << kOpcodeShift, "bltc", "STB" },
250
251 { kITypeMask, 24 << kOpcodeShift, "bnec", "STB" },
252
253 { kITypeMask, 25 << kOpcodeShift, "daddiu", "TSi", },
254 { kITypeMask, 29 << kOpcodeShift, "daui", "TSi", },
255
256 { kITypeMask, 32u << kOpcodeShift, "lb", "TO", },
257 { kITypeMask, 33u << kOpcodeShift, "lh", "TO", },
258 { kITypeMask, 35u << kOpcodeShift, "lw", "TO", },
259 { kITypeMask, 36u << kOpcodeShift, "lbu", "TO", },
260 { kITypeMask, 37u << kOpcodeShift, "lhu", "TO", },
261 { kITypeMask, 39u << kOpcodeShift, "lwu", "TO", },
262 { kITypeMask, 40u << kOpcodeShift, "sb", "TO", },
263 { kITypeMask, 41u << kOpcodeShift, "sh", "TO", },
264 { kITypeMask, 43u << kOpcodeShift, "sw", "TO", },
265 { kITypeMask, 49u << kOpcodeShift, "lwc1", "tO", },
266 { kITypeMask, 53u << kOpcodeShift, "ldc1", "tO", },
267 { kITypeMask | (0x1f << 21), 54u << kOpcodeShift, "jic", "Ti" },
268 { kITypeMask | (1 << 21), (54u << kOpcodeShift) | (1 << 21), "beqzc", "Sb" }, // TODO: de-dup?
269 { kITypeMask | (1 << 22), (54u << kOpcodeShift) | (1 << 22), "beqzc", "Sb" },
270 { kITypeMask | (1 << 23), (54u << kOpcodeShift) | (1 << 23), "beqzc", "Sb" },
271 { kITypeMask | (1 << 24), (54u << kOpcodeShift) | (1 << 24), "beqzc", "Sb" },
272 { kITypeMask | (1 << 25), (54u << kOpcodeShift) | (1 << 25), "beqzc", "Sb" },
273 { kITypeMask, 55u << kOpcodeShift, "ld", "TO", },
274 { kITypeMask, 57u << kOpcodeShift, "swc1", "tO", },
275 { kITypeMask | (0x1f << 16), (59u << kOpcodeShift) | (30 << 16), "auipc", "Si" },
276 { kITypeMask, 61u << kOpcodeShift, "sdc1", "tO", },
277 { kITypeMask | (0x1f << 21), 62u << kOpcodeShift, "jialc", "Ti" },
278 { kITypeMask | (1 << 21), (62u << kOpcodeShift) | (1 << 21), "bnezc", "Sb" }, // TODO: de-dup?
279 { kITypeMask | (1 << 22), (62u << kOpcodeShift) | (1 << 22), "bnezc", "Sb" },
280 { kITypeMask | (1 << 23), (62u << kOpcodeShift) | (1 << 23), "bnezc", "Sb" },
281 { kITypeMask | (1 << 24), (62u << kOpcodeShift) | (1 << 24), "bnezc", "Sb" },
282 { kITypeMask | (1 << 25), (62u << kOpcodeShift) | (1 << 25), "bnezc", "Sb" },
283 { kITypeMask, 63u << kOpcodeShift, "sd", "TO", },
284
285 // Floating point.
286 { kFpMask | (0x1f << 21), kCop1 | (0x00 << 21), "mfc1", "Td" },
287 { kFpMask | (0x1f << 21), kCop1 | (0x01 << 21), "dmfc1", "Td" },
288 { kFpMask | (0x1f << 21), kCop1 | (0x03 << 21), "mfhc1", "Td" },
289 { kFpMask | (0x1f << 21), kCop1 | (0x04 << 21), "mtc1", "Td" },
290 { kFpMask | (0x1f << 21), kCop1 | (0x05 << 21), "dmtc1", "Td" },
291 { kFpMask | (0x1f << 21), kCop1 | (0x07 << 21), "mthc1", "Td" },
292 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 0, "add", "fadt" },
293 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 1, "sub", "fadt" },
294 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 2, "mul", "fadt" },
295 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 3, "div", "fadt" },
296 { kFpMask | (0x10 << 21), kCop1 | (0x10 << 21) | 4, "sqrt", "fad" },
297 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 5, "abs", "fad" },
298 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 6, "mov", "fad" },
299 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 7, "neg", "fad" },
300 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 8, "round.l", "fad" },
301 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 9, "trunc.l", "fad" },
302 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 10, "ceil.l", "fad" },
303 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 11, "floor.l", "fad" },
304 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 12, "round.w", "fad" },
305 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 13, "trunc.w", "fad" },
306 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 14, "ceil.w", "fad" },
307 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 15, "floor.w", "fad" },
308 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 32, "cvt.s", "fad" },
309 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 33, "cvt.d", "fad" },
310 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 36, "cvt.w", "fad" },
311 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 37, "cvt.l", "fad" },
312 { kFpMask | (0x21f << 16), kCop1 | (0x200 << 16) | 38, "cvt.ps", "fad" },
313 };
314
ReadU32(const uint8_t * ptr)315 static uint32_t ReadU32(const uint8_t* ptr) {
316 // We only support little-endian MIPS.
317 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
318 }
319
Dump(std::ostream & os,const uint8_t * instr_ptr)320 size_t DisassemblerMips::Dump(std::ostream& os, const uint8_t* instr_ptr) {
321 uint32_t instruction = ReadU32(instr_ptr);
322
323 uint32_t rs = (instruction >> 21) & 0x1f; // I-type, R-type.
324 uint32_t rt = (instruction >> 16) & 0x1f; // I-type, R-type.
325 uint32_t rd = (instruction >> 11) & 0x1f; // R-type.
326 uint32_t sa = (instruction >> 6) & 0x1f; // R-type.
327
328 std::string opcode;
329 std::ostringstream args;
330
331 // TODO: remove this!
332 uint32_t op = (instruction >> 26) & 0x3f;
333 uint32_t function = (instruction & 0x3f); // R-type.
334 opcode = StringPrintf("op=%d fn=%d", op, function);
335
336 for (size_t i = 0; i < arraysize(gMipsInstructions); ++i) {
337 if (gMipsInstructions[i].Matches(instruction)) {
338 opcode = gMipsInstructions[i].name;
339 for (const char* args_fmt = gMipsInstructions[i].args_fmt; *args_fmt; ++args_fmt) {
340 switch (*args_fmt) {
341 case 'A': // sa (shift amount or [d]ext position).
342 args << sa;
343 break;
344 case 'B': // Branch offset.
345 {
346 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
347 offset <<= 2;
348 offset += 4; // Delay slot.
349 args << FormatInstructionPointer(instr_ptr + offset)
350 << StringPrintf(" ; %+d", offset);
351 }
352 break;
353 case 'b': // 21-bit branch offset.
354 {
355 int32_t offset = (instruction & 0x1fffff) - ((instruction & 0x100000) << 1);
356 offset <<= 2;
357 offset += 4; // Delay slot.
358 args << FormatInstructionPointer(instr_ptr + offset)
359 << StringPrintf(" ; %+d", offset);
360 }
361 break;
362 case 'D': args << 'r' << rd; break;
363 case 'd': args << 'f' << rd; break;
364 case 'a': args << 'f' << sa; break;
365 case 'f': // Floating point "fmt".
366 {
367 size_t fmt = (instruction >> 21) & 0x7; // TODO: other fmts?
368 switch (fmt) {
369 case 0: opcode += ".s"; break;
370 case 1: opcode += ".d"; break;
371 case 4: opcode += ".w"; break;
372 case 5: opcode += ".l"; break;
373 case 6: opcode += ".ps"; break;
374 default: opcode += ".?"; break;
375 }
376 continue; // No ", ".
377 }
378 case 'I': // Upper 16-bit immediate.
379 args << reinterpret_cast<void*>((instruction & 0xffff) << 16);
380 break;
381 case 'i': // Sign-extended lower 16-bit immediate.
382 args << static_cast<int16_t>(instruction & 0xffff);
383 break;
384 case 'L': // Jump label.
385 {
386 // TODO: is this right?
387 uint32_t instr_index = (instruction & 0x1ffffff);
388 uint32_t target = (instr_index << 2);
389 target |= (reinterpret_cast<uintptr_t>(instr_ptr + 4) & 0xf0000000);
390 args << reinterpret_cast<void*>(target);
391 }
392 break;
393 case 'O': // +x(rs)
394 {
395 int32_t offset = static_cast<int16_t>(instruction & 0xffff);
396 args << StringPrintf("%+d(r%d)", offset, rs);
397 if (rs == 17) {
398 args << " ; ";
399 if (is64bit_) {
400 Thread::DumpThreadOffset<8>(args, offset);
401 } else {
402 Thread::DumpThreadOffset<4>(args, offset);
403 }
404 }
405 }
406 break;
407 case 'S': args << 'r' << rs; break;
408 case 's': args << 'f' << rs; break;
409 case 'T': args << 'r' << rt; break;
410 case 't': args << 'f' << rt; break;
411 case 'Z': args << rd; break; // sz ([d]ext size).
412 }
413 if (*(args_fmt + 1)) {
414 args << ", ";
415 }
416 }
417 break;
418 }
419 }
420
421 // Special cases for sequences of:
422 // pc-relative +/- 2GB branch:
423 // auipc reg, imm
424 // jic reg, imm
425 // pc-relative +/- 2GB branch and link:
426 // auipc reg, imm
427 // daddiu reg, reg, imm
428 // jialc reg, 0
429 if (((op == 0x36 && rs == 0 && rt != 0) || // jic
430 (op == 0x19 && rs == rt && rt != 0)) && // daddiu
431 last_ptr_ && (intptr_t)instr_ptr - (intptr_t)last_ptr_ == 4 &&
432 (last_instr_ & 0xFC1F0000) == 0xEC1E0000 && // auipc
433 ((last_instr_ >> 21) & 0x1F) == rt) {
434 uint32_t offset = (last_instr_ << 16) | (instruction & 0xFFFF);
435 offset -= (offset & 0x8000) << 1;
436 offset -= 4;
437 if (op == 0x36) {
438 args << " ; b ";
439 } else {
440 args << " ; move r" << rt << ", ";
441 }
442 args << FormatInstructionPointer(instr_ptr + (int32_t)offset);
443 args << StringPrintf(" ; %+d", (int32_t)offset);
444 }
445
446 os << FormatInstructionPointer(instr_ptr)
447 << StringPrintf(": %08x\t%-7s ", instruction, opcode.c_str())
448 << args.str() << '\n';
449 last_ptr_ = instr_ptr;
450 last_instr_ = instruction;
451 return 4;
452 }
453
Dump(std::ostream & os,const uint8_t * begin,const uint8_t * end)454 void DisassemblerMips::Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) {
455 for (const uint8_t* cur = begin; cur < end; cur += 4) {
456 Dump(os, cur);
457 }
458 }
459
460 } // namespace mips
461 } // namespace art
462