libjit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Libjit] Can jit_value_t variables be reused for different functions?


From: Peng Yu
Subject: [Libjit] Can jit_value_t variables be reused for different functions?
Date: Tue, 19 Feb 2019 23:39:13 -0600

Hi,

In the following example, the #if macro works whether the condition is
0 or 1. So I can always reuse existing jit_value_t variables for
different functions. Or this example just happened to work, but there
may be cases in which reuse the variables can cause problems? Thanks.

$ cat main.c
// vim: set noexpandtab tabstop=2:

#include <stdio.h>
#include <jit/jit.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
        jit_context_t context = jit_context_create();
        jit_context_build_start(context);

        jit_type_t params[2];
        params[0] = jit_type_int;
        params[1] = jit_type_int;
        jit_type_t signature = jit_type_create_signature(jit_abi_cdecl,
jit_type_int, params, 2, 1);

        jit_function_t F1 = jit_function_create(context, signature);
        jit_function_t F2 = jit_function_create(context, signature);
        jit_type_free(signature);

        jit_value_t x1 = jit_value_get_param(F1, 0);
        jit_value_t y1 = jit_value_get_param(F1, 1);
        jit_value_t r1 = jit_insn_add(F1, x1, y1);
        jit_insn_return(F1, r1);

#if 0
        jit_value_t x2 = jit_value_get_param(F2, 0);
        jit_value_t y2 = jit_value_get_param(F2, 1);
        jit_value_t temp_args[2];
        temp_args[0] = x2;
        temp_args[1] = y2;
#else
        x1 = jit_value_get_param(F2, 0);
        y1 = jit_value_get_param(F2, 1);
        jit_value_t temp_args[2];
        temp_args[0] = x1;
        temp_args[1] = y1;
#endif
        unsigned int num_args = 2;
        int flags = 0;
        jit_value_t r2 = jit_insn_call(F2, "F2", F1, NULL, temp_args, num_args, 
flags);
        jit_insn_return(F2, r2);

        jit_function_compile(F1);
        jit_function_compile(F2);

        jit_context_build_end(context);

        jit_int arg1 = atoi(argv[1]);
        jit_int arg2 = atoi(argv[2]);
        void *args[2];
        args[0] = &arg1;
        args[1] = &arg2;
        jit_int result;
        jit_function_apply(F2, args, &result);
        printf("%d\n", (int)result);

        jit_context_destroy(context);
        return 0;
}


-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]