Filter: Implement rshift, lshift, bit not operators
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-visitor-generate-ir.c
CommitLineData
953192ba
MD
1/*
2 * filter-visitor-generate-ir.c
3 *
4 * LTTng filter generate intermediate representation
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
11 *
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.
16 *
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
20 */
21
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdlib.h>
26#include <assert.h>
27#include <errno.h>
28#include <inttypes.h>
953192ba 29#include "filter-ast.h"
95b9bd90 30#include "filter-parser.h"
953192ba
MD
31#include "filter-ir.h"
32
a187da1a 33#include <common/macros.h>
9f449915 34#include <common/string-utils/string-utils.h>
a187da1a 35
953192ba
MD
36static
37struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx,
38 struct filter_node *node, enum ir_side side);
39
40static
41struct ir_op *make_op_root(struct ir_op *child, enum ir_side side)
42{
43 struct ir_op *op;
44
45 op = calloc(sizeof(struct ir_op), 1);
46 if (!op)
47 return NULL;
48 switch (child->data_type) {
49 case IR_DATA_UNKNOWN:
50 default:
51 fprintf(stderr, "[error] Unknown root child data type\n");
7d8868f9 52 free(op);
953192ba
MD
53 return NULL;
54 case IR_DATA_STRING:
55 fprintf(stderr, "[error] String cannot be root data type\n");
7d8868f9 56 free(op);
953192ba
MD
57 return NULL;
58 case IR_DATA_NUMERIC:
59 case IR_DATA_FIELD_REF:
586dc72f 60 case IR_DATA_GET_CONTEXT_REF:
87942d06 61 case IR_DATA_EXPRESSION:
953192ba
MD
62 /* ok */
63 break;
64 }
65 op->op = IR_OP_ROOT;
66 op->side = side;
67 op->data_type = child->data_type;
68 op->signedness = child->signedness;
69 op->u.root.child = child;
70 return op;
71}
72
9f449915
PP
73static
74enum ir_load_string_type get_literal_string_type(const char *string)
75{
76 assert(string);
77
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;
81 }
82
83 return IR_LOAD_STRING_TYPE_GLOB_STAR;
84 }
85
86 return IR_LOAD_STRING_TYPE_PLAIN;
87}
88
953192ba
MD
89static
90struct ir_op *make_op_load_string(char *string, enum ir_side side)
91{
92 struct ir_op *op;
93
94 op = calloc(sizeof(struct ir_op), 1);
95 if (!op)
96 return NULL;
97 op->op = IR_OP_LOAD;
98 op->data_type = IR_DATA_STRING;
99 op->signedness = IR_SIGN_UNKNOWN;
100 op->side = side;
9f449915
PP
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) {
953192ba
MD
104 free(op);
105 return NULL;
106 }
107 return op;
108}
109
110static
111struct ir_op *make_op_load_numeric(int64_t v, enum ir_side side)
112{
113 struct ir_op *op;
114
115 op = calloc(sizeof(struct ir_op), 1);
116 if (!op)
117 return NULL;
118 op->op = IR_OP_LOAD;
119 op->data_type = IR_DATA_NUMERIC;
120 /* TODO: for now, all numeric values are signed */
121 op->signedness = IR_SIGNED;
122 op->side = side;
123 op->u.load.u.num = v;
124 return op;
125}
126
e90d8561
MD
127static
128struct ir_op *make_op_load_float(double v, enum ir_side side)
129{
130 struct ir_op *op;
131
132 op = calloc(sizeof(struct ir_op), 1);
133 if (!op)
134 return NULL;
135 op->op = IR_OP_LOAD;
136 op->data_type = IR_DATA_FLOAT;
137 op->signedness = IR_SIGN_UNKNOWN;
138 op->side = side;
139 op->u.load.u.flt = v;
140 return op;
141}
142
87942d06 143#if 0
953192ba 144static
5590fc2c
MD
145struct ir_op *make_op_load_field_ref(char *string,
146 enum ir_side side)
953192ba
MD
147{
148 struct ir_op *op;
149
150 op = calloc(sizeof(struct ir_op), 1);
151 if (!op)
152 return NULL;
153 op->op = IR_OP_LOAD;
154 op->data_type = IR_DATA_FIELD_REF;
155 op->signedness = IR_SIGN_DYN;
156 op->side = side;
157 op->u.load.u.ref = strdup(string);
158 if (!op->u.load.u.ref) {
5590fc2c
MD
159 goto error;
160 }
161 return op;
162
163error:
164 free(op);
165 return NULL;
166}
87942d06 167#endif
5590fc2c
MD
168
169static
87942d06 170void free_load_expression(struct ir_load_expression *load_expression)
5590fc2c 171{
87942d06 172 struct ir_load_expression_op *exp_op;
5590fc2c 173
87942d06
MD
174 if (!load_expression)
175 return;
176 exp_op = load_expression->child;
177 for (;;) {
178 struct ir_load_expression_op *prev_exp_op;
179
180 if (!exp_op)
181 break;
182 switch (exp_op->type) {
183 case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT:
184 case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT:
185 case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT:
186 case IR_LOAD_EXPRESSION_GET_INDEX:
187 case IR_LOAD_EXPRESSION_LOAD_FIELD:
188 break;
189 case IR_LOAD_EXPRESSION_GET_SYMBOL:
190 free(exp_op->u.symbol);
191 break;
192 }
193 prev_exp_op = exp_op;
194 exp_op = exp_op->next;
195 free(prev_exp_op);
953192ba 196 }
87942d06
MD
197 free(load_expression);
198}
5590fc2c 199
87942d06
MD
200/*
201 * Returns the first node of the chain, after initializing the next
202 * pointers.
203 */
204static
205struct filter_node *load_expression_get_forward_chain(struct filter_node *node)
206{
207 struct filter_node *prev_node;
208
209 for (;;) {
210 assert(node->type == NODE_EXPRESSION);
211 prev_node = node;
212 node = node->u.expression.prev;
213 if (!node) {
214 break;
215 }
216 node->u.expression.next = prev_node;
217 }
218 return prev_node;
953192ba
MD
219}
220
586dc72f 221static
87942d06 222struct ir_load_expression *create_load_expression(struct filter_node *node)
586dc72f 223{
87942d06
MD
224 struct ir_load_expression *load_exp;
225 struct ir_load_expression_op *load_exp_op, *prev_op;
226 char *str;
586dc72f 227
87942d06
MD
228 /* Get forward chain. */
229 node = load_expression_get_forward_chain(node);
230 if (!node)
586dc72f 231 return NULL;
87942d06
MD
232 load_exp = calloc(sizeof(struct ir_load_expression), 1);
233 if (!load_exp)
234 return NULL;
235
236 /* Root */
237 load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1);
238 if (!load_exp_op)
5590fc2c 239 goto error;
87942d06
MD
240 load_exp->child = load_exp_op;
241 str = node->u.expression.u.string;
242 if (!strcmp(str, "$ctx")) {
243 load_exp_op->type = IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT;
244 node = node->u.expression.next;
245 if (!node) {
246 fprintf(stderr, "[error] Expecting identifier after \'%s\'\n", str);
247 goto error;
248 }
249 str = node->u.expression.u.string;
250 } else if (!strcmp(str, "$app")) {
251 load_exp_op->type = IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT;
252 node = node->u.expression.next;
253 if (!node) {
254 fprintf(stderr, "[error] Expecting identifier after \'%s\'\n", str);
255 goto error;
256 }
257 str = node->u.expression.u.string;
258 } else if (str[0] == '$') {
259 fprintf(stderr, "[error] Unexpected identifier \'%s\'\n", str);
260 goto error;
261 } else {
262 load_exp_op->type = IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT;
5590fc2c 263 }
87942d06
MD
264
265 for (;;) {
266 struct filter_node *bracket_node;
267
268 prev_op = load_exp_op;
269 load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1);
270 if (!load_exp_op)
271 goto error;
272 prev_op->next = load_exp_op;
273 load_exp_op->type = IR_LOAD_EXPRESSION_GET_SYMBOL;
274 load_exp_op->u.symbol = strdup(str);
275 if (!load_exp_op->u.symbol)
276 goto error;
277
278 /* Explore brackets from current node. */
279 for (bracket_node = node->u.expression.next_bracket;
280 bracket_node != NULL;
281 bracket_node = bracket_node->u.expression.next_bracket) {
282 prev_op = load_exp_op;
283 load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1);
284 if (!load_exp_op)
285 goto error;
286 prev_op->next = load_exp_op;
287 load_exp_op->type = IR_LOAD_EXPRESSION_GET_INDEX;
288 load_exp_op->u.index = bracket_node->u.expression.u.constant;
289 }
290 /* Go to next chain element. */
291 node = node->u.expression.next;
292 if (!node)
293 break;
294 str = node->u.expression.u.string;
295 }
296 /* Add final load field */
297 prev_op = load_exp_op;
298 load_exp_op = calloc(sizeof(struct ir_load_expression_op), 1);
299 if (!load_exp_op)
300 goto error;
301 prev_op->next = load_exp_op;
302 load_exp_op->type = IR_LOAD_EXPRESSION_LOAD_FIELD;
303 return load_exp;
5590fc2c
MD
304
305error:
87942d06 306 free_load_expression(load_exp);
5590fc2c
MD
307 return NULL;
308}
309
310static
87942d06 311struct ir_op *make_op_load_expression(struct filter_node *node,
5590fc2c
MD
312 enum ir_side side)
313{
314 struct ir_op *op;
315
316 op = calloc(sizeof(struct ir_op), 1);
317 if (!op)
586dc72f 318 return NULL;
5590fc2c 319 op->op = IR_OP_LOAD;
87942d06 320 op->data_type = IR_DATA_EXPRESSION;
5590fc2c
MD
321 op->signedness = IR_SIGN_DYN;
322 op->side = side;
87942d06
MD
323 op->u.load.u.expression = create_load_expression(node);
324 if (!op->u.load.u.expression) {
5590fc2c
MD
325 goto error;
326 }
586dc72f 327 return op;
5590fc2c
MD
328
329error:
87942d06 330 free_load_expression(op->u.load.u.expression);
5590fc2c
MD
331 free(op);
332 return NULL;
586dc72f
MD
333}
334
953192ba
MD
335static
336struct ir_op *make_op_unary(enum unary_op_type unary_op_type,
337 const char *op_str, enum ir_op_signedness signedness,
338 struct ir_op *child, enum ir_side side)
339{
340 struct ir_op *op = NULL;
341
342 if (child->data_type == IR_DATA_STRING) {
343 fprintf(stderr, "[error] unary operation '%s' not allowed on string literal\n", op_str);
344 goto error;
345 }
346
347 op = calloc(sizeof(struct ir_op), 1);
348 if (!op)
349 return NULL;
350 op->op = IR_OP_UNARY;
351 op->data_type = child->data_type;
352 op->signedness = signedness;
353 op->side = side;
354 op->u.unary.type = unary_op_type;
355 op->u.unary.child = child;
356 return op;
357
358error:
359 free(op);
360 return NULL;
361}
362
363/*
364 * unary + is pretty much useless.
365 */
366static
367struct ir_op *make_op_unary_plus(struct ir_op *child, enum ir_side side)
368{
369 return make_op_unary(AST_UNARY_PLUS, "+", child->signedness,
370 child, side);
371}
372
373static
374struct ir_op *make_op_unary_minus(struct ir_op *child, enum ir_side side)
375{
376 return make_op_unary(AST_UNARY_MINUS, "-", child->signedness,
377 child, side);
378}
379
380static
381struct ir_op *make_op_unary_not(struct ir_op *child, enum ir_side side)
382{
383 return make_op_unary(AST_UNARY_NOT, "!", child->signedness,
384 child, side);
385}
386
26107045
MD
387static
388struct ir_op *make_op_unary_bit_not(struct ir_op *child, enum ir_side side)
389{
390 return make_op_unary(AST_UNARY_BIT_NOT, "~", child->signedness,
391 child, side);
392}
393
953192ba
MD
394static
395struct ir_op *make_op_binary_compare(enum op_type bin_op_type,
396 const char *op_str, struct ir_op *left, struct ir_op *right,
397 enum ir_side side)
398{
399 struct ir_op *op = NULL;
400
401 if (left->data_type == IR_DATA_UNKNOWN
402 || right->data_type == IR_DATA_UNKNOWN) {
403 fprintf(stderr, "[error] binary operation '%s' has unknown operand type\n", op_str);
404 goto error;
405
406 }
407 if ((left->data_type == IR_DATA_STRING
e90d8561
MD
408 && (right->data_type == IR_DATA_NUMERIC || right->data_type == IR_DATA_FLOAT))
409 || ((left->data_type == IR_DATA_NUMERIC || left->data_type == IR_DATA_FLOAT) &&
953192ba
MD
410 right->data_type == IR_DATA_STRING)) {
411 fprintf(stderr, "[error] binary operation '%s' operand type mismatch\n", op_str);
412 goto error;
413 }
414
415 op = calloc(sizeof(struct ir_op), 1);
416 if (!op)
417 return NULL;
418 op->op = IR_OP_BINARY;
419 op->u.binary.type = bin_op_type;
420 op->u.binary.left = left;
421 op->u.binary.right = right;
422
423 /* we return a boolean, represented as signed numeric */
424 op->data_type = IR_DATA_NUMERIC;
425 op->signedness = IR_SIGNED;
426 op->side = side;
427
428 return op;
429
430error:
431 free(op);
432 return NULL;
433}
434
435static
436struct ir_op *make_op_binary_eq(struct ir_op *left, struct ir_op *right,
437 enum ir_side side)
438{
439 return make_op_binary_compare(AST_OP_EQ, "==", left, right, side);
440}
441
442static
443struct ir_op *make_op_binary_ne(struct ir_op *left, struct ir_op *right,
444 enum ir_side side)
445{
446 return make_op_binary_compare(AST_OP_NE, "!=", left, right, side);
447}
448
449static
450struct ir_op *make_op_binary_gt(struct ir_op *left, struct ir_op *right,
451 enum ir_side side)
452{
453 return make_op_binary_compare(AST_OP_GT, ">", left, right, side);
454}
455
456static
457struct ir_op *make_op_binary_lt(struct ir_op *left, struct ir_op *right,
458 enum ir_side side)
459{
460 return make_op_binary_compare(AST_OP_LT, "<", left, right, side);
461}
462
463static
464struct ir_op *make_op_binary_ge(struct ir_op *left, struct ir_op *right,
465 enum ir_side side)
466{
467 return make_op_binary_compare(AST_OP_GE, ">=", left, right, side);
468}
469
470static
471struct ir_op *make_op_binary_le(struct ir_op *left, struct ir_op *right,
472 enum ir_side side)
473{
474 return make_op_binary_compare(AST_OP_LE, "<=", left, right, side);
475}
476
477static
478struct ir_op *make_op_binary_logical(enum op_type bin_op_type,
479 const char *op_str, struct ir_op *left, struct ir_op *right,
480 enum ir_side side)
481{
482 struct ir_op *op = NULL;
483
484 if (left->data_type == IR_DATA_UNKNOWN
485 || right->data_type == IR_DATA_UNKNOWN) {
486 fprintf(stderr, "[error] binary operation '%s' has unknown operand type\n", op_str);
487 goto error;
488
489 }
490 if (left->data_type == IR_DATA_STRING
491 || right->data_type == IR_DATA_STRING) {
492 fprintf(stderr, "[error] logical binary operation '%s' cannot have string operand\n", op_str);
493 goto error;
494 }
495
496 op = calloc(sizeof(struct ir_op), 1);
497 if (!op)
498 return NULL;
499 op->op = IR_OP_LOGICAL;
500 op->u.binary.type = bin_op_type;
501 op->u.binary.left = left;
502 op->u.binary.right = right;
503
504 /* we return a boolean, represented as signed numeric */
505 op->data_type = IR_DATA_NUMERIC;
506 op->signedness = IR_SIGNED;
507 op->side = side;
508
509 return op;
510
511error:
512 free(op);
513 return NULL;
514}
515
87942d06
MD
516static
517struct ir_op *make_op_binary_bitwise(enum op_type bin_op_type,
518 const char *op_str, struct ir_op *left, struct ir_op *right,
519 enum ir_side side)
520{
521 struct ir_op *op = NULL;
522
523 if (left->data_type == IR_DATA_UNKNOWN
524 || right->data_type == IR_DATA_UNKNOWN) {
525 fprintf(stderr, "[error] bitwise binary operation '%s' has unknown operand type\n", op_str);
526 goto error;
527
528 }
529 if (left->data_type == IR_DATA_STRING
530 || right->data_type == IR_DATA_STRING) {
531 fprintf(stderr, "[error] bitwise binary operation '%s' cannot have string operand\n", op_str);
532 goto error;
533 }
534 if (left->data_type == IR_DATA_FLOAT
535 || right->data_type == IR_DATA_FLOAT) {
536 fprintf(stderr, "[error] bitwise binary operation '%s' cannot have floating point operand\n", op_str);
537 goto error;
538 }
539
540 op = calloc(sizeof(struct ir_op), 1);
541 if (!op)
542 return NULL;
543 op->op = IR_OP_BINARY;
544 op->u.binary.type = bin_op_type;
545 op->u.binary.left = left;
546 op->u.binary.right = right;
547
548 /* we return a signed numeric */
549 op->data_type = IR_DATA_NUMERIC;
550 op->signedness = IR_SIGNED;
551 op->side = side;
552
553 return op;
554
555error:
556 free(op);
557 return NULL;
558}
559
953192ba
MD
560static
561struct ir_op *make_op_binary_logical_and(struct ir_op *left, struct ir_op *right,
562 enum ir_side side)
563{
564 return make_op_binary_logical(AST_OP_AND, "&&", left, right, side);
565}
566
567static
568struct ir_op *make_op_binary_logical_or(struct ir_op *left, struct ir_op *right,
569 enum ir_side side)
570{
571 return make_op_binary_logical(AST_OP_OR, "||", left, right, side);
572}
573
26107045
MD
574static
575struct ir_op *make_op_binary_bitwise_rshift(struct ir_op *left, struct ir_op *right,
576 enum ir_side side)
577{
578 return make_op_binary_bitwise(AST_OP_BIT_RSHIFT, ">>", left, right, side);
579}
580
581static
582struct ir_op *make_op_binary_bitwise_lshift(struct ir_op *left, struct ir_op *right,
583 enum ir_side side)
584{
585 return make_op_binary_bitwise(AST_OP_BIT_LSHIFT, "<<", left, right, side);
586}
587
87942d06
MD
588static
589struct ir_op *make_op_binary_bitwise_and(struct ir_op *left, struct ir_op *right,
590 enum ir_side side)
591{
592 return make_op_binary_bitwise(AST_OP_BIT_AND, "&", left, right, side);
593}
594
595static
596struct ir_op *make_op_binary_bitwise_or(struct ir_op *left, struct ir_op *right,
597 enum ir_side side)
598{
599 return make_op_binary_bitwise(AST_OP_BIT_OR, "|", left, right, side);
600}
601
602static
603struct ir_op *make_op_binary_bitwise_xor(struct ir_op *left, struct ir_op *right,
604 enum ir_side side)
605{
606 return make_op_binary_bitwise(AST_OP_BIT_XOR, "^", left, right, side);
607}
608
953192ba
MD
609static
610void filter_free_ir_recursive(struct ir_op *op)
611{
612 if (!op)
613 return;
614 switch (op->op) {
615 case IR_OP_UNKNOWN:
616 default:
617 fprintf(stderr, "[error] Unknown op type in %s\n",
618 __func__);
619 break;
620 case IR_OP_ROOT:
621 filter_free_ir_recursive(op->u.root.child);
622 break;
623 case IR_OP_LOAD:
624 switch (op->data_type) {
625 case IR_DATA_STRING:
9f449915 626 free(op->u.load.u.string.value);
953192ba 627 break;
586dc72f
MD
628 case IR_DATA_FIELD_REF: /* fall-through */
629 case IR_DATA_GET_CONTEXT_REF:
953192ba
MD
630 free(op->u.load.u.ref);
631 break;
87942d06
MD
632 case IR_DATA_EXPRESSION:
633 free_load_expression(op->u.load.u.expression);
953192ba
MD
634 default:
635 break;
636 }
637 break;
638 case IR_OP_UNARY:
639 filter_free_ir_recursive(op->u.unary.child);
640 break;
641 case IR_OP_BINARY:
642 filter_free_ir_recursive(op->u.binary.left);
643 filter_free_ir_recursive(op->u.binary.right);
644 break;
645 case IR_OP_LOGICAL:
646 filter_free_ir_recursive(op->u.logical.left);
647 filter_free_ir_recursive(op->u.logical.right);
648 break;
649 }
650 free(op);
651}
652
653static
654struct ir_op *make_expression(struct filter_parser_ctx *ctx,
655 struct filter_node *node, enum ir_side side)
656{
657 switch (node->u.expression.type) {
658 case AST_EXP_UNKNOWN:
659 default:
660 fprintf(stderr, "[error] %s: unknown expression type\n", __func__);
661 return NULL;
662
663 case AST_EXP_STRING:
664 return make_op_load_string(node->u.expression.u.string, side);
665 case AST_EXP_CONSTANT:
666 return make_op_load_numeric(node->u.expression.u.constant,
667 side);
e90d8561
MD
668 case AST_EXP_FLOAT_CONSTANT:
669 return make_op_load_float(node->u.expression.u.float_constant,
670 side);
953192ba 671 case AST_EXP_IDENTIFIER:
87942d06
MD
672 case AST_EXP_GLOBAL_IDENTIFIER:
673 return make_op_load_expression(node, side);
674#if 0
5590fc2c
MD
675 switch (node->u.expression.pre_op) {
676 case AST_LINK_UNKNOWN:
677 return make_op_load_field_ref(node->u.expression.u.identifier,
678 side);
679 case AST_LINK_BRACKET:
680 return make_op_load_field_ref_index(node->u.expression.u.identifier,
681 node->u.expression.next,
682 side);
683 default:
953192ba
MD
684 fprintf(stderr, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__);
685 return NULL;
686 }
87942d06
MD
687#endif
688#if 0
586dc72f 689 {
a1f68b22 690 const char *name;
586dc72f 691
a1f68b22
MD
692 /*
693 * We currently only support $ctx (context) and $app
694 * identifiers.
695 */
586dc72f 696 if (strncmp(node->u.expression.u.identifier,
a1f68b22
MD
697 "$ctx.", strlen("$ctx.")) != 0
698 && strncmp(node->u.expression.u.identifier,
699 "$app.", strlen("$app.")) != 0) {
700 fprintf(stderr, "[error] %s: \"%s\" global identifier is unknown. Only \"$ctx\" and \"$app\" are currently implemented.\n", __func__, node->u.expression.u.identifier);
586dc72f
MD
701 return NULL;
702 }
a1f68b22
MD
703 name = strchr(node->u.expression.u.identifier, '.');
704 if (!name) {
705 fprintf(stderr, "[error] %s: Expecting '.'\n", __func__);
586dc72f
MD
706 return NULL;
707 }
a1f68b22
MD
708 name++; /* Skip . */
709 if (!strlen(name)) {
710 fprintf(stderr, "[error] %s: Expecting a context name, e.g. \'$ctx.name\'.\n", __func__);
586dc72f
MD
711 return NULL;
712 }
5590fc2c
MD
713 switch (node->u.expression.pre_op) {
714 case AST_LINK_UNKNOWN:
715 return make_op_load_get_context_ref(node->u.expression.u.identifier,
586dc72f 716 side);
5590fc2c
MD
717 case AST_LINK_BRACKET:
718 return make_op_load_get_context_ref_index(node->u.expression.u.identifier,
719 node->u.expression.next,
720 side);
721 default:
722 fprintf(stderr, "[error] %s: dotted and dereferenced identifiers not supported\n", __func__);
723 return NULL;
724 }
725
586dc72f 726 }
87942d06 727#endif
953192ba
MD
728 case AST_EXP_NESTED:
729 return generate_ir_recursive(ctx, node->u.expression.u.child,
730 side);
731 }
732}
733
734static
735struct ir_op *make_op(struct filter_parser_ctx *ctx,
736 struct filter_node *node, enum ir_side side)
737{
738 struct ir_op *op = NULL, *lchild, *rchild;
739 const char *op_str = "?";
740
741 switch (node->u.op.type) {
742 case AST_OP_UNKNOWN:
743 default:
744 fprintf(stderr, "[error] %s: unknown binary op type\n", __func__);
745 return NULL;
746
747 /*
87942d06
MD
748 * The following binary operators other than comparators and
749 * logical and/or are not supported yet.
953192ba
MD
750 */
751 case AST_OP_MUL:
752 op_str = "*";
753 goto error_not_supported;
754 case AST_OP_DIV:
755 op_str = "/";
756 goto error_not_supported;
757 case AST_OP_MOD:
758 op_str = "%";
759 goto error_not_supported;
760 case AST_OP_PLUS:
761 op_str = "+";
762 goto error_not_supported;
763 case AST_OP_MINUS:
764 op_str = "-";
765 goto error_not_supported;
87942d06 766
26107045
MD
767 case AST_OP_BIT_RSHIFT:
768 case AST_OP_BIT_LSHIFT:
87942d06
MD
769 case AST_OP_BIT_AND:
770 case AST_OP_BIT_OR:
771 case AST_OP_BIT_XOR:
772 lchild = generate_ir_recursive(ctx, node->u.op.lchild, IR_LEFT);
773 if (!lchild)
774 return NULL;
775 rchild = generate_ir_recursive(ctx, node->u.op.rchild, IR_RIGHT);
776 if (!rchild) {
777 filter_free_ir_recursive(lchild);
778 return NULL;
779 }
780 break;
953192ba
MD
781
782 case AST_OP_EQ:
783 case AST_OP_NE:
784 case AST_OP_GT:
785 case AST_OP_LT:
786 case AST_OP_GE:
787 case AST_OP_LE:
788 lchild = generate_ir_recursive(ctx, node->u.op.lchild, IR_LEFT);
789 if (!lchild)
790 return NULL;
791 rchild = generate_ir_recursive(ctx, node->u.op.rchild, IR_RIGHT);
792 if (!rchild) {
793 filter_free_ir_recursive(lchild);
794 return NULL;
795 }
796 break;
797
798 case AST_OP_AND:
799 case AST_OP_OR:
800 /*
801 * Both children considered as left, since we need to
802 * populate R0.
803 */
804 lchild = generate_ir_recursive(ctx, node->u.op.lchild, IR_LEFT);
805 if (!lchild)
806 return NULL;
807 rchild = generate_ir_recursive(ctx, node->u.op.rchild, IR_LEFT);
808 if (!rchild) {
809 filter_free_ir_recursive(lchild);
810 return NULL;
811 }
812 break;
813 }
814
815 switch (node->u.op.type) {
816 case AST_OP_AND:
817 op = make_op_binary_logical_and(lchild, rchild, side);
818 break;
819 case AST_OP_OR:
820 op = make_op_binary_logical_or(lchild, rchild, side);
821 break;
822 case AST_OP_EQ:
823 op = make_op_binary_eq(lchild, rchild, side);
824 break;
825 case AST_OP_NE:
826 op = make_op_binary_ne(lchild, rchild, side);
827 break;
828 case AST_OP_GT:
829 op = make_op_binary_gt(lchild, rchild, side);
830 break;
831 case AST_OP_LT:
832 op = make_op_binary_lt(lchild, rchild, side);
833 break;
834 case AST_OP_GE:
835 op = make_op_binary_ge(lchild, rchild, side);
836 break;
837 case AST_OP_LE:
838 op = make_op_binary_le(lchild, rchild, side);
839 break;
26107045
MD
840 case AST_OP_BIT_RSHIFT:
841 op = make_op_binary_bitwise_rshift(lchild, rchild, side);
842 break;
843 case AST_OP_BIT_LSHIFT:
844 op = make_op_binary_bitwise_lshift(lchild, rchild, side);
845 break;
87942d06
MD
846 case AST_OP_BIT_AND:
847 op = make_op_binary_bitwise_and(lchild, rchild, side);
848 break;
849 case AST_OP_BIT_OR:
850 op = make_op_binary_bitwise_or(lchild, rchild, side);
851 break;
852 case AST_OP_BIT_XOR:
853 op = make_op_binary_bitwise_xor(lchild, rchild, side);
854 break;
953192ba
MD
855 default:
856 break;
857 }
858
859 if (!op) {
860 filter_free_ir_recursive(rchild);
861 filter_free_ir_recursive(lchild);
862 }
863 return op;
864
865error_not_supported:
866 fprintf(stderr, "[error] %s: binary operation '%s' not supported\n",
867 __func__, op_str);
868 return NULL;
869}
870
871static
872struct ir_op *make_unary_op(struct filter_parser_ctx *ctx,
873 struct filter_node *node, enum ir_side side)
874{
875 switch (node->u.unary_op.type) {
876 case AST_UNARY_UNKNOWN:
877 default:
878 fprintf(stderr, "[error] %s: unknown unary op type\n", __func__);
879 return NULL;
880
881 case AST_UNARY_PLUS:
882 {
883 struct ir_op *op, *child;
884
885 child = generate_ir_recursive(ctx, node->u.unary_op.child,
886 side);
887 if (!child)
888 return NULL;
889 op = make_op_unary_plus(child, side);
890 if (!op) {
891 filter_free_ir_recursive(child);
892 return NULL;
893 }
894 return op;
895 }
896 case AST_UNARY_MINUS:
897 {
898 struct ir_op *op, *child;
899
900 child = generate_ir_recursive(ctx, node->u.unary_op.child,
901 side);
902 if (!child)
903 return NULL;
904 op = make_op_unary_minus(child, side);
905 if (!op) {
906 filter_free_ir_recursive(child);
907 return NULL;
908 }
909 return op;
910 }
911 case AST_UNARY_NOT:
912 {
913 struct ir_op *op, *child;
914
915 child = generate_ir_recursive(ctx, node->u.unary_op.child,
916 side);
917 if (!child)
918 return NULL;
919 op = make_op_unary_not(child, side);
920 if (!op) {
921 filter_free_ir_recursive(child);
922 return NULL;
923 }
924 return op;
925 }
87942d06 926 case AST_UNARY_BIT_NOT:
ab78f161 927 {
26107045
MD
928 struct ir_op *op, *child;
929
930 child = generate_ir_recursive(ctx, node->u.unary_op.child,
931 side);
932 if (!child)
933 return NULL;
934 op = make_op_unary_bit_not(child, side);
935 if (!op) {
936 filter_free_ir_recursive(child);
937 return NULL;
938 }
939 return op;
ab78f161 940 }
953192ba 941 }
ab78f161 942
ab78f161 943 return NULL;
953192ba
MD
944}
945
946static
947struct ir_op *generate_ir_recursive(struct filter_parser_ctx *ctx,
948 struct filter_node *node, enum ir_side side)
949{
950 switch (node->type) {
951 case NODE_UNKNOWN:
952 default:
953 fprintf(stderr, "[error] %s: unknown node type\n", __func__);
954 return NULL;
955
956 case NODE_ROOT:
957 {
958 struct ir_op *op, *child;
959
960 child = generate_ir_recursive(ctx, node->u.root.child,
961 side);
962 if (!child)
963 return NULL;
964 op = make_op_root(child, side);
965 if (!op) {
966 filter_free_ir_recursive(child);
967 return NULL;
968 }
969 return op;
970 }
971 case NODE_EXPRESSION:
972 return make_expression(ctx, node, side);
973 case NODE_OP:
974 return make_op(ctx, node, side);
975 case NODE_UNARY_OP:
976 return make_unary_op(ctx, node, side);
977 }
978 return 0;
979}
980
a187da1a 981LTTNG_HIDDEN
953192ba
MD
982void filter_ir_free(struct filter_parser_ctx *ctx)
983{
984 filter_free_ir_recursive(ctx->ir_root);
985 ctx->ir_root = NULL;
986}
987
a187da1a 988LTTNG_HIDDEN
953192ba
MD
989int filter_visitor_ir_generate(struct filter_parser_ctx *ctx)
990{
991 struct ir_op *op;
992
993 op = generate_ir_recursive(ctx, &ctx->ast->root, IR_LEFT);
994 if (!op) {
995 return -EINVAL;
996 }
997 ctx->ir_root = op;
998 return 0;
999}
This page took 0.095834 seconds and 5 git commands to generate.