Annotation of gforth/arch/mips/disasm.fs, revision 1.13

1.1       anton       1: \ disasm.fs    disassembler file (for MIPS R3000)
                      2: \
1.11      anton       3: \ Copyright (C) 2000 Free Software Foundation, Inc.
                      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
1.13    ! anton       9: \ as published by the Free Software Foundation, either version 3
1.11      anton      10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
1.13    ! anton      18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      19: 
1.4       anton      20: \ this disassembler is based on data from the R4400 manual
                     21: \ http://www.mips.com/Documentation/R4400_Uman_book_Ed2.pdf, in
1.6       anton      22: \ particular pages A3, A181, A182 (p. 471, 649, 650 in xpdf).
1.4       anton      23: \ it is limited to the R3000 (MIPS-I) architecture, though.
1.2       anton      24: 
1.7       anton      25: \ test this with
1.9       anton      26: \ gforth arch/mips/disasm.fs -e "here" arch/mips/testdisasm.fs -e "here over - disasm bye" |sed 's/([^)]*) //'|diff -u - arch/mips/testasm.fs
1.7       anton      27: 
1.8       anton      28: get-current
                     29: vocabulary disassembler
                     30: also disassembler definitions
                     31: 
1.4       anton      32: \ instruction fields
1.3       anton      33: 
                     34: : disasm-op ( w -- u )
                     35:     26 rshift ;
                     36: 
                     37: : disasm-rs ( w -- u )
                     38:     21 rshift $1F and ;
                     39: 
                     40: : disasm-rt ( w -- u )
                     41:     16 rshift $1f and ;
                     42: 
                     43: : disasm-rd ( w -- u )
                     44:     11 rshift $1f and ;
                     45: 
                     46: : disasm-shamt ( w -- u )
                     47:     \ shift amount field
                     48:     6 rshift $1f and ;
                     49: 
                     50: : disasm-funct ( w -- u )
                     51:     $3f and ;
                     52: 
                     53: : disasm-copz ( w -- u )
                     54:     disasm-op 3 and ;
                     55: 
1.5       anton      56: : disasm-uimm ( w -- u )
                     57:     $ffff and ;
                     58: 
1.3       anton      59: : disasm-imm ( w -- n )
1.5       anton      60:     disasm-uimm dup 15 rshift negate 15 lshift or ;
1.3       anton      61: 
                     62: : disasm-relative ( addr n -- w )
                     63:     \ compute printable form of relative address n relative to addr
1.6       anton      64:     2 lshift nip ( + ) ;
1.3       anton      65: 
1.4       anton      66: \ decode tables
                     67: 
                     68: : disasm-illegal ( addr w -- )
                     69:     \ disassemble illegal/unknown instruction w at addr
                     70:     hex. ." , ( illegal inst ) " drop ;
                     71: 
                     72: : disasm-table ( n "name" -- )
                     73:     \ initialize table with n entries with disasm-illegal
                     74:     create 0 ?do
                     75:        ['] disasm-illegal ,
                     76:     loop
                     77: does> ( u -- addr )
                     78:     swap cells + ;
                     79: 
                     80: $40 disasm-table opc-tab-entry     \ top-level decode table
                     81: $40 disasm-table funct-tab-entry   \ special function table
                     82: $20 disasm-table regimm-tab-entry  \ regim instructions rt table
                     83: $20 disasm-table copz-rs-tab-entry \ COPz instructions rs table
                     84: $20 disasm-table copz-rt-tab-entry \ COPz BC instructions rt table
                     85: $40 disasm-table cp0-tab-entry     \ COP0 CO instructions funct table
                     86: 
1.3       anton      87: \ disassembler central decode cascade
                     88: 
1.8       anton      89: dup set-current
                     90: 
1.3       anton      91: : disasm-inst ( addr w -- )
                     92:     \G disassemble instruction w at addr (addr is used for computing
                     93:     \G branch targets)
1.4       anton      94:     dup disasm-op opc-tab-entry @ execute ;
1.3       anton      95: 
1.9       anton      96: : disasm ( addr u -- ) \ gforth
1.3       anton      97:     \G disassemble u aus starting at addr
                     98:     bounds u+do
                     99:        cr ." ( " i hex. ." ) " i i @ disasm-inst
1.10      anton     100:        1 cells +loop
                    101:     cr ;
                    102: 
                    103: ' disasm IS discode
1.3       anton     104: 
1.8       anton     105: definitions
                    106: 
1.3       anton     107: : disasm-special ( addr w -- )
                    108:     \ disassemble inst with opcode special
1.4       anton     109:     dup disasm-funct funct-tab-entry @ execute ;
                    110: ' disasm-special 0 opc-tab-entry ! \ enter it for opcode special
1.3       anton     111: 
                    112: : disasm-regimm ( addr w -- )
                    113:     \ disassemble regimm inst
1.4       anton     114:     dup disasm-rt regimm-tab-entry @ execute ;
                    115: ' disasm-regimm 1 opc-tab-entry ! \ enter it for opcode regimm
1.3       anton     116: 
                    117: : disasm-copz-rs ( addr w -- )
                    118:     \ disassemble inst with opcode COPz
1.4       anton     119:     dup disasm-rs copz-rs-tab-entry @ execute ;
                    120: ' disasm-copz-rs $10 opc-tab-entry ! \ enter it for opcodes COPz
                    121: ' disasm-copz-rs $11 opc-tab-entry !
                    122: ' disasm-copz-rs $12 opc-tab-entry !
1.3       anton     123: 
                    124: : disasm-copz-rt ( addr w -- )
                    125:     \ disassemble inst with opcode COPz, rs=BC
1.4       anton     126:     dup disasm-rt copz-rt-tab-entry @ execute ;
                    127: ' disasm-copz-rt $08 copz-rs-tab-entry ! \ into COPz-table for rs=BC
1.3       anton     128: 
                    129: : disasm-cp0 ( addr w -- )
                    130:     \ disassemble inst with opcode COPz, rs=CO
1.4       anton     131:     dup disasm-funct cp0-tab-entry @ execute ;
                    132: ' disasm-cp0 $10 copz-rs-tab-entry ! \ into COPz-table for rs=CO
1.3       anton     133: 
1.4       anton     134: \ dummy words for insts.fs (words with these names are needed by asm.fs)
1.3       anton     135: 
                    136: : asm-op ( -- ) ;
1.4       anton     137: : asm-rs ( -- ) ;
                    138: : asm-rt ( -- ) ;
                    139: 
                    140: \ disassemble various formats
1.3       anton     141: 
                    142: : disasm-J-target ( addr w -- )
                    143:     \ print jump target
1.7       anton     144:     2 lshift $0fffffff and swap $f0000000 and or hex. ;
1.3       anton     145: 
                    146: : disasm-I-rs,rt,imm ( addr w -- )
                    147:     dup disasm-rs .
                    148:     dup disasm-rt .
                    149:     disasm-imm disasm-relative . ;
                    150: 
                    151: : disasm-I-rs,imm ( addr w -- )
                    152:     dup disasm-rs .
                    153:     disasm-imm disasm-relative . ;
                    154: 
                    155: : disasm-rt,rs,imm ( addr w -- )
                    156:     dup disasm-rt .
                    157:     dup disasm-rs .
                    158:     disasm-imm .
                    159:     drop ;
1.1       anton     160: 
1.5       anton     161: : disasm-rt,rs,uimm ( addr w -- )
1.3       anton     162:     dup disasm-rt .
1.5       anton     163:     dup disasm-rs .
                    164:     disasm-uimm hex.
                    165:     drop ;
                    166: 
                    167: : disasm-rt,uimm ( addr w -- )
                    168:     dup disasm-rt .
                    169:     disasm-uimm hex.
1.1       anton     170:     drop ;
                    171: 
1.3       anton     172: : disasm-rt,imm,rs ( addr w -- )
                    173:     dup disasm-rt .
                    174:     dup disasm-imm .
                    175:     dup disasm-rs .
                    176:     2drop ;
                    177: 
                    178: : disasm-rd,rt,sa ( addr w -- )
                    179:     dup disasm-rd .
                    180:     dup disasm-rt .
                    181:     dup disasm-shamt .
                    182:     2drop ;
                    183: 
                    184: : disasm-rd,rt,rs ( addr w -- )
                    185:     dup disasm-rd .
                    186:     dup disasm-rt .
                    187:     dup disasm-rs .
                    188:     2drop ;
                    189: 
                    190: : disasm-rs. ( addr w -- )
                    191:     dup disasm-rs .
                    192:     2drop ;
                    193: 
                    194: : disasm-rd,rs ( addr w -- )
                    195:     dup disasm-rd .
                    196:     dup disasm-rs .
                    197:     2drop ;
                    198: 
                    199: : disasm-rd. ( addr w -- )
                    200:     dup disasm-rd .
                    201:     2drop ;
                    202: 
                    203: : disasm-rs,rt ( addr w -- )
                    204:     dup disasm-rs .
                    205:     dup disasm-rt .
                    206:     2drop ;
                    207: 
                    208: : disasm-rd,rs,rt ( addr w -- )
                    209:     dup disasm-rd .
                    210:     dup disasm-rs .
                    211:     dup disasm-rt .
                    212:     2drop ;
                    213: 
                    214: : disasm-rt,rd,z ( addr w -- )
                    215:     dup disasm-rt .
                    216:     dup disasm-rd .
                    217:     dup disasm-copz .
                    218:     2drop ;
                    219: 
1.5       anton     220: : disasm-I-imm,z ( addr w -- )
                    221:     tuck disasm-imm disasm-relative .
                    222:     disasm-copz . ;
1.4       anton     223: 
                    224: \ meta-defining word for instruction format disassembling definitions
                    225: 
                    226: \ The following word defines instruction-format words, which in turn
                    227: \ define anonymous words for disassembling specific instructions and
                    228: \ put them in the appropriate decode table.
                    229: 
                    230: : define-format ( disasm-xt table-xt -- )
                    231:     \ define an instruction format that uses disasm-xt for
                    232:     \ disassembling and enters the defined instructions into table
                    233:     \ table-xt
                    234:     create 2,
                    235: does> ( u "inst" -- )
                    236:     \ defines an anonymous word for disassembling instruction inst,
                    237:     \ and enters it as u-th entry into table-xt
                    238:     2@ swap here name string, ( u table-xt disasm-xt c-addr ) \ remember string
                    239:     noname create 2,      \ define anonymous word
                    240:     execute lastxt swap ! \ enter xt of defined word into table-xt
                    241: does> ( addr w -- )
                    242:     \ disassemble instruction w at addr
                    243:     2@ >r ( addr w disasm-xt R: c-addr )
                    244:     execute ( R: c-addr ) \ disassemble operands
                    245:     r> count type ; \ print name 
                    246: 
                    247: \ all the following words have the stack effect ( u "name" )
                    248: ' disasm-J-target    ' opc-tab-entry    define-format asm-J-target
                    249: ' disasm-I-rs,rt,imm ' opc-tab-entry    define-format asm-I-rs,rt,imm
                    250: ' disasm-I-rs,imm    ' opc-tab-entry    define-format asm-I-rs,imm1
                    251: ' disasm-rt,rs,imm   ' opc-tab-entry    define-format asm-I-rt,rs,imm
1.5       anton     252: ' disasm-rt,rs,uimm   ' opc-tab-entry   define-format asm-I-rt,rs,uimm
                    253: ' disasm-rt,uimm      ' opc-tab-entry   define-format asm-I-rt,uimm
1.4       anton     254: ' disasm-rt,imm,rs   ' opc-tab-entry    define-format asm-I-rt,offset,rs
                    255: ' disasm-rd,rt,sa    ' funct-tab-entry          define-format asm-special-rd,rt,sa
                    256: ' disasm-rd,rt,rs    ' funct-tab-entry          define-format asm-special-rd,rt,rs
                    257: ' disasm-rs.         ' funct-tab-entry          define-format asm-special-rs
                    258: ' disasm-rd,rs       ' funct-tab-entry          define-format asm-special-rd,rs
                    259: ' 2drop              ' funct-tab-entry          define-format asm-special-nothing
                    260: ' disasm-rd.         ' funct-tab-entry          define-format asm-special-rd
                    261: ' disasm-rs,rt       ' funct-tab-entry          define-format asm-special-rs,rt
                    262: ' disasm-rd,rs,rt    ' funct-tab-entry          define-format asm-special-rd,rs,rt
                    263: ' disasm-I-rs,imm    ' regimm-tab-entry  define-format asm-regimm-rs,imm
                    264: ' 2drop              ' cp0-tab-entry     define-format asm-copz0
                    265: ' disasm-rt,rd,z     ' copz-rs-tab-entry define-format asm-copz-rt,rd1
1.5       anton     266: ' disasm-I-imm,z     ' copz-rt-tab-entry define-format asm-copz-imm1
1.4       anton     267: 
                    268: : asm-I-rs,imm ( u1 u2 "name" -- ; compiled code: addr w -- )
                    269:     nip asm-I-rs,imm1 ;
                    270: 
1.3       anton     271: : asm-copz-rt,rd ( u1 u2 "name" -- )
1.4       anton     272:     drop asm-copz-rt,rd1 ;
1.3       anton     273: 
1.4       anton     274: : asm-copz-rt,offset,rs ( u "name" -- )
                    275:     \ ignore these insts, we disassemble using  asm-I-rt,offset,rs
                    276:     drop name 2drop ;
1.3       anton     277: 
                    278: : asm-copz-imm ( u1 u2 u3 "name" -- )
1.4       anton     279:     drop nip asm-copz-imm1 ;
1.1       anton     280: 
1.3       anton     281: include ./insts.fs
1.8       anton     282: 
                    283: previous set-current

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>