Commit 8f0f8b4a by Zachary Snow

explicit decl type and expr traversals

- fix int type conversion not visiting function return type expressions
- add node-based traversal for decls visiting top level types and exprs
- explicit expr and type recursion in package ident resolution
- expose mapBothM traversal helper
- avoid double-visiting of decl types in certain conversions
parent 1de9b69e
...@@ -13,9 +13,26 @@ convert :: [AST] -> [AST] ...@@ -13,9 +13,26 @@ convert :: [AST] -> [AST]
convert = map $ traverseDescriptions $ traverseModuleItems convertModuleItem convert = map $ traverseDescriptions $ traverseModuleItems convertModuleItem
convertModuleItem :: ModuleItem -> ModuleItem convertModuleItem :: ModuleItem -> ModuleItem
convertModuleItem = convertModuleItem = traverseNodes
traverseTypes (traverseNestedTypes convertType) . traverseExpr traverseDecl traverseType traverseLHS traverseStmt
traverseExprs (traverseNestedExprs convertExpr) where
traverseDecl = traverseDeclNodes traverseType traverseExpr
traverseLHS = traverseNestedLHSs $ traverseLHSExprs traverseExpr
traverseStmt = traverseNestedStmts $
traverseStmtDecls (traverseDeclNodes traverseType id) .
traverseStmtExprs traverseExpr
traverseType :: Type -> Type
traverseType =
traverseSinglyNestedTypes traverseType .
traverseTypeExprs traverseExpr .
convertType
traverseExpr :: Expr -> Expr
traverseExpr =
traverseSinglyNestedExprs traverseExpr .
traverseExprTypes traverseType .
convertExpr
convertType :: Type -> Type convertType :: Type -> Type
convertType (Struct pk fields rs) = convertType (Struct pk fields rs) =
......
...@@ -310,8 +310,7 @@ processItems topName packageName moduleItems = do ...@@ -310,8 +310,7 @@ processItems topName packageName moduleItems = do
traverseDeclM :: Decl -> Scope Decl traverseDeclM :: Decl -> Scope Decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- traverseDeclTypesM traverseTypeM decl decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
>>= traverseDeclExprsM traverseExprM
case decl' of case decl' of
Variable d t x a e -> declHelp x $ \x' -> Variable d t x' a e Variable d t x a e -> declHelp x $ \x' -> Variable d t x' a e
Net d n s t x a e -> declHelp x $ \x' -> Net d n s t x' a e Net d n s t x a e -> declHelp x $ \x' -> Net d n s t x' a e
...@@ -320,21 +319,36 @@ processItems topName packageName moduleItems = do ...@@ -320,21 +319,36 @@ processItems topName packageName moduleItems = do
CommentDecl c -> return $ CommentDecl c CommentDecl c -> return $ CommentDecl c
where declHelp x f = prefixIdent x >>= return . f where declHelp x f = prefixIdent x >>= return . f
traverseRangeM :: Range -> Scope Range
traverseRangeM = mapBothM traverseExprM
traverseEnumItemM :: (Identifier, Expr) -> Scope (Identifier, Expr)
traverseEnumItemM (x, e) = do
x' <- prefixIdent x
e' <- traverseExprM e
return (x', e')
traverseTypeM :: Type -> Scope Type traverseTypeM :: Type -> Scope Type
traverseTypeM (CSAlias p b x rs) = do traverseTypeM (CSAlias p b x rs) = do
x' <- resolveCSIdent' p b x x' <- resolveCSIdent' p b x
return $ Alias x' rs rs' <- mapM traverseRangeM rs
return $ Alias x' rs'
traverseTypeM (PSAlias p x rs) = do traverseTypeM (PSAlias p x rs) = do
x' <- resolvePSIdent' p x x' <- resolvePSIdent' p x
return $ Alias x' rs rs' <- mapM traverseRangeM rs
traverseTypeM (Alias x rs) = return $ Alias x' rs'
resolveIdent x >>= \x' -> return $ Alias x' rs traverseTypeM (Alias x rs) = do
rs' <- mapM traverseRangeM rs
x' <- resolveIdent x
return $ Alias x' rs'
traverseTypeM (Enum t enumItems rs) = do traverseTypeM (Enum t enumItems rs) = do
t' <- traverseTypeM t t' <- traverseTypeM t
enumItems' <- mapM prefixEnumItem enumItems enumItems' <- mapM traverseEnumItemM enumItems
return $ Enum t' enumItems' rs rs' <- mapM traverseRangeM rs
where prefixEnumItem (x, e) = prefixIdent x >>= \x' -> return (x', e) return $ Enum t' enumItems' rs'
traverseTypeM other = traverseSinglyNestedTypesM traverseTypeM other traverseTypeM other =
traverseSinglyNestedTypesM traverseTypeM other
>>= traverseTypeExprsM traverseExprM
traverseExprM :: Expr -> Scope Expr traverseExprM :: Expr -> Scope Expr
traverseExprM (CSIdent p b x) = do traverseExprM (CSIdent p b x) = do
...@@ -344,7 +358,9 @@ processItems topName packageName moduleItems = do ...@@ -344,7 +358,9 @@ processItems topName packageName moduleItems = do
x' <- resolvePSIdent' p x x' <- resolvePSIdent' p x
return $ Ident x' return $ Ident x'
traverseExprM (Ident x) = resolveIdent x >>= return . Ident traverseExprM (Ident x) = resolveIdent x >>= return . Ident
traverseExprM other = traverseSinglyNestedExprsM traverseExprM other traverseExprM other =
traverseSinglyNestedExprsM traverseExprM other
>>= traverseExprTypesM traverseTypeM
traverseLHSM :: LHS -> Scope LHS traverseLHSM :: LHS -> Scope LHS
traverseLHSM (LHSIdent x) = resolveIdent x >>= return . LHSIdent traverseLHSM (LHSIdent x) = resolveIdent x >>= return . LHSIdent
...@@ -735,9 +751,9 @@ traverseStmtIdentsM identMapper = fullMapper ...@@ -735,9 +751,9 @@ traverseStmtIdentsM identMapper = fullMapper
-- visits all identifiers in a declaration -- visits all identifiers in a declaration
traverseDeclIdentsM :: Monad m => MapperM m Identifier -> MapperM m Decl traverseDeclIdentsM :: Monad m => MapperM m Identifier -> MapperM m Decl
traverseDeclIdentsM identMapper = traverseDeclIdentsM identMapper = traverseDeclNodesM
traverseDeclExprsM (traverseExprIdentsM identMapper) >=> (traverseTypeIdentsM identMapper)
traverseDeclTypesM (traverseTypeIdentsM identMapper) (traverseExprIdentsM identMapper)
-- returns any names defined by a package item -- returns any names defined by a package item
piNames :: PackageItem -> [Identifier] piNames :: PackageItem -> [Identifier]
......
...@@ -142,8 +142,7 @@ convert files = ...@@ -142,8 +142,7 @@ convert files =
then Parameter then Parameter
else Localparam else Localparam
rewriteDecl other = rewriteDecl other =
traverseDeclTypes rewriteType $ traverseDeclNodes rewriteType rewriteExpr other
traverseDeclExprs rewriteExpr other
additionalParamItems = concatMap makeAddedParams $ additionalParamItems = concatMap makeAddedParams $
Map.toList $ Map.map snd inst Map.toList $ Map.map snd inst
rewriteExpr :: Expr -> Expr rewriteExpr :: Expr -> Expr
......
...@@ -10,6 +10,7 @@ module Convert.Traverse ...@@ -10,6 +10,7 @@ module Convert.Traverse
, CollectorM , CollectorM
, unmonad , unmonad
, collectify , collectify
, mapBothM
, traverseDescriptionsM , traverseDescriptionsM
, traverseDescriptions , traverseDescriptions
, collectDescriptionsM , collectDescriptionsM
...@@ -54,6 +55,8 @@ module Convert.Traverse ...@@ -54,6 +55,8 @@ module Convert.Traverse
, traverseGenItemExprsM , traverseGenItemExprsM
, traverseGenItemExprs , traverseGenItemExprs
, collectGenItemExprsM , collectGenItemExprsM
, traverseDeclNodesM
, traverseDeclNodes
, traverseDeclExprsM , traverseDeclExprsM
, traverseDeclExprs , traverseDeclExprs
, collectDeclExprsM , collectDeclExprsM
...@@ -531,7 +534,7 @@ traverseExprsM :: Monad m => MapperM m Expr -> MapperM m ModuleItem ...@@ -531,7 +534,7 @@ traverseExprsM :: Monad m => MapperM m Expr -> MapperM m ModuleItem
traverseExprsM exprMapper = traverseExprsM exprMapper =
traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper
where where
declMapper = traverseDeclExprsM exprMapper declMapper = traverseDeclNodesM typeMapper exprMapper
typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper) typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper) lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper)
stmtMapper = traverseNestedStmtsM (traverseStmtExprsM exprMapper) stmtMapper = traverseNestedStmtsM (traverseStmtExprsM exprMapper)
...@@ -935,12 +938,11 @@ traverseGenItemExprs = unmonad traverseGenItemExprsM ...@@ -935,12 +938,11 @@ traverseGenItemExprs = unmonad traverseGenItemExprsM
collectGenItemExprsM :: Monad m => CollectorM m Expr -> CollectorM m GenItem collectGenItemExprsM :: Monad m => CollectorM m Expr -> CollectorM m GenItem
collectGenItemExprsM = collectify traverseGenItemExprsM collectGenItemExprsM = collectify traverseGenItemExprsM
traverseDeclExprsM :: Monad m => MapperM m Expr -> MapperM m Decl traverseDeclNodesM
traverseDeclExprsM exprMapper = :: Monad m => MapperM m Type -> MapperM m Expr -> MapperM m Decl
traverseDeclNodesM typeMapper exprMapper =
declMapper declMapper
where where
typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
declMapper (Param s t x e) = do declMapper (Param s t x e) = do
t' <- typeMapper t t' <- typeMapper t
e' <- exprMapper e e' <- exprMapper e
...@@ -961,21 +963,24 @@ traverseDeclExprsM exprMapper = ...@@ -961,21 +963,24 @@ traverseDeclExprsM exprMapper =
declMapper (CommentDecl c) = declMapper (CommentDecl c) =
return $ CommentDecl c return $ CommentDecl c
traverseDeclNodes :: Mapper Type -> Mapper Expr -> Mapper Decl
traverseDeclNodes typeMapper exprMapper =
runIdentity . traverseDeclNodesM
(return . typeMapper)
(return . exprMapper)
traverseDeclExprsM :: Monad m => MapperM m Expr -> MapperM m Decl
traverseDeclExprsM exprMapper = traverseDeclNodesM typeMapper exprMapper
where typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
traverseDeclExprs :: Mapper Expr -> Mapper Decl traverseDeclExprs :: Mapper Expr -> Mapper Decl
traverseDeclExprs = unmonad traverseDeclExprsM traverseDeclExprs = unmonad traverseDeclExprsM
collectDeclExprsM :: Monad m => CollectorM m Expr -> CollectorM m Decl collectDeclExprsM :: Monad m => CollectorM m Expr -> CollectorM m Decl
collectDeclExprsM = collectify traverseDeclExprsM collectDeclExprsM = collectify traverseDeclExprsM
traverseDeclTypesM :: Monad m => MapperM m Type -> MapperM m Decl traverseDeclTypesM :: Monad m => MapperM m Type -> MapperM m Decl
traverseDeclTypesM mapper (Param s t x e) = traverseDeclTypesM typeMapper = traverseDeclNodesM typeMapper exprMapper
mapper t >>= \t' -> return $ Param s t' x e where exprMapper = traverseNestedExprsM (traverseExprTypesM typeMapper)
traverseDeclTypesM mapper (ParamType s x t) =
mapper t >>= \t' -> return $ ParamType s x t'
traverseDeclTypesM mapper (Variable d t x a e) =
mapper t >>= \t' -> return $ Variable d t' x a e
traverseDeclTypesM mapper (Net d n s t x a e) =
mapper t >>= \t' -> return $ Net d n s t' x a e
traverseDeclTypesM _ (CommentDecl c) = return $ CommentDecl c
traverseDeclTypes :: Mapper Type -> Mapper Decl traverseDeclTypes :: Mapper Type -> Mapper Decl
traverseDeclTypes = unmonad traverseDeclTypesM traverseDeclTypes = unmonad traverseDeclTypesM
...@@ -990,8 +995,7 @@ traverseTypesM typeMapper = ...@@ -990,8 +995,7 @@ traverseTypesM typeMapper =
lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper) lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper)
stmtMapper = traverseNestedStmtsM $ stmtMapper = traverseNestedStmtsM $
traverseStmtDeclsM declMapper >=> traverseStmtExprsM exprMapper traverseStmtDeclsM declMapper >=> traverseStmtExprsM exprMapper
declMapper = declMapper = traverseDeclNodesM typeMapper exprMapper
traverseDeclExprsM exprMapper >=> traverseDeclTypesM typeMapper
traverseTypes :: Mapper Type -> Mapper ModuleItem traverseTypes :: Mapper Type -> Mapper ModuleItem
traverseTypes = unmonad traverseTypesM traverseTypes = unmonad traverseTypesM
......
...@@ -44,8 +44,7 @@ traverseDeclM :: Decl -> ST Decl ...@@ -44,8 +44,7 @@ traverseDeclM :: Decl -> ST Decl
traverseDeclM decl @ Net{} = traverseDeclM decl @ Net{} =
traverseNetAsVarM traverseDeclM decl traverseNetAsVarM traverseDeclM decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- traverseDeclExprsM traverseExprM decl decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
>>= traverseDeclTypesM traverseTypeM
case decl' of case decl' of
Variable _ (Implicit sg rs) ident a _ -> Variable _ (Implicit sg rs) ident a _ ->
-- implicit types, which are commonly found in function return -- implicit types, which are commonly found in function return
......
...@@ -72,8 +72,7 @@ traverseGenItemM = traverseGenItemExprsM traverseExprM ...@@ -72,8 +72,7 @@ traverseGenItemM = traverseGenItemExprsM traverseExprM
traverseDeclM :: Decl -> Scoper Type Decl traverseDeclM :: Decl -> Scoper Type Decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- traverseDeclExprsM traverseExprM decl decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
>>= traverseDeclTypesM traverseTypeM
case decl' of case decl' of
Variable{} -> return decl' Variable{} -> return decl'
Net{} -> return decl' Net{} -> return decl'
...@@ -110,7 +109,4 @@ traverseTypeM other = ...@@ -110,7 +109,4 @@ traverseTypeM other =
>>= traverseTypeExprsM traverseExprM >>= traverseTypeExprsM traverseExprM
traverseRangeM :: Range -> Scoper Type Range traverseRangeM :: Range -> Scoper Type Range
traverseRangeM (a, b) = do traverseRangeM = mapBothM traverseExprM
a' <- traverseExprM a
b' <- traverseExprM b
return (a', b')
module top;
localparam type T = int unsigned;
wire [T'(32'sd32) - 1:0] x;
function automatic [T'(32'sd32) - 1:0] foo;
input reg [T'(32'sd32) - 1:0] inp;
foo = inp;
endfunction
assign x = foo(1'sb1);
endmodule
module top;
wire [31:0] x = 1'sb1;
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment