views:

957

answers:

6

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?

+5  A: 

Um, that's called "a compiler". You're asking for a C compiler that targets the JVM.

Google for that and you will find the Axiomatic Ansi C Compiler.

Charlie Martin
+2  A: 

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
+2  A: 

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
+4  A: 

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.

+1  A: 

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

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

feroz
A: 

Thanks to feroz, I was reminded of C2J, 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