Combining C and java

Discussion in 'Java' started by John Eskie, Sep 21, 2003.

  1. John Eskie

    John Eskie Guest

    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
    John Eskie, Sep 21, 2003
    #1
    1. Advertising

  2. John Eskie

    kim bruning Guest

    John Eskie <> wrote:
    > 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


    --
    Fingerprint for GPG (www.gnupg.org) key EDDE AA07 :
    BA7F BF9D 8F70 6031 D9E4 14A1 AD0F 180F EDDE AA07
    kim bruning, Sep 22, 2003
    #2
    1. Advertising

  3. John Eskie

    John Eskie Guest

    > 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
    John Eskie, Sep 22, 2003
    #3
  4. John Eskie

    Roedy Green Guest

    On Mon, 22 Sep 2003 20:03:32 +0200, "John Eskie"
    <> wrote or quoted :

    >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.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 22, 2003
    #4
  5. John Eskie

    Chris Uppal Guest

    John Eskie wrote:

    > 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
    Chris Uppal, Sep 23, 2003
    #5
  6. John Eskie

    kim bruning Guest

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

    John Eskie wrote:
    >>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.


    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
    kim bruning, Oct 21, 2003
    #6
  7. John Eskie

    kim bruning Guest

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

    John Eskie wrote:
    >>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.


    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
    kim bruning, Oct 21, 2003
    #7
  8. John Eskie

    Mike Yawn Guest

    kim bruning wrote:
    > My apologies for this late response. I've recently switched ISPs, and
    > things tend to get lost in-between.
    >
    > John Eskie wrote:
    >
    >>> 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.

    >
    >
    > 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
    Mike Yawn, Oct 21, 2003
    #8
  9. John Eskie

    Jezuch Guest

    Mike Yawn wrote:
    > 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."
    --
    Ecce Jezuch
    "I think I'll lie here for a while.
    Then, after that...
    I think I'll lie here for a while." - Garfield
    Jezuch, Oct 21, 2003
    #9
  10. Gordon Beaton, Oct 21, 2003
    #10
  11. John Eskie

    kim bruning Guest

    Jezuch wrote:
    > 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
    kim bruning, Oct 21, 2003
    #11
  12. John Eskie

    Paul Lutus Guest

    Jezuch wrote:

    > Mike Yawn wrote:
    >> 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."


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

    --
    Paul Lutus
    http://www.arachnoid.com
    Paul Lutus, Oct 21, 2003
    #12
  13. Jezuch <> writes:
    > 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
    Thomas Weidenfeller, Oct 22, 2003
    #13
  14. John Eskie

    John Eskie Guest

    > 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
    John Eskie, Oct 22, 2003
    #14
  15. John Eskie

    Jezuch Guest

    Paul Lutus wrote:
    > 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.
    --
    Ecce Jezuch
    "Life feeds on life feeds on life feeds on life feeds on life...
    This is necessary." - M. J. Keenan
    Jezuch, Oct 22, 2003
    #15
  16. John Eskie

    Jezuch Guest

    Thomas Weidenfeller wrote:
    > 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 :)
    --
    Ecce Jezuch
    "Life feeds on life feeds on life feeds on life feeds on life...
    This is necessary." - M. J. Keenan
    Jezuch, Oct 22, 2003
    #16
  17. John Eskie

    kim bruning Guest

    John Eskie wrote:
    >>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.


    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
    kim bruning, Oct 23, 2003
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nils Magnus Englund

    Combining forms and Windows authentication

    Nils Magnus Englund, Apr 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,414
    Nils Magnus Englund
    Apr 19, 2004
  2. Red
    Replies:
    1
    Views:
    700
  3. Michael Herman \(Parallelspace\)
    Replies:
    5
    Views:
    2,993
    Juan T. Llibre
    Jul 5, 2005
  4. Tim Daneliuk
    Replies:
    9
    Views:
    378
    Ravi Teja
    Jun 13, 2006
  5. pek
    Replies:
    2
    Views:
    1,049
Loading...

Share This Page