Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RE: Creating an XSUB on the fly

3 views
Skip to first unread message

bulk 88

unread,
Jul 21, 2012, 3:31:52 AM7/21/12
to dcmerte...@gmail.com, per...@perl.org


--Forwarded Message Attachment--
Date: Tue, 20 Mar 2012 11:34:37 -0500
Subject: Creating an XSUB on the fly
From: dcmerte...@gmail.com
To: per...@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

unread,
Jul 21, 2012, 7:28:17 AM7/21/12
to bulk 88, per...@perl.org
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
--
"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

unread,
Jul 21, 2012, 3:57:36 PM7/21/12
to bulk 88, per...@perl.org

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

On Jul 21, 2012 2:01 PM, "bulk 88" <bul...@hotmail.com> wrote:


> Date: Sat, 21 Jul 2012 06:28:17 -0500
> Subject: Re: Creating an XSUB on the fly
> From: dcmerte...@gmail.com
> To: bul...@hotmail.com
> CC: per...@perl.org

>
> 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?
0 new messages