[ruby/strscan] jruby: Check if len++ walked off the end
[ruby.git] / iseq.c
blobbcd299cf7d525c57936b23690d279d7855dc139f
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/set_table.h"
35 #include "internal/symbol.h"
36 #include "internal/thread.h"
37 #include "internal/variable.h"
38 #include "iseq.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 set_table *ics = (set_table *)lookup_result;
115 set_delete(ics, &ic_data);
117 if (ics->num_entries == 0 &&
118 // See comment in vm_track_constant_cache on why we need this check
119 id != vm->inserting_constant_cache_id) {
120 rb_id_table_delete(vm->constant_cache, id);
121 set_free_table(ics);
126 // When an ISEQ is being freed, all of its associated ICs are going to go away
127 // as well. Because of this, we need to iterate over the ICs, and clear them
128 // from the VM's constant cache.
129 static void
130 iseq_clear_ic_references(const rb_iseq_t *iseq)
132 // In some cases (when there is a compilation error), we end up with
133 // ic_size greater than 0, but no allocated is_entries buffer.
134 // If there's no is_entries buffer to loop through, return early.
135 // [Bug #19173]
136 if (!ISEQ_BODY(iseq)->is_entries) {
137 return;
140 for (unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
141 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
143 // Iterate over the IC's constant path's segments and clean any references to
144 // the ICs out of the VM's constant cache table.
145 const ID *segments = ic->segments;
147 // It's possible that segments is NULL if we overallocated an IC but
148 // optimizations removed the instruction using it
149 if (segments == NULL)
150 continue;
152 for (int i = 0; segments[i]; i++) {
153 ID id = segments[i];
154 if (id == idNULL) continue;
155 remove_from_constant_cache(id, ic);
158 ruby_xfree((void *)segments);
162 void
163 rb_iseq_free(const rb_iseq_t *iseq)
165 RUBY_FREE_ENTER("iseq");
167 if (iseq && ISEQ_BODY(iseq)) {
168 iseq_clear_ic_references(iseq);
169 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
170 #if USE_YJIT
171 rb_yjit_iseq_free(iseq);
172 if (FL_TEST_RAW((VALUE)iseq, ISEQ_TRANSLATED)) {
173 RUBY_ASSERT(rb_yjit_live_iseq_count > 0);
174 rb_yjit_live_iseq_count--;
176 #endif
177 ruby_xfree((void *)body->iseq_encoded);
178 ruby_xfree((void *)body->insns_info.body);
179 ruby_xfree((void *)body->insns_info.positions);
180 #if VM_INSN_INFO_TABLE_IMPL == 2
181 ruby_xfree(body->insns_info.succ_index_table);
182 #endif
183 ruby_xfree((void *)body->is_entries);
184 ruby_xfree(body->call_data);
185 ruby_xfree((void *)body->catch_table);
186 ruby_xfree((void *)body->param.opt_table);
187 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
188 ruby_xfree((void *)body->mark_bits.list);
191 ruby_xfree(body->variable.original_iseq);
193 if (body->param.keyword != NULL) {
194 if (body->param.keyword->table != &body->local_table[body->param.keyword->bits_start - body->param.keyword->num])
195 ruby_xfree((void *)body->param.keyword->table);
196 if (body->param.keyword->default_values) {
197 ruby_xfree((void *)body->param.keyword->default_values);
199 ruby_xfree((void *)body->param.keyword);
201 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
202 ruby_xfree((void *)body->local_table);
203 compile_data_free(ISEQ_COMPILE_DATA(iseq));
204 if (body->outer_variables) rb_id_table_free(body->outer_variables);
205 ruby_xfree(body);
208 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
209 rb_hook_list_free(iseq->aux.exec.local_hooks);
212 RUBY_FREE_LEAVE("iseq");
215 typedef VALUE iseq_value_itr_t(void *ctx, VALUE obj);
217 static inline void
218 iseq_scan_bits(unsigned int page, iseq_bits_t bits, VALUE *code, VALUE *original_iseq)
220 unsigned int offset;
221 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
223 while (bits) {
224 offset = ntz_intptr(bits);
225 VALUE op = code[page_offset + offset];
226 rb_gc_mark_and_move(&code[page_offset + offset]);
227 VALUE newop = code[page_offset + offset];
228 if (original_iseq && newop != op) {
229 original_iseq[page_offset + offset] = newop;
231 bits &= bits - 1; // Reset Lowest Set Bit (BLSR)
235 static void
236 rb_iseq_mark_and_move_each_compile_data_value(const rb_iseq_t *iseq, VALUE *original_iseq)
238 unsigned int size;
239 VALUE *code;
240 const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
242 size = compile_data->iseq_size;
243 code = compile_data->iseq_encoded;
245 // Embedded VALUEs
246 if (compile_data->mark_bits.list) {
247 if(compile_data->is_single_mark_bit) {
248 iseq_scan_bits(0, compile_data->mark_bits.single, code, original_iseq);
250 else {
251 for (unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
252 iseq_bits_t bits = compile_data->mark_bits.list[i];
253 iseq_scan_bits(i, bits, code, original_iseq);
258 static void
259 rb_iseq_mark_and_move_each_body_value(const rb_iseq_t *iseq, VALUE *original_iseq)
261 unsigned int size;
262 VALUE *code;
263 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
265 size = body->iseq_size;
266 code = body->iseq_encoded;
268 union iseq_inline_storage_entry *is_entries = body->is_entries;
270 if (body->is_entries) {
271 // Skip iterating over ivc caches
272 is_entries += body->ivc_size;
274 // ICVARC entries
275 for (unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
276 ICVARC icvarc = (ICVARC)is_entries;
277 if (icvarc->entry) {
278 RUBY_ASSERT(!RB_TYPE_P(icvarc->entry->class_value, T_NONE));
280 rb_gc_mark_and_move(&icvarc->entry->class_value);
284 // ISE entries
285 for (unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
286 union iseq_inline_storage_entry *const is = (union iseq_inline_storage_entry *)is_entries;
287 if (is->once.value) {
288 rb_gc_mark_and_move(&is->once.value);
292 // IC Entries
293 for (unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
294 IC ic = (IC)is_entries;
295 if (ic->entry) {
296 rb_gc_mark_and_move_ptr(&ic->entry);
301 // Embedded VALUEs
302 if (body->mark_bits.list) {
303 if (ISEQ_MBITS_BUFLEN(size) == 1) {
304 iseq_scan_bits(0, body->mark_bits.single, code, original_iseq);
306 else {
307 for (unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
308 iseq_bits_t bits = body->mark_bits.list[i];
309 iseq_scan_bits(i, bits, code, original_iseq);
315 static bool
316 cc_is_active(const struct rb_callcache *cc, bool reference_updating)
318 if (cc) {
319 if (cc == rb_vm_empty_cc() || rb_vm_empty_cc_for_super()) {
320 return false;
323 if (reference_updating) {
324 cc = (const struct rb_callcache *)rb_gc_location((VALUE)cc);
327 if (vm_cc_markable(cc)) {
328 if (cc->klass) { // cc is not invalidated
329 const struct rb_callable_method_entry_struct *cme = vm_cc_cme(cc);
330 if (reference_updating) {
331 cme = (const struct rb_callable_method_entry_struct *)rb_gc_location((VALUE)cme);
333 if (!METHOD_ENTRY_INVALIDATED(cme)) {
334 return true;
339 return false;
342 void
343 rb_iseq_mark_and_move(rb_iseq_t *iseq, bool reference_updating)
345 RUBY_MARK_ENTER("iseq");
347 rb_gc_mark_and_move(&iseq->wrapper);
349 if (ISEQ_BODY(iseq)) {
350 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
352 rb_iseq_mark_and_move_each_body_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
354 rb_gc_mark_and_move(&body->variable.coverage);
355 rb_gc_mark_and_move(&body->variable.pc2branchindex);
356 rb_gc_mark_and_move(&body->variable.script_lines);
357 rb_gc_mark_and_move(&body->location.label);
358 rb_gc_mark_and_move(&body->location.base_label);
359 rb_gc_mark_and_move(&body->location.pathobj);
360 if (body->local_iseq) rb_gc_mark_and_move_ptr(&body->local_iseq);
361 if (body->parent_iseq) rb_gc_mark_and_move_ptr(&body->parent_iseq);
362 if (body->mandatory_only_iseq) rb_gc_mark_and_move_ptr(&body->mandatory_only_iseq);
364 if (body->call_data) {
365 for (unsigned int i = 0; i < body->ci_size; i++) {
366 struct rb_call_data *cds = body->call_data;
368 if (cds[i].ci) rb_gc_mark_and_move_ptr(&cds[i].ci);
370 if (cc_is_active(cds[i].cc, reference_updating)) {
371 rb_gc_mark_and_move_ptr(&cds[i].cc);
373 else if (cds[i].cc != rb_vm_empty_cc()) {
374 cds[i].cc = rb_vm_empty_cc();
379 if (body->param.flags.has_kw && body->param.keyword != NULL) {
380 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
382 if (keyword->default_values != NULL) {
383 for (int j = 0, i = keyword->required_num; i < keyword->num; i++, j++) {
384 rb_gc_mark_and_move(&keyword->default_values[j]);
389 if (body->catch_table) {
390 struct iseq_catch_table *table = body->catch_table;
392 for (unsigned int i = 0; i < table->size; i++) {
393 struct iseq_catch_table_entry *entry;
394 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
395 if (entry->iseq) {
396 rb_gc_mark_and_move_ptr(&entry->iseq);
401 if (reference_updating) {
402 #if USE_YJIT
403 rb_yjit_iseq_update_references(iseq);
404 #endif
406 else {
407 #if USE_YJIT
408 rb_yjit_iseq_mark(body->yjit_payload);
409 #endif
413 if (FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) {
414 rb_gc_mark_and_move(&iseq->aux.loader.obj);
416 else if (FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) {
417 const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
419 rb_iseq_mark_and_move_insn_storage(compile_data->insn.storage_head);
420 rb_iseq_mark_and_move_each_compile_data_value(iseq, reference_updating ? ISEQ_ORIGINAL_ISEQ(iseq) : NULL);
422 rb_gc_mark_and_move((VALUE *)&compile_data->err_info);
423 rb_gc_mark_and_move((VALUE *)&compile_data->catch_table_ary);
425 else {
426 /* executable */
427 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
429 if (iseq->aux.exec.local_hooks) {
430 rb_hook_list_mark_and_update(iseq->aux.exec.local_hooks);
434 RUBY_MARK_LEAVE("iseq");
437 static size_t
438 param_keyword_size(const struct rb_iseq_param_keyword *pkw)
440 size_t size = 0;
442 if (!pkw) return size;
444 size += sizeof(struct rb_iseq_param_keyword);
445 size += sizeof(VALUE) * (pkw->num - pkw->required_num);
447 return size;
450 size_t
451 rb_iseq_memsize(const rb_iseq_t *iseq)
453 size_t size = 0; /* struct already counted as RVALUE size */
454 const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
455 const struct iseq_compile_data *compile_data;
457 /* TODO: should we count original_iseq? */
459 if (ISEQ_EXECUTABLE_P(iseq) && body) {
460 size += sizeof(struct rb_iseq_constant_body);
461 size += body->iseq_size * sizeof(VALUE);
462 size += body->insns_info.size * (sizeof(struct iseq_insn_info_entry) + sizeof(unsigned int));
463 size += body->local_table_size * sizeof(ID);
464 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
465 if (body->catch_table) {
466 size += iseq_catch_table_bytes(body->catch_table->size);
468 size += (body->param.opt_num + 1) * sizeof(VALUE);
469 size += param_keyword_size(body->param.keyword);
471 /* body->is_entries */
472 size += ISEQ_IS_SIZE(body) * sizeof(union iseq_inline_storage_entry);
474 if (ISEQ_BODY(iseq)->is_entries) {
475 /* IC entries constant segments */
476 for (unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
477 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
478 const ID *ids = ic->segments;
479 if (!ids) continue;
480 while (*ids++) {
481 size += sizeof(ID);
483 size += sizeof(ID); // null terminator
487 /* body->call_data */
488 size += body->ci_size * sizeof(struct rb_call_data);
489 // TODO: should we count imemo_callinfo?
492 compile_data = ISEQ_COMPILE_DATA(iseq);
493 if (compile_data) {
494 struct iseq_compile_data_storage *cur;
496 size += sizeof(struct iseq_compile_data);
498 cur = compile_data->node.storage_head;
499 while (cur) {
500 size += cur->size + offsetof(struct iseq_compile_data_storage, buff);
501 cur = cur->next;
505 return size;
508 struct rb_iseq_constant_body *
509 rb_iseq_constant_body_alloc(void)
511 struct rb_iseq_constant_body *iseq_body;
512 iseq_body = ZALLOC(struct rb_iseq_constant_body);
513 return iseq_body;
516 static rb_iseq_t *
517 iseq_alloc(void)
519 rb_iseq_t *iseq = iseq_imemo_alloc();
520 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
521 return iseq;
524 VALUE
525 rb_iseq_pathobj_new(VALUE path, VALUE realpath)
527 VALUE pathobj;
528 VM_ASSERT(RB_TYPE_P(path, T_STRING));
529 VM_ASSERT(NIL_P(realpath) || RB_TYPE_P(realpath, T_STRING));
531 if (path == realpath ||
532 (!NIL_P(realpath) && rb_str_cmp(path, realpath) == 0)) {
533 pathobj = rb_fstring(path);
535 else {
536 if (!NIL_P(realpath)) realpath = rb_fstring(realpath);
537 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
538 rb_ary_freeze(pathobj);
540 return pathobj;
543 void
544 rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath)
546 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->location.pathobj,
547 rb_iseq_pathobj_new(path, realpath));
550 // Make a dummy iseq for a dummy frame that exposes a path for profilers to inspect
551 rb_iseq_t *
552 rb_iseq_alloc_with_dummy_path(VALUE fname)
554 rb_iseq_t *dummy_iseq = iseq_alloc();
556 ISEQ_BODY(dummy_iseq)->type = ISEQ_TYPE_TOP;
557 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.pathobj, fname);
558 RB_OBJ_WRITE(dummy_iseq, &ISEQ_BODY(dummy_iseq)->location.label, fname);
560 return dummy_iseq;
563 static rb_iseq_location_t *
564 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)
566 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
568 rb_iseq_pathobj_set(iseq, path, realpath);
569 RB_OBJ_WRITE(iseq, &loc->label, name);
570 RB_OBJ_WRITE(iseq, &loc->base_label, name);
571 loc->first_lineno = first_lineno;
573 if (ISEQ_BODY(iseq)->local_iseq == iseq && strcmp(RSTRING_PTR(name), "initialize") == 0) {
574 ISEQ_BODY(iseq)->param.flags.use_block = 1;
577 if (code_location) {
578 loc->node_id = node_id;
579 loc->code_location = *code_location;
581 else {
582 loc->code_location.beg_pos.lineno = 0;
583 loc->code_location.beg_pos.column = 0;
584 loc->code_location.end_pos.lineno = -1;
585 loc->code_location.end_pos.column = -1;
588 return loc;
591 static void
592 set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
594 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
595 const VALUE type = body->type;
597 /* set class nest stack */
598 if (type == ISEQ_TYPE_TOP) {
599 body->local_iseq = iseq;
601 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
602 body->local_iseq = iseq;
604 else if (piseq) {
605 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
608 if (piseq) {
609 body->parent_iseq = piseq;
612 if (type == ISEQ_TYPE_MAIN) {
613 body->local_iseq = iseq;
617 static struct iseq_compile_data_storage *
618 new_arena(void)
620 struct iseq_compile_data_storage * new_arena =
621 (struct iseq_compile_data_storage *)
622 ALLOC_N(char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
623 offsetof(struct iseq_compile_data_storage, buff));
625 new_arena->pos = 0;
626 new_arena->next = 0;
627 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
629 return new_arena;
632 static VALUE
633 prepare_iseq_build(rb_iseq_t *iseq,
634 VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id,
635 const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type type,
636 VALUE script_lines, const rb_compile_option_t *option)
638 VALUE coverage = Qfalse;
639 VALUE err_info = Qnil;
640 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
642 if (parent && (type == ISEQ_TYPE_MAIN || type == ISEQ_TYPE_TOP))
643 err_info = Qfalse;
645 body->type = type;
646 set_relation(iseq, parent);
648 name = rb_fstring(name);
649 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
650 if (iseq != body->local_iseq) {
651 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
653 ISEQ_COVERAGE_SET(iseq, Qnil);
654 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
655 body->variable.flip_count = 0;
657 if (NIL_P(script_lines)) {
658 RB_OBJ_WRITE(iseq, &body->variable.script_lines, Qnil);
660 else {
661 RB_OBJ_WRITE(iseq, &body->variable.script_lines, rb_ractor_make_shareable(script_lines));
664 ISEQ_COMPILE_DATA_ALLOC(iseq);
665 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
666 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, Qnil);
668 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
669 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
670 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
671 ISEQ_COMPILE_DATA(iseq)->option = option;
672 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
673 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
675 if (option->coverage_enabled) {
676 VALUE coverages = rb_get_coverages();
677 if (RTEST(coverages)) {
678 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
679 if (NIL_P(coverage)) coverage = Qfalse;
682 ISEQ_COVERAGE_SET(iseq, coverage);
683 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
684 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_hidden_new(0));
686 return Qtrue;
689 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
690 static void validate_get_insn_info(const rb_iseq_t *iseq);
691 #endif
693 void
694 rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq)
696 #if VM_INSN_INFO_TABLE_IMPL == 2
697 /* create succ_index_table */
698 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
699 int size = body->insns_info.size;
700 int max_pos = body->iseq_size;
701 int *data = (int *)body->insns_info.positions;
702 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
703 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
704 #if VM_CHECK_MODE == 0
705 ruby_xfree(body->insns_info.positions);
706 body->insns_info.positions = NULL;
707 #endif
708 #endif
711 #if VM_INSN_INFO_TABLE_IMPL == 2
712 unsigned int *
713 rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body)
715 int size = body->insns_info.size;
716 int max_pos = body->iseq_size;
717 struct succ_index_table *sd = body->insns_info.succ_index_table;
718 return succ_index_table_invert(max_pos, sd, size);
720 #endif
722 void
723 rb_iseq_init_trace(rb_iseq_t *iseq)
725 iseq->aux.exec.global_trace_events = 0;
726 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
727 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
731 static VALUE
732 finish_iseq_build(rb_iseq_t *iseq)
734 struct iseq_compile_data *data = ISEQ_COMPILE_DATA(iseq);
735 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
736 VALUE err = data->err_info;
737 ISEQ_COMPILE_DATA_CLEAR(iseq);
738 compile_data_free(data);
740 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
741 validate_get_insn_info(iseq);
742 #endif
744 if (RTEST(err)) {
745 VALUE path = pathobj_path(body->location.pathobj);
746 if (err == Qtrue) err = rb_exc_new_cstr(rb_eSyntaxError, "compile error");
747 rb_funcallv(err, rb_intern("set_backtrace"), 1, &path);
748 rb_exc_raise(err);
751 RB_DEBUG_COUNTER_INC(iseq_num);
752 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
754 rb_iseq_init_trace(iseq);
755 return Qtrue;
758 static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
759 .inline_const_cache = OPT_INLINE_CONST_CACHE,
760 .peephole_optimization = OPT_PEEPHOLE_OPTIMIZATION,
761 .tailcall_optimization = OPT_TAILCALL_OPTIMIZATION,
762 .specialized_instruction = OPT_SPECIALISED_INSTRUCTION,
763 .operands_unification = OPT_OPERANDS_UNIFICATION,
764 .instructions_unification = OPT_INSTRUCTIONS_UNIFICATION,
765 .frozen_string_literal = OPT_FROZEN_STRING_LITERAL,
766 .debug_frozen_string_literal = OPT_DEBUG_FROZEN_STRING_LITERAL,
767 .coverage_enabled = TRUE,
770 static const rb_compile_option_t COMPILE_OPTION_FALSE = {
771 .frozen_string_literal = -1, // unspecified
775 rb_iseq_opt_frozen_string_literal(void)
777 return COMPILE_OPTION_DEFAULT.frozen_string_literal;
780 static void
781 set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt)
783 #define SET_COMPILE_OPTION(o, h, mem) \
784 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
785 if (flag == Qtrue) { (o)->mem = 1; } \
786 else if (flag == Qfalse) { (o)->mem = 0; } \
788 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
789 { VALUE num = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
790 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
792 SET_COMPILE_OPTION(option, opt, inline_const_cache);
793 SET_COMPILE_OPTION(option, opt, peephole_optimization);
794 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
795 SET_COMPILE_OPTION(option, opt, specialized_instruction);
796 SET_COMPILE_OPTION(option, opt, operands_unification);
797 SET_COMPILE_OPTION(option, opt, instructions_unification);
798 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
799 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
800 SET_COMPILE_OPTION(option, opt, coverage_enabled);
801 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
802 #undef SET_COMPILE_OPTION
803 #undef SET_COMPILE_OPTION_NUM
806 static rb_compile_option_t *
807 set_compile_option_from_ast(rb_compile_option_t *option, const rb_ast_body_t *ast)
809 #define SET_COMPILE_OPTION(o, a, mem) \
810 ((a)->mem < 0 ? 0 : ((o)->mem = (a)->mem > 0))
811 SET_COMPILE_OPTION(option, ast, coverage_enabled);
812 #undef SET_COMPILE_OPTION
813 if (ast->frozen_string_literal >= 0) {
814 option->frozen_string_literal = ast->frozen_string_literal;
816 return option;
819 static void
820 make_compile_option(rb_compile_option_t *option, VALUE opt)
822 if (NIL_P(opt)) {
823 *option = COMPILE_OPTION_DEFAULT;
825 else if (opt == Qfalse) {
826 *option = COMPILE_OPTION_FALSE;
828 else if (opt == Qtrue) {
829 int i;
830 for (i = 0; i < (int)(sizeof(rb_compile_option_t) / sizeof(int)); ++i)
831 ((int *)option)[i] = 1;
833 else if (RB_TYPE_P(opt, T_HASH)) {
834 *option = COMPILE_OPTION_DEFAULT;
835 set_compile_option_from_hash(option, opt);
837 else {
838 rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
842 static VALUE
843 make_compile_option_value(rb_compile_option_t *option)
845 VALUE opt = rb_hash_new_with_size(11);
846 #define SET_COMPILE_OPTION(o, h, mem) \
847 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
848 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
849 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
851 SET_COMPILE_OPTION(option, opt, inline_const_cache);
852 SET_COMPILE_OPTION(option, opt, peephole_optimization);
853 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
854 SET_COMPILE_OPTION(option, opt, specialized_instruction);
855 SET_COMPILE_OPTION(option, opt, operands_unification);
856 SET_COMPILE_OPTION(option, opt, instructions_unification);
857 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
858 SET_COMPILE_OPTION(option, opt, coverage_enabled);
859 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
861 #undef SET_COMPILE_OPTION
862 #undef SET_COMPILE_OPTION_NUM
863 VALUE frozen_string_literal = option->frozen_string_literal == -1 ? Qnil : RBOOL(option->frozen_string_literal);
864 rb_hash_aset(opt, ID2SYM(rb_intern("frozen_string_literal")), frozen_string_literal);
865 return opt;
868 rb_iseq_t *
869 rb_iseq_new(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath,
870 const rb_iseq_t *parent, enum rb_iseq_type type)
872 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent,
873 0, type, &COMPILE_OPTION_DEFAULT,
874 Qnil);
877 static int
878 ast_line_count(const VALUE ast_value)
880 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
881 return ast->body.line_count;
884 static VALUE
885 iseq_setup_coverage(VALUE coverages, VALUE path, int line_count)
887 if (line_count >= 0) {
888 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
890 VALUE coverage = rb_default_coverage(len);
891 rb_hash_aset(coverages, path, coverage);
893 return coverage;
896 return Qnil;
899 static inline void
900 iseq_new_setup_coverage(VALUE path, int line_count)
902 VALUE coverages = rb_get_coverages();
904 if (RTEST(coverages)) {
905 iseq_setup_coverage(coverages, path, line_count);
909 rb_iseq_t *
910 rb_iseq_new_top(const VALUE ast_value, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent)
912 iseq_new_setup_coverage(path, ast_line_count(ast_value));
914 return rb_iseq_new_with_opt(ast_value, name, path, realpath, 0, parent, 0,
915 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT,
916 Qnil);
920 * The main entry-point into the prism compiler when a file is required.
922 rb_iseq_t *
923 pm_iseq_new_top(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent, int *error_state)
925 iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1));
927 return pm_iseq_new_with_opt(node, name, path, realpath, 0, parent, 0,
928 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT, error_state);
931 rb_iseq_t *
932 rb_iseq_new_main(const VALUE ast_value, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt)
934 iseq_new_setup_coverage(path, ast_line_count(ast_value));
936 return rb_iseq_new_with_opt(ast_value, rb_fstring_lit("<main>"),
937 path, realpath, 0,
938 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE,
939 Qnil);
943 * The main entry-point into the prism compiler when a file is executed as the
944 * main file in the program.
946 rb_iseq_t *
947 pm_iseq_new_main(pm_scope_node_t *node, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt, int *error_state)
949 iseq_new_setup_coverage(path, (int) (node->parser->newline_list.size - 1));
951 return pm_iseq_new_with_opt(node, rb_fstring_lit("<main>"),
952 path, realpath, 0,
953 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE, error_state);
956 rb_iseq_t *
957 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)
959 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
960 VALUE coverages = rb_get_coverages();
961 if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
962 iseq_setup_coverage(coverages, path, ast_line_count(ast_value) + first_lineno - 1);
966 return rb_iseq_new_with_opt(ast_value, name, path, realpath, first_lineno,
967 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT,
968 Qnil);
971 rb_iseq_t *
972 pm_iseq_new_eval(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath,
973 int first_lineno, const rb_iseq_t *parent, int isolated_depth, int *error_state)
975 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
976 VALUE coverages = rb_get_coverages();
977 if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
978 iseq_setup_coverage(coverages, path, ((int) (node->parser->newline_list.size - 1)) + first_lineno - 1);
982 return pm_iseq_new_with_opt(node, name, path, realpath, first_lineno,
983 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT, error_state);
986 static inline rb_iseq_t *
987 iseq_translate(rb_iseq_t *iseq)
989 if (rb_respond_to(rb_cISeq, rb_intern("translate"))) {
990 VALUE v1 = iseqw_new(iseq);
991 VALUE v2 = rb_funcall(rb_cISeq, rb_intern("translate"), 1, v1);
992 if (v1 != v2 && CLASS_OF(v2) == rb_cISeq) {
993 iseq = (rb_iseq_t *)iseqw_check(v2);
997 return iseq;
1000 rb_iseq_t *
1001 rb_iseq_new_with_opt(VALUE ast_value, VALUE name, VALUE path, VALUE realpath,
1002 int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1003 enum rb_iseq_type type, const rb_compile_option_t *option,
1004 VALUE script_lines)
1006 rb_ast_t *ast = rb_ruby_ast_data_get(ast_value);
1007 rb_ast_body_t *body = ast ? &ast->body : NULL;
1008 const NODE *node = body ? body->root : 0;
1009 /* TODO: argument check */
1010 rb_iseq_t *iseq = iseq_alloc();
1011 rb_compile_option_t new_opt;
1013 if (!option) option = &COMPILE_OPTION_DEFAULT;
1014 if (body) {
1015 new_opt = *option;
1016 option = set_compile_option_from_ast(&new_opt, body);
1019 if (!NIL_P(script_lines)) {
1020 // noop
1022 else if (body && body->script_lines) {
1023 script_lines = rb_parser_build_script_lines_from(body->script_lines);
1025 else if (parent) {
1026 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1029 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
1030 parent, isolated_depth, type, script_lines, option);
1032 rb_iseq_compile_node(iseq, node);
1033 finish_iseq_build(iseq);
1034 RB_GC_GUARD(ast_value);
1036 return iseq_translate(iseq);
1039 struct pm_iseq_new_with_opt_data {
1040 rb_iseq_t *iseq;
1041 pm_scope_node_t *node;
1044 VALUE
1045 pm_iseq_new_with_opt_try(VALUE d)
1047 struct pm_iseq_new_with_opt_data *data = (struct pm_iseq_new_with_opt_data *)d;
1049 // This can compile child iseqs, which can raise syntax errors
1050 pm_iseq_compile_node(data->iseq, data->node);
1052 // This raises an exception if there is a syntax error
1053 finish_iseq_build(data->iseq);
1055 return Qundef;
1059 * This is a step in the prism compiler that is called once all of the various
1060 * options have been established. It is called from one of the pm_iseq_new_*
1061 * functions or from the RubyVM::InstructionSequence APIs. It is responsible for
1062 * allocating the instruction sequence, calling into the compiler, and returning
1063 * the built instruction sequence.
1065 * Importantly, this is also the function where the compiler is re-entered to
1066 * compile child instruction sequences. A child instruction sequence is always
1067 * compiled using a scope node, which is why we cast it explicitly to that here
1068 * in the parameters (as opposed to accepting a generic pm_node_t *).
1070 rb_iseq_t *
1071 pm_iseq_new_with_opt(pm_scope_node_t *node, VALUE name, VALUE path, VALUE realpath,
1072 int first_lineno, const rb_iseq_t *parent, int isolated_depth,
1073 enum rb_iseq_type type, const rb_compile_option_t *option, int *error_state)
1075 rb_iseq_t *iseq = iseq_alloc();
1076 ISEQ_BODY(iseq)->prism = true;
1078 rb_compile_option_t next_option;
1079 if (!option) option = &COMPILE_OPTION_DEFAULT;
1081 next_option = *option;
1082 next_option.coverage_enabled = node->coverage_enabled < 0 ? 0 : node->coverage_enabled > 0;
1083 option = &next_option;
1085 pm_location_t *location = &node->base.location;
1086 int32_t start_line = node->parser->start_line;
1088 pm_line_column_t start = pm_newline_list_line_column(&node->parser->newline_list, location->start, start_line);
1089 pm_line_column_t end = pm_newline_list_line_column(&node->parser->newline_list, location->end, start_line);
1091 rb_code_location_t code_location = (rb_code_location_t) {
1092 .beg_pos = { .lineno = (int) start.line, .column = (int) start.column },
1093 .end_pos = { .lineno = (int) end.line, .column = (int) end.column }
1096 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &code_location, node->ast_node->node_id,
1097 parent, isolated_depth, type, node->script_lines == NULL ? Qnil : *node->script_lines, option);
1099 struct pm_iseq_new_with_opt_data data = {
1100 .iseq = iseq,
1101 .node = node
1103 rb_protect(pm_iseq_new_with_opt_try, (VALUE)&data, error_state);
1105 if (*error_state) return NULL;
1107 return iseq_translate(iseq);
1110 rb_iseq_t *
1111 rb_iseq_new_with_callback(
1112 const struct rb_iseq_new_with_callback_callback_func * ifunc,
1113 VALUE name, VALUE path, VALUE realpath,
1114 int first_lineno, const rb_iseq_t *parent,
1115 enum rb_iseq_type type, const rb_compile_option_t *option)
1117 /* TODO: argument check */
1118 rb_iseq_t *iseq = iseq_alloc();
1120 if (!option) option = &COMPILE_OPTION_DEFAULT;
1121 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0, type, Qnil, option);
1123 rb_iseq_compile_callback(iseq, ifunc);
1124 finish_iseq_build(iseq);
1126 return iseq;
1129 const rb_iseq_t *
1130 rb_iseq_load_iseq(VALUE fname)
1132 VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname);
1134 if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) {
1135 return iseqw_check(iseqv);
1138 return NULL;
1141 #define CHECK_ARRAY(v) rb_to_array_type(v)
1142 #define CHECK_HASH(v) rb_to_hash_type(v)
1143 #define CHECK_STRING(v) rb_str_to_str(v)
1144 #define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1145 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
1147 static enum rb_iseq_type
1148 iseq_type_from_sym(VALUE type)
1150 const ID id_top = rb_intern("top");
1151 const ID id_method = rb_intern("method");
1152 const ID id_block = rb_intern("block");
1153 const ID id_class = rb_intern("class");
1154 const ID id_rescue = rb_intern("rescue");
1155 const ID id_ensure = rb_intern("ensure");
1156 const ID id_eval = rb_intern("eval");
1157 const ID id_main = rb_intern("main");
1158 const ID id_plain = rb_intern("plain");
1159 /* ensure all symbols are static or pinned down before
1160 * conversion */
1161 const ID typeid = rb_check_id(&type);
1162 if (typeid == id_top) return ISEQ_TYPE_TOP;
1163 if (typeid == id_method) return ISEQ_TYPE_METHOD;
1164 if (typeid == id_block) return ISEQ_TYPE_BLOCK;
1165 if (typeid == id_class) return ISEQ_TYPE_CLASS;
1166 if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
1167 if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
1168 if (typeid == id_eval) return ISEQ_TYPE_EVAL;
1169 if (typeid == id_main) return ISEQ_TYPE_MAIN;
1170 if (typeid == id_plain) return ISEQ_TYPE_PLAIN;
1171 return (enum rb_iseq_type)-1;
1174 static VALUE
1175 iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
1177 rb_iseq_t *iseq = iseq_alloc();
1179 VALUE magic, version1, version2, format_type, misc;
1180 VALUE name, path, realpath, code_location, node_id;
1181 VALUE type, body, locals, params, exception;
1183 st_data_t iseq_type;
1184 rb_compile_option_t option;
1185 int i = 0;
1186 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1188 /* [magic, major_version, minor_version, format_type, misc,
1189 * label, path, first_lineno,
1190 * type, locals, args, exception_table, body]
1193 data = CHECK_ARRAY(data);
1195 magic = CHECK_STRING(rb_ary_entry(data, i++));
1196 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1197 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1198 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1199 misc = CHECK_HASH(rb_ary_entry(data, i++));
1200 ((void)magic, (void)version1, (void)version2, (void)format_type);
1202 name = CHECK_STRING(rb_ary_entry(data, i++));
1203 path = CHECK_STRING(rb_ary_entry(data, i++));
1204 realpath = rb_ary_entry(data, i++);
1205 realpath = NIL_P(realpath) ? Qnil : CHECK_STRING(realpath);
1206 int first_lineno = RB_NUM2INT(rb_ary_entry(data, i++));
1208 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1209 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1210 params = CHECK_HASH(rb_ary_entry(data, i++));
1211 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1212 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1214 ISEQ_BODY(iseq)->local_iseq = iseq;
1216 iseq_type = iseq_type_from_sym(type);
1217 if (iseq_type == (enum rb_iseq_type)-1) {
1218 rb_raise(rb_eTypeError, "unsupported type: :%"PRIsVALUE, rb_sym2str(type));
1221 node_id = rb_hash_aref(misc, ID2SYM(rb_intern("node_id")));
1223 code_location = rb_hash_aref(misc, ID2SYM(rb_intern("code_location")));
1224 if (RB_TYPE_P(code_location, T_ARRAY) && RARRAY_LEN(code_location) == 4) {
1225 tmp_loc.beg_pos.lineno = NUM2INT(rb_ary_entry(code_location, 0));
1226 tmp_loc.beg_pos.column = NUM2INT(rb_ary_entry(code_location, 1));
1227 tmp_loc.end_pos.lineno = NUM2INT(rb_ary_entry(code_location, 2));
1228 tmp_loc.end_pos.column = NUM2INT(rb_ary_entry(code_location, 3));
1231 if (SYM2ID(rb_hash_aref(misc, ID2SYM(rb_intern("parser")))) == rb_intern("prism")) {
1232 ISEQ_BODY(iseq)->prism = true;
1235 make_compile_option(&option, opt);
1236 option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
1237 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc, NUM2INT(node_id),
1238 parent, 0, (enum rb_iseq_type)iseq_type, Qnil, &option);
1240 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1242 finish_iseq_build(iseq);
1244 return iseqw_new(iseq);
1248 * :nodoc:
1250 static VALUE
1251 iseq_s_load(int argc, VALUE *argv, VALUE self)
1253 VALUE data, opt=Qnil;
1254 rb_scan_args(argc, argv, "11", &data, &opt);
1255 return iseq_load(data, NULL, opt);
1258 VALUE
1259 rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
1261 return iseq_load(data, RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1264 static rb_iseq_t *
1265 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1267 rb_iseq_t *iseq = NULL;
1268 rb_compile_option_t option;
1269 #if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1270 # define INITIALIZED volatile /* suppress warnings by gcc 4.8 */
1271 #else
1272 # define INITIALIZED /* volatile */
1273 #endif
1274 VALUE (*parse)(VALUE vparser, VALUE fname, VALUE file, int start);
1275 int ln;
1276 VALUE INITIALIZED ast_value;
1277 rb_ast_t *ast;
1278 VALUE name = rb_fstring_lit("<compiled>");
1280 /* safe results first */
1281 make_compile_option(&option, opt);
1282 ln = NUM2INT(line);
1283 StringValueCStr(file);
1284 if (RB_TYPE_P(src, T_FILE)) {
1285 parse = rb_parser_compile_file_path;
1287 else {
1288 parse = rb_parser_compile_string_path;
1289 StringValue(src);
1292 const VALUE parser = rb_parser_new();
1293 const rb_iseq_t *outer_scope = rb_iseq_new(Qnil, name, name, Qnil, 0, ISEQ_TYPE_TOP);
1294 VALUE outer_scope_v = (VALUE)outer_scope;
1295 rb_parser_set_context(parser, outer_scope, FALSE);
1296 if (ruby_vm_keep_script_lines) rb_parser_set_script_lines(parser);
1297 RB_GC_GUARD(outer_scope_v);
1298 ast_value = (*parse)(parser, file, src, ln);
1301 ast = rb_ruby_ast_data_get(ast_value);
1303 if (!ast || !ast->body.root) {
1304 rb_ast_dispose(ast);
1305 rb_exc_raise(GET_EC()->errinfo);
1307 else {
1308 iseq = rb_iseq_new_with_opt(ast_value, name, file, realpath, ln,
1309 NULL, 0, ISEQ_TYPE_TOP, &option,
1310 Qnil);
1311 rb_ast_dispose(ast);
1314 return iseq;
1317 static rb_iseq_t *
1318 pm_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1320 rb_iseq_t *iseq = NULL;
1321 rb_compile_option_t option;
1322 int ln;
1323 VALUE name = rb_fstring_lit("<compiled>");
1325 /* safe results first */
1326 make_compile_option(&option, opt);
1327 ln = NUM2INT(line);
1328 StringValueCStr(file);
1330 pm_parse_result_t result = { 0 };
1331 pm_options_line_set(&result.options, NUM2INT(line));
1332 pm_options_scopes_init(&result.options, 1);
1333 result.node.coverage_enabled = 1;
1335 switch (option.frozen_string_literal) {
1336 case ISEQ_FROZEN_STRING_LITERAL_UNSET:
1337 break;
1338 case ISEQ_FROZEN_STRING_LITERAL_DISABLED:
1339 pm_options_frozen_string_literal_set(&result.options, false);
1340 break;
1341 case ISEQ_FROZEN_STRING_LITERAL_ENABLED:
1342 pm_options_frozen_string_literal_set(&result.options, true);
1343 break;
1344 default:
1345 rb_bug("pm_iseq_compile_with_option: invalid frozen_string_literal=%d", option.frozen_string_literal);
1346 break;
1349 VALUE script_lines;
1350 VALUE error;
1352 if (RB_TYPE_P(src, T_FILE)) {
1353 VALUE filepath = rb_io_path(src);
1354 error = pm_load_parse_file(&result, filepath, ruby_vm_keep_script_lines ? &script_lines : NULL);
1355 RB_GC_GUARD(filepath);
1357 else {
1358 src = StringValue(src);
1359 error = pm_parse_string(&result, src, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1362 if (error == Qnil) {
1363 int error_state;
1364 iseq = pm_iseq_new_with_opt(&result.node, name, file, realpath, ln, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1366 pm_parse_result_free(&result);
1368 if (error_state) {
1369 RUBY_ASSERT(iseq == NULL);
1370 rb_jump_tag(error_state);
1373 else {
1374 pm_parse_result_free(&result);
1375 rb_exc_raise(error);
1378 return iseq;
1381 VALUE
1382 rb_iseq_path(const rb_iseq_t *iseq)
1384 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1387 VALUE
1388 rb_iseq_realpath(const rb_iseq_t *iseq)
1390 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1393 VALUE
1394 rb_iseq_absolute_path(const rb_iseq_t *iseq)
1396 return rb_iseq_realpath(iseq);
1400 rb_iseq_from_eval_p(const rb_iseq_t *iseq)
1402 return NIL_P(rb_iseq_realpath(iseq));
1405 VALUE
1406 rb_iseq_label(const rb_iseq_t *iseq)
1408 return ISEQ_BODY(iseq)->location.label;
1411 VALUE
1412 rb_iseq_base_label(const rb_iseq_t *iseq)
1414 return ISEQ_BODY(iseq)->location.base_label;
1417 VALUE
1418 rb_iseq_first_lineno(const rb_iseq_t *iseq)
1420 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1423 VALUE
1424 rb_iseq_method_name(const rb_iseq_t *iseq)
1426 struct rb_iseq_constant_body *const body = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq);
1428 if (body->type == ISEQ_TYPE_METHOD) {
1429 return body->location.base_label;
1431 else {
1432 return Qnil;
1436 void
1437 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)
1439 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1440 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1441 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1442 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1443 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1446 static ID iseq_type_id(enum rb_iseq_type type);
1448 VALUE
1449 rb_iseq_type(const rb_iseq_t *iseq)
1451 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->type));
1454 VALUE
1455 rb_iseq_coverage(const rb_iseq_t *iseq)
1457 return ISEQ_COVERAGE(iseq);
1460 static int
1461 remove_coverage_i(void *vstart, void *vend, size_t stride, void *data)
1463 VALUE v = (VALUE)vstart;
1464 for (; v != (VALUE)vend; v += stride) {
1465 void *ptr = rb_asan_poisoned_object_p(v);
1466 rb_asan_unpoison_object(v, false);
1468 if (rb_obj_is_iseq(v)) {
1469 rb_iseq_t *iseq = (rb_iseq_t *)v;
1470 ISEQ_COVERAGE_SET(iseq, Qnil);
1473 asan_poison_object_if(ptr, v);
1475 return 0;
1478 void
1479 rb_iseq_remove_coverage_all(void)
1481 rb_objspace_each_objects(remove_coverage_i, NULL);
1484 /* define wrapper class methods (RubyVM::InstructionSequence) */
1486 static void
1487 iseqw_mark(void *ptr)
1489 rb_gc_mark_movable(*(VALUE *)ptr);
1492 static size_t
1493 iseqw_memsize(const void *ptr)
1495 return rb_iseq_memsize(*(const rb_iseq_t **)ptr);
1498 static void
1499 iseqw_ref_update(void *ptr)
1501 VALUE *vptr = ptr;
1502 *vptr = rb_gc_location(*vptr);
1505 static const rb_data_type_t iseqw_data_type = {
1506 "T_IMEMO/iseq",
1508 iseqw_mark,
1509 RUBY_TYPED_DEFAULT_FREE,
1510 iseqw_memsize,
1511 iseqw_ref_update,
1513 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1516 static VALUE
1517 iseqw_new(const rb_iseq_t *iseq)
1519 if (iseq->wrapper) {
1520 if (*(const rb_iseq_t **)rb_check_typeddata(iseq->wrapper, &iseqw_data_type) != iseq) {
1521 rb_raise(rb_eTypeError, "wrong iseq wrapper: %" PRIsVALUE " for %p",
1522 iseq->wrapper, (void *)iseq);
1524 return iseq->wrapper;
1526 else {
1527 rb_iseq_t **ptr;
1528 VALUE obj = TypedData_Make_Struct(rb_cISeq, rb_iseq_t *, &iseqw_data_type, ptr);
1529 RB_OBJ_WRITE(obj, ptr, iseq);
1531 /* cache a wrapper object */
1532 RB_OBJ_WRITE((VALUE)iseq, &iseq->wrapper, obj);
1533 RB_OBJ_FREEZE((VALUE)iseq);
1535 return obj;
1539 VALUE
1540 rb_iseqw_new(const rb_iseq_t *iseq)
1542 return iseqw_new(iseq);
1546 * Accept the options given to InstructionSequence.compile and
1547 * InstructionSequence.compile_prism and share the logic for creating the
1548 * instruction sequence.
1550 static VALUE
1551 iseqw_s_compile_parser(int argc, VALUE *argv, VALUE self, bool prism)
1553 VALUE src, file = Qnil, path = Qnil, line = Qnil, opt = Qnil;
1554 int i;
1556 i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt);
1557 if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5);
1558 switch (i) {
1559 case 5: opt = argv[--i];
1560 case 4: line = argv[--i];
1561 case 3: path = argv[--i];
1562 case 2: file = argv[--i];
1565 if (NIL_P(file)) file = rb_fstring_lit("<compiled>");
1566 if (NIL_P(path)) path = file;
1567 if (NIL_P(line)) line = INT2FIX(1);
1569 Check_Type(path, T_STRING);
1570 Check_Type(file, T_STRING);
1572 rb_iseq_t *iseq;
1573 if (prism) {
1574 iseq = pm_iseq_compile_with_option(src, file, path, line, opt);
1576 else {
1577 iseq = rb_iseq_compile_with_option(src, file, path, line, opt);
1580 return iseqw_new(iseq);
1584 * call-seq:
1585 * InstructionSequence.compile(source[, file[, path[, line[, options]]]]) -> iseq
1586 * InstructionSequence.new(source[, file[, path[, line[, options]]]]) -> iseq
1588 * Takes +source+, which can be a string of Ruby code, or an open +File+ object.
1589 * that contains Ruby source code.
1591 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1592 * real path and first line number of the ruby code in +source+ which are
1593 * metadata attached to the returned +iseq+.
1595 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1596 * +require_relative+ base. It is recommended these should be the same full
1597 * path.
1599 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1600 * modify the default behavior of the Ruby iseq compiler.
1602 * For details regarding valid compile options see ::compile_option=.
1604 * RubyVM::InstructionSequence.compile("a = 1 + 2")
1605 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1607 * path = "test.rb"
1608 * RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
1609 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1611 * file = File.open("test.rb")
1612 * RubyVM::InstructionSequence.compile(file)
1613 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
1615 * path = File.expand_path("test.rb")
1616 * RubyVM::InstructionSequence.compile(File.read(path), path, path)
1617 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1620 static VALUE
1621 iseqw_s_compile(int argc, VALUE *argv, VALUE self)
1623 return iseqw_s_compile_parser(argc, argv, self, rb_ruby_prism_p());
1627 * call-seq:
1628 * InstructionSequence.compile_parsey(source[, file[, path[, line[, options]]]]) -> iseq
1630 * Takes +source+, which can be a string of Ruby code, or an open +File+ object.
1631 * that contains Ruby source code. It parses and compiles using parse.y.
1633 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1634 * real path and first line number of the ruby code in +source+ which are
1635 * metadata attached to the returned +iseq+.
1637 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1638 * +require_relative+ base. It is recommended these should be the same full
1639 * path.
1641 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1642 * modify the default behavior of the Ruby iseq compiler.
1644 * For details regarding valid compile options see ::compile_option=.
1646 * RubyVM::InstructionSequence.compile_parsey("a = 1 + 2")
1647 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1649 * path = "test.rb"
1650 * RubyVM::InstructionSequence.compile_parsey(File.read(path), path, File.expand_path(path))
1651 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1653 * file = File.open("test.rb")
1654 * RubyVM::InstructionSequence.compile_parsey(file)
1655 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
1657 * path = File.expand_path("test.rb")
1658 * RubyVM::InstructionSequence.compile_parsey(File.read(path), path, path)
1659 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1662 static VALUE
1663 iseqw_s_compile_parsey(int argc, VALUE *argv, VALUE self)
1665 return iseqw_s_compile_parser(argc, argv, self, false);
1669 * call-seq:
1670 * InstructionSequence.compile_prism(source[, file[, path[, line[, options]]]]) -> iseq
1672 * Takes +source+, which can be a string of Ruby code, or an open +File+ object.
1673 * that contains Ruby source code. It parses and compiles using prism.
1675 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1676 * real path and first line number of the ruby code in +source+ which are
1677 * metadata attached to the returned +iseq+.
1679 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1680 * +require_relative+ base. It is recommended these should be the same full
1681 * path.
1683 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1684 * modify the default behavior of the Ruby iseq compiler.
1686 * For details regarding valid compile options see ::compile_option=.
1688 * RubyVM::InstructionSequence.compile_prism("a = 1 + 2")
1689 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1691 * path = "test.rb"
1692 * RubyVM::InstructionSequence.compile_prism(File.read(path), path, File.expand_path(path))
1693 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1695 * file = File.open("test.rb")
1696 * RubyVM::InstructionSequence.compile_prism(file)
1697 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>:1>
1699 * path = File.expand_path("test.rb")
1700 * RubyVM::InstructionSequence.compile_prism(File.read(path), path, path)
1701 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1704 static VALUE
1705 iseqw_s_compile_prism(int argc, VALUE *argv, VALUE self)
1707 return iseqw_s_compile_parser(argc, argv, self, true);
1711 * call-seq:
1712 * InstructionSequence.compile_file(file[, options]) -> iseq
1714 * Takes +file+, a String with the location of a Ruby source file, reads,
1715 * parses and compiles the file, and returns +iseq+, the compiled
1716 * InstructionSequence with source location metadata set.
1718 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1719 * modify the default behavior of the Ruby iseq compiler.
1721 * For details regarding valid compile options see ::compile_option=.
1723 * # /tmp/hello.rb
1724 * puts "Hello, world!"
1726 * # elsewhere
1727 * RubyVM::InstructionSequence.compile_file("/tmp/hello.rb")
1728 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1730 static VALUE
1731 iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
1733 VALUE file, opt = Qnil;
1734 VALUE parser, f, exc = Qnil, ret;
1735 rb_ast_t *ast;
1736 VALUE ast_value;
1737 rb_compile_option_t option;
1738 int i;
1740 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1741 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1742 switch (i) {
1743 case 2: opt = argv[--i];
1745 FilePathValue(file);
1746 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1748 f = rb_file_open_str(file, "r");
1750 rb_execution_context_t *ec = GET_EC();
1751 VALUE v = rb_vm_push_frame_fname(ec, file);
1753 parser = rb_parser_new();
1754 rb_parser_set_context(parser, NULL, FALSE);
1755 ast_value = rb_parser_load_file(parser, file);
1756 ast = rb_ruby_ast_data_get(ast_value);
1757 if (!ast->body.root) exc = GET_EC()->errinfo;
1759 rb_io_close(f);
1760 if (!ast->body.root) {
1761 rb_ast_dispose(ast);
1762 rb_exc_raise(exc);
1765 make_compile_option(&option, opt);
1767 ret = iseqw_new(rb_iseq_new_with_opt(ast_value, rb_fstring_lit("<main>"),
1768 file,
1769 rb_realpath_internal(Qnil, file, 1),
1770 1, NULL, 0, ISEQ_TYPE_TOP, &option,
1771 Qnil));
1772 rb_ast_dispose(ast);
1773 RB_GC_GUARD(ast_value);
1775 rb_vm_pop_frame(ec);
1776 RB_GC_GUARD(v);
1777 return ret;
1781 * call-seq:
1782 * InstructionSequence.compile_file_prism(file[, options]) -> iseq
1784 * Takes +file+, a String with the location of a Ruby source file, reads,
1785 * parses and compiles the file, and returns +iseq+, the compiled
1786 * InstructionSequence with source location metadata set. It parses and
1787 * compiles using prism.
1789 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1790 * modify the default behavior of the Ruby iseq compiler.
1792 * For details regarding valid compile options see ::compile_option=.
1794 * # /tmp/hello.rb
1795 * puts "Hello, world!"
1797 * # elsewhere
1798 * RubyVM::InstructionSequence.compile_file_prism("/tmp/hello.rb")
1799 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1801 static VALUE
1802 iseqw_s_compile_file_prism(int argc, VALUE *argv, VALUE self)
1804 VALUE file, opt = Qnil, ret;
1805 rb_compile_option_t option;
1806 int i;
1808 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1809 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1810 switch (i) {
1811 case 2: opt = argv[--i];
1813 FilePathValue(file);
1814 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1816 rb_execution_context_t *ec = GET_EC();
1817 VALUE v = rb_vm_push_frame_fname(ec, file);
1819 pm_parse_result_t result = { 0 };
1820 result.options.line = 1;
1821 result.node.coverage_enabled = 1;
1823 VALUE script_lines;
1824 VALUE error = pm_load_parse_file(&result, file, ruby_vm_keep_script_lines ? &script_lines : NULL);
1826 if (error == Qnil) {
1827 make_compile_option(&option, opt);
1829 int error_state;
1830 rb_iseq_t *iseq = pm_iseq_new_with_opt(&result.node, rb_fstring_lit("<main>"),
1831 file,
1832 rb_realpath_internal(Qnil, file, 1),
1833 1, NULL, 0, ISEQ_TYPE_TOP, &option, &error_state);
1835 pm_parse_result_free(&result);
1837 if (error_state) {
1838 RUBY_ASSERT(iseq == NULL);
1839 rb_jump_tag(error_state);
1842 ret = iseqw_new(iseq);
1843 rb_vm_pop_frame(ec);
1844 RB_GC_GUARD(v);
1845 return ret;
1847 else {
1848 pm_parse_result_free(&result);
1849 rb_vm_pop_frame(ec);
1850 RB_GC_GUARD(v);
1851 rb_exc_raise(error);
1856 * call-seq:
1857 * InstructionSequence.compile_option = options
1859 * Sets the default values for various optimizations in the Ruby iseq
1860 * compiler.
1862 * Possible values for +options+ include +true+, which enables all options,
1863 * +false+ which disables all options, and +nil+ which leaves all options
1864 * unchanged.
1866 * You can also pass a +Hash+ of +options+ that you want to change, any
1867 * options not present in the hash will be left unchanged.
1869 * Possible option names (which are keys in +options+) which can be set to
1870 * +true+ or +false+ include:
1872 * * +:inline_const_cache+
1873 * * +:instructions_unification+
1874 * * +:operands_unification+
1875 * * +:peephole_optimization+
1876 * * +:specialized_instruction+
1877 * * +:tailcall_optimization+
1879 * Additionally, +:debug_level+ can be set to an integer.
1881 * These default options can be overwritten for a single run of the iseq
1882 * compiler by passing any of the above values as the +options+ parameter to
1883 * ::new, ::compile and ::compile_file.
1885 static VALUE
1886 iseqw_s_compile_option_set(VALUE self, VALUE opt)
1888 rb_compile_option_t option;
1889 make_compile_option(&option, opt);
1890 COMPILE_OPTION_DEFAULT = option;
1891 return opt;
1895 * call-seq:
1896 * InstructionSequence.compile_option -> options
1898 * Returns a hash of default options used by the Ruby iseq compiler.
1900 * For details, see InstructionSequence.compile_option=.
1902 static VALUE
1903 iseqw_s_compile_option_get(VALUE self)
1905 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1908 static const rb_iseq_t *
1909 iseqw_check(VALUE iseqw)
1911 rb_iseq_t **iseq_ptr;
1912 TypedData_Get_Struct(iseqw, rb_iseq_t *, &iseqw_data_type, iseq_ptr);
1913 rb_iseq_t *iseq = *iseq_ptr;
1915 if (!ISEQ_BODY(iseq)) {
1916 rb_ibf_load_iseq_complete(iseq);
1919 if (!ISEQ_BODY(iseq)->location.label) {
1920 rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
1922 return iseq;
1925 const rb_iseq_t *
1926 rb_iseqw_to_iseq(VALUE iseqw)
1928 return iseqw_check(iseqw);
1932 * call-seq:
1933 * iseq.eval -> obj
1935 * Evaluates the instruction sequence and returns the result.
1937 * RubyVM::InstructionSequence.compile("1 + 2").eval #=> 3
1939 static VALUE
1940 iseqw_eval(VALUE self)
1942 const rb_iseq_t *iseq = iseqw_check(self);
1943 if (0 == ISEQ_BODY(iseq)->iseq_size) {
1944 rb_raise(rb_eTypeError, "attempt to evaluate dummy InstructionSequence");
1946 return rb_iseq_eval(iseq);
1950 * Returns a human-readable string representation of this instruction
1951 * sequence, including the #label and #path.
1953 static VALUE
1954 iseqw_inspect(VALUE self)
1956 const rb_iseq_t *iseq = iseqw_check(self);
1957 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1958 VALUE klass = rb_class_name(rb_obj_class(self));
1960 if (!body->location.label) {
1961 return rb_sprintf("#<%"PRIsVALUE": uninitialized>", klass);
1963 else {
1964 return rb_sprintf("<%"PRIsVALUE":%"PRIsVALUE"@%"PRIsVALUE":%d>",
1965 klass,
1966 body->location.label, rb_iseq_path(iseq),
1967 FIX2INT(rb_iseq_first_lineno(iseq)));
1972 * Returns the path of this instruction sequence.
1974 * <code><compiled></code> if the iseq was evaluated from a string.
1976 * For example, using irb:
1978 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1979 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1980 * iseq.path
1981 * #=> "<compiled>"
1983 * Using ::compile_file:
1985 * # /tmp/method.rb
1986 * def hello
1987 * puts "hello, world"
1988 * end
1990 * # in irb
1991 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1992 * > iseq.path #=> /tmp/method.rb
1994 static VALUE
1995 iseqw_path(VALUE self)
1997 return rb_iseq_path(iseqw_check(self));
2001 * Returns the absolute path of this instruction sequence.
2003 * +nil+ if the iseq was evaluated from a string.
2005 * For example, using ::compile_file:
2007 * # /tmp/method.rb
2008 * def hello
2009 * puts "hello, world"
2010 * end
2012 * # in irb
2013 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
2014 * > iseq.absolute_path #=> /tmp/method.rb
2016 static VALUE
2017 iseqw_absolute_path(VALUE self)
2019 return rb_iseq_realpath(iseqw_check(self));
2022 /* Returns the label of this instruction sequence.
2024 * <code><main></code> if it's at the top level, <code><compiled></code> if it
2025 * was evaluated from a string.
2027 * For example, using irb:
2029 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
2030 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
2031 * iseq.label
2032 * #=> "<compiled>"
2034 * Using ::compile_file:
2036 * # /tmp/method.rb
2037 * def hello
2038 * puts "hello, world"
2039 * end
2041 * # in irb
2042 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
2043 * > iseq.label #=> <main>
2045 static VALUE
2046 iseqw_label(VALUE self)
2048 return rb_iseq_label(iseqw_check(self));
2051 /* Returns the base label of this instruction sequence.
2053 * For example, using irb:
2055 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
2056 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
2057 * iseq.base_label
2058 * #=> "<compiled>"
2060 * Using ::compile_file:
2062 * # /tmp/method.rb
2063 * def hello
2064 * puts "hello, world"
2065 * end
2067 * # in irb
2068 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
2069 * > iseq.base_label #=> <main>
2071 static VALUE
2072 iseqw_base_label(VALUE self)
2074 return rb_iseq_base_label(iseqw_check(self));
2077 /* Returns the number of the first source line where the instruction sequence
2078 * was loaded from.
2080 * For example, using irb:
2082 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
2083 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
2084 * iseq.first_lineno
2085 * #=> 1
2087 static VALUE
2088 iseqw_first_lineno(VALUE self)
2090 return rb_iseq_first_lineno(iseqw_check(self));
2093 static VALUE iseq_data_to_ary(const rb_iseq_t *iseq);
2096 * call-seq:
2097 * iseq.to_a -> ary
2099 * Returns an Array with 14 elements representing the instruction sequence
2100 * with the following data:
2102 * [magic]
2103 * A string identifying the data format. <b>Always
2104 * +YARVInstructionSequence/SimpleDataFormat+.</b>
2106 * [major_version]
2107 * The major version of the instruction sequence.
2109 * [minor_version]
2110 * The minor version of the instruction sequence.
2112 * [format_type]
2113 * A number identifying the data format. <b>Always 1</b>.
2115 * [misc]
2116 * A hash containing:
2118 * [+:arg_size+]
2119 * the total number of arguments taken by the method or the block (0 if
2120 * _iseq_ doesn't represent a method or block)
2121 * [+:local_size+]
2122 * the number of local variables + 1
2123 * [+:stack_max+]
2124 * used in calculating the stack depth at which a SystemStackError is
2125 * thrown.
2127 * [#label]
2128 * The name of the context (block, method, class, module, etc.) that this
2129 * instruction sequence belongs to.
2131 * <code><main></code> if it's at the top level, <code><compiled></code> if
2132 * it was evaluated from a string.
2134 * [#path]
2135 * The relative path to the Ruby file where the instruction sequence was
2136 * loaded from.
2138 * <code><compiled></code> if the iseq was evaluated from a string.
2140 * [#absolute_path]
2141 * The absolute path to the Ruby file where the instruction sequence was
2142 * loaded from.
2144 * +nil+ if the iseq was evaluated from a string.
2146 * [#first_lineno]
2147 * The number of the first source line where the instruction sequence was
2148 * loaded from.
2150 * [type]
2151 * The type of the instruction sequence.
2153 * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
2154 * +:ensure+, +:eval+, +:main+, and +plain+.
2156 * [locals]
2157 * An array containing the names of all arguments and local variables as
2158 * symbols.
2160 * [params]
2161 * An Hash object containing parameter information.
2163 * More info about these values can be found in +vm_core.h+.
2165 * [catch_table]
2166 * A list of exceptions and control flow operators (rescue, next, redo,
2167 * break, etc.).
2169 * [bytecode]
2170 * An array of arrays containing the instruction names and operands that
2171 * make up the body of the instruction sequence.
2173 * Note that this format is MRI specific and version dependent.
2176 static VALUE
2177 iseqw_to_a(VALUE self)
2179 const rb_iseq_t *iseq = iseqw_check(self);
2180 return iseq_data_to_ary(iseq);
2183 #if VM_INSN_INFO_TABLE_IMPL == 1 /* binary search */
2184 static const struct iseq_insn_info_entry *
2185 get_insn_info_binary_search(const rb_iseq_t *iseq, size_t pos)
2187 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2188 size_t size = body->insns_info.size;
2189 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2190 const unsigned int *positions = body->insns_info.positions;
2191 const int debug = 0;
2193 if (debug) {
2194 printf("size: %"PRIuSIZE"\n", size);
2195 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2196 (size_t)0, positions[0], insns_info[0].line_no, pos);
2199 if (size == 0) {
2200 return NULL;
2202 else if (size == 1) {
2203 return &insns_info[0];
2205 else {
2206 size_t l = 1, r = size - 1;
2207 while (l <= r) {
2208 size_t m = l + (r - l) / 2;
2209 if (positions[m] == pos) {
2210 return &insns_info[m];
2212 if (positions[m] < pos) {
2213 l = m + 1;
2215 else {
2216 r = m - 1;
2219 if (l >= size) {
2220 return &insns_info[size-1];
2222 if (positions[l] > pos) {
2223 return &insns_info[l-1];
2225 return &insns_info[l];
2229 static const struct iseq_insn_info_entry *
2230 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2232 return get_insn_info_binary_search(iseq, pos);
2234 #endif
2236 #if VM_INSN_INFO_TABLE_IMPL == 2 /* succinct bitvector */
2237 static const struct iseq_insn_info_entry *
2238 get_insn_info_succinct_bitvector(const rb_iseq_t *iseq, size_t pos)
2240 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2241 size_t size = body->insns_info.size;
2242 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2243 const int debug = 0;
2245 if (debug) {
2246 #if VM_CHECK_MODE > 0
2247 const unsigned int *positions = body->insns_info.positions;
2248 printf("size: %"PRIuSIZE"\n", size);
2249 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2250 (size_t)0, positions[0], insns_info[0].line_no, pos);
2251 #else
2252 printf("size: %"PRIuSIZE"\n", size);
2253 printf("insns_info[%"PRIuSIZE"]: line: %d, pos: %"PRIuSIZE"\n",
2254 (size_t)0, insns_info[0].line_no, pos);
2255 #endif
2258 if (size == 0) {
2259 return NULL;
2261 else if (size == 1) {
2262 return &insns_info[0];
2264 else {
2265 int index;
2266 VM_ASSERT(body->insns_info.succ_index_table != NULL);
2267 index = succ_index_lookup(body->insns_info.succ_index_table, (int)pos);
2268 return &insns_info[index-1];
2272 static const struct iseq_insn_info_entry *
2273 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2275 return get_insn_info_succinct_bitvector(iseq, pos);
2277 #endif
2279 #if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
2280 static const struct iseq_insn_info_entry *
2281 get_insn_info_linear_search(const rb_iseq_t *iseq, size_t pos)
2283 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2284 size_t i = 0, size = body->insns_info.size;
2285 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
2286 const unsigned int *positions = body->insns_info.positions;
2287 const int debug = 0;
2289 if (debug) {
2290 printf("size: %"PRIuSIZE"\n", size);
2291 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2292 i, positions[i], insns_info[i].line_no, pos);
2295 if (size == 0) {
2296 return NULL;
2298 else if (size == 1) {
2299 return &insns_info[0];
2301 else {
2302 for (i=1; i<size; i++) {
2303 if (debug) printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
2304 i, positions[i], insns_info[i].line_no, pos);
2306 if (positions[i] == pos) {
2307 return &insns_info[i];
2309 if (positions[i] > pos) {
2310 return &insns_info[i-1];
2314 return &insns_info[i-1];
2316 #endif
2318 #if VM_INSN_INFO_TABLE_IMPL == 0 /* linear search */
2319 static const struct iseq_insn_info_entry *
2320 get_insn_info(const rb_iseq_t *iseq, size_t pos)
2322 return get_insn_info_linear_search(iseq, pos);
2324 #endif
2326 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
2327 static void
2328 validate_get_insn_info(const rb_iseq_t *iseq)
2330 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2331 size_t i;
2332 for (i = 0; i < body->iseq_size; i++) {
2333 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
2334 rb_bug("validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE") != get_insn_info(iseq, %"PRIuSIZE")", i, i);
2338 #endif
2340 unsigned int
2341 rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
2343 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2345 if (entry) {
2346 return entry->line_no;
2348 else {
2349 return 0;
2353 #ifdef USE_ISEQ_NODE_ID
2355 rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos)
2357 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2359 if (entry) {
2360 return entry->node_id;
2362 else {
2363 return 0;
2366 #endif
2368 rb_event_flag_t
2369 rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos)
2371 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2372 if (entry) {
2373 return entry->events;
2375 else {
2376 return 0;
2380 // Clear tracing event flags and turn off tracing for a given instruction as needed.
2381 // This is currently used after updating a one-shot line coverage for the current instruction.
2382 void
2383 rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t reset)
2385 struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos);
2386 if (entry) {
2387 entry->events &= ~reset;
2388 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2389 void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos);
2390 rb_iseq_trace_flag_cleared(iseq, pos);
2395 static VALUE
2396 local_var_name(const rb_iseq_t *diseq, VALUE level, VALUE op)
2398 VALUE i;
2399 VALUE name;
2400 ID lid;
2401 int idx;
2403 for (i = 0; i < level; i++) {
2404 diseq = ISEQ_BODY(diseq)->parent_iseq;
2406 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2407 lid = ISEQ_BODY(diseq)->local_table[idx];
2408 name = rb_id2str(lid);
2409 if (!name) {
2410 name = rb_str_new_cstr("?");
2412 else if (!rb_is_local_id(lid)) {
2413 name = rb_str_inspect(name);
2415 else {
2416 name = rb_str_dup(name);
2418 rb_str_catf(name, "@%d", idx);
2419 return name;
2422 int rb_insn_unified_local_var_level(VALUE);
2423 VALUE rb_dump_literal(VALUE lit);
2425 VALUE
2426 rb_insn_operand_intern(const rb_iseq_t *iseq,
2427 VALUE insn, int op_no, VALUE op,
2428 int len, size_t pos, const VALUE *pnop, VALUE child)
2430 const char *types = insn_op_types(insn);
2431 char type = types[op_no];
2432 VALUE ret = Qundef;
2434 switch (type) {
2435 case TS_OFFSET: /* LONG */
2436 ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
2437 break;
2439 case TS_NUM: /* ULONG */
2440 if (insn == BIN(defined) && op_no == 0) {
2441 enum defined_type deftype = (enum defined_type)op;
2442 switch (deftype) {
2443 case DEFINED_FUNC:
2444 ret = rb_fstring_lit("func");
2445 break;
2446 case DEFINED_REF:
2447 ret = rb_fstring_lit("ref");
2448 break;
2449 case DEFINED_CONST_FROM:
2450 ret = rb_fstring_lit("constant-from");
2451 break;
2452 default:
2453 ret = rb_iseq_defined_string(deftype);
2454 break;
2456 if (ret) break;
2458 else if (insn == BIN(checktype) && op_no == 0) {
2459 const char *type_str = rb_type_str((enum ruby_value_type)op);
2460 if (type_str) {
2461 ret = rb_str_new_cstr(type_str); break;
2464 ret = rb_sprintf("%"PRIuVALUE, op);
2465 break;
2467 case TS_LINDEX:{
2468 int level;
2469 if (types[op_no+1] == TS_NUM && pnop) {
2470 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2472 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2473 ret = local_var_name(iseq, (VALUE)level, op - VM_ENV_DATA_SIZE);
2475 else {
2476 ret = rb_inspect(INT2FIX(op));
2478 break;
2480 case TS_ID: /* ID (symbol) */
2481 ret = rb_inspect(ID2SYM(op));
2482 break;
2484 case TS_VALUE: /* VALUE */
2485 op = obj_resurrect(op);
2486 if (insn == BIN(defined) && op_no == 1 && FIXNUM_P(op)) {
2487 /* should be DEFINED_REF */
2488 int type = NUM2INT(op);
2489 if (type) {
2490 if (type & 1) {
2491 ret = rb_sprintf(":$%c", (type >> 1));
2493 else {
2494 ret = rb_sprintf(":$%d", (type >> 1));
2496 break;
2499 ret = rb_dump_literal(op);
2500 if (CLASS_OF(op) == rb_cISeq) {
2501 if (child) {
2502 rb_ary_push(child, op);
2505 break;
2507 case TS_ISEQ: /* iseq */
2509 if (op) {
2510 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2511 ret = ISEQ_BODY(iseq)->location.label;
2512 if (child) {
2513 rb_ary_push(child, (VALUE)iseq);
2516 else {
2517 ret = rb_str_new2("nil");
2519 break;
2522 case TS_IC:
2524 ret = rb_sprintf("<ic:%"PRIdPTRDIFF" ", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2525 const ID *segments = ((IC)op)->segments;
2526 rb_str_cat2(ret, rb_id2name(*segments++));
2527 while (*segments) {
2528 rb_str_catf(ret, "::%s", rb_id2name(*segments++));
2530 rb_str_cat2(ret, ">");
2532 break;
2533 case TS_IVC:
2534 case TS_ICVARC:
2535 case TS_ISE:
2536 ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2537 break;
2539 case TS_CALLDATA:
2541 struct rb_call_data *cd = (struct rb_call_data *)op;
2542 const struct rb_callinfo *ci = cd->ci;
2543 VALUE ary = rb_ary_new();
2544 ID mid = vm_ci_mid(ci);
2546 if (mid) {
2547 rb_ary_push(ary, rb_sprintf("mid:%"PRIsVALUE, rb_id2str(mid)));
2550 rb_ary_push(ary, rb_sprintf("argc:%d", vm_ci_argc(ci)));
2552 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2553 const struct rb_callinfo_kwarg *kw_args = vm_ci_kwarg(ci);
2554 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2555 rb_ary_push(ary, rb_sprintf("kw:[%"PRIsVALUE"]", rb_ary_join(kw_ary, rb_str_new2(","))));
2558 if (vm_ci_flag(ci)) {
2559 VALUE flags = rb_ary_new();
2560 # define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2561 CALL_FLAG(ARGS_SPLAT);
2562 CALL_FLAG(ARGS_SPLAT_MUT);
2563 CALL_FLAG(ARGS_BLOCKARG);
2564 CALL_FLAG(FCALL);
2565 CALL_FLAG(VCALL);
2566 CALL_FLAG(ARGS_SIMPLE);
2567 CALL_FLAG(TAILCALL);
2568 CALL_FLAG(SUPER);
2569 CALL_FLAG(ZSUPER);
2570 CALL_FLAG(KWARG);
2571 CALL_FLAG(KW_SPLAT);
2572 CALL_FLAG(KW_SPLAT_MUT);
2573 CALL_FLAG(FORWARDING);
2574 CALL_FLAG(OPT_SEND); /* maybe not reachable */
2575 rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
2578 ret = rb_sprintf("<calldata!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
2580 break;
2582 case TS_CDHASH:
2583 ret = rb_str_new2("<cdhash>");
2584 break;
2586 case TS_FUNCPTR:
2588 #ifdef HAVE_DLADDR
2589 Dl_info info;
2590 if (dladdr((void *)op, &info) && info.dli_sname) {
2591 ret = rb_str_new_cstr(info.dli_sname);
2592 break;
2594 #endif
2595 ret = rb_str_new2("<funcptr>");
2597 break;
2599 case TS_BUILTIN:
2601 const struct rb_builtin_function *bf = (const struct rb_builtin_function *)op;
2602 ret = rb_sprintf("<builtin!%s/%d>",
2603 bf->name, bf->argc);
2605 break;
2607 default:
2608 rb_bug("unknown operand type: %c", type);
2610 return ret;
2613 static VALUE
2614 right_strip(VALUE str)
2616 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2617 while (end-- > beg && *end == ' ');
2618 rb_str_set_len(str, end - beg + 1);
2619 return str;
2623 * Disassemble a instruction
2624 * Iseq -> Iseq inspect object
2627 rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
2628 const rb_iseq_t *iseq, VALUE child)
2630 VALUE insn = code[pos];
2631 int len = insn_len(insn);
2632 int j;
2633 const char *types = insn_op_types(insn);
2634 VALUE str = rb_str_new(0, 0);
2635 const char *insn_name_buff;
2637 insn_name_buff = insn_name(insn);
2638 if (1) {
2639 extern const int rb_vm_max_insn_name_size;
2640 rb_str_catf(str, "%04"PRIuSIZE" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2642 else {
2643 rb_str_catf(str, "%04"PRIuSIZE" %-28.*s ", pos,
2644 (int)strcspn(insn_name_buff, "_"), insn_name_buff);
2647 for (j = 0; types[j]; j++) {
2648 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2649 len, pos, &code[pos + j + 2],
2650 child);
2651 rb_str_concat(str, opstr);
2653 if (types[j + 1]) {
2654 rb_str_cat2(str, ", ");
2659 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2660 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2661 if (line_no && line_no != prev) {
2662 long slen = RSTRING_LEN(str);
2663 slen = (slen > 70) ? 0 : (70 - slen);
2664 str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
2669 rb_event_flag_t events = rb_iseq_event_flags(iseq, pos);
2670 if (events) {
2671 str = rb_str_catf(str, "[%s%s%s%s%s%s%s%s%s%s%s%s]",
2672 events & RUBY_EVENT_LINE ? "Li" : "",
2673 events & RUBY_EVENT_CLASS ? "Cl" : "",
2674 events & RUBY_EVENT_END ? "En" : "",
2675 events & RUBY_EVENT_CALL ? "Ca" : "",
2676 events & RUBY_EVENT_RETURN ? "Re" : "",
2677 events & RUBY_EVENT_C_CALL ? "Cc" : "",
2678 events & RUBY_EVENT_C_RETURN ? "Cr" : "",
2679 events & RUBY_EVENT_B_CALL ? "Bc" : "",
2680 events & RUBY_EVENT_B_RETURN ? "Br" : "",
2681 events & RUBY_EVENT_RESCUE ? "Rs" : "",
2682 events & RUBY_EVENT_COVERAGE_LINE ? "Cli" : "",
2683 events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : "");
2687 right_strip(str);
2688 if (ret) {
2689 rb_str_cat2(str, "\n");
2690 rb_str_concat(ret, str);
2692 else {
2693 printf("%.*s\n", (int)RSTRING_LEN(str), RSTRING_PTR(str));
2695 return len;
2698 static const char *
2699 catch_type(int type)
2701 switch (type) {
2702 case CATCH_TYPE_RESCUE:
2703 return "rescue";
2704 case CATCH_TYPE_ENSURE:
2705 return "ensure";
2706 case CATCH_TYPE_RETRY:
2707 return "retry";
2708 case CATCH_TYPE_BREAK:
2709 return "break";
2710 case CATCH_TYPE_REDO:
2711 return "redo";
2712 case CATCH_TYPE_NEXT:
2713 return "next";
2714 default:
2715 rb_bug("unknown catch type: %d", type);
2716 return 0;
2720 static VALUE
2721 iseq_inspect(const rb_iseq_t *iseq)
2723 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2724 if (!body->location.label) {
2725 return rb_sprintf("#<ISeq: uninitialized>");
2727 else {
2728 const rb_code_location_t *loc = &body->location.code_location;
2729 return rb_sprintf("#<ISeq:%"PRIsVALUE"@%"PRIsVALUE":%d (%d,%d)-(%d,%d)>",
2730 body->location.label, rb_iseq_path(iseq),
2731 loc->beg_pos.lineno,
2732 loc->beg_pos.lineno,
2733 loc->beg_pos.column,
2734 loc->end_pos.lineno,
2735 loc->end_pos.column);
2739 static const rb_data_type_t tmp_set = {
2740 "tmpset",
2741 {(void (*)(void *))rb_mark_set, (void (*)(void *))st_free_table, 0, 0,},
2742 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2745 static VALUE
2746 rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
2748 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2749 VALUE *code;
2750 VALUE str = rb_str_new(0, 0);
2751 VALUE child = rb_ary_hidden_new(3);
2752 unsigned int size;
2753 unsigned int i;
2754 long l;
2755 size_t n;
2756 enum {header_minlen = 72};
2757 st_table *done_iseq = 0;
2758 VALUE done_iseq_wrapper = Qnil;
2759 const char *indent_str;
2760 long indent_len;
2762 size = body->iseq_size;
2764 indent_len = RSTRING_LEN(indent);
2765 indent_str = RSTRING_PTR(indent);
2767 rb_str_cat(str, indent_str, indent_len);
2768 rb_str_cat2(str, "== disasm: ");
2770 rb_str_append(str, iseq_inspect(iseq));
2771 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2772 rb_str_modify_expand(str, header_minlen - l);
2773 memset(RSTRING_END(str), '=', header_minlen - l);
2775 if (iseq->body->builtin_attrs) {
2776 #define disasm_builtin_attr(str, iseq, attr) \
2777 if (iseq->body->builtin_attrs & BUILTIN_ATTR_ ## attr) { \
2778 rb_str_cat2(str, " " #attr); \
2780 disasm_builtin_attr(str, iseq, LEAF);
2781 disasm_builtin_attr(str, iseq, SINGLE_NOARG_LEAF);
2782 disasm_builtin_attr(str, iseq, INLINE_BLOCK);
2783 disasm_builtin_attr(str, iseq, C_TRACE);
2785 rb_str_cat2(str, "\n");
2787 /* show catch table information */
2788 if (body->catch_table) {
2789 rb_str_cat(str, indent_str, indent_len);
2790 rb_str_cat2(str, "== catch table\n");
2792 if (body->catch_table) {
2793 rb_str_cat_cstr(indent, "| ");
2794 indent_str = RSTRING_PTR(indent);
2795 for (i = 0; i < body->catch_table->size; i++) {
2796 const struct iseq_catch_table_entry *entry =
2797 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2798 rb_str_cat(str, indent_str, indent_len);
2799 rb_str_catf(str,
2800 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2801 catch_type((int)entry->type), (int)entry->start,
2802 (int)entry->end, (int)entry->sp, (int)entry->cont);
2803 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2804 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2805 if (!done_iseq) {
2806 done_iseq = st_init_numtable();
2807 done_iseq_wrapper = TypedData_Wrap_Struct(0, &tmp_set, done_iseq);
2809 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2810 indent_str = RSTRING_PTR(indent);
2813 rb_str_resize(indent, indent_len);
2814 indent_str = RSTRING_PTR(indent);
2816 if (body->catch_table) {
2817 rb_str_cat(str, indent_str, indent_len);
2818 rb_str_cat2(str, "|-------------------------------------"
2819 "-----------------------------------\n");
2822 /* show local table information */
2823 if (body->local_table) {
2824 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
2825 rb_str_cat(str, indent_str, indent_len);
2826 rb_str_catf(str,
2827 "local table (size: %d, argc: %d "
2828 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2829 body->local_table_size,
2830 body->param.lead_num,
2831 body->param.opt_num,
2832 body->param.flags.has_rest ? body->param.rest_start : -1,
2833 body->param.post_num,
2834 body->param.flags.has_block ? body->param.block_start : -1,
2835 body->param.flags.has_kw ? keyword->num : -1,
2836 body->param.flags.has_kw ? keyword->required_num : -1,
2837 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2839 for (i = body->local_table_size; i > 0;) {
2840 int li = body->local_table_size - --i - 1;
2841 long width;
2842 VALUE name = local_var_name(iseq, 0, i);
2843 char argi[0x100];
2844 char opti[0x100];
2846 opti[0] = '\0';
2847 if (body->param.flags.has_opt) {
2848 int argc = body->param.lead_num;
2849 int opts = body->param.opt_num;
2850 if (li >= argc && li < argc + opts) {
2851 snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
2852 body->param.opt_table[li - argc]);
2856 snprintf(argi, sizeof(argi), "%s%s%s%s%s%s", /* arg, opts, rest, post, kwrest, block */
2857 (body->param.lead_num > li) ? (body->param.flags.ambiguous_param0 ? "AmbiguousArg" : "Arg") : "",
2858 opti,
2859 (body->param.flags.has_rest && body->param.rest_start == li) ? (body->param.flags.anon_rest ? "AnonRest" : "Rest") : "",
2860 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ? "Post" : "",
2861 (body->param.flags.has_kwrest && keyword->rest_start == li) ? (body->param.flags.anon_kwrest ? "AnonKwrest" : "Kwrest") : "",
2862 (body->param.flags.has_block && body->param.block_start == li) ? "Block" : "");
2864 rb_str_cat(str, indent_str, indent_len);
2865 rb_str_catf(str, "[%2d] ", i + 1);
2866 width = RSTRING_LEN(str) + 11;
2867 rb_str_append(str, name);
2868 if (*argi) rb_str_catf(str, "<%s>", argi);
2869 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
2871 rb_str_cat_cstr(right_strip(str), "\n");
2874 /* show each line */
2875 code = rb_iseq_original_iseq(iseq);
2876 for (n = 0; n < size;) {
2877 rb_str_cat(str, indent_str, indent_len);
2878 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2881 for (l = 0; l < RARRAY_LEN(child); l++) {
2882 VALUE isv = rb_ary_entry(child, l);
2883 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv)) continue;
2884 rb_str_cat_cstr(str, "\n");
2885 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2886 indent_str = RSTRING_PTR(indent);
2888 RB_GC_GUARD(done_iseq_wrapper);
2890 return str;
2893 VALUE
2894 rb_iseq_disasm(const rb_iseq_t *iseq)
2896 VALUE str = rb_iseq_disasm_recursive(iseq, rb_str_new(0, 0));
2897 rb_str_resize(str, RSTRING_LEN(str));
2898 return str;
2902 * Estimates the number of instance variables that will be set on
2903 * a given `class` with the initialize method defined in
2904 * `initialize_iseq`
2906 attr_index_t
2907 rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq)
2909 struct rb_id_table * iv_names = rb_id_table_create(0);
2911 for (unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2912 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2914 if (cache->iv_set_name) {
2915 rb_id_table_insert(iv_names, cache->iv_set_name, Qtrue);
2919 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2921 VALUE superclass = rb_class_superclass(klass);
2922 count += RCLASS_EXT(superclass)->max_iv_count;
2924 rb_id_table_free(iv_names);
2926 return count;
2930 * call-seq:
2931 * iseq.disasm -> str
2932 * iseq.disassemble -> str
2934 * Returns the instruction sequence as a +String+ in human readable form.
2936 * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
2938 * Produces:
2940 * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
2941 * 0000 trace 1 ( 1)
2942 * 0002 putobject 1
2943 * 0004 putobject 2
2944 * 0006 opt_plus <ic:1>
2945 * 0008 leave
2947 static VALUE
2948 iseqw_disasm(VALUE self)
2950 return rb_iseq_disasm(iseqw_check(self));
2953 static int
2954 iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *child_iseq, void *data), void *data)
2956 unsigned int i;
2957 VALUE *code = rb_iseq_original_iseq(iseq);
2958 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2959 const rb_iseq_t *child;
2960 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2962 if (body->catch_table) {
2963 for (i = 0; i < body->catch_table->size; i++) {
2964 const struct iseq_catch_table_entry *entry =
2965 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2966 child = entry->iseq;
2967 if (child) {
2968 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2969 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2970 (*iter_func)(child, data);
2976 for (i=0; i<body->iseq_size;) {
2977 VALUE insn = code[i];
2978 int len = insn_len(insn);
2979 const char *types = insn_op_types(insn);
2980 int j;
2982 for (j=0; types[j]; j++) {
2983 switch (types[j]) {
2984 case TS_ISEQ:
2985 child = (const rb_iseq_t *)code[i+j+1];
2986 if (child) {
2987 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2988 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2989 (*iter_func)(child, data);
2992 break;
2993 default:
2994 break;
2997 i += len;
3000 return (int)RHASH_SIZE(all_children);
3003 static void
3004 yield_each_children(const rb_iseq_t *child_iseq, void *data)
3006 rb_yield(iseqw_new(child_iseq));
3010 * call-seq:
3011 * iseq.each_child{|child_iseq| ...} -> iseq
3013 * Iterate all direct child instruction sequences.
3014 * Iteration order is implementation/version defined
3015 * so that people should not rely on the order.
3017 static VALUE
3018 iseqw_each_child(VALUE self)
3020 const rb_iseq_t *iseq = iseqw_check(self);
3021 iseq_iterate_children(iseq, yield_each_children, NULL);
3022 return self;
3025 static void
3026 push_event_info(const rb_iseq_t *iseq, rb_event_flag_t events, int line, VALUE ary)
3028 #define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
3029 C(RUBY_EVENT_CLASS, "class", rb_iseq_first_lineno(iseq));
3030 C(RUBY_EVENT_CALL, "call", rb_iseq_first_lineno(iseq));
3031 C(RUBY_EVENT_B_CALL, "b_call", rb_iseq_first_lineno(iseq));
3032 C(RUBY_EVENT_LINE, "line", INT2FIX(line));
3033 C(RUBY_EVENT_END, "end", INT2FIX(line));
3034 C(RUBY_EVENT_RETURN, "return", INT2FIX(line));
3035 C(RUBY_EVENT_B_RETURN, "b_return", INT2FIX(line));
3036 C(RUBY_EVENT_RESCUE, "rescue", INT2FIX(line));
3037 #undef C
3041 * call-seq:
3042 * iseq.trace_points -> ary
3044 * Return trace points in the instruction sequence.
3045 * Return an array of [line, event_symbol] pair.
3047 static VALUE
3048 iseqw_trace_points(VALUE self)
3050 const rb_iseq_t *iseq = iseqw_check(self);
3051 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3052 unsigned int i;
3053 VALUE ary = rb_ary_new();
3055 for (i=0; i<body->insns_info.size; i++) {
3056 const struct iseq_insn_info_entry *entry = &body->insns_info.body[i];
3057 if (entry->events) {
3058 push_event_info(iseq, entry->events, entry->line_no, ary);
3061 return ary;
3065 * Returns the instruction sequence containing the given proc or method.
3067 * For example, using irb:
3069 * # a proc
3070 * > p = proc { num = 1 + 2 }
3071 * > RubyVM::InstructionSequence.of(p)
3072 * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
3074 * # for a method
3075 * > def foo(bar); puts bar; end
3076 * > RubyVM::InstructionSequence.of(method(:foo))
3077 * > #=> <RubyVM::InstructionSequence:foo@(irb)>
3079 * Using ::compile_file:
3081 * # /tmp/iseq_of.rb
3082 * def hello
3083 * puts "hello, world"
3084 * end
3086 * $a_global_proc = proc { str = 'a' + 'b' }
3088 * # in irb
3089 * > require '/tmp/iseq_of.rb'
3091 * # first the method hello
3092 * > RubyVM::InstructionSequence.of(method(:hello))
3093 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
3095 * # then the global proc
3096 * > RubyVM::InstructionSequence.of($a_global_proc)
3097 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
3099 static VALUE
3100 iseqw_s_of(VALUE klass, VALUE body)
3102 const rb_iseq_t *iseq = NULL;
3104 if (rb_frame_info_p(body)) {
3105 iseq = rb_get_iseq_from_frame_info(body);
3107 else if (rb_obj_is_proc(body)) {
3108 iseq = vm_proc_iseq(body);
3110 if (!rb_obj_is_iseq((VALUE)iseq)) {
3111 iseq = NULL;
3114 else if (rb_obj_is_method(body)) {
3115 iseq = rb_method_iseq(body);
3117 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
3118 return body;
3121 return iseq ? iseqw_new(iseq) : Qnil;
3125 * call-seq:
3126 * InstructionSequence.disasm(body) -> str
3127 * InstructionSequence.disassemble(body) -> str
3129 * Takes +body+, a +Method+ or +Proc+ object, and returns a +String+
3130 * with the human readable instructions for +body+.
3132 * For a +Method+ object:
3134 * # /tmp/method.rb
3135 * def hello
3136 * puts "hello, world"
3137 * end
3139 * puts RubyVM::InstructionSequence.disasm(method(:hello))
3141 * Produces:
3143 * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
3144 * 0000 trace 8 ( 1)
3145 * 0002 trace 1 ( 2)
3146 * 0004 putself
3147 * 0005 putstring "hello, world"
3148 * 0007 send :puts, 1, nil, 8, <ic:0>
3149 * 0013 trace 16 ( 3)
3150 * 0015 leave ( 2)
3152 * For a +Proc+ object:
3154 * # /tmp/proc.rb
3155 * p = proc { num = 1 + 2 }
3156 * puts RubyVM::InstructionSequence.disasm(p)
3158 * Produces:
3160 * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
3161 * == catch table
3162 * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
3163 * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
3164 * |------------------------------------------------------------------------
3165 * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
3166 * [ 2] num
3167 * 0000 trace 1 ( 1)
3168 * 0002 putobject 1
3169 * 0004 putobject 2
3170 * 0006 opt_plus <ic:1>
3171 * 0008 dup
3172 * 0009 setlocal num, 0
3173 * 0012 leave
3176 static VALUE
3177 iseqw_s_disasm(VALUE klass, VALUE body)
3179 VALUE iseqw = iseqw_s_of(klass, body);
3180 return NIL_P(iseqw) ? Qnil : rb_iseq_disasm(iseqw_check(iseqw));
3183 static VALUE
3184 register_label(struct st_table *table, unsigned long idx)
3186 VALUE sym = rb_str_intern(rb_sprintf("label_%lu", idx));
3187 st_insert(table, idx, sym);
3188 return sym;
3191 static VALUE
3192 exception_type2symbol(VALUE type)
3194 ID id;
3195 switch (type) {
3196 case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
3197 case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
3198 case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
3199 case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
3200 case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
3201 case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
3202 default:
3203 rb_bug("unknown exception type: %d", (int)type);
3205 return ID2SYM(id);
3208 static int
3209 cdhash_each(VALUE key, VALUE value, VALUE ary)
3211 rb_ary_push(ary, obj_resurrect(key));
3212 rb_ary_push(ary, value);
3213 return ST_CONTINUE;
3216 static const rb_data_type_t label_wrapper = {
3217 "label_wrapper",
3218 {(void (*)(void *))rb_mark_tbl, (void (*)(void *))st_free_table, 0, 0,},
3219 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
3222 #define DECL_ID(name) \
3223 static ID id_##name
3225 #define INIT_ID(name) \
3226 id_##name = rb_intern(#name)
3228 static VALUE
3229 iseq_type_id(enum rb_iseq_type type)
3231 DECL_ID(top);
3232 DECL_ID(method);
3233 DECL_ID(block);
3234 DECL_ID(class);
3235 DECL_ID(rescue);
3236 DECL_ID(ensure);
3237 DECL_ID(eval);
3238 DECL_ID(main);
3239 DECL_ID(plain);
3241 if (id_top == 0) {
3242 INIT_ID(top);
3243 INIT_ID(method);
3244 INIT_ID(block);
3245 INIT_ID(class);
3246 INIT_ID(rescue);
3247 INIT_ID(ensure);
3248 INIT_ID(eval);
3249 INIT_ID(main);
3250 INIT_ID(plain);
3253 switch (type) {
3254 case ISEQ_TYPE_TOP: return id_top;
3255 case ISEQ_TYPE_METHOD: return id_method;
3256 case ISEQ_TYPE_BLOCK: return id_block;
3257 case ISEQ_TYPE_CLASS: return id_class;
3258 case ISEQ_TYPE_RESCUE: return id_rescue;
3259 case ISEQ_TYPE_ENSURE: return id_ensure;
3260 case ISEQ_TYPE_EVAL: return id_eval;
3261 case ISEQ_TYPE_MAIN: return id_main;
3262 case ISEQ_TYPE_PLAIN: return id_plain;
3265 rb_bug("unsupported iseq type: %d", (int)type);
3268 static VALUE
3269 iseq_data_to_ary(const rb_iseq_t *iseq)
3271 unsigned int i;
3272 long l;
3273 const struct rb_iseq_constant_body *const iseq_body = ISEQ_BODY(iseq);
3274 const struct iseq_insn_info_entry *prev_insn_info;
3275 unsigned int pos;
3276 int last_line = 0;
3277 VALUE *seq, *iseq_original;
3279 VALUE val = rb_ary_new();
3280 ID type; /* Symbol */
3281 VALUE locals = rb_ary_new();
3282 VALUE params = rb_hash_new();
3283 VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
3284 VALUE nbody;
3285 VALUE exception = rb_ary_new(); /* [[....]] */
3286 VALUE misc = rb_hash_new();
3288 static ID insn_syms[VM_BARE_INSTRUCTION_SIZE]; /* w/o-trace only */
3289 struct st_table *labels_table = st_init_numtable();
3290 VALUE labels_wrapper = TypedData_Wrap_Struct(0, &label_wrapper, labels_table);
3292 if (insn_syms[0] == 0) {
3293 int i;
3294 for (i=0; i<numberof(insn_syms); i++) {
3295 insn_syms[i] = rb_intern(insn_name(i));
3299 /* type */
3300 type = iseq_type_id(iseq_body->type);
3302 /* locals */
3303 for (i=0; i<iseq_body->local_table_size; i++) {
3304 ID lid = iseq_body->local_table[i];
3305 if (lid) {
3306 if (rb_id2str(lid)) {
3307 rb_ary_push(locals, ID2SYM(lid));
3309 else { /* hidden variable from id_internal() */
3310 rb_ary_push(locals, ULONG2NUM(iseq_body->local_table_size-i+1));
3313 else {
3314 rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
3318 /* params */
3320 const struct rb_iseq_param_keyword *const keyword = iseq_body->param.keyword;
3321 int j;
3323 if (iseq_body->param.flags.has_opt) {
3324 int len = iseq_body->param.opt_num + 1;
3325 VALUE arg_opt_labels = rb_ary_new2(len);
3327 for (j = 0; j < len; j++) {
3328 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
3329 rb_ary_push(arg_opt_labels, l);
3331 rb_hash_aset(params, ID2SYM(rb_intern("opt")), arg_opt_labels);
3334 /* commit */
3335 if (iseq_body->param.flags.has_lead) rb_hash_aset(params, ID2SYM(rb_intern("lead_num")), INT2FIX(iseq_body->param.lead_num));
3336 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_num")), INT2FIX(iseq_body->param.post_num));
3337 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_start")), INT2FIX(iseq_body->param.post_start));
3338 if (iseq_body->param.flags.has_rest) rb_hash_aset(params, ID2SYM(rb_intern("rest_start")), INT2FIX(iseq_body->param.rest_start));
3339 if (iseq_body->param.flags.has_block) rb_hash_aset(params, ID2SYM(rb_intern("block_start")), INT2FIX(iseq_body->param.block_start));
3340 if (iseq_body->param.flags.has_kw) {
3341 VALUE keywords = rb_ary_new();
3342 int i, j;
3343 for (i=0; i<keyword->required_num; i++) {
3344 rb_ary_push(keywords, ID2SYM(keyword->table[i]));
3346 for (j=0; i<keyword->num; i++, j++) {
3347 VALUE key = rb_ary_new_from_args(1, ID2SYM(keyword->table[i]));
3348 if (!UNDEF_P(keyword->default_values[j])) {
3349 rb_ary_push(key, keyword->default_values[j]);
3351 rb_ary_push(keywords, key);
3354 rb_hash_aset(params, ID2SYM(rb_intern("kwbits")),
3355 INT2FIX(keyword->bits_start));
3356 rb_hash_aset(params, ID2SYM(rb_intern("keyword")), keywords);
3358 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(keyword->rest_start));
3359 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
3360 if (iseq_body->param.flags.use_block) rb_hash_aset(params, ID2SYM(rb_intern("use_block")), Qtrue);
3363 /* body */
3364 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
3366 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
3367 VALUE insn = *seq++;
3368 int j, len = insn_len(insn);
3369 VALUE *nseq = seq + len - 1;
3370 VALUE ary = rb_ary_new2(len);
3372 rb_ary_push(ary, ID2SYM(insn_syms[insn%numberof(insn_syms)]));
3373 for (j=0; j<len-1; j++, seq++) {
3374 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
3376 switch (op_type) {
3377 case TS_OFFSET: {
3378 unsigned long idx = nseq - iseq_original + *seq;
3379 rb_ary_push(ary, register_label(labels_table, idx));
3380 break;
3382 case TS_LINDEX:
3383 case TS_NUM:
3384 rb_ary_push(ary, INT2FIX(*seq));
3385 break;
3386 case TS_VALUE:
3387 rb_ary_push(ary, obj_resurrect(*seq));
3388 break;
3389 case TS_ISEQ:
3391 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3392 if (iseq) {
3393 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3394 rb_ary_push(ary, val);
3396 else {
3397 rb_ary_push(ary, Qnil);
3400 break;
3401 case TS_IC:
3403 VALUE list = rb_ary_new();
3404 const ID *ids = ((IC)*seq)->segments;
3405 while (*ids) {
3406 rb_ary_push(list, ID2SYM(*ids++));
3408 rb_ary_push(ary, list);
3410 break;
3411 case TS_IVC:
3412 case TS_ICVARC:
3413 case TS_ISE:
3415 union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
3416 rb_ary_push(ary, INT2FIX(is - ISEQ_IS_ENTRY_START(ISEQ_BODY(iseq), op_type)));
3418 break;
3419 case TS_CALLDATA:
3421 struct rb_call_data *cd = (struct rb_call_data *)*seq;
3422 const struct rb_callinfo *ci = cd->ci;
3423 VALUE e = rb_hash_new();
3424 int argc = vm_ci_argc(ci);
3426 ID mid = vm_ci_mid(ci);
3427 rb_hash_aset(e, ID2SYM(rb_intern("mid")), mid ? ID2SYM(mid) : Qnil);
3428 rb_hash_aset(e, ID2SYM(rb_intern("flag")), UINT2NUM(vm_ci_flag(ci)));
3430 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3431 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
3432 int i;
3433 VALUE kw = rb_ary_new2((long)kwarg->keyword_len);
3435 argc -= kwarg->keyword_len;
3436 for (i = 0; i < kwarg->keyword_len; i++) {
3437 rb_ary_push(kw, kwarg->keywords[i]);
3439 rb_hash_aset(e, ID2SYM(rb_intern("kw_arg")), kw);
3442 rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")),
3443 INT2FIX(argc));
3444 rb_ary_push(ary, e);
3446 break;
3447 case TS_ID:
3448 rb_ary_push(ary, ID2SYM(*seq));
3449 break;
3450 case TS_CDHASH:
3452 VALUE hash = *seq;
3453 VALUE val = rb_ary_new();
3454 int i;
3456 rb_hash_foreach(hash, cdhash_each, val);
3458 for (i=0; i<RARRAY_LEN(val); i+=2) {
3459 VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
3460 unsigned long idx = nseq - iseq_original + pos;
3462 rb_ary_store(val, i+1,
3463 register_label(labels_table, idx));
3465 rb_ary_push(ary, val);
3467 break;
3468 case TS_FUNCPTR:
3470 #if SIZEOF_VALUE <= SIZEOF_LONG
3471 VALUE val = LONG2NUM((SIGNED_VALUE)*seq);
3472 #else
3473 VALUE val = LL2NUM((SIGNED_VALUE)*seq);
3474 #endif
3475 rb_ary_push(ary, val);
3477 break;
3478 case TS_BUILTIN:
3480 VALUE val = rb_hash_new();
3481 #if SIZEOF_VALUE <= SIZEOF_LONG
3482 VALUE func_ptr = LONG2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3483 #else
3484 VALUE func_ptr = LL2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3485 #endif
3486 rb_hash_aset(val, ID2SYM(rb_intern("func_ptr")), func_ptr);
3487 rb_hash_aset(val, ID2SYM(rb_intern("argc")), INT2NUM(((RB_BUILTIN)*seq)->argc));
3488 rb_hash_aset(val, ID2SYM(rb_intern("index")), INT2NUM(((RB_BUILTIN)*seq)->index));
3489 rb_hash_aset(val, ID2SYM(rb_intern("name")), rb_str_new_cstr(((RB_BUILTIN)*seq)->name));
3490 rb_ary_push(ary, val);
3492 break;
3493 default:
3494 rb_bug("unknown operand: %c", insn_op_type(insn, j));
3497 rb_ary_push(body, ary);
3500 nbody = body;
3502 /* exception */
3503 if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
3504 VALUE ary = rb_ary_new();
3505 const struct iseq_catch_table_entry *entry =
3506 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3507 rb_ary_push(ary, exception_type2symbol(entry->type));
3508 if (entry->iseq) {
3509 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3511 else {
3512 rb_ary_push(ary, Qnil);
3514 rb_ary_push(ary, register_label(labels_table, entry->start));
3515 rb_ary_push(ary, register_label(labels_table, entry->end));
3516 rb_ary_push(ary, register_label(labels_table, entry->cont));
3517 rb_ary_push(ary, UINT2NUM(entry->sp));
3518 rb_ary_push(exception, ary);
3521 /* make body with labels and insert line number */
3522 body = rb_ary_new();
3523 prev_insn_info = NULL;
3524 #ifdef USE_ISEQ_NODE_ID
3525 VALUE node_ids = rb_ary_new();
3526 #endif
3528 for (l=0, pos=0; l<RARRAY_LEN(nbody); l++) {
3529 const struct iseq_insn_info_entry *info;
3530 VALUE ary = RARRAY_AREF(nbody, l);
3531 st_data_t label;
3533 if (st_lookup(labels_table, pos, &label)) {
3534 rb_ary_push(body, (VALUE)label);
3537 info = get_insn_info(iseq, pos);
3538 #ifdef USE_ISEQ_NODE_ID
3539 rb_ary_push(node_ids, INT2FIX(info->node_id));
3540 #endif
3542 if (prev_insn_info != info) {
3543 int line = info->line_no;
3544 rb_event_flag_t events = info->events;
3546 if (line > 0 && last_line != line) {
3547 rb_ary_push(body, INT2FIX(line));
3548 last_line = line;
3550 #define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3551 CHECK_EVENT(RUBY_EVENT_LINE);
3552 CHECK_EVENT(RUBY_EVENT_CLASS);
3553 CHECK_EVENT(RUBY_EVENT_END);
3554 CHECK_EVENT(RUBY_EVENT_CALL);
3555 CHECK_EVENT(RUBY_EVENT_RETURN);
3556 CHECK_EVENT(RUBY_EVENT_B_CALL);
3557 CHECK_EVENT(RUBY_EVENT_B_RETURN);
3558 CHECK_EVENT(RUBY_EVENT_RESCUE);
3559 #undef CHECK_EVENT
3560 prev_insn_info = info;
3563 rb_ary_push(body, ary);
3564 pos += RARRAY_LENINT(ary); /* reject too huge data */
3566 RB_GC_GUARD(nbody);
3567 RB_GC_GUARD(labels_wrapper);
3569 rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq_body->param.size));
3570 rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq_body->local_table_size));
3571 rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq_body->stack_max));
3572 rb_hash_aset(misc, ID2SYM(rb_intern("node_id")), INT2FIX(iseq_body->location.node_id));
3573 rb_hash_aset(misc, ID2SYM(rb_intern("code_location")),
3574 rb_ary_new_from_args(4,
3575 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3576 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3577 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3578 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3579 #ifdef USE_ISEQ_NODE_ID
3580 rb_hash_aset(misc, ID2SYM(rb_intern("node_ids")), node_ids);
3581 #endif
3582 rb_hash_aset(misc, ID2SYM(rb_intern("parser")), iseq_body->prism ? ID2SYM(rb_intern("prism")) : ID2SYM(rb_intern("parse.y")));
3585 * [:magic, :major_version, :minor_version, :format_type, :misc,
3586 * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
3587 * :catch_table, :bytecode]
3589 rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
3590 rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
3591 rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
3592 rb_ary_push(val, INT2FIX(1));
3593 rb_ary_push(val, misc);
3594 rb_ary_push(val, iseq_body->location.label);
3595 rb_ary_push(val, rb_iseq_path(iseq));
3596 rb_ary_push(val, rb_iseq_realpath(iseq));
3597 rb_ary_push(val, RB_INT2NUM(iseq_body->location.first_lineno));
3598 rb_ary_push(val, ID2SYM(type));
3599 rb_ary_push(val, locals);
3600 rb_ary_push(val, params);
3601 rb_ary_push(val, exception);
3602 rb_ary_push(val, body);
3603 return val;
3606 VALUE
3607 rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
3609 int i, r;
3610 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3611 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
3612 VALUE a, args = rb_ary_new2(body->param.size);
3613 ID req, opt, rest, block, key, keyrest;
3614 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3615 #define PARAM_ID(i) body->local_table[(i)]
3616 #define PARAM(i, type) ( \
3617 PARAM_TYPE(type), \
3618 rb_id2str(PARAM_ID(i)) ? \
3619 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3622 CONST_ID(req, "req");
3623 CONST_ID(opt, "opt");
3625 if (body->param.flags.forwardable) {
3626 // [[:rest, :*], [:keyrest, :**], [:block, :&]]
3627 CONST_ID(rest, "rest");
3628 CONST_ID(keyrest, "keyrest");
3629 CONST_ID(block, "block");
3630 rb_ary_push(args, rb_ary_new_from_args(2, ID2SYM(rest), ID2SYM(idMULT)));
3631 rb_ary_push(args, rb_ary_new_from_args(2, ID2SYM(keyrest), ID2SYM(idPow)));
3632 rb_ary_push(args, rb_ary_new_from_args(2, ID2SYM(block), ID2SYM(idAnd)));
3635 if (is_proc) {
3636 for (i = 0; i < body->param.lead_num; i++) {
3637 PARAM_TYPE(opt);
3638 if (rb_id2str(PARAM_ID(i))) {
3639 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3641 rb_ary_push(args, a);
3644 else {
3645 for (i = 0; i < body->param.lead_num; i++) {
3646 rb_ary_push(args, PARAM(i, req));
3649 r = body->param.lead_num + body->param.opt_num;
3650 for (; i < r; i++) {
3651 PARAM_TYPE(opt);
3652 if (rb_id2str(PARAM_ID(i))) {
3653 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3655 rb_ary_push(args, a);
3657 if (body->param.flags.has_rest) {
3658 CONST_ID(rest, "rest");
3659 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3661 r = body->param.post_start + body->param.post_num;
3662 if (is_proc) {
3663 for (i = body->param.post_start; i < r; i++) {
3664 PARAM_TYPE(opt);
3665 if (rb_id2str(PARAM_ID(i))) {
3666 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3668 rb_ary_push(args, a);
3671 else {
3672 for (i = body->param.post_start; i < r; i++) {
3673 rb_ary_push(args, PARAM(i, req));
3676 if (body->param.flags.accepts_no_kwarg) {
3677 ID nokey;
3678 CONST_ID(nokey, "nokey");
3679 PARAM_TYPE(nokey);
3680 rb_ary_push(args, a);
3682 if (body->param.flags.has_kw) {
3683 i = 0;
3684 if (keyword->required_num > 0) {
3685 ID keyreq;
3686 CONST_ID(keyreq, "keyreq");
3687 for (; i < keyword->required_num; i++) {
3688 PARAM_TYPE(keyreq);
3689 if (rb_id2str(keyword->table[i])) {
3690 rb_ary_push(a, ID2SYM(keyword->table[i]));
3692 rb_ary_push(args, a);
3695 CONST_ID(key, "key");
3696 for (; i < keyword->num; i++) {
3697 PARAM_TYPE(key);
3698 if (rb_id2str(keyword->table[i])) {
3699 rb_ary_push(a, ID2SYM(keyword->table[i]));
3701 rb_ary_push(args, a);
3704 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3705 ID param;
3706 CONST_ID(keyrest, "keyrest");
3707 PARAM_TYPE(keyrest);
3708 if (body->param.flags.has_kwrest &&
3709 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3710 rb_ary_push(a, ID2SYM(param));
3712 else if (body->param.flags.ruby2_keywords) {
3713 rb_ary_push(a, ID2SYM(idPow));
3715 rb_ary_push(args, a);
3717 if (body->param.flags.has_block) {
3718 CONST_ID(block, "block");
3719 rb_ary_push(args, PARAM(body->param.block_start, block));
3721 return args;
3724 VALUE
3725 rb_iseq_defined_string(enum defined_type type)
3727 static const char expr_names[][18] = {
3728 "nil",
3729 "instance-variable",
3730 "local-variable",
3731 "global-variable",
3732 "class variable",
3733 "constant",
3734 "method",
3735 "yield",
3736 "super",
3737 "self",
3738 "true",
3739 "false",
3740 "assignment",
3741 "expression",
3743 const char *estr;
3745 if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) rb_bug("unknown defined type %d", type);
3746 estr = expr_names[type - 1];
3747 return rb_fstring_cstr(estr);
3750 // A map from encoded_insn to insn_data: decoded insn number, its len,
3751 // decoded ZJIT insn number, non-trace version of encoded insn,
3752 // trace version, and zjit version.
3753 static st_table *encoded_insn_data;
3754 typedef struct insn_data_struct {
3755 int insn;
3756 int insn_len;
3757 void *notrace_encoded_insn;
3758 void *trace_encoded_insn;
3759 #if USE_ZJIT
3760 int zjit_insn;
3761 void *zjit_encoded_insn;
3762 #endif
3763 } insn_data_t;
3764 static insn_data_t insn_data[VM_BARE_INSTRUCTION_SIZE];
3766 void
3767 rb_free_encoded_insn_data(void)
3769 st_free_table(encoded_insn_data);
3772 // Initialize a table to decode bare, trace, and zjit instructions.
3773 // This function also determines which instructions are used when TracePoint is enabled.
3774 void
3775 rb_vm_encoded_insn_data_table_init(void)
3777 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3778 const void * const *table = rb_vm_get_insns_address_table();
3779 #define INSN_CODE(insn) ((VALUE)table[insn])
3780 #else
3781 #define INSN_CODE(insn) (insn)
3782 #endif
3783 encoded_insn_data = st_init_numtable_with_size(VM_BARE_INSTRUCTION_SIZE);
3785 for (int insn = 0; insn < VM_BARE_INSTRUCTION_SIZE; insn++) {
3786 insn_data[insn].insn = insn;
3787 insn_data[insn].insn_len = insn_len(insn);
3789 // When tracing :return events, we convert opt_invokebuiltin_delegate_leave + leave into
3790 // opt_invokebuiltin_delegate + trace_leave, presumably because we don't want to fire
3791 // :return events before invokebuiltin. https://github.com/ruby/ruby/pull/3256
3792 int notrace_insn = (insn != BIN(opt_invokebuiltin_delegate_leave)) ? insn : BIN(opt_invokebuiltin_delegate);
3793 insn_data[insn].notrace_encoded_insn = (void *)INSN_CODE(notrace_insn);
3794 insn_data[insn].trace_encoded_insn = (void *)INSN_CODE(notrace_insn + VM_BARE_INSTRUCTION_SIZE);
3796 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3797 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_BARE_INSTRUCTION_SIZE);
3798 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3799 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3801 #if USE_ZJIT
3802 int zjit_insn = vm_bare_insn_to_zjit_insn(insn);
3803 insn_data[insn].zjit_insn = zjit_insn;
3804 insn_data[insn].zjit_encoded_insn = (insn != zjit_insn) ? (void *)INSN_CODE(zjit_insn) : 0;
3806 if (insn != zjit_insn) {
3807 st_data_t key3 = (st_data_t)INSN_CODE(zjit_insn);
3808 st_add_direct(encoded_insn_data, key3, (st_data_t)&insn_data[insn]);
3810 #endif
3814 // Decode an insn address to an insn. This returns bare instructions
3815 // even if they're trace/zjit instructions. Use rb_vm_insn_addr2opcode
3816 // to decode trace/zjit instructions as is.
3818 rb_vm_insn_addr2insn(const void *addr)
3820 st_data_t key = (st_data_t)addr;
3821 st_data_t val;
3823 if (st_lookup(encoded_insn_data, key, &val)) {
3824 insn_data_t *e = (insn_data_t *)val;
3825 return (int)e->insn;
3828 rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3831 // Decode an insn address to an insn. Unlike rb_vm_insn_addr2insn,
3832 // this function can return trace/zjit opcode variants.
3834 rb_vm_insn_addr2opcode(const void *addr)
3836 st_data_t key = (st_data_t)addr;
3837 st_data_t val;
3839 if (st_lookup(encoded_insn_data, key, &val)) {
3840 insn_data_t *e = (insn_data_t *)val;
3841 int opcode = e->insn;
3842 if (addr == e->trace_encoded_insn) {
3843 opcode += VM_BARE_INSTRUCTION_SIZE;
3845 #if USE_ZJIT
3846 else if (addr == e->zjit_encoded_insn) {
3847 opcode = e->zjit_insn;
3849 #endif
3850 return opcode;
3853 rb_bug("rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3856 // Decode `ISEQ_BODY(iseq)->iseq_encoded[i]` to an insn. This returns
3857 // bare instructions even if they're trace/zjit instructions. Use
3858 // rb_vm_insn_addr2opcode to decode trace/zjit instructions as is.
3860 rb_vm_insn_decode(const VALUE encoded)
3862 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3863 int insn = rb_vm_insn_addr2insn((void *)encoded);
3864 #else
3865 int insn = (int)encoded;
3866 #endif
3867 return insn;
3870 // Turn on or off tracing for a given instruction address
3871 static inline int
3872 encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace)
3874 st_data_t key = (st_data_t)*iseq_encoded_insn;
3875 st_data_t val;
3877 if (st_lookup(encoded_insn_data, key, &val)) {
3878 insn_data_t *e = (insn_data_t *)val;
3879 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3880 turnon = 1;
3882 *iseq_encoded_insn = (VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3883 return e->insn_len;
3886 rb_bug("trace_instrument: invalid insn address: %p", (void *)*iseq_encoded_insn);
3889 // Turn off tracing for an instruction at pos after tracing event flags are cleared
3890 void
3891 rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos)
3893 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3894 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3895 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0, false);
3898 // We need to fire call events on instructions with b_call events if the block
3899 // is running as a method. So, if we are listening for call events, then
3900 // instructions that have b_call events need to become trace variants.
3901 // Use this function when making decisions about recompiling to trace variants.
3902 static inline rb_event_flag_t
3903 add_bmethod_events(rb_event_flag_t events)
3905 if (events & RUBY_EVENT_CALL) {
3906 events |= RUBY_EVENT_B_CALL;
3908 if (events & RUBY_EVENT_RETURN) {
3909 events |= RUBY_EVENT_B_RETURN;
3911 return events;
3914 // Note, to support call/return events for bmethods, turnon_event can have more events than tpval.
3915 static int
3916 iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
3918 unsigned int pc;
3919 int n = 0;
3920 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3921 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3923 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3925 for (pc=0; pc<body->iseq_size;) {
3926 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc);
3927 rb_event_flag_t pc_events = entry->events;
3928 rb_event_flag_t target_events = turnon_events;
3929 unsigned int line = (int)entry->line_no;
3931 if (target_line == 0 || target_line == line) {
3932 /* ok */
3934 else {
3935 target_events &= ~RUBY_EVENT_LINE;
3938 if (pc_events & target_events) {
3939 n++;
3941 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events), true);
3944 if (n > 0) {
3945 if (iseq->aux.exec.local_hooks == NULL) {
3946 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = RB_ZALLOC(rb_hook_list_t);
3947 iseq->aux.exec.local_hooks->is_local = true;
3949 rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3952 return n;
3955 struct trace_set_local_events_struct {
3956 rb_event_flag_t turnon_events;
3957 VALUE tpval;
3958 unsigned int target_line;
3959 int n;
3962 static void
3963 iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3965 struct trace_set_local_events_struct *data = (struct trace_set_local_events_struct *)p;
3966 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3967 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3971 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)
3973 struct trace_set_local_events_struct data;
3974 if (target_bmethod) {
3975 turnon_events = add_bmethod_events(turnon_events);
3977 data.turnon_events = turnon_events;
3978 data.tpval = tpval;
3979 data.target_line = target_line;
3980 data.n = 0;
3982 iseq_add_local_tracepoint_i(iseq, (void *)&data);
3983 if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */
3984 return data.n;
3987 static int
3988 iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval)
3990 int n = 0;
3992 if (iseq->aux.exec.local_hooks) {
3993 unsigned int pc;
3994 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3995 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3996 rb_event_flag_t local_events = 0;
3998 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3999 local_events = iseq->aux.exec.local_hooks->events;
4001 if (local_events == 0) {
4002 rb_hook_list_free(iseq->aux.exec.local_hooks);
4003 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
4006 local_events = add_bmethod_events(local_events);
4007 for (pc = 0; pc<body->iseq_size;) {
4008 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
4009 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false);
4012 return n;
4015 struct trace_clear_local_events_struct {
4016 VALUE tpval;
4017 int n;
4020 static void
4021 iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
4023 struct trace_clear_local_events_struct *data = (struct trace_clear_local_events_struct *)p;
4024 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
4025 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
4029 rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval)
4031 struct trace_clear_local_events_struct data;
4032 data.tpval = tpval;
4033 data.n = 0;
4035 iseq_remove_local_tracepoint_i(iseq, (void *)&data);
4036 return data.n;
4039 void
4040 rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
4042 if (iseq->aux.exec.global_trace_events == turnon_events) {
4043 return;
4046 if (!ISEQ_EXECUTABLE_P(iseq)) {
4047 /* this is building ISeq */
4048 return;
4050 else {
4051 unsigned int pc;
4052 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
4053 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
4054 rb_event_flag_t enabled_events;
4055 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
4056 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
4057 enabled_events = add_bmethod_events(turnon_events | local_events);
4059 for (pc=0; pc<body->iseq_size;) {
4060 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
4061 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events, true);
4066 void rb_vm_cc_general(const struct rb_callcache *cc);
4068 static bool
4069 clear_attr_cc(VALUE v)
4071 if (imemo_type_p(v, imemo_callcache) && vm_cc_ivar_p((const struct rb_callcache *)v)) {
4072 rb_vm_cc_general((struct rb_callcache *)v);
4073 return true;
4075 else {
4076 return false;
4080 static bool
4081 clear_bf_cc(VALUE v)
4083 if (imemo_type_p(v, imemo_callcache) && vm_cc_bf_p((const struct rb_callcache *)v)) {
4084 rb_vm_cc_general((struct rb_callcache *)v);
4085 return true;
4087 else {
4088 return false;
4092 static int
4093 clear_attr_ccs_i(void *vstart, void *vend, size_t stride, void *data)
4095 VALUE v = (VALUE)vstart;
4096 for (; v != (VALUE)vend; v += stride) {
4097 void *ptr = rb_asan_poisoned_object_p(v);
4098 rb_asan_unpoison_object(v, false);
4099 clear_attr_cc(v);
4100 asan_poison_object_if(ptr, v);
4102 return 0;
4105 void
4106 rb_clear_attr_ccs(void)
4108 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
4111 static int
4112 clear_bf_ccs_i(void *vstart, void *vend, size_t stride, void *data)
4114 VALUE v = (VALUE)vstart;
4115 for (; v != (VALUE)vend; v += stride) {
4116 void *ptr = rb_asan_poisoned_object_p(v);
4117 rb_asan_unpoison_object(v, false);
4118 clear_bf_cc(v);
4119 asan_poison_object_if(ptr, v);
4121 return 0;
4124 void
4125 rb_clear_bf_ccs(void)
4127 rb_objspace_each_objects(clear_bf_ccs_i, NULL);
4130 static int
4131 trace_set_i(void *vstart, void *vend, size_t stride, void *data)
4133 rb_event_flag_t turnon_events = *(rb_event_flag_t *)data;
4135 VALUE v = (VALUE)vstart;
4136 for (; v != (VALUE)vend; v += stride) {
4137 void *ptr = rb_asan_poisoned_object_p(v);
4138 rb_asan_unpoison_object(v, false);
4140 if (rb_obj_is_iseq(v)) {
4141 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
4143 else if (clear_attr_cc(v)) {
4145 else if (clear_bf_cc(v)) {
4148 asan_poison_object_if(ptr, v);
4150 return 0;
4153 void
4154 rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
4156 rb_objspace_each_objects(trace_set_i, &turnon_events);
4159 VALUE
4160 rb_iseqw_local_variables(VALUE iseqval)
4162 return rb_iseq_local_variables(iseqw_check(iseqval));
4166 * call-seq:
4167 * iseq.to_binary(extra_data = nil) -> binary str
4169 * Returns serialized iseq binary format data as a String object.
4170 * A corresponding iseq object is created by
4171 * RubyVM::InstructionSequence.load_from_binary() method.
4173 * String extra_data will be saved with binary data.
4174 * You can access this data with
4175 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary).
4177 * Note that the translated binary data is not portable.
4178 * You can not move this binary data to another machine.
4179 * You can not use the binary data which is created by another
4180 * version/another architecture of Ruby.
4182 static VALUE
4183 iseqw_to_binary(int argc, VALUE *argv, VALUE self)
4185 VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0];
4186 return rb_iseq_ibf_dump(iseqw_check(self), opt);
4190 * call-seq:
4191 * RubyVM::InstructionSequence.load_from_binary(binary) -> iseq
4193 * Load an iseq object from binary format String object
4194 * created by RubyVM::InstructionSequence.to_binary.
4196 * This loader does not have a verifier, so that loading broken/modified
4197 * binary causes critical problem.
4199 * You should not load binary data provided by others.
4200 * You should use binary data translated by yourself.
4202 static VALUE
4203 iseqw_s_load_from_binary(VALUE self, VALUE str)
4205 return iseqw_new(rb_iseq_ibf_load(str));
4209 * call-seq:
4210 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary) -> str
4212 * Load extra data embed into binary format String object.
4214 static VALUE
4215 iseqw_s_load_from_binary_extra_data(VALUE self, VALUE str)
4217 return rb_iseq_ibf_load_extra_data(str);
4220 #if VM_INSN_INFO_TABLE_IMPL == 2
4222 /* An implementation of succinct bit-vector for insn_info table.
4224 * A succinct bit-vector is a small and efficient data structure that provides
4225 * a bit-vector augmented with an index for O(1) rank operation:
4227 * rank(bv, n): the number of 1's within a range from index 0 to index n
4229 * This can be used to lookup insn_info table from PC.
4230 * For example, consider the following iseq and insn_info_table:
4232 * iseq insn_info_table
4233 * PC insn+operand position lineno event
4234 * 0: insn1 0: 1 [Li]
4235 * 2: insn2 2: 2 [Li] <= (A)
4236 * 5: insn3 8: 3 [Li] <= (B)
4237 * 8: insn4
4239 * In this case, a succinct bit-vector whose indexes 0, 2, 8 is "1" and
4240 * other indexes is "0", i.e., "101000001", is created.
4241 * To lookup the lineno of insn2, calculate rank("10100001", 2) = 2, so
4242 * the line (A) is the entry in question.
4243 * To lookup the lineno of insn4, calculate rank("10100001", 8) = 3, so
4244 * the line (B) is the entry in question.
4246 * A naive implementation of succinct bit-vector works really well
4247 * not only for large size but also for small size. However, it has
4248 * tiny overhead for very small size. So, this implementation consist
4249 * of two parts: one part is the "immediate" table that keeps rank result
4250 * as a raw table, and the other part is a normal succinct bit-vector.
4253 #define IMMEDIATE_TABLE_SIZE 54 /* a multiple of 9, and < 128 */
4255 struct succ_index_table {
4256 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
4257 struct succ_dict_block {
4258 unsigned int rank;
4259 uint64_t small_block_ranks; /* 9 bits * 7 = 63 bits */
4260 uint64_t bits[512/64];
4261 } succ_part[FLEX_ARY_LEN];
4264 #define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
4265 #define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
4266 #define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
4267 #define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
4269 static struct succ_index_table *
4270 succ_index_table_create(int max_pos, int *data, int size)
4272 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4273 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4274 struct succ_index_table *sd =
4275 rb_xcalloc_mul_add_mul(
4276 imm_size, sizeof(uint64_t),
4277 succ_size, sizeof(struct succ_dict_block));
4278 int i, j, k, r;
4280 r = 0;
4281 for (j = 0; j < imm_size; j++) {
4282 for (i = 0; i < 9; i++) {
4283 if (r < size && data[r] == j * 9 + i) r++;
4284 imm_block_rank_set(sd->imm_part[j], i, r);
4287 for (k = 0; k < succ_size; k++) {
4288 struct succ_dict_block *sd_block = &sd->succ_part[k];
4289 int small_rank = 0;
4290 sd_block->rank = r;
4291 for (j = 0; j < 8; j++) {
4292 uint64_t bits = 0;
4293 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
4294 for (i = 0; i < 64; i++) {
4295 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
4296 bits |= ((uint64_t)1) << i;
4297 r++;
4300 sd_block->bits[j] = bits;
4301 small_rank += rb_popcount64(bits);
4304 return sd;
4307 static unsigned int *
4308 succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size)
4310 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
4311 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
4312 unsigned int *positions = ALLOC_N(unsigned int, size), *p;
4313 int i, j, k, r = -1;
4314 p = positions;
4315 for (j = 0; j < imm_size; j++) {
4316 for (i = 0; i < 9; i++) {
4317 int nr = imm_block_rank_get(sd->imm_part[j], i);
4318 if (r != nr) *p++ = j * 9 + i;
4319 r = nr;
4322 for (k = 0; k < succ_size; k++) {
4323 for (j = 0; j < 8; j++) {
4324 for (i = 0; i < 64; i++) {
4325 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
4326 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
4331 return positions;
4334 static int
4335 succ_index_lookup(const struct succ_index_table *sd, int x)
4337 if (x < IMMEDIATE_TABLE_SIZE) {
4338 const int i = x / 9;
4339 const int j = x % 9;
4340 return imm_block_rank_get(sd->imm_part[i], j);
4342 else {
4343 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
4344 const struct succ_dict_block *block = &sd->succ_part[block_index];
4345 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
4346 const int small_block_index = block_bit_index / 64;
4347 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
4348 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
4350 return block->rank + small_block_popcount + popcnt;
4353 #endif
4357 * call-seq:
4358 * iseq.script_lines -> array or nil
4360 * It returns recorded script lines if it is available.
4361 * The script lines are not limited to the iseq range, but
4362 * are entire lines of the source file.
4364 * Note that this is an API for ruby internal use, debugging,
4365 * and research. Do not use this for any other purpose.
4366 * The compatibility is not guaranteed.
4368 static VALUE
4369 iseqw_script_lines(VALUE self)
4371 const rb_iseq_t *iseq = iseqw_check(self);
4372 return ISEQ_BODY(iseq)->variable.script_lines;
4376 * Document-class: RubyVM::InstructionSequence
4378 * The InstructionSequence class represents a compiled sequence of
4379 * instructions for the Virtual Machine used in MRI. Not all implementations of Ruby
4380 * may implement this class, and for the implementations that implement it,
4381 * the methods defined and behavior of the methods can change in any version.
4383 * With it, you can get a handle to the instructions that make up a method or
4384 * a proc, compile strings of Ruby code down to VM instructions, and
4385 * disassemble instruction sequences to strings for easy inspection. It is
4386 * mostly useful if you want to learn how YARV works, but it also lets
4387 * you control various settings for the Ruby iseq compiler.
4389 * You can find the source for the VM instructions in +insns.def+ in the Ruby
4390 * source.
4392 * The instruction sequence results will almost certainly change as Ruby
4393 * changes, so example output in this documentation may be different from what
4394 * you see.
4396 * Of course, this class is MRI specific.
4399 void
4400 Init_ISeq(void)
4402 /* declare ::RubyVM::InstructionSequence */
4403 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
4404 rb_undef_alloc_func(rb_cISeq);
4405 rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
4406 rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
4407 rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
4408 rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
4409 rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
4411 rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
4412 rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1);
4413 rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1);
4415 /* location APIs */
4416 rb_define_method(rb_cISeq, "path", iseqw_path, 0);
4417 rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0);
4418 rb_define_method(rb_cISeq, "label", iseqw_label, 0);
4419 rb_define_method(rb_cISeq, "base_label", iseqw_base_label, 0);
4420 rb_define_method(rb_cISeq, "first_lineno", iseqw_first_lineno, 0);
4421 rb_define_method(rb_cISeq, "trace_points", iseqw_trace_points, 0);
4422 rb_define_method(rb_cISeq, "each_child", iseqw_each_child, 0);
4424 #if 0 /* TBD */
4425 rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
4426 rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
4427 /* disable this feature because there is no verifier. */
4428 rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
4429 #endif
4430 (void)iseq_s_load;
4432 rb_define_singleton_method(rb_cISeq, "compile", iseqw_s_compile, -1);
4433 rb_define_singleton_method(rb_cISeq, "compile_parsey", iseqw_s_compile_parsey, -1);
4434 rb_define_singleton_method(rb_cISeq, "compile_prism", iseqw_s_compile_prism, -1);
4435 rb_define_singleton_method(rb_cISeq, "compile_file_prism", iseqw_s_compile_file_prism, -1);
4436 rb_define_singleton_method(rb_cISeq, "new", iseqw_s_compile, -1);
4437 rb_define_singleton_method(rb_cISeq, "compile_file", iseqw_s_compile_file, -1);
4438 rb_define_singleton_method(rb_cISeq, "compile_option", iseqw_s_compile_option_get, 0);
4439 rb_define_singleton_method(rb_cISeq, "compile_option=", iseqw_s_compile_option_set, 1);
4440 rb_define_singleton_method(rb_cISeq, "disasm", iseqw_s_disasm, 1);
4441 rb_define_singleton_method(rb_cISeq, "disassemble", iseqw_s_disasm, 1);
4442 rb_define_singleton_method(rb_cISeq, "of", iseqw_s_of, 1);
4444 // script lines
4445 rb_define_method(rb_cISeq, "script_lines", iseqw_script_lines, 0);
4447 rb_undef_method(CLASS_OF(rb_cISeq), "translate");
4448 rb_undef_method(CLASS_OF(rb_cISeq), "load_iseq");