module Language.Netlist.AST where
import Data.Binary ( Binary(..), putWord8, getWord8 )
import Data.Generics ( Data, Typeable )
data Module = Module
{ module_name :: Ident
, module_inputs :: [(Ident, Maybe Range)]
, module_outputs :: [(Ident, Maybe Range)]
, module_statics :: [(Ident, ConstExpr)]
, module_decls :: [Decl]
}
deriving (Eq, Ord, Show, Data, Typeable)
type Ident = String
type Size = Int
data Decl
= NetDecl Ident (Maybe Range) (Maybe Expr)
| NetAssign Ident Expr
| MemDecl Ident (Maybe Range) (Maybe Range) (Maybe [Expr])
| MemAssign Ident Expr Expr
| InstDecl Ident
Ident
[(Ident, Expr)]
[(Ident, Expr)]
[(Ident, Expr)]
| ProcessDecl Event (Maybe (Event, Stmt)) Stmt
| InitProcessDecl Stmt
| CommentDecl String
deriving (Eq, Ord, Show, Data, Typeable)
data Range
= Range ConstExpr ConstExpr
deriving (Eq, Ord, Show, Data, Typeable)
type ConstExpr = Expr
data Event
= Event Expr Edge
deriving (Eq, Ord, Show, Data, Typeable)
data Edge
= PosEdge
| NegEdge
deriving (Eq, Ord, Show, Data, Typeable)
data Expr
= ExprLit (Maybe Size) ExprLit
| ExprVar Ident
| ExprString String
| ExprIndex Ident Expr
| ExprSlice Ident Expr Expr
| ExprSliceOff Ident Expr Int
| ExprCase Expr [([ConstExpr], Expr)] (Maybe Expr)
| ExprConcat [Expr]
| ExprCond Expr Expr Expr
| ExprUnary UnaryOp Expr
| ExprBinary BinaryOp Expr Expr
| ExprFunCall Ident [Expr]
deriving (Eq, Ord, Show, Data, Typeable)
data ExprLit
= ExprNum Integer
| ExprBit Bit
| ExprBitVector [Bit]
deriving (Eq, Ord, Show, Data, Typeable)
data Bit
= T | F | U | Z
deriving (Eq, Ord, Show, Data, Typeable)
data Stmt
= Assign LValue Expr
| If Expr Stmt (Maybe Stmt)
| Case Expr [([Expr], Stmt)] (Maybe Stmt)
| Seq [Stmt]
| FunCallStmt Ident [Expr]
deriving (Eq, Ord, Show, Data, Typeable)
type LValue = Expr
data UnaryOp
= UPlus | UMinus | LNeg | Neg | UAnd | UNand | UOr | UNor | UXor | UXnor
deriving (Eq, Ord, Show, Data, Typeable)
data BinaryOp
= Pow | Plus | Minus | Times | Divide | Modulo
| Equals | NotEquals
| CEquals | CNotEquals
| LAnd | LOr
| LessThan | LessEqual | GreaterThan | GreaterEqual
| And | Nand | Or | Nor | Xor | Xnor
| ShiftLeft | ShiftRight | RotateLeft | RotateRight
| ShiftLeftArith | ShiftRightArith
deriving (Eq, Ord, Show, Data, Typeable)
instance Binary Module where
put (Module x1 x2 x3 x4 x5)
= do put x1
put x2
put x3
put x4
put x5
get
= do x1 <- get
x2 <- get
x3 <- get
x4 <- get
x5 <- get
return (Module x1 x2 x3 x4 x5)
instance Binary Decl where
put x
= case x of
NetDecl x1 x2 x3 -> do putWord8 0
put x1
put x2
put x3
NetAssign x1 x2 -> do putWord8 1
put x1
put x2
MemDecl x1 x2 x3 x4 -> do putWord8 2
put x1
put x2
put x3
put x4
MemAssign x1 x2 x3 -> do putWord8 3
put x1
put x2
put x3
InstDecl x1 x2 x3 x4 x5 -> do putWord8 4
put x1
put x2
put x3
put x4
put x5
ProcessDecl x1 x2 x3 -> do putWord8 5
put x1
put x2
put x3
InitProcessDecl x1 -> do putWord8 6
put x1
CommentDecl x1 -> do putWord8 7
put x1
get
= do i <- getWord8
case i of
0 -> do x1 <- get
x2 <- get
x3 <- get
return (NetDecl x1 x2 x3)
1 -> do x1 <- get
x2 <- get
return (NetAssign x1 x2)
2 -> do x1 <- get
x2 <- get
x3 <- get
x4 <- get
return (MemDecl x1 x2 x3 x4)
3 -> do x1 <- get
x2 <- get
x3 <- get
return (MemAssign x1 x2 x3)
4 -> do x1 <- get
x2 <- get
x3 <- get
x4 <- get
x5 <- get
return (InstDecl x1 x2 x3 x4 x5)
5 -> do x1 <- get
x2 <- get
x3 <- get
return (ProcessDecl x1 x2 x3)
6 -> do x1 <- get
return (InitProcessDecl x1)
7 -> do x1 <- get
return (CommentDecl x1)
_ -> error "Corrupted binary data for Decl"
instance Binary Range where
put (Range x1 x2)
= do put x1
put x2
get
= do x1 <- get
x2 <- get
return (Range x1 x2)
instance Binary Event where
put (Event x1 x2)
= do put x1
put x2
get
= do x1 <- get
x2 <- get
return (Event x1 x2)
instance Binary Edge where
put x
= case x of
PosEdge -> putWord8 0
NegEdge -> putWord8 1
get
= do i <- getWord8
case i of
0 -> return PosEdge
1 -> return NegEdge
_ -> error "Corrupted binary data for Edge"
instance Binary Expr where
put x
= case x of
ExprLit x1 x2 -> do putWord8 0
put x1
put x2
ExprVar x1 -> do putWord8 1
put x1
ExprString x1 -> do putWord8 2
put x1
ExprIndex x1 x2 -> do putWord8 3
put x1
put x2
ExprSlice x1 x2 x3 -> do putWord8 4
put x1
put x2
put x3
ExprSliceOff x1 x2 x3 -> do putWord8 5
put x1
put x2
put x3
ExprCase x1 x2 x3 -> do putWord8 6
put x1
put x2
put x3
ExprConcat x1 -> do putWord8 7
put x1
ExprCond x1 x2 x3 -> do putWord8 8
put x1
put x2
put x3
ExprUnary x1 x2 -> do putWord8 9
put x1
put x2
ExprBinary x1 x2 x3 -> do putWord8 10
put x1
put x2
put x3
ExprFunCall x1 x2 -> do putWord8 11
put x1
put x2
get
= do i <- getWord8
case i of
0 -> do x1 <- get
x2 <- get
return (ExprLit x1 x2)
1 -> do x1 <- get
return (ExprVar x1)
2 -> do x1 <- get
return (ExprString x1)
3 -> do x1 <- get
x2 <- get
return (ExprIndex x1 x2)
4 -> do x1 <- get
x2 <- get
x3 <- get
return (ExprSlice x1 x2 x3)
5 -> do x1 <- get
x2 <- get
x3 <- get
return (ExprSliceOff x1 x2 x3)
6 -> do x1 <- get
x2 <- get
x3 <- get
return (ExprCase x1 x2 x3)
7 -> do x1 <- get
return (ExprConcat x1)
8 -> do x1 <- get
x2 <- get
x3 <- get
return (ExprCond x1 x2 x3)
9 -> do x1 <- get
x2 <- get
return (ExprUnary x1 x2)
10 -> do x1 <- get
x2 <- get
x3 <- get
return (ExprBinary x1 x2 x3)
11 -> do x1 <- get
x2 <- get
return (ExprFunCall x1 x2)
_ -> error "Corrupted binary data for Expr"
instance Binary ExprLit where
put x
= case x of
ExprNum x1 -> do putWord8 0
put x1
ExprBit x1 -> do putWord8 1
put x1
ExprBitVector x1 -> do putWord8 2
put x1
get
= do i <- getWord8
case i of
0 -> do x1 <- get
return (ExprNum x1)
1 -> do x1 <- get
return (ExprBit x1)
2 -> do x1 <- get
return (ExprBitVector x1)
_ -> error "Corrupted binary data for ExprLit"
instance Binary Bit where
put x
= case x of
T -> putWord8 0
F -> putWord8 1
U -> putWord8 2
Z -> putWord8 3
get
= do i <- getWord8
case i of
0 -> return T
1 -> return F
2 -> return U
3 -> return Z
_ -> error "Corrupted binary data for Bit"
instance Binary Stmt where
put x
= case x of
Assign x1 x2 -> do putWord8 0
put x1
put x2
If x1 x2 x3 -> do putWord8 1
put x1
put x2
put x3
Case x1 x2 x3 -> do putWord8 2
put x1
put x2
put x3
Seq x1 -> do putWord8 3
put x1
FunCallStmt x1 x2 -> do putWord8 4
put x1
put x2
get
= do i <- getWord8
case i of
0 -> do x1 <- get
x2 <- get
return (Assign x1 x2)
1 -> do x1 <- get
x2 <- get
x3 <- get
return (If x1 x2 x3)
2 -> do x1 <- get
x2 <- get
x3 <- get
return (Case x1 x2 x3)
3 -> do x1 <- get
return (Seq x1)
4 -> do x1 <- get
x2 <- get
return (FunCallStmt x1 x2)
_ -> error "Corrupted binary data for Stmt"
instance Binary UnaryOp where
put x
= case x of
UPlus -> putWord8 0
UMinus -> putWord8 1
LNeg -> putWord8 2
Neg -> putWord8 3
UAnd -> putWord8 4
UNand -> putWord8 5
UOr -> putWord8 6
UNor -> putWord8 7
UXor -> putWord8 8
UXnor -> putWord8 9
get
= do i <- getWord8
case i of
0 -> return UPlus
1 -> return UMinus
2 -> return LNeg
3 -> return Neg
4 -> return UAnd
5 -> return UNand
6 -> return UOr
7 -> return UNor
8 -> return UXor
9 -> return UXnor
_ -> error "Corrupted binary data for UnaryOp"
instance Binary BinaryOp where
put x
= case x of
Pow -> putWord8 0
Plus -> putWord8 1
Minus -> putWord8 2
Times -> putWord8 3
Divide -> putWord8 4
Modulo -> putWord8 5
Equals -> putWord8 6
NotEquals -> putWord8 7
CEquals -> putWord8 8
CNotEquals -> putWord8 9
LAnd -> putWord8 10
LOr -> putWord8 11
LessThan -> putWord8 12
LessEqual -> putWord8 13
GreaterThan -> putWord8 14
GreaterEqual -> putWord8 15
And -> putWord8 16
Nand -> putWord8 17
Or -> putWord8 18
Nor -> putWord8 19
Xor -> putWord8 20
Xnor -> putWord8 21
ShiftLeft -> putWord8 22
ShiftRight -> putWord8 23
RotateLeft -> putWord8 24
RotateRight -> putWord8 25
ShiftLeftArith -> putWord8 26
ShiftRightArith -> putWord8 27
get
= do i <- getWord8
case i of
0 -> return Pow
1 -> return Plus
2 -> return Minus
3 -> return Times
4 -> return Divide
5 -> return Modulo
6 -> return Equals
7 -> return NotEquals
8 -> return CEquals
9 -> return CNotEquals
10 -> return LAnd
11 -> return LOr
12 -> return LessThan
13 -> return LessEqual
14 -> return GreaterThan
15 -> return GreaterEqual
16 -> return And
17 -> return Nand
18 -> return Or
19 -> return Nor
20 -> return Xor
21 -> return Xnor
22 -> return ShiftLeft
23 -> return ShiftRight
24 -> return RotateLeft
25 -> return RotateRight
26 -> return ShiftLeftArith
27 -> return ShiftRightArith
_ -> error "Corrupted binary data for BinaryOp"