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