Commit | Line | Data |
---|---|---|
953192ba MD |
1 | /* |
2 | * filter-grammar-test.c | |
3 | * | |
4 | * LTTng filter grammar test | |
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-bytecode.h" |
32 | ||
33 | int main(int argc, char **argv) | |
34 | { | |
35 | struct filter_parser_ctx *ctx; | |
36 | int ret; | |
14eabff9 MD |
37 | int print_xml = 0, generate_ir = 0, generate_bytecode = 0, |
38 | print_bytecode = 0; | |
953192ba MD |
39 | int i; |
40 | ||
41 | for (i = 1; i < argc; i++) { | |
42 | if (strcmp(argv[i], "-p") == 0) | |
43 | print_xml = 1; | |
44 | else if (strcmp(argv[i], "-i") == 0) | |
45 | generate_ir = 1; | |
46 | else if (strcmp(argv[i], "-b") == 0) | |
47 | generate_bytecode = 1; | |
48 | else if (strcmp(argv[i], "-d") == 0) | |
49 | filter_parser_debug = 1; | |
14eabff9 MD |
50 | else if (strcmp(argv[i], "-B") == 0) |
51 | print_bytecode = 1; | |
953192ba MD |
52 | } |
53 | ||
54 | ctx = filter_parser_ctx_alloc(stdin); | |
55 | if (!ctx) { | |
56 | fprintf(stderr, "Error allocating parser\n"); | |
57 | goto alloc_error; | |
58 | } | |
59 | ret = filter_parser_ctx_append_ast(ctx); | |
60 | if (ret) { | |
61 | fprintf(stderr, "Parse error\n"); | |
62 | goto parse_error; | |
63 | } | |
953192ba MD |
64 | if (print_xml) { |
65 | ret = filter_visitor_print_xml(ctx, stdout, 0); | |
66 | if (ret) { | |
67 | fflush(stdout); | |
68 | fprintf(stderr, "XML print error\n"); | |
69 | goto parse_error; | |
70 | } | |
71 | } | |
72 | if (generate_ir) { | |
73 | printf("Generating IR... "); | |
74 | fflush(stdout); | |
75 | ret = filter_visitor_ir_generate(ctx); | |
76 | if (ret) { | |
77 | fprintf(stderr, "Generate IR error\n"); | |
78 | goto parse_error; | |
79 | } | |
80 | printf("done\n"); | |
81 | ||
82 | printf("Validating IR... "); | |
83 | fflush(stdout); | |
84 | ret = filter_visitor_ir_check_binary_op_nesting(ctx); | |
85 | if (ret) { | |
86 | goto parse_error; | |
87 | } | |
88 | printf("done\n"); | |
89 | } | |
90 | if (generate_bytecode) { | |
91 | printf("Generating bytecode... "); | |
92 | fflush(stdout); | |
93 | ret = filter_visitor_bytecode_generate(ctx); | |
94 | if (ret) { | |
95 | fprintf(stderr, "Generate bytecode error\n"); | |
96 | goto parse_error; | |
97 | } | |
98 | printf("done\n"); | |
99 | printf("Size of bytecode generated: %u bytes.\n", | |
100 | bytecode_get_len(&ctx->bytecode->b)); | |
101 | } | |
953192ba | 102 | |
14eabff9 MD |
103 | if (print_bytecode) { |
104 | unsigned int bytecode_len, len, i; | |
105 | ||
106 | len = bytecode_get_len(&ctx->bytecode->b); | |
107 | bytecode_len = ctx->bytecode->b.reloc_table_offset; | |
108 | printf("Bytecode:\n"); | |
109 | for (i = 0; i < bytecode_len; i++) { | |
110 | printf("0x%X ", | |
111 | ((uint8_t *) ctx->bytecode->b.data)[i]); | |
112 | } | |
113 | printf("\n"); | |
114 | printf("Reloc table:\n"); | |
115 | for (i = bytecode_len; i < len;) { | |
116 | printf("{ 0x%X, ", | |
117 | *(uint16_t *) &ctx->bytecode->b.data[i]); | |
118 | i += sizeof(uint16_t); | |
119 | printf("%s } ", &((char *) ctx->bytecode->b.data)[i]); | |
120 | i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1; | |
121 | } | |
122 | printf("\n"); | |
123 | } | |
124 | ||
953192ba MD |
125 | filter_bytecode_free(ctx); |
126 | filter_ir_free(ctx); | |
127 | filter_parser_ctx_free(ctx); | |
128 | return 0; | |
129 | ||
130 | parse_error: | |
131 | filter_bytecode_free(ctx); | |
132 | filter_ir_free(ctx); | |
133 | filter_parser_ctx_free(ctx); | |
134 | alloc_error: | |
135 | exit(EXIT_FAILURE); | |
136 | } |