Creating DLLs from within an application

Hello cfe-dev,

I'm investigating dynamic code generation and Clang seems to be the
perfect fit. I couldn't find an answer in Google - but maybe its just
my Google-fu. Anyway, my scenario is this: I'd like to create a
program that generates several .c files with trivial C functions.
These rely on no includes - only arithmetic operations on arrays of
doubles. I'd like to package up all these functions into a DLL/SO,
load it and call the functions from within the running application.
The parameters being passed in will be the arrays to doubles that
would have already been loaded into memory by the main program.

I just wanted to double-check with the mailing list that all of the
above can be done by linking at compile time against a clang library
(static or dynamic); my program would generate the .c files and the
clang infrastructure would do the magic to come up with the DLL/SO.
I'm fairly confident with the rest - traditional DLL/SO loading and
unloading - unless some one foresees any problems.

Many thanks for your time, and for a great compiler and surrounding
infrastructure.

Cheers

Marco

Hello cfe-dev,

I just wanted to double-check with the mailing list that all of the
above can be done by linking at compile time against a clang library
(static or dynamic); my program would generate the .c files and the
clang infrastructure would do the magic to come up with the DLL/SO.

Well, I didn't find an answer to my question but I did find cling:

http://root.cern.ch/drupal/content/cling

For anyone looking for this sort of thing, it doesn't look like the
worse place to start.

Cheers

Marco

Hello cfe-dev,

I'm investigating dynamic code generation and Clang seems to be the
perfect fit. I couldn't find an answer in Google - but maybe its just
my Google-fu. Anyway, my scenario is this: I'd like to create a
program that generates several .c files with trivial C functions.
These rely on no includes - only arithmetic operations on arrays of
doubles. I'd like to package up all these functions into a DLL/SO,
load it and call the functions from within the running application.
The parameters being passed in will be the arrays to doubles that
would have already been loaded into memory by the main program.

I just wanted to double-check with the mailing list that all of the
above can be done by linking at compile time against a clang library
(static or dynamic); my program would generate the .c files and the
clang infrastructure would do the magic to come up with the DLL/SO.
I'm fairly confident with the rest - traditional DLL/SO loading and
unloading - unless some one foresees any problems.

Many thanks for your time, and for a great compiler and surrounding
infrastructure.

Can you generate LLVM IR instead of C code or Clang ASTs? I'm not familiar with the Clang APIs, but I'm pretty sure the LLVM APIs are designed to do this (because this is more or less how the LLVM JIT works).

It might also be more efficient as you wouldn't need to go from C source -> Clang AST -> LLVM IR -> native code; you could just go from LLVM IR -> native code.

Just a thought,

-- John T.

Hi John,

Can you generate LLVM IR instead of C code or Clang ASTs? I'm not familiar
with the Clang APIs, but I'm pretty sure the LLVM APIs are designed to do
this (because this is more or less how the LLVM JIT works).

It might also be more efficient as you wouldn't need to go from C source ->
Clang AST -> LLVM IR -> native code; you could just go from LLVM IR ->
native code.

Unfortunately the "source code" given to me is a very simple subset of
C; to do that, I'd have to parse it myself into LLVM IR. I was hoping
for a more canned solution, if you see what I mean.

Thanks for your time.

Marco