translate a language that runs on JVM

J

jrefactors

If we want to write a language translator that translates one language
to another language (Java Source Code or Byte Code) that runs on JVM,
which of the following approaches is better?

1) Non-Java Source Code -> Language Translator -> Java Source Code
2) Non-Java Source Code -> Language Translator -> Java Byte Code
please advise. thanks!!
 
P

Phil Staite

Unless your "Non-Java Source Code" is very close syntactically and
semantically to Java (and if that's the case, why not just write in
Java?) I would go with #2.
 
C

Chris Uppal

If we want to write a language translator that translates one language
to another language (Java Source Code or Byte Code) that runs on JVM,
which of the following approaches is better?

1) Non-Java Source Code -> Language Translator -> Java Source Code
2) Non-Java Source Code -> Language Translator -> Java Byte Code

It depends on number of factors.

a) Are you already familar with Java at a fairly deep technical level ? If not
then either give up the project or resign yourself to the idea that you will
/become/ familar by the time you've finished.

b) Are you already familar with the JVM classfile format, either directly or
via some bytecode library such as BCEL, ASM, or the GNU Bytecode library ? If
not then generating straight Java will be simpler.

c) Are you already familar with the JVM bytecode instruction set ? If not then
generating straight Java will be simpler.

d) Do you have any particular reason to avoid creating files ? If so then
you'll have difficulty if you go via straight Java (you can create and use byte
arrays in class file format on the fly).

e) Do you need to generate code that does not correspond to any legal Java.
I'd be quite surprised you did since there aren't many bytecode sequences that
/don't/ correspond to legal Java[*]. The only examples I can think of are
overlapping, but not nested, exeption handlers; and the possibility (but you'll
have to be careful of the verifiers flow-of-control analysis) to create what in
Java would be illegally overlapping control structures. E.g (totally
meaningless example):
switch (x)
{
while (x > 10)
{
case 20:
y = x;
}
case 30:
y++;
break;
}

f) Do you need to avoid the Java compiler's insistance on checking stuff
statically that will be checked at runtime anyway ? E.g. calling a method for
which no definition exists /yet/.

g) other factors that I've forgotten...;-)

Unless one of (d, e, f) is an important consideration for you, then I'd say
that generating straight Java will be easier to do, and considerably easier to
debug.

BTW (just for completeness since I imagine you've already considered and
rejected the idea) another option would be to write an /interpreter/ in Java
rather than attempting a translation. Or even use a hybrid approach --
interpret most if it but generate classfile/Java for the hotspots. JITs within
JITs...

([*] when I say that most bytecode sequences correspond to legal Java, I mean
that most legal bytecode sequences are identical /in effect/ to some sequence
that would be produced by the Java compiler, not that every possible sequence
can be directly mapped onto Java.)

-- chris
 
C

Chris Uppal

I said:
e) Do you need to generate code that does not correspond to any legal
Java. I'd be quite surprised you did since there aren't many bytecode
sequences that /don't/ correspond to legal Java[*]. The only examples I
can think of are [...]

Or define/call methods that have the same name/signature, but differ in return
type.

-- chris
 
M

Mike Schilling

/.
g) other factors that I've forgotten...;-)

Do you need to debug the resulting programs? There are many good Java
debuggers, and few (if any) good bytecode debuggers.
 
C

Chris Uppal

Mike said:
Do you need to debug the resulting programs? There are many good Java
debuggers, and few (if any) good bytecode debuggers.

Good point.

Though I'd have /expected/ (I've never tried it) that if you set up the
filename, line number tables and temp-vars tables suitably in the generated
classfile, that most debuggers would work OK. (Provided, at least, that they
didn't try to be clever and parse the "java" source as well as displaying it).
This assumes, of course, that the "filename" means anything, and that the
debugger is willing and able to find and display the named file.

If that /does/ work, then it'd be better for end users of the source language
than being obliged to debug through generated Java code.

-- chris
 
M

Mike Schilling

Chris Uppal said:
Good point.

Though I'd have /expected/ (I've never tried it) that if you set up the
filename, line number tables and temp-vars tables suitably in the
generated
classfile, that most debuggers would work OK. (Provided, at least, that
they
didn't try to be clever and parse the "java" source as well as displaying
it).
This assumes, of course, that the "filename" means anything, and that the
debugger is willing and able to find and display the named file.

If that /does/ work, then it'd be better for end users of the source
language
than being obliged to debug through generated Java code.

True, though my guess is that it would work just badly enough to be
irritating as hell :)
 
M

Mike Schilling

Chris Uppal said:
I said:
e) Do you need to generate code that does not correspond to any legal
Java. I'd be quite surprised you did since there aren't many bytecode
sequences that /don't/ correspond to legal Java[*]. The only examples I
can think of are [...]

Or define/call methods that have the same name/signature, but differ in
return
type.

Java does tha t:)

It's how Java implements covariant return types.

Superclass:
Object method()
{
...
}

Subclass
String method()
{
...
}

will generate

Subclass
Object method()
{
return String.method();
}
 
C

Chris Uppal

Mike said:
Java does tha t:)

It's how Java implements covariant return types.

Oh, Gods above! Please don't mention that vile hack. It turns my stomach.

-- chris
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top