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

View File

@ -1,7 +1,10 @@
package P; package P;
localparam [31:0] L = 8; localparam [31:0] L = 8;
function automatic integer F;
F = -1;
endfunction
typedef struct packed { 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; } S;
endpackage endpackage
module top; module top;