2020-09-10 17:20:46 -04:00
|
|
|
#include <assert.h>
|
|
|
|
#include "insns.inc"
|
|
|
|
#include "internal.h"
|
|
|
|
#include "vm_core.h"
|
2020-10-15 09:06:42 -04:00
|
|
|
#include "vm_sync.h"
|
2020-09-10 17:20:46 -04:00
|
|
|
#include "vm_callinfo.h"
|
|
|
|
#include "builtin.h"
|
2020-10-13 11:15:22 -04:00
|
|
|
#include "internal/compile.h"
|
2020-11-10 15:35:55 -05:00
|
|
|
#include "internal/class.h"
|
2020-09-10 17:20:46 -04:00
|
|
|
#include "insns_info.inc"
|
|
|
|
#include "ujit_compile.h"
|
|
|
|
#include "ujit_asm.h"
|
2020-09-17 17:09:42 -04:00
|
|
|
#include "ujit_utils.h"
|
2020-09-10 17:20:46 -04:00
|
|
|
|
2020-09-23 03:02:01 -04:00
|
|
|
// TODO: give ujit_examples.inc some more meaningful file name
|
2020-09-17 17:09:42 -04:00
|
|
|
// eg ujit_hook.h
|
2020-09-23 03:02:01 -04:00
|
|
|
#include "ujit_examples.inc"
|
2020-09-14 13:31:34 -04:00
|
|
|
|
2020-10-05 07:41:46 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define PLATFORM_SUPPORTED_P 0
|
|
|
|
#else
|
|
|
|
#define PLATFORM_SUPPORTED_P 1
|
|
|
|
#endif
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
#ifndef UJIT_CHECK_MODE
|
|
|
|
#define UJIT_CHECK_MODE 0
|
|
|
|
#endif
|
|
|
|
|
2020-10-29 17:26:49 -04:00
|
|
|
// >= 1: print when output code invalidation happens
|
|
|
|
// >= 2: dump list of instructions when regions compile
|
2020-10-27 18:49:17 -04:00
|
|
|
#ifndef UJIT_DUMP_MODE
|
|
|
|
#define UJIT_DUMP_MODE 0
|
|
|
|
#endif
|
|
|
|
|
2020-10-13 11:15:22 -04:00
|
|
|
bool rb_ujit_enabled;
|
|
|
|
|
2020-09-24 15:57:53 -04:00
|
|
|
// Hash table of encoded instructions
|
|
|
|
extern st_table *rb_encoded_insn_data;
|
|
|
|
|
2020-09-15 17:04:33 -04:00
|
|
|
// Code generation context
|
|
|
|
typedef struct ctx_struct
|
|
|
|
{
|
2020-09-16 10:33:34 -04:00
|
|
|
// Current PC
|
2020-10-13 11:15:22 -04:00
|
|
|
VALUE *pc;
|
2020-09-16 10:33:34 -04:00
|
|
|
|
2020-09-17 13:40:50 -04:00
|
|
|
// Difference between the current stack pointer and actual stack top
|
|
|
|
int32_t stack_diff;
|
2020-09-15 17:04:33 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// The iseq that owns the region that is compiling
|
2020-10-13 11:15:22 -04:00
|
|
|
const rb_iseq_t *iseq;
|
2020-11-09 16:38:50 -05:00
|
|
|
|
|
|
|
// Index in the iseq of the opcode we are replacing
|
|
|
|
size_t start_idx;
|
|
|
|
|
|
|
|
// The start of the generated code
|
|
|
|
uint8_t *code_ptr;
|
2020-10-13 11:15:22 -04:00
|
|
|
|
2020-09-15 17:04:33 -04:00
|
|
|
} ctx_t;
|
|
|
|
|
2020-09-24 15:57:53 -04:00
|
|
|
// MicroJIT code generation function signature
|
2020-10-15 11:46:37 -04:00
|
|
|
typedef bool (*codegen_fn)(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx);
|
2020-09-15 17:04:33 -04:00
|
|
|
|
|
|
|
// Map from YARV opcodes to code generation functions
|
|
|
|
static st_table *gen_fns;
|
|
|
|
|
|
|
|
// Code block into which we write machine code
|
2020-09-10 17:20:46 -04:00
|
|
|
static codeblock_t block;
|
|
|
|
static codeblock_t* cb = NULL;
|
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Code block into which we write out-of-line machine code
|
|
|
|
static codeblock_t outline_block;
|
|
|
|
static codeblock_t* ocb = NULL;
|
2020-09-14 13:31:34 -04:00
|
|
|
|
2020-10-20 11:11:47 -04:00
|
|
|
// Register MicroJIT receives the CFP and EC into
|
|
|
|
#define REG_CFP RDI
|
|
|
|
#define REG_EC RSI
|
|
|
|
|
|
|
|
// Register MicroJIT loads the SP into
|
|
|
|
#define REG_SP RDX
|
|
|
|
|
|
|
|
// Scratch registers used by MicroJIT
|
2020-10-20 15:03:06 -04:00
|
|
|
#define REG0 RAX
|
|
|
|
#define REG1 RCX
|
2020-10-27 11:10:19 -04:00
|
|
|
#define REG0_32 EAX
|
|
|
|
#define REG1_32 ECX
|
2020-10-20 11:11:47 -04:00
|
|
|
|
2020-09-14 13:31:34 -04:00
|
|
|
// Keep track of mapping from instructions to generated code
|
2020-09-11 15:36:40 -04:00
|
|
|
// See comment for rb_encoded_insn_data in iseq.c
|
2020-09-11 14:58:32 -04:00
|
|
|
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)) {
|
2020-09-22 18:25:29 -04:00
|
|
|
st_insert(rb_encoded_insn_data, (st_data_t)code_ptr, encoded_insn_data);
|
2020-09-11 14:58:32 -04:00
|
|
|
}
|
|
|
|
else {
|
2020-09-11 15:36:40 -04:00
|
|
|
rb_bug("ujit: failed to find info for original instruction while dealing with addr2insn");
|
2020-09-11 14:58:32 -04:00
|
|
|
}
|
|
|
|
}
|
2020-09-10 17:20:46 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// GC root for interacting with the GC
|
|
|
|
struct ujit_root_struct {};
|
|
|
|
|
|
|
|
// Map cme_or_cc => [[iseq, offset]]. An entry in the map means compiled code at iseq[offset]
|
|
|
|
// is only valid when cme_or_cc is valid
|
|
|
|
static st_table *method_lookup_dependency;
|
|
|
|
|
|
|
|
struct compiled_region_array {
|
|
|
|
int32_t size;
|
|
|
|
int32_t capa;
|
|
|
|
struct compiled_region {
|
|
|
|
const rb_iseq_t *iseq;
|
2020-11-09 16:38:50 -05:00
|
|
|
size_t start_idx;
|
2020-10-27 18:49:17 -04:00
|
|
|
uint8_t *code;
|
2020-11-09 16:54:29 -05:00
|
|
|
} data[];
|
2020-10-27 18:49:17 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add an element to a region array, or allocate a new region array.
|
|
|
|
static struct compiled_region_array *
|
2020-11-09 16:38:50 -05:00
|
|
|
add_compiled_region(struct compiled_region_array *array, const rb_iseq_t *iseq, size_t start_idx, uint8_t *code)
|
2020-10-27 18:49:17 -04:00
|
|
|
{
|
|
|
|
if (!array) {
|
|
|
|
// Allocate a brand new array with space for one
|
|
|
|
array = malloc(sizeof(*array) + sizeof(struct compiled_region));
|
|
|
|
if (!array) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array->size = 0;
|
|
|
|
array->capa = 1;
|
|
|
|
}
|
|
|
|
if (array->size == INT32_MAX) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
// Check if the region is already present
|
|
|
|
for (int32_t i = 0; i < array->size; i++) {
|
2020-11-09 16:38:50 -05:00
|
|
|
if (array->data[i].iseq == iseq && array->data[i].start_idx == start_idx) {
|
2020-10-27 18:49:17 -04:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (array->size + 1 > array->capa) {
|
|
|
|
// Double the array's capacity.
|
|
|
|
int64_t double_capa = ((int64_t)array->capa) * 2;
|
|
|
|
int32_t new_capa = (int32_t)double_capa;
|
|
|
|
if (new_capa != double_capa) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array = realloc(array, sizeof(*array) + new_capa * sizeof(struct compiled_region));
|
|
|
|
if (array == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
array->capa = new_capa;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t size = array->size;
|
|
|
|
array->data[size].iseq = iseq;
|
2020-11-09 16:38:50 -05:00
|
|
|
array->data[size].start_idx = start_idx;
|
2020-10-27 18:49:17 -04:00
|
|
|
array->data[size].code = code;
|
|
|
|
array->size++;
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
add_lookup_dependency_i(st_data_t *key, st_data_t *value, st_data_t data, int existing)
|
|
|
|
{
|
|
|
|
ctx_t *ctx = (ctx_t *)data;
|
|
|
|
struct compiled_region_array *regions = NULL;
|
|
|
|
if (existing) {
|
|
|
|
regions = (struct compiled_region_array *)*value;
|
|
|
|
}
|
2020-11-09 16:38:50 -05:00
|
|
|
regions = add_compiled_region(regions, ctx->iseq, ctx->start_idx, ctx->code_ptr);
|
2020-10-27 18:49:17 -04:00
|
|
|
if (!regions) {
|
|
|
|
rb_bug("ujit: failed to add method lookup dependency"); // TODO: we could bail out of compiling instead
|
|
|
|
}
|
|
|
|
*value = (st_data_t)regions;
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store info to remember that the currently compiling region is only valid while cme and cc and valid.
|
|
|
|
static void
|
|
|
|
ujit_assume_method_lookup_stable(const struct rb_callcache *cc, const rb_callable_method_entry_t *cme, ctx_t *ctx)
|
|
|
|
{
|
|
|
|
st_update(method_lookup_dependency, (st_data_t)cme, add_lookup_dependency_i, (st_data_t)ctx);
|
|
|
|
st_update(method_lookup_dependency, (st_data_t)cc, add_lookup_dependency_i, (st_data_t)ctx);
|
|
|
|
// FIXME: This is a leak! When either the cme or the cc become invalid, the other also needs to go
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ujit_root_mark_i(st_data_t k, st_data_t v, st_data_t ignore)
|
|
|
|
{
|
|
|
|
// FIXME: This leaks everything that end up in the dependency table!
|
|
|
|
// One way to deal with this is with weak references...
|
|
|
|
rb_gc_mark((VALUE)k);
|
|
|
|
struct compiled_region_array *regions = (void *)v;
|
|
|
|
for (int32_t i = 0; i < regions->size; i++) {
|
|
|
|
rb_gc_mark((VALUE)regions->data[i].iseq);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ST_CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC callback during mark phase
|
|
|
|
static void
|
|
|
|
ujit_root_mark(void *ptr)
|
|
|
|
{
|
|
|
|
if (method_lookup_dependency) {
|
|
|
|
st_foreach(method_lookup_dependency, ujit_root_mark_i, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ujit_root_free(void *ptr)
|
|
|
|
{
|
|
|
|
// Do nothing. The root lives as long as the process.
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
ujit_root_memsize(const void *ptr)
|
|
|
|
{
|
|
|
|
// Count off-gc-heap allocation size of the dependency table
|
|
|
|
return st_memsize(method_lookup_dependency); // TODO: more accurate accounting
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom type for interacting with the GC
|
|
|
|
// TODO: compaction support
|
|
|
|
// TODO: make this write barrier protected
|
|
|
|
static const rb_data_type_t ujit_root_type = {
|
|
|
|
"ujit_root",
|
|
|
|
{ujit_root_mark, ujit_root_free, ujit_root_memsize, },
|
|
|
|
0, 0, RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
};
|
|
|
|
|
2020-10-13 11:15:22 -04:00
|
|
|
static int
|
|
|
|
opcode_at_pc(const rb_iseq_t *iseq, const VALUE *pc)
|
|
|
|
{
|
|
|
|
const VALUE at_pc = *pc;
|
|
|
|
if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
|
2020-10-19 09:47:39 -04:00
|
|
|
return rb_vm_insn_addr2opcode((const void *)at_pc);
|
2020-10-13 11:15:22 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return (int)at_pc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// Get the current instruction opcode from the context object
|
2020-10-29 17:40:58 -04:00
|
|
|
static int
|
|
|
|
ctx_get_opcode(ctx_t *ctx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
2020-10-13 11:15:22 -04:00
|
|
|
return opcode_at_pc(ctx->iseq, ctx->pc);
|
2020-09-16 10:33:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get an instruction argument from the context object
|
2020-10-29 17:40:58 -04:00
|
|
|
static VALUE
|
|
|
|
ctx_get_arg(ctx_t* ctx, size_t arg_idx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
|
|
|
assert (arg_idx + 1 < insn_len(ctx_get_opcode(ctx)));
|
|
|
|
return *(ctx->pc + arg_idx + 1);
|
|
|
|
}
|
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
/*
|
|
|
|
Get an operand for the adjusted stack pointer address
|
|
|
|
*/
|
2020-10-29 17:40:58 -04:00
|
|
|
static x86opnd_t
|
|
|
|
ctx_sp_opnd(ctx_t* ctx, int32_t offset_bytes)
|
2020-09-28 15:50:41 -04:00
|
|
|
{
|
2020-10-27 14:25:00 -04:00
|
|
|
int32_t offset = (ctx->stack_diff) * 8 + offset_bytes;
|
2020-10-20 11:11:47 -04:00
|
|
|
return mem_opnd(64, REG_SP, offset);
|
2020-09-28 15:50:41 -04:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:40:50 -04:00
|
|
|
/*
|
|
|
|
Make space on the stack for N values
|
|
|
|
Return a pointer to the new stack top
|
|
|
|
*/
|
2020-10-29 17:40:58 -04:00
|
|
|
static x86opnd_t
|
|
|
|
ctx_stack_push(ctx_t* ctx, size_t n)
|
2020-09-17 13:40:50 -04:00
|
|
|
{
|
|
|
|
ctx->stack_diff += n;
|
|
|
|
|
|
|
|
// SP points just above the topmost value
|
|
|
|
int32_t offset = (ctx->stack_diff - 1) * 8;
|
2020-10-20 11:11:47 -04:00
|
|
|
return mem_opnd(64, REG_SP, offset);
|
2020-09-17 13:40:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pop N values off the stack
|
|
|
|
Return a pointer to the stack top before the pop operation
|
|
|
|
*/
|
2020-10-29 17:40:58 -04:00
|
|
|
static x86opnd_t
|
|
|
|
ctx_stack_pop(ctx_t* ctx, size_t n)
|
2020-09-17 13:40:50 -04:00
|
|
|
{
|
|
|
|
// SP points just above the topmost value
|
|
|
|
int32_t offset = (ctx->stack_diff - 1) * 8;
|
2020-10-20 11:11:47 -04:00
|
|
|
x86opnd_t top = mem_opnd(64, REG_SP, offset);
|
2020-09-18 12:40:05 -04:00
|
|
|
|
|
|
|
ctx->stack_diff -= n;
|
|
|
|
|
|
|
|
return top;
|
2020-09-17 13:40:50 -04:00
|
|
|
}
|
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static x86opnd_t
|
|
|
|
ctx_stack_opnd(ctx_t* ctx, int32_t idx)
|
2020-09-30 15:17:13 -04:00
|
|
|
{
|
|
|
|
// SP points just above the topmost value
|
|
|
|
int32_t offset = (ctx->stack_diff - 1 - idx) * 8;
|
2020-10-20 11:11:47 -04:00
|
|
|
x86opnd_t opnd = mem_opnd(64, REG_SP, offset);
|
2020-09-30 15:17:13 -04:00
|
|
|
|
|
|
|
return opnd;
|
|
|
|
}
|
2020-09-28 15:50:41 -04:00
|
|
|
|
|
|
|
// Ruby instruction entry
|
|
|
|
static void
|
|
|
|
ujit_gen_entry(codeblock_t* cb)
|
|
|
|
{
|
2020-10-29 17:37:40 -04:00
|
|
|
for (size_t i = 0; i < sizeof(ujit_with_ec_pre_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, ujit_with_ec_pre_call_bytes[i]);
|
2020-09-28 15:50:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate an inline exit to return to the interpreter
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ujit_gen_exit(codeblock_t* cb, ctx_t* ctx, VALUE* exit_pc)
|
|
|
|
{
|
|
|
|
// Write the adjusted SP back into the CFP
|
|
|
|
if (ctx->stack_diff != 0)
|
|
|
|
{
|
2020-10-27 14:25:00 -04:00
|
|
|
x86opnd_t stack_pointer = ctx_sp_opnd(ctx, 0);
|
2020-10-20 11:11:47 -04:00
|
|
|
lea(cb, REG_SP, stack_pointer);
|
|
|
|
mov(cb, member_opnd(REG_CFP, rb_control_frame_t, sp), REG_SP);
|
2020-09-28 15:50:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Directly return the next PC, which is a constant
|
|
|
|
mov(cb, RAX, const_ptr_opnd(exit_pc));
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, member_opnd(REG_CFP, rb_control_frame_t, pc), RAX);
|
2020-09-28 15:50:41 -04:00
|
|
|
|
|
|
|
// Write the post call bytes
|
2020-10-29 17:37:40 -04:00
|
|
|
for (size_t i = 0; i < sizeof(ujit_with_ec_post_call_bytes); ++i)
|
|
|
|
cb_write_byte(cb, ujit_with_ec_post_call_bytes[i]);
|
2020-09-28 15:50:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Generate an out-of-line exit to return to the interpreter
|
|
|
|
*/
|
2020-10-29 17:40:58 -04:00
|
|
|
static uint8_t *
|
2020-09-28 15:50:41 -04:00
|
|
|
ujit_side_exit(codeblock_t* cb, ctx_t* ctx, VALUE* exit_pc)
|
|
|
|
{
|
|
|
|
uint8_t* code_ptr = cb_get_ptr(cb, cb->write_pos);
|
|
|
|
|
2020-11-09 16:54:29 -05:00
|
|
|
// Table mapping opcodes to interpreter handlers
|
|
|
|
const void * const *table = rb_vm_get_insns_address_table();
|
|
|
|
|
|
|
|
// Write back the old instruction at the entry PC
|
|
|
|
// To deotimize the code block this instruction belongs to
|
|
|
|
VALUE* entry_pc = &ctx->iseq->body->iseq_encoded[ctx->start_idx];
|
|
|
|
int entry_opcode = opcode_at_pc(ctx->iseq, entry_pc);
|
|
|
|
void* entry_instr = (void*)table[entry_opcode];
|
|
|
|
mov(cb, RAX, const_ptr_opnd(entry_pc));
|
|
|
|
mov(cb, RCX, const_ptr_opnd(entry_instr));
|
|
|
|
mov(cb, mem_opnd(64, RAX, 0), RCX);
|
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Write back the old instruction at the exit PC
|
|
|
|
// Otherwise the interpreter may jump right back to the
|
|
|
|
// JITted code we're trying to exit
|
2020-11-09 16:54:29 -05:00
|
|
|
int exit_opcode = opcode_at_pc(ctx->iseq, exit_pc);
|
|
|
|
void* exit_instr = (void*)table[exit_opcode];
|
2020-09-28 15:50:41 -04:00
|
|
|
mov(cb, RAX, const_ptr_opnd(exit_pc));
|
2020-11-09 16:54:29 -05:00
|
|
|
mov(cb, RCX, const_ptr_opnd(exit_instr));
|
2020-09-28 15:50:41 -04:00
|
|
|
mov(cb, mem_opnd(64, RAX, 0), RCX);
|
|
|
|
|
|
|
|
// Generate the code to exit to the interpreters
|
|
|
|
ujit_gen_exit(cb, ctx, exit_pc);
|
|
|
|
|
|
|
|
return code_ptr;
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
/*
|
2020-10-29 14:45:53 -04:00
|
|
|
Compile a sequence of bytecode instructions starting at `insn_idx`.
|
|
|
|
Return the index to the first instruction not compiled in the sequence
|
|
|
|
through `next_ujit_idx`. Return `NULL` in case compilation fails.
|
2020-09-16 10:33:34 -04:00
|
|
|
*/
|
2020-10-29 17:26:49 -04:00
|
|
|
static uint8_t *
|
2020-10-29 14:45:53 -04:00
|
|
|
ujit_compile_insn(const rb_iseq_t *iseq, unsigned int insn_idx, unsigned int *next_ujit_idx)
|
2020-09-10 17:20:46 -04:00
|
|
|
{
|
2020-10-20 11:11:47 -04:00
|
|
|
assert (cb != NULL);
|
2020-10-29 17:26:49 -04:00
|
|
|
unsigned first_insn_idx = insn_idx;
|
2020-10-13 11:15:22 -04:00
|
|
|
VALUE *encoded = iseq->body->iseq_encoded;
|
2020-09-10 17:20:46 -04:00
|
|
|
|
2020-09-17 11:49:53 -04:00
|
|
|
// NOTE: if we are ever deployed in production, we
|
|
|
|
// should probably just log an error and return NULL here,
|
|
|
|
// so we can fail more gracefully
|
2020-10-29 17:26:49 -04:00
|
|
|
if (cb->write_pos + 1024 >= cb->mem_size) {
|
2020-09-14 16:59:39 -04:00
|
|
|
rb_bug("out of executable memory");
|
|
|
|
}
|
2020-10-29 17:26:49 -04:00
|
|
|
if (ocb->write_pos + 1024 >= ocb->mem_size) {
|
2020-09-28 17:01:26 -04:00
|
|
|
rb_bug("out of executable memory (outlined block)");
|
|
|
|
}
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-09-20 14:23:14 -04:00
|
|
|
// Align the current write positon to cache line boundaries
|
|
|
|
cb_align_pos(cb, 64);
|
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// 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);
|
|
|
|
|
2020-09-16 13:52:35 -04:00
|
|
|
// Get the first opcode in the sequence
|
2020-10-13 11:15:22 -04:00
|
|
|
int first_opcode = opcode_at_pc(iseq, &encoded[insn_idx]);
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// Create codegen context
|
|
|
|
ctx_t ctx;
|
2020-09-17 13:40:50 -04:00
|
|
|
ctx.pc = NULL;
|
|
|
|
ctx.stack_diff = 0;
|
2020-10-13 11:15:22 -04:00
|
|
|
ctx.iseq = iseq;
|
2020-11-09 16:38:50 -05:00
|
|
|
ctx.code_ptr = code_ptr;
|
|
|
|
ctx.start_idx = insn_idx;
|
2020-09-16 10:33:34 -04:00
|
|
|
|
2020-09-16 13:52:35 -04:00
|
|
|
// For each instruction to compile
|
2020-11-04 12:03:39 -05:00
|
|
|
unsigned num_instrs = 0;
|
|
|
|
for (;;) {
|
2020-09-16 13:52:35 -04:00
|
|
|
// Set the current PC
|
2020-10-13 11:15:22 -04:00
|
|
|
ctx.pc = &encoded[insn_idx];
|
2020-09-16 13:52:35 -04:00
|
|
|
|
|
|
|
// Get the current opcode
|
|
|
|
int opcode = ctx_get_opcode(&ctx);
|
|
|
|
|
|
|
|
// Lookup the codegen function for this instruction
|
|
|
|
st_data_t st_gen_fn;
|
2020-10-29 17:26:49 -04:00
|
|
|
if (!rb_st_lookup(gen_fns, opcode, &st_gen_fn)) {
|
2020-09-21 16:46:57 -04:00
|
|
|
//print_int(cb, imm_opnd(num_instrs));
|
2020-09-18 12:20:43 -04:00
|
|
|
//print_str(cb, insn_name(opcode));
|
2020-09-16 13:52:35 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the pre call bytes before the first instruction
|
2020-10-29 17:26:49 -04:00
|
|
|
if (num_instrs == 0) {
|
2020-09-28 15:50:41 -04:00
|
|
|
ujit_gen_entry(cb);
|
2020-09-17 13:40:50 -04:00
|
|
|
|
2020-10-20 11:11:47 -04:00
|
|
|
// Load the current SP from the CFP into REG_SP
|
|
|
|
mov(cb, REG_SP, member_opnd(REG_CFP, rb_control_frame_t, sp));
|
2020-09-16 13:52:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the code generation function
|
|
|
|
codegen_fn gen_fn = (codegen_fn)st_gen_fn;
|
2020-10-29 17:26:49 -04:00
|
|
|
if (!gen_fn(cb, ocb, &ctx)) {
|
2020-10-15 11:46:37 -04:00
|
|
|
break;
|
|
|
|
}
|
2020-09-16 13:52:35 -04:00
|
|
|
|
|
|
|
// Move to the next instruction
|
|
|
|
insn_idx += insn_len(opcode);
|
2020-11-04 12:03:39 -05:00
|
|
|
num_instrs++;
|
2020-10-29 17:26:49 -04:00
|
|
|
|
|
|
|
// Ensure we only have one send per region. Our code invalidation mechanism can't
|
|
|
|
// invalidate running code and one send could invalidate the other if we had
|
|
|
|
// multiple in the same region.
|
|
|
|
if (opcode == BIN(opt_send_without_block)) {
|
|
|
|
break;
|
|
|
|
}
|
2020-09-16 13:52:35 -04:00
|
|
|
}
|
2020-09-11 15:36:40 -04:00
|
|
|
|
2020-09-17 11:49:53 -04:00
|
|
|
// Let the caller know how many instructions ujit compiled
|
|
|
|
*next_ujit_idx = insn_idx;
|
|
|
|
|
2020-09-16 13:52:35 -04:00
|
|
|
// If no instructions were compiled
|
2020-10-29 17:26:49 -04:00
|
|
|
if (num_instrs == 0) {
|
2020-09-16 13:52:35 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-09-14 14:36:39 -04:00
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Generate code to exit to the interpreter
|
2020-10-29 17:26:49 -04:00
|
|
|
ujit_gen_exit(cb, &ctx, &encoded[*next_ujit_idx]);
|
2020-09-21 16:46:57 -04:00
|
|
|
|
2020-09-16 13:52:35 -04:00
|
|
|
addr2insn_bookkeeping(code_ptr, first_opcode);
|
2020-09-15 17:04:33 -04:00
|
|
|
|
2020-10-29 17:26:49 -04:00
|
|
|
if (UJIT_DUMP_MODE >= 2) {
|
|
|
|
// Dump list of compiled instrutions
|
|
|
|
fprintf(stderr, "Compiled the following for iseq=%p:\n", (void *)iseq);
|
|
|
|
VALUE *pc = &encoded[first_insn_idx];
|
|
|
|
VALUE *end_pc = &encoded[*next_ujit_idx];
|
|
|
|
while (pc < end_pc) {
|
|
|
|
int opcode = opcode_at_pc(iseq, pc);
|
|
|
|
fprintf(stderr, " %04td %s\n", pc - encoded, insn_name(opcode));
|
|
|
|
pc += insn_len(opcode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 17:04:33 -04:00
|
|
|
return code_ptr;
|
2020-09-16 10:33:34 -04:00
|
|
|
}
|
2020-09-14 11:54:25 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_dup(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-18 12:40:05 -04:00
|
|
|
{
|
|
|
|
x86opnd_t dup_val = ctx_stack_pop(ctx, 1);
|
|
|
|
x86opnd_t loc0 = ctx_stack_push(ctx, 1);
|
|
|
|
x86opnd_t loc1 = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, RAX, dup_val);
|
|
|
|
mov(cb, loc0, RAX);
|
|
|
|
mov(cb, loc1, RAX);
|
2020-10-15 11:46:37 -04:00
|
|
|
return true;
|
2020-09-18 12:40:05 -04:00
|
|
|
}
|
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_nop(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
2020-09-18 12:20:43 -04:00
|
|
|
// Do nothing
|
2020-10-15 11:46:37 -04:00
|
|
|
return true;
|
2020-09-16 10:33:34 -04:00
|
|
|
}
|
2020-09-14 11:54:25 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_pop(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
|
|
|
// Decrement SP
|
2020-09-17 13:40:50 -04:00
|
|
|
ctx_stack_pop(ctx, 1);
|
2020-10-15 11:46:37 -04:00
|
|
|
return true;
|
2020-09-16 10:33:34 -04:00
|
|
|
}
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_putnil(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
|
|
|
// Write constant at SP
|
2020-09-17 13:40:50 -04:00
|
|
|
x86opnd_t stack_top = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, stack_top, imm_opnd(Qnil));
|
2020-10-15 11:46:37 -04:00
|
|
|
return true;
|
2020-09-16 10:33:34 -04:00
|
|
|
}
|
2020-09-14 16:59:39 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_putobject(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-16 15:30:23 -04:00
|
|
|
{
|
2020-10-05 11:31:24 -04:00
|
|
|
// Load the argument from the bytecode sequence.
|
|
|
|
// We need to do this as the argument can chanage due to GC compaction.
|
|
|
|
x86opnd_t pc_imm = const_ptr_opnd((void*)ctx->pc);
|
|
|
|
mov(cb, RAX, pc_imm);
|
|
|
|
mov(cb, RAX, mem_opnd(64, RAX, 8)); // One after the opcode
|
2020-09-16 15:30:23 -04:00
|
|
|
|
2020-10-05 11:31:24 -04:00
|
|
|
// Write argument at SP
|
2020-09-17 13:40:50 -04:00
|
|
|
x86opnd_t stack_top = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, stack_top, RAX);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-09-17 13:40:50 -04:00
|
|
|
}
|
2020-09-16 15:30:23 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_putobject_int2fix(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-17 13:40:50 -04:00
|
|
|
{
|
|
|
|
int opcode = ctx_get_opcode(ctx);
|
|
|
|
int cst_val = (opcode == BIN(putobject_INT2FIX_0_))? 0:1;
|
2020-09-16 15:30:23 -04:00
|
|
|
|
2020-09-17 13:40:50 -04:00
|
|
|
// Write constant at SP
|
|
|
|
x86opnd_t stack_top = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, stack_top, imm_opnd(INT2FIX(cst_val)));
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-09-16 15:30:23 -04:00
|
|
|
}
|
2020-09-15 15:12:31 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_putself(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-18 12:20:43 -04:00
|
|
|
{
|
|
|
|
// Load self from CFP
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, RAX, member_opnd(REG_CFP, rb_control_frame_t, self));
|
2020-09-18 12:20:43 -04:00
|
|
|
|
|
|
|
// Write it on the stack
|
|
|
|
x86opnd_t stack_top = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, stack_top, RAX);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-09-18 12:20:43 -04:00
|
|
|
}
|
2020-09-15 15:12:31 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_getlocal_wc0(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-16 10:33:34 -04:00
|
|
|
{
|
2020-09-24 15:57:53 -04:00
|
|
|
// Load environment pointer EP from CFP
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, REG0, member_opnd(REG_CFP, rb_control_frame_t, ep));
|
2020-09-15 15:12:31 -04:00
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// Compute the offset from BP to the local
|
|
|
|
int32_t local_idx = (int32_t)ctx_get_arg(ctx, 0);
|
|
|
|
const int32_t offs = -8 * local_idx;
|
2020-09-15 15:12:31 -04:00
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// Load the local from the block
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, REG0, mem_opnd(64, REG0, offs));
|
2020-09-15 15:12:31 -04:00
|
|
|
|
2020-09-16 10:33:34 -04:00
|
|
|
// Write the local at SP
|
2020-09-17 13:40:50 -04:00
|
|
|
x86opnd_t stack_top = ctx_stack_push(ctx, 1);
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, stack_top, REG0);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-09-10 17:20:46 -04:00
|
|
|
}
|
2020-09-14 13:31:34 -04:00
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_setlocal_wc0(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-24 15:57:53 -04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
vm_env_write(const VALUE *ep, int index, VALUE v)
|
|
|
|
{
|
|
|
|
VALUE flags = ep[VM_ENV_DATA_INDEX_FLAGS];
|
|
|
|
if (LIKELY((flags & VM_ENV_FLAG_WB_REQUIRED) == 0)) {
|
|
|
|
VM_STACK_ENV_WRITE(ep, index, v);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm_env_write_slowpath(ep, index, v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Load environment pointer EP from CFP
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, REG0, member_opnd(REG_CFP, rb_control_frame_t, ep));
|
2020-09-24 15:57:53 -04:00
|
|
|
|
|
|
|
// flags & VM_ENV_FLAG_WB_REQUIRED
|
2020-10-20 11:11:47 -04:00
|
|
|
x86opnd_t flags_opnd = mem_opnd(64, REG0, 8 * VM_ENV_DATA_INDEX_FLAGS);
|
2020-09-28 15:50:41 -04:00
|
|
|
test(cb, flags_opnd, imm_opnd(VM_ENV_FLAG_WB_REQUIRED));
|
2020-09-24 15:57:53 -04:00
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Create a size-exit to fall back to the interpreter
|
|
|
|
uint8_t* side_exit = ujit_side_exit(ocb, ctx, ctx->pc);
|
2020-09-24 15:57:53 -04:00
|
|
|
|
|
|
|
// if (flags & VM_ENV_FLAG_WB_REQUIRED) != 0
|
2020-09-28 15:50:41 -04:00
|
|
|
jnz_ptr(cb, side_exit);
|
2020-09-24 15:57:53 -04:00
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Pop the value to write from the stack
|
2020-09-24 15:57:53 -04:00
|
|
|
x86opnd_t stack_top = ctx_stack_pop(ctx, 1);
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, REG1, stack_top);
|
2020-09-24 15:57:53 -04:00
|
|
|
|
2020-09-28 15:50:41 -04:00
|
|
|
// Write the value at the environment pointer
|
2020-09-24 15:57:53 -04:00
|
|
|
int32_t local_idx = (int32_t)ctx_get_arg(ctx, 0);
|
|
|
|
const int32_t offs = -8 * local_idx;
|
2020-10-20 11:11:47 -04:00
|
|
|
mov(cb, mem_opnd(64, REG0, offs), REG1);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-09-24 15:57:53 -04:00
|
|
|
}
|
|
|
|
|
2020-11-10 15:35:55 -05:00
|
|
|
static bool
|
|
|
|
gen_getinstancevariable(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
|
|
|
{
|
|
|
|
IVC ic = (IVC)ctx_get_arg(ctx, 1);
|
|
|
|
|
|
|
|
// Check that the inline cache has been set, slot index is known
|
|
|
|
if (!ic->entry)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:25:14 -05:00
|
|
|
// If the class uses the default allocator, instances should all be T_OBJECT
|
2020-11-12 14:25:11 -05:00
|
|
|
// NOTE: This assumes nobody changes the allocator of the class after allocation.
|
|
|
|
// Eventually, we can encode whether an object is T_OBJECT or not
|
|
|
|
// inside object shapes.
|
2020-11-12 12:25:14 -05:00
|
|
|
if (rb_get_alloc_func(ic->entry->class_value) != rb_class_allocate_instance)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-11-10 15:35:55 -05:00
|
|
|
uint32_t ivar_index = ic->entry->index;
|
|
|
|
|
|
|
|
// Create a size-exit to fall back to the interpreter
|
|
|
|
uint8_t* side_exit = ujit_side_exit(ocb, ctx, ctx->pc);
|
|
|
|
|
|
|
|
// Load self from CFP
|
|
|
|
mov(cb, REG0, member_opnd(REG_CFP, rb_control_frame_t, self));
|
|
|
|
|
|
|
|
// Check that the receiver is a heap object
|
|
|
|
test(cb, REG0, imm_opnd(RUBY_IMMEDIATE_MASK));
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
cmp(cb, REG0, imm_opnd(Qfalse));
|
|
|
|
je_ptr(cb, side_exit);
|
|
|
|
cmp(cb, REG0, imm_opnd(Qnil));
|
|
|
|
je_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Bail if receiver class is different from compiled time call cache class
|
|
|
|
x86opnd_t klass_opnd = mem_opnd(64, REG0, offsetof(struct RBasic, klass));
|
|
|
|
mov(cb, REG1, klass_opnd);
|
|
|
|
x86opnd_t serial_opnd = mem_opnd(64, REG1, offsetof(struct RClass, class_serial));
|
|
|
|
cmp(cb, serial_opnd, imm_opnd(ic->entry->class_serial));
|
|
|
|
jne_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Bail if the ivars are not on the extended table
|
|
|
|
// See ROBJECT_IVPTR() from include/ruby/internal/core/robject.h
|
|
|
|
x86opnd_t flags_opnd = member_opnd(REG0, struct RBasic, flags);
|
|
|
|
test(cb, flags_opnd, imm_opnd(ROBJECT_EMBED));
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Get a pointer to the extended table
|
|
|
|
x86opnd_t tbl_opnd = mem_opnd(64, REG0, offsetof(struct RObject, as.heap.ivptr));
|
|
|
|
mov(cb, REG0, tbl_opnd);
|
|
|
|
|
|
|
|
// Read the ivar from the extended table
|
|
|
|
x86opnd_t ivar_opnd = mem_opnd(64, REG0, sizeof(VALUE) * ivar_index);
|
|
|
|
mov(cb, REG0, ivar_opnd);
|
|
|
|
|
|
|
|
// Check that the ivar is not Qundef
|
|
|
|
cmp(cb, REG0, imm_opnd(Qundef));
|
|
|
|
je_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Push the ivar on the stack
|
|
|
|
x86opnd_t out_opnd = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, out_opnd, REG0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 15:04:36 -05:00
|
|
|
static bool
|
|
|
|
gen_setinstancevariable(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
|
|
|
{
|
|
|
|
IVC ic = (IVC)ctx_get_arg(ctx, 1);
|
|
|
|
|
|
|
|
// Check that the inline cache has been set, slot index is known
|
|
|
|
if (!ic->entry)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the class uses the default allocator, instances should all be T_OBJECT
|
|
|
|
// NOTE: This assumes nobody changes the allocator of the class after allocation.
|
|
|
|
// Eventually, we can encode whether an object is T_OBJECT or not
|
|
|
|
// inside object shapes.
|
|
|
|
if (rb_get_alloc_func(ic->entry->class_value) != rb_class_allocate_instance)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ivar_index = ic->entry->index;
|
|
|
|
|
|
|
|
// Create a size-exit to fall back to the interpreter
|
|
|
|
uint8_t* side_exit = ujit_side_exit(ocb, ctx, ctx->pc);
|
|
|
|
|
|
|
|
// Load self from CFP
|
|
|
|
mov(cb, REG0, member_opnd(REG_CFP, rb_control_frame_t, self));
|
|
|
|
|
|
|
|
// Check that the receiver is a heap object
|
|
|
|
test(cb, REG0, imm_opnd(RUBY_IMMEDIATE_MASK));
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
cmp(cb, REG0, imm_opnd(Qfalse));
|
|
|
|
je_ptr(cb, side_exit);
|
|
|
|
cmp(cb, REG0, imm_opnd(Qnil));
|
|
|
|
je_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Bail if receiver class is different from compiled time call cache class
|
|
|
|
x86opnd_t klass_opnd = mem_opnd(64, REG0, offsetof(struct RBasic, klass));
|
|
|
|
mov(cb, REG1, klass_opnd);
|
|
|
|
x86opnd_t serial_opnd = mem_opnd(64, REG1, offsetof(struct RClass, class_serial));
|
|
|
|
cmp(cb, serial_opnd, imm_opnd(ic->entry->class_serial));
|
|
|
|
jne_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Bail if the ivars are not on the extended table
|
|
|
|
// See ROBJECT_IVPTR() from include/ruby/internal/core/robject.h
|
|
|
|
x86opnd_t flags_opnd = member_opnd(REG0, struct RBasic, flags);
|
|
|
|
test(cb, flags_opnd, imm_opnd(ROBJECT_EMBED));
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// If we can't guarantee that the extended table is big enoughg
|
|
|
|
if (ivar_index >= ROBJECT_EMBED_LEN_MAX + 1)
|
|
|
|
{
|
|
|
|
// Check that the slot is inside the extended table (num_slots > index)
|
|
|
|
x86opnd_t num_slots = mem_opnd(32, REG0, offsetof(struct RObject, as.heap.numiv));
|
|
|
|
cmp(cb, num_slots, imm_opnd(ivar_index));
|
|
|
|
jle_ptr(cb, side_exit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a pointer to the extended table
|
|
|
|
x86opnd_t tbl_opnd = mem_opnd(64, REG0, offsetof(struct RObject, as.heap.ivptr));
|
|
|
|
mov(cb, REG0, tbl_opnd);
|
|
|
|
|
|
|
|
// Pop the value to write from the stack
|
|
|
|
x86opnd_t stack_top = ctx_stack_pop(ctx, 1);
|
|
|
|
mov(cb, REG1, stack_top);
|
|
|
|
|
|
|
|
// Bail if this is a heap object, because this needs a write barrier
|
|
|
|
test(cb, REG1, imm_opnd(RUBY_IMMEDIATE_MASK));
|
|
|
|
jz_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Write the ivar to the extended table
|
|
|
|
x86opnd_t ivar_opnd = mem_opnd(64, REG0, sizeof(VALUE) * ivar_index);
|
|
|
|
mov(cb, ivar_opnd, REG1);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_opt_minus(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-10-02 12:12:28 -04:00
|
|
|
{
|
|
|
|
// Create a size-exit to fall back to the interpreter
|
|
|
|
// Note: we generate the side-exit before popping operands from the stack
|
|
|
|
uint8_t* side_exit = ujit_side_exit(ocb, ctx, ctx->pc);
|
|
|
|
|
|
|
|
// TODO: make a helper function for this
|
|
|
|
// Make sure that minus isn't redefined for integers
|
|
|
|
mov(cb, RAX, const_ptr_opnd(ruby_current_vm_ptr));
|
|
|
|
test(
|
|
|
|
cb,
|
|
|
|
member_opnd_idx(RAX, rb_vm_t, redefined_flag, BOP_MINUS),
|
|
|
|
imm_opnd(INTEGER_REDEFINED_OP_FLAG)
|
|
|
|
);
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Get the operands and destination from the stack
|
|
|
|
x86opnd_t arg1 = ctx_stack_pop(ctx, 1);
|
|
|
|
x86opnd_t arg0 = ctx_stack_pop(ctx, 1);
|
|
|
|
|
|
|
|
// If not fixnums, fall back
|
|
|
|
test(cb, arg0, imm_opnd(RUBY_FIXNUM_FLAG));
|
|
|
|
jz_ptr(cb, side_exit);
|
|
|
|
test(cb, arg1, imm_opnd(RUBY_FIXNUM_FLAG));
|
|
|
|
jz_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Subtract arg0 - arg1 and test for overflow
|
|
|
|
mov(cb, RAX, arg0);
|
|
|
|
sub(cb, RAX, arg1);
|
|
|
|
jo_ptr(cb, side_exit);
|
|
|
|
add(cb, RAX, imm_opnd(1));
|
|
|
|
|
|
|
|
// Push the output on the stack
|
|
|
|
x86opnd_t dst = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, dst, RAX);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
|
|
|
return true;
|
2020-10-02 12:12:28 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// Verify that calling with cd on receiver goes to callee
|
|
|
|
static void
|
|
|
|
check_cfunc_dispatch(VALUE receiver, struct rb_call_data *cd, void *callee, rb_callable_method_entry_t *compile_time_cme)
|
|
|
|
{
|
|
|
|
if (METHOD_ENTRY_INVALIDATED(compile_time_cme)) {
|
|
|
|
rb_bug("ujit: output code uses invalidated cme %p", (void *)compile_time_cme);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool callee_correct = false;
|
|
|
|
const rb_callable_method_entry_t *cme = rb_callable_method_entry(CLASS_OF(receiver), vm_ci_mid(cd->ci));
|
|
|
|
if (cme->def->type == VM_METHOD_TYPE_CFUNC) {
|
|
|
|
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
|
|
|
|
if ((void *)cfunc->func == callee) {
|
|
|
|
callee_correct = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!callee_correct) {
|
|
|
|
rb_bug("ujit: output code calls wrong method cd->cc->klass: %p", (void *)cd->cc->klass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:58:26 -04:00
|
|
|
MJIT_FUNC_EXPORTED VALUE rb_hash_has_key(VALUE hash, VALUE key);
|
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-27 15:58:26 -04:00
|
|
|
cfunc_needs_frame(const rb_method_cfunc_t *cfunc)
|
|
|
|
{
|
|
|
|
void* fptr = (void*)cfunc->func;
|
|
|
|
|
|
|
|
// Leaf C functions do not need a stack frame
|
|
|
|
// or a stack overflow check
|
|
|
|
return !(
|
|
|
|
// Hash#key?
|
|
|
|
fptr == (void*)rb_hash_has_key
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-29 17:40:58 -04:00
|
|
|
static bool
|
2020-10-15 11:46:37 -04:00
|
|
|
gen_opt_send_without_block(codeblock_t* cb, codeblock_t* ocb, ctx_t* ctx)
|
2020-09-30 15:17:13 -04:00
|
|
|
{
|
2020-10-19 16:31:15 -04:00
|
|
|
// Relevant definitions:
|
2020-10-27 14:25:00 -04:00
|
|
|
// rb_execution_context_t : vm_core.h
|
|
|
|
// invoker, cfunc logic : method.h, vm_method.c
|
|
|
|
// rb_callable_method_entry_t : method.h
|
|
|
|
// vm_call_cfunc_with_frame : vm_insnhelper.c
|
|
|
|
// rb_callcache : vm_callinfo.h
|
2020-10-15 15:15:31 -04:00
|
|
|
|
2020-09-30 15:17:13 -04:00
|
|
|
struct rb_call_data * cd = (struct rb_call_data *)ctx_get_arg(ctx, 0);
|
|
|
|
int32_t argc = (int32_t)vm_ci_argc(cd->ci);
|
|
|
|
|
2020-10-15 11:46:37 -04:00
|
|
|
// Don't JIT calls with keyword splat
|
|
|
|
if (vm_ci_flag(cd->ci) & VM_CALL_KW_SPLAT)
|
2020-09-30 16:57:54 -04:00
|
|
|
{
|
2020-10-15 11:46:37 -04:00
|
|
|
return false;
|
2020-09-30 15:17:13 -04:00
|
|
|
}
|
|
|
|
|
2020-10-15 15:15:31 -04:00
|
|
|
// Don't JIT calls that aren't simple
|
2020-10-15 11:46:37 -04:00
|
|
|
if (!(vm_ci_flag(cd->ci) & VM_CALL_ARGS_SIMPLE))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:15:31 -04:00
|
|
|
// Don't JIT if the inline cache is not set
|
2020-10-27 18:49:17 -04:00
|
|
|
if (!cd->cc || !cd->cc->klass) {
|
2020-10-15 15:15:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
const rb_callable_method_entry_t *cme = vm_cc_cme(cd->cc);
|
|
|
|
|
|
|
|
// Don't JIT if the method entry is out of date
|
|
|
|
if (METHOD_ENTRY_INVALIDATED(cme)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-19 16:31:15 -04:00
|
|
|
|
2020-10-15 15:15:31 -04:00
|
|
|
// Don't JIT if this is not a C call
|
2020-10-27 18:49:17 -04:00
|
|
|
if (cme->def->type != VM_METHOD_TYPE_CFUNC)
|
2020-10-15 15:15:31 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
const rb_method_cfunc_t *cfunc = UNALIGNED_MEMBER_PTR(cme->def, body.cfunc);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Don't JIT if the argument count doesn't match
|
2020-10-19 16:31:15 -04:00
|
|
|
if (cfunc->argc < 0 || cfunc->argc != argc)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Don't JIT functions that need C stack arguments for now
|
|
|
|
if (argc + 1 > NUM_C_ARG_REGS)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 14:46:46 -04:00
|
|
|
// Create a size-exit to fall back to the interpreter
|
|
|
|
uint8_t* side_exit = ujit_side_exit(ocb, ctx, ctx->pc);
|
|
|
|
|
2020-10-27 11:10:19 -04:00
|
|
|
// Check for interrupts
|
|
|
|
// RUBY_VM_CHECK_INTS(ec)
|
|
|
|
mov(cb, REG0_32, member_opnd(REG_EC, rb_execution_context_t, interrupt_mask));
|
|
|
|
not(cb, REG0_32);
|
|
|
|
test(cb, member_opnd(REG_EC, rb_execution_context_t, interrupt_flag), REG0_32);
|
|
|
|
jnz_ptr(cb, side_exit);
|
|
|
|
|
2020-10-16 14:46:46 -04:00
|
|
|
// Points to the receiver operand on the stack
|
|
|
|
x86opnd_t recv = ctx_stack_opnd(ctx, argc);
|
2020-10-20 15:03:06 -04:00
|
|
|
mov(cb, REG0, recv);
|
2020-10-16 14:46:46 -04:00
|
|
|
|
2020-10-23 16:49:59 -04:00
|
|
|
// Callee method ID
|
|
|
|
//ID mid = vm_ci_mid(cd->ci);
|
2020-10-22 12:11:29 -04:00
|
|
|
//printf("JITting call to C function \"%s\", argc: %lu\n", rb_id2name(mid), argc);
|
2020-10-21 16:10:19 -04:00
|
|
|
//print_str(cb, "");
|
|
|
|
//print_str(cb, "calling CFUNC:");
|
|
|
|
//print_str(cb, rb_id2name(mid));
|
|
|
|
//print_str(cb, "recv");
|
|
|
|
//print_ptr(cb, recv);
|
|
|
|
|
2020-10-19 16:31:15 -04:00
|
|
|
// Check that the receiver is a heap object
|
2020-10-20 15:03:06 -04:00
|
|
|
test(cb, REG0, imm_opnd(RUBY_IMMEDIATE_MASK));
|
2020-10-16 14:46:46 -04:00
|
|
|
jnz_ptr(cb, side_exit);
|
2020-10-20 15:03:06 -04:00
|
|
|
cmp(cb, REG0, imm_opnd(Qfalse));
|
2020-10-16 16:57:56 -04:00
|
|
|
je_ptr(cb, side_exit);
|
2020-10-20 15:03:06 -04:00
|
|
|
cmp(cb, REG0, imm_opnd(Qnil));
|
2020-10-16 16:57:56 -04:00
|
|
|
je_ptr(cb, side_exit);
|
2020-10-16 14:46:46 -04:00
|
|
|
|
|
|
|
// Pointer to the klass field of the receiver &(recv->klass)
|
2020-10-20 15:03:06 -04:00
|
|
|
x86opnd_t klass_opnd = mem_opnd(64, REG0, offsetof(struct RBasic, klass));
|
2020-10-16 14:46:46 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// Bail if receiver class is different from compiled time call cache class
|
|
|
|
mov(cb, REG1, imm_opnd(cd->cc->klass));
|
|
|
|
cmp(cb, klass_opnd, REG1);
|
2020-10-16 14:46:46 -04:00
|
|
|
jne_ptr(cb, side_exit);
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// Store incremented PC into current control frame in case callee raises.
|
|
|
|
mov(cb, REG0, const_ptr_opnd(ctx->pc + insn_len(BIN(opt_send_without_block))));
|
|
|
|
mov(cb, mem_opnd(64, REG_CFP, offsetof(rb_control_frame_t, pc)), REG0);
|
2020-10-16 14:46:46 -04:00
|
|
|
|
2020-10-27 15:58:26 -04:00
|
|
|
// If this function needs a Ruby stack frame
|
|
|
|
if (cfunc_needs_frame(cfunc))
|
|
|
|
{
|
|
|
|
// Stack overflow check
|
|
|
|
// #define CHECK_VM_STACK_OVERFLOW0(cfp, sp, margin)
|
|
|
|
// REG_CFP <= REG_SP + 4 * sizeof(VALUE) + sizeof(rb_control_frame_t)
|
|
|
|
lea(cb, REG0, ctx_sp_opnd(ctx, sizeof(VALUE) * 4 + sizeof(rb_control_frame_t)));
|
|
|
|
cmp(cb, REG_CFP, REG0);
|
|
|
|
jle_ptr(cb, side_exit);
|
|
|
|
|
|
|
|
// Increment the stack pointer by 3 (in the callee)
|
|
|
|
// sp += 3
|
|
|
|
lea(cb, REG0, ctx_sp_opnd(ctx, sizeof(VALUE) * 3));
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// Put compile time cme into REG1. It's assumed to be valid because we are notified when
|
|
|
|
// any cme we depend on become outdated. See rb_ujit_method_lookup_change().
|
|
|
|
mov(cb, REG1, const_ptr_opnd(cme));
|
2020-10-27 15:58:26 -04:00
|
|
|
// Write method entry at sp[-3]
|
|
|
|
// sp[-3] = me;
|
|
|
|
mov(cb, mem_opnd(64, REG0, 8 * -3), REG1);
|
|
|
|
|
|
|
|
// Write block handler at sp[-2]
|
|
|
|
// sp[-2] = block_handler;
|
|
|
|
mov(cb, mem_opnd(64, REG0, 8 * -2), imm_opnd(VM_BLOCK_HANDLER_NONE));
|
|
|
|
|
|
|
|
// Write env flags at sp[-1]
|
|
|
|
// sp[-1] = frame_type;
|
|
|
|
uint64_t frame_type = VM_FRAME_MAGIC_CFUNC | VM_FRAME_FLAG_CFRAME | VM_ENV_FLAG_LOCAL;
|
|
|
|
mov(cb, mem_opnd(64, REG0, 8 * -1), imm_opnd(frame_type));
|
|
|
|
|
|
|
|
// Allocate a new CFP (ec->cfp--)
|
|
|
|
sub(
|
|
|
|
cb,
|
|
|
|
member_opnd(REG_EC, rb_execution_context_t, cfp),
|
|
|
|
imm_opnd(sizeof(rb_control_frame_t))
|
|
|
|
);
|
|
|
|
|
|
|
|
// Setup the new frame
|
|
|
|
// *cfp = (const struct rb_control_frame_struct) {
|
|
|
|
// .pc = 0,
|
|
|
|
// .sp = sp,
|
|
|
|
// .iseq = 0,
|
|
|
|
// .self = recv,
|
|
|
|
// .ep = sp - 1,
|
|
|
|
// .block_code = 0,
|
|
|
|
// .__bp__ = sp,
|
|
|
|
// };
|
|
|
|
mov(cb, REG1, member_opnd(REG_EC, rb_execution_context_t, cfp));
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, pc), imm_opnd(0));
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, sp), REG0);
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, iseq), imm_opnd(0));
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, block_code), imm_opnd(0));
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, __bp__), REG0);
|
|
|
|
sub(cb, REG0, imm_opnd(sizeof(VALUE)));
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, ep), REG0);
|
|
|
|
mov(cb, REG0, recv);
|
|
|
|
mov(cb, member_opnd(REG1, rb_control_frame_t, self), REG0);
|
|
|
|
}
|
2020-09-30 15:17:13 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
if (UJIT_CHECK_MODE > 0) {
|
|
|
|
// Verify that we are calling the right function
|
|
|
|
// Save MicroJIT registers
|
|
|
|
push(cb, REG_CFP);
|
|
|
|
push(cb, REG_EC);
|
|
|
|
push(cb, REG_SP);
|
|
|
|
// Maintain 16-byte RSP alignment
|
|
|
|
sub(cb, RSP, imm_opnd(8));
|
|
|
|
|
|
|
|
// Call check_cfunc_dispatch
|
|
|
|
mov(cb, RDI, recv);
|
|
|
|
mov(cb, RSI, const_ptr_opnd(cd));
|
|
|
|
mov(cb, RDX, const_ptr_opnd((void *)cfunc->func));
|
|
|
|
mov(cb, RCX, const_ptr_opnd(cme));
|
|
|
|
call_ptr(cb, REG0, (void *)&check_cfunc_dispatch);
|
|
|
|
|
|
|
|
// Restore registers
|
|
|
|
add(cb, RSP, imm_opnd(8));
|
|
|
|
pop(cb, REG_SP);
|
|
|
|
pop(cb, REG_EC);
|
|
|
|
pop(cb, REG_CFP);
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:31:15 -04:00
|
|
|
// Save the MicroJIT registers
|
2020-10-20 11:11:47 -04:00
|
|
|
push(cb, REG_CFP);
|
|
|
|
push(cb, REG_EC);
|
|
|
|
push(cb, REG_SP);
|
2020-09-30 15:17:13 -04:00
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Maintain 16-byte RSP alignment
|
2020-10-22 12:11:29 -04:00
|
|
|
sub(cb, RSP, imm_opnd(8));
|
2020-09-30 15:17:13 -04:00
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Copy SP into RAX because REG_SP will get overwritten
|
2020-10-27 14:25:00 -04:00
|
|
|
lea(cb, RAX, ctx_sp_opnd(ctx, 0));
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Copy the arguments from the stack to the C argument registers
|
2020-10-21 16:10:19 -04:00
|
|
|
// self is the 0th argument and is at index argc from the stack top
|
|
|
|
for (int32_t i = 0; i < argc + 1; ++i)
|
2020-10-20 15:03:06 -04:00
|
|
|
{
|
2020-10-21 16:10:19 -04:00
|
|
|
x86opnd_t stack_opnd = mem_opnd(64, RAX, -(argc + 1 - i) * 8);
|
|
|
|
//print_ptr(cb, stack_opnd);
|
2020-10-20 15:03:06 -04:00
|
|
|
x86opnd_t c_arg_reg = C_ARG_REGS[i];
|
|
|
|
mov(cb, c_arg_reg, stack_opnd);
|
|
|
|
}
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-20 15:03:06 -04:00
|
|
|
// Pop the C function arguments from the stack (in the caller)
|
|
|
|
ctx_stack_pop(ctx, argc + 1);
|
2020-10-19 16:31:15 -04:00
|
|
|
|
2020-10-22 12:11:29 -04:00
|
|
|
//print_str(cb, "before C call");
|
2020-10-19 16:31:15 -04:00
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
ujit_assume_method_lookup_stable(cd->cc, cme, ctx);
|
2020-10-20 15:03:06 -04:00
|
|
|
// Call the C function
|
|
|
|
// VALUE ret = (cfunc->func)(recv, argv[0], argv[1]);
|
2020-10-27 18:49:17 -04:00
|
|
|
// cfunc comes from compile-time cme->def, which we assume to be stable.
|
|
|
|
// Invalidation logic is in rb_ujit_method_lookup_change()
|
2020-10-23 16:49:59 -04:00
|
|
|
call_ptr(cb, REG0, (void*)cfunc->func);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-22 12:11:29 -04:00
|
|
|
//print_str(cb, "after C call");
|
2020-10-20 15:03:06 -04:00
|
|
|
|
|
|
|
// Maintain 16-byte RSP alignment
|
2020-10-22 12:11:29 -04:00
|
|
|
add(cb, RSP, imm_opnd(8));
|
2020-10-19 16:31:15 -04:00
|
|
|
|
|
|
|
// Restore MicroJIT registers
|
2020-10-20 11:11:47 -04:00
|
|
|
pop(cb, REG_SP);
|
|
|
|
pop(cb, REG_EC);
|
|
|
|
pop(cb, REG_CFP);
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-20 16:55:49 -04:00
|
|
|
// Push the return value on the Ruby stack
|
|
|
|
x86opnd_t stack_ret = ctx_stack_push(ctx, 1);
|
|
|
|
mov(cb, stack_ret, RAX);
|
|
|
|
|
2020-10-27 15:58:26 -04:00
|
|
|
// If this function needs a Ruby stack frame
|
|
|
|
if (cfunc_needs_frame(cfunc))
|
|
|
|
{
|
|
|
|
// Pop the stack frame (ec->cfp++)
|
|
|
|
add(
|
|
|
|
cb,
|
|
|
|
member_opnd(REG_EC, rb_execution_context_t, cfp),
|
|
|
|
imm_opnd(sizeof(rb_control_frame_t))
|
|
|
|
);
|
|
|
|
}
|
2020-10-19 16:31:15 -04:00
|
|
|
|
2020-10-15 11:46:37 -04:00
|
|
|
return true;
|
2020-09-30 15:17:13 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 11:15:22 -04:00
|
|
|
void
|
|
|
|
rb_ujit_compile_iseq(const rb_iseq_t *iseq)
|
2020-09-28 07:03:28 -04:00
|
|
|
{
|
2020-10-13 11:15:22 -04:00
|
|
|
#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
|
2020-10-27 18:49:17 -04:00
|
|
|
RB_VM_LOCK_ENTER();
|
2020-10-13 11:15:22 -04:00
|
|
|
VALUE *encoded = (VALUE *)iseq->body->iseq_encoded;
|
|
|
|
|
|
|
|
unsigned int insn_idx;
|
|
|
|
unsigned int next_ujit_idx = 0;
|
|
|
|
|
|
|
|
for (insn_idx = 0; insn_idx < iseq->body->iseq_size; /* */) {
|
|
|
|
int insn = opcode_at_pc(iseq, &encoded[insn_idx]);
|
|
|
|
int len = insn_len(insn);
|
|
|
|
|
|
|
|
uint8_t *native_code_ptr = NULL;
|
|
|
|
|
|
|
|
// If ujit hasn't already compiled this instruction
|
|
|
|
if (insn_idx >= next_ujit_idx) {
|
|
|
|
native_code_ptr = ujit_compile_insn(iseq, insn_idx, &next_ujit_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (native_code_ptr) {
|
|
|
|
encoded[insn_idx] = (VALUE)native_code_ptr;
|
|
|
|
}
|
|
|
|
insn_idx += len;
|
|
|
|
}
|
2020-10-27 18:49:17 -04:00
|
|
|
RB_VM_LOCK_LEAVE();
|
2020-10-13 11:15:22 -04:00
|
|
|
#endif
|
2020-09-28 07:03:28 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 18:49:17 -04:00
|
|
|
// Callback when cme or cc become invalid
|
|
|
|
void
|
|
|
|
rb_ujit_method_lookup_change(VALUE cme_or_cc)
|
|
|
|
{
|
|
|
|
if (!method_lookup_dependency) return;
|
|
|
|
|
|
|
|
RUBY_ASSERT(IMEMO_TYPE_P(cme_or_cc, imemo_ment) || IMEMO_TYPE_P(cme_or_cc, imemo_callcache));
|
|
|
|
|
|
|
|
st_data_t image;
|
|
|
|
if (st_lookup(method_lookup_dependency, (st_data_t)cme_or_cc, &image)) {
|
|
|
|
struct compiled_region_array *array = (void *)image;
|
|
|
|
// Invalidate all regions that depend on the cme or cc
|
|
|
|
for (int32_t i = 0; i < array->size; i++) {
|
|
|
|
struct compiled_region *region = &array->data[i];
|
|
|
|
const struct rb_iseq_constant_body *body = region->iseq->body;
|
2020-11-09 16:38:50 -05:00
|
|
|
RUBY_ASSERT((unsigned int)region->start_idx < body->iseq_size);
|
2020-10-27 18:49:17 -04:00
|
|
|
// Restore region address to interpreter address in bytecode sequence
|
2020-11-09 16:38:50 -05:00
|
|
|
if (body->iseq_encoded[region->start_idx] == (VALUE)region->code) {
|
2020-10-27 18:49:17 -04:00
|
|
|
const void *const *code_threading_table = rb_vm_get_insns_address_table();
|
|
|
|
int opcode = rb_vm_insn_addr2insn(region->code);
|
2020-11-09 16:38:50 -05:00
|
|
|
body->iseq_encoded[region->start_idx] = (VALUE)code_threading_table[opcode];
|
2020-10-27 18:49:17 -04:00
|
|
|
if (UJIT_DUMP_MODE > 0) {
|
2020-11-09 16:38:50 -05:00
|
|
|
fprintf(stderr, "cc_or_cme=%p now out of date. Restored idx=%u in iseq=%p\n", (void *)cme_or_cc, (unsigned)region->start_idx, (void *)region->iseq);
|
2020-10-27 18:49:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
array->size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-28 07:03:28 -04:00
|
|
|
void
|
|
|
|
rb_ujit_init(void)
|
2020-09-14 13:31:34 -04:00
|
|
|
{
|
2020-10-15 11:46:37 -04:00
|
|
|
if (!ujit_scrape_successful || !PLATFORM_SUPPORTED_P)
|
|
|
|
{
|
2020-10-05 07:21:45 -04:00
|
|
|
return;
|
|
|
|
}
|
2020-10-15 11:46:37 -04:00
|
|
|
|
2020-10-13 11:15:22 -04:00
|
|
|
rb_ujit_enabled = true;
|
2020-09-28 15:50:41 -04:00
|
|
|
// Initialize the code blocks
|
2020-10-05 13:38:17 -04:00
|
|
|
size_t mem_size = 128 * 1024 * 1024;
|
2020-09-28 15:50:41 -04:00
|
|
|
uint8_t* mem_block = alloc_exec_mem(mem_size);
|
2020-09-14 13:31:34 -04:00
|
|
|
cb = █
|
2020-09-28 15:50:41 -04:00
|
|
|
cb_init(cb, mem_block, mem_size/2);
|
|
|
|
ocb = &outline_block;
|
|
|
|
cb_init(ocb, mem_block + mem_size/2, mem_size/2);
|
2020-09-15 17:04:33 -04:00
|
|
|
|
|
|
|
// Initialize the codegen function table
|
|
|
|
gen_fns = rb_st_init_numtable();
|
|
|
|
|
|
|
|
// Map YARV opcodes to the corresponding codegen functions
|
2020-09-18 12:40:05 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(dup), (st_data_t)&gen_dup);
|
2020-09-15 17:04:33 -04:00
|
|
|
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);
|
2020-09-16 15:30:23 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(putnil), (st_data_t)&gen_putnil);
|
2020-09-17 13:40:50 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(putobject), (st_data_t)&gen_putobject);
|
2020-09-16 10:33:34 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(putobject_INT2FIX_0_), (st_data_t)&gen_putobject_int2fix);
|
|
|
|
st_insert(gen_fns, (st_data_t)BIN(putobject_INT2FIX_1_), (st_data_t)&gen_putobject_int2fix);
|
2020-09-18 12:20:43 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(putself), (st_data_t)&gen_putself);
|
2020-09-16 10:33:34 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(getlocal_WC_0), (st_data_t)&gen_getlocal_wc0);
|
2020-09-28 15:50:41 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(setlocal_WC_0), (st_data_t)&gen_setlocal_wc0);
|
2020-11-10 15:35:55 -05:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(getinstancevariable), (st_data_t)&gen_getinstancevariable);
|
2020-11-17 15:04:36 -05:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(setinstancevariable), (st_data_t)&gen_setinstancevariable);
|
2020-10-02 12:12:28 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(opt_minus), (st_data_t)&gen_opt_minus);
|
2020-09-30 15:17:13 -04:00
|
|
|
st_insert(gen_fns, (st_data_t)BIN(opt_send_without_block), (st_data_t)&gen_opt_send_without_block);
|
2020-10-27 18:49:17 -04:00
|
|
|
|
|
|
|
method_lookup_dependency = st_init_numtable();
|
|
|
|
struct ujit_root_struct *root;
|
|
|
|
VALUE ujit_root = TypedData_Make_Struct(0, struct ujit_root_struct, &ujit_root_type, root);
|
|
|
|
rb_gc_register_mark_object(ujit_root);
|
2020-09-14 13:31:34 -04:00
|
|
|
}
|