Compile, link and run on the fly

Hi,

I am working on my diploma thesis "Consideration of Virtual Machines Suitability for Packet Decoding". One of VMs I am testing is LLVM. Currently I have a filter written in LLVM IR which consist of 3 modules: filter, decoder and filter_rule. The last one is generated using my own-written compiler. I`d like to ask if there is a possibility to generate filter_rule, then link it with other two modules and run on the fly. Now I compile my own BPF-like minilanguage to IR, then translate it to bitcode using llvm-as, then link it with other two modules with llvm-link and finally run with lli. I don`t want to use makefile I want to do everything on the fly in my compiler. Thanks.

Radovan Baranec

Is your compiler written in C++ or something that can interface with
it easily? If so, you should probably check out the JIT, and the
tutorial here:
http://llvm.org/docs/tutorial/

In particular, this bit is relevant to using a JIT:
http://llvm.org/docs/tutorial/LangImpl4.html#jit

It sounds like you're generating LLVM IL and dumping it to a .ll file
in your own compiler, so you'll probably have to just add some driver
code to your compiler that drives llvm-as, opt, llvm-link, and llc.

Reid

First of all thanks for fast replay. I appreciated it:-)

Is your compiler written in C++ or something that can interface with
it easily? If so, you should probably check out the JIT, and the
tutorial here:

Yes my compiler is written in C++ and I had followed tutorial you mentioned.

It sounds like you're generating LLVM IL and dumping it to a .ll file
in your own compiler, so you'll probably have to just add some driver
code to your compiler that drives llvm-as, opt, llvm-link, and llc.

Yes I am generating LLVM IR and dumping it to, so far only, .s file using my compiler. Other two modules are already manually written so I need the one that I generate to link with other two and run. And this was precisely my question about. Is it even possible to do so? Can I generate only one module, link it with other two and run, everything on the fly in my compiler? It will be like glued everything together in one binary file (compiler), all phases: compiling, linking, running using single binary. From your answer I partially assumed that it is possible. Can you recommend me some other tutorial or sample code?

Rado

Dòa Thu, 15 Apr 2010 22:19:28 +0300 Reid Kleckner <rnk@mit.edu> napísal:

No, you can link the modules and run them on the fly. If you look at
the source code for llvm-link, opt, and lli, you can see that they are
thin wrappers around LLVM library functions that you should be able to
call yourself from your own code.

So you would probably compile each module, link them together with
Linker::LinkModules, optimize them via your own FuncitonPassManager,
and run them with the JIT ExecutionEngine.

Reid

see tools/llvmc/example/mcc16 for an example driver.