[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Libjit] How to pass pointers through jit_function_apply()?
From: |
阿保 純一 |
Subject: |
[Libjit] How to pass pointers through jit_function_apply()? |
Date: |
Wed, 10 Jan 2018 01:48:35 +0900 |
I'm using these at gentoo linux on 32bit Pentium-M.
When I tried a brainf*ck interpreter code from somewhere and I builded it using
LibJIT downloaded on January 6th 2018(according to it's ChangeLog, it's
last-edited is 2017-11-23), I failed passing a pointer on to the function
LibJIT generate.
For example,
/* swap.c */
#include <stdio.h>
#include <jit/jit.h>
#include <jit/jit-dump.h>
static jit_function_t compile(jit_context_t c)
{
/*
void swap(jit_int *p1, jit_int *p2) {
jit_int v1 = *p1, v2 = *p2;
*p1 = v2;
*p2 = v1;
}
*/
jit_function_t f;
jit_type_t params[2];
jit_type_t signature;
jit_value_t o1p, o2p, o1, o2;
params[0] = jit_type_create_pointer(jit_type_int, 1);
params[1] = jit_type_copy(params[0]);
signature =
jit_type_create_signature(jit_abi_cdecl, jit_type_void, params, 2,
1);
f = jit_function_create(c, signature);
o1p = jit_value_get_param(f, 0);
o2p = jit_value_get_param(f, 1);
o1 = jit_insn_load_relative(f, o1p, 0, jit_type_int);
o2 = jit_insn_load_relative(f, o2p, 0, jit_type_int);
jit_insn_store_relative(f, o1p, 0, o2);
jit_insn_store_relative(f, o2p, 0, o1);
jit_insn_return(f, NULL);
jit_dump_function(stderr, f, "function");
jit_function_compile(f);
jit_dump_function(stderr, f, "function");
return f;
}
int main(void)
{
jit_int o1, o2;
jit_context_t c;
jit_function_t f;
c = jit_context_create();
jit_context_build_start(c);
f = compile(c);
jit_context_build_end(c);
scanf("%i,%i", &o1, &o2);
if (1) {
void (*ff) (int *, int *) = jit_function_to_closure(f);
ff(&o1, &o2);
} else {
int *do1 = &o1, *do2 = &o2;
void *a[] = { &do1, &do2 };
jit_function_apply(f, a, NULL);
}
printf("%i,%i\n", o1, o2);
jit_context_destroy(c);
return 0;
}
This code works well when using jit_function_to_closure(), but fails (Lucky to
happen nothing) when using jit_function_apply().
It seems jit_function_apply picks first argument only, and, passes it to a
JITed function without dereferencing.
/* swap1.c */
#include <stdio.h>
#include <jit/jit.h>
#include <jit/jit-dump.h>
static jit_function_t compile(jit_context_t c)
{
/*
void swap(jit_int *p1, jit_int *p2) {
jit_int v1 = *p1, v2 = *p2;
*p1 = v2;
*p2 = v1;
}
*/
jit_function_t f;
jit_type_t params[2];
jit_type_t signature;
jit_value_t o1p, o2p, o1, o2;
params[0] = jit_type_create_pointer(jit_type_int, 1);
params[1] = jit_type_copy(params[0]);
signature =
jit_type_create_signature(jit_abi_cdecl, jit_type_void, params, 2,
1);
f = jit_function_create(c, signature);
o1p = jit_value_get_param(f, 0);
/* same value of o1p? */ o2p = jit_value_get_param(f, 1);
o1 = jit_insn_load_relative(f, o1p, 0, jit_type_int);
o2 = jit_insn_load_relative(f, o2p,
/* need to adjust */ 4, jit_type_int);
jit_insn_store_relative(f, o1p, 0, o2);
jit_insn_store_relative(f, o2p, 4, o1);
jit_insn_return(f, NULL);
jit_dump_function(stderr, f, "swap");
jit_function_compile(f);
jit_dump_function(stderr, f, "swap");
return f;
}
int main(void)
{
jit_int o1, o2;
jit_context_t c;
jit_function_t f;
c = jit_context_create();
jit_context_build_start(c);
f = compile(c);
jit_context_build_end(c);
scanf("%i,%i", &o1, &o2);
if (0) {
/* don't works for this example. */
void (*ff) (int *, int *) = jit_function_to_closure(f);
ff(&o1, &o2);
} else {
int *do1 = &o1, *do2 = &o2;
//void *a[] = { &do1, &do2 };
void *a[] = { &o1, &o2 };
jit_function_apply(f, a, NULL);
}
printf("%i,%i\n", o1, o2);
jit_context_destroy(c);
return 0;
}
In the second codes, jit_function_to_closure() doesn't work instead.
(And this code relys on the addresses of local variables of "o1" and "o2".)
Maybe I'm making mistakes at invoking jit_function_apply().
Any help is welcome.
swap.c
Description: Text Data
swap1.c
Description: Text Data
- [Libjit] How to pass pointers through jit_function_apply()?,
阿保 純一 <=