{-# LANGUAGE ScopedTypeVariables #-}
-{-# OPTIONS -Wall -fno-warn-name-shadowing #-}
module MkZipCfg
- ( AGraph, (<*>), emptyAGraph, withFreshLabel
+ ( AGraph, (<*>), emptyAGraph, withFreshLabel, withUnique
, mkMiddle, mkMiddles, mkLast, mkZTail, mkBranch, mkLabel, mkIfThenElse, mkWhileDo
+ , outOfLine
, emptyGraph, graphOfMiddles, graphOfZTail
, lgraphOfAGraph, graphOfAGraph, labelAGraph
)
where
+import ZipCfg
+
import Outputable
-import Prelude hiding (zip, unzip, last)
+import Unique
+import UniqFM
import UniqSupply
-import ZipCfg
+
+import Prelude hiding (zip, unzip, last)
+
-------------------------------------------------------------------------
-- GENERIC ZIPPER-BASED CONTROL-FLOW GRAPH (CONSTRUCTOR VIEW) --
-- | This function provides access to fresh labels without requiring
-- clients to be programmed monadically.
withFreshLabel :: String -> (BlockId -> AGraph m l) -> AGraph m l
+withUnique :: (Unique -> AGraph m l) -> AGraph m l
+
+
+outOfLine :: (LastNode l, Outputable m, Outputable l)
+ => AGraph m l -> AGraph m l
+-- ^ The argument is an AGraph that has an
+-- empty entry sequence and no exit sequence.
+-- The result is a new AGraph that has an empty entry sequence
+-- connected to an empty exit sequence, with the original graph
+-- sitting to the side out-of-line.
+--
+-- Example: mkMiddle (x = 3)
+-- <*> outOfLine (mkLabel L <*> ...stuff...)
+-- <*> mkMiddle (y = x)
+-- Control will flow directly from x=3 to y=x;
+-- the block starting with L is "on the side".
+--
+-- N.B. algebraically forall g g' : g <*> outOfLine g' == outOfLine g' <*> g
+
+
-- below for convenience
mkMiddles :: [m] -> AGraph m l
let AGraph f' = ofId id
f' g
+withUnique ofU = AGraph f
+ where f g = do u <- getUniqueUs
+ let AGraph f' = ofU u
+ f' g
+
+outOfLine (AGraph f) = AGraph f'
+ where f' (Graph tail' blocks') =
+ do Graph emptyEntrance blocks <- f emptyGraph
+ note_this_code_becomes_unreachable emptyEntrance
+ return $ Graph tail' (blocks `plusUFM` blocks')
+
+
mkIfThenElse cbranch tbranch fbranch =
withFreshLabel "end of if" $ \endif ->
withFreshLabel "start of then" $ \tid ->
note_this_code_becomes_unreachable ::
(Monad m, LastNode l, Outputable middle, Outputable l) => ZTail middle l -> m ()
+#ifdef DEBUG
note_this_code_becomes_unreachable = u
where u (ZLast LastExit) = return ()
u (ZLast (LastOther l)) | isBranchNode l = return ()
-- Note [Branch follows branch]
u tail = fail ("unreachable code: " ++ showSDoc (ppr tail))
+#else
+note_this_code_becomes_unreachable _ = return ()
+#endif
+
{-
Note [Branch follows branch]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~