[ruby/etc] bump up to 1.3.1
[ruby-80x24.org.git] / iseq.c
blobf711367cfcd59c0133e9c0156b7212402e7f3ba1
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 "gc.h"
23 #include "id_table.h"
24 #include "internal.h"
25 #include "internal/bits.h"
26 #include "internal/class.h"
27 #include "internal/compile.h"
28 #include "internal/error.h"
29 #include "internal/file.h"
30 #include "internal/hash.h"
31 #include "internal/parse.h"
32 #include "internal/sanitizers.h"
33 #include "internal/symbol.h"
34 #include "internal/thread.h"
35 #include "internal/variable.h"
36 #include "iseq.h"
37 #include "mjit.h"
38 #include "ruby/util.h"
39 #include "vm_core.h"
40 #include "vm_callinfo.h"
41 #include "yjit.h"
42 #include "ruby/ractor.h"
43 #include "builtin.h"
44 #include "insns.inc"
45 #include "insns_info.inc"
47 VALUE rb_cISeq;
48 static VALUE iseqw_new(const rb_iseq_t *iseq);
49 static const rb_iseq_t *iseqw_check(VALUE iseqw);
51 #if VM_INSN_INFO_TABLE_IMPL == 2
52 static struct succ_index_table *succ_index_table_create(int max_pos, int *data, int size);
53 static unsigned int *succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size);
54 static int succ_index_lookup(const struct succ_index_table *sd, int x);
55 #endif
57 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
59 static inline VALUE
60 obj_resurrect(VALUE obj)
62 if (hidden_obj_p(obj)) {
63 switch (BUILTIN_TYPE(obj)) {
64 case T_STRING:
65 obj = rb_str_resurrect(obj);
66 break;
67 case T_ARRAY:
68 obj = rb_ary_resurrect(obj);
69 break;
70 case T_HASH:
71 obj = rb_hash_resurrect(obj);
72 break;
73 default:
74 break;
77 return obj;
80 static void
81 free_arena(struct iseq_compile_data_storage *cur)
83 struct iseq_compile_data_storage *next;
85 while (cur) {
86 next = cur->next;
87 ruby_xfree(cur);
88 cur = next;
92 static void
93 compile_data_free(struct iseq_compile_data *compile_data)
95 if (compile_data) {
96 free_arena(compile_data->node.storage_head);
97 free_arena(compile_data->insn.storage_head);
98 if (compile_data->ivar_cache_table) {
99 rb_id_table_free(compile_data->ivar_cache_table);
101 ruby_xfree(compile_data);
105 void
106 rb_iseq_free(const rb_iseq_t *iseq)
108 RUBY_FREE_ENTER("iseq");
110 if (iseq && iseq->body) {
111 struct rb_iseq_constant_body *const body = iseq->body;
112 mjit_free_iseq(iseq); /* Notify MJIT */
113 rb_yjit_iseq_free(body);
114 ruby_xfree((void *)body->iseq_encoded);
115 ruby_xfree((void *)body->insns_info.body);
116 if (body->insns_info.positions) ruby_xfree((void *)body->insns_info.positions);
117 #if VM_INSN_INFO_TABLE_IMPL == 2
118 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
119 #endif
120 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
121 ruby_xfree((void *)body->local_table);
122 ruby_xfree((void *)body->is_entries);
124 if (body->call_data) {
125 ruby_xfree(body->call_data);
127 ruby_xfree((void *)body->catch_table);
128 ruby_xfree((void *)body->param.opt_table);
130 if (body->param.keyword != NULL) {
131 ruby_xfree((void *)body->param.keyword->default_values);
132 ruby_xfree((void *)body->param.keyword);
134 compile_data_free(ISEQ_COMPILE_DATA(iseq));
135 if (body->outer_variables) rb_id_table_free(body->outer_variables);
136 ruby_xfree(body);
139 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
140 rb_hook_list_free(iseq->aux.exec.local_hooks);
143 RUBY_FREE_LEAVE("iseq");
146 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
147 static VALUE
148 rb_vm_insn_addr2insn2(const void *addr)
150 return (VALUE)rb_vm_insn_addr2insn(addr);
152 #endif
154 static VALUE
155 rb_vm_insn_null_translator(const void *addr)
157 return (VALUE)addr;
160 typedef VALUE iseq_value_itr_t(void *ctx, VALUE obj);
161 typedef VALUE rb_vm_insns_translator_t(const void *addr);
163 static int
164 iseq_extract_values(VALUE *code, size_t pos, iseq_value_itr_t * func, void *data, rb_vm_insns_translator_t * translator)
166 VALUE insn = translator((void *)code[pos]);
167 int len = insn_len(insn);
168 int op_no;
169 const char *types = insn_op_types(insn);
171 for (op_no = 0; types[op_no]; op_no++) {
172 char type = types[op_no];
173 switch (type) {
174 case TS_CDHASH:
175 case TS_ISEQ:
176 case TS_VALUE:
178 VALUE op = code[pos + op_no + 1];
179 if (!SPECIAL_CONST_P(op)) {
180 VALUE newop = func(data, op);
181 if (newop != op) {
182 code[pos + op_no + 1] = newop;
186 break;
187 case TS_IC:
189 IC ic = (IC)code[pos + op_no + 1];
190 if (ic->entry) {
191 VALUE nv = func(data, (VALUE)ic->entry);
192 if ((VALUE)ic->entry != nv) {
193 ic->entry = (void *)nv;
197 break;
198 case TS_IVC:
200 IVC ivc = (IVC)code[pos + op_no + 1];
201 if (ivc->entry) {
202 if (RB_TYPE_P(ivc->entry->class_value, T_NONE)) {
203 rb_bug("!! %u", ivc->entry->index);
205 VALUE nv = func(data, ivc->entry->class_value);
206 if (ivc->entry->class_value != nv) {
207 ivc->entry->class_value = nv;
211 break;
212 case TS_ISE:
214 union iseq_inline_storage_entry *const is = (union iseq_inline_storage_entry *)code[pos + op_no + 1];
215 if (is->once.value) {
216 VALUE nv = func(data, is->once.value);
217 if (is->once.value != nv) {
218 is->once.value = nv;
222 break;
223 default:
224 break;
228 return len;
231 static void
232 rb_iseq_each_value(const rb_iseq_t *iseq, iseq_value_itr_t * func, void *data)
234 unsigned int size;
235 VALUE *code;
236 size_t n;
237 rb_vm_insns_translator_t *const translator =
238 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
239 (FL_TEST((VALUE)iseq, ISEQ_TRANSLATED)) ? rb_vm_insn_addr2insn2 :
240 #endif
241 rb_vm_insn_null_translator;
242 const struct rb_iseq_constant_body *const body = iseq->body;
244 size = body->iseq_size;
245 code = body->iseq_encoded;
247 for (n = 0; n < size;) {
248 n += iseq_extract_values(code, n, func, data, translator);
252 static VALUE
253 update_each_insn_value(void *ctx, VALUE obj)
255 return rb_gc_location(obj);
258 void
259 rb_iseq_update_references(rb_iseq_t *iseq)
261 if (iseq->body) {
262 struct rb_iseq_constant_body *body = iseq->body;
264 body->variable.coverage = rb_gc_location(body->variable.coverage);
265 body->variable.pc2branchindex = rb_gc_location(body->variable.pc2branchindex);
266 body->variable.script_lines = rb_gc_location(body->variable.script_lines);
267 body->location.label = rb_gc_location(body->location.label);
268 body->location.base_label = rb_gc_location(body->location.base_label);
269 body->location.pathobj = rb_gc_location(body->location.pathobj);
270 if (body->local_iseq) {
271 body->local_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->local_iseq);
273 if (body->parent_iseq) {
274 body->parent_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->parent_iseq);
276 if (body->mandatory_only_iseq) {
277 body->mandatory_only_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->mandatory_only_iseq);
279 if (body->call_data) {
280 for (unsigned int i=0; i<body->ci_size; i++) {
281 struct rb_call_data *cds = body->call_data;
282 if (!SPECIAL_CONST_P((VALUE)cds[i].ci)) {
283 cds[i].ci = (struct rb_callinfo *)rb_gc_location((VALUE)cds[i].ci);
285 cds[i].cc = (struct rb_callcache *)rb_gc_location((VALUE)cds[i].cc);
288 if (FL_TEST((VALUE)iseq, ISEQ_MARKABLE_ISEQ)) {
289 rb_iseq_each_value(iseq, update_each_insn_value, NULL);
290 VALUE *original_iseq = ISEQ_ORIGINAL_ISEQ(iseq);
291 if (original_iseq) {
292 size_t n = 0;
293 const unsigned int size = body->iseq_size;
294 while (n < size) {
295 n += iseq_extract_values(original_iseq, n, update_each_insn_value, NULL, rb_vm_insn_null_translator);
300 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
301 int i, j;
303 i = body->param.keyword->required_num;
305 for (j = 0; i < body->param.keyword->num; i++, j++) {
306 VALUE obj = body->param.keyword->default_values[j];
307 if (obj != Qundef) {
308 body->param.keyword->default_values[j] = rb_gc_location(obj);
313 if (body->catch_table) {
314 struct iseq_catch_table *table = body->catch_table;
315 unsigned int i;
316 for (i = 0; i < table->size; i++) {
317 struct iseq_catch_table_entry *entry;
318 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
319 if (entry->iseq) {
320 entry->iseq = (rb_iseq_t *)rb_gc_location((VALUE)entry->iseq);
324 #if USE_MJIT
325 mjit_update_references(iseq);
326 #endif
327 rb_yjit_iseq_update_references(body);
331 static VALUE
332 each_insn_value(void *ctx, VALUE obj)
334 rb_gc_mark_movable(obj);
335 return obj;
338 void
339 rb_iseq_mark(const rb_iseq_t *iseq)
341 RUBY_MARK_ENTER("iseq");
343 RUBY_MARK_UNLESS_NULL(iseq->wrapper);
345 if (iseq->body) {
346 const struct rb_iseq_constant_body *const body = iseq->body;
348 if (FL_TEST((VALUE)iseq, ISEQ_MARKABLE_ISEQ)) {
349 rb_iseq_each_value(iseq, each_insn_value, NULL);
352 rb_gc_mark_movable(body->variable.coverage);
353 rb_gc_mark_movable(body->variable.pc2branchindex);
354 rb_gc_mark_movable(body->variable.script_lines);
355 rb_gc_mark_movable(body->location.label);
356 rb_gc_mark_movable(body->location.base_label);
357 rb_gc_mark_movable(body->location.pathobj);
358 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->mandatory_only_iseq);
359 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->parent_iseq);
361 if (body->call_data) {
362 struct rb_call_data *cds = (struct rb_call_data *)body->call_data;
363 for (unsigned int i=0; i<body->ci_size; i++) {
364 const struct rb_callinfo *ci = cds[i].ci;
365 const struct rb_callcache *cc = cds[i].cc;
367 if (vm_ci_markable(ci)) {
368 rb_gc_mark_movable((VALUE)ci);
371 if (cc) {
372 VM_ASSERT((cc->flags & VM_CALLCACHE_ON_STACK) == 0);
374 if (vm_cc_markable(cc)) {
375 if (!vm_cc_invalidated_p(cc)) {
376 rb_gc_mark_movable((VALUE)cc);
378 else {
379 cds[i].cc = rb_vm_empty_cc();
386 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
387 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
388 int i, j;
390 i = keyword->required_num;
392 for (j = 0; i < keyword->num; i++, j++) {
393 VALUE obj = keyword->default_values[j];
394 if (!SPECIAL_CONST_P(obj)) {
395 rb_gc_mark_movable(obj);
400 if (body->catch_table) {
401 const struct iseq_catch_table *table = body->catch_table;
402 unsigned int i;
403 for (i = 0; i < table->size; i++) {
404 const struct iseq_catch_table_entry *entry;
405 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
406 if (entry->iseq) {
407 rb_gc_mark_movable((VALUE)entry->iseq);
412 #if USE_MJIT
413 mjit_mark_cc_entries(body);
414 #endif
415 rb_yjit_iseq_mark(body);
418 if (FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) {
419 rb_gc_mark(iseq->aux.loader.obj);
421 else if (FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) {
422 const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
424 rb_iseq_mark_insn_storage(compile_data->insn.storage_head);
426 RUBY_MARK_UNLESS_NULL(compile_data->err_info);
427 if (RTEST(compile_data->catch_table_ary)) {
428 rb_gc_mark(compile_data->catch_table_ary);
430 VM_ASSERT(compile_data != NULL);
432 else {
433 /* executable */
434 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
435 if (iseq->aux.exec.local_hooks) {
436 rb_hook_list_mark(iseq->aux.exec.local_hooks);
440 RUBY_MARK_LEAVE("iseq");
443 static size_t
444 param_keyword_size(const struct rb_iseq_param_keyword *pkw)
446 size_t size = 0;
448 if (!pkw) return size;
450 size += sizeof(struct rb_iseq_param_keyword);
451 size += sizeof(VALUE) * (pkw->num - pkw->required_num);
453 return size;
456 size_t
457 rb_iseq_memsize(const rb_iseq_t *iseq)
459 size_t size = 0; /* struct already counted as RVALUE size */
460 const struct rb_iseq_constant_body *body = iseq->body;
461 const struct iseq_compile_data *compile_data;
463 /* TODO: should we count original_iseq? */
465 if (ISEQ_EXECUTABLE_P(iseq) && body) {
466 size += sizeof(struct rb_iseq_constant_body);
467 size += body->iseq_size * sizeof(VALUE);
468 size += body->insns_info.size * (sizeof(struct iseq_insn_info_entry) + sizeof(unsigned int));
469 size += body->local_table_size * sizeof(ID);
470 if (body->catch_table) {
471 size += iseq_catch_table_bytes(body->catch_table->size);
473 size += (body->param.opt_num + 1) * sizeof(VALUE);
474 size += param_keyword_size(body->param.keyword);
476 /* body->is_entries */
477 size += body->is_size * sizeof(union iseq_inline_storage_entry);
479 /* body->call_data */
480 size += body->ci_size * sizeof(struct rb_call_data);
481 // TODO: should we count imemo_callinfo?
484 compile_data = ISEQ_COMPILE_DATA(iseq);
485 if (compile_data) {
486 struct iseq_compile_data_storage *cur;
488 size += sizeof(struct iseq_compile_data);
490 cur = compile_data->node.storage_head;
491 while (cur) {
492 size += cur->size + offsetof(struct iseq_compile_data_storage, buff);
493 cur = cur->next;
497 return size;
500 struct rb_iseq_constant_body *
501 rb_iseq_constant_body_alloc(void)
503 struct rb_iseq_constant_body *iseq_body;
504 iseq_body = ZALLOC(struct rb_iseq_constant_body);
505 return iseq_body;
508 static rb_iseq_t *
509 iseq_alloc(void)
511 rb_iseq_t *iseq = iseq_imemo_alloc();
512 iseq->body = rb_iseq_constant_body_alloc();
513 return iseq;
516 VALUE
517 rb_iseq_pathobj_new(VALUE path, VALUE realpath)
519 VALUE pathobj;
520 VM_ASSERT(RB_TYPE_P(path, T_STRING));
521 VM_ASSERT(NIL_P(realpath) || RB_TYPE_P(realpath, T_STRING));
523 if (path == realpath ||
524 (!NIL_P(realpath) && rb_str_cmp(path, realpath) == 0)) {
525 pathobj = rb_fstring(path);
527 else {
528 if (!NIL_P(realpath)) realpath = rb_fstring(realpath);
529 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
530 rb_obj_freeze(pathobj);
532 return pathobj;
535 void
536 rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath)
538 RB_OBJ_WRITE(iseq, &iseq->body->location.pathobj,
539 rb_iseq_pathobj_new(path, realpath));
542 static rb_iseq_location_t *
543 iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location, const int node_id)
545 rb_iseq_location_t *loc = &iseq->body->location;
547 rb_iseq_pathobj_set(iseq, path, realpath);
548 RB_OBJ_WRITE(iseq, &loc->label, name);
549 RB_OBJ_WRITE(iseq, &loc->base_label, name);
550 loc->first_lineno = first_lineno;
551 if (code_location) {
552 loc->node_id = node_id;
553 loc->code_location = *code_location;
555 else {
556 loc->code_location.beg_pos.lineno = 0;
557 loc->code_location.beg_pos.column = 0;
558 loc->code_location.end_pos.lineno = -1;
559 loc->code_location.end_pos.column = -1;
562 return loc;
565 static void
566 set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
568 struct rb_iseq_constant_body *const body = iseq->body;
569 const VALUE type = body->type;
571 /* set class nest stack */
572 if (type == ISEQ_TYPE_TOP) {
573 body->local_iseq = iseq;
575 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
576 body->local_iseq = iseq;
578 else if (piseq) {
579 body->local_iseq = piseq->body->local_iseq;
582 if (piseq) {
583 body->parent_iseq = piseq;
586 if (type == ISEQ_TYPE_MAIN) {
587 body->local_iseq = iseq;
591 static struct iseq_compile_data_storage *
592 new_arena(void)
594 struct iseq_compile_data_storage * new_arena =
595 (struct iseq_compile_data_storage *)
596 ALLOC_N(char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
597 offsetof(struct iseq_compile_data_storage, buff));
599 new_arena->pos = 0;
600 new_arena->next = 0;
601 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
603 return new_arena;
606 static VALUE
607 prepare_iseq_build(rb_iseq_t *iseq,
608 VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_code_location_t *code_location, const int node_id,
609 const rb_iseq_t *parent, int isolated_depth, enum iseq_type type,
610 VALUE script_lines, const rb_compile_option_t *option)
612 VALUE coverage = Qfalse;
613 VALUE err_info = Qnil;
614 struct rb_iseq_constant_body *const body = iseq->body;
616 if (parent && (type == ISEQ_TYPE_MAIN || type == ISEQ_TYPE_TOP))
617 err_info = Qfalse;
619 body->type = type;
620 set_relation(iseq, parent);
622 name = rb_fstring(name);
623 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
624 if (iseq != body->local_iseq) {
625 RB_OBJ_WRITE(iseq, &body->location.base_label, body->local_iseq->body->location.label);
627 ISEQ_COVERAGE_SET(iseq, Qnil);
628 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
629 body->variable.flip_count = 0;
631 if (NIL_P(script_lines)) {
632 RB_OBJ_WRITE(iseq, &body->variable.script_lines, Qnil);
634 else {
635 RB_OBJ_WRITE(iseq, &body->variable.script_lines, rb_ractor_make_shareable(script_lines));
638 ISEQ_COMPILE_DATA_ALLOC(iseq);
639 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
640 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, Qnil);
642 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
643 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
644 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
645 ISEQ_COMPILE_DATA(iseq)->option = option;
646 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
647 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
650 if (option->coverage_enabled) {
651 VALUE coverages = rb_get_coverages();
652 if (RTEST(coverages)) {
653 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
654 if (NIL_P(coverage)) coverage = Qfalse;
657 ISEQ_COVERAGE_SET(iseq, coverage);
658 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
659 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_tmp_new(0));
661 return Qtrue;
664 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
665 static void validate_get_insn_info(const rb_iseq_t *iseq);
666 #endif
668 void
669 rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq)
671 #if VM_INSN_INFO_TABLE_IMPL == 2
672 /* create succ_index_table */
673 struct rb_iseq_constant_body *const body = iseq->body;
674 int size = body->insns_info.size;
675 int max_pos = body->iseq_size;
676 int *data = (int *)body->insns_info.positions;
677 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
678 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
679 #if VM_CHECK_MODE == 0
680 ruby_xfree(body->insns_info.positions);
681 body->insns_info.positions = NULL;
682 #endif
683 #endif
686 #if VM_INSN_INFO_TABLE_IMPL == 2
687 unsigned int *
688 rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body)
690 int size = body->insns_info.size;
691 int max_pos = body->iseq_size;
692 struct succ_index_table *sd = body->insns_info.succ_index_table;
693 return succ_index_table_invert(max_pos, sd, size);
695 #endif
697 void
698 rb_iseq_init_trace(rb_iseq_t *iseq)
700 iseq->aux.exec.global_trace_events = 0;
701 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
702 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
706 static VALUE
707 finish_iseq_build(rb_iseq_t *iseq)
709 struct iseq_compile_data *data = ISEQ_COMPILE_DATA(iseq);
710 const struct rb_iseq_constant_body *const body = iseq->body;
711 VALUE err = data->err_info;
712 ISEQ_COMPILE_DATA_CLEAR(iseq);
713 compile_data_free(data);
715 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
716 validate_get_insn_info(iseq);
717 #endif
719 if (RTEST(err)) {
720 VALUE path = pathobj_path(body->location.pathobj);
721 if (err == Qtrue) err = rb_exc_new_cstr(rb_eSyntaxError, "compile error");
722 rb_funcallv(err, rb_intern("set_backtrace"), 1, &path);
723 rb_exc_raise(err);
726 RB_DEBUG_COUNTER_INC(iseq_num);
727 RB_DEBUG_COUNTER_ADD(iseq_cd_num, iseq->body->ci_size);
729 rb_iseq_init_trace(iseq);
730 return Qtrue;
733 static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
734 OPT_INLINE_CONST_CACHE, /* int inline_const_cache; */
735 OPT_PEEPHOLE_OPTIMIZATION, /* int peephole_optimization; */
736 OPT_TAILCALL_OPTIMIZATION, /* int tailcall_optimization */
737 OPT_SPECIALISED_INSTRUCTION, /* int specialized_instruction; */
738 OPT_OPERANDS_UNIFICATION, /* int operands_unification; */
739 OPT_INSTRUCTIONS_UNIFICATION, /* int instructions_unification; */
740 OPT_STACK_CACHING, /* int stack_caching; */
741 OPT_FROZEN_STRING_LITERAL,
742 OPT_DEBUG_FROZEN_STRING_LITERAL,
743 TRUE, /* coverage_enabled */
746 static const rb_compile_option_t COMPILE_OPTION_FALSE = {0};
748 static void
749 set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt)
751 #define SET_COMPILE_OPTION(o, h, mem) \
752 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
753 if (flag == Qtrue) { (o)->mem = 1; } \
754 else if (flag == Qfalse) { (o)->mem = 0; } \
756 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
757 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
758 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
760 SET_COMPILE_OPTION(option, opt, inline_const_cache);
761 SET_COMPILE_OPTION(option, opt, peephole_optimization);
762 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
763 SET_COMPILE_OPTION(option, opt, specialized_instruction);
764 SET_COMPILE_OPTION(option, opt, operands_unification);
765 SET_COMPILE_OPTION(option, opt, instructions_unification);
766 SET_COMPILE_OPTION(option, opt, stack_caching);
767 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
768 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
769 SET_COMPILE_OPTION(option, opt, coverage_enabled);
770 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
771 #undef SET_COMPILE_OPTION
772 #undef SET_COMPILE_OPTION_NUM
775 static void
776 rb_iseq_make_compile_option(rb_compile_option_t *option, VALUE opt)
778 Check_Type(opt, T_HASH);
779 set_compile_option_from_hash(option, opt);
782 static void
783 make_compile_option(rb_compile_option_t *option, VALUE opt)
785 if (NIL_P(opt)) {
786 *option = COMPILE_OPTION_DEFAULT;
788 else if (opt == Qfalse) {
789 *option = COMPILE_OPTION_FALSE;
791 else if (opt == Qtrue) {
792 int i;
793 for (i = 0; i < (int)(sizeof(rb_compile_option_t) / sizeof(int)); ++i)
794 ((int *)option)[i] = 1;
796 else if (RB_TYPE_P(opt, T_HASH)) {
797 *option = COMPILE_OPTION_DEFAULT;
798 set_compile_option_from_hash(option, opt);
800 else {
801 rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
805 static VALUE
806 make_compile_option_value(rb_compile_option_t *option)
808 VALUE opt = rb_hash_new_with_size(11);
809 #define SET_COMPILE_OPTION(o, h, mem) \
810 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
811 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
812 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
814 SET_COMPILE_OPTION(option, opt, inline_const_cache);
815 SET_COMPILE_OPTION(option, opt, peephole_optimization);
816 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
817 SET_COMPILE_OPTION(option, opt, specialized_instruction);
818 SET_COMPILE_OPTION(option, opt, operands_unification);
819 SET_COMPILE_OPTION(option, opt, instructions_unification);
820 SET_COMPILE_OPTION(option, opt, stack_caching);
821 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
822 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
823 SET_COMPILE_OPTION(option, opt, coverage_enabled);
824 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
826 #undef SET_COMPILE_OPTION
827 #undef SET_COMPILE_OPTION_NUM
828 return opt;
831 rb_iseq_t *
832 rb_iseq_new(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
833 const rb_iseq_t *parent, enum iseq_type type)
835 return rb_iseq_new_with_opt(ast, name, path, realpath, INT2FIX(0), parent,
836 0, type, &COMPILE_OPTION_DEFAULT);
839 static int
840 ast_line_count(const rb_ast_body_t *ast)
842 if (ast->script_lines == Qfalse) {
843 // this occurs when failed to parse the source code with a syntax error
844 return 0;
846 if (RB_TYPE_P(ast->script_lines, T_ARRAY)){
847 return (int)RARRAY_LEN(ast->script_lines);
849 return FIX2INT(ast->script_lines);
852 rb_iseq_t *
853 rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent)
855 VALUE coverages = rb_get_coverages();
856 if (RTEST(coverages)) {
857 int line_count = ast_line_count(ast);
858 if (line_count >= 0) {
859 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
860 VALUE coverage = rb_default_coverage(len);
861 rb_hash_aset(coverages, path, coverage);
865 return rb_iseq_new_with_opt(ast, name, path, realpath, INT2FIX(0), parent, 0,
866 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
869 rb_iseq_t *
870 rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt)
872 return rb_iseq_new_with_opt(ast, rb_fstring_lit("<main>"),
873 path, realpath, INT2FIX(0),
874 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
877 rb_iseq_t *
878 rb_iseq_new_eval(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno, const rb_iseq_t *parent, int isolated_depth)
880 return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
881 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
884 static inline rb_iseq_t *
885 iseq_translate(rb_iseq_t *iseq)
887 if (rb_respond_to(rb_cISeq, rb_intern("translate"))) {
888 VALUE v1 = iseqw_new(iseq);
889 VALUE v2 = rb_funcall(rb_cISeq, rb_intern("translate"), 1, v1);
890 if (v1 != v2 && CLASS_OF(v2) == rb_cISeq) {
891 iseq = (rb_iseq_t *)iseqw_check(v2);
895 return iseq;
898 rb_iseq_t *
899 rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
900 VALUE first_lineno, const rb_iseq_t *parent, int isolated_depth,
901 enum iseq_type type, const rb_compile_option_t *option)
903 const NODE *node = ast ? ast->root : 0;
904 /* TODO: argument check */
905 rb_iseq_t *iseq = iseq_alloc();
906 rb_compile_option_t new_opt;
908 if (option) {
909 new_opt = *option;
911 else {
912 new_opt = COMPILE_OPTION_DEFAULT;
914 if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option);
916 VALUE script_lines = Qnil;
918 if (ast && !FIXNUM_P(ast->script_lines) && ast->script_lines) {
919 script_lines = ast->script_lines;
921 else if (parent) {
922 script_lines = parent->body->variable.script_lines;
925 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
926 parent, isolated_depth, type, script_lines, &new_opt);
928 rb_iseq_compile_node(iseq, node);
929 finish_iseq_build(iseq);
931 return iseq_translate(iseq);
934 rb_iseq_t *
935 rb_iseq_new_with_callback(
936 const struct rb_iseq_new_with_callback_callback_func * ifunc,
937 VALUE name, VALUE path, VALUE realpath,
938 VALUE first_lineno, const rb_iseq_t *parent,
939 enum iseq_type type, const rb_compile_option_t *option)
941 /* TODO: argument check */
942 rb_iseq_t *iseq = iseq_alloc();
944 if (!option) option = &COMPILE_OPTION_DEFAULT;
945 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0, type, Qnil, option);
947 rb_iseq_compile_callback(iseq, ifunc);
948 finish_iseq_build(iseq);
950 return iseq;
953 const rb_iseq_t *
954 rb_iseq_load_iseq(VALUE fname)
956 VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname);
958 if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) {
959 return iseqw_check(iseqv);
962 return NULL;
965 #define CHECK_ARRAY(v) rb_to_array_type(v)
966 #define CHECK_HASH(v) rb_to_hash_type(v)
967 #define CHECK_STRING(v) rb_str_to_str(v)
968 #define CHECK_SYMBOL(v) rb_to_symbol_type(v)
969 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
971 static enum iseq_type
972 iseq_type_from_sym(VALUE type)
974 const ID id_top = rb_intern("top");
975 const ID id_method = rb_intern("method");
976 const ID id_block = rb_intern("block");
977 const ID id_class = rb_intern("class");
978 const ID id_rescue = rb_intern("rescue");
979 const ID id_ensure = rb_intern("ensure");
980 const ID id_eval = rb_intern("eval");
981 const ID id_main = rb_intern("main");
982 const ID id_plain = rb_intern("plain");
983 /* ensure all symbols are static or pinned down before
984 * conversion */
985 const ID typeid = rb_check_id(&type);
986 if (typeid == id_top) return ISEQ_TYPE_TOP;
987 if (typeid == id_method) return ISEQ_TYPE_METHOD;
988 if (typeid == id_block) return ISEQ_TYPE_BLOCK;
989 if (typeid == id_class) return ISEQ_TYPE_CLASS;
990 if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
991 if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
992 if (typeid == id_eval) return ISEQ_TYPE_EVAL;
993 if (typeid == id_main) return ISEQ_TYPE_MAIN;
994 if (typeid == id_plain) return ISEQ_TYPE_PLAIN;
995 return (enum iseq_type)-1;
998 static VALUE
999 iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
1001 rb_iseq_t *iseq = iseq_alloc();
1003 VALUE magic, version1, version2, format_type, misc;
1004 VALUE name, path, realpath, first_lineno, code_location, node_id;
1005 VALUE type, body, locals, params, exception;
1007 st_data_t iseq_type;
1008 rb_compile_option_t option;
1009 int i = 0;
1010 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1012 /* [magic, major_version, minor_version, format_type, misc,
1013 * label, path, first_lineno,
1014 * type, locals, args, exception_table, body]
1017 data = CHECK_ARRAY(data);
1019 magic = CHECK_STRING(rb_ary_entry(data, i++));
1020 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1021 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1022 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1023 misc = CHECK_HASH(rb_ary_entry(data, i++));
1024 ((void)magic, (void)version1, (void)version2, (void)format_type);
1026 name = CHECK_STRING(rb_ary_entry(data, i++));
1027 path = CHECK_STRING(rb_ary_entry(data, i++));
1028 realpath = rb_ary_entry(data, i++);
1029 realpath = NIL_P(realpath) ? Qnil : CHECK_STRING(realpath);
1030 first_lineno = CHECK_INTEGER(rb_ary_entry(data, i++));
1032 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1033 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1034 params = CHECK_HASH(rb_ary_entry(data, i++));
1035 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1036 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1038 iseq->body->local_iseq = iseq;
1040 iseq_type = iseq_type_from_sym(type);
1041 if (iseq_type == (enum iseq_type)-1) {
1042 rb_raise(rb_eTypeError, "unsupported type: :%"PRIsVALUE, rb_sym2str(type));
1045 node_id = rb_hash_aref(misc, ID2SYM(rb_intern("node_id")));
1047 code_location = rb_hash_aref(misc, ID2SYM(rb_intern("code_location")));
1048 if (RB_TYPE_P(code_location, T_ARRAY) && RARRAY_LEN(code_location) == 4) {
1049 tmp_loc.beg_pos.lineno = NUM2INT(rb_ary_entry(code_location, 0));
1050 tmp_loc.beg_pos.column = NUM2INT(rb_ary_entry(code_location, 1));
1051 tmp_loc.end_pos.lineno = NUM2INT(rb_ary_entry(code_location, 2));
1052 tmp_loc.end_pos.column = NUM2INT(rb_ary_entry(code_location, 3));
1055 make_compile_option(&option, opt);
1056 option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
1057 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc, NUM2INT(node_id),
1058 parent, 0, (enum iseq_type)iseq_type, Qnil, &option);
1060 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1062 finish_iseq_build(iseq);
1064 return iseqw_new(iseq);
1068 * :nodoc:
1070 static VALUE
1071 iseq_s_load(int argc, VALUE *argv, VALUE self)
1073 VALUE data, opt=Qnil;
1074 rb_scan_args(argc, argv, "11", &data, &opt);
1075 return iseq_load(data, NULL, opt);
1078 VALUE
1079 rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
1081 return iseq_load(data, RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1084 static rb_iseq_t *
1085 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1087 rb_iseq_t *iseq = NULL;
1088 rb_compile_option_t option;
1089 #if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1090 # define INITIALIZED volatile /* suppress warnings by gcc 4.8 */
1091 #else
1092 # define INITIALIZED /* volatile */
1093 #endif
1094 rb_ast_t *(*parse)(VALUE vparser, VALUE fname, VALUE file, int start);
1095 int ln;
1096 rb_ast_t *INITIALIZED ast;
1098 /* safe results first */
1099 make_compile_option(&option, opt);
1100 ln = NUM2INT(line);
1101 StringValueCStr(file);
1102 if (RB_TYPE_P(src, T_FILE)) {
1103 parse = rb_parser_compile_file_path;
1105 else {
1106 parse = rb_parser_compile_string_path;
1107 StringValue(src);
1110 const VALUE parser = rb_parser_new();
1111 VALUE name = rb_fstring_lit("<compiled>");
1112 const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name, Qnil, 0, ISEQ_TYPE_TOP);
1113 VALUE outer_scope_v = (VALUE)outer_scope;
1114 rb_parser_set_context(parser, outer_scope, FALSE);
1115 RB_GC_GUARD(outer_scope_v);
1116 ast = (*parse)(parser, file, src, ln);
1119 if (!ast->body.root) {
1120 rb_ast_dispose(ast);
1121 rb_exc_raise(GET_EC()->errinfo);
1123 else {
1124 INITIALIZED VALUE label = rb_fstring_lit("<compiled>");
1125 iseq = rb_iseq_new_with_opt(&ast->body, label, file, realpath, line,
1126 NULL, 0, ISEQ_TYPE_TOP, &option);
1127 rb_ast_dispose(ast);
1130 return iseq;
1133 VALUE
1134 rb_iseq_path(const rb_iseq_t *iseq)
1136 return pathobj_path(iseq->body->location.pathobj);
1139 VALUE
1140 rb_iseq_realpath(const rb_iseq_t *iseq)
1142 return pathobj_realpath(iseq->body->location.pathobj);
1145 VALUE
1146 rb_iseq_absolute_path(const rb_iseq_t *iseq)
1148 return rb_iseq_realpath(iseq);
1152 rb_iseq_from_eval_p(const rb_iseq_t *iseq)
1154 return NIL_P(rb_iseq_realpath(iseq));
1157 VALUE
1158 rb_iseq_label(const rb_iseq_t *iseq)
1160 return iseq->body->location.label;
1163 VALUE
1164 rb_iseq_base_label(const rb_iseq_t *iseq)
1166 return iseq->body->location.base_label;
1169 VALUE
1170 rb_iseq_first_lineno(const rb_iseq_t *iseq)
1172 return iseq->body->location.first_lineno;
1175 VALUE
1176 rb_iseq_method_name(const rb_iseq_t *iseq)
1178 struct rb_iseq_constant_body *const body = iseq->body->local_iseq->body;
1180 if (body->type == ISEQ_TYPE_METHOD) {
1181 return body->location.base_label;
1183 else {
1184 return Qnil;
1188 void
1189 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)
1191 const rb_code_location_t *loc = &iseq->body->location.code_location;
1192 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1193 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1194 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1195 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1198 static ID iseq_type_id(enum iseq_type type);
1200 VALUE
1201 rb_iseq_type(const rb_iseq_t *iseq)
1203 return ID2SYM(iseq_type_id(iseq->body->type));
1206 VALUE
1207 rb_iseq_coverage(const rb_iseq_t *iseq)
1209 return ISEQ_COVERAGE(iseq);
1212 static int
1213 remove_coverage_i(void *vstart, void *vend, size_t stride, void *data)
1215 VALUE v = (VALUE)vstart;
1216 for (; v != (VALUE)vend; v += stride) {
1217 void *ptr = asan_poisoned_object_p(v);
1218 asan_unpoison_object(v, false);
1220 if (rb_obj_is_iseq(v)) {
1221 rb_iseq_t *iseq = (rb_iseq_t *)v;
1222 ISEQ_COVERAGE_SET(iseq, Qnil);
1225 asan_poison_object_if(ptr, v);
1227 return 0;
1230 void
1231 rb_iseq_remove_coverage_all(void)
1233 rb_objspace_each_objects(remove_coverage_i, NULL);
1236 /* define wrapper class methods (RubyVM::InstructionSequence) */
1238 static void
1239 iseqw_mark(void *ptr)
1241 rb_gc_mark((VALUE)ptr);
1244 static size_t
1245 iseqw_memsize(const void *ptr)
1247 return rb_iseq_memsize((const rb_iseq_t *)ptr);
1250 static const rb_data_type_t iseqw_data_type = {
1251 "T_IMEMO/iseq",
1252 {iseqw_mark, NULL, iseqw_memsize,},
1253 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1256 static VALUE
1257 iseqw_new(const rb_iseq_t *iseq)
1259 if (iseq->wrapper) {
1260 return iseq->wrapper;
1262 else {
1263 union { const rb_iseq_t *in; void *out; } deconst;
1264 VALUE obj;
1265 deconst.in = iseq;
1266 obj = TypedData_Wrap_Struct(rb_cISeq, &iseqw_data_type, deconst.out);
1267 RB_OBJ_WRITTEN(obj, Qundef, iseq);
1269 /* cache a wrapper object */
1270 RB_OBJ_WRITE((VALUE)iseq, &iseq->wrapper, obj);
1271 RB_OBJ_FREEZE((VALUE)iseq);
1273 return obj;
1277 VALUE
1278 rb_iseqw_new(const rb_iseq_t *iseq)
1280 return iseqw_new(iseq);
1284 * call-seq:
1285 * InstructionSequence.compile(source[, file[, path[, line[, options]]]]) -> iseq
1286 * InstructionSequence.new(source[, file[, path[, line[, options]]]]) -> iseq
1288 * Takes +source+, a String of Ruby code and compiles it to an
1289 * InstructionSequence.
1291 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1292 * real path and first line number of the ruby code in +source+ which are
1293 * metadata attached to the returned +iseq+.
1295 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1296 * +require_relative+ base. It is recommended these should be the same full
1297 * path.
1299 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1300 * modify the default behavior of the Ruby iseq compiler.
1302 * For details regarding valid compile options see ::compile_option=.
1304 * RubyVM::InstructionSequence.compile("a = 1 + 2")
1305 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1307 * path = "test.rb"
1308 * RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
1309 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1311 * path = File.expand_path("test.rb")
1312 * RubyVM::InstructionSequence.compile(File.read(path), path, path)
1313 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1316 static VALUE
1317 iseqw_s_compile(int argc, VALUE *argv, VALUE self)
1319 VALUE src, file = Qnil, path = Qnil, line = INT2FIX(1), opt = Qnil;
1320 int i;
1322 i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt);
1323 if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5);
1324 switch (i) {
1325 case 5: opt = argv[--i];
1326 case 4: line = argv[--i];
1327 case 3: path = argv[--i];
1328 case 2: file = argv[--i];
1331 if (NIL_P(file)) file = rb_fstring_lit("<compiled>");
1332 if (NIL_P(path)) path = file;
1333 if (NIL_P(line)) line = INT2FIX(1);
1335 Check_Type(path, T_STRING);
1336 Check_Type(file, T_STRING);
1338 return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
1342 * call-seq:
1343 * InstructionSequence.compile_file(file[, options]) -> iseq
1345 * Takes +file+, a String with the location of a Ruby source file, reads,
1346 * parses and compiles the file, and returns +iseq+, the compiled
1347 * InstructionSequence with source location metadata set.
1349 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1350 * modify the default behavior of the Ruby iseq compiler.
1352 * For details regarding valid compile options see ::compile_option=.
1354 * # /tmp/hello.rb
1355 * puts "Hello, world!"
1357 * # elsewhere
1358 * RubyVM::InstructionSequence.compile_file("/tmp/hello.rb")
1359 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1361 static VALUE
1362 iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
1364 VALUE file, line = INT2FIX(1), opt = Qnil;
1365 VALUE parser, f, exc = Qnil, ret;
1366 rb_ast_t *ast;
1367 rb_compile_option_t option;
1368 int i;
1370 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1371 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1372 switch (i) {
1373 case 2: opt = argv[--i];
1375 FilePathValue(file);
1376 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1378 f = rb_file_open_str(file, "r");
1380 parser = rb_parser_new();
1381 rb_parser_set_context(parser, NULL, FALSE);
1382 ast = (rb_ast_t *)rb_parser_load_file(parser, file);
1383 if (!ast->body.root) exc = GET_EC()->errinfo;
1385 rb_io_close(f);
1386 if (!ast->body.root) {
1387 rb_ast_dispose(ast);
1388 rb_exc_raise(exc);
1391 make_compile_option(&option, opt);
1393 ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit("<main>"),
1394 file,
1395 rb_realpath_internal(Qnil, file, 1),
1396 line, NULL, 0, ISEQ_TYPE_TOP, &option));
1397 rb_ast_dispose(ast);
1398 return ret;
1402 * call-seq:
1403 * InstructionSequence.compile_option = options
1405 * Sets the default values for various optimizations in the Ruby iseq
1406 * compiler.
1408 * Possible values for +options+ include +true+, which enables all options,
1409 * +false+ which disables all options, and +nil+ which leaves all options
1410 * unchanged.
1412 * You can also pass a +Hash+ of +options+ that you want to change, any
1413 * options not present in the hash will be left unchanged.
1415 * Possible option names (which are keys in +options+) which can be set to
1416 * +true+ or +false+ include:
1418 * * +:inline_const_cache+
1419 * * +:instructions_unification+
1420 * * +:operands_unification+
1421 * * +:peephole_optimization+
1422 * * +:specialized_instruction+
1423 * * +:stack_caching+
1424 * * +:tailcall_optimization+
1426 * Additionally, +:debug_level+ can be set to an integer.
1428 * These default options can be overwritten for a single run of the iseq
1429 * compiler by passing any of the above values as the +options+ parameter to
1430 * ::new, ::compile and ::compile_file.
1432 static VALUE
1433 iseqw_s_compile_option_set(VALUE self, VALUE opt)
1435 rb_compile_option_t option;
1436 make_compile_option(&option, opt);
1437 COMPILE_OPTION_DEFAULT = option;
1438 return opt;
1442 * call-seq:
1443 * InstructionSequence.compile_option -> options
1445 * Returns a hash of default options used by the Ruby iseq compiler.
1447 * For details, see InstructionSequence.compile_option=.
1449 static VALUE
1450 iseqw_s_compile_option_get(VALUE self)
1452 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1455 static const rb_iseq_t *
1456 iseqw_check(VALUE iseqw)
1458 rb_iseq_t *iseq = DATA_PTR(iseqw);
1460 if (!iseq->body) {
1461 rb_ibf_load_iseq_complete(iseq);
1464 if (!iseq->body->location.label) {
1465 rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
1467 return iseq;
1470 const rb_iseq_t *
1471 rb_iseqw_to_iseq(VALUE iseqw)
1473 return iseqw_check(iseqw);
1477 * call-seq:
1478 * iseq.eval -> obj
1480 * Evaluates the instruction sequence and returns the result.
1482 * RubyVM::InstructionSequence.compile("1 + 2").eval #=> 3
1484 static VALUE
1485 iseqw_eval(VALUE self)
1487 return rb_iseq_eval(iseqw_check(self));
1491 * Returns a human-readable string representation of this instruction
1492 * sequence, including the #label and #path.
1494 static VALUE
1495 iseqw_inspect(VALUE self)
1497 const rb_iseq_t *iseq = iseqw_check(self);
1498 const struct rb_iseq_constant_body *const body = iseq->body;
1499 VALUE klass = rb_class_name(rb_obj_class(self));
1501 if (!body->location.label) {
1502 return rb_sprintf("#<%"PRIsVALUE": uninitialized>", klass);
1504 else {
1505 return rb_sprintf("<%"PRIsVALUE":%"PRIsVALUE"@%"PRIsVALUE":%d>",
1506 klass,
1507 body->location.label, rb_iseq_path(iseq),
1508 FIX2INT(rb_iseq_first_lineno(iseq)));
1513 * Returns the path of this instruction sequence.
1515 * <code><compiled></code> if the iseq was evaluated from a string.
1517 * For example, using irb:
1519 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1520 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1521 * iseq.path
1522 * #=> "<compiled>"
1524 * Using ::compile_file:
1526 * # /tmp/method.rb
1527 * def hello
1528 * puts "hello, world"
1529 * end
1531 * # in irb
1532 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1533 * > iseq.path #=> /tmp/method.rb
1535 static VALUE
1536 iseqw_path(VALUE self)
1538 return rb_iseq_path(iseqw_check(self));
1542 * Returns the absolute path of this instruction sequence.
1544 * +nil+ if the iseq was evaluated from a string.
1546 * For example, using ::compile_file:
1548 * # /tmp/method.rb
1549 * def hello
1550 * puts "hello, world"
1551 * end
1553 * # in irb
1554 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1555 * > iseq.absolute_path #=> /tmp/method.rb
1557 static VALUE
1558 iseqw_absolute_path(VALUE self)
1560 return rb_iseq_realpath(iseqw_check(self));
1563 /* Returns the label of this instruction sequence.
1565 * <code><main></code> if it's at the top level, <code><compiled></code> if it
1566 * was evaluated from a string.
1568 * For example, using irb:
1570 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1571 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1572 * iseq.label
1573 * #=> "<compiled>"
1575 * Using ::compile_file:
1577 * # /tmp/method.rb
1578 * def hello
1579 * puts "hello, world"
1580 * end
1582 * # in irb
1583 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1584 * > iseq.label #=> <main>
1586 static VALUE
1587 iseqw_label(VALUE self)
1589 return rb_iseq_label(iseqw_check(self));
1592 /* Returns the base label of this instruction sequence.
1594 * For example, using irb:
1596 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1597 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1598 * iseq.base_label
1599 * #=> "<compiled>"
1601 * Using ::compile_file:
1603 * # /tmp/method.rb
1604 * def hello
1605 * puts "hello, world"
1606 * end
1608 * # in irb
1609 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1610 * > iseq.base_label #=> <main>
1612 static VALUE
1613 iseqw_base_label(VALUE self)
1615 return rb_iseq_base_label(iseqw_check(self));
1618 /* Returns the number of the first source line where the instruction sequence
1619 * was loaded from.
1621 * For example, using irb:
1623 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1624 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1625 * iseq.first_lineno
1626 * #=> 1
1628 static VALUE
1629 iseqw_first_lineno(VALUE self)
1631 return rb_iseq_first_lineno(iseqw_check(self));
1634 static VALUE iseq_data_to_ary(const rb_iseq_t *iseq);
1637 * call-seq:
1638 * iseq.to_a -> ary
1640 * Returns an Array with 14 elements representing the instruction sequence
1641 * with the following data:
1643 * [magic]
1644 * A string identifying the data format. <b>Always
1645 * +YARVInstructionSequence/SimpleDataFormat+.</b>
1647 * [major_version]
1648 * The major version of the instruction sequence.
1650 * [minor_version]
1651 * The minor version of the instruction sequence.
1653 * [format_type]
1654 * A number identifying the data format. <b>Always 1</b>.
1656 * [misc]
1657 * A hash containing:
1659 * [+:arg_size+]
1660 * the total number of arguments taken by the method or the block (0 if
1661 * _iseq_ doesn't represent a method or block)
1662 * [+:local_size+]
1663 * the number of local variables + 1
1664 * [+:stack_max+]
1665 * used in calculating the stack depth at which a SystemStackError is
1666 * thrown.
1668 * [#label]
1669 * The name of the context (block, method, class, module, etc.) that this
1670 * instruction sequence belongs to.
1672 * <code><main></code> if it's at the top level, <code><compiled></code> if
1673 * it was evaluated from a string.
1675 * [#path]
1676 * The relative path to the Ruby file where the instruction sequence was
1677 * loaded from.
1679 * <code><compiled></code> if the iseq was evaluated from a string.
1681 * [#absolute_path]
1682 * The absolute path to the Ruby file where the instruction sequence was
1683 * loaded from.
1685 * +nil+ if the iseq was evaluated from a string.
1687 * [#first_lineno]
1688 * The number of the first source line where the instruction sequence was
1689 * loaded from.
1691 * [type]
1692 * The type of the instruction sequence.
1694 * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
1695 * +:ensure+, +:eval+, +:main+, and +plain+.
1697 * [locals]
1698 * An array containing the names of all arguments and local variables as
1699 * symbols.
1701 * [params]
1702 * An Hash object containing parameter information.
1704 * More info about these values can be found in +vm_core.h+.
1706 * [catch_table]
1707 * A list of exceptions and control flow operators (rescue, next, redo,
1708 * break, etc.).
1710 * [bytecode]
1711 * An array of arrays containing the instruction names and operands that
1712 * make up the body of the instruction sequence.
1714 * Note that this format is MRI specific and version dependent.
1717 static VALUE
1718 iseqw_to_a(VALUE self)
1720 const rb_iseq_t *iseq = iseqw_check(self);
1721 return iseq_data_to_ary(iseq);
1724 #if VM_INSN_INFO_TABLE_IMPL == 1 /* binary search */
1725 static const struct iseq_insn_info_entry *
1726 get_insn_info_binary_search(const rb_iseq_t *iseq, size_t pos)
1728 const struct rb_iseq_constant_body *const body = iseq->body;
1729 size_t size = body->insns_info.size;
1730 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1731 const unsigned int *positions = body->insns_info.positions;
1732 const int debug = 0;
1734 if (debug) {
1735 printf("size: %"PRIuSIZE"\n", size);
1736 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1737 (size_t)0, positions[0], insns_info[0].line_no, pos);
1740 if (size == 0) {
1741 return NULL;
1743 else if (size == 1) {
1744 return &insns_info[0];
1746 else {
1747 size_t l = 1, r = size - 1;
1748 while (l <= r) {
1749 size_t m = l + (r - l) / 2;
1750 if (positions[m] == pos) {
1751 return &insns_info[m];
1753 if (positions[m] < pos) {
1754 l = m + 1;
1756 else {
1757 r = m - 1;
1760 if (l >= size) {
1761 return &insns_info[size-1];
1763 if (positions[l] > pos) {
1764 return &insns_info[l-1];
1766 return &insns_info[l];
1770 static const struct iseq_insn_info_entry *
1771 get_insn_info(const rb_iseq_t *iseq, size_t pos)
1773 return get_insn_info_binary_search(iseq, pos);
1775 #endif
1777 #if VM_INSN_INFO_TABLE_IMPL == 2 /* succinct bitvector */
1778 static const struct iseq_insn_info_entry *
1779 get_insn_info_succinct_bitvector(const rb_iseq_t *iseq, size_t pos)
1781 const struct rb_iseq_constant_body *const body = iseq->body;
1782 size_t size = body->insns_info.size;
1783 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1784 const int debug = 0;
1786 if (debug) {
1787 #if VM_CHECK_MODE > 0
1788 const unsigned int *positions = body->insns_info.positions;
1789 printf("size: %"PRIuSIZE"\n", size);
1790 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1791 (size_t)0, positions[0], insns_info[0].line_no, pos);
1792 #else
1793 printf("size: %"PRIuSIZE"\n", size);
1794 printf("insns_info[%"PRIuSIZE"]: line: %d, pos: %"PRIuSIZE"\n",
1795 (size_t)0, insns_info[0].line_no, pos);
1796 #endif
1799 if (size == 0) {
1800 return NULL;
1802 else if (size == 1) {
1803 return &insns_info[0];
1805 else {
1806 int index;
1807 VM_ASSERT(body->insns_info.succ_index_table != NULL);
1808 index = succ_index_lookup(body->insns_info.succ_index_table, (int)pos);
1809 return &insns_info[index-1];
1813 static const struct iseq_insn_info_entry *
1814 get_insn_info(const rb_iseq_t *iseq, size_t pos)
1816 return get_insn_info_succinct_bitvector(iseq, pos);
1818 #endif
1820 #if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
1821 static const struct iseq_insn_info_entry *
1822 get_insn_info_linear_search(const rb_iseq_t *iseq, size_t pos)
1824 const struct rb_iseq_constant_body *const body = iseq->body;
1825 size_t i = 0, size = body->insns_info.size;
1826 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1827 const unsigned int *positions = body->insns_info.positions;
1828 const int debug = 0;
1830 if (debug) {
1831 printf("size: %"PRIuSIZE"\n", size);
1832 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1833 i, positions[i], insns_info[i].line_no, pos);
1836 if (size == 0) {
1837 return NULL;
1839 else if (size == 1) {
1840 return &insns_info[0];
1842 else {
1843 for (i=1; i<size; i++) {
1844 if (debug) printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1845 i, positions[i], insns_info[i].line_no, pos);
1847 if (positions[i] == pos) {
1848 return &insns_info[i];
1850 if (positions[i] > pos) {
1851 return &insns_info[i-1];
1855 return &insns_info[i-1];
1857 #endif
1859 #if VM_INSN_INFO_TABLE_IMPL == 0 /* linear search */
1860 static const struct iseq_insn_info_entry *
1861 get_insn_info(const rb_iseq_t *iseq, size_t pos)
1863 return get_insn_info_linear_search(iseq, pos);
1865 #endif
1867 #if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
1868 static void
1869 validate_get_insn_info(const rb_iseq_t *iseq)
1871 const struct rb_iseq_constant_body *const body = iseq->body;
1872 size_t i;
1873 for (i = 0; i < body->iseq_size; i++) {
1874 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
1875 rb_bug("validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE") != get_insn_info(iseq, %"PRIuSIZE")", i, i);
1879 #endif
1881 unsigned int
1882 rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
1884 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
1886 if (entry) {
1887 return entry->line_no;
1889 else {
1890 return 0;
1894 #ifdef USE_ISEQ_NODE_ID
1896 rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos)
1898 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
1900 if (entry) {
1901 return entry->node_id;
1903 else {
1904 return 0;
1907 #endif
1909 MJIT_FUNC_EXPORTED rb_event_flag_t
1910 rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos)
1912 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
1913 if (entry) {
1914 return entry->events;
1916 else {
1917 return 0;
1921 void
1922 rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t reset)
1924 struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos);
1925 if (entry) {
1926 entry->events &= ~reset;
1927 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
1928 void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos);
1929 rb_iseq_trace_flag_cleared(iseq, pos);
1934 static VALUE
1935 local_var_name(const rb_iseq_t *diseq, VALUE level, VALUE op)
1937 VALUE i;
1938 VALUE name;
1939 ID lid;
1940 int idx;
1942 for (i = 0; i < level; i++) {
1943 diseq = diseq->body->parent_iseq;
1945 idx = diseq->body->local_table_size - (int)op - 1;
1946 lid = diseq->body->local_table[idx];
1947 name = rb_id2str(lid);
1948 if (!name) {
1949 name = rb_str_new_cstr("?");
1951 else if (!rb_str_symname_p(name)) {
1952 name = rb_str_inspect(name);
1954 else {
1955 name = rb_str_dup(name);
1957 rb_str_catf(name, "@%d", idx);
1958 return name;
1961 int rb_insn_unified_local_var_level(VALUE);
1962 VALUE rb_dump_literal(VALUE lit);
1964 VALUE
1965 rb_insn_operand_intern(const rb_iseq_t *iseq,
1966 VALUE insn, int op_no, VALUE op,
1967 int len, size_t pos, const VALUE *pnop, VALUE child)
1969 const char *types = insn_op_types(insn);
1970 char type = types[op_no];
1971 VALUE ret = Qundef;
1973 switch (type) {
1974 case TS_OFFSET: /* LONG */
1975 ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
1976 break;
1978 case TS_NUM: /* ULONG */
1979 if (insn == BIN(defined) && op_no == 0) {
1980 enum defined_type deftype = (enum defined_type)op;
1981 switch (deftype) {
1982 case DEFINED_FUNC:
1983 ret = rb_fstring_lit("func");
1984 break;
1985 case DEFINED_REF:
1986 ret = rb_fstring_lit("ref");
1987 break;
1988 case DEFINED_CONST_FROM:
1989 ret = rb_fstring_lit("constant-from");
1990 break;
1991 default:
1992 ret = rb_iseq_defined_string(deftype);
1993 break;
1995 if (ret) break;
1997 else if (insn == BIN(checktype) && op_no == 0) {
1998 const char *type_str = rb_type_str((enum ruby_value_type)op);
1999 if (type_str) {
2000 ret = rb_str_new_cstr(type_str); break;
2003 ret = rb_sprintf("%"PRIuVALUE, op);
2004 break;
2006 case TS_LINDEX:{
2007 int level;
2008 if (types[op_no+1] == TS_NUM && pnop) {
2009 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2011 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2012 ret = local_var_name(iseq, (VALUE)level, op - VM_ENV_DATA_SIZE);
2014 else {
2015 ret = rb_inspect(INT2FIX(op));
2017 break;
2019 case TS_ID: /* ID (symbol) */
2020 ret = rb_inspect(ID2SYM(op));
2021 break;
2023 case TS_VALUE: /* VALUE */
2024 op = obj_resurrect(op);
2025 if (insn == BIN(defined) && op_no == 1 && FIXNUM_P(op)) {
2026 /* should be DEFINED_REF */
2027 int type = NUM2INT(op);
2028 if (type) {
2029 if (type & 1) {
2030 ret = rb_sprintf(":$%c", (type >> 1));
2032 else {
2033 ret = rb_sprintf(":$%d", (type >> 1));
2035 break;
2038 ret = rb_dump_literal(op);
2039 if (CLASS_OF(op) == rb_cISeq) {
2040 if (child) {
2041 rb_ary_push(child, op);
2044 break;
2046 case TS_ISEQ: /* iseq */
2048 if (op) {
2049 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2050 ret = iseq->body->location.label;
2051 if (child) {
2052 rb_ary_push(child, (VALUE)iseq);
2055 else {
2056 ret = rb_str_new2("nil");
2058 break;
2061 case TS_IC:
2062 case TS_IVC:
2063 case TS_ISE:
2064 ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - iseq->body->is_entries);
2065 break;
2067 case TS_CALLDATA:
2069 struct rb_call_data *cd = (struct rb_call_data *)op;
2070 const struct rb_callinfo *ci = cd->ci;
2071 VALUE ary = rb_ary_new();
2072 ID mid = vm_ci_mid(ci);
2074 if (mid) {
2075 rb_ary_push(ary, rb_sprintf("mid:%"PRIsVALUE, rb_id2str(mid)));
2078 rb_ary_push(ary, rb_sprintf("argc:%d", vm_ci_argc(ci)));
2080 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2081 const struct rb_callinfo_kwarg *kw_args = vm_ci_kwarg(ci);
2082 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2083 rb_ary_push(ary, rb_sprintf("kw:[%"PRIsVALUE"]", rb_ary_join(kw_ary, rb_str_new2(","))));
2086 if (vm_ci_flag(ci)) {
2087 VALUE flags = rb_ary_new();
2088 # define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2089 CALL_FLAG(ARGS_SPLAT);
2090 CALL_FLAG(ARGS_BLOCKARG);
2091 CALL_FLAG(FCALL);
2092 CALL_FLAG(VCALL);
2093 CALL_FLAG(ARGS_SIMPLE);
2094 CALL_FLAG(BLOCKISEQ);
2095 CALL_FLAG(TAILCALL);
2096 CALL_FLAG(SUPER);
2097 CALL_FLAG(ZSUPER);
2098 CALL_FLAG(KWARG);
2099 CALL_FLAG(KW_SPLAT);
2100 CALL_FLAG(KW_SPLAT_MUT);
2101 CALL_FLAG(OPT_SEND); /* maybe not reachable */
2102 rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
2105 ret = rb_sprintf("<calldata!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
2107 break;
2109 case TS_CDHASH:
2110 ret = rb_str_new2("<cdhash>");
2111 break;
2113 case TS_FUNCPTR:
2115 #ifdef HAVE_DLADDR
2116 Dl_info info;
2117 if (dladdr((void *)op, &info) && info.dli_sname) {
2118 ret = rb_str_new_cstr(info.dli_sname);
2119 break;
2121 #endif
2122 ret = rb_str_new2("<funcptr>");
2124 break;
2126 case TS_BUILTIN:
2128 const struct rb_builtin_function *bf = (const struct rb_builtin_function *)op;
2129 ret = rb_sprintf("<builtin!%s/%d>",
2130 bf->name, bf->argc);
2132 break;
2134 default:
2135 rb_bug("unknown operand type: %c", type);
2137 return ret;
2140 static VALUE
2141 right_strip(VALUE str)
2143 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2144 while (end-- > beg && *end == ' ');
2145 rb_str_set_len(str, end - beg + 1);
2146 return str;
2150 * Disassemble a instruction
2151 * Iseq -> Iseq inspect object
2154 rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
2155 const rb_iseq_t *iseq, VALUE child)
2157 VALUE insn = code[pos];
2158 int len = insn_len(insn);
2159 int j;
2160 const char *types = insn_op_types(insn);
2161 VALUE str = rb_str_new(0, 0);
2162 const char *insn_name_buff;
2164 insn_name_buff = insn_name(insn);
2165 if (1) {
2166 extern const int rb_vm_max_insn_name_size;
2167 rb_str_catf(str, "%04"PRIuSIZE" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2169 else {
2170 rb_str_catf(str, "%04"PRIuSIZE" %-28.*s ", pos,
2171 (int)strcspn(insn_name_buff, "_"), insn_name_buff);
2174 for (j = 0; types[j]; j++) {
2175 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2176 len, pos, &code[pos + j + 2],
2177 child);
2178 rb_str_concat(str, opstr);
2180 if (types[j + 1]) {
2181 rb_str_cat2(str, ", ");
2186 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2187 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2188 if (line_no && line_no != prev) {
2189 long slen = RSTRING_LEN(str);
2190 slen = (slen > 70) ? 0 : (70 - slen);
2191 str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
2196 rb_event_flag_t events = rb_iseq_event_flags(iseq, pos);
2197 if (events) {
2198 str = rb_str_catf(str, "[%s%s%s%s%s%s%s%s%s%s%s]",
2199 events & RUBY_EVENT_LINE ? "Li" : "",
2200 events & RUBY_EVENT_CLASS ? "Cl" : "",
2201 events & RUBY_EVENT_END ? "En" : "",
2202 events & RUBY_EVENT_CALL ? "Ca" : "",
2203 events & RUBY_EVENT_RETURN ? "Re" : "",
2204 events & RUBY_EVENT_C_CALL ? "Cc" : "",
2205 events & RUBY_EVENT_C_RETURN ? "Cr" : "",
2206 events & RUBY_EVENT_B_CALL ? "Bc" : "",
2207 events & RUBY_EVENT_B_RETURN ? "Br" : "",
2208 events & RUBY_EVENT_COVERAGE_LINE ? "Cli" : "",
2209 events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : "");
2213 right_strip(str);
2214 if (ret) {
2215 rb_str_cat2(str, "\n");
2216 rb_str_concat(ret, str);
2218 else {
2219 printf("%.*s\n", (int)RSTRING_LEN(str), RSTRING_PTR(str));
2221 return len;
2224 static const char *
2225 catch_type(int type)
2227 switch (type) {
2228 case CATCH_TYPE_RESCUE:
2229 return "rescue";
2230 case CATCH_TYPE_ENSURE:
2231 return "ensure";
2232 case CATCH_TYPE_RETRY:
2233 return "retry";
2234 case CATCH_TYPE_BREAK:
2235 return "break";
2236 case CATCH_TYPE_REDO:
2237 return "redo";
2238 case CATCH_TYPE_NEXT:
2239 return "next";
2240 default:
2241 rb_bug("unknown catch type: %d", type);
2242 return 0;
2246 static VALUE
2247 iseq_inspect(const rb_iseq_t *iseq)
2249 const struct rb_iseq_constant_body *const body = iseq->body;
2250 if (!body->location.label) {
2251 return rb_sprintf("#<ISeq: uninitialized>");
2253 else {
2254 const rb_code_location_t *loc = &body->location.code_location;
2255 return rb_sprintf("#<ISeq:%"PRIsVALUE"@%"PRIsVALUE":%d (%d,%d)-(%d,%d)>",
2256 body->location.label, rb_iseq_path(iseq),
2257 loc->beg_pos.lineno,
2258 loc->beg_pos.lineno,
2259 loc->beg_pos.column,
2260 loc->end_pos.lineno,
2261 loc->end_pos.column);
2265 static const rb_data_type_t tmp_set = {
2266 "tmpset",
2267 {(void (*)(void *))rb_mark_set, (void (*)(void *))st_free_table, 0, 0,},
2268 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2271 static VALUE
2272 rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
2274 const struct rb_iseq_constant_body *const body = iseq->body;
2275 VALUE *code;
2276 VALUE str = rb_str_new(0, 0);
2277 VALUE child = rb_ary_tmp_new(3);
2278 unsigned int size;
2279 unsigned int i;
2280 long l;
2281 size_t n;
2282 enum {header_minlen = 72};
2283 st_table *done_iseq = 0;
2284 VALUE done_iseq_wrapper = Qnil;
2285 const char *indent_str;
2286 long indent_len;
2288 size = body->iseq_size;
2290 indent_len = RSTRING_LEN(indent);
2291 indent_str = RSTRING_PTR(indent);
2293 rb_str_cat(str, indent_str, indent_len);
2294 rb_str_cat2(str, "== disasm: ");
2296 rb_str_append(str, iseq_inspect(iseq));
2297 rb_str_catf(str, " (catch: %s)", body->catch_except_p ? "TRUE" : "FALSE");
2298 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2299 rb_str_modify_expand(str, header_minlen - l);
2300 memset(RSTRING_END(str), '=', header_minlen - l);
2302 rb_str_cat2(str, "\n");
2304 /* show catch table information */
2305 if (body->catch_table) {
2306 rb_str_cat(str, indent_str, indent_len);
2307 rb_str_cat2(str, "== catch table\n");
2309 if (body->catch_table) {
2310 rb_str_cat_cstr(indent, "| ");
2311 indent_str = RSTRING_PTR(indent);
2312 for (i = 0; i < body->catch_table->size; i++) {
2313 const struct iseq_catch_table_entry *entry =
2314 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2315 rb_str_cat(str, indent_str, indent_len);
2316 rb_str_catf(str,
2317 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2318 catch_type((int)entry->type), (int)entry->start,
2319 (int)entry->end, (int)entry->sp, (int)entry->cont);
2320 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2321 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2322 if (!done_iseq) {
2323 done_iseq = st_init_numtable();
2324 done_iseq_wrapper = TypedData_Wrap_Struct(0, &tmp_set, done_iseq);
2326 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2327 indent_str = RSTRING_PTR(indent);
2330 rb_str_resize(indent, indent_len);
2331 indent_str = RSTRING_PTR(indent);
2333 if (body->catch_table) {
2334 rb_str_cat(str, indent_str, indent_len);
2335 rb_str_cat2(str, "|-------------------------------------"
2336 "-----------------------------------\n");
2339 /* show local table information */
2340 if (body->local_table) {
2341 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
2342 rb_str_cat(str, indent_str, indent_len);
2343 rb_str_catf(str,
2344 "local table (size: %d, argc: %d "
2345 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2346 body->local_table_size,
2347 body->param.lead_num,
2348 body->param.opt_num,
2349 body->param.flags.has_rest ? body->param.rest_start : -1,
2350 body->param.post_num,
2351 body->param.flags.has_block ? body->param.block_start : -1,
2352 body->param.flags.has_kw ? keyword->num : -1,
2353 body->param.flags.has_kw ? keyword->required_num : -1,
2354 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2356 for (i = body->local_table_size; i > 0;) {
2357 int li = body->local_table_size - --i - 1;
2358 long width;
2359 VALUE name = local_var_name(iseq, 0, i);
2360 char argi[0x100];
2361 char opti[0x100];
2363 opti[0] = '\0';
2364 if (body->param.flags.has_opt) {
2365 int argc = body->param.lead_num;
2366 int opts = body->param.opt_num;
2367 if (li >= argc && li < argc + opts) {
2368 snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
2369 body->param.opt_table[li - argc]);
2373 snprintf(argi, sizeof(argi), "%s%s%s%s%s%s", /* arg, opts, rest, post, kwrest, block */
2374 body->param.lead_num > li ? "Arg" : "",
2375 opti,
2376 (body->param.flags.has_rest && body->param.rest_start == li) ? "Rest" : "",
2377 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ? "Post" : "",
2378 (body->param.flags.has_kwrest && keyword->rest_start == li) ? "Kwrest" : "",
2379 (body->param.flags.has_block && body->param.block_start == li) ? "Block" : "");
2381 rb_str_cat(str, indent_str, indent_len);
2382 rb_str_catf(str, "[%2d] ", i + 1);
2383 width = RSTRING_LEN(str) + 11;
2384 rb_str_append(str, name);
2385 if (*argi) rb_str_catf(str, "<%s>", argi);
2386 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
2388 rb_str_cat_cstr(right_strip(str), "\n");
2391 /* show each line */
2392 code = rb_iseq_original_iseq(iseq);
2393 for (n = 0; n < size;) {
2394 rb_str_cat(str, indent_str, indent_len);
2395 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2398 for (l = 0; l < RARRAY_LEN(child); l++) {
2399 VALUE isv = rb_ary_entry(child, l);
2400 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv)) continue;
2401 rb_str_cat_cstr(str, "\n");
2402 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2403 indent_str = RSTRING_PTR(indent);
2405 RB_GC_GUARD(done_iseq_wrapper);
2407 return str;
2410 VALUE
2411 rb_iseq_disasm(const rb_iseq_t *iseq)
2413 VALUE str = rb_iseq_disasm_recursive(iseq, rb_str_new(0, 0));
2414 rb_str_resize(str, RSTRING_LEN(str));
2415 return str;
2419 * call-seq:
2420 * iseq.disasm -> str
2421 * iseq.disassemble -> str
2423 * Returns the instruction sequence as a +String+ in human readable form.
2425 * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
2427 * Produces:
2429 * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
2430 * 0000 trace 1 ( 1)
2431 * 0002 putobject 1
2432 * 0004 putobject 2
2433 * 0006 opt_plus <ic:1>
2434 * 0008 leave
2436 static VALUE
2437 iseqw_disasm(VALUE self)
2439 return rb_iseq_disasm(iseqw_check(self));
2442 static int
2443 iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *child_iseq, void *data), void *data)
2445 unsigned int i;
2446 VALUE *code = rb_iseq_original_iseq(iseq);
2447 const struct rb_iseq_constant_body *const body = iseq->body;
2448 const rb_iseq_t *child;
2449 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2451 if (body->catch_table) {
2452 for (i = 0; i < body->catch_table->size; i++) {
2453 const struct iseq_catch_table_entry *entry =
2454 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2455 child = entry->iseq;
2456 if (child) {
2457 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2458 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2459 (*iter_func)(child, data);
2465 for (i=0; i<body->iseq_size;) {
2466 VALUE insn = code[i];
2467 int len = insn_len(insn);
2468 const char *types = insn_op_types(insn);
2469 int j;
2471 for (j=0; types[j]; j++) {
2472 switch (types[j]) {
2473 case TS_ISEQ:
2474 child = (const rb_iseq_t *)code[i+j+1];
2475 if (child) {
2476 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2477 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2478 (*iter_func)(child, data);
2481 break;
2482 default:
2483 break;
2486 i += len;
2489 return (int)RHASH_SIZE(all_children);
2492 static void
2493 yield_each_children(const rb_iseq_t *child_iseq, void *data)
2495 rb_yield(iseqw_new(child_iseq));
2499 * call-seq:
2500 * iseq.each_child{|child_iseq| ...} -> iseq
2502 * Iterate all direct child instruction sequences.
2503 * Iteration order is implementation/version defined
2504 * so that people should not rely on the order.
2506 static VALUE
2507 iseqw_each_child(VALUE self)
2509 const rb_iseq_t *iseq = iseqw_check(self);
2510 iseq_iterate_children(iseq, yield_each_children, NULL);
2511 return self;
2514 static void
2515 push_event_info(const rb_iseq_t *iseq, rb_event_flag_t events, int line, VALUE ary)
2517 #define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2518 C(RUBY_EVENT_CLASS, "class", rb_iseq_first_lineno(iseq));
2519 C(RUBY_EVENT_CALL, "call", rb_iseq_first_lineno(iseq));
2520 C(RUBY_EVENT_B_CALL, "b_call", rb_iseq_first_lineno(iseq));
2521 C(RUBY_EVENT_LINE, "line", INT2FIX(line));
2522 C(RUBY_EVENT_END, "end", INT2FIX(line));
2523 C(RUBY_EVENT_RETURN, "return", INT2FIX(line));
2524 C(RUBY_EVENT_B_RETURN, "b_return", INT2FIX(line));
2525 #undef C
2529 * call-seq:
2530 * iseq.trace_points -> ary
2532 * Return trace points in the instruction sequence.
2533 * Return an array of [line, event_symbol] pair.
2535 static VALUE
2536 iseqw_trace_points(VALUE self)
2538 const rb_iseq_t *iseq = iseqw_check(self);
2539 const struct rb_iseq_constant_body *const body = iseq->body;
2540 unsigned int i;
2541 VALUE ary = rb_ary_new();
2543 for (i=0; i<body->insns_info.size; i++) {
2544 const struct iseq_insn_info_entry *entry = &body->insns_info.body[i];
2545 if (entry->events) {
2546 push_event_info(iseq, entry->events, entry->line_no, ary);
2549 return ary;
2553 * Returns the instruction sequence containing the given proc or method.
2555 * For example, using irb:
2557 * # a proc
2558 * > p = proc { num = 1 + 2 }
2559 * > RubyVM::InstructionSequence.of(p)
2560 * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
2562 * # for a method
2563 * > def foo(bar); puts bar; end
2564 * > RubyVM::InstructionSequence.of(method(:foo))
2565 * > #=> <RubyVM::InstructionSequence:foo@(irb)>
2567 * Using ::compile_file:
2569 * # /tmp/iseq_of.rb
2570 * def hello
2571 * puts "hello, world"
2572 * end
2574 * $a_global_proc = proc { str = 'a' + 'b' }
2576 * # in irb
2577 * > require '/tmp/iseq_of.rb'
2579 * # first the method hello
2580 * > RubyVM::InstructionSequence.of(method(:hello))
2581 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
2583 * # then the global proc
2584 * > RubyVM::InstructionSequence.of($a_global_proc)
2585 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
2587 static VALUE
2588 iseqw_s_of(VALUE klass, VALUE body)
2590 const rb_iseq_t *iseq = NULL;
2592 if (rb_obj_is_proc(body)) {
2593 iseq = vm_proc_iseq(body);
2595 if (!rb_obj_is_iseq((VALUE)iseq)) {
2596 iseq = NULL;
2599 else if (rb_obj_is_method(body)) {
2600 iseq = rb_method_iseq(body);
2602 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2603 return body;
2606 return iseq ? iseqw_new(iseq) : Qnil;
2610 * call-seq:
2611 * InstructionSequence.disasm(body) -> str
2612 * InstructionSequence.disassemble(body) -> str
2614 * Takes +body+, a Method or Proc object, and returns a String with the
2615 * human readable instructions for +body+.
2617 * For a Method object:
2619 * # /tmp/method.rb
2620 * def hello
2621 * puts "hello, world"
2622 * end
2624 * puts RubyVM::InstructionSequence.disasm(method(:hello))
2626 * Produces:
2628 * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
2629 * 0000 trace 8 ( 1)
2630 * 0002 trace 1 ( 2)
2631 * 0004 putself
2632 * 0005 putstring "hello, world"
2633 * 0007 send :puts, 1, nil, 8, <ic:0>
2634 * 0013 trace 16 ( 3)
2635 * 0015 leave ( 2)
2637 * For a Proc:
2639 * # /tmp/proc.rb
2640 * p = proc { num = 1 + 2 }
2641 * puts RubyVM::InstructionSequence.disasm(p)
2643 * Produces:
2645 * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
2646 * == catch table
2647 * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
2648 * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
2649 * |------------------------------------------------------------------------
2650 * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
2651 * [ 2] num
2652 * 0000 trace 1 ( 1)
2653 * 0002 putobject 1
2654 * 0004 putobject 2
2655 * 0006 opt_plus <ic:1>
2656 * 0008 dup
2657 * 0009 setlocal num, 0
2658 * 0012 leave
2661 static VALUE
2662 iseqw_s_disasm(VALUE klass, VALUE body)
2664 VALUE iseqw = iseqw_s_of(klass, body);
2665 return NIL_P(iseqw) ? Qnil : rb_iseq_disasm(iseqw_check(iseqw));
2668 const char *
2669 ruby_node_name(int node)
2671 switch (node) {
2672 #include "node_name.inc"
2673 default:
2674 rb_bug("unknown node: %d", node);
2675 return 0;
2679 static VALUE
2680 register_label(struct st_table *table, unsigned long idx)
2682 VALUE sym = rb_str_intern(rb_sprintf("label_%lu", idx));
2683 st_insert(table, idx, sym);
2684 return sym;
2687 static VALUE
2688 exception_type2symbol(VALUE type)
2690 ID id;
2691 switch (type) {
2692 case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
2693 case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
2694 case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
2695 case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
2696 case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
2697 case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
2698 default:
2699 rb_bug("unknown exception type: %d", (int)type);
2701 return ID2SYM(id);
2704 static int
2705 cdhash_each(VALUE key, VALUE value, VALUE ary)
2707 rb_ary_push(ary, obj_resurrect(key));
2708 rb_ary_push(ary, value);
2709 return ST_CONTINUE;
2712 static const rb_data_type_t label_wrapper = {
2713 "label_wrapper",
2714 {(void (*)(void *))rb_mark_tbl, (void (*)(void *))st_free_table, 0, 0,},
2715 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2718 #define DECL_ID(name) \
2719 static ID id_##name
2721 #define INIT_ID(name) \
2722 id_##name = rb_intern(#name)
2724 static VALUE
2725 iseq_type_id(enum iseq_type type)
2727 DECL_ID(top);
2728 DECL_ID(method);
2729 DECL_ID(block);
2730 DECL_ID(class);
2731 DECL_ID(rescue);
2732 DECL_ID(ensure);
2733 DECL_ID(eval);
2734 DECL_ID(main);
2735 DECL_ID(plain);
2737 if (id_top == 0) {
2738 INIT_ID(top);
2739 INIT_ID(method);
2740 INIT_ID(block);
2741 INIT_ID(class);
2742 INIT_ID(rescue);
2743 INIT_ID(ensure);
2744 INIT_ID(eval);
2745 INIT_ID(main);
2746 INIT_ID(plain);
2749 switch (type) {
2750 case ISEQ_TYPE_TOP: return id_top;
2751 case ISEQ_TYPE_METHOD: return id_method;
2752 case ISEQ_TYPE_BLOCK: return id_block;
2753 case ISEQ_TYPE_CLASS: return id_class;
2754 case ISEQ_TYPE_RESCUE: return id_rescue;
2755 case ISEQ_TYPE_ENSURE: return id_ensure;
2756 case ISEQ_TYPE_EVAL: return id_eval;
2757 case ISEQ_TYPE_MAIN: return id_main;
2758 case ISEQ_TYPE_PLAIN: return id_plain;
2761 rb_bug("unsupported iseq type: %d", (int)type);
2764 static VALUE
2765 iseq_data_to_ary(const rb_iseq_t *iseq)
2767 unsigned int i;
2768 long l;
2769 const struct rb_iseq_constant_body *const iseq_body = iseq->body;
2770 const struct iseq_insn_info_entry *prev_insn_info;
2771 unsigned int pos;
2772 int last_line = 0;
2773 VALUE *seq, *iseq_original;
2775 VALUE val = rb_ary_new();
2776 ID type; /* Symbol */
2777 VALUE locals = rb_ary_new();
2778 VALUE params = rb_hash_new();
2779 VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
2780 VALUE nbody;
2781 VALUE exception = rb_ary_new(); /* [[....]] */
2782 VALUE misc = rb_hash_new();
2784 static ID insn_syms[VM_INSTRUCTION_SIZE/2]; /* w/o-trace only */
2785 struct st_table *labels_table = st_init_numtable();
2786 VALUE labels_wrapper = TypedData_Wrap_Struct(0, &label_wrapper, labels_table);
2788 if (insn_syms[0] == 0) {
2789 int i;
2790 for (i=0; i<numberof(insn_syms); i++) {
2791 insn_syms[i] = rb_intern(insn_name(i));
2795 /* type */
2796 type = iseq_type_id(iseq_body->type);
2798 /* locals */
2799 for (i=0; i<iseq_body->local_table_size; i++) {
2800 ID lid = iseq_body->local_table[i];
2801 if (lid) {
2802 if (rb_id2str(lid)) {
2803 rb_ary_push(locals, ID2SYM(lid));
2805 else { /* hidden variable from id_internal() */
2806 rb_ary_push(locals, ULONG2NUM(iseq_body->local_table_size-i+1));
2809 else {
2810 rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
2814 /* params */
2816 const struct rb_iseq_param_keyword *const keyword = iseq_body->param.keyword;
2817 int j;
2819 if (iseq_body->param.flags.has_opt) {
2820 int len = iseq_body->param.opt_num + 1;
2821 VALUE arg_opt_labels = rb_ary_new2(len);
2823 for (j = 0; j < len; j++) {
2824 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
2825 rb_ary_push(arg_opt_labels, l);
2827 rb_hash_aset(params, ID2SYM(rb_intern("opt")), arg_opt_labels);
2830 /* commit */
2831 if (iseq_body->param.flags.has_lead) rb_hash_aset(params, ID2SYM(rb_intern("lead_num")), INT2FIX(iseq_body->param.lead_num));
2832 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_num")), INT2FIX(iseq_body->param.post_num));
2833 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_start")), INT2FIX(iseq_body->param.post_start));
2834 if (iseq_body->param.flags.has_rest) rb_hash_aset(params, ID2SYM(rb_intern("rest_start")), INT2FIX(iseq_body->param.rest_start));
2835 if (iseq_body->param.flags.has_block) rb_hash_aset(params, ID2SYM(rb_intern("block_start")), INT2FIX(iseq_body->param.block_start));
2836 if (iseq_body->param.flags.has_kw) {
2837 VALUE keywords = rb_ary_new();
2838 int i, j;
2839 for (i=0; i<keyword->required_num; i++) {
2840 rb_ary_push(keywords, ID2SYM(keyword->table[i]));
2842 for (j=0; i<keyword->num; i++, j++) {
2843 VALUE key = rb_ary_new_from_args(1, ID2SYM(keyword->table[i]));
2844 if (keyword->default_values[j] != Qundef) {
2845 rb_ary_push(key, keyword->default_values[j]);
2847 rb_ary_push(keywords, key);
2850 rb_hash_aset(params, ID2SYM(rb_intern("kwbits")),
2851 INT2FIX(keyword->bits_start));
2852 rb_hash_aset(params, ID2SYM(rb_intern("keyword")), keywords);
2854 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(keyword->rest_start));
2855 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
2858 /* body */
2859 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
2861 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
2862 VALUE insn = *seq++;
2863 int j, len = insn_len(insn);
2864 VALUE *nseq = seq + len - 1;
2865 VALUE ary = rb_ary_new2(len);
2867 rb_ary_push(ary, ID2SYM(insn_syms[insn%numberof(insn_syms)]));
2868 for (j=0; j<len-1; j++, seq++) {
2869 switch (insn_op_type(insn, j)) {
2870 case TS_OFFSET: {
2871 unsigned long idx = nseq - iseq_original + *seq;
2872 rb_ary_push(ary, register_label(labels_table, idx));
2873 break;
2875 case TS_LINDEX:
2876 case TS_NUM:
2877 rb_ary_push(ary, INT2FIX(*seq));
2878 break;
2879 case TS_VALUE:
2880 rb_ary_push(ary, obj_resurrect(*seq));
2881 break;
2882 case TS_ISEQ:
2884 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
2885 if (iseq) {
2886 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
2887 rb_ary_push(ary, val);
2889 else {
2890 rb_ary_push(ary, Qnil);
2893 break;
2894 case TS_IC:
2895 case TS_IVC:
2896 case TS_ISE:
2898 union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
2899 rb_ary_push(ary, INT2FIX(is - iseq_body->is_entries));
2901 break;
2902 case TS_CALLDATA:
2904 struct rb_call_data *cd = (struct rb_call_data *)*seq;
2905 const struct rb_callinfo *ci = cd->ci;
2906 VALUE e = rb_hash_new();
2907 int argc = vm_ci_argc(ci);
2909 ID mid = vm_ci_mid(ci);
2910 rb_hash_aset(e, ID2SYM(rb_intern("mid")), mid ? ID2SYM(mid) : Qnil);
2911 rb_hash_aset(e, ID2SYM(rb_intern("flag")), UINT2NUM(vm_ci_flag(ci)));
2913 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2914 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
2915 int i;
2916 VALUE kw = rb_ary_new2((long)kwarg->keyword_len);
2918 argc -= kwarg->keyword_len;
2919 for (i = 0; i < kwarg->keyword_len; i++) {
2920 rb_ary_push(kw, kwarg->keywords[i]);
2922 rb_hash_aset(e, ID2SYM(rb_intern("kw_arg")), kw);
2925 rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")),
2926 INT2FIX(argc));
2927 rb_ary_push(ary, e);
2929 break;
2930 case TS_ID:
2931 rb_ary_push(ary, ID2SYM(*seq));
2932 break;
2933 case TS_CDHASH:
2935 VALUE hash = *seq;
2936 VALUE val = rb_ary_new();
2937 int i;
2939 rb_hash_foreach(hash, cdhash_each, val);
2941 for (i=0; i<RARRAY_LEN(val); i+=2) {
2942 VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
2943 unsigned long idx = nseq - iseq_original + pos;
2945 rb_ary_store(val, i+1,
2946 register_label(labels_table, idx));
2948 rb_ary_push(ary, val);
2950 break;
2951 case TS_FUNCPTR:
2953 #if SIZEOF_VALUE <= SIZEOF_LONG
2954 VALUE val = LONG2NUM((SIGNED_VALUE)*seq);
2955 #else
2956 VALUE val = LL2NUM((SIGNED_VALUE)*seq);
2957 #endif
2958 rb_ary_push(ary, val);
2960 break;
2961 case TS_BUILTIN:
2963 VALUE val = rb_hash_new();
2964 #if SIZEOF_VALUE <= SIZEOF_LONG
2965 VALUE func_ptr = LONG2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
2966 #else
2967 VALUE func_ptr = LL2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
2968 #endif
2969 rb_hash_aset(val, ID2SYM(rb_intern("func_ptr")), func_ptr);
2970 rb_hash_aset(val, ID2SYM(rb_intern("argc")), INT2NUM(((RB_BUILTIN)*seq)->argc));
2971 rb_hash_aset(val, ID2SYM(rb_intern("index")), INT2NUM(((RB_BUILTIN)*seq)->index));
2972 rb_hash_aset(val, ID2SYM(rb_intern("name")), rb_str_new_cstr(((RB_BUILTIN)*seq)->name));
2973 rb_ary_push(ary, val);
2975 break;
2976 default:
2977 rb_bug("unknown operand: %c", insn_op_type(insn, j));
2980 rb_ary_push(body, ary);
2983 nbody = body;
2985 /* exception */
2986 if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
2987 VALUE ary = rb_ary_new();
2988 const struct iseq_catch_table_entry *entry =
2989 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
2990 rb_ary_push(ary, exception_type2symbol(entry->type));
2991 if (entry->iseq) {
2992 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
2994 else {
2995 rb_ary_push(ary, Qnil);
2997 rb_ary_push(ary, register_label(labels_table, entry->start));
2998 rb_ary_push(ary, register_label(labels_table, entry->end));
2999 rb_ary_push(ary, register_label(labels_table, entry->cont));
3000 rb_ary_push(ary, UINT2NUM(entry->sp));
3001 rb_ary_push(exception, ary);
3004 /* make body with labels and insert line number */
3005 body = rb_ary_new();
3006 prev_insn_info = NULL;
3007 #ifdef USE_ISEQ_NODE_ID
3008 VALUE node_ids = rb_ary_new();
3009 #endif
3011 for (l=0, pos=0; l<RARRAY_LEN(nbody); l++) {
3012 const struct iseq_insn_info_entry *info;
3013 VALUE ary = RARRAY_AREF(nbody, l);
3014 st_data_t label;
3016 if (st_lookup(labels_table, pos, &label)) {
3017 rb_ary_push(body, (VALUE)label);
3020 info = get_insn_info(iseq, pos);
3021 #ifdef USE_ISEQ_NODE_ID
3022 rb_ary_push(node_ids, INT2FIX(info->node_id));
3023 #endif
3025 if (prev_insn_info != info) {
3026 int line = info->line_no;
3027 rb_event_flag_t events = info->events;
3029 if (line > 0 && last_line != line) {
3030 rb_ary_push(body, INT2FIX(line));
3031 last_line = line;
3033 #define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3034 CHECK_EVENT(RUBY_EVENT_LINE);
3035 CHECK_EVENT(RUBY_EVENT_CLASS);
3036 CHECK_EVENT(RUBY_EVENT_END);
3037 CHECK_EVENT(RUBY_EVENT_CALL);
3038 CHECK_EVENT(RUBY_EVENT_RETURN);
3039 CHECK_EVENT(RUBY_EVENT_B_CALL);
3040 CHECK_EVENT(RUBY_EVENT_B_RETURN);
3041 #undef CHECK_EVENT
3042 prev_insn_info = info;
3045 rb_ary_push(body, ary);
3046 pos += RARRAY_LENINT(ary); /* reject too huge data */
3048 RB_GC_GUARD(nbody);
3049 RB_GC_GUARD(labels_wrapper);
3051 rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq_body->param.size));
3052 rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq_body->local_table_size));
3053 rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq_body->stack_max));
3054 rb_hash_aset(misc, ID2SYM(rb_intern("node_id")), INT2FIX(iseq_body->location.node_id));
3055 rb_hash_aset(misc, ID2SYM(rb_intern("code_location")),
3056 rb_ary_new_from_args(4,
3057 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3058 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3059 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3060 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3061 #ifdef USE_ISEQ_NODE_ID
3062 rb_hash_aset(misc, ID2SYM(rb_intern("node_ids")), node_ids);
3063 #endif
3066 * [:magic, :major_version, :minor_version, :format_type, :misc,
3067 * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
3068 * :catch_table, :bytecode]
3070 rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
3071 rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
3072 rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
3073 rb_ary_push(val, INT2FIX(1));
3074 rb_ary_push(val, misc);
3075 rb_ary_push(val, iseq_body->location.label);
3076 rb_ary_push(val, rb_iseq_path(iseq));
3077 rb_ary_push(val, rb_iseq_realpath(iseq));
3078 rb_ary_push(val, iseq_body->location.first_lineno);
3079 rb_ary_push(val, ID2SYM(type));
3080 rb_ary_push(val, locals);
3081 rb_ary_push(val, params);
3082 rb_ary_push(val, exception);
3083 rb_ary_push(val, body);
3084 return val;
3087 VALUE
3088 rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
3090 int i, r;
3091 const struct rb_iseq_constant_body *const body = iseq->body;
3092 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
3093 VALUE a, args = rb_ary_new2(body->param.size);
3094 ID req, opt, rest, block, key, keyrest;
3095 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3096 #define PARAM_ID(i) body->local_table[(i)]
3097 #define PARAM(i, type) ( \
3098 PARAM_TYPE(type), \
3099 rb_id2str(PARAM_ID(i)) ? \
3100 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3103 CONST_ID(req, "req");
3104 CONST_ID(opt, "opt");
3105 if (is_proc) {
3106 for (i = 0; i < body->param.lead_num; i++) {
3107 PARAM_TYPE(opt);
3108 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3109 rb_ary_push(args, a);
3112 else {
3113 for (i = 0; i < body->param.lead_num; i++) {
3114 rb_ary_push(args, PARAM(i, req));
3117 r = body->param.lead_num + body->param.opt_num;
3118 for (; i < r; i++) {
3119 PARAM_TYPE(opt);
3120 if (rb_id2str(PARAM_ID(i))) {
3121 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3123 rb_ary_push(args, a);
3125 if (body->param.flags.has_rest) {
3126 CONST_ID(rest, "rest");
3127 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3129 r = body->param.post_start + body->param.post_num;
3130 if (is_proc) {
3131 for (i = body->param.post_start; i < r; i++) {
3132 PARAM_TYPE(opt);
3133 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3134 rb_ary_push(args, a);
3137 else {
3138 for (i = body->param.post_start; i < r; i++) {
3139 rb_ary_push(args, PARAM(i, req));
3142 if (body->param.flags.accepts_no_kwarg) {
3143 ID nokey;
3144 CONST_ID(nokey, "nokey");
3145 PARAM_TYPE(nokey);
3146 rb_ary_push(args, a);
3148 if (body->param.flags.has_kw) {
3149 i = 0;
3150 if (keyword->required_num > 0) {
3151 ID keyreq;
3152 CONST_ID(keyreq, "keyreq");
3153 for (; i < keyword->required_num; i++) {
3154 PARAM_TYPE(keyreq);
3155 if (rb_id2str(keyword->table[i])) {
3156 rb_ary_push(a, ID2SYM(keyword->table[i]));
3158 rb_ary_push(args, a);
3161 CONST_ID(key, "key");
3162 for (; i < keyword->num; i++) {
3163 PARAM_TYPE(key);
3164 if (rb_id2str(keyword->table[i])) {
3165 rb_ary_push(a, ID2SYM(keyword->table[i]));
3167 rb_ary_push(args, a);
3170 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3171 ID param;
3172 CONST_ID(keyrest, "keyrest");
3173 PARAM_TYPE(keyrest);
3174 if (body->param.flags.has_kwrest &&
3175 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3176 rb_ary_push(a, ID2SYM(param));
3178 else if (body->param.flags.ruby2_keywords) {
3179 rb_ary_push(a, ID2SYM(idPow));
3181 rb_ary_push(args, a);
3183 if (body->param.flags.has_block) {
3184 CONST_ID(block, "block");
3185 rb_ary_push(args, PARAM(body->param.block_start, block));
3187 return args;
3190 VALUE
3191 rb_iseq_defined_string(enum defined_type type)
3193 static const char expr_names[][18] = {
3194 "nil",
3195 "instance-variable",
3196 "local-variable",
3197 "global-variable",
3198 "class variable",
3199 "constant",
3200 "method",
3201 "yield",
3202 "super",
3203 "self",
3204 "true",
3205 "false",
3206 "assignment",
3207 "expression",
3209 const char *estr;
3211 if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) rb_bug("unknown defined type %d", type);
3212 estr = expr_names[type - 1];
3213 return rb_fstring_cstr(estr);
3216 /* A map from encoded_insn to insn_data: decoded insn number, its len,
3217 * non-trace version of encoded insn, and trace version. */
3219 static st_table *encoded_insn_data;
3220 typedef struct insn_data_struct {
3221 int insn;
3222 int insn_len;
3223 void *notrace_encoded_insn;
3224 void *trace_encoded_insn;
3225 } insn_data_t;
3226 static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3228 void
3229 rb_vm_encoded_insn_data_table_init(void)
3231 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3232 const void * const *table = rb_vm_get_insns_address_table();
3233 #define INSN_CODE(insn) ((VALUE)table[insn])
3234 #else
3235 #define INSN_CODE(insn) (insn)
3236 #endif
3237 st_data_t insn;
3238 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3240 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3241 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3242 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3244 insn_data[insn].insn = (int)insn;
3245 insn_data[insn].insn_len = insn_len(insn);
3247 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3248 insn_data[insn].notrace_encoded_insn = (void *) key1;
3249 insn_data[insn].trace_encoded_insn = (void *) key2;
3251 else {
3252 insn_data[insn].notrace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3253 insn_data[insn].trace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3256 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3257 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3262 rb_vm_insn_addr2insn(const void *addr)
3264 st_data_t key = (st_data_t)addr;
3265 st_data_t val;
3267 if (st_lookup(encoded_insn_data, key, &val)) {
3268 insn_data_t *e = (insn_data_t *)val;
3269 return (int)e->insn;
3272 rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3275 // Unlike rb_vm_insn_addr2insn, this function can return trace opcode variants.
3277 rb_vm_insn_addr2opcode(const void *addr)
3279 st_data_t key = (st_data_t)addr;
3280 st_data_t val;
3282 if (st_lookup(encoded_insn_data, key, &val)) {
3283 insn_data_t *e = (insn_data_t *)val;
3284 int opcode = e->insn;
3285 if (addr == e->trace_encoded_insn) {
3286 opcode += VM_INSTRUCTION_SIZE/2;
3288 return opcode;
3291 rb_bug("rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3294 // Decode `iseq->body->iseq_encoded[i]` to an insn.
3296 rb_vm_insn_decode(const VALUE encoded)
3298 #if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3299 int insn = rb_vm_insn_addr2insn((void *)encoded);
3300 #else
3301 int insn = (int)encoded;
3302 #endif
3303 return insn;
3306 static inline int
3307 encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace)
3309 st_data_t key = (st_data_t)*iseq_encoded_insn;
3310 st_data_t val;
3312 if (st_lookup(encoded_insn_data, key, &val)) {
3313 insn_data_t *e = (insn_data_t *)val;
3314 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3315 turnon = 1;
3317 *iseq_encoded_insn = (VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3318 return e->insn_len;
3321 rb_bug("trace_instrument: invalid insn address: %p", (void *)*iseq_encoded_insn);
3324 void
3325 rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos)
3327 const struct rb_iseq_constant_body *const body = iseq->body;
3328 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3329 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0, false);
3332 // We need to fire call events on instructions with b_call events if the block
3333 // is running as a method. So, if we are listening for call events, then
3334 // instructions that have b_call events need to become trace variants.
3335 // Use this function when making decisions about recompiling to trace variants.
3336 static inline rb_event_flag_t
3337 add_bmethod_events(rb_event_flag_t events)
3339 if (events & RUBY_EVENT_CALL) {
3340 events |= RUBY_EVENT_B_CALL;
3342 if (events & RUBY_EVENT_RETURN) {
3343 events |= RUBY_EVENT_B_RETURN;
3345 return events;
3348 // Note, to support call/return events for bmethods, turnon_event can have more events than tpval.
3349 static int
3350 iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
3352 unsigned int pc;
3353 int n = 0;
3354 const struct rb_iseq_constant_body *const body = iseq->body;
3355 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3357 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3359 for (pc=0; pc<body->iseq_size;) {
3360 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc);
3361 rb_event_flag_t pc_events = entry->events;
3362 rb_event_flag_t target_events = turnon_events;
3363 unsigned int line = (int)entry->line_no;
3365 if (target_line == 0 || target_line == line) {
3366 /* ok */
3368 else {
3369 target_events &= ~RUBY_EVENT_LINE;
3372 if (pc_events & target_events) {
3373 n++;
3375 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events), true);
3378 if (n > 0) {
3379 if (iseq->aux.exec.local_hooks == NULL) {
3380 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = RB_ZALLOC(rb_hook_list_t);
3381 iseq->aux.exec.local_hooks->is_local = true;
3383 rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3386 return n;
3389 struct trace_set_local_events_struct {
3390 rb_event_flag_t turnon_events;
3391 VALUE tpval;
3392 unsigned int target_line;
3393 int n;
3396 static void
3397 iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3399 struct trace_set_local_events_struct *data = (struct trace_set_local_events_struct *)p;
3400 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3401 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3405 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)
3407 struct trace_set_local_events_struct data;
3408 if (target_bmethod) {
3409 turnon_events = add_bmethod_events(turnon_events);
3411 data.turnon_events = turnon_events;
3412 data.tpval = tpval;
3413 data.target_line = target_line;
3414 data.n = 0;
3416 iseq_add_local_tracepoint_i(iseq, (void *)&data);
3417 if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */
3418 return data.n;
3421 static int
3422 iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval)
3424 int n = 0;
3426 if (iseq->aux.exec.local_hooks) {
3427 unsigned int pc;
3428 const struct rb_iseq_constant_body *const body = iseq->body;
3429 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3430 rb_event_flag_t local_events = 0;
3432 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3433 local_events = iseq->aux.exec.local_hooks->events;
3435 if (local_events == 0) {
3436 rb_hook_list_free(iseq->aux.exec.local_hooks);
3437 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3440 local_events = add_bmethod_events(local_events);
3441 for (pc = 0; pc<body->iseq_size;) {
3442 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3443 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false);
3446 return n;
3449 struct trace_clear_local_events_struct {
3450 VALUE tpval;
3451 int n;
3454 static void
3455 iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3457 struct trace_clear_local_events_struct *data = (struct trace_clear_local_events_struct *)p;
3458 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3459 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3463 rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval)
3465 struct trace_clear_local_events_struct data;
3466 data.tpval = tpval;
3467 data.n = 0;
3469 iseq_remove_local_tracepoint_i(iseq, (void *)&data);
3470 return data.n;
3473 void
3474 rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
3476 if (iseq->aux.exec.global_trace_events == turnon_events) {
3477 return;
3480 if (!ISEQ_EXECUTABLE_P(iseq)) {
3481 /* this is building ISeq */
3482 return;
3484 else {
3485 unsigned int pc;
3486 const struct rb_iseq_constant_body *const body = iseq->body;
3487 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3488 rb_event_flag_t enabled_events;
3489 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3490 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3491 enabled_events = add_bmethod_events(turnon_events | local_events);
3493 for (pc=0; pc<body->iseq_size;) {
3494 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3495 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events, true);
3500 bool rb_vm_call_ivar_attrset_p(const vm_call_handler ch);
3501 void rb_vm_cc_general(const struct rb_callcache *cc);
3503 static int
3504 clear_attr_ccs_i(void *vstart, void *vend, size_t stride, void *data)
3506 VALUE v = (VALUE)vstart;
3507 for (; v != (VALUE)vend; v += stride) {
3508 void *ptr = asan_poisoned_object_p(v);
3509 asan_unpoison_object(v, false);
3511 if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((const struct rb_callcache *)v)->call_)) {
3512 rb_vm_cc_general((struct rb_callcache *)v);
3515 asan_poison_object_if(ptr, v);
3517 return 0;
3520 void
3521 rb_clear_attr_ccs(void)
3523 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3526 static int
3527 trace_set_i(void *vstart, void *vend, size_t stride, void *data)
3529 rb_event_flag_t turnon_events = *(rb_event_flag_t *)data;
3531 VALUE v = (VALUE)vstart;
3532 for (; v != (VALUE)vend; v += stride) {
3533 void *ptr = asan_poisoned_object_p(v);
3534 asan_unpoison_object(v, false);
3536 if (rb_obj_is_iseq(v)) {
3537 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
3539 else if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((const struct rb_callcache *)v)->call_)) {
3540 rb_vm_cc_general((struct rb_callcache *)v);
3543 asan_poison_object_if(ptr, v);
3545 return 0;
3548 void
3549 rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
3551 rb_objspace_each_objects(trace_set_i, &turnon_events);
3554 VALUE
3555 rb_iseqw_local_variables(VALUE iseqval)
3557 return rb_iseq_local_variables(iseqw_check(iseqval));
3561 * call-seq:
3562 * iseq.to_binary(extra_data = nil) -> binary str
3564 * Returns serialized iseq binary format data as a String object.
3565 * A corresponding iseq object is created by
3566 * RubyVM::InstructionSequence.load_from_binary() method.
3568 * String extra_data will be saved with binary data.
3569 * You can access this data with
3570 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary).
3572 * Note that the translated binary data is not portable.
3573 * You can not move this binary data to another machine.
3574 * You can not use the binary data which is created by another
3575 * version/another architecture of Ruby.
3577 static VALUE
3578 iseqw_to_binary(int argc, VALUE *argv, VALUE self)
3580 VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0];
3581 return rb_iseq_ibf_dump(iseqw_check(self), opt);
3585 * call-seq:
3586 * RubyVM::InstructionSequence.load_from_binary(binary) -> iseq
3588 * Load an iseq object from binary format String object
3589 * created by RubyVM::InstructionSequence.to_binary.
3591 * This loader does not have a verifier, so that loading broken/modified
3592 * binary causes critical problem.
3594 * You should not load binary data provided by others.
3595 * You should use binary data translated by yourself.
3597 static VALUE
3598 iseqw_s_load_from_binary(VALUE self, VALUE str)
3600 return iseqw_new(rb_iseq_ibf_load(str));
3604 * call-seq:
3605 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary) -> str
3607 * Load extra data embed into binary format String object.
3609 static VALUE
3610 iseqw_s_load_from_binary_extra_data(VALUE self, VALUE str)
3612 return rb_iseq_ibf_load_extra_data(str);
3615 #if VM_INSN_INFO_TABLE_IMPL == 2
3617 /* An implementation of succinct bit-vector for insn_info table.
3619 * A succinct bit-vector is a small and efficient data structure that provides
3620 * a bit-vector augmented with an index for O(1) rank operation:
3622 * rank(bv, n): the number of 1's within a range from index 0 to index n
3624 * This can be used to lookup insn_info table from PC.
3625 * For example, consider the following iseq and insn_info_table:
3627 * iseq insn_info_table
3628 * PC insn+operand position lineno event
3629 * 0: insn1 0: 1 [Li]
3630 * 2: insn2 2: 2 [Li] <= (A)
3631 * 5: insn3 8: 3 [Li] <= (B)
3632 * 8: insn4
3634 * In this case, a succinct bit-vector whose indexes 0, 2, 8 is "1" and
3635 * other indexes is "0", i.e., "101000001", is created.
3636 * To lookup the lineno of insn2, calculate rank("10100001", 2) = 2, so
3637 * the line (A) is the entry in question.
3638 * To lookup the lineno of insn4, calculate rank("10100001", 8) = 3, so
3639 * the line (B) is the entry in question.
3641 * A naive implementation of succinct bit-vector works really well
3642 * not only for large size but also for small size. However, it has
3643 * tiny overhead for very small size. So, this implementation consist
3644 * of two parts: one part is the "immediate" table that keeps rank result
3645 * as a raw table, and the other part is a normal succinct bit-vector.
3648 #define IMMEDIATE_TABLE_SIZE 54 /* a multiple of 9, and < 128 */
3650 struct succ_index_table {
3651 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
3652 struct succ_dict_block {
3653 unsigned int rank;
3654 uint64_t small_block_ranks; /* 9 bits * 7 = 63 bits */
3655 uint64_t bits[512/64];
3656 } succ_part[FLEX_ARY_LEN];
3659 #define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
3660 #define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
3661 #define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
3662 #define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
3664 static struct succ_index_table *
3665 succ_index_table_create(int max_pos, int *data, int size)
3667 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3668 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3669 struct succ_index_table *sd =
3670 rb_xcalloc_mul_add_mul(
3671 imm_size, sizeof(uint64_t),
3672 succ_size, sizeof(struct succ_dict_block));
3673 int i, j, k, r;
3675 r = 0;
3676 for (j = 0; j < imm_size; j++) {
3677 for (i = 0; i < 9; i++) {
3678 if (r < size && data[r] == j * 9 + i) r++;
3679 imm_block_rank_set(sd->imm_part[j], i, r);
3682 for (k = 0; k < succ_size; k++) {
3683 struct succ_dict_block *sd_block = &sd->succ_part[k];
3684 int small_rank = 0;
3685 sd_block->rank = r;
3686 for (j = 0; j < 8; j++) {
3687 uint64_t bits = 0;
3688 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
3689 for (i = 0; i < 64; i++) {
3690 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
3691 bits |= ((uint64_t)1) << i;
3692 r++;
3695 sd_block->bits[j] = bits;
3696 small_rank += rb_popcount64(bits);
3699 return sd;
3702 static unsigned int *
3703 succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size)
3705 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3706 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3707 unsigned int *positions = ALLOC_N(unsigned int, size), *p;
3708 int i, j, k, r = -1;
3709 p = positions;
3710 for (j = 0; j < imm_size; j++) {
3711 for (i = 0; i < 9; i++) {
3712 int nr = imm_block_rank_get(sd->imm_part[j], i);
3713 if (r != nr) *p++ = j * 9 + i;
3714 r = nr;
3717 for (k = 0; k < succ_size; k++) {
3718 for (j = 0; j < 8; j++) {
3719 for (i = 0; i < 64; i++) {
3720 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
3721 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
3726 return positions;
3729 static int
3730 succ_index_lookup(const struct succ_index_table *sd, int x)
3732 if (x < IMMEDIATE_TABLE_SIZE) {
3733 const int i = x / 9;
3734 const int j = x % 9;
3735 return imm_block_rank_get(sd->imm_part[i], j);
3737 else {
3738 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
3739 const struct succ_dict_block *block = &sd->succ_part[block_index];
3740 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
3741 const int small_block_index = block_bit_index / 64;
3742 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
3743 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
3745 return block->rank + small_block_popcount + popcnt;
3748 #endif
3752 * call-seq:
3753 * iseq.script_lines -> array or nil
3755 * It returns recorded script lines if it is availalble.
3756 * The script lines are not limited to the iseq range, but
3757 * are entire lines of the source file.
3759 * Note that this is an API for ruby internal use, debugging,
3760 * and research. Do not use this for any other purpose.
3761 * The compatibility is not guaranteed.
3763 static VALUE
3764 iseqw_script_lines(VALUE self)
3766 const rb_iseq_t *iseq = iseqw_check(self);
3767 return iseq->body->variable.script_lines;
3771 * Document-class: RubyVM::InstructionSequence
3773 * The InstructionSequence class represents a compiled sequence of
3774 * instructions for the Virtual Machine used in MRI. Not all implementations of Ruby
3775 * may implement this class, and for the implementations that implement it,
3776 * the methods defined and behavior of the methods can change in any version.
3778 * With it, you can get a handle to the instructions that make up a method or
3779 * a proc, compile strings of Ruby code down to VM instructions, and
3780 * disassemble instruction sequences to strings for easy inspection. It is
3781 * mostly useful if you want to learn how YARV works, but it also lets
3782 * you control various settings for the Ruby iseq compiler.
3784 * You can find the source for the VM instructions in +insns.def+ in the Ruby
3785 * source.
3787 * The instruction sequence results will almost certainly change as Ruby
3788 * changes, so example output in this documentation may be different from what
3789 * you see.
3791 * Of course, this class is MRI specific.
3794 void
3795 Init_ISeq(void)
3797 /* declare ::RubyVM::InstructionSequence */
3798 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
3799 rb_undef_alloc_func(rb_cISeq);
3800 rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
3801 rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
3802 rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
3803 rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
3804 rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
3806 rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
3807 rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1);
3808 rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1);
3810 /* location APIs */
3811 rb_define_method(rb_cISeq, "path", iseqw_path, 0);
3812 rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0);
3813 rb_define_method(rb_cISeq, "label", iseqw_label, 0);
3814 rb_define_method(rb_cISeq, "base_label", iseqw_base_label, 0);
3815 rb_define_method(rb_cISeq, "first_lineno", iseqw_first_lineno, 0);
3816 rb_define_method(rb_cISeq, "trace_points", iseqw_trace_points, 0);
3817 rb_define_method(rb_cISeq, "each_child", iseqw_each_child, 0);
3819 #if 0 /* TBD */
3820 rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
3821 rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
3822 /* disable this feature because there is no verifier. */
3823 rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
3824 #endif
3825 (void)iseq_s_load;
3827 rb_define_singleton_method(rb_cISeq, "compile", iseqw_s_compile, -1);
3828 rb_define_singleton_method(rb_cISeq, "new", iseqw_s_compile, -1);
3829 rb_define_singleton_method(rb_cISeq, "compile_file", iseqw_s_compile_file, -1);
3830 rb_define_singleton_method(rb_cISeq, "compile_option", iseqw_s_compile_option_get, 0);
3831 rb_define_singleton_method(rb_cISeq, "compile_option=", iseqw_s_compile_option_set, 1);
3832 rb_define_singleton_method(rb_cISeq, "disasm", iseqw_s_disasm, 1);
3833 rb_define_singleton_method(rb_cISeq, "disassemble", iseqw_s_disasm, 1);
3834 rb_define_singleton_method(rb_cISeq, "of", iseqw_s_of, 1);
3836 // script lines
3837 rb_define_method(rb_cISeq, "script_lines", iseqw_script_lines, 0);
3839 rb_undef_method(CLASS_OF(rb_cISeq), "translate");
3840 rb_undef_method(CLASS_OF(rb_cISeq), "load_iseq");