Use line numbers as builtin-index

The order of iseq may differ from the order of tokens, typically
`while`/`until` conditions are put after the body.

These orders can match by using line numbers as builtin-indexes, but
at the same time, it introduces the restriction that multiple `cexpr!`
and `cstmt!` cannot appear in the same line.

Another possible idea is to use `RubyVM::AbstractSyntaxTree` and
`node_id` instead of ripper, with making BASERUBY 3.1 or later.
This commit is contained in:
Nobuyoshi Nakada 2024-01-17 16:45:57 +09:00
parent 15f6ee057d
commit 127b19ab56
5 changed files with 3 additions and 10 deletions

View File

@ -46,7 +46,6 @@ rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin
rb_vm_t *vm = GET_VM(); rb_vm_t *vm = GET_VM();
if (vm->builtin_function_table != NULL) rb_bug("vm->builtin_function_table should be NULL."); if (vm->builtin_function_table != NULL) rb_bug("vm->builtin_function_table should be NULL.");
vm->builtin_function_table = table; vm->builtin_function_table = table;
vm->builtin_inline_index = 0;
const rb_iseq_t *iseq = rb_iseq_ibf_load_bytes((const char *)bin, size); const rb_iseq_t *iseq = rb_iseq_ibf_load_bytes((const char *)bin, size);
ASSUME(iseq); // otherwise an exception should have raised ASSUME(iseq); // otherwise an exception should have raised
vm->builtin_function_table = NULL; vm->builtin_function_table = NULL;

View File

@ -8787,7 +8787,6 @@ compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NOD
} }
else if (strcmp("cinit!", builtin_func) == 0) { else if (strcmp("cinit!", builtin_func) == 0) {
// ignore // ignore
GET_VM()->builtin_inline_index++;
return COMPILE_OK; return COMPILE_OK;
} }
else if (strcmp("attr!", builtin_func) == 0) { else if (strcmp("attr!", builtin_func) == 0) {
@ -8815,10 +8814,7 @@ compile_builtin_function_call(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NOD
return COMPILE_NG; return COMPILE_NG;
} }
if (GET_VM()->builtin_inline_index == INT_MAX) { int inline_index = nd_line(node);
rb_bug("builtin inline function index overflow:%s", builtin_func);
}
int inline_index = GET_VM()->builtin_inline_index++;
snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index); snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
builtin_func = inline_func; builtin_func = inline_func;
args_node = NULL; args_node = NULL;

View File

@ -27,7 +27,6 @@ builtin_iseq_load(const char *feature_name, const struct rb_builtin_function *ta
feature_name); feature_name);
} }
vm->builtin_function_table = table; vm->builtin_function_table = table;
vm->builtin_inline_index = 0;
static const rb_compile_option_t optimization = { static const rb_compile_option_t optimization = {
TRUE, /* unsigned int inline_const_cache; */ TRUE, /* unsigned int inline_const_cache; */
TRUE, /* unsigned int peephole_optimization; */ TRUE, /* unsigned int peephole_optimization; */

View File

@ -166,7 +166,7 @@ def collect_builtin base, tree, name, bs, inlines, locals = nil
when 'cstmt' when 'cstmt'
text = inline_text argc, args.first text = inline_text argc, args.first
func_name = "_bi#{inlines.size}" func_name = "_bi#{lineno}"
cfunc_name = make_cfunc_name(inlines, name, lineno) cfunc_name = make_cfunc_name(inlines, name, lineno)
inlines[cfunc_name] = [lineno, text, locals, func_name] inlines[cfunc_name] = [lineno, text, locals, func_name]
argc -= 1 argc -= 1
@ -174,7 +174,7 @@ def collect_builtin base, tree, name, bs, inlines, locals = nil
text = inline_text argc, args.first text = inline_text argc, args.first
code = "return #{text};" code = "return #{text};"
func_name = "_bi#{inlines.size}" func_name = "_bi#{lineno}"
cfunc_name = make_cfunc_name(inlines, name, lineno) cfunc_name = make_cfunc_name(inlines, name, lineno)
locals = [] if $1 == 'cconst' locals = [] if $1 == 'cconst'

View File

@ -747,7 +747,6 @@ typedef struct rb_vm_struct {
st_table *frozen_strings; st_table *frozen_strings;
const struct rb_builtin_function *builtin_function_table; const struct rb_builtin_function *builtin_function_table;
int builtin_inline_index;
struct rb_id_table *negative_cme_table; struct rb_id_table *negative_cme_table;
st_table *overloaded_cme_table; // cme -> overloaded_cme st_table *overloaded_cme_table; // cme -> overloaded_cme