Add some Show instances.
authorIlya Portnov <portnov84@rambler.ru>
Mon, 13 Jun 2011 12:51:26 +0000 (18:51 +0600)
committerIlya Portnov <portnov84@rambler.ru>
Mon, 13 Jun 2011 12:51:26 +0000 (18:51 +0600)
JVM/ClassFile.hs
JVM/Types.hs
disassemble.hs

index 0f8a18289dbb84a68540997e78008a88d02fd713..3a9f3bf39d6a59f99f1480fadc0288404e126325 100644 (file)
@@ -9,6 +9,7 @@ import Data.Binary.Get
 import Data.Binary.Put
 import Data.Word
 import Data.Char
+import Data.List
 import qualified Data.ByteString.Lazy as B
 
 import Debug.Trace
@@ -100,7 +101,20 @@ data FieldType =
   | BoolType   -- Z
   | ObjectType String -- L <class name>
   | Array (Maybe Int) FieldType
-  deriving (Eq, Show)
+  deriving (Eq)
+
+instance Show FieldType where
+  show SignedByte = "byte"
+  show CharByte = "char"
+  show DoubleType = "double"
+  show FloatType = "float"
+  show IntType = "int"
+  show LongInt = "long"
+  show ShortInt = "short"
+  show BoolType = "bool"
+  show (ObjectType s) = "Object " ++ s
+  show (Array Nothing t) = show t ++ "[]"
+  show (Array (Just n) t) = show t ++ "[" ++ show n ++ "]"
 
 type FieldSignature = FieldType
 
@@ -166,7 +180,11 @@ getToSemicolon = do
 data ReturnSignature =
     Returns FieldType
   | ReturnsVoid
-  deriving (Eq, Show)
+  deriving (Eq)
+
+instance Show ReturnSignature where
+  show (Returns t) = show t
+  show ReturnsVoid = "Void"
 
 instance Binary ReturnSignature where
   put (Returns sig) = put sig
@@ -182,7 +200,10 @@ type ArgumentSignature = FieldType
 
 data MethodSignature =
     MethodSignature [ArgumentSignature] ReturnSignature
-  deriving (Eq, Show)
+  deriving (Eq)
+
+instance Show MethodSignature where
+  show (MethodSignature args ret) = "(" ++ intercalate ", " (map show args) ++ ") returns " ++ show ret
 
 instance Binary MethodSignature where
   put (MethodSignature args ret) = do
index 2f2f3a17808cd9eb6bddef9adbfbc58f73436362..834af21e86aa279e609864d9d7c937a68d98f711 100644 (file)
@@ -15,6 +15,9 @@ import JVM.ClassFile
 instance IsString B.ByteString where
   fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s
 
+toString :: B.ByteString -> String
+toString bstr = decodeString $ map (chr . fromIntegral) $ B.unpack bstr
+
 type Pool = Array Word16 Constant
 
 class HasAttributes a where
@@ -42,7 +45,9 @@ data NameType a = NameType {
   ntName :: B.ByteString,
   ntSignature :: Signature a }
 
-deriving instance Show (Signature a) => Show (NameType a)
+instance Show (Signature a) => Show (NameType a) where
+  show (NameType n t) = toString n ++ ": " ++ show t
+
 deriving instance Eq (Signature a) => Eq (NameType a)
 
 data Constant =
@@ -58,7 +63,21 @@ data Constant =
   | CNameType B.ByteString B.ByteString
   | CUTF8 {getString :: B.ByteString}
   | CUnicode {getString :: B.ByteString}
-  deriving (Eq, Show)
+  deriving (Eq)
+
+instance Show Constant where
+  show (CClass name) = "class " ++ toString name
+  show (CField cls nt) = "field " ++ toString cls ++ "." ++ show nt
+  show (CMethod cls nt) = "method " ++ toString cls ++ "." ++ show nt
+  show (CIfaceMethod cls nt) = "interface method " ++ toString cls ++ "." ++ show nt
+  show (CString s) = "String \"" ++ toString s ++ "\""
+  show (CInteger x) = show x
+  show (CFloat x) = show x
+  show (CLong x) = show x
+  show (CDouble x) = show x
+  show (CNameType name tp) = toString name ++ ": " ++ toString tp
+  show (CUTF8 s) = "UTF8 \"" ++ toString s ++ "\""
+  show (CUnicode s) = "Unicode \"" ++ toString s ++ "\""
 
 data Field = Field {
   fieldAccess :: Access,
index fc6e77f4da9e04633af4aa2c27c06b0e478a825b..619ef2c736d81a6d9dfd40ea311b55eda074d644 100644 (file)
@@ -1,7 +1,9 @@
 {-# LANGUAGE OverloadedStrings #-}
 import Control.Monad
+import Data.Array
 import System.Environment
 import qualified Data.ByteString.Lazy as B
+import Text.Printf
 
 import Data.BinaryState
 import JVM.Types
@@ -15,6 +17,9 @@ main = do
       cls <- decompileFile clspath
       putStr "Class: "
       B.putStrLn (this cls)
+      putStrLn "Constants pool:"
+      forM_ (assocs $ constantPool cls) $ \(i, c) ->
+        putStrLn $ printf "  #%d:\t%s" i (show c)
       putStrLn "Methods:"
       forM_ (methods cls) $ \m -> do
         putStr ">> Method "