Compiler as dll

Sandeep Kakarlapudi sandeep.iitkgpspammenot at gmail.com
Tue Jan 27 07:47:23 PST 2009


I have been wishing for something similar though for different reasons. Often I have code (in C++) that is executed for a large amount of data like in stream programming and becomes a major hotspot for the application. Think software graphics pipelines. Further, this code has lots of branches and since its executed over many elements they are essentially branches in inner-loops. The values over which they branch are unknown till runtime and the number of combinations of the values can be very very high. Here are the basic attempts to solve that I'm aware of and the problems with each:

1) Programmer codes for each combination of conditions - Takes a lot of time and is hard to maintain. 

2) Use metaprogramming to generate all combinations at compile time. I usually go for this using C++ templates. This can bloat the binary size very quickly when the number of values are many. 

3) Generate the code at runtime. Often the values don't change too often and hence generating the code at runtime would help. But here the   programmer often has dive into assembly. IIRC C# libraries allow you to target its bytecode. 

4) With the philosophy of Life Long Program Optimization, the code is monitored/ traced and automatically optimized. Even if such a language targets some intermediate bytecode, there can be cases where it might significantly outperform native code by using this approach. Might not be suitable for a language that targets native code?? Not suited for systems programming. 

5) Perhaps with some support in the language/library the application can invoke the optimizer and give the know values and the optimizer can create an optimized version a function for those values. Is this feasible? 

In summary, what I'm asking for is provision for the programmer to direct code specialization at runtime. This becomes a subset of bearophile's request so I would also like to see a more generic and standard(!) way of runtime compiling / optimization of high level code. 

I also wonder if any of the std library functionality can be better implemented given such runtime code compilation and optimization features.

Sandeep 

bearophile Wrote:

> In C# 4.0 the VM becomes a module of the standard lib, so it can be used from normal C# code, to do various things.
> 
> While writing a genetic programming program (and in some other situations) I find it useful to have an eval() function, like in Lisp/Scheme and most scripting languages.
> 
> I think such functionalities may be added to DMD and LDC too:
> - 99% of the compiler can be moved into a DLL (or a shared dynamic lib of some kind) and the DMD/LDC executable can then become very little, it can load such dynamic lib, and it has just to manage the I/O from/to disk and the command line arguments, ad little else.
> - By default such dynamic lib isn't loaded by D programs, so the size of D programs is unchanged (and they don't need such dll to run).
> - A module can be added to the std lib (Tango and Phobos) that loads such dll and offers a function like eval() that accepts in input a string of code (or an AST too, if you want) and compiles/runs it.
> 
> Bye,
> bearophile




More information about the Digitalmars-d mailing list