2 * filter-visitor-generate-bytecode.c
4 * LTTng filter bytecode generation
6 * Copyright 2012 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
26 #include "filter-bytecode.h"
27 #include "filter-ir.h"
28 #include "filter-ast.h"
30 #include <common/macros.h>
33 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
36 //#define INIT_ALLOC_SIZE PAGE_SIZE
37 #define INIT_ALLOC_SIZE 4
40 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
43 static inline int fls(unsigned int x
)
49 if (!(x
& 0xFFFF0000U
)) {
53 if (!(x
& 0xFF000000U
)) {
57 if (!(x
& 0xF0000000U
)) {
61 if (!(x
& 0xC0000000U
)) {
65 if (!(x
& 0x80000000U
)) {
72 static inline int get_count_order(unsigned int count
)
76 order
= fls(count
) - 1;
77 if (count
& (count
- 1))
83 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
87 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
88 *fb
= calloc(alloc_len
, 1);
92 (*fb
)->alloc_len
= alloc_len
;
98 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
101 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
102 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
103 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
104 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
106 if (new_len
> LTTNG_FILTER_MAX_LEN
)
109 if (new_alloc_len
> old_alloc_len
) {
111 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
112 *fb
= realloc(*fb
, new_alloc_len
);
115 /* We zero directly the memory from start of allocation. */
116 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
117 (*fb
)->alloc_len
= new_alloc_len
;
119 (*fb
)->b
.len
+= padding
;
126 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
127 uint32_t align
, uint32_t len
)
131 offset
= bytecode_reserve(fb
, align
, len
);
134 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
139 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
140 struct logical_op
*data
,
141 uint32_t align
, uint32_t len
,
142 uint16_t *skip_offset
)
146 offset
= bytecode_reserve(fb
, align
, len
);
149 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
151 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
152 - (void *) &(*fb
)->b
.data
[0];
157 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
162 if (offset
>= (*fb
)->b
.len
) {
165 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
170 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
173 struct return_op insn
;
176 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
180 /* Generate end of bytecode instruction */
181 insn
.op
= FILTER_OP_RETURN
;
182 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
186 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
190 switch (node
->data_type
) {
191 case IR_DATA_UNKNOWN
:
193 fprintf(stderr
, "[error] Unknown data type in %s\n",
199 struct load_op
*insn
;
200 uint32_t insn_len
= sizeof(struct load_op
)
201 + strlen(node
->u
.load
.u
.string
) + 1;
203 insn
= calloc(insn_len
, 1);
206 insn
->op
= FILTER_OP_LOAD_STRING
;
207 strcpy(insn
->data
, node
->u
.load
.u
.string
);
208 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
212 case IR_DATA_NUMERIC
:
214 struct load_op
*insn
;
215 uint32_t insn_len
= sizeof(struct load_op
)
216 + sizeof(struct literal_numeric
);
218 insn
= calloc(insn_len
, 1);
221 insn
->op
= FILTER_OP_LOAD_S64
;
222 *(int64_t *) insn
->data
= node
->u
.load
.u
.num
;
223 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
229 struct load_op
*insn
;
230 uint32_t insn_len
= sizeof(struct load_op
)
231 + sizeof(struct literal_double
);
233 insn
= calloc(insn_len
, 1);
236 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
237 *(double *) insn
->data
= node
->u
.load
.u
.flt
;
238 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
242 case IR_DATA_FIELD_REF
:
244 struct load_op
*insn
;
245 uint32_t insn_len
= sizeof(struct load_op
)
246 + sizeof(struct field_ref
);
247 struct field_ref ref_offset
;
248 uint32_t reloc_offset_u32
;
249 uint16_t reloc_offset
;
251 insn
= calloc(insn_len
, 1);
254 insn
->op
= FILTER_OP_LOAD_FIELD_REF
;
255 ref_offset
.offset
= (uint16_t) -1U;
256 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
257 /* reloc_offset points to struct load_op */
258 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
259 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
263 reloc_offset
= (uint16_t) reloc_offset_u32
;
264 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
270 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
271 1, sizeof(reloc_offset
));
276 ret
= bytecode_push(&ctx
->bytecode_reloc
, node
->u
.load
.u
.ref
,
277 1, strlen(node
->u
.load
.u
.ref
) + 1);
285 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
288 struct unary_op insn
;
291 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
295 /* Generate end of bytecode instruction */
296 switch (node
->u
.unary
.type
) {
297 case AST_UNARY_UNKNOWN
:
299 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
305 case AST_UNARY_MINUS
:
306 insn
.op
= FILTER_OP_UNARY_MINUS
;
307 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
309 insn
.op
= FILTER_OP_UNARY_NOT
;
310 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
315 * Binary comparator nesting is disallowed. This allows fitting into
319 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
322 struct binary_op insn
;
325 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
328 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
332 switch (node
->u
.binary
.type
) {
335 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
341 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
346 insn
.op
= FILTER_OP_MUL
;
349 insn
.op
= FILTER_OP_DIV
;
352 insn
.op
= FILTER_OP_MOD
;
355 insn
.op
= FILTER_OP_PLUS
;
358 insn
.op
= FILTER_OP_MINUS
;
361 insn
.op
= FILTER_OP_RSHIFT
;
364 insn
.op
= FILTER_OP_LSHIFT
;
367 insn
.op
= FILTER_OP_BIN_AND
;
370 insn
.op
= FILTER_OP_BIN_OR
;
373 insn
.op
= FILTER_OP_BIN_XOR
;
377 insn
.op
= FILTER_OP_EQ
;
380 insn
.op
= FILTER_OP_NE
;
383 insn
.op
= FILTER_OP_GT
;
386 insn
.op
= FILTER_OP_LT
;
389 insn
.op
= FILTER_OP_GE
;
392 insn
.op
= FILTER_OP_LE
;
395 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
399 * A logical op always return a s64 (1 or 0).
402 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
405 struct logical_op insn
;
406 uint16_t skip_offset_loc
;
409 /* Visit left child */
410 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
413 /* Cast to s64 if float or field ref */
414 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
415 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
416 struct cast_op cast_insn
;
418 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
) {
419 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
421 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
423 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
424 1, sizeof(cast_insn
));
428 switch (node
->u
.logical
.type
) {
430 fprintf(stderr
, "[error] Unknown node type in %s\n",
435 insn
.op
= FILTER_OP_AND
;
438 insn
.op
= FILTER_OP_OR
;
441 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
442 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
446 /* Visit right child */
447 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
450 /* Cast to s64 if float or field ref */
451 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
452 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
453 struct cast_op cast_insn
;
455 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
) {
456 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
458 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
460 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
461 1, sizeof(cast_insn
));
465 /* We now know where the logical op can skip. */
466 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
467 ret
= bytecode_patch(&ctx
->bytecode
,
468 &target_loc
, /* Offset to jump to */
469 skip_offset_loc
, /* Where to patch */
475 * Postorder traversal of the tree. We need the children result before
476 * we can evaluate the parent.
479 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
485 fprintf(stderr
, "[error] Unknown node type in %s\n",
490 return visit_node_root(ctx
, node
);
492 return visit_node_load(ctx
, node
);
494 return visit_node_unary(ctx
, node
);
496 return visit_node_binary(ctx
, node
);
498 return visit_node_logical(ctx
, node
);
503 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
506 ctx
->bytecode
= NULL
;
507 free(ctx
->bytecode_reloc
);
508 ctx
->bytecode_reloc
= NULL
;
512 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
516 ret
= bytecode_init(&ctx
->bytecode
);
519 ret
= bytecode_init(&ctx
->bytecode_reloc
);
522 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
526 /* Finally, append symbol table to bytecode */
527 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
528 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
529 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
532 filter_bytecode_free(ctx
);