| 1 | /* |
| 2 | * Copyright (c) 2015-2018, 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 | #ifndef MCCLELLANCOMPILE_H |
| 30 | #define MCCLELLANCOMPILE_H |
| 31 | |
| 32 | #include "accel_dfa_build_strat.h" |
| 33 | #include "rdfa.h" |
| 34 | #include "ue2common.h" |
| 35 | #include "util/bytecode_ptr.h" |
| 36 | |
| 37 | #include <memory> |
| 38 | #include <vector> |
| 39 | #include <set> |
| 40 | |
| 41 | struct NFA; |
| 42 | |
| 43 | namespace ue2 { |
| 44 | |
| 45 | class ReportManager; |
| 46 | struct CompileContext; |
| 47 | |
| 48 | class mcclellan_build_strat : public accel_dfa_build_strat { |
| 49 | public: |
| 50 | mcclellan_build_strat(raw_dfa &rdfa_in, const ReportManager &rm_in, |
| 51 | bool only_accel_init_in) |
| 52 | : accel_dfa_build_strat(rm_in, only_accel_init_in), rdfa(rdfa_in) {} |
| 53 | raw_dfa &get_raw() const override { return rdfa; } |
| 54 | std::unique_ptr<raw_report_info> gatherReports( |
| 55 | std::vector<u32> &reports /* out */, |
| 56 | std::vector<u32> &reports_eod /* out */, |
| 57 | u8 *isSingleReport /* out */, |
| 58 | ReportID *arbReport /* out */) const override; |
| 59 | size_t accelSize(void) const override; |
| 60 | u32 max_allowed_offset_accel() const override; |
| 61 | u32 max_stop_char() const override; |
| 62 | u32 max_floating_stop_char() const override; |
| 63 | DfaType getType() const override { return McClellan; } |
| 64 | |
| 65 | private: |
| 66 | raw_dfa &rdfa; |
| 67 | }; |
| 68 | |
| 69 | /** |
| 70 | * \brief Construct an implementation DFA. |
| 71 | * |
| 72 | * \param raw the raw dfa to construct from |
| 73 | * \param cc compile context |
| 74 | * \param rm report manger |
| 75 | * \param only_accel_init if true, only the init states will be examined for |
| 76 | * acceleration opportunities |
| 77 | * \param trust_daddy_states if true, trust the daddy state set in the raw dfa |
| 78 | * rather than conducting a search for a better daddy (for Sherman |
| 79 | * states) |
| 80 | * \param accel_states (optional) success, is filled with the set of |
| 81 | * accelerable states |
| 82 | */ |
| 83 | bytecode_ptr<NFA> |
| 84 | mcclellanCompile(raw_dfa &raw, const CompileContext &cc, |
| 85 | const ReportManager &rm, bool only_accel_init, |
| 86 | bool trust_daddy_states = false, |
| 87 | std::set<dstate_id_t> *accel_states = nullptr); |
| 88 | |
| 89 | /* used internally by mcclellan/haig/gough compile process */ |
| 90 | bytecode_ptr<NFA> |
| 91 | mcclellanCompile_i(raw_dfa &raw, accel_dfa_build_strat &strat, |
| 92 | const CompileContext &cc, bool trust_daddy_states = false, |
| 93 | std::set<dstate_id_t> *accel_states = nullptr); |
| 94 | |
| 95 | /** |
| 96 | * \brief Returns the width of the character reach at start. |
| 97 | */ |
| 98 | u32 mcclellanStartReachSize(const raw_dfa *raw); |
| 99 | |
| 100 | std::set<ReportID> all_reports(const raw_dfa &rdfa); |
| 101 | |
| 102 | bool has_accel_mcclellan(const NFA *nfa); |
| 103 | |
| 104 | } // namespace ue2 |
| 105 | |
| 106 | #endif // MCCLELLANCOMPILE_H |
| 107 | |