@@ -18,6 +18,7 @@ Author: Daniel Kroening, kroening@kroening.com
1818#include < util/simplify_expr.h>
1919#include < util/std_expr.h>
2020
21+ #include " aval_bval_encoding.h"
2122#include " expr2verilog.h"
2223#include " sva_expr.h"
2324#include " verilog_expr.h"
@@ -74,6 +75,18 @@ exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
7475 UNREACHABLE;
7576 }
7677 }
78+ else if (expr.id () == ID_constant)
79+ {
80+ // encode into aval/bval
81+ if (
82+ expr.type ().id () == ID_verilog_unsignedbv ||
83+ expr.type ().id () == ID_verilog_signedbv)
84+ {
85+ return lower_to_aval_bval (to_constant_expr (expr));
86+ }
87+
88+ return expr;
89+ }
7790 else if (expr.id ()==ID_function_call)
7891 {
7992 return expand_function_call (to_function_call_expr (expr));
@@ -94,6 +107,19 @@ exprt verilog_synthesist::synth_expr(exprt expr, symbol_statet symbol_state)
94107 if (typecast_expr.op ().is_constant ())
95108 simplify (expr, ns);
96109
110+ if (
111+ expr.type ().id () == ID_verilog_unsignedbv ||
112+ expr.type ().id () == ID_verilog_signedbv)
113+ {
114+ auto aval_bval_type = lower_to_aval_bval (expr.type ());
115+
116+ if (is_aval_bval (typecast_expr.op ().type ()))
117+ {
118+ // separately convert aval and bval
119+ return aval_bval_conversion (typecast_expr.op (), aval_bval_type);
120+ }
121+ }
122+
97123 return expr;
98124 }
99125 else if (expr.id () == ID_verilog_non_indexed_part_select)
@@ -2260,71 +2286,34 @@ exprt verilog_synthesist::case_comparison(
22602286 const exprt &case_operand,
22612287 const exprt &pattern)
22622288{
2263- // we need to take case of ?, x, z in the pattern
2289+ // the pattern has the max type, not the case operand
22642290 const typet &pattern_type=pattern.type ();
2265-
2266- if (pattern_type.id ()==ID_verilog_signedbv ||
2267- pattern_type.id ()==ID_verilog_unsignedbv)
2268- {
2269- // try to simplify the pattern
2270- exprt tmp=pattern;
2271-
2272- simplify (tmp, ns);
2273-
2274- if (tmp.id ()!=ID_constant)
2275- {
2276- warning ().source_location =pattern.source_location ();
2277- warning () << " unexpected case pattern: " << to_string (tmp) << eom;
2278- }
2279- else
2280- {
2281- exprt new_case_operand=case_operand;
2282-
2283- // the pattern has the max type
2284- unsignedbv_typet new_type (pattern.type ().get_int (ID_width));
2285- new_case_operand = typecast_exprt{new_case_operand, new_type};
2286-
2287- // we are using masking!
2288-
2289- std::string new_pattern_value=
2290- id2string (to_constant_expr (tmp).get_value ());
2291-
2292- // ?zx -> 0
2293- for (unsigned i=0 ; i<new_pattern_value.size (); i++)
2294- if (new_pattern_value[i]==' ?' ||
2295- new_pattern_value[i]==' z' ||
2296- new_pattern_value[i]==' x' )
2297- new_pattern_value[i]=' 0' ;
2298-
2299- auto new_pattern =
2300- from_integer (string2integer (new_pattern_value, 2 ), new_type);
2301-
2302- std::string new_mask_value=
2303- id2string (to_constant_expr (tmp).get_value ());
23042291
2305- // ?zx -> 0, 0 -> 1
2306- for (unsigned i=0 ; i<new_mask_value.size (); i++)
2307- if (new_mask_value[i]==' ?' ||
2308- new_mask_value[i]==' z' ||
2309- new_mask_value[i]==' x' )
2310- new_mask_value[i]=' 0' ;
2311- else
2312- new_mask_value[i]=' 1' ;
2313-
2314- auto new_mask = from_integer (string2integer (new_mask_value, 2 ), new_type);
2292+ // we need to take case of ?, x, z in the pattern
2293+ if (is_aval_bval (pattern_type))
2294+ {
2295+ // We are using masking based on the pattern.
2296+ // The aval is the comparison value, and the
2297+ // negation of bval is the mask.
2298+ auto pattern_aval = ::aval (pattern);
2299+ auto pattern_bval = ::bval (pattern);
2300+ auto mask_expr = bitnot_exprt{pattern_bval};
23152301
2316- exprt bitand_expr = bitand_exprt{new_case_operand, new_mask};
2302+ auto case_operand_casted = typecast_exprt{
2303+ typecast_exprt::conditional_cast (
2304+ case_operand, aval_bval_underlying (pattern_type)),
2305+ mask_expr.type ()};
23172306
2318- return equal_exprt{bitand_expr, new_pattern};
2319- }
2307+ return equal_exprt{
2308+ bitand_exprt{case_operand_casted, mask_expr},
2309+ bitand_exprt{pattern_aval, mask_expr}};
23202310 }
23212311
2322- if (pattern.type ()==case_operand.type ())
2323- return equal_exprt (case_operand, pattern);
2312+ // 2-valued comparison
2313+ exprt case_operand_casted =
2314+ typecast_exprt::conditional_cast (case_operand, pattern_type);
23242315
2325- // the pattern has the max type
2326- exprt tmp_case_operand=typecast_exprt (case_operand, pattern.type ());
2327- return equal_exprt (tmp_case_operand, pattern);
2316+ return equal_exprt (case_operand_casted, pattern);
23282317}
23292318
23302319/* ******************************************************************\
0 commit comments