2 * filter-visitor-generate-ir.c
4 * LTTng filter generate intermediate representation
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "filter-ast.h"
30 #include "filter-parser.h"
31 #include "filter-ir.h"
33 #include <common/macros.h>
34 #include <common/string-utils/string-utils.h>
37 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
38 struct filter_node
*node
, enum ir_side side
);
41 struct ir_op
*make_op_root(struct ir_op
*child
, enum ir_side side
)
45 op
= calloc(sizeof(struct ir_op
), 1);
48 switch (child
->data_type
) {
51 fprintf(stderr
, "[error] Unknown root child data type\n");
55 fprintf(stderr
, "[error] String cannot be root data type\n");
59 case IR_DATA_FIELD_REF
:
60 case IR_DATA_GET_CONTEXT_REF
:
61 case IR_DATA_EXPRESSION
:
67 op
->data_type
= child
->data_type
;
68 op
->signedness
= child
->signedness
;
69 op
->u
.root
.child
= child
;
74 enum ir_load_string_type
get_literal_string_type(const char *string
)
78 if (strutils_is_star_glob_pattern(string
)) {
79 if (strutils_is_star_at_the_end_only_glob_pattern(string
)) {
80 return IR_LOAD_STRING_TYPE_GLOB_STAR_END
;
83 return IR_LOAD_STRING_TYPE_GLOB_STAR
;
86 return IR_LOAD_STRING_TYPE_PLAIN
;
90 struct ir_op
*make_op_load_string(char *string
, enum ir_side side
)
94 op
= calloc(sizeof(struct ir_op
), 1);
98 op
->data_type
= IR_DATA_STRING
;
99 op
->signedness
= IR_SIGN_UNKNOWN
;
101 op
->u
.load
.u
.string
.type
= get_literal_string_type(string
);
102 op
->u
.load
.u
.string
.value
= strdup(string
);
103 if (!op
->u
.load
.u
.string
.value
) {
111 struct ir_op
*make_op_load_numeric(int64_t v
, enum ir_side side
)
115 op
= calloc(sizeof(struct ir_op
), 1);
119 op
->data_type
= IR_DATA_NUMERIC
;
120 /* TODO: for now, all numeric values are signed */
121 op
->signedness
= IR_SIGNED
;
123 op
->u
.load
.u
.num
= v
;
128 struct ir_op
*make_op_load_float(double v
, enum ir_side side
)
132 op
= calloc(sizeof(struct ir_op
), 1);
136 op
->data_type
= IR_DATA_FLOAT
;
137 op
->signedness
= IR_SIGN_UNKNOWN
;
139 op
->u
.load
.u
.flt
= v
;
144 void free_load_expression(struct ir_load_expression
*load_expression
)
146 struct ir_load_expression_op
*exp_op
;
148 if (!load_expression
)
150 exp_op
= load_expression
->child
;
152 struct ir_load_expression_op
*prev_exp_op
;
156 switch (exp_op
->type
) {
157 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
:
158 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
:
159 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
:
160 case IR_LOAD_EXPRESSION_GET_INDEX
:
161 case IR_LOAD_EXPRESSION_LOAD_FIELD
:
163 case IR_LOAD_EXPRESSION_GET_SYMBOL
:
164 free(exp_op
->u
.symbol
);
167 prev_exp_op
= exp_op
;
168 exp_op
= exp_op
->next
;
171 free(load_expression
);
175 * Returns the first node of the chain, after initializing the next
179 struct filter_node
*load_expression_get_forward_chain(struct filter_node
*node
)
181 struct filter_node
*prev_node
;
184 assert(node
->type
== NODE_EXPRESSION
);
186 node
= node
->u
.expression
.prev
;
190 node
->u
.expression
.next
= prev_node
;
196 struct ir_load_expression
*create_load_expression(struct filter_node
*node
)
198 struct ir_load_expression
*load_exp
;
199 struct ir_load_expression_op
*load_exp_op
, *prev_op
;
202 /* Get forward chain. */
203 node
= load_expression_get_forward_chain(node
);
206 load_exp
= calloc(sizeof(struct ir_load_expression
), 1);
211 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
214 load_exp
->child
= load_exp_op
;
215 str
= node
->u
.expression
.u
.string
;
216 if (!strcmp(str
, "$ctx")) {
217 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT
;
218 node
= node
->u
.expression
.next
;
220 fprintf(stderr
, "[error] Expecting identifier after \'%s\'\n", str
);
223 str
= node
->u
.expression
.u
.string
;
224 } else if (!strcmp(str
, "$app")) {
225 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT
;
226 node
= node
->u
.expression
.next
;
228 fprintf(stderr
, "[error] Expecting identifier after \'%s\'\n", str
);
231 str
= node
->u
.expression
.u
.string
;
232 } else if (str
[0] == '$') {
233 fprintf(stderr
, "[error] Unexpected identifier \'%s\'\n", str
);
236 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT
;
240 struct filter_node
*bracket_node
;
242 prev_op
= load_exp_op
;
243 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
246 prev_op
->next
= load_exp_op
;
247 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_SYMBOL
;
248 load_exp_op
->u
.symbol
= strdup(str
);
249 if (!load_exp_op
->u
.symbol
)
252 /* Explore brackets from current node. */
253 for (bracket_node
= node
->u
.expression
.next_bracket
;
254 bracket_node
!= NULL
;
255 bracket_node
= bracket_node
->u
.expression
.next_bracket
) {
256 prev_op
= load_exp_op
;
257 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
260 prev_op
->next
= load_exp_op
;
261 load_exp_op
->type
= IR_LOAD_EXPRESSION_GET_INDEX
;
262 load_exp_op
->u
.index
= bracket_node
->u
.expression
.u
.constant
;
264 /* Go to next chain element. */
265 node
= node
->u
.expression
.next
;
268 str
= node
->u
.expression
.u
.string
;
270 /* Add final load field */
271 prev_op
= load_exp_op
;
272 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
275 prev_op
->next
= load_exp_op
;
276 load_exp_op
->type
= IR_LOAD_EXPRESSION_LOAD_FIELD
;
280 free_load_expression(load_exp
);
285 struct ir_op
*make_op_load_expression(struct filter_node
*node
,
290 op
= calloc(sizeof(struct ir_op
), 1);
294 op
->data_type
= IR_DATA_EXPRESSION
;
295 op
->signedness
= IR_SIGN_DYN
;
297 op
->u
.load
.u
.expression
= create_load_expression(node
);
298 if (!op
->u
.load
.u
.expression
) {
304 free_load_expression(op
->u
.load
.u
.expression
);
310 struct ir_op
*make_op_unary(enum unary_op_type unary_op_type
,
311 const char *op_str
, enum ir_op_signedness signedness
,
312 struct ir_op
*child
, enum ir_side side
)
314 struct ir_op
*op
= NULL
;
316 if (child
->data_type
== IR_DATA_STRING
) {
317 fprintf(stderr
, "[error] unary operation '%s' not allowed on string literal\n", op_str
);
321 op
= calloc(sizeof(struct ir_op
), 1);
324 op
->op
= IR_OP_UNARY
;
325 op
->data_type
= child
->data_type
;
326 op
->signedness
= signedness
;
328 op
->u
.unary
.type
= unary_op_type
;
329 op
->u
.unary
.child
= child
;
338 * unary + is pretty much useless.
341 struct ir_op
*make_op_unary_plus(struct ir_op
*child
, enum ir_side side
)
343 return make_op_unary(AST_UNARY_PLUS
, "+", child
->signedness
,
348 struct ir_op
*make_op_unary_minus(struct ir_op
*child
, enum ir_side side
)
350 return make_op_unary(AST_UNARY_MINUS
, "-", child
->signedness
,
355 struct ir_op
*make_op_unary_not(struct ir_op
*child
, enum ir_side side
)
357 return make_op_unary(AST_UNARY_NOT
, "!", child
->signedness
,
362 struct ir_op
*make_op_binary_compare(enum op_type bin_op_type
,
363 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
366 struct ir_op
*op
= NULL
;
368 if (left
->data_type
== IR_DATA_UNKNOWN
369 || right
->data_type
== IR_DATA_UNKNOWN
) {
370 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
374 if ((left
->data_type
== IR_DATA_STRING
375 && (right
->data_type
== IR_DATA_NUMERIC
|| right
->data_type
== IR_DATA_FLOAT
))
376 || ((left
->data_type
== IR_DATA_NUMERIC
|| left
->data_type
== IR_DATA_FLOAT
) &&
377 right
->data_type
== IR_DATA_STRING
)) {
378 fprintf(stderr
, "[error] binary operation '%s' operand type mismatch\n", op_str
);
382 op
= calloc(sizeof(struct ir_op
), 1);
385 op
->op
= IR_OP_BINARY
;
386 op
->u
.binary
.type
= bin_op_type
;
387 op
->u
.binary
.left
= left
;
388 op
->u
.binary
.right
= right
;
390 /* we return a boolean, represented as signed numeric */
391 op
->data_type
= IR_DATA_NUMERIC
;
392 op
->signedness
= IR_SIGNED
;
403 struct ir_op
*make_op_binary_eq(struct ir_op
*left
, struct ir_op
*right
,
406 return make_op_binary_compare(AST_OP_EQ
, "==", left
, right
, side
);
410 struct ir_op
*make_op_binary_ne(struct ir_op
*left
, struct ir_op
*right
,
413 return make_op_binary_compare(AST_OP_NE
, "!=", left
, right
, side
);
417 struct ir_op
*make_op_binary_gt(struct ir_op
*left
, struct ir_op
*right
,
420 return make_op_binary_compare(AST_OP_GT
, ">", left
, right
, side
);
424 struct ir_op
*make_op_binary_lt(struct ir_op
*left
, struct ir_op
*right
,
427 return make_op_binary_compare(AST_OP_LT
, "<", left
, right
, side
);
431 struct ir_op
*make_op_binary_ge(struct ir_op
*left
, struct ir_op
*right
,
434 return make_op_binary_compare(AST_OP_GE
, ">=", left
, right
, side
);
438 struct ir_op
*make_op_binary_le(struct ir_op
*left
, struct ir_op
*right
,
441 return make_op_binary_compare(AST_OP_LE
, "<=", left
, right
, side
);
445 struct ir_op
*make_op_binary_logical(enum op_type bin_op_type
,
446 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
449 struct ir_op
*op
= NULL
;
451 if (left
->data_type
== IR_DATA_UNKNOWN
452 || right
->data_type
== IR_DATA_UNKNOWN
) {
453 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
457 if (left
->data_type
== IR_DATA_STRING
458 || right
->data_type
== IR_DATA_STRING
) {
459 fprintf(stderr
, "[error] logical binary operation '%s' cannot have string operand\n", op_str
);
463 op
= calloc(sizeof(struct ir_op
), 1);
466 op
->op
= IR_OP_LOGICAL
;
467 op
->u
.binary
.type
= bin_op_type
;
468 op
->u
.binary
.left
= left
;
469 op
->u
.binary
.right
= right
;
471 /* we return a boolean, represented as signed numeric */
472 op
->data_type
= IR_DATA_NUMERIC
;
473 op
->signedness
= IR_SIGNED
;
484 struct ir_op
*make_op_binary_bitwise(enum op_type bin_op_type
,
485 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
488 struct ir_op
*op
= NULL
;
490 if (left
->data_type
== IR_DATA_UNKNOWN
491 || right
->data_type
== IR_DATA_UNKNOWN
) {
492 fprintf(stderr
, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str
);
496 if (left
->data_type
== IR_DATA_STRING
497 || right
->data_type
== IR_DATA_STRING
) {
498 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have string operand\n", op_str
);
501 if (left
->data_type
== IR_DATA_FLOAT
502 || right
->data_type
== IR_DATA_FLOAT
) {
503 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have floating point operand\n", op_str
);
507 op
= calloc(sizeof(struct ir_op
), 1);
510 op
->op
= IR_OP_BINARY
;
511 op
->u
.binary
.type
= bin_op_type
;
512 op
->u
.binary
.left
= left
;
513 op
->u
.binary
.right
= right
;
515 /* we return a signed numeric */
516 op
->data_type
= IR_DATA_NUMERIC
;
517 op
->signedness
= IR_SIGNED
;
528 struct ir_op
*make_op_binary_logical_and(struct ir_op
*left
, struct ir_op
*right
,
531 return make_op_binary_logical(AST_OP_AND
, "&&", left
, right
, side
);
535 struct ir_op
*make_op_binary_logical_or(struct ir_op
*left
, struct ir_op
*right
,
538 return make_op_binary_logical(AST_OP_OR
, "||", left
, right
, side
);
542 struct ir_op
*make_op_binary_bitwise_and(struct ir_op
*left
, struct ir_op
*right
,
545 return make_op_binary_bitwise(AST_OP_BIT_AND
, "&", left
, right
, side
);
549 struct ir_op
*make_op_binary_bitwise_or(struct ir_op
*left
, struct ir_op
*right
,
552 return make_op_binary_bitwise(AST_OP_BIT_OR
, "|", left
, right
, side
);
556 struct ir_op
*make_op_binary_bitwise_xor(struct ir_op
*left
, struct ir_op
*right
,
559 return make_op_binary_bitwise(AST_OP_BIT_XOR
, "^", left
, right
, side
);
563 void filter_free_ir_recursive(struct ir_op
*op
)
570 fprintf(stderr
, "[error] Unknown op type in %s\n",
574 filter_free_ir_recursive(op
->u
.root
.child
);
577 switch (op
->data_type
) {
579 free(op
->u
.load
.u
.string
.value
);
581 case IR_DATA_FIELD_REF
: /* fall-through */
582 case IR_DATA_GET_CONTEXT_REF
:
583 free(op
->u
.load
.u
.ref
);
585 case IR_DATA_EXPRESSION
:
586 free_load_expression(op
->u
.load
.u
.expression
);
592 filter_free_ir_recursive(op
->u
.unary
.child
);
595 filter_free_ir_recursive(op
->u
.binary
.left
);
596 filter_free_ir_recursive(op
->u
.binary
.right
);
599 filter_free_ir_recursive(op
->u
.logical
.left
);
600 filter_free_ir_recursive(op
->u
.logical
.right
);
607 struct ir_op
*make_expression(struct filter_parser_ctx
*ctx
,
608 struct filter_node
*node
, enum ir_side side
)
610 switch (node
->u
.expression
.type
) {
611 case AST_EXP_UNKNOWN
:
613 fprintf(stderr
, "[error] %s: unknown expression type\n", __func__
);
617 return make_op_load_string(node
->u
.expression
.u
.string
, side
);
618 case AST_EXP_CONSTANT
:
619 return make_op_load_numeric(node
->u
.expression
.u
.constant
,
621 case AST_EXP_FLOAT_CONSTANT
:
622 return make_op_load_float(node
->u
.expression
.u
.float_constant
,
624 case AST_EXP_IDENTIFIER
:
625 case AST_EXP_GLOBAL_IDENTIFIER
:
626 return make_op_load_expression(node
, side
);
628 return generate_ir_recursive(ctx
, node
->u
.expression
.u
.child
,
634 struct ir_op
*make_op(struct filter_parser_ctx
*ctx
,
635 struct filter_node
*node
, enum ir_side side
)
637 struct ir_op
*op
= NULL
, *lchild
, *rchild
;
638 const char *op_str
= "?";
640 switch (node
->u
.op
.type
) {
643 fprintf(stderr
, "[error] %s: unknown binary op type\n", __func__
);
647 * The following binary operators other than comparators and
648 * logical and/or are not supported yet.
652 goto error_not_supported
;
655 goto error_not_supported
;
658 goto error_not_supported
;
661 goto error_not_supported
;
664 goto error_not_supported
;
667 goto error_not_supported
;
670 goto error_not_supported
;
675 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
678 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
680 filter_free_ir_recursive(lchild
);
691 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
694 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
696 filter_free_ir_recursive(lchild
);
704 * Both children considered as left, since we need to
707 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
710 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_LEFT
);
712 filter_free_ir_recursive(lchild
);
718 switch (node
->u
.op
.type
) {
720 op
= make_op_binary_logical_and(lchild
, rchild
, side
);
723 op
= make_op_binary_logical_or(lchild
, rchild
, side
);
726 op
= make_op_binary_eq(lchild
, rchild
, side
);
729 op
= make_op_binary_ne(lchild
, rchild
, side
);
732 op
= make_op_binary_gt(lchild
, rchild
, side
);
735 op
= make_op_binary_lt(lchild
, rchild
, side
);
738 op
= make_op_binary_ge(lchild
, rchild
, side
);
741 op
= make_op_binary_le(lchild
, rchild
, side
);
744 op
= make_op_binary_bitwise_and(lchild
, rchild
, side
);
747 op
= make_op_binary_bitwise_or(lchild
, rchild
, side
);
750 op
= make_op_binary_bitwise_xor(lchild
, rchild
, side
);
757 filter_free_ir_recursive(rchild
);
758 filter_free_ir_recursive(lchild
);
763 fprintf(stderr
, "[error] %s: binary operation '%s' not supported\n",
769 struct ir_op
*make_unary_op(struct filter_parser_ctx
*ctx
,
770 struct filter_node
*node
, enum ir_side side
)
772 const char *op_str
= "?";
774 switch (node
->u
.unary_op
.type
) {
775 case AST_UNARY_UNKNOWN
:
777 fprintf(stderr
, "[error] %s: unknown unary op type\n", __func__
);
782 struct ir_op
*op
, *child
;
784 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
788 op
= make_op_unary_plus(child
, side
);
790 filter_free_ir_recursive(child
);
795 case AST_UNARY_MINUS
:
797 struct ir_op
*op
, *child
;
799 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
803 op
= make_op_unary_minus(child
, side
);
805 filter_free_ir_recursive(child
);
812 struct ir_op
*op
, *child
;
814 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
818 op
= make_op_unary_not(child
, side
);
820 filter_free_ir_recursive(child
);
825 case AST_UNARY_BIT_NOT
:
828 goto error_not_supported
;
833 fprintf(stderr
, "[error] %s: unary operation '%s' not supported\n",
839 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
840 struct filter_node
*node
, enum ir_side side
)
842 switch (node
->type
) {
845 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
850 struct ir_op
*op
, *child
;
852 child
= generate_ir_recursive(ctx
, node
->u
.root
.child
,
856 op
= make_op_root(child
, side
);
858 filter_free_ir_recursive(child
);
863 case NODE_EXPRESSION
:
864 return make_expression(ctx
, node
, side
);
866 return make_op(ctx
, node
, side
);
868 return make_unary_op(ctx
, node
, side
);
874 void filter_ir_free(struct filter_parser_ctx
*ctx
)
876 filter_free_ir_recursive(ctx
->ir_root
);
881 int filter_visitor_ir_generate(struct filter_parser_ctx
*ctx
)
885 op
= generate_ir_recursive(ctx
, &ctx
->ast
->root
, IR_LEFT
);