12#define RUBY_VM_INSNS_INFO 1
15#include "ruby/internal/config.h"
21#include "eval_intern.h"
25#include "internal/bits.h"
26#include "internal/class.h"
27#include "internal/compile.h"
28#include "internal/error.h"
29#include "internal/file.h"
30#include "internal/hash.h"
31#include "internal/parse.h"
32#include "internal/sanitizers.h"
33#include "internal/symbol.h"
34#include "internal/thread.h"
35#include "internal/variable.h"
40#include "vm_callinfo.h"
45#include "insns_info.inc"
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);
57#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
60obj_resurrect(VALUE obj)
62 if (hidden_obj_p(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);
108 RUBY_FREE_ENTER(
"iseq");
110 if (iseq && iseq->body) {
112 mjit_free_iseq(iseq);
113 rb_yjit_iseq_free(body);
116 if (body->insns_info.positions)
ruby_xfree((
void *)body->insns_info.positions);
117#if VM_INSN_INFO_TABLE_IMPL == 2
118 if (body->insns_info.succ_index_table)
ruby_xfree(body->insns_info.succ_index_table);
120 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
124 if (body->call_data) {
130 if (body->param.keyword != NULL) {
131 ruby_xfree((
void *)body->param.keyword->default_values);
134 compile_data_free(ISEQ_COMPILE_DATA(iseq));
135 if (body->outer_variables) rb_id_table_free(body->outer_variables);
139 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
140 rb_hook_list_free(iseq->aux.exec.local_hooks);
143 RUBY_FREE_LEAVE(
"iseq");
146#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
148rb_vm_insn_addr2insn2(
const void *addr)
150 return (VALUE)rb_vm_insn_addr2insn(addr);
155rb_vm_insn_null_translator(
const void *addr)
160typedef VALUE iseq_value_itr_t(
void *ctx, VALUE obj);
161typedef VALUE rb_vm_insns_translator_t(
const void *addr);
164iseq_extract_values(VALUE *code,
size_t pos, iseq_value_itr_t * func,
void *data, rb_vm_insns_translator_t * translator)
166 VALUE insn = translator((
void *)code[pos]);
167 int len = insn_len(insn);
169 const char *types = insn_op_types(insn);
171 for (op_no = 0; types[op_no]; op_no++) {
172 char type = types[op_no];
178 VALUE op = code[pos + op_no + 1];
180 VALUE newop = func(data, op);
182 code[pos + op_no + 1] = newop;
189 IC ic = (
IC)code[pos + op_no + 1];
191 VALUE nv = func(data, (VALUE)ic->entry);
192 if ((VALUE)ic->entry != nv) {
193 ic->entry = (
void *)nv;
200 IVC ivc = (
IVC)code[pos + op_no + 1];
203 rb_bug(
"!! %u", ivc->entry->index);
205 VALUE nv = func(data, ivc->entry->class_value);
206 if (ivc->entry->class_value != nv) {
207 ivc->entry->class_value = nv;
215 if (is->once.value) {
216 VALUE nv = func(data, is->once.value);
217 if (is->once.value != nv) {
232rb_iseq_each_value(
const rb_iseq_t *iseq, iseq_value_itr_t * func,
void *data)
237 rb_vm_insns_translator_t *
const translator =
238#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
239 (
FL_TEST((VALUE)iseq, ISEQ_TRANSLATED)) ? rb_vm_insn_addr2insn2 :
241 rb_vm_insn_null_translator;
244 size = body->iseq_size;
245 code = body->iseq_encoded;
247 for (n = 0; n < size;) {
248 n += iseq_extract_values(code, n, func, data, translator);
253update_each_insn_value(
void *ctx, VALUE obj)
259rb_iseq_update_references(
rb_iseq_t *iseq)
264 body->variable.coverage =
rb_gc_location(body->variable.coverage);
265 body->variable.pc2branchindex =
rb_gc_location(body->variable.pc2branchindex);
266 body->variable.script_lines =
rb_gc_location(body->variable.script_lines);
268 body->location.base_label =
rb_gc_location(body->location.base_label);
270 if (body->local_iseq) {
273 if (body->parent_iseq) {
276 if (body->mandatory_only_iseq) {
279 if (body->call_data) {
280 for (
unsigned int i=0; i<body->ci_size; i++) {
288 if (
FL_TEST((VALUE)iseq, ISEQ_MARKABLE_ISEQ)) {
289 rb_iseq_each_value(iseq, update_each_insn_value, NULL);
290 VALUE *original_iseq = ISEQ_ORIGINAL_ISEQ(iseq);
293 const unsigned int size = body->iseq_size;
295 n += iseq_extract_values(original_iseq, n, update_each_insn_value, NULL, rb_vm_insn_null_translator);
300 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
303 i = body->param.keyword->required_num;
305 for (j = 0; i < body->param.keyword->num; i++, j++) {
306 VALUE obj = body->param.keyword->default_values[j];
313 if (body->catch_table) {
316 for (i = 0; i < table->size; i++) {
318 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
325 mjit_update_references(iseq);
327 rb_yjit_iseq_update_references(body);
332each_insn_value(
void *ctx, VALUE obj)
341 RUBY_MARK_ENTER(
"iseq");
343 RUBY_MARK_UNLESS_NULL(iseq->wrapper);
348 if (
FL_TEST((VALUE)iseq, ISEQ_MARKABLE_ISEQ)) {
349 rb_iseq_each_value(iseq, each_insn_value, NULL);
358 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->mandatory_only_iseq);
359 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->parent_iseq);
361 if (body->call_data) {
363 for (
unsigned int i=0; i<body->ci_size; i++) {
367 if (vm_ci_markable(ci)) {
372 VM_ASSERT((cc->flags & VM_CALLCACHE_ON_STACK) == 0);
374 if (vm_cc_markable(cc)) {
375 if (!vm_cc_invalidated_p(cc)) {
379 cds[i].cc = rb_vm_empty_cc();
386 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
387 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
390 i = keyword->required_num;
392 for (j = 0; i < keyword->num; i++, j++) {
393 VALUE obj = keyword->default_values[j];
400 if (body->catch_table) {
403 for (i = 0; i < table->size; i++) {
405 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
413 mjit_mark_cc_entries(body);
415 rb_yjit_iseq_mark(body);
418 if (
FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) {
421 else if (
FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) {
424 rb_iseq_mark_insn_storage(compile_data->insn.storage_head);
426 RUBY_MARK_UNLESS_NULL(compile_data->err_info);
427 if (
RTEST(compile_data->catch_table_ary)) {
430 VM_ASSERT(compile_data != NULL);
434 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
435 if (iseq->aux.exec.local_hooks) {
436 rb_hook_list_mark(iseq->aux.exec.local_hooks);
440 RUBY_MARK_LEAVE(
"iseq");
444param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
448 if (!pkw)
return size;
450 size +=
sizeof(
struct rb_iseq_param_keyword);
451 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
465 if (ISEQ_EXECUTABLE_P(iseq) && body) {
467 size += body->iseq_size *
sizeof(
VALUE);
469 size += body->local_table_size *
sizeof(
ID);
470 if (body->catch_table) {
471 size += iseq_catch_table_bytes(body->catch_table->size);
473 size += (body->param.opt_num + 1) *
sizeof(VALUE);
474 size += param_keyword_size(body->param.keyword);
484 compile_data = ISEQ_COMPILE_DATA(iseq);
490 cur = compile_data->node.storage_head;
501rb_iseq_constant_body_alloc(
void)
512 iseq->body = rb_iseq_constant_body_alloc();
517rb_iseq_pathobj_new(VALUE path, VALUE realpath)
523 if (path == realpath ||
525 pathobj = rb_fstring(path);
528 if (!
NIL_P(realpath)) realpath = rb_fstring(realpath);
530 rb_obj_freeze(pathobj);
536rb_iseq_pathobj_set(
const rb_iseq_t *iseq, VALUE path, VALUE realpath)
539 rb_iseq_pathobj_new(path, realpath));
543iseq_location_setup(
rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
const rb_code_location_t *code_location,
const int node_id)
547 rb_iseq_pathobj_set(iseq, path, realpath);
550 loc->first_lineno = first_lineno;
552 loc->node_id = node_id;
553 loc->code_location = *code_location;
556 loc->code_location.beg_pos.lineno = 0;
557 loc->code_location.beg_pos.column = 0;
558 loc->code_location.end_pos.lineno = -1;
559 loc->code_location.end_pos.column = -1;
569 const VALUE
type = body->type;
572 if (
type == ISEQ_TYPE_TOP) {
573 body->local_iseq = iseq;
575 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
576 body->local_iseq = iseq;
579 body->local_iseq = piseq->body->local_iseq;
583 body->parent_iseq = piseq;
586 if (
type == ISEQ_TYPE_MAIN) {
587 body->local_iseq = iseq;
596 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
601 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
608 VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
const rb_code_location_t *code_location,
const int node_id,
609 const rb_iseq_t *parent,
int isolated_depth,
enum iseq_type
type,
613 VALUE err_info =
Qnil;
616 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
620 set_relation(iseq, parent);
622 name = rb_fstring(name);
623 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
624 if (iseq != body->local_iseq) {
625 RB_OBJ_WRITE(iseq, &body->location.base_label, body->local_iseq->body->location.label);
627 ISEQ_COVERAGE_SET(iseq,
Qnil);
628 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
629 body->variable.flip_count = 0;
631 if (
NIL_P(script_lines)) {
638 ISEQ_COMPILE_DATA_ALLOC(iseq);
639 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
642 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
643 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
644 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
645 ISEQ_COMPILE_DATA(iseq)->option = option;
646 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
647 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
650 if (option->coverage_enabled) {
651 VALUE coverages = rb_get_coverages();
652 if (
RTEST(coverages)) {
657 ISEQ_COVERAGE_SET(iseq, coverage);
658 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
664#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
665static void validate_get_insn_info(
const rb_iseq_t *iseq);
669rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
671#if VM_INSN_INFO_TABLE_IMPL == 2
674 int size = body->insns_info.size;
675 int max_pos = body->iseq_size;
676 int *data = (
int *)body->insns_info.positions;
677 if (body->insns_info.succ_index_table)
ruby_xfree(body->insns_info.succ_index_table);
678 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
679#if VM_CHECK_MODE == 0
681 body->insns_info.positions = NULL;
686#if VM_INSN_INFO_TABLE_IMPL == 2
690 int size = body->insns_info.size;
691 int max_pos = body->iseq_size;
693 return succ_index_table_invert(max_pos, sd, size);
700 iseq->aux.exec.global_trace_events = 0;
701 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
702 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
711 VALUE err = data->err_info;
712 ISEQ_COMPILE_DATA_CLEAR(iseq);
713 compile_data_free(data);
715#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
716 validate_get_insn_info(iseq);
720 VALUE path = pathobj_path(body->location.pathobj);
726 RB_DEBUG_COUNTER_INC(iseq_num);
727 RB_DEBUG_COUNTER_ADD(iseq_cd_num, iseq->body->ci_size);
729 rb_iseq_init_trace(iseq);
734 OPT_INLINE_CONST_CACHE,
735 OPT_PEEPHOLE_OPTIMIZATION,
736 OPT_TAILCALL_OPTIMIZATION,
737 OPT_SPECIALISED_INSTRUCTION,
738 OPT_OPERANDS_UNIFICATION,
739 OPT_INSTRUCTIONS_UNIFICATION,
741 OPT_FROZEN_STRING_LITERAL,
742 OPT_DEBUG_FROZEN_STRING_LITERAL,
751#define SET_COMPILE_OPTION(o, h, mem) \
752 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
753 if (flag == Qtrue) { (o)->mem = 1; } \
754 else if (flag == Qfalse) { (o)->mem = 0; } \
756#define SET_COMPILE_OPTION_NUM(o, h, mem) \
757 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
758 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
760 SET_COMPILE_OPTION(option, opt, inline_const_cache);
761 SET_COMPILE_OPTION(option, opt, peephole_optimization);
762 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
763 SET_COMPILE_OPTION(option, opt, specialized_instruction);
764 SET_COMPILE_OPTION(option, opt, operands_unification);
765 SET_COMPILE_OPTION(option, opt, instructions_unification);
766 SET_COMPILE_OPTION(option, opt, stack_caching);
767 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
768 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
769 SET_COMPILE_OPTION(option, opt, coverage_enabled);
770 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
771#undef SET_COMPILE_OPTION
772#undef SET_COMPILE_OPTION_NUM
779 set_compile_option_from_hash(option, opt);
786 *option = COMPILE_OPTION_DEFAULT;
789 *option = COMPILE_OPTION_FALSE;
791 else if (opt ==
Qtrue) {
794 ((
int *)option)[i] = 1;
797 *option = COMPILE_OPTION_DEFAULT;
798 set_compile_option_from_hash(option, opt);
801 rb_raise(rb_eTypeError,
"Compile option must be Hash/true/false/nil");
808 VALUE opt = rb_hash_new_with_size(11);
809#define SET_COMPILE_OPTION(o, h, mem) \
810 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
811#define SET_COMPILE_OPTION_NUM(o, h, mem) \
812 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
814 SET_COMPILE_OPTION(option, opt, inline_const_cache);
815 SET_COMPILE_OPTION(option, opt, peephole_optimization);
816 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
817 SET_COMPILE_OPTION(option, opt, specialized_instruction);
818 SET_COMPILE_OPTION(option, opt, operands_unification);
819 SET_COMPILE_OPTION(option, opt, instructions_unification);
820 SET_COMPILE_OPTION(option, opt, stack_caching);
821 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
822 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
823 SET_COMPILE_OPTION(option, opt, coverage_enabled);
824 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
826#undef SET_COMPILE_OPTION
827#undef SET_COMPILE_OPTION_NUM
832rb_iseq_new(
const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
835 return rb_iseq_new_with_opt(ast, name, path, realpath,
INT2FIX(0), parent,
836 0,
type, &COMPILE_OPTION_DEFAULT);
842 if (ast->script_lines ==
Qfalse) {
849 return FIX2INT(ast->script_lines);
855 VALUE coverages = rb_get_coverages();
856 if (
RTEST(coverages)) {
857 int line_count = ast_line_count(ast);
858 if (line_count >= 0) {
859 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
860 VALUE coverage = rb_default_coverage(len);
865 return rb_iseq_new_with_opt(ast, name, path, realpath,
INT2FIX(0), parent, 0,
866 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
872 return rb_iseq_new_with_opt(ast, rb_fstring_lit(
"<main>"),
874 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
878rb_iseq_new_eval(
const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
880 return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
881 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
888 VALUE v1 = iseqw_new(iseq);
890 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
899rb_iseq_new_with_opt(
const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
900 VALUE first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
903 const NODE *node = ast ? ast->root : 0;
912 new_opt = COMPILE_OPTION_DEFAULT;
914 if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option);
916 VALUE script_lines =
Qnil;
918 if (ast && !
FIXNUM_P(ast->script_lines) && ast->script_lines) {
919 script_lines = ast->script_lines;
922 script_lines = parent->body->variable.script_lines;
925 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
926 parent, isolated_depth,
type, script_lines, &new_opt);
928 rb_iseq_compile_node(iseq, node);
929 finish_iseq_build(iseq);
931 return iseq_translate(iseq);
935rb_iseq_new_with_callback(
937 VALUE name, VALUE path, VALUE realpath,
938 VALUE first_lineno,
const rb_iseq_t *parent,
944 if (!option) option = &COMPILE_OPTION_DEFAULT;
945 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
947 rb_iseq_compile_callback(iseq, ifunc);
948 finish_iseq_build(iseq);
954rb_iseq_load_iseq(VALUE fname)
959 return iseqw_check(iseqv);
965#define CHECK_ARRAY(v) rb_to_array_type(v)
966#define CHECK_HASH(v) rb_to_hash_type(v)
967#define CHECK_STRING(v) rb_str_to_str(v)
968#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
969static inline VALUE CHECK_INTEGER(VALUE v) {(void)
NUM2LONG(v);
return v;}
972iseq_type_from_sym(VALUE
type)
975 const ID id_method =
rb_intern(
"method");
978 const ID id_rescue =
rb_intern(
"rescue");
979 const ID id_ensure =
rb_intern(
"ensure");
986 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
987 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
988 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
989 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
990 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
991 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
992 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
993 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
994 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
995 return (
enum iseq_type)-1;
999iseq_load(VALUE data,
const rb_iseq_t *parent, VALUE opt)
1003 VALUE magic, version1, version2, format_type, misc;
1004 VALUE name, path, realpath, first_lineno, code_location, node_id;
1005 VALUE
type, body, locals, params, exception;
1007 st_data_t iseq_type;
1017 data = CHECK_ARRAY(data);
1024 ((void)magic, (
void)version1, (void)version2, (
void)format_type);
1029 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1038 iseq->body->local_iseq = iseq;
1040 iseq_type = iseq_type_from_sym(
type);
1041 if (iseq_type == (
enum iseq_type)-1) {
1055 make_compile_option(&option, opt);
1056 option.peephole_optimization = FALSE;
1057 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1058 parent, 0, (
enum iseq_type)iseq_type,
Qnil, &option);
1060 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1062 finish_iseq_build(iseq);
1064 return iseqw_new(iseq);
1071iseq_s_load(
int argc, VALUE *argv, VALUE
self)
1073 VALUE data, opt=
Qnil;
1075 return iseq_load(data, NULL, opt);
1079rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
1081 return iseq_load(data,
RTEST(parent) ? (
rb_iseq_t *)parent : NULL, opt);
1085rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1089#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1090# define INITIALIZED volatile
1094 rb_ast_t *(*parse)(VALUE vparser, VALUE fname, VALUE file,
int start);
1099 make_compile_option(&option, opt);
1103 parse = rb_parser_compile_file_path;
1106 parse = rb_parser_compile_string_path;
1110 const VALUE parser = rb_parser_new();
1111 VALUE name = rb_fstring_lit(
"<compiled>");
1112 const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1113 VALUE outer_scope_v = (
VALUE)outer_scope;
1114 rb_parser_set_context(parser, outer_scope, FALSE);
1116 ast = (*parse)(parser, file, src, ln);
1119 if (!ast->body.root) {
1120 rb_ast_dispose(ast);
1124 INITIALIZED VALUE label = rb_fstring_lit(
"<compiled>");
1125 iseq = rb_iseq_new_with_opt(&ast->body, label, file, realpath, line,
1126 NULL, 0, ISEQ_TYPE_TOP, &option);
1127 rb_ast_dispose(ast);
1136 return pathobj_path(iseq->body->location.pathobj);
1142 return pathobj_realpath(iseq->body->location.pathobj);
1146rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1148 return rb_iseq_realpath(iseq);
1152rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1154 return NIL_P(rb_iseq_realpath(iseq));
1160 return iseq->body->location.label;
1164rb_iseq_base_label(
const rb_iseq_t *iseq)
1166 return iseq->body->location.base_label;
1170rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1172 return iseq->body->location.first_lineno;
1176rb_iseq_method_name(
const rb_iseq_t *iseq)
1180 if (body->type == ISEQ_TYPE_METHOD) {
1181 return body->location.base_label;
1189rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1192 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1193 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1194 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1195 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1198static ID iseq_type_id(
enum iseq_type
type);
1203 return ID2SYM(iseq_type_id(iseq->body->type));
1209 return ISEQ_COVERAGE(iseq);
1213remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1215 VALUE v = (
VALUE)vstart;
1216 for (; v != (
VALUE)vend; v += stride) {
1217 void *ptr = asan_poisoned_object_p(v);
1218 asan_unpoison_object(v,
false);
1220 if (rb_obj_is_iseq(v)) {
1222 ISEQ_COVERAGE_SET(iseq,
Qnil);
1225 asan_poison_object_if(ptr, v);
1231rb_iseq_remove_coverage_all(
void)
1233 rb_objspace_each_objects(remove_coverage_i, NULL);
1239iseqw_mark(
void *ptr)
1245iseqw_memsize(
const void *ptr)
1247 return rb_iseq_memsize((
const rb_iseq_t *)ptr);
1252 {iseqw_mark, NULL, iseqw_memsize,},
1253 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1259 if (iseq->wrapper) {
1260 return iseq->wrapper;
1263 union {
const rb_iseq_t *in;
void *out; } deconst;
1280 return iseqw_new(iseq);
1317iseqw_s_compile(
int argc, VALUE *argv, VALUE
self)
1323 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1325 case 5: opt = argv[--i];
1326 case 4: line = argv[--i];
1327 case 3: path = argv[--i];
1328 case 2: file = argv[--i];
1331 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1332 if (
NIL_P(path)) path = file;
1338 return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
1362iseqw_s_compile_file(
int argc, VALUE *argv, VALUE
self)
1365 VALUE parser, f, exc =
Qnil, ret;
1370 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1371 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1373 case 2: opt = argv[--i];
1376 file = rb_fstring(file);
1380 parser = rb_parser_new();
1381 rb_parser_set_context(parser, NULL, FALSE);
1382 ast = (
rb_ast_t *)rb_parser_load_file(parser, file);
1383 if (!ast->body.root) exc = GET_EC()->errinfo;
1386 if (!ast->body.root) {
1387 rb_ast_dispose(ast);
1391 make_compile_option(&option, opt);
1393 ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit(
"<main>"),
1395 rb_realpath_internal(
Qnil, file, 1),
1396 line, NULL, 0, ISEQ_TYPE_TOP, &option));
1397 rb_ast_dispose(ast);
1433iseqw_s_compile_option_set(VALUE
self, VALUE opt)
1436 make_compile_option(&option, opt);
1437 COMPILE_OPTION_DEFAULT = option;
1450iseqw_s_compile_option_get(VALUE
self)
1452 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1456iseqw_check(VALUE iseqw)
1461 rb_ibf_load_iseq_complete(iseq);
1464 if (!iseq->body->location.label) {
1465 rb_raise(rb_eTypeError,
"uninitialized InstructionSequence");
1471rb_iseqw_to_iseq(VALUE iseqw)
1473 return iseqw_check(iseqw);
1485iseqw_eval(VALUE
self)
1487 return rb_iseq_eval(iseqw_check(
self));
1495iseqw_inspect(VALUE
self)
1497 const rb_iseq_t *iseq = iseqw_check(
self);
1501 if (!body->location.label) {
1502 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
1505 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
1507 body->location.label, rb_iseq_path(iseq),
1508 FIX2INT(rb_iseq_first_lineno(iseq)));
1536iseqw_path(VALUE
self)
1538 return rb_iseq_path(iseqw_check(
self));
1558iseqw_absolute_path(VALUE
self)
1560 return rb_iseq_realpath(iseqw_check(
self));
1587iseqw_label(VALUE
self)
1589 return rb_iseq_label(iseqw_check(
self));
1613iseqw_base_label(VALUE
self)
1615 return rb_iseq_base_label(iseqw_check(
self));
1629iseqw_first_lineno(VALUE
self)
1631 return rb_iseq_first_lineno(iseqw_check(
self));
1634static VALUE iseq_data_to_ary(
const rb_iseq_t *iseq);
1718iseqw_to_a(VALUE
self)
1720 const rb_iseq_t *iseq = iseqw_check(
self);
1721 return iseq_data_to_ary(iseq);
1724#if VM_INSN_INFO_TABLE_IMPL == 1
1726get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
1729 size_t size = body->insns_info.size;
1731 const unsigned int *positions = body->insns_info.positions;
1732 const int debug = 0;
1735 printf(
"size: %"PRIuSIZE
"\n", size);
1736 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1737 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1743 else if (size == 1) {
1744 return &insns_info[0];
1747 size_t l = 1, r = size - 1;
1749 size_t m = l + (r - l) / 2;
1750 if (positions[m] == pos) {
1751 return &insns_info[m];
1753 if (positions[m] < pos) {
1761 return &insns_info[size-1];
1763 if (positions[l] > pos) {
1764 return &insns_info[l-1];
1766 return &insns_info[l];
1771get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1773 return get_insn_info_binary_search(iseq, pos);
1777#if VM_INSN_INFO_TABLE_IMPL == 2
1779get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
1782 size_t size = body->insns_info.size;
1784 const int debug = 0;
1787#if VM_CHECK_MODE > 0
1788 const unsigned int *positions = body->insns_info.positions;
1789 printf(
"size: %"PRIuSIZE
"\n", size);
1790 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1791 (
size_t)0, positions[0], insns_info[0].line_no, pos);
1793 printf(
"size: %"PRIuSIZE
"\n", size);
1794 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
1795 (
size_t)0, insns_info[0].line_no, pos);
1802 else if (size == 1) {
1803 return &insns_info[0];
1807 VM_ASSERT(body->insns_info.succ_index_table != NULL);
1808 index = succ_index_lookup(body->insns_info.succ_index_table, (
int)pos);
1809 return &insns_info[index-1];
1814get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1816 return get_insn_info_succinct_bitvector(iseq, pos);
1820#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
1822get_insn_info_linear_search(
const rb_iseq_t *iseq,
size_t pos)
1825 size_t i = 0, size = body->insns_info.size;
1827 const unsigned int *positions = body->insns_info.positions;
1828 const int debug = 0;
1831 printf(
"size: %"PRIuSIZE
"\n", size);
1832 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1833 i, positions[i], insns_info[i].line_no, pos);
1839 else if (size == 1) {
1840 return &insns_info[0];
1843 for (i=1; i<size; i++) {
1844 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
1845 i, positions[i], insns_info[i].line_no, pos);
1847 if (positions[i] == pos) {
1848 return &insns_info[i];
1850 if (positions[i] > pos) {
1851 return &insns_info[i-1];
1855 return &insns_info[i-1];
1859#if VM_INSN_INFO_TABLE_IMPL == 0
1861get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
1863 return get_insn_info_linear_search(iseq, pos);
1867#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
1869validate_get_insn_info(
const rb_iseq_t *iseq)
1873 for (i = 0; i < body->iseq_size; i++) {
1874 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
1875 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
1882rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
1887 return entry->line_no;
1894#ifdef USE_ISEQ_NODE_ID
1896rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
1901 return entry->node_id;
1909MJIT_FUNC_EXPORTED rb_event_flag_t
1910rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
1914 return entry->events;
1922rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos, rb_event_flag_t reset)
1926 entry->events &= ~reset;
1927 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
1928 void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
1929 rb_iseq_trace_flag_cleared(iseq, pos);
1935local_var_name(
const rb_iseq_t *diseq, VALUE level, VALUE op)
1942 for (i = 0; i < level; i++) {
1943 diseq = diseq->body->parent_iseq;
1945 idx = diseq->body->local_table_size - (int)op - 1;
1946 lid = diseq->body->local_table[idx];
1951 else if (!rb_str_symname_p(name)) {
1961int rb_insn_unified_local_var_level(VALUE);
1962VALUE rb_dump_literal(VALUE lit);
1965rb_insn_operand_intern(
const rb_iseq_t *iseq,
1966 VALUE insn,
int op_no, VALUE op,
1967 int len,
size_t pos,
const VALUE *pnop, VALUE child)
1969 const char *types = insn_op_types(insn);
1970 char type = types[op_no];
1975 ret =
rb_sprintf(
"%"PRIdVALUE, (VALUE)(pos + len + op));
1979 if (insn == BIN(defined) && op_no == 0) {
1980 enum defined_type deftype = (
enum defined_type)op;
1983 ret = rb_fstring_lit(
"func");
1986 ret = rb_fstring_lit(
"ref");
1988 case DEFINED_CONST_FROM:
1989 ret = rb_fstring_lit(
"constant-from");
1992 ret = rb_iseq_defined_string(deftype);
1997 else if (insn == BIN(checktype) && op_no == 0) {
1998 const char *type_str = rb_type_str((
enum ruby_value_type)op);
2008 if (types[op_no+1] == TS_NUM && pnop) {
2009 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2011 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2012 ret = local_var_name(iseq, (VALUE)level, op - VM_ENV_DATA_SIZE);
2015 ret = rb_inspect(
INT2FIX(op));
2020 ret = rb_inspect(
ID2SYM(op));
2024 op = obj_resurrect(op);
2025 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2038 ret = rb_dump_literal(op);
2050 ret = iseq->body->location.label;
2072 ID mid = vm_ci_mid(ci);
2080 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2086 if (vm_ci_flag(ci)) {
2088# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2089 CALL_FLAG(ARGS_SPLAT);
2090 CALL_FLAG(ARGS_BLOCKARG);
2093 CALL_FLAG(ARGS_SIMPLE);
2094 CALL_FLAG(BLOCKISEQ);
2095 CALL_FLAG(TAILCALL);
2099 CALL_FLAG(KW_SPLAT);
2100 CALL_FLAG(KW_SPLAT_MUT);
2101 CALL_FLAG(OPT_SEND);
2117 if (dladdr((
void *)op, &info) && info.dli_sname) {
2130 bf->name, bf->argc);
2141right_strip(VALUE str)
2144 while (end-- > beg && *end ==
' ');
2154rb_iseq_disasm_insn(VALUE ret,
const VALUE *code,
size_t pos,
2157 VALUE insn = code[pos];
2158 int len = insn_len(insn);
2160 const char *types = insn_op_types(insn);
2162 const char *insn_name_buff;
2164 insn_name_buff = insn_name(insn);
2166 extern const int rb_vm_max_insn_name_size;
2167 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2171 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2174 for (j = 0; types[j]; j++) {
2175 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2176 len, pos, &code[pos + j + 2],
2186 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2187 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2188 if (line_no && line_no != prev) {
2190 slen = (slen > 70) ? 0 : (70 - slen);
2191 str =
rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2196 rb_event_flag_t events = rb_iseq_event_flags(iseq, pos);
2198 str =
rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s]",
2208 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2209 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2228 case CATCH_TYPE_RESCUE:
2230 case CATCH_TYPE_ENSURE:
2232 case CATCH_TYPE_RETRY:
2234 case CATCH_TYPE_BREAK:
2236 case CATCH_TYPE_REDO:
2238 case CATCH_TYPE_NEXT:
2250 if (!body->location.label) {
2255 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2256 body->location.label, rb_iseq_path(iseq),
2257 loc->beg_pos.lineno,
2258 loc->beg_pos.lineno,
2259 loc->beg_pos.column,
2260 loc->end_pos.lineno,
2261 loc->end_pos.column);
2267 {(void (*)(
void *))
rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
2268 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2272rb_iseq_disasm_recursive(const
rb_iseq_t *iseq, VALUE indent)
2282 enum {header_minlen = 72};
2284 VALUE done_iseq_wrapper =
Qnil;
2285 const char *indent_str;
2288 size = body->iseq_size;
2297 rb_str_catf(str,
" (catch: %s)", body->catch_except_p ?
"TRUE" :
"FALSE");
2298 if ((l =
RSTRING_LEN(str) - indent_len) < header_minlen) {
2305 if (body->catch_table) {
2309 if (body->catch_table) {
2312 for (i = 0; i < body->catch_table->size; i++) {
2314 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2317 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2318 catch_type((
int)entry->type), (
int)entry->start,
2319 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
2320 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2321 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2323 done_iseq = st_init_numtable();
2326 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2333 if (body->catch_table) {
2335 rb_str_cat2(str,
"|-------------------------------------"
2336 "-----------------------------------\n");
2340 if (body->local_table) {
2341 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
2344 "local table (size: %d, argc: %d "
2345 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2346 body->local_table_size,
2347 body->param.lead_num,
2348 body->param.opt_num,
2349 body->param.flags.has_rest ? body->param.rest_start : -1,
2350 body->param.post_num,
2351 body->param.flags.has_block ? body->param.block_start : -1,
2352 body->param.flags.has_kw ? keyword->num : -1,
2353 body->param.flags.has_kw ? keyword->required_num : -1,
2354 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2356 for (i = body->local_table_size; i > 0;) {
2357 int li = body->local_table_size - --i - 1;
2359 VALUE name = local_var_name(iseq, 0, i);
2364 if (body->param.flags.has_opt) {
2365 int argc = body->param.lead_num;
2366 int opts = body->param.opt_num;
2367 if (li >= argc && li < argc + opts) {
2368 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
2369 body->param.opt_table[li - argc]);
2373 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
2374 body->param.lead_num > li ?
"Arg" :
"",
2376 (body->param.flags.has_rest && body->param.rest_start == li) ?
"Rest" :
"",
2377 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ?
"Post" :
"",
2378 (body->param.flags.has_kwrest && keyword->rest_start == li) ?
"Kwrest" :
"",
2379 (body->param.flags.has_block && body->param.block_start == li) ?
"Block" :
"");
2392 code = rb_iseq_original_iseq(iseq);
2393 for (n = 0; n < size;) {
2395 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2400 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
2413 VALUE str = rb_iseq_disasm_recursive(iseq,
rb_str_new(0, 0));
2437iseqw_disasm(VALUE
self)
2439 return rb_iseq_disasm(iseqw_check(
self));
2443iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
2446 VALUE *code = rb_iseq_original_iseq(iseq);
2449 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2451 if (body->catch_table) {
2452 for (i = 0; i < body->catch_table->size; i++) {
2454 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2455 child = entry->iseq;
2459 (*iter_func)(child, data);
2465 for (i=0; i<body->iseq_size;) {
2466 VALUE insn = code[i];
2467 int len = insn_len(insn);
2468 const char *types = insn_op_types(insn);
2471 for (j=0; types[j]; j++) {
2478 (*iter_func)(child, data);
2493yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
2507iseqw_each_child(VALUE
self)
2509 const rb_iseq_t *iseq = iseqw_check(
self);
2510 iseq_iterate_children(iseq, yield_each_children, NULL);
2515push_event_info(
const rb_iseq_t *iseq, rb_event_flag_t events,
int line, VALUE ary)
2517#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2536iseqw_trace_points(VALUE
self)
2538 const rb_iseq_t *iseq = iseqw_check(
self);
2543 for (i=0; i<body->insns_info.size; i++) {
2545 if (entry->events) {
2546 push_event_info(iseq, entry->events, entry->line_no, ary);
2588iseqw_s_of(VALUE klass, VALUE body)
2593 iseq = vm_proc_iseq(body);
2595 if (!rb_obj_is_iseq((VALUE)iseq)) {
2600 iseq = rb_method_iseq(body);
2602 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2606 return iseq ? iseqw_new(iseq) :
Qnil;
2662iseqw_s_disasm(VALUE klass, VALUE body)
2664 VALUE iseqw = iseqw_s_of(klass, body);
2665 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
2669ruby_node_name(
int node)
2672#include "node_name.inc"
2674 rb_bug(
"unknown node: %d", node);
2680register_label(
struct st_table *table,
unsigned long idx)
2683 st_insert(table, idx, sym);
2688exception_type2symbol(VALUE
type)
2692 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
2693 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
2694 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
2695 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
2696 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
2697 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
2699 rb_bug(
"unknown exception type: %d", (
int)
type);
2705cdhash_each(VALUE key, VALUE value, VALUE ary)
2714 {(void (*)(
void *))
rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
2715 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2718#define DECL_ID(name) \
2721#define INIT_ID(name) \
2722 id_##name = rb_intern(#name)
2725iseq_type_id(
enum iseq_type
type)
2750 case ISEQ_TYPE_TOP:
return id_top;
2751 case ISEQ_TYPE_METHOD:
return id_method;
2752 case ISEQ_TYPE_BLOCK:
return id_block;
2753 case ISEQ_TYPE_CLASS:
return id_class;
2754 case ISEQ_TYPE_RESCUE:
return id_rescue;
2755 case ISEQ_TYPE_ENSURE:
return id_ensure;
2756 case ISEQ_TYPE_EVAL:
return id_eval;
2757 case ISEQ_TYPE_MAIN:
return id_main;
2758 case ISEQ_TYPE_PLAIN:
return id_plain;
2761 rb_bug(
"unsupported iseq type: %d", (
int)
type);
2773 VALUE *seq, *iseq_original;
2784 static ID insn_syms[VM_INSTRUCTION_SIZE/2];
2785 struct st_table *labels_table = st_init_numtable();
2788 if (insn_syms[0] == 0) {
2790 for (i=0; i<numberof(insn_syms); i++) {
2796 type = iseq_type_id(iseq_body->type);
2799 for (i=0; i<iseq_body->local_table_size; i++) {
2800 ID lid = iseq_body->local_table[i];
2816 const struct rb_iseq_param_keyword *
const keyword = iseq_body->param.keyword;
2819 if (iseq_body->param.flags.has_opt) {
2820 int len = iseq_body->param.opt_num + 1;
2823 for (j = 0; j < len; j++) {
2824 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
2836 if (iseq_body->param.flags.has_kw) {
2839 for (i=0; i<keyword->required_num; i++) {
2842 for (j=0; i<keyword->num; i++, j++) {
2844 if (keyword->default_values[j] !=
Qundef) {
2851 INT2FIX(keyword->bits_start));
2859 iseq_original = rb_iseq_original_iseq((
rb_iseq_t *)iseq);
2861 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
2862 VALUE insn = *seq++;
2863 int j, len = insn_len(insn);
2864 VALUE *nseq = seq + len - 1;
2868 for (j=0; j<len-1; j++, seq++) {
2869 switch (insn_op_type(insn, j)) {
2871 unsigned long idx = nseq - iseq_original + *seq;
2872 rb_ary_push(ary, register_label(labels_table, idx));
2886 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
2907 int argc = vm_ci_argc(ci);
2909 ID mid = vm_ci_mid(ci);
2913 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2918 argc -= kwarg->keyword_len;
2919 for (i = 0; i < kwarg->keyword_len; i++) {
2943 unsigned long idx = nseq - iseq_original + pos;
2946 register_label(labels_table, idx));
2953#if SIZEOF_VALUE <= SIZEOF_LONG
2964#if SIZEOF_VALUE <= SIZEOF_LONG
2977 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
2986 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
2989 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
2990 rb_ary_push(ary, exception_type2symbol(entry->type));
2992 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
2997 rb_ary_push(ary, register_label(labels_table, entry->start));
2998 rb_ary_push(ary, register_label(labels_table, entry->end));
2999 rb_ary_push(ary, register_label(labels_table, entry->cont));
3006 prev_insn_info = NULL;
3007#ifdef USE_ISEQ_NODE_ID
3016 if (st_lookup(labels_table, pos, &label)) {
3020 info = get_insn_info(iseq, pos);
3021#ifdef USE_ISEQ_NODE_ID
3025 if (prev_insn_info != info) {
3026 int line = info->line_no;
3027 rb_event_flag_t events = info->events;
3029 if (line > 0 && last_line != line) {
3033#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3042 prev_insn_info = info;
3057 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3058 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3059 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3060 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3061#ifdef USE_ISEQ_NODE_ID
3078 rb_ary_push(val, iseq_body->location.first_lineno);
3088rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3092 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
3094 ID req, opt, rest, block, key, keyrest;
3095#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3096#define PARAM_ID(i) body->local_table[(i)]
3097#define PARAM(i, type) ( \
3099 rb_id2str(PARAM_ID(i)) ? \
3100 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3106 for (i = 0; i < body->param.lead_num; i++) {
3113 for (i = 0; i < body->param.lead_num; i++) {
3117 r = body->param.lead_num + body->param.opt_num;
3118 for (; i < r; i++) {
3125 if (body->param.flags.has_rest) {
3127 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3129 r = body->param.post_start + body->param.post_num;
3131 for (i = body->param.post_start; i < r; i++) {
3138 for (i = body->param.post_start; i < r; i++) {
3142 if (body->param.flags.accepts_no_kwarg) {
3148 if (body->param.flags.has_kw) {
3150 if (keyword->required_num > 0) {
3153 for (; i < keyword->required_num; i++) {
3162 for (; i < keyword->num; i++) {
3170 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3173 PARAM_TYPE(keyrest);
3174 if (body->param.flags.has_kwrest &&
3175 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3178 else if (body->param.flags.ruby2_keywords) {
3183 if (body->param.flags.has_block) {
3185 rb_ary_push(args, PARAM(body->param.block_start, block));
3191rb_iseq_defined_string(
enum defined_type
type)
3193 static const char expr_names[][18] = {
3195 "instance-variable",
3211 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names))
rb_bug(
"unknown defined type %d",
type);
3212 estr = expr_names[
type - 1];
3213 return rb_fstring_cstr(estr);
3223 void *notrace_encoded_insn;
3224 void *trace_encoded_insn;
3226static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3229rb_vm_encoded_insn_data_table_init(
void)
3231#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3232 const void *
const *table = rb_vm_get_insns_address_table();
3233#define INSN_CODE(insn) ((VALUE)table[insn])
3235#define INSN_CODE(insn) (insn)
3238 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3240 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3241 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3242 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3244 insn_data[insn].insn = (int)insn;
3245 insn_data[insn].insn_len = insn_len(insn);
3247 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3248 insn_data[insn].notrace_encoded_insn = (
void *) key1;
3249 insn_data[insn].trace_encoded_insn = (
void *) key2;
3252 insn_data[insn].notrace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3253 insn_data[insn].trace_encoded_insn = (
void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3256 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3257 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3262rb_vm_insn_addr2insn(
const void *addr)
3264 st_data_t key = (st_data_t)addr;
3267 if (st_lookup(encoded_insn_data, key, &val)) {
3269 return (
int)e->insn;
3272 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3277rb_vm_insn_addr2opcode(
const void *addr)
3279 st_data_t key = (st_data_t)addr;
3282 if (st_lookup(encoded_insn_data, key, &val)) {
3284 int opcode = e->insn;
3285 if (addr == e->trace_encoded_insn) {
3286 opcode += VM_INSTRUCTION_SIZE/2;
3291 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3296rb_vm_insn_decode(
const VALUE encoded)
3298#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3299 int insn = rb_vm_insn_addr2insn((
void *)encoded);
3301 int insn = (int)encoded;
3307encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon,
bool remain_current_trace)
3309 st_data_t key = (st_data_t)*iseq_encoded_insn;
3312 if (st_lookup(encoded_insn_data, key, &val)) {
3314 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3317 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3321 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
3325rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
3328 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3329 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
3336static inline rb_event_flag_t
3337add_bmethod_events(rb_event_flag_t events)
3350iseq_add_local_tracepoint(
const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval,
unsigned int target_line)
3355 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3357 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3359 for (pc=0; pc<body->iseq_size;) {
3361 rb_event_flag_t pc_events = entry->events;
3362 rb_event_flag_t target_events = turnon_events;
3363 unsigned int line = (int)entry->line_no;
3365 if (target_line == 0 || target_line == line) {
3369 target_events &= ~RUBY_EVENT_LINE;
3372 if (pc_events & target_events) {
3375 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
3379 if (iseq->aux.exec.local_hooks == NULL) {
3381 iseq->aux.exec.local_hooks->is_local =
true;
3383 rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3390 rb_event_flag_t turnon_events;
3392 unsigned int target_line;
3397iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3400 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3401 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3405rb_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)
3408 if (target_bmethod) {
3409 turnon_events = add_bmethod_events(turnon_events);
3411 data.turnon_events = turnon_events;
3413 data.target_line = target_line;
3416 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
3422iseq_remove_local_tracepoint(
const rb_iseq_t *iseq, VALUE tpval)
3426 if (iseq->aux.exec.local_hooks) {
3429 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3430 rb_event_flag_t local_events = 0;
3432 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3433 local_events = iseq->aux.exec.local_hooks->events;
3435 if (local_events == 0) {
3436 rb_hook_list_free(iseq->aux.exec.local_hooks);
3437 ((
rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3440 local_events = add_bmethod_events(local_events);
3441 for (pc = 0; pc<body->iseq_size;) {
3442 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3443 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
3455iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
3458 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3459 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3463rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq, VALUE tpval)
3469 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
3474rb_iseq_trace_set(
const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
3476 if (iseq->aux.exec.global_trace_events == turnon_events) {
3480 if (!ISEQ_EXECUTABLE_P(iseq)) {
3487 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3488 rb_event_flag_t enabled_events;
3489 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3490 ((
rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3491 enabled_events = add_bmethod_events(turnon_events | local_events);
3493 for (pc=0; pc<body->iseq_size;) {
3494 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3495 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
3500bool rb_vm_call_ivar_attrset_p(
const vm_call_handler ch);
3504clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3506 VALUE v = (
VALUE)vstart;
3507 for (; v != (
VALUE)vend; v += stride) {
3508 void *ptr = asan_poisoned_object_p(v);
3509 asan_unpoison_object(v,
false);
3511 if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((
const struct rb_callcache *)v)->call_)) {
3515 asan_poison_object_if(ptr, v);
3521rb_clear_attr_ccs(
void)
3523 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3527trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
3529 rb_event_flag_t turnon_events = *(rb_event_flag_t *)data;
3531 VALUE v = (
VALUE)vstart;
3532 for (; v != (
VALUE)vend; v += stride) {
3533 void *ptr = asan_poisoned_object_p(v);
3534 asan_unpoison_object(v,
false);
3536 if (rb_obj_is_iseq(v)) {
3537 rb_iseq_trace_set(rb_iseq_check((
rb_iseq_t *)v), turnon_events);
3539 else if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((
const struct rb_callcache *)v)->call_)) {
3543 asan_poison_object_if(ptr, v);
3549rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
3551 rb_objspace_each_objects(trace_set_i, &turnon_events);
3555rb_iseqw_local_variables(VALUE iseqval)
3557 return rb_iseq_local_variables(iseqw_check(iseqval));
3578iseqw_to_binary(
int argc, VALUE *argv, VALUE
self)
3581 return rb_iseq_ibf_dump(iseqw_check(
self), opt);
3598iseqw_s_load_from_binary(VALUE
self, VALUE str)
3600 return iseqw_new(rb_iseq_ibf_load(str));
3610iseqw_s_load_from_binary_extra_data(VALUE
self, VALUE str)
3612 return rb_iseq_ibf_load_extra_data(str);
3615#if VM_INSN_INFO_TABLE_IMPL == 2
3648#define IMMEDIATE_TABLE_SIZE 54
3651 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
3654 uint64_t small_block_ranks;
3655 uint64_t bits[512/64];
3656 } succ_part[FLEX_ARY_LEN];
3659#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
3660#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
3661#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
3662#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
3665succ_index_table_create(
int max_pos,
int *data,
int size)
3667 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3668 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3670 rb_xcalloc_mul_add_mul(
3671 imm_size,
sizeof(uint64_t),
3672 succ_size,
sizeof(
struct succ_dict_block));
3676 for (j = 0; j < imm_size; j++) {
3677 for (i = 0; i < 9; i++) {
3678 if (r < size && data[r] == j * 9 + i) r++;
3679 imm_block_rank_set(sd->imm_part[j], i, r);
3682 for (k = 0; k < succ_size; k++) {
3683 struct succ_dict_block *sd_block = &sd->succ_part[k];
3686 for (j = 0; j < 8; j++) {
3688 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
3689 for (i = 0; i < 64; i++) {
3690 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
3691 bits |= ((uint64_t)1) << i;
3695 sd_block->bits[j] = bits;
3696 small_rank += rb_popcount64(bits);
3702static unsigned int *
3705 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3706 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3707 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
3708 int i, j, k, r = -1;
3710 for (j = 0; j < imm_size; j++) {
3711 for (i = 0; i < 9; i++) {
3712 int nr = imm_block_rank_get(sd->imm_part[j], i);
3713 if (r != nr) *p++ = j * 9 + i;
3717 for (k = 0; k < succ_size; k++) {
3718 for (j = 0; j < 8; j++) {
3719 for (i = 0; i < 64; i++) {
3720 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
3721 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
3732 if (x < IMMEDIATE_TABLE_SIZE) {
3733 const int i = x / 9;
3734 const int j = x % 9;
3735 return imm_block_rank_get(sd->imm_part[i], j);
3738 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
3739 const struct succ_dict_block *block = &sd->succ_part[block_index];
3740 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
3741 const int small_block_index = block_bit_index / 64;
3742 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
3743 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
3745 return block->rank + small_block_popcount + popcnt;
3764iseqw_script_lines(VALUE
self)
3766 const rb_iseq_t *iseq = iseqw_check(
self);
3767 return iseq->body->variable.script_lines;
#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.
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
#define RB_OBJ_FREEZE
Just another name of rb_obj_freeze_inline.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_FILE
Old name of RUBY_T_FILE.
#define T_STRING
Old name of RUBY_T_STRING.
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
#define ID2SYM
Old name of RB_ID2SYM.
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define 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 FL_TEST
Old name of RB_FL_TEST.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_ary_new2
Old name of rb_ary_new_capa.
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
void rb_bug(const char *fmt,...)
Interpreter panic switch.
#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.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
Defines RBIMPL_HAS_BUILTIN.
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
VALUE rb_ary_resurrect(VALUE ary)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_tmp_new(long capa)
Allocates a "temporary" array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_ary_new_from_args(long n,...)
Constructs an array from the passed objects.
VALUE rb_ary_entry(VALUE ary, long off)
Queries an element of an array.
VALUE rb_ary_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_gc_mark(VALUE obj)
Marks an object.
void rb_gc_mark_movable(VALUE obj)
Maybe this is the only function provided for C extensions to control the pinning of objects,...
void rb_mark_tbl(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only values of the table and leave their associated keys...
VALUE rb_gc_location(VALUE obj)
Finds a new "location" of an object.
void rb_mark_set(struct st_table *tbl)
Identical to rb_mark_hash(), except it marks only keys of the table and leave their associated values...
void rb_hash_foreach(VALUE hash, int(*func)(VALUE key, VALUE val, VALUE arg), VALUE arg)
Iterates over a hash.
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Identical to rb_hash_aref(), except it always returns RUBY_Qnil for misshits.
VALUE rb_hash_new(void)
Creates a new, empty hash object.
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
VALUE rb_io_close(VALUE io)
Closes the IO.
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_resurrect(VALUE str)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_cat_cstr(VALUE dst, const char *src)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
VALUE rb_id2str(ID id)
Identical to rb_id2name(), except it returns a Ruby's String instead of C's.
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
VALUE rb_yield(VALUE val)
Yields the block.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define RB_ZALLOC(type)
Shorthand of RB_ZALLOC_N with n=1.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
#define RARRAY_LEN
Just another name of rb_array_len.
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
#define RARRAY_AREF(a, i)
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
#define DATA_PTR(obj)
Convenient getter macro.
#define RHASH_SIZE(h)
Queries the size of the hash.
#define StringValue(v)
Ensures that the parameter object is a String.
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define RTEST
This is an old name of RB_TEST.
This is the struct that holds necessary info for a struct.
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.
void ruby_xfree(void *ptr)
Deallocates a storage instance.