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