1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Refactor MicroJIT codegen to use hash table of codegen functions

This commit is contained in:
Maxime Chevalier-Boisvert 2020-09-15 17:04:33 -04:00 committed by Alan Wu
parent 2811792f91
commit b0b7d0a87e

View file

@ -11,6 +11,20 @@
// TODO: give ujit_examples.h some more meaningful file name // TODO: give ujit_examples.h some more meaningful file name
#include "ujit_examples.h" #include "ujit_examples.h"
// Code generation context
typedef struct ctx_struct
{
// TODO: virtual stack pointer handling
} ctx_t;
// Code generation function
typedef void (*codegen_fn)(codeblock_t* cb, ctx_t* ctx);
// Map from YARV opcodes to code generation functions
static st_table *gen_fns;
// Code block into which we write machine code
static codeblock_t block; static codeblock_t block;
static codeblock_t* cb = NULL; static codeblock_t* cb = NULL;
@ -76,6 +90,15 @@ ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
//const char* name = insn_name(insn); //const char* name = insn_name(insn);
//printf("%s\n", name); //printf("%s\n", name);
// Lookup the codegen function for this instruction
st_data_t st_gen_fn;
int found = rb_st_lookup(gen_fns, insn, &st_gen_fn);
if (!found)
return 0;
codegen_fn gen_fn = (codegen_fn)st_gen_fn;
// Compute the address of the next instruction // Compute the address of the next instruction
void *next_pc = &iseq->body->iseq_encoded[insn_idx + len]; void *next_pc = &iseq->body->iseq_encoded[insn_idx + len];
@ -86,44 +109,28 @@ ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
// Write the pre call bytes // Write the pre call bytes
ujit_instr_entry(cb); ujit_instr_entry(cb);
// TODO: encode individual instructions, eg // TODO: create codegen context
// nop, putnil, putobject, putself, pop, dup, getlocal, setlocal, nilp
// TODO: we should move the codegen for individual instructions // Call the code generation function
// into separate functions gen_fn(cb, NULL);
if (insn == BIN(nop))
{
//add(cb, RSI, imm_opnd(8)); // increment PC
//mov(cb, mem_opnd(64, RDI, 0), RSI); // write new PC to CFP object, not necessary for nop bytecode?
//mov(cb, RAX, RSI); // return new PC
// Directly return the next PC, which is a constant // Directly return the next PC, which is a constant
mov(cb, RAX, const_ptr_opnd(next_pc)); mov(cb, RAX, const_ptr_opnd(next_pc));
// Write the post call bytes // Write the post call bytes
ujit_instr_exit(cb); ujit_instr_exit(cb);
addr2insn_bookkeeping(code_ptr, insn); addr2insn_bookkeeping(code_ptr, insn);
return code_ptr; return code_ptr;
}
if (insn == BIN(pop))
{
// Decrement SP
sub(cb, mem_opnd(64, RDI, 8), imm_opnd(8));
// 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(putobject_INT2FIX_0_) || insn == BIN(putobject_INT2FIX_1_)) if (insn == BIN(putobject_INT2FIX_0_) || insn == BIN(putobject_INT2FIX_1_))
{ {
// Load current SP into RAX // Load current SP into RAX
@ -149,6 +156,7 @@ ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
return code_ptr; return code_ptr;
} }
*/
// TODO: implement putself // TODO: implement putself
/* /*
@ -164,6 +172,7 @@ ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
} }
*/ */
/*
if (insn == BIN(getlocal_WC_0)) if (insn == BIN(getlocal_WC_0))
{ {
//printf("compiling getlocal_WC_0\n"); //printf("compiling getlocal_WC_0\n");
@ -199,8 +208,17 @@ ujit_compile_insn(rb_iseq_t *iseq, size_t insn_idx)
addr2insn_bookkeeping(code_ptr, insn); addr2insn_bookkeeping(code_ptr, insn);
} }
*/
}
return 0; void gen_nop(codeblock_t* cb, ctx_t* ctx)
{
}
void gen_pop(codeblock_t* cb, ctx_t* ctx)
{
// Decrement SP
sub(cb, mem_opnd(64, RDI, 8), imm_opnd(8));
} }
static void ujit_init() static void ujit_init()
@ -208,4 +226,11 @@ static void ujit_init()
// 4MB ought to be enough for anybody // 4MB ought to be enough for anybody
cb = █ cb = █
cb_init(cb, 4000000); cb_init(cb, 4000000);
// Initialize the codegen function table
gen_fns = rb_st_init_numtable();
// Map YARV opcodes to the corresponding codegen functions
st_insert(gen_fns, (st_data_t)BIN(nop), (st_data_t)&gen_nop);
st_insert(gen_fns, (st_data_t)BIN(pop), (st_data_t)&gen_pop);
} }