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
Родитель 15f6ee057d
Коммит 127b19ab56
5 изменённых файлов: 3 добавлений и 10 удалений

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

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

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

@ -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) {
// ignore
GET_VM()->builtin_inline_index++;
return COMPILE_OK;
}
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;
}
if (GET_VM()->builtin_inline_index == INT_MAX) {
rb_bug("builtin inline function index overflow:%s", builtin_func);
}
int inline_index = GET_VM()->builtin_inline_index++;
int inline_index = nd_line(node);
snprintf(inline_func, sizeof(inline_func), BUILTIN_INLINE_PREFIX "%d", inline_index);
builtin_func = inline_func;
args_node = NULL;

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

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

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

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

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

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