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/ruby_parser.h"
32#include "internal/sanitizers.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
40#include "vm_callinfo.h"
45#include "insns_info.inc"
48static VALUE iseqw_new(
const rb_iseq_t *iseq);
49static const rb_iseq_t *iseqw_check(
VALUE iseqw);
51#if VM_INSN_INFO_TABLE_IMPL == 2
52static struct succ_index_table *succ_index_table_create(
int max_pos,
int *data,
int size);
53static unsigned int *succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size);
54static int succ_index_lookup(
const struct succ_index_table *sd,
int x);
57#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
60obj_resurrect(
VALUE obj)
62 if (hidden_obj_p(obj)) {
68 obj = rb_ary_resurrect(obj);
71 obj = rb_hash_resurrect(obj);
96 free_arena(compile_data->node.storage_head);
97 free_arena(compile_data->insn.storage_head);
98 if (compile_data->ivar_cache_table) {
99 rb_id_table_free(compile_data->ivar_cache_table);
101 ruby_xfree(compile_data);
106remove_from_constant_cache(
ID id, IC ic)
108 rb_vm_t *vm = GET_VM();
110 st_data_t ic_data = (st_data_t)ic;
112 if (rb_id_table_lookup(vm->constant_cache,
id, &lookup_result)) {
113 st_table *ics = (st_table *)lookup_result;
114 st_delete(ics, &ic_data, NULL);
116 if (ics->num_entries == 0 &&
118 id != vm->inserting_constant_cache_id) {
119 rb_id_table_delete(vm->constant_cache,
id);
129iseq_clear_ic_references(
const rb_iseq_t *iseq)
135 if (!ISEQ_BODY(iseq)->is_entries) {
139 for (
unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
140 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
148 if (segments == NULL)
151 for (
int i = 0; segments[i]; i++) {
153 if (
id == idNULL)
continue;
154 remove_from_constant_cache(
id, ic);
157 ruby_xfree((
void *)segments);
162rb_iseq_free(
const rb_iseq_t *iseq)
164 RUBY_FREE_ENTER(
"iseq");
166 if (iseq && ISEQ_BODY(iseq)) {
167 iseq_clear_ic_references(iseq);
169 rb_rjit_free_iseq(iseq);
171 rb_yjit_iseq_free(body->yjit_payload);
174 rb_yjit_live_iseq_count--;
177 ruby_xfree((
void *)body->iseq_encoded);
178 ruby_xfree((
void *)body->insns_info.body);
179 ruby_xfree((
void *)body->insns_info.positions);
180#if VM_INSN_INFO_TABLE_IMPL == 2
181 ruby_xfree(body->insns_info.succ_index_table);
183 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
184 ruby_xfree((
void *)body->local_table);
185 ruby_xfree((
void *)body->is_entries);
187 if (body->call_data) {
188 ruby_xfree(body->call_data);
190 ruby_xfree((
void *)body->catch_table);
191 ruby_xfree((
void *)body->
param.opt_table);
192 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
193 ruby_xfree((
void *)body->mark_bits.list);
196 ruby_xfree(body->variable.original_iseq);
198 if (body->
param.keyword != NULL) {
199 if (body->
param.keyword->table != &body->local_table[body->
param.keyword->bits_start - body->
param.keyword->num])
200 ruby_xfree((
void *)body->
param.keyword->table);
201 ruby_xfree((
void *)body->
param.keyword->default_values);
202 ruby_xfree((
void *)body->
param.keyword);
204 compile_data_free(ISEQ_COMPILE_DATA(iseq));
205 if (body->outer_variables) rb_id_table_free(body->outer_variables);
209 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
210 rb_hook_list_free(iseq->aux.exec.local_hooks);
213 RUBY_FREE_LEAVE(
"iseq");
216typedef VALUE iseq_value_itr_t(
void *ctx,
VALUE obj);
219iseq_scan_bits(
unsigned int page, iseq_bits_t bits,
VALUE *code,
VALUE *original_iseq)
222 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
225 offset = ntz_intptr(bits);
226 VALUE op = code[page_offset + offset];
227 rb_gc_mark_and_move(&code[page_offset + offset]);
228 VALUE newop = code[page_offset + offset];
229 if (original_iseq && newop != op) {
230 original_iseq[page_offset + offset] = newop;
237rb_iseq_mark_and_move_each_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
243 size = body->iseq_size;
244 code = body->iseq_encoded;
248 if (body->is_entries) {
250 is_entries += body->ivc_size;
253 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
254 ICVARC icvarc = (ICVARC)is_entries;
258 rb_gc_mark_and_move(&icvarc->entry->class_value);
263 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
265 if (is->once.value) {
266 rb_gc_mark_and_move(&is->once.value);
271 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
272 IC ic = (IC)is_entries;
274 rb_gc_mark_and_move_ptr(&ic->entry);
280 if (body->mark_bits.list) {
281 if (ISEQ_MBITS_BUFLEN(size) == 1) {
282 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
285 if (body->mark_bits.list) {
286 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
287 iseq_bits_t bits = body->mark_bits.list[i];
288 iseq_scan_bits(i, bits, code, original_iseq);
296cc_is_active(
const struct rb_callcache *cc,
bool reference_updating)
299 if (reference_updating) {
303 if (vm_cc_markable(cc)) {
306 if (reference_updating) {
309 if (!METHOD_ENTRY_INVALIDATED(cme)) {
319rb_iseq_mark_and_move(rb_iseq_t *iseq,
bool reference_updating)
321 RUBY_MARK_ENTER(
"iseq");
323 rb_gc_mark_and_move(&iseq->wrapper);
325 if (ISEQ_BODY(iseq)) {
328 rb_iseq_mark_and_move_each_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
330 rb_gc_mark_and_move(&body->variable.coverage);
331 rb_gc_mark_and_move(&body->variable.pc2branchindex);
332 rb_gc_mark_and_move(&body->variable.script_lines);
333 rb_gc_mark_and_move(&body->location.label);
334 rb_gc_mark_and_move(&body->location.base_label);
335 rb_gc_mark_and_move(&body->location.pathobj);
336 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
337 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
338 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
340 if (body->call_data) {
341 for (
unsigned int i = 0; i < body->ci_size; i++) {
344 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
346 if (cc_is_active(cds[i].cc, reference_updating)) {
347 rb_gc_mark_and_move_ptr(&cds[i].cc);
350 cds[i].cc = rb_vm_empty_cc();
355 if (body->
param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
356 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
358 for (
int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
359 rb_gc_mark_and_move(&keyword->default_values[j]);
363 if (body->catch_table) {
366 for (
unsigned int i = 0; i < table->size; i++) {
368 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
370 rb_gc_mark_and_move_ptr(&entry->iseq);
375 if (reference_updating) {
377 rb_rjit_iseq_update_references(body);
380 rb_yjit_iseq_update_references(body->yjit_payload);
385 rb_rjit_iseq_mark(body->rjit_blocks);
388 rb_yjit_iseq_mark(body->yjit_payload);
394 rb_gc_mark_and_move(&iseq->aux.loader.obj);
399 if (!reference_updating) {
405 rb_iseq_mark_and_pin_insn_storage(compile_data->insn.storage_head);
408 rb_gc_mark_and_move((
VALUE *)&compile_data->err_info);
409 rb_gc_mark_and_move((
VALUE *)&compile_data->catch_table_ary);
413 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
415 if (iseq->aux.exec.local_hooks) {
416 rb_hook_list_mark_and_update(iseq->aux.exec.local_hooks);
420 RUBY_MARK_LEAVE(
"iseq");
424param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
428 if (!pkw)
return size;
430 size +=
sizeof(
struct rb_iseq_param_keyword);
431 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
437rb_iseq_memsize(
const rb_iseq_t *iseq)
445 if (ISEQ_EXECUTABLE_P(iseq) && body) {
447 size += body->iseq_size *
sizeof(
VALUE);
449 size += body->local_table_size *
sizeof(
ID);
450 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
451 if (body->catch_table) {
452 size += iseq_catch_table_bytes(body->catch_table->size);
454 size += (body->
param.opt_num + 1) *
sizeof(
VALUE);
455 size += param_keyword_size(body->
param.keyword);
460 if (ISEQ_BODY(iseq)->is_entries) {
462 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
463 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
478 compile_data = ISEQ_COMPILE_DATA(iseq);
484 cur = compile_data->node.storage_head;
495rb_iseq_constant_body_alloc(
void)
505 rb_iseq_t *iseq = iseq_imemo_alloc();
506 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
511rb_iseq_pathobj_new(
VALUE path,
VALUE realpath)
514 VM_ASSERT(RB_TYPE_P(path,
T_STRING));
517 if (path == realpath ||
519 pathobj = rb_fstring(path);
522 if (!
NIL_P(realpath)) realpath = rb_fstring(realpath);
523 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
524 rb_obj_freeze(pathobj);
530rb_iseq_pathobj_set(
const rb_iseq_t *iseq,
VALUE path,
VALUE realpath)
533 rb_iseq_pathobj_new(path, realpath));
536static rb_iseq_location_t *
537iseq_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)
539 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
541 rb_iseq_pathobj_set(iseq, path, realpath);
544 loc->first_lineno = first_lineno;
546 loc->node_id = node_id;
547 loc->code_location = *code_location;
550 loc->code_location.beg_pos.lineno = 0;
551 loc->code_location.beg_pos.column = 0;
552 loc->code_location.end_pos.lineno = -1;
553 loc->code_location.end_pos.column = -1;
560set_relation(rb_iseq_t *iseq,
const rb_iseq_t *piseq)
566 if (
type == ISEQ_TYPE_TOP) {
567 body->local_iseq = iseq;
569 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
570 body->local_iseq = iseq;
573 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
577 body->parent_iseq = piseq;
580 if (
type == ISEQ_TYPE_MAIN) {
581 body->local_iseq = iseq;
590 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
595 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
601prepare_iseq_build(rb_iseq_t *iseq,
602 VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_code_location_t *code_location,
const int node_id,
603 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
604 VALUE script_lines,
const rb_compile_option_t *option)
610 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
614 set_relation(iseq, parent);
616 name = rb_fstring(name);
617 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
618 if (iseq != body->local_iseq) {
619 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
621 ISEQ_COVERAGE_SET(iseq,
Qnil);
622 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
623 body->variable.flip_count = 0;
625 if (
NIL_P(script_lines)) {
629 RB_OBJ_WRITE(iseq, &body->variable.script_lines, rb_ractor_make_shareable(script_lines));
632 ISEQ_COMPILE_DATA_ALLOC(iseq);
633 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
636 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
637 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
638 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
639 ISEQ_COMPILE_DATA(iseq)->option = option;
640 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
641 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
643 if (option->coverage_enabled) {
644 VALUE coverages = rb_get_coverages();
645 if (
RTEST(coverages)) {
646 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
650 ISEQ_COVERAGE_SET(iseq, coverage);
651 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
652 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_hidden_new(0));
657#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
658static void validate_get_insn_info(
const rb_iseq_t *iseq);
662rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
664#if VM_INSN_INFO_TABLE_IMPL == 2
667 int size = body->insns_info.size;
668 int max_pos = body->iseq_size;
669 int *data = (
int *)body->insns_info.positions;
670 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
671 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
672#if VM_CHECK_MODE == 0
673 ruby_xfree(body->insns_info.positions);
674 body->insns_info.positions = NULL;
679#if VM_INSN_INFO_TABLE_IMPL == 2
683 int size = body->insns_info.size;
684 int max_pos = body->iseq_size;
685 struct succ_index_table *sd = body->insns_info.succ_index_table;
686 return succ_index_table_invert(max_pos, sd, size);
691rb_iseq_init_trace(rb_iseq_t *iseq)
693 iseq->aux.exec.global_trace_events = 0;
694 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
695 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
700finish_iseq_build(rb_iseq_t *iseq)
704 VALUE err = data->err_info;
705 ISEQ_COMPILE_DATA_CLEAR(iseq);
706 compile_data_free(data);
708#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
709 validate_get_insn_info(iseq);
713 VALUE path = pathobj_path(body->location.pathobj);
715 rb_funcallv(err, rb_intern(
"set_backtrace"), 1, &path);
719 RB_DEBUG_COUNTER_INC(iseq_num);
720 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
722 rb_iseq_init_trace(iseq);
726static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
727 OPT_INLINE_CONST_CACHE,
728 OPT_PEEPHOLE_OPTIMIZATION,
729 OPT_TAILCALL_OPTIMIZATION,
730 OPT_SPECIALISED_INSTRUCTION,
731 OPT_OPERANDS_UNIFICATION,
732 OPT_INSTRUCTIONS_UNIFICATION,
733 OPT_FROZEN_STRING_LITERAL,
734 OPT_DEBUG_FROZEN_STRING_LITERAL,
738static const rb_compile_option_t COMPILE_OPTION_FALSE = {0};
741set_compile_option_from_hash(rb_compile_option_t *option,
VALUE opt)
743#define SET_COMPILE_OPTION(o, h, mem) \
744 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
745 if (flag == Qtrue) { (o)->mem = 1; } \
746 else if (flag == Qfalse) { (o)->mem = 0; } \
748#define SET_COMPILE_OPTION_NUM(o, h, mem) \
749 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
750 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
752 SET_COMPILE_OPTION(option, opt, inline_const_cache);
753 SET_COMPILE_OPTION(option, opt, peephole_optimization);
754 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
755 SET_COMPILE_OPTION(option, opt, specialized_instruction);
756 SET_COMPILE_OPTION(option, opt, operands_unification);
757 SET_COMPILE_OPTION(option, opt, instructions_unification);
758 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
759 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
760 SET_COMPILE_OPTION(option, opt, coverage_enabled);
761 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
762#undef SET_COMPILE_OPTION
763#undef SET_COMPILE_OPTION_NUM
766static rb_compile_option_t *
767set_compile_option_from_ast(rb_compile_option_t *option,
const rb_ast_body_t *ast)
769#define SET_COMPILE_OPTION(o, a, mem) \
770 ((a)->mem < 0 ? 0 : ((o)->mem = (a)->mem > 0))
771 SET_COMPILE_OPTION(option, ast, frozen_string_literal);
772 SET_COMPILE_OPTION(option, ast, coverage_enabled);
773#undef SET_COMPILE_OPTION
778make_compile_option(rb_compile_option_t *option,
VALUE opt)
781 *option = COMPILE_OPTION_DEFAULT;
784 *option = COMPILE_OPTION_FALSE;
786 else if (opt ==
Qtrue) {
788 for (i = 0; i < (int)(
sizeof(rb_compile_option_t) /
sizeof(int)); ++i)
789 ((
int *)option)[i] = 1;
791 else if (RB_TYPE_P(opt,
T_HASH)) {
792 *option = COMPILE_OPTION_DEFAULT;
793 set_compile_option_from_hash(option, opt);
796 rb_raise(
rb_eTypeError,
"Compile option must be Hash/true/false/nil");
801make_compile_option_value(rb_compile_option_t *option)
803 VALUE opt = rb_hash_new_with_size(11);
804#define SET_COMPILE_OPTION(o, h, mem) \
805 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
806#define SET_COMPILE_OPTION_NUM(o, h, mem) \
807 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
809 SET_COMPILE_OPTION(option, opt, inline_const_cache);
810 SET_COMPILE_OPTION(option, opt, peephole_optimization);
811 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
812 SET_COMPILE_OPTION(option, opt, specialized_instruction);
813 SET_COMPILE_OPTION(option, opt, operands_unification);
814 SET_COMPILE_OPTION(option, opt, instructions_unification);
815 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
816 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
817 SET_COMPILE_OPTION(option, opt, coverage_enabled);
818 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
820#undef SET_COMPILE_OPTION
821#undef SET_COMPILE_OPTION_NUM
826rb_iseq_new(
const rb_ast_body_t *ast,
VALUE name,
VALUE path,
VALUE realpath,
827 const rb_iseq_t *parent,
enum rb_iseq_type
type)
829 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent,
830 0,
type, &COMPILE_OPTION_DEFAULT);
834ast_line_count(
const rb_ast_body_t *ast)
836 if (ast->script_lines ==
Qfalse) {
840 if (RB_TYPE_P(ast->script_lines,
T_ARRAY)){
843 return FIX2INT(ast->script_lines);
847iseq_setup_coverage(
VALUE coverages,
VALUE path,
const rb_ast_body_t *ast,
int line_offset)
849 int line_count = line_offset + ast_line_count(ast);
851 if (line_count >= 0) {
852 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
854 VALUE coverage = rb_default_coverage(
len);
855 rb_hash_aset(coverages, path, coverage);
864iseq_new_setup_coverage(
VALUE path,
const rb_ast_body_t *ast,
int line_offset)
866 VALUE coverages = rb_get_coverages();
868 if (
RTEST(coverages)) {
869 iseq_setup_coverage(coverages, path, ast, line_offset);
874rb_iseq_new_top(
const rb_ast_body_t *ast,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent)
876 iseq_new_setup_coverage(path, ast, 0);
878 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent, 0,
879 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
883rb_iseq_new_main(
const rb_ast_body_t *ast,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt)
885 iseq_new_setup_coverage(path, ast, 0);
887 return rb_iseq_new_with_opt(ast, rb_fstring_lit(
"<main>"),
889 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
893rb_iseq_new_eval(
const rb_ast_body_t *ast,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
895 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
896 VALUE coverages = rb_get_coverages();
897 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
898 iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
902 return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
903 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
906static inline rb_iseq_t *
907iseq_translate(rb_iseq_t *iseq)
910 VALUE v1 = iseqw_new(iseq);
912 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
913 iseq = (rb_iseq_t *)iseqw_check(v2);
921rb_iseq_new_with_opt(
const rb_ast_body_t *ast,
VALUE name,
VALUE path,
VALUE realpath,
922 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
923 enum rb_iseq_type
type,
const rb_compile_option_t *option)
925 const NODE *node = ast ? ast->root : 0;
927 rb_iseq_t *iseq = iseq_alloc();
928 rb_compile_option_t new_opt;
930 if (!option) option = &COMPILE_OPTION_DEFAULT;
933 option = set_compile_option_from_ast(&new_opt, ast);
938 if (ast && !
FIXNUM_P(ast->script_lines) && ast->script_lines) {
939 script_lines = ast->script_lines;
942 script_lines = ISEQ_BODY(parent)->variable.script_lines;
945 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
946 parent, isolated_depth,
type, script_lines, option);
948 rb_iseq_compile_node(iseq, node);
949 finish_iseq_build(iseq);
951 return iseq_translate(iseq);
954VALUE rb_iseq_compile_prism_node(rb_iseq_t * iseq, pm_scope_node_t *scope_node,
pm_parser_t *parser);
965 *code_location = (rb_code_location_t) {
966 .beg_pos = { .lineno = (int) start.
line, .column = (
int) start.
column },
967 .end_pos = { .lineno = (int) end.
line, .column = (
int) end.
column }
973 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
974 enum rb_iseq_type
type,
const rb_compile_option_t *option)
976 rb_iseq_t *iseq = iseq_alloc();
978 if (!option) option = &COMPILE_OPTION_DEFAULT;
980 rb_code_location_t code_location;
985 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node_id,
986 parent, isolated_depth,
type, script_lines, option);
988 rb_iseq_compile_prism_node(iseq, scope_node, parser);
989 finish_iseq_build(iseq);
991 return iseq_translate(iseq);
995rb_iseq_new_with_callback(
998 int first_lineno,
const rb_iseq_t *parent,
999 enum rb_iseq_type
type,
const rb_compile_option_t *option)
1002 rb_iseq_t *iseq = iseq_alloc();
1004 if (!option) option = &COMPILE_OPTION_DEFAULT;
1005 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1007 rb_iseq_compile_callback(iseq, ifunc);
1008 finish_iseq_build(iseq);
1014rb_iseq_load_iseq(
VALUE fname)
1019 return iseqw_check(iseqv);
1025#define CHECK_ARRAY(v) rb_to_array_type(v)
1026#define CHECK_HASH(v) rb_to_hash_type(v)
1027#define CHECK_STRING(v) rb_str_to_str(v)
1028#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1031static enum rb_iseq_type
1034 const ID id_top = rb_intern(
"top");
1035 const ID id_method = rb_intern(
"method");
1036 const ID id_block = rb_intern(
"block");
1037 const ID id_class = rb_intern(
"class");
1038 const ID id_rescue = rb_intern(
"rescue");
1039 const ID id_ensure = rb_intern(
"ensure");
1040 const ID id_eval = rb_intern(
"eval");
1041 const ID id_main = rb_intern(
"main");
1042 const ID id_plain = rb_intern(
"plain");
1046 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1047 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1048 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1049 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1050 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1051 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1052 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1053 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1054 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1055 return (
enum rb_iseq_type)-1;
1059iseq_load(
VALUE data,
const rb_iseq_t *parent,
VALUE opt)
1061 rb_iseq_t *iseq = iseq_alloc();
1063 VALUE magic, version1, version2, format_type, misc;
1064 VALUE name, path, realpath, code_location, node_id;
1065 VALUE type, body, locals, params, exception;
1067 st_data_t iseq_type;
1068 rb_compile_option_t option;
1070 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1077 data = CHECK_ARRAY(data);
1079 magic = CHECK_STRING(rb_ary_entry(data, i++));
1080 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1081 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1082 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1083 misc = CHECK_HASH(rb_ary_entry(data, i++));
1084 ((void)magic, (void)version1, (void)version2, (void)format_type);
1086 name = CHECK_STRING(rb_ary_entry(data, i++));
1087 path = CHECK_STRING(rb_ary_entry(data, i++));
1088 realpath = rb_ary_entry(data, i++);
1089 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1090 int first_lineno =
RB_NUM2INT(rb_ary_entry(data, i++));
1092 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1093 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1094 params = CHECK_HASH(rb_ary_entry(data, i++));
1095 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1096 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1098 ISEQ_BODY(iseq)->local_iseq = iseq;
1100 iseq_type = iseq_type_from_sym(
type);
1101 if (iseq_type == (
enum rb_iseq_type)-1) {
1105 node_id = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_id")));
1107 code_location = rb_hash_aref(misc,
ID2SYM(rb_intern(
"code_location")));
1109 tmp_loc.beg_pos.lineno =
NUM2INT(rb_ary_entry(code_location, 0));
1110 tmp_loc.beg_pos.column =
NUM2INT(rb_ary_entry(code_location, 1));
1111 tmp_loc.end_pos.lineno =
NUM2INT(rb_ary_entry(code_location, 2));
1112 tmp_loc.end_pos.column =
NUM2INT(rb_ary_entry(code_location, 3));
1115 make_compile_option(&option, opt);
1116 option.peephole_optimization = FALSE;
1117 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1118 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1120 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1122 finish_iseq_build(iseq);
1124 return iseqw_new(iseq);
1131iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1135 return iseq_load(data, NULL, opt);
1141 return iseq_load(data,
RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1147 rb_iseq_t *iseq = NULL;
1148 rb_compile_option_t option;
1149#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1150# define INITIALIZED volatile
1154 rb_ast_t *(*parse)(
VALUE vparser,
VALUE fname,
VALUE file,
int start);
1156 rb_ast_t *INITIALIZED ast;
1157 VALUE name = rb_fstring_lit(
"<compiled>");
1160 make_compile_option(&option, opt);
1163 if (RB_TYPE_P(src,
T_FILE)) {
1164 parse = rb_parser_compile_file_path;
1167 parse = rb_parser_compile_string_path;
1171 const VALUE parser = rb_parser_new();
1172 const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1174 rb_parser_set_context(parser, outer_scope, FALSE);
1175 rb_parser_set_script_lines(parser, RBOOL(ruby_vm_keep_script_lines));
1177 ast = (*parse)(parser, file, src, ln);
1180 if (!ast->body.root) {
1181 rb_ast_dispose(ast);
1182 rb_exc_raise(GET_EC()->errinfo);
1185 iseq = rb_iseq_new_with_opt(&ast->body, name, file, realpath, ln,
1186 NULL, 0, ISEQ_TYPE_TOP, &option);
1187 rb_ast_dispose(ast);
1194rb_iseq_path(
const rb_iseq_t *iseq)
1196 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1200rb_iseq_realpath(
const rb_iseq_t *iseq)
1202 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1206rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1208 return rb_iseq_realpath(iseq);
1212rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1214 return NIL_P(rb_iseq_realpath(iseq));
1218rb_iseq_label(
const rb_iseq_t *iseq)
1220 return ISEQ_BODY(iseq)->location.label;
1224rb_iseq_base_label(
const rb_iseq_t *iseq)
1226 return ISEQ_BODY(iseq)->location.base_label;
1230rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1232 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1236rb_iseq_method_name(
const rb_iseq_t *iseq)
1240 if (body->type == ISEQ_TYPE_METHOD) {
1241 return body->location.base_label;
1249rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1251 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1252 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1253 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1254 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1255 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1258static ID iseq_type_id(
enum rb_iseq_type
type);
1261rb_iseq_type(
const rb_iseq_t *iseq)
1263 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1267rb_iseq_coverage(
const rb_iseq_t *iseq)
1269 return ISEQ_COVERAGE(iseq);
1273remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1276 for (; v != (
VALUE)vend; v += stride) {
1277 void *ptr = asan_poisoned_object_p(v);
1278 asan_unpoison_object(v,
false);
1280 if (rb_obj_is_iseq(v)) {
1281 rb_iseq_t *iseq = (rb_iseq_t *)v;
1282 ISEQ_COVERAGE_SET(iseq,
Qnil);
1285 asan_poison_object_if(ptr, v);
1291rb_iseq_remove_coverage_all(
void)
1293 rb_objspace_each_objects(remove_coverage_i, NULL);
1299iseqw_mark(
void *ptr)
1301 rb_gc_mark((
VALUE)ptr);
1305iseqw_memsize(
const void *ptr)
1307 return rb_iseq_memsize((
const rb_iseq_t *)ptr);
1312 {iseqw_mark, NULL, iseqw_memsize,},
1313 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1317iseqw_new(
const rb_iseq_t *iseq)
1319 if (iseq->wrapper) {
1320 return iseq->wrapper;
1323 union {
const rb_iseq_t *in;
void *out; } deconst;
1338rb_iseqw_new(
const rb_iseq_t *iseq)
1340 return iseqw_new(iseq);
1381iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1387 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1389 case 5: opt = argv[--i];
1390 case 4: line = argv[--i];
1391 case 3: path = argv[--i];
1392 case 2: file = argv[--i];
1395 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1396 if (
NIL_P(path)) path = file;
1402 return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
1409 rb_code_location_t code_location;
1412 rb_compile_option_t option;
1413 make_compile_option(&option, opt);
1414 prepare_iseq_build(iseq, rb_fstring_lit(
"<compiled>"), file, path, first_lineno, &code_location, -1, NULL, 0, ISEQ_TYPE_TOP,
Qnil, &option);
1416 pm_scope_node_t scope_node;
1417 pm_scope_node_init(node, &scope_node, NULL, parser);
1418 rb_iseq_compile_prism_node(iseq, &scope_node, parser);
1420 finish_iseq_build(iseq);
1421 pm_node_destroy(parser, node);
1425iseqw_s_compile_prism(
int argc,
VALUE *argv,
VALUE self)
1431 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1433 case 5: opt = argv[--i];
1434 case 4: line = argv[--i];
1435 case 3: path = argv[--i];
1436 case 2: file = argv[--i];
1439 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1440 if (
NIL_P(path)) path = file;
1447 pm_options_filepath_set(&options, RSTRING_PTR(file));
1449 int start_line =
NUM2INT(line);
1450 pm_options_line_set(&options, start_line);
1454 if (RB_TYPE_P(src,
T_FILE)) {
1456 file = rb_fstring(src);
1459 pm_string_mapped_init(&input, RSTRING_PTR(file));
1461 pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options);
1464 pm_parser_init(&parser, (
const uint8_t *) RSTRING_PTR(src), RSTRING_LEN(src), &options);
1467 rb_iseq_t *iseq = iseq_alloc();
1468 iseqw_s_compile_prism_compile(&parser, opt, iseq, file, path, start_line);
1469 pm_parser_free(&parser);
1470 pm_options_free(&options);
1472 return iseqw_new(iseq);
1476iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self)
1481 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1482 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1484 case 2: opt = argv[--i];
1487 file = rb_fstring(file);
1490 pm_string_mapped_init(&input, RSTRING_PTR(file));
1493 pm_options_filepath_set(&options, RSTRING_PTR(file));
1496 pm_parser_init(&parser, pm_string_source(&input), pm_string_length(&input), &options);
1498 rb_iseq_t *iseq = iseq_alloc();
1499 iseqw_s_compile_prism_compile(&parser, opt, iseq, file, rb_realpath_internal(
Qnil, file, 1), 1);
1500 pm_parser_free(&parser);
1501 pm_string_free(&input);
1502 pm_options_free(&options);
1504 return iseqw_new(iseq);
1511 pm_parser_init(&parser, pm_string_source(input), pm_string_length(input), options);
1513 if (
NIL_P(path)) path = rb_fstring_lit(
"<compiled>");
1515 pm_options_line_set(options, start_line);
1517 rb_iseq_t *iseq = iseq_alloc();
1518 iseqw_s_compile_prism_compile(&parser,
Qnil, iseq, path, path, start_line);
1520 pm_parser_free(&parser);
1545iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1550 rb_compile_option_t option;
1553 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1554 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1556 case 2: opt = argv[--i];
1559 file = rb_fstring(file);
1563 rb_execution_context_t *ec = GET_EC();
1564 VALUE v = rb_vm_push_frame_fname(ec, file);
1566 parser = rb_parser_new();
1567 rb_parser_set_context(parser, NULL, FALSE);
1568 ast = (rb_ast_t *)rb_parser_load_file(parser, file);
1569 if (!ast->body.root) exc = GET_EC()->errinfo;
1572 if (!ast->body.root) {
1573 rb_ast_dispose(ast);
1577 make_compile_option(&option, opt);
1579 ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit(
"<main>"),
1581 rb_realpath_internal(
Qnil, file, 1),
1582 1, NULL, 0, ISEQ_TYPE_TOP, &option));
1583 rb_ast_dispose(ast);
1585 rb_vm_pop_frame(ec);
1621iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
1623 rb_compile_option_t option;
1624 make_compile_option(&option, opt);
1625 COMPILE_OPTION_DEFAULT = option;
1638iseqw_s_compile_option_get(
VALUE self)
1640 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1643static const rb_iseq_t *
1644iseqw_check(
VALUE iseqw)
1648 if (!ISEQ_BODY(iseq)) {
1649 rb_ibf_load_iseq_complete(iseq);
1652 if (!ISEQ_BODY(iseq)->location.label) {
1653 rb_raise(
rb_eTypeError,
"uninitialized InstructionSequence");
1659rb_iseqw_to_iseq(
VALUE iseqw)
1661 return iseqw_check(iseqw);
1673iseqw_eval(
VALUE self)
1675 return rb_iseq_eval(iseqw_check(self));
1683iseqw_inspect(
VALUE self)
1685 const rb_iseq_t *iseq = iseqw_check(self);
1689 if (!body->location.label) {
1690 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
1693 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
1695 body->location.label, rb_iseq_path(iseq),
1696 FIX2INT(rb_iseq_first_lineno(iseq)));
1724iseqw_path(
VALUE self)
1726 return rb_iseq_path(iseqw_check(self));
1746iseqw_absolute_path(
VALUE self)
1748 return rb_iseq_realpath(iseqw_check(self));
1775iseqw_label(
VALUE self)
1777 return rb_iseq_label(iseqw_check(self));
1801iseqw_base_label(
VALUE self)
1803 return rb_iseq_base_label(iseqw_check(self));
1817iseqw_first_lineno(
VALUE self)
1819 return rb_iseq_first_lineno(iseqw_check(self));
1822static VALUE iseq_data_to_ary(
const rb_iseq_t *iseq);
1906iseqw_to_a(
VALUE self)
1908 const rb_iseq_t *iseq = iseqw_check(self);
1909 return iseq_data_to_ary(iseq);
1912#if VM_INSN_INFO_TABLE_IMPL == 1
1914get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
1917 size_t size = body->insns_info.size;
1919 const unsigned int *positions = body->insns_info.positions;
1920 const int debug = 0;
1923 printf(
"size: %"PRIuSIZE
"\n", size);
1924 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1925 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1931 else if (size == 1) {
1932 return &insns_info[0];
1935 size_t l = 1, r = size - 1;
1937 size_t m = l + (r - l) / 2;
1938 if (positions[m] == pos) {
1939 return &insns_info[m];
1941 if (positions[m] < pos) {
1949 return &insns_info[size-1];
1951 if (positions[l] > pos) {
1952 return &insns_info[l-1];
1954 return &insns_info[l];
1959get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1961 return get_insn_info_binary_search(iseq, pos);
1965#if VM_INSN_INFO_TABLE_IMPL == 2
1967get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
1970 size_t size = body->insns_info.size;
1972 const int debug = 0;
1975#if VM_CHECK_MODE > 0
1976 const unsigned int *positions = body->insns_info.positions;
1977 printf(
"size: %"PRIuSIZE
"\n", size);
1978 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1979 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1981 printf(
"size: %"PRIuSIZE
"\n", size);
1982 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
1983 (
size_t)0, insns_info[0].line_no, pos);
1990 else if (size == 1) {
1991 return &insns_info[0];
1995 VM_ASSERT(body->insns_info.succ_index_table != NULL);
1996 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
1997 return &insns_info[index-1];
2002get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2004 return get_insn_info_succinct_bitvector(iseq, pos);
2008#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2010get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
2013 size_t i = 0, size = body->insns_info.size;
2015 const unsigned int *positions = body->insns_info.positions;
2016 const int debug = 0;
2019 printf(
"size: %"PRIuSIZE
"\n", size);
2020 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2021 i, positions[i], insns_info[i].line_no, pos);
2027 else if (size == 1) {
2028 return &insns_info[0];
2031 for (i=1; i<size; i++) {
2032 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2033 i, positions[i], insns_info[i].line_no, pos);
2035 if (positions[i] == pos) {
2036 return &insns_info[i];
2038 if (positions[i] > pos) {
2039 return &insns_info[i-1];
2043 return &insns_info[i-1];
2047#if VM_INSN_INFO_TABLE_IMPL == 0
2049get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2051 return get_insn_info_linear_search(iseq, pos);
2055#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2057validate_get_insn_info(
const rb_iseq_t *iseq)
2061 for (i = 0; i < body->iseq_size; i++) {
2062 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2063 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
2070rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
2075 return entry->line_no;
2082#ifdef USE_ISEQ_NODE_ID
2084rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
2089 return entry->node_id;
2098rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2102 return entry->events;
2110rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos,
rb_event_flag_t reset)
2114 entry->events &= ~reset;
2115 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2116 void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2117 rb_iseq_trace_flag_cleared(iseq, pos);
2123local_var_name(
const rb_iseq_t *diseq,
VALUE level,
VALUE op)
2130 for (i = 0; i < level; i++) {
2131 diseq = ISEQ_BODY(diseq)->parent_iseq;
2133 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2134 lid = ISEQ_BODY(diseq)->local_table[idx];
2135 name = rb_id2str(lid);
2143 name = rb_str_dup(name);
2145 rb_str_catf(name,
"@%d", idx);
2149int rb_insn_unified_local_var_level(
VALUE);
2153rb_insn_operand_intern(
const rb_iseq_t *iseq,
2157 const char *types = insn_op_types(insn);
2158 char type = types[op_no];
2163 ret = rb_sprintf(
"%"PRIdVALUE, (
VALUE)(pos +
len + op));
2167 if (insn == BIN(defined) && op_no == 0) {
2168 enum defined_type deftype = (
enum defined_type)op;
2171 ret = rb_fstring_lit(
"func");
2174 ret = rb_fstring_lit(
"ref");
2176 case DEFINED_CONST_FROM:
2177 ret = rb_fstring_lit(
"constant-from");
2180 ret = rb_iseq_defined_string(deftype);
2185 else if (insn == BIN(checktype) && op_no == 0) {
2191 ret = rb_sprintf(
"%"PRIuVALUE, op);
2196 if (types[op_no+1] == TS_NUM && pnop) {
2197 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2199 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2200 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2212 op = obj_resurrect(op);
2213 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2218 ret = rb_sprintf(
":$%c", (
type >> 1));
2221 ret = rb_sprintf(
":$%d", (
type >> 1));
2226 ret = rb_dump_literal(op);
2229 rb_ary_push(child, op);
2237 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2238 ret = ISEQ_BODY(iseq)->location.label;
2240 rb_ary_push(child, (
VALUE)iseq);
2252 const ID *segments = ((IC)op)->segments;
2255 rb_str_catf(ret,
"::%s", rb_id2name(*segments++));
2270 VALUE ary = rb_ary_new();
2271 ID mid = vm_ci_mid(ci);
2274 rb_ary_push(ary, rb_sprintf(
"mid:%"PRIsVALUE, rb_id2str(mid)));
2277 rb_ary_push(ary, rb_sprintf(
"argc:%d", vm_ci_argc(ci)));
2279 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2281 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2282 rb_ary_push(ary, rb_sprintf(
"kw:[%"PRIsVALUE
"]", rb_ary_join(kw_ary,
rb_str_new2(
","))));
2285 if (vm_ci_flag(ci)) {
2286 VALUE flags = rb_ary_new();
2287# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2288 CALL_FLAG(ARGS_SPLAT);
2289 CALL_FLAG(ARGS_BLOCKARG);
2292 CALL_FLAG(ARGS_SIMPLE);
2293 CALL_FLAG(TAILCALL);
2297 CALL_FLAG(KW_SPLAT);
2298 CALL_FLAG(KW_SPLAT_MUT);
2299 CALL_FLAG(OPT_SEND);
2300 rb_ary_push(ary, rb_ary_join(flags,
rb_str_new2(
"|")));
2303 ret = rb_sprintf(
"<calldata!%"PRIsVALUE
">", rb_ary_join(ary,
rb_str_new2(
", ")));
2315 if (dladdr((
void *)op, &info) && info.dli_sname) {
2327 ret = rb_sprintf(
"<builtin!%s/%d>",
2328 bf->name, bf->argc);
2333 rb_bug(
"unknown operand type: %c",
type);
2339right_strip(
VALUE str)
2341 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2342 while (end-- > beg && *end ==
' ');
2343 rb_str_set_len(str, end - beg + 1);
2352rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2353 const rb_iseq_t *iseq,
VALUE child)
2355 VALUE insn = code[pos];
2356 int len = insn_len(insn);
2358 const char *types = insn_op_types(insn);
2360 const char *insn_name_buff;
2362 insn_name_buff = insn_name(insn);
2364 extern const int rb_vm_max_insn_name_size;
2365 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2368 rb_str_catf(str,
"%04"PRIuSIZE
" %-28.*s ", pos,
2369 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2372 for (j = 0; types[j]; j++) {
2373 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2374 len, pos, &code[pos + j + 2],
2384 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2385 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2386 if (line_no && line_no != prev) {
2387 long slen = RSTRING_LEN(str);
2388 slen = (slen > 70) ? 0 : (70 - slen);
2389 str = rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2396 str = rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s%s]",
2407 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2408 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2418 printf(
"%.*s\n", (
int)RSTRING_LEN(str), RSTRING_PTR(str));
2427 case CATCH_TYPE_RESCUE:
2429 case CATCH_TYPE_ENSURE:
2431 case CATCH_TYPE_RETRY:
2433 case CATCH_TYPE_BREAK:
2435 case CATCH_TYPE_REDO:
2437 case CATCH_TYPE_NEXT:
2440 rb_bug(
"unknown catch type: %d",
type);
2446iseq_inspect(
const rb_iseq_t *iseq)
2449 if (!body->location.label) {
2450 return rb_sprintf(
"#<ISeq: uninitialized>");
2453 const rb_code_location_t *loc = &body->location.code_location;
2454 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2455 body->location.label, rb_iseq_path(iseq),
2456 loc->beg_pos.lineno,
2457 loc->beg_pos.lineno,
2458 loc->beg_pos.column,
2459 loc->end_pos.lineno,
2460 loc->end_pos.column);
2466 {(void (*)(
void *))rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2467 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2471rb_iseq_disasm_recursive(const rb_iseq_t *iseq,
VALUE indent)
2476 VALUE child = rb_ary_hidden_new(3);
2481 enum {header_minlen = 72};
2482 st_table *done_iseq = 0;
2484 const char *indent_str;
2487 size = body->iseq_size;
2489 indent_len = RSTRING_LEN(indent);
2490 indent_str = RSTRING_PTR(indent);
2492 rb_str_cat(str, indent_str, indent_len);
2496 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2498 memset(RSTRING_END(str),
'=', header_minlen - l);
2503 if (body->catch_table) {
2504 rb_str_cat(str, indent_str, indent_len);
2507 if (body->catch_table) {
2509 indent_str = RSTRING_PTR(indent);
2510 for (i = 0; i < body->catch_table->size; i++) {
2512 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2513 rb_str_cat(str, indent_str, indent_len);
2515 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2516 catch_type((
int)entry->type), (
int)entry->start,
2517 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2518 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2519 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2521 done_iseq = st_init_numtable();
2524 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2525 indent_str = RSTRING_PTR(indent);
2528 rb_str_resize(indent, indent_len);
2529 indent_str = RSTRING_PTR(indent);
2531 if (body->catch_table) {
2532 rb_str_cat(str, indent_str, indent_len);
2533 rb_str_cat2(str,
"|-------------------------------------"
2534 "-----------------------------------\n");
2538 if (body->local_table) {
2539 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
2540 rb_str_cat(str, indent_str, indent_len);
2542 "local table (size: %d, argc: %d "
2543 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2544 body->local_table_size,
2545 body->
param.lead_num,
2546 body->
param.opt_num,
2547 body->
param.flags.has_rest ? body->
param.rest_start : -1,
2548 body->
param.post_num,
2549 body->
param.flags.has_block ? body->
param.block_start : -1,
2550 body->
param.flags.has_kw ? keyword->num : -1,
2551 body->
param.flags.has_kw ? keyword->required_num : -1,
2552 body->
param.flags.has_kwrest ? keyword->rest_start : -1);
2554 for (i = body->local_table_size; i > 0;) {
2555 int li = body->local_table_size - --i - 1;
2557 VALUE name = local_var_name(iseq, 0, i);
2562 if (body->
param.flags.has_opt) {
2563 int argc = body->
param.lead_num;
2564 int opts = body->
param.opt_num;
2565 if (li >= argc && li < argc + opts) {
2566 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2567 body->
param.opt_table[li - argc]);
2571 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2572 body->
param.lead_num > li ?
"Arg" :
"",
2574 (body->
param.flags.has_rest && body->
param.rest_start == li) ?
"Rest" :
"",
2575 (body->
param.flags.has_post && body->
param.post_start <= li && li < body->param.post_start + body->
param.post_num) ?
"Post" :
"",
2576 (body->
param.flags.has_kwrest && keyword->rest_start == li) ?
"Kwrest" :
"",
2577 (body->
param.flags.has_block && body->
param.block_start == li) ?
"Block" :
"");
2579 rb_str_cat(str, indent_str, indent_len);
2580 rb_str_catf(str,
"[%2d] ", i + 1);
2581 width = RSTRING_LEN(str) + 11;
2583 if (*argi) rb_str_catf(str,
"<%s>", argi);
2584 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str,
"%*s", (
int)width,
"");
2590 code = rb_iseq_original_iseq(iseq);
2591 for (n = 0; n < size;) {
2592 rb_str_cat(str, indent_str, indent_len);
2593 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2597 VALUE isv = rb_ary_entry(child, l);
2598 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2600 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2601 indent_str = RSTRING_PTR(indent);
2609rb_iseq_disasm(
const rb_iseq_t *iseq)
2612 rb_str_resize(str, RSTRING_LEN(str));
2622rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
2624 struct rb_id_table * iv_names = rb_id_table_create(0);
2626 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2627 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2629 if (cache->iv_set_name) {
2630 rb_id_table_insert(iv_names, cache->iv_set_name,
Qtrue);
2634 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2637 count += RCLASS_EXT(superclass)->max_iv_count;
2639 rb_id_table_free(iv_names);
2663iseqw_disasm(
VALUE self)
2665 return rb_iseq_disasm(iseqw_check(self));
2669iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
2672 VALUE *code = rb_iseq_original_iseq(iseq);
2674 const rb_iseq_t *child;
2675 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2677 if (body->catch_table) {
2678 for (i = 0; i < body->catch_table->size; i++) {
2680 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2681 child = entry->iseq;
2683 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
2684 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
2685 (*iter_func)(child, data);
2691 for (i=0; i<body->iseq_size;) {
2692 VALUE insn = code[i];
2693 int len = insn_len(insn);
2694 const char *types = insn_op_types(insn);
2697 for (j=0; types[j]; j++) {
2700 child = (
const rb_iseq_t *)code[i+j+1];
2702 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
2703 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
2704 (*iter_func)(child, data);
2719yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
2733iseqw_each_child(
VALUE self)
2735 const rb_iseq_t *iseq = iseqw_check(self);
2736 iseq_iterate_children(iseq, yield_each_children, NULL);
2743#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2763iseqw_trace_points(
VALUE self)
2765 const rb_iseq_t *iseq = iseqw_check(self);
2768 VALUE ary = rb_ary_new();
2770 for (i=0; i<body->insns_info.size; i++) {
2772 if (entry->events) {
2773 push_event_info(iseq, entry->events, entry->line_no, ary);
2817 const rb_iseq_t *iseq = NULL;
2820 iseq = vm_proc_iseq(body);
2822 if (!rb_obj_is_iseq((
VALUE)iseq)) {
2827 iseq = rb_method_iseq(body);
2829 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2833 return iseq ? iseqw_new(iseq) :
Qnil;
2891 VALUE iseqw = iseqw_s_of(klass, body);
2892 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
2896register_label(
struct st_table *table,
unsigned long idx)
2898 VALUE sym = rb_str_intern(rb_sprintf(
"label_%lu", idx));
2899 st_insert(table, idx, sym);
2908 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
2909 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
2910 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
2911 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
2912 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
2913 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
2915 rb_bug(
"unknown exception type: %d", (
int)
type);
2923 rb_ary_push(ary, obj_resurrect(key));
2924 rb_ary_push(ary, value);
2930 {(void (*)(
void *))rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
2931 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2934#define DECL_ID(name) \
2937#define INIT_ID(name) \
2938 id_##name = rb_intern(#name)
2941iseq_type_id(
enum rb_iseq_type
type)
2966 case ISEQ_TYPE_TOP:
return id_top;
2967 case ISEQ_TYPE_METHOD:
return id_method;
2968 case ISEQ_TYPE_BLOCK:
return id_block;
2969 case ISEQ_TYPE_CLASS:
return id_class;
2970 case ISEQ_TYPE_RESCUE:
return id_rescue;
2971 case ISEQ_TYPE_ENSURE:
return id_ensure;
2972 case ISEQ_TYPE_EVAL:
return id_eval;
2973 case ISEQ_TYPE_MAIN:
return id_main;
2974 case ISEQ_TYPE_PLAIN:
return id_plain;
2977 rb_bug(
"unsupported iseq type: %d", (
int)
type);
2981iseq_data_to_ary(
const rb_iseq_t *iseq)
2989 VALUE *seq, *iseq_original;
2991 VALUE val = rb_ary_new();
2993 VALUE locals = rb_ary_new();
2994 VALUE params = rb_hash_new();
2995 VALUE body = rb_ary_new();
2997 VALUE exception = rb_ary_new();
2998 VALUE misc = rb_hash_new();
3000 static ID insn_syms[VM_INSTRUCTION_SIZE/2];
3001 struct st_table *labels_table = st_init_numtable();
3004 if (insn_syms[0] == 0) {
3006 for (i=0; i<numberof(insn_syms); i++) {
3007 insn_syms[i] = rb_intern(insn_name(i));
3012 type = iseq_type_id(iseq_body->type);
3015 for (i=0; i<iseq_body->local_table_size; i++) {
3016 ID lid = iseq_body->local_table[i];
3018 if (rb_id2str(lid)) {
3019 rb_ary_push(locals,
ID2SYM(lid));
3022 rb_ary_push(locals,
ULONG2NUM(iseq_body->local_table_size-i+1));
3026 rb_ary_push(locals,
ID2SYM(rb_intern(
"#arg_rest")));
3032 const struct rb_iseq_param_keyword *
const keyword = iseq_body->
param.keyword;
3035 if (iseq_body->
param.flags.has_opt) {
3036 int len = iseq_body->
param.opt_num + 1;
3039 for (j = 0; j <
len; j++) {
3040 VALUE l = register_label(labels_table, iseq_body->
param.opt_table[j]);
3041 rb_ary_push(arg_opt_labels, l);
3043 rb_hash_aset(params,
ID2SYM(rb_intern(
"opt")), arg_opt_labels);
3047 if (iseq_body->
param.flags.has_lead) rb_hash_aset(params,
ID2SYM(rb_intern(
"lead_num")),
INT2FIX(iseq_body->
param.lead_num));
3048 if (iseq_body->
param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_num")),
INT2FIX(iseq_body->
param.post_num));
3049 if (iseq_body->
param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_start")),
INT2FIX(iseq_body->
param.post_start));
3050 if (iseq_body->
param.flags.has_rest) rb_hash_aset(params,
ID2SYM(rb_intern(
"rest_start")),
INT2FIX(iseq_body->
param.rest_start));
3051 if (iseq_body->
param.flags.has_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"block_start")),
INT2FIX(iseq_body->
param.block_start));
3052 if (iseq_body->
param.flags.has_kw) {
3053 VALUE keywords = rb_ary_new();
3055 for (i=0; i<keyword->required_num; i++) {
3056 rb_ary_push(keywords,
ID2SYM(keyword->table[i]));
3058 for (j=0; i<keyword->num; i++, j++) {
3059 VALUE key = rb_ary_new_from_args(1,
ID2SYM(keyword->table[i]));
3060 if (!UNDEF_P(keyword->default_values[j])) {
3061 rb_ary_push(key, keyword->default_values[j]);
3063 rb_ary_push(keywords, key);
3066 rb_hash_aset(params,
ID2SYM(rb_intern(
"kwbits")),
3067 INT2FIX(keyword->bits_start));
3068 rb_hash_aset(params,
ID2SYM(rb_intern(
"keyword")), keywords);
3070 if (iseq_body->
param.flags.has_kwrest) rb_hash_aset(params,
ID2SYM(rb_intern(
"kwrest")),
INT2FIX(keyword->rest_start));
3071 if (iseq_body->
param.flags.ambiguous_param0) rb_hash_aset(params,
ID2SYM(rb_intern(
"ambiguous_param0")),
Qtrue);
3075 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3077 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3078 VALUE insn = *seq++;
3079 int j,
len = insn_len(insn);
3083 rb_ary_push(ary,
ID2SYM(insn_syms[insn%numberof(insn_syms)]));
3084 for (j=0; j<
len-1; j++, seq++) {
3085 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3089 unsigned long idx = nseq - iseq_original + *seq;
3090 rb_ary_push(ary, register_label(labels_table, idx));
3095 rb_ary_push(ary,
INT2FIX(*seq));
3098 rb_ary_push(ary, obj_resurrect(*seq));
3102 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3104 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3105 rb_ary_push(ary, val);
3108 rb_ary_push(ary,
Qnil);
3114 VALUE list = rb_ary_new();
3115 const ID *ids = ((IC)*seq)->segments;
3117 rb_ary_push(list,
ID2SYM(*ids++));
3119 rb_ary_push(ary, list);
3127 rb_ary_push(ary,
INT2FIX(is - ISEQ_IS_ENTRY_START(ISEQ_BODY(iseq), op_type)));
3134 VALUE e = rb_hash_new();
3135 int argc = vm_ci_argc(ci);
3137 ID mid = vm_ci_mid(ci);
3139 rb_hash_aset(e,
ID2SYM(rb_intern(
"flag")),
UINT2NUM(vm_ci_flag(ci)));
3141 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3146 argc -= kwarg->keyword_len;
3147 for (i = 0; i < kwarg->keyword_len; i++) {
3148 rb_ary_push(kw, kwarg->keywords[i]);
3150 rb_hash_aset(e,
ID2SYM(rb_intern(
"kw_arg")), kw);
3153 rb_hash_aset(e,
ID2SYM(rb_intern(
"orig_argc")),
3155 rb_ary_push(ary, e);
3159 rb_ary_push(ary,
ID2SYM(*seq));
3164 VALUE val = rb_ary_new();
3171 unsigned long idx = nseq - iseq_original + pos;
3173 rb_ary_store(val, i+1,
3174 register_label(labels_table, idx));
3176 rb_ary_push(ary, val);
3181#if SIZEOF_VALUE <= SIZEOF_LONG
3186 rb_ary_push(ary, val);
3191 VALUE val = rb_hash_new();
3192#if SIZEOF_VALUE <= SIZEOF_LONG
3197 rb_hash_aset(val,
ID2SYM(rb_intern(
"func_ptr")), func_ptr);
3198 rb_hash_aset(val,
ID2SYM(rb_intern(
"argc")),
INT2NUM(((RB_BUILTIN)*seq)->argc));
3199 rb_hash_aset(val,
ID2SYM(rb_intern(
"index")),
INT2NUM(((RB_BUILTIN)*seq)->index));
3201 rb_ary_push(ary, val);
3205 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3208 rb_ary_push(body, ary);
3214 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3215 VALUE ary = rb_ary_new();
3217 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3218 rb_ary_push(ary, exception_type2symbol(entry->type));
3220 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3223 rb_ary_push(ary,
Qnil);
3225 rb_ary_push(ary, register_label(labels_table, entry->start));
3226 rb_ary_push(ary, register_label(labels_table, entry->end));
3227 rb_ary_push(ary, register_label(labels_table, entry->cont));
3228 rb_ary_push(ary,
UINT2NUM(entry->sp));
3229 rb_ary_push(exception, ary);
3233 body = rb_ary_new();
3234 prev_insn_info = NULL;
3235#ifdef USE_ISEQ_NODE_ID
3236 VALUE node_ids = rb_ary_new();
3244 if (st_lookup(labels_table, pos, &label)) {
3245 rb_ary_push(body, (
VALUE)label);
3248 info = get_insn_info(iseq, pos);
3249#ifdef USE_ISEQ_NODE_ID
3250 rb_ary_push(node_ids,
INT2FIX(info->node_id));
3253 if (prev_insn_info != info) {
3254 int line = info->line_no;
3257 if (line > 0 && last_line != line) {
3258 rb_ary_push(body,
INT2FIX(line));
3261#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3271 prev_insn_info = info;
3274 rb_ary_push(body, ary);
3281 rb_hash_aset(misc,
ID2SYM(rb_intern(
"local_size")),
INT2FIX(iseq_body->local_table_size));
3282 rb_hash_aset(misc,
ID2SYM(rb_intern(
"stack_max")),
INT2FIX(iseq_body->stack_max));
3283 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_id")),
INT2FIX(iseq_body->location.node_id));
3284 rb_hash_aset(misc,
ID2SYM(rb_intern(
"code_location")),
3285 rb_ary_new_from_args(4,
3286 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3287 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3288 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3289 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3290#ifdef USE_ISEQ_NODE_ID
3291 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_ids")), node_ids);
3299 rb_ary_push(val,
rb_str_new2(
"YARVInstructionSequence/SimpleDataFormat"));
3300 rb_ary_push(val,
INT2FIX(ISEQ_MAJOR_VERSION));
3301 rb_ary_push(val,
INT2FIX(ISEQ_MINOR_VERSION));
3303 rb_ary_push(val, misc);
3304 rb_ary_push(val, iseq_body->location.label);
3305 rb_ary_push(val, rb_iseq_path(iseq));
3306 rb_ary_push(val, rb_iseq_realpath(iseq));
3307 rb_ary_push(val,
RB_INT2NUM(iseq_body->location.first_lineno));
3309 rb_ary_push(val, locals);
3310 rb_ary_push(val, params);
3311 rb_ary_push(val, exception);
3312 rb_ary_push(val, body);
3317rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3321 const struct rb_iseq_param_keyword *
const keyword = body->
param.keyword;
3323 ID req, opt, rest, block, key, keyrest;
3324#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3325#define PARAM_ID(i) body->local_table[(i)]
3326#define PARAM(i, type) ( \
3328 rb_id2str(PARAM_ID(i)) ? \
3329 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3335 for (i = 0; i < body->
param.lead_num; i++) {
3337 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ?
ID2SYM(PARAM_ID(i)) :
Qnil);
3338 rb_ary_push(args, a);
3342 for (i = 0; i < body->
param.lead_num; i++) {
3343 rb_ary_push(args, PARAM(i, req));
3346 r = body->
param.lead_num + body->
param.opt_num;
3347 for (; i < r; i++) {
3349 if (rb_id2str(PARAM_ID(i))) {
3350 rb_ary_push(a,
ID2SYM(PARAM_ID(i)));
3352 rb_ary_push(args, a);
3354 if (body->
param.flags.has_rest) {
3356 rb_ary_push(args, PARAM(body->
param.rest_start, rest));
3358 r = body->
param.post_start + body->
param.post_num;
3360 for (i = body->
param.post_start; i < r; i++) {
3362 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ?
ID2SYM(PARAM_ID(i)) :
Qnil);
3363 rb_ary_push(args, a);
3367 for (i = body->
param.post_start; i < r; i++) {
3368 rb_ary_push(args, PARAM(i, req));
3371 if (body->
param.flags.accepts_no_kwarg) {
3375 rb_ary_push(args, a);
3377 if (body->
param.flags.has_kw) {
3379 if (keyword->required_num > 0) {
3382 for (; i < keyword->required_num; i++) {
3384 if (rb_id2str(keyword->table[i])) {
3385 rb_ary_push(a,
ID2SYM(keyword->table[i]));
3387 rb_ary_push(args, a);
3391 for (; i < keyword->num; i++) {
3393 if (rb_id2str(keyword->table[i])) {
3394 rb_ary_push(a,
ID2SYM(keyword->table[i]));
3396 rb_ary_push(args, a);
3399 if (body->
param.flags.has_kwrest || body->
param.flags.ruby2_keywords) {
3402 PARAM_TYPE(keyrest);
3403 if (body->
param.flags.has_kwrest &&
3404 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3405 rb_ary_push(a,
ID2SYM(param));
3407 else if (body->
param.flags.ruby2_keywords) {
3408 rb_ary_push(a,
ID2SYM(idPow));
3410 rb_ary_push(args, a);
3412 if (body->
param.flags.has_block) {
3414 rb_ary_push(args, PARAM(body->
param.block_start, block));
3420rb_iseq_defined_string(
enum defined_type
type)
3422 static const char expr_names[][18] = {
3424 "instance-variable",
3440 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names)) rb_bug(
"unknown defined type %d",
type);
3441 estr = expr_names[
type - 1];
3442 return rb_fstring_cstr(estr);
3448static st_table *encoded_insn_data;
3452 void *notrace_encoded_insn;
3453 void *trace_encoded_insn;
3455static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3458rb_free_encoded_insn_data(
void)
3460 st_free_table(encoded_insn_data);
3464rb_vm_encoded_insn_data_table_init(
void)
3466#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3467 const void *
const *table = rb_vm_get_insns_address_table();
3468#define INSN_CODE(insn) ((VALUE)table[insn])
3470#define INSN_CODE(insn) (insn)
3473 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3475 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3476 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3477 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3479 insn_data[insn].insn = (int)insn;
3480 insn_data[insn].insn_len = insn_len(insn);
3482 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3483 insn_data[insn].notrace_encoded_insn = (
void *) key1;
3484 insn_data[insn].trace_encoded_insn = (
void *) key2;
3487 insn_data[insn].notrace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3488 insn_data[insn].trace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3491 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3492 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3497rb_vm_insn_addr2insn(
const void *addr)
3499 st_data_t key = (st_data_t)addr;
3502 if (st_lookup(encoded_insn_data, key, &val)) {
3503 insn_data_t *e = (insn_data_t *)val;
3504 return (
int)e->insn;
3507 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3512rb_vm_insn_addr2opcode(
const void *addr)
3514 st_data_t key = (st_data_t)addr;
3517 if (st_lookup(encoded_insn_data, key, &val)) {
3518 insn_data_t *e = (insn_data_t *)val;
3519 int opcode = e->insn;
3520 if (addr == e->trace_encoded_insn) {
3521 opcode += VM_INSTRUCTION_SIZE/2;
3526 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3531rb_vm_insn_decode(
const VALUE encoded)
3533#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3534 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3536 int insn = (int)encoded;
3542encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_current_trace)
3544 st_data_t key = (st_data_t)*iseq_encoded_insn;
3547 if (st_lookup(encoded_insn_data, key, &val)) {
3548 insn_data_t *e = (insn_data_t *)val;
3549 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3552 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3556 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3560rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3563 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3564 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
3585iseq_add_local_tracepoint(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events,
VALUE tpval,
unsigned int target_line)
3590 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3592 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3594 for (pc=0; pc<body->iseq_size;) {
3598 unsigned int line = (int)entry->line_no;
3600 if (target_line == 0 || target_line == line) {
3607 if (pc_events & target_events) {
3610 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
3614 if (iseq->aux.exec.local_hooks == NULL) {
3615 ((rb_iseq_t *)iseq)->aux.exec.local_hooks =
RB_ZALLOC(rb_hook_list_t);
3616 iseq->aux.exec.local_hooks->is_local =
true;
3618 rb_hook_list_connect_tracepoint((
VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3627 unsigned int target_line;
3632iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3635 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3636 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3640rb_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)
3643 if (target_bmethod) {
3644 turnon_events = add_bmethod_events(turnon_events);
3646 data.turnon_events = turnon_events;
3648 data.target_line = target_line;
3651 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
3652 if (0)
rb_funcall(
Qnil, rb_intern(
"puts"), 1, rb_iseq_disasm(iseq));
3657iseq_remove_local_tracepoint(
const rb_iseq_t *iseq,
VALUE tpval)
3661 if (iseq->aux.exec.local_hooks) {
3664 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3667 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3668 local_events = iseq->aux.exec.local_hooks->events;
3670 if (local_events == 0) {
3671 rb_hook_list_free(iseq->aux.exec.local_hooks);
3672 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3675 local_events = add_bmethod_events(local_events);
3676 for (pc = 0; pc<body->iseq_size;) {
3678 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
3690iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3693 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3694 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3698rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval)
3704 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
3709rb_iseq_trace_set(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events)
3711 if (iseq->aux.exec.global_trace_events == turnon_events) {
3715 if (!ISEQ_EXECUTABLE_P(iseq)) {
3722 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3724 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3725 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3726 enabled_events = add_bmethod_events(turnon_events | local_events);
3728 for (pc=0; pc<body->iseq_size;) {
3730 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
3738clear_attr_cc(
VALUE v)
3740 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((
const struct rb_callcache *)v)) {
3752 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((
const struct rb_callcache *)v)) {
3762clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3765 for (; v != (
VALUE)vend; v += stride) {
3766 void *ptr = asan_poisoned_object_p(v);
3767 asan_unpoison_object(v,
false);
3769 asan_poison_object_if(ptr, v);
3775rb_clear_attr_ccs(
void)
3777 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3781clear_bf_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3784 for (; v != (
VALUE)vend; v += stride) {
3785 void *ptr = asan_poisoned_object_p(v);
3786 asan_unpoison_object(v,
false);
3788 asan_poison_object_if(ptr, v);
3794rb_clear_bf_ccs(
void)
3796 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
3800trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3805 for (; v != (
VALUE)vend; v += stride) {
3806 void *ptr = asan_poisoned_object_p(v);
3807 asan_unpoison_object(v,
false);
3809 if (rb_obj_is_iseq(v)) {
3810 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
3812 else if (clear_attr_cc(v)) {
3814 else if (clear_bf_cc(v)) {
3817 asan_poison_object_if(ptr, v);
3825 rb_objspace_each_objects(trace_set_i, &turnon_events);
3829rb_iseqw_local_variables(
VALUE iseqval)
3831 return rb_iseq_local_variables(iseqw_check(iseqval));
3852iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
3855 return rb_iseq_ibf_dump(iseqw_check(self), opt);
3872iseqw_s_load_from_binary(
VALUE self,
VALUE str)
3874 return iseqw_new(rb_iseq_ibf_load(str));
3884iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
3886 return rb_iseq_ibf_load_extra_data(str);
3889#if VM_INSN_INFO_TABLE_IMPL == 2
3922#define IMMEDIATE_TABLE_SIZE 54
3924struct succ_index_table {
3925 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
3926 struct succ_dict_block {
3928 uint64_t small_block_ranks;
3929 uint64_t bits[512/64];
3930 } succ_part[FLEX_ARY_LEN];
3933#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
3934#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
3935#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
3936#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
3938static struct succ_index_table *
3939succ_index_table_create(
int max_pos,
int *data,
int size)
3941 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3942 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3943 struct succ_index_table *sd =
3944 rb_xcalloc_mul_add_mul(
3945 imm_size,
sizeof(uint64_t),
3946 succ_size,
sizeof(
struct succ_dict_block));
3950 for (j = 0; j < imm_size; j++) {
3951 for (i = 0; i < 9; i++) {
3952 if (r < size && data[r] == j * 9 + i) r++;
3953 imm_block_rank_set(sd->imm_part[j], i, r);
3956 for (k = 0; k < succ_size; k++) {
3957 struct succ_dict_block *sd_block = &sd->succ_part[k];
3960 for (j = 0; j < 8; j++) {
3962 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
3963 for (i = 0; i < 64; i++) {
3964 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
3965 bits |= ((uint64_t)1) << i;
3969 sd_block->bits[j] = bits;
3970 small_rank += rb_popcount64(bits);
3976static unsigned int *
3977succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
3979 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3980 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3981 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
3982 int i, j, k, r = -1;
3984 for (j = 0; j < imm_size; j++) {
3985 for (i = 0; i < 9; i++) {
3986 int nr = imm_block_rank_get(sd->imm_part[j], i);
3987 if (r != nr) *p++ = j * 9 + i;
3991 for (k = 0; k < succ_size; k++) {
3992 for (j = 0; j < 8; j++) {
3993 for (i = 0; i < 64; i++) {
3994 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
3995 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4004succ_index_lookup(
const struct succ_index_table *sd,
int x)
4006 if (x < IMMEDIATE_TABLE_SIZE) {
4007 const int i = x / 9;
4008 const int j = x % 9;
4009 return imm_block_rank_get(sd->imm_part[i], j);
4012 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4013 const struct succ_dict_block *block = &sd->succ_part[block_index];
4014 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4015 const int small_block_index = block_bit_index / 64;
4016 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4017 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4019 return block->rank + small_block_popcount + popcnt;
4038iseqw_script_lines(
VALUE self)
4040 const rb_iseq_t *iseq = iseqw_check(self);
4041 return ISEQ_BODY(iseq)->variable.script_lines;
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
struct pm_node pm_node_t
This is the base structure that represents a node in the syntax tree.
#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.
#define RB_OBJ_FREEZE
Just another name of rb_obj_freeze_inline.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_FILE
Old name of RUBY_T_FILE.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define ZALLOC
Old name of RB_ZALLOC.
#define LL2NUM
Old name of RB_LL2NUM.
#define CLASS_OF
Old name of rb_class_of.
#define T_NONE
Old name of RUBY_T_NONE.
#define FIX2INT
Old name of RB_FIX2INT.
#define T_HASH
Old name of RUBY_T_HASH.
#define ALLOC_N
Old name of RB_ALLOC_N.
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
#define LONG2NUM
Old name of RB_LONG2NUM.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
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_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_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
VALUE rb_io_close(VALUE io)
Closes the IO.
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_resurrect(VALUE str)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
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_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 id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
int len
Length of the buffer.
#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.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
struct pm_parser pm_parser_t
The parser used to parse Ruby source.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#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.
size_t column
The column number.
size_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.
A list of offsets of newlines in a string.
pm_location_t location
This is the location of the node in the source.
The options that can be passed to the parser.
pm_newline_list_t newline_list
This is the list of newline offsets in the source file.
A generic string type that can have various ownership semantics.
struct rb_iseq_constant_body::@000024342312237062266020177166377106262102236123 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.
ruby_value_type
C-level type of an object.