Repository navigation
Expand file tree
/
Copy pathstrAstReduce.cpp
More file actions
1379 lines (1256 loc) · 44.3 KB
/
Copy pathstrAstReduce.cpp
File metadata and controls
1379 lines (1256 loc) · 44.3 KB
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"
bool defaultCharSet = true;
/*
*
*/
std::string encodeToEscape(char c) {
int idx = (unsigned char) c;
if (0 <= idx && idx <= 255) {
return escapeDict[idx];
} else {
printf("> Error: should not get here @ %d.\n", __LINE__);
exit(0);
}
}
/*
*
*/
void setAlphabet() {
if (defaultCharSet) {
// a string in C notion cannot contain a char '\0'
charSetSize = 255;
charSet = new char[charSetSize];
int idx = 0;
// small letters
for (int i = 97; i < 123; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// caps
for (int i = 65; i < 91; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// numbers
for (int i = 48; i < 58; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// printable marks - 1
for (int i = 32; i < 48; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// printable marks - 2
for (int i = 58; i < 65; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// printable marks - 3
for (int i = 91; i < 97; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// printable marks - 4
for (int i = 123; i < 127; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// non-printable - 1
for (int i = 1; i < 32; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
// non-printable - 2
for (int i = 127; i < 256; i++) {
charSet[idx] = (char) i;
charSetLookupTable[charSet[idx]] = idx;
idx++;
}
} else {
const char setset[] = { 'a', 'b', 'c' };
int fSize = sizeof(setset) / sizeof(char);
charSet = new char[fSize];
charSetSize = fSize;
for (int i = 0; i < charSetSize; i++) {
charSet[i] = setset[i];
charSetLookupTable[setset[i]] = i;
}
}
}
/****************************************
* Z3 input parser doesn't understand constant string
* So, we pass const string by adding a special mark "$",
* --------------------------------------
* "__cOnStStR__x23_x5c_x6e_x5c_x22_x53"
****************************************/
inline bool isValidHexDigit(char c) {
if (('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')) {
return true;
}
return false;
}
/*
*
*/
inline int hexDigitToInt(char a) {
if ('0' <= a && a <= '9')
return a - '0';
else if ('a' <= a && a <= 'f')
return 10 + a - 'a';
else if ('A' <= a && a <= 'F')
return 10 + a - 'A';
else
return 0;
}
/*
*
*/
inline int twoHexDigitToChar(char a, char b) {
return (hexDigitToInt(a) * 16 + hexDigitToInt(b));
}
/*
*
*/
std::string convertInputTrickyConstStr(std::string inputStr) {
std::string outputStr = "";
std::string innerStr = inputStr.substr(11, inputStr.length() - 11);
int innerStrLen = innerStr.length();
if (innerStrLen % 4 != 0) {
fprintf(stdout, "> Error: Constant string conversion error. Exit.\n");
fprintf(stdout, " Input encoding: %s\n", inputStr.c_str());
fflush(stdout);
exit(0);
}
for (int i = 0; i < (innerStrLen / 4); i++) {
std::string cc = innerStr.substr(i * 4, 4);
if (cc[0] == '_' && cc[1] == 'x' && isValidHexDigit(cc[2]) && isValidHexDigit(cc[3])) {
char dc = twoHexDigitToChar(cc[2], cc[3]);
// Check whether the input character in the charSet
if (charSetLookupTable.find(dc) == charSetLookupTable.end()) {
fprintf(stdout, "> Error: Character '%s' in a constant string is not in the system alphabet.\n", encodeToEscape(dc).c_str());
fprintf(stdout, " Please set the character set accordingly.\n");
fflush(stdout);
exit(0);
}
outputStr = outputStr + std::string(1, dc);
}
}
return outputStr;
}
/*
*
*/
Z3_ast reduce_contains(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast reduceAst = NULL;
if (args[0] == args[1]) {
return Z3_mk_true(ctx);
}
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg0Str.find(arg1Str) != std::string::npos)
reduceAst = Z3_mk_true(ctx);
else
reduceAst = Z3_mk_false(ctx);
} else {
Z3_ast ts0 = my_mk_internal_string_var(t);
Z3_ast ts1 = my_mk_internal_string_var(t);
reduceAst = registerContain(t, args[0], args[1]); // reduceAst = Contains(args[0], args[1])
breakdownAssert = Z3_mk_eq(ctx, reduceAst, Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, mk_concat(t, args[1], ts1))));
}
return reduceAst;
}
/*
*
*/
Z3_ast reduce_startswith(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast reduceAst = NULL;
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg0Str.length() < arg1Str.length()) {
reduceAst = Z3_mk_false(ctx);
} else {
if (arg0Str.substr(0, arg1Str.length()) == arg1Str) {
reduceAst = Z3_mk_true(ctx);
} else {
reduceAst = Z3_mk_false(ctx);
}
}
} else {
Z3_ast resBoolVar = my_mk_internal_bool_var(t);
Z3_ast ts0 = my_mk_internal_string_var(t);
Z3_ast ts1 = my_mk_internal_string_var(t);
// boolVar = startswith(arg[0], arg[1])
// --------------------------------------------
std::vector<Z3_ast> innerItems;
innerItems.push_back( Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, ts1)) );
innerItems.push_back( Z3_mk_eq(ctx, mk_length(t, ts0), mk_length(t, args[1])) );
innerItems.push_back( Z3_mk_ite(ctx, Z3_mk_eq(ctx, ts0, args[1]), Z3_mk_eq(ctx, resBoolVar, Z3_mk_true(ctx)), Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ) );
Z3_ast then1 = mk_and_fromVector(t, innerItems);
breakdownAssert = Z3_mk_ite(ctx, Z3_mk_ge(ctx, mk_length(t, args[0]), mk_length(t, args[1])), then1, Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) );
reduceAst = resBoolVar;
}
return reduceAst;
}
/*
*
*/
Z3_ast reduce_endswith(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast reduceAst = NULL;
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg0Str.length() < arg1Str.length()) {
reduceAst = Z3_mk_false(ctx);
} else {
if (arg0Str.substr(arg0Str.length() - arg1Str.length(), arg1Str.length()) == arg1Str) {
reduceAst = Z3_mk_true(ctx);
} else {
reduceAst = Z3_mk_false(ctx);
}
}
} else {
Z3_ast resBoolVar = my_mk_internal_bool_var(t);
Z3_ast ts0 = my_mk_internal_string_var(t);
Z3_ast ts1 = my_mk_internal_string_var(t);
// boolVar = endswith(arg[0], arg[1])
// --------------------------------------------
std::vector<Z3_ast> innerItems;
innerItems.push_back( Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, ts1)) );
innerItems.push_back( Z3_mk_eq(ctx, mk_length(t, ts1), mk_length(t, args[1])) );
innerItems.push_back( Z3_mk_ite(ctx, Z3_mk_eq(ctx, ts1, args[1]), Z3_mk_eq(ctx, resBoolVar, Z3_mk_true(ctx)), Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) ) );
Z3_ast then1 = mk_and_fromVector(t, innerItems);
breakdownAssert = Z3_mk_ite(ctx, Z3_mk_ge(ctx, mk_length(t, args[0]), mk_length(t, args[1])), then1, Z3_mk_eq(ctx, resBoolVar, Z3_mk_false(ctx) ) );
reduceAst = resBoolVar;
}
return reduceAst;
}
/*
*
*/
Z3_ast reduce_indexof(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg0Str.find(arg1Str) != std::string::npos) {
int index = arg0Str.find(arg1Str);
return mk_int(ctx, index);
} else {
return mk_int(ctx, -1);
}
} else {
Z3_ast x1 = my_mk_internal_string_var(t);
Z3_ast x2 = my_mk_internal_string_var(t);
Z3_ast indexAst = my_mk_internal_int_var(t);
// condAst = Contains(args[0], args[1])
Z3_ast condAst = registerContain(t, args[0], args[1]);
// -----------------------
// true branch
std::vector<Z3_ast> thenItems;
// args[0] = x1 . args[1] . x2
thenItems.push_back(Z3_mk_eq(ctx, args[0], mk_concat(t, x1, mk_concat(t, args[1], x2))));
// indexAst = |x1|
thenItems.push_back(Z3_mk_eq(ctx, indexAst, mk_length(t, x1)));
// args[0] = x3 . x4
// /\ |x3| = |x1| + |args[1]| - 1
// /\ ! contains(x3, args[1])
Z3_ast x3 = my_mk_internal_string_var(t);
Z3_ast x4 = my_mk_internal_string_var(t);
Z3_ast tmpLenItems[3] = { indexAst, mk_length(t, args[1]), mk_int(ctx, -1) };
Z3_ast tmpLen = Z3_mk_add(ctx, 3, tmpLenItems);
thenItems.push_back(Z3_mk_eq(ctx, args[0], mk_concat(t, x3, x4)));
thenItems.push_back(Z3_mk_eq(ctx, mk_length(t, x3), tmpLen));
thenItems.push_back(Z3_mk_not(ctx, mk_contains(t, x3, args[1])));
// -----------------------
// false branch
Z3_ast elseBranch = Z3_mk_eq(ctx, indexAst, mk_int(ctx, -1));
breakdownAssert = Z3_mk_ite(ctx, condAst, mk_and_fromVector(t, thenItems), elseBranch);
return indexAst;
}
}
/*
*
*/
Z3_ast reduce_indexof2(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr && getNodeType(t, args[2]) == my_Z3_Num) {
std::string arg0 = getConstStrValue(t, args[0]);
std::string arg1 = getConstStrValue(t, args[1]);
int arg2Int = getConstIntValue(t, args[2]);
if (arg2Int >= (int)arg0.length()) {
return mk_int(ctx, -1);
} else if (arg2Int < 0) {
int index = arg0.find(arg1);
return mk_int(ctx, index);
} else {
std::string suffixStr = arg0.substr(arg2Int, arg0.length() - arg2Int);
if (suffixStr.find(arg1) != std::string::npos) {
int index = suffixStr.find(arg1) + arg2Int;
return mk_int(ctx, index);
} else {
return mk_int(ctx, -1);
}
}
} else {
// -------------------------------------------------------------------------------
// if (arg[2] >= length(arg[0])) // ite2
// resAst = -1
// else
// args[0] = prefix . suffix
// /\ indexAst = indexof(suffix, arg[1])
// /\ args[2] = len(prefix)
// /\ if (indexAst == -1) resAst = indexAst // ite3
// else resAst = args[2] + indexAst
// -------------------------------------------------------------------------------
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_ast resAst = my_mk_internal_int_var(t);
Z3_ast indexAst = my_mk_internal_int_var(t);
Z3_ast prefix = my_mk_internal_string_var(t);
Z3_ast suffix = my_mk_internal_string_var(t);
Z3_ast prefixLen = mk_length(t, prefix);
Z3_ast zeroAst = mk_int(ctx, 0);
Z3_ast negOneAst = mk_int(ctx, -1);
Z3_ast ite3 = Z3_mk_ite(ctx, Z3_mk_eq(ctx, indexAst, negOneAst),
Z3_mk_eq(ctx, resAst, negOneAst),
Z3_mk_eq(ctx, resAst, mk_2_add(t, args[2], indexAst))
);
Z3_ast ite2ElseItems[4];
ite2ElseItems[0] = Z3_mk_eq(ctx, args[0], mk_concat(t, prefix, suffix));
ite2ElseItems[1] = Z3_mk_eq(ctx, indexAst, mk_2_arg_app(ctx, td->Indexof, suffix, args[1]));
ite2ElseItems[2] = Z3_mk_eq(ctx, args[2], prefixLen);
ite2ElseItems[3] = ite3;
Z3_ast ite2Else = Z3_mk_and(ctx, 4, ite2ElseItems);
Z3_ast ite2 = Z3_mk_ite(ctx, Z3_mk_ge(ctx, args[2], mk_length(t, args[0])),
Z3_mk_eq(ctx, resAst, negOneAst),
ite2Else
);
Z3_ast ite1 = Z3_mk_ite(ctx, Z3_mk_lt(ctx, args[2], zeroAst),
Z3_mk_eq(ctx, resAst, mk_2_arg_app(ctx, td->Indexof, args[0], args[1])),
ite2
);
breakdownAssert = ite1;
return resAst;
}
}
/*
*
*/
Z3_ast reduce_lastindexof(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg0Str.rfind(arg1Str) != std::string::npos) {
int index = arg0Str.rfind(arg1Str);
return mk_int(ctx, index);
} else {
return mk_int(ctx, -1);
}
} else {
Z3_ast x1 = my_mk_internal_string_var(t);
Z3_ast x2 = my_mk_internal_string_var(t);
Z3_ast indexAst = my_mk_internal_int_var(t);
std::vector<Z3_ast> items;
// args[0] = x1 . args[1] . x2
Z3_ast eq1 = Z3_mk_eq(ctx, args[0], mk_concat(t, x1, mk_concat(t, args[1], x2)));
Z3_ast arg0HasArg1 = registerContain(t, args[0], args[1]); // arg0HasArg1 = Contains(args[0], args[1])
items.push_back(Z3_mk_eq(ctx, arg0HasArg1, eq1));
Z3_ast condAst = arg0HasArg1;
//----------------------------
// true branch
std::vector<Z3_ast> thenItems;
thenItems.push_back(Z3_mk_ge(ctx, indexAst, mk_int(ctx, 0)));
// args[0] = x1 . args[1] . x2
// x1 doesn't contain args[1]
thenItems.push_back(Z3_mk_not(ctx, mk_contains(t, x2, args[1])));
thenItems.push_back(Z3_mk_eq(ctx, indexAst, mk_length(t, x1)));
bool canSkip = false;
if (getNodeType(t, args[1]) == my_Z3_ConstStr) {
std::string arg1Str = getConstStrValue(t, args[1]);
if (arg1Str.length() == 1) {
canSkip = true;
}
}
if (!canSkip) {
// args[0] = x3 . x4 /\ |x3| = |x1| + 1 /\ ! contains(x4, args[1])
Z3_ast x3 = my_mk_internal_string_var(t);
Z3_ast x4 = my_mk_internal_string_var(t);
Z3_ast tmpLenItems[2] = { indexAst, mk_int(ctx, 1) };
Z3_ast tmpLen = Z3_mk_add(ctx, 2, tmpLenItems);
thenItems.push_back(Z3_mk_eq(ctx, args[0], mk_concat(t, x3, x4)));
thenItems.push_back(Z3_mk_eq(ctx, mk_length(t, x3), tmpLen));
thenItems.push_back(Z3_mk_not(ctx, mk_contains(t, x4, args[1])));
}
//----------------------------
// else branch
std::vector<Z3_ast> elseItems;
elseItems.push_back(Z3_mk_eq(ctx, indexAst, mk_int(ctx, -1)));
items.push_back(Z3_mk_ite(ctx, condAst, mk_and_fromVector(t, thenItems), mk_and_fromVector(t, elseItems)));
breakdownAssert = mk_and_fromVector(t, items);
return indexAst;
}
}
/*
*
*/
Z3_ast reduce_charAt(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
__debugPrint(logFile, "%d, %d | %d", getNodeType(t, args[0]), getNodeType(t, args[1]), my_Z3_Num);
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_Num) {
__debugPrint(logFile, "quick path\n");
std::string arg0Str = getConstStrValue(t, args[0]);
int arg1Int = getConstIntValue(t, args[1]);
std::string resStr;
if (0 <= arg1Int && arg1Int <= (int) arg0Str.length()) {
resStr.push_back(arg0Str.c_str()[arg1Int]);
} else {
resStr = "";
}
return my_mk_str_value(t, resStr.c_str());
} else {
Z3_ast ts0 = my_mk_internal_string_var(t);
Z3_ast ts1 = my_mk_internal_string_var(t);
Z3_ast ts2 = my_mk_internal_string_var(t);
Z3_ast cond = mk_2_and(t, Z3_mk_ge(ctx, args[1], mk_int(ctx, 0)), Z3_mk_lt(ctx, args[1], mk_length(t, args[0])));
Z3_ast and_item[3];
and_item[0] = Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, mk_concat(t, ts1, ts2)));
and_item[1] = Z3_mk_eq(ctx, args[1], mk_length(t, ts0));
and_item[2] = Z3_mk_eq(ctx, mk_length(t, ts1), mk_int(ctx, 1));
Z3_ast thenBranch = Z3_mk_and(ctx, 3, and_item);
Z3_ast elseBranch = Z3_mk_eq(ctx, ts1, my_mk_str_value(t, ""));
breakdownAssert = Z3_mk_ite(ctx, cond, thenBranch, elseBranch);
return ts1;
}
}
/*
*
*/
Z3_ast reduce_replace(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
if (getNodeType(t, args[0]) == my_Z3_ConstStr && getNodeType(t, args[1]) == my_Z3_ConstStr && getNodeType(t, args[2]) == my_Z3_ConstStr) {
std::string arg0Str = getConstStrValue(t, args[0]);
std::string arg1Str = getConstStrValue(t, args[1]);
std::string arg2Str = getConstStrValue(t, args[2]);
if (arg0Str.find(arg1Str) != std::string::npos) {
int index1 = arg0Str.find(arg1Str);
int index2 = index1 + arg1Str.length();
std::string substr0 = arg0Str.substr(0, index1);
std::string substr2 = arg0Str.substr(index2);
std::string replaced = substr0 + arg2Str + substr2;
return my_mk_str_value(t, replaced.c_str());
} else {
return args[0];
}
} else {
Z3_ast x1 = my_mk_internal_string_var(t);
Z3_ast x2 = my_mk_internal_string_var(t);
Z3_ast i1 = my_mk_internal_int_var(t);
Z3_ast result = my_mk_internal_string_var(t);
// condAst = Contains(args[0], args[1])
Z3_ast condAst = registerContain(t, args[0], args[1]);
// -----------------------
// true branch
std::vector<Z3_ast> thenItems;
// args[0] = x1 . args[1] . x2
thenItems.push_back(Z3_mk_eq(ctx, args[0], mk_concat(t, x1, mk_concat(t, args[1], x2))));
// i1 = |x1|
thenItems.push_back(Z3_mk_eq(ctx, i1, mk_length(t, x1)));
// args[0] = x3 . x4 /\ |x3| = |x1| + |args[1]| - 1 /\ ! contains(x3, args[1])
Z3_ast x3 = my_mk_internal_string_var(t);
Z3_ast x4 = my_mk_internal_string_var(t);
Z3_ast tmpLenItems[3] = {i1, mk_length(t, args[1]), mk_int(ctx, -1) };
Z3_ast tmpLen = Z3_mk_add(ctx, 3, tmpLenItems);
thenItems.push_back(Z3_mk_eq(ctx, args[0], mk_concat(t, x3, x4)));
thenItems.push_back(Z3_mk_eq(ctx, mk_length(t, x3), tmpLen));
thenItems.push_back(Z3_mk_not(ctx, mk_contains(t, x3, args[1])));
thenItems.push_back(Z3_mk_eq(ctx, result, mk_concat(t, x1, mk_concat(t, args[2], x2))));
// -----------------------
// false branch
Z3_ast elseBranch = Z3_mk_eq(ctx, result, args[0]);
breakdownAssert = Z3_mk_ite(ctx, condAst, mk_and_fromVector(t, thenItems), elseBranch);
return result;
}
}
/*
*
*/
Z3_ast reduce_subStr(Z3_theory t, Z3_ast const args[], Z3_ast & breakdownAssert) {
Z3_context ctx = Z3_theory_get_context(t);
bool arg0IsConstStr = (getNodeType(t, args[0]) == my_Z3_ConstStr);
bool arg1IsConstNum = (getNodeType(t, args[1]) == my_Z3_Num);
bool arg2IsConstNum = (getNodeType(t, args[2]) == my_Z3_Num);
// shortcut
if (arg1IsConstNum && getConstIntValue(t, args[1]) < 0) {
#ifdef DEBUGLOG
__debugPrint(logFile, "reducing substr-1\n");
#endif
breakdownAssert = NULL;
return my_mk_str_value(t, "");
}
// shortcut: arg0 is an empty string
if (arg0IsConstStr && "" == getConstStrValue(t, args[0])) {
#ifdef DEBUGLOG
__debugPrint(logFile, "reducing substr-2\n");
#endif
breakdownAssert = NULL;
return my_mk_str_value(t, "");
}
// shortcut
if (arg0IsConstStr && arg2IsConstNum) {
int len = getConstStrValue(t, args[0]).length();
int offset = getConstIntValue(t, args[2]);
if (offset > len) {
#ifdef DEBUGLOG
__debugPrint(logFile, "reducing substr-3: len = %d, offset = %d\n", len, offset);
#endif
breakdownAssert = NULL;
return my_mk_str_value(t, "");
}
}
// shortcut
if (arg0IsConstStr && arg1IsConstNum && arg2IsConstNum) {
std::string arg0Str = getConstStrValue(t, args[0]);
int idx = getConstIntValue(t, args[1]);
int offset = getConstIntValue(t, args[2]);
int len = arg0Str.length();
if ((idx + offset) > len) {
#ifdef DEBUGLOG
__debugPrint(logFile, "reducing substr-4: len = %d, idx = %d, offset = %d\n", len, idx, offset);
#endif
breakdownAssert = NULL;
return my_mk_str_value(t, "");
}
}
// arg1 >= 0 && (arg1 + arg2) <= len(arg0)
Z3_ast cond_items[2];
cond_items[0] = Z3_mk_ge(ctx, args[1], mk_int(ctx, 0));
Z3_ast len_add[2] = {args[1], args[2]};
cond_items[1] = Z3_mk_le(ctx, Z3_mk_add(ctx, 2, len_add), mk_length(t, args[0]));
Z3_ast cond = Z3_mk_and(ctx, 2, cond_items);
Z3_ast ts0 = my_mk_internal_string_var(t);
Z3_ast ts1 = my_mk_internal_string_var(t);
Z3_ast ts2 = my_mk_internal_string_var(t);
Z3_ast and_item[4];
and_item[0] = registerContain(t, args[0], ts1);
and_item[1] = Z3_mk_eq(ctx, args[0], mk_concat(t, ts0, mk_concat(t, ts1, ts2)));
and_item[2] = Z3_mk_eq(ctx, args[1], mk_length(t, ts0));
and_item[3] = Z3_mk_eq(ctx, args[2], mk_length(t, ts1));
Z3_ast true_branch = Z3_mk_and(ctx, 4, and_item);
Z3_ast false_branch = Z3_mk_eq(ctx, ts1, my_mk_str_value(t, ""));
breakdownAssert = Z3_mk_ite(ctx, cond, true_branch, false_branch);
return ts1;
}
/*
*
*/
Z3_ast reduce_str2regex(Z3_theory t, Z3_func_decl d, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
Z3_ast converted = mk_1_arg_app(ctx, d, args[0]);
return converted;
}
/*
*
*/
Z3_ast reduce_regexStar(Z3_theory t, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_app arg1_func_app = Z3_to_app(ctx, args[0]);
Z3_func_decl funcDecl = Z3_get_app_decl(ctx, arg1_func_app);
if (funcDecl == td->RegexStar)
return args[0];
else
return NULL;
}
/*
*
*/
Z3_ast reduce_regexPlus(Z3_theory t, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_app childFuncApp = Z3_to_app(ctx, args[0]);
Z3_func_decl childFuncDecl = Z3_get_app_decl(ctx, childFuncApp);
if (childFuncDecl == td->RegexPlus) {
// (r+)+ --> r+
return args[0];
} else if (childFuncDecl == td->RegexStar) {
// (r*)+ --> r*
Z3_ast childArg0 = Z3_get_app_arg(ctx, childFuncApp, 0);
return mk_1_arg_app(ctx, td->RegexStar, childArg0);
} else {
// r+ --> RegexConcat(r, r*)
return mk_2_arg_app(ctx, td->RegexConcat, args[0], mk_1_arg_app(ctx, td->RegexStar, args[0]));
}
}
/*
*
*/
Z3_ast reduce_regexConcat(Z3_theory t, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_app arg0_func_app = Z3_to_app(ctx, args[0]);
Z3_app arg1_func_app = Z3_to_app(ctx, args[1]);
Z3_func_decl arg0_funcDecl = Z3_get_app_decl(ctx, arg0_func_app);
Z3_func_decl arg1_funcDecl = Z3_get_app_decl(ctx, arg1_func_app);
if (arg0_funcDecl == td->Str2Reg && arg1_funcDecl == td->Str2Reg) {
Z3_ast arg0Str = Z3_get_app_arg(ctx, arg0_func_app, 0);
Z3_ast arg1Str = Z3_get_app_arg(ctx, arg1_func_app, 0);
Z3_ast comb = mk_concat(t, arg0Str, arg1Str);
return mk_1_arg_app(ctx, td->Str2Reg, comb);
}
return NULL;
}
/*
*
*/
Z3_ast reduce_regexCharRange(Z3_theory t, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
if (isConstStr(t, args[0]) && isConstStr(t, args[1])) {
std::string arg0Value = getConstStrValue(t, args[0]);
std::string arg1Value = getConstStrValue(t, args[1]);
if (arg0Value.size() == 1 && arg1Value.size() == 1) {
char low = arg0Value[0];
char high = arg1Value[0];
if (low > high) {
char t = low;
low = high;
high = t;
}
#ifdef DEBUGLOG
__debugPrint(logFile, ">> reduce_regexCharRange: arg0 = \"%s\", arg1 = \"%s\"\n", arg0Value.c_str(), arg1Value.c_str());
__debugPrint(logFile, " low = '%c', high = '%c'\n", low, high);
#endif
char c = low;
std::string cStr;
cStr.push_back(c);
Z3_ast res = mk_1_arg_app(ctx, td->Str2Reg, my_mk_str_value(t, cStr.c_str()));
c++;
for (; c <= high; c++) {
cStr.clear();
cStr.push_back(c);
res = mk_2_arg_app(ctx, td->RegexUnion, mk_1_arg_app(ctx, td->Str2Reg, my_mk_str_value(t, cStr.c_str())), res);
}
return res;
} else {
printf("> Error: The argument of RegexCharRange should be constant strings of size 1.\n");
printf(" Found (RegexCharRange %s %s)\n", arg0Value.c_str(), arg1Value.c_str());
}
} else {
printf("> Error: The argument of RegexCharRange should be constant strings.\n");
printf(" Found (RegexCharRange %s %s)\n", Z3_ast_to_string(ctx, args[0]), Z3_ast_to_string(ctx, args[1]));
}
exit(0);
}
/*
*
*/
Z3_ast reduce_regexIn(Z3_theory t, Z3_ast const args[], Z3_ast & extraAssert) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
Z3_app arg1_func_app = Z3_to_app(ctx, args[1]);
Z3_func_decl funcDecl = Z3_get_app_decl(ctx, arg1_func_app);
// quick path:
// >> if RegexIn(T4_4, (Str2Reg #)) --> "T4_4 = #" is enough
if (funcDecl == td->Str2Reg) {
return Z3_mk_eq(ctx, args[0], Z3_get_app_arg(ctx, arg1_func_app, 0));
}
// ----------------------------------------------------
else {
Z3_ast rhs = NULL;
Z3_ast resBoolVar = my_mk_internal_bool_var(t);
std::string regexStr = getStdRegexStr(t, args[1]);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> RegexIn(");
printZ3Node(t, args[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, args[1]);
__debugPrint(logFile, ")\n --> \n");
printZ3Node(t, args[0]);
__debugPrint(logFile, " \\in %s\n\n", regexStr.c_str());
#endif
std::pair<Z3_ast, std::string> key1 = std::make_pair(args[0], regexStr);
if (regexInBoolMap.find(key1) == regexInBoolMap.end()) {
regexInVarRegStrMap[args[0]].insert(regexStr);
regexInBoolMap[key1] = resBoolVar;
}
// -------------------------------------
// regexConcat
if (funcDecl == td->RegexConcat) {
Z3_ast var1 = mk_regexRepVar(t);
Z3_ast var2 = mk_regexRepVar(t);
rhs = mk_concat(t, var1, var2);
Z3_ast regex1 = Z3_get_app_arg(ctx, arg1_func_app, 0);
Z3_ast regex2 = Z3_get_app_arg(ctx, arg1_func_app, 1);
Z3_ast var1InRegex1 = mk_2_arg_app(ctx, td->RegexIn, var1, regex1);
Z3_ast var2InRegex2 = mk_2_arg_app(ctx, td->RegexIn, var2, regex2);
std::vector<Z3_ast> items;
items.push_back(var1InRegex1);
items.push_back(var2InRegex2);
items.push_back(Z3_mk_eq(ctx, resBoolVar, Z3_mk_eq(ctx, args[0], rhs)));
extraAssert = mk_and_fromVector(t, items);
return resBoolVar;
}
// -------------------------------------
// regexUnion
else if (funcDecl == td->RegexUnion) {
Z3_ast var1 = mk_regexRepVar(t);
Z3_ast var2 = mk_regexRepVar(t);
Z3_ast orVar = mk_2_or(t, Z3_mk_eq(ctx, args[0], var1), Z3_mk_eq(ctx, args[0], var2));
Z3_ast regex1 = Z3_get_app_arg(ctx, arg1_func_app, 0);
Z3_ast regex2 = Z3_get_app_arg(ctx, arg1_func_app, 1);
Z3_ast var1InRegex1 = mk_2_arg_app(ctx, td->RegexIn, var1, regex1);
Z3_ast var2InRegex2 = mk_2_arg_app(ctx, td->RegexIn, var2, regex2);
std::vector<Z3_ast> items;
items.push_back(var1InRegex1);
items.push_back(var2InRegex2);
items.push_back(Z3_mk_eq(ctx, resBoolVar, orVar));
extraAssert = mk_and_fromVector(t, items);
return resBoolVar;
}
// -------------------------------------
// regexStar
else if (funcDecl == td->RegexStar) {
Z3_ast regex1 = Z3_get_app_arg(ctx, arg1_func_app, 0);
Z3_ast unrollCnt = mk_unrollBoundVar(t);
Z3_ast unrollFunc = mk_unroll(t, regex1, unrollCnt);
std::vector<Z3_ast> items;
items.push_back(Z3_mk_eq(ctx, resBoolVar, Z3_mk_eq(ctx, args[0], unrollFunc)));
items.push_back(Z3_mk_eq(ctx, Z3_mk_eq(ctx, unrollCnt, mk_int(ctx, 0)), Z3_mk_eq(ctx, unrollFunc, my_mk_str_value(t, ""))));
extraAssert = mk_and_fromVector(t, items);
return resBoolVar;
}
// -------------------------------------
// error
else {
printf("> Error: Unknown Regex Operator @ %d", __LINE__);
exit(0);
return NULL;
}
}
}
/*
*
*/
Z3_bool cb_reduce_app(Z3_theory t, Z3_func_decl d, unsigned n, Z3_ast const * args, Z3_ast * result) {
Z3_context ctx = Z3_theory_get_context(t);
PATheoryData * td = (PATheoryData*) Z3_theory_get_ext_data(t);
// Convert the tricky "string" representation to string constant
int convertedFlag = 0;
Z3_ast * convertedArgs = new Z3_ast[n];
for (int i = 0; i < (int) n; i++) {
std::string symbolStr = std::string(Z3_ast_to_string(ctx, args[i]));
if (symbolStr.length() >= 11 && symbolStr.substr(0, 11) == "__cOnStStR_") {
convertedFlag = 1;
convertedArgs[i] = my_mk_str_value(t, convertInputTrickyConstStr(symbolStr).c_str());
} else {
convertedArgs[i] = args[i];
}
}
//---------------------------------
// reduce app: Concat
//---------------------------------
if (d == td->Concat) {
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): Concat(");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[1]);
__debugPrint(logFile, ")\n\n");
#endif
*result = mk_concat(t, convertedArgs[0], convertedArgs[1]);
delete[] convertedArgs;
return Z3_TRUE;
}
//---------------------------------
// reduce app: Length
//---------------------------------
else if (d == td->Length) {
if (getNodeType(t, convertedArgs[0]) == my_Z3_ConstStr) {
int size = getConstStrValue(t, convertedArgs[0]).size();
*result = mk_int(ctx, size);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): Length( ");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, " ) = ");
__debugPrint(logFile, "%d\n\n", size);
#endif
delete[] convertedArgs;
return Z3_TRUE;
} else {
if (convertedFlag == 1) {
*result = mk_1_arg_app(ctx, d, convertedArgs[0]);
delete[] convertedArgs;
return Z3_TRUE;
} else {
delete[] convertedArgs;
return Z3_FALSE;
}
}
}
//---------------------------------
// reduce app: SubString
//---------------------------------
else if (d == td->SubString) {
Z3_ast breakDownAst = NULL;
*result = reduce_subStr(t, convertedArgs, breakDownAst);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n");
__debugPrint(logFile, "\n>> cb_reduce_app(): SubString( ");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[1]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[2]);
__debugPrint(logFile, ") => ");
printZ3Node(t, *result);
__debugPrint(logFile, "\n-- ADD(@%d, Level %d):\n", __LINE__, sLevel);
printZ3Node(t, breakDownAst);
__debugPrint(logFile, "\n\n");
#endif
if (breakDownAst != NULL) {
Z3_assert_cnstr(ctx, breakDownAst);
}
delete[] convertedArgs;
return Z3_TRUE;
}
//------------------------------------------
// Reduce app: Contains
//------------------------------------------
else if (d == td->Contains) {
Z3_ast breakDownAst = NULL;
*result = reduce_contains(t, convertedArgs, breakDownAst);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): Contains(");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[1]);
__debugPrint(logFile, ")");
__debugPrint(logFile, " => ");
printZ3Node(t, *result);
if( breakDownAst != NULL )
{
__debugPrint(logFile, "\n-- ADD(@%d): \n", __LINE__);
printZ3Node(t, breakDownAst);
}
__debugPrint(logFile, "\n\n");
#endif
// when quick path is taken, breakDownAst == NULL;
if (breakDownAst != NULL)
Z3_assert_cnstr(ctx, breakDownAst);
delete[] convertedArgs;
return Z3_TRUE;
}
//------------------------------------------
// Reduce app: StartsWith
//------------------------------------------
else if (d == td->StartsWith) {
Z3_ast breakDownAst = NULL;
*result = reduce_startswith(t, convertedArgs, breakDownAst);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): StartsWith(");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[1]);
__debugPrint(logFile, " )");
__debugPrint(logFile, " => ");
printZ3Node(t, *result);
__debugPrint(logFile, "\n\n");
#endif
if (breakDownAst != NULL)
Z3_assert_cnstr(ctx, breakDownAst);
delete[] convertedArgs;
return Z3_TRUE;
}
//------------------------------------------
// Reduce app: EndsWith
//------------------------------------------
else if (d == td->EndsWith) {
Z3_ast breakDownAst = NULL;
*result = reduce_endswith(t, convertedArgs, breakDownAst);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): EndsWith(");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");
printZ3Node(t, convertedArgs[1]);
__debugPrint(logFile, ")");
__debugPrint(logFile, " => ");
printZ3Node(t, *result);
if( breakDownAst != NULL )
{
__debugPrint(logFile, "\n-- ADD(@%d): \n", __LINE__);
printZ3Node(t, breakDownAst);
}
__debugPrint(logFile, "\n\n");
#endif
if (breakDownAst != NULL)
Z3_assert_cnstr(ctx, breakDownAst);
delete[] convertedArgs;
return Z3_TRUE;
}
//------------------------------------------
// Reduce app: Indexof
//------------------------------------------
else if (d == td->Indexof) {
Z3_ast breakDownAst = NULL;
*result = reduce_indexof(t, convertedArgs, breakDownAst);
#ifdef DEBUGLOG
__debugPrint(logFile, "\n>> cb_reduce_app(): Indexof(");
printZ3Node(t, convertedArgs[0]);
__debugPrint(logFile, ", ");