Commit 378ede9e by Zachary Snow

standardize internal representation of unknown types

parent 58e5bfa6
...@@ -490,7 +490,7 @@ inlineInstance ranges modportBindings items ...@@ -490,7 +490,7 @@ inlineInstance ranges modportBindings items
parameterBinds = map makeParameterBind instanceParams parameterBinds = map makeParameterBind instanceParams
makeParameterBind :: ParamBinding -> Decl makeParameterBind :: ParamBinding -> Decl
makeParameterBind (x, Left t) = makeParameterBind (x, Left t) =
ParamType Localparam (paramTmp ++ x) (Just t) ParamType Localparam (paramTmp ++ x) t
makeParameterBind (x, Right e) = makeParameterBind (x, Right e) =
Param Localparam (TypeOf e) (paramTmp ++ x) e Param Localparam (TypeOf e) (paramTmp ++ x) e
...@@ -501,11 +501,11 @@ inlineInstance ranges modportBindings items ...@@ -501,11 +501,11 @@ inlineInstance ranges modportBindings items
Just (Right _) -> Param Localparam t x (Ident $ paramTmp ++ x) Just (Right _) -> Param Localparam t x (Ident $ paramTmp ++ x)
Just (Left t') -> error $ inlineKind ++ " param " ++ x Just (Left t') -> error $ inlineKind ++ " param " ++ x
++ " expected expr, found type: " ++ show t' ++ " expected expr, found type: " ++ show t'
overrideParam (ParamType Parameter x mt) = overrideParam (ParamType Parameter x t) =
case lookup x instanceParams of case lookup x instanceParams of
Nothing -> ParamType Localparam x mt Nothing -> ParamType Localparam x t
Just (Left _) -> Just (Left _) ->
ParamType Localparam x (Just $ Alias (paramTmp ++ x) []) ParamType Localparam x $ Alias (paramTmp ++ x) []
Just (Right e') -> error $ inlineKind ++ " param " ++ x Just (Right e') -> error $ inlineKind ++ " param " ++ x
++ " expected type, found expr: " ++ show e' ++ " expected type, found expr: " ++ show e'
overrideParam other = other overrideParam other = other
......
...@@ -55,12 +55,12 @@ traverseDeclM (Param Parameter t x e) = do ...@@ -55,12 +55,12 @@ traverseDeclM (Param Parameter t x e) = do
return $ if e == Nil return $ if e == Nil
then Param Parameter t x $ RawNum 0 then Param Parameter t x $ RawNum 0
else Param Parameter t x e 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" 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 -- parameter types are rewritten separately, so no fake default here
tell [(x, mt == Nothing)] tell [(x, t == UnknownType)]
return $ ParamType Parameter x mt return $ ParamType Parameter x t
traverseDeclM other = return other traverseDeclM other = return other
-- check for instances missing values for parameters without defaults -- check for instances missing values for parameters without defaults
......
...@@ -116,7 +116,7 @@ convert files = ...@@ -116,7 +116,7 @@ convert files =
traverseModuleItems (traverseDecls rewriteDecl) part traverseModuleItems (traverseDecls rewriteDecl) part
rewriteDecl :: Decl -> Decl rewriteDecl :: Decl -> Decl
rewriteDecl (ParamType Parameter x _) = rewriteDecl (ParamType Parameter x _) =
ParamType Parameter x Nothing ParamType Parameter x UnknownType
rewriteDecl other = other rewriteDecl other = other
removeDefaultTypeParams _ = error "not possible" removeDefaultTypeParams _ = error "not possible"
...@@ -152,7 +152,7 @@ convert files = ...@@ -152,7 +152,7 @@ convert files =
items' = map (traverseDecls rewriteDecl) items items' = map (traverseDecls rewriteDecl) items
rewriteDecl :: Decl -> Decl rewriteDecl :: Decl -> Decl
rewriteDecl (ParamType Parameter x _) = rewriteDecl (ParamType Parameter x _) =
ParamType Localparam x (Just $ fst $ typeMap Map.! x) ParamType Localparam x (fst $ typeMap Map.! x)
rewriteDecl other = other rewriteDecl other = other
additionalParamItems = concatMap makeAddedParams $ additionalParamItems = concatMap makeAddedParams $
Map.toList $ Map.map snd typeMap Map.toList $ Map.map snd typeMap
...@@ -170,7 +170,7 @@ convert files = ...@@ -170,7 +170,7 @@ convert files =
typ = Alias (addedParamTypeName paramName ident) [] typ = Alias (addedParamTypeName paramName ident) []
name = addedParamName paramName ident name = addedParamName paramName ident
toTypeParam :: Identifier -> Decl toTypeParam :: Identifier -> Decl
toTypeParam ident = ParamType Parameter name Nothing toTypeParam ident = ParamType Parameter name UnknownType
where name = addedParamTypeName paramName ident where name = addedParamTypeName paramName ident
-- write down module parameter names and type parameters -- write down module parameter names and type parameters
...@@ -184,9 +184,13 @@ collectDescriptionM (part @ (Part _ _ _ _ name _ _)) = ...@@ -184,9 +184,13 @@ collectDescriptionM (part @ (Part _ _ _ _ name _ _)) =
maybeTypeMap = Map.fromList $ maybeTypeMap = Map.fromList $
map (\(x, y) -> (x, fromJust y)) $ map (\(x, y) -> (x, fromJust y)) $
filter (isJust . snd) params filter (isJust . snd) params
--- TODO FIXME XXX
collectDeclM :: Decl -> Writer [(Identifier, Maybe (Maybe Type))] () collectDeclM :: Decl -> Writer [(Identifier, Maybe (Maybe Type))] ()
collectDeclM (Param Parameter _ x _) = tell [(x, Nothing)] 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 () collectDeclM _ = return ()
collectDescriptionM _ = return () collectDescriptionM _ = return ()
......
{-# LANGUAGE PatternSynonyms #-}
{- sv2v {- sv2v
- Author: Zachary Snow <zach@zachjs.com> - Author: Zachary Snow <zach@zachjs.com>
- -
...@@ -52,9 +51,6 @@ parameterNames = ...@@ -52,9 +51,6 @@ parameterNames =
collectDeclM (ParamType Parameter x _) = tell [x] collectDeclM (ParamType Parameter x _) = tell [x]
collectDeclM _ = return () collectDeclM _ = return ()
pattern UnknownType :: Type
pattern UnknownType = Implicit Unspecified []
-- rewrite an existing string parameter -- rewrite an existing string parameter
traverseModuleItemM :: ModuleItem -> Writer [Identifier] ModuleItem traverseModuleItemM :: ModuleItem -> Writer [Identifier] ModuleItem
traverseModuleItemM (orig @ (MIPackageItem (Decl (Param Parameter t x e)))) = traverseModuleItemM (orig @ (MIPackageItem (Decl (Param Parameter t x e)))) =
......
...@@ -913,9 +913,9 @@ traverseDeclExprsM exprMapper = ...@@ -913,9 +913,9 @@ traverseDeclExprsM exprMapper =
t' <- typeMapper t t' <- typeMapper t
e' <- exprMapper e e' <- exprMapper e
return $ Param s t' x e' return $ Param s t' x e'
declMapper (ParamType s x mt) = do declMapper (ParamType s x t) = do
mt' <- mapM typeMapper mt t' <- typeMapper t
return $ ParamType s x mt' return $ ParamType s x t'
declMapper (Variable d t x a e) = do declMapper (Variable d t x a e) = do
t' <- typeMapper t t' <- typeMapper t
a' <- mapM (mapBothM exprMapper) a a' <- mapM (mapBothM exprMapper) a
...@@ -932,8 +932,8 @@ collectDeclExprsM = collectify traverseDeclExprsM ...@@ -932,8 +932,8 @@ 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 mapper (Param s t x e) =
mapper t >>= \t' -> return $ Param s t' x e mapper t >>= \t' -> return $ Param s t' x e
traverseDeclTypesM mapper (ParamType s x mt) = traverseDeclTypesM mapper (ParamType s x t) =
mapM mapper mt >>= \mt' -> return $ ParamType s x mt' mapper t >>= \t' -> return $ ParamType s x t'
traverseDeclTypesM mapper (Variable d t x a e) = traverseDeclTypesM mapper (Variable d t x a e) =
mapper t >>= \t' -> return $ Variable d t' x a e mapper t >>= \t' -> return $ Variable d t' x a e
traverseDeclTypesM _ (CommentDecl c) = return $ CommentDecl c traverseDeclTypesM _ (CommentDecl c) = return $ CommentDecl c
......
...@@ -27,10 +27,6 @@ convert :: [AST] -> [AST] ...@@ -27,10 +27,6 @@ convert :: [AST] -> [AST]
convert = map $ traverseDescriptions $ partScoper convert = map $ traverseDescriptions $ partScoper
traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM traverseDeclM traverseModuleItemM traverseGenItemM traverseStmtM
-- internal representation of a fully implicit type
pattern UnknownType :: Type
pattern UnknownType = Implicit Unspecified []
-- single bit 4-state `logic` type -- single bit 4-state `logic` type
pattern UnitType :: Type pattern UnitType :: Type
pattern UnitType = IntegerVector TLogic Unspecified [] pattern UnitType = IntegerVector TLogic Unspecified []
......
...@@ -70,7 +70,7 @@ traverseDeclM decl = do ...@@ -70,7 +70,7 @@ traverseDeclM decl = do
case decl' of case decl' of
Variable{} -> return decl' Variable{} -> return decl'
Param{} -> return decl' Param{} -> return decl'
ParamType Localparam x (Just t) -> do ParamType Localparam x t -> do
t' <- traverseNestedTypesM traverseTypeM t t' <- traverseNestedTypesM traverseTypeM t
insertElem x t' insertElem x t'
return $ CommentDecl $ "removed localparam type " ++ x return $ CommentDecl $ "removed localparam type " ++ x
......
{-# LANGUAGE PatternSynonyms #-}
{- sv2v {- sv2v
- Author: Zachary Snow <zach@zachjs.com> - Author: Zachary Snow <zach@zachjs.com>
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com> - Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
...@@ -16,12 +17,12 @@ module Language.SystemVerilog.AST.Decl ...@@ -16,12 +17,12 @@ module Language.SystemVerilog.AST.Decl
import Text.Printf (printf) import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp (showPad, unlines') 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) import Language.SystemVerilog.AST.Expr (Expr, Range, showRanges, showAssignment)
data Decl data Decl
= Param ParamScope Type Identifier Expr = Param ParamScope Type Identifier Expr
| ParamType ParamScope Identifier (Maybe Type) | ParamType ParamScope Identifier Type
| Variable Direction Type Identifier [Range] Expr | Variable Direction Type Identifier [Range] Expr
| CommentDecl String | CommentDecl String
deriving (Eq, Ord) deriving (Eq, Ord)
...@@ -29,8 +30,8 @@ data Decl ...@@ -29,8 +30,8 @@ data Decl
instance Show Decl where instance Show Decl where
showList l _ = unlines' $ map show l 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 (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 show (ParamType s x t) = printf "%s type %s%s;" (show s) x tStr
where tStr = maybe "" ((" = " ++) . show) mt 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 (Variable d t x a e) = printf "%s%s%s%s%s;" (showPad d) (showPad t) x (showRanges a) (showAssignment e)
show (CommentDecl c) = show (CommentDecl c) =
if elem '\n' c if elem '\n' c
......
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE PatternSynonyms #-}
{- sv2v {- sv2v
- Author: Zachary Snow <zach@zachjs.com> - Author: Zachary Snow <zach@zachjs.com>
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com> - Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
...@@ -21,6 +22,7 @@ module Language.SystemVerilog.AST.Type ...@@ -21,6 +22,7 @@ module Language.SystemVerilog.AST.Type
, Strength0 (..) , Strength0 (..)
, Strength1 (..) , Strength1 (..)
, ChargeStrength (..) , ChargeStrength (..)
, pattern UnknownType
, typeRanges , typeRanges
, nullRange , nullRange
, elaborateIntegerAtom , elaborateIntegerAtom
...@@ -80,6 +82,10 @@ showFields items = itemsStr ...@@ -80,6 +82,10 @@ showFields items = itemsStr
itemsStr = indent $ unlines' $ map showItem items itemsStr = indent $ unlines' $ map showItem items
showItem (t, x) = printf "%s %s;" (show t) x 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 instance Show ([Range] -> Type) where
show tf = show (tf []) show tf = show (tf [])
instance Eq ([Range] -> Type) where instance Eq ([Range] -> Type) where
......
...@@ -1077,12 +1077,12 @@ ParameterDeclKW :: { ParamScope } ...@@ -1077,12 +1077,12 @@ ParameterDeclKW :: { ParamScope }
: "parameter" { Parameter } : "parameter" { Parameter }
| "localparam" { Localparam } | "localparam" { Localparam }
TypeAsgns :: { [(Identifier, Maybe Type)] } TypeAsgns :: { [(Identifier, Type)] }
: TypeAsgn { [$1] } : TypeAsgn { [$1] }
| TypeAsgns "," TypeAsgn { $1 ++ [$3] } | TypeAsgns "," TypeAsgn { $1 ++ [$3] }
TypeAsgn :: { (Identifier, Maybe Type) } TypeAsgn :: { (Identifier, Type) }
: Identifier "=" Type { ($1, Just $3) } : Identifier "=" Type { ($1, $3) }
| Identifier { ($1, Nothing) } | Identifier { ($1, UnknownType) }
-- TODO: This does not allow for @identifier -- TODO: This does not allow for @identifier
ClockingEvent :: { Sense } ClockingEvent :: { Sense }
...@@ -1476,7 +1476,7 @@ validateGenCases items = ...@@ -1476,7 +1476,7 @@ validateGenCases items =
makeLocalparam :: Decl -> Decl makeLocalparam :: Decl -> Decl
makeLocalparam (Param _ t x e) = Param Localparam t x e 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 makeLocalparam other = other
} }
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