Discussion:
Creating an XSUB on the fly
(too old to reply)
bulk 88
2012-07-21 07:31:52 UTC
Permalink
--Forwarded Message Attachment--
Date: Tue, 20 Mar 2012 11:34:37 -0500
Subject: Creating an XSUB on the fly
From: ***@gmail.com
To: perl-***@perl.org

Hello everybody -

I'm looking into hooking the Tiny C Compiler into Perl. Among other things, it can JIT-compile a string of C code and let you get a function pointer (or a pointer to any other structure of the compiled code that is globally scoped) that you can subsequently call from your original C program. I would like to try to use this to build Perl CVs/XSUBs, but I see no interface for creating an XSUB that is not installed into a package.


The easy work around is to have a designated package into which all such xsubs are installed and simply use newxs (see http://perldoc.perl.org/perlembed.html). The name given to the Perl-side function would be composed from a hashing algorithm of some sort to avoid collisions. Still, I would really like to create simple function references without having to create the entry in the package.


Thoughts? Wisdom?
David
.......................................................
_______________________________________________________
Per, http://perl5.git.perl.org/perl.git/blob/d845901fab02ff3b498515ad6e2d06c711724d24:/op.c#l7375 , I think if the package name is NULL, you get an anonymous CV that has no package with a refcount of 1.
David Mertens
2012-07-21 11:28:17 UTC
Permalink
bulk88 -

That's exactly what I needed! Thank you! Now I have to figure out how
to add args processing to this jit-compiled C function, but that's a
different problem. :-)

David
Post by bulk 88
--Forwarded Message Attachment--
Date: Tue, 20 Mar 2012 11:34:37 -0500
Subject: Creating an XSUB on the fly
Hello everybody -
I'm looking into hooking the Tiny C Compiler into Perl. Among other things, it can JIT-compile a string of C code and let you get a function pointer (or a pointer to any other structure of the compiled code that is globally scoped) that you can subsequently call from your original C program. I would like to try to use this to build Perl CVs/XSUBs, but I see no interface for creating an XSUB that is not installed into a package.
The easy work around is to have a designated package into which all such xsubs are installed and simply use newxs (see http://perldoc.perl.org/perlembed.html). The name given to the Perl-side function would be composed from a hashing algorithm of some sort to avoid collisions. Still, I would really like to create simple function references without having to create the entry in the package.
Thoughts? Wisdom?
David
.......................................................
_______________________________________________________
Per, http://perl5.git.perl.org/perl.git/blob/d845901fab02ff3b498515ad6e2d06c711724d24:/op.c#l7375 , I think if the package name is NULL, you get an anonymous CV that has no package with a refcount of 1.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
David Mertens
2012-07-21 19:57:36 UTC
Permalink
In this case, Perl stack args. The easy solution would be to include
EXTERN.h, perl.h, etc (as with a normal XS file), but that leads to a lot
of work for the preprocessor. This work targets true jit compiling, so the
less work I throw at the preprocessor, the better. My plan here is to comb
over those header files and pull out the most useful bits into fine-grained
pieces so function writers only pull in what they need. One of those pieces
would be Perl stack manipulation.

An alternative would be to simply return a function pointer that function
writers could wrap with a Foreign Function Interface caller. A few months
ago I was pushing Reini and others on their Ctypes work, and Joel Berger on
Alien::Base so I could distribute libffi as a Ctypes dependency. Actually,
I think this would be a simpler solution to the problem rather than digging
around the Perl XS headers. For me, work on both fronts has stopped as my
attention has shifted to other projects.

David
Date: Sat, 21 Jul 2012 06:28:17 -0500
Subject: Re: Creating an XSUB on the fly
bulk88 -
That's exactly what I needed! Thank you! Now I have to figure out how
to add args processing to this jit-compiled C function, but that's a
different problem. :-)
David
C stack args or Perl stack args?
Loading...