Commit 4ced649a by Zachary Snow

convert do while loops

parent 1315bed8
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
* Added support for enumerated type ranges (e.g., `enum { X[3:5] }`) * Added support for enumerated type ranges (e.g., `enum { X[3:5] }`)
* Added support for the SystemVerilog `edge` event * Added support for the SystemVerilog `edge` event
* Added support for cycle delay ranges in assertion sequence expressions * Added support for cycle delay ranges in assertion sequence expressions
* Added conversion for `do` `while` loops
* Added support for passing through DPI imports and exports * Added support for passing through DPI imports and exports
* Added support for passing through functions with output ports * Added support for passing through functions with output ports
......
...@@ -17,6 +17,7 @@ import qualified Convert.Assertion ...@@ -17,6 +17,7 @@ import qualified Convert.Assertion
import qualified Convert.BlockDecl import qualified Convert.BlockDecl
import qualified Convert.Cast import qualified Convert.Cast
import qualified Convert.DimensionQuery import qualified Convert.DimensionQuery
import qualified Convert.DoWhile
import qualified Convert.DuplicateGenvar import qualified Convert.DuplicateGenvar
import qualified Convert.EmptyArgs import qualified Convert.EmptyArgs
import qualified Convert.Enum import qualified Convert.Enum
...@@ -105,6 +106,7 @@ initialPhases selectExclude = ...@@ -105,6 +106,7 @@ initialPhases selectExclude =
, Convert.SenseEdge.convert , Convert.SenseEdge.convert
, Convert.LogOp.convert , Convert.LogOp.convert
, Convert.EmptyArgs.convert , Convert.EmptyArgs.convert
, Convert.DoWhile.convert
, Convert.Foreach.convert , Convert.Foreach.convert
, Convert.FuncRoutine.convert , Convert.FuncRoutine.convert
, selectExclude Job.Assert Convert.Assertion.convert , selectExclude Job.Assert Convert.Assertion.convert
......
{- sv2v
- Author: Zachary Snow <zach@zachjs.com>
-
- Conversion for `do` `while` loops.
-
- These are converted into while loops with an extra condition which is
- initially true and immediately set to false in the body. This strategy is
- preferrable to simply duplicating the loop body as it could contain jumps.
-}
module Convert.DoWhile (convert) where
import Convert.Traverse
import Language.SystemVerilog.AST
convert :: [AST] -> [AST]
convert =
map $ traverseDescriptions $ traverseModuleItems $
traverseStmts $ traverseNestedStmts convertStmt
convertStmt :: Stmt -> Stmt
convertStmt (DoWhile cond body) =
Block Seq "" [decl] [While cond' body']
where
ident = "sv2v_do_while"
typ = IntegerVector TLogic Unspecified []
decl = Variable Local typ ident [] (RawNum 1)
cond' = BinOp LogOr (Ident ident) cond
asgn = Asgn AsgnOpEq Nothing (LHSIdent ident) (RawNum 0)
body' = Block Seq "" [] [asgn, body]
convertStmt other = other
...@@ -208,9 +208,6 @@ convertStmt (For inits comp incr stmt) = ...@@ -208,9 +208,6 @@ convertStmt (For inits comp incr stmt) =
convertStmt (While comp stmt) = convertStmt (While comp stmt) =
convertLoop Nothing loop comp [] stmt convertLoop Nothing loop comp [] stmt
where loop c _ s = While c s where loop c _ s = While c s
convertStmt (DoWhile comp stmt) =
convertLoop Nothing loop comp [] stmt
where loop c _ s = DoWhile c s
convertStmt (Continue) = do convertStmt (Continue) = do
loopDepth <- gets sLoopDepth loopDepth <- gets sLoopDepth
......
...@@ -68,6 +68,7 @@ executable sv2v ...@@ -68,6 +68,7 @@ executable sv2v
Convert.BlockDecl Convert.BlockDecl
Convert.Cast Convert.Cast
Convert.DimensionQuery Convert.DimensionQuery
Convert.DoWhile
Convert.DuplicateGenvar Convert.DuplicateGenvar
Convert.EmptyArgs Convert.EmptyArgs
Convert.Enum Convert.Enum
......
module top;
integer x = 0;
initial
do begin
$display("hi %0d", x);
x++;
if (x == 2)
continue;
$display("step");
end while (0 < x && x < 3);
endmodule
module top;
integer x = 0;
initial
while (x < 3) begin
$display("hi %0d", x);
x++;
if (x != 2)
$display("step");
end
endmodule
module top;
initial do
$display("hi");
while (0);
endmodule
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment