2021-01-24 17:55:03 +01:00
|
|
|
{-# LANGUAGE PatternSynonyms #-}
|
2019-03-25 18:29:35 +01:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
|
|
|
|
|
-
|
2021-02-19 18:24:56 +01:00
|
|
|
- SystemVerilog data, net, and parameter declarations
|
2019-03-25 18:29:35 +01:00
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
module Language.SystemVerilog.AST.Decl
|
2019-09-07 04:29:14 +02:00
|
|
|
( Decl (..)
|
|
|
|
|
, Direction (..)
|
|
|
|
|
, ParamScope (..)
|
2021-06-25 20:53:03 +02:00
|
|
|
, showDecls
|
2019-03-25 18:29:35 +01:00
|
|
|
) where
|
|
|
|
|
|
2021-06-25 20:53:03 +02:00
|
|
|
import Data.List (intercalate)
|
2019-03-25 18:29:35 +01:00
|
|
|
import Text.Printf (printf)
|
|
|
|
|
|
|
|
|
|
import Language.SystemVerilog.AST.ShowHelp (showPad, unlines')
|
2021-06-25 20:53:03 +02:00
|
|
|
import Language.SystemVerilog.AST.Type (Type(TypedefRef, UnpackedType), Identifier, pattern UnknownType)
|
2019-03-25 18:29:35 +01:00
|
|
|
import Language.SystemVerilog.AST.Expr (Expr, Range, showRanges, showAssignment)
|
|
|
|
|
|
|
|
|
|
data Decl
|
2019-09-07 04:29:14 +02:00
|
|
|
= Param ParamScope Type Identifier Expr
|
2021-01-24 17:55:03 +01:00
|
|
|
| ParamType ParamScope Identifier Type
|
2020-06-14 21:56:09 +02:00
|
|
|
| Variable Direction Type Identifier [Range] Expr
|
2020-01-31 04:17:17 +01:00
|
|
|
| CommentDecl String
|
2021-03-09 20:18:37 +01:00
|
|
|
deriving Eq
|
2019-03-25 18:29:35 +01:00
|
|
|
|
|
|
|
|
instance Show Decl where
|
|
|
|
|
showList l _ = unlines' $ map show l
|
2021-01-21 19:48:36 +01:00
|
|
|
show (Param s t x e) = printf "%s %s%s%s;" (show s) (showPad t) x (showAssignment e)
|
2021-06-25 20:53:03 +02:00
|
|
|
show (ParamType Localparam x (TypedefRef e)) =
|
|
|
|
|
printf "typedef %s %s;" (show e) x
|
2021-05-25 05:06:07 +02:00
|
|
|
show (ParamType Localparam x (UnpackedType t rs)) =
|
|
|
|
|
printf "typedef %s %s%s;" (show t) x (showRanges rs)
|
2021-01-24 17:55:03 +01:00
|
|
|
show (ParamType s x t) = printf "%s type %s%s;" (show s) x tStr
|
|
|
|
|
where tStr = if t == UnknownType then "" else " = " ++ show t
|
2020-06-14 21:56:09 +02:00
|
|
|
show (Variable d t x a e) = printf "%s%s%s%s%s;" (showPad d) (showPad t) x (showRanges a) (showAssignment e)
|
2020-01-31 04:17:17 +01:00
|
|
|
show (CommentDecl c) =
|
|
|
|
|
if elem '\n' c
|
|
|
|
|
then "// " ++ show c
|
|
|
|
|
else "// " ++ c
|
2019-03-25 18:29:35 +01:00
|
|
|
|
2021-06-25 20:53:03 +02:00
|
|
|
showDecls :: Char -> String -> [Decl] -> String
|
|
|
|
|
showDecls delim whitespace =
|
|
|
|
|
dropDelim . intercalate whitespace . map showDecl
|
|
|
|
|
where
|
|
|
|
|
dropDelim :: String -> String
|
|
|
|
|
dropDelim [] = []
|
|
|
|
|
dropDelim [x] = if x == delim then [] else [x]
|
|
|
|
|
dropDelim (x : xs) = x : dropDelim xs
|
|
|
|
|
showDecl (CommentDecl c) =
|
|
|
|
|
if whitespace == " "
|
|
|
|
|
then "/* " ++ c ++ " */"
|
|
|
|
|
else show $ CommentDecl c
|
|
|
|
|
showDecl decl = (init $ show decl) ++ [delim]
|
|
|
|
|
|
2019-03-25 18:29:35 +01:00
|
|
|
data Direction
|
|
|
|
|
= Input
|
|
|
|
|
| Output
|
|
|
|
|
| Inout
|
|
|
|
|
| Local
|
2021-03-09 20:18:37 +01:00
|
|
|
deriving Eq
|
2019-03-25 18:29:35 +01:00
|
|
|
|
|
|
|
|
instance Show Direction where
|
|
|
|
|
show Input = "input"
|
|
|
|
|
show Output = "output"
|
|
|
|
|
show Inout = "inout"
|
|
|
|
|
show Local = ""
|
2019-09-07 04:29:14 +02:00
|
|
|
|
|
|
|
|
data ParamScope
|
|
|
|
|
= Parameter
|
|
|
|
|
| Localparam
|
2021-03-09 20:18:37 +01:00
|
|
|
deriving Eq
|
2019-09-07 04:29:14 +02:00
|
|
|
|
|
|
|
|
instance Show ParamScope where
|
|
|
|
|
show Parameter = "parameter"
|
|
|
|
|
show Localparam = "localparam"
|