Decomp.st
author Claus Gittinger <cg@exept.de>
Sat, 11 Nov 1995 16:31:47 +0100
changeset 135 aa4f7b8f121e
parent 4 f6fd83437415
child 164 b511f22fea3b
permissions -rw-r--r--
uff - version methods changed to return stings

"
 COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

ByteCodeCompiler subclass:#Decompiler
       instanceVariableNames:'extra hasLineNo bytes literals index '
       classVariableNames:''
       poolDictionaries:''
       category:'System-Compiler'
!

!Decompiler class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1991 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
    ^ '$Header: /cvs/stx/stx/libcomp/Attic/Decomp.st,v 1.4 1995-11-11 15:30:20 cg Exp $'
!

documentation
"
    only for stx-debugging. Not for public eyes.

    Decompiler decompile:(someClass compiledMethodAt:#someSelector)
"
! !

!Decompiler class methodsFor:'decompiling'!

decompile:aMethod
    ^ (self new) decompile:aMethod

    "Decompiler decompile:(FileBrowser compiledMethodAt:#initialize)"
! !

!Decompiler methodsFor:'decompiling'!

showOffset:byte
    |offs|

    index := index + 1.
    (byte > 127) ifTrue:[
	offs := byte - 256
    ] ifFalse:[
	offs := byte
    ].
    Transcript show:(offs printString).
    Transcript show:' ('.
    Transcript show:(index + offs) printString.
    Transcript show:')'
!

showLongOffset:byte
    |offs|

    index := index + 1.
    (byte > 127) ifTrue:[
	offs := byte - 256 - 128
    ] ifFalse:[
	offs := byte + 128
    ].
    Transcript show:offs printString.
    Transcript show:' ('.
    Transcript show:(index + offs) printString.
    Transcript show:')'
!

showVeryLongOffset:byte
    |offs|

    index := index + 1.
    (byte > 127) ifTrue:[
	offs := byte - 256 - 256
    ] ifFalse:[
	offs := byte + 256
    ].
    Transcript show:offs printString.
    Transcript show:' ('.
    Transcript show:(index + offs) printString.
    Transcript show:')'
!

showAbsOffset:byte
    |offs b2|

    index := index + 1.
    b2 := (bytes at:index).
    offs := byte + (b2 bitShift:8).
    index := index + 1.
    Transcript show:offs printString.
    Transcript show:' ('.
    Transcript show:offs printString.
    Transcript show:')'
!

showAbsOffsetLevel:byte
    |offs b2|

    index := index + 1.
    b2 := (bytes at:index).
    offs := byte + (b2 bitShift:8).
    index := index + 1.
    Transcript show:offs printString.
    Transcript show:' ('.
    Transcript show:offs printString.
    Transcript show:')'.
    self showNvarNargsAt:index.
    index := index + 2
!

showNvarNargsAt:index
    Transcript show:' nv='.
    Transcript show:(bytes at:index) printString.
    Transcript show:' na='.
    Transcript show:(bytes at:(index + 1)) printString
!

showLiteralAt:index
    |offs|

    offs := bytes at:index.
    Transcript show:(literals at:offs) printString
!

showLiteral:byte
    index := index + 1.
    Transcript show:(literals at:byte) printString.
!

showLiteralSkip5:byte
    index := index + 1.
    Transcript show:(literals at:byte) printString.
    index := index + 5
!

showOffsetLevel:byte
    self showOffset:byte.
    self showNvarNargsAt:index.
    index := index + 2
!

showLongOffsetLevel:byte
    self showLongOffset:byte.
    self showNvarNargsAt:index.
    index := index + 2
!

showVeryLongOffsetLevel:byte
    self showVeryLongOffset:byte.
    self showNvarNargsAt:index.
    index := index + 2
!

showSendArgs:byte
    Transcript show:byte printString.
    Transcript show:' '.
    index := index + 1.
    self showLiteralAt:index.
    index := index + 1
!

showSuperSendArgs:byte
    Transcript show:byte printString.
    Transcript show:' '.
    index := index + 1.
    self showLiteralAt:index.
    index := index + 1.
    self showLiteralAt:index.
    index := index + 1
!

showLineNo:byte
    Transcript show:' line:' , byte printString.
    index := index + 1
!

showIndex:byte
    Transcript show:byte printString.
    index := index + 1
!

showIndexLevel:byte
    self showIndex:byte.
    self showNvarNargsAt:index.
    index := index + 2
!

showNumber:byte
    Transcript show:byte printString.
    index := index + 1
!

showPC
    Transcript show:(index printStringRadix:10 size:3 fill:(Character space)).
    Transcript show:': '.
    Transcript show:((bytes at:index) printStringRadix:16 size:2 fill:$0).
    Transcript show:' '.
!

decompile:aMethod
    |nBytes offs byte sym sel|

    aMethod isNil ifTrue:[
	Transcript showCr:'nil method'.
	^ self
    ].   

    bytes := aMethod byteCode.
    bytes isNil ifTrue:[
	Transcript showCr:'no bytecode'.
	^ self
    ].
    literals := aMethod literals.
    index := 1.
    nBytes := bytes size.
    [index <= nBytes] whileTrue:[
	self showPC.
	sym := self symbolicCodeFor:(bytes at:index).
	Transcript show:sym.
"
	extra notNil ifTrue:[Transcript show:(extra printString)].
"
	hasLineNo ifTrue:[
	    index := index + 1
	].
	index := index + 1.
	extra notNil ifTrue:[
	    Transcript show:' '.
	    byte := bytes at:index.

	    "compute argument showXXX selector from extra ..."

	    sel := 'show' , extra , ':'.
	    sel at:5 put:(sel at:5) asUppercase.
	    self perform:sel asSymbol with:byte
	].
	Transcript showCr:''
    ]
!

symbolicCodeFor:aByte
    |syms extras lnos|

    syms := #(  retTop         " 0  "
		retNil
		retTrue
		retFalse
		ret0
		retSelf         " 5  "
		blockRetTop
		nil
		nil
		nil
		pushNil         " 10 "
		pushTrue
		pushFalse
		pushInt
		pushLit
		pushSelf        " 15 "
		pushNum
		pushChar
		drop
		send
		superSend       " 20 "
		send0
		send1
		send2
		send3
		sendDrop        " 25 "
		sendDrop0
		sendDrop1
		sendDrop2
		sendDrop3
		pushMethodArg   " 30 "
		pushMethodVar
		pushBlockArg
		pushBlockVar
		pushInstVar
		pushClassVar    " 35 "
		pushGlobal
		storeMethodVar
		storeBlockVar
		storeInstVar
		storeClassVar   " 40 "
		storeGlobal
		pushOuterBlockArg
		pushOuter1BlockArg
		pushOuter2BlockArg
		equal           " 45 "
		notEqual
		dup
		equal0
		notEqual0
		falseJump       " 50 "
		trueJump
		nilJump
		notNilJump
		jump
		makeBlock       " 55 "
		zeroJump
		notZeroJump
		eqJump
		notEqJump
		falseJump       " 60 "
		trueJump
		nilJump
		notNilJump
		jump
		makeBlock       " 65 "
		zeroJump
		notZeroJump
		eqJump
		notEqJump
		falseJump       " 70 "
		trueJump
		nilJump
		notNilJump
		jump
		makeBlock       " 75 "
		zeroJump
		notZeroJump
		eqJump
		notEqJump
		pushMethodVar1  " 80 "
		pushMethodVar2
		pushMethodVar3
		pushMethodVar4
		pushMethodVar5
		pushMethodVar6  " 85 "
		pushMethodArg1
		pushMethodArg2
		pushMethodArg3
		pushMethodArg4
		pushInstVar1    " 90 "
		pushInstVar2
		pushInstVar3
		pushInstVar4
		pushInstVar5
		pushInstVar6    " 95 "
		pushInstVar7
		pushInstVar8
		pushInstVar9
		pushInstVar10
		storeMethodVar1 " 100 "
		storeMethodVar2
		storeMethodVar3
		storeMethodVar4
		storeMethodVar5
		storeMethodVar6 " 105 "
		sendY
		sendX
		sendWidth
		sendHeight
		storeInstVar1   " 110 "
		storeInstVar2
		storeInstVar3
		storeInstVar4
		storeInstVar5
		storeInstVar6  " 115 "
		storeInstVar7
		storeInstVar8
		storeInstVar9
		storeInstVar10
		push0           " 120 "
		push1
		pushMinus1
		sendPlus1
		sendMinus1
		incMethodVar    " 125 "
		decMethodVar
		retNum
		pushOuterBlockVar
		storeOuterBlockVar
		sendEQ          " 130 "
		sendPLUS
		sendNE
		sendMINUS
		sendCLASS
		sendAT          " 135 "
		sendATPUT
		sendBitAnd
		sendBitOr
		push2
		pushBlockArg1   " 140 "
		pushBlockArg2
		pushBlockArg3
		pushBlockArg4
		pushContext
		sendGT          " 145 "
		sendGE
		sendLT
		sendLE
		sendNEXT
		sendPEEK        " 150 "
		sendVALUE
		sendVALUE1
		sendSIZE
		sendORIGIN
		sendEXTENT      " 155 "
		make0Block             
		makeNILBlock
		nil
		nil
		retMvar1        " 160 "
		retMvar2        
		retMvar3        
		retMvar4        
		retMvar5        
		retMvar6        " 165 "
		retIvar1        
		retIvar2        
		retIvar3        
		retIvar4        
		retIvar5        " 170 "
		retIvar6        
		retIvar7        
		retIvar8        
		retMarg1        
		retMarg2        " 175 "
		pushClassInstVar
		storeClassInstVar
		nil
		nil
		sendSelf0       " 180 "
		sendSelf1
		sendSelf2
		sendSelf3
		sendSelfDrop0   
		sendSelfDrop1   " 185 "
		sendSelfDrop2
		sendSelfDrop3
		nil
		nil
		falseJumpAbs    " 190 "
		trueJumpAbs     
		nilJumpAbs     
		notNilJumpAbs   
		jumpAbs        
		makeBlockAbs    " 195 "
		zeroJumpAbs
		notZeroJumpAbs
		eqJumpAbs
		notEqJumpAbs    " 199 "
	      ).

    lnos := #(  false          " 0  "
		false   
		false   
		false   
		false
		false          " 5  "
		false
		false
		false
		false
		false          " 10 "
		false
		false
		false
		false
		false          " 15 "
		false
		false
		false
		true
		true           " 20 "
		true 
		true 
		true 
		true 
		true           " 25 "
		true 
		true 
		true 
		true 
		false          " 30 "
		false
		false
		false
		false
		false          " 35 "
		false
		false
		false
		false
		false           " 40 "
		false
		false
		false
		false
		false           " 45 "
		false
		false
		false
		false
		false       " 50 "
		false
		false
		false
		false
		false       " 55 "
		false
		false
		false
		false
		false       " 60 "
		false
		false
		false
		false
		false       " 65 "
		false
		false
		false
		false
		false       " 70 "
		false
		false
		false
		false
		false       " 75 "
		false
		false
		false
		false 
		false  " 80 "
		false
		false
		false
		false
		false  " 85 "
		false
		false
		false
		false
		false    " 90 "
		false
		false
		false
		false
		false    " 95 "
		false
		false
		false
		false
		false " 100 "
		false
		false
		false
		false
		false " 105 "
		false
		false
		false
		false
		false   " 110 "
		false
		false
		false
		false
		false  " 115 "
		false
		false
		false
		false
		false           " 120 "
		false
		false
		false
		false
		false    " 125 "
		false
		false
		false
		false
		false          " 130 "
		false
		false
		false
		false
		false          " 135 "
		false
		false
		false
		false
		false   " 140 "
		false
		false
		false
		false
		false          " 145 "
		false
		false
		false
		false
		false        " 150 "
		false
		false
		false
		false
		false             " 155 "
		false
		false
		false
		false
		false        " 160 "
		false
		false
		false
		false
		false        " 165 "
		false
		false
		false
		false
		false        " 170 "
		false
		false
		false
		false
		false        " 175 "
		false
		false
		false
		false
		true         " 180 "
		true
		true
		true
		true
		true         " 185 "
		true
		true
		false
		false
		false         " 190 "
		false
		false
		false
		false
		false         " 195 "
		false
		false
		false
		false         " 199 "
	      ).

    extras := #(nil             " 0  "
		nil
		nil
		nil
		nil
		nil             " 5  "
		nil
		nil
		nil
		nil
		nil             " 10 "
		nil
		nil
		nil
		literal
		nil             " 15 "
		number
		nil
		nil
		sendArgs
		superSendArgs   " 20 "
		literal
		literal
		literal
		literal
		sendArgs        " 25 "
		literal
		literal
		literal
		literal
		index           " 30 "
		index
		index
		index
		index
		literalSkip5    " 35 "
		literalSkip5
		index
		index
		index
		literalSkip5    " 40 "
		literalSkip5
		indexLevel
		index
		index
		nil             " 45 "
		nil
		nil
		nil
		nil
		offset          " 50 "
		offset
		offset
		offset
		offset
		offsetLevel     " 55 "
		offset
		offset
		offset
		offset
		longOffset      " 60 "
		longOffset
		longOffset
		longOffset
		longOffset
		longOffsetLevel " 65 "
		longOffset
		longOffset
		longOffset
		longOffset
		veryLongOffset  " 70 "
		veryLongOffset
		veryLongOffset
		veryLongOffset
		veryLongOffset
		veryLongOffsetLevel  " 75 "
		veryLongOffset
		veryLongOffset
		veryLongOffset
		veryLongOffset
		nil             " 80 "
		nil
		nil
		nil
		nil
		nil             " 85 "
		nil
		nil
		nil
		nil
		nil             " 90 "
		nil
		nil
		nil
		nil
		nil             " 95 "
		nil
		nil
		nil
		nil
		nil             " 100 "
		nil
		nil
		nil
		nil
		nil             " 105 "
		nil
		nil
		nil
		nil
		nil             " 110 "
		nil
		nil
		nil
		nil
		nil             " 115 "
		nil
		nil
		nil
		nil
		nil             " 120 "
		nil
		nil
		nil
		nil
		index           " 125 "
		index
		number
		indexLevel
		indexLevel
		nil             " 130 "
		nil
		nil
		nil
		nil
		nil             " 135 "
		nil
		nil
		nil
		nil
		nil             " 140 "
		nil
		nil
		nil
		nil
		nil             " 145 "
		nil
		nil
		nil
		nil
		nil             " 150 "
		nil
		nil
		nil
		nil
		nil             " 155 "
		nil
		nil
		nil
		nil
		nil             " 160 "
		nil
		nil
		nil
		nil
		nil             " 165 "
		nil
		nil
		nil
		nil
		nil             " 170 "
		nil
		nil
		nil
		nil
		nil             " 175 "
		index
		index
		nil
		nil
		literal         " 180 "
		literal
		literal
		literal
		literal         
		literal         " 185 "
		literal
		literal
		nil
		nil
		absOffset       " 190 "
		absOffset
		absOffset
		absOffset
		absOffset
		absOffsetLevel
		absOffset
		absOffset
		absOffset
		absOffset       " 199 "
	     ).

    extra := extras at:(aByte + 1).
    hasLineNo := lnos at:(aByte + 1).
    ^ syms at:(aByte + 1)
! !