MI: xsd: sort output_type
[lttng-tools.git] / src / common / filter-grammar-test.c
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 <inttypes.h>
18
19 #include <common/compat/errno.h>
20 #include <common/bytecode/bytecode.h>
21 #include <common/filter/filter-ast.h>
22 #include <common/filter/filter-parser.h>
23
24 /* For error.h */
25 int lttng_opt_quiet = 1;
26 int lttng_opt_verbose;
27 int lttng_opt_mi;
28
29 int main(int argc, char **argv)
30 {
31 struct filter_parser_ctx *ctx;
32 int ret;
33 int print_xml = 0, generate_ir = 0, generate_bytecode = 0,
34 print_bytecode = 0;
35 int argidx;
36
37 for (argidx = 1; argidx < argc; argidx++) {
38 if (strcmp(argv[argidx], "-p") == 0)
39 print_xml = 1;
40 else if (strcmp(argv[argidx], "-i") == 0)
41 generate_ir = 1;
42 else if (strcmp(argv[argidx], "-b") == 0)
43 generate_bytecode = 1;
44 else if (strcmp(argv[argidx], "-d") == 0)
45 filter_parser_debug = 1;
46 else if (strcmp(argv[argidx], "-B") == 0)
47 print_bytecode = 1;
48 }
49
50 /*
51 * Force generate the bytecode if the user asks to print the bytecode
52 * (can't print it without generating it first).
53 */
54 if (print_bytecode) {
55 generate_bytecode = 1;
56 }
57
58 /*
59 * Force generate the IR if the user asks to generate the bytecode
60 * (the bytecode is generated by visiting the IR).
61 */
62 if (generate_bytecode) {
63 generate_ir = 1;
64 }
65
66 ctx = filter_parser_ctx_alloc(stdin);
67 if (!ctx) {
68 fprintf(stderr, "Error allocating parser\n");
69 goto alloc_error;
70 }
71 ret = filter_parser_ctx_append_ast(ctx);
72 if (ret) {
73 fprintf(stderr, "Parse error\n");
74 goto parse_error;
75 }
76 if (print_xml) {
77 ret = filter_visitor_print_xml(ctx, stdout, 0);
78 if (ret) {
79 fflush(stdout);
80 fprintf(stderr, "XML print error\n");
81 goto parse_error;
82 }
83 }
84 if (generate_ir) {
85 printf("Generating IR... ");
86 fflush(stdout);
87 ret = filter_visitor_ir_generate(ctx);
88 if (ret) {
89 fprintf(stderr, "Generate IR error\n");
90 goto parse_error;
91 }
92 printf("done\n");
93
94 printf("Validating IR... ");
95 fflush(stdout);
96 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
97 if (ret) {
98 goto parse_error;
99 }
100 printf("done\n");
101 }
102 if (generate_bytecode) {
103 printf("Generating bytecode... ");
104 fflush(stdout);
105 ret = filter_visitor_bytecode_generate(ctx);
106 if (ret) {
107 fprintf(stderr, "Generate bytecode error\n");
108 goto parse_error;
109 }
110 printf("done\n");
111 printf("Size of bytecode generated: %u bytes.\n",
112 bytecode_get_len(&ctx->bytecode->b));
113 }
114
115 if (print_bytecode) {
116 unsigned int bytecode_len, len, i;
117
118 len = bytecode_get_len(&ctx->bytecode->b);
119 bytecode_len = ctx->bytecode->b.reloc_table_offset;
120 printf("Bytecode:\n");
121 for (i = 0; i < bytecode_len; i++) {
122 printf("0x%X ",
123 ((uint8_t *) ctx->bytecode->b.data)[i]);
124 }
125 printf("\n");
126 printf("Reloc table:\n");
127 for (i = bytecode_len; i < len;) {
128 printf("{ 0x%X, ",
129 *(uint16_t *) &ctx->bytecode->b.data[i]);
130 i += sizeof(uint16_t);
131 printf("%s } ", &((char *) ctx->bytecode->b.data)[i]);
132 i += strlen(&((char *) ctx->bytecode->b.data)[i]) + 1;
133 }
134 printf("\n");
135 }
136
137 filter_bytecode_free(ctx);
138 filter_ir_free(ctx);
139 filter_parser_ctx_free(ctx);
140 return 0;
141
142 parse_error:
143 filter_bytecode_free(ctx);
144 filter_ir_free(ctx);
145 filter_parser_ctx_free(ctx);
146 alloc_error:
147 exit(EXIT_FAILURE);
148 }
This page took 0.031473 seconds and 4 git commands to generate.