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>
20 #include "common/bytecode/bytecode.h"
21 #include "filter-ast.h"
22 #include "filter-parser.h"
24 int main(int argc
, char **argv
)
26 struct filter_parser_ctx
*ctx
;
28 int print_xml
= 0, generate_ir
= 0, generate_bytecode
= 0,
32 for (i
= 1; i
< argc
; i
++) {
33 if (strcmp(argv
[i
], "-p") == 0)
35 else if (strcmp(argv
[i
], "-i") == 0)
37 else if (strcmp(argv
[i
], "-b") == 0)
38 generate_bytecode
= 1;
39 else if (strcmp(argv
[i
], "-d") == 0)
40 filter_parser_debug
= 1;
41 else if (strcmp(argv
[i
], "-B") == 0)
46 * Force generate the bytecode if the user asks to print the bytecode
47 * (can't print it without generating it first).
50 generate_bytecode
= 1;
54 * Force generate the IR if the user asks to generate the bytecode
55 * (the bytecode is generated by visiting the IR).
57 if (generate_bytecode
) {
61 ctx
= filter_parser_ctx_alloc(stdin
);
63 fprintf(stderr
, "Error allocating parser\n");
66 ret
= filter_parser_ctx_append_ast(ctx
);
68 fprintf(stderr
, "Parse error\n");
72 ret
= filter_visitor_print_xml(ctx
, stdout
, 0);
75 fprintf(stderr
, "XML print error\n");
80 printf("Generating IR... ");
82 ret
= filter_visitor_ir_generate(ctx
);
84 fprintf(stderr
, "Generate IR error\n");
89 printf("Validating IR... ");
91 ret
= filter_visitor_ir_check_binary_op_nesting(ctx
);
97 if (generate_bytecode
) {
98 printf("Generating bytecode... ");
100 ret
= filter_visitor_bytecode_generate(ctx
);
102 fprintf(stderr
, "Generate bytecode error\n");
106 printf("Size of bytecode generated: %u bytes.\n",
107 bytecode_get_len(&ctx
->bytecode
->b
));
110 if (print_bytecode
) {
111 unsigned int bytecode_len
, len
, i
;
113 len
= bytecode_get_len(&ctx
->bytecode
->b
);
114 bytecode_len
= ctx
->bytecode
->b
.reloc_table_offset
;
115 printf("Bytecode:\n");
116 for (i
= 0; i
< bytecode_len
; i
++) {
118 ((uint8_t *) ctx
->bytecode
->b
.data
)[i
]);
121 printf("Reloc table:\n");
122 for (i
= bytecode_len
; i
< len
;) {
124 *(uint16_t *) &ctx
->bytecode
->b
.data
[i
]);
125 i
+= sizeof(uint16_t);
126 printf("%s } ", &((char *) ctx
->bytecode
->b
.data
)[i
]);
127 i
+= strlen(&((char *) ctx
->bytecode
->b
.data
)[i
]) + 1;
132 filter_bytecode_free(ctx
);
134 filter_parser_ctx_free(ctx
);
138 filter_bytecode_free(ctx
);
140 filter_parser_ctx_free(ctx
);
This page took 0.031681 seconds and 4 git commands to generate.