| 1 | #ifndef _FILTER_AST_H |
| 2 | #define _FILTER_AST_H |
| 3 | |
| 4 | /* |
| 5 | * filter-ast.h |
| 6 | * |
| 7 | * LTTng filter AST |
| 8 | * |
| 9 | * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 13 | * as published by the Free Software Foundation. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public License |
| 21 | * along with this library; if not, write to the Free Software Foundation, |
| 22 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * Note: filter-ast.h should be included before filter-parser.h. |
| 27 | */ |
| 28 | |
| 29 | #include <urcu/list.h> |
| 30 | #include <stdint.h> |
| 31 | |
| 32 | #define printf_debug(fmt, args...) \ |
| 33 | do { \ |
| 34 | if (filter_parser_debug) \ |
| 35 | fprintf(stdout, "[debug] " fmt, ## args); \ |
| 36 | } while (0) |
| 37 | |
| 38 | // the parameter name (of the reentrant 'yyparse' function) |
| 39 | // data is a pointer to a 'SParserParam' structure |
| 40 | //#define YYPARSE_PARAM parser_ctx |
| 41 | |
| 42 | #ifndef YY_TYPEDEF_YY_SCANNER_T |
| 43 | #define YY_TYPEDEF_YY_SCANNER_T |
| 44 | typedef void* yyscan_t; |
| 45 | #endif |
| 46 | |
| 47 | extern int filter_parser_debug; |
| 48 | |
| 49 | struct filter_node; |
| 50 | struct filter_parser; |
| 51 | |
| 52 | enum node_type { |
| 53 | NODE_UNKNOWN = 0, |
| 54 | NODE_ROOT, |
| 55 | |
| 56 | NODE_EXPRESSION, |
| 57 | NODE_OP, |
| 58 | NODE_UNARY_OP, |
| 59 | |
| 60 | NR_NODE_TYPES, |
| 61 | }; |
| 62 | |
| 63 | enum op_type { |
| 64 | AST_OP_UNKNOWN = 0, |
| 65 | AST_OP_MUL, |
| 66 | AST_OP_DIV, |
| 67 | AST_OP_MOD, |
| 68 | AST_OP_PLUS, |
| 69 | AST_OP_MINUS, |
| 70 | AST_OP_RSHIFT, |
| 71 | AST_OP_LSHIFT, |
| 72 | AST_OP_AND, |
| 73 | AST_OP_OR, |
| 74 | AST_OP_BIN_AND, |
| 75 | AST_OP_BIN_OR, |
| 76 | AST_OP_BIN_XOR, |
| 77 | |
| 78 | AST_OP_EQ, |
| 79 | AST_OP_NE, |
| 80 | AST_OP_GT, |
| 81 | AST_OP_LT, |
| 82 | AST_OP_GE, |
| 83 | AST_OP_LE, |
| 84 | }; |
| 85 | |
| 86 | enum unary_op_type { |
| 87 | AST_UNARY_UNKNOWN = 0, |
| 88 | AST_UNARY_PLUS, |
| 89 | AST_UNARY_MINUS, |
| 90 | AST_UNARY_NOT, |
| 91 | AST_UNARY_BIN_NOT, |
| 92 | }; |
| 93 | |
| 94 | enum ast_link_type { |
| 95 | AST_LINK_UNKNOWN = 0, |
| 96 | AST_LINK_DOT, |
| 97 | AST_LINK_RARROW, |
| 98 | }; |
| 99 | |
| 100 | struct filter_node { |
| 101 | /* |
| 102 | * Parent node is only set on demand by specific visitor. |
| 103 | */ |
| 104 | struct filter_node *parent; |
| 105 | struct cds_list_head gc; |
| 106 | |
| 107 | enum node_type type; |
| 108 | union { |
| 109 | struct { |
| 110 | } unknown; |
| 111 | struct { |
| 112 | struct filter_node *child; |
| 113 | } root; |
| 114 | struct { |
| 115 | enum { |
| 116 | AST_EXP_UNKNOWN = 0, |
| 117 | AST_EXP_STRING, |
| 118 | AST_EXP_CONSTANT, |
| 119 | AST_EXP_FLOAT_CONSTANT, |
| 120 | AST_EXP_IDENTIFIER, |
| 121 | AST_EXP_GLOBAL_IDENTIFIER, |
| 122 | AST_EXP_NESTED, |
| 123 | } type; |
| 124 | enum ast_link_type post_op; /* reverse */ |
| 125 | enum ast_link_type pre_op; /* forward */ |
| 126 | union { |
| 127 | char *string; |
| 128 | uint64_t constant; |
| 129 | double float_constant; |
| 130 | char *identifier; |
| 131 | /* |
| 132 | * child can be nested. |
| 133 | */ |
| 134 | struct filter_node *child; |
| 135 | } u; |
| 136 | /* linked dot/arrow chain */ |
| 137 | struct filter_node *prev; |
| 138 | struct filter_node *next; |
| 139 | } expression; |
| 140 | struct { |
| 141 | enum op_type type; |
| 142 | struct filter_node *lchild; |
| 143 | struct filter_node *rchild; |
| 144 | } op; |
| 145 | struct { |
| 146 | enum unary_op_type type; |
| 147 | struct filter_node *child; |
| 148 | } unary_op; |
| 149 | } u; |
| 150 | }; |
| 151 | |
| 152 | struct filter_ast { |
| 153 | struct filter_node root; |
| 154 | struct cds_list_head allocated_nodes; |
| 155 | }; |
| 156 | |
| 157 | const char *node_type(struct filter_node *node); |
| 158 | |
| 159 | struct ir_op; |
| 160 | |
| 161 | struct filter_parser_ctx { |
| 162 | yyscan_t scanner; |
| 163 | struct filter_ast *ast; |
| 164 | struct cds_list_head allocated_strings; |
| 165 | struct ir_op *ir_root; |
| 166 | struct lttng_filter_bytecode_alloc *bytecode; |
| 167 | struct lttng_filter_bytecode_alloc *bytecode_reloc; |
| 168 | }; |
| 169 | |
| 170 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input); |
| 171 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx); |
| 172 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx); |
| 173 | |
| 174 | static inline |
| 175 | struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx) |
| 176 | { |
| 177 | return parser_ctx->ast; |
| 178 | } |
| 179 | |
| 180 | int filter_visitor_set_parent(struct filter_parser_ctx *ctx); |
| 181 | int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream, |
| 182 | int indent); |
| 183 | int filter_visitor_ir_generate(struct filter_parser_ctx *ctx); |
| 184 | void filter_ir_free(struct filter_parser_ctx *ctx); |
| 185 | int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx); |
| 186 | void filter_bytecode_free(struct filter_parser_ctx *ctx); |
| 187 | int filter_visitor_ir_check_binary_op_nesting(struct filter_parser_ctx *ctx); |
| 188 | int filter_visitor_ir_check_binary_comparator(struct filter_parser_ctx *ctx); |
| 189 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx); |
| 190 | |
| 191 | #endif /* _FILTER_AST_H */ |