File:  [gforth] / gforth / arch / mips / disasm.fs
Revision 1.1: download - view: text, annotated - select for diffs
Sat May 27 19:03:48 2000 UTC (21 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
added mips assembler and disassembler (from Christian Pirker/RAFTS)

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

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