Compile a string?

Hi,

I'm trying to compile a string into a Module so that I can link and
then JIT it. What would be the best way of doing this?

Thanks,

Daniel

I’m trying to compile a string into a Module so that I can link and
then JIT it. What would be the best way of doing this?

Not quite sure what you mean, but if you’re just trying to get a global variable into a module you can use a GlobalVariable for that purpose - there are some helper routines to create a string. If this isn’t what you were wondering then would you give a bit more information?

Thanks!

-eric

Sorry, what I mean is I've trying to make/find a function like this:

Module *compile_string(const char *source)

that could be used like this:

Module *m = compile_string("int function(int a, int b) { return a + b; }");

I hope that makes things clearer. Does such a function exist within
clang, or will I have to setup the components to do that? As far as I
can tell, I'll have to use a Preprocessor to start with, but how do I
then generate the AST in order to lower to LLVM's IR.

Daniel

Aaah. Entirely differently. I'd look through the clang driver for ideas
on how to generate a module based on incoming C code (this is C
code yes?).

-eric

Hi Daniel,

Take a look at the clang-interpreter example for most of it. Instead
of using CompilerInvocation::CreateFromArgs, you will want to
construct a CompilerInvocation by hand, including an input file name,
and then use the remapping arguments to associate your string with
that file.

- Daniel

Hi,

So I create the CompilerInvocation and remap with
addRemappedFile(llvm::StringRef From, const llvm::MemoryBuffer * To)
in PreprocessorOptions. Sounds easy enough [=

Thanks,

Daniel