File: disasm.fs

package info (click to toggle)
gforth 0.7.0%2Bds2-0.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,808 kB
  • sloc: ansic: 8,506; sh: 3,660; lisp: 1,783; makefile: 993; yacc: 186; sed: 141; lex: 102; awk: 21
file content (283 lines) | stat: -rw-r--r-- 8,420 bytes parent folder | download | duplicates (5)
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
\ disasm.fs	disassembler file (for MIPS R3000)
\
\ Copyright (C) 2000,2007 Free Software Foundation, Inc.

\ This file is part of Gforth.

\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.

\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
\ GNU General Public License for more details.

\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.

\ this disassembler is based on data from the R4400 manual
\ http://www.mips.com/Documentation/R4400_Uman_book_Ed2.pdf, in
\ particular pages A3, A181, A182 (p. 471, 649, 650 in xpdf).
\ it is limited to the R3000 (MIPS-I) architecture, though.

\ test this with
\ gforth arch/mips/disasm.fs -e "here" arch/mips/testdisasm.fs -e "here over - disasm bye" |sed 's/([^)]*) //'|diff -u - arch/mips/testasm.fs

get-current
vocabulary disassembler
also disassembler definitions

\ instruction fields

: disasm-op ( w -- u )
    26 rshift ;

: disasm-rs ( w -- u )
    21 rshift $1F and ;

: disasm-rt ( w -- u )
    16 rshift $1f and ;

: disasm-rd ( w -- u )
    11 rshift $1f and ;

: disasm-shamt ( w -- u )
    \ shift amount field
    6 rshift $1f and ;

: disasm-funct ( w -- u )
    $3f and ;

: disasm-copz ( w -- u )
    disasm-op 3 and ;

: disasm-uimm ( w -- u )
    $ffff and ;

: disasm-imm ( w -- n )
    disasm-uimm dup 15 rshift negate 15 lshift or ;

: disasm-relative ( addr n -- w )
    \ compute printable form of relative address n relative to addr
    2 lshift nip ( + ) ;

\ decode tables

: disasm-illegal ( addr w -- )
    \ disassemble illegal/unknown instruction w at addr
    hex. ." , ( illegal inst ) " drop ;

: disasm-table ( n "name" -- )
    \ initialize table with n entries with disasm-illegal
    create 0 ?do
	['] disasm-illegal ,
    loop
does> ( u -- addr )
    swap cells + ;

$40 disasm-table opc-tab-entry     \ top-level decode table
$40 disasm-table funct-tab-entry   \ special function table
$20 disasm-table regimm-tab-entry  \ regim instructions rt table
$20 disasm-table copz-rs-tab-entry \ COPz instructions rs table
$20 disasm-table copz-rt-tab-entry \ COPz BC instructions rt table
$40 disasm-table cp0-tab-entry     \ COP0 CO instructions funct table

\ disassembler central decode cascade

dup set-current

: disasm-inst ( addr w -- )
    \G disassemble instruction w at addr (addr is used for computing
    \G branch targets)
    dup disasm-op opc-tab-entry @ execute ;

: disasm ( addr u -- ) \ gforth
    \G disassemble u aus starting at addr
    bounds u+do
	cr ." ( " i hex. ." ) " i i @ disasm-inst
	1 cells +loop
    cr ;

' disasm IS discode

definitions

: disasm-special ( addr w -- )
    \ disassemble inst with opcode special
    dup disasm-funct funct-tab-entry @ execute ;
' disasm-special 0 opc-tab-entry ! \ enter it for opcode special

: disasm-regimm ( addr w -- )
    \ disassemble regimm inst
    dup disasm-rt regimm-tab-entry @ execute ;
' disasm-regimm 1 opc-tab-entry ! \ enter it for opcode regimm

: disasm-copz-rs ( addr w -- )
    \ disassemble inst with opcode COPz
    dup disasm-rs copz-rs-tab-entry @ execute ;
' disasm-copz-rs $10 opc-tab-entry ! \ enter it for opcodes COPz
' disasm-copz-rs $11 opc-tab-entry !
' disasm-copz-rs $12 opc-tab-entry !

: disasm-copz-rt ( addr w -- )
    \ disassemble inst with opcode COPz, rs=BC
    dup disasm-rt copz-rt-tab-entry @ execute ;
' disasm-copz-rt $08 copz-rs-tab-entry ! \ into COPz-table for rs=BC

: disasm-cp0 ( addr w -- )
    \ disassemble inst with opcode COPz, rs=CO
    dup disasm-funct cp0-tab-entry @ execute ;
' disasm-cp0 $10 copz-rs-tab-entry ! \ into COPz-table for rs=CO

\ dummy words for insts.fs (words with these names are needed by asm.fs)

: asm-op ( -- ) ;
: asm-rs ( -- ) ;
: asm-rt ( -- ) ;

\ disassemble various formats

: disasm-J-target ( addr w -- )
    \ print jump target
    2 lshift $0fffffff and swap $f0000000 and or hex. ;

: disasm-I-rs,rt,imm ( addr w -- )
    dup disasm-rs .
    dup disasm-rt .
    disasm-imm disasm-relative . ;

: disasm-I-rs,imm ( addr w -- )
    dup disasm-rs .
    disasm-imm disasm-relative . ;

: disasm-rt,rs,imm ( addr w -- )
    dup disasm-rt .
    dup disasm-rs .
    disasm-imm .
    drop ;

: disasm-rt,rs,uimm ( addr w -- )
    dup disasm-rt .
    dup disasm-rs .
    disasm-uimm hex.
    drop ;

: disasm-rt,uimm ( addr w -- )
    dup disasm-rt .
    disasm-uimm hex.
    drop ;

: disasm-rt,imm,rs ( addr w -- )
    dup disasm-rt .
    dup disasm-imm .
    dup disasm-rs .
    2drop ;

: disasm-rd,rt,sa ( addr w -- )
    dup disasm-rd .
    dup disasm-rt .
    dup disasm-shamt .
    2drop ;

: disasm-rd,rt,rs ( addr w -- )
    dup disasm-rd .
    dup disasm-rt .
    dup disasm-rs .
    2drop ;

: disasm-rs. ( addr w -- )
    dup disasm-rs .
    2drop ;

: disasm-rd,rs ( addr w -- )
    dup disasm-rd .
    dup disasm-rs .
    2drop ;

: disasm-rd. ( addr w -- )
    dup disasm-rd .
    2drop ;

: disasm-rs,rt ( addr w -- )
    dup disasm-rs .
    dup disasm-rt .
    2drop ;

: disasm-rd,rs,rt ( addr w -- )
    dup disasm-rd .
    dup disasm-rs .
    dup disasm-rt .
    2drop ;

: disasm-rt,rd,z ( addr w -- )
    dup disasm-rt .
    dup disasm-rd .
    dup disasm-copz .
    2drop ;

: disasm-I-imm,z ( addr w -- )
    tuck disasm-imm disasm-relative .
    disasm-copz . ;

\ meta-defining word for instruction format disassembling definitions

\ The following word defines instruction-format words, which in turn
\ define anonymous words for disassembling specific instructions and
\ put them in the appropriate decode table.

: define-format ( disasm-xt table-xt -- )
    \ define an instruction format that uses disasm-xt for
    \ disassembling and enters the defined instructions into table
    \ table-xt
    create 2,
does> ( u "inst" -- )
    \ defines an anonymous word for disassembling instruction inst,
    \ and enters it as u-th entry into table-xt
    2@ swap here name string, ( u table-xt disasm-xt c-addr ) \ remember string
    noname create 2,      \ define anonymous word
    execute lastxt swap ! \ enter xt of defined word into table-xt
does> ( addr w -- )
    \ disassemble instruction w at addr
    2@ >r ( addr w disasm-xt R: c-addr )
    execute ( R: c-addr ) \ disassemble operands
    r> count type ; \ print name 

\ all the following words have the stack effect ( u "name" )
' disasm-J-target    ' opc-tab-entry 	 define-format asm-J-target
' disasm-I-rs,rt,imm ' opc-tab-entry 	 define-format asm-I-rs,rt,imm
' disasm-I-rs,imm    ' opc-tab-entry 	 define-format asm-I-rs,imm1
' disasm-rt,rs,imm   ' opc-tab-entry 	 define-format asm-I-rt,rs,imm
' disasm-rt,rs,uimm   ' opc-tab-entry 	 define-format asm-I-rt,rs,uimm
' disasm-rt,uimm      ' opc-tab-entry 	 define-format asm-I-rt,uimm
' disasm-rt,imm,rs   ' opc-tab-entry 	 define-format asm-I-rt,offset,rs
' disasm-rd,rt,sa    ' funct-tab-entry 	 define-format asm-special-rd,rt,sa
' disasm-rd,rt,rs    ' funct-tab-entry 	 define-format asm-special-rd,rt,rs
' disasm-rs.         ' funct-tab-entry 	 define-format asm-special-rs
' disasm-rd,rs       ' funct-tab-entry 	 define-format asm-special-rd,rs
' 2drop              ' funct-tab-entry 	 define-format asm-special-nothing
' disasm-rd.         ' funct-tab-entry 	 define-format asm-special-rd
' disasm-rs,rt       ' funct-tab-entry 	 define-format asm-special-rs,rt
' disasm-rd,rs,rt    ' funct-tab-entry 	 define-format asm-special-rd,rs,rt
' disasm-I-rs,imm    ' regimm-tab-entry  define-format asm-regimm-rs,imm
' 2drop              ' cp0-tab-entry     define-format asm-copz0
' disasm-rt,rd,z     ' copz-rs-tab-entry define-format asm-copz-rt,rd1
' disasm-I-imm,z     ' copz-rt-tab-entry define-format asm-copz-imm1

: asm-I-rs,imm ( u1 u2 "name" -- ; compiled code: addr w -- )
    nip asm-I-rs,imm1 ;

: asm-copz-rt,rd ( u1 u2 "name" -- )
    drop asm-copz-rt,rd1 ;

: asm-copz-rt,offset,rs ( u "name" -- )
    \ ignore these insts, we disassemble using  asm-I-rt,offset,rs
    drop name 2drop ;

: asm-copz-imm ( u1 u2 u3 "name" -- )
    drop nip asm-copz-imm1 ;

include ./insts.fs

previous set-current