1 {-# LANGUAGE OverloadedStrings #-}
2 module Mate.BasicBlocks(
13 import qualified Data.Map as H
14 import System.Environment
15 import qualified Data.Map as M
16 import qualified Data.ByteString.Lazy as B
28 type Name = String -- use "virtual register id" instead?
29 data Type = JInt | JFloat -- add more
30 type Variable = (Type,Name)
33 -- Represents a CFG node
34 data BasicBlock = BasicBlock {
35 -- inputs :: [Variable],
36 -- outputs :: [Variable],
37 code :: [Instruction],
40 -- describes (leaving) edges of a CFG node
41 data BBEnd = Return | OneTarget BlockID | TwoTarget BlockID BlockID deriving Show
43 type MapBB = H.Map BlockID BasicBlock
45 -- for immediate representation for determine BBs
46 type Offset = (Int, Maybe BBEnd) -- (offset in bytecode, offset to jump target)
47 type OffIns = (Offset, Instruction)
50 printMapBB :: Maybe MapBB -> IO ()
51 printMapBB Nothing = putStrLn "No BasicBlock"
52 printMapBB (Just hmap) = do
54 let keys = fst $ unzip $ H.toList hmap
55 mapM_ (putStr . (flip (++)) ", " . show) keys
56 putStrLn "\n\nBasicBlocks:"
59 printMapBB' :: [BlockID] -> MapBB -> IO ()
60 printMapBB' [] _ = return ()
61 printMapBB' (i:is) hmap = case H.lookup i hmap of
63 putStrLn $ "Block " ++ (show i)
64 mapM_ putStrLn (map ((++) "\t" . show) $ code bb)
67 OneTarget t1 -> putStrLn $ "Sucessor: " ++ (show t1) ++ "\n"
68 TwoTarget t1 t2 -> putStrLn $ "Sucessor: " ++ (show t1) ++ ", " ++ (show t2) ++ "\n"
70 Nothing -> error $ "BlockID " ++ show i ++ " not found."
72 testInstance :: String -> B.ByteString -> IO ()
73 testInstance cf method = do
74 hmap <- parseMethod cf method
77 test_01 = testInstance "./tests/Fib.class" "fib"
78 test_02 = testInstance "./tests/While.class" "f"
79 test_03 = testInstance "./tests/While.class" "g"
82 parseMethod :: String -> B.ByteString -> IO (Maybe MapBB)
83 parseMethod clspath method = do
84 cls <- parseClassFile clspath
85 return $ testCFG $ lookupMethod method cls
88 testCFG :: Maybe (Method Resolved) -> Maybe MapBB
89 testCFG (Just m) = case attrByName m "Code" of
91 Just bytecode -> let code = decodeMethod bytecode
92 instructions = codeInstructions code
93 in Just $ buildCFG instructions
97 buildCFG :: [Instruction] -> MapBB
98 buildCFG xs = buildCFG' H.empty xs' xs'
101 xs' = calculateInstructionOffset xs
103 buildCFG' :: MapBB -> [OffIns] -> [OffIns] -> MapBB
104 buildCFG' hmap [] _ = hmap
105 buildCFG' hmap (((off, entry), _):xs) insns = buildCFG' (insertlist entryi hmap) xs insns
107 insertlist :: [BlockID] -> MapBB -> MapBB
108 insertlist [] hmap = hmap
109 insertlist (x:xs) hmap = insertlist xs newhmap
111 newhmap = if H.member x hmap then hmap else H.insert x value hmap
112 value = parseBasicBlock x insns
115 entryi = (if off == 0 then [0] else []) ++ -- also consider the entrypoint
117 Just (TwoTarget t1 t2) -> [t1, t2]
118 Just (OneTarget t) -> [t]
123 parseBasicBlock :: Int -> [OffIns] -> BasicBlock
124 parseBasicBlock i insns = BasicBlock insonly endblock
126 startlist = dropWhile (\((x,_),_) -> x < i) insns
127 (Just ((_,(Just endblock)),_), is) = takeWhilePlusOne validins startlist
128 insonly = snd $ unzip is
130 -- also take last (non-matched) element and return it
131 takeWhilePlusOne :: (a -> Bool) -> [a] -> (Maybe a,[a])
132 takeWhilePlusOne _ [] = (Nothing,[])
133 takeWhilePlusOne p (x:xs)
134 | p x = let (lastins, list) = takeWhilePlusOne p xs in (lastins, (x:list))
135 | otherwise = (Just x,[x])
137 validins :: ((Int, Maybe BBEnd), Instruction) -> Bool
138 validins ((_,x),_) = case x of Just _ -> False; Nothing -> True
141 calculateInstructionOffset :: [Instruction] -> [OffIns]
142 calculateInstructionOffset = cio' (0, Nothing)
144 newoffset :: Instruction -> Int -> Offset
145 newoffset x off = (off + (fromIntegral $ B.length $ encodeInstructions [x]), Nothing)
147 addW16Signed :: Int -> Word16 -> Int
148 addW16Signed i w16 = i + (fromIntegral s16)
149 where s16 = (fromIntegral w16) :: Int16
151 cio' :: Offset -> [Instruction] -> [OffIns]
153 -- TODO(bernhard): add more instruction with offset (IF_ACMP, JSR, ...)
154 cio' (off,_) (x:xs) = case x of
155 IF _ w16 -> twotargets w16
156 IF_ICMP _ w16 -> twotargets w16
157 GOTO w16 -> onetarget w16
159 _ -> ((off, Nothing), x):next
161 notarget = ((off, Just Return), x):next
162 onetarget w16 = ((off, Just $ OneTarget $ (off `addW16Signed` w16)), x):next
163 twotargets w16 = ((off, Just $ TwoTarget (off + 3) (off `addW16Signed` w16)), x):next
164 next = cio' (newoffset x off) xs