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]
convert = map $ traverseDescriptions $ traverseModuleItems convertModuleItem
convertModuleItem :: ModuleItem -> ModuleItem
convertModuleItem =
traverseTypes (traverseNestedTypes convertType) .
traverseExprs (traverseNestedExprs convertExpr)
convertModuleItem = traverseNodes
traverseExpr traverseDecl traverseType traverseLHS traverseStmt
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 (Struct pk fields rs) =
......
......@@ -310,8 +310,7 @@ processItems topName packageName moduleItems = do
traverseDeclM :: Decl -> Scope Decl
traverseDeclM decl = do
decl' <- traverseDeclTypesM traverseTypeM decl
>>= traverseDeclExprsM traverseExprM
decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
case decl' of
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
......@@ -320,21 +319,36 @@ processItems topName packageName moduleItems = do
CommentDecl c -> return $ CommentDecl c
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 (CSAlias p b x rs) = do
x' <- resolveCSIdent' p b x
return $ Alias x' rs
rs' <- mapM traverseRangeM rs
return $ Alias x' rs'
traverseTypeM (PSAlias p x rs) = do
x' <- resolvePSIdent' p x
return $ Alias x' rs
traverseTypeM (Alias x rs) =
resolveIdent x >>= \x' -> return $ Alias x' rs
rs' <- mapM traverseRangeM rs
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
t' <- traverseTypeM t
enumItems' <- mapM prefixEnumItem enumItems
return $ Enum t' enumItems' rs
where prefixEnumItem (x, e) = prefixIdent x >>= \x' -> return (x', e)
traverseTypeM other = traverseSinglyNestedTypesM traverseTypeM other
enumItems' <- mapM traverseEnumItemM enumItems
rs' <- mapM traverseRangeM rs
return $ Enum t' enumItems' rs'
traverseTypeM other =
traverseSinglyNestedTypesM traverseTypeM other
>>= traverseTypeExprsM traverseExprM
traverseExprM :: Expr -> Scope Expr
traverseExprM (CSIdent p b x) = do
......@@ -344,7 +358,9 @@ processItems topName packageName moduleItems = do
x' <- resolvePSIdent' p x
return $ Ident x'
traverseExprM (Ident x) = resolveIdent x >>= return . Ident
traverseExprM other = traverseSinglyNestedExprsM traverseExprM other
traverseExprM other =
traverseSinglyNestedExprsM traverseExprM other
>>= traverseExprTypesM traverseTypeM
traverseLHSM :: LHS -> Scope LHS
traverseLHSM (LHSIdent x) = resolveIdent x >>= return . LHSIdent
......@@ -735,9 +751,9 @@ traverseStmtIdentsM identMapper = fullMapper
-- visits all identifiers in a declaration
traverseDeclIdentsM :: Monad m => MapperM m Identifier -> MapperM m Decl
traverseDeclIdentsM identMapper =
traverseDeclExprsM (traverseExprIdentsM identMapper) >=>
traverseDeclTypesM (traverseTypeIdentsM identMapper)
traverseDeclIdentsM identMapper = traverseDeclNodesM
(traverseTypeIdentsM identMapper)
(traverseExprIdentsM identMapper)
-- returns any names defined by a package item
piNames :: PackageItem -> [Identifier]
......
......@@ -142,8 +142,7 @@ convert files =
then Parameter
else Localparam
rewriteDecl other =
traverseDeclTypes rewriteType $
traverseDeclExprs rewriteExpr other
traverseDeclNodes rewriteType rewriteExpr other
additionalParamItems = concatMap makeAddedParams $
Map.toList $ Map.map snd inst
rewriteExpr :: Expr -> Expr
......
......@@ -10,6 +10,7 @@ module Convert.Traverse
, CollectorM
, unmonad
, collectify
, mapBothM
, traverseDescriptionsM
, traverseDescriptions
, collectDescriptionsM
......@@ -54,6 +55,8 @@ module Convert.Traverse
, traverseGenItemExprsM
, traverseGenItemExprs
, collectGenItemExprsM
, traverseDeclNodesM
, traverseDeclNodes
, traverseDeclExprsM
, traverseDeclExprs
, collectDeclExprsM
......@@ -531,7 +534,7 @@ traverseExprsM :: Monad m => MapperM m Expr -> MapperM m ModuleItem
traverseExprsM exprMapper =
traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper
where
declMapper = traverseDeclExprsM exprMapper
declMapper = traverseDeclNodesM typeMapper exprMapper
typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper)
stmtMapper = traverseNestedStmtsM (traverseStmtExprsM exprMapper)
......@@ -935,12 +938,11 @@ traverseGenItemExprs = unmonad traverseGenItemExprsM
collectGenItemExprsM :: Monad m => CollectorM m Expr -> CollectorM m GenItem
collectGenItemExprsM = collectify traverseGenItemExprsM
traverseDeclExprsM :: Monad m => MapperM m Expr -> MapperM m Decl
traverseDeclExprsM exprMapper =
traverseDeclNodesM
:: Monad m => MapperM m Type -> MapperM m Expr -> MapperM m Decl
traverseDeclNodesM typeMapper exprMapper =
declMapper
where
typeMapper = traverseNestedTypesM (traverseTypeExprsM exprMapper)
declMapper (Param s t x e) = do
t' <- typeMapper t
e' <- exprMapper e
......@@ -961,21 +963,24 @@ traverseDeclExprsM exprMapper =
declMapper (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 = unmonad traverseDeclExprsM
collectDeclExprsM :: Monad m => CollectorM m Expr -> CollectorM m Decl
collectDeclExprsM = collectify traverseDeclExprsM
traverseDeclTypesM :: Monad m => MapperM m Type -> MapperM m Decl
traverseDeclTypesM mapper (Param s t x e) =
mapper t >>= \t' -> return $ Param s t' x e
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
traverseDeclTypesM typeMapper = traverseDeclNodesM typeMapper exprMapper
where exprMapper = traverseNestedExprsM (traverseExprTypesM typeMapper)
traverseDeclTypes :: Mapper Type -> Mapper Decl
traverseDeclTypes = unmonad traverseDeclTypesM
......@@ -990,8 +995,7 @@ traverseTypesM typeMapper =
lhsMapper = traverseNestedLHSsM (traverseLHSExprsM exprMapper)
stmtMapper = traverseNestedStmtsM $
traverseStmtDeclsM declMapper >=> traverseStmtExprsM exprMapper
declMapper =
traverseDeclExprsM exprMapper >=> traverseDeclTypesM typeMapper
declMapper = traverseDeclNodesM typeMapper exprMapper
traverseTypes :: Mapper Type -> Mapper ModuleItem
traverseTypes = unmonad traverseTypesM
......
......@@ -44,8 +44,7 @@ traverseDeclM :: Decl -> ST Decl
traverseDeclM decl @ Net{} =
traverseNetAsVarM traverseDeclM decl
traverseDeclM decl = do
decl' <- traverseDeclExprsM traverseExprM decl
>>= traverseDeclTypesM traverseTypeM
decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
case decl' of
Variable _ (Implicit sg rs) ident a _ ->
-- implicit types, which are commonly found in function return
......
......@@ -72,8 +72,7 @@ traverseGenItemM = traverseGenItemExprsM traverseExprM
traverseDeclM :: Decl -> Scoper Type Decl
traverseDeclM decl = do
decl' <- traverseDeclExprsM traverseExprM decl
>>= traverseDeclTypesM traverseTypeM
decl' <- traverseDeclNodesM traverseTypeM traverseExprM decl
case decl' of
Variable{} -> return decl'
Net{} -> return decl'
......@@ -110,7 +109,4 @@ traverseTypeM other =
>>= traverseTypeExprsM traverseExprM
traverseRangeM :: Range -> Scoper Type Range
traverseRangeM (a, b) = do
a' <- traverseExprM a
b' <- traverseExprM b
return (a', b')
traverseRangeM = mapBothM traverseExprM
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