File:  [gforth] / gforth / arch / mips / disasm.fs
Revision 1.2: download - view: text, annotated - select for diffs
Sun May 28 08:32:55 2000 UTC (22 years, 4 months ago) by anton
Branches: MAIN
CVS tags: HEAD
some fixes to the MIPS disassembler

    1: \ disasm.fs	disassembler file (for MIPS R3000)
    2: \
    3: \ Copyright (C) 1995-97 Martin Anton Ertl, Christian Pirker
    4: \
    5: \ This file is part of RAFTS.
    6: \
    7: \	RAFTS 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 2
   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, write to the Free Software
   19: \	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   20: 
   21: require arch/mips/asm.fs
   22: 
   23: : hex.r ( u n -- )
   24:     drop hex. ;
   25: 
   26: : (disasm-op) ( code -- n )
   27:     $1a rshift $6 asm-bitmask and ;
   28: 
   29: : disasm-op ( code -- )
   30:     (disasm-op) 2 hex.r space ;
   31: 
   32: : (disasm-rs) ( code -- n )
   33:     $15 rshift $5 asm-bitmask and ;
   34: 
   35: : disasm-rs ( code -- )
   36:     (disasm-rs) 2 hex.r space ;
   37: 
   38: : (disasm-rt) ( code -- n )
   39:     $10 rshift $5 asm-bitmask and ;
   40: 
   41: : disasm-rt ( code -- )
   42:     (disasm-rt) 2 hex.r space ;
   43: 
   44: : (disasm-imm) ( code -- n )
   45:     $10 asm-bitmask and ;
   46: 
   47: : disasm-imm ( code -- )
   48:     (disasm-imm) 4 hex.r space ;
   49: 
   50: : disasm-addr ( addr code -- n )
   51:     (disasm-imm) $2 lshift asm-expand dup 4 hex.r space ." ( " + cell+ hex. ." ) " ;
   52: 
   53: : (disasm-target) ( code -- n )
   54:     $1a asm-bitmask and ;
   55: 
   56: : disasm-target ( code -- )
   57:     (disasm-target) $2 lshift or hex. ;
   58: 
   59: : (disasm-rd) ( code -- n )
   60:     $b rshift $5 asm-bitmask and ;
   61: 
   62: : disasm-rd ( code -- )
   63:     (disasm-rd) 2 hex.r space ;
   64: 
   65: : (disasm-shamt) ( code -- n )
   66:     $6 rshift $5 asm-bitmask and ;
   67: 
   68: : disasm-shamt ( code -- )
   69:     (disasm-shamt) 2 hex.r space ;
   70: 
   71: ' disasm-shamt alias disasm-sa
   72: 
   73: : disasm-funct ( code -- n )
   74:     $6 asm-bitmask and ;
   75: 
   76: \ ***** I-types
   77: : disasm-I-rt,imm ( addr -- )
   78:     @ dup disasm-rt disasm-imm ;
   79: 
   80: : disasm-I-rs,imm ( addr -- )
   81:     dup @ dup disasm-rs disasm-addr ;
   82: 
   83: : disasm-I-rt,rs,imm ( addr -- )
   84:     @ dup disasm-rt dup disasm-rs disasm-imm ;
   85: 
   86: : disasm-I-rs,rt,imm ( addr -- )
   87:     dup @ dup disasm-rs dup disasm-rt disasm-addr ;
   88: 
   89: : disasm-I-rt,offset,rs ( addr -- )
   90:     @ dup disasm-rt dup disasm-imm disasm-rs ;
   91: 
   92: \ ***** regimm types
   93: ' disasm-I-rs,imm alias disasm-regimm-rs,imm
   94: 
   95: \ ***** copz types 1
   96: : disasm-copz-imm ( addr -- )
   97:     dup @ dup disasm-addr disasm-op ;
   98: 
   99: : disasm-copz-rt,offset,rs,z ( addr -- )
  100:     @ dup disasm-rt dup disasm-imm
  101:     dup disasm-rs disasm-op ;
  102: 
  103: \ ***** J-types
  104: : disasm-J-target ( addr -- )
  105:     dup $fc000000 and swap @ disasm-target ;
  106: 
  107: \ ***** R-types
  108: : disasm-R-nothing ( addr -- )
  109:     @ hex. ;
  110: 
  111: : disasm-R-rd ( addr -- )
  112:     @ disasm-rd ;
  113: 
  114: : disasm-R-rs ( addr -- )
  115:     @ disasm-rs ;
  116: 
  117: : disasm-R-rd,rs ( addr -- )
  118:     @ dup disasm-rd disasm-rs ;
  119: 
  120: : disasm-R-rs,rt ( addr -- )
  121:     @ dup disasm-rs disasm-rt ;
  122: 
  123: : disasm-R-rd,rs,rt ( addr -- )
  124:     @ dup disasm-rd dup disasm-rs disasm-rt ;
  125: 
  126: : disasm-R-rd,rt,rs ( addr -- )
  127:     @ dup disasm-rd dup disasm-rt disasm-rs ;
  128: 
  129: : disasm-R-rd,rt,sa ( addr -- )
  130:     @ dup disasm-rd dup disasm-rt disasm-sa ;
  131: 
  132: \ ***** special types
  133: ' disasm-R-nothing	alias disasm-special-nothing
  134: ' disasm-R-rd		alias disasm-special-rd
  135: ' disasm-R-rs		alias disasm-special-rs
  136: ' disasm-R-rd,rs	alias disasm-special-rd,rs
  137: ' disasm-R-rs,rt	alias disasm-special-rs,rt
  138: ' disasm-R-rd,rs,rt	alias disasm-special-rd,rs,rt
  139: ' disasm-R-rd,rt,rs	alias disasm-special-rd,rt,rs
  140: ' disasm-R-rd,rt,sa	alias disasm-special-rd,rt,sa
  141: 
  142: \ ***** copz types 2
  143: : disasm-cop0 ( addr -- )
  144:     @ disasm-rs ;
  145: 
  146: : disasm-copz-rt,rd ( addr -- )
  147:     @ dup disasm-rt dup disasm-rd disasm-op ;
  148: 
  149: $40 2 matrix disasm-opc
  150: $40 2 matrix disasm-opc-special
  151: $20 2 matrix disasm-opc-regimm
  152: $20 2 matrix disasm-opc-copzrs
  153: $20 2 matrix disasm-opc-copzrt
  154: $40 2 matrix disasm-opc-cop0
  155: 
  156: : (disasm-print) ( addr n addr -- )
  157:     >r dup 1 r@ [ 1 -2 wword-regs-adjust ] execute @ rot swap [ 0 -1 wword-regs-adjust ] execute
  158:     0 r> [ 1 -2 wword-regs-adjust ] execute @ name. ;
  159: 
  160: : disasm-print ( addr -- )
  161:     dup @ if
  162: 	dup @ (disasm-op)
  163: 	dup 0 disasm-opc @ NIL <> if
  164: 	    ['] disasm-opc (disasm-print)
  165: 	else
  166: 	    1 disasm-opc @ [ 0 -1 wword-regs-adjust ] execute
  167: 	endif
  168:     else
  169: 	drop ['] nop, name.
  170:     endif ;
  171: 
  172: : disasm-dump ( addr count -- )
  173:     cr
  174:     over + swap ?do
  175: 	i ." ( " dup hex. ." , " dup @ hex. ." ) " disasm-print cr
  176:     4 +loop ;
  177: 
  178: : (disasm-gen) ( name func n addr -- )
  179:     >r tuck 1 r@ [ 1 -2 wword-regs-adjust ] execute !
  180:     0 r> [ 1 -2 wword-regs-adjust ] execute ! ;
  181: 
  182: : disasm-gen ( name func n -- )
  183:     ['] disasm-opc (disasm-gen) ;
  184: 
  185: : disasm-print-special ( addr -- )
  186:     dup @ disasm-funct ['] disasm-opc-special (disasm-print) ;
  187: 
  188: : disasm-gen-special ( name func n -- )
  189:     ['] disasm-opc-special (disasm-gen) ;
  190: 
  191: : disasm-print-regimm ( addr -- )
  192:     dup @ (disasm-rt) ['] disasm-opc-regimm (disasm-print) ;
  193: 
  194: : disasm-gen-regimm ( name func n -- )
  195:     ['] disasm-opc-regimm (disasm-gen) ;
  196: 
  197: : disasm-print-copzrs ( addr -- )
  198:     dup @ (disasm-rs)
  199:     dup 0 disasm-opc-copzrs @ NIL <> if
  200: 	['] disasm-opc-copzrs (disasm-print)
  201:     else
  202: 	1 disasm-opc-copzrs @
  203: 	[ 0 -1 wword-regs-adjust ]
  204: 	execute
  205:     endif ;
  206: 
  207: : disasm-gen-copzrs ( name func n -- )
  208:     ['] disasm-opc-copzrs (disasm-gen) ;
  209: 
  210: : disasm-print-copzrt ( addr -- )
  211:     dup @ (disasm-rt) ['] disasm-opc-copzrt (disasm-print) ;
  212: 
  213: : disasm-gen-copzrt ( name func n -- )
  214:     ['] disasm-opc-copzrt (disasm-gen) ;
  215: 
  216: : disasm-print-copzi ( addr -- )
  217:     dup @ (disasm-rs) ['] disasm-opc-copzrs (disasm-print) ;
  218: 
  219: : disasm-gen-copzi ( name func n -- )
  220:     >r 2dup r@ 1+ disasm-gen
  221:     2dup r@ 2 + disasm-gen
  222:     r> 3 + disasm-gen ;
  223: 
  224: : disasm-print-cop0 ( addr -- )
  225:     dup @ disasm-funct ['] disasm-opc-cop0 (disasm-print) ;
  226: 
  227: : disasm-gen-cop0 ( name func n -- )
  228:     ['] disasm-opc-cop0 (disasm-gen) ;
  229: 
  230: : illegal-code ( -- ) ;
  231: 
  232: : disasm-nop ( code -- )
  233:     @ 8 ." ( " hex.r space ." ) " ;
  234: 
  235: : disasm-init ( xt n -- )
  236:     0 ?do
  237: 	['] illegal-code ['] disasm-nop i 3 pick
  238: 	[ 0 -3 wword-regs-adjust ] execute
  239:     loop
  240:     drop ;
  241: ' disasm-gen $40 disasm-init
  242: ' disasm-gen-special $40 disasm-init
  243: ' disasm-gen-regimm $20 disasm-init
  244: ' disasm-gen-copzrs $20 disasm-init
  245: ' disasm-gen-copzrt $20 disasm-init
  246: ' disasm-gen-cop0 $40 disasm-init
  247: NIL ' disasm-print-special $00 disasm-gen
  248: NIL ' disasm-print-regimm $01 disasm-gen
  249: NIL ' disasm-print-cop0 $10 disasm-gen
  250: NIL ' disasm-print-copzrs $11 disasm-gen
  251: NIL ' disasm-print-copzrs $12 disasm-gen
  252: NIL ' disasm-print-copzrs $13 disasm-gen
  253: NIL ' disasm-print-copzrt asm-copz-BC disasm-gen-copzrs
  254: 
  255: ' beq,		' disasm-I-rs,rt,imm $04 disasm-gen
  256: ' bne,		' disasm-I-rs,rt,imm $05 disasm-gen
  257: ' blez,		' disasm-I-rs,imm $06 disasm-gen
  258: ' bgtz,		' disasm-I-rs,imm $07 disasm-gen
  259: ' addi,		' disasm-I-rt,rs,imm $08 disasm-gen
  260: ' addiu,	' disasm-I-rt,rs,imm $09 disasm-gen
  261: ' slti,		' disasm-I-rt,rs,imm $0a disasm-gen
  262: ' sltiu,	' disasm-I-rt,rs,imm $0b disasm-gen
  263: ' andi,		' disasm-I-rt,rs,imm $0c disasm-gen
  264: ' ori,		' disasm-I-rt,rs,imm $0d disasm-gen
  265: ' xori,		' disasm-I-rt,rs,imm $0e disasm-gen
  266: ' lui,		' disasm-I-rt,imm $0f disasm-gen
  267: ' lb,		' disasm-I-rt,offset,rs $20 disasm-gen
  268: ' lh,		' disasm-I-rt,offset,rs $21 disasm-gen
  269: ' lwl,		' disasm-I-rt,offset,rs $22 disasm-gen
  270: ' lw,		' disasm-I-rt,offset,rs $23 disasm-gen
  271: ' lbu,		' disasm-I-rt,offset,rs $24 disasm-gen
  272: ' lhu,		' disasm-I-rt,offset,rs $25 disasm-gen
  273: ' lwr,		' disasm-I-rt,offset,rs $26 disasm-gen
  274: ' sb,		' disasm-I-rt,offset,rs $28 disasm-gen
  275: ' sh,		' disasm-I-rt,offset,rs $29 disasm-gen
  276: ' swl,		' disasm-I-rt,offset,rs $2a disasm-gen
  277: ' sw,		' disasm-I-rt,offset,rs $2b disasm-gen
  278: ' swr,		' disasm-I-rt,offset,rs $2e disasm-gen
  279: 
  280: ' j,		' disasm-J-target $02 disasm-gen
  281: ' jal,		' disasm-J-target $03 disasm-gen
  282: 
  283: ' sll,		' disasm-special-rd,rt,sa $00 disasm-gen-special
  284: ' srl,		' disasm-special-rd,rt,sa $02 disasm-gen-special
  285: ' sra,		' disasm-special-rd,rt,sa $03 disasm-gen-special
  286: ' sllv,		' disasm-special-rd,rt,rs $04 disasm-gen-special
  287: ' srlv,		' disasm-special-rd,rt,rs $06 disasm-gen-special
  288: ' srav,		' disasm-special-rd,rt,rs $07 disasm-gen-special
  289: ' jr,		' disasm-special-rs $08 disasm-gen-special
  290: ' jalr,		' disasm-special-rd,rs $09 disasm-gen-special
  291: ' syscall,	' disasm-special-nothing $0c disasm-gen-special
  292: ' break,	' disasm-special-nothing $0d disasm-gen-special
  293: ' mfhi,		' disasm-special-rd $10 disasm-gen-special
  294: ' mthi,		' disasm-special-rs $11 disasm-gen-special
  295: ' mflo,		' disasm-special-rd $12 disasm-gen-special
  296: ' mtlo,		' disasm-special-rs $13 disasm-gen-special
  297: ' mult,		' disasm-special-rs,rt $18 disasm-gen-special
  298: ' multu,	' disasm-special-rs,rt $19 disasm-gen-special
  299: ' div,		' disasm-special-rs,rt $1a disasm-gen-special
  300: ' divu,		' disasm-special-rs,rt $1b disasm-gen-special
  301: ' add,		' disasm-special-rd,rs,rt $20 disasm-gen-special
  302: ' addu,		' disasm-special-rd,rs,rt $21 disasm-gen-special
  303: ' sub,		' disasm-special-rd,rs,rt $22 disasm-gen-special
  304: ' subu,		' disasm-special-rd,rs,rt $23 disasm-gen-special
  305: ' and,		' disasm-special-rd,rs,rt $24 disasm-gen-special
  306: ' or,		' disasm-special-rd,rs,rt $25 disasm-gen-special
  307: ' xor,		' disasm-special-rd,rs,rt $26 disasm-gen-special
  308: ' nor,		' disasm-special-rd,rs,rt $27 disasm-gen-special
  309: ' slt,		' disasm-special-rd,rs,rt $2a disasm-gen-special
  310: ' sltu,		' disasm-special-rd,rs,rt $2b disasm-gen-special
  311: 
  312: ' bltz,		' disasm-regimm-rs,imm $00 disasm-gen-regimm
  313: ' bgez,		' disasm-regimm-rs,imm $01 disasm-gen-regimm
  314: ' bltzal,	' disasm-regimm-rs,imm $10 disasm-gen-regimm
  315: ' bgezal,	' disasm-regimm-rs,imm $11 disasm-gen-regimm
  316: 
  317: ' lwcz,		' disasm-copz-rt,offset,rs,z $30 disasm-gen-copzi
  318: ' swcz,		' disasm-copz-rt,offset,rs,z $38 disasm-gen-copzi
  319: ' mfcz,		' disasm-copz-rt,rd asm-copz-MF disasm-gen-copzrs
  320: ' cfcz,		' disasm-copz-rt,rd asm-copz-CF disasm-gen-copzrs
  321: ' mtcz,		' disasm-copz-rt,rd asm-copz-MT disasm-gen-copzrs
  322: ' ctcz,		' disasm-copz-rt,rd asm-copz-CT disasm-gen-copzrs
  323: ' bczf,		' disasm-copz-imm asm-copz-BCF disasm-gen-copzrt
  324: ' bczt,		' disasm-copz-imm asm-copz-BCT disasm-gen-copzrt
  325: ' tlbr,		' disasm-cop0 $01 disasm-gen-cop0
  326: ' tlbwi,	' disasm-cop0 $02 disasm-gen-cop0
  327: ' tlbwr,	' disasm-cop0 $06 disasm-gen-cop0
  328: ' tlbl,		' disasm-cop0 $08 disasm-gen-cop0
  329: 
  330: ?test $0800 [IF]
  331: cr ." Test for disasm..fs" cr
  332: 
  333: : gen ( coden ... code0 n -- )
  334:     0 ?do
  335: 	a,
  336:     loop ;
  337: 
  338: here
  339: $00210820 $00000820 $00200020 $00010020 $03fff820 5 gen
  340: $20210001 $20010000 $20200000 $20000001 $23ffffff 5 gen
  341: $24210001 $24010000 $24200000 $24000001 $27ffffff 5 gen
  342: $00210821 $00000821 $00200021 $00010021 $03fff821 5 gen
  343: $00210824 $00000824 $00200024 $00010024 $03fff824 5 gen
  344: $30210001 $30010000 $30200000 $30000001 $33ffffff 5 gen
  345: $45000001 $4500ffff 2 gen
  346: $45010001 $4501ffff 2 gen
  347: $10210001 $10200000 $10010000 $10000001 $13ffffff 5 gen
  348: $04210001 $04210000 $04010001 $07e1ffff 4 gen
  349: $04310001 $04310000 $04110001 $07f1ffff 4 gen
  350: $1c200001 $1c200000 $1c000001 $1fe0ffff 4 gen
  351: $18200001 $18200000 $18000001 $1be0ffff 4 gen
  352: $04200001 $04200000 $04000001 $07e0ffff 4 gen
  353: $04300001 $04300000 $04100001 $07f0ffff 4 gen
  354: $14210001 $14200000 $14010000 $14000001 $17ffffff 5 gen
  355: $0000000d 1 gen
  356: $44410800 $44410000 $44400800 $445ff800 4 gen
  357: $44c10800 $44c10000 $44c00800 $44dff800 4 gen
  358: $0021001a $0020001a $0001001a $03ff001a 4 gen
  359: $0021001b $0020001b $0001001b $03ff001b 4 gen
  360: $08000001 $0bffffff 2 gen
  361: $0c000001 $0fffffff 2 gen
  362: $00200809 $00000809 $00200009 $03e0f809 4 gen
  363: $00200008 $03e00008 2 gen
  364: $80210001 $80010000 $80000001 $80200000 $83ffffff 5 gen
  365: $90210001 $90010000 $90000001 $90200000 $93ffffff 5 gen
  366: $84210001 $84010000 $84000001 $84200000 $87ffffff 5 gen
  367: $94210001 $94010000 $94000001 $94200000 $97ffffff 5 gen
  368: $3c010001 $3c010000 $3c000001 $3c1fffff 4 gen
  369: $8c210001 $8c010000 $8c000001 $8c200000 $8fffffff 5 gen
  370: $c4210001 $c4010000 $c4000001 $c4200000 $c7ffffff 5 gen
  371: $88210001 $88010000 $88000001 $88200000 $8bffffff 5 gen
  372: $98210001 $98010000 $98000001 $98200000 $9bffffff 5 gen
  373: $44010800 $44010000 $44000800 $441ff800 4 gen
  374: $00000810 $0000f810 2 gen
  375: $00000812 $0000f812 2 gen
  376: $44810800 $44810000 $44800800 $449ff800 4 gen
  377: $00200011 $03e00011 2 gen
  378: $00200013 $03e00013 2 gen
  379: $00210018 $00200018 $00010018 $03ff0018 4 gen
  380: $00210019 $00200019 $00010019 $03ff0019 4 gen
  381: $00210827 $00000827 $00200027 $00010027 $03fff827 5 gen
  382: $00210825 $00000825 $00200025 $00010025 $03fff825 5 gen
  383: $34210001 $34010000 $34200000 $34000001 $37ffffff 5 gen
  384: $a0210001 $a0010000 $a0000001 $a0200000 $a3ffffff 5 gen
  385: $a4210001 $a4010000 $a4000001 $a4200000 $a7ffffff 5 gen
  386: $0021082a $0000082a $0020002a $0001002a $03fff82a 5 gen
  387: $28210001 $28010000 $28200000 $28000001 $2bffffff 5 gen
  388: $2c210001 $2c010000 $2c200000 $2c000001 $2fffffff 5 gen
  389: $0021082b $0000082b $0020002b $0001002b $03fff82b 5 gen
  390: $00210822 $00000822 $00200022 $00010022 $03fff822 5 gen
  391: $00210823 $00000823 $00200023 $00010023 $03fff823 5 gen
  392: $ac210001 $ac010000 $ac000001 $ac200000 $afffffff 5 gen
  393: $e4210001 $e4010000 $e4000001 $e4200000 $e7ffffff 5 gen
  394: $a8210001 $a8010000 $a8000001 $a8200000 $abffffff 5 gen
  395: $b8210001 $b8010000 $b8000001 $b8200000 $bbffffff 5 gen
  396: $0000000c 1 gen
  397: $42000008 1 gen
  398: $42000001 1 gen
  399: $42000002 1 gen
  400: $42000006 1 gen
  401: $00210826 $00000826 $00200026 $00010026 $03fff826 5 gen
  402: $38210001 $38010000 $38200000 $38000001 $3bffffff 5 gen
  403: 
  404: $00200821 $00000821 $00200021 $03e0f821 4 gen
  405: $00010822 $00200821 $04210002 $00000822 $00000821 $04010002
  406: $00010022 $00200021 $04210002 $001ff822 $03e0f821 $07e10002 12 gen
  407: $00010822 $00000822 $00010022 $001ff822 4 gen
  408: $00010823 $00000823 $00010023 $001ff823 4 gen
  409: $00200827 $00000827 $00200027 $03e0f827 4 gen
  410: $14200001 $0021082a $14200000 $0020082a $14200000 $0001082a
  411: $14200001 $0000082a $1420ffff $03ff082a 10 gen
  412: $10200001 $0021082a $10200000 $0001082a $10200000 $0020082a
  413: $10200001 $0000082a $1020ffff $03ff082a 10 gen
  414: $14200001 $0021082a $14200000 $0001082a $14200000 $0020082a
  415: $14200001 $0000082a $1420ffff $03ff082a 10 gen
  416: $10200001 $0021082b $10200000 $0020082b $10200000 $0001082b
  417: $10200001 $0000082b $1020ffff $03ff082b 10 gen
  418: $14200001 $0021082b $14200000 $0020082b $14200000 $0001082b
  419: $14200001 $0000082b $1420ffff $03ff082b 10 gen
  420: $10200001 $0021082b $10200000 $0001082b $10200000 $0020082b
  421: $10200001 $0000082b $1020ffff $03ff082b 10 gen
  422: $14200001 $0021082b $14200000 $0001082b $14200000 $0020082b
  423: $14200001 $0000082b $1420ffff $03ff082b 10 gen
  424: $10200001 $0021082b $10200000 $0020082b $10200000 $0001082b
  425: $10200001 $0000082b $1020ffff $03ff082b 10 gen
  426: here over - disasm-dump
  427: 
  428: finish
  429: [THEN]

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