1 /**********************************************************************
6 created at: 2006-07-11(Tue) 09:00:03 +0900
8 Copyright (C) 2006 Koichi Sasada
10 **********************************************************************/
12 #include "ruby/ruby.h"
14 /* #define MARK_FREE_DEBUG 1 */
20 #include "insns_info.inc"
25 compile_data_free(struct iseq_compile_data *compile_data)
28 struct iseq_compile_data_storage *cur, *next;
29 cur = compile_data->storage_head;
35 ruby_xfree(compile_data);
43 RUBY_FREE_ENTER("iseq");
48 /* It's possible that strings are freed
49 * GC_INFO("%s @ %s\n", RSTRING_PTR(iseq->name),
50 * RSTRING_PTR(iseq->filename));
52 if (iseq->iseq != iseq->iseq_encoded) {
53 RUBY_FREE_UNLESS_NULL(iseq->iseq_encoded);
56 RUBY_FREE_UNLESS_NULL(iseq->iseq);
57 RUBY_FREE_UNLESS_NULL(iseq->insn_info_table);
58 RUBY_FREE_UNLESS_NULL(iseq->local_table);
59 RUBY_FREE_UNLESS_NULL(iseq->catch_table);
60 RUBY_FREE_UNLESS_NULL(iseq->arg_opt_table);
61 compile_data_free(iseq->compile_data);
65 RUBY_FREE_LEAVE("iseq");
72 RUBY_MARK_ENTER("iseq");
76 RUBY_GC_INFO("%s @ %s\n", RSTRING_PTR(iseq->name), RSTRING_PTR(iseq->filename));
77 RUBY_MARK_UNLESS_NULL(iseq->mark_ary);
78 RUBY_MARK_UNLESS_NULL(iseq->name);
79 RUBY_MARK_UNLESS_NULL(iseq->filename);
80 RUBY_MARK_UNLESS_NULL((VALUE)iseq->cref_stack);
81 RUBY_MARK_UNLESS_NULL(iseq->klass);
82 RUBY_MARK_UNLESS_NULL(iseq->coverage);
83 /* RUBY_MARK_UNLESS_NULL((VALUE)iseq->node); */
84 /* RUBY_MARK_UNLESS_NULL(iseq->cached_special_block); */
85 RUBY_MARK_UNLESS_NULL(iseq->orig);
87 if (iseq->compile_data != 0) {
88 RUBY_MARK_UNLESS_NULL(iseq->compile_data->mark_ary);
89 RUBY_MARK_UNLESS_NULL(iseq->compile_data->err_info);
90 RUBY_MARK_UNLESS_NULL(iseq->compile_data->catch_table_ary);
93 RUBY_MARK_LEAVE("iseq");
97 iseq_alloc(VALUE klass)
102 obj = Data_Make_Struct(klass, rb_iseq_t, iseq_mark, iseq_free, iseq);
103 MEMZERO(iseq, rb_iseq_t, 1);
108 set_relation(rb_iseq_t *iseq, const VALUE parent)
110 const int type = iseq->type;
111 rb_thread_t *th = GET_THREAD();
113 /* set class nest stack */
114 if (type == ISEQ_TYPE_TOP) {
115 /* toplevel is private */
116 iseq->cref_stack = NEW_BLOCK(th->top_wrapper ? th->top_wrapper : rb_cObject);
117 iseq->cref_stack->nd_file = 0;
118 iseq->cref_stack->nd_visi = NOEX_PRIVATE;
120 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
121 iseq->cref_stack = NEW_BLOCK(0); /* place holder */
122 iseq->cref_stack->nd_file = 0;
124 else if (RTEST(parent)) {
126 GetISeqPtr(parent, piseq);
127 iseq->cref_stack = piseq->cref_stack;
130 if (type == ISEQ_TYPE_TOP ||
131 type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
132 iseq->local_iseq = iseq;
134 else if (RTEST(parent)) {
136 GetISeqPtr(parent, piseq);
137 iseq->local_iseq = piseq->local_iseq;
142 GetISeqPtr(parent, piseq);
143 iseq->parent_iseq = piseq;
148 prepare_iseq_build(rb_iseq_t *iseq,
149 VALUE name, VALUE filename,
150 VALUE parent, VALUE type, VALUE block_opt,
151 const rb_compile_option_t *option)
154 OBJ_FREEZE(filename);
157 iseq->filename = filename;
158 iseq->defined_method_id = 0;
159 iseq->mark_ary = rb_ary_new();
160 RBASIC(iseq->mark_ary)->klass = 0;
164 iseq->arg_block = -1;
168 * iseq->special_block_builder = GC_GUARDED_PTR_REF(block_opt);
169 * iseq->cached_special_block_builder = 0;
170 * iseq->cached_special_block = 0;
173 iseq->compile_data = ALLOC(struct iseq_compile_data);
174 MEMZERO(iseq->compile_data, struct iseq_compile_data, 1);
175 iseq->compile_data->mark_ary = rb_ary_new();
176 RBASIC(iseq->compile_data->mark_ary)->klass = 0;
178 iseq->compile_data->storage_head = iseq->compile_data->storage_current =
179 (struct iseq_compile_data_storage *)
180 ALLOC_N(char, INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE +
181 sizeof(struct iseq_compile_data_storage));
183 iseq->compile_data->catch_table_ary = rb_ary_new();
184 iseq->compile_data->storage_head->pos = 0;
185 iseq->compile_data->storage_head->next = 0;
186 iseq->compile_data->storage_head->size =
187 INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE;
188 iseq->compile_data->storage_head->buff =
189 (char *)(&iseq->compile_data->storage_head->buff + 1);
190 iseq->compile_data->option = option;
192 set_relation(iseq, parent);
194 iseq->coverage = Qfalse;
195 if (!GET_THREAD()->parse_in_eval) {
196 extern VALUE rb_get_coverages(void);
197 VALUE coverages = rb_get_coverages();
198 if (RTEST(coverages)) {
199 iseq->coverage = rb_hash_lookup(coverages, filename);
200 if (NIL_P(iseq->coverage)) iseq->coverage = Qfalse;
208 cleanup_iseq_build(rb_iseq_t *iseq)
210 struct iseq_compile_data *data = iseq->compile_data;
211 VALUE err = data->err_info;
212 iseq->compile_data = 0;
213 compile_data_free(data);
216 rb_funcall2(err, rb_intern("set_backtrace"), 1, &iseq->filename);
222 static rb_compile_option_t COMPILE_OPTION_DEFAULT = {
223 OPT_INLINE_CONST_CACHE, /* int inline_const_cache; */
224 OPT_PEEPHOLE_OPTIMIZATION, /* int peephole_optimization; */
225 OPT_TAILCALL_OPTIMIZATION, /* int tailcall_optimization */
226 OPT_SPECIALISED_INSTRUCTION, /* int specialized_instruction; */
227 OPT_OPERANDS_UNIFICATION, /* int operands_unification; */
228 OPT_INSTRUCTIONS_UNIFICATION, /* int instructions_unification; */
229 OPT_STACK_CACHING, /* int stack_caching; */
230 OPT_TRACE_INSTRUCTION, /* int trace_instruction */
232 static const rb_compile_option_t COMPILE_OPTION_FALSE = {0};
235 make_compile_option(rb_compile_option_t *option, VALUE opt)
238 *option = COMPILE_OPTION_DEFAULT;
240 else if (opt == Qfalse) {
241 *option = COMPILE_OPTION_FALSE;
243 else if (opt == Qtrue) {
244 memset(option, 1, sizeof(rb_compile_option_t));
246 else if (CLASS_OF(opt) == rb_cHash) {
247 *option = COMPILE_OPTION_DEFAULT;
249 #define SET_COMPILE_OPTION(o, h, mem) \
250 { VALUE flag = rb_hash_aref(h, ID2SYM(rb_intern(#mem))); \
251 if (flag == Qtrue) { o->mem = 1; } \
252 else if (flag == Qfalse) { o->mem = 0; } \
254 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
255 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
256 if (!NIL_P(num)) o->mem = NUM2INT(num); \
258 SET_COMPILE_OPTION(option, opt, inline_const_cache);
259 SET_COMPILE_OPTION(option, opt, peephole_optimization);
260 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
261 SET_COMPILE_OPTION(option, opt, specialized_instruction);
262 SET_COMPILE_OPTION(option, opt, operands_unification);
263 SET_COMPILE_OPTION(option, opt, instructions_unification);
264 SET_COMPILE_OPTION(option, opt, stack_caching);
265 SET_COMPILE_OPTION(option, opt, trace_instruction);
266 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
267 #undef SET_COMPILE_OPTION
268 #undef SET_COMPILE_OPTION_NUM
271 rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
276 make_compile_option_value(rb_compile_option_t *option)
278 VALUE opt = rb_hash_new();
279 #define SET_COMPILE_OPTION(o, h, mem) \
280 rb_hash_aset(h, ID2SYM(rb_intern(#mem)), o->mem ? Qtrue : Qfalse)
281 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
282 rb_hash_aset(h, ID2SYM(rb_intern(#mem)), INT2NUM(o->mem))
284 SET_COMPILE_OPTION(option, opt, inline_const_cache);
285 SET_COMPILE_OPTION(option, opt, peephole_optimization);
286 SET_COMPILE_OPTION(option, opt, tailcall_optimization);
287 SET_COMPILE_OPTION(option, opt, specialized_instruction);
288 SET_COMPILE_OPTION(option, opt, operands_unification);
289 SET_COMPILE_OPTION(option, opt, instructions_unification);
290 SET_COMPILE_OPTION(option, opt, stack_caching);
291 SET_COMPILE_OPTION_NUM(option, opt, debug_level);
293 #undef SET_COMPILE_OPTION
294 #undef SET_COMPILE_OPTION_NUM
299 rb_iseq_new(NODE *node, VALUE name, VALUE filename,
300 VALUE parent, VALUE type)
302 return rb_iseq_new_with_opt(node, name, filename, parent, type,
303 &COMPILE_OPTION_DEFAULT);
307 rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE parent)
309 return rb_iseq_new_with_opt(node, name, filename, parent, ISEQ_TYPE_TOP,
310 &COMPILE_OPTION_DEFAULT);
314 rb_iseq_new_main(NODE *node, VALUE filename)
316 rb_thread_t *th = GET_THREAD();
317 VALUE parent = th->base_block->iseq->self;
318 return rb_iseq_new_with_opt(node, rb_str_new2("<main>"), filename,
319 parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
323 rb_iseq_new_with_bopt_and_opt(NODE *node, VALUE name, VALUE filename,
324 VALUE parent, VALUE type, VALUE bopt,
325 const rb_compile_option_t *option)
328 VALUE self = iseq_alloc(rb_cISeq);
330 GetISeqPtr(self, iseq);
333 prepare_iseq_build(iseq, name, filename, parent, type, bopt, option);
334 ruby_iseq_compile(self, node);
335 cleanup_iseq_build(iseq);
340 rb_iseq_new_with_opt(NODE *node, VALUE name, VALUE filename,
341 VALUE parent, VALUE type,
342 const rb_compile_option_t *option)
344 return rb_iseq_new_with_bopt_and_opt(node, name, filename, parent, type,
349 rb_iseq_new_with_bopt(NODE *node, VALUE name, VALUE filename,
350 VALUE parent, VALUE type, VALUE bopt)
352 return rb_iseq_new_with_bopt_and_opt(node, name, filename, parent, type,
353 bopt, &COMPILE_OPTION_DEFAULT);
356 #define CHECK_ARRAY(v) rb_convert_type(v, T_ARRAY, "Array", "to_ary")
357 #define CHECK_STRING(v) rb_convert_type(v, T_STRING, "String", "to_str")
358 #define CHECK_SYMBOL(v) rb_convert_type(v, T_SYMBOL, "Symbol", "to_sym")
359 static inline VALUE CHECK_INTEGER(VALUE v) {NUM2LONG(v); return v;}
361 iseq_load(VALUE self, VALUE data, VALUE parent, VALUE opt)
363 VALUE iseqval = iseq_alloc(self);
365 VALUE magic, version1, version2, format_type, misc;
366 VALUE name, filename;
367 VALUE type, body, locals, args, exception;
370 struct st_table *type_map = 0;
372 rb_compile_option_t option;
375 /* [magic, major_version, minor_version, format_type, misc,
377 * type, locals, args, exception_table, body]
380 data = CHECK_ARRAY(data);
382 magic = CHECK_STRING(rb_ary_entry(data, i++));
383 version1 = CHECK_INTEGER(rb_ary_entry(data, i++));
384 version2 = CHECK_INTEGER(rb_ary_entry(data, i++));
385 format_type = CHECK_INTEGER(rb_ary_entry(data, i++));
386 misc = rb_ary_entry(data, i++); /* TODO */
388 name = CHECK_STRING(rb_ary_entry(data, i++));
389 filename = CHECK_STRING(rb_ary_entry(data, i++));
391 type = CHECK_SYMBOL(rb_ary_entry(data, i++));
392 locals = CHECK_ARRAY(rb_ary_entry(data, i++));
394 args = rb_ary_entry(data, i++);
395 if (FIXNUM_P(args) || (args = CHECK_ARRAY(args))) {
399 exception = CHECK_ARRAY(rb_ary_entry(data, i++));
400 body = CHECK_ARRAY(rb_ary_entry(data, i++));
402 GetISeqPtr(iseqval, iseq);
403 iseq->self = iseqval;
406 type_map = st_init_numtable();
407 st_insert(type_map, ID2SYM(rb_intern("top")), ISEQ_TYPE_TOP);
408 st_insert(type_map, ID2SYM(rb_intern("method")), ISEQ_TYPE_METHOD);
409 st_insert(type_map, ID2SYM(rb_intern("block")), ISEQ_TYPE_BLOCK);
410 st_insert(type_map, ID2SYM(rb_intern("class")), ISEQ_TYPE_CLASS);
411 st_insert(type_map, ID2SYM(rb_intern("rescue")), ISEQ_TYPE_RESCUE);
412 st_insert(type_map, ID2SYM(rb_intern("ensure")), ISEQ_TYPE_ENSURE);
413 st_insert(type_map, ID2SYM(rb_intern("eval")), ISEQ_TYPE_EVAL);
414 st_insert(type_map, ID2SYM(rb_intern("main")), ISEQ_TYPE_MAIN);
415 st_insert(type_map, ID2SYM(rb_intern("defined_guard")), ISEQ_TYPE_DEFINED_GUARD);
418 if (st_lookup(type_map, type, &iseq_type) == 0) {
419 const char *typename = rb_id2name(type);
421 rb_raise(rb_eTypeError, "unsupport type: :%s", typename);
423 rb_raise(rb_eTypeError, "unsupport type: %p", (void *)type);
426 if (parent == Qnil) {
430 make_compile_option(&option, opt);
431 prepare_iseq_build(iseq, name, filename,
432 parent, iseq_type, 0, &option);
434 ruby_iseq_build_from_ary(iseq, locals, args, exception, body);
436 cleanup_iseq_build(iseq);
441 iseq_s_load(int argc, VALUE *argv, VALUE self)
443 VALUE data, opt=Qnil;
444 rb_scan_args(argc, argv, "11", &data, &opt);
446 return iseq_load(self, data, 0, opt);
450 ruby_iseq_load(VALUE data, VALUE parent, VALUE opt)
452 return iseq_load(rb_cISeq, data, parent, opt);
456 compile_string(VALUE str, VALUE file, VALUE line)
458 VALUE parser = rb_parser_new();
459 NODE *node = rb_parser_compile_string(parser, StringValueCStr(file),
463 rb_exc_raise(GET_THREAD()->errinfo); /* TODO: check err */
469 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE line, VALUE opt)
471 rb_compile_option_t option;
472 NODE *node = compile_string(StringValue(src), file, line);
473 rb_thread_t *th = GET_THREAD();
474 make_compile_option(&option, opt);
476 if (th->base_block && th->base_block->iseq) {
477 return rb_iseq_new_with_opt(node, th->base_block->iseq->name,
478 file, th->base_block->iseq->self,
479 ISEQ_TYPE_EVAL, &option);
482 return rb_iseq_new_with_opt(node, rb_str_new2("<compiled>"), file, Qfalse,
483 ISEQ_TYPE_TOP, &option);
488 rb_iseq_compile(VALUE src, VALUE file, VALUE line)
490 return rb_iseq_compile_with_option(src, file, line, Qnil);
494 iseq_s_compile(int argc, VALUE *argv, VALUE self)
496 VALUE src, file = Qnil, line = INT2FIX(1), opt = Qnil;
500 rb_scan_args(argc, argv, "13", &src, &file, &line, &opt);
501 file = file == Qnil ? rb_str_new2("<compiled>") : file;
502 line = line == Qnil ? INT2FIX(1) : line;
504 return rb_iseq_compile_with_option(src, file, line, opt);
508 iseq_s_compile_file(int argc, VALUE *argv, VALUE self)
510 VALUE file, line = INT2FIX(1), opt = Qnil;
515 rb_compile_option_t option;
518 rb_scan_args(argc, argv, "11", &file, &opt);
520 fname = StringValueCStr(file);
522 f = rb_file_open_str(file, "r");
524 parser = rb_parser_new();
525 node = rb_parser_compile_file(parser, fname, f, NUM2INT(line));
526 make_compile_option(&option, opt);
527 return rb_iseq_new_with_opt(node, rb_str_new2("<main>"), file, Qfalse,
528 ISEQ_TYPE_TOP, &option);
532 iseq_s_compile_option_set(VALUE self, VALUE opt)
534 rb_compile_option_t option;
536 make_compile_option(&option, opt);
537 COMPILE_OPTION_DEFAULT = option;
542 iseq_s_compile_option_get(VALUE self)
544 return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
548 iseq_check(VALUE val)
551 GetISeqPtr(val, iseq);
553 rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
559 iseq_eval(VALUE self)
562 return rb_iseq_eval(self);
566 iseq_inspect(VALUE self)
569 GetISeqPtr(self, iseq);
571 return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self));
574 return rb_sprintf("<%s:%s@%s>",
575 rb_obj_classname(self),
576 RSTRING_PTR(iseq->name), RSTRING_PTR(iseq->filename));
580 VALUE iseq_data_to_ary(rb_iseq_t *iseq);
583 iseq_to_a(VALUE self)
585 rb_iseq_t *iseq = iseq_check(self);
587 return iseq_data_to_ary(iseq);
591 rb_iseq_first_lineno(rb_iseq_t *iseq)
593 return iseq->insn_info_table[0].line_no;
596 /* TODO: search algorithm is brute force.
597 this should be binary search or so. */
599 static struct iseq_insn_info_entry *
600 get_insn_info(const rb_iseq_t *iseq, const unsigned long pos)
602 unsigned long i, size = iseq->insn_info_size;
603 struct iseq_insn_info_entry *table = iseq->insn_info_table;
605 for (i = 0; i < size; i++) {
606 if (table[i].position == pos) {
614 static unsigned short
615 find_line_no(rb_iseq_t *iseq, unsigned long pos)
617 struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
619 return entry->line_no;
626 static unsigned short
627 find_prev_line_no(rb_iseq_t *iseqdat, unsigned long pos)
629 unsigned long i, size = iseqdat->insn_info_size;
630 struct iseq_insn_info_entry *iiary = iseqdat->insn_info_table;
632 for (i = 0; i < size; i++) {
633 if (iiary[i].position == pos) {
635 return iiary[i - 1].line_no;
647 insn_operand_intern(rb_iseq_t *iseq,
648 int insn, int op_no, VALUE op,
649 int len, int pos, VALUE *pnop, VALUE child)
651 const char *types = insn_op_types(insn);
652 char type = types[op_no];
656 case TS_OFFSET: /* LONG */
657 ret = rb_sprintf("%ld", pos + len + op);
660 case TS_NUM: /* ULONG */
661 ret = rb_sprintf("%lu", op);
666 rb_iseq_t *ip = iseq->local_iseq;
667 int lidx = ip->local_size - op;
668 const char *name = rb_id2name(ip->local_table[lidx]);
671 ret = rb_str_new2(name);
674 ret = rb_str_new2("*");
679 if (insn == BIN(getdynamic) || insn == BIN(setdynamic)) {
680 rb_iseq_t *ip = iseq;
681 int level = *pnop, i;
683 for (i = 0; i < level; i++) {
684 ip = ip->parent_iseq;
686 name = rb_id2name(ip->local_table[ip->local_size - op]);
691 ret = rb_str_new2(name);
694 ret = rb_inspect(INT2FIX(op));
698 case TS_ID: /* ID (symbol) */
701 case TS_VALUE: /* VALUE */
702 ret = rb_inspect(op);
703 if (CLASS_OF(op) == rb_cISeq) {
704 rb_ary_push(child, op);
708 case TS_ISEQ: /* iseq */
710 rb_iseq_t *iseq = (rb_iseq_t *)op;
714 rb_ary_push(child, iseq->self);
718 ret = rb_str_new2("nil");
724 struct global_entry *entry = (struct global_entry *)op;
725 ret = rb_str_dup(rb_id2str(entry->id));
730 ret = rb_str_new2("<ic>");
734 ret = rb_str_new2("<cdhash>");
738 ret = rb_str_new2("<funcptr>");
742 rb_bug("ruby_iseq_disasm: unknown operand type: %c", type);
748 * Disassemble a instruction
749 * Iseq -> Iseq inspect object
752 ruby_iseq_disasm_insn(VALUE ret, VALUE *iseq, int pos,
753 rb_iseq_t *iseqdat, VALUE child)
755 int insn = iseq[pos];
756 int len = insn_len(insn);
758 const char *types = insn_op_types(insn);
759 VALUE str = rb_str_new(0, 0);
760 const char *insn_name_buff;
762 insn_name_buff = insn_name(insn);
764 rb_str_catf(str, "%04d %-16s ", pos, insn_name_buff);
767 rb_str_catf(str, "%04d %-16.*s ", pos,
768 (int)strcspn(insn_name_buff, "_"), insn_name_buff);
771 for (j = 0; types[j]; j++) {
772 const char *types = insn_op_types(insn);
773 VALUE opstr = insn_operand_intern(iseqdat, insn, j, iseq[pos + j + 1],
774 len, pos, &iseq[pos + j + 2],
776 rb_str_concat(str, opstr);
779 rb_str_cat2(str, ", ");
784 int line_no = find_line_no(iseqdat, pos);
785 int prev = find_prev_line_no(iseqdat, pos);
786 if (line_no && line_no != prev) {
787 long slen = RSTRING_LEN(str);
788 slen = (slen > 70) ? 0 : (70 - slen);
789 str = rb_str_catf(str, "%*s(%4d)", (int)slen, "", line_no);
794 struct iseq_insn_info_entry *entry = get_insn_info(iseqdat, pos);
795 long slen = RSTRING_LEN(str);
796 slen = (slen > 60) ? 0 : (60 - slen);
797 str = rb_str_catf(str, "%*s(line: %d, sp: %d)",
798 (int)slen, "", entry->line_no, entry->sp);
802 rb_str_cat2(str, "\n");
803 rb_str_concat(ret, str);
806 printf("%s\n", RSTRING_PTR(str));
815 case CATCH_TYPE_RESCUE:
817 case CATCH_TYPE_ENSURE:
819 case CATCH_TYPE_RETRY:
821 case CATCH_TYPE_BREAK:
823 case CATCH_TYPE_REDO:
825 case CATCH_TYPE_NEXT:
828 rb_bug("unknown catch type (%d)", type);
834 ruby_iseq_disasm(VALUE self)
836 rb_iseq_t *iseqdat = iseq_check(self);
838 VALUE str = rb_str_new(0, 0);
839 VALUE child = rb_ary_new();
843 enum {header_minlen = 72};
847 iseq = iseqdat->iseq;
848 size = iseqdat->iseq_size;
850 rb_str_cat2(str, "== disasm: ");
852 rb_str_concat(str, iseq_inspect(iseqdat->self));
853 if ((i = RSTRING_LEN(str)) < header_minlen) {
854 rb_str_resize(str, header_minlen);
855 memset(RSTRING_PTR(str) + i, '=', header_minlen - i);
857 rb_str_cat2(str, "\n");
859 /* show catch table information */
860 if (iseqdat->catch_table_size != 0) {
861 rb_str_cat2(str, "== catch table\n");
863 for (i = 0; i < iseqdat->catch_table_size; i++) {
864 struct iseq_catch_table_entry *entry = &iseqdat->catch_table[i];
866 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
867 catch_type((int)entry->type), (int)entry->start,
868 (int)entry->end, (int)entry->sp, (int)entry->cont);
870 rb_str_concat(str, ruby_iseq_disasm(entry->iseq));
873 if (iseqdat->catch_table_size != 0) {
874 rb_str_cat2(str, "|-------------------------------------"
875 "-----------------------------------\n");
878 /* show local table information */
879 tbl = iseqdat->local_table;
883 "local table (size: %d, argc: %d "
884 "[opts: %d, rest: %d, post: %d, block: %d] s%d)\n",
885 iseqdat->local_size, iseqdat->argc,
886 iseqdat->arg_opts, iseqdat->arg_rest,
887 iseqdat->arg_post_len, iseqdat->arg_block,
888 iseqdat->arg_simple);
890 for (i = 0; i < iseqdat->local_table_size; i++) {
891 const char *name = rb_id2name(tbl[i]);
893 char argi[0x100] = "";
894 char opti[0x100] = "";
896 if (iseqdat->arg_opts) {
897 int argc = iseqdat->argc;
898 int opts = iseqdat->arg_opts;
899 if (i >= argc && i < argc + opts - 1) {
900 snprintf(opti, sizeof(opti), "Opt=%ld",
901 iseqdat->arg_opt_table[i - argc]);
905 snprintf(argi, sizeof(argi), "%s%s%s%s%s", /* arg, opts, rest, post block */
906 iseqdat->argc > i ? "Arg" : "",
908 iseqdat->arg_rest == i ? "Rest" : "",
909 (iseqdat->arg_post_start <= i &&
910 i < iseqdat->arg_post_start + iseqdat->arg_post_len) ? "Post" : "",
911 iseqdat->arg_block == i ? "Block" : "");
913 snprintf(info, sizeof(info), "%s%s%s%s", name ? name : "?",
914 *argi ? "<" : "", argi, *argi ? ">" : "");
916 rb_str_catf(str, "[%2d] %-11s", iseqdat->local_size - i, info);
918 rb_str_cat2(str, "\n");
922 for (i = 0; i < size;) {
923 i += ruby_iseq_disasm_insn(str, iseq, i, iseqdat, child);
926 for (i = 0; i < RARRAY_LEN(child); i++) {
927 VALUE isv = rb_ary_entry(child, i);
928 rb_str_concat(str, ruby_iseq_disasm(isv));
935 iseq_s_disasm(VALUE klass, VALUE body)
937 extern NODE *rb_method_body(VALUE body);
943 if ((node = rb_method_body(body)) != 0) {
944 if (nd_type(node) == RUBY_VM_METHOD_NODE) {
945 VALUE iseqval = (VALUE)node->nd_body;
946 ret = ruby_iseq_disasm(iseqval);
954 ruby_node_name(int node)
957 #include "node_name.inc"
959 rb_bug("unknown node (%d)", node);
964 #define DECL_SYMBOL(name) \
965 static VALUE sym_##name
967 #define INIT_SYMBOL(name) \
968 sym_##name = ID2SYM(rb_intern(#name))
971 register_label(struct st_table *table, int idx)
974 char buff[8 + (sizeof(idx) * CHAR_BIT * 32 / 100)];
976 snprintf(buff, sizeof(buff), "label_%u", idx);
977 sym = ID2SYM(rb_intern(buff));
978 st_insert(table, idx, sym);
983 exception_type2symbol(VALUE type)
987 case CATCH_TYPE_RESCUE: CONST_ID(id, "rescue"); break;
988 case CATCH_TYPE_ENSURE: CONST_ID(id, "ensure"); break;
989 case CATCH_TYPE_RETRY: CONST_ID(id, "retry"); break;
990 case CATCH_TYPE_BREAK: CONST_ID(id, "break"); break;
991 case CATCH_TYPE_REDO: CONST_ID(id, "redo"); break;
992 case CATCH_TYPE_NEXT: CONST_ID(id, "next"); break;
1000 cdhash_each(VALUE key, VALUE value, VALUE ary)
1002 rb_ary_push(ary, key);
1003 rb_ary_push(ary, value);
1008 iseq_data_to_ary(rb_iseq_t *iseq)
1010 int i, pos, line = 0;
1013 VALUE val = rb_ary_new();
1014 VALUE type; /* Symbol */
1015 VALUE locals = rb_ary_new();
1016 VALUE args = rb_ary_new();
1017 VALUE body = rb_ary_new(); /* [[:insn1, ...], ...] */
1019 VALUE exception = rb_ary_new(); /* [[....]] */
1020 VALUE misc = rb_hash_new();
1022 static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1023 struct st_table *labels_table = st_init_numtable();
1026 DECL_SYMBOL(method);
1029 DECL_SYMBOL(rescue);
1030 DECL_SYMBOL(ensure);
1033 DECL_SYMBOL(defined_guard);
1037 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1038 insn_syms[i] = ID2SYM(rb_intern(insn_name(i)));
1041 INIT_SYMBOL(method);
1044 INIT_SYMBOL(rescue);
1045 INIT_SYMBOL(ensure);
1048 INIT_SYMBOL(defined_guard);
1052 switch(iseq->type) {
1053 case ISEQ_TYPE_TOP: type = sym_top; break;
1054 case ISEQ_TYPE_METHOD: type = sym_method; break;
1055 case ISEQ_TYPE_BLOCK: type = sym_block; break;
1056 case ISEQ_TYPE_CLASS: type = sym_class; break;
1057 case ISEQ_TYPE_RESCUE: type = sym_rescue; break;
1058 case ISEQ_TYPE_ENSURE: type = sym_ensure; break;
1059 case ISEQ_TYPE_EVAL: type = sym_eval; break;
1060 case ISEQ_TYPE_MAIN: type = sym_main; break;
1061 case ISEQ_TYPE_DEFINED_GUARD: type = sym_defined_guard; break;
1062 default: rb_bug("unsupported iseq type");
1066 for (i=0; i<iseq->local_table_size; i++) {
1067 ID lid = iseq->local_table[i];
1069 if (rb_id2str(lid)) rb_ary_push(locals, ID2SYM(lid));
1072 rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
1080 * [label1, label2, ...] # opts
1088 VALUE arg_opt_labels = rb_ary_new();
1091 for (j=0; j<iseq->arg_opts; j++) {
1092 rb_ary_push(arg_opt_labels,
1093 register_label(labels_table, iseq->arg_opt_table[j]));
1097 if (iseq->arg_simple == 1) {
1098 args = INT2FIX(iseq->argc);
1101 rb_ary_push(args, INT2FIX(iseq->argc));
1102 rb_ary_push(args, arg_opt_labels);
1103 rb_ary_push(args, INT2FIX(iseq->arg_post_len));
1104 rb_ary_push(args, INT2FIX(iseq->arg_post_start));
1105 rb_ary_push(args, INT2FIX(iseq->arg_rest));
1106 rb_ary_push(args, INT2FIX(iseq->arg_block));
1107 rb_ary_push(args, INT2FIX(iseq->arg_simple));
1112 for (seq = iseq->iseq; seq < iseq->iseq + iseq->iseq_size; ) {
1113 VALUE insn = *seq++;
1114 int j, len = insn_len(insn);
1115 VALUE *nseq = seq + len - 1;
1116 VALUE ary = rb_ary_new2(len);
1118 rb_ary_push(ary, insn_syms[insn]);
1119 for (j=0; j<len-1; j++, seq++) {
1120 switch (insn_op_type(insn, j)) {
1122 unsigned int idx = nseq - iseq->iseq + *seq;
1123 rb_ary_push(ary, register_label(labels_table, idx));
1129 rb_ary_push(ary, INT2FIX(*seq));
1132 rb_ary_push(ary, *seq);
1136 rb_iseq_t *iseq = (rb_iseq_t *)*seq;
1138 VALUE val = iseq_data_to_ary(iseq);
1139 rb_ary_push(ary, val);
1142 rb_ary_push(ary, Qnil);
1148 struct global_entry *entry = (struct global_entry *)*seq;
1149 rb_ary_push(ary, ID2SYM(entry->id));
1153 rb_ary_push(ary, Qnil);
1156 rb_ary_push(ary, ID2SYM(*seq));
1161 VALUE val = rb_ary_new();
1164 rb_hash_foreach(hash, cdhash_each, val);
1166 for (i=0; i<RARRAY_LEN(val); i+=2) {
1167 VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
1168 unsigned int idx = nseq - iseq->iseq + pos;
1170 rb_ary_store(val, i+1,
1171 register_label(labels_table, idx));
1173 rb_ary_push(ary, val);
1177 rb_bug("unknown operand: %c", insn_op_type(insn, j));
1180 rb_ary_push(body, ary);
1186 for (i=0; i<iseq->catch_table_size; i++) {
1187 VALUE ary = rb_ary_new();
1188 struct iseq_catch_table_entry *entry = &iseq->catch_table[i];
1189 rb_ary_push(ary, exception_type2symbol(entry->type));
1192 GetISeqPtr(entry->iseq, eiseq);
1193 rb_ary_push(ary, iseq_data_to_ary(eiseq));
1196 rb_ary_push(ary, Qnil);
1198 rb_ary_push(ary, register_label(labels_table, entry->start));
1199 rb_ary_push(ary, register_label(labels_table, entry->end));
1200 rb_ary_push(ary, register_label(labels_table, entry->cont));
1201 rb_ary_push(ary, INT2FIX(entry->sp));
1202 rb_ary_push(exception, ary);
1205 /* make body with labels and insert line number */
1206 body = rb_ary_new();
1208 for (i=0, pos=0; i<RARRAY_LEN(nbody); i++) {
1209 VALUE ary = RARRAY_PTR(nbody)[i];
1212 if (st_lookup(labels_table, pos, &label)) {
1213 rb_ary_push(body, label);
1216 if (iseq->insn_info_table[i].line_no != line) {
1217 line = iseq->insn_info_table[i].line_no;
1218 rb_ary_push(body, INT2FIX(line));
1221 rb_ary_push(body, ary);
1222 pos += RARRAY_LEN(ary);
1225 st_free_table(labels_table);
1227 rb_hash_aset(misc, ID2SYM(rb_intern("arg_size")), INT2FIX(iseq->arg_size));
1228 rb_hash_aset(misc, ID2SYM(rb_intern("local_size")), INT2FIX(iseq->local_size));
1229 rb_hash_aset(misc, ID2SYM(rb_intern("stack_max")), INT2FIX(iseq->stack_max));
1232 * [:magic, :major_version, :minor_version, :format_type, :misc,
1233 * :name, :filename, :type, :locals, :args,
1234 * :catch_table, :bytecode]
1236 rb_ary_push(val, rb_str_new2("YARVInstructionSequence/SimpleDataFormat"));
1237 rb_ary_push(val, INT2FIX(1));
1238 rb_ary_push(val, INT2FIX(1));
1239 rb_ary_push(val, INT2FIX(1));
1240 rb_ary_push(val, misc);
1241 rb_ary_push(val, iseq->name);
1242 rb_ary_push(val, iseq->filename);
1243 rb_ary_push(val, type);
1244 rb_ary_push(val, locals);
1245 rb_ary_push(val, args);
1246 rb_ary_push(val, exception);
1247 rb_ary_push(val, body);
1252 ruby_insn_make_insn_table(void)
1254 struct st_table *table;
1256 table = st_init_numtable();
1258 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1259 st_insert(table, ID2SYM(rb_intern(insn_name(i))), i);
1266 rb_iseq_clone(VALUE iseqval, VALUE newcbase)
1268 VALUE newiseq = iseq_alloc(rb_cISeq);
1269 rb_iseq_t *iseq0, *iseq1;
1271 GetISeqPtr(iseqval, iseq0);
1272 GetISeqPtr(newiseq, iseq1);
1275 iseq1->self = newiseq;
1277 iseq1->orig = iseqval;
1280 iseq1->cref_stack = NEW_BLOCK(newcbase);
1281 if (iseq0->cref_stack->nd_next) {
1282 iseq1->cref_stack->nd_next = iseq0->cref_stack->nd_next;
1292 rb_iseq_build_for_ruby2cext(
1293 const rb_iseq_t *iseq_template,
1294 const rb_insn_func_t *func,
1295 const struct iseq_insn_info_entry *insn_info_table,
1296 const char **local_table,
1297 const VALUE *arg_opt_table,
1298 const struct iseq_catch_table_entry *catch_table,
1300 const char *filename)
1303 VALUE iseqval = iseq_alloc(rb_cISeq);
1305 GetISeqPtr(iseqval, iseq);
1308 *iseq = *iseq_template;
1309 iseq->name = rb_str_new2(name);
1310 iseq->filename = rb_str_new2(filename);
1311 iseq->mark_ary = rb_ary_new();
1312 iseq->self = iseqval;
1314 iseq->iseq = ALLOC_N(VALUE, iseq->iseq_size);
1316 for (i=0; i<iseq->iseq_size; i+=2) {
1317 iseq->iseq[i] = BIN(opt_call_c_function);
1318 iseq->iseq[i+1] = (VALUE)func;
1321 ruby_iseq_translate_threaded_code(iseq);
1323 #define ALLOC_AND_COPY(dst, src, type, size) do { \
1325 (dst) = ALLOC_N(type, (size)); \
1326 MEMCPY((dst), (src), type, (size)); \
1330 ALLOC_AND_COPY(iseq->insn_info_table, insn_info_table,
1331 struct iseq_insn_info_entry, iseq->insn_info_size);
1333 ALLOC_AND_COPY(iseq->catch_table, catch_table,
1334 struct iseq_catch_table_entry, iseq->catch_table_size);
1336 ALLOC_AND_COPY(iseq->arg_opt_table, arg_opt_table,
1337 VALUE, iseq->arg_opts);
1339 set_relation(iseq, 0);
1347 /* declare ::VM::InstructionSequence */
1348 rb_cISeq = rb_define_class_under(rb_cRubyVM, "InstructionSequence", rb_cObject);
1349 rb_define_alloc_func(rb_cISeq, iseq_alloc);
1350 rb_define_method(rb_cISeq, "inspect", iseq_inspect, 0);
1351 rb_define_method(rb_cISeq, "disasm", ruby_iseq_disasm, 0);
1352 rb_define_method(rb_cISeq, "disassemble", ruby_iseq_disasm, 0);
1353 rb_define_method(rb_cISeq, "to_a", iseq_to_a, 0);
1354 rb_define_method(rb_cISeq, "eval", iseq_eval, 0);
1356 /* disable this feature because there is no verifier. */
1357 /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1); */
1360 rb_define_singleton_method(rb_cISeq, "compile", iseq_s_compile, -1);
1361 rb_define_singleton_method(rb_cISeq, "new", iseq_s_compile, -1);
1362 rb_define_singleton_method(rb_cISeq, "compile_file", iseq_s_compile_file, -1);
1363 rb_define_singleton_method(rb_cISeq, "compile_option", iseq_s_compile_option_get, 0);
1364 rb_define_singleton_method(rb_cISeq, "compile_option=", iseq_s_compile_option_set, 1);
1365 rb_define_singleton_method(rb_cISeq, "disasm", iseq_s_disasm, 1);
1366 rb_define_singleton_method(rb_cISeq, "disassemble", iseq_s_disasm, 1);