12#define RUBY_VM_INSNS_INFO 1
15#include "ruby/internal/config.h"
21#include "eval_intern.h"
25#include "internal/bits.h"
26#include "internal/class.h"
27#include "internal/compile.h"
28#include "internal/error.h"
29#include "internal/file.h"
30#include "internal/hash.h"
31#include "internal/parse.h"
32#include "internal/sanitizers.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
40#include "vm_callinfo.h"
45#include "insns_info.inc"
51#if VM_INSN_INFO_TABLE_IMPL == 2
52static struct succ_index_table *succ_index_table_create(
int max_pos,
int *data,
int size);
53static unsigned int *succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size);
54static int succ_index_lookup(
const struct succ_index_table *sd,
int x);
57#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
60obj_resurrect(
VALUE obj)
62 if (hidden_obj_p(obj)) {
68 obj = rb_ary_resurrect(obj);
71 obj = rb_hash_resurrect(obj);
96 free_arena(compile_data->node.storage_head);
97 free_arena(compile_data->insn.storage_head);
98 if (compile_data->ivar_cache_table) {
99 rb_id_table_free(compile_data->ivar_cache_table);
101 ruby_xfree(compile_data);
106remove_from_constant_cache(
ID id,
IC ic)
110 st_data_t ic_data = (st_data_t)ic;
112 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
114 st_delete(ics, &ic_data, NULL);
116 if (ics->num_entries == 0) {
117 rb_id_table_delete(vm->constant_cache,
id);
127iseq_clear_ic_references(
const rb_iseq_t *iseq)
133 if (!ISEQ_BODY(iseq)->is_entries) {
137 for (
unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
138 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
146 if (segments == NULL)
149 for (
int i = 0; segments[i]; i++) {
151 if (
id == idNULL)
continue;
152 remove_from_constant_cache(
id, ic);
155 ruby_xfree((
void *)segments);
162 RUBY_FREE_ENTER(
"iseq");
164 if (iseq && ISEQ_BODY(iseq)) {
165 iseq_clear_ic_references(iseq);
167 mjit_free_iseq(iseq);
169 rb_yjit_iseq_free(body->yjit_payload);
171 ruby_xfree((
void *)body->iseq_encoded);
172 ruby_xfree((
void *)body->insns_info.body);
173 if (body->insns_info.positions) ruby_xfree((
void *)body->insns_info.positions);
174#if VM_INSN_INFO_TABLE_IMPL == 2
175 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
177 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
178 ruby_xfree((
void *)body->local_table);
179 ruby_xfree((
void *)body->is_entries);
181 if (body->call_data) {
182 ruby_xfree(body->call_data);
184 ruby_xfree((
void *)body->catch_table);
185 ruby_xfree((
void *)body->
param.opt_table);
186 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
187 ruby_xfree((
void *)body->mark_bits.list);
190 ruby_xfree(body->variable.original_iseq);
192 if (body->
param.keyword != NULL) {
193 if (body->
param.keyword->table != &body->local_table[body->
param.keyword->bits_start - body->
param.keyword->num])
194 ruby_xfree((
void *)body->
param.keyword->table);
195 ruby_xfree((
void *)body->
param.keyword->default_values);
196 ruby_xfree((
void *)body->
param.keyword);
198 compile_data_free(ISEQ_COMPILE_DATA(iseq));
199 if (body->outer_variables) rb_id_table_free(body->outer_variables);
203 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
204 rb_hook_list_free(iseq->aux.exec.local_hooks);
207 RUBY_FREE_LEAVE(
"iseq");
210typedef VALUE iseq_value_itr_t(
void *ctx,
VALUE obj);
213iseq_scan_bits(
unsigned int page, iseq_bits_t bits,
VALUE *code, iseq_value_itr_t *func,
void *data)
216 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
219 offset = ntz_intptr(bits);
220 VALUE op = code[page_offset + offset];
221 VALUE newop = func(data, op);
223 code[page_offset + offset] = newop;
226 original_iseq[page_offset + offset] = newop;
234rb_iseq_each_value(
const rb_iseq_t *iseq, iseq_value_itr_t * func,
void *data)
240 size = body->iseq_size;
241 code = body->iseq_encoded;
245 if (body->is_entries) {
247 is_entries += body->ivc_size;
250 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
255 VALUE nv = func(data, icvarc->entry->class_value);
256 if (icvarc->entry->class_value != nv) {
257 icvarc->entry->class_value = nv;
263 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
265 if (is->once.value) {
266 VALUE nv = func(data, is->once.value);
267 if (is->once.value != nv) {
274 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
275 IC ic = (
IC)is_entries;
278 if ((
VALUE)ic->entry != nv) {
279 ic->entry = (
void *)nv;
286 if (body->mark_bits.list) {
287 if (ISEQ_MBITS_BUFLEN(size) == 1) {
288 iseq_scan_bits(0, body->mark_bits.single, code, func, data);
291 if (body->mark_bits.list) {
292 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
293 iseq_bits_t bits = body->mark_bits.list[i];
294 iseq_scan_bits(i, bits, code, func, data);
302update_each_insn_value(
void *ctx,
VALUE obj)
304 return rb_gc_location(obj);
308rb_iseq_update_references(
rb_iseq_t *iseq)
310 if (ISEQ_BODY(iseq)) {
313 body->variable.coverage = rb_gc_location(body->variable.coverage);
314 body->variable.pc2branchindex = rb_gc_location(body->variable.pc2branchindex);
315 body->variable.script_lines = rb_gc_location(body->variable.script_lines);
316 body->location.label = rb_gc_location(body->location.label);
317 body->location.base_label = rb_gc_location(body->location.base_label);
318 body->location.pathobj = rb_gc_location(body->location.pathobj);
319 if (body->local_iseq) {
322 if (body->parent_iseq) {
325 if (body->mandatory_only_iseq) {
326 body->mandatory_only_iseq = (
struct rb_iseq_struct *)rb_gc_location((
VALUE)body->mandatory_only_iseq);
328 if (body->call_data) {
329 for (
unsigned int i=0; i<body->ci_size; i++) {
337 VALUE *original_iseq = ISEQ_ORIGINAL_ISEQ(iseq);
338 rb_iseq_each_value(iseq, update_each_insn_value, (
void *)original_iseq);
340 if (body->
param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
343 i = body->
param.keyword->required_num;
345 for (j = 0; i < body->
param.keyword->num; i++, j++) {
346 VALUE obj = body->
param.keyword->default_values[j];
348 body->
param.keyword->default_values[j] = rb_gc_location(obj);
353 if (body->catch_table) {
356 for (i = 0; i < table->size; i++) {
358 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
365 mjit_update_references(iseq);
368 rb_yjit_iseq_update_references(body->yjit_payload);
374each_insn_value(
void *ctx,
VALUE obj)
376 rb_gc_mark_movable(obj);
383 RUBY_MARK_ENTER(
"iseq");
385 RUBY_MARK_UNLESS_NULL(iseq->wrapper);
387 if (ISEQ_BODY(iseq)) {
390 rb_iseq_each_value(iseq, each_insn_value, NULL);
392 rb_gc_mark_movable(body->variable.coverage);
393 rb_gc_mark_movable(body->variable.pc2branchindex);
394 rb_gc_mark_movable(body->variable.script_lines);
395 rb_gc_mark_movable(body->location.label);
396 rb_gc_mark_movable(body->location.base_label);
397 rb_gc_mark_movable(body->location.pathobj);
398 RUBY_MARK_MOVABLE_UNLESS_NULL((
VALUE)body->mandatory_only_iseq);
399 RUBY_MARK_MOVABLE_UNLESS_NULL((
VALUE)body->parent_iseq);
401 if (body->call_data) {
403 for (
unsigned int i=0; i<body->ci_size; i++) {
407 if (vm_ci_markable(ci)) {
408 rb_gc_mark_movable((
VALUE)ci);
412 VM_ASSERT((cc->flags & VM_CALLCACHE_ON_STACK) == 0);
414 if (vm_cc_markable(cc)) {
415 if (!vm_cc_invalidated_p(cc)) {
416 rb_gc_mark_movable((
VALUE)cc);
419 cds[i].cc = rb_vm_empty_cc();
426 if (body->
param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
427 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
430 i = keyword->required_num;
432 for (j = 0; i < keyword->num; i++, j++) {
433 VALUE obj = keyword->default_values[j];
435 rb_gc_mark_movable(obj);
440 if (body->catch_table) {
443 for (i = 0; i < table->size; i++) {
445 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
447 rb_gc_mark_movable((
VALUE)entry->iseq);
453 mjit_mark_cc_entries(body);
456 rb_yjit_iseq_mark(body->yjit_payload);
461 rb_gc_mark(iseq->aux.loader.obj);
466 rb_iseq_mark_insn_storage(compile_data->insn.storage_head);
468 RUBY_MARK_UNLESS_NULL(compile_data->err_info);
469 if (
RTEST(compile_data->catch_table_ary)) {
470 rb_gc_mark(compile_data->catch_table_ary);
472 VM_ASSERT(compile_data != NULL);
476 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
477 if (iseq->aux.exec.local_hooks) {
478 rb_hook_list_mark(iseq->aux.exec.local_hooks);
482 RUBY_MARK_LEAVE(
"iseq");
486param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
490 if (!pkw)
return size;
492 size +=
sizeof(
struct rb_iseq_param_keyword);
493 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
507 if (ISEQ_EXECUTABLE_P(iseq) && body) {
509 size += body->iseq_size *
sizeof(
VALUE);
511 size += body->local_table_size *
sizeof(
ID);
512 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
513 if (body->catch_table) {
514 size += iseq_catch_table_bytes(body->catch_table->size);
516 size += (body->
param.opt_num + 1) *
sizeof(
VALUE);
517 size += param_keyword_size(body->
param.keyword);
522 if (ISEQ_BODY(iseq)->is_entries) {
524 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
525 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
540 compile_data = ISEQ_COMPILE_DATA(iseq);
546 cur = compile_data->node.storage_head;
557rb_iseq_constant_body_alloc(
void)
568 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
573rb_iseq_pathobj_new(
VALUE path,
VALUE realpath)
579 if (path == realpath ||
581 pathobj = rb_fstring(path);
584 if (!
NIL_P(realpath)) realpath = rb_fstring(realpath);
585 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
595 rb_iseq_pathobj_new(path, realpath));
603 rb_iseq_pathobj_set(iseq, path, realpath);
606 loc->first_lineno = first_lineno;
608 loc->node_id = node_id;
609 loc->code_location = *code_location;
612 loc->code_location.beg_pos.lineno = 0;
613 loc->code_location.beg_pos.column = 0;
614 loc->code_location.end_pos.lineno = -1;
615 loc->code_location.end_pos.column = -1;
628 if (
type == ISEQ_TYPE_TOP) {
629 body->local_iseq = iseq;
631 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
632 body->local_iseq = iseq;
635 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
639 body->parent_iseq = piseq;
642 if (
type == ISEQ_TYPE_MAIN) {
643 body->local_iseq = iseq;
652 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
657 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
665 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
672 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
676 set_relation(iseq, parent);
678 name = rb_fstring(name);
679 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
680 if (iseq != body->local_iseq) {
681 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
683 ISEQ_COVERAGE_SET(iseq,
Qnil);
684 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
685 body->variable.flip_count = 0;
687 if (
NIL_P(script_lines)) {
694 ISEQ_COMPILE_DATA_ALLOC(iseq);
695 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
698 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
699 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
700 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
701 ISEQ_COMPILE_DATA(iseq)->option = option;
702 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
703 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
705 if (option->coverage_enabled) {
706 VALUE coverages = rb_get_coverages();
707 if (
RTEST(coverages)) {
708 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
712 ISEQ_COVERAGE_SET(iseq, coverage);
713 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
714 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_hidden_new(0));
719#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
720static void validate_get_insn_info(
const rb_iseq_t *iseq);
724rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
726#if VM_INSN_INFO_TABLE_IMPL == 2
729 int size = body->insns_info.size;
730 int max_pos = body->iseq_size;
731 int *data = (
int *)body->insns_info.positions;
732 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
733 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
734#if VM_CHECK_MODE == 0
735 ruby_xfree(body->insns_info.positions);
736 body->insns_info.positions = NULL;
741#if VM_INSN_INFO_TABLE_IMPL == 2
745 int size = body->insns_info.size;
746 int max_pos = body->iseq_size;
747 struct succ_index_table *sd = body->insns_info.succ_index_table;
748 return succ_index_table_invert(max_pos, sd, size);
755 iseq->aux.exec.global_trace_events = 0;
756 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
757 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
766 VALUE err = data->err_info;
767 ISEQ_COMPILE_DATA_CLEAR(iseq);
768 compile_data_free(data);
770#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
771 validate_get_insn_info(iseq);
775 VALUE path = pathobj_path(body->location.pathobj);
777 rb_funcallv(err,
rb_intern(
"set_backtrace"), 1, &path);
781 RB_DEBUG_COUNTER_INC(iseq_num);
782 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
784 rb_iseq_init_trace(iseq);
789 OPT_INLINE_CONST_CACHE,
790 OPT_PEEPHOLE_OPTIMIZATION,
791 OPT_TAILCALL_OPTIMIZATION,
792 OPT_SPECIALISED_INSTRUCTION,
793 OPT_OPERANDS_UNIFICATION,
794 OPT_INSTRUCTIONS_UNIFICATION,
796 OPT_FROZEN_STRING_LITERAL,
797 OPT_DEBUG_FROZEN_STRING_LITERAL,
806#define SET_COMPILE_OPTION(o, h, mem) \
807 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
808 if (flag == Qtrue) { (o)->mem = 1; } \
809 else if (flag == Qfalse) { (o)->mem = 0; } \
811#define SET_COMPILE_OPTION_NUM(o, h, mem) \
812 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
813 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
815 SET_COMPILE_OPTION(option, opt, inline_const_cache);
816 SET_COMPILE_OPTION(option, opt, peephole_optimization);
817 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
818 SET_COMPILE_OPTION(option, opt, specialized_instruction);
819 SET_COMPILE_OPTION(option, opt, operands_unification);
820 SET_COMPILE_OPTION(option, opt, instructions_unification);
821 SET_COMPILE_OPTION(option, opt, stack_caching);
822 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
823 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
824 SET_COMPILE_OPTION(option, opt, coverage_enabled);
825 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
826#undef SET_COMPILE_OPTION
827#undef SET_COMPILE_OPTION_NUM
834 set_compile_option_from_hash(option, opt);
841 *option = COMPILE_OPTION_DEFAULT;
844 *option = COMPILE_OPTION_FALSE;
846 else if (opt ==
Qtrue) {
849 ((
int *)option)[i] = 1;
852 *option = COMPILE_OPTION_DEFAULT;
853 set_compile_option_from_hash(option, opt);
863 VALUE opt = rb_hash_new_with_size(11);
864#define SET_COMPILE_OPTION(o, h, mem) \
865 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
866#define SET_COMPILE_OPTION_NUM(o, h, mem) \
867 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
869 SET_COMPILE_OPTION(option, opt, inline_const_cache);
870 SET_COMPILE_OPTION(option, opt, peephole_optimization);
871 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
872 SET_COMPILE_OPTION(option, opt, specialized_instruction);
873 SET_COMPILE_OPTION(option, opt, operands_unification);
874 SET_COMPILE_OPTION(option, opt, instructions_unification);
875 SET_COMPILE_OPTION(option, opt, stack_caching);
876 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
877 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
878 SET_COMPILE_OPTION(option, opt, coverage_enabled);
879 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
881#undef SET_COMPILE_OPTION
882#undef SET_COMPILE_OPTION_NUM
890 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent,
891 0,
type, &COMPILE_OPTION_DEFAULT);
897 if (ast->script_lines ==
Qfalse) {
904 return FIX2INT(ast->script_lines);
910 int line_count = line_offset + ast_line_count(ast);
912 if (line_count >= 0) {
913 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
915 VALUE coverage = rb_default_coverage(len);
916 rb_hash_aset(coverages, path, coverage);
927 VALUE coverages = rb_get_coverages();
929 if (
RTEST(coverages)) {
930 iseq_setup_coverage(coverages, path, ast, 0);
937 iseq_new_setup_coverage(path, ast, 0);
939 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent, 0,
940 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
946 iseq_new_setup_coverage(path, ast, 0);
948 return rb_iseq_new_with_opt(ast, rb_fstring_lit(
"<main>"),
950 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
956 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
957 VALUE coverages = rb_get_coverages();
958 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
959 iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
963 return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
964 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
971 VALUE v1 = iseqw_new(iseq);
973 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
983 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
986 const NODE *node = ast ? ast->root : 0;
995 new_opt = COMPILE_OPTION_DEFAULT;
997 if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option);
1001 if (ast && !
FIXNUM_P(ast->script_lines) && ast->script_lines) {
1002 script_lines = ast->script_lines;
1005 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1008 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
1009 parent, isolated_depth,
type, script_lines, &new_opt);
1011 rb_iseq_compile_node(iseq, node);
1012 finish_iseq_build(iseq);
1014 return iseq_translate(iseq);
1018rb_iseq_new_with_callback(
1021 int first_lineno,
const rb_iseq_t *parent,
1027 if (!option) option = &COMPILE_OPTION_DEFAULT;
1028 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1030 rb_iseq_compile_callback(iseq, ifunc);
1031 finish_iseq_build(iseq);
1037rb_iseq_load_iseq(
VALUE fname)
1042 return iseqw_check(iseqv);
1048#define CHECK_ARRAY(v) rb_to_array_type(v)
1049#define CHECK_HASH(v) rb_to_hash_type(v)
1050#define CHECK_STRING(v) rb_str_to_str(v)
1051#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1054static enum rb_iseq_type
1069 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1070 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1071 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1072 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1073 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1074 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1075 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1076 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1077 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1078 return (
enum rb_iseq_type)-1;
1086 VALUE magic, version1, version2, format_type, misc;
1087 VALUE name, path, realpath, code_location, node_id;
1088 VALUE type, body, locals, params, exception;
1090 st_data_t iseq_type;
1100 data = CHECK_ARRAY(data);
1102 magic = CHECK_STRING(rb_ary_entry(data, i++));
1103 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1104 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1105 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1106 misc = CHECK_HASH(rb_ary_entry(data, i++));
1107 ((void)magic, (
void)version1, (void)version2, (
void)format_type);
1109 name = CHECK_STRING(rb_ary_entry(data, i++));
1110 path = CHECK_STRING(rb_ary_entry(data, i++));
1111 realpath = rb_ary_entry(data, i++);
1112 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1113 int first_lineno =
RB_NUM2INT(rb_ary_entry(data, i++));
1115 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1116 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1117 params = CHECK_HASH(rb_ary_entry(data, i++));
1118 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1119 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1121 ISEQ_BODY(iseq)->local_iseq = iseq;
1123 iseq_type = iseq_type_from_sym(
type);
1124 if (iseq_type == (
enum rb_iseq_type)-1) {
1130 code_location = rb_hash_aref(misc,
ID2SYM(
rb_intern(
"code_location")));
1132 tmp_loc.beg_pos.lineno =
NUM2INT(rb_ary_entry(code_location, 0));
1133 tmp_loc.beg_pos.column =
NUM2INT(rb_ary_entry(code_location, 1));
1134 tmp_loc.end_pos.lineno =
NUM2INT(rb_ary_entry(code_location, 2));
1135 tmp_loc.end_pos.column =
NUM2INT(rb_ary_entry(code_location, 3));
1138 make_compile_option(&option, opt);
1139 option.peephole_optimization = FALSE;
1140 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1141 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1143 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1145 finish_iseq_build(iseq);
1147 return iseqw_new(iseq);
1154iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1158 return iseq_load(data, NULL, opt);
1164 return iseq_load(data,
RTEST(parent) ? (
rb_iseq_t *)parent : NULL, opt);
1172#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1173# define INITIALIZED volatile
1180 VALUE name = rb_fstring_lit(
"<compiled>");
1183 make_compile_option(&option, opt);
1187 parse = rb_parser_compile_file_path;
1190 parse = rb_parser_compile_string_path;
1194 const VALUE parser = rb_parser_new();
1195 const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1197 rb_parser_set_context(parser, outer_scope, FALSE);
1199 ast = (*parse)(parser, file, src, ln);
1202 if (!ast->body.root) {
1203 rb_ast_dispose(ast);
1207 iseq = rb_iseq_new_with_opt(&ast->body, name, file, realpath, ln,
1208 NULL, 0, ISEQ_TYPE_TOP, &option);
1209 rb_ast_dispose(ast);
1218 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1224 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1228rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1230 return rb_iseq_realpath(iseq);
1234rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1236 return NIL_P(rb_iseq_realpath(iseq));
1242 return ISEQ_BODY(iseq)->location.label;
1246rb_iseq_base_label(
const rb_iseq_t *iseq)
1248 return ISEQ_BODY(iseq)->location.base_label;
1252rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1254 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1258rb_iseq_method_name(
const rb_iseq_t *iseq)
1262 if (body->type == ISEQ_TYPE_METHOD) {
1263 return body->location.base_label;
1271rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1274 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1275 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1276 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1277 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1280static ID iseq_type_id(
enum rb_iseq_type
type);
1285 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1291 return ISEQ_COVERAGE(iseq);
1295remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1298 for (; v != (
VALUE)vend; v += stride) {
1299 void *ptr = asan_poisoned_object_p(v);
1300 asan_unpoison_object(v,
false);
1302 if (rb_obj_is_iseq(v)) {
1304 ISEQ_COVERAGE_SET(iseq,
Qnil);
1307 asan_poison_object_if(ptr, v);
1313rb_iseq_remove_coverage_all(
void)
1315 rb_objspace_each_objects(remove_coverage_i, NULL);
1321iseqw_mark(
void *ptr)
1323 rb_gc_mark((
VALUE)ptr);
1327iseqw_memsize(
const void *ptr)
1329 return rb_iseq_memsize((
const rb_iseq_t *)ptr);
1334 {iseqw_mark, NULL, iseqw_memsize,},
1335 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1341 if (iseq->wrapper) {
1342 return iseq->wrapper;
1345 union {
const rb_iseq_t *in;
void *out; } deconst;
1362 return iseqw_new(iseq);
1399iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1405 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1407 case 5: opt = argv[--i];
1408 case 4: line = argv[--i];
1409 case 3: path = argv[--i];
1410 case 2: file = argv[--i];
1413 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1414 if (
NIL_P(path)) path = file;
1420 return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
1444iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1452 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1453 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1455 case 2: opt = argv[--i];
1458 file = rb_fstring(file);
1463 VALUE v = rb_vm_push_frame_fname(ec, file);
1465 parser = rb_parser_new();
1466 rb_parser_set_context(parser, NULL, FALSE);
1467 ast = (
rb_ast_t *)rb_parser_load_file(parser, file);
1468 if (!ast->body.root) exc = GET_EC()->errinfo;
1471 if (!ast->body.root) {
1472 rb_ast_dispose(ast);
1476 make_compile_option(&option, opt);
1478 ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit(
"<main>"),
1480 rb_realpath_internal(
Qnil, file, 1),
1481 1, NULL, 0, ISEQ_TYPE_TOP, &option));
1482 rb_ast_dispose(ast);
1484 rb_vm_pop_frame(ec);
1521iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
1524 make_compile_option(&option, opt);
1525 COMPILE_OPTION_DEFAULT = option;
1538iseqw_s_compile_option_get(
VALUE self)
1540 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1544iseqw_check(
VALUE iseqw)
1548 if (!ISEQ_BODY(iseq)) {
1549 rb_ibf_load_iseq_complete(iseq);
1552 if (!ISEQ_BODY(iseq)->location.label) {
1559rb_iseqw_to_iseq(
VALUE iseqw)
1561 return iseqw_check(iseqw);
1573iseqw_eval(
VALUE self)
1575 return rb_iseq_eval(iseqw_check(self));
1583iseqw_inspect(
VALUE self)
1585 const rb_iseq_t *iseq = iseqw_check(self);
1589 if (!body->location.label) {
1590 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
1593 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
1595 body->location.label, rb_iseq_path(iseq),
1596 FIX2INT(rb_iseq_first_lineno(iseq)));
1624iseqw_path(
VALUE self)
1626 return rb_iseq_path(iseqw_check(self));
1646iseqw_absolute_path(
VALUE self)
1648 return rb_iseq_realpath(iseqw_check(self));
1675iseqw_label(
VALUE self)
1677 return rb_iseq_label(iseqw_check(self));
1701iseqw_base_label(
VALUE self)
1703 return rb_iseq_base_label(iseqw_check(self));
1717iseqw_first_lineno(
VALUE self)
1719 return rb_iseq_first_lineno(iseqw_check(self));
1806iseqw_to_a(
VALUE self)
1808 const rb_iseq_t *iseq = iseqw_check(self);
1809 return iseq_data_to_ary(iseq);
1812#if VM_INSN_INFO_TABLE_IMPL == 1
1814get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
1817 size_t size = body->insns_info.size;
1819 const unsigned int *positions = body->insns_info.positions;
1820 const int debug = 0;
1823 printf(
"size: %"PRIuSIZE
"\n", size);
1824 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1825 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1831 else if (size == 1) {
1832 return &insns_info[0];
1835 size_t l = 1, r = size - 1;
1837 size_t m = l + (r - l) / 2;
1838 if (positions[m] == pos) {
1839 return &insns_info[m];
1841 if (positions[m] < pos) {
1849 return &insns_info[size-1];
1851 if (positions[l] > pos) {
1852 return &insns_info[l-1];
1854 return &insns_info[l];
1859get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1861 return get_insn_info_binary_search(iseq, pos);
1865#if VM_INSN_INFO_TABLE_IMPL == 2
1867get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
1870 size_t size = body->insns_info.size;
1872 const int debug = 0;
1875#if VM_CHECK_MODE > 0
1876 const unsigned int *positions = body->insns_info.positions;
1877 printf(
"size: %"PRIuSIZE
"\n", size);
1878 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1879 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1881 printf(
"size: %"PRIuSIZE
"\n", size);
1882 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
1883 (
size_t)0, insns_info[0].line_no, pos);
1890 else if (size == 1) {
1891 return &insns_info[0];
1895 VM_ASSERT(body->insns_info.succ_index_table != NULL);
1896 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
1897 return &insns_info[index-1];
1902get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1904 return get_insn_info_succinct_bitvector(iseq, pos);
1908#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
1910get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
1913 size_t i = 0, size = body->insns_info.size;
1915 const unsigned int *positions = body->insns_info.positions;
1916 const int debug = 0;
1919 printf(
"size: %"PRIuSIZE
"\n", size);
1920 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1921 i, positions[i], insns_info[i].line_no, pos);
1927 else if (size == 1) {
1928 return &insns_info[0];
1931 for (i=1; i<size; i++) {
1932 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1933 i, positions[i], insns_info[i].line_no, pos);
1935 if (positions[i] == pos) {
1936 return &insns_info[i];
1938 if (positions[i] > pos) {
1939 return &insns_info[i-1];
1943 return &insns_info[i-1];
1947#if VM_INSN_INFO_TABLE_IMPL == 0
1949get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1951 return get_insn_info_linear_search(iseq, pos);
1955#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
1957validate_get_insn_info(
const rb_iseq_t *iseq)
1961 for (i = 0; i < body->iseq_size; i++) {
1962 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
1963 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
1970rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
1975 return entry->line_no;
1982#ifdef USE_ISEQ_NODE_ID
1984rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
1989 return entry->node_id;
1998rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2002 return entry->events;
2014 entry->events &= ~reset;
2015 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2016 void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2017 rb_iseq_trace_flag_cleared(iseq, pos);
2030 for (i = 0; i < level; i++) {
2031 diseq = ISEQ_BODY(diseq)->parent_iseq;
2033 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2034 lid = ISEQ_BODY(diseq)->local_table[idx];
2035 name = rb_id2str(lid);
2039 else if (!rb_str_symname_p(name)) {
2049int rb_insn_unified_local_var_level(
VALUE);
2053rb_insn_operand_intern(
const rb_iseq_t *iseq,
2055 int len,
size_t pos,
const VALUE *pnop,
VALUE child)
2057 const char *types = insn_op_types(insn);
2058 char type = types[op_no];
2067 if (insn == BIN(defined) && op_no == 0) {
2068 enum defined_type deftype = (
enum defined_type)op;
2071 ret = rb_fstring_lit(
"func");
2074 ret = rb_fstring_lit(
"ref");
2076 case DEFINED_CONST_FROM:
2077 ret = rb_fstring_lit(
"constant-from");
2080 ret = rb_iseq_defined_string(deftype);
2085 else if (insn == BIN(checktype) && op_no == 0) {
2096 if (types[op_no+1] == TS_NUM && pnop) {
2097 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2099 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2100 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2112 op = obj_resurrect(op);
2113 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2126 ret = rb_dump_literal(op);
2129 rb_ary_push(child, op);
2138 ret = ISEQ_BODY(iseq)->location.label;
2140 rb_ary_push(child, (
VALUE)iseq);
2152 const ID *segments = ((
IC)op)->segments;
2170 VALUE ary = rb_ary_new();
2171 ID mid = vm_ci_mid(ci);
2174 rb_ary_push(ary,
rb_sprintf(
"mid:%"PRIsVALUE, rb_id2str(mid)));
2177 rb_ary_push(ary,
rb_sprintf(
"argc:%d", vm_ci_argc(ci)));
2179 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2181 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2185 if (vm_ci_flag(ci)) {
2186 VALUE flags = rb_ary_new();
2187# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2188 CALL_FLAG(ARGS_SPLAT);
2189 CALL_FLAG(ARGS_BLOCKARG);
2192 CALL_FLAG(ARGS_SIMPLE);
2193 CALL_FLAG(BLOCKISEQ);
2194 CALL_FLAG(TAILCALL);
2198 CALL_FLAG(KW_SPLAT);
2199 CALL_FLAG(KW_SPLAT_MUT);
2200 CALL_FLAG(OPT_SEND);
2201 rb_ary_push(ary, rb_ary_join(flags,
rb_str_new2(
"|")));
2216 if (dladdr((
void *)op, &info) && info.dli_sname) {
2229 bf->name, bf->argc);
2240right_strip(
VALUE str)
2243 while (end-- > beg && *end ==
' ');
2253rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2256 VALUE insn = code[pos];
2257 int len = insn_len(insn);
2259 const char *types = insn_op_types(insn);
2261 const char *insn_name_buff;
2263 insn_name_buff = insn_name(insn);
2265 extern const int rb_vm_max_insn_name_size;
2266 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2270 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2273 for (j = 0; types[j]; j++) {
2274 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2275 len, pos, &code[pos + j + 2],
2285 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2286 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2287 if (line_no && line_no != prev) {
2289 slen = (slen > 70) ? 0 : (70 - slen);
2290 str =
rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2297 str =
rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s]",
2307 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2308 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2327 case CATCH_TYPE_RESCUE:
2329 case CATCH_TYPE_ENSURE:
2331 case CATCH_TYPE_RETRY:
2333 case CATCH_TYPE_BREAK:
2335 case CATCH_TYPE_REDO:
2337 case CATCH_TYPE_NEXT:
2349 if (!body->location.label) {
2354 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2355 body->location.label, rb_iseq_path(iseq),
2356 loc->beg_pos.lineno,
2357 loc->beg_pos.lineno,
2358 loc->beg_pos.column,
2359 loc->end_pos.lineno,
2360 loc->end_pos.column);
2366 {(void (*)(
void *))rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2367 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2376 VALUE child = rb_ary_hidden_new(3);
2381 enum {header_minlen = 72};
2384 const char *indent_str;
2387 size = body->iseq_size;
2396 rb_str_catf(str,
" (catch: %s)", body->catch_except_p ?
"true" :
"false");
2397 if ((l =
RSTRING_LEN(str) - indent_len) < header_minlen) {
2404 if (body->catch_table) {
2408 if (body->catch_table) {
2411 for (i = 0; i < body->catch_table->size; i++) {
2413 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2416 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2417 catch_type((
int)entry->type), (
int)entry->start,
2418 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2419 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2420 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2422 done_iseq = st_init_numtable();
2425 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2432 if (body->catch_table) {
2434 rb_str_cat2(str,
"|-------------------------------------"
2435 "-----------------------------------\n");
2439 if (body->local_table) {
2440 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
2443 "local table (size: %d, argc: %d "
2444 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2445 body->local_table_size,
2446 body->
param.lead_num,
2447 body->
param.opt_num,
2448 body->
param.flags.has_rest ? body->
param.rest_start : -1,
2449 body->param.post_num,
2450 body->param.flags.has_block ? body->param.block_start : -1,
2451 body->param.flags.has_kw ? keyword->num : -1,
2452 body->param.flags.has_kw ? keyword->required_num : -1,
2453 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2455 for (i = body->local_table_size; i > 0;) {
2456 int li = body->local_table_size - --i - 1;
2458 VALUE name = local_var_name(iseq, 0, i);
2463 if (body->
param.flags.has_opt) {
2464 int argc = body->
param.lead_num;
2465 int opts = body->
param.opt_num;
2466 if (li >= argc && li < argc + opts) {
2467 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2468 body->
param.opt_table[li - argc]);
2472 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2473 body->
param.lead_num > li ?
"Arg" :
"",
2475 (body->param.flags.has_rest && body->param.rest_start == li) ?
"Rest" :
"",
2476 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ?
"Post" :
"",
2477 (body->param.flags.has_kwrest && keyword->rest_start == li) ?
"Kwrest" :
"",
2478 (body->param.flags.has_block && body->param.block_start == li) ?
"Block" :
"");
2491 code = rb_iseq_original_iseq(iseq);
2492 for (n = 0; n < size;) {
2494 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2498 VALUE isv = rb_ary_entry(child, l);
2499 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2523rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
2525 struct rb_id_table * iv_names = rb_id_table_create(0);
2527 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2528 IVC cache = (
IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2530 if (cache->iv_set_name) {
2531 rb_id_table_insert(iv_names, cache->iv_set_name,
Qtrue);
2535 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2538 count += RCLASS_EXT(superclass)->max_iv_count;
2540 rb_id_table_free(iv_names);
2564iseqw_disasm(
VALUE self)
2566 return rb_iseq_disasm(iseqw_check(self));
2570iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
2573 VALUE *code = rb_iseq_original_iseq(iseq);
2578 if (body->catch_table) {
2579 for (i = 0; i < body->catch_table->size; i++) {
2581 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2582 child = entry->iseq;
2584 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
2585 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
2586 (*iter_func)(child, data);
2592 for (i=0; i<body->iseq_size;) {
2593 VALUE insn = code[i];
2594 int len = insn_len(insn);
2595 const char *types = insn_op_types(insn);
2598 for (j=0; types[j]; j++) {
2603 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
2604 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
2605 (*iter_func)(child, data);
2620yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
2634iseqw_each_child(
VALUE self)
2636 const rb_iseq_t *iseq = iseqw_check(self);
2637 iseq_iterate_children(iseq, yield_each_children, NULL);
2644#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2663iseqw_trace_points(
VALUE self)
2665 const rb_iseq_t *iseq = iseqw_check(self);
2668 VALUE ary = rb_ary_new();
2670 for (i=0; i<body->insns_info.size; i++) {
2672 if (entry->events) {
2673 push_event_info(iseq, entry->events, entry->line_no, ary);
2720 iseq = vm_proc_iseq(body);
2722 if (!rb_obj_is_iseq((
VALUE)iseq)) {
2727 iseq = rb_method_iseq(body);
2729 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2733 return iseq ? iseqw_new(iseq) :
Qnil;
2791 VALUE iseqw = iseqw_s_of(klass, body);
2792 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
2796ruby_node_name(
int node)
2799#include "node_name.inc"
2801 rb_bug(
"unknown node: %d", node);
2807register_label(
struct st_table *table,
unsigned long idx)
2810 st_insert(table, idx, sym);
2819 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
2820 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
2821 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
2822 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
2823 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
2824 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
2826 rb_bug(
"unknown exception type: %d", (
int)
type);
2834 rb_ary_push(ary, obj_resurrect(key));
2835 rb_ary_push(ary, value);
2841 {(void (*)(
void *))rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
2842 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2845#define DECL_ID(name) \
2848#define INIT_ID(name) \
2849 id_##name = rb_intern(#name)
2852iseq_type_id(
enum rb_iseq_type
type)
2877 case ISEQ_TYPE_TOP:
return id_top;
2878 case ISEQ_TYPE_METHOD:
return id_method;
2879 case ISEQ_TYPE_BLOCK:
return id_block;
2880 case ISEQ_TYPE_CLASS:
return id_class;
2881 case ISEQ_TYPE_RESCUE:
return id_rescue;
2882 case ISEQ_TYPE_ENSURE:
return id_ensure;
2883 case ISEQ_TYPE_EVAL:
return id_eval;
2884 case ISEQ_TYPE_MAIN:
return id_main;
2885 case ISEQ_TYPE_PLAIN:
return id_plain;
2888 rb_bug(
"unsupported iseq type: %d", (
int)
type);
2900 VALUE *seq, *iseq_original;
2902 VALUE val = rb_ary_new();
2904 VALUE locals = rb_ary_new();
2905 VALUE params = rb_hash_new();
2906 VALUE body = rb_ary_new();
2908 VALUE exception = rb_ary_new();
2909 VALUE misc = rb_hash_new();
2911 static ID insn_syms[VM_INSTRUCTION_SIZE/2];
2912 struct st_table *labels_table = st_init_numtable();
2915 if (insn_syms[0] == 0) {
2917 for (i=0; i<numberof(insn_syms); i++) {
2923 type = iseq_type_id(iseq_body->type);
2926 for (i=0; i<iseq_body->local_table_size; i++) {
2927 ID lid = iseq_body->local_table[i];
2929 if (rb_id2str(lid)) {
2930 rb_ary_push(locals,
ID2SYM(lid));
2933 rb_ary_push(locals,
ULONG2NUM(iseq_body->local_table_size-i+1));
2943 const struct rb_iseq_param_keyword *
const keyword = iseq_body->
param.keyword;
2946 if (iseq_body->
param.flags.has_opt) {
2947 int len = iseq_body->
param.opt_num + 1;
2950 for (j = 0; j < len; j++) {
2951 VALUE l = register_label(labels_table, iseq_body->
param.opt_table[j]);
2952 rb_ary_push(arg_opt_labels, l);
2963 if (iseq_body->
param.flags.has_kw) {
2964 VALUE keywords = rb_ary_new();
2966 for (i=0; i<keyword->required_num; i++) {
2967 rb_ary_push(keywords,
ID2SYM(keyword->table[i]));
2969 for (j=0; i<keyword->num; i++, j++) {
2970 VALUE key = rb_ary_new_from_args(1,
ID2SYM(keyword->table[i]));
2971 if (!UNDEF_P(keyword->default_values[j])) {
2972 rb_ary_push(key, keyword->default_values[j]);
2974 rb_ary_push(keywords, key);
2978 INT2FIX(keyword->bits_start));
2986 iseq_original = rb_iseq_original_iseq((
rb_iseq_t *)iseq);
2988 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
2989 VALUE insn = *seq++;
2990 int j, len = insn_len(insn);
2991 VALUE *nseq = seq + len - 1;
2994 rb_ary_push(ary,
ID2SYM(insn_syms[insn%numberof(insn_syms)]));
2995 for (j=0; j<len-1; j++, seq++) {
2996 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3000 unsigned long idx = nseq - iseq_original + *seq;
3001 rb_ary_push(ary, register_label(labels_table, idx));
3006 rb_ary_push(ary,
INT2FIX(*seq));
3009 rb_ary_push(ary, obj_resurrect(*seq));
3015 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3016 rb_ary_push(ary, val);
3019 rb_ary_push(ary,
Qnil);
3025 VALUE list = rb_ary_new();
3026 const ID *ids = ((
IC)*seq)->segments;
3028 rb_ary_push(list,
ID2SYM(*ids++));
3030 rb_ary_push(ary, list);
3038 rb_ary_push(ary,
INT2FIX(is - ISEQ_IS_ENTRY_START(ISEQ_BODY(iseq), op_type)));
3045 VALUE e = rb_hash_new();
3046 int argc = vm_ci_argc(ci);
3048 ID mid = vm_ci_mid(ci);
3052 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3057 argc -= kwarg->keyword_len;
3058 for (i = 0; i < kwarg->keyword_len; i++) {
3059 rb_ary_push(kw, kwarg->keywords[i]);
3066 rb_ary_push(ary, e);
3070 rb_ary_push(ary,
ID2SYM(*seq));
3075 VALUE val = rb_ary_new();
3082 unsigned long idx = nseq - iseq_original + pos;
3084 rb_ary_store(val, i+1,
3085 register_label(labels_table, idx));
3087 rb_ary_push(ary, val);
3092#if SIZEOF_VALUE <= SIZEOF_LONG
3097 rb_ary_push(ary, val);
3102 VALUE val = rb_hash_new();
3103#if SIZEOF_VALUE <= SIZEOF_LONG
3112 rb_ary_push(ary, val);
3116 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3119 rb_ary_push(body, ary);
3125 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3126 VALUE ary = rb_ary_new();
3128 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3129 rb_ary_push(ary, exception_type2symbol(entry->type));
3131 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3134 rb_ary_push(ary,
Qnil);
3136 rb_ary_push(ary, register_label(labels_table, entry->start));
3137 rb_ary_push(ary, register_label(labels_table, entry->end));
3138 rb_ary_push(ary, register_label(labels_table, entry->cont));
3139 rb_ary_push(ary,
UINT2NUM(entry->sp));
3140 rb_ary_push(exception, ary);
3144 body = rb_ary_new();
3145 prev_insn_info = NULL;
3146#ifdef USE_ISEQ_NODE_ID
3147 VALUE node_ids = rb_ary_new();
3155 if (st_lookup(labels_table, pos, &label)) {
3156 rb_ary_push(body, (
VALUE)label);
3159 info = get_insn_info(iseq, pos);
3160#ifdef USE_ISEQ_NODE_ID
3161 rb_ary_push(node_ids,
INT2FIX(info->node_id));
3164 if (prev_insn_info != info) {
3165 int line = info->line_no;
3168 if (line > 0 && last_line != line) {
3169 rb_ary_push(body,
INT2FIX(line));
3172#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3181 prev_insn_info = info;
3184 rb_ary_push(body, ary);
3195 rb_ary_new_from_args(4,
3196 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3197 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3198 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3199 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3200#ifdef USE_ISEQ_NODE_ID
3209 rb_ary_push(val,
rb_str_new2(
"YARVInstructionSequence/SimpleDataFormat"));
3210 rb_ary_push(val,
INT2FIX(ISEQ_MAJOR_VERSION));
3211 rb_ary_push(val,
INT2FIX(ISEQ_MINOR_VERSION));
3213 rb_ary_push(val, misc);
3214 rb_ary_push(val, iseq_body->location.label);
3215 rb_ary_push(val, rb_iseq_path(iseq));
3216 rb_ary_push(val, rb_iseq_realpath(iseq));
3217 rb_ary_push(val,
RB_INT2NUM(iseq_body->location.first_lineno));
3219 rb_ary_push(val, locals);
3220 rb_ary_push(val, params);
3221 rb_ary_push(val, exception);
3222 rb_ary_push(val, body);
3227rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3231 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
3233 ID req, opt, rest, block, key, keyrest;
3234#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3235#define PARAM_ID(i) body->local_table[(i)]
3236#define PARAM(i, type) ( \
3238 rb_id2str(PARAM_ID(i)) ? \
3239 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3245 for (i = 0; i < body->
param.lead_num; i++) {
3247 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ?
ID2SYM(PARAM_ID(i)) :
Qnil);
3248 rb_ary_push(args, a);
3252 for (i = 0; i < body->
param.lead_num; i++) {
3253 rb_ary_push(args, PARAM(i, req));
3256 r = body->
param.lead_num + body->
param.opt_num;
3257 for (; i < r; i++) {
3259 if (rb_id2str(PARAM_ID(i))) {
3260 rb_ary_push(a,
ID2SYM(PARAM_ID(i)));
3262 rb_ary_push(args, a);
3264 if (body->
param.flags.has_rest) {
3266 rb_ary_push(args, PARAM(body->
param.rest_start, rest));
3268 r = body->
param.post_start + body->
param.post_num;
3270 for (i = body->
param.post_start; i < r; i++) {
3272 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ?
ID2SYM(PARAM_ID(i)) :
Qnil);
3273 rb_ary_push(args, a);
3277 for (i = body->
param.post_start; i < r; i++) {
3278 rb_ary_push(args, PARAM(i, req));
3281 if (body->
param.flags.accepts_no_kwarg) {
3285 rb_ary_push(args, a);
3287 if (body->
param.flags.has_kw) {
3289 if (keyword->required_num > 0) {
3292 for (; i < keyword->required_num; i++) {
3294 if (rb_id2str(keyword->table[i])) {
3295 rb_ary_push(a,
ID2SYM(keyword->table[i]));
3297 rb_ary_push(args, a);
3301 for (; i < keyword->num; i++) {
3303 if (rb_id2str(keyword->table[i])) {
3304 rb_ary_push(a,
ID2SYM(keyword->table[i]));
3306 rb_ary_push(args, a);
3309 if (body->
param.flags.has_kwrest || body->
param.flags.ruby2_keywords) {
3312 PARAM_TYPE(keyrest);
3313 if (body->
param.flags.has_kwrest &&
3314 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3315 rb_ary_push(a,
ID2SYM(param));
3317 else if (body->
param.flags.ruby2_keywords) {
3318 rb_ary_push(a,
ID2SYM(idPow));
3320 rb_ary_push(args, a);
3322 if (body->
param.flags.has_block) {
3324 rb_ary_push(args, PARAM(body->
param.block_start, block));
3330rb_iseq_defined_string(
enum defined_type
type)
3332 static const char expr_names[][18] = {
3334 "instance-variable",
3350 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names))
rb_bug(
"unknown defined type %d",
type);
3351 estr = expr_names[
type - 1];
3352 return rb_fstring_cstr(estr);
3362 void *notrace_encoded_insn;
3363 void *trace_encoded_insn;
3365static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3368rb_vm_encoded_insn_data_table_init(
void)
3370#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3371 const void *
const *table = rb_vm_get_insns_address_table();
3372#define INSN_CODE(insn) ((VALUE)table[insn])
3374#define INSN_CODE(insn) (insn)
3377 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3379 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3380 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3381 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3383 insn_data[insn].insn = (int)insn;
3384 insn_data[insn].insn_len = insn_len(insn);
3386 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3387 insn_data[insn].notrace_encoded_insn = (
void *) key1;
3388 insn_data[insn].trace_encoded_insn = (
void *) key2;
3391 insn_data[insn].notrace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3392 insn_data[insn].trace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3395 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3396 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3401rb_vm_insn_addr2insn(
const void *addr)
3403 st_data_t key = (st_data_t)addr;
3406 if (st_lookup(encoded_insn_data, key, &val)) {
3408 return (
int)e->insn;
3411 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3416rb_vm_insn_addr2opcode(
const void *addr)
3418 st_data_t key = (st_data_t)addr;
3421 if (st_lookup(encoded_insn_data, key, &val)) {
3423 int opcode = e->insn;
3424 if (addr == e->trace_encoded_insn) {
3425 opcode += VM_INSTRUCTION_SIZE/2;
3430 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3435rb_vm_insn_decode(
const VALUE encoded)
3437#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3438 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3440 int insn = (int)encoded;
3446encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_current_trace)
3448 st_data_t key = (st_data_t)*iseq_encoded_insn;
3451 if (st_lookup(encoded_insn_data, key, &val)) {
3453 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3456 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3460 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3464rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3467 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3468 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
3494 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3496 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3498 for (pc=0; pc<body->iseq_size;) {
3502 unsigned int line = (int)entry->line_no;
3504 if (target_line == 0 || target_line == line) {
3508 target_events &= ~RUBY_EVENT_LINE;
3511 if (pc_events & target_events) {
3514 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
3518 if (iseq->aux.exec.local_hooks == NULL) {
3520 iseq->aux.exec.local_hooks->is_local =
true;
3522 rb_hook_list_connect_tracepoint((
VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3531 unsigned int target_line;
3536iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3539 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3540 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3544rb_iseq_add_local_tracepoint_recursively(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events,
VALUE tpval,
unsigned int target_line,
bool target_bmethod)
3547 if (target_bmethod) {
3548 turnon_events = add_bmethod_events(turnon_events);
3550 data.turnon_events = turnon_events;
3552 data.target_line = target_line;
3555 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
3565 if (iseq->aux.exec.local_hooks) {
3568 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3571 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3572 local_events = iseq->aux.exec.local_hooks->events;
3574 if (local_events == 0) {
3575 rb_hook_list_free(iseq->aux.exec.local_hooks);
3576 ((
rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3579 local_events = add_bmethod_events(local_events);
3580 for (pc = 0; pc<body->iseq_size;) {
3582 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
3594iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3597 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3598 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3602rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval)
3608 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
3615 if (iseq->aux.exec.global_trace_events == turnon_events) {
3619 if (!ISEQ_EXECUTABLE_P(iseq)) {
3626 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3628 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3629 ((
rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3630 enabled_events = add_bmethod_events(turnon_events | local_events);
3632 for (pc=0; pc<body->iseq_size;) {
3634 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
3639bool rb_vm_call_ivar_attrset_p(
const vm_call_handler ch);
3643clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3646 for (; v != (
VALUE)vend; v += stride) {
3647 void *ptr = asan_poisoned_object_p(v);
3648 asan_unpoison_object(v,
false);
3650 if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((
const struct rb_callcache *)v)->call_)) {
3654 asan_poison_object_if(ptr, v);
3660rb_clear_attr_ccs(
void)
3662 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3666trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3671 for (; v != (
VALUE)vend; v += stride) {
3672 void *ptr = asan_poisoned_object_p(v);
3673 asan_unpoison_object(v,
false);
3675 if (rb_obj_is_iseq(v)) {
3676 rb_iseq_trace_set(rb_iseq_check((
rb_iseq_t *)v), turnon_events);
3678 else if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((
const struct rb_callcache *)v)->call_)) {
3682 asan_poison_object_if(ptr, v);
3690 rb_objspace_each_objects(trace_set_i, &turnon_events);
3694rb_iseqw_local_variables(
VALUE iseqval)
3696 return rb_iseq_local_variables(iseqw_check(iseqval));
3717iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
3720 return rb_iseq_ibf_dump(iseqw_check(self), opt);
3737iseqw_s_load_from_binary(
VALUE self,
VALUE str)
3739 return iseqw_new(rb_iseq_ibf_load(str));
3749iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
3751 return rb_iseq_ibf_load_extra_data(str);
3754#if VM_INSN_INFO_TABLE_IMPL == 2
3787#define IMMEDIATE_TABLE_SIZE 54
3789struct succ_index_table {
3790 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
3791 struct succ_dict_block {
3793 uint64_t small_block_ranks;
3794 uint64_t bits[512/64];
3795 } succ_part[FLEX_ARY_LEN];
3798#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
3799#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
3800#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
3801#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
3803static struct succ_index_table *
3804succ_index_table_create(
int max_pos,
int *data,
int size)
3806 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3807 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3808 struct succ_index_table *sd =
3809 rb_xcalloc_mul_add_mul(
3810 imm_size,
sizeof(uint64_t),
3811 succ_size,
sizeof(
struct succ_dict_block));
3815 for (j = 0; j < imm_size; j++) {
3816 for (i = 0; i < 9; i++) {
3817 if (r < size && data[r] == j * 9 + i) r++;
3818 imm_block_rank_set(sd->imm_part[j], i, r);
3821 for (k = 0; k < succ_size; k++) {
3822 struct succ_dict_block *sd_block = &sd->succ_part[k];
3825 for (j = 0; j < 8; j++) {
3827 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
3828 for (i = 0; i < 64; i++) {
3829 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
3830 bits |= ((uint64_t)1) << i;
3834 sd_block->bits[j] = bits;
3835 small_rank += rb_popcount64(bits);
3841static unsigned int *
3842succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
3844 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3845 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3846 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
3847 int i, j, k, r = -1;
3849 for (j = 0; j < imm_size; j++) {
3850 for (i = 0; i < 9; i++) {
3851 int nr = imm_block_rank_get(sd->imm_part[j], i);
3852 if (r != nr) *p++ = j * 9 + i;
3856 for (k = 0; k < succ_size; k++) {
3857 for (j = 0; j < 8; j++) {
3858 for (i = 0; i < 64; i++) {
3859 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
3860 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
3869succ_index_lookup(
const struct succ_index_table *sd,
int x)
3871 if (x < IMMEDIATE_TABLE_SIZE) {
3872 const int i = x / 9;
3873 const int j = x % 9;
3874 return imm_block_rank_get(sd->imm_part[i], j);
3877 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
3878 const struct succ_dict_block *block = &sd->succ_part[block_index];
3879 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
3880 const int small_block_index = block_bit_index / 64;
3881 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
3882 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
3884 return block->rank + small_block_popcount + popcnt;
3903iseqw_script_lines(
VALUE self)
3905 const rb_iseq_t *iseq = iseqw_check(self);
3906 return ISEQ_BODY(iseq)->variable.script_lines;
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define RUBY_EVENT_END
Encountered an end of a class clause.
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
#define RUBY_EVENT_CLASS
Encountered a new class.
#define RUBY_EVENT_LINE
Encountered a new line.
#define RUBY_EVENT_RETURN
Encountered a return statement.
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
uint32_t rb_event_flag_t
Represents event(s).
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RB_OBJ_FREEZE
Just another name of rb_obj_freeze_inline.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_FILE
Old name of RUBY_T_FILE.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define ZALLOC
Old name of RB_ZALLOC.
#define LL2NUM
Old name of RB_LL2NUM.
#define CLASS_OF
Old name of rb_class_of.
#define T_NONE
Old name of RUBY_T_NONE.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#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 BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_class_superclass(VALUE klass)
Queries the parent of the given class.
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
VALUE rb_io_close(VALUE io)
Closes the IO.
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_resurrect(VALUE str)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define RB_NUM2INT
Just another name of rb_num2int_inline.
#define RB_INT2NUM
Just another name of rb_int2num_inline.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ZALLOC(type)
Shorthand of RB_ZALLOC_N with n=1.
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define RTEST
This is an old name of RB_TEST.
const ID * segments
A null-terminated list of ids, used to represent a constant's path idNULL is used to represent the ::...
This is the struct that holds necessary info for a struct.
struct rb_iseq_constant_body::@132 param
parameter information
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
uintptr_t VALUE
Type that represents a Ruby object.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.