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