10#include "internal/sanitizers.h"
11#include "internal/string.h"
12#include "internal/hash.h"
13#include "internal/variable.h"
14#include "internal/compile.h"
15#include "internal/class.h"
16#include "internal/fixnum.h"
17#include "internal/numeric.h"
18#include "internal/gc.h"
20#include "vm_callinfo.h"
23#include "insns_info.inc"
26#include "vm_insnhelper.h"
28#include "probes_helper.h"
31#include "internal/cont.h"
32#include "internal/jit.h"
44STATIC_ASSERT(64b_size_t, SIZE_MAX == UINT64_MAX);
47STATIC_ASSERT(size_t_no_padding_bits,
sizeof(
size_t) ==
sizeof(uint64_t));
51STATIC_ASSERT(pointer_tagging_scheme,
USE_FLONUM);
55STATIC_ASSERT(rb_len_t_matches_bindings,
sizeof(
rb_len_t) ==
sizeof(int64_t));
57enum yjit_bindgen_constants {
60 YJIT_ISEQ_TRANSLATED = ISEQ_TRANSLATED,
83 VALUE frame_id = PTR2NUM(frame);
85 if (
RTEST(rb_hash_aref(hash, frame_id))) {
89 VALUE frame_info = rb_hash_new();
102 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"name")), name);
103 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"file")), file);
104 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"samples")),
INT2NUM(0));
105 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"total_samples")),
INT2NUM(0));
106 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"edges")), rb_hash_new());
107 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"lines")), rb_hash_new());
110 rb_hash_aset(frame_info,
ID2SYM(rb_intern(
"line")), line);
113 rb_hash_aset(hash, frame_id, frame_info);
123rb_yjit_exit_locations_dict(
VALUE *yjit_raw_samples,
int *yjit_line_samples,
int samples_len)
125 VALUE result = rb_hash_new();
128 VALUE frames = rb_hash_new();
133 while (idx < samples_len) {
134 int num = (int)yjit_raw_samples[idx];
135 int line_num = (int)yjit_line_samples[idx];
145 for (
int o = 0; o < num; o++) {
146 rb_yjit_add_frame(frames, yjit_raw_samples[idx]);
163 rb_hash_aset(result,
ID2SYM(rb_intern(
"raw")), raw_samples);
164 rb_hash_aset(result,
ID2SYM(rb_intern(
"lines")), line_samples);
165 rb_hash_aset(result,
ID2SYM(rb_intern(
"frames")), frames);
174 return ruby_vm_c_events_enabled > 0;
195 EXEC_EVENT_HOOK(ec,
RUBY_EVENT_C_RETURN, cfp->self, me->def->original_id, me->called_id, me->owner, return_value);
199 RUBY_DTRACE_CMETHOD_RETURN_HOOK(ec, me->owner, me->def->original_id);
203 ec->cfp->sp[0] = return_value;
210typedef struct rb_iseq_param_keyword rb_seq_param_keyword_struct;
212ID rb_get_symbol_id(
VALUE namep);
216invokebuiltin_delegate_leave_p(
const rb_iseq_t *iseq)
218 int insn1 = rb_vm_insn_addr2opcode((
void *)ISEQ_BODY(iseq)->iseq_encoded[0]);
219 if ((
int)ISEQ_BODY(iseq)->iseq_size != insn_len(insn1) + insn_len(BIN(leave))) {
222 int insn2 = rb_vm_insn_addr2opcode((
void *)ISEQ_BODY(iseq)->iseq_encoded[insn_len(insn1)]);
223 return (insn1 == BIN(opt_invokebuiltin_delegate) || insn1 == BIN(opt_invokebuiltin_delegate_leave)) &&
229rb_yjit_builtin_function(
const rb_iseq_t *iseq)
231 if (invokebuiltin_delegate_leave_p(iseq)) {
264 if (len < 0 || len > VM_ARGC_STACK_MAX)
return Qfalse;
284rb_yjit_splat_varg_cfunc(
VALUE *stack_splat_array)
286 VALUE splat_array = *stack_splat_array;
301rb_yjit_dump_iseq_loc(
const rb_iseq_t *iseq, uint32_t insn_idx)
305 VALUE path = rb_iseq_path(iseq);
307 fprintf(stderr,
"%s %.*s:%u\n", __func__, (
int)
len, ptr, rb_iseq_line_no(iseq, insn_idx));
312num_digits(
int integer)
315 while (integer /= 10) {
323rb_yjit_iseq_inspect(
const rb_iseq_t *iseq)
325 const char *label = RSTRING_PTR(ISEQ_BODY(iseq)->location.label);
326 const char *path = RSTRING_PTR(rb_iseq_path(iseq));
327 int lineno = ISEQ_BODY(iseq)->location.code_location.beg_pos.lineno;
329 const size_t size = strlen(label) + strlen(path) + num_digits(lineno) + 3;
331 snprintf(buf, size,
"%s@%s:%d", label, path, lineno);
346rb_ENCODING_GET(
VALUE obj)
354rb_yjit_obj_written(
VALUE old,
VALUE young,
const char *file,
int line)
356 rb_obj_written(old,
Qundef, young, file, line);
367 uintptr_t code_ptr = (uintptr_t)rb_yjit_iseq_gen_entry_point(iseq, ec, jit_exception);
370 ISEQ_BODY(iseq)->jit_exception = (rb_jit_func_t)code_ptr;
373 ISEQ_BODY(iseq)->jit_entry = (rb_jit_func_t)code_ptr;
385rb_yjit_invalidate_all_method_lookup_assumptions(
void)
393rb_object_shape_count(
void)
396 return ULONG2NUM((
unsigned long)rb_shapes_count());
400rb_yjit_shape_obj_complex_p(
VALUE obj)
402 return rb_obj_shape_complex_p(obj);
406rb_yjit_shape_obj_embedded_p(
VALUE obj)
408 return rb_obj_shape_embedded_p(obj);
412rb_yjit_shape_capacity(shape_id_t shape_id)
414 return RSHAPE_CAPACITY(shape_id);
418rb_yjit_shape_index(shape_id_t shape_id)
420 return RSHAPE_INDEX(shape_id);
425rb_yjit_sendish_sp_pops(
const struct rb_callinfo *ci)
427 return 1 - sp_inc_of_sendish(ci);
432rb_yjit_invokeblock_sp_pops(
const struct rb_callinfo *ci)
434 return 1 - sp_inc_of_invokeblock(ci);
440 return cme->def->body.bmethod.defined_ractor_id;
446rb_yjit_set_exception_return(
rb_control_frame_t *cfp,
void *leave_exit,
void *leave_exception)
448 if (VM_FRAME_FINISHED_P(cfp)) {
450 cfp->jit_return = leave_exit;
452 else if (cfp->jit_return) {
453 while (!VM_FRAME_FINISHED_P(cfp)) {
454 if (cfp->jit_return == leave_exit) {
459 cfp->jit_return = leave_exception;
462 cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp);
468 cfp->jit_return = leave_exception;
485static const uint8_t yjit_unwind_info[] = {
499static RUNTIME_FUNCTION yjit_runtime_function;
504rb_yjit_reserve_addr_space(uint32_t mem_size)
506 uint32_t page_size = rb_jit_get_page_size();
507 uint8_t *base = rb_jit_reserve_addr_space(page_size + mem_size);
510 if (!VirtualAlloc(base, page_size, MEM_COMMIT, PAGE_READWRITE)) {
511 rb_bug(
"yjit: failed to commit the unwind info page, error: %lu", GetLastError());
513 memcpy(base, yjit_unwind_info,
sizeof(yjit_unwind_info));
514 VirtualProtect(base, page_size, PAGE_READONLY, &old_protect);
518 yjit_runtime_function.BeginAddress = 0;
519 yjit_runtime_function.EndAddress = page_size + mem_size;
520 yjit_runtime_function.UnwindData = 0;
521 if (!RtlAddFunctionTable(&yjit_runtime_function, 1, (DWORD64)base)) {
522 rb_bug(
"yjit: failed to register unwind info for JIT code");
525 return base + page_size;
533rb_vm_instruction_size(
void)
535 return VM_INSTRUCTION_SIZE;
539yjit_cdhash_all_fixnum_i(st_data_t key, st_data_t _val, st_data_t data)
542 *((
bool *)data) =
false;
549rb_yjit_cdhash_all_fixnum_p(
VALUE cdhash)
551 bool all_fixnum =
true;
552 st_foreach(rb_imemo_cdhash_tbl(cdhash), yjit_cdhash_all_fixnum_i, (st_data_t)&all_fixnum);
557rb_yjit_cdhash_lookup(
VALUE cdhash, st_data_t key, st_data_t *val)
559 return st_lookup(rb_imemo_cdhash_tbl(cdhash), key, val);
570void rb_yjit_update_max_compile_time_ns(uint64_t max_compile_time_ns);
583 return ULL2NUM(rb_yjit_total_compile_time_ns);
589 return ULL2NUM(rb_yjit_max_compile_time_ns);
595 rb_yjit_update_max_compile_time_ns(
NUM2ULL(max_compile_time_ns));
596 return max_compile_time_ns;
603#define yjit_c_builtin_p rb_yjit_c_builtin_p
#define RUBY_ASSERT_ALWAYS(expr,...)
A variant of RUBY_ASSERT that does not interface with RUBY_DEBUG.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
VALUE rb_profile_frame_full_label(VALUE frame)
Identical to rb_profile_frame_label(), except it returns a qualified result.
VALUE rb_profile_frame_absolute_path(VALUE frame)
Identical to rb_profile_frame_path(), except it tries to expand the returning path.
VALUE rb_profile_frame_path(VALUE frame)
Queries the path of the passed backtrace.
VALUE rb_profile_frame_first_lineno(VALUE frame)
Queries the first line of the method of the passed frame pointer.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define SIZET2NUM
Old name of RB_SIZE2NUM.
#define ZALLOC_N
Old name of RB_ZALLOC_N.
#define T_HASH
Old name of RUBY_T_HASH.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define NUM2ULL
Old name of RB_NUM2ULL.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
static int RB_ENCODING_GET(VALUE obj)
Just another name of rb_enc_get_index.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new_capa(rb_len_t capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_subseq(VALUE ary, rb_len_t beg, rb_len_t len)
Obtains a part of the passed array.
int len
Length of the buffer.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define RARRAY_CONST_PTR
Just another name of rb_array_const_ptr.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
static VALUE RSTRUCT_SET(VALUE st, int k, VALUE v)
Resembles Struct#[]=.
#define RTEST
This is an old name of RB_TEST.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.