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