codeExceptionsN :: Word16,
codeExceptions :: [CodeException],
codeAttrsN :: Word16,
- codeAttributes :: [AttributeInfo] }
+ codeAttributes :: Attributes Pointers }
deriving (Eq, Show)
-- | Exception descriptor
get = CodeException <$> get <*> get <*> get <*> get
-instance BinaryState Integer AttributeInfo where
+instance BinaryState Integer Attribute where
put a = do
let sz = 6 + attributeLength a -- full size of AttributeInfo structure
liftOffset (fromIntegral sz) Binary.put a
put codeExceptionsN
forM_ codeExceptions put
put codeAttrsN
- forM_ codeAttributes put
+ forM_ (attributesList codeAttributes) put
get = do
stackSz <- get
excs <- replicateM (fromIntegral excn) get
nAttrs <- get
attrs <- replicateM (fromIntegral nAttrs) get
- return $ Code stackSz locals len code excn excs nAttrs attrs
+ return $ Code stackSz locals len code excn excs nAttrs (AP attrs)
-- | Read sequence of instructions (to end of stream)
readInstructions :: GetState Integer [Instruction]
HasSignature (..), HasAttributes (..),
AccessFlag (..), AccessFlags,
Attributes (..),
- className
+ className,
+ apsize, arsize, arlist
)
where
type instance AccessFlags Resolved = S.Set AccessFlag
-type family Attributes stage
+data family Attributes stage
-type instance Attributes Pointers = [Attribute]
-type instance Attributes Resolved = M.Map B.ByteString B.ByteString
+data instance Attributes Pointers = AP {attributesList :: [Attribute]}
+ deriving (Eq, Show)
+data instance Attributes Resolved = AR (M.Map B.ByteString B.ByteString)
+ deriving (Eq, Show)
+
+arsize :: Attributes Resolved -> Int
+arsize (AR m) = M.size m
+
+arlist :: Attributes Resolved -> [(B.ByteString, B.ByteString)]
+arlist (AR m) = M.assocs m
+
+apsize :: Attributes Pointers -> Int
+apsize (AP list) = length list
-- | Access flags. Used for classess, methods, variables.
data AccessFlag =
-- | Constant pool item
data Constant stage =
- CClass B.ByteString
+ CClass (Link stage B.ByteString)
| CField {refClass :: Link stage B.ByteString, fieldNameType :: Link stage (NameType Field)}
| CMethod {refClass :: Link stage B.ByteString, nameType :: Link stage (NameType Method)}
| CIfaceMethod {refClass :: Link stage B.ByteString, nameType :: Link stage (NameType Method)}
put classMethodsCount
forM_ classMethods put
put classAttributesCount
- forM_ classAttributes put
+ forM_ (attributesList classAttributes) put
get = do
magic <- get
as <- replicateM (fromIntegral $ asCount) get
let pool' = M.fromList $ zip [1..] pool
return $ Class magic minor major poolsize pool' af this super
- interfacesCount ifaces classFieldsCount classFields classMethodsCount classMethods asCount as
+ interfacesCount ifaces classFieldsCount classFields
+ classMethodsCount classMethods asCount (AP as)
-- | Field signature format
data FieldType =
put fieldName
put fieldSignature
put fieldAttributesCount
- forM_ fieldAttributes put
+ forM_ (attributesList fieldAttributes) put
get = do
af <- get
si <- get
n <- get
as <- replicateM (fromIntegral n) get
- return $ Field af ni si n as
+ return $ Field af ni si n (AP as)
-- | Class method format
data Method stage = Method {
- methodAccessFlags :: Attributes stage,
+ methodAccessFlags :: AccessFlags stage,
methodName :: Link stage B.ByteString,
methodSignature :: Link stage MethodSignature,
methodAttributesCount :: Word16,
put methodName
put methodSignature
put methodAttributesCount
- forM_ methodAttributes put
+ forM_ (attributesList methodAttributes) put
get = do
offset <- bytesRead
si <- get
n <- get
as <- replicateM (fromIntegral n) get
- return $ Method af ni si n as
+ return $ Method {
+ methodAccessFlags = af,
+ methodName = ni,
+ methodSignature = si,
+ methodAttributesCount = n,
+ methodAttributes = AP as }
-- | Any (class/ field/ method/ ...) attribute format.
-- Some formats specify special formats for @attributeValue@.
classFields = map (fieldInfo poolInfo) classFields,
classMethodsCount = fromIntegral (length classMethods),
classMethods = map (methodInfo poolInfo) classMethods,
- classAttributesCount = fromIntegral (M.size classAttributes),
- classAttributes = map (attrInfo poolInfo) (M.assocs classAttributes) }
+ classAttributesCount = fromIntegral $ arsize classAttributes,
+ classAttributes = to (arlist classAttributes) }
where
poolInfo = toCPInfo constsPool
+ to :: [(B.ByteString, B.ByteString)] -> Attributes Pointers
+ to pairs = AP (map (attrInfo poolInfo) pairs)
toCPInfo :: Pool Resolved -> Pool Pointers
toCPInfo pool = result
cpInfo (CDouble x) = CDouble x
cpInfo (CNameType n t) =
CNameType (force "name" $ poolIndex result n) (force "type" $ poolIndex result t)
- cpInfo (CUTF8 s) = CUTF8 (fromIntegral $ B.length s) s
- cpInfo (CUnicode s) = CUnicode (fromIntegral $ B.length s) s
+ cpInfo (CUTF8 s) = CUTF8 s
+ cpInfo (CUnicode s) = CUnicode s
-- | Find index of given string in the list of constants
poolIndex :: (Throws NoItemInPool e) => Pool Pointers -> B.ByteString -> EM e Word16
-poolIndex list name = case findIndex test list of
+poolIndex list name = case findIndex test (M.elems list) of
Nothing -> throw (NoItemInPool name)
Just i -> return $ fromIntegral $ i+1
where
-- | Find index of given string in the list of constants
poolClassIndex :: (Throws NoItemInPool e) => Pool Pointers -> B.ByteString -> EM e Word16
-poolClassIndex list name = case findIndex checkString list of
+poolClassIndex list name = case findIndex checkString (M.elems list) of
Nothing -> throw (NoItemInPool name)
- Just i -> case findIndex (checkClass $ fromIntegral $ i+1) list of
+ Just i -> case findIndex (checkClass $ fromIntegral $ i+1) (M.elems list) of
Nothing -> throw (NoItemInPool $ i+1)
Just j -> return $ fromIntegral $ j+1
where
poolNTIndex list x@(NameType n t) = do
ni <- poolIndex list n
ti <- poolIndex list (byteString t)
- case findIndex (check ni ti) list of
+ case findIndex (check ni ti) (M.elems list) of
Nothing -> throw (NoItemInPool x)
Just i -> return $ fromIntegral (i+1)
where
fieldInfo :: Pool Pointers -> Field Resolved -> Field Pointers
fieldInfo pool (Field {..}) = Field {
- fieldAccessFlags = access2word16 fieldAccessFlags,
- fieldName = force "field name" $ poolIndex pool fieldName,
- fieldSignature = force "signature" $ poolIndex pool (encode fieldSignature),
- fieldAttributesCount = fromIntegral (M.size fieldAttributes),
- fieldAttributes = map (attrInfo pool) (M.assocs fieldAttributes) }
+ fieldAccessFlags = access2word16 fieldAccessFlags,
+ fieldName = force "field name" $ poolIndex pool fieldName,
+ fieldSignature = force "signature" $ poolIndex pool (encode fieldSignature),
+ fieldAttributesCount = fromIntegral (arsize fieldAttributes),
+ fieldAttributes = to (arlist fieldAttributes) }
+ where
+ to :: [(B.ByteString, B.ByteString)] -> Attributes Pointers
+ to pairs = AP (map (attrInfo pool) pairs)
methodInfo :: Pool Pointers -> Method Resolved -> Method Pointers
methodInfo pool (Method {..}) = Method {
- methodAccessFlags = access2word16 methodAccessFlags,
- methodName = force "method name" $ poolIndex pool methodName,
- methodSignature = force "method sig" $ poolIndex pool (encode methodSignature),
- methodAttributesCount = fromIntegral (M.size methodAttributes),
- methodAttributes = map (attrInfo pool) (M.assocs methodAttributes) }
+ methodAccessFlags = access2word16 methodAccessFlags,
+ methodName = force "method name" $ poolIndex pool methodName,
+ methodSignature = force "method sig" $ poolIndex pool (encode methodSignature),
+ methodAttributesCount = fromIntegral (arsize methodAttributes),
+ methodAttributes = to (arlist methodAttributes) }
+ where
+ to :: [(B.ByteString, B.ByteString)] -> Attributes Pointers
+ to pairs = AP (map (attrInfo pool) pairs)
-attrInfo :: Pool Pointers -> (B.ByteString, B.ByteString) -> Attributes Pointers
+attrInfo :: Pool Pointers -> (B.ByteString, B.ByteString) -> Attribute
attrInfo pool (name, value) = Attribute {
attributeName = force "attr name" $ poolIndex pool name,
attributeLength = fromIntegral (B.length value),
constantPoolArray :: Pool Pointers -> Pool Resolved
constantPoolArray ps = pool
where
- pool :: Pool
+ pool :: Pool Resolved
pool = M.map convert ps
- n = fromIntegral $ length ps
+ n = fromIntegral $ M.size ps
convertNameType :: (HasSignature a, Binary (Signature a)) => Word16 -> NameType a
convertNameType i =
convert (CLong x) = CLong (fromIntegral x)
convert (CDouble x) = CDouble x
convert (CNameType i j) = CNameType (getString $ pool ! i) (getString $ pool ! j)
- convert (CUTF8 _ bs) = CUTF8 bs
- convert (CUnicode _ bs) = CUnicode bs
+ convert (CUTF8 bs) = CUTF8 bs
+ convert (CUnicode bs) = CUnicode bs
convertAccess :: AccessFlags Pointers -> AccessFlags Resolved
convertAccess w = S.fromList $ concat $ zipWith (\i f -> if testBit w i then [f] else []) [0..] $ [
fieldAccessFlags = convertAccess fieldAccessFlags,
fieldName = getString $ pool ! fieldName,
fieldSignature = decode $ getString $ pool ! fieldSignature,
+ fieldAttributesCount = fromIntegral (apsize fieldAttributes),
fieldAttributes = convertAttrs pool fieldAttributes }
convertMethod :: Pool Resolved -> Method Pointers -> Method Resolved
methodAccessFlags = convertAccess methodAccessFlags,
methodName = getString $ pool ! methodName,
methodSignature = decode $ getString $ pool ! methodSignature,
+ methodAttributesCount = fromIntegral (apsize methodAttributes),
methodAttributes = convertAttrs pool methodAttributes }
convertAttrs :: Pool Resolved -> Attributes Pointers -> Attributes Resolved
-convertAttrs pool attrs = M.fromList $ map go attrs
+convertAttrs pool (AP attrs) = AR (M.fromList $ map go attrs)
where
+ go :: Attribute -> (B.ByteString, B.ByteString)
go (Attribute {..}) = (getString $ pool ! attributeName,
attributeValue)
-- | Try to get object attribute by name
attrByName :: (HasAttributes a) => a Resolved -> B.ByteString -> Maybe B.ByteString
-attrByName x name = M.lookup name (attributes x)
+attrByName x name =
+ let (AR m) = attributes x
+ in M.lookup name m
-- | Try to get Code for class method (no Code for interface methods)
methodCode :: Class Resolved
import Text.Printf
import JVM.Types
+import JVM.ClassFile
import JVM.Converter
import JVM.Assembler
-dumpClass :: Class -> IO ()
+dumpClass :: Class Resolved -> IO ()
dumpClass cls = do
putStr "Class: "
- B.putStrLn (this cls)
+ B.putStrLn (thisClass cls)
putStrLn "Constants pool:"
- forM_ (M.assocs $ constantPool cls) $ \(i, c) ->
+ forM_ (M.assocs $ constsPool cls) $ \(i, c) ->
putStrLn $ printf " #%d:\t%s" i (show c)
putStrLn "Methods:"
- forM_ (methods cls) $ \m -> do
+ forM_ (classMethods cls) $ \m -> do
putStr ">> Method "
B.putStr (methodName m)
print (methodSignature m)
data GState = GState {
generated :: [Instruction],
- currentPool :: Pool,
- doneMethods :: [Method],
- currentMethod :: Maybe Method}
+ currentPool :: Pool Resolved,
+ doneMethods :: [Method Resolved],
+ currentMethod :: Maybe (Method Resolved)}
deriving (Eq,Show)
emptyGState = GState {
type Generate a = State GState a
-appendPool :: Constant -> Pool -> (Pool, Word16)
+appendPool :: Constant Resolved -> Pool Resolved -> (Pool Resolved, Word16)
appendPool c pool =
let size = fromIntegral (M.size pool)
pool' = M.insert size c pool
in (pool', size)
-addItem :: Constant -> Generate Word16
+addItem :: Constant Resolved -> Generate Word16
addItem c = do
pool <- St.gets currentPool
case lookupPool c pool of
St.put $ st {currentPool = pool'}
return (i+1)
-lookupPool :: Constant -> Pool -> Maybe Word16
+lookupPool :: Constant Resolved -> Pool Resolved -> Maybe Word16
lookupPool c pool =
fromIntegral `fmap` findIndex (== c) (M.elems pool)
let bsig = encode c
addItem (CUTF8 bsig)
-addToPool :: Constant -> Generate Word16
+addToPool :: Constant Resolved -> Generate Word16
addToPool c@(CClass str) = do
addItem (CUTF8 str)
addItem c
i0 :: Instruction -> Generate ()
i0 = putInstruction
-i1 :: (Word16 -> Instruction) -> Constant -> Generate ()
+i1 :: (Word16 -> Instruction) -> Constant Resolved -> Generate ()
i1 fn c = do
ix <- addToPool c
i0 (fn ix)
-i8 :: (Word8 -> Instruction) -> Constant -> Generate ()
+i8 :: (Word8 -> Instruction) -> Constant Resolved -> Generate ()
i8 fn c = do
ix <- addToPool c
i0 (fn $ fromIntegral ix)
addSig sig
st <- St.get
let method = Method {
- methodAccess = S.fromList flags,
+ methodAccessFlags = S.fromList flags,
methodName = name,
methodSignature = sig,
- methodAttrs = M.empty }
+ methodAttributesCount = 0,
+ methodAttributes = AR M.empty }
St.put $ st {generated = [],
currentMethod = Just method }
case m of
Nothing -> fail "endMethod without startMethod!"
Just method -> do
- let method' = method {methodAttrs = M.fromList [("Code", encodeMethod code)] }
+ let method' = method {methodAttributes = AR $ M.fromList [("Code", encodeMethod code)],
+ methodAttributesCount = 1}
st <- St.get
St.put $ st {generated = [],
currentMethod = Nothing,
codeExceptionsN = 0,
codeExceptions = [],
codeAttrsN = 0,
- codeAttributes = [] }
+ codeAttributes = AP [] }
where
len = fromIntegral $ B.length $ encodeInstructions (generated st)
addToPool (CClass name)
addToPool (CString "Code")
-generate :: B.ByteString -> Generate () -> Class
+generate :: B.ByteString -> Generate () -> Class Resolved
generate name gen =
let generator = do
initClass name
res = execState generator emptyGState
code = genCode res
in Class {
- constantPool = currentPool res,
- classAccess = S.fromList [ACC_PUBLIC, ACC_STATIC],
- this = name,
- super = Just "java/lang/Object",
- implements = [],
- fields = [],
- methods = doneMethods res,
- classAttrs = M.empty }
+ magic = 0xCAFEBABE,
+ minorVersion = 0,
+ majorVersion = 50,
+ constsPoolSize = fromIntegral $ M.size (currentPool res),
+ constsPool = currentPool res,
+ accessFlags = S.fromList [ACC_PUBLIC, ACC_STATIC],
+ thisClass = name,
+ superClass = "java/lang/Object",
+ interfacesCount = 0,
+ interfaces = [],
+ classFieldsCount = 0,
+ classFields = [],
+ classMethodsCount = fromIntegral $ length (doneMethods res),
+ classMethods = doneMethods res,
+ classAttributesCount = 0,
+ classAttributes = AR M.empty }
import System.Environment
import qualified Data.ByteString.Lazy as B
import Text.Printf
+import qualified Data.Map as M
import JVM.Types
import JVM.ClassFile
case args of
[clspath] -> do
clsFile <- decodeFile clspath
- putStrLn $ showListIx $ constsPool clsFile
+ putStrLn $ showListIx $ M.elems $ constsPool (clsFile :: Class Pointers)
cls <- parseClassFile clspath
dumpClass cls
_ -> error "Synopsis: dump-class File.class"
import System.Environment
import qualified Data.ByteString.Lazy as B
import Text.Printf
+import qualified Data.Map as M
import JVM.Types
import JVM.ClassFile
case args of
[clspath,outpath] -> do
cls <- parseClassFile clspath
- clsfile <- decodeFile clspath :: IO ClassFile
+ clsfile <- decodeFile clspath :: IO (Class Pointers)
dumpClass cls
- putStrLn $ "Source pool:\n" ++ showListIx (constsPool clsfile)
+ putStrLn $ "Source pool:\n" ++ showListIx (M.elems $ constsPool clsfile)
let result = classFile cls
- putStrLn $ "Result pool:\n" ++ showListIx (constsPool result)
+ putStrLn $ "Result pool:\n" ++ showListIx (M.elems $ constsPool result)
B.writeFile outpath (encodeClass cls)
_ -> error "Synopsis: rebuild-class File.class Output.class"