X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Ffilter%2Ffilter-visitor-generate-ir.c;h=cd7930ad5476cb36e62f949d7a9ae818b4cf3723;hp=e1cab3468686691ecae04fbd680bf7bf4f174182;hb=bff988fac4f8d1ffab3f85f0eec9546c76e57706;hpb=a187da1ab55a90f95fed8a29ed232b344f0d32b2 diff --git a/src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c b/src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c index e1cab3468..cd7930ad5 100644 --- a/src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c +++ b/src/lib/lttng-ctl/filter/filter-visitor-generate-ir.c @@ -31,6 +31,7 @@ #include "filter-ir.h" #include +#include static struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx, @@ -56,6 +57,8 @@ struct ir_op *make_op_root(struct ir_op *child, enum ir_side side) return NULL; case IR_DATA_NUMERIC: case IR_DATA_FIELD_REF: + case IR_DATA_GET_CONTEXT_REF: + case IR_DATA_EXPRESSION: /* ok */ break; } @@ -67,6 +70,22 @@ struct ir_op *make_op_root(struct ir_op *child, enum ir_side side) return op; } +static +enum ir_load_string_type get_literal_string_type(const char *string) +{ + assert(string); + + if (strutils_is_star_glob_pattern(string)) { + if (strutils_is_star_at_the_end_only_glob_pattern(string)) { + return IR_LOAD_STRING_TYPE_GLOB_STAR_END; + } + + return IR_LOAD_STRING_TYPE_GLOB_STAR; + } + + return IR_LOAD_STRING_TYPE_PLAIN; +} + static struct ir_op *make_op_load_string(char *string, enum ir_side side) { @@ -79,8 +98,9 @@ struct ir_op *make_op_load_string(char *string, enum ir_side side) op->data_type = IR_DATA_STRING; op->signedness = IR_SIGN_UNKNOWN; op->side = side; - op->u.load.u.string = strdup(string); - if (!op->u.load.u.string) { + op->u.load.u.string.type = get_literal_string_type(string); + op->u.load.u.string.value = strdup(string); + if (!op->u.load.u.string.value) { free(op); return NULL; } @@ -121,7 +141,149 @@ struct ir_op *make_op_load_float(double v, enum ir_side side) } static -struct ir_op *make_op_load_field_ref(char *string, enum ir_side side) +void free_load_expression(struct ir_load_expression *load_expression) +{ + struct ir_load_expression_op *exp_op; + + if (!load_expression) + return; + exp_op = load_expression->child; + for (;;) { + struct ir_load_expression_op *prev_exp_op; + + if (!exp_op) + break; + switch (exp_op->type) { + case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: + case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: + case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: + case IR_LOAD_EXPRESSION_GET_INDEX: + case IR_LOAD_EXPRESSION_LOAD_FIELD: + break; + case IR_LOAD_EXPRESSION_GET_SYMBOL: + free(exp_op->u.symbol); + break; + } + prev_exp_op = exp_op; + exp_op = exp_op->next; + free(prev_exp_op); + } + free(load_expression); +} + +/* + * Returns the first node of the chain, after initializing the next + * pointers. + */ +static +struct filter_node *load_expression_get_forward_chain(struct filter_node *node) +{ + struct filter_node *prev_node; + + for (;;) { + assert(node->type == NODE_EXPRESSION); + prev_node = node; + node = node->u.expression.prev; + if (!node) { + break; + } + node->u.expression.next = prev_node; + } + return prev_node; +} + +static +struct ir_load_expression *create_load_expression(struct filter_node *node) +{ + struct ir_load_expression *load_exp; + struct ir_load_expression_op *load_exp_op, *prev_op; + char *str; + + /* Get forward chain. */ + node = load_expression_get_forward_chain(node); + if (!node) + return NULL; + load_exp = calloc(sizeof(struct ir_load_expression), 1); + if (!load_exp) + return NULL; + + /* Root */ + load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1); + if (!load_exp_op) + goto error; + load_exp->child = load_exp_op; + str = node->u.expression.u.string; + if (!strcmp(str, "$ctx")) { + load_exp_op->type = IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT; + node = node->u.expression.next; + if (!node) { + fprintf(stderr, "[error] Expecting identifier after \'%s\'\n", str); + goto error; + } + str = node->u.expression.u.string; + } else if (!strcmp(str, "$app")) { + load_exp_op->type = IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT; + node = node->u.expression.next; + if (!node) { + fprintf(stderr, "[error] Expecting identifier after \'%s\'\n", str); + goto error; + } + str = node->u.expression.u.string; + } else if (str[0] == '$') { + fprintf(stderr, "[error] Unexpected identifier \'%s\'\n", str); + goto error; + } else { + load_exp_op->type = IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT; + } + + for (;;) { + struct filter_node *bracket_node; + + prev_op = load_exp_op; + load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1); + if (!load_exp_op) + goto error; + prev_op->next = load_exp_op; + load_exp_op->type = IR_LOAD_EXPRESSION_GET_SYMBOL; + load_exp_op->u.symbol = strdup(str); + if (!load_exp_op->u.symbol) + goto error; + + /* Explore brackets from current node. */ + for (bracket_node = node->u.expression.next_bracket; + bracket_node != NULL; + bracket_node = bracket_node->u.expression.next_bracket) { + prev_op = load_exp_op; + load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1); + if (!load_exp_op) + goto error; + prev_op->next = load_exp_op; + load_exp_op->type = IR_LOAD_EXPRESSION_GET_INDEX; + load_exp_op->u.index = bracket_node->u.expression.u.constant; + } + /* Go to next chain element. */ + node = node->u.expression.next; + if (!node) + break; + str = node->u.expression.u.string; + } + /* Add final load field */ + prev_op = load_exp_op; + load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1); + if (!load_exp_op) + goto error; + prev_op->next = load_exp_op; + load_exp_op->type = IR_LOAD_EXPRESSION_LOAD_FIELD; + return load_exp; + +error: + free_load_expression(load_exp); + return NULL; +} + +static +struct ir_op *make_op_load_expression(struct filter_node *node, + enum ir_side side) { struct ir_op *op; @@ -129,15 +291,19 @@ struct ir_op *make_op_load_field_ref(char *string, enum ir_side side) if (!op) return NULL; op->op = IR_OP_LOAD; - op->data_type = IR_DATA_FIELD_REF; + op->data_type = IR_DATA_EXPRESSION; op->signedness = IR_SIGN_DYN; op->side = side; - op->u.load.u.ref = strdup(string); - if (!op->u.load.u.ref) { - free(op); - return NULL; + op->u.load.u.expression = create_load_expression(node); + if (!op->u.load.u.expression) { + goto error; } return op; + +error: + free_load_expression(op->u.load.u.expression); + free(op); + return NULL; } static @@ -192,55 +358,39 @@ struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side) child, side); } -#if 0 static -struct ir_op *make_op_binary_numeric(enum op_type bin_op_type, +struct ir_op *make_op_binary_compare(enum op_type bin_op_type, const char *op_str, struct ir_op *left, struct ir_op *right, enum ir_side side) { struct ir_op *op = NULL; - if (right->data_type == IR_DATA_STRING - || right->data_type == IR_DATA_STRING) { - fprintf(stderr, "[error] binary operation '%s' not allowed on string literal\n", op_str); - goto error; - } if (left->data_type == IR_DATA_UNKNOWN || right->data_type == IR_DATA_UNKNOWN) { - fprintf(stderr, "[error] binary operation '%s' has unknown type for both children\n", op_str); + fprintf(stderr, "[error] binary operation '%s' has unknown operand type\n", op_str); goto error; } + if ((left->data_type == IR_DATA_STRING + && (right->data_type == IR_DATA_NUMERIC || right->data_type == IR_DATA_FLOAT)) + || ((left->data_type == IR_DATA_NUMERIC || left->data_type == IR_DATA_FLOAT) && + right->data_type == IR_DATA_STRING)) { + fprintf(stderr, "[error] binary operation '%s' operand type mismatch\n", op_str); + goto error; + } - op = calloc(sizeof(struct ir_op_binary), 1); + op = calloc(sizeof(struct ir_op), 1); if (!op) return NULL; op->op = IR_OP_BINARY; op->u.binary.type = bin_op_type; op->u.binary.left = left; op->u.binary.right = right; - op->side = side; - /* - * The field that is not a field ref will select type. - */ - if (left->data_type != IR_DATA_FIELD_REF) - op->data_type = left->data_type; - else - op->data_type = right->data_type; - - if (left->signedness == IR_SIGNED - || right->signedness == IR_SIGNED) { - op->signedness = IR_SIGNED; - } else if (left->signedness == IR_SIGN_DYN - || right->signedness == IR_SIGN_DYN) { - op->signedness = IR_SIGN_DYN; - } else if (left->signedness == IR_UNSIGNED - && right->signedness == IR_UNSIGNED) { - op->signedness = IR_UNSIGNED; - } else { - op->signedness = IR_SIGN_UNKNOWN; - } + /* we return a boolean, represented as signed numeric */ + op->data_type = IR_DATA_NUMERIC; + op->signedness = IR_SIGNED; + op->side = side; return op; @@ -250,78 +400,49 @@ error: } static -struct ir_op *make_op_binary_mul(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_numeric(AST_OP_MUL, "*", left, right, side); -} - -static -struct ir_op *make_op_binary_div(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_numeric(AST_OP_DIV, "/", left, right, side); -} - -static -struct ir_op *make_op_binary_mod(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_numeric(AST_OP_MOD, "%", left, right, side); -} - -static -struct ir_op *make_op_binary_plus(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_numeric(AST_OP_PLUS, "+", left, right, side); -} - -static -struct ir_op *make_op_binary_minus(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_eq(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_MINUS, "-", left, right, side); + return make_op_binary_compare(AST_OP_EQ, "==", left, right, side); } static -struct ir_op *make_op_binary_rshift(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_ne(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_RSHIFT, ">>", left, right, side); + return make_op_binary_compare(AST_OP_NE, "!=", left, right, side); } static -struct ir_op *make_op_binary_lshift(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_gt(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_LSHIFT, "<<", left, right, side); + return make_op_binary_compare(AST_OP_GT, ">", left, right, side); } static -struct ir_op *make_op_binary_and(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_lt(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_BIN_AND, "&", left, right, side); + return make_op_binary_compare(AST_OP_LT, "<", left, right, side); } static -struct ir_op *make_op_binary_or(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_ge(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_BIN_OR, "|", left, right, side); + return make_op_binary_compare(AST_OP_GE, ">=", left, right, side); } static -struct ir_op *make_op_binary_xor(struct ir_op *left, struct ir_op *right, +struct ir_op *make_op_binary_le(struct ir_op *left, struct ir_op *right, enum ir_side side) { - return make_op_binary_numeric(AST_OP_BIN_XOR, "^", left, right, side); + return make_op_binary_compare(AST_OP_LE, "<=", left, right, side); } -#endif //0 static -struct ir_op *make_op_binary_compare(enum op_type bin_op_type, +struct ir_op *make_op_binary_logical(enum op_type bin_op_type, const char *op_str, struct ir_op *left, struct ir_op *right, enum ir_side side) { @@ -333,18 +454,16 @@ struct ir_op *make_op_binary_compare(enum op_type bin_op_type, goto error; } - if ((left->data_type == IR_DATA_STRING - && (right->data_type == IR_DATA_NUMERIC || right->data_type == IR_DATA_FLOAT)) - || ((left->data_type == IR_DATA_NUMERIC || left->data_type == IR_DATA_FLOAT) && - right->data_type == IR_DATA_STRING)) { - fprintf(stderr, "[error] binary operation '%s' operand type mismatch\n", op_str); + if (left->data_type == IR_DATA_STRING + || right->data_type == IR_DATA_STRING) { + fprintf(stderr, "[error] logical binary operation '%s' cannot have string operand\n", op_str); goto error; } op = calloc(sizeof(struct ir_op), 1); if (!op) return NULL; - op->op = IR_OP_BINARY; + op->op = IR_OP_LOGICAL; op->u.binary.type = bin_op_type; op->u.binary.left = left; op->u.binary.right = right; @@ -362,49 +481,7 @@ error: } static -struct ir_op *make_op_binary_eq(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_EQ, "==", left, right, side); -} - -static -struct ir_op *make_op_binary_ne(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_NE, "!=", left, right, side); -} - -static -struct ir_op *make_op_binary_gt(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_GT, ">", left, right, side); -} - -static -struct ir_op *make_op_binary_lt(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_LT, "<", left, right, side); -} - -static -struct ir_op *make_op_binary_ge(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_GE, ">=", left, right, side); -} - -static -struct ir_op *make_op_binary_le(struct ir_op *left, struct ir_op *right, - enum ir_side side) -{ - return make_op_binary_compare(AST_OP_LE, "<=", left, right, side); -} - -static -struct ir_op *make_op_binary_logical(enum op_type bin_op_type, +struct ir_op *make_op_binary_bitwise(enum op_type bin_op_type, const char *op_str, struct ir_op *left, struct ir_op *right, enum ir_side side) { @@ -412,25 +489,30 @@ struct ir_op *make_op_binary_logical(enum op_type bin_op_type, if (left->data_type == IR_DATA_UNKNOWN || right->data_type == IR_DATA_UNKNOWN) { - fprintf(stderr, "[error] binary operation '%s' has unknown operand type\n", op_str); + fprintf(stderr, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str); goto error; } if (left->data_type == IR_DATA_STRING || right->data_type == IR_DATA_STRING) { - fprintf(stderr, "[error] logical binary operation '%s' cannot have string operand\n", op_str); + fprintf(stderr, "[error] bitwise binary operation '%s' cannot have string operand\n", op_str); + goto error; + } + if (left->data_type == IR_DATA_FLOAT + || right->data_type == IR_DATA_FLOAT) { + fprintf(stderr, "[error] bitwise binary operation '%s' cannot have floating point operand\n", op_str); goto error; } op = calloc(sizeof(struct ir_op), 1); if (!op) return NULL; - op->op = IR_OP_LOGICAL; + op->op = IR_OP_BINARY; op->u.binary.type = bin_op_type; op->u.binary.left = left; op->u.binary.right = right; - /* we return a boolean, represented as signed numeric */ + /* we return a signed numeric */ op->data_type = IR_DATA_NUMERIC; op->signedness = IR_SIGNED; op->side = side; @@ -456,6 +538,27 @@ struct ir_op *make_op_binary_logical_or(struct ir_op *left, struct ir_op *right, return make_op_binary_logical(AST_OP_OR, "||", left, right, side); } +static +struct ir_op *make_op_binary_bitwise_and(struct ir_op *left, struct ir_op *right, + enum ir_side side) +{ + return make_op_binary_bitwise(AST_OP_BIT_AND, "&", left, right, side); +} + +static +struct ir_op *make_op_binary_bitwise_or(struct ir_op *left, struct ir_op *right, + enum ir_side side) +{ + return make_op_binary_bitwise(AST_OP_BIT_OR, "|", left, right, side); +} + +static +struct ir_op *make_op_binary_bitwise_xor(struct ir_op *left, struct ir_op *right, + enum ir_side side) +{ + return make_op_binary_bitwise(AST_OP_BIT_XOR, "^", left, right, side); +} + static void filter_free_ir_recursive(struct ir_op *op) { @@ -473,11 +576,14 @@ void filter_free_ir_recursive(struct ir_op *op) case IR_OP_LOAD: switch (op->data_type) { case IR_DATA_STRING: - free(op->u.load.u.string); + free(op->u.load.u.string.value); break; - case IR_DATA_FIELD_REF: + case IR_DATA_FIELD_REF: /* fall-through */ + case IR_DATA_GET_CONTEXT_REF: free(op->u.load.u.ref); break; + case IR_DATA_EXPRESSION: + free_load_expression(op->u.load.u.expression); default: break; } @@ -516,12 +622,8 @@ struct ir_op *make_expression(struct filter_parser_ctx *ctx, return make_op_load_float(node->u.expression.u.float_constant, side); case AST_EXP_IDENTIFIER: - if (node->u.expression.pre_op != AST_LINK_UNKNOWN) { - fprintf(stderr, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__); - return NULL; - } - return make_op_load_field_ref(node->u.expression.u.identifier, - side); + case AST_EXP_GLOBAL_IDENTIFIER: + return make_op_load_expression(node, side); case AST_EXP_NESTED: return generate_ir_recursive(ctx, node->u.expression.u.child, side); @@ -542,10 +644,8 @@ struct ir_op *make_op(struct filter_parser_ctx *ctx, return NULL; /* - * Binary operators other than comparators and logical and/or - * are not supported. If we ever want to support those, we will - * need a stack for the general case rather than just 2 - * registers (see bytecode). + * The following binary operators other than comparators and + * logical and/or are not supported yet. */ case AST_OP_MUL: op_str = "*"; @@ -568,15 +668,19 @@ struct ir_op *make_op(struct filter_parser_ctx *ctx, case AST_OP_LSHIFT: op_str = "<<"; goto error_not_supported; - case AST_OP_BIN_AND: - op_str = "&"; - goto error_not_supported; - case AST_OP_BIN_OR: - op_str = "|"; - goto error_not_supported; - case AST_OP_BIN_XOR: - op_str = "^"; - goto error_not_supported; + + case AST_OP_BIT_AND: + case AST_OP_BIT_OR: + case AST_OP_BIT_XOR: + lchild = generate_ir_recursive(ctx, node->u.op.lchild, IR_LEFT); + if (!lchild) + return NULL; + rchild = generate_ir_recursive(ctx, node->u.op.rchild, IR_RIGHT); + if (!rchild) { + filter_free_ir_recursive(lchild); + return NULL; + } + break; case AST_OP_EQ: case AST_OP_NE: @@ -636,6 +740,15 @@ struct ir_op *make_op(struct filter_parser_ctx *ctx, case AST_OP_LE: op = make_op_binary_le(lchild, rchild, side); break; + case AST_OP_BIT_AND: + op = make_op_binary_bitwise_and(lchild, rchild, side); + break; + case AST_OP_BIT_OR: + op = make_op_binary_bitwise_or(lchild, rchild, side); + break; + case AST_OP_BIT_XOR: + op = make_op_binary_bitwise_xor(lchild, rchild, side); + break; default: break; } @@ -709,7 +822,7 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, } return op; } - case AST_UNARY_BIN_NOT: + case AST_UNARY_BIT_NOT: { op_str = "~"; goto error_not_supported;