Skip to content

Commit

Permalink
invert expression scoping application order
Browse files Browse the repository at this point in the history
  • Loading branch information
zachjs committed Dec 11, 2024
1 parent c56a91b commit 1c13bcf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Convert/Scoper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module Convert.Scoper

import Control.Monad (join, when)
import Control.Monad.State.Strict
import Data.Functor ((<&>))
import Data.List (findIndices, intercalate, isPrefixOf, partition)
import Data.Maybe (isNothing)
import qualified Data.Map.Strict as Map
Expand Down Expand Up @@ -204,18 +205,24 @@ replaceInExpr' replacements other =
-- refer to currently visible declarations so it can be substituted elsewhere
scopeExpr :: Monad m => Expr -> ScoperT a m Expr
scopeExpr expr = do
expr' <- traverseSinglyNestedExprsM scopeExpr expr
>>= traverseExprTypesM scopeType
details <- lookupElemM expr'
details <- lookupElemM expr
case details of
Just (accesses, _, _) -> return $ accessesToExpr accesses
_ -> return expr'
Just (accesses, replacements, _) ->
mapM (scopeResolvedAccess replacements) accesses <&> accessesToExpr
_ -> traverseSinglyNestedExprsM scopeExpr expr
>>= traverseExprTypesM scopeType
scopeType :: Monad m => Type -> ScoperT a m Type
scopeType = traverseNestedTypesM $ traverseTypeExprsM scopeExpr

{-# INLINABLE scopeExpr #-}
{-# INLINABLE scopeType #-}

scopeResolvedAccess :: Monad m => Replacements -> Access -> ScoperT a m Access
scopeResolvedAccess _ access@(Access _ Nil) = return access
scopeResolvedAccess replacements access@(Access _ (Ident index))
| Map.member index replacements = return access
scopeResolvedAccess _ (Access name expr) = scopeExpr expr <&> Access name

scopeExprWithScopes :: Scopes a -> Expr -> Expr
scopeExprWithScopes scopes = flip evalState scopes . scopeExpr

Expand Down
6 changes: 6 additions & 0 deletions test/core/struct_hier_shadow.sv
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ module top;
$display("%b %b", x[1:0], top.x[1:0]);
$display("%b %b", x[0+:1], top.x[0+:1]);
end
parameter int Q = 2;
typedef struct {
logic [Q-1:0] y;
} T;
T y;
assign y.y = 0;
endmodule
2 changes: 2 additions & 0 deletions test/core/struct_hier_shadow.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ module top;
$display("%b %b", x[1:0], top[1:0]);
$display("%b %b", x[0+:1], top[0+:1]);
end
wire [1:0] y;
assign y = 0;
endmodule

0 comments on commit 1c13bcf

Please sign in to comment.