simplified SeverityTask.hs a bit further

This commit is contained in:
Zachary Snow 2024-06-09 18:50:39 -04:00
parent 8c41d049e5
commit 1bb76cbe32
6 changed files with 30 additions and 78 deletions

View File

@ -5,10 +5,7 @@
- `$error`, and `$fatal` (20.10 and 20.11).
-
- 1. Severity task messages are converted into `$display` tasks.
- 2. Standard `$fatal` tasks run `$finish` directly after running `$display`.
- 3. Elaboration `$fatal` tasks set `_sv2v_elaboration_fatal` to a non-negative
- value, causing the simulation to exit once all elaboration severity task
- messages have been printed.
- 2. `$fatal` tasks run `$finish` directly after running `$display`.
-}
module Convert.SeverityTask (convert) where
@ -19,90 +16,39 @@ import Convert.Traverse
import Language.SystemVerilog.AST
convert :: [AST] -> [AST]
convert = map $ traverseDescriptions traverseDescription
-- Identifiers for fatal elaboration checker
elaborationFatalIdent :: Identifier
elaborationFatalIdent = "_sv2v_elaboration_fatal"
elaborationFatalCancelCode :: Expr
elaborationFatalCancelCode = UniOp UniSub $ RawNum 1
-- Checker for fatal elaboration
elaborationFatalDecl :: ModuleItem
elaborationFatalDecl =
MIPackageItem $ Decl $
Variable Local t elaborationFatalIdent [] elaborationFatalCancelCode
where t = IntegerAtom TInteger Unspecified
elaborationFatalCheck :: ModuleItem
elaborationFatalCheck =
Initial $ Block Seq "" []
[ zeroDelay
, If NoCheck checkCancelCode finishCall Null
]
where
zeroDelay = Timing (Delay $ RawNum 0) Null
checkCancelCode = BinOpA Ne [] (Ident elaborationFatalIdent) elaborationFatalCancelCode
finishCall = call "$finish" [Ident elaborationFatalIdent]
-- If SV code uses elab fatal, add checker for fatal elaboration
traverseDescription :: Description -> Description
traverseDescription (Part att ext kw lif name pts items) =
traverseModuleItems convertModuleItem $
Part att ext kw lif name pts $
if hasElaborationFatal
then elaborationFatalDecl : items ++ [elaborationFatalCheck]
else items
where
hasElaborationFatal = any isElabTaskFatal items
isElabTaskFatal (ElabTask SeverityFatal _) = True
isElabTaskFatal _ = False
traverseDescription description = traverseModuleItems convertModuleItem description
severityToString :: Severity -> String
severityToString severity = toUpper ch : str
where '$' : ch : str = show severity
convert = map $ traverseDescriptions $ traverseModuleItems convertModuleItem
-- Convert Elaboration Severity Tasks
convertModuleItem :: ModuleItem -> ModuleItem
convertModuleItem (ElabTask severity taskArgs) =
Initial $ Block Seq "" [] [stmtDisplay, stmtFinish]
where
stmtDisplay = call "$display" $ msg : args
args = if severity /= SeverityFatal || null taskArgs
then taskArgs
else tail taskArgs
msg = String $ "Elaboration " ++ severityToString severity ++ ':' : trailingSpace
trailingSpace = if length args > 0 then " " else ""
stmtFinish = if severity /= SeverityFatal
then Null
else Asgn AsgnOpEq Nothing (LHSIdent elaborationFatalIdent) finishArg
finishArg = if null taskArgs then RawNum 0 else head taskArgs
Initial $ elab severity taskArgs "Elaboration" []
convertModuleItem other =
traverseStmts (traverseNestedStmts convertStmt) other
timeExpr :: Expr
timeExpr = Ident "$time"
-- Convert Standard Severity Tasks
convertStmt :: Stmt -> Stmt
convertStmt (SeverityStmt severity taskArgs) =
elab severity taskArgs "[%0t]" [Ident "$time"]
convertStmt other = other
elab :: Severity -> [Expr] -> String -> [Expr] -> Stmt
elab severity taskArgs prefixStr prefixArgs =
Block Seq "" [] [stmtDisplay, stmtFinish]
where
stmtDisplay = call "$display" $ [msg, timeExpr] ++ args
msg = String $ "[%0t] " ++ severityToString severity ++ ':' : trailingSpace
trailingSpace = if length args > 0 then " " else ""
stmtDisplay = call "$display" $ msg : prefixArgs ++ args
msg = String $ prefixStr ++ ' ' : severityToString severity ++ ':' : trailingSpace
trailingSpace = if null args then "" else " "
args = if severity /= SeverityFatal || null taskArgs
then taskArgs
else tail taskArgs
stmtFinish = if severity /= SeverityFatal
then Null
else call "$finish" argsFinish
argsFinish = if null taskArgs then [] else [head taskArgs]
convertStmt other = other
call :: Identifier -> [Expr] -> Stmt
call func args = Subroutine (Ident func) (Args args [])
severityToString :: Severity -> String
severityToString severity = toUpper ch : str
where '$' : ch : str = show severity

View File

@ -1,5 +1,4 @@
module top;
integer _sv2v_elaboration_fatal = -1;
initial $display("Elaboration Info:");
initial $display("Elaboration Info: ", "%b", 1);
initial $display("Elaboration Warning:");
@ -8,19 +7,14 @@ module top;
initial $display("Elaboration Error: ", "%b", 3);
initial begin
$display("Elaboration Fatal:");
_sv2v_elaboration_fatal = 0;
$finish;
end
initial begin
$display("Elaboration Fatal:");
_sv2v_elaboration_fatal = 0;
$finish(0);
end
initial begin
$display("Elaboration Fatal: ", "%b", 4);
_sv2v_elaboration_fatal = 0;
end
initial begin
#0;
if (_sv2v_elaboration_fatal != -1)
$finish(_sv2v_elaboration_fatal);
$finish(0);
end
endmodule

View File

@ -0,0 +1,4 @@
affirm $finish;
affirm $display("Elaboration Fatal:");
affirm $finish(0);
affirm $display("Elaboration Fatal: ", "%b", 4);

View File

@ -8,5 +8,9 @@ module top;
$display("[%0t] Error: ", $time, "%b", 3);
$display("[%0t] Fatal:", $time);
$finish;
$display("[%0t] Fatal:", $time);
$finish(0);
$display("[%0t] Fatal: ", $time, "%b", 4);
$finish(0);
end
endmodule

View File

@ -0,0 +1,4 @@
affirm $finish;
affirm $display("[%0t] Fatal:", $time);
affirm $finish(0);
affirm $display("[%0t] Fatal: ", $time, "%b", 4);