1 {-# LANGUAGE TypeFamilies, StandaloneDeriving, FlexibleInstances, FlexibleContexts, UndecidableInstances, RecordWildCards, OverloadedStrings #-}
2 module JVM.Converter where
4 import Codec.Binary.UTF8.String hiding (encode, decode)
11 import qualified Data.ByteString.Lazy as B
13 import qualified Data.Set as S
14 import qualified Data.Map as M
21 instance IsString B.ByteString where
22 fromString s = B.pack $ map (fromIntegral . ord) $ encodeString s
24 decompile :: B.ByteString -> Class
25 decompile bstr = convertClass $ decode bstr
27 decompileFile :: FilePath -> IO Class
28 decompileFile path = convertClass `fmap` decodeFile path
30 convertClass :: ClassFile -> Class
31 convertClass (ClassFile {..}) =
32 let pool = constantPoolArray constsPool
33 superName = className $ pool ! superClass
36 classAccess = convertAccess accessFlags,
37 this = className $ pool ! thisClass,
38 super = if superClass == 0 then Nothing else Just superName,
39 implements = map (\i -> className $ pool ! i) interfaces,
40 fields = map (convertField pool) classFields,
41 methods = map (convertMethod pool) classMethods,
42 classAttrs = convertAttrs pool classAttributes }
44 constantPoolArray :: [CpInfo] -> Pool
45 constantPoolArray list = pool
48 pool = listArray (1,n) $ map convert list
49 n = fromIntegral $ length list
51 convertNameType :: (HasSignature a, Binary (Signature a)) => Word16 -> NameType a
53 let (CNameType n s) = pool ! i
54 in NameType n (decode s)
56 convert (CONSTANT_Class i) = CClass $ getString $ pool ! i
57 convert (CONSTANT_Fieldref i j) = CField (className $ pool ! i) (convertNameType j)
58 convert (CONSTANT_Methodref i j) = CMethod (className $ pool ! i) (convertNameType j)
59 convert (CONSTANT_InterfaceMethodref i j) = CIfaceMethod (className $ pool ! i) (convertNameType j)
60 convert (CONSTANT_String i) = CString $ getString $ pool ! i
61 convert (CONSTANT_Integer x) = CInteger x
62 convert (CONSTANT_Float x) = CFloat x
63 convert (CONSTANT_Long x) = CLong (fromIntegral x)
64 convert (CONSTANT_Double x) = CDouble x
65 convert (CONSTANT_NameAndType i j) = CNameType (getString $ pool ! i) (getString $ pool ! j)
66 convert (CONSTANT_Utf8 _ bs) = CUTF8 bs
67 convert (CONSTANT_Unicode _ bs) = CUnicode bs
69 className' x = trace ("Class name: " ++ show x) B.empty
71 convertAccess :: Word16 -> Access
72 convertAccess w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then [f] else []) [0..] $ [
85 convertField :: Pool -> FieldInfo -> Field
86 convertField pool (FieldInfo {..}) = Field {
87 fieldAccess = convertAccess fieldAccessFlags,
88 fieldName = getString $ pool ! fieldNameIndex,
89 fieldSignature = decode $ getString $ pool ! fieldSignatureIndex,
90 fieldAttrs = convertAttrs pool fieldAttributes }
92 convertMethod :: Pool -> MethodInfo -> Method
93 convertMethod pool (MethodInfo {..}) = Method {
94 methodAccess = convertAccess methodAccessFlags,
95 methodName = getString $ pool ! methodNameIndex,
96 methodSignature = decode $ getString $ pool ! methodSignatureIndex,
97 methodAttrs = convertAttrs pool methodAttributes }
99 convertAttrs :: Pool -> [AttributeInfo] -> Attributes
100 convertAttrs pool attrs = M.fromList $ map go attrs
102 go (AttributeInfo {..}) = (getString $ pool ! attributeName,
105 methodByName :: Class -> B.ByteString -> Maybe Method
106 methodByName cls name =
107 find (\m -> methodName m == name) (methods cls)
109 attrByName :: (HasAttributes a) => a -> B.ByteString -> Maybe B.ByteString
110 attrByName x name = M.lookup name (attributes x)
112 methodCode :: Class -> B.ByteString -> Maybe B.ByteString
113 methodCode cls name = do
114 method <- methodByName cls name
115 attrByName method "Code"