Ruby: iseq.c Source File

Ruby 3.4.1p0 (2024-12-25 revision 48d4efcb85000e1ebae42004e963b5d0cedddcf2)

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