X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=Mate%2FBasicBlocks.hs;h=fbb61f7a06ecf6a471d5b33385953f58228ee19b;hb=f82dbecc763818452667ac568da96b7c5dd7cc97;hp=bde2697d67634e4a896813633097dde1a276eb7d;hpb=938e054590e5e6b1a158f496678b7dfc3d61d943;p=mate.git diff --git a/Mate/BasicBlocks.hs b/Mate/BasicBlocks.hs index bde2697..fbb61f7 100644 --- a/Mate/BasicBlocks.hs +++ b/Mate/BasicBlocks.hs @@ -1,12 +1,15 @@ +{-# LANGUAGE CPP #-} {-# LANGUAGE OverloadedStrings #-} +#include "debug.h" module Mate.BasicBlocks( BlockID, BasicBlock (..), BBEnd (..), MapBB, +#ifdef DBG_BB printMapBB, +#endif parseMethod, - test_main, testCFG -- added by hs to perform benches from outside )where @@ -20,20 +23,25 @@ import JVM.ClassFile import JVM.Converter import JVM.Assembler -import Mate.Utilities import Mate.Types +import Mate.Debug +import Mate.Utilities +#ifdef DEBUG +import Text.Printf +#endif -- for immediate representation to determine BBs type Offset = (Int, Maybe BBEnd) -- (offset in bytecode, offset to jump target) type OffIns = (Offset, Instruction) +#ifdef DBG_BB printMapBB :: Maybe MapBB -> IO () printMapBB Nothing = putStrLn "No BasicBlock" printMapBB (Just hmap) = do putStr "BlockIDs: " - let keys = fst $ unzip $ M.toList hmap + let keys = M.keys hmap mapM_ (putStr . (flip (++)) ", " . show) keys putStrLn "\n\nBasicBlocks:" printMapBB' keys hmap @@ -51,13 +59,18 @@ printMapBB (Just hmap) = do TwoTarget t1 t2 -> putStrLn $ "Sucessor: " ++ (show t1) ++ ", " ++ (show t2) ++ "\n" printMapBB' is hmap Nothing -> error $ "BlockID " ++ show i ++ " not found." +#endif -testInstance :: String -> B.ByteString -> IO () -testInstance cf method = do +#if 0 +#ifdef DBG_BB +testInstance :: String -> B.ByteString -> MethodSignature -> IO () +testInstance cf method sig = do cls <- parseClassFile cf - hmap <- parseMethod cls method + hmap <- parseMethod cls method sig printMapBB hmap +#endif +#ifdef DBG_BB test_main :: IO () test_main = do test_01 @@ -70,21 +83,33 @@ 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 Resolved -> B.ByteString -> IO (Maybe MapBB) -parseMethod cls method = do - putStr "BB: analysing: " - let msig = methodSignature $ (classMethods cls) !! 1 - B.putStrLn (method `B.append` ": " `B.append` (encode msig)) - return $ testCFG $ lookupMethod method cls - - -testCFG :: Maybe (Method Resolved) -> Maybe MapBB -testCFG (Just m) = case attrByName m "Code" of - Nothing -> Nothing - Just bytecode -> Just $ buildCFG $ codeInstructions $ decodeMethod bytecode -testCFG _ = Nothing +#endif +#endif + + +parseMethod :: Class Direct -> B.ByteString -> MethodSignature -> IO (Maybe MapBB) +parseMethod cls method sig = do + let maybe_bb = testCFG $ lookupMethodSig method sig cls + let msig = methodSignature $ classMethods cls !! 1 + printfBb "BB: analysing \"%s\"\n" $ toString (method `B.append` ": " `B.append` encode msig) +#ifdef DBG_BB + printMapBB maybe_bb +#endif + -- small example how to get information about + -- exceptions of a method + -- TODO: remove ;-) + let (Just m) = lookupMethodSig method sig cls + case attrByName m "Code" of + Nothing -> printfBb "exception: no handler for this method\n" + Just exceptionstream -> printfBb "exception: \"%s\"\n" (show $ codeExceptions $ decodeMethod exceptionstream) + return maybe_bb + + +testCFG :: Maybe (Method Direct) -> Maybe MapBB +testCFG m = do + m' <- m + bytecode <- attrByName m' "Code" + return $ buildCFG $ codeInstructions $ decodeMethod bytecode buildCFG :: [Instruction] -> MapBB @@ -100,7 +125,7 @@ markBackwardTargets :: [OffIns] -> [OffIns] markBackwardTargets [] = [] markBackwardTargets (x:[]) = [x] markBackwardTargets insns@(x@((x_off,x_bbend),x_ins):y@((y_off,_),_):xs) = - (x_new):(markBackwardTargets (y:xs)) + x_new:markBackwardTargets (y:xs) where x_new = if isTarget then checkX y_off else x checkX w16 = case x_bbend of @@ -109,8 +134,8 @@ markBackwardTargets insns@(x@((x_off,x_bbend),x_ins):y@((y_off,_),_):xs) = -- look through all remaining insns in the stream if there is a jmp to `y' isTarget = case find cmpOffset insns of Just _ -> True; Nothing -> False - cmpOffset ((_,(Just (OneTarget w16))),_) = w16 == y_off - cmpOffset ((_,(Just (TwoTarget _ w16))),_) = w16 == y_off + cmpOffset ((_,Just (OneTarget w16)),_) = w16 == y_off + cmpOffset ((_,Just (TwoTarget _ w16)),_) = w16 == y_off cmpOffset _ = False @@ -126,27 +151,27 @@ buildCFG' hmap (((off, entry), _):xs) insns = buildCFG' (insertlist entryi hmap) value = parseBasicBlock y insns entryi :: [BlockID] - entryi = (if off == 0 then [0] else []) ++ -- also consider the entrypoint - case entry of - Just (TwoTarget t1 t2) -> [t1, t2] - Just (OneTarget t) -> [t] - Just (FallThrough t) -> [t] - Just (Return) -> [] - Nothing -> [] + entryi = if off == 0 then 0:ys else ys -- also consider the entrypoint + where ys = case entry of + Just (TwoTarget t1 t2) -> [t1, t2] + Just (OneTarget t) -> [t] + Just (FallThrough t) -> [t] + Just Return -> [] + Nothing -> [] parseBasicBlock :: Int -> [OffIns] -> BasicBlock parseBasicBlock i insns = BasicBlock insonly endblock where startlist = dropWhile (\((x,_),_) -> x < i) insns - (Just ((_,(Just endblock)),_), is) = takeWhilePlusOne validins startlist + (Just ((_, Just endblock),_), is) = takeWhilePlusOne validins startlist insonly = snd $ unzip is -- also take last (non-matched) element and return it takeWhilePlusOne :: (a -> Bool) -> [a] -> (Maybe a,[a]) takeWhilePlusOne _ [] = (Nothing,[]) takeWhilePlusOne p (x:xs) - | p x = let (lastins, list) = takeWhilePlusOne p xs in (lastins, (x:list)) + | p x = let (lastins, list) = takeWhilePlusOne p xs in (lastins, x:list) | otherwise = (Just x,[x]) validins :: ((Int, Maybe BBEnd), Instruction) -> Bool @@ -157,11 +182,11 @@ calculateInstructionOffset :: [Instruction] -> [OffIns] calculateInstructionOffset = cio' (0, Nothing) where newoffset :: Instruction -> Int -> Offset - newoffset x off = (off + (fromIntegral $ B.length $ encodeInstructions [x]), Nothing) + newoffset x off = (off + fromIntegral (B.length $ encodeInstructions [x]), Nothing) addW16Signed :: Int -> Word16 -> Int - addW16Signed i w16 = i + (fromIntegral s16) - where s16 = (fromIntegral w16) :: Int16 + addW16Signed i w16 = i + fromIntegral s16 + where s16 = fromIntegral w16 :: Int16 cio' :: Offset -> [Instruction] -> [OffIns] cio' _ [] = [] @@ -170,6 +195,8 @@ calculateInstructionOffset = cio' (0, Nothing) IF _ w16 -> twotargets w16 IF_ICMP _ w16 -> twotargets w16 IF_ACMP _ w16 -> twotargets w16 + IFNONNULL w16 -> twotargets w16 + IFNULL w16 -> twotargets w16 GOTO w16 -> onetarget w16 IRETURN -> notarget ARETURN -> notarget @@ -177,6 +204,6 @@ calculateInstructionOffset = cio' (0, Nothing) _ -> ((off, Nothing), x):next where notarget = ((off, Just Return), x):next - onetarget w16 = ((off, Just $ OneTarget $ (off `addW16Signed` w16)), x):next + onetarget w16 = ((off, Just $ OneTarget (off `addW16Signed` w16)), x):next twotargets w16 = ((off, Just $ TwoTarget (off + 3) (off `addW16Signed` w16)), x):next next = cio' (newoffset x off) xs