Page 1 of 1
creating a jit compiler
Posted: Mon Jun 20, 2011 2:00 am
by psnix
hi
how can i create a jit (just-in-time) compiler manually with out using any 3rd party library? (in linux and win32)
i can create byte-code (in x86 asm) form my input script language but how can i run it?
Re: creating a jit compiler
Posted: Mon Jun 20, 2011 2:36 am
by Love4Boobies
I would start by reading a book on compiler theory (and perhaps a few others on computer science in general; you should have easily deduced the general idea even without much knowledge of how things work, programmers don't usually need to be told).
Anyway, the CPU is only capable of executing native code. There are two classic types of compilers: AOT (most compilers you know) and interpreters (they work much like emulators). What you want to do is to have your compiler run at runtime (usually, using self-modifying code), a combination of the two techniques. The advantage of JIT compilers is that they can do certain optimizations such as runtime profiling. Because they can be slow until the translation is complete, you will have to choose your algorithms carefully (they need to be able to perform in real-time) and you'll also probably want to cache the results and use them again later.
Re: creating a jit compiler
Posted: Mon Jun 20, 2011 4:22 am
by psnix
Anyway, the CPU is only capable of executing native code...
i know this and compiler theory but how can i execute a code at run time. for example how can i run the following code:
push ebx
mov ebx,12
...
What I want to do is strip out the VM and execute native machine code on the fly. i read about it (jit compilers) and i found several libraries like as libjit and gnu lightning. but i don't want use this libraries for executing or generating native code
and also i found a thread in stackoverflow.com that says how run this codes in linux but how can i do this linux?
http://stackoverflow.com/questions/5986 ... t-compiler
Re: creating a jit compiler
Posted: Mon Jun 20, 2011 4:38 am
by bluemoon
basically, you allocate a piece of memory, mark it as executable (VirtualProtect on Windows, mmap on Linux)
You then write the generated code into that piece of memory, including any stubs
and call stub_address