support and conversion for -> and <->

This commit is contained in:
Zachary Snow 2019-09-15 13:55:40 -04:00
parent c0e38f793d
commit 2ca8a022ad
9 changed files with 75 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import qualified Convert.Interface
import qualified Convert.IntTypes import qualified Convert.IntTypes
import qualified Convert.KWArgs import qualified Convert.KWArgs
import qualified Convert.Logic import qualified Convert.Logic
import qualified Convert.LogOp
import qualified Convert.NamedBlock import qualified Convert.NamedBlock
import qualified Convert.NestPI import qualified Convert.NestPI
import qualified Convert.Package import qualified Convert.Package
@ -52,6 +53,7 @@ phases excludes =
, Convert.EmptyArgs.convert , Convert.EmptyArgs.convert
, Convert.IntTypes.convert , Convert.IntTypes.convert
, Convert.KWArgs.convert , Convert.KWArgs.convert
, Convert.LogOp.convert
, Convert.PackedArray.convert , Convert.PackedArray.convert
, Convert.DimensionQuery.convert , Convert.DimensionQuery.convert
, Convert.ParamType.convert , Convert.ParamType.convert

28
src/Convert/LogOp.hs Normal file
View File

@ -0,0 +1,28 @@
{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
-
- Conversion for logical implication (->) and logical equality (<->) operators.
-
- We convert `a -> b` to `!a || b`, as per the definition of implication.
-
- We convert `a <-> b` to `!a = !b`. Note that we can't simply use `a = b` as
- `1 != 2`, but `1 <-> 2`.
-}
module Convert.LogOp (convert) where
import Convert.Traverse
import Language.SystemVerilog.AST
convert :: [AST] -> [AST]
convert =
map $
traverseDescriptions $ traverseModuleItems $
traverseExprs $ traverseNestedExprs convertExpr
convertExpr :: Expr -> Expr
convertExpr (BinOp LogEq a b) =
BinOp Eq (UniOp LogNot a) (UniOp LogNot b)
convertExpr (BinOp LogImp a b) =
BinOp LogOr (UniOp LogNot a) b
convertExpr other = other

View File

@ -40,6 +40,8 @@ instance Show UniOp where
data BinOp data BinOp
= LogAnd = LogAnd
| LogOr | LogOr
| LogImp
| LogEq
| BitAnd | BitAnd
| BitXor | BitXor
| BitXnor | BitXnor
@ -69,6 +71,8 @@ data BinOp
instance Show BinOp where instance Show BinOp where
show LogAnd = "&&" show LogAnd = "&&"
show LogOr = "||" show LogOr = "||"
show LogImp = "->"
show LogEq = "<->"
show BitAnd = "&" show BitAnd = "&"
show BitXor = "^" show BitXor = "^"
show BitXnor = "~^" show BitXnor = "~^"

View File

@ -458,6 +458,7 @@ tokens :-
"<<<" { tok Sym_lt_lt_lt } "<<<" { tok Sym_lt_lt_lt }
"<<=" { tok Sym_lt_lt_eq } "<<=" { tok Sym_lt_lt_eq }
">>=" { tok Sym_gt_gt_eq } ">>=" { tok Sym_gt_gt_eq }
"<->" { tok Sym_lt_dash_gt }
"|->" { tok Sym_bar_dash_gt } "|->" { tok Sym_bar_dash_gt }
"|=>" { tok Sym_bar_eq_gt } "|=>" { tok Sym_bar_eq_gt }
"[->" { tok Sym_brack_l_dash_gt } "[->" { tok Sym_brack_l_dash_gt }

View File

@ -366,6 +366,7 @@ time { Token Lit_time _ _ }
"<<<" { Token Sym_lt_lt_lt _ _ } "<<<" { Token Sym_lt_lt_lt _ _ }
"<<=" { Token Sym_lt_lt_eq _ _ } "<<=" { Token Sym_lt_lt_eq _ _ }
">>=" { Token Sym_gt_gt_eq _ _ } ">>=" { Token Sym_gt_gt_eq _ _ }
"<->" { Token Sym_lt_dash_gt _ _ }
"|->" { Token Sym_bar_dash_gt _ _ } "|->" { Token Sym_bar_dash_gt _ _ }
"|=>" { Token Sym_bar_eq_gt _ _ } "|=>" { Token Sym_bar_eq_gt _ _ }
"[->" { Token Sym_brack_l_dash_gt _ _ } "[->" { Token Sym_brack_l_dash_gt _ _ }
@ -391,6 +392,7 @@ time { Token Lit_time _ _ }
%right "throughout" %right "throughout"
%left "##" %left "##"
%nonassoc "[*]" "[=]" "[->]" %nonassoc "[*]" "[=]" "[->]"
%right "->" "<->"
%right "?" ":" %right "?" ":"
%left "||" %left "||"
%left "&&" %left "&&"
@ -1042,6 +1044,8 @@ Expr :: { Expr }
-- binary expressions -- binary expressions
| Expr "||" Expr { BinOp LogOr $1 $3 } | Expr "||" Expr { BinOp LogOr $1 $3 }
| Expr "&&" Expr { BinOp LogAnd $1 $3 } | Expr "&&" Expr { BinOp LogAnd $1 $3 }
| Expr "->" Expr { BinOp LogImp $1 $3 }
| Expr "<->" Expr { BinOp LogEq $1 $3 }
| Expr "|" Expr { BinOp BitOr $1 $3 } | Expr "|" Expr { BinOp BitOr $1 $3 }
| Expr "^" Expr { BinOp BitXor $1 $3 } | Expr "^" Expr { BinOp BitXor $1 $3 }
| Expr "&" Expr { BinOp BitAnd $1 $3 } | Expr "&" Expr { BinOp BitAnd $1 $3 }

View File

@ -366,6 +366,7 @@ data TokenName
| Sym_lt_lt_lt | Sym_lt_lt_lt
| Sym_lt_lt_eq | Sym_lt_lt_eq
| Sym_gt_gt_eq | Sym_gt_gt_eq
| Sym_lt_dash_gt
| Sym_bar_dash_gt | Sym_bar_dash_gt
| Sym_bar_eq_gt | Sym_bar_eq_gt
| Sym_pound_dash_pound | Sym_pound_dash_pound

View File

@ -67,6 +67,7 @@ executable sv2v
Convert.IntTypes Convert.IntTypes
Convert.KWArgs Convert.KWArgs
Convert.Logic Convert.Logic
Convert.LogOp
Convert.NamedBlock Convert.NamedBlock
Convert.NestPI Convert.NestPI
Convert.Package Convert.Package

16
test/basic/log_op.sv Normal file
View File

@ -0,0 +1,16 @@
module top;
function log_imp;
input integer a;
input integer b;
return a -> b;
endfunction
function log_eq;
input integer a;
input integer b;
return a <-> b;
endfunction
initial
for (integer a = -2; a <= 2; a++)
for (integer b = -2; b <= 2; b++)
$display(log_imp(a, b), log_eq(a, b));
endmodule

18
test/basic/log_op.v Normal file
View File

@ -0,0 +1,18 @@
module top;
function log_imp;
input integer a;
input integer b;
log_imp = !a || b;
endfunction
function log_eq;
input integer a;
input integer b;
log_eq = !a == !b;
endfunction
initial begin : foo
integer a, b;
for (a = -2; a <= 2; a = a + 1)
for (b = -2; b <= 2; b = b + 1)
$display(log_imp(a, b), log_eq(a, b));
end
endmodule