LibJIT - Perl bindings for GNU LibJIT
use LibJIT qw/:all/; my $ctx = jit_context_create; jit_context_build_start $ctx; my $sig = jit_type_create_signature jit_abi_cdecl, jit_type_nint, [ jit_type_nint, jit_type_nint ], 1; my $fun = jit_function_create $ctx, $sig; my ($i, $j) = map jit_value_get_param($fun, $_), 0 .. 1; my $prod = jit_insn_mul $fun, $i, $j; jit_insn_return $fun, $prod; jit_function_compile $fun; jit_context_build_end $ctx;
As straightforward as bindings can be - for every function and constant defined by the C library a Perl subroutine is exported.
Where C function takes two arguments, a pointer and number of elements, Perl function would take a single array reference instead. This should be straightforward in most cases, except, probably, jit_function_apply.
jit_function_apply
In C, jit_function_apply has this signature (see LibJIT documentation for description of each argument):
int jit_function_apply (jit_function_t func, void **args, void *return_area)
In Perl, args is represented as a reference to an array of strings, where each string contains binary representation of the argument, as produced by pack. return_area should be a scalar variable, when jit_function_apply returns it will be set to the binary representation of the return value, that can be decoded with unpack.
args
pack
return_area
unpack
FFI::Raw in combination with jit_function_to_closure can be a cleaner way to call compiled functions.
jit_function_to_closure
GNU LibJIT home page and documentation: https://www.gnu.org/software/libjit/.
Vickenty Fesunov, <cpan+libjit@setattr.net>
Copyright (C) 2015 by Vickenty Fesunov.
The library is distributed under the terms of the GNU Lesser General Public License. See the LICENSE file for details.
To install LibJIT, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LibJIT
CPAN shell
perl -MCPAN -e shell install LibJIT
For more information on module installation, please visit the detailed CPAN module installation guide.