10

I'm looking for a way to compile C source code into high-performance Java bytecode. I've successfully used NestedVM, but the performance hit is not acceptable for a project I'm working on. I've also seen various open source projects aimed at this problem and a couple of commercial products. This SO question deals with general problem of converting non-Java into Java source, but I only want to go from C to Java bytecode.

What's the best way to compile C source code into high-performance, pure Java bytecode?

Community
  • 1
  • 1
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
  • What are you comparing the performance to? Are you saying the converted C code runs slower in the VM than native C code? or are you saying that it runs slower than Java under the VM? If it is the first then I'm don't think you can do anything about (maybe use JNI but that's not your question) – hhafez Jan 20 '09 at 01:32
  • NestedVM works by creating a virtual maching on top of the JVM. This leads to significant overhead, compared to an approach where the C source is compiled directly into bytecode. – Rich Apodaca Jan 21 '09 at 23:57

4 Answers4

9

Being the author of Cibyl, I might be biased here. Anyway, I've looked at the java bytecode generated by the axiomatic C compiler, and it is not efficient. NestedVM and Cibyl both works by compiling MIPS binaries and then translating the binary into Java bytecode. It's surprisingly efficient, with the main problem being memory access of 8- and 16-byte values (which needs to be done in multiple steps).

NestedVM and Cibyl have slightly different performance characteristics, with Cibyl typically being faster for integer-heavy workloads whereas NestedVM handles floats and doubles better. This is because Cibyl uses GCC soft-float support (though using "real" Java bytecode floating point instructions) while NestedVM translates MIPS FPU instructions.

Cibyl is also more targeted to J2ME environments, although it's definately useable on other platforms as well. My guess is that you would have more luck with either of them than with the Axiomatic C compiler.

sleske
  • 81,358
  • 34
  • 189
  • 227
  • I took the liberty of linking to Cibyl on Google code. I hope the link is the right one. – sleske Dec 18 '13 at 09:02
  • Finding your answer by Google after 7 years I am highly surprised, how and why it didn't get the needed attention in the opensource world. Anyways I've migrated your wonderful project from the shutting down google code to my github account: https://github.com/HorvathAkosPeter/cibyl – peterh Mar 30 '16 at 15:15
  • Thanks for the encouraging words! Anyway, I think Cibyl came a bit too late, just before smartphones took off. A year earlier and with some kind of J2ME "appstore" it would probably have seen more interest. Anyway, I don't consider it a failure. I had great fun implementing it, and used it to play through several of the old Sierra quests on my cellphone :-). Early versions of Waze also used it for J2ME and Blackberry (and thorugh a fork also on Windows phone). I also moved over the code to my github account a few years ago. – Simon Kågström Apr 01 '16 at 06:03
4

It's not exactly what you asked for, but Cibyl converts compiled C programs into JVM bytecode. It's the same idea as NestedVM (which you mentioned) but might be faster for your task being as how it's an independent implementation.

geocar
  • 9,085
  • 1
  • 29
  • 37
3

I believe some projects have attempted this, but there is just no way to deal with pointers without some pretty severe restrictions about what can access what (essentially they have to be converted into array indexes and arrays allocated instead of memory)

If you have C without too much reliance on pointers, and you want it into the JVM, you might just convert it to Java--should be pretty easy and the performance shouldn't be too bad. C still beats Java in most areas by about 2x with some areas much worse and in a few areas Java actually beats C (heap memory management, for one), but compared to most other languages (interpreted ones at least), java and c are 100x faster, so the difference between them is pretty meaningless from that point of view.

Bill K
  • 62,186
  • 18
  • 105
  • 157
  • Well, there is a very important difference: I have a C library (sqlite), and I want to integrate it into a java project without losing platform-independence (so SNI isn't ok). Speed is not so critical as generally for sqlite. So, what to do? Compile the library into java .class files, and integrate them. It is the solution, imho. – peterh Mar 30 '16 at 15:17
  • Would it be possible to find a java equivalent? Is this the same thing? http://www.tutorialspoint.com/sqlite/index.htm. When you want platform independence it's probably not good to stick with a C version because you will have to recompile or select a different library to link with whenever you go to a different architecture or want to optimize for a different CPU whereas the Java version just works and even re-optimizes itself for the CPU. – Bill K Mar 30 '16 at 16:46
1

try C2J software................

type c to java translator in google .you will get the link to download

feroz
  • 11
  • 1
  • Thanks to [feroz](http://stackoverflow.com/questions/459822/from-c-source-to-java-bytecode/1720807#1720807), I was reminded of [C2J](http://tech.novosoft-us.com/product_c2j.jsp), which looks like it's GPL-licensed (at least for the beta period - which started in... 2001?). Not sure if it uses a NestedVM-type approach or something else. Might be worth checking out again. – Rich Apodaca Nov 12 '09 at 16:59