2 * filter-grammar-test.c
4 * LTTng filter grammar test
6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * SPDX-License-Identifier: LGPL-2.1-only
19 #include <common/compat/errno.h>
21 #include "filter-ast.h"
22 #include "filter-parser.h"
23 #include "filter-bytecode.h"
25 int main(int argc
, char **argv
)
27 struct filter_parser_ctx
*ctx
;
29 int print_xml
= 0, generate_ir
= 0, generate_bytecode
= 0,
33 for (i
= 1; i
< argc
; i
++) {
34 if (strcmp(argv
[i
], "-p") == 0)
36 else if (strcmp(argv
[i
], "-i") == 0)
38 else if (strcmp(argv
[i
], "-b") == 0)
39 generate_bytecode
= 1;
40 else if (strcmp(argv
[i
], "-d") == 0)
41 filter_parser_debug
= 1;
42 else if (strcmp(argv
[i
], "-B") == 0)
47 * Force generate the bytecode if the user asks to print the bytecode
48 * (can't print it without generating it first).
51 generate_bytecode
= 1;
55 * Force generate the IR if the user asks to generate the bytecode
56 * (the bytecode is generated by visiting the IR).
58 if (generate_bytecode
) {
62 ctx
= filter_parser_ctx_alloc(stdin
);
64 fprintf(stderr
, "Error allocating parser\n");
67 ret
= filter_parser_ctx_append_ast(ctx
);
69 fprintf(stderr
, "Parse error\n");
73 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
76 fprintf(stderr
, "XML print error\n");
81 printf("Generating IR... ");
83 ret
= filter_visitor_ir_generate(ctx
);
85 fprintf(stderr
, "Generate IR error\n");
90 printf("Validating IR... ");
92 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
98 if (generate_bytecode
) {
99 printf("Generating bytecode... ");
101 ret
= filter_visitor_bytecode_generate(ctx
);
103 fprintf(stderr
, "Generate bytecode error\n");
107 printf("Size of bytecode generated: %u bytes.\n",
108 bytecode_get_len(&ctx
->bytecode
->b
));
111 if (print_bytecode
) {
112 unsigned int bytecode_len
, len
, i
;
114 len
= bytecode_get_len(&ctx
->bytecode
->b
);
115 bytecode_len
= ctx
->bytecode
->b
.reloc_table_offset
;
116 printf("Bytecode:\n");
117 for (i
= 0; i
< bytecode_len
; i
++) {
119 ((uint8_t *) ctx
->bytecode
->b
.data
)[i
]);
122 printf("Reloc table:\n");
123 for (i
= bytecode_len
; i
< len
;) {
125 *(uint16_t *) &ctx
->bytecode
->b
.data
[i
]);
126 i
+= sizeof(uint16_t);
127 printf("%s } ", &((char *) ctx
->bytecode
->b
.data
)[i
]);
128 i
+= strlen(&((char *) ctx
->bytecode
->b
.data
)[i
]) + 1;
133 filter_bytecode_free(ctx
);
135 filter_parser_ctx_free(ctx
);
139 filter_bytecode_free(ctx
);
141 filter_parser_ctx_free(ctx
);
This page took 0.031549 seconds and 4 git commands to generate.