Commit dbcaa072

dbcaa07220c3Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

fix(analyze,codegen): Regexp.new() folds into static pattern table

Regexp.new("lit") / Regexp.compile("lit") with a string-literal argument now folds into the same static @regexp_patterns table as a /lit/ regex literal. The LV/const holding the value lowers to a plain mrb_int (placeholder); .match? / .match / =~ resolve at the call site through regex_pat_c_expr, dispatching to the pre-compiled sp_re_pat_<i> global.

Before this fix, infer_constructor_type returned obj_Regexp for Regexp.new, which lowered to sp_Regexp *lv_x = NULL; -- a type the runtime doesn't declare, so the generated C failed to compile.

Three new arms:

  • regexp_new_literal_pattern(nid) (mirrored in both files): returns the literal source string for Regexp.new(StringNode) / Regexp.compile(StringNode) calls, "" otherwise.
  • scan_features CallNode arm: registers the pattern in @regexp_patterns and bumps @needs_regexp. LV-write arm records the local-name -> pattern-index map so single-write LVs resolve through @local_regex_names.
  • codegen find_regexp_index CallNode arm + ConstantReadNode recurse: RE = Regexp.new("lit") dispatches identically to RE = /lit/. compile_constructor_expr returns "0" for this shape so the LV assignment lowers cleanly without warn_unresolved_call.

Inferring the call return as "int" sidesteps the obj_Regexp -> sp_Regexp* lowering. The actual regex pointer is reached through the static pattern table, never the LV value, so the placeholder is harmless.

Dynamic-arg forms (Regexp.new("<#{tag}>")) still fall through to the previous unresolved path -- the write-once-vs-read-eval semantics need a separate design and will be addressed in a follow-up.

Refs #586

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

Details

Commit
dbcaa07220c3
Parent
ce48af2b667e

spinel_analyze.rbmodifiedView file

@@ -1540,6 +1540,45 @@
15401540 parts.join("|")
15411541 end
15421542
1543+ # Returns the literal source string when `nid` is a `Regexp.new("...")`
1544+ # (or `Regexp.compile("...")`) call with a single StringNode argument,
1545+ # "" otherwise. Lets `pattern = Regexp.new("foo")` fold into the same
1546+ # static pattern table as `pattern = /foo/`, so `pattern.match?(s)`
1547+ # dispatches through `sp_re_pat_<i>` instead of falling through to
1548+ # unresolved-call. Dynamic-arg forms return "" and fall back.
1549+ def regexp_new_literal_pattern(nid)
1550+ if @nd_type[nid] != "CallNode"
1551+ return ""
1552+ end
1553+ mn = @nd_name[nid]
1554+ if mn != "new" && mn != "compile"
1555+ return ""
1556+ end
1557+ recv = @nd_receiver[nid]
1558+ if recv < 0
1559+ return ""
1560+ end
1561+ if @nd_type[recv] != "ConstantReadNode"
1562+ return ""
1563+ end
1564+ if @nd_name[recv] != "Regexp"
1565+ return ""
1566+ end
1567+ args_id = @nd_arguments[nid]
1568+ if args_id < 0
1569+ return ""
1570+ end
1571+ arg_ids = get_args(args_id)
1572+ if arg_ids.length == 0
1573+ return ""
1574+ end
1575+ a0 = arg_ids[0]
1576+ if @nd_type[a0] != "StringNode"
1577+ return ""
1578+ end
1579+ @nd_content[a0]
1580+ end
1581+
15431582 # Index of an InterpolatedRegularExpressionNode in @dyn_regex_node_ids,
15441583 # or -1 if scan_features hasn't registered it (defensive — should not
15451584 # happen for any reachable node).
@@ -5289,6 +5328,19 @@
52895328 if rn == "Time"
52905329 return "time"
52915330 end
5331+ # `Regexp.new(literal)` / `Regexp.compile(literal)` registers the
5332+ # pattern with the static regex table (scan_features arm) and the
5333+ # local-name → pattern map (LocalVariableWriteNode arm). The LV
5334+ # itself carries no useful runtime payload — match?/=~/match all
5335+ # dispatch through `regex_pat_c_expr` at the call site, which
5336+ # resolves the LV via `@local_regex_names` and emits the global
5337+ # `sp_re_pat_<i>`. Type the call as "int" so the LV lowers to
5338+ # `mrb_int lv_x = 0;` (the same shape `pattern = /foo/` produces).
5339+ if rn == "Regexp"
5340+ if regexp_new_literal_pattern(nid) != ""
5341+ return "int"
5342+ end
5343+ end
52925344 return "obj_" + rn
52935345 end
52945346 end
@@ -17654,6 +17706,29 @@
1765417706 @regexp_flags.push(flags)
1765517707 end
1765617708 end
17709+ # `Regexp.new("literal")` / `Regexp.compile("literal")` folds into
17710+ # the same static pattern table so `.match?`/`=~` dispatch through
17711+ # `sp_re_pat_<i>` like a `/literal/` literal. Dynamic-arg forms
17712+ # (`Regexp.new(s)` with a non-literal arg) return "" from the helper
17713+ # and are left to the existing fallback (currently unresolved).
17714+ if t == "CallNode"
17715+ rxlit = regexp_new_literal_pattern(nid)
17716+ if rxlit != ""
17717+ @needs_regexp = 1
17718+ already_rx = 0
17719+ rxi = 0
17720+ while rxi < @regexp_patterns.length
17721+ if @regexp_patterns[rxi] == rxlit
17722+ already_rx = 1
17723+ end
17724+ rxi = rxi + 1
17725+ end
17726+ if already_rx == 0
17727+ @regexp_patterns.push(rxlit)
17728+ @regexp_flags.push("0")
17729+ end
17730+ end
17731+ end
1765717732 # Track `var = /lit/` so a regex held in a local can be dispatched
1765817733 # by find_regexp_index. A name with multiple writes (any kind, any
1765917734 # regex literal) is marked ambiguous (-1) and falls through.
@@ -17675,6 +17750,22 @@
1767517750 ri = ri + 1
1767617751 end
1767717752 end
17753+ # `var = Regexp.new("lit")` resolves the same way as `var = /lit/`,
17754+ # so the local-name → pattern mapping records the static index and
17755+ # `var.match?(s)` reaches the engine.
17756+ if this_idx == -1 && vid >= 0
17757+ rxlit2 = regexp_new_literal_pattern(vid)
17758+ if rxlit2 != ""
17759+ scan_features(vid)
17760+ ri2 = 0
17761+ while ri2 < @regexp_patterns.length
17762+ if @regexp_patterns[ri2] == rxlit2
17763+ this_idx = ri2
17764+ end
17765+ ri2 = ri2 + 1
17766+ end
17767+ end
17768+ end
1767817769 i2 = 0
1767917770 found = 0
1768017771 while i2 < @local_regex_names.length