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