21 #define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
24 #include "insns_info.inc"
26 #define ISEQ_MAJOR_VERSION 2
27 #define ISEQ_MINOR_VERSION 0
31 #define hidden_obj_p(obj) (!SPECIAL_CONST_P(obj) && !RBASIC(obj)->klass)
208 if (type == ISEQ_TYPE_TOP) {
215 cref->nd_refinements =
Qnil;
222 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
227 else if (
RTEST(parent)) {
238 if (type == ISEQ_TYPE_MAIN) {
308 if (
RTEST(coverages)) {
353 else if (opt ==
Qtrue) {
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; } \
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); \
377 #undef SET_COMPILE_OPTION
378 #undef SET_COMPILE_OPTION_NUM
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))
404 #undef SET_COMPILE_OPTION
405 #undef SET_COMPILE_OPTION_NUM
414 &COMPILE_OPTION_DEFAULT);
421 &COMPILE_OPTION_DEFAULT);
430 parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
444 prepare_iseq_build(iseq, name, path, absolute_path, first_lineno, parent, type, bopt, option);
466 bopt, &COMPILE_OPTION_DEFAULT);
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")
478 VALUE magic, version1, version2, format_type, misc;
479 VALUE name, path, absolute_path, first_lineno;
500 ((void)magic, (
void)version1, (void)version2, (
void)format_type, (void)misc);
520 iseq->
self = iseqval;
535 if (
st_lookup(type_map, type, &iseq_type) == 0) {
544 if (parent ==
Qnil) {
550 parent, (
enum iseq_type)iseq_type, 0, &option);
612 if (base_block && base_block->
iseq) {
614 file, absolute_path, line, base_block->
iseq->
self,
615 ISEQ_TYPE_EVAL, &option);
619 ISEQ_TYPE_TOP, &option);
673 rb_scan_args(argc, argv,
"14", &src, &file, &path, &line, &opt);
722 ISEQ_TYPE_TOP, &option);
763 COMPILE_OPTION_DEFAULT = option;
1067 const int debug = 0;
1078 else if (
size == 1) {
1082 for (i=1; i<
size; i++) {
1083 if (debug) printf(
"table[%"PRIdSIZE"]: position: %d, line: %d, pos: %"PRIdSIZE"\n",
1086 if (table[i].position == pos) {
1089 if (table[i].position > pos) {
1125 str = default_value;
1136 int len,
size_t pos,
VALUE *pnop,
VALUE child)
1138 const char *types = insn_op_types(insn);
1139 char type = types[op_no];
1152 if (insn == BIN(getlocal) || insn == BIN(setlocal)) {
1157 for (i = 0; i <
level; i++) {
1252 rb_bug(
"insn_operand_intern: unknown operand type: %c", type);
1265 VALUE insn = iseq[pos];
1266 int len = insn_len(insn);
1268 const char *types = insn_op_types(insn);
1270 const char *insn_name_buff;
1272 insn_name_buff = insn_name(insn);
1278 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
1281 for (j = 0; types[j]; j++) {
1282 const char *types = insn_op_types(insn);
1284 len, pos, &iseq[pos + j + 2],
1295 unsigned int prev = pos == 0 ? 0 :
find_line_no(iseqdat, pos - 1);
1296 if (line_no && line_no != prev) {
1298 slen = (slen > 70) ? 0 : (70 - slen);
1299 str =
rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
1317 case CATCH_TYPE_RESCUE:
1319 case CATCH_TYPE_ENSURE:
1321 case CATCH_TYPE_RETRY:
1323 case CATCH_TYPE_BREAK:
1325 case CATCH_TYPE_REDO:
1327 case CATCH_TYPE_NEXT:
1330 rb_bug(
"unknown catch type (%d)", type);
1365 enum {header_minlen = 72};
1369 iseq = iseqdat->
iseq;
1377 memset(
RSTRING_PTR(str) + l,
'=', header_minlen - l);
1388 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
1390 (
int)entry->
end, (
int)entry->
sp, (
int)entry->
cont);
1396 rb_str_cat2(str,
"|-------------------------------------"
1397 "-----------------------------------\n");
1405 "local table (size: %d, argc: %d "
1406 "[opts: %d, rest: %d, post: %d, block: %d] s%d)\n",
1415 char argi[0x100] =
"";
1416 char opti[0x100] =
"";
1421 if (i >= argc && i < argc + opts - 1) {
1427 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s",
1428 iseqdat->
argc > i ?
"Arg" :
"",
1430 iseqdat->
arg_rest == i ?
"Rest" :
"",
1432 i < iseqdat->arg_post_start + iseqdat->
arg_post_len) ?
"Post" :
"",
1433 iseqdat->
arg_block == i ?
"Block" :
"");
1448 for (n = 0; n <
size;) {
1581 #include "node_name.inc"
1583 rb_bug(
"unknown node (%d)", node);
1588 #define DECL_SYMBOL(name) \
1589 static VALUE sym_##name
1591 #define INIT_SYMBOL(name) \
1592 sym_##name = ID2SYM(rb_intern(#name))
1598 char buff[8 + (
sizeof(idx) *
CHAR_BIT * 32 / 100)];
1600 snprintf(buff,
sizeof(buff),
"label_%lu", idx);
1637 unsigned int line = 0;
1649 static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1664 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
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");
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;
1746 for (j=0; j<len-1; j++, seq++) {
1747 switch (insn_op_type(insn, j)) {
1749 unsigned long idx = nseq - iseq->
iseq + *seq;
1807 unsigned long idx = nseq - iseq->
iseq + pos;
1816 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
1852 if (
st_lookup(labels_table, pos, &label)) {
1905 iseq1->
self = newiseq;
1907 iseq1->
orig = iseqval;
1919 iseq1->
klass = newcbase;
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) ( \
1935 rb_id2str(PARAM_ID(i)) ? \
1936 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
1942 for (i = 0; i < iseq->
argc; i++) {
1949 for (i = 0; i < iseq->
argc; i++) {
1954 for (; i < r; i++) {
2002 static const char expr_names[][18] = {
2004 "instance-variable",
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;
2027 GET_VM()->defined_strings = defs;
2045 const char **local_table,
2046 const VALUE *arg_opt_table,
2050 const unsigned short first_lineno)
2058 *iseq = *iseq_template;
2063 iseq->
self = iseqval;
2068 iseq->
iseq[
i] = BIN(opt_call_c_function);
2074 #define ALLOC_AND_COPY(dst, src, type, size) do { \
2076 (dst) = ALLOC_N(type, (size)); \
2077 MEMCPY((dst), (src), type, (size)); \
2108 for (pos = 0; cont && pos < iseq->
iseq_size; pos += insn_len(insn)) {
2109 insn = iseq->
iseq[pos];
2111 if (insn == BIN(trace)) {
2121 cont = (*func)(line, &events, data);
2122 if (current_events != events) {
2124 (
VALUE)(current_events | (events & RUBY_EVENT_SPECIFIED_LINE));
2165 if (data->
pos == 0) {
2211 if (data.
prev == 0) {
#define VM_CALL_ARGS_BLOCKARG
int rb_iseq_disasm_insn(VALUE ret, VALUE *iseq, size_t pos, rb_iseq_t *iseqdat, VALUE child)
Disassemble a instruction Iseq -> Iseq inspect object.
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)
unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
VALUE rb_ary_entry(VALUE ary, long offset)
void rb_bug(const char *fmt,...)
#define DECL_SYMBOL(name)
NODE * rb_parser_compile_file(volatile VALUE vparser, const char *f, VALUE file, int start)
static void iseq_mark(void *ptr)
VALUE rb_iseq_new(NODE *node, VALUE name, VALUE path, VALUE absolute_path, VALUE parent, enum iseq_type type)
VALUE rb_iseq_line_trace_all(VALUE iseqval)
static void make_compile_option(rb_compile_option_t *option, VALUE opt)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
if(dispIdMember==DISPID_VALUE)
#define GetProcPtr(obj, ptr)
#define RUBY_VM_NORMAL_ISEQ_P(ptr)
int rb_iseq_line_trace_each(VALUE iseqval, int(*func)(int line, rb_event_flag_t *events_ptr, void *d), void *data)
static void set_relation(rb_iseq_t *iseq, const VALUE parent)
int st_insert(st_table *, st_data_t, st_data_t)
struct iseq_compile_data * compile_data
struct iseq_compile_data_storage * storage_head
static VALUE iseq_absolute_path(VALUE self)
void rb_define_private_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
rb_call_info_t * callinfo_entries
static VALUE iseq_s_compile(int argc, VALUE *argv, VALUE self)
static VALUE id_to_name(ID id, VALUE default_value)
int rb_iseq_translate_threaded_code(rb_iseq_t *iseq)
VALUE rb_ary_push(VALUE ary, VALUE item)
SSL_METHOD *(* func)(void)
void st_free_table(st_table *)
VALUE rb_str_concat(VALUE, VALUE)
#define OPT_INLINE_CONST_CACHE
VALUE rb_ary_tmp_new(long capa)
VALUE rb_iseq_defined_string(enum defined_type type)
VALUE rb_iseq_compile_with_option(VALUE src, VALUE file, VALUE absolute_path, VALUE line, rb_block_t *base_block, VALUE opt)
static int cdhash_each(VALUE key, VALUE value, VALUE ary)
struct rb_iseq_struct * local_iseq
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
#define RUBY_MARK_LEAVE(msg)
struct iseq_compile_data_storage * next
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
struct iseq_inline_cache_entry * ic_entries
#define VM_CALL_ARGS_SPLAT
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)
VALUE rb_iseq_compile_node(VALUE self, NODE *node)
static VALUE iseq_label(VALUE self)
#define ALLOC_AND_COPY(dst, src, type, size)
VALUE rb_file_open_str(VALUE, const char *)
int rb_str_cmp(VALUE, VALUE)
static VALUE iseq_path(VALUE self)
const char * rb_obj_classname(VALUE)
#define OPT_TAILCALL_OPTIMIZATION
const rb_compile_option_t * option
static rb_iseq_location_t * iseq_location_setup(rb_iseq_t *iseq, VALUE path, VALUE absolute_path, VALUE name, size_t first_lineno)
enum iseq_catch_table_entry::catch_type type
void rb_hash_foreach(VALUE hash, int(*func)(ANYARGS), VALUE farg)
void rb_exc_raise(VALUE mesg)
#define SET_COMPILE_OPTION_NUM(o, h, mem)
static VALUE iseq_s_compile_file(int argc, VALUE *argv, VALUE self)
#define RB_TYPE_P(obj, type)
void * ruby_xcalloc(size_t n, size_t size)
enum rb_iseq_struct::iseq_type type
static VALUE iseq_load(VALUE self, VALUE data, VALUE parent, VALUE opt)
int st_lookup(st_table *, st_data_t, st_data_t *)
#define MEMZERO(p, type, n)
static rb_compile_option_t COMPILE_OPTION_DEFAULT
#define OPT_PEEPHOLE_OPTIMIZATION
static struct iseq_line_info_entry * get_line_info(const rb_iseq_t *iseq, size_t pos)
int rb_str_symname_p(VALUE)
NODE * rb_parser_compile_string(volatile VALUE vparser, const char *f, VALUE s, int line)
VALUE rb_iseq_new_main(NODE *node, VALUE path, VALUE absolute_path)
VALUE rb_iseq_new_top(NODE *node, VALUE name, VALUE path, VALUE absolute_path, VALUE parent)
static VALUE CHECK_INTEGER(VALUE v)
#define VM_CALL_ARGS_SKIP_SETUP
int rb_iseq_first_lineno(const rb_iseq_t *iseq)
RUBY_EXTERN VALUE rb_cObject
static VALUE register_label(struct st_table *table, unsigned long idx)
VALUE rb_get_coverages(void)
VALUE rb_str_cat2(VALUE, const char *)
static unsigned int find_line_no(const rb_iseq_t *iseq, size_t pos)
static VALUE obj_resurrect(VALUE obj)
static VALUE make_compile_option_value(rb_compile_option_t *option)
int argc
argument information
static VALUE iseq_s_of(VALUE klass, VALUE body)
static VALUE iseq_eval(VALUE self)
void rb_ary_store(VALUE ary, long idx, VALUE val)
#define RUBY_MARK_ENTER(msg)
#define OPT_INSTRUCTIONS_UNIFICATION
VALUE rb_str_resurrect(VALUE str)
VALUE rb_str_resize(VALUE, long)
static VALUE exception_type2symbol(VALUE type)
#define INIT_SYMBOL(name)
#define RUBY_EVENT_SPECIFIED_LINE
#define hidden_obj_p(obj)
VALUE rb_obj_is_proc(VALUE)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
#define ISEQ_MINOR_VERSION
VALUE rb_sprintf(const char *format,...)
#define OPT_TRACE_INSTRUCTION
VALUE rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE locals, VALUE args, VALUE exception, VALUE body)
static VALUE iseq_data_to_ary(rb_iseq_t *iseq)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
struct rb_iseq_struct * parent_iseq
static VALUE iseq_s_compile_option_set(VALUE self, VALUE opt)
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)
#define SET_COMPILE_OPTION(o, h, mem)
VALUE rb_iseq_eval(VALUE iseqval)
rb_iseq_location_t location
VALUE rb_iseq_load(VALUE data, VALUE parent, VALUE opt)
#define INITIAL_ISEQ_COMPILE_DATA_STORAGE_BUFF_SIZE
#define OPT_STACK_CACHING
VALUE rb_str_new_cstr(const char *)
#define RARRAY_LENINT(ary)
st_table * st_init_numtable(void)
static VALUE cleanup_iseq_build(rb_iseq_t *iseq)
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)
#define StringValueCStr(v)
void rb_iseq_add_mark_object(rb_iseq_t *iseq, VALUE obj)
static VALUE iseq_s_disasm(VALUE klass, VALUE body)
static VALUE iseq_s_load(int argc, VALUE *argv, VALUE self)
#define RUBY_FREE_UNLESS_NULL(ptr)
VALUE rb_iseq_line_trace_specify(VALUE iseqval, VALUE pos, VALUE set)
struct rb_iseq_struct rb_iseq_t
#define OPT_SPECIALISED_INSTRUCTION
static VALUE iseq_alloc(VALUE klass)
#define ISEQ_MAJOR_VERSION
static int line_trace_specify(int line, rb_event_flag_t *events_ptr, void *ptr)
static NODE * parse_string(VALUE str, const char *file, int line)
#define RUBY_FREE_LEAVE(msg)
unsigned long rb_event_flag_t
#define RUBY_FREE_ENTER(msg)
const char * ruby_node_name(int node)
VALUE rb_str_catf(VALUE str, const char *format,...)
struct iseq_compile_data_storage * storage_current
struct iseq_catch_table_entry * catch_table
rb_control_frame_t *FUNC_FASTCALL rb_insn_func_t(rb_thread_t *, rb_control_frame_t *)
VALUE rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, rb_block_t *base_block)
static VALUE iseq_to_a(VALUE self)
static VALUE iseq_inspect(VALUE self)
#define RUBY_MARK_UNLESS_NULL(ptr)
struct iseq_line_info_entry * line_info_table
static const rb_data_type_t iseq_data_type
#define TypedData_Make_Struct(klass, type, data_type, sval)
VALUE rb_str_inspect(VALUE)
rb_iseq_t * rb_method_get_iseq(VALUE body)
static unsigned int hash(const char *str, unsigned int len)
VALUE rb_ary_join(VALUE ary, VALUE sep)
VALUE rb_ary_new2(long capa)
static const char * catch_type(int type)
VALUE rb_parser_new(void)
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
int main(int argc, char **argv)
#define GetISeqPtr(obj, ptr)
VALUE rb_iseq_clone(VALUE iseqval, VALUE newcbase)
const char * rb_id2name(ID id)
static const rb_compile_option_t COMPILE_OPTION_FALSE
#define CONST_ID(var, str)
struct rb_call_info_struct rb_call_info_t
VALUE rb_ary_resurrect(VALUE ary)
VALUE insn_operand_intern(rb_iseq_t *iseq, VALUE insn, int op_no, VALUE op, int len, size_t pos, VALUE *pnop, VALUE child)
NODE * rb_compile_file(const char *f, VALUE file, int start)
static int collect_trace(int line, rb_event_flag_t *events_ptr, void *ptr)
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
static void iseq_free(void *ptr)
static rb_thread_t * GET_THREAD(void)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
VALUE rb_iseq_disasm(VALUE self)
VALUE rb_str_new2(const char *)
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
static VALUE iseq_first_lineno(VALUE self)
#define OPT_OPERANDS_UNIFICATION
static size_t iseq_memsize(const void *ptr)
VALUE rb_iseq_compile(VALUE src, VALUE file, VALUE line)
static void compile_data_free(struct iseq_compile_data *compile_data)
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)
static rb_iseq_t * iseq_check(VALUE val)
int parse_in_eval
Thread-local state of evaluation context.
static VALUE iseq_s_compile_option_get(VALUE self)
static VALUE iseq_base_label(VALUE self)
VALUE rb_str_new(const char *, long)