forked from z3str/Z3-str
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrTheory.cpp
6275 lines (5751 loc) · 219 KB
/
strTheory.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
#include "strTheory.h"
FILE * logFile = NULL;
int sLevel = 0;
int searchStart = 0;
int tmpStringVarCount = 0;
int tmpIntVarCount = 0;
int tmpXorVarCount = 0;
int tmpBoolVarCount = 0;
int tmpConcatCount = 0;
int tmpUnrollVarCount = 0;
std::map<std::string, Z3_ast> constStr_astNode_map;
std::map<Z3_ast, Z3_ast> length_astNode_map;
std::map<std::pair<Z3_ast, Z3_ast>, Z3_ast> containPairBoolMap;
std::map<Z3_ast, std::set<std::pair<Z3_ast, Z3_ast> > > containPairIdxMap;
std::map<Z3_ast, int> basicStrVarAxiom_added;
std::map<std::pair<Z3_ast, Z3_ast>, Z3_ast> concat_astNode_map;
std::map<std::pair<Z3_ast, Z3_ast>, Z3_ast> contains_astNode_map;
std::map<std::pair<Z3_ast, Z3_ast>, std::map<int, Z3_ast> > varForBreakConcat;
//----------------------------------------------------------------
std::map<Z3_ast, int> inputVarMap;
std::set<Z3_ast> inputVarInLen;
//----------------------------------------------------------------
std::map<Z3_ast, unsigned int> fvarLenCountMap;
std::map<Z3_ast, std::vector<Z3_ast> > fvarLenTesterMap;
std::map<Z3_ast, Z3_ast> lenTesterFvarMap;
std::map<Z3_ast, std::map<int, std::vector<std::pair<int, Z3_ast> > > > fvarValueTesterMap;
std::map<Z3_ast, std::vector<int> > valRangeMap;
std::map<Z3_ast, Z3_ast> valueTesterFvarMap;
//----------------------------------------------------------------
char * charSet = NULL;
std::map<char, int> charSetLookupTable;
int charSetSize = 0;
Z3_ast emptyConstStr;
const std::string escapeDict[] = { "\\x00", "\\x01", "\\x02", "\\x03", "\\x04", "\\x05", "\\x06", "\\x07", "\\x08", "\\t", "\\n", "\\x0b", "\\x0c",
"\\r", "\\x0e", "\\x0f", "\\x10", "\\x11", "\\x12", "\\x13", "\\x14", "\\x15", "\\x16", "\\x17", "\\x18", "\\x19", "\\x1a", "\\x1b", "\\x1c",
"\\x1d", "\\x1e", "\\x1f", " ", "!", "\\\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z", "[", "\\\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "\\x7f", "\\x80", "\\x81", "\\x82", "\\x83", "\\x84", "\\x85", "\\x86",
"\\x87", "\\x88", "\\x89", "\\x8a", "\\x8b", "\\x8c", "\\x8d", "\\x8e", "\\x8f", "\\x90", "\\x91", "\\x92", "\\x93", "\\x94", "\\x95", "\\x96",
"\\x97", "\\x98", "\\x99", "\\x9a", "\\x9b", "\\x9c", "\\x9d", "\\x9e", "\\x9f", "\\xa0", "\\xa1", "\\xa2", "\\xa3", "\\xa4", "\\xa5", "\\xa6",
"\\xa7", "\\xa8", "\\xa9", "\\xaa", "\\xab", "\\xac", "\\xad", "\\xae", "\\xaf", "\\xb0", "\\xb1", "\\xb2", "\\xb3", "\\xb4", "\\xb5", "\\xb6",
"\\xb7", "\\xb8", "\\xb9", "\\xba", "\\xbb", "\\xbc", "\\xbd", "\\xbe", "\\xbf", "\\xc0", "\\xc1", "\\xc2", "\\xc3", "\\xc4", "\\xc5", "\\xc6",
"\\xc7", "\\xc8", "\\xc9", "\\xca", "\\xcb", "\\xcc", "\\xcd", "\\xce", "\\xcf", "\\xd0", "\\xd1", "\\xd2", "\\xd3", "\\xd4", "\\xd5", "\\xd6",
"\\xd7", "\\xd8", "\\xd9", "\\xda", "\\xdb", "\\xdc", "\\xdd", "\\xde", "\\xdf", "\\xe0", "\\xe1", "\\xe2", "\\xe3", "\\xe4", "\\xe5", "\\xe6",
"\\xe7", "\\xe8", "\\xe9", "\\xea", "\\xeb", "\\xec", "\\xed", "\\xee", "\\xef", "\\xf0", "\\xf1", "\\xf2", "\\xf3", "\\xf4", "\\xf5", "\\xf6",
"\\xf7", "\\xf8", "\\xf9", "\\xfa", "\\xfb", "\\xfc", "\\xfd", "\\xfe", "\\xff" };
//----------------------------------------------------------------
/*
*
*/
bool isConcatFunc(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->Concat)
return true;
else
return false;
}
/*
*
*/
inline bool isLengthFunc(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->Length)
return true;
else
return false;
}
/*
*
*/
bool isUnrollFunc(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->Unroll)
return true;
else
return false;
}
/*
*
*/
bool isSelectRetString(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
Z3_decl_kind decl_kind = Z3_get_decl_kind(ctx, d);
if (decl_kind == Z3_OP_SELECT && td->String == Z3_get_sort(ctx, n)) {
return true;
} else {
return false;
}
}
/*
*
*/
bool isRegexStar(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->RegexStar)
return true;
else
return false;
}
/*
*
*/
bool isStr2Regex(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->Str2Reg)
return true;
else
return false;
}
/*
*
*/
bool isRegexUnion(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->RegexUnion)
return true;
else
return false;
}
/*
*
*/
bool isRegexConcat(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (d == td->RegexConcat)
return true;
else
return false;
}
/*
*
*/
inline Z3_ast getAliasIndexAst(std::map<Z3_ast, Z3_ast> & aliasIndexMap, Z3_ast node) {
if (aliasIndexMap.find(node) != aliasIndexMap.end())
return aliasIndexMap[node];
else
return node;
}
/*
*
*/
std::string intToString(int i) {
std::stringstream ss;
ss << i;
return ss.str();
}
/*
*
*/
inline std::string longLongToString(long long i) {
std::stringstream ss;
ss << i;
return ss.str();
}
/*
*
*/
Z3_ast mk_var(Z3_context ctx, const char * name, Z3_sort ty) {
Z3_symbol s = Z3_mk_string_symbol(ctx, name);
return Z3_mk_const(ctx, s, ty);
}
/*
*
*/
Z3_ast mk_bool_var(Z3_context ctx, const char * name) {
Z3_sort ty = Z3_mk_bool_sort(ctx);
return mk_var(ctx, name, ty);
}
/*
*
*/
Z3_ast mk_int_var(Z3_context ctx, const char * name) {
Z3_sort ty = Z3_mk_int_sort(ctx);
return mk_var(ctx, name, ty);
}
/*
*
*/
Z3_ast mk_int(Z3_context ctx, int v) {
Z3_sort ty = Z3_mk_int_sort(ctx);
return Z3_mk_int(ctx, v, ty);
}
/*
*
*/
Z3_ast my_mk_str_value(Z3_theory t, char const * str) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData *) Z3_theory_get_ext_data(t);
// if the empty string is not created, create one
if (constStr_astNode_map.find("") == constStr_astNode_map.end()) {
Z3_symbol empty_str_sym = Z3_mk_string_symbol(ctx, "\"\"");
constStr_astNode_map[""] = Z3_theory_mk_value(ctx, t, empty_str_sym, td->String);
}
std::string keyStr = std::string(str);
// if the str is not created, create one
if (constStr_astNode_map.find(keyStr) == constStr_astNode_map.end()) {
Z3_symbol str_sym = Z3_mk_string_symbol(ctx, str);
Z3_ast strNode = Z3_theory_mk_value(ctx, t, str_sym, td->String);
constStr_astNode_map[keyStr] = strNode;
}
return constStr_astNode_map[keyStr];
}
/*
*
*/
Z3_ast my_mk_str_var(Z3_theory t, char const * name) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData *) Z3_theory_get_ext_data(t);
Z3_ast varAst = mk_var(ctx, name, td->String);
basicStrVarAxiom(t, varAst, __LINE__);
return varAst;
}
/*
*
*/
Z3_ast my_mk_internal_string_var(Z3_theory t) {
std::stringstream ss;
ss << tmpStringVarCount;
tmpStringVarCount++;
std::string name = "$$_str" + ss.str();
return my_mk_str_var(t, name.c_str());
}
Z3_ast my_mk_nonEmpty_string_var(Z3_theory t) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData *) Z3_theory_get_ext_data(t);
std::stringstream ss;
ss << tmpStringVarCount;
tmpStringVarCount++;
std::string name = "$$_str" + ss.str();
Z3_ast varAst = mk_var(ctx, name.c_str(), td->String);
nonEmptyStrVarAxiom(t, varAst, __LINE__);
return varAst;
}
/*
* Make an integer variable used for intermediated representation
*/
Z3_ast my_mk_internal_int_var(Z3_theory t) {
Z3_context ctx = Z3_theory_get_context(t);
std::stringstream ss;
ss << tmpIntVarCount;
tmpIntVarCount++;
std::string name = "$$_int_" + ss.str();
return mk_int_var(ctx, name.c_str());
}
Z3_ast my_mk_internal_bool_var(Z3_theory t) {
Z3_context ctx = Z3_theory_get_context(t);
std::stringstream ss;
ss << tmpBoolVarCount;
tmpBoolVarCount++;
std::string name = "$$_bol" + ss.str();
return mk_bool_var(ctx, name.c_str());
}
/*
*
*/
Z3_ast mk_internal_xor_var(Z3_theory t) {
Z3_context ctx = Z3_theory_get_context(t);
std::stringstream ss;
ss << tmpXorVarCount;
tmpXorVarCount++;
std::string name = "$$_xor_" + ss.str();
return mk_int_var(ctx, name.c_str());
}
/*
*
*/
Z3_ast mk_unrollBoundVar(Z3_theory t) {
Z3_context ctx = Z3_theory_get_context(t);
std::stringstream ss;
ss << tmpUnrollVarCount;
tmpUnrollVarCount++;
std::string name = "$$_unr_" + ss.str();
return mk_int_var(ctx, name.c_str());
}
/* ---------------------------------
* Return the node type in Enum
* ---------------------------------
*/
T_myZ3Type getNodeType(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_ast_kind z3Kind = Z3_get_ast_kind(ctx, n);
switch (z3Kind) {
case Z3_NUMERAL_AST: {
return my_Z3_Num;
break;
}
case Z3_APP_AST: {
Z3_sort s = Z3_get_sort(ctx, n);
Z3_sort_kind sk = Z3_get_sort_kind(ctx, s);
Z3_func_decl d = Z3_get_app_decl(ctx, Z3_to_app(ctx, n));
if (Z3_theory_is_value(t, n)) {
if (sk == Z3_BOOL_SORT) {
if (d == td->Contains || d == td->StartsWith || d == td->EndsWith || d == td->RegexIn) {
return my_Z3_Func;
} else {
return my_Z3_ConstBool;
}
} else if (sk == Z3_INT_SORT) {
if (d == td->Length || d == td->Indexof || d == td->Indexof2 || d == td->LastIndexof) {
return my_Z3_Func;
}
} else if (sk == Z3_UNKNOWN_SORT) {
if (s == td->String) {
if (d == td->Concat || d == td->SubString || d == td->Replace || d == td->Unroll || d == td->CharAt) {
return my_Z3_Func;
} else {
return my_Z3_ConstStr;
}
}
if (s == td->Regex) {
if (d == td->RegexConcat || d == td->RegexStar || d == td->RegexPlus || d == td->RegexCharRange || d == td->RegexUnion || d == td->Str2Reg)
return my_Z3_Func;
else
return my_Z3_Regex_Var;
}
} else if (sk == Z3_ARRAY_SORT) {
std::string vName = std::string(Z3_ast_to_string(ctx, n));
__debugPrint(logFile, "> [getNodeType] my_Z3_Func: %s\n\n", vName.c_str());
return my_Z3_Func;
}
} else {
//Z3 native functions fall into this category
Z3_decl_kind dk = Z3_get_decl_kind(ctx, d);
unsigned domainSize = Z3_get_domain_size(ctx, d);
if (dk != Z3_OP_UNINTERPRETED) {
// built-in function
return my_Z3_Func;
} else {
if (domainSize != 0) {
// "real" UNINTERPRETED function declared in the input
return my_Z3_Func;
} else {
if (s == td->String) {
return my_Z3_Str_Var;
} else if (s == td->Regex) {
return my_Z3_Regex_Var;
} else {
return my_Z3_Var;
}
}
}
}
break;
}
case Z3_VAR_AST: {
return my_Z3_Var;
break;
}
default: {
break;
}
}
return my_Z3_Unknown;
}
/*
*
*/
bool isConstStr(Z3_theory t, Z3_ast node) {
if (node == NULL)
return false;
if (getNodeType(t, node) == my_Z3_ConstStr) {
return true;
} else {
return false;
}
}
bool isStrVar(Z3_theory t, Z3_ast node) {
if (node == NULL)
return false;
if (getNodeType(t, node) == my_Z3_Str_Var) {
return true;
} else {
return false;
}
}
inline bool isStringSort(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_sort s = Z3_get_sort(ctx, n);
if (s == td->String)
return true;
else
return false;
}
inline bool isRegexSort(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_sort s = Z3_get_sort(ctx, n);
if (s == td->String)
return true;
else
return false;
}
/*
*
*/
Z3_ast mk_1_arg_app(Z3_context ctx, Z3_func_decl f, Z3_ast x) {
Z3_ast args[1] = { x };
return Z3_mk_app(ctx, f, 1, args);
}
/*
*
*/
Z3_ast mk_2_arg_app(Z3_context ctx, Z3_func_decl f, Z3_ast x, Z3_ast y) {
Z3_ast args[2] = { x, y };
return Z3_mk_app(ctx, f, 2, args);
}
/*
*
*/
Z3_ast my_mk_and(Z3_theory t, Z3_ast * item, int count) {
Z3_context ctx = Z3_theory_get_context(t);
if (count == 0)
return Z3_mk_true(ctx);
else if (count == 1)
return item[0];
else
return Z3_mk_and(ctx, count, item);
}
/*
*
*/
Z3_ast mk_2_and(Z3_theory t, Z3_ast and1, Z3_ast and2) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast and_items[2] = { and1, and2 };
return Z3_mk_and(ctx, 2, and_items);
}
/*
*
*/
Z3_ast mk_2_or(Z3_theory t, Z3_ast and1, Z3_ast and2) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast and_items[2] = { and1, and2 };
return Z3_mk_or(ctx, 2, and_items);
}
/*
*
*/
Z3_ast mk_2_sub(Z3_theory t, Z3_ast ast1, Z3_ast ast2) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast ast_items[2] = { ast1, ast2 };
return Z3_mk_sub(ctx, 2, ast_items);
}
/*
*
*/
Z3_ast mk_2_add(Z3_theory t, Z3_ast ast1, Z3_ast ast2) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast ast_items[2] = { ast1, ast2 };
return Z3_mk_add(ctx, 2, ast_items);
}
/*
*
*/
Z3_ast mk_and_fromVector(Z3_theory t, std::vector<Z3_ast> & vec) {
Z3_context ctx = Z3_theory_get_context(t);
if (vec.size() == 0) {
return NULL;
} else if (vec.size() == 1) {
return vec[0];
} else {
Z3_ast * items = new Z3_ast[vec.size()];
for (unsigned int i = 0; i < vec.size(); i++)
items[i] = vec[i];
Z3_ast toAssert = Z3_mk_and(ctx, vec.size(), items);
delete[] items;
return toAssert;
}
}
Z3_ast mk_and_fromSet(Z3_theory t, const std::set<Z3_ast> & fSet) {
Z3_context ctx = Z3_theory_get_context(t);
if (fSet.size() == 0) {
return NULL;
} else if (fSet.size() == 1) {
return (*fSet.begin());
} else {
Z3_ast * items = new Z3_ast[fSet.size()];
int i = 0;
std::set<Z3_ast>::const_iterator itor = fSet.begin();
for (; itor != fSet.end(); itor++) {
items[i++] = *itor;
}
Z3_ast toAssert = Z3_mk_and(ctx, fSet.size(), items);
delete[] items;
return toAssert;
}
}
/*
*
*/
Z3_ast mk_or_fromVector(Z3_theory t, std::vector<Z3_ast> & vec) {
Z3_context ctx = Z3_theory_get_context(t);
if (vec.size() == 0) {
return NULL;
} else if (vec.size() == 1) {
return vec[0];
} else {
Z3_ast * items = new Z3_ast[vec.size()];
for (unsigned int i = 0; i < vec.size(); i++)
items[i] = vec[i];
Z3_ast toAssert = Z3_mk_or(ctx, vec.size(), items);
delete[] items;
return toAssert;
}
}
Z3_ast mk_or_fromSet(Z3_theory t, const std::set<Z3_ast> & fSet) {
Z3_context ctx = Z3_theory_get_context(t);
if (fSet.size() == 0) {
return NULL;
} else if (fSet.size() == 1) {
return (*fSet.begin());
} else {
Z3_ast * items = new Z3_ast[fSet.size()];
int i = 0;
std::set<Z3_ast>::const_iterator itor = fSet.begin();
for (; itor != fSet.end(); itor++) {
items[i++] = *itor;
}
Z3_ast toAssert = Z3_mk_or(ctx, fSet.size(), items);
delete[] items;
return toAssert;
}
}
/*
*
*/
Z3_ast mk_length(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
if (length_astNode_map.find(n) == length_astNode_map.end()) {
if (isConstStr(t, n)) {
length_astNode_map[n] = mk_int(ctx, getConstStrValue(t, n).length());
} else {
length_astNode_map[n] = mk_1_arg_app(ctx, td->Length, n);
}
}
return length_astNode_map[n];
}
/*
*
*/
Z3_ast mk_unroll(Z3_theory t, Z3_ast n, Z3_ast bound) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_ast unrollFunc = NULL;
unrollFunc = mk_2_arg_app(ctx, td->Unroll, n, bound);
std::vector<Z3_ast> items;
items.push_back(Z3_mk_eq(ctx, Z3_mk_eq(ctx, bound, mk_int(ctx, 0)), Z3_mk_eq(ctx, unrollFunc, my_mk_str_value(t, ""))));
items.push_back(Z3_mk_ge(ctx, bound, mk_int(ctx, 0)));
items.push_back(Z3_mk_ge(ctx, mk_length(t, unrollFunc), mk_int(ctx, 0)));
addAxiom(t, mk_and_fromVector(t, items), __LINE__, false);
return unrollFunc;
}
/*
*
*/
Z3_ast mk_contains(Z3_theory t, Z3_ast n1, Z3_ast n2) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
std::pair<Z3_ast, Z3_ast> containsKey(n1, n2);
if (contains_astNode_map.find(containsKey) == contains_astNode_map.end()) {
if (isConstStr(t, n1) && isConstStr(t, n2)) {
std::string n1Str = getConstStrValue(t, n1);
std::string n2Str = getConstStrValue(t, n2);
if (n1Str.find(n2Str) != std::string::npos)
contains_astNode_map[containsKey] = Z3_mk_true(ctx);
else
contains_astNode_map[containsKey] = Z3_mk_false(ctx);
} else {
contains_astNode_map[containsKey] = mk_2_arg_app(ctx, td->Contains, n1, n2);
}
}
return contains_astNode_map[containsKey];
}
/*
*
*/
Z3_ast mk_concat(Z3_theory t, Z3_ast n1, Z3_ast n2) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
if (n1 == NULL || n2 == NULL) {
fprintf(stdout, "> Error: the strings to be concat cannot be NULL (@ %d).\n", __LINE__);
exit(0);
} else {
bool n1HasEqcValue = false;
bool n2HasEqcValue = false;
n1 = get_eqc_value(t, n1, n1HasEqcValue);
n2 = get_eqc_value(t, n2, n2HasEqcValue);
if (n1HasEqcValue && n2HasEqcValue) {
return Concat(t, n1, n2);
} else if (n1HasEqcValue && !n2HasEqcValue) {
bool n2_isConcatFunc = isConcatFunc(t, n2);
if (getConstStrValue(t, n1) == "") {
return n2;
}
if (n2_isConcatFunc) {
Z3_ast n2_arg0 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n2), 0);
Z3_ast n2_arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n2), 1);
if (isConstStr(t, n2_arg0)) {
n1 = Concat(t, n1, n2_arg0); // n1 will be a constant
n2 = n2_arg1;
}
}
} else if (!n1HasEqcValue && n2HasEqcValue) {
if (getConstStrValue(t, n2) == "") {
return n1;
}
if (isConcatFunc(t, n1)) {
Z3_ast n1_arg0 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n1), 0);
Z3_ast n1_arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n1), 1);
if (isConstStr(t, n1_arg1)) {
n1 = n1_arg0;
n2 = Concat(t, n1_arg1, n2); // n2 will be a constant
}
}
} else {
if (isConcatFunc(t, n1) && isConcatFunc(t, n2)) {
Z3_ast n1_arg0 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n1), 0);
Z3_ast n1_arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n1), 1);
Z3_ast n2_arg0 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n2), 0);
Z3_ast n2_arg1 = Z3_get_app_arg(ctx, Z3_to_app(ctx, n2), 1);
if (isConstStr(t, n1_arg1) && isConstStr(t, n2_arg0)) {
Z3_ast tmpN1 = n1_arg0;
Z3_ast tmpN2 = Concat(t, n1_arg1, n2_arg0);
n1 = mk_concat(t, tmpN1, tmpN2);
n2 = n2_arg1;
}
}
}
//------------------------------------------------------
// * Z3_ast ast1 = mk_2_arg_app(ctx, td->Concat, n1, n2);
// * Z3_ast ast2 = mk_2_arg_app(ctx, td->Concat, n1, n2);
// Z3 treats (ast1) and (ast2) as two different nodes.
//-------------------------------------------------------
std::pair<Z3_ast, Z3_ast> concatArgs(n1, n2);
Z3_ast concatAst = NULL;
if (concat_astNode_map.find(concatArgs) == concat_astNode_map.end()) {
concatAst = mk_2_arg_app(ctx, td->Concat, n1, n2);
concat_astNode_map[concatArgs] = concatAst;
Z3_ast concat_length = mk_length(t, concatAst);
std::vector<Z3_ast> childrenVector;
getNodesInConcat(t, concatAst, childrenVector);
Z3_ast * items = new Z3_ast[childrenVector.size()];
for (unsigned int i = 0; i < childrenVector.size(); i++) {
items[i] = mk_length(t, childrenVector[i]);
}
Z3_ast lenAssert = Z3_mk_eq(ctx, concat_length, Z3_mk_add(ctx, childrenVector.size(), items));
addAxiom(t, lenAssert, __LINE__, false);
delete[] items;
} else {
concatAst = concat_astNode_map[concatArgs];
}
return concatAst;
}
}
/*
*
*/
Z3_ast getLengthAST(Z3_theory t, Z3_ast n) {
return mk_length(t, n);
}
/*
* Query the integer theory.
* - If n has length value in integer theory, return the value.
* - Else, return -1.
*/
int getLenValue(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast lenAst = getLengthAST(t, n);
Z3_ast lenValueAst = Z3_theory_get_value_of_len(t, lenAst);
if (lenValueAst == NULL) {
return -1;
}
if (getNodeType(t, n) == my_Z3_ConstStr || lenAst != lenValueAst) {
char * str = (char *) Z3_ast_to_string(ctx, lenValueAst);
int len = atoi(str);
if (len < 0) {
__debugPrint(logFile, "\n\n\n\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
__debugPrint(logFile, "ERROR: length of ");
printZ3Node(t, n)
__debugPrint(logFile, " < 0\n");
exit(0);
}
return len;
}
return -1;
}
int getIntValue(Z3_theory t, Z3_ast n, bool & hasValue) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast valueAst = Z3_theory_get_value_of_len(t, n);
hasValue = false;
if (valueAst == NULL) {
return -1;
}
if (n != valueAst) {
char * str = (char *) Z3_ast_to_string(ctx, valueAst);
int val = atoi(str);
hasValue = true;
return val;
}
return -1;
}
/*
*
*/
void addAxiom(Z3_theory t, Z3_ast toAssert, int line, bool display) {
#ifdef DEBUGLOG
if (display) {
if (searchStart == 1) {
__debugPrint(logFile, "---------------------\nAxiom Add(@%d, Level %d):\n", line, sLevel);
printZ3Node(t, toAssert);
__debugPrint(logFile, "\n---------------------\n\n");
} else {
__debugPrint(logFile, "---------------------\nAssertion Add(@%d, Level %d):\n", line, sLevel);
printZ3Node(t, toAssert);
__debugPrint(logFile, "\n---------------------\n\n");
}
}
#endif
if (toAssert == NULL) {
return;
}
if (searchStart == 1) {
Z3_theory_assert_axiom(t, toAssert);
} else {
Z3_context ctx = Z3_theory_get_context(t);
Z3_assert_cnstr(ctx, toAssert);
}
}
/*
*
*/
void print_eq_class(Z3_theory t, Z3_ast n) {
#ifdef DEBUGLOG
__debugPrint(logFile, " EQC={ ");
Z3_ast curr = n;
int count = 0;
do {
if (count != 0) {
__debugPrint(logFile, ", ");
}
printZ3Node(t, curr);
curr = Z3_theory_get_eqc_next(t, curr);
count++;
}while (curr != n);
__debugPrint(logFile, " }");
#endif
}
/*
*
*/
void __printZ3Node(Z3_theory t, Z3_ast node) {
#ifdef DEBUGLOG
Z3_context ctx = Z3_theory_get_context(t);
if (node == NULL) {
__debugPrint(logFile, "NULL");
return;
}
T_myZ3Type nodeType = getNodeType(t, node);
switch (nodeType) {
case my_Z3_ConstStr: {
std::string str = getConstStrValue(t, node);
__debugPrint(logFile, "\"%s\"", str.c_str());
break;
}
case my_Z3_Func: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
case my_Z3_Num: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
case my_Z3_Var: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
case my_Z3_Str_Var: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
case my_Z3_Quantifier: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
case my_Z3_Unknown: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
default: {
__debugPrint(logFile, "%s", Z3_ast_to_string(ctx, node));
break;
}
}
#endif
}
/*
* Look for the equivalent constant for a node "n"
* Iterate the equivalence class
* If there is a constant,
* return the constant
* Otherwise,
* return n
*/
Z3_ast get_eqc_value(Z3_theory t, Z3_ast n, bool & hasEqcValue) {
Z3_ast curr = n;
do {
if (Z3_theory_is_value(t, curr)) {
if (isConstStr(t, curr)) {
hasEqcValue = true;
return curr;
}
}
curr = Z3_theory_get_eqc_next(t, curr);
} while (curr != n);
hasEqcValue = false;
return n;
}
/*
*
*/
std::string getConstStrValue(Z3_theory t, Z3_ast n) {
if (n == emptyConstStr) {
return std::string("");
}
Z3_context ctx = Z3_theory_get_context(t);
std::string strValue;
if (isConstStr(t, n)) {
char * str = (char *) Z3_ast_to_string(ctx, n);
strValue = std::string(str);
} else {
strValue = std::string("__NotConstStr__");
}
return strValue;
}
/*
*
*/
int getConstIntValue(Z3_theory t, Z3_ast n) {
Z3_context ctx = Z3_theory_get_context(t);
if (getNodeType(t, n) == my_Z3_Num) {
char * str = (char *) Z3_ast_to_string(ctx, n);
int val = atoi(str);
return val;
} else {
fprintf(stdout, "> Error: converting a non integer string to int @ %d. Exit.\n", __LINE__);
fflush(stdout);
exit(0);
}
return -1;
}
/*
*
*/
Z3_ast registerContain(Z3_theory t, Z3_ast str, Z3_ast subStr) {
#ifdef DEBUGLOG
__debugPrint(logFile, ">> [containRegister] Contains(");
printZ3Node(t, str);
__debugPrint(logFile, ", ");
printZ3Node(t, subStr);
#endif
std::pair<Z3_ast, Z3_ast> key = std::make_pair(str, subStr);
if (containPairBoolMap.find(key) == containPairBoolMap.end()) {
containPairBoolMap[key] = my_mk_internal_bool_var(t);
containPairIdxMap[str].insert(key);
containPairIdxMap[subStr].insert(key);
}
#ifdef DEBUGLOG
__debugPrint(logFile, ") = ");
printZ3Node(t, containPairBoolMap[key]);
__debugPrint(logFile, "\n");
#endif
return containPairBoolMap[key];
}
/*
*
*/
Z3_ast Concat(Z3_theory t, Z3_ast n1, Z3_ast n2) {
bool n1HasEqcValue = false;
bool n2HasEqcValue = false;
Z3_ast v1 = get_eqc_value(t, n1, n1HasEqcValue);
Z3_ast v2 = get_eqc_value(t, n2, n2HasEqcValue);
if (n1HasEqcValue && n2HasEqcValue) {
std::string n1_str = getConstStrValue(t, v1);
std::string n2_str = getConstStrValue(t, v2);
std::string result = n1_str + n2_str;
return my_mk_str_value(t, result.c_str());
} else if (n1HasEqcValue && !n2HasEqcValue) {