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