Combining C and java

J

John Eskie

I'm trying to do something which I'm not sure is even possible.

I made a code generator which has a C/C++ output writer module and will in
future also have a java output writer module.
These modules will write out source code.

The generated code will need a runtime engine which is small and must be
written in the native language. I wrote one for C and now I'd like to reuse
the same source for java.

I'm unsure if there are any C compilers that can generate java bytecode but
a preprocessor would probably do in my case.

What I have in mind is something like:

typedef struct _foo
{
unsigned char H1[24];
unsigned char H2[24];
} foo;

becomming something like:

#ifdef JAVA
#define U1 byte
#else
#define U1 unsigned char
#endif

#ifdef JAVA
class foo
#else
typedef struct _foo
#endif
{
U1 H1[24];
U2 H2[24];
}
#ifndef JAVA
foo;
}

Atleast something along those lines.

The idea is to twist out those minor differences between the languages.
Before someone says this is impossible I'd like to say that my runtime will
be very small and won't have every possible language feature included so
there should be a chance to develop it.
The alternative is to have 2 codebases but thats bad if you want to have
them in sync.

Thanks in advance.
-- John
 
K

kim bruning

John Eskie said:
I'm trying to do something which I'm not sure is even possible.
I made a code generator which has a C/C++ output writer module and will in
future also have a java output writer module.
These modules will write out source code.
The generated code will need a runtime engine which is small and must be
written in the native language. I wrote one for C and now I'd like to reuse
the same source for java.
I'm unsure if there are any C compilers that can generate java bytecode but
a preprocessor would probably do in my case.

I wonder if you can configure gcc to do this?

searching:

fsf/unesco free software directory:
http://www.gnu.org/directory/GNU/

free software directory reference to gcc:
http://www.gnu.org/directory/gcc.html

gcc homepage:
http://gcc.gnu.org/

relevant information to be found at:

gcc for java:
http://gcc.gnu.org/java/index.html


Please drop me a mail if you can get it to work!


hope this helps,
read you soon,
Kim
 
J

John Eskie

I wonder if you can configure gcc to do this?
Thanks for the suggestion but I don't think it can be done with gcc itself.

GCC seems not to be able to generate java binary code. Atleast I didn't find
it yet.

There is a frontend for GCC which you gave a link for which is called GCJ.
This is just another java compiler with the feature of beeing able to
produce native code from java source code. This is not really useful to me
unfortunatly.
The whole reason for me writing the runtime in C is to make it native code
for multiply platforms. Switching to java will not allow me that.

-- John
 
R

Roedy Green

GCC seems not to be able to generate java binary code. Atleast I didn't find
it yet.

not very likely. It would have to be a subset of C. Java has a strong
safety net which is visible in the JVM. C lets you do many dangerous
and platform specific things.
 
C

Chris Uppal

John said:
I made a code generator which has a C/C++ output writer module and will in
future also have a java output writer module.
These modules will write out source code.

The generated code will need a runtime engine which is small and must be
written in the native language. I wrote one for C and now I'd like to
reuse the same source for java.

My gut feeling is that you'd be wasting your time -- the languages are much too
different for it to be worthwhile trying to mask the differences, and that
you'd be spending longer trying to shoehorn the two together than you'd be
saving by having two different code-bases (at least when they are both small,
as you say).

Why not try this ? Write (in whatever language you fancy) a pre-processor that
converts your existing C engine into rough Java. If you find that you can do a
complete conversion and you are happy with the end result, then keep your
converter tool and re-use it in the future. But if you find that it's
difficult to do *all* the conversions, then settle for something that just does
90% of the dog-work, and then finish the conversion off by hand. That'll leave
you with two code bases, but the bulk of the effort has been done for you by
the script.

-- chris
 
K

kim bruning

My apologies for this late response. I've recently switched ISPs, and
things tend to get lost in-between.

John said:
Thanks for the suggestion but I don't think it can be done with gcc itself.

GCC seems not to be able to generate java binary code. Atleast I didn't find
it yet.

It looks like you're right. Apparently it *is* possible to configure gcc
to target quite a number of different binary platforms, see:

http://gcc.gnu.org/install/specific.html

for instance. Just not for jvm, yet, apparently.

It actually surprises me that nobody has thought to add support for the
jvm as a target platform yet.

I've heard stories that it isn't entirely too hard to add support for
additional platforms to GCC. The fact that so many platforms are already
supported is testament to this.

If you're still interested in generating code to a jvm target, perhaps
it might be interesting to contact the gcc team and pose it as a
suggestion? They'd likely be able to tell you how hard it is to add the
option to their compiler.


I hope this is still of some use,
sincerely,
Kim Bruning
 
K

kim bruning

My apologies for this late response. I've recently switched ISPs, and
things tend to get lost in-between.

John said:
Thanks for the suggestion but I don't think it can be done with gcc itself.

GCC seems not to be able to generate java binary code. Atleast I didn't find
it yet.

It looks like you're right. Apparently it *is* possible to configure gcc
to target quite a number of different binary platforms, see:

http://gcc.gnu.org/install/specific.html

for instance. Just not for jvm, yet, apparently.

It actually surprises me that nobody has thought to add support for the
jvm as a target platform yet.

I've heard stories that it isn't entirely too hard to add support for
additional platforms to GCC. The fact that so many platforms are already
supported is testament to this.

If you're still interested in generating code to a jvm target, perhaps
it might be interesting to contact the gcc team and pose it as a
suggestion? They'd likely be able to tell you how hard it is to add the
option to their compiler.


I hope this is still of some use,
sincerely,
Kim Bruning
 
M

Mike Yawn

kim said:
My apologies for this late response. I've recently switched ISPs, and
things tend to get lost in-between.




It looks like you're right. Apparently it *is* possible to configure gcc
to target quite a number of different binary platforms, see:

http://gcc.gnu.org/install/specific.html

for instance. Just not for jvm, yet, apparently.

It actually surprises me that nobody has thought to add support for the
jvm as a target platform yet.

I've heard stories that it isn't entirely too hard to add support for
additional platforms to GCC. The fact that so many platforms are already
supported is testament to this.

If you're still interested in generating code to a jvm target, perhaps
it might be interesting to contact the gcc team and pose it as a
suggestion? They'd likely be able to tell you how hard it is to add the
option to their compiler.


I hope this is still of some use,
sincerely,
Kim Bruning

I suspect it would be very difficult to get a C compiler to target the
JVM. The JVM bytecodes were designed to implement a strongly-typed
language (Java). While compilers for other strongly-typed languages
might be able to target the JVM, I can't imagine how some of the crap C
allows you to get away with could be mapped to a valid bytecode sequence.

Mike
 
J

Jezuch

Mike said:
I suspect it would be very difficult to get a C compiler to target the
JVM. The JVM bytecodes were designed to implement a strongly-typed
language (Java). While compilers for other strongly-typed languages
might be able to target the JVM, I can't imagine how some of the crap C
allows you to get away with could be mapped to a valid bytecode sequence.

But GCC is *not* a C compiler. It is among others a C compiler :) It can
also compile C++, Ada, Fortran and Java, both to native and byte-code.

see http://www.gnu.org/software/gcc/java/

"GCJ is a portable, optimizing, ahead-of-time compiler for the Java
Programming Language. It can compile:
* Java source code directly to native machine code,
* Java source code to Java bytecode (class files),
* and Java bytecode to native machine code."
 
K

kim bruning

Jezuch said:
Mike Yawn wrote:

But GCC is *not* a C compiler. It is among others a C compiler :) It can
also compile C++, Ada, Fortran and Java, both to native and byte-code.

see http://www.gnu.org/software/gcc/java/

"GCJ is a portable, optimizing, ahead-of-time compiler for the Java
Programming Language. It can compile:
* Java source code directly to native machine code,
* Java source code to Java bytecode (class files),
* and Java bytecode to native machine code."

Heh, cool!

"In IT whenever you say something is impossible, there's always
someone who will will stand up the and point out that not only is it
possible, but that it's already being done." - I wonder who said this?

Thank you very much. I'd better start looking at gcj more seriously for
myself too.

read you soon,
Kim Bruning
 
P

Paul Lutus

Jezuch said:
But GCC is *not* a C compiler. It is among others a C compiler :) It can
also compile C++, Ada, Fortran and Java, both to native and byte-code.

see http://www.gnu.org/software/gcc/java/

"GCJ is a portable, optimizing, ahead-of-time compiler for the Java
Programming Language. It can compile:
* Java source code directly to native machine code,
* Java source code to Java bytecode (class files),
* and Java bytecode to native machine code."

Yes, but only for Java 1.1, and that with important limitations. An
important qualification.
 
T

Thomas Weidenfeller

Jezuch said:
But GCC is *not* a C compiler. It is among others a C compiler :) It can
also compile C++, Ada, Fortran and Java, both to native and byte-code.

see http://www.gnu.org/software/gcc/java/

"GCJ is a portable, optimizing, ahead-of-time compiler for the Java
Programming Language. It can compile:
* Java source code directly to native machine code,
* Java source code to Java bytecode (class files),
* and Java bytecode to native machine code."


I think we all know that it is possible to compile Java to Java byte
code.

The question was to compile C to Java byte code. And(!) to get a
program that indeed works the same as if the C code would have been
compiled to native machine code directly.

/Thomas
 
J

John Eskie

I think we all know that it is possible to compile Java to Java byte
code.

The question was to compile C to Java byte code. And(!) to get a
program that indeed works the same as if the C code would have been
compiled to native machine code directly.

Thats right. I think I came to the conclusion that it was not possible with
GCC or GCJ. I concluded I will go via the JNI way.
But this raises new problems. One is that I will need to native library for
every platform I support. I don't know java good enough to say if JNI is
actually supported on all it's platforms but I think it should be.
The other problem is if people actually accept this solution. After all java
is similar to saying "write once, execute anywhere". By having the JNI it
becomes "write once and run where I want it to run". I will naturally make
sure to cover the major platforms but I got no reallife experience if people
actually accept solutions like this.

-- John
 
J

Jezuch

Paul said:
Yes, but only for Java 1.1, and that with important limitations. An
important qualification.

I'm not sure about this, the main limitation is libraries, mostly lack
od Swing and parts of AWT (it will compile, but Toolkit won't work, for
example). And I've successfully compiled programs written for 1.3.
 
J

Jezuch

Thomas said:
The question was to compile C to Java byte code. And(!) to get a
program that indeed works the same as if the C code would have been
compiled to native machine code directly.

Ok, then I missed this part :)
 
K

kim bruning

John said:
Thats right. I think I came to the conclusion that it was not possible with
GCC or GCJ.

Well, there *is* experimental support to be found here:
http://www.itee.uq.edu.au/~cristina/uqbt.html#gcc-jvm

Unfortunately there probably won't be mainstram gcc support in time for
you to use it in this case. This is a 1999 project in fact, and for some
reason people don't appear to have implemented this in the main source
tree. (according to google)

Perhaps because too few people are showing a vocal interest?

Do annoy the gcc team with a complaint I'd say. You never know.

In the mean time, good luck with your chosen solution, and
have fun!

sincerely,
Kim Bruning
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top