-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.hs
100 lines (90 loc) · 3.41 KB
/
Utils.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module Utils where
import Control.Monad
import Data.Either
import Data.List
import Data.List.Split
import Data.Map as Map
import Data.Maybe
import GraphParser
import IR
import LinterTypes
import qualified Parser as P
import qualified ParserCombinators as P
import State (State)
import qualified State as S
import System.Console.Pretty (Color (..), Style (..), bgColor, color,
style, supportsPretty)
import Text.Printf
import Text.Read
-------------------- general utils ----------------------
-- A helper method for the getConstants method.
getConstantsHelper :: [Statement] -> Map Variable (Either Int String)
getConstantsHelper [] = Map.empty
getConstantsHelper (x:xs) =
case x of
(Assign (Assignment (VT v t:_) f _ _))
->
case f of
(Constant (Just y)) -> Map.insert v y (getConstantsHelper xs)
(Constant _) -> getConstantsHelper xs
_ -> Map.empty
_ -> Map.empty
-- getConstants gets a list of constants declared at the beginning of the SSA file.
getConstants :: Graph -> Map Variable (Either Int String)
getConstants (Graph args l returns) = getConstantsHelper l
-- A helper method for the getLineAnnots method.
getLineAnnotsHelper :: [Statement] -> Map Variable LineAnnot
getLineAnnotsHelper [] = Map.empty
getLineAnnotsHelper (x:xs) =
case x of
(Assign a@(Assignment (VT v t:_) f _ (Just annot))) ->
Map.insert v annot (getLineAnnotsHelper xs)
(If a@(Assignment vartypes f _ (Just annot)) (Block _ l1 _) (Block _ l2 _)) ->
Map.union (getLineAnnotsHelper l1) $
Map.union
(getLineAnnotsHelper l2)
(Data.List.foldr
(\(VT v t) acc -> Map.insert v annot acc)
(getLineAnnotsHelper xs)
vartypes)
(Loop a@(Assignment vartypes f _ (Just annot)) (Block _ l ret)) ->
let recd = getLineAnnotsHelper xs
in Map.union
(getLineAnnotsHelper l)
(Data.List.foldr
(\(VT v t) acc -> Map.insert v annot acc)
recd
vartypes)
_ -> getLineAnnotsHelper xs
-- getLineAnnots creates a map of all variables associated with their line annotations.
-- Every variable is guaranteed to only appear once due to the nature of SSA.
getLineAnnots :: Graph -> Map Variable LineAnnot
getLineAnnots (Graph args l _) = getLineAnnotsHelper l
-- Converts a string like "tensor.1" to "tensor".
getRealVarName :: Variable -> Variable
getRealVarName s =
let s' = Data.List.Split.splitOn "." s
in case s' of
[] -> error "invalid variable name"
(x:_) ->
if isJust (readMaybe x :: Maybe Int)
then "<anonymous tensor>"
else x
-- Composes multiple state transformers into one.
composeStatesUnit :: [State s ()] -> State s ()
composeStatesUnit [] = return ()
composeStatesUnit (x:xs) = do
x
composeStatesUnit xs
-- Serializes a line annotation.
serializeAnnot :: Maybe LineAnnot -> String
serializeAnnot Nothing = ""
serializeAnnot (Just (LineAnnot str lineNum colNum)) =
printf "%s:%d:%d" str lineNum colNum
-- Prints several lint messages to the terminal.
printMessages :: [LintMessage] -> IO ()
printMessages =
mapM_
(\x -> do
putStr $ color Magenta "warning: "
putStrLn x)