#if __GLASGOW_HASKELL__ >= 701
#endif
module Compiler.Hoopl.MkGraph
( AGraph, graphOfAGraph, aGraphOfGraph
, (<*>), (|*><*|), catGraphs, addEntrySeq, addExitSeq, addBlocks, unionBlocks
, emptyGraph, emptyClosedGraph, withFresh
, mkFirst, mkMiddle, mkMiddles, mkLast, mkBranch, mkLabel, mkWhileDo
, IfThenElseable(mkIfThenElse)
, mkEntry, mkExit
, HooplNode(mkLabelNode, mkBranchNode)
)
where
import Compiler.Hoopl.Label (Label, uniqueToLbl)
import Compiler.Hoopl.Block
import Compiler.Hoopl.Graph as U
import Compiler.Hoopl.Unique
import Control.Monad (liftM2)
class GraphRep g where
emptyGraph :: g n O O
emptyClosedGraph :: g n C C
mkFirst :: n C O -> g n C O
mkMiddle :: n O O -> g n O O
mkLast :: n O C -> g n O C
mkFirst n = mkExit (BlockCO n BNil)
mkLast n = mkEntry (BlockOC BNil n)
infixl 3 <*>
infixl 2 |*><*|
(<*>) :: NonLocal n => g n e O -> g n O x -> g n e x
(|*><*|) :: NonLocal n => g n e C -> g n C x -> g n e x
catGraphs :: NonLocal n => [g n O O] -> g n O O
catGraphs = foldr (<*>) emptyGraph
mkLabel :: HooplNode n => Label -> g n C O
mkBranch :: HooplNode n => Label -> g n O C
mkMiddles :: NonLocal n => [n O O] -> g n O O
mkLabel id = mkFirst $ mkLabelNode id
mkBranch target = mkLast $ mkBranchNode target
mkMiddles ms = catGraphs $ map mkMiddle ms
mkEntry :: Block n O C -> g n O C
mkExit :: Block n C O -> g n C O
instance GraphRep Graph where
emptyGraph = GNil
emptyClosedGraph = GMany NothingO emptyBody NothingO
(<*>) = U.gSplice
(|*><*|) = U.gSplice
mkMiddle = GUnit . BMiddle
mkExit block = GMany NothingO emptyBody (JustO block)
mkEntry block = GMany (JustO block) emptyBody NothingO
instance GraphRep AGraph where
emptyGraph = aGraphOfGraph emptyGraph
emptyClosedGraph = aGraphOfGraph emptyClosedGraph
(<*>) = liftA2 (<*>)
(|*><*|) = liftA2 (|*><*|)
mkMiddle = aGraphOfGraph . mkMiddle
mkExit = aGraphOfGraph . mkExit
mkEntry = aGraphOfGraph . mkEntry
newtype AGraph n e x =
A { graphOfAGraph :: forall m. UniqueMonad m =>
m (Graph n e x)
}
aGraphOfGraph :: Graph n e x -> AGraph n e x
aGraphOfGraph g = A (return g)
class Uniques u where
withFresh :: (u -> AGraph n e x) -> AGraph n e x
instance Uniques Unique where
withFresh f = A $ freshUnique >>= (graphOfAGraph . f)
instance Uniques Label where
withFresh f = A $ freshUnique >>= (graphOfAGraph . f . uniqueToLbl)
liftA2 :: (Graph n a b -> Graph n c d -> Graph n e f)
-> (AGraph n a b -> AGraph n c d -> AGraph n e f)
liftA2 f (A g) (A g') = A (liftM2 f g g')
addBlocks :: HooplNode n
=> AGraph n e x -> AGraph n C C -> AGraph n e x
addBlocks (A g) (A blocks) = A $ g >>= \g -> blocks >>= add g
where add :: (UniqueMonad m, HooplNode n)
=> Graph n e x -> Graph n C C -> m (Graph n e x)
add (GMany e body x) (GMany NothingO body' NothingO) =
return $ GMany e (body `U.bodyUnion` body') x
add g@GNil blocks = spliceOO g blocks
add g@(GUnit _) blocks = spliceOO g blocks
spliceOO :: (HooplNode n, UniqueMonad m)
=> Graph n O O -> Graph n C C -> m (Graph n O O)
spliceOO g blocks = graphOfAGraph $ withFresh $ \l ->
A (return g) <*> mkBranch l |*><*| A (return blocks) |*><*| mkLabel l
class NonLocal n => HooplNode n where
mkBranchNode :: Label -> n O C
mkLabelNode :: Label -> n C O
class IfThenElseable x where
mkIfThenElse :: HooplNode n
=> (Label -> Label -> AGraph n O C)
-> AGraph n O x
-> AGraph n O x
-> AGraph n O x
mkWhileDo :: HooplNode n
=> (Label -> Label -> AGraph n O C)
-> AGraph n O O
-> AGraph n O O
instance IfThenElseable O where
mkIfThenElse cbranch tbranch fbranch = withFresh $ \(endif, ltrue, lfalse) ->
cbranch ltrue lfalse |*><*|
mkLabel ltrue <*> tbranch <*> mkBranch endif |*><*|
mkLabel lfalse <*> fbranch <*> mkBranch endif |*><*|
mkLabel endif
instance IfThenElseable C where
mkIfThenElse cbranch tbranch fbranch = withFresh $ \(ltrue, lfalse) ->
cbranch ltrue lfalse |*><*|
mkLabel ltrue <*> tbranch |*><*|
mkLabel lfalse <*> fbranch
mkWhileDo cbranch body = withFresh $ \(test, head, endwhile) ->
mkBranch test |*><*|
mkLabel head <*> body <*> mkBranch test |*><*|
mkLabel test <*> cbranch head endwhile |*><*|
mkLabel endwhile
instance (Uniques u1, Uniques u2) => Uniques (u1, u2) where
withFresh f = withFresh $ \u1 ->
withFresh $ \u2 ->
f (u1, u2)
instance (Uniques u1, Uniques u2, Uniques u3) => Uniques (u1, u2, u3) where
withFresh f = withFresh $ \u1 ->
withFresh $ \u2 ->
withFresh $ \u3 ->
f (u1, u2, u3)
instance (Uniques u1, Uniques u2, Uniques u3, Uniques u4) => Uniques (u1, u2, u3, u4) where
withFresh f = withFresh $ \u1 ->
withFresh $ \u2 ->
withFresh $ \u3 ->
withFresh $ \u4 ->
f (u1, u2, u3, u4)
addEntrySeq :: NonLocal n => AGraph n O C -> AGraph n C x -> AGraph n O x
addExitSeq :: NonLocal n => AGraph n e C -> AGraph n C O -> AGraph n e O
unionBlocks :: NonLocal n => AGraph n C C -> AGraph n C C -> AGraph n C C
addEntrySeq = (|*><*|)
addExitSeq = (|*><*|)
unionBlocks = (|*><*|)