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
) {
110 struct lttng_filter_bytecode_alloc
*newptr
;
113 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
114 newptr
= realloc(*fb
, new_alloc_len
);
118 /* We zero directly the memory from start of allocation. */
119 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
120 (*fb
)->alloc_len
= new_alloc_len
;
122 (*fb
)->b
.len
+= padding
;
129 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
130 uint32_t align
, uint32_t len
)
134 offset
= bytecode_reserve(fb
, align
, len
);
137 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
142 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
143 struct logical_op
*data
,
144 uint32_t align
, uint32_t len
,
145 uint16_t *skip_offset
)
149 offset
= bytecode_reserve(fb
, align
, len
);
152 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
154 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
155 - (void *) &(*fb
)->b
.data
[0];
160 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
165 if (offset
>= (*fb
)->b
.len
) {
168 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
173 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
176 struct return_op insn
;
179 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
183 /* Generate end of bytecode instruction */
184 insn
.op
= FILTER_OP_RETURN
;
185 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
189 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
193 switch (node
->data_type
) {
194 case IR_DATA_UNKNOWN
:
196 fprintf(stderr
, "[error] Unknown data type in %s\n",
202 struct load_op
*insn
;
203 uint32_t insn_len
= sizeof(struct load_op
)
204 + strlen(node
->u
.load
.u
.string
) + 1;
206 insn
= calloc(insn_len
, 1);
209 insn
->op
= FILTER_OP_LOAD_STRING
;
210 strcpy(insn
->data
, node
->u
.load
.u
.string
);
211 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
215 case IR_DATA_NUMERIC
:
217 struct load_op
*insn
;
218 uint32_t insn_len
= sizeof(struct load_op
)
219 + sizeof(struct literal_numeric
);
221 insn
= calloc(insn_len
, 1);
224 insn
->op
= FILTER_OP_LOAD_S64
;
225 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
226 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
232 struct load_op
*insn
;
233 uint32_t insn_len
= sizeof(struct load_op
)
234 + sizeof(struct literal_double
);
236 insn
= calloc(insn_len
, 1);
239 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
240 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
241 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
245 case IR_DATA_FIELD_REF
: /* fall-through */
246 case IR_DATA_GET_CONTEXT_REF
:
248 struct load_op
*insn
;
249 uint32_t insn_len
= sizeof(struct load_op
)
250 + sizeof(struct field_ref
);
251 struct field_ref ref_offset
;
252 uint32_t reloc_offset_u32
;
253 uint16_t reloc_offset
;
255 insn
= calloc(insn_len
, 1);
258 switch(node
->data_type
) {
259 case IR_DATA_FIELD_REF
:
260 insn
->op
= FILTER_OP_LOAD_FIELD_REF
;
262 case IR_DATA_GET_CONTEXT_REF
:
263 insn
->op
= FILTER_OP_GET_CONTEXT_REF
;
269 ref_offset
.offset
= (uint16_t) -1U;
270 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
271 /* reloc_offset points to struct load_op */
272 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
273 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
277 reloc_offset
= (uint16_t) reloc_offset_u32
;
278 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
284 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
285 1, sizeof(reloc_offset
));
290 ret
= bytecode_push(&ctx
->bytecode_reloc
, node
->u
.load
.u
.ref
,
291 1, strlen(node
->u
.load
.u
.ref
) + 1);
299 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
302 struct unary_op insn
;
305 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
309 /* Generate end of bytecode instruction */
310 switch (node
->u
.unary
.type
) {
311 case AST_UNARY_UNKNOWN
:
313 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
319 case AST_UNARY_MINUS
:
320 insn
.op
= FILTER_OP_UNARY_MINUS
;
321 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
323 insn
.op
= FILTER_OP_UNARY_NOT
;
324 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
329 * Binary comparator nesting is disallowed. This allows fitting into
333 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
336 struct binary_op insn
;
339 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
342 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
346 switch (node
->u
.binary
.type
) {
349 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
355 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
360 insn
.op
= FILTER_OP_MUL
;
363 insn
.op
= FILTER_OP_DIV
;
366 insn
.op
= FILTER_OP_MOD
;
369 insn
.op
= FILTER_OP_PLUS
;
372 insn
.op
= FILTER_OP_MINUS
;
375 insn
.op
= FILTER_OP_RSHIFT
;
378 insn
.op
= FILTER_OP_LSHIFT
;
381 insn
.op
= FILTER_OP_BIN_AND
;
384 insn
.op
= FILTER_OP_BIN_OR
;
387 insn
.op
= FILTER_OP_BIN_XOR
;
391 insn
.op
= FILTER_OP_EQ
;
394 insn
.op
= FILTER_OP_NE
;
397 insn
.op
= FILTER_OP_GT
;
400 insn
.op
= FILTER_OP_LT
;
403 insn
.op
= FILTER_OP_GE
;
406 insn
.op
= FILTER_OP_LE
;
409 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
413 * A logical op always return a s64 (1 or 0).
416 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
419 struct logical_op insn
;
420 uint16_t skip_offset_loc
;
423 /* Visit left child */
424 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
427 /* Cast to s64 if float or field ref */
428 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
429 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
)
430 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
431 struct cast_op cast_insn
;
433 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
434 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
) {
435 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
437 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
439 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
440 1, sizeof(cast_insn
));
444 switch (node
->u
.logical
.type
) {
446 fprintf(stderr
, "[error] Unknown node type in %s\n",
451 insn
.op
= FILTER_OP_AND
;
454 insn
.op
= FILTER_OP_OR
;
457 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
458 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
462 /* Visit right child */
463 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
466 /* Cast to s64 if float or field ref */
467 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
468 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
)
469 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
470 struct cast_op cast_insn
;
472 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
473 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
) {
474 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
476 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
478 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
479 1, sizeof(cast_insn
));
483 /* We now know where the logical op can skip. */
484 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
485 ret
= bytecode_patch(&ctx
->bytecode
,
486 &target_loc
, /* Offset to jump to */
487 skip_offset_loc
, /* Where to patch */
493 * Postorder traversal of the tree. We need the children result before
494 * we can evaluate the parent.
497 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
503 fprintf(stderr
, "[error] Unknown node type in %s\n",
508 return visit_node_root(ctx
, node
);
510 return visit_node_load(ctx
, node
);
512 return visit_node_unary(ctx
, node
);
514 return visit_node_binary(ctx
, node
);
516 return visit_node_logical(ctx
, node
);
521 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
525 ctx
->bytecode
= NULL
;
528 if (ctx
->bytecode_reloc
) {
529 free(ctx
->bytecode_reloc
);
530 ctx
->bytecode_reloc
= NULL
;
535 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
539 ret
= bytecode_init(&ctx
->bytecode
);
542 ret
= bytecode_init(&ctx
->bytecode_reloc
);
545 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
549 /* Finally, append symbol table to bytecode */
550 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
551 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
552 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
555 filter_bytecode_free(ctx
);