JavaDecompiler.st
changeset 454 38f590639d65
parent 450 4f6899814521
child 456 bbed0c5ce05f
--- a/JavaDecompiler.st	Mon Nov 16 15:13:04 1998 +0000
+++ b/JavaDecompiler.st	Mon Nov 16 15:17:54 1998 +0000
@@ -1,10 +1,64 @@
+"
+ COPYRIGHT (c) 1997 by eXept Software AG
+              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.
+"
+
+
 Object subclass:#JavaDecompiler
-	instanceVariableNames:'code pc javaMethod outStream classToCompileFor'
+	instanceVariableNames:'code pc javaMethod outStream classToCompileFor isStaticMethod
+		lastConstIndex lastConstant accessedInstvars modifiedInstvars
+		accessedStaticVars modifiedStaticVars sentMessages'
 	classVariableNames:'DecoderTable'
 	poolDictionaries:''
 	category:'Java-Support'
 !
 
+!JavaDecompiler class methodsFor:'documentation'!
+
+copyright
+"
+ COPYRIGHT (c) 1997 by eXept Software AG
+              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.
+"
+
+!
+
+examples
+"
+
+                                                                        [exBegin]
+    |m|
+
+    m := JAVA::java::lang::Object compiledMethodAt:#'toString()Ljava/lang/String;'.
+    JavaDecompiler decompile:m to:Transcript.
+                                                                        [exEnd]
+
+
+
+                                                                        [exBegin]
+    |m|
+
+    m := JAVA::java::lang::Object compiledMethodAt:#'toString()Ljava/lang/String;'.
+    JavaDecompiler decompile:m to:nil.
+                                                                        [exEnd]
+
+"
+
+! !
 
 !JavaDecompiler class methodsFor:'initialization'!
 
@@ -29,10 +83,10 @@
         (dconst_1)                              "/ 15
         (bipush signedByte)                     "/ 16
         (sipush signedShort)                    "/ 17
-        (ldc1 constIndexByte)                   "/ 18
-        (ldc2 constIndexShort)                  "/ 19
+        (ldc1 constIndexByte remember_ldc1)              "/ 18
+        (ldc2 constIndexShort remember_ldc2)             "/ 19
 
-        (ldc2w constIndexShort)                 "/ 20
+        (ldc2w constIndexShort remember_ldc2w)            "/ 20
         (iload localIndexByte)                  "/ 21
         (lload localIndexByte)                  "/ 22
         (fload localIndexByte)                  "/ 23
@@ -209,22 +263,22 @@
         (dreturn)                               "/ 175
         (areturn)                               "/ 176
         (return)                                "/ 177
-        (getstatic constIndexShort)             "/ 178
-        (putstatic constIndexShort)             "/ 179
+        (getstatic constIndexShort remember_getstatic)               "/ 178
+        (putstatic constIndexShort remember_putstatic)               "/ 179
 
-        (getfield constIndexShort)              "/ 180
-        (putfield constIndexShort)              "/ 181
-        (invokevirtual constIndexShort)         "/ 182
-        (invokenonvirtual constIndexShort)      "/ 183
-        (invokestatic constIndexShort)          "/ 184
-        (invokeinterface constIndexShort nargsByte reservedByte)          "/ 185
+        (getfield constIndexShort remember_getfield)                 "/ 180
+        (putfield constIndexShort remember_putfield)                 "/ 181
+        (invokevirtual constIndexShort remember_invokevirtual)       "/ 182
+        (invokenonvirtual constIndexShort remember_invokenonvirtual) "/ 183
+        (invokestatic constIndexShort remember_invokestatic)         "/ 184
+        (invokeinterface constIndexShort nargsByte reservedByte remember_invokeinterface)          "/ 185
         (newfromname)                           "/ 186
-        (new constIndexShort)                   "/ 187
+        (new constIndexShort remember_new)      "/ 187
         (newarray arrayTypeByte)                "/ 188
         (anewarray constIndexShort)             "/ 189
 
         (arraylength)                           "/ 190
-        (athrow)                                "/ 191
+        (athrow remember_athrow)                "/ 191
         (checkcast constIndexShort)             "/ 192
         (instanceof constIndexShort)            "/ 193
         (monitorenter)                          "/ 194
@@ -305,7 +359,7 @@
      JavaDecompiler initialize
     "
 
-    "Modified: 4.8.1997 / 19:12:45 / cg"
+    "Modified: / 16.11.1998 / 15:55:49 / cg"
 ! !
 
 !JavaDecompiler class methodsFor:'accessing'!
@@ -629,27 +683,33 @@
 
     outStream := aStream.
 
+    isStaticMethod := aJavaMethod isStatic.
+
     who := aJavaMethod who.
     who notNil ifTrue:[
         classToCompileFor := who methodClass.
-        aStream cr.
-"/        aStream showCR:'decompiling ' , classToCompileFor name , '>>' , (who methodSelector) , ':'.
-        aStream showCR:'decompiling ' , aJavaMethod displayString , ':'.
-        aJavaMethod isStatic ifTrue:[
-            aStream show:'static method'.
-        ].
-        aStream cr.
-        aStream cr.
+        outStream notNil ifTrue:[
+            outStream cr.
+"/            outStream showCR:'decompiling ' , classToCompileFor name , '>>' , (who methodSelector) , ':'.
+            outStream showCR:'decompiling ' , aJavaMethod displayString , ':'.
+            isStaticMethod ifTrue:[
+                outStream show:'static method'.
+            ].
+            outStream cr.
+            outStream cr.
+        ]
     ].
 
     javaMethod := aJavaMethod.
     code := aJavaMethod javaByteCode.
     endPC := code size.
     endPC == 0 ifTrue:[
-        javaMethod isNative ifTrue:[
-            outStream nextPutAll:'// native method'; cr
-        ] ifFalse:[
-            outStream nextPutAll:'{}  // no bytecode'; cr
+        outStream notNil ifTrue:[
+            javaMethod isNative ifTrue:[
+                outStream nextPutAll:'// native method'; cr
+            ] ifFalse:[
+                outStream nextPutAll:'{}  // no bytecode'; cr
+            ].
         ].
         ^ self
     ].
@@ -665,13 +725,16 @@
         ] ifFalse:[
             op := spec at:1
         ].
-        outStream 
-            show:((pc - 1 - 1) printStringPaddedTo:4); 
-            show:' '.
-        outStream
-            show:(wide ifTrue:[op , '(w)'] ifFalse:[op]).
-        outStream
-            show:' '.
+
+        outStream notNil ifTrue:[
+            outStream 
+                show:((pc - 1 - 1) printStringPaddedTo:4); 
+                show:' '.
+            outStream
+                show:(wide ifTrue:[op , '(w)'] ifFalse:[op]).
+            outStream
+                show:' '.
+        ].
 
         op == #wide ifTrue:[
             wide := true.
@@ -687,11 +750,13 @@
             ].
             wide := false.
         ].
-        outStream cr.
+        outStream notNil ifTrue:[
+            outStream cr.
+        ]
     ]
 
     "Created: / 16.4.1996 / 14:59:29 / cg"
-    "Modified: / 13.1.1998 / 23:58:34 / cg"
+    "Modified: / 16.11.1998 / 15:51:10 / cg"
 ! !
 
 !JavaDecompiler methodsFor:'operand decoding'!
@@ -706,46 +771,52 @@
               'T_CHAR'  'T_FLOAT'  'T_DOUBLE'  'T_BYTE' 
               'T_SHORT' 'T_INT'    'T_LONG' ) at:index ifAbsent:['invalid' , index printString].
 
-    outStream
-        show:index; 
-        show:' [';
-        show:type;
-        show:']'
+    outStream notNil ifTrue:[
+        outStream
+            show:index; 
+            show:' [';
+            show:type;
+            show:']'
+    ]
 
-    "Created: / 16.4.1996 / 15:00:04 / cg"
-    "Modified: / 12.11.1998 / 22:58:06 / cg"
+    "Created: / 16.11.1998 / 15:41:47 / cg"
+    "Modified: / 16.11.1998 / 15:43:27 / cg"
 !
 
 constIndexByte
-    |index|
-
-    index := code at:pc.
+    lastConstIndex := code at:pc.
     pc := pc + 1.
 
-    outStream
-        show:index; 
-        show:' ['; 
-        show:(javaMethod constantPool at:index ifAbsent:['??']) displayString; 
-        show:'] '
+    lastConstant := javaMethod constantPool at:lastConstIndex ifAbsent:nil.
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    outStream notNil ifTrue:[
+        outStream
+            show:lastConstIndex; 
+            show:' ['; 
+            show:(lastConstant ? '??') displayString; 
+            show:'] '
+    ]
+
+    "Created: / 16.11.1998 / 15:42:56 / cg"
+    "Modified: / 16.11.1998 / 15:58:52 / cg"
 !
 
 constIndexShort
-    |index|
-
-    index := code wordAt:pc MSB:true.
+    lastConstIndex := code wordAt:pc MSB:true.
     pc := pc + 2.
 
-    outStream
-        show:index; 
-        show:' ['; 
-        show:(javaMethod constantPool at:index ifAbsent:'???') displayString; 
-        show:'] '
+    lastConstant := javaMethod constantPool at:lastConstIndex ifAbsent:nil.
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    outStream notNil ifTrue:[
+        outStream
+            show:lastConstIndex; 
+            show:' ['; 
+            show:(lastConstant ? '??') displayString; 
+            show:'] '
+    ]
+
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:59:23 / cg"
 !
 
 dimensionsByte
@@ -759,12 +830,14 @@
     index := code at:pc.
     pc := pc + 1.
 
-    outStream
-        show:index; 
-        show:' '
+    outStream notNil ifTrue:[
+        outStream
+            show:index; 
+            show:' '
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 4.8.1997 / 19:01:52 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:43:55 / cg"
 !
 
 localIndexByte_wide
@@ -773,12 +846,14 @@
     index := code wordAt:pc MSB:true.
     pc := pc + 2.
 
-    outStream
-        show:index; 
-        show:' '
+    outStream notNil ifTrue:[
+        outStream
+            show:index; 
+            show:' '
+    ]
 
-    "Modified: 16.4.1996 / 15:30:55 / cg"
-    "Created: 4.8.1997 / 19:02:09 / cg"
+    "Created: / 4.8.1997 / 19:02:09 / cg"
+    "Modified: / 16.11.1998 / 15:44:02 / cg"
 !
 
 lookupSwitchBytes
@@ -802,25 +877,29 @@
         pc := pc + 4.
         delta := code signedDoubleWordAt:pc MSB:true.
         pc := pc + 4.
-        outStream show:'    '; 
-                  show:match;
-                  show:' -> ';
-                  show:delta;
-                  show:' [';
-                  show:(pc0 - 1 + delta);
-                  show:']';
-                  cr.
+        outStream notNil ifTrue:[
+            outStream show:'    '; 
+                      show:match;
+                      show:' -> ';
+                      show:delta;
+                      show:' [';
+                      show:(pc0 - 1 + delta);
+                      show:']';
+                      cr.
+        ]
     ].
 
-    outStream show:'    '; 
-              show:'default';
-              show:' -> ';
-              show:defaultOffset;
-              show:' [';
-              show:(pc0 - 1 + defaultOffset);
-              show:']'.
+    outStream notNil ifTrue:[
+        outStream show:'    '; 
+                  show:'default';
+                  show:' -> ';
+                  show:defaultOffset;
+                  show:' [';
+                  show:(pc0 - 1 + defaultOffset);
+                  show:']'.
+    ]
 
-    "Modified: / 5.1.1998 / 00:33:57 / cg"
+    "Modified: / 16.11.1998 / 15:44:26 / cg"
 !
 
 nargsByte
@@ -829,11 +908,13 @@
     byte := code byteAt:pc.
     pc := pc + 1.
 
-    outStream
-        show:byte 
+    outStream notNil ifTrue:[
+        outStream
+            show:byte 
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:44:34 / cg"
 !
 
 reservedByte
@@ -842,11 +923,13 @@
     byte := code byteAt:pc.
     pc := pc + 1.
 
-    outStream
-        show:byte 
+    outStream notNil ifTrue:[
+        outStream
+            show:byte 
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:44:42 / cg"
 !
 
 signedBranchShort
@@ -854,16 +937,18 @@
 
     index := code signedWordAt:pc MSB:true.
 
-    outStream
-        show:index; 
-        show:' ['; 
-        show:(pc - 1 + index - 1); 
-        show:']'.
+    outStream notNil ifTrue:[
+        outStream
+            show:index; 
+            show:' ['; 
+            show:(pc - 1 + index - 1); 
+            show:']'.
+    ].
 
     pc := pc + 2.
 
     "Created: / 16.4.1996 / 15:00:04 / cg"
-    "Modified: / 5.1.1998 / 00:34:14 / cg"
+    "Modified: / 16.11.1998 / 15:44:51 / cg"
 !
 
 signedByte
@@ -872,11 +957,13 @@
     byte := code signedByteAt:pc.
     pc := pc + 1.
 
-    outStream
-        show:byte 
+    outStream notNil ifTrue:[
+        outStream
+            show:byte 
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:44:58 / cg"
 !
 
 signedByte_wide
@@ -891,11 +978,13 @@
     word := code signedWordAt:pc MSB:true.
     pc := pc + 2.
 
-    outStream
-        show:word 
+    outStream notNil ifTrue:[
+        outStream
+            show:word 
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:45:06 / cg"
 !
 
 tableSwitchBytes
@@ -919,25 +1008,29 @@
     low to:high do:[:switchValue |
         delta := code signedDoubleWordAt:pc MSB:true.
         pc := pc + 4.
-        outStream show:'    '; 
-                  show:switchValue;
-                  show:' -> ';
-                  show:delta;
-                  show:' [';
-                  show:(pc0 - 1 + delta);
-                  show:']';
-                  cr.
+        outStream notNil ifTrue:[
+            outStream show:'    '; 
+                      show:switchValue;
+                      show:' -> ';
+                      show:delta;
+                      show:' [';
+                      show:(pc0 - 1 + delta);
+                      show:']';
+                      cr.
+        ].
     ].
 
-    outStream show:'    '; 
-              show:'default';
-              show:' -> ';
-              show:defaultOffset;
-              show:' [';
-              show:(pc0 - 1 + defaultOffset);
-              show:']'.
+    outStream notNil ifTrue:[
+        outStream show:'    '; 
+                  show:'default';
+                  show:' -> ';
+                  show:defaultOffset;
+                  show:' [';
+                  show:(pc0 - 1 + defaultOffset);
+                  show:']'.
+    ]
 
-    "Modified: / 5.1.1998 / 00:33:46 / cg"
+    "Modified: / 16.11.1998 / 15:45:25 / cg"
 !
 
 unsignedByte
@@ -946,11 +1039,13 @@
     byte := code byteAt:pc.
     pc := pc + 1.
 
-    outStream
-        show:byte 
+    outStream notNil ifTrue:[
+        outStream
+            show:byte 
+    ]
 
-    "Created: 16.4.1996 / 15:00:04 / cg"
-    "Modified: 16.4.1996 / 15:30:55 / cg"
+    "Created: / 16.4.1996 / 15:00:04 / cg"
+    "Modified: / 16.11.1998 / 15:45:33 / cg"
 ! !
 
 !JavaDecompiler methodsFor:'private'!
@@ -967,9 +1062,119 @@
     "Created: 16.8.1997 / 03:14:11 / cg"
 ! !
 
+!JavaDecompiler methodsFor:'statistics'!
+
+remember_getfield
+    |fieldRef|
+
+    fieldRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:07:58 / cg"
+!
+
+remember_getstatic
+    |fieldRef|
+
+    fieldRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:08:06 / cg"
+!
+
+remember_invokeinterface
+    |mthdRef|
+
+    mthdRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:07:29 / cg"
+!
+
+remember_invokenonvirtual
+    |mthdRef|
+
+    mthdRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:06:49 / cg"
+!
+
+remember_invokestatic
+    |mthdRef|
+
+    mthdRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:06:40 / cg"
+!
+
+remember_invokevirtual
+    |mthdRef|
+
+    mthdRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:06:32 / cg"
+!
+
+remember_ldc1
+    |what|
+
+    what := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:07:12 / cg"
+!
+
+remember_ldc2
+    |what|
+
+    what := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:07:19 / cg"
+!
+
+remember_ldc2w
+    |what|
+
+    what := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:07:20 / cg"
+!
+
+remember_new
+    |class|
+
+    class := lastConstant.
+
+    "Created: / 16.11.1998 / 15:59:43 / cg"
+    "Modified: / 16.11.1998 / 16:12:27 / cg"
+!
+
+remember_putfield
+    |fieldRef|
+
+    fieldRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:08:00 / cg"
+!
+
+remember_putstatic
+    |fieldRef|
+
+    fieldRef := lastConstant.
+    "/ ....
+
+    "Created: / 16.11.1998 / 16:08:04 / cg"
+! !
+
 !JavaDecompiler class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaDecompiler.st,v 1.39 1998/11/14 17:45:53 cg Exp $'
+    ^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaDecompiler.st,v 1.40 1998/11/16 15:14:48 cg Exp $'
 ! !
 JavaDecompiler initialize!