Commit d01df611 by Zachary Snow

preliminary language support for packages

parent 4178751b
...@@ -64,6 +64,7 @@ convertDescription ports orig = ...@@ -64,6 +64,7 @@ convertDescription ports orig =
Part _ Interface _ _ _ _ -> False Part _ Interface _ _ _ _ -> False
Part _ Module _ _ _ _ -> True Part _ Module _ _ _ _ -> True
PackageItem _ -> True PackageItem _ -> True
Package _ _ _ -> False
Directive _ -> False Directive _ -> False
conversion = traverseDecls convertDecl . convertModuleItem conversion = traverseDecls convertDecl . convertModuleItem
idents = execWriter (collectModuleItemsM regIdents orig) idents = execWriter (collectModuleItemsM regIdents orig)
......
...@@ -151,6 +151,14 @@ traverseModuleItemsM mapper (PackageItem packageItem) = do ...@@ -151,6 +151,14 @@ 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
converted <-
traverseModuleItemsM mapper (Part False Module Nothing "DNE" [] items)
let items' = case converted of
Part False Module Nothing "DNE" [] newItems -> newItems
_ -> error $ "redirected Package traverse failed: "
++ show converted
return $ Package lifetime name items'
traverseModuleItemsM _ (Directive str) = return $ Directive str traverseModuleItemsM _ (Directive str) = return $ Directive str
traverseModuleItems :: Mapper ModuleItem -> Mapper Description traverseModuleItems :: Mapper ModuleItem -> Mapper Description
...@@ -399,6 +407,7 @@ traverseNestedExprsM mapper = exprMapper ...@@ -399,6 +407,7 @@ traverseNestedExprsM mapper = exprMapper
em (String s) = return $ String s em (String s) = return $ String s
em (Number s) = return $ Number s em (Number s) = return $ Number s
em (Ident i) = return $ Ident i em (Ident i) = return $ Ident i
em (PSIdent x y) = return $ PSIdent x y
em (Range e m (e1, e2)) = do em (Range e m (e1, e2)) = do
e' <- exprMapper e e' <- exprMapper e
e1' <- exprMapper e1 e1' <- exprMapper e1
...@@ -552,6 +561,8 @@ traverseExprsM' strat exprMapper = moduleItemMapper ...@@ -552,6 +561,8 @@ traverseExprsM' strat exprMapper = moduleItemMapper
return $ MIPackageItem $ Typedef t x return $ MIPackageItem $ Typedef t x
moduleItemMapper (MIPackageItem (Comment c)) = moduleItemMapper (MIPackageItem (Comment c)) =
return $ MIPackageItem $ Comment c return $ MIPackageItem $ Comment c
moduleItemMapper (MIPackageItem (Import imports)) =
return $ MIPackageItem $ Import imports
moduleItemMapper (AssertionItem (mx, a)) = do moduleItemMapper (AssertionItem (mx, a)) = do
a' <- traverseAssertionStmtsM stmtMapper a a' <- traverseAssertionStmtsM stmtMapper a
a'' <- traverseAssertionExprsM exprMapper a' a'' <- traverseAssertionExprsM exprMapper a'
......
...@@ -12,6 +12,7 @@ module Language.SystemVerilog.AST.Description ...@@ -12,6 +12,7 @@ module Language.SystemVerilog.AST.Description
, Lifetime (..) , Lifetime (..)
) where ) where
import Data.Maybe (fromMaybe)
import Data.List (intercalate) import Data.List (intercalate)
import Text.Printf (printf) import Text.Printf (printf)
...@@ -25,6 +26,7 @@ import {-# SOURCE #-} Language.SystemVerilog.AST.ModuleItem (ModuleItem) ...@@ -25,6 +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]
| Directive String -- currently unused | Directive String -- currently unused
deriving Eq deriving Eq
...@@ -42,6 +44,11 @@ instance Show Description where ...@@ -42,6 +44,11 @@ instance Show Description where
then "" then ""
else " " ++ indentedParenList ports else " " ++ indentedParenList ports
bodyStr = indent $ unlines' $ map show items bodyStr = indent $ unlines' $ map show items
show (Package lifetime name items) =
printf "package %s%s;\n%s\nendpackage"
(showLifetime lifetime) name bodyStr
where
bodyStr = indent $ unlines' $ map show items
show (PackageItem i) = show i show (PackageItem i) = show i
show (Directive str) = str show (Directive str) = str
...@@ -49,6 +56,7 @@ data PackageItem ...@@ -49,6 +56,7 @@ data PackageItem
= Typedef Type Identifier = Typedef Type Identifier
| 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)]
| Comment String | Comment String
deriving Eq deriving Eq
...@@ -62,6 +70,11 @@ instance Show PackageItem where ...@@ -62,6 +70,11 @@ instance Show PackageItem where
printf "task %s%s;\n%s\n%s\nendtask" printf "task %s%s;\n%s\n%s\nendtask"
(showLifetime ml) x (indent $ show i) (showLifetime ml) x (indent $ show i)
(indent $ unlines' $ map show b) (indent $ unlines' $ map show b)
show (Import imports) =
printf "import %s;"
(commas $ map showImport imports)
where
showImport (x, y) = printf "%s::%s" x (fromMaybe "*" y)
show (Comment c) = show (Comment c) =
if elem '\n' c if elem '\n' c
then "// " ++ show c then "// " ++ show c
......
...@@ -34,6 +34,7 @@ data Expr ...@@ -34,6 +34,7 @@ data Expr
= String String = String String
| Number String | Number String
| Ident Identifier | Ident Identifier
| PSIdent Identifier Identifier
| Range Expr PartSelectMode Range | Range Expr PartSelectMode Range
| Bit Expr Expr | Bit Expr Expr
| Repeat Expr [Expr] | Repeat Expr [Expr]
...@@ -51,6 +52,7 @@ data Expr ...@@ -51,6 +52,7 @@ data Expr
instance Show Expr where instance Show Expr where
show (Number str ) = str show (Number str ) = str
show (Ident str ) = str show (Ident str ) = str
show (PSIdent x y) = printf "%s::%s" x y
show (String str ) = printf "\"%s\"" str show (String str ) = printf "\"%s\"" str
show (Bit e b ) = printf "%s[%s]" (show e) (show b) show (Bit e b ) = printf "%s[%s]" (show e) (show b)
show (Range e m r) = printf "%s[%s%s%s]" (show e) (show $ fst r) (show m) (show $ snd r) show (Range e m r) = printf "%s[%s%s%s]" (show e) (show $ fst r) (show m) (show $ snd r)
......
...@@ -134,8 +134,10 @@ tokens :- ...@@ -134,8 +134,10 @@ tokens :-
"endgenerate" { tok KW_endgenerate } "endgenerate" { tok KW_endgenerate }
"endinterface" { tok KW_endinterface } "endinterface" { tok KW_endinterface }
"endmodule" { tok KW_endmodule } "endmodule" { tok KW_endmodule }
"endpackage" { tok KW_endpackage }
"endtask" { tok KW_endtask } "endtask" { tok KW_endtask }
"enum" { tok KW_enum } "enum" { tok KW_enum }
"export" { tok KW_export }
"extern" { tok KW_extern } "extern" { tok KW_extern }
"first_match" { tok KW_first_match } "first_match" { tok KW_first_match }
"for" { tok KW_for } "for" { tok KW_for }
...@@ -145,6 +147,7 @@ tokens :- ...@@ -145,6 +147,7 @@ tokens :-
"genvar" { tok KW_genvar } "genvar" { tok KW_genvar }
"if" { tok KW_if } "if" { tok KW_if }
"iff" { tok KW_iff } "iff" { tok KW_iff }
"import" { tok KW_import }
"initial" { tok KW_initial } "initial" { tok KW_initial }
"inout" { tok KW_inout } "inout" { tok KW_inout }
"input" { tok KW_input } "input" { tok KW_input }
...@@ -163,6 +166,7 @@ tokens :- ...@@ -163,6 +166,7 @@ tokens :-
"not" { tok KW_not } "not" { tok KW_not }
"or" { tok KW_or } "or" { tok KW_or }
"output" { tok KW_output } "output" { tok KW_output }
"package" { tok KW_package }
"packed" { tok KW_packed } "packed" { tok KW_packed }
"parameter" { tok KW_parameter } "parameter" { tok KW_parameter }
"posedge" { tok KW_posedge } "posedge" { tok KW_posedge }
......
...@@ -55,8 +55,10 @@ import Language.SystemVerilog.Parser.Tokens ...@@ -55,8 +55,10 @@ import Language.SystemVerilog.Parser.Tokens
"endgenerate" { Token KW_endgenerate _ _ } "endgenerate" { Token KW_endgenerate _ _ }
"endinterface" { Token KW_endinterface _ _ } "endinterface" { Token KW_endinterface _ _ }
"endmodule" { Token KW_endmodule _ _ } "endmodule" { Token KW_endmodule _ _ }
"endpackage" { Token KW_endpackage _ _ }
"endtask" { Token KW_endtask _ _ } "endtask" { Token KW_endtask _ _ }
"enum" { Token KW_enum _ _ } "enum" { Token KW_enum _ _ }
"export" { Token KW_export _ _ }
"extern" { Token KW_extern _ _ } "extern" { Token KW_extern _ _ }
"first_match" { Token KW_first_match _ _ } "first_match" { Token KW_first_match _ _ }
"for" { Token KW_for _ _ } "for" { Token KW_for _ _ }
...@@ -66,6 +68,7 @@ import Language.SystemVerilog.Parser.Tokens ...@@ -66,6 +68,7 @@ import Language.SystemVerilog.Parser.Tokens
"genvar" { Token KW_genvar _ _ } "genvar" { Token KW_genvar _ _ }
"if" { Token KW_if _ _ } "if" { Token KW_if _ _ }
"iff" { Token KW_iff _ _ } "iff" { Token KW_iff _ _ }
"import" { Token KW_import _ _ }
"initial" { Token KW_initial _ _ } "initial" { Token KW_initial _ _ }
"inout" { Token KW_inout _ _ } "inout" { Token KW_inout _ _ }
"input" { Token KW_input _ _ } "input" { Token KW_input _ _ }
...@@ -84,6 +87,7 @@ import Language.SystemVerilog.Parser.Tokens ...@@ -84,6 +87,7 @@ import Language.SystemVerilog.Parser.Tokens
"not" { Token KW_not _ _ } "not" { Token KW_not _ _ }
"or" { Token KW_or _ _ } "or" { Token KW_or _ _ }
"output" { Token KW_output _ _ } "output" { Token KW_output _ _ }
"package" { Token KW_package _ _ }
"packed" { Token KW_packed _ _ } "packed" { Token KW_packed _ _ }
"parameter" { Token KW_parameter _ _ } "parameter" { Token KW_parameter _ _ }
"posedge" { Token KW_posedge _ _ } "posedge" { Token KW_posedge _ _ }
...@@ -258,6 +262,7 @@ Description :: { Description } ...@@ -258,6 +262,7 @@ Description :: { Description }
: Part(ModuleKW , "endmodule" ) { $1 } : Part(ModuleKW , "endmodule" ) { $1 }
| Part(InterfaceKW, "endinterface") { $1 } | Part(InterfaceKW, "endinterface") { $1 }
| PackageItem { PackageItem $1 } | PackageItem { PackageItem $1 }
| PackageDeclaration { $1 }
Type :: { Type } Type :: { Type }
: TypeNonIdent { $1 } : TypeNonIdent { $1 }
...@@ -338,6 +343,9 @@ ModuleKW :: { PartKW } ...@@ -338,6 +343,9 @@ ModuleKW :: { PartKW }
InterfaceKW :: { PartKW } InterfaceKW :: { PartKW }
: "interface" { Interface } : "interface" { Interface }
PackageDeclaration :: { Description }
: "package" opt(Lifetime) Identifier ";" ModuleItems "endpackage" opt(Tag) { Package $2 $3 $5 }
Tag :: { Identifier } Tag :: { Identifier }
: ":" Identifier { $2 } : ":" Identifier { $2 }
...@@ -560,6 +568,14 @@ PackageItem :: { PackageItem } ...@@ -560,6 +568,14 @@ PackageItem :: { 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) }
| "import" PackageImportItems ";" { Import $2 }
PackageImportItems :: { [(Identifier, Maybe Identifier)] }
: PackageImportItem { [$1] }
| PackageImportItems "," PackageImportItem { $1 ++ [$3] }
PackageImportItem :: { (Identifier, Maybe Identifier) }
: Identifier "::" Identifier { ($1, Just $3) }
| Identifier "::" "*" { ($1, Nothing) }
FuncRetAndName :: { (Type, Identifier) } FuncRetAndName :: { (Type, Identifier) }
: Type Identifier { ($1 , $2) } : Type Identifier { ($1 , $2) }
...@@ -791,6 +807,7 @@ Expr :: { Expr } ...@@ -791,6 +807,7 @@ Expr :: { Expr }
| Identifier "(" CallArgs ")" { Call $1 $3 } | Identifier "(" CallArgs ")" { Call $1 $3 }
| "$bits" "(" BitsArg ")" { Bits $3 } | "$bits" "(" BitsArg ")" { Bits $3 }
| Identifier { Ident $1 } | Identifier { Ident $1 }
| Identifier "::" Identifier { PSIdent $1 $3 }
| Expr PartSelect { Range $1 (fst $2) (snd $2) } | Expr PartSelect { Range $1 (fst $2) (snd $2) }
| Expr "[" Expr "]" { Bit $1 $3 } | Expr "[" Expr "]" { Bit $1 $3 }
| "{" Expr "{" Exprs "}" "}" { Repeat $2 $4 } | "{" Expr "{" Exprs "}" "}" { Repeat $2 $4 }
......
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