11

Suppose I have a software and I want to make cross-plataform plugins. You compile the plugin for a virtual machine, and any platform running my software would be able to run this code.

I am wondering if it is possible to use LLVM interpreter and bytecode for this purpose. Also, I am wondering if does make sense using LLVM for this purpose instead of something else, i.e. is it what LLVM was made for?

1
  • 1
    You should look at llvm.org , they have plenty of documentation and I've tested jitting as part of a proof-of-concept program some years ago. You should go through the kaleidoscope tutorial, which is also a good tutorial if you start into compiler construction. Commented Nov 8, 2011 at 16:57

5 Answers 5

8

I'm not sure that LLVM was designed for it. However, I doubt there is anything that hasn't been done using LLVM1

Other virtual-machines based script engines are specifically created for the job:

If you're looking for embeddable virtual machines:

  • IKVM supports embedding JVM and CLR in a bridged mode (interoperable)
  • Parrot supports embedding (and includes a Python interpreter; mind you, you can just run python bytecode images)
  • Perl has similar architecture and supports embedding
  • Javascript supports embedding (not sure about the architecture of v8, but I guess it would use a virtual machine)
  • Mono's CLR engine supports embedding: http://www.mono-project.com/Embedding_Mono

1 including compiling c++ information to javascript to run in your browser...

6
  • Thank you for the answer. It is being helpful. I am avoiding Lua because I am looking for something more like "virtual machine" and less like "interpreted". Actually, I do not have a real problem where performance is a issue, but I am researching a virtual machine solution with LLVM anyway. Commented Nov 8, 2011 at 16:55
  • @AndréPuel: What's the difference? First off, it's a blurry line in any case: Lua compiles to bytecode and hands that to a bytecode VM (you can also dump the bytecode to a binary file and run that, though it may have portability issues depending on how the Lua VM was compiled). Second, any sensible distiction between "interpreter" and "VM" I can imagine boils down to minor implementation details, which probably shouldn't concern you.
    – user395760
    Commented Nov 8, 2011 at 17:03
  • @delnan I appreciate your comment and I totally agree with you. But it is not a real scenario, lets say I am just wondering about embedding virtual machine in a software. If I had a real scenario where I needed to make extensions I probably would use Lua or something like that. Commented Nov 8, 2011 at 17:07
  • @AndréPuel: My question is simply: What do you call a "virtual machine" and how is the Lua implementation not one?
    – user395760
    Commented Nov 8, 2011 at 17:10
  • @AndréPuel Yup, I'm guessing you'd find 'embedding a Parrot virtual machine' sound more attractive than 'embedding a Python interpreter'. But it is really the same (although, with the VM you could do other languages on top of the same VM... I'm not sure what that would be useful for)
    – sehe
    Commented Nov 8, 2011 at 17:13
8

There is VMIR (https://github.com/andoma/vmir) which is a LLVM bitcode interpreter / JIT engine that's intended to be embedded into other apps.

Disclaimer: I'm the author of it and it's still work-in-progress but works reasonable well.

1
  • 1
    This project is awesome! Really nice! I've loved it. Commented Aug 14, 2016 at 16:09
6

In theory, there exist a limited subset of LLVM IR which can be portable across various platforms. You shall not specify alignments, you shall not bitcast pointers to integral types, you must avoid intrinsics, etc. Which means - you can't immediately use a code generated by a stock C compiler (llvm-gcc, Clang, whatever), unless you specify a limited target for it and implement sanitising LLVM passes. Another issue is that the bitcode format from different LLVM versions is not guaranteed to be compatible.

In practice, I would not go there. Mono is a reasonably small, embeddable, fast VM, and all the .NET stack of tools is available for it. VM itself is pretty low-level (as long as you do not care about the verifyability).

0
2

LLVM includes an interpreter, so if you can build this interpreter for your target platforms, you can then evaluate LLVM bitcode on the fly.

It's apparently not so fast though.

0

In their classic discussion (that you do not want to miss if you're a fan of open source, LLVM, compilers) about LLVM vs libJIT, that has happened long before LLVM became famous and established, the author of libJIT Rhys Weatherley raised this particular issue, he stated that LLVM is not suitable to be embedded, while Chris Lattner, the author of LLVM stated that otherwise, it is modular and you can use it in any possible fashion including embedding only the parts you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.