Code injecting/assembly inlining in Java?

huseyin tugrul buyukisik picture huseyin tugrul buyukisik · Jul 24, 2012 · Viewed 23.8k times · Source

I know Java is a secure language but when matrix calculations are needed, can I try something faster?

I am learning __asm{} in C++, Digital-Mars compiler and FASM. I want to do the same in Java. How can I inline assembly codes in functions? Is this even possible?

Something like this (a vectorized loop to clamp all elements of an array to a value without branching, using AVX support of CPU):

JavaAsmBlock(
   # get pointers into registers somehow
   # and tell Java which registers the asm clobbers somehow
     vbroadcastss  twenty_five(%rip), %ymm0
     xor   %edx,%edx
.Lloop:                            # do {
    vmovups   (%rsi, %rdx, 4), %ymm1
    vcmpltps   %ymm1, %ymm0, %ymm2
    vblendvps  %ymm2, %ymm0, %ymm1, %ymm1  # TODO: use vminps instead
    vmovups    %ymm1, (%rdi, %rdx, 4)
    # TODO: unroll the loop a bit, and maybe handle unaligned output specially if that's common
    add         $32, %rdx
    cmp         %rcx, %rdx
    jb     .Lloop                  # } while(idx < count)
    vzeroupper
);

System.out.println(var[0]);

I don't want to use a code-injector. I want to see the Intel or AT&T style x86 instructions.

Answer

Ernest Friedman-Hill picture Ernest Friedman-Hill · Jul 24, 2012

There is a layer of abstraction between your Java code and the underlying hardware that makes this kind of thing impossible in principle; you technically can't know how your code is represented on the underlying machine, since the same bytecode can run on different processors and different architectures.

What you officially can do is use the Java Native Interface (JNI) to call native code from your Java code. The call overhead is substantial, and sharing data with Java is fairly expensive, so this should be used only for decent-sized chunks of native code.

In theory, such an extension should be possible, though. One can imagine a Java compiler that targeted a specific platform and allowed assembly escapes. The compiler would have to publish its ABI, so you'd know the calling conventions. I'm not aware of any that do, however. But there are several compilers available that compile Java directly to native code; it's possible one of them supports something like this without my knowing, or could be extended to do so.

Finally, on a different level altogether, there are bytecode assemblers for the JVM, like Jasmin. A bytecode assembler lets you write "machine code" that targets the JVM directly, and sometimes you can write better code than the javac compiler can generate. It's fun to play with, in any event.