Chr*_*son 32 c++ linux compiler-construction x86 jit
我想为我编写的玩具语言处理器编写一个非常小的概念验证JIT编译器(纯粹是学术性的),但我在设计的中间高度上遇到了一些麻烦.从概念上讲,我熟悉JIT的工作原理 - 您将字节码编译成(机器或汇编?)代码来运行.然而,在螺栓和螺栓水平上,我并不是在想你实际上是怎么做的.
我的(非常"新手")下意识的反应,因为我没有第一个线索从哪里开始,将尝试类似以下内容:
这甚至接近正确的算法吗?我已经尝试过阅读我知道有JIT编译器学习的不同项目(比如V8),但是由于它们的大小,这些代码库很难被消费,而我几乎不知道从哪里开始寻找.
She*_*ien 28
不确定linux,但这适用于x86/windows.
更新:http://codepad.org/sQoF6kR8
#include <stdio.h>
#include <windows.h>
typedef unsigned char byte;
int arg1;
int arg2;
int res1;
typedef void (*pfunc)(void);
union funcptr {
pfunc x;
byte* y;
};
int main( void ) {
byte* buf = (byte*)VirtualAllocEx( GetCurrentProcess(), 0, 1<<16, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
if( buf==0 ) return 0;
byte* p = buf;
*p++ = 0x50; // push eax
*p++ = 0x52; // push edx
*p++ = 0xA1; // mov eax, [arg2]
(int*&)p[0] = &arg2; p+=sizeof(int*);
*p++ = 0x92; // xchg edx,eax
*p++ = 0xA1; // mov eax, [arg1]
(int*&)p[0] = &arg1; p+=sizeof(int*);
*p++ = 0xF7; *p++ = 0xEA; // imul edx
*p++ = 0xA3; // mov [res1],eax
(int*&)p[0] = &res1; p+=sizeof(int*);
*p++ = 0x5A; // pop edx
*p++ = 0x58; // pop eax
*p++ = 0xC3; // ret
funcptr func;
func.y = buf;
arg1 = 123; arg2 = 321; res1 = 0;
func.x(); // call generated code
printf( "arg1=%i arg2=%i arg1*arg2=%i func(arg1,arg2)=%i\n", arg1,arg2,arg1*arg2,res1 );
}
Run Code Online (Sandbox Code Playgroud)
您可能想看看libjit,它提供了您正在寻找的基础设施:
libjit 库实现了即时编译功能。与其他 JIT 不同,该 JIT 被设计为独立于任何特定的虚拟机字节码格式或语言。
http://freshmeat.net/projects/libjit
| 归档时间: |
|
| 查看次数: |
9871 次 |
| 最近记录: |