Idea: Run Time Compilation

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Mar 9 08:25:03 PST 2007


Johan Granberg wrote:
> Craig Black wrote:
> 
>> Since Walter is in the habit of adding super advanced features to D, I
>> thought it possible that this one might eventually make it in.  As always,
>> please let me know if I'm way off base.
>>
>> What if we could invoke the compiler from an application at run time and
>> generate a function that would be instantiated on the heap?  The function
>> would be defined by a text string and would be invoked via a function
>> pointer or delegate.  This would allow a script-like capability with
>> fully-optimized performance provided by the D compiler.  I think the D
>> compiler would be ideal for this since it is so fast.  Since the method
>> would be allocated on the heap dynamically, it could also be removed when
>> it is done being used.

I've actually been doing something very similar to this already, but I 
just write some source code to a file, compile it to a shared library, 
and load it :P.
Note, In my case, the generated source is C instead of D because 
http://dsource.org/projects/ddl/ says it's not compatible with current 
compilers, and the code I generate doesn't use any advanced features 
anyway. But once DDL is updated it should be quite possible to do this 
with D code as well.

This does of course require that the user has the compiler installed, 
but in my case I'm the only one running it, so that's not a problem here :).

This stuff is actually pretty easy to do as long as you're OK with using 
the compiler as an external program (and keep any source needed for 
compilation[1] around, obviously).


[1]: i.e. .h files for C/C++, .d/.di for D

> I like this idea but it sounds a bit tricky to implement (not to mention
> possible license issues where the compiler is made a part of the programs
> it compiles).

I think many Common Lisp compilers already do something like this, and 
license issues don't seem to be a problem. Just consider it an expanded 
runtime library :P.



More information about the Digitalmars-d mailing list