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