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
:
66 op
->data_type
= child
->data_type
;
67 op
->signedness
= child
->signedness
;
68 op
->u
.root
.child
= child
;
73 enum ir_load_string_type
get_literal_string_type(const char *string
)
77 if (strutils_is_star_glob_pattern(string
)) {
78 if (strutils_is_star_at_the_end_only_glob_pattern(string
)) {
79 return IR_LOAD_STRING_TYPE_GLOB_STAR_END
;
82 return IR_LOAD_STRING_TYPE_GLOB_STAR
;
85 return IR_LOAD_STRING_TYPE_PLAIN
;
89 struct ir_op
*make_op_load_string(char *string
, enum ir_side side
)
93 op
= calloc(sizeof(struct ir_op
), 1);
97 op
->data_type
= IR_DATA_STRING
;
98 op
->signedness
= IR_SIGN_UNKNOWN
;
100 op
->u
.load
.u
.string
.type
= get_literal_string_type(string
);
101 op
->u
.load
.u
.string
.value
= strdup(string
);
102 if (!op
->u
.load
.u
.string
.value
) {
110 struct ir_op
*make_op_load_numeric(int64_t v
, enum ir_side side
)
114 op
= calloc(sizeof(struct ir_op
), 1);
118 op
->data_type
= IR_DATA_NUMERIC
;
119 /* TODO: for now, all numeric values are signed */
120 op
->signedness
= IR_SIGNED
;
122 op
->u
.load
.u
.num
= v
;
127 struct ir_op
*make_op_load_float(double v
, enum ir_side side
)
131 op
= calloc(sizeof(struct ir_op
), 1);
135 op
->data_type
= IR_DATA_FLOAT
;
136 op
->signedness
= IR_SIGN_UNKNOWN
;
138 op
->u
.load
.u
.flt
= v
;
143 struct ir_op
*make_op_load_field_ref(char *string
, enum ir_side side
)
147 op
= calloc(sizeof(struct ir_op
), 1);
151 op
->data_type
= IR_DATA_FIELD_REF
;
152 op
->signedness
= IR_SIGN_DYN
;
154 op
->u
.load
.u
.ref
= strdup(string
);
155 if (!op
->u
.load
.u
.ref
) {
163 struct ir_op
*make_op_load_get_context_ref(char *string
, enum ir_side side
)
167 op
= calloc(sizeof(struct ir_op
), 1);
171 op
->data_type
= IR_DATA_GET_CONTEXT_REF
;
172 op
->signedness
= IR_SIGN_DYN
;
174 op
->u
.load
.u
.ref
= strdup(string
);
175 if (!op
->u
.load
.u
.ref
) {
183 struct ir_op
*make_op_unary(enum unary_op_type unary_op_type
,
184 const char *op_str
, enum ir_op_signedness signedness
,
185 struct ir_op
*child
, enum ir_side side
)
187 struct ir_op
*op
= NULL
;
189 if (child
->data_type
== IR_DATA_STRING
) {
190 fprintf(stderr
, "[error] unary operation '%s' not allowed on string literal\n", op_str
);
194 op
= calloc(sizeof(struct ir_op
), 1);
197 op
->op
= IR_OP_UNARY
;
198 op
->data_type
= child
->data_type
;
199 op
->signedness
= signedness
;
201 op
->u
.unary
.type
= unary_op_type
;
202 op
->u
.unary
.child
= child
;
211 * unary + is pretty much useless.
214 struct ir_op
*make_op_unary_plus(struct ir_op
*child
, enum ir_side side
)
216 return make_op_unary(AST_UNARY_PLUS
, "+", child
->signedness
,
221 struct ir_op
*make_op_unary_minus(struct ir_op
*child
, enum ir_side side
)
223 return make_op_unary(AST_UNARY_MINUS
, "-", child
->signedness
,
228 struct ir_op
*make_op_unary_not(struct ir_op
*child
, enum ir_side side
)
230 return make_op_unary(AST_UNARY_NOT
, "!", child
->signedness
,
235 struct ir_op
*make_op_binary_compare(enum op_type bin_op_type
,
236 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
239 struct ir_op
*op
= NULL
;
241 if (left
->data_type
== IR_DATA_UNKNOWN
242 || right
->data_type
== IR_DATA_UNKNOWN
) {
243 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
247 if ((left
->data_type
== IR_DATA_STRING
248 && (right
->data_type
== IR_DATA_NUMERIC
|| right
->data_type
== IR_DATA_FLOAT
))
249 || ((left
->data_type
== IR_DATA_NUMERIC
|| left
->data_type
== IR_DATA_FLOAT
) &&
250 right
->data_type
== IR_DATA_STRING
)) {
251 fprintf(stderr
, "[error] binary operation '%s' operand type mismatch\n", op_str
);
255 op
= calloc(sizeof(struct ir_op
), 1);
258 op
->op
= IR_OP_BINARY
;
259 op
->u
.binary
.type
= bin_op_type
;
260 op
->u
.binary
.left
= left
;
261 op
->u
.binary
.right
= right
;
263 /* we return a boolean, represented as signed numeric */
264 op
->data_type
= IR_DATA_NUMERIC
;
265 op
->signedness
= IR_SIGNED
;
276 struct ir_op
*make_op_binary_eq(struct ir_op
*left
, struct ir_op
*right
,
279 return make_op_binary_compare(AST_OP_EQ
, "==", left
, right
, side
);
283 struct ir_op
*make_op_binary_ne(struct ir_op
*left
, struct ir_op
*right
,
286 return make_op_binary_compare(AST_OP_NE
, "!=", left
, right
, side
);
290 struct ir_op
*make_op_binary_gt(struct ir_op
*left
, struct ir_op
*right
,
293 return make_op_binary_compare(AST_OP_GT
, ">", left
, right
, side
);
297 struct ir_op
*make_op_binary_lt(struct ir_op
*left
, struct ir_op
*right
,
300 return make_op_binary_compare(AST_OP_LT
, "<", left
, right
, side
);
304 struct ir_op
*make_op_binary_ge(struct ir_op
*left
, struct ir_op
*right
,
307 return make_op_binary_compare(AST_OP_GE
, ">=", left
, right
, side
);
311 struct ir_op
*make_op_binary_le(struct ir_op
*left
, struct ir_op
*right
,
314 return make_op_binary_compare(AST_OP_LE
, "<=", left
, right
, side
);
318 struct ir_op
*make_op_binary_logical(enum op_type bin_op_type
,
319 const char *op_str
, struct ir_op
*left
, struct ir_op
*right
,
322 struct ir_op
*op
= NULL
;
324 if (left
->data_type
== IR_DATA_UNKNOWN
325 || right
->data_type
== IR_DATA_UNKNOWN
) {
326 fprintf(stderr
, "[error] binary operation '%s' has unknown operand type\n", op_str
);
330 if (left
->data_type
== IR_DATA_STRING
331 || right
->data_type
== IR_DATA_STRING
) {
332 fprintf(stderr
, "[error] logical binary operation '%s' cannot have string operand\n", op_str
);
336 op
= calloc(sizeof(struct ir_op
), 1);
339 op
->op
= IR_OP_LOGICAL
;
340 op
->u
.binary
.type
= bin_op_type
;
341 op
->u
.binary
.left
= left
;
342 op
->u
.binary
.right
= right
;
344 /* we return a boolean, represented as signed numeric */
345 op
->data_type
= IR_DATA_NUMERIC
;
346 op
->signedness
= IR_SIGNED
;
357 struct ir_op
*make_op_binary_logical_and(struct ir_op
*left
, struct ir_op
*right
,
360 return make_op_binary_logical(AST_OP_AND
, "&&", left
, right
, side
);
364 struct ir_op
*make_op_binary_logical_or(struct ir_op
*left
, struct ir_op
*right
,
367 return make_op_binary_logical(AST_OP_OR
, "||", left
, right
, side
);
371 void filter_free_ir_recursive(struct ir_op
*op
)
378 fprintf(stderr
, "[error] Unknown op type in %s\n",
382 filter_free_ir_recursive(op
->u
.root
.child
);
385 switch (op
->data_type
) {
387 free(op
->u
.load
.u
.string
.value
);
389 case IR_DATA_FIELD_REF
: /* fall-through */
390 case IR_DATA_GET_CONTEXT_REF
:
391 free(op
->u
.load
.u
.ref
);
398 filter_free_ir_recursive(op
->u
.unary
.child
);
401 filter_free_ir_recursive(op
->u
.binary
.left
);
402 filter_free_ir_recursive(op
->u
.binary
.right
);
405 filter_free_ir_recursive(op
->u
.logical
.left
);
406 filter_free_ir_recursive(op
->u
.logical
.right
);
413 struct ir_op
*make_expression(struct filter_parser_ctx
*ctx
,
414 struct filter_node
*node
, enum ir_side side
)
416 switch (node
->u
.expression
.type
) {
417 case AST_EXP_UNKNOWN
:
419 fprintf(stderr
, "[error] %s: unknown expression type\n", __func__
);
423 return make_op_load_string(node
->u
.expression
.u
.string
, side
);
424 case AST_EXP_CONSTANT
:
425 return make_op_load_numeric(node
->u
.expression
.u
.constant
,
427 case AST_EXP_FLOAT_CONSTANT
:
428 return make_op_load_float(node
->u
.expression
.u
.float_constant
,
430 case AST_EXP_IDENTIFIER
:
431 if (node
->u
.expression
.pre_op
!= AST_LINK_UNKNOWN
) {
432 fprintf(stderr
, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__
);
435 return make_op_load_field_ref(node
->u
.expression
.u
.identifier
,
437 case AST_EXP_GLOBAL_IDENTIFIER
:
442 * We currently only support $ctx (context) and $app
445 if (strncmp(node
->u
.expression
.u
.identifier
,
446 "$ctx.", strlen("$ctx.")) != 0
447 && strncmp(node
->u
.expression
.u
.identifier
,
448 "$app.", strlen("$app.")) != 0) {
449 fprintf(stderr
, "[error] %s: \"%s\" global identifier is unknown. Only \"$ctx\" and \"$app\" are currently implemented.\n", __func__
, node
->u
.expression
.u
.identifier
);
452 name
= strchr(node
->u
.expression
.u
.identifier
, '.');
454 fprintf(stderr
, "[error] %s: Expecting '.'\n", __func__
);
459 fprintf(stderr
, "[error] %s: Expecting a context name, e.g. \'$ctx.name\'.\n", __func__
);
462 return make_op_load_get_context_ref(node
->u
.expression
.u
.identifier
,
466 return generate_ir_recursive(ctx
, node
->u
.expression
.u
.child
,
472 struct ir_op
*make_op(struct filter_parser_ctx
*ctx
,
473 struct filter_node
*node
, enum ir_side side
)
475 struct ir_op
*op
= NULL
, *lchild
, *rchild
;
476 const char *op_str
= "?";
478 switch (node
->u
.op
.type
) {
481 fprintf(stderr
, "[error] %s: unknown binary op type\n", __func__
);
485 * Binary operators other than comparators and logical and/or
486 * are not supported. If we ever want to support those, we will
487 * need a stack for the general case rather than just 2
488 * registers (see bytecode).
492 goto error_not_supported
;
495 goto error_not_supported
;
498 goto error_not_supported
;
501 goto error_not_supported
;
504 goto error_not_supported
;
507 goto error_not_supported
;
510 goto error_not_supported
;
513 goto error_not_supported
;
516 goto error_not_supported
;
519 goto error_not_supported
;
527 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
530 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_RIGHT
);
532 filter_free_ir_recursive(lchild
);
540 * Both children considered as left, since we need to
543 lchild
= generate_ir_recursive(ctx
, node
->u
.op
.lchild
, IR_LEFT
);
546 rchild
= generate_ir_recursive(ctx
, node
->u
.op
.rchild
, IR_LEFT
);
548 filter_free_ir_recursive(lchild
);
554 switch (node
->u
.op
.type
) {
556 op
= make_op_binary_logical_and(lchild
, rchild
, side
);
559 op
= make_op_binary_logical_or(lchild
, rchild
, side
);
562 op
= make_op_binary_eq(lchild
, rchild
, side
);
565 op
= make_op_binary_ne(lchild
, rchild
, side
);
568 op
= make_op_binary_gt(lchild
, rchild
, side
);
571 op
= make_op_binary_lt(lchild
, rchild
, side
);
574 op
= make_op_binary_ge(lchild
, rchild
, side
);
577 op
= make_op_binary_le(lchild
, rchild
, side
);
584 filter_free_ir_recursive(rchild
);
585 filter_free_ir_recursive(lchild
);
590 fprintf(stderr
, "[error] %s: binary operation '%s' not supported\n",
596 struct ir_op
*make_unary_op(struct filter_parser_ctx
*ctx
,
597 struct filter_node
*node
, enum ir_side side
)
599 const char *op_str
= "?";
601 switch (node
->u
.unary_op
.type
) {
602 case AST_UNARY_UNKNOWN
:
604 fprintf(stderr
, "[error] %s: unknown unary op type\n", __func__
);
609 struct ir_op
*op
, *child
;
611 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
615 op
= make_op_unary_plus(child
, side
);
617 filter_free_ir_recursive(child
);
622 case AST_UNARY_MINUS
:
624 struct ir_op
*op
, *child
;
626 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
630 op
= make_op_unary_minus(child
, side
);
632 filter_free_ir_recursive(child
);
639 struct ir_op
*op
, *child
;
641 child
= generate_ir_recursive(ctx
, node
->u
.unary_op
.child
,
645 op
= make_op_unary_not(child
, side
);
647 filter_free_ir_recursive(child
);
652 case AST_UNARY_BIN_NOT
:
655 goto error_not_supported
;
660 fprintf(stderr
, "[error] %s: unary operation '%s' not supported\n",
666 struct ir_op
*generate_ir_recursive(struct filter_parser_ctx
*ctx
,
667 struct filter_node
*node
, enum ir_side side
)
669 switch (node
->type
) {
672 fprintf(stderr
, "[error] %s: unknown node type\n", __func__
);
677 struct ir_op
*op
, *child
;
679 child
= generate_ir_recursive(ctx
, node
->u
.root
.child
,
683 op
= make_op_root(child
, side
);
685 filter_free_ir_recursive(child
);
690 case NODE_EXPRESSION
:
691 return make_expression(ctx
, node
, side
);
693 return make_op(ctx
, node
, side
);
695 return make_unary_op(ctx
, node
, side
);
701 void filter_ir_free(struct filter_parser_ctx
*ctx
)
703 filter_free_ir_recursive(ctx
->ir_root
);
708 int filter_visitor_ir_generate(struct filter_parser_ctx
*ctx
)
712 op
= generate_ir_recursive(ctx
, &ctx
->ast
->root
, IR_LEFT
);