mruby-compiler: convert new_float to always use variable-sized nodes

Updated new_float() to always create variable-sized nodes and removed
the conditional logic. Also updated codegen_negate() to handle
NODE_VARIABLE wrapper containing NODE_FLOAT for negative float literals.

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-09-04 09:46:14 +09:00
parent 47857cea30
commit e0e3c14a69
3 changed files with 1105 additions and 1136 deletions
+25 -26
View File
@@ -3915,7 +3915,6 @@ codegen_nil(codegen_scope *s, node *tree, int val)
gen_load_op1(s, OP_LOADNIL, val);
}
static void
codegen_lvar(codegen_scope *s, mrb_sym sym, int val)
{
@@ -4206,6 +4205,24 @@ codegen_negate(codegen_scope *s, node *tree, int val)
}
break;
case NODE_VARIABLE:
#ifndef MRB_NO_FLOAT
if (VAR_NODE_TYPE(tree->cdr) == NODE_FLOAT) {
if (val) {
struct mrb_ast_float_node *float_n = (struct mrb_ast_float_node*)tree->cdr;
const char *value = float_n->value;
double f;
mrb_read_float(value, NULL, &f);
int off = new_lit_float(s, (mrb_float)-f);
gen_load_lit(s, off);
}
#endif
break;
}
/* fall through for other variable node types */
default:
codegen(s, tree, VAL);
pop();
@@ -4219,17 +4236,6 @@ codegen_negate(codegen_scope *s, node *tree, int val)
}
}
static void
codegen_float(codegen_scope *s, node *tree, int val)
{
char *p = (char*)tree;
double f;
mrb_read_float(p, NULL, &f);
int off = new_lit_float(s, (mrb_float)f);
gen_load_op2(s, OP_LOADL, off, val);
}
static void
codegen_int(codegen_scope *s, node *tree, int val)
{
@@ -4250,7 +4256,6 @@ codegen_int(codegen_scope *s, node *tree, int val)
push();
}
static void
codegen_xstr(codegen_scope *s, node *tree, int val)
{
@@ -5423,11 +5428,14 @@ gen_dot3_var(codegen_scope *s, node *varnode, int val)
static void
gen_float_var(codegen_scope *s, node *varnode, int val)
{
struct mrb_ast_float_node *float_n = float_node(varnode->car);
const char *value = FLOAT_NODE_VALUE(float_n);
struct mrb_ast_float_node *float_n = (struct mrb_ast_float_node*)varnode;
const char *value = float_n->value;
double f;
/* Use traditional float codegen logic directly */
codegen_float(s, (node*)value, val);
mrb_read_float(value, NULL, &f);
int off = new_lit_float(s, (mrb_float)f);
gen_load_op2(s, OP_LOADL, off, val);
}
/* Variable-sized simple node generation functions */
@@ -5570,7 +5578,6 @@ gen_until_mod_var(codegen_scope *s, node *varnode, int val)
codegen_while_until(s, (node*)&temp_tree, val, NODE_UNTIL_MOD);
}
static void
gen_xstr_var(codegen_scope *s, node *varnode, int val)
{
@@ -6518,22 +6525,14 @@ codegen(codegen_scope *s, node *tree, int val)
codegen_int(s, tree, val);
break;
#ifndef MRB_NO_FLOAT
case NODE_FLOAT:
codegen_float(s, tree, val);
break;
#endif
case NODE_NEGATE:
codegen_negate(s, tree, val);
break;
case NODE_STR:
codegen_cons_list_string(s, tree, val);
break;
case NODE_XSTR:
codegen_xstr(s, tree, val);
break;
+10 -25
View File
@@ -610,7 +610,6 @@ static node* new_masgn_var(parser_state *p, node *lhs, node *rhs);
static node* new_op_asgn_var(parser_state *p, node *lhs, mrb_sym op, node *rhs);
static node* new_and_var(parser_state *p, node *left, node *right);
static node* new_or_var(parser_state *p, node *left, node *right);
static node* new_float_var(parser_state *p, const char *value);
/* (:if cond then else) */
static node*
@@ -1155,19 +1154,6 @@ new_or_var(parser_state *p, node *left, node *right)
/* Variable-sized literal node creation functions */
static node*
new_float_var(parser_state *p, const char *value)
{
size_t total_size = sizeof(struct mrb_ast_float_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_float_node *n = (struct mrb_ast_float_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_FLOAT, class);
n->value = strdup(value);
return cons_head((node*)NODE_VARIABLE, (node*)n);
}
/* Variable-sized simple node creation functions */
static node*
@@ -2058,13 +2044,16 @@ new_int(parser_state *p, const char *s, int base, int suffix)
static node*
new_float(parser_state *p, const char *s, int suffix)
{
node* result;
if (p->var_nodes_enabled) {
result = new_float_var(p, s);
}
else {
result = cons((node*)NODE_FLOAT, (node*)strdup(s));
}
size_t total_size = sizeof(struct mrb_ast_float_node);
enum mrb_ast_size_class class = size_to_class(total_size);
struct mrb_ast_float_node *n = (struct mrb_ast_float_node*)parser_alloc_var(p, total_size, class);
init_var_header(&n->hdr, p, NODE_FLOAT, class);
n->value = strdup(s);
node* result = cons_head((node*)NODE_VARIABLE, (node*)n);
if (suffix & NUM_SUFFIX_R) {
result = new_rational(p, result);
}
@@ -2075,9 +2064,6 @@ new_float(parser_state *p, const char *s, int suffix)
}
#endif
/* Create string node from cons list */
/* (:str . a) */
static node*
@@ -2349,7 +2335,6 @@ new_negate(parser_state *p, node *n)
return cons_head((node*)NODE_VARIABLE, (node*)negate_node);
}
static node*
cond(node *n)
{
File diff suppressed because it is too large Load Diff