Commit 57a8389a

57a8389a71ebMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · ~3 modified

Description

Fix AOT compiler to detect ivars, regexps, exceptions across class methods and required files

  • Add scan_ivars_deep() to discover @ivar assignments in all method bodies, not just initialize (fixes missing struct members for Parser, State, Context)
  • Add PM_CLASS_NODE/PM_MODULE_NODE cases to has_exception_nodes(), has_split_calls(), and collect_regexp_patterns() so they descend into classes
  • Extend collect_regexp_patterns() with PM_CASE_NODE, PM_WHEN_NODE, PM_BEGIN_NODE, PM_RESCUE_NODE, PM_ENSURE_NODE, PM_UNTIL_NODE, PM_UNLESS_NODE, PM_RETURN_NODE, and other node types for thorough regex discovery
  • Scan all required files for exceptions, splits, and regexps in codegen_program
  • Add infer_type() cases for PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE and PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE so string += generates sp_str_concat
  • Handle string += on instance variables in stmt.c

Reduces lrama compile errors from 310 to 158 (~49%). All 60 tests pass.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
57a8389a71eb
Parent
4a5fcd6409f1

src/codegen.cmodifiedView file

@@ -428,6 +428,112 @@
428428 }
429429 }
430430
431+/* Recursively scan an AST subtree for @ivar write nodes and register them.
432+ * This catches ivars assigned in non-initialize methods (e.g., Racc runtime). */
433+static void scan_ivars_deep(codegen_ctx_t *ctx, class_info_t *cls, pm_node_t *node) {
434+ if (!node) return;
435+ switch (PM_NODE_TYPE(node)) {
436+ case PM_INSTANCE_VARIABLE_WRITE_NODE: {
437+ pm_instance_variable_write_node_t *iw =
438+ (pm_instance_variable_write_node_t *)node;
439+ char *ivname = cstr(ctx, iw->name);
440+ if (!find_ivar(cls, ivname + 1) && cls->ivar_count < MAX_IVARS) {
441+ ivar_info_t *iv = &cls->ivars[cls->ivar_count++];
442+ snprintf(iv->name, sizeof(iv->name), "%s", escape_c_keyword(ivname + 1));
443+ iv->type = vt_prim(SPINEL_TYPE_VALUE);
444+ }
445+ free(ivname);
446+ break;
447+ }
448+ case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
449+ pm_instance_variable_operator_write_node_t *iw =
450+ (pm_instance_variable_operator_write_node_t *)node;
451+ char *ivname = cstr(ctx, iw->name);
452+ if (!find_ivar(cls, ivname + 1) && cls->ivar_count < MAX_IVARS) {
453+ ivar_info_t *iv = &cls->ivars[cls->ivar_count++];
454+ snprintf(iv->name, sizeof(iv->name), "%s", escape_c_keyword(ivname + 1));
455+ iv->type = vt_prim(SPINEL_TYPE_VALUE);
456+ }
457+ free(ivname);
458+ break;
459+ }
460+ case PM_STATEMENTS_NODE: {
461+ pm_statements_node_t *s = (pm_statements_node_t *)node;
462+ for (size_t i = 0; i < s->body.size; i++)
463+ scan_ivars_deep(ctx, cls, s->body.nodes[i]);
464+ break;
465+ }
466+ case PM_IF_NODE: {
467+ pm_if_node_t *n = (pm_if_node_t *)node;
468+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
469+ if (n->subsequent) scan_ivars_deep(ctx, cls, (pm_node_t *)n->subsequent);
470+ break;
471+ }
472+ case PM_ELSE_NODE: {
473+ pm_else_node_t *n = (pm_else_node_t *)node;
474+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
475+ break;
476+ }
477+ case PM_WHILE_NODE: {
478+ pm_while_node_t *n = (pm_while_node_t *)node;
479+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
480+ break;
481+ }
482+ case PM_UNTIL_NODE: {
483+ pm_until_node_t *n = (pm_until_node_t *)node;
484+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
485+ break;
486+ }
487+ case PM_CASE_NODE: {
488+ pm_case_node_t *n = (pm_case_node_t *)node;
489+ for (size_t i = 0; i < n->conditions.size; i++)
490+ scan_ivars_deep(ctx, cls, n->conditions.nodes[i]);
491+ if (n->else_clause) scan_ivars_deep(ctx, cls, (pm_node_t *)n->else_clause);
492+ break;
493+ }
494+ case PM_WHEN_NODE: {
495+ pm_when_node_t *n = (pm_when_node_t *)node;
496+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
497+ break;
498+ }
499+ case PM_BEGIN_NODE: {
500+ pm_begin_node_t *n = (pm_begin_node_t *)node;
501+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
502+ if (n->rescue_clause) scan_ivars_deep(ctx, cls, (pm_node_t *)n->rescue_clause);
503+ if (n->ensure_clause) scan_ivars_deep(ctx, cls, (pm_node_t *)n->ensure_clause);
504+ break;
505+ }
506+ case PM_RESCUE_NODE: {
507+ pm_rescue_node_t *n = (pm_rescue_node_t *)node;
508+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
509+ if (n->subsequent) scan_ivars_deep(ctx, cls, (pm_node_t *)n->subsequent);
510+ break;
511+ }
512+ case PM_ENSURE_NODE: {
513+ pm_ensure_node_t *n = (pm_ensure_node_t *)node;
514+ if (n->statements) scan_ivars_deep(ctx, cls, (pm_node_t *)n->statements);
515+ break;
516+ }
517+ case PM_BLOCK_NODE: {
518+ pm_block_node_t *b = (pm_block_node_t *)node;
519+ if (b->body) scan_ivars_deep(ctx, cls, (pm_node_t *)b->body);
520+ break;
521+ }
522+ case PM_CALL_NODE: {
523+ pm_call_node_t *c = (pm_call_node_t *)node;
524+ if (c->block) scan_ivars_deep(ctx, cls, (pm_node_t *)c->block);
525+ break;
526+ }
527+ case PM_LOCAL_VARIABLE_WRITE_NODE: {
528+ pm_local_variable_write_node_t *n = (pm_local_variable_write_node_t *)node;
529+ scan_ivars_deep(ctx, cls, n->value);
530+ break;
531+ }
532+ default:
533+ break;
534+ }
535+}
536+
431537 static void analyze_module(codegen_ctx_t *ctx, pm_module_node_t *node);
432538
433539 static void analyze_class(codegen_ctx_t *ctx, pm_class_node_t *node) {
@@ -717,6 +823,13 @@
717823 }
718824 }
719825
826+ /* Scan ALL method bodies for @ivar assignments (catches ivars set outside
827+ * initialize, e.g., Racc runtime's _racc_init_sysvars). */
828+ for (int mi = 0; mi < cls->method_count; mi++) {
829+ if (cls->methods[mi].body_node)
830+ scan_ivars_deep(ctx, cls, cls->methods[mi].body_node);
831+ }
832+
720833 /* Heuristic: classes with only float/int ivars and <= 4 fields are value types */
721834 cls->is_value_type = (cls->ivar_count <= 4 && cls->ivar_count > 0);
722835 }
@@ -1434,6 +1547,14 @@
14341547 pm_while_node_t *n = (pm_while_node_t *)node;
14351548 return n->statements ? has_exception_nodes(ctx, (pm_node_t *)n->statements) : false;
14361549 }
1550+ case PM_CLASS_NODE: {
1551+ pm_class_node_t *cn = (pm_class_node_t *)node;
1552+ return cn->body ? has_exception_nodes(ctx, (pm_node_t *)cn->body) : false;
1553+ }
1554+ case PM_MODULE_NODE: {
1555+ pm_module_node_t *mn = (pm_module_node_t *)node;
1556+ return mn->body ? has_exception_nodes(ctx, (pm_node_t *)mn->body) : false;
1557+ }
14371558 default:
14381559 return false;
14391560 }
@@ -1509,6 +1630,14 @@
15091630 }
15101631 return false;
15111632 }
1633+ case PM_CLASS_NODE: {
1634+ pm_class_node_t *cn = (pm_class_node_t *)node;
1635+ return cn->body ? has_split_calls(ctx, (pm_node_t *)cn->body) : false;
1636+ }
1637+ case PM_MODULE_NODE: {
1638+ pm_module_node_t *mn = (pm_module_node_t *)node;
1639+ return mn->body ? has_split_calls(ctx, (pm_node_t *)mn->body) : false;
1640+ }
15121641 default:
15131642 return false;
15141643 }
@@ -1597,6 +1726,87 @@
15971726 ctx->needs_regexp = true;
15981727 break;
15991728 }
1729+ case PM_CLASS_NODE: {
1730+ pm_class_node_t *cn = (pm_class_node_t *)node;
1731+ if (cn->body) collect_regexp_patterns(ctx, (pm_node_t *)cn->body);
1732+ break;
1733+ }
1734+ case PM_MODULE_NODE: {
1735+ pm_module_node_t *mn = (pm_module_node_t *)node;
1736+ if (mn->body) collect_regexp_patterns(ctx, (pm_node_t *)mn->body);
1737+ break;
1738+ }
1739+ case PM_CASE_NODE: {
1740+ pm_case_node_t *cn = (pm_case_node_t *)node;
1741+ if (cn->predicate) collect_regexp_patterns(ctx, cn->predicate);
1742+ for (size_t i = 0; i < cn->conditions.size; i++)
1743+ collect_regexp_patterns(ctx, cn->conditions.nodes[i]);
1744+ if (cn->else_clause) collect_regexp_patterns(ctx, (pm_node_t *)cn->else_clause);
1745+ break;
1746+ }
1747+ case PM_WHEN_NODE: {
1748+ pm_when_node_t *wn = (pm_when_node_t *)node;
1749+ for (size_t i = 0; i < wn->conditions.size; i++)
1750+ collect_regexp_patterns(ctx, wn->conditions.nodes[i]);
1751+ if (wn->statements) collect_regexp_patterns(ctx, (pm_node_t *)wn->statements);
1752+ break;
1753+ }
1754+ case PM_UNTIL_NODE: {
1755+ pm_until_node_t *n = (pm_until_node_t *)node;
1756+ if (n->predicate) collect_regexp_patterns(ctx, n->predicate);
1757+ if (n->statements) collect_regexp_patterns(ctx, (pm_node_t *)n->statements);
1758+ break;
1759+ }
1760+ case PM_UNLESS_NODE: {
1761+ pm_unless_node_t *n = (pm_unless_node_t *)node;
1762+ if (n->predicate) collect_regexp_patterns(ctx, n->predicate);
1763+ if (n->statements) collect_regexp_patterns(ctx, (pm_node_t *)n->statements);
1764+ if (n->else_clause) collect_regexp_patterns(ctx, (pm_node_t *)n->else_clause);
1765+ break;
1766+ }
1767+ case PM_BEGIN_NODE: {
1768+ pm_begin_node_t *n = (pm_begin_node_t *)node;
1769+ if (n->statements) collect_regexp_patterns(ctx, (pm_node_t *)n->statements);
1770+ if (n->rescue_clause) collect_regexp_patterns(ctx, (pm_node_t *)n->rescue_clause);
1771+ if (n->ensure_clause) collect_regexp_patterns(ctx, (pm_node_t *)n->ensure_clause);
1772+ break;
1773+ }
1774+ case PM_RESCUE_NODE: {
1775+ pm_rescue_node_t *n = (pm_rescue_node_t *)node;
1776+ if (n->statements) collect_regexp_patterns(ctx, (pm_node_t *)n->statements);
1777+ if (n->subsequent) collect_regexp_patterns(ctx, (pm_node_t *)n->subsequent);
1778+ break;
1779+ }
1780+ case PM_ENSURE_NODE: {
1781+ pm_ensure_node_t *n = (pm_ensure_node_t *)node;
1782+ if (n->statements) collect_regexp_patterns(ctx, (pm_node_t *)n->statements);
1783+ break;
1784+ }
1785+ case PM_CONSTANT_WRITE_NODE: {
1786+ pm_constant_write_node_t *n = (pm_constant_write_node_t *)node;
1787+ collect_regexp_patterns(ctx, n->value);
1788+ break;
1789+ }
1790+ case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
1791+ pm_local_variable_operator_write_node_t *n =
1792+ (pm_local_variable_operator_write_node_t *)node;
1793+ collect_regexp_patterns(ctx, n->value);
1794+ break;
1795+ }
1796+ case PM_INSTANCE_VARIABLE_WRITE_NODE: {
1797+ pm_instance_variable_write_node_t *n =
1798+ (pm_instance_variable_write_node_t *)node;
1799+ collect_regexp_patterns(ctx, n->value);
1800+ break;
1801+ }
1802+ case PM_RETURN_NODE: {
1803+ pm_return_node_t *n = (pm_return_node_t *)node;
1804+ if (n->arguments) {
1805+ for (size_t i = 0; i < n->arguments->arguments.size; i++)
1806+ collect_regexp_patterns(ctx, n->arguments->arguments.nodes[i]);
1807+ }
1808+ break;
1809+ }
16001810 default:
16011811 break;
16021812 }
@@ -1926,6 +2136,15 @@
19262136 /* Detect regexp usage and collect patterns (must be before emit_header) */
19272137 collect_regexp_patterns(ctx, root);
19282138
2139+ /* Also scan all required files for exceptions, splits, and regexps */
2140+ for (int ri = 0; ri < ctx->required_file_count; ri++) {
2141+ pm_node_t *rroot = (pm_node_t *)ctx->required_files[ri].root;
2142+ if (!rroot) continue;
2143+ if (!ctx->needs_exc) ctx->needs_exc = has_exception_nodes(ctx, rroot);
2144+ if (!ctx->needs_str_split) ctx->needs_str_split = has_split_calls(ctx, rroot);
2145+ collect_regexp_patterns(ctx, rroot);
2146+ }
2147+
19292148 /* Pass 1: Analyze classes, modules, functions */
19302149 class_analysis_pass(ctx, root);
19312150