Skip to content

Commit

Permalink
avoid excessive scoping of top-level variables
Browse files Browse the repository at this point in the history
  • Loading branch information
zachjs committed Dec 11, 2024
1 parent 1c13bcf commit 5a63672
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
37 changes: 23 additions & 14 deletions src/Convert/HierConst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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 ([_, _], _, HierVar), Just ([_, _], _, HierVar)) ->
return $ Ident x
(Just (accesses@[Access _ Nil, _], _, Left False), _) -> do
(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
Expand Down
5 changes: 4 additions & 1 deletion test/core/struct_hier_shadow.sv
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 5a63672

Please sign in to comment.