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