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