1 {-# LANGUAGE OverloadedStrings #-}
2 module BasicBlocks where
5 import System.Environment
6 import qualified Data.Map as M
7 import qualified Data.ByteString.Lazy as B
17 type Name = String -- use "virtual register id" instead?
18 data Type = JInt | JFloat -- add more
19 type Variable = (Type,Name)
21 -- Represents a CFG node
22 data BasicBlock = BasicBlock {
24 outputs :: [Variable],
25 code :: [Instruction] }
27 -- Represents a Control-Flow-Graph as
28 -- Adjacency list (add matrix representation if appropriate)
29 type CFList = [(BasicBlock, [BasicBlock])]
35 [clspath] -> parseFile clspath "fib" -- TODO
36 _ -> error "Synopsis: dump-class File.class"
39 parseFile :: String -> B.ByteString -> IO ()
40 parseFile clspath method = do
41 --clsFile <- decodeFile clspath
42 --putStrLn $ showListIx $ M.assocs $ constsPool (clsFile :: Class Pointers)
43 cls <- parseClassFile clspath
45 let mainmethod = lookupMethod method cls -- "main|([Ljava/lang/String;)V" cf
46 mapM_ putStrLn (testCFG mainmethod)
48 test_01 = parseFile "./tests/Fib.class" "fib"
49 test_02 = parseFile "./tests/While.class" "f"
50 test_03 = parseFile "./tests/While.class" "g"
53 testCFG :: Maybe (Method Resolved) -> [String]
54 testCFG (Just m) = case attrByName m "Code" of
55 Nothing -> error "no code"
56 Just bytecode -> let code = decodeMethod bytecode
57 instructions = codeInstructions code
58 in buildCFG instructions
59 testCFG _ = error "no method to build cfg"
62 buildCFG :: [Instruction] -> [String]
63 buildCFG xs = map show xs