CBMC
Loading...
Searching...
No Matches
smt2_conv.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: SMT Backend
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
11
12#include "smt2_conv.h"
13
15#include <util/arith_tools.h>
16#include <util/bitvector_expr.h>
17#include <util/byte_operators.h>
18#include <util/c_types.h>
19#include <util/config.h>
21#include <util/expr_iterator.h>
22#include <util/expr_util.h>
23#include <util/find_symbols.h>
24#include <util/fixedbv.h>
25#include <util/floatbv_expr.h>
26#include <util/format_expr.h>
27#include <util/ieee_float.h>
28#include <util/invariant.h>
30#include <util/namespace.h>
33#include <util/prefix.h>
34#include <util/range.h>
35#include <util/rational.h>
36#include <util/rational_tools.h>
37#include <util/simplify_expr.h>
38#include <util/std_expr.h>
39#include <util/string2int.h>
41#include <util/threeval.h>
42
47
48#include "smt2_tokenizer.h"
49
50#include <cstdint>
51#include <map>
52
53// Mark different kinds of error conditions
54
55// Unexpected types and other combinations not implemented and not
56// expected to be needed
57#define UNEXPECTEDCASE(S) PRECONDITION_WITH_DIAGNOSTICS(false, S);
58
59// General todos
60#define SMT2_TODO(S) PRECONDITION_WITH_DIAGNOSTICS(false, "TODO: " S)
61
63 const namespacet &_ns,
64 const std::string &_benchmark,
65 const std::string &_notes,
66 const std::string &_logic,
67 solvert _solver,
68 std::ostream &_out)
69 : ns(_ns),
70 out(_out),
71 benchmark(_benchmark),
72 notes(_notes),
73 logic(_logic),
74 solver(_solver),
75 boolbv_width(_ns),
76 pointer_logic(_ns),
78{
79 // We set some defaults differently
80 // for some solvers.
81
82 switch(solver)
83 {
85 break;
86
88 use_FPA_theory = true;
89 use_array_of_bool = true;
90 use_as_const = true;
93 emit_set_logic = false;
94 break;
95
97 break;
98
100 use_FPA_theory = true;
101 use_array_of_bool = true;
102 use_as_const = true;
104 emit_set_logic = false;
105 break;
106
107 case solvert::CVC5:
108 logic = "ALL";
109 use_FPA_theory = true;
110 use_array_of_bool = true;
111 use_as_const = true;
113 use_datatypes = true;
114 break;
115
116 case solvert::MATHSAT:
117 break;
118
119 case solvert::YICES:
120 break;
121
122 case solvert::Z3:
123 use_array_of_bool = true;
124 // `as const` is disabled because of a soundness issue in Z3,
125 // see https://github.com/Z3Prover/z3/issues/9550. Revisit once
126 // the upstream bug is fixed and we can bump the minimum Z3
127 // version accordingly.
128 use_as_const = false;
131 emit_set_logic = false;
132 use_datatypes = true;
133 break;
134 }
135
136 write_header();
137}
138
140{
141 return "SMT2";
142}
143
144void smt2_convt::print_assignment(std::ostream &os) const
145{
146 // Boolean stuff
147
148 for(std::size_t v=0; v<boolean_assignment.size(); v++)
149 os << "b" << v << "=" << boolean_assignment[v] << "\n";
150
151 // others
152}
153
155{
156 if(l.is_true())
157 return tvt(true);
158 if(l.is_false())
159 return tvt(false);
160
161 INVARIANT(
162 l.var_no() < boolean_assignment.size(),
163 "variable number shall be within bounds");
164 return tvt(boolean_assignment[l.var_no()]^l.sign());
165}
166
168{
169 out << "; SMT 2" << "\n";
170
171 switch(solver)
172 {
173 // clang-format off
174 case solvert::GENERIC: break;
175 case solvert::BITWUZLA: out << "; Generated for Bitwuzla\n"; break;
176 case solvert::BOOLECTOR: out << "; Generated for Boolector\n"; break;
178 out << "; Generated for the CPROVER SMT2 solver\n"; break;
179 case solvert::CVC5: out << "; Generated for CVC 5\n"; break;
180 case solvert::MATHSAT: out << "; Generated for MathSAT\n"; break;
181 case solvert::YICES: out << "; Generated for Yices\n"; break;
182 case solvert::Z3: out << "; Generated for Z3\n"; break;
183 // clang-format on
184 }
185
186 out << "(set-info :source \"" << notes << "\")" << "\n";
187
188 out << "(set-option :produce-models true)" << "\n";
189
190 // We use a broad mixture of logics, so on some solvers
191 // its better not to declare here.
192 // set-logic should come after setting options
193 if(emit_set_logic && !logic.empty())
194 out << "(set-logic " << logic << ")" << "\n";
195}
196
198{
199 out << "\n";
200
201 // Output object size definitions
202 // Note: object_sizes is std::map, so iteration is deterministic (sorted by
203 // key)
204 for(const auto &object : object_sizes)
205 define_object_size(object.second, object.first);
206
207 if(use_check_sat_assuming && !assumptions.empty())
208 {
209 out << "(check-sat-assuming (";
210 for(const auto &assumption : assumptions)
211 convert_literal(assumption);
212 out << "))\n";
213 }
214 else
215 {
216 // add the assumptions, if any
217 if(!assumptions.empty())
218 {
219 out << "; assumptions\n";
220
221 for(const auto &assumption : assumptions)
222 {
223 out << "(assert ";
224 convert_literal(assumption);
225 out << ")"
226 << "\n";
227 }
228 }
229
230 out << "(check-sat)\n";
231 }
232
233 out << "\n";
234
236 {
237 // Output get-value commands for all identifiers
238 // Note: smt2_identifiers is std::set, so iteration is deterministic
239 // (sorted)
240 for(const auto &id : smt2_identifiers)
241 out << "(get-value (" << id << "))"
242 << "\n";
243 }
244
245 out << "\n";
246
247 out << "(exit)\n";
248
249 out << "; end of SMT2 file"
250 << "\n";
251}
252
254 const irep_idt &id,
255 const object_size_exprt &expr)
256{
257 const exprt &ptr = expr.pointer();
258 std::size_t pointer_width = boolbv_width(ptr.type());
259 std::size_t number = 0;
260 std::size_t h=pointer_width-1;
261 std::size_t l=pointer_width-config.bv_encoding.object_bits;
262
263 for(const auto &o : pointer_logic.objects)
264 {
265 const typet &type = o.type();
266 auto size_expr = size_of_expr(type, ns);
267
268 if(
269 (o.id() != ID_symbol && o.id() != ID_string_constant) ||
270 !size_expr.has_value())
271 {
272 ++number;
273 continue;
274 }
275
276 find_symbols(*size_expr);
277 out << "(assert (=> (= "
278 << "((_ extract " << h << " " << l << ") ";
279 convert_expr(ptr);
280 out << ") (_ bv" << number << " " << config.bv_encoding.object_bits << "))"
281 << "(= " << id << " ";
282 convert_expr(*size_expr);
283 out << ")))\n";
284
285 ++number;
286 }
287}
288
290{
291 if(assumption.is_nil())
292 write_footer();
293 else
294 {
295 assumptions.push_back(convert(assumption));
296 write_footer();
297 assumptions.pop_back();
298 }
299
300 out.flush();
302}
303
304exprt smt2_convt::get(const exprt &expr) const
305{
306 if(expr.id()==ID_symbol)
307 {
308 const irep_idt &id = to_symbol_expr(expr).identifier();
309
310 identifier_mapt::const_iterator it=identifier_map.find(id);
311
312 if(it!=identifier_map.end())
313 return it->second.value;
314 return expr;
315 }
316 else if(expr.id()==ID_nondet_symbol)
317 {
319
320 identifier_mapt::const_iterator it=identifier_map.find(id);
321
322 if(it!=identifier_map.end())
323 return it->second.value;
324 }
325 else if(expr.id() == ID_literal)
326 {
327 auto l = to_literal_expr(expr).get_literal();
328 if(l_get(l).is_true())
329 return true_exprt();
330 else
331 return false_exprt();
332 }
333 else if(expr.id() == ID_not)
334 {
335 auto op = get(to_not_expr(expr).op());
336 if(op == true)
337 return false_exprt();
338 else if(op == false)
339 return true_exprt();
340 }
341 else if(
342 expr.is_constant() || expr.id() == ID_empty_union ||
343 (!expr.has_operands() && (expr.id() == ID_struct || expr.id() == ID_array)))
344 {
345 return expr;
346 }
347 else if(expr.has_operands())
348 {
349 exprt copy = expr;
350 for(auto &op : copy.operands())
351 {
352 exprt eval_op = get(op);
353 if(eval_op.is_nil())
354 return nil_exprt{};
355 op = std::move(eval_op);
356 }
357 return copy;
358 }
359
360 return nil_exprt();
361}
362
364 const irept &src,
365 const typet &type)
366{
367 // See http://www.grammatech.com/resources/smt/SMTLIBTutorial.pdf for the
368 // syntax of SMTlib2 literals.
369 //
370 // A literal expression is one of the following forms:
371 //
372 // * Numeral -- this is a natural number in decimal and is of the form:
373 // 0|([1-9][0-9]*)
374 // * Decimal -- this is a decimal expansion of a real number of the form:
375 // (0|[1-9][0-9]*)[.]([0-9]+)
376 // * Binary -- this is a natural number in binary and is of the form:
377 // #b[01]+
378 // * Hex -- this is a natural number in hexadecimal and is of the form:
379 // #x[0-9a-fA-F]+
380 //
381 // Right now I'm not parsing decimals. It'd be nice if we had a real YACC
382 // parser here, but whatever.
383
384 mp_integer value;
385
386 if(!src.id().empty())
387 {
388 const std::string &s=src.id_string();
389
390 if(s.size()>=2 && s[0]=='#' && s[1]=='b')
391 {
392 // Binary #b010101
393 value=string2integer(s.substr(2), 2);
394 }
395 else if(s.size()>=2 && s[0]=='#' && s[1]=='x')
396 {
397 // Hex #x012345
398 value=string2integer(s.substr(2), 16);
399 }
400 else
401 {
402 std::size_t pos = s.find(".");
403 if(pos != std::string::npos)
404 {
405 // Decimal, return as rational or real
406 if(type.id() == ID_rational)
407 {
408 rationalt rational_value;
409 bool failed = to_rational(
410 constant_exprt{src.id(), rational_typet{}}, rational_value);
412 return from_rational(rational_value);
413 }
414 else if(type.id() == ID_real)
415 {
416 rationalt rational_value;
417 bool failed = to_rational(
418 constant_exprt{src.id(), rational_typet{}}, rational_value);
420 return algebraic_numbert{rational_value}.as_expr();
421 }
422 else
423 {
425 "smt2_convt::parse_literal parsed a number with a decimal point "
426 "as type " +
427 type.id_string());
428 }
429 }
430 // Numeral
431 value=string2integer(s);
432 }
433 }
434 else if(src.get_sub().size()==2 &&
435 src.get_sub()[0].id()=="-") // (- 100)
436 {
437 value=-string2integer(src.get_sub()[1].id_string());
438 }
439 else if(src.get_sub().size()==3 &&
440 src.get_sub()[0].id()=="_" &&
441 // (_ bvDECIMAL_VALUE SIZE)
442 src.get_sub()[1].id_string().substr(0, 2)=="bv")
443 {
444 value=string2integer(src.get_sub()[1].id_string().substr(2));
445 }
446 else if(
447 type.id() == ID_rational && src.get_sub().size() == 3 &&
448 src.get_sub()[0].id() == "/")
449 {
450 rationalt numerator;
451 rationalt denominator;
452 bool failed =
453 to_rational(parse_literal(src.get_sub()[1], type), numerator) ||
454 to_rational(parse_literal(src.get_sub()[2], type), denominator);
456 return from_rational(numerator / denominator);
457 }
458 else if(src.get_sub().size()==4 &&
459 src.get_sub()[0].id()=="fp") // (fp signbv exponentbv significandbv)
460 {
461 if(type.id()==ID_floatbv)
462 {
463 const floatbv_typet &floatbv_type=to_floatbv_type(type);
466 parse_literal(src.get_sub()[2], unsignedbv_typet(floatbv_type.get_e()));
467 constant_exprt s3 =
468 parse_literal(src.get_sub()[3], unsignedbv_typet(floatbv_type.get_f()));
469
470 const auto s1_int = numeric_cast_v<mp_integer>(s1);
471 const auto s2_int = numeric_cast_v<mp_integer>(s2);
472 const auto s3_int = numeric_cast_v<mp_integer>(s3);
473
474 // stitch the bits together
475 value = bitwise_or(
476 s1_int << (floatbv_type.get_e() + floatbv_type.get_f()),
477 bitwise_or((s2_int << floatbv_type.get_f()), s3_int));
478 }
479 else
480 value=0;
481 }
482 else if(src.get_sub().size()==4 &&
483 src.get_sub()[0].id()=="_" &&
484 src.get_sub()[1].id()=="+oo") // (_ +oo e s)
485 {
486 std::size_t e = unsafe_string2size_t(src.get_sub()[2].id_string());
487 std::size_t s = unsafe_string2size_t(src.get_sub()[3].id_string());
489 .to_expr();
490 }
491 else if(src.get_sub().size()==4 &&
492 src.get_sub()[0].id()=="_" &&
493 src.get_sub()[1].id()=="-oo") // (_ -oo e s)
494 {
495 std::size_t e = unsafe_string2size_t(src.get_sub()[2].id_string());
496 std::size_t s = unsafe_string2size_t(src.get_sub()[3].id_string());
498 .to_expr();
499 }
500 else if(src.get_sub().size()==4 &&
501 src.get_sub()[0].id()=="_" &&
502 src.get_sub()[1].id()=="NaN") // (_ NaN e s)
503 {
504 std::size_t e = unsafe_string2size_t(src.get_sub()[2].id_string());
505 std::size_t s = unsafe_string2size_t(src.get_sub()[3].id_string());
507 }
508 else if(
509 src.get_sub().size() == 3 &&
510 src.get_sub()[0].id() == "root-obj") // (root-obj (+ ...) <index>)
511 {
512 // Z3 emits these while there isn't an agreed-upon standard for representing
513 // algebraic numbers just yet. https://smt-comp.github.io/2023/model.html
514 // gave some proposals, but these don't seem to have been implemented.
515 // For now, we use DATA_INVARIANT as our parsing may be overly restrictive.
516 // Eventually, these should become proper, user-facing exceptions.
517 //
518 // The third element is the 1-based index identifying which real root of
519 // the polynomial Z3 chose for its model. We do not constrain its value
520 // because algebraic_numbert tracks only the polynomial, not the specific
521 // root, so any root of the same polynomial maps to the same expression.
523 src.get_sub()[1].id().empty() && src.get_sub()[1].get_sub().size() == 3 &&
524 src.get_sub()[1].get_sub()[0].id() == "+",
525 "unexpected root-obj expression",
526 src.pretty());
527 irept sum_rhs = src.get_sub()[1].get_sub()[2];
528 rationalt constant_coeff;
529 bool failed =
530 to_rational(parse_literal(sum_rhs, rational_typet{}), constant_coeff);
532 !failed, "failed to parse rational constant coefficient", src.pretty());
533 irept sum_lhs = src.get_sub()[1].get_sub()[1];
535 sum_lhs.id().empty() && sum_lhs.get_sub().size() == 3 &&
536 sum_lhs.get_sub()[0].id() == "^" && sum_lhs.get_sub()[1].id() == "x",
537 "unexpected first operand to root-obj",
538 src.pretty());
539 std::size_t degree = unsafe_string2size_t(sum_lhs.get_sub()[2].id_string());
541 degree > 0, "polynomial degree must be positive", src.pretty());
542 std::vector<rationalt> coefficients{degree + 1, rationalt{}};
543 coefficients.front() = constant_coeff;
544 coefficients.back() = rationalt{1};
545 algebraic_numbert a{coefficients};
546 return a.as_expr();
547 }
548
549 if(type.id()==ID_signedbv ||
550 type.id()==ID_unsignedbv ||
551 type.id()==ID_c_enum ||
552 type.id()==ID_c_bool)
553 {
554 return from_integer(value, type);
555 }
556 else if(type.id()==ID_c_enum_tag)
557 {
558 constant_exprt result =
559 from_integer(value, ns.follow_tag(to_c_enum_tag_type(type)));
560
561 // restore the c_enum_tag type
562 result.type() = type;
563 return result;
564 }
565 else if(type.id()==ID_fixedbv ||
566 type.id()==ID_floatbv)
567 {
568 std::size_t width=boolbv_width(type);
569 return constant_exprt(integer2bvrep(value, width), type);
570 }
571 else if(
572 type.id() == ID_integer || type.id() == ID_natural ||
573 type.id() == ID_rational || type.id() == ID_real)
574 {
575 return from_integer(value, type);
576 }
577 else if(type.id() == ID_range)
578 {
579 return from_integer(value + to_integer_range_type(type).from(), type);
580 }
581 else
583 "smt2_convt::parse_literal should not be of unsupported type " +
584 type.id_string());
585}
586
588 const irept &src,
589 const array_typet &type)
590{
591 std::unordered_map<int64_t, exprt> operands_map;
592 walk_array_tree(&operands_map, src, type);
593 exprt::operandst operands;
594 // Try to find the default value, if there is none then set it
595 auto maybe_default_op = operands_map.find(-1);
596 exprt default_op;
597 if(maybe_default_op == operands_map.end())
598 default_op = nil_exprt();
599 else
600 default_op = maybe_default_op->second;
601 int64_t i = 0;
602 auto maybe_size = numeric_cast<std::int64_t>(type.size());
603 if(maybe_size.has_value())
604 {
605 while(i < maybe_size.value())
606 {
607 auto found_op = operands_map.find(i);
608 if(found_op != operands_map.end())
609 operands.emplace_back(found_op->second);
610 else
611 operands.emplace_back(default_op);
612 i++;
613 }
614 }
615 else
616 {
617 // Array size is unknown, keep adding with known indexes in order
618 // until we fail to find one.
619 auto found_op = operands_map.find(i);
620 while(found_op != operands_map.end())
621 {
622 operands.emplace_back(found_op->second);
623 i++;
624 found_op = operands_map.find(i);
625 }
626 operands.emplace_back(default_op);
627 }
628 return array_exprt(operands, type);
629}
630
632 std::unordered_map<int64_t, exprt> *operands_map,
633 const irept &src,
634 const array_typet &type)
635{
636 if(src.get_sub().size()==4 && src.get_sub()[0].id()=="store")
637 {
638 // This is the SMT syntax being parsed here
639 // (store array index value)
640 // Recurse
641 walk_array_tree(operands_map, src.get_sub()[1], type);
642 const auto index_expr = parse_rec(src.get_sub()[2], type.size().type());
643 if(!index_expr.is_constant())
644 // The index in a (store array index value) term is not necessarily a
645 // constant: models for unbounded or non-integer-keyed arrays can carry
646 // a symbolic/non-constant index. Such an entry cannot be placed in the
647 // index-keyed operands map, so skip it during model reconstruction.
648 return;
649 const constant_exprt index_constant = to_constant_expr(index_expr);
650 mp_integer tempint;
651 bool failure = to_integer(index_constant, tempint);
652 if(failure)
653 return;
654 long index = tempint.to_long();
655 exprt value = parse_rec(src.get_sub()[3], type.element_type());
656 operands_map->emplace(index, value);
657 }
658 else if(src.get_sub().size()==2 &&
659 src.get_sub()[0].get_sub().size()==3 &&
660 src.get_sub()[0].get_sub()[0].id()=="as" &&
661 src.get_sub()[0].get_sub()[1].id()=="const")
662 {
663 // (as const type_info default_value)
664 exprt default_value = parse_rec(src.get_sub()[1], type.element_type());
665 operands_map->emplace(-1, default_value);
666 }
667 else
668 {
669 auto bindings_it = current_bindings.find(src.id());
670 if(bindings_it != current_bindings.end())
671 walk_array_tree(operands_map, bindings_it->second, type);
672 }
673}
674
676 const irept &src,
677 const union_typet &type)
678{
679 // these are always flat
680 PRECONDITION(!type.components().empty());
681 const union_typet::componentt &first=type.components().front();
682 std::size_t width=boolbv_width(type);
683 exprt value = parse_rec(src, unsignedbv_typet(width));
684 if(value.is_nil())
685 return nil_exprt();
686 const typecast_exprt converted(value, first.type());
687 return union_exprt(first.get_name(), converted, type);
688}
689
692{
693 const struct_typet::componentst &components =
694 type.components();
695
696 struct_exprt result(exprt::operandst(components.size(), nil_exprt()), type);
697
698 if(components.empty())
699 return result;
700
701 if(use_datatypes)
702 {
703 // Structs look like:
704 // (mk-struct.1 <component0> <component1> ... <componentN>)
705 std::size_t j = 1;
706 for(std::size_t i=0; i<components.size(); i++)
707 {
708 const struct_typet::componentt &c=components[i];
709 if(is_zero_width(components[i].type(), ns))
710 {
711 result.operands()[i] = nil_exprt{};
712 }
713 else
714 {
716 src.get_sub().size() > j, "insufficient number of component values");
717 result.operands()[i] = parse_rec(src.get_sub()[j], c.type());
718 ++j;
719 }
720 }
721 }
722 else
723 {
724 // These are just flattened, i.e., we expect to see a monster bit vector.
725 std::size_t total_width=boolbv_width(type);
726 const auto l = parse_literal(src, unsignedbv_typet(total_width));
727
728 const irep_idt binary =
730
731 CHECK_RETURN(binary.size() == total_width);
732
733 std::size_t offset=0;
734
735 for(std::size_t i=0; i<components.size(); i++)
736 {
737 if(is_zero_width(components[i].type(), ns))
738 continue;
739
740 std::size_t component_width=boolbv_width(components[i].type());
741
742 INVARIANT(
743 offset + component_width <= total_width,
744 "struct component bits shall be within struct bit vector");
745
746 std::string component_binary=
747 "#b"+id2string(binary).substr(
748 total_width-offset-component_width, component_width);
749
750 result.operands()[i]=
751 parse_rec(irept(component_binary), components[i].type());
752
753 offset+=component_width;
754 }
755 }
756
757 return result;
758}
759
760exprt smt2_convt::parse_rec(const irept &src, const typet &type)
761{
762 if(src.get_sub().size() == 3 && src.get_sub()[0].id() == ID_let)
763 {
764 // This is produced by Z3
765 // (let (....) (....))
766 auto previous_bindings = current_bindings;
767 for(const auto &binding : src.get_sub()[1].get_sub())
768 {
769 const irep_idt &name = binding.get_sub()[0].id();
770 current_bindings.emplace(name, binding.get_sub()[1]);
771 }
772 exprt result = parse_rec(src.get_sub()[2], type);
773 current_bindings = std::move(previous_bindings);
774 return result;
775 }
776
777 auto bindings_it = current_bindings.find(src.id());
778 if(bindings_it != current_bindings.end())
779 {
780 return parse_rec(bindings_it->second, type);
781 }
782
783 if(
784 type.id() == ID_signedbv || type.id() == ID_unsignedbv ||
785 type.id() == ID_integer || type.id() == ID_rational ||
786 type.id() == ID_natural || type.id() == ID_real || type.id() == ID_c_enum ||
787 type.id() == ID_c_enum_tag || type.id() == ID_fixedbv ||
788 type.id() == ID_floatbv || type.id() == ID_c_bool || type.id() == ID_range)
789 {
790 return parse_literal(src, type);
791 }
792 else if(type.id()==ID_bool)
793 {
794 if(src.id()==ID_1 || src.id()==ID_true)
795 return true_exprt();
796 else if(src.id()==ID_0 || src.id()==ID_false)
797 return false_exprt();
798 }
799 else if(type.id()==ID_pointer)
800 {
801 // these come in as bit-vector literals
802 std::size_t width=boolbv_width(type);
803 constant_exprt bv_expr = parse_literal(src, unsignedbv_typet(width));
804
806
807 // split into object and offset
808 mp_integer pow=power(2, width-config.bv_encoding.object_bits);
812 bv_expr.get_value(),
813 pointer_logic.pointer_expr(ptr, to_pointer_type(type)));
814 }
815 else if(type.id()==ID_struct)
816 {
817 return parse_struct(src, to_struct_type(type));
818 }
819 else if(type.id() == ID_struct_tag)
820 {
821 auto struct_expr =
822 parse_struct(src, ns.follow_tag(to_struct_tag_type(type)));
823 // restore the tag type
824 struct_expr.type() = type;
825 return std::move(struct_expr);
826 }
827 else if(type.id()==ID_union)
828 {
829 return parse_union(src, to_union_type(type));
830 }
831 else if(type.id() == ID_union_tag)
832 {
833 auto union_expr = parse_union(src, ns.follow_tag(to_union_tag_type(type)));
834 // restore the tag type
835 union_expr.type() = type;
836 return union_expr;
837 }
838 else if(type.id()==ID_array)
839 {
840 return parse_array(src, to_array_type(type));
841 }
842
843 return nil_exprt();
844}
845
847 const exprt &expr,
848 const pointer_typet &result_type)
849{
850 if(
851 expr.id() == ID_symbol || expr.is_constant() ||
852 expr.id() == ID_string_constant || expr.id() == ID_label)
853 {
854 const std::size_t object_bits = config.bv_encoding.object_bits;
855 const std::size_t max_objects = std::size_t(1) << object_bits;
856 const mp_integer object_id = pointer_logic.add_object(expr);
857
858 if(object_id >= max_objects)
859 {
861 "too many addressed objects: maximum number of objects is set to 2^n=" +
862 std::to_string(max_objects) +
863 " (with n=" + std::to_string(object_bits) + "); " +
864 "use the `--object-bits n` option to increase the maximum number"};
865 }
866
867 out << "(concat (_ bv" << object_id << " " << object_bits << ")"
868 << " (_ bv0 " << boolbv_width(result_type) - object_bits << "))";
869 }
870 else if(expr.id()==ID_index)
871 {
872 const index_exprt &index_expr = to_index_expr(expr);
873
874 const exprt &array = index_expr.array();
875 const exprt &index = index_expr.index();
876
877 if(index == 0)
878 {
879 if(array.type().id()==ID_pointer)
880 convert_expr(array);
881 else if(array.type().id()==ID_array)
882 convert_address_of_rec(array, result_type);
883 else
885 }
886 else
887 {
888 // this is really pointer arithmetic
889 index_exprt new_index_expr = index_expr;
890 new_index_expr.index() = from_integer(0, index.type());
891
892 address_of_exprt address_of_expr(
893 new_index_expr,
895
896 plus_exprt plus_expr{address_of_expr, index};
897
898 convert_expr(plus_expr);
899 }
900 }
901 else if(expr.id()==ID_member)
902 {
903 const member_exprt &member_expr=to_member_expr(expr);
904
905 const exprt &struct_op=member_expr.struct_op();
906 const typet &struct_op_type = struct_op.type();
907
909 struct_op_type.id() == ID_struct || struct_op_type.id() == ID_struct_tag,
910 "member expression operand shall have struct type");
911
912 const struct_typet &struct_type =
913 struct_op_type.id() == ID_struct_tag
914 ? ns.follow_tag(to_struct_tag_type(struct_op_type))
915 : to_struct_type(struct_op_type);
916
917 const irep_idt &component_name = member_expr.get_component_name();
918
919 const auto offset = member_offset(struct_type, component_name, ns);
920 CHECK_RETURN(offset.has_value() && *offset >= bytest{0});
921
922 unsignedbv_typet index_type(boolbv_width(result_type));
923
924 // pointer arithmetic
925 out << "(bvadd ";
926 convert_address_of_rec(struct_op, result_type);
927 convert_expr(from_integer(*offset, index_type));
928 out << ")"; // bvadd
929 }
930 else if(expr.id()==ID_if)
931 {
932 const if_exprt &if_expr = to_if_expr(expr);
933
934 out << "(ite ";
935 convert_expr(if_expr.cond());
936 out << " ";
937 convert_address_of_rec(if_expr.true_case(), result_type);
938 out << " ";
939 convert_address_of_rec(if_expr.false_case(), result_type);
940 out << ")";
941 }
942 else
943 INVARIANT(
944 false,
945 "operand of address of expression should not be of kind " +
946 expr.id_string());
947}
948
949static bool has_quantifier(const exprt &expr)
950{
951 bool result = false;
952 expr.visit_post([&result](const exprt &node) {
953 if(node.id() == ID_exists || node.id() == ID_forall)
954 result = true;
955 });
956 return result;
957}
958
960{
961 PRECONDITION(expr.is_boolean());
962
963 // Three cases where no new handle is needed.
964
965 if(expr == true)
966 return const_literal(true);
967 else if(expr == false)
968 return const_literal(false);
969 else if(expr.id()==ID_literal)
970 return to_literal_expr(expr).get_literal();
971
972 // Need a new handle
973
974 out << "\n";
975
976 exprt prepared_expr = prepare_for_convert_expr(expr);
977
980
981 out << "; convert\n";
982 out << "; Converting var_no " << l.var_no() << " with expr ID of "
983 << expr.id_string() << "\n";
984 // We're converting the expression, so store it in the defined_expressions
985 // store and in future we use the literal instead of the whole expression
986 // Note that here we are always converting, so we do not need to consider
987 // other literal kinds, only "|B###|"
988
989 // Z3 refuses get-value when a defined symbol contains a quantifier.
990 if(has_quantifier(prepared_expr))
991 {
992 out << "(declare-fun ";
994 out << " () Bool)\n";
995 out << "(assert (= ";
997 out << ' ';
998 convert_expr(prepared_expr);
999 out << "))\n";
1000 }
1001 else
1002 {
1003 auto identifier =
1004 convert_identifier(std::string{"B"} + std::to_string(l.var_no()));
1005 defined_expressions[expr] = identifier;
1006 smt2_identifiers.insert(identifier);
1007 out << "(define-fun " << identifier << " () Bool ";
1008 convert_expr(prepared_expr);
1009 out << ")\n";
1010 }
1011
1012 return l;
1013}
1014
1016{
1017 // We can only improve Booleans.
1018 if(!expr.is_boolean())
1019 return expr;
1020
1021 return literal_exprt(convert(expr));
1022}
1023
1025{
1026 if(l==const_literal(false))
1027 out << "false";
1028 else if(l==const_literal(true))
1029 out << "true";
1030 else
1031 {
1032 if(l.sign())
1033 out << "(not ";
1034
1035 const auto identifier =
1036 convert_identifier("B" + std::to_string(l.var_no()));
1037
1038 out << identifier;
1039
1040 if(l.sign())
1041 out << ")";
1042
1043 smt2_identifiers.insert(identifier);
1044 }
1045}
1046
1048{
1050}
1051
1052void smt2_convt::push(const std::vector<exprt> &_assumptions)
1053{
1054 INVARIANT(assumptions.empty(), "nested contexts are not supported");
1055
1056 assumptions.reserve(_assumptions.size());
1057 for(auto &assumption : _assumptions)
1058 assumptions.push_back(convert(assumption));
1059}
1060
1062{
1063 assumptions.clear();
1064}
1065
1066static bool is_smt2_simple_identifier(const std::string &identifier)
1067{
1068 if(identifier.empty())
1069 return false;
1070
1071 if(isdigit(identifier[0]))
1072 return false;
1073
1074 for(auto ch : id2string(identifier))
1075 {
1077 return false;
1078 }
1079
1080 return true;
1081}
1082
1083std::string smt2_convt::convert_identifier(const irep_idt &identifier)
1084{
1085 // Is this a "simple identifier"?
1086 if(is_smt2_simple_identifier(id2string(identifier)))
1087 return id2string(identifier);
1088
1089 // Backslashes are disallowed in quoted symbols just for simplicity.
1090 // Otherwise, for Common Lisp compatibility they would have to be treated
1091 // as escaping symbols.
1092
1093 std::string result = "|";
1094
1095 for(auto ch : identifier)
1096 {
1097 switch(ch)
1098 {
1099 case '|':
1100 case '\\':
1101 case '&': // we use the & for escaping
1102 result+='&';
1103 result+=std::to_string(ch);
1104 result+=';';
1105 break;
1106
1107 case '$': // $ _is_ allowed
1108 default:
1109 result+=ch;
1110 }
1111 }
1112
1113 result += '|';
1114
1115 return result;
1116}
1117
1118std::string smt2_convt::type2id(const typet &type) const
1119{
1120 if(type.id()==ID_floatbv)
1121 {
1123 return "f"+std::to_string(spec.width())+"_"+std::to_string(spec.f);
1124 }
1125 else if(type.id() == ID_bv)
1126 {
1127 return "B" + std::to_string(to_bitvector_type(type).get_width());
1128 }
1129 else if(type.id()==ID_unsignedbv)
1130 {
1131 return "u"+std::to_string(to_unsignedbv_type(type).get_width());
1132 }
1133 else if(type.id()==ID_c_bool)
1134 {
1135 return "u"+std::to_string(to_c_bool_type(type).get_width());
1136 }
1137 else if(type.id()==ID_signedbv)
1138 {
1139 return "s"+std::to_string(to_signedbv_type(type).get_width());
1140 }
1141 else if(type.id()==ID_bool)
1142 {
1143 return "b";
1144 }
1145 else if(type.id()==ID_c_enum_tag)
1146 {
1147 return type2id(ns.follow_tag(to_c_enum_tag_type(type)).underlying_type());
1148 }
1149 else if(type.id() == ID_pointer)
1150 {
1151 return "p" + std::to_string(to_pointer_type(type).get_width());
1152 }
1153 else if(type.id() == ID_struct_tag)
1154 {
1155 if(use_datatypes)
1156 return datatype_map.at(type);
1157 else
1158 return "S" + std::to_string(boolbv_width(type));
1159 }
1160 else if(type.id() == ID_union_tag)
1161 {
1162 return "U" + std::to_string(boolbv_width(type));
1163 }
1164 else if(type.id() == ID_array)
1165 {
1166 return "A" + type2id(to_array_type(type).element_type());
1167 }
1168 else if(type.id() == ID_integer)
1169 {
1170 return "Int";
1171 }
1172 else if(type.id() == ID_real)
1173 {
1174 return "Real";
1175 }
1176 else if(type.id() == ID_string)
1177 {
1178 return "String";
1179 }
1180 else if(type.id() == ID_regex)
1181 {
1182 return "RegLan";
1183 }
1184 else if(type.id() == ID_mathematical_function)
1185 {
1186 std::string result = "MF";
1187 const auto &mf = to_mathematical_function_type(type);
1188 for(const auto &d : mf.domain())
1189 result += "_" + type2id(d);
1190 result += "_" + type2id(mf.codomain());
1191 return result;
1192 }
1193 else
1194 {
1196 }
1197}
1198
1199std::string smt2_convt::floatbv_suffix(const exprt &expr) const
1200{
1201 PRECONDITION(!expr.operands().empty());
1202 return "_" + type2id(to_multi_ary_expr(expr).op0().type()) + "->" +
1203 type2id(expr.type());
1204}
1205
1207{
1209
1210 if(expr.id()==ID_symbol)
1211 {
1212 const irep_idt &id = to_symbol_expr(expr).identifier();
1213 out << convert_identifier(id);
1214 return;
1215 }
1216
1217 if(expr.id()==ID_smt2_symbol)
1218 {
1219 const irep_idt &id = to_smt2_symbol(expr).get_identifier();
1220 out << id;
1221 return;
1222 }
1223
1224 INVARIANT(
1225 !expr.operands().empty(), "non-symbol expressions shall have operands");
1226
1227 out << '('
1229 "float_bv." + expr.id_string() + floatbv_suffix(expr));
1230
1231 for(const auto &op : expr.operands())
1232 {
1233 out << ' ';
1234 convert_expr(op);
1235 }
1236
1237 out << ')';
1238}
1239
1240void smt2_convt::convert_string_literal(const std::string &s)
1241{
1242 out << '"';
1243 for(auto ch : s)
1244 {
1245 // " is escaped by double-quoting
1246 if(ch == '"')
1247 out << '"';
1248 out << ch;
1249 }
1250 out << '"';
1251}
1252
1254{
1255 // try hash table first
1256 auto converter_result = converters.find(expr.id());
1257 if(converter_result != converters.end())
1258 {
1259 converter_result->second(expr);
1260 return; // done
1261 }
1262
1263 // huge monster case split over expression id
1264 if(expr.id()==ID_symbol)
1265 {
1266 const irep_idt &id = to_symbol_expr(expr).identifier();
1267 DATA_INVARIANT(!id.empty(), "symbol must have identifier");
1268 out << convert_identifier(id);
1269 }
1270 else if(expr.id()==ID_nondet_symbol)
1271 {
1272 const irep_idt &id = to_nondet_symbol_expr(expr).get_identifier();
1273 DATA_INVARIANT(!id.empty(), "nondet symbol must have identifier");
1274 out << convert_identifier("nondet_" + id2string(id));
1275 }
1276 else if(expr.id()==ID_smt2_symbol)
1277 {
1278 const irep_idt &id = to_smt2_symbol(expr).get_identifier();
1279 DATA_INVARIANT(!id.empty(), "smt2 symbol must have identifier");
1280 out << id;
1281 }
1282 else if(expr.id()==ID_typecast)
1283 {
1285 }
1286 else if(expr.id()==ID_floatbv_typecast)
1287 {
1289 }
1290 else if(expr.id() == ID_floatbv_round_to_integral)
1291 {
1293 }
1294 else if(expr.id()==ID_struct)
1295 {
1297 }
1298 else if(expr.id()==ID_union)
1299 {
1301 }
1302 else if(expr.is_constant())
1303 {
1305 }
1306 else if(expr.id() == ID_concatenation)
1307 {
1309 !expr.operands().empty(),
1310 "concatenation expression should have at least one operand",
1311 expr.id_string());
1312
1313 // collect non-zero-width operands (zero-width not allowed by SMT-LIB)
1314 exprt::operandst non_zero_width_ops;
1315 for(const auto &op : expr.operands())
1316 {
1317 if(!is_zero_width(op.type(), ns))
1318 non_zero_width_ops.push_back(op);
1319 }
1320
1322 !non_zero_width_ops.empty(),
1323 "concatenation must have at least one non-zero-width operand");
1324
1325 if(non_zero_width_ops.size() == 1)
1326 {
1327 // unary concat is not valid SMT-LIB; emit the operand directly
1328 flatten2bv(non_zero_width_ops.front());
1329 }
1330 else
1331 {
1332 out << "(concat";
1333
1334 for(const auto &op : non_zero_width_ops)
1335 {
1336 out << ' ';
1337 flatten2bv(op);
1338 }
1339
1340 out << ')';
1341 }
1342 }
1343 else if(
1344 expr.id() == ID_bitand || expr.id() == ID_bitor || expr.id() == ID_bitxor)
1345 {
1347 !expr.operands().empty(),
1348 "given expression should have at least one operand",
1349 expr.id_string());
1350
1351 if(expr.operands().size() == 1)
1352 {
1353 flatten2bv(expr.operands().front());
1354 }
1355 else // >= 2
1356 {
1357 out << '(';
1358
1359 if(expr.id() == ID_concatenation)
1360 out << "concat";
1361 else if(expr.id() == ID_bitand)
1362 out << "bvand";
1363 else if(expr.id() == ID_bitor)
1364 out << "bvor";
1365 else if(expr.id() == ID_bitxor)
1366 out << "bvxor";
1367
1368 for(const auto &op : expr.operands())
1369 {
1370 out << ' ';
1371 flatten2bv(op);
1372 }
1373
1374 out << ')';
1375 }
1376 }
1377 else if(
1378 expr.id() == ID_bitxnor || expr.id() == ID_bitnand ||
1379 expr.id() == ID_bitnor)
1380 {
1381 // SMT-LIB only has these as a binary expression,
1382 // owing to their ambiguity.
1383 if(expr.operands().size() == 2)
1384 {
1385 const auto &binary_expr = to_binary_expr(expr);
1386
1387 out << '(';
1388 if(binary_expr.id() == ID_bitxnor)
1389 out << "bvxnor";
1390 else if(binary_expr.id() == ID_bitnand)
1391 out << "bvnand";
1392 else if(binary_expr.id() == ID_bitnor)
1393 out << "bvnor";
1394 out << ' ';
1395 flatten2bv(binary_expr.op0());
1396 out << ' ';
1397 flatten2bv(binary_expr.op1());
1398 out << ')';
1399 }
1400 else if(expr.operands().size() == 1)
1401 {
1402 out << "(bvnot ";
1403 flatten2bv(to_unary_expr(expr).op());
1404 out << ')';
1405 }
1406 else if(expr.operands().size() >= 3)
1407 {
1408 out << "(bvnot (";
1409 if(expr.id() == ID_bitxnor)
1410 out << "bvxor";
1411 else if(expr.id() == ID_bitnand)
1412 out << "bvand";
1413 else if(expr.id() == ID_bitnor)
1414 out << "bvor";
1415
1416 for(const auto &op : expr.operands())
1417 {
1418 out << ' ';
1419 flatten2bv(op);
1420 }
1421
1422 out << "))"; // bvX, bvnot
1423 }
1424 else
1425 {
1427 expr.operands().size() >= 1,
1428 expr.id_string() + " should have at least one operand");
1429 }
1430 }
1431 else if(expr.id()==ID_bitnot)
1432 {
1433 const bitnot_exprt &bitnot_expr = to_bitnot_expr(expr);
1434
1435 out << "(bvnot ";
1436 convert_expr(bitnot_expr.op());
1437 out << ")";
1438 }
1439 else if(expr.id()==ID_unary_minus)
1440 {
1441 const unary_minus_exprt &unary_minus_expr = to_unary_minus_expr(expr);
1442 const auto &type = expr.type();
1443
1444 if(
1445 type.id() == ID_rational || type.id() == ID_integer ||
1446 type.id() == ID_real)
1447 {
1448 out << "(- ";
1449 convert_expr(unary_minus_expr.op());
1450 out << ")";
1451 }
1452 else if(type.id() == ID_range)
1453 {
1454 auto &range_type = to_integer_range_type(type);
1455 PRECONDITION(type == unary_minus_expr.op().type());
1456 // turn -x into 0-x
1457 auto minus_expr =
1458 minus_exprt{range_type.zero_expr(), unary_minus_expr.op()};
1459 convert_expr(minus_expr);
1460 }
1461 else if(type.id() == ID_floatbv)
1462 {
1463 // this has no rounding mode
1464 if(use_FPA_theory)
1465 {
1466 out << "(fp.neg ";
1467 convert_expr(unary_minus_expr.op());
1468 out << ")";
1469 }
1470 else
1471 convert_floatbv(unary_minus_expr);
1472 }
1473 else
1474 {
1475 PRECONDITION(type.id() != ID_natural);
1476 out << "(bvneg ";
1477 convert_expr(unary_minus_expr.op());
1478 out << ")";
1479 }
1480 }
1481 else if(expr.id()==ID_unary_plus)
1482 {
1483 // A no-op (apart from type promotion)
1484 convert_expr(to_unary_plus_expr(expr).op());
1485 }
1486 else if(expr.id()==ID_sign)
1487 {
1488 const sign_exprt &sign_expr = to_sign_expr(expr);
1489
1490 const typet &op_type = sign_expr.op().type();
1491
1492 if(op_type.id()==ID_floatbv)
1493 {
1494 if(use_FPA_theory)
1495 {
1496 out << "(fp.isNegative ";
1497 convert_expr(sign_expr.op());
1498 out << ")";
1499 }
1500 else
1501 convert_floatbv(sign_expr);
1502 }
1503 else if(op_type.id()==ID_signedbv)
1504 {
1505 std::size_t op_width=to_signedbv_type(op_type).get_width();
1506
1507 out << "(bvslt ";
1508 convert_expr(sign_expr.op());
1509 out << " (_ bv0 " << op_width << "))";
1510 }
1511 else
1513 false,
1514 "sign should not be applied to unsupported type",
1515 sign_expr.type().id_string());
1516 }
1517 else if(expr.id()==ID_if)
1518 {
1519 const if_exprt &if_expr = to_if_expr(expr);
1520
1521 out << "(ite ";
1522 convert_expr(if_expr.cond());
1523 out << " ";
1524 if(
1525 expr.type().id() == ID_array && !use_array_theory(if_expr.true_case()) &&
1526 use_array_theory(if_expr.false_case()))
1527 {
1528 unflatten(wheret::BEGIN, expr.type());
1529
1530 convert_expr(if_expr.true_case());
1531
1532 unflatten(wheret::END, expr.type());
1533 }
1534 else
1535 {
1536 convert_expr(if_expr.true_case());
1537 }
1538 out << " ";
1539 if(
1540 expr.type().id() == ID_array && use_array_theory(if_expr.true_case()) &&
1541 !use_array_theory(if_expr.false_case()))
1542 {
1543 unflatten(wheret::BEGIN, expr.type());
1544
1545 convert_expr(if_expr.false_case());
1546
1547 unflatten(wheret::END, expr.type());
1548 }
1549 else
1550 {
1551 convert_expr(if_expr.false_case());
1552 }
1553 out << ")";
1554 }
1555 else if(expr.id()==ID_and ||
1556 expr.id()==ID_or ||
1557 expr.id()==ID_xor)
1558 {
1560 expr.is_boolean(),
1561 "logical and, or, and xor expressions should have Boolean type");
1563 expr.operands().size() >= 2,
1564 "logical and, or, and xor expressions should have at least two operands");
1565
1566 out << "(" << expr.id();
1567 for(const auto &op : expr.operands())
1568 {
1569 out << " ";
1570 convert_expr(op);
1571 }
1572 out << ")";
1573 }
1574 else if(expr.id() == ID_nand || expr.id() == ID_nor || expr.id() == ID_xnor)
1575 {
1577 expr.is_boolean(),
1578 "logical nand, nor, xnor expressions should have Boolean type");
1580 expr.operands().size() >= 1,
1581 "logical nand, nor, xnor expressions should have at least one operand");
1582
1583 // SMT-LIB doesn't have nand, nor, xnor
1584 out << "(not ";
1585 if(expr.operands().size() == 1)
1586 convert_expr(to_multi_ary_expr(expr).op0());
1587 else
1588 {
1589 if(expr.id() == ID_nand)
1590 out << "(and";
1591 else if(expr.id() == ID_nor)
1592 out << "(or";
1593 else if(expr.id() == ID_xnor)
1594 out << "(xor";
1595 else
1596 DATA_INVARIANT(false, "unexpected expression");
1597 for(const auto &op : expr.operands())
1598 {
1599 out << ' ';
1600 convert_expr(op);
1601 }
1602 out << ')'; // or, and, xor
1603 }
1604 out << ')'; // not
1605 }
1606 else if(expr.id()==ID_implies)
1607 {
1608 const implies_exprt &implies_expr = to_implies_expr(expr);
1609
1611 implies_expr.is_boolean(), "implies expression should have Boolean type");
1612
1613 out << "(=> ";
1614 convert_expr(implies_expr.op0());
1615 out << " ";
1616 convert_expr(implies_expr.op1());
1617 out << ")";
1618 }
1619 else if(expr.id()==ID_not)
1620 {
1621 const not_exprt &not_expr = to_not_expr(expr);
1622
1624 not_expr.is_boolean(), "not expression should have Boolean type");
1625
1626 out << "(not ";
1627 convert_expr(not_expr.op());
1628 out << ")";
1629 }
1630 else if(expr.id() == ID_equal)
1631 {
1632 const equal_exprt &equal_expr = to_equal_expr(expr);
1633
1635 equal_expr.op0().type() == equal_expr.op1().type(),
1636 "operands of equal expression shall have same type");
1637
1638 if(is_zero_width(equal_expr.lhs().type(), ns))
1639 {
1641 }
1642 else
1643 {
1644 out << "(= ";
1645 convert_expr(equal_expr.op0());
1646 out << " ";
1647 convert_expr(equal_expr.op1());
1648 out << ")";
1649 }
1650 }
1651 else if(expr.id() == ID_notequal)
1652 {
1653 const notequal_exprt &notequal_expr = to_notequal_expr(expr);
1654
1656 notequal_expr.op0().type() == notequal_expr.op1().type(),
1657 "operands of not equal expression shall have same type");
1658
1659 out << "(not (= ";
1660 convert_expr(notequal_expr.op0());
1661 out << " ";
1662 convert_expr(notequal_expr.op1());
1663 out << "))";
1664 }
1665 else if(expr.id()==ID_ieee_float_equal ||
1666 expr.id()==ID_ieee_float_notequal)
1667 {
1668 // These are not the same as (= A B)
1669 // because of NaN and negative zero.
1670 const auto &rel_expr = to_binary_relation_expr(expr);
1671
1673 rel_expr.lhs().type() == rel_expr.rhs().type(),
1674 "operands of float equal and not equal expressions shall have same type");
1675
1676 // The FPA theory properly treats NaN and negative zero.
1677 if(use_FPA_theory)
1678 {
1679 if(rel_expr.id() == ID_ieee_float_notequal)
1680 out << "(not ";
1681
1682 out << "(fp.eq ";
1683 convert_expr(rel_expr.lhs());
1684 out << " ";
1685 convert_expr(rel_expr.rhs());
1686 out << ")";
1687
1688 if(rel_expr.id() == ID_ieee_float_notequal)
1689 out << ")";
1690 }
1691 else
1692 convert_floatbv(expr);
1693 }
1694 else if(expr.id()==ID_le ||
1695 expr.id()==ID_lt ||
1696 expr.id()==ID_ge ||
1697 expr.id()==ID_gt)
1698 {
1700 }
1701 else if(expr.id()==ID_plus)
1702 {
1704 }
1705 else if(expr.id()==ID_floatbv_plus)
1706 {
1708 }
1709 else if(expr.id()==ID_minus)
1710 {
1712 }
1713 else if(expr.id()==ID_floatbv_minus)
1714 {
1716 }
1717 else if(expr.id()==ID_div)
1718 {
1719 convert_div(to_div_expr(expr));
1720 }
1721 else if(expr.id()==ID_floatbv_div)
1722 {
1724 }
1725 else if(expr.id()==ID_mod)
1726 {
1727 convert_mod(to_mod_expr(expr));
1728 }
1729 else if(expr.id() == ID_euclidean_mod)
1730 {
1732 }
1733 else if(expr.id()==ID_mult)
1734 {
1736 }
1737 else if(expr.id()==ID_floatbv_mult)
1738 {
1740 }
1741 else if(expr.id() == ID_floatbv_rem)
1742 {
1744 }
1745 else if(expr.id() == ID_floatbv_fma)
1746 {
1748 }
1749 else if(expr.id()==ID_address_of)
1750 {
1751 const address_of_exprt &address_of_expr = to_address_of_expr(expr);
1753 address_of_expr.object(), to_pointer_type(address_of_expr.type()));
1754 }
1755 else if(expr.id() == ID_array_of)
1756 {
1757 const auto &array_of_expr = to_array_of_expr(expr);
1758
1760 array_of_expr.type().id() == ID_array,
1761 "array of expression shall have array type");
1762
1763 if(use_as_const)
1764 {
1765 out << "((as const ";
1766 convert_type(array_of_expr.type());
1767 out << ") ";
1768 convert_expr(array_of_expr.what());
1769 out << ")";
1770 }
1771 else
1772 {
1773 defined_expressionst::const_iterator it =
1774 defined_expressions.find(array_of_expr);
1775 CHECK_RETURN(it != defined_expressions.end());
1776 out << it->second;
1777 }
1778 }
1779 else if(expr.id() == ID_array_comprehension)
1780 {
1781 const auto &array_comprehension = to_array_comprehension_expr(expr);
1782
1784 array_comprehension.type().id() == ID_array,
1785 "array_comprehension expression shall have array type");
1786
1788 {
1789 out << "(lambda ((";
1790 convert_expr(array_comprehension.arg());
1791 out << " ";
1792 convert_type(array_comprehension.type().size().type());
1793 out << ")) ";
1794 convert_expr(array_comprehension.body());
1795 out << ")";
1796 }
1797 else
1798 {
1799 const auto &it = defined_expressions.find(array_comprehension);
1800 CHECK_RETURN(it != defined_expressions.end());
1801 out << it->second;
1802 }
1803 }
1804 else if(expr.id()==ID_index)
1805 {
1807 }
1808 else if(expr.id()==ID_ashr ||
1809 expr.id()==ID_lshr ||
1810 expr.id()==ID_shl)
1811 {
1812 const shift_exprt &shift_expr = to_shift_expr(expr);
1813 const typet &type = shift_expr.type();
1814
1815 if(type.id()==ID_unsignedbv ||
1816 type.id()==ID_signedbv ||
1817 type.id()==ID_bv)
1818 {
1819 if(shift_expr.id() == ID_ashr)
1820 out << "(bvashr ";
1821 else if(shift_expr.id() == ID_lshr)
1822 out << "(bvlshr ";
1823 else if(shift_expr.id() == ID_shl)
1824 out << "(bvshl ";
1825 else
1827
1828 convert_expr(shift_expr.op());
1829 out << " ";
1830
1831 // SMT2 requires the shift distance to have the same width as
1832 // the value that is shifted -- odd!
1833
1834 const auto &distance_type = shift_expr.distance().type();
1835 if(distance_type.id() == ID_integer || distance_type.id() == ID_natural)
1836 {
1837 const auto i =
1839
1840 // shift distance must be bit vector
1841 std::size_t width_op0 = boolbv_width(shift_expr.op().type());
1842 exprt tmp=from_integer(i, unsignedbv_typet(width_op0));
1843 convert_expr(tmp);
1844 }
1845 else if(
1846 distance_type.id() == ID_signedbv ||
1847 distance_type.id() == ID_unsignedbv ||
1848 distance_type.id() == ID_c_enum || distance_type.id() == ID_c_bool)
1849 {
1850 std::size_t width_op0 = boolbv_width(shift_expr.op().type());
1851 std::size_t width_op1 = boolbv_width(distance_type);
1852
1853 if(width_op0==width_op1)
1854 convert_expr(shift_expr.distance());
1855 else if(width_op0>width_op1)
1856 {
1857 out << "((_ zero_extend " << width_op0-width_op1 << ") ";
1858 convert_expr(shift_expr.distance());
1859 out << ")"; // zero_extend
1860 }
1861 else // width_op0<width_op1
1862 {
1863 out << "((_ extract " << width_op0-1 << " 0) ";
1864 convert_expr(shift_expr.distance());
1865 out << ")"; // extract
1866 }
1867 }
1868 else
1869 {
1871 "unsupported distance type for " + shift_expr.id_string() + ": " +
1872 distance_type.id_string());
1873 }
1874
1875 out << ")"; // bv*sh
1876 }
1877 else
1879 "unsupported type for " + shift_expr.id_string() + ": " +
1880 type.id_string());
1881 }
1882 else if(expr.id() == ID_rol || expr.id() == ID_ror)
1883 {
1884 const shift_exprt &shift_expr = to_shift_expr(expr);
1885 const typet &type = shift_expr.type();
1886
1887 if(
1888 type.id() == ID_unsignedbv || type.id() == ID_signedbv ||
1889 type.id() == ID_bv)
1890 {
1891 // SMT-LIB offers rotate_left and rotate_right, but these require a
1892 // constant distance.
1893 if(shift_expr.id() == ID_rol)
1894 out << "((_ rotate_left";
1895 else if(shift_expr.id() == ID_ror)
1896 out << "((_ rotate_right";
1897 else
1899
1900 out << ' ';
1901
1902 auto distance_int_op = numeric_cast<bitst>(shift_expr.distance());
1903
1904 if(distance_int_op.has_value())
1905 {
1906 out << distance_int_op.value();
1907 }
1908 else
1910 "distance type for " + shift_expr.id_string() + "must be constant");
1911
1912 out << ") ";
1913 convert_expr(shift_expr.op());
1914
1915 out << ")"; // rotate_*
1916 }
1917 else
1919 "unsupported type for " + shift_expr.id_string() + ": " +
1920 type.id_string());
1921 }
1922 else if(expr.id() == ID_named_term)
1923 {
1924 const auto &named_term_expr = to_named_term_expr(expr);
1925 out << "(! ";
1926 convert(named_term_expr.value());
1927 out << " :named "
1928 << convert_identifier(named_term_expr.symbol().identifier()) << ')';
1929 }
1930 else if(expr.id()==ID_with)
1931 {
1933 }
1934 else if(expr.id()==ID_update)
1935 {
1937 }
1938 else if(expr.id() == ID_update_bit)
1939 {
1941 }
1942 else if(expr.id() == ID_update_bits)
1943 {
1945 }
1946 else if(expr.id() == ID_object_address)
1947 {
1948 out << "(object-address ";
1950 id2string(to_object_address_expr(expr).object_identifier()));
1951 out << ')';
1952 }
1953 else if(expr.id() == ID_element_address)
1954 {
1955 // We turn this binary expression into a ternary expression
1956 // by adding the size of the array elements as third argument.
1957 const auto &element_address_expr = to_element_address_expr(expr);
1958
1959 auto element_size_expr_opt =
1960 ::size_of_expr(element_address_expr.element_type(), ns);
1961 CHECK_RETURN(element_size_expr_opt.has_value());
1962
1963 out << "(element-address-" << type2id(expr.type()) << ' ';
1964 convert_expr(element_address_expr.base());
1965 out << ' ';
1966 convert_expr(element_address_expr.index());
1967 out << ' ';
1969 *element_size_expr_opt, element_address_expr.index().type()));
1970 out << ')';
1971 }
1972 else if(expr.id() == ID_field_address)
1973 {
1974 const auto &field_address_expr = to_field_address_expr(expr);
1975 out << "(field-address-" << type2id(expr.type()) << ' ';
1976 convert_expr(field_address_expr.base());
1977 out << ' ';
1978 convert_string_literal(id2string(field_address_expr.component_name()));
1979 out << ')';
1980 }
1981 else if(expr.id()==ID_member)
1982 {
1984 }
1985 else if(expr.id()==ID_pointer_offset)
1986 {
1987 const auto &op = to_pointer_offset_expr(expr).pointer();
1988
1990 op.type().id() == ID_pointer,
1991 "operand of pointer offset expression shall be of pointer type");
1992
1993 std::size_t offset_bits =
1994 boolbv_width(op.type()) - config.bv_encoding.object_bits;
1995 std::size_t result_width=boolbv_width(expr.type());
1996
1997 // max extract width
1998 if(offset_bits>result_width)
1999 offset_bits=result_width;
2000
2001 // too few bits?
2002 if(result_width>offset_bits)
2003 out << "((_ zero_extend " << result_width-offset_bits << ") ";
2004
2005 out << "((_ extract " << offset_bits-1 << " 0) ";
2006 convert_expr(op);
2007 out << ")";
2008
2009 if(result_width>offset_bits)
2010 out << ")"; // zero_extend
2011 }
2012 else if(expr.id()==ID_pointer_object)
2013 {
2014 const auto &op = to_pointer_object_expr(expr).pointer();
2015
2017 op.type().id() == ID_pointer,
2018 "pointer object expressions should be of pointer type");
2019
2020 std::size_t ext=boolbv_width(expr.type())-config.bv_encoding.object_bits;
2021 std::size_t pointer_width = boolbv_width(op.type());
2022
2023 if(ext>0)
2024 out << "((_ zero_extend " << ext << ") ";
2025
2026 out << "((_ extract "
2027 << pointer_width-1 << " "
2028 << pointer_width-config.bv_encoding.object_bits << ") ";
2029 convert_expr(op);
2030 out << ")";
2031
2032 if(ext>0)
2033 out << ")"; // zero_extend
2034 }
2035 else if(expr.id() == ID_is_dynamic_object)
2036 {
2038 }
2039 else if(expr.id() == ID_is_invalid_pointer)
2040 {
2041 const auto &op = to_unary_expr(expr).op();
2042 std::size_t pointer_width = boolbv_width(op.type());
2043 out << "(= ((_ extract "
2044 << pointer_width-1 << " "
2045 << pointer_width-config.bv_encoding.object_bits << ") ";
2046 convert_expr(op);
2047 out << ") (_ bv" << pointer_logic.get_invalid_object()
2048 << " " << config.bv_encoding.object_bits << "))";
2049 }
2050 else if(expr.id()==ID_string_constant)
2051 {
2052 defined_expressionst::const_iterator it=defined_expressions.find(expr);
2053 CHECK_RETURN(it != defined_expressions.end());
2054 out << it->second;
2055 }
2056 else if(expr.id()==ID_extractbit)
2057 {
2058 const extractbit_exprt &extractbit_expr = to_extractbit_expr(expr);
2059
2060 if(extractbit_expr.index().is_constant())
2061 {
2062 const auto i =
2063 numeric_cast_v<bitst>(to_constant_expr(extractbit_expr.index()));
2064
2065 out << "(= ((_ extract " << i << " " << i << ") ";
2066 flatten2bv(extractbit_expr.src());
2067 out << ") #b1)";
2068 }
2069 else
2070 {
2071 // Shift the source right by the index, then check bit 0. The index is
2072 // a bit position: zero-extend both operands to the wider of the two
2073 // widths rather than using a typecast to the type of the source, which
2074 // would convert the value (e.g., scale for fixedbv, or map any non-zero
2075 // value to 1 for c_bool). Out-of-range indices then yield false.
2076 const std::size_t src_width = boolbv_width(extractbit_expr.src().type());
2077 const std::size_t idx_width =
2078 boolbv_width(extractbit_expr.index().type());
2079 const std::size_t width = std::max(src_width, idx_width);
2080
2081 out << "(= ((_ extract 0 0) ";
2082 out << "(bvlshr ";
2083 if(src_width < width)
2084 out << "((_ zero_extend " << width - src_width << ") ";
2085 flatten2bv(extractbit_expr.src());
2086 if(src_width < width)
2087 out << ')';
2088 out << ' ';
2089 if(idx_width < width)
2090 out << "((_ zero_extend " << width - idx_width << ") ";
2091 convert_expr(extractbit_expr.index());
2092 if(idx_width < width)
2093 out << ')';
2094 out << ")) #b1)"; // bvlshr, extract, =
2095 }
2096 }
2097 else if(expr.id() == ID_onehot)
2098 {
2099 convert_expr(to_onehot_expr(expr).lower());
2100 }
2101 else if(expr.id() == ID_onehot0)
2102 {
2103 convert_expr(to_onehot0_expr(expr).lower());
2104 }
2105 else if(expr.id()==ID_extractbits)
2106 {
2107 const extractbits_exprt &extractbits_expr = to_extractbits_expr(expr);
2108 auto width = boolbv_width(expr.type());
2109
2110 if(extractbits_expr.index().is_constant())
2111 {
2112 auto index_i =
2113 numeric_cast_v<bitst>(to_constant_expr(extractbits_expr.index()));
2114
2115 out << "((_ extract " << (bitst{width} + index_i - bitst{1}) << " "
2116 << index_i << ") ";
2117 flatten2bv(extractbits_expr.src());
2118 out << ")";
2119 }
2120 else
2121 {
2122 #if 0
2123 out << "(= ((_ extract 0 0) ";
2124 // the arguments of the shift need to have the same width
2125 out << "(bvlshr ";
2126 convert_expr(expr.op0());
2127 typecast_exprt tmp(expr.op0().type());
2128 tmp.op0()=expr.op1();
2129 convert_expr(tmp);
2130 out << ")) bin1)"; // bvlshr, extract, =
2131 #endif
2132 SMT2_TODO("smt2: extractbits with non-constant index");
2133 }
2134 }
2135 else if(expr.id()==ID_replication)
2136 {
2137 const replication_exprt &replication_expr = to_replication_expr(expr);
2138
2139 mp_integer times = numeric_cast_v<mp_integer>(replication_expr.times());
2140
2141 // SMT-LIB requires that repeat is given a number of repetitions that is at
2142 // least 1.
2143 PRECONDITION(times >= 1);
2144
2145 out << "((_ repeat " << times << ") ";
2146 flatten2bv(replication_expr.op());
2147 out << ")";
2148 }
2149 else if(expr.id()==ID_byte_extract_little_endian ||
2150 expr.id()==ID_byte_extract_big_endian)
2151 {
2152 INVARIANT(
2153 false, "byte_extract ops should be lowered in prepare_for_convert_expr");
2154 }
2155 else if(expr.id()==ID_byte_update_little_endian ||
2156 expr.id()==ID_byte_update_big_endian)
2157 {
2158 INVARIANT(
2159 false, "byte_update ops should be lowered in prepare_for_convert_expr");
2160 }
2161 else if(expr.id()==ID_abs)
2162 {
2163 const abs_exprt &abs_expr = to_abs_expr(expr);
2164
2165 const typet &type = abs_expr.type();
2166
2167 if(type.id()==ID_signedbv)
2168 {
2169 std::size_t result_width = to_signedbv_type(type).get_width();
2170
2171 out << "(ite (bvslt ";
2172 convert_expr(abs_expr.op());
2173 out << " (_ bv0 " << result_width << ")) ";
2174 out << "(bvneg ";
2175 convert_expr(abs_expr.op());
2176 out << ") ";
2177 convert_expr(abs_expr.op());
2178 out << ")";
2179 }
2180 else if(type.id()==ID_fixedbv)
2181 {
2182 std::size_t result_width=to_fixedbv_type(type).get_width();
2183
2184 out << "(ite (bvslt ";
2185 convert_expr(abs_expr.op());
2186 out << " (_ bv0 " << result_width << ")) ";
2187 out << "(bvneg ";
2188 convert_expr(abs_expr.op());
2189 out << ") ";
2190 convert_expr(abs_expr.op());
2191 out << ")";
2192 }
2193 else if(type.id()==ID_floatbv)
2194 {
2195 if(use_FPA_theory)
2196 {
2197 out << "(fp.abs ";
2198 convert_expr(abs_expr.op());
2199 out << ")";
2200 }
2201 else
2202 convert_floatbv(abs_expr);
2203 }
2204 else
2206 }
2207 else if(expr.id()==ID_isnan)
2208 {
2209 const isnan_exprt &isnan_expr = to_isnan_expr(expr);
2210
2211 const typet &op_type = isnan_expr.op().type();
2212
2213 if(op_type.id()==ID_fixedbv)
2214 out << "false";
2215 else if(op_type.id()==ID_floatbv)
2216 {
2217 if(use_FPA_theory)
2218 {
2219 out << "(fp.isNaN ";
2220 convert_expr(isnan_expr.op());
2221 out << ")";
2222 }
2223 else
2224 convert_floatbv(isnan_expr);
2225 }
2226 else
2228 }
2229 else if(expr.id()==ID_isfinite)
2230 {
2231 const isfinite_exprt &isfinite_expr = to_isfinite_expr(expr);
2232
2233 const typet &op_type = isfinite_expr.op().type();
2234
2235 if(op_type.id()==ID_fixedbv)
2236 out << "true";
2237 else if(op_type.id()==ID_floatbv)
2238 {
2239 if(use_FPA_theory)
2240 {
2241 out << "(and ";
2242
2243 out << "(not (fp.isNaN ";
2244 convert_expr(isfinite_expr.op());
2245 out << "))";
2246
2247 out << "(not (fp.isInfinite ";
2248 convert_expr(isfinite_expr.op());
2249 out << "))";
2250
2251 out << ")";
2252 }
2253 else
2254 convert_floatbv(isfinite_expr);
2255 }
2256 else
2258 }
2259 else if(expr.id()==ID_isinf)
2260 {
2261 const isinf_exprt &isinf_expr = to_isinf_expr(expr);
2262
2263 const typet &op_type = isinf_expr.op().type();
2264
2265 if(op_type.id()==ID_fixedbv)
2266 out << "false";
2267 else if(op_type.id()==ID_floatbv)
2268 {
2269 if(use_FPA_theory)
2270 {
2271 out << "(fp.isInfinite ";
2272 convert_expr(isinf_expr.op());
2273 out << ")";
2274 }
2275 else
2276 convert_floatbv(isinf_expr);
2277 }
2278 else
2280 }
2281 else if(expr.id()==ID_isnormal)
2282 {
2283 const isnormal_exprt &isnormal_expr = to_isnormal_expr(expr);
2284
2285 const typet &op_type = isnormal_expr.op().type();
2286
2287 if(op_type.id()==ID_fixedbv)
2288 out << "true";
2289 else if(op_type.id()==ID_floatbv)
2290 {
2291 if(use_FPA_theory)
2292 {
2293 out << "(fp.isNormal ";
2294 convert_expr(isnormal_expr.op());
2295 out << ")";
2296 }
2297 else
2298 convert_floatbv(isnormal_expr);
2299 }
2300 else
2302 }
2303 else if(
2306 expr.id() == ID_overflow_result_plus ||
2307 expr.id() == ID_overflow_result_minus)
2308 {
2309 const bool keep_result = can_cast_expr<overflow_result_exprt>(expr);
2310
2311 const auto &op0 = to_binary_expr(expr).op0();
2312 const auto &op1 = to_binary_expr(expr).op1();
2313
2315 keep_result || expr.is_boolean(),
2316 "overflow plus and overflow minus expressions shall be of Boolean type");
2317
2318 bool subtract = can_cast_expr<minus_overflow_exprt>(expr) ||
2319 expr.id() == ID_overflow_result_minus;
2320 const typet &op_type = op0.type();
2321 std::size_t width=boolbv_width(op_type);
2322
2323 if(op_type.id()==ID_signedbv)
2324 {
2325 // an overflow occurs if the top two bits of the extended sum differ
2326 out << "(let ((?sum (";
2327 out << (subtract?"bvsub":"bvadd");
2328 out << " ((_ sign_extend 1) ";
2329 convert_expr(op0);
2330 out << ")";
2331 out << " ((_ sign_extend 1) ";
2332 convert_expr(op1);
2333 out << ")))) "; // sign_extend, bvadd/sub
2334
2335 const std::string overflow_condition =
2336 "(not (= ((_ extract " + std::to_string(width) + " " +
2337 std::to_string(width) + ") ?sum) ((_ extract " +
2338 std::to_string(width - 1) + " " + std::to_string(width - 1) +
2339 ") ?sum)))";
2340
2341 if(keep_result)
2342 {
2343 const std::string result =
2344 "((_ extract " + std::to_string(width - 1) + " 0) ?sum)";
2345
2346 if(use_datatypes)
2347 {
2348 const std::string &smt_typename = datatype_map.at(expr.type());
2349
2350 // use the constructor for the Z3 datatype
2351 out << "(mk-" << smt_typename << ' ' << result << ' '
2352 << overflow_condition << ')';
2353 }
2354 else
2355 {
2356 // The struct is flattened into a bit-vector. The first component
2357 // (the result) occupies the least-significant bits, so the overflow
2358 // flag is the first operand of the concatenation -- cf. flatten2bv,
2359 // convert_struct and convert_member.
2360 out << "(concat (ite " << overflow_condition << " #b1 #b0) " << result
2361 << ')';
2362 }
2363 }
2364 else
2365 out << overflow_condition;
2366
2367 out << ")"; // let
2368 }
2369 else if(op_type.id()==ID_unsignedbv ||
2370 op_type.id()==ID_pointer)
2371 {
2372 // overflow is simply carry-out
2373 out << "(let ((?sum (" << (subtract ? "bvsub" : "bvadd");
2374 out << " ((_ zero_extend 1) ";
2375 convert_expr(op0);
2376 out << ")";
2377 out << " ((_ zero_extend 1) ";
2378 convert_expr(op1);
2379 out << "))))"; // zero_extend, bvsub/bvadd
2380 if(keep_result && !use_datatypes)
2381 {
2382 // ?sum is already the correct flattening of the struct: the result
2383 // occupies the least-significant `width` bits and the carry-out (the
2384 // overflow flag) is the most-significant bit.
2385 out << " ?sum";
2386 }
2387 else
2388 {
2389 if(keep_result && use_datatypes)
2390 {
2391 const std::string &smt_typename = datatype_map.at(expr.type());
2392
2393 // use the constructor for the Z3 datatype
2394 out << "(mk-" << smt_typename;
2395 out << " ((_ extract " << width - 1 << " 0) ?sum) ";
2396 }
2397
2398 out << "(= ";
2399 out << "((_ extract " << width << " " << width << ") ?sum)";
2400 out << "#b1)"; // =
2401
2402 if(keep_result && use_datatypes)
2403 out << ")"; // mk
2404 }
2405 out << ")"; // let
2406 }
2407 else
2409 false,
2410 "overflow check should not be performed on unsupported type",
2411 op_type.id_string());
2412 }
2413 else if(
2415 expr.id() == ID_overflow_result_mult)
2416 {
2417 const bool keep_result = can_cast_expr<overflow_result_exprt>(expr);
2418
2419 const auto &op0 = to_binary_expr(expr).op0();
2420 const auto &op1 = to_binary_expr(expr).op1();
2421
2423 keep_result || expr.is_boolean(),
2424 "overflow mult expression shall be of Boolean type");
2425
2426 // No better idea than to multiply with double the bits and then compare
2427 // with max value.
2428
2429 const typet &op_type = op0.type();
2430 std::size_t width=boolbv_width(op_type);
2431
2432 if(op_type.id()==ID_signedbv)
2433 {
2434 out << "(let ( (prod (bvmul ((_ sign_extend " << width << ") ";
2435 convert_expr(op0);
2436 out << ") ((_ sign_extend " << width << ") ";
2437 convert_expr(op1);
2438 out << ")) )) ";
2439
2440 const std::string bound = "(_ bv" + integer2string(power(2, width - 1)) +
2441 " " + std::to_string(width * 2) + ")";
2442 const std::string overflow_condition =
2443 "(or (bvsge prod " + bound + ") (bvslt prod (bvneg " + bound + ")))";
2444
2445 if(keep_result)
2446 {
2447 const std::string result =
2448 "((_ extract " + std::to_string(width - 1) + " 0) prod)";
2449
2450 if(use_datatypes)
2451 {
2452 const std::string &smt_typename = datatype_map.at(expr.type());
2453
2454 // use the constructor for the Z3 datatype
2455 out << "(mk-" << smt_typename << ' ' << result << ' '
2456 << overflow_condition << ')';
2457 }
2458 else
2459 {
2460 // The struct is flattened into a bit-vector. The first component
2461 // (the result) occupies the least-significant bits, so the overflow
2462 // flag is the first operand of the concatenation -- cf. flatten2bv,
2463 // convert_struct and convert_member.
2464 out << "(concat (ite " << overflow_condition << " #b1 #b0) " << result
2465 << ')';
2466 }
2467 }
2468 else
2469 out << overflow_condition;
2470
2471 out << ")"; // let
2472 }
2473 else if(op_type.id()==ID_unsignedbv)
2474 {
2475 out << "(let ((prod (bvmul ((_ zero_extend " << width << ") ";
2476 convert_expr(op0);
2477 out << ") ((_ zero_extend " << width << ") ";
2478 convert_expr(op1);
2479 out << ")))) ";
2480
2481 const std::string overflow_condition =
2482 "(bvuge prod (_ bv" + integer2string(power(2, width)) + " " +
2483 std::to_string(width * 2) + "))";
2484
2485 if(keep_result)
2486 {
2487 const std::string result =
2488 "((_ extract " + std::to_string(width - 1) + " 0) prod)";
2489
2490 if(use_datatypes)
2491 {
2492 const std::string &smt_typename = datatype_map.at(expr.type());
2493
2494 // use the constructor for the Z3 datatype
2495 out << "(mk-" << smt_typename << ' ' << result << ' '
2496 << overflow_condition << ')';
2497 }
2498 else
2499 {
2500 // The struct is flattened into a bit-vector. The first component
2501 // (the result) occupies the least-significant bits, so the overflow
2502 // flag is the first operand of the concatenation -- cf. flatten2bv,
2503 // convert_struct and convert_member.
2504 out << "(concat (ite " << overflow_condition << " #b1 #b0) " << result
2505 << ')';
2506 }
2507 }
2508 else
2509 out << overflow_condition;
2510
2511 out << ")"; // let
2512 }
2513 else
2515 false,
2516 "overflow check should not be performed on unsupported type",
2517 op_type.id_string());
2518 }
2519 else if(expr.id() == ID_saturating_plus || expr.id() == ID_saturating_minus)
2520 {
2521 const bool subtract = expr.id() == ID_saturating_minus;
2522 const auto &op_type = expr.type();
2523 const auto &op0 = to_binary_expr(expr).op0();
2524 const auto &op1 = to_binary_expr(expr).op1();
2525
2526 if(op_type.id() == ID_signedbv)
2527 {
2528 auto width = to_signedbv_type(op_type).get_width();
2529
2530 // compute sum with one extra bit
2531 out << "(let ((?sum (";
2532 out << (subtract ? "bvsub" : "bvadd");
2533 out << " ((_ sign_extend 1) ";
2534 convert_expr(op0);
2535 out << ")";
2536 out << " ((_ sign_extend 1) ";
2537 convert_expr(op1);
2538 out << ")))) "; // sign_extend, bvadd/sub
2539
2540 // pick one of MAX, MIN, or the sum
2541 out << "(ite (= "
2542 "((_ extract "
2543 << width << " " << width
2544 << ") ?sum) "
2545 "((_ extract "
2546 << (width - 1) << " " << (width - 1) << ") ?sum)";
2547 out << ") "; // =
2548
2549 // no overflow and no underflow case, return the sum
2550 out << "((_ extract " << width - 1 << " 0) ?sum) ";
2551
2552 // MAX
2553 out << "(ite (= ((_ extract " << width << " " << width << ") ?sum) #b0) ";
2554 convert_expr(to_signedbv_type(op_type).largest_expr());
2555
2556 // MIN
2557 convert_expr(to_signedbv_type(op_type).smallest_expr());
2558 out << ")))"; // ite, ite, let
2559 }
2560 else if(op_type.id() == ID_unsignedbv)
2561 {
2562 auto width = to_unsignedbv_type(op_type).get_width();
2563
2564 // compute sum with one extra bit
2565 out << "(let ((?sum (" << (subtract ? "bvsub" : "bvadd");
2566 out << " ((_ zero_extend 1) ";
2567 convert_expr(op0);
2568 out << ")";
2569 out << " ((_ zero_extend 1) ";
2570 convert_expr(op1);
2571 out << "))))"; // zero_extend, bvsub/bvadd
2572
2573 // pick one of MAX, MIN, or the sum
2574 out << "(ite (= ((_ extract " << width << " " << width << ") ?sum) #b0) ";
2575
2576 // no overflow and no underflow case, return the sum
2577 out << " ((_ extract " << width - 1 << " 0) ?sum) ";
2578
2579 // overflow when adding, underflow when subtracting
2580 if(subtract)
2581 convert_expr(to_unsignedbv_type(op_type).smallest_expr());
2582 else
2583 convert_expr(to_unsignedbv_type(op_type).largest_expr());
2584
2585 // MIN
2586 out << "))"; // ite, let
2587 }
2588 else
2590 false,
2591 "saturating_plus/minus on unsupported type",
2592 op_type.id_string());
2593 }
2594 else if(expr.id()==ID_array)
2595 {
2596 defined_expressionst::const_iterator it=defined_expressions.find(expr);
2597 CHECK_RETURN(it != defined_expressions.end());
2598 out << it->second;
2599 }
2600 else if(expr.id()==ID_literal)
2601 {
2602 convert_literal(to_literal_expr(expr).get_literal());
2603 }
2604 else if(expr.id()==ID_forall ||
2605 expr.id()==ID_exists)
2606 {
2607 const quantifier_exprt &quantifier_expr = to_quantifier_expr(expr);
2608
2610 // NOLINTNEXTLINE(readability/throw)
2611 throw "MathSAT does not support quantifiers";
2612
2613 if(quantifier_expr.id() == ID_forall)
2614 out << "(forall ";
2615 else if(quantifier_expr.id() == ID_exists)
2616 out << "(exists ";
2617
2618 out << '(';
2619 bool first = true;
2620 for(const auto &bound : quantifier_expr.variables())
2621 {
2622 if(first)
2623 first = false;
2624 else
2625 out << ' ';
2626 out << '(';
2627 convert_expr(bound);
2628 out << ' ';
2629 convert_type(bound.type());
2630 out << ')';
2631 }
2632 out << ") ";
2633
2634 convert_expr(quantifier_expr.where());
2635
2636 out << ')';
2637 }
2638 else if(
2640 {
2642 }
2643 else if(expr.id()==ID_let)
2644 {
2645 const let_exprt &let_expr=to_let_expr(expr);
2646 const auto &variables = let_expr.variables();
2647 const auto &values = let_expr.values();
2648
2649 out << "(let (";
2650 bool first = true;
2651
2652 for(auto &binding : make_range(variables).zip(values))
2653 {
2654 if(first)
2655 first = false;
2656 else
2657 out << ' ';
2658
2659 out << '(';
2660 convert_expr(binding.first);
2661 out << ' ';
2662 convert_expr(binding.second);
2663 out << ')';
2664 }
2665
2666 out << ") "; // bindings
2667
2668 convert_expr(let_expr.where());
2669 out << ')'; // let
2670 }
2671 else if(expr.id()==ID_constraint_select_one)
2672 {
2674 "smt2_convt::convert_expr: '" + expr.id_string() +
2675 "' is not yet supported");
2676 }
2677 else if(expr.id() == ID_bswap)
2678 {
2679 const bswap_exprt &bswap_expr = to_bswap_expr(expr);
2680
2682 bswap_expr.op().type() == bswap_expr.type(),
2683 "operand of byte swap expression shall have same type as the expression");
2684
2685 // first 'let' the operand
2686 out << "(let ((bswap_op ";
2687 convert_expr(bswap_expr.op());
2688 out << ")) ";
2689
2690 if(
2691 bswap_expr.type().id() == ID_signedbv ||
2692 bswap_expr.type().id() == ID_unsignedbv)
2693 {
2694 const std::size_t width =
2695 to_bitvector_type(bswap_expr.type()).get_width();
2696
2697 const std::size_t bits_per_byte = bswap_expr.get_bits_per_byte();
2698
2699 // width must be multiple of bytes
2701 width % bits_per_byte == 0,
2702 "bit width indicated by type of bswap expression should be a multiple "
2703 "of the number of bits per byte");
2704
2705 const std::size_t bytes = width / bits_per_byte;
2706
2707 if(bytes <= 1)
2708 out << "bswap_op";
2709 else
2710 {
2711 // do a parallel 'let' for each byte
2712 out << "(let (";
2713
2714 for(std::size_t byte = 0; byte < bytes; byte++)
2715 {
2716 if(byte != 0)
2717 out << ' ';
2718 out << "(bswap_byte_" << byte << ' ';
2719 out << "((_ extract " << (byte * bits_per_byte + (bits_per_byte - 1))
2720 << " " << (byte * bits_per_byte) << ") bswap_op)";
2721 out << ')';
2722 }
2723
2724 out << ") ";
2725
2726 // now stitch back together with 'concat'
2727 out << "(concat";
2728
2729 for(std::size_t byte = 0; byte < bytes; byte++)
2730 out << " bswap_byte_" << byte;
2731
2732 out << ')'; // concat
2733 out << ')'; // let bswap_byte_*
2734 }
2735 }
2736 else
2737 UNEXPECTEDCASE("bswap must get bitvector operand");
2738
2739 out << ')'; // let bswap_op
2740 }
2741 else if(expr.id() == ID_popcount)
2742 {
2744 }
2745 else if(expr.id() == ID_count_leading_zeros)
2746 {
2748 }
2749 else if(expr.id() == ID_count_trailing_zeros)
2750 {
2752 }
2753 else if(expr.id() == ID_find_first_set)
2754 {
2756 }
2757 else if(expr.id() == ID_bitreverse)
2758 {
2760 }
2761 else if(expr.id() == ID_zero_extend)
2762 {
2763 convert_expr(to_zero_extend_expr(expr).lower());
2764 }
2765 else if(expr.id() == ID_function_application)
2766 {
2767 const auto &function_application_expr = to_function_application_expr(expr);
2768
2769 // Front-ends such as Strata represent string and regex operations as
2770 // applications of the CPROVER string built-in functions
2771 // (ID_cprover_string_*_func / ID_cprover_regex_*_func). In the SMT-LIB
2772 // back-end these carry SMT-LIB-native operands (String / RegLan, and
2773 // scalar Int/bit-vector index arguments) -- as opposed to the
2774 // refined-string (length, char-array) representation that the SAT string
2775 // solver in src/solvers/strings consumes -- and we lower them directly to
2776 // the SMT-LIB theory-of-strings operators here.
2777 //
2778 // Operand contract (what a front-end must emit): arguments are SMT-LIB
2779 // native and in SMT-LIB operand order, so most built-ins lower to a flat
2780 // application (op arg0 arg1 ...). Specifically:
2781 // concat(s, t, ...) -> (str.++ s t ...)
2782 // length(s) -> (str.len s) : Int result
2783 // char_at(s, i) -> (str.at s i) : 1-char String
2784 // substring(s, off, len) -> (str.substr s off len) : offset+length
2785 // contains(haystack, needle) -> (str.contains haystack needle)
2786 // is_prefix(prefix, s) -> (str.prefixof prefix s)
2787 // is_suffix(suffix, s) -> (str.suffixof suffix s)
2788 // replace(s, old, new) -> (str.replace s old new) : first match
2789 // equal(s, t) -> (= s t)
2790 // index_of(s, sub[, off]) -> (str.indexof s sub off) : off defaults 0
2791 // startswith(s, prefix) -> (str.prefixof prefix s)
2792 // endswith(s, suffix) -> (str.suffixof suffix s)
2793 // is_empty(s) -> (= s "")
2794 // and the regex operators map to str.to_re / str.in_re and the re.* family
2795 // (re.range, re.++, re.*, re.+, re.opt, re.loop, re.union, re.inter,
2796 // re.comp, re.diff, re.all, re.allchar, re.none). Built-ins without an
2797 // exact SMT-LIB theory-of-strings counterpart (e.g. compare_to, of_int,
2798 // parse_int, format, case folding, trim) are intentionally not lowered and
2799 // fall through to the generic function-application handling below.
2800 //
2801 // Note: this lowering is specific to the (non-incremental) smt2_convt
2802 // back-end. The incremental SMT back-end uses a separate converter
2803 // (src/solvers/smt2_incremental) and does not support these built-ins.
2804 irep_idt fn_id;
2805 if(function_application_expr.function().id() == ID_symbol)
2806 fn_id =
2807 to_symbol_expr(function_application_expr.function()).get_identifier();
2808
2809 const auto &args = function_application_expr.arguments();
2810
2811 // Flat-application operators: lowered to (op arg0 arg1 ...) with operands
2812 // in the order given. The operands are interpreted in SMT-LIB order, which
2813 // can DIVERGE from the canonical SAT string solver's built-ins -- notably
2814 // substring is (string, offset, length) per SMT-LIB str.substr, NOT the
2815 // SAT solver's (string, start, end), and index_of's optional third operand
2816 // is a start offset. A front-end must emit these built-ins with SMT-LIB
2817 // operand semantics; the soundness guard below only catches refined-string
2818 // (array/pointer) operands, not a mismatched integer operand pair.
2819 static const std::map<irep_idt, std::string> flat_string_ops = {
2820 // string operators
2821 {ID_cprover_string_concat_func, "str.++"},
2822 {ID_cprover_string_length_func, "str.len"},
2823 {ID_cprover_string_substring_func, "str.substr"},
2824 {ID_cprover_string_char_at_func, "str.at"},
2825 {ID_cprover_string_contains_func, "str.contains"},
2826 {ID_cprover_string_is_prefix_func, "str.prefixof"},
2827 {ID_cprover_string_is_suffix_func, "str.suffixof"},
2828 {ID_cprover_string_replace_func, "str.replace"},
2829 {ID_cprover_string_equal_func, "="},
2830 // regex operators
2831 {ID_cprover_string_to_regex_func, "str.to_re"},
2832 {ID_cprover_string_in_regex_func, "str.in_re"},
2833 {ID_cprover_regex_range_func, "re.range"},
2834 {ID_cprover_regex_concat_func, "re.++"},
2835 {ID_cprover_regex_star_func, "re.*"},
2836 {ID_cprover_regex_plus_func, "re.+"},
2837 {ID_cprover_regex_opt_func, "re.opt"},
2838 {ID_cprover_regex_union_func, "re.union"},
2839 {ID_cprover_regex_inter_func, "re.inter"},
2840 {ID_cprover_regex_comp_func, "re.comp"},
2841 {ID_cprover_regex_diff_func, "re.diff"},
2842 {ID_cprover_regex_all_func, "re.all"},
2843 {ID_cprover_regex_allchar_func, "re.allchar"},
2844 {ID_cprover_regex_none_func, "re.none"}};
2845
2846 std::string smt_name;
2847 if(auto it = flat_string_ops.find(fn_id); it != flat_string_ops.end())
2848 smt_name = it->second;
2849
2850 // Operators needing a fixed operand rearrangement or wrapper.
2851 const bool is_index_of = fn_id == ID_cprover_string_index_of_func;
2852 const bool is_startswith = fn_id == ID_cprover_string_startswith_func;
2853 const bool is_endswith = fn_id == ID_cprover_string_endswith_func;
2854 const bool is_is_empty = fn_id == ID_cprover_string_is_empty_func;
2855 const bool is_regex_loop = fn_id == ID_cprover_regex_loop_func;
2856
2857 if(
2858 !smt_name.empty() || is_index_of || is_startswith || is_endswith ||
2859 is_is_empty || is_regex_loop)
2860 {
2861 // Soundness guard: this native lowering expects SMT-LIB-native operands.
2862 // The same cprover_string_*_func ids are also used with the
2863 // refined-string (length, char-array) representation consumed by the SAT
2864 // string solver; such an application carries array- or pointer-typed
2865 // operands and must NOT be lowered here, as doing so would be silently
2866 // unsound. Reject it with a clear message instead.
2867 for(const auto &arg : args)
2868 {
2869 if(arg.type().id() == ID_array || arg.type().id() == ID_pointer)
2870 {
2872 "string/regex built-in '" + id2string(fn_id) +
2873 "' reached the SMT-LIB string lowering with a refined-string "
2874 "(array/pointer) operand; the refined-string representation "
2875 "requires the SAT string solver (--refine-strings)");
2876 }
2877 }
2878
2879 if(is_index_of)
2880 {
2881 // str.indexof requires three operands; default the start offset to the
2882 // SMT-LIB Int 0 when the front-end omits it.
2883 PRECONDITION(args.size() == 2 || args.size() == 3);
2884 out << "(str.indexof ";
2885 convert_expr(args[0]);
2886 out << ' ';
2887 convert_expr(args[1]);
2888 out << ' ';
2889 if(args.size() == 3)
2890 convert_expr(args[2]);
2891 else
2892 out << '0';
2893 out << ')';
2894 }
2895 else if(is_startswith || is_endswith)
2896 {
2897 // startswith(s, prefix) / endswith(s, suffix): the affix is the second
2898 // argument, but SMT-LIB str.prefixof/str.suffixof take the affix first.
2899 PRECONDITION(args.size() == 2);
2900 out << (is_startswith ? "(str.prefixof " : "(str.suffixof ");
2901 convert_expr(args[1]);
2902 out << ' ';
2903 convert_expr(args[0]);
2904 out << ')';
2905 }
2906 else if(is_is_empty)
2907 {
2908 // is_empty(s) -> (= s "")
2909 PRECONDITION(args.size() == 1);
2910 out << "(= ";
2911 convert_expr(args[0]);
2912 out << " \"\")";
2913 }
2914 else if(is_regex_loop)
2915 {
2916 // re.loop is an indexed SMT-LIB operator: ((_ re.loop lo hi) r), where
2917 // lo and hi are numerals. Contract: loop(regex, lo, hi) with lo/hi
2918 // constant naturals.
2919 PRECONDITION(args.size() == 3);
2920 const auto lo = numeric_cast_v<mp_integer>(to_constant_expr(args[1]));
2921 const auto hi = numeric_cast_v<mp_integer>(to_constant_expr(args[2]));
2922 out << "((_ re.loop " << lo << ' ' << hi << ") ";
2923 convert_expr(args[0]);
2924 out << ')';
2925 }
2926 else if(args.empty())
2927 out << smt_name;
2928 else
2929 {
2930 out << '(' << smt_name;
2931 for(const auto &arg : args)
2932 {
2933 out << ' ';
2934 convert_expr(arg);
2935 }
2936 out << ')';
2937 }
2938 }
2939 // do not use parentheses if there function is a constant
2940 else if(function_application_expr.arguments().empty())
2941 {
2942 convert_expr(function_application_expr.function());
2943 }
2944 else
2945 {
2946 out << '(';
2947 convert_expr(function_application_expr.function());
2948 for(auto &op : function_application_expr.arguments())
2949 {
2950 out << ' ';
2951 convert_expr(op);
2952 }
2953 out << ')';
2954 }
2955 }
2956 else if(expr.id() == ID_cond)
2957 {
2958 // use the lowering
2959 convert_expr(to_cond_expr(expr).lower());
2960 }
2961 else if(expr.id() == ID_reduction_and)
2962 {
2963 convert_expr(to_reduction_and_expr(expr).lower());
2964 }
2965 else if(expr.id() == ID_reduction_nand)
2966 {
2967 convert_expr(to_reduction_nand_expr(expr).lower());
2968 }
2969 else if(expr.id() == ID_reduction_or)
2970 {
2971 convert_expr(to_reduction_or_expr(expr).lower());
2972 }
2973 else if(expr.id() == ID_reduction_nor)
2974 {
2975 convert_expr(to_reduction_nor_expr(expr).lower());
2976 }
2977 else if(expr.id() == ID_reduction_xor)
2978 {
2979 // This is the parity of the operand. No SMT-LIB 2 equivalent.
2980 // Do bit-wise. SMT-LIB 3.0 could do this with "fold bvxor".
2981 auto &op = to_reduction_xor_expr(expr).op();
2982 auto width = to_bitvector_type(op.type()).get_width();
2983 PRECONDITION(width >= 1);
2984
2985 if(width == 1)
2986 {
2987 out << "(= ";
2988 flatten2bv(op);
2989 out << " #b1)";
2990 }
2991 else
2992 {
2993 out << "(let ((?rop ";
2994 flatten2bv(op);
2995 out << ")) ";
2996
2997 // XOR all bits: extract each bit and use multi-ary bvxor
2998 out << "(= (bvxor";
2999 for(std::size_t i = 0; i < width; i++)
3000 out << " ((_ extract " << i << " " << i << ") ?rop)";
3001 out << ") #b1)";
3002
3003 out << ')'; // let
3004 }
3005 }
3006 else if(expr.id() == ID_reduction_xnor)
3007 {
3008 // This is the negation of "reduction xor"
3009 auto &op = to_reduction_xnor_expr(expr).op();
3011 }
3012 else
3014 false,
3015 "smt2_convt::convert_expr should not be applied to unsupported "
3016 "expression",
3017 expr.id_string());
3018}
3019
3021{
3022 const exprt &src = expr.op();
3023
3024 typet dest_type = expr.type();
3025
3026 if(dest_type == src.type()) // identity
3027 {
3028 convert_expr(src);
3029 return;
3030 }
3031
3032 if(dest_type.id()==ID_c_enum_tag)
3033 dest_type=ns.follow_tag(to_c_enum_tag_type(dest_type));
3034
3035 typet src_type = src.type();
3036 if(src_type.id()==ID_c_enum_tag)
3037 src_type=ns.follow_tag(to_c_enum_tag_type(src_type));
3038
3039 if(dest_type.id()==ID_bool)
3040 {
3041 // this is comparison with zero
3042 if(
3043 src_type.id() == ID_signedbv || src_type.id() == ID_unsignedbv ||
3044 src_type.id() == ID_c_bool || src_type.id() == ID_fixedbv ||
3045 src_type.id() == ID_pointer || src_type.id() == ID_integer ||
3046 src_type.id() == ID_natural || src_type.id() == ID_rational ||
3047 src_type.id() == ID_real)
3048 {
3049 out << "(not (= ";
3050 convert_expr(src);
3051 out << " ";
3052 convert_expr(from_integer(0, src_type));
3053 out << "))";
3054 }
3055 else if(src_type.id()==ID_floatbv)
3056 {
3057 if(use_FPA_theory)
3058 {
3059 out << "(not (fp.isZero ";
3060 convert_expr(src);
3061 out << "))";
3062 }
3063 else
3064 convert_floatbv(expr);
3065 }
3066 else
3067 {
3068 UNEXPECTEDCASE("TODO typecast1 "+src_type.id_string()+" -> bool");
3069 }
3070 }
3071 else if(dest_type.id()==ID_c_bool)
3072 {
3073 std::size_t to_width=boolbv_width(dest_type);
3074 out << "(ite ";
3075 out << "(not (= ";
3076 convert_expr(src);
3077 out << " ";
3078 convert_expr(from_integer(0, src_type));
3079 out << "))"; // not, =
3080 out << " (_ bv1 " << to_width << ")";
3081 out << " (_ bv0 " << to_width << ")";
3082 out << ")"; // ite
3083 }
3084 else if(dest_type.id()==ID_signedbv ||
3085 dest_type.id()==ID_unsignedbv ||
3086 dest_type.id()==ID_c_enum ||
3087 dest_type.id()==ID_bv)
3088 {
3089 std::size_t to_width=boolbv_width(dest_type);
3090
3091 if(src_type.id()==ID_signedbv || // from signedbv
3092 src_type.id()==ID_unsignedbv || // from unsigedbv
3093 src_type.id()==ID_c_bool ||
3094 src_type.id()==ID_c_enum ||
3095 src_type.id()==ID_bv)
3096 {
3097 std::size_t from_width=boolbv_width(src_type);
3098
3099 if(from_width==to_width)
3100 convert_expr(src); // ignore
3101 else if(from_width<to_width) // extend
3102 {
3103 if(
3104 src_type.id() == ID_signedbv ||
3105 (src_type.id() == ID_c_enum &&
3106 to_c_enum_type(src_type).underlying_type().id() == ID_signedbv))
3107 out << "((_ sign_extend ";
3108 else
3109 out << "((_ zero_extend ";
3110
3111 out << (to_width-from_width)
3112 << ") "; // ind
3113 convert_expr(src);
3114 out << ")";
3115 }
3116 else // chop off extra bits
3117 {
3118 out << "((_ extract " << (to_width-1) << " 0) ";
3119 convert_expr(src);
3120 out << ")";
3121 }
3122 }
3123 else if(src_type.id()==ID_fixedbv) // from fixedbv to int
3124 {
3125 const fixedbv_typet &fixedbv_type=to_fixedbv_type(src_type);
3126
3127 std::size_t from_width=fixedbv_type.get_width();
3128 std::size_t from_integer_bits=fixedbv_type.get_integer_bits();
3129 std::size_t from_fraction_bits=fixedbv_type.get_fraction_bits();
3130
3131 // we might need to round up in case of negative numbers
3132 // e.g., (int)(-1.00001)==1
3133
3134 out << "(let ((?tcop ";
3135 convert_expr(src);
3136 out << ")) ";
3137
3138 out << "(bvadd ";
3139
3140 if(to_width>from_integer_bits)
3141 {
3142 out << "((_ sign_extend "
3143 << (to_width-from_integer_bits) << ") ";
3144 out << "((_ extract " << (from_width-1) << " "
3145 << from_fraction_bits << ") ";
3146 convert_expr(src);
3147 out << "))";
3148 }
3149 else
3150 {
3151 out << "((_ extract " << (from_fraction_bits+to_width-1)
3152 << " " << from_fraction_bits << ") ";
3153 convert_expr(src);
3154 out << ")";
3155 }
3156
3157 out << " (ite (and ";
3158
3159 // some fraction bit is not zero
3160 if(from_fraction_bits == 0)
3161 out << "false";
3162 else
3163 {
3164 out << "(not (= ((_ extract " << (from_fraction_bits - 1)
3165 << " 0) ?tcop) (_ bv0 " << from_fraction_bits << ")))";
3166 }
3167
3168 // number negative
3169 out << " (= ((_ extract " << (from_width-1) << " " << (from_width-1)
3170 << ") ?tcop) #b1)";
3171
3172 out << ")"; // and
3173
3174 out << " (_ bv1 " << to_width << ") (_ bv0 " << to_width << "))"; // ite
3175 out << ")"; // bvadd
3176 out << ")"; // let
3177 }
3178 else if(src_type.id()==ID_floatbv) // from floatbv to int
3179 {
3180 if(dest_type.id()==ID_bv)
3181 {
3182 // this is _NOT_ a semantic conversion, but bit-wise
3183
3184 if(use_FPA_theory)
3185 {
3186 defined_expressionst::const_iterator it =
3187 defined_expressions.find(expr);
3188 CHECK_RETURN(it != defined_expressions.end());
3189 out << it->second;
3190 }
3191 else
3192 {
3193 // straight-forward if width matches
3194 convert_expr(src);
3195 }
3196 }
3197 else if(dest_type.id()==ID_signedbv)
3198 {
3199 // this should be floatbv_typecast, not typecast
3201 "typecast unexpected "+src_type.id_string()+" -> "+
3202 dest_type.id_string());
3203 }
3204 else if(dest_type.id()==ID_unsignedbv)
3205 {
3206 // this should be floatbv_typecast, not typecast
3208 "typecast unexpected "+src_type.id_string()+" -> "+
3209 dest_type.id_string());
3210 }
3211 }
3212 else if(src_type.id()==ID_bool) // from boolean to int
3213 {
3214 out << "(ite ";
3215 convert_expr(src);
3216
3217 if(dest_type.id()==ID_fixedbv)
3218 {
3219 fixedbv_spect spec(to_fixedbv_type(dest_type));
3220 out << " (concat (_ bv1 "
3221 << spec.integer_bits << ") " <<
3222 "(_ bv0 " << spec.get_fraction_bits() << ")) " <<
3223 "(_ bv0 " << spec.width << ")";
3224 }
3225 else
3226 {
3227 out << " (_ bv1 " << to_width << ")";
3228 out << " (_ bv0 " << to_width << ")";
3229 }
3230
3231 out << ")";
3232 }
3233 else if(src_type.id()==ID_pointer) // from pointer to int
3234 {
3235 std::size_t from_width=boolbv_width(src_type);
3236
3237 if(from_width<to_width) // extend
3238 {
3239 out << "((_ sign_extend ";
3240 out << (to_width-from_width)
3241 << ") ";
3242 convert_expr(src);
3243 out << ")";
3244 }
3245 else // chop off extra bits
3246 {
3247 out << "((_ extract " << (to_width-1) << " 0) ";
3248 convert_expr(src);
3249 out << ")";
3250 }
3251 }
3252 else if(src_type.id() == ID_integer || src_type.id() == ID_natural)
3253 {
3254 // from integer to bit-vector, must be constant
3255 if(src.is_constant())
3256 {
3258 out << "(_ bv" << i << " " << to_width << ")";
3259 }
3260 else
3261 SMT2_TODO("can't convert non-constant integer to bitvector");
3262 }
3263 else if(
3264 src_type.id() == ID_struct ||
3265 src_type.id() == ID_struct_tag) // flatten a struct to a bit-vector
3266 {
3267 if(use_datatypes)
3268 {
3269 INVARIANT(
3270 boolbv_width(src_type) == boolbv_width(dest_type),
3271 "bit vector with of source and destination type shall be equal");
3272 flatten2bv(src);
3273 }
3274 else
3275 {
3276 INVARIANT(
3277 boolbv_width(src_type) == boolbv_width(dest_type),
3278 "bit vector with of source and destination type shall be equal");
3279 convert_expr(src); // nothing else to do!
3280 }
3281 }
3282 else if(
3283 src_type.id() == ID_union ||
3284 src_type.id() == ID_union_tag) // flatten a union
3285 {
3286 INVARIANT(
3287 boolbv_width(src_type) == boolbv_width(dest_type),
3288 "bit vector with of source and destination type shall be equal");
3289 convert_expr(src); // nothing else to do!
3290 }
3291 else if(src_type.id()==ID_c_bit_field)
3292 {
3293 std::size_t from_width=boolbv_width(src_type);
3294
3295 if(from_width==to_width)
3296 convert_expr(src); // ignore
3297 else
3298 {
3300 typecast_exprt tmp(typecast_exprt(src, t), dest_type);
3301 convert_typecast(tmp);
3302 }
3303 }
3304 else
3305 {
3306 std::ostringstream e_str;
3307 e_str << src_type.id() << " -> " << dest_type.id()
3308 << " src == " << format(src);
3309 UNEXPECTEDCASE("TODO typecast2 " + e_str.str());
3310 }
3311 }
3312 else if(dest_type.id()==ID_fixedbv) // to fixedbv
3313 {
3314 const fixedbv_typet &fixedbv_type=to_fixedbv_type(dest_type);
3315 std::size_t to_fraction_bits=fixedbv_type.get_fraction_bits();
3316 std::size_t to_integer_bits=fixedbv_type.get_integer_bits();
3317
3318 if(
3319 src_type.id() == ID_unsignedbv || src_type.id() == ID_signedbv ||
3320 src_type.id() == ID_c_enum || src_type.id() == ID_c_bool)
3321 {
3322 // integer to fixedbv
3323
3324 std::size_t from_width = boolbv_width(src_type);
3325 out << "(concat ";
3326
3327 if(from_width==to_integer_bits)
3328 convert_expr(src);
3329 else if(from_width>to_integer_bits)
3330 {
3331 // too many integer bits
3332 out << "((_ extract " << (to_integer_bits-1) << " 0) ";
3333 convert_expr(src);
3334 out << ")";
3335 }
3336 else
3337 {
3338 // too few integer bits
3339 INVARIANT(
3340 from_width < to_integer_bits,
3341 "from_width should be smaller than to_integer_bits as other case "
3342 "have been handled above");
3343 if(
3344 src_type.id() == ID_unsignedbv || src_type.id() == ID_c_bool ||
3345 (src_type.id() == ID_c_enum &&
3346 to_c_enum_type(src_type).underlying_type().id() == ID_unsignedbv))
3347 {
3348 out << "((_ zero_extend " << (to_integer_bits - from_width) << ") ";
3349 convert_expr(src);
3350 out << ")";
3351 }
3352 else
3353 {
3354 out << "((_ sign_extend "
3355 << (to_integer_bits-from_width) << ") ";
3356 convert_expr(src);
3357 out << ")";
3358 }
3359 }
3360
3361 out << "(_ bv0 " << to_fraction_bits << ")";
3362 out << ")"; // concat
3363 }
3364 else if(src_type.id()==ID_bool) // bool to fixedbv
3365 {
3366 out << "(concat (concat"
3367 << " (_ bv0 " << (to_integer_bits-1) << ") ";
3368 flatten2bv(src); // produces #b0 or #b1
3369 out << ") (_ bv0 "
3370 << to_fraction_bits
3371 << "))";
3372 }
3373 else if(src_type.id()==ID_fixedbv) // fixedbv to fixedbv
3374 {
3375 const fixedbv_typet &from_fixedbv_type=to_fixedbv_type(src_type);
3376 std::size_t from_fraction_bits=from_fixedbv_type.get_fraction_bits();
3377 std::size_t from_integer_bits=from_fixedbv_type.get_integer_bits();
3378 std::size_t from_width=from_fixedbv_type.get_width();
3379
3380 out << "(let ((?tcop ";
3381 convert_expr(src);
3382 out << ")) ";
3383
3384 out << "(concat ";
3385
3386 if(to_integer_bits<=from_integer_bits)
3387 {
3388 out << "((_ extract "
3389 << (from_fraction_bits+to_integer_bits-1) << " "
3390 << from_fraction_bits
3391 << ") ?tcop)";
3392 }
3393 else
3394 {
3395 INVARIANT(
3396 to_integer_bits > from_integer_bits,
3397 "to_integer_bits should be greater than from_integer_bits as the"
3398 "other case has been handled above");
3399 out << "((_ sign_extend "
3400 << (to_integer_bits-from_integer_bits)
3401 << ") ((_ extract "
3402 << (from_width-1) << " "
3403 << from_fraction_bits
3404 << ") ?tcop))";
3405 }
3406
3407 out << " ";
3408
3409 if(to_fraction_bits<=from_fraction_bits)
3410 {
3411 out << "((_ extract "
3412 << (from_fraction_bits-1) << " "
3413 << (from_fraction_bits-to_fraction_bits)
3414 << ") ?tcop)";
3415 }
3416 else
3417 {
3418 INVARIANT(
3419 to_fraction_bits > from_fraction_bits,
3420 "to_fraction_bits should be greater than from_fraction_bits as the"
3421 "other case has been handled above");
3422 out << "(concat ((_ extract "
3423 << (from_fraction_bits-1) << " 0) ";
3424 convert_expr(src);
3425 out << ")"
3426 << " (_ bv0 " << to_fraction_bits-from_fraction_bits
3427 << "))";
3428 }
3429
3430 out << "))"; // concat, let
3431 }
3432 else
3433 UNEXPECTEDCASE("unexpected typecast to fixedbv");
3434 }
3435 else if(dest_type.id()==ID_pointer)
3436 {
3437 std::size_t to_width=boolbv_width(dest_type);
3438
3439 if(src_type.id()==ID_pointer) // pointer to pointer
3440 {
3441 // this just passes through
3442 convert_expr(src);
3443 }
3444 else if(
3445 src_type.id() == ID_unsignedbv || src_type.id() == ID_signedbv ||
3446 src_type.id() == ID_bv)
3447 {
3448 // integer to pointer
3449
3450 std::size_t from_width=boolbv_width(src_type);
3451
3452 if(from_width==to_width)
3453 convert_expr(src);
3454 else if(from_width<to_width)
3455 {
3456 out << "((_ sign_extend "
3457 << (to_width-from_width)
3458 << ") ";
3459 convert_expr(src);
3460 out << ")"; // sign_extend
3461 }
3462 else // from_width>to_width
3463 {
3464 out << "((_ extract " << to_width - 1 << " 0) ";
3465 convert_expr(src);
3466 out << ")"; // extract
3467 }
3468 }
3469 else
3470 UNEXPECTEDCASE("TODO typecast3 "+src_type.id_string()+" -> pointer");
3471 }
3472 else if(dest_type.id()==ID_range)
3473 {
3474 auto &dest_range_type = to_integer_range_type(dest_type);
3475 const auto dest_width = address_bits(dest_range_type.size());
3476 if(src_type.id() == ID_range)
3477 {
3478 // Range values are stored as (value - from). A cast between range
3479 // types must add (src_from - dest_from) to adjust the offset,
3480 // matching the SAT backend (boolbv_typecast.cpp).
3481 auto &src_range_type = to_integer_range_type(src_type);
3482 const auto src_width = address_bits(src_range_type.size());
3483 const mp_integer offset = src_range_type.from() - dest_range_type.from();
3484
3485 // First, bring the source to dest_width.
3486 const bool need_extend = src_width < dest_width;
3487 const bool need_extract = src_width > dest_width;
3488 const bool need_offset = offset != 0;
3489
3490 if(need_offset)
3491 out << "(bvadd ";
3492
3493 if(need_extend)
3494 {
3495 out << "((_ zero_extend " << dest_width - src_width << ") ";
3496 convert_expr(src);
3497 out << ')'; // zero_extend
3498 }
3499 else if(need_extract)
3500 {
3501 out << "((_ extract " << dest_width - 1 << " 0) ";
3502 convert_expr(src);
3503 out << ')'; // extract
3504 }
3505 else
3506 {
3507 convert_expr(src);
3508 }
3509
3510 if(need_offset)
3511 {
3512 // the offset may exceed the bit width, reduce it modulo 2^dest_width
3513 const mp_integer modulus = power(2, dest_width);
3514 mp_integer offset_bits = offset % modulus;
3515 if(offset_bits < 0)
3516 offset_bits += modulus;
3517 out << " (_ bv" << integer2string(offset_bits) << " " << dest_width
3518 << "))"; // bvadd
3519 }
3520 }
3521 else
3522 SMT2_TODO("typecast from " + src_type.id_string() + " to range");
3523 }
3524 else if(dest_type.id()==ID_floatbv)
3525 {
3526 // Typecast from integer to floating-point should have be been
3527 // converted to ID_floatbv_typecast during symbolic execution,
3528 // adding the rounding mode. See
3529 // smt2_convt::convert_floatbv_typecast.
3530 // The exception is bool and c_bool to float.
3531 const auto &dest_floatbv_type = to_floatbv_type(dest_type);
3532
3533 if(src_type.id()==ID_bool)
3534 {
3535 out << "(ite ";
3536 convert_expr(src);
3537 out << ' ';
3538 convert_constant(ieee_floatt::one(dest_floatbv_type).to_expr());
3539 out << ' ';
3540 convert_constant(ieee_floatt::zero(dest_floatbv_type).to_expr());
3541 out << ')';
3542 }
3543 else if(src_type.id()==ID_c_bool)
3544 {
3545 // turn into proper bool
3546 const typecast_exprt tmp(src, bool_typet());
3547 convert_typecast(typecast_exprt(tmp, dest_type));
3548 }
3549 else if(src_type.id() == ID_bv)
3550 {
3551 if(to_bv_type(src_type).get_width() != dest_floatbv_type.get_width())
3552 {
3553 UNEXPECTEDCASE("Typecast bv -> float with wrong width");
3554 }
3555
3556 if(use_FPA_theory)
3557 {
3558 out << "((_ to_fp " << dest_floatbv_type.get_e() << " "
3559 << dest_floatbv_type.get_f() + 1 << ") ";
3560 convert_expr(src);
3561 out << ')';
3562 }
3563 else
3564 convert_expr(src);
3565 }
3566 else
3567 UNEXPECTEDCASE("Unknown typecast "+src_type.id_string()+" -> float");
3568 }
3569 else if(dest_type.id() == ID_integer || dest_type.id() == ID_natural)
3570 {
3571 if(src_type.id()==ID_bool)
3572 {
3573 out << "(ite ";
3574 convert_expr(src);
3575 out <<" 1 0)";
3576 }
3577 else
3578 UNEXPECTEDCASE("Unknown typecast "+src_type.id_string()+" -> integer");
3579 }
3580 else if(dest_type.id()==ID_c_bit_field)
3581 {
3582 std::size_t from_width=boolbv_width(src_type);
3583 std::size_t to_width=boolbv_width(dest_type);
3584
3585 if(from_width==to_width)
3586 convert_expr(src); // ignore
3587 else
3588 {
3590 typecast_exprt tmp(typecast_exprt(src, t), dest_type);
3591 convert_typecast(tmp);
3592 }
3593 }
3594 else if(dest_type.id() == ID_rational)
3595 {
3596 if(src_type.id() == ID_signedbv)
3597 {
3598 // TODO: negative numbers
3599 out << "(/ ";
3600 convert_expr(src);
3601 out << " 1)";
3602 }
3603 else
3605 "Unknown typecast " + src_type.id_string() + " -> rational");
3606 }
3607 else
3609 "TODO typecast8 "+src_type.id_string()+" -> "+dest_type.id_string());
3610}
3611
3613{
3614 const exprt &src=expr.op();
3615 // const exprt &rounding_mode=expr.rounding_mode();
3616 const typet &src_type=src.type();
3617 const typet &dest_type=expr.type();
3618
3619 if(dest_type.id()==ID_floatbv)
3620 {
3621 if(src_type.id()==ID_floatbv)
3622 {
3623 // float to float
3624
3625 /* ISO 9899:1999
3626 * 6.3.1.5 Real floating types
3627 * 1 When a float is promoted to double or long double, or a
3628 * double is promoted to long double, its value is unchanged.
3629 *
3630 * 2 When a double is demoted to float, a long double is
3631 * demoted to double or float, or a value being represented in
3632 * greater precision and range than required by its semantic
3633 * type (see 6.3.1.8) is explicitly converted to its semantic
3634 * type, if the value being converted can be represented
3635 * exactly in the new type, it is unchanged. If the value
3636 * being converted is in the range of values that can be
3637 * represented but cannot be represented exactly, the result
3638 * is either the nearest higher or nearest lower representable
3639 * value, chosen in an implementation-defined manner. If the
3640 * value being converted is outside the range of values that
3641 * can be represented, the behavior is undefined.
3642 */
3643
3644 const floatbv_typet &dst=to_floatbv_type(dest_type);
3645
3646 if(use_FPA_theory)
3647 {
3648 out << "((_ to_fp " << dst.get_e() << " "
3649 << dst.get_f() + 1 << ") ";
3651 out << " ";
3652 convert_expr(src);
3653 out << ")";
3654 }
3655 else
3656 convert_floatbv(expr);
3657 }
3658 else if(src_type.id()==ID_unsignedbv)
3659 {
3660 // unsigned to float
3661
3662 /* ISO 9899:1999
3663 * 6.3.1.4 Real floating and integer
3664 * 2 When a value of integer type is converted to a real
3665 * floating type, if the value being converted can be
3666 * represented exactly in the new type, it is unchanged. If the
3667 * value being converted is in the range of values that can be
3668 * represented but cannot be represented exactly, the result is
3669 * either the nearest higher or nearest lower representable
3670 * value, chosen in an implementation-defined manner. If the
3671 * value being converted is outside the range of values that can
3672 * be represented, the behavior is undefined.
3673 */
3674
3675 const floatbv_typet &dst=to_floatbv_type(dest_type);
3676
3677 if(use_FPA_theory)
3678 {
3679 out << "((_ to_fp_unsigned " << dst.get_e() << " "
3680 << dst.get_f() + 1 << ") ";
3682 out << " ";
3683 convert_expr(src);
3684 out << ")";
3685 }
3686 else
3687 convert_floatbv(expr);
3688 }
3689 else if(src_type.id()==ID_signedbv)
3690 {
3691 // signed to float
3692
3693 const floatbv_typet &dst=to_floatbv_type(dest_type);
3694
3695 if(use_FPA_theory)
3696 {
3697 out << "((_ to_fp " << dst.get_e() << " "
3698 << dst.get_f() + 1 << ") ";
3700 out << " ";
3701 convert_expr(src);
3702 out << ")";
3703 }
3704 else
3705 convert_floatbv(expr);
3706 }
3707 else if(src_type.id()==ID_c_enum_tag)
3708 {
3709 // enum to float
3710
3711 // We first convert to 'underlying type'
3712 floatbv_typecast_exprt tmp=expr;
3713 tmp.op() = typecast_exprt(
3714 src, ns.follow_tag(to_c_enum_tag_type(src_type)).underlying_type());
3716 }
3717 else
3719 "TODO typecast11 "+src_type.id_string()+" -> "+dest_type.id_string());
3720 }
3721 else if(dest_type.id()==ID_signedbv)
3722 {
3723 if(use_FPA_theory)
3724 {
3725 std::size_t dest_width=to_signedbv_type(dest_type).get_width();
3726 out << "((_ fp.to_sbv " << dest_width << ") ";
3728 out << " ";
3729 convert_expr(src);
3730 out << ")";
3731 }
3732 else
3733 convert_floatbv(expr);
3734 }
3735 else if(dest_type.id()==ID_unsignedbv)
3736 {
3737 if(use_FPA_theory)
3738 {
3739 std::size_t dest_width=to_unsignedbv_type(dest_type).get_width();
3740 out << "((_ fp.to_ubv " << dest_width << ") ";
3742 out << " ";
3743 convert_expr(src);
3744 out << ")";
3745 }
3746 else
3747 convert_floatbv(expr);
3748 }
3749 else
3750 {
3752 "TODO typecast12 "+src_type.id_string()+" -> "+dest_type.id_string());
3753 }
3754}
3755
3758{
3759 PRECONDITION(expr.type().id() == ID_floatbv);
3760
3761 if(use_FPA_theory)
3762 {
3763 out << "(fp.roundToIntegral ";
3765 out << ' ';
3766 convert_expr(expr.op());
3767 out << ")";
3768 }
3769 else
3770 UNEXPECTEDCASE("TODO floatbv_round_to_integral without FPA");
3771}
3772
3774{
3775 const struct_typet &struct_type =
3776 expr.type().id() == ID_struct_tag
3777 ? ns.follow_tag(to_struct_tag_type(expr.type()))
3778 : to_struct_type(expr.type());
3779
3780 const struct_typet::componentst &components=
3781 struct_type.components();
3782
3784 components.size() == expr.operands().size(),
3785 "number of struct components as indicated by the struct type shall be equal"
3786 "to the number of operands of the struct expression");
3787
3788 DATA_INVARIANT(!components.empty(), "struct shall have struct components");
3789
3790 if(use_datatypes)
3791 {
3792 const std::string &smt_typename = datatype_map.at(struct_type);
3793
3794 // use the constructor for the Z3 datatype
3795 out << "(mk-" << smt_typename;
3796
3797 std::size_t i=0;
3798 for(struct_typet::componentst::const_iterator
3799 it=components.begin();
3800 it!=components.end();
3801 it++, i++)
3802 {
3803 if(is_zero_width(it->type(), ns))
3804 continue;
3805 out << " ";
3806 convert_expr(expr.operands()[i]);
3807 }
3808
3809 out << ")";
3810 }
3811 else
3812 {
3813 auto convert_operand = [this](const exprt &op) {
3814 // may need to flatten array-theory arrays in there
3815 if(op.type().id() == ID_array && use_array_theory(op))
3816 flatten_array(op);
3817 else if(op.type().id() == ID_bool)
3818 flatten2bv(op);
3819 else
3820 convert_expr(op);
3821 };
3822
3823 // SMT-LIB 2 concat is binary only
3824 std::size_t n_concat = 0;
3825 for(std::size_t i = components.size(); i > 1; i--)
3826 {
3827 if(is_zero_width(components[i - 1].type(), ns))
3828 continue;
3829 else if(i > 2 || !is_zero_width(components[0].type(), ns))
3830 {
3831 ++n_concat;
3832 out << "(concat ";
3833 }
3834
3835 convert_operand(expr.operands()[i - 1]);
3836
3837 out << " ";
3838 }
3839
3840 if(!is_zero_width(components[0].type(), ns))
3841 convert_operand(expr.op0());
3842
3843 out << std::string(n_concat, ')');
3844 }
3845}
3846
3849{
3850 const array_typet &array_type = to_array_type(expr.type());
3851 const auto &size_expr = array_type.size();
3852 // Flattening an array to a bit-vector requires a concrete size. Arrays of
3853 // unknown or non-constant size (e.g. those indexed by a mathematical
3854 // integer) can only be encoded with the SMT-LIB array theory, not
3855 // bit-blasted; report that clearly rather than aborting an invariant.
3856 if(!size_expr.is_constant())
3857 {
3859 "cannot flatten an array of non-constant size to a bit-vector; such an "
3860 "array can only be encoded with the SMT-LIB array theory");
3861 }
3862
3864 CHECK_RETURN_WITH_DIAGNOSTICS(size != 0, "can't convert zero-sized array");
3865
3866 out << "(let ((?far ";
3867 convert_expr(expr);
3868 out << ")) ";
3869
3870 for(mp_integer i=size; i!=0; --i)
3871 {
3872 if(i!=1)
3873 out << "(concat ";
3874 out << "(select ?far ";
3875 convert_expr(from_integer(i - 1, array_type.index_type()));
3876 out << ")";
3877 if(i!=1)
3878 out << " ";
3879 }
3880
3881 // close the many parentheses
3882 for(mp_integer i=size; i>1; --i)
3883 out << ")";
3884
3885 out << ")"; // let
3886}
3887
3889{
3890 const exprt &op=expr.op();
3891
3892 std::size_t total_width = boolbv_width(expr.type());
3893
3894 std::size_t member_width=boolbv_width(op.type());
3895
3896 if(total_width==member_width)
3897 {
3898 flatten2bv(op);
3899 }
3900 else
3901 {
3902 // we will pad with zeros, but non-det would be better
3903 INVARIANT(
3904 total_width > member_width,
3905 "total_width should be greater than member_width as member_width can be"
3906 "at most as large as total_width and the other case has been handled "
3907 "above");
3908 out << "(concat ";
3909 out << "(_ bv0 "
3910 << (total_width-member_width) << ") ";
3911 flatten2bv(op);
3912 out << ")";
3913 }
3914}
3915
3917{
3918 const typet &expr_type=expr.type();
3919
3920 if(expr_type.id()==ID_unsignedbv ||
3921 expr_type.id()==ID_signedbv ||
3922 expr_type.id()==ID_bv ||
3923 expr_type.id()==ID_c_enum ||
3924 expr_type.id()==ID_c_enum_tag ||
3925 expr_type.id()==ID_c_bool ||
3926 expr_type.id()==ID_c_bit_field)
3927 {
3928 const std::size_t width = boolbv_width(expr_type);
3929
3930 const mp_integer value = bvrep2integer(expr.get_value(), width, false);
3931
3932 out << "(_ bv" << value
3933 << " " << width << ")";
3934 }
3935 else if(expr_type.id()==ID_fixedbv)
3936 {
3937 const fixedbv_spect spec(to_fixedbv_type(expr_type));
3938
3939 const mp_integer v = bvrep2integer(expr.get_value(), spec.width, false);
3940
3941 out << "(_ bv" << v << " " << spec.width << ")";
3942 }
3943 else if(expr_type.id()==ID_floatbv)
3944 {
3945 const floatbv_typet &floatbv_type=
3946 to_floatbv_type(expr_type);
3947
3948 if(use_FPA_theory)
3949 {
3950 /* CBMC stores floating point literals in the most
3951 computationally useful form; biased exponents and
3952 significands including the hidden bit. Thus some encoding
3953 is needed to get to IEEE-754 style representations. */
3954
3956 size_t e=floatbv_type.get_e();
3957 size_t f=floatbv_type.get_f()+1;
3958
3959 /* Should be sufficient, but not currently supported by mathsat */
3960 #if 0
3961 mp_integer binary = v.pack();
3962
3963 out << "((_ to_fp " << e << " " << f << ")"
3964 << " #b" << integer2binary(v.pack(), v.spec.width()) << ")";
3965 #endif
3966
3967 if(v.is_NaN())
3968 {
3969 out << "(_ NaN " << e << " " << f << ")";
3970 }
3971 else if(v.is_infinity())
3972 {
3973 if(v.get_sign())
3974 out << "(_ -oo " << e << " " << f << ")";
3975 else
3976 out << "(_ +oo " << e << " " << f << ")";
3977 }
3978 else
3979 {
3980 // Zero, normal or subnormal
3981
3982 mp_integer binary = v.pack();
3983 std::string binaryString(integer2binary(v.pack(), v.spec.width()));
3984
3985 out << "(fp "
3986 << "#b" << binaryString.substr(0, 1) << " "
3987 << "#b" << binaryString.substr(1, e) << " "
3988 << "#b" << binaryString.substr(1+e, f-1) << ")";
3989 }
3990 }
3991 else
3992 {
3993 // produce corresponding bit-vector
3994 const ieee_float_spect spec(floatbv_type);
3995 const mp_integer v = bvrep2integer(expr.get_value(), spec.width(), false);
3996 out << "(_ bv" << v << " " << spec.width() << ")";
3997 }
3998 }
3999 else if(expr_type.id()==ID_pointer)
4000 {
4001 if(expr.is_null_pointer())
4002 {
4003 out << "(_ bv0 " << boolbv_width(expr_type)
4004 << ")";
4005 }
4006 else
4007 {
4008 // just treat the pointer as a bit vector
4009 const std::size_t width = boolbv_width(expr_type);
4010
4011 const mp_integer value = bvrep2integer(expr.get_value(), width, false);
4012
4013 out << "(_ bv" << value << " " << width << ")";
4014 }
4015 }
4016 else if(expr_type.id()==ID_bool)
4017 {
4018 if(expr == true)
4019 out << "true";
4020 else if(expr == false)
4021 out << "false";
4022 else
4023 UNEXPECTEDCASE("unknown Boolean constant");
4024 }
4025 else if(expr_type.id()==ID_array)
4026 {
4027 defined_expressionst::const_iterator it=defined_expressions.find(expr);
4028 CHECK_RETURN(it != defined_expressions.end());
4029 out << it->second;
4030 }
4031 else if(expr_type.id()==ID_rational)
4032 {
4033 std::string value=id2string(expr.get_value());
4034 const bool negative = has_prefix(value, "-");
4035
4036 if(negative)
4037 {
4038 out << "(- ";
4039 value = value.substr(1);
4040 }
4041
4042 size_t pos=value.find("/");
4043
4044 if(pos==std::string::npos)
4045 out << value << ".0";
4046 else
4047 {
4048 out << "(/ " << value.substr(0, pos) << ".0 "
4049 << value.substr(pos+1) << ".0)";
4050 }
4051
4052 if(negative)
4053 out << ')';
4054 }
4055 else if(expr_type.id() == ID_real)
4056 {
4057 const std::string &value = id2string(expr.get_value());
4058 out << value;
4059 if(value.find('.') == std::string::npos)
4060 out << ".0";
4061 }
4062 else if(expr_type.id()==ID_integer)
4063 {
4064 const auto value = id2string(expr.get_value());
4065
4066 // SMT2 has no negative integer literals
4067 if(has_prefix(value, "-"))
4068 out << "(- " << value.substr(1, std::string::npos) << ')';
4069 else
4070 out << value;
4071 }
4072 else if(expr_type.id() == ID_natural)
4073 {
4074 out << expr.get_value();
4075 }
4076 else if(expr_type.id() == ID_range)
4077 {
4078 auto &range_type = to_integer_range_type(expr_type);
4079 const auto width = address_bits(range_type.size());
4080 const auto value_int = numeric_cast_v<mp_integer>(expr);
4081 out << "(_ bv" << (value_int - range_type.from()) << " " << width << ")";
4082 }
4083 else if(expr_type.id() == ID_string)
4084 {
4085 // SMT-LIB 2.6 string literal. The only in-string escape is "" for a double
4086 // quote (backslash is literal). Only printable ASCII may appear verbatim;
4087 // control and non-ASCII bytes are encoded with the \u{...} hex escape.
4088 const std::string &value = id2string(expr.get_value());
4089 out << '"';
4090 for(char ch : value)
4091 {
4092 const auto c = static_cast<unsigned char>(ch);
4093 if(c == '"')
4094 out << "\"\"";
4095 else if(c >= 0x20 && c <= 0x7e)
4096 out << ch;
4097 else
4098 out << "\\u{" << std::hex << static_cast<unsigned>(c) << std::dec
4099 << '}';
4100 }
4101 out << '"';
4102 }
4103 else
4104 UNEXPECTEDCASE("unknown constant: "+expr_type.id_string());
4105}
4106
4108{
4109 if(expr.type().id() == ID_integer)
4110 {
4111 out << "(mod ";
4112 convert_expr(expr.op0());
4113 out << ' ';
4114 convert_expr(expr.op1());
4115 out << ')';
4116 }
4117 else
4119 "unsupported type for euclidean_mod: " + expr.type().id_string());
4120}
4121
4123{
4124 if(expr.type().id()==ID_unsignedbv ||
4125 expr.type().id()==ID_signedbv)
4126 {
4127 if(expr.type().id()==ID_unsignedbv)
4128 out << "(bvurem ";
4129 else
4130 out << "(bvsrem ";
4131
4132 convert_expr(expr.op0());
4133 out << " ";
4134 convert_expr(expr.op1());
4135 out << ")";
4136 }
4137 else if(expr.type().id() == ID_integer)
4138 {
4139 // Mathematical integers (mp_integer) truncate toward zero, so the
4140 // remainder takes the sign of the dividend; SMT-LIB mod is always
4141 // non-negative. Take the remainder of the magnitudes and re-apply the
4142 // dividend's sign.
4143 out << "(let ((?ma ";
4144 convert_expr(expr.op0());
4145 out << ") (?mb ";
4146 convert_expr(expr.op1());
4147 out << ")) (let ((?mr (mod (ite (< ?ma 0) (- ?ma) ?ma)";
4148 out << " (ite (< ?mb 0) (- ?mb) ?mb)))) (ite (< ?ma 0) (- ?mr) ?mr)))";
4149 }
4150 else if(expr.type().id() == ID_natural)
4151 {
4152 // Naturals are non-negative, so SMT-LIB mod already matches mp_integer.
4153 out << "(mod ";
4154 convert_expr(expr.op0());
4155 out << " ";
4156 convert_expr(expr.op1());
4157 out << ")";
4158 }
4159 else
4160 UNEXPECTEDCASE("unsupported type for mod: "+expr.type().id_string());
4161}
4162
4164{
4165 std::vector<mp_integer> dynamic_objects;
4166 pointer_logic.get_dynamic_objects(dynamic_objects);
4167
4168 if(dynamic_objects.empty())
4169 out << "false";
4170 else
4171 {
4172 std::size_t pointer_width = boolbv_width(expr.op().type());
4173
4174 out << "(let ((?obj ((_ extract "
4175 << pointer_width-1 << " "
4176 << pointer_width-config.bv_encoding.object_bits << ") ";
4177 convert_expr(expr.op());
4178 out << "))) ";
4179
4180 if(dynamic_objects.size()==1)
4181 {
4182 out << "(= (_ bv" << dynamic_objects.front()
4183 << " " << config.bv_encoding.object_bits << ") ?obj)";
4184 }
4185 else
4186 {
4187 out << "(or";
4188
4189 for(const auto &object : dynamic_objects)
4190 out << " (= (_ bv" << object
4191 << " " << config.bv_encoding.object_bits << ") ?obj)";
4192
4193 out << ")"; // or
4194 }
4195
4196 out << ")"; // let
4197 }
4198}
4199
4201{
4202 const typet &op_type=expr.op0().type();
4203
4204 if(
4205 op_type.id() == ID_unsignedbv || op_type.id() == ID_bv ||
4206 op_type.id() == ID_range)
4207 {
4208 // The range type is encoded in binary
4209 out << "(";
4210 if(expr.id()==ID_le)
4211 out << "bvule";
4212 else if(expr.id()==ID_lt)
4213 out << "bvult";
4214 else if(expr.id()==ID_ge)
4215 out << "bvuge";
4216 else if(expr.id()==ID_gt)
4217 out << "bvugt";
4218
4219 out << " ";
4220 convert_expr(expr.op0());
4221 out << " ";
4222 convert_expr(expr.op1());
4223 out << ")";
4224 }
4225 else if(op_type.id()==ID_signedbv ||
4226 op_type.id()==ID_fixedbv)
4227 {
4228 out << "(";
4229 if(expr.id()==ID_le)
4230 out << "bvsle";
4231 else if(expr.id()==ID_lt)
4232 out << "bvslt";
4233 else if(expr.id()==ID_ge)
4234 out << "bvsge";
4235 else if(expr.id()==ID_gt)
4236 out << "bvsgt";
4237
4238 out << " ";
4239 convert_expr(expr.op0());
4240 out << " ";
4241 convert_expr(expr.op1());
4242 out << ")";
4243 }
4244 else if(op_type.id()==ID_floatbv)
4245 {
4246 if(use_FPA_theory)
4247 {
4248 out << "(";
4249 if(expr.id()==ID_le)
4250 out << "fp.leq";
4251 else if(expr.id()==ID_lt)
4252 out << "fp.lt";
4253 else if(expr.id()==ID_ge)
4254 out << "fp.geq";
4255 else if(expr.id()==ID_gt)
4256 out << "fp.gt";
4257
4258 out << " ";
4259 convert_expr(expr.op0());
4260 out << " ";
4261 convert_expr(expr.op1());
4262 out << ")";
4263 }
4264 else
4265 convert_floatbv(expr);
4266 }
4267 else if(
4268 op_type.id() == ID_rational || op_type.id() == ID_integer ||
4269 op_type.id() == ID_natural || op_type.id() == ID_real)
4270 {
4271 out << "(";
4272 out << expr.id();
4273
4274 out << " ";
4275 convert_expr(expr.op0());
4276 out << " ";
4277 convert_expr(expr.op1());
4278 out << ")";
4279 }
4280 else if(op_type.id() == ID_pointer)
4281 {
4282 const exprt same_object = ::same_object(expr.op0(), expr.op1());
4283
4284 out << "(and ";
4286
4287 out << " (";
4288 if(expr.id() == ID_le)
4289 out << "bvsle";
4290 else if(expr.id() == ID_lt)
4291 out << "bvslt";
4292 else if(expr.id() == ID_ge)
4293 out << "bvsge";
4294 else if(expr.id() == ID_gt)
4295 out << "bvsgt";
4296
4297 out << ' ';
4299 out << ' ';
4301 out << ')';
4302
4303 out << ')';
4304 }
4305 else
4307 "unsupported type for "+expr.id_string()+": "+op_type.id_string());
4308}
4309
4311{
4312 if(
4313 expr.type().id() == ID_rational || expr.type().id() == ID_integer ||
4314 expr.type().id() == ID_natural || expr.type().id() == ID_real)
4315 {
4316 // these are multi-ary in SMT-LIB2
4317 out << "(+";
4318
4319 for(const auto &op : expr.operands())
4320 {
4321 out << ' ';
4322 convert_expr(op);
4323 }
4324
4325 out << ')';
4326 }
4327 else if(
4328 expr.type().id() == ID_unsignedbv || expr.type().id() == ID_signedbv ||
4329 expr.type().id() == ID_fixedbv)
4330 {
4331 // These could be chained, i.e., need not be binary,
4332 // but at least MathSat doesn't like that.
4333 if(expr.operands().size() == 2)
4334 {
4335 out << "(bvadd ";
4336 convert_expr(expr.op0());
4337 out << " ";
4338 convert_expr(expr.op1());
4339 out << ")";
4340 }
4341 else
4342 {
4344 }
4345 }
4346 else if(expr.type().id() == ID_range)
4347 {
4348 auto &range_type = to_integer_range_type(expr.type());
4349
4350 // These could be chained, i.e., need not be binary,
4351 // but at least MathSat doesn't like that.
4352 if(expr.operands().size() == 2)
4353 {
4354 // add: lhs + from + rhs + from - from = lhs + rhs + from
4355 const auto width = address_bits(range_type.size());
4356
4357 out << "(bvadd ";
4358 convert_expr(expr.op0());
4359 out << " (bvadd ";
4360 convert_expr(expr.op1());
4361 out << " (_ bv" << range_type.from() << ' ' << width
4362 << ")))"; // bv, bvadd, bvadd
4363 }
4364 else
4365 {
4367 }
4368 }
4369 else if(expr.type().id() == ID_floatbv)
4370 {
4371 // Floating-point additions should have be been converted
4372 // to ID_floatbv_plus during symbolic execution, adding
4373 // the rounding mode. See smt2_convt::convert_floatbv_plus.
4375 }
4376 else if(expr.type().id() == ID_pointer)
4377 {
4378 if(expr.operands().size() == 2)
4379 {
4380 exprt p = expr.op0(), i = expr.op1();
4381
4382 if(p.type().id() != ID_pointer)
4383 p.swap(i);
4384
4386 p.type().id() == ID_pointer,
4387 "one of the operands should have pointer type");
4388
4389 const auto &base_type = to_pointer_type(expr.type()).base_type();
4391 base_type.id() != ID_empty, "no pointer arithmetic over void pointers");
4392
4393 auto element_size_opt = pointer_offset_size(base_type, ns);
4395 element_size_opt.has_value() && *element_size_opt >= bytest{0});
4396 bytest element_size = *element_size_opt;
4397
4398 // First convert the pointer operand
4399 out << "(let ((?pointerop ";
4400 convert_expr(p);
4401 out << ")) ";
4402
4403 // The addition is done on the offset part only.
4404 const std::size_t pointer_width = boolbv_width(p.type());
4405 const std::size_t offset_bits =
4406 pointer_width - config.bv_encoding.object_bits;
4407
4408 out << "(concat ";
4409 out << "((_ extract " << pointer_width - 1 << ' ' << offset_bits
4410 << ") ?pointerop) ";
4411 out << "(bvadd ((_ extract " << offset_bits - 1 << " 0) ?pointerop) ";
4412
4413 if(element_size >= bytest{2})
4414 {
4415 out << "(bvmul ((_ extract " << offset_bits - 1 << " 0) ";
4416 convert_expr(i);
4417 out << ") (_ bv" << element_size << " " << offset_bits << "))";
4418 }
4419 else
4420 {
4421 out << "((_ extract " << offset_bits - 1 << " 0) ";
4422 convert_expr(i);
4423 out << ')'; // extract
4424 }
4425
4426 out << ")))"; // bvadd, concat, let
4427 }
4428 else
4429 {
4431 }
4432 }
4433 else
4434 UNEXPECTEDCASE("unsupported type for +: " + expr.type().id_string());
4435}
4436
4441{
4443
4444 /* CProver uses the x86 numbering of the rounding-mode
4445 * 0 == FE_TONEAREST
4446 * 1 == FE_DOWNWARD
4447 * 2 == FE_UPWARD
4448 * 3 == FE_TOWARDZERO
4449 * These literal values must be used rather than the macros
4450 * the macros from fenv.h to avoid portability problems.
4451 */
4452
4453 if(expr.is_constant())
4454 {
4455 const constant_exprt &cexpr=to_constant_expr(expr);
4456
4458
4459 if(value==0)
4460 out << "roundNearestTiesToEven";
4461 else if(value==1)
4462 out << "roundTowardNegative";
4463 else if(value==2)
4464 out << "roundTowardPositive";
4465 else if(value==3)
4466 out << "roundTowardZero";
4467 else if(value == 4)
4468 out << "roundNearestTiesToAway";
4469 else
4471 false,
4472 "Rounding mode should have value 0, 1, 2, 3, or 4",
4473 id2string(cexpr.get_value()));
4474 }
4475 else
4476 {
4477 std::size_t width=to_bitvector_type(expr.type()).get_width();
4478
4479 // Need to make the choice above part of the model
4480 out << "(ite (= (_ bv0 " << width << ") ";
4481 convert_expr(expr);
4482 out << ") roundNearestTiesToEven ";
4483
4484 out << "(ite (= (_ bv1 " << width << ") ";
4485 convert_expr(expr);
4486 out << ") roundTowardNegative ";
4487
4488 out << "(ite (= (_ bv2 " << width << ") ";
4489 convert_expr(expr);
4490 out << ") roundTowardPositive ";
4491
4492 out << "(ite (= (_ bv3 " << width << ") ";
4493 convert_expr(expr);
4494 out << ") roundTowardZero ";
4495
4496 // TODO: add some kind of error checking here
4497 out << "roundNearestTiesToAway";
4498
4499 out << "))))";
4500 }
4501}
4502
4504{
4505 const typet &type=expr.type();
4506
4508 type.id() == ID_floatbv ||
4509 (type.id() == ID_complex &&
4510 to_complex_type(type).subtype().id() == ID_floatbv));
4511
4512 if(use_FPA_theory)
4513 {
4514 if(type.id()==ID_floatbv)
4515 {
4516 out << "(fp.add ";
4518 out << " ";
4519 convert_expr(expr.lhs());
4520 out << " ";
4521 convert_expr(expr.rhs());
4522 out << ")";
4523 }
4524 else if(type.id()==ID_complex)
4525 {
4526 SMT2_TODO("+ for floatbv complex");
4527 }
4528 else
4530 false,
4531 "type should not be one of the unsupported types",
4532 type.id_string());
4533 }
4534 else
4535 convert_floatbv(expr);
4536}
4537
4539{
4540 if(
4541 expr.type().id() == ID_integer || expr.type().id() == ID_natural ||
4542 expr.type().id() == ID_rational || expr.type().id() == ID_real)
4543 {
4544 out << "(- ";
4545 convert_expr(expr.op0());
4546 out << " ";
4547 convert_expr(expr.op1());
4548 out << ")";
4549 }
4550 else if(expr.type().id()==ID_unsignedbv ||
4551 expr.type().id()==ID_signedbv ||
4552 expr.type().id()==ID_fixedbv)
4553 {
4554 if(expr.op0().type().id()==ID_pointer &&
4555 expr.op1().type().id()==ID_pointer)
4556 {
4557 // Pointer difference
4558 const auto &base_type = to_pointer_type(expr.op0().type()).base_type();
4560 base_type.id() != ID_empty, "no pointer arithmetic over void pointers");
4561 auto element_size_opt = pointer_offset_size(base_type, ns);
4563 element_size_opt.has_value() && *element_size_opt >= bytest{1});
4564 bytest element_size = *element_size_opt;
4565
4566 if(element_size >= bytest{2})
4567 out << "(bvsdiv ";
4568
4569 INVARIANT(
4570 boolbv_width(expr.op0().type()) == boolbv_width(expr.type()),
4571 "bitvector width of operand shall be equal to the bitvector width of "
4572 "the expression");
4573
4574 out << "(bvsub ";
4575 convert_expr(expr.op0());
4576 out << " ";
4577 convert_expr(expr.op1());
4578 out << ")";
4579
4580 if(element_size >= bytest{2})
4581 out << " (_ bv" << element_size << " " << boolbv_width(expr.type())
4582 << "))";
4583 }
4584 else
4585 {
4586 out << "(bvsub ";
4587 convert_expr(expr.op0());
4588 out << " ";
4589 convert_expr(expr.op1());
4590 out << ")";
4591 }
4592 }
4593 else if(expr.type().id()==ID_floatbv)
4594 {
4595 // Floating-point subtraction should have be been converted
4596 // to ID_floatbv_minus during symbolic execution, adding
4597 // the rounding mode. See smt2_convt::convert_floatbv_minus.
4599 }
4600 else if(expr.type().id()==ID_pointer)
4601 {
4602 if(
4603 expr.op0().type().id() == ID_pointer &&
4604 (expr.op1().type().id() == ID_unsignedbv ||
4605 expr.op1().type().id() == ID_signedbv))
4606 {
4607 // rewrite p-o to p+(-o)
4608 return convert_plus(
4609 plus_exprt(expr.op0(), unary_minus_exprt(expr.op1())));
4610 }
4611 else
4613 "unsupported operand types for -: " + expr.op0().type().id_string() +
4614 " and " + expr.op1().type().id_string());
4615 }
4616 else if(expr.type().id() == ID_range)
4617 {
4618 auto &range_type = to_integer_range_type(expr.type());
4619
4620 // sub: lhs + from - (rhs + from) - from = lhs - rhs - from
4621 const auto width = address_bits(range_type.size());
4622
4623 out << "(bvsub (bvsub ";
4624 convert_expr(expr.op0());
4625 out << ' ';
4626 convert_expr(expr.op1());
4627 out << ") (_ bv" << range_type.from() << ' ' << width << "))"; // bv, bvsub
4628 }
4629 else
4630 UNEXPECTEDCASE("unsupported type for -: "+expr.type().id_string());
4631}
4632
4634{
4636 expr.type().id() == ID_floatbv,
4637 "type of ieee floating point expression shall be floatbv");
4638
4639 if(use_FPA_theory)
4640 {
4641 out << "(fp.sub ";
4643 out << " ";
4644 convert_expr(expr.lhs());
4645 out << " ";
4646 convert_expr(expr.rhs());
4647 out << ")";
4648 }
4649 else
4650 convert_floatbv(expr);
4651}
4652
4654{
4655 if(expr.type().id()==ID_unsignedbv ||
4656 expr.type().id()==ID_signedbv)
4657 {
4658 if(expr.type().id()==ID_unsignedbv)
4659 out << "(bvudiv ";
4660 else
4661 out << "(bvsdiv ";
4662
4663 convert_expr(expr.op0());
4664 out << " ";
4665 convert_expr(expr.op1());
4666 out << ")";
4667 }
4668 else if(expr.type().id()==ID_fixedbv)
4669 {
4670 fixedbv_spect spec(to_fixedbv_type(expr.type()));
4671 std::size_t fraction_bits=spec.get_fraction_bits();
4672
4673 out << "((_ extract " << spec.width-1 << " 0) ";
4674 out << "(bvsdiv ";
4675
4676 out << "(concat ";
4677 convert_expr(expr.op0());
4678 out << " (_ bv0 " << fraction_bits << ")) ";
4679
4680 out << "((_ sign_extend " << fraction_bits << ") ";
4681 convert_expr(expr.op1());
4682 out << ")";
4683
4684 out << "))";
4685 }
4686 else if(expr.type().id()==ID_floatbv)
4687 {
4688 // Floating-point division should have be been converted
4689 // to ID_floatbv_div during symbolic execution, adding
4690 // the rounding mode. See smt2_convt::convert_floatbv_div.
4692 }
4693 else if(
4694 expr.type().id() == ID_rational || expr.type().id() == ID_integer ||
4695 expr.type().id() == ID_natural || expr.type().id() == ID_real)
4696 {
4697 if(expr.type().id() == ID_integer)
4698 {
4699 // Mathematical integers (mp_integer) truncate division toward zero,
4700 // whereas SMT-LIB div floors. Encode truncation: divide the magnitudes
4701 // and make the quotient negative iff the operands have opposite signs.
4702 out << "(let ((?da ";
4703 convert_expr(expr.op0());
4704 out << ") (?db ";
4705 convert_expr(expr.op1());
4706 out << ")) (let ((?dq (div (ite (< ?da 0) (- ?da) ?da)";
4707 out << " (ite (< ?db 0) (- ?db) ?db))))";
4708 out << " (ite (= (< ?da 0) (< ?db 0)) ?dq (- ?dq))))";
4709 }
4710 else
4711 {
4712 // Naturals are non-negative (SMT-LIB div already truncates); rationals
4713 // and reals use real division.
4714 if(expr.type().id() == ID_natural)
4715 out << "(div ";
4716 else
4717 out << "(/ ";
4718 convert_expr(expr.op0());
4719 out << " ";
4720 convert_expr(expr.op1());
4721 out << ")";
4722 }
4723 }
4724 else
4725 UNEXPECTEDCASE("unsupported type for /: "+expr.type().id_string());
4726}
4727
4729{
4731 expr.type().id() == ID_floatbv,
4732 "type of ieee floating point expression shall be floatbv");
4733
4734 if(use_FPA_theory)
4735 {
4736 out << "(fp.div ";
4738 out << " ";
4739 convert_expr(expr.lhs());
4740 out << " ";
4741 convert_expr(expr.rhs());
4742 out << ")";
4743 }
4744 else
4745 convert_floatbv(expr);
4746}
4747
4749{
4750 // re-write to binary if needed
4751 if(expr.operands().size()>2)
4752 {
4753 // strip last operand
4754 exprt tmp=expr;
4755 tmp.operands().pop_back();
4756
4757 // recursive call
4758 return convert_mult(mult_exprt(tmp, expr.operands().back()));
4759 }
4760
4761 INVARIANT(
4762 expr.operands().size() == 2,
4763 "expression should have been converted to a variant with two operands");
4764
4765 if(expr.type().id()==ID_unsignedbv ||
4766 expr.type().id()==ID_signedbv)
4767 {
4768 // Note that bvmul is really unsigned,
4769 // but this is irrelevant as we chop-off any extra result
4770 // bits.
4771 out << "(bvmul ";
4772 convert_expr(expr.op0());
4773 out << " ";
4774 convert_expr(expr.op1());
4775 out << ")";
4776 }
4777 else if(expr.type().id()==ID_floatbv)
4778 {
4779 // Floating-point multiplication should have be been converted
4780 // to ID_floatbv_mult during symbolic execution, adding
4781 // the rounding mode. See smt2_convt::convert_floatbv_mult.
4783 }
4784 else if(expr.type().id()==ID_fixedbv)
4785 {
4786 fixedbv_spect spec(to_fixedbv_type(expr.type()));
4787 std::size_t fraction_bits=spec.get_fraction_bits();
4788
4789 out << "((_ extract "
4790 << spec.width+fraction_bits-1 << " "
4791 << fraction_bits << ") ";
4792
4793 out << "(bvmul ";
4794
4795 out << "((_ sign_extend " << fraction_bits << ") ";
4796 convert_expr(expr.op0());
4797 out << ") ";
4798
4799 out << "((_ sign_extend " << fraction_bits << ") ";
4800 convert_expr(expr.op1());
4801 out << ")";
4802
4803 out << "))"; // bvmul, extract
4804 }
4805 else if(
4806 expr.type().id() == ID_rational || expr.type().id() == ID_integer ||
4807 expr.type().id() == ID_natural || expr.type().id() == ID_real)
4808 {
4809 out << "(*";
4810
4811 for(const auto &op : expr.operands())
4812 {
4813 out << " ";
4814 convert_expr(op);
4815 }
4816
4817 out << ")";
4818 }
4819 else
4820 UNEXPECTEDCASE("unsupported type for *: "+expr.type().id_string());
4821}
4822
4824{
4826 expr.type().id() == ID_floatbv,
4827 "type of ieee floating point expression shall be floatbv");
4828
4829 if(use_FPA_theory)
4830 {
4831 out << "(fp.mul ";
4833 out << " ";
4834 convert_expr(expr.lhs());
4835 out << " ";
4836 convert_expr(expr.rhs());
4837 out << ")";
4838 }
4839 else
4840 convert_floatbv(expr);
4841}
4842
4844{
4846 expr.type().id() == ID_floatbv,
4847 "type of ieee floating point expression shall be floatbv");
4848
4849 if(use_FPA_theory)
4850 {
4851 // Note that these do not have a rounding mode
4852 out << "(fp.rem ";
4853 convert_expr(expr.lhs());
4854 out << " ";
4855 convert_expr(expr.rhs());
4856 out << ")";
4857 }
4858 else
4859 {
4860 SMT2_TODO(
4861 "smt2_convt::convert_floatbv_rem to be implemented when not using "
4862 "FPA_theory");
4863 }
4864}
4865
4867{
4869 expr.type().id() == ID_floatbv,
4870 "type of ieee floating point expression shall be floatbv");
4871
4872 if(use_FPA_theory)
4873 {
4874 out << "(fp.fma ";
4876 out << " ";
4878 out << " ";
4880 out << " ";
4881 convert_expr(expr.op_add());
4882 out << ")";
4883 }
4884 else
4885 convert_floatbv(expr);
4886}
4887
4889{
4890 INVARIANT(
4891 expr.operands().size() == 3,
4892 "with expression should have exactly three operands");
4893
4894 const typet &expr_type = expr.type();
4895
4896 if(expr_type.id()==ID_array)
4897 {
4898 const array_typet &array_type=to_array_type(expr_type);
4899
4900 if(use_array_theory(expr))
4901 {
4902 out << "(store ";
4903 convert_expr(expr.old());
4904 out << " ";
4905 convert_expr(typecast_exprt(expr.where(), array_type.index_type()));
4906 out << " ";
4907 convert_expr(expr.new_value());
4908 out << ")";
4909 }
4910 else
4911 {
4912 // fixed-width
4913 std::size_t array_width=boolbv_width(array_type);
4914 std::size_t sub_width = boolbv_width(array_type.element_type());
4915 std::size_t index_width=boolbv_width(expr.where().type());
4916
4917 // We mask out the updated bits with AND,
4918 // and then OR-in the shifted new value.
4919
4920 out << "(let ((distance? ";
4921 out << "(bvmul (_ bv" << sub_width << " " << array_width << ") ";
4922
4923 // SMT2 says that the shift distance needs to be as wide
4924 // as the stuff we are shifting.
4925 if(array_width>index_width)
4926 {
4927 out << "((_ zero_extend " << array_width-index_width << ") ";
4928 convert_expr(expr.where());
4929 out << ")";
4930 }
4931 else
4932 {
4933 out << "((_ extract " << array_width-1 << " 0) ";
4934 convert_expr(expr.where());
4935 out << ")";
4936 }
4937
4938 out << "))) "; // bvmul, distance?
4939
4940 out << "(bvor ";
4941 out << "(bvand ";
4942 out << "(bvnot ";
4943 out << "(bvshl (_ bv" << power(2, sub_width) - 1 << " " << array_width
4944 << ") ";
4945 out << "distance?)) "; // bvnot, bvlshl
4946 convert_expr(expr.old());
4947 out << ") "; // bvand
4948 out << "(bvshl ";
4949 out << "((_ zero_extend " << array_width-sub_width << ") ";
4950 convert_expr(expr.new_value());
4951 out << ") distance?)))"; // zero_extend, bvshl, bvor, let
4952 }
4953 }
4954 else if(expr_type.id() == ID_struct || expr_type.id() == ID_struct_tag)
4955 {
4956 const struct_typet &struct_type =
4957 expr_type.id() == ID_struct_tag
4958 ? ns.follow_tag(to_struct_tag_type(expr_type))
4959 : to_struct_type(expr_type);
4960
4961 const exprt &index = expr.where();
4962 const exprt &value = expr.new_value();
4963
4964 const irep_idt &component_name=index.get(ID_component_name);
4965
4966 INVARIANT(
4967 struct_type.has_component(component_name),
4968 "struct should have accessed component");
4969
4970 if(use_datatypes)
4971 {
4972 const std::string &smt_typename = datatype_map.at(expr_type);
4973
4974 out << "(update-" << smt_typename << "." << component_name << " ";
4975 convert_expr(expr.old());
4976 out << " ";
4977 convert_expr(value);
4978 out << ")";
4979 }
4980 else
4981 {
4982 auto convert_operand = [this](const exprt &op)
4983 {
4984 // may need to flatten array-theory arrays in there
4985 if(op.type().id() == ID_array && use_array_theory(op))
4986 flatten_array(op);
4987 else if(op.type().id() == ID_bool)
4988 flatten2bv(op);
4989 else
4990 convert_expr(op);
4991 };
4992
4993 std::size_t struct_width=boolbv_width(struct_type);
4994
4995 // figure out the offset and width of the member
4996 const boolbv_widtht::membert &m =
4997 boolbv_width.get_member(struct_type, component_name);
4998
4999 if(m.width==struct_width)
5000 {
5001 // the struct is the same as the member, no concat needed
5002 convert_operand(value);
5003 }
5004 else
5005 {
5006 out << "(let ((?withop ";
5007 convert_operand(expr.old());
5008 out << ")) ";
5009
5010 if(m.offset == 0)
5011 {
5012 // the member is at the beginning
5013 out << "(concat "
5014 << "((_ extract " << (struct_width - 1) << " " << m.width
5015 << ") ?withop) ";
5016 convert_operand(value);
5017 out << ")"; // concat
5018 }
5019 else if(m.offset + m.width == struct_width)
5020 {
5021 // the member is at the end
5022 out << "(concat ";
5023 convert_operand(value);
5024 out << " ((_ extract " << (m.offset - 1) << " 0) ?withop))";
5025 }
5026 else
5027 {
5028 // most general case, need two concat-s
5029 out << "(concat (concat "
5030 << "((_ extract " << (struct_width - 1) << " "
5031 << (m.offset + m.width) << ") ?withop) ";
5032 convert_operand(value);
5033 out << ") ((_ extract " << (m.offset - 1) << " 0) ?withop)";
5034 out << ")"; // concat
5035 }
5036
5037 out << ")"; // let ?withop
5038 }
5039 }
5040 }
5041 else if(expr_type.id() == ID_union || expr_type.id() == ID_union_tag)
5042 {
5043 const exprt &value = expr.new_value();
5044
5045 std::size_t total_width = boolbv_width(expr_type);
5046
5047 std::size_t member_width=boolbv_width(value.type());
5048
5049 if(total_width==member_width)
5050 {
5051 flatten2bv(value);
5052 }
5053 else
5054 {
5055 INVARIANT(
5056 total_width > member_width,
5057 "total width should be greater than member_width as member_width is at "
5058 "most as large as total_width and the other case has been handled "
5059 "above");
5060 out << "(concat ";
5061 out << "((_ extract "
5062 << (total_width-1)
5063 << " " << member_width << ") ";
5064 convert_expr(expr.old());
5065 out << ") ";
5066 flatten2bv(value);
5067 out << ")";
5068 }
5069 }
5070 else if(expr_type.id()==ID_bv ||
5071 expr_type.id()==ID_unsignedbv ||
5072 expr_type.id()==ID_signedbv)
5073 {
5074 if(expr.new_value().type().id() == ID_bool)
5075 {
5077 update_bit_exprt(expr.old(), expr.where(), expr.new_value()));
5078 }
5079 else
5080 {
5082 update_bits_exprt(expr.old(), expr.where(), expr.new_value()));
5083 }
5084 }
5085 else
5087 "with expects struct, union, or array type, but got "+
5088 expr.type().id_string());
5089}
5090
5092{
5093 PRECONDITION(expr.operands().size() == 3);
5094
5095 SMT2_TODO("smt2_convt::convert_update to be implemented");
5096}
5097
5099{
5100 return convert_expr(expr.lower());
5101}
5102
5104{
5105 return convert_expr(expr.lower());
5106}
5107
5109{
5110 const typet &array_op_type = expr.array().type();
5111
5112 if(array_op_type.id()==ID_array)
5113 {
5114 const array_typet &array_type=to_array_type(array_op_type);
5115
5116 if(use_array_theory(expr.array()))
5117 {
5118 if(expr.is_boolean() && !use_array_of_bool)
5119 {
5120 out << "(= ";
5121 out << "(select ";
5122 convert_expr(expr.array());
5123 out << " ";
5124 convert_expr(typecast_exprt(expr.index(), array_type.index_type()));
5125 out << ")";
5126 out << " #b1)";
5127 }
5128 else
5129 {
5130 out << "(select ";
5131 convert_expr(expr.array());
5132 out << " ";
5133 convert_expr(typecast_exprt(expr.index(), array_type.index_type()));
5134 out << ")";
5135 }
5136 }
5137 else
5138 {
5139 // fixed size
5140 std::size_t array_width=boolbv_width(array_type);
5141
5142 unflatten(wheret::BEGIN, array_type.element_type());
5143
5144 std::size_t sub_width = boolbv_width(array_type.element_type());
5145 std::size_t index_width=boolbv_width(expr.index().type());
5146
5147 out << "((_ extract " << sub_width-1 << " 0) ";
5148 out << "(bvlshr ";
5149 convert_expr(expr.array());
5150 out << " ";
5151 out << "(bvmul (_ bv" << sub_width << " " << array_width << ") ";
5152
5153 // SMT2 says that the shift distance must be the same as
5154 // the width of what we shift.
5155 if(array_width>index_width)
5156 {
5157 out << "((_ zero_extend " << array_width-index_width << ") ";
5158 convert_expr(expr.index());
5159 out << ")"; // zero_extend
5160 }
5161 else
5162 {
5163 out << "((_ extract " << array_width-1 << " 0) ";
5164 convert_expr(expr.index());
5165 out << ")"; // extract
5166 }
5167
5168 out << ")))"; // mult, bvlshr, extract
5169
5170 unflatten(wheret::END, array_type.element_type());
5171 }
5172 }
5173 else
5174 INVARIANT(
5175 false, "index with unsupported array type: " + array_op_type.id_string());
5176}
5177
5179{
5180 const member_exprt &member_expr=to_member_expr(expr);
5181 const exprt &struct_op=member_expr.struct_op();
5182 const typet &struct_op_type = struct_op.type();
5183 const irep_idt &name=member_expr.get_component_name();
5184
5185 if(struct_op_type.id() == ID_struct || struct_op_type.id() == ID_struct_tag)
5186 {
5187 const struct_typet &struct_type =
5188 struct_op_type.id() == ID_struct_tag
5189 ? ns.follow_tag(to_struct_tag_type(struct_op_type))
5190 : to_struct_type(struct_op_type);
5191
5192 INVARIANT(
5193 struct_type.has_component(name), "struct should have accessed component");
5194
5195 if(use_datatypes)
5196 {
5197 const std::string &smt_typename = datatype_map.at(struct_type);
5198
5199 out << "(" << smt_typename << "."
5200 << struct_type.get_component(name).get_name()
5201 << " ";
5202 convert_expr(struct_op);
5203 out << ")";
5204 }
5205 else
5206 {
5207 // we extract
5208 const auto &member_offset = boolbv_width.get_member(struct_type, name);
5209
5210 if(expr.type().id() == ID_bool)
5211 out << "(= ";
5212 out << "((_ extract " << (member_offset.offset + member_offset.width - 1)
5213 << " " << member_offset.offset << ") ";
5214 convert_expr(struct_op);
5215 out << ")";
5216 if(expr.type().id() == ID_bool)
5217 out << " #b1)";
5218 }
5219 }
5220 else if(
5221 struct_op_type.id() == ID_union || struct_op_type.id() == ID_union_tag)
5222 {
5223 std::size_t width=boolbv_width(expr.type());
5225 width != 0, "failed to get union member width");
5226
5227 if(use_datatypes)
5228 {
5229 unflatten(wheret::BEGIN, expr.type());
5230
5231 out << "((_ extract " << (width - 1) << " 0) ";
5232 convert_expr(struct_op);
5233 out << ")";
5234
5235 unflatten(wheret::END, expr.type());
5236 }
5237 else
5238 {
5239 out << "((_ extract " << (width - 1) << " 0) ";
5240 convert_expr(struct_op);
5241 out << ")";
5242 }
5243 }
5244 else
5246 "convert_member on an unexpected type "+struct_op_type.id_string());
5247}
5248
5250{
5251 const typet &type = expr.type();
5252
5253 if(type.id()==ID_bool)
5254 {
5255 out << "(ite ";
5256 convert_expr(expr); // this returns a Bool
5257 out << " #b1 #b0)"; // this is a one-bit bit-vector
5258 }
5259 else if(type.id()==ID_array)
5260 {
5261 if(use_array_theory(expr))
5262 {
5263 // concatenate elements
5264 const array_typet &array_type = to_array_type(type);
5265
5266 mp_integer size =
5268
5269 // SMT-LIB 2 concat is binary only
5270 std::size_t n_concat = 0;
5271 for(mp_integer i = size; i > 1; --i)
5272 {
5273 ++n_concat;
5274 out << "(concat ";
5275
5276 flatten2bv(
5277 index_exprt{expr, from_integer(i - 1, array_type.index_type())});
5278
5279 out << " ";
5280 }
5281
5282 flatten2bv(index_exprt{expr, from_integer(0, array_type.index_type())});
5283
5284 out << std::string(n_concat, ')'); // concat
5285 }
5286 else
5287 convert_expr(expr);
5288 }
5289 else if(type.id() == ID_struct || type.id() == ID_struct_tag)
5290 {
5291 if(use_datatypes)
5292 {
5293 // concatenate elements
5294 const struct_typet &struct_type =
5295 type.id() == ID_struct_tag ? ns.follow_tag(to_struct_tag_type(type))
5296 : to_struct_type(type);
5297
5298 const struct_typet::componentst &components=
5299 struct_type.components();
5300
5301 // SMT-LIB 2 concat is binary only
5302 std::size_t n_concat = 0;
5303 for(std::size_t i=components.size(); i>1; i--)
5304 {
5305 if(is_zero_width(components[i - 1].type(), ns))
5306 continue;
5307 else if(i > 2 || !is_zero_width(components[0].type(), ns))
5308 {
5309 ++n_concat;
5310 out << "(concat ";
5311 }
5312
5313 flatten2bv(member_exprt{expr, components[i - 1]});
5314
5315 out << " ";
5316 }
5317
5318 if(!is_zero_width(components[0].type(), ns))
5319 {
5320 flatten2bv(member_exprt{expr, components[0]});
5321 }
5322
5323 out << std::string(n_concat, ')'); // concat
5324 }
5325 else
5326 convert_expr(expr);
5327 }
5328 else if(type.id()==ID_floatbv)
5329 {
5330 if(use_FPA_theory)
5331 {
5332 // A floatbv constant's IEEE-754 interchange bit pattern is exactly its
5333 // bit-vector representation, so it is emitted as a literal bit-vector.
5334 // This is the only shape that reaches flatten2bv under FPA: a
5335 // non-constant float whose bits are read is lowered by
5336 // lower_byte_operators into a float typecast, which is handled by the
5337 // bvfromfloat round-trip in find_symbols and never reaches here.
5338 if(expr.is_constant())
5339 {
5340 const ieee_float_spect spec(to_floatbv_type(type));
5341 const mp_integer value = bvrep2integer(
5342 to_constant_expr(expr).get_value(), spec.width(), false);
5343 out << "(_ bv" << value << " " << spec.width() << ")";
5344 }
5345 else
5346 {
5348 "flatten2bv of a non-constant FPA-encoded float is unsupported");
5349 }
5350 }
5351 else
5352 convert_expr(expr);
5353 }
5354 else
5355 convert_expr(expr);
5356}
5357
5359 wheret where,
5360 const typet &type,
5361 unsigned nesting)
5362{
5363 if(type.id()==ID_bool)
5364 {
5365 if(where==wheret::BEGIN)
5366 out << "(= "; // produces a bool
5367 else
5368 out << " #b1)";
5369 }
5370 else if(type.id() == ID_array)
5371 {
5373
5374 if(where == wheret::BEGIN)
5375 out << "(let ((?ufop" << nesting << " ";
5376 else
5377 {
5378 out << ")) ";
5379
5380 const array_typet &array_type = to_array_type(type);
5381
5382 std::size_t subtype_width = boolbv_width(array_type.element_type());
5383
5385 array_type.size().is_constant(),
5386 "cannot unflatten arrays of non-constant size");
5387 mp_integer size =
5389
5390 for(mp_integer i = 1; i < size; ++i)
5391 out << "(store ";
5392
5393 // Build a constant array filled with element 0 as the base, then
5394 // overwrite indices 1..N-1 via (store ...).
5395 if(use_as_const)
5396 {
5397 out << "((as const ";
5398 convert_type(array_type);
5399 out << ") ";
5400 }
5401 else
5402 {
5403 INVARIANT(
5405 "unflatten relies on `(lambda ...)` for constant arrays "
5406 "when `(as const ...)` is unavailable");
5407 // Note: lambda is a Z3/Bitwuzla extension; not part of the
5408 // SMT-LIB 2.6 standard. The bound variable `?ufidx<n>` is
5409 // intentionally unused -- the body returns the element-0 value
5410 // regardless of its argument, making this semantically
5411 // equivalent to `(as const ...)`.
5412 out << "(lambda ((?ufidx" << nesting << " ";
5413 convert_type(array_type.index_type());
5414 out << ")) ";
5415 }
5416 // use element at index 0 as default value
5417 unflatten(wheret::BEGIN, array_type.element_type(), nesting + 1);
5418 out << "((_ extract " << subtype_width - 1 << " "
5419 << "0) ?ufop" << nesting << ")";
5420 unflatten(wheret::END, array_type.element_type(), nesting + 1);
5421 out << ") ";
5422
5423 std::size_t offset = subtype_width;
5424 for(mp_integer i = 1; i < size; ++i, offset += subtype_width)
5425 {
5426 convert_expr(from_integer(i, array_type.index_type()));
5427 out << ' ';
5428 unflatten(wheret::BEGIN, array_type.element_type(), nesting + 1);
5429 out << "((_ extract " << offset + subtype_width - 1 << " " << offset
5430 << ") ?ufop" << nesting << ")";
5431 unflatten(wheret::END, array_type.element_type(), nesting + 1);
5432 out << ")"; // store
5433 }
5434
5435 out << ")"; // let
5436 }
5437 }
5438 else if(type.id() == ID_struct || type.id() == ID_struct_tag)
5439 {
5440 if(use_datatypes)
5441 {
5442 // extract members
5443 if(where==wheret::BEGIN)
5444 out << "(let ((?ufop" << nesting << " ";
5445 else
5446 {
5447 out << ")) ";
5448
5449 const std::string &smt_typename = datatype_map.at(type);
5450
5451 out << "(mk-" << smt_typename;
5452
5453 const struct_typet &struct_type =
5454 type.id() == ID_struct_tag ? ns.follow_tag(to_struct_tag_type(type))
5455 : to_struct_type(type);
5456
5457 const struct_typet::componentst &components=
5458 struct_type.components();
5459
5460 std::size_t offset=0;
5461
5462 for(struct_typet::componentst::const_iterator it = components.begin();
5463 it != components.end();
5464 it++)
5465 {
5466 if(is_zero_width(it->type(), ns))
5467 continue;
5468
5469 std::size_t member_width=boolbv_width(it->type());
5470
5471 out << " ";
5472 unflatten(wheret::BEGIN, it->type(), nesting+1);
5473 out << "((_ extract " << offset+member_width-1 << " "
5474 << offset << ") ?ufop" << nesting << ")";
5475 unflatten(wheret::END, it->type(), nesting+1);
5476 offset+=member_width;
5477 }
5478
5479 out << "))"; // mk-, let
5480 }
5481 }
5482 else
5483 {
5484 // nop, already a bv
5485 }
5486 }
5487 else
5488 {
5489 // nop
5490 }
5491}
5492
5493void smt2_convt::set_to(const exprt &expr, bool value)
5494{
5495 PRECONDITION(expr.is_boolean());
5496
5497 if(expr.id()==ID_and && value)
5498 {
5499 for(const auto &op : expr.operands())
5500 set_to(op, true);
5501 return;
5502 }
5503
5504 if(expr.id()==ID_or && !value)
5505 {
5506 for(const auto &op : expr.operands())
5507 set_to(op, false);
5508 return;
5509 }
5510
5511 if(expr.id()==ID_not)
5512 {
5513 return set_to(to_not_expr(expr).op(), !value);
5514 }
5515
5516 out << "\n";
5517
5518 // special treatment for "set_to(a=b, true)" where
5519 // a is a new symbol
5520
5521 if(expr.id() == ID_equal && value)
5522 {
5523 const equal_exprt &equal_expr=to_equal_expr(expr);
5524 if(is_zero_width(equal_expr.lhs().type(), ns))
5525 {
5526 // ignore equality checking over expressions with empty (void) type
5527 return;
5528 }
5529
5530 if(equal_expr.lhs().id()==ID_symbol)
5531 {
5532 const irep_idt &identifier =
5533 to_symbol_expr(equal_expr.lhs()).identifier();
5534
5535 // A symbol that is not yet declared and that is equated to an
5536 // expression is defined by that expression. This is not possible
5537 // when the expression refers to the symbol itself, e.g.,
5538 // x == (x & y) | z: such an equality is a constraint on x, and is
5539 // asserted below.
5540 if(
5541 identifier_map.find(identifier) == identifier_map.end() &&
5542 equal_expr.lhs() != equal_expr.rhs() &&
5543 !has_symbol_expr(equal_expr.rhs(), identifier, true))
5544 {
5545 auto id_entry = identifier_map.insert(
5546 {identifier, identifiert{equal_expr.lhs().type(), false}});
5547 CHECK_RETURN(id_entry.second);
5548
5549 find_symbols(id_entry.first->second.type);
5550 exprt prepared_rhs = prepare_for_convert_expr(equal_expr.rhs());
5551
5552 std::string smt2_identifier=convert_identifier(identifier);
5553 smt2_identifiers.insert(smt2_identifier);
5554
5555 out << "; set_to true (equal)\n";
5556
5557 // Helper: emit the body of a definition for `smt2_identifier`
5558 // -- either a direct `convert_expr(prepared_rhs)` for non-array
5559 // or array-theory cases, or a `unflatten ... convert_expr ...
5560 // unflatten` reconstruction for array RHSes. Used twice below
5561 // to keep the `declare-fun + assert` and `define-fun` paths in
5562 // sync.
5563 auto emit_definition_body = [&]()
5564 {
5565 if(
5566 equal_expr.lhs().type().id() != ID_array ||
5567 use_array_theory(prepared_rhs))
5568 {
5569 convert_expr(prepared_rhs);
5570 }
5571 else
5572 {
5573 unflatten(wheret::BEGIN, equal_expr.lhs().type());
5574 convert_expr(prepared_rhs);
5575 unflatten(wheret::END, equal_expr.lhs().type());
5576 }
5577 };
5578
5579 if(equal_expr.lhs().type().id() == ID_mathematical_function)
5580 {
5581 // We avoid define-fun, since it has been reported to cause
5582 // trouble with Z3's parser.
5583 out << "(declare-fun " << smt2_identifier;
5584
5585 auto &mathematical_function_type =
5586 to_mathematical_function_type(equal_expr.lhs().type());
5587
5588 out << " (";
5589 bool first = true;
5590
5591 for(auto &t : mathematical_function_type.domain())
5592 {
5593 if(first)
5594 first = false;
5595 else
5596 out << ' ';
5597
5598 convert_type(t);
5599 }
5600
5601 out << ") ";
5602 convert_type(mathematical_function_type.codomain());
5603 out << ")\n";
5604
5605 out << "(assert (= " << smt2_identifier << ' ';
5606 convert_expr(prepared_rhs);
5607 out << ')' << ')' << '\n';
5608 }
5609 else if(use_lambda_for_array)
5610 {
5611 // The body emitted below may contain a `(lambda ...)` from
5612 // `unflatten` (used as a stand-in for `(as const ...)` for
5613 // back-ends with `use_as_const = false`). Z3 rejects
5614 // `get-value` on symbols whose `define-fun` body contains
5615 // a lambda, so we use `declare-fun` + `assert (= ...)` here.
5616 // Back-ends with `use_lambda_for_array = false` (the
5617 // default, currently every back-end other than Z3) keep
5618 // using the `define-fun` form below, so their SMT2 output
5619 // is unaffected.
5620 out << "(declare-fun " << smt2_identifier;
5621 out << " () ";
5622 convert_type(equal_expr.lhs().type());
5623 out << ")\n";
5624 out << "(assert (= " << smt2_identifier << ' ';
5625 emit_definition_body();
5626 out << "))\n";
5627 }
5628 else
5629 {
5630 out << "(define-fun " << smt2_identifier;
5631 out << " () ";
5632 convert_type(equal_expr.lhs().type());
5633 out << ' ';
5634 emit_definition_body();
5635 out << ')' << '\n';
5636 }
5637
5638 return; // done
5639 }
5640 }
5641 }
5642
5643 exprt prepared_expr = prepare_for_convert_expr(expr);
5644
5645#if 0
5646 out << "; CONV: "
5647 << format(expr) << "\n";
5648#endif
5649
5650 out << "; set_to " << (value?"true":"false") << "\n"
5651 << "(assert ";
5652 if(!value)
5653 {
5654 out << "(not ";
5655 }
5656 const auto found_literal = defined_expressions.find(expr);
5657 if(!(found_literal == defined_expressions.end()))
5658 {
5659 // This is a converted expression, we can just assert the literal name
5660 // since the expression is already defined
5661 out << found_literal->second;
5662 set_values[found_literal->second] = value;
5663 }
5664 else
5665 {
5666 convert_expr(prepared_expr);
5667 }
5668 if(!value)
5669 {
5670 out << ")";
5671 }
5672 out << ")\n";
5673 return;
5674}
5675
5683{
5684 exprt lowered_expr = expr;
5685
5686 for(auto it = lowered_expr.depth_begin(), itend = lowered_expr.depth_end();
5687 it != itend;
5688 ++it)
5689 {
5690 if(
5691 it->id() == ID_byte_extract_little_endian ||
5692 it->id() == ID_byte_extract_big_endian)
5693 {
5694 it.mutate() = lower_byte_extract(to_byte_extract_expr(*it), ns);
5695 }
5696 else if(
5697 it->id() == ID_byte_update_little_endian ||
5698 it->id() == ID_byte_update_big_endian)
5699 {
5700 it.mutate() = lower_byte_update(to_byte_update_expr(*it), ns);
5701 }
5702 }
5703
5704 return lowered_expr;
5705}
5706
5715{
5716 // First, replace byte operators, because they may introduce new array
5717 // expressions that must be seen by find_symbols:
5718 exprt lowered_expr = lower_byte_operators(expr);
5719 INVARIANT(
5720 !has_byte_operator(lowered_expr),
5721 "lower_byte_operators should remove all byte operators");
5722
5723 // Perform rewrites that may introduce new symbols
5724 for(auto it = lowered_expr.depth_begin(), itend = lowered_expr.depth_end();
5725 it != itend;) // no ++it
5726 {
5727 if(
5728 auto prophecy_r_or_w_ok =
5730 {
5731 exprt lowered = simplify_expr(prophecy_r_or_w_ok->lower(ns), ns);
5732 it.mutate() = lowered;
5733 it.next_sibling_or_parent();
5734 }
5735 else if(
5736 auto prophecy_pointer_in_range =
5738 {
5739 exprt lowered = simplify_expr(prophecy_pointer_in_range->lower(ns), ns);
5740 it.mutate() = lowered;
5741 it.next_sibling_or_parent();
5742 }
5743 else
5744 ++it;
5745 }
5746
5747 // Now create symbols for all composite expressions present in lowered_expr:
5748 find_symbols(lowered_expr);
5749
5750 return lowered_expr;
5751}
5752
5763{
5764 if(is_zero_width(expr.type(), ns))
5765 return;
5766
5767 // recursive call on type
5768 find_symbols(expr.type());
5769
5770 if(expr.id() == ID_exists || expr.id() == ID_forall)
5771 {
5772 std::unordered_map<irep_idt, std::optional<identifiert>> shadowed_syms;
5773
5774 // do not declare the quantified symbol, but record
5775 // as 'bound symbol'
5776 const auto &q_expr = to_quantifier_expr(expr);
5777 for(const auto &symbol : q_expr.variables())
5778 {
5779 const auto identifier = symbol.identifier();
5780 auto id_entry =
5781 identifier_map.insert({identifier, identifiert{symbol.type(), true}});
5782 shadowed_syms.insert(
5783 {identifier,
5784 id_entry.second ? std::nullopt
5785 : std::optional{id_entry.first->second}});
5786 }
5787 find_symbols(q_expr.where());
5788 for(const auto &[id, shadowed_val] : shadowed_syms)
5789 {
5790 auto previous_entry = identifier_map.find(id);
5791 if(!shadowed_val.has_value())
5792 identifier_map.erase(previous_entry);
5793 else
5794 previous_entry->second = std::move(*shadowed_val);
5795 }
5796 return;
5797 }
5798
5799 // recursive call on operands
5800 for(const auto &op : expr.operands())
5801 find_symbols(op);
5802
5803 if(expr.id()==ID_symbol ||
5804 expr.id()==ID_nondet_symbol)
5805 {
5806 // we don't track function-typed symbols
5807 if(expr.type().id()==ID_code)
5808 return;
5809
5810 irep_idt identifier;
5811
5812 if(expr.id()==ID_symbol)
5813 identifier = to_symbol_expr(expr).identifier();
5814 else
5815 identifier="nondet_"+
5816 id2string(to_nondet_symbol_expr(expr).get_identifier());
5817
5818 auto id_entry =
5819 identifier_map.insert({identifier, identifiert{expr.type(), false}});
5820
5821 if(id_entry.second)
5822 {
5823 std::string smt2_identifier=convert_identifier(identifier);
5824 smt2_identifiers.insert(smt2_identifier);
5825
5826 out << "; find_symbols\n";
5827 out << "(declare-fun " << smt2_identifier;
5828
5829 if(expr.type().id() == ID_mathematical_function)
5830 {
5831 auto &mathematical_function_type =
5833 out << " (";
5834 bool first = true;
5835
5836 for(auto &type : mathematical_function_type.domain())
5837 {
5838 if(first)
5839 first = false;
5840 else
5841 out << ' ';
5842 convert_type(type);
5843 }
5844
5845 out << ") ";
5846 convert_type(mathematical_function_type.codomain());
5847 }
5848 else
5849 {
5850 out << " () ";
5851 convert_type(expr.type());
5852 }
5853
5854 out << ')' << '\n';
5855
5856 // We need an additional constraint for range-typed symbols,
5857 // or otherwise we get satisfying assignments with values
5858 // outside of the range when the size of the range isn't
5859 // a power of two.
5860 if(expr.type().id() == ID_range)
5861 {
5862 auto &range_type = to_integer_range_type(expr.type());
5863 if(!is_power_of_two(range_type.size()))
5864 {
5865 out << "(assert (bvule " << smt2_identifier << ' ';
5866 convert_expr(from_integer(range_type.to(), range_type));
5867 out << "))\n"; // bvule, assert
5868 }
5869 }
5870 }
5871 }
5872 else if(expr.id() == ID_array_of)
5873 {
5874 if(!use_as_const)
5875 {
5876 if(defined_expressions.find(expr) == defined_expressions.end())
5877 {
5878 const auto &array_of = to_array_of_expr(expr);
5879 const auto &array_type = array_of.type();
5880
5881 const irep_idt id =
5882 "array_of." + std::to_string(defined_expressions.size());
5883 out << "; the following is a substitute for lambda i. x\n";
5884 out << "(declare-fun " << id << " () ";
5885 convert_type(array_type);
5886 out << ")\n";
5887
5888 if(!is_zero_width(array_type.element_type(), ns))
5889 {
5890 // use a quantifier-based initialization instead of lambda
5891 out << "(assert (forall ((i ";
5892 convert_type(array_type.index_type());
5893 out << ")) (= (select " << id << " i) ";
5894 if(array_type.element_type().id() == ID_bool && !use_array_of_bool)
5895 {
5896 out << "(ite ";
5897 convert_expr(array_of.what());
5898 out << " #b1 #b0)";
5899 }
5900 else
5901 {
5902 convert_expr(array_of.what());
5903 }
5904 out << ")))\n";
5905 }
5906
5907 defined_expressions[expr] = id;
5908 }
5909 }
5910 }
5911 else if(expr.id() == ID_array_comprehension)
5912 {
5914 {
5915 if(defined_expressions.find(expr) == defined_expressions.end())
5916 {
5917 const auto &array_comprehension = to_array_comprehension_expr(expr);
5918 const auto &array_type = array_comprehension.type();
5919 const auto &array_size = array_type.size();
5920
5921 const irep_idt id =
5922 "array_comprehension." + std::to_string(defined_expressions.size());
5923 out << "(declare-fun " << id << " () ";
5924 convert_type(array_type);
5925 out << ")\n";
5926
5927 out << "; the following is a substitute for lambda i . x(i)\n";
5928 out << "; universally quantified initialization of the array\n";
5929 out << "(assert (forall ((";
5930 convert_expr(array_comprehension.arg());
5931 out << " ";
5932 convert_type(array_size.type());
5933 out << ")) (=> (and (bvule (_ bv0 " << boolbv_width(array_size.type())
5934 << ") ";
5935 convert_expr(array_comprehension.arg());
5936 out << ") (bvult ";
5937 convert_expr(array_comprehension.arg());
5938 out << " ";
5939 convert_expr(array_size);
5940 out << ")) (= (select " << id << " ";
5941 convert_expr(array_comprehension.arg());
5942 out << ") ";
5943 if(array_type.element_type().id() == ID_bool && !use_array_of_bool)
5944 {
5945 out << "(ite ";
5946 convert_expr(array_comprehension.body());
5947 out << " #b1 #b0)";
5948 }
5949 else
5950 {
5951 convert_expr(array_comprehension.body());
5952 }
5953 out << "))))\n";
5954
5955 defined_expressions[expr] = id;
5956 }
5957 }
5958 }
5959 else if(expr.id()==ID_array)
5960 {
5961 if(defined_expressions.find(expr)==defined_expressions.end())
5962 {
5963 const array_typet &array_type=to_array_type(expr.type());
5964
5965 const irep_idt id = "array." + std::to_string(defined_expressions.size());
5966 out << "; the following is a substitute for an array constructor" << "\n";
5967 out << "(declare-fun " << id << " () ";
5968 convert_type(array_type);
5969 out << ")" << "\n";
5970
5971 if(!is_zero_width(array_type.element_type(), ns))
5972 {
5973 for(std::size_t i = 0; i < expr.operands().size(); i++)
5974 {
5975 out << "(assert (= (select " << id << " ";
5976 convert_expr(from_integer(i, array_type.index_type()));
5977 out << ") "; // select
5978 if(array_type.element_type().id() == ID_bool && !use_array_of_bool)
5979 {
5980 out << "(ite ";
5981 convert_expr(expr.operands()[i]);
5982 out << " #b1 #b0)";
5983 }
5984 else
5985 {
5986 convert_expr(expr.operands()[i]);
5987 }
5988 out << "))"
5989 << "\n"; // =, assert
5990 }
5991 }
5992
5993 defined_expressions[expr]=id;
5994 }
5995 }
5996 else if(expr.id()==ID_string_constant)
5997 {
5998 if(defined_expressions.find(expr)==defined_expressions.end())
5999 {
6000 // introduce a temporary array.
6002 const array_typet &array_type=to_array_type(tmp.type());
6003
6004 const irep_idt id =
6005 "string." + std::to_string(defined_expressions.size());
6006 out << "; the following is a substitute for a string" << "\n";
6007 out << "(declare-fun " << id << " () ";
6008 convert_type(array_type);
6009 out << ")" << "\n";
6010
6011 for(std::size_t i=0; i<tmp.operands().size(); i++)
6012 {
6013 out << "(assert (= (select " << id << ' ';
6014 convert_expr(from_integer(i, array_type.index_type()));
6015 out << ") "; // select
6016 convert_expr(tmp.operands()[i]);
6017 out << "))" << "\n";
6018 }
6019
6020 defined_expressions[expr]=id;
6021 }
6022 }
6023 else if(
6025 {
6026 if(object_sizes.find(*object_size) == object_sizes.end())
6027 {
6028 const irep_idt id = convert_identifier(
6029 "object_size." + std::to_string(object_sizes.size()));
6030 out << "(declare-fun " << id << " () ";
6031 convert_type(object_size->type());
6032 out << ")"
6033 << "\n";
6034
6036 }
6037 }
6038 // clang-format off
6039 else if(!use_FPA_theory &&
6040 expr.operands().size() >= 1 &&
6041 (expr.id() == ID_floatbv_plus ||
6042 expr.id() == ID_floatbv_minus ||
6043 expr.id() == ID_floatbv_mult ||
6044 expr.id() == ID_floatbv_div ||
6045 expr.id() == ID_floatbv_fma ||
6046 expr.id() == ID_floatbv_typecast ||
6047 expr.id() == ID_ieee_float_equal ||
6048 expr.id() == ID_ieee_float_notequal ||
6049 ((expr.id() == ID_lt ||
6050 expr.id() == ID_gt ||
6051 expr.id() == ID_le ||
6052 expr.id() == ID_ge ||
6053 expr.id() == ID_isnan ||
6054 expr.id() == ID_isnormal ||
6055 expr.id() == ID_isfinite ||
6056 expr.id() == ID_isinf ||
6057 expr.id() == ID_sign ||
6058 expr.id() == ID_unary_minus ||
6059 expr.id() == ID_typecast ||
6060 expr.id() == ID_abs) &&
6061 to_multi_ary_expr(expr).op0().type().id() == ID_floatbv)))
6062 // clang-format on
6063 {
6064 irep_idt function =
6065 convert_identifier("float_bv." + expr.id_string() + floatbv_suffix(expr));
6066
6067 if(bvfp_set.insert(function).second)
6068 {
6069 out << "; this is a model for " << expr.id() << " : "
6070 << type2id(to_multi_ary_expr(expr).op0().type()) << " -> "
6071 << type2id(expr.type()) << "\n"
6072 << "(define-fun " << function << " (";
6073
6074 for(std::size_t i = 0; i < expr.operands().size(); i++)
6075 {
6076 if(i!=0)
6077 out << " ";
6078 out << "(op" << i << ' ';
6079 convert_type(expr.operands()[i].type());
6080 out << ')';
6081 }
6082
6083 out << ") ";
6084 convert_type(expr.type()); // return type
6085 out << ' ';
6086
6087 exprt tmp1=expr;
6088 for(std::size_t i = 0; i < tmp1.operands().size(); i++)
6089 tmp1.operands()[i]=
6090 smt2_symbolt("op"+std::to_string(i), tmp1.operands()[i].type());
6091
6092 exprt tmp2=float_bv(tmp1);
6093 tmp2=letify(tmp2);
6094 CHECK_RETURN(!tmp2.is_nil());
6095
6096 convert_expr(tmp2);
6097
6098 out << ")\n"; // define-fun
6099 }
6100 }
6101 else if(
6102 use_FPA_theory && expr.id() == ID_typecast &&
6103 to_typecast_expr(expr).op().type().id() == ID_floatbv &&
6104 expr.type().id() == ID_bv)
6105 {
6106 // This is _NOT_ a semantic conversion, but bit-wise.
6107 if(defined_expressions.find(expr) == defined_expressions.end())
6108 {
6109 // This conversion is non-trivial as it requires creating a
6110 // new bit-vector variable and then asserting that it converts
6111 // to the required floating-point number.
6112 const irep_idt id =
6113 "bvfromfloat." + std::to_string(defined_expressions.size());
6114 out << "(declare-fun " << id << " () ";
6115 convert_type(expr.type());
6116 out << ')' << '\n';
6117
6118 const typecast_exprt &tc = to_typecast_expr(expr);
6119 const auto &floatbv_type = to_floatbv_type(tc.op().type());
6120 out << "(assert (= ";
6121 out << "((_ to_fp " << floatbv_type.get_e() << " "
6122 << floatbv_type.get_f() + 1 << ") " << id << ')';
6123 convert_expr(tc.op());
6124 out << ')'; // =
6125 out << ')' << '\n';
6126
6127 defined_expressions[expr] = id;
6128 }
6129 }
6130 else if(expr.id() == ID_initial_state)
6131 {
6132 irep_idt function = "initial-state";
6133
6134 if(state_fkt_declared.insert(function).second)
6135 {
6136 out << "(declare-fun " << function << " (";
6137 convert_type(to_unary_expr(expr).op().type());
6138 out << ") ";
6139 convert_type(expr.type()); // return type
6140 out << ")\n"; // declare-fun
6141 }
6142 }
6143 else if(expr.id() == ID_evaluate)
6144 {
6145 irep_idt function = "evaluate-" + type2id(expr.type());
6146
6147 if(state_fkt_declared.insert(function).second)
6148 {
6149 out << "(declare-fun " << function << " (";
6150 convert_type(to_binary_expr(expr).op0().type());
6151 out << ' ';
6152 convert_type(to_binary_expr(expr).op1().type());
6153 out << ") ";
6154 convert_type(expr.type()); // return type
6155 out << ")\n"; // declare-fun
6156 }
6157 }
6158 else if(
6159 expr.id() == ID_state_is_cstring ||
6160 expr.id() == ID_state_is_dynamic_object ||
6161 expr.id() == ID_state_live_object || expr.id() == ID_state_writeable_object)
6162 {
6163 irep_idt function =
6164 expr.id() == ID_state_is_cstring ? "state-is-cstring"
6165 : expr.id() == ID_state_is_dynamic_object ? "state-is-dynamic-object"
6166 : expr.id() == ID_state_live_object ? "state-live-object"
6167 : "state-writeable-object";
6168
6169 if(state_fkt_declared.insert(function).second)
6170 {
6171 out << "(declare-fun " << function << " (";
6172 convert_type(to_binary_expr(expr).op0().type());
6173 out << ' ';
6174 convert_type(to_binary_expr(expr).op1().type());
6175 out << ") ";
6176 convert_type(expr.type()); // return type
6177 out << ")\n"; // declare-fun
6178 }
6179 }
6180 else if(
6181 expr.id() == ID_state_r_ok || expr.id() == ID_state_w_ok ||
6182 expr.id() == ID_state_rw_ok)
6183 {
6184 irep_idt function = expr.id() == ID_state_r_ok ? "state-r-ok"
6185 : expr.id() == ID_state_w_ok ? "state-w-ok"
6186 : "state-rw-ok";
6187
6188 if(state_fkt_declared.insert(function).second)
6189 {
6190 out << "(declare-fun " << function << " (";
6191 convert_type(to_ternary_expr(expr).op0().type());
6192 out << ' ';
6193 convert_type(to_ternary_expr(expr).op1().type());
6194 out << ' ';
6195 convert_type(to_ternary_expr(expr).op2().type());
6196 out << ") ";
6197 convert_type(expr.type()); // return type
6198 out << ")\n"; // declare-fun
6199 }
6200 }
6201 else if(expr.id() == ID_update_state)
6202 {
6203 irep_idt function =
6204 "update-state-" + type2id(to_multi_ary_expr(expr).op2().type());
6205
6206 if(state_fkt_declared.insert(function).second)
6207 {
6208 out << "(declare-fun " << function << " (";
6209 convert_type(to_multi_ary_expr(expr).op0().type());
6210 out << ' ';
6211 convert_type(to_multi_ary_expr(expr).op1().type());
6212 out << ' ';
6213 convert_type(to_multi_ary_expr(expr).op2().type());
6214 out << ") ";
6215 convert_type(expr.type()); // return type
6216 out << ")\n"; // declare-fun
6217 }
6218 }
6219 else if(expr.id() == ID_enter_scope_state)
6220 {
6221 irep_idt function =
6222 "enter-scope-state-" + type2id(to_binary_expr(expr).op1().type());
6223
6224 if(state_fkt_declared.insert(function).second)
6225 {
6226 out << "(declare-fun " << function << " (";
6227 convert_type(to_binary_expr(expr).op0().type());
6228 out << ' ';
6229 convert_type(to_binary_expr(expr).op1().type());
6230 out << ' ';
6232 out << ") ";
6233 convert_type(expr.type()); // return type
6234 out << ")\n"; // declare-fun
6235 }
6236 }
6237 else if(expr.id() == ID_exit_scope_state)
6238 {
6239 irep_idt function =
6240 "exit-scope-state-" + type2id(to_binary_expr(expr).op1().type());
6241
6242 if(state_fkt_declared.insert(function).second)
6243 {
6244 out << "(declare-fun " << function << " (";
6245 convert_type(to_binary_expr(expr).op0().type());
6246 out << ' ';
6247 convert_type(to_binary_expr(expr).op1().type());
6248 out << ") ";
6249 convert_type(expr.type()); // return type
6250 out << ")\n"; // declare-fun
6251 }
6252 }
6253 else if(expr.id() == ID_allocate)
6254 {
6255 irep_idt function = "allocate";
6256
6257 if(state_fkt_declared.insert(function).second)
6258 {
6259 out << "(declare-fun " << function << " (";
6260 convert_type(to_binary_expr(expr).op0().type());
6261 out << ' ';
6262 convert_type(to_binary_expr(expr).op1().type());
6263 out << ") ";
6264 convert_type(expr.type()); // return type
6265 out << ")\n"; // declare-fun
6266 }
6267 }
6268 else if(expr.id() == ID_reallocate)
6269 {
6270 irep_idt function = "reallocate";
6271
6272 if(state_fkt_declared.insert(function).second)
6273 {
6274 out << "(declare-fun " << function << " (";
6275 convert_type(to_ternary_expr(expr).op0().type());
6276 out << ' ';
6277 convert_type(to_ternary_expr(expr).op1().type());
6278 out << ' ';
6279 convert_type(to_ternary_expr(expr).op2().type());
6280 out << ") ";
6281 convert_type(expr.type()); // return type
6282 out << ")\n"; // declare-fun
6283 }
6284 }
6285 else if(expr.id() == ID_deallocate_state)
6286 {
6287 irep_idt function = "deallocate";
6288
6289 if(state_fkt_declared.insert(function).second)
6290 {
6291 out << "(declare-fun " << function << " (";
6292 convert_type(to_binary_expr(expr).op0().type());
6293 out << ' ';
6294 convert_type(to_binary_expr(expr).op1().type());
6295 out << ") ";
6296 convert_type(expr.type()); // return type
6297 out << ")\n"; // declare-fun
6298 }
6299 }
6300 else if(expr.id() == ID_object_address)
6301 {
6302 irep_idt function = "object-address";
6303
6304 if(state_fkt_declared.insert(function).second)
6305 {
6306 out << "(declare-fun " << function << " (String) ";
6307 convert_type(expr.type()); // return type
6308 out << ")\n"; // declare-fun
6309 }
6310 }
6311 else if(expr.id() == ID_field_address)
6312 {
6313 irep_idt function = "field-address-" + type2id(expr.type());
6314
6315 if(state_fkt_declared.insert(function).second)
6316 {
6317 out << "(declare-fun " << function << " (";
6318 convert_type(to_field_address_expr(expr).op().type());
6319 out << ' ';
6320 out << "String";
6321 out << ") ";
6322 convert_type(expr.type()); // return type
6323 out << ")\n"; // declare-fun
6324 }
6325 }
6326 else if(expr.id() == ID_element_address)
6327 {
6328 irep_idt function = "element-address-" + type2id(expr.type());
6329
6330 if(state_fkt_declared.insert(function).second)
6331 {
6332 out << "(declare-fun " << function << " (";
6333 convert_type(to_element_address_expr(expr).base().type());
6334 out << ' ';
6335 convert_type(to_element_address_expr(expr).index().type());
6336 out << ' '; // repeat, for the element size
6337 convert_type(to_element_address_expr(expr).index().type());
6338 out << ") ";
6339 convert_type(expr.type()); // return type
6340 out << ")\n"; // declare-fun
6341 }
6342 }
6343}
6344
6346{
6347 const typet &type = expr.type();
6348 PRECONDITION(type.id()==ID_array);
6349
6350 // arrays inside structs get flattened, unless we have datatypes
6351 if(expr.id() == ID_with)
6352 return use_array_theory(to_with_expr(expr).old());
6353 else if(expr.id() == ID_if)
6354 {
6355 // For an array-typed if-then-else, the SMT sort produced by
6356 // convert_expr (see the ID_if branch above) is determined by the
6357 // sorts chosen for its two operands:
6358 // - if both branches are bit-vector-encoded, i.e. neither uses array
6359 // theory (typically because both are array-typed members of a
6360 // struct that has been flattened to a bit-vector), the resulting
6361 // ite is a bit-vector;
6362 // - if both branches use array theory, the ite is an SMT array;
6363 // - if exactly one branch uses array theory, the ID_if handler in
6364 // convert_expr unflattens the bit-vector branch back to an SMT
6365 // array (see the wheret::BEGIN/wheret::END unflatten calls), so
6366 // the ite is again an SMT array.
6367 // The ite therefore "uses array theory" iff at least one branch
6368 // does. Without this clause, the fall-through below would
6369 // unconditionally return true for ID_if (since ID_if != ID_member),
6370 // which is wrong in the symmetric bit-vector case: callers like
6371 // convert_index, convert_with, flatten2bv, and the array-typed
6372 // define-fun path would then emit array-theory operators -- e.g.
6373 // (select <ite> ...) or (store <ite> ...) -- on a bit-vector
6374 // operand, producing ill-typed SMT-LIB 2 that is rejected by
6375 // conforming solvers (cf. issue #9008). The asymmetric case was
6376 // already handled, for ID_with branches, in the ID_if conversion
6377 // logic of convert_expr; the present clause makes use_array_theory
6378 // consistent with that conversion in all four combinations.
6379 const if_exprt &if_expr = to_if_expr(expr);
6380 return use_array_theory(if_expr.true_case()) ||
6381 use_array_theory(if_expr.false_case());
6382 }
6383 else
6384 return use_datatypes || expr.id() != ID_member;
6385}
6386
6388{
6389 if(type.id()==ID_array)
6390 {
6391 const array_typet &array_type = to_array_type(type);
6392
6393 // we always use array theory for top-level arrays
6394 const typet &subtype = array_type.element_type();
6395
6396 // Arrays map the index type to the element type.
6397 out << "(Array ";
6398 convert_type(array_type.index_type());
6399 out << " ";
6400
6401 if(subtype.id()==ID_bool && !use_array_of_bool)
6402 out << "(_ BitVec 1)";
6403 else
6404 convert_type(array_type.element_type());
6405
6406 out << ")";
6407 }
6408 else if(type.id()==ID_bool)
6409 {
6410 out << "Bool";
6411 }
6412 else if(type.id() == ID_struct || type.id() == ID_struct_tag)
6413 {
6414 if(use_datatypes)
6415 {
6416 out << datatype_map.at(type);
6417 }
6418 else
6419 {
6420 std::size_t width=boolbv_width(type);
6421
6422 out << "(_ BitVec " << width << ")";
6423 }
6424 }
6425 else if(type.id()==ID_code)
6426 {
6427 // These may appear in structs.
6428 // We replace this by "Bool" in order to keep the same
6429 // member count.
6430 out << "Bool";
6431 }
6432 else if(type.id() == ID_union || type.id() == ID_union_tag)
6433 {
6434 std::size_t width=boolbv_width(type);
6435 const union_typet &union_type = type.id() == ID_union_tag
6436 ? ns.follow_tag(to_union_tag_type(type))
6437 : to_union_type(type);
6439 union_type.components().empty() || width != 0,
6440 "failed to get width of union");
6441
6442 out << "(_ BitVec " << width << ")";
6443 }
6444 else if(type.id()==ID_pointer)
6445 {
6446 out << "(_ BitVec "
6447 << boolbv_width(type) << ")";
6448 }
6449 else if(type.id()==ID_bv ||
6450 type.id()==ID_fixedbv ||
6451 type.id()==ID_unsignedbv ||
6452 type.id()==ID_signedbv ||
6453 type.id()==ID_c_bool)
6454 {
6455 out << "(_ BitVec "
6456 << to_bitvector_type(type).get_width() << ")";
6457 }
6458 else if(type.id()==ID_c_enum)
6459 {
6460 // these have an underlying type
6461 out << "(_ BitVec "
6462 << to_bitvector_type(to_c_enum_type(type).underlying_type()).get_width()
6463 << ")";
6464 }
6465 else if(type.id()==ID_c_enum_tag)
6466 {
6467 convert_type(ns.follow_tag(to_c_enum_tag_type(type)));
6468 }
6469 else if(type.id()==ID_floatbv)
6470 {
6471 const floatbv_typet &floatbv_type=to_floatbv_type(type);
6472
6473 if(use_FPA_theory)
6474 out << "(_ FloatingPoint "
6475 << floatbv_type.get_e() << " "
6476 << floatbv_type.get_f() + 1 << ")";
6477 else
6478 out << "(_ BitVec "
6479 << floatbv_type.get_width() << ")";
6480 }
6481 else if(type.id()==ID_rational ||
6482 type.id()==ID_real)
6483 out << "Real";
6484 else if(type.id()==ID_integer)
6485 out << "Int";
6486 else if(type.id() == ID_natural)
6487 out << "Nat";
6488 else if(type.id()==ID_complex)
6489 {
6490 if(use_datatypes)
6491 {
6492 out << datatype_map.at(type);
6493 }
6494 else
6495 {
6496 std::size_t width=boolbv_width(type);
6497
6498 out << "(_ BitVec " << width << ")";
6499 }
6500 }
6501 else if(type.id()==ID_c_bit_field)
6502 {
6504 }
6505 else if(type.id() == ID_state)
6506 {
6507 out << "state";
6508 }
6509 else if(type.id() == ID_range)
6510 {
6511 auto &range_type = to_integer_range_type(type);
6512 if(range_type.empty())
6513 UNEXPECTEDCASE("unsupported range type");
6514 out << "(_ BitVec " << address_bits(range_type.size()) << ")";
6515 }
6516 else if(type.id() == ID_string)
6517 out << "String";
6518 else if(type.id() == ID_regex)
6519 out << "RegLan";
6520 else
6521 {
6522 UNEXPECTEDCASE("unsupported type: "+type.id_string());
6523 }
6524}
6525
6527{
6528 std::set<irep_idt> recstack;
6529 find_symbols_rec(type, recstack);
6530}
6531
6533 const typet &type,
6534 std::set<irep_idt> &recstack)
6535{
6536 if(type.id()==ID_array)
6537 {
6538 const array_typet &array_type=to_array_type(type);
6539 find_symbols(array_type.size());
6540 find_symbols_rec(array_type.element_type(), recstack);
6541 }
6542 else if(type.id()==ID_complex)
6543 {
6544 find_symbols_rec(to_complex_type(type).subtype(), recstack);
6545
6546 if(use_datatypes &&
6547 datatype_map.find(type)==datatype_map.end())
6548 {
6549 const std::string smt_typename =
6550 "complex." + std::to_string(datatype_map.size());
6551 datatype_map[type] = smt_typename;
6552
6553 out << "(declare-datatypes ((" << smt_typename << " 0)) "
6554 << "(((mk-" << smt_typename;
6555
6556 out << " (" << smt_typename << ".imag ";
6557 convert_type(to_complex_type(type).subtype());
6558 out << ")";
6559
6560 out << " (" << smt_typename << ".real ";
6561 convert_type(to_complex_type(type).subtype());
6562 out << ")";
6563
6564 out << "))))\n";
6565 }
6566 }
6567 else if(type.id() == ID_struct)
6568 {
6569 // Cater for mutually recursive struct types
6570 bool need_decl=false;
6571 if(use_datatypes &&
6572 datatype_map.find(type)==datatype_map.end())
6573 {
6574 const std::string smt_typename =
6575 "struct." + std::to_string(datatype_map.size());
6576 datatype_map[type] = smt_typename;
6577 need_decl=true;
6578 }
6579
6580 const struct_typet::componentst &components =
6581 to_struct_type(type).components();
6582
6583 for(const auto &component : components)
6584 find_symbols_rec(component.type(), recstack);
6585
6586 // Declare the corresponding SMT type if we haven't already.
6587 if(need_decl)
6588 {
6589 const std::string &smt_typename = datatype_map.at(type);
6590
6591 // We're going to create a datatype named something like `struct.0'.
6592 // It's going to have a single constructor named `mk-struct.0' with an
6593 // argument for each member of the struct. The declaration that
6594 // creates this type looks like:
6595 //
6596 // (declare-datatypes ((struct.0 0)) (((mk-struct.0
6597 // (struct.0.component1 type1)
6598 // ...
6599 // (struct.0.componentN typeN)))))
6600 out << "(declare-datatypes ((" << smt_typename << " 0)) "
6601 << "(((mk-" << smt_typename << " ";
6602
6603 for(const auto &component : components)
6604 {
6605 if(is_zero_width(component.type(), ns))
6606 continue;
6607
6608 out << "(" << smt_typename << "." << component.get_name()
6609 << " ";
6610 convert_type(component.type());
6611 out << ") ";
6612 }
6613
6614 out << "))))" << "\n";
6615
6616 // Let's also declare convenience functions to update individual
6617 // members of the struct whil we're at it. The functions are
6618 // named like `update-struct.0.component1'. Their declarations
6619 // look like:
6620 //
6621 // (declare-fun update-struct.0.component1
6622 // ((s struct.0) ; first arg -- the struct to update
6623 // (v type1)) ; second arg -- the value to update
6624 // struct.0 ; the output type
6625 // (mk-struct.0 ; build the new struct...
6626 // v ; the updated value
6627 // (struct.0.component2 s) ; retain the other members
6628 // ...
6629 // (struct.0.componentN s)))
6630
6631 for(struct_union_typet::componentst::const_iterator
6632 it=components.begin();
6633 it!=components.end();
6634 ++it)
6635 {
6636 if(is_zero_width(it->type(), ns))
6637 continue;
6638
6640 out << "(define-fun update-" << smt_typename << "."
6641 << component.get_name() << " "
6642 << "((s " << smt_typename << ") "
6643 << "(v ";
6644 convert_type(component.type());
6645 out << ")) " << smt_typename << " "
6646 << "(mk-" << smt_typename
6647 << " ";
6648
6649 for(struct_union_typet::componentst::const_iterator
6650 it2=components.begin();
6651 it2!=components.end();
6652 ++it2)
6653 {
6654 if(it==it2)
6655 out << "v ";
6656 else if(!is_zero_width(it2->type(), ns))
6657 {
6658 out << "(" << smt_typename << "."
6659 << it2->get_name() << " s) ";
6660 }
6661 }
6662
6663 out << "))" << "\n";
6664 }
6665
6666 out << "\n";
6667 }
6668 }
6669 else if(type.id() == ID_union)
6670 {
6671 const union_typet::componentst &components =
6672 to_union_type(type).components();
6673
6674 for(const auto &component : components)
6675 find_symbols_rec(component.type(), recstack);
6676 }
6677 else if(type.id()==ID_code)
6678 {
6679 const code_typet::parameterst &parameters=
6680 to_code_type(type).parameters();
6681 for(const auto &param : parameters)
6682 find_symbols_rec(param.type(), recstack);
6683
6684 find_symbols_rec(to_code_type(type).return_type(), recstack);
6685 }
6686 else if(type.id()==ID_pointer)
6687 {
6688 find_symbols_rec(to_pointer_type(type).base_type(), recstack);
6689 }
6690 else if(type.id() == ID_struct_tag)
6691 {
6692 const auto &struct_tag = to_struct_tag_type(type);
6693 const irep_idt &id = struct_tag.get_identifier();
6694
6695 if(recstack.find(id) == recstack.end())
6696 {
6697 const auto &base_struct = ns.follow_tag(struct_tag);
6698 recstack.insert(id);
6699 find_symbols_rec(base_struct, recstack);
6700 datatype_map[type] = datatype_map[base_struct];
6701 }
6702 }
6703 else if(type.id() == ID_union_tag)
6704 {
6705 const auto &union_tag = to_union_tag_type(type);
6706 const irep_idt &id = union_tag.get_identifier();
6707
6708 if(recstack.find(id) == recstack.end())
6709 {
6710 recstack.insert(id);
6711 find_symbols_rec(ns.follow_tag(union_tag), recstack);
6712 }
6713 }
6714 else if(type.id() == ID_state)
6715 {
6716 if(datatype_map.find(type) == datatype_map.end())
6717 {
6718 datatype_map[type] = "state";
6719 out << "(declare-sort state 0)\n";
6720 }
6721 }
6722 else if(type.id() == ID_mathematical_function)
6723 {
6724 const auto &mathematical_function_type =
6726 for(auto &d_type : mathematical_function_type.domain())
6727 find_symbols_rec(d_type, recstack);
6728
6729 find_symbols_rec(mathematical_function_type.codomain(), recstack);
6730 }
6731}
6732
6734{
6736}
configt config
Definition config.cpp:25
mp_integer bvrep2integer(const irep_idt &src, std::size_t width, bool is_signed)
convert a bit-vector representation (possibly signed) to integer
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
std::size_t address_bits(const mp_integer &size)
ceil(log2(size))
bool to_integer(const constant_exprt &expr, mp_integer &int_value)
Convert a constant expression expr to an arbitrary-precision integer.
bool is_power_of_two(const mp_integer &n)
irep_idt integer2bvrep(const mp_integer &src, std::size_t width)
convert an integer to bit-vector representation with given width This uses two's complement for negat...
mp_integer power(const mp_integer &base, const mp_integer &exponent)
A multi-precision implementation of the power operator.
Target numeric_cast_v(const mp_integer &arg)
Convert an mp_integer to integral type Target An invariant will fail if the conversion is not possibl...
std::optional< Target > numeric_cast(const exprt &arg)
Converts an expression to any integral type.
API to expression classes for bitvectors.
const onehot0_exprt & to_onehot0_expr(const exprt &expr)
Cast an exprt to a onehot0_exprt.
const replication_exprt & to_replication_expr(const exprt &expr)
Cast an exprt to a replication_exprt.
const reduction_xnor_exprt & to_reduction_xnor_expr(const exprt &expr)
Cast an exprt to a reduction_xnor_exprt.
const reduction_nand_exprt & to_reduction_nand_expr(const exprt &expr)
Cast an exprt to a reduction_nand_exprt.
const shift_exprt & to_shift_expr(const exprt &expr)
Cast an exprt to a shift_exprt.
const reduction_and_exprt & to_reduction_and_expr(const exprt &expr)
Cast an exprt to a reduction_and_exprt.
const reduction_or_exprt & to_reduction_or_expr(const exprt &expr)
Cast an exprt to a reduction_or_exprt.
const popcount_exprt & to_popcount_expr(const exprt &expr)
Cast an exprt to a popcount_exprt.
const update_bits_exprt & to_update_bits_expr(const exprt &expr)
Cast an exprt to an update_bits_exprt.
const extractbits_exprt & to_extractbits_expr(const exprt &expr)
Cast an exprt to an extractbits_exprt.
const onehot_exprt & to_onehot_expr(const exprt &expr)
Cast an exprt to a onehot_exprt.
bool can_cast_expr< minus_overflow_exprt >(const exprt &base)
const find_first_set_exprt & to_find_first_set_expr(const exprt &expr)
Cast an exprt to a find_first_set_exprt.
bool can_cast_expr< overflow_result_exprt >(const exprt &base)
const update_bit_exprt & to_update_bit_expr(const exprt &expr)
Cast an exprt to an update_bit_exprt.
const bitnot_exprt & to_bitnot_expr(const exprt &expr)
Cast an exprt to a bitnot_exprt.
const bswap_exprt & to_bswap_expr(const exprt &expr)
Cast an exprt to a bswap_exprt.
const count_leading_zeros_exprt & to_count_leading_zeros_expr(const exprt &expr)
Cast an exprt to a count_leading_zeros_exprt.
const reduction_xor_exprt & to_reduction_xor_expr(const exprt &expr)
Cast an exprt to a reduction_xor_exprt.
const bitreverse_exprt & to_bitreverse_expr(const exprt &expr)
Cast an exprt to a bitreverse_exprt.
const extractbit_exprt & to_extractbit_expr(const exprt &expr)
Cast an exprt to an extractbit_exprt.
const reduction_nor_exprt & to_reduction_nor_expr(const exprt &expr)
Cast an exprt to a reduction_nor_exprt.
const zero_extend_exprt & to_zero_extend_expr(const exprt &expr)
Cast an exprt to a zero_extend_exprt.
const count_trailing_zeros_exprt & to_count_trailing_zeros_expr(const exprt &expr)
Cast an exprt to a count_trailing_zeros_exprt.
const bv_typet & to_bv_type(const typet &type)
Cast a typet to a bv_typet.
const fixedbv_typet & to_fixedbv_type(const typet &type)
Cast a typet to a fixedbv_typet.
const bitvector_typet & to_bitvector_type(const typet &type)
Cast a typet to a bitvector_typet.
const floatbv_typet & to_floatbv_type(const typet &type)
Cast a typet to a floatbv_typet.
const unsignedbv_typet & to_unsignedbv_type(const typet &type)
Cast a typet to an unsignedbv_typet.
const signedbv_typet & to_signedbv_type(const typet &type)
Cast a typet to a signedbv_typet.
bool has_byte_operator(const exprt &src)
Return true iff src or one of its operands contain a byte extract or byte update expression.
Expression classes for byte-level operators.
const byte_update_exprt & to_byte_update_expr(const exprt &expr)
exprt lower_byte_extract(const byte_extract_exprt &src, const namespacet &ns)
Rewrite a byte extract expression to more fundamental operations.
const byte_extract_exprt & to_byte_extract_expr(const exprt &expr)
exprt lower_byte_update(const byte_update_exprt &src, const namespacet &ns)
Rewrite a byte update expression to more fundamental operations.
int16_t s2
int8_t s1
typet c_bit_field_replacement_type(const c_bit_field_typet &src, const namespacet &ns)
pointer_typet pointer_type(const typet &subtype)
Definition c_types.cpp:235
const c_bit_field_typet & to_c_bit_field_type(const typet &type)
Cast a typet to a c_bit_field_typet.
Definition c_types.h:80
const c_enum_typet & to_c_enum_type(const typet &type)
Cast a typet to a c_enum_typet.
Definition c_types.h:335
const c_enum_tag_typet & to_c_enum_tag_type(const typet &type)
Cast a typet to a c_enum_tag_typet.
Definition c_types.h:377
const union_typet & to_union_type(const typet &type)
Cast a typet to a union_typet.
Definition c_types.h:184
const c_bool_typet & to_c_bool_type(const typet &type)
Cast a typet to a c_bool_typet.
Definition c_types.h:128
const union_tag_typet & to_union_tag_type(const typet &type)
Cast a typet to a union_tag_typet.
Definition c_types.h:224
Absolute value.
Definition std_expr.h:440
Operator to return the address of an object.
Represents real numbers as roots (zeros) of a polynomial with rational coefficients.
constant_exprt as_expr() const
Thrown when an unexpected error occurs during the analysis (e.g., when the SAT solver returns an erro...
Pointer-typed bitvector constant annotated with the pointer expression that the bitvector is the nume...
Array constructor from list of elements.
Definition std_expr.h:1570
Arrays with given size.
Definition std_types.h:806
typet index_type() const
The type of the index expressions into any instance of this type.
Definition std_types.cpp:34
const exprt & size() const
Definition std_types.h:839
const typet & element_type() const
The type of the elements of the array.
Definition std_types.h:826
A base class for binary expressions.
Definition std_expr.h:649
exprt & lhs()
Definition std_expr.h:679
exprt & rhs()
Definition std_expr.h:689
exprt & op0()
Definition expr.h:134
exprt & op1()
Definition expr.h:137
A base class for relations, i.e., binary predicates whose two operands have the same type.
Definition std_expr.h:784
exprt & where()
Definition std_expr.h:3201
variablest & variables()
Definition std_expr.h:3191
Bit-wise negation of bit-vectors.
std::size_t get_width() const
The Boolean type.
Definition std_types.h:35
The byte swap expression.
std::size_t get_bits_per_byte() const
std::vector< parametert > parameterst
Definition std_types.h:585
const parameterst & parameters() const
Definition std_types.h:698
A constant literal expression.
Definition std_expr.h:3007
const irep_idt & get_value() const
Definition std_expr.h:3015
bool is_null_pointer() const
Returns true if expr has a pointer type and a value NULL; it also returns true when expr has value ze...
Definition std_expr.cpp:170
resultt
Result of running the decision procedure.
Division.
Definition std_expr.h:1152
bool empty() const
Definition dstring.h:101
Equality.
Definition std_expr.h:1339
Boute's Euclidean definition of Modulo – to match SMT-LIB2.
Definition std_expr.h:1277
exprt & op0()
Definition expr.h:134
Base class for all expressions.
Definition expr.h:57
std::vector< exprt > operandst
Definition expr.h:59
bool has_operands() const
Return true if there is at least one operand.
Definition expr.h:92
depth_iteratort depth_end()
Definition expr.cpp:170
bool is_boolean() const
Return whether the expression represents a Boolean.
Definition expr.h:229
depth_iteratort depth_begin()
Definition expr.cpp:168
exprt & op0()
Definition expr.h:134
exprt & op1()
Definition expr.h:137
bool is_constant() const
Return whether the expression is a constant.
Definition expr.h:213
typet & type()
Return the type of the expression.
Definition expr.h:85
void visit_post(std::function< void(exprt &)>)
These are post-order traversal visitors, i.e., the visitor is executed on a node after its children h...
Definition expr.cpp:119
operandst & operands()
Definition expr.h:95
Extracts a single bit of a bit-vector operand.
Extracts a sub-range of a bit-vector operand.
The Boolean constant false.
Definition std_expr.h:3135
std::size_t integer_bits
Definition fixedbv.h:22
std::size_t width
Definition fixedbv.h:22
std::size_t get_fraction_bits() const
Definition fixedbv.h:35
Fixed-width bit-vector with signed fixed-point interpretation.
std::size_t get_fraction_bits() const
std::size_t get_integer_bits() const
Fused multiply-add expression: round(op0 * op1 + op2) with a single rounding.
exprt & op_multiply_lhs()
exprt & op_multiply_rhs()
exprt & rounding_mode()
Round a floating-point number to an integral value considering the given rounding mode.
Semantic type conversion from/to floating-point formats.
Fixed-width bit-vector with IEEE floating-point interpretation.
std::size_t get_f() const
std::size_t get_e() const
IEEE floating-point operations These have two data operands (op0 and op1) and one rounding mode (op2)...
std::size_t f
Definition ieee_float.h:26
std::size_t width() const
Definition ieee_float.h:50
An IEEE 754 floating-point value, including specificiation.
Definition ieee_float.h:117
static ieee_float_valuet minus_infinity(const ieee_float_spect &_spec)
Definition ieee_float.h:209
bool is_NaN() const
Definition ieee_float.h:259
static ieee_float_valuet one(const floatbv_typet &)
ieee_float_spect spec
Definition ieee_float.h:119
constant_exprt to_expr() const
mp_integer pack() const
bool get_sign() const
Definition ieee_float.h:254
static ieee_float_valuet zero(const floatbv_typet &type)
Definition ieee_float.h:172
static ieee_float_valuet NaN(const ieee_float_spect &_spec)
Definition ieee_float.h:195
static ieee_float_valuet plus_infinity(const ieee_float_spect &_spec)
Definition ieee_float.h:202
bool is_infinity() const
Definition ieee_float.h:260
The trinary if-then-else operator.
Definition std_expr.h:2426
exprt & cond()
Definition std_expr.h:2443
exprt & false_case()
Definition std_expr.h:2463
exprt & true_case()
Definition std_expr.h:2453
Boolean implication.
Definition std_expr.h:2154
Array index operator.
Definition std_expr.h:1431
exprt & index()
Definition std_expr.h:1471
exprt & array()
Definition std_expr.h:1461
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition irep.h:366
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition irep.cpp:482
const irep_idt & get(const irep_idt &name) const
Definition irep.cpp:44
const std::string & id_string() const
Definition irep.h:393
subt & get_sub()
Definition irep.h:450
void swap(irept &irep)
Definition irep.h:436
const irep_idt & id() const
Definition irep.h:390
bool is_nil() const
Definition irep.h:370
Evaluates to true if the operand is finite.
Evaluates to true if the operand is infinite.
Evaluates to true if the operand is NaN.
Evaluates to true if the operand is a normal number.
A let expression.
Definition std_expr.h:3259
binding_exprt::variablest & variables()
convenience accessor for binding().variables()
Definition std_expr.h:3340
operandst & values()
Definition std_expr.h:3329
exprt & where()
convenience accessor for binding().where()
Definition std_expr.h:3352
literalt get_literal() const
bool is_true() const
Definition literal.h:156
bool sign() const
Definition literal.h:88
var_not var_no() const
Definition literal.h:83
bool is_false() const
Definition literal.h:161
Extract member of struct or union.
Definition std_expr.h:2866
const exprt & struct_op() const
Definition std_expr.h:2904
irep_idt get_component_name() const
Definition std_expr.h:2888
Binary minus.
Definition std_expr.h:1065
Modulo defined as lhs-(rhs * truncate(lhs/rhs)).
Definition std_expr.h:1216
Binary multiplication Associativity is not specified.
Definition std_expr.h:1104
exprt & op1()
Definition std_expr.h:942
exprt & op0()
Definition std_expr.h:936
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition namespace.h:91
The NIL expression.
Definition std_expr.h:3144
const irep_idt & get_identifier() const
Definition std_expr.h:323
Boolean negation.
Definition std_expr.h:2388
Disequality.
Definition std_expr.h:1393
Expression for finding the size (in bytes) of the object a pointer points to.
The plus expression Associativity is not specified.
Definition std_expr.h:1006
The pointer type These are both 'bitvector_typet' (they have a width) and 'type_with_subtypet' (they ...
const typet & base_type() const
The type of the data what we point to.
A base class for quantifier expressions.
Unbounded, signed rational numbers.
Boolean reduction: XOR (parity) of all bits in the operand.
Bit-vector replication.
constant_exprt & times()
A base class for shift and rotate operators.
exprt & distance()
Sign of an expression Predicate is true if _op is negative, false otherwise.
Definition std_expr.h:612
const irep_idt & get_identifier() const
Definition smt2_conv.h:219
void convert_relation(const binary_relation_exprt &)
bool use_lambda_for_array
Definition smt2_conv.h:71
void convert_type(const typet &)
void unflatten(wheret, const typet &, unsigned nesting=0)
bool use_array_theory(const exprt &)
void find_symbols(const exprt &expr)
Find and declare symbols used in an expression This function traverses the expression tree and create...
std::size_t number_of_solver_calls
Definition smt2_conv.h:110
void convert_typecast(const typecast_exprt &expr)
void write_footer()
Writes the end of the SMT file to the smt_convt::out stream.
tvt l_get(literalt l) const
std::string benchmark
Definition smt2_conv.h:101
void convert_floatbv_rem(const binary_exprt &expr)
std::unordered_map< irep_idt, irept > current_bindings
Definition smt2_conv.h:200
bool use_FPA_theory
Definition smt2_conv.h:66
resultt dec_solve(const exprt &) override
Implementation of the decision procedure.
std::set< irep_idt > bvfp_set
Definition smt2_conv.h:206
void convert_address_of_rec(const exprt &expr, const pointer_typet &result_type)
void push() override
Unimplemented.
void convert_is_dynamic_object(const unary_exprt &)
void convert_literal(const literalt)
void convert_floatbv_div(const ieee_float_op_exprt &expr)
void convert_string_literal(const std::string &)
std::size_t get_number_of_solver_calls() const override
Return the number of incremental solver calls.
const namespacet & ns
Definition smt2_conv.h:99
void convert_floatbv_mult(const ieee_float_op_exprt &expr)
boolbv_widtht boolbv_width
Definition smt2_conv.h:108
void convert_constant(const constant_exprt &expr)
std::string floatbv_suffix(const exprt &) const
void flatten2bv(const exprt &)
std::string notes
Definition smt2_conv.h:101
void convert_floatbv_fma(const floatbv_fma_exprt &expr)
void convert_div(const div_exprt &expr)
std::ostream & out
Definition smt2_conv.h:100
exprt lower_byte_operators(const exprt &expr)
Lower byte_update and byte_extract operations within expr.
std::string type2id(const typet &) const
bool emit_set_logic
Definition smt2_conv.h:72
void convert_rounding_mode_FPA(const exprt &expr)
Converting a constant or symbolic rounding mode to SMT-LIB.
void convert_floatbv_typecast(const floatbv_typecast_exprt &expr)
struct_exprt parse_struct(const irept &s, const struct_typet &type)
std::string logic
Definition smt2_conv.h:101
void convert_mult(const mult_exprt &expr)
void convert_update_bit(const update_bit_exprt &)
exprt prepare_for_convert_expr(const exprt &expr)
Perform steps necessary before an expression is passed to convert_expr.
exprt get(const exprt &expr) const override
Return expr with variables replaced by values from satisfying assignment if available.
std::string decision_procedure_text() const override
Return a textual description of the decision procedure.
void convert_floatbv_minus(const ieee_float_op_exprt &expr)
bool use_check_sat_assuming
Definition smt2_conv.h:69
std::map< object_size_exprt, irep_idt > object_sizes
Definition smt2_conv.h:287
void define_object_size(const irep_idt &id, const object_size_exprt &expr)
bool use_datatypes
Definition smt2_conv.h:70
datatype_mapt datatype_map
Definition smt2_conv.h:272
void convert_mod(const mod_exprt &expr)
static std::string convert_identifier(const irep_idt &identifier)
void convert_floatbv_plus(const ieee_float_op_exprt &expr)
void convert_struct(const struct_exprt &expr)
std::unordered_map< irep_idt, bool > set_values
The values which boolean identifiers have been smt2_convt::set_to or in other words those which are a...
Definition smt2_conv.h:285
smt2_convt(const namespacet &_ns, const std::string &_benchmark, const std::string &_notes, const std::string &_logic, solvert _solver, std::ostream &_out)
Definition smt2_conv.cpp:62
void convert_member(const member_exprt &expr)
void convert_euclidean_mod(const euclidean_mod_exprt &expr)
void convert_index(const index_exprt &expr)
converterst converters
Definition smt2_conv.h:105
pointer_logict pointer_logic
Definition smt2_conv.h:240
exprt handle(const exprt &expr) override
Generate a handle, which is an expression that has the same value as the argument in any model that i...
void print_assignment(std::ostream &out) const override
Print satisfying assignment to out.
void walk_array_tree(std::unordered_map< int64_t, exprt > *operands_map, const irept &src, const array_typet &type)
This function walks the SMT output and populates a map with index/value pairs for the array.
void convert_floatbv_round_to_integral(const floatbv_round_to_integral_exprt &)
void set_to(const exprt &expr, bool value) override
For a Boolean expression expr, add the constraint 'expr' if value is true, otherwise add 'not expr'.
bool use_as_const
Definition smt2_conv.h:68
exprt parse_rec(const irept &s, const typet &type)
void convert_union(const union_exprt &expr)
exprt parse_union(const irept &s, const union_typet &type)
exprt parse_array(const irept &s, const array_typet &type)
This function is for parsing array output from SMT solvers when "(get-value |???|)" returns an array ...
std::vector< bool > boolean_assignment
Definition smt2_conv.h:294
void flatten_array(const exprt &)
produce a flat bit-vector for a given array of fixed size
void convert_with(const with_exprt &expr)
letifyt letify
Definition smt2_conv.h:177
bool use_array_of_bool
Definition smt2_conv.h:67
std::vector< literalt > assumptions
Definition smt2_conv.h:107
void convert_plus(const plus_exprt &expr)
defined_expressionst defined_expressions
Definition smt2_conv.h:281
void pop() override
Currently, only implements a single stack element (no nested contexts).
void convert_update_bits(const update_bits_exprt &)
void find_symbols_rec(const typet &type, std::set< irep_idt > &recstack)
void convert_update(const update_exprt &)
void write_header()
std::set< irep_idt > state_fkt_declared
Definition smt2_conv.h:210
solvert solver
Definition smt2_conv.h:102
identifier_mapt identifier_map
Definition smt2_conv.h:265
void convert_minus(const minus_exprt &expr)
void convert_expr(const exprt &)
constant_exprt parse_literal(const irept &, const typet &type)
const smt2_symbolt & to_smt2_symbol(const exprt &expr)
Definition smt2_conv.h:225
std::size_t no_boolean_variables
Definition smt2_conv.h:293
smt2_identifierst smt2_identifiers
Definition smt2_conv.h:290
void convert_floatbv(const exprt &expr)
literalt convert(const exprt &expr)
array_exprt to_array_expr() const
convert string into array constant
Struct constructor from list of elements.
Definition std_expr.h:1820
Structure type, corresponds to C style structs.
Definition std_types.h:230
const irep_idt & get_name() const
Definition std_types.h:78
const componentst & components() const
Definition std_types.h:146
const componentt & get_component(const irep_idt &component_name) const
Get the reference to a component with given name.
Definition std_types.cpp:64
bool has_component(const irep_idt &component_name) const
Definition std_types.h:156
std::vector< componentt > componentst
Definition std_types.h:139
const irep_idt & get_identifier() const
Definition std_expr.h:166
void identifier(const irep_idt &identifier)
Definition std_expr.h:160
The Boolean constant true.
Definition std_expr.h:3126
Definition threeval.h:20
Semantic type conversion.
Definition std_expr.h:1995
static exprt conditional_cast(const exprt &expr, const typet &type)
Definition std_expr.h:2003
The type of an expression, extends irept.
Definition type.h:29
Generic base class for unary expressions.
Definition std_expr.h:364
const exprt & op() const
Definition std_expr.h:394
The unary minus expression.
Definition std_expr.h:477
Union constructor from single element.
Definition std_expr.h:1724
The union type.
Definition c_types.h:147
Fixed-width bit-vector with unsigned binary interpretation.
Thrown when we encounter an instruction, parameters to an instruction etc.
Replaces a sub-range of a bit-vector operand.
exprt lower() const
A lowering to masking, shifting, or.
Replaces a sub-range of a bit-vector operand.
exprt lower() const
A lowering to masking, shifting, or.
Operator to update elements in structs and arrays.
Definition std_expr.h:2679
Operator to update elements in structs and arrays.
Definition std_expr.h:2520
exprt & new_value()
Definition std_expr.h:2550
exprt & where()
Definition std_expr.h:2540
exprt & old()
Definition std_expr.h:2530
bool has_prefix(const std::string &s, const std::string &prefix)
Definition converter.cpp:13
int isdigit(int c)
Definition ctype.c:24
bool can_cast_expr(const exprt &base)
Check whether a reference to a generic exprt is of a specific derived class.
auto expr_try_dynamic_cast(TExpr &base) -> typename detail::expr_try_dynamic_cast_return_typet< T, TExpr >::type
Try to cast a reference to a generic exprt to a specific derived class.
Definition expr_cast.h:81
Forward depth-first search iterators These iterators' copy operations are expensive,...
exprt make_binary(const exprt &expr)
splits an expression with >=3 operands into nested binary expressions
Definition expr_util.cpp:38
Deprecated expression utility functions.
bool has_symbol_expr(const exprt &src, const irep_idt &identifier, bool include_bound_symbols)
Returns true if one of the symbol expressions in src has identifier identifier; if include_bound_symb...
exprt float_bv(const exprt &src)
Definition float_bv.h:202
API to expression classes for floating-point arithmetic.
const ieee_float_op_exprt & to_ieee_float_op_expr(const exprt &expr)
Cast an exprt to an ieee_float_op_exprt.
const floatbv_fma_exprt & to_floatbv_fma_expr(const exprt &expr)
const floatbv_round_to_integral_exprt & to_floatbv_round_to_integral_expr(const exprt &expr)
Cast an exprt to a floatbv_round_to_integral_exprt.
const isnormal_exprt & to_isnormal_expr(const exprt &expr)
Cast an exprt to a isnormal_exprt.
const isinf_exprt & to_isinf_expr(const exprt &expr)
Cast an exprt to a isinf_exprt.
const isfinite_exprt & to_isfinite_expr(const exprt &expr)
Cast an exprt to a isfinite_exprt.
const isnan_exprt & to_isnan_expr(const exprt &expr)
Cast an exprt to a isnan_exprt.
const floatbv_typecast_exprt & to_floatbv_typecast_expr(const exprt &expr)
Cast an exprt to a floatbv_typecast_exprt.
static format_containert< T > format(const T &o)
Definition format.h:37
const std::string & id2string(const irep_idt &d)
Definition irep.h:44
static std::string binary(const constant_exprt &src)
exprt to_expr(const namespacet &ns, const irep_idt &identifier, const std::string &src)
bool is_true(const literalt &l)
Definition literal.h:198
literalt const_literal(bool value)
Definition literal.h:188
literalt pos(literalt a)
Definition literal.h:194
const literal_exprt & to_literal_expr(const exprt &expr)
Cast a generic exprt to a literal_exprt.
double pow(double x, double y)
Definition math.c:3049
API to expression classes for 'mathematical' expressions.
const quantifier_exprt & to_quantifier_expr(const exprt &expr)
Cast an exprt to a quantifier_exprt.
const function_application_exprt & to_function_application_expr(const exprt &expr)
Cast an exprt to a function_application_exprt.
const integer_range_typet & to_integer_range_type(const typet &type)
Cast a typet to a integer_range_typet.
const mathematical_function_typet & to_mathematical_function_type(const typet &type)
Cast a typet to a mathematical_function_typet.
const mp_integer string2integer(const std::string &n, unsigned base)
Definition mp_arith.cpp:54
const std::string integer2string(const mp_integer &n, unsigned base)
Definition mp_arith.cpp:103
mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
bitwise 'or' of two nonnegative integers
Definition mp_arith.cpp:215
const std::string integer2binary(const mp_integer &n, std::size_t width)
Definition mp_arith.cpp:64
named_mp_integert< bits_tagt > bitst
Definition mp_arith.h:179
named_mp_integert< bytes_tagt > bytest
Definition mp_arith.h:184
const element_address_exprt & to_element_address_expr(const exprt &expr)
Cast an exprt to an element_address_exprt.
const object_address_exprt & to_object_address_expr(const exprt &expr)
Cast an exprt to an object_address_exprt.
const address_of_exprt & to_address_of_expr(const exprt &expr)
Cast an exprt to an address_of_exprt.
const pointer_typet & to_pointer_type(const typet &type)
Cast a typet to a pointer_typet.
const pointer_offset_exprt & to_pointer_offset_expr(const exprt &expr)
Cast an exprt to a pointer_offset_exprt.
const pointer_object_exprt & to_pointer_object_expr(const exprt &expr)
Cast an exprt to a pointer_object_exprt.
const field_address_exprt & to_field_address_expr(const exprt &expr)
Cast an exprt to an field_address_exprt.
bool is_zero_width(const typet &type, const namespacet &ns)
Returns true iff type has effective width of zero bits.
std::optional< exprt > size_of_expr(const typet &type, const namespacet &ns)
std::optional< bytest > member_offset(const struct_typet &type, const irep_idt &member, const namespacet &ns)
std::optional< bytest > pointer_offset_size(const typet &type, const namespacet &ns)
Compute the size of a type in bytes, rounding up to full bytes.
Pointer Logic.
exprt pointer_offset(const exprt &pointer)
exprt object_size(const exprt &pointer)
exprt same_object(const exprt &p1, const exprt &p2)
Various predicates over pointers in programs.
Ranges: pair of begin and end iterators, which can be initialized from containers,...
ranget< iteratort > make_range(iteratort begin, iteratort end)
Definition range.h:522
bool to_rational(const exprt &expr, rationalt &rational_value)
constant_exprt from_rational(const rationalt &a)
exprt simplify_expr(exprt src, const namespacet &ns)
static bool has_quantifier(const exprt &expr)
#define SMT2_TODO(S)
Definition smt2_conv.cpp:60
static bool is_smt2_simple_identifier(const std::string &identifier)
#define UNEXPECTEDCASE(S)
Definition smt2_conv.cpp:57
bool is_smt2_simple_symbol_character(char ch)
Tokenizer for the SMT-LIB v2.6 syntax.
BigInt mp_integer
Definition smt_terms.h:17
#define CHECK_RETURN(CONDITION)
Definition invariant.h:495
#define UNREACHABLE
This should be used to mark dead code.
Definition invariant.h:525
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
Definition invariant.h:534
#define PRECONDITION(CONDITION)
Definition invariant.h:463
#define UNIMPLEMENTED
Definition invariant.h:558
#define INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Same as invariant, with one or more diagnostics attached Diagnostics can be of any type that has a sp...
Definition invariant.h:437
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
Definition invariant.h:423
#define CHECK_RETURN_WITH_DIAGNOSTICS(CONDITION,...)
Definition invariant.h:496
#define DATA_INVARIANT_WITH_DIAGNOSTICS(CONDITION, REASON,...)
Definition invariant.h:535
#define UNREACHABLE_BECAUSE(REASON)
Definition invariant.h:526
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition std_expr.cpp:291
API to expression classes.
const struct_exprt & to_struct_expr(const exprt &expr)
Cast an exprt to a struct_exprt.
Definition std_expr.h:1843
const array_of_exprt & to_array_of_expr(const exprt &expr)
Cast an exprt to an array_of_exprt.
Definition std_expr.h:1552
const binary_relation_exprt & to_binary_relation_expr(const exprt &expr)
Cast an exprt to a binary_relation_exprt.
Definition std_expr.h:828
const unary_plus_exprt & to_unary_plus_expr(const exprt &expr)
Cast an exprt to a unary_plus_exprt.
Definition std_expr.h:539
const index_exprt & to_index_expr(const exprt &expr)
Cast an exprt to an index_exprt.
Definition std_expr.h:1494
const mod_exprt & to_mod_expr(const exprt &expr)
Cast an exprt to a mod_exprt.
Definition std_expr.h:1260
const mult_exprt & to_mult_expr(const exprt &expr)
Cast an exprt to a mult_exprt.
Definition std_expr.h:1134
const array_comprehension_exprt & to_array_comprehension_expr(const exprt &expr)
Cast an exprt to a array_comprehension_exprt.
Definition std_expr.h:3529
const ternary_exprt & to_ternary_expr(const exprt &expr)
Cast an exprt to a ternary_exprt.
Definition std_expr.h:117
const named_term_exprt & to_named_term_expr(const exprt &expr)
Cast an exprt to a named_term_exprt.
Definition std_expr.h:3702
const cond_exprt & to_cond_expr(const exprt &expr)
Cast an exprt to a cond_exprt.
Definition std_expr.h:3443
const typecast_exprt & to_typecast_expr(const exprt &expr)
Cast an exprt to a typecast_exprt.
Definition std_expr.h:2024
const div_exprt & to_div_expr(const exprt &expr)
Cast an exprt to a div_exprt.
Definition std_expr.h:1196
const binary_exprt & to_binary_expr(const exprt &expr)
Cast an exprt to a binary_exprt.
Definition std_expr.h:721
const plus_exprt & to_plus_expr(const exprt &expr)
Cast an exprt to a plus_exprt.
Definition std_expr.h:1045
const notequal_exprt & to_notequal_expr(const exprt &expr)
Cast an exprt to an notequal_exprt.
Definition std_expr.h:1413
const unary_exprt & to_unary_expr(const exprt &expr)
Cast an exprt to a unary_exprt.
Definition std_expr.h:424
const multi_ary_exprt & to_multi_ary_expr(const exprt &expr)
Cast an exprt to a multi_ary_exprt.
Definition std_expr.h:991
const let_exprt & to_let_expr(const exprt &expr)
Cast an exprt to a let_exprt.
Definition std_expr.h:3383
const abs_exprt & to_abs_expr(const exprt &expr)
Cast an exprt to a abs_exprt.
Definition std_expr.h:459
const if_exprt & to_if_expr(const exprt &expr)
Cast an exprt to an if_exprt.
Definition std_expr.h:2501
const member_exprt & to_member_expr(const exprt &expr)
Cast an exprt to a member_exprt.
Definition std_expr.h:2953
const minus_exprt & to_minus_expr(const exprt &expr)
Cast an exprt to a minus_exprt.
Definition std_expr.h:1085
const union_exprt & to_union_expr(const exprt &expr)
Cast an exprt to a union_exprt.
Definition std_expr.h:1765
const constant_exprt & to_constant_expr(const exprt &expr)
Cast an exprt to a constant_exprt.
Definition std_expr.h:3078
const not_exprt & to_not_expr(const exprt &expr)
Cast an exprt to an not_exprt.
Definition std_expr.h:2408
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
Definition std_expr.h:221
const with_exprt & to_with_expr(const exprt &expr)
Cast an exprt to a with_exprt.
Definition std_expr.h:2573
const implies_exprt & to_implies_expr(const exprt &expr)
Cast an exprt to a implies_exprt.
Definition std_expr.h:2174
const update_exprt & to_update_expr(const exprt &expr)
Cast an exprt to an update_exprt.
Definition std_expr.h:2762
const unary_minus_exprt & to_unary_minus_expr(const exprt &expr)
Cast an exprt to a unary_minus_exprt.
Definition std_expr.h:502
const equal_exprt & to_equal_expr(const exprt &expr)
Cast an exprt to an equal_exprt.
Definition std_expr.h:1375
const nondet_symbol_exprt & to_nondet_symbol_expr(const exprt &expr)
Cast an exprt to a nondet_symbol_exprt.
Definition std_expr.h:346
const sign_exprt & to_sign_expr(const exprt &expr)
Cast an exprt to a sign_exprt.
Definition std_expr.h:632
const euclidean_mod_exprt & to_euclidean_mod_expr(const exprt &expr)
Cast an exprt to a euclidean_mod_exprt.
Definition std_expr.h:1321
const code_typet & to_code_type(const typet &type)
Cast a typet to a code_typet.
Definition std_types.h:787
const struct_typet & to_struct_type(const typet &type)
Cast a typet to a struct_typet.
Definition std_types.h:307
const struct_tag_typet & to_struct_tag_type(const typet &type)
Cast a typet to a struct_tag_typet.
Definition std_types.h:517
const complex_typet & to_complex_type(const typet &type)
Cast a typet to a complex_typet.
Definition std_types.h:1048
const array_typet & to_array_type(const typet &type)
Cast a typet to an array_typet.
Definition std_types.h:887
std::size_t unsafe_string2size_t(const std::string &str, int base)
const string_constantt & to_string_constant(const exprt &expr)
static bool failed(bool error_indicator)
#define size_type
Definition unistd.c:186
dstringt irep_idt