Skip to content
gqlxj1987's Blog
Go back

Basic JIT compiler

Edit page

原文链接

So rather than stepping through the operations one by one, my program converts the operations into native machine code and lets the hardware do the work directly

Modern operating systems have page-granularity protections for different parts of process memory: read, write, and execute.

In a running process, the pages holding program code and loaded libraries will have their write bit cleared and execute bit set. Most of the other pages will have their execute bit cleared and their write bit set. 读写部分的安全以及独立性

struct asmbuf *
asmbuf_create(void)
{
    int prot = PROT_READ | PROT_WRITE;
    int flags = MAP_ANONYMOUS | MAP_PRIVATE;
    return mmap(NULL, PAGE_SIZE, prot, flags, -1, 0);
}

在编译器级别来做

switch (operator) {
    case '+':
        asmbuf_ins(buf, 3, 0x4801f8);   // add   rax, rdi
        break;
    case '-':
        asmbuf_ins(buf, 3, 0x4829f8);   // sub   rax, rdi
        break;
    case '*':
        asmbuf_ins(buf, 4, 0x480fafc7); // imul  rax, rdi
        break;
    case '/':
        asmbuf_ins(buf, 3, 0x4831d2);   // xor   rdx, rdx
        asmbuf_ins(buf, 3, 0x48f7ff);   // idiv  rdi
        break;
}

Edit page
Share this post on:

Previous Post
Almighty pause container
Next Post
Using CompletableFuture