port: FreeBSD has no ENODATA, alias it to ENOATTR
[lttng-tools.git] / src / common / filter / filter-grammar-test.c
CommitLineData
953192ba
MD
1/*
2 * filter-grammar-test.c
3 *
4 * LTTng filter grammar test
5 *
ab5be9fa 6 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
953192ba 7 *
ab5be9fa 8 * SPDX-License-Identifier: LGPL-2.1-only
953192ba 9 *
953192ba
MD
10 */
11
12#include <stdio.h>
13#include <unistd.h>
14#include <string.h>
15#include <stdlib.h>
16#include <assert.h>
953192ba 17#include <inttypes.h>
edf4b93e
MJ
18
19#include <common/compat/errno.h>
20
953192ba 21#include "filter-ast.h"
95b9bd90 22#include "filter-parser.h"
953192ba
MD
23#include "filter-bytecode.h"
24
25int main(int argc, char **argv)
26{
27 struct filter_parser_ctx *ctx;
28 int ret;
14eabff9
MD
29 int print_xml = 0, generate_ir = 0, generate_bytecode = 0,
30 print_bytecode = 0;
953192ba
MD
31 int i;
32
33 for (i = 1; i < argc; i++) {
34 if (strcmp(argv[i], "-p") == 0)
35 print_xml = 1;
36 else if (strcmp(argv[i], "-i") == 0)
37 generate_ir = 1;
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;
14eabff9
MD
42 else if (strcmp(argv[i], "-B") == 0)
43 print_bytecode = 1;
953192ba
MD
44 }
45
606757e4
SM
46 /*
47 * Force generate the bytecode if the user asks to print the bytecode
48 * (can't print it without generating it first).
49 */
50 if (print_bytecode) {
51 generate_bytecode = 1;
52 }
53
54 /*
55 * Force generate the IR if the user asks to generate the bytecode
56 * (the bytecode is generated by visiting the IR).
57 */
58 if (generate_bytecode) {
59 generate_ir = 1;
60 }
61
953192ba
MD
62 ctx = filter_parser_ctx_alloc(stdin);
63 if (!ctx) {
64 fprintf(stderr, "Error allocating parser\n");
65 goto alloc_error;
66 }
67 ret = filter_parser_ctx_append_ast(ctx);
68 if (ret) {
69 fprintf(stderr, "Parse error\n");
70 goto parse_error;
71 }
953192ba
MD
72 if (print_xml) {
73 ret = filter_visitor_print_xml(ctx, stdout, 0);
74 if (ret) {
75 fflush(stdout);
76 fprintf(stderr, "XML print error\n");
77 goto parse_error;
78 }
79 }
80 if (generate_ir) {
81 printf("Generating IR... ");
82 fflush(stdout);
83 ret = filter_visitor_ir_generate(ctx);
84 if (ret) {
85 fprintf(stderr, "Generate IR error\n");
86 goto parse_error;
87 }
88 printf("done\n");
89
90 printf("Validating IR... ");
91 fflush(stdout);
92 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
93 if (ret) {
94 goto parse_error;
95 }
96 printf("done\n");
97 }
98 if (generate_bytecode) {
99 printf("Generating bytecode... ");
100 fflush(stdout);
101 ret = filter_visitor_bytecode_generate(ctx);
102 if (ret) {
103 fprintf(stderr, "Generate bytecode error\n");
104 goto parse_error;
105 }
106 printf("done\n");
107 printf("Size of bytecode generated: %u bytes.\n",
108 bytecode_get_len(&ctx->bytecode->b));
109 }
953192ba 110
14eabff9
MD
111 if (print_bytecode) {
112 unsigned int bytecode_len, len, i;
113
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++) {
118 printf("0x%X ",
119 ((uint8_t *) ctx->bytecode->b.data)[i]);
120 }
121 printf("\n");
122 printf("Reloc table:\n");
123 for (i = bytecode_len; i < len;) {
124 printf("{ 0x%X, ",
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;
129 }
130 printf("\n");
131 }
132
953192ba
MD
133 filter_bytecode_free(ctx);
134 filter_ir_free(ctx);
135 filter_parser_ctx_free(ctx);
136 return 0;
137
138parse_error:
139 filter_bytecode_free(ctx);
140 filter_ir_free(ctx);
141 filter_parser_ctx_free(ctx);
142alloc_error:
143 exit(EXIT_FAILURE);
144}
This page took 0.050652 seconds and 4 git commands to generate.