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 "filter-ast.h"
20 #include "filter-parser.h"
21 #include "filter-bytecode.h"
23 int main(int argc
, char **argv
)
25 struct filter_parser_ctx
*ctx
;
27 int print_xml
= 0, generate_ir
= 0, generate_bytecode
= 0,
31 for (i
= 1; i
< argc
; i
++) {
32 if (strcmp(argv
[i
], "-p") == 0)
34 else if (strcmp(argv
[i
], "-i") == 0)
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)
45 * Force generate the bytecode if the user asks to print the bytecode
46 * (can't print it without generating it first).
49 generate_bytecode
= 1;
53 * Force generate the IR if the user asks to generate the bytecode
54 * (the bytecode is generated by visiting the IR).
56 if (generate_bytecode
) {
60 ctx
= filter_parser_ctx_alloc(stdin
);
62 fprintf(stderr
, "Error allocating parser\n");
65 ret
= filter_parser_ctx_append_ast(ctx
);
67 fprintf(stderr
, "Parse error\n");
71 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
74 fprintf(stderr
, "XML print error\n");
79 printf("Generating IR... ");
81 ret
= filter_visitor_ir_generate(ctx
);
83 fprintf(stderr
, "Generate IR error\n");
88 printf("Validating IR... ");
90 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
96 if (generate_bytecode
) {
97 printf("Generating bytecode... ");
99 ret
= filter_visitor_bytecode_generate(ctx
);
101 fprintf(stderr
, "Generate bytecode error\n");
105 printf("Size of bytecode generated: %u bytes.\n",
106 bytecode_get_len(&ctx
->bytecode
->b
));
109 if (print_bytecode
) {
110 unsigned int bytecode_len
, len
, i
;
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
++) {
117 ((uint8_t *) ctx
->bytecode
->b
.data
)[i
]);
120 printf("Reloc table:\n");
121 for (i
= bytecode_len
; i
< len
;) {
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;
131 filter_bytecode_free(ctx
);
133 filter_parser_ctx_free(ctx
);
137 filter_bytecode_free(ctx
);
139 filter_parser_ctx_free(ctx
);
This page took 0.036318 seconds and 5 git commands to generate.