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)
205 if (type == ISEQ_TYPE_TOP) {
212 cref->nd_refinements =
Qnil;
219 else if (type == ISEQ_TYPE_METHOD || type == ISEQ_TYPE_CLASS) {
224 else if (
RTEST(parent)) {
235 if (type == ISEQ_TYPE_MAIN) {
296 if (
RTEST(coverages)) {
341 else if (opt ==
Qtrue) {
347 #define SET_COMPILE_OPTION(o, h, mem) \
348 { VALUE flag = rb_hash_aref((h), ID2SYM(rb_intern(#mem))); \
349 if (flag == Qtrue) { (o)->mem = 1; } \
350 else if (flag == Qfalse) { (o)->mem = 0; } \
352 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
353 { VALUE num = rb_hash_aref(opt, ID2SYM(rb_intern(#mem))); \
354 if (!NIL_P(num)) (o)->mem = NUM2INT(num); \
365 #undef SET_COMPILE_OPTION
366 #undef SET_COMPILE_OPTION_NUM
377 #define SET_COMPILE_OPTION(o, h, mem) \
378 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), (o)->mem ? Qtrue : Qfalse)
379 #define SET_COMPILE_OPTION_NUM(o, h, mem) \
380 rb_hash_aset((h), ID2SYM(rb_intern(#mem)), INT2NUM((o)->mem))
392 #undef SET_COMPILE_OPTION
393 #undef SET_COMPILE_OPTION_NUM
402 &COMPILE_OPTION_DEFAULT);
409 &COMPILE_OPTION_DEFAULT);
418 parent, ISEQ_TYPE_MAIN, &COMPILE_OPTION_DEFAULT);
432 prepare_iseq_build(iseq, name, path, absolute_path, first_lineno, parent, type, bopt, option);
454 bopt, &COMPILE_OPTION_DEFAULT);
457 #define CHECK_ARRAY(v) rb_convert_type((v), T_ARRAY, "Array", "to_ary")
458 #define CHECK_STRING(v) rb_convert_type((v), T_STRING, "String", "to_str")
459 #define CHECK_SYMBOL(v) rb_convert_type((v), T_SYMBOL, "Symbol", "to_sym")
466 VALUE magic, version1, version2, format_type, misc;
488 ((
void)magic, (
void)version1, (
void)version2, (
void)format_type, (
void)misc);
508 iseq->
self = iseqval;
523 if (
st_lookup(type_map, type, &iseq_type) == 0) {
532 if (parent ==
Qnil) {
538 parent, (
enum iseq_type)iseq_type, 0, &option);
600 if (base_block && base_block->
iseq) {
602 file, absolute_path, line, base_block->
iseq->
self,
603 ISEQ_TYPE_EVAL, &option);
607 ISEQ_TYPE_TOP, &option);
661 rb_scan_args(argc, argv,
"14", &src, &file, &path, &line, &opt);
710 ISEQ_TYPE_TOP, &option);
751 COMPILE_OPTION_DEFAULT = option;
1055 const int debug = 0;
1066 else if (
size == 1) {
1070 for (i=1; i<
size; i++) {
1071 if (debug) printf(
"table[%"PRIdSIZE"]: position: %d, line: %d, pos: %"PRIdSIZE"\n",
1074 if (table[i].position == pos) {
1077 if (table[i].position > pos) {
1113 str = default_value;
1126 const char *types = insn_op_types(insn);
1127 char type = types[op_no];
1140 if (insn == BIN(getlocal) || insn == BIN(setlocal)) {
1145 for (i = 0; i <
level; i++) {
1240 rb_bug(
"insn_operand_intern: unknown operand type: %c", type);
1253 VALUE insn = iseq[pos];
1254 int len = insn_len(insn);
1256 const char *types = insn_op_types(insn);
1258 const char *insn_name_buff;
1260 insn_name_buff = insn_name(insn);
1266 (
int)strcspn(insn_name_buff,
"_"), insn_name_buff);
1269 for (j = 0; types[j]; j++) {
1270 const char *types = insn_op_types(insn);
1272 len, pos, &iseq[pos + j + 2],
1283 unsigned int prev = pos == 0 ? 0 :
find_line_no(iseqdat, pos - 1);
1284 if (line_no && line_no != prev) {
1286 slen = (slen > 70) ? 0 : (70 - slen);
1287 str =
rb_str_catf(str,
"%*s(%4d)", (
int)slen,
"", line_no);
1305 case CATCH_TYPE_RESCUE:
1307 case CATCH_TYPE_ENSURE:
1309 case CATCH_TYPE_RETRY:
1311 case CATCH_TYPE_BREAK:
1313 case CATCH_TYPE_REDO:
1315 case CATCH_TYPE_NEXT:
1318 rb_bug(
"unknown catch type (%d)", type);
1353 enum {header_minlen = 72};
1357 iseq = iseqdat->
iseq;
1376 "| catch type: %-6s st: %04d ed: %04d sp: %04d cont: %04d\n",
1378 (
int)entry->
end, (
int)entry->
sp, (
int)entry->
cont);
1384 rb_str_cat2(str,
"|-------------------------------------"
1385 "-----------------------------------\n");
1393 "local table (size: %d, argc: %d "
1394 "[opts: %d, rest: %d, post: %d, block: %d] s%d)\n",
1403 char argi[0x100] =
"";
1404 char opti[0x100] =
"";
1409 if (i >= argc && i < argc + opts - 1) {
1415 snprintf(argi,
sizeof(argi),
"%s%s%s%s%s",
1416 iseqdat->
argc > i ?
"Arg" :
"",
1418 iseqdat->
arg_rest == i ?
"Rest" :
"",
1420 i < iseqdat->arg_post_start + iseqdat->
arg_post_len) ?
"Post" :
"",
1421 iseqdat->
arg_block == i ?
"Block" :
"");
1436 for (n = 0; n <
size;) {
1569 #include "node_name.inc"
1571 rb_bug(
"unknown node (%d)", node);
1576 #define DECL_SYMBOL(name) \
1577 static VALUE sym_##name
1579 #define INIT_SYMBOL(name) \
1580 sym_##name = ID2SYM(rb_intern(#name))
1586 char buff[8 + (
sizeof(
idx) *
CHAR_BIT * 32 / 100)];
1588 snprintf(buff,
sizeof(buff),
"label_%lu", idx);
1625 unsigned int line = 0;
1637 static VALUE insn_syms[VM_INSTRUCTION_SIZE];
1652 for (i=0; i<VM_INSTRUCTION_SIZE; i++) {
1667 switch (iseq->
type) {
1668 case ISEQ_TYPE_TOP: type = sym_top;
break;
1669 case ISEQ_TYPE_METHOD: type = sym_method;
break;
1670 case ISEQ_TYPE_BLOCK: type = sym_block;
break;
1671 case ISEQ_TYPE_CLASS: type = sym_class;
break;
1672 case ISEQ_TYPE_RESCUE: type = sym_rescue;
break;
1673 case ISEQ_TYPE_ENSURE: type = sym_ensure;
break;
1674 case ISEQ_TYPE_EVAL: type = sym_eval;
break;
1675 case ISEQ_TYPE_MAIN: type = sym_main;
break;
1676 case ISEQ_TYPE_DEFINED_GUARD: type = sym_defined_guard;
break;
1677 default:
rb_bug(
"unsupported iseq type");
1727 for (seq = iseq->
iseq; seq < iseq->iseq + iseq->
iseq_size; ) {
1728 VALUE insn = *seq++;
1729 int j,
len = insn_len(insn);
1730 VALUE *nseq = seq + len - 1;
1734 for (j=0; j<len-1; j++, seq++) {
1735 switch (insn_op_type(insn, j)) {
1737 unsigned long idx = nseq - iseq->
iseq + *seq;
1795 unsigned long idx = nseq - iseq->
iseq + pos;
1804 rb_bug(
"unknown operand: %c", insn_op_type(insn, j));
1840 if (
st_lookup(labels_table, pos, &label)) {
1893 iseq1->
self = newiseq;
1895 iseq1->
orig = iseqval;
1907 iseq1->
klass = newcbase;
1919 #define PARAM_TYPE(type) rb_ary_push(a = rb_ary_new2(2), ID2SYM(type))
1920 #define PARAM_ID(i) iseq->local_table[(i)]
1921 #define PARAM(i, type) ( \
1923 rb_id2str(PARAM_ID(i)) ? \
1924 rb_ary_push(a, ID2SYM(PARAM_ID(i))) : \
1930 for (i = 0; i < iseq->
argc; i++) {
1937 for (i = 0; i < iseq->
argc; i++) {
1942 for (; i <
r; i++) {
1990 static const char expr_names[][18] = {
1992 "instance-variable",
2009 if ((
unsigned)(type - 1) >= (
unsigned)
numberof(expr_names))
return 0;
2010 estr = expr_names[type - 1];
2011 if (!estr[0])
return 0;
2012 defs =
GET_VM()->defined_strings;
2015 GET_VM()->defined_strings = defs;
2033 const char **local_table,
2034 const VALUE *arg_opt_table,
2038 const unsigned short first_lineno)
2046 *iseq = *iseq_template;
2052 iseq->
self = iseqval;
2057 iseq->
iseq[
i] = BIN(opt_call_c_function);
2063 #define ALLOC_AND_COPY(dst, src, type, size) do { \
2065 (dst) = ALLOC_N(type, (size)); \
2066 MEMCPY((dst), (src), type, (size)); \
2097 for (pos = 0; cont && pos < iseq->
iseq_size; pos += insn_len(insn)) {
2098 insn = iseq->
iseq[pos];
2100 if (insn == BIN(trace)) {
2110 cont = (*func)(line, &events,
data);
2111 if (current_events != events) {
2113 (
VALUE)(current_events | (events & RUBY_EVENT_SPECIFIED_LINE));
2154 if (data->
pos == 0) {
2200 if (data.
prev == 0) {
#define RB_TYPE_P(obj, type)
#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)
int register char * block
unsigned int rb_iseq_line_no(const rb_iseq_t *iseq, size_t pos)
VALUE rb_get_coverages(void)
VALUE rb_ary_entry(VALUE ary, long offset)
void rb_bug(const char *fmt,...)
#define DECL_SYMBOL(name)
VALUE rb_str_resurrect(VALUE str)
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)
const char * rb_obj_classname(VALUE)
VALUE rb_iseq_line_trace_all(VALUE iseqval)
#define OPT_SPECIALISED_INSTRUCTION
int st_lookup(st_table *, st_data_t, st_data_t *)
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.
#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)
st_table * st_init_numtable(void)
static void set_relation(rb_iseq_t *iseq, const VALUE parent)
SSL_METHOD *(* func)(void)
struct iseq_compile_data * compile_data
struct iseq_compile_data_storage * storage_head
static VALUE iseq_absolute_path(VALUE self)
VALUE rb_str_new_cstr(const char *)
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)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_ary_push(VALUE ary, VALUE item)
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
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)
static VALUE iseq_path(VALUE self)
VALUE rb_str_append(VALUE, VALUE)
VALUE rb_str_concat(VALUE, VALUE)
void rb_hash_foreach(VALUE, int(*)(ANYARGS), VALUE)
unsigned long rb_event_flag_t
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
#define OPT_STACK_CACHING
#define MEMZERO(p, type, n)
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)
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)
static rb_compile_option_t COMPILE_OPTION_DEFAULT
static struct iseq_line_info_entry * get_line_info(const rb_iseq_t *iseq, size_t pos)
memset(y->frac+ix+1, 0,(y->Prec-(ix+1))*sizeof(BDIGIT))
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)
int rb_str_symname_p(VALUE)
static VALUE register_label(struct st_table *table, unsigned long idx)
RUBY_EXTERN VALUE rb_cHash
#define StringValueCStr(v)
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)
VALUE rb_str_cat2(VALUE, const char *)
static VALUE iseq_eval(VALUE self)
void rb_ary_store(VALUE ary, long idx, VALUE val)
#define RUBY_MARK_ENTER(msg)
#define OPT_INLINE_CONST_CACHE
static VALUE exception_type2symbol(VALUE type)
#define INIT_SYMBOL(name)
VALUE rb_hash_aset(VALUE, VALUE, VALUE)
VALUE rb_str_resize(VALUE, long)
#define hidden_obj_p(obj)
#define ISEQ_MINOR_VERSION
VALUE rb_sprintf(const char *format,...)
#define CONST_ID(var, str)
VALUE rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE locals, VALUE args, VALUE exception, VALUE body)
#define OPT_PEEPHOLE_OPTIMIZATION
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_str_inspect(VALUE)
#define OPT_INSTRUCTIONS_UNIFICATION
VALUE rb_file_open_str(VALUE, const char *)
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)
struct parser_params * parser
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
static VALUE cleanup_iseq_build(rb_iseq_t *iseq)
VALUE rb_obj_is_proc(VALUE)
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)
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
static VALUE iseq_alloc(VALUE klass)
#define ISEQ_MAJOR_VERSION
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
static int line_trace_specify(int line, rb_event_flag_t *events_ptr, void *ptr)
if(RB_TYPE_P(r, T_FLOAT))
static NODE * parse_string(VALUE str, const char *file, int line)
#define RUBY_EVENT_SPECIFIED_LINE
#define RUBY_FREE_LEAVE(msg)
#define RUBY_FREE_ENTER(msg)
const char * ruby_node_name(int node)
#define TypedData_Make_Struct(klass, type, data_type, sval)
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 *)
RUBY_EXTERN VALUE rb_cObject
VALUE rb_iseq_compile_on_base(VALUE src, VALUE file, VALUE line, rb_block_t *base_block)
static VALUE iseq_to_a(VALUE self)
#define RARRAY_LENINT(ary)
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 OPT_TRACE_INSTRUCTION
int st_insert(st_table *, st_data_t, st_data_t)
#define OPT_OPERANDS_UNIFICATION
rb_iseq_t * rb_method_get_iseq(VALUE body)
#define OPT_TAILCALL_OPTIMIZATION
VALUE rb_ary_join(VALUE ary, VALUE sep)
VALUE rb_ary_new2(long capa)
VALUE rb_str_new(const char *, long)
static const char * catch_type(int type)
VALUE rb_parser_new(void)
int main(int argc, char **argv)
#define GetISeqPtr(obj, ptr)
const char * rb_id2name(ID id)
static const rb_compile_option_t COMPILE_OPTION_FALSE
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
struct rb_call_info_struct rb_call_info_t
Tcl_QueuePosition position
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_iseq_clone(VALUE iseqval, VALUE newcbase)
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_iseq_disasm(VALUE self)
VALUE rb_iseq_parameters(const rb_iseq_t *iseq, int is_proc)
static VALUE iseq_first_lineno(VALUE self)
void st_free_table(st_table *)
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)