Skip to content

Commit 9053c5f

Browse files
committed
format_expr now formats floating-point expressions
This adds formatters to format_expr for floating-point expressions.
1 parent f9af202 commit 9053c5f

File tree

1 file changed

+59
-10
lines changed

1 file changed

+59
-10
lines changed

src/util/format_expr.cpp

+59-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
#include "bitvector_expr.h"
1616
#include "byte_operators.h"
1717
#include "expr_util.h"
18+
#include "floatbv_expr.h"
1819
#include "format_type.h"
1920
#include "ieee_float.h"
2021
#include "mathematical_expr.h"
@@ -158,6 +159,14 @@ static std::ostream &format_rec(std::ostream &os, const unary_exprt &src)
158159
return os << format(src.op());
159160
}
160161

162+
/// This formats a ternary expression
163+
static std::ostream &format_rec(std::ostream &os, const ternary_exprt &src)
164+
{
165+
os << src.id() << '(' << format(src.op0()) << ", " << format(src.op1())
166+
<< ", " << format(src.op2()) << ')';
167+
return os;
168+
}
169+
161170
/// This formats a constant
162171
static std::ostream &format_rec(std::ostream &os, const constant_exprt &src)
163172
{
@@ -278,19 +287,30 @@ void format_expr_configt::setup()
278287
expr_map[ID_or] = multi_ary_expr;
279288
expr_map[ID_xor] = multi_ary_expr;
280289

281-
auto binary_expr = [](std::ostream &os, const exprt &expr) -> std::ostream & {
290+
auto binary_infix_expr =
291+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
282292
return format_rec(os, to_binary_expr(expr));
283293
};
284294

285-
expr_map[ID_lt] = binary_expr;
286-
expr_map[ID_gt] = binary_expr;
287-
expr_map[ID_ge] = binary_expr;
288-
expr_map[ID_le] = binary_expr;
289-
expr_map[ID_div] = binary_expr;
290-
expr_map[ID_minus] = binary_expr;
291-
expr_map[ID_implies] = binary_expr;
292-
expr_map[ID_equal] = binary_expr;
293-
expr_map[ID_notequal] = binary_expr;
295+
expr_map[ID_lt] = binary_infix_expr;
296+
expr_map[ID_gt] = binary_infix_expr;
297+
expr_map[ID_ge] = binary_infix_expr;
298+
expr_map[ID_le] = binary_infix_expr;
299+
expr_map[ID_div] = binary_infix_expr;
300+
expr_map[ID_minus] = binary_infix_expr;
301+
expr_map[ID_implies] = binary_infix_expr;
302+
expr_map[ID_equal] = binary_infix_expr;
303+
expr_map[ID_notequal] = binary_infix_expr;
304+
305+
auto binary_prefix_expr =
306+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
307+
os << expr.id() << '(' << format(to_binary_expr(expr).op0()) << ", "
308+
<< format(to_binary_expr(expr).op1()) << ')';
309+
return os;
310+
};
311+
312+
expr_map[ID_ieee_float_equal] = binary_prefix_expr;
313+
expr_map[ID_ieee_float_notequal] = binary_prefix_expr;
294314

295315
auto unary_expr = [](std::ostream &os, const exprt &expr) -> std::ostream & {
296316
return format_rec(os, to_unary_expr(expr));
@@ -299,6 +319,27 @@ void format_expr_configt::setup()
299319
expr_map[ID_not] = unary_expr;
300320
expr_map[ID_unary_minus] = unary_expr;
301321

322+
auto unary_with_parentheses_expr =
323+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
324+
return os << expr.id() << '(' << format(to_unary_expr(expr).op()) << ')';
325+
};
326+
327+
expr_map[ID_isnan] = unary_with_parentheses_expr;
328+
expr_map[ID_isinf] = unary_with_parentheses_expr;
329+
expr_map[ID_isfinite] = unary_with_parentheses_expr;
330+
expr_map[ID_isnormal] = unary_with_parentheses_expr;
331+
332+
auto ternary_expr =
333+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
334+
return format_rec(os, to_ternary_expr(expr));
335+
};
336+
337+
expr_map[ID_floatbv_plus] = ternary_expr;
338+
expr_map[ID_floatbv_minus] = ternary_expr;
339+
expr_map[ID_floatbv_mult] = ternary_expr;
340+
expr_map[ID_floatbv_div] = ternary_expr;
341+
expr_map[ID_floatbv_mod] = ternary_expr;
342+
302343
expr_map[ID_constant] =
303344
[](std::ostream &os, const exprt &expr) -> std::ostream & {
304345
return format_rec(os, to_constant_expr(expr));
@@ -322,6 +363,14 @@ void format_expr_configt::setup()
322363
<< format(expr.type()) << ')';
323364
};
324365

366+
expr_map[ID_floatbv_typecast] =
367+
[](std::ostream &os, const exprt &expr) -> std::ostream & {
368+
const auto &floatbv_typecast_expr = to_floatbv_typecast_expr(expr);
369+
return os << "floatbv_typecast(" << format(floatbv_typecast_expr.op())
370+
<< ", " << format(floatbv_typecast_expr.type()) << ", "
371+
<< format(floatbv_typecast_expr.rounding_mode()) << ')';
372+
};
373+
325374
auto byte_extract =
326375
[](std::ostream &os, const exprt &expr) -> std::ostream & {
327376
const auto &byte_extract_expr = to_byte_extract_expr(expr);

0 commit comments

Comments
 (0)