From 127b19ab561b5365884b465d50356a1e4019713c Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Wed, 17 Jan 2024 16:45:57 +0900 Subject: [PATCH] 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. --- builtin.c | 1 - compile.c | 6 +----- mini_builtin.c | 1 - tool/mk_builtin_loader.rb | 4 ++-- vm_core.h | 1 - 5 files changed, 3 insertions(+), 10 deletions(-) diff --git a/builtin.c b/builtin.c index 4fc2ab43d1..fbc11bf1b4 100644 --- a/builtin.c +++ b/builtin.c @@ -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; diff --git a/compile.c b/compile.c index 8d3588c81b..b15c04a37a 100644 --- a/compile.c +++ b/compile.c @@ -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; diff --git a/mini_builtin.c b/mini_builtin.c index 457327ee06..8371073f28 100644 --- a/mini_builtin.c +++ b/mini_builtin.c @@ -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; */ diff --git a/tool/mk_builtin_loader.rb b/tool/mk_builtin_loader.rb index 8cfae13802..871ac87006 100644 --- a/tool/mk_builtin_loader.rb +++ b/tool/mk_builtin_loader.rb @@ -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' diff --git a/vm_core.h b/vm_core.h index 743071ac85..881e1beaac 100644 --- a/vm_core.h +++ b/vm_core.h @@ -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