-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmipsdis.c
More file actions
438 lines (355 loc) · 8 KB
/
mipsdis.c
File metadata and controls
438 lines (355 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
* mipsdis is a simple command line MIPS disassembler. The core dispatch and
* decoding routines are autogenerated by mipsgen.
*
* usage:
* mipsdis input.txt
*
* The expected input format is the first two columns from objdump:
*
* hex_addr hex_opcode
* 80001678: 27bd0008
*
* The decoder will ignore any extra columns, and any lines not matching the
* above format.
*
* The output format is similar to objdump output:
*
* hex_addr hex_opcode asm
* 80001678: 27bd0008 addiu $29,$29,8
*
* This is not a fully featured disassembler, as it lacks quite a few
* usability features, but it is sufficient for debugging and exercising the
* code generator.
*
* See codegen/cgen.rb for C specific information.
* See scripts/mipsgen.rb for the code generator framework.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
/*
* The field_xxx functions provide raw access to the bits denoted by code
* letter in the 'opcode_bits' file.
*
* They are not responsible for interpretation, they just pick out the right
* bits, shift them and return as unsigned.
*/
static uint32_t field_m(uint32_t op)
{
return (op & 0xfc000000) >> 26;
}
static uint32_t field_u(uint32_t op)
{
return (op & 0x0000003f);
}
static uint32_t field_s(uint32_t op)
{
return (op & 0x03e00000) >> 21;
}
static uint32_t field_t(uint32_t op)
{
return (op & 0x001f0000) >> 16;
}
static uint32_t field_o(uint32_t op)
{
return (op & 0x0000ffff);
}
static uint32_t field_i(uint32_t op)
{
return (op & 0x0000ffff);
}
static uint32_t field_b(uint32_t op)
{
return (op & 0x03e00000) >> 21;
}
static uint32_t field_p(uint32_t op)
{
return (op & 0x001f0000) >> 16;
}
static uint32_t field_f(uint32_t op)
{
return (op & 0x001f0000) >> 16;
}
static uint32_t field_d(uint32_t op)
{
return (op & 0x0000f800) >> 11;
}
static uint32_t field_a(uint32_t op)
{
return (op & 0x000007c0) >> 6;
}
static uint32_t field_c(uint32_t op)
{
return (op & 0x03ffffc0) >> 6;
}
static uint32_t field_y(uint32_t op)
{
return (op & 0x000007c0) >> 6;
}
static uint32_t field_e(uint32_t op)
{
return (op & 0x0000ffc0) >> 6;
}
static uint32_t field_n(uint32_t op)
{
return (op & 0x00000007);
}
static uint32_t field_x(uint32_t op)
{
return (op & 0x03ffffff);
}
static uint32_t field_z(uint32_t op)
{
return (op & 0x01ffffc0) >> 6;
}
/* Top 10 bits of syscode. */
static uint32_t field_bc1(uint32_t op)
{
return (op & 0x03ff0000) >> 16;
}
/* Bottom 10 bits of syscode. */
static uint32_t field_bc2(uint32_t op)
{
return (op & 0x0000ffc0) >> 6;
}
/*
* The getxxx functions interpret the results returned by field_xxx and any
* extra arithmetic or sign handling is done here
*/
static uint32_t getopcode(uint32_t op)
{
return field_m(op);
}
static uint32_t getfunction(uint32_t op)
{
return field_u(op);
}
static uint32_t getrt(uint32_t op)
{
return field_t(op);
}
static uint32_t getrs(uint32_t op)
{
return field_s(op);
}
static uint32_t getrd(uint32_t op)
{
return field_d(op);
}
/* Compute the jump target address (used in j, jal) */
static uint32_t gettarget(uint32_t pc, uint32_t op)
{
uint32_t x = field_x(op);
uint32_t hibits = (pc + 4) & 0xf0000000;
uint32_t lobits = x << 2;
return hibits | lobits;
}
/* compute the branch offset (used in beq, bne, etc) */
static uint32_t getbroff(uint32_t pc, uint32_t op)
{
int32_t o = (int32_t) (field_o(op) << 2);
/* from bithacks */
const int32_t m = 1U << (18 - 1);
int32_t r = (o ^ m) - m;
return (pc + 4) + (uint32_t) r;
}
static int32_t getsimm(uint32_t op)
{
return (int16_t) (field_i(op));
}
static uint32_t getimm(uint32_t op)
{
return field_i(op);
}
static int32_t getoffset(uint32_t op)
{
return getsimm(op);
}
static uint32_t getbase(uint32_t op)
{
return field_b(op);
}
static uint32_t getcacheop(uint32_t op)
{
return field_p(op);
}
static uint32_t getprefhint(uint32_t op)
{
return field_f(op);
}
static uint32_t getsa(uint32_t op)
{
return field_a(op);
}
static uint32_t getbc1(uint32_t op)
{
return field_bc1(op);
}
static uint32_t getbc2(uint32_t op)
{
return field_bc2(op);
}
static uint32_t getsyscode(uint32_t op)
{
return field_c(op);
}
static uint32_t getstype(uint32_t op)
{
return field_y(op);
}
static uint32_t gettrapcode(uint32_t op)
{
return field_e(op);
}
static uint32_t getsel(uint32_t op)
{
return field_n(op);
}
static uint32_t getwaitcode(uint32_t op)
{
return field_z(op);
}
/* Error handling functions. */
static void decode_illegal(char *outbuf, size_t n, uint32_t addr,
uint32_t opcode)
{
sprintf(outbuf, "illegal");
}
static void decode_unusable(char *outbuf, size_t n, uint32_t addr,
uint32_t opcode)
{
decode_illegal(outbuf, n, addr, opcode);
}
static void decode_reserved(char *outbuf, size_t n, uint32_t addr,
uint32_t opcode)
{
decode_illegal(outbuf, n, addr, opcode);
}
/*
* Instruction validation functions.
* Returns 1 for OK, 0 otherwise.
*/
static int check_opcode(uint32_t op, uint32_t mask, uint32_t value)
{
return (op & mask) == value;
}
static int check_cl(uint32_t rt, uint32_t rd)
{
return rt == rd;
}
static int check_jalr(uint32_t rs, uint32_t rd)
{
return rs != rd;
}
#include "mips_dispatch.h"
/* The entry point into the auto generated code. */
static void decode(char *out_buf, size_t n, uint32_t addr, uint32_t opcode)
{
decode_OPCODE(out_buf, n, addr, opcode);
}
/*
* Determines if the c string is a valid address.
* Returns 1 for true, 0 otherwise
*
* A regex matching this would look like this:
* [0-9a-f]+:
*/
static int is_addr(const char *s)
{
size_t n = strlen(s);
size_t i = 0;
if (n <= 1)
return 0;
n--;
for (i = 0; i < n; ++i) {
if (!isxdigit(s[i]))
return 0;
}
return 1;
}
/*
* Determines if the c string is a valid hexword.
* Returns 1 for yes, 0 otherwise.
*
* A regex matching this would look like this:
* [0-9a-f]{8}
*/
static int is_hexword(const char *s)
{
size_t n = strlen(s);
size_t i = 0;
if (n != 8)
return 0;
for (i = 0; i < n; ++i) {
if (!isxdigit(s[i]))
return 0;
}
return 1;
}
/* Convert the c_string containing hex chars to a u32. */
static uint32_t hex_to_u32(const char *s)
{
return (uint32_t) strtoul(s, 0, 16);
}
/*
* For each line of the given stream, call the decoder and print the
* input address, opcode and disasm to stdout.
*/
static void decode_stream(FILE * stream)
{
const char *sep = " \t\r\n";
char *lineptr = 0;
size_t n = 0;
char outbuf[64] = { 0 };
ssize_t rn = 0;
char *saveptr = 0;
char *addrtok = 0;
char *optok = 0;
uint32_t address = 0;
uint32_t opcode = 0;
while (1) {
rn = getline(&lineptr, &n, stream);
if (rn <= 0)
break;
addrtok = strtok_r(lineptr, sep, &saveptr);
optok = strtok_r(0, sep, &saveptr);
if (!addrtok || !optok)
continue;
if (!is_addr(addrtok) || !is_hexword(optok))
continue;
address = hex_to_u32(addrtok);
opcode = hex_to_u32(optok);
decode(outbuf, 64, address, opcode);
printf("%08x:\t%08x\t%s\n", address, opcode, outbuf);
}
free(lineptr);
}
/* Handle the command line options and handoff to decode_stream. */
int main(int argc, char **argv)
{
const char *filename = NULL;
FILE *stream = 0;
int do_close = 0;
if (argc != 2) {
fprintf(stderr, "usage: decoder file\n");
return 1;
}
filename = argv[1];
if (strcmp(filename, "-") == 0) {
stream = stdin;
} else {
stream = fopen(filename, "r");
if (!stream) {
fprintf(stderr, "could not open %s\n", filename);
return 1;
}
do_close = 1;
}
decode_stream(stream);
if (do_close)
fclose(stream);
return 0;
}