Is it possible to integrate the JVM's assembly language into standard
high level java code? I'm thinking along the lines of the __asm
keyword/directive in C/C++ which allows embedded assembly language
Oliver Wong - 24 Oct 2005 18:03 GMT
> Is it possible to integrate the JVM's assembly language into standard
> high level java code? I'm thinking along the lines of the __asm
> keyword/directive in C/C++ which allows embedded assembly language
You'd have to write your own compiler to do this.
But yes, it's possible, in the Turing-Computable sense of the word.
- Oliver
Stefan Ram - 24 Oct 2005 18:15 GMT
>>Is it possible to integrate the JVM's assembly language into
>>standard high level java code?
>You'd have to write your own compiler to do this.
For some special cases, the following approach might work:
Within a normal method use the user-defined class "JVM"
{ int a = 1;
a++;
JVM.aload_0();
JVM.invokespecial( 1 ); }
Then use the normal javac to compile this to bytecode and
transform all "JVM"-calls of the bytecode to their real JVM
counterparts using a bytecode transformer, so that the call of
the method "JVM.aload_0()" will be replaced by a real
"aload_0" instruction.
Possibly Javassist might help in this regard.
http://www.csg.is.titech.ac.jp/~chiba/javassist/tutorial/tutorial.html
Roedy Green - 25 Oct 2005 04:56 GMT
> You'd have to write your own compiler to do this.
And the language would no longer be Java, and you'd have the Sun
lawyers on your back if you tried to call it that.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Skip - 24 Oct 2005 18:10 GMT
> Is it possible to integrate the JVM's assembly language into standard
> high level java code? I'm thinking along the lines of the __asm
> keyword/directive in C/C++ which allows embedded assembly language
No, but you can make a class-file parser, extract the data, manipulate it,
and write it back. I miss this feature too, but in the end the JIT compiler
will generates good native code anyway, using the bytecodes only as a guide.
So in the end it might not get faster, if that was what you were after.
Daniel Sjöblom - 24 Oct 2005 18:15 GMT
> Is it possible to integrate the JVM's assembly language into standard
> high level java code? I'm thinking along the lines of the __asm
> keyword/directive in C/C++ which allows embedded assembly language
Not with any standard tools. It wouldn't be too hard to implement, but
the real question is, why would you want to do that? I can't think of
any practical use of such a feature ;-)
(To explain a bit further: it would not be useful in the sameway inline
assembly is useful in C. Java bytecode is very similar to plain Java
source code, and doesn't lend itself very well to any optimization tricks.)
Daniel Sjöblom
Bent C Dalager - 24 Oct 2005 18:38 GMT
>Is it possible to integrate the JVM's assembly language into standard
>high level java code? I'm thinking along the lines of the __asm
>keyword/directive in C/C++ which allows embedded assembly language
You can presumably do something like this with the BCEL library:
http://jakarta.apache.org/bcel
I've never actually used it, but this seems like the sort of thing
it's meant for.
Cheers
Bent D

Signature
Bent Dalager - bcd@pvv.org - http://www.pvv.org/~bcd
powered by emacs
Ross Bamford - 24 Oct 2005 20:07 GMT
> Is it possible to integrate the JVM's assembly language into standard
> high level java code? I'm thinking along the lines of the __asm
> keyword/directive in C/C++ which allows embedded assembly language
Well, Jen (http://jen.dev.java.net/) provides a really convenient
interface for working with bytecode, but I don't think it's going to do
exactly what you want to do. I assume you're thinking something like:
public int someMethod() {
asm {
aload_0
invokestatic com/mycompany/SomeClass someMethod()V
ireturn
}
}
In which case it's a case of a custom compiler. As someone else said,
though, it's not something that's especially useful in itself since the
language maps quite well to the bytecode. Also, 'optimization' is a bit of
a tricky subject - http://wiki.java.net/bin/view/Javatools/JenHotWarning
gives a little bit of background on this,
A few other people have mentioned Javassist and BCEL, which would both
allow you to do much the same thing as Jen, namely allow you to implement
methods on classes by generating the bytecode yourself. If doing this
you'd probably find Jen the easiest to use and most flexible, and it does
give some utility classes to achieve convenient generation (for example
without worrying about constant pool index and boxing/unboxing). You can
see a few examples at
http://wiki.java.net/bin/view/Javatools/JenBytecodeGeneration of using
Java code for custom generation (albeit with a separate run for the
generation).
Whether you did this as a separate compile step, at load time, or runtime
depends very much on what you're doing, but in my opinion the most useful
place for such a tool would be at compile time. It's still not the same as
mixing 'assembler' and Java code in the source though. Another option
would be to use a 'true' Java Assembler such as Jasmin
(http://jasmin.sourceforge.net/) and again have a separate compile task.
I've not used Jasmin myself for anything beyond play, however.

Signature
Ross Bamford - rosco@roscopeco.remove.co.uk
Roedy Green - 25 Oct 2005 04:55 GMT
>Is it possible to integrate the JVM's assembly language into standard
>high level java code? I'm thinking along the lines of the __asm
>keyword/directive in C/C++ which allows embedded assembly language
No. About the closest you could come is creating a class file of JVM
byte codes on the fly. Java is strictly multiplatform. That leaves
out any assembler except in the native methods which in theory could
be written in pure assembler.
see http://mindprod.com/jgloss/jasm.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.