10

how viable is it to port a C++ application to Java bytecode using LLVM (I guess LLJVM)?

The thing is that we currently have a process written in C++ but a new client has made mandatory to been able to run the program in a multiplatform way, using the Java Virtual Machine with obviously no native code (no JNI). The idea is to be able to take the generated jar and copy then to different systems (Linux, Win, 32 bits - 64 bits) and it should just work.

Looking around looks like it is possible to compile C++ to LLVM IR code and then that code to java bytecode. There is no need of the generated code to be readable.

I have test a bit with similar things using emscripten, this takes C++ code and compile it to JavaScript. The result is valid JS but totally unreadable (looks like assambler).

  • Does anybody done a port of an application from C++ to Java bytecode using this tecnique?
  • What problems could we face?
  • Is a valid approach for production code?

To make more clear my point after some comments, maybe port is not well used, I do not expect readable source code as a result, just java bytecode, so it is not a 'port' which will be developed for anymore, just that the target plattform must be the java JVM not the native assamblear.

Note: I am aware that currently we have some non standard C++ and close source libraries, we are looking to removing this non standard code and all close source libraries and use Free Libre Open Source Software, so lets suppose all code is standard C++ code with all code available at compile time.

Note2: It is not an option to write portable C++ code and then compile it to the desired target platform, the compiled program must be multiplatform, thus the use of JVM .

Note3: Right now we are not looking into similar solutions applied to Python or other language base, but i would also like to heard about it. With this I mean that our target executable must be java bytecode but if there are options to compile C++ to valid python compiled code I would also like to hear about them.

8
  • not sure what you mean on the last sentence about Python, but Jython is exactly the same: use JVM instead of the Python VM, and used in exactly that scenario: programmers want to use Python, deployment must be on JVM.
    – Javier
    Commented Oct 30, 2013 at 14:09
  • How many lines of code are we talking about? It may be worth your time to rewrite it, but that is no simple decision. Also, if your code does any pointer arithmetic I would be curious to know how that gets handled when working on the JVM. Commented Oct 30, 2013 at 14:39
  • 1
    Debugging that should be fun O_o Commented Oct 30, 2013 at 16:37
  • @LeviMorrison. Well the code is quite extensive (various libraries dependencies for communications, utlity functions) but it is assumed that we have available all the code at compile time. And also if another client does not require it, we will still generate the native binary.
    – Javier Mr
    Commented Oct 31, 2013 at 12:41
  • @jozefg. About pointer aritmetics and debugging purposed I don't expect to be debuggable. Emscripten for example does the same thing but the target language is Javascript, you end just with a big byte array as the heap and bit wise operations for program counter and just operations with bytes no objects, strings or thing like that. I expect the result similar to assamblear in java bytecode, it could be assumed to not be debuggable.
    – Javier Mr
    Commented Oct 31, 2013 at 12:44

1 Answer 1

11

I really doubt this will work. You might be able to translate your code into Java byte code, but it will not magically translate library calls into equivalent calls to the Java runtime and libraries. There may not even be equivalent Java runtime calls! Even if you eliminate all proprietary libraries you're still left with the C++ standard library.

To make this concrete: your C++ program may contain a call to fprintf(). That function is implemented in the C standard library and it's perfectly legit for a C++ program to call it. The LLVM to LLJVM translator is probably not going to magically figure out the sequence of Java run time calls that will produce the equivalent result to fprintf() and substitute those in. To provide that facility would require essentially reimplementing the C and C++ runtimes in Java byte code.

There are some tools that perform C++ to Java translation but they only convert a handful of the simpler runtime library calls. The rest are left to you to figure out.

2
  • I see your point, but as far as i understand emscripten does something similar with the target being Javascript, if I did not misunderstood emscripten provides with a custom standard library in order to avoid what you pointed out (and even mappings for webGL through SDL library). But i can't find the equivalent for Java (LLJVM seems abandoned). I'm thinking in proposing llvm bytecode as a platform independent build (of course with no compilation branches depending on the platform, by API or data; using apr or similar)
    – Javier Mr
    Commented Nov 4, 2013 at 19:39
  • 3
    lljvm provides a C runtime library, partly as C compiled to JVM bytecode, and partly as Java classes. It's a pretty complete libc. You would need to create the equivalent for libstdc++. Also the lljvm backend doesn't actually support C++ at the moment anyway. I've been trying to fixup lljvm to work with a more recent llvm build. It's slow going since the llvm APIs and tools keep changing so much between releases. You can follow along here, it's almost in usable shape now. github.com/hyc/lljvm/tree/llvm3.3
    – hyc
    Commented Jan 24, 2014 at 23:32

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.