aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/nfa/nfa_build_util.cpp
blob: 293a84f8ac0fbde3e264b13a3c95a084cc75a9c0 (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
/*
 * Copyright (c) 2015-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 "nfa_build_util.h"

#include "limex_internal.h"
#include "mcclellancompile.h"
#include "mcsheng_compile.h" 
#include "shengcompile.h" 
#include "nfa_internal.h"
#include "repeat_internal.h"
#include "ue2common.h"

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>

using namespace std;

namespace ue2 {

namespace {

template<NFAEngineType t> struct NFATraits { };

template<template<NFAEngineType t> class sfunc, typename rv_t, typename arg_t,
         NFAEngineType lb>
struct DISPATCH_BY_NFA_TYPE_INT {
    static rv_t doOp(NFAEngineType i, const arg_t &arg) {
        if (i == lb) {
            return sfunc<lb>::call(arg);
        } else {
            return DISPATCH_BY_NFA_TYPE_INT<sfunc, rv_t, arg_t,
                                            (NFAEngineType)(lb + 1)>
                     ::doOp(i, arg);
        }
    }
};

template<template<NFAEngineType t> class sfunc, typename rv_t, typename arg_t>
struct DISPATCH_BY_NFA_TYPE_INT<sfunc, rv_t, arg_t, INVALID_NFA> {
    // dummy
    static rv_t doOp(NFAEngineType, const arg_t &) {
        assert(0);
        throw std::logic_error("Unreachable");
    }
};

#define DISPATCH_BY_NFA_TYPE(i, op, arg)                                       \
    DISPATCH_BY_NFA_TYPE_INT<op, decltype(op<(NFAEngineType)0>::call(arg)),    \
                             decltype(arg), (NFAEngineType)0>::doOp(i, arg)
}

typedef bool (*nfa_dispatch_fn)(const NFA *nfa); 

template<typename T>
static
bool has_accel_limex(const NFA *nfa) {
    const T *limex = (const T *)getImplNfa(nfa);
    return limex->accelCount;
}

template<typename T> 
static
bool has_repeats_limex(const NFA *nfa) { 
    const T *limex = (const T *)getImplNfa(nfa); 
    return limex->repeatCount; 
} 
 
 
template<typename T> 
static 
bool has_repeats_other_than_firsts_limex(const NFA *nfa) { 
    const T *limex = (const T *)getImplNfa(nfa); 
    const char *ptr = (const char *)limex; 
 
    const u32 *repeatOffset = (const u32 *)(ptr + limex->repeatOffset); 
 
    for (u32 i = 0; i < limex->repeatCount; i++) { 
        u32 offset = repeatOffset[i]; 
        const NFARepeatInfo *info = (const NFARepeatInfo *)(ptr + offset); 
        const RepeatInfo *repeat = 
            (const RepeatInfo *)((const char *)info + sizeof(*info)); 
        if (repeat->type != REPEAT_FIRST) { 
            return true; 
        } 
    } 
 
    return false;
}

static 
bool dispatch_false(const NFA *) { 
    return false; 
} 
 
#ifdef DUMP_SUPPORT
namespace {
template<NFAEngineType t>
struct getName {
    static const char *call(void *) {
        return NFATraits<t>::name;
    }
};

// descr helper for LimEx NFAs
template<NFAEngineType t>
static
string getDescriptionLimEx(const NFA *nfa) {
    const typename NFATraits<t>::implNFA_t *limex =
        (const typename NFATraits<t>::implNFA_t *)getImplNfa(nfa);
    ostringstream oss;
    oss << NFATraits<t>::name << "/" << limex->exceptionCount;
    if (limex->repeatCount) {
        oss << " +" << limex->repeatCount << "r";
    }
    return oss.str();
}
}

// generic description: just return the name
namespace {
template<NFAEngineType t>
struct getDescription {
    static string call(const void *) {
        return string(NFATraits<t>::name);
    }
};
}
#endif


/* build-utility Traits */

namespace {
enum NFACategory {NFA_LIMEX, NFA_OTHER};

// Some of our traits we want around in DUMP_SUPPORT mode only.
#if defined(DUMP_SUPPORT)
#define DO_IF_DUMP_SUPPORT(a) a
#else
#define DO_IF_DUMP_SUPPORT(a)
#endif

#define MAKE_LIMEX_TRAITS(mlt_size, mlt_align)                          \ 
    template<> struct NFATraits<LIMEX_NFA_##mlt_size> {                 \ 
        static UNUSED const char *name;                                 \
        static const NFACategory category = NFA_LIMEX;                  \
        typedef LimExNFA##mlt_size implNFA_t;                           \
        static const nfa_dispatch_fn has_accel;                         \ 
        static const nfa_dispatch_fn has_repeats;                       \ 
        static const nfa_dispatch_fn has_repeats_other_than_firsts;     \ 
        static const u32 stateAlign =                                   \
                MAX(mlt_align, alignof(RepeatControl));                 \ 
    };                                                                  \
    const nfa_dispatch_fn NFATraits<LIMEX_NFA_##mlt_size>::has_accel    \ 
            = has_accel_limex<LimExNFA##mlt_size>;                      \
    const nfa_dispatch_fn NFATraits<LIMEX_NFA_##mlt_size>::has_repeats  \ 
            = has_repeats_limex<LimExNFA##mlt_size>;                    \ 
    const nfa_dispatch_fn                                               \ 
        NFATraits<LIMEX_NFA_##mlt_size>::has_repeats_other_than_firsts  \ 
            = has_repeats_other_than_firsts_limex<LimExNFA##mlt_size>;  \ 
    DO_IF_DUMP_SUPPORT(                                                 \
    const char *NFATraits<LIMEX_NFA_##mlt_size>::name                   \ 
        = "LimEx "#mlt_size;                                            \ 
    template<> struct getDescription<LIMEX_NFA_##mlt_size> {            \ 
        static string call(const void *p) {                             \ 
            return getDescriptionLimEx<LIMEX_NFA_##mlt_size>((const NFA *)p); \ 
        }                                                               \ 
    };)

MAKE_LIMEX_TRAITS(32,  alignof(u32)) 
MAKE_LIMEX_TRAITS(64,  alignof(m128)) /* special, 32bit arch uses m128 */ 
MAKE_LIMEX_TRAITS(128, alignof(m128)) 
MAKE_LIMEX_TRAITS(256, alignof(m256)) 
MAKE_LIMEX_TRAITS(384, alignof(m384)) 
MAKE_LIMEX_TRAITS(512, alignof(m512)) 

template<> struct NFATraits<MCCLELLAN_NFA_8> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 1;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_8>::has_accel = has_accel_mcclellan; 
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_8>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_8>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<MCCLELLAN_NFA_8>::name = "McClellan 8";
#endif

template<> struct NFATraits<MCCLELLAN_NFA_16> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 2;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_16>::has_accel = has_accel_mcclellan; 
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_16>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<MCCLELLAN_NFA_16>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<MCCLELLAN_NFA_16>::name = "McClellan 16";
#endif

template<> struct NFATraits<GOUGH_NFA_8> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<GOUGH_NFA_8>::has_accel = has_accel_mcclellan; 
const nfa_dispatch_fn NFATraits<GOUGH_NFA_8>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<GOUGH_NFA_8>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<GOUGH_NFA_8>::name = "Goughfish 8";
#endif

template<> struct NFATraits<GOUGH_NFA_16> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<GOUGH_NFA_16>::has_accel = has_accel_mcclellan; 
const nfa_dispatch_fn NFATraits<GOUGH_NFA_16>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<GOUGH_NFA_16>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<GOUGH_NFA_16>::name = "Goughfish 16";
#endif

template<> struct NFATraits<MPV_NFA> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<MPV_NFA>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<MPV_NFA>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<MPV_NFA>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<MPV_NFA>::name = "Mega-Puff-Vac"; 
#endif

template<> struct NFATraits<CASTLE_NFA> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<CASTLE_NFA>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<CASTLE_NFA>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<CASTLE_NFA>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<CASTLE_NFA>::name = "Castle"; 
#endif

template<> struct NFATraits<LBR_NFA_DOT> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<LBR_NFA_DOT>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_DOT>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_DOT>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<LBR_NFA_DOT>::name = "Lim Bounded Repeat (D)"; 
#endif

template<> struct NFATraits<LBR_NFA_VERM> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<LBR_NFA_VERM>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_VERM>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_VERM>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<LBR_NFA_VERM>::name = "Lim Bounded Repeat (V)"; 
#endif

template<> struct NFATraits<LBR_NFA_NVERM> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<LBR_NFA_NVERM>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_NVERM>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_NVERM>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<LBR_NFA_NVERM>::name = "Lim Bounded Repeat (NV)"; 
#endif

template<> struct NFATraits<LBR_NFA_SHUF> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<LBR_NFA_SHUF>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_SHUF>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_SHUF>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<LBR_NFA_SHUF>::name = "Lim Bounded Repeat (S)"; 
#endif

template<> struct NFATraits<LBR_NFA_TRUF> { 
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 8;
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
};
const nfa_dispatch_fn NFATraits<LBR_NFA_TRUF>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_TRUF>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<LBR_NFA_TRUF>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT)
const char *NFATraits<LBR_NFA_TRUF>::name = "Lim Bounded Repeat (M)"; 
#endif

template<> struct NFATraits<SHENG_NFA> { 
    UNUSED static const char *name; 
    static const NFACategory category = NFA_OTHER; 
    static const u32 stateAlign = 1; 
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
}; 
const nfa_dispatch_fn NFATraits<SHENG_NFA>::has_accel = has_accel_sheng; 
const nfa_dispatch_fn NFATraits<SHENG_NFA>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<SHENG_NFA>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT) 
const char *NFATraits<SHENG_NFA>::name = "Sheng"; 
#endif 
 
template<> struct NFATraits<TAMARAMA_NFA> { 
    UNUSED static const char *name; 
    static const NFACategory category = NFA_OTHER; 
    static const u32 stateAlign = 64; 
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
}; 
const nfa_dispatch_fn NFATraits<TAMARAMA_NFA>::has_accel = dispatch_false; 
const nfa_dispatch_fn NFATraits<TAMARAMA_NFA>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<TAMARAMA_NFA>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT) 
const char *NFATraits<TAMARAMA_NFA>::name = "Tamarama"; 
#endif 
 
template<> struct NFATraits<MCSHENG_NFA_8> { 
    UNUSED static const char *name; 
    static const NFACategory category = NFA_OTHER; 
    static const u32 stateAlign = 1; 
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
}; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_8>::has_accel = has_accel_mcsheng; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_8>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_8>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT) 
const char *NFATraits<MCSHENG_NFA_8>::name = "Shengy McShengFace 8"; 
#endif 
 
template<> struct NFATraits<MCSHENG_NFA_16> { 
    UNUSED static const char *name; 
    static const NFACategory category = NFA_OTHER; 
    static const u32 stateAlign = 2; 
    static const nfa_dispatch_fn has_accel; 
    static const nfa_dispatch_fn has_repeats; 
    static const nfa_dispatch_fn has_repeats_other_than_firsts; 
}; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_16>::has_accel = has_accel_mcsheng; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_16>::has_repeats = dispatch_false; 
const nfa_dispatch_fn NFATraits<MCSHENG_NFA_16>::has_repeats_other_than_firsts = dispatch_false; 
#if defined(DUMP_SUPPORT) 
const char *NFATraits<MCSHENG_NFA_16>::name = "Shengy McShengFace 16"; 
#endif 
 
template<> struct NFATraits<SHENG_NFA_32> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 1;
    static const nfa_dispatch_fn has_accel;
    static const nfa_dispatch_fn has_repeats;
    static const nfa_dispatch_fn has_repeats_other_than_firsts;
};
const nfa_dispatch_fn NFATraits<SHENG_NFA_32>::has_accel = has_accel_sheng;
const nfa_dispatch_fn NFATraits<SHENG_NFA_32>::has_repeats = dispatch_false;
const nfa_dispatch_fn NFATraits<SHENG_NFA_32>::has_repeats_other_than_firsts = dispatch_false;
#if defined(DUMP_SUPPORT)
const char *NFATraits<SHENG_NFA_32>::name = "Sheng 32";
#endif

template<> struct NFATraits<SHENG_NFA_64> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 1;
    static const nfa_dispatch_fn has_accel;
    static const nfa_dispatch_fn has_repeats;
    static const nfa_dispatch_fn has_repeats_other_than_firsts;
};
const nfa_dispatch_fn NFATraits<SHENG_NFA_64>::has_accel = has_accel_sheng;
const nfa_dispatch_fn NFATraits<SHENG_NFA_64>::has_repeats = dispatch_false;
const nfa_dispatch_fn NFATraits<SHENG_NFA_64>::has_repeats_other_than_firsts = dispatch_false;
#if defined(DUMP_SUPPORT)
const char *NFATraits<SHENG_NFA_64>::name = "Sheng 64";
#endif

template<> struct NFATraits<MCSHENG_64_NFA_8> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 1;
    static const nfa_dispatch_fn has_accel;
    static const nfa_dispatch_fn has_repeats;
    static const nfa_dispatch_fn has_repeats_other_than_firsts;
};
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_8>::has_accel = has_accel_mcsheng;
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_8>::has_repeats = dispatch_false;
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_8>::has_repeats_other_than_firsts = dispatch_false;
#if defined(DUMP_SUPPORT)
const char *NFATraits<MCSHENG_64_NFA_8>::name = "Shengy64 McShengFace 8";
#endif

template<> struct NFATraits<MCSHENG_64_NFA_16> {
    UNUSED static const char *name;
    static const NFACategory category = NFA_OTHER;
    static const u32 stateAlign = 2;
    static const nfa_dispatch_fn has_accel;
    static const nfa_dispatch_fn has_repeats;
    static const nfa_dispatch_fn has_repeats_other_than_firsts;
};
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_16>::has_accel = has_accel_mcsheng;
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_16>::has_repeats = dispatch_false;
const nfa_dispatch_fn NFATraits<MCSHENG_64_NFA_16>::has_repeats_other_than_firsts = dispatch_false;
#if defined(DUMP_SUPPORT)
const char *NFATraits<MCSHENG_64_NFA_16>::name = "Shengy64 McShengFace 16";
#endif
} // namespace

#if defined(DUMP_SUPPORT)

const char *nfa_type_name(NFAEngineType type) {
    return DISPATCH_BY_NFA_TYPE(type, getName, nullptr);
}

string describe(const NFA &nfa) {
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, getDescription, &nfa);
}

#endif /* DUMP_SUPPORT */

namespace {
template<NFAEngineType t>
struct getStateAlign {
    static u32 call(void *) {
        return NFATraits<t>::stateAlign;
    }
};
}

u32 state_alignment(const NFA &nfa) {
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, getStateAlign, nullptr);
}

namespace {
template<NFAEngineType t>
struct is_limex {
    static bool call(const void *) {
        return NFATraits<t>::category == NFA_LIMEX;
    }
};
}

namespace { 
template<NFAEngineType t> 
struct has_repeats_other_than_firsts_dispatch { 
    static nfa_dispatch_fn call(const void *) { 
        return NFATraits<t>::has_repeats_other_than_firsts; 
    }
}; 
} 

bool has_bounded_repeats_other_than_firsts(const NFA &nfa) { 
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, 
                                has_repeats_other_than_firsts_dispatch, 
                                &nfa)(&nfa); 
} 

namespace { 
template<NFAEngineType t> 
struct has_repeats_dispatch { 
    static nfa_dispatch_fn call(const void *) { 
        return NFATraits<t>::has_repeats; 
    }
}; 
}

bool has_bounded_repeats(const NFA &nfa) {
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, has_repeats_dispatch, 
                                &nfa)(&nfa); 
}

namespace {
template<NFAEngineType t>
struct has_accel_dispatch {
    static nfa_dispatch_fn call(const void *) { 
        return NFATraits<t>::has_accel;
    }
};
}

bool has_accel(const NFA &nfa) {
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, has_accel_dispatch,
                                &nfa)(&nfa); 
}

bool requires_decompress_key(const NFA &nfa) {
    return DISPATCH_BY_NFA_TYPE((NFAEngineType)nfa.type, is_limex, &nfa);
}

} // namespace ue2