1/*
2 * Copyright (c) 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/** \file
30 * \brief Parse and build ParsedLogical::logicalTree and combInfoMap.
31 */
32
33#ifndef LOGICAL_COMBINATION_H
34#define LOGICAL_COMBINATION_H
35
36#include "util/logical.h"
37
38#include <map>
39#include <set>
40#include <vector>
41
42namespace ue2 {
43
44class ParsedLogical {
45 friend class ReportManager;
46public:
47 /** \brief Parse 1 logical expression \a logical, assign temporary ckey. */
48 void parseLogicalCombination(unsigned id, const char *logical, u32 ekey,
49 u64a min_offset, u64a max_offset);
50
51 /** \brief Check if all sub-expression id in combinations are valid. */
52 void validateSubIDs(const unsigned *ids, const char *const *expressions,
53 const unsigned *flags, unsigned elements);
54
55 /** \brief Renumber and assign final lkey for each logical operation
56 * after parsed all logical expressions. */
57 void logicalKeyRenumber();
58
59 /** \brief Fetch the lkey associated with the given expression id,
60 * assigning one if necessary. */
61 u32 getLogicalKey(u32 expressionId);
62
63 /** \brief Fetch the ckey associated with the given expression id,
64 * assigning one if necessary. */
65 u32 getCombKey(u32 expressionId);
66
67 /** \brief Add lkey's corresponding combination id. */
68 void addRelateCKey(u32 lkey, u32 ckey);
69
70 /** \brief Add one Logical Operation. */
71 u32 logicalTreeAdd(u32 op, u32 left, u32 right);
72
73 /** \brief Assign the combination info associated with the given ckey. */
74 void combinationInfoAdd(u32 ckey, u32 id, u32 ekey, u32 lkey_start,
75 u32 lkey_result, u64a min_offset, u64a max_offset);
76
77 const std::map<u32, u32> &getLkeyMap() const {
78 return toLogicalKeyMap;
79 }
80
81 const std::vector<LogicalOp> &getLogicalTree() const {
82 return logicalTree;
83 }
84
85 CombInfo getCombInfoById(u32 id) const {
86 u32 ckey = toCombKeyMap.at(id);
87 assert(ckey < combInfoMap.size());
88 return combInfoMap.at(ckey);
89 }
90
91private:
92 /** \brief Mapping from ckey to combination info. */
93 std::vector<CombInfo> combInfoMap;
94
95 /** \brief Mapping from combination expression id to combination key,
96 * combination key is used in combination bit-vector cache. */
97 std::map<u32, u32> toCombKeyMap;
98
99 /** \brief Mapping from expression id to logical key, logical key is used
100 * as index in LogicalOp array. */
101 std::map<u32, u32> toLogicalKeyMap;
102
103 /** \brief Mapping from logical key to related combination keys. */
104 std::map<u32, std::set<u32>> lkey2ckeys;
105
106 /** \brief Logical constraints, each operation from postfix notation. */
107 std::vector<LogicalOp> logicalTree;
108};
109
110} // namespace ue2
111
112#endif
113