summaryrefslogtreecommitdiff
path: root/ujit_compile.c
blob: 19b2b35c12c0766175a324da88a54107d62d5350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <assert.h>
#include "insns.inc"
#include "internal.h"
#include "vm_core.h"
#include "vm_callinfo.h"
#include "builtin.h"
#include "insns_info.inc"
#include "ujit_compile.h"
#include "ujit_asm.h"

// TODO: give ujit_examples.h some more meaningful file name
#include "ujit_examples.h"

static codeblock_t block;
static codeblock_t* cb = NULL;

// Hash table of encoded instructions
extern st_table *rb_encoded_insn_data;

static void ujit_init();

// Ruby instruction entry
static void
ujit_instr_entry(codeblock_t* cb)
{
    for (size_t i = 0; i < sizeof(ujit_pre_call_bytes); ++i)
        cb_write_byte(cb, ujit_pre_call_bytes[i]);
}

// Ruby instruction exit
static void
ujit_instr_exit(codeblock_t* cb)
{
    for (size_t i = 0; i < sizeof(ujit_post_call_bytes); ++i)
        cb_write_byte(cb, ujit_post_call_bytes[i]);
}

// Keep track of mapping from instructions to generated code
// See comment for rb_encoded_insn_data in iseq.c
static void
addr2insn_bookkeeping(void *code_ptr, int insn)
{
    const void * const *table = rb_vm_get_insns_address_table();
    const void * const translated_address = table[insn];
    st_data_t encoded_insn_data;
    if (st_lookup(rb_encoded_insn_data, (st_data_t)translated_address, &encoded_insn_data)) {
        st_insert(rb_encoded_insn_data, (st_data_t)code_ptr, encoded_insn_data);
    }
    else {
        rb_bug("ujit: failed to find info for original instruction while dealing with addr2insn");
    }
}

// Generate a chunk of machinecode for one individual bytecode instruction
// Eventually, this will handle multiple instructions in a sequence
uint8_t *
ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
{
    // If not previously done, initialize ujit
    if (!cb)
    {
        ujit_init();
    }

    int insn = (int)iseq->body->iseq_encoded[insn_idx];
	int len = insn_len(insn);
    //const char* name = insn_name(insn);
    //printf("%s\n", name);

    // Compute the address of the next instruction
    void* next_pc = &iseq->body->iseq_encoded[insn_idx + len];

    // Get a pointer to the current write position in the code block
    uint8_t* code_ptr = &cb->mem_block[cb->write_pos];
    //printf("write pos: %ld\n", cb->write_pos);

    // TODO: encode individual instructions, eg
    // nop, putnil, putobject, putself, pop, dup, getlocal, setlocal, nilp

    // TODO: we should move the codegen for individual instructions
    // into separate functions
    if (insn == BIN(nop))
    {
        // Write the pre call bytes
        ujit_instr_entry(cb);

        //add(cb, RSI, imm_opnd(8));                  // increment PC
        //mov(cb, mem_opnd(64, RDI, 0), RSI);         // write new PC to EC object, not necessary for nop bytecode?
        //mov(cb, RAX, RSI);                          // return new PC

        // Directly return the next PC, which is a constant
        mov(cb, RAX, const_ptr_opnd(next_pc));

        // Write the post call bytes
        ujit_instr_exit(cb);

        addr2insn_bookkeeping(code_ptr, insn);

        return code_ptr;
    }

    if (insn == BIN(pop))
    {
        // Write the pre call bytes
        ujit_instr_entry(cb);

        sub(cb, mem_opnd(64, RDI, 8), imm_opnd(8)); // decrement SP

        // Directly return the next PC, which is a constant
        mov(cb, RAX, const_ptr_opnd(next_pc));

        // Write the post call bytes
        ujit_instr_exit(cb);

        addr2insn_bookkeeping(code_ptr, insn);

        return code_ptr;
    }

    return 0;
}

static void ujit_init()
{
    // 4MB ought to be enough for anybody
    cb = &block;
    cb_init(cb, 4000000);
}