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