mruby-compiler: convert new_nil and new_self to always use variable-sized nodes

Remove conditional var_nodes_enabled logic from new_nil and new_self functions.
These functions now directly create variable-sized AST nodes using proper
size classes and memory allocation. Remove helper functions new_nil_var and
new_self_var as they are no longer needed.

Also update codegen to handle the new variable-sized node structure:
- Add NODE_VARIABLE handling to gen_assignment function
- Fix self-method call detection in call generation
- Update assignment generation to properly handle variable-sized nil nodes

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-09-04 11:44:18 +09:00
parent b5a4b3a7a7
commit 4931570f0c
3 changed files with 1244 additions and 1256 deletions
+26 -2
View File
@@ -2818,7 +2818,9 @@ gen_call(codegen_scope *s, node *tree, int val, int safe)
else if (sym == MRB_OPSYM_2(s->mrb, aref)) opt_op = OP_GETIDX;
else if (sym == MRB_OPSYM_2(s->mrb, aset)) opt_op = OP_SETIDX;
}
if (!tree->car || (opt_op == OP_NOP && node_to_int(tree->car->car) == NODE_SELF)) {
if (!tree->car || (opt_op == OP_NOP &&
node_to_int(tree->car->car) == NODE_VARIABLE &&
VAR_NODE_TYPE(tree->car->cdr) == NODE_SELF)) {
noself = 1;
push();
}
@@ -2937,6 +2939,28 @@ gen_assignment(codegen_scope *s, node *tree, node *rhs, int sp, int val)
codegen_error(s, "Can't assign to numbered parameter");
break;
case NODE_VARIABLE:
/* Handle variable-sized nodes completely here */
{
enum node_type var_type = VAR_NODE_TYPE(tree->cdr);
switch (var_type) {
case NODE_NIL:
if (rhs) {
codegen(s, rhs, VAL);
pop();
sp = cursp();
}
/* NODE_NIL assignment is complete - just break (splat without assignment) */
break;
default:
codegen_error(s, "unsupported variable-sized lhs");
break;
}
/* Variable-sized node handling is complete - skip second switch */
if (val) push();
return;
}
default:
codegen_error(s, "unknown lhs");
break;
@@ -3092,6 +3116,7 @@ gen_assignment(codegen_scope *s, node *tree, node *rhs, int sp, int val)
case NODE_NIL:
break;
default:
codegen_error(s, "unknown lhs");
break;
@@ -6501,7 +6526,6 @@ codegen(codegen_scope *s, node *tree, int val)
codegen_nil(s, tree, val);
break;
case NODE_UNDEF:
codegen_undef(s, tree, val);
break;
+16 -34
View File
@@ -47,8 +47,6 @@ static const char* tok(parser_state *p);
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_const_var(parser_state *p, mrb_sym symbol);
/* Forward declarations for variable-sized advanced node functions */
@@ -546,10 +544,14 @@ new_ensure(parser_state *p, node *a, node *b)
static node*
new_nil(parser_state *p)
{
if (p->var_nodes_enabled) {
return new_nil_var(p);
}
return list1((node*)NODE_NIL);
size_t total_size = sizeof(struct mrb_ast_nil_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_nil_node *n = (struct mrb_ast_nil_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_NIL, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
/* (:true) */
@@ -729,10 +731,14 @@ new_postexe(parser_state *p, node *a)
static node*
new_self(parser_state *p)
{
if (p->var_nodes_enabled) {
return new_self_var(p);
}
return list1((node*)NODE_SELF);
size_t total_size = sizeof(struct mrb_ast_self_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_self_node *n = (struct mrb_ast_self_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_SELF, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
/* (:call a b c) */
@@ -1119,31 +1125,7 @@ new_op_asgn_var(parser_state *p, node *lhs, mrb_sym op, node *rhs)
/* Variable-sized expression node creation */
/* Variable-sized simple node creation functions */
static node*
new_self_var(parser_state *p)
{
size_t total_size = sizeof(struct mrb_ast_self_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_self_node *n = (struct mrb_ast_self_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_SELF, class);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
static node*
new_nil_var(parser_state *p)
{
size_t total_size = sizeof(struct mrb_ast_nil_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_nil_node *n = (struct mrb_ast_nil_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_NIL, 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