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/gc.h"
31#include "internal/hash.h"
32#include "internal/io.h"
33#include "internal/ruby_parser.h"
34#include "internal/sanitizers.h"
35#include "internal/set_table.h"
36#include "internal/symbol.h"
37#include "internal/thread.h"
38#include "internal/variable.h"
42#include "ractor_core.h"
43#include "vm_callinfo.h"
48#include "insns_info.inc"
52static VALUE iseqw_new(
const rb_iseq_t *iseq);
53static const rb_iseq_t *iseqw_check(
VALUE iseqw);
55#if VM_INSN_INFO_TABLE_IMPL == 2
56static struct succ_index_table *succ_index_table_create(
int max_pos,
int *data,
int size);
57static unsigned int *succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size);
58static int succ_index_lookup(
const struct succ_index_table *sd,
int x);
61#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
64obj_resurrect(
VALUE obj)
66 if (hidden_obj_p(obj)) {
75 obj = rb_hash_resurrect(obj);
100 free_arena(compile_data->node.storage_head);
101 free_arena(compile_data->insn.storage_head);
102 if (compile_data->ivar_cache_table) {
103 rb_id_table_free(compile_data->ivar_cache_table);
105 ruby_xfree(compile_data);
110remove_from_constant_cache(
ID id, IC ic)
112 rb_vm_t *vm = GET_VM();
114 st_data_t ic_data = (st_data_t)ic;
116 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
118 set_table_delete(ics, &ic_data);
120 if (ics->num_entries == 0 &&
122 id != vm->inserting_constant_cache_id) {
123 rb_id_table_delete(vm->constant_cache,
id);
133iseq_clear_ic_references(
const rb_iseq_t *iseq)
139 if (!ISEQ_BODY(iseq)->is_entries) {
143 for (
unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
144 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
152 if (segments == NULL)
155 for (
int i = 0; segments[i]; i++) {
157 if (
id == idNULL)
continue;
158 remove_from_constant_cache(
id, ic);
161 ruby_xfree((
void *)segments);
167rb_iseq_local_hooks(
const rb_iseq_t *iseq, rb_ractor_t *r,
bool create)
169 rb_hook_list_t *hook_list = NULL;
171 if (st_lookup(rb_ractor_targeted_hooks(r), (st_data_t)iseq, &val)) {
172 hook_list = (rb_hook_list_t*)val;
173 RUBY_ASSERT(hook_list->type == hook_list_type_targeted_iseq);
177 hook_list->type = hook_list_type_targeted_iseq;
178 st_insert(rb_ractor_targeted_hooks(r), (st_data_t)iseq, (st_data_t)hook_list);
184rb_iseq_free(
const rb_iseq_t *iseq)
186 RUBY_FREE_ENTER(
"iseq");
188 if (iseq && ISEQ_BODY(iseq)) {
189 iseq_clear_ic_references(iseq);
192 rb_yjit_iseq_free(iseq);
195 rb_yjit_live_iseq_count--;
199 rb_zjit_iseq_free(iseq);
201 ruby_xfree((
void *)body->iseq_encoded);
202 ruby_xfree((
void *)body->insns_info.body);
203 ruby_xfree((
void *)body->insns_info.positions);
204#if VM_INSN_INFO_TABLE_IMPL == 2
205 ruby_xfree(body->insns_info.succ_index_table);
207 ruby_xfree((
void *)body->is_entries);
208 ruby_xfree(body->call_data);
209 ruby_xfree((
void *)body->catch_table);
210 ruby_xfree((
void *)body->param.opt_table);
211 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
212 ruby_xfree((
void *)body->mark_bits.list);
215 ruby_xfree(body->variable.original_iseq);
217 if (body->param.keyword != NULL) {
218 if (body->param.keyword->table != &body->local_table[body->param.keyword->bits_start - body->param.keyword->num])
219 ruby_xfree((
void *)body->param.keyword->table);
220 if (body->param.keyword->default_values) {
221 ruby_xfree((
void *)body->param.keyword->default_values);
223 ruby_xfree((
void *)body->param.keyword);
225 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl)) {
226 ruby_xfree((
void *)body->local_table);
228 ruby_xfree((
void *)body->lvar_states);
230 compile_data_free(ISEQ_COMPILE_DATA(iseq));
231 if (body->outer_variables) rb_id_table_free(body->outer_variables);
235 RUBY_FREE_LEAVE(
"iseq");
238typedef VALUE iseq_value_itr_t(
void *ctx,
VALUE obj);
241iseq_scan_bits(
unsigned int page, iseq_bits_t bits,
VALUE *code,
VALUE *original_iseq)
244 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
247 offset = ntz_intptr(bits);
249 VALUE op = original_iseq[page_offset + offset];
250 rb_gc_mark_and_move(&code[page_offset + offset]);
251 VALUE newop = code[page_offset + offset];
253 original_iseq[page_offset + offset] = newop;
257 rb_gc_mark_and_move(&code[page_offset + offset]);
264rb_iseq_mark_and_move_each_compile_data_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
270 size = compile_data->iseq_size;
271 code = compile_data->iseq_encoded;
274 if (compile_data->mark_bits.list) {
275 if(compile_data->is_single_mark_bit) {
276 iseq_scan_bits(0, compile_data->mark_bits.single, code, original_iseq);
279 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
280 iseq_bits_t bits = compile_data->mark_bits.list[i];
281 iseq_scan_bits(i, bits, code, original_iseq);
287rb_iseq_mark_and_move_each_body_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
293 size = body->iseq_size;
294 code = body->iseq_encoded;
298 if (body->is_entries) {
300 is_entries += body->ivc_size;
303 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
304 ICVARC icvarc = (ICVARC)is_entries;
306 rb_gc_mark_and_move((
VALUE *)&icvarc->entry);
311 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
313 if (is->once.value) {
314 rb_gc_mark_and_move(&is->once.value);
319 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
320 IC ic = (IC)is_entries;
322 rb_gc_mark_and_move_ptr(&ic->entry);
328 if (body->mark_bits.list) {
329 if (ISEQ_MBITS_BUFLEN(size) == 1) {
330 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
333 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
334 iseq_bits_t bits = body->mark_bits.list[i];
335 iseq_scan_bits(i, bits, code, original_iseq);
342cc_is_active(
const struct rb_callcache *cc,
bool reference_updating)
345 if (cc == rb_vm_empty_cc() || rb_vm_empty_cc_for_super()) {
349 if (reference_updating) {
353 if (vm_cc_markable(cc) && vm_cc_valid(cc)) {
355 if (reference_updating) {
358 if (!METHOD_ENTRY_INVALIDATED(cme)) {
367rb_iseq_mark_and_move(rb_iseq_t *iseq,
bool reference_updating)
369 RUBY_MARK_ENTER(
"iseq");
371 rb_gc_mark_and_move(&iseq->wrapper);
373 if (ISEQ_BODY(iseq)) {
376 rb_iseq_mark_and_move_each_body_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
378 rb_gc_mark_and_move(&body->variable.script_lines);
379 rb_gc_mark_and_move(&body->location.label);
380 rb_gc_mark_and_move(&body->location.base_label);
381 rb_gc_mark_and_move(&body->location.pathobj);
382 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
383 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
384 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
386 if (body->call_data) {
387 for (
unsigned int i = 0; i < body->ci_size; i++) {
390 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
392 if (cc_is_active(cds[i].cc, reference_updating)) {
393 rb_gc_mark_and_move_ptr(&cds[i].cc);
395 else if (cds[i].cc != rb_vm_empty_cc()) {
396 cds[i].cc = rb_vm_empty_cc();
401 if (body->param.flags.has_kw && body->param.keyword != NULL) {
402 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
404 if (keyword->default_values != NULL) {
405 for (
int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
406 rb_gc_mark_and_move(&keyword->default_values[j]);
411 if (body->catch_table) {
414 for (
unsigned int i = 0; i < table->size; i++) {
416 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
418 rb_gc_mark_and_move_ptr(&entry->iseq);
423 if (reference_updating) {
425 rb_yjit_iseq_update_references(iseq);
428 rb_zjit_iseq_update_references(body->zjit_payload);
433 if (!rb_gc_checking_shareable()) {
435 rb_yjit_iseq_mark(body->yjit_payload);
438 rb_zjit_iseq_mark(body->zjit_payload);
444 if (!rb_gc_checking_shareable()) {
445 rb_gc_mark_and_move(&body->variable.coverage);
446 rb_gc_mark_and_move(&body->variable.pc2branchindex);
451 if (!rb_gc_checking_shareable()) {
452 rb_gc_mark_and_move(&iseq->aux.loader.obj);
456 if (!rb_gc_checking_shareable()) {
459 rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head);
460 rb_iseq_mark_and_move_each_compile_data_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
462 rb_gc_mark_and_move((
VALUE *)&compile_data->err_info);
463 rb_gc_mark_and_move((
VALUE *)&compile_data->catch_table_ary);
468 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
471 RUBY_MARK_LEAVE(
"iseq");
475param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
479 if (!pkw)
return size;
481 size +=
sizeof(
struct rb_iseq_param_keyword);
482 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
488rb_iseq_memsize(
const rb_iseq_t *iseq)
496 if (ISEQ_EXECUTABLE_P(iseq) && body) {
498 size += body->iseq_size *
sizeof(
VALUE);
500 size += body->local_table_size *
sizeof(
ID);
501 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
502 if (body->catch_table) {
503 size += iseq_catch_table_bytes(body->catch_table->size);
505 size += (body->param.opt_num + 1) *
sizeof(
VALUE);
506 size += param_keyword_size(body->param.keyword);
511 if (ISEQ_BODY(iseq)->is_entries) {
513 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
514 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
529 compile_data = ISEQ_COMPILE_DATA(iseq);
535 cur = compile_data->node.storage_head;
546rb_iseq_constant_body_alloc(
void)
556 rb_iseq_t *iseq = iseq_imemo_alloc();
557 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
562rb_iseq_pathobj_new(
VALUE path,
VALUE realpath)
565 VM_ASSERT(RB_TYPE_P(path,
T_STRING));
568 if (path == realpath ||
570 pathobj = rb_fstring(path);
573 if (!
NIL_P(realpath)) {
574 realpath = rb_fstring(realpath);
576 VALUE fpath = rb_fstring(path);
578 pathobj = rb_ary_new_from_args(2, fpath, realpath);
580 RB_OBJ_SET_SHAREABLE(pathobj);
586rb_iseq_pathobj_set(
const rb_iseq_t *iseq,
VALUE path,
VALUE realpath)
589 rb_iseq_pathobj_new(path, realpath));
594rb_iseq_alloc_with_dummy_path(
VALUE fname)
596 rb_iseq_t *dummy_iseq = iseq_alloc();
598 ISEQ_BODY(dummy_iseq)->type = ISEQ_TYPE_TOP;
601 RB_OBJ_SET_FROZEN_SHAREABLE(fname);
604 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.pathobj, fname);
605 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.label, fname);
610static rb_iseq_location_t *
611iseq_location_setup(rb_iseq_t *iseq,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_code_location_t *code_location,
const int node_id)
613 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
615 rb_iseq_pathobj_set(iseq, path, realpath);
618 loc->first_lineno = first_lineno;
620 if (ISEQ_BODY(iseq)->local_iseq == iseq && strcmp(RSTRING_PTR(name),
"initialize") == 0) {
621 ISEQ_BODY(iseq)->param.flags.use_block = 1;
625 loc->node_id = node_id;
626 loc->code_location = *code_location;
629 loc->code_location.beg_pos.lineno = 0;
630 loc->code_location.beg_pos.column = 0;
631 loc->code_location.end_pos.lineno = -1;
632 loc->code_location.end_pos.column = -1;
639set_relation(rb_iseq_t *iseq,
const rb_iseq_t *piseq)
645 if (
type == ISEQ_TYPE_TOP) {
646 body->local_iseq = iseq;
648 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
649 body->local_iseq = iseq;
652 RB_OBJ_WRITE(iseq, &body->local_iseq, ISEQ_BODY(piseq)->local_iseq);
659 if (
type == ISEQ_TYPE_MAIN) {
660 body->local_iseq = iseq;
669 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
674 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
680prepare_node_id(
const NODE *node)
682 if (!node)
return -1;
684 if (nd_type(node) == NODE_SCOPE && RNODE_SCOPE(node)->nd_parent) {
685 return nd_node_id(RNODE_SCOPE(node)->nd_parent);
688 return nd_node_id(node);
692prepare_iseq_build(rb_iseq_t *iseq,
693 VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_code_location_t *code_location,
const int node_id,
694 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
695 VALUE script_lines,
const rb_compile_option_t *option)
701 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
705 set_relation(iseq, parent);
707 name = rb_fstring(name);
708 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
709 if (iseq != body->local_iseq) {
710 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
712 ISEQ_COVERAGE_SET(iseq,
Qnil);
713 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
714 body->variable.flip_count = 0;
716 if (
NIL_P(script_lines)) {
723 ISEQ_COMPILE_DATA_ALLOC(iseq);
724 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
727 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
728 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
729 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
730 ISEQ_COMPILE_DATA(iseq)->option = option;
731 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
732 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
734 if (option->coverage_enabled) {
735 VALUE coverages = rb_get_coverages();
736 if (
RTEST(coverages)) {
737 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
741 ISEQ_COVERAGE_SET(iseq, coverage);
742 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
748#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
749static void validate_get_insn_info(
const rb_iseq_t *iseq);
753rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
755#if VM_INSN_INFO_TABLE_IMPL == 2
758 int size = body->insns_info.size;
759 int max_pos = body->iseq_size;
760 int *data = (
int *)body->insns_info.positions;
761 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
762 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
763#if VM_CHECK_MODE == 0
764 ruby_xfree(body->insns_info.positions);
765 body->insns_info.positions = NULL;
770#if VM_INSN_INFO_TABLE_IMPL == 2
774 int size = body->insns_info.size;
775 int max_pos = body->iseq_size;
776 struct succ_index_table *sd = body->insns_info.succ_index_table;
777 return succ_index_table_invert(max_pos, sd, size);
782rb_iseq_init_trace(rb_iseq_t *iseq)
784 iseq->aux.exec.global_trace_events = 0;
785 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
786 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
791finish_iseq_build(rb_iseq_t *iseq)
795 VALUE err = data->err_info;
796 ISEQ_COMPILE_DATA_CLEAR(iseq);
797 compile_data_free(data);
799#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
800 validate_get_insn_info(iseq);
804 VALUE path = pathobj_path(body->location.pathobj);
806 rb_funcallv(err, rb_intern(
"set_backtrace"), 1, &path);
810 RB_DEBUG_COUNTER_INC(iseq_num);
811 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
813 rb_iseq_init_trace(iseq);
817static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
818 .inline_const_cache = OPT_INLINE_CONST_CACHE,
819 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
820 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
821 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
822 .operands_unification = OPT_OPERANDS_UNIFICATION,
823 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
824 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
825 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
826 .coverage_enabled = TRUE,
829static const rb_compile_option_t COMPILE_OPTION_FALSE = {
830 .frozen_string_literal = -1,
834rb_iseq_opt_frozen_string_literal(
void)
836 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
840set_compile_option_from_hash(rb_compile_option_t *option,
VALUE opt)
842#define SET_COMPILE_OPTION(o, h, mem) \
843 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
844 if (flag == Qtrue) { (o)->mem = 1; } \
845 else if (flag == Qfalse) { (o)->mem = 0; } \
847#define SET_COMPILE_OPTION_NUM(o, h, mem) \
848 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
849 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
851 SET_COMPILE_OPTION(option, opt, inline_const_cache);
852 SET_COMPILE_OPTION(option, opt, peephole_optimization);
853 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
854 SET_COMPILE_OPTION(option, opt, specialized_instruction);
855 SET_COMPILE_OPTION(option, opt, operands_unification);
856 SET_COMPILE_OPTION(option, opt, instructions_unification);
857 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
858 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
859 SET_COMPILE_OPTION(option, opt, coverage_enabled);
860 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
861#undef SET_COMPILE_OPTION
862#undef SET_COMPILE_OPTION_NUM
865static rb_compile_option_t *
866set_compile_option_from_ast(rb_compile_option_t *option,
const rb_ast_body_t *ast)
868 if (ast->frozen_string_literal >= 0) {
869 option->frozen_string_literal = ast->frozen_string_literal;
875make_compile_option(rb_compile_option_t *option,
VALUE opt)
878 *option = COMPILE_OPTION_DEFAULT;
881 *option = COMPILE_OPTION_FALSE;
883 else if (opt ==
Qtrue) {
885 for (i = 0; i < (int)(
sizeof(rb_compile_option_t) /
sizeof(int)); ++i)
886 ((
int *)option)[i] = 1;
888 else if (RB_TYPE_P(opt,
T_HASH)) {
889 *option = COMPILE_OPTION_DEFAULT;
890 set_compile_option_from_hash(option, opt);
893 rb_raise(
rb_eTypeError,
"Compile option must be Hash/true/false/nil");
898make_compile_option_value(rb_compile_option_t *option)
900 VALUE opt = rb_hash_new_with_size(11);
901#define SET_COMPILE_OPTION(o, h, mem) \
902 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
903#define SET_COMPILE_OPTION_NUM(o, h, mem) \
904 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
906 SET_COMPILE_OPTION(option, opt, inline_const_cache);
907 SET_COMPILE_OPTION(option, opt, peephole_optimization);
908 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
909 SET_COMPILE_OPTION(option, opt, specialized_instruction);
910 SET_COMPILE_OPTION(option, opt, operands_unification);
911 SET_COMPILE_OPTION(option, opt, instructions_unification);
912 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
913 SET_COMPILE_OPTION(option, opt, coverage_enabled);
914 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
916#undef SET_COMPILE_OPTION
917#undef SET_COMPILE_OPTION_NUM
918 VALUE frozen_string_literal = option->frozen_string_literal == -1 ?
Qnil : RBOOL(option->frozen_string_literal);
919 rb_hash_aset(opt,
ID2SYM(rb_intern(
"frozen_string_literal")), frozen_string_literal);
925 const rb_iseq_t *parent,
enum rb_iseq_type
type)
927 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
928 0,
type, &COMPILE_OPTION_DEFAULT,
933ast_line_count(
const VALUE ast_value)
935 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
936 return ast->body.line_count;
940iseq_setup_coverage(
VALUE coverages,
VALUE path,
int line_count)
942 if (line_count >= 0) {
943 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
945 VALUE coverage = rb_default_coverage(
len);
946 rb_hash_aset(coverages, path, coverage);
955iseq_new_setup_coverage(
VALUE path,
int line_count)
957 VALUE coverages = rb_get_coverages();
959 if (
RTEST(coverages)) {
960 iseq_setup_coverage(coverages, path, line_count);
965rb_iseq_new_top(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent)
967 iseq_new_setup_coverage(path, ast_line_count(ast_value));
969 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
970 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
978pm_iseq_new_top(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int *error_state)
982 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
983 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT, error_state);
987rb_iseq_new_main(
const VALUE ast_value,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt)
989 iseq_new_setup_coverage(path, ast_line_count(ast_value));
991 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
993 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
1002pm_iseq_new_main(pm_scope_node_t *node,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt,
int *error_state)
1004 iseq_new_setup_coverage(path, (
int) (node->parser->
newline_list.
size - 1));
1006 return pm_iseq_new_with_opt(node, rb_fstring_lit(
"<main>"),
1008 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE, error_state);
1012rb_iseq_new_eval(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
1014 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1015 VALUE coverages = rb_get_coverages();
1016 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1017 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
1021 rb_compile_option_t option = COMPILE_OPTION_DEFAULT;
1022 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1023 if (ast->body.coverage_enabled >= 0) {
1024 option.coverage_enabled = ast->body.coverage_enabled;
1026 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
1027 parent, isolated_depth, ISEQ_TYPE_EVAL, &option,
1032pm_iseq_new_eval(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1033 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
int *error_state)
1035 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1036 VALUE coverages = rb_get_coverages();
1037 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1038 iseq_setup_coverage(coverages, path, ((
int) (node->parser->
newline_list.
size - 1)) + first_lineno - 1);
1042 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
1043 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT, error_state);
1046static inline rb_iseq_t *
1047iseq_translate(rb_iseq_t *iseq)
1050 VALUE v1 = iseqw_new(iseq);
1052 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
1053 iseq = (rb_iseq_t *)iseqw_check(v2);
1062 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1063 enum rb_iseq_type
type,
const rb_compile_option_t *option,
1066 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1067 rb_ast_body_t *body = ast ? &ast->body : NULL;
1068 const NODE *node = body ? body->root : 0;
1070 rb_iseq_t *iseq = iseq_alloc();
1071 rb_compile_option_t new_opt;
1073 if (!option) option = &COMPILE_OPTION_DEFAULT;
1076 option = set_compile_option_from_ast(&new_opt, body);
1079 if (!
NIL_P(script_lines)) {
1082 else if (body && body->script_lines) {
1083 script_lines = rb_parser_build_script_lines_from(body->script_lines);
1086 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1089 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, prepare_node_id(node),
1090 parent, isolated_depth,
type, script_lines, option);
1092 rb_iseq_compile_node(iseq, node);
1093 finish_iseq_build(iseq);
1096 return iseq_translate(iseq);
1101 pm_scope_node_t *node;
1105pm_iseq_new_with_opt_try(
VALUE d)
1110 pm_iseq_compile_node(data->iseq, data->node);
1113 finish_iseq_build(data->iseq);
1131pm_iseq_new_with_opt(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1132 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1133 enum rb_iseq_type
type,
const rb_compile_option_t *option,
int *error_state)
1135 rb_iseq_t *iseq = iseq_alloc();
1136 ISEQ_BODY(iseq)->prism =
true;
1138 rb_compile_option_t next_option;
1139 if (!option) option = &COMPILE_OPTION_DEFAULT;
1141 next_option = *option;
1142 next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
1143 option = &next_option;
1146 int32_t start_line = node->parser->
start_line;
1151 rb_code_location_t code_location = (rb_code_location_t) {
1152 .beg_pos = { .lineno = (int) start.
line, .column = (
int) start.
column },
1153 .end_pos = { .lineno = (int) end.
line, .column = (
int) end.
column }
1156 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node->ast_node->
node_id,
1163 rb_protect(pm_iseq_new_with_opt_try, (
VALUE)&data, error_state);
1165 if (*error_state)
return NULL;
1167 return iseq_translate(iseq);
1171rb_iseq_new_with_callback(
1174 int first_lineno,
const rb_iseq_t *parent,
1175 enum rb_iseq_type
type,
const rb_compile_option_t *option)
1178 rb_iseq_t *iseq = iseq_alloc();
1180 if (!option) option = &COMPILE_OPTION_DEFAULT;
1181 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1183 rb_iseq_compile_callback(iseq, ifunc);
1184 finish_iseq_build(iseq);
1190rb_iseq_load_iseq(
VALUE fname)
1195 return iseqw_check(iseqv);
1210 return iseqw_check(iseqv);
1216#define CHECK_ARRAY(v) rb_to_array_type(v)
1217#define CHECK_HASH(v) rb_to_hash_type(v)
1218#define CHECK_STRING(v) rb_str_to_str(v)
1219#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1222static enum rb_iseq_type
1225 const ID id_top = rb_intern(
"top");
1226 const ID id_method = rb_intern(
"method");
1227 const ID id_block = rb_intern(
"block");
1228 const ID id_class = rb_intern(
"class");
1229 const ID id_rescue = rb_intern(
"rescue");
1230 const ID id_ensure = rb_intern(
"ensure");
1231 const ID id_eval = rb_intern(
"eval");
1232 const ID id_main = rb_intern(
"main");
1233 const ID id_plain = rb_intern(
"plain");
1237 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1238 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1239 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1240 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1241 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1242 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1243 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1244 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1245 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1246 return (
enum rb_iseq_type)-1;
1250iseq_load(
VALUE data,
const rb_iseq_t *parent,
VALUE opt)
1252 rb_iseq_t *iseq = iseq_alloc();
1254 VALUE magic, version1, version2, format_type, misc;
1255 VALUE name, path, realpath, code_location, node_id;
1256 VALUE type, body, locals, params, exception;
1258 st_data_t iseq_type;
1259 rb_compile_option_t option;
1261 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1268 data = CHECK_ARRAY(data);
1275 ((void)magic, (void)version1, (void)version2, (void)format_type);
1280 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1289 ISEQ_BODY(iseq)->local_iseq = iseq;
1291 iseq_type = iseq_type_from_sym(
type);
1292 if (iseq_type == (
enum rb_iseq_type)-1) {
1296 node_id = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_id")));
1298 code_location = rb_hash_aref(misc,
ID2SYM(rb_intern(
"code_location")));
1306 if (
SYM2ID(rb_hash_aref(misc,
ID2SYM(rb_intern(
"parser")))) == rb_intern(
"prism")) {
1307 ISEQ_BODY(iseq)->prism =
true;
1310 make_compile_option(&option, opt);
1311 option.peephole_optimization = FALSE;
1312 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1313 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1315 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1317 finish_iseq_build(iseq);
1319 return iseqw_new(iseq);
1326iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1330 return iseq_load(data, NULL, opt);
1336 return iseq_load(data,
RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1342 rb_iseq_t *iseq = NULL;
1343 rb_compile_option_t option;
1344#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1345# define INITIALIZED volatile
1351 VALUE INITIALIZED ast_value;
1353 VALUE name = rb_fstring_lit(
"<compiled>");
1356 make_compile_option(&option, opt);
1359 if (RB_TYPE_P(src,
T_FILE)) {
1360 parse = rb_parser_compile_file_path;
1363 parse = rb_parser_compile_string_path;
1367 const VALUE parser = rb_parser_new();
1368 const rb_iseq_t *outer_scope = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1370 rb_parser_set_context(parser, outer_scope, FALSE);
1371 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1373 ast_value = (*parse)(parser, file, src, ln);
1376 ast = rb_ruby_ast_data_get(ast_value);
1378 if (!ast || !ast->body.root) {
1379 rb_ast_dispose(ast);
1383 if (option.coverage_enabled) iseq_new_setup_coverage(file, ast_line_count(ast_value));
1384 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1385 NULL, 0, ISEQ_TYPE_TOP, &option,
1387 rb_ast_dispose(ast);
1396 rb_iseq_t *iseq = NULL;
1397 rb_compile_option_t option;
1399 VALUE name = rb_fstring_lit(
"<compiled>");
1402 make_compile_option(&option, opt);
1406 bool parse_file =
false;
1407 if (RB_TYPE_P(src,
T_FILE)) {
1417 pm_options_scopes_init(&result.
options, 1);
1418 result.
node.coverage_enabled = option.coverage_enabled;
1420 switch (option.frozen_string_literal) {
1421 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1423 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1424 pm_options_frozen_string_literal_set(&result.
options,
false);
1426 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1427 pm_options_frozen_string_literal_set(&result.
options,
true);
1430 rb_bug(
"pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1438 error = pm_load_parse_file(&result, src, ruby_vm_keep_script_lines ? &script_lines : NULL);
1441 error = pm_parse_string(&result, src, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1446 if (error ==
Qnil) {
1448 if (option.coverage_enabled) iseq_new_setup_coverage(file, (
int) (result.
node.parser->
newline_list.
size - 1));
1449 iseq = pm_iseq_new_with_opt(&result.
node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1451 pm_parse_result_free(&result);
1455 rb_jump_tag(error_state);
1459 pm_parse_result_free(&result);
1467rb_iseq_path(
const rb_iseq_t *iseq)
1469 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1473rb_iseq_realpath(
const rb_iseq_t *iseq)
1475 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1479rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1481 return rb_iseq_realpath(iseq);
1485rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1487 return NIL_P(rb_iseq_realpath(iseq));
1491rb_iseq_label(
const rb_iseq_t *iseq)
1493 return ISEQ_BODY(iseq)->location.label;
1497rb_iseq_base_label(
const rb_iseq_t *iseq)
1499 return ISEQ_BODY(iseq)->location.base_label;
1503rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1505 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1509rb_iseq_method_name(
const rb_iseq_t *iseq)
1513 if (body->type == ISEQ_TYPE_METHOD) {
1514 return body->location.base_label;
1522rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1524 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1525 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1526 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1527 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1528 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1531static ID iseq_type_id(
enum rb_iseq_type
type);
1534rb_iseq_type(
const rb_iseq_t *iseq)
1536 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1540rb_iseq_coverage(
const rb_iseq_t *iseq)
1542 return ISEQ_COVERAGE(iseq);
1546remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1549 for (; v != (
VALUE)vend; v += stride) {
1550 void *
ptr = rb_asan_poisoned_object_p(v);
1551 rb_asan_unpoison_object(v,
false);
1553 if (rb_obj_is_iseq(v)) {
1554 rb_iseq_t *iseq = (rb_iseq_t *)v;
1555 ISEQ_COVERAGE_SET(iseq,
Qnil);
1558 asan_poison_object_if(
ptr, v);
1564rb_iseq_remove_coverage_all(
void)
1566 rb_objspace_each_objects(remove_coverage_i, NULL);
1572iseqw_mark_and_move(
void *
ptr)
1578iseqw_memsize(
const void *
ptr)
1580 return rb_iseq_memsize(*(
const rb_iseq_t **)
ptr);
1586 iseqw_mark_and_move,
1589 iseqw_mark_and_move,
1591 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1595iseqw_new(
const rb_iseq_t *iseq)
1597 if (iseq->wrapper) {
1598 if (*(
const rb_iseq_t **)
rb_check_typeddata(iseq->wrapper, &iseqw_data_type) != iseq) {
1599 rb_raise(
rb_eTypeError,
"wrong iseq wrapper: %" PRIsVALUE
" for %p",
1600 iseq->wrapper, (
void *)iseq);
1602 return iseq->wrapper;
1610 RB_OBJ_SET_FROZEN_SHAREABLE((
VALUE)obj);
1618rb_iseqw_new(
const rb_iseq_t *iseq)
1620 return iseqw_new(iseq);
1629iseqw_s_compile_parser(
int argc,
VALUE *argv,
VALUE self,
bool prism)
1635 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1637 case 5: opt = argv[--i];
1638 case 4: line = argv[--i];
1639 case 3: path = argv[--i];
1640 case 2: file = argv[--i];
1643 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1644 if (
NIL_P(path)) path = file;
1652 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1655 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1658 return iseqw_new(iseq);
1699iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1701 return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p());
1741iseqw_s_compile_parsey(
int argc,
VALUE *argv,
VALUE self)
1743 return iseqw_s_compile_parser(argc, argv, self,
false);
1783iseqw_s_compile_prism(
int argc,
VALUE *argv,
VALUE self)
1785 return iseqw_s_compile_parser(argc, argv, self,
true);
1788static VALUE iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self);
1811iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1813 if (rb_ruby_prism_p()) {
1814 return iseqw_s_compile_file_prism(argc, argv, self);
1821 rb_compile_option_t option;
1824 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1825 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1827 case 2: opt = argv[--i];
1830 file = rb_fstring(file);
1832 make_compile_option(&option, opt);
1836 rb_execution_context_t *ec = GET_EC();
1837 VALUE v = rb_vm_push_frame_fname(ec, file);
1839 parser = rb_parser_new();
1840 rb_parser_set_context(parser, NULL, FALSE);
1841 ast_value = rb_parser_load_file(parser, file);
1842 if (option.coverage_enabled) iseq_new_setup_coverage(file, ast_line_count(ast_value));
1843 ast = rb_ruby_ast_data_get(ast_value);
1844 if (!ast->body.root) exc = GET_EC()->errinfo;
1847 if (!ast->body.root) {
1848 rb_ast_dispose(ast);
1852 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
1854 rb_realpath_internal(
Qnil, file, 1),
1855 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1857 rb_ast_dispose(ast);
1860 rb_vm_pop_frame(ec);
1887iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self)
1890 rb_compile_option_t option;
1893 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1894 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1896 case 2: opt = argv[--i];
1899 file = rb_fstring(file);
1901 rb_execution_context_t *ec = GET_EC();
1902 VALUE v = rb_vm_push_frame_fname(ec, file);
1904 make_compile_option(&option, opt);
1908 result.
node.coverage_enabled = option.coverage_enabled;
1910 switch (option.frozen_string_literal) {
1911 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1913 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1914 pm_options_frozen_string_literal_set(&result.
options,
false);
1916 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1917 pm_options_frozen_string_literal_set(&result.
options,
true);
1920 rb_bug(
"iseqw_s_compile_file_prism: invalid frozen_string_literal=%d", option.frozen_string_literal);
1925 VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1927 if (error ==
Qnil) {
1929 if (option.coverage_enabled) iseq_new_setup_coverage(file, (
int) (result.
node.parser->
newline_list.
size - 1));
1930 rb_iseq_t *iseq = pm_iseq_new_with_opt(&result.
node, rb_fstring_lit(
"<main>"),
1932 rb_realpath_internal(
Qnil, file, 1),
1933 1, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1935 pm_parse_result_free(&result);
1939 rb_jump_tag(error_state);
1942 ret = iseqw_new(iseq);
1943 rb_vm_pop_frame(ec);
1948 pm_parse_result_free(&result);
1949 rb_vm_pop_frame(ec);
1986iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
1988 rb_compile_option_t option;
1989 make_compile_option(&option, opt);
1990 COMPILE_OPTION_DEFAULT = option;
2003iseqw_s_compile_option_get(
VALUE self)
2005 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
2008static const rb_iseq_t *
2009iseqw_check(
VALUE iseqw)
2011 rb_iseq_t **iseq_ptr;
2013 rb_iseq_t *iseq = *iseq_ptr;
2015 if (!ISEQ_BODY(iseq)) {
2016 rb_ibf_load_iseq_complete(iseq);
2019 if (!ISEQ_BODY(iseq)->location.label) {
2020 rb_raise(
rb_eTypeError,
"uninitialized InstructionSequence");
2026rb_iseqw_to_iseq(
VALUE iseqw)
2028 return iseqw_check(iseqw);
2040iseqw_eval(
VALUE self)
2042 const rb_iseq_t *iseq = iseqw_check(self);
2043 if (0 == ISEQ_BODY(iseq)->iseq_size) {
2044 rb_raise(
rb_eTypeError,
"attempt to evaluate dummy InstructionSequence");
2046 return rb_iseq_eval(iseq, rb_current_box());
2054iseqw_inspect(
VALUE self)
2056 const rb_iseq_t *iseq = iseqw_check(self);
2060 if (!body->location.label) {
2061 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
2064 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
2066 body->location.label, rb_iseq_path(iseq),
2067 FIX2INT(rb_iseq_first_lineno(iseq)));
2095iseqw_path(
VALUE self)
2097 return rb_iseq_path(iseqw_check(self));
2117iseqw_absolute_path(
VALUE self)
2119 return rb_iseq_realpath(iseqw_check(self));
2146iseqw_label(
VALUE self)
2148 return rb_iseq_label(iseqw_check(self));
2172iseqw_base_label(
VALUE self)
2174 return rb_iseq_base_label(iseqw_check(self));
2188iseqw_first_lineno(
VALUE self)
2190 return rb_iseq_first_lineno(iseqw_check(self));
2193static VALUE iseq_data_to_ary(
const rb_iseq_t *iseq);
2277iseqw_to_a(
VALUE self)
2279 const rb_iseq_t *iseq = iseqw_check(self);
2280 return iseq_data_to_ary(iseq);
2283#if VM_INSN_INFO_TABLE_IMPL == 1
2285get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
2288 size_t size = body->insns_info.size;
2290 const unsigned int *positions = body->insns_info.positions;
2291 const int debug = 0;
2294 printf(
"size: %"PRIuSIZE
"\n", size);
2295 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2296 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2302 else if (size == 1) {
2303 return &insns_info[0];
2306 size_t l = 1, r = size - 1;
2308 size_t m = l + (r - l) / 2;
2309 if (positions[m] == pos) {
2310 return &insns_info[m];
2312 if (positions[m] < pos) {
2320 return &insns_info[size-1];
2322 if (positions[l] > pos) {
2323 return &insns_info[l-1];
2325 return &insns_info[l];
2330get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2332 return get_insn_info_binary_search(iseq, pos);
2336#if VM_INSN_INFO_TABLE_IMPL == 2
2338get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
2341 size_t size = body->insns_info.size;
2343 const int debug = 0;
2346#if VM_CHECK_MODE > 0
2347 const unsigned int *positions = body->insns_info.positions;
2348 printf(
"size: %"PRIuSIZE
"\n", size);
2349 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2350 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2352 printf(
"size: %"PRIuSIZE
"\n", size);
2353 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
2354 (
size_t)0, insns_info[0].line_no, pos);
2361 else if (size == 1) {
2362 return &insns_info[0];
2366 VM_ASSERT(body->insns_info.succ_index_table != NULL);
2367 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
2368 return &insns_info[index-1];
2373get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2375 return get_insn_info_succinct_bitvector(iseq, pos);
2379#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2381get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
2384 size_t i = 0, size = body->insns_info.size;
2386 const unsigned int *positions = body->insns_info.positions;
2387 const int debug = 0;
2390 printf(
"size: %"PRIuSIZE
"\n", size);
2391 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2392 i, positions[i], insns_info[i].line_no, pos);
2398 else if (size == 1) {
2399 return &insns_info[0];
2402 for (i=1; i<size; i++) {
2403 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2404 i, positions[i], insns_info[i].line_no, pos);
2406 if (positions[i] == pos) {
2407 return &insns_info[i];
2409 if (positions[i] > pos) {
2410 return &insns_info[i-1];
2414 return &insns_info[i-1];
2418#if VM_INSN_INFO_TABLE_IMPL == 0
2420get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2422 return get_insn_info_linear_search(iseq, pos);
2426#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2428validate_get_insn_info(
const rb_iseq_t *iseq)
2432 for (i = 0; i < body->iseq_size; i++) {
2433 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2434 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
2441rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
2446 return entry->line_no;
2453#ifdef USE_ISEQ_NODE_ID
2455rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
2460 return entry->node_id;
2469rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2473 return entry->events;
2480static void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2485rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos,
rb_event_flag_t reset)
2492 entry->events &= ~reset;
2493 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2494 rb_iseq_trace_flag_cleared(iseq, pos);
2501local_var_name(
const rb_iseq_t *diseq,
VALUE level,
VALUE op)
2508 for (i = 0; i < level; i++) {
2509 diseq = ISEQ_BODY(diseq)->parent_iseq;
2511 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2512 lid = ISEQ_BODY(diseq)->local_table[idx];
2513 name = rb_id2str(lid);
2523 rb_str_catf(name,
"@%d", idx);
2527int rb_insn_unified_local_var_level(
VALUE);
2531rb_insn_operand_intern(
const rb_iseq_t *iseq,
2535 const char *types = insn_op_types(insn);
2536 char type = types[op_no];
2541 ret = rb_sprintf(
"%"PRIdVALUE, (
VALUE)(pos +
len + op));
2545 if (insn == BIN(defined) && op_no == 0) {
2546 enum defined_type deftype = (
enum defined_type)op;
2549 ret = rb_fstring_lit(
"func");
2552 ret = rb_fstring_lit(
"ref");
2554 case DEFINED_CONST_FROM:
2555 ret = rb_fstring_lit(
"constant-from");
2558 ret = rb_iseq_defined_string(deftype);
2563 else if (insn == BIN(checktype) && op_no == 0) {
2569 ret = rb_sprintf(
"%"PRIuVALUE, op);
2574 if (types[op_no+1] == TS_NUM && pnop) {
2575 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2577 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2578 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2590 op = obj_resurrect(op);
2591 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2596 ret = rb_sprintf(
":$%c", (
type >> 1));
2599 ret = rb_sprintf(
":$%d", (
type >> 1));
2604 ret = rb_dump_literal(op);
2615 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2616 ret = ISEQ_BODY(iseq)->location.label;
2630 const ID *segments = ((IC)op)->segments;
2633 rb_str_catf(ret,
"::%s", rb_id2name(*segments++));
2649 ID mid = vm_ci_mid(ci);
2652 rb_ary_push(ary, rb_sprintf(
"mid:%"PRIsVALUE, rb_id2str(mid)));
2655 rb_ary_push(ary, rb_sprintf(
"argc:%d", vm_ci_argc(ci)));
2657 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2663 if (vm_ci_flag(ci)) {
2665# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2666 CALL_FLAG(ARGS_SPLAT);
2667 CALL_FLAG(ARGS_SPLAT_MUT);
2668 CALL_FLAG(ARGS_BLOCKARG);
2671 CALL_FLAG(ARGS_SIMPLE);
2672 CALL_FLAG(TAILCALL);
2676 CALL_FLAG(KW_SPLAT);
2677 CALL_FLAG(KW_SPLAT_MUT);
2678 CALL_FLAG(FORWARDING);
2679 CALL_FLAG(IGNORE_REFINEMENTS);
2680 CALL_FLAG(OPT_SEND);
2696 if (dladdr((
void *)op, &info) && info.dli_sname) {
2708 ret = rb_sprintf(
"<builtin!%s/%d>",
2709 bf->name, bf->argc);
2714 rb_bug(
"unknown operand type: %c",
type);
2720right_strip(
VALUE str)
2722 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2723 while (end-- > beg && *end ==
' ');
2733rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2734 const rb_iseq_t *iseq,
VALUE child)
2736 VALUE insn = code[pos];
2737 int len = insn_len(insn);
2739 const char *types = insn_op_types(insn);
2741 const char *insn_name_buff;
2743 insn_name_buff = insn_name(insn);
2745 extern const int rb_vm_max_insn_name_size;
2746 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2749 rb_str_catf(str,
"%04"PRIuSIZE
" %-28.*s ", pos,
2750 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2753 for (j = 0; types[j]; j++) {
2754 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2755 len, pos, &code[pos + j + 2],
2765 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2766 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2767 if (line_no && line_no != prev) {
2768 long slen = RSTRING_LEN(str);
2769 slen = (slen > 70) ? 0 : (70 - slen);
2770 str = rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2777 str = rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s%s]",
2788 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2789 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2799 printf(
"%.*s\n", (
int)RSTRING_LEN(str), RSTRING_PTR(str));
2808 case CATCH_TYPE_RESCUE:
2810 case CATCH_TYPE_ENSURE:
2812 case CATCH_TYPE_RETRY:
2814 case CATCH_TYPE_BREAK:
2816 case CATCH_TYPE_REDO:
2818 case CATCH_TYPE_NEXT:
2821 rb_bug(
"unknown catch type: %d",
type);
2827iseq_inspect(
const rb_iseq_t *iseq)
2830 if (!body->location.label) {
2831 return rb_sprintf(
"#<ISeq: uninitialized>");
2834 const rb_code_location_t *loc = &body->location.code_location;
2835 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2836 body->location.label, rb_iseq_path(iseq),
2837 loc->beg_pos.lineno,
2838 loc->beg_pos.lineno,
2839 loc->beg_pos.column,
2840 loc->end_pos.lineno,
2841 loc->end_pos.column);
2847 {(void (*)(
void *))rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2848 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2852rb_iseq_disasm_recursive(const rb_iseq_t *iseq,
VALUE indent)
2862 enum {header_minlen = 72};
2863 st_table *done_iseq = 0;
2865 const char *indent_str;
2868 size = body->iseq_size;
2870 indent_len = RSTRING_LEN(indent);
2871 indent_str = RSTRING_PTR(indent);
2877 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2879 memset(RSTRING_END(str),
'=', header_minlen - l);
2881 if (iseq->body->builtin_attrs) {
2882#define disasm_builtin_attr(str, iseq, attr) \
2883 if (iseq->body->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
2884 rb_str_cat2(str, " " #attr); \
2886 disasm_builtin_attr(str, iseq, LEAF);
2887 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
2888 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
2889 disasm_builtin_attr(str, iseq, C_TRACE);
2894 if (body->catch_table) {
2898 if (body->catch_table) {
2900 indent_str = RSTRING_PTR(indent);
2901 for (i = 0; i < body->catch_table->size; i++) {
2903 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2906 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2907 catch_type((
int)entry->type), (
int)entry->start,
2908 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2909 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2910 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2912 done_iseq = st_init_numtable();
2915 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2916 indent_str = RSTRING_PTR(indent);
2919 rb_str_resize(indent, indent_len);
2920 indent_str = RSTRING_PTR(indent);
2922 if (body->catch_table) {
2924 rb_str_cat2(str,
"|-------------------------------------"
2925 "-----------------------------------\n");
2929 if (body->local_table) {
2930 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
2933 "local table (size: %d, argc: %d "
2934 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2935 body->local_table_size,
2936 body->param.lead_num,
2937 body->param.opt_num,
2938 body->param.flags.has_rest ? body->param.rest_start : -1,
2939 body->param.post_num,
2940 body->param.flags.has_block ? body->param.block_start : -1,
2941 body->param.flags.has_kw ? keyword->num : -1,
2942 body->param.flags.has_kw ? keyword->required_num : -1,
2943 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2945 for (i = body->local_table_size; i > 0;) {
2946 int li = body->local_table_size - --i - 1;
2948 VALUE name = local_var_name(iseq, 0, i);
2953 if (body->param.flags.has_opt) {
2954 int argc = body->param.lead_num;
2955 int opts = body->param.opt_num;
2956 if (li >= argc && li < argc + opts) {
2957 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2958 body->param.opt_table[li - argc]);
2962 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2963 (body->param.lead_num > li) ? (body->param.flags.ambiguous_param0 ?
"AmbiguousArg" :
"Arg") :
"",
2965 (body->param.flags.has_rest && body->param.rest_start == li) ? (body->param.flags.anon_rest ?
"AnonRest" :
"Rest") :
"",
2966 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ?
"Post" :
"",
2967 (body->param.flags.has_kwrest && keyword->rest_start == li) ? (body->param.flags.anon_kwrest ?
"AnonKwrest" :
"Kwrest") :
"",
2968 (body->param.flags.has_block && body->param.block_start == li) ?
"Block" :
"");
2971 rb_str_catf(str,
"[%2d] ", i + 1);
2972 width = RSTRING_LEN(str) + 11;
2974 if (*argi) rb_str_catf(str,
"<%s>", argi);
2975 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str,
"%*s", (
int)width,
"");
2981 code = rb_iseq_original_iseq(iseq);
2982 for (n = 0; n < size;) {
2984 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2989 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2991 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2992 indent_str = RSTRING_PTR(indent);
3000rb_iseq_disasm(
const rb_iseq_t *iseq)
3003 rb_str_resize(str, RSTRING_LEN(str));
3013rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
3015 struct rb_id_table * iv_names = rb_id_table_create(0);
3017 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
3018 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
3020 if (cache->iv_set_name) {
3021 rb_id_table_insert(iv_names, cache->iv_set_name,
Qtrue);
3025 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
3028 if (!
NIL_P(superclass)) {
3029 count += RCLASS_MAX_IV_COUNT(superclass);
3032 rb_id_table_free(iv_names);
3056iseqw_disasm(
VALUE self)
3058 return rb_iseq_disasm(iseqw_check(self));
3062iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
3065 VALUE *code = rb_iseq_original_iseq(iseq);
3067 const rb_iseq_t *child;
3070 if (body->catch_table) {
3071 for (i = 0; i < body->catch_table->size; i++) {
3073 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
3074 child = entry->iseq;
3076 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3077 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3078 (*iter_func)(child, data);
3084 for (i=0; i<body->iseq_size;) {
3085 VALUE insn = code[i];
3086 int len = insn_len(insn);
3087 const char *types = insn_op_types(insn);
3090 for (j=0; types[j]; j++) {
3093 child = (
const rb_iseq_t *)code[i+j+1];
3095 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3096 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3097 (*iter_func)(child, data);
3112yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
3126iseqw_each_child(
VALUE self)
3128 const rb_iseq_t *iseq = iseqw_check(self);
3129 iseq_iterate_children(iseq, yield_each_children, NULL);
3136#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
3156iseqw_trace_points(
VALUE self)
3158 const rb_iseq_t *iseq = iseqw_check(self);
3163 for (i=0; i<body->insns_info.size; i++) {
3165 if (entry->events) {
3166 push_event_info(iseq, entry->events, entry->line_no, ary);
3210 const rb_iseq_t *iseq = NULL;
3212 if (rb_frame_info_p(body)) {
3213 iseq = rb_get_iseq_from_frame_info(body);
3216 iseq = vm_proc_iseq(body);
3218 if (!rb_obj_is_iseq((
VALUE)iseq)) {
3223 iseq = rb_method_iseq(body);
3225 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
3229 return iseq ? iseqw_new(iseq) :
Qnil;
3287 VALUE iseqw = iseqw_s_of(klass, body);
3288 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3292register_label(
struct st_table *table,
unsigned long idx)
3295 st_insert(table, idx, sym);
3304 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
3305 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
3306 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
3307 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
3308 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
3309 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
3311 rb_bug(
"unknown exception type: %d", (
int)
type);
3326 {(void (*)(
void *))rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
3327 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3330#define DECL_ID(name) \
3333#define INIT_ID(name) \
3334 id_##name = rb_intern(#name)
3337iseq_type_id(
enum rb_iseq_type
type)
3362 case ISEQ_TYPE_TOP:
return id_top;
3363 case ISEQ_TYPE_METHOD:
return id_method;
3364 case ISEQ_TYPE_BLOCK:
return id_block;
3365 case ISEQ_TYPE_CLASS:
return id_class;
3366 case ISEQ_TYPE_RESCUE:
return id_rescue;
3367 case ISEQ_TYPE_ENSURE:
return id_ensure;
3368 case ISEQ_TYPE_EVAL:
return id_eval;
3369 case ISEQ_TYPE_MAIN:
return id_main;
3370 case ISEQ_TYPE_PLAIN:
return id_plain;
3373 rb_bug(
"unsupported iseq type: %d", (
int)
type);
3377iseq_data_to_ary(
const rb_iseq_t *iseq)
3386 VALUE *seq, *iseq_original;
3391 VALUE params = rb_hash_new();
3395 VALUE misc = rb_hash_new();
3397 static ID insn_syms[VM_BARE_INSTRUCTION_SIZE];
3398 struct st_table *labels_table = st_init_numtable();
3401 if (insn_syms[0] == 0) {
3403 for (i=0; i<numberof(insn_syms); i++) {
3404 insn_syms[i] = rb_intern(insn_name(i));
3409 type = iseq_type_id(iseq_body->type);
3412 for (i=0; i<iseq_body->local_table_size; i++) {
3413 ID lid = iseq_body->local_table[i];
3415 if (lid != idItImplicit && rb_id2str(lid)) {
3429 const struct rb_iseq_param_keyword *
const keyword = iseq_body->param.keyword;
3432 if (iseq_body->param.flags.has_opt) {
3433 int len = iseq_body->param.opt_num + 1;
3436 for (j = 0; j <
len; j++) {
3437 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
3440 rb_hash_aset(params,
ID2SYM(rb_intern(
"opt")), arg_opt_labels);
3444 if (iseq_body->param.flags.has_lead) rb_hash_aset(params,
ID2SYM(rb_intern(
"lead_num")),
INT2FIX(iseq_body->param.lead_num));
3445 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_num")),
INT2FIX(iseq_body->param.post_num));
3446 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_start")),
INT2FIX(iseq_body->param.post_start));
3447 if (iseq_body->param.flags.has_rest) rb_hash_aset(params,
ID2SYM(rb_intern(
"rest_start")),
INT2FIX(iseq_body->param.rest_start));
3448 if (iseq_body->param.flags.has_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"block_start")),
INT2FIX(iseq_body->param.block_start));
3449 if (iseq_body->param.flags.has_kw) {
3452 for (i=0; i<keyword->required_num; i++) {
3455 for (j=0; i<keyword->num; i++, j++) {
3456 VALUE key = rb_ary_new_from_args(1,
ID2SYM(keyword->table[i]));
3457 if (!UNDEF_P(keyword->default_values[j])) {
3463 rb_hash_aset(params,
ID2SYM(rb_intern(
"kwbits")),
3464 INT2FIX(keyword->bits_start));
3465 rb_hash_aset(params,
ID2SYM(rb_intern(
"keyword")), keywords);
3467 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params,
ID2SYM(rb_intern(
"kwrest")),
INT2FIX(keyword->rest_start));
3468 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params,
ID2SYM(rb_intern(
"ambiguous_param0")),
Qtrue);
3469 if (iseq_body->param.flags.use_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"use_block")),
Qtrue);
3473 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3475 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3476 VALUE insn = *seq++;
3477 int j,
len = insn_len(insn);
3482 for (j=0; j<
len-1; j++, seq++) {
3483 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3487 unsigned long idx = nseq - iseq_original + *seq;
3488 rb_ary_push(ary, register_label(labels_table, idx));
3500 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3502 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3513 const ID *ids = ((IC)*seq)->segments;
3532 VALUE e = rb_hash_new();
3533 int argc = vm_ci_argc(ci);
3535 ID mid = vm_ci_mid(ci);
3537 rb_hash_aset(e,
ID2SYM(rb_intern(
"flag")),
UINT2NUM(vm_ci_flag(ci)));
3539 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3544 argc -= kwarg->keyword_len;
3545 for (i = 0; i < kwarg->keyword_len; i++) {
3548 rb_hash_aset(e,
ID2SYM(rb_intern(
"kw_arg")), kw);
3551 rb_hash_aset(e,
ID2SYM(rb_intern(
"orig_argc")),
3565 rb_hash_foreach(hash, cdhash_each, val);
3569 unsigned long idx = nseq - iseq_original + pos;
3572 register_label(labels_table, idx));
3579#if SIZEOF_VALUE <= SIZEOF_LONG
3589 VALUE val = rb_hash_new();
3590#if SIZEOF_VALUE <= SIZEOF_LONG
3595 rb_hash_aset(val,
ID2SYM(rb_intern(
"func_ptr")), func_ptr);
3596 rb_hash_aset(val,
ID2SYM(rb_intern(
"argc")),
INT2NUM(((RB_BUILTIN)*seq)->argc));
3597 rb_hash_aset(val,
ID2SYM(rb_intern(
"index")),
INT2NUM(((RB_BUILTIN)*seq)->index));
3603 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3612 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3615 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3616 rb_ary_push(ary, exception_type2symbol(entry->type));
3618 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3623 rb_ary_push(ary, register_label(labels_table, entry->start));
3624 rb_ary_push(ary, register_label(labels_table, entry->end));
3625 rb_ary_push(ary, register_label(labels_table, entry->cont));
3632 prev_insn_info = NULL;
3633#ifdef USE_ISEQ_NODE_ID
3642 if (st_lookup(labels_table, pos, &label)) {
3646 info = get_insn_info(iseq, pos);
3647#ifdef USE_ISEQ_NODE_ID
3651 if (prev_insn_info != info) {
3652 int line = info->line_no;
3655 if (line > 0 && last_line != line) {
3659#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3669 prev_insn_info = info;
3673 pos += RARRAY_LENINT(ary);
3678 rb_hash_aset(misc,
ID2SYM(rb_intern(
"arg_size")),
INT2FIX(iseq_body->param.size));
3679 rb_hash_aset(misc,
ID2SYM(rb_intern(
"local_size")),
INT2FIX(iseq_body->local_table_size));
3680 rb_hash_aset(misc,
ID2SYM(rb_intern(
"stack_max")),
INT2FIX(iseq_body->stack_max));
3681 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_id")),
INT2FIX(iseq_body->location.node_id));
3682 rb_hash_aset(misc,
ID2SYM(rb_intern(
"code_location")),
3683 rb_ary_new_from_args(4,
3684 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3685 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3686 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3687 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3688#ifdef USE_ISEQ_NODE_ID
3689 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_ids")), node_ids);
3691 rb_hash_aset(misc,
ID2SYM(rb_intern(
"parser")), iseq_body->prism ?
ID2SYM(rb_intern(
"prism")) :
ID2SYM(rb_intern(
"parse.y")));
3719rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3723 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
3725 ID req, opt, rest, block, key, keyrest;
3726#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3727#define PARAM_ID(i) body->local_table[(i)]
3728#define PARAM(i, type) ( \
3730 PARAM_ID(i) != idItImplicit && rb_id2str(PARAM_ID(i)) ? \
3731 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3737 if (body->param.flags.forwardable) {
3748 for (i = 0; i < body->param.lead_num; i++) {
3753 for (i = 0; i < body->param.lead_num; i++) {
3757 r = body->param.lead_num + body->param.opt_num;
3758 for (; i < r; i++) {
3761 if (body->param.flags.has_rest) {
3763 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3765 r = body->param.post_start + body->param.post_num;
3767 for (i = body->param.post_start; i < r; i++) {
3772 for (i = body->param.post_start; i < r; i++) {
3776 if (body->param.flags.accepts_no_kwarg) {
3782 if (body->param.flags.has_kw) {
3784 if (keyword->required_num > 0) {
3787 for (; i < keyword->required_num; i++) {
3789 if (rb_id2str(keyword->table[i])) {
3796 for (; i < keyword->num; i++) {
3798 if (rb_id2str(keyword->table[i])) {
3804 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3807 PARAM_TYPE(keyrest);
3808 if (body->param.flags.has_kwrest &&
3809 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3812 else if (body->param.flags.ruby2_keywords) {
3817 if (body->param.flags.has_block) {
3819 rb_ary_push(args, PARAM(body->param.block_start, block));
3825rb_iseq_defined_string(
enum defined_type
type)
3827 static const char expr_names[][18] = {
3829 "instance-variable",
3845 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names)) rb_bug(
"unknown defined type %d",
type);
3846 estr = expr_names[
type - 1];
3847 return rb_fstring_cstr(estr);
3853static st_table *encoded_insn_data;
3857 void *notrace_encoded_insn;
3858 void *trace_encoded_insn;
3861 void *zjit_encoded_insn;
3864static insn_data_t insn_data[VM_BARE_INSTRUCTION_SIZE];
3867rb_free_encoded_insn_data(
void)
3869 st_free_table(encoded_insn_data);
3875rb_vm_encoded_insn_data_table_init(
void)
3877#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3878 const void *
const *table = rb_vm_get_insns_address_table();
3879#define INSN_CODE(insn) ((VALUE)table[insn])
3881#define INSN_CODE(insn) ((VALUE)(insn))
3883 encoded_insn_data = st_init_numtable_with_size(VM_BARE_INSTRUCTION_SIZE);
3885 for (
int insn = 0; insn < VM_BARE_INSTRUCTION_SIZE; insn++) {
3886 insn_data[insn].insn = insn;
3887 insn_data[insn].insn_len = insn_len(insn);
3892 int notrace_insn = (insn != BIN(opt_invokebuiltin_delegate_leave)) ? insn : BIN(opt_invokebuiltin_delegate);
3893 insn_data[insn].notrace_encoded_insn = (
void *)INSN_CODE(notrace_insn);
3894 insn_data[insn].trace_encoded_insn = (
void *)INSN_CODE(notrace_insn + VM_BARE_INSTRUCTION_SIZE);
3896 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3897 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_BARE_INSTRUCTION_SIZE);
3898 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3899 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3902 int zjit_insn = vm_bare_insn_to_zjit_insn(insn);
3903 insn_data[insn].zjit_insn = zjit_insn;
3904 insn_data[insn].zjit_encoded_insn = (insn != zjit_insn) ? (
void *)INSN_CODE(zjit_insn) : 0;
3906 if (insn != zjit_insn) {
3907 st_data_t key3 = (st_data_t)INSN_CODE(zjit_insn);
3908 st_add_direct(encoded_insn_data, key3, (st_data_t)&insn_data[insn]);
3918rb_vm_insn_addr2insn(
const void *addr)
3920 st_data_t key = (st_data_t)addr;
3923 if (st_lookup(encoded_insn_data, key, &val)) {
3924 insn_data_t *e = (insn_data_t *)val;
3925 return (
int)e->insn;
3928 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3934rb_vm_insn_addr2opcode(
const void *addr)
3936 st_data_t key = (st_data_t)addr;
3939 if (st_lookup(encoded_insn_data, key, &val)) {
3940 insn_data_t *e = (insn_data_t *)val;
3941 int opcode = e->insn;
3942 if (addr == e->trace_encoded_insn) {
3943 opcode += VM_BARE_INSTRUCTION_SIZE;
3946 else if (addr == e->zjit_encoded_insn) {
3947 opcode = e->zjit_insn;
3953 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3960rb_vm_insn_decode(
const VALUE encoded)
3962#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3963 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3965 int insn = (int)encoded;
3972encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_traced)
3974 st_data_t key = (st_data_t)*iseq_encoded_insn;
3977 if (st_lookup(encoded_insn_data, key, &val)) {
3978 insn_data_t *e = (insn_data_t *)val;
3979 if (remain_traced && key == (st_data_t)e->trace_encoded_insn) {
3982 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3986 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3991rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3994 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3995 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
4016iseq_add_local_tracepoint(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events,
VALUE tpval,
unsigned int target_line, rb_ractor_t *r)
4021 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4022 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4024 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
4025 ASSERT_vm_locking_with_barrier();
4027 for (pc=0; pc<body->iseq_size;) {
4031 unsigned int line = (int)entry->line_no;
4033 if (target_line == 0 || target_line == line) {
4040 if (pc_events & target_events) {
4043 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
4047 rb_hook_list_t *hook_list = rb_iseq_local_hooks(iseq, r,
true);
4048 rb_hook_list_connect_local_tracepoint(hook_list, tpval, target_line);
4049 iseq_mut->aux.exec.local_hooks_cnt++;
4058 unsigned int target_line;
4064iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4067 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line, data->r);
4068 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
4072rb_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)
4074 ASSERT_vm_locking_with_barrier();
4076 if (target_bmethod) {
4077 turnon_events = add_bmethod_events(turnon_events);
4079 data.turnon_events = turnon_events;
4081 data.target_line = target_line;
4083 data.r = GET_RACTOR();
4085 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
4086 if (0) fprintf(stderr,
"Iseq disasm:\n:%s", RSTRING_PTR(rb_iseq_disasm(iseq)));
4091iseq_remove_local_tracepoint(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4094 unsigned int num_hooks_left;
4097 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4098 rb_hook_list_t *hook_list;
4099 VALUE *iseq_encoded;
4100 ASSERT_vm_locking_with_barrier();
4102 hook_list = rb_iseq_local_hooks(iseq, r,
false);
4108 if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) {
4110 iseq_mut->aux.exec.local_hooks_cnt--;
4111 local_events = hook_list->events;
4112 num_hooks_left = rb_hook_list_count(hook_list);
4113 if (local_events == 0 && prev_events != 0) {
4114 st_delete(rb_ractor_targeted_hooks(r), (st_data_t*)&iseq, NULL);
4115 rb_hook_list_free(hook_list);
4118 if (iseq->aux.exec.local_hooks_cnt == num_hooks_left) {
4119 body = ISEQ_BODY(iseq);
4120 iseq_encoded = (
VALUE *)body->iseq_encoded;
4121 local_events = add_bmethod_events(local_events);
4122 for (pc = 0; pc<body->iseq_size;) {
4124 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
4141iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4144 data->n += iseq_remove_local_tracepoint(iseq, data->tpval, data->r);
4145 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
4149rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4152 ASSERT_vm_locking_with_barrier();
4157 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
4162rb_iseq_trace_set(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events)
4164 if (iseq->aux.exec.global_trace_events == turnon_events) {
4168 if (!ISEQ_EXECUTABLE_P(iseq)) {
4177 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4179 rb_hook_list_t *local_hooks = rb_iseq_local_hooks(iseq, GET_RACTOR(),
false);
4181 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
4182 enabled_events = add_bmethod_events(turnon_events | local_events);
4184 for (pc=0; pc<body->iseq_size;) {
4186 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
4194clear_attr_cc(
VALUE v)
4196 ASSERT_vm_locking_with_barrier();
4197 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((
const struct rb_callcache *)v)) {
4209 ASSERT_vm_locking_with_barrier();
4210 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((
const struct rb_callcache *)v)) {
4220clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4223 for (; v != (
VALUE)vend; v += stride) {
4224 void *
ptr = rb_asan_poisoned_object_p(v);
4225 rb_asan_unpoison_object(v,
false);
4227 asan_poison_object_if(
ptr, v);
4233rb_clear_attr_ccs(
void)
4237 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
4242clear_bf_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4245 for (; v != (
VALUE)vend; v += stride) {
4246 void *
ptr = rb_asan_poisoned_object_p(v);
4247 rb_asan_unpoison_object(v,
false);
4249 asan_poison_object_if(
ptr, v);
4255rb_clear_bf_ccs(
void)
4257 ASSERT_vm_locking_with_barrier();
4258 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
4262trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4267 for (; v != (
VALUE)vend; v += stride) {
4268 void *
ptr = rb_asan_poisoned_object_p(v);
4269 rb_asan_unpoison_object(v,
false);
4271 if (rb_obj_is_iseq(v)) {
4272 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
4274 else if (clear_attr_cc(v)) {
4276 else if (clear_bf_cc(v)) {
4279 asan_poison_object_if(
ptr, v);
4289 rb_objspace_each_objects(trace_set_i, &turnon_events);
4294rb_iseqw_local_variables(
VALUE iseqval)
4296 return rb_iseq_local_variables(iseqw_check(iseqval));
4317iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
4319 VALUE opt = !rb_check_arity(argc, 0, 1) ?
Qnil : argv[0];
4320 return rb_iseq_ibf_dump(iseqw_check(self), opt);
4337iseqw_s_load_from_binary(
VALUE self,
VALUE str)
4339 return iseqw_new(rb_iseq_ibf_load(str));
4349iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
4351 return rb_iseq_ibf_load_extra_data(str);
4354#if VM_INSN_INFO_TABLE_IMPL == 2
4387#define IMMEDIATE_TABLE_SIZE 54
4389struct succ_index_table {
4390 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4391 struct succ_dict_block {
4393 uint64_t small_block_ranks;
4394 uint64_t bits[512/64];
4395 } succ_part[FLEX_ARY_LEN];
4398#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4399#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4400#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4401#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4403static struct succ_index_table *
4404succ_index_table_create(
int max_pos,
int *data,
int size)
4406 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4407 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4408 struct succ_index_table *sd =
4409 rb_xcalloc_mul_add_mul(
4410 imm_size,
sizeof(uint64_t),
4411 succ_size,
sizeof(
struct succ_dict_block));
4415 for (j = 0; j < imm_size; j++) {
4416 for (i = 0; i < 9; i++) {
4417 if (r < size && data[r] == j * 9 + i) r++;
4418 imm_block_rank_set(sd->imm_part[j], i, r);
4421 for (k = 0; k < succ_size; k++) {
4422 struct succ_dict_block *sd_block = &sd->succ_part[k];
4425 for (j = 0; j < 8; j++) {
4427 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4428 for (i = 0; i < 64; i++) {
4429 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4430 bits |= ((uint64_t)1) << i;
4434 sd_block->bits[j] = bits;
4435 small_rank += rb_popcount64(bits);
4441static unsigned int *
4442succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
4444 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4445 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4446 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
4447 int i, j, k, r = -1;
4449 for (j = 0; j < imm_size; j++) {
4450 for (i = 0; i < 9; i++) {
4451 int nr = imm_block_rank_get(sd->imm_part[j], i);
4452 if (r != nr) *p++ = j * 9 + i;
4456 for (k = 0; k < succ_size; k++) {
4457 for (j = 0; j < 8; j++) {
4458 for (i = 0; i < 64; i++) {
4459 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4460 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4469succ_index_lookup(
const struct succ_index_table *sd,
int x)
4471 if (x < IMMEDIATE_TABLE_SIZE) {
4472 const int i = x / 9;
4473 const int j = x % 9;
4474 return imm_block_rank_get(sd->imm_part[i], j);
4477 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4478 const struct succ_dict_block *block = &sd->succ_part[block_index];
4479 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4480 const int small_block_index = block_bit_index / 64;
4481 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4482 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4484 return block->rank + small_block_popcount + popcnt;
4503iseqw_script_lines(
VALUE self)
4505 const rb_iseq_t *iseq = iseqw_check(self);
4506 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_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 RUBY_EVENT_RESCUE
Encountered a rescue statement.
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 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 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_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eTypeError
TypeError exception.
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.
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_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_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.
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_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)
Like rb_str_dup(), but always create an instance of rb_cString regardless of the given object's class...
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.
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.
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
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.
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
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_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.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a 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.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
uint32_t node_id
The unique identifier for this node, which is deterministic based on the source.
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...
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.
ruby_value_type
C-level type of an object.