-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathsmt2_parser.cpp
1756 lines (1389 loc) · 50.9 KB
/
smt2_parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module:
Author: Daniel Kroening, kroening@kroening.com
\*******************************************************************/
#include "smt2_parser.h"
#include "smt2_format.h"
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/bitvector_types.h>
#include <util/floatbv_expr.h>
#include <util/ieee_float.h>
#include <util/invariant.h>
#include <util/mathematical_expr.h>
#include <util/prefix.h>
#include <util/range.h>
#include <numeric>
smt2_tokenizert::tokent smt2_parsert::next_token()
{
const auto token = smt2_tokenizer.next_token();
if(token == smt2_tokenizert::OPEN)
parenthesis_level++;
else if(token == smt2_tokenizert::CLOSE)
parenthesis_level--;
return token;
}
void smt2_parsert::skip_to_end_of_list()
{
while(parenthesis_level > 0)
if(next_token() == smt2_tokenizert::END_OF_FILE)
return;
}
void smt2_parsert::command_sequence()
{
exit=false;
while(!exit)
{
if(smt2_tokenizer.peek() == smt2_tokenizert::END_OF_FILE)
{
exit = true;
return;
}
if(next_token() != smt2_tokenizert::OPEN)
throw error("command must start with '('");
if(next_token() != smt2_tokenizert::SYMBOL)
{
ignore_command();
throw error("expected symbol as command");
}
command(smt2_tokenizer.get_buffer());
switch(next_token())
{
case smt2_tokenizert::END_OF_FILE:
throw error(
"expected closing parenthesis at end of command,"
" but got EOF");
case smt2_tokenizert::CLOSE:
// what we expect
break;
case smt2_tokenizert::OPEN:
case smt2_tokenizert::SYMBOL:
case smt2_tokenizert::NUMERAL:
case smt2_tokenizert::STRING_LITERAL:
case smt2_tokenizert::NONE:
case smt2_tokenizert::KEYWORD:
throw error("expected ')' at end of command");
}
}
}
void smt2_parsert::ignore_command()
{
std::size_t parentheses=0;
while(true)
{
switch(smt2_tokenizer.peek())
{
case smt2_tokenizert::OPEN:
next_token();
parentheses++;
break;
case smt2_tokenizert::CLOSE:
if(parentheses==0)
return; // done
next_token();
parentheses--;
break;
case smt2_tokenizert::END_OF_FILE:
throw error("unexpected EOF in command");
case smt2_tokenizert::SYMBOL:
case smt2_tokenizert::NUMERAL:
case smt2_tokenizert::STRING_LITERAL:
case smt2_tokenizert::NONE:
case smt2_tokenizert::KEYWORD:
next_token();
}
}
}
exprt::operandst smt2_parsert::operands()
{
exprt::operandst result;
while(smt2_tokenizer.peek() != smt2_tokenizert::CLOSE)
result.push_back(expression());
next_token(); // eat the ')'
return result;
}
void smt2_parsert::add_unique_id(irep_idt id, exprt expr)
{
if(!id_map
.emplace(
std::piecewise_construct,
std::forward_as_tuple(id),
std::forward_as_tuple(idt::VARIABLE, std::move(expr)))
.second)
{
// id already used
throw error() << "identifier '" << id << "' defined twice";
}
}
exprt smt2_parsert::let_expression()
{
if(next_token() != smt2_tokenizert::OPEN)
throw error("expected bindings after let");
std::vector<std::pair<irep_idt, exprt>> bindings;
while(smt2_tokenizer.peek() == smt2_tokenizert::OPEN)
{
next_token();
if(next_token() != smt2_tokenizert::SYMBOL)
throw error("expected symbol in binding");
irep_idt identifier = smt2_tokenizer.get_buffer();
// note that the previous bindings are _not_ visible yet
exprt value=expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after value in binding");
bindings.push_back(
std::pair<irep_idt, exprt>(identifier, value));
}
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' at end of bindings");
// we may hide identifiers in outer scopes
std::vector<std::pair<irep_idt, idt>> saved_ids;
// add the bindings to the id_map
for(auto &b : bindings)
{
auto insert_result = id_map.insert({b.first, idt{idt::BINDING, b.second}});
if(!insert_result.second) // already there
{
auto &id_entry = *insert_result.first;
saved_ids.emplace_back(id_entry.first, std::move(id_entry.second));
id_entry.second = idt{idt::BINDING, b.second};
}
}
// now parse, with bindings in place
exprt where = expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after let");
binding_exprt::variablest variables;
exprt::operandst values;
for(const auto &b : bindings)
{
variables.push_back(symbol_exprt(b.first, b.second.type()));
values.push_back(b.second);
}
// delete the bindings from the id_map
for(const auto &binding : bindings)
id_map.erase(binding.first);
// restore any previous ids
for(auto &saved_id : saved_ids)
id_map.insert(std::move(saved_id));
return let_exprt(variables, values, where);
}
std::pair<binding_exprt::variablest, exprt> smt2_parsert::binding(irep_idt id)
{
if(next_token() != smt2_tokenizert::OPEN)
throw error() << "expected bindings after " << id;
binding_exprt::variablest bindings;
while(smt2_tokenizer.peek() == smt2_tokenizert::OPEN)
{
next_token();
if(next_token() != smt2_tokenizert::SYMBOL)
throw error("expected symbol in binding");
irep_idt identifier = smt2_tokenizer.get_buffer();
typet type=sort();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after sort in binding");
bindings.push_back(symbol_exprt(identifier, type));
}
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' at end of bindings");
// we may hide identifiers in outer scopes
std::vector<std::pair<irep_idt, idt>> saved_ids;
// add the bindings to the id_map
for(auto &b : bindings)
{
auto insert_result =
id_map.insert({b.get_identifier(), idt{idt::BINDING, b.type()}});
if(!insert_result.second) // already there
{
auto &id_entry = *insert_result.first;
saved_ids.emplace_back(id_entry.first, std::move(id_entry.second));
id_entry.second = idt{idt::BINDING, b.type()};
}
}
// now parse, with bindings in place
exprt expr=expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error() << "expected ')' after " << id;
// remove bindings from id_map
for(const auto &b : bindings)
id_map.erase(b.get_identifier());
// restore any previous ids
for(auto &saved_id : saved_ids)
id_map.insert(std::move(saved_id));
return {std::move(bindings), std::move(expr)};
}
exprt smt2_parsert::lambda_expression()
{
auto binding = this->binding(ID_lambda);
return lambda_exprt(binding.first, binding.second);
}
exprt smt2_parsert::quantifier_expression(irep_idt id)
{
auto binding = this->binding(id);
if(!binding.second.is_boolean())
throw error() << id << " expects a boolean term";
return quantifier_exprt(id, binding.first, binding.second);
}
exprt smt2_parsert::function_application(
const symbol_exprt &function,
const exprt::operandst &op)
{
const auto &function_type = to_mathematical_function_type(function.type());
// check the arguments
if(op.size() != function_type.domain().size())
throw error("wrong number of arguments for function");
for(std::size_t i=0; i<op.size(); i++)
{
if(op[i].type() != function_type.domain()[i])
throw error("wrong type for arguments for function");
}
return function_application_exprt(function, op);
}
exprt::operandst smt2_parsert::cast_bv_to_signed(const exprt::operandst &op)
{
exprt::operandst result = op;
for(auto &expr : result)
{
if(expr.type().id() == ID_signedbv) // no need to cast
{
}
else if(expr.type().id() != ID_unsignedbv)
{
throw error("expected unsigned bitvector");
}
else
{
const auto width = to_unsignedbv_type(expr.type()).get_width();
expr = typecast_exprt(expr, signedbv_typet(width));
}
}
return result;
}
exprt smt2_parsert::cast_bv_to_unsigned(const exprt &expr)
{
if(expr.type().id()==ID_unsignedbv) // no need to cast
return expr;
if(expr.type().id()!=ID_signedbv)
throw error("expected signed bitvector");
const auto width=to_signedbv_type(expr.type()).get_width();
return typecast_exprt(expr, unsignedbv_typet(width));
}
void smt2_parsert::check_matching_operand_types(
const exprt::operandst &op) const
{
for(std::size_t i = 1; i < op.size(); i++)
{
if(op[i].type() != op[0].type())
{
throw error() << "expression must have operands with matching types,"
" but got '"
<< smt2_format(op[0].type()) << "' and '"
<< smt2_format(op[i].type()) << '\'';
}
}
}
exprt smt2_parsert::multi_ary(irep_idt id, const exprt::operandst &op)
{
if(op.empty())
throw error("expression must have at least one operand");
check_matching_operand_types(op);
exprt result(id, op[0].type());
result.operands() = op;
return result;
}
exprt smt2_parsert::binary_predicate(irep_idt id, const exprt::operandst &op)
{
if(op.size()!=2)
throw error("expression must have two operands");
check_matching_operand_types(op);
return binary_predicate_exprt(op[0], id, op[1]);
}
exprt smt2_parsert::unary(irep_idt id, const exprt::operandst &op)
{
if(op.size()!=1)
throw error("expression must have one operand");
return unary_exprt(id, op[0], op[0].type());
}
exprt smt2_parsert::binary(irep_idt id, const exprt::operandst &op)
{
if(op.size()!=2)
throw error("expression must have two operands");
check_matching_operand_types(op);
return binary_exprt(op[0], id, op[1], op[0].type());
}
exprt smt2_parsert::function_application_ieee_float_eq(
const exprt::operandst &op)
{
if(op.size() != 2)
throw error() << "FloatingPoint equality takes two operands";
if(op[0].type().id() != ID_floatbv || op[1].type().id() != ID_floatbv)
throw error() << "FloatingPoint equality takes FloatingPoint operands";
if(op[0].type() != op[1].type())
{
throw error() << "FloatingPoint equality takes FloatingPoint operands with "
<< "matching sort, but got " << smt2_format(op[0].type())
<< " vs " << smt2_format(op[1].type());
}
return ieee_float_equal_exprt(op[0], op[1]);
}
exprt smt2_parsert::function_application_ieee_float_op(
const irep_idt &id,
const exprt::operandst &op)
{
if(op.size() != 3)
throw error() << id << " takes three operands";
if(op[1].type().id() != ID_floatbv || op[2].type().id() != ID_floatbv)
throw error() << id << " takes FloatingPoint operands";
if(op[1].type() != op[2].type())
{
throw error() << id << " takes FloatingPoint operands with matching sort, "
<< "but got " << smt2_format(op[1].type()) << " vs "
<< smt2_format(op[2].type());
}
// clang-format off
const irep_idt expr_id =
id == "fp.add" ? ID_floatbv_plus :
id == "fp.sub" ? ID_floatbv_minus :
id == "fp.mul" ? ID_floatbv_mult :
id == "fp.div" ? ID_floatbv_div :
throw error("unsupported floating-point operation");
// clang-format on
return ieee_float_op_exprt(op[1], expr_id, op[2], op[0]);
}
exprt smt2_parsert::function_application_fp(const exprt::operandst &op)
{
// floating-point from bit-vectors
if(op.size() != 3)
throw error("fp takes three operands");
if(op[0].type().id() != ID_unsignedbv)
throw error("fp takes BitVec as first operand");
if(op[1].type().id() != ID_unsignedbv)
throw error("fp takes BitVec as second operand");
if(op[2].type().id() != ID_unsignedbv)
throw error("fp takes BitVec as third operand");
if(to_unsignedbv_type(op[0].type()).get_width() != 1)
throw error("fp takes BitVec of size 1 as first operand");
const auto width_e = to_unsignedbv_type(op[1].type()).get_width();
const auto width_f = to_unsignedbv_type(op[2].type()).get_width();
// stitch the bits together
const auto concat_type = unsignedbv_typet(width_f + width_e + 1);
// We need a bitvector type without numerical interpretation
// to do this conversion.
const auto bv_type = bv_typet(concat_type.get_width());
// The target type
const auto fp_type = ieee_float_spect(width_f, width_e).to_type();
return typecast_exprt(
typecast_exprt(
concatenation_exprt(exprt::operandst(op), concat_type), bv_type),
fp_type);
}
exprt smt2_parsert::function_application()
{
switch(next_token())
{
case smt2_tokenizert::SYMBOL:
if(smt2_tokenizer.get_buffer() == "_") // indexed identifier
{
// indexed identifier
if(next_token() != smt2_tokenizert::SYMBOL)
throw error("expected symbol after '_'");
// copy, the reference won't be stable
const auto id = smt2_tokenizer.get_buffer();
if(has_prefix(id, "bv"))
{
mp_integer i = string2integer(
std::string(smt2_tokenizer.get_buffer(), 2, std::string::npos));
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected numeral as bitvector literal width");
auto width = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after bitvector literal");
return from_integer(i, unsignedbv_typet(width));
}
else if(id == "+oo" || id == "-oo" || id == "NaN")
{
// These are the "plus infinity", "minus infinity" and NaN
// floating-point literals.
if(next_token() != smt2_tokenizert::NUMERAL)
throw error() << "expected number after " << id;
auto width_e = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::NUMERAL)
throw error() << "expected second number after " << id;
auto width_f = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error() << "expected ')' after " << id;
// width_f *includes* the hidden bit
const ieee_float_spect spec(width_f - 1, width_e);
if(id == "+oo")
return ieee_floatt::plus_infinity(spec).to_expr();
else if(id == "-oo")
return ieee_floatt::minus_infinity(spec).to_expr();
else // NaN
return ieee_floatt::NaN(spec).to_expr();
}
else
{
throw error() << "unknown indexed identifier " << id;
}
}
else if(smt2_tokenizer.get_buffer() == "!")
{
// these are "term attributes"
const auto term = expression();
while(smt2_tokenizer.peek() == smt2_tokenizert::KEYWORD)
{
next_token(); // eat the keyword
if(smt2_tokenizer.get_buffer() == "named")
{
// 'named terms' must be Boolean
if(!term.is_boolean())
throw error("named terms must be Boolean");
if(next_token() == smt2_tokenizert::SYMBOL)
{
const symbol_exprt symbol_expr(
smt2_tokenizer.get_buffer(), bool_typet());
named_terms.emplace(
symbol_expr.get_identifier(), named_termt(term, symbol_expr));
}
else
throw error("invalid name attribute, expected symbol");
}
else
throw error("unknown term attribute");
}
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' at end of term attribute");
else
return term;
}
else
{
// non-indexed symbol, look up in expression table
const auto id = smt2_tokenizer.get_buffer();
const auto e_it = expressions.find(id);
if(e_it != expressions.end())
return e_it->second();
// get the operands
auto op = operands();
// rummage through id_map
auto id_it = id_map.find(id);
if(id_it != id_map.end())
{
if(id_it->second.type.id() == ID_mathematical_function)
{
return function_application(symbol_exprt(id, id_it->second.type), op);
}
else
return symbol_exprt(id, id_it->second.type);
}
else
throw error() << "unknown function symbol '" << id << '\'';
}
break;
case smt2_tokenizert::OPEN: // likely indexed identifier
if(smt2_tokenizer.peek() == smt2_tokenizert::SYMBOL)
{
next_token(); // eat symbol
if(smt2_tokenizer.get_buffer() == "_")
{
// indexed identifier
if(next_token() != smt2_tokenizert::SYMBOL)
throw error("expected symbol after '_'");
irep_idt id = smt2_tokenizer.get_buffer(); // hash it
if(id=="extract")
{
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected numeral after extract");
auto upper = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected two numerals after extract");
auto lower = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after extract");
auto op=operands();
if(op.size()!=1)
throw error("extract takes one operand");
if(upper<lower)
throw error("extract got bad indices");
auto lower_e = from_integer(lower, integer_typet());
unsignedbv_typet t(upper-lower+1);
return extractbits_exprt(op[0], lower_e, t);
}
else if(id=="rotate_left" ||
id=="rotate_right" ||
id == ID_repeat ||
id=="sign_extend" ||
id=="zero_extend")
{
if(next_token() != smt2_tokenizert::NUMERAL)
throw error() << "expected numeral after " << id;
auto index = string2integer(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error() << "expected ')' after " << id << " index";
auto op=operands();
if(op.size()!=1)
throw error() << id << " takes one operand";
if(id=="rotate_left")
{
auto dist=from_integer(index, integer_typet());
return binary_exprt(op[0], ID_rol, dist, op[0].type());
}
else if(id=="rotate_right")
{
auto dist=from_integer(index, integer_typet());
return binary_exprt(op[0], ID_ror, dist, op[0].type());
}
else if(id=="sign_extend")
{
// we first convert to a signed type of the original width,
// then extend to the new width, and then go to unsigned
const auto width = to_unsignedbv_type(op[0].type()).get_width();
const signedbv_typet small_signed_type{width};
const signedbv_typet large_signed_type{width + index};
const unsignedbv_typet unsigned_type{width + index};
return typecast_exprt(
typecast_exprt(
typecast_exprt(op[0], small_signed_type), large_signed_type),
unsigned_type);
}
else if(id == ID_zero_extend)
{
auto width=to_unsignedbv_type(op[0].type()).get_width();
unsignedbv_typet unsigned_type{width + index};
return zero_extend_exprt{op[0], unsigned_type};
}
else if(id == ID_repeat)
{
auto i = from_integer(index, integer_typet());
auto width = to_unsignedbv_type(op[0].type()).get_width() * index;
return replication_exprt(i, op[0], unsignedbv_typet(width));
}
else
return nil_exprt();
}
else if(id == "to_fp")
{
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected number after to_fp");
auto width_e = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected second number after to_fp");
auto width_f = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after to_fp");
// width_f *includes* the hidden bit
const ieee_float_spect spec(width_f - 1, width_e);
auto rounding_mode = expression();
auto source_op = expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' at the end of to_fp");
// There are several options for the source operand:
// 1) real or integer
// 2) bit-vector, which is interpreted as signed 2's complement
// 3) another floating-point format
if(
source_op.type().id() == ID_real ||
source_op.type().id() == ID_integer)
{
// For now, we can only do this when
// the source operand is a constant.
if(source_op.is_constant())
{
mp_integer significand, exponent;
const auto &real_number =
id2string(to_constant_expr(source_op).get_value());
auto dot_pos = real_number.find('.');
if(dot_pos == std::string::npos)
{
exponent = 0;
significand = string2integer(real_number);
}
else
{
// remove the '.'
std::string significand_str;
significand_str.reserve(real_number.size());
for(auto ch : real_number)
{
if(ch != '.')
significand_str += ch;
}
exponent =
mp_integer(dot_pos) - mp_integer(real_number.size()) + 1;
significand = string2integer(significand_str);
}
ieee_floatt a(
spec,
static_cast<ieee_floatt::rounding_modet>(
numeric_cast_v<int>(to_constant_expr(rounding_mode))));
a.from_base10(significand, exponent);
return a.to_expr();
}
else
throw error()
<< "to_fp for non-constant real expressions is not implemented";
}
else if(source_op.type().id() == ID_unsignedbv)
{
// The operand is hard-wired to be interpreted as a signed number.
return floatbv_typecast_exprt(
typecast_exprt(
source_op,
signedbv_typet(
to_unsignedbv_type(source_op.type()).get_width())),
rounding_mode,
spec.to_type());
}
else if(source_op.type().id() == ID_floatbv)
{
return floatbv_typecast_exprt(
source_op, rounding_mode, spec.to_type());
}
else
throw error() << "unexpected sort given as operand to to_fp";
}
else if(id == "to_fp_unsigned")
{
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected number after to_fp_unsigned");
auto width_e = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::NUMERAL)
throw error("expected second number after to_fp_unsigned");
auto width_f = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' after to_fp_unsigned");
// width_f *includes* the hidden bit
const ieee_float_spect spec(width_f - 1, width_e);
auto rounding_mode = expression();
auto source_op = expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("expected ')' at the end of to_fp_unsigned");
if(source_op.type().id() == ID_unsignedbv)
{
// The operand is hard-wired to be interpreted
// as an unsigned number.
return floatbv_typecast_exprt(
source_op, rounding_mode, spec.to_type());
}
else
throw error()
<< "unexpected sort given as operand to to_fp_unsigned";
}
else if(id == "fp.to_sbv" || id == "fp.to_ubv")
{
// These are indexed by the number of bits of the result.
if(next_token() != smt2_tokenizert::NUMERAL)
throw error() << "expected number after " << id;
auto width = std::stoll(smt2_tokenizer.get_buffer());
if(next_token() != smt2_tokenizert::CLOSE)
throw error() << "expected ')' after " << id;
auto op = operands();
if(op.size() != 2)
throw error() << id << " takes two operands";
if(op[1].type().id() != ID_floatbv)
throw error() << id << " takes a FloatingPoint operand";
if(id == "fp.to_sbv")
return typecast_exprt(
floatbv_typecast_exprt(op[1], op[0], signedbv_typet(width)),
unsignedbv_typet(width));
else
return floatbv_typecast_exprt(
op[1], op[0], unsignedbv_typet(width));
}
else
{
throw error() << "unknown indexed identifier '"
<< smt2_tokenizer.get_buffer() << '\'';
}
}
else if(smt2_tokenizer.get_buffer() == "as")
{
// This is an extension understood by Z3 and CVC4.
if(
smt2_tokenizer.peek() == smt2_tokenizert::SYMBOL &&
smt2_tokenizer.get_buffer() == "const")
{
next_token(); // eat the "const"
auto sort = this->sort();
if(sort.id() != ID_array)
{
throw error()
<< "unexpected 'as const' expression expects array type";
}
const auto &array_sort = to_array_type(sort);
if(smt2_tokenizer.next_token() != smt2_tokenizert::CLOSE)
throw error() << "expecting ')' after sort in 'as const'";
auto value = expression();
if(value.type() != array_sort.element_type())
throw error() << "unexpected 'as const' with wrong element type";
if(smt2_tokenizer.next_token() != smt2_tokenizert::CLOSE)
throw error() << "expecting ')' at the end of 'as const'";
return array_of_exprt(value, array_sort);
}
else
throw error() << "unexpected 'as' expression";
}
else
{
// just double parentheses
exprt tmp=expression();
if(
next_token() != smt2_tokenizert::CLOSE &&
next_token() != smt2_tokenizert::CLOSE)
{
throw error("mismatched parentheses in an expression");
}
return tmp;
}
}
else
{
// just double parentheses
exprt tmp=expression();
if(
next_token() != smt2_tokenizert::CLOSE &&
next_token() != smt2_tokenizert::CLOSE)
{
throw error("mismatched parentheses in an expression");
}
return tmp;
}
break;
case smt2_tokenizert::CLOSE:
case smt2_tokenizert::NUMERAL:
case smt2_tokenizert::STRING_LITERAL:
case smt2_tokenizert::END_OF_FILE:
case smt2_tokenizert::NONE:
case smt2_tokenizert::KEYWORD:
// just parentheses
exprt tmp=expression();
if(next_token() != smt2_tokenizert::CLOSE)
throw error("mismatched parentheses in an expression");
return tmp;
}
UNREACHABLE;
}
exprt smt2_parsert::bv_division(
const exprt::operandst &operands,
bool is_signed)
{
if(operands.size() != 2)
throw error() << "bitvector division expects two operands";
// SMT-LIB2 defines the result of division by 0 to be 1....1
auto divisor = symbol_exprt("divisor", operands[1].type());
auto divisor_is_zero = equal_exprt(divisor, from_integer(0, divisor.type()));
auto all_ones = to_unsignedbv_type(operands[0].type()).largest_expr();
exprt division_result;
if(is_signed)
{
auto signed_operands = cast_bv_to_signed({operands[0], divisor});
division_result =
cast_bv_to_unsigned(div_exprt(signed_operands[0], signed_operands[1]));
}
else
division_result = div_exprt(operands[0], divisor);
return let_exprt(
{divisor},
operands[1],
if_exprt(divisor_is_zero, all_ones, division_result));
}
exprt smt2_parsert::bv_mod(const exprt::operandst &operands, bool is_signed)
{
if(operands.size() != 2)
throw error() << "bitvector modulo expects two operands";
// SMT-LIB2 defines the result of "lhs modulo 0" to be "lhs"
auto dividend = symbol_exprt("dividend", operands[0].type());
auto divisor = symbol_exprt("divisor", operands[1].type());
auto divisor_is_zero = equal_exprt(divisor, from_integer(0, divisor.type()));
exprt mod_result;
// bvurem and bvsrem match our mod_exprt.
// bvsmod doesn't.
if(is_signed)
{
auto signed_operands = cast_bv_to_signed({dividend, divisor});
mod_result =
cast_bv_to_unsigned(mod_exprt(signed_operands[0], signed_operands[1]));