Begun writing a small JVM with x86/64-only backend powered by the fantastic AsmJit project in C++. This allows me to learn C++ and x86 and probably have SIMD vector intrinsics working before OpenJDK’s Vector API is out…
Today, I got basic class file parsing done and jit’ting of a simple method. So, if you have this class:
public class Main {
public static int main(String[] args) {
return 4;
}
}
(I modified the type of the main method a bit, to be able to directly return an int value via EAX/RAX.)
I will first identify a method with name “main” and descriptor “([Ljava/lang/String;)I” and jit its code (which in this example is iconst_4, ireturn) via AsmJit’s Compiler interface to at least not care about register allocation and calling conventions, which for that code AsmJit generates: 0xB8 0x04 0x00 0x00 0x00 0xC3
(mov of immediate value 4 to eax and ret).
Calling that as an int (*MainMethod)(char const**)
-typed function pointer then gets 4
as int result.
Yay!
Though, I’ll probably soon abort this project again, because, well… there is a TON of stuff to add. An actual standard class library like OpenJDK would be nice and that requires JNI. Then we need GC.
But what I actually want is a truthful JVM and JIT that translates the Java Bytecode predictably 1:1 to x86 code without one wondering whether some arbitrary optimization kicks in because of arbitrary limits and whatnot, because: there won’t be any optimizations to begin with. Just a low-level single-tier JIT.
EDIT: Now, simple arithmetic operations work:
public int f(int arg) {
return arg * 2 + arg;
}
generates:
0: b9 02 00 00 00 mov ecx,0x2
5: 8b c7 mov eax,edi
7: 0f af c1 imul eax,ecx
a: 03 c7 add eax,edi
c: c3 ret
No strength reduction yet, so a*2 won’t be replaced with a<<1.
EDIT2: Btw.: JetBrain’s CLion is an amazing IDE, if you already work with IntelliJ IDEA in Java and on Linux.
EDIT3: Read this http://www.ssw.uni-linz.ac.at/Research/Papers/Stadler14PhD/Thesis_Stadler_14.pdf awesome paper. Going to implement that. Then I’m immediately better than HotSpot when it comes to e.g. scalar replacement.