C --> MSIL --> java bytecode translation ??

R

robert_dodier

Hello,

I'm working with a C program (about 40 k LOC) and I'm interested
in the possibility of translating it into Java bytecodes, the
goal being to package it as part of a system, the remainder of
which is written in Java.

It seems theoretically possible that the C code could be
compiled into MSIL, and then the MSIL translated into Java
bytecodes. Can someone comment on the feasibility of this?
I've searched the web, but all I've found are some vague
statements (and a lot of flamage). Maybe it's not possible,
I'd just like to get a clear statement one way or the other.

Language wars and "that's stupid, why do you want to do that?"
comments go straight into the bit bucket. Fwiw.

Thanks a lot for any light you can shed on this topic.

Robert Dodier
 
S

Sean Hederman

Hello,

I'm working with a C program (about 40 k LOC) and I'm interested
in the possibility of translating it into Java bytecodes, the
goal being to package it as part of a system, the remainder of
which is written in Java.

It seems theoretically possible that the C code could be
compiled into MSIL, and then the MSIL translated into Java
bytecodes. Can someone comment on the feasibility of this?
I've searched the web, but all I've found are some vague
statements (and a lot of flamage). Maybe it's not possible,
I'd just like to get a clear statement one way or the other.

There is a C compiler that ships with VS.NET, in "C:\Program Files\Microsoft
Visual Studio .NET 2003\SDK\v1.1\Tool Developers Guide\Samples\myc" on my
machine. It's a small subset of C though. It could be very tricky (if not
impossible) to extend it to support ANSI C in .NET, since some major
features of C (e.g. pointer arithmetic) are disallowed by the framework. If
you look at the differences between Managed C++, and C++ you'll see some of
the hoops that need to be jumped through to accomplish C/C++ support. I
think that providing ANSI standard C support will not be possible, but I'm
not 100% sure.

Assuming you can write a C to MSIL compiler, it should theoretically be
possible to create an MSIL->JBC compiler. Of course, in addition to the
straight conversion of MSIL to JBC, you'd also have to rework all method
calls from the .NET libraries to the Java libraries. But then you'd have had
to do something similar when creating your C compiler too. This might be
easier in Whidbey, since I understand that it supports the standard Java
libraries. Then you could make your C compiler accept Java libraries, and
just map them to the .NET Java libraries in your C->MSIL compiler, and then
map then from .NET Java to Java libraries in your MSIL->JBC compiler.

Given that you can support all C language constructs in .NET, it should be
possible to do what you want, but I'd seriously doubt that it's feasible.
Language wars and "that's stupid, why do you want to do that?"
comments go straight into the bit bucket. Fwiw.

But you've piqued my interest. I'd love to know the reason you feel that you
require this kind of solution. Are you trying to provide a sort of generic
..NET/Java compiler using C as the base language?
 
R

robert_dodier

Sean, thanks for your comments. The scenario I'm faced
with is that the C code in question is a library for computing
solutions of constrained optimization problems. But it's just
one step that needs to be carried out in a larger framework,
and the rest of the framework is Java code. I haven't been
able to find any equivalent libraries in Java, and the
library is too big to translate by hand. I looked for a C to
Java translator without finding anything, so that's why I'm
thinking about C to Java bytecode translation -- it seems
like it might be simpler.

I know that in this situation the standard solution is to
recompile the C code for each platform and then call it
through JNI. That's probably OK under the circumstances.

Regards,
Robert Dodier
 
S

Sean Hederman

Yeah, you lose your platform independence though, which is a pain. However,
I think JNI will be a lot easier than trying to bash the square peg of C
into the round hole of a managed execution environment...
 
N

napi

*Hello,

I'm working with a C program (about 40 k LOC) and I'm interested
in the possibility of translating it into Java bytecodes, the
goal being to package it as part of a system, the remainder of
which is written in Java.

It seems theoretically possible that the C code could be
compiled into MSIL, and then the MSIL translated into Java
bytecodes. Can someone comment on the feasibility of this?
I've searched the web, but all I've found are some vague
statements (and a lot of flamage). Maybe it's not possible,
I'd just like to get a clear statement one way or the other.

Language wars and "that's stupid, why do you want to do that?"
comments go straight into the bit bucket. Fwiw.

Thanks a lot for any light you can shed on this topic.

Robert Dodier *

Hi Robert:

You should try MPC (Multi-Platform C)
at http://www.axiomsol.com
It's a C to JVM compiler. You might
be able to achieve what you need to
do with tweaking.

Cheers.

Nap


-
nap
 
R

robert_dodier

Thanks a lot, Napi. I have downloaded MPC and tried it
out on the code that I'm interested in translating.
Unfortunately there are a lot of static variables,
which aren't handled, and a lot of double floats, and
a lot of miscellaneous errors (which seem to mostly
stem from ambiguities about types of things).
So I think MPC is a great project, and I'd like to
encourage you to keep working on it. I'll try again
later if there is another version at some point.

regards,
Robert Dodier
 
S

Serge Baltic

I'm working with a C program (about 40 k LOC) and I'm interested in
the possibility of translating it into Java bytecodes, the goal being
to package it as part of a system, the remainder of which is written
in Java.

If it won't be tied into the system too closely, there is a workaround:
• Make a DLL out of this thing so that all the needed functions are exported.
That's just an ordinary C task.
• Write a C# or any other CLR-aware wrapper that provides a class which exposes
interfaces for the needed functionality and invokes the DLL internally (provided
that C# is able to make direct DLL calls, that's not a problem). From this
point on, the C code is available for convenient use in CLR applications.

These steps I've tried myself over an FFT algorithm written in plain C which
was to be used in a C# application, and that worked perfect.

• The last step involves exposing CLR code to Java. You can generate a Java
package that calls the C# code internally, for example, with the IKVM tool.

Of course, there is a way of doing the second step for Java immediately,
but I know nothing about it because I'm not a Java addict ;) You'd better
ask in a specialized Java echo.
 
N

napi

*Thanks a lot, Napi. I have downloaded MPC and tried it
out on the code that I'm interested in translating.
Unfortunately there are a lot of static variables,
which aren't handled, and a lot of double floats, and
a lot of miscellaneous errors (which seem to mostly
stem from ambiguities about types of things).
So I think MPC is a great project, and I'd like to
encourage you to keep working on it. I'll try again
later if there is another version at some point.

regards,
Robert Dodier *

Robert, is the support for "static"
or "double" more important to you?
Thanks.

Napi
 

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

Top