-type Name = String -- use "virtual register id" instead?
-data Type = JInt | JFloat -- add more
-type Variable = (Type,Name)
-
--- Represents a CFG node
-data BasicBlock = BasicBlock {
- inputs :: [Variable],
- outputs :: [Variable],
- code :: [Instruction] }
-
--- Represents a Control-Flow-Graph as
--- Adjacency list (add matrix representation if appropriate)
-type CFList = [(BasicBlock, [BasicBlock])]
-
-
-main = do
- args <- getArgs
- case args of
- [clspath] -> parseFile clspath "fib" -- TODO
- _ -> error "Synopsis: dump-class File.class"
-
-
-parseFile :: String -> B.ByteString -> IO ()
-parseFile clspath method = do
- --clsFile <- decodeFile clspath
- --putStrLn $ showListIx $ M.assocs $ constsPool (clsFile :: Class Pointers)
- cls <- parseClassFile clspath
- --dumpClass cls
- let mainmethod = lookupMethod method cls -- "main|([Ljava/lang/String;)V" cf
- mapM_ putStrLn (testCFG mainmethod)
-
-test_01 = parseFile "./tests/Fib.class" "fib"
-test_02 = parseFile "./tests/While.class" "f"
-test_03 = parseFile "./tests/While.class" "g"
-
+-- (offset in bytecode, offset to jump target, ins)
+type OffIns = (Int, Maybe BBEnd, Instruction)
+
+type Target = BlockID
+type BBState = S.Set Target
+type AnalyseState = State BBState [OffIns]
+
+
+noException :: B.ByteString
+noException = B.empty
+
+emptyBasicBlock :: BasicBlock
+emptyBasicBlock = BasicBlock
+ { code = []
+ , exception = noException
+ , successor = Return }
+
+printMapBB :: MapBB -> IO ()
+printMapBB hmap = do
+ printfBb "BlockIDs: "
+ let keys = M.keys hmap
+ mapM_ (printfBb . flip (++) ", " . show) keys
+ printfBb "\n\nBasicBlocks:\n"
+ printMapBB' keys hmap
+ where
+ printMapBB' :: [BlockID] -> MapBB -> IO ()
+ printMapBB' [] _ = return ()
+ printMapBB' (i:is) hmap' = case M.lookup i hmap' of
+ Just bb -> do
+ printfBb $ "Block " ++ show i ++ "\n"
+ mapM_ (printfBb . flip (++) "\n" . (++) "\t" . show) $ code bb
+ printfBb $ case successor bb of
+ Return -> ""
+ FallThrough t1 -> "Sucessor: " ++ show t1 ++ "\n"
+ OneTarget t1 -> "Sucessor: " ++ show t1 ++ "\n"
+ TwoTarget t1 t2 -> "Sucessor: " ++ show t1 ++ ", " ++ show t2 ++ "\n"
+ printMapBB' is hmap
+ Nothing -> error $ "BlockID " ++ show i ++ " not found."
+
+{-
+testInstance :: String -> B.ByteString -> MethodSignature -> IO ()
+testInstance cf method sig = do
+ cls <- parseClassFile cf
+ hmap <- parseMethod cls method sig
+ printMapBB hmap
+
+test_main :: IO ()
+test_main = do
+ test_01
+ test_02
+ test_03
+ test_04
+
+test_01, test_02, test_03, test_04 :: IO ()
+test_01 = testInstance "./tests/Fib.class" "fib"
+test_02 = testInstance "./tests/While.class" "f"
+test_03 = testInstance "./tests/While.class" "g"
+test_04 = testInstance "./tests/Fac.class" "fac"
+-}
+
+
+parseMethod :: Class Direct -> B.ByteString -> MethodSignature -> IO RawMethod
+parseMethod cls methodname sig = do
+ let method = fromMaybe
+ (error $ "method " ++ (show . toString) methodname ++ " not found")
+ (lookupMethodSig methodname sig cls)
+ let codeseg = fromMaybe
+ (error $ "codeseg " ++ (show . toString) methodname ++ " not found")
+ (attrByName method "Code")
+ let decoded = decodeMethod codeseg
+ let mapbb = testCFG decoded
+ let locals = fromIntegral (codeMaxLocals decoded)
+ let stacks = fromIntegral (codeStackSize decoded)
+ let codelen = fromIntegral (codeLength decoded)
+ let methoddirect = methodInfoToMethod (MethodInfo methodname "" sig) cls
+ let isStatic = methodIsStatic methoddirect
+ let nametype = methodNameType methoddirect
+ let argscount = methodGetArgsCount nametype + (if isStatic then 0 else 1)
+
+ -- TODO: remove ;-)
+ -- small example how to get information about
+ -- exceptions of a method
+ let (Just m) = lookupMethodSig methodname sig cls
+ case attrByName m "Code" of
+ Nothing ->
+ printfBb $ printf "exception: no handler for this method\n"
+ Just exceptionstream ->
+ printfBb $ printf "exception: \"%s\"\n" (show $ codeExceptions $ decodeMethod exceptionstream)
+ -- [/remove]
+ let msig = methodSignature method
+ printfBb $ printf "BB: analysing \"%s\"\n" $ toString (methodname `B.append` ": " `B.append` encode msig)
+ printMapBB mapbb
+ return $ RawMethod mapbb locals stacks argscount codelen
+
+
+testCFG :: Code -> MapBB
+testCFG c = buildCFG (codeInstructions c) (codeExceptions c)
+
+buildCFG :: [Instruction] -> [CodeException] -> MapBB
+buildCFG xs excps = execState (mapM (buildCFG' offins) $ alltargets ++ handlerEntries) M.empty
+ where
+ (offins, targets) = runState (calculateInstructionOffset tryBlocks xs) S.empty
+ alltargets = S.toList $ S.insert 0 targets
+ tryBlocks = map (fromIntegral . eStartPC) excps
+ handlerEntries = map (fromIntegral . eHandlerPC) excps
+
+buildCFG' :: [OffIns] -> Int -> State MapBB ()
+buildCFG' insns off = do
+ let value = parseBasicBlock off insns
+ modify (M.insert off value)
+
+parseBasicBlock :: Int -> [OffIns] -> BasicBlock
+parseBasicBlock i insns = emptyBasicBlock { code = insonly, successor = endblock }
+ where
+ (lastblock, is) = takeWhilePlusOne validins omitins insns
+ (_, _, insonly) = unzip3 is
+ (_, Just endblock, _) = fromJust lastblock