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
25 #include <common/align.h>
26 #include <common/compat/string.h>
28 #include "filter-bytecode.h"
29 #include "filter-ir.h"
30 #include "filter-ast.h"
32 #include <common/macros.h>
35 #define max_t(type, a, b) ((type) ((a) > (b) ? (a) : (b)))
38 #define INIT_ALLOC_SIZE 4
41 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
44 static inline int get_count_order(unsigned int count
)
48 order
= lttng_fls(count
) - 1;
49 if (count
& (count
- 1))
55 int bytecode_init(struct lttng_filter_bytecode_alloc
**fb
)
59 alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + INIT_ALLOC_SIZE
;
60 *fb
= calloc(alloc_len
, 1);
64 (*fb
)->alloc_len
= alloc_len
;
70 int32_t bytecode_reserve(struct lttng_filter_bytecode_alloc
**fb
, uint32_t align
, uint32_t len
)
73 uint32_t padding
= offset_align((*fb
)->b
.len
, align
);
74 uint32_t new_len
= (*fb
)->b
.len
+ padding
+ len
;
75 uint32_t new_alloc_len
= sizeof(struct lttng_filter_bytecode_alloc
) + new_len
;
76 uint32_t old_alloc_len
= (*fb
)->alloc_len
;
78 if (new_len
> LTTNG_FILTER_MAX_LEN
)
81 if (new_alloc_len
> old_alloc_len
) {
82 struct lttng_filter_bytecode_alloc
*newptr
;
85 max_t(uint32_t, 1U << get_count_order(new_alloc_len
), old_alloc_len
<< 1);
86 newptr
= realloc(*fb
, new_alloc_len
);
90 /* We zero directly the memory from start of allocation. */
91 memset(&((char *) *fb
)[old_alloc_len
], 0, new_alloc_len
- old_alloc_len
);
92 (*fb
)->alloc_len
= new_alloc_len
;
94 (*fb
)->b
.len
+= padding
;
101 int bytecode_push(struct lttng_filter_bytecode_alloc
**fb
, const void *data
,
102 uint32_t align
, uint32_t len
)
106 offset
= bytecode_reserve(fb
, align
, len
);
109 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
114 int bytecode_push_logical(struct lttng_filter_bytecode_alloc
**fb
,
115 struct logical_op
*data
,
116 uint32_t align
, uint32_t len
,
117 uint16_t *skip_offset
)
121 offset
= bytecode_reserve(fb
, align
, len
);
124 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
126 (void *) &((struct logical_op
*) &(*fb
)->b
.data
[offset
])->skip_offset
127 - (void *) &(*fb
)->b
.data
[0];
132 int bytecode_patch(struct lttng_filter_bytecode_alloc
**fb
,
137 if (offset
>= (*fb
)->b
.len
) {
140 memcpy(&(*fb
)->b
.data
[offset
], data
, len
);
145 int visit_node_root(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
148 struct return_op insn
;
151 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.root
.child
);
155 /* Generate end of bytecode instruction */
156 insn
.op
= FILTER_OP_RETURN
;
157 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
161 int visit_node_load(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
165 switch (node
->data_type
) {
166 case IR_DATA_UNKNOWN
:
168 fprintf(stderr
, "[error] Unknown data type in %s\n",
174 struct load_op
*insn
;
175 uint32_t insn_len
= sizeof(struct load_op
)
176 + strlen(node
->u
.load
.u
.string
) + 1;
178 insn
= calloc(insn_len
, 1);
181 insn
->op
= FILTER_OP_LOAD_STRING
;
182 strcpy(insn
->data
, node
->u
.load
.u
.string
);
183 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
187 case IR_DATA_NUMERIC
:
189 struct load_op
*insn
;
190 uint32_t insn_len
= sizeof(struct load_op
)
191 + sizeof(struct literal_numeric
);
193 insn
= calloc(insn_len
, 1);
196 insn
->op
= FILTER_OP_LOAD_S64
;
197 memcpy(insn
->data
, &node
->u
.load
.u
.num
, sizeof(int64_t));
198 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
204 struct load_op
*insn
;
205 uint32_t insn_len
= sizeof(struct load_op
)
206 + sizeof(struct literal_double
);
208 insn
= calloc(insn_len
, 1);
211 insn
->op
= FILTER_OP_LOAD_DOUBLE
;
212 memcpy(insn
->data
, &node
->u
.load
.u
.flt
, sizeof(double));
213 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
217 case IR_DATA_FIELD_REF
: /* fall-through */
218 case IR_DATA_GET_CONTEXT_REF
:
220 struct load_op
*insn
;
221 uint32_t insn_len
= sizeof(struct load_op
)
222 + sizeof(struct field_ref
);
223 struct field_ref ref_offset
;
224 uint32_t reloc_offset_u32
;
225 uint16_t reloc_offset
;
227 insn
= calloc(insn_len
, 1);
230 switch(node
->data_type
) {
231 case IR_DATA_FIELD_REF
:
232 insn
->op
= FILTER_OP_LOAD_FIELD_REF
;
234 case IR_DATA_GET_CONTEXT_REF
:
235 insn
->op
= FILTER_OP_GET_CONTEXT_REF
;
241 ref_offset
.offset
= (uint16_t) -1U;
242 memcpy(insn
->data
, &ref_offset
, sizeof(ref_offset
));
243 /* reloc_offset points to struct load_op */
244 reloc_offset_u32
= bytecode_get_len(&ctx
->bytecode
->b
);
245 if (reloc_offset_u32
> LTTNG_FILTER_MAX_LEN
- 1) {
249 reloc_offset
= (uint16_t) reloc_offset_u32
;
250 ret
= bytecode_push(&ctx
->bytecode
, insn
, 1, insn_len
);
256 ret
= bytecode_push(&ctx
->bytecode_reloc
, &reloc_offset
,
257 1, sizeof(reloc_offset
));
262 ret
= bytecode_push(&ctx
->bytecode_reloc
, node
->u
.load
.u
.ref
,
263 1, strlen(node
->u
.load
.u
.ref
) + 1);
271 int visit_node_unary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
274 struct unary_op insn
;
277 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.unary
.child
);
281 /* Generate end of bytecode instruction */
282 switch (node
->u
.unary
.type
) {
283 case AST_UNARY_UNKNOWN
:
285 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
291 case AST_UNARY_MINUS
:
292 insn
.op
= FILTER_OP_UNARY_MINUS
;
293 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
295 insn
.op
= FILTER_OP_UNARY_NOT
;
296 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
301 * Binary comparator nesting is disallowed. This allows fitting into
305 int visit_node_binary(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
308 struct binary_op insn
;
311 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
314 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
318 switch (node
->u
.binary
.type
) {
321 fprintf(stderr
, "[error] Unknown unary node type in %s\n",
327 fprintf(stderr
, "[error] Unexpected logical node type in %s\n",
332 insn
.op
= FILTER_OP_MUL
;
335 insn
.op
= FILTER_OP_DIV
;
338 insn
.op
= FILTER_OP_MOD
;
341 insn
.op
= FILTER_OP_PLUS
;
344 insn
.op
= FILTER_OP_MINUS
;
347 insn
.op
= FILTER_OP_RSHIFT
;
350 insn
.op
= FILTER_OP_LSHIFT
;
353 insn
.op
= FILTER_OP_BIN_AND
;
356 insn
.op
= FILTER_OP_BIN_OR
;
359 insn
.op
= FILTER_OP_BIN_XOR
;
363 insn
.op
= FILTER_OP_EQ
;
366 insn
.op
= FILTER_OP_NE
;
369 insn
.op
= FILTER_OP_GT
;
372 insn
.op
= FILTER_OP_LT
;
375 insn
.op
= FILTER_OP_GE
;
378 insn
.op
= FILTER_OP_LE
;
381 return bytecode_push(&ctx
->bytecode
, &insn
, 1, sizeof(insn
));
385 * A logical op always return a s64 (1 or 0).
388 int visit_node_logical(struct filter_parser_ctx
*ctx
, struct ir_op
*node
)
391 struct logical_op insn
;
392 uint16_t skip_offset_loc
;
395 /* Visit left child */
396 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.left
);
399 /* Cast to s64 if float or field ref */
400 if ((node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
401 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
)
402 || node
->u
.binary
.left
->data_type
== IR_DATA_FLOAT
) {
403 struct cast_op cast_insn
;
405 if (node
->u
.binary
.left
->data_type
== IR_DATA_FIELD_REF
406 || node
->u
.binary
.left
->data_type
== IR_DATA_GET_CONTEXT_REF
) {
407 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
409 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
411 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
412 1, sizeof(cast_insn
));
416 switch (node
->u
.logical
.type
) {
418 fprintf(stderr
, "[error] Unknown node type in %s\n",
423 insn
.op
= FILTER_OP_AND
;
426 insn
.op
= FILTER_OP_OR
;
429 insn
.skip_offset
= (uint16_t) -1UL; /* Temporary */
430 ret
= bytecode_push_logical(&ctx
->bytecode
, &insn
, 1, sizeof(insn
),
434 /* Visit right child */
435 ret
= recursive_visit_gen_bytecode(ctx
, node
->u
.binary
.right
);
438 /* Cast to s64 if float or field ref */
439 if ((node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
440 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
)
441 || node
->u
.binary
.right
->data_type
== IR_DATA_FLOAT
) {
442 struct cast_op cast_insn
;
444 if (node
->u
.binary
.right
->data_type
== IR_DATA_FIELD_REF
445 || node
->u
.binary
.right
->data_type
== IR_DATA_GET_CONTEXT_REF
) {
446 cast_insn
.op
= FILTER_OP_CAST_TO_S64
;
448 cast_insn
.op
= FILTER_OP_CAST_DOUBLE_TO_S64
;
450 ret
= bytecode_push(&ctx
->bytecode
, &cast_insn
,
451 1, sizeof(cast_insn
));
455 /* We now know where the logical op can skip. */
456 target_loc
= (uint16_t) bytecode_get_len(&ctx
->bytecode
->b
);
457 ret
= bytecode_patch(&ctx
->bytecode
,
458 &target_loc
, /* Offset to jump to */
459 skip_offset_loc
, /* Where to patch */
465 * Postorder traversal of the tree. We need the children result before
466 * we can evaluate the parent.
469 int recursive_visit_gen_bytecode(struct filter_parser_ctx
*ctx
,
475 fprintf(stderr
, "[error] Unknown node type in %s\n",
480 return visit_node_root(ctx
, node
);
482 return visit_node_load(ctx
, node
);
484 return visit_node_unary(ctx
, node
);
486 return visit_node_binary(ctx
, node
);
488 return visit_node_logical(ctx
, node
);
493 void filter_bytecode_free(struct filter_parser_ctx
*ctx
)
501 ctx
->bytecode
= NULL
;
504 if (ctx
->bytecode_reloc
) {
505 free(ctx
->bytecode_reloc
);
506 ctx
->bytecode_reloc
= NULL
;
511 int filter_visitor_bytecode_generate(struct filter_parser_ctx
*ctx
)
515 ret
= bytecode_init(&ctx
->bytecode
);
518 ret
= bytecode_init(&ctx
->bytecode_reloc
);
521 ret
= recursive_visit_gen_bytecode(ctx
, ctx
->ir_root
);
525 /* Finally, append symbol table to bytecode */
526 ctx
->bytecode
->b
.reloc_table_offset
= bytecode_get_len(&ctx
->bytecode
->b
);
527 return bytecode_push(&ctx
->bytecode
, ctx
->bytecode_reloc
->b
.data
,
528 1, bytecode_get_len(&ctx
->bytecode_reloc
->b
));
531 filter_bytecode_free(ctx
);