conversion for block decls with asignments

This commit is contained in:
Zachary Snow 2019-09-02 13:08:41 -04:00
parent bafe7e4396
commit d6c932d0fc
5 changed files with 69 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import qualified Convert.AlwaysKW
import qualified Convert.AsgnOp import qualified Convert.AsgnOp
import qualified Convert.Assertion import qualified Convert.Assertion
import qualified Convert.Bits import qualified Convert.Bits
import qualified Convert.BlockDecl
import qualified Convert.EmptyArgs import qualified Convert.EmptyArgs
import qualified Convert.Enum import qualified Convert.Enum
import qualified Convert.ForDecl import qualified Convert.ForDecl
@ -43,6 +44,7 @@ phases excludes =
, Convert.NamedBlock.convert , Convert.NamedBlock.convert
, Convert.Assertion.convert , Convert.Assertion.convert
, Convert.Bits.convert , Convert.Bits.convert
, Convert.BlockDecl.convert
, selectExclude (Job.Logic , Convert.Logic.convert) , selectExclude (Job.Logic , Convert.Logic.convert)
, Convert.ForDecl.convert , Convert.ForDecl.convert
, Convert.FuncRet.convert , Convert.FuncRet.convert

38
src/Convert/BlockDecl.hs Normal file
View File

@ -0,0 +1,38 @@
{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
-
- Verilog-2005 forbids block declarations with default values. We convert
- these assignments to separate statements. If we handle static lifetimes in
- the future, this conversion may have to change.
-}
module Convert.BlockDecl (convert) where
import Data.Maybe (mapMaybe)
import Convert.Traverse
import Language.SystemVerilog.AST
convert :: [AST] -> [AST]
convert =
map
$ traverseDescriptions $ traverseModuleItems
$ traverseStmts $ convertStmt
convertStmt :: Stmt -> Stmt
convertStmt (Block name decls stmts) =
Block name decls' stmts'
where
splitDecls = map splitDecl decls
decls' = map fst splitDecls
asgns = map asgnStmt $ mapMaybe snd splitDecls
stmts' = asgns ++ stmts
convertStmt other = other
splitDecl :: Decl -> (Decl, Maybe (LHS, Expr))
splitDecl (Variable d t ident a (Just e)) =
(Variable d t ident a Nothing, Just (LHSIdent ident, e))
splitDecl other = (other, Nothing)
asgnStmt :: (LHS, Expr) -> Stmt
asgnStmt = uncurry $ AsgnBlk AsgnOpEq

View File

@ -57,6 +57,7 @@ executable sv2v
Convert.AsgnOp Convert.AsgnOp
Convert.Assertion Convert.Assertion
Convert.Bits Convert.Bits
Convert.BlockDecl
Convert.EmptyArgs Convert.EmptyArgs
Convert.Enum Convert.Enum
Convert.ForDecl Convert.ForDecl

View File

@ -34,4 +34,19 @@ module top;
end end
end end
initial begin
integer i = 0;
for (; i < 32; i++)
$display("6: ", ~a[i]);
end
initial begin
integer j = 0, k;
for (; j < 4; j++) begin
k = 0;
for (; k < 8; k++)
$display("7: ", ~a[j * 8 + k] + 11);
end
end
endmodule endmodule

View File

@ -42,4 +42,17 @@ module top;
$display("5: ", ~a[j * 8 + k] + 11); $display("5: ", ~a[j * 8 + k] + 11);
end end
initial begin : foo_6
integer i;
for (i = 0; i < 32; i = i + 1)
$display("6: ", ~a[i]);
end
initial begin : foo_7
integer j, k;
for (j = 0; j < 4; j++)
for (k = 0; k < 8; k++)
$display("7: ", ~a[j * 8 + k] + 11);
end
endmodule endmodule