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"
31 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
34 //#define INIT_ALLOC_SIZE PAGE_SIZE
35 #define INIT_ALLOC_SIZE 4
38 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
41 static inline int fls(unsigned int x
)
47 if (!(x
& 0xFFFF0000U
)) {
51 if (!(x
& 0xFF000000U
)) {
55 if (!(x
& 0xF0000000U
)) {
59 if (!(x
& 0xC0000000U
)) {
63 if (!(x
& 0x80000000U
)) {
70 static inline int get_count_order(unsigned int count
)
74 order
= fls(count
) - 1;
75 if (count
& (count
- 1))
81 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
83 *fb
= calloc(sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
, 1);
87 (*fb
)->alloc_len
= INIT_ALLOC_SIZE
;
93 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
96 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
97 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
98 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode
) + new_len
;
99 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
101 if (new_len
> LTTNG_FILTER_MAX_LEN
)
104 if (new_alloc_len
> old_alloc_len
) {
106 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
107 *fb
= realloc(*fb
, new_alloc_len
);
110 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
111 (*fb
)->alloc_len
= new_alloc_len
;
113 (*fb
)->b
.len
+= padding
;
120 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
121 uint32_t align
, uint32_t len
)
125 offset
= bytecode_reserve(fb
, align
, len
);
128 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
133 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
134 struct logical_op
*data
,
135 uint32_t align
, uint32_t len
,
136 uint16_t *skip_offset
)
140 offset
= bytecode_reserve(fb
, align
, len
);
143 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
145 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
146 - (void *) &(*fb
)->b
.data
[0];
151 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
156 if (offset
>= (*fb
)->b
.len
) {
159 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
164 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
167 struct return_op insn
;
170 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
174 /* Generate end of bytecode instruction */
175 insn
.op
= FILTER_OP_RETURN
;
176 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
180 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
184 switch (node
->data_type
) {
185 case IR_DATA_UNKNOWN
:
187 fprintf(stderr
, "[error] Unknown data type in %s\n",
193 struct load_op
*insn
;
194 uint32_t insn_len
= sizeof(struct load_op
)
195 + strlen(node
->u
.load
.u
.string
) + 1;
197 insn
= calloc(insn_len
, 1);
200 insn
->op
= FILTER_OP_LOAD_STRING
;
201 strcpy(insn
->data
, node
->u
.load
.u
.string
);
202 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
206 case IR_DATA_NUMERIC
:
208 struct load_op
*insn
;
209 uint32_t insn_len
= sizeof(struct load_op
)
210 + sizeof(struct literal_numeric
);
212 insn
= calloc(insn_len
, 1);
215 insn
->op
= FILTER_OP_LOAD_S64
;
216 *(int64_t *) insn
->data
= node
->u
.load
.u
.num
;
217 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
223 struct load_op
*insn
;
224 uint32_t insn_len
= sizeof(struct load_op
)
225 + sizeof(struct literal_double
);
227 insn
= calloc(insn_len
, 1);
230 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
231 *(double *) insn
->data
= node
->u
.load
.u
.flt
;
232 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
236 case IR_DATA_FIELD_REF
:
238 struct load_op
*insn
;
239 uint32_t insn_len
= sizeof(struct load_op
)
240 + sizeof(struct field_ref
);
241 struct field_ref ref_offset
;
242 uint32_t reloc_offset_u32
;
243 uint16_t reloc_offset
;
245 insn
= calloc(insn_len
, 1);
248 insn
->op
= FILTER_OP_LOAD_FIELD_REF
;
249 ref_offset
.offset
= (uint16_t) -1U;
250 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
251 /* reloc_offset points to struct load_op */
252 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
253 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
257 reloc_offset
= (uint16_t) reloc_offset_u32
;
258 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
264 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
265 1, sizeof(reloc_offset
));
270 ret
= bytecode_push(&ctx
->bytecode_reloc
, node
->u
.load
.u
.ref
,
271 1, strlen(node
->u
.load
.u
.ref
) + 1);
279 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
282 struct unary_op insn
;
285 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
289 /* Generate end of bytecode instruction */
290 switch (node
->u
.unary
.type
) {
291 case AST_UNARY_UNKNOWN
:
293 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
299 case AST_UNARY_MINUS
:
300 insn
.op
= FILTER_OP_UNARY_MINUS
;
301 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
303 insn
.op
= FILTER_OP_UNARY_NOT
;
304 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
309 * Binary comparator nesting is disallowed. This allows fitting into
313 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
316 struct binary_op insn
;
319 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
322 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
326 switch (node
->u
.binary
.type
) {
329 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
335 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
340 insn
.op
= FILTER_OP_MUL
;
343 insn
.op
= FILTER_OP_DIV
;
346 insn
.op
= FILTER_OP_MOD
;
349 insn
.op
= FILTER_OP_PLUS
;
352 insn
.op
= FILTER_OP_MINUS
;
355 insn
.op
= FILTER_OP_RSHIFT
;
358 insn
.op
= FILTER_OP_LSHIFT
;
361 insn
.op
= FILTER_OP_BIN_AND
;
364 insn
.op
= FILTER_OP_BIN_OR
;
367 insn
.op
= FILTER_OP_BIN_XOR
;
371 insn
.op
= FILTER_OP_EQ
;
374 insn
.op
= FILTER_OP_NE
;
377 insn
.op
= FILTER_OP_GT
;
380 insn
.op
= FILTER_OP_LT
;
383 insn
.op
= FILTER_OP_GE
;
386 insn
.op
= FILTER_OP_LE
;
389 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
393 * A logical op always return a s64 (1 or 0).
396 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
399 struct logical_op insn
;
400 uint16_t skip_offset_loc
;
403 /* Visit left child */
404 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
407 /* Cast to s64 if float or field ref */
408 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
409 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
410 struct cast_op cast_insn
;
412 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
) {
413 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
415 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
417 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
418 1, sizeof(cast_insn
));
422 switch (node
->u
.logical
.type
) {
424 fprintf(stderr
, "[error] Unknown node type in %s\n",
429 insn
.op
= FILTER_OP_AND
;
432 insn
.op
= FILTER_OP_OR
;
435 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
436 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
440 /* Visit right child */
441 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
444 /* Cast to s64 if float or field ref */
445 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
446 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
447 struct cast_op cast_insn
;
449 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
) {
450 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
452 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
454 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
455 1, sizeof(cast_insn
));
459 /* We now know where the logical op can skip. */
460 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
461 ret
= bytecode_patch(&ctx
->bytecode
,
462 &target_loc
, /* Offset to jump to */
463 skip_offset_loc
, /* Where to patch */
469 * Postorder traversal of the tree. We need the children result before
470 * we can evaluate the parent.
473 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
479 fprintf(stderr
, "[error] Unknown node type in %s\n",
484 return visit_node_root(ctx
, node
);
486 return visit_node_load(ctx
, node
);
488 return visit_node_unary(ctx
, node
);
490 return visit_node_binary(ctx
, node
);
492 return visit_node_logical(ctx
, node
);
496 __attribute__((visibility("hidden")))
497 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
500 ctx
->bytecode
= NULL
;
501 free(ctx
->bytecode_reloc
);
502 ctx
->bytecode_reloc
= NULL
;
505 __attribute__((visibility("hidden")))
506 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
510 ret
= bytecode_init(&ctx
->bytecode
);
513 ret
= bytecode_init(&ctx
->bytecode_reloc
);
516 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
520 /* Finally, append symbol table to bytecode */
521 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
522 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
523 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
526 filter_bytecode_free(ctx
);