summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2019-04-28 17:56:08 +0200
committertomsmeding <tom.smeding@gmail.com>2019-04-28 17:56:08 +0200
commitb6720c744c642048f93e04a3a2d4b6895d8b2c83 (patch)
treed3df7b199ff4e1943f34b5ec1f67d4af13f0c1fa
parentb2320404202ad3296480bd472a6a79f5e5427de8 (diff)
WIP dataflow analysis for lifetime analysis
-rw-r--r--DataFlow.hs38
-rw-r--r--lisphs.cabal1
2 files changed, 39 insertions, 0 deletions
diff --git a/DataFlow.hs b/DataFlow.hs
new file mode 100644
index 0000000..78ec018
--- /dev/null
+++ b/DataFlow.hs
@@ -0,0 +1,38 @@
+module DataFlow(dataFlowAnalysis, Direction(..)) where
+
+import Data.Function
+import Data.List
+import qualified Data.Map.Strict as Map
+
+
+data Direction = Forwards | Backwards
+ deriving (Show, Eq)
+
+dataFlowAnalysis
+ :: Ord id
+ -- | The direction of the dataflow analysis
+ => Direction
+ -- | The control flow graph of basic blocks
+ -> Map.Map id [id]
+ -- | The initial values
+ -> (id -> state)
+ -- | The transfer function; note that the meaning of the states
+ -- depends on the direction
+ -> (state -> id -> state)
+ -- | For each block, the incoming state and the outgoing state
+ -> Map.Map id (state, state)
+dataFlowAnalysis Backwards cfg initF transF =
+ Map.map (\(a, b) -> (b, a)) $
+ dataFlowAnalysis Forwards (reverseGraph cfg) initF transF
+dataFlowAnalysis Forwards cfg initF transF = undefined
+
+
+reverseGraph :: Ord a => Map.Map a [a] -> Map.Map a [a]
+reverseGraph graph =
+ Map.fromList
+ . map ((,) <$> (fst . head) <*> map snd)
+ . groupBy ((==) `on` fst)
+ . sortBy (compare `on` fst)
+ $ [(to, from)
+ | (from, tos) <- Map.assocs graph
+ , to <- tos]
diff --git a/lisphs.cabal b/lisphs.cabal
index c608571..66dc3c8 100644
--- a/lisphs.cabal
+++ b/lisphs.cabal
@@ -16,6 +16,7 @@ executable lisp
other-modules:
AST
Compiler
+ DataFlow
Intermediate
Lower
Optimiser