Skip to content

Commit

Permalink
Move IdleCircuit to clash-protocols and move Template haskell cod…
Browse files Browse the repository at this point in the history
…e to separate modules
  • Loading branch information
lmbollen committed Sep 27, 2024
1 parent 06db0cb commit 70d3d89
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
6 changes: 3 additions & 3 deletions clash-protocols-base/clash-protocols-base.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ library
, template-haskell

exposed-modules:
Protocols.Cpp
Protocols.Circuit
Protocols.Internal.Types
Protocols.Cpp
Protocols.Internal.TaggedBundle
Protocols.Internal.TaggedBundle.TH
Protocols.Internal.TH
Protocols.Internal.Types
Protocols.Internal.Units
Protocols.Internal.Units.TH
Protocols.Plugin
Protocols.Plugin.Internal
Protocols.Protocol.TH

default-language: Haskell2010
2 changes: 1 addition & 1 deletion clash-protocols-base/src/Protocols/Circuit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import Clash.Signal
import Clash.Sized.Vector
import GHC.TypeNats (KnownNat)
import Protocols.Cpp (maxTupleSize)
import Protocols.Internal.TH
import Protocols.Internal.Types
import Protocols.Protocol.TH

instance Protocol () where
type Fwd () = ()
Expand Down
34 changes: 34 additions & 0 deletions clash-protocols-base/src/Protocols/Protocol/TH.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{-# OPTIONS_HADDOCK hide #-}

module Protocols.Protocol.TH where
import Language.Haskell.TH

appTs :: Q Type -> [Q Type] -> Q Type
appTs = foldl appT

protocolTupleInstances :: Int -> Int -> Q [Dec]
protocolTupleInstances n m = mapM protocolTupleInstance [n .. m]

protocolTupleInstance :: Int -> Q Dec
protocolTupleInstance n =
instanceD
(pure []) -- context
(protocolConT `appT` tup) -- head
[mkTyInst fwdConName, mkTyInst bwdConName] -- body
where
fwdConName = mkName "Fwd"
bwdConName = mkName "Bwd"
protocolConT = conT (mkName "Protocol")

tyVars :: [TypeQ]
tyVars = map (varT . mkName . ('a' :) . show) [1 .. n]

tup = tupleT n `appTs` tyVars

mkTyInst :: Name -> DecQ
mkTyInst con =
tySynInstD $ tySynEqn Nothing lhs rhs
where
lhs, rhs :: TypeQ
lhs = conT con `appT` tup
rhs = tupleT n `appTs` map (conT con `appT`) tyVars
1 change: 1 addition & 0 deletions clash-protocols/clash-protocols.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ library
Protocols.Hedgehog
Protocols.Hedgehog.Internal
Protocols.Idle
Protocols.Internal.TH
Protocols.Wishbone
Protocols.Wishbone.Standard
Protocols.Wishbone.Standard.Hedgehog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,6 @@ import Protocols.Internal.Types
appTs :: Q Type -> [Q Type] -> Q Type
appTs = foldl appT

protocolTupleInstances :: Int -> Int -> Q [Dec]
protocolTupleInstances n m = mapM protocolTupleInstance [n .. m]

protocolTupleInstance :: Int -> Q Dec
protocolTupleInstance n =
instanceD
(pure []) -- context
(protocolConT `appT` tup) -- head
[mkTyInst fwdConName, mkTyInst bwdConName] -- body
where
fwdConName = mkName "Fwd"
bwdConName = mkName "Bwd"
protocolConT = conT (mkName "Protocol")

tyVars :: [TypeQ]
tyVars = map (varT . mkName . ('a' :) . show) [1 .. n]

tup = tupleT n `appTs` tyVars

mkTyInst :: Name -> DecQ
mkTyInst con =
tySynInstD $ tySynEqn Nothing lhs rhs
where
lhs, rhs :: TypeQ
lhs = conT con `appT` tup
rhs = tupleT n `appTs` map (conT con `appT`) tyVars

{- | Template haskell function to generate IdleCircuit instances for the tuples
n through m inclusive. To see a 2-tuple version of the pattern we generate,
see @Protocols.IdleCircuit@.
Expand All @@ -56,8 +29,8 @@ idleCircuitTupleInstance n =
|]
where
circTys = map (\i -> varT $ mkName $ "c" <> show i) [1 .. n]
instCtx = foldl appT (tupleT n) $ map (\ty -> [t|IdleCircuit $ty|]) circTys
instTy = foldl appT (tupleT n) circTys
instCtx = appTs (tupleT n) $ map (\ty -> [t|IdleCircuit $ty|]) circTys
instTy = appTs (tupleT n) circTys
fwdExpr = tupE $ map mkFwdExpr circTys
mkFwdExpr ty = [e|idleFwd $ Proxy @($ty)|]
bwdExpr = tupE $ map mkBwdExpr circTys
Expand Down

0 comments on commit 70d3d89

Please sign in to comment.