2019-03-23 00:24:45 +01:00
|
|
|
{- sv2v
|
|
|
|
|
- Author: Zachary Snow <zach@zachjs.com>
|
|
|
|
|
- Initial Verilog AST Author: Tom Hawkins <tomahawkins@gmail.com>
|
|
|
|
|
-
|
|
|
|
|
- This AST allows for the representation of many syntactically invalid things,
|
|
|
|
|
- like input regs or modport declarations inside a module. Representing only
|
|
|
|
|
- syntactically valid files would make working with the AST a nightmare. We
|
|
|
|
|
- have placed an emphasis on making the conversion procedures in this project
|
|
|
|
|
- more easier to write, interpret, and maintain.
|
|
|
|
|
-
|
|
|
|
|
- In the future, we may want to have a utility which performs some basic
|
|
|
|
|
- invariant checks. I want to avoid making a full type-checker though, as we
|
|
|
|
|
- should only be given valid SystemVerilog input files.
|
|
|
|
|
-}
|
|
|
|
|
|
2019-02-08 06:19:39 +01:00
|
|
|
module Language.SystemVerilog.AST
|
2019-04-04 02:24:09 +02:00
|
|
|
( AST
|
2019-03-26 06:54:16 +01:00
|
|
|
, module Attr
|
2019-03-25 18:29:35 +01:00
|
|
|
, module Decl
|
2019-04-04 02:24:09 +02:00
|
|
|
, module Description
|
2019-03-23 00:24:45 +01:00
|
|
|
, module Expr
|
2019-04-04 02:24:09 +02:00
|
|
|
, module GenItem
|
2019-03-25 18:29:35 +01:00
|
|
|
, module LHS
|
2019-04-04 02:24:09 +02:00
|
|
|
, module ModuleItem
|
2020-07-12 23:06:27 +02:00
|
|
|
, module Number
|
2019-03-23 00:24:45 +01:00
|
|
|
, module Op
|
2019-03-25 18:29:35 +01:00
|
|
|
, module Stmt
|
2019-03-23 00:24:45 +01:00
|
|
|
, module Type
|
2019-04-23 02:44:35 +02:00
|
|
|
, exprToLHS
|
2019-09-04 05:36:29 +02:00
|
|
|
, lhsToExpr
|
2019-09-04 05:52:22 +02:00
|
|
|
, shortHash
|
2019-03-23 00:24:45 +01:00
|
|
|
) where
|
|
|
|
|
|
2019-09-04 05:52:22 +02:00
|
|
|
import Text.Printf (printf)
|
|
|
|
|
import Data.Hashable (hash)
|
|
|
|
|
|
2019-03-26 06:54:16 +01:00
|
|
|
import Language.SystemVerilog.AST.Attr as Attr
|
2019-03-25 18:29:35 +01:00
|
|
|
import Language.SystemVerilog.AST.Decl as Decl
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST.Description as Description
|
2019-03-23 00:24:45 +01:00
|
|
|
import Language.SystemVerilog.AST.Expr as Expr
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST.GenItem as GenItem
|
2019-03-25 18:29:35 +01:00
|
|
|
import Language.SystemVerilog.AST.LHS as LHS
|
2019-04-04 02:24:09 +02:00
|
|
|
import Language.SystemVerilog.AST.ModuleItem as ModuleItem
|
2020-07-12 23:06:27 +02:00
|
|
|
import Language.SystemVerilog.AST.Number as Number
|
2019-03-23 00:24:45 +01:00
|
|
|
import Language.SystemVerilog.AST.Op as Op
|
2019-03-25 18:29:35 +01:00
|
|
|
import Language.SystemVerilog.AST.Stmt as Stmt
|
2019-03-23 00:24:45 +01:00
|
|
|
import Language.SystemVerilog.AST.Type as Type
|
|
|
|
|
|
2019-02-18 09:59:17 +01:00
|
|
|
type AST = [Description]
|
2019-04-23 02:44:35 +02:00
|
|
|
|
|
|
|
|
exprToLHS :: Expr -> Maybe LHS
|
|
|
|
|
exprToLHS (Ident x ) = Just $ LHSIdent x
|
|
|
|
|
exprToLHS (Bit l e ) = do
|
|
|
|
|
l' <- exprToLHS l
|
|
|
|
|
Just $ LHSBit l' e
|
|
|
|
|
exprToLHS (Range l m r) = do
|
|
|
|
|
l' <- exprToLHS l
|
|
|
|
|
Just $ LHSRange l' m r
|
|
|
|
|
exprToLHS (Dot l x ) = do
|
|
|
|
|
l' <- exprToLHS l
|
|
|
|
|
Just $ LHSDot l' x
|
|
|
|
|
exprToLHS (Concat ls ) = do
|
|
|
|
|
ls' <- mapM exprToLHS ls
|
|
|
|
|
Just $ LHSConcat ls'
|
2019-09-03 02:46:35 +02:00
|
|
|
exprToLHS (Stream o e ls) = do
|
|
|
|
|
ls' <- mapM exprToLHS ls
|
|
|
|
|
Just $ LHSStream o e ls'
|
2019-04-23 02:44:35 +02:00
|
|
|
exprToLHS _ = Nothing
|
2019-09-04 05:36:29 +02:00
|
|
|
|
|
|
|
|
lhsToExpr :: LHS -> Expr
|
|
|
|
|
lhsToExpr (LHSIdent x ) = Ident x
|
|
|
|
|
lhsToExpr (LHSBit l e ) = Bit (lhsToExpr l) e
|
|
|
|
|
lhsToExpr (LHSRange l m r ) = Range (lhsToExpr l) m r
|
|
|
|
|
lhsToExpr (LHSDot l x ) = Dot (lhsToExpr l) x
|
|
|
|
|
lhsToExpr (LHSConcat ls) = Concat $ map lhsToExpr ls
|
|
|
|
|
lhsToExpr (LHSStream o e ls) = Stream o e $ map lhsToExpr ls
|
2019-09-04 05:52:22 +02:00
|
|
|
|
|
|
|
|
shortHash :: (Show a) => a -> String
|
|
|
|
|
shortHash x =
|
|
|
|
|
take 5 $ printf "%05X" val
|
|
|
|
|
where val = hash $ show x
|