From 378ede9e1a5def785de0d2d63691fd2951206c96 Mon Sep 17 00:00:00 2001 From: Zachary Snow Date: Sun, 24 Jan 2021 09:55:03 -0700 Subject: [PATCH] standardize internal representation of unknown types --- src/Convert/Interface.hs | 8 ++++---- src/Convert/ParamNoDefault.hs | 8 ++++---- src/Convert/ParamType.hs | 12 ++++++++---- src/Convert/StringParam.hs | 4 ---- src/Convert/Traverse.hs | 10 +++++----- src/Convert/TypeOf.hs | 4 ---- src/Convert/Typedef.hs | 2 +- src/Language/SystemVerilog/AST/Decl.hs | 9 +++++---- src/Language/SystemVerilog/AST/Type.hs | 6 ++++++ src/Language/SystemVerilog/Parser/Parse.y | 10 +++++----- 10 files changed, 38 insertions(+), 35 deletions(-) diff --git a/src/Convert/Interface.hs b/src/Convert/Interface.hs index 25c275f..4bedff6 100644 --- a/src/Convert/Interface.hs +++ b/src/Convert/Interface.hs @@ -490,7 +490,7 @@ inlineInstance ranges modportBindings items parameterBinds = map makeParameterBind instanceParams makeParameterBind :: ParamBinding -> Decl makeParameterBind (x, Left t) = - ParamType Localparam (paramTmp ++ x) (Just t) + ParamType Localparam (paramTmp ++ x) t makeParameterBind (x, Right e) = Param Localparam (TypeOf e) (paramTmp ++ x) e @@ -501,11 +501,11 @@ inlineInstance ranges modportBindings items Just (Right _) -> Param Localparam t x (Ident $ paramTmp ++ x) Just (Left t') -> error $ inlineKind ++ " param " ++ x ++ " expected expr, found type: " ++ show t' - overrideParam (ParamType Parameter x mt) = + overrideParam (ParamType Parameter x t) = case lookup x instanceParams of - Nothing -> ParamType Localparam x mt + Nothing -> ParamType Localparam x t Just (Left _) -> - ParamType Localparam x (Just $ Alias (paramTmp ++ x) []) + ParamType Localparam x $ Alias (paramTmp ++ x) [] Just (Right e') -> error $ inlineKind ++ " param " ++ x ++ " expected type, found expr: " ++ show e' overrideParam other = other diff --git a/src/Convert/ParamNoDefault.hs b/src/Convert/ParamNoDefault.hs index 5893b28..3c121da 100644 --- a/src/Convert/ParamNoDefault.hs +++ b/src/Convert/ParamNoDefault.hs @@ -55,12 +55,12 @@ traverseDeclM (Param Parameter t x e) = do return $ if e == Nil then Param Parameter t x $ RawNum 0 else Param Parameter t x e -traverseDeclM (ParamType Localparam x Nothing) = +traverseDeclM (ParamType Localparam x UnknownType) = error $ "localparam type " ++ show x ++ " has no default value" -traverseDeclM (ParamType Parameter x mt) = do +traverseDeclM (ParamType Parameter x t) = do -- parameter types are rewritten separately, so no fake default here - tell [(x, mt == Nothing)] - return $ ParamType Parameter x mt + tell [(x, t == UnknownType)] + return $ ParamType Parameter x t traverseDeclM other = return other -- check for instances missing values for parameters without defaults diff --git a/src/Convert/ParamType.hs b/src/Convert/ParamType.hs index e7a1bf0..e1d5657 100644 --- a/src/Convert/ParamType.hs +++ b/src/Convert/ParamType.hs @@ -116,7 +116,7 @@ convert files = traverseModuleItems (traverseDecls rewriteDecl) part rewriteDecl :: Decl -> Decl rewriteDecl (ParamType Parameter x _) = - ParamType Parameter x Nothing + ParamType Parameter x UnknownType rewriteDecl other = other removeDefaultTypeParams _ = error "not possible" @@ -152,7 +152,7 @@ convert files = items' = map (traverseDecls rewriteDecl) items rewriteDecl :: Decl -> Decl rewriteDecl (ParamType Parameter x _) = - ParamType Localparam x (Just $ fst $ typeMap Map.! x) + ParamType Localparam x (fst $ typeMap Map.! x) rewriteDecl other = other additionalParamItems = concatMap makeAddedParams $ Map.toList $ Map.map snd typeMap @@ -170,7 +170,7 @@ convert files = typ = Alias (addedParamTypeName paramName ident) [] name = addedParamName paramName ident toTypeParam :: Identifier -> Decl - toTypeParam ident = ParamType Parameter name Nothing + toTypeParam ident = ParamType Parameter name UnknownType where name = addedParamTypeName paramName ident -- write down module parameter names and type parameters @@ -184,9 +184,13 @@ collectDescriptionM (part @ (Part _ _ _ _ name _ _)) = maybeTypeMap = Map.fromList $ map (\(x, y) -> (x, fromJust y)) $ filter (isJust . snd) params + --- TODO FIXME XXX collectDeclM :: Decl -> Writer [(Identifier, Maybe (Maybe Type))] () collectDeclM (Param Parameter _ x _) = tell [(x, Nothing)] - collectDeclM (ParamType Parameter x v) = tell [(x, Just v )] + collectDeclM (ParamType Parameter x v) = + if v == UnknownType + then tell [(x, Just Nothing)] + else tell [(x, Just $ Just v)] collectDeclM _ = return () collectDescriptionM _ = return () diff --git a/src/Convert/StringParam.hs b/src/Convert/StringParam.hs index 83b610c..56eecc9 100644 --- a/src/Convert/StringParam.hs +++ b/src/Convert/StringParam.hs @@ -1,4 +1,3 @@ -{-# LANGUAGE PatternSynonyms #-} {- sv2v - Author: Zachary Snow - @@ -52,9 +51,6 @@ parameterNames = collectDeclM (ParamType Parameter x _) = tell [x] collectDeclM _ = return () -pattern UnknownType :: Type -pattern UnknownType = Implicit Unspecified [] - -- rewrite an existing string parameter traverseModuleItemM :: ModuleItem -> Writer [Identifier] ModuleItem traverseModuleItemM (orig @ (MIPackageItem (Decl (Param Parameter t x e)))) = diff --git a/src/Convert/Traverse.hs b/src/Convert/Traverse.hs index def7e96..6629290 100644 --- a/src/Convert/Traverse.hs +++ b/src/Convert/Traverse.hs @@ -913,9 +913,9 @@ traverseDeclExprsM exprMapper = t' <- typeMapper t e' <- exprMapper e return $ Param s t' x e' - declMapper (ParamType s x mt) = do - mt' <- mapM typeMapper mt - return $ ParamType s x mt' + declMapper (ParamType s x t) = do + t' <- typeMapper t + return $ ParamType s x t' declMapper (Variable d t x a e) = do t' <- typeMapper t a' <- mapM (mapBothM exprMapper) a @@ -932,8 +932,8 @@ 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 mt) = - mapM mapper mt >>= \mt' -> return $ ParamType s x mt' +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 _ (CommentDecl c) = return $ CommentDecl c diff --git a/src/Convert/TypeOf.hs b/src/Convert/TypeOf.hs index 98ea1c7..6367cb9 100644 --- a/src/Convert/TypeOf.hs +++ b/src/Convert/TypeOf.hs @@ -27,10 +27,6 @@ convert :: [AST] -> [AST] convert = map $ traverseDescriptions $ partScoper traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM --- internal representation of a fully implicit type -pattern UnknownType :: Type -pattern UnknownType = Implicit Unspecified [] - -- single bit 4-state `logic` type pattern UnitType :: Type pattern UnitType = IntegerVector TLogic Unspecified [] diff --git a/src/Convert/Typedef.hs b/src/Convert/Typedef.hs index f4b5c38..eeefc26 100644 --- a/src/Convert/Typedef.hs +++ b/src/Convert/Typedef.hs @@ -70,7 +70,7 @@ traverseDeclM decl = do case decl' of Variable{} -> return decl' Param{} -> return decl' - ParamType Localparam x (Just t) -> do + ParamType Localparam x t -> do t' <- traverseNestedTypesM traverseTypeM t insertElem x t' return $ CommentDecl $ "removed localparam type " ++ x diff --git a/src/Language/SystemVerilog/AST/Decl.hs b/src/Language/SystemVerilog/AST/Decl.hs index 677298b..05444c7 100644 --- a/src/Language/SystemVerilog/AST/Decl.hs +++ b/src/Language/SystemVerilog/AST/Decl.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE PatternSynonyms #-} {- sv2v - Author: Zachary Snow - Initial Verilog AST Author: Tom Hawkins @@ -16,12 +17,12 @@ module Language.SystemVerilog.AST.Decl import Text.Printf (printf) import Language.SystemVerilog.AST.ShowHelp (showPad, unlines') -import Language.SystemVerilog.AST.Type (Type, Identifier) +import Language.SystemVerilog.AST.Type (Type, Identifier, pattern UnknownType) import Language.SystemVerilog.AST.Expr (Expr, Range, showRanges, showAssignment) data Decl = Param ParamScope Type Identifier Expr - | ParamType ParamScope Identifier (Maybe Type) + | ParamType ParamScope Identifier Type | Variable Direction Type Identifier [Range] Expr | CommentDecl String deriving (Eq, Ord) @@ -29,8 +30,8 @@ data Decl instance Show Decl where showList l _ = unlines' $ map show l show (Param s t x e) = printf "%s %s%s%s;" (show s) (showPad t) x (showAssignment e) - show (ParamType s x mt) = printf "%s type %s%s;" (show s) x tStr - where tStr = maybe "" ((" = " ++) . show) mt + show (ParamType s x t) = printf "%s type %s%s;" (show s) x tStr + where tStr = if t == UnknownType then "" else " = " ++ show t show (Variable d t x a e) = printf "%s%s%s%s%s;" (showPad d) (showPad t) x (showRanges a) (showAssignment e) show (CommentDecl c) = if elem '\n' c diff --git a/src/Language/SystemVerilog/AST/Type.hs b/src/Language/SystemVerilog/AST/Type.hs index f1fea7e..4112f79 100644 --- a/src/Language/SystemVerilog/AST/Type.hs +++ b/src/Language/SystemVerilog/AST/Type.hs @@ -1,4 +1,5 @@ {-# LANGUAGE FlexibleInstances #-} +{-# LANGUAGE PatternSynonyms #-} {- sv2v - Author: Zachary Snow - Initial Verilog AST Author: Tom Hawkins @@ -21,6 +22,7 @@ module Language.SystemVerilog.AST.Type , Strength0 (..) , Strength1 (..) , ChargeStrength (..) + , pattern UnknownType , typeRanges , nullRange , elaborateIntegerAtom @@ -80,6 +82,10 @@ showFields items = itemsStr itemsStr = indent $ unlines' $ map showItem items showItem (t, x) = printf "%s %s;" (show t) x +-- internal representation of a fully implicit or unknown type +pattern UnknownType :: Type +pattern UnknownType = Implicit Unspecified [] + instance Show ([Range] -> Type) where show tf = show (tf []) instance Eq ([Range] -> Type) where diff --git a/src/Language/SystemVerilog/Parser/Parse.y b/src/Language/SystemVerilog/Parser/Parse.y index b5e9e15..028e178 100644 --- a/src/Language/SystemVerilog/Parser/Parse.y +++ b/src/Language/SystemVerilog/Parser/Parse.y @@ -1077,12 +1077,12 @@ ParameterDeclKW :: { ParamScope } : "parameter" { Parameter } | "localparam" { Localparam } -TypeAsgns :: { [(Identifier, Maybe Type)] } +TypeAsgns :: { [(Identifier, Type)] } : TypeAsgn { [$1] } | TypeAsgns "," TypeAsgn { $1 ++ [$3] } -TypeAsgn :: { (Identifier, Maybe Type) } - : Identifier "=" Type { ($1, Just $3) } - | Identifier { ($1, Nothing) } +TypeAsgn :: { (Identifier, Type) } + : Identifier "=" Type { ($1, $3) } + | Identifier { ($1, UnknownType) } -- TODO: This does not allow for @identifier ClockingEvent :: { Sense } @@ -1476,7 +1476,7 @@ validateGenCases items = makeLocalparam :: Decl -> Decl makeLocalparam (Param _ t x e) = Param Localparam t x e -makeLocalparam (ParamType _ x mt) = ParamType Localparam x mt +makeLocalparam (ParamType _ x t) = ParamType Localparam x t makeLocalparam other = other }