Ruby 2.0.0p353(2013-11-22revision43784)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

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