Skip to content

Commit 422433c

Browse files
committed
Verilog: extract verilog_synthesist::synthesis_constant
This extracts the conversion of expressions into synthesis-time constants into a new method.
1 parent ada2100 commit 422433c

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

src/verilog/verilog_synthesis.cpp

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,33 @@ 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) const
420+
{
421+
exprt simplified = simplify_expr(expr, ns);
422+
423+
if(!simplified.is_constant())
424+
return {};
425+
426+
auto number = numeric_cast<mp_integer>(to_constant_expr(simplified));
427+
if(!number.has_value())
428+
DATA_INVARIANT(false, "synthesis_constant expects a numerical type");
429+
430+
return number.value();
431+
}
432+
433+
/*******************************************************************\
434+
408435
Function: verilog_synthesist::value_mapt::guarded_expr
409436
410437
Inputs:
@@ -2950,19 +2977,19 @@ void verilog_synthesist::synth_for(const verilog_fort &statement)
29502977
{
29512978
exprt tmp_guard=statement.condition();
29522979
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())
2980+
2981+
auto guard_value_opt = synthesis_constant(tmp_guard);
2982+
2983+
if(!guard_value_opt.has_value())
29572984
{
2958-
throw errort().with_location(
2959-
to_multi_ary_expr(statement).op1().source_location())
2985+
throw errort().with_location(statement.condition().source_location())
29602986
<< "synthesis failed to evaluate loop guard: `" << to_string(tmp_guard)
29612987
<< '\'';
29622988
}
29632989

2964-
if(tmp_guard.is_false()) break;
2965-
2990+
if(guard_value_opt.value() == 0)
2991+
break;
2992+
29662993
// copy the body
29672994
verilog_statementt tmp_body=statement.body();
29682995
synth_statement(tmp_body);

src/verilog/verilog_synthesis_class.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ class verilog_synthesist:
177177

178178
exprt guarded_expr(exprt) const;
179179
};
180-
180+
181+
// expressions
182+
[[nodiscard]] std::optional<mp_integer>
183+
synthesis_constant(const exprt &) const;
184+
181185
exprt current_value(
182186
const value_mapt::mapt &map,
183187
const symbolt &symbol,

0 commit comments

Comments
 (0)