creating a jit compiler

Programming, for all ages and all languages.
Post Reply
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

creating a jit compiler

Post 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?
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: creating a jit compiler

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
psnix
Member
Member
Posts: 50
Joined: Fri Oct 24, 2008 12:34 pm

Re: creating a jit compiler

Post 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
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: creating a jit compiler

Post 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
Post Reply