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
)
85 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
86 *fb
= calloc(alloc_len
, 1);
90 (*fb
)->alloc_len
= alloc_len
;
96 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
99 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
100 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
101 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
102 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
104 if (new_len
> LTTNG_FILTER_MAX_LEN
)
107 if (new_alloc_len
> old_alloc_len
) {
109 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
110 *fb
= realloc(*fb
, new_alloc_len
);
113 /* We zero directly the memory from start of allocation. */
114 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
115 (*fb
)->alloc_len
= new_alloc_len
;
117 (*fb
)->b
.len
+= padding
;
124 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
125 uint32_t align
, uint32_t len
)
129 offset
= bytecode_reserve(fb
, align
, len
);
132 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
137 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
138 struct logical_op
*data
,
139 uint32_t align
, uint32_t len
,
140 uint16_t *skip_offset
)
144 offset
= bytecode_reserve(fb
, align
, len
);
147 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
149 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
150 - (void *) &(*fb
)->b
.data
[0];
155 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
160 if (offset
>= (*fb
)->b
.len
) {
163 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
168 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
171 struct return_op insn
;
174 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
178 /* Generate end of bytecode instruction */
179 insn
.op
= FILTER_OP_RETURN
;
180 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
184 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
188 switch (node
->data_type
) {
189 case IR_DATA_UNKNOWN
:
191 fprintf(stderr
, "[error] Unknown data type in %s\n",
197 struct load_op
*insn
;
198 uint32_t insn_len
= sizeof(struct load_op
)
199 + strlen(node
->u
.load
.u
.string
) + 1;
201 insn
= calloc(insn_len
, 1);
204 insn
->op
= FILTER_OP_LOAD_STRING
;
205 strcpy(insn
->data
, node
->u
.load
.u
.string
);
206 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
210 case IR_DATA_NUMERIC
:
212 struct load_op
*insn
;
213 uint32_t insn_len
= sizeof(struct load_op
)
214 + sizeof(struct literal_numeric
);
216 insn
= calloc(insn_len
, 1);
219 insn
->op
= FILTER_OP_LOAD_S64
;
220 *(int64_t *) insn
->data
= node
->u
.load
.u
.num
;
221 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
227 struct load_op
*insn
;
228 uint32_t insn_len
= sizeof(struct load_op
)
229 + sizeof(struct literal_double
);
231 insn
= calloc(insn_len
, 1);
234 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
235 *(double *) insn
->data
= node
->u
.load
.u
.flt
;
236 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
240 case IR_DATA_FIELD_REF
:
242 struct load_op
*insn
;
243 uint32_t insn_len
= sizeof(struct load_op
)
244 + sizeof(struct field_ref
);
245 struct field_ref ref_offset
;
246 uint32_t reloc_offset_u32
;
247 uint16_t reloc_offset
;
249 insn
= calloc(insn_len
, 1);
252 insn
->op
= FILTER_OP_LOAD_FIELD_REF
;
253 ref_offset
.offset
= (uint16_t) -1U;
254 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
255 /* reloc_offset points to struct load_op */
256 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
257 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
261 reloc_offset
= (uint16_t) reloc_offset_u32
;
262 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
268 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
269 1, sizeof(reloc_offset
));
274 ret
= bytecode_push(&ctx
->bytecode_reloc
, node
->u
.load
.u
.ref
,
275 1, strlen(node
->u
.load
.u
.ref
) + 1);
283 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
286 struct unary_op insn
;
289 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
293 /* Generate end of bytecode instruction */
294 switch (node
->u
.unary
.type
) {
295 case AST_UNARY_UNKNOWN
:
297 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
303 case AST_UNARY_MINUS
:
304 insn
.op
= FILTER_OP_UNARY_MINUS
;
305 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
307 insn
.op
= FILTER_OP_UNARY_NOT
;
308 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
313 * Binary comparator nesting is disallowed. This allows fitting into
317 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
320 struct binary_op insn
;
323 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
326 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
330 switch (node
->u
.binary
.type
) {
333 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
339 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
344 insn
.op
= FILTER_OP_MUL
;
347 insn
.op
= FILTER_OP_DIV
;
350 insn
.op
= FILTER_OP_MOD
;
353 insn
.op
= FILTER_OP_PLUS
;
356 insn
.op
= FILTER_OP_MINUS
;
359 insn
.op
= FILTER_OP_RSHIFT
;
362 insn
.op
= FILTER_OP_LSHIFT
;
365 insn
.op
= FILTER_OP_BIN_AND
;
368 insn
.op
= FILTER_OP_BIN_OR
;
371 insn
.op
= FILTER_OP_BIN_XOR
;
375 insn
.op
= FILTER_OP_EQ
;
378 insn
.op
= FILTER_OP_NE
;
381 insn
.op
= FILTER_OP_GT
;
384 insn
.op
= FILTER_OP_LT
;
387 insn
.op
= FILTER_OP_GE
;
390 insn
.op
= FILTER_OP_LE
;
393 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
397 * A logical op always return a s64 (1 or 0).
400 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
403 struct logical_op insn
;
404 uint16_t skip_offset_loc
;
407 /* Visit left child */
408 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
411 /* Cast to s64 if float or field ref */
412 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
413 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
414 struct cast_op cast_insn
;
416 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
) {
417 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
419 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
421 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
422 1, sizeof(cast_insn
));
426 switch (node
->u
.logical
.type
) {
428 fprintf(stderr
, "[error] Unknown node type in %s\n",
433 insn
.op
= FILTER_OP_AND
;
436 insn
.op
= FILTER_OP_OR
;
439 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
440 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
444 /* Visit right child */
445 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
448 /* Cast to s64 if float or field ref */
449 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
450 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
451 struct cast_op cast_insn
;
453 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
) {
454 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
456 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
458 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
459 1, sizeof(cast_insn
));
463 /* We now know where the logical op can skip. */
464 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
465 ret
= bytecode_patch(&ctx
->bytecode
,
466 &target_loc
, /* Offset to jump to */
467 skip_offset_loc
, /* Where to patch */
473 * Postorder traversal of the tree. We need the children result before
474 * we can evaluate the parent.
477 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
483 fprintf(stderr
, "[error] Unknown node type in %s\n",
488 return visit_node_root(ctx
, node
);
490 return visit_node_load(ctx
, node
);
492 return visit_node_unary(ctx
, node
);
494 return visit_node_binary(ctx
, node
);
496 return visit_node_logical(ctx
, node
);
500 __attribute__((visibility("hidden")))
501 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
504 ctx
->bytecode
= NULL
;
505 free(ctx
->bytecode_reloc
);
506 ctx
->bytecode_reloc
= NULL
;
509 __attribute__((visibility("hidden")))
510 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
514 ret
= bytecode_init(&ctx
->bytecode
);
517 ret
= bytecode_init(&ctx
->bytecode_reloc
);
520 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
524 /* Finally, append symbol table to bytecode */
525 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
526 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
527 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
530 filter_bytecode_free(ctx
);