Commit da38776d by Zachary Snow

decls are now package items

parent d01df611
...@@ -73,7 +73,7 @@ convergeUsage items enums = ...@@ -73,7 +73,7 @@ convergeUsage items enums =
toItem :: EnumItem -> ModuleItem toItem :: EnumItem -> ModuleItem
toItem ((r, x), v) = toItem ((r, x), v) =
MIDecl $ Localparam itemType x v' MIPackageItem $ Decl $ Localparam itemType x v'
where where
v' = sizedExpr x r (simplify v) v' = sizedExpr x r (simplify v)
itemType = Implicit Unspecified [r] itemType = Implicit Unspecified [r]
......
...@@ -58,7 +58,7 @@ convertDescription interfaces modules (Part extern Module lifetime name ports it ...@@ -58,7 +58,7 @@ convertDescription interfaces modules (Part extern Module lifetime name ports it
(instances, modports) = execWriter $ mapM (instances, modports) = execWriter $ mapM
(collectNestedModuleItemsM collectInterface) items (collectNestedModuleItemsM collectInterface) items
collectInterface :: ModuleItem -> Writer (Instances, Modports) () collectInterface :: ModuleItem -> Writer (Instances, Modports) ()
collectInterface (MIDecl (Variable _ t ident _ _)) = collectInterface (MIPackageItem (Decl (Variable _ t ident _ _))) =
case t of case t of
InterfaceT interfaceName (Just modportName) [] -> InterfaceT interfaceName (Just modportName) [] ->
tell (Map.empty, Map.singleton ident modportDecls) tell (Map.empty, Map.singleton ident modportDecls)
...@@ -71,10 +71,11 @@ convertDescription interfaces modules (Part extern Module lifetime name ports it ...@@ -71,10 +71,11 @@ convertDescription interfaces modules (Part extern Module lifetime name ports it
collectInterface _ = return () collectInterface _ = return ()
mapInterface :: ModuleItem -> ModuleItem mapInterface :: ModuleItem -> ModuleItem
mapInterface (orig @ (MIDecl (Variable Local t ident _ _))) = mapInterface (orig @ (MIPackageItem (Decl (Variable Local t ident _ _)))) =
case Map.lookup ident modports of case Map.lookup ident modports of
Just modportDecls -> Generate $ Just modportDecls -> Generate $
map (GenModuleItem . MIDecl . mapper) modportDecls map (GenModuleItem . MIPackageItem . Decl . mapper)
modportDecls
Nothing -> orig Nothing -> orig
where where
InterfaceT interfaceName (Just _) [] = t InterfaceT interfaceName (Just _) [] = t
...@@ -208,7 +209,7 @@ lookupType items (Ident ident) = ...@@ -208,7 +209,7 @@ lookupType items (Ident ident) =
ts -> head ts ts -> head ts
where where
findType :: ModuleItem -> Maybe (Type, [Range]) findType :: ModuleItem -> Maybe (Type, [Range])
findType (MIDecl (Variable _ t x rs Nothing)) = findType (MIPackageItem (Decl (Variable _ t x rs Nothing))) =
if x == ident then Just (t, rs) else Nothing if x == ident then Just (t, rs) else Nothing
findType _ = Nothing findType _ = Nothing
lookupType _ expr = lookupType _ expr =
...@@ -222,7 +223,7 @@ inlineInterface (ports, items) (instanceName, instancePorts) = ...@@ -222,7 +223,7 @@ inlineInterface (ports, items) (instanceName, instancePorts) =
(:) (MIPackageItem $ Comment $ "expanded instance: " ++ instanceName) $ (:) (MIPackageItem $ Comment $ "expanded instance: " ++ instanceName) $
flip (++) portBindings $ flip (++) portBindings $
map (traverseNestedModuleItems removeModport) $ map (traverseNestedModuleItems removeModport) $
map (traverseNestedModuleItems removeMIDeclDir) $ map (traverseNestedModuleItems removeDeclDir) $
itemsPrefixed itemsPrefixed
where where
prefix = instanceName ++ "_" prefix = instanceName ++ "_"
...@@ -238,10 +239,10 @@ inlineInterface (ports, items) (instanceName, instancePorts) = ...@@ -238,10 +239,10 @@ inlineInterface (ports, items) (instanceName, instancePorts) =
mapMaybe portBindingItem $ mapMaybe portBindingItem $
zip instancePortNames instancePortExprs zip instancePortNames instancePortExprs
removeMIDeclDir :: ModuleItem -> ModuleItem removeDeclDir :: ModuleItem -> ModuleItem
removeMIDeclDir (MIDecl (Variable _ t x a me)) = removeDeclDir (MIPackageItem (Decl (Variable _ t x a me))) =
MIDecl $ Variable Local t x a me MIPackageItem $ Decl $ Variable Local t x a me
removeMIDeclDir other = other removeDeclDir other = other
removeModport :: ModuleItem -> ModuleItem removeModport :: ModuleItem -> ModuleItem
removeModport (Modport x _) = removeModport (Modport x _) =
MIPackageItem $ Comment $ "removed modport " ++ x MIPackageItem $ Comment $ "removed modport " ++ x
......
...@@ -43,7 +43,7 @@ convert ast = ...@@ -43,7 +43,7 @@ convert ast =
collectModuleItemsM collectPortDirsM orig collectModuleItemsM collectPortDirsM orig
where where
collectPortDirsM :: ModuleItem -> Writer Ports () collectPortDirsM :: ModuleItem -> Writer Ports ()
collectPortDirsM (MIDecl (Variable dir _ ident _ _)) = collectPortDirsM (MIPackageItem (Decl (Variable dir _ ident _ _))) =
if dir == Local then if dir == Local then
return () return ()
else if elem ident portNames then else if elem ident portNames then
...@@ -100,7 +100,7 @@ convertDescription ports orig = ...@@ -100,7 +100,7 @@ convertDescription ports orig =
tmpExpr = Ident tmp tmpExpr = Ident tmp
t = Net TWire [(Bits $ Right expr, Number "1")] t = Net TWire [(Bits $ Right expr, Number "1")]
items = items =
[ MIDecl $ Variable Local t tmp [] Nothing [ MIPackageItem $ Decl $ Variable Local t tmp [] Nothing
, AlwaysC AlwaysComb $ AsgnBlk AsgnOpEq lhs tmpExpr] , AlwaysC AlwaysComb $ AsgnBlk AsgnOpEq lhs tmpExpr]
lhs = case exprToLHS expr of lhs = case exprToLHS expr of
Just l -> l Just l -> l
...@@ -110,8 +110,8 @@ convertDescription ports orig = ...@@ -110,8 +110,8 @@ convertDescription ports orig =
++ portName ++ " of " ++ instanceName ++ portName ++ " of " ++ instanceName
fixBinding other = (other, []) fixBinding other = (other, [])
-- rewrite variable declarations to have the correct type -- rewrite variable declarations to have the correct type
convertModuleItem (MIDecl (Variable dir (IntegerVector TLogic sg mr) ident a me)) = convertModuleItem (MIPackageItem (Decl (Variable dir (IntegerVector TLogic sg mr) ident a me))) =
MIDecl $ Variable dir (t mr) ident a me MIPackageItem $ Decl $ Variable dir (t mr) ident a me
where where
t = if sg /= Unspecified || Set.member ident idents t = if sg /= Unspecified || Set.member ident idents
then IntegerVector TReg sg then IntegerVector TReg sg
......
...@@ -151,14 +151,15 @@ traverseModuleItemsM mapper (PackageItem packageItem) = do ...@@ -151,14 +151,15 @@ traverseModuleItemsM mapper (PackageItem packageItem) = do
return $ case item' of return $ case item' of
MIPackageItem packageItem' -> PackageItem packageItem' MIPackageItem packageItem' -> PackageItem packageItem'
other -> error $ "encountered bad package module item: " ++ show other other -> error $ "encountered bad package module item: " ++ show other
traverseModuleItemsM mapper (Package lifetime name items) = do traverseModuleItemsM mapper (Package lifetime name packageItems) = do
let items = map MIPackageItem packageItems
converted <- converted <-
traverseModuleItemsM mapper (Part False Module Nothing "DNE" [] items) traverseModuleItemsM mapper (Part False Module Nothing "DNE" [] items)
let items' = case converted of let items' = case converted of
Part False Module Nothing "DNE" [] newItems -> newItems Part False Module Nothing "DNE" [] newItems -> newItems
_ -> error $ "redirected Package traverse failed: " _ -> error $ "redirected Package traverse failed: "
++ show converted ++ show converted
return $ Package lifetime name items' return $ Package lifetime name $ map (\(MIPackageItem item) -> item) items'
traverseModuleItemsM _ (Directive str) = return $ Directive str traverseModuleItemsM _ (Directive str) = return $ Directive str
traverseModuleItems :: Mapper ModuleItem -> Mapper Description traverseModuleItems :: Mapper ModuleItem -> Mapper Description
...@@ -503,8 +504,8 @@ traverseExprsM' strat exprMapper = moduleItemMapper ...@@ -503,8 +504,8 @@ traverseExprsM' strat exprMapper = moduleItemMapper
moduleItemMapper (MIAttr attr mi) = moduleItemMapper (MIAttr attr mi) =
-- note: we exclude expressions in attributes from conversion -- note: we exclude expressions in attributes from conversion
return $ MIAttr attr mi return $ MIAttr attr mi
moduleItemMapper (MIDecl decl) = moduleItemMapper (MIPackageItem (Decl decl)) =
declMapper decl >>= return . MIDecl declMapper decl >>= return . MIPackageItem . Decl
moduleItemMapper (Defparam lhs expr) = do moduleItemMapper (Defparam lhs expr) = do
lhs' <- lhsMapper lhs lhs' <- lhsMapper lhs
expr' <- exprMapper expr expr' <- exprMapper expr
...@@ -725,8 +726,8 @@ traverseDeclsM' strat mapper item = do ...@@ -725,8 +726,8 @@ traverseDeclsM' strat mapper item = do
item' <- miMapper item item' <- miMapper item
traverseStmtsM' strat stmtMapper item' traverseStmtsM' strat stmtMapper item'
where where
miMapper (MIDecl decl) = miMapper (MIPackageItem (Decl decl)) =
mapper decl >>= return . MIDecl mapper decl >>= return . MIPackageItem . Decl
miMapper (MIPackageItem (Function l t x decls stmts)) = do miMapper (MIPackageItem (Function l t x decls stmts)) = do
decls' <- decls' <-
if strat == IncludeTFs if strat == IncludeTFs
...@@ -921,7 +922,7 @@ collectNestedExprsM = collectify traverseNestedExprsM ...@@ -921,7 +922,7 @@ collectNestedExprsM = collectify traverseNestedExprsM
-- Traverse all the declaration scopes within a ModuleItem. Note that Functions, -- Traverse all the declaration scopes within a ModuleItem. Note that Functions,
-- Tasks, Always and Initial blocks are all NOT passed through ModuleItem -- Tasks, Always and Initial blocks are all NOT passed through ModuleItem
-- mapper, and MIDecl ModuleItems are NOT passed through the Decl mapper. The -- mapper, and Decl ModuleItems are NOT passed through the Decl mapper. The
-- state is restored to its previous value after each scope is exited. Only the -- state is restored to its previous value after each scope is exited. Only the
-- Decl mapper may modify the state, as we maintain the invariant that all other -- Decl mapper may modify the state, as we maintain the invariant that all other
-- functions restore the state on exit. The Stmt mapper must not traverse -- functions restore the state on exit. The Stmt mapper must not traverse
...@@ -992,12 +993,12 @@ scopedConversion ...@@ -992,12 +993,12 @@ scopedConversion
scopedConversion traverseDeclM traverseModuleItemM traverseStmtM s description = scopedConversion traverseDeclM traverseModuleItemM traverseStmtM s description =
evalState (initialTraverse description >>= scopedTraverse) s evalState (initialTraverse description >>= scopedTraverse) s
where where
initialTraverse = traverseModuleItemsM traverseMIDecl initialTraverse = traverseModuleItemsM traverseMIPackageItemDecl
scopedTraverse = traverseModuleItemsM $ scopedTraverse = traverseModuleItemsM $
traverseScopesM traverseDeclM traverseModuleItemM traverseStmtM traverseScopesM traverseDeclM traverseModuleItemM traverseStmtM
traverseMIDecl (MIDecl decl) = traverseMIPackageItemDecl (MIPackageItem (Decl decl)) =
traverseDeclM decl >>= return . MIDecl traverseDeclM decl >>= return . MIPackageItem . Decl
traverseMIDecl other = return other traverseMIPackageItemDecl other = return other
-- convert a basic mapper with an initial argument to a stateful mapper -- convert a basic mapper with an initial argument to a stateful mapper
stately :: (Eq s, Show s) => (s -> Mapper a) -> MapperM (State s) a stately :: (Eq s, Show s) => (s -> Mapper a) -> MapperM (State s) a
......
...@@ -26,7 +26,7 @@ import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem) ...@@ -26,7 +26,7 @@ import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem)
data Description data Description
= Part Bool PartKW (Maybe Lifetime) Identifier [Identifier] [ModuleItem] = Part Bool PartKW (Maybe Lifetime) Identifier [Identifier] [ModuleItem]
| PackageItem PackageItem | PackageItem PackageItem
| Package (Maybe Lifetime) Identifier [ModuleItem] | Package (Maybe Lifetime) Identifier [PackageItem]
| Directive String -- currently unused | Directive String -- currently unused
deriving Eq deriving Eq
...@@ -57,6 +57,7 @@ data PackageItem ...@@ -57,6 +57,7 @@ data PackageItem
| Function (Maybe Lifetime) Type Identifier [Decl] [Stmt] | Function (Maybe Lifetime) Type Identifier [Decl] [Stmt]
| Task (Maybe Lifetime) Identifier [Decl] [Stmt] | Task (Maybe Lifetime) Identifier [Decl] [Stmt]
| Import [(Identifier, Maybe Identifier)] | Import [(Identifier, Maybe Identifier)]
| Decl Decl
| Comment String | Comment String
deriving Eq deriving Eq
...@@ -75,6 +76,7 @@ instance Show PackageItem where ...@@ -75,6 +76,7 @@ instance Show PackageItem where
(commas $ map showImport imports) (commas $ map showImport imports)
where where
showImport (x, y) = printf "%s::%s" x (fromMaybe "*" y) showImport (x, y) = printf "%s::%s" x (fromMaybe "*" y)
show (Decl decl) = show decl
show (Comment c) = show (Comment c) =
if elem '\n' c if elem '\n' c
then "// " ++ show c then "// " ++ show c
......
...@@ -21,7 +21,7 @@ import Text.Printf (printf) ...@@ -21,7 +21,7 @@ import Text.Printf (printf)
import Language.SystemVerilog.AST.ShowHelp import Language.SystemVerilog.AST.ShowHelp
import Language.SystemVerilog.AST.Attr (Attr) import Language.SystemVerilog.AST.Attr (Attr)
import Language.SystemVerilog.AST.Decl (Decl, Direction) import Language.SystemVerilog.AST.Decl (Direction)
import Language.SystemVerilog.AST.Description (PackageItem) import Language.SystemVerilog.AST.Description (PackageItem)
import Language.SystemVerilog.AST.Expr (Expr(Ident), Range, showRanges) import Language.SystemVerilog.AST.Expr (Expr(Ident), Range, showRanges)
import Language.SystemVerilog.AST.GenItem (GenItem) import Language.SystemVerilog.AST.GenItem (GenItem)
...@@ -31,7 +31,6 @@ import Language.SystemVerilog.AST.Type (Identifier) ...@@ -31,7 +31,6 @@ import Language.SystemVerilog.AST.Type (Identifier)
data ModuleItem data ModuleItem
= MIAttr Attr ModuleItem = MIAttr Attr ModuleItem
| MIDecl Decl
| AlwaysC AlwaysKW Stmt | AlwaysC AlwaysKW Stmt
| Assign (Maybe Expr) LHS Expr | Assign (Maybe Expr) LHS Expr
| Defparam LHS Expr | Defparam LHS Expr
...@@ -47,7 +46,6 @@ data ModuleItem ...@@ -47,7 +46,6 @@ data ModuleItem
deriving Eq deriving Eq
instance Show ModuleItem where instance Show ModuleItem where
show (MIDecl nest) = show nest
show (MIPackageItem i) = show i show (MIPackageItem i) = show i
show (MIAttr attr mi ) = printf "%s %s" (show attr) (show mi) show (MIAttr attr mi ) = printf "%s %s" (show attr) (show mi)
show (AlwaysC k b) = printf "%s %s" (show k) (show b) show (AlwaysC k b) = printf "%s %s" (show k) (show b)
......
...@@ -256,13 +256,13 @@ opt(p) :: { Maybe a } ...@@ -256,13 +256,13 @@ opt(p) :: { Maybe a }
Descriptions :: { [Description] } Descriptions :: { [Description] }
: {- empty -} { [] } : {- empty -} { [] }
| Descriptions ";" { $1 } | Descriptions ";" { $1 }
| Descriptions Description { $1 ++ [$2] } | Descriptions Description { $1 ++ $2 }
Description :: { Description } Description :: { [Description] }
: Part(ModuleKW , "endmodule" ) { $1 } : Part(ModuleKW , "endmodule" ) { [$1] }
| Part(InterfaceKW, "endinterface") { $1 } | Part(InterfaceKW, "endinterface") { [$1] }
| PackageItem { PackageItem $1 } | PackageDeclaration { [$1] }
| PackageDeclaration { $1 } | PackageItem { map PackageItem $1 }
Type :: { Type } Type :: { Type }
: TypeNonIdent { $1 } : TypeNonIdent { $1 }
...@@ -344,7 +344,7 @@ InterfaceKW :: { PartKW } ...@@ -344,7 +344,7 @@ InterfaceKW :: { PartKW }
: "interface" { Interface } : "interface" { Interface }
PackageDeclaration :: { Description } PackageDeclaration :: { Description }
: "package" opt(Lifetime) Identifier ";" ModuleItems "endpackage" opt(Tag) { Package $2 $3 $5 } : "package" opt(Lifetime) Identifier ";" PackageItems "endpackage" opt(Tag) { Package $2 $3 $5 }
Tag :: { Identifier } Tag :: { Identifier }
: ":" Identifier { $2 } : ":" Identifier { $2 }
...@@ -356,7 +356,7 @@ ParamDecls :: { [ModuleItem] } ...@@ -356,7 +356,7 @@ ParamDecls :: { [ModuleItem] }
: ParamDecl(")") { $1 } : ParamDecl(")") { $1 }
| ParamDecl(",") ParamDecls { $1 ++ $2 } | ParamDecl(",") ParamDecls { $1 ++ $2 }
ParamDecl(delim) :: { [ModuleItem] } ParamDecl(delim) :: { [ModuleItem] }
: ParameterDecl(OnlyParamKW, delim) { map MIDecl $1 } : ParameterDecl(OnlyParamKW, delim) { map (MIPackageItem . Decl) $1 }
OnlyParamKW :: { Type -> Identifier -> Expr -> Decl } OnlyParamKW :: { Type -> Identifier -> Expr -> Decl }
: "parameter" { Parameter } : "parameter" { Parameter }
...@@ -447,14 +447,14 @@ ModuleItem :: { [ModuleItem] } ...@@ -447,14 +447,14 @@ ModuleItem :: { [ModuleItem] }
NonGenerateModuleItem :: { [ModuleItem] } NonGenerateModuleItem :: { [ModuleItem] }
-- This item covers module instantiations and all declarations -- This item covers module instantiations and all declarations
: DeclTokens(";") { parseDTsAsModuleItems $1 } : DeclTokens(";") { parseDTsAsModuleItems $1 }
| ParameterDecl(ParameterDeclKW, ";") { map MIDecl $1 } | ParameterDecl(ParameterDeclKW, ";") { map (MIPackageItem . Decl) $1 }
| "defparam" DefparamAsgns ";" { map (uncurry Defparam) $2 } | "defparam" DefparamAsgns ";" { map (uncurry Defparam) $2 }
| "assign" opt(DelayControl) LHS "=" Expr ";" { [Assign $2 $3 $5] } | "assign" opt(DelayControl) LHS "=" Expr ";" { [Assign $2 $3 $5] }
| AlwaysKW Stmt { [AlwaysC $1 $2] } | AlwaysKW Stmt { [AlwaysC $1 $2] }
| "initial" Stmt { [Initial $2] } | "initial" Stmt { [Initial $2] }
| "genvar" Identifiers ";" { map Genvar $2 } | "genvar" Identifiers ";" { map Genvar $2 }
| "modport" ModportItems ";" { map (uncurry Modport) $2 } | "modport" ModportItems ";" { map (uncurry Modport) $2 }
| PackageItem { [MIPackageItem $1] } | NonDeclPackageItem { [MIPackageItem $1] }
| NInputGateKW NInputGates ";" { map (\(a, b, c) -> NInputGate $1 a b c) $2 } | NInputGateKW NInputGates ";" { map (\(a, b, c) -> NInputGate $1 a b c) $2 }
| NOutputGateKW NOutputGates ";" { map (\(a, b, c) -> NOutputGate $1 a b c) $2 } | NOutputGateKW NOutputGates ";" { map (\(a, b, c) -> NOutputGate $1 a b c) $2 }
| AttributeInstance ModuleItem { map (MIAttr $1) $2 } | AttributeInstance ModuleItem { map (MIAttr $1) $2 }
...@@ -564,7 +564,14 @@ DefparamAsgns :: { [(LHS, Expr)] } ...@@ -564,7 +564,14 @@ DefparamAsgns :: { [(LHS, Expr)] }
DefparamAsgn :: { (LHS, Expr) } DefparamAsgn :: { (LHS, Expr) }
: LHS "=" Expr { ($1, $3) } : LHS "=" Expr { ($1, $3) }
PackageItem :: { PackageItem } PackageItems :: { [PackageItem] }
: PackageItem { $1 }
| PackageItems PackageItem { $1 ++ $2 }
PackageItem :: { [PackageItem] }
: DeclTokens(";") { map Decl $ parseDTsAsDecls $1 }
| ParameterDecl(ParameterDeclKW, ";") { map Decl $1 }
| NonDeclPackageItem { [$1] }
NonDeclPackageItem :: { PackageItem }
: "typedef" Type Identifier ";" { Typedef $2 $3 } : "typedef" Type Identifier ";" { Typedef $2 $3 }
| "function" opt(Lifetime) FuncRetAndName TFItems DeclsAndStmts "endfunction" opt(Tag) { Function $2 (fst $3) (snd $3) (map defaultFuncInput $ (map makeInput $4) ++ fst $5) (snd $5) } | "function" opt(Lifetime) FuncRetAndName TFItems DeclsAndStmts "endfunction" opt(Tag) { Function $2 (fst $3) (snd $3) (map defaultFuncInput $ (map makeInput $4) ++ fst $5) (snd $5) }
| "task" opt(Lifetime) Identifier TFItems DeclsAndStmts "endtask" opt(Tag) { Task $2 $3 (map defaultFuncInput $ $4 ++ fst $5) (snd $5) } | "task" opt(Lifetime) Identifier TFItems DeclsAndStmts "endtask" opt(Tag) { Task $2 $3 (map defaultFuncInput $ $4 ++ fst $5) (snd $5) }
......
...@@ -84,7 +84,7 @@ parseDTsAsPortDecls pieces = ...@@ -84,7 +84,7 @@ parseDTsAsPortDecls pieces =
forbidNonEqAsgn pieces $ forbidNonEqAsgn pieces $
if isSimpleList if isSimpleList
then (simpleIdents, []) then (simpleIdents, [])
else (portNames declarations, map MIDecl declarations) else (portNames declarations, map (MIPackageItem . Decl) declarations)
where where
commaIdxs = findIndices isComma pieces commaIdxs = findIndices isComma pieces
identIdxs = findIndices isIdent pieces identIdxs = findIndices isIdent pieces
...@@ -116,7 +116,7 @@ parseDTsAsModuleItems tokens = ...@@ -116,7 +116,7 @@ parseDTsAsModuleItems tokens =
forbidNonEqAsgn tokens $ forbidNonEqAsgn tokens $
if any isInstance tokens if any isInstance tokens
then parseDTsAsIntantiations tokens then parseDTsAsIntantiations tokens
else map MIDecl $ parseDTsAsDecl tokens else map (MIPackageItem . Decl) $ parseDTsAsDecl tokens
where where
isInstance :: DeclToken -> Bool isInstance :: DeclToken -> Bool
isInstance (DTInstance _) = True isInstance (DTInstance _) = True
......
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