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