1 | /* |
2 | * Copyright (c) 2015-2017, 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 Compiler front-end interface |
31 | */ |
32 | |
33 | #ifndef COMPILER_H |
34 | #define COMPILER_H |
35 | |
36 | #include "ue2common.h" |
37 | #include "database.h" |
38 | #include "compiler/expression_info.h" |
39 | #include "parser/Component.h" |
40 | #include "util/noncopyable.h" |
41 | |
42 | #include <memory> |
43 | |
44 | struct hs_database; |
45 | struct hs_expr_ext; |
46 | |
47 | namespace ue2 { |
48 | |
49 | struct CompileContext; |
50 | struct Grey; |
51 | struct target_t; |
52 | class NG; |
53 | class NGHolder; |
54 | class ReportManager; |
55 | |
56 | /** \brief Class gathering together the pieces of a parsed expression. */ |
57 | class ParsedExpression : noncopyable { |
58 | public: |
59 | ParsedExpression(unsigned index, const char *expression, unsigned flags, |
60 | ReportID report, const hs_expr_ext *ext = nullptr); |
61 | |
62 | /** \brief Expression information (from flags, extparam etc) */ |
63 | ExpressionInfo expr; |
64 | |
65 | /** \brief Root node of parsed component tree. */ |
66 | std::unique_ptr<Component> component; |
67 | }; |
68 | |
69 | /** |
70 | * \brief Class gathering together the pieces of an expression that has been |
71 | * built into an NFA graph. |
72 | */ |
73 | struct BuiltExpression { |
74 | /** \brief Expression information (from flags, extparam etc) */ |
75 | ExpressionInfo expr; |
76 | |
77 | /** \brief Built Glushkov NFA graph. */ |
78 | std::unique_ptr<NGHolder> g; |
79 | }; |
80 | |
81 | /** |
82 | * Add an expression to the compiler. |
83 | * |
84 | * @param ng |
85 | * The global NG object. |
86 | * @param index |
87 | * The index of the expression (used for errors) |
88 | * @param expression |
89 | * NULL-terminated PCRE expression |
90 | * @param flags |
91 | * The full set of Hyperscan flags associated with this rule. |
92 | * @param ext |
93 | * Struct containing extra parameters for this expression, or NULL if |
94 | * none. |
95 | * @param report |
96 | * The identifier to associate with the expression; returned by engine on |
97 | * match. |
98 | */ |
99 | void addExpression(NG &ng, unsigned index, const char *expression, |
100 | unsigned flags, const hs_expr_ext *ext, ReportID report); |
101 | |
102 | /** |
103 | * Build a Hyperscan database out of the expressions we've been given. A |
104 | * fatal error will result in an exception being thrown. |
105 | * |
106 | * @param ng |
107 | * The global NG object. |
108 | * @param[out] length |
109 | * The number of bytes occupied by the compiled structure. |
110 | * @return |
111 | * The compiled structure. Should be deallocated with the |
112 | * hs_database_free() function. |
113 | */ |
114 | struct hs_database *build(NG &ng, unsigned int *length); |
115 | |
116 | /** |
117 | * Constructs an NFA graph from the given expression tree. |
118 | * |
119 | * @param rm |
120 | * Global ReportManager for this compile. |
121 | * @param cc |
122 | * Global compile context for this compile. |
123 | * @param expr |
124 | * ParsedExpression object. |
125 | * @return |
126 | * nullptr on error. |
127 | */ |
128 | BuiltExpression buildGraph(ReportManager &rm, const CompileContext &cc, |
129 | const ParsedExpression &expr); |
130 | |
131 | /** |
132 | * Build a platform_t out of a target_t. |
133 | */ |
134 | platform_t target_to_platform(const target_t &target_info); |
135 | |
136 | #if defined(DUMP_SUPPORT) || defined(DEBUG) |
137 | void dumpExpression(const ParsedExpression &expr, const char *stage, |
138 | const Grey &grey); |
139 | #else |
140 | static really_inline |
141 | void dumpExpression(UNUSED const ParsedExpression &expr, |
142 | UNUSED const char *stage, UNUSED const Grey &grey) { |
143 | } |
144 | |
145 | #endif |
146 | |
147 | } // namespace |
148 | |
149 | #endif // COMPILER_H |
150 | |