Filter: cast double directly to s64
[lttng-tools.git] / src / lib / lttng-ctl / filter-visitor-generate-bytecode.c
index 25128b6a2fc6b9f2bd1826f66efbe8909d8f4a23..a9e741cd21ae2983dbb1ba28d949bbb6e01643e6 100644 (file)
@@ -200,6 +200,24 @@ int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node)
                free(insn);
                return ret;
        }
+       case IR_DATA_FLOAT:
+       {
+               struct load_op *insn;
+               uint32_t insn_len = sizeof(struct load_op)
+                       + sizeof(struct literal_double);
+
+               insn = calloc(insn_len, 1);
+               if (!insn)
+                       return -ENOMEM;
+               insn->op = FILTER_OP_LOAD_DOUBLE;
+               insn->reg = reg_sel(node);
+               if (insn->reg == REG_ERROR)
+                       return -EINVAL;
+               *(double *) insn->data = node->u.load.u.flt;
+               ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
+               free(insn);
+               return ret;
+       }
        case IR_DATA_FIELD_REF:
        {
                struct load_op *insn;
@@ -217,9 +235,8 @@ int visit_node_load(struct filter_parser_ctx *ctx, struct ir_op *node)
                memcpy(insn->data, &ref_offset, sizeof(ref_offset));
                if (insn->reg == REG_ERROR)
                        return -EINVAL;
-               /* reloc_offset points to struct field_ref */
+               /* reloc_offset points to struct load_op */
                reloc_offset = bytecode_get_len(&ctx->bytecode->b);
-               reloc_offset += sizeof(struct load_op);
                ret = bytecode_push(&ctx->bytecode, insn, 1, insn_len);
                if (ret) {
                        free(insn);
@@ -360,6 +377,9 @@ int visit_node_binary(struct filter_parser_ctx *ctx, struct ir_op *node)
        return bytecode_push(&ctx->bytecode, &insn, 1, sizeof(insn));
 }
 
+/*
+ * A logical op always return a s64 (1 or 0).
+ */
 static
 int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node)
 {
@@ -372,6 +392,22 @@ int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node)
        ret = recursive_visit_gen_bytecode(ctx, node->u.binary.left);
        if (ret)
                return ret;
+       /* Cast to s64 if float or field ref */
+       if (node->u.binary.left->data_type == IR_DATA_FIELD_REF
+                       || node->u.binary.left->data_type == IR_DATA_FLOAT) {
+               struct cast_op cast_insn;
+
+               if (node->u.binary.left->data_type == IR_DATA_FIELD_REF) {
+                       cast_insn.op = FILTER_OP_CAST_TO_S64;
+               } else {
+                       cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
+               }
+               cast_insn.reg = REG_R0;
+               ret = bytecode_push(&ctx->bytecode, &cast_insn,
+                                       1, sizeof(cast_insn));
+               if (ret)
+                       return ret;
+       }
        switch (node->u.logical.type) {
        default:
                fprintf(stderr, "[error] Unknown node type in %s\n",
@@ -394,6 +430,22 @@ int visit_node_logical(struct filter_parser_ctx *ctx, struct ir_op *node)
        ret = recursive_visit_gen_bytecode(ctx, node->u.binary.right);
        if (ret)
                return ret;
+       /* Cast to s64 if float or field ref */
+       if (node->u.binary.right->data_type == IR_DATA_FIELD_REF
+                       || node->u.binary.right->data_type == IR_DATA_FLOAT) {
+               struct cast_op cast_insn;
+
+               if (node->u.binary.right->data_type == IR_DATA_FIELD_REF) {
+                       cast_insn.op = FILTER_OP_CAST_TO_S64;
+               } else {
+                       cast_insn.op = FILTER_OP_CAST_DOUBLE_TO_S64;
+               }
+               cast_insn.reg = REG_R0;
+               ret = bytecode_push(&ctx->bytecode, &cast_insn,
+                                       1, sizeof(cast_insn));
+               if (ret)
+                       return ret;
+       }
        /* We now know where the logical op can skip. */
        target_loc = (uint16_t) bytecode_get_len(&ctx->bytecode->b);
        ret = bytecode_patch(&ctx->bytecode,
This page took 0.023868 seconds and 4 git commands to generate.