-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
61 lines (52 loc) · 2.35 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import Bril.Lang.AST
import Bril.Lang.Parse
import Bril.Optim.DCE
import Bril.Optim.Loop
import Bril.Structure.CFG
import Bril.Structure.SSA
import Data.Aeson
import Data.Maybe
import Data.Tree
import System.Environment
import qualified Data.ByteString.Lazy as B
import qualified Data.HashMap.Strict as M
import qualified Data.HashSet as S
-- | print a set of objects
identSet :: Show a => S.HashSet a -> String
identSet s = if null s then "∅" else foldl1 cat $ S.map show s
where
cat x y = x ++ ", " ++ y
-- | print the name of a function
printName :: Function -> IO ()
printName (Function n _ _ _) = putStrLn $ show n ++ ":"
-- | print an entry in a map
printLine :: Show a => (a, S.HashSet a) -> IO ()
printLine (i, ss) = putStrLn $ " " ++ show i ++ ": " ++ identSet ss
-- | print the dominators of each block in a function
printDoms :: Function -> IO ()
printDoms f = mapM_ printLine . M.toList . dominators $ mkCFG f
-- | append two spaces to every line in the string
appendSpaces :: String -> String
appendSpaces x = unlines $ (" " ++ ) <$> lines x
-- | print the domination tree of a function
printDomTree :: Function -> IO ()
printDomTree f = putStrLn . appendSpaces . drawTree . (show <$>) . dominationTree $ mkCFG f
-- | print the domination frontier of each block in a function
printDomFront :: Function -> IO ()
printDomFront f = mapM_ printLine . M.toList . dominationFrontier $ mkCFG f
-- | perform the given action with the program
action :: Program -> String -> IO ()
action (Program fs) "ssa" = B.putStr . encode . Program $ tdce . ssa . tdce <$> fs
action (Program fs) "nossa" = B.putStr . encode . Program $ tdce . ssa' . tdce <$> fs
action (Program fs) "tdce" = B.putStr . encode . Program $ tdce <$> fs
action (Program fs) "licm" = B.putStr . encode . Program $ licm <$> fs
action (Program fs) "tree" = mapM_ (\x -> printName x *> printDomTree x) fs
action (Program fs) "front" = mapM_ (\x -> printName x *> printDomFront x) fs
action (Program fs) "doms" = mapM_ (\x -> printName x *> printDoms x) fs
action (Program fs) _ = return ()
main :: IO ()
main = do bytes <- B.getContents
args <- listToMaybe <$> getArgs
case eitherDecode bytes of
Left e -> putStrLn e
Right p -> action p $ fromMaybe "" args