mruby-compiler: convert new_true and new_false to always use variable-sized nodes

Complete the conversion of boolean literal nodes by:

1. Convert new_true to always use variable-sized nodes and inline new_true_var
   directly into the function, eliminating function call overhead
2. Remove obsolete NODE_TRUE case from traditional codegen() and inline
   codegen_true function into gen_true_var for cleaner code
3. Apply the same optimizations to new_false - inline new_false_var and
   remove obsolete NODE_FALSE case and codegen_false function
4. Clean up unused functions and forward declarations

Both true and false literals now always use the variable-sized node path
with direct OP_LOADT/OP_LOADF instruction generation, eliminating
conditional branching and function call overhead.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-09-03 14:33:44 +09:00
parent 4dafdc72fc
commit e19d13f093
3 changed files with 1138 additions and 1184 deletions
+4 -23
View File
@@ -3915,17 +3915,6 @@ codegen_nil(codegen_scope *s, node *tree, int val)
gen_load_op1(s, OP_LOADNIL, val);
}
static void
codegen_true(codegen_scope *s, node *tree, int val)
{
gen_load_op1(s, OP_LOADT, val);
}
static void
codegen_false(codegen_scope *s, node *tree, int val)
{
gen_load_op1(s, OP_LOADF, val);
}
static void
codegen_lvar(codegen_scope *s, mrb_sym sym, int val)
@@ -5568,15 +5557,15 @@ gen_nil_var(codegen_scope *s, node *varnode, int val)
static void
gen_true_var(codegen_scope *s, node *varnode, int val)
{
/* Use traditional true codegen logic */
codegen_true(s, NULL, val);
/* Generate OP_LOADT instruction for true literal */
gen_load_op1(s, OP_LOADT, val);
}
static void
gen_false_var(codegen_scope *s, node *varnode, int val)
{
/* Use traditional false codegen logic */
codegen_false(s, NULL, val);
/* Generate OP_LOADF instruction for false literal */
gen_load_op1(s, OP_LOADF, val);
}
static void
@@ -6655,14 +6644,6 @@ codegen(codegen_scope *s, node *tree, int val)
codegen_nil(s, tree, val);
break;
case NODE_TRUE:
codegen_true(s, tree, val);
break;
case NODE_FALSE:
codegen_false(s, tree, val);
break;
case NODE_ALIAS:
codegen_alias(s, tree, val);
break;
+16 -34
View File
@@ -49,8 +49,6 @@ static int toklen(parser_state *p);
/* Forward declarations for variable-sized simple node functions */
static node* new_self_var(parser_state *p);
static node* new_nil_var(parser_state *p);
static node* new_true_var(parser_state *p);
static node* new_false_var(parser_state *p);
static node* new_const_var(parser_state *p, mrb_sym symbol);
/* Forward declarations for variable-sized advanced node functions */
@@ -558,20 +556,28 @@ new_nil(parser_state *p)
static node*
new_true(parser_state *p)
{
if (p->var_nodes_enabled) {
return new_true_var(p);
}
return list1((node*)NODE_TRUE);
size_t total_size = sizeof(struct mrb_ast_true_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_true_node *n = (struct mrb_ast_true_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_TRUE, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
/* (:false) */
static node*
new_false(parser_state *p)
{
if (p->var_nodes_enabled) {
return new_false_var(p);
}
return list1((node*)NODE_FALSE);
size_t total_size = sizeof(struct mrb_ast_false_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_false_node *n = (struct mrb_ast_false_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_FALSE, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
/* (:alias new old) */
@@ -1263,31 +1269,7 @@ new_nil_var(parser_state *p)
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
static node*
new_true_var(parser_state *p)
{
size_t total_size = sizeof(struct mrb_ast_true_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_true_node *n = (struct mrb_ast_true_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_TRUE, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
static node*
new_false_var(parser_state *p)
{
size_t total_size = sizeof(struct mrb_ast_false_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_false_node *n = (struct mrb_ast_false_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_FALSE, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
static node*
new_const_var(parser_state *p, mrb_sym symbol)
File diff suppressed because it is too large Load Diff