1 | /* |
2 | * Copyright (c) 2015-2016, Intel Corporation |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
7 | * * Redistributions of source code must retain the above copyright notice, |
8 | * this list of conditions and the following disclaimer. |
9 | * * Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * * Neither the name of Intel Corporation nor the names of its contributors |
13 | * may be used to endorse or promote products derived from this software |
14 | * without specific prior written permission. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | /** \file |
30 | * \brief LimEx NFA: native GPR runtime implementations. |
31 | */ |
32 | |
33 | //#define DEBUG |
34 | //#define DEBUG_INPUT |
35 | //#define DEBUG_EXCEPTIONS |
36 | |
37 | #include "limex.h" |
38 | |
39 | #include "accel.h" |
40 | #include "limex_internal.h" |
41 | #include "nfa_internal.h" |
42 | #include "ue2common.h" |
43 | #include "util/bitutils.h" |
44 | |
45 | // Common code |
46 | #define STATE_ON_STACK |
47 | #define ESTATE_ON_STACK |
48 | |
49 | #include "limex_runtime.h" |
50 | |
51 | // Other implementation code from X-Macro impl. |
52 | #define SIZE 32 |
53 | #define STATE_T u32 |
54 | #define ENG_STATE_T u32 |
55 | #define LOAD_FROM_ENG load_u32 |
56 | |
57 | #include "limex_state_impl.h" |
58 | |
59 | #define INLINE_ATTR really_inline |
60 | #include "limex_common_impl.h" |
61 | |
62 | //////////////////////////////////////////////////////////////////////////// |
63 | // LimEx NFA implementation code - general purpose registers |
64 | //////////////////////////////////////////////////////////////////////////// |
65 | |
66 | // Process exceptional states |
67 | |
68 | #define STATE_ON_STACK |
69 | #define ESTATE_ON_STACK |
70 | #define RUN_EXCEPTION_FN_ONLY |
71 | #include "limex_exceptional.h" |
72 | |
73 | static really_inline |
74 | int processExceptional32(u32 s, u32 estate, UNUSED u32 diffmask, u32 *succ, |
75 | const struct LimExNFA32 *limex, |
76 | const struct NFAException32 *exceptions, u64a offset, |
77 | struct NFAContext32 *ctx, char in_rev, char flags) { |
78 | assert(estate != 0); // guaranteed by calling macro |
79 | |
80 | if (estate == ctx->cached_estate) { |
81 | DEBUG_PRINTF("using cached succ from previous state\n" ); |
82 | *succ |= ctx->cached_esucc; |
83 | if (ctx->cached_reports && (flags & CALLBACK_OUTPUT)) { |
84 | DEBUG_PRINTF("firing cached reports from previous state\n" ); |
85 | if (unlikely(limexRunReports(ctx->cached_reports, ctx->callback, |
86 | ctx->context, offset) |
87 | == MO_HALT_MATCHING)) { |
88 | return PE_RV_HALT; // halt; |
89 | } |
90 | } |
91 | return 0; |
92 | } |
93 | |
94 | u32 orig_estate = estate; // for caching |
95 | u32 local_succ = 0; |
96 | struct proto_cache new_cache = {0, NULL}; |
97 | enum CacheResult cacheable = CACHE_RESULT; |
98 | |
99 | /* Note that only exception-states that consist of exceptions that _only_ |
100 | * set successors (not fire accepts or squash states) are cacheable. */ |
101 | |
102 | do { |
103 | u32 bit = findAndClearLSB_32(&estate); |
104 | u32 idx = rank_in_mask32(limex->exceptionMask, bit); |
105 | const struct NFAException32 *e = &exceptions[idx]; |
106 | if (!runException32(e, s, succ, &local_succ, limex, offset, ctx, |
107 | &new_cache, &cacheable, in_rev, flags)) { |
108 | return PE_RV_HALT; |
109 | } |
110 | } while (estate != 0); |
111 | |
112 | *succ |= local_succ; |
113 | |
114 | if (cacheable == CACHE_RESULT) { |
115 | ctx->cached_estate = orig_estate; |
116 | ctx->cached_esucc = local_succ; |
117 | ctx->cached_reports = new_cache.reports; |
118 | ctx->cached_br = new_cache.br; |
119 | } else if (cacheable == DO_NOT_CACHE_RESULT_AND_FLUSH_BR_ENTRIES) { |
120 | if (ctx->cached_br) { |
121 | ctx->cached_estate = 0U; |
122 | } |
123 | } |
124 | |
125 | return 0; |
126 | } |
127 | |
128 | // 32-bit models. |
129 | #include "limex_runtime_impl.h" |
130 | |