2019-08-30 06:11:57 +02:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
-
|
|
|
|
|
- Verilog-2005 requires that for loops have have exactly one assignment in the
|
|
|
|
|
- initialization section. For generate for loops, we move any genvar
|
|
|
|
|
- declarations to a wrapping generate block. For procedural for loops, we pull
|
|
|
|
|
- the declarations out to a wrapping block, and convert all but one assignment
|
|
|
|
|
- to a preceding statement. If a for loop has no assignments or declarations, a
|
|
|
|
|
- dummy declaration is generated.
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Convert.ForDecl (convert) where
|
|
|
|
|
|
|
|
|
|
import Convert.Traverse
|
|
|
|
|
import Language.SystemVerilog.AST
|
|
|
|
|
|
|
|
|
|
convert :: [AST] -> [AST]
|
|
|
|
|
convert =
|
|
|
|
|
map $ traverseDescriptions $ traverseModuleItems $
|
|
|
|
|
( traverseStmts convertStmt
|
|
|
|
|
. traverseGenItems convertGenItem
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
convertGenItem :: GenItem -> GenItem
|
2020-02-10 05:20:35 +01:00
|
|
|
convertGenItem (GenFor (True, _, _) _ _ GenNull) = GenNull
|
|
|
|
|
convertGenItem (GenFor (True, _, _) _ _ (GenBlock _ [])) = GenNull
|
2019-10-06 21:36:29 +02:00
|
|
|
convertGenItem (GenFor (True, x, e) a b c) =
|
2019-10-01 05:03:55 +02:00
|
|
|
GenBlock "" genItems
|
2019-09-21 20:14:55 +02:00
|
|
|
where
|
2019-10-06 21:36:29 +02:00
|
|
|
bx = case c of
|
|
|
|
|
GenBlock name _ -> name
|
|
|
|
|
_ -> ""
|
2019-10-01 05:03:55 +02:00
|
|
|
x' = if null bx then x else bx ++ "_" ++ x
|
2019-09-21 20:14:55 +02:00
|
|
|
Generate genItems =
|
|
|
|
|
traverseNestedModuleItems converter $ Generate $
|
|
|
|
|
[ GenModuleItem $ Genvar x'
|
2019-10-06 21:36:29 +02:00
|
|
|
, GenFor (False, x, e) a b c
|
2019-09-21 20:14:55 +02:00
|
|
|
]
|
|
|
|
|
converter =
|
|
|
|
|
(traverseExprs $ traverseNestedExprs convertExpr) .
|
|
|
|
|
(traverseLHSs $ traverseNestedLHSs convertLHS )
|
|
|
|
|
prefix :: String -> String
|
|
|
|
|
prefix ident = if ident == x then x' else ident
|
|
|
|
|
convertExpr (Ident ident) = Ident $ prefix ident
|
|
|
|
|
convertExpr other = other
|
|
|
|
|
convertLHS (LHSIdent ident) = LHSIdent $ prefix ident
|
|
|
|
|
convertLHS other = other
|
2019-08-30 06:11:57 +02:00
|
|
|
convertGenItem other = other
|
|
|
|
|
|
|
|
|
|
convertStmt :: Stmt -> Stmt
|
2019-10-01 05:03:55 +02:00
|
|
|
convertStmt (For (Left []) cc asgns stmt) =
|
|
|
|
|
convertStmt $ For (Right []) cc asgns stmt
|
|
|
|
|
convertStmt (For (Right []) cc asgns stmt) =
|
2019-08-30 06:11:57 +02:00
|
|
|
convertStmt $ For inits cc asgns stmt
|
2019-10-01 05:03:55 +02:00
|
|
|
where inits = Left [dummyDecl (Just $ Number "0")]
|
|
|
|
|
convertStmt (orig @ (For (Right [_]) _ _ _)) = orig
|
2019-08-30 06:11:57 +02:00
|
|
|
|
2019-10-01 05:03:55 +02:00
|
|
|
convertStmt (For (Left inits) cc asgns stmt) =
|
|
|
|
|
Block Seq "" decls $
|
|
|
|
|
initAsgns ++
|
|
|
|
|
[For (Right [(lhs, expr)]) cc asgns stmt]
|
2019-08-30 06:11:57 +02:00
|
|
|
where
|
2019-10-01 05:03:55 +02:00
|
|
|
splitDecls = map splitDecl inits
|
2019-08-30 06:11:57 +02:00
|
|
|
decls = map fst splitDecls
|
|
|
|
|
initAsgns = map asgnStmt $ init $ map snd splitDecls
|
|
|
|
|
(lhs, expr) = snd $ last splitDecls
|
|
|
|
|
|
2019-10-01 05:03:55 +02:00
|
|
|
convertStmt (For (Right origPairs) cc asgns stmt) =
|
|
|
|
|
Block Seq "" [] $
|
|
|
|
|
initAsgns ++
|
|
|
|
|
[For (Right [(lhs, expr)]) cc asgns stmt]
|
2019-08-30 06:11:57 +02:00
|
|
|
where
|
|
|
|
|
(lhs, expr) = last origPairs
|
|
|
|
|
initAsgns = map asgnStmt $ init origPairs
|
|
|
|
|
|
|
|
|
|
convertStmt other = other
|
|
|
|
|
|
|
|
|
|
splitDecl :: Decl -> (Decl, (LHS, Expr))
|
|
|
|
|
splitDecl (Variable d t ident a (Just e)) =
|
|
|
|
|
(Variable d t ident a Nothing, (LHSIdent ident, e))
|
|
|
|
|
splitDecl other =
|
|
|
|
|
error $ "invalid for loop decl: " ++ show other
|
|
|
|
|
|
|
|
|
|
asgnStmt :: (LHS, Expr) -> Stmt
|
2020-02-01 21:52:52 +01:00
|
|
|
asgnStmt = uncurry $ Asgn AsgnOpEq Nothing
|
2019-08-30 06:11:57 +02:00
|
|
|
|
|
|
|
|
dummyDecl :: Maybe Expr -> Decl
|
|
|
|
|
dummyDecl = Variable Local (IntegerAtom TInteger Unspecified) "_sv2v_dummy" []
|