Ruby  2.4.2p198(2017-09-14revision59899)
iseq.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  iseq.c -
4 
5  $Author: nobu $
6  created at: 2006-07-11(Tue) 09:00:03 +0900
7 
8  Copyright (C) 2006 Koichi Sasada
9 
10 **********************************************************************/
11 
12 #include "internal.h"
13 #include "ruby/util.h"
14 #include "eval_intern.h"
15 
16 #ifdef HAVE_DLADDR
17 # include <dlfcn.h>
18 #endif
19 
20 /* #define RUBY_MARK_FREE_DEBUG 1 */
21 #include "gc.h"
22 #include "vm_core.h"
23 #include "iseq.h"
24 #include "id_table.h"
25 
26 #include "insns.inc"
27 #include "insns_info.inc"
28 
30 static VALUE iseqw_new(const rb_iseq_t *iseq);
31 static const rb_iseq_t *iseqw_check(VALUE iseqw);
32 
33 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
34 
35 static inline VALUE
37 {
38  if (hidden_obj_p(obj)) {
39  switch (BUILTIN_TYPE(obj)) {
40  case T_STRING:
41  obj = rb_str_resurrect(obj);
42  break;
43  case T_ARRAY:
44  obj = rb_ary_resurrect(obj);
45  break;
46  }
47  }
48  return obj;
49 }
50 
51 static void
52 compile_data_free(struct iseq_compile_data *compile_data)
53 {
54  if (compile_data) {
55  struct iseq_compile_data_storage *cur, *next;
56  cur = compile_data->storage_head;
57  while (cur) {
58  next = cur->next;
59  ruby_xfree(cur);
60  cur = next;
61  }
62  if (compile_data->ivar_cache_table) {
63  rb_id_table_free(compile_data->ivar_cache_table);
64  }
65  ruby_xfree(compile_data);
66  }
67 }
68 
69 void
70 rb_iseq_free(const rb_iseq_t *iseq)
71 {
72  RUBY_FREE_ENTER("iseq");
73 
74  if (iseq) {
75  if (iseq->body) {
76  ruby_xfree((void *)iseq->body->iseq_encoded);
77  ruby_xfree((void *)iseq->body->line_info_table);
78  ruby_xfree((void *)iseq->body->local_table);
79  ruby_xfree((void *)iseq->body->is_entries);
80 
81  if (iseq->body->ci_entries) {
82  unsigned int i;
83  struct rb_call_info_with_kwarg *ci_kw_entries = (struct rb_call_info_with_kwarg *)&iseq->body->ci_entries[iseq->body->ci_size];
84  for (i=0; i<iseq->body->ci_kw_size; i++) {
85  const struct rb_call_info_kw_arg *kw_arg = ci_kw_entries[i].kw_arg;
86  ruby_xfree((void *)kw_arg);
87  }
88  ruby_xfree(iseq->body->ci_entries);
89  ruby_xfree(iseq->body->cc_entries);
90  }
91  ruby_xfree((void *)iseq->body->catch_table);
92  ruby_xfree((void *)iseq->body->param.opt_table);
93 
94  if (iseq->body->param.keyword != NULL) {
95  ruby_xfree((void *)iseq->body->param.keyword->default_values);
96  ruby_xfree((void *)iseq->body->param.keyword);
97  }
99  ruby_xfree(iseq->body);
100  }
101  }
102  RUBY_FREE_LEAVE("iseq");
103 }
104 
105 void
107 {
108  RUBY_MARK_ENTER("iseq");
109 
110  RUBY_GC_INFO("%s @ %s\n", RSTRING_PTR(iseq->body->location.label), RSTRING_PTR(iseq->body->location.path));
111 
112  if (iseq->body) {
113  const struct rb_iseq_constant_body *body = iseq->body;
114 
116  rb_gc_mark(body->location.label);
118  rb_gc_mark(body->location.path);
121  }
122 
123  if (FL_TEST(iseq, ISEQ_NOT_LOADED_YET)) {
124  rb_gc_mark(iseq->aux.loader.obj);
125  }
126  else if (ISEQ_COMPILE_DATA(iseq) != 0) {
127  const struct iseq_compile_data *const compile_data = ISEQ_COMPILE_DATA(iseq);
128  RUBY_MARK_UNLESS_NULL(compile_data->mark_ary);
129  RUBY_MARK_UNLESS_NULL(compile_data->err_info);
130  RUBY_MARK_UNLESS_NULL(compile_data->catch_table_ary);
131  }
132 
133  RUBY_MARK_LEAVE("iseq");
134 }
135 
136 static size_t
137 param_keyword_size(const struct rb_iseq_param_keyword *pkw)
138 {
139  size_t size = 0;
140 
141  if (!pkw) return size;
142 
143  size += sizeof(struct rb_iseq_param_keyword);
144  size += sizeof(VALUE) * (pkw->num - pkw->required_num);
145 
146  return size;
147 }
148 
149 static size_t
151 {
152  size_t size = 0; /* struct already counted as RVALUE size */
153  const struct rb_iseq_constant_body *body = iseq->body;
154  const struct iseq_compile_data *compile_data;
155 
156  /* TODO: should we count original_iseq? */
157 
158  if (body) {
159  struct rb_call_info_with_kwarg *ci_kw_entries = (struct rb_call_info_with_kwarg *)&body->ci_entries[body->ci_size];
160 
161  size += sizeof(struct rb_iseq_constant_body);
162  size += body->iseq_size * sizeof(VALUE);
163  size += body->line_info_size * sizeof(struct iseq_line_info_entry);
164  size += body->local_table_size * sizeof(ID);
165  if (body->catch_table) {
166  size += iseq_catch_table_bytes(body->catch_table->size);
167  }
168  size += (body->param.opt_num + 1) * sizeof(VALUE);
169  size += param_keyword_size(body->param.keyword);
170 
171  /* body->is_entries */
172  size += body->is_size * sizeof(union iseq_inline_storage_entry);
173 
174  /* body->ci_entries */
175  size += body->ci_size * sizeof(struct rb_call_info);
176  size += body->ci_kw_size * sizeof(struct rb_call_info_with_kwarg);
177 
178  /* body->cc_entries */
179  size += body->ci_size * sizeof(struct rb_call_cache);
180  size += body->ci_kw_size * sizeof(struct rb_call_cache);
181 
182  if (ci_kw_entries) {
183  unsigned int i;
184 
185  for (i = 0; i < body->ci_kw_size; i++) {
186  const struct rb_call_info_kw_arg *kw_arg = ci_kw_entries[i].kw_arg;
187 
188  if (kw_arg) {
189  size += rb_call_info_kw_arg_bytes(kw_arg->keyword_len);
190  }
191  }
192  }
193  }
194 
195  compile_data = ISEQ_COMPILE_DATA(iseq);
196  if (compile_data) {
197  struct iseq_compile_data_storage *cur;
198 
199  size += sizeof(struct iseq_compile_data);
200 
201  cur = compile_data->storage_head;
202  while (cur) {
203  size += cur->size + SIZEOF_ISEQ_COMPILE_DATA_STORAGE;
204  cur = cur->next;
205  }
206  }
207 
208  return size;
209 }
210 
211 static rb_iseq_t *
213 {
214  rb_iseq_t *iseq = iseq_imemo_alloc();
215  iseq->body = ZALLOC(struct rb_iseq_constant_body);
216  return iseq;
217 }
218 
219 static rb_iseq_location_t *
220 iseq_location_setup(rb_iseq_t *iseq, VALUE path, VALUE absolute_path, VALUE name, VALUE first_lineno)
221 {
222  rb_iseq_location_t *loc = &iseq->body->location;
223  RB_OBJ_WRITE(iseq, &loc->path, path);
224  if (RTEST(absolute_path) && rb_str_cmp(path, absolute_path) == 0) {
225  RB_OBJ_WRITE(iseq, &loc->absolute_path, path);
226  }
227  else {
228  RB_OBJ_WRITE(iseq, &loc->absolute_path, absolute_path);
229  }
230  RB_OBJ_WRITE(iseq, &loc->label, name);
231  RB_OBJ_WRITE(iseq, &loc->base_label, name);
232  loc->first_lineno = first_lineno;
233  return loc;
234 }
235 
236 static void
237 set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
238 {
239  const VALUE type = iseq->body->type;
240 
241  /* set class nest stack */
242  if (type == ISEQ_TYPE_TOP) {
243  iseq->body->local_iseq = iseq;
244  }
245  else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
246  iseq->body->local_iseq = iseq;
247  }
248  else if (piseq) {
249  iseq->body->local_iseq = piseq->body->local_iseq;
250  }
251 
252  if (piseq) {
253  iseq->body->parent_iseq = piseq;
254  }
255 
256  if (type == ISEQ_TYPE_MAIN) {
257  iseq->body->local_iseq = iseq;
258  }
259 }
260 
261 void
263 {
264  /* TODO: check dedup */
265  rb_ary_push(ISEQ_MARK_ARY(iseq), obj);
266 }
267 
268 static VALUE
270  VALUE name, VALUE path, VALUE absolute_path, VALUE first_lineno,
271  const rb_iseq_t *parent, enum iseq_type type,
273 {
275 
276  iseq->body->type = type;
277  set_relation(iseq, parent);
278 
279  name = rb_fstring(name);
280  path = rb_fstring(path);
281  if (RTEST(absolute_path)) absolute_path = rb_fstring(absolute_path);
282  iseq_location_setup(iseq, path, absolute_path, name, first_lineno);
283  if (iseq != iseq->body->local_iseq) {
285  }
286  RB_OBJ_WRITE(iseq, &iseq->body->mark_ary, iseq_mark_ary_create(0));
287 
291 
292  ISEQ_COMPILE_DATA(iseq)->storage_head = ISEQ_COMPILE_DATA(iseq)->storage_current =
293  (struct iseq_compile_data_storage *)
296 
297  RB_OBJ_WRITE(iseq, &ISEQ_COMPILE_DATA(iseq)->catch_table_ary, rb_ary_tmp_new(3));
298  ISEQ_COMPILE_DATA(iseq)->storage_head->pos = 0;
299  ISEQ_COMPILE_DATA(iseq)->storage_head->next = 0;
300  ISEQ_COMPILE_DATA(iseq)->storage_head->size =
302  ISEQ_COMPILE_DATA(iseq)->option = option;
303  ISEQ_COMPILE_DATA(iseq)->last_coverable_line = -1;
304 
305  ISEQ_COMPILE_DATA(iseq)->ivar_cache_table = NULL;
306 
307  if (option->coverage_enabled) {
308  VALUE coverages = rb_get_coverages();
309  if (RTEST(coverages)) {
310  coverage = rb_hash_lookup(coverages, path);
311  if (NIL_P(coverage)) coverage = Qfalse;
312  }
313  }
314  ISEQ_COVERAGE_SET(iseq, coverage);
315 
316  return Qtrue;
317 }
318 
319 static VALUE
321 {
322  struct iseq_compile_data *data = ISEQ_COMPILE_DATA(iseq);
323  VALUE err = data->err_info;
324  ISEQ_COMPILE_DATA(iseq) = 0;
325  compile_data_free(data);
326 
327  if (RTEST(err)) {
328  rb_funcallv(err, rb_intern("set_backtrace"), 1, &iseq->body->location.path);
329  rb_exc_raise(err);
330  }
331  return Qtrue;
332 }
333 
335  OPT_INLINE_CONST_CACHE, /* int inline_const_cache; */
336  OPT_PEEPHOLE_OPTIMIZATION, /* int peephole_optimization; */
337  OPT_TAILCALL_OPTIMIZATION, /* int tailcall_optimization */
338  OPT_SPECIALISED_INSTRUCTION, /* int specialized_instruction; */
339  OPT_OPERANDS_UNIFICATION, /* int operands_unification; */
340  OPT_INSTRUCTIONS_UNIFICATION, /* int instructions_unification; */
341  OPT_STACK_CACHING, /* int stack_caching; */
342  OPT_TRACE_INSTRUCTION, /* int trace_instruction */
345  TRUE, /* coverage_enabled */
346 };
347 
349 
350 static void
352 {
353 #define SET_COMPILE_OPTION(o, h, mem) \
354  { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
355  if (flag == Qtrue) { (o)->mem = 1; } \
356  else if (flag == Qfalse) { (o)->mem = 0; } \
357  }
358 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
359  { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
360  if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
361  }
362  SET_COMPILE_OPTION(option, opt, inline_const_cache);
363  SET_COMPILE_OPTION(option, opt, peephole_optimization);
364  SET_COMPILE_OPTION(option, opt, tailcall_optimization);
365  SET_COMPILE_OPTION(option, opt, specialized_instruction);
366  SET_COMPILE_OPTION(option, opt, operands_unification);
367  SET_COMPILE_OPTION(option, opt, instructions_unification);
368  SET_COMPILE_OPTION(option, opt, stack_caching);
369  SET_COMPILE_OPTION(option, opt, trace_instruction);
370  SET_COMPILE_OPTION(option, opt, frozen_string_literal);
371  SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
372  SET_COMPILE_OPTION(option, opt, coverage_enabled);
373  SET_COMPILE_OPTION_NUM(option, opt, debug_level);
374 #undef SET_COMPILE_OPTION
375 #undef SET_COMPILE_OPTION_NUM
376 }
377 
378 void
380 {
381  Check_Type(opt, T_HASH);
382  set_compile_option_from_hash(option, opt);
383 }
384 
385 static void
387 {
388  if (opt == Qnil) {
389  *option = COMPILE_OPTION_DEFAULT;
390  }
391  else if (opt == Qfalse) {
392  *option = COMPILE_OPTION_FALSE;
393  }
394  else if (opt == Qtrue) {
395  int i;
396  for (i = 0; i < (int)(sizeof(rb_compile_option_t) / sizeof(int)); ++i)
397  ((int *)option)[i] = 1;
398  }
399  else if (RB_TYPE_P(opt, T_HASH)) {
400  *option = COMPILE_OPTION_DEFAULT;
401  set_compile_option_from_hash(option, opt);
402  }
403  else {
404  rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
405  }
406 }
407 
408 static VALUE
410 {
411  VALUE opt = rb_hash_new();
412 #define SET_COMPILE_OPTION(o, h, mem) \
413  rb_hash_aset((h), ID2SYM(rb_intern(#mem)), (o)->mem ? Qtrue : Qfalse)
414 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
415  rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
416  {
417  SET_COMPILE_OPTION(option, opt, inline_const_cache);
418  SET_COMPILE_OPTION(option, opt, peephole_optimization);
419  SET_COMPILE_OPTION(option, opt, tailcall_optimization);
420  SET_COMPILE_OPTION(option, opt, specialized_instruction);
421  SET_COMPILE_OPTION(option, opt, operands_unification);
422  SET_COMPILE_OPTION(option, opt, instructions_unification);
423  SET_COMPILE_OPTION(option, opt, stack_caching);
424  SET_COMPILE_OPTION(option, opt, trace_instruction);
425  SET_COMPILE_OPTION(option, opt, frozen_string_literal);
426  SET_COMPILE_OPTION(option, opt, debug_frozen_string_literal);
427  SET_COMPILE_OPTION(option, opt, coverage_enabled);
428  SET_COMPILE_OPTION_NUM(option, opt, debug_level);
429  }
430 #undef SET_COMPILE_OPTION
431 #undef SET_COMPILE_OPTION_NUM
432  return opt;
433 }
434 
435 rb_iseq_t *
436 rb_iseq_new(NODE *node, VALUE name, VALUE path, VALUE absolute_path,
437  const rb_iseq_t *parent, enum iseq_type type)
438 {
439  return rb_iseq_new_with_opt(node, name, path, absolute_path, INT2FIX(0), parent, type,
440  &COMPILE_OPTION_DEFAULT);
441 }
442 
443 rb_iseq_t *
444 rb_iseq_new_top(NODE *node, VALUE name, VALUE path, VALUE absolute_path, const rb_iseq_t *parent)
445 {
446  return rb_iseq_new_with_opt(node, name, path, absolute_path, INT2FIX(0), parent, ISEQ_TYPE_TOP,
447  &COMPILE_OPTION_DEFAULT);
448 }
449 
450 rb_iseq_t *
451 rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path, const rb_iseq_t *parent)
452 {
453  return rb_iseq_new_with_opt(node, rb_fstring_cstr("<main>"),
454  path, absolute_path, INT2FIX(0),
455  parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
456 }
457 
458 static inline rb_iseq_t *
460 {
461  if (rb_respond_to(rb_cISeq, rb_intern("translate"))) {
462  VALUE v1 = iseqw_new(iseq);
463  VALUE v2 = rb_funcall(rb_cISeq, rb_intern("translate"), 1, v1);
464  if (v1 != v2 && CLASS_OF(v2) == rb_cISeq) {
465  iseq = (rb_iseq_t *)iseqw_check(v2);
466  }
467  }
468 
469  return iseq;
470 }
471 
472 rb_iseq_t *
473 rb_iseq_new_with_opt(NODE *node, VALUE name, VALUE path, VALUE absolute_path,
474  VALUE first_lineno, const rb_iseq_t *parent,
475  enum iseq_type type, const rb_compile_option_t *option)
476 {
477  /* TODO: argument check */
478  rb_iseq_t *iseq = iseq_alloc();
479 
480  if (!option) option = &COMPILE_OPTION_DEFAULT;
481  prepare_iseq_build(iseq, name, path, absolute_path, first_lineno, parent, type, option);
482 
483  rb_iseq_compile_node(iseq, node);
484  cleanup_iseq_build(iseq);
485 
486  return iseq_translate(iseq);
487 }
488 
489 const rb_iseq_t *
491 {
492  VALUE iseqv = rb_check_funcall(rb_cISeq, rb_intern("load_iseq"), 1, &fname);
493 
494  if (!SPECIAL_CONST_P(iseqv) && RBASIC_CLASS(iseqv) == rb_cISeq) {
495  return iseqw_check(iseqv);
496  }
497 
498  return NULL;
499 }
500 
501 #define CHECK_ARRAY(v) rb_convert_type((v), T_ARRAY, "Array", "to_ary")
502 #define CHECK_HASH(v) rb_convert_type((v), T_HASH, "Hash", "to_hash")
503 #define CHECK_STRING(v) rb_convert_type((v), T_STRING, "String", "to_str")
504 #define CHECK_SYMBOL(v) rb_convert_type((v), T_SYMBOL, "Symbol", "to_sym")
505 static inline VALUE CHECK_INTEGER(VALUE v) {(void)NUM2LONG(v); return v;}
506 
507 static enum iseq_type
509 {
510  const ID id_top = rb_intern("top");
511  const ID id_method = rb_intern("method");
512  const ID id_block = rb_intern("block");
513  const ID id_class = rb_intern("class");
514  const ID id_rescue = rb_intern("rescue");
515  const ID id_ensure = rb_intern("ensure");
516  const ID id_eval = rb_intern("eval");
517  const ID id_main = rb_intern("main");
518  const ID id_defined_guard = rb_intern("defined_guard");
519  /* ensure all symbols are static or pinned down before
520  * conversion */
521  const ID typeid = rb_check_id(&type);
522  if (typeid == id_top) return ISEQ_TYPE_TOP;
523  if (typeid == id_method) return ISEQ_TYPE_METHOD;
524  if (typeid == id_block) return ISEQ_TYPE_BLOCK;
525  if (typeid == id_class) return ISEQ_TYPE_CLASS;
526  if (typeid == id_rescue) return ISEQ_TYPE_RESCUE;
527  if (typeid == id_ensure) return ISEQ_TYPE_ENSURE;
528  if (typeid == id_eval) return ISEQ_TYPE_EVAL;
529  if (typeid == id_main) return ISEQ_TYPE_MAIN;
530  if (typeid == id_defined_guard) return ISEQ_TYPE_DEFINED_GUARD;
531  return (enum iseq_type)-1;
532 }
533 
534 static VALUE
535 iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
536 {
537  rb_iseq_t *iseq = iseq_alloc();
538 
539  VALUE magic, version1, version2, format_type, misc;
540  VALUE name, path, absolute_path, first_lineno;
541  VALUE type, body, locals, params, exception;
542 
543  st_data_t iseq_type;
545  int i = 0;
546 
547  /* [magic, major_version, minor_version, format_type, misc,
548  * label, path, first_lineno,
549  * type, locals, args, exception_table, body]
550  */
551 
552  data = CHECK_ARRAY(data);
553 
554  magic = CHECK_STRING(rb_ary_entry(data, i++));
555  version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
556  version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
557  format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
558  misc = CHECK_HASH(rb_ary_entry(data, i++));
559  ((void)magic, (void)version1, (void)version2, (void)format_type);
560 
561  name = CHECK_STRING(rb_ary_entry(data, i++));
562  path = CHECK_STRING(rb_ary_entry(data, i++));
563  absolute_path = rb_ary_entry(data, i++);
564  absolute_path = NIL_P(absolute_path) ? Qnil : CHECK_STRING(absolute_path);
565  first_lineno = CHECK_INTEGER(rb_ary_entry(data, i++));
566 
567  type = CHECK_SYMBOL(rb_ary_entry(data, i++));
568  locals = CHECK_ARRAY(rb_ary_entry(data, i++));
569  params = CHECK_HASH(rb_ary_entry(data, i++));
570  exception = CHECK_ARRAY(rb_ary_entry(data, i++));
571  body = CHECK_ARRAY(rb_ary_entry(data, i++));
572 
573  iseq->body->local_iseq = iseq;
574 
575  iseq_type = iseq_type_from_sym(type);
576  if (iseq_type == (enum iseq_type)-1) {
577  rb_raise(rb_eTypeError, "unsupport type: :%"PRIsVALUE, rb_sym2str(type));
578  }
579 
580  make_compile_option(&option, opt);
581  option.peephole_optimization = FALSE; /* because peephole optimization can modify original iseq */
582  prepare_iseq_build(iseq, name, path, absolute_path, first_lineno,
583  parent, (enum iseq_type)iseq_type, &option);
584 
585  rb_iseq_build_from_ary(iseq, misc, locals, params, exception, body);
586 
587  cleanup_iseq_build(iseq);
588 
589  return iseqw_new(iseq);
590 }
591 
592 /*
593  * :nodoc:
594  */
595 static VALUE
597 {
598  VALUE data, opt=Qnil;
599  rb_scan_args(argc, argv, "11", &data, &opt);
600  return iseq_load(data, NULL, opt);
601 }
602 
603 VALUE
604 rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
605 {
606  return iseq_load(data, RTEST(parent) ? (rb_iseq_t *)parent : NULL, opt);
607 }
608 
609 rb_iseq_t *
610 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE absolute_path, VALUE line, const struct rb_block *base_block, VALUE opt)
611 {
612  rb_thread_t *th = GET_THREAD();
613  rb_iseq_t *iseq = NULL;
614  const rb_iseq_t *const parent = base_block ? vm_block_iseq(base_block) : NULL;
616  const enum iseq_type type = parent ? ISEQ_TYPE_EVAL : ISEQ_TYPE_TOP;
617 #if !defined(__GNUC__) || (__GNUC__ == 4 && __GNUC_MINOR__ == 8)
618 # define INITIALIZED volatile /* suppress warnings by gcc 4.8 */
619 #else
620 # define INITIALIZED /* volatile */
621 #endif
622  NODE *(*parse)(VALUE vparser, VALUE fname, VALUE file, int start);
623  int ln;
624  NODE *INITIALIZED node;
625 
626  /* safe results first */
627  make_compile_option(&option, opt);
628  ln = NUM2INT(line);
629  StringValueCStr(file);
630  if (RB_TYPE_P(src, T_FILE)) {
632  }
633  else {
635  StringValue(src);
636  }
637  {
638  const VALUE parser = rb_parser_new();
639  rb_parser_set_context(parser, base_block, FALSE);
640  node = (*parse)(parser, file, src, ln);
641  }
642 
643  if (!node) {
644  rb_exc_raise(th->errinfo);
645  }
646  else {
647  INITIALIZED VALUE label = parent ?
648  parent->body->location.label :
649  rb_fstring_cstr("<compiled>");
650  iseq = rb_iseq_new_with_opt(node, label, file, absolute_path, line,
651  parent, type, &option);
652  }
653 
654  return iseq;
655 }
656 
657 rb_iseq_t *
659 {
660  return rb_iseq_compile_with_option(src, file, Qnil, line, 0, Qnil);
661 }
662 
663 rb_iseq_t *
664 rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, const struct rb_block *base_block)
665 {
666  return rb_iseq_compile_with_option(src, file, Qnil, line, base_block, Qnil);
667 }
668 
669 VALUE
671 {
672  return iseq->body->location.path;
673 }
674 
675 VALUE
677 {
678  return iseq->body->location.absolute_path;
679 }
680 
681 VALUE
683 {
684  return iseq->body->location.label;
685 }
686 
687 VALUE
689 {
690  return iseq->body->location.base_label;
691 }
692 
693 VALUE
695 {
696  return iseq->body->location.first_lineno;
697 }
698 
699 VALUE
701 {
702  const rb_iseq_t *local_iseq;
703 
704  local_iseq = iseq->body->local_iseq;
705 
706  if (local_iseq->body->type == ISEQ_TYPE_METHOD) {
707  return local_iseq->body->location.base_label;
708  }
709  else {
710  return Qnil;
711  }
712 }
713 
714 VALUE
716 {
717  return ISEQ_COVERAGE(iseq);
718 }
719 
720 /* define wrapper class methods (RubyVM::InstructionSequence) */
721 
722 static void
723 iseqw_mark(void *ptr)
724 {
725  rb_gc_mark((VALUE)ptr);
726 }
727 
728 static size_t
729 iseqw_memsize(const void *ptr)
730 {
731  return iseq_memsize((const rb_iseq_t *)ptr);
732 }
733 
735  "T_IMEMO/iseq",
738 };
739 
740 static VALUE
741 iseqw_new(const rb_iseq_t *iseq)
742 {
743  union { const rb_iseq_t *in; void *out; } deconst;
744  VALUE obj;
745 
746  deconst.in = iseq;
747  obj = TypedData_Wrap_Struct(rb_cISeq, &iseqw_data_type, deconst.out);
748  RB_OBJ_WRITTEN(obj, Qundef, iseq);
749 
750  return obj;
751 }
752 
753 VALUE
755 {
756  return iseqw_new(iseq);
757 }
758 
759 /*
760  * call-seq:
761  * InstructionSequence.compile(source[, file[, path[, line[, options]]]]) -> iseq
762  * InstructionSequence.new(source[, file[, path[, line[, options]]]]) -> iseq
763  *
764  * Takes +source+, a String of Ruby code and compiles it to an
765  * InstructionSequence.
766  *
767  * Optionally takes +file+, +path+, and +line+ which describe the filename,
768  * absolute path and first line number of the ruby code in +source+ which are
769  * metadata attached to the returned +iseq+.
770  *
771  * +options+, which can be +true+, +false+ or a +Hash+, is used to
772  * modify the default behavior of the Ruby iseq compiler.
773  *
774  * For details regarding valid compile options see ::compile_option=.
775  *
776  * RubyVM::InstructionSequence.compile("a = 1 + 2")
777  * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
778  *
779  */
780 static VALUE
782 {
783  VALUE src, file = Qnil, path = Qnil, line = INT2FIX(1), opt = Qnil;
784  int i;
785 
786  rb_secure(1);
787 
788  i = rb_scan_args(argc, argv, "1*:", &src, NULL, &opt);
789  if (i > 4+NIL_P(opt)) rb_error_arity(argc, 1, 5);
790  switch (i) {
791  case 5: opt = argv[--i];
792  case 4: line = argv[--i];
793  case 3: path = argv[--i];
794  case 2: file = argv[--i];
795  }
796  if (NIL_P(file)) file = rb_fstring_cstr("<compiled>");
797  if (NIL_P(line)) line = INT2FIX(1);
798 
799  return iseqw_new(rb_iseq_compile_with_option(src, file, path, line, 0, opt));
800 }
801 
802 /*
803  * call-seq:
804  * InstructionSequence.compile_file(file[, options]) -> iseq
805  *
806  * Takes +file+, a String with the location of a Ruby source file, reads,
807  * parses and compiles the file, and returns +iseq+, the compiled
808  * InstructionSequence with source location metadata set.
809  *
810  * Optionally takes +options+, which can be +true+, +false+ or a +Hash+, to
811  * modify the default behavior of the Ruby iseq compiler.
812  *
813  * For details regarding valid compile options see ::compile_option=.
814  *
815  * # /tmp/hello.rb
816  * puts "Hello, world!"
817  *
818  * # elsewhere
819  * RubyVM::InstructionSequence.compile_file("/tmp/hello.rb")
820  * #=> <RubyVM::InstructionSequence:<main>@/tmp/hello.rb>
821  */
822 static VALUE
824 {
825  VALUE file, line = INT2FIX(1), opt = Qnil;
826  VALUE parser, f, exc = Qnil;
827  NODE *node;
829  int i;
830 
831  rb_secure(1);
832  i = rb_scan_args(argc, argv, "1*:", &file, NULL, &opt);
833  if (i > 1+NIL_P(opt)) rb_error_arity(argc, 1, 2);
834  switch (i) {
835  case 2: opt = argv[--i];
836  }
837  FilePathValue(file);
838  file = rb_fstring(file); /* rb_io_t->pathv gets frozen anyways */
839 
840  f = rb_file_open_str(file, "r");
841 
842  parser = rb_parser_new();
843  rb_parser_set_context(parser, NULL, FALSE);
844  node = rb_parser_compile_file_path(parser, file, f, NUM2INT(line));
845  if (!node) exc = GET_THREAD()->errinfo;
846 
847  rb_io_close(f);
848  if (!node) rb_exc_raise(exc);
849 
850  make_compile_option(&option, opt);
851 
852  return iseqw_new(rb_iseq_new_with_opt(node, rb_fstring_cstr("<main>"),
853  file,
854  rb_realpath_internal(Qnil, file, 1),
855  line, NULL, ISEQ_TYPE_TOP, &option));
856 }
857 
858 /*
859  * call-seq:
860  * InstructionSequence.compile_option = options
861  *
862  * Sets the default values for various optimizations in the Ruby iseq
863  * compiler.
864  *
865  * Possible values for +options+ include +true+, which enables all options,
866  * +false+ which disables all options, and +nil+ which leaves all options
867  * unchanged.
868  *
869  * You can also pass a +Hash+ of +options+ that you want to change, any
870  * options not present in the hash will be left unchanged.
871  *
872  * Possible option names (which are keys in +options+) which can be set to
873  * +true+ or +false+ include:
874  *
875  * * +:inline_const_cache+
876  * * +:instructions_unification+
877  * * +:operands_unification+
878  * * +:peephole_optimization+
879  * * +:specialized_instruction+
880  * * +:stack_caching+
881  * * +:tailcall_optimization+
882  * * +:trace_instruction+
883  *
884  * Additionally, +:debug_level+ can be set to an integer.
885  *
886  * These default options can be overwritten for a single run of the iseq
887  * compiler by passing any of the above values as the +options+ parameter to
888  * ::new, ::compile and ::compile_file.
889  */
890 static VALUE
892 {
894  rb_secure(1);
895  make_compile_option(&option, opt);
896  COMPILE_OPTION_DEFAULT = option;
897  return opt;
898 }
899 
900 /*
901  * call-seq:
902  * InstructionSequence.compile_option -> options
903  *
904  * Returns a hash of default options used by the Ruby iseq compiler.
905  *
906  * For details, see InstructionSequence.compile_option=.
907  */
908 static VALUE
910 {
911  return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
912 }
913 
914 static const rb_iseq_t *
916 {
917  rb_iseq_t *iseq = DATA_PTR(iseqw);
918 
919  if (!iseq->body) {
921  }
922 
923  if (!iseq->body->location.label) {
924  rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
925  }
926  return iseq;
927 }
928 
929 const rb_iseq_t *
931 {
932  return iseqw_check(iseqw);
933 }
934 
935 /*
936  * call-seq:
937  * iseq.eval -> obj
938  *
939  * Evaluates the instruction sequence and returns the result.
940  *
941  * RubyVM::InstructionSequence.compile("1 + 2").eval #=> 3
942  */
943 static VALUE
945 {
946  rb_secure(1);
947  return rb_iseq_eval(iseqw_check(self));
948 }
949 
950 /*
951  * Returns a human-readable string representation of this instruction
952  * sequence, including the #label and #path.
953  */
954 static VALUE
956 {
957  const rb_iseq_t *iseq = iseqw_check(self);
958 
959  if (!iseq->body->location.label) {
960  return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self));
961  }
962  else {
963  return rb_sprintf("<%s:%s@%s>",
964  rb_obj_classname(self),
966  }
967 }
968 
969 /*
970  * Returns the path of this instruction sequence.
971  *
972  * <code><compiled></code> if the iseq was evaluated from a string.
973  *
974  * For example, using irb:
975  *
976  * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
977  * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
978  * iseq.path
979  * #=> "<compiled>"
980  *
981  * Using ::compile_file:
982  *
983  * # /tmp/method.rb
984  * def hello
985  * puts "hello, world"
986  * end
987  *
988  * # in irb
989  * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
990  * > iseq.path #=> /tmp/method.rb
991  */
992 static VALUE
994 {
995  return rb_iseq_path(iseqw_check(self));
996 }
997 
998 /*
999  * Returns the absolute path of this instruction sequence.
1000  *
1001  * +nil+ if the iseq was evaluated from a string.
1002  *
1003  * For example, using ::compile_file:
1004  *
1005  * # /tmp/method.rb
1006  * def hello
1007  * puts "hello, world"
1008  * end
1009  *
1010  * # in irb
1011  * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1012  * > iseq.absolute_path #=> /tmp/method.rb
1013  */
1014 static VALUE
1016 {
1017  return rb_iseq_absolute_path(iseqw_check(self));
1018 }
1019 
1020 /* Returns the label of this instruction sequence.
1021  *
1022  * <code><main></code> if it's at the top level, <code><compiled></code> if it
1023  * was evaluated from a string.
1024  *
1025  * For example, using irb:
1026  *
1027  * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1028  * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1029  * iseq.label
1030  * #=> "<compiled>"
1031  *
1032  * Using ::compile_file:
1033  *
1034  * # /tmp/method.rb
1035  * def hello
1036  * puts "hello, world"
1037  * end
1038  *
1039  * # in irb
1040  * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1041  * > iseq.label #=> <main>
1042  */
1043 static VALUE
1045 {
1046  return rb_iseq_label(iseqw_check(self));
1047 }
1048 
1049 /* Returns the base label of this instruction sequence.
1050  *
1051  * For example, using irb:
1052  *
1053  * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1054  * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1055  * iseq.base_label
1056  * #=> "<compiled>"
1057  *
1058  * Using ::compile_file:
1059  *
1060  * # /tmp/method.rb
1061  * def hello
1062  * puts "hello, world"
1063  * end
1064  *
1065  * # in irb
1066  * > iseq = RubyVM::InstructionSequence.compile_file('/tmp/method.rb')
1067  * > iseq.base_label #=> <main>
1068  */
1069 static VALUE
1071 {
1072  return rb_iseq_base_label(iseqw_check(self));
1073 }
1074 
1075 /* Returns the number of the first source line where the instruction sequence
1076  * was loaded from.
1077  *
1078  * For example, using irb:
1079  *
1080  * iseq = RubyVM::InstructionSequence.compile('num = 1 + 2')
1081  * #=> <RubyVM::InstructionSequence:<compiled>@<compiled>>
1082  * iseq.first_lineno
1083  * #=> 1
1084  */
1085 static VALUE
1087 {
1088  return rb_iseq_first_lineno(iseqw_check(self));
1089 }
1090 
1091 static VALUE iseq_data_to_ary(const rb_iseq_t *iseq);
1092 
1093 /*
1094  * call-seq:
1095  * iseq.to_a -> ary
1096  *
1097  * Returns an Array with 14 elements representing the instruction sequence
1098  * with the following data:
1099  *
1100  * [magic]
1101  * A string identifying the data format. <b>Always
1102  * +YARVInstructionSequence/SimpleDataFormat+.</b>
1103  *
1104  * [major_version]
1105  * The major version of the instruction sequence.
1106  *
1107  * [minor_version]
1108  * The minor version of the instruction sequence.
1109  *
1110  * [format_type]
1111  * A number identifying the data format. <b>Always 1</b>.
1112  *
1113  * [misc]
1114  * A hash containing:
1115  *
1116  * [+:arg_size+]
1117  * the total number of arguments taken by the method or the block (0 if
1118  * _iseq_ doesn't represent a method or block)
1119  * [+:local_size+]
1120  * the number of local variables + 1
1121  * [+:stack_max+]
1122  * used in calculating the stack depth at which a SystemStackError is
1123  * thrown.
1124  *
1125  * [#label]
1126  * The name of the context (block, method, class, module, etc.) that this
1127  * instruction sequence belongs to.
1128  *
1129  * <code><main></code> if it's at the top level, <code><compiled></code> if
1130  * it was evaluated from a string.
1131  *
1132  * [#path]
1133  * The relative path to the Ruby file where the instruction sequence was
1134  * loaded from.
1135  *
1136  * <code><compiled></code> if the iseq was evaluated from a string.
1137  *
1138  * [#absolute_path]
1139  * The absolute path to the Ruby file where the instruction sequence was
1140  * loaded from.
1141  *
1142  * +nil+ if the iseq was evaluated from a string.
1143  *
1144  * [#first_lineno]
1145  * The number of the first source line where the instruction sequence was
1146  * loaded from.
1147  *
1148  * [type]
1149  * The type of the instruction sequence.
1150  *
1151  * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
1152  * +:ensure+, +:eval+, +:main+, and +:defined_guard+.
1153  *
1154  * [locals]
1155  * An array containing the names of all arguments and local variables as
1156  * symbols.
1157  *
1158  * [params]
1159  * An Hash object containing parameter information.
1160  *
1161  * More info about these values can be found in +vm_core.h+.
1162  *
1163  * [catch_table]
1164  * A list of exceptions and control flow operators (rescue, next, redo,
1165  * break, etc.).
1166  *
1167  * [bytecode]
1168  * An array of arrays containing the instruction names and operands that
1169  * make up the body of the instruction sequence.
1170  *
1171  * Note that this format is MRI specific and version dependent.
1172  *
1173  */
1174 static VALUE
1176 {
1177  const rb_iseq_t *iseq = iseqw_check(self);
1178  rb_secure(1);
1179  return iseq_data_to_ary(iseq);
1180 }
1181 
1182 /* TODO: search algorithm is brute force.
1183  this should be binary search or so. */
1184 
1185 static const struct iseq_line_info_entry *
1186 get_line_info(const rb_iseq_t *iseq, size_t pos)
1187 {
1188  size_t i = 0, size = iseq->body->line_info_size;
1189  const struct iseq_line_info_entry *table = iseq->body->line_info_table;
1190  const int debug = 0;
1191 
1192  if (debug) {
1193  printf("size: %"PRIuSIZE"\n", size);
1194  printf("table[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1195  i, table[i].position, table[i].line_no, pos);
1196  }
1197 
1198  if (size == 0) {
1199  return 0;
1200  }
1201  else if (size == 1) {
1202  return &table[0];
1203  }
1204  else {
1205  for (i=1; i<size; i++) {
1206  if (debug) printf("table[%"PRIuSIZE"]: position: %d, line: %d, pos: %"PRIuSIZE"\n",
1207  i, table[i].position, table[i].line_no, pos);
1208 
1209  if (table[i].position == pos) {
1210  return &table[i];
1211  }
1212  if (table[i].position > pos) {
1213  return &table[i-1];
1214  }
1215  }
1216  }
1217  return &table[i-1];
1218 }
1219 
1220 static unsigned int
1221 find_line_no(const rb_iseq_t *iseq, size_t pos)
1222 {
1223  const struct iseq_line_info_entry *entry = get_line_info(iseq, pos);
1224 
1225  if (entry) {
1226  return entry->line_no;
1227  }
1228  else {
1229  return 0;
1230  }
1231 }
1232 
1233 unsigned int
1234 rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
1235 {
1236  if (pos == 0) {
1237  return find_line_no(iseq, pos);
1238  }
1239  else {
1240  return find_line_no(iseq, pos - 1);
1241  }
1242 }
1243 
1244 static VALUE
1245 id_to_name(ID id, VALUE default_value)
1246 {
1247  VALUE str = rb_id2str(id);
1248  if (!str) {
1249  str = default_value;
1250  }
1251  else if (!rb_str_symname_p(str)) {
1252  str = rb_str_inspect(str);
1253  }
1254  return str;
1255 }
1256 
1257 VALUE
1259  VALUE insn, int op_no, VALUE op,
1260  int len, size_t pos, const VALUE *pnop, VALUE child)
1261 {
1262  const char *types = insn_op_types(insn);
1263  char type = types[op_no];
1264  VALUE ret = Qundef;
1265 
1266  switch (type) {
1267  case TS_OFFSET: /* LONG */
1268  ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
1269  break;
1270 
1271  case TS_NUM: /* ULONG */
1272  ret = rb_sprintf("%"PRIuVALUE, op);
1273  break;
1274 
1275  case TS_LINDEX:{
1276  if (insn == BIN(getlocal) || insn == BIN(setlocal)) {
1277  if (pnop) {
1278  const rb_iseq_t *diseq = iseq;
1279  VALUE level = *pnop, i;
1280  ID lid;
1281 
1282  for (i = 0; i < level; i++) {
1283  diseq = diseq->body->parent_iseq;
1284  }
1285  lid = diseq->body->local_table[diseq->body->local_table_size +
1286  VM_ENV_DATA_SIZE - 1 - op];
1287  ret = id_to_name(lid, INT2FIX('*'));
1288  }
1289  else {
1290  ret = rb_sprintf("%"PRIuVALUE, op);
1291  }
1292  }
1293  else {
1294  ret = rb_inspect(INT2FIX(op));
1295  }
1296  break;
1297  }
1298  case TS_ID: /* ID (symbol) */
1299  op = ID2SYM(op);
1300 
1301  case TS_VALUE: /* VALUE */
1302  op = obj_resurrect(op);
1303  ret = rb_inspect(op);
1304  if (CLASS_OF(op) == rb_cISeq) {
1305  if (child) {
1306  rb_ary_push(child, op);
1307  }
1308  }
1309  break;
1310 
1311  case TS_ISEQ: /* iseq */
1312  {
1313  if (op) {
1314  const rb_iseq_t *iseq = rb_iseq_check((rb_iseq_t *)op);
1315  ret = iseq->body->location.label;
1316  if (child) {
1317  rb_ary_push(child, (VALUE)iseq);
1318  }
1319  }
1320  else {
1321  ret = rb_str_new2("nil");
1322  }
1323  break;
1324  }
1325  case TS_GENTRY:
1326  {
1327  struct rb_global_entry *entry = (struct rb_global_entry *)op;
1328  ret = rb_str_dup(rb_id2str(entry->id));
1329  }
1330  break;
1331 
1332  case TS_IC:
1333  ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - iseq->body->is_entries);
1334  break;
1335 
1336  case TS_CALLINFO:
1337  {
1338  struct rb_call_info *ci = (struct rb_call_info *)op;
1339  VALUE ary = rb_ary_new();
1340 
1341  if (ci->mid) {
1342  rb_ary_push(ary, rb_sprintf("mid:%"PRIsVALUE, rb_id2str(ci->mid)));
1343  }
1344 
1345  rb_ary_push(ary, rb_sprintf("argc:%d", ci->orig_argc));
1346 
1347  if (ci->flag & VM_CALL_KWARG) {
1348  struct rb_call_info_kw_arg *kw_args = ((struct rb_call_info_with_kwarg *)ci)->kw_arg;
1349  VALUE kw_ary = rb_ary_new_from_values(kw_args->keyword_len, kw_args->keywords);
1350  rb_ary_push(ary, rb_sprintf("kw:[%"PRIsVALUE"]", rb_ary_join(kw_ary, rb_str_new2(","))));
1351  }
1352 
1353  if (ci->flag) {
1354  VALUE flags = rb_ary_new();
1355  if (ci->flag & VM_CALL_ARGS_SPLAT) rb_ary_push(flags, rb_str_new2("ARGS_SPLAT"));
1356  if (ci->flag & VM_CALL_ARGS_BLOCKARG) rb_ary_push(flags, rb_str_new2("ARGS_BLOCKARG"));
1357  if (ci->flag & VM_CALL_FCALL) rb_ary_push(flags, rb_str_new2("FCALL"));
1358  if (ci->flag & VM_CALL_VCALL) rb_ary_push(flags, rb_str_new2("VCALL"));
1359  if (ci->flag & VM_CALL_TAILCALL) rb_ary_push(flags, rb_str_new2("TAILCALL"));
1360  if (ci->flag & VM_CALL_SUPER) rb_ary_push(flags, rb_str_new2("SUPER"));
1361  if (ci->flag & VM_CALL_KWARG) rb_ary_push(flags, rb_str_new2("KWARG"));
1362  if (ci->flag & VM_CALL_OPT_SEND) rb_ary_push(flags, rb_str_new2("SNED")); /* maybe not reachable */
1363  if (ci->flag & VM_CALL_ARGS_SIMPLE) rb_ary_push(flags, rb_str_new2("ARGS_SIMPLE")); /* maybe not reachable */
1364  rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
1365  }
1366  ret = rb_sprintf("<callinfo!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
1367  }
1368  break;
1369 
1370  case TS_CALLCACHE:
1371  ret = rb_str_new2("<callcache>");
1372  break;
1373 
1374  case TS_CDHASH:
1375  ret = rb_str_new2("<cdhash>");
1376  break;
1377 
1378  case TS_FUNCPTR:
1379  {
1380 #ifdef HAVE_DLADDR
1381  Dl_info info;
1382  if (dladdr((void *)op, &info) && info.dli_sname) {
1383  ret = rb_str_new_cstr(info.dli_sname);
1384  break;
1385  }
1386 #endif
1387  ret = rb_str_new2("<funcptr>");
1388  }
1389  break;
1390 
1391  default:
1392  rb_bug("insn_operand_intern: unknown operand type: %c", type);
1393  }
1394  return ret;
1395 }
1396 
1401 int
1402 rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos,
1403  const rb_iseq_t *iseq, VALUE child)
1404 {
1405  VALUE insn = code[pos];
1406  int len = insn_len(insn);
1407  int j;
1408  const char *types = insn_op_types(insn);
1409  VALUE str = rb_str_new(0, 0);
1410  const char *insn_name_buff;
1411 
1412  insn_name_buff = insn_name(insn);
1413  if (1) {
1414  rb_str_catf(str, "%04"PRIuSIZE" %-16s ", pos, insn_name_buff);
1415  }
1416  else {
1417  rb_str_catf(str, "%04"PRIuSIZE" %-16.*s ", pos,
1418  (int)strcspn(insn_name_buff, "_"), insn_name_buff);
1419  }
1420 
1421  for (j = 0; types[j]; j++) {
1422  const char *types = insn_op_types(insn);
1423  VALUE opstr = rb_insn_operand_intern(iseq, insn, j, code[pos + j + 1],
1424  len, pos, &code[pos + j + 2],
1425  child);
1426  rb_str_concat(str, opstr);
1427 
1428  if (types[j + 1]) {
1429  rb_str_cat2(str, ", ");
1430  }
1431  }
1432 
1433  {
1434  unsigned int line_no = find_line_no(iseq, pos);
1435  unsigned int prev = pos == 0 ? 0 : find_line_no(iseq, pos - 1);
1436  if (line_no && line_no != prev) {
1437  long slen = RSTRING_LEN(str);
1438  slen = (slen > 70) ? 0 : (70 - slen);
1439  str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
1440  }
1441  }
1442 
1443  if (ret) {
1444  rb_str_cat2(str, "\n");
1445  rb_str_concat(ret, str);
1446  }
1447  else {
1448  printf("%s\n", RSTRING_PTR(str));
1449  }
1450  return len;
1451 }
1452 
1453 static const char *
1454 catch_type(int type)
1455 {
1456  switch (type) {
1457  case CATCH_TYPE_RESCUE:
1458  return "rescue";
1459  case CATCH_TYPE_ENSURE:
1460  return "ensure";
1461  case CATCH_TYPE_RETRY:
1462  return "retry";
1463  case CATCH_TYPE_BREAK:
1464  return "break";
1465  case CATCH_TYPE_REDO:
1466  return "redo";
1467  case CATCH_TYPE_NEXT:
1468  return "next";
1469  default:
1470  rb_bug("unknown catch type (%d)", type);
1471  return 0;
1472  }
1473 }
1474 
1475 static VALUE
1477 {
1478  if (!iseq->body->location.label) {
1479  return rb_sprintf("#<ISeq: uninitialized>");
1480  }
1481  else {
1482  return rb_sprintf("#<ISeq:%s@%s>", RSTRING_PTR(iseq->body->location.label), RSTRING_PTR(iseq->body->location.path));
1483  }
1484 }
1485 
1486 VALUE
1488 {
1489  VALUE *code;
1490  VALUE str = rb_str_new(0, 0);
1491  VALUE child = rb_ary_tmp_new(3);
1492  unsigned int size;
1493  unsigned int i;
1494  long l;
1495  const ID *tbl;
1496  size_t n;
1497  enum {header_minlen = 72};
1498 
1499  rb_secure(1);
1500 
1501  size = iseq->body->iseq_size;
1502 
1503  rb_str_cat2(str, "== disasm: ");
1504 
1505  rb_str_concat(str, iseq_inspect(iseq));
1506  if ((l = RSTRING_LEN(str)) < header_minlen) {
1507  rb_str_resize(str, header_minlen);
1508  memset(RSTRING_PTR(str) + l, '=', header_minlen - l);
1509  }
1510  rb_str_cat2(str, "\n");
1511 
1512  /* show catch table information */
1513  if (iseq->body->catch_table) {
1514  rb_str_cat2(str, "== catch table\n");
1515  }
1516  if (iseq->body->catch_table) {
1517  for (i = 0; i < iseq->body->catch_table->size; i++) {
1518  const struct iseq_catch_table_entry *entry = &iseq->body->catch_table->entries[i];
1519  rb_str_catf(str,
1520  "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
1521  catch_type((int)entry->type), (int)entry->start,
1522  (int)entry->end, (int)entry->sp, (int)entry->cont);
1523  if (entry->iseq) {
1525  }
1526  }
1527  }
1528  if (iseq->body->catch_table) {
1529  rb_str_cat2(str, "|-------------------------------------"
1530  "-----------------------------------\n");
1531  }
1532 
1533  /* show local table information */
1534  tbl = iseq->body->local_table;
1535 
1536  if (tbl) {
1537  rb_str_catf(str,
1538  "local table (size: %d, argc: %d "
1539  "[opts: %d, rest: %d, post: %d, block: %d, kw: %d@%d, kwrest: %d])\n",
1540  iseq->body->local_table_size,
1541  iseq->body->param.lead_num,
1542  iseq->body->param.opt_num,
1543  iseq->body->param.flags.has_rest ? iseq->body->param.rest_start : -1,
1544  iseq->body->param.post_num,
1545  iseq->body->param.flags.has_block ? iseq->body->param.block_start : -1,
1546  iseq->body->param.flags.has_kw ? iseq->body->param.keyword->num : -1,
1547  iseq->body->param.flags.has_kw ? iseq->body->param.keyword->required_num : -1,
1548  iseq->body->param.flags.has_kwrest ? iseq->body->param.keyword->rest_start : -1);
1549 
1550  for (i = 0; i < iseq->body->local_table_size; i++) {
1551  int li = (int)i;
1552  long width;
1553  VALUE name = id_to_name(tbl[i], 0);
1554  char argi[0x100] = "";
1555  char opti[0x100] = "";
1556 
1557  if (iseq->body->param.flags.has_opt) {
1558  int argc = iseq->body->param.lead_num;
1559  int opts = iseq->body->param.opt_num;
1560  if (li >= argc && li < argc + opts) {
1561  snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
1562  iseq->body->param.opt_table[li - argc]);
1563  }
1564  }
1565 
1566  snprintf(argi, sizeof(argi), "%s%s%s%s%s", /* arg, opts, rest, post block */
1567  iseq->body->param.lead_num > li ? "Arg" : "",
1568  opti,
1569  (iseq->body->param.flags.has_rest && iseq->body->param.rest_start == li) ? "Rest" : "",
1570  (iseq->body->param.flags.has_post && iseq->body->param.post_start <= li && li < iseq->body->param.post_start + iseq->body->param.post_num) ? "Post" : "",
1571  (iseq->body->param.flags.has_block && iseq->body->param.block_start == li) ? "Block" : "");
1572 
1573  rb_str_catf(str, "[%2d] ", iseq->body->local_table_size - i);
1574  width = RSTRING_LEN(str) + 11;
1575  if (name)
1576  rb_str_append(str, name);
1577  else
1578  rb_str_cat2(str, "?");
1579  if (*argi) rb_str_catf(str, "<%s>", argi);
1580  if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
1581  }
1582  rb_str_cat2(str, "\n");
1583  }
1584 
1585  /* show each line */
1586  code = rb_iseq_original_iseq(iseq);
1587  for (n = 0; n < size;) {
1588  n += rb_iseq_disasm_insn(str, code, n, iseq, child);
1589  }
1590 
1591  for (l = 0; l < RARRAY_LEN(child); l++) {
1592  VALUE isv = rb_ary_entry(child, l);
1594  }
1595 
1596  return str;
1597 }
1598 
1599 /*
1600  * call-seq:
1601  * iseq.disasm -> str
1602  * iseq.disassemble -> str
1603  *
1604  * Returns the instruction sequence as a +String+ in human readable form.
1605  *
1606  * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
1607  *
1608  * Produces:
1609  *
1610  * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
1611  * 0000 trace 1 ( 1)
1612  * 0002 putobject 1
1613  * 0004 putobject 2
1614  * 0006 opt_plus <ic:1>
1615  * 0008 leave
1616  */
1617 static VALUE
1619 {
1620  return rb_iseq_disasm(iseqw_check(self));
1621 }
1622 
1623 /*
1624  * Returns the instruction sequence containing the given proc or method.
1625  *
1626  * For example, using irb:
1627  *
1628  * # a proc
1629  * > p = proc { num = 1 + 2 }
1630  * > RubyVM::InstructionSequence.of(p)
1631  * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
1632  *
1633  * # for a method
1634  * > def foo(bar); puts bar; end
1635  * > RubyVM::InstructionSequence.of(method(:foo))
1636  * > #=> <RubyVM::InstructionSequence:foo@(irb)>
1637  *
1638  * Using ::compile_file:
1639  *
1640  * # /tmp/iseq_of.rb
1641  * def hello
1642  * puts "hello, world"
1643  * end
1644  *
1645  * $a_global_proc = proc { str = 'a' + 'b' }
1646  *
1647  * # in irb
1648  * > require '/tmp/iseq_of.rb'
1649  *
1650  * # first the method hello
1651  * > RubyVM::InstructionSequence.of(method(:hello))
1652  * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
1653  *
1654  * # then the global proc
1655  * > RubyVM::InstructionSequence.of($a_global_proc)
1656  * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
1657  */
1658 static VALUE
1659 iseqw_s_of(VALUE klass, VALUE body)
1660 {
1661  const rb_iseq_t *iseq = NULL;
1662 
1663  rb_secure(1);
1664 
1665  if (rb_obj_is_proc(body)) {
1666  iseq = vm_proc_iseq(body);
1667 
1668  if (!rb_obj_is_iseq((VALUE)iseq)) {
1669  iseq = NULL;
1670  }
1671  }
1672  else {
1673  iseq = rb_method_iseq(body);
1674  }
1675 
1676  return iseq ? iseqw_new(iseq) : Qnil;
1677 }
1678 
1679 /*
1680  * call-seq:
1681  * InstructionSequence.disasm(body) -> str
1682  * InstructionSequence.disassemble(body) -> str
1683  *
1684  * Takes +body+, a Method or Proc object, and returns a String with the
1685  * human readable instructions for +body+.
1686  *
1687  * For a Method object:
1688  *
1689  * # /tmp/method.rb
1690  * def hello
1691  * puts "hello, world"
1692  * end
1693  *
1694  * puts RubyVM::InstructionSequence.disasm(method(:hello))
1695  *
1696  * Produces:
1697  *
1698  * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
1699  * 0000 trace 8 ( 1)
1700  * 0002 trace 1 ( 2)
1701  * 0004 putself
1702  * 0005 putstring "hello, world"
1703  * 0007 send :puts, 1, nil, 8, <ic:0>
1704  * 0013 trace 16 ( 3)
1705  * 0015 leave ( 2)
1706  *
1707  * For a Proc:
1708  *
1709  * # /tmp/proc.rb
1710  * p = proc { num = 1 + 2 }
1711  * puts RubyVM::InstructionSequence.disasm(p)
1712  *
1713  * Produces:
1714  *
1715  * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
1716  * == catch table
1717  * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
1718  * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
1719  * |------------------------------------------------------------------------
1720  * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
1721  * [ 2] num
1722  * 0000 trace 1 ( 1)
1723  * 0002 putobject 1
1724  * 0004 putobject 2
1725  * 0006 opt_plus <ic:1>
1726  * 0008 dup
1727  * 0009 setlocal num, 0
1728  * 0012 leave
1729  *
1730  */
1731 static VALUE
1733 {
1734  VALUE iseqw = iseqw_s_of(klass, body);
1735  return NIL_P(iseqw) ? Qnil : rb_iseq_disasm(iseqw_check(iseqw));
1736 }
1737 
1738 const char *
1740 {
1741  switch (node) {
1742 #include "node_name.inc"
1743  default:
1744  rb_bug("unknown node (%d)", node);
1745  return 0;
1746  }
1747 }
1748 
1749 #define DECL_SYMBOL(name) \
1750  static VALUE sym_##name
1751 
1752 #define INIT_SYMBOL(name) \
1753  sym_##name = ID2SYM(rb_intern(#name))
1754 
1755 static VALUE
1756 register_label(struct st_table *table, unsigned long idx)
1757 {
1758  VALUE sym = rb_str_intern(rb_sprintf("label_%lu", idx));
1759  st_insert(table, idx, sym);
1760  return sym;
1761 }
1762 
1763 static VALUE
1765 {
1766  ID id;
1767  switch (type) {
1768  case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
1769  case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
1770  case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
1771  case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
1772  case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
1773  case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
1774  default:
1775  rb_bug("exception_type2symbol: unknown type %d", (int)type);
1776  }
1777  return ID2SYM(id);
1778 }
1779 
1780 static int
1782 {
1783  rb_ary_push(ary, obj_resurrect(key));
1784  rb_ary_push(ary, value);
1785  return ST_CONTINUE;
1786 }
1787 
1788 static VALUE
1790 {
1791  unsigned int i;
1792  long l;
1793  size_t ti;
1794  unsigned int pos;
1795  unsigned int line = 0;
1796  VALUE *seq, *iseq_original;
1797 
1798  VALUE val = rb_ary_new();
1799  VALUE type; /* Symbol */
1800  VALUE locals = rb_ary_new();
1801  VALUE params = rb_hash_new();
1802  VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
1803  VALUE nbody;
1804  VALUE exception = rb_ary_new(); /* [[....]] */
1805  VALUE misc = rb_hash_new();
1806 
1807  static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1808  struct st_table *labels_table = st_init_numtable();
1809 
1810  DECL_SYMBOL(top);
1811  DECL_SYMBOL(method);
1812  DECL_SYMBOL(block);
1813  DECL_SYMBOL(class);
1814  DECL_SYMBOL(rescue);
1815  DECL_SYMBOL(ensure);
1816  DECL_SYMBOL(eval);
1817  DECL_SYMBOL(main);
1818  DECL_SYMBOL(defined_guard);
1819 
1820  if (sym_top == 0) {
1821  int i;
1822  for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1823  insn_syms[i] = ID2SYM(rb_intern(insn_name(i)));
1824  }
1825  INIT_SYMBOL(top);
1826  INIT_SYMBOL(method);
1827  INIT_SYMBOL(block);
1828  INIT_SYMBOL(class);
1829  INIT_SYMBOL(rescue);
1830  INIT_SYMBOL(ensure);
1831  INIT_SYMBOL(eval);
1832  INIT_SYMBOL(main);
1833  INIT_SYMBOL(defined_guard);
1834  }
1835 
1836  /* type */
1837  switch (iseq->body->type) {
1838  case ISEQ_TYPE_TOP: type = sym_top; break;
1839  case ISEQ_TYPE_METHOD: type = sym_method; break;
1840  case ISEQ_TYPE_BLOCK: type = sym_block; break;
1841  case ISEQ_TYPE_CLASS: type = sym_class; break;
1842  case ISEQ_TYPE_RESCUE: type = sym_rescue; break;
1843  case ISEQ_TYPE_ENSURE: type = sym_ensure; break;
1844  case ISEQ_TYPE_EVAL: type = sym_eval; break;
1845  case ISEQ_TYPE_MAIN: type = sym_main; break;
1846  case ISEQ_TYPE_DEFINED_GUARD: type = sym_defined_guard; break;
1847  default: rb_bug("unsupported iseq type");
1848  };
1849 
1850  /* locals */
1851  for (i=0; i<iseq->body->local_table_size; i++) {
1852  ID lid = iseq->body->local_table[i];
1853  if (lid) {
1854  if (rb_id2str(lid)) {
1855  rb_ary_push(locals, ID2SYM(lid));
1856  }
1857  else { /* hidden variable from id_internal() */
1858  rb_ary_push(locals, ULONG2NUM(iseq->body->local_table_size-i+1));
1859  }
1860  }
1861  else {
1862  rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
1863  }
1864  }
1865 
1866  /* params */
1867  {
1868  int j;
1869 
1870  if (iseq->body->param.flags.has_opt) {
1871  int len = iseq->body->param.opt_num + 1;
1872  VALUE arg_opt_labels = rb_ary_new2(len);
1873 
1874  for (j = 0; j < len; j++) {
1875  VALUE l = register_label(labels_table, iseq->body->param.opt_table[j]);
1876  rb_ary_push(arg_opt_labels, l);
1877  }
1878  rb_hash_aset(params, ID2SYM(rb_intern("opt")), arg_opt_labels);
1879  }
1880 
1881  /* commit */
1882  if (iseq->body->param.flags.has_lead) rb_hash_aset(params, ID2SYM(rb_intern("lead_num")), INT2FIX(iseq->body->param.lead_num));
1883  if (iseq->body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_num")), INT2FIX(iseq->body->param.post_num));
1884  if (iseq->body->param.flags.has_post) rb_hash_aset(params, ID2SYM(rb_intern("post_start")), INT2FIX(iseq->body->param.post_start));
1885  if (iseq->body->param.flags.has_rest) rb_hash_aset(params, ID2SYM(rb_intern("rest_start")), INT2FIX(iseq->body->param.rest_start));
1886  if (iseq->body->param.flags.has_block) rb_hash_aset(params, ID2SYM(rb_intern("block_start")), INT2FIX(iseq->body->param.block_start));
1887  if (iseq->body->param.flags.has_kw) {
1888  VALUE keywords = rb_ary_new();
1889  int i, j;
1890  for (i=0; i<iseq->body->param.keyword->required_num; i++) {
1891  rb_ary_push(keywords, ID2SYM(iseq->body->param.keyword->table[i]));
1892  }
1893  for (j=0; i<iseq->body->param.keyword->num; i++, j++) {
1894  VALUE key = rb_ary_new_from_args(1, ID2SYM(iseq->body->param.keyword->table[i]));
1895  if (iseq->body->param.keyword->default_values[j] != Qundef) {
1896  rb_ary_push(key, iseq->body->param.keyword->default_values[j]);
1897  }
1898  rb_ary_push(keywords, key);
1899  }
1900 
1901  rb_hash_aset(params, ID2SYM(rb_intern("kwbits")),
1902  INT2FIX(iseq->body->param.keyword->bits_start));
1903  rb_hash_aset(params, ID2SYM(rb_intern("keyword")), keywords);
1904  }
1905  if (iseq->body->param.flags.has_kwrest) rb_hash_aset(params, ID2SYM(rb_intern("kwrest")), INT2FIX(iseq->body->param.keyword->rest_start));
1906  if (iseq->body->param.flags.ambiguous_param0) rb_hash_aset(params, ID2SYM(rb_intern("ambiguous_param0")), Qtrue);
1907  }
1908 
1909  /* body */
1910  iseq_original = rb_iseq_original_iseq((rb_iseq_t *)iseq);
1911 
1912  for (seq = iseq_original; seq < iseq_original + iseq->body->iseq_size; ) {
1913  VALUE insn = *seq++;
1914  int j, len = insn_len(insn);
1915  VALUE *nseq = seq + len - 1;
1916  VALUE ary = rb_ary_new2(len);
1917 
1918  rb_ary_push(ary, insn_syms[insn]);
1919  for (j=0; j<len-1; j++, seq++) {
1920  switch (insn_op_type(insn, j)) {
1921  case TS_OFFSET: {
1922  unsigned long idx = nseq - iseq_original + *seq;
1923  rb_ary_push(ary, register_label(labels_table, idx));
1924  break;
1925  }
1926  case TS_LINDEX:
1927  case TS_NUM:
1928  rb_ary_push(ary, INT2FIX(*seq));
1929  break;
1930  case TS_VALUE:
1931  rb_ary_push(ary, obj_resurrect(*seq));
1932  break;
1933  case TS_ISEQ:
1934  {
1935  const rb_iseq_t *iseq = (rb_iseq_t *)*seq;
1936  if (iseq) {
1937  VALUE val = iseq_data_to_ary(rb_iseq_check(iseq));
1938  rb_ary_push(ary, val);
1939  }
1940  else {
1941  rb_ary_push(ary, Qnil);
1942  }
1943  }
1944  break;
1945  case TS_GENTRY:
1946  {
1947  struct rb_global_entry *entry = (struct rb_global_entry *)*seq;
1948  rb_ary_push(ary, ID2SYM(entry->id));
1949  }
1950  break;
1951  case TS_IC:
1952  {
1953  union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
1954  rb_ary_push(ary, INT2FIX(is - iseq->body->is_entries));
1955  }
1956  break;
1957  case TS_CALLINFO:
1958  {
1959  struct rb_call_info *ci = (struct rb_call_info *)*seq;
1960  VALUE e = rb_hash_new();
1961  int orig_argc = ci->orig_argc;
1962 
1963  rb_hash_aset(e, ID2SYM(rb_intern("mid")), ci->mid ? ID2SYM(ci->mid) : Qnil);
1964  rb_hash_aset(e, ID2SYM(rb_intern("flag")), UINT2NUM(ci->flag));
1965 
1966  if (ci->flag & VM_CALL_KWARG) {
1967  struct rb_call_info_with_kwarg *ci_kw = (struct rb_call_info_with_kwarg *)ci;
1968  int i;
1969  VALUE kw = rb_ary_new2((long)ci_kw->kw_arg->keyword_len);
1970 
1971  orig_argc -= ci_kw->kw_arg->keyword_len;
1972  for (i = 0; i < ci_kw->kw_arg->keyword_len; i++) {
1973  rb_ary_push(kw, ci_kw->kw_arg->keywords[i]);
1974  }
1975  rb_hash_aset(e, ID2SYM(rb_intern("kw_arg")), kw);
1976  }
1977 
1978  rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")),
1979  INT2FIX(orig_argc));
1980  rb_ary_push(ary, e);
1981  }
1982  break;
1983  case TS_CALLCACHE:
1984  rb_ary_push(ary, Qfalse);
1985  break;
1986  case TS_ID:
1987  rb_ary_push(ary, ID2SYM(*seq));
1988  break;
1989  case TS_CDHASH:
1990  {
1991  VALUE hash = *seq;
1992  VALUE val = rb_ary_new();
1993  int i;
1994 
1995  rb_hash_foreach(hash, cdhash_each, val);
1996 
1997  for (i=0; i<RARRAY_LEN(val); i+=2) {
1998  VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
1999  unsigned long idx = nseq - iseq_original + pos;
2000 
2001  rb_ary_store(val, i+1,
2002  register_label(labels_table, idx));
2003  }
2004  rb_ary_push(ary, val);
2005  }
2006  break;
2007  case TS_FUNCPTR:
2008  {
2009 #if SIZEOF_VALUE <= SIZEOF_LONG
2010  VALUE val = LONG2NUM((SIGNED_VALUE)*seq);
2011 #else
2012  VALUE val = LL2NUM((SIGNED_VALUE)*seq);
2013 #endif
2014  rb_ary_push(ary, val);
2015  }
2016  break;
2017  default:
2018  rb_bug("unknown operand: %c", insn_op_type(insn, j));
2019  }
2020  }
2021  rb_ary_push(body, ary);
2022  }
2023 
2024  nbody = body;
2025 
2026  /* exception */
2027  if (iseq->body->catch_table) for (i=0; i<iseq->body->catch_table->size; i++) {
2028  VALUE ary = rb_ary_new();
2029  const struct iseq_catch_table_entry *entry = &iseq->body->catch_table->entries[i];
2030  rb_ary_push(ary, exception_type2symbol(entry->type));
2031  if (entry->iseq) {
2033  }
2034  else {
2035  rb_ary_push(ary, Qnil);
2036  }
2037  rb_ary_push(ary, register_label(labels_table, entry->start));
2038  rb_ary_push(ary, register_label(labels_table, entry->end));
2039  rb_ary_push(ary, register_label(labels_table, entry->cont));
2040  rb_ary_push(ary, UINT2NUM(entry->sp));
2041  rb_ary_push(exception, ary);
2042  }
2043 
2044  /* make body with labels and insert line number */
2045  body = rb_ary_new();
2046  ti = 0;
2047 
2048  for (l=0, pos=0; l<RARRAY_LEN(nbody); l++) {
2049  VALUE ary = RARRAY_AREF(nbody, l);
2050  st_data_t label;
2051 
2052  if (st_lookup(labels_table, pos, &label)) {
2053  rb_ary_push(body, (VALUE)label);
2054  }
2055 
2056  if (ti < iseq->body->line_info_size && iseq->body->line_info_table[ti].position == pos) {
2057  line = iseq->body->line_info_table[ti].line_no;
2058  rb_ary_push(body, INT2FIX(line));
2059  ti++;
2060  }
2061 
2062  rb_ary_push(body, ary);
2063  pos += RARRAY_LENINT(ary); /* reject too huge data */
2064  }
2065  RB_GC_GUARD(nbody);
2066 
2067  st_free_table(labels_table);
2068 
2069  rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq->body->param.size));
2070  rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq->body->local_table_size));
2071  rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq->body->stack_max));
2072 
2073  /* TODO: compatibility issue */
2074  /*
2075  * [:magic, :major_version, :minor_version, :format_type, :misc,
2076  * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
2077  * :catch_table, :bytecode]
2078  */
2079  rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
2080  rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
2081  rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
2082  rb_ary_push(val, INT2FIX(1));
2083  rb_ary_push(val, misc);
2084  rb_ary_push(val, iseq->body->location.label);
2085  rb_ary_push(val, iseq->body->location.path);
2086  rb_ary_push(val, iseq->body->location.absolute_path);
2087  rb_ary_push(val, iseq->body->location.first_lineno);
2088  rb_ary_push(val, type);
2089  rb_ary_push(val, locals);
2090  rb_ary_push(val, params);
2091  rb_ary_push(val, exception);
2092  rb_ary_push(val, body);
2093  return val;
2094 }
2095 
2096 VALUE
2097 rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
2098 {
2099  int i, r;
2100  VALUE a, args = rb_ary_new2(iseq->body->param.size);
2101  ID req, opt, rest, block, key, keyrest;
2102 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
2103 #define PARAM_ID(i) iseq->body->local_table[(i)]
2104 #define PARAM(i, type) ( \
2105  PARAM_TYPE(type), \
2106  rb_id2str(PARAM_ID(i)) ? \
2107  rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
2108  a)
2109 
2110  CONST_ID(req, "req");
2111  CONST_ID(opt, "opt");
2112  if (is_proc) {
2113  for (i = 0; i < iseq->body->param.lead_num; i++) {
2114  PARAM_TYPE(opt);
2116  rb_ary_push(args, a);
2117  }
2118  }
2119  else {
2120  for (i = 0; i < iseq->body->param.lead_num; i++) {
2121  rb_ary_push(args, PARAM(i, req));
2122  }
2123  }
2124  r = iseq->body->param.lead_num + iseq->body->param.opt_num;
2125  for (; i < r; i++) {
2126  PARAM_TYPE(opt);
2127  if (rb_id2str(PARAM_ID(i))) {
2128  rb_ary_push(a, ID2SYM(PARAM_ID(i)));
2129  }
2130  rb_ary_push(args, a);
2131  }
2132  if (iseq->body->param.flags.has_rest) {
2133  CONST_ID(rest, "rest");
2134  rb_ary_push(args, PARAM(iseq->body->param.rest_start, rest));
2135  }
2136  r = iseq->body->param.post_start + iseq->body->param.post_num;
2137  if (is_proc) {
2138  for (i = iseq->body->param.post_start; i < r; i++) {
2139  PARAM_TYPE(opt);
2141  rb_ary_push(args, a);
2142  }
2143  }
2144  else {
2145  for (i = iseq->body->param.post_start; i < r; i++) {
2146  rb_ary_push(args, PARAM(i, req));
2147  }
2148  }
2149  if (iseq->body->param.flags.has_kw) {
2150  i = 0;
2151  if (iseq->body->param.keyword->required_num > 0) {
2152  ID keyreq;
2153  CONST_ID(keyreq, "keyreq");
2154  for (; i < iseq->body->param.keyword->required_num; i++) {
2155  PARAM_TYPE(keyreq);
2156  if (rb_id2str(iseq->body->param.keyword->table[i])) {
2157  rb_ary_push(a, ID2SYM(iseq->body->param.keyword->table[i]));
2158  }
2159  rb_ary_push(args, a);
2160  }
2161  }
2162  CONST_ID(key, "key");
2163  for (; i < iseq->body->param.keyword->num; i++) {
2164  PARAM_TYPE(key);
2165  if (rb_id2str(iseq->body->param.keyword->table[i])) {
2166  rb_ary_push(a, ID2SYM(iseq->body->param.keyword->table[i]));
2167  }
2168  rb_ary_push(args, a);
2169  }
2170  }
2171  if (iseq->body->param.flags.has_kwrest) {
2172  CONST_ID(keyrest, "keyrest");
2173  rb_ary_push(args, PARAM(iseq->body->param.keyword->rest_start, keyrest));
2174  }
2175  if (iseq->body->param.flags.has_block) {
2176  CONST_ID(block, "block");
2177  rb_ary_push(args, PARAM(iseq->body->param.block_start, block));
2178  }
2179  return args;
2180 }
2181 
2182 VALUE
2184 {
2185  static const char expr_names[][18] = {
2186  "nil",
2187  "instance-variable",
2188  "local-variable",
2189  "global-variable",
2190  "class variable",
2191  "constant",
2192  "method",
2193  "yield",
2194  "super",
2195  "self",
2196  "true",
2197  "false",
2198  "assignment",
2199  "expression",
2200  };
2201  const char *estr;
2202  VALUE *defs, str;
2203 
2204  if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) return 0;
2205  estr = expr_names[type - 1];
2206  if (!estr[0]) return 0;
2207  defs = GET_VM()->defined_strings;
2208  if (!defs) {
2209  defs = ruby_xcalloc(numberof(expr_names), sizeof(VALUE));
2210  GET_VM()->defined_strings = defs;
2211  }
2212  str = defs[type-1];
2213  if (!str) {
2214  str = rb_str_new_cstr(estr);
2215  OBJ_FREEZE(str);
2216  defs[type-1] = str;
2218  }
2219  return str;
2220 }
2221 
2222 /* Experimental tracing support: trace(line) -> trace(specified_line)
2223  * MRI Specific.
2224  */
2225 
2226 int
2227 rb_iseqw_line_trace_each(VALUE iseqw, int (*func)(int line, rb_event_flag_t *events_ptr, void *d), void *data)
2228 {
2229  int trace_num = 0;
2230  unsigned int pos;
2231  size_t insn;
2232  const rb_iseq_t *iseq = iseqw_check(iseqw);
2233  int cont = 1;
2234  VALUE *iseq_original;
2235 
2236  iseq_original = rb_iseq_original_iseq(iseq);
2237  for (pos = 0; cont && pos < iseq->body->iseq_size; pos += insn_len(insn)) {
2238  insn = iseq_original[pos];
2239 
2240  if (insn == BIN(trace)) {
2241  rb_event_flag_t current_events;
2242 
2243  current_events = (rb_event_flag_t)iseq_original[pos+1];
2244 
2245  if (current_events & RUBY_EVENT_LINE) {
2246  rb_event_flag_t events = current_events & RUBY_EVENT_SPECIFIED_LINE;
2247  trace_num++;
2248 
2249  if (func) {
2250  int line = find_line_no(iseq, pos);
2251  /* printf("line: %d\n", line); */
2252  cont = (*func)(line, &events, data);
2253  if (current_events != events) {
2254  VALUE *encoded = (VALUE *)iseq->body->iseq_encoded;
2255  iseq_original[pos+1] = encoded[pos+1] =
2256  (VALUE)(current_events | (events & RUBY_EVENT_SPECIFIED_LINE));
2257  }
2258  }
2259  }
2260  }
2261  }
2262  return trace_num;
2263 }
2264 
2265 static int
2266 collect_trace(int line, rb_event_flag_t *events_ptr, void *ptr)
2267 {
2268  VALUE result = (VALUE)ptr;
2269  rb_ary_push(result, INT2NUM(line));
2270  return 1;
2271 }
2272 
2273 /*
2274  * <b>Experimental MRI specific feature, only available as C level api.</b>
2275  *
2276  * Returns all +specified_line+ events.
2277  */
2278 VALUE
2280 {
2281  VALUE result = rb_ary_new();
2282  rb_iseqw_line_trace_each(iseqw, collect_trace, (void *)result);
2283  return result;
2284 }
2285 
2287  int pos;
2288  int set;
2289  int prev; /* 1: set, 2: unset, 0: not found */
2290 };
2291 
2292 static int
2293 line_trace_specify(int line, rb_event_flag_t *events_ptr, void *ptr)
2294 {
2295  struct set_specifc_data *data = (struct set_specifc_data *)ptr;
2296 
2297  if (data->pos == 0) {
2298  data->prev = *events_ptr & RUBY_EVENT_SPECIFIED_LINE ? 1 : 2;
2299  if (data->set) {
2300  *events_ptr = *events_ptr | RUBY_EVENT_SPECIFIED_LINE;
2301  }
2302  else {
2303  *events_ptr = *events_ptr & ~RUBY_EVENT_SPECIFIED_LINE;
2304  }
2305  return 0; /* found */
2306  }
2307  else {
2308  data->pos--;
2309  return 1;
2310  }
2311 }
2312 
2313 /*
2314  * <b>Experimental MRI specific feature, only available as C level api.</b>
2315  *
2316  * Set a +specified_line+ event at the given line position, if the +set+
2317  * parameter is +true+.
2318  *
2319  * This method is useful for building a debugger breakpoint at a specific line.
2320  *
2321  * A TypeError is raised if +set+ is not boolean.
2322  *
2323  * If +pos+ is a negative integer a TypeError exception is raised.
2324  */
2325 VALUE
2327 {
2328  struct set_specifc_data data;
2329 
2330  data.prev = 0;
2331  data.pos = NUM2INT(pos);
2332  if (data.pos < 0) rb_raise(rb_eTypeError, "`pos' is negative");
2333 
2334  switch (set) {
2335  case Qtrue: data.set = 1; break;
2336  case Qfalse: data.set = 0; break;
2337  default:
2338  rb_raise(rb_eTypeError, "`set' should be true/false");
2339  }
2340 
2341  rb_iseqw_line_trace_each(iseqval, line_trace_specify, (void *)&data);
2342 
2343  if (data.prev == 0) {
2344  rb_raise(rb_eTypeError, "`pos' is out of range.");
2345  }
2346  return data.prev == 1 ? Qtrue : Qfalse;
2347 }
2348 
2349 VALUE
2351 {
2352  return rb_iseq_local_variables(iseqw_check(iseqval));
2353 }
2354 
2355 /*
2356  * call-seq:
2357  * iseq.to_binary(extra_data = nil) -> binary str
2358  *
2359  * Returns serialized iseq binary format data as a String object.
2360  * A corresponding iseq object is created by
2361  * RubyVM::InstructionSequence.load_from_binary() method.
2362  *
2363  * String extra_data will be saved with binary data.
2364  * You can access this data with
2365  * RubyVM::InstructionSequence.load_from_binary_extra_data(binary).
2366  *
2367  * Note that the translated binary data is not portable.
2368  * You can not move this binary data to another machine.
2369  * You can not use the binary data which is created by another
2370  * version/another architecture of Ruby.
2371  */
2372 static VALUE
2374 {
2375  VALUE opt;
2376  rb_scan_args(argc, argv, "01", &opt);
2377  return iseq_ibf_dump(iseqw_check(self), opt);
2378 }
2379 
2380 /*
2381  * call-seq:
2382  * RubyVM::InstructionSequence.load_from_binary(binary) -> iseq
2383  *
2384  * Load an iseq object from binary format String object
2385  * created by RubyVM::InstructionSequence.to_binary.
2386  *
2387  * This loader does not have a verifier, so that loading broken/modified
2388  * binary causes critical problem.
2389  *
2390  * You should not load binary data provided by others.
2391  * You should use binary data translated by yourself.
2392  */
2393 static VALUE
2395 {
2396  return iseqw_new(iseq_ibf_load(str));
2397 }
2398 
2399 /*
2400  * call-seq:
2401  * RubyVM::InstructionSequence.load_from_binary_extra_data(binary) -> str
2402  *
2403  * Load extra data embed into binary format String object.
2404  */
2405 static VALUE
2407 {
2408  return iseq_ibf_load_extra_data(str);
2409 }
2410 
2411 /*
2412  * Document-class: RubyVM::InstructionSequence
2413  *
2414  * The InstructionSequence class represents a compiled sequence of
2415  * instructions for the Ruby Virtual Machine.
2416  *
2417  * With it, you can get a handle to the instructions that make up a method or
2418  * a proc, compile strings of Ruby code down to VM instructions, and
2419  * disassemble instruction sequences to strings for easy inspection. It is
2420  * mostly useful if you want to learn how the Ruby VM works, but it also lets
2421  * you control various settings for the Ruby iseq compiler.
2422  *
2423  * You can find the source for the VM instructions in +insns.def+ in the Ruby
2424  * source.
2425  *
2426  * The instruction sequence results will almost certainly change as Ruby
2427  * changes, so example output in this documentation may be different from what
2428  * you see.
2429  */
2430 
2431 void
2433 {
2434  /* declare ::RubyVM::InstructionSequence */
2435  rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
2437  rb_define_method(rb_cISeq, "inspect", iseqw_inspect, 0);
2438  rb_define_method(rb_cISeq, "disasm", iseqw_disasm, 0);
2439  rb_define_method(rb_cISeq, "disassemble", iseqw_disasm, 0);
2440  rb_define_method(rb_cISeq, "to_a", iseqw_to_a, 0);
2441  rb_define_method(rb_cISeq, "eval", iseqw_eval, 0);
2442 
2443  rb_define_method(rb_cISeq, "to_binary", iseqw_to_binary, -1);
2445  rb_define_singleton_method(rb_cISeq, "load_from_binary_extra_data", iseqw_s_load_from_binary_extra_data, 1);
2446 
2447 
2448  /* location APIs */
2449  rb_define_method(rb_cISeq, "path", iseqw_path, 0);
2450  rb_define_method(rb_cISeq, "absolute_path", iseqw_absolute_path, 0);
2451  rb_define_method(rb_cISeq, "label", iseqw_label, 0);
2452  rb_define_method(rb_cISeq, "base_label", iseqw_base_label, 0);
2453  rb_define_method(rb_cISeq, "first_lineno", iseqw_first_lineno, 0);
2454 
2455 #if 0
2456  /* Now, it is experimental. No discussions, no tests. */
2457  /* They can be used from C level. Please give us feedback. */
2458  rb_define_method(rb_cISeq, "line_trace_all", rb_iseqw_line_trace_all, 0);
2459  rb_define_method(rb_cISeq, "line_trace_specify", rb_iseqw_line_trace_specify, 2);
2460 #else
2463 #endif
2464 
2465 #if 0 /* TBD */
2466  rb_define_private_method(rb_cISeq, "marshal_dump", iseqw_marshal_dump, 0);
2467  rb_define_private_method(rb_cISeq, "marshal_load", iseqw_marshal_load, 1);
2468  /* disable this feature because there is no verifier. */
2470 #endif
2471  (void)iseq_s_load;
2472 
2481 
2482  rb_undef_method(CLASS_OF(rb_cISeq), "translate");
2483  rb_undef_method(CLASS_OF(rb_cISeq), "load_iseq");
2484 }
static VALUE iseq_inspect(const rb_iseq_t *iseq)
Definition: iseq.c:1476
int rb_iseqw_line_trace_each(VALUE iseqw, int(*func)(int line, rb_event_flag_t *events_ptr, void *d), void *data)
Definition: iseq.c:2227
VALUE rb_iseqw_line_trace_all(VALUE iseqw)
Definition: iseq.c:2279
unsigned int stack_max
Definition: vm_core.h:387
#define VM_CALL_ARGS_BLOCKARG
Definition: vm_core.h:904
union iseq_inline_storage_entry * is_entries
Definition: vm_core.h:372
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
Definition: symbol.c:923
VALUE iseq_ibf_load_extra_data(VALUE str)
Definition: compile.c:8644
rb_iseq_t * rb_iseq_new(NODE *node, VALUE name, VALUE path, VALUE absolute_path, const rb_iseq_t *parent, enum iseq_type type)
Definition: iseq.c:436
const rb_iseq_t * iseq
Definition: iseq.h:155
unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
Definition: iseq.c:1234
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1196
#define RARRAY_LEN(a)
Definition: ruby.h:1026
void rb_bug(const char *fmt,...)
Definition: error.c:482
void ibf_load_iseq_complete(rb_iseq_t *iseq)
Definition: compile.c:8509
#define FALSE
Definition: nkf.h:174
#define RUBY_TYPED_FREE_IMMEDIATELY
Definition: ruby.h:1145
void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc, VALUE locals, VALUE params, VALUE exception, VALUE body)
Definition: compile.c:6921
NODE * rb_parser_compile_string_path(VALUE vparser, VALUE f, VALUE s, int line)
Definition: ripper.c:12349
const rb_iseq_t * rb_method_iseq(VALUE body)
Definition: proc.c:2467
#define DECL_SYMBOL(name)
Definition: iseq.c:1749
#define INT2NUM(x)
Definition: ruby.h:1538
#define VM_CALL_FCALL
Definition: vm_core.h:905
Definition: st.h:79
void rb_iseq_make_compile_option(rb_compile_option_t *option, VALUE opt)
Definition: iseq.c:379
static VALUE iseqw_path(VALUE self)
Definition: iseq.c:993
#define NUM2INT(x)
Definition: ruby.h:684
static const rb_iseq_t * iseqw_check(VALUE iseqw)
Definition: iseq.c:915
#define RB_OBJ_WRITTEN(a, oldv, b)
Definition: ruby.h:1438
static unsigned int hash(str, len) register const char *str
VALUE rb_iseq_method_name(const rb_iseq_t *iseq)
Definition: iseq.c:700
static void make_compile_option(rb_compile_option_t *option, VALUE opt)
Definition: iseq.c:386
void rb_undef_alloc_func(VALUE)
Definition: vm_method.c:681
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1716
unsigned int is_size
Definition: vm_core.h:383
Definition: iseq.h:150
#define OPT_DEBUG_FROZEN_STRING_LITERAL
Definition: vm_opts.h:27
void rb_iseq_mark(const rb_iseq_t *iseq)
Definition: iseq.c:106
#define FilePathValue(v)
Definition: ruby.h:594
#define CLASS_OF(v)
Definition: ruby.h:453
VALUE rb_fstring_cstr(const char *str)
Definition: string.c:387
static size_t param_keyword_size(const struct rb_iseq_param_keyword *pkw)
Definition: iseq.c:137
static enum iseq_type iseq_type_from_sym(VALUE type)
Definition: iseq.c:508
#define Qtrue
Definition: ruby.h:437
static const rb_iseq_t * rb_iseq_check(const rb_iseq_t *iseq)
Definition: vm_core.h:416
#define ISEQ_MAJOR_VERSION
Definition: iseq.h:15
NODE * rb_parser_compile_file_path(VALUE vparser, VALUE fname, VALUE file, int start)
Definition: ripper.c:12392
#define TypedData_Wrap_Struct(klass, data_type, sval)
Definition: ruby.h:1169
static VALUE iseqw_s_compile_option_set(VALUE self, VALUE opt)
Definition: iseq.c:891
struct iseq_compile_data_storage * storage_head
Definition: iseq.h:207
#define rb_id2str(id)
Definition: vm_backtrace.c:29
Definition: st.h:99
const int id
Definition: nkf.c:209
unsigned int end
Definition: iseq.h:157
static const rb_iseq_t * vm_proc_iseq(VALUE procval)
Definition: vm_core.h:1322
VALUE rb_iseqw_line_trace_specify(VALUE iseqval, VALUE pos, VALUE set)
Definition: iseq.c:2326
struct rb_iseq_constant_body::@196::@197 flags
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1527
VALUE rb_eTypeError
Definition: error.c:762
static VALUE id_to_name(ID id, VALUE default_value)
Definition: iseq.c:1245
rb_iseq_t * rb_iseq_new_with_opt(NODE *node, VALUE name, VALUE path, VALUE absolute_path, VALUE first_lineno, const rb_iseq_t *parent, enum iseq_type type, const rb_compile_option_t *option)
Definition: iseq.c:473
union rb_iseq_struct::@198 aux
#define ULONG2NUM(x)
Definition: ruby.h:1574
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:905
const ID * local_table
Definition: vm_core.h:363
VALUE rb_iseq_absolute_path(const rb_iseq_t *iseq)
Definition: iseq.c:676
VALUE rb_iseq_local_variables(const rb_iseq_t *iseq)
Definition: vm.c:792
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:54
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2890
static VALUE iseqw_first_lineno(VALUE self)
Definition: iseq.c:1086
#define OPT_INLINE_CONST_CACHE
Definition: vm_opts.h:25
VALUE rb_ary_tmp_new(long capa)
Definition: array.c:532
VALUE rb_iseq_defined_string(enum defined_type type)
Definition: iseq.c:2183
struct rb_iseq_constant_body * body
Definition: vm_core.h:395
#define debug(lvl, x...)
Definition: ffi.c:52
VALUE rb_funcall(VALUE, ID, int,...)
Calls a method.
Definition: vm_eval.c:821
static int cdhash_each(VALUE key, VALUE value, VALUE ary)
Definition: iseq.c:1781
static VALUE iseqw_base_label(VALUE self)
Definition: iseq.c:1070
struct rb_call_info_kw_arg * kw_arg
Definition: vm_core.h:228
VALUE rb_iseq_label(const rb_iseq_t *iseq)
Definition: iseq.c:682
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:693
#define Check_Type(v, t)
Definition: ruby.h:562
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2207
#define RUBY_MARK_LEAVE(msg)
Definition: gc.h:54
unsigned int flag
Definition: vm_core.h:217
struct iseq_compile_data_storage * next
Definition: iseq.h:181
struct rb_iseq_constant_body::@196 param
parameter information
#define RB_GC_GUARD(v)
Definition: ruby.h:552
#define T_HASH
Definition: ruby.h:499
static VALUE iseqw_to_binary(int argc, VALUE *argv, VALUE self)
Definition: iseq.c:2373
#define VM_CALL_ARGS_SPLAT
Definition: vm_core.h:903
int main(void)
Definition: closure_fn0.c:49
#define DATA_PTR(dta)
Definition: ruby.h:1113
void rb_gc_mark(VALUE ptr)
Definition: gc.c:4394
VALUE rb_hash_lookup(VALUE hash, VALUE key)
Definition: hash.c:867
#define RUBY_GC_INFO
Definition: gc.h:57
#define T_ARRAY
Definition: ruby.h:498
#define st_lookup
Definition: regint.h:185
#define VM_CALL_KWARG
Definition: vm_core.h:909
ID id
Definition: internal.h:889
static VALUE iseqw_s_compile(int argc, VALUE *argv, VALUE self)
Definition: iseq.c:781
VALUE rb_file_open_str(VALUE, const char *)
Definition: io.c:5723
int rb_str_cmp(VALUE, VALUE)
Definition: string.c:3050
static VALUE iseq_load(VALUE data, const rb_iseq_t *parent, VALUE opt)
Definition: iseq.c:535
static VALUE parse(int argc, VALUE *argv, VALUE self)
Definition: psych_parser.c:244
static void set_relation(rb_iseq_t *iseq, const rb_iseq_t *piseq)
Definition: iseq.c:237
static VALUE iseqw_s_disasm(VALUE klass, VALUE body)
Definition: iseq.c:1732
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1533
unsigned int cont
Definition: iseq.h:158
static VALUE iseqw_eval(VALUE self)
Definition: iseq.c:944
rb_iseq_t * rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, const struct rb_block *base_block)
Definition: iseq.c:664
const rb_iseq_t * rb_iseq_load_iseq(VALUE fname)
Definition: iseq.c:490
static VALUE iseqw_label(VALUE self)
Definition: iseq.c:1044
#define VM_CALL_VCALL
Definition: vm_core.h:906
const char * rb_obj_classname(VALUE)
Definition: variable.c:458
#define OPT_TAILCALL_OPTIMIZATION
Definition: vm_opts.h:22
#define rb_ary_new2
Definition: intern.h:90
#define CHECK_HASH(v)
Definition: iseq.c:502
const rb_compile_option_t * option
Definition: iseq.h:215
VALUE iseq_ibf_dump(const rb_iseq_t *iseq, VALUE opt)
Definition: compile.c:8450
#define GET_THREAD()
Definition: vm_core.h:1513
#define sym(x)
Definition: date_core.c:3721
const VALUE catch_table_ary
Definition: iseq.h:195
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
Definition: st.h:22
Definition: node.h:235
enum iseq_catch_table_entry::catch_type type
static size_t rb_call_info_kw_arg_bytes(int keyword_len)
Definition: iseq.h:24
const VALUE * iseq_encoded
Definition: vm_core.h:286
struct rb_id_table * ivar_cache_table
Definition: iseq.h:216
VALUE rb_cISeq
Definition: iseq.c:29
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
Definition: hash.c:402
#define ISEQ_MINOR_VERSION
Definition: iseq.h:16
rb_iseq_t * rb_iseq_compile(VALUE src, VALUE file, VALUE line)
Definition: iseq.c:658
void rb_exc_raise(VALUE mesg)
Definition: eval.c:620
int orig_argc
Definition: vm_core.h:218
#define SET_COMPILE_OPTION_NUM(o, h, mem)
#define ISEQ_NOT_LOADED_YET
Definition: iseq.h:85
unsigned int iseq_size
Definition: vm_core.h:285
#define CHECK_SYMBOL(v)
Definition: iseq.c:504
#define RB_TYPE_P(obj, type)
Definition: ruby.h:527
void * ruby_xcalloc(size_t n, size_t size)
Definition: gc.c:7966
static VALUE iseqw_s_load_from_binary(VALUE self, VALUE str)
Definition: iseq.c:2394
static VALUE coverage(VALUE fname, int n)
Definition: ripper.c:12176
static rb_compile_option_t COMPILE_OPTION_DEFAULT
Definition: iseq.c:334
#define OPT_PEEPHOLE_OPTIMIZATION
Definition: vm_opts.h:23
#define FL_TEST(x, f)
Definition: ruby.h:1284
int rb_str_symname_p(VALUE)
Definition: string.c:9600
#define RUBY_TYPED_WB_PROTECTED
Definition: ruby.h:1146
Definition: iseq.h:148
static VALUE iseqw_s_load_from_binary_extra_data(VALUE self, VALUE str)
Definition: iseq.c:2406
void rb_id_table_free(struct rb_id_table *tbl)
static VALUE CHECK_INTEGER(VALUE v)
Definition: iseq.c:505
static int rb_obj_is_iseq(VALUE iseq)
Definition: vm_core.h:1043
#define ALLOC_N(type, n)
Definition: ruby.h:1587
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1576
VALUE mark_ary
Definition: iseq.h:194
unsigned int local_table_size
Definition: vm_core.h:382
#define level
Definition: internal.h:887
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1872
static size_t iseq_memsize(const rb_iseq_t *iseq)
Definition: iseq.c:150
static VALUE prepare_iseq_build(rb_iseq_t *iseq, VALUE name, VALUE path, VALUE absolute_path, VALUE first_lineno, const rb_iseq_t *parent, enum iseq_type type, const rb_compile_option_t *option)
Definition: iseq.c:269
#define PRIdVALUE
Definition: ruby.h:130
#define PARAM(i, type)
static VALUE register_label(struct st_table *table, unsigned long idx)
Definition: iseq.c:1756
struct rb_call_info * ci_entries
Definition: vm_core.h:373
VALUE rb_get_coverages(void)
Definition: thread.c:5004
VALUE rb_str_cat2(VALUE, const char *)
static VALUE iseqw_s_compile_option_get(VALUE self)
Definition: iseq.c:909
VALUE rb_ary_new(void)
Definition: array.c:493
static unsigned int find_line_no(const rb_iseq_t *iseq, size_t pos)
Definition: iseq.c:1221
static VALUE obj_resurrect(VALUE obj)
Definition: iseq.c:36
#define UINT2NUM(x)
Definition: ruby.h:1539
#define SIZEOF_ISEQ_COMPILE_DATA_STORAGE
Definition: iseq.h:188
static VALUE make_compile_option_value(rb_compile_option_t *option)
Definition: iseq.c:409
VALUE rb_iseqw_new(const rb_iseq_t *iseq)
Definition: iseq.c:754
VALUE keywords[1]
Definition: vm_core.h:223
const VALUE err_info
Definition: iseq.h:193
VALUE rb_iseq_first_lineno(const rb_iseq_t *iseq)
Definition: iseq.c:694
#define snprintf
Definition: subst.h:6
#define NIL_P(v)
Definition: ruby.h:451
VALUE rb_insn_operand_intern(const rb_iseq_t *iseq, VALUE insn, int op_no, VALUE op, int len, size_t pos, const VALUE *pnop, VALUE child)
Definition: iseq.c:1258
#define CHECK_ARRAY(v)
Definition: iseq.c:501
#define VM_CALL_ARGS_SIMPLE
Definition: vm_core.h:907
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:799
VALUE rb_cRubyVM
Definition: vm.c:310
static VALUE iseqw_inspect(VALUE self)
Definition: iseq.c:955
Definition: vm_core.h:195
#define RUBY_MARK_ENTER(msg)
Definition: gc.h:53
int argc
Definition: ruby.c:183
VALUE * rb_iseq_original_iseq(const rb_iseq_t *iseq)
Definition: compile.c:716
#define Qfalse
Definition: ruby.h:436
static const rb_data_type_t iseqw_data_type
Definition: iseq.c:734
static rb_iseq_location_t * iseq_location_setup(rb_iseq_t *iseq, VALUE path, VALUE absolute_path, VALUE name, VALUE first_lineno)
Definition: iseq.c:220
VALUE rb_parser_set_context(VALUE vparser, const struct rb_block *base, int main)
Definition: ripper.c:17749
void rb_gc_register_mark_object(VALUE obj)
Definition: gc.c:6154
#define rb_str_new2
Definition: intern.h:857
int err
Definition: win32.c:135
#define OBJ_FREEZE(x)
Definition: ruby.h:1308
const struct rb_iseq_constant_body::@196::rb_iseq_param_keyword * keyword
#define OPT_INSTRUCTIONS_UNIFICATION
Definition: vm_opts.h:46
#define ISEQ_COVERAGE_SET(iseq, cov)
Definition: iseq.h:49
#define PRIdPTRDIFF
Definition: ruby.h:159
static VALUE iseqw_s_compile_file(int argc, VALUE *argv, VALUE self)
Definition: iseq.c:823
rb_iseq_t * rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path, const rb_iseq_t *parent)
Definition: iseq.c:451
#define numberof(array)
Definition: etc.c:616
static VALUE iseqw_disasm(VALUE self)
Definition: iseq.c:1618
VALUE rb_str_resurrect(VALUE str)
Definition: string.c:1442
void Init_ISeq(void)
Definition: iseq.c:2432
#define PRIuVALUE
Definition: ruby.h:132
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2562
ID id_class
Definition: eventids1.c:32
#define VM_CALL_SUPER
Definition: vm_core.h:911
static VALUE iseq_data_to_ary(const rb_iseq_t *iseq)
Definition: iseq.c:1789
static VALUE exception_type2symbol(VALUE type)
Definition: iseq.c:1764
#define INIT_SYMBOL(name)
Definition: iseq.c:1752
Definition: iseq.h:149
#define ZALLOC(type)
Definition: ruby.h:1590
struct rb_call_cache * cc_entries
Definition: vm_core.h:378
#define RSTRING_LEN(str)
Definition: ruby.h:978
#define RUBY_EVENT_SPECIFIED_LINE
Definition: ruby.h:2078
#define hidden_obj_p(obj)
Definition: iseq.c:33
Definition: iseq.h:153
unsigned int size
Definition: iseq.h:183
#define TRUE
Definition: nkf.h:175
defined_type
Definition: iseq.h:224
VALUE rb_obj_is_proc(VALUE)
Definition: proc.c:117
unsigned int coverage_enabled
Definition: iseq.h:137
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1440
VALUE rb_hash_new(void)
Definition: hash.c:441
void ruby_xfree(void *x)
Definition: gc.c:8017
#define OPT_TRACE_INSTRUCTION
Definition: vm_opts.h:21
static const rb_iseq_t * vm_block_iseq(const struct rb_block *block)
Definition: vm_core.h:1335
#define VM_ENV_DATA_SIZE
Definition: vm_core.h:988
ID id_rescue
Definition: eventids1.c:88
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1919
VALUE rb_iseq_coverage(const rb_iseq_t *iseq)
Definition: iseq.c:715
#define RUBY_EVENT_LINE
Definition: ruby.h:2059
#define PRIsVALUE
Definition: ruby.h:135
unsigned long ID
Definition: ruby.h:86
unsigned int peephole_optimization
Definition: iseq.h:128
static int iseq_catch_table_bytes(int n)
Definition: iseq.h:168
#define Qnil
Definition: ruby.h:438
#define SET_COMPILE_OPTION(o, h, mem)
#define ISEQ_COVERAGE(iseq)
Definition: iseq.h:48
#define BUILTIN_TYPE(x)
Definition: ruby.h:518
VALUE rb_io_close(VALUE)
Definition: io.c:4469
unsigned long VALUE
Definition: ruby.h:85
static VALUE result
Definition: nkf.c:40
Definition: iseq.h:146
VALUE rb_iseq_compile_node(rb_iseq_t *iseq, NODE *node)
Definition: compile.c:576
#define FIX2INT(x)
Definition: ruby.h:686
unsigned int ci_kw_size
Definition: vm_core.h:385
#define VM_CALL_OPT_SEND
Definition: vm_core.h:912
unsigned int ci_size
Definition: vm_core.h:384
VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
Definition: iseq.c:604
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:439
#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE
Definition: iseq.h:178
#define OPT_STACK_CACHING
Definition: vm_opts.h:48
VALUE rb_str_new_cstr(const char *)
Definition: string.c:770
Definition: iseq.h:151
#define OPT_FROZEN_STRING_LITERAL
Definition: vm_opts.h:26
#define RARRAY_LENINT(ary)
Definition: ruby.h:1027
void rb_iseq_free(const rb_iseq_t *iseq)
Definition: iseq.c:70
VALUE rb_fstring(VALUE)
Definition: string.c:305
enum rb_iseq_constant_body::iseq_type type
VALUE rb_str_dup(VALUE)
Definition: string.c:1436
static VALUE cleanup_iseq_build(rb_iseq_t *iseq)
Definition: iseq.c:320
#define PARAM_ID(i)
#define LONG2NUM(x)
Definition: ruby.h:1573
#define rb_funcallv
Definition: console.c:21
Definition: iseq.h:152
int rb_respond_to(VALUE, ID)
Definition: vm_method.c:1995
register unsigned int len
Definition: zonetab.h:51
VALUE rb_ary_new_from_values(long n, const VALUE *elts)
Definition: array.c:518
#define StringValueCStr(v)
Definition: ruby.h:571
static VALUE iseq_s_load(int argc, VALUE *argv, VALUE self)
Definition: iseq.c:596
#define RSTRING_PTR(str)
Definition: ruby.h:982
unsigned int top
Definition: nkf.c:4310
const rb_iseq_t * rb_iseqw_to_iseq(VALUE iseqw)
Definition: iseq.c:930
VALUE rb_iseq_disasm(const rb_iseq_t *iseq)
Definition: iseq.c:1487
#define OPT_SPECIALISED_INSTRUCTION
Definition: vm_opts.h:24
static VALUE iseq_mark_ary_create(int flip_cnt)
Definition: iseq.h:37
struct rb_iseq_struct::@198::@199 loader
int size
Definition: encoding.c:57
static const struct iseq_line_info_entry * get_line_info(const rb_iseq_t *iseq, size_t pos)
Definition: iseq.c:1186
#define PARAM_TYPE(type)
#define f
#define INT2FIX(i)
Definition: ruby.h:232
static int line_trace_specify(int line, rb_event_flag_t *events_ptr, void *ptr)
Definition: iseq.c:2293
unsigned int start
Definition: iseq.h:156
const struct iseq_catch_table * catch_table
Definition: vm_core.h:366
#define RARRAY_AREF(a, i)
Definition: ruby.h:1040
unsigned int position
Definition: iseq.h:142
int rb_iseq_disasm_insn(VALUE ret, const VALUE *code, size_t pos, const rb_iseq_t *iseq, VALUE child)
Disassemble a instruction Iseq -> Iseq inspect object.
Definition: iseq.c:1402
#define INITIALIZED
#define st_init_numtable
Definition: regint.h:178
#define RBASIC_CLASS(obj)
Definition: ruby.h:878
static rb_iseq_t * iseq_imemo_alloc(void)
Definition: iseq.h:80
static VALUE iseqw_new(const rb_iseq_t *iseq)
Definition: iseq.c:741
#define RUBY_FREE_LEAVE(msg)
Definition: gc.h:56
void rb_error_arity(int argc, int min, int max)
struct rb_iseq_struct * local_iseq
Definition: vm_core.h:370
#define RUBY_FREE_ENTER(msg)
Definition: gc.h:55
const char * ruby_node_name(int node)
Definition: iseq.c:1739
VALUE rb_str_catf(VALUE str, const char *format,...)
Definition: sprintf.c:1480
void rb_iseq_add_mark_object(const rb_iseq_t *iseq, VALUE obj)
Definition: iseq.c:262
VALUE rb_iseq_eval(const rb_iseq_t *iseq)
Definition: vm.c:2004
#define RTEST(v)
Definition: ruby.h:450
#define T_STRING
Definition: ruby.h:496
#define PRIuSIZE
Definition: ruby.h:177
#define RUBY_MARK_UNLESS_NULL(ptr)
Definition: gc.h:60
#define T_FILE
Definition: ruby.h:502
VALUE rb_str_inspect(VALUE)
Definition: string.c:5664
VALUE rb_iseq_path(const rb_iseq_t *iseq)
Definition: iseq.c:670
#define VM_CALL_TAILCALL
Definition: vm_core.h:910
VALUE rb_ary_join(VALUE ary, VALUE sep)
Definition: array.c:2034
#define st_insert
Definition: regint.h:184
#define CHECK_STRING(v)
Definition: iseq.c:503
static const char * catch_type(int type)
Definition: iseq.c:1454
VALUE rb_iseq_base_label(const rb_iseq_t *iseq)
Definition: iseq.c:688
const struct rb_iseq_struct * parent_iseq
Definition: vm_core.h:369
VALUE rb_parser_new(void)
Definition: ripper.c:17739
static size_t iseqw_memsize(const void *ptr)
Definition: iseq.c:729
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
Definition: file.c:3873
const struct iseq_line_info_entry * line_info_table
Definition: vm_core.h:361
const char * name
Definition: nkf.c:208
const VALUE * opt_table
Definition: vm_core.h:333
static void iseqw_mark(void *ptr)
Definition: iseq.c:723
#define ID2SYM(x)
Definition: ruby.h:383
static const rb_compile_option_t COMPILE_OPTION_FALSE
Definition: iseq.c:348
rb_iseq_t * rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE absolute_path, VALUE line, const struct rb_block *base_block, VALUE opt)
Definition: iseq.c:610
VALUE() rb_ary_new_from_args(long n,...)
Definition: array.c:499
unsigned int line_info_size
Definition: vm_core.h:386
#define st_free_table
Definition: regint.h:188
VALUE rb_inspect(VALUE)
Definition: object.c:519
uint32_t rb_event_flag_t
Definition: ruby.h:2095
static void set_compile_option_from_hash(rb_compile_option_t *option, VALUE opt)
Definition: iseq.c:351
rb_iseq_t * rb_iseq_new_top(NODE *node, VALUE name, VALUE path, VALUE absolute_path, const rb_iseq_t *parent)
Definition: iseq.c:444
void rb_secure(int)
Definition: safe.c:83
#define CONST_ID(var, str)
Definition: ruby.h:1743
VALUE rb_str_intern(VALUE)
Definition: symbol.c:661
#define SPECIAL_CONST_P(x)
Definition: ruby.h:1249
#define ISEQ_COMPILE_DATA(iseq)
Definition: iseq.h:77
static VALUE iseqw_s_of(VALUE klass, VALUE body)
Definition: iseq.c:1659
Definition: iseq.h:141
VALUE rb_ary_resurrect(VALUE ary)
Definition: array.c:1937
VALUE rb_iseqw_local_variables(VALUE iseqval)
Definition: iseq.c:2350
ID id_ensure
Definition: eventids1.c:48
#define rb_intern(str)
#define ISEQ_MARK_ARY(iseq)
Definition: iseq.h:46
unsigned int size
Definition: vm_core.h:324
static int collect_trace(int line, rb_event_flag_t *events_ptr, void *ptr)
Definition: iseq.c:2266
#define NULL
Definition: _sdbm.c:102
unsigned int line_no
Definition: iseq.h:143
#define Qundef
Definition: ruby.h:439
unsigned int sp
Definition: iseq.h:159
const rb_iseq_t * iseq_ibf_load(VALUE str)
Definition: compile.c:8630
static VALUE iseqw_absolute_path(VALUE self)
Definition: iseq.c:1015
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1515
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2818
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
Definition: iseq.c:2097
static rb_iseq_t * iseq_translate(rb_iseq_t *iseq)
Definition: iseq.c:459
static ID id_method
Definition: enumerator.c:106
#define OPT_OPERANDS_UNIFICATION
Definition: vm_opts.h:45
static rb_iseq_t * iseq_alloc(void)
Definition: iseq.c:212
#define NUM2LONG(x)
Definition: ruby.h:648
static void compile_data_free(struct iseq_compile_data *compile_data)
Definition: iseq.c:52
#define RB_OBJ_WRITE(a, slot, b)
Definition: ruby.h:1437
static VALUE iseqw_to_a(VALUE self)
Definition: iseq.c:1175
char ** argv
Definition: ruby.c:184
#define StringValue(v)
Definition: ruby.h:569
rb_iseq_location_t location
Definition: vm_core.h:358
#define rb_sym2str(sym)
Definition: console.c:107
VALUE rb_str_new(const char *, long)
Definition: string.c:736
#define SIGNED_VALUE
Definition: ruby.h:87
#define GET_VM()
Definition: vm_core.h:1507