Ruby 3.2.4p170 (2024-04-23 revision af471c0e0127eea0cafa6f308c0425bbfab0acf5)
iseq.c
1/**********************************************************************
2
3 iseq.c -
4
5 $Author$
6 created at: 2006-07-11(Tue) 09:00:03 +0900
7
8 Copyright (C) 2006 Koichi Sasada
9
10**********************************************************************/
11
12#define RUBY_VM_INSNS_INFO 1
13/* #define RUBY_MARK_FREE_DEBUG 1 */
14
15#include "ruby/internal/config.h"
16
17#ifdef HAVE_DLADDR
18# include <dlfcn.h>
19#endif
20
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"
46
47VALUE rb_cISeq;
48static VALUE iseqw_new(const rb_iseq_t *iseq);
49static const rb_iseq_t *iseqw_check(VALUE iseqw);
50
51#if VM_INSN_INFO_TABLE_IMPL == 2
52static struct succ_index_table *succ_index_table_create(int max_pos, int *data, int size);
53static unsigned int *succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size);
54static int succ_index_lookup(const struct succ_index_table *sd, int x);
55#endif
56
57#define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
58
59static inline VALUE
60obj_resurrect(VALUE obj)
61{
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;
75 }
76 }
77 return obj;
78}
79
80static void
81free_arena(struct iseq_compile_data_storage *cur)
82{
83 struct iseq_compile_data_storage *next;
84
85 while (cur) {
86 next = cur->next;
87 ruby_xfree(cur);
88 cur = next;
89 }
90}
91
92static void
93compile_data_free(struct iseq_compile_data *compile_data)
94{
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);
100 }
101 ruby_xfree(compile_data);
102 }
103}
104
105static void
106remove_from_constant_cache(ID id, IC ic)
107{
108 rb_vm_t *vm = GET_VM();
109 VALUE lookup_result;
110 st_data_t ic_data = (st_data_t)ic;
111
112 if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
113 st_table *ics = (st_table *)lookup_result;
114 st_delete(ics, &ic_data, NULL);
115
116 if (ics->num_entries == 0) {
117 rb_id_table_delete(vm->constant_cache, id);
118 st_free_table(ics);
119 }
120 }
121}
122
123// When an ISEQ is being freed, all of its associated ICs are going to go away
124// as well. Because of this, we need to iterate over the ICs, and clear them
125// from the VM's constant cache.
126static void
127iseq_clear_ic_references(const rb_iseq_t *iseq)
128{
129 // In some cases (when there is a compilation error), we end up with
130 // ic_size greater than 0, but no allocated is_entries buffer.
131 // If there's no is_entries buffer to loop through, return early.
132 // [Bug #19173]
133 if (!ISEQ_BODY(iseq)->is_entries) {
134 return;
135 }
136
137 for (unsigned int ic_idx = 0; ic_idx < ISEQ_BODY(iseq)->ic_size; ic_idx++) {
138 IC ic = &ISEQ_IS_IC_ENTRY(ISEQ_BODY(iseq), ic_idx);
139
140 // Iterate over the IC's constant path's segments and clean any references to
141 // the ICs out of the VM's constant cache table.
142 const ID *segments = ic->segments;
143
144 // It's possible that segments is NULL if we overallocated an IC but
145 // optimizations removed the instruction using it
146 if (segments == NULL)
147 continue;
148
149 for (int i = 0; segments[i]; i++) {
150 ID id = segments[i];
151 if (id == idNULL) continue;
152 remove_from_constant_cache(id, ic);
153 }
154
155 ruby_xfree((void *)segments);
156 }
157}
158
159void
160rb_iseq_free(const rb_iseq_t *iseq)
161{
162 RUBY_FREE_ENTER("iseq");
163
164 if (iseq && ISEQ_BODY(iseq)) {
165 iseq_clear_ic_references(iseq);
166 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
167 mjit_free_iseq(iseq); /* Notify MJIT */
168#if USE_YJIT
169 rb_yjit_iseq_free(body->yjit_payload);
170#endif
171 ruby_xfree((void *)body->iseq_encoded);
172 ruby_xfree((void *)body->insns_info.body);
173 if (body->insns_info.positions) ruby_xfree((void *)body->insns_info.positions);
174#if VM_INSN_INFO_TABLE_IMPL == 2
175 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
176#endif
177 if (LIKELY(body->local_table != rb_iseq_shared_exc_local_tbl))
178 ruby_xfree((void *)body->local_table);
179 ruby_xfree((void *)body->is_entries);
180
181 if (body->call_data) {
182 ruby_xfree(body->call_data);
183 }
184 ruby_xfree((void *)body->catch_table);
185 ruby_xfree((void *)body->param.opt_table);
186 if (ISEQ_MBITS_BUFLEN(body->iseq_size) > 1 && body->mark_bits.list) {
187 ruby_xfree((void *)body->mark_bits.list);
188 }
189
190 ruby_xfree(body->variable.original_iseq);
191
192 if (body->param.keyword != NULL) {
193 if (body->param.keyword->table != &body->local_table[body->param.keyword->bits_start - body->param.keyword->num])
194 ruby_xfree((void *)body->param.keyword->table);
195 ruby_xfree((void *)body->param.keyword->default_values);
196 ruby_xfree((void *)body->param.keyword);
197 }
198 compile_data_free(ISEQ_COMPILE_DATA(iseq));
199 if (body->outer_variables) rb_id_table_free(body->outer_variables);
200 ruby_xfree(body);
201 }
202
203 if (iseq && ISEQ_EXECUTABLE_P(iseq) && iseq->aux.exec.local_hooks) {
204 rb_hook_list_free(iseq->aux.exec.local_hooks);
205 }
206
207 RUBY_FREE_LEAVE("iseq");
208}
209
210typedef VALUE iseq_value_itr_t(void *ctx, VALUE obj);
211
212static inline void
213iseq_scan_bits(unsigned int page, iseq_bits_t bits, VALUE *code, iseq_value_itr_t *func, void *data)
214{
215 unsigned int offset;
216 unsigned int page_offset = (page * ISEQ_MBITS_BITLENGTH);
217
218 while (bits) {
219 offset = ntz_intptr(bits);
220 VALUE op = code[page_offset + offset];
221 VALUE newop = func(data, op);
222 if (newop != op) {
223 code[page_offset + offset] = newop;
224 if (data) {
225 VALUE *original_iseq = (VALUE *)data;
226 original_iseq[page_offset + offset] = newop;
227 }
228 }
229 bits &= bits - 1; // Reset Lowest Set Bit (BLSR)
230 }
231}
232
233static void
234rb_iseq_each_value(const rb_iseq_t *iseq, iseq_value_itr_t * func, void *data)
235{
236 unsigned int size;
237 VALUE *code;
238 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
239
240 size = body->iseq_size;
241 code = body->iseq_encoded;
242
243 union iseq_inline_storage_entry *is_entries = body->is_entries;
244
245 if (body->is_entries) {
246 // Skip iterating over ivc caches
247 is_entries += body->ivc_size;
248
249 // ICVARC entries
250 for (unsigned int i = 0; i < body->icvarc_size; i++, is_entries++) {
251 ICVARC icvarc = (ICVARC)is_entries;
252 if (icvarc->entry) {
253 RUBY_ASSERT(!RB_TYPE_P(icvarc->entry->class_value, T_NONE));
254
255 VALUE nv = func(data, icvarc->entry->class_value);
256 if (icvarc->entry->class_value != nv) {
257 icvarc->entry->class_value = nv;
258 }
259 }
260 }
261
262 // ISE entries
263 for (unsigned int i = 0; i < body->ise_size; i++, is_entries++) {
264 union iseq_inline_storage_entry *const is = (union iseq_inline_storage_entry *)is_entries;
265 if (is->once.value) {
266 VALUE nv = func(data, is->once.value);
267 if (is->once.value != nv) {
268 is->once.value = nv;
269 }
270 }
271 }
272
273 // IC Entries
274 for (unsigned int i = 0; i < body->ic_size; i++, is_entries++) {
275 IC ic = (IC)is_entries;
276 if (ic->entry) {
277 VALUE nv = func(data, (VALUE)ic->entry);
278 if ((VALUE)ic->entry != nv) {
279 ic->entry = (void *)nv;
280 }
281 }
282 }
283 }
284
285 // Embedded VALUEs
286 if (body->mark_bits.list) {
287 if (ISEQ_MBITS_BUFLEN(size) == 1) {
288 iseq_scan_bits(0, body->mark_bits.single, code, func, data);
289 }
290 else {
291 if (body->mark_bits.list) {
292 for (unsigned int i = 0; i < ISEQ_MBITS_BUFLEN(size); i++) {
293 iseq_bits_t bits = body->mark_bits.list[i];
294 iseq_scan_bits(i, bits, code, func, data);
295 }
296 }
297 }
298 }
299}
300
301static VALUE
302update_each_insn_value(void *ctx, VALUE obj)
303{
304 return rb_gc_location(obj);
305}
306
307void
308rb_iseq_update_references(rb_iseq_t *iseq)
309{
310 if (ISEQ_BODY(iseq)) {
311 struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
312
313 body->variable.coverage = rb_gc_location(body->variable.coverage);
314 body->variable.pc2branchindex = rb_gc_location(body->variable.pc2branchindex);
315 body->variable.script_lines = rb_gc_location(body->variable.script_lines);
316 body->location.label = rb_gc_location(body->location.label);
317 body->location.base_label = rb_gc_location(body->location.base_label);
318 body->location.pathobj = rb_gc_location(body->location.pathobj);
319 if (body->local_iseq) {
320 body->local_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->local_iseq);
321 }
322 if (body->parent_iseq) {
323 body->parent_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->parent_iseq);
324 }
325 if (body->mandatory_only_iseq) {
326 body->mandatory_only_iseq = (struct rb_iseq_struct *)rb_gc_location((VALUE)body->mandatory_only_iseq);
327 }
328 if (body->call_data) {
329 for (unsigned int i=0; i<body->ci_size; i++) {
330 struct rb_call_data *cds = body->call_data;
331 if (!SPECIAL_CONST_P((VALUE)cds[i].ci)) {
332 cds[i].ci = (struct rb_callinfo *)rb_gc_location((VALUE)cds[i].ci);
333 }
334 cds[i].cc = (struct rb_callcache *)rb_gc_location((VALUE)cds[i].cc);
335 }
336 }
337 VALUE *original_iseq = ISEQ_ORIGINAL_ISEQ(iseq);
338 rb_iseq_each_value(iseq, update_each_insn_value, (void *)original_iseq);
339
340 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
341 int i, j;
342
343 i = body->param.keyword->required_num;
344
345 for (j = 0; i < body->param.keyword->num; i++, j++) {
346 VALUE obj = body->param.keyword->default_values[j];
347 if (!UNDEF_P(obj)) {
348 body->param.keyword->default_values[j] = rb_gc_location(obj);
349 }
350 }
351 }
352
353 if (body->catch_table) {
354 struct iseq_catch_table *table = body->catch_table;
355 unsigned int i;
356 for (i = 0; i < table->size; i++) {
357 struct iseq_catch_table_entry *entry;
358 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
359 if (entry->iseq) {
360 entry->iseq = (rb_iseq_t *)rb_gc_location((VALUE)entry->iseq);
361 }
362 }
363 }
364#if USE_MJIT
365 mjit_update_references(iseq);
366#endif
367#if USE_YJIT
368 rb_yjit_iseq_update_references(body->yjit_payload);
369#endif
370 }
371}
372
373static VALUE
374each_insn_value(void *ctx, VALUE obj)
375{
376 rb_gc_mark_movable(obj);
377 return obj;
378}
379
380void
381rb_iseq_mark(const rb_iseq_t *iseq)
382{
383 RUBY_MARK_ENTER("iseq");
384
385 RUBY_MARK_UNLESS_NULL(iseq->wrapper);
386
387 if (ISEQ_BODY(iseq)) {
388 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
389
390 rb_iseq_each_value(iseq, each_insn_value, NULL);
391
392 rb_gc_mark_movable(body->variable.coverage);
393 rb_gc_mark_movable(body->variable.pc2branchindex);
394 rb_gc_mark_movable(body->variable.script_lines);
395 rb_gc_mark_movable(body->location.label);
396 rb_gc_mark_movable(body->location.base_label);
397 rb_gc_mark_movable(body->location.pathobj);
398 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->mandatory_only_iseq);
399 RUBY_MARK_MOVABLE_UNLESS_NULL((VALUE)body->parent_iseq);
400
401 if (body->call_data) {
402 struct rb_call_data *cds = (struct rb_call_data *)body->call_data;
403 for (unsigned int i=0; i<body->ci_size; i++) {
404 const struct rb_callinfo *ci = cds[i].ci;
405 const struct rb_callcache *cc = cds[i].cc;
406
407 if (vm_ci_markable(ci)) {
408 rb_gc_mark_movable((VALUE)ci);
409 }
410
411 if (cc) {
412 VM_ASSERT((cc->flags & VM_CALLCACHE_ON_STACK) == 0);
413
414 if (vm_cc_markable(cc)) {
415 if (!vm_cc_invalidated_p(cc)) {
416 rb_gc_mark_movable((VALUE)cc);
417 }
418 else {
419 cds[i].cc = rb_vm_empty_cc();
420 }
421 }
422 }
423 }
424 }
425
426 if (body->param.flags.has_kw && ISEQ_COMPILE_DATA(iseq) == NULL) {
427 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
428 int i, j;
429
430 i = keyword->required_num;
431
432 for (j = 0; i < keyword->num; i++, j++) {
433 VALUE obj = keyword->default_values[j];
434 if (!SPECIAL_CONST_P(obj)) {
435 rb_gc_mark_movable(obj);
436 }
437 }
438 }
439
440 if (body->catch_table) {
441 const struct iseq_catch_table *table = body->catch_table;
442 unsigned int i;
443 for (i = 0; i < table->size; i++) {
444 const struct iseq_catch_table_entry *entry;
445 entry = UNALIGNED_MEMBER_PTR(table, entries[i]);
446 if (entry->iseq) {
447 rb_gc_mark_movable((VALUE)entry->iseq);
448 }
449 }
450 }
451
452#if USE_MJIT
453 mjit_mark_cc_entries(body);
454#endif
455#if USE_YJIT
456 rb_yjit_iseq_mark(body->yjit_payload);
457#endif
458 }
459
460 if (FL_TEST_RAW((VALUE)iseq, ISEQ_NOT_LOADED_YET)) {
461 rb_gc_mark(iseq->aux.loader.obj);
462 }
463 else if (FL_TEST_RAW((VALUE)iseq, ISEQ_USE_COMPILE_DATA)) {
464 const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
465
466 rb_iseq_mark_insn_storage(compile_data->insn.storage_head);
467
468 RUBY_MARK_UNLESS_NULL(compile_data->err_info);
469 if (RTEST(compile_data->catch_table_ary)) {
470 rb_gc_mark(compile_data->catch_table_ary);
471 }
472 VM_ASSERT(compile_data != NULL);
473 }
474 else {
475 /* executable */
476 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
477 if (iseq->aux.exec.local_hooks) {
478 rb_hook_list_mark(iseq->aux.exec.local_hooks);
479 }
480 }
481
482 RUBY_MARK_LEAVE("iseq");
483}
484
485static size_t
486param_keyword_size(const struct rb_iseq_param_keyword *pkw)
487{
488 size_t size = 0;
489
490 if (!pkw) return size;
491
492 size += sizeof(struct rb_iseq_param_keyword);
493 size += sizeof(VALUE) * (pkw->num - pkw->required_num);
494
495 return size;
496}
497
498size_t
499rb_iseq_memsize(const rb_iseq_t *iseq)
500{
501 size_t size = 0; /* struct already counted as RVALUE size */
502 const struct rb_iseq_constant_body *body = ISEQ_BODY(iseq);
503 const struct iseq_compile_data *compile_data;
504
505 /* TODO: should we count original_iseq? */
506
507 if (ISEQ_EXECUTABLE_P(iseq) && body) {
508 size += sizeof(struct rb_iseq_constant_body);
509 size += body->iseq_size * sizeof(VALUE);
510 size += body->insns_info.size * (sizeof(struct iseq_insn_info_entry) + sizeof(unsigned int));
511 size += body->local_table_size * sizeof(ID);
512 size += ISEQ_MBITS_BUFLEN(body->iseq_size) * ISEQ_MBITS_SIZE;
513 if (body->catch_table) {
514 size += iseq_catch_table_bytes(body->catch_table->size);
515 }
516 size += (body->param.opt_num + 1) * sizeof(VALUE);
517 size += param_keyword_size(body->param.keyword);
518
519 /* body->is_entries */
520 size += ISEQ_IS_SIZE(body) * sizeof(union iseq_inline_storage_entry);
521
522 if (ISEQ_BODY(iseq)->is_entries) {
523 /* IC entries constant segments */
524 for (unsigned int ic_idx = 0; ic_idx < body->ic_size; ic_idx++) {
525 IC ic = &ISEQ_IS_IC_ENTRY(body, ic_idx);
526 const ID *ids = ic->segments;
527 if (!ids) continue;
528 while (*ids++) {
529 size += sizeof(ID);
530 }
531 size += sizeof(ID); // null terminator
532 }
533 }
534
535 /* body->call_data */
536 size += body->ci_size * sizeof(struct rb_call_data);
537 // TODO: should we count imemo_callinfo?
538 }
539
540 compile_data = ISEQ_COMPILE_DATA(iseq);
541 if (compile_data) {
542 struct iseq_compile_data_storage *cur;
543
544 size += sizeof(struct iseq_compile_data);
545
546 cur = compile_data->node.storage_head;
547 while (cur) {
548 size += cur->size + offsetof(struct iseq_compile_data_storage, buff);
549 cur = cur->next;
550 }
551 }
552
553 return size;
554}
555
557rb_iseq_constant_body_alloc(void)
558{
559 struct rb_iseq_constant_body *iseq_body;
560 iseq_body = ZALLOC(struct rb_iseq_constant_body);
561 return iseq_body;
562}
563
564static rb_iseq_t *
565iseq_alloc(void)
566{
567 rb_iseq_t *iseq = iseq_imemo_alloc();
568 ISEQ_BODY(iseq) = rb_iseq_constant_body_alloc();
569 return iseq;
570}
571
572VALUE
573rb_iseq_pathobj_new(VALUE path, VALUE realpath)
574{
575 VALUE pathobj;
576 VM_ASSERT(RB_TYPE_P(path, T_STRING));
577 VM_ASSERT(NIL_P(realpath) || RB_TYPE_P(realpath, T_STRING));
578
579 if (path == realpath ||
580 (!NIL_P(realpath) && rb_str_cmp(path, realpath) == 0)) {
581 pathobj = rb_fstring(path);
582 }
583 else {
584 if (!NIL_P(realpath)) realpath = rb_fstring(realpath);
585 pathobj = rb_ary_new_from_args(2, rb_fstring(path), realpath);
586 rb_obj_freeze(pathobj);
587 }
588 return pathobj;
589}
590
591void
592rb_iseq_pathobj_set(const rb_iseq_t *iseq, VALUE path, VALUE realpath)
593{
594 RB_OBJ_WRITE(iseq, &ISEQ_BODY(iseq)->location.pathobj,
595 rb_iseq_pathobj_new(path, realpath));
596}
597
598static rb_iseq_location_t *
599iseq_location_setup(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id)
600{
601 rb_iseq_location_t *loc = &ISEQ_BODY(iseq)->location;
602
603 rb_iseq_pathobj_set(iseq, path, realpath);
604 RB_OBJ_WRITE(iseq, &loc->label, name);
605 RB_OBJ_WRITE(iseq, &loc->base_label, name);
606 loc->first_lineno = first_lineno;
607 if (code_location) {
608 loc->node_id = node_id;
609 loc->code_location = *code_location;
610 }
611 else {
612 loc->code_location.beg_pos.lineno = 0;
613 loc->code_location.beg_pos.column = 0;
614 loc->code_location.end_pos.lineno = -1;
615 loc->code_location.end_pos.column = -1;
616 }
617
618 return loc;
619}
620
621static void
622set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
623{
624 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
625 const VALUE type = body->type;
626
627 /* set class nest stack */
628 if (type == ISEQ_TYPE_TOP) {
629 body->local_iseq = iseq;
630 }
631 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
632 body->local_iseq = iseq;
633 }
634 else if (piseq) {
635 body->local_iseq = ISEQ_BODY(piseq)->local_iseq;
636 }
637
638 if (piseq) {
639 body->parent_iseq = piseq;
640 }
641
642 if (type == ISEQ_TYPE_MAIN) {
643 body->local_iseq = iseq;
644 }
645}
646
647static struct iseq_compile_data_storage *
648new_arena(void)
649{
650 struct iseq_compile_data_storage * new_arena =
652 ALLOC_N(char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
653 offsetof(struct iseq_compile_data_storage, buff));
654
655 new_arena->pos = 0;
656 new_arena->next = 0;
657 new_arena->size = INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
658
659 return new_arena;
660}
661
662static VALUE
663prepare_iseq_build(rb_iseq_t *iseq,
664 VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_code_location_t *code_location, const int node_id,
665 const rb_iseq_t *parent, int isolated_depth, enum rb_iseq_type type,
666 VALUE script_lines, const rb_compile_option_t *option)
667{
668 VALUE coverage = Qfalse;
669 VALUE err_info = Qnil;
670 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
671
672 if (parent && (type == ISEQ_TYPE_MAIN || type == ISEQ_TYPE_TOP))
673 err_info = Qfalse;
674
675 body->type = type;
676 set_relation(iseq, parent);
677
678 name = rb_fstring(name);
679 iseq_location_setup(iseq, name, path, realpath, first_lineno, code_location, node_id);
680 if (iseq != body->local_iseq) {
681 RB_OBJ_WRITE(iseq, &body->location.base_label, ISEQ_BODY(body->local_iseq)->location.label);
682 }
683 ISEQ_COVERAGE_SET(iseq, Qnil);
684 ISEQ_ORIGINAL_ISEQ_CLEAR(iseq);
685 body->variable.flip_count = 0;
686
687 if (NIL_P(script_lines)) {
688 RB_OBJ_WRITE(iseq, &body->variable.script_lines, Qnil);
689 }
690 else {
691 RB_OBJ_WRITE(iseq, &body->variable.script_lines, rb_ractor_make_shareable(script_lines));
692 }
693
694 ISEQ_COMPILE_DATA_ALLOC(iseq);
695 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->err_info, err_info);
696 RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, Qnil);
697
698 ISEQ_COMPILE_DATA(iseq)->node.storage_head = ISEQ_COMPILE_DATA(iseq)->node.storage_current = new_arena();
699 ISEQ_COMPILE_DATA(iseq)->insn.storage_head = ISEQ_COMPILE_DATA(iseq)->insn.storage_current = new_arena();
700 ISEQ_COMPILE_DATA(iseq)->isolated_depth = isolated_depth;
701 ISEQ_COMPILE_DATA(iseq)->option = option;
702 ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
703 ISEQ_COMPILE_DATA(iseq)->builtin_function_table = GET_VM()->builtin_function_table;
704
705 if (option->coverage_enabled) {
706 VALUE coverages = rb_get_coverages();
707 if (RTEST(coverages)) {
708 coverage = rb_hash_lookup(coverages, rb_iseq_path(iseq));
709 if (NIL_P(coverage)) coverage = Qfalse;
710 }
711 }
712 ISEQ_COVERAGE_SET(iseq, coverage);
713 if (coverage && ISEQ_BRANCH_COVERAGE(iseq))
714 ISEQ_PC2BRANCHINDEX_SET(iseq, rb_ary_hidden_new(0));
715
716 return Qtrue;
717}
718
719#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
720static void validate_get_insn_info(const rb_iseq_t *iseq);
721#endif
722
723void
724rb_iseq_insns_info_encode_positions(const rb_iseq_t *iseq)
725{
726#if VM_INSN_INFO_TABLE_IMPL == 2
727 /* create succ_index_table */
728 struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
729 int size = body->insns_info.size;
730 int max_pos = body->iseq_size;
731 int *data = (int *)body->insns_info.positions;
732 if (body->insns_info.succ_index_table) ruby_xfree(body->insns_info.succ_index_table);
733 body->insns_info.succ_index_table = succ_index_table_create(max_pos, data, size);
734#if VM_CHECK_MODE == 0
735 ruby_xfree(body->insns_info.positions);
736 body->insns_info.positions = NULL;
737#endif
738#endif
739}
740
741#if VM_INSN_INFO_TABLE_IMPL == 2
742unsigned int *
743rb_iseq_insns_info_decode_positions(const struct rb_iseq_constant_body *body)
744{
745 int size = body->insns_info.size;
746 int max_pos = body->iseq_size;
747 struct succ_index_table *sd = body->insns_info.succ_index_table;
748 return succ_index_table_invert(max_pos, sd, size);
749}
750#endif
751
752void
753rb_iseq_init_trace(rb_iseq_t *iseq)
754{
755 iseq->aux.exec.global_trace_events = 0;
756 if (ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS) {
757 rb_iseq_trace_set(iseq, ruby_vm_event_enabled_global_flags & ISEQ_TRACE_EVENTS);
758 }
759}
760
761static VALUE
762finish_iseq_build(rb_iseq_t *iseq)
763{
764 struct iseq_compile_data *data = ISEQ_COMPILE_DATA(iseq);
765 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
766 VALUE err = data->err_info;
767 ISEQ_COMPILE_DATA_CLEAR(iseq);
768 compile_data_free(data);
769
770#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
771 validate_get_insn_info(iseq);
772#endif
773
774 if (RTEST(err)) {
775 VALUE path = pathobj_path(body->location.pathobj);
776 if (err == Qtrue) err = rb_exc_new_cstr(rb_eSyntaxError, "compile error");
777 rb_funcallv(err, rb_intern("set_backtrace"), 1, &path);
778 rb_exc_raise(err);
779 }
780
781 RB_DEBUG_COUNTER_INC(iseq_num);
782 RB_DEBUG_COUNTER_ADD(iseq_cd_num, ISEQ_BODY(iseq)->ci_size);
783
784 rb_iseq_init_trace(iseq);
785 return Qtrue;
786}
787
788static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
789 OPT_INLINE_CONST_CACHE, /* int inline_const_cache; */
790 OPT_PEEPHOLE_OPTIMIZATION, /* int peephole_optimization; */
791 OPT_TAILCALL_OPTIMIZATION, /* int tailcall_optimization */
792 OPT_SPECIALISED_INSTRUCTION, /* int specialized_instruction; */
793 OPT_OPERANDS_UNIFICATION, /* int operands_unification; */
794 OPT_INSTRUCTIONS_UNIFICATION, /* int instructions_unification; */
795 OPT_STACK_CACHING, /* int stack_caching; */
796 OPT_FROZEN_STRING_LITERAL,
797 OPT_DEBUG_FROZEN_STRING_LITERAL,
798 TRUE, /* coverage_enabled */
799};
800
801static const rb_compile_option_t COMPILE_OPTION_FALSE = {0};
802
803static void
804set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt)
805{
806#define SET_COMPILE_OPTION(o, h, mem) \
807 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
808 if (flag == Qtrue) { (o)->mem = 1; } \
809 else if (flag == Qfalse) { (o)->mem = 0; } \
810 }
811#define SET_COMPILE_OPTION_NUM(o, h, mem) \
812 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
813 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
814 }
815 SET_COMPILE_OPTION(option, opt, inline_const_cache);
816 SET_COMPILE_OPTION(option, opt, peephole_optimization);
817 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
818 SET_COMPILE_OPTION(option, opt, specialized_instruction);
819 SET_COMPILE_OPTION(option, opt, operands_unification);
820 SET_COMPILE_OPTION(option, opt, instructions_unification);
821 SET_COMPILE_OPTION(option, opt, stack_caching);
822 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
823 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
824 SET_COMPILE_OPTION(option, opt, coverage_enabled);
825 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
826#undef SET_COMPILE_OPTION
827#undef SET_COMPILE_OPTION_NUM
828}
829
830static void
831rb_iseq_make_compile_option(rb_compile_option_t *option, VALUE opt)
832{
833 Check_Type(opt, T_HASH);
834 set_compile_option_from_hash(option, opt);
835}
836
837static void
838make_compile_option(rb_compile_option_t *option, VALUE opt)
839{
840 if (NIL_P(opt)) {
841 *option = COMPILE_OPTION_DEFAULT;
842 }
843 else if (opt == Qfalse) {
844 *option = COMPILE_OPTION_FALSE;
845 }
846 else if (opt == Qtrue) {
847 int i;
848 for (i = 0; i < (int)(sizeof(rb_compile_option_t) / sizeof(int)); ++i)
849 ((int *)option)[i] = 1;
850 }
851 else if (RB_TYPE_P(opt, T_HASH)) {
852 *option = COMPILE_OPTION_DEFAULT;
853 set_compile_option_from_hash(option, opt);
854 }
855 else {
856 rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
857 }
858}
859
860static VALUE
861make_compile_option_value(rb_compile_option_t *option)
862{
863 VALUE opt = rb_hash_new_with_size(11);
864#define SET_COMPILE_OPTION(o, h, mem) \
865 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), RBOOL((o)->mem))
866#define SET_COMPILE_OPTION_NUM(o, h, mem) \
867 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
868 {
869 SET_COMPILE_OPTION(option, opt, inline_const_cache);
870 SET_COMPILE_OPTION(option, opt, peephole_optimization);
871 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
872 SET_COMPILE_OPTION(option, opt, specialized_instruction);
873 SET_COMPILE_OPTION(option, opt, operands_unification);
874 SET_COMPILE_OPTION(option, opt, instructions_unification);
875 SET_COMPILE_OPTION(option, opt, stack_caching);
876 SET_COMPILE_OPTION(option, opt, frozen_string_literal);
877 SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
878 SET_COMPILE_OPTION(option, opt, coverage_enabled);
879 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
880 }
881#undef SET_COMPILE_OPTION
882#undef SET_COMPILE_OPTION_NUM
883 return opt;
884}
885
886rb_iseq_t *
887rb_iseq_new(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
888 const rb_iseq_t *parent, enum rb_iseq_type type)
889{
890 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent,
891 0, type, &COMPILE_OPTION_DEFAULT);
892}
893
894static int
895ast_line_count(const rb_ast_body_t *ast)
896{
897 if (ast->script_lines == Qfalse) {
898 // this occurs when failed to parse the source code with a syntax error
899 return 0;
900 }
901 if (RB_TYPE_P(ast->script_lines, T_ARRAY)){
902 return (int)RARRAY_LEN(ast->script_lines);
903 }
904 return FIX2INT(ast->script_lines);
905}
906
907static VALUE
908iseq_setup_coverage(VALUE coverages, VALUE path, const rb_ast_body_t *ast, int line_offset)
909{
910 int line_count = line_offset + ast_line_count(ast);
911
912 if (line_count >= 0) {
913 int len = (rb_get_coverage_mode() & COVERAGE_TARGET_ONESHOT_LINES) ? 0 : line_count;
914
915 VALUE coverage = rb_default_coverage(len);
916 rb_hash_aset(coverages, path, coverage);
917
918 return coverage;
919 }
920
921 return Qnil;
922}
923
924static inline void
925iseq_new_setup_coverage(VALUE path, const rb_ast_body_t *ast, int line_offset)
926{
927 VALUE coverages = rb_get_coverages();
928
929 if (RTEST(coverages)) {
930 iseq_setup_coverage(coverages, path, ast, 0);
931 }
932}
933
934rb_iseq_t *
935rb_iseq_new_top(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, const rb_iseq_t *parent)
936{
937 iseq_new_setup_coverage(path, ast, 0);
938
939 return rb_iseq_new_with_opt(ast, name, path, realpath, 0, parent, 0,
940 ISEQ_TYPE_TOP, &COMPILE_OPTION_DEFAULT);
941}
942
943rb_iseq_t *
944rb_iseq_new_main(const rb_ast_body_t *ast, VALUE path, VALUE realpath, const rb_iseq_t *parent, int opt)
945{
946 iseq_new_setup_coverage(path, ast, 0);
947
948 return rb_iseq_new_with_opt(ast, rb_fstring_lit("<main>"),
949 path, realpath, 0,
950 parent, 0, ISEQ_TYPE_MAIN, opt ? &COMPILE_OPTION_DEFAULT : &COMPILE_OPTION_FALSE);
951}
952
953rb_iseq_t *
954rb_iseq_new_eval(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath, int first_lineno, const rb_iseq_t *parent, int isolated_depth)
955{
956 if (rb_get_coverage_mode() & COVERAGE_TARGET_EVAL) {
957 VALUE coverages = rb_get_coverages();
958 if (RTEST(coverages) && RTEST(path) && !RTEST(rb_hash_has_key(coverages, path))) {
959 iseq_setup_coverage(coverages, path, ast, first_lineno - 1);
960 }
961 }
962
963 return rb_iseq_new_with_opt(ast, name, path, realpath, first_lineno,
964 parent, isolated_depth, ISEQ_TYPE_EVAL, &COMPILE_OPTION_DEFAULT);
965}
966
967static inline rb_iseq_t *
968iseq_translate(rb_iseq_t *iseq)
969{
970 if (rb_respond_to(rb_cISeq, rb_intern("translate"))) {
971 VALUE v1 = iseqw_new(iseq);
972 VALUE v2 = rb_funcall(rb_cISeq, rb_intern("translate"), 1, v1);
973 if (v1 != v2 && CLASS_OF(v2) == rb_cISeq) {
974 iseq = (rb_iseq_t *)iseqw_check(v2);
975 }
976 }
977
978 return iseq;
979}
980
981rb_iseq_t *
982rb_iseq_new_with_opt(const rb_ast_body_t *ast, VALUE name, VALUE path, VALUE realpath,
983 int first_lineno, const rb_iseq_t *parent, int isolated_depth,
984 enum rb_iseq_type type, const rb_compile_option_t *option)
985{
986 const NODE *node = ast ? ast->root : 0;
987 /* TODO: argument check */
988 rb_iseq_t *iseq = iseq_alloc();
989 rb_compile_option_t new_opt;
990
991 if (option) {
992 new_opt = *option;
993 }
994 else {
995 new_opt = COMPILE_OPTION_DEFAULT;
996 }
997 if (ast && ast->compile_option) rb_iseq_make_compile_option(&new_opt, ast->compile_option);
998
999 VALUE script_lines = Qnil;
1000
1001 if (ast && !FIXNUM_P(ast->script_lines) && ast->script_lines) {
1002 script_lines = ast->script_lines;
1003 }
1004 else if (parent) {
1005 script_lines = ISEQ_BODY(parent)->variable.script_lines;
1006 }
1007
1008 prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, node ? nd_node_id(node) : -1,
1009 parent, isolated_depth, type, script_lines, &new_opt);
1010
1011 rb_iseq_compile_node(iseq, node);
1012 finish_iseq_build(iseq);
1013
1014 return iseq_translate(iseq);
1015}
1016
1017rb_iseq_t *
1018rb_iseq_new_with_callback(
1019 const struct rb_iseq_new_with_callback_callback_func * ifunc,
1020 VALUE name, VALUE path, VALUE realpath,
1021 int first_lineno, const rb_iseq_t *parent,
1022 enum rb_iseq_type type, const rb_compile_option_t *option)
1023{
1024 /* TODO: argument check */
1025 rb_iseq_t *iseq = iseq_alloc();
1026
1027 if (!option) option = &COMPILE_OPTION_DEFAULT;
1028 prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, -1, parent, 0, type, Qnil, option);
1029
1030 rb_iseq_compile_callback(iseq, ifunc);
1031 finish_iseq_build(iseq);
1032
1033 return iseq;
1034}
1035
1036const rb_iseq_t *
1037rb_iseq_load_iseq(VALUE fname)
1038{
1039 VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname);
1040
1041 if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) {
1042 return iseqw_check(iseqv);
1043 }
1044
1045 return NULL;
1046}
1047
1048#define CHECK_ARRAY(v) rb_to_array_type(v)
1049#define CHECK_HASH(v) rb_to_hash_type(v)
1050#define CHECK_STRING(v) rb_str_to_str(v)
1051#define CHECK_SYMBOL(v) rb_to_symbol_type(v)
1052static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
1053
1054static enum rb_iseq_type
1055iseq_type_from_sym(VALUE type)
1056{
1057 const ID id_top = rb_intern("top");
1058 const ID id_method = rb_intern("method");
1059 const ID id_block = rb_intern("block");
1060 const ID id_class = rb_intern("class");
1061 const ID id_rescue = rb_intern("rescue");
1062 const ID id_ensure = rb_intern("ensure");
1063 const ID id_eval = rb_intern("eval");
1064 const ID id_main = rb_intern("main");
1065 const ID id_plain = rb_intern("plain");
1066 /* ensure all symbols are static or pinned down before
1067 * conversion */
1068 const ID typeid = rb_check_id(&type);
1069 if (typeid == id_top) return ISEQ_TYPE_TOP;
1070 if (typeid == id_method) return ISEQ_TYPE_METHOD;
1071 if (typeid == id_block) return ISEQ_TYPE_BLOCK;
1072 if (typeid == id_class) return ISEQ_TYPE_CLASS;
1073 if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
1074 if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
1075 if (typeid == id_eval) return ISEQ_TYPE_EVAL;
1076 if (typeid == id_main) return ISEQ_TYPE_MAIN;
1077 if (typeid == id_plain) return ISEQ_TYPE_PLAIN;
1078 return (enum rb_iseq_type)-1;
1079}
1080
1081static VALUE
1082iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
1083{
1084 rb_iseq_t *iseq = iseq_alloc();
1085
1086 VALUE magic, version1, version2, format_type, misc;
1087 VALUE name, path, realpath, code_location, node_id;
1088 VALUE type, body, locals, params, exception;
1089
1090 st_data_t iseq_type;
1091 rb_compile_option_t option;
1092 int i = 0;
1093 rb_code_location_t tmp_loc = { {0, 0}, {-1, -1} };
1094
1095 /* [magic, major_version, minor_version, format_type, misc,
1096 * label, path, first_lineno,
1097 * type, locals, args, exception_table, body]
1098 */
1099
1100 data = CHECK_ARRAY(data);
1101
1102 magic = CHECK_STRING(rb_ary_entry(data, i++));
1103 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
1104 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
1105 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
1106 misc = CHECK_HASH(rb_ary_entry(data, i++));
1107 ((void)magic, (void)version1, (void)version2, (void)format_type);
1108
1109 name = CHECK_STRING(rb_ary_entry(data, i++));
1110 path = CHECK_STRING(rb_ary_entry(data, i++));
1111 realpath = rb_ary_entry(data, i++);
1112 realpath = NIL_P(realpath) ? Qnil : CHECK_STRING(realpath);
1113 int first_lineno = RB_NUM2INT(rb_ary_entry(data, i++));
1114
1115 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
1116 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
1117 params = CHECK_HASH(rb_ary_entry(data, i++));
1118 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
1119 body = CHECK_ARRAY(rb_ary_entry(data, i++));
1120
1121 ISEQ_BODY(iseq)->local_iseq = iseq;
1122
1123 iseq_type = iseq_type_from_sym(type);
1124 if (iseq_type == (enum rb_iseq_type)-1) {
1125 rb_raise(rb_eTypeError, "unsupported type: :%"PRIsVALUE, rb_sym2str(type));
1126 }
1127
1128 node_id = rb_hash_aref(misc, ID2SYM(rb_intern("node_id")));
1129
1130 code_location = rb_hash_aref(misc, ID2SYM(rb_intern("code_location")));
1131 if (RB_TYPE_P(code_location, T_ARRAY) && RARRAY_LEN(code_location) == 4) {
1132 tmp_loc.beg_pos.lineno = NUM2INT(rb_ary_entry(code_location, 0));
1133 tmp_loc.beg_pos.column = NUM2INT(rb_ary_entry(code_location, 1));
1134 tmp_loc.end_pos.lineno = NUM2INT(rb_ary_entry(code_location, 2));
1135 tmp_loc.end_pos.column = NUM2INT(rb_ary_entry(code_location, 3));
1136 }
1137
1138 make_compile_option(&option, opt);
1139 option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
1140 prepare_iseq_build(iseq, name, path, realpath, first_lineno, &tmp_loc, NUM2INT(node_id),
1141 parent, 0, (enum rb_iseq_type)iseq_type, Qnil, &option);
1142
1143 rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
1144
1145 finish_iseq_build(iseq);
1146
1147 return iseqw_new(iseq);
1148}
1149
1150/*
1151 * :nodoc:
1152 */
1153static VALUE
1154iseq_s_load(int argc, VALUE *argv, VALUE self)
1155{
1156 VALUE data, opt=Qnil;
1157 rb_scan_args(argc, argv, "11", &data, &opt);
1158 return iseq_load(data, NULL, opt);
1159}
1160
1161VALUE
1162rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
1163{
1164 return iseq_load(data, RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
1165}
1166
1167static rb_iseq_t *
1168rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE realpath, VALUE line, VALUE opt)
1169{
1170 rb_iseq_t *iseq = NULL;
1171 rb_compile_option_t option;
1172#if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
1173# define INITIALIZED volatile /* suppress warnings by gcc 4.8 */
1174#else
1175# define INITIALIZED /* volatile */
1176#endif
1177 rb_ast_t *(*parse)(VALUE vparser, VALUE fname, VALUE file, int start);
1178 int ln;
1179 rb_ast_t *INITIALIZED ast;
1180 VALUE name = rb_fstring_lit("<compiled>");
1181
1182 /* safe results first */
1183 make_compile_option(&option, opt);
1184 ln = NUM2INT(line);
1185 StringValueCStr(file);
1186 if (RB_TYPE_P(src, T_FILE)) {
1187 parse = rb_parser_compile_file_path;
1188 }
1189 else {
1190 parse = rb_parser_compile_string_path;
1191 StringValue(src);
1192 }
1193 {
1194 const VALUE parser = rb_parser_new();
1195 const rb_iseq_t *outer_scope = rb_iseq_new(NULL, name, name, Qnil, 0, ISEQ_TYPE_TOP);
1196 VALUE outer_scope_v = (VALUE)outer_scope;
1197 rb_parser_set_context(parser, outer_scope, FALSE);
1198 RB_GC_GUARD(outer_scope_v);
1199 ast = (*parse)(parser, file, src, ln);
1200 }
1201
1202 if (!ast->body.root) {
1203 rb_ast_dispose(ast);
1204 rb_exc_raise(GET_EC()->errinfo);
1205 }
1206 else {
1207 iseq = rb_iseq_new_with_opt(&ast->body, name, file, realpath, ln,
1208 NULL, 0, ISEQ_TYPE_TOP, &option);
1209 rb_ast_dispose(ast);
1210 }
1211
1212 return iseq;
1213}
1214
1215VALUE
1216rb_iseq_path(const rb_iseq_t *iseq)
1217{
1218 return pathobj_path(ISEQ_BODY(iseq)->location.pathobj);
1219}
1220
1221VALUE
1222rb_iseq_realpath(const rb_iseq_t *iseq)
1223{
1224 return pathobj_realpath(ISEQ_BODY(iseq)->location.pathobj);
1225}
1226
1227VALUE
1228rb_iseq_absolute_path(const rb_iseq_t *iseq)
1229{
1230 return rb_iseq_realpath(iseq);
1231}
1232
1233int
1234rb_iseq_from_eval_p(const rb_iseq_t *iseq)
1235{
1236 return NIL_P(rb_iseq_realpath(iseq));
1237}
1238
1239VALUE
1240rb_iseq_label(const rb_iseq_t *iseq)
1241{
1242 return ISEQ_BODY(iseq)->location.label;
1243}
1244
1245VALUE
1246rb_iseq_base_label(const rb_iseq_t *iseq)
1247{
1248 return ISEQ_BODY(iseq)->location.base_label;
1249}
1250
1251VALUE
1252rb_iseq_first_lineno(const rb_iseq_t *iseq)
1253{
1254 return RB_INT2NUM(ISEQ_BODY(iseq)->location.first_lineno);
1255}
1256
1257VALUE
1258rb_iseq_method_name(const rb_iseq_t *iseq)
1259{
1260 struct rb_iseq_constant_body *const body = ISEQ_BODY(ISEQ_BODY(iseq)->local_iseq);
1261
1262 if (body->type == ISEQ_TYPE_METHOD) {
1263 return body->location.base_label;
1264 }
1265 else {
1266 return Qnil;
1267 }
1268}
1269
1270void
1271rb_iseq_code_location(const rb_iseq_t *iseq, int *beg_pos_lineno, int *beg_pos_column, int *end_pos_lineno, int *end_pos_column)
1272{
1273 const rb_code_location_t *loc = &ISEQ_BODY(iseq)->location.code_location;
1274 if (beg_pos_lineno) *beg_pos_lineno = loc->beg_pos.lineno;
1275 if (beg_pos_column) *beg_pos_column = loc->beg_pos.column;
1276 if (end_pos_lineno) *end_pos_lineno = loc->end_pos.lineno;
1277 if (end_pos_column) *end_pos_column = loc->end_pos.column;
1278}
1279
1280static ID iseq_type_id(enum rb_iseq_type type);
1281
1282VALUE
1283rb_iseq_type(const rb_iseq_t *iseq)
1284{
1285 return ID2SYM(iseq_type_id(ISEQ_BODY(iseq)->type));
1286}
1287
1288VALUE
1289rb_iseq_coverage(const rb_iseq_t *iseq)
1290{
1291 return ISEQ_COVERAGE(iseq);
1292}
1293
1294static int
1295remove_coverage_i(void *vstart, void *vend, size_t stride, void *data)
1296{
1297 VALUE v = (VALUE)vstart;
1298 for (; v != (VALUE)vend; v += stride) {
1299 void *ptr = asan_poisoned_object_p(v);
1300 asan_unpoison_object(v, false);
1301
1302 if (rb_obj_is_iseq(v)) {
1303 rb_iseq_t *iseq = (rb_iseq_t *)v;
1304 ISEQ_COVERAGE_SET(iseq, Qnil);
1305 }
1306
1307 asan_poison_object_if(ptr, v);
1308 }
1309 return 0;
1310}
1311
1312void
1313rb_iseq_remove_coverage_all(void)
1314{
1315 rb_objspace_each_objects(remove_coverage_i, NULL);
1316}
1317
1318/* define wrapper class methods (RubyVM::InstructionSequence) */
1319
1320static void
1321iseqw_mark(void *ptr)
1322{
1323 rb_gc_mark((VALUE)ptr);
1324}
1325
1326static size_t
1327iseqw_memsize(const void *ptr)
1328{
1329 return rb_iseq_memsize((const rb_iseq_t *)ptr);
1330}
1331
1332static const rb_data_type_t iseqw_data_type = {
1333 "T_IMEMO/iseq",
1334 {iseqw_mark, NULL, iseqw_memsize,},
1335 0, 0, RUBY_TYPED_FREE_IMMEDIATELY|RUBY_TYPED_WB_PROTECTED
1336};
1337
1338static VALUE
1339iseqw_new(const rb_iseq_t *iseq)
1340{
1341 if (iseq->wrapper) {
1342 return iseq->wrapper;
1343 }
1344 else {
1345 union { const rb_iseq_t *in; void *out; } deconst;
1346 VALUE obj;
1347 deconst.in = iseq;
1348 obj = TypedData_Wrap_Struct(rb_cISeq, &iseqw_data_type, deconst.out);
1349 RB_OBJ_WRITTEN(obj, Qundef, iseq);
1350
1351 /* cache a wrapper object */
1352 RB_OBJ_WRITE((VALUE)iseq, &iseq->wrapper, obj);
1353 RB_OBJ_FREEZE((VALUE)iseq);
1354
1355 return obj;
1356 }
1357}
1358
1359VALUE
1360rb_iseqw_new(const rb_iseq_t *iseq)
1361{
1362 return iseqw_new(iseq);
1363}
1364
1365/*
1366 * call-seq:
1367 * InstructionSequence.compile(source[, file[, path[, line[, options]]]]) -> iseq
1368 * InstructionSequence.new(source[, file[, path[, line[, options]]]]) -> iseq
1369 *
1370 * Takes +source+, a String of Ruby code and compiles it to an
1371 * InstructionSequence.
1372 *
1373 * Optionally takes +file+, +path+, and +line+ which describe the file path,
1374 * real path and first line number of the ruby code in +source+ which are
1375 * metadata attached to the returned +iseq+.
1376 *
1377 * +file+ is used for `__FILE__` and exception backtrace. +path+ is used for
1378 * +require_relative+ base. It is recommended these should be the same full
1379 * path.
1380 *
1381 * +options+, which can be +true+, +false+ or a +Hash+, is used to
1382 * modify the default behavior of the Ruby iseq compiler.
1383 *
1384 * For details regarding valid compile options see ::compile_option=.
1385 *
1386 * RubyVM::InstructionSequence.compile("a = 1 + 2")
1387 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1388 *
1389 * path = "test.rb"
1390 * RubyVM::InstructionSequence.compile(File.read(path), path, File.expand_path(path))
1391 * #=> <RubyVM::InstructionSequence:<compiled>@test.rb:1>
1392 *
1393 * path = File.expand_path("test.rb")
1394 * RubyVM::InstructionSequence.compile(File.read(path), path, path)
1395 * #=> <RubyVM::InstructionSequence:<compiled>@/absolute/path/to/test.rb:1>
1396 *
1397 */
1398static VALUE
1399iseqw_s_compile(int argc, VALUE *argv, VALUE self)
1400{
1401 VALUE src, file = Qnil, path = Qnil, line = INT2FIX(1), opt = Qnil;
1402 int i;
1403
1404 i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt);
1405 if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5);
1406 switch (i) {
1407 case 5: opt = argv[--i];
1408 case 4: line = argv[--i];
1409 case 3: path = argv[--i];
1410 case 2: file = argv[--i];
1411 }
1412
1413 if (NIL_P(file)) file = rb_fstring_lit("<compiled>");
1414 if (NIL_P(path)) path = file;
1415 if (NIL_P(line)) line = INT2FIX(1);
1416
1417 Check_Type(path, T_STRING);
1418 Check_Type(file, T_STRING);
1419
1420 return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, opt));
1421}
1422
1423/*
1424 * call-seq:
1425 * InstructionSequence.compile_file(file[, options]) -> iseq
1426 *
1427 * Takes +file+, a String with the location of a Ruby source file, reads,
1428 * parses and compiles the file, and returns +iseq+, the compiled
1429 * InstructionSequence with source location metadata set.
1430 *
1431 * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
1432 * modify the default behavior of the Ruby iseq compiler.
1433 *
1434 * For details regarding valid compile options see ::compile_option=.
1435 *
1436 * # /tmp/hello.rb
1437 * puts "Hello, world!"
1438 *
1439 * # elsewhere
1440 * RubyVM::InstructionSequence.compile_file("/tmp/hello.rb")
1441 * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
1442 */
1443static VALUE
1444iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
1445{
1446 VALUE file, opt = Qnil;
1447 VALUE parser, f, exc = Qnil, ret;
1448 rb_ast_t *ast;
1449 rb_compile_option_t option;
1450 int i;
1451
1452 i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
1453 if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
1454 switch (i) {
1455 case 2: opt = argv[--i];
1456 }
1457 FilePathValue(file);
1458 file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
1459
1460 f = rb_file_open_str(file, "r");
1461
1462 rb_execution_context_t *ec = GET_EC();
1463 VALUE v = rb_vm_push_frame_fname(ec, file);
1464
1465 parser = rb_parser_new();
1466 rb_parser_set_context(parser, NULL, FALSE);
1467 ast = (rb_ast_t *)rb_parser_load_file(parser, file);
1468 if (!ast->body.root) exc = GET_EC()->errinfo;
1469
1470 rb_io_close(f);
1471 if (!ast->body.root) {
1472 rb_ast_dispose(ast);
1473 rb_exc_raise(exc);
1474 }
1475
1476 make_compile_option(&option, opt);
1477
1478 ret = iseqw_new(rb_iseq_new_with_opt(&ast->body, rb_fstring_lit("<main>"),
1479 file,
1480 rb_realpath_internal(Qnil, file, 1),
1481 1, NULL, 0, ISEQ_TYPE_TOP, &option));
1482 rb_ast_dispose(ast);
1483
1484 rb_vm_pop_frame(ec);
1485 RB_GC_GUARD(v);
1486 return ret;
1487}
1488
1489/*
1490 * call-seq:
1491 * InstructionSequence.compile_option = options
1492 *
1493 * Sets the default values for various optimizations in the Ruby iseq
1494 * compiler.
1495 *
1496 * Possible values for +options+ include +true+, which enables all options,
1497 * +false+ which disables all options, and +nil+ which leaves all options
1498 * unchanged.
1499 *
1500 * You can also pass a +Hash+ of +options+ that you want to change, any
1501 * options not present in the hash will be left unchanged.
1502 *
1503 * Possible option names (which are keys in +options+) which can be set to
1504 * +true+ or +false+ include:
1505 *
1506 * * +:inline_const_cache+
1507 * * +:instructions_unification+
1508 * * +:operands_unification+
1509 * * +:peephole_optimization+
1510 * * +:specialized_instruction+
1511 * * +:stack_caching+
1512 * * +:tailcall_optimization+
1513 *
1514 * Additionally, +:debug_level+ can be set to an integer.
1515 *
1516 * These default options can be overwritten for a single run of the iseq
1517 * compiler by passing any of the above values as the +options+ parameter to
1518 * ::new, ::compile and ::compile_file.
1519 */
1520static VALUE
1521iseqw_s_compile_option_set(VALUE self, VALUE opt)
1522{
1523 rb_compile_option_t option;
1524 make_compile_option(&option, opt);
1525 COMPILE_OPTION_DEFAULT = option;
1526 return opt;
1527}
1528
1529/*
1530 * call-seq:
1531 * InstructionSequence.compile_option -> options
1532 *
1533 * Returns a hash of default options used by the Ruby iseq compiler.
1534 *
1535 * For details, see InstructionSequence.compile_option=.
1536 */
1537static VALUE
1538iseqw_s_compile_option_get(VALUE self)
1539{
1540 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
1541}
1542
1543static const rb_iseq_t *
1544iseqw_check(VALUE iseqw)
1545{
1546 rb_iseq_t *iseq = DATA_PTR(iseqw);
1547
1548 if (!ISEQ_BODY(iseq)) {
1549 rb_ibf_load_iseq_complete(iseq);
1550 }
1551
1552 if (!ISEQ_BODY(iseq)->location.label) {
1553 rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
1554 }
1555 return iseq;
1556}
1557
1558const rb_iseq_t *
1559rb_iseqw_to_iseq(VALUE iseqw)
1560{
1561 return iseqw_check(iseqw);
1562}
1563
1564/*
1565 * call-seq:
1566 * iseq.eval -> obj
1567 *
1568 * Evaluates the instruction sequence and returns the result.
1569 *
1570 * RubyVM::InstructionSequence.compile("1 + 2").eval #=> 3
1571 */
1572static VALUE
1573iseqw_eval(VALUE self)
1574{
1575 return rb_iseq_eval(iseqw_check(self));
1576}
1577
1578/*
1579 * Returns a human-readable string representation of this instruction
1580 * sequence, including the #label and #path.
1581 */
1582static VALUE
1583iseqw_inspect(VALUE self)
1584{
1585 const rb_iseq_t *iseq = iseqw_check(self);
1586 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1587 VALUE klass = rb_class_name(rb_obj_class(self));
1588
1589 if (!body->location.label) {
1590 return rb_sprintf("#<%"PRIsVALUE": uninitialized>", klass);
1591 }
1592 else {
1593 return rb_sprintf("<%"PRIsVALUE":%"PRIsVALUE"@%"PRIsVALUE":%d>",
1594 klass,
1595 body->location.label, rb_iseq_path(iseq),
1596 FIX2INT(rb_iseq_first_lineno(iseq)));
1597 }
1598}
1599
1600/*
1601 * Returns the path of this instruction sequence.
1602 *
1603 * <code><compiled></code> if the iseq was evaluated from a string.
1604 *
1605 * For example, using irb:
1606 *
1607 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1608 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1609 * iseq.path
1610 * #=> "<compiled>"
1611 *
1612 * Using ::compile_file:
1613 *
1614 * # /tmp/method.rb
1615 * def hello
1616 * puts "hello, world"
1617 * end
1618 *
1619 * # in irb
1620 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1621 * > iseq.path #=> /tmp/method.rb
1622 */
1623static VALUE
1624iseqw_path(VALUE self)
1625{
1626 return rb_iseq_path(iseqw_check(self));
1627}
1628
1629/*
1630 * Returns the absolute path of this instruction sequence.
1631 *
1632 * +nil+ if the iseq was evaluated from a string.
1633 *
1634 * For example, using ::compile_file:
1635 *
1636 * # /tmp/method.rb
1637 * def hello
1638 * puts "hello, world"
1639 * end
1640 *
1641 * # in irb
1642 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1643 * > iseq.absolute_path #=> /tmp/method.rb
1644 */
1645static VALUE
1646iseqw_absolute_path(VALUE self)
1647{
1648 return rb_iseq_realpath(iseqw_check(self));
1649}
1650
1651/* Returns the label of this instruction sequence.
1652 *
1653 * <code><main></code> if it's at the top level, <code><compiled></code> if it
1654 * was evaluated from a string.
1655 *
1656 * For example, using irb:
1657 *
1658 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1659 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1660 * iseq.label
1661 * #=> "<compiled>"
1662 *
1663 * Using ::compile_file:
1664 *
1665 * # /tmp/method.rb
1666 * def hello
1667 * puts "hello, world"
1668 * end
1669 *
1670 * # in irb
1671 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1672 * > iseq.label #=> <main>
1673 */
1674static VALUE
1675iseqw_label(VALUE self)
1676{
1677 return rb_iseq_label(iseqw_check(self));
1678}
1679
1680/* Returns the base label of this instruction sequence.
1681 *
1682 * For example, using irb:
1683 *
1684 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1685 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1686 * iseq.base_label
1687 * #=> "<compiled>"
1688 *
1689 * Using ::compile_file:
1690 *
1691 * # /tmp/method.rb
1692 * def hello
1693 * puts "hello, world"
1694 * end
1695 *
1696 * # in irb
1697 * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1698 * > iseq.base_label #=> <main>
1699 */
1700static VALUE
1701iseqw_base_label(VALUE self)
1702{
1703 return rb_iseq_base_label(iseqw_check(self));
1704}
1705
1706/* Returns the number of the first source line where the instruction sequence
1707 * was loaded from.
1708 *
1709 * For example, using irb:
1710 *
1711 * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1712 * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1713 * iseq.first_lineno
1714 * #=> 1
1715 */
1716static VALUE
1717iseqw_first_lineno(VALUE self)
1718{
1719 return rb_iseq_first_lineno(iseqw_check(self));
1720}
1721
1722static VALUE iseq_data_to_ary(const rb_iseq_t *iseq);
1723
1724/*
1725 * call-seq:
1726 * iseq.to_a -> ary
1727 *
1728 * Returns an Array with 14 elements representing the instruction sequence
1729 * with the following data:
1730 *
1731 * [magic]
1732 * A string identifying the data format. <b>Always
1733 * +YARVInstructionSequence/SimpleDataFormat+.</b>
1734 *
1735 * [major_version]
1736 * The major version of the instruction sequence.
1737 *
1738 * [minor_version]
1739 * The minor version of the instruction sequence.
1740 *
1741 * [format_type]
1742 * A number identifying the data format. <b>Always 1</b>.
1743 *
1744 * [misc]
1745 * A hash containing:
1746 *
1747 * [+:arg_size+]
1748 * the total number of arguments taken by the method or the block (0 if
1749 * _iseq_ doesn't represent a method or block)
1750 * [+:local_size+]
1751 * the number of local variables + 1
1752 * [+:stack_max+]
1753 * used in calculating the stack depth at which a SystemStackError is
1754 * thrown.
1755 *
1756 * [#label]
1757 * The name of the context (block, method, class, module, etc.) that this
1758 * instruction sequence belongs to.
1759 *
1760 * <code><main></code> if it's at the top level, <code><compiled></code> if
1761 * it was evaluated from a string.
1762 *
1763 * [#path]
1764 * The relative path to the Ruby file where the instruction sequence was
1765 * loaded from.
1766 *
1767 * <code><compiled></code> if the iseq was evaluated from a string.
1768 *
1769 * [#absolute_path]
1770 * The absolute path to the Ruby file where the instruction sequence was
1771 * loaded from.
1772 *
1773 * +nil+ if the iseq was evaluated from a string.
1774 *
1775 * [#first_lineno]
1776 * The number of the first source line where the instruction sequence was
1777 * loaded from.
1778 *
1779 * [type]
1780 * The type of the instruction sequence.
1781 *
1782 * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
1783 * +:ensure+, +:eval+, +:main+, and +plain+.
1784 *
1785 * [locals]
1786 * An array containing the names of all arguments and local variables as
1787 * symbols.
1788 *
1789 * [params]
1790 * An Hash object containing parameter information.
1791 *
1792 * More info about these values can be found in +vm_core.h+.
1793 *
1794 * [catch_table]
1795 * A list of exceptions and control flow operators (rescue, next, redo,
1796 * break, etc.).
1797 *
1798 * [bytecode]
1799 * An array of arrays containing the instruction names and operands that
1800 * make up the body of the instruction sequence.
1801 *
1802 * Note that this format is MRI specific and version dependent.
1803 *
1804 */
1805static VALUE
1806iseqw_to_a(VALUE self)
1807{
1808 const rb_iseq_t *iseq = iseqw_check(self);
1809 return iseq_data_to_ary(iseq);
1810}
1811
1812#if VM_INSN_INFO_TABLE_IMPL == 1 /* binary search */
1813static const struct iseq_insn_info_entry *
1814get_insn_info_binary_search(const rb_iseq_t *iseq, size_t pos)
1815{
1816 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1817 size_t size = body->insns_info.size;
1818 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1819 const unsigned int *positions = body->insns_info.positions;
1820 const int debug = 0;
1821
1822 if (debug) {
1823 printf("size: %"PRIuSIZE"\n", size);
1824 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1825 (size_t)0, positions[0], insns_info[0].line_no, pos);
1826 }
1827
1828 if (size == 0) {
1829 return NULL;
1830 }
1831 else if (size == 1) {
1832 return &insns_info[0];
1833 }
1834 else {
1835 size_t l = 1, r = size - 1;
1836 while (l <= r) {
1837 size_t m = l + (r - l) / 2;
1838 if (positions[m] == pos) {
1839 return &insns_info[m];
1840 }
1841 if (positions[m] < pos) {
1842 l = m + 1;
1843 }
1844 else {
1845 r = m - 1;
1846 }
1847 }
1848 if (l >= size) {
1849 return &insns_info[size-1];
1850 }
1851 if (positions[l] > pos) {
1852 return &insns_info[l-1];
1853 }
1854 return &insns_info[l];
1855 }
1856}
1857
1858static const struct iseq_insn_info_entry *
1859get_insn_info(const rb_iseq_t *iseq, size_t pos)
1860{
1861 return get_insn_info_binary_search(iseq, pos);
1862}
1863#endif
1864
1865#if VM_INSN_INFO_TABLE_IMPL == 2 /* succinct bitvector */
1866static const struct iseq_insn_info_entry *
1867get_insn_info_succinct_bitvector(const rb_iseq_t *iseq, size_t pos)
1868{
1869 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1870 size_t size = body->insns_info.size;
1871 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1872 const int debug = 0;
1873
1874 if (debug) {
1875#if VM_CHECK_MODE > 0
1876 const unsigned int *positions = body->insns_info.positions;
1877 printf("size: %"PRIuSIZE"\n", size);
1878 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1879 (size_t)0, positions[0], insns_info[0].line_no, pos);
1880#else
1881 printf("size: %"PRIuSIZE"\n", size);
1882 printf("insns_info[%"PRIuSIZE"]: line: %d, pos: %"PRIuSIZE"\n",
1883 (size_t)0, insns_info[0].line_no, pos);
1884#endif
1885 }
1886
1887 if (size == 0) {
1888 return NULL;
1889 }
1890 else if (size == 1) {
1891 return &insns_info[0];
1892 }
1893 else {
1894 int index;
1895 VM_ASSERT(body->insns_info.succ_index_table != NULL);
1896 index = succ_index_lookup(body->insns_info.succ_index_table, (int)pos);
1897 return &insns_info[index-1];
1898 }
1899}
1900
1901static const struct iseq_insn_info_entry *
1902get_insn_info(const rb_iseq_t *iseq, size_t pos)
1903{
1904 return get_insn_info_succinct_bitvector(iseq, pos);
1905}
1906#endif
1907
1908#if VM_CHECK_MODE > 0 || VM_INSN_INFO_TABLE_IMPL == 0
1909static const struct iseq_insn_info_entry *
1910get_insn_info_linear_search(const rb_iseq_t *iseq, size_t pos)
1911{
1912 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1913 size_t i = 0, size = body->insns_info.size;
1914 const struct iseq_insn_info_entry *insns_info = body->insns_info.body;
1915 const unsigned int *positions = body->insns_info.positions;
1916 const int debug = 0;
1917
1918 if (debug) {
1919 printf("size: %"PRIuSIZE"\n", size);
1920 printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1921 i, positions[i], insns_info[i].line_no, pos);
1922 }
1923
1924 if (size == 0) {
1925 return NULL;
1926 }
1927 else if (size == 1) {
1928 return &insns_info[0];
1929 }
1930 else {
1931 for (i=1; i<size; i++) {
1932 if (debug) printf("insns_info[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1933 i, positions[i], insns_info[i].line_no, pos);
1934
1935 if (positions[i] == pos) {
1936 return &insns_info[i];
1937 }
1938 if (positions[i] > pos) {
1939 return &insns_info[i-1];
1940 }
1941 }
1942 }
1943 return &insns_info[i-1];
1944}
1945#endif
1946
1947#if VM_INSN_INFO_TABLE_IMPL == 0 /* linear search */
1948static const struct iseq_insn_info_entry *
1949get_insn_info(const rb_iseq_t *iseq, size_t pos)
1950{
1951 return get_insn_info_linear_search(iseq, pos);
1952}
1953#endif
1954
1955#if VM_CHECK_MODE > 0 && VM_INSN_INFO_TABLE_IMPL > 0
1956static void
1957validate_get_insn_info(const rb_iseq_t *iseq)
1958{
1959 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
1960 size_t i;
1961 for (i = 0; i < body->iseq_size; i++) {
1962 if (get_insn_info_linear_search(iseq, i) != get_insn_info(iseq, i)) {
1963 rb_bug("validate_get_insn_info: get_insn_info_linear_search(iseq, %"PRIuSIZE") != get_insn_info(iseq, %"PRIuSIZE")", i, i);
1964 }
1965 }
1966}
1967#endif
1968
1969unsigned int
1970rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
1971{
1972 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
1973
1974 if (entry) {
1975 return entry->line_no;
1976 }
1977 else {
1978 return 0;
1979 }
1980}
1981
1982#ifdef USE_ISEQ_NODE_ID
1983int
1984rb_iseq_node_id(const rb_iseq_t *iseq, size_t pos)
1985{
1986 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
1987
1988 if (entry) {
1989 return entry->node_id;
1990 }
1991 else {
1992 return 0;
1993 }
1994}
1995#endif
1996
1997MJIT_FUNC_EXPORTED rb_event_flag_t
1998rb_iseq_event_flags(const rb_iseq_t *iseq, size_t pos)
1999{
2000 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
2001 if (entry) {
2002 return entry->events;
2003 }
2004 else {
2005 return 0;
2006 }
2007}
2008
2009void
2010rb_iseq_clear_event_flags(const rb_iseq_t *iseq, size_t pos, rb_event_flag_t reset)
2011{
2012 struct iseq_insn_info_entry *entry = (struct iseq_insn_info_entry *)get_insn_info(iseq, pos);
2013 if (entry) {
2014 entry->events &= ~reset;
2015 if (!(entry->events & iseq->aux.exec.global_trace_events)) {
2016 void rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos);
2017 rb_iseq_trace_flag_cleared(iseq, pos);
2018 }
2019 }
2020}
2021
2022static VALUE
2023local_var_name(const rb_iseq_t *diseq, VALUE level, VALUE op)
2024{
2025 VALUE i;
2026 VALUE name;
2027 ID lid;
2028 int idx;
2029
2030 for (i = 0; i < level; i++) {
2031 diseq = ISEQ_BODY(diseq)->parent_iseq;
2032 }
2033 idx = ISEQ_BODY(diseq)->local_table_size - (int)op - 1;
2034 lid = ISEQ_BODY(diseq)->local_table[idx];
2035 name = rb_id2str(lid);
2036 if (!name) {
2037 name = rb_str_new_cstr("?");
2038 }
2039 else if (!rb_str_symname_p(name)) {
2040 name = rb_str_inspect(name);
2041 }
2042 else {
2043 name = rb_str_dup(name);
2044 }
2045 rb_str_catf(name, "@%d", idx);
2046 return name;
2047}
2048
2049int rb_insn_unified_local_var_level(VALUE);
2050VALUE rb_dump_literal(VALUE lit);
2051
2052VALUE
2053rb_insn_operand_intern(const rb_iseq_t *iseq,
2054 VALUE insn, int op_no, VALUE op,
2055 int len, size_t pos, const VALUE *pnop, VALUE child)
2056{
2057 const char *types = insn_op_types(insn);
2058 char type = types[op_no];
2059 VALUE ret = Qundef;
2060
2061 switch (type) {
2062 case TS_OFFSET: /* LONG */
2063 ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
2064 break;
2065
2066 case TS_NUM: /* ULONG */
2067 if (insn == BIN(defined) && op_no == 0) {
2068 enum defined_type deftype = (enum defined_type)op;
2069 switch (deftype) {
2070 case DEFINED_FUNC:
2071 ret = rb_fstring_lit("func");
2072 break;
2073 case DEFINED_REF:
2074 ret = rb_fstring_lit("ref");
2075 break;
2076 case DEFINED_CONST_FROM:
2077 ret = rb_fstring_lit("constant-from");
2078 break;
2079 default:
2080 ret = rb_iseq_defined_string(deftype);
2081 break;
2082 }
2083 if (ret) break;
2084 }
2085 else if (insn == BIN(checktype) && op_no == 0) {
2086 const char *type_str = rb_type_str((enum ruby_value_type)op);
2087 if (type_str) {
2088 ret = rb_str_new_cstr(type_str); break;
2089 }
2090 }
2091 ret = rb_sprintf("%"PRIuVALUE, op);
2092 break;
2093
2094 case TS_LINDEX:{
2095 int level;
2096 if (types[op_no+1] == TS_NUM && pnop) {
2097 ret = local_var_name(iseq, *pnop, op - VM_ENV_DATA_SIZE);
2098 }
2099 else if ((level = rb_insn_unified_local_var_level(insn)) >= 0) {
2100 ret = local_var_name(iseq, (VALUE)level, op - VM_ENV_DATA_SIZE);
2101 }
2102 else {
2103 ret = rb_inspect(INT2FIX(op));
2104 }
2105 break;
2106 }
2107 case TS_ID: /* ID (symbol) */
2108 ret = rb_inspect(ID2SYM(op));
2109 break;
2110
2111 case TS_VALUE: /* VALUE */
2112 op = obj_resurrect(op);
2113 if (insn == BIN(defined) && op_no == 1 && FIXNUM_P(op)) {
2114 /* should be DEFINED_REF */
2115 int type = NUM2INT(op);
2116 if (type) {
2117 if (type & 1) {
2118 ret = rb_sprintf(":$%c", (type >> 1));
2119 }
2120 else {
2121 ret = rb_sprintf(":$%d", (type >> 1));
2122 }
2123 break;
2124 }
2125 }
2126 ret = rb_dump_literal(op);
2127 if (CLASS_OF(op) == rb_cISeq) {
2128 if (child) {
2129 rb_ary_push(child, op);
2130 }
2131 }
2132 break;
2133
2134 case TS_ISEQ: /* iseq */
2135 {
2136 if (op) {
2137 const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
2138 ret = ISEQ_BODY(iseq)->location.label;
2139 if (child) {
2140 rb_ary_push(child, (VALUE)iseq);
2141 }
2142 }
2143 else {
2144 ret = rb_str_new2("nil");
2145 }
2146 break;
2147 }
2148
2149 case TS_IC:
2150 {
2151 ret = rb_sprintf("<ic:%"PRIdPTRDIFF" ", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2152 const ID *segments = ((IC)op)->segments;
2153 rb_str_cat2(ret, rb_id2name(*segments++));
2154 while (*segments) {
2155 rb_str_catf(ret, "::%s", rb_id2name(*segments++));
2156 }
2157 rb_str_cat2(ret, ">");
2158 }
2159 break;
2160 case TS_IVC:
2161 case TS_ICVARC:
2162 case TS_ISE:
2163 ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - ISEQ_BODY(iseq)->is_entries);
2164 break;
2165
2166 case TS_CALLDATA:
2167 {
2168 struct rb_call_data *cd = (struct rb_call_data *)op;
2169 const struct rb_callinfo *ci = cd->ci;
2170 VALUE ary = rb_ary_new();
2171 ID mid = vm_ci_mid(ci);
2172
2173 if (mid) {
2174 rb_ary_push(ary, rb_sprintf("mid:%"PRIsVALUE, rb_id2str(mid)));
2175 }
2176
2177 rb_ary_push(ary, rb_sprintf("argc:%d", vm_ci_argc(ci)));
2178
2179 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
2180 const struct rb_callinfo_kwarg *kw_args = vm_ci_kwarg(ci);
2181 VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
2182 rb_ary_push(ary, rb_sprintf("kw:[%"PRIsVALUE"]", rb_ary_join(kw_ary, rb_str_new2(","))));
2183 }
2184
2185 if (vm_ci_flag(ci)) {
2186 VALUE flags = rb_ary_new();
2187# define CALL_FLAG(n) if (vm_ci_flag(ci) & VM_CALL_##n) rb_ary_push(flags, rb_str_new2(#n))
2188 CALL_FLAG(ARGS_SPLAT);
2189 CALL_FLAG(ARGS_BLOCKARG);
2190 CALL_FLAG(FCALL);
2191 CALL_FLAG(VCALL);
2192 CALL_FLAG(ARGS_SIMPLE);
2193 CALL_FLAG(BLOCKISEQ);
2194 CALL_FLAG(TAILCALL);
2195 CALL_FLAG(SUPER);
2196 CALL_FLAG(ZSUPER);
2197 CALL_FLAG(KWARG);
2198 CALL_FLAG(KW_SPLAT);
2199 CALL_FLAG(KW_SPLAT_MUT);
2200 CALL_FLAG(OPT_SEND); /* maybe not reachable */
2201 rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
2202 }
2203
2204 ret = rb_sprintf("<calldata!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
2205 }
2206 break;
2207
2208 case TS_CDHASH:
2209 ret = rb_str_new2("<cdhash>");
2210 break;
2211
2212 case TS_FUNCPTR:
2213 {
2214#ifdef HAVE_DLADDR
2215 Dl_info info;
2216 if (dladdr((void *)op, &info) && info.dli_sname) {
2217 ret = rb_str_new_cstr(info.dli_sname);
2218 break;
2219 }
2220#endif
2221 ret = rb_str_new2("<funcptr>");
2222 }
2223 break;
2224
2225 case TS_BUILTIN:
2226 {
2227 const struct rb_builtin_function *bf = (const struct rb_builtin_function *)op;
2228 ret = rb_sprintf("<builtin!%s/%d>",
2229 bf->name, bf->argc);
2230 }
2231 break;
2232
2233 default:
2234 rb_bug("unknown operand type: %c", type);
2235 }
2236 return ret;
2237}
2238
2239static VALUE
2240right_strip(VALUE str)
2241{
2242 const char *beg = RSTRING_PTR(str), *end = RSTRING_END(str);
2243 while (end-- > beg && *end == ' ');
2244 rb_str_set_len(str, end - beg + 1);
2245 return str;
2246}
2247
2252int
2253rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
2254 const rb_iseq_t *iseq, VALUE child)
2255{
2256 VALUE insn = code[pos];
2257 int len = insn_len(insn);
2258 int j;
2259 const char *types = insn_op_types(insn);
2260 VALUE str = rb_str_new(0, 0);
2261 const char *insn_name_buff;
2262
2263 insn_name_buff = insn_name(insn);
2264 if (1) {
2265 extern const int rb_vm_max_insn_name_size;
2266 rb_str_catf(str, "%04"PRIuSIZE" %-*s ", pos, rb_vm_max_insn_name_size, insn_name_buff);
2267 }
2268 else {
2269 rb_str_catf(str, "%04"PRIuSIZE" %-28.*s ", pos,
2270 (int)strcspn(insn_name_buff, "_"), insn_name_buff);
2271 }
2272
2273 for (j = 0; types[j]; j++) {
2274 VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
2275 len, pos, &code[pos + j + 2],
2276 child);
2277 rb_str_concat(str, opstr);
2278
2279 if (types[j + 1]) {
2280 rb_str_cat2(str, ", ");
2281 }
2282 }
2283
2284 {
2285 unsigned int line_no = rb_iseq_line_no(iseq, pos);
2286 unsigned int prev = pos == 0 ? 0 : rb_iseq_line_no(iseq, pos - 1);
2287 if (line_no && line_no != prev) {
2288 long slen = RSTRING_LEN(str);
2289 slen = (slen > 70) ? 0 : (70 - slen);
2290 str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
2291 }
2292 }
2293
2294 {
2295 rb_event_flag_t events = rb_iseq_event_flags(iseq, pos);
2296 if (events) {
2297 str = rb_str_catf(str, "[%s%s%s%s%s%s%s%s%s%s%s]",
2298 events & RUBY_EVENT_LINE ? "Li" : "",
2299 events & RUBY_EVENT_CLASS ? "Cl" : "",
2300 events & RUBY_EVENT_END ? "En" : "",
2301 events & RUBY_EVENT_CALL ? "Ca" : "",
2302 events & RUBY_EVENT_RETURN ? "Re" : "",
2303 events & RUBY_EVENT_C_CALL ? "Cc" : "",
2304 events & RUBY_EVENT_C_RETURN ? "Cr" : "",
2305 events & RUBY_EVENT_B_CALL ? "Bc" : "",
2306 events & RUBY_EVENT_B_RETURN ? "Br" : "",
2307 events & RUBY_EVENT_COVERAGE_LINE ? "Cli" : "",
2308 events & RUBY_EVENT_COVERAGE_BRANCH ? "Cbr" : "");
2309 }
2310 }
2311
2312 right_strip(str);
2313 if (ret) {
2314 rb_str_cat2(str, "\n");
2315 rb_str_concat(ret, str);
2316 }
2317 else {
2318 printf("%.*s\n", (int)RSTRING_LEN(str), RSTRING_PTR(str));
2319 }
2320 return len;
2321}
2322
2323static const char *
2324catch_type(int type)
2325{
2326 switch (type) {
2327 case CATCH_TYPE_RESCUE:
2328 return "rescue";
2329 case CATCH_TYPE_ENSURE:
2330 return "ensure";
2331 case CATCH_TYPE_RETRY:
2332 return "retry";
2333 case CATCH_TYPE_BREAK:
2334 return "break";
2335 case CATCH_TYPE_REDO:
2336 return "redo";
2337 case CATCH_TYPE_NEXT:
2338 return "next";
2339 default:
2340 rb_bug("unknown catch type: %d", type);
2341 return 0;
2342 }
2343}
2344
2345static VALUE
2346iseq_inspect(const rb_iseq_t *iseq)
2347{
2348 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2349 if (!body->location.label) {
2350 return rb_sprintf("#<ISeq: uninitialized>");
2351 }
2352 else {
2353 const rb_code_location_t *loc = &body->location.code_location;
2354 return rb_sprintf("#<ISeq:%"PRIsVALUE"@%"PRIsVALUE":%d (%d,%d)-(%d,%d)>",
2355 body->location.label, rb_iseq_path(iseq),
2356 loc->beg_pos.lineno,
2357 loc->beg_pos.lineno,
2358 loc->beg_pos.column,
2359 loc->end_pos.lineno,
2360 loc->end_pos.column);
2361 }
2362}
2363
2364static const rb_data_type_t tmp_set = {
2365 "tmpset",
2366 {(void (*)(void *))rb_mark_set, (void (*)(void *))st_free_table, 0, 0,},
2367 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2368};
2369
2370static VALUE
2371rb_iseq_disasm_recursive(const rb_iseq_t *iseq, VALUE indent)
2372{
2373 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2374 VALUE *code;
2375 VALUE str = rb_str_new(0, 0);
2376 VALUE child = rb_ary_hidden_new(3);
2377 unsigned int size;
2378 unsigned int i;
2379 long l;
2380 size_t n;
2381 enum {header_minlen = 72};
2382 st_table *done_iseq = 0;
2383 VALUE done_iseq_wrapper = Qnil;
2384 const char *indent_str;
2385 long indent_len;
2386
2387 size = body->iseq_size;
2388
2389 indent_len = RSTRING_LEN(indent);
2390 indent_str = RSTRING_PTR(indent);
2391
2392 rb_str_cat(str, indent_str, indent_len);
2393 rb_str_cat2(str, "== disasm: ");
2394
2395 rb_str_append(str, iseq_inspect(iseq));
2396 rb_str_catf(str, " (catch: %s)", body->catch_except_p ? "true" : "false");
2397 if ((l = RSTRING_LEN(str) - indent_len) < header_minlen) {
2398 rb_str_modify_expand(str, header_minlen - l);
2399 memset(RSTRING_END(str), '=', header_minlen - l);
2400 }
2401 rb_str_cat2(str, "\n");
2402
2403 /* show catch table information */
2404 if (body->catch_table) {
2405 rb_str_cat(str, indent_str, indent_len);
2406 rb_str_cat2(str, "== catch table\n");
2407 }
2408 if (body->catch_table) {
2409 rb_str_cat_cstr(indent, "| ");
2410 indent_str = RSTRING_PTR(indent);
2411 for (i = 0; i < body->catch_table->size; i++) {
2412 const struct iseq_catch_table_entry *entry =
2413 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2414 rb_str_cat(str, indent_str, indent_len);
2415 rb_str_catf(str,
2416 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
2417 catch_type((int)entry->type), (int)entry->start,
2418 (int)entry->end, (int)entry->sp, (int)entry->cont);
2419 if (entry->iseq && !(done_iseq && st_is_member(done_iseq, (st_data_t)entry->iseq))) {
2420 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check(entry->iseq), indent));
2421 if (!done_iseq) {
2422 done_iseq = st_init_numtable();
2423 done_iseq_wrapper = TypedData_Wrap_Struct(0, &tmp_set, done_iseq);
2424 }
2425 st_insert(done_iseq, (st_data_t)entry->iseq, (st_data_t)0);
2426 indent_str = RSTRING_PTR(indent);
2427 }
2428 }
2429 rb_str_resize(indent, indent_len);
2430 indent_str = RSTRING_PTR(indent);
2431 }
2432 if (body->catch_table) {
2433 rb_str_cat(str, indent_str, indent_len);
2434 rb_str_cat2(str, "|-------------------------------------"
2435 "-----------------------------------\n");
2436 }
2437
2438 /* show local table information */
2439 if (body->local_table) {
2440 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
2441 rb_str_cat(str, indent_str, indent_len);
2442 rb_str_catf(str,
2443 "local table (size: %d, argc: %d "
2444 "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
2445 body->local_table_size,
2446 body->param.lead_num,
2447 body->param.opt_num,
2448 body->param.flags.has_rest ? body->param.rest_start : -1,
2449 body->param.post_num,
2450 body->param.flags.has_block ? body->param.block_start : -1,
2451 body->param.flags.has_kw ? keyword->num : -1,
2452 body->param.flags.has_kw ? keyword->required_num : -1,
2453 body->param.flags.has_kwrest ? keyword->rest_start : -1);
2454
2455 for (i = body->local_table_size; i > 0;) {
2456 int li = body->local_table_size - --i - 1;
2457 long width;
2458 VALUE name = local_var_name(iseq, 0, i);
2459 char argi[0x100];
2460 char opti[0x100];
2461
2462 opti[0] = '\0';
2463 if (body->param.flags.has_opt) {
2464 int argc = body->param.lead_num;
2465 int opts = body->param.opt_num;
2466 if (li >= argc && li < argc + opts) {
2467 snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
2468 body->param.opt_table[li - argc]);
2469 }
2470 }
2471
2472 snprintf(argi, sizeof(argi), "%s%s%s%s%s%s", /* arg, opts, rest, post, kwrest, block */
2473 body->param.lead_num > li ? "Arg" : "",
2474 opti,
2475 (body->param.flags.has_rest && body->param.rest_start == li) ? "Rest" : "",
2476 (body->param.flags.has_post && body->param.post_start <= li && li < body->param.post_start + body->param.post_num) ? "Post" : "",
2477 (body->param.flags.has_kwrest && keyword->rest_start == li) ? "Kwrest" : "",
2478 (body->param.flags.has_block && body->param.block_start == li) ? "Block" : "");
2479
2480 rb_str_cat(str, indent_str, indent_len);
2481 rb_str_catf(str, "[%2d] ", i + 1);
2482 width = RSTRING_LEN(str) + 11;
2483 rb_str_append(str, name);
2484 if (*argi) rb_str_catf(str, "<%s>", argi);
2485 if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
2486 }
2487 rb_str_cat_cstr(right_strip(str), "\n");
2488 }
2489
2490 /* show each line */
2491 code = rb_iseq_original_iseq(iseq);
2492 for (n = 0; n < size;) {
2493 rb_str_cat(str, indent_str, indent_len);
2494 n += rb_iseq_disasm_insn(str, code, n, iseq, child);
2495 }
2496
2497 for (l = 0; l < RARRAY_LEN(child); l++) {
2498 VALUE isv = rb_ary_entry(child, l);
2499 if (done_iseq && st_is_member(done_iseq, (st_data_t)isv)) continue;
2500 rb_str_cat_cstr(str, "\n");
2501 rb_str_concat(str, rb_iseq_disasm_recursive(rb_iseq_check((rb_iseq_t *)isv), indent));
2502 indent_str = RSTRING_PTR(indent);
2503 }
2504 RB_GC_GUARD(done_iseq_wrapper);
2505
2506 return str;
2507}
2508
2509VALUE
2510rb_iseq_disasm(const rb_iseq_t *iseq)
2511{
2512 VALUE str = rb_iseq_disasm_recursive(iseq, rb_str_new(0, 0));
2513 rb_str_resize(str, RSTRING_LEN(str));
2514 return str;
2515}
2516
2517/*
2518 * Estimates the number of instance variables that will be set on
2519 * a given `class` with the initialize method defined in
2520 * `initialize_iseq`
2521 */
2522attr_index_t
2523rb_estimate_iv_count(VALUE klass, const rb_iseq_t * initialize_iseq)
2524{
2525 struct rb_id_table * iv_names = rb_id_table_create(0);
2526
2527 for (unsigned int i = 0; i < ISEQ_BODY(initialize_iseq)->ivc_size; i++) {
2528 IVC cache = (IVC)&ISEQ_BODY(initialize_iseq)->is_entries[i];
2529
2530 if (cache->iv_set_name) {
2531 rb_id_table_insert(iv_names, cache->iv_set_name, Qtrue);
2532 }
2533 }
2534
2535 attr_index_t count = (attr_index_t)rb_id_table_size(iv_names);
2536
2537 VALUE superclass = rb_class_superclass(klass);
2538 count += RCLASS_EXT(superclass)->max_iv_count;
2539
2540 rb_id_table_free(iv_names);
2541
2542 return count;
2543}
2544
2545/*
2546 * call-seq:
2547 * iseq.disasm -> str
2548 * iseq.disassemble -> str
2549 *
2550 * Returns the instruction sequence as a +String+ in human readable form.
2551 *
2552 * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
2553 *
2554 * Produces:
2555 *
2556 * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
2557 * 0000 trace 1 ( 1)
2558 * 0002 putobject 1
2559 * 0004 putobject 2
2560 * 0006 opt_plus <ic:1>
2561 * 0008 leave
2562 */
2563static VALUE
2564iseqw_disasm(VALUE self)
2565{
2566 return rb_iseq_disasm(iseqw_check(self));
2567}
2568
2569static int
2570iseq_iterate_children(const rb_iseq_t *iseq, void (*iter_func)(const rb_iseq_t *child_iseq, void *data), void *data)
2571{
2572 unsigned int i;
2573 VALUE *code = rb_iseq_original_iseq(iseq);
2574 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2575 const rb_iseq_t *child;
2576 VALUE all_children = rb_obj_hide(rb_ident_hash_new());
2577
2578 if (body->catch_table) {
2579 for (i = 0; i < body->catch_table->size; i++) {
2580 const struct iseq_catch_table_entry *entry =
2581 UNALIGNED_MEMBER_PTR(body->catch_table, entries[i]);
2582 child = entry->iseq;
2583 if (child) {
2584 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2585 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2586 (*iter_func)(child, data);
2587 }
2588 }
2589 }
2590 }
2591
2592 for (i=0; i<body->iseq_size;) {
2593 VALUE insn = code[i];
2594 int len = insn_len(insn);
2595 const char *types = insn_op_types(insn);
2596 int j;
2597
2598 for (j=0; types[j]; j++) {
2599 switch (types[j]) {
2600 case TS_ISEQ:
2601 child = (const rb_iseq_t *)code[i+j+1];
2602 if (child) {
2603 if (NIL_P(rb_hash_aref(all_children, (VALUE)child))) {
2604 rb_hash_aset(all_children, (VALUE)child, Qtrue);
2605 (*iter_func)(child, data);
2606 }
2607 }
2608 break;
2609 default:
2610 break;
2611 }
2612 }
2613 i += len;
2614 }
2615
2616 return (int)RHASH_SIZE(all_children);
2617}
2618
2619static void
2620yield_each_children(const rb_iseq_t *child_iseq, void *data)
2621{
2622 rb_yield(iseqw_new(child_iseq));
2623}
2624
2625/*
2626 * call-seq:
2627 * iseq.each_child{|child_iseq| ...} -> iseq
2628 *
2629 * Iterate all direct child instruction sequences.
2630 * Iteration order is implementation/version defined
2631 * so that people should not rely on the order.
2632 */
2633static VALUE
2634iseqw_each_child(VALUE self)
2635{
2636 const rb_iseq_t *iseq = iseqw_check(self);
2637 iseq_iterate_children(iseq, yield_each_children, NULL);
2638 return self;
2639}
2640
2641static void
2642push_event_info(const rb_iseq_t *iseq, rb_event_flag_t events, int line, VALUE ary)
2643{
2644#define C(ev, cstr, l) if (events & ev) rb_ary_push(ary, rb_ary_new_from_args(2, l, ID2SYM(rb_intern(cstr))));
2645 C(RUBY_EVENT_CLASS, "class", rb_iseq_first_lineno(iseq));
2646 C(RUBY_EVENT_CALL, "call", rb_iseq_first_lineno(iseq));
2647 C(RUBY_EVENT_B_CALL, "b_call", rb_iseq_first_lineno(iseq));
2648 C(RUBY_EVENT_LINE, "line", INT2FIX(line));
2649 C(RUBY_EVENT_END, "end", INT2FIX(line));
2650 C(RUBY_EVENT_RETURN, "return", INT2FIX(line));
2651 C(RUBY_EVENT_B_RETURN, "b_return", INT2FIX(line));
2652#undef C
2653}
2654
2655/*
2656 * call-seq:
2657 * iseq.trace_points -> ary
2658 *
2659 * Return trace points in the instruction sequence.
2660 * Return an array of [line, event_symbol] pair.
2661 */
2662static VALUE
2663iseqw_trace_points(VALUE self)
2664{
2665 const rb_iseq_t *iseq = iseqw_check(self);
2666 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
2667 unsigned int i;
2668 VALUE ary = rb_ary_new();
2669
2670 for (i=0; i<body->insns_info.size; i++) {
2671 const struct iseq_insn_info_entry *entry = &body->insns_info.body[i];
2672 if (entry->events) {
2673 push_event_info(iseq, entry->events, entry->line_no, ary);
2674 }
2675 }
2676 return ary;
2677}
2678
2679/*
2680 * Returns the instruction sequence containing the given proc or method.
2681 *
2682 * For example, using irb:
2683 *
2684 * # a proc
2685 * > p = proc { num = 1 + 2 }
2686 * > RubyVM::InstructionSequence.of(p)
2687 * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
2688 *
2689 * # for a method
2690 * > def foo(bar); puts bar; end
2691 * > RubyVM::InstructionSequence.of(method(:foo))
2692 * > #=> <RubyVM::InstructionSequence:foo@(irb)>
2693 *
2694 * Using ::compile_file:
2695 *
2696 * # /tmp/iseq_of.rb
2697 * def hello
2698 * puts "hello, world"
2699 * end
2700 *
2701 * $a_global_proc = proc { str = 'a' + 'b' }
2702 *
2703 * # in irb
2704 * > require '/tmp/iseq_of.rb'
2705 *
2706 * # first the method hello
2707 * > RubyVM::InstructionSequence.of(method(:hello))
2708 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
2709 *
2710 * # then the global proc
2711 * > RubyVM::InstructionSequence.of($a_global_proc)
2712 * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
2713 */
2714static VALUE
2715iseqw_s_of(VALUE klass, VALUE body)
2716{
2717 const rb_iseq_t *iseq = NULL;
2718
2719 if (rb_obj_is_proc(body)) {
2720 iseq = vm_proc_iseq(body);
2721
2722 if (!rb_obj_is_iseq((VALUE)iseq)) {
2723 iseq = NULL;
2724 }
2725 }
2726 else if (rb_obj_is_method(body)) {
2727 iseq = rb_method_iseq(body);
2728 }
2729 else if (rb_typeddata_is_instance_of(body, &iseqw_data_type)) {
2730 return body;
2731 }
2732
2733 return iseq ? iseqw_new(iseq) : Qnil;
2734}
2735
2736/*
2737 * call-seq:
2738 * InstructionSequence.disasm(body) -> str
2739 * InstructionSequence.disassemble(body) -> str
2740 *
2741 * Takes +body+, a Method or Proc object, and returns a String with the
2742 * human readable instructions for +body+.
2743 *
2744 * For a Method object:
2745 *
2746 * # /tmp/method.rb
2747 * def hello
2748 * puts "hello, world"
2749 * end
2750 *
2751 * puts RubyVM::InstructionSequence.disasm(method(:hello))
2752 *
2753 * Produces:
2754 *
2755 * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
2756 * 0000 trace 8 ( 1)
2757 * 0002 trace 1 ( 2)
2758 * 0004 putself
2759 * 0005 putstring "hello, world"
2760 * 0007 send :puts, 1, nil, 8, <ic:0>
2761 * 0013 trace 16 ( 3)
2762 * 0015 leave ( 2)
2763 *
2764 * For a Proc:
2765 *
2766 * # /tmp/proc.rb
2767 * p = proc { num = 1 + 2 }
2768 * puts RubyVM::InstructionSequence.disasm(p)
2769 *
2770 * Produces:
2771 *
2772 * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
2773 * == catch table
2774 * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
2775 * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
2776 * |------------------------------------------------------------------------
2777 * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
2778 * [ 2] num
2779 * 0000 trace 1 ( 1)
2780 * 0002 putobject 1
2781 * 0004 putobject 2
2782 * 0006 opt_plus <ic:1>
2783 * 0008 dup
2784 * 0009 setlocal num, 0
2785 * 0012 leave
2786 *
2787 */
2788static VALUE
2789iseqw_s_disasm(VALUE klass, VALUE body)
2790{
2791 VALUE iseqw = iseqw_s_of(klass, body);
2792 return NIL_P(iseqw) ? Qnil : rb_iseq_disasm(iseqw_check(iseqw));
2793}
2794
2795const char *
2796ruby_node_name(int node)
2797{
2798 switch (node) {
2799#include "node_name.inc"
2800 default:
2801 rb_bug("unknown node: %d", node);
2802 return 0;
2803 }
2804}
2805
2806static VALUE
2807register_label(struct st_table *table, unsigned long idx)
2808{
2809 VALUE sym = rb_str_intern(rb_sprintf("label_%lu", idx));
2810 st_insert(table, idx, sym);
2811 return sym;
2812}
2813
2814static VALUE
2815exception_type2symbol(VALUE type)
2816{
2817 ID id;
2818 switch (type) {
2819 case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
2820 case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
2821 case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
2822 case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
2823 case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
2824 case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
2825 default:
2826 rb_bug("unknown exception type: %d", (int)type);
2827 }
2828 return ID2SYM(id);
2829}
2830
2831static int
2832cdhash_each(VALUE key, VALUE value, VALUE ary)
2833{
2834 rb_ary_push(ary, obj_resurrect(key));
2835 rb_ary_push(ary, value);
2836 return ST_CONTINUE;
2837}
2838
2839static const rb_data_type_t label_wrapper = {
2840 "label_wrapper",
2841 {(void (*)(void *))rb_mark_tbl, (void (*)(void *))st_free_table, 0, 0,},
2842 0, 0, RUBY_TYPED_FREE_IMMEDIATELY
2843};
2844
2845#define DECL_ID(name) \
2846 static ID id_##name
2847
2848#define INIT_ID(name) \
2849 id_##name = rb_intern(#name)
2850
2851static VALUE
2852iseq_type_id(enum rb_iseq_type type)
2853{
2854 DECL_ID(top);
2855 DECL_ID(method);
2856 DECL_ID(block);
2857 DECL_ID(class);
2858 DECL_ID(rescue);
2859 DECL_ID(ensure);
2860 DECL_ID(eval);
2861 DECL_ID(main);
2862 DECL_ID(plain);
2863
2864 if (id_top == 0) {
2865 INIT_ID(top);
2866 INIT_ID(method);
2867 INIT_ID(block);
2868 INIT_ID(class);
2869 INIT_ID(rescue);
2870 INIT_ID(ensure);
2871 INIT_ID(eval);
2872 INIT_ID(main);
2873 INIT_ID(plain);
2874 }
2875
2876 switch (type) {
2877 case ISEQ_TYPE_TOP: return id_top;
2878 case ISEQ_TYPE_METHOD: return id_method;
2879 case ISEQ_TYPE_BLOCK: return id_block;
2880 case ISEQ_TYPE_CLASS: return id_class;
2881 case ISEQ_TYPE_RESCUE: return id_rescue;
2882 case ISEQ_TYPE_ENSURE: return id_ensure;
2883 case ISEQ_TYPE_EVAL: return id_eval;
2884 case ISEQ_TYPE_MAIN: return id_main;
2885 case ISEQ_TYPE_PLAIN: return id_plain;
2886 };
2887
2888 rb_bug("unsupported iseq type: %d", (int)type);
2889}
2890
2891static VALUE
2892iseq_data_to_ary(const rb_iseq_t *iseq)
2893{
2894 unsigned int i;
2895 long l;
2896 const struct rb_iseq_constant_body *const iseq_body = ISEQ_BODY(iseq);
2897 const struct iseq_insn_info_entry *prev_insn_info;
2898 unsigned int pos;
2899 int last_line = 0;
2900 VALUE *seq, *iseq_original;
2901
2902 VALUE val = rb_ary_new();
2903 ID type; /* Symbol */
2904 VALUE locals = rb_ary_new();
2905 VALUE params = rb_hash_new();
2906 VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
2907 VALUE nbody;
2908 VALUE exception = rb_ary_new(); /* [[....]] */
2909 VALUE misc = rb_hash_new();
2910
2911 static ID insn_syms[VM_INSTRUCTION_SIZE/2]; /* w/o-trace only */
2912 struct st_table *labels_table = st_init_numtable();
2913 VALUE labels_wrapper = TypedData_Wrap_Struct(0, &label_wrapper, labels_table);
2914
2915 if (insn_syms[0] == 0) {
2916 int i;
2917 for (i=0; i<numberof(insn_syms); i++) {
2918 insn_syms[i] = rb_intern(insn_name(i));
2919 }
2920 }
2921
2922 /* type */
2923 type = iseq_type_id(iseq_body->type);
2924
2925 /* locals */
2926 for (i=0; i<iseq_body->local_table_size; i++) {
2927 ID lid = iseq_body->local_table[i];
2928 if (lid) {
2929 if (rb_id2str(lid)) {
2930 rb_ary_push(locals, ID2SYM(lid));
2931 }
2932 else { /* hidden variable from id_internal() */
2933 rb_ary_push(locals, ULONG2NUM(iseq_body->local_table_size-i+1));
2934 }
2935 }
2936 else {
2937 rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
2938 }
2939 }
2940
2941 /* params */
2942 {
2943 const struct rb_iseq_param_keyword *const keyword = iseq_body->param.keyword;
2944 int j;
2945
2946 if (iseq_body->param.flags.has_opt) {
2947 int len = iseq_body->param.opt_num + 1;
2948 VALUE arg_opt_labels = rb_ary_new2(len);
2949
2950 for (j = 0; j < len; j++) {
2951 VALUE l = register_label(labels_table, iseq_body->param.opt_table[j]);
2952 rb_ary_push(arg_opt_labels, l);
2953 }
2954 rb_hash_aset(params, ID2SYM(rb_intern("opt")), arg_opt_labels);
2955 }
2956
2957 /* commit */
2958 if (iseq_body->param.flags.has_lead) rb_hash_aset(params, ID2SYM(rb_intern("lead_num")), INT2FIX(iseq_body->param.lead_num));
2959 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_num")), INT2FIX(iseq_body->param.post_num));
2960 if (iseq_body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_start")), INT2FIX(iseq_body->param.post_start));
2961 if (iseq_body->param.flags.has_rest) rb_hash_aset(params, ID2SYM(rb_intern("rest_start")), INT2FIX(iseq_body->param.rest_start));
2962 if (iseq_body->param.flags.has_block) rb_hash_aset(params, ID2SYM(rb_intern("block_start")), INT2FIX(iseq_body->param.block_start));
2963 if (iseq_body->param.flags.has_kw) {
2964 VALUE keywords = rb_ary_new();
2965 int i, j;
2966 for (i=0; i<keyword->required_num; i++) {
2967 rb_ary_push(keywords, ID2SYM(keyword->table[i]));
2968 }
2969 for (j=0; i<keyword->num; i++, j++) {
2970 VALUE key = rb_ary_new_from_args(1, ID2SYM(keyword->table[i]));
2971 if (!UNDEF_P(keyword->default_values[j])) {
2972 rb_ary_push(key, keyword->default_values[j]);
2973 }
2974 rb_ary_push(keywords, key);
2975 }
2976
2977 rb_hash_aset(params, ID2SYM(rb_intern("kwbits")),
2978 INT2FIX(keyword->bits_start));
2979 rb_hash_aset(params, ID2SYM(rb_intern("keyword")), keywords);
2980 }
2981 if (iseq_body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(keyword->rest_start));
2982 if (iseq_body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
2983 }
2984
2985 /* body */
2986 iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
2987
2988 for (seq = iseq_original; seq < iseq_original + iseq_body->iseq_size; ) {
2989 VALUE insn = *seq++;
2990 int j, len = insn_len(insn);
2991 VALUE *nseq = seq + len - 1;
2992 VALUE ary = rb_ary_new2(len);
2993
2994 rb_ary_push(ary, ID2SYM(insn_syms[insn%numberof(insn_syms)]));
2995 for (j=0; j<len-1; j++, seq++) {
2996 enum ruby_insn_type_chars op_type = insn_op_type(insn, j);
2997
2998 switch (op_type) {
2999 case TS_OFFSET: {
3000 unsigned long idx = nseq - iseq_original + *seq;
3001 rb_ary_push(ary, register_label(labels_table, idx));
3002 break;
3003 }
3004 case TS_LINDEX:
3005 case TS_NUM:
3006 rb_ary_push(ary, INT2FIX(*seq));
3007 break;
3008 case TS_VALUE:
3009 rb_ary_push(ary, obj_resurrect(*seq));
3010 break;
3011 case TS_ISEQ:
3012 {
3013 const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
3014 if (iseq) {
3015 VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
3016 rb_ary_push(ary, val);
3017 }
3018 else {
3019 rb_ary_push(ary, Qnil);
3020 }
3021 }
3022 break;
3023 case TS_IC:
3024 {
3025 VALUE list = rb_ary_new();
3026 const ID *ids = ((IC)*seq)->segments;
3027 while (*ids) {
3028 rb_ary_push(list, ID2SYM(*ids++));
3029 }
3030 rb_ary_push(ary, list);
3031 }
3032 break;
3033 case TS_IVC:
3034 case TS_ICVARC:
3035 case TS_ISE:
3036 {
3037 union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
3038 rb_ary_push(ary, INT2FIX(is - ISEQ_IS_ENTRY_START(ISEQ_BODY(iseq), op_type)));
3039 }
3040 break;
3041 case TS_CALLDATA:
3042 {
3043 struct rb_call_data *cd = (struct rb_call_data *)*seq;
3044 const struct rb_callinfo *ci = cd->ci;
3045 VALUE e = rb_hash_new();
3046 int argc = vm_ci_argc(ci);
3047
3048 ID mid = vm_ci_mid(ci);
3049 rb_hash_aset(e, ID2SYM(rb_intern("mid")), mid ? ID2SYM(mid) : Qnil);
3050 rb_hash_aset(e, ID2SYM(rb_intern("flag")), UINT2NUM(vm_ci_flag(ci)));
3051
3052 if (vm_ci_flag(ci) & VM_CALL_KWARG) {
3053 const struct rb_callinfo_kwarg *kwarg = vm_ci_kwarg(ci);
3054 int i;
3055 VALUE kw = rb_ary_new2((long)kwarg->keyword_len);
3056
3057 argc -= kwarg->keyword_len;
3058 for (i = 0; i < kwarg->keyword_len; i++) {
3059 rb_ary_push(kw, kwarg->keywords[i]);
3060 }
3061 rb_hash_aset(e, ID2SYM(rb_intern("kw_arg")), kw);
3062 }
3063
3064 rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")),
3065 INT2FIX(argc));
3066 rb_ary_push(ary, e);
3067 }
3068 break;
3069 case TS_ID:
3070 rb_ary_push(ary, ID2SYM(*seq));
3071 break;
3072 case TS_CDHASH:
3073 {
3074 VALUE hash = *seq;
3075 VALUE val = rb_ary_new();
3076 int i;
3077
3078 rb_hash_foreach(hash, cdhash_each, val);
3079
3080 for (i=0; i<RARRAY_LEN(val); i+=2) {
3081 VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
3082 unsigned long idx = nseq - iseq_original + pos;
3083
3084 rb_ary_store(val, i+1,
3085 register_label(labels_table, idx));
3086 }
3087 rb_ary_push(ary, val);
3088 }
3089 break;
3090 case TS_FUNCPTR:
3091 {
3092#if SIZEOF_VALUE <= SIZEOF_LONG
3093 VALUE val = LONG2NUM((SIGNED_VALUE)*seq);
3094#else
3095 VALUE val = LL2NUM((SIGNED_VALUE)*seq);
3096#endif
3097 rb_ary_push(ary, val);
3098 }
3099 break;
3100 case TS_BUILTIN:
3101 {
3102 VALUE val = rb_hash_new();
3103#if SIZEOF_VALUE <= SIZEOF_LONG
3104 VALUE func_ptr = LONG2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3105#else
3106 VALUE func_ptr = LL2NUM((SIGNED_VALUE)((RB_BUILTIN)*seq)->func_ptr);
3107#endif
3108 rb_hash_aset(val, ID2SYM(rb_intern("func_ptr")), func_ptr);
3109 rb_hash_aset(val, ID2SYM(rb_intern("argc")), INT2NUM(((RB_BUILTIN)*seq)->argc));
3110 rb_hash_aset(val, ID2SYM(rb_intern("index")), INT2NUM(((RB_BUILTIN)*seq)->index));
3111 rb_hash_aset(val, ID2SYM(rb_intern("name")), rb_str_new_cstr(((RB_BUILTIN)*seq)->name));
3112 rb_ary_push(ary, val);
3113 }
3114 break;
3115 default:
3116 rb_bug("unknown operand: %c", insn_op_type(insn, j));
3117 }
3118 }
3119 rb_ary_push(body, ary);
3120 }
3121
3122 nbody = body;
3123
3124 /* exception */
3125 if (iseq_body->catch_table) for (i=0; i<iseq_body->catch_table->size; i++) {
3126 VALUE ary = rb_ary_new();
3127 const struct iseq_catch_table_entry *entry =
3128 UNALIGNED_MEMBER_PTR(iseq_body->catch_table, entries[i]);
3129 rb_ary_push(ary, exception_type2symbol(entry->type));
3130 if (entry->iseq) {
3131 rb_ary_push(ary, iseq_data_to_ary(rb_iseq_check(entry->iseq)));
3132 }
3133 else {
3134 rb_ary_push(ary, Qnil);
3135 }
3136 rb_ary_push(ary, register_label(labels_table, entry->start));
3137 rb_ary_push(ary, register_label(labels_table, entry->end));
3138 rb_ary_push(ary, register_label(labels_table, entry->cont));
3139 rb_ary_push(ary, UINT2NUM(entry->sp));
3140 rb_ary_push(exception, ary);
3141 }
3142
3143 /* make body with labels and insert line number */
3144 body = rb_ary_new();
3145 prev_insn_info = NULL;
3146#ifdef USE_ISEQ_NODE_ID
3147 VALUE node_ids = rb_ary_new();
3148#endif
3149
3150 for (l=0, pos=0; l<RARRAY_LEN(nbody); l++) {
3151 const struct iseq_insn_info_entry *info;
3152 VALUE ary = RARRAY_AREF(nbody, l);
3153 st_data_t label;
3154
3155 if (st_lookup(labels_table, pos, &label)) {
3156 rb_ary_push(body, (VALUE)label);
3157 }
3158
3159 info = get_insn_info(iseq, pos);
3160#ifdef USE_ISEQ_NODE_ID
3161 rb_ary_push(node_ids, INT2FIX(info->node_id));
3162#endif
3163
3164 if (prev_insn_info != info) {
3165 int line = info->line_no;
3166 rb_event_flag_t events = info->events;
3167
3168 if (line > 0 && last_line != line) {
3169 rb_ary_push(body, INT2FIX(line));
3170 last_line = line;
3171 }
3172#define CHECK_EVENT(ev) if (events & ev) rb_ary_push(body, ID2SYM(rb_intern(#ev)));
3173 CHECK_EVENT(RUBY_EVENT_LINE);
3174 CHECK_EVENT(RUBY_EVENT_CLASS);
3175 CHECK_EVENT(RUBY_EVENT_END);
3176 CHECK_EVENT(RUBY_EVENT_CALL);
3177 CHECK_EVENT(RUBY_EVENT_RETURN);
3178 CHECK_EVENT(RUBY_EVENT_B_CALL);
3179 CHECK_EVENT(RUBY_EVENT_B_RETURN);
3180#undef CHECK_EVENT
3181 prev_insn_info = info;
3182 }
3183
3184 rb_ary_push(body, ary);
3185 pos += RARRAY_LENINT(ary); /* reject too huge data */
3186 }
3187 RB_GC_GUARD(nbody);
3188 RB_GC_GUARD(labels_wrapper);
3189
3190 rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq_body->param.size));
3191 rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq_body->local_table_size));
3192 rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq_body->stack_max));
3193 rb_hash_aset(misc, ID2SYM(rb_intern("node_id")), INT2FIX(iseq_body->location.node_id));
3194 rb_hash_aset(misc, ID2SYM(rb_intern("code_location")),
3195 rb_ary_new_from_args(4,
3196 INT2FIX(iseq_body->location.code_location.beg_pos.lineno),
3197 INT2FIX(iseq_body->location.code_location.beg_pos.column),
3198 INT2FIX(iseq_body->location.code_location.end_pos.lineno),
3199 INT2FIX(iseq_body->location.code_location.end_pos.column)));
3200#ifdef USE_ISEQ_NODE_ID
3201 rb_hash_aset(misc, ID2SYM(rb_intern("node_ids")), node_ids);
3202#endif
3203
3204 /*
3205 * [:magic, :major_version, :minor_version, :format_type, :misc,
3206 * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
3207 * :catch_table, :bytecode]
3208 */
3209 rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
3210 rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
3211 rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
3212 rb_ary_push(val, INT2FIX(1));
3213 rb_ary_push(val, misc);
3214 rb_ary_push(val, iseq_body->location.label);
3215 rb_ary_push(val, rb_iseq_path(iseq));
3216 rb_ary_push(val, rb_iseq_realpath(iseq));
3217 rb_ary_push(val, RB_INT2NUM(iseq_body->location.first_lineno));
3218 rb_ary_push(val, ID2SYM(type));
3219 rb_ary_push(val, locals);
3220 rb_ary_push(val, params);
3221 rb_ary_push(val, exception);
3222 rb_ary_push(val, body);
3223 return val;
3224}
3225
3226VALUE
3227rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
3228{
3229 int i, r;
3230 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3231 const struct rb_iseq_param_keyword *const keyword = body->param.keyword;
3232 VALUE a, args = rb_ary_new2(body->param.size);
3233 ID req, opt, rest, block, key, keyrest;
3234#define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
3235#define PARAM_ID(i) body->local_table[(i)]
3236#define PARAM(i, type) ( \
3237 PARAM_TYPE(type), \
3238 rb_id2str(PARAM_ID(i)) ? \
3239 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
3240 a)
3241
3242 CONST_ID(req, "req");
3243 CONST_ID(opt, "opt");
3244 if (is_proc) {
3245 for (i = 0; i < body->param.lead_num; i++) {
3246 PARAM_TYPE(opt);
3247 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3248 rb_ary_push(args, a);
3249 }
3250 }
3251 else {
3252 for (i = 0; i < body->param.lead_num; i++) {
3253 rb_ary_push(args, PARAM(i, req));
3254 }
3255 }
3256 r = body->param.lead_num + body->param.opt_num;
3257 for (; i < r; i++) {
3258 PARAM_TYPE(opt);
3259 if (rb_id2str(PARAM_ID(i))) {
3260 rb_ary_push(a, ID2SYM(PARAM_ID(i)));
3261 }
3262 rb_ary_push(args, a);
3263 }
3264 if (body->param.flags.has_rest) {
3265 CONST_ID(rest, "rest");
3266 rb_ary_push(args, PARAM(body->param.rest_start, rest));
3267 }
3268 r = body->param.post_start + body->param.post_num;
3269 if (is_proc) {
3270 for (i = body->param.post_start; i < r; i++) {
3271 PARAM_TYPE(opt);
3272 rb_ary_push(a, rb_id2str(PARAM_ID(i)) ? ID2SYM(PARAM_ID(i)) : Qnil);
3273 rb_ary_push(args, a);
3274 }
3275 }
3276 else {
3277 for (i = body->param.post_start; i < r; i++) {
3278 rb_ary_push(args, PARAM(i, req));
3279 }
3280 }
3281 if (body->param.flags.accepts_no_kwarg) {
3282 ID nokey;
3283 CONST_ID(nokey, "nokey");
3284 PARAM_TYPE(nokey);
3285 rb_ary_push(args, a);
3286 }
3287 if (body->param.flags.has_kw) {
3288 i = 0;
3289 if (keyword->required_num > 0) {
3290 ID keyreq;
3291 CONST_ID(keyreq, "keyreq");
3292 for (; i < keyword->required_num; i++) {
3293 PARAM_TYPE(keyreq);
3294 if (rb_id2str(keyword->table[i])) {
3295 rb_ary_push(a, ID2SYM(keyword->table[i]));
3296 }
3297 rb_ary_push(args, a);
3298 }
3299 }
3300 CONST_ID(key, "key");
3301 for (; i < keyword->num; i++) {
3302 PARAM_TYPE(key);
3303 if (rb_id2str(keyword->table[i])) {
3304 rb_ary_push(a, ID2SYM(keyword->table[i]));
3305 }
3306 rb_ary_push(args, a);
3307 }
3308 }
3309 if (body->param.flags.has_kwrest || body->param.flags.ruby2_keywords) {
3310 ID param;
3311 CONST_ID(keyrest, "keyrest");
3312 PARAM_TYPE(keyrest);
3313 if (body->param.flags.has_kwrest &&
3314 rb_id2str(param = PARAM_ID(keyword->rest_start))) {
3315 rb_ary_push(a, ID2SYM(param));
3316 }
3317 else if (body->param.flags.ruby2_keywords) {
3318 rb_ary_push(a, ID2SYM(idPow));
3319 }
3320 rb_ary_push(args, a);
3321 }
3322 if (body->param.flags.has_block) {
3323 CONST_ID(block, "block");
3324 rb_ary_push(args, PARAM(body->param.block_start, block));
3325 }
3326 return args;
3327}
3328
3329VALUE
3330rb_iseq_defined_string(enum defined_type type)
3331{
3332 static const char expr_names[][18] = {
3333 "nil",
3334 "instance-variable",
3335 "local-variable",
3336 "global-variable",
3337 "class variable",
3338 "constant",
3339 "method",
3340 "yield",
3341 "super",
3342 "self",
3343 "true",
3344 "false",
3345 "assignment",
3346 "expression",
3347 };
3348 const char *estr;
3349
3350 if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) rb_bug("unknown defined type %d", type);
3351 estr = expr_names[type - 1];
3352 return rb_fstring_cstr(estr);
3353}
3354
3355/* A map from encoded_insn to insn_data: decoded insn number, its len,
3356 * non-trace version of encoded insn, and trace version. */
3357
3358static st_table *encoded_insn_data;
3359typedef struct insn_data_struct {
3360 int insn;
3361 int insn_len;
3362 void *notrace_encoded_insn;
3363 void *trace_encoded_insn;
3364} insn_data_t;
3365static insn_data_t insn_data[VM_INSTRUCTION_SIZE/2];
3366
3367void
3368rb_vm_encoded_insn_data_table_init(void)
3369{
3370#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3371 const void * const *table = rb_vm_get_insns_address_table();
3372#define INSN_CODE(insn) ((VALUE)table[insn])
3373#else
3374#define INSN_CODE(insn) (insn)
3375#endif
3376 st_data_t insn;
3377 encoded_insn_data = st_init_numtable_with_size(VM_INSTRUCTION_SIZE / 2);
3378
3379 for (insn = 0; insn < VM_INSTRUCTION_SIZE/2; insn++) {
3380 st_data_t key1 = (st_data_t)INSN_CODE(insn);
3381 st_data_t key2 = (st_data_t)INSN_CODE(insn + VM_INSTRUCTION_SIZE/2);
3382
3383 insn_data[insn].insn = (int)insn;
3384 insn_data[insn].insn_len = insn_len(insn);
3385
3386 if (insn != BIN(opt_invokebuiltin_delegate_leave)) {
3387 insn_data[insn].notrace_encoded_insn = (void *) key1;
3388 insn_data[insn].trace_encoded_insn = (void *) key2;
3389 }
3390 else {
3391 insn_data[insn].notrace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate));
3392 insn_data[insn].trace_encoded_insn = (void *) INSN_CODE(BIN(opt_invokebuiltin_delegate) + VM_INSTRUCTION_SIZE/2);
3393 }
3394
3395 st_add_direct(encoded_insn_data, key1, (st_data_t)&insn_data[insn]);
3396 st_add_direct(encoded_insn_data, key2, (st_data_t)&insn_data[insn]);
3397 }
3398}
3399
3400int
3401rb_vm_insn_addr2insn(const void *addr)
3402{
3403 st_data_t key = (st_data_t)addr;
3404 st_data_t val;
3405
3406 if (st_lookup(encoded_insn_data, key, &val)) {
3407 insn_data_t *e = (insn_data_t *)val;
3408 return (int)e->insn;
3409 }
3410
3411 rb_bug("rb_vm_insn_addr2insn: invalid insn address: %p", addr);
3412}
3413
3414// Unlike rb_vm_insn_addr2insn, this function can return trace opcode variants.
3415int
3416rb_vm_insn_addr2opcode(const void *addr)
3417{
3418 st_data_t key = (st_data_t)addr;
3419 st_data_t val;
3420
3421 if (st_lookup(encoded_insn_data, key, &val)) {
3422 insn_data_t *e = (insn_data_t *)val;
3423 int opcode = e->insn;
3424 if (addr == e->trace_encoded_insn) {
3425 opcode += VM_INSTRUCTION_SIZE/2;
3426 }
3427 return opcode;
3428 }
3429
3430 rb_bug("rb_vm_insn_addr2opcode: invalid insn address: %p", addr);
3431}
3432
3433// Decode `ISEQ_BODY(iseq)->iseq_encoded[i]` to an insn.
3434int
3435rb_vm_insn_decode(const VALUE encoded)
3436{
3437#if OPT_DIRECT_THREADED_CODE || OPT_CALL_THREADED_CODE
3438 int insn = rb_vm_insn_addr2insn((void *)encoded);
3439#else
3440 int insn = (int)encoded;
3441#endif
3442 return insn;
3443}
3444
3445static inline int
3446encoded_iseq_trace_instrument(VALUE *iseq_encoded_insn, rb_event_flag_t turnon, bool remain_current_trace)
3447{
3448 st_data_t key = (st_data_t)*iseq_encoded_insn;
3449 st_data_t val;
3450
3451 if (st_lookup(encoded_insn_data, key, &val)) {
3452 insn_data_t *e = (insn_data_t *)val;
3453 if (remain_current_trace && key == (st_data_t)e->trace_encoded_insn) {
3454 turnon = 1;
3455 }
3456 *iseq_encoded_insn = (VALUE) (turnon ? e->trace_encoded_insn : e->notrace_encoded_insn);
3457 return e->insn_len;
3458 }
3459
3460 rb_bug("trace_instrument: invalid insn address: %p", (void *)*iseq_encoded_insn);
3461}
3462
3463void
3464rb_iseq_trace_flag_cleared(const rb_iseq_t *iseq, size_t pos)
3465{
3466 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3467 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3468 encoded_iseq_trace_instrument(&iseq_encoded[pos], 0, false);
3469}
3470
3471// We need to fire call events on instructions with b_call events if the block
3472// is running as a method. So, if we are listening for call events, then
3473// instructions that have b_call events need to become trace variants.
3474// Use this function when making decisions about recompiling to trace variants.
3475static inline rb_event_flag_t
3476add_bmethod_events(rb_event_flag_t events)
3477{
3478 if (events & RUBY_EVENT_CALL) {
3479 events |= RUBY_EVENT_B_CALL;
3480 }
3481 if (events & RUBY_EVENT_RETURN) {
3482 events |= RUBY_EVENT_B_RETURN;
3483 }
3484 return events;
3485}
3486
3487// Note, to support call/return events for bmethods, turnon_event can have more events than tpval.
3488static int
3489iseq_add_local_tracepoint(const rb_iseq_t *iseq, rb_event_flag_t turnon_events, VALUE tpval, unsigned int target_line)
3490{
3491 unsigned int pc;
3492 int n = 0;
3493 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3494 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3495
3496 VM_ASSERT(ISEQ_EXECUTABLE_P(iseq));
3497
3498 for (pc=0; pc<body->iseq_size;) {
3499 const struct iseq_insn_info_entry *entry = get_insn_info(iseq, pc);
3500 rb_event_flag_t pc_events = entry->events;
3501 rb_event_flag_t target_events = turnon_events;
3502 unsigned int line = (int)entry->line_no;
3503
3504 if (target_line == 0 || target_line == line) {
3505 /* ok */
3506 }
3507 else {
3508 target_events &= ~RUBY_EVENT_LINE;
3509 }
3510
3511 if (pc_events & target_events) {
3512 n++;
3513 }
3514 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (target_events | iseq->aux.exec.global_trace_events), true);
3515 }
3516
3517 if (n > 0) {
3518 if (iseq->aux.exec.local_hooks == NULL) {
3519 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = RB_ZALLOC(rb_hook_list_t);
3520 iseq->aux.exec.local_hooks->is_local = true;
3521 }
3522 rb_hook_list_connect_tracepoint((VALUE)iseq, iseq->aux.exec.local_hooks, tpval, target_line);
3523 }
3524
3525 return n;
3526}
3527
3529 rb_event_flag_t turnon_events;
3530 VALUE tpval;
3531 unsigned int target_line;
3532 int n;
3533};
3534
3535static void
3536iseq_add_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3537{
3539 data->n += iseq_add_local_tracepoint(iseq, data->turnon_events, data->tpval, data->target_line);
3540 iseq_iterate_children(iseq, iseq_add_local_tracepoint_i, p);
3541}
3542
3543int
3544rb_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)
3545{
3547 if (target_bmethod) {
3548 turnon_events = add_bmethod_events(turnon_events);
3549 }
3550 data.turnon_events = turnon_events;
3551 data.tpval = tpval;
3552 data.target_line = target_line;
3553 data.n = 0;
3554
3555 iseq_add_local_tracepoint_i(iseq, (void *)&data);
3556 if (0) rb_funcall(Qnil, rb_intern("puts"), 1, rb_iseq_disasm(iseq)); /* for debug */
3557 return data.n;
3558}
3559
3560static int
3561iseq_remove_local_tracepoint(const rb_iseq_t *iseq, VALUE tpval)
3562{
3563 int n = 0;
3564
3565 if (iseq->aux.exec.local_hooks) {
3566 unsigned int pc;
3567 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3568 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3569 rb_event_flag_t local_events = 0;
3570
3571 rb_hook_list_remove_tracepoint(iseq->aux.exec.local_hooks, tpval);
3572 local_events = iseq->aux.exec.local_hooks->events;
3573
3574 if (local_events == 0) {
3575 rb_hook_list_free(iseq->aux.exec.local_hooks);
3576 ((rb_iseq_t *)iseq)->aux.exec.local_hooks = NULL;
3577 }
3578
3579 local_events = add_bmethod_events(local_events);
3580 for (pc = 0; pc<body->iseq_size;) {
3581 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3582 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & (local_events | iseq->aux.exec.global_trace_events), false);
3583 }
3584 }
3585 return n;
3586}
3587
3589 VALUE tpval;
3590 int n;
3591};
3592
3593static void
3594iseq_remove_local_tracepoint_i(const rb_iseq_t *iseq, void *p)
3595{
3597 data->n += iseq_remove_local_tracepoint(iseq, data->tpval);
3598 iseq_iterate_children(iseq, iseq_remove_local_tracepoint_i, p);
3599}
3600
3601int
3602rb_iseq_remove_local_tracepoint_recursively(const rb_iseq_t *iseq, VALUE tpval)
3603{
3605 data.tpval = tpval;
3606 data.n = 0;
3607
3608 iseq_remove_local_tracepoint_i(iseq, (void *)&data);
3609 return data.n;
3610}
3611
3612void
3613rb_iseq_trace_set(const rb_iseq_t *iseq, rb_event_flag_t turnon_events)
3614{
3615 if (iseq->aux.exec.global_trace_events == turnon_events) {
3616 return;
3617 }
3618
3619 if (!ISEQ_EXECUTABLE_P(iseq)) {
3620 /* this is building ISeq */
3621 return;
3622 }
3623 else {
3624 unsigned int pc;
3625 const struct rb_iseq_constant_body *const body = ISEQ_BODY(iseq);
3626 VALUE *iseq_encoded = (VALUE *)body->iseq_encoded;
3627 rb_event_flag_t enabled_events;
3628 rb_event_flag_t local_events = iseq->aux.exec.local_hooks ? iseq->aux.exec.local_hooks->events : 0;
3629 ((rb_iseq_t *)iseq)->aux.exec.global_trace_events = turnon_events;
3630 enabled_events = add_bmethod_events(turnon_events | local_events);
3631
3632 for (pc=0; pc<body->iseq_size;) {
3633 rb_event_flag_t pc_events = rb_iseq_event_flags(iseq, pc);
3634 pc += encoded_iseq_trace_instrument(&iseq_encoded[pc], pc_events & enabled_events, true);
3635 }
3636 }
3637}
3638
3639bool rb_vm_call_ivar_attrset_p(const vm_call_handler ch);
3640void rb_vm_cc_general(const struct rb_callcache *cc);
3641
3642static int
3643clear_attr_ccs_i(void *vstart, void *vend, size_t stride, void *data)
3644{
3645 VALUE v = (VALUE)vstart;
3646 for (; v != (VALUE)vend; v += stride) {
3647 void *ptr = asan_poisoned_object_p(v);
3648 asan_unpoison_object(v, false);
3649
3650 if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((const struct rb_callcache *)v)->call_)) {
3651 rb_vm_cc_general((struct rb_callcache *)v);
3652 }
3653
3654 asan_poison_object_if(ptr, v);
3655 }
3656 return 0;
3657}
3658
3659void
3660rb_clear_attr_ccs(void)
3661{
3662 rb_objspace_each_objects(clear_attr_ccs_i, NULL);
3663}
3664
3665static int
3666trace_set_i(void *vstart, void *vend, size_t stride, void *data)
3667{
3668 rb_event_flag_t turnon_events = *(rb_event_flag_t *)data;
3669
3670 VALUE v = (VALUE)vstart;
3671 for (; v != (VALUE)vend; v += stride) {
3672 void *ptr = asan_poisoned_object_p(v);
3673 asan_unpoison_object(v, false);
3674
3675 if (rb_obj_is_iseq(v)) {
3676 rb_iseq_trace_set(rb_iseq_check((rb_iseq_t *)v), turnon_events);
3677 }
3678 else if (imemo_type_p(v, imemo_callcache) && rb_vm_call_ivar_attrset_p(((const struct rb_callcache *)v)->call_)) {
3679 rb_vm_cc_general((struct rb_callcache *)v);
3680 }
3681
3682 asan_poison_object_if(ptr, v);
3683 }
3684 return 0;
3685}
3686
3687void
3688rb_iseq_trace_set_all(rb_event_flag_t turnon_events)
3689{
3690 rb_objspace_each_objects(trace_set_i, &turnon_events);
3691}
3692
3693VALUE
3694rb_iseqw_local_variables(VALUE iseqval)
3695{
3696 return rb_iseq_local_variables(iseqw_check(iseqval));
3697}
3698
3699/*
3700 * call-seq:
3701 * iseq.to_binary(extra_data = nil) -> binary str
3702 *
3703 * Returns serialized iseq binary format data as a String object.
3704 * A corresponding iseq object is created by
3705 * RubyVM::InstructionSequence.load_from_binary() method.
3706 *
3707 * String extra_data will be saved with binary data.
3708 * You can access this data with
3709 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary).
3710 *
3711 * Note that the translated binary data is not portable.
3712 * You can not move this binary data to another machine.
3713 * You can not use the binary data which is created by another
3714 * version/another architecture of Ruby.
3715 */
3716static VALUE
3717iseqw_to_binary(int argc, VALUE *argv, VALUE self)
3718{
3719 VALUE opt = !rb_check_arity(argc, 0, 1) ? Qnil : argv[0];
3720 return rb_iseq_ibf_dump(iseqw_check(self), opt);
3721}
3722
3723/*
3724 * call-seq:
3725 * RubyVM::InstructionSequence.load_from_binary(binary) -> iseq
3726 *
3727 * Load an iseq object from binary format String object
3728 * created by RubyVM::InstructionSequence.to_binary.
3729 *
3730 * This loader does not have a verifier, so that loading broken/modified
3731 * binary causes critical problem.
3732 *
3733 * You should not load binary data provided by others.
3734 * You should use binary data translated by yourself.
3735 */
3736static VALUE
3737iseqw_s_load_from_binary(VALUE self, VALUE str)
3738{
3739 return iseqw_new(rb_iseq_ibf_load(str));
3740}
3741
3742/*
3743 * call-seq:
3744 * RubyVM::InstructionSequence.load_from_binary_extra_data(binary) -> str
3745 *
3746 * Load extra data embed into binary format String object.
3747 */
3748static VALUE
3749iseqw_s_load_from_binary_extra_data(VALUE self, VALUE str)
3750{
3751 return rb_iseq_ibf_load_extra_data(str);
3752}
3753
3754#if VM_INSN_INFO_TABLE_IMPL == 2
3755
3756/* An implementation of succinct bit-vector for insn_info table.
3757 *
3758 * A succinct bit-vector is a small and efficient data structure that provides
3759 * a bit-vector augmented with an index for O(1) rank operation:
3760 *
3761 * rank(bv, n): the number of 1's within a range from index 0 to index n
3762 *
3763 * This can be used to lookup insn_info table from PC.
3764 * For example, consider the following iseq and insn_info_table:
3765 *
3766 * iseq insn_info_table
3767 * PC insn+operand position lineno event
3768 * 0: insn1 0: 1 [Li]
3769 * 2: insn2 2: 2 [Li] <= (A)
3770 * 5: insn3 8: 3 [Li] <= (B)
3771 * 8: insn4
3772 *
3773 * In this case, a succinct bit-vector whose indexes 0, 2, 8 is "1" and
3774 * other indexes is "0", i.e., "101000001", is created.
3775 * To lookup the lineno of insn2, calculate rank("10100001", 2) = 2, so
3776 * the line (A) is the entry in question.
3777 * To lookup the lineno of insn4, calculate rank("10100001", 8) = 3, so
3778 * the line (B) is the entry in question.
3779 *
3780 * A naive implementation of succinct bit-vector works really well
3781 * not only for large size but also for small size. However, it has
3782 * tiny overhead for very small size. So, this implementation consist
3783 * of two parts: one part is the "immediate" table that keeps rank result
3784 * as a raw table, and the other part is a normal succinct bit-vector.
3785 */
3786
3787#define IMMEDIATE_TABLE_SIZE 54 /* a multiple of 9, and < 128 */
3788
3789struct succ_index_table {
3790 uint64_t imm_part[IMMEDIATE_TABLE_SIZE / 9];
3791 struct succ_dict_block {
3792 unsigned int rank;
3793 uint64_t small_block_ranks; /* 9 bits * 7 = 63 bits */
3794 uint64_t bits[512/64];
3795 } succ_part[FLEX_ARY_LEN];
3796};
3797
3798#define imm_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (7 * (i))
3799#define imm_block_rank_get(v, i) (((int)((v) >> ((i) * 7))) & 0x7f)
3800#define small_block_rank_set(v, i, r) (v) |= (uint64_t)(r) << (9 * ((i) - 1))
3801#define small_block_rank_get(v, i) ((i) == 0 ? 0 : (((int)((v) >> (((i) - 1) * 9))) & 0x1ff))
3802
3803static struct succ_index_table *
3804succ_index_table_create(int max_pos, int *data, int size)
3805{
3806 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3807 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3808 struct succ_index_table *sd =
3809 rb_xcalloc_mul_add_mul(
3810 imm_size, sizeof(uint64_t),
3811 succ_size, sizeof(struct succ_dict_block));
3812 int i, j, k, r;
3813
3814 r = 0;
3815 for (j = 0; j < imm_size; j++) {
3816 for (i = 0; i < 9; i++) {
3817 if (r < size && data[r] == j * 9 + i) r++;
3818 imm_block_rank_set(sd->imm_part[j], i, r);
3819 }
3820 }
3821 for (k = 0; k < succ_size; k++) {
3822 struct succ_dict_block *sd_block = &sd->succ_part[k];
3823 int small_rank = 0;
3824 sd_block->rank = r;
3825 for (j = 0; j < 8; j++) {
3826 uint64_t bits = 0;
3827 if (j) small_block_rank_set(sd_block->small_block_ranks, j, small_rank);
3828 for (i = 0; i < 64; i++) {
3829 if (r < size && data[r] == k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE) {
3830 bits |= ((uint64_t)1) << i;
3831 r++;
3832 }
3833 }
3834 sd_block->bits[j] = bits;
3835 small_rank += rb_popcount64(bits);
3836 }
3837 }
3838 return sd;
3839}
3840
3841static unsigned int *
3842succ_index_table_invert(int max_pos, struct succ_index_table *sd, int size)
3843{
3844 const int imm_size = (max_pos < IMMEDIATE_TABLE_SIZE ? max_pos + 8 : IMMEDIATE_TABLE_SIZE) / 9;
3845 const int succ_size = (max_pos < IMMEDIATE_TABLE_SIZE ? 0 : (max_pos - IMMEDIATE_TABLE_SIZE + 511)) / 512;
3846 unsigned int *positions = ALLOC_N(unsigned int, size), *p;
3847 int i, j, k, r = -1;
3848 p = positions;
3849 for (j = 0; j < imm_size; j++) {
3850 for (i = 0; i < 9; i++) {
3851 int nr = imm_block_rank_get(sd->imm_part[j], i);
3852 if (r != nr) *p++ = j * 9 + i;
3853 r = nr;
3854 }
3855 }
3856 for (k = 0; k < succ_size; k++) {
3857 for (j = 0; j < 8; j++) {
3858 for (i = 0; i < 64; i++) {
3859 if (sd->succ_part[k].bits[j] & (((uint64_t)1) << i)) {
3860 *p++ = k * 512 + j * 64 + i + IMMEDIATE_TABLE_SIZE;
3861 }
3862 }
3863 }
3864 }
3865 return positions;
3866}
3867
3868static int
3869succ_index_lookup(const struct succ_index_table *sd, int x)
3870{
3871 if (x < IMMEDIATE_TABLE_SIZE) {
3872 const int i = x / 9;
3873 const int j = x % 9;
3874 return imm_block_rank_get(sd->imm_part[i], j);
3875 }
3876 else {
3877 const int block_index = (x - IMMEDIATE_TABLE_SIZE) / 512;
3878 const struct succ_dict_block *block = &sd->succ_part[block_index];
3879 const int block_bit_index = (x - IMMEDIATE_TABLE_SIZE) % 512;
3880 const int small_block_index = block_bit_index / 64;
3881 const int small_block_popcount = small_block_rank_get(block->small_block_ranks, small_block_index);
3882 const int popcnt = rb_popcount64(block->bits[small_block_index] << (63 - block_bit_index % 64));
3883
3884 return block->rank + small_block_popcount + popcnt;
3885 }
3886}
3887#endif
3888
3889
3890/*
3891 * call-seq:
3892 * iseq.script_lines -> array or nil
3893 *
3894 * It returns recorded script lines if it is availalble.
3895 * The script lines are not limited to the iseq range, but
3896 * are entire lines of the source file.
3897 *
3898 * Note that this is an API for ruby internal use, debugging,
3899 * and research. Do not use this for any other purpose.
3900 * The compatibility is not guaranteed.
3901 */
3902static VALUE
3903iseqw_script_lines(VALUE self)
3904{
3905 const rb_iseq_t *iseq = iseqw_check(self);
3906 return ISEQ_BODY(iseq)->variable.script_lines;
3907}
3908
3909/*
3910 * Document-class: RubyVM::InstructionSequence
3911 *
3912 * The InstructionSequence class represents a compiled sequence of
3913 * instructions for the Virtual Machine used in MRI. Not all implementations of Ruby
3914 * may implement this class, and for the implementations that implement it,
3915 * the methods defined and behavior of the methods can change in any version.
3916 *
3917 * With it, you can get a handle to the instructions that make up a method or
3918 * a proc, compile strings of Ruby code down to VM instructions, and
3919 * disassemble instruction sequences to strings for easy inspection. It is
3920 * mostly useful if you want to learn how YARV works, but it also lets
3921 * you control various settings for the Ruby iseq compiler.
3922 *
3923 * You can find the source for the VM instructions in +insns.def+ in the Ruby
3924 * source.
3925 *
3926 * The instruction sequence results will almost certainly change as Ruby
3927 * changes, so example output in this documentation may be different from what
3928 * you see.
3929 *
3930 * Of course, this class is MRI specific.
3931 */
3932
3933void
3934Init_ISeq(void)
3935{
3936 /* declare ::RubyVM::InstructionSequence */
3937 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
3938 rb_undef_alloc_func(rb_cISeq);
3939 rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
3940 rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
3941 rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
3942 rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
3943 rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
3944
3945 rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
3946 rb_define_singleton_method(rb_cISeq, "load_from_binary", iseqw_s_load_from_binary, 1);
3947 rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1);
3948
3949 /* location APIs */
3950 rb_define_method(rb_cISeq, "path", iseqw_path, 0);
3951 rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0);
3952 rb_define_method(rb_cISeq, "label", iseqw_label, 0);
3953 rb_define_method(rb_cISeq, "base_label", iseqw_base_label, 0);
3954 rb_define_method(rb_cISeq, "first_lineno", iseqw_first_lineno, 0);
3955 rb_define_method(rb_cISeq, "trace_points", iseqw_trace_points, 0);
3956 rb_define_method(rb_cISeq, "each_child", iseqw_each_child, 0);
3957
3958#if 0 /* TBD */
3959 rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
3960 rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
3961 /* disable this feature because there is no verifier. */
3962 rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1);
3963#endif
3964 (void)iseq_s_load;
3965
3966 rb_define_singleton_method(rb_cISeq, "compile", iseqw_s_compile, -1);
3967 rb_define_singleton_method(rb_cISeq, "new", iseqw_s_compile, -1);
3968 rb_define_singleton_method(rb_cISeq, "compile_file", iseqw_s_compile_file, -1);
3969 rb_define_singleton_method(rb_cISeq, "compile_option", iseqw_s_compile_option_get, 0);
3970 rb_define_singleton_method(rb_cISeq, "compile_option=", iseqw_s_compile_option_set, 1);
3971 rb_define_singleton_method(rb_cISeq, "disasm", iseqw_s_disasm, 1);
3972 rb_define_singleton_method(rb_cISeq, "disassemble", iseqw_s_disasm, 1);
3973 rb_define_singleton_method(rb_cISeq, "of", iseqw_s_of, 1);
3974
3975 // script lines
3976 rb_define_method(rb_cISeq, "script_lines", iseqw_script_lines, 0);
3977
3978 rb_undef_method(CLASS_OF(rb_cISeq), "translate");
3979 rb_undef_method(CLASS_OF(rb_cISeq), "load_iseq");
3980}
#define RUBY_ASSERT(expr)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:177
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define RUBY_EVENT_END
Encountered an end of a class clause.
Definition event.h:36
#define RUBY_EVENT_C_CALL
A method, written in C, is called.
Definition event.h:39
#define RUBY_EVENT_B_RETURN
Encountered a next statement.
Definition event.h:52
#define RUBY_EVENT_CLASS
Encountered a new class.
Definition event.h:35
#define RUBY_EVENT_LINE
Encountered a new line.
Definition event.h:34
#define RUBY_EVENT_RETURN
Encountered a return statement.
Definition event.h:38
#define RUBY_EVENT_C_RETURN
Return from a method, written in C.
Definition event.h:40
#define RUBY_EVENT_B_CALL
Encountered an yield statement.
Definition event.h:51
uint32_t rb_event_flag_t
Represents event(s).
Definition event.h:103
#define RUBY_EVENT_CALL
A method, written in Ruby, is called.
Definition event.h:37
#define RB_OBJ_FREEZE
Just another name of rb_obj_freeze_inline.
Definition fl_type.h:94
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:955
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2108
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:2574
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition string.h:1675
#define T_FILE
Old name of RUBY_T_FILE.
Definition value_type.h:62
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1683
#define ID2SYM
Old name of RB_ID2SYM.
Definition symbol.h:44
#define SPECIAL_CONST_P
Old name of RB_SPECIAL_CONST_P.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition long.h:60
#define ZALLOC
Old name of RB_ZALLOC.
Definition memory.h:396
#define LL2NUM
Old name of RB_LL2NUM.
Definition long_long.h:30
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:203
#define T_NONE
Old name of RUBY_T_NONE.
Definition value_type.h:74
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
#define T_HASH
Old name of RUBY_T_HASH.
Definition value_type.h:65
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition memory.h:393
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:140
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define BUILTIN_TYPE
Old name of RB_BUILTIN_TYPE.
Definition value_type.h:85
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define UINT2NUM
Old name of RB_UINT2NUM.
Definition int.h:46
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition symbol.h:47
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:651
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition error.c:3150
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:688
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition error.c:794
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1091
VALUE rb_eSyntaxError
SyntaxError exception.
Definition error.c:1108
VALUE rb_class_superclass(VALUE klass)
Queries the parent of the given class.
Definition object.c:1995
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:84
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:190
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:600
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition object.c:1182
#define RB_OBJ_WRITTEN(old, oldv, young)
Identical to RB_OBJ_WRITE(), except it doesn't write any values, but only a WB declaration.
Definition rgengc.h:232
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition rgengc.h:220
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1102
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:280
VALUE rb_file_open_str(VALUE fname, const char *fmode)
Identical to rb_file_open(), except it takes the pathname as a Ruby's string instead of C's.
Definition io.c:7165
VALUE rb_io_close(VALUE io)
Closes the IO.
Definition io.c:5667
VALUE rb_obj_is_method(VALUE recv)
Queries if the given object is a method.
Definition proc.c:1637
VALUE rb_obj_is_proc(VALUE recv)
Queries if the given object is a proc.
Definition proc.c:175
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3353
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1498
#define rb_exc_new_cstr(exc, str)
Identical to rb_exc_new(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1670
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:1834
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition string.c:3177
VALUE rb_str_resurrect(VALUE str)
I guess there is no use case of this function in extension libraries, but this is a routine identical...
Definition string.c:1840
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3020
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition string.c:6710
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
Definition string.c:3637
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:3453
#define rb_str_cat_cstr(buf, str)
Identical to rb_str_cat(), except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1656
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
Definition string.c:3064
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition string.c:2445
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
Definition string.h:1514
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:851
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
Definition variable.c:310
int rb_respond_to(VALUE obj, ID mid)
Queries if the object responds to the method.
Definition vm_method.c:2805
void rb_undef_alloc_func(VALUE klass)
Deletes the allocator function of a class.
Definition vm_method.c:1142
VALUE rb_check_funcall(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcallv(), except it returns RUBY_Qundef instead of raising rb_eNoMethodError.
Definition vm_eval.c:664
ID rb_check_id(volatile VALUE *namep)
Detects if the given name is already interned or not.
Definition symbol.c:1085
ID rb_intern(const char *name)
Finds or creates a symbol of the given name.
Definition symbol.c:796
VALUE rb_sym2str(VALUE id)
Identical to rb_id2str(), except it takes an instance of rb_cSymbol rather than an ID.
Definition symbol.c:943
const char * rb_id2name(ID id)
Retrieves the name mapped to the given id.
Definition symbol.c:960
VALUE rb_ractor_make_shareable(VALUE obj)
Destructively transforms the passed object so that multiple Ractors can share it.
Definition ractor.c:2522
#define RB_NUM2INT
Just another name of rb_num2int_inline.
Definition int.h:38
#define RB_INT2NUM
Just another name of rb_int2num_inline.
Definition int.h:37
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition sprintf.c:1219
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition sprintf.c:1242
VALUE rb_yield(VALUE val)
Yields the block.
Definition vm_eval.c:1357
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:161
#define RB_ZALLOC(type)
Shorthand of RB_ZALLOC_N with n=1.
Definition memory.h:243
VALUE type(ANYARGS)
ANYARGS-ed function type.
void rb_hash_foreach(VALUE q, int_type *w, VALUE e)
Iteration over the given hash.
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:68
static int RARRAY_LENINT(VALUE ary)
Identical to rb_array_len(), except it differs for the return type.
Definition rarray.h:343
#define RARRAY_AREF(a, i)
Definition rarray.h:583
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:152
#define DATA_PTR(obj)
Convenient getter macro.
Definition rdata.h:71
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:82
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:72
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:528
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition rstring.h:484
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition rstring.h:498
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:95
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition rtypeddata.h:441
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition ruby.h:91
#define RTEST
This is an old name of RB_TEST.
Definition node.h:156
Definition iseq.h:263
const ID * segments
A null-terminated list of ids, used to represent a constant's path idNULL is used to represent the ::...
Definition vm_core.h:273
Definition vm_core.h:281
Definition vm_core.h:276
Definition iseq.h:234
This is the struct that holds necessary info for a struct.
Definition rtypeddata.h:190
struct rb_iseq_constant_body::@132 param
parameter information
Definition st.h:79
Definition vm_core.h:285
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:432
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:375
ruby_value_type
C-level type of an object.
Definition value_type.h:112