Commit ff0c7b02 by Zachary Snow

properly distinguish nets and variables internally

- new net decl to replace net pseudo-type
- support nets with complex base types, including typenames
- support var declaration type prefix for all data types
- support var as lone type shorthand
- refactor AST representation of strengths
- traversal helpers for treating nets as variables
- use decl traversals where appropriate
parent d32c0a1b
...@@ -61,6 +61,12 @@ traverseDeclM decl = do ...@@ -61,6 +61,12 @@ traverseDeclM decl = do
else do else do
insertElem x Nil insertElem x Nil
return $ Variable d t x a e' return $ Variable d t x a e'
Net d n s t x a e -> do
enterStmt
e' <- traverseExprM e
exitStmt
insertElem x Nil
return $ Net d n s t x a e'
Param _ _ x _ -> Param _ _ x _ ->
insertElem x Nil >> return decl insertElem x Nil >> return decl
ParamType _ _ _ -> return decl ParamType _ _ _ -> return decl
......
...@@ -113,7 +113,6 @@ convertBits (Left t) = ...@@ -113,7 +113,6 @@ convertBits (Left t) =
case elaborateType t of case elaborateType t of
IntegerVector _ _ rs -> dimensionsSize rs IntegerVector _ _ rs -> dimensionsSize rs
Implicit _ rs -> dimensionsSize rs Implicit _ rs -> dimensionsSize rs
Net _ _ rs -> dimensionsSize rs
Struct _ fields rs -> Struct _ fields rs ->
BinOp Mul BinOp Mul
(dimensionsSize rs) (dimensionsSize rs)
......
...@@ -40,6 +40,7 @@ traverseDeclM :: Decl -> Scoper () Decl ...@@ -40,6 +40,7 @@ traverseDeclM :: Decl -> Scoper () Decl
traverseDeclM decl = do traverseDeclM decl = do
case decl of case decl of
Variable _ _ x _ _ -> insertElem x () Variable _ _ x _ _ -> insertElem x ()
Net _ _ _ _ x _ _ -> insertElem x ()
Param _ _ x _ -> insertElem x () Param _ _ x _ -> insertElem x ()
ParamType{} -> return () ParamType{} -> return ()
CommentDecl{} -> return () CommentDecl{} -> return ()
...@@ -81,15 +82,14 @@ needsIdent defaultNetType x = do ...@@ -81,15 +82,14 @@ needsIdent defaultNetType x = do
when (details == Nothing) $ do when (details == Nothing) $ do
insertElem x () insertElem x ()
injectItem decl injectItem decl
where where decl = MIPackageItem $ Decl $ impliedNet x defaultNetType
t = impliedNetType x defaultNetType Unspecified []
decl = MIPackageItem $ Decl $ Variable Local t x [] Nil
impliedNetType :: String -> DefaultNetType -> Signing -> [Range] -> Type impliedNet :: Identifier -> DefaultNetType -> Decl
impliedNetType var Nothing = impliedNet var Nothing =
error $ "implicit declaration of " ++ error $ "implicit declaration of " ++
show var ++ " but default_nettype is none" show var ++ " but default_nettype is none"
impliedNetType _ (Just netType) = Net (NetType netType) impliedNet var (Just netType) =
Net Local netType DefaultStrength UnknownType var [] Nil
parseDefaultNetType :: String -> DefaultNetType parseDefaultNetType :: String -> DefaultNetType
parseDefaultNetType "tri" = Just TTri parseDefaultNetType "tri" = Just TTri
......
...@@ -74,6 +74,7 @@ convertDescription parts (Part attrs extern Module lifetime name ports items) = ...@@ -74,6 +74,7 @@ convertDescription parts (Part attrs extern Module lifetime name ports items) =
traverseDeclM decl = do traverseDeclM decl = do
case decl of case decl of
Variable _ _ x _ _ -> insertElem x DeclVal Variable _ _ x _ _ -> insertElem x DeclVal
Net _ _ _ _ x _ _ -> insertElem x DeclVal
Param _ _ x _ -> insertElem x DeclVal Param _ _ x _ -> insertElem x DeclVal
ParamType _ x _ -> insertElem x DeclVal ParamType _ x _ -> insertElem x DeclVal
CommentDecl{} -> return () CommentDecl{} -> return ()
...@@ -324,10 +325,13 @@ convertDescription _ other = other ...@@ -324,10 +325,13 @@ convertDescription _ other = other
-- produce the implicit modport decls for an interface bundle -- produce the implicit modport decls for an interface bundle
impliedModport :: [ModuleItem] -> [ModportDecl] impliedModport :: [ModuleItem] -> [ModportDecl]
impliedModport = impliedModport =
execWriter . mapM (collectNestedModuleItemsM collectModportDecls) execWriter . mapM
(collectNestedModuleItemsM $ collectDeclsM collectModportDecls)
where where
collectModportDecls :: ModuleItem -> Writer [ModportDecl] () collectModportDecls :: Decl -> Writer [ModportDecl] ()
collectModportDecls (MIPackageItem (Decl (Variable _ _ x _ _))) = collectModportDecls (Variable _ _ x _ _) =
tell [(Inout, x, Ident x)]
collectModportDecls (Net _ _ _ _ x _ _) =
tell [(Inout, x, Ident x)] tell [(Inout, x, Ident x)]
collectModportDecls _ = return () collectModportDecls _ = return ()
...@@ -384,15 +388,17 @@ inlineInstance global ranges modportBindings items partName ...@@ -384,15 +388,17 @@ inlineInstance global ranges modportBindings items partName
rewriteItem :: ModuleItem -> ModuleItem rewriteItem :: ModuleItem -> ModuleItem
rewriteItem = rewriteItem =
traverseDecls $
removeModportInstance . removeModportInstance .
removeDeclDir . removeDeclDir .
traverseDecls overrideParam overrideParam
traverseDeclM :: Decl -> Scoper Expr Decl traverseDeclM :: Decl -> Scoper Expr Decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- traverseDeclExprsM substituteExprM decl decl' <- traverseDeclExprsM substituteExprM decl
case decl' of case decl' of
Variable _ _ x _ _ -> insertElem x Nil Variable _ _ x _ _ -> insertElem x Nil
Net _ _ _ _ x _ _ -> insertElem x Nil
Param _ _ x e -> insertElem x e Param _ _ x e -> insertElem x e
ParamType _ x _ -> insertElem x Nil ParamType _ x _ -> insertElem x Nil
CommentDecl{} -> return () CommentDecl{} -> return ()
...@@ -507,9 +513,8 @@ inlineInstance global ranges modportBindings items partName ...@@ -507,9 +513,8 @@ inlineInstance global ranges modportBindings items partName
++ "\" resolvable when it wasn't previously" ++ "\" resolvable when it wasn't previously"
_ -> id _ -> id
removeModportInstance :: ModuleItem -> ModuleItem removeModportInstance :: Decl -> Decl
removeModportInstance (MIPackageItem (Decl (Variable d t x a e))) = removeModportInstance (Variable d t x a e) =
MIPackageItem $ Decl $
if maybeModportBinding == Nothing then if maybeModportBinding == Nothing then
Variable d t x a e Variable d t x a e
else if makeBindingBaseExpr modportE == Nothing then else if makeBindingBaseExpr modportE == Nothing then
...@@ -525,16 +530,17 @@ inlineInstance global ranges modportBindings items partName ...@@ -525,16 +530,17 @@ inlineInstance global ranges modportBindings items partName
bindingBaseExpr = Ident $ bindingBaseName ++ x bindingBaseExpr = Ident $ bindingBaseName ++ x
modportDims = a ++ snd (typeRanges t) modportDims = a ++ snd (typeRanges t)
[modportDim] = modportDims [modportDim] = modportDims
removeModportInstance other = other removeModportInstance other = other
removeDeclDir :: ModuleItem -> ModuleItem removeDeclDir :: Decl -> Decl
removeDeclDir (MIPackageItem (Decl (Variable _ t x a e))) = removeDeclDir (Variable _ t x a e) =
MIPackageItem $ Decl $ Variable Local t' x a e Variable Local t' x a e
where t' = case t of where t' = case t of
Implicit Unspecified rs -> Implicit Unspecified rs ->
IntegerVector TLogic Unspecified rs IntegerVector TLogic Unspecified rs
_ -> t _ -> t
removeDeclDir decl @ Net{} =
traverseNetAsVar removeDeclDir decl
removeDeclDir other = other removeDeclDir other = other
-- capture the lower bound for each modport array binding -- capture the lower bound for each modport array binding
...@@ -599,6 +605,8 @@ inlineInstance global ranges modportBindings items partName ...@@ -599,6 +605,8 @@ inlineInstance global ranges modportBindings items partName
collectDeclDir (Variable dir _ ident _ _) = collectDeclDir (Variable dir _ ident _ _) =
when (dir /= Local) $ when (dir /= Local) $
tell $ Map.singleton ident dir tell $ Map.singleton ident dir
collectDeclDir net @ Net{} =
collectNetAsVarM collectDeclDir net
collectDeclDir _ = return () collectDeclDir _ = return ()
findDeclDir :: Identifier -> Direction findDeclDir :: Identifier -> Direction
findDeclDir ident = findDeclDir ident =
......
...@@ -63,6 +63,8 @@ convert = ...@@ -63,6 +63,8 @@ convert =
(_, InterfaceT{}) -> tell [(ident, Local)] (_, InterfaceT{}) -> tell [(ident, Local)]
(Local, _) -> return () (Local, _) -> return ()
_ -> tell [(ident, dir)] _ -> tell [(ident, dir)]
collectDeclDirsM (MIPackageItem (Decl net @ Net{})) =
collectNetAsVarM (collectDeclDirsM . MIPackageItem . Decl) net
collectDeclDirsM _ = return () collectDeclDirsM _ = return ()
convertDescription :: Ports -> Description -> Description convertDescription :: Ports -> Description -> Description
...@@ -112,13 +114,14 @@ traverseModuleItem ports scopes = ...@@ -112,13 +114,14 @@ traverseModuleItem ports scopes =
then Assign AssignOptionNone lhs expr then Assign AssignOptionNone lhs expr
else else
Generate $ map GenModuleItem Generate $ map GenModuleItem
[ MIPackageItem (Decl (Variable Local t x [] Nil)) [ MIPackageItem $ Decl decl
, Assign AssignOptionNone (LHSIdent x) expr , Assign AssignOptionNone (LHSIdent x) expr
, always_comb $ Asgn AsgnOpEq Nothing lhs (Ident x) , always_comb $ Asgn AsgnOpEq Nothing lhs (Ident x)
] ]
where where
t = Net (NetType TWire) Unspecified decl = Net Local TWire DefaultStrength t x [] Nil
[(DimsFn FnBits $ Right $ lhsToExpr lhs, RawNum 1)] t = Implicit Unspecified [r]
r = (DimsFn FnBits $ Right $ lhsToExpr lhs, RawNum 1)
x = "sv2v_tmp_" ++ shortHash (lhs, expr) x = "sv2v_tmp_" ++ shortHash (lhs, expr)
-- rewrite port bindings to use temporary nets where necessary -- rewrite port bindings to use temporary nets where necessary
fixModuleItem (Instance moduleName params instanceName rs bindings) = fixModuleItem (Instance moduleName params instanceName rs bindings) =
...@@ -143,10 +146,11 @@ traverseModuleItem ports scopes = ...@@ -143,10 +146,11 @@ traverseModuleItem ports scopes =
portDir = maybeModulePorts >>= lookup portName portDir = maybeModulePorts >>= lookup portName
tmp = "sv2v_tmp_" ++ instanceName ++ "_" ++ portName tmp = "sv2v_tmp_" ++ instanceName ++ "_" ++ portName
tmpExpr = Ident tmp tmpExpr = Ident tmp
t = Net (NetType TWire) Unspecified decl = Net Local TWire DefaultStrength t tmp [] Nil
[(DimsFn FnBits $ Right expr, RawNum 1)] t = Implicit Unspecified [r]
r = (DimsFn FnBits $ Right expr, RawNum 1)
items = items =
[ MIPackageItem $ Decl $ Variable Local t tmp [] Nil [ MIPackageItem $ Decl decl
, always_comb $ Asgn AsgnOpEq Nothing lhs tmpExpr] , always_comb $ Asgn AsgnOpEq Nothing lhs tmpExpr]
lhs = case exprToLHS expr of lhs = case exprToLHS expr of
Just l -> l Just l -> l
...@@ -160,26 +164,35 @@ traverseModuleItem ports scopes = ...@@ -160,26 +164,35 @@ traverseModuleItem ports scopes =
traverseDeclM :: Decl -> ST Decl traverseDeclM :: Decl -> ST Decl
traverseDeclM (decl @ (Variable _ t x _ _)) = traverseDeclM (decl @ (Variable _ t x _ _)) =
insertElem x t >> return decl insertElem x t >> return decl
traverseDeclM (decl @ (Net _ _ _ t x _ _)) =
insertElem x t >> return decl
traverseDeclM decl = return decl traverseDeclM decl = return decl
rewriteDeclM :: Decl -> ST Decl rewriteDeclM :: Decl -> ST Decl
rewriteDeclM (Variable d t x a e) = do rewriteDeclM (Variable d (t @ (IntegerVector TLogic sg rs)) x a e) = do
(d', t') <- case t of insertElem x t
IntegerVector TLogic sg rs -> do details <- lookupElemM x
insertElem x t let Just (accesses, _, _) = details
details <- lookupElemM x let location = map accessName accesses
let Just (accesses, _, _) = details usedAsReg <- lift $ gets $ Set.member location
let location = map accessName accesses blockLogic <- withinProcedureM
usedAsReg <- lift $ gets $ Set.member location if usedAsReg || blockLogic || e /= Nil
blockLogic <- withinProcedureM then do
if usedAsReg || blockLogic || e /= Nil let d' = if d == Inout then Output else d
then do let t' = IntegerVector TReg sg rs
let dir = if d == Inout then Output else d insertElem x t'
return (dir, IntegerVector TReg sg rs) return $ Variable d' t' x a e
else return (d, Net (NetType TWire) sg rs) else do
_ -> return (d, t) let t' = Implicit sg rs
insertElem x t' insertElem x t'
return $ Variable d' t' x a e return $ Net d TWire DefaultStrength t' x a e
rewriteDeclM (decl @ (Variable _ t x _ _)) =
insertElem x t >> return decl
rewriteDeclM (Net d n s (IntegerVector _ sg rs) x a e) =
insertElem x t >> return (Net d n s t x a e)
where t = Implicit sg rs
rewriteDeclM (decl @ (Net _ _ _ t x _ _)) =
insertElem x t >> return decl
rewriteDeclM (Param s (IntegerVector _ sg []) x e) = rewriteDeclM (Param s (IntegerVector _ sg []) x e) =
return $ Param s (Implicit sg [(zero, zero)]) x e return $ Param s (Implicit sg [(zero, zero)]) x e
where zero = RawNum 0 where zero = RawNum 0
......
...@@ -52,6 +52,8 @@ traverseDeclM :: Decl -> Scoper TypeInfo Decl ...@@ -52,6 +52,8 @@ traverseDeclM :: Decl -> Scoper TypeInfo Decl
traverseDeclM (Variable dir t ident a e) = do traverseDeclM (Variable dir t ident a e) = do
t' <- traverseTypeM t a ident t' <- traverseTypeM t a ident
traverseDeclExprsM traverseExprM $ Variable dir t' ident a e traverseDeclExprsM traverseExprM $ Variable dir t' ident a e
traverseDeclM net @ Net{} =
traverseNetAsVarM traverseDeclM net
traverseDeclM (Param s t ident e) = do traverseDeclM (Param s t ident e) = do
t' <- traverseTypeM t [] ident t' <- traverseTypeM t [] ident
traverseDeclExprsM traverseExprM $ Param s t' ident e traverseDeclExprsM traverseExprM $ Param s t' ident e
......
...@@ -314,6 +314,7 @@ processItems topName packageName moduleItems = do ...@@ -314,6 +314,7 @@ processItems topName packageName moduleItems = do
>>= traverseDeclExprsM traverseExprM >>= 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
Param p t x e -> declHelp x $ \x' -> Param p t x' e Param p t x e -> declHelp x $ \x' -> Param p t x' e
ParamType p x t -> declHelp x $ \x' -> ParamType p x' t ParamType p x t -> declHelp x $ \x' -> ParamType p x' t
CommentDecl c -> return $ CommentDecl c CommentDecl c -> return $ CommentDecl c
...@@ -660,6 +661,7 @@ writeDeclIdents :: Decl -> IdentWriter Decl ...@@ -660,6 +661,7 @@ writeDeclIdents :: Decl -> IdentWriter Decl
writeDeclIdents decl = do writeDeclIdents decl = do
case decl of case decl of
Variable _ _ x _ _ -> insertElem x () Variable _ _ x _ _ -> insertElem x ()
Net _ _ _ _ x _ _ -> insertElem x ()
Param _ _ x _ -> insertElem x () Param _ _ x _ -> insertElem x ()
ParamType _ x _ -> insertElem x () ParamType _ x _ -> insertElem x ()
CommentDecl{} -> return () CommentDecl{} -> return ()
...@@ -744,6 +746,7 @@ piNames (Decl (ParamType _ ident (Enum _ enumItems _))) = ...@@ -744,6 +746,7 @@ piNames (Decl (ParamType _ ident (Enum _ enumItems _))) =
piNames (Function _ _ ident _ _) = [ident] piNames (Function _ _ ident _ _) = [ident]
piNames (Task _ ident _ _) = [ident] piNames (Task _ ident _ _) = [ident]
piNames (Decl (Variable _ _ ident _ _)) = [ident] piNames (Decl (Variable _ _ ident _ _)) = [ident]
piNames (Decl (Net _ _ _ _ ident _ _)) = [ident]
piNames (Decl (Param _ _ ident _)) = [ident] piNames (Decl (Param _ _ ident _)) = [ident]
piNames (Decl (ParamType _ ident _)) = [ident] piNames (Decl (ParamType _ ident _)) = [ident]
piNames (Decl (CommentDecl _)) = [] piNames (Decl (CommentDecl _)) = []
......
...@@ -224,7 +224,6 @@ isSimpleType typ = ...@@ -224,7 +224,6 @@ isSimpleType typ =
IntegerVector{} -> True IntegerVector{} -> True
IntegerAtom {} -> True IntegerAtom {} -> True
NonInteger {} -> True NonInteger {} -> True
Net {} -> True
Implicit {} -> True Implicit {} -> True
Struct _ fields _ -> all (isSimpleType . fst) fields Struct _ fields _ -> all (isSimpleType . fst) fields
Union _ fields _ -> all (isSimpleType . fst) fields Union _ fields _ -> all (isSimpleType . fst) fields
......
...@@ -451,6 +451,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper = ...@@ -451,6 +451,7 @@ scopeModuleItemT declMapper moduleItemMapper genItemMapper stmtMapper =
if d == Local if d == Local
then Nothing then Nothing
else Just $ \i -> Variable d t (show i) a e else Just $ \i -> Variable d t (show i) a e
argIdxDecl Net{} = Nothing
argIdxDecl Param{} = Nothing argIdxDecl Param{} = Nothing
argIdxDecl ParamType{} = Nothing argIdxDecl ParamType{} = Nothing
argIdxDecl CommentDecl{} = Nothing argIdxDecl CommentDecl{} = Nothing
......
...@@ -36,6 +36,7 @@ traverseDeclM (Variable d t x [] (expr @ (Stream StreamL chunk exprs))) = do ...@@ -36,6 +36,7 @@ traverseDeclM (Variable d t x [] (expr @ (Stream StreamL chunk exprs))) = do
fnName = streamerFuncName x fnName = streamerFuncName x
func = streamerFunc fnName chunk (TypeOf $ Concat exprs) t func = streamerFunc fnName chunk (TypeOf $ Concat exprs) t
expr' = Call (Ident fnName) (Args [Concat exprs] []) expr' = Call (Ident fnName) (Args [Concat exprs] [])
traverseDeclM decl @ Net{} = traverseNetAsVarM traverseDeclM decl
traverseDeclM decl = return decl traverseDeclM decl = return decl
traverseModuleItemM :: ModuleItem -> Scoper () ModuleItem traverseModuleItemM :: ModuleItem -> Scoper () ModuleItem
......
...@@ -105,6 +105,8 @@ convertType t1 = ...@@ -105,6 +105,8 @@ convertType t1 =
-- write down the types of declarations -- write down the types of declarations
traverseDeclM :: Decl -> Scoper Type Decl traverseDeclM :: Decl -> Scoper Type Decl
traverseDeclM decl @ Net{} =
traverseNetAsVarM traverseDeclM decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- case decl of decl' <- case decl of
Variable d t x a e -> do Variable d t x a e -> do
...@@ -119,8 +121,7 @@ traverseDeclM decl = do ...@@ -119,8 +121,7 @@ traverseDeclM decl = do
let e' = convertExpr t e let e' = convertExpr t e
let t' = convertType t let t' = convertType t
return $ Param s t' x e' return $ Param s t' x e'
ParamType{} -> return decl _ -> return decl
CommentDecl{} -> return decl
traverseDeclExprsM traverseExprM decl' traverseDeclExprsM traverseExprM decl'
where where
isRangeable :: Type -> Bool isRangeable :: Type -> Bool
......
...@@ -66,6 +66,7 @@ declNames = filter (not . null) . map declName ...@@ -66,6 +66,7 @@ declNames = filter (not . null) . map declName
declName :: Decl -> Identifier declName :: Decl -> Identifier
declName (Variable _ _ x _ _) = x declName (Variable _ _ x _ _) = x
declName (Net _ _ _ _ x _ _) = x
declName (Param _ _ x _) = x declName (Param _ _ x _) = x
declName (ParamType _ x _) = x declName (ParamType _ x _) = x
declName CommentDecl{} = "" declName CommentDecl{} = ""
......
...@@ -101,6 +101,9 @@ module Convert.Traverse ...@@ -101,6 +101,9 @@ module Convert.Traverse
, traverseSinglyNestedStmtsM , traverseSinglyNestedStmtsM
, traverseSinglyNestedStmts , traverseSinglyNestedStmts
, collectSinglyNestedStmtsM , collectSinglyNestedStmtsM
, traverseNetAsVarM
, traverseNetAsVar
, collectNetAsVarM
) where ) where
import Data.Functor.Identity (Identity, runIdentity) import Data.Functor.Identity (Identity, runIdentity)
...@@ -576,7 +579,7 @@ traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper = ...@@ -576,7 +579,7 @@ traverseNodesM exprMapper declMapper typeMapper lhsMapper stmtMapper =
moduleItemMapper (Assign opt lhs expr) = do moduleItemMapper (Assign opt lhs expr) = do
opt' <- case opt of opt' <- case opt of
AssignOptionNone -> return $ AssignOptionNone AssignOptionNone -> return $ AssignOptionNone
AssignOptionDrive ds -> return $ AssignOptionDrive ds AssignOptionDrive s0 s1 -> return $ AssignOptionDrive s0 s1
AssignOptionDelay delay -> AssignOptionDelay delay ->
exprMapper delay >>= return . AssignOptionDelay exprMapper delay >>= return . AssignOptionDelay
lhs' <- lhsMapper lhs lhs' <- lhsMapper lhs
...@@ -814,7 +817,6 @@ traverseSinglyNestedTypesM mapper = tm ...@@ -814,7 +817,6 @@ traverseSinglyNestedTypesM mapper = tm
vals' <- mapM typeOrExprMapper $ map snd pm vals' <- mapM typeOrExprMapper $ map snd pm
let pm' = zip (map fst pm) vals' let pm' = zip (map fst pm) vals'
return $ CSAlias ps pm' xx rs return $ CSAlias ps pm' xx rs
tm (Net kw sg rs) = return $ Net kw sg rs
tm (Implicit sg rs) = return $ Implicit sg rs tm (Implicit sg rs) = return $ Implicit sg rs
tm (IntegerVector kw sg rs) = return $ IntegerVector kw sg rs tm (IntegerVector kw sg rs) = return $ IntegerVector kw sg rs
tm (IntegerAtom kw sg ) = return $ IntegerAtom kw sg tm (IntegerAtom kw sg ) = return $ IntegerAtom kw sg
...@@ -946,6 +948,11 @@ traverseDeclExprsM exprMapper = ...@@ -946,6 +948,11 @@ traverseDeclExprsM exprMapper =
a' <- mapM (mapBothM exprMapper) a a' <- mapM (mapBothM exprMapper) a
e' <- exprMapper e e' <- exprMapper e
return $ Variable d t' x a' e' return $ Variable d t' x a' e'
declMapper (Net d n s t x a e) = do
t' <- typeMapper t
a' <- mapM (mapBothM exprMapper) a
e' <- exprMapper e
return $ Net d n s t' x a' e'
declMapper (CommentDecl c) = declMapper (CommentDecl c) =
return $ CommentDecl c return $ CommentDecl c
...@@ -961,6 +968,8 @@ traverseDeclTypesM mapper (ParamType s x t) = ...@@ -961,6 +968,8 @@ traverseDeclTypesM mapper (ParamType s x t) =
mapper t >>= \t' -> return $ ParamType s x t' 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 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 _ (CommentDecl c) = return $ CommentDecl c
traverseDeclTypes :: Mapper Type -> Mapper Decl traverseDeclTypes :: Mapper Type -> Mapper Decl
...@@ -1128,3 +1137,17 @@ traverseFiles ...@@ -1128,3 +1137,17 @@ traverseFiles
traverseFiles fileCollectorM fileMapper files = traverseFiles fileCollectorM fileMapper files =
runIdentity (traverseFilesM fileCollectorM fileMapperM files) runIdentity (traverseFilesM fileCollectorM fileMapperM files)
where fileMapperM = (\w -> return . fileMapper w) where fileMapperM = (\w -> return . fileMapper w)
traverseNetAsVarM :: Monad m => MapperM m Decl -> MapperM m Decl
traverseNetAsVarM func net = do
let Net d n s t x a e = net
let var = Variable d t x a e
var' <- func var
let Variable d' t' x' a' e' = var'
let net' = Net d' n s t' x' a' e'
return net'
traverseNetAsVar :: Mapper Decl -> Mapper Decl
traverseNetAsVar = unmonad traverseNetAsVarM
collectNetAsVarM :: Monad m => CollectorM m Decl -> CollectorM m Decl
collectNetAsVarM = collectify traverseNetAsVarM
...@@ -41,6 +41,8 @@ type ST = Scoper Type ...@@ -41,6 +41,8 @@ type ST = Scoper Type
-- insert the given declaration into the scope, and convert an TypeOfs within -- insert the given declaration into the scope, and convert an TypeOfs within
traverseDeclM :: Decl -> ST Decl traverseDeclM :: Decl -> ST Decl
traverseDeclM decl @ Net{} =
traverseNetAsVarM traverseDeclM decl
traverseDeclM decl = do traverseDeclM decl = do
decl' <- traverseDeclExprsM traverseExprM decl decl' <- traverseDeclExprsM traverseExprM decl
>>= traverseDeclTypesM traverseTypeM >>= traverseDeclTypesM traverseTypeM
...@@ -65,8 +67,7 @@ traverseDeclM decl = do ...@@ -65,8 +67,7 @@ traverseDeclM decl = do
where t' = IntegerVector TLogic sg rs where t' = IntegerVector TLogic sg rs
Param _ t ident _ -> Param _ t ident _ ->
insertType ident t >> return decl' insertType ident t >> return decl'
ParamType{} -> return decl' _ -> return decl'
CommentDecl{} -> return decl'
-- rewrite and store a non-genvar data declaration's type information -- rewrite and store a non-genvar data declaration's type information
insertType :: Identifier -> Type -> ST () insertType :: Identifier -> Type -> ST ()
...@@ -155,13 +156,8 @@ lookupTypeOf expr = do ...@@ -155,13 +156,8 @@ lookupTypeOf expr = do
then IntegerAtom TInteger Unspecified then IntegerAtom TInteger Unspecified
else TypeOf expr else TypeOf expr
_ -> return $ TypeOf expr _ -> return $ TypeOf expr
Just (_, replacements, typ) -> do Just (_, replacements, typ) ->
let typ' = toVarType typ return $ replaceInType replacements typ
return $ replaceInType replacements typ'
where
toVarType :: Type -> Type
toVarType (Net _ sg rs) = IntegerVector TLogic sg rs
toVarType other = other
-- determines the type of an expression based on the available scope information -- determines the type of an expression based on the available scope information
-- according the semantics defined in IEEE 1800-2017, especially Section 11.6 -- according the semantics defined in IEEE 1800-2017, especially Section 11.6
...@@ -248,7 +244,6 @@ typeSignednessOverride fallback sg t = ...@@ -248,7 +244,6 @@ typeSignednessOverride fallback sg t =
case t of case t of
IntegerVector base _ rs -> IntegerVector base sg rs IntegerVector base _ rs -> IntegerVector base sg rs
IntegerAtom base _ -> IntegerAtom base sg IntegerAtom base _ -> IntegerAtom base sg
Net base _ rs -> Net base sg rs
_ -> fallback _ -> fallback
-- type of a unary operator expression -- type of a unary operator expression
...@@ -319,7 +314,6 @@ binopSignedness Signed Signed = Signed ...@@ -319,7 +314,6 @@ binopSignedness Signed Signed = Signed
-- returns the signedness of the given type, if possible -- returns the signedness of the given type, if possible
typeSignedness :: Type -> Signing typeSignedness :: Type -> Signing
typeSignedness (Net _ sg _) = signednessFallback Unsigned sg
typeSignedness (IntegerVector _ sg _) = signednessFallback Unsigned sg typeSignedness (IntegerVector _ sg _) = signednessFallback Unsigned sg
typeSignedness (IntegerAtom t sg ) = signednessFallback fallback sg typeSignedness (IntegerAtom t sg ) = signednessFallback fallback sg
where fallback = if t == TTime then Unsigned else Signed where fallback = if t == TTime then Unsigned else Signed
...@@ -389,7 +383,6 @@ typeCastUnneeded t1 t2 = ...@@ -389,7 +383,6 @@ typeCastUnneeded t1 t2 =
sz1 = typeSize t1 sz1 = typeSize t1
sz2 = typeSize t2 sz2 = typeSize t2
typeSize :: Type -> Maybe Expr typeSize :: Type -> Maybe Expr
typeSize (Net _ _ rs) = Just $ dimensionsSize rs
typeSize (IntegerVector _ _ rs) = Just $ dimensionsSize rs typeSize (IntegerVector _ _ rs) = Just $ dimensionsSize rs
typeSize (t @ IntegerAtom{}) = typeSize (t @ IntegerAtom{}) =
typeSize $ tf [(RawNum 1, RawNum 1)] typeSize $ tf [(RawNum 1, RawNum 1)]
......
...@@ -76,6 +76,7 @@ traverseDeclM decl = do ...@@ -76,6 +76,7 @@ traverseDeclM decl = do
>>= traverseDeclTypesM traverseTypeM >>= traverseDeclTypesM traverseTypeM
case decl' of case decl' of
Variable{} -> return decl' Variable{} -> return decl'
Net{} -> return decl'
Param _ _ x _ -> Param _ _ x _ ->
insertElem x UnknownType >> return decl' insertElem x UnknownType >> return decl'
ParamType Localparam x t -> do ParamType Localparam x t -> do
......
...@@ -105,6 +105,9 @@ determinePortSize portName instanceParams moduleItems = ...@@ -105,6 +105,9 @@ determinePortSize portName instanceParams moduleItems =
then substituteExpr (reverse mapping) size then substituteExpr (reverse mapping) size
else step mapping rest else step mapping rest
where size = BinOp Mul (dimensionsSize a) (DimsFn FnBits $ Left t) where size = BinOp Mul (dimensionsSize a) (DimsFn FnBits $ Left t)
step mapping (MIPackageItem (Decl (Net d _ _ t x a e)) : rest) =
step mapping $ item : rest
where item = MIPackageItem $ Decl $ Variable d t x a e
step mapping (_ : rest) = step mapping rest step mapping (_ : rest) = step mapping rest
step _ [] = error $ "could not find size of port " ++ portName step _ [] = error $ "could not find size of port " ++ portName
......
...@@ -48,6 +48,7 @@ traverseDeclM :: Decl -> S Decl ...@@ -48,6 +48,7 @@ traverseDeclM :: Decl -> S Decl
traverseDeclM decl = traverseDeclM decl =
case decl of case decl of
Variable _ _ x _ _ -> declaration x decl Variable _ _ x _ _ -> declaration x decl
Net _ _ _ _ x _ _ -> declaration x decl
Param _ _ x _ -> declaration x decl Param _ _ x _ -> declaration x decl
ParamType _ x _ -> declaration x decl ParamType _ x _ -> declaration x decl
CommentDecl{} -> return decl CommentDecl{} -> return decl
......
...@@ -44,6 +44,8 @@ traverseDeclM ports (decl @ (Variable _ _ x _ e)) = do ...@@ -44,6 +44,8 @@ traverseDeclM ports (decl @ (Variable _ _ x _ e)) = do
then flatUsageM x then flatUsageM x
else return () else return ()
return decl return decl
traverseDeclM ports decl @ Net{} =
traverseNetAsVarM (traverseDeclM ports) decl
traverseDeclM _ other = return other traverseDeclM _ other = return other
-- pack decls marked for packing -- pack decls marked for packing
...@@ -62,6 +64,7 @@ rewriteDeclM (decl @ (Variable d t x a e)) = do ...@@ -62,6 +64,7 @@ rewriteDeclM (decl @ (Variable d t x a e)) = do
let t' = tf $ shifted ++ rs let t' = tf $ shifted ++ rs
return $ Variable d t' x rest e return $ Variable d t' x rest e
Nothing -> return decl Nothing -> return decl
rewriteDeclM decl @ Net{} = traverseNetAsVarM rewriteDeclM decl
rewriteDeclM other = return other rewriteDeclM other = return other
traverseModuleItemM :: ModuleItem -> ST ModuleItem traverseModuleItemM :: ModuleItem -> ST ModuleItem
......
...@@ -23,7 +23,6 @@ convert = ...@@ -23,7 +23,6 @@ convert =
convertType :: Type -> Type convertType :: Type -> Type
convertType (Implicit Unsigned rs) = Implicit Unspecified rs convertType (Implicit Unsigned rs) = Implicit Unspecified rs
convertType (IntegerVector t Unsigned rs) = IntegerVector t Unspecified rs convertType (IntegerVector t Unsigned rs) = IntegerVector t Unspecified rs
convertType (Net t Unsigned rs) = Net t Unspecified rs
convertType (IntegerAtom TInteger Unsigned) = convertType (IntegerAtom TInteger Unsigned) =
IntegerVector TReg Unspecified [(RawNum 31, RawNum 0)] IntegerVector TReg Unspecified [(RawNum 31, RawNum 0)]
convertType other = other convertType other = other
...@@ -16,14 +16,15 @@ module Language.SystemVerilog.AST.Decl ...@@ -16,14 +16,15 @@ module Language.SystemVerilog.AST.Decl
import Data.List (intercalate) import Data.List (intercalate)
import Text.Printf (printf) import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp (showPad, unlines') import Language.SystemVerilog.AST.ShowHelp (showPad, showPadBefore, unlines')
import Language.SystemVerilog.AST.Type (Type(TypedefRef, UnpackedType), Identifier, pattern UnknownType) import Language.SystemVerilog.AST.Type (Type(TypedefRef, UnpackedType), Identifier, pattern UnknownType, NetType, Strength)
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 Type | ParamType ParamScope Identifier Type
| Variable Direction Type Identifier [Range] Expr | Variable Direction Type Identifier [Range] Expr
| Net Direction NetType Strength Type Identifier [Range] Expr
| CommentDecl String | CommentDecl String
deriving Eq deriving Eq
...@@ -37,6 +38,7 @@ instance Show Decl where ...@@ -37,6 +38,7 @@ instance Show Decl where
show (ParamType s x t) = 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 = if t == UnknownType then "" else " = " ++ show t 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 (Net d n s t x a e) = printf "%s%s%s %s%s%s%s;" (showPad d) (show n) (showPadBefore s) (showPad t) x (showRanges a) (showAssignment e)
show (CommentDecl c) = show (CommentDecl c) =
if elem '\n' c if elem '\n' c
then "// " ++ show c then "// " ++ show c
......
...@@ -28,7 +28,7 @@ import Language.SystemVerilog.AST.Expr (Expr(Nil), pattern Ident, Range, showRan ...@@ -28,7 +28,7 @@ import Language.SystemVerilog.AST.Expr (Expr(Nil), pattern Ident, Range, showRan
import Language.SystemVerilog.AST.GenItem (GenItem) import Language.SystemVerilog.AST.GenItem (GenItem)
import Language.SystemVerilog.AST.LHS (LHS) import Language.SystemVerilog.AST.LHS (LHS)
import Language.SystemVerilog.AST.Stmt (Stmt, AssertionItem, Timing(Delay)) import Language.SystemVerilog.AST.Stmt (Stmt, AssertionItem, Timing(Delay))
import Language.SystemVerilog.AST.Type (Identifier, DriveStrength) import Language.SystemVerilog.AST.Type (Identifier, Strength0, Strength1)
data ModuleItem data ModuleItem
= MIAttr Attr ModuleItem = MIAttr Attr ModuleItem
...@@ -140,11 +140,11 @@ instance Show NOutputGateKW where ...@@ -140,11 +140,11 @@ instance Show NOutputGateKW where
data AssignOption data AssignOption
= AssignOptionNone = AssignOptionNone
| AssignOptionDelay Expr | AssignOptionDelay Expr
| AssignOptionDrive DriveStrength | AssignOptionDrive Strength0 Strength1
deriving Eq deriving Eq
instance Show AssignOption where instance Show AssignOption where
show AssignOptionNone = "" show AssignOptionNone = ""
show (AssignOptionDelay de) = printf "#(%s)" (show de) show (AssignOptionDelay de) = printf "#(%s)" (show de)
show (AssignOptionDrive ds) = show ds show (AssignOptionDrive s0 s1) = printf "(%s, %s)" (show s0) (show s1)
...@@ -17,8 +17,7 @@ module Language.SystemVerilog.AST.Type ...@@ -17,8 +17,7 @@ module Language.SystemVerilog.AST.Type
, IntegerVectorType (..) , IntegerVectorType (..)
, IntegerAtomType (..) , IntegerAtomType (..)
, NonIntegerType (..) , NonIntegerType (..)
, NetTypeAndStrength (..) , Strength (..)
, DriveStrength (..)
, Strength0 (..) , Strength0 (..)
, Strength1 (..) , Strength1 (..)
, ChargeStrength (..) , ChargeStrength (..)
...@@ -41,7 +40,6 @@ data Type ...@@ -41,7 +40,6 @@ data Type
= IntegerVector IntegerVectorType Signing [Range] = IntegerVector IntegerVectorType Signing [Range]
| IntegerAtom IntegerAtomType Signing | IntegerAtom IntegerAtomType Signing
| NonInteger NonIntegerType | NonInteger NonIntegerType
| Net NetTypeAndStrength Signing [Range]
| Implicit Signing [Range] | Implicit Signing [Range]
| Alias Identifier [Range] | Alias Identifier [Range]
| PSAlias Identifier Identifier [Range] | PSAlias Identifier Identifier [Range]
...@@ -59,7 +57,6 @@ instance Show Type where ...@@ -59,7 +57,6 @@ instance Show Type where
show (Alias xx rs) = printf "%s%s" xx (showRanges rs) show (Alias xx rs) = printf "%s%s" xx (showRanges rs)
show (PSAlias ps xx rs) = printf "%s::%s%s" ps xx (showRanges rs) show (PSAlias ps xx rs) = printf "%s::%s%s" ps xx (showRanges rs)
show (CSAlias ps pm xx rs) = printf "%s#%s::%s%s" ps (showParams pm) xx (showRanges rs) show (CSAlias ps pm xx rs) = printf "%s#%s::%s%s" ps (showParams pm) xx (showRanges rs)
show (Net kw sg rs) = printf "%s%s%s" (show kw) (showPadBefore sg) (showRanges rs)
show (Implicit sg rs) = printf "%s%s" (showPad sg) (dropWhile (== ' ') $ showRanges rs) show (Implicit sg rs) = printf "%s%s" (showPad sg) (dropWhile (== ' ') $ showRanges rs)
show (IntegerVector kw sg rs) = printf "%s%s%s" (show kw) (showPadBefore sg) (showRanges rs) show (IntegerVector kw sg rs) = printf "%s%s%s" (show kw) (showPadBefore sg) (showRanges rs)
show (IntegerAtom kw sg ) = printf "%s%s" (show kw) (showPadBefore sg) show (IntegerAtom kw sg ) = printf "%s%s" (show kw) (showPadBefore sg)
...@@ -101,7 +98,6 @@ instance Eq (Signing -> [Range] -> Type) where ...@@ -101,7 +98,6 @@ instance Eq (Signing -> [Range] -> Type) where
typeRanges :: Type -> ([Range] -> Type, [Range]) typeRanges :: Type -> ([Range] -> Type, [Range])
typeRanges typ = typeRanges typ =
case typ of case typ of
Net kw sg rs -> (Net kw sg, rs)
Implicit sg rs -> (Implicit sg, rs) Implicit sg rs -> (Implicit sg, rs)
IntegerVector kw sg rs -> (IntegerVector kw sg, rs) IntegerVector kw sg rs -> (IntegerVector kw sg, rs)
Enum t v rs -> (Enum t v, rs) Enum t v rs -> (Enum t v, rs)
...@@ -237,22 +233,15 @@ instance Show Packing where ...@@ -237,22 +233,15 @@ instance Show Packing where
show (Unpacked) = "" show (Unpacked) = ""
show (Packed s) = "packed" ++ (showPadBefore s) show (Packed s) = "packed" ++ (showPadBefore s)
data NetTypeAndStrength data Strength
= NetType NetType = DefaultStrength
| NetTypeDrive NetType DriveStrength | DriveStrength Strength0 Strength1
| NetTypeCharge NetType ChargeStrength | ChargeStrength ChargeStrength
deriving (Eq, Ord) deriving (Eq, Ord)
instance Show NetTypeAndStrength where instance Show Strength where
show (NetType nt ) = show nt show DefaultStrength = ""
show (NetTypeDrive nt ds) = printf "%s %s" (show nt) (show ds) show (ChargeStrength cs) = printf "(%s)" (show cs)
show (NetTypeCharge nt cs) = printf "%s %s" (show nt) (show cs)
data DriveStrength
= DriveStrength Strength0 Strength1
deriving (Eq, Ord)
instance Show DriveStrength where
show (DriveStrength s0 s1) = printf "(%s, %s)" (show s0) (show s1) show (DriveStrength s0 s1) = printf "(%s, %s)" (show s0) (show s1)
data Strength0 data Strength0
...@@ -292,6 +281,6 @@ data ChargeStrength ...@@ -292,6 +281,6 @@ data ChargeStrength
deriving (Eq, Ord) deriving (Eq, Ord)
instance Show ChargeStrength where instance Show ChargeStrength where
show Small = "(small)" show Small = "small"
show Medium = "(medium)" show Medium = "medium"
show Large = "(large)" show Large = "large"
...@@ -399,6 +399,8 @@ time { Token Lit_time _ _ } ...@@ -399,6 +399,8 @@ time { Token Lit_time _ _ }
-- operator precedences, from *lowest* to *highest* -- operator precedences, from *lowest* to *highest*
%nonassoc DefaultStrength
%nonassoc DriveStrength ChargeStrength
%nonassoc Asgn %nonassoc Asgn
%nonassoc NoElse %nonassoc NoElse
%nonassoc "else" %nonassoc "else"
...@@ -456,16 +458,11 @@ TypeAlias :: { Type } ...@@ -456,16 +458,11 @@ TypeAlias :: { Type }
: Identifier Dimensions { Alias $1 $2 } : Identifier Dimensions { Alias $1 $2 }
| Identifier "::" Identifier Dimensions { PSAlias $1 $3 $4 } | Identifier "::" Identifier Dimensions { PSAlias $1 $3 $4 }
| Identifier ParamBindings "::" Identifier Dimensions { CSAlias $1 $2 $4 $5 } | Identifier ParamBindings "::" Identifier Dimensions { CSAlias $1 $2 $4 $5 }
PartialTypeAlias :: { Signing -> [Range] -> Type }
: Identifier { \Unspecified -> Alias $1 }
| Identifier "::" Identifier { \Unspecified -> PSAlias $1 $3 }
| Identifier ParamBindings "::" Identifier { \Unspecified -> CSAlias $1 $2 $4 }
TypeNonIdent :: { Type } TypeNonIdent :: { Type }
: PartialType OptSigning Dimensions { $1 $2 $3 } : PartialType OptSigning Dimensions { $1 $2 $3 }
| "type" "(" Expr ")" { TypeOf $3 } | "type" "(" Expr ")" { TypeOf $3 }
PartialType :: { Signing -> [Range] -> Type } PartialType :: { Signing -> [Range] -> Type }
: NetTypeAndStrength { Net $1 } : IntegerVectorType { IntegerVector $1 }
| IntegerVectorType { IntegerVector $1 }
| IntegerAtomType { \sg -> \[] -> IntegerAtom $1 sg } | IntegerAtomType { \sg -> \[] -> IntegerAtom $1 sg }
| NonIntegerType { \Unspecified -> \[] -> NonInteger $1 } | NonIntegerType { \Unspecified -> \[] -> NonInteger $1 }
| "enum" EnumBaseType "{" EnumItems "}" { \Unspecified -> Enum $2 $4 } | "enum" EnumBaseType "{" EnumItems "}" { \Unspecified -> Enum $2 $4 }
...@@ -480,11 +477,6 @@ EnumBaseType :: { Type } ...@@ -480,11 +477,6 @@ EnumBaseType :: { Type }
: Type { $1 } : Type { $1 }
| Dimensions { Implicit Unspecified $1 } | Dimensions { Implicit Unspecified $1 }
NetTypeAndStrength :: { NetTypeAndStrength }
: NetType %prec "+" { NetType $1 }
| NetType DriveStrength %prec "*" { NetTypeDrive $1 $2 }
| NetType ChargeStrength %prec "*" { NetTypeCharge $1 $2 }
Signing :: { Signing } Signing :: { Signing }
: "signed" { Signed } : "signed" { Signed }
| "unsigned" { Unsigned } | "unsigned" { Unsigned }
...@@ -623,6 +615,11 @@ Identifiers :: { [Identifier] } ...@@ -623,6 +615,11 @@ Identifiers :: { [Identifier] }
: Identifier { [$1] } : Identifier { [$1] }
| Identifiers "," Identifier { $1 ++ [$3] } | Identifiers "," Identifier { $1 ++ [$3] }
Strength :: { Strength }
: {- empty -} %prec DefaultStrength { DefaultStrength }
| DriveStrength %prec DriveStrength { uncurry DriveStrength $1 }
| ChargeStrength %prec ChargeStrength { ChargeStrength $1 }
-- uses delimiter propagation hack to avoid conflicts -- uses delimiter propagation hack to avoid conflicts
DeclTokens(delim) :: { [DeclToken] } DeclTokens(delim) :: { [DeclToken] }
: DeclTokensBase(DeclTokens(delim), delim) { $1 } : DeclTokensBase(DeclTokens(delim), delim) { $1 }
...@@ -635,21 +632,22 @@ DeclTokensBase(repeat, delim) :: { [DeclToken] } ...@@ -635,21 +632,22 @@ DeclTokensBase(repeat, delim) :: { [DeclToken] }
DeclToken :: { DeclToken } DeclToken :: { DeclToken }
: "," {% posInject \p -> DTComma p } : "," {% posInject \p -> DTComma p }
| "[" "]" {% posInject \p -> DTAutoDim p } | "[" "]" {% posInject \p -> DTAutoDim p }
| "const" {% posInject \p -> DTConst p }
| "var" {% posInject \p -> DTVar p }
| PartSelect {% posInject \p -> DTRange p $1 } | PartSelect {% posInject \p -> DTRange p $1 }
| Identifier {% posInject \p -> DTIdent p $1 } | Identifier {% posInject \p -> DTIdent p $1 }
| Direction {% posInject \p -> DTDir p $1 } | Direction {% posInject \p -> DTDir p $1 }
| "[" Expr "]" {% posInject \p -> DTBit p $2 } | "[" Expr "]" {% posInject \p -> DTBit p $2 }
| LHSConcat {% posInject \p -> DTConcat p $1 } | LHSConcat {% posInject \p -> DTConcat p $1 }
| PartialType {% posInject \p -> DTType p $1 } | PartialType {% posInject \p -> DTType p $1 }
| NetType Strength {% posInject \p -> DTNet p $1 $2 }
| "." Identifier {% posInject \p -> DTDot p $2 } | "." Identifier {% posInject \p -> DTDot p $2 }
| PortBindings {% posInject \p -> DTInstance p $1 } | PortBindings {% posInject \p -> DTInstance p $1 }
| Signing {% posInject \p -> DTSigning p $1 } | Signing {% posInject \p -> DTSigning p $1 }
| "automatic" {% posInject \p -> DTLifetime p Automatic } | "automatic" {% posInject \p -> DTLifetime p Automatic }
| "const" PartialType {% posInject \p -> DTType p $2 }
| "const" PartialTypeAlias {% posInject \p -> DTType p $2 }
| "{" StreamOp StreamSize Concat "}" {% posInject \p -> DTStream p $2 $3 (map toLHS $4) } | "{" StreamOp StreamSize Concat "}" {% posInject \p -> DTStream p $2 $3 (map toLHS $4) }
| "{" StreamOp Concat "}" {% posInject \p -> DTStream p $2 (RawNum 1) (map toLHS $3) } | "{" StreamOp Concat "}" {% posInject \p -> DTStream p $2 (RawNum 1) (map toLHS $3) }
| opt("var") "type" "(" Expr ")" {% posInject \p -> DTType p (\Unspecified -> \[] -> TypeOf $4) } | "type" "(" Expr ")" {% posInject \p -> DTType p (\Unspecified -> \[] -> TypeOf $3) }
| IncOrDecOperator {% posInject \p -> DTAsgn p (AsgnOp $1) Nothing (RawNum 1) } | IncOrDecOperator {% posInject \p -> DTAsgn p (AsgnOp $1) Nothing (RawNum 1) }
| "<=" opt(DelayOrEvent) Expr %prec Asgn {% posInject \p -> DTAsgn p AsgnOpNonBlocking $2 $3 } | "<=" opt(DelayOrEvent) Expr %prec Asgn {% posInject \p -> DTAsgn p AsgnOpNonBlocking $2 $3 }
| Identifier "::" Identifier {% posInject \p -> DTPSIdent p $1 $3 } | Identifier "::" Identifier {% posInject \p -> DTPSIdent p $1 $3 }
...@@ -711,7 +709,7 @@ NonGenerateModuleItem :: { [ModuleItem] } ...@@ -711,7 +709,7 @@ NonGenerateModuleItem :: { [ModuleItem] }
AssignOption :: { AssignOption } AssignOption :: { AssignOption }
: {- empty -} { AssignOptionNone } : {- empty -} { AssignOptionNone }
| DelayControl { AssignOptionDelay $1 } | DelayControl { AssignOptionDelay $1 }
| DriveStrength { AssignOptionDrive $1 } | DriveStrength { uncurry AssignOptionDrive $1 }
-- for ModuleItem, for now -- for ModuleItem, for now
AssertionItem :: { AssertionItem } AssertionItem :: { AssertionItem }
...@@ -816,13 +814,13 @@ NOutputGateKW :: { NOutputGateKW } ...@@ -816,13 +814,13 @@ NOutputGateKW :: { NOutputGateKW }
: "buf" { GateBuf } : "buf" { GateBuf }
| "not" { GateNot } | "not" { GateNot }
DriveStrength :: { DriveStrength } DriveStrength :: { (Strength0, Strength1) }
: "(" Strength0 "," Strength1 ")" { DriveStrength $2 $4 } : "(" Strength0 "," Strength1 ")" { ($2 , $4 ) }
| "(" Strength1 "," Strength0 ")" { DriveStrength $4 $2 } | "(" Strength1 "," Strength0 ")" { ($4 , $2 ) }
| "(" Strength0 "," "highz1" ")" { DriveStrength $2 Highz1 } | "(" Strength0 "," "highz1" ")" { ($2 , Highz1) }
| "(" Strength1 "," "highz0" ")" { DriveStrength Highz0 $2 } | "(" Strength1 "," "highz0" ")" { (Highz0, $2 ) }
| "(" "highz0" "," Strength1 ")" { DriveStrength Highz0 $4 } | "(" "highz0" "," Strength1 ")" { (Highz0, $4 ) }
| "(" "highz1" "," Strength0 ")" { DriveStrength $4 Highz1 } | "(" "highz1" "," Strength0 ")" { ($4 , Highz1) }
Strength0 :: { Strength0 } Strength0 :: { Strength0 }
: "supply0" { Supply0 } : "supply0" { Supply0 }
| "strong0" { Strong0 } | "strong0" { Strong0 }
......
...@@ -54,6 +54,8 @@ import Language.SystemVerilog.Parser.Tokens (Position(..)) ...@@ -54,6 +54,8 @@ import Language.SystemVerilog.Parser.Tokens (Position(..))
data DeclToken data DeclToken
= DTComma Position = DTComma Position
| DTAutoDim Position | DTAutoDim Position
| DTConst Position
| DTVar Position
| DTAsgn Position AsgnOp (Maybe Timing) Expr | DTAsgn Position AsgnOp (Maybe Timing) Expr
| DTRange Position (PartSelectMode, Range) | DTRange Position (PartSelectMode, Range)
| DTIdent Position Identifier | DTIdent Position Identifier
...@@ -61,6 +63,7 @@ data DeclToken ...@@ -61,6 +63,7 @@ data DeclToken
| DTCSIdent Position Identifier [ParamBinding] Identifier | DTCSIdent Position Identifier [ParamBinding] Identifier
| DTDir Position Direction | DTDir Position Direction
| DTType Position (Signing -> [Range] -> Type) | DTType Position (Signing -> [Range] -> Type)
| DTNet Position NetType Strength
| DTParams Position [ParamBinding] | DTParams Position [ParamBinding]
| DTInstance Position [PortBinding] | DTInstance Position [PortBinding]
| DTBit Position Expr | DTBit Position Expr
...@@ -130,6 +133,11 @@ parseDTsAsPortDecls' pieces = ...@@ -130,6 +133,11 @@ parseDTsAsPortDecls' pieces =
where where
decl = Variable dir t x a e decl = Variable dir t x a e
dir = if currDir == Local then lastDir else currDir dir = if currDir == Local then lastDir else currDir
propagateDirections lastDir (Net currDir n s t x a e : decls) =
decl : propagateDirections dir decls
where
decl = Net dir n s t x a e
dir = if currDir == Local then lastDir else currDir
propagateDirections dir (decl : decls) = propagateDirections dir (decl : decls) =
decl : propagateDirections dir decls decl : propagateDirections dir decls
propagateDirections _ [] = [] propagateDirections _ [] = []
...@@ -138,6 +146,7 @@ parseDTsAsPortDecls' pieces = ...@@ -138,6 +146,7 @@ parseDTsAsPortDecls' pieces =
portNames = filter (not . null) . map portName portNames = filter (not . null) . map portName
portName :: Decl -> Identifier portName :: Decl -> Identifier
portName (Variable _ _ ident _ _) = ident portName (Variable _ _ ident _ _) = ident
portName (Net _ _ _ _ ident _ _) = ident
portName CommentDecl{} = "" portName CommentDecl{} = ""
portName decl = portName decl =
error $ "unexpected non-variable port declaration: " ++ (show decl) error $ "unexpected non-variable port declaration: " ++ (show decl)
...@@ -271,11 +280,12 @@ parseDTsAsDeclOrStmt tokens = ...@@ -271,11 +280,12 @@ parseDTsAsDeclOrStmt tokens =
lhs = case takeLHS lhsToks of lhs = case takeLHS lhsToks of
Nothing -> error $ "could not parse as LHS: " ++ show lhsToks Nothing -> error $ "could not parse as LHS: " ++ show lhsToks
Just l -> l Just l -> l
hasLeadingDecl = tokens /= l4 && tripLookahead l4 hasLeadingDecl = tokens /= l5 && tripLookahead l5
(_, l1) = takeDir tokens (_, l1) = takeDir tokens
(_, l2) = takeLifetime l1 (_, l2) = takeLifetime l1
(_, l3) = takeType l2 (_, l3) = takeVarOrNet l2
(_, l4) = takeRanges l3 (_, l4) = takeType l3
(_, l5) = takeRanges l4
traceStmt :: Position -> Stmt traceStmt :: Position -> Stmt
traceStmt pos = CommentStmt $ "Trace: " ++ show pos traceStmt pos = CommentStmt $ "Trace: " ++ show pos
...@@ -366,12 +376,13 @@ takeLHSStep _ _ = Nothing ...@@ -366,12 +376,13 @@ takeLHSStep _ _ = Nothing
-- batches together separate declaration lists -- batches together separate declaration lists
type DeclBase = Direction -> Identifier -> [Range] -> Expr -> Decl
type Triplet = (Identifier, [Range], Expr) type Triplet = (Identifier, [Range], Expr)
type Component = (Direction, Type, [Triplet]) type Component = (Direction, DeclBase, [Triplet])
finalize :: (Position, Component) -> [Decl] finalize :: (Position, Component) -> [Decl]
finalize (pos, (dir, typ, trips)) = finalize (pos, (dir, base, trips)) =
CommentDecl ("Trace: " ++ show pos) : CommentDecl ("Trace: " ++ show pos) :
map (\(x, a, e) -> Variable dir typ x a e) trips map (\(x, a, e) -> base dir x a e) trips
-- internal; entrypoint of the critical portion of our parser -- internal; entrypoint of the critical portion of our parser
...@@ -387,18 +398,19 @@ parseDTsAsComponent [] = error "parseDTsAsComponent unexpected end of tokens" ...@@ -387,18 +398,19 @@ parseDTsAsComponent [] = error "parseDTsAsComponent unexpected end of tokens"
parseDTsAsComponent l0 = parseDTsAsComponent l0 =
if l /= Nothing && l /= Just Automatic then if l /= Nothing && l /= Just Automatic then
error $ "unexpected non-automatic lifetime: " ++ show l0 error $ "unexpected non-automatic lifetime: " ++ show l0
else if dir == Local && tf rs == Implicit Unspecified [] then else if dir == Local && length l2 == length l5 then
error $ "declaration(s) missing type information: " error $ "declaration(s) missing type information: "
++ show (position, tps) ++ show (position, tps)
else else
(position, component, l5) (position, component, l6)
where where
(dir, l1) = takeDir l0 (dir, l1) = takeDir l0
(l , l2) = takeLifetime l1 (l , l2) = takeLifetime l1
(tf , l3) = takeType l2 (von, l3) = takeVarOrNet l2
(rs , l4) = takeRanges l3 (tf , l4) = takeType l3
(tps, l5) = takeTrips l4 True (rs , l5) = takeRanges l4
component = (dir, tf rs, tps) (tps, l6) = takeTrips l5 True
component = (dir, von $ tf rs, tps)
position = tokPos $ head l0 position = tokPos $ head l0
takeTrips :: [DeclToken] -> Bool -> ([Triplet], [DeclToken]) takeTrips :: [DeclToken] -> Bool -> ([Triplet], [DeclToken])
...@@ -449,6 +461,13 @@ takeLifetime :: [DeclToken] -> (Maybe Lifetime, [DeclToken]) ...@@ -449,6 +461,13 @@ takeLifetime :: [DeclToken] -> (Maybe Lifetime, [DeclToken])
takeLifetime (DTLifetime _ l : rest) = (Just l, rest) takeLifetime (DTLifetime _ l : rest) = (Just l, rest)
takeLifetime rest = (Nothing, rest) takeLifetime rest = (Nothing, rest)
takeVarOrNet :: [DeclToken] -> (Type -> DeclBase, [DeclToken])
takeVarOrNet (DTConst _ : DTConst pos : _) =
error $ "unexpected const after const at " ++ show pos
takeVarOrNet (DTConst _ : tokens) = takeVarOrNet tokens
takeVarOrNet (DTNet _ n s : tokens) = (\t d -> Net d n s t, tokens)
takeVarOrNet tokens = (flip Variable, tokens)
takeType :: [DeclToken] -> ([Range] -> Type, [DeclToken]) takeType :: [DeclToken] -> ([Range] -> Type, [DeclToken])
takeType (DTIdent _ a : DTDot _ b : rest) = (InterfaceT a b , rest) takeType (DTIdent _ a : DTDot _ b : rest) = (InterfaceT a b , rest)
takeType (DTType _ tf : DTSigning _ sg : rest) = (tf sg , rest) takeType (DTType _ tf : DTSigning _ sg : rest) = (tf sg , rest)
...@@ -469,6 +488,13 @@ takeType (DTIdent pos tn : rest) = ...@@ -469,6 +488,13 @@ takeType (DTIdent pos tn : rest) =
(_, Nothing) -> True (_, Nothing) -> True
-- if comma is first, then this ident is a declaration -- if comma is first, then this ident is a declaration
(Just a, Just b) -> a < b (Just a, Just b) -> a < b
takeType (DTVar _ : DTVar pos : _) =
error $ "unexpected var after var at " ++ show pos
takeType (DTVar _ : rest) =
case tf [] of
Implicit sg [] -> (IntegerVector TLogic sg, rest')
_ -> (tf, rest')
where (tf, rest') = takeType rest
takeType rest = (Implicit Unspecified, rest) takeType rest = (Implicit Unspecified, rest)
takeRanges :: [DeclToken] -> ([Range], [DeclToken]) takeRanges :: [DeclToken] -> ([Range], [DeclToken])
...@@ -526,6 +552,8 @@ isComma _ = False ...@@ -526,6 +552,8 @@ isComma _ = False
tokPos :: DeclToken -> Position tokPos :: DeclToken -> Position
tokPos (DTComma p) = p tokPos (DTComma p) = p
tokPos (DTAutoDim p) = p tokPos (DTAutoDim p) = p
tokPos (DTConst p) = p
tokPos (DTVar p) = p
tokPos (DTAsgn p _ _ _) = p tokPos (DTAsgn p _ _ _) = p
tokPos (DTRange p _) = p tokPos (DTRange p _) = p
tokPos (DTIdent p _) = p tokPos (DTIdent p _) = p
...@@ -533,6 +561,7 @@ tokPos (DTPSIdent p _ _) = p ...@@ -533,6 +561,7 @@ tokPos (DTPSIdent p _ _) = p
tokPos (DTCSIdent p _ _ _) = p tokPos (DTCSIdent p _ _ _) = p
tokPos (DTDir p _) = p tokPos (DTDir p _) = p
tokPos (DTType p _) = p tokPos (DTType p _) = p
tokPos (DTNet p _ _) = p
tokPos (DTParams p _) = p tokPos (DTParams p _) = p
tokPos (DTInstance p _) = p tokPos (DTInstance p _) = p
tokPos (DTBit p _) = p tokPos (DTBit p _) = p
......
`include "net_or_var.vh"
module top;
typedef struct packed {
logic [3:0] a;
logic [2:0] b;
} t;
`TEST_ALL(logic, logic)
`TEST_ALL(wire, wire)
`TEST_ALL(wire logic, wire_logic)
`TEST_ALL(wand, wand)
`TEST_ALL(wand logic, wand_logic)
`TEST_ALL(var, var)
`TEST_ALL(var logic, var_logic)
`TEST_ALL(reg, reg)
`TEST_ALL(var reg, var_reg)
`TEST_BASE(t, t)
`TEST_BASE(wire t, wire_t)
`TEST_BASE(wand t, wand_t)
`TEST_BASE(var t, var_t)
endmodule
`include "net_or_var.vh"
module top;
`TEST_ALL(reg, logic)
`TEST_ALL(wire, wire)
`TEST_ALL(wire logic, wire_logic)
`TEST_ALL(wand, wand)
`TEST_ALL(wand logic, wand_logic)
`TEST_ALL(reg, var)
`TEST_ALL(reg, var_logic)
`TEST_ALL(reg, reg)
`TEST_ALL(reg, var_reg)
`TEST_BASE(reg [6:0], t)
`TEST_BASE(wire [6:0], wire_t)
`TEST_BASE(wand [6:0], wand_t)
`TEST_BASE(reg [6:0], var_t)
endmodule
`define TEST_BASE(keywords, identifier) \
keywords a_``identifier = 1'sb1; \
keywords [1:0] a_ranged_``identifier = 1'sb1;
`define TEST_ALL(keywords, identifier) \
`TEST_BASE(keywords, identifier) \
keywords signed a_signed_``identifier = 1'sb1; \
keywords unsigned a_unsigned_``identifier = 1'sb1; \
keywords signed [1:0] a_signed_ranged_``identifier = 1'sb1; \
keywords unsigned [1:0] a_unsigned_ranged_``identifier = 1'sb1;
typedef wire b_t; typedef logic b_t;
module top( module top(
input a [1:0], input a [1:0],
input b_t b input b_t b
); );
initial $display("%d %d %1d %1d", a, b, $bits(a), $bits(b)); initial $display("%b %b %b %1d %1d", a[0], a[1], b, $bits(a), $bits(b));
endmodule endmodule
module top( module top(
input [1:0] a, input wire [1:0] a,
input wire b input wire b
); );
initial $display("%d %d %1d %1d", a, b, $bits(a), $bits(b)); initial $display("%b %b %b %1d %1d", a[0], a[1], b, $bits(a), $bits(b));
endmodule 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