2 * filter-visitor-generate-ir.c
4 * LTTng filter generate intermediate representation
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * SPDX-License-Identifier: LGPL-2.1-only
19 #include "filter-ast.h"
20 #include "filter-parser.h"
21 #include "filter-ir.h"
23 #include <common/macros.h>
24 #include <common/string-utils/string-utils.h>
27 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
28 struct filter_node
*node
, enum ir_side side
);
31 struct ir_op
*make_op_root(struct ir_op
*child
, enum ir_side side
)
35 op
= calloc(sizeof(struct ir_op
), 1);
38 switch (child
->data_type
) {
41 fprintf(stderr
, "[error] Unknown root child data type\n");
45 fprintf(stderr
, "[error] String cannot be root data type\n");
49 case IR_DATA_FIELD_REF
:
50 case IR_DATA_GET_CONTEXT_REF
:
51 case IR_DATA_EXPRESSION
:
57 op
->data_type
= child
->data_type
;
58 op
->signedness
= child
->signedness
;
59 op
->u
.root
.child
= child
;
64 enum ir_load_string_type
get_literal_string_type(const char *string
)
68 if (strutils_is_star_glob_pattern(string
)) {
69 if (strutils_is_star_at_the_end_only_glob_pattern(string
)) {
70 return IR_LOAD_STRING_TYPE_GLOB_STAR_END
;
73 return IR_LOAD_STRING_TYPE_GLOB_STAR
;
76 return IR_LOAD_STRING_TYPE_PLAIN
;
80 struct ir_op
*make_op_load_string(const char *string
, enum ir_side side
)
84 op
= calloc(sizeof(struct ir_op
), 1);
88 op
->data_type
= IR_DATA_STRING
;
89 op
->signedness
= IR_SIGN_UNKNOWN
;
91 op
->u
.load
.u
.string
.type
= get_literal_string_type(string
);
92 op
->u
.load
.u
.string
.value
= strdup(string
);
93 if (!op
->u
.load
.u
.string
.value
) {
101 struct ir_op
*make_op_load_numeric(int64_t v
, enum ir_side side
)
105 op
= calloc(sizeof(struct ir_op
), 1);
109 op
->data_type
= IR_DATA_NUMERIC
;
110 /* TODO: for now, all numeric values are signed */
111 op
->signedness
= IR_SIGNED
;
113 op
->u
.load
.u
.num
= v
;
118 struct ir_op
*make_op_load_float(double v
, enum ir_side side
)
122 op
= calloc(sizeof(struct ir_op
), 1);
126 op
->data_type
= IR_DATA_FLOAT
;
127 op
->signedness
= IR_SIGN_UNKNOWN
;
129 op
->u
.load
.u
.flt
= v
;
134 void free_load_expression(struct ir_load_expression
*load_expression
)
136 struct ir_load_expression_op
*exp_op
;
138 if (!load_expression
)
140 exp_op
= load_expression
->child
;
142 struct ir_load_expression_op
*prev_exp_op
;
146 switch (exp_op
->type
) {
147 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
148 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
149 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
150 case IR_LOAD_EXPRESSION_GET_INDEX
:
151 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
153 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
154 free(exp_op
->u
.symbol
);
157 prev_exp_op
= exp_op
;
158 exp_op
= exp_op
->next
;
161 free(load_expression
);
165 * Returns the first node of the chain, after initializing the next
169 struct filter_node
*load_expression_get_forward_chain(struct filter_node
*node
)
171 struct filter_node
*prev_node
;
174 assert(node
->type
== NODE_EXPRESSION
);
176 node
= node
->u
.expression
.prev
;
180 node
->u
.expression
.next
= prev_node
;
186 struct ir_load_expression
*create_load_expression(struct filter_node
*node
)
188 struct ir_load_expression
*load_exp
;
189 struct ir_load_expression_op
*load_exp_op
, *prev_op
;
192 /* Get forward chain. */
193 node
= load_expression_get_forward_chain(node
);
196 load_exp
= calloc(sizeof(struct ir_load_expression
), 1);
201 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
204 load_exp
->child
= load_exp_op
;
205 str
= node
->u
.expression
.u
.string
;
206 if (!strcmp(str
, "$ctx")) {
207 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
;
208 node
= node
->u
.expression
.next
;
210 fprintf(stderr
, "[error] Expecting identifier after \'%s\'\n", str
);
213 str
= node
->u
.expression
.u
.string
;
214 } else if (!strcmp(str
, "$app")) {
215 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
;
216 node
= node
->u
.expression
.next
;
218 fprintf(stderr
, "[error] Expecting identifier after \'%s\'\n", str
);
221 str
= node
->u
.expression
.u
.string
;
222 } else if (str
[0] == '$') {
223 fprintf(stderr
, "[error] Unexpected identifier \'%s\'\n", str
);
226 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
;
230 struct filter_node
*bracket_node
;
232 prev_op
= load_exp_op
;
233 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
236 prev_op
->next
= load_exp_op
;
237 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_SYMBOL
;
238 load_exp_op
->u
.symbol
= strdup(str
);
239 if (!load_exp_op
->u
.symbol
)
242 /* Explore brackets from current node. */
243 for (bracket_node
= node
->u
.expression
.next_bracket
;
244 bracket_node
!= NULL
;
245 bracket_node
= bracket_node
->u
.expression
.next_bracket
) {
246 prev_op
= load_exp_op
;
247 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
250 prev_op
->next
= load_exp_op
;
251 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_INDEX
;
252 load_exp_op
->u
.index
= bracket_node
->u
.expression
.u
.constant
;
254 /* Go to next chain element. */
255 node
= node
->u
.expression
.next
;
258 str
= node
->u
.expression
.u
.string
;
260 /* Add final load field */
261 prev_op
= load_exp_op
;
262 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
265 prev_op
->next
= load_exp_op
;
266 load_exp_op
->type
= IR_LOAD_EXPRESSION_LOAD_FIELD
;
270 free_load_expression(load_exp
);
275 struct ir_op
*make_op_load_expression(struct filter_node
*node
,
280 op
= calloc(sizeof(struct ir_op
), 1);
284 op
->data_type
= IR_DATA_EXPRESSION
;
285 op
->signedness
= IR_SIGN_DYN
;
287 op
->u
.load
.u
.expression
= create_load_expression(node
);
288 if (!op
->u
.load
.u
.expression
) {
294 free_load_expression(op
->u
.load
.u
.expression
);
300 struct ir_op
*make_op_unary(enum unary_op_type unary_op_type
,
301 const char *op_str
, enum ir_op_signedness signedness
,
302 struct ir_op
*child
, enum ir_side side
)
304 struct ir_op
*op
= NULL
;
306 if (child
->data_type
== IR_DATA_STRING
) {
307 fprintf(stderr
, "[error] unary operation '%s' not allowed on string literal\n", op_str
);
311 op
= calloc(sizeof(struct ir_op
), 1);
314 op
->op
= IR_OP_UNARY
;
315 op
->data_type
= child
->data_type
;
316 op
->signedness
= signedness
;
318 op
->u
.unary
.type
= unary_op_type
;
319 op
->u
.unary
.child
= child
;
328 * unary + is pretty much useless.
331 struct ir_op
*make_op_unary_plus(struct ir_op
*child
, enum ir_side side
)
333 return make_op_unary(AST_UNARY_PLUS
, "+", child
->signedness
,
338 struct ir_op
*make_op_unary_minus(struct ir_op
*child
, enum ir_side side
)
340 return make_op_unary(AST_UNARY_MINUS
, "-", child
->signedness
,
345 struct ir_op
*make_op_unary_not(struct ir_op
*child
, enum ir_side side
)
347 return make_op_unary(AST_UNARY_NOT
, "!", child
->signedness
,
352 struct ir_op
*make_op_unary_bit_not(struct ir_op
*child
, enum ir_side side
)
354 return make_op_unary(AST_UNARY_BIT_NOT
, "~", child
->signedness
,
359 struct ir_op
*make_op_binary_compare(enum op_type bin_op_type
,
360 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
363 struct ir_op
*op
= NULL
;
365 if (left
->data_type
== IR_DATA_UNKNOWN
366 || right
->data_type
== IR_DATA_UNKNOWN
) {
367 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
371 if ((left
->data_type
== IR_DATA_STRING
372 && (right
->data_type
== IR_DATA_NUMERIC
|| right
->data_type
== IR_DATA_FLOAT
))
373 || ((left
->data_type
== IR_DATA_NUMERIC
|| left
->data_type
== IR_DATA_FLOAT
) &&
374 right
->data_type
== IR_DATA_STRING
)) {
375 fprintf(stderr
, "[error] binary operation '%s' operand type mismatch\n", op_str
);
379 op
= calloc(sizeof(struct ir_op
), 1);
382 op
->op
= IR_OP_BINARY
;
383 op
->u
.binary
.type
= bin_op_type
;
384 op
->u
.binary
.left
= left
;
385 op
->u
.binary
.right
= right
;
387 /* we return a boolean, represented as signed numeric */
388 op
->data_type
= IR_DATA_NUMERIC
;
389 op
->signedness
= IR_SIGNED
;
400 struct ir_op
*make_op_binary_eq(struct ir_op
*left
, struct ir_op
*right
,
403 return make_op_binary_compare(AST_OP_EQ
, "==", left
, right
, side
);
407 struct ir_op
*make_op_binary_ne(struct ir_op
*left
, struct ir_op
*right
,
410 return make_op_binary_compare(AST_OP_NE
, "!=", left
, right
, side
);
414 struct ir_op
*make_op_binary_gt(struct ir_op
*left
, struct ir_op
*right
,
417 return make_op_binary_compare(AST_OP_GT
, ">", left
, right
, side
);
421 struct ir_op
*make_op_binary_lt(struct ir_op
*left
, struct ir_op
*right
,
424 return make_op_binary_compare(AST_OP_LT
, "<", left
, right
, side
);
428 struct ir_op
*make_op_binary_ge(struct ir_op
*left
, struct ir_op
*right
,
431 return make_op_binary_compare(AST_OP_GE
, ">=", left
, right
, side
);
435 struct ir_op
*make_op_binary_le(struct ir_op
*left
, struct ir_op
*right
,
438 return make_op_binary_compare(AST_OP_LE
, "<=", left
, right
, side
);
442 struct ir_op
*make_op_binary_logical(enum op_type bin_op_type
,
443 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
446 struct ir_op
*op
= NULL
;
448 if (left
->data_type
== IR_DATA_UNKNOWN
449 || right
->data_type
== IR_DATA_UNKNOWN
) {
450 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
454 if (left
->data_type
== IR_DATA_STRING
455 || right
->data_type
== IR_DATA_STRING
) {
456 fprintf(stderr
, "[error] logical binary operation '%s' cannot have string operand\n", op_str
);
460 op
= calloc(sizeof(struct ir_op
), 1);
463 op
->op
= IR_OP_LOGICAL
;
464 op
->u
.binary
.type
= bin_op_type
;
465 op
->u
.binary
.left
= left
;
466 op
->u
.binary
.right
= right
;
468 /* we return a boolean, represented as signed numeric */
469 op
->data_type
= IR_DATA_NUMERIC
;
470 op
->signedness
= IR_SIGNED
;
481 struct ir_op
*make_op_binary_bitwise(enum op_type bin_op_type
,
482 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
485 struct ir_op
*op
= NULL
;
487 if (left
->data_type
== IR_DATA_UNKNOWN
488 || right
->data_type
== IR_DATA_UNKNOWN
) {
489 fprintf(stderr
, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str
);
493 if (left
->data_type
== IR_DATA_STRING
494 || right
->data_type
== IR_DATA_STRING
) {
495 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have string operand\n", op_str
);
498 if (left
->data_type
== IR_DATA_FLOAT
499 || right
->data_type
== IR_DATA_FLOAT
) {
500 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have floating point operand\n", op_str
);
504 op
= calloc(sizeof(struct ir_op
), 1);
507 op
->op
= IR_OP_BINARY
;
508 op
->u
.binary
.type
= bin_op_type
;
509 op
->u
.binary
.left
= left
;
510 op
->u
.binary
.right
= right
;
512 /* we return a signed numeric */
513 op
->data_type
= IR_DATA_NUMERIC
;
514 op
->signedness
= IR_SIGNED
;
525 struct ir_op
*make_op_binary_logical_and(struct ir_op
*left
, struct ir_op
*right
,
528 return make_op_binary_logical(AST_OP_AND
, "&&", left
, right
, side
);
532 struct ir_op
*make_op_binary_logical_or(struct ir_op
*left
, struct ir_op
*right
,
535 return make_op_binary_logical(AST_OP_OR
, "||", left
, right
, side
);
539 struct ir_op
*make_op_binary_bitwise_rshift(struct ir_op
*left
, struct ir_op
*right
,
542 return make_op_binary_bitwise(AST_OP_BIT_RSHIFT
, ">>", left
, right
, side
);
546 struct ir_op
*make_op_binary_bitwise_lshift(struct ir_op
*left
, struct ir_op
*right
,
549 return make_op_binary_bitwise(AST_OP_BIT_LSHIFT
, "<<", left
, right
, side
);
553 struct ir_op
*make_op_binary_bitwise_and(struct ir_op
*left
, struct ir_op
*right
,
556 return make_op_binary_bitwise(AST_OP_BIT_AND
, "&", left
, right
, side
);
560 struct ir_op
*make_op_binary_bitwise_or(struct ir_op
*left
, struct ir_op
*right
,
563 return make_op_binary_bitwise(AST_OP_BIT_OR
, "|", left
, right
, side
);
567 struct ir_op
*make_op_binary_bitwise_xor(struct ir_op
*left
, struct ir_op
*right
,
570 return make_op_binary_bitwise(AST_OP_BIT_XOR
, "^", left
, right
, side
);
574 void filter_free_ir_recursive(struct ir_op
*op
)
581 fprintf(stderr
, "[error] Unknown op type in %s\n",
585 filter_free_ir_recursive(op
->u
.root
.child
);
588 switch (op
->data_type
) {
590 free(op
->u
.load
.u
.string
.value
);
592 case IR_DATA_FIELD_REF
: /* fall-through */
593 case IR_DATA_GET_CONTEXT_REF
:
594 free(op
->u
.load
.u
.ref
);
596 case IR_DATA_EXPRESSION
:
597 free_load_expression(op
->u
.load
.u
.expression
);
603 filter_free_ir_recursive(op
->u
.unary
.child
);
606 filter_free_ir_recursive(op
->u
.binary
.left
);
607 filter_free_ir_recursive(op
->u
.binary
.right
);
610 filter_free_ir_recursive(op
->u
.logical
.left
);
611 filter_free_ir_recursive(op
->u
.logical
.right
);
618 struct ir_op
*make_expression(struct filter_parser_ctx
*ctx
,
619 struct filter_node
*node
, enum ir_side side
)
621 switch (node
->u
.expression
.type
) {
622 case AST_EXP_UNKNOWN
:
624 fprintf(stderr
, "[error] %s: unknown expression type\n", __func__
);
628 return make_op_load_string(node
->u
.expression
.u
.string
, side
);
629 case AST_EXP_CONSTANT
:
630 return make_op_load_numeric(node
->u
.expression
.u
.constant
,
632 case AST_EXP_FLOAT_CONSTANT
:
633 return make_op_load_float(node
->u
.expression
.u
.float_constant
,
635 case AST_EXP_IDENTIFIER
:
636 case AST_EXP_GLOBAL_IDENTIFIER
:
637 return make_op_load_expression(node
, side
);
639 return generate_ir_recursive(ctx
, node
->u
.expression
.u
.child
,
645 struct ir_op
*make_op(struct filter_parser_ctx
*ctx
,
646 struct filter_node
*node
, enum ir_side side
)
648 struct ir_op
*op
= NULL
, *lchild
, *rchild
;
649 const char *op_str
= "?";
651 switch (node
->u
.op
.type
) {
654 fprintf(stderr
, "[error] %s: unknown binary op type\n", __func__
);
658 * The following binary operators other than comparators and
659 * logical and/or are not supported yet.
663 goto error_not_supported
;
666 goto error_not_supported
;
669 goto error_not_supported
;
672 goto error_not_supported
;
675 goto error_not_supported
;
677 case AST_OP_BIT_RSHIFT
:
678 case AST_OP_BIT_LSHIFT
:
682 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
685 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
687 filter_free_ir_recursive(lchild
);
698 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
701 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
703 filter_free_ir_recursive(lchild
);
711 * Both children considered as left, since we need to
714 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
717 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_LEFT
);
719 filter_free_ir_recursive(lchild
);
725 switch (node
->u
.op
.type
) {
727 op
= make_op_binary_logical_and(lchild
, rchild
, side
);
730 op
= make_op_binary_logical_or(lchild
, rchild
, side
);
733 op
= make_op_binary_eq(lchild
, rchild
, side
);
736 op
= make_op_binary_ne(lchild
, rchild
, side
);
739 op
= make_op_binary_gt(lchild
, rchild
, side
);
742 op
= make_op_binary_lt(lchild
, rchild
, side
);
745 op
= make_op_binary_ge(lchild
, rchild
, side
);
748 op
= make_op_binary_le(lchild
, rchild
, side
);
750 case AST_OP_BIT_RSHIFT
:
751 op
= make_op_binary_bitwise_rshift(lchild
, rchild
, side
);
753 case AST_OP_BIT_LSHIFT
:
754 op
= make_op_binary_bitwise_lshift(lchild
, rchild
, side
);
757 op
= make_op_binary_bitwise_and(lchild
, rchild
, side
);
760 op
= make_op_binary_bitwise_or(lchild
, rchild
, side
);
763 op
= make_op_binary_bitwise_xor(lchild
, rchild
, side
);
770 filter_free_ir_recursive(rchild
);
771 filter_free_ir_recursive(lchild
);
776 fprintf(stderr
, "[error] %s: binary operation '%s' not supported\n",
782 struct ir_op
*make_unary_op(struct filter_parser_ctx
*ctx
,
783 struct filter_node
*node
, enum ir_side side
)
785 switch (node
->u
.unary_op
.type
) {
786 case AST_UNARY_UNKNOWN
:
788 fprintf(stderr
, "[error] %s: unknown unary op type\n", __func__
);
793 struct ir_op
*op
, *child
;
795 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
799 op
= make_op_unary_plus(child
, side
);
801 filter_free_ir_recursive(child
);
806 case AST_UNARY_MINUS
:
808 struct ir_op
*op
, *child
;
810 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
814 op
= make_op_unary_minus(child
, side
);
816 filter_free_ir_recursive(child
);
823 struct ir_op
*op
, *child
;
825 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
829 op
= make_op_unary_not(child
, side
);
831 filter_free_ir_recursive(child
);
836 case AST_UNARY_BIT_NOT
:
838 struct ir_op
*op
, *child
;
840 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
844 op
= make_op_unary_bit_not(child
, side
);
846 filter_free_ir_recursive(child
);
857 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
858 struct filter_node
*node
, enum ir_side side
)
860 switch (node
->type
) {
863 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
868 struct ir_op
*op
, *child
;
870 child
= generate_ir_recursive(ctx
, node
->u
.root
.child
,
874 op
= make_op_root(child
, side
);
876 filter_free_ir_recursive(child
);
881 case NODE_EXPRESSION
:
882 return make_expression(ctx
, node
, side
);
884 return make_op(ctx
, node
, side
);
886 return make_unary_op(ctx
, node
, side
);
892 void filter_ir_free(struct filter_parser_ctx
*ctx
)
894 filter_free_ir_recursive(ctx
->ir_root
);
899 int filter_visitor_ir_generate(struct filter_parser_ctx
*ctx
)
903 op
= generate_ir_recursive(ctx
, &ctx
->ast
->root
, IR_LEFT
);