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