Add lttng-ctl SWIG python bindings
[lttng-tools.git] / src / lib / lttng-ctl / filter / filter-visitor-generate-bytecode.c
CommitLineData
953192ba
MD
1/*
2 * filter-visitor-generate-bytecode.c
3 *
4 * LTTng filter bytecode generation
5 *
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <stdlib.h>
23#include <string.h>
24#include <errno.h>
25#include "align.h"
26#include "filter-bytecode.h"
27#include "filter-ir.h"
28#include "filter-ast.h"
29
30#ifndef max_t
31#define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
32#endif
33
34//#define INIT_ALLOC_SIZE PAGE_SIZE
35#define INIT_ALLOC_SIZE 4
36
37static
38int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx,
39 struct ir_op *node);
40
41static
53a80697 42int bytecode_init(struct lttng_filter_bytecode_alloc **fb)
953192ba 43{
53a80697 44 *fb = calloc(sizeof(struct lttng_filter_bytecode_alloc) + INIT_ALLOC_SIZE, 1);
953192ba
MD
45 if (!*fb) {
46 return -ENOMEM;
47 } else {
48 (*fb)->alloc_len = INIT_ALLOC_SIZE;
49 return 0;
50 }
51}
52
53static
53a80697 54int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc **fb, uint32_t align, uint32_t len)
953192ba
MD
55{
56 int32_t ret;
57 uint32_t padding = offset_align((*fb)->b.len, align);
58
59 if ((*fb)->b.len + padding + len > (*fb)->alloc_len) {
60 uint32_t new_len =
61 max_t(uint32_t, (*fb)->b.len + padding + len,
62 (*fb)->alloc_len << 1);
63 uint32_t old_len = (*fb)->alloc_len;
64
65 if (new_len > 0xFFFF)
66 return -EINVAL;
53a80697 67 *fb = realloc(*fb, sizeof(struct lttng_filter_bytecode_alloc) + new_len);
953192ba
MD
68 if (!*fb)
69 return -ENOMEM;
70 memset(&(*fb)->b.data[old_len], 0, new_len - old_len);
71 (*fb)->alloc_len = new_len;
72 }
73 (*fb)->b.len += padding;
74 ret = (*fb)->b.len;
75 (*fb)->b.len += len;
76 return ret;
77}
78
79static
53a80697 80int bytecode_push(struct lttng_filter_bytecode_alloc **fb, const void *data,
953192ba
MD
81 uint32_t align, uint32_t len)
82{
83 int32_t offset;
84
85 offset = bytecode_reserve(fb, align, len);
86 if (offset < 0)
87 return offset;
88 memcpy(&(*fb)->b.data[offset], data, len);
89 return 0;
90}
91
92static
53a80697 93int bytecode_push_logical(struct lttng_filter_bytecode_alloc **fb,
953192ba
MD
94 struct logical_op *data,
95 uint32_t align, uint32_t len,
96 uint16_t *skip_offset)
97{
98 int32_t offset;
99
100 offset = bytecode_reserve(fb, align, len);
101 if (offset < 0)
102 return offset;
103 memcpy(&(*fb)->b.data[offset], data, len);
104 *skip_offset =
105 (void *) &((struct logical_op *) &(*fb)->b.data[offset])->skip_offset
106 - (void *) &(*fb)->b.data[0];
107 return 0;
108}
109
110static
53a80697 111int bytecode_patch(struct lttng_filter_bytecode_alloc **fb,
953192ba
MD
112 const void *data,
113 uint16_t offset,
114 uint32_t len)
115{
116 if (offset >= (*fb)->b.len) {
117 return -EINVAL;
118 }
119 memcpy(&(*fb)->b.data[offset], data, len);
120 return 0;
121}
122
123static
124int visit_node_root(struct filter_parser_ctx *ctx, struct ir_op *node)
125{
126 int ret;
127 struct return_op insn;
128
129 /* Visit child */
130 ret = recursive_visit_gen_bytecode(ctx, node->u.root.child);
131 if (ret)
132 return ret;
133
134 /* Generate end of bytecode instruction */
135 insn.op = FILTER_OP_RETURN;
136 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
137}
138
953192ba
MD
139static
140int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node)
141{
142 int ret;
143
144 switch (node->data_type) {
145 case IR_DATA_UNKNOWN:
146 default:
147 fprintf(stderr, "[error] Unknown data type in %s\n",
148 __func__);
149 return -EINVAL;
150
151 case IR_DATA_STRING:
152 {
153 struct load_op *insn;
154 uint32_t insn_len = sizeof(struct load_op)
155 + strlen(node->u.load.u.string) + 1;
156
157 insn = calloc(insn_len, 1);
158 if (!insn)
159 return -ENOMEM;
160 insn->op = FILTER_OP_LOAD_STRING;
953192ba
MD
161 strcpy(insn->data, node->u.load.u.string);
162 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
163 free(insn);
164 return ret;
165 }
166 case IR_DATA_NUMERIC:
167 {
168 struct load_op *insn;
169 uint32_t insn_len = sizeof(struct load_op)
170 + sizeof(struct literal_numeric);
171
172 insn = calloc(insn_len, 1);
173 if (!insn)
174 return -ENOMEM;
175 insn->op = FILTER_OP_LOAD_S64;
953192ba
MD
176 *(int64_t *) insn->data = node->u.load.u.num;
177 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
178 free(insn);
179 return ret;
180 }
e90d8561
MD
181 case IR_DATA_FLOAT:
182 {
183 struct load_op *insn;
184 uint32_t insn_len = sizeof(struct load_op)
185 + sizeof(struct literal_double);
186
187 insn = calloc(insn_len, 1);
188 if (!insn)
189 return -ENOMEM;
190 insn->op = FILTER_OP_LOAD_DOUBLE;
e90d8561
MD
191 *(double *) insn->data = node->u.load.u.flt;
192 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
193 free(insn);
194 return ret;
195 }
953192ba
MD
196 case IR_DATA_FIELD_REF:
197 {
198 struct load_op *insn;
199 uint32_t insn_len = sizeof(struct load_op)
200 + sizeof(struct field_ref);
201 struct field_ref ref_offset;
202 uint16_t reloc_offset;
203
204 insn = calloc(insn_len, 1);
205 if (!insn)
206 return -ENOMEM;
207 insn->op = FILTER_OP_LOAD_FIELD_REF;
953192ba
MD
208 ref_offset.offset = (uint16_t) -1U;
209 memcpy(insn->data, &ref_offset, sizeof(ref_offset));
65775683 210 /* reloc_offset points to struct load_op */
953192ba 211 reloc_offset = bytecode_get_len(&ctx->bytecode->b);
953192ba
MD
212 ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
213 if (ret) {
214 free(insn);
215 return ret;
216 }
217 /* append reloc */
218 ret = bytecode_push(&ctx->bytecode_reloc, &reloc_offset,
219 1, sizeof(reloc_offset));
220 if (ret) {
221 free(insn);
222 return ret;
223 }
224 ret = bytecode_push(&ctx->bytecode_reloc, node->u.load.u.ref,
225 1, strlen(node->u.load.u.ref) + 1);
226 free(insn);
227 return ret;
228 }
229 }
230}
231
232static
233int visit_node_unary(struct filter_parser_ctx *ctx, struct ir_op *node)
234{
235 int ret;
236 struct unary_op insn;
237
238 /* Visit child */
239 ret = recursive_visit_gen_bytecode(ctx, node->u.unary.child);
240 if (ret)
241 return ret;
242
243 /* Generate end of bytecode instruction */
244 switch (node->u.unary.type) {
245 case AST_UNARY_UNKNOWN:
246 default:
247 fprintf(stderr, "[error] Unknown unary node type in %s\n",
248 __func__);
249 return -EINVAL;
250 case AST_UNARY_PLUS:
251 /* Nothing to do. */
252 return 0;
253 case AST_UNARY_MINUS:
254 insn.op = FILTER_OP_UNARY_MINUS;
953192ba
MD
255 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
256 case AST_UNARY_NOT:
257 insn.op = FILTER_OP_UNARY_NOT;
953192ba
MD
258 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
259 }
260}
261
262/*
263 * Binary comparator nesting is disallowed. This allows fitting into
264 * only 2 registers.
265 */
266static
267int visit_node_binary(struct filter_parser_ctx *ctx, struct ir_op *node)
268{
269 int ret;
270 struct binary_op insn;
271
272 /* Visit child */
273 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left);
274 if (ret)
275 return ret;
276 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right);
277 if (ret)
278 return ret;
279
280 switch (node->u.binary.type) {
281 case AST_OP_UNKNOWN:
282 default:
283 fprintf(stderr, "[error] Unknown unary node type in %s\n",
284 __func__);
285 return -EINVAL;
286
287 case AST_OP_AND:
288 case AST_OP_OR:
289 fprintf(stderr, "[error] Unexpected logical node type in %s\n",
290 __func__);
291 return -EINVAL;
292
293 case AST_OP_MUL:
294 insn.op = FILTER_OP_MUL;
295 break;
296 case AST_OP_DIV:
297 insn.op = FILTER_OP_DIV;
298 break;
299 case AST_OP_MOD:
300 insn.op = FILTER_OP_MOD;
301 break;
302 case AST_OP_PLUS:
303 insn.op = FILTER_OP_PLUS;
304 break;
305 case AST_OP_MINUS:
306 insn.op = FILTER_OP_MINUS;
307 break;
308 case AST_OP_RSHIFT:
309 insn.op = FILTER_OP_RSHIFT;
310 break;
311 case AST_OP_LSHIFT:
312 insn.op = FILTER_OP_LSHIFT;
313 break;
314 case AST_OP_BIN_AND:
315 insn.op = FILTER_OP_BIN_AND;
316 break;
317 case AST_OP_BIN_OR:
318 insn.op = FILTER_OP_BIN_OR;
319 break;
320 case AST_OP_BIN_XOR:
321 insn.op = FILTER_OP_BIN_XOR;
322 break;
323
324 case AST_OP_EQ:
325 insn.op = FILTER_OP_EQ;
326 break;
327 case AST_OP_NE:
328 insn.op = FILTER_OP_NE;
329 break;
330 case AST_OP_GT:
331 insn.op = FILTER_OP_GT;
332 break;
333 case AST_OP_LT:
334 insn.op = FILTER_OP_LT;
335 break;
336 case AST_OP_GE:
337 insn.op = FILTER_OP_GE;
338 break;
339 case AST_OP_LE:
340 insn.op = FILTER_OP_LE;
341 break;
342 }
343 return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
344}
345
8cf9540a
MD
346/*
347 * A logical op always return a s64 (1 or 0).
348 */
953192ba
MD
349static
350int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node)
351{
352 int ret;
353 struct logical_op insn;
354 uint16_t skip_offset_loc;
355 uint16_t target_loc;
356
357 /* Visit left child */
358 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left);
359 if (ret)
360 return ret;
8cf9540a
MD
361 /* Cast to s64 if float or field ref */
362 if (node->u.binary.left->data_type == IR_DATA_FIELD_REF
363 || node->u.binary.left->data_type == IR_DATA_FLOAT) {
364 struct cast_op cast_insn;
365
29fefef8
MD
366 if (node->u.binary.left->data_type == IR_DATA_FIELD_REF) {
367 cast_insn.op = FILTER_OP_CAST_TO_S64;
368 } else {
369 cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
370 }
8cf9540a
MD
371 ret = bytecode_push(&ctx->bytecode, &cast_insn,
372 1, sizeof(cast_insn));
373 if (ret)
374 return ret;
375 }
953192ba
MD
376 switch (node->u.logical.type) {
377 default:
378 fprintf(stderr, "[error] Unknown node type in %s\n",
379 __func__);
380 return -EINVAL;
381
382 case AST_OP_AND:
383 insn.op = FILTER_OP_AND;
384 break;
385 case AST_OP_OR:
386 insn.op = FILTER_OP_OR;
387 break;
388 }
389 insn.skip_offset = (uint16_t) -1UL; /* Temporary */
390 ret = bytecode_push_logical(&ctx->bytecode, &insn, 1, sizeof(insn),
391 &skip_offset_loc);
392 if (ret)
393 return ret;
394 /* Visit right child */
395 ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right);
396 if (ret)
397 return ret;
8cf9540a
MD
398 /* Cast to s64 if float or field ref */
399 if (node->u.binary.right->data_type == IR_DATA_FIELD_REF
400 || node->u.binary.right->data_type == IR_DATA_FLOAT) {
401 struct cast_op cast_insn;
402
29fefef8
MD
403 if (node->u.binary.right->data_type == IR_DATA_FIELD_REF) {
404 cast_insn.op = FILTER_OP_CAST_TO_S64;
405 } else {
406 cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
407 }
8cf9540a
MD
408 ret = bytecode_push(&ctx->bytecode, &cast_insn,
409 1, sizeof(cast_insn));
410 if (ret)
411 return ret;
412 }
953192ba
MD
413 /* We now know where the logical op can skip. */
414 target_loc = (uint16_t) bytecode_get_len(&ctx->bytecode->b);
415 ret = bytecode_patch(&ctx->bytecode,
416 &target_loc, /* Offset to jump to */
417 skip_offset_loc, /* Where to patch */
418 sizeof(uint16_t));
419 return ret;
420}
421
422/*
423 * Postorder traversal of the tree. We need the children result before
424 * we can evaluate the parent.
425 */
426static
427int recursive_visit_gen_bytecode(struct filter_parser_ctx *ctx,
428 struct ir_op *node)
429{
430 switch (node->op) {
431 case IR_OP_UNKNOWN:
432 default:
433 fprintf(stderr, "[error] Unknown node type in %s\n",
434 __func__);
435 return -EINVAL;
436
437 case IR_OP_ROOT:
438 return visit_node_root(ctx, node);
439 case IR_OP_LOAD:
440 return visit_node_load(ctx, node);
441 case IR_OP_UNARY:
442 return visit_node_unary(ctx, node);
443 case IR_OP_BINARY:
444 return visit_node_binary(ctx, node);
445 case IR_OP_LOGICAL:
446 return visit_node_logical(ctx, node);
447 }
448}
449
d00c599e 450__attribute__((visibility("hidden")))
953192ba
MD
451void filter_bytecode_free(struct filter_parser_ctx *ctx)
452{
453 free(ctx->bytecode);
454 ctx->bytecode = NULL;
455 free(ctx->bytecode_reloc);
456 ctx->bytecode_reloc = NULL;
457}
458
d00c599e 459__attribute__((visibility("hidden")))
953192ba
MD
460int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx)
461{
462 int ret;
463
464 ret = bytecode_init(&ctx->bytecode);
465 if (ret)
466 return ret;
467 ret = bytecode_init(&ctx->bytecode_reloc);
468 if (ret)
469 goto error;
470 ret = recursive_visit_gen_bytecode(ctx, ctx->ir_root);
471 if (ret)
472 goto error;
473
474 /* Finally, append symbol table to bytecode */
475 ctx->bytecode->b.reloc_table_offset = bytecode_get_len(&ctx->bytecode->b);
476 return bytecode_push(&ctx->bytecode, ctx->bytecode_reloc->b.data,
477 1, bytecode_get_len(&ctx->bytecode_reloc->b));
478
479error:
480 filter_bytecode_free(ctx);
481 return ret;
482}
This page took 0.040865 seconds and 4 git commands to generate.