File:  [gforth] / gforth / arch / mips / disasm.fs
Revision 1.13: download - view: text, annotated - select for diffs
Mon Dec 31 18:40:25 2007 UTC (14 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
updated copyright notices for GPL v3

    1: \ disasm.fs	disassembler file (for MIPS R3000)
    2: \
    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
    9: \ as published by the Free Software Foundation, either version 3
   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
   18: \ along with this program. If not, see http://www.gnu.org/licenses/.
   19: 
   20: \ this disassembler is based on data from the R4400 manual
   21: \ http://www.mips.com/Documentation/R4400_Uman_book_Ed2.pdf, in
   22: \ particular pages A3, A181, A182 (p. 471, 649, 650 in xpdf).
   23: \ it is limited to the R3000 (MIPS-I) architecture, though.
   24: 
   25: \ test this with
   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
   27: 
   28: get-current
   29: vocabulary disassembler
   30: also disassembler definitions
   31: 
   32: \ instruction fields
   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: 
   56: : disasm-uimm ( w -- u )
   57:     $ffff and ;
   58: 
   59: : disasm-imm ( w -- n )
   60:     disasm-uimm dup 15 rshift negate 15 lshift or ;
   61: 
   62: : disasm-relative ( addr n -- w )
   63:     \ compute printable form of relative address n relative to addr
   64:     2 lshift nip ( + ) ;
   65: 
   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: 
   87: \ disassembler central decode cascade
   88: 
   89: dup set-current
   90: 
   91: : disasm-inst ( addr w -- )
   92:     \G disassemble instruction w at addr (addr is used for computing
   93:     \G branch targets)
   94:     dup disasm-op opc-tab-entry @ execute ;
   95: 
   96: : disasm ( addr u -- ) \ gforth
   97:     \G disassemble u aus starting at addr
   98:     bounds u+do
   99: 	cr ." ( " i hex. ." ) " i i @ disasm-inst
  100: 	1 cells +loop
  101:     cr ;
  102: 
  103: ' disasm IS discode
  104: 
  105: definitions
  106: 
  107: : disasm-special ( addr w -- )
  108:     \ disassemble inst with opcode special
  109:     dup disasm-funct funct-tab-entry @ execute ;
  110: ' disasm-special 0 opc-tab-entry ! \ enter it for opcode special
  111: 
  112: : disasm-regimm ( addr w -- )
  113:     \ disassemble regimm inst
  114:     dup disasm-rt regimm-tab-entry @ execute ;
  115: ' disasm-regimm 1 opc-tab-entry ! \ enter it for opcode regimm
  116: 
  117: : disasm-copz-rs ( addr w -- )
  118:     \ disassemble inst with opcode COPz
  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 !
  123: 
  124: : disasm-copz-rt ( addr w -- )
  125:     \ disassemble inst with opcode COPz, rs=BC
  126:     dup disasm-rt copz-rt-tab-entry @ execute ;
  127: ' disasm-copz-rt $08 copz-rs-tab-entry ! \ into COPz-table for rs=BC
  128: 
  129: : disasm-cp0 ( addr w -- )
  130:     \ disassemble inst with opcode COPz, rs=CO
  131:     dup disasm-funct cp0-tab-entry @ execute ;
  132: ' disasm-cp0 $10 copz-rs-tab-entry ! \ into COPz-table for rs=CO
  133: 
  134: \ dummy words for insts.fs (words with these names are needed by asm.fs)
  135: 
  136: : asm-op ( -- ) ;
  137: : asm-rs ( -- ) ;
  138: : asm-rt ( -- ) ;
  139: 
  140: \ disassemble various formats
  141: 
  142: : disasm-J-target ( addr w -- )
  143:     \ print jump target
  144:     2 lshift $0fffffff and swap $f0000000 and or hex. ;
  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 ;
  160: 
  161: : disasm-rt,rs,uimm ( addr w -- )
  162:     dup disasm-rt .
  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.
  170:     drop ;
  171: 
  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: 
  220: : disasm-I-imm,z ( addr w -- )
  221:     tuck disasm-imm disasm-relative .
  222:     disasm-copz . ;
  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
  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
  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
  266: ' disasm-I-imm,z     ' copz-rt-tab-entry define-format asm-copz-imm1
  267: 
  268: : asm-I-rs,imm ( u1 u2 "name" -- ; compiled code: addr w -- )
  269:     nip asm-I-rs,imm1 ;
  270: 
  271: : asm-copz-rt,rd ( u1 u2 "name" -- )
  272:     drop asm-copz-rt,rd1 ;
  273: 
  274: : asm-copz-rt,offset,rs ( u "name" -- )
  275:     \ ignore these insts, we disassemble using  asm-I-rt,offset,rs
  276:     drop name 2drop ;
  277: 
  278: : asm-copz-imm ( u1 u2 u3 "name" -- )
  279:     drop nip asm-copz-imm1 ;
  280: 
  281: include ./insts.fs
  282: 
  283: previous set-current

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