-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmipsdis.rb
executable file
·283 lines (221 loc) · 4.45 KB
/
mipsdis.rb
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
#!/usr/bin/env ruby
# mipsdis is a simple ruby-based 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/rbgen.rb for more information.
$:.unshift(File.expand_path("../../disasm/rb", __FILE__))
require 'pp'
Signal.trap("PIPE", "EXIT")
# 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 and shift them. Ruby will return these as positve integers.
def field_m(op)
(op & 0xfc000000) >> 26
end
def field_u(op)
(op & 0x0000003f)
end
def field_s(op)
(op & 0x03e00000) >> 21
end
def field_t(op)
(op & 0x001f0000) >> 16
end
def field_o(op)
(op & 0x0000ffff)
end
def field_i(op)
(op & 0x0000ffff)
end
def field_b(op)
(op & 0x03e00000) >> 21
end
def field_p(op)
(op & 0x001f0000) >> 16
end
def field_f(op)
(op & 0x001f0000) >> 16
end
def field_d(op)
(op & 0x0000f800) >> 11
end
def field_a(op)
(op & 0x000007c0) >> 6
end
def field_c(op)
(op & 0x03ffffc0) >> 6
end
def field_y(op)
(op & 0x000007c0) >> 6
end
def field_e(op)
(op & 0x0000ffc0) >> 6
end
def field_n(op)
(op & 0x00000007)
end
def field_x(op)
(op & 0x03ffffff)
end
def field_z(op)
(op & 0x01ffffc0) >> 6
end
# Top 10 bits of syscode.
def field_bc1(op)
(op & 0x03ff0000) >> 16
end
# Bottom 10 bits of syscode.
def field_bc2(op)
(op & 0x0000ffc0) >> 6
end
# The getxxx functions interpret the results returned by field_xxx and any
# extra arithmetic or sign handling is done here
def getopcode(op)
field_m(op)
end
def getfunction(op)
field_u(op)
end
def getrt(op)
field_t(op)
end
def getrs(op)
field_s(op)
end
def getrd(op)
field_d(op)
end
# Compute the jump target address (used in j, jal).
def gettarget(pc, op)
x = field_x(op)
hibits = (pc + 4) & 0xf0000000
lobits = x << 2
(hibits | lobits)
end
# Compute the branch offset (used in beq, bne, etc).
def getbroff(pc, op)
# o is sign extended to 18 bits
o = field_o(op) << 2
m = 1 << (18 - 1)
r = (o ^ m) - m;
(pc + 4) + r
end
# Get the immediate field as a signed 16 bit quantity.
def getsimm(op)
o = field_o(op);
m = 1 << (16 - 1);
(o ^ m) - m;
end
def getimm(op)
field_i(op)
end
def getoffset(op)
getsimm(op)
end
def getbase(op)
field_b(op)
end
def getcacheop(op)
field_p(op)
end
def getprefhint(op)
field_f(op)
end
def getsa(op)
field_a(op)
end
def getbc1(op)
field_bc1(op)
end
def getbc2(op)
field_bc2(op)
end
def getsyscode(op)
field_c(op)
end
def getstype(op)
field_y(op)
end
def gettrapcode(op)
field_e(op)
end
def getsel(op)
field_n(op)
end
def getwaitcode(op)
field_z(op)
end
# Instruction validation functions.
# Returns true for OK, false otherwise.
def check_opcode(op, mask, value)
(op & mask) == value
end
def check_cl(rt, rd)
rt == rd
end
def check_jalr(rs, rd)
rs != rd
end
# Handle illegal and reserved opcodes.
def decode_unusable(pc, op)
return 'illegal'
end
def decode_reserved(pc, op)
return 'illegal'
end
require 'mips_dispatch.rb'
def decode(address, opcode)
decode_OPCODE(address, opcode)
end
# Determines if a string is a valid address.
def is_addr(s)
s.match(/^[0-9a-fA-F]+:$/) != nil
end
# Determines if a string is a valid hexword.
def is_hexword(s)
s.match(/^[0-9a-fA-F]{8}$/) != nil
end
# Convert a string containing hex chars to a positive integer.
def hex_to_u32(s)
s.hex
end
# For each line in the stream, pass it through the decoder and output
# address, opcode and disasm to stdout
def decode_stream(stream)
stream.each_line{|l|
toks = l.split(/[ \t\r]/)
next unless toks.length >= 2
addrtok, optok = toks
next unless is_addr(addrtok) && is_hexword(optok)
address = hex_to_u32(addrtok)
opcode = hex_to_u32(optok)
out = decode(address, opcode)
printf("%08x:\t%08x\t%s\n", address, opcode, out)
}
end
def main()
decode_stream(ARGF)
end
main()