Code injecting/assembly inlining in Java?

前端 未结 6 607
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 11:00

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 wan

相关标签:
6条回答
  • 2021-02-03 11:03

    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.

    0 讨论(0)
  • 2021-02-03 11:11

    You cannot directly inline assembly in your Java code. Nevertheless, contrarily to what is claimed by some other answers, conveniently calling assembly without going through any intermediary C (or C++) layer is possible.

    Quick walkthrough

    Consider the following Java class:

    public class MyJNIClass {
    
        public native void printVersion();
    
    }
    

    The main idea is to declare a symbol using the JNI naming convention. In this case, the mangled name to use in your assembly code is Java_MyJNIClass_printVersion. This symbol must be visible from other translation units, which can for instance be achieved using the public directive in FASM or the global directive in NASM. If you're on macOS, prepend an extra underscore to the name.

    Write your assembly code with the calling conventions of the targeted architecture (arguments may be passed in registers, on the stack, in other memory structures, etc.). The first argument passed to your assembly function is a pointer to JNIEnv, which itself is a pointer to the JNI function table. Use it to make calls to JNI functions. For instance, using NASM and targeting x86_64:

    global Java_MyJNIClass_printVersion
    
    section .text
    
    Java_MyJNIClass_printVersion:
        mov rax, [rdi]
        call [rax + 8*4]  ; pointer size in x86_64 * index of GetVersion
        ...
    

    Indexes for JNI functions can be found in the Java documentation. As the JNI function table is basically an array of pointers, don't forget to multiply these indexes by the size of a pointer in the targeted architecture.

    The second argument passed to your assembly function is a reference to the calling Java class or object. All subsequent arguments are the parameters of your native Java method.

    Finally, assemble your code to generate an object file, and then create a shared library from that object file. GCC and Clang can perform this last step with a command similar to gcc/clang -shared -o ....

    Additional resources

    A more comprehensive walkthrough is available in this DZone article. I have also created a fully runnable example on GitHub, feel free to take a look and play around with it to get a better understanding.

    0 讨论(0)
  • 2021-02-03 11:12

    You cannot call assembly directly from Java. But you can call C code via JNI, and from there you can call assembly.

    This article shows how.

    0 讨论(0)
  • 2021-02-03 11:21

    You use JNI or JNA and call your native functions from Java. Or as an alternative, you have bytecode as InputStream and make a Java class out of it.

    0 讨论(0)
  • 2021-02-03 11:23

    It is possible to call assembly from Java using the Machine Level Java technology. It transparently packs your assembly code, written in Java, but very similar to the most used assembly syntax, into a native library. And next you just need to call a native method, that you define in the same class, where your assembly is written. So, you always stay within Java environment and have no need to switch from Java IDE to some assembly tools and then back to Java.

    0 讨论(0)
  • 2021-02-03 11:23

    You may also wish to have a look at Aparapi.

    0 讨论(0)
提交回复
热议问题