@@ -405,6 +405,35 @@ exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
405405
406406/* ******************************************************************\
407407
408+ Function: verilog_synthesist::synthesis_constant
409+
410+ Inputs:
411+
412+ Outputs:
413+
414+ Purpose:
415+
416+ \*******************************************************************/
417+
418+ std::optional<mp_integer>
419+ verilog_synthesist::synthesis_constant (const exprt &expr)
420+ {
421+ exprt synthesised = synth_expr (expr, symbol_statet::CURRENT);
422+
423+ exprt simplified = simplify_expr (synthesised, ns);
424+
425+ if (!simplified.is_constant ())
426+ return {};
427+
428+ auto number = numeric_cast<mp_integer>(to_constant_expr (simplified));
429+ if (!number.has_value ())
430+ DATA_INVARIANT (false , " synthesis_constant expects a numerical type" );
431+
432+ return number.value ();
433+ }
434+
435+ /* ******************************************************************\
436+
408437Function: verilog_synthesist::value_mapt::guarded_expr
409438
410439 Inputs:
@@ -2950,19 +2979,19 @@ void verilog_synthesist::synth_for(const verilog_fort &statement)
29502979 {
29512980 exprt tmp_guard=statement.condition ();
29522981 tmp_guard = typecast_exprt{tmp_guard, bool_typet{}};
2953- tmp_guard = synth_expr (tmp_guard, symbol_statet::CURRENT);
2954- simplify (tmp_guard, ns );
2955-
2956- if (!tmp_guard. is_constant ())
2982+
2983+ auto guard_value_opt = synthesis_constant (tmp_guard);
2984+
2985+ if (!guard_value_opt. has_value ())
29572986 {
2958- throw errort ().with_location (
2959- to_multi_ary_expr (statement).op1 ().source_location ())
2987+ throw errort ().with_location (statement.condition ().source_location ())
29602988 << " synthesis failed to evaluate loop guard: `" << to_string (tmp_guard)
29612989 << ' \' ' ;
29622990 }
29632991
2964- if (tmp_guard.is_false ()) break ;
2965-
2992+ if (guard_value_opt.value () == 0 )
2993+ break ;
2994+
29662995 // copy the body
29672996 verilog_statementt tmp_body=statement.body ();
29682997 synth_statement (tmp_body);
0 commit comments