Since I started a small project to embedded asm for Intel x86 64bit processors within Java and seamlessly run and compile those methods (and maybe later emulate those calls as fall back), I wonder if there is a Java implementation of a x86 compiler already existing. I want support for those advanced instructions as well.
The Goal is to finally have something like:
public int myJavaMethod(long value) {
doSomeJavaStuff...
int result = 0;
asm(" push eax",
" mov eax, value",
" cmp eax, 0",
" jne notEqual",
" mov result, 1",
" jmp done",
"notEqual: mov result, 0",
"done: pop eax");
doSomeMoreJavaStuff();
return result;
}
What I have:
- The Intel manual for their OPCodes and the Architecture (VOL2)
- I officially can compile and run a asm("nop") program part by now using JNA
Current challenges:
Due to the nature of the task I will try and gain access to the underlying JavaVM stack frames - which is possible (remember you get the esp stack pointer and the base pointer ebp even with the addresses the ret op will place in the eip pointer) but couples the solution with a certain java VM + build target, which I do not have a problem with since one can create various target adapters and poking around in the JavaVM implementations will be fun and interesting.
A problem with the above string based solution would be the handling of parameters and results. It might be necessary to provide a parameter mapping like asm(params(result, value, whatever), "opcodes") and place output variables either directly in the right memory slots or the registers the JIT used to fill the variables in the params call or do something like result = asmResult(result) to read the actual changed value from the data section of the asm call and place it in the correct value.
By using Unsafe on those information one can even access Object and Class variables and navigate those values obtaining the addresses and types of all those references used within the asm code. So using asm("mov eax, super.field") would be possible without interfering with Java - unless the JIT again does some interesting optimization on register caching but might be unlikely since asm(...) is a certain call and caching object variable content might violate the concurrency conditions regarding objects. But yet again thats a question for the JVM developers from Oracle.
So if you have any information about existing Java code or C code that can help me with anything like this, you are welcome.
The best help would be a Java implementation of a x86 compiler that already has ways to translate certain asm constructs in a series of bytes (op codes) so I might go the route of simply create a real ASM method from those above calls and the information gathered by inspecting the methods byte code along with Unsafe and whatever source of information.
In the end I want to just write assembler within my Java code without going at length and compile c/ASM, load ship libraries for different OS and platforms. Does not make sense. Once this works one can easily generate "C" function calls on demand and might even possibly bypass JNA getting rid of another dependency.
So if you know of code and projects or have additional information you are welcome.
[Update]
This question does not aim at JNI / JNA. The goal is simply bypass the use of an alien compiler and load a library. The idea is simply compile into memory directly from within Java and start the method by using a pointer to it along with some additional data for parameter and value processing.
Just think about this workflow write C/ASM, compile using gcc, loadLibrary, call method by JNA into the workflow compile (using Java) into memory, call method by JNA. I already can do this I just want to use more op codes without creating my own compiler front-end. It is not about if this possible but about how to do it lazily.
But once I demonstrated the basic feasibility I guess people will come around adding more target platforms (I only target Linux and i5 + Intel Xeon v3 processors).
[Update 2]
Currently I investigate the NASM compiler as being mentioned to be a good and actively developed compiler using the Intel notation. I asked the developers already how a transparent, simple and fast (no disk access for instance) integration would be possible. If this works creating a similar solution for C using a popular compiler would also be possible.
The idea is edit Java source in eclipse, run program done. The compilation of those snippets in a different language and the invocation and all the other fun should be completely transparent.