| 1 | /* |
| 2 | * filter-grammar-test.c |
| 3 | * |
| 4 | * LTTng filter grammar test |
| 5 | * |
| 6 | * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * SPDX-License-Identifier: LGPL-2.1-only |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
| 14 | #include <string.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <assert.h> |
| 17 | #include <errno.h> |
| 18 | #include <inttypes.h> |
| 19 | #include "filter-ast.h" |
| 20 | #include "filter-parser.h" |
| 21 | #include "filter-bytecode.h" |
| 22 | |
| 23 | int main(int argc, char **argv) |
| 24 | { |
| 25 | struct filter_parser_ctx *ctx; |
| 26 | int ret; |
| 27 | int print_xml = 0, generate_ir = 0, generate_bytecode = 0, |
| 28 | print_bytecode = 0; |
| 29 | int i; |
| 30 | |
| 31 | for (i = 1; i < argc; i++) { |
| 32 | if (strcmp(argv[i], "-p") == 0) |
| 33 | print_xml = 1; |
| 34 | else if (strcmp(argv[i], "-i") == 0) |
| 35 | generate_ir = 1; |
| 36 | else if (strcmp(argv[i], "-b") == 0) |
| 37 | generate_bytecode = 1; |
| 38 | else if (strcmp(argv[i], "-d") == 0) |
| 39 | filter_parser_debug = 1; |
| 40 | else if (strcmp(argv[i], "-B") == 0) |
| 41 | print_bytecode = 1; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * Force generate the bytecode if the user asks to print the bytecode |
| 46 | * (can't print it without generating it first). |
| 47 | */ |
| 48 | if (print_bytecode) { |
| 49 | generate_bytecode = 1; |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * Force generate the IR if the user asks to generate the bytecode |
| 54 | * (the bytecode is generated by visiting the IR). |
| 55 | */ |
| 56 | if (generate_bytecode) { |
| 57 | generate_ir = 1; |
| 58 | } |
| 59 | |
| 60 | ctx = filter_parser_ctx_alloc(stdin); |
| 61 | if (!ctx) { |
| 62 | fprintf(stderr, "Error allocating parser\n"); |
| 63 | goto alloc_error; |
| 64 | } |
| 65 | ret = filter_parser_ctx_append_ast(ctx); |
| 66 | if (ret) { |
| 67 | fprintf(stderr, "Parse error\n"); |
| 68 | goto parse_error; |
| 69 | } |
| 70 | if (print_xml) { |
| 71 | ret = filter_visitor_print_xml(ctx, stdout, 0); |
| 72 | if (ret) { |
| 73 | fflush(stdout); |
| 74 | fprintf(stderr, "XML print error\n"); |
| 75 | goto parse_error; |
| 76 | } |
| 77 | } |
| 78 | if (generate_ir) { |
| 79 | printf("Generating IR... "); |
| 80 | fflush(stdout); |
| 81 | ret = filter_visitor_ir_generate(ctx); |
| 82 | if (ret) { |
| 83 | fprintf(stderr, "Generate IR error\n"); |
| 84 | goto parse_error; |
| 85 | } |
| 86 | printf("done\n"); |
| 87 | |
| 88 | printf("Validating IR... "); |
| 89 | fflush(stdout); |
| 90 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); |
| 91 | if (ret) { |
| 92 | goto parse_error; |
| 93 | } |
| 94 | printf("done\n"); |
| 95 | } |
| 96 | if (generate_bytecode) { |
| 97 | printf("Generating bytecode... "); |
| 98 | fflush(stdout); |
| 99 | ret = filter_visitor_bytecode_generate(ctx); |
| 100 | if (ret) { |
| 101 | fprintf(stderr, "Generate bytecode error\n"); |
| 102 | goto parse_error; |
| 103 | } |
| 104 | printf("done\n"); |
| 105 | printf("Size of bytecode generated: %u bytes.\n", |
| 106 | bytecode_get_len(&ctx->bytecode->b)); |
| 107 | } |
| 108 | |
| 109 | if (print_bytecode) { |
| 110 | unsigned int bytecode_len, len, i; |
| 111 | |
| 112 | len = bytecode_get_len(&ctx->bytecode->b); |
| 113 | bytecode_len = ctx->bytecode->b.reloc_table_offset; |
| 114 | printf("Bytecode:\n"); |
| 115 | for (i = 0; i < bytecode_len; i++) { |
| 116 | printf("0x%X ", |
| 117 | ((uint8_t *) ctx->bytecode->b.data)[i]); |
| 118 | } |
| 119 | printf("\n"); |
| 120 | printf("Reloc table:\n"); |
| 121 | for (i = bytecode_len; i < len;) { |
| 122 | printf("{ 0x%X, ", |
| 123 | *(uint16_t *) &ctx->bytecode->b.data[i]); |
| 124 | i += sizeof(uint16_t); |
| 125 | printf("%s } ", &((char *) ctx->bytecode->b.data)[i]); |
| 126 | i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1; |
| 127 | } |
| 128 | printf("\n"); |
| 129 | } |
| 130 | |
| 131 | filter_bytecode_free(ctx); |
| 132 | filter_ir_free(ctx); |
| 133 | filter_parser_ctx_free(ctx); |
| 134 | return 0; |
| 135 | |
| 136 | parse_error: |
| 137 | filter_bytecode_free(ctx); |
| 138 | filter_ir_free(ctx); |
| 139 | filter_parser_ctx_free(ctx); |
| 140 | alloc_error: |
| 141 | exit(EXIT_FAILURE); |
| 142 | } |