avoid excessive scoping of top-level variables

This commit is contained in:
Zachary Snow 2024-12-11 08:51:39 -05:00
parent 1c13bcf557
commit 5a636724d7
2 changed files with 27 additions and 15 deletions

View File

@ -20,7 +20,6 @@
module Convert.HierConst (convert) where
import Control.Monad (when)
import Data.Either (fromLeft)
import qualified Data.Map.Strict as Map
import Convert.Scoper
@ -43,7 +42,7 @@ convertDescription (Part attrs extern kw lifetime name ports items) =
(traverseExprsM traverseExprM)
(traverseGenItemExprsM traverseExprM)
(traverseStmtExprsM traverseExprM)
shadowedParams = Map.keys $ Map.filter (fromLeft False) $
shadowedParams = Map.keys $ Map.filter (== HierParam True) $
extractMapping mapping
expand = traverseNestedModuleItems $ expandParam shadowedParams
convertDescription description = description
@ -61,23 +60,31 @@ expandParam _ item = item
prefix :: Identifier -> Identifier
prefix = (++) "_sv2v_disambiguate_"
type ST = Scoper (Either Bool Expr)
data Hier
= HierParam Bool
| HierLocalparam Expr
| HierVar
deriving Eq
type ST = Scoper Hier
traverseDeclM :: Decl -> ST Decl
traverseDeclM decl = do
case decl of
Param Parameter _ x _ ->
insertElem x (Left False)
insertElem x (HierParam False)
Param Localparam UnknownType x e ->
scopeExpr e >>= insertElem x . Right
scopeExpr e >>= insertElem x . HierLocalparam
Param Localparam (Implicit sg rs) x e ->
scopeExpr (Cast (Left t) e) >>= insertElem x . Right
scopeExpr (Cast (Left t) e) >>= insertElem x . HierLocalparam
where t = IntegerVector TBit sg rs
Param Localparam (IntegerVector _ sg rs) x e ->
scopeExpr (Cast (Left t) e) >>= insertElem x . Right
scopeExpr (Cast (Left t) e) >>= insertElem x . HierLocalparam
where t = IntegerVector TBit sg rs
Param Localparam t x e ->
scopeExpr (Cast (Left t) e) >>= insertElem x . Right
scopeExpr (Cast (Left t) e) >>= insertElem x . HierLocalparam
Variable _ _ x [] _ -> insertElem x HierVar
Net _ _ _ _ x [] _ -> insertElem x HierVar
_ -> return ()
traverseDeclExprsM traverseExprM decl
@ -88,21 +95,23 @@ traverseExprM expr@(Dot _ x) = do
detailsE <- lookupElemM expr'
detailsX <- lookupElemM x
case (detailsE, detailsX) of
(Just ([_, _], _, Left{}), Just ([_, _], _, Left{})) ->
(Just ([_, _], _, HierParam{}), Just ([_, _], _, HierParam{})) ->
return $ Ident x
(Just (accesses@[Access _ Nil, _], _, Left False), _) -> do
(Just ([_, _], _, HierVar), Just ([_, _], _, HierVar)) ->
return $ Ident x
(Just (accesses@[Access _ Nil, _], _, HierParam False), _) -> do
details <- lookupElemM $ prefix x
when (details == Nothing) $
insertElem accesses (Left True)
insertElem accesses (HierParam True)
return $ Ident $ prefix x
(Just ([Access _ Nil, _], _, Left True), _) ->
(Just ([Access _ Nil, _], _, HierParam True), _) ->
return $ Ident $ prefix x
(Just (aE, replacements, Right value), Just (aX, _, _)) ->
(Just (aE, replacements, HierLocalparam value), Just (aX, _, _)) ->
if aE == aX && Map.null replacements
then return $ Ident x
else traverseSinglyNestedExprsM traverseExprM $
replaceInExpr replacements value
(Just (_, replacements, Right value), Nothing) ->
(Just (_, replacements, HierLocalparam value), Nothing) ->
traverseSinglyNestedExprsM traverseExprM $
replaceInExpr replacements value
_ -> traverseSinglyNestedExprsM traverseExprM expr

View File

@ -1,7 +1,10 @@
package P;
localparam [31:0] L = 8;
function automatic integer F;
F = -1;
endfunction
typedef struct packed {
logic [L + L[0] + L[1:0] + L[0+:1] - 1:0] x;
logic [L + L[0] + L[1:0] + L[0+:1] + F():0] x;
} S;
endpackage
module top;