12 #define RUBY_VM_INSNS_INFO 1
15 #include "ruby/internal/config.h"
21 #include "eval_intern.h"
24 #include "internal/bits.h"
25 #include "internal/class.h"
26 #include "internal/compile.h"
27 #include "internal/error.h"
28 #include "internal/file.h"
29 #include "internal/gc.h"
30 #include "internal/hash.h"
31 #include "internal/io.h"
32 #include "internal/ruby_parser.h"
33 #include "internal/sanitizers.h"
34 #include "internal/symbol.h"
35 #include "internal/thread.h"
36 #include "internal/variable.h"
41 #include "vm_callinfo.h"
46 #include "insns_info.inc"
52 #if VM_INSN_INFO_TABLE_IMPL == 2
53 static struct succ_index_table *succ_index_table_create(
int max_pos,
int *data,
int size);
54 static unsigned int *succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size);
58 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
61 obj_resurrect(
VALUE obj)
63 if (hidden_obj_p(obj)) {
72 obj = rb_hash_resurrect(obj);
97 free_arena(compile_data->node.storage_head);
98 free_arena(compile_data->insn.storage_head);
99 if (compile_data->ivar_cache_table) {
100 rb_id_table_free(compile_data->ivar_cache_table);
107 remove_from_constant_cache(
ID id,
IC ic)
111 st_data_t ic_data = (st_data_t)ic;
113 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
115 st_delete(ics, &ic_data, NULL);
117 if (ics->num_entries == 0) {
118 rb_id_table_delete(vm->constant_cache,
id);
128 iseq_clear_ic_references(
const rb_iseq_t *iseq)
134 if (!ISEQ_BODY(iseq)->is_entries) {
138 for (
unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
139 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
147 if (segments == NULL)
150 for (
int i = 0; segments[i]; i++) {
152 if (
id == idNULL)
continue;
153 remove_from_constant_cache(
id, ic);
163 RUBY_FREE_ENTER(
"iseq");
165 if (iseq && ISEQ_BODY(iseq)) {
166 iseq_clear_ic_references(iseq);
168 rb_rjit_free_iseq(iseq);
170 rb_yjit_iseq_free(iseq);
173 rb_yjit_live_iseq_count--;
178 ruby_xfree((
void *)body->insns_info.positions);
179 #if VM_INSN_INFO_TABLE_IMPL == 2
180 ruby_xfree(body->insns_info.succ_index_table);
182 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
188 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
194 if (body->
param.keyword != NULL) {
195 if (body->
param.keyword->table != &body->local_table[body->
param.keyword->bits_start - body->
param.keyword->num])
197 if (body->
param.keyword->default_values) {
202 compile_data_free(ISEQ_COMPILE_DATA(iseq));
203 if (body->outer_variables) rb_id_table_free(body->outer_variables);
207 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
208 rb_hook_list_free(iseq->aux.exec.local_hooks);
211 RUBY_FREE_LEAVE(
"iseq");
214 typedef VALUE iseq_value_itr_t(
void *ctx,
VALUE obj);
217 iseq_scan_bits(
unsigned int page, iseq_bits_t bits,
VALUE *code,
VALUE *original_iseq)
220 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
223 offset = ntz_intptr(bits);
224 VALUE op = code[page_offset + offset];
225 rb_gc_mark_and_move(&code[page_offset + offset]);
226 VALUE newop = code[page_offset + offset];
227 if (original_iseq && newop != op) {
228 original_iseq[page_offset + offset] = newop;
235 rb_iseq_mark_and_move_each_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
241 size = body->iseq_size;
242 code = body->iseq_encoded;
246 if (body->is_entries) {
248 is_entries += body->ivc_size;
251 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
256 rb_gc_mark_and_move(&icvarc->entry->class_value);
261 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
263 if (is->once.value) {
264 rb_gc_mark_and_move(&is->once.value);
269 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
270 IC ic = (
IC)is_entries;
272 rb_gc_mark_and_move_ptr(&ic->entry);
278 if (body->mark_bits.list) {
279 if (ISEQ_MBITS_BUFLEN(size) == 1) {
280 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
283 if (body->mark_bits.list) {
284 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
285 iseq_bits_t bits = body->mark_bits.list[i];
286 iseq_scan_bits(i, bits, code, original_iseq);
294 cc_is_active(
const struct rb_callcache *cc,
bool reference_updating)
297 if (cc == rb_vm_empty_cc() || rb_vm_empty_cc_for_super()) {
301 if (reference_updating) {
305 if (vm_cc_markable(cc)) {
308 if (reference_updating) {
311 if (!METHOD_ENTRY_INVALIDATED(cme)) {
321 rb_iseq_mark_and_move(
rb_iseq_t *iseq,
bool reference_updating)
323 RUBY_MARK_ENTER(
"iseq");
325 rb_gc_mark_and_move(&iseq->wrapper);
327 if (ISEQ_BODY(iseq)) {
330 rb_iseq_mark_and_move_each_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
332 rb_gc_mark_and_move(&body->variable.coverage);
333 rb_gc_mark_and_move(&body->variable.pc2branchindex);
334 rb_gc_mark_and_move(&body->variable.script_lines);
335 rb_gc_mark_and_move(&body->location.label);
336 rb_gc_mark_and_move(&body->location.base_label);
337 rb_gc_mark_and_move(&body->location.pathobj);
338 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
339 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
340 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
342 if (body->call_data) {
343 for (
unsigned int i = 0; i < body->ci_size; i++) {
346 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
348 if (cc_is_active(cds[i].cc, reference_updating)) {
349 rb_gc_mark_and_move_ptr(&cds[i].cc);
351 else if (cds[i].cc != rb_vm_empty_cc()) {
352 cds[i].cc = rb_vm_empty_cc();
357 if (body->
param.flags.has_kw && body->
param.keyword != NULL) {
358 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
360 if (keyword->default_values != NULL) {
361 for (
int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
362 rb_gc_mark_and_move(&keyword->default_values[j]);
367 if (body->catch_table) {
370 for (
unsigned int i = 0; i < table->size; i++) {
372 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
374 rb_gc_mark_and_move_ptr(&entry->iseq);
379 if (reference_updating) {
381 rb_rjit_iseq_update_references(body);
384 rb_yjit_iseq_update_references(iseq);
389 rb_rjit_iseq_mark(body->rjit_blocks);
392 rb_yjit_iseq_mark(body->yjit_payload);
398 rb_gc_mark_and_move(&iseq->aux.loader.obj);
403 if (!reference_updating) {
409 rb_iseq_mark_and_pin_insn_storage(compile_data->insn.storage_head);
412 rb_gc_mark_and_move((
VALUE *)&compile_data->err_info);
413 rb_gc_mark_and_move((
VALUE *)&compile_data->catch_table_ary);
417 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
419 if (iseq->aux.exec.local_hooks) {
420 rb_hook_list_mark_and_update(iseq->aux.exec.local_hooks);
424 RUBY_MARK_LEAVE(
"iseq");
428 param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
432 if (!pkw)
return size;
434 size +=
sizeof(
struct rb_iseq_param_keyword);
435 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
449 if (ISEQ_EXECUTABLE_P(iseq) && body) {
451 size += body->iseq_size *
sizeof(
VALUE);
453 size += body->local_table_size *
sizeof(
ID);
454 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
455 if (body->catch_table) {
456 size += iseq_catch_table_bytes(body->catch_table->size);
458 size += (body->
param.opt_num + 1) *
sizeof(
VALUE);
459 size += param_keyword_size(body->
param.keyword);
464 if (ISEQ_BODY(iseq)->is_entries) {
466 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
467 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
482 compile_data = ISEQ_COMPILE_DATA(iseq);
488 cur = compile_data->node.storage_head;
499 rb_iseq_constant_body_alloc(
void)
510 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
515 rb_iseq_pathobj_new(
VALUE path,
VALUE realpath)
521 if (path == realpath ||
523 pathobj = rb_fstring(path);
526 if (!
NIL_P(realpath)) realpath = rb_fstring(realpath);
537 rb_iseq_pathobj_new(path, realpath));
545 rb_iseq_pathobj_set(iseq, path, realpath);
548 loc->first_lineno = first_lineno;
550 if (ISEQ_BODY(iseq)->local_iseq == iseq && strcmp(
RSTRING_PTR(name),
"initialize") == 0) {
551 ISEQ_BODY(iseq)->param.flags.use_block = 1;
555 loc->node_id = node_id;
556 loc->code_location = *code_location;
559 loc->code_location.beg_pos.lineno = 0;
560 loc->code_location.beg_pos.column = 0;
561 loc->code_location.end_pos.lineno = -1;
562 loc->code_location.end_pos.column = -1;
575 if (
type == ISEQ_TYPE_TOP) {
576 body->local_iseq = iseq;
578 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
579 body->local_iseq = iseq;
582 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
586 body->parent_iseq = piseq;
589 if (
type == ISEQ_TYPE_MAIN) {
590 body->local_iseq = iseq;
599 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
604 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
612 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
619 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
623 set_relation(iseq, parent);
625 name = rb_fstring(name);
626 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
627 if (iseq != body->local_iseq) {
628 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
630 ISEQ_COVERAGE_SET(iseq,
Qnil);
631 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
632 body->variable.flip_count = 0;
634 if (
NIL_P(script_lines)) {
641 ISEQ_COMPILE_DATA_ALLOC(iseq);
642 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
645 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
646 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
647 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
648 ISEQ_COMPILE_DATA(iseq)->option = option;
649 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
650 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
652 if (option->coverage_enabled) {
653 VALUE coverages = rb_get_coverages();
654 if (
RTEST(coverages)) {
659 ISEQ_COVERAGE_SET(iseq, coverage);
660 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
666 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
667 static void validate_get_insn_info(
const rb_iseq_t *iseq);
671 rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
673 #if VM_INSN_INFO_TABLE_IMPL == 2
676 int size = body->insns_info.size;
677 int max_pos = body->iseq_size;
678 int *data = (
int *)body->insns_info.positions;
679 if (body->insns_info.succ_index_table)
ruby_xfree(body->insns_info.succ_index_table);
680 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
681 #if VM_CHECK_MODE == 0
683 body->insns_info.positions = NULL;
688 #if VM_INSN_INFO_TABLE_IMPL == 2
692 int size = body->insns_info.size;
693 int max_pos = body->iseq_size;
695 return succ_index_table_invert(max_pos, sd, size);
702 iseq->aux.exec.global_trace_events = 0;
703 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
704 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
713 VALUE err = data->err_info;
714 ISEQ_COMPILE_DATA_CLEAR(iseq);
715 compile_data_free(data);
717 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
718 validate_get_insn_info(iseq);
722 VALUE path = pathobj_path(body->location.pathobj);
728 RB_DEBUG_COUNTER_INC(iseq_num);
729 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
731 rb_iseq_init_trace(iseq);
736 .inline_const_cache = OPT_INLINE_CONST_CACHE,
737 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
738 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
739 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
740 .operands_unification = OPT_OPERANDS_UNIFICATION,
741 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
742 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
743 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
744 .coverage_enabled = TRUE,
748 .frozen_string_literal = -1,
752 rb_iseq_opt_frozen_string_literal(
void)
754 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
760 #define SET_COMPILE_OPTION(o, h, mem) \
761 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
762 if (flag == Qtrue) { (o)->mem = 1; } \
763 else if (flag == Qfalse) { (o)->mem = 0; } \
765 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
766 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
767 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
769 SET_COMPILE_OPTION(option, opt, inline_const_cache);
770 SET_COMPILE_OPTION(option, opt, peephole_optimization);
771 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
772 SET_COMPILE_OPTION(option, opt, specialized_instruction);
773 SET_COMPILE_OPTION(option, opt, operands_unification);
774 SET_COMPILE_OPTION(option, opt, instructions_unification);
775 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
776 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
777 SET_COMPILE_OPTION(option, opt, coverage_enabled);
778 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
779 #undef SET_COMPILE_OPTION
780 #undef SET_COMPILE_OPTION_NUM
786 #define SET_COMPILE_OPTION(o, a, mem) \
787 ((a)->mem < 0 ? 0 : ((o)->mem = (a)->mem > 0))
788 SET_COMPILE_OPTION(option, ast, coverage_enabled);
789 #undef SET_COMPILE_OPTION
790 if (ast->frozen_string_literal >= 0) {
791 option->frozen_string_literal = ast->frozen_string_literal;
800 *option = COMPILE_OPTION_DEFAULT;
803 *option = COMPILE_OPTION_FALSE;
805 else if (opt ==
Qtrue) {
808 ((
int *)option)[i] = 1;
811 *option = COMPILE_OPTION_DEFAULT;
812 set_compile_option_from_hash(option, opt);
822 VALUE opt = rb_hash_new_with_size(11);
823 #define SET_COMPILE_OPTION(o, h, mem) \
824 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
825 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
826 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
828 SET_COMPILE_OPTION(option, opt, inline_const_cache);
829 SET_COMPILE_OPTION(option, opt, peephole_optimization);
830 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
831 SET_COMPILE_OPTION(option, opt, specialized_instruction);
832 SET_COMPILE_OPTION(option, opt, operands_unification);
833 SET_COMPILE_OPTION(option, opt, instructions_unification);
834 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
835 SET_COMPILE_OPTION(option, opt, coverage_enabled);
836 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
838 #undef SET_COMPILE_OPTION
839 #undef SET_COMPILE_OPTION_NUM
840 VALUE frozen_string_literal = option->frozen_string_literal == -1 ?
Qnil : RBOOL(option->frozen_string_literal);
849 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
850 0,
type, &COMPILE_OPTION_DEFAULT,
855 ast_line_count(
const VALUE ast_value)
857 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
858 return ast->body.line_count;
862 iseq_setup_coverage(
VALUE coverages,
VALUE path,
int line_count)
864 if (line_count >= 0) {
865 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
867 VALUE coverage = rb_default_coverage(
len);
877 iseq_new_setup_coverage(
VALUE path,
int line_count)
879 VALUE coverages = rb_get_coverages();
881 if (
RTEST(coverages)) {
882 iseq_setup_coverage(coverages, path, line_count);
889 iseq_new_setup_coverage(path, ast_line_count(ast_value));
891 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
892 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
904 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
905 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
911 iseq_new_setup_coverage(path, ast_line_count(ast_value));
913 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
915 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
928 return pm_iseq_new_with_opt(node, rb_fstring_lit(
"<main>"),
930 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
934 rb_iseq_new_eval(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
936 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
937 VALUE coverages = rb_get_coverages();
938 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
939 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
943 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
944 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT,
950 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
952 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
953 VALUE coverages = rb_get_coverages();
954 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
955 iseq_setup_coverage(coverages, path, ((
int) (node->parser->
newline_list.
size - 1)) + first_lineno - 1);
959 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
960 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
967 VALUE v1 = iseqw_new(iseq);
969 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
979 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
983 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
985 const NODE *node = body ? body->root : 0;
990 if (!option) option = &COMPILE_OPTION_DEFAULT;
993 option = set_compile_option_from_ast(&new_opt, body);
996 if (!
NIL_P(script_lines)) {
999 else if (body && body->script_lines) {
1000 script_lines = rb_parser_build_script_lines_from(body->script_lines);
1003 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1006 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
1007 parent, isolated_depth,
type, script_lines, option);
1009 rb_iseq_compile_node(iseq, node);
1010 finish_iseq_build(iseq);
1013 return iseq_translate(iseq);
1030 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1034 ISEQ_BODY(iseq)->prism =
true;
1037 if (!option) option = &COMPILE_OPTION_DEFAULT;
1039 next_option = *option;
1040 next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
1041 option = &next_option;
1044 int32_t start_line = node->parser->
start_line;
1050 .beg_pos = { .lineno = (int) start.
line, .column = (
int) start.
column },
1051 .end_pos = { .lineno = (int) end.
line, .column = (
int) end.
column }
1054 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, -1,
1057 pm_iseq_compile_node(iseq, node);
1058 finish_iseq_build(iseq);
1060 return iseq_translate(iseq);
1064 rb_iseq_new_with_callback(
1067 int first_lineno,
const rb_iseq_t *parent,
1073 if (!option) option = &COMPILE_OPTION_DEFAULT;
1074 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1076 rb_iseq_compile_callback(iseq, ifunc);
1077 finish_iseq_build(iseq);
1083 rb_iseq_load_iseq(
VALUE fname)
1088 return iseqw_check(iseqv);
1094 #define CHECK_ARRAY(v) rb_to_array_type(v)
1095 #define CHECK_HASH(v) rb_to_hash_type(v)
1096 #define CHECK_STRING(v) rb_str_to_str(v)
1097 #define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1100 static enum rb_iseq_type
1115 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1116 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1117 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1118 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1119 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1120 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1121 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1122 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1123 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1124 return (
enum rb_iseq_type)-1;
1132 VALUE magic, version1, version2, format_type, misc;
1133 VALUE name, path, realpath, code_location, node_id;
1134 VALUE type, body, locals, params, exception;
1136 st_data_t iseq_type;
1146 data = CHECK_ARRAY(data);
1153 ((void)magic, (
void)version1, (void)version2, (
void)format_type);
1158 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1167 ISEQ_BODY(iseq)->local_iseq = iseq;
1169 iseq_type = iseq_type_from_sym(
type);
1170 if (iseq_type == (
enum rb_iseq_type)-1) {
1185 ISEQ_BODY(iseq)->prism =
true;
1188 make_compile_option(&option, opt);
1189 option.peephole_optimization = FALSE;
1190 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1191 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1193 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1195 finish_iseq_build(iseq);
1197 return iseqw_new(iseq);
1204 iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1208 return iseq_load(data, NULL, opt);
1214 return iseq_load(data,
RTEST(parent) ? (
rb_iseq_t *)parent : NULL, opt);
1222 #if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1223 # define INITIALIZED volatile
1225 # define INITIALIZED
1229 VALUE INITIALIZED ast_value;
1231 VALUE name = rb_fstring_lit(
"<compiled>");
1234 make_compile_option(&option, opt);
1238 parse = rb_parser_compile_file_path;
1241 parse = rb_parser_compile_string_path;
1245 const VALUE parser = rb_parser_new();
1246 const rb_iseq_t *outer_scope = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1248 rb_parser_set_context(parser, outer_scope, FALSE);
1249 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1251 ast_value = (*parse)(parser, file, src, ln);
1254 ast = rb_ruby_ast_data_get(ast_value);
1256 if (!ast || !ast->body.root) {
1257 rb_ast_dispose(ast);
1261 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1262 NULL, 0, ISEQ_TYPE_TOP, &option,
1264 rb_ast_dispose(ast);
1276 VALUE name = rb_fstring_lit(
"<compiled>");
1279 make_compile_option(&option, opt);
1286 result.
node.coverage_enabled = 1;
1288 switch (option.frozen_string_literal) {
1289 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1291 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1294 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1298 rb_bug(
"pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1307 error = pm_load_parse_file(&result, filepath, ruby_vm_keep_script_lines ? &script_lines : NULL);
1312 error = pm_parse_string(&result, src, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1315 if (error ==
Qnil) {
1316 iseq = pm_iseq_new_with_opt(&result.
node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option);
1317 pm_parse_result_free(&result);
1320 pm_parse_result_free(&result);
1330 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1336 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1340 rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1342 return rb_iseq_realpath(iseq);
1346 rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1348 return NIL_P(rb_iseq_realpath(iseq));
1354 return ISEQ_BODY(iseq)->location.label;
1358 rb_iseq_base_label(
const rb_iseq_t *iseq)
1360 return ISEQ_BODY(iseq)->location.base_label;
1364 rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1366 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1370 rb_iseq_method_name(
const rb_iseq_t *iseq)
1374 if (body->type == ISEQ_TYPE_METHOD) {
1375 return body->location.base_label;
1383 rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1386 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1387 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1388 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1389 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1392 static ID iseq_type_id(
enum rb_iseq_type
type);
1397 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1403 return ISEQ_COVERAGE(iseq);
1407 remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1410 for (; v != (
VALUE)vend; v += stride) {
1411 void *
ptr = asan_poisoned_object_p(v);
1412 asan_unpoison_object(v,
false);
1414 if (rb_obj_is_iseq(v)) {
1416 ISEQ_COVERAGE_SET(iseq,
Qnil);
1419 asan_poison_object_if(
ptr, v);
1425 rb_iseq_remove_coverage_all(
void)
1427 rb_objspace_each_objects(remove_coverage_i, NULL);
1433 iseqw_mark(
void *
ptr)
1439 iseqw_memsize(
const void *
ptr)
1445 iseqw_ref_update(
void *
ptr)
1459 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1465 if (iseq->wrapper) {
1468 iseq->wrapper, (
void *)iseq);
1470 return iseq->wrapper;
1488 return iseqw_new(iseq);
1497 iseqw_s_compile_parser(
int argc,
VALUE *argv,
VALUE self,
bool prism)
1503 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1505 case 5: opt = argv[--i];
1506 case 4: line = argv[--i];
1507 case 3: path = argv[--i];
1508 case 2: file = argv[--i];
1511 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1512 if (
NIL_P(path)) path = file;
1520 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1523 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1526 return iseqw_new(iseq);
1567 iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1569 return iseqw_s_compile_parser(argc, argv,
self, rb_ruby_prism_p());
1609 iseqw_s_compile_parsey(
int argc,
VALUE *argv,
VALUE self)
1611 return iseqw_s_compile_parser(argc, argv,
self,
false);
1651 iseqw_s_compile_prism(
int argc,
VALUE *argv,
VALUE self)
1653 return iseqw_s_compile_parser(argc, argv,
self,
true);
1677 iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1686 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1687 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1689 case 2: opt = argv[--i];
1692 file = rb_fstring(file);
1697 VALUE v = rb_vm_push_frame_fname(ec, file);
1699 parser = rb_parser_new();
1700 rb_parser_set_context(parser, NULL, FALSE);
1701 ast_value = rb_parser_load_file(parser, file);
1702 ast = rb_ruby_ast_data_get(ast_value);
1703 if (!ast->body.root) exc = GET_EC()->errinfo;
1706 if (!ast->body.root) {
1707 rb_ast_dispose(ast);
1711 make_compile_option(&option, opt);
1713 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
1715 rb_realpath_internal(
Qnil, file, 1),
1716 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1718 rb_ast_dispose(ast);
1720 rb_vm_pop_frame(ec);
1747 iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self)
1753 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1754 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1756 case 2: opt = argv[--i];
1759 file = rb_fstring(file);
1762 VALUE v = rb_vm_push_frame_fname(ec, file);
1766 result.
node.coverage_enabled = 1;
1769 VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1771 if (error ==
Qnil) {
1772 make_compile_option(&option, opt);
1774 ret = iseqw_new(pm_iseq_new_with_opt(&result.
node, rb_fstring_lit(
"<main>"),
1776 rb_realpath_internal(
Qnil, file, 1),
1777 1, NULL, 0, ISEQ_TYPE_TOP, &option));
1778 pm_parse_result_free(&result);
1779 rb_vm_pop_frame(ec);
1783 pm_parse_result_free(&result);
1784 rb_vm_pop_frame(ec);
1821 iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
1824 make_compile_option(&option, opt);
1825 COMPILE_OPTION_DEFAULT = option;
1838 iseqw_s_compile_option_get(
VALUE self)
1840 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1844 iseqw_check(
VALUE iseqw)
1850 if (!ISEQ_BODY(iseq)) {
1851 rb_ibf_load_iseq_complete(iseq);
1854 if (!ISEQ_BODY(iseq)->location.label) {
1861 rb_iseqw_to_iseq(
VALUE iseqw)
1863 return iseqw_check(iseqw);
1875 iseqw_eval(
VALUE self)
1877 return rb_iseq_eval(iseqw_check(
self));
1885 iseqw_inspect(
VALUE self)
1887 const rb_iseq_t *iseq = iseqw_check(
self);
1891 if (!body->location.label) {
1892 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
1895 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
1897 body->location.label, rb_iseq_path(iseq),
1898 FIX2INT(rb_iseq_first_lineno(iseq)));
1926 iseqw_path(
VALUE self)
1928 return rb_iseq_path(iseqw_check(
self));
1948 iseqw_absolute_path(
VALUE self)
1950 return rb_iseq_realpath(iseqw_check(
self));
1977 iseqw_label(
VALUE self)
1979 return rb_iseq_label(iseqw_check(
self));
2003 iseqw_base_label(
VALUE self)
2005 return rb_iseq_base_label(iseqw_check(
self));
2019 iseqw_first_lineno(
VALUE self)
2021 return rb_iseq_first_lineno(iseqw_check(
self));
2108 iseqw_to_a(
VALUE self)
2110 const rb_iseq_t *iseq = iseqw_check(
self);
2111 return iseq_data_to_ary(iseq);
2114 #if VM_INSN_INFO_TABLE_IMPL == 1
2116 get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
2119 size_t size = body->insns_info.size;
2121 const unsigned int *positions = body->insns_info.positions;
2122 const int debug = 0;
2125 printf(
"size: %"PRIuSIZE
"\n", size);
2126 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2127 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2133 else if (size == 1) {
2134 return &insns_info[0];
2137 size_t l = 1, r = size - 1;
2139 size_t m = l + (r - l) / 2;
2140 if (positions[m] == pos) {
2141 return &insns_info[m];
2143 if (positions[m] < pos) {
2151 return &insns_info[size-1];
2153 if (positions[l] > pos) {
2154 return &insns_info[l-1];
2156 return &insns_info[l];
2161 get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2163 return get_insn_info_binary_search(iseq, pos);
2167 #if VM_INSN_INFO_TABLE_IMPL == 2
2169 get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
2172 size_t size = body->insns_info.size;
2174 const int debug = 0;
2177 #if VM_CHECK_MODE > 0
2178 const unsigned int *positions = body->insns_info.positions;
2179 printf(
"size: %"PRIuSIZE
"\n", size);
2180 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2181 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2183 printf(
"size: %"PRIuSIZE
"\n", size);
2184 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
2185 (
size_t)0, insns_info[0].line_no, pos);
2192 else if (size == 1) {
2193 return &insns_info[0];
2197 VM_ASSERT(body->insns_info.succ_index_table != NULL);
2198 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
2199 return &insns_info[index-1];
2204 get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2206 return get_insn_info_succinct_bitvector(iseq, pos);
2210 #if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2212 get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
2215 size_t i = 0, size = body->insns_info.size;
2217 const unsigned int *positions = body->insns_info.positions;
2218 const int debug = 0;
2221 printf(
"size: %"PRIuSIZE
"\n", size);
2222 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2223 i, positions[i], insns_info[i].line_no, pos);
2229 else if (size == 1) {
2230 return &insns_info[0];
2233 for (i=1; i<size; i++) {
2234 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2235 i, positions[i], insns_info[i].line_no, pos);
2237 if (positions[i] == pos) {
2238 return &insns_info[i];
2240 if (positions[i] > pos) {
2241 return &insns_info[i-1];
2245 return &insns_info[i-1];
2249 #if VM_INSN_INFO_TABLE_IMPL == 0
2251 get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2253 return get_insn_info_linear_search(iseq, pos);
2257 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2259 validate_get_insn_info(
const rb_iseq_t *iseq)
2263 for (i = 0; i < body->iseq_size; i++) {
2264 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2265 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
2272 rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
2277 return entry->line_no;
2284 #ifdef USE_ISEQ_NODE_ID
2286 rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
2291 return entry->node_id;
2300 rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2304 return entry->events;
2316 entry->events &= ~reset;
2317 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2318 void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2319 rb_iseq_trace_flag_cleared(iseq, pos);
2332 for (i = 0; i < level; i++) {
2333 diseq = ISEQ_BODY(diseq)->parent_iseq;
2335 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2336 lid = ISEQ_BODY(diseq)->local_table[idx];
2351 int rb_insn_unified_local_var_level(
VALUE);
2355 rb_insn_operand_intern(
const rb_iseq_t *iseq,
2359 const char *types = insn_op_types(insn);
2360 char type = types[op_no];
2369 if (insn == BIN(defined) && op_no == 0) {
2370 enum defined_type deftype = (
enum defined_type)op;
2373 ret = rb_fstring_lit(
"func");
2376 ret = rb_fstring_lit(
"ref");
2378 case DEFINED_CONST_FROM:
2379 ret = rb_fstring_lit(
"constant-from");
2382 ret = rb_iseq_defined_string(deftype);
2387 else if (insn == BIN(checktype) && op_no == 0) {
2398 if (types[op_no+1] == TS_NUM && pnop) {
2399 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2401 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2402 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2414 op = obj_resurrect(op);
2415 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2428 ret = rb_dump_literal(op);
2440 ret = ISEQ_BODY(iseq)->location.label;
2454 const ID *segments = ((
IC)op)->segments;
2473 ID mid = vm_ci_mid(ci);
2481 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2487 if (vm_ci_flag(ci)) {
2489 # define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2490 CALL_FLAG(ARGS_SPLAT);
2491 CALL_FLAG(ARGS_SPLAT_MUT);
2492 CALL_FLAG(ARGS_BLOCKARG);
2495 CALL_FLAG(ARGS_SIMPLE);
2496 CALL_FLAG(TAILCALL);
2500 CALL_FLAG(KW_SPLAT);
2501 CALL_FLAG(KW_SPLAT_MUT);
2502 CALL_FLAG(FORWARDING);
2503 CALL_FLAG(OPT_SEND);
2519 if (dladdr((
void *)op, &info) && info.dli_sname) {
2532 bf->name, bf->argc);
2543 right_strip(
VALUE str)
2546 while (end-- > beg && *end ==
' ');
2556 rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2559 VALUE insn = code[pos];
2560 int len = insn_len(insn);
2562 const char *types = insn_op_types(insn);
2564 const char *insn_name_buff;
2566 insn_name_buff = insn_name(insn);
2568 extern const int rb_vm_max_insn_name_size;
2569 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2573 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2576 for (j = 0; types[j]; j++) {
2577 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2578 len, pos, &code[pos + j + 2],
2588 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2589 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2590 if (line_no && line_no != prev) {
2592 slen = (slen > 70) ? 0 : (70 - slen);
2593 str =
rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2600 str =
rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s%s]",
2611 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2612 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2628 catch_type(
int type)
2631 case CATCH_TYPE_RESCUE:
2633 case CATCH_TYPE_ENSURE:
2635 case CATCH_TYPE_RETRY:
2637 case CATCH_TYPE_BREAK:
2639 case CATCH_TYPE_REDO:
2641 case CATCH_TYPE_NEXT:
2653 if (!body->location.label) {
2658 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2659 body->location.label, rb_iseq_path(iseq),
2660 loc->beg_pos.lineno,
2661 loc->beg_pos.lineno,
2662 loc->beg_pos.column,
2663 loc->end_pos.lineno,
2664 loc->end_pos.column);
2670 {(void (*)(
void *))
rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2671 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2685 enum {header_minlen = 72};
2688 const char *indent_str;
2691 size = body->iseq_size;
2700 if ((l =
RSTRING_LEN(str) - indent_len) < header_minlen) {
2704 if (iseq->body->builtin_attrs) {
2705 #define disasm_builtin_attr(str, iseq, attr) \
2706 if (iseq->body->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
2707 rb_str_cat2(str, " " #attr); \
2709 disasm_builtin_attr(str, iseq, LEAF);
2710 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
2711 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
2716 if (body->catch_table) {
2720 if (body->catch_table) {
2723 for (i = 0; i < body->catch_table->size; i++) {
2725 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2728 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2729 catch_type((
int)entry->type), (
int)entry->start,
2730 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2731 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2732 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2734 done_iseq = st_init_numtable();
2737 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2744 if (body->catch_table) {
2746 rb_str_cat2(str,
"|-------------------------------------"
2747 "-----------------------------------\n");
2751 if (body->local_table) {
2752 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
2755 "local table (size: %d, argc: %d "
2756 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2757 body->local_table_size,
2758 body->
param.lead_num,
2759 body->
param.opt_num,
2760 body->
param.flags.has_rest ? body->
param.rest_start : -1,
2761 body->
param.post_num,
2762 body->
param.flags.has_block ? body->
param.block_start : -1,
2763 body->
param.flags.has_kw ? keyword->num : -1,
2764 body->
param.flags.has_kw ? keyword->required_num : -1,
2765 body->
param.flags.has_kwrest ? keyword->rest_start : -1);
2767 for (i = body->local_table_size; i > 0;) {
2768 int li = body->local_table_size - --i - 1;
2770 VALUE name = local_var_name(iseq, 0, i);
2775 if (body->
param.flags.has_opt) {
2776 int argc = body->
param.lead_num;
2777 int opts = body->
param.opt_num;
2778 if (li >= argc && li < argc + opts) {
2779 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2780 body->
param.opt_table[li - argc]);
2784 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2785 (body->
param.lead_num > li) ? (body->
param.flags.ambiguous_param0 ?
"AmbiguousArg" :
"Arg") :
"",
2787 (body->
param.flags.has_rest && body->
param.rest_start == li) ? (body->
param.flags.anon_rest ?
"AnonRest" :
"Rest") :
"",
2788 (body->
param.flags.has_post && body->
param.post_start <= li && li < body->param.post_start + body->
param.post_num) ?
"Post" :
"",
2789 (body->
param.flags.has_kwrest && keyword->rest_start == li) ? (body->
param.flags.anon_kwrest ?
"AnonKwrest" :
"Kwrest") :
"",
2790 (body->
param.flags.has_block && body->
param.block_start == li) ?
"Block" :
"");
2803 code = rb_iseq_original_iseq(iseq);
2804 for (n = 0; n < size;) {
2806 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2811 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2835 rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
2837 struct rb_id_table * iv_names = rb_id_table_create(0);
2839 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2840 IVC cache = (
IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2842 if (cache->iv_set_name) {
2843 rb_id_table_insert(iv_names, cache->iv_set_name,
Qtrue);
2847 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2850 count += RCLASS_EXT(superclass)->max_iv_count;
2852 rb_id_table_free(iv_names);
2876 iseqw_disasm(
VALUE self)
2878 return rb_iseq_disasm(iseqw_check(
self));
2882 iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
2885 VALUE *code = rb_iseq_original_iseq(iseq);
2890 if (body->catch_table) {
2891 for (i = 0; i < body->catch_table->size; i++) {
2893 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2894 child = entry->iseq;
2898 (*iter_func)(child, data);
2904 for (i=0; i<body->iseq_size;) {
2905 VALUE insn = code[i];
2906 int len = insn_len(insn);
2907 const char *types = insn_op_types(insn);
2910 for (j=0; types[j]; j++) {
2917 (*iter_func)(child, data);
2932 yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
2946 iseqw_each_child(
VALUE self)
2948 const rb_iseq_t *iseq = iseqw_check(
self);
2949 iseq_iterate_children(iseq, yield_each_children, NULL);
2956 #define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2976 iseqw_trace_points(
VALUE self)
2978 const rb_iseq_t *iseq = iseqw_check(
self);
2983 for (i=0; i<body->insns_info.size; i++) {
2985 if (entry->events) {
2986 push_event_info(iseq, entry->events, entry->line_no, ary);
3032 if (rb_frame_info_p(body)) {
3033 iseq = rb_get_iseq_from_frame_info(body);
3036 iseq = vm_proc_iseq(body);
3038 if (!rb_obj_is_iseq((
VALUE)iseq)) {
3043 iseq = rb_method_iseq(body);
3045 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
3049 return iseq ? iseqw_new(iseq) :
Qnil;
3107 VALUE iseqw = iseqw_s_of(klass, body);
3108 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3112 register_label(
struct st_table *table,
unsigned long idx)
3115 st_insert(table, idx, sym);
3124 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
3125 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
3126 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
3127 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
3128 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
3129 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
3131 rb_bug(
"unknown exception type: %d", (
int)
type);
3146 {(void (*)(
void *))
rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
3147 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3150 #define DECL_ID(name) \
3153 #define INIT_ID(name) \
3154 id_##name = rb_intern(#name)
3157 iseq_type_id(
enum rb_iseq_type
type)
3182 case ISEQ_TYPE_TOP:
return id_top;
3183 case ISEQ_TYPE_METHOD:
return id_method;
3184 case ISEQ_TYPE_BLOCK:
return id_block;
3185 case ISEQ_TYPE_CLASS:
return id_class;
3186 case ISEQ_TYPE_RESCUE:
return id_rescue;
3187 case ISEQ_TYPE_ENSURE:
return id_ensure;
3188 case ISEQ_TYPE_EVAL:
return id_eval;
3189 case ISEQ_TYPE_MAIN:
return id_main;
3190 case ISEQ_TYPE_PLAIN:
return id_plain;
3193 rb_bug(
"unsupported iseq type: %d", (
int)
type);
3205 VALUE *seq, *iseq_original;
3216 static ID insn_syms[VM_INSTRUCTION_SIZE/2];
3217 struct st_table *labels_table = st_init_numtable();
3220 if (insn_syms[0] == 0) {
3222 for (i=0; i<numberof(insn_syms); i++) {
3228 type = iseq_type_id(iseq_body->type);
3231 for (i=0; i<iseq_body->local_table_size; i++) {
3232 ID lid = iseq_body->local_table[i];
3248 const struct rb_iseq_param_keyword *
const keyword = iseq_body->
param.keyword;
3251 if (iseq_body->
param.flags.has_opt) {
3252 int len = iseq_body->
param.opt_num + 1;
3255 for (j = 0; j <
len; j++) {
3256 VALUE l = register_label(labels_table, iseq_body->
param.opt_table[j]);
3268 if (iseq_body->
param.flags.has_kw) {
3271 for (i=0; i<keyword->required_num; i++) {
3274 for (j=0; i<keyword->num; i++, j++) {
3276 if (!UNDEF_P(keyword->default_values[j])) {
3283 INT2FIX(keyword->bits_start));
3292 iseq_original = rb_iseq_original_iseq((
rb_iseq_t *)iseq);
3294 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3295 VALUE insn = *seq++;
3296 int j,
len = insn_len(insn);
3301 for (j=0; j<
len-1; j++, seq++) {
3302 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3306 unsigned long idx = nseq - iseq_original + *seq;
3307 rb_ary_push(ary, register_label(labels_table, idx));
3321 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3332 const ID *ids = ((
IC)*seq)->segments;
3352 int argc = vm_ci_argc(ci);
3354 ID mid = vm_ci_mid(ci);
3358 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3363 argc -= kwarg->keyword_len;
3364 for (i = 0; i < kwarg->keyword_len; i++) {
3388 unsigned long idx = nseq - iseq_original + pos;
3391 register_label(labels_table, idx));
3398 #if SIZEOF_VALUE <= SIZEOF_LONG
3409 #if SIZEOF_VALUE <= SIZEOF_LONG
3422 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3431 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3434 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3435 rb_ary_push(ary, exception_type2symbol(entry->type));
3437 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3442 rb_ary_push(ary, register_label(labels_table, entry->start));
3443 rb_ary_push(ary, register_label(labels_table, entry->end));
3444 rb_ary_push(ary, register_label(labels_table, entry->cont));
3451 prev_insn_info = NULL;
3452 #ifdef USE_ISEQ_NODE_ID
3461 if (st_lookup(labels_table, pos, &label)) {
3465 info = get_insn_info(iseq, pos);
3466 #ifdef USE_ISEQ_NODE_ID
3470 if (prev_insn_info != info) {
3471 int line = info->line_no;
3474 if (line > 0 && last_line != line) {
3478 #define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3488 prev_insn_info = info;
3503 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3504 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3505 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3506 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3507 #ifdef USE_ISEQ_NODE_ID
3535 rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3539 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
3541 ID req, opt, rest, block, key, keyrest;
3542 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3543 #define PARAM_ID(i) body->local_table[(i)]
3544 #define PARAM(i, type) ( \
3546 rb_id2str(PARAM_ID(i)) ? \
3547 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3553 if (body->
param.flags.forwardable) {
3564 for (i = 0; i < body->
param.lead_num; i++) {
3571 for (i = 0; i < body->
param.lead_num; i++) {
3575 r = body->
param.lead_num + body->
param.opt_num;
3576 for (; i < r; i++) {
3583 if (body->
param.flags.has_rest) {
3587 r = body->
param.post_start + body->
param.post_num;
3589 for (i = body->
param.post_start; i < r; i++) {
3596 for (i = body->
param.post_start; i < r; i++) {
3600 if (body->
param.flags.accepts_no_kwarg) {
3606 if (body->
param.flags.has_kw) {
3608 if (keyword->required_num > 0) {
3611 for (; i < keyword->required_num; i++) {
3620 for (; i < keyword->num; i++) {
3628 if (body->
param.flags.has_kwrest || body->
param.flags.ruby2_keywords) {
3631 PARAM_TYPE(keyrest);
3632 if (body->
param.flags.has_kwrest &&
3633 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3636 else if (body->
param.flags.ruby2_keywords) {
3641 if (body->
param.flags.has_block) {
3649 rb_iseq_defined_string(
enum defined_type
type)
3651 static const char expr_names[][18] = {
3653 "instance-variable",
3669 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names))
rb_bug(
"unknown defined type %d",
type);
3670 estr = expr_names[
type - 1];
3671 return rb_fstring_cstr(estr);
3677 static st_table *encoded_insn_data;
3681 void *notrace_encoded_insn;
3682 void *trace_encoded_insn;
3684 static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3687 rb_free_encoded_insn_data(
void)
3689 st_free_table(encoded_insn_data);
3693 rb_vm_encoded_insn_data_table_init(
void)
3695 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3696 const void *
const *table = rb_vm_get_insns_address_table();
3697 #define INSN_CODE(insn) ((VALUE)table[insn])
3699 #define INSN_CODE(insn) (insn)
3702 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3704 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3705 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3706 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3708 insn_data[insn].insn = (int)insn;
3709 insn_data[insn].insn_len = insn_len(insn);
3711 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3712 insn_data[insn].notrace_encoded_insn = (
void *) key1;
3713 insn_data[insn].trace_encoded_insn = (
void *) key2;
3716 insn_data[insn].notrace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3717 insn_data[insn].trace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3720 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3721 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3726 rb_vm_insn_addr2insn(
const void *addr)
3728 st_data_t key = (st_data_t)addr;
3731 if (st_lookup(encoded_insn_data, key, &val)) {
3733 return (
int)e->insn;
3736 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3741 rb_vm_insn_addr2opcode(
const void *addr)
3743 st_data_t key = (st_data_t)addr;
3746 if (st_lookup(encoded_insn_data, key, &val)) {
3748 int opcode = e->insn;
3749 if (addr == e->trace_encoded_insn) {
3750 opcode += VM_INSTRUCTION_SIZE/2;
3755 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3760 rb_vm_insn_decode(
const VALUE encoded)
3762 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3763 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3765 int insn = (int)encoded;
3771 encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_current_trace)
3773 st_data_t key = (st_data_t)*iseq_encoded_insn;
3776 if (st_lookup(encoded_insn_data, key, &val)) {
3778 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3781 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3785 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3789 rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3792 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3793 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
3819 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3821 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3823 for (pc=0; pc<body->iseq_size;) {
3827 unsigned int line = (int)entry->line_no;
3829 if (target_line == 0 || target_line == line) {
3836 if (pc_events & target_events) {
3839 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
3843 if (iseq->aux.exec.local_hooks == NULL) {
3845 iseq->aux.exec.local_hooks->is_local =
true;
3847 rb_hook_list_connect_tracepoint((
VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3856 unsigned int target_line;
3861 iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3864 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3865 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3869 rb_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)
3872 if (target_bmethod) {
3873 turnon_events = add_bmethod_events(turnon_events);
3875 data.turnon_events = turnon_events;
3877 data.target_line = target_line;
3880 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
3890 if (iseq->aux.exec.local_hooks) {
3893 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3896 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3897 local_events = iseq->aux.exec.local_hooks->events;
3899 if (local_events == 0) {
3900 rb_hook_list_free(iseq->aux.exec.local_hooks);
3901 ((
rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3904 local_events = add_bmethod_events(local_events);
3905 for (pc = 0; pc<body->iseq_size;) {
3907 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
3919 iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3922 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3923 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3927 rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval)
3933 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
3940 if (iseq->aux.exec.global_trace_events == turnon_events) {
3944 if (!ISEQ_EXECUTABLE_P(iseq)) {
3951 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3953 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3954 ((
rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3955 enabled_events = add_bmethod_events(turnon_events | local_events);
3957 for (pc=0; pc<body->iseq_size;) {
3959 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
3967 clear_attr_cc(
VALUE v)
3969 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((
const struct rb_callcache *)v)) {
3979 clear_bf_cc(
VALUE v)
3981 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((
const struct rb_callcache *)v)) {
3991 clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3994 for (; v != (
VALUE)vend; v += stride) {
3995 void *
ptr = asan_poisoned_object_p(v);
3996 asan_unpoison_object(v,
false);
3998 asan_poison_object_if(
ptr, v);
4004 rb_clear_attr_ccs(
void)
4006 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
4010 clear_bf_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4013 for (; v != (
VALUE)vend; v += stride) {
4014 void *
ptr = asan_poisoned_object_p(v);
4015 asan_unpoison_object(v,
false);
4017 asan_poison_object_if(
ptr, v);
4023 rb_clear_bf_ccs(
void)
4025 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
4029 trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4034 for (; v != (
VALUE)vend; v += stride) {
4035 void *
ptr = asan_poisoned_object_p(v);
4036 asan_unpoison_object(v,
false);
4038 if (rb_obj_is_iseq(v)) {
4039 rb_iseq_trace_set(rb_iseq_check((
rb_iseq_t *)v), turnon_events);
4041 else if (clear_attr_cc(v)) {
4043 else if (clear_bf_cc(v)) {
4046 asan_poison_object_if(
ptr, v);
4054 rb_objspace_each_objects(trace_set_i, &turnon_events);
4058 rb_iseqw_local_variables(
VALUE iseqval)
4060 return rb_iseq_local_variables(iseqw_check(iseqval));
4081 iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
4084 return rb_iseq_ibf_dump(iseqw_check(
self), opt);
4101 iseqw_s_load_from_binary(
VALUE self,
VALUE str)
4103 return iseqw_new(rb_iseq_ibf_load(str));
4113 iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
4115 return rb_iseq_ibf_load_extra_data(str);
4118 #if VM_INSN_INFO_TABLE_IMPL == 2
4151 #define IMMEDIATE_TABLE_SIZE 54
4154 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4157 uint64_t small_block_ranks;
4158 uint64_t bits[512/64];
4159 } succ_part[FLEX_ARY_LEN];
4162 #define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4163 #define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4164 #define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4165 #define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4168 succ_index_table_create(
int max_pos,
int *data,
int size)
4170 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4171 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4173 rb_xcalloc_mul_add_mul(
4174 imm_size,
sizeof(uint64_t),
4175 succ_size,
sizeof(
struct succ_dict_block));
4179 for (j = 0; j < imm_size; j++) {
4180 for (i = 0; i < 9; i++) {
4181 if (r < size && data[r] == j * 9 + i) r++;
4182 imm_block_rank_set(sd->imm_part[j], i, r);
4185 for (k = 0; k < succ_size; k++) {
4186 struct succ_dict_block *sd_block = &sd->succ_part[k];
4189 for (j = 0; j < 8; j++) {
4191 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4192 for (i = 0; i < 64; i++) {
4193 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4194 bits |= ((uint64_t)1) << i;
4198 sd_block->bits[j] = bits;
4199 small_rank += rb_popcount64(bits);
4205 static unsigned int *
4206 succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
4208 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4209 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4210 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
4211 int i, j, k, r = -1;
4213 for (j = 0; j < imm_size; j++) {
4214 for (i = 0; i < 9; i++) {
4215 int nr = imm_block_rank_get(sd->imm_part[j], i);
4216 if (r != nr) *p++ = j * 9 + i;
4220 for (k = 0; k < succ_size; k++) {
4221 for (j = 0; j < 8; j++) {
4222 for (i = 0; i < 64; i++) {
4223 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4224 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4235 if (x < IMMEDIATE_TABLE_SIZE) {
4236 const int i = x / 9;
4237 const int j = x % 9;
4238 return imm_block_rank_get(sd->imm_part[i], j);
4241 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4242 const struct succ_dict_block *block = &sd->succ_part[block_index];
4243 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4244 const int small_block_index = block_bit_index / 64;
4245 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4246 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4248 return block->rank + small_block_popcount + popcnt;
4267 iseqw_script_lines(
VALUE self)
4269 const rb_iseq_t *iseq = iseqw_check(
self);
4270 return ISEQ_BODY(iseq)->variable.script_lines;
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
#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 RUBY_EVENT_RESCUE
Encountered a rescue statement.
#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.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#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 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 SYM2ID
Old name of RB_SYM2ID.
#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_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
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.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(long capa)
Allocates a hidden (no class) empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_freeze(VALUE obj)
Freeze an array, preventing further modifications.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
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.
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
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_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_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
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.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), 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.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), 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.
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
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.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
char * ptr
Pointer to the underlying memory region, of at least capa bytes.
VALUE rb_io_path(VALUE io)
Returns the path for the given IO.
int len
Length of the buffer.
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.
PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line)
Set the line option on the given options struct.
PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal)
Set the frozen string literal option on the given options struct.
PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count)
Allocate and zero out the scopes array on the given options struct.
pm_line_column_t pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line)
Returns the line and column of the given offset.
#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 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 char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#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 ::...
A line and column in a string.
uint32_t column
The column number.
int32_t line
The line number.
This represents a range of bytes in the source string to which a node or token corresponds.
const uint8_t * start
A pointer to the start location of the range in the source.
const uint8_t * end
A pointer to the end location of the range in the source.
size_t size
The number of offsets in the list.
pm_location_t location
This is the location of the node in the source.
int32_t line
The line within the file that the parse starts on.
pm_scope_node_t node
The resulting scope node that will hold the generated AST.
pm_options_t options
The options that will be passed to the parser.
int32_t start_line
The line number at the start of the parse.
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
This is the struct that holds necessary info for a struct.
struct rb_iseq_constant_body::@150 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.
void ruby_xfree(void *ptr)
Deallocates a storage instance.