iseq.c: Refactor out rb_iseq_new_ifunc from rb_iseq_new_with_opt

It is too error-prone to pass IMEMO_IFUNC object as NODE*.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61592 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
mame 2018-01-04 07:07:49 +00:00
Родитель 1e4be7a852
Коммит e743a35314
4 изменённых файлов: 57 добавлений и 13 удалений

Просмотреть файл

@ -611,21 +611,31 @@ validate_labels(rb_iseq_t *iseq, st_table *labels_table)
st_free_table(labels_table);
}
VALUE
rb_iseq_compile_ifunc(rb_iseq_t *iseq, const struct vm_ifunc *ifunc)
{
DECL_ANCHOR(ret);
INIT_ANCHOR(ret);
(*ifunc->func)(iseq, ret, ifunc->data);
ADD_INSN(ret, ISEQ_COMPILE_DATA(iseq)->last_line, leave);
return iseq_setup(iseq, ret);
}
VALUE
rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node)
{
DECL_ANCHOR(ret);
INIT_ANCHOR(ret);
VM_ASSERT(!imemo_type_p((VALUE)node, imemo_ifunc));
if (node == 0) {
COMPILE(ret, "nil", node);
iseq_set_local_table(iseq, 0);
}
else if (imemo_type_p((VALUE)node, imemo_ifunc)) {
const struct vm_ifunc *ifunc = (struct vm_ifunc *)node;
/* user callback */
(*ifunc->func)(iseq, ret, ifunc->data);
}
/* assume node is T_NODE */
else if (nd_type(node) == NODE_SCOPE) {
/* iseq type of top, method, class, block */
@ -1227,6 +1237,21 @@ new_child_iseq(rb_iseq_t *iseq, const NODE *const node,
return ret_iseq;
}
static rb_iseq_t *
new_child_iseq_ifunc(rb_iseq_t *iseq, const struct vm_ifunc *ifunc,
VALUE name, const rb_iseq_t *parent, enum iseq_type type, int line_no)
{
rb_iseq_t *ret_iseq;
debugs("[new_child_iseq_ifunc]> ---------------------------------------\n");
ret_iseq = rb_iseq_new_ifunc(ifunc, name,
rb_iseq_path(iseq), rb_iseq_realpath(iseq),
INT2FIX(line_no), parent, type, ISEQ_COMPILE_DATA(iseq)->option);
debugs("[new_child_iseq_ifunc]< ---------------------------------------\n");
iseq_add_mark_object(iseq, (VALUE)ret_iseq);
return ret_iseq;
}
static int
iseq_setup(rb_iseq_t *iseq, LINK_ANCHOR *const anchor)
{
@ -6928,8 +6953,9 @@ iseq_compile_each0(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node, in
* ONCE{ rb_mRubyVMFrozenCore::core#set_postexe{ ... } }
*/
int is_index = iseq->body->is_size++;
const rb_iseq_t *once_iseq = NEW_CHILD_ISEQ((const NODE *)IFUNC_NEW(build_postexe_iseq, node->nd_body, 0),
make_name_for_block(iseq), ISEQ_TYPE_BLOCK, line);
const rb_iseq_t *once_iseq =
new_child_iseq_ifunc(iseq, IFUNC_NEW(build_postexe_iseq, node->nd_body, 0),
rb_fstring(make_name_for_block(iseq)), iseq, ISEQ_TYPE_BLOCK, line);
ADD_INSN2(ret, line, once, once_iseq, INT2FIX(is_index));
@ -7911,9 +7937,9 @@ method_for_self(VALUE name, VALUE arg, rb_insn_func_t func,
acc.arg = arg;
acc.func = func;
acc.line = caller_location(&path, &realpath);
return rb_iseq_new_with_opt((const NODE *)IFUNC_NEW(build, (VALUE)&acc, 0),
rb_sym2str(name), path, realpath,
INT2FIX(acc.line), 0, ISEQ_TYPE_METHOD, 0);
return rb_iseq_new_ifunc(IFUNC_NEW(build, (VALUE)&acc, 0),
rb_sym2str(name), path, realpath,
INT2FIX(acc.line), 0, ISEQ_TYPE_METHOD, 0);
}
static VALUE

21
iseq.c
Просмотреть файл

@ -519,11 +519,9 @@ rb_iseq_new_with_opt(const NODE *node, VALUE name, VALUE path, VALUE realpath,
{
/* TODO: argument check */
rb_iseq_t *iseq = iseq_alloc();
const rb_code_range_t *code_range = NULL;
if (!option) option = &COMPILE_OPTION_DEFAULT;
if (node && !imemo_type_p((VALUE)node, imemo_ifunc)) code_range = &node->nd_loc;
prepare_iseq_build(iseq, name, path, realpath, first_lineno, code_range, parent, type, option);
prepare_iseq_build(iseq, name, path, realpath, first_lineno, node ? &node->nd_loc : NULL, parent, type, option);
rb_iseq_compile_node(iseq, node);
finish_iseq_build(iseq);
@ -531,6 +529,23 @@ rb_iseq_new_with_opt(const NODE *node, VALUE name, VALUE path, VALUE realpath,
return iseq_translate(iseq);
}
rb_iseq_t *
rb_iseq_new_ifunc(const struct vm_ifunc *ifunc, VALUE name, VALUE path, VALUE realpath,
VALUE first_lineno, const rb_iseq_t *parent,
enum iseq_type type, const rb_compile_option_t *option)
{
/* TODO: argument check */
rb_iseq_t *iseq = iseq_alloc();
if (!option) option = &COMPILE_OPTION_DEFAULT;
prepare_iseq_build(iseq, name, path, realpath, first_lineno, NULL, parent, type, option);
rb_iseq_compile_ifunc(iseq, ifunc);
finish_iseq_build(iseq);
return iseq_translate(iseq);
}
const rb_iseq_t *
rb_iseq_load_iseq(VALUE fname)
{

1
iseq.h
Просмотреть файл

@ -165,6 +165,7 @@ RUBY_SYMBOL_EXPORT_BEGIN
/* compile.c */
VALUE rb_iseq_compile_node(rb_iseq_t *iseq, const NODE *node);
VALUE rb_iseq_compile_ifunc(rb_iseq_t *iseq, const struct vm_ifunc *ifunc);
int rb_iseq_translate_threaded_code(rb_iseq_t *iseq);
VALUE *rb_iseq_original_iseq(const rb_iseq_t *iseq);
void rb_iseq_build_from_ary(rb_iseq_t *iseq, VALUE misc,

Просмотреть файл

@ -889,6 +889,8 @@ rb_iseq_t *rb_iseq_new_top (const NODE *node, VALUE name, VALUE path, VALUE
rb_iseq_t *rb_iseq_new_main (const NODE *node, VALUE path, VALUE realpath, const rb_iseq_t *parent);
rb_iseq_t *rb_iseq_new_with_opt(const NODE *node, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
const rb_iseq_t *parent, enum iseq_type, const rb_compile_option_t*);
rb_iseq_t *rb_iseq_new_ifunc(const struct vm_ifunc *ifunc, VALUE name, VALUE path, VALUE realpath, VALUE first_lineno,
const rb_iseq_t *parent, enum iseq_type, const rb_compile_option_t*);
/* src -> iseq */
rb_iseq_t *rb_iseq_compile(VALUE src, VALUE file, VALUE line);