[ruby/irb] Improve help message for no meta commands
[ruby.git] / iseq.c
blobaab58e3760f35efe2a671f7fb38ebc8d7b7a8a7e
1 /**********************************************************************
3 iseq.c -
5 $Author$
6 created at: 2006-07-11(Tue) 09:00:03 +0900
8 Copyright (C) 2006 Koichi Sasada
10 **********************************************************************/
12 #define RUBY_VM_INSNS_INFO 1
13 /* #define RUBY_MARK_FREE_DEBUG 1 */
15 #include "ruby/internal/config.h"
17 #ifdef HAVE_DLADDR
18 # include <dlfcn.h>
19 #endif
21 #include "eval_intern.h"
22 #include "id_table.h"
23 #include "internal.h"
24 #include "internal/bits.h"
25 #include "internal/class.h"
26 #include "internal/compile.h"
27 #include "internal/error.h"
28 #include "internal/file.h"
29 #include "internal/gc.h"
30 #include "internal/hash.h"
31 #include "internal/io.h"
32 #include "internal/ruby_parser.h"
33 #include "internal/sanitizers.h"
34 #include "internal/symbol.h"
35 #include "internal/thread.h"
36 #include "internal/variable.h"
37 #include "iseq.h"
38 #include "rjit.h"
39 #include "ruby/util.h"
40 #include "vm_core.h"
41 #include "vm_callinfo.h"
42 #include "yjit.h"
43 #include "ruby/ractor.h"
44 #include "builtin.h"
45 #include "insns.inc"
46 #include "insns_info.inc"
48 VALUE rb_cISeq;
49 static VALUE iseqw_new(const rb_iseq_t *iseq);
50 static const rb_iseq_t *iseqw_check(VALUE iseqw);
52 #if VM_INSN_INFO_TABLE_IMPL == 2
53 static struct succ_index_table *succ_index_table_create(int max_pos, int *data, int size);
54 static unsigned int *succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size);
55 static int succ_index_lookup(const struct succ_index_table *sd, int x);
56 #endif
58 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
60 static inline VALUE
61 obj_resurrect(VALUE obj)
63 if (hidden_obj_p(obj)) {
64 switch (BUILTIN_TYPE(obj)) {
65 case T_STRING:
66 obj = rb_str_resurrect(obj);
67 break;
68 case T_ARRAY:
69 obj = rb_ary_resurrect(obj);
70 break;
71 case T_HASH:
72 obj = rb_hash_resurrect(obj);
73 break;
74 default:
75 break;
78 return obj;
81 static void
82 free_arena(struct iseq_compile_data_storage *cur)
84 struct iseq_compile_data_storage *next;
86 while (cur) {
87 next = cur->next;
88 ruby_xfree(cur);
89 cur = next;
93 static void
94 compile_data_free(struct iseq_compile_data *compile_data)
96 if (compile_data) {
97 free_arena(compile_data->node.storage_head);
98 free_arena(compile_data->insn.storage_head);
99 if (compile_data->ivar_cache_table) {
100 rb_id_table_free(compile_data->ivar_cache_table);
102 ruby_xfree(compile_data);
106 static void
107 remove_from_constant_cache(ID id, IC ic)
109 rb_vm_t *vm = GET_VM();
110 VALUE lookup_result;
111 st_data_t ic_data = (st_data_t)ic;
113 if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
114 st_table *ics = (st_table *)lookup_result;
115 st_delete(ics, &ic_data, NULL);
117 if (ics->num_entries == 0) {
118 rb_id_table_delete(vm->constant_cache, id);
119 st_free_table(ics);
124 // When an ISEQ is being freed, all of its associated ICs are going to go away
125 // as well. Because of this, we need to iterate over the ICs, and clear them
126 // from the VM's constant cache.
127 static void
128 iseq_clear_ic_references(const rb_iseq_t *iseq)
130 // In some cases (when there is a compilation error), we end up with
131 // ic_size greater than 0, but no allocated is_entries buffer.
132 // If there's no is_entries buffer to loop through, return early.
133 // [Bug #19173]
134 if (!ISEQ_BODY(iseq)->is_entries) {
135 return;
138 for (unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
139 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
141 // Iterate over the IC's constant path's segments and clean any references to
142 // the ICs out of the VM's constant cache table.
143 const ID *segments = ic->segments;
145 // It's possible that segments is NULL if we overallocated an IC but
146 // optimizations removed the instruction using it
147 if (segments == NULL)
148 continue;
150 for (int i = 0; segments[i]; i++) {
151 ID id = segments[i];
152 if (id == idNULL) continue;
153 remove_from_constant_cache(id, ic);
156 ruby_xfree((void *)segments);
160 void
161 rb_iseq_free(const rb_iseq_t *iseq)
163 RUBY_FREE_ENTER("iseq");
165 if (iseq && ISEQ_BODY(iseq)) {
166 iseq_clear_ic_references(iseq);
167 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
168 rb_rjit_free_iseq(iseq); /* Notify RJIT */
169 #if USE_YJIT
170 rb_yjit_iseq_free(iseq);
171 if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
172 RUBY_ASSERT(rb_yjit_live_iseq_count > 0);
173 rb_yjit_live_iseq_count--;
175 #endif
176 ruby_xfree((void *)body->iseq_encoded);
177 ruby_xfree((void *)body->insns_info.body);
178 ruby_xfree((void *)body->insns_info.positions);
179 #if VM_INSN_INFO_TABLE_IMPL == 2
180 ruby_xfree(body->insns_info.succ_index_table);
181 #endif
182 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
183 ruby_xfree((void *)body->local_table);
184 ruby_xfree((void *)body->is_entries);
185 ruby_xfree(body->call_data);
186 ruby_xfree((void *)body->catch_table);
187 ruby_xfree((void *)body->param.opt_table);
188 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
189 ruby_xfree((void *)body->mark_bits.list);
192 ruby_xfree(body->variable.original_iseq);
194 if (body->param.keyword != NULL) {
195 if (body->param.keyword->table != &body->local_table[body->param.keyword->bits_start - body->param.keyword->num])
196 ruby_xfree((void *)body->param.keyword->table);
197 ruby_xfree((void *)body->param.keyword->default_values);
198 ruby_xfree((void *)body->param.keyword);
200 compile_data_free(ISEQ_COMPILE_DATA(iseq));
201 if (body->outer_variables) rb_id_table_free(body->outer_variables);
202 ruby_xfree(body);
205 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
206 rb_hook_list_free(iseq->aux.exec.local_hooks);
209 RUBY_FREE_LEAVE("iseq");
212 typedef VALUE iseq_value_itr_t(void *ctx, VALUE obj);
214 static inline void
215 iseq_scan_bits(unsigned int page, iseq_bits_t bits, VALUE *code, VALUE *original_iseq)
217 unsigned int offset;
218 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
220 while (bits) {
221 offset = ntz_intptr(bits);
222 VALUE op = code[page_offset + offset];
223 rb_gc_mark_and_move(&code[page_offset + offset]);
224 VALUE newop = code[page_offset + offset];
225 if (original_iseq && newop != op) {
226 original_iseq[page_offset + offset] = newop;
228 bits &= bits - 1; // Reset Lowest Set Bit (BLSR)
232 static void
233 rb_iseq_mark_and_move_each_value(const rb_iseq_t *iseq, VALUE *original_iseq)
235 unsigned int size;
236 VALUE *code;
237 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
239 size = body->iseq_size;
240 code = body->iseq_encoded;
242 union iseq_inline_storage_entry *is_entries = body->is_entries;
244 if (body->is_entries) {
245 // Skip iterating over ivc caches
246 is_entries += body->ivc_size;
248 // ICVARC entries
249 for (unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
250 ICVARC icvarc = (ICVARC)is_entries;
251 if (icvarc->entry) {
252 RUBY_ASSERT(!RB_TYPE_P(icvarc->entry->class_value, T_NONE));
254 rb_gc_mark_and_move(&icvarc->entry->class_value);
258 // ISE entries
259 for (unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
260 union iseq_inline_storage_entry *const is = (union iseq_inline_storage_entry *)is_entries;
261 if (is->once.value) {
262 rb_gc_mark_and_move(&is->once.value);
266 // IC Entries
267 for (unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
268 IC ic = (IC)is_entries;
269 if (ic->entry) {
270 rb_gc_mark_and_move_ptr(&ic->entry);
275 // Embedded VALUEs
276 if (body->mark_bits.list) {
277 if (ISEQ_MBITS_BUFLEN(size) == 1) {
278 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
280 else {
281 if (body->mark_bits.list) {
282 for (unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
283 iseq_bits_t bits = body->mark_bits.list[i];
284 iseq_scan_bits(i, bits, code, original_iseq);
291 static bool
292 cc_is_active(const struct rb_callcache *cc, bool reference_updating)
294 if (cc) {
295 if (cc == rb_vm_empty_cc() || rb_vm_empty_cc_for_super()) {
296 return false;
299 if (reference_updating) {
300 cc = (const struct rb_callcache *)rb_gc_location((VALUE)cc);
303 if (vm_cc_markable(cc)) {
304 if (cc->klass) { // cc is not invalidated
305 const struct rb_callable_method_entry_struct *cme = vm_cc_cme(cc);
306 if (reference_updating) {
307 cme = (const struct rb_callable_method_entry_struct *)rb_gc_location((VALUE)cme);
309 if (!METHOD_ENTRY_INVALIDATED(cme)) {
310 return true;
315 return false;
318 void
319 rb_iseq_mark_and_move(rb_iseq_t *iseq, bool reference_updating)
321 RUBY_MARK_ENTER("iseq");
323 rb_gc_mark_and_move(&iseq->wrapper);
325 if (ISEQ_BODY(iseq)) {
326 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
328 rb_iseq_mark_and_move_each_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
330 rb_gc_mark_and_move(&body->variable.coverage);
331 rb_gc_mark_and_move(&body->variable.pc2branchindex);
332 rb_gc_mark_and_move(&body->variable.script_lines);
333 rb_gc_mark_and_move(&body->location.label);
334 rb_gc_mark_and_move(&body->location.base_label);
335 rb_gc_mark_and_move(&body->location.pathobj);
336 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
337 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
338 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
340 if (body->call_data) {
341 for (unsigned int i = 0; i < body->ci_size; i++) {
342 struct rb_call_data *cds = body->call_data;
344 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
346 if (cc_is_active(cds[i].cc, reference_updating)) {
347 rb_gc_mark_and_move_ptr(&cds[i].cc);
349 else {
350 cds[i].cc = rb_vm_empty_cc();
355 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
356 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
358 for (int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
359 rb_gc_mark_and_move(&keyword->default_values[j]);
363 if (body->catch_table) {
364 struct iseq_catch_table *table = body->catch_table;
366 for (unsigned int i = 0; i < table->size; i++) {
367 struct iseq_catch_table_entry *entry;
368 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
369 if (entry->iseq) {
370 rb_gc_mark_and_move_ptr(&entry->iseq);
375 if (reference_updating) {
376 #if USE_RJIT
377 rb_rjit_iseq_update_references(body);
378 #endif
379 #if USE_YJIT
380 rb_yjit_iseq_update_references(iseq);
381 #endif
383 else {
384 #if USE_RJIT
385 rb_rjit_iseq_mark(body->rjit_blocks);
386 #endif
387 #if USE_YJIT
388 rb_yjit_iseq_mark(body->yjit_payload);
389 #endif
393 if (FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) {
394 rb_gc_mark_and_move(&iseq->aux.loader.obj);
396 else if (FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) {
397 const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
399 if (!reference_updating) {
400 /* The operands in each instruction needs to be pinned because
401 * if auto-compaction runs in iseq_set_sequence, then the objects
402 * could exist on the generated_iseq buffer, which would not be
403 * reference updated which can lead to T_MOVED (and subsequently
404 * T_NONE) objects on the iseq. */
405 rb_iseq_mark_and_pin_insn_storage(compile_data->insn.storage_head);
408 rb_gc_mark_and_move((VALUE *)&compile_data->err_info);
409 rb_gc_mark_and_move((VALUE *)&compile_data->catch_table_ary);
411 else {
412 /* executable */
413 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
415 if (iseq->aux.exec.local_hooks) {
416 rb_hook_list_mark_and_update(iseq->aux.exec.local_hooks);
420 RUBY_MARK_LEAVE("iseq");
423 static size_t
424 param_keyword_size(const struct rb_iseq_param_keyword *pkw)
426 size_t size = 0;
428 if (!pkw) return size;
430 size += sizeof(struct rb_iseq_param_keyword);
431 size += sizeof(VALUE) * (pkw->num - pkw->required_num);
433 return size;
436 size_t
437 rb_iseq_memsize(const rb_iseq_t *iseq)
439 size_t size = 0; /* struct already counted as RVALUE size */
440 const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
441 const struct iseq_compile_data *compile_data;
443 /* TODO: should we count original_iseq? */
445 if (ISEQ_EXECUTABLE_P(iseq) && body) {
446 size += sizeof(struct rb_iseq_constant_body);
447 size += body->iseq_size * sizeof(VALUE);
448 size += body->insns_info.size * (sizeof(struct iseq_insn_info_entry) + sizeof(unsigned int));
449 size += body->local_table_size * sizeof(ID);
450 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
451 if (body->catch_table) {
452 size += iseq_catch_table_bytes(body->catch_table->size);
454 size += (body->param.opt_num + 1) * sizeof(VALUE);
455 size += param_keyword_size(body->param.keyword);
457 /* body->is_entries */
458 size += ISEQ_IS_SIZE(body) * sizeof(union iseq_inline_storage_entry);
460 if (ISEQ_BODY(iseq)->is_entries) {
461 /* IC entries constant segments */
462 for (unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
463 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
464 const ID *ids = ic->segments;
465 if (!ids) continue;
466 while (*ids++) {
467 size += sizeof(ID);
469 size += sizeof(ID); // null terminator
473 /* body->call_data */
474 size += body->ci_size * sizeof(struct rb_call_data);
475 // TODO: should we count imemo_callinfo?
478 compile_data = ISEQ_COMPILE_DATA(iseq);
479 if (compile_data) {
480 struct iseq_compile_data_storage *cur;
482 size += sizeof(struct iseq_compile_data);
484 cur = compile_data->node.storage_head;
485 while (cur) {
486 size += cur->size + offsetof(struct iseq_compile_data_storage, buff);
487 cur = cur->next;
491 return size;
494 struct rb_iseq_constant_body *
495 rb_iseq_constant_body_alloc(void)
497 struct rb_iseq_constant_body *iseq_body;
498 iseq_body = ZALLOC(struct rb_iseq_constant_body);
499 return iseq_body;
502 static rb_iseq_t *
503 iseq_alloc(void)
505 rb_iseq_t *iseq = iseq_imemo_alloc();
506 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
507 return iseq;
510 VALUE
511 rb_iseq_pathobj_new(VALUE path, VALUE realpath)
513 VALUE pathobj;
514 VM_ASSERT(RB_TYPE_P(path, T_STRING));
515 VM_ASSERT(NIL_P(realpath) || RB_TYPE_P(realpath, T_STRING));
517 if (path == realpath ||
518 (!NIL_P(realpath) && rb_str_cmp(path, realpath) == 0)) {
519 pathobj = rb_fstring(path);
521 else {
522 if (!NIL_P(realpath)) realpath = rb_fstring(realpath);
523 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
524 rb_obj_freeze(pathobj);
526 return pathobj;
529 void
530 rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath)
532 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->location.pathobj,
533 rb_iseq_pathobj_new(path, realpath));
536 static rb_iseq_location_t *
537 iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id)
539 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
541 rb_iseq_pathobj_set(iseq, path, realpath);
542 RB_OBJ_WRITE(iseq, &loc->label, name);
543 RB_OBJ_WRITE(iseq, &loc->base_label, name);
544 loc->first_lineno = first_lineno;
546 if (ISEQ_BODY(iseq)->local_iseq == iseq && strcmp(RSTRING_PTR(name), "initialize") == 0) {
547 ISEQ_BODY(iseq)->param.flags.use_block = 1;
550 if (code_location) {
551 loc->node_id = node_id;
552 loc->code_location = *code_location;
554 else {
555 loc->code_location.beg_pos.lineno = 0;
556 loc->code_location.beg_pos.column = 0;
557 loc->code_location.end_pos.lineno = -1;
558 loc->code_location.end_pos.column = -1;
561 return loc;
564 static void
565 set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
567 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
568 const VALUE type = body->type;
570 /* set class nest stack */
571 if (type == ISEQ_TYPE_TOP) {
572 body->local_iseq = iseq;
574 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
575 body->local_iseq = iseq;
577 else if (piseq) {
578 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
581 if (piseq) {
582 body->parent_iseq = piseq;
585 if (type == ISEQ_TYPE_MAIN) {
586 body->local_iseq = iseq;
590 static struct iseq_compile_data_storage *
591 new_arena(void)
593 struct iseq_compile_data_storage * new_arena =
594 (struct iseq_compile_data_storage *)
595 ALLOC_N(char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
596 offsetof(struct iseq_compile_data_storage, buff));
598 new_arena->pos = 0;
599 new_arena->next = 0;
600 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
602 return new_arena;
605 static VALUE
606 prepare_iseq_build(rb_iseq_t *iseq,
607 VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id,
608 const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type type,
609 VALUE script_lines, const rb_compile_option_t *option)
611 VALUE coverage = Qfalse;
612 VALUE err_info = Qnil;
613 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
615 if (parent && (type == ISEQ_TYPE_MAIN || type == ISEQ_TYPE_TOP))
616 err_info = Qfalse;
618 body->type = type;
619 set_relation(iseq, parent);
621 name = rb_fstring(name);
622 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
623 if (iseq != body->local_iseq) {
624 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
626 ISEQ_COVERAGE_SET(iseq, Qnil);
627 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
628 body->variable.flip_count = 0;
630 if (NIL_P(script_lines)) {
631 RB_OBJ_WRITE(iseq, &body->variable.script_lines, Qnil);
633 else {
634 RB_OBJ_WRITE(iseq, &body->variable.script_lines, rb_ractor_make_shareable(script_lines));
637 ISEQ_COMPILE_DATA_ALLOC(iseq);
638 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
639 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, Qnil);
641 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
642 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
643 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
644 ISEQ_COMPILE_DATA(iseq)->option = option;
645 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
646 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
648 if (option->coverage_enabled) {
649 VALUE coverages = rb_get_coverages();
650 if (RTEST(coverages)) {
651 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
652 if (NIL_P(coverage)) coverage = Qfalse;
655 ISEQ_COVERAGE_SET(iseq, coverage);
656 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
657 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_hidden_new(0));
659 return Qtrue;
662 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
663 static void validate_get_insn_info(const rb_iseq_t *iseq);
664 #endif
666 void
667 rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq)
669 #if VM_INSN_INFO_TABLE_IMPL == 2
670 /* create succ_index_table */
671 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
672 int size = body->insns_info.size;
673 int max_pos = body->iseq_size;
674 int *data = (int *)body->insns_info.positions;
675 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
676 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
677 #if VM_CHECK_MODE == 0
678 ruby_xfree(body->insns_info.positions);
679 body->insns_info.positions = NULL;
680 #endif
681 #endif
684 #if VM_INSN_INFO_TABLE_IMPL == 2
685 unsigned int *
686 rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body)
688 int size = body->insns_info.size;
689 int max_pos = body->iseq_size;
690 struct succ_index_table *sd = body->insns_info.succ_index_table;
691 return succ_index_table_invert(max_pos, sd, size);
693 #endif
695 void
696 rb_iseq_init_trace(rb_iseq_t *iseq)
698 iseq->aux.exec.global_trace_events = 0;
699 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
700 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
704 static VALUE
705 finish_iseq_build(rb_iseq_t *iseq)
707 struct iseq_compile_data *data = ISEQ_COMPILE_DATA(iseq);
708 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
709 VALUE err = data->err_info;
710 ISEQ_COMPILE_DATA_CLEAR(iseq);
711 compile_data_free(data);
713 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
714 validate_get_insn_info(iseq);
715 #endif
717 if (RTEST(err)) {
718 VALUE path = pathobj_path(body->location.pathobj);
719 if (err == Qtrue) err = rb_exc_new_cstr(rb_eSyntaxError, "compile error");
720 rb_funcallv(err, rb_intern("set_backtrace"), 1, &path);
721 rb_exc_raise(err);
724 RB_DEBUG_COUNTER_INC(iseq_num);
725 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
727 rb_iseq_init_trace(iseq);
728 return Qtrue;
731 static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
732 .inline_const_cache = OPT_INLINE_CONST_CACHE,
733 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
734 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
735 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
736 .operands_unification = OPT_OPERANDS_UNIFICATION,
737 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
738 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
739 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
740 .coverage_enabled = TRUE,
743 static const rb_compile_option_t COMPILE_OPTION_FALSE = {
744 .frozen_string_literal = -1, // unspecified
748 rb_iseq_opt_frozen_string_literal(void)
750 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
753 static void
754 set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt)
756 #define SET_COMPILE_OPTION(o, h, mem) \
757 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
758 if (flag == Qtrue) { (o)->mem = 1; } \
759 else if (flag == Qfalse) { (o)->mem = 0; } \
761 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
762 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
763 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
765 SET_COMPILE_OPTION(option, opt, inline_const_cache);
766 SET_COMPILE_OPTION(option, opt, peephole_optimization);
767 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
768 SET_COMPILE_OPTION(option, opt, specialized_instruction);
769 SET_COMPILE_OPTION(option, opt, operands_unification);
770 SET_COMPILE_OPTION(option, opt, instructions_unification);
771 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
772 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
773 SET_COMPILE_OPTION(option, opt, coverage_enabled);
774 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
775 #undef SET_COMPILE_OPTION
776 #undef SET_COMPILE_OPTION_NUM
779 static rb_compile_option_t *
780 set_compile_option_from_ast(rb_compile_option_t *option, const rb_ast_body_t *ast)
782 #define SET_COMPILE_OPTION(o, a, mem) \
783 ((a)->mem < 0 ? 0 : ((o)->mem = (a)->mem > 0))
784 SET_COMPILE_OPTION(option, ast, coverage_enabled);
785 #undef SET_COMPILE_OPTION
786 if (ast->frozen_string_literal >= 0) {
787 option->frozen_string_literal = ast->frozen_string_literal;
789 return option;
792 static void
793 make_compile_option(rb_compile_option_t *option, VALUE opt)
795 if (NIL_P(opt)) {
796 *option = COMPILE_OPTION_DEFAULT;
798 else if (opt == Qfalse) {
799 *option = COMPILE_OPTION_FALSE;
801 else if (opt == Qtrue) {
802 int i;
803 for (i = 0; i < (int)(sizeof(rb_compile_option_t) / sizeof(int)); ++i)
804 ((int *)option)[i] = 1;
806 else if (RB_TYPE_P(opt, T_HASH)) {
807 *option = COMPILE_OPTION_DEFAULT;
808 set_compile_option_from_hash(option, opt);
810 else {
811 rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
815 static VALUE
816 make_compile_option_value(rb_compile_option_t *option)
818 VALUE opt = rb_hash_new_with_size(11);
819 #define SET_COMPILE_OPTION(o, h, mem) \
820 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
821 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
822 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
824 SET_COMPILE_OPTION(option, opt, inline_const_cache);
825 SET_COMPILE_OPTION(option, opt, peephole_optimization);
826 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
827 SET_COMPILE_OPTION(option, opt, specialized_instruction);
828 SET_COMPILE_OPTION(option, opt, operands_unification);
829 SET_COMPILE_OPTION(option, opt, instructions_unification);
830 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
831 SET_COMPILE_OPTION(option, opt, coverage_enabled);
832 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
834 #undef SET_COMPILE_OPTION
835 #undef SET_COMPILE_OPTION_NUM
836 VALUE frozen_string_literal = option->frozen_string_literal == -1 ? Qnil : RBOOL(option->frozen_string_literal);
837 rb_hash_aset(opt, ID2SYM(rb_intern("frozen_string_literal")), frozen_string_literal);
838 return opt;
841 rb_iseq_t *
842 rb_iseq_new(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath,
843 const rb_iseq_t *parent, enum rb_iseq_type type)
845 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
846 0, type, &COMPILE_OPTION_DEFAULT,
847 Qnil);
850 static int
851 ast_line_count(const VALUE ast_value)
853 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
854 return ast->body.line_count;
857 static VALUE
858 iseq_setup_coverage(VALUE coverages, VALUE path, int line_count)
860 if (line_count >= 0) {
861 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
863 VALUE coverage = rb_default_coverage(len);
864 rb_hash_aset(coverages, path, coverage);
866 return coverage;
869 return Qnil;
872 static inline void
873 iseq_new_setup_coverage(VALUE path, int line_count)
875 VALUE coverages = rb_get_coverages();
877 if (RTEST(coverages)) {
878 iseq_setup_coverage(coverages, path, line_count);
882 rb_iseq_t *
883 rb_iseq_new_top(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent)
885 iseq_new_setup_coverage(path, ast_line_count(ast_value));
887 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
888 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
889 Qnil);
893 * The main entry-point into the prism compiler when a file is required.
895 rb_iseq_t *
896 pm_iseq_new_top(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent)
898 iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1));
900 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
901 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
904 rb_iseq_t *
905 rb_iseq_new_main(const VALUE ast_value, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt)
907 iseq_new_setup_coverage(path, ast_line_count(ast_value));
909 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit("<main>"),
910 path, realpath, 0,
911 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
912 Qnil);
916 * The main entry-point into the prism compiler when a file is executed as the
917 * main file in the program.
919 rb_iseq_t *
920 pm_iseq_new_main(pm_scope_node_t *node, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt)
922 iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1));
924 return pm_iseq_new_with_opt(node, rb_fstring_lit("<main>"),
925 path, realpath, 0,
926 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
929 rb_iseq_t *
930 rb_iseq_new_eval(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth)
932 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
933 VALUE coverages = rb_get_coverages();
934 if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
935 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
939 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
940 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT,
941 Qnil);
944 rb_iseq_t *
945 pm_iseq_new_eval(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath,
946 int first_lineno, const rb_iseq_t *parent, int isolated_depth)
948 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
949 VALUE coverages = rb_get_coverages();
950 if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
951 iseq_setup_coverage(coverages, path, ((int) (node->parser->newline_list.size - 1)) + first_lineno - 1);
955 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
956 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
959 static inline rb_iseq_t *
960 iseq_translate(rb_iseq_t *iseq)
962 if (rb_respond_to(rb_cISeq, rb_intern("translate"))) {
963 VALUE v1 = iseqw_new(iseq);
964 VALUE v2 = rb_funcall(rb_cISeq, rb_intern("translate"), 1, v1);
965 if (v1 != v2 && CLASS_OF(v2) == rb_cISeq) {
966 iseq = (rb_iseq_t *)iseqw_check(v2);
970 return iseq;
973 rb_iseq_t *
974 rb_iseq_new_with_opt(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath,
975 int first_lineno, const rb_iseq_t *parent, int isolated_depth,
976 enum rb_iseq_type type, const rb_compile_option_t *option,
977 VALUE script_lines)
979 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
980 rb_ast_body_t *body = ast ? &ast->body : NULL;
981 const NODE *node = body ? body->root : 0;
982 /* TODO: argument check */
983 rb_iseq_t *iseq = iseq_alloc();
984 rb_compile_option_t new_opt;
986 if (!option) option = &COMPILE_OPTION_DEFAULT;
987 if (body) {
988 new_opt = *option;
989 option = set_compile_option_from_ast(&new_opt, body);
992 if (!NIL_P(script_lines)) {
993 // noop
995 else if (body && body->script_lines) {
996 script_lines = rb_parser_build_script_lines_from(body->script_lines);
998 else if (parent) {
999 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1002 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
1003 parent, isolated_depth, type, script_lines, option);
1005 rb_iseq_compile_node(iseq, node);
1006 finish_iseq_build(iseq);
1008 return iseq_translate(iseq);
1012 * This is a step in the prism compiler that is called once all of the various
1013 * options have been established. It is called from one of the pm_iseq_new_*
1014 * functions or from the RubyVM::InstructionSequence APIs. It is responsible for
1015 * allocating the instruction sequence, calling into the compiler, and returning
1016 * the built instruction sequence.
1018 * Importantly, this is also the function where the compiler is re-entered to
1019 * compile child instruction sequences. A child instruction sequence is always
1020 * compiled using a scope node, which is why we cast it explicitly to that here
1021 * in the parameters (as opposed to accepting a generic pm_node_t *).
1023 rb_iseq_t *
1024 pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath,
1025 int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1026 enum rb_iseq_type type, const rb_compile_option_t *option)
1028 rb_iseq_t *iseq = iseq_alloc();
1029 ISEQ_BODY(iseq)->prism = true;
1030 ISEQ_BODY(iseq)->param.flags.use_block = true; // unused block warning is not supported yet
1032 if (!option) option = &COMPILE_OPTION_DEFAULT;
1034 pm_location_t *location = &node->base.location;
1035 int32_t start_line = node->parser->start_line;
1037 pm_line_column_t start = pm_newline_list_line_column(&node->parser->newline_list, location->start, start_line);
1038 pm_line_column_t end = pm_newline_list_line_column(&node->parser->newline_list, location->end, start_line);
1040 rb_code_location_t code_location = (rb_code_location_t) {
1041 .beg_pos = { .lineno = (int) start.line, .column = (int) start.column },
1042 .end_pos = { .lineno = (int) end.line, .column = (int) end.column }
1045 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, -1,
1046 parent, isolated_depth, type, Qnil, option);
1048 pm_iseq_compile_node(iseq, node);
1049 finish_iseq_build(iseq);
1051 return iseq_translate(iseq);
1054 rb_iseq_t *
1055 rb_iseq_new_with_callback(
1056 const struct rb_iseq_new_with_callback_callback_func * ifunc,
1057 VALUE name, VALUE path, VALUE realpath,
1058 int first_lineno, const rb_iseq_t *parent,
1059 enum rb_iseq_type type, const rb_compile_option_t *option)
1061 /* TODO: argument check */
1062 rb_iseq_t *iseq = iseq_alloc();
1064 if (!option) option = &COMPILE_OPTION_DEFAULT;
1065 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0, type, Qnil, option);
1067 rb_iseq_compile_callback(iseq, ifunc);
1068 finish_iseq_build(iseq);
1070 return iseq;
1073 const rb_iseq_t *
1074 rb_iseq_load_iseq(VALUE fname)
1076 VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname);
1078 if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) {
1079 return iseqw_check(iseqv);
1082 return NULL;
1085 #define CHECK_ARRAY(v) rb_to_array_type(v)
1086 #define CHECK_HASH(v) rb_to_hash_type(v)
1087 #define CHECK_STRING(v) rb_str_to_str(v)
1088 #define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1089 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
1091 static enum rb_iseq_type
1092 iseq_type_from_sym(VALUE type)
1094 const ID id_top = rb_intern("top");
1095 const ID id_method = rb_intern("method");
1096 const ID id_block = rb_intern("block");
1097 const ID id_class = rb_intern("class");
1098 const ID id_rescue = rb_intern("rescue");
1099 const ID id_ensure = rb_intern("ensure");
1100 const ID id_eval = rb_intern("eval");
1101 const ID id_main = rb_intern("main");
1102 const ID id_plain = rb_intern("plain");
1103 /* ensure all symbols are static or pinned down before
1104 * conversion */
1105 const ID typeid = rb_check_id(&type);
1106 if (typeid == id_top) return ISEQ_TYPE_TOP;
1107 if (typeid == id_method) return ISEQ_TYPE_METHOD;
1108 if (typeid == id_block) return ISEQ_TYPE_BLOCK;
1109 if (typeid == id_class) return ISEQ_TYPE_CLASS;
1110 if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
1111 if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
1112 if (typeid == id_eval) return ISEQ_TYPE_EVAL;
1113 if (typeid == id_main) return ISEQ_TYPE_MAIN;
1114 if (typeid == id_plain) return ISEQ_TYPE_PLAIN;
1115 return (enum rb_iseq_type)-1;
1118 static VALUE
1119 iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
1121 rb_iseq_t *iseq = iseq_alloc();
1123 VALUE magic, version1, version2, format_type, misc;
1124 VALUE name, path, realpath, code_location, node_id;
1125 VALUE type, body, locals, params, exception;
1127 st_data_t iseq_type;
1128 rb_compile_option_t option;
1129 int i = 0;
1130 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1132 /* [magic, major_version, minor_version, format_type, misc,
1133 * label, path, first_lineno,
1134 * type, locals, args, exception_table, body]
1137 data = CHECK_ARRAY(data);
1139 magic = CHECK_STRING(rb_ary_entry(data, i++));
1140 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1141 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1142 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1143 misc = CHECK_HASH(rb_ary_entry(data, i++));
1144 ((void)magic, (void)version1, (void)version2, (void)format_type);
1146 name = CHECK_STRING(rb_ary_entry(data, i++));
1147 path = CHECK_STRING(rb_ary_entry(data, i++));
1148 realpath = rb_ary_entry(data, i++);
1149 realpath = NIL_P(realpath) ? Qnil : CHECK_STRING(realpath);
1150 int first_lineno = RB_NUM2INT(rb_ary_entry(data, i++));
1152 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1153 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1154 params = CHECK_HASH(rb_ary_entry(data, i++));
1155 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1156 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1158 ISEQ_BODY(iseq)->local_iseq = iseq;
1160 iseq_type = iseq_type_from_sym(type);
1161 if (iseq_type == (enum rb_iseq_type)-1) {
1162 rb_raise(rb_eTypeError, "unsupported type: :%"PRIsVALUE, rb_sym2str(type));
1165 node_id = rb_hash_aref(misc, ID2SYM(rb_intern("node_id")));
1167 code_location = rb_hash_aref(misc, ID2SYM(rb_intern("code_location")));
1168 if (RB_TYPE_P(code_location, T_ARRAY) && RARRAY_LEN(code_location) == 4) {
1169 tmp_loc.beg_pos.lineno = NUM2INT(rb_ary_entry(code_location, 0));
1170 tmp_loc.beg_pos.column = NUM2INT(rb_ary_entry(code_location, 1));
1171 tmp_loc.end_pos.lineno = NUM2INT(rb_ary_entry(code_location, 2));
1172 tmp_loc.end_pos.column = NUM2INT(rb_ary_entry(code_location, 3));
1175 if (SYM2ID(rb_hash_aref(misc, ID2SYM(rb_intern("parser")))) == rb_intern("prism")) {
1176 ISEQ_BODY(iseq)->prism = true;
1179 make_compile_option(&option, opt);
1180 option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
1181 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc, NUM2INT(node_id),
1182 parent, 0, (enum rb_iseq_type)iseq_type, Qnil, &option);
1184 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1186 finish_iseq_build(iseq);
1188 return iseqw_new(iseq);
1192 * :nodoc:
1194 static VALUE
1195 iseq_s_load(int argc, VALUE *argv, VALUE self)
1197 VALUE data, opt=Qnil;
1198 rb_scan_args(argc, argv, "11", &data, &opt);
1199 return iseq_load(data, NULL, opt);
1202 VALUE
1203 rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
1205 return iseq_load(data, RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1208 static rb_iseq_t *
1209 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1211 rb_iseq_t *iseq = NULL;
1212 rb_compile_option_t option;
1213 #if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1214 # define INITIALIZED volatile /* suppress warnings by gcc 4.8 */
1215 #else
1216 # define INITIALIZED /* volatile */
1217 #endif
1218 VALUE (*parse)(VALUE vparser, VALUE fname, VALUE file, int start);
1219 int ln;
1220 VALUE INITIALIZED ast_value;
1221 rb_ast_t *ast;
1222 VALUE name = rb_fstring_lit("<compiled>");
1224 /* safe results first */
1225 make_compile_option(&option, opt);
1226 ln = NUM2INT(line);
1227 StringValueCStr(file);
1228 if (RB_TYPE_P(src, T_FILE)) {
1229 parse = rb_parser_compile_file_path;
1231 else {
1232 parse = rb_parser_compile_string_path;
1233 StringValue(src);
1236 const VALUE parser = rb_parser_new();
1237 const rb_iseq_t *outer_scope = rb_iseq_new(Qnil, name, name, Qnil, 0, ISEQ_TYPE_TOP);
1238 VALUE outer_scope_v = (VALUE)outer_scope;
1239 rb_parser_set_context(parser, outer_scope, FALSE);
1240 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1241 RB_GC_GUARD(outer_scope_v);
1242 ast_value = (*parse)(parser, file, src, ln);
1245 ast = rb_ruby_ast_data_get(ast_value);
1247 if (!ast || !ast->body.root) {
1248 rb_ast_dispose(ast);
1249 rb_exc_raise(GET_EC()->errinfo);
1251 else {
1252 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1253 NULL, 0, ISEQ_TYPE_TOP, &option,
1254 Qnil);
1255 rb_ast_dispose(ast);
1258 return iseq;
1261 static rb_iseq_t *
1262 pm_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1264 rb_iseq_t *iseq = NULL;
1265 rb_compile_option_t option;
1266 int ln;
1267 VALUE name = rb_fstring_lit("<compiled>");
1269 /* safe results first */
1270 make_compile_option(&option, opt);
1271 ln = NUM2INT(line);
1272 StringValueCStr(file);
1274 pm_parse_result_t result = { 0 };
1275 pm_options_line_set(&result.options, NUM2INT(line));
1277 switch (option.frozen_string_literal) {
1278 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1279 break;
1280 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1281 pm_options_frozen_string_literal_set(&result.options, false);
1282 break;
1283 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1284 pm_options_frozen_string_literal_set(&result.options, true);
1285 break;
1286 default:
1287 rb_bug("pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1288 break;
1291 VALUE error;
1292 if (RB_TYPE_P(src, T_FILE)) {
1293 VALUE filepath = rb_io_path(src);
1294 error = pm_load_parse_file(&result, filepath);
1295 RB_GC_GUARD(filepath);
1297 else {
1298 src = StringValue(src);
1299 error = pm_parse_string(&result, src, file);
1302 if (error == Qnil) {
1303 iseq = pm_iseq_new_with_opt(&result.node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option);
1304 pm_parse_result_free(&result);
1306 else {
1307 pm_parse_result_free(&result);
1308 rb_exc_raise(error);
1311 return iseq;
1314 VALUE
1315 rb_iseq_path(const rb_iseq_t *iseq)
1317 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1320 VALUE
1321 rb_iseq_realpath(const rb_iseq_t *iseq)
1323 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1326 VALUE
1327 rb_iseq_absolute_path(const rb_iseq_t *iseq)
1329 return rb_iseq_realpath(iseq);
1333 rb_iseq_from_eval_p(const rb_iseq_t *iseq)
1335 return NIL_P(rb_iseq_realpath(iseq));
1338 VALUE
1339 rb_iseq_label(const rb_iseq_t *iseq)
1341 return ISEQ_BODY(iseq)->location.label;
1344 VALUE
1345 rb_iseq_base_label(const rb_iseq_t *iseq)
1347 return ISEQ_BODY(iseq)->location.base_label;
1350 VALUE
1351 rb_iseq_first_lineno(const rb_iseq_t *iseq)
1353 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1356 VALUE
1357 rb_iseq_method_name(const rb_iseq_t *iseq)
1359 struct rb_iseq_constant_body *const body = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq);
1361 if (body->type == ISEQ_TYPE_METHOD) {
1362 return body->location.base_label;
1364 else {
1365 return Qnil;
1369 void
1370 rb_iseq_code_location(const rb_iseq_t *iseq, int *beg_pos_lineno, int *beg_pos_column, int *end_pos_lineno, int *end_pos_column)
1372 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1373 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1374 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1375 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1376 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1379 static ID iseq_type_id(enum rb_iseq_type type);
1381 VALUE
1382 rb_iseq_type(const rb_iseq_t *iseq)
1384 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->type));
1387 VALUE
1388 rb_iseq_coverage(const rb_iseq_t *iseq)
1390 return ISEQ_COVERAGE(iseq);
1393 static int
1394 remove_coverage_i(void *vstart, void *vend, size_t stride, void *data)
1396 VALUE v = (VALUE)vstart;
1397 for (; v != (VALUE)vend; v += stride) {
1398 void *ptr = asan_poisoned_object_p(v);
1399 asan_unpoison_object(v, false);
1401 if (rb_obj_is_iseq(v)) {
1402 rb_iseq_t *iseq = (rb_iseq_t *)v;
1403 ISEQ_COVERAGE_SET(iseq, Qnil);
1406 asan_poison_object_if(ptr, v);
1408 return 0;
1411 void
1412 rb_iseq_remove_coverage_all(void)
1414 rb_objspace_each_objects(remove_coverage_i, NULL);
1417 /* define wrapper class methods (RubyVM::InstructionSequence) */
1419 static void
1420 iseqw_mark(void *ptr)
1422 rb_gc_mark_movable(*(VALUE *)ptr);
1425 static size_t
1426 iseqw_memsize(const void *ptr)
1428 return rb_iseq_memsize(*(const rb_iseq_t **)ptr);
1431 static void
1432 iseqw_ref_update(void *ptr)
1434 VALUE *vptr = ptr;
1435 *vptr = rb_gc_location(*vptr);
1438 static const rb_data_type_t iseqw_data_type = {
1439 "T_IMEMO/iseq",
1441 iseqw_mark,
1442 RUBY_TYPED_DEFAULT_FREE,
1443 iseqw_memsize,
1444 iseqw_ref_update,
1446 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1449 static VALUE
1450 iseqw_new(const rb_iseq_t *iseq)
1452 if (iseq->wrapper) {
1453 if (*(const rb_iseq_t **)rb_check_typeddata(iseq->wrapper, &iseqw_data_type) != iseq) {
1454 rb_raise(rb_eTypeError, "wrong iseq wrapper: %" PRIsVALUE " for %p",
1455 iseq->wrapper, (void *)iseq);
1457 return iseq->wrapper;
1459 else {
1460 rb_iseq_t **ptr;
1461 VALUE obj = TypedData_Make_Struct(rb_cISeq, rb_iseq_t *, &iseqw_data_type, ptr);
1462 RB_OBJ_WRITE(obj, ptr, iseq);
1464 /* cache a wrapper object */
1465 RB_OBJ_WRITE((VALUE)iseq, &iseq->wrapper, obj);
1466 RB_OBJ_FREEZE((VALUE)iseq);
1468 return obj;
1472 VALUE
1473 rb_iseqw_new(const rb_iseq_t *iseq)
1475 return iseqw_new(iseq);
1479 * Accept the options given to InstructionSequence.compile and
1480 * InstructionSequence.compile_prism and share the logic for creating the
1481 * instruction sequence.
1483 static VALUE
1484 iseqw_s_compile_parser(int argc, VALUE *argv, VALUE self, bool prism)
1486 VALUE src, file = Qnil, path = Qnil, line = Qnil, opt = Qnil;
1487 int i;
1489 i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt);
1490 if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5);
1491 switch (i) {
1492 case 5: opt = argv[--i];
1493 case 4: line = argv[--i];
1494 case 3: path = argv[--i];
1495 case 2: file = argv[--i];
1498 if (NIL_P(file)) file = rb_fstring_lit("<compiled>");
1499 if (NIL_P(path)) path = file;
1500 if (NIL_P(line)) line = INT2FIX(1);
1502 Check_Type(path, T_STRING);
1503 Check_Type(file, T_STRING);
1505 rb_iseq_t *iseq;
1506 if (prism) {
1507 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1509 else {
1510 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1513 return iseqw_new(iseq);
1517 * call-seq:
1518 * InstructionSequence.compile(source[, file[, path[, line[, options]]]]) -> iseq
1519 * InstructionSequence.new(source[, file[, path[, line[, options]]]]) -> iseq
1521 * Takes +source+, which can be a string of Ruby code, or an open +File+ object.
1522 * that contains Ruby source code.
1524 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1525 * real path and first line number of the ruby code in +source+ which are
1526 * metadata attached to the returned +iseq+.
1528 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1529 * +require_relative+ base. It is recommended these should be the same full
1530 * path.
1532 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1533 * modify the default behavior of the Ruby iseq compiler.
1535 * For details regarding valid compile options see ::compile_option=.
1537 * RubyVM::InstructionSequence.compile("a = 1 + 2")
1538 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1540 * path = "test.rb"
1541 * RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
1542 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1544 * file = File.open("test.rb")
1545 * RubyVM::InstructionSequence.compile(file)
1546 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
1548 * path = File.expand_path("test.rb")
1549 * RubyVM::InstructionSequence.compile(File.read(path), path, path)
1550 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1553 static VALUE
1554 iseqw_s_compile(int argc, VALUE *argv, VALUE self)
1556 return iseqw_s_compile_parser(argc, argv, self, *rb_ruby_prism_ptr());
1560 * call-seq:
1561 * InstructionSequence.compile_prism(source[, file[, path[, line[, options]]]]) -> iseq
1563 * Takes +source+, which can be a string of Ruby code, or an open +File+ object.
1564 * that contains Ruby source code. It parses and compiles using prism.
1566 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1567 * real path and first line number of the ruby code in +source+ which are
1568 * metadata attached to the returned +iseq+.
1570 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1571 * +require_relative+ base. It is recommended these should be the same full
1572 * path.
1574 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1575 * modify the default behavior of the Ruby iseq compiler.
1577 * For details regarding valid compile options see ::compile_option=.
1579 * RubyVM::InstructionSequence.compile("a = 1 + 2")
1580 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1582 * path = "test.rb"
1583 * RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
1584 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1586 * file = File.open("test.rb")
1587 * RubyVM::InstructionSequence.compile(file)
1588 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
1590 * path = File.expand_path("test.rb")
1591 * RubyVM::InstructionSequence.compile(File.read(path), path, path)
1592 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1595 static VALUE
1596 iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self)
1598 return iseqw_s_compile_parser(argc, argv, self, true);
1602 * call-seq:
1603 * InstructionSequence.compile_file(file[, options]) -> iseq
1605 * Takes +file+, a String with the location of a Ruby source file, reads,
1606 * parses and compiles the file, and returns +iseq+, the compiled
1607 * InstructionSequence with source location metadata set.
1609 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1610 * modify the default behavior of the Ruby iseq compiler.
1612 * For details regarding valid compile options see ::compile_option=.
1614 * # /tmp/hello.rb
1615 * puts "Hello, world!"
1617 * # elsewhere
1618 * RubyVM::InstructionSequence.compile_file("/tmp/hello.rb")
1619 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1621 static VALUE
1622 iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
1624 VALUE file, opt = Qnil;
1625 VALUE parser, f, exc = Qnil, ret;
1626 rb_ast_t *ast;
1627 VALUE ast_value;
1628 rb_compile_option_t option;
1629 int i;
1631 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1632 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1633 switch (i) {
1634 case 2: opt = argv[--i];
1636 FilePathValue(file);
1637 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1639 f = rb_file_open_str(file, "r");
1641 rb_execution_context_t *ec = GET_EC();
1642 VALUE v = rb_vm_push_frame_fname(ec, file);
1644 parser = rb_parser_new();
1645 rb_parser_set_context(parser, NULL, FALSE);
1646 ast_value = rb_parser_load_file(parser, file);
1647 ast = rb_ruby_ast_data_get(ast_value);
1648 if (!ast->body.root) exc = GET_EC()->errinfo;
1650 rb_io_close(f);
1651 if (!ast->body.root) {
1652 rb_ast_dispose(ast);
1653 rb_exc_raise(exc);
1656 make_compile_option(&option, opt);
1658 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit("<main>"),
1659 file,
1660 rb_realpath_internal(Qnil, file, 1),
1661 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1662 Qnil));
1663 rb_ast_dispose(ast);
1665 rb_vm_pop_frame(ec);
1666 RB_GC_GUARD(v);
1667 return ret;
1671 * call-seq:
1672 * InstructionSequence.compile_file_prism(file[, options]) -> iseq
1674 * Takes +file+, a String with the location of a Ruby source file, reads,
1675 * parses and compiles the file, and returns +iseq+, the compiled
1676 * InstructionSequence with source location metadata set. It parses and
1677 * compiles using prism.
1679 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1680 * modify the default behavior of the Ruby iseq compiler.
1682 * For details regarding valid compile options see ::compile_option=.
1684 * # /tmp/hello.rb
1685 * puts "Hello, world!"
1687 * # elsewhere
1688 * RubyVM::InstructionSequence.compile_file_prism("/tmp/hello.rb")
1689 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1691 static VALUE
1692 iseqw_s_compile_file_prism(int argc, VALUE *argv, VALUE self)
1694 VALUE file, opt = Qnil, ret;
1695 rb_compile_option_t option;
1696 int i;
1698 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1699 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1700 switch (i) {
1701 case 2: opt = argv[--i];
1703 FilePathValue(file);
1704 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1706 rb_execution_context_t *ec = GET_EC();
1707 VALUE v = rb_vm_push_frame_fname(ec, file);
1709 pm_parse_result_t result = { 0 };
1710 result.options.line = 1;
1712 VALUE error = pm_load_parse_file(&result, file);
1714 if (error == Qnil) {
1715 make_compile_option(&option, opt);
1717 ret = iseqw_new(pm_iseq_new_with_opt(&result.node, rb_fstring_lit("<main>"),
1718 file,
1719 rb_realpath_internal(Qnil, file, 1),
1720 1, NULL, 0, ISEQ_TYPE_TOP, &option));
1721 pm_parse_result_free(&result);
1722 rb_vm_pop_frame(ec);
1723 RB_GC_GUARD(v);
1724 return ret;
1725 } else {
1726 pm_parse_result_free(&result);
1727 rb_vm_pop_frame(ec);
1728 RB_GC_GUARD(v);
1729 rb_exc_raise(error);
1734 * call-seq:
1735 * InstructionSequence.compile_option = options
1737 * Sets the default values for various optimizations in the Ruby iseq
1738 * compiler.
1740 * Possible values for +options+ include +true+, which enables all options,
1741 * +false+ which disables all options, and +nil+ which leaves all options
1742 * unchanged.
1744 * You can also pass a +Hash+ of +options+ that you want to change, any
1745 * options not present in the hash will be left unchanged.
1747 * Possible option names (which are keys in +options+) which can be set to
1748 * +true+ or +false+ include:
1750 * * +:inline_const_cache+
1751 * * +:instructions_unification+
1752 * * +:operands_unification+
1753 * * +:peephole_optimization+
1754 * * +:specialized_instruction+
1755 * * +:tailcall_optimization+
1757 * Additionally, +:debug_level+ can be set to an integer.
1759 * These default options can be overwritten for a single run of the iseq
1760 * compiler by passing any of the above values as the +options+ parameter to
1761 * ::new, ::compile and ::compile_file.
1763 static VALUE
1764 iseqw_s_compile_option_set(VALUE self, VALUE opt)
1766 rb_compile_option_t option;
1767 make_compile_option(&option, opt);
1768 COMPILE_OPTION_DEFAULT = option;
1769 return opt;
1773 * call-seq:
1774 * InstructionSequence.compile_option -> options
1776 * Returns a hash of default options used by the Ruby iseq compiler.
1778 * For details, see InstructionSequence.compile_option=.
1780 static VALUE
1781 iseqw_s_compile_option_get(VALUE self)
1783 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1786 static const rb_iseq_t *
1787 iseqw_check(VALUE iseqw)
1789 rb_iseq_t **iseq_ptr;
1790 TypedData_Get_Struct(iseqw, rb_iseq_t *, &iseqw_data_type, iseq_ptr);
1791 rb_iseq_t *iseq = *iseq_ptr;
1793 if (!ISEQ_BODY(iseq)) {
1794 rb_ibf_load_iseq_complete(iseq);
1797 if (!ISEQ_BODY(iseq)->location.label) {
1798 rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
1800 return iseq;
1803 const rb_iseq_t *
1804 rb_iseqw_to_iseq(VALUE iseqw)
1806 return iseqw_check(iseqw);
1810 * call-seq:
1811 * iseq.eval -> obj
1813 * Evaluates the instruction sequence and returns the result.
1815 * RubyVM::InstructionSequence.compile("1 + 2").eval #=> 3
1817 static VALUE
1818 iseqw_eval(VALUE self)
1820 return rb_iseq_eval(iseqw_check(self));
1824 * Returns a human-readable string representation of this instruction
1825 * sequence, including the #label and #path.
1827 static VALUE
1828 iseqw_inspect(VALUE self)
1830 const rb_iseq_t *iseq = iseqw_check(self);
1831 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1832 VALUE klass = rb_class_name(rb_obj_class(self));
1834 if (!body->location.label) {
1835 return rb_sprintf("#<%"PRIsVALUE": uninitialized>", klass);
1837 else {
1838 return rb_sprintf("<%"PRIsVALUE":%"PRIsVALUE"@%"PRIsVALUE":%d>",
1839 klass,
1840 body->location.label, rb_iseq_path(iseq),
1841 FIX2INT(rb_iseq_first_lineno(iseq)));
1846 * Returns the path of this instruction sequence.
1848 * <code><compiled></code> if the iseq was evaluated from a string.
1850 * For example, using irb:
1852 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1853 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1854 * iseq.path
1855 * #=> "<compiled>"
1857 * Using ::compile_file:
1859 * # /tmp/method.rb
1860 * def hello
1861 * puts "hello, world"
1862 * end
1864 * # in irb
1865 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1866 * > iseq.path #=> /tmp/method.rb
1868 static VALUE
1869 iseqw_path(VALUE self)
1871 return rb_iseq_path(iseqw_check(self));
1875 * Returns the absolute path of this instruction sequence.
1877 * +nil+ if the iseq was evaluated from a string.
1879 * For example, using ::compile_file:
1881 * # /tmp/method.rb
1882 * def hello
1883 * puts "hello, world"
1884 * end
1886 * # in irb
1887 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1888 * > iseq.absolute_path #=> /tmp/method.rb
1890 static VALUE
1891 iseqw_absolute_path(VALUE self)
1893 return rb_iseq_realpath(iseqw_check(self));
1896 /* Returns the label of this instruction sequence.
1898 * <code><main></code> if it's at the top level, <code><compiled></code> if it
1899 * was evaluated from a string.
1901 * For example, using irb:
1903 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1904 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1905 * iseq.label
1906 * #=> "<compiled>"
1908 * Using ::compile_file:
1910 * # /tmp/method.rb
1911 * def hello
1912 * puts "hello, world"
1913 * end
1915 * # in irb
1916 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1917 * > iseq.label #=> <main>
1919 static VALUE
1920 iseqw_label(VALUE self)
1922 return rb_iseq_label(iseqw_check(self));
1925 /* Returns the base label of this instruction sequence.
1927 * For example, using irb:
1929 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1930 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1931 * iseq.base_label
1932 * #=> "<compiled>"
1934 * Using ::compile_file:
1936 * # /tmp/method.rb
1937 * def hello
1938 * puts "hello, world"
1939 * end
1941 * # in irb
1942 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1943 * > iseq.base_label #=> <main>
1945 static VALUE
1946 iseqw_base_label(VALUE self)
1948 return rb_iseq_base_label(iseqw_check(self));
1951 /* Returns the number of the first source line where the instruction sequence
1952 * was loaded from.
1954 * For example, using irb:
1956 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1957 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1958 * iseq.first_lineno
1959 * #=> 1
1961 static VALUE
1962 iseqw_first_lineno(VALUE self)
1964 return rb_iseq_first_lineno(iseqw_check(self));
1967 static VALUE iseq_data_to_ary(const rb_iseq_t *iseq);
1970 * call-seq:
1971 * iseq.to_a -> ary
1973 * Returns an Array with 14 elements representing the instruction sequence
1974 * with the following data:
1976 * [magic]
1977 * A string identifying the data format. <b>Always
1978 * +YARVInstructionSequence/SimpleDataFormat+.</b>
1980 * [major_version]
1981 * The major version of the instruction sequence.
1983 * [minor_version]
1984 * The minor version of the instruction sequence.
1986 * [format_type]
1987 * A number identifying the data format. <b>Always 1</b>.
1989 * [misc]
1990 * A hash containing:
1992 * [+:arg_size+]
1993 * the total number of arguments taken by the method or the block (0 if
1994 * _iseq_ doesn't represent a method or block)
1995 * [+:local_size+]
1996 * the number of local variables + 1
1997 * [+:stack_max+]
1998 * used in calculating the stack depth at which a SystemStackError is
1999 * thrown.
2001 * [#label]
2002 * The name of the context (block, method, class, module, etc.) that this
2003 * instruction sequence belongs to.
2005 * <code><main></code> if it's at the top level, <code><compiled></code> if
2006 * it was evaluated from a string.
2008 * [#path]
2009 * The relative path to the Ruby file where the instruction sequence was
2010 * loaded from.
2012 * <code><compiled></code> if the iseq was evaluated from a string.
2014 * [#absolute_path]
2015 * The absolute path to the Ruby file where the instruction sequence was
2016 * loaded from.
2018 * +nil+ if the iseq was evaluated from a string.
2020 * [#first_lineno]
2021 * The number of the first source line where the instruction sequence was
2022 * loaded from.
2024 * [type]
2025 * The type of the instruction sequence.
2027 * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
2028 * +:ensure+, +:eval+, +:main+, and +plain+.
2030 * [locals]
2031 * An array containing the names of all arguments and local variables as
2032 * symbols.
2034 * [params]
2035 * An Hash object containing parameter information.
2037 * More info about these values can be found in +vm_core.h+.
2039 * [catch_table]
2040 * A list of exceptions and control flow operators (rescue, next, redo,
2041 * break, etc.).
2043 * [bytecode]
2044 * An array of arrays containing the instruction names and operands that
2045 * make up the body of the instruction sequence.
2047 * Note that this format is MRI specific and version dependent.
2050 static VALUE
2051 iseqw_to_a(VALUE self)
2053 const rb_iseq_t *iseq = iseqw_check(self);
2054 return iseq_data_to_ary(iseq);
2057 #if VM_INSN_INFO_TABLE_IMPL == 1 /* binary search */
2058 static const struct iseq_insn_info_entry *
2059 get_insn_info_binary_search(const rb_iseq_t *iseq, size_t pos)
2061 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2062 size_t size = body->insns_info.size;
2063 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2064 const unsigned int *positions = body->insns_info.positions;
2065 const int debug = 0;
2067 if (debug) {
2068 printf("size: %"PRIuSIZE"\n", size);
2069 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2070 (size_t)0, positions[0], insns_info[0].line_no, pos);
2073 if (size == 0) {
2074 return NULL;
2076 else if (size == 1) {
2077 return &insns_info[0];
2079 else {
2080 size_t l = 1, r = size - 1;
2081 while (l <= r) {
2082 size_t m = l + (r - l) / 2;
2083 if (positions[m] == pos) {
2084 return &insns_info[m];
2086 if (positions[m] < pos) {
2087 l = m + 1;
2089 else {
2090 r = m - 1;
2093 if (l >= size) {
2094 return &insns_info[size-1];
2096 if (positions[l] > pos) {
2097 return &insns_info[l-1];
2099 return &insns_info[l];
2103 static const struct iseq_insn_info_entry *
2104 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2106 return get_insn_info_binary_search(iseq, pos);
2108 #endif
2110 #if VM_INSN_INFO_TABLE_IMPL == 2 /* succinct bitvector */
2111 static const struct iseq_insn_info_entry *
2112 get_insn_info_succinct_bitvector(const rb_iseq_t *iseq, size_t pos)
2114 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2115 size_t size = body->insns_info.size;
2116 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2117 const int debug = 0;
2119 if (debug) {
2120 #if VM_CHECK_MODE > 0
2121 const unsigned int *positions = body->insns_info.positions;
2122 printf("size: %"PRIuSIZE"\n", size);
2123 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2124 (size_t)0, positions[0], insns_info[0].line_no, pos);
2125 #else
2126 printf("size: %"PRIuSIZE"\n", size);
2127 printf("insns_info[%"PRIuSIZE"]: line: %d, pos: %"PRIuSIZE"\n",
2128 (size_t)0, insns_info[0].line_no, pos);
2129 #endif
2132 if (size == 0) {
2133 return NULL;
2135 else if (size == 1) {
2136 return &insns_info[0];
2138 else {
2139 int index;
2140 VM_ASSERT(body->insns_info.succ_index_table != NULL);
2141 index = succ_index_lookup(body->insns_info.succ_index_table, (int)pos);
2142 return &insns_info[index-1];
2146 static const struct iseq_insn_info_entry *
2147 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2149 return get_insn_info_succinct_bitvector(iseq, pos);
2151 #endif
2153 #if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2154 static const struct iseq_insn_info_entry *
2155 get_insn_info_linear_search(const rb_iseq_t *iseq, size_t pos)
2157 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2158 size_t i = 0, size = body->insns_info.size;
2159 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2160 const unsigned int *positions = body->insns_info.positions;
2161 const int debug = 0;
2163 if (debug) {
2164 printf("size: %"PRIuSIZE"\n", size);
2165 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2166 i, positions[i], insns_info[i].line_no, pos);
2169 if (size == 0) {
2170 return NULL;
2172 else if (size == 1) {
2173 return &insns_info[0];
2175 else {
2176 for (i=1; i<size; i++) {
2177 if (debug) printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2178 i, positions[i], insns_info[i].line_no, pos);
2180 if (positions[i] == pos) {
2181 return &insns_info[i];
2183 if (positions[i] > pos) {
2184 return &insns_info[i-1];
2188 return &insns_info[i-1];
2190 #endif
2192 #if VM_INSN_INFO_TABLE_IMPL == 0 /* linear search */
2193 static const struct iseq_insn_info_entry *
2194 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2196 return get_insn_info_linear_search(iseq, pos);
2198 #endif
2200 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2201 static void
2202 validate_get_insn_info(const rb_iseq_t *iseq)
2204 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2205 size_t i;
2206 for (i = 0; i < body->iseq_size; i++) {
2207 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2208 rb_bug("validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE") != get_insn_info(iseq, %"PRIuSIZE")", i, i);
2212 #endif
2214 unsigned int
2215 rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
2217 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2219 if (entry) {
2220 return entry->line_no;
2222 else {
2223 return 0;
2227 #ifdef USE_ISEQ_NODE_ID
2229 rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos)
2231 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2233 if (entry) {
2234 return entry->node_id;
2236 else {
2237 return 0;
2240 #endif
2242 rb_event_flag_t
2243 rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos)
2245 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2246 if (entry) {
2247 return entry->events;
2249 else {
2250 return 0;
2254 void
2255 rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t reset)
2257 struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos);
2258 if (entry) {
2259 entry->events &= ~reset;
2260 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2261 void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos);
2262 rb_iseq_trace_flag_cleared(iseq, pos);
2267 static VALUE
2268 local_var_name(const rb_iseq_t *diseq, VALUE level, VALUE op)
2270 VALUE i;
2271 VALUE name;
2272 ID lid;
2273 int idx;
2275 for (i = 0; i < level; i++) {
2276 diseq = ISEQ_BODY(diseq)->parent_iseq;
2278 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2279 lid = ISEQ_BODY(diseq)->local_table[idx];
2280 name = rb_id2str(lid);
2281 if (!name) {
2282 name = rb_str_new_cstr("?");
2284 else if (!rb_is_local_id(lid)) {
2285 name = rb_str_inspect(name);
2287 else {
2288 name = rb_str_dup(name);
2290 rb_str_catf(name, "@%d", idx);
2291 return name;
2294 int rb_insn_unified_local_var_level(VALUE);
2295 VALUE rb_dump_literal(VALUE lit);
2297 VALUE
2298 rb_insn_operand_intern(const rb_iseq_t *iseq,
2299 VALUE insn, int op_no, VALUE op,
2300 int len, size_t pos, const VALUE *pnop, VALUE child)
2302 const char *types = insn_op_types(insn);
2303 char type = types[op_no];
2304 VALUE ret = Qundef;
2306 switch (type) {
2307 case TS_OFFSET: /* LONG */
2308 ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
2309 break;
2311 case TS_NUM: /* ULONG */
2312 if (insn == BIN(defined) && op_no == 0) {
2313 enum defined_type deftype = (enum defined_type)op;
2314 switch (deftype) {
2315 case DEFINED_FUNC:
2316 ret = rb_fstring_lit("func");
2317 break;
2318 case DEFINED_REF:
2319 ret = rb_fstring_lit("ref");
2320 break;
2321 case DEFINED_CONST_FROM:
2322 ret = rb_fstring_lit("constant-from");
2323 break;
2324 default:
2325 ret = rb_iseq_defined_string(deftype);
2326 break;
2328 if (ret) break;
2330 else if (insn == BIN(checktype) && op_no == 0) {
2331 const char *type_str = rb_type_str((enum ruby_value_type)op);
2332 if (type_str) {
2333 ret = rb_str_new_cstr(type_str); break;
2336 ret = rb_sprintf("%"PRIuVALUE, op);
2337 break;
2339 case TS_LINDEX:{
2340 int level;
2341 if (types[op_no+1] == TS_NUM && pnop) {
2342 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2344 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2345 ret = local_var_name(iseq, (VALUE)level, op - VM_ENV_DATA_SIZE);
2347 else {
2348 ret = rb_inspect(INT2FIX(op));
2350 break;
2352 case TS_ID: /* ID (symbol) */
2353 ret = rb_inspect(ID2SYM(op));
2354 break;
2356 case TS_VALUE: /* VALUE */
2357 op = obj_resurrect(op);
2358 if (insn == BIN(defined) && op_no == 1 && FIXNUM_P(op)) {
2359 /* should be DEFINED_REF */
2360 int type = NUM2INT(op);
2361 if (type) {
2362 if (type & 1) {
2363 ret = rb_sprintf(":$%c", (type >> 1));
2365 else {
2366 ret = rb_sprintf(":$%d", (type >> 1));
2368 break;
2371 ret = rb_dump_literal(op);
2372 if (CLASS_OF(op) == rb_cISeq) {
2373 if (child) {
2374 rb_ary_push(child, op);
2377 break;
2379 case TS_ISEQ: /* iseq */
2381 if (op) {
2382 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2383 ret = ISEQ_BODY(iseq)->location.label;
2384 if (child) {
2385 rb_ary_push(child, (VALUE)iseq);
2388 else {
2389 ret = rb_str_new2("nil");
2391 break;
2394 case TS_IC:
2396 ret = rb_sprintf("<ic:%"PRIdPTRDIFF" ", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2397 const ID *segments = ((IC)op)->segments;
2398 rb_str_cat2(ret, rb_id2name(*segments++));
2399 while (*segments) {
2400 rb_str_catf(ret, "::%s", rb_id2name(*segments++));
2402 rb_str_cat2(ret, ">");
2404 break;
2405 case TS_IVC:
2406 case TS_ICVARC:
2407 case TS_ISE:
2408 ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2409 break;
2411 case TS_CALLDATA:
2413 struct rb_call_data *cd = (struct rb_call_data *)op;
2414 const struct rb_callinfo *ci = cd->ci;
2415 VALUE ary = rb_ary_new();
2416 ID mid = vm_ci_mid(ci);
2418 if (mid) {
2419 rb_ary_push(ary, rb_sprintf("mid:%"PRIsVALUE, rb_id2str(mid)));
2422 rb_ary_push(ary, rb_sprintf("argc:%d", vm_ci_argc(ci)));
2424 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2425 const struct rb_callinfo_kwarg *kw_args = vm_ci_kwarg(ci);
2426 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2427 rb_ary_push(ary, rb_sprintf("kw:[%"PRIsVALUE"]", rb_ary_join(kw_ary, rb_str_new2(","))));
2430 if (vm_ci_flag(ci)) {
2431 VALUE flags = rb_ary_new();
2432 # define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2433 CALL_FLAG(ARGS_SPLAT);
2434 CALL_FLAG(ARGS_SPLAT_MUT);
2435 CALL_FLAG(ARGS_BLOCKARG);
2436 CALL_FLAG(FCALL);
2437 CALL_FLAG(VCALL);
2438 CALL_FLAG(ARGS_SIMPLE);
2439 CALL_FLAG(TAILCALL);
2440 CALL_FLAG(SUPER);
2441 CALL_FLAG(ZSUPER);
2442 CALL_FLAG(KWARG);
2443 CALL_FLAG(KW_SPLAT);
2444 CALL_FLAG(KW_SPLAT_MUT);
2445 CALL_FLAG(OPT_SEND); /* maybe not reachable */
2446 rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
2449 ret = rb_sprintf("<calldata!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
2451 break;
2453 case TS_CDHASH:
2454 ret = rb_str_new2("<cdhash>");
2455 break;
2457 case TS_FUNCPTR:
2459 #ifdef HAVE_DLADDR
2460 Dl_info info;
2461 if (dladdr((void *)op, &info) && info.dli_sname) {
2462 ret = rb_str_new_cstr(info.dli_sname);
2463 break;
2465 #endif
2466 ret = rb_str_new2("<funcptr>");
2468 break;
2470 case TS_BUILTIN:
2472 const struct rb_builtin_function *bf = (const struct rb_builtin_function *)op;
2473 ret = rb_sprintf("<builtin!%s/%d>",
2474 bf->name, bf->argc);
2476 break;
2478 default:
2479 rb_bug("unknown operand type: %c", type);
2481 return ret;
2484 static VALUE
2485 right_strip(VALUE str)
2487 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2488 while (end-- > beg && *end == ' ');
2489 rb_str_set_len(str, end - beg + 1);
2490 return str;
2494 * Disassemble a instruction
2495 * Iseq -> Iseq inspect object
2498 rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
2499 const rb_iseq_t *iseq, VALUE child)
2501 VALUE insn = code[pos];
2502 int len = insn_len(insn);
2503 int j;
2504 const char *types = insn_op_types(insn);
2505 VALUE str = rb_str_new(0, 0);
2506 const char *insn_name_buff;
2508 insn_name_buff = insn_name(insn);
2509 if (1) {
2510 extern const int rb_vm_max_insn_name_size;
2511 rb_str_catf(str, "%04"PRIuSIZE" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2513 else {
2514 rb_str_catf(str, "%04"PRIuSIZE" %-28.*s ", pos,
2515 (int)strcspn(insn_name_buff, "_"), insn_name_buff);
2518 for (j = 0; types[j]; j++) {
2519 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2520 len, pos, &code[pos + j + 2],
2521 child);
2522 rb_str_concat(str, opstr);
2524 if (types[j + 1]) {
2525 rb_str_cat2(str, ", ");
2530 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2531 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2532 if (line_no && line_no != prev) {
2533 long slen = RSTRING_LEN(str);
2534 slen = (slen > 70) ? 0 : (70 - slen);
2535 str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
2540 rb_event_flag_t events = rb_iseq_event_flags(iseq, pos);
2541 if (events) {
2542 str = rb_str_catf(str, "[%s%s%s%s%s%s%s%s%s%s%s%s]",
2543 events & RUBY_EVENT_LINE ? "Li" : "",
2544 events & RUBY_EVENT_CLASS ? "Cl" : "",
2545 events & RUBY_EVENT_END ? "En" : "",
2546 events & RUBY_EVENT_CALL ? "Ca" : "",
2547 events & RUBY_EVENT_RETURN ? "Re" : "",
2548 events & RUBY_EVENT_C_CALL ? "Cc" : "",
2549 events & RUBY_EVENT_C_RETURN ? "Cr" : "",
2550 events & RUBY_EVENT_B_CALL ? "Bc" : "",
2551 events & RUBY_EVENT_B_RETURN ? "Br" : "",
2552 events & RUBY_EVENT_RESCUE ? "Rs" : "",
2553 events & RUBY_EVENT_COVERAGE_LINE ? "Cli" : "",
2554 events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : "");
2558 right_strip(str);
2559 if (ret) {
2560 rb_str_cat2(str, "\n");
2561 rb_str_concat(ret, str);
2563 else {
2564 printf("%.*s\n", (int)RSTRING_LEN(str), RSTRING_PTR(str));
2566 return len;
2569 static const char *
2570 catch_type(int type)
2572 switch (type) {
2573 case CATCH_TYPE_RESCUE:
2574 return "rescue";
2575 case CATCH_TYPE_ENSURE:
2576 return "ensure";
2577 case CATCH_TYPE_RETRY:
2578 return "retry";
2579 case CATCH_TYPE_BREAK:
2580 return "break";
2581 case CATCH_TYPE_REDO:
2582 return "redo";
2583 case CATCH_TYPE_NEXT:
2584 return "next";
2585 default:
2586 rb_bug("unknown catch type: %d", type);
2587 return 0;
2591 static VALUE
2592 iseq_inspect(const rb_iseq_t *iseq)
2594 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2595 if (!body->location.label) {
2596 return rb_sprintf("#<ISeq: uninitialized>");
2598 else {
2599 const rb_code_location_t *loc = &body->location.code_location;
2600 return rb_sprintf("#<ISeq:%"PRIsVALUE"@%"PRIsVALUE":%d (%d,%d)-(%d,%d)>",
2601 body->location.label, rb_iseq_path(iseq),
2602 loc->beg_pos.lineno,
2603 loc->beg_pos.lineno,
2604 loc->beg_pos.column,
2605 loc->end_pos.lineno,
2606 loc->end_pos.column);
2610 static const rb_data_type_t tmp_set = {
2611 "tmpset",
2612 {(void (*)(void *))rb_mark_set, (void (*)(void *))st_free_table, 0, 0,},
2613 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2616 static VALUE
2617 rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
2619 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2620 VALUE *code;
2621 VALUE str = rb_str_new(0, 0);
2622 VALUE child = rb_ary_hidden_new(3);
2623 unsigned int size;
2624 unsigned int i;
2625 long l;
2626 size_t n;
2627 enum {header_minlen = 72};
2628 st_table *done_iseq = 0;
2629 VALUE done_iseq_wrapper = Qnil;
2630 const char *indent_str;
2631 long indent_len;
2633 size = body->iseq_size;
2635 indent_len = RSTRING_LEN(indent);
2636 indent_str = RSTRING_PTR(indent);
2638 rb_str_cat(str, indent_str, indent_len);
2639 rb_str_cat2(str, "== disasm: ");
2641 rb_str_append(str, iseq_inspect(iseq));
2642 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2643 rb_str_modify_expand(str, header_minlen - l);
2644 memset(RSTRING_END(str), '=', header_minlen - l);
2646 if (iseq->body->builtin_attrs) {
2647 #define disasm_builtin_attr(str, iseq, attr) \
2648 if (iseq->body->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
2649 rb_str_cat2(str, " " #attr); \
2651 disasm_builtin_attr(str, iseq, LEAF);
2652 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
2653 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
2655 rb_str_cat2(str, "\n");
2657 /* show catch table information */
2658 if (body->catch_table) {
2659 rb_str_cat(str, indent_str, indent_len);
2660 rb_str_cat2(str, "== catch table\n");
2662 if (body->catch_table) {
2663 rb_str_cat_cstr(indent, "| ");
2664 indent_str = RSTRING_PTR(indent);
2665 for (i = 0; i < body->catch_table->size; i++) {
2666 const struct iseq_catch_table_entry *entry =
2667 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2668 rb_str_cat(str, indent_str, indent_len);
2669 rb_str_catf(str,
2670 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2671 catch_type((int)entry->type), (int)entry->start,
2672 (int)entry->end, (int)entry->sp, (int)entry->cont);
2673 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2674 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2675 if (!done_iseq) {
2676 done_iseq = st_init_numtable();
2677 done_iseq_wrapper = TypedData_Wrap_Struct(0, &tmp_set, done_iseq);
2679 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2680 indent_str = RSTRING_PTR(indent);
2683 rb_str_resize(indent, indent_len);
2684 indent_str = RSTRING_PTR(indent);
2686 if (body->catch_table) {
2687 rb_str_cat(str, indent_str, indent_len);
2688 rb_str_cat2(str, "|-------------------------------------"
2689 "-----------------------------------\n");
2692 /* show local table information */
2693 if (body->local_table) {
2694 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
2695 rb_str_cat(str, indent_str, indent_len);
2696 rb_str_catf(str,
2697 "local table (size: %d, argc: %d "
2698 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2699 body->local_table_size,
2700 body->param.lead_num,
2701 body->param.opt_num,
2702 body->param.flags.has_rest ? body->param.rest_start : -1,
2703 body->param.post_num,
2704 body->param.flags.has_block ? body->param.block_start : -1,
2705 body->param.flags.has_kw ? keyword->num : -1,
2706 body->param.flags.has_kw ? keyword->required_num : -1,
2707 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2709 for (i = body->local_table_size; i > 0;) {
2710 int li = body->local_table_size - --i - 1;
2711 long width;
2712 VALUE name = local_var_name(iseq, 0, i);
2713 char argi[0x100];
2714 char opti[0x100];
2716 opti[0] = '\0';
2717 if (body->param.flags.has_opt) {
2718 int argc = body->param.lead_num;
2719 int opts = body->param.opt_num;
2720 if (li >= argc && li < argc + opts) {
2721 snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
2722 body->param.opt_table[li - argc]);
2726 snprintf(argi, sizeof(argi), "%s%s%s%s%s%s", /* arg, opts, rest, post, kwrest, block */
2727 body->param.lead_num > li ? "Arg" : "",
2728 opti,
2729 (body->param.flags.has_rest && body->param.rest_start == li) ? "Rest" : "",
2730 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ? "Post" : "",
2731 (body->param.flags.has_kwrest && keyword->rest_start == li) ? "Kwrest" : "",
2732 (body->param.flags.has_block && body->param.block_start == li) ? "Block" : "");
2734 rb_str_cat(str, indent_str, indent_len);
2735 rb_str_catf(str, "[%2d] ", i + 1);
2736 width = RSTRING_LEN(str) + 11;
2737 rb_str_append(str, name);
2738 if (*argi) rb_str_catf(str, "<%s>", argi);
2739 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
2741 rb_str_cat_cstr(right_strip(str), "\n");
2744 /* show each line */
2745 code = rb_iseq_original_iseq(iseq);
2746 for (n = 0; n < size;) {
2747 rb_str_cat(str, indent_str, indent_len);
2748 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2751 for (l = 0; l < RARRAY_LEN(child); l++) {
2752 VALUE isv = rb_ary_entry(child, l);
2753 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv)) continue;
2754 rb_str_cat_cstr(str, "\n");
2755 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2756 indent_str = RSTRING_PTR(indent);
2758 RB_GC_GUARD(done_iseq_wrapper);
2760 return str;
2763 VALUE
2764 rb_iseq_disasm(const rb_iseq_t *iseq)
2766 VALUE str = rb_iseq_disasm_recursive(iseq, rb_str_new(0, 0));
2767 rb_str_resize(str, RSTRING_LEN(str));
2768 return str;
2772 * Estimates the number of instance variables that will be set on
2773 * a given `class` with the initialize method defined in
2774 * `initialize_iseq`
2776 attr_index_t
2777 rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq)
2779 struct rb_id_table * iv_names = rb_id_table_create(0);
2781 for (unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2782 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2784 if (cache->iv_set_name) {
2785 rb_id_table_insert(iv_names, cache->iv_set_name, Qtrue);
2789 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2791 VALUE superclass = rb_class_superclass(klass);
2792 count += RCLASS_EXT(superclass)->max_iv_count;
2794 rb_id_table_free(iv_names);
2796 return count;
2800 * call-seq:
2801 * iseq.disasm -> str
2802 * iseq.disassemble -> str
2804 * Returns the instruction sequence as a +String+ in human readable form.
2806 * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
2808 * Produces:
2810 * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
2811 * 0000 trace 1 ( 1)
2812 * 0002 putobject 1
2813 * 0004 putobject 2
2814 * 0006 opt_plus <ic:1>
2815 * 0008 leave
2817 static VALUE
2818 iseqw_disasm(VALUE self)
2820 return rb_iseq_disasm(iseqw_check(self));
2823 static int
2824 iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *child_iseq, void *data), void *data)
2826 unsigned int i;
2827 VALUE *code = rb_iseq_original_iseq(iseq);
2828 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2829 const rb_iseq_t *child;
2830 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2832 if (body->catch_table) {
2833 for (i = 0; i < body->catch_table->size; i++) {
2834 const struct iseq_catch_table_entry *entry =
2835 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2836 child = entry->iseq;
2837 if (child) {
2838 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2839 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2840 (*iter_func)(child, data);
2846 for (i=0; i<body->iseq_size;) {
2847 VALUE insn = code[i];
2848 int len = insn_len(insn);
2849 const char *types = insn_op_types(insn);
2850 int j;
2852 for (j=0; types[j]; j++) {
2853 switch (types[j]) {
2854 case TS_ISEQ:
2855 child = (const rb_iseq_t *)code[i+j+1];
2856 if (child) {
2857 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2858 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2859 (*iter_func)(child, data);
2862 break;
2863 default:
2864 break;
2867 i += len;
2870 return (int)RHASH_SIZE(all_children);
2873 static void
2874 yield_each_children(const rb_iseq_t *child_iseq, void *data)
2876 rb_yield(iseqw_new(child_iseq));
2880 * call-seq:
2881 * iseq.each_child{|child_iseq| ...} -> iseq
2883 * Iterate all direct child instruction sequences.
2884 * Iteration order is implementation/version defined
2885 * so that people should not rely on the order.
2887 static VALUE
2888 iseqw_each_child(VALUE self)
2890 const rb_iseq_t *iseq = iseqw_check(self);
2891 iseq_iterate_children(iseq, yield_each_children, NULL);
2892 return self;
2895 static void
2896 push_event_info(const rb_iseq_t *iseq, rb_event_flag_t events, int line, VALUE ary)
2898 #define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2899 C(RUBY_EVENT_CLASS, "class", rb_iseq_first_lineno(iseq));
2900 C(RUBY_EVENT_CALL, "call", rb_iseq_first_lineno(iseq));
2901 C(RUBY_EVENT_B_CALL, "b_call", rb_iseq_first_lineno(iseq));
2902 C(RUBY_EVENT_LINE, "line", INT2FIX(line));
2903 C(RUBY_EVENT_END, "end", INT2FIX(line));
2904 C(RUBY_EVENT_RETURN, "return", INT2FIX(line));
2905 C(RUBY_EVENT_B_RETURN, "b_return", INT2FIX(line));
2906 C(RUBY_EVENT_RESCUE, "rescue", INT2FIX(line));
2907 #undef C
2911 * call-seq:
2912 * iseq.trace_points -> ary
2914 * Return trace points in the instruction sequence.
2915 * Return an array of [line, event_symbol] pair.
2917 static VALUE
2918 iseqw_trace_points(VALUE self)
2920 const rb_iseq_t *iseq = iseqw_check(self);
2921 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2922 unsigned int i;
2923 VALUE ary = rb_ary_new();
2925 for (i=0; i<body->insns_info.size; i++) {
2926 const struct iseq_insn_info_entry *entry = &body->insns_info.body[i];
2927 if (entry->events) {
2928 push_event_info(iseq, entry->events, entry->line_no, ary);
2931 return ary;
2935 * Returns the instruction sequence containing the given proc or method.
2937 * For example, using irb:
2939 * # a proc
2940 * > p = proc { num = 1 + 2 }
2941 * > RubyVM::InstructionSequence.of(p)
2942 * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
2944 * # for a method
2945 * > def foo(bar); puts bar; end
2946 * > RubyVM::InstructionSequence.of(method(:foo))
2947 * > #=> <RubyVM::InstructionSequence:foo@(irb)>
2949 * Using ::compile_file:
2951 * # /tmp/iseq_of.rb
2952 * def hello
2953 * puts "hello, world"
2954 * end
2956 * $a_global_proc = proc { str = 'a' + 'b' }
2958 * # in irb
2959 * > require '/tmp/iseq_of.rb'
2961 * # first the method hello
2962 * > RubyVM::InstructionSequence.of(method(:hello))
2963 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
2965 * # then the global proc
2966 * > RubyVM::InstructionSequence.of($a_global_proc)
2967 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
2969 static VALUE
2970 iseqw_s_of(VALUE klass, VALUE body)
2972 const rb_iseq_t *iseq = NULL;
2974 if (rb_obj_is_proc(body)) {
2975 iseq = vm_proc_iseq(body);
2977 if (!rb_obj_is_iseq((VALUE)iseq)) {
2978 iseq = NULL;
2981 else if (rb_obj_is_method(body)) {
2982 iseq = rb_method_iseq(body);
2984 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2985 return body;
2988 return iseq ? iseqw_new(iseq) : Qnil;
2992 * call-seq:
2993 * InstructionSequence.disasm(body) -> str
2994 * InstructionSequence.disassemble(body) -> str
2996 * Takes +body+, a Method or Proc object, and returns a String with the
2997 * human readable instructions for +body+.
2999 * For a Method object:
3001 * # /tmp/method.rb
3002 * def hello
3003 * puts "hello, world"
3004 * end
3006 * puts RubyVM::InstructionSequence.disasm(method(:hello))
3008 * Produces:
3010 * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
3011 * 0000 trace 8 ( 1)
3012 * 0002 trace 1 ( 2)
3013 * 0004 putself
3014 * 0005 putstring "hello, world"
3015 * 0007 send :puts, 1, nil, 8, <ic:0>
3016 * 0013 trace 16 ( 3)
3017 * 0015 leave ( 2)
3019 * For a Proc:
3021 * # /tmp/proc.rb
3022 * p = proc { num = 1 + 2 }
3023 * puts RubyVM::InstructionSequence.disasm(p)
3025 * Produces:
3027 * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
3028 * == catch table
3029 * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
3030 * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
3031 * |------------------------------------------------------------------------
3032 * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
3033 * [ 2] num
3034 * 0000 trace 1 ( 1)
3035 * 0002 putobject 1
3036 * 0004 putobject 2
3037 * 0006 opt_plus <ic:1>
3038 * 0008 dup
3039 * 0009 setlocal num, 0
3040 * 0012 leave
3043 static VALUE
3044 iseqw_s_disasm(VALUE klass, VALUE body)
3046 VALUE iseqw = iseqw_s_of(klass, body);
3047 return NIL_P(iseqw) ? Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3050 static VALUE
3051 register_label(struct st_table *table, unsigned long idx)
3053 VALUE sym = rb_str_intern(rb_sprintf("label_%lu", idx));
3054 st_insert(table, idx, sym);
3055 return sym;
3058 static VALUE
3059 exception_type2symbol(VALUE type)
3061 ID id;
3062 switch (type) {
3063 case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
3064 case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
3065 case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
3066 case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
3067 case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
3068 case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
3069 default:
3070 rb_bug("unknown exception type: %d", (int)type);
3072 return ID2SYM(id);
3075 static int
3076 cdhash_each(VALUE key, VALUE value, VALUE ary)
3078 rb_ary_push(ary, obj_resurrect(key));
3079 rb_ary_push(ary, value);
3080 return ST_CONTINUE;
3083 static const rb_data_type_t label_wrapper = {
3084 "label_wrapper",
3085 {(void (*)(void *))rb_mark_tbl, (void (*)(void *))st_free_table, 0, 0,},
3086 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3089 #define DECL_ID(name) \
3090 static ID id_##name
3092 #define INIT_ID(name) \
3093 id_##name = rb_intern(#name)
3095 static VALUE
3096 iseq_type_id(enum rb_iseq_type type)
3098 DECL_ID(top);
3099 DECL_ID(method);
3100 DECL_ID(block);
3101 DECL_ID(class);
3102 DECL_ID(rescue);
3103 DECL_ID(ensure);
3104 DECL_ID(eval);
3105 DECL_ID(main);
3106 DECL_ID(plain);
3108 if (id_top == 0) {
3109 INIT_ID(top);
3110 INIT_ID(method);
3111 INIT_ID(block);
3112 INIT_ID(class);
3113 INIT_ID(rescue);
3114 INIT_ID(ensure);
3115 INIT_ID(eval);
3116 INIT_ID(main);
3117 INIT_ID(plain);
3120 switch (type) {
3121 case ISEQ_TYPE_TOP: return id_top;
3122 case ISEQ_TYPE_METHOD: return id_method;
3123 case ISEQ_TYPE_BLOCK: return id_block;
3124 case ISEQ_TYPE_CLASS: return id_class;
3125 case ISEQ_TYPE_RESCUE: return id_rescue;
3126 case ISEQ_TYPE_ENSURE: return id_ensure;
3127 case ISEQ_TYPE_EVAL: return id_eval;
3128 case ISEQ_TYPE_MAIN: return id_main;
3129 case ISEQ_TYPE_PLAIN: return id_plain;
3132 rb_bug("unsupported iseq type: %d", (int)type);
3135 static VALUE
3136 iseq_data_to_ary(const rb_iseq_t *iseq)
3138 unsigned int i;
3139 long l;
3140 const struct rb_iseq_constant_body *const iseq_body = ISEQ_BODY(iseq);
3141 const struct iseq_insn_info_entry *prev_insn_info;
3142 unsigned int pos;
3143 int last_line = 0;
3144 VALUE *seq, *iseq_original;
3146 VALUE val = rb_ary_new();
3147 ID type; /* Symbol */
3148 VALUE locals = rb_ary_new();
3149 VALUE params = rb_hash_new();
3150 VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
3151 VALUE nbody;
3152 VALUE exception = rb_ary_new(); /* [[....]] */
3153 VALUE misc = rb_hash_new();
3155 static ID insn_syms[VM_INSTRUCTION_SIZE/2]; /* w/o-trace only */
3156 struct st_table *labels_table = st_init_numtable();
3157 VALUE labels_wrapper = TypedData_Wrap_Struct(0, &label_wrapper, labels_table);
3159 if (insn_syms[0] == 0) {
3160 int i;
3161 for (i=0; i<numberof(insn_syms); i++) {
3162 insn_syms[i] = rb_intern(insn_name(i));
3166 /* type */
3167 type = iseq_type_id(iseq_body->type);
3169 /* locals */
3170 for (i=0; i<iseq_body->local_table_size; i++) {
3171 ID lid = iseq_body->local_table[i];
3172 if (lid) {
3173 if (rb_id2str(lid)) {
3174 rb_ary_push(locals, ID2SYM(lid));
3176 else { /* hidden variable from id_internal() */
3177 rb_ary_push(locals, ULONG2NUM(iseq_body->local_table_size-i+1));
3180 else {
3181 rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
3185 /* params */
3187 const struct rb_iseq_param_keyword *const keyword = iseq_body->param.keyword;
3188 int j;
3190 if (iseq_body->param.flags.has_opt) {
3191 int len = iseq_body->param.opt_num + 1;
3192 VALUE arg_opt_labels = rb_ary_new2(len);
3194 for (j = 0; j < len; j++) {
3195 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
3196 rb_ary_push(arg_opt_labels, l);
3198 rb_hash_aset(params, ID2SYM(rb_intern("opt")), arg_opt_labels);
3201 /* commit */
3202 if (iseq_body->param.flags.has_lead) rb_hash_aset(params, ID2SYM(rb_intern("lead_num")), INT2FIX(iseq_body->param.lead_num));
3203 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_num")), INT2FIX(iseq_body->param.post_num));
3204 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_start")), INT2FIX(iseq_body->param.post_start));
3205 if (iseq_body->param.flags.has_rest) rb_hash_aset(params, ID2SYM(rb_intern("rest_start")), INT2FIX(iseq_body->param.rest_start));
3206 if (iseq_body->param.flags.has_block) rb_hash_aset(params, ID2SYM(rb_intern("block_start")), INT2FIX(iseq_body->param.block_start));
3207 if (iseq_body->param.flags.has_kw) {
3208 VALUE keywords = rb_ary_new();
3209 int i, j;
3210 for (i=0; i<keyword->required_num; i++) {
3211 rb_ary_push(keywords, ID2SYM(keyword->table[i]));
3213 for (j=0; i<keyword->num; i++, j++) {
3214 VALUE key = rb_ary_new_from_args(1, ID2SYM(keyword->table[i]));
3215 if (!UNDEF_P(keyword->default_values[j])) {
3216 rb_ary_push(key, keyword->default_values[j]);
3218 rb_ary_push(keywords, key);
3221 rb_hash_aset(params, ID2SYM(rb_intern("kwbits")),
3222 INT2FIX(keyword->bits_start));
3223 rb_hash_aset(params, ID2SYM(rb_intern("keyword")), keywords);
3225 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(keyword->rest_start));
3226 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
3227 if (iseq_body->param.flags.use_block) rb_hash_aset(params, ID2SYM(rb_intern("use_block")), Qtrue);
3230 /* body */
3231 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3233 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3234 VALUE insn = *seq++;
3235 int j, len = insn_len(insn);
3236 VALUE *nseq = seq + len - 1;
3237 VALUE ary = rb_ary_new2(len);
3239 rb_ary_push(ary, ID2SYM(insn_syms[insn%numberof(insn_syms)]));
3240 for (j=0; j<len-1; j++, seq++) {
3241 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3243 switch (op_type) {
3244 case TS_OFFSET: {
3245 unsigned long idx = nseq - iseq_original + *seq;
3246 rb_ary_push(ary, register_label(labels_table, idx));
3247 break;
3249 case TS_LINDEX:
3250 case TS_NUM:
3251 rb_ary_push(ary, INT2FIX(*seq));
3252 break;
3253 case TS_VALUE:
3254 rb_ary_push(ary, obj_resurrect(*seq));
3255 break;
3256 case TS_ISEQ:
3258 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3259 if (iseq) {
3260 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3261 rb_ary_push(ary, val);
3263 else {
3264 rb_ary_push(ary, Qnil);
3267 break;
3268 case TS_IC:
3270 VALUE list = rb_ary_new();
3271 const ID *ids = ((IC)*seq)->segments;
3272 while (*ids) {
3273 rb_ary_push(list, ID2SYM(*ids++));
3275 rb_ary_push(ary, list);
3277 break;
3278 case TS_IVC:
3279 case TS_ICVARC:
3280 case TS_ISE:
3282 union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
3283 rb_ary_push(ary, INT2FIX(is - ISEQ_IS_ENTRY_START(ISEQ_BODY(iseq), op_type)));
3285 break;
3286 case TS_CALLDATA:
3288 struct rb_call_data *cd = (struct rb_call_data *)*seq;
3289 const struct rb_callinfo *ci = cd->ci;
3290 VALUE e = rb_hash_new();
3291 int argc = vm_ci_argc(ci);
3293 ID mid = vm_ci_mid(ci);
3294 rb_hash_aset(e, ID2SYM(rb_intern("mid")), mid ? ID2SYM(mid) : Qnil);
3295 rb_hash_aset(e, ID2SYM(rb_intern("flag")), UINT2NUM(vm_ci_flag(ci)));
3297 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3298 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
3299 int i;
3300 VALUE kw = rb_ary_new2((long)kwarg->keyword_len);
3302 argc -= kwarg->keyword_len;
3303 for (i = 0; i < kwarg->keyword_len; i++) {
3304 rb_ary_push(kw, kwarg->keywords[i]);
3306 rb_hash_aset(e, ID2SYM(rb_intern("kw_arg")), kw);
3309 rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")),
3310 INT2FIX(argc));
3311 rb_ary_push(ary, e);
3313 break;
3314 case TS_ID:
3315 rb_ary_push(ary, ID2SYM(*seq));
3316 break;
3317 case TS_CDHASH:
3319 VALUE hash = *seq;
3320 VALUE val = rb_ary_new();
3321 int i;
3323 rb_hash_foreach(hash, cdhash_each, val);
3325 for (i=0; i<RARRAY_LEN(val); i+=2) {
3326 VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
3327 unsigned long idx = nseq - iseq_original + pos;
3329 rb_ary_store(val, i+1,
3330 register_label(labels_table, idx));
3332 rb_ary_push(ary, val);
3334 break;
3335 case TS_FUNCPTR:
3337 #if SIZEOF_VALUE <= SIZEOF_LONG
3338 VALUE val = LONG2NUM((SIGNED_VALUE)*seq);
3339 #else
3340 VALUE val = LL2NUM((SIGNED_VALUE)*seq);
3341 #endif
3342 rb_ary_push(ary, val);
3344 break;
3345 case TS_BUILTIN:
3347 VALUE val = rb_hash_new();
3348 #if SIZEOF_VALUE <= SIZEOF_LONG
3349 VALUE func_ptr = LONG2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3350 #else
3351 VALUE func_ptr = LL2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3352 #endif
3353 rb_hash_aset(val, ID2SYM(rb_intern("func_ptr")), func_ptr);
3354 rb_hash_aset(val, ID2SYM(rb_intern("argc")), INT2NUM(((RB_BUILTIN)*seq)->argc));
3355 rb_hash_aset(val, ID2SYM(rb_intern("index")), INT2NUM(((RB_BUILTIN)*seq)->index));
3356 rb_hash_aset(val, ID2SYM(rb_intern("name")), rb_str_new_cstr(((RB_BUILTIN)*seq)->name));
3357 rb_ary_push(ary, val);
3359 break;
3360 default:
3361 rb_bug("unknown operand: %c", insn_op_type(insn, j));
3364 rb_ary_push(body, ary);
3367 nbody = body;
3369 /* exception */
3370 if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
3371 VALUE ary = rb_ary_new();
3372 const struct iseq_catch_table_entry *entry =
3373 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3374 rb_ary_push(ary, exception_type2symbol(entry->type));
3375 if (entry->iseq) {
3376 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3378 else {
3379 rb_ary_push(ary, Qnil);
3381 rb_ary_push(ary, register_label(labels_table, entry->start));
3382 rb_ary_push(ary, register_label(labels_table, entry->end));
3383 rb_ary_push(ary, register_label(labels_table, entry->cont));
3384 rb_ary_push(ary, UINT2NUM(entry->sp));
3385 rb_ary_push(exception, ary);
3388 /* make body with labels and insert line number */
3389 body = rb_ary_new();
3390 prev_insn_info = NULL;
3391 #ifdef USE_ISEQ_NODE_ID
3392 VALUE node_ids = rb_ary_new();
3393 #endif
3395 for (l=0, pos=0; l<RARRAY_LEN(nbody); l++) {
3396 const struct iseq_insn_info_entry *info;
3397 VALUE ary = RARRAY_AREF(nbody, l);
3398 st_data_t label;
3400 if (st_lookup(labels_table, pos, &label)) {
3401 rb_ary_push(body, (VALUE)label);
3404 info = get_insn_info(iseq, pos);
3405 #ifdef USE_ISEQ_NODE_ID
3406 rb_ary_push(node_ids, INT2FIX(info->node_id));
3407 #endif
3409 if (prev_insn_info != info) {
3410 int line = info->line_no;
3411 rb_event_flag_t events = info->events;
3413 if (line > 0 && last_line != line) {
3414 rb_ary_push(body, INT2FIX(line));
3415 last_line = line;
3417 #define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3418 CHECK_EVENT(RUBY_EVENT_LINE);
3419 CHECK_EVENT(RUBY_EVENT_CLASS);
3420 CHECK_EVENT(RUBY_EVENT_END);
3421 CHECK_EVENT(RUBY_EVENT_CALL);
3422 CHECK_EVENT(RUBY_EVENT_RETURN);
3423 CHECK_EVENT(RUBY_EVENT_B_CALL);
3424 CHECK_EVENT(RUBY_EVENT_B_RETURN);
3425 CHECK_EVENT(RUBY_EVENT_RESCUE);
3426 #undef CHECK_EVENT
3427 prev_insn_info = info;
3430 rb_ary_push(body, ary);
3431 pos += RARRAY_LENINT(ary); /* reject too huge data */
3433 RB_GC_GUARD(nbody);
3434 RB_GC_GUARD(labels_wrapper);
3436 rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq_body->param.size));
3437 rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq_body->local_table_size));
3438 rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq_body->stack_max));
3439 rb_hash_aset(misc, ID2SYM(rb_intern("node_id")), INT2FIX(iseq_body->location.node_id));
3440 rb_hash_aset(misc, ID2SYM(rb_intern("code_location")),
3441 rb_ary_new_from_args(4,
3442 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3443 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3444 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3445 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3446 #ifdef USE_ISEQ_NODE_ID
3447 rb_hash_aset(misc, ID2SYM(rb_intern("node_ids")), node_ids);
3448 #endif
3449 rb_hash_aset(misc, ID2SYM(rb_intern("parser")), iseq_body->prism ? ID2SYM(rb_intern("prism")) : ID2SYM(rb_intern("parse.y")));
3452 * [:magic, :major_version, :minor_version, :format_type, :misc,
3453 * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
3454 * :catch_table, :bytecode]
3456 rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
3457 rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
3458 rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
3459 rb_ary_push(val, INT2FIX(1));
3460 rb_ary_push(val, misc);
3461 rb_ary_push(val, iseq_body->location.label);
3462 rb_ary_push(val, rb_iseq_path(iseq));
3463 rb_ary_push(val, rb_iseq_realpath(iseq));
3464 rb_ary_push(val, RB_INT2NUM(iseq_body->location.first_lineno));
3465 rb_ary_push(val, ID2SYM(type));
3466 rb_ary_push(val, locals);
3467 rb_ary_push(val, params);
3468 rb_ary_push(val, exception);
3469 rb_ary_push(val, body);
3470 return val;
3473 VALUE
3474 rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
3476 int i, r;
3477 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3478 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
3479 VALUE a, args = rb_ary_new2(body->param.size);
3480 ID req, opt, rest, block, key, keyrest;
3481 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3482 #define PARAM_ID(i) body->local_table[(i)]
3483 #define PARAM(i, type) ( \
3484 PARAM_TYPE(type), \
3485 rb_id2str(PARAM_ID(i)) ? \
3486 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3489 CONST_ID(req, "req");
3490 CONST_ID(opt, "opt");
3491 if (is_proc) {
3492 for (i = 0; i < body->param.lead_num; i++) {
3493 PARAM_TYPE(opt);
3494 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3495 rb_ary_push(args, a);
3498 else {
3499 for (i = 0; i < body->param.lead_num; i++) {
3500 rb_ary_push(args, PARAM(i, req));
3503 r = body->param.lead_num + body->param.opt_num;
3504 for (; i < r; i++) {
3505 PARAM_TYPE(opt);
3506 if (rb_id2str(PARAM_ID(i))) {
3507 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3509 rb_ary_push(args, a);
3511 if (body->param.flags.has_rest) {
3512 CONST_ID(rest, "rest");
3513 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3515 r = body->param.post_start + body->param.post_num;
3516 if (is_proc) {
3517 for (i = body->param.post_start; i < r; i++) {
3518 PARAM_TYPE(opt);
3519 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3520 rb_ary_push(args, a);
3523 else {
3524 for (i = body->param.post_start; i < r; i++) {
3525 rb_ary_push(args, PARAM(i, req));
3528 if (body->param.flags.accepts_no_kwarg) {
3529 ID nokey;
3530 CONST_ID(nokey, "nokey");
3531 PARAM_TYPE(nokey);
3532 rb_ary_push(args, a);
3534 if (body->param.flags.has_kw) {
3535 i = 0;
3536 if (keyword->required_num > 0) {
3537 ID keyreq;
3538 CONST_ID(keyreq, "keyreq");
3539 for (; i < keyword->required_num; i++) {
3540 PARAM_TYPE(keyreq);
3541 if (rb_id2str(keyword->table[i])) {
3542 rb_ary_push(a, ID2SYM(keyword->table[i]));
3544 rb_ary_push(args, a);
3547 CONST_ID(key, "key");
3548 for (; i < keyword->num; i++) {
3549 PARAM_TYPE(key);
3550 if (rb_id2str(keyword->table[i])) {
3551 rb_ary_push(a, ID2SYM(keyword->table[i]));
3553 rb_ary_push(args, a);
3556 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3557 ID param;
3558 CONST_ID(keyrest, "keyrest");
3559 PARAM_TYPE(keyrest);
3560 if (body->param.flags.has_kwrest &&
3561 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3562 rb_ary_push(a, ID2SYM(param));
3564 else if (body->param.flags.ruby2_keywords) {
3565 rb_ary_push(a, ID2SYM(idPow));
3567 rb_ary_push(args, a);
3569 if (body->param.flags.has_block) {
3570 CONST_ID(block, "block");
3571 rb_ary_push(args, PARAM(body->param.block_start, block));
3573 return args;
3576 VALUE
3577 rb_iseq_defined_string(enum defined_type type)
3579 static const char expr_names[][18] = {
3580 "nil",
3581 "instance-variable",
3582 "local-variable",
3583 "global-variable",
3584 "class variable",
3585 "constant",
3586 "method",
3587 "yield",
3588 "super",
3589 "self",
3590 "true",
3591 "false",
3592 "assignment",
3593 "expression",
3595 const char *estr;
3597 if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) rb_bug("unknown defined type %d", type);
3598 estr = expr_names[type - 1];
3599 return rb_fstring_cstr(estr);
3602 /* A map from encoded_insn to insn_data: decoded insn number, its len,
3603 * non-trace version of encoded insn, and trace version. */
3605 static st_table *encoded_insn_data;
3606 typedef struct insn_data_struct {
3607 int insn;
3608 int insn_len;
3609 void *notrace_encoded_insn;
3610 void *trace_encoded_insn;
3611 } insn_data_t;
3612 static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3614 void
3615 rb_free_encoded_insn_data(void)
3617 st_free_table(encoded_insn_data);
3620 void
3621 rb_vm_encoded_insn_data_table_init(void)
3623 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3624 const void * const *table = rb_vm_get_insns_address_table();
3625 #define INSN_CODE(insn) ((VALUE)table[insn])
3626 #else
3627 #define INSN_CODE(insn) (insn)
3628 #endif
3629 st_data_t insn;
3630 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3632 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3633 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3634 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3636 insn_data[insn].insn = (int)insn;
3637 insn_data[insn].insn_len = insn_len(insn);
3639 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3640 insn_data[insn].notrace_encoded_insn = (void *) key1;
3641 insn_data[insn].trace_encoded_insn = (void *) key2;
3643 else {
3644 insn_data[insn].notrace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3645 insn_data[insn].trace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3648 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3649 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3654 rb_vm_insn_addr2insn(const void *addr)
3656 st_data_t key = (st_data_t)addr;
3657 st_data_t val;
3659 if (st_lookup(encoded_insn_data, key, &val)) {
3660 insn_data_t *e = (insn_data_t *)val;
3661 return (int)e->insn;
3664 rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3667 // Unlike rb_vm_insn_addr2insn, this function can return trace opcode variants.
3669 rb_vm_insn_addr2opcode(const void *addr)
3671 st_data_t key = (st_data_t)addr;
3672 st_data_t val;
3674 if (st_lookup(encoded_insn_data, key, &val)) {
3675 insn_data_t *e = (insn_data_t *)val;
3676 int opcode = e->insn;
3677 if (addr == e->trace_encoded_insn) {
3678 opcode += VM_INSTRUCTION_SIZE/2;
3680 return opcode;
3683 rb_bug("rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3686 // Decode `ISEQ_BODY(iseq)->iseq_encoded[i]` to an insn.
3688 rb_vm_insn_decode(const VALUE encoded)
3690 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3691 int insn = rb_vm_insn_addr2insn((void *)encoded);
3692 #else
3693 int insn = (int)encoded;
3694 #endif
3695 return insn;
3698 static inline int
3699 encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace)
3701 st_data_t key = (st_data_t)*iseq_encoded_insn;
3702 st_data_t val;
3704 if (st_lookup(encoded_insn_data, key, &val)) {
3705 insn_data_t *e = (insn_data_t *)val;
3706 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3707 turnon = 1;
3709 *iseq_encoded_insn = (VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3710 return e->insn_len;
3713 rb_bug("trace_instrument: invalid insn address: %p", (void *)*iseq_encoded_insn);
3716 void
3717 rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos)
3719 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3720 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3721 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0, false);
3724 // We need to fire call events on instructions with b_call events if the block
3725 // is running as a method. So, if we are listening for call events, then
3726 // instructions that have b_call events need to become trace variants.
3727 // Use this function when making decisions about recompiling to trace variants.
3728 static inline rb_event_flag_t
3729 add_bmethod_events(rb_event_flag_t events)
3731 if (events & RUBY_EVENT_CALL) {
3732 events |= RUBY_EVENT_B_CALL;
3734 if (events & RUBY_EVENT_RETURN) {
3735 events |= RUBY_EVENT_B_RETURN;
3737 return events;
3740 // Note, to support call/return events for bmethods, turnon_event can have more events than tpval.
3741 static int
3742 iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
3744 unsigned int pc;
3745 int n = 0;
3746 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3747 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3749 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3751 for (pc=0; pc<body->iseq_size;) {
3752 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc);
3753 rb_event_flag_t pc_events = entry->events;
3754 rb_event_flag_t target_events = turnon_events;
3755 unsigned int line = (int)entry->line_no;
3757 if (target_line == 0 || target_line == line) {
3758 /* ok */
3760 else {
3761 target_events &= ~RUBY_EVENT_LINE;
3764 if (pc_events & target_events) {
3765 n++;
3767 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events), true);
3770 if (n > 0) {
3771 if (iseq->aux.exec.local_hooks == NULL) {
3772 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = RB_ZALLOC(rb_hook_list_t);
3773 iseq->aux.exec.local_hooks->is_local = true;
3775 rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3778 return n;
3781 struct trace_set_local_events_struct {
3782 rb_event_flag_t turnon_events;
3783 VALUE tpval;
3784 unsigned int target_line;
3785 int n;
3788 static void
3789 iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3791 struct trace_set_local_events_struct *data = (struct trace_set_local_events_struct *)p;
3792 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3793 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3797 rb_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)
3799 struct trace_set_local_events_struct data;
3800 if (target_bmethod) {
3801 turnon_events = add_bmethod_events(turnon_events);
3803 data.turnon_events = turnon_events;
3804 data.tpval = tpval;
3805 data.target_line = target_line;
3806 data.n = 0;
3808 iseq_add_local_tracepoint_i(iseq, (void *)&data);
3809 if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */
3810 return data.n;
3813 static int
3814 iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval)
3816 int n = 0;
3818 if (iseq->aux.exec.local_hooks) {
3819 unsigned int pc;
3820 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3821 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3822 rb_event_flag_t local_events = 0;
3824 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3825 local_events = iseq->aux.exec.local_hooks->events;
3827 if (local_events == 0) {
3828 rb_hook_list_free(iseq->aux.exec.local_hooks);
3829 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3832 local_events = add_bmethod_events(local_events);
3833 for (pc = 0; pc<body->iseq_size;) {
3834 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3835 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false);
3838 return n;
3841 struct trace_clear_local_events_struct {
3842 VALUE tpval;
3843 int n;
3846 static void
3847 iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3849 struct trace_clear_local_events_struct *data = (struct trace_clear_local_events_struct *)p;
3850 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3851 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3855 rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval)
3857 struct trace_clear_local_events_struct data;
3858 data.tpval = tpval;
3859 data.n = 0;
3861 iseq_remove_local_tracepoint_i(iseq, (void *)&data);
3862 return data.n;
3865 void
3866 rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
3868 if (iseq->aux.exec.global_trace_events == turnon_events) {
3869 return;
3872 if (!ISEQ_EXECUTABLE_P(iseq)) {
3873 /* this is building ISeq */
3874 return;
3876 else {
3877 unsigned int pc;
3878 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3879 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3880 rb_event_flag_t enabled_events;
3881 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3882 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3883 enabled_events = add_bmethod_events(turnon_events | local_events);
3885 for (pc=0; pc<body->iseq_size;) {
3886 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3887 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events, true);
3892 void rb_vm_cc_general(const struct rb_callcache *cc);
3894 static bool
3895 clear_attr_cc(VALUE v)
3897 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((const struct rb_callcache *)v)) {
3898 rb_vm_cc_general((struct rb_callcache *)v);
3899 return true;
3901 else {
3902 return false;
3906 static bool
3907 clear_bf_cc(VALUE v)
3909 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((const struct rb_callcache *)v)) {
3910 rb_vm_cc_general((struct rb_callcache *)v);
3911 return true;
3913 else {
3914 return false;
3918 static int
3919 clear_attr_ccs_i(void *vstart, void *vend, size_t stride, void *data)
3921 VALUE v = (VALUE)vstart;
3922 for (; v != (VALUE)vend; v += stride) {
3923 void *ptr = asan_poisoned_object_p(v);
3924 asan_unpoison_object(v, false);
3925 clear_attr_cc(v);
3926 asan_poison_object_if(ptr, v);
3928 return 0;
3931 void
3932 rb_clear_attr_ccs(void)
3934 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3937 static int
3938 clear_bf_ccs_i(void *vstart, void *vend, size_t stride, void *data)
3940 VALUE v = (VALUE)vstart;
3941 for (; v != (VALUE)vend; v += stride) {
3942 void *ptr = asan_poisoned_object_p(v);
3943 asan_unpoison_object(v, false);
3944 clear_bf_cc(v);
3945 asan_poison_object_if(ptr, v);
3947 return 0;
3950 void
3951 rb_clear_bf_ccs(void)
3953 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
3956 static int
3957 trace_set_i(void *vstart, void *vend, size_t stride, void *data)
3959 rb_event_flag_t turnon_events = *(rb_event_flag_t *)data;
3961 VALUE v = (VALUE)vstart;
3962 for (; v != (VALUE)vend; v += stride) {
3963 void *ptr = asan_poisoned_object_p(v);
3964 asan_unpoison_object(v, false);
3966 if (rb_obj_is_iseq(v)) {
3967 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
3969 else if (clear_attr_cc(v)) {
3971 else if (clear_bf_cc(v)) {
3974 asan_poison_object_if(ptr, v);
3976 return 0;
3979 void
3980 rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
3982 rb_objspace_each_objects(trace_set_i, &turnon_events);
3985 VALUE
3986 rb_iseqw_local_variables(VALUE iseqval)
3988 return rb_iseq_local_variables(iseqw_check(iseqval));
3992 * call-seq:
3993 * iseq.to_binary(extra_data = nil) -> binary str
3995 * Returns serialized iseq binary format data as a String object.
3996 * A corresponding iseq object is created by
3997 * RubyVM::InstructionSequence.load_from_binary() method.
3999 * String extra_data will be saved with binary data.
4000 * You can access this data with
4001 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary).
4003 * Note that the translated binary data is not portable.
4004 * You can not move this binary data to another machine.
4005 * You can not use the binary data which is created by another
4006 * version/another architecture of Ruby.
4008 static VALUE
4009 iseqw_to_binary(int argc, VALUE *argv, VALUE self)
4011 VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0];
4012 return rb_iseq_ibf_dump(iseqw_check(self), opt);
4016 * call-seq:
4017 * RubyVM::InstructionSequence.load_from_binary(binary) -> iseq
4019 * Load an iseq object from binary format String object
4020 * created by RubyVM::InstructionSequence.to_binary.
4022 * This loader does not have a verifier, so that loading broken/modified
4023 * binary causes critical problem.
4025 * You should not load binary data provided by others.
4026 * You should use binary data translated by yourself.
4028 static VALUE
4029 iseqw_s_load_from_binary(VALUE self, VALUE str)
4031 return iseqw_new(rb_iseq_ibf_load(str));
4035 * call-seq:
4036 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary) -> str
4038 * Load extra data embed into binary format String object.
4040 static VALUE
4041 iseqw_s_load_from_binary_extra_data(VALUE self, VALUE str)
4043 return rb_iseq_ibf_load_extra_data(str);
4046 #if VM_INSN_INFO_TABLE_IMPL == 2
4048 /* An implementation of succinct bit-vector for insn_info table.
4050 * A succinct bit-vector is a small and efficient data structure that provides
4051 * a bit-vector augmented with an index for O(1) rank operation:
4053 * rank(bv, n): the number of 1's within a range from index 0 to index n
4055 * This can be used to lookup insn_info table from PC.
4056 * For example, consider the following iseq and insn_info_table:
4058 * iseq insn_info_table
4059 * PC insn+operand position lineno event
4060 * 0: insn1 0: 1 [Li]
4061 * 2: insn2 2: 2 [Li] <= (A)
4062 * 5: insn3 8: 3 [Li] <= (B)
4063 * 8: insn4
4065 * In this case, a succinct bit-vector whose indexes 0, 2, 8 is "1" and
4066 * other indexes is "0", i.e., "101000001", is created.
4067 * To lookup the lineno of insn2, calculate rank("10100001", 2) = 2, so
4068 * the line (A) is the entry in question.
4069 * To lookup the lineno of insn4, calculate rank("10100001", 8) = 3, so
4070 * the line (B) is the entry in question.
4072 * A naive implementation of succinct bit-vector works really well
4073 * not only for large size but also for small size. However, it has
4074 * tiny overhead for very small size. So, this implementation consist
4075 * of two parts: one part is the "immediate" table that keeps rank result
4076 * as a raw table, and the other part is a normal succinct bit-vector.
4079 #define IMMEDIATE_TABLE_SIZE 54 /* a multiple of 9, and < 128 */
4081 struct succ_index_table {
4082 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4083 struct succ_dict_block {
4084 unsigned int rank;
4085 uint64_t small_block_ranks; /* 9 bits * 7 = 63 bits */
4086 uint64_t bits[512/64];
4087 } succ_part[FLEX_ARY_LEN];
4090 #define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4091 #define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4092 #define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4093 #define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4095 static struct succ_index_table *
4096 succ_index_table_create(int max_pos, int *data, int size)
4098 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4099 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4100 struct succ_index_table *sd =
4101 rb_xcalloc_mul_add_mul(
4102 imm_size, sizeof(uint64_t),
4103 succ_size, sizeof(struct succ_dict_block));
4104 int i, j, k, r;
4106 r = 0;
4107 for (j = 0; j < imm_size; j++) {
4108 for (i = 0; i < 9; i++) {
4109 if (r < size && data[r] == j * 9 + i) r++;
4110 imm_block_rank_set(sd->imm_part[j], i, r);
4113 for (k = 0; k < succ_size; k++) {
4114 struct succ_dict_block *sd_block = &sd->succ_part[k];
4115 int small_rank = 0;
4116 sd_block->rank = r;
4117 for (j = 0; j < 8; j++) {
4118 uint64_t bits = 0;
4119 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4120 for (i = 0; i < 64; i++) {
4121 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4122 bits |= ((uint64_t)1) << i;
4123 r++;
4126 sd_block->bits[j] = bits;
4127 small_rank += rb_popcount64(bits);
4130 return sd;
4133 static unsigned int *
4134 succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size)
4136 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4137 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4138 unsigned int *positions = ALLOC_N(unsigned int, size), *p;
4139 int i, j, k, r = -1;
4140 p = positions;
4141 for (j = 0; j < imm_size; j++) {
4142 for (i = 0; i < 9; i++) {
4143 int nr = imm_block_rank_get(sd->imm_part[j], i);
4144 if (r != nr) *p++ = j * 9 + i;
4145 r = nr;
4148 for (k = 0; k < succ_size; k++) {
4149 for (j = 0; j < 8; j++) {
4150 for (i = 0; i < 64; i++) {
4151 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4152 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4157 return positions;
4160 static int
4161 succ_index_lookup(const struct succ_index_table *sd, int x)
4163 if (x < IMMEDIATE_TABLE_SIZE) {
4164 const int i = x / 9;
4165 const int j = x % 9;
4166 return imm_block_rank_get(sd->imm_part[i], j);
4168 else {
4169 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4170 const struct succ_dict_block *block = &sd->succ_part[block_index];
4171 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4172 const int small_block_index = block_bit_index / 64;
4173 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4174 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4176 return block->rank + small_block_popcount + popcnt;
4179 #endif
4183 * call-seq:
4184 * iseq.script_lines -> array or nil
4186 * It returns recorded script lines if it is available.
4187 * The script lines are not limited to the iseq range, but
4188 * are entire lines of the source file.
4190 * Note that this is an API for ruby internal use, debugging,
4191 * and research. Do not use this for any other purpose.
4192 * The compatibility is not guaranteed.
4194 static VALUE
4195 iseqw_script_lines(VALUE self)
4197 const rb_iseq_t *iseq = iseqw_check(self);
4198 return ISEQ_BODY(iseq)->variable.script_lines;
4202 * Document-class: RubyVM::InstructionSequence
4204 * The InstructionSequence class represents a compiled sequence of
4205 * instructions for the Virtual Machine used in MRI. Not all implementations of Ruby
4206 * may implement this class, and for the implementations that implement it,
4207 * the methods defined and behavior of the methods can change in any version.
4209 * With it, you can get a handle to the instructions that make up a method or
4210 * a proc, compile strings of Ruby code down to VM instructions, and
4211 * disassemble instruction sequences to strings for easy inspection. It is
4212 * mostly useful if you want to learn how YARV works, but it also lets
4213 * you control various settings for the Ruby iseq compiler.
4215 * You can find the source for the VM instructions in +insns.def+ in the Ruby
4216 * source.
4218 * The instruction sequence results will almost certainly change as Ruby
4219 * changes, so example output in this documentation may be different from what
4220 * you see.
4222 * Of course, this class is MRI specific.
4225 void
4226 Init_ISeq(void)
4228 /* declare ::RubyVM::InstructionSequence */
4229 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
4230 rb_undef_alloc_func(rb_cISeq);
4231 rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
4232 rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
4233 rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
4234 rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
4235 rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
4237 rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
4238 rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1);
4239 rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1);
4241 /* location APIs */
4242 rb_define_method(rb_cISeq, "path", iseqw_path, 0);
4243 rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0);
4244 rb_define_method(rb_cISeq, "label", iseqw_label, 0);
4245 rb_define_method(rb_cISeq, "base_label", iseqw_base_label, 0);
4246 rb_define_method(rb_cISeq, "first_lineno", iseqw_first_lineno, 0);
4247 rb_define_method(rb_cISeq, "trace_points", iseqw_trace_points, 0);
4248 rb_define_method(rb_cISeq, "each_child", iseqw_each_child, 0);
4250 #if 0 /* TBD */
4251 rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
4252 rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
4253 /* disable this feature because there is no verifier. */
4254 rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
4255 #endif
4256 (void)iseq_s_load;
4258 rb_define_singleton_method(rb_cISeq, "compile", iseqw_s_compile, -1);
4259 rb_define_singleton_method(rb_cISeq, "compile_prism", iseqw_s_compile_prism, -1);
4260 rb_define_singleton_method(rb_cISeq, "compile_file_prism", iseqw_s_compile_file_prism, -1);
4261 rb_define_singleton_method(rb_cISeq, "new", iseqw_s_compile, -1);
4262 rb_define_singleton_method(rb_cISeq, "compile_file", iseqw_s_compile_file, -1);
4263 rb_define_singleton_method(rb_cISeq, "compile_option", iseqw_s_compile_option_get, 0);
4264 rb_define_singleton_method(rb_cISeq, "compile_option=", iseqw_s_compile_option_set, 1);
4265 rb_define_singleton_method(rb_cISeq, "disasm", iseqw_s_disasm, 1);
4266 rb_define_singleton_method(rb_cISeq, "disassemble", iseqw_s_disasm, 1);
4267 rb_define_singleton_method(rb_cISeq, "of", iseqw_s_of, 1);
4269 // script lines
4270 rb_define_method(rb_cISeq, "script_lines", iseqw_script_lines, 0);
4272 rb_undef_method(CLASS_OF(rb_cISeq), "translate");
4273 rb_undef_method(CLASS_OF(rb_cISeq), "load_iseq");