Ruby  2.1.3p242(2014-09-19revision47630)
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 #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) {
349  *option = COMPILE_OPTION_DEFAULT;
350  }
351  else if (opt == Qfalse) {
352  *option = COMPILE_OPTION_FALSE;
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) {
360  *option = COMPILE_OPTION_DEFAULT;
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,
417  &COMPILE_OPTION_DEFAULT);
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,
424  &COMPILE_OPTION_DEFAULT);
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  node = rb_parser_compile_string_path(parser, file, src, ln);
614 
615  if (!node) {
616  rb_exc_raise(GET_THREAD()->errinfo); /* TODO: check err */
617  }
618  }
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 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 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 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 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 VALUE
959 {
960  rb_iseq_t *iseq;
961  GetISeqPtr(self, iseq);
962  return iseq->location.first_lineno;
963 }
964 
965 VALUE
967 {
968  rb_iseq_t *iseq;
969  GetISeqPtr(self, iseq);
970  return iseq->local_iseq->klass;
971 }
972 
973 VALUE
975 {
976  rb_iseq_t *iseq, *local_iseq;
977  GetISeqPtr(self, iseq);
978  local_iseq = iseq->local_iseq;
979  if (local_iseq->type == ISEQ_TYPE_METHOD) {
980  return local_iseq->location.base_label;
981  }
982  else {
983  return Qnil;
984  }
985 }
986 
987 static
989 
990 /*
991  * call-seq:
992  * iseq.to_a -> ary
993  *
994  * Returns an Array with 14 elements representing the instruction sequence
995  * with the following data:
996  *
997  * [magic]
998  * A string identifying the data format. <b>Always
999  * +YARVInstructionSequence/SimpleDataFormat+.</b>
1000  *
1001  * [major_version]
1002  * The major version of the instruction sequence.
1003  *
1004  * [minor_version]
1005  * The minor version of the instruction sequence.
1006  *
1007  * [format_type]
1008  * A number identifying the data format. <b>Always 1</b>.
1009  *
1010  * [misc]
1011  * A hash containing:
1012  *
1013  * [+:arg_size+]
1014  * the total number of arguments taken by the method or the block (0 if
1015  * _iseq_ doesn't represent a method or block)
1016  * [+:local_size+]
1017  * the number of local variables + 1
1018  * [+:stack_max+]
1019  * used in calculating the stack depth at which a SystemStackError is
1020  * thrown.
1021  *
1022  * [#label]
1023  * The name of the context (block, method, class, module, etc.) that this
1024  * instruction sequence belongs to.
1025  *
1026  * <code><main></code> if it's at the top level, <code><compiled></code> if
1027  * it was evaluated from a string.
1028  *
1029  * [#path]
1030  * The relative path to the Ruby file where the instruction sequence was
1031  * loaded from.
1032  *
1033  * <code><compiled></code> if the iseq was evaluated from a string.
1034  *
1035  * [#absolute_path]
1036  * The absolute path to the Ruby file where the instruction sequence was
1037  * loaded from.
1038  *
1039  * +nil+ if the iseq was evaluated from a string.
1040  *
1041  * [#first_lineno]
1042  * The number of the first source line where the instruction sequence was
1043  * loaded from.
1044  *
1045  * [type]
1046  * The type of the instruction sequence.
1047  *
1048  * Valid values are +:top+, +:method+, +:block+, +:class+, +:rescue+,
1049  * +:ensure+, +:eval+, +:main+, and +:defined_guard+.
1050  *
1051  * [locals]
1052  * An array containing the names of all arguments and local variables as
1053  * symbols.
1054  *
1055  * [args]
1056  * The arity if the method or block only has required arguments.
1057  *
1058  * Otherwise an array of:
1059  *
1060  * [required_argc, [optional_arg_labels, ...],
1061  * splat_index, post_splat_argc, post_splat_index,
1062  * block_index, simple]
1063  *
1064  * More info about these values can be found in +vm_core.h+.
1065  *
1066  * [catch_table]
1067  * A list of exceptions and control flow operators (rescue, next, redo,
1068  * break, etc.).
1069  *
1070  * [bytecode]
1071  * An array of arrays containing the instruction names and operands that
1072  * make up the body of the instruction sequence.
1073  *
1074  */
1075 static VALUE
1077 {
1078  rb_iseq_t *iseq = iseq_check(self);
1079  rb_secure(1);
1080  return iseq_data_to_ary(iseq);
1081 }
1082 
1083 /* TODO: search algorithm is brute force.
1084  this should be binary search or so. */
1085 
1086 static struct iseq_line_info_entry *
1087 get_line_info(const rb_iseq_t *iseq, size_t pos)
1088 {
1089  size_t i = 0, size = iseq->line_info_size;
1090  struct iseq_line_info_entry *table = iseq->line_info_table;
1091  const int debug = 0;
1092 
1093  if (debug) {
1094  printf("size: %"PRIdSIZE"\n", size);
1095  printf("table[%"PRIdSIZE"]: position: %d, line: %d, pos: %"PRIdSIZE"\n",
1096  i, table[i].position, table[i].line_no, pos);
1097  }
1098 
1099  if (size == 0) {
1100  return 0;
1101  }
1102  else if (size == 1) {
1103  return &table[0];
1104  }
1105  else {
1106  for (i=1; i<size; i++) {
1107  if (debug) printf("table[%"PRIdSIZE"]: position: %d, line: %d, pos: %"PRIdSIZE"\n",
1108  i, table[i].position, table[i].line_no, pos);
1109 
1110  if (table[i].position == pos) {
1111  return &table[i];
1112  }
1113  if (table[i].position > pos) {
1114  return &table[i-1];
1115  }
1116  }
1117  }
1118  return &table[i-1];
1119 }
1120 
1121 static unsigned int
1122 find_line_no(const rb_iseq_t *iseq, size_t pos)
1123 {
1124  struct iseq_line_info_entry *entry = get_line_info(iseq, pos);
1125  if (entry) {
1126  return entry->line_no;
1127  }
1128  else {
1129  return 0;
1130  }
1131 }
1132 
1133 unsigned int
1134 rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
1135 {
1136  if (pos == 0) {
1137  return find_line_no(iseq, pos);
1138  }
1139  else {
1140  return find_line_no(iseq, pos - 1);
1141  }
1142 }
1143 
1144 static VALUE
1145 id_to_name(ID id, VALUE default_value)
1146 {
1147  VALUE str = rb_id2str(id);
1148  if (!str) {
1149  str = default_value;
1150  }
1151  else if (!rb_str_symname_p(str)) {
1152  str = rb_str_inspect(str);
1153  }
1154  return str;
1155 }
1156 
1157 VALUE
1159  VALUE insn, int op_no, VALUE op,
1160  int len, size_t pos, VALUE *pnop, VALUE child)
1161 {
1162  const char *types = insn_op_types(insn);
1163  char type = types[op_no];
1164  VALUE ret;
1165 
1166  switch (type) {
1167  case TS_OFFSET: /* LONG */
1168  ret = rb_sprintf("%"PRIdVALUE, (VALUE)(pos + len + op));
1169  break;
1170 
1171  case TS_NUM: /* ULONG */
1172  ret = rb_sprintf("%"PRIuVALUE, op);
1173  break;
1174 
1175  case TS_LINDEX:{
1176  if (insn == BIN(getlocal) || insn == BIN(setlocal)) {
1177  if (pnop) {
1178  rb_iseq_t *diseq = iseq;
1179  VALUE level = *pnop, i;
1180 
1181  for (i = 0; i < level; i++) {
1182  diseq = diseq->parent_iseq;
1183  }
1184  ret = id_to_name(diseq->local_table[diseq->local_size - op], INT2FIX('*'));
1185  }
1186  else {
1187  ret = rb_sprintf("%"PRIuVALUE, op);
1188  }
1189  }
1190  else {
1191  ret = rb_inspect(INT2FIX(op));
1192  }
1193  break;
1194  }
1195  case TS_ID: /* ID (symbol) */
1196  op = ID2SYM(op);
1197 
1198  case TS_VALUE: /* VALUE */
1199  op = obj_resurrect(op);
1200  ret = rb_inspect(op);
1201  if (CLASS_OF(op) == rb_cISeq) {
1202  if (child) {
1203  rb_ary_push(child, op);
1204  }
1205  }
1206  break;
1207 
1208  case TS_ISEQ: /* iseq */
1209  {
1210  rb_iseq_t *iseq = (rb_iseq_t *)op;
1211  if (iseq) {
1212  ret = iseq->location.label;
1213  if (child) {
1214  rb_ary_push(child, iseq->self);
1215  }
1216  }
1217  else {
1218  ret = rb_str_new2("nil");
1219  }
1220  break;
1221  }
1222  case TS_GENTRY:
1223  {
1224  struct rb_global_entry *entry = (struct rb_global_entry *)op;
1225  ret = rb_str_dup(rb_id2str(entry->id));
1226  }
1227  break;
1228 
1229  case TS_IC:
1230  ret = rb_sprintf("<is:%"PRIdPTRDIFF">", (union iseq_inline_storage_entry *)op - iseq->is_entries);
1231  break;
1232 
1233  case TS_CALLINFO:
1234  {
1235  rb_call_info_t *ci = (rb_call_info_t *)op;
1236  VALUE ary = rb_ary_new();
1237 
1238  if (ci->mid) {
1239  rb_ary_push(ary, rb_sprintf("mid:%s", rb_id2name(ci->mid)));
1240  }
1241 
1242  rb_ary_push(ary, rb_sprintf("argc:%d", ci->orig_argc));
1243 
1244  if (ci->blockiseq) {
1245  if (child) {
1246  rb_ary_push(child, ci->blockiseq->self);
1247  }
1248  rb_ary_push(ary, rb_sprintf("block:%"PRIsVALUE, ci->blockiseq->location.label));
1249  }
1250 
1251  if (ci->flag) {
1252  VALUE flags = rb_ary_new();
1253  if (ci->flag & VM_CALL_ARGS_SPLAT) rb_ary_push(flags, rb_str_new2("ARGS_SPLAT"));
1254  if (ci->flag & VM_CALL_ARGS_BLOCKARG) rb_ary_push(flags, rb_str_new2("ARGS_BLOCKARG"));
1255  if (ci->flag & VM_CALL_FCALL) rb_ary_push(flags, rb_str_new2("FCALL"));
1256  if (ci->flag & VM_CALL_VCALL) rb_ary_push(flags, rb_str_new2("VCALL"));
1257  if (ci->flag & VM_CALL_TAILCALL) rb_ary_push(flags, rb_str_new2("TAILCALL"));
1258  if (ci->flag & VM_CALL_SUPER) rb_ary_push(flags, rb_str_new2("SUPER"));
1259  if (ci->flag & VM_CALL_OPT_SEND) rb_ary_push(flags, rb_str_new2("SNED")); /* maybe not reachable */
1260  if (ci->flag & VM_CALL_ARGS_SKIP_SETUP) rb_ary_push(flags, rb_str_new2("ARGS_SKIP")); /* maybe not reachable */
1261  rb_ary_push(ary, rb_ary_join(flags, rb_str_new2("|")));
1262  }
1263  ret = rb_sprintf("<callinfo!%"PRIsVALUE">", rb_ary_join(ary, rb_str_new2(", ")));
1264  }
1265  break;
1266 
1267  case TS_CDHASH:
1268  ret = rb_str_new2("<cdhash>");
1269  break;
1270 
1271  case TS_FUNCPTR:
1272  ret = rb_str_new2("<funcptr>");
1273  break;
1274 
1275  default:
1276  rb_bug("insn_operand_intern: unknown operand type: %c", type);
1277  }
1278  return ret;
1279 }
1280 
1285 int
1286 rb_iseq_disasm_insn(VALUE ret, VALUE *iseq, size_t pos,
1287  rb_iseq_t *iseqdat, VALUE child)
1288 {
1289  VALUE insn = iseq[pos];
1290  int len = insn_len(insn);
1291  int j;
1292  const char *types = insn_op_types(insn);
1293  VALUE str = rb_str_new(0, 0);
1294  const char *insn_name_buff;
1295 
1296  insn_name_buff = insn_name(insn);
1297  if (1) {
1298  rb_str_catf(str, "%04"PRIdSIZE" %-16s ", pos, insn_name_buff);
1299  }
1300  else {
1301  rb_str_catf(str, "%04"PRIdSIZE" %-16.*s ", pos,
1302  (int)strcspn(insn_name_buff, "_"), insn_name_buff);
1303  }
1304 
1305  for (j = 0; types[j]; j++) {
1306  const char *types = insn_op_types(insn);
1307  VALUE opstr = rb_insn_operand_intern(iseqdat, insn, j, iseq[pos + j + 1],
1308  len, pos, &iseq[pos + j + 2],
1309  child);
1310  rb_str_concat(str, opstr);
1311 
1312  if (types[j + 1]) {
1313  rb_str_cat2(str, ", ");
1314  }
1315  }
1316 
1317  {
1318  unsigned int line_no = find_line_no(iseqdat, pos);
1319  unsigned int prev = pos == 0 ? 0 : find_line_no(iseqdat, pos - 1);
1320  if (line_no && line_no != prev) {
1321  long slen = RSTRING_LEN(str);
1322  slen = (slen > 70) ? 0 : (70 - slen);
1323  str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
1324  }
1325  }
1326 
1327  if (ret) {
1328  rb_str_cat2(str, "\n");
1329  rb_str_concat(ret, str);
1330  }
1331  else {
1332  printf("%s\n", RSTRING_PTR(str));
1333  }
1334  return len;
1335 }
1336 
1337 static const char *
1339 {
1340  switch (type) {
1341  case CATCH_TYPE_RESCUE:
1342  return "rescue";
1343  case CATCH_TYPE_ENSURE:
1344  return "ensure";
1345  case CATCH_TYPE_RETRY:
1346  return "retry";
1347  case CATCH_TYPE_BREAK:
1348  return "break";
1349  case CATCH_TYPE_REDO:
1350  return "redo";
1351  case CATCH_TYPE_NEXT:
1352  return "next";
1353  default:
1354  rb_bug("unknown catch type (%d)", type);
1355  return 0;
1356  }
1357 }
1358 
1359 /*
1360  * call-seq:
1361  * iseq.disasm -> str
1362  * iseq.disassemble -> str
1363  *
1364  * Returns the instruction sequence as a +String+ in human readable form.
1365  *
1366  * puts RubyVM::InstructionSequence.compile('1 + 2').disasm
1367  *
1368  * Produces:
1369  *
1370  * == disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
1371  * 0000 trace 1 ( 1)
1372  * 0002 putobject 1
1373  * 0004 putobject 2
1374  * 0006 opt_plus <ic:1>
1375  * 0008 leave
1376  */
1377 VALUE
1379 {
1380  rb_iseq_t *iseqdat = iseq_check(self);
1381  VALUE *iseq;
1382  VALUE str = rb_str_new(0, 0);
1383  VALUE child = rb_ary_new();
1384  unsigned long size;
1385  int i;
1386  long l;
1387  ID *tbl;
1388  size_t n;
1389  enum {header_minlen = 72};
1390 
1391  rb_secure(1);
1392 
1393  iseq = iseqdat->iseq;
1394  size = iseqdat->iseq_size;
1395 
1396  rb_str_cat2(str, "== disasm: ");
1397 
1398  rb_str_concat(str, iseq_inspect(iseqdat->self));
1399  if ((l = RSTRING_LEN(str)) < header_minlen) {
1400  rb_str_resize(str, header_minlen);
1401  memset(RSTRING_PTR(str) + l, '=', header_minlen - l);
1402  }
1403  rb_str_cat2(str, "\n");
1404 
1405  /* show catch table information */
1406  if (iseqdat->catch_table_size != 0) {
1407  rb_str_cat2(str, "== catch table\n");
1408  }
1409  for (i = 0; i < iseqdat->catch_table_size; i++) {
1410  struct iseq_catch_table_entry *entry = &iseqdat->catch_table[i];
1411  rb_str_catf(str,
1412  "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
1413  catch_type((int)entry->type), (int)entry->start,
1414  (int)entry->end, (int)entry->sp, (int)entry->cont);
1415  if (entry->iseq) {
1416  rb_str_concat(str, rb_iseq_disasm(entry->iseq));
1417  }
1418  }
1419  if (iseqdat->catch_table_size != 0) {
1420  rb_str_cat2(str, "|-------------------------------------"
1421  "-----------------------------------\n");
1422  }
1423 
1424  /* show local table information */
1425  tbl = iseqdat->local_table;
1426 
1427  if (tbl) {
1428  rb_str_catf(str,
1429  "local table (size: %d, argc: %d "
1430  "[opts: %d, rest: %d, post: %d, block: %d, keyword: %d@%d] s%d)\n",
1431  iseqdat->local_size, iseqdat->argc,
1432  iseqdat->arg_opts, iseqdat->arg_rest,
1433  iseqdat->arg_post_len, iseqdat->arg_block,
1434  iseqdat->arg_keywords, iseqdat->local_size-iseqdat->arg_keyword,
1435  iseqdat->arg_simple);
1436 
1437  for (i = 0; i < iseqdat->local_table_size; i++) {
1438  long width;
1439  VALUE name = id_to_name(tbl[i], 0);
1440  char argi[0x100] = "";
1441  char opti[0x100] = "";
1442 
1443  if (iseqdat->arg_opts) {
1444  int argc = iseqdat->argc;
1445  int opts = iseqdat->arg_opts;
1446  if (i >= argc && i < argc + opts - 1) {
1447  snprintf(opti, sizeof(opti), "Opt=%"PRIdVALUE,
1448  iseqdat->arg_opt_table[i - argc]);
1449  }
1450  }
1451 
1452  snprintf(argi, sizeof(argi), "%s%s%s%s%s", /* arg, opts, rest, post block */
1453  iseqdat->argc > i ? "Arg" : "",
1454  opti,
1455  iseqdat->arg_rest == i ? "Rest" : "",
1456  (iseqdat->arg_post_start <= i &&
1457  i < iseqdat->arg_post_start + iseqdat->arg_post_len) ? "Post" : "",
1458  iseqdat->arg_block == i ? "Block" : "");
1459 
1460  rb_str_catf(str, "[%2d] ", iseqdat->local_size - i);
1461  width = RSTRING_LEN(str) + 11;
1462  if (name)
1463  rb_str_append(str, name);
1464  else
1465  rb_str_cat2(str, "?");
1466  if (*argi) rb_str_catf(str, "<%s>", argi);
1467  if ((width -= RSTRING_LEN(str)) > 0) rb_str_catf(str, "%*s", (int)width, "");
1468  }
1469  rb_str_cat2(str, "\n");
1470  }
1471 
1472  /* show each line */
1473  for (n = 0; n < size;) {
1474  n += rb_iseq_disasm_insn(str, iseq, n, iseqdat, child);
1475  }
1476 
1477  for (i = 0; i < RARRAY_LEN(child); i++) {
1478  VALUE isv = rb_ary_entry(child, i);
1479  rb_str_concat(str, rb_iseq_disasm(isv));
1480  }
1481 
1482  return str;
1483 }
1484 
1485 /*
1486  * Returns the instruction sequence containing the given proc or method.
1487  *
1488  * For example, using irb:
1489  *
1490  * # a proc
1491  * > p = proc { num = 1 + 2 }
1492  * > RubyVM::InstructionSequence.of(p)
1493  * > #=> <RubyVM::InstructionSequence:block in irb_binding@(irb)>
1494  *
1495  * # for a method
1496  * > def foo(bar); puts bar; end
1497  * > RubyVM::InstructionSequence.of(method(:foo))
1498  * > #=> <RubyVM::InstructionSequence:foo@(irb)>
1499  *
1500  * Using ::compile_file:
1501  *
1502  * # /tmp/iseq_of.rb
1503  * def hello
1504  * puts "hello, world"
1505  * end
1506  *
1507  * $a_global_proc = proc { str = 'a' + 'b' }
1508  *
1509  * # in irb
1510  * > require '/tmp/iseq_of.rb'
1511  *
1512  * # first the method hello
1513  * > RubyVM::InstructionSequence.of(method(:hello))
1514  * > #=> #<RubyVM::InstructionSequence:0x007fb73d7cb1d0>
1515  *
1516  * # then the global proc
1517  * > RubyVM::InstructionSequence.of($a_global_proc)
1518  * > #=> #<RubyVM::InstructionSequence:0x007fb73d7caf78>
1519  */
1520 static VALUE
1521 iseq_s_of(VALUE klass, VALUE body)
1522 {
1523  VALUE ret = Qnil;
1524  rb_iseq_t *iseq;
1525 
1526  rb_secure(1);
1527 
1528  if (rb_obj_is_proc(body)) {
1529  rb_proc_t *proc;
1530  GetProcPtr(body, proc);
1531  iseq = proc->block.iseq;
1532  if (RUBY_VM_NORMAL_ISEQ_P(iseq)) {
1533  ret = iseq->self;
1534  }
1535  }
1536  else if ((iseq = rb_method_get_iseq(body)) != 0) {
1537  ret = iseq->self;
1538  }
1539  return ret;
1540 }
1541 
1542 /*
1543  * call-seq:
1544  * InstructionSequence.disasm(body) -> str
1545  * InstructionSequence.disassemble(body) -> str
1546  *
1547  * Takes +body+, a Method or Proc object, and returns a String with the
1548  * human readable instructions for +body+.
1549  *
1550  * For a Method object:
1551  *
1552  * # /tmp/method.rb
1553  * def hello
1554  * puts "hello, world"
1555  * end
1556  *
1557  * puts RubyVM::InstructionSequence.disasm(method(:hello))
1558  *
1559  * Produces:
1560  *
1561  * == disasm: <RubyVM::InstructionSequence:hello@/tmp/method.rb>============
1562  * 0000 trace 8 ( 1)
1563  * 0002 trace 1 ( 2)
1564  * 0004 putself
1565  * 0005 putstring "hello, world"
1566  * 0007 send :puts, 1, nil, 8, <ic:0>
1567  * 0013 trace 16 ( 3)
1568  * 0015 leave ( 2)
1569  *
1570  * For a Proc:
1571  *
1572  * # /tmp/proc.rb
1573  * p = proc { num = 1 + 2 }
1574  * puts RubyVM::InstructionSequence.disasm(p)
1575  *
1576  * Produces:
1577  *
1578  * == disasm: <RubyVM::InstructionSequence:block in <main>@/tmp/proc.rb>===
1579  * == catch table
1580  * | catch type: redo st: 0000 ed: 0012 sp: 0000 cont: 0000
1581  * | catch type: next st: 0000 ed: 0012 sp: 0000 cont: 0012
1582  * |------------------------------------------------------------------------
1583  * local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1] s1)
1584  * [ 2] num
1585  * 0000 trace 1 ( 1)
1586  * 0002 putobject 1
1587  * 0004 putobject 2
1588  * 0006 opt_plus <ic:1>
1589  * 0008 dup
1590  * 0009 setlocal num, 0
1591  * 0012 leave
1592  *
1593  */
1594 
1595 static VALUE
1597 {
1598  VALUE iseqval = iseq_s_of(klass, body);
1599  return NIL_P(iseqval) ? Qnil : rb_iseq_disasm(iseqval);
1600 }
1601 
1602 const char *
1604 {
1605  switch (node) {
1606 #include "node_name.inc"
1607  default:
1608  rb_bug("unknown node (%d)", node);
1609  return 0;
1610  }
1611 }
1612 
1613 #define DECL_SYMBOL(name) \
1614  static VALUE sym_##name
1615 
1616 #define INIT_SYMBOL(name) \
1617  sym_##name = ID2SYM(rb_intern(#name))
1618 
1619 static VALUE
1620 register_label(struct st_table *table, unsigned long idx)
1621 {
1622  VALUE sym;
1623  char buff[8 + (sizeof(idx) * CHAR_BIT * 32 / 100)];
1624 
1625  snprintf(buff, sizeof(buff), "label_%lu", idx);
1626  sym = ID2SYM(rb_intern(buff));
1627  st_insert(table, idx, sym);
1628  return sym;
1629 }
1630 
1631 static VALUE
1633 {
1634  ID id;
1635  switch (type) {
1636  case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
1637  case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
1638  case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
1639  case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
1640  case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
1641  case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
1642  default:
1643  rb_bug("...");
1644  }
1645  return ID2SYM(id);
1646 }
1647 
1648 static int
1650 {
1651  rb_ary_push(ary, obj_resurrect(key));
1652  rb_ary_push(ary, value);
1653  return ST_CONTINUE;
1654 }
1655 
1656 static VALUE
1658 {
1659  long i;
1660  size_t ti;
1661  unsigned int pos;
1662  unsigned int line = 0;
1663  VALUE *seq;
1664 
1665  VALUE val = rb_ary_new();
1666  VALUE type; /* Symbol */
1667  VALUE locals = rb_ary_new();
1668  VALUE args = rb_ary_new();
1669  VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
1670  VALUE nbody;
1671  VALUE exception = rb_ary_new(); /* [[....]] */
1672  VALUE misc = rb_hash_new();
1673 
1674  static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1675  struct st_table *labels_table = st_init_numtable();
1676 
1677  DECL_SYMBOL(top);
1678  DECL_SYMBOL(method);
1679  DECL_SYMBOL(block);
1680  DECL_SYMBOL(class);
1681  DECL_SYMBOL(rescue);
1682  DECL_SYMBOL(ensure);
1683  DECL_SYMBOL(eval);
1684  DECL_SYMBOL(main);
1685  DECL_SYMBOL(defined_guard);
1686 
1687  if (sym_top == 0) {
1688  int i;
1689  for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1690  insn_syms[i] = ID2SYM(rb_intern(insn_name(i)));
1691  }
1692  INIT_SYMBOL(top);
1693  INIT_SYMBOL(method);
1694  INIT_SYMBOL(block);
1695  INIT_SYMBOL(class);
1696  INIT_SYMBOL(rescue);
1697  INIT_SYMBOL(ensure);
1698  INIT_SYMBOL(eval);
1699  INIT_SYMBOL(main);
1700  INIT_SYMBOL(defined_guard);
1701  }
1702 
1703  /* type */
1704  switch (iseq->type) {
1705  case ISEQ_TYPE_TOP: type = sym_top; break;
1706  case ISEQ_TYPE_METHOD: type = sym_method; break;
1707  case ISEQ_TYPE_BLOCK: type = sym_block; break;
1708  case ISEQ_TYPE_CLASS: type = sym_class; break;
1709  case ISEQ_TYPE_RESCUE: type = sym_rescue; break;
1710  case ISEQ_TYPE_ENSURE: type = sym_ensure; break;
1711  case ISEQ_TYPE_EVAL: type = sym_eval; break;
1712  case ISEQ_TYPE_MAIN: type = sym_main; break;
1713  case ISEQ_TYPE_DEFINED_GUARD: type = sym_defined_guard; break;
1714  default: rb_bug("unsupported iseq type");
1715  };
1716 
1717  /* locals */
1718  for (i=0; i<iseq->local_table_size; i++) {
1719  ID lid = iseq->local_table[i];
1720  if (lid) {
1721  if (rb_id2str(lid)) rb_ary_push(locals, ID2SYM(lid));
1722  }
1723  else {
1724  rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
1725  }
1726  }
1727 
1728  /* args */
1729  {
1730  /*
1731  * [argc, # argc
1732  * [label1, label2, ...] # opts
1733  * rest index,
1734  * post_len
1735  * post_start
1736  * block index,
1737  * simple,
1738  * ]
1739  */
1740  VALUE arg_opt_labels = rb_ary_new();
1741  int j;
1742 
1743  for (j=0; j<iseq->arg_opts; j++) {
1744  rb_ary_push(arg_opt_labels,
1745  register_label(labels_table, iseq->arg_opt_table[j]));
1746  }
1747 
1748  /* commit */
1749  if (iseq->arg_simple == 1) {
1750  args = INT2FIX(iseq->argc);
1751  }
1752  else {
1753  rb_ary_push(args, INT2FIX(iseq->argc));
1754  rb_ary_push(args, arg_opt_labels);
1755  rb_ary_push(args, INT2FIX(iseq->arg_post_len));
1756  rb_ary_push(args, INT2FIX(iseq->arg_post_start));
1757  rb_ary_push(args, INT2FIX(iseq->arg_rest));
1758  rb_ary_push(args, INT2FIX(iseq->arg_block));
1759  rb_ary_push(args, INT2FIX(iseq->arg_simple));
1760  }
1761  }
1762 
1763  /* body */
1764  for (seq = iseq->iseq; seq < iseq->iseq + iseq->iseq_size; ) {
1765  VALUE insn = *seq++;
1766  int j, len = insn_len(insn);
1767  VALUE *nseq = seq + len - 1;
1768  VALUE ary = rb_ary_new2(len);
1769 
1770  rb_ary_push(ary, insn_syms[insn]);
1771  for (j=0; j<len-1; j++, seq++) {
1772  switch (insn_op_type(insn, j)) {
1773  case TS_OFFSET: {
1774  unsigned long idx = nseq - iseq->iseq + *seq;
1775  rb_ary_push(ary, register_label(labels_table, idx));
1776  break;
1777  }
1778  case TS_LINDEX:
1779  case TS_NUM:
1780  rb_ary_push(ary, INT2FIX(*seq));
1781  break;
1782  case TS_VALUE:
1783  rb_ary_push(ary, obj_resurrect(*seq));
1784  break;
1785  case TS_ISEQ:
1786  {
1787  rb_iseq_t *iseq = (rb_iseq_t *)*seq;
1788  if (iseq) {
1789  VALUE val = iseq_data_to_ary(iseq);
1790  rb_ary_push(ary, val);
1791  }
1792  else {
1793  rb_ary_push(ary, Qnil);
1794  }
1795  }
1796  break;
1797  case TS_GENTRY:
1798  {
1799  struct rb_global_entry *entry = (struct rb_global_entry *)*seq;
1800  rb_ary_push(ary, ID2SYM(entry->id));
1801  }
1802  break;
1803  case TS_IC:
1804  {
1805  union iseq_inline_storage_entry *is = (union iseq_inline_storage_entry *)*seq;
1806  rb_ary_push(ary, INT2FIX(is - iseq->is_entries));
1807  }
1808  break;
1809  case TS_CALLINFO:
1810  {
1811  rb_call_info_t *ci = (rb_call_info_t *)*seq;
1812  VALUE e = rb_hash_new();
1813  rb_hash_aset(e, ID2SYM(rb_intern("mid")), ci->mid ? ID2SYM(ci->mid) : Qnil);
1814  rb_hash_aset(e, ID2SYM(rb_intern("flag")), ULONG2NUM(ci->flag));
1815  rb_hash_aset(e, ID2SYM(rb_intern("orig_argc")), INT2FIX(ci->orig_argc));
1816  rb_hash_aset(e, ID2SYM(rb_intern("blockptr")), ci->blockiseq ? iseq_data_to_ary(ci->blockiseq) : Qnil);
1817  rb_ary_push(ary, e);
1818  }
1819  break;
1820  case TS_ID:
1821  rb_ary_push(ary, ID2SYM(*seq));
1822  break;
1823  case TS_CDHASH:
1824  {
1825  VALUE hash = *seq;
1826  VALUE val = rb_ary_new();
1827  int i;
1828 
1829  rb_hash_foreach(hash, cdhash_each, val);
1830 
1831  for (i=0; i<RARRAY_LEN(val); i+=2) {
1832  VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
1833  unsigned long idx = nseq - iseq->iseq + pos;
1834 
1835  rb_ary_store(val, i+1,
1836  register_label(labels_table, idx));
1837  }
1838  rb_ary_push(ary, val);
1839  }
1840  break;
1841  default:
1842  rb_bug("unknown operand: %c", insn_op_type(insn, j));
1843  }
1844  }
1845  rb_ary_push(body, ary);
1846  }
1847 
1848  nbody = body;
1849 
1850  /* exception */
1851  for (i=0; i<iseq->catch_table_size; i++) {
1852  VALUE ary = rb_ary_new();
1853  struct iseq_catch_table_entry *entry = &iseq->catch_table[i];
1854  rb_ary_push(ary, exception_type2symbol(entry->type));
1855  if (entry->iseq) {
1856  rb_iseq_t *eiseq;
1857  GetISeqPtr(entry->iseq, eiseq);
1858  rb_ary_push(ary, iseq_data_to_ary(eiseq));
1859  }
1860  else {
1861  rb_ary_push(ary, Qnil);
1862  }
1863  rb_ary_push(ary, register_label(labels_table, entry->start));
1864  rb_ary_push(ary, register_label(labels_table, entry->end));
1865  rb_ary_push(ary, register_label(labels_table, entry->cont));
1866  rb_ary_push(ary, INT2FIX(entry->sp));
1867  rb_ary_push(exception, ary);
1868  }
1869 
1870  /* make body with labels and insert line number */
1871  body = rb_ary_new();
1872  ti = 0;
1873 
1874  for (i=0, pos=0; i<RARRAY_LEN(nbody); i++) {
1875  VALUE ary = RARRAY_AREF(nbody, i);
1876  st_data_t label;
1877 
1878  if (st_lookup(labels_table, pos, &label)) {
1879  rb_ary_push(body, (VALUE)label);
1880  }
1881 
1882  if (ti < iseq->line_info_size && iseq->line_info_table[ti].position == pos) {
1883  line = iseq->line_info_table[ti].line_no;
1884  rb_ary_push(body, INT2FIX(line));
1885  ti++;
1886  }
1887 
1888  rb_ary_push(body, ary);
1889  pos += RARRAY_LENINT(ary); /* reject too huge data */
1890  }
1891 
1892  st_free_table(labels_table);
1893 
1894  rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq->arg_size));
1895  rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq->local_size));
1896  rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq->stack_max));
1897 
1898  /* TODO: compatibility issue */
1899  /*
1900  * [:magic, :major_version, :minor_version, :format_type, :misc,
1901  * :name, :path, :absolute_path, :start_lineno, :type, :locals, :args,
1902  * :catch_table, :bytecode]
1903  */
1904  rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
1905  rb_ary_push(val, INT2FIX(ISEQ_MAJOR_VERSION)); /* major */
1906  rb_ary_push(val, INT2FIX(ISEQ_MINOR_VERSION)); /* minor */
1907  rb_ary_push(val, INT2FIX(1));
1908  rb_ary_push(val, misc);
1909  rb_ary_push(val, iseq->location.label);
1910  rb_ary_push(val, iseq->location.path);
1911  rb_ary_push(val, iseq->location.absolute_path);
1912  rb_ary_push(val, iseq->location.first_lineno);
1913  rb_ary_push(val, type);
1914  rb_ary_push(val, locals);
1915  rb_ary_push(val, args);
1916  rb_ary_push(val, exception);
1917  rb_ary_push(val, body);
1918  return val;
1919 }
1920 
1921 VALUE
1922 rb_iseq_clone(VALUE iseqval, VALUE newcbase)
1923 {
1924  VALUE newiseq = iseq_alloc(rb_cISeq);
1925  rb_iseq_t *iseq0, *iseq1;
1926 
1927  GetISeqPtr(iseqval, iseq0);
1928  GetISeqPtr(newiseq, iseq1);
1929 
1930  MEMCPY(iseq1, iseq0, rb_iseq_t, 1); /* TODO: write barrier? */
1931 
1932  iseq1->self = newiseq;
1933  if (!iseq1->orig) {
1934  RB_OBJ_WRITE(iseq1->self, &iseq1->orig, iseqval);
1935  }
1936  if (iseq0->local_iseq == iseq0) {
1937  iseq1->local_iseq = iseq1;
1938  }
1939  if (newcbase) {
1940  ISEQ_SET_CREF(iseq1, NEW_CREF(newcbase));
1941  RB_OBJ_WRITE(iseq1->cref_stack, &iseq1->cref_stack->nd_refinements, iseq0->cref_stack->nd_refinements);
1942  iseq1->cref_stack->nd_visi = iseq0->cref_stack->nd_visi;
1943  if (iseq0->cref_stack->nd_next) {
1944  RB_OBJ_WRITE(iseq1->cref_stack, &iseq1->cref_stack->nd_next, iseq0->cref_stack->nd_next);
1945  }
1946  RB_OBJ_WRITE(iseq1->self, &iseq1->klass, newcbase);
1947  }
1948 
1949  return newiseq;
1950 }
1951 
1952 VALUE
1953 rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
1954 {
1955  int i, r;
1956  VALUE a, args = rb_ary_new2(iseq->arg_size);
1957  ID req, opt, rest, block, key, keyrest;
1958 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
1959 #define PARAM_ID(i) iseq->local_table[(i)]
1960 #define PARAM(i, type) ( \
1961  PARAM_TYPE(type), \
1962  rb_id2str(PARAM_ID(i)) ? \
1963  rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
1964  a)
1965 
1966  CONST_ID(req, "req");
1967  CONST_ID(opt, "opt");
1968  if (is_proc) {
1969  for (i = 0; i < iseq->argc; i++) {
1970  PARAM_TYPE(opt);
1972  rb_ary_push(args, a);
1973  }
1974  }
1975  else {
1976  for (i = 0; i < iseq->argc; i++) {
1977  rb_ary_push(args, PARAM(i, req));
1978  }
1979  }
1980  r = iseq->argc + iseq->arg_opts - 1;
1981  for (; i < r; i++) {
1982  PARAM_TYPE(opt);
1983  if (rb_id2str(PARAM_ID(i))) {
1984  rb_ary_push(a, ID2SYM(PARAM_ID(i)));
1985  }
1986  rb_ary_push(args, a);
1987  }
1988  if (iseq->arg_rest != -1) {
1989  CONST_ID(rest, "rest");
1990  rb_ary_push(args, PARAM(iseq->arg_rest, rest));
1991  }
1992  r = iseq->arg_post_start + iseq->arg_post_len;
1993  if (is_proc) {
1994  for (i = iseq->arg_post_start; i < r; i++) {
1995  PARAM_TYPE(opt);
1997  rb_ary_push(args, a);
1998  }
1999  }
2000  else {
2001  for (i = iseq->arg_post_start; i < r; i++) {
2002  rb_ary_push(args, PARAM(i, req));
2003  }
2004  }
2005  if (iseq->arg_keyword != -1) {
2006  i = 0;
2007  if (iseq->arg_keyword_required) {
2008  ID keyreq;
2009  CONST_ID(keyreq, "keyreq");
2010  for (; i < iseq->arg_keyword_required; i++) {
2011  PARAM_TYPE(keyreq);
2012  if (rb_id2str(iseq->arg_keyword_table[i])) {
2013  rb_ary_push(a, ID2SYM(iseq->arg_keyword_table[i]));
2014  }
2015  rb_ary_push(args, a);
2016  }
2017  }
2018  CONST_ID(key, "key");
2019  for (; i < iseq->arg_keywords; i++) {
2020  PARAM_TYPE(key);
2021  if (rb_id2str(iseq->arg_keyword_table[i])) {
2022  rb_ary_push(a, ID2SYM(iseq->arg_keyword_table[i]));
2023  }
2024  rb_ary_push(args, a);
2025  }
2026  if (!iseq->arg_keyword_check) {
2027  CONST_ID(keyrest, "keyrest");
2028  rb_ary_push(args, PARAM(iseq->arg_keyword, keyrest));
2029  }
2030  }
2031  if (iseq->arg_block != -1) {
2032  CONST_ID(block, "block");
2033  rb_ary_push(args, PARAM(iseq->arg_block, block));
2034  }
2035  return args;
2036 }
2037 
2038 VALUE
2040 {
2041  static const char expr_names[][18] = {
2042  "nil",
2043  "instance-variable",
2044  "local-variable",
2045  "global-variable",
2046  "class variable",
2047  "constant",
2048  "method",
2049  "yield",
2050  "super",
2051  "self",
2052  "true",
2053  "false",
2054  "assignment",
2055  "expression",
2056  };
2057  const char *estr;
2058  VALUE *defs, str;
2059 
2060  if ((unsigned)(type - 1) >= (unsigned)numberof(expr_names)) return 0;
2061  estr = expr_names[type - 1];
2062  if (!estr[0]) return 0;
2063  defs = GET_VM()->defined_strings;
2064  if (!defs) {
2065  defs = ruby_xcalloc(numberof(expr_names), sizeof(VALUE));
2066  GET_VM()->defined_strings = defs;
2067  }
2068  str = defs[type-1];
2069  if (!str) {
2070  str = rb_str_new_cstr(estr);;
2071  OBJ_FREEZE(str);
2072  defs[type-1] = str;
2073  }
2074  return str;
2075 }
2076 
2077 /* ruby2cext */
2078 
2079 VALUE
2081  const rb_iseq_t *iseq_template,
2082  const rb_insn_func_t *func,
2083  const struct iseq_line_info_entry *line_info_table,
2084  const char **local_table,
2085  const VALUE *arg_opt_table,
2086  const struct iseq_catch_table_entry *catch_table,
2087  const char *name,
2088  const char *path,
2089  const unsigned short first_lineno)
2090 {
2091  unsigned long i;
2092  VALUE iseqval = iseq_alloc(rb_cISeq);
2093  rb_iseq_t *iseq;
2094  GetISeqPtr(iseqval, iseq);
2095 
2096  /* copy iseq */
2097  MEMCPY(iseq, iseq_template, rb_iseq_t, 1); /* TODO: write barrier, *iseq = *iseq_template; */
2098  RB_OBJ_WRITE(iseq->self, &iseq->location.label, rb_str_new2(name));
2099  RB_OBJ_WRITE(iseq->self, &iseq->location.path, rb_str_new2(path));
2100  iseq->location.first_lineno = first_lineno;
2101  RB_OBJ_WRITE(iseq->self, &iseq->mark_ary, 0);
2102  iseq->self = iseqval;
2103 
2104  iseq->iseq = ALLOC_N(VALUE, iseq->iseq_size);
2105 
2106  for (i=0; i<iseq->iseq_size; i+=2) {
2107  iseq->iseq[i] = BIN(opt_call_c_function);
2108  iseq->iseq[i+1] = (VALUE)func;
2109  }
2110 
2112 
2113 #define ALLOC_AND_COPY(dst, src, type, size) do { \
2114  if (size) { \
2115  (dst) = ALLOC_N(type, (size)); \
2116  MEMCPY((dst), (src), type, (size)); \
2117  } \
2118 } while (0)
2119 
2120  ALLOC_AND_COPY(iseq->line_info_table, line_info_table,
2121  struct iseq_line_info_entry, iseq->line_info_size);
2122 
2123  ALLOC_AND_COPY(iseq->catch_table, catch_table,
2125 
2126  ALLOC_AND_COPY(iseq->arg_opt_table, arg_opt_table,
2127  VALUE, iseq->arg_opts);
2128 
2129  set_relation(iseq, 0);
2130 
2131  return iseqval;
2132 }
2133 
2134 /* Experimental tracing support: trace(line) -> trace(specified_line)
2135  * MRI Specific.
2136  */
2137 
2138 int
2139 rb_iseq_line_trace_each(VALUE iseqval, int (*func)(int line, rb_event_flag_t *events_ptr, void *d), void *data)
2140 {
2141  int trace_num = 0;
2142  size_t pos, insn;
2143  rb_iseq_t *iseq;
2144  int cont = 1;
2145  GetISeqPtr(iseqval, iseq);
2146 
2147  for (pos = 0; cont && pos < iseq->iseq_size; pos += insn_len(insn)) {
2148  insn = iseq->iseq[pos];
2149 
2150  if (insn == BIN(trace)) {
2151  rb_event_flag_t current_events = (VALUE)iseq->iseq[pos+1];
2152 
2153  if (current_events & RUBY_EVENT_LINE) {
2154  rb_event_flag_t events = current_events & RUBY_EVENT_SPECIFIED_LINE;
2155  trace_num++;
2156 
2157  if (func) {
2158  int line = find_line_no(iseq, pos);
2159  /* printf("line: %d\n", line); */
2160  cont = (*func)(line, &events, data);
2161  if (current_events != events) {
2162  iseq->iseq[pos+1] = iseq->iseq_encoded[pos+1] =
2163  (VALUE)(current_events | (events & RUBY_EVENT_SPECIFIED_LINE));
2164  }
2165  }
2166  }
2167  }
2168  }
2169  return trace_num;
2170 }
2171 
2172 static int
2173 collect_trace(int line, rb_event_flag_t *events_ptr, void *ptr)
2174 {
2175  VALUE result = (VALUE)ptr;
2176  rb_ary_push(result, INT2NUM(line));
2177  return 1;
2178 }
2179 
2180 /*
2181  * <b>Experimental MRI specific feature, only available as C level api.</b>
2182  *
2183  * Returns all +specified_line+ events.
2184  */
2185 VALUE
2187 {
2188  VALUE result = rb_ary_new();
2189  rb_iseq_line_trace_each(iseqval, collect_trace, (void *)result);
2190  return result;
2191 }
2192 
2194  int pos;
2195  int set;
2196  int prev; /* 1: set, 2: unset, 0: not found */
2197 };
2198 
2199 static int
2200 line_trace_specify(int line, rb_event_flag_t *events_ptr, void *ptr)
2201 {
2202  struct set_specifc_data *data = (struct set_specifc_data *)ptr;
2203 
2204  if (data->pos == 0) {
2205  data->prev = *events_ptr & RUBY_EVENT_SPECIFIED_LINE ? 1 : 2;
2206  if (data->set) {
2207  *events_ptr = *events_ptr | RUBY_EVENT_SPECIFIED_LINE;
2208  }
2209  else {
2210  *events_ptr = *events_ptr & ~RUBY_EVENT_SPECIFIED_LINE;
2211  }
2212  return 0; /* found */
2213  }
2214  else {
2215  data->pos--;
2216  return 1;
2217  }
2218 }
2219 
2220 /*
2221  * <b>Experimental MRI specific feature, only available as C level api.</b>
2222  *
2223  * Set a +specified_line+ event at the given line position, if the +set+
2224  * parameter is +true+.
2225  *
2226  * This method is useful for building a debugger breakpoint at a specific line.
2227  *
2228  * A TypeError is raised if +set+ is not boolean.
2229  *
2230  * If +pos+ is a negative integer a TypeError exception is raised.
2231  */
2232 VALUE
2234 {
2235  struct set_specifc_data data;
2236 
2237  data.prev = 0;
2238  data.pos = NUM2INT(pos);
2239  if (data.pos < 0) rb_raise(rb_eTypeError, "`pos' is negative");
2240 
2241  switch (set) {
2242  case Qtrue: data.set = 1; break;
2243  case Qfalse: data.set = 0; break;
2244  default:
2245  rb_raise(rb_eTypeError, "`set' should be true/false");
2246  }
2247 
2248  rb_iseq_line_trace_each(iseqval, line_trace_specify, (void *)&data);
2249 
2250  if (data.prev == 0) {
2251  rb_raise(rb_eTypeError, "`pos' is out of range.");
2252  }
2253  return data.prev == 1 ? Qtrue : Qfalse;
2254 }
2255 
2256 /*
2257  * Document-class: RubyVM::InstructionSequence
2258  *
2259  * The InstructionSequence class represents a compiled sequence of
2260  * instructions for the Ruby Virtual Machine.
2261  *
2262  * With it, you can get a handle to the instructions that make up a method or
2263  * a proc, compile strings of Ruby code down to VM instructions, and
2264  * disassemble instruction sequences to strings for easy inspection. It is
2265  * mostly useful if you want to learn how the Ruby VM works, but it also lets
2266  * you control various settings for the Ruby iseq compiler.
2267  *
2268  * You can find the source for the VM instructions in +insns.def+ in the Ruby
2269  * source.
2270  *
2271  * The instruction sequence results will almost certainly change as Ruby
2272  * changes, so example output in this documentation may be different from what
2273  * you see.
2274  */
2275 
2276 void
2278 {
2279  /* declare ::RubyVM::InstructionSequence */
2280  rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
2282  rb_define_method(rb_cISeq, "inspect", iseq_inspect, 0);
2283  rb_define_method(rb_cISeq, "disasm", rb_iseq_disasm, 0);
2284  rb_define_method(rb_cISeq, "disassemble", rb_iseq_disasm, 0);
2285  rb_define_method(rb_cISeq, "to_a", iseq_to_a, 0);
2286  rb_define_method(rb_cISeq, "eval", iseq_eval, 0);
2287 
2288  /* location APIs */
2289  rb_define_method(rb_cISeq, "path", rb_iseq_path, 0);
2290  rb_define_method(rb_cISeq, "absolute_path", rb_iseq_absolute_path, 0);
2291  rb_define_method(rb_cISeq, "label", rb_iseq_label, 0);
2292  rb_define_method(rb_cISeq, "base_label", rb_iseq_base_label, 0);
2293  rb_define_method(rb_cISeq, "first_lineno", rb_iseq_first_lineno, 0);
2294 
2295 #if 0
2296  /* Now, it is experimental. No discussions, no tests. */
2297  /* They can be used from C level. Please give us feedback. */
2298  rb_define_method(rb_cISeq, "line_trace_all", rb_iseq_line_trace_all, 0);
2299  rb_define_method(rb_cISeq, "line_trace_specify", rb_iseq_line_trace_specify, 2);
2300 #else
2301  (void)rb_iseq_line_trace_all;
2303 #endif
2304 
2305 #if 0 /* TBD */
2306  rb_define_private_method(rb_cISeq, "marshal_dump", iseq_marshal_dump, 0);
2307  rb_define_private_method(rb_cISeq, "marshal_load", iseq_marshal_load, 1);
2308 #endif
2309 
2310  /* disable this feature because there is no verifier. */
2311  /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1); */
2312  (void)iseq_s_load;
2313 
2322 }
#define RBASIC_CLEAR_CLASS(obj)
Definition: internal.h:607
#define VM_CALL_ARGS_BLOCKARG
Definition: vm_core.h:744
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:1286
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:1134
NODE *const cref_stack
Definition: vm_core.h:315
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1171
#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:1613
#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:12115
static void iseq_mark(void *ptr)
Definition: iseq.c:97
#define INT2NUM(x)
Definition: ruby.h:1288
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:745
Definition: st.h:69
VALUE rb_id2str(ID id)
Definition: ripper.c:17157
ID * arg_keyword_table
Definition: vm_core.h:287
VALUE rb_iseq_line_trace_all(VALUE iseqval)
Definition: iseq.c:2186
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:1655
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:834
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:2139
#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:1500
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:675
VALUE rb_eTypeError
Definition: error.c:548
static VALUE id_to_name(ID id, VALUE default_value)
Definition: iseq.c:1145
int rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
Definition: compile.c:561
#define ULONG2NUM(x)
Definition: ruby.h:1319
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:896
void st_free_table(st_table *)
Definition: st.c:334
VALUE rb_str_concat(VALUE, VALUE)
Definition: string.c:2340
#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:534
VALUE rb_iseq_defined_string(enum defined_type type)
Definition: iseq.c:2039
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:1649
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:676
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1854
#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:743
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
size_t stack_max
Definition: vm_core.h:289
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:5554
int rb_str_cmp(VALUE, VALUE)
Definition: string.c:2486
#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:746
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:264
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:709
#define CHECK_SYMBOL(v)
Definition: iseq.c:474
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1664
void * ruby_xcalloc(size_t n, size_t size)
Definition: gc.c:6191
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:1351
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:1087
int arg_keyword_required
Definition: vm_core.h:286
int rb_str_symname_p(VALUE)
Definition: string.c:8381
#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:1333
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:1393
#define EXEC_TAG()
Definition: eval_intern.h:168
#define VM_CALL_ARGS_SKIP_SETUP
Definition: vm_core.h:750
VALUE mark_ary
Definition: iseq.h:83
#define val
Definition: node.h:499
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:1553
#define PRIdVALUE
Definition: ruby.h:132
#define PARAM(i, type)
static VALUE register_label(struct st_table *table, unsigned long idx)
Definition: iseq.c:1620
VALUE rb_get_coverages(void)
Definition: thread.c:5287
VALUE rb_str_cat2(VALUE, const char *)
Definition: string.c:2159
VALUE rb_ary_new(void)
Definition: array.c:495
static unsigned int find_line_no(const rb_iseq_t *iseq, size_t pos)
Definition: iseq.c:1122
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:1521
#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:881
#define NIL_P(v)
Definition: ruby.h:438
VALUE rb_iseq_method_name(VALUE self)
Definition: iseq.c:974
static VALUE iseq_eval(VALUE self)
Definition: iseq.c:809
#define CHECK_ARRAY(v)
Definition: iseq.c:472
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:790
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
if((ID)(DISPID) nameid!=nameid)
Definition: win32ole.c:770
rb_block_t * base_block
Definition: vm_core.h:555
#define MEMCPY(p1, p2, type, n)
Definition: ruby.h:1352
VALUE rb_iseq_klass(VALUE self)
Definition: iseq.c:966
#define rb_str_new2
Definition: intern.h:840
int err
Definition: win32.c:114
#define OBJ_FREEZE(x)
Definition: ruby.h:1186
#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:595
#define ALLOC(type)
Definition: ruby.h:1334
VALUE rb_str_resurrect(VALUE str)
Definition: string.c:1068
void Init_ISeq(void)
Definition: iseq.c:2277
#define PRIuVALUE
Definition: ruby.h:134
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2025
#define VM_CALL_SUPER
Definition: vm_core.h:748
static VALUE exception_type2symbol(VALUE type)
Definition: iseq.c:1632
#define INIT_SYMBOL(name)
Definition: iseq.c:1616
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:1725
#define hidden_obj_p(obj)
Definition: iseq.c:29
Definition: iseq.h:62
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:113
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:298
void ruby_xfree(void *x)
Definition: gc.c:6242
#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:5824
static VALUE iseq_data_to_ary(rb_iseq_t *iseq)
Definition: iseq.c:1657
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1728
#define RUBY_EVENT_LINE
Definition: ruby.h:1707
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:766
#define Qnil
Definition: ruby.h:427
VALUE rb_iseq_first_lineno(VALUE self)
Definition: iseq.c:958
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:2080
#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:52
unsigned long VALUE
Definition: ruby.h:88
VALUE rb_iseq_eval(VALUE iseqval)
Definition: vm.c:1607
#define rb_funcall2
Definition: ruby.h:1456
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:749
VALUE rb_iseq_path(VALUE self)
Definition: iseq.c:857
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:1596
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:2233
#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:2200
int arg_keyword_check
Definition: vm_core.h:284
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:1740
#define RUBY_FREE_ENTER(msg)
Definition: gc.h:55
const char * ruby_node_name(int node)
Definition: iseq.c:1603
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:796
#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:648
static VALUE iseq_to_a(VALUE self)
Definition: iseq.c:1076
#define RTEST(v)
Definition: ruby.h:437
static VALUE iseq_inspect(VALUE self)
Definition: iseq.c:820
#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:4792
#define VM_CALL_TAILCALL
Definition: vm_core.h:747
rb_iseq_t * rb_method_get_iseq(VALUE body)
Definition: proc.c:2156
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:1994
#define CHECK_STRING(v)
Definition: iseq.c:473
static const char * catch_type(int type)
Definition: iseq.c:1338
VALUE rb_parser_new(void)
Definition: ripper.c:17587
#define PRIdSIZE
Definition: ruby.h:176
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
Definition: file.c:3620
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:1922
const char * rb_id2name(ID id)
Definition: ripper.c:17227
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:471
VALUE iseq
Definition: iseq.h:64
NODE * rb_parser_compile_string_path(volatile VALUE vparser, VALUE f, VALUE s, int line)
Definition: ripper.c:12080
VALUE rb_iseq_base_label(VALUE self)
Definition: iseq.c:940
void rb_secure(int)
Definition: safe.c:88
#define CONST_ID(var, str)
Definition: ruby.h:1428
struct rb_call_info_struct rb_call_info_t
VALUE rb_iseq_label(VALUE self)
Definition: iseq.c:912
Definition: iseq.h:50
VALUE rb_ary_resurrect(VALUE ary)
Definition: array.c:1897
#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:1158
static int collect_trace(int line, rb_event_flag_t *events_ptr, void *ptr)
Definition: iseq.c:2173
#define NULL
Definition: _sdbm.c:103
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:924
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1488
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2298
VALUE rb_iseq_disasm(VALUE self)
Definition: iseq.c:1378
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
Definition: iseq.c:1953
NODE * rb_parser_compile_file_path(volatile VALUE vparser, VALUE fname, VALUE file, int start)
Definition: ripper.c:12121
#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:642
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:1213
static rb_iseq_t * iseq_check(VALUE val)
Definition: iseq.c:790
char ** argv
Definition: ruby.c:132
static VALUE iseq_s_compile_option_get(VALUE self)
Definition: iseq.c:784
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:917