OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / iseq.c
1 /**********************************************************************
2
3   iseq.c -
4
5   $Author: yugui $
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
14 /* #define MARK_FREE_DEBUG 1 */
15 #include "gc.h"
16 #include "vm_core.h"
17 #include "iseq.h"
18
19 #include "insns.inc"
20 #include "insns_info.inc"
21
22 VALUE rb_cISeq;
23
24 static void
25 compile_data_free(struct iseq_compile_data *compile_data)
26 {
27     if (compile_data) {
28         struct iseq_compile_data_storage *cur, *next;
29         cur = compile_data->storage_head;
30         while (cur) {
31             next = cur->next;
32             ruby_xfree(cur);
33             cur = next;
34         }
35         ruby_xfree(compile_data);
36     }
37 }
38
39 static void
40 iseq_free(void *ptr)
41 {
42     rb_iseq_t *iseq;
43     RUBY_FREE_ENTER("iseq");
44
45     if (ptr) {
46         iseq = ptr;
47         if (!iseq->orig) {
48             /* It's possible that strings are freed
49              * GC_INFO("%s @ %s\n", RSTRING_PTR(iseq->name),
50              *                      RSTRING_PTR(iseq->filename));
51              */
52             if (iseq->iseq != iseq->iseq_encoded) {
53                 RUBY_FREE_UNLESS_NULL(iseq->iseq_encoded);
54             }
55
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);
62         }
63         ruby_xfree(ptr);
64     }
65     RUBY_FREE_LEAVE("iseq");
66 }
67
68 static void
69 iseq_mark(void *ptr)
70 {
71     rb_iseq_t *iseq;
72     RUBY_MARK_ENTER("iseq");
73
74     if (ptr) {
75         iseq = ptr;
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);
86
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);
91         }
92     }
93     RUBY_MARK_LEAVE("iseq");
94 }
95
96 static VALUE
97 iseq_alloc(VALUE klass)
98 {
99     VALUE volatile obj;
100     rb_iseq_t *iseq;
101
102     obj = Data_Make_Struct(klass, rb_iseq_t, iseq_mark, iseq_free, iseq);
103     MEMZERO(iseq, rb_iseq_t, 1);
104     return obj;
105 }
106
107 static void
108 set_relation(rb_iseq_t *iseq, const VALUE parent)
109 {
110     const int type = iseq->type;
111     rb_thread_t *th = GET_THREAD();
112
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;
119     }
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;
123     }
124     else if (RTEST(parent)) {
125         rb_iseq_t *piseq;
126         GetISeqPtr(parent, piseq);
127         iseq->cref_stack = piseq->cref_stack;
128     }
129
130     if (type == ISEQ_TYPE_TOP ||
131         type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
132         iseq->local_iseq = iseq;
133     }
134     else if (RTEST(parent)) {
135         rb_iseq_t *piseq;
136         GetISeqPtr(parent, piseq);
137         iseq->local_iseq = piseq->local_iseq;
138     }
139
140     if (RTEST(parent)) {
141         rb_iseq_t *piseq;
142         GetISeqPtr(parent, piseq);
143         iseq->parent_iseq = piseq;
144     }
145 }
146
147 static VALUE
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)
152 {
153     OBJ_FREEZE(name);
154     OBJ_FREEZE(filename);
155
156     iseq->name = name;
157     iseq->filename = filename;
158     iseq->defined_method_id = 0;
159     iseq->mark_ary = rb_ary_new();
160     RBASIC(iseq->mark_ary)->klass = 0;
161
162     iseq->type = type;
163     iseq->arg_rest = -1;
164     iseq->arg_block = -1;
165     iseq->klass = 0;
166
167     /*
168      * iseq->special_block_builder = GC_GUARDED_PTR_REF(block_opt);
169      * iseq->cached_special_block_builder = 0;
170      * iseq->cached_special_block = 0;
171      */
172
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;
177
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));
182
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;
191
192     set_relation(iseq, parent);
193
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;
201         }
202     }
203
204     return Qtrue;
205 }
206
207 static VALUE
208 cleanup_iseq_build(rb_iseq_t *iseq)
209 {
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);
214
215     if (RTEST(err)) {
216         rb_funcall2(err, rb_intern("set_backtrace"), 1, &iseq->filename);
217         rb_exc_raise(err);
218     }
219     return Qtrue;
220 }
221
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 */
231 };
232 static const rb_compile_option_t COMPILE_OPTION_FALSE = {0};
233
234 static void
235 make_compile_option(rb_compile_option_t *option, VALUE opt)
236 {
237     if (opt == Qnil) {
238         *option = COMPILE_OPTION_DEFAULT;
239     }
240     else if (opt == Qfalse) {
241         *option = COMPILE_OPTION_FALSE;
242     }
243     else if (opt == Qtrue) {
244         memset(option, 1, sizeof(rb_compile_option_t));
245     }
246     else if (CLASS_OF(opt) == rb_cHash) {
247         *option = COMPILE_OPTION_DEFAULT;
248
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; } \
253   }
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); \
257   }
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
269     }
270     else {
271         rb_raise(rb_eTypeError, "Compile option must be Hash/true/false/nil");
272     }
273 }
274
275 static VALUE
276 make_compile_option_value(rb_compile_option_t *option)
277 {
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))
283     {
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);
292     }
293 #undef SET_COMPILE_OPTION
294 #undef SET_COMPILE_OPTION_NUM
295     return opt;
296 }
297
298 VALUE
299 rb_iseq_new(NODE *node, VALUE name, VALUE filename,
300               VALUE parent, VALUE type)
301 {
302     return rb_iseq_new_with_opt(node, name, filename, parent, type,
303                                 &COMPILE_OPTION_DEFAULT);
304 }
305
306 VALUE
307 rb_iseq_new_top(NODE *node, VALUE name, VALUE filename, VALUE parent)
308 {
309     return rb_iseq_new_with_opt(node, name, filename, parent, ISEQ_TYPE_TOP,
310                                 &COMPILE_OPTION_DEFAULT);
311 }
312
313 VALUE
314 rb_iseq_new_main(NODE *node, VALUE filename)
315 {
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);
320 }
321
322 static VALUE
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)
326 {
327     rb_iseq_t *iseq;
328     VALUE self = iseq_alloc(rb_cISeq);
329
330     GetISeqPtr(self, iseq);
331     iseq->self = self;
332
333     prepare_iseq_build(iseq, name, filename, parent, type, bopt, option);
334     ruby_iseq_compile(self, node);
335     cleanup_iseq_build(iseq);
336     return self;
337 }
338
339 VALUE
340 rb_iseq_new_with_opt(NODE *node, VALUE name, VALUE filename,
341                      VALUE parent, VALUE type,
342                      const rb_compile_option_t *option)
343 {
344     return rb_iseq_new_with_bopt_and_opt(node, name, filename, parent, type,
345                                            Qfalse, option);
346 }
347
348 VALUE
349 rb_iseq_new_with_bopt(NODE *node, VALUE name, VALUE filename,
350                        VALUE parent, VALUE type, VALUE bopt)
351 {
352     return rb_iseq_new_with_bopt_and_opt(node, name, filename, parent, type,
353                                            bopt, &COMPILE_OPTION_DEFAULT);
354 }
355
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;}
360 static VALUE
361 iseq_load(VALUE self, VALUE data, VALUE parent, VALUE opt)
362 {
363     VALUE iseqval = iseq_alloc(self);
364
365     VALUE magic, version1, version2, format_type, misc;
366     VALUE name, filename;
367     VALUE type, body, locals, args, exception;
368
369     VALUE iseq_type;
370     struct st_table *type_map = 0;
371     rb_iseq_t *iseq;
372     rb_compile_option_t option;
373     int i = 0;
374
375     /* [magic, major_version, minor_version, format_type, misc,
376      *  name, filename,
377      *  type, locals, args, exception_table, body]
378      */
379
380     data        = CHECK_ARRAY(data);
381
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 */
387
388     name        = CHECK_STRING(rb_ary_entry(data, i++));
389     filename    = CHECK_STRING(rb_ary_entry(data, i++));
390
391     type        = CHECK_SYMBOL(rb_ary_entry(data, i++));
392     locals      = CHECK_ARRAY(rb_ary_entry(data, i++));
393
394     args        = rb_ary_entry(data, i++);
395     if (FIXNUM_P(args) || (args = CHECK_ARRAY(args))) {
396         /* */
397     }
398
399     exception   = CHECK_ARRAY(rb_ary_entry(data, i++));
400     body        = CHECK_ARRAY(rb_ary_entry(data, i++));
401
402     GetISeqPtr(iseqval, iseq);
403     iseq->self = iseqval;
404
405     if (type_map == 0) {
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);
416     }
417
418     if (st_lookup(type_map, type, &iseq_type) == 0) {
419         const char *typename = rb_id2name(type);
420         if (typename)
421             rb_raise(rb_eTypeError, "unsupport type: :%s", typename);
422         else
423             rb_raise(rb_eTypeError, "unsupport type: %p", (void *)type);
424     }
425
426     if (parent == Qnil) {
427         parent = 0;
428     }
429
430     make_compile_option(&option, opt);
431     prepare_iseq_build(iseq, name, filename,
432                        parent, iseq_type, 0, &option);
433
434     ruby_iseq_build_from_ary(iseq, locals, args, exception, body);
435
436     cleanup_iseq_build(iseq);
437     return iseqval;
438 }
439
440 static VALUE
441 iseq_s_load(int argc, VALUE *argv, VALUE self)
442 {
443     VALUE data, opt=Qnil;
444     rb_scan_args(argc, argv, "11", &data, &opt);
445
446     return iseq_load(self, data, 0, opt);
447 }
448
449 VALUE
450 ruby_iseq_load(VALUE data, VALUE parent, VALUE opt)
451 {
452     return iseq_load(rb_cISeq, data, parent, opt);
453 }
454
455 static NODE *
456 compile_string(VALUE str, VALUE file, VALUE line)
457 {
458     VALUE parser = rb_parser_new();
459     NODE *node = rb_parser_compile_string(parser, StringValueCStr(file),
460                                           str, NUM2INT(line));
461
462     if (!node) {
463         rb_exc_raise(GET_THREAD()->errinfo);    /* TODO: check err */
464     }
465     return node;
466 }
467
468 VALUE
469 rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE line, VALUE opt)
470 {
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);
475
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);
480     }
481     else {
482         return rb_iseq_new_with_opt(node, rb_str_new2("<compiled>"), file, Qfalse,
483                                     ISEQ_TYPE_TOP, &option);
484     }
485 }
486
487 VALUE
488 rb_iseq_compile(VALUE src, VALUE file, VALUE line)
489 {
490     return rb_iseq_compile_with_option(src, file, line, Qnil);
491 }
492
493 static VALUE
494 iseq_s_compile(int argc, VALUE *argv, VALUE self)
495 {
496     VALUE src, file = Qnil, line = INT2FIX(1), opt = Qnil;
497
498     rb_secure(1);
499
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;
503
504     return rb_iseq_compile_with_option(src, file, line, opt);
505 }
506
507 static VALUE
508 iseq_s_compile_file(int argc, VALUE *argv, VALUE self)
509 {
510     VALUE file, line = INT2FIX(1), opt = Qnil;
511     VALUE parser;
512     VALUE f;
513     NODE *node;
514     const char *fname;
515     rb_compile_option_t option;
516
517     rb_secure(1);
518     rb_scan_args(argc, argv, "11", &file, &opt);
519     FilePathValue(file);
520     fname = StringValueCStr(file);
521
522     f = rb_file_open_str(file, "r");
523
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);
529 }
530
531 static VALUE
532 iseq_s_compile_option_set(VALUE self, VALUE opt)
533 {
534     rb_compile_option_t option;
535     rb_secure(1);
536     make_compile_option(&option, opt);
537     COMPILE_OPTION_DEFAULT = option;
538     return opt;
539 }
540
541 static VALUE
542 iseq_s_compile_option_get(VALUE self)
543 {
544     return make_compile_option_value(&COMPILE_OPTION_DEFAULT);
545 }
546
547 static rb_iseq_t *
548 iseq_check(VALUE val)
549 {
550     rb_iseq_t *iseq;
551     GetISeqPtr(val, iseq);
552     if (!iseq->name) {
553         rb_raise(rb_eTypeError, "uninitialized InstructionSequence");
554     }
555     return iseq;
556 }
557
558 static VALUE
559 iseq_eval(VALUE self)
560 {
561     rb_secure(1);
562     return rb_iseq_eval(self);
563 }
564
565 static VALUE
566 iseq_inspect(VALUE self)
567 {
568     rb_iseq_t *iseq;
569     GetISeqPtr(self, iseq);
570     if (!iseq->name) {
571         return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self));
572     }
573
574     return rb_sprintf("<%s:%s@%s>",
575                       rb_obj_classname(self),
576                       RSTRING_PTR(iseq->name), RSTRING_PTR(iseq->filename));
577 }
578
579 static
580 VALUE iseq_data_to_ary(rb_iseq_t *iseq);
581
582 static VALUE
583 iseq_to_a(VALUE self)
584 {
585     rb_iseq_t *iseq = iseq_check(self);
586     rb_secure(1);
587     return iseq_data_to_ary(iseq);
588 }
589
590 int
591 rb_iseq_first_lineno(rb_iseq_t *iseq)
592 {
593     return iseq->insn_info_table[0].line_no;
594 }
595
596 /* TODO: search algorithm is brute force.
597          this should be binary search or so. */
598
599 static struct iseq_insn_info_entry *
600 get_insn_info(const rb_iseq_t *iseq, const unsigned long pos)
601 {
602     unsigned long i, size = iseq->insn_info_size;
603     struct iseq_insn_info_entry *table = iseq->insn_info_table;
604
605     for (i = 0; i < size; i++) {
606         if (table[i].position == pos) {
607             return &table[i];
608         }
609     }
610
611     return 0;
612 }
613
614 static unsigned short
615 find_line_no(rb_iseq_t *iseq, unsigned long pos)
616 {
617     struct iseq_insn_info_entry *entry = get_insn_info(iseq, pos);
618     if (entry) {
619         return entry->line_no;
620     }
621     else {
622         return 0;
623     }
624 }
625
626 static unsigned short
627 find_prev_line_no(rb_iseq_t *iseqdat, unsigned long pos)
628 {
629     unsigned long i, size = iseqdat->insn_info_size;
630     struct iseq_insn_info_entry *iiary = iseqdat->insn_info_table;
631
632     for (i = 0; i < size; i++) {
633         if (iiary[i].position == pos) {
634             if (i > 0) {
635                 return iiary[i - 1].line_no;
636             }
637             else {
638                 return 0;
639             }
640         }
641     }
642
643     return 0;
644 }
645
646 static VALUE
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)
650 {
651     const char *types = insn_op_types(insn);
652     char type = types[op_no];
653     VALUE ret;
654
655     switch (type) {
656       case TS_OFFSET:           /* LONG */
657         ret = rb_sprintf("%ld", pos + len + op);
658         break;
659
660       case TS_NUM:              /* ULONG */
661         ret = rb_sprintf("%lu", op);
662         break;
663
664       case TS_LINDEX:
665         {
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]);
669
670             if (name) {
671                 ret = rb_str_new2(name);
672             }
673             else {
674                 ret = rb_str_new2("*");
675             }
676             break;
677         }
678       case TS_DINDEX:{
679         if (insn == BIN(getdynamic) || insn == BIN(setdynamic)) {
680             rb_iseq_t *ip = iseq;
681             int level = *pnop, i;
682             const char *name;
683             for (i = 0; i < level; i++) {
684                 ip = ip->parent_iseq;
685             }
686             name = rb_id2name(ip->local_table[ip->local_size - op]);
687
688             if (!name) {
689                 name = "*";
690             }
691             ret = rb_str_new2(name);
692         }
693         else {
694             ret = rb_inspect(INT2FIX(op));
695         }
696         break;
697       }
698       case TS_ID:               /* ID (symbol) */
699         op = ID2SYM(op);
700
701       case TS_VALUE:            /* VALUE */
702         ret = rb_inspect(op);
703         if (CLASS_OF(op) == rb_cISeq) {
704             rb_ary_push(child, op);
705         }
706         break;
707
708       case TS_ISEQ:             /* iseq */
709         {
710             rb_iseq_t *iseq = (rb_iseq_t *)op;
711             if (iseq) {
712                 ret = iseq->name;
713                 if (child) {
714                     rb_ary_push(child, iseq->self);
715                 }
716             }
717             else {
718                 ret = rb_str_new2("nil");
719             }
720             break;
721         }
722       case TS_GENTRY:
723         {
724             struct global_entry *entry = (struct global_entry *)op;
725             ret = rb_str_dup(rb_id2str(entry->id));
726         }
727         break;
728
729       case TS_IC:
730         ret = rb_str_new2("<ic>");
731         break;
732
733       case TS_CDHASH:
734         ret = rb_str_new2("<cdhash>");
735         break;
736
737       case TS_FUNCPTR:
738         ret = rb_str_new2("<funcptr>");
739         break;
740
741       default:
742         rb_bug("ruby_iseq_disasm: unknown operand type: %c", type);
743     }
744     return ret;
745 }
746
747 /**
748  * Disassemble a instruction
749  * Iseq -> Iseq inspect object
750  */
751 VALUE
752 ruby_iseq_disasm_insn(VALUE ret, VALUE *iseq, int pos,
753                       rb_iseq_t *iseqdat, VALUE child)
754 {
755     int insn = iseq[pos];
756     int len = insn_len(insn);
757     int j;
758     const char *types = insn_op_types(insn);
759     VALUE str = rb_str_new(0, 0);
760     const char *insn_name_buff;
761
762     insn_name_buff = insn_name(insn);
763     if (1) {
764         rb_str_catf(str, "%04d %-16s ", pos, insn_name_buff);
765     }
766     else {
767         rb_str_catf(str, "%04d %-16.*s ", pos,
768                     (int)strcspn(insn_name_buff, "_"), insn_name_buff);
769     }
770
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],
775                                           child);
776         rb_str_concat(str, opstr);
777
778         if (types[j + 1]) {
779             rb_str_cat2(str, ", ");
780         }
781     }
782
783     if (1) {
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);
790         }
791     }
792     else {
793         /* for debug */
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);
799     }
800
801     if (ret) {
802         rb_str_cat2(str, "\n");
803         rb_str_concat(ret, str);
804     }
805     else {
806         printf("%s\n", RSTRING_PTR(str));
807     }
808     return len;
809 }
810
811 static const char *
812 catch_type(int type)
813 {
814     switch (type) {
815       case CATCH_TYPE_RESCUE:
816         return "rescue";
817       case CATCH_TYPE_ENSURE:
818         return "ensure";
819       case CATCH_TYPE_RETRY:
820         return "retry";
821       case CATCH_TYPE_BREAK:
822         return "break";
823       case CATCH_TYPE_REDO:
824         return "redo";
825       case CATCH_TYPE_NEXT:
826         return "next";
827       default:
828         rb_bug("unknown catch type (%d)", type);
829         return 0;
830     }
831 }
832
833 VALUE
834 ruby_iseq_disasm(VALUE self)
835 {
836     rb_iseq_t *iseqdat = iseq_check(self);
837     VALUE *iseq;
838     VALUE str = rb_str_new(0, 0);
839     VALUE child = rb_ary_new();
840     unsigned long size;
841     int i;
842     ID *tbl;
843     enum {header_minlen = 72};
844
845     rb_secure(1);
846
847     iseq = iseqdat->iseq;
848     size = iseqdat->iseq_size;
849
850     rb_str_cat2(str, "== disasm: ");
851
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);
856     }
857     rb_str_cat2(str, "\n");
858
859     /* show catch table information */
860     if (iseqdat->catch_table_size != 0) {
861         rb_str_cat2(str, "== catch table\n");
862     }
863     for (i = 0; i < iseqdat->catch_table_size; i++) {
864         struct iseq_catch_table_entry *entry = &iseqdat->catch_table[i];
865         rb_str_catf(str,
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);
869         if (entry->iseq) {
870             rb_str_concat(str, ruby_iseq_disasm(entry->iseq));
871         }
872     }
873     if (iseqdat->catch_table_size != 0) {
874         rb_str_cat2(str, "|-------------------------------------"
875                     "-----------------------------------\n");
876     }
877
878     /* show local table information */
879     tbl = iseqdat->local_table;
880
881     if (tbl) {
882         rb_str_catf(str,
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);
889
890         for (i = 0; i < iseqdat->local_table_size; i++) {
891             const char *name = rb_id2name(tbl[i]);
892             char info[0x100];
893             char argi[0x100] = "";
894             char opti[0x100] = "";
895
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]);
902                 }
903             }
904
905             snprintf(argi, sizeof(argi), "%s%s%s%s%s",  /* arg, opts, rest, post  block */
906                      iseqdat->argc > i ? "Arg" : "",
907                      opti,
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" : "");
912
913             snprintf(info, sizeof(info), "%s%s%s%s", name ? name : "?",
914                      *argi ? "<" : "", argi, *argi ? ">" : "");
915
916             rb_str_catf(str, "[%2d] %-11s", iseqdat->local_size - i, info);
917         }
918         rb_str_cat2(str, "\n");
919     }
920
921     /* show each line */
922     for (i = 0; i < size;) {
923         i += ruby_iseq_disasm_insn(str, iseq, i, iseqdat, child);
924     }
925
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));
929     }
930
931     return str;
932 }
933
934 static VALUE
935 iseq_s_disasm(VALUE klass, VALUE body)
936 {
937     extern NODE *rb_method_body(VALUE body);
938     NODE *node;
939     VALUE ret = Qnil;
940
941     rb_secure(1);
942
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);
947         }
948     }
949
950     return ret;
951 }
952
953 const char *
954 ruby_node_name(int node)
955 {
956     switch (node) {
957 #include "node_name.inc"
958       default:
959         rb_bug("unknown node (%d)", node);
960         return 0;
961     }
962 }
963
964 #define DECL_SYMBOL(name) \
965   static VALUE sym_##name
966
967 #define INIT_SYMBOL(name) \
968   sym_##name = ID2SYM(rb_intern(#name))
969
970 static VALUE
971 register_label(struct st_table *table, int idx)
972 {
973     VALUE sym;
974     char buff[8 + (sizeof(idx) * CHAR_BIT * 32 / 100)];
975
976     snprintf(buff, sizeof(buff), "label_%u", idx);
977     sym = ID2SYM(rb_intern(buff));
978     st_insert(table, idx, sym);
979     return sym;
980 }
981
982 static VALUE
983 exception_type2symbol(VALUE type)
984 {
985     ID id;
986     switch(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;
993       default:
994         rb_bug("...");
995     }
996     return ID2SYM(id);
997 }
998
999 static int
1000 cdhash_each(VALUE key, VALUE value, VALUE ary)
1001 {
1002     rb_ary_push(ary, key);
1003     rb_ary_push(ary, value);
1004     return ST_CONTINUE;
1005 }
1006
1007 static VALUE
1008 iseq_data_to_ary(rb_iseq_t *iseq)
1009 {
1010     int i, pos, line = 0;
1011     VALUE *seq;
1012
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, ...], ...] */
1018     VALUE nbody;
1019     VALUE exception = rb_ary_new(); /* [[....]] */
1020     VALUE misc = rb_hash_new();
1021
1022     static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1023     struct st_table *labels_table = st_init_numtable();
1024
1025     DECL_SYMBOL(top);
1026     DECL_SYMBOL(method);
1027     DECL_SYMBOL(block);
1028     DECL_SYMBOL(class);
1029     DECL_SYMBOL(rescue);
1030     DECL_SYMBOL(ensure);
1031     DECL_SYMBOL(eval);
1032     DECL_SYMBOL(main);
1033     DECL_SYMBOL(defined_guard);
1034
1035     if (sym_top == 0) {
1036         int i;
1037         for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1038             insn_syms[i] = ID2SYM(rb_intern(insn_name(i)));
1039         }
1040         INIT_SYMBOL(top);
1041         INIT_SYMBOL(method);
1042         INIT_SYMBOL(block);
1043         INIT_SYMBOL(class);
1044         INIT_SYMBOL(rescue);
1045         INIT_SYMBOL(ensure);
1046         INIT_SYMBOL(eval);
1047         INIT_SYMBOL(main);
1048         INIT_SYMBOL(defined_guard);
1049     }
1050
1051     /* type */
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");
1063     };
1064
1065     /* locals */
1066     for (i=0; i<iseq->local_table_size; i++) {
1067         ID lid = iseq->local_table[i];
1068         if (lid) {
1069             if (rb_id2str(lid)) rb_ary_push(locals, ID2SYM(lid));
1070         }
1071         else {
1072             rb_ary_push(locals, ID2SYM(rb_intern("#arg_rest")));
1073         }
1074     }
1075
1076     /* args */
1077     {
1078         /*
1079          * [argc,                 # argc
1080          *  [label1, label2, ...] # opts
1081          *  rest index,
1082          *  post_len
1083          *  post_start
1084          *  block index,
1085          *  simple,
1086          * ]
1087          */
1088         VALUE arg_opt_labels = rb_ary_new();
1089         int j;
1090
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]));
1094         }
1095
1096         /* commit */
1097         if (iseq->arg_simple == 1) {
1098             args = INT2FIX(iseq->argc);
1099         }
1100         else {
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));
1108         }
1109     }
1110
1111     /* body */
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);
1117
1118         rb_ary_push(ary, insn_syms[insn]);
1119         for (j=0; j<len-1; j++, seq++) {
1120             switch (insn_op_type(insn, j)) {
1121               case TS_OFFSET: {
1122                 unsigned int idx = nseq - iseq->iseq + *seq;
1123                 rb_ary_push(ary, register_label(labels_table, idx));
1124                 break;
1125               }
1126               case TS_LINDEX:
1127               case TS_DINDEX:
1128               case TS_NUM:
1129                 rb_ary_push(ary, INT2FIX(*seq));
1130                 break;
1131               case TS_VALUE:
1132                 rb_ary_push(ary, *seq);
1133                 break;
1134               case TS_ISEQ:
1135                 {
1136                     rb_iseq_t *iseq = (rb_iseq_t *)*seq;
1137                     if (iseq) {
1138                         VALUE val = iseq_data_to_ary(iseq);
1139                         rb_ary_push(ary, val);
1140                     }
1141                     else {
1142                         rb_ary_push(ary, Qnil);
1143                     }
1144                 }
1145                 break;
1146               case TS_GENTRY:
1147                 {
1148                     struct global_entry *entry = (struct global_entry *)*seq;
1149                     rb_ary_push(ary, ID2SYM(entry->id));
1150                 }
1151                 break;
1152               case TS_IC:
1153                 rb_ary_push(ary, Qnil);
1154                 break;
1155               case TS_ID:
1156                 rb_ary_push(ary, ID2SYM(*seq));
1157                 break;
1158               case TS_CDHASH:
1159                 {
1160                     VALUE hash = *seq;
1161                     VALUE val = rb_ary_new();
1162                     int i;
1163
1164                     rb_hash_foreach(hash, cdhash_each, val);
1165
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;
1169
1170                         rb_ary_store(val, i+1,
1171                                      register_label(labels_table, idx));
1172                     }
1173                     rb_ary_push(ary, val);
1174                 }
1175                 break;
1176               default:
1177                 rb_bug("unknown operand: %c", insn_op_type(insn, j));
1178             }
1179         }
1180         rb_ary_push(body, ary);
1181     }
1182
1183     nbody = body;
1184
1185     /* exception */
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));
1190         if (entry->iseq) {
1191             rb_iseq_t *eiseq;
1192             GetISeqPtr(entry->iseq, eiseq);
1193             rb_ary_push(ary, iseq_data_to_ary(eiseq));
1194         }
1195         else {
1196             rb_ary_push(ary, Qnil);
1197         }
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);
1203     }
1204
1205     /* make body with labels and insert line number */
1206     body = rb_ary_new();
1207
1208     for (i=0, pos=0; i<RARRAY_LEN(nbody); i++) {
1209         VALUE ary = RARRAY_PTR(nbody)[i];
1210         VALUE label;
1211
1212         if (st_lookup(labels_table, pos, &label)) {
1213             rb_ary_push(body, label);
1214         }
1215
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));
1219         }
1220
1221         rb_ary_push(body, ary);
1222         pos += RARRAY_LEN(ary);
1223     }
1224
1225     st_free_table(labels_table);
1226
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));
1230
1231     /* 
1232      * [:magic, :major_version, :minor_version, :format_type, :misc,
1233      *  :name, :filename, :type, :locals, :args,
1234      *  :catch_table, :bytecode]
1235      */
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);
1248     return val;
1249 }
1250
1251 struct st_table *
1252 ruby_insn_make_insn_table(void)
1253 {
1254     struct st_table *table;
1255     int i;
1256     table = st_init_numtable();
1257
1258     for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1259         st_insert(table, ID2SYM(rb_intern(insn_name(i))), i);
1260     }
1261
1262     return table;
1263 }
1264
1265 VALUE
1266 rb_iseq_clone(VALUE iseqval, VALUE newcbase)
1267 {
1268     VALUE newiseq = iseq_alloc(rb_cISeq);
1269     rb_iseq_t *iseq0, *iseq1;
1270
1271     GetISeqPtr(iseqval, iseq0);
1272     GetISeqPtr(newiseq, iseq1);
1273
1274     *iseq1 = *iseq0;
1275     iseq1->self = newiseq;
1276     if (!iseq1->orig) {
1277         iseq1->orig = iseqval;
1278     }
1279     if (newcbase) {
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;
1283         }
1284     }
1285
1286     return newiseq;
1287 }
1288
1289 /* ruby2cext */
1290
1291 VALUE
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,
1299     const char *name,
1300     const char *filename)
1301 {
1302     int i;
1303     VALUE iseqval = iseq_alloc(rb_cISeq);
1304     rb_iseq_t *iseq;
1305     GetISeqPtr(iseqval, iseq);
1306
1307     /* copy 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;
1313
1314     iseq->iseq = ALLOC_N(VALUE, iseq->iseq_size);
1315
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;
1319     }
1320
1321     ruby_iseq_translate_threaded_code(iseq);
1322
1323 #define ALLOC_AND_COPY(dst, src, type, size) do { \
1324   if (size) { \
1325       (dst) = ALLOC_N(type, (size)); \
1326       MEMCPY((dst), (src), type, (size)); \
1327   } \
1328 } while (0)
1329
1330     ALLOC_AND_COPY(iseq->insn_info_table, insn_info_table,
1331                    struct iseq_insn_info_entry, iseq->insn_info_size);
1332
1333     ALLOC_AND_COPY(iseq->catch_table, catch_table,
1334                    struct iseq_catch_table_entry, iseq->catch_table_size);
1335
1336     ALLOC_AND_COPY(iseq->arg_opt_table, arg_opt_table,
1337                    VALUE, iseq->arg_opts);
1338
1339     set_relation(iseq, 0);
1340
1341     return iseqval;
1342 }
1343
1344 void
1345 Init_ISeq(void)
1346 {
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);
1355
1356     /* disable this feature because there is no verifier. */
1357     /* rb_define_singleton_method(rb_cISeq, "load", iseq_s_load, -1); */
1358     (void)iseq_s_load;
1359
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);
1367 }
1368