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