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/coverage.h"
29#include "internal/error.h"
30#include "internal/file.h"
31#include "internal/gc.h"
32#include "internal/hash.h"
33#include "internal/io.h"
34#include "internal/ruby_parser.h"
35#include "internal/sanitizers.h"
36#include "internal/set_table.h"
37#include "internal/symbol.h"
38#include "internal/thread.h"
39#include "internal/variable.h"
41#include "prism_compile.h"
45#include "ractor_core.h"
46#include "vm_callinfo.h"
51#include "insns_info.inc"
55static VALUE iseqw_new(
const rb_iseq_t *iseq);
56static const rb_iseq_t *iseqw_check(
VALUE iseqw);
58#if VM_INSN_INFO_TABLE_IMPL == 2
59static struct succ_index_table *succ_index_table_create(
int max_pos,
int *data,
int size);
60static unsigned int *succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size);
61static int succ_index_lookup(
const struct succ_index_table *sd,
int x);
64#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
67obj_resurrect(
VALUE obj)
69 if (hidden_obj_p(obj)) {
78 obj = rb_hash_resurrect(obj);
103 free_arena(compile_data->node.storage_head);
104 free_arena(compile_data->insn.storage_head);
105 if (compile_data->ivar_cache_table) {
106 rb_id_table_free(compile_data->ivar_cache_table);
108 SIZED_FREE(compile_data);
113remove_from_constant_cache(
ID id, IC ic)
115 rb_vm_t *vm = GET_VM();
117 st_data_t ic_data = (st_data_t)ic;
119 if (rb_id_table_lookup(&vm->constant_cache,
id, &lookup_result)) {
121 set_table_delete(ics, &ic_data);
123 if (ics->num_entries == 0 &&
125 id != vm->inserting_constant_cache_id) {
126 rb_id_table_delete(&vm->constant_cache,
id);
136iseq_clear_ic_references(
const rb_iseq_t *iseq)
142 if (!ISEQ_BODY(iseq)->is_entries) {
146 for (
unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
147 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
155 if (segments == NULL)
159 for (i = 0; segments[i]; i++) {
161 if (
id == idNULL)
continue;
162 remove_from_constant_cache(
id, ic);
165 SIZED_FREE_N(segments, i + 1);
171rb_iseq_local_hooks(
const rb_iseq_t *iseq, rb_ractor_t *r,
bool create)
173 rb_hook_list_t *hook_list = NULL;
175 if (st_lookup(rb_ractor_targeted_hooks(r), (st_data_t)iseq, &val)) {
176 hook_list = (rb_hook_list_t*)val;
177 RUBY_ASSERT(hook_list->type == hook_list_type_targeted_iseq);
181 hook_list->type = hook_list_type_targeted_iseq;
182 st_insert(rb_ractor_targeted_hooks(r), (st_data_t)iseq, (st_data_t)hook_list);
188rb_iseq_free(
const rb_iseq_t *iseq)
190 RUBY_FREE_ENTER(
"iseq");
192 if (iseq && ISEQ_BODY(iseq)) {
193 iseq_clear_ic_references(iseq);
196 if (rb_yjit_enabled_p) rb_yjit_iseq_free(iseq);
199 rb_yjit_live_iseq_count--;
203 if (rb_zjit_enabled_p) rb_zjit_iseq_free(iseq);
205 SIZED_FREE_N(body->iseq_encoded, body->iseq_size);
206 SIZED_FREE_N(body->insns_info.body, body->insns_info.size);
207#ifdef USE_ISEQ_NODE_ID
208 SIZED_FREE_N(body->insns_info.node_ids, body->insns_info.node_ids_size);
210#if VM_INSN_INFO_TABLE_IMPL == 2
211 ruby_xfree(body->insns_info.positions_or_succ_index_table.succ_index_table);
213 SIZED_FREE_N(body->insns_info.positions_or_succ_index_table.positions, body->insns_info.size);
215 SIZED_FREE_N(body->is_entries, ISEQ_IS_SIZE(body));
216 SIZED_FREE_N(body->call_data, body->ci_size);
217 if (body->catch_table) {
218 ruby_xfree_sized(body->catch_table, iseq_catch_table_bytes(body->catch_table->size));
220 SIZED_FREE_N(body->param.opt_table, body->param.opt_num + 1);
221 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
222 SIZED_FREE_N(body->mark_bits.list, ISEQ_MBITS_BUFLEN(body->iseq_size));
225 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
226 if (body->variable)
xfree(body->variable);
228 struct rb_iseq_param_keyword *pkw = (
struct rb_iseq_param_keyword *)body->param.keyword;
230 if (pkw->table != &body->local_table[pkw->bits_start - pkw->num])
231 SIZED_FREE_N(pkw->table, pkw->required_num);
232 if (pkw->default_values) {
233 SIZED_FREE_N(pkw->default_values, pkw->num - pkw->required_num);
237 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl)) {
238 SIZED_FREE_N(body->local_table, body->local_table_size);
240 if (!ISEQ_LVAR_STATES_EMBED_P(body->local_table_size)) {
241 SIZED_FREE_N(body->lvar_states.list, ISEQ_LVAR_STATES_BUFLEN(body->local_table_size));
244 compile_data_free(ISEQ_COMPILE_DATA(iseq));
245 if (body->outer_variables) rb_id_table_free(body->outer_variables);
249 RUBY_FREE_LEAVE(
"iseq");
256rb_iseq_variable_ensure(rb_iseq_t *iseq)
262 v->script_lines =
Qnil;
264 v->pc2branchindex =
Qfalse;
265 v->original_iseq = NULL;
266 struct rb_iseq_variable *prev = ATOMIC_PTR_CAS(ISEQ_BODY(iseq)->variable, NULL, v);
275rb_iseq_coverage_set(rb_iseq_t *iseq,
VALUE cov)
277 if (!
RTEST(cov) && !ISEQ_VARIABLE(iseq))
return;
283rb_iseq_pc2branchindex_set(rb_iseq_t *iseq,
VALUE h)
285 if (!
RTEST(h) && !ISEQ_VARIABLE(iseq))
return;
291rb_iseq_flip_cnt_increment(
const rb_iseq_t *iseq)
294 rb_snum_t cnt = v->flip_count;
299typedef VALUE iseq_value_itr_t(
void *ctx,
VALUE obj);
302iseq_scan_bits(
const struct rb_gc_mark_ctx *ctx,
unsigned int page, iseq_bits_t bits,
VALUE *code,
VALUE *original_iseq)
305 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
308 offset = ntz_intptr(bits);
310 VALUE op = original_iseq[page_offset + offset];
311 rb_gc_mark_and_move_ctx(ctx, &code[page_offset + offset]);
312 VALUE newop = code[page_offset + offset];
314 original_iseq[page_offset + offset] = newop;
318 rb_gc_mark_and_move_ctx(ctx, &code[page_offset + offset]);
325rb_iseq_mark_and_move_each_compile_data_value(
const struct rb_gc_mark_ctx *ctx,
const rb_iseq_t *iseq,
VALUE *original_iseq)
331 size = compile_data->iseq_size;
332 code = compile_data->iseq_encoded;
335 if (compile_data->mark_bits.list) {
336 if(compile_data->is_single_mark_bit) {
337 iseq_scan_bits(ctx, 0, compile_data->mark_bits.single, code, original_iseq);
340 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
341 iseq_bits_t bits = compile_data->mark_bits.list[i];
342 iseq_scan_bits(ctx, i, bits, code, original_iseq);
348rb_iseq_mark_and_move_each_body_value(
const struct rb_gc_mark_ctx *ctx,
const rb_iseq_t *iseq,
VALUE *original_iseq)
354 size = body->iseq_size;
355 code = body->iseq_encoded;
359 if (body->is_entries) {
361 is_entries += body->ivc_size;
364 for (
unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
365 ICVARC icvarc = (ICVARC)is_entries;
367 rb_gc_mark_and_move_ctx(ctx, (
VALUE *)&icvarc->entry);
372 for (
unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
374 if (is->once.value) {
375 rb_gc_mark_and_move_ctx(ctx, &is->once.value);
380 for (
unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
381 IC ic = (IC)is_entries;
383 rb_gc_mark_and_move_ptr_ctx(ctx, &ic->entry);
389 if (body->mark_bits.list) {
390 if (ISEQ_MBITS_BUFLEN(size) == 1) {
391 iseq_scan_bits(ctx, 0, body->mark_bits.single, code, original_iseq);
394 for (
unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
395 iseq_bits_t bits = body->mark_bits.list[i];
396 iseq_scan_bits(ctx, i, bits, code, original_iseq);
408 return vm_cc_valid(cc) && !METHOD_ENTRY_INVALIDATED(vm_cc_cme(cc));
412rb_iseq_mark_and_move(
const struct rb_gc_mark_ctx *ctx, rb_iseq_t *iseq,
bool reference_updating)
414 RUBY_MARK_ENTER(
"iseq");
416 if (ISEQ_BODY(iseq)) {
419 rb_iseq_mark_and_move_each_body_value(ctx, iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
422 if (v) rb_gc_mark_and_move_ctx(ctx, &v->script_lines);
423 rb_gc_mark_and_move_ctx(ctx, &body->location.label);
424 rb_gc_mark_and_move_ctx(ctx, &body->location.pathobj);
425 if (body->local_iseq) rb_gc_mark_and_move_ptr_ctx(ctx, &body->local_iseq);
426 if (body->parent_iseq) rb_gc_mark_and_move_ptr_ctx(ctx, &body->parent_iseq);
427 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr_ctx(ctx, &body->mandatory_only_iseq);
429 if (body->call_data) {
431 for (
unsigned int i = 0; i < body->ci_size; i++) {
433 if (cds[i].ci) rb_gc_mark_and_move_ptr_ctx(ctx, &cds[i].ci);
436 if (!cc || cc == &rb_vm_empty_cc || cc == &rb_vm_empty_cc_for_super ||
437 cc == &rb_vm_uncached_cc) {
443 if (reference_updating) {
444 rb_gc_update_moved_ptr_ctx(ctx, &cds[i].cc);
447 if (cc_is_active(cc)) {
448 rb_gc_mark_movable_ctx(ctx, (
VALUE)cc);
453 cds[i].cc = &rb_vm_empty_cc;
459 if (body->param.flags.has_kw && body->param.keyword != NULL) {
460 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
462 if (keyword->default_values != NULL) {
463 for (
int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
464 rb_gc_mark_and_move_ctx(ctx, &keyword->default_values[j]);
469 if (body->catch_table) {
472 for (
unsigned int i = 0; i < table->size; i++) {
474 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
476 rb_gc_mark_and_move_ptr_ctx(ctx, &entry->iseq);
481#if USE_YJIT || USE_ZJIT
485 const bool jit_payload_lock_p = rb_gc_multi_objspace_p();
486 bool jit_payload_p = body->jit_payload != NULL;
488 if (reference_updating) {
489#if USE_YJIT || USE_ZJIT
491 if (jit_payload_lock_p) {
492 RB_VM_LOCKING_NO_BARRIER() {
494 if (rb_yjit_enabled_p) rb_yjit_iseq_update_references(iseq);
497 if (rb_zjit_enabled_p) rb_zjit_iseq_update_references(body->jit_payload);
503 if (rb_yjit_enabled_p) rb_yjit_iseq_update_references(iseq);
506 if (rb_zjit_enabled_p) rb_zjit_iseq_update_references(body->jit_payload);
514 if (!rb_gc_checking_shareable_ctx(ctx)) {
515#if USE_YJIT || USE_ZJIT
517 if (jit_payload_lock_p) {
518 RB_VM_LOCKING_NO_BARRIER() {
520 if (rb_yjit_enabled_p) rb_yjit_iseq_mark(body->jit_payload);
523 if (rb_zjit_enabled_p) rb_zjit_iseq_mark(body->jit_payload);
529 if (rb_yjit_enabled_p) rb_yjit_iseq_mark(body->jit_payload);
532 if (rb_zjit_enabled_p) rb_zjit_iseq_mark(body->jit_payload);
541 if (!rb_gc_checking_shareable_ctx(ctx)) {
543 rb_gc_mark_and_move_ctx(ctx, &v->coverage);
544 rb_gc_mark_and_move_ctx(ctx, &v->pc2branchindex);
550 if (!rb_gc_checking_shareable_ctx(ctx)) {
551 rb_gc_mark_and_move_ctx(ctx, &iseq->aux.loader.obj);
555 if (!rb_gc_checking_shareable_ctx(ctx)) {
558 rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head);
559 rb_iseq_mark_and_move_each_compile_data_value(ctx, iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
561 rb_gc_mark_and_move_ctx(ctx, (
VALUE *)&compile_data->err_info);
562 rb_gc_mark_and_move_ctx(ctx, (
VALUE *)&compile_data->catch_table_ary);
567 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
570 RUBY_MARK_LEAVE(
"iseq");
574param_keyword_size(
const struct rb_iseq_param_keyword *pkw)
578 if (!pkw)
return size;
580 size +=
sizeof(
struct rb_iseq_param_keyword);
581 size +=
sizeof(
VALUE) * (pkw->num - pkw->required_num);
587rb_iseq_memsize(
const rb_iseq_t *iseq)
595 if (ISEQ_EXECUTABLE_P(iseq) && body) {
598 size += body->iseq_size *
sizeof(
VALUE);
600#ifdef USE_ISEQ_NODE_ID
601 size += body->insns_info.node_ids_size;
603 size += body->local_table_size *
sizeof(
ID);
604 if (!ISEQ_LVAR_STATES_EMBED_P(body->local_table_size) && body->lvar_states.list) {
605 size += ISEQ_LVAR_STATES_BUFLEN(body->local_table_size) * sizeof(uint8_t);
607 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
608 if (body->catch_table) {
609 size += iseq_catch_table_bytes(body->catch_table->size);
611 size += (body->param.opt_num + 1) *
sizeof(
VALUE);
612 size += param_keyword_size(body->param.keyword);
614 if (body->outer_variables) size += rb_id_table_memsize(body->outer_variables);
619 if (ISEQ_BODY(iseq)->is_entries) {
621 for (
unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
622 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
637 compile_data = ISEQ_COMPILE_DATA(iseq);
643 cur = compile_data->node.storage_head;
654rb_iseq_constant_body_alloc(
void)
664 rb_iseq_t *iseq = iseq_imemo_alloc();
665 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
670rb_iseq_pathobj_new(
VALUE path,
VALUE realpath,
bool *no_realpath)
676 *no_realpath =
NIL_P(realpath);
678 if (*no_realpath || path == realpath ||
rb_str_cmp(path, realpath) == 0) {
679 pathobj = rb_fstring(path);
682 realpath = rb_fstring(realpath);
683 VALUE fpath = rb_fstring(path);
685 pathobj = rb_ary_new_from_args(2, fpath, realpath);
693rb_iseq_pathobj_set(
const rb_iseq_t *iseq,
VALUE path,
VALUE realpath)
696 VALUE pathobj = rb_iseq_pathobj_new(path, realpath, &no_realpath);
697 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->location.pathobj, pathobj);
698 ISEQ_BODY(iseq)->no_realpath = no_realpath;
703rb_iseq_alloc_with_dummy_path(
VALUE fname)
705 rb_iseq_t *dummy_iseq = iseq_alloc();
707 ISEQ_BODY(dummy_iseq)->type = ISEQ_TYPE_TOP;
713 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.pathobj, fname);
714 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.label, fname);
719static rb_iseq_location_t *
720iseq_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)
722 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
724 rb_iseq_pathobj_set(iseq, path, realpath);
726 loc->first_lineno = first_lineno;
728 if (ISEQ_BODY(iseq)->local_iseq == iseq && rb_streql_lit(name,
"initialize")) {
729 ISEQ_BODY(iseq)->param.flags.use_block = 1;
733 loc->node_id = node_id;
734 loc->code_location = *code_location;
737 loc->code_location.beg_pos.lineno = 0;
738 loc->code_location.beg_pos.column = 0;
739 loc->code_location.end_pos.lineno = -1;
740 loc->code_location.end_pos.column = -1;
747set_relation(rb_iseq_t *iseq,
const rb_iseq_t *piseq)
753 if (
type == ISEQ_TYPE_TOP) {
754 body->local_iseq = iseq;
756 else if (
type == ISEQ_TYPE_METHOD ||
type == ISEQ_TYPE_CLASS) {
757 body->local_iseq = iseq;
760 RB_OBJ_WRITE(iseq, &body->local_iseq, ISEQ_BODY(piseq)->local_iseq);
767 if (
type == ISEQ_TYPE_MAIN) {
768 body->local_iseq = iseq;
777 ALLOC_N(
char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
782 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
788prepare_node_id(
const NODE *node)
790 if (!node)
return -1;
792 if (nd_type(node) == NODE_SCOPE && RNODE_SCOPE(node)->nd_parent) {
793 return nd_node_id(RNODE_SCOPE(node)->nd_parent);
796 return nd_node_id(node);
800prepare_iseq_build(rb_iseq_t *iseq,
801 VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_code_location_t *code_location,
const int node_id,
802 const rb_iseq_t *parent,
int isolated_depth,
enum rb_iseq_type
type,
803 VALUE script_lines,
const rb_compile_option_t *option)
809 if (parent && (
type == ISEQ_TYPE_MAIN ||
type == ISEQ_TYPE_TOP))
813 set_relation(iseq, parent);
815 name = rb_fstring(name);
816 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
817 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
818 if (body->variable) {
819 body->variable->flip_count = 0;
824 if (!
NIL_P(script_lines)) {
829 ISEQ_COMPILE_DATA_ALLOC(iseq);
830 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
833 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
834 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
835 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
836 ISEQ_COMPILE_DATA(iseq)->option = option;
837 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
838 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
840 if (option->coverage_enabled) {
841 VALUE coverages = rb_get_coverages();
842 if (
RTEST(coverages)) {
843 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
847 ISEQ_COVERAGE_SET(iseq, coverage);
848 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
854#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
855static void validate_get_insn_info(
const rb_iseq_t *iseq);
859rb_iseq_insns_info_encode_positions(
const rb_iseq_t *iseq)
861#if VM_INSN_INFO_TABLE_IMPL == 2
864 int size = body->insns_info.size;
865 int max_pos = body->iseq_size;
866 unsigned int *positions = body->insns_info.positions_or_succ_index_table.positions;
867 struct succ_index_table *sd = succ_index_table_create(max_pos, (
int *)positions, size);
868 SIZED_FREE_N(positions, size);
869 body->insns_info.positions_or_succ_index_table.succ_index_table = sd;
873#if VM_INSN_INFO_TABLE_IMPL == 2
877 int size = body->insns_info.size;
878 int max_pos = body->iseq_size;
879 struct succ_index_table *sd = body->insns_info.positions_or_succ_index_table.succ_index_table;
880 return succ_index_table_invert(max_pos, sd, size);
885rb_iseq_init_trace(rb_iseq_t *iseq)
887 iseq->aux.exec.global_trace_events = 0;
888 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
889 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
894finish_iseq_build(rb_iseq_t *iseq)
898 VALUE err = data->err_info;
899 ISEQ_COMPILE_DATA_CLEAR(iseq);
900 compile_data_free(data);
902#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
903 validate_get_insn_info(iseq);
907 VALUE path = pathobj_path(body->location.pathobj);
909 rb_funcallv(err, rb_intern(
"set_backtrace"), 1, &path);
913 RB_DEBUG_COUNTER_INC(iseq_num);
914 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
916 rb_iseq_init_trace(iseq);
920static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
921 .inline_const_cache = OPT_INLINE_CONST_CACHE,
922 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
923 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
924 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
925 .operands_unification = OPT_OPERANDS_UNIFICATION,
926 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
927 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
928 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
929 .coverage_enabled = TRUE,
932static const rb_compile_option_t COMPILE_OPTION_FALSE = {
933 .frozen_string_literal = -1,
937rb_iseq_opt_frozen_string_literal(
void)
939 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
943set_compile_option_from_hash(rb_compile_option_t *option,
VALUE opt)
945#define SET_COMPILE_OPTION(o, h, mem) \
946 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
947 if (flag == Qtrue) { (o)->mem = 1; } \
948 else if (flag == Qfalse) { (o)->mem = 0; } \
950#define SET_COMPILE_OPTION_NUM(o, h, mem) \
951 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
952 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
954 SET_COMPILE_OPTION(option, opt, inline_const_cache);
955 SET_COMPILE_OPTION(option, opt, peephole_optimization);
956 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
957 SET_COMPILE_OPTION(option, opt, specialized_instruction);
958 SET_COMPILE_OPTION(option, opt, operands_unification);
959 SET_COMPILE_OPTION(option, opt, instructions_unification);
960 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
961 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
962 SET_COMPILE_OPTION(option, opt, coverage_enabled);
963 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
964#undef SET_COMPILE_OPTION
965#undef SET_COMPILE_OPTION_NUM
968static rb_compile_option_t *
969set_compile_option_from_ast(rb_compile_option_t *option,
const rb_ast_body_t *ast)
971 if (ast->frozen_string_literal >= 0) {
972 option->frozen_string_literal = ast->frozen_string_literal;
978make_compile_option(rb_compile_option_t *option,
VALUE opt)
981 *option = COMPILE_OPTION_DEFAULT;
984 *option = COMPILE_OPTION_FALSE;
986 else if (opt ==
Qtrue) {
988 for (i = 0; i < (int)(
sizeof(rb_compile_option_t) /
sizeof(int)); ++i)
989 ((
int *)option)[i] = 1;
992 *option = COMPILE_OPTION_DEFAULT;
993 set_compile_option_from_hash(option, opt);
996 rb_raise(
rb_eTypeError,
"Compile option must be Hash/true/false/nil");
1001make_compile_option_value(rb_compile_option_t *option)
1003 VALUE opt = rb_hash_new_capa(11);
1004#define SET_COMPILE_OPTION(o, h, mem) \
1005 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
1006#define SET_COMPILE_OPTION_NUM(o, h, mem) \
1007 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
1009 SET_COMPILE_OPTION(option, opt, inline_const_cache);
1010 SET_COMPILE_OPTION(option, opt, peephole_optimization);
1011 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
1012 SET_COMPILE_OPTION(option, opt, specialized_instruction);
1013 SET_COMPILE_OPTION(option, opt, operands_unification);
1014 SET_COMPILE_OPTION(option, opt, instructions_unification);
1015 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
1016 SET_COMPILE_OPTION(option, opt, coverage_enabled);
1017 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
1019#undef SET_COMPILE_OPTION
1020#undef SET_COMPILE_OPTION_NUM
1021 VALUE frozen_string_literal = option->frozen_string_literal == -1 ?
Qnil : RBOOL(option->frozen_string_literal);
1022 rb_hash_aset(opt,
ID2SYM(rb_intern(
"frozen_string_literal")), frozen_string_literal);
1028 const rb_iseq_t *parent,
enum rb_iseq_type
type)
1030 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
1031 0,
type, &COMPILE_OPTION_DEFAULT,
1036ast_line_count(
const VALUE ast_value)
1038 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1039 return ast->body.line_count;
1043iseq_setup_coverage(
VALUE coverages,
VALUE path,
int line_count)
1045 if (line_count >= 0) {
1046 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
1048 VALUE coverage = rb_default_coverage(
len);
1049 rb_hash_aset(coverages, path, coverage);
1058iseq_new_setup_coverage(
VALUE path,
int line_count)
1060 VALUE coverages = rb_get_coverages();
1062 if (
RTEST(coverages)) {
1063 iseq_setup_coverage(coverages, path, line_count);
1068rb_iseq_new_top(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent)
1070 iseq_new_setup_coverage(path, ast_line_count(ast_value));
1072 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
1073 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
1081pm_iseq_new_top(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int *error_state)
1083 iseq_new_setup_coverage(path, (
int) (pm_parser_line_offsets(node->parser)->size - 1));
1085 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
1086 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT, error_state);
1090rb_iseq_new_main(
const VALUE ast_value,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt)
1092 iseq_new_setup_coverage(path, ast_line_count(ast_value));
1094 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
1096 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
1105pm_iseq_new_main(pm_scope_node_t *node,
VALUE path,
VALUE realpath,
const rb_iseq_t *parent,
int opt,
int *error_state)
1107 iseq_new_setup_coverage(path, (
int) (pm_parser_line_offsets(node->parser)->size - 1));
1109 return pm_iseq_new_with_opt(node, rb_fstring_lit(
"<main>"),
1111 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE, error_state);
1115rb_iseq_new_eval(
const VALUE ast_value,
VALUE name,
VALUE path,
VALUE realpath,
int first_lineno,
const rb_iseq_t *parent,
int isolated_depth)
1117 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1118 VALUE coverages = rb_get_coverages();
1119 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1120 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
1124 rb_compile_option_t option = COMPILE_OPTION_DEFAULT;
1125 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1126 if (ast->body.coverage_enabled >= 0) {
1127 option.coverage_enabled = ast->body.coverage_enabled;
1129 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
1130 parent, isolated_depth, ISEQ_TYPE_EVAL, &option,
1135pm_iseq_new_eval(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1136 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
int *error_state)
1138 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
1139 VALUE coverages = rb_get_coverages();
1140 if (
RTEST(coverages) &&
RTEST(path) && !
RTEST(rb_hash_has_key(coverages, path))) {
1141 iseq_setup_coverage(coverages, path, ((
int) (pm_parser_line_offsets(node->parser)->size - 1)) + first_lineno - 1);
1145 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
1146 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT, error_state);
1149static inline rb_iseq_t *
1150iseq_translate(rb_iseq_t *iseq)
1153 VALUE v1 = iseqw_new(iseq);
1155 if (v1 != v2 &&
CLASS_OF(v2) == rb_cISeq) {
1156 iseq = (rb_iseq_t *)iseqw_check(v2);
1165 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1166 enum rb_iseq_type
type,
const rb_compile_option_t *option,
1169 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1170 rb_ast_body_t *body = ast ? &ast->body : NULL;
1171 const NODE *node = body ? body->root : 0;
1173 rb_iseq_t *iseq = iseq_alloc();
1174 rb_compile_option_t new_opt;
1176 if (!option) option = &COMPILE_OPTION_DEFAULT;
1179 option = set_compile_option_from_ast(&new_opt, body);
1182 if (!
NIL_P(script_lines)) {
1185 else if (body && body->script_lines) {
1186 script_lines = rb_parser_build_script_lines_from(body->script_lines);
1189 script_lines = ISEQ_SCRIPT_LINES(parent);
1192 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, prepare_node_id(node),
1193 parent, isolated_depth,
type, script_lines, option);
1195 if (body && body->has_source_hash) {
1196 ISEQ_BODY(iseq)->source_hash = body->source_hash;
1200 rb_iseq_compile_node(iseq, node);
1201 finish_iseq_build(iseq);
1204 return iseq_translate(iseq);
1213pm_iseq_build(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1214 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1215 enum rb_iseq_type
type,
const rb_compile_option_t *option)
1217 rb_iseq_t *iseq = iseq_alloc();
1218 ISEQ_BODY(iseq)->prism =
true;
1223 rb_compile_option_t next_option;
1224 if (!option) option = &COMPILE_OPTION_DEFAULT;
1226 next_option = *option;
1227 next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
1228 option = &next_option;
1231 int32_t start_line = pm_parser_start_line(node->parser);
1234 pm_line_column_t start = pm_line_offset_list_line_column(line_offsets, location->
start, start_line);
1237 rb_code_location_t code_location = (rb_code_location_t) {
1238 .beg_pos = { .lineno = (int) start.
line, .column = (
int) start.
column },
1239 .end_pos = { .lineno = (int) end.
line, .column = (
int) end.
column }
1242 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node->ast_node->
node_id,
1245 pm_iseq_compile_node(iseq, node);
1246 finish_iseq_build(iseq);
1248 return iseq_translate(iseq);
1253 pm_scope_node_t *node;
1254 VALUE name, path, realpath;
1255 int first_lineno, isolated_depth;
1256 const rb_iseq_t *parent;
1257 enum rb_iseq_type type;
1258 const rb_compile_option_t *option;
1262pm_iseq_new_with_opt_try(
VALUE d)
1265 data->iseq = pm_iseq_build(data->node, data->name, data->path, data->realpath,
1266 data->first_lineno, data->parent, data->isolated_depth,
1267 data->type, data->option);
1284pm_iseq_new_with_opt(pm_scope_node_t *node,
VALUE name,
VALUE path,
VALUE realpath,
1285 int first_lineno,
const rb_iseq_t *parent,
int isolated_depth,
1286 enum rb_iseq_type
type,
const rb_compile_option_t *option,
int *error_state)
1289 .node = node, .name = name, .path = path, .realpath = realpath,
1290 .first_lineno = first_lineno, .parent = parent,
1291 .isolated_depth = isolated_depth, .type =
type, .option = option
1293 rb_protect(pm_iseq_new_with_opt_try, (
VALUE)&data, error_state);
1295 if (*error_state)
return NULL;
1301rb_iseq_new_with_callback(
1304 int first_lineno,
const rb_iseq_t *parent,
1305 enum rb_iseq_type
type,
const rb_compile_option_t *option)
1308 rb_iseq_t *iseq = iseq_alloc();
1310 if (!option) option = &COMPILE_OPTION_DEFAULT;
1311 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0,
type,
Qnil, option);
1313 rb_iseq_compile_callback(iseq, ifunc);
1314 finish_iseq_build(iseq);
1320rb_iseq_load_iseq(
VALUE fname)
1325 return iseqw_check(iseqv);
1340 return iseqw_check(iseqv);
1346#define CHECK_ARRAY(v) rb_to_array_type(v)
1347#define CHECK_HASH(v) rb_to_hash_type(v)
1348#define CHECK_STRING(v) rb_str_to_str(v)
1349#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1352static enum rb_iseq_type
1355 const ID id_top = rb_intern(
"top");
1356 const ID id_method = rb_intern(
"method");
1357 const ID id_block = rb_intern(
"block");
1358 const ID id_class = rb_intern(
"class");
1359 const ID id_rescue = rb_intern(
"rescue");
1360 const ID id_ensure = rb_intern(
"ensure");
1361 const ID id_eval = rb_intern(
"eval");
1362 const ID id_main = rb_intern(
"main");
1363 const ID id_plain = rb_intern(
"plain");
1367 if (
typeid == id_top)
return ISEQ_TYPE_TOP;
1368 if (
typeid == id_method)
return ISEQ_TYPE_METHOD;
1369 if (
typeid == id_block)
return ISEQ_TYPE_BLOCK;
1370 if (
typeid == id_class)
return ISEQ_TYPE_CLASS;
1371 if (
typeid == id_rescue)
return ISEQ_TYPE_RESCUE;
1372 if (
typeid == id_ensure)
return ISEQ_TYPE_ENSURE;
1373 if (
typeid == id_eval)
return ISEQ_TYPE_EVAL;
1374 if (
typeid == id_main)
return ISEQ_TYPE_MAIN;
1375 if (
typeid == id_plain)
return ISEQ_TYPE_PLAIN;
1376 return (
enum rb_iseq_type)-1;
1380iseq_load(
VALUE data,
const rb_iseq_t *parent,
VALUE opt)
1382 rb_iseq_t *iseq = iseq_alloc();
1384 VALUE magic, version1, version2, format_type, misc;
1385 VALUE name, path, realpath, code_location, node_id;
1386 VALUE type, body, locals, params, exception;
1388 st_data_t iseq_type;
1389 rb_compile_option_t option;
1391 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1398 data = CHECK_ARRAY(data);
1405 ((void)magic, (void)version1, (void)version2, (void)format_type);
1410 realpath =
NIL_P(realpath) ?
Qnil : CHECK_STRING(realpath);
1419 ISEQ_BODY(iseq)->local_iseq = iseq;
1421 iseq_type = iseq_type_from_sym(
type);
1422 if (iseq_type == (
enum rb_iseq_type)-1) {
1426 node_id = rb_hash_aref(misc,
ID2SYM(rb_intern(
"node_id")));
1428 code_location = rb_hash_aref(misc,
ID2SYM(rb_intern(
"code_location")));
1436 if (
SYM2ID(rb_hash_aref(misc,
ID2SYM(rb_intern(
"parser")))) == rb_intern(
"prism")) {
1437 ISEQ_BODY(iseq)->prism =
true;
1440 make_compile_option(&option, opt);
1441 option.peephole_optimization = FALSE;
1442 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc,
NUM2INT(node_id),
1443 parent, 0, (
enum rb_iseq_type)iseq_type,
Qnil, &option);
1445 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1447 finish_iseq_build(iseq);
1449 return iseqw_new(iseq);
1456iseq_s_load(
int argc,
VALUE *argv,
VALUE self)
1460 return iseq_load(data, NULL, opt);
1466 return iseq_load(data,
RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1472 rb_iseq_t *iseq = NULL;
1473 rb_compile_option_t option;
1474#if !defined(__GNUC__)
1475# define INITIALIZED volatile
1481 VALUE INITIALIZED ast_value;
1483 VALUE name = rb_fstring_lit(
"<compiled>");
1486 make_compile_option(&option, opt);
1490 parse = rb_parser_compile_file_path;
1493 parse = rb_parser_compile_string_path;
1497 const VALUE parser = rb_parser_new();
1498 const rb_iseq_t *outer_scope = rb_iseq_new(
Qnil, name, name,
Qnil, 0, ISEQ_TYPE_TOP);
1500 rb_parser_set_context(parser, outer_scope, FALSE);
1501 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1503 ast_value = (*parse)(parser, file, src, ln);
1506 ast = rb_ruby_ast_data_get(ast_value);
1508 if (!ast || !ast->body.root) {
1509 rb_ast_dispose(ast);
1513 if (option.coverage_enabled) iseq_new_setup_coverage(file, ast_line_count(ast_value));
1514 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1515 NULL, 0, ISEQ_TYPE_TOP, &option,
1517 rb_ast_dispose(ast);
1526 rb_iseq_t *iseq = NULL;
1527 rb_compile_option_t option;
1529 VALUE name = rb_fstring_lit(
"<compiled>");
1532 make_compile_option(&option, opt);
1536 bool parse_file =
false;
1546 pm_parse_result_init(&result);
1548 pm_options_scopes_init(result.
options, 1);
1549 result.
node.coverage_enabled = option.coverage_enabled;
1551 switch (option.frozen_string_literal) {
1552 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1554 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1555 pm_options_frozen_string_literal_set(result.
options,
false);
1557 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1558 pm_options_frozen_string_literal_set(result.
options,
true);
1561 rb_bug(
"pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1569 error = pm_load_parse_file(&result, src, ruby_vm_keep_script_lines ? &script_lines : NULL);
1572 error = pm_parse_string(&result, src, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1577 if (error ==
Qnil) {
1579 if (option.coverage_enabled) iseq_new_setup_coverage(file, (
int) (pm_parser_line_offsets(result.
node.parser)->size - 1));
1580 iseq = pm_iseq_new_with_opt(&result.
node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1582 pm_parse_result_free(&result);
1586 rb_jump_tag(error_state);
1590 pm_parse_result_free(&result);
1598rb_iseq_path(
const rb_iseq_t *iseq)
1600 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1604rb_iseq_realpath(
const rb_iseq_t *iseq)
1606 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj, ISEQ_BODY(iseq)->no_realpath);
1610rb_iseq_absolute_path(
const rb_iseq_t *iseq)
1612 return rb_iseq_realpath(iseq);
1616rb_iseq_from_eval_p(
const rb_iseq_t *iseq)
1618 return NIL_P(rb_iseq_realpath(iseq));
1622rb_iseq_label(
const rb_iseq_t *iseq)
1624 return ISEQ_BODY(iseq)->location.label;
1628rb_iseq_base_label(
const rb_iseq_t *iseq)
1631 const rb_iseq_t *local_iseq = body->local_iseq;
1633 if (local_iseq == NULL || local_iseq == iseq) {
1634 return body->location.label;
1637 return ISEQ_BODY(rb_iseq_check(local_iseq))->location.label;
1642rb_iseq_first_lineno(
const rb_iseq_t *iseq)
1644 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1648rb_iseq_method_name(
const rb_iseq_t *iseq)
1652 if (body->type == ISEQ_TYPE_METHOD) {
1653 return body->location.label;
1661rb_iseq_code_location(
const rb_iseq_t *iseq,
int *beg_pos_lineno,
int *beg_pos_column,
int *end_pos_lineno,
int *end_pos_column)
1663 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1664 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1665 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1666 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1667 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1670static ID iseq_type_id(
enum rb_iseq_type
type);
1673rb_iseq_type(
const rb_iseq_t *iseq)
1675 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->
type));
1679rb_iseq_coverage(
const rb_iseq_t *iseq)
1681 return ISEQ_COVERAGE(iseq);
1685remove_coverage_i(
void *vstart,
void *vend,
size_t stride,
void *data)
1688 for (; v != (
VALUE)vend; v += stride) {
1689 void *ptr = rb_asan_poisoned_object_p(v);
1690 rb_asan_unpoison_object(v,
false);
1692 if (rb_obj_is_iseq(v)) {
1693 rb_iseq_t *iseq = (rb_iseq_t *)v;
1694 if (ISEQ_VARIABLE(iseq)) {
1695 ISEQ_COVERAGE_SET(iseq,
Qnil);
1699 asan_poison_object_if(ptr, v);
1705rb_iseq_remove_coverage_all(
void)
1707 rb_objspace_each_objects(remove_coverage_i, NULL);
1713iseqw_mark_and_move(
void *ptr)
1715 rb_gc_mark_and_move((
VALUE *)ptr);
1719iseqw_memsize(
const void *ptr)
1721 return rb_iseq_memsize(*(
const rb_iseq_t **)ptr);
1727 iseqw_mark_and_move,
1730 iseqw_mark_and_move,
1732 0, 0, RUBY_TYPED_THREAD_SAFE_FREE|RUBY_TYPED_WB_PROTECTED
1736iseqw_new(
const rb_iseq_t *iseq)
1746rb_iseqw_new(
const rb_iseq_t *iseq)
1748 return iseqw_new(iseq);
1757iseqw_s_compile_parser(
int argc,
VALUE *argv,
VALUE self,
bool prism)
1763 if (i > 4+
NIL_P(opt)) rb_error_arity(argc, 1, 5);
1765 case 5: opt = argv[--i];
1766 case 4: line = argv[--i];
1767 case 3: path = argv[--i];
1768 case 2: file = argv[--i];
1771 if (
NIL_P(file)) file = rb_fstring_lit(
"<compiled>");
1772 if (
NIL_P(path)) path = file;
1780 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1783 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1786 return iseqw_new(iseq);
1827iseqw_s_compile(
int argc,
VALUE *argv,
VALUE self)
1829 return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p());
1869iseqw_s_compile_parsey(
int argc,
VALUE *argv,
VALUE self)
1871 return iseqw_s_compile_parser(argc, argv, self,
false);
1911iseqw_s_compile_prism(
int argc,
VALUE *argv,
VALUE self)
1913 return iseqw_s_compile_parser(argc, argv, self,
true);
1916static VALUE iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self);
1939iseqw_s_compile_file(
int argc,
VALUE *argv,
VALUE self)
1941 if (rb_ruby_prism_p()) {
1942 return iseqw_s_compile_file_prism(argc, argv, self);
1949 rb_compile_option_t option;
1952 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
1953 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
1955 case 2: opt = argv[--i];
1958 file = rb_fstring(file);
1960 make_compile_option(&option, opt);
1964 rb_execution_context_t *ec = GET_EC();
1965 VALUE v = rb_vm_push_frame_fname(ec, file);
1967 parser = rb_parser_new();
1968 rb_parser_set_context(parser, NULL, FALSE);
1969 ast_value = rb_parser_load_file(parser, file);
1970 if (option.coverage_enabled) iseq_new_setup_coverage(file, ast_line_count(ast_value));
1971 ast = rb_ruby_ast_data_get(ast_value);
1972 if (!ast->body.root) exc = GET_EC()->errinfo;
1975 if (!ast->body.root) {
1976 rb_ast_dispose(ast);
1980 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit(
"<main>"),
1982 rb_realpath_internal(
Qnil, file, 1),
1983 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1985 rb_ast_dispose(ast);
1988 rb_vm_pop_frame(ec);
2015iseqw_s_compile_file_prism(
int argc,
VALUE *argv,
VALUE self)
2018 rb_compile_option_t option;
2021 i =
rb_scan_args(argc, argv,
"1*:", &file, NULL, &opt);
2022 if (i > 1+
NIL_P(opt)) rb_error_arity(argc, 1, 2);
2024 case 2: opt = argv[--i];
2027 file = rb_fstring(file);
2029 rb_execution_context_t *ec = GET_EC();
2030 VALUE v = rb_vm_push_frame_fname(ec, file);
2032 make_compile_option(&option, opt);
2035 pm_parse_result_init(&result);
2036 result.
node.coverage_enabled = option.coverage_enabled;
2038 switch (option.frozen_string_literal) {
2039 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
2041 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
2042 pm_options_frozen_string_literal_set(result.
options,
false);
2044 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
2045 pm_options_frozen_string_literal_set(result.
options,
true);
2048 rb_bug(
"iseqw_s_compile_file_prism: invalid frozen_string_literal=%d", option.frozen_string_literal);
2053 VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
2055 if (error ==
Qnil) {
2057 if (option.coverage_enabled) iseq_new_setup_coverage(file, (
int) (pm_parser_line_offsets(result.
node.parser)->size - 1));
2058 rb_iseq_t *iseq = pm_iseq_new_with_opt(&result.
node, rb_fstring_lit(
"<main>"),
2060 rb_realpath_internal(
Qnil, file, 1),
2061 1, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
2063 pm_parse_result_free(&result);
2067 rb_jump_tag(error_state);
2070 ret = iseqw_new(iseq);
2071 rb_vm_pop_frame(ec);
2076 pm_parse_result_free(&result);
2077 rb_vm_pop_frame(ec);
2114iseqw_s_compile_option_set(
VALUE self,
VALUE opt)
2116 rb_compile_option_t option;
2117 make_compile_option(&option, opt);
2118 COMPILE_OPTION_DEFAULT = option;
2131iseqw_s_compile_option_get(
VALUE self)
2133 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
2136static const rb_iseq_t *
2137iseqw_check(
VALUE iseqw)
2139 rb_iseq_t **iseq_ptr;
2141 rb_iseq_t *iseq = *iseq_ptr;
2143 if (!ISEQ_BODY(iseq)) {
2144 rb_ibf_load_iseq_complete(iseq);
2147 if (!ISEQ_BODY(iseq)->location.label) {
2148 rb_raise(
rb_eTypeError,
"uninitialized InstructionSequence");
2154rb_iseqw_to_iseq(
VALUE iseqw)
2156 return iseqw_check(iseqw);
2168iseqw_eval(
VALUE self)
2170 const rb_iseq_t *iseq = iseqw_check(self);
2171 if (0 == ISEQ_BODY(iseq)->iseq_size) {
2172 rb_raise(
rb_eTypeError,
"attempt to evaluate dummy InstructionSequence");
2174 return rb_iseq_eval(iseq, rb_current_box());
2182iseqw_inspect(
VALUE self)
2184 const rb_iseq_t *iseq = iseqw_check(self);
2188 if (!body->location.label) {
2189 return rb_sprintf(
"#<%"PRIsVALUE
": uninitialized>", klass);
2192 return rb_sprintf(
"<%"PRIsVALUE
":%"PRIsVALUE
"@%"PRIsVALUE
":%d>",
2194 body->location.label, rb_iseq_path(iseq),
2195 FIX2INT(rb_iseq_first_lineno(iseq)));
2223iseqw_path(
VALUE self)
2225 return rb_iseq_path(iseqw_check(self));
2245iseqw_absolute_path(
VALUE self)
2247 return rb_iseq_realpath(iseqw_check(self));
2274iseqw_label(
VALUE self)
2276 return rb_iseq_label(iseqw_check(self));
2300iseqw_base_label(
VALUE self)
2302 return rb_iseq_base_label(iseqw_check(self));
2316iseqw_first_lineno(
VALUE self)
2318 return rb_iseq_first_lineno(iseqw_check(self));
2321static VALUE iseq_data_to_ary(
const rb_iseq_t *iseq);
2405iseqw_to_a(
VALUE self)
2407 const rb_iseq_t *iseq = iseqw_check(self);
2408 return iseq_data_to_ary(iseq);
2411#if VM_INSN_INFO_TABLE_IMPL == 1
2413get_insn_info_binary_search(
const rb_iseq_t *iseq,
size_t pos)
2416 size_t size = body->insns_info.size;
2418 const unsigned int *positions = body->insns_info.positions_or_succ_index_table.positions;
2419 const int debug = 0;
2422 printf(
"size: %"PRIuSIZE
"\n", size);
2423 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2424 (
size_t)0, positions[0], insns_info[0].line_no, pos);
2430 else if (size == 1) {
2431 return &insns_info[0];
2434 size_t l = 1, r = size - 1;
2436 size_t m = l + (r - l) / 2;
2437 if (positions[m] == pos) {
2438 return &insns_info[m];
2440 if (positions[m] < pos) {
2448 return &insns_info[size-1];
2450 if (positions[l] > pos) {
2451 return &insns_info[l-1];
2453 return &insns_info[l];
2458get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2460 return get_insn_info_binary_search(iseq, pos);
2464#if VM_INSN_INFO_TABLE_IMPL == 2
2466get_insn_info_succinct_bitvector(
const rb_iseq_t *iseq,
size_t pos)
2469 size_t size = body->insns_info.size;
2471 const int debug = 0;
2474 printf(
"size: %"PRIuSIZE
"\n", size);
2475 printf(
"insns_info[%"PRIuSIZE
"]: line: %d, pos: %"PRIuSIZE
"\n",
2476 (
size_t)0, insns_info[0].line_no, pos);
2482 else if (size == 1) {
2483 return &insns_info[0];
2487 VM_ASSERT(body->insns_info.positions_or_succ_index_table.succ_index_table != NULL);
2488 index = succ_index_lookup(body->insns_info.positions_or_succ_index_table.succ_index_table, (
int)pos);
2489 return &insns_info[index-1];
2494get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2496 return get_insn_info_succinct_bitvector(iseq, pos);
2500#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2502get_insn_info_linear_search(
const rb_iseq_t *iseq,
const unsigned int *positions,
size_t pos)
2505 size_t i = 0, size = body->insns_info.size;
2507 const int debug = 0;
2510 printf(
"size: %"PRIuSIZE
"\n", size);
2511 printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2512 i, positions[i], insns_info[i].line_no, pos);
2518 else if (size == 1) {
2519 return &insns_info[0];
2522 for (i=1; i<size; i++) {
2523 if (debug) printf(
"insns_info[%"PRIuSIZE
"]: position: %d, line: %d, pos: %"PRIuSIZE
"\n",
2524 i, positions[i], insns_info[i].line_no, pos);
2526 if (positions[i] == pos) {
2527 return &insns_info[i];
2529 if (positions[i] > pos) {
2530 return &insns_info[i-1];
2534 return &insns_info[i-1];
2538#if VM_INSN_INFO_TABLE_IMPL == 0
2540get_insn_info(
const rb_iseq_t *iseq,
size_t pos)
2542 return get_insn_info_linear_search(iseq, ISEQ_BODY(iseq)->insns_info.positions_or_succ_index_table.positions, pos);
2546#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2548validate_get_insn_info(
const rb_iseq_t *iseq)
2552#if VM_INSN_INFO_TABLE_IMPL == 2
2553 unsigned int *positions = rb_iseq_insns_info_decode_positions(body);
2555 const unsigned int *positions = body->insns_info.positions_or_succ_index_table.positions;
2557 for (i = 0; i < body->iseq_size; i++) {
2558 if (get_insn_info_linear_search(iseq, positions, i) != get_insn_info(iseq, i)) {
2559 rb_bug(
"validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE
") != get_insn_info(iseq, %"PRIuSIZE
")", i, i);
2562#if VM_INSN_INFO_TABLE_IMPL == 2
2563 SIZED_FREE_N(positions, body->insns_info.size);
2569rb_iseq_line_no(
const rb_iseq_t *iseq,
size_t pos)
2574 return entry->line_no;
2581#ifdef USE_ISEQ_NODE_ID
2583node_id_stream_read(
const uint8_t **ptr,
const uint8_t *end)
2586 unsigned int shift = 0;
2587 while (*ptr < end) {
2588 uint8_t
byte = **ptr;
2590 zz |= (uint32_t)(
byte & 0x7f) << shift;
2591 if (!(
byte & 0x80))
break;
2594 return (
int)(zz >> 1) ^ -(
int)(zz & 1);
2598rb_iseq_node_id(
const rb_iseq_t *iseq,
size_t pos)
2601 if (body->insns_info.node_ids_size == 0) {
2605 const VALUE *code = rb_iseq_original_iseq(iseq);
2606 const uint8_t *ptr = body->insns_info.node_ids;
2607 const uint8_t *end = ptr + body->insns_info.node_ids_size;
2608 size_t code_pos = 0;
2611 while (ptr < end && code_pos < body->iseq_size) {
2612 size_t len = (size_t)insn_len(code[code_pos]);
2613 node_id += node_id_stream_read(&ptr, end);
2614 if (pos < code_pos +
len) {
2620 rb_bug(
"node_id out of sync with iseq");
2625rb_iseq_event_flags(
const rb_iseq_t *iseq,
size_t pos)
2629 return entry->events;
2636static void rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos);
2641rb_iseq_clear_event_flags(
const rb_iseq_t *iseq,
size_t pos,
rb_event_flag_t reset)
2648 entry->events &= ~reset;
2649 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2650 rb_iseq_trace_flag_cleared(iseq, pos);
2657local_var_name(
const rb_iseq_t *diseq,
VALUE level,
VALUE op)
2664 for (i = 0; i < level; i++) {
2665 diseq = ISEQ_BODY(diseq)->parent_iseq;
2667 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2668 lid = ISEQ_BODY(diseq)->local_table[idx];
2669 name = rb_id2str(lid);
2679 rb_str_catf(name,
"@%d", idx);
2683int rb_insn_unified_local_var_level(
VALUE);
2687rb_insn_operand_intern(
const rb_iseq_t *iseq,
2691 const char *types = insn_op_types(insn);
2692 char type = types[op_no];
2697 ret = rb_sprintf(
"%"PRIdVALUE, (
VALUE)(pos +
len + op));
2701 if (insn == BIN(defined) && op_no == 0) {
2702 enum defined_type deftype = (
enum defined_type)op;
2705 ret = rb_fstring_lit(
"func");
2708 ret = rb_fstring_lit(
"ref");
2710 case DEFINED_CONST_FROM:
2711 ret = rb_fstring_lit(
"constant-from");
2714 ret = rb_iseq_defined_string(deftype);
2719 else if (insn == BIN(checktype) && op_no == 0) {
2725 ret = rb_sprintf(
"%"PRIuVALUE, op);
2730 if (types[op_no+1] == TS_NUM && pnop) {
2731 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2733 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2734 ret = local_var_name(iseq, (
VALUE)level, op - VM_ENV_DATA_SIZE);
2746 op = obj_resurrect(op);
2747 if (insn == BIN(defined) && op_no == 1 &&
FIXNUM_P(op)) {
2752 ret = rb_sprintf(
":$%c", (
type >> 1));
2755 ret = rb_sprintf(
":$%d", (
type >> 1));
2760 ret = rb_dump_literal(op);
2771 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2772 ret = ISEQ_BODY(iseq)->location.label;
2786 const ID *segments = ((IC)op)->segments;
2789 rb_str_catf(ret,
"::%s", rb_id2name(*segments++));
2805 ID mid = vm_ci_mid(ci);
2808 rb_ary_push(ary, rb_sprintf(
"mid:%"PRIsVALUE, rb_id2str(mid)));
2811 rb_ary_push(ary, rb_sprintf(
"argc:%d", vm_ci_argc(ci)));
2813 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2819 if (vm_ci_flag(ci)) {
2821# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2822 CALL_FLAG(ARGS_SPLAT);
2823 CALL_FLAG(ARGS_SPLAT_MUT);
2824 CALL_FLAG(ARGS_BLOCKARG);
2827 CALL_FLAG(ARGS_SIMPLE);
2828 CALL_FLAG(TAILCALL);
2832 CALL_FLAG(KW_SPLAT);
2833 CALL_FLAG(KW_SPLAT_MUT);
2834 CALL_FLAG(FORWARDING);
2835 CALL_FLAG(IGNORE_REFINEMENTS);
2836 CALL_FLAG(OPT_SEND);
2852 if (dladdr((
void *)op, &info) && info.dli_sname) {
2864 ret = rb_sprintf(
"<builtin!%s/%d>",
2865 bf->name, bf->argc);
2870 rb_bug(
"unknown operand type: %c",
type);
2876right_strip(
VALUE str)
2878 const char *beg = RSTRING_PTR(str), *end =
RSTRING_END(str);
2879 while (end-- > beg && *end ==
' ');
2889rb_iseq_disasm_insn(
VALUE ret,
const VALUE *code,
size_t pos,
2890 const rb_iseq_t *iseq,
VALUE child)
2892 VALUE insn = code[pos];
2893 int len = insn_len(insn);
2895 const char *types = insn_op_types(insn);
2897 const char *insn_name_buff;
2899 insn_name_buff = insn_name(insn);
2901 extern const int rb_vm_max_insn_name_size;
2902 rb_str_catf(str,
"%04"PRIuSIZE
" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2905 rb_str_catf(str,
"%04"PRIuSIZE
" %-28.*s ", pos,
2906 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
2909 for (j = 0; types[j]; j++) {
2910 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2911 len, pos, &code[pos + j + 2],
2921 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2922 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2923 if (line_no && line_no != prev) {
2925 slen = (slen > 70) ? 0 : (70 - slen);
2926 str = rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
2933 str = rb_str_catf(str,
"[%s%s%s%s%s%s%s%s%s%s%s%s]",
2944 events & RUBY_EVENT_COVERAGE_LINE ?
"Cli" :
"",
2945 events & RUBY_EVENT_COVERAGE_BRANCH ?
"Cbr" :
"");
2955 printf(
"%.*s\n", (
int)RSTRING_LEN(str), RSTRING_PTR(str));
2964 case CATCH_TYPE_RESCUE:
2966 case CATCH_TYPE_ENSURE:
2968 case CATCH_TYPE_RETRY:
2970 case CATCH_TYPE_BREAK:
2972 case CATCH_TYPE_REDO:
2974 case CATCH_TYPE_NEXT:
2977 rb_bug(
"unknown catch type: %d",
type);
2983iseq_inspect(
const rb_iseq_t *iseq)
2986 if (!body->location.label) {
2987 return rb_sprintf(
"#<ISeq: uninitialized>");
2990 const rb_code_location_t *loc = &body->location.code_location;
2991 return rb_sprintf(
"#<ISeq:%"PRIsVALUE
"@%"PRIsVALUE
":%d (%d,%d)-(%d,%d)>",
2992 body->location.label, rb_iseq_path(iseq),
2993 loc->beg_pos.lineno,
2994 loc->beg_pos.lineno,
2995 loc->beg_pos.column,
2996 loc->end_pos.lineno,
2997 loc->end_pos.column);
3003 {(void (*)(
void *))rb_mark_set, (void (*)(
void *))st_free_table, 0, 0,},
3004 0, 0, RUBY_TYPED_THREAD_SAFE_FREE
3008rb_iseq_disasm_recursive(const rb_iseq_t *iseq,
VALUE indent)
3018 enum {header_minlen = 72};
3019 st_table *done_iseq = 0;
3021 const char *indent_str;
3024 size = body->iseq_size;
3026 indent_len = RSTRING_LEN(indent);
3027 indent_str = RSTRING_PTR(indent);
3033 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
3037 if (ISEQ_BODY(iseq)->builtin_attrs) {
3038#define disasm_builtin_attr(str, iseq, attr) \
3039 if (ISEQ_BODY(iseq)->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
3040 rb_str_cat2(str, " " #attr); \
3042 disasm_builtin_attr(str, iseq, LEAF);
3043 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
3044 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
3045 disasm_builtin_attr(str, iseq, C_TRACE);
3046 disasm_builtin_attr(str, iseq, WITHOUT_INTERRUPTS);
3047 disasm_builtin_attr(str, iseq, CALLER_USER_BOX);
3048 disasm_builtin_attr(str, iseq, ALWAYS_INLINE);
3053 if (body->catch_table) {
3057 if (body->catch_table) {
3059 indent_str = RSTRING_PTR(indent);
3060 for (i = 0; i < body->catch_table->size; i++) {
3062 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
3065 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
3066 catch_type((
int)entry->type), (
int)entry->start,
3067 (
int)entry->end, (
int)entry->sp, (
int)entry->cont);
3068 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
3069 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
3071 done_iseq = st_init_numtable();
3074 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
3075 indent_str = RSTRING_PTR(indent);
3078 rb_str_resize(indent, indent_len);
3079 indent_str = RSTRING_PTR(indent);
3081 if (body->catch_table) {
3083 rb_str_cat2(str,
"|-------------------------------------"
3084 "-----------------------------------\n");
3088 if (body->local_table) {
3089 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
3092 "local table (size: %d, argc: %d "
3093 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
3094 body->local_table_size,
3095 body->param.lead_num,
3096 body->param.opt_num,
3097 body->param.flags.has_rest ? body->param.rest_start : -1,
3098 body->param.post_num,
3099 body->param.flags.has_block ? body->param.block_start : -1,
3100 body->param.flags.has_kw ? keyword->num : -1,
3101 body->param.flags.has_kw ? keyword->required_num : -1,
3102 body->param.flags.has_kwrest ? keyword->rest_start : -1);
3104 for (i = body->local_table_size; i > 0;) {
3105 int li = body->local_table_size - --i - 1;
3107 VALUE name = local_var_name(iseq, 0, i);
3112 if (body->param.flags.has_opt) {
3113 int argc = body->param.lead_num;
3114 int opts = body->param.opt_num;
3115 if (li >= argc && li < argc + opts) {
3116 snprintf(opti,
sizeof(opti),
"Opt=%"PRIdVALUE,
3117 body->param.opt_table[li - argc]);
3121 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s%s",
3122 (body->param.lead_num > li) ? (body->param.flags.ambiguous_param0 ?
"AmbiguousArg" :
"Arg") :
"",
3124 (body->param.flags.has_rest && body->param.rest_start == li) ? (body->param.flags.anon_rest ?
"AnonRest" :
"Rest") :
"",
3125 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ?
"Post" :
"",
3126 (body->param.flags.has_kwrest && keyword->rest_start == li) ? (body->param.flags.anon_kwrest ?
"AnonKwrest" :
"Kwrest") :
"",
3127 (body->param.flags.has_block && body->param.block_start == li) ?
"Block" :
"");
3130 rb_str_catf(str,
"[%2d] ", i + 1);
3131 width = RSTRING_LEN(str) + 11;
3133 if (*argi) rb_str_catf(str,
"<%s>", argi);
3134 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str,
"%*s", (
int)width,
"");
3140 code = rb_iseq_original_iseq(iseq);
3141 for (n = 0; n < size;) {
3143 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
3146 if (body->mandatory_only_iseq) {
3152 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv))
continue;
3154 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
3155 indent_str = RSTRING_PTR(indent);
3163rb_iseq_disasm(
const rb_iseq_t *iseq)
3166 rb_str_resize(str, RSTRING_LEN(str));
3176rb_estimate_iv_count(
VALUE klass,
const rb_iseq_t * initialize_iseq)
3179 set_init_embedded_numtable_with_size(&iv_names, 0);
3181 for (
unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
3182 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
3184 if (cache->iv_set_name) {
3185 set_insert(&iv_names, cache->iv_set_name);
3189 size_t count = iv_names.num_entries;
3192 if (!
NIL_P(superclass)) {
3193 count += RCLASS_MAX_IV_COUNT(superclass);
3196 set_free_embedded_table(&iv_names);
3198 if (count > (attr_index_t)-1) {
3199 return (attr_index_t)-1;
3202 return (attr_index_t)count;
3224iseqw_disasm(
VALUE self)
3226 return rb_iseq_disasm(iseqw_check(self));
3230iseq_iterate_children(
const rb_iseq_t *iseq,
void (*iter_func)(
const rb_iseq_t *child_iseq,
void *data),
void *data)
3233 VALUE *code = rb_iseq_original_iseq(iseq);
3235 const rb_iseq_t *child;
3238 if (body->catch_table) {
3239 for (i = 0; i < body->catch_table->size; i++) {
3241 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
3242 child = entry->iseq;
3244 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3245 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3246 (*iter_func)(child, data);
3252 for (i=0; i<body->iseq_size;) {
3253 VALUE insn = code[i];
3254 int len = insn_len(insn);
3255 const char *types = insn_op_types(insn);
3258 for (j=0; types[j]; j++) {
3261 child = (
const rb_iseq_t *)code[i+j+1];
3263 if (
NIL_P(rb_hash_aref(all_children, (
VALUE)child))) {
3264 rb_hash_aset(all_children, (
VALUE)child,
Qtrue);
3265 (*iter_func)(child, data);
3280yield_each_children(
const rb_iseq_t *child_iseq,
void *data)
3294iseqw_each_child(
VALUE self)
3296 const rb_iseq_t *iseq = iseqw_check(self);
3297 iseq_iterate_children(iseq, yield_each_children, NULL);
3304#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
3324iseqw_trace_points(
VALUE self)
3326 const rb_iseq_t *iseq = iseqw_check(self);
3331 for (i=0; i<body->insns_info.size; i++) {
3333 if (entry->events) {
3334 push_event_info(iseq, entry->events, entry->line_no, ary);
3382 const rb_iseq_t *iseq = NULL;
3384 if (rb_frame_info_p(body)) {
3385 iseq = rb_get_iseq_from_frame_info(body);
3389 iseq = rb_proc_get_iseq(body, NULL);
3391 if (!rb_obj_is_iseq((
VALUE)iseq)) {
3396 iseq = rb_method_iseq(body);
3398 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
3402 return iseq ? iseqw_new(iseq) :
Qnil;
3460 VALUE iseqw = iseqw_s_of(klass, body);
3461 return NIL_P(iseqw) ?
Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3465register_label(
struct st_table *table,
unsigned long idx)
3468 st_insert(table, idx, sym);
3477 case CATCH_TYPE_RESCUE:
CONST_ID(
id,
"rescue");
break;
3478 case CATCH_TYPE_ENSURE:
CONST_ID(
id,
"ensure");
break;
3479 case CATCH_TYPE_RETRY:
CONST_ID(
id,
"retry");
break;
3480 case CATCH_TYPE_BREAK:
CONST_ID(
id,
"break");
break;
3481 case CATCH_TYPE_REDO:
CONST_ID(
id,
"redo");
break;
3482 case CATCH_TYPE_NEXT:
CONST_ID(
id,
"next");
break;
3484 rb_bug(
"unknown exception type: %d", (
int)
type);
3499 {(void (*)(
void *))rb_mark_tbl, (void (*)(
void *))st_free_table, 0, 0,},
3500 0, 0, RUBY_TYPED_THREAD_SAFE_FREE
3503#define DECL_ID(name) \
3506#define INIT_ID(name) \
3507 id_##name = rb_intern(#name)
3510iseq_type_id(
enum rb_iseq_type
type)
3535 case ISEQ_TYPE_TOP:
return id_top;
3536 case ISEQ_TYPE_METHOD:
return id_method;
3537 case ISEQ_TYPE_BLOCK:
return id_block;
3538 case ISEQ_TYPE_CLASS:
return id_class;
3539 case ISEQ_TYPE_RESCUE:
return id_rescue;
3540 case ISEQ_TYPE_ENSURE:
return id_ensure;
3541 case ISEQ_TYPE_EVAL:
return id_eval;
3542 case ISEQ_TYPE_MAIN:
return id_main;
3543 case ISEQ_TYPE_PLAIN:
return id_plain;
3546 rb_bug(
"unsupported iseq type: %d", (
int)
type);
3550iseq_data_to_ary(
const rb_iseq_t *iseq)
3559 VALUE *seq, *iseq_original;
3570 static ID insn_syms[VM_BARE_INSTRUCTION_SIZE];
3571 struct st_table *labels_table = st_init_numtable();
3574 if (insn_syms[0] == 0) {
3576 for (i=0; i<numberof(insn_syms); i++) {
3577 insn_syms[i] = rb_intern(insn_name(i));
3582 type = iseq_type_id(iseq_body->type);
3585 for (i=0; i<iseq_body->local_table_size; i++) {
3586 ID lid = iseq_body->local_table[i];
3588 if (lid != idItImplicit && rb_id2str(lid)) {
3602 const struct rb_iseq_param_keyword *
const keyword = iseq_body->param.keyword;
3605 if (iseq_body->param.flags.has_opt) {
3606 int len = iseq_body->param.opt_num + 1;
3609 for (j = 0; j <
len; j++) {
3610 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
3613 rb_hash_aset(params,
ID2SYM(rb_intern(
"opt")), arg_opt_labels);
3617 if (iseq_body->param.flags.has_lead) rb_hash_aset(params,
ID2SYM(rb_intern(
"lead_num")),
INT2FIX(iseq_body->param.lead_num));
3618 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_num")),
INT2FIX(iseq_body->param.post_num));
3619 if (iseq_body->param.flags.has_post) rb_hash_aset(params,
ID2SYM(rb_intern(
"post_start")),
INT2FIX(iseq_body->param.post_start));
3620 if (iseq_body->param.flags.has_rest) rb_hash_aset(params,
ID2SYM(rb_intern(
"rest_start")),
INT2FIX(iseq_body->param.rest_start));
3621 if (iseq_body->param.flags.has_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"block_start")),
INT2FIX(iseq_body->param.block_start));
3622 if (iseq_body->param.flags.has_kw) {
3625 for (i=0; i<keyword->required_num; i++) {
3628 for (j=0; i<keyword->num; i++, j++) {
3629 VALUE key = rb_ary_new_from_args(1,
ID2SYM(keyword->table[i]));
3630 if (!UNDEF_P(keyword->default_values[j])) {
3636 rb_hash_aset(params,
ID2SYM(rb_intern(
"kwbits")),
3637 INT2FIX(keyword->bits_start));
3638 rb_hash_aset(params,
ID2SYM(rb_intern(
"keyword")), keywords);
3640 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params,
ID2SYM(rb_intern(
"kwrest")),
INT2FIX(keyword->rest_start));
3641 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params,
ID2SYM(rb_intern(
"ambiguous_param0")),
Qtrue);
3642 if (iseq_body->param.flags.use_block) rb_hash_aset(params,
ID2SYM(rb_intern(
"use_block")),
Qtrue);
3646 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3648 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3649 VALUE insn = *seq++;
3650 int j,
len = insn_len(insn);
3655 for (j=0; j<
len-1; j++, seq++) {
3656 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3660 unsigned long idx = nseq - iseq_original + *seq;
3661 rb_ary_push(ary, register_label(labels_table, idx));
3673 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3675 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3686 const ID *ids = ((IC)*seq)->segments;
3706 int argc = vm_ci_argc(ci);
3708 ID mid = vm_ci_mid(ci);
3710 rb_hash_aset(e,
ID2SYM(rb_intern(
"flag")),
UINT2NUM(vm_ci_flag(ci)));
3712 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3717 argc -= kwarg->keyword_len;
3718 for (i = 0; i < kwarg->keyword_len; i++) {
3721 rb_hash_aset(e,
ID2SYM(rb_intern(
"kw_arg")), kw);
3724 rb_hash_aset(e,
ID2SYM(rb_intern(
"orig_argc")),
3734 VALUE cdhash = *seq;
3738 st_foreach(rb_imemo_cdhash_tbl(cdhash), cdhash_each, val);
3742 unsigned long idx = nseq - iseq_original + pos;
3745 register_label(labels_table, idx));
3752#if SIZEOF_VALUE <= SIZEOF_LONG
3763#if SIZEOF_VALUE <= SIZEOF_LONG
3768 rb_hash_aset(val,
ID2SYM(rb_intern(
"func_ptr")), func_ptr);
3769 rb_hash_aset(val,
ID2SYM(rb_intern(
"argc")),
INT2NUM(((RB_BUILTIN)*seq)->argc));
3770 rb_hash_aset(val,
ID2SYM(rb_intern(
"index")),
INT2NUM(((RB_BUILTIN)*seq)->index));
3776 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
3785 if (iseq_body->catch_table)
for (i=0; i<iseq_body->catch_table->size; i++) {
3788 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3789 rb_ary_push(ary, exception_type2symbol(entry->type));
3791 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3796 rb_ary_push(ary, register_label(labels_table, entry->start));
3797 rb_ary_push(ary, register_label(labels_table, entry->end));
3798 rb_ary_push(ary, register_label(labels_table, entry->cont));
3805 prev_insn_info = NULL;
3806#ifdef USE_ISEQ_NODE_ID
3808 const uint8_t *nid_ptr = iseq_body->insns_info.node_ids;
3809 const uint8_t *nid_end = nid_ptr ? nid_ptr + iseq_body->insns_info.node_ids_size : NULL;
3818 if (st_lookup(labels_table, pos, &label)) {
3822 info = get_insn_info(iseq, pos);
3823#ifdef USE_ISEQ_NODE_ID
3824 VM_ASSERT(nid_ptr && nid_ptr < nid_end);
3825 if (nid_ptr && nid_ptr < nid_end) {
3826 node_id += node_id_stream_read(&nid_ptr, nid_end);
3831 if (prev_insn_info != info) {
3832 int line = info->line_no;
3835 if (line > 0 && last_line != line) {
3839#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3849 prev_insn_info = info;
3858 rb_hash_aset(misc,
ID2SYM(rb_intern(
"arg_size")),
INT2FIX(iseq_body->param.size));
3859 rb_hash_aset(misc,
ID2SYM(rb_intern(
"local_size")),
INT2FIX(iseq_body->local_table_size));
3860 rb_hash_aset(misc,
ID2SYM(rb_intern(
"stack_max")),
INT2FIX(iseq_body->stack_max));
3861 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_id")),
INT2FIX(iseq_body->location.node_id));
3862 rb_hash_aset(misc,
ID2SYM(rb_intern(
"source_hash")), iseq_body->source_hash ?
ULL2NUM(iseq_body->source_hash) :
Qnil);
3863 rb_hash_aset(misc,
ID2SYM(rb_intern(
"code_location")),
3864 rb_ary_new_from_args(4,
3865 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3866 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3867 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3868 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3869#ifdef USE_ISEQ_NODE_ID
3870 rb_hash_aset(misc,
ID2SYM(rb_intern(
"node_ids")), node_ids);
3872 rb_hash_aset(misc,
ID2SYM(rb_intern(
"parser")), iseq_body->prism ?
ID2SYM(rb_intern(
"prism")) :
ID2SYM(rb_intern(
"parse.y")));
3900rb_iseq_parameters(
const rb_iseq_t *iseq,
int is_proc)
3904 const struct rb_iseq_param_keyword *
const keyword = body->param.keyword;
3906 ID req, opt, rest, block, key, keyrest;
3907#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3908#define PARAM_ID(i) body->local_table[(i)]
3909#define PARAM(i, type) ( \
3911 PARAM_ID(i) != idItImplicit && rb_id2str(PARAM_ID(i)) ? \
3912 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3918 if (body->param.flags.forwardable) {
3929 for (i = 0; i < body->param.lead_num; i++) {
3934 for (i = 0; i < body->param.lead_num; i++) {
3938 r = body->param.lead_num + body->param.opt_num;
3939 for (; i < r; i++) {
3942 if (body->param.flags.has_rest) {
3944 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3946 r = body->param.post_start + body->param.post_num;
3948 for (i = body->param.post_start; i < r; i++) {
3953 for (i = body->param.post_start; i < r; i++) {
3957 if (body->param.flags.accepts_no_kwarg) {
3963 if (body->param.flags.has_kw) {
3965 if (keyword->required_num > 0) {
3968 for (; i < keyword->required_num; i++) {
3970 if (rb_id2str(keyword->table[i])) {
3977 for (; i < keyword->num; i++) {
3979 if (rb_id2str(keyword->table[i])) {
3985 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3988 PARAM_TYPE(keyrest);
3989 if (body->param.flags.has_kwrest &&
3990 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3993 else if (body->param.flags.ruby2_keywords) {
3998 if (body->param.flags.accepts_no_block) {
4001 PARAM_TYPE(noblock);
4004 else if (body->param.flags.has_block) {
4006 rb_ary_push(args, PARAM(body->param.block_start, block));
4012rb_iseq_defined_string(
enum defined_type
type)
4014 static const char expr_names[][18] = {
4016 "instance-variable",
4032 if ((
unsigned)(
type - 1) >= (
unsigned)numberof(expr_names)) rb_bug(
"unknown defined type %d",
type);
4033 estr = expr_names[
type - 1];
4034 return rb_fstring_cstr(estr);
4040static st_table *encoded_insn_data;
4044 void *notrace_encoded_insn;
4045 void *trace_encoded_insn;
4048 void *zjit_encoded_insn;
4051static insn_data_t insn_data[VM_BARE_INSTRUCTION_SIZE];
4054rb_free_encoded_insn_data(
void)
4056 st_free_table(encoded_insn_data);
4062rb_vm_encoded_insn_data_table_init(
void)
4064#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
4065 const void *
const *table = rb_vm_get_insns_address_table();
4066#define INSN_CODE(insn) ((VALUE)table[insn])
4068#define INSN_CODE(insn) ((VALUE)(insn))
4070 encoded_insn_data = st_init_numtable_with_size(VM_BARE_INSTRUCTION_SIZE);
4072 for (
int insn = 0; insn < VM_BARE_INSTRUCTION_SIZE; insn++) {
4073 insn_data[insn].insn = insn;
4074 insn_data[insn].insn_len = insn_len(insn);
4079 int notrace_insn = (insn != BIN(opt_invokebuiltin_delegate_leave)) ? insn : BIN(opt_invokebuiltin_delegate);
4080 insn_data[insn].notrace_encoded_insn = (
void *)INSN_CODE(notrace_insn);
4081 insn_data[insn].trace_encoded_insn = (
void *)INSN_CODE(notrace_insn + VM_BARE_INSTRUCTION_SIZE);
4083 st_data_t key1 = (st_data_t)INSN_CODE(insn);
4084 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_BARE_INSTRUCTION_SIZE);
4085 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
4086 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
4089 int zjit_insn = vm_bare_insn_to_zjit_insn(insn);
4090 insn_data[insn].zjit_insn = zjit_insn;
4091 insn_data[insn].zjit_encoded_insn = (insn != zjit_insn) ? (
void *)INSN_CODE(zjit_insn) : 0;
4093 if (insn != zjit_insn) {
4094 st_data_t key3 = (st_data_t)INSN_CODE(zjit_insn);
4095 st_add_direct(encoded_insn_data, key3, (st_data_t)&insn_data[insn]);
4105rb_vm_insn_addr2insn(
const void *addr)
4107 st_data_t key = (st_data_t)addr;
4110 if (st_lookup(encoded_insn_data, key, &val)) {
4111 insn_data_t *e = (insn_data_t *)val;
4112 return (
int)e->insn;
4115 rb_bug(
"rb_vm_insn_addr2insn: invalid insn address: %p", addr);
4121rb_vm_insn_addr2opcode(
const void *addr)
4123 st_data_t key = (st_data_t)addr;
4126 if (st_lookup(encoded_insn_data, key, &val)) {
4127 insn_data_t *e = (insn_data_t *)val;
4128 int opcode = e->insn;
4129 if (addr == e->trace_encoded_insn) {
4130 opcode += VM_BARE_INSTRUCTION_SIZE;
4133 else if (addr == e->zjit_encoded_insn) {
4134 opcode = e->zjit_insn;
4140 rb_bug(
"rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
4147rb_vm_insn_decode(
const VALUE encoded)
4149 return rb_vm_insn_addr2insn((
void *)encoded);
4154encoded_iseq_trace_instrument(
VALUE *iseq_encoded_insn,
rb_event_flag_t turnon,
bool remain_traced)
4156 st_data_t key = (st_data_t)*iseq_encoded_insn;
4159 if (st_lookup(encoded_insn_data, key, &val)) {
4160 insn_data_t *e = (insn_data_t *)val;
4161 if (remain_traced && key == (st_data_t)e->trace_encoded_insn) {
4164 *iseq_encoded_insn = (
VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
4168 rb_bug(
"trace_instrument: invalid insn address: %p", (
void *)*iseq_encoded_insn);
4173rb_iseq_trace_flag_cleared(
const rb_iseq_t *iseq,
size_t pos)
4176 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4177 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0,
false);
4198iseq_add_local_tracepoint(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events,
VALUE tpval,
unsigned int target_line, rb_ractor_t *r)
4203 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4204 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4206 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
4207 ASSERT_vm_locking_with_barrier();
4209 for (pc=0; pc<body->iseq_size;) {
4213 unsigned int line = (int)entry->line_no;
4215 if (target_line == 0 || target_line == line) {
4222 if (pc_events & target_events) {
4225 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events),
true);
4229 rb_hook_list_t *hook_list = rb_iseq_local_hooks(iseq, r,
true);
4230 rb_hook_list_connect_local_tracepoint(hook_list, tpval, target_line);
4231 iseq_mut->aux.exec.local_hooks_cnt++;
4240 unsigned int target_line;
4246iseq_add_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4249 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line, data->r);
4250 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
4254rb_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)
4256 ASSERT_vm_locking_with_barrier();
4258 if (target_bmethod) {
4259 turnon_events = add_bmethod_events(turnon_events);
4261 data.turnon_events = turnon_events;
4263 data.target_line = target_line;
4265 data.r = GET_RACTOR();
4267 iseq_add_local_tracepoint_i(iseq, (
void *)&data);
4269 VALUE disasm = rb_iseq_disasm(iseq);
4270 fprintf(stderr,
"Iseq disasm:\n:%.*s",
4277iseq_remove_local_tracepoint(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4280 unsigned int num_hooks_left;
4283 rb_iseq_t *iseq_mut = (rb_iseq_t*)iseq;
4284 rb_hook_list_t *hook_list;
4285 VALUE *iseq_encoded;
4286 ASSERT_vm_locking_with_barrier();
4288 hook_list = rb_iseq_local_hooks(iseq, r,
false);
4294 if (rb_hook_list_remove_local_tracepoint(hook_list, tpval)) {
4296 iseq_mut->aux.exec.local_hooks_cnt--;
4297 local_events = hook_list->events;
4298 num_hooks_left = rb_hook_list_count(hook_list);
4299 if (local_events == 0 && prev_events != 0) {
4300 st_delete(rb_ractor_targeted_hooks(r), (st_data_t*)&iseq, NULL);
4301 rb_hook_list_free(hook_list);
4304 if (iseq->aux.exec.local_hooks_cnt == num_hooks_left) {
4305 body = ISEQ_BODY(iseq);
4306 iseq_encoded = (
VALUE *)body->iseq_encoded;
4307 local_events = add_bmethod_events(local_events);
4308 for (pc = 0; pc<body->iseq_size;) {
4310 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events),
false);
4327iseq_remove_local_tracepoint_i(
const rb_iseq_t *iseq,
void *p)
4330 data->n += iseq_remove_local_tracepoint(iseq, data->tpval, data->r);
4331 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
4335rb_iseq_remove_local_tracepoint_recursively(
const rb_iseq_t *iseq,
VALUE tpval, rb_ractor_t *r)
4338 ASSERT_vm_locking_with_barrier();
4343 iseq_remove_local_tracepoint_i(iseq, (
void *)&data);
4348rb_iseq_trace_set(
const rb_iseq_t *iseq,
rb_event_flag_t turnon_events)
4350 if (iseq->aux.exec.global_trace_events == turnon_events) {
4354 if (!ISEQ_EXECUTABLE_P(iseq)) {
4363 VALUE *iseq_encoded = (
VALUE *)body->iseq_encoded;
4365 rb_hook_list_t *local_hooks = rb_iseq_local_hooks(iseq, GET_RACTOR(),
false);
4367 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
4368 enabled_events = add_bmethod_events(turnon_events | local_events);
4370 for (pc=0; pc<body->iseq_size;) {
4372 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events,
true);
4380clear_attr_cc(
VALUE v)
4382 ASSERT_vm_locking_with_barrier();
4383 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((
const struct rb_callcache *)v)) {
4395 ASSERT_vm_locking_with_barrier();
4396 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((
const struct rb_callcache *)v)) {
4406clear_attr_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4409 for (; v != (
VALUE)vend; v += stride) {
4410 void *ptr = rb_asan_poisoned_object_p(v);
4411 rb_asan_unpoison_object(v,
false);
4413 asan_poison_object_if(ptr, v);
4419rb_clear_attr_ccs(
void)
4421 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
4425clear_bf_ccs_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4428 for (; v != (
VALUE)vend; v += stride) {
4429 void *ptr = rb_asan_poisoned_object_p(v);
4430 rb_asan_unpoison_object(v,
false);
4432 asan_poison_object_if(ptr, v);
4438rb_clear_bf_ccs(
void)
4440 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
4444trace_set_i(
void *vstart,
void *vend,
size_t stride,
void *data)
4449 for (; v != (
VALUE)vend; v += stride) {
4450 void *ptr = rb_asan_poisoned_object_p(v);
4451 rb_asan_unpoison_object(v,
false);
4453 if (rb_obj_is_iseq(v)) {
4454 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
4456 else if (clear_attr_cc(v)) {
4458 else if (clear_bf_cc(v)) {
4461 asan_poison_object_if(ptr, v);
4469 rb_objspace_each_objects(trace_set_i, &turnon_events);
4473rb_iseqw_local_variables(
VALUE iseqval)
4475 return rb_iseq_local_variables(iseqw_check(iseqval));
4496iseqw_to_binary(
int argc,
VALUE *argv,
VALUE self)
4499 return rb_iseq_ibf_dump(iseqw_check(self), opt);
4516iseqw_s_load_from_binary(
VALUE self,
VALUE str)
4518 return iseqw_new(rb_iseq_ibf_load(str));
4524 return iseqw_new(rb_iseq_ibf_load_bytes(ptr,
len));
4534iseqw_s_load_from_binary_extra_data(
VALUE self,
VALUE str)
4536 return rb_iseq_ibf_load_extra_data(str);
4539#if VM_INSN_INFO_TABLE_IMPL == 2
4572#define IMMEDIATE_TABLE_SIZE 54
4574struct succ_index_table {
4575 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4576 struct succ_dict_block {
4578 uint64_t small_block_ranks;
4579 uint64_t bits[512/64];
4580 } succ_part[FLEX_ARY_LEN];
4583#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4584#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4585#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4586#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4588static struct succ_index_table *
4589succ_index_table_create(
int max_pos,
int *data,
int size)
4591 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4592 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4593 struct succ_index_table *sd =
4594 rb_xcalloc_mul_add_mul(
4595 imm_size,
sizeof(uint64_t),
4596 succ_size,
sizeof(
struct succ_dict_block));
4600 for (j = 0; j < imm_size; j++) {
4601 for (i = 0; i < 9; i++) {
4602 if (r < size && data[r] == j * 9 + i) r++;
4603 imm_block_rank_set(sd->imm_part[j], i, r);
4606 for (k = 0; k < succ_size; k++) {
4607 struct succ_dict_block *sd_block = &sd->succ_part[k];
4610 for (j = 0; j < 8; j++) {
4612 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4613 for (i = 0; i < 64; i++) {
4614 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4615 bits |= ((uint64_t)1) << i;
4619 sd_block->bits[j] = bits;
4620 small_rank += rb_popcount64(bits);
4626static unsigned int *
4627succ_index_table_invert(
int max_pos,
struct succ_index_table *sd,
int size)
4629 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4630 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4631 unsigned int *positions =
ALLOC_N(
unsigned int, size), *p;
4632 int i, j, k, r = -1;
4634 for (j = 0; j < imm_size; j++) {
4635 for (i = 0; i < 9; i++) {
4636 int nr = imm_block_rank_get(sd->imm_part[j], i);
4637 if (r != nr) *p++ = j * 9 + i;
4641 for (k = 0; k < succ_size; k++) {
4642 for (j = 0; j < 8; j++) {
4643 for (i = 0; i < 64; i++) {
4644 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4645 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4654succ_index_lookup(
const struct succ_index_table *sd,
int x)
4656 if (x < IMMEDIATE_TABLE_SIZE) {
4657 const int i = x / 9;
4658 const int j = x % 9;
4659 return imm_block_rank_get(sd->imm_part[i], j);
4662 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4663 const struct succ_dict_block *block = &sd->succ_part[block_index];
4664 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4665 const int small_block_index = block_bit_index / 64;
4666 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4667 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4669 return block->rank + small_block_popcount + popcnt;
4688iseqw_script_lines(
VALUE self)
4690 const rb_iseq_t *iseq = iseqw_check(self);
4691 return ISEQ_SCRIPT_LINES(iseq);
4697iseqw_source_hash(
VALUE self)
4699 const rb_iseq_t *iseq = iseqw_check(self);
4700 if (!ISEQ_BODY(iseq)->source_hash)
return Qnil;
4701 return ULL2NUM(ISEQ_BODY(iseq)->source_hash);
4706iseqw_node_id(
VALUE self)
4708 const rb_iseq_t *iseq = iseqw_check(self);
4709 return INT2NUM(ISEQ_BODY(iseq)->location.node_id);
4740 rb_cISeq = rb_define_class_under(rb_cRubyVM,
"InstructionSequence",
rb_cObject);
#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.
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 ALLOC
Old name of RB_ALLOC.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#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 ULL2NUM
Old name of RB_ULL2NUM.
#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.
VALUE rb_eSyntaxError
SyntaxError exception.
VALUE rb_class_superclass(VALUE klass)
Queries the parent of the given class.
VALUE rb_cObject
Object 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.
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
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.
VALUE rb_iseq_load_from_binary(const char *ptr, size_t len)
Load an iseq object from binary format String object created by RubyVM::InstructionSequence....
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_entry(VALUE ary, rb_len_t off)
Queries an element of an array.
void rb_ary_store(VALUE ary, rb_len_t key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_hidden_new(rb_len_t 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_join(VALUE ary, VALUE sep)
Recursively stringises the elements of the passed array, flattens that result, then joins the sequenc...
VALUE rb_ary_new_from_values(rb_len_t n, const VALUE *elts)
Identical to rb_ary_new_from_args(), except how objects are passed.
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.
void rb_str_modify_expand(VALUE str, rb_len_t capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
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.
void rb_str_set_len(VALUE str, rb_len_t len)
Overwrites the length of the string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, rb_len_t 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...
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.
#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_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
#define RB_OBJ_SHAREABLE_P(obj)
Queries if the passed object has previously classified as shareable or not.
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
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.
int st_foreach(st_table *q, int_type *w, st_data_t e)
Iteration over the given table.
#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)
References an object in an array.
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 int RSTRING_LENINT(VALUE str)
Identical to RSTRING_LEN(), except it differs for the return type.
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 in bytes.
int32_t line
The line number.
A list of offsets of the start of lines in a string.
This struct represents a slice in the source code, defined by an offset and a length.
uint32_t start
The offset of the location from the start of the source.
uint32_t length
The length of the location.
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.
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.
uint64_t source_hash
The source hash of the parsed source, propagated to every iseq.
VALUE * script_lines
This is a pointer to the list of script lines for the ISEQs that will be associated with this scope n...
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
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.