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);
248 VALUE op = code[page_offset + offset];
249 rb_gc_mark_and_move(&code[page_offset + offset]);
250 VALUE newop = code[page_offset + offset];
251 if (original_iseq && newop != op) {
252 original_iseq[page_offset + offset] = newop;
259rb_iseq_mark_and_move_each_compile_data_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
265 size = compile_data->iseq_size;
266 code = compile_data->iseq_encoded;
269 if (compile_data->mark_bits.list) {
270 if(compile_data->is_single_mark_bit) {
271 iseq_scan_bits(0, compile_data->mark_bits.single, code, original_iseq);
274 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
275 iseq_bits_t bits = compile_data->mark_bits.list[i];
276 iseq_scan_bits(i, bits, code, original_iseq);
282rb_iseq_mark_and_move_each_body_value(
const rb_iseq_t *iseq,
VALUE *original_iseq)
288 size = body->iseq_size;
289 code = body->iseq_encoded;
293 if (body->is_entries) {
295 is_entries += body->ivc_size;
298 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
299 ICVARC icvarc = (ICVARC)is_entries;
301 rb_gc_mark_and_move((
VALUE *)&icvarc->entry);
306 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
308 if (is->once.value) {
309 rb_gc_mark_and_move(&is->once.value);
314 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
315 IC ic = (IC)is_entries;
317 rb_gc_mark_and_move_ptr(&ic->entry);
323 if (body->mark_bits.list) {
324 if (ISEQ_MBITS_BUFLEN(size) == 1) {
325 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
328 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
329 iseq_bits_t bits = body->mark_bits.list[i];
330 iseq_scan_bits(i, bits, code, original_iseq);
337cc_is_active(
const struct rb_callcache *cc,
bool reference_updating)
340 if (cc == rb_vm_empty_cc() || rb_vm_empty_cc_for_super()) {
344 if (reference_updating) {
348 if (vm_cc_markable(cc) && vm_cc_valid(cc)) {
350 if (reference_updating) {
353 if (!METHOD_ENTRY_INVALIDATED(cme)) {
362rb_iseq_mark_and_move(rb_iseq_t *iseq,
bool reference_updating)
364 RUBY_MARK_ENTER(
"iseq");
366 rb_gc_mark_and_move(&iseq->wrapper);
368 if (ISEQ_BODY(iseq)) {
371 rb_iseq_mark_and_move_each_body_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
373 rb_gc_mark_and_move(&body->variable.script_lines);
374 rb_gc_mark_and_move(&body->location.label);
375 rb_gc_mark_and_move(&body->location.base_label);
376 rb_gc_mark_and_move(&body->location.pathobj);
377 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
378 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
379 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
381 if (body->call_data) {
382 for (
unsigned int i = 0; i < body->ci_size; i++) {
385 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
387 if (cc_is_active(cds[i].cc, reference_updating)) {
388 rb_gc_mark_and_move_ptr(&cds[i].cc);
390 else if (cds[i].cc != rb_vm_empty_cc()) {
391 cds[i].cc = rb_vm_empty_cc();
396 if (body->param.flags.has_kw && body->param.keyword != NULL) {
397 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
399 if (keyword->default_values != NULL) {
400 for (
int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
401 rb_gc_mark_and_move(&keyword->default_values[j]);
406 if (body->catch_table) {
409 for (
unsigned int i = 0; i < table->size; i++) {
411 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
413 rb_gc_mark_and_move_ptr(&entry->iseq);
418 if (reference_updating) {
420 rb_yjit_iseq_update_references(iseq);
423 rb_zjit_iseq_update_references(body->zjit_payload);
428 if (!rb_gc_checking_shareable()) {
430 rb_yjit_iseq_mark(body->yjit_payload);
433 rb_zjit_iseq_mark(body->zjit_payload);
439 if (!rb_gc_checking_shareable()) {
440 rb_gc_mark_and_move(&body->variable.coverage);
441 rb_gc_mark_and_move(&body->variable.pc2branchindex);
446 if (!rb_gc_checking_shareable()) {
447 rb_gc_mark_and_move(&iseq->aux.loader.obj);
451 if (!rb_gc_checking_shareable()) {
454 rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head);
455 rb_iseq_mark_and_move_each_compile_data_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
457 rb_gc_mark_and_move((
VALUE *)&compile_data->err_info);
458 rb_gc_mark_and_move((
VALUE *)&compile_data->catch_table_ary);
463 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
466 RUBY_MARK_LEAVE(
"iseq");
470param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
474 if (!pkw)
return size;
476 size +=
sizeof(
struct rb_iseq_param_keyword);
477 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
483rb_iseq_memsize(
const rb_iseq_t *iseq)
491 if (ISEQ_EXECUTABLE_P(iseq) && body) {
493 size += body->iseq_size *
sizeof(
VALUE);
495 size += body->local_table_size *
sizeof(
ID);
496 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
497 if (body->catch_table) {
498 size += iseq_catch_table_bytes(body->catch_table->size);
500 size += (body->param.opt_num + 1) *
sizeof(
VALUE);
501 size += param_keyword_size(body->param.keyword);
506 if (ISEQ_BODY(iseq)->is_entries) {
508 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
509 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
524 compile_data = ISEQ_COMPILE_DATA(iseq);
530 cur = compile_data->node.storage_head;
541rb_iseq_constant_body_alloc(
void)
551 rb_iseq_t *iseq = iseq_imemo_alloc();
552 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
557rb_iseq_pathobj_new(
VALUE path,
VALUE realpath)
563 if (path == realpath ||
565 pathobj = rb_fstring(path);
568 if (!
NIL_P(realpath)) {
569 realpath = rb_fstring(realpath);
571 VALUE fpath = rb_fstring(path);
573 pathobj = rb_ary_new_from_args(2, fpath, realpath);
575 RB_OBJ_SET_SHAREABLE(pathobj);
581rb_iseq_pathobj_set(
const rb_iseq_t *iseq,
VALUE path,
VALUE realpath)
584 rb_iseq_pathobj_new(path, realpath));
589rb_iseq_alloc_with_dummy_path(
VALUE fname)
591 rb_iseq_t *dummy_iseq = iseq_alloc();
593 ISEQ_BODY(dummy_iseq)->type = ISEQ_TYPE_TOP;
596 RB_OBJ_SET_FROZEN_SHAREABLE(fname);
599 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.pathobj, fname);
600 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.label, fname);
605static rb_iseq_location_t *
606iseq_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)
608 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
610 rb_iseq_pathobj_set(iseq, path, realpath);
613 loc->first_lineno = first_lineno;
615 if (ISEQ_BODY(iseq)->local_iseq == iseq && strcmp(RSTRING_PTR(name),
"initialize") == 0) {
616 ISEQ_BODY(iseq)->param.flags.use_block = 1;
620 loc->node_id = node_id;
621 loc->code_location = *code_location;
624 loc->code_location.beg_pos.lineno = 0;
625 loc->code_location.beg_pos.column = 0;
626 loc->code_location.end_pos.lineno = -1;
627 loc->code_location.end_pos.column = -1;
634set_relation(rb_iseq_t *iseq,
const rb_iseq_t *piseq)
640 if (
type == ISEQ_TYPE_TOP) {
641 body->local_iseq = iseq;
643 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
644 body->local_iseq = iseq;
647 RB_OBJ_WRITE(iseq, &body->local_iseq, ISEQ_BODY(piseq)->local_iseq);
654 if (
type == ISEQ_TYPE_MAIN) {
655 body->local_iseq = iseq;
664 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
669 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
675prepare_node_id(
const NODE *node)
677 if (!node)
return -1;
679 if (nd_type(node) == NODE_SCOPE && RNODE_SCOPE(node)->nd_parent) {
680 return nd_node_id(RNODE_SCOPE(node)->nd_parent);
683 return nd_node_id(node);
687prepare_iseq_build(rb_iseq_t *iseq,
688 VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_code_location_t *code_location,
const int node_id,
689 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
690 VALUE script_lines,
const rb_compile_option_t *option)
696 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
700 set_relation(iseq, parent);
702 name = rb_fstring(name);
703 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
704 if (iseq != body->local_iseq) {
705 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
707 ISEQ_COVERAGE_SET(iseq,
Qnil);
708 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
709 body->variable.flip_count = 0;
711 if (
NIL_P(script_lines)) {
718 ISEQ_COMPILE_DATA_ALLOC(iseq);
719 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
722 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
723 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
724 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
725 ISEQ_COMPILE_DATA(iseq)->option = option;
726 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
727 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
729 if (option->coverage_enabled) {
730 VALUE coverages = rb_get_coverages();
731 if (
RTEST(coverages)) {
732 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
736 ISEQ_COVERAGE_SET(iseq, coverage);
737 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
743#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
744static void validate_get_insn_info(
const rb_iseq_t *iseq);
748rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
750#if VM_INSN_INFO_TABLE_IMPL == 2
753 int size = body->insns_info.size;
754 int max_pos = body->iseq_size;
755 int *data = (
int *)body->insns_info.positions;
756 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
757 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
758#if VM_CHECK_MODE == 0
759 ruby_xfree(body->insns_info.positions);
760 body->insns_info.positions = NULL;
765#if VM_INSN_INFO_TABLE_IMPL == 2
769 int size = body->insns_info.size;
770 int max_pos = body->iseq_size;
771 struct succ_index_table *sd = body->insns_info.succ_index_table;
772 return succ_index_table_invert(max_pos, sd, size);
777rb_iseq_init_trace(rb_iseq_t *iseq)
779 iseq->aux.exec.global_trace_events = 0;
780 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
781 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
786finish_iseq_build(rb_iseq_t *iseq)
790 VALUE err = data->err_info;
791 ISEQ_COMPILE_DATA_CLEAR(iseq);
792 compile_data_free(data);
794#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
795 validate_get_insn_info(iseq);
799 VALUE path = pathobj_path(body->location.pathobj);
801 rb_funcallv(err, rb_intern(
"set_backtrace"), 1, &path);
805 RB_DEBUG_COUNTER_INC(iseq_num);
806 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
808 rb_iseq_init_trace(iseq);
812static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
813 .inline_const_cache = OPT_INLINE_CONST_CACHE,
814 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
815 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
816 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
817 .operands_unification = OPT_OPERANDS_UNIFICATION,
818 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
819 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
820 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
821 .coverage_enabled = TRUE,
824static const rb_compile_option_t COMPILE_OPTION_FALSE = {
825 .frozen_string_literal = -1,
829rb_iseq_opt_frozen_string_literal(
void)
831 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
835set_compile_option_from_hash(rb_compile_option_t *option,
VALUE opt)
837#define SET_COMPILE_OPTION(o, h, mem) \
838 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
839 if (flag == Qtrue) { (o)->mem = 1; } \
840 else if (flag == Qfalse) { (o)->mem = 0; } \
842#define SET_COMPILE_OPTION_NUM(o, h, mem) \
843 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
844 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
846 SET_COMPILE_OPTION(option, opt, inline_const_cache);
847 SET_COMPILE_OPTION(option, opt, peephole_optimization);
848 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
849 SET_COMPILE_OPTION(option, opt, specialized_instruction);
850 SET_COMPILE_OPTION(option, opt, operands_unification);
851 SET_COMPILE_OPTION(option, opt, instructions_unification);
852 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
853 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
854 SET_COMPILE_OPTION(option, opt, coverage_enabled);
855 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
856#undef SET_COMPILE_OPTION
857#undef SET_COMPILE_OPTION_NUM
860static rb_compile_option_t *
861set_compile_option_from_ast(rb_compile_option_t *option,
const rb_ast_body_t *ast)
863 if (ast->frozen_string_literal >= 0) {
864 option->frozen_string_literal = ast->frozen_string_literal;
870make_compile_option(rb_compile_option_t *option,
VALUE opt)
873 *option = COMPILE_OPTION_DEFAULT;
876 *option = COMPILE_OPTION_FALSE;
878 else if (opt ==
Qtrue) {
880 for (i = 0; i < (int)(
sizeof(rb_compile_option_t) /
sizeof(int)); ++i)
881 ((
int *)option)[i] = 1;
884 *option = COMPILE_OPTION_DEFAULT;
885 set_compile_option_from_hash(option, opt);
888 rb_raise(
rb_eTypeError,
"Compile option must be Hash/true/false/nil");
893make_compile_option_value(rb_compile_option_t *option)
895 VALUE opt = rb_hash_new_with_size(11);
896#define SET_COMPILE_OPTION(o, h, mem) \
897 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
898#define SET_COMPILE_OPTION_NUM(o, h, mem) \
899 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
901 SET_COMPILE_OPTION(option, opt, inline_const_cache);
902 SET_COMPILE_OPTION(option, opt, peephole_optimization);
903 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
904 SET_COMPILE_OPTION(option, opt, specialized_instruction);
905 SET_COMPILE_OPTION(option, opt, operands_unification);
906 SET_COMPILE_OPTION(option, opt, instructions_unification);
907 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
908 SET_COMPILE_OPTION(option, opt, coverage_enabled);
909 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
911#undef SET_COMPILE_OPTION
912#undef SET_COMPILE_OPTION_NUM
913 VALUE frozen_string_literal = option->frozen_string_literal == -1 ?
Qnil : RBOOL(option->frozen_string_literal);
914 rb_hash_aset(opt,
ID2SYM(rb_intern(
"frozen_string_literal")), frozen_string_literal);
920 const rb_iseq_t *parent,
enum rb_iseq_type
type)
922 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
923 0,
type, &COMPILE_OPTION_DEFAULT,
928ast_line_count(
const VALUE ast_value)
930 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
931 return ast->body.line_count;
935iseq_setup_coverage(
VALUE coverages,
VALUE path,
int line_count)
937 if (line_count >= 0) {
938 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
940 VALUE coverage = rb_default_coverage(
len);
941 rb_hash_aset(coverages, path, coverage);
950iseq_new_setup_coverage(
VALUE path,
int line_count)
952 VALUE coverages = rb_get_coverages();
954 if (
RTEST(coverages)) {
955 iseq_setup_coverage(coverages, path, line_count);
960rb_iseq_new_top(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent)
962 iseq_new_setup_coverage(path, ast_line_count(ast_value));
964 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
965 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
973pm_iseq_new_top(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int *error_state)
977 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
978 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT, error_state);
982rb_iseq_new_main(
const VALUE ast_value,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt)
984 iseq_new_setup_coverage(path, ast_line_count(ast_value));
986 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
988 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
997pm_iseq_new_main(pm_scope_node_t *node,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt,
int *error_state)
1001 return pm_iseq_new_with_opt(node, rb_fstring_lit(
"<main>"),
1003 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE, error_state);
1007rb_iseq_new_eval(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
1009 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1010 VALUE coverages = rb_get_coverages();
1011 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1012 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
1016 rb_compile_option_t option = COMPILE_OPTION_DEFAULT;
1017 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1018 if (ast->body.coverage_enabled >= 0) {
1019 option.coverage_enabled = ast->body.coverage_enabled;
1021 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
1022 parent, isolated_depth, ISEQ_TYPE_EVAL, &option,
1027pm_iseq_new_eval(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1028 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
int *error_state)
1030 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1031 VALUE coverages = rb_get_coverages();
1032 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1033 iseq_setup_coverage(coverages, path, ((
int) (node->parser->
newline_list.
size - 1)) + first_lineno - 1);
1037 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
1038 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT, error_state);
1041static inline rb_iseq_t *
1042iseq_translate(rb_iseq_t *iseq)
1045 VALUE v1 = iseqw_new(iseq);
1047 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
1048 iseq = (rb_iseq_t *)iseqw_check(v2);
1057 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1058 enum rb_iseq_type
type,
const rb_compile_option_t *option,
1061 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1062 rb_ast_body_t *body = ast ? &ast->body : NULL;
1063 const NODE *node = body ? body->root : 0;
1065 rb_iseq_t *iseq = iseq_alloc();
1066 rb_compile_option_t new_opt;
1068 if (!option) option = &COMPILE_OPTION_DEFAULT;
1071 option = set_compile_option_from_ast(&new_opt, body);
1074 if (!
NIL_P(script_lines)) {
1077 else if (body && body->script_lines) {
1078 script_lines = rb_parser_build_script_lines_from(body->script_lines);
1081 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1084 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, prepare_node_id(node),
1085 parent, isolated_depth,
type, script_lines, option);
1087 rb_iseq_compile_node(iseq, node);
1088 finish_iseq_build(iseq);
1091 return iseq_translate(iseq);
1096 pm_scope_node_t *node;
1100pm_iseq_new_with_opt_try(
VALUE d)
1105 pm_iseq_compile_node(data->iseq, data->node);
1108 finish_iseq_build(data->iseq);
1126pm_iseq_new_with_opt(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1127 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1128 enum rb_iseq_type
type,
const rb_compile_option_t *option,
int *error_state)
1130 rb_iseq_t *iseq = iseq_alloc();
1131 ISEQ_BODY(iseq)->prism =
true;
1133 rb_compile_option_t next_option;
1134 if (!option) option = &COMPILE_OPTION_DEFAULT;
1136 next_option = *option;
1137 next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
1138 option = &next_option;
1141 int32_t start_line = node->parser->
start_line;
1146 rb_code_location_t code_location = (rb_code_location_t) {
1147 .beg_pos = { .lineno = (int) start.
line, .column = (
int) start.
column },
1148 .end_pos = { .lineno = (int) end.
line, .column = (
int) end.
column }
1151 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node->ast_node->
node_id,
1158 rb_protect(pm_iseq_new_with_opt_try, (
VALUE)&data, error_state);
1160 if (*error_state)
return NULL;
1162 return iseq_translate(iseq);
1166rb_iseq_new_with_callback(
1169 int first_lineno,
const rb_iseq_t *parent,
1170 enum rb_iseq_type
type,
const rb_compile_option_t *option)
1173 rb_iseq_t *iseq = iseq_alloc();
1175 if (!option) option = &COMPILE_OPTION_DEFAULT;
1176 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1178 rb_iseq_compile_callback(iseq, ifunc);
1179 finish_iseq_build(iseq);
1185rb_iseq_load_iseq(
VALUE fname)
1190 return iseqw_check(iseqv);
1205 return iseqw_check(iseqv);
1211#define CHECK_ARRAY(v) rb_to_array_type(v)
1212#define CHECK_HASH(v) rb_to_hash_type(v)
1213#define CHECK_STRING(v) rb_str_to_str(v)
1214#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1217static enum rb_iseq_type
1220 const ID id_top = rb_intern(
"top");
1221 const ID id_method = rb_intern(
"method");
1222 const ID id_block = rb_intern(
"block");
1223 const ID id_class = rb_intern(
"class");
1224 const ID id_rescue = rb_intern(
"rescue");
1225 const ID id_ensure = rb_intern(
"ensure");
1226 const ID id_eval = rb_intern(
"eval");
1227 const ID id_main = rb_intern(
"main");
1228 const ID id_plain = rb_intern(
"plain");
1232 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1233 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1234 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1235 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1236 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1237 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1238 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1239 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1240 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1241 return (
enum rb_iseq_type)-1;
1245iseq_load(
VALUE data,
const rb_iseq_t *parent,
VALUE opt)
1247 rb_iseq_t *iseq = iseq_alloc();
1249 VALUE magic, version1, version2, format_type, misc;
1250 VALUE name, path, realpath, code_location, node_id;
1251 VALUE type, body, locals, params, exception;
1253 st_data_t iseq_type;
1254 rb_compile_option_t option;
1256 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1263 data = CHECK_ARRAY(data);
1270 ((void)magic, (void)version1, (void)version2, (void)format_type);
1275 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1284 ISEQ_BODY(iseq)->local_iseq = iseq;
1286 iseq_type = iseq_type_from_sym(
type);
1287 if (iseq_type == (
enum rb_iseq_type)-1) {
1291 node_id = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_id")));
1293 code_location = rb_hash_aref(misc,
ID2SYM(rb_intern(
"code_location")));
1301 if (
SYM2ID(rb_hash_aref(misc,
ID2SYM(rb_intern(
"parser")))) == rb_intern(
"prism")) {
1302 ISEQ_BODY(iseq)->prism =
true;
1305 make_compile_option(&option, opt);
1306 option.peephole_optimization = FALSE;
1307 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1308 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1310 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1312 finish_iseq_build(iseq);
1314 return iseqw_new(iseq);
1321iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1325 return iseq_load(data, NULL, opt);
1331 return iseq_load(data,
RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1337 rb_iseq_t *iseq = NULL;
1338 rb_compile_option_t option;
1339#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1340# define INITIALIZED volatile
1346 VALUE INITIALIZED ast_value;
1348 VALUE name = rb_fstring_lit(
"<compiled>");
1351 make_compile_option(&option, opt);
1355 parse = rb_parser_compile_file_path;
1358 parse = rb_parser_compile_string_path;
1362 const VALUE parser = rb_parser_new();
1363 const rb_iseq_t *outer_scope = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1365 rb_parser_set_context(parser, outer_scope, FALSE);
1366 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1368 ast_value = (*parse)(parser, file, src, ln);
1371 ast = rb_ruby_ast_data_get(ast_value);
1373 if (!ast || !ast->body.root) {
1374 rb_ast_dispose(ast);
1378 iseq_new_setup_coverage(file, ast_line_count(ast_value));
1379 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1380 NULL, 0, ISEQ_TYPE_TOP, &option,
1382 rb_ast_dispose(ast);
1391 rb_iseq_t *iseq = NULL;
1392 rb_compile_option_t option;
1394 VALUE name = rb_fstring_lit(
"<compiled>");
1397 make_compile_option(&option, opt);
1401 bool parse_file =
false;
1412 pm_options_scopes_init(&result.
options, 1);
1413 result.
node.coverage_enabled = 1;
1415 switch (option.frozen_string_literal) {
1416 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1418 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1419 pm_options_frozen_string_literal_set(&result.
options,
false);
1421 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1422 pm_options_frozen_string_literal_set(&result.
options,
true);
1425 rb_bug(
"pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1433 error = pm_load_parse_file(&result, src, ruby_vm_keep_script_lines ? &script_lines : NULL);
1436 error = pm_parse_string(&result, src, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1441 if (error ==
Qnil) {
1444 iseq = pm_iseq_new_with_opt(&result.
node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1446 pm_parse_result_free(&result);
1450 rb_jump_tag(error_state);
1454 pm_parse_result_free(&result);
1462rb_iseq_path(
const rb_iseq_t *iseq)
1464 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1468rb_iseq_realpath(
const rb_iseq_t *iseq)
1470 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1474rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1476 return rb_iseq_realpath(iseq);
1480rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1482 return NIL_P(rb_iseq_realpath(iseq));
1486rb_iseq_label(
const rb_iseq_t *iseq)
1488 return ISEQ_BODY(iseq)->location.label;
1492rb_iseq_base_label(
const rb_iseq_t *iseq)
1494 return ISEQ_BODY(iseq)->location.base_label;
1498rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1500 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1504rb_iseq_method_name(
const rb_iseq_t *iseq)
1508 if (body->type == ISEQ_TYPE_METHOD) {
1509 return body->location.base_label;
1517rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1519 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1520 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1521 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1522 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1523 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1526static ID iseq_type_id(
enum rb_iseq_type
type);
1529rb_iseq_type(
const rb_iseq_t *iseq)
1531 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1535rb_iseq_coverage(
const rb_iseq_t *iseq)
1537 return ISEQ_COVERAGE(iseq);
1541remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1544 for (; v != (
VALUE)vend; v += stride) {
1545 void *ptr = rb_asan_poisoned_object_p(v);
1546 rb_asan_unpoison_object(v,
false);
1548 if (rb_obj_is_iseq(v)) {
1549 rb_iseq_t *iseq = (rb_iseq_t *)v;
1550 ISEQ_COVERAGE_SET(iseq,
Qnil);
1553 asan_poison_object_if(ptr, v);
1559rb_iseq_remove_coverage_all(
void)
1561 rb_objspace_each_objects(remove_coverage_i, NULL);
1567iseqw_mark_and_move(
void *ptr)
1569 rb_gc_mark_and_move((
VALUE *)ptr);
1573iseqw_memsize(
const void *ptr)
1575 return rb_iseq_memsize(*(
const rb_iseq_t **)ptr);
1581 iseqw_mark_and_move,
1584 iseqw_mark_and_move,
1586 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1590iseqw_new(
const rb_iseq_t *iseq)
1592 if (iseq->wrapper) {
1593 if (*(
const rb_iseq_t **)
rb_check_typeddata(iseq->wrapper, &iseqw_data_type) != iseq) {
1594 rb_raise(
rb_eTypeError,
"wrong iseq wrapper: %" PRIsVALUE
" for %p",
1595 iseq->wrapper, (
void *)iseq);
1597 return iseq->wrapper;
1605 RB_OBJ_SET_FROZEN_SHAREABLE((
VALUE)obj);
1613rb_iseqw_new(
const rb_iseq_t *iseq)
1615 return iseqw_new(iseq);
1624iseqw_s_compile_parser(
int argc,
VALUE *argv,
VALUE self,
bool prism)
1630 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1632 case 5: opt = argv[--i];
1633 case 4: line = argv[--i];
1634 case 3: path = argv[--i];
1635 case 2: file = argv[--i];
1638 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1639 if (
NIL_P(path)) path = file;
1647 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1650 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1653 return iseqw_new(iseq);
1694iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1696 return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p());
1736iseqw_s_compile_parsey(
int argc,
VALUE *argv,
VALUE self)
1738 return iseqw_s_compile_parser(argc, argv, self,
false);
1778iseqw_s_compile_prism(
int argc,
VALUE *argv,
VALUE self)
1780 return iseqw_s_compile_parser(argc, argv, self,
true);
1783static VALUE iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self);
1806iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1808 if (rb_ruby_prism_p()) {
1809 return iseqw_s_compile_file_prism(argc, argv, self);
1816 rb_compile_option_t option;
1819 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1820 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1822 case 2: opt = argv[--i];
1825 file = rb_fstring(file);
1829 rb_execution_context_t *ec = GET_EC();
1830 VALUE v = rb_vm_push_frame_fname(ec, file);
1832 parser = rb_parser_new();
1833 rb_parser_set_context(parser, NULL, FALSE);
1834 ast_value = rb_parser_load_file(parser, file);
1835 iseq_new_setup_coverage(file, ast_line_count(ast_value));
1836 ast = rb_ruby_ast_data_get(ast_value);
1837 if (!ast->body.root) exc = GET_EC()->errinfo;
1840 if (!ast->body.root) {
1841 rb_ast_dispose(ast);
1845 make_compile_option(&option, opt);
1847 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
1849 rb_realpath_internal(
Qnil, file, 1),
1850 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1852 rb_ast_dispose(ast);
1855 rb_vm_pop_frame(ec);
1882iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self)
1885 rb_compile_option_t option;
1888 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1889 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1891 case 2: opt = argv[--i];
1894 file = rb_fstring(file);
1896 rb_execution_context_t *ec = GET_EC();
1897 VALUE v = rb_vm_push_frame_fname(ec, file);
1899 make_compile_option(&option, opt);
1903 result.
node.coverage_enabled = 1;
1905 switch (option.frozen_string_literal) {
1906 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1908 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1909 pm_options_frozen_string_literal_set(&result.
options,
false);
1911 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1912 pm_options_frozen_string_literal_set(&result.
options,
true);
1915 rb_bug(
"iseqw_s_compile_file_prism: invalid frozen_string_literal=%d", option.frozen_string_literal);
1920 VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1922 if (error ==
Qnil) {
1925 rb_iseq_t *iseq = pm_iseq_new_with_opt(&result.
node, rb_fstring_lit(
"<main>"),
1927 rb_realpath_internal(
Qnil, file, 1),
1928 1, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1930 pm_parse_result_free(&result);
1934 rb_jump_tag(error_state);
1937 ret = iseqw_new(iseq);
1938 rb_vm_pop_frame(ec);
1943 pm_parse_result_free(&result);
1944 rb_vm_pop_frame(ec);
1981iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
1983 rb_compile_option_t option;
1984 make_compile_option(&option, opt);
1985 COMPILE_OPTION_DEFAULT = option;
1998iseqw_s_compile_option_get(
VALUE self)
2000 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
2003static const rb_iseq_t *
2004iseqw_check(
VALUE iseqw)
2006 rb_iseq_t **iseq_ptr;
2008 rb_iseq_t *iseq = *iseq_ptr;
2010 if (!ISEQ_BODY(iseq)) {
2011 rb_ibf_load_iseq_complete(iseq);
2014 if (!ISEQ_BODY(iseq)->location.label) {
2015 rb_raise(
rb_eTypeError,
"uninitialized InstructionSequence");
2021rb_iseqw_to_iseq(
VALUE iseqw)
2023 return iseqw_check(iseqw);
2035iseqw_eval(
VALUE self)
2037 const rb_iseq_t *iseq = iseqw_check(self);
2038 if (0 == ISEQ_BODY(iseq)->iseq_size) {
2039 rb_raise(
rb_eTypeError,
"attempt to evaluate dummy InstructionSequence");
2041 return rb_iseq_eval(iseq, rb_current_box());
2049iseqw_inspect(
VALUE self)
2051 const rb_iseq_t *iseq = iseqw_check(self);
2055 if (!body->location.label) {
2056 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
2059 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
2061 body->location.label, rb_iseq_path(iseq),
2062 FIX2INT(rb_iseq_first_lineno(iseq)));
2090iseqw_path(
VALUE self)
2092 return rb_iseq_path(iseqw_check(self));
2112iseqw_absolute_path(
VALUE self)
2114 return rb_iseq_realpath(iseqw_check(self));
2141iseqw_label(
VALUE self)
2143 return rb_iseq_label(iseqw_check(self));
2167iseqw_base_label(
VALUE self)
2169 return rb_iseq_base_label(iseqw_check(self));
2183iseqw_first_lineno(
VALUE self)
2185 return rb_iseq_first_lineno(iseqw_check(self));
2188static VALUE iseq_data_to_ary(
const rb_iseq_t *iseq);
2272iseqw_to_a(
VALUE self)
2274 const rb_iseq_t *iseq = iseqw_check(self);
2275 return iseq_data_to_ary(iseq);
2278#if VM_INSN_INFO_TABLE_IMPL == 1
2280get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
2283 size_t size = body->insns_info.size;
2285 const unsigned int *positions = body->insns_info.positions;
2286 const int debug = 0;
2289 printf(
"size: %"PRIuSIZE
"\n", size);
2290 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2291 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2297 else if (size == 1) {
2298 return &insns_info[0];
2301 size_t l = 1, r = size - 1;
2303 size_t m = l + (r - l) / 2;
2304 if (positions[m] == pos) {
2305 return &insns_info[m];
2307 if (positions[m] < pos) {
2315 return &insns_info[size-1];
2317 if (positions[l] > pos) {
2318 return &insns_info[l-1];
2320 return &insns_info[l];
2325get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2327 return get_insn_info_binary_search(iseq, pos);
2331#if VM_INSN_INFO_TABLE_IMPL == 2
2333get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
2336 size_t size = body->insns_info.size;
2338 const int debug = 0;
2341#if VM_CHECK_MODE > 0
2342 const unsigned int *positions = body->insns_info.positions;
2343 printf(
"size: %"PRIuSIZE
"\n", size);
2344 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2345 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2347 printf(
"size: %"PRIuSIZE
"\n", size);
2348 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
2349 (
size_t)0, insns_info[0].line_no, pos);
2356 else if (size == 1) {
2357 return &insns_info[0];
2361 VM_ASSERT(body->insns_info.succ_index_table != NULL);
2362 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
2363 return &insns_info[index-1];
2368get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2370 return get_insn_info_succinct_bitvector(iseq, pos);
2374#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2376get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
2379 size_t i = 0, size = body->insns_info.size;
2381 const unsigned int *positions = body->insns_info.positions;
2382 const int debug = 0;
2385 printf(
"size: %"PRIuSIZE
"\n", size);
2386 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2387 i, positions[i], insns_info[i].line_no, pos);
2393 else if (size == 1) {
2394 return &insns_info[0];
2397 for (i=1; i<size; i++) {
2398 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2399 i, positions[i], insns_info[i].line_no, pos);
2401 if (positions[i] == pos) {
2402 return &insns_info[i];
2404 if (positions[i] > pos) {
2405 return &insns_info[i-1];
2409 return &insns_info[i-1];
2413#if VM_INSN_INFO_TABLE_IMPL == 0
2415get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2417 return get_insn_info_linear_search(iseq, pos);
2421#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2423validate_get_insn_info(
const rb_iseq_t *iseq)
2427 for (i = 0; i < body->iseq_size; i++) {
2428 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2429 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
2436rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
2441 return entry->line_no;
2448#ifdef USE_ISEQ_NODE_ID
2450rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
2455 return entry->node_id;
2464rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2468 return entry->events;
2475static void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2480rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos,
rb_event_flag_t reset)
2487 entry->events &= ~reset;
2488 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2489 rb_iseq_trace_flag_cleared(iseq, pos);
2496local_var_name(
const rb_iseq_t *diseq,
VALUE level,
VALUE op)
2503 for (i = 0; i < level; i++) {
2504 diseq = ISEQ_BODY(diseq)->parent_iseq;
2506 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2507 lid = ISEQ_BODY(diseq)->local_table[idx];
2508 name = rb_id2str(lid);
2518 rb_str_catf(name,
"@%d", idx);
2522int rb_insn_unified_local_var_level(
VALUE);
2526rb_insn_operand_intern(
const rb_iseq_t *iseq,
2530 const char *types = insn_op_types(insn);
2531 char type = types[op_no];
2536 ret = rb_sprintf(
"%"PRIdVALUE, (
VALUE)(pos +
len + op));
2540 if (insn == BIN(defined) && op_no == 0) {
2541 enum defined_type deftype = (
enum defined_type)op;
2544 ret = rb_fstring_lit(
"func");
2547 ret = rb_fstring_lit(
"ref");
2549 case DEFINED_CONST_FROM:
2550 ret = rb_fstring_lit(
"constant-from");
2553 ret = rb_iseq_defined_string(deftype);
2558 else if (insn == BIN(checktype) && op_no == 0) {
2564 ret = rb_sprintf(
"%"PRIuVALUE, op);
2569 if (types[op_no+1] == TS_NUM && pnop) {
2570 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2572 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2573 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2585 op = obj_resurrect(op);
2586 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2591 ret = rb_sprintf(
":$%c", (
type >> 1));
2594 ret = rb_sprintf(
":$%d", (
type >> 1));
2599 ret = rb_dump_literal(op);
2610 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2611 ret = ISEQ_BODY(iseq)->location.label;
2625 const ID *segments = ((IC)op)->segments;
2628 rb_str_catf(ret,
"::%s", rb_id2name(*segments++));
2644 ID mid = vm_ci_mid(ci);
2647 rb_ary_push(ary, rb_sprintf(
"mid:%"PRIsVALUE, rb_id2str(mid)));
2650 rb_ary_push(ary, rb_sprintf(
"argc:%d", vm_ci_argc(ci)));
2652 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2658 if (vm_ci_flag(ci)) {
2660# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2661 CALL_FLAG(ARGS_SPLAT);
2662 CALL_FLAG(ARGS_SPLAT_MUT);
2663 CALL_FLAG(ARGS_BLOCKARG);
2666 CALL_FLAG(ARGS_SIMPLE);
2667 CALL_FLAG(TAILCALL);
2671 CALL_FLAG(KW_SPLAT);
2672 CALL_FLAG(KW_SPLAT_MUT);
2673 CALL_FLAG(FORWARDING);
2674 CALL_FLAG(OPT_SEND);
2690 if (dladdr((
void *)op, &info) && info.dli_sname) {
2702 ret = rb_sprintf(
"<builtin!%s/%d>",
2703 bf->name, bf->argc);
2708 rb_bug(
"unknown operand type: %c",
type);
2714right_strip(
VALUE str)
2716 const char *beg = RSTRING_PTR(str), *end =
RSTRING_END(str);
2717 while (end-- > beg && *end ==
' ');
2727rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2728 const rb_iseq_t *iseq,
VALUE child)
2730 VALUE insn = code[pos];
2731 int len = insn_len(insn);
2733 const char *types = insn_op_types(insn);
2735 const char *insn_name_buff;
2737 insn_name_buff = insn_name(insn);
2739 extern const int rb_vm_max_insn_name_size;
2740 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2743 rb_str_catf(str,
"%04"PRIuSIZE
" %-28.*s ", pos,
2744 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2747 for (j = 0; types[j]; j++) {
2748 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2749 len, pos, &code[pos + j + 2],
2759 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2760 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2761 if (line_no && line_no != prev) {
2762 long slen = RSTRING_LEN(str);
2763 slen = (slen > 70) ? 0 : (70 - slen);
2764 str = rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2771 str = rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s%s]",
2782 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2783 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2793 printf(
"%.*s\n", (
int)RSTRING_LEN(str), RSTRING_PTR(str));
2802 case CATCH_TYPE_RESCUE:
2804 case CATCH_TYPE_ENSURE:
2806 case CATCH_TYPE_RETRY:
2808 case CATCH_TYPE_BREAK:
2810 case CATCH_TYPE_REDO:
2812 case CATCH_TYPE_NEXT:
2815 rb_bug(
"unknown catch type: %d",
type);
2821iseq_inspect(
const rb_iseq_t *iseq)
2824 if (!body->location.label) {
2825 return rb_sprintf(
"#<ISeq: uninitialized>");
2828 const rb_code_location_t *loc = &body->location.code_location;
2829 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2830 body->location.label, rb_iseq_path(iseq),
2831 loc->beg_pos.lineno,
2832 loc->beg_pos.lineno,
2833 loc->beg_pos.column,
2834 loc->end_pos.lineno,
2835 loc->end_pos.column);
2841 {(void (*)(
void *))rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2842 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2846rb_iseq_disasm_recursive(const rb_iseq_t *iseq,
VALUE indent)
2856 enum {header_minlen = 72};
2857 st_table *done_iseq = 0;
2859 const char *indent_str;
2862 size = body->iseq_size;
2864 indent_len = RSTRING_LEN(indent);
2865 indent_str = RSTRING_PTR(indent);
2871 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2875 if (iseq->body->builtin_attrs) {
2876#define disasm_builtin_attr(str, iseq, attr) \
2877 if (iseq->body->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
2878 rb_str_cat2(str, " " #attr); \
2880 disasm_builtin_attr(str, iseq, LEAF);
2881 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
2882 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
2883 disasm_builtin_attr(str, iseq, C_TRACE);
2888 if (body->catch_table) {
2892 if (body->catch_table) {
2894 indent_str = RSTRING_PTR(indent);
2895 for (i = 0; i < body->catch_table->size; i++) {
2897 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2900 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2901 catch_type((
int)entry->type), (
int)entry->start,
2902 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2903 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2904 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2906 done_iseq = st_init_numtable();
2909 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2910 indent_str = RSTRING_PTR(indent);
2913 rb_str_resize(indent, indent_len);
2914 indent_str = RSTRING_PTR(indent);
2916 if (body->catch_table) {
2918 rb_str_cat2(str,
"|-------------------------------------"
2919 "-----------------------------------\n");
2923 if (body->local_table) {
2924 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
2927 "local table (size: %d, argc: %d "
2928 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2929 body->local_table_size,
2930 body->param.lead_num,
2931 body->param.opt_num,
2932 body->param.flags.has_rest ? body->param.rest_start : -1,
2933 body->param.post_num,
2934 body->param.flags.has_block ? body->param.block_start : -1,
2935 body->param.flags.has_kw ? keyword->num : -1,
2936 body->param.flags.has_kw ? keyword->required_num : -1,
2937 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2939 for (i = body->local_table_size; i > 0;) {
2940 int li = body->local_table_size - --i - 1;
2942 VALUE name = local_var_name(iseq, 0, i);
2947 if (body->param.flags.has_opt) {
2948 int argc = body->param.lead_num;
2949 int opts = body->param.opt_num;
2950 if (li >= argc && li < argc + opts) {
2951 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2952 body->param.opt_table[li - argc]);
2956 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2957 (body->param.lead_num > li) ? (body->param.flags.ambiguous_param0 ?
"AmbiguousArg" :
"Arg") :
"",
2959 (body->param.flags.has_rest && body->param.rest_start == li) ? (body->param.flags.anon_rest ?
"AnonRest" :
"Rest") :
"",
2960 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ?
"Post" :
"",
2961 (body->param.flags.has_kwrest && keyword->rest_start == li) ? (body->param.flags.anon_kwrest ?
"AnonKwrest" :
"Kwrest") :
"",
2962 (body->param.flags.has_block && body->param.block_start == li) ?
"Block" :
"");
2965 rb_str_catf(str,
"[%2d] ", i + 1);
2966 width = RSTRING_LEN(str) + 11;
2968 if (*argi) rb_str_catf(str,
"<%s>", argi);
2969 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str,
"%*s", (
int)width,
"");
2975 code = rb_iseq_original_iseq(iseq);
2976 for (n = 0; n < size;) {
2978 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2983 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2985 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2986 indent_str = RSTRING_PTR(indent);
2994rb_iseq_disasm(
const rb_iseq_t *iseq)
2997 rb_str_resize(str, RSTRING_LEN(str));
3007rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
3009 struct rb_id_table * iv_names = rb_id_table_create(0);
3011 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
3012 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
3014 if (cache->iv_set_name) {
3015 rb_id_table_insert(iv_names, cache->iv_set_name,
Qtrue);
3019 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
3022 if (!
NIL_P(superclass)) {
3023 count += RCLASS_MAX_IV_COUNT(superclass);
3026 rb_id_table_free(iv_names);
3050iseqw_disasm(
VALUE self)
3052 return rb_iseq_disasm(iseqw_check(self));
3056iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
3059 VALUE *code = rb_iseq_original_iseq(iseq);
3061 const rb_iseq_t *child;
3064 if (body->catch_table) {
3065 for (i = 0; i < body->catch_table->size; i++) {
3067 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
3068 child = entry->iseq;
3070 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3071 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3072 (*iter_func)(child, data);
3078 for (i=0; i<body->iseq_size;) {
3079 VALUE insn = code[i];
3080 int len = insn_len(insn);
3081 const char *types = insn_op_types(insn);
3084 for (j=0; types[j]; j++) {
3087 child = (
const rb_iseq_t *)code[i+j+1];
3089 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3090 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3091 (*iter_func)(child, data);
3106yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
3120iseqw_each_child(
VALUE self)
3122 const rb_iseq_t *iseq = iseqw_check(self);
3123 iseq_iterate_children(iseq, yield_each_children, NULL);
3130#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
3150iseqw_trace_points(
VALUE self)
3152 const rb_iseq_t *iseq = iseqw_check(self);
3157 for (i=0; i<body->insns_info.size; i++) {
3159 if (entry->events) {
3160 push_event_info(iseq, entry->events, entry->line_no, ary);
3204 const rb_iseq_t *iseq = NULL;
3206 if (rb_frame_info_p(body)) {
3207 iseq = rb_get_iseq_from_frame_info(body);
3210 iseq = vm_proc_iseq(body);
3212 if (!rb_obj_is_iseq((
VALUE)iseq)) {
3217 iseq = rb_method_iseq(body);
3219 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
3223 return iseq ? iseqw_new(iseq) :
Qnil;
3281 VALUE iseqw = iseqw_s_of(klass, body);
3282 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3286register_label(
struct st_table *table,
unsigned long idx)
3289 st_insert(table, idx, sym);
3298 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
3299 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
3300 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
3301 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
3302 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
3303 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
3305 rb_bug(
"unknown exception type: %d", (
int)
type);
3320 {(void (*)(
void *))rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
3321 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3324#define DECL_ID(name) \
3327#define INIT_ID(name) \
3328 id_##name = rb_intern(#name)
3331iseq_type_id(
enum rb_iseq_type
type)
3356 case ISEQ_TYPE_TOP:
return id_top;
3357 case ISEQ_TYPE_METHOD:
return id_method;
3358 case ISEQ_TYPE_BLOCK:
return id_block;
3359 case ISEQ_TYPE_CLASS:
return id_class;
3360 case ISEQ_TYPE_RESCUE:
return id_rescue;
3361 case ISEQ_TYPE_ENSURE:
return id_ensure;
3362 case ISEQ_TYPE_EVAL:
return id_eval;
3363 case ISEQ_TYPE_MAIN:
return id_main;
3364 case ISEQ_TYPE_PLAIN:
return id_plain;
3367 rb_bug(
"unsupported iseq type: %d", (
int)
type);
3371iseq_data_to_ary(
const rb_iseq_t *iseq)
3379 VALUE *seq, *iseq_original;
3390 static ID insn_syms[VM_BARE_INSTRUCTION_SIZE];
3391 struct st_table *labels_table = st_init_numtable();
3394 if (insn_syms[0] == 0) {
3396 for (i=0; i<numberof(insn_syms); i++) {
3397 insn_syms[i] = rb_intern(insn_name(i));
3402 type = iseq_type_id(iseq_body->type);
3405 for (i=0; i<iseq_body->local_table_size; i++) {
3406 ID lid = iseq_body->local_table[i];
3408 if (lid != idItImplicit && rb_id2str(lid)) {
3422 const struct rb_iseq_param_keyword *
const keyword = iseq_body->param.keyword;
3425 if (iseq_body->param.flags.has_opt) {
3426 int len = iseq_body->param.opt_num + 1;
3429 for (j = 0; j <
len; j++) {
3430 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
3433 rb_hash_aset(params,
ID2SYM(rb_intern(
"opt")), arg_opt_labels);
3437 if (iseq_body->param.flags.has_lead) rb_hash_aset(params,
ID2SYM(rb_intern(
"lead_num")),
INT2FIX(iseq_body->param.lead_num));
3438 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_num")),
INT2FIX(iseq_body->param.post_num));
3439 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_start")),
INT2FIX(iseq_body->param.post_start));
3440 if (iseq_body->param.flags.has_rest) rb_hash_aset(params,
ID2SYM(rb_intern(
"rest_start")),
INT2FIX(iseq_body->param.rest_start));
3441 if (iseq_body->param.flags.has_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"block_start")),
INT2FIX(iseq_body->param.block_start));
3442 if (iseq_body->param.flags.has_kw) {
3445 for (i=0; i<keyword->required_num; i++) {
3448 for (j=0; i<keyword->num; i++, j++) {
3449 VALUE key = rb_ary_new_from_args(1,
ID2SYM(keyword->table[i]));
3450 if (!UNDEF_P(keyword->default_values[j])) {
3456 rb_hash_aset(params,
ID2SYM(rb_intern(
"kwbits")),
3457 INT2FIX(keyword->bits_start));
3458 rb_hash_aset(params,
ID2SYM(rb_intern(
"keyword")), keywords);
3460 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params,
ID2SYM(rb_intern(
"kwrest")),
INT2FIX(keyword->rest_start));
3461 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params,
ID2SYM(rb_intern(
"ambiguous_param0")),
Qtrue);
3462 if (iseq_body->param.flags.use_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"use_block")),
Qtrue);
3466 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3468 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3469 VALUE insn = *seq++;
3470 int j,
len = insn_len(insn);
3475 for (j=0; j<
len-1; j++, seq++) {
3476 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3480 unsigned long idx = nseq - iseq_original + *seq;
3481 rb_ary_push(ary, register_label(labels_table, idx));
3493 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3495 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3506 const ID *ids = ((IC)*seq)->segments;
3526 int argc = vm_ci_argc(ci);
3528 ID mid = vm_ci_mid(ci);
3530 rb_hash_aset(e,
ID2SYM(rb_intern(
"flag")),
UINT2NUM(vm_ci_flag(ci)));
3532 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3537 argc -= kwarg->keyword_len;
3538 for (i = 0; i < kwarg->keyword_len; i++) {
3541 rb_hash_aset(e,
ID2SYM(rb_intern(
"kw_arg")), kw);
3544 rb_hash_aset(e,
ID2SYM(rb_intern(
"orig_argc")),
3562 unsigned long idx = nseq - iseq_original + pos;
3565 register_label(labels_table, idx));
3572#if SIZEOF_VALUE <= SIZEOF_LONG
3583#if SIZEOF_VALUE <= SIZEOF_LONG
3588 rb_hash_aset(val,
ID2SYM(rb_intern(
"func_ptr")), func_ptr);
3589 rb_hash_aset(val,
ID2SYM(rb_intern(
"argc")),
INT2NUM(((RB_BUILTIN)*seq)->argc));
3590 rb_hash_aset(val,
ID2SYM(rb_intern(
"index")),
INT2NUM(((RB_BUILTIN)*seq)->index));
3596 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3605 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3608 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3609 rb_ary_push(ary, exception_type2symbol(entry->type));
3611 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3616 rb_ary_push(ary, register_label(labels_table, entry->start));
3617 rb_ary_push(ary, register_label(labels_table, entry->end));
3618 rb_ary_push(ary, register_label(labels_table, entry->cont));
3625 prev_insn_info = NULL;
3626#ifdef USE_ISEQ_NODE_ID
3635 if (st_lookup(labels_table, pos, &label)) {
3639 info = get_insn_info(iseq, pos);
3640#ifdef USE_ISEQ_NODE_ID
3644 if (prev_insn_info != info) {
3645 int line = info->line_no;
3648 if (line > 0 && last_line != line) {
3652#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3662 prev_insn_info = info;
3671 rb_hash_aset(misc,
ID2SYM(rb_intern(
"arg_size")),
INT2FIX(iseq_body->param.size));
3672 rb_hash_aset(misc,
ID2SYM(rb_intern(
"local_size")),
INT2FIX(iseq_body->local_table_size));
3673 rb_hash_aset(misc,
ID2SYM(rb_intern(
"stack_max")),
INT2FIX(iseq_body->stack_max));
3674 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_id")),
INT2FIX(iseq_body->location.node_id));
3675 rb_hash_aset(misc,
ID2SYM(rb_intern(
"code_location")),
3676 rb_ary_new_from_args(4,
3677 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3678 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3679 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3680 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3681#ifdef USE_ISEQ_NODE_ID
3682 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_ids")), node_ids);
3684 rb_hash_aset(misc,
ID2SYM(rb_intern(
"parser")), iseq_body->prism ?
ID2SYM(rb_intern(
"prism")) :
ID2SYM(rb_intern(
"parse.y")));
3709rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3713 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
3715 ID req, opt, rest, block, key, keyrest;
3716#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3717#define PARAM_ID(i) body->local_table[(i)]
3718#define PARAM(i, type) ( \
3720 PARAM_ID(i) != idItImplicit && rb_id2str(PARAM_ID(i)) ? \
3721 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3727 if (body->param.flags.forwardable) {
3738 for (i = 0; i < body->param.lead_num; i++) {
3743 for (i = 0; i < body->param.lead_num; i++) {
3747 r = body->param.lead_num + body->param.opt_num;
3748 for (; i < r; i++) {
3751 if (body->param.flags.has_rest) {
3753 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3755 r = body->param.post_start + body->param.post_num;
3757 for (i = body->param.post_start; i < r; i++) {
3762 for (i = body->param.post_start; i < r; i++) {
3766 if (body->param.flags.accepts_no_kwarg) {
3772 if (body->param.flags.has_kw) {
3774 if (keyword->required_num > 0) {
3777 for (; i < keyword->required_num; i++) {
3779 if (rb_id2str(keyword->table[i])) {
3786 for (; i < keyword->num; i++) {
3788 if (rb_id2str(keyword->table[i])) {
3794 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3797 PARAM_TYPE(keyrest);
3798 if (body->param.flags.has_kwrest &&
3799 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3802 else if (body->param.flags.ruby2_keywords) {
3807 if (body->param.flags.has_block) {
3809 rb_ary_push(args, PARAM(body->param.block_start, block));
3815rb_iseq_defined_string(
enum defined_type
type)
3817 static const char expr_names[][18] = {
3819 "instance-variable",
3835 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names)) rb_bug(
"unknown defined type %d",
type);
3836 estr = expr_names[
type - 1];
3837 return rb_fstring_cstr(estr);
3843static st_table *encoded_insn_data;
3847 void *notrace_encoded_insn;
3848 void *trace_encoded_insn;
3851 void *zjit_encoded_insn;
3854static insn_data_t insn_data[VM_BARE_INSTRUCTION_SIZE];
3857rb_free_encoded_insn_data(
void)
3859 st_free_table(encoded_insn_data);
3865rb_vm_encoded_insn_data_table_init(
void)
3867#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3868 const void *
const *table = rb_vm_get_insns_address_table();
3869#define INSN_CODE(insn) ((VALUE)table[insn])
3871#define INSN_CODE(insn) ((VALUE)(insn))
3873 encoded_insn_data = st_init_numtable_with_size(VM_BARE_INSTRUCTION_SIZE);
3875 for (
int insn = 0; insn < VM_BARE_INSTRUCTION_SIZE; insn++) {
3876 insn_data[insn].insn = insn;
3877 insn_data[insn].insn_len = insn_len(insn);
3882 int notrace_insn = (insn != BIN(opt_invokebuiltin_delegate_leave)) ? insn : BIN(opt_invokebuiltin_delegate);
3883 insn_data[insn].notrace_encoded_insn = (
void *)INSN_CODE(notrace_insn);
3884 insn_data[insn].trace_encoded_insn = (
void *)INSN_CODE(notrace_insn + VM_BARE_INSTRUCTION_SIZE);
3886 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3887 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_BARE_INSTRUCTION_SIZE);
3888 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3889 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3892 int zjit_insn = vm_bare_insn_to_zjit_insn(insn);
3893 insn_data[insn].zjit_insn = zjit_insn;
3894 insn_data[insn].zjit_encoded_insn = (insn != zjit_insn) ? (
void *)INSN_CODE(zjit_insn) : 0;
3896 if (insn != zjit_insn) {
3897 st_data_t key3 = (st_data_t)INSN_CODE(zjit_insn);
3898 st_add_direct(encoded_insn_data, key3, (st_data_t)&insn_data[insn]);
3908rb_vm_insn_addr2insn(
const void *addr)
3910 st_data_t key = (st_data_t)addr;
3913 if (st_lookup(encoded_insn_data, key, &val)) {
3914 insn_data_t *e = (insn_data_t *)val;
3915 return (
int)e->insn;
3918 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3924rb_vm_insn_addr2opcode(
const void *addr)
3926 st_data_t key = (st_data_t)addr;
3929 if (st_lookup(encoded_insn_data, key, &val)) {
3930 insn_data_t *e = (insn_data_t *)val;
3931 int opcode = e->insn;
3932 if (addr == e->trace_encoded_insn) {
3933 opcode += VM_BARE_INSTRUCTION_SIZE;
3936 else if (addr == e->zjit_encoded_insn) {
3937 opcode = e->zjit_insn;
3943 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3950rb_vm_insn_decode(
const VALUE encoded)
3952#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3953 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3955 int insn = (int)encoded;
3962encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_traced)
3964 st_data_t key = (st_data_t)*iseq_encoded_insn;
3967 if (st_lookup(encoded_insn_data, key, &val)) {
3968 insn_data_t *e = (insn_data_t *)val;
3969 if (remain_traced && key == (st_data_t)e->trace_encoded_insn) {
3972 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3976 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3981rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3984 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
3985 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
4006iseq_add_local_tracepoint(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events,
VALUE tpval,
unsigned int target_line, rb_ractor_t *r)
4011 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4012 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4014 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
4015 ASSERT_vm_locking_with_barrier();
4017 for (pc=0; pc<body->iseq_size;) {
4021 unsigned int line = (int)entry->line_no;
4023 if (target_line == 0 || target_line == line) {
4030 if (pc_events & target_events) {
4033 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
4037 rb_hook_list_t *hook_list = rb_iseq_local_hooks(iseq, r,
true);
4038 rb_hook_list_connect_local_tracepoint(hook_list, tpval, target_line);
4039 iseq_mut->aux.exec.local_hooks_cnt++;
4048 unsigned int target_line;
4054iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4057 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line, data->r);
4058 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
4062rb_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)
4064 ASSERT_vm_locking_with_barrier();
4066 if (target_bmethod) {
4067 turnon_events = add_bmethod_events(turnon_events);
4069 data.turnon_events = turnon_events;
4071 data.target_line = target_line;
4073 data.r = GET_RACTOR();
4075 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
4076 if (0) fprintf(stderr,
"Iseq disasm:\n:%s", RSTRING_PTR(rb_iseq_disasm(iseq)));
4081iseq_remove_local_tracepoint(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4084 unsigned int num_hooks_left;
4087 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4088 rb_hook_list_t *hook_list;
4089 VALUE *iseq_encoded;
4090 ASSERT_vm_locking_with_barrier();
4092 hook_list = rb_iseq_local_hooks(iseq, r,
false);
4098 if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) {
4100 iseq_mut->aux.exec.local_hooks_cnt--;
4101 local_events = hook_list->events;
4102 num_hooks_left = rb_hook_list_count(hook_list);
4103 if (local_events == 0 && prev_events != 0) {
4104 st_delete(rb_ractor_targeted_hooks(r), (st_data_t*)&iseq, NULL);
4105 rb_hook_list_free(hook_list);
4108 if (iseq->aux.exec.local_hooks_cnt == num_hooks_left) {
4109 body = ISEQ_BODY(iseq);
4110 iseq_encoded = (
VALUE *)body->iseq_encoded;
4111 local_events = add_bmethod_events(local_events);
4112 for (pc = 0; pc<body->iseq_size;) {
4114 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
4131iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4134 data->n += iseq_remove_local_tracepoint(iseq, data->tpval, data->r);
4135 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
4139rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4142 ASSERT_vm_locking_with_barrier();
4147 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
4152rb_iseq_trace_set(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events)
4154 if (iseq->aux.exec.global_trace_events == turnon_events) {
4158 if (!ISEQ_EXECUTABLE_P(iseq)) {
4167 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4169 rb_hook_list_t *local_hooks = rb_iseq_local_hooks(iseq, GET_RACTOR(),
false);
4171 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
4172 enabled_events = add_bmethod_events(turnon_events | local_events);
4174 for (pc=0; pc<body->iseq_size;) {
4176 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
4184clear_attr_cc(
VALUE v)
4186 ASSERT_vm_locking_with_barrier();
4187 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((
const struct rb_callcache *)v)) {
4199 ASSERT_vm_locking_with_barrier();
4200 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((
const struct rb_callcache *)v)) {
4210clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4213 for (; v != (
VALUE)vend; v += stride) {
4214 void *ptr = rb_asan_poisoned_object_p(v);
4215 rb_asan_unpoison_object(v,
false);
4217 asan_poison_object_if(ptr, v);
4223rb_clear_attr_ccs(
void)
4227 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
4232clear_bf_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4235 for (; v != (
VALUE)vend; v += stride) {
4236 void *ptr = rb_asan_poisoned_object_p(v);
4237 rb_asan_unpoison_object(v,
false);
4239 asan_poison_object_if(ptr, v);
4245rb_clear_bf_ccs(
void)
4247 ASSERT_vm_locking_with_barrier();
4248 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
4252trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4257 for (; v != (
VALUE)vend; v += stride) {
4258 void *ptr = rb_asan_poisoned_object_p(v);
4259 rb_asan_unpoison_object(v,
false);
4261 if (rb_obj_is_iseq(v)) {
4262 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
4264 else if (clear_attr_cc(v)) {
4266 else if (clear_bf_cc(v)) {
4269 asan_poison_object_if(ptr, v);
4279 rb_objspace_each_objects(trace_set_i, &turnon_events);
4284rb_iseqw_local_variables(
VALUE iseqval)
4286 return rb_iseq_local_variables(iseqw_check(iseqval));
4307iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
4310 return rb_iseq_ibf_dump(iseqw_check(self), opt);
4327iseqw_s_load_from_binary(
VALUE self,
VALUE str)
4329 return iseqw_new(rb_iseq_ibf_load(str));
4339iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
4341 return rb_iseq_ibf_load_extra_data(str);
4344#if VM_INSN_INFO_TABLE_IMPL == 2
4377#define IMMEDIATE_TABLE_SIZE 54
4379struct succ_index_table {
4380 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4381 struct succ_dict_block {
4383 uint64_t small_block_ranks;
4384 uint64_t bits[512/64];
4385 } succ_part[FLEX_ARY_LEN];
4388#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4389#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4390#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4391#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4393static struct succ_index_table *
4394succ_index_table_create(
int max_pos,
int *data,
int size)
4396 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4397 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4398 struct succ_index_table *sd =
4399 rb_xcalloc_mul_add_mul(
4400 imm_size,
sizeof(uint64_t),
4401 succ_size,
sizeof(
struct succ_dict_block));
4405 for (j = 0; j < imm_size; j++) {
4406 for (i = 0; i < 9; i++) {
4407 if (r < size && data[r] == j * 9 + i) r++;
4408 imm_block_rank_set(sd->imm_part[j], i, r);
4411 for (k = 0; k < succ_size; k++) {
4412 struct succ_dict_block *sd_block = &sd->succ_part[k];
4415 for (j = 0; j < 8; j++) {
4417 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4418 for (i = 0; i < 64; i++) {
4419 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4420 bits |= ((uint64_t)1) << i;
4424 sd_block->bits[j] = bits;
4425 small_rank += rb_popcount64(bits);
4431static unsigned int *
4432succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
4434 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4435 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4436 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
4437 int i, j, k, r = -1;
4439 for (j = 0; j < imm_size; j++) {
4440 for (i = 0; i < 9; i++) {
4441 int nr = imm_block_rank_get(sd->imm_part[j], i);
4442 if (r != nr) *p++ = j * 9 + i;
4446 for (k = 0; k < succ_size; k++) {
4447 for (j = 0; j < 8; j++) {
4448 for (i = 0; i < 64; i++) {
4449 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4450 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4459succ_index_lookup(
const struct succ_index_table *sd,
int x)
4461 if (x < IMMEDIATE_TABLE_SIZE) {
4462 const int i = x / 9;
4463 const int j = x % 9;
4464 return imm_block_rank_get(sd->imm_part[i], j);
4467 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4468 const struct succ_dict_block *block = &sd->succ_part[block_index];
4469 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4470 const int small_block_index = block_bit_index / 64;
4471 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4472 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4474 return block->rank + small_block_popcount + popcnt;
4493iseqw_script_lines(
VALUE self)
4495 const rb_iseq_t *iseq = iseqw_check(self);
4496 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.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
VALUE rb_io_close(VALUE io)
Closes the IO.
int rb_is_local_id(ID id)
Classifies the given ID, then sees if it is a local variable.
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_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).
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.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
#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.
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
ruby_value_type
C-level type of an object.