2019-02-22 02:12:34 +01:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
-
|
|
|
|
|
- Conversion for flattening multi-dimensional packed arrays
|
|
|
|
|
-
|
2019-02-28 06:16:53 +01:00
|
|
|
- This removes one dimension per identifier at a time. This works fine because
|
|
|
|
|
- the conversions are repeatedly applied.
|
2019-02-22 02:12:34 +01:00
|
|
|
-
|
2019-03-01 01:48:58 +01:00
|
|
|
- Packed arrays can be used in any of the following ways: A) as a whole,
|
|
|
|
|
- including as a port; B) with an index (`foo[0]`); or C) with a range
|
|
|
|
|
- (`foo[10:0]`). The rules for this conversion are:
|
|
|
|
|
- 1. If used with an index, then we must have an unflattened/unpacked
|
|
|
|
|
- version of that array after the conversion, so that we may get at the
|
|
|
|
|
- packed sub-arrays.
|
|
|
|
|
- 2. If used as a whole or with a range, then we must have a flattened
|
|
|
|
|
- version of that array after the conversion, so that we may get at a
|
|
|
|
|
- contiguous sequence of elements.
|
|
|
|
|
- 3. If both 1 and 2 apply, then we will make a fancy generate block to
|
|
|
|
|
- derive one from the other. The derivation direction is decided based on
|
|
|
|
|
- which version, if any, is exposed directly as a port.
|
|
|
|
|
-
|
2019-03-01 00:04:34 +01:00
|
|
|
- TODO FIXME XXX: The Parser/AST don't yet support indexing into an identifier
|
|
|
|
|
- twice, or indexing into an identifier, and then selecting a range.
|
|
|
|
|
-
|
2019-02-28 06:16:53 +01:00
|
|
|
- TODO: This assumes that the first range index is the upper bound. We could
|
2019-03-01 00:04:34 +01:00
|
|
|
- probably get around this with some cleverness in the generate block. I don't
|
|
|
|
|
- think it's urgent to have support for "backwards" ranges.
|
2019-02-22 02:12:34 +01:00
|
|
|
-}
|
|
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
module Convert.PackedArray (convert) where
|
2019-02-22 02:12:34 +01:00
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
import Control.Monad.State
|
|
|
|
|
import Data.List (partition)
|
2019-03-01 01:48:58 +01:00
|
|
|
import qualified Data.Set as Set
|
2019-02-22 02:12:34 +01:00
|
|
|
import qualified Data.Map.Strict as Map
|
|
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
import Convert.Traverse
|
2019-02-22 02:12:34 +01:00
|
|
|
import Language.SystemVerilog.AST
|
|
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
type DirMap = Map.Map Identifier Direction
|
2019-02-22 02:12:34 +01:00
|
|
|
type DimMap = Map.Map Identifier (Type, Range)
|
2019-03-01 01:48:58 +01:00
|
|
|
type IdentSet = Set.Set Identifier
|
|
|
|
|
|
|
|
|
|
data Info = Info
|
|
|
|
|
{ sTypeDims :: DimMap
|
|
|
|
|
, sPortDirs :: DirMap
|
|
|
|
|
, sIdxUses :: IdentSet
|
|
|
|
|
, sSeqUses :: IdentSet }
|
|
|
|
|
deriving Show
|
2019-02-22 02:12:34 +01:00
|
|
|
|
|
|
|
|
convert :: AST -> AST
|
2019-02-28 06:16:53 +01:00
|
|
|
convert = traverseDescriptions convertDescription
|
2019-02-22 02:12:34 +01:00
|
|
|
|
|
|
|
|
convertDescription :: Description -> Description
|
2019-03-04 08:58:00 +01:00
|
|
|
convertDescription (description @ (Part _ _ ports _)) =
|
2019-02-28 06:16:53 +01:00
|
|
|
hoistPortDecls $
|
2019-03-01 01:48:58 +01:00
|
|
|
traverseModuleItems (flattenModuleItem info . rewriteModuleItem info) description
|
2019-02-22 02:12:34 +01:00
|
|
|
where
|
2019-03-01 01:48:58 +01:00
|
|
|
-- collect all possible information info our Info structure
|
|
|
|
|
rawInfo =
|
|
|
|
|
execState (collectModuleItemsM (collectLHSsM collectLHS) description) $
|
|
|
|
|
execState (collectModuleItemsM (collectExprsM collectExpr) description) $
|
|
|
|
|
execState (collectModuleItemsM collectDecl description) $
|
|
|
|
|
(Info Map.empty Map.empty Set.empty (Set.fromList ports))
|
|
|
|
|
relevantIdents = Map.keysSet $ sTypeDims rawInfo
|
|
|
|
|
-- restrict the sets/maps to only contain keys which need transformation
|
|
|
|
|
info = rawInfo
|
|
|
|
|
{ sPortDirs = Map.restrictKeys (sPortDirs rawInfo) relevantIdents
|
|
|
|
|
, sIdxUses = Set.intersection (sIdxUses rawInfo) relevantIdents
|
|
|
|
|
, sSeqUses = Set.intersection (sSeqUses rawInfo) relevantIdents }
|
|
|
|
|
convertDescription description = description
|
2019-02-22 02:12:34 +01:00
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
-- collects port direction and packed-array dimension info into the state
|
2019-03-01 01:48:58 +01:00
|
|
|
collectDecl :: ModuleItem -> State Info ()
|
2019-02-28 06:16:53 +01:00
|
|
|
collectDecl (MIDecl (Variable dir t ident _ _)) = do
|
2019-03-01 01:48:58 +01:00
|
|
|
let (tf, rs) = typeRanges t
|
2019-03-01 00:04:34 +01:00
|
|
|
if not (typeIsImplicit t) && length rs > 1
|
2019-03-01 01:48:58 +01:00
|
|
|
then
|
|
|
|
|
let dets = (tf $ tail rs, head rs) in
|
|
|
|
|
modify $ \s -> s { sTypeDims = Map.insert ident dets (sTypeDims s) }
|
2019-02-28 06:16:53 +01:00
|
|
|
else return ()
|
|
|
|
|
if dir /= Local
|
2019-03-01 01:48:58 +01:00
|
|
|
then modify $ \s -> s { sPortDirs = Map.insert ident dir (sPortDirs s) }
|
2019-02-28 06:16:53 +01:00
|
|
|
else return ()
|
|
|
|
|
collectDecl _ = return ()
|
|
|
|
|
|
2019-03-01 01:48:58 +01:00
|
|
|
-- collectors for identifier usage information
|
|
|
|
|
recordSeqUsage :: Identifier -> State Info ()
|
|
|
|
|
recordSeqUsage i = modify $ \s -> s { sSeqUses = Set.insert i $ sSeqUses s }
|
|
|
|
|
recordIdxUsage :: Identifier -> State Info ()
|
|
|
|
|
recordIdxUsage i = modify $ \s -> s { sIdxUses = Set.insert i $ sIdxUses s }
|
|
|
|
|
collectExpr :: Expr -> State Info ()
|
|
|
|
|
collectExpr (Ident i ) = recordSeqUsage i
|
|
|
|
|
collectExpr (IdentRange i _) = recordSeqUsage i
|
|
|
|
|
collectExpr (IdentBit i _) = recordIdxUsage i
|
|
|
|
|
collectExpr _ = return ()
|
|
|
|
|
collectLHS :: LHS -> State Info ()
|
|
|
|
|
collectLHS (LHS i ) = recordSeqUsage i
|
|
|
|
|
collectLHS (LHSRange i _) = recordSeqUsage i
|
|
|
|
|
collectLHS (LHSBit i _) = recordIdxUsage i
|
|
|
|
|
collectLHS (LHSConcat lhss) = mapM collectLHS lhss >>= \_ -> return ()
|
2019-03-04 08:58:00 +01:00
|
|
|
collectLHS (LHSDot lhs _) = collectLHS lhs
|
2019-03-01 01:48:58 +01:00
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
-- VCS doesn't like port declarations inside of `generate` blocks, so we hoist
|
|
|
|
|
-- them out with this function. This obviously isn't ideal, but it's a
|
|
|
|
|
-- relatively straightforward transformation, and testing in VCS is important.
|
|
|
|
|
hoistPortDecls :: Description -> Description
|
2019-03-04 08:58:00 +01:00
|
|
|
hoistPortDecls (Part kw name ports items) =
|
|
|
|
|
Part kw name ports (concat $ map explode items)
|
2019-02-22 23:21:16 +01:00
|
|
|
where
|
2019-02-28 06:16:53 +01:00
|
|
|
explode :: ModuleItem -> [ModuleItem]
|
|
|
|
|
explode (Generate genItems) =
|
|
|
|
|
portDecls ++ [Generate rest]
|
|
|
|
|
where
|
|
|
|
|
(wrappedPortDecls, rest) = partition isPortDecl genItems
|
|
|
|
|
portDecls = map (\(GenModuleItem item) -> item) wrappedPortDecls
|
|
|
|
|
isPortDecl :: GenItem -> Bool
|
|
|
|
|
isPortDecl (GenModuleItem (MIDecl (Variable dir _ _ _ _))) =
|
|
|
|
|
dir /= Local
|
|
|
|
|
isPortDecl _ = False
|
|
|
|
|
explode other = [other]
|
|
|
|
|
hoistPortDecls other = other
|
2019-02-22 23:21:16 +01:00
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
-- rewrite a module item if it contains a declaration to flatten
|
2019-03-01 01:48:58 +01:00
|
|
|
flattenModuleItem :: Info -> ModuleItem -> ModuleItem
|
|
|
|
|
flattenModuleItem info (origDecl @ (MIDecl (Variable dir t ident a me))) =
|
|
|
|
|
-- if it doesn't need any mapping, then skip it
|
|
|
|
|
if Map.notMember ident typeDims then origDecl
|
|
|
|
|
-- if it is never used as a sequence (whole or range), then move the packed
|
|
|
|
|
-- dimension to the unpacked side
|
|
|
|
|
else if Set.notMember ident seqUses then flipDecl
|
|
|
|
|
-- if it is used as a sequence, but never indexed-into (sub-array), then
|
|
|
|
|
-- flatten (combine) the ranges, leaving them packed
|
|
|
|
|
else if Set.notMember ident duoUses then flatDecl
|
|
|
|
|
-- if it is both used as a sequence and is indexed-into
|
2019-02-28 06:16:53 +01:00
|
|
|
else
|
2019-03-01 01:48:58 +01:00
|
|
|
-- if this is not the fully-typed declaration of this item, then flatten
|
|
|
|
|
-- it, but don't make the `generate` block this time to avoid duplicates
|
|
|
|
|
if typeIsImplicit t then flatDecl
|
|
|
|
|
-- otherwise, flatten it, and also create an unflattened copy
|
|
|
|
|
else Generate $ (GenModuleItem flatDecl) : genItems
|
2019-02-28 06:16:53 +01:00
|
|
|
where
|
2019-03-01 01:48:58 +01:00
|
|
|
Info typeDims portDirs idxUses seqUses = info
|
|
|
|
|
duoUses = Set.intersection idxUses seqUses
|
|
|
|
|
writeToFlatVariant = Map.lookup ident portDirs == Just Output
|
|
|
|
|
genItems = unflattener writeToFlatVariant ident (typeDims Map.! ident)
|
|
|
|
|
(tf, rs) = typeRanges t
|
|
|
|
|
flipDecl = MIDecl $ Variable dir (tf $ tail rs) ident (a ++ [head rs]) me
|
|
|
|
|
flatDecl = MIDecl $ Variable dir (tf $ flattenRanges rs) ident a me
|
2019-02-28 06:16:53 +01:00
|
|
|
flattenModuleItem _ other = other
|
|
|
|
|
|
2019-03-01 01:48:58 +01:00
|
|
|
-- produces `generate` items for creating an unflattened copy of the given
|
|
|
|
|
-- flattened, packed array
|
2019-02-28 06:16:53 +01:00
|
|
|
unflattener :: Bool -> Identifier -> (Type, Range) -> [GenItem]
|
2019-03-01 01:48:58 +01:00
|
|
|
unflattener writeToFlatVariant arr (t, (majorHi, majorLo)) =
|
2019-02-28 06:16:53 +01:00
|
|
|
[ GenModuleItem $ Comment $ "sv2v packed-array-flatten unflattener for " ++ arr
|
|
|
|
|
, GenModuleItem $ MIDecl $ Variable Local t arrUnflat [(majorHi, majorLo)] Nothing
|
|
|
|
|
, GenModuleItem $ Genvar index
|
2019-02-24 09:06:40 +01:00
|
|
|
, GenModuleItem $ MIDecl $ Variable Local IntegerT (arrUnflat ++ "_repeater_index") [] Nothing
|
2019-02-22 02:12:34 +01:00
|
|
|
, GenFor
|
2019-02-22 23:21:16 +01:00
|
|
|
(index, majorLo)
|
|
|
|
|
(BinOp Le (Ident index) majorHi)
|
2019-02-22 02:12:34 +01:00
|
|
|
(index, BinOp Add (Ident index) (Number "1"))
|
|
|
|
|
(prefix "unflatten")
|
|
|
|
|
[ localparam startBit
|
2019-02-22 23:21:16 +01:00
|
|
|
(simplify $ BinOp Add majorLo
|
|
|
|
|
(BinOp Mul (Ident index) size))
|
2019-02-22 02:12:34 +01:00
|
|
|
, GenModuleItem $ (uncurry Assign) $
|
2019-03-01 01:48:58 +01:00
|
|
|
if not writeToFlatVariant
|
2019-02-22 02:12:34 +01:00
|
|
|
then (LHSBit arrUnflat $ Ident index, IdentRange arr origRange)
|
|
|
|
|
else (LHSRange arr origRange, IdentBit arrUnflat $ Ident index)
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
where
|
2019-02-22 23:21:16 +01:00
|
|
|
startBit = prefix "_tmp_start"
|
2019-02-22 02:12:34 +01:00
|
|
|
arrUnflat = prefix arr
|
2019-02-22 23:21:16 +01:00
|
|
|
index = prefix "_tmp_index"
|
2019-03-01 01:48:58 +01:00
|
|
|
(minorHi, minorLo) = head $ snd $ typeRanges t
|
2019-02-22 23:21:16 +01:00
|
|
|
size = simplify $ BinOp Add (BinOp Sub minorHi minorLo) (Number "1")
|
2019-02-22 02:12:34 +01:00
|
|
|
localparam :: Identifier -> Expr -> GenItem
|
2019-02-24 09:06:40 +01:00
|
|
|
localparam x v = GenModuleItem $ MIDecl $ Localparam (Implicit []) x v
|
2019-02-22 02:12:34 +01:00
|
|
|
origRange = ( (BinOp Add (Ident startBit)
|
2019-02-22 23:21:16 +01:00
|
|
|
(BinOp Sub size (Number "1")))
|
2019-02-22 02:12:34 +01:00
|
|
|
, Ident startBit )
|
|
|
|
|
|
2019-03-01 00:04:34 +01:00
|
|
|
typeIsImplicit :: Type -> Bool
|
|
|
|
|
typeIsImplicit (Implicit _) = True
|
|
|
|
|
typeIsImplicit _ = False
|
|
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
-- basic expression simplfication utility to help us generate nicer code in the
|
|
|
|
|
-- common case of ranges like `[FOO-1:0]`
|
|
|
|
|
simplify :: Expr -> Expr
|
|
|
|
|
simplify (BinOp op e1 e2) =
|
|
|
|
|
case (op, e1', e2') of
|
|
|
|
|
(Add, Number "0", e) -> e
|
|
|
|
|
(Add, e, Number "0") -> e
|
|
|
|
|
(Sub, e, Number "0") -> e
|
|
|
|
|
(Add, BinOp Sub e (Number "1"), Number "1") -> e
|
|
|
|
|
(Add, e, BinOp Sub (Number "0") (Number "1")) -> BinOp Sub e (Number "1")
|
|
|
|
|
_ -> BinOp op e1' e2'
|
|
|
|
|
where
|
|
|
|
|
e1' = simplify e1
|
|
|
|
|
e2' = simplify e2
|
|
|
|
|
simplify other = other
|
|
|
|
|
|
|
|
|
|
-- prefix a string with a namespace of sorts
|
2019-02-22 02:12:34 +01:00
|
|
|
prefix :: Identifier -> Identifier
|
2019-02-22 23:21:16 +01:00
|
|
|
prefix ident = "_sv2v_" ++ ident
|
2019-02-22 02:12:34 +01:00
|
|
|
|
2019-02-28 06:16:53 +01:00
|
|
|
-- combines (flattens) the bottom two ranges in the given list of ranges
|
2019-02-22 02:12:34 +01:00
|
|
|
flattenRanges :: [Range] -> [Range]
|
|
|
|
|
flattenRanges rs =
|
|
|
|
|
if length rs >= 2
|
|
|
|
|
then rs'
|
|
|
|
|
else error $ "flattenRanges on too small list: " ++ (show rs)
|
|
|
|
|
where
|
|
|
|
|
(s1, e1) = head rs
|
|
|
|
|
(s2, e2) = head $ tail rs
|
|
|
|
|
size1 = BinOp Add (BinOp Sub s1 e1) (Number "1")
|
|
|
|
|
size2 = BinOp Add (BinOp Sub s2 e2) (Number "1")
|
|
|
|
|
upper = BinOp Add (BinOp Mul size1 size2) (BinOp Sub e1 (Number "1"))
|
2019-02-22 23:21:16 +01:00
|
|
|
r' = (simplify upper, e1)
|
2019-02-22 02:12:34 +01:00
|
|
|
rs' = (tail $ tail rs) ++ [r']
|
|
|
|
|
|
2019-03-01 01:48:58 +01:00
|
|
|
rewriteModuleItem :: Info -> ModuleItem -> ModuleItem
|
|
|
|
|
rewriteModuleItem info =
|
2019-02-28 23:12:37 +01:00
|
|
|
traverseStmts rewriteStmt .
|
|
|
|
|
traverseExprs rewriteExpr
|
2019-02-22 02:12:34 +01:00
|
|
|
where
|
2019-03-01 01:48:58 +01:00
|
|
|
Info typeDims portDirs idxUses seqUses = info
|
|
|
|
|
duoUses = Set.intersection idxUses seqUses
|
|
|
|
|
|
|
|
|
|
rewriteIdent :: Bool -> Identifier -> Identifier
|
|
|
|
|
rewriteIdent isAsgn x =
|
|
|
|
|
if isDuod && (isOutputPort == isAsgn)
|
|
|
|
|
then prefix x
|
|
|
|
|
else x
|
|
|
|
|
where
|
|
|
|
|
isDuod = Set.member x duoUses
|
|
|
|
|
isOutputPort = Map.lookup x portDirs == Just Output
|
|
|
|
|
rewriteReadIdent = rewriteIdent False
|
|
|
|
|
rewriteAsgnIdent = rewriteIdent True
|
2019-02-28 23:12:37 +01:00
|
|
|
|
|
|
|
|
rewriteExpr :: Expr -> Expr
|
2019-03-01 01:48:58 +01:00
|
|
|
rewriteExpr (Ident i) = Ident (rewriteReadIdent i)
|
|
|
|
|
rewriteExpr (IdentBit i e) = IdentBit (rewriteReadIdent i) e
|
2019-02-28 23:12:37 +01:00
|
|
|
rewriteExpr (IdentRange i (r @ (s, e))) =
|
2019-03-01 01:48:58 +01:00
|
|
|
if Map.member i typeDims
|
|
|
|
|
then IdentRange i r'
|
|
|
|
|
else IdentRange i r
|
|
|
|
|
where
|
|
|
|
|
(a, b) = head $ snd $ typeRanges $ fst $ typeDims Map.! i
|
|
|
|
|
size = BinOp Add (BinOp Sub a b) (Number "1")
|
|
|
|
|
s' = BinOp Sub (BinOp Mul size (BinOp Add s (Number "1"))) (Number "1")
|
|
|
|
|
e' = BinOp Mul size e
|
|
|
|
|
r' = (simplify s', simplify e')
|
2019-02-28 23:12:37 +01:00
|
|
|
rewriteExpr other = other
|
|
|
|
|
|
2019-03-01 00:04:34 +01:00
|
|
|
rewriteLHS :: LHS -> LHS
|
2019-03-01 01:48:58 +01:00
|
|
|
rewriteLHS (LHS x ) = LHS (rewriteAsgnIdent x)
|
|
|
|
|
rewriteLHS (LHSBit x e) = LHSBit (rewriteAsgnIdent x) e
|
|
|
|
|
rewriteLHS (LHSRange x r) = LHSRange (rewriteAsgnIdent x) r
|
2019-03-01 00:04:34 +01:00
|
|
|
rewriteLHS (LHSConcat ls) = LHSConcat $ map rewriteLHS ls
|
2019-03-04 08:58:00 +01:00
|
|
|
rewriteLHS (LHSDot lhs x) = LHSDot (rewriteLHS lhs) x
|
2019-03-01 00:04:34 +01:00
|
|
|
|
2019-02-28 23:12:37 +01:00
|
|
|
rewriteStmt :: Stmt -> Stmt
|
|
|
|
|
rewriteStmt (AsgnBlk lhs expr) = convertAssignment AsgnBlk lhs expr
|
|
|
|
|
rewriteStmt (Asgn lhs expr) = convertAssignment Asgn lhs expr
|
|
|
|
|
rewriteStmt other = other
|
2019-02-22 02:12:34 +01:00
|
|
|
convertAssignment :: (LHS -> Expr -> Stmt) -> LHS -> Expr -> Stmt
|
|
|
|
|
convertAssignment constructor (lhs @ (LHS ident)) (expr @ (Repeat _ exprs)) =
|
2019-03-01 01:48:58 +01:00
|
|
|
if Map.member ident typeDims
|
|
|
|
|
then For inir chkr incr assign
|
|
|
|
|
else constructor (rewriteLHS lhs) expr
|
|
|
|
|
where
|
|
|
|
|
(_, (a, b)) = typeDims Map.! ident
|
|
|
|
|
index = prefix $ ident ++ "_repeater_index"
|
|
|
|
|
assign = constructor
|
|
|
|
|
(LHSBit (prefix ident) (Ident index))
|
|
|
|
|
(Concat exprs)
|
|
|
|
|
inir = (index, b)
|
|
|
|
|
chkr = BinOp Le (Ident index) a
|
|
|
|
|
incr = (index, BinOp Add (Ident index) (Number "1"))
|
2019-02-22 02:12:34 +01:00
|
|
|
convertAssignment constructor lhs expr =
|
2019-03-01 00:04:34 +01:00
|
|
|
constructor (rewriteLHS lhs) expr
|