Skip to content

Commit a23c084

Browse files
committed
Add braces to some if-stmts in generated Bluesim C++
to avoid warnings about ambiguous 'else'
1 parent 388a61e commit a23c084

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

src/comp/CCSyntax.hs

+13-3
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,20 @@ instance PPrint CCFragment where
675675
pPrint d p (CPPInclude file True) = text ("#include <" ++ file ++ ">")
676676
pPrint d p (CPPInclude file False) = text ("#include \"" ++ file ++ "\"")
677677
pPrint d p (CIf c th Nothing) =
678-
(text "if (") <> (pp c) <> (text ")") $+$ (printClauseOrBlock th)
678+
-- If the true-arm is a nested if-stmt with an else-clause,
679+
-- braces are needed to avoid ambiguous 'else'
680+
let th' = case th of
681+
CIf _ _ (Just _) -> CBlock [th]
682+
_ -> th
683+
in (text "if (") <> (pp c) <> (text ")") $+$ (printClauseOrBlock th')
679684
pPrint d p (CIf c th (Just el)) =
680-
(text "if (") <> (pp c) <> (text ")") $+$
681-
(printClauseOrBlock th) $+$ (text "else") $+$ (printClauseOrBlock el)
685+
-- If the true-arm is a nested if-stmt without an else-clause,
686+
-- parentheses are needed to avoid ambiguous 'else'
687+
let th' = case th of
688+
CIf _ _ Nothing -> CBlock [th]
689+
_ -> th
690+
in (text "if (") <> (pp c) <> (text ")") $+$
691+
(printClauseOrBlock th') $+$ (text "else") $+$ (printClauseOrBlock el)
682692
pPrint d p (CSwitch idx arms deflt) =
683693
let ppArm (n, blk) = (text "case") <+> (pp n) <> (text ":") $+$
684694
nest 2 (vsep (map printStmt blk))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
3+
ActionValue #(Bit #(32)) cur_cycle = actionvalue
4+
Bit #(32) t <- $stime;
5+
if (genVerilog)
6+
t = t + 5;
7+
return t / 10;
8+
endactionvalue;
9+
10+
(* synthesize *)
11+
module sysNestedIfBraces ();
12+
13+
Reg #(Bit #(4)) cfg_verbosity <- mkReg (1);
14+
15+
Reg #(Bool) rg_state <- mkReg (False);
16+
17+
rule rl_display (! rg_state);
18+
if (cfg_verbosity != 0)
19+
$display ("%0d: display", cur_cycle);
20+
rg_state <= True;
21+
endrule
22+
23+
rule rl_finish (rg_state);
24+
$finish (0);
25+
endrule
26+
27+
endmodule
28+

testsuite/bsc.bluesim/misc/misc.exp

+10
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,13 @@ compile_object_pass AVMethBDPIWithReset.bsv
9090
copy c_func.c.keep c_func.c
9191
link_objects_pass {} sysAVMethBDPIWithReset {c_func.c}
9292
}
93+
94+
# GitHub #442: Nested if-else needs braces when possible ambiguity
95+
# This tests "if (!RST) if (cond) e1 else e2" in the code for
96+
# executing system tasks.
97+
if {$ctest == 1} {
98+
compile_object_pass NestedIfBraces.bsv
99+
link_objects_pass {} sysNestedIfBraces
100+
# Test that there were no warnings during C++ compilation
101+
compare_file [make_bsc_ccomp_output_name sysNestedIfBraces]
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Bluesim object created: sysNestedIfBraces.{h,o}
2+
Bluesim object created: model_sysNestedIfBraces.{h,o}
3+
Simulation shared library created: sysNestedIfBraces.cexe.so
4+
Simulation executable created: sysNestedIfBraces.cexe

0 commit comments

Comments
 (0)