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 if (bracket_node
->type
!= NODE_EXPRESSION
||
258 bracket_node
->u
.expression
.type
!= AST_EXP_CONSTANT
) {
259 fprintf(stderr
, "[error] Expecting constant index in array expression\n");
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_GET_INDEX
;
267 load_exp_op
->u
.index
= bracket_node
->u
.expression
.u
.constant
;
269 /* Go to next chain element. */
270 node
= node
->u
.expression
.next
;
273 str
= node
->u
.expression
.u
.string
;
275 /* Add final load field */
276 prev_op
= load_exp_op
;
277 load_exp_op
= calloc(sizeof(struct ir_load_expression_op
), 1);
280 prev_op
->next
= load_exp_op
;
281 load_exp_op
->type
= IR_LOAD_EXPRESSION_LOAD_FIELD
;
285 free_load_expression(load_exp
);
290 struct ir_op
*make_op_load_expression(struct filter_node
*node
,
295 op
= calloc(sizeof(struct ir_op
), 1);
299 op
->data_type
= IR_DATA_EXPRESSION
;
300 op
->signedness
= IR_SIGN_DYN
;
302 op
->u
.load
.u
.expression
= create_load_expression(node
);
303 if (!op
->u
.load
.u
.expression
) {
309 free_load_expression(op
->u
.load
.u
.expression
);
315 struct ir_op
*make_op_unary(enum unary_op_type unary_op_type
,
316 const char *op_str
, enum ir_op_signedness signedness
,
317 struct ir_op
*child
, enum ir_side side
)
319 struct ir_op
*op
= NULL
;
321 if (child
->data_type
== IR_DATA_STRING
) {
322 fprintf(stderr
, "[error] unary operation '%s' not allowed on string literal\n", op_str
);
326 op
= calloc(sizeof(struct ir_op
), 1);
329 op
->op
= IR_OP_UNARY
;
330 op
->data_type
= child
->data_type
;
331 op
->signedness
= signedness
;
333 op
->u
.unary
.type
= unary_op_type
;
334 op
->u
.unary
.child
= child
;
343 * unary + is pretty much useless.
346 struct ir_op
*make_op_unary_plus(struct ir_op
*child
, enum ir_side side
)
348 return make_op_unary(AST_UNARY_PLUS
, "+", child
->signedness
,
353 struct ir_op
*make_op_unary_minus(struct ir_op
*child
, enum ir_side side
)
355 return make_op_unary(AST_UNARY_MINUS
, "-", child
->signedness
,
360 struct ir_op
*make_op_unary_not(struct ir_op
*child
, enum ir_side side
)
362 return make_op_unary(AST_UNARY_NOT
, "!", child
->signedness
,
367 struct ir_op
*make_op_unary_bit_not(struct ir_op
*child
, enum ir_side side
)
369 return make_op_unary(AST_UNARY_BIT_NOT
, "~", child
->signedness
,
374 struct ir_op
*make_op_binary_compare(enum op_type bin_op_type
,
375 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
378 struct ir_op
*op
= NULL
;
380 if (left
->data_type
== IR_DATA_UNKNOWN
381 || right
->data_type
== IR_DATA_UNKNOWN
) {
382 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
386 if ((left
->data_type
== IR_DATA_STRING
387 && (right
->data_type
== IR_DATA_NUMERIC
|| right
->data_type
== IR_DATA_FLOAT
))
388 || ((left
->data_type
== IR_DATA_NUMERIC
|| left
->data_type
== IR_DATA_FLOAT
) &&
389 right
->data_type
== IR_DATA_STRING
)) {
390 fprintf(stderr
, "[error] binary operation '%s' operand type mismatch\n", op_str
);
394 op
= calloc(sizeof(struct ir_op
), 1);
397 op
->op
= IR_OP_BINARY
;
398 op
->u
.binary
.type
= bin_op_type
;
399 op
->u
.binary
.left
= left
;
400 op
->u
.binary
.right
= right
;
402 /* we return a boolean, represented as signed numeric */
403 op
->data_type
= IR_DATA_NUMERIC
;
404 op
->signedness
= IR_SIGNED
;
415 struct ir_op
*make_op_binary_eq(struct ir_op
*left
, struct ir_op
*right
,
418 return make_op_binary_compare(AST_OP_EQ
, "==", left
, right
, side
);
422 struct ir_op
*make_op_binary_ne(struct ir_op
*left
, struct ir_op
*right
,
425 return make_op_binary_compare(AST_OP_NE
, "!=", left
, right
, side
);
429 struct ir_op
*make_op_binary_gt(struct ir_op
*left
, struct ir_op
*right
,
432 return make_op_binary_compare(AST_OP_GT
, ">", left
, right
, side
);
436 struct ir_op
*make_op_binary_lt(struct ir_op
*left
, struct ir_op
*right
,
439 return make_op_binary_compare(AST_OP_LT
, "<", left
, right
, side
);
443 struct ir_op
*make_op_binary_ge(struct ir_op
*left
, struct ir_op
*right
,
446 return make_op_binary_compare(AST_OP_GE
, ">=", left
, right
, side
);
450 struct ir_op
*make_op_binary_le(struct ir_op
*left
, struct ir_op
*right
,
453 return make_op_binary_compare(AST_OP_LE
, "<=", left
, right
, side
);
457 struct ir_op
*make_op_binary_logical(enum op_type bin_op_type
,
458 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
461 struct ir_op
*op
= NULL
;
463 if (left
->data_type
== IR_DATA_UNKNOWN
464 || right
->data_type
== IR_DATA_UNKNOWN
) {
465 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
469 if (left
->data_type
== IR_DATA_STRING
470 || right
->data_type
== IR_DATA_STRING
) {
471 fprintf(stderr
, "[error] logical binary operation '%s' cannot have string operand\n", op_str
);
475 op
= calloc(sizeof(struct ir_op
), 1);
478 op
->op
= IR_OP_LOGICAL
;
479 op
->u
.binary
.type
= bin_op_type
;
480 op
->u
.binary
.left
= left
;
481 op
->u
.binary
.right
= right
;
483 /* we return a boolean, represented as signed numeric */
484 op
->data_type
= IR_DATA_NUMERIC
;
485 op
->signedness
= IR_SIGNED
;
496 struct ir_op
*make_op_binary_bitwise(enum op_type bin_op_type
,
497 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
500 struct ir_op
*op
= NULL
;
502 if (left
->data_type
== IR_DATA_UNKNOWN
503 || right
->data_type
== IR_DATA_UNKNOWN
) {
504 fprintf(stderr
, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str
);
508 if (left
->data_type
== IR_DATA_STRING
509 || right
->data_type
== IR_DATA_STRING
) {
510 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have string operand\n", op_str
);
513 if (left
->data_type
== IR_DATA_FLOAT
514 || right
->data_type
== IR_DATA_FLOAT
) {
515 fprintf(stderr
, "[error] bitwise binary operation '%s' cannot have floating point operand\n", op_str
);
519 op
= calloc(sizeof(struct ir_op
), 1);
522 op
->op
= IR_OP_BINARY
;
523 op
->u
.binary
.type
= bin_op_type
;
524 op
->u
.binary
.left
= left
;
525 op
->u
.binary
.right
= right
;
527 /* we return a signed numeric */
528 op
->data_type
= IR_DATA_NUMERIC
;
529 op
->signedness
= IR_SIGNED
;
540 struct ir_op
*make_op_binary_logical_and(struct ir_op
*left
, struct ir_op
*right
,
543 return make_op_binary_logical(AST_OP_AND
, "&&", left
, right
, side
);
547 struct ir_op
*make_op_binary_logical_or(struct ir_op
*left
, struct ir_op
*right
,
550 return make_op_binary_logical(AST_OP_OR
, "||", left
, right
, side
);
554 struct ir_op
*make_op_binary_bitwise_rshift(struct ir_op
*left
, struct ir_op
*right
,
557 return make_op_binary_bitwise(AST_OP_BIT_RSHIFT
, ">>", left
, right
, side
);
561 struct ir_op
*make_op_binary_bitwise_lshift(struct ir_op
*left
, struct ir_op
*right
,
564 return make_op_binary_bitwise(AST_OP_BIT_LSHIFT
, "<<", left
, right
, side
);
568 struct ir_op
*make_op_binary_bitwise_and(struct ir_op
*left
, struct ir_op
*right
,
571 return make_op_binary_bitwise(AST_OP_BIT_AND
, "&", left
, right
, side
);
575 struct ir_op
*make_op_binary_bitwise_or(struct ir_op
*left
, struct ir_op
*right
,
578 return make_op_binary_bitwise(AST_OP_BIT_OR
, "|", left
, right
, side
);
582 struct ir_op
*make_op_binary_bitwise_xor(struct ir_op
*left
, struct ir_op
*right
,
585 return make_op_binary_bitwise(AST_OP_BIT_XOR
, "^", left
, right
, side
);
589 void filter_free_ir_recursive(struct ir_op
*op
)
596 fprintf(stderr
, "[error] Unknown op type in %s\n",
600 filter_free_ir_recursive(op
->u
.root
.child
);
603 switch (op
->data_type
) {
605 free(op
->u
.load
.u
.string
.value
);
607 case IR_DATA_FIELD_REF
: /* fall-through */
608 case IR_DATA_GET_CONTEXT_REF
:
609 free(op
->u
.load
.u
.ref
);
611 case IR_DATA_EXPRESSION
:
612 free_load_expression(op
->u
.load
.u
.expression
);
618 filter_free_ir_recursive(op
->u
.unary
.child
);
621 filter_free_ir_recursive(op
->u
.binary
.left
);
622 filter_free_ir_recursive(op
->u
.binary
.right
);
625 filter_free_ir_recursive(op
->u
.logical
.left
);
626 filter_free_ir_recursive(op
->u
.logical
.right
);
633 struct ir_op
*make_expression(struct filter_parser_ctx
*ctx
,
634 struct filter_node
*node
, enum ir_side side
)
636 switch (node
->u
.expression
.type
) {
637 case AST_EXP_UNKNOWN
:
639 fprintf(stderr
, "[error] %s: unknown expression type\n", __func__
);
643 return make_op_load_string(node
->u
.expression
.u
.string
, side
);
644 case AST_EXP_CONSTANT
:
645 return make_op_load_numeric(node
->u
.expression
.u
.constant
,
647 case AST_EXP_FLOAT_CONSTANT
:
648 return make_op_load_float(node
->u
.expression
.u
.float_constant
,
650 case AST_EXP_IDENTIFIER
:
651 case AST_EXP_GLOBAL_IDENTIFIER
:
652 return make_op_load_expression(node
, side
);
654 return generate_ir_recursive(ctx
, node
->u
.expression
.u
.child
,
660 struct ir_op
*make_op(struct filter_parser_ctx
*ctx
,
661 struct filter_node
*node
, enum ir_side side
)
663 struct ir_op
*op
= NULL
, *lchild
, *rchild
;
664 const char *op_str
= "?";
666 switch (node
->u
.op
.type
) {
669 fprintf(stderr
, "[error] %s: unknown binary op type\n", __func__
);
673 * The following binary operators other than comparators and
674 * logical and/or are not supported yet.
678 goto error_not_supported
;
681 goto error_not_supported
;
684 goto error_not_supported
;
687 goto error_not_supported
;
690 goto error_not_supported
;
692 case AST_OP_BIT_RSHIFT
:
693 case AST_OP_BIT_LSHIFT
:
697 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
700 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
702 filter_free_ir_recursive(lchild
);
713 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
716 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
718 filter_free_ir_recursive(lchild
);
726 * Both children considered as left, since we need to
729 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
732 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_LEFT
);
734 filter_free_ir_recursive(lchild
);
740 switch (node
->u
.op
.type
) {
742 op
= make_op_binary_logical_and(lchild
, rchild
, side
);
745 op
= make_op_binary_logical_or(lchild
, rchild
, side
);
748 op
= make_op_binary_eq(lchild
, rchild
, side
);
751 op
= make_op_binary_ne(lchild
, rchild
, side
);
754 op
= make_op_binary_gt(lchild
, rchild
, side
);
757 op
= make_op_binary_lt(lchild
, rchild
, side
);
760 op
= make_op_binary_ge(lchild
, rchild
, side
);
763 op
= make_op_binary_le(lchild
, rchild
, side
);
765 case AST_OP_BIT_RSHIFT
:
766 op
= make_op_binary_bitwise_rshift(lchild
, rchild
, side
);
768 case AST_OP_BIT_LSHIFT
:
769 op
= make_op_binary_bitwise_lshift(lchild
, rchild
, side
);
772 op
= make_op_binary_bitwise_and(lchild
, rchild
, side
);
775 op
= make_op_binary_bitwise_or(lchild
, rchild
, side
);
778 op
= make_op_binary_bitwise_xor(lchild
, rchild
, side
);
785 filter_free_ir_recursive(rchild
);
786 filter_free_ir_recursive(lchild
);
791 fprintf(stderr
, "[error] %s: binary operation '%s' not supported\n",
797 struct ir_op
*make_unary_op(struct filter_parser_ctx
*ctx
,
798 struct filter_node
*node
, enum ir_side side
)
800 switch (node
->u
.unary_op
.type
) {
801 case AST_UNARY_UNKNOWN
:
803 fprintf(stderr
, "[error] %s: unknown unary op type\n", __func__
);
808 struct ir_op
*op
, *child
;
810 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
814 op
= make_op_unary_plus(child
, side
);
816 filter_free_ir_recursive(child
);
821 case AST_UNARY_MINUS
:
823 struct ir_op
*op
, *child
;
825 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
829 op
= make_op_unary_minus(child
, side
);
831 filter_free_ir_recursive(child
);
838 struct ir_op
*op
, *child
;
840 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
844 op
= make_op_unary_not(child
, side
);
846 filter_free_ir_recursive(child
);
851 case AST_UNARY_BIT_NOT
:
853 struct ir_op
*op
, *child
;
855 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
859 op
= make_op_unary_bit_not(child
, side
);
861 filter_free_ir_recursive(child
);
872 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
873 struct filter_node
*node
, enum ir_side side
)
875 switch (node
->type
) {
878 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
883 struct ir_op
*op
, *child
;
885 child
= generate_ir_recursive(ctx
, node
->u
.root
.child
,
889 op
= make_op_root(child
, side
);
891 filter_free_ir_recursive(child
);
896 case NODE_EXPRESSION
:
897 return make_expression(ctx
, node
, side
);
899 return make_op(ctx
, node
, side
);
901 return make_unary_op(ctx
, node
, side
);
907 void filter_ir_free(struct filter_parser_ctx
*ctx
)
909 filter_free_ir_recursive(ctx
->ir_root
);
914 int filter_visitor_ir_generate(struct filter_parser_ctx
*ctx
)
918 op
= generate_ir_recursive(ctx
, &ctx
->ast
->root
, IR_LEFT
);