mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mruby-compiler: add variable-sized simple node support
add variable-sized node structures for simple nodes (self, nil, true, false, const) with conditional usage based on var_nodes_enabled. singleton nodes use only 8-byte header for maximum memory efficiency. includes proper forward declarations, casting macros, creation functions, and codegen support maintaining compatibility with existing functions. Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -5492,6 +5492,45 @@ gen_float_var(codegen_scope *s, node *varnode, int val)
|
||||
codegen_float(s, (node*)value, val);
|
||||
}
|
||||
|
||||
/* Variable-sized simple node generation functions */
|
||||
static void
|
||||
gen_self_var(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
/* Use traditional self codegen logic */
|
||||
codegen_self(s, NULL, val);
|
||||
}
|
||||
|
||||
static void
|
||||
gen_nil_var(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
/* Use traditional nil codegen logic */
|
||||
codegen_nil(s, NULL, val);
|
||||
}
|
||||
|
||||
static void
|
||||
gen_true_var(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
/* Use traditional true codegen logic */
|
||||
codegen_true(s, NULL, val);
|
||||
}
|
||||
|
||||
static void
|
||||
gen_false_var(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
/* Use traditional false codegen logic */
|
||||
codegen_false(s, NULL, val);
|
||||
}
|
||||
|
||||
static void
|
||||
gen_const_var(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
struct mrb_ast_const_node *const_n = const_node(varnode->car);
|
||||
mrb_sym symbol = CONST_NODE_SYMBOL(const_n);
|
||||
|
||||
/* Use traditional const codegen logic */
|
||||
codegen_const(s, symbol, val);
|
||||
}
|
||||
|
||||
static mrb_bool
|
||||
codegen_variable_node(codegen_scope *s, node *varnode, int val)
|
||||
{
|
||||
@@ -5633,6 +5672,26 @@ codegen_variable_node(codegen_scope *s, node *varnode, int val)
|
||||
gen_float_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
case NODE_SELF:
|
||||
gen_self_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
case NODE_NIL:
|
||||
gen_nil_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
case NODE_TRUE:
|
||||
gen_true_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
case NODE_FALSE:
|
||||
gen_false_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
case NODE_CONST:
|
||||
gen_const_var(s, varnode, val);
|
||||
return TRUE;
|
||||
|
||||
default:
|
||||
return FALSE; /* Not handled, fall through to main codegen */
|
||||
}
|
||||
|
||||
@@ -569,4 +569,36 @@ struct mrb_ast_float_node {
|
||||
|
||||
#define FLOAT_NODE_VALUE(n) (float_node(n)->value)
|
||||
|
||||
/* Variable-sized simple node structures */
|
||||
struct mrb_ast_self_node {
|
||||
struct mrb_ast_var_header hdr;
|
||||
};
|
||||
|
||||
struct mrb_ast_nil_node {
|
||||
struct mrb_ast_var_header hdr;
|
||||
};
|
||||
|
||||
struct mrb_ast_true_node {
|
||||
struct mrb_ast_var_header hdr;
|
||||
};
|
||||
|
||||
struct mrb_ast_false_node {
|
||||
struct mrb_ast_var_header hdr;
|
||||
};
|
||||
|
||||
struct mrb_ast_const_node {
|
||||
struct mrb_ast_var_header hdr;
|
||||
mrb_sym symbol;
|
||||
};
|
||||
|
||||
/* Simple node casting macros */
|
||||
#define self_node(n) ((struct mrb_ast_self_node*)(n))
|
||||
#define nil_node(n) ((struct mrb_ast_nil_node*)(n))
|
||||
#define true_node(n) ((struct mrb_ast_true_node*)(n))
|
||||
#define false_node(n) ((struct mrb_ast_false_node*)(n))
|
||||
#define const_node(n) ((struct mrb_ast_const_node*)(n))
|
||||
|
||||
/* Simple node value access macros */
|
||||
#define CONST_NODE_SYMBOL(n) (const_node(n)->symbol)
|
||||
|
||||
#endif /* MRUBY_COMPILER_NODE_H */
|
||||
|
||||
@@ -43,6 +43,13 @@ static void backref_error(parser_state *p, node *n);
|
||||
static void void_expr_error(parser_state *p, node *n);
|
||||
static void tokadd(parser_state *p, int32_t c);
|
||||
|
||||
/* 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);
|
||||
|
||||
#define identchar(c) (ISALNUM(c) || (c) == '_' || !ISASCII(c))
|
||||
|
||||
typedef unsigned int stack_type;
|
||||
@@ -501,6 +508,9 @@ 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);
|
||||
}
|
||||
|
||||
@@ -508,6 +518,9 @@ 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);
|
||||
}
|
||||
|
||||
@@ -515,6 +528,9 @@ new_true(parser_state *p)
|
||||
static node*
|
||||
new_false(parser_state *p)
|
||||
{
|
||||
if (p->var_nodes_enabled) {
|
||||
return new_false_var(p);
|
||||
}
|
||||
return list1((node*)NODE_FALSE);
|
||||
}
|
||||
|
||||
@@ -654,6 +670,9 @@ 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);
|
||||
}
|
||||
|
||||
@@ -1213,6 +1232,78 @@ new_float_var(parser_state *p, const char *value)
|
||||
return cons_head((node*)NODE_VARIABLE, (node*)n);
|
||||
}
|
||||
|
||||
/* 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_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)
|
||||
{
|
||||
size_t total_size = sizeof(struct mrb_ast_const_node);
|
||||
enum mrb_ast_size_class class = size_to_class(total_size);
|
||||
|
||||
struct mrb_ast_const_node *n = (struct mrb_ast_const_node*)
|
||||
parser_alloc_var(p, total_size, class);
|
||||
|
||||
init_var_header(&n->hdr, p, NODE_CONST, class);
|
||||
n->symbol = symbol;
|
||||
|
||||
return cons_head((node*)NODE_VARIABLE, (node*)n);
|
||||
}
|
||||
|
||||
/* (:fcall self mid args) */
|
||||
static node*
|
||||
new_fcall(parser_state *p, mrb_sym b, node *c)
|
||||
@@ -1517,6 +1608,9 @@ new_nvar(parser_state *p, int num)
|
||||
static node*
|
||||
new_const(parser_state *p, mrb_sym sym)
|
||||
{
|
||||
if (p->var_nodes_enabled) {
|
||||
return new_const_var(p, sym);
|
||||
}
|
||||
return cons_head((node*)NODE_CONST, sym_to_node(sym));
|
||||
}
|
||||
|
||||
|
||||
+1462
-1018
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user