X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Ffilter%2Ffilter-visitor-generate-ir.cpp;h=1bfe66c6f0d5010d71c2d8fe02f774f5eb5744a7;hb=HEAD;hp=a7236bd104f7e7314301507dc7442bb3a82b6f6a;hpb=c9e313bc594f40a86eed237dce222c0fc99c957f;p=lttng-tools.git diff --git a/src/common/filter/filter-visitor-generate-ir.cpp b/src/common/filter/filter-visitor-generate-ir.cpp index a7236bd10..9283fc3d1 100644 --- a/src/common/filter/filter-visitor-generate-ir.cpp +++ b/src/common/filter/filter-visitor-generate-ir.cpp @@ -9,29 +9,28 @@ * */ -#include -#include -#include -#include -#include #include "filter-ast.hpp" -#include "filter-parser.hpp" #include "filter-ir.hpp" +#include "filter-parser.hpp" #include #include #include -static -struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx, - struct filter_node *node, enum ir_side side); +#include +#include +#include +#include +#include + +static struct ir_op * +generate_ir_recursive(struct filter_parser_ctx *ctx, struct filter_node *node, enum ir_side side); -static -struct ir_op *make_op_root(struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_root(struct ir_op *child, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; switch (child->data_type) { @@ -59,8 +58,7 @@ 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) +static enum ir_load_string_type get_literal_string_type(const char *string) { LTTNG_ASSERT(string); @@ -75,12 +73,11 @@ enum ir_load_string_type get_literal_string_type(const char *string) return IR_LOAD_STRING_TYPE_PLAIN; } -static -struct ir_op *make_op_load_string(const char *string, enum ir_side side) +static struct ir_op *make_op_load_string(const char *string, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -96,12 +93,11 @@ struct ir_op *make_op_load_string(const char *string, enum ir_side side) return op; } -static -struct ir_op *make_op_load_numeric(int64_t v, enum ir_side side) +static struct ir_op *make_op_load_numeric(int64_t v, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -113,12 +109,11 @@ struct ir_op *make_op_load_numeric(int64_t v, enum ir_side side) return op; } -static -struct ir_op *make_op_load_float(double v, enum ir_side side) +static struct ir_op *make_op_load_float(double v, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -129,8 +124,7 @@ struct ir_op *make_op_load_float(double v, enum ir_side side) return op; } -static -void free_load_expression(struct ir_load_expression *load_expression) +static void free_load_expression(struct ir_load_expression *load_expression) { struct ir_load_expression_op *exp_op; @@ -164,8 +158,7 @@ void free_load_expression(struct ir_load_expression *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) +static struct filter_node *load_expression_get_forward_chain(struct filter_node *node) { struct filter_node *prev_node; @@ -181,8 +174,7 @@ struct filter_node *load_expression_get_forward_chain(struct filter_node *node) return prev_node; } -static -struct ir_load_expression *create_load_expression(struct filter_node *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; @@ -192,12 +184,12 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) node = load_expression_get_forward_chain(node); if (!node) return NULL; - load_exp = (ir_load_expression *) zmalloc(sizeof(struct ir_load_expression)); + load_exp = zmalloc(); if (!load_exp) return NULL; /* Root */ - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; load_exp->child = load_exp_op; @@ -229,7 +221,7 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) struct filter_node *bracket_node; prev_op = load_exp_op; - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -239,16 +231,16 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) 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) { + for (bracket_node = node->u.expression.next_bracket; bracket_node != NULL; + bracket_node = bracket_node->u.expression.next_bracket) { prev_op = load_exp_op; if (bracket_node->type != NODE_EXPRESSION || - bracket_node->u.expression.type != AST_EXP_CONSTANT) { - fprintf(stderr, "[error] Expecting constant index in array expression\n"); - goto error; - } - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + bracket_node->u.expression.type != AST_EXP_CONSTANT) { + fprintf(stderr, + "[error] Expecting constant index in array expression\n"); + goto error; + } + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -263,7 +255,7 @@ struct ir_load_expression *create_load_expression(struct filter_node *node) } /* Add final load field */ prev_op = load_exp_op; - load_exp_op = (ir_load_expression_op *) zmalloc(sizeof(struct ir_load_expression_op)); + load_exp_op = zmalloc(); if (!load_exp_op) goto error; prev_op->next = load_exp_op; @@ -275,13 +267,11 @@ error: return NULL; } -static -struct ir_op *make_op_load_expression(struct filter_node *node, - enum ir_side side) +static struct ir_op *make_op_load_expression(struct filter_node *node, enum ir_side side) { struct ir_op *op; - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOAD; @@ -300,19 +290,22 @@ error: return NULL; } -static -struct ir_op *make_op_unary(enum unary_op_type unary_op_type, - const char *op_str, enum ir_op_signedness signedness, - struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_unary(enum unary_op_type unary_op_type, + const char *op_str, + enum ir_op_signedness signedness, + struct ir_op *child, + enum ir_side side) { struct ir_op *op = NULL; if (child->data_type == IR_DATA_STRING) { - fprintf(stderr, "[error] unary operation '%s' not allowed on string literal\n", op_str); + fprintf(stderr, + "[error] unary operation '%s' not allowed on string literal\n", + op_str); goto error; } - op = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_UNARY; @@ -331,56 +324,47 @@ error: /* * unary + is pretty much useless. */ -static -struct ir_op *make_op_unary_plus(struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_unary_plus(struct ir_op *child, enum ir_side side) { - return make_op_unary(AST_UNARY_PLUS, "+", child->signedness, - child, side); + return make_op_unary(AST_UNARY_PLUS, "+", child->signedness, child, side); } -static -struct ir_op *make_op_unary_minus(struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_unary_minus(struct ir_op *child, enum ir_side side) { - return make_op_unary(AST_UNARY_MINUS, "-", child->signedness, - child, side); + return make_op_unary(AST_UNARY_MINUS, "-", child->signedness, child, side); } -static -struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side) { - return make_op_unary(AST_UNARY_NOT, "!", child->signedness, - child, side); + return make_op_unary(AST_UNARY_NOT, "!", child->signedness, child, side); } -static -struct ir_op *make_op_unary_bit_not(struct ir_op *child, enum ir_side side) +static struct ir_op *make_op_unary_bit_not(struct ir_op *child, enum ir_side side) { - return make_op_unary(AST_UNARY_BIT_NOT, "~", child->signedness, - child, side); + return make_op_unary(AST_UNARY_BIT_NOT, "~", child->signedness, child, side); } -static -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) +static 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 (left->data_type == IR_DATA_UNKNOWN - || right->data_type == IR_DATA_UNKNOWN) { + 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); 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)) { + 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 = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_BINARY; @@ -400,68 +384,56 @@ error: return NULL; } -static -struct ir_op *make_op_binary_eq(struct ir_op *left, struct ir_op *right, - enum ir_side side) +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) +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) +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) +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) +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) +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, - const char *op_str, struct ir_op *left, struct ir_op *right, - enum ir_side side) +static 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) { struct ir_op *op = NULL; - if (left->data_type == IR_DATA_UNKNOWN - || right->data_type == IR_DATA_UNKNOWN) { + 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); 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); + 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 = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_LOGICAL; @@ -481,31 +453,34 @@ error: return NULL; } -static -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) +static 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) { struct ir_op *op = NULL; - if (left->data_type == IR_DATA_UNKNOWN - || right->data_type == IR_DATA_UNKNOWN) { - fprintf(stderr, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str); + if (left->data_type == IR_DATA_UNKNOWN || right->data_type == IR_DATA_UNKNOWN) { + 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] bitwise binary operation '%s' cannot have string operand\n", op_str); + if (left->data_type == IR_DATA_STRING || right->data_type == IR_DATA_STRING) { + 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); + 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 = (ir_op *) zmalloc(sizeof(struct ir_op)); + op = zmalloc(); if (!op) return NULL; op->op = IR_OP_BINARY; @@ -525,65 +500,56 @@ error: return NULL; } -static -struct ir_op *make_op_binary_logical_and(struct ir_op *left, struct ir_op *right, - enum ir_side side) +static struct ir_op * +make_op_binary_logical_and(struct ir_op *left, struct ir_op *right, enum ir_side side) { return make_op_binary_logical(AST_OP_AND, "&&", left, right, side); } -static -struct ir_op *make_op_binary_logical_or(struct ir_op *left, struct ir_op *right, - enum ir_side side) +static struct ir_op * +make_op_binary_logical_or(struct ir_op *left, struct ir_op *right, enum ir_side side) { return make_op_binary_logical(AST_OP_OR, "||", left, right, side); } -static -struct ir_op *make_op_binary_bitwise_rshift(struct ir_op *left, struct ir_op *right, - enum ir_side side) +static struct ir_op * +make_op_binary_bitwise_rshift(struct ir_op *left, struct ir_op *right, enum ir_side side) { return make_op_binary_bitwise(AST_OP_BIT_RSHIFT, ">>", left, right, side); } -static -struct ir_op *make_op_binary_bitwise_lshift(struct ir_op *left, struct ir_op *right, - enum ir_side side) +static struct ir_op * +make_op_binary_bitwise_lshift(struct ir_op *left, struct ir_op *right, enum ir_side side) { return make_op_binary_bitwise(AST_OP_BIT_LSHIFT, "<<", left, right, side); } -static -struct ir_op *make_op_binary_bitwise_and(struct ir_op *left, struct ir_op *right, - enum ir_side 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) +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) +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) +static void filter_free_ir_recursive(struct ir_op *op) { if (!op) return; switch (op->op) { case IR_OP_UNKNOWN: default: - fprintf(stderr, "[error] Unknown op type in %s\n", - __func__); + fprintf(stderr, "[error] Unknown op type in %s\n", __func__); break; case IR_OP_ROOT: filter_free_ir_recursive(op->u.root.child); @@ -593,7 +559,7 @@ void filter_free_ir_recursive(struct ir_op *op) case IR_DATA_STRING: free(op->u.load.u.string.value); break; - case IR_DATA_FIELD_REF: /* fall-through */ + case IR_DATA_FIELD_REF: /* fall-through */ case IR_DATA_GET_CONTEXT_REF: free(op->u.load.u.ref); break; @@ -618,9 +584,8 @@ void filter_free_ir_recursive(struct ir_op *op) free(op); } -static -struct ir_op *make_expression(struct filter_parser_ctx *ctx, - struct filter_node *node, enum ir_side side) +static struct ir_op * +make_expression(struct filter_parser_ctx *ctx, struct filter_node *node, enum ir_side side) { switch (node->u.expression.type) { case AST_EXP_UNKNOWN: @@ -631,23 +596,19 @@ struct ir_op *make_expression(struct filter_parser_ctx *ctx, case AST_EXP_STRING: return make_op_load_string(node->u.expression.u.string, side); case AST_EXP_CONSTANT: - return make_op_load_numeric(node->u.expression.u.constant, - side); + return make_op_load_numeric(node->u.expression.u.constant, side); case AST_EXP_FLOAT_CONSTANT: - return make_op_load_float(node->u.expression.u.float_constant, - side); + return make_op_load_float(node->u.expression.u.float_constant, side); case AST_EXP_IDENTIFIER: 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); + return generate_ir_recursive(ctx, node->u.expression.u.child, side); } } -static -struct ir_op *make_op(struct filter_parser_ctx *ctx, - struct filter_node *node, enum ir_side side) +static struct ir_op * +make_op(struct filter_parser_ctx *ctx, struct filter_node *node, enum ir_side side) { struct ir_op *op = NULL, *lchild, *rchild; const char *op_str = "?"; @@ -777,14 +738,12 @@ struct ir_op *make_op(struct filter_parser_ctx *ctx, return op; error_not_supported: - fprintf(stderr, "[error] %s: binary operation '%s' not supported\n", - __func__, op_str); + fprintf(stderr, "[error] %s: binary operation '%s' not supported\n", __func__, op_str); return NULL; } -static -struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, - struct filter_node *node, enum ir_side side) +static struct ir_op * +make_unary_op(struct filter_parser_ctx *ctx, struct filter_node *node, enum ir_side side) { switch (node->u.unary_op.type) { case AST_UNARY_UNKNOWN: @@ -796,8 +755,7 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, { struct ir_op *op, *child; - child = generate_ir_recursive(ctx, node->u.unary_op.child, - side); + child = generate_ir_recursive(ctx, node->u.unary_op.child, side); if (!child) return NULL; op = make_op_unary_plus(child, side); @@ -811,8 +769,7 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, { struct ir_op *op, *child; - child = generate_ir_recursive(ctx, node->u.unary_op.child, - side); + child = generate_ir_recursive(ctx, node->u.unary_op.child, side); if (!child) return NULL; op = make_op_unary_minus(child, side); @@ -826,8 +783,7 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, { struct ir_op *op, *child; - child = generate_ir_recursive(ctx, node->u.unary_op.child, - side); + child = generate_ir_recursive(ctx, node->u.unary_op.child, side); if (!child) return NULL; op = make_op_unary_not(child, side); @@ -841,8 +797,7 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, { struct ir_op *op, *child; - child = generate_ir_recursive(ctx, node->u.unary_op.child, - side); + child = generate_ir_recursive(ctx, node->u.unary_op.child, side); if (!child) return NULL; op = make_op_unary_bit_not(child, side); @@ -857,9 +812,8 @@ struct ir_op *make_unary_op(struct filter_parser_ctx *ctx, return NULL; } -static -struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx, - struct filter_node *node, enum ir_side side) +static struct ir_op * +generate_ir_recursive(struct filter_parser_ctx *ctx, struct filter_node *node, enum ir_side side) { switch (node->type) { case NODE_UNKNOWN: @@ -871,8 +825,7 @@ struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx, { struct ir_op *op, *child; - child = generate_ir_recursive(ctx, node->u.root.child, - side); + child = generate_ir_recursive(ctx, node->u.root.child, side); if (!child) return NULL; op = make_op_root(child, side);