Ruby: iseq.c Source File

Ruby 3.3.7p123 (2025-01-15 revision be31f993d7fa0219d85f7b3c694d454da4ecc10b)

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