AST.hs 2.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
{- 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.
 -}

16
module Language.SystemVerilog.AST
17
    ( AST
18
    , module Attr
19
    , module Decl
20
    , module Description
21
    , module Expr
22
    , module GenItem
23
    , module LHS
24
    , module ModuleItem
25
    , module Op
26
    , module Stmt
27
    , module Type
28
    , exprToLHS
29
    , lhsToExpr
30
    , shortHash
31 32
    ) where

33 34 35
import Text.Printf (printf)
import Data.Hashable (hash)

36
import Language.SystemVerilog.AST.Attr as Attr
37
import Language.SystemVerilog.AST.Decl as Decl
38
import Language.SystemVerilog.AST.Description as Description
39
import Language.SystemVerilog.AST.Expr as Expr
40
import Language.SystemVerilog.AST.GenItem as GenItem
41
import Language.SystemVerilog.AST.LHS as LHS
42
import Language.SystemVerilog.AST.ModuleItem as ModuleItem
43
import Language.SystemVerilog.AST.Op as Op
44
import Language.SystemVerilog.AST.Stmt as Stmt
45 46
import Language.SystemVerilog.AST.Type as Type

47
type AST = [Description]
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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'
63 64 65
exprToLHS (Stream o e ls) = do
    ls' <- mapM exprToLHS ls
    Just $ LHSStream o e ls'
66
exprToLHS _ = Nothing
67 68 69 70 71 72 73 74

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
75 76 77 78 79

shortHash :: (Show a) => a -> String
shortHash x =
    take 5 $ printf "%05X" val
    where val = hash $ show x