aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/nfa/mcsheng_compile.cpp
blob: 4cb40c643581b1a845c0a721d7b4a2f90dac00cf (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
/* 
 * Copyright (c) 2016-2020, Intel Corporation
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 
 *  * Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer. 
 *  * Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 *  * Neither the name of Intel Corporation nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE. 
 */ 
 
#include "mcsheng_compile.h" 
 
#include "accel.h" 
#include "accelcompile.h" 
#include "grey.h" 
#include "mcclellancompile.h" 
#include "mcclellancompile_util.h" 
#include "mcsheng_internal.h" 
#include "nfa_internal.h" 
#include "rdfa_graph.h" 
#include "shufticompile.h" 
#include "trufflecompile.h" 
#include "ue2common.h" 
#include "util/alloc.h" 
#include "util/bitutils.h" 
#include "util/charreach.h" 
#include "util/compare.h" 
#include "util/compile_context.h" 
#include "util/container.h" 
#include "util/flat_containers.h" 
#include "util/graph.h" 
#include "util/graph_range.h" 
#include "util/make_unique.h" 
#include "util/order_check.h" 
#include "util/report_manager.h" 
#include "util/unaligned.h" 
#include "util/unordered.h" 
#include "util/verify_types.h" 
 
#include <algorithm> 
#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <map> 
#include <memory> 
#include <set> 
#include <deque> 
#include <vector> 
#include <boost/range/adaptor/map.hpp> 
 
using namespace std; 
using boost::adaptors::map_keys; 
 
namespace ue2 { 
 
namespace /* anon */ { 
 
#define MIN_SHENG_SIZE 6 
#define INVALID_SHENG_ID 255 
 
struct dstate_extra { 
    u16 daddytaken = 0; 
    bool shermanState = false; 
    bool sheng_succ = false; 
    u8 sheng_id = INVALID_SHENG_ID; 
}; 
 
struct dfa_info { 
    accel_dfa_build_strat &strat; 
    raw_dfa &raw; 
    vector<dstate> &states; 
    vector<dstate_extra> extra; 
    const u16 alpha_size; /* including special symbols */ 
    const array<u16, ALPHABET_SIZE> &alpha_remap; 
    vector<CharReach> rev_alpha; 
    const u16 impl_alpha_size; 
 
    u8 getAlphaShift() const; 
 
    explicit dfa_info(accel_dfa_build_strat &s) 
                                : strat(s), 
                                  raw(s.get_raw()), 
                                  states(raw.states), 
                                  extra(raw.states.size()), 
                                  alpha_size(raw.alpha_size), 
                                  alpha_remap(raw.alpha_remap), 
                                  impl_alpha_size(raw.getImplAlphaSize()) { 
        rev_alpha.resize(impl_alpha_size); 
        for (u32 i = 0; i < N_CHARS; i++) { 
            rev_alpha[alpha_remap[i]].set(i); 
        } 
    } 
 
    dstate_id_t implId(dstate_id_t raw_id) const { 
        return states[raw_id].impl_id; 
    } 
 
    bool is_sherman(dstate_id_t raw_id) const { 
        return extra[raw_id].shermanState; 
    } 
 
    bool is_sheng(dstate_id_t raw_id) const { 
        return extra[raw_id].sheng_id != INVALID_SHENG_ID; 
    } 
 
    bool is_sheng_succ(dstate_id_t raw_id) const { 
        return extra[raw_id].sheng_succ; 
    } 
 
    /* states which use the normal transition/successor table */ 
    bool is_normal(dstate_id_t raw_id) const { 
        return raw_id != DEAD_STATE && !is_sheng(raw_id) && !is_sherman(raw_id); 
    } 
    size_t size(void) const { return states.size(); } 
}; 
 
u8 dfa_info::getAlphaShift() const { 
    if (impl_alpha_size < 2) { 
        return 1; 
    } else { 
        /* log2 round up */ 
        return 32 - clz32(impl_alpha_size - 1); 
    } 
} 
 
} // namespace 
 
static 
mstate_aux *getAux(NFA *n, dstate_id_t i) { 
    mcsheng *m = (mcsheng *)getMutableImplNfa(n); 
    mstate_aux *aux_base = (mstate_aux *)((char *)n + m->aux_offset); 
 
    mstate_aux *aux = aux_base + i; 
    assert((const char *)aux < (const char *)n + m->length); 
    return aux; 
} 
 
static 
void createShuffleMasks(mcsheng *m, const dfa_info &info, 
                       dstate_id_t sheng_end, 
                       const map<dstate_id_t, AccelScheme> &accel_escape_info) { 
    DEBUG_PRINTF("using first %hu states for a sheng\n", sheng_end); 
    assert(sheng_end > DEAD_STATE + 1); 
    assert(sheng_end <= sizeof(m128) + 1); 
    vector<array<u8, sizeof(m128)>> masks; 
    masks.resize(info.alpha_size); 
    /* -1 to avoid wasting a slot as we do not include dead state */ 
    vector<dstate_id_t> raw_ids; 
    raw_ids.resize(sheng_end - 1); 
    for (dstate_id_t s = DEAD_STATE + 1; s < info.states.size(); s++) { 
        assert(info.implId(s)); /* should not map to DEAD_STATE */ 
        if (info.is_sheng(s)) { 
            raw_ids[info.extra[s].sheng_id] = s; 
        } 
    } 
    for (u32 i = 0; i < info.alpha_size; i++) { 
        if (i == info.alpha_remap[TOP]) { 
            continue; 
        } 
        auto &mask = masks[i]; 
        assert(sizeof(mask) == sizeof(m128)); 
        mask.fill(0); 
 
        for (dstate_id_t sheng_id = 0; sheng_id < sheng_end - 1; sheng_id++) { 
            dstate_id_t raw_id = raw_ids[sheng_id]; 
            dstate_id_t next_id = info.implId(info.states[raw_id].next[i]); 
            if (next_id == DEAD_STATE) { 
                next_id = sheng_end - 1; 
            } else if (next_id < sheng_end) { 
                next_id--; 
            } 
            DEBUG_PRINTF("%hu: %u->next %hu\n", sheng_id, i, next_id); 
            mask[sheng_id] = verify_u8(next_id); 
        } 
    } 
    for (u32 i = 0; i < N_CHARS; i++) { 
        assert(info.alpha_remap[i] != info.alpha_remap[TOP]); 
        memcpy((u8 *)&m->sheng_masks[i], 
               (u8 *)masks[info.alpha_remap[i]].data(), sizeof(m128)); 
    } 
    m->sheng_end = sheng_end; 
    m->sheng_accel_limit = sheng_end - 1; 
 
    for (dstate_id_t s : raw_ids) { 
        if (contains(accel_escape_info, s)) { 
            LIMIT_TO_AT_MOST(&m->sheng_accel_limit, info.extra[s].sheng_id); 
        } 
    } 
} 
 
static 
void populateBasicInfo(size_t state_size, const dfa_info &info, 
                       u32 total_size, u32 aux_offset, u32 accel_offset, 
                       u32 accel_count, ReportID arb, bool single, NFA *nfa) { 
    assert(state_size == sizeof(u16) || state_size == sizeof(u8)); 
 
    nfa->length = total_size; 
    nfa->nPositions = info.states.size(); 
 
    nfa->scratchStateSize = verify_u32(state_size); 
    nfa->streamStateSize = verify_u32(state_size); 
 
    if (state_size == sizeof(u8)) { 
        nfa->type = MCSHENG_NFA_8; 
    } else { 
        nfa->type = MCSHENG_NFA_16; 
    } 
 
    mcsheng *m = (mcsheng *)getMutableImplNfa(nfa); 
    for (u32 i = 0; i < 256; i++) { 
        m->remap[i] = verify_u8(info.alpha_remap[i]); 
    } 
    m->alphaShift = info.getAlphaShift(); 
    m->length = total_size; 
    m->aux_offset = aux_offset; 
    m->accel_offset = accel_offset; 
    m->arb_report = arb; 
    m->state_count = verify_u16(info.size()); 
    m->start_anchored = info.implId(info.raw.start_anchored); 
    m->start_floating = info.implId(info.raw.start_floating); 
    m->has_accel = accel_count ? 1 : 0; 
 
    if (single) { 
        m->flags |= MCSHENG_FLAG_SINGLE; 
    } 
} 
 
static 
mstate_aux *getAux64(NFA *n, dstate_id_t i) {
    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(n);
    mstate_aux *aux_base = (mstate_aux *)((char *)n + m->aux_offset);

    mstate_aux *aux = aux_base + i;
    assert((const char *)aux < (const char *)n + m->length);
    return aux;
}

static
void createShuffleMasks64(mcsheng64 *m, const dfa_info &info,
                      dstate_id_t sheng_end,
                      const map<dstate_id_t, AccelScheme> &accel_escape_info) {
    DEBUG_PRINTF("using first %hu states for a sheng\n", sheng_end);
    assert(sheng_end > DEAD_STATE + 1);
    assert(sheng_end <= sizeof(m512) + 1);
    vector<array<u8, sizeof(m512)>> masks;
    masks.resize(info.alpha_size);
    /* -1 to avoid wasting a slot as we do not include dead state */
    vector<dstate_id_t> raw_ids;
    raw_ids.resize(sheng_end - 1);
    for (dstate_id_t s = DEAD_STATE + 1; s < info.states.size(); s++) {
        assert(info.implId(s)); /* should not map to DEAD_STATE */
        if (info.is_sheng(s)) {
            raw_ids[info.extra[s].sheng_id] = s;
        }
    }
    for (u32 i = 0; i < info.alpha_size; i++) {
        if (i == info.alpha_remap[TOP]) {
            continue;
        }
        auto &mask = masks[i];
        assert(sizeof(mask) == sizeof(m512));
        mask.fill(0);

        for (dstate_id_t sheng_id = 0; sheng_id < sheng_end - 1; sheng_id++) {
            dstate_id_t raw_id = raw_ids[sheng_id];
            dstate_id_t next_id = info.implId(info.states[raw_id].next[i]);
            if (next_id == DEAD_STATE) {
                next_id = sheng_end - 1;
            } else if (next_id < sheng_end) {
                next_id--;
            }
            DEBUG_PRINTF("%hu: %u->next %hu\n", sheng_id, i, next_id);
            mask[sheng_id] = verify_u8(next_id);
        }
    }
    for (u32 i = 0; i < N_CHARS; i++) {
        assert(info.alpha_remap[i] != info.alpha_remap[TOP]);
        memcpy((u8 *)&m->sheng_succ_masks[i],
               (u8 *)masks[info.alpha_remap[i]].data(), sizeof(m512));
    }
    m->sheng_end = sheng_end;
    m->sheng_accel_limit = sheng_end - 1;

    for (dstate_id_t s : raw_ids) {
        if (contains(accel_escape_info, s)) {
            LIMIT_TO_AT_MOST(&m->sheng_accel_limit, info.extra[s].sheng_id);
        }
    }
}

static
void populateBasicInfo64(size_t state_size, const dfa_info &info,
                         u32 total_size, u32 aux_offset, u32 accel_offset,
                         u32 accel_count, ReportID arb, bool single, NFA *nfa) {
    assert(state_size == sizeof(u16) || state_size == sizeof(u8));

    nfa->length = total_size;
    nfa->nPositions = info.states.size();

    nfa->scratchStateSize = verify_u32(state_size);
    nfa->streamStateSize = verify_u32(state_size);

    if (state_size == sizeof(u8)) {
        nfa->type = MCSHENG_64_NFA_8;
    } else {
        nfa->type = MCSHENG_64_NFA_16;
    }

    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(nfa);
    for (u32 i = 0; i < 256; i++) {
        m->remap[i] = verify_u8(info.alpha_remap[i]);
    }
    m->alphaShift = info.getAlphaShift();
    m->length = total_size;
    m->aux_offset = aux_offset;
    m->accel_offset = accel_offset;
    m->arb_report = arb;
    m->state_count = verify_u16(info.size());
    m->start_anchored = info.implId(info.raw.start_anchored);
    m->start_floating = info.implId(info.raw.start_floating);
    m->has_accel = accel_count ? 1 : 0;

    if (single) {
        m->flags |= MCSHENG_FLAG_SINGLE;
    }
}

static
size_t calcShermanRegionSize(const dfa_info &info) { 
    size_t rv = 0; 
 
    for (size_t i = 0; i < info.size(); i++) { 
        if (info.is_sherman(i)) { 
            rv += SHERMAN_FIXED_SIZE; 
        } 
    } 
 
    return ROUNDUP_16(rv); 
} 
 
static 
void fillInAux(mstate_aux *aux, dstate_id_t i, const dfa_info &info, 
               const vector<u32> &reports, const vector<u32> &reports_eod, 
               const vector<u32> &reportOffsets) { 
    const dstate &raw_state = info.states[i]; 
    aux->accept = raw_state.reports.empty() ? 0 : reportOffsets[reports[i]]; 
    aux->accept_eod = raw_state.reports_eod.empty() ? 0 
                                              : reportOffsets[reports_eod[i]]; 
    aux->top = info.implId(i ? raw_state.next[info.alpha_remap[TOP]] 
                             : info.raw.start_floating); 
} 
 
/* returns false on error */ 
static 
bool allocateImplId16(dfa_info &info, dstate_id_t sheng_end, 
                      dstate_id_t *sherman_base) {
    info.states[0].impl_id = 0; /* dead is always 0 */ 
 
    vector<dstate_id_t> norm; 
    vector<dstate_id_t> sherm; 
    vector<dstate_id_t> norm_sheng_succ; 
    vector<dstate_id_t> sherm_sheng_succ; 
 
    if (info.size() > (1 << 16)) { 
        DEBUG_PRINTF("too many states\n"); 
        *sherman_base = 0; 
        return false; 
    } 
 
    for (u32 i = 1; i < info.size(); i++) { 
        if (info.is_sheng(i)) { 
            continue; /* sheng impl ids have already been allocated */ 
        } if (info.is_sherman(i)) { 
            if (info.is_sheng_succ(i)) { 
                sherm_sheng_succ.push_back(i); 
            } else { 
                sherm.push_back(i); 
            } 
        } else { 
            if (info.is_sheng_succ(i)) { 
                norm_sheng_succ.push_back(i); 
            } else { 
                norm.push_back(i); 
            } 
        } 
    } 
 
    dstate_id_t next_norm = sheng_end; 
    for (dstate_id_t s : norm_sheng_succ) { 
        info.states[s].impl_id = next_norm++; 
    } 
    if (next_norm + norm.size() + sherm_sheng_succ.size() > UINT8_MAX) { 
        /* we need to give sheng_succs ids which fit into a u8 -- demote these 
         * to normal states */ 
        for (dstate_id_t s : sherm_sheng_succ) { 
            info.states[s].impl_id = next_norm++; 
            info.extra[s].shermanState = false; 
        } 
        sherm_sheng_succ.clear(); 
    } 
    for (dstate_id_t s : norm) { 
        info.states[s].impl_id = next_norm++; 
    } 
 
    *sherman_base = next_norm; 
    dstate_id_t next_sherman = next_norm; 
 
    for (dstate_id_t s : sherm_sheng_succ) { 
        info.states[s].impl_id = next_sherman++; 
    } 
 
    for (dstate_id_t s : sherm) { 
        info.states[s].impl_id = next_sherman++; 
    } 
 
    /* Check to see if we haven't over allocated our states */ 
    DEBUG_PRINTF("next sherman %u masked %u\n", next_sherman, 
                 (dstate_id_t)(next_sherman & STATE_MASK)); 
    return (next_sherman - 1) == ((next_sherman - 1) & STATE_MASK); 
} 
 
typedef RdfaGraph::vertex_descriptor RdfaVertex; 
 
static 
bool mark_sheng_succs(const RdfaGraph &g, dfa_info &info, 
                      const flat_set<RdfaVertex> &sheng_states) { 
    u32 exit_count = 0; 
 
    for (auto v : sheng_states) { 
        dstate_id_t s = g[v].index; 
        for (u32 i = 0; i != info.alpha_size; i++) { 
            if (i == info.alpha_remap[TOP]) { 
                continue; 
            } 
            dstate_id_t next = info.states[s].next[i]; 
            if (!next || info.is_sheng(next) || info.is_sheng_succ(next)) { 
                continue; 
            } 
            exit_count++; 
            info.extra[next].sheng_succ = true; 
        } 
    } 
 
    if (exit_count + sheng_states.size() < UINT8_MAX) { 
        return true; 
    } else { 
        DEBUG_PRINTF("fail: unable to fit %u exits in byte", exit_count); 
        return false; 
    } 
} 
 
static 
CharReach get_edge_reach(dstate_id_t u, dstate_id_t v, const dfa_info &info) { 
    CharReach rv; 
    for (u32 i = 0; i < info.impl_alpha_size; i++) { 
        if (info.raw.states[u].next[i] == v) { 
            assert(info.rev_alpha[i].any()); 
            rv |= info.rev_alpha[i]; 
        } 
    } 
    assert(rv.any()); 
    return rv; 
} 
 
#define MAX_SHENG_STATES 16 
#define MAX_SHENG64_STATES 64
#define MAX_SHENG_LEAKINESS 0.05 
 
using LeakinessCache = ue2_unordered_map<pair<RdfaVertex, u32>, double>; 
 
/** 
 * Returns the proportion of strings of length 'depth' which will leave the 
 * sheng region when starting at state 'u'. 
 */ 
static 
double leakiness(const RdfaGraph &g, dfa_info &info, 
                 const flat_set<RdfaVertex> &sheng_states, RdfaVertex u, 
                 u32 depth, LeakinessCache &cache) { 
    double rv = 0; 
    if (contains(cache, make_pair(u, depth))) { 
        return cache[make_pair(u, depth)]; 
    } 
    for (RdfaVertex v : adjacent_vertices_range(u, g)) { 
        if (g[v].index == DEAD_STATE) { 
            continue; 
        } 
        double width = get_edge_reach(g[u].index, g[v].index, info).count(); 
        width /= N_CHARS; 
 
        double weight; 
        if (!contains(sheng_states, v)) { 
            weight = 1; 
        } else if (depth > 1) { 
             weight = leakiness(g, info, sheng_states, v, depth - 1, cache); 
        } else { 
            continue; /* weight = 0 */ 
        } 
        rv += width * weight; 
    } 
 
    cache[make_pair(u, depth)] = rv; 
    DEBUG_PRINTF("%zu [%u] q = %g\n", g[u].index, depth, rv); 
    return rv; 
} 
 
/** 
 * Returns the proportion of 8 byte strings which will leave the sheng region 
 * when starting at state 'u'. 
 */ 
static 
double leakiness(const RdfaGraph &g, dfa_info &info, 
                 const flat_set<RdfaVertex> &sheng_states, RdfaVertex u) { 
    LeakinessCache cache; 
    double rv = leakiness(g, info, sheng_states, u, 8, cache); 
    return rv; 
} 
 
static 
dstate_id_t find_sheng_states(dfa_info &info, 
                              map<dstate_id_t, AccelScheme> &accel_escape_info,
                              size_t max_sheng_states) {
    RdfaGraph g(info.raw); 
    auto cyclics = find_vertices_in_cycles(g); 
 
    auto base_cyclic = RdfaGraph::null_vertex(); 
    for (const auto &v : cyclics) { 
        if (g[v].index == DEAD_STATE) { 
            continue; 
        } 
        DEBUG_PRINTF("considering cyclic %zu\n", g[v].index); 
        /* get an estimate of stickness of the cyclic: assume any edges from 
         * states with larger state ids are back edges */ 
        CharReach est_back_reach; 
        for (const auto &u : inv_adjacent_vertices_range(v, g)) { 
            if (g[u].index < g[v].index) { 
                continue; 
            } 
            est_back_reach |= get_edge_reach(g[u].index, g[v].index, info); 
        } 
 
        if (est_back_reach.count() < 30) { 
            continue; 
        } 
        base_cyclic = v; 
        break; 
    } 
    if (!base_cyclic) { 
        return DEAD_STATE; 
    } 
 
    flat_set<RdfaVertex> sheng_states; 
    deque<RdfaVertex> to_consider = { base_cyclic }; 
    flat_set<dstate_id_t> considered = { DEAD_STATE }; 
    bool seen_back_edge = false; 
    while (!to_consider.empty() 
           && sheng_states.size() < max_sheng_states) {
        auto v = to_consider.front(); 
        to_consider.pop_front(); 
        if (!considered.insert(g[v].index).second) { 
            continue; 
        } 
 
        assert(!contains(sheng_states, v)); 
 
        if (generates_callbacks(info.raw.kind) 
            && !info.states[g[v].index].reports.empty()) { 
            /* cannot raise callbacks from sheng region */ 
            continue; 
        } 
 
        sheng_states.insert(v); 
        for (const auto &t : adjacent_vertices_range(v, g)) { 
            if (!contains(considered, g[t].index)) { 
                to_consider.push_back(t); 
            } 
            if (t == base_cyclic) { 
                seen_back_edge = true; 
            } 
        } 
    } 
 
    /* allocate normal ids */ 
    dstate_id_t sheng_end = DEAD_STATE + 1; 
    for (auto v : sheng_states) { 
        dstate_id_t s = g[v].index; 
        if (!contains(accel_escape_info, s)) { 
            info.states[s].impl_id = sheng_end++; 
            info.extra[s].sheng_id = info.states[s].impl_id - 1; 
        } 
    } 
 
    /* allocate accel ids */ 
    for (auto v : sheng_states) { 
        dstate_id_t s = g[v].index; 
        if (contains(accel_escape_info, s)) { 
            assert(!info.states[s].impl_id); 
            info.states[s].impl_id = sheng_end++; 
            info.extra[s].sheng_id = info.states[s].impl_id - 1; 
        } 
    } 
 
    if (sheng_states.size() < MIN_SHENG_SIZE) { 
        DEBUG_PRINTF("sheng region too small\n"); 
        return DEAD_STATE; 
    } 
 
    if (!seen_back_edge) { 
        DEBUG_PRINTF("did not include cyclic\n"); 
        return DEAD_STATE; 
    } 
 
    double leak = leakiness(g, info, sheng_states, base_cyclic); 
    if (leak > MAX_SHENG_LEAKINESS) { 
        DEBUG_PRINTF("too leaky (%g)\n", leak); 
        return DEAD_STATE; 
    } 
 
    if (!mark_sheng_succs(g, info, sheng_states)) { 
        return DEAD_STATE; 
    } 
 
    /* TODO: ensure sufficiently 'sticky' */ 
    /* TODO: check not all states accel */ 
    DEBUG_PRINTF("sheng_end = %hu\n", sheng_end); 
    return sheng_end; 
} 
 
static 
void fill_in_aux_info(NFA *nfa, const dfa_info &info, 
                      const map<dstate_id_t, AccelScheme> &accel_escape_info, 
                      u32 accel_offset, UNUSED u32 accel_end_offset, 
                      const vector<u32> &reports, 
                      const vector<u32> &reports_eod, 
                      u32 report_base_offset, 
                      const raw_report_info &ri) { 
    mcsheng *m = (mcsheng *)getMutableImplNfa(nfa); 
 
    vector<u32> reportOffsets; 
 
    ri.fillReportLists(nfa, report_base_offset, reportOffsets); 
 
    for (u32 i = 0; i < info.size(); i++) { 
        u16 impl_id = info.implId(i); 
        mstate_aux *this_aux = getAux(nfa, impl_id); 
 
        fillInAux(this_aux, i, info, reports, reports_eod, reportOffsets); 
        if (contains(accel_escape_info, i)) { 
            this_aux->accel_offset = accel_offset; 
            accel_offset += info.strat.accelSize(); 
            assert(accel_offset <= accel_end_offset); 
            assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); 
            info.strat.buildAccel(i, accel_escape_info.at(i), 
                                  (void *)((char *)m + this_aux->accel_offset)); 
        } 
    } 
} 
 
static 
u16 get_edge_flags(NFA *nfa, dstate_id_t target_impl_id) { 
    mstate_aux *aux = getAux(nfa, target_impl_id); 
    u16 flags = 0; 
 
    if (aux->accept) { 
        flags |= ACCEPT_FLAG; 
    } 
 
    if (aux->accel_offset) { 
        flags |= ACCEL_FLAG; 
    } 
 
    return flags; 
} 
 
static 
void fill_in_succ_table_16(NFA *nfa, const dfa_info &info, 
                           dstate_id_t sheng_end, 
                           UNUSED dstate_id_t sherman_base) { 
    u16 *succ_table = (u16 *)((char *)nfa + sizeof(NFA) + sizeof(mcsheng)); 
 
    u8 alphaShift = info.getAlphaShift(); 
    assert(alphaShift <= 8); 
 
    for (size_t i = 0; i < info.size(); i++) { 
        if (!info.is_normal(i)) { 
            assert(info.implId(i) < sheng_end || info.is_sherman(i)); 
            continue; 
        } 
 
        assert(info.implId(i) < sherman_base); 
        u16 normal_id = verify_u16(info.implId(i) - sheng_end); 
 
        for (size_t s = 0; s < info.impl_alpha_size; s++) { 
            dstate_id_t raw_succ = info.states[i].next[s]; 
            u16 &entry = succ_table[((size_t)normal_id << alphaShift) + s]; 
 
            entry = info.implId(raw_succ); 
            entry |= get_edge_flags(nfa, entry); 
        } 
    } 
} 
 
static
void fill_in_aux_info64(NFA *nfa, const dfa_info &info,
                        const map<dstate_id_t, AccelScheme> &accel_escape_info,
                        u32 accel_offset, UNUSED u32 accel_end_offset,
                        const vector<u32> &reports,
                        const vector<u32> &reports_eod,
                        u32 report_base_offset,
                        const raw_report_info &ri) {
    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(nfa);

    vector<u32> reportOffsets;

    ri.fillReportLists(nfa, report_base_offset, reportOffsets);

    for (u32 i = 0; i < info.size(); i++) {
        u16 impl_id = info.implId(i);
        mstate_aux *this_aux = getAux64(nfa, impl_id);

        fillInAux(this_aux, i, info, reports, reports_eod, reportOffsets);
        if (contains(accel_escape_info, i)) {
            this_aux->accel_offset = accel_offset;
            accel_offset += info.strat.accelSize();
            assert(accel_offset <= accel_end_offset);
            assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));
            info.strat.buildAccel(i, accel_escape_info.at(i),
                                  (void *)((char *)m + this_aux->accel_offset));
        }
    }
}

static
u16 get_edge_flags64(NFA *nfa, dstate_id_t target_impl_id) {
    mstate_aux *aux = getAux64(nfa, target_impl_id);
    u16 flags = 0;

    if (aux->accept) {
        flags |= ACCEPT_FLAG;
    }

    if (aux->accel_offset) {
        flags |= ACCEL_FLAG;
    }

    return flags;
}

static
void fill_in_succ_table_64_16(NFA *nfa, const dfa_info &info,
                              dstate_id_t sheng_end,
                              UNUSED dstate_id_t sherman_base) {
    u16 *succ_table = (u16 *)((char *)nfa + sizeof(NFA) + sizeof(mcsheng64));

    u8 alphaShift = info.getAlphaShift();
    assert(alphaShift <= 8);

    for (size_t i = 0; i < info.size(); i++) {
        if (!info.is_normal(i)) {
            assert(info.implId(i) < sheng_end || info.is_sherman(i));
            continue;
        }

        assert(info.implId(i) < sherman_base);
        u16 normal_id = verify_u16(info.implId(i) - sheng_end);

        for (size_t s = 0; s < info.impl_alpha_size; s++) {
            dstate_id_t raw_succ = info.states[i].next[s];
            u16 &entry = succ_table[((size_t)normal_id << alphaShift) + s];

            entry = info.implId(raw_succ);
            entry |= get_edge_flags64(nfa, entry);
        }
    }
}

#define MAX_SHERMAN_LIST_LEN 8 
 
static 
void addIfEarlier(flat_set<dstate_id_t> &dest, dstate_id_t candidate, 
                  dstate_id_t max) { 
    if (candidate < max) { 
        dest.insert(candidate); 
    } 
} 
 
static 
void addSuccessors(flat_set<dstate_id_t> &dest, const dstate &source, 
                   u16 alphasize, dstate_id_t curr_id) { 
    for (symbol_t s = 0; s < alphasize; s++) { 
        addIfEarlier(dest, source.next[s], curr_id); 
    } 
} 
 
/* \brief Returns a set of states to search for a better daddy. */ 
static 
flat_set<dstate_id_t> find_daddy_candidates(const dfa_info &info, 
                                            dstate_id_t curr_id) { 
    flat_set<dstate_id_t> hinted; 
 
    addIfEarlier(hinted, 0, curr_id); 
    addIfEarlier(hinted, info.raw.start_anchored, curr_id); 
    addIfEarlier(hinted, info.raw.start_floating, curr_id); 
 
    // Add existing daddy and his successors, then search back one generation. 
    const u16 alphasize = info.impl_alpha_size; 
    dstate_id_t daddy = info.states[curr_id].daddy; 
    for (u32 level = 0; daddy && level < 2; level++) { 
        addIfEarlier(hinted, daddy, curr_id); 
        addSuccessors(hinted, info.states[daddy], alphasize, curr_id); 
        daddy = info.states[daddy].daddy; 
    } 
 
    return hinted; 
} 
 
#define MAX_SHERMAN_SELF_LOOP 20 
 
static 
void find_better_daddy(dfa_info &info, dstate_id_t curr_id, 
                       bool any_cyclic_near_anchored_state, const Grey &grey) { 
    if (!grey.allowShermanStates) { 
        return; 
    } 
 
    const u16 width = sizeof(u16); 
    const u16 alphasize = info.impl_alpha_size; 
 
    if (info.raw.start_anchored != DEAD_STATE 
        && any_cyclic_near_anchored_state 
        && curr_id < alphasize * 3) { 
        /* crude attempt to prevent frequent states from being sherman'ed 
         * depends on the fact that states are numbers are currently in bfs 
         * order */ 
        DEBUG_PRINTF("%hu is banned\n", curr_id); 
        return; 
    } 
 
    if (info.raw.start_floating != DEAD_STATE 
        && curr_id >= info.raw.start_floating 
        && curr_id < info.raw.start_floating + alphasize * 3) { 
        /* crude attempt to prevent frequent states from being sherman'ed 
         * depends on the fact that states are numbers are currently in bfs 
         * order */ 
        DEBUG_PRINTF("%hu is banned (%hu)\n", curr_id, info.raw.start_floating); 
        return; 
    } 
 
    const u16 full_state_size = width * alphasize; 
    const u16 max_list_len = MIN(MAX_SHERMAN_LIST_LEN, 
                           (full_state_size - 2)/(width + 1)); 
    u16 best_score = 0; 
    dstate_id_t best_daddy = 0; 
    dstate &currState = info.states[curr_id]; 
 
    flat_set<dstate_id_t> hinted = find_daddy_candidates(info, curr_id); 
 
    for (const dstate_id_t &donor : hinted) { 
        assert(donor < curr_id); 
        u32 score = 0; 
 
        if (!info.is_normal(donor)) { 
            continue; 
        } 
 
        const dstate &donorState = info.states[donor]; 
        for (symbol_t s = 0; s < alphasize; s++) { 
            if (currState.next[s] == donorState.next[s]) { 
                score++; 
            } 
        } 
 
        /* prefer lower ids to provide some stability amongst potential 
         * siblings */ 
        if (score > best_score || (score == best_score && donor < best_daddy)) { 
            best_daddy = donor; 
            best_score = score; 
 
            if (score == alphasize) { 
                break; 
            } 
        } 
    } 
 
    currState.daddy = best_daddy; 
    info.extra[curr_id].daddytaken = best_score; 
    DEBUG_PRINTF("%hu -> daddy %hu: %u/%u BF\n", curr_id, best_daddy, 
                 best_score, alphasize); 
 
    if (best_daddy == DEAD_STATE) { 
        return; /* No good daddy */ 
    } 
 
    if (best_score + max_list_len < alphasize) { 
        return; /* ??? */ 
    } 
 
    assert(info.is_normal(currState.daddy)); 
 
    u32 self_loop_width = 0; 
    const dstate &curr_raw = info.states[curr_id]; 
    for (unsigned i = 0; i < N_CHARS; i++) { 
        if (curr_raw.next[info.alpha_remap[i]] == curr_id) { 
            self_loop_width++; 
        } 
    } 
 
    if (self_loop_width > MAX_SHERMAN_SELF_LOOP) { 
        DEBUG_PRINTF("%hu is banned wide self loop (%u)\n", curr_id, 
                      self_loop_width); 
        return; 
    } 
 
    if (info.is_sheng(curr_id)) { 
        return; 
    } 
 
    DEBUG_PRINTF("%hu is sherman\n", curr_id); 
    info.extra[curr_id].shermanState = true; 
} 
 
static 
bool is_cyclic_near(const raw_dfa &raw, dstate_id_t root) { 
    symbol_t alphasize = raw.getImplAlphaSize(); 
    for (symbol_t s = 0; s < alphasize; s++) { 
        dstate_id_t succ_id = raw.states[root].next[s]; 
        if (succ_id == DEAD_STATE) { 
            continue; 
        } 
 
        const dstate &succ = raw.states[succ_id]; 
        for (symbol_t t = 0; t < alphasize; t++) { 
            if (succ.next[t] == root || succ.next[t] == succ_id) { 
                return true; 
            } 
        } 
    } 
    return false; 
} 
 
static 
void fill_in_sherman(NFA *nfa, dfa_info &info, UNUSED u16 sherman_limit) { 
    char *nfa_base = (char *)nfa; 
    mcsheng *m = (mcsheng *)getMutableImplNfa(nfa); 
    char *sherman_table = nfa_base + m->sherman_offset; 
 
    assert(ISALIGNED_16(sherman_table)); 
    for (size_t i = 0; i < info.size(); i++) { 
        if (!info.is_sherman(i)) { 
            continue; 
        } 
        u16 fs = verify_u16(info.implId(i)); 
        DEBUG_PRINTF("building sherman %zu impl %hu\n", i, fs); 
 
        assert(fs >= sherman_limit); 
 
        char *curr_sherman_entry 
            = sherman_table + (fs - m->sherman_limit) * SHERMAN_FIXED_SIZE; 
        assert(curr_sherman_entry <= nfa_base + m->length); 
 
        u8 len = verify_u8(info.impl_alpha_size - info.extra[i].daddytaken); 
        assert(len <= 9); 
        dstate_id_t d = info.states[i].daddy; 
 
        *(u8 *)(curr_sherman_entry + SHERMAN_TYPE_OFFSET) = SHERMAN_STATE; 
        *(u8 *)(curr_sherman_entry + SHERMAN_LEN_OFFSET) = len; 
        *(u16 *)(curr_sherman_entry + SHERMAN_DADDY_OFFSET) = info.implId(d); 
        u8 *chars = (u8 *)(curr_sherman_entry + SHERMAN_CHARS_OFFSET); 
 
        for (u16 s = 0; s < info.impl_alpha_size; s++) { 
            if (info.states[i].next[s] != info.states[d].next[s]) { 
                *(chars++) = (u8)s; 
            } 
        } 
 
        u16 *states = (u16 *)(curr_sherman_entry + SHERMAN_STATES_OFFSET(len)); 
        for (u16 s = 0; s < info.impl_alpha_size; s++) { 
            if (info.states[i].next[s] != info.states[d].next[s]) { 
                DEBUG_PRINTF("s overrider %hu dad %hu char next %hu\n", fs, 
                             info.implId(d), 
                             info.implId(info.states[i].next[s])); 
                u16 entry_val = info.implId(info.states[i].next[s]); 
                entry_val |= get_edge_flags(nfa, entry_val); 
                unaligned_store_u16((u8 *)states++, entry_val); 
            } 
        } 
    } 
} 
 
static 
bytecode_ptr<NFA> mcshengCompile16(dfa_info &info, dstate_id_t sheng_end, 
                        const map<dstate_id_t, AccelScheme> &accel_escape_info, 
                        const Grey &grey) { 
    DEBUG_PRINTF("building mcsheng 16\n"); 
 
    vector<u32> reports; /* index in ri for the appropriate report list */ 
    vector<u32> reports_eod; /* as above */ 
    ReportID arb; 
    u8 single; 
 
    assert(info.getAlphaShift() <= 8); 
 
    // Sherman optimization
    if (info.impl_alpha_size > 16) {
        u16 total_daddy = 0;
        for (u32 i = 0; i < info.size(); i++) {
            find_better_daddy(info, i,
                              is_cyclic_near(info.raw, info.raw.start_anchored),
                              grey);
            total_daddy += info.extra[i].daddytaken;
        }

        DEBUG_PRINTF("daddy %hu/%zu states=%zu alpha=%hu\n", total_daddy,
                     info.size() * info.impl_alpha_size, info.size(),
                     info.impl_alpha_size);
    } 
 
    u16 sherman_limit; 
    if (!allocateImplId16(info, sheng_end, &sherman_limit)) { 
        DEBUG_PRINTF("failed to allocate state numbers, %zu states total\n", 
                     info.size()); 
        return nullptr; 
    } 
    u16 count_real_states = sherman_limit - sheng_end; 
 
    auto ri = info.strat.gatherReports(reports, reports_eod, &single, &arb); 
 
    size_t tran_size = (1 << info.getAlphaShift()) * sizeof(u16) 
                     * count_real_states; 
 
    size_t aux_size = sizeof(mstate_aux) * info.size(); 
 
    size_t aux_offset = ROUNDUP_16(sizeof(NFA) + sizeof(mcsheng) + tran_size); 
    size_t accel_size = info.strat.accelSize() * accel_escape_info.size(); 
    size_t accel_offset = ROUNDUP_N(aux_offset + aux_size 
                                    + ri->getReportListSize(), 32); 
    size_t sherman_offset = ROUNDUP_16(accel_offset + accel_size); 
    size_t sherman_size = calcShermanRegionSize(info); 
 
    size_t total_size = sherman_offset + sherman_size; 
 
    accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ 
    assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); 
 
    auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size); 
    mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get()); 
 
    populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset, 
                      accel_escape_info.size(), arb, single, nfa.get()); 
    createShuffleMasks(m, info, sheng_end, accel_escape_info); 
 
    /* copy in the mc header information */ 
    m->sherman_offset = sherman_offset; 
    m->sherman_end = total_size; 
    m->sherman_limit = sherman_limit; 
 
    DEBUG_PRINTF("%hu sheng, %hu norm, %zu total\n", sheng_end, 
                 count_real_states, info.size()); 
 
    fill_in_aux_info(nfa.get(), info, accel_escape_info, accel_offset, 
                     sherman_offset - sizeof(NFA), reports, reports_eod, 
                     aux_offset + aux_size, *ri); 
 
    fill_in_succ_table_16(nfa.get(), info, sheng_end, sherman_limit); 
 
    fill_in_sherman(nfa.get(), info, sherman_limit); 
 
    return nfa; 
} 
 
static 
void fill_in_succ_table_8(NFA *nfa, const dfa_info &info, 
                          dstate_id_t sheng_end) { 
    u8 *succ_table = (u8 *)nfa + sizeof(NFA) + sizeof(mcsheng); 
 
    u8 alphaShift = info.getAlphaShift(); 
    assert(alphaShift <= 8); 
 
    for (size_t i = 0; i < info.size(); i++) { 
        assert(!info.is_sherman(i)); 
        if (!info.is_normal(i)) { 
            assert(info.implId(i) < sheng_end); 
            continue; 
        } 
        u8 normal_id = verify_u8(info.implId(i) - sheng_end); 
 
        for (size_t s = 0; s < info.impl_alpha_size; s++) { 
            dstate_id_t raw_succ = info.states[i].next[s]; 
            succ_table[((size_t)normal_id << alphaShift) + s] 
                = info.implId(raw_succ); 
        } 
    } 
} 
 
static 
void fill_in_sherman64(NFA *nfa, dfa_info &info, UNUSED u16 sherman_limit) {
    char *nfa_base = (char *)nfa;
    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(nfa);
    char *sherman_table = nfa_base + m->sherman_offset;

    assert(ISALIGNED_16(sherman_table));
    for (size_t i = 0; i < info.size(); i++) {
        if (!info.is_sherman(i)) {
            continue;
        }
        u16 fs = verify_u16(info.implId(i));
        DEBUG_PRINTF("building sherman %zu impl %hu\n", i, fs);

        assert(fs >= sherman_limit);

        char *curr_sherman_entry
            = sherman_table + (fs - m->sherman_limit) * SHERMAN_FIXED_SIZE;
        assert(curr_sherman_entry <= nfa_base + m->length);

        u8 len = verify_u8(info.impl_alpha_size - info.extra[i].daddytaken);
        assert(len <= 9);
        dstate_id_t d = info.states[i].daddy;

        *(u8 *)(curr_sherman_entry + SHERMAN_TYPE_OFFSET) = SHERMAN_STATE;
        *(u8 *)(curr_sherman_entry + SHERMAN_LEN_OFFSET) = len;
        *(u16 *)(curr_sherman_entry + SHERMAN_DADDY_OFFSET) = info.implId(d);
        u8 *chars = (u8 *)(curr_sherman_entry + SHERMAN_CHARS_OFFSET);

        for (u16 s = 0; s < info.impl_alpha_size; s++) {
            if (info.states[i].next[s] != info.states[d].next[s]) {
                *(chars++) = (u8)s;
            }
        }

        u16 *states = (u16 *)(curr_sherman_entry + SHERMAN_STATES_OFFSET(len));
        for (u16 s = 0; s < info.impl_alpha_size; s++) {
            if (info.states[i].next[s] != info.states[d].next[s]) {
                DEBUG_PRINTF("s overrider %hu dad %hu char next %hu\n", fs,
                             info.implId(d),
                             info.implId(info.states[i].next[s]));
                u16 entry_val = info.implId(info.states[i].next[s]);
                entry_val |= get_edge_flags64(nfa, entry_val);
                unaligned_store_u16((u8 *)states++, entry_val);
            }
        }
    }
}

static
bytecode_ptr<NFA> mcsheng64Compile16(dfa_info&info, dstate_id_t sheng_end,
                         const map<dstate_id_t, AccelScheme>&accel_escape_info,
                         const Grey &grey) {
    DEBUG_PRINTF("building mcsheng 64-16\n");

    vector<u32> reports; /* index in ri for the appropriate report list */
    vector<u32> reports_eod; /* as above */
    ReportID arb;
    u8 single;

    assert(info.getAlphaShift() <= 8);

    // Sherman optimization
    if (info.impl_alpha_size > 16) {
        u16 total_daddy = 0;
        for (u32 i = 0; i < info.size(); i++) {
            find_better_daddy(info, i,
                              is_cyclic_near(info.raw, info.raw.start_anchored),
                              grey);
            total_daddy += info.extra[i].daddytaken;
        }

        DEBUG_PRINTF("daddy %hu/%zu states=%zu alpha=%hu\n", total_daddy,
                     info.size() * info.impl_alpha_size, info.size(),
                     info.impl_alpha_size);
    }

    u16 sherman_limit;
    if (!allocateImplId16(info, sheng_end, &sherman_limit)) {
        DEBUG_PRINTF("failed to allocate state numbers, %zu states total\n",
                     info.size());
        return nullptr;
    }
    u16 count_real_states = sherman_limit - sheng_end;

    auto ri = info.strat.gatherReports(reports, reports_eod, &single, &arb);

    size_t tran_size = (1 << info.getAlphaShift()) * sizeof(u16)
                     * count_real_states;

    size_t aux_size = sizeof(mstate_aux) * info.size();

    size_t aux_offset = ROUNDUP_16(sizeof(NFA) + sizeof(mcsheng64) + tran_size);
    size_t accel_size = info.strat.accelSize() * accel_escape_info.size();
    size_t accel_offset = ROUNDUP_N(aux_offset + aux_size
                                    + ri->getReportListSize(), 32);
    size_t sherman_offset = ROUNDUP_16(accel_offset + accel_size);
    size_t sherman_size = calcShermanRegionSize(info);

    size_t total_size = sherman_offset + sherman_size;

    accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
    assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));

    auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(nfa.get());

    populateBasicInfo64(sizeof(u16), info, total_size, aux_offset, accel_offset,
                        accel_escape_info.size(), arb, single, nfa.get());
    createShuffleMasks64(m, info, sheng_end, accel_escape_info);

    /* copy in the mc header information */
    m->sherman_offset = sherman_offset;
    m->sherman_end = total_size;
    m->sherman_limit = sherman_limit;

    DEBUG_PRINTF("%hu sheng, %hu norm, %zu total\n", sheng_end,
                 count_real_states, info.size());

    fill_in_aux_info64(nfa.get(), info, accel_escape_info, accel_offset,
                       sherman_offset - sizeof(NFA), reports, reports_eod,
                       aux_offset + aux_size, *ri);

    fill_in_succ_table_64_16(nfa.get(), info, sheng_end, sherman_limit);

    fill_in_sherman64(nfa.get(), info, sherman_limit);

    return nfa;
}

static
void fill_in_succ_table_64_8(NFA *nfa, const dfa_info &info,
                             dstate_id_t sheng_end) {
    u8 *succ_table = (u8 *)nfa + sizeof(NFA) + sizeof(mcsheng64);

    u8 alphaShift = info.getAlphaShift();
    assert(alphaShift <= 8);

    for (size_t i = 0; i < info.size(); i++) {
        assert(!info.is_sherman(i));
        if (!info.is_normal(i)) {
            assert(info.implId(i) < sheng_end);
            continue;
        }
        u8 normal_id = verify_u8(info.implId(i) - sheng_end);

        for (size_t s = 0; s < info.impl_alpha_size; s++) {
            dstate_id_t raw_succ = info.states[i].next[s];
            succ_table[((size_t)normal_id << alphaShift) + s]
                = info.implId(raw_succ);
        }
    }
}

static
void allocateImplId8(dfa_info &info, dstate_id_t sheng_end, 
                     const map<dstate_id_t, AccelScheme> &accel_escape_info, 
                     u16 *accel_limit, u16 *accept_limit) { 
    info.states[0].impl_id = 0; /* dead is always 0 */ 
 
    vector<dstate_id_t> norm; 
    vector<dstate_id_t> accel; 
    vector<dstate_id_t> accept; 
 
    assert(info.size() <= (1 << 8)); 
 
    for (u32 i = 1; i < info.size(); i++) { 
        if (info.is_sheng(i)) { 
            continue; /* already allocated */ 
        } else if (!info.states[i].reports.empty()) { 
            accept.push_back(i); 
        } else if (contains(accel_escape_info, i)) { 
            accel.push_back(i); 
        } else { 
            norm.push_back(i); 
        } 
    } 
 
    u32 j = sheng_end; 
    for (const dstate_id_t &s : norm) { 
        assert(j <= 256); 
        DEBUG_PRINTF("mapping state %u to %u\n", s, j); 
        info.states[s].impl_id = j++; 
    } 
    *accel_limit = j; 
    for (const dstate_id_t &s : accel) { 
        assert(j <= 256); 
        DEBUG_PRINTF("mapping state %u to %u\n", s, j); 
        info.states[s].impl_id = j++; 
    } 
    *accept_limit = j; 
    for (const dstate_id_t &s : accept) { 
        assert(j <= 256); 
        DEBUG_PRINTF("mapping state %u to %u\n",  s, j); 
        info.states[s].impl_id = j++; 
    } 
} 
 
static 
bytecode_ptr<NFA> mcshengCompile8(dfa_info &info, dstate_id_t sheng_end, 
                       const map<dstate_id_t, AccelScheme> &accel_escape_info) { 
    DEBUG_PRINTF("building mcsheng 8\n"); 
 
    vector<u32> reports; 
    vector<u32> reports_eod; 
    ReportID arb; 
    u8 single; 
 
    auto ri = info.strat.gatherReports(reports, reports_eod, &single, &arb); 
 
    size_t normal_count = info.size() - sheng_end; 
 
    size_t tran_size = sizeof(u8) * (1 << info.getAlphaShift()) * normal_count; 
    size_t aux_size = sizeof(mstate_aux) * info.size(); 
    size_t aux_offset = ROUNDUP_16(sizeof(NFA) + sizeof(mcsheng) + tran_size); 
    size_t accel_size = info.strat.accelSize() * accel_escape_info.size(); 
    size_t accel_offset = ROUNDUP_N(aux_offset + aux_size 
                                     + ri->getReportListSize(), 32); 
    size_t total_size = accel_offset + accel_size; 
 
    DEBUG_PRINTF("aux_size %zu\n", aux_size); 
    DEBUG_PRINTF("aux_offset %zu\n", aux_offset); 
    DEBUG_PRINTF("rl size %u\n", ri->getReportListSize()); 
    DEBUG_PRINTF("accel_size %zu\n", accel_size); 
    DEBUG_PRINTF("accel_offset %zu\n", accel_offset); 
    DEBUG_PRINTF("total_size %zu\n", total_size); 
 
    accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */ 
    assert(ISALIGNED_N(accel_offset, alignof(union AccelAux))); 
 
    auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size); 
    mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get()); 
 
    allocateImplId8(info, sheng_end, accel_escape_info, &m->accel_limit_8, 
                    &m->accept_limit_8); 
 
    populateBasicInfo(sizeof(u8), info, total_size, aux_offset, accel_offset, 
                      accel_escape_info.size(), arb, single, nfa.get()); 
    createShuffleMasks(m, info, sheng_end, accel_escape_info); 
 
    fill_in_aux_info(nfa.get(), info, accel_escape_info, accel_offset, 
                     total_size - sizeof(NFA), reports, reports_eod, 
                     aux_offset + aux_size, *ri); 
 
    fill_in_succ_table_8(nfa.get(), info, sheng_end); 
 
    DEBUG_PRINTF("rl size %zu\n", ri->size()); 
 
    return nfa; 
} 
 
static
bytecode_ptr<NFA> mcsheng64Compile8(dfa_info &info, dstate_id_t sheng_end,
                      const map<dstate_id_t, AccelScheme> &accel_escape_info) {
    DEBUG_PRINTF("building mcsheng 64-8\n");

    vector<u32> reports;
    vector<u32> reports_eod;
    ReportID arb;
    u8 single;

    auto ri = info.strat.gatherReports(reports, reports_eod, &single, &arb);

    size_t normal_count = info.size() - sheng_end;

    size_t tran_size = sizeof(u8) * (1 << info.getAlphaShift()) * normal_count;
    size_t aux_size = sizeof(mstate_aux) * info.size();
    size_t aux_offset = ROUNDUP_16(sizeof(NFA) + sizeof(mcsheng64) + tran_size);
    size_t accel_size = info.strat.accelSize() * accel_escape_info.size();
    size_t accel_offset = ROUNDUP_N(aux_offset + aux_size
                                    + ri->getReportListSize(), 32);
    size_t total_size = accel_offset + accel_size;

    DEBUG_PRINTF("aux_size %zu\n", aux_size);
    DEBUG_PRINTF("aux_offset %zu\n", aux_offset);
    DEBUG_PRINTF("rl size %u\n", ri->getReportListSize());
    DEBUG_PRINTF("accel_size %zu\n", accel_size);
    DEBUG_PRINTF("accel_offset %zu\n", accel_offset);
    DEBUG_PRINTF("total_size %zu\n", total_size);

    accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
    assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));

    auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
    mcsheng64 *m = (mcsheng64 *)getMutableImplNfa(nfa.get());

    allocateImplId8(info, sheng_end, accel_escape_info, &m->accel_limit_8,
                    &m->accept_limit_8);

    populateBasicInfo64(sizeof(u8), info, total_size, aux_offset, accel_offset,
                        accel_escape_info.size(), arb, single, nfa.get());
    createShuffleMasks64(m, info, sheng_end, accel_escape_info);

    fill_in_aux_info64(nfa.get(), info, accel_escape_info, accel_offset,
                       total_size - sizeof(NFA), reports, reports_eod,
                       aux_offset + aux_size, *ri);

    fill_in_succ_table_64_8(nfa.get(), info, sheng_end);
    DEBUG_PRINTF("rl size %zu\n", ri->size());

    return nfa;
}

bytecode_ptr<NFA> mcshengCompile(raw_dfa &raw, const CompileContext &cc, 
                                 const ReportManager &rm) { 
    if (!cc.grey.allowMcSheng) { 
        return nullptr; 
    } 
 
    mcclellan_build_strat mbs(raw, rm, false); 
    dfa_info info(mbs); 
    bool using8bit = cc.grey.allowMcClellan8 && info.size() <= 256; 
 
    if (!cc.streaming) { /* TODO: work out if we can do the strip in streaming 
                          * mode with our semantics */ 
        raw.stripExtraEodReports(); 
    } 
 
    bool has_eod_reports = raw.hasEodReports(); 
 
    map<dstate_id_t, AccelScheme> accel_escape_info 
        = info.strat.getAccelInfo(cc.grey); 
    auto old_states = info.states;
    dstate_id_t sheng_end = find_sheng_states(info, accel_escape_info, MAX_SHENG_STATES);
 
    if (sheng_end <= DEAD_STATE + 1) { 
        info.states = old_states;
        return nullptr; 
    } 
 
    bytecode_ptr<NFA> nfa; 

    if (!using8bit) { 
        nfa = mcshengCompile16(info, sheng_end, accel_escape_info, cc.grey); 
    } else { 
        nfa = mcshengCompile8(info, sheng_end, accel_escape_info); 
    } 
 
    if (!nfa) { 
        info.states = old_states;
        return nfa; 
    } 
 
    if (has_eod_reports) { 
        nfa->flags |= NFA_ACCEPTS_EOD; 
    } 
 
    DEBUG_PRINTF("compile done\n"); 
    return nfa; 
} 
 
bytecode_ptr<NFA> mcshengCompile64(raw_dfa &raw, const CompileContext &cc,
                                   const ReportManager &rm) {
    if (!cc.grey.allowMcSheng) {
        return nullptr;
    }

    if (!cc.target_info.has_avx512vbmi()) {
        DEBUG_PRINTF("McSheng64 failed, no HS_CPU_FEATURES_AVX512VBMI!\n");
        return nullptr;
    }

    mcclellan_build_strat mbs(raw, rm, false);
    dfa_info info(mbs);
    bool using8bit = cc.grey.allowMcClellan8 && info.size() <= 256;

    if (!cc.streaming) { /* TODO: work out if we can do the strip in streaming
                          * mode with our semantics */
        raw.stripExtraEodReports();
    }

    bool has_eod_reports = raw.hasEodReports();

    map<dstate_id_t, AccelScheme> accel_escape_info
        = info.strat.getAccelInfo(cc.grey);
    bool using64state = false; /*default flag*/
    dstate_id_t sheng_end64;
    sheng_end64 = find_sheng_states(info, accel_escape_info, MAX_SHENG64_STATES);

    if (sheng_end64 <= DEAD_STATE + 1) {
        return nullptr;
    } else {
        using64state = true;
    }

    bytecode_ptr<NFA> nfa;

    if (using64state) {
        assert((sheng_end64 > 17) && (sheng_end64 <= 65));
        if (!using8bit) {
            nfa = mcsheng64Compile16(info, sheng_end64, accel_escape_info, cc.grey);
        } else {
            assert(using8bit);
            nfa = mcsheng64Compile8(info, sheng_end64, accel_escape_info);
            assert(nfa);
            assert(nfa->type == MCSHENG_64_NFA_8);
        }
    }

    if (!nfa) {
        return nfa;
    }

    if (has_eod_reports) {
        nfa->flags |= NFA_ACCEPTS_EOD;
    }

    DEBUG_PRINTF("compile done\n");
    return nfa;
}

bool has_accel_mcsheng(const NFA *) { 
    return true; /* consider the sheng region as accelerated */ 
} 
 
} // namespace ue2