Commit d7db4dd3

d7db4dd36138Merged

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

Description

Merge pull request #311 from ryanseys/rs-up-interp-regex

feat(parse,codegen,runtime): support InterpolatedRegularExpressionNode (/foo_#{x}/)

Details

Commit
d7db4dd36138
Parent
b54c50b00120
Merge parent
c3ca6fc1e64f

spinel_codegen.rbmodifiedView file

@@ -281,6 +281,13 @@
281281 @needs_rand = 0
282282 @regexp_patterns = "".split(",")
283283 @regexp_flags = "".split(",")
284+ # Dynamic-regex (InterpolatedRegularExpressionNode) call-site cache.
285+ # Each AST node gets a unique idx so the emitter can produce one
286+ # `sp_re_dyn_<idx>` helper per source location with its own
287+ # function-scope cache (string key + compiled pattern). Collected
288+ # in scan_features so indexes are stable across compile_expr visits.
289+ @dyn_regex_node_ids = []
290+ @dyn_regex_flags = "".split(",")
284291 # `var = /lit/` resolution. Parallel arrays: `@local_regex_names`
285292 # holds the local-variable name and `@local_regex_idx` holds the
286293 # corresponding `@regexp_patterns` index, or -1 when the same name
@@ -1552,6 +1559,66 @@
15521559 -1
15531560 end
15541561
1562+ # Returns a C expression evaluating to a `mrb_regexp_pattern *`, or "" if
1563+ # the node isn't a regex source. Static literals resolve to their
1564+ # pre-compiled `sp_re_pat_<i>` global; InterpolatedRegularExpressionNode
1565+ # gets a runtime `sp_re_runtime_compile(...)` call. Centralizes the
1566+ # dispatch so each =~/match?/match/gsub/sub/scan/split call site doesn't
1567+ # have to repeat the InterpolatedRegex check.
1568+ def regex_pat_c_expr(nid)
1569+ ridx = find_regexp_index(nid)
1570+ if ridx >= 0
1571+ return "sp_re_pat_" + ridx.to_s
1572+ end
1573+ if @nd_type[nid] == "InterpolatedRegularExpressionNode"
1574+ @needs_regexp = 1
1575+ return compile_expr(nid)
1576+ end
1577+ ""
1578+ end
1579+
1580+ # Maps Prism's regex flag bits to the engine's `RE_FLAG_*` values and
1581+ # returns a C bitwise-OR string ("0", "1", "1|6", etc.). Single source
1582+ # of truth used by both the static-regex collector arm in scan_features
1583+ # and the per-call-site helper emitted by emit_dyn_regex_helpers.
1584+ # Prism: IGNORE_CASE=4, EXTENDED=8, MULTI_LINE=16.
1585+ # Engine: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8.
1586+ # Ruby's /m (dot-matches-newline) maps to MULTILINE|DOTALL = 6.
1587+ def regex_engine_flags(nid)
1588+ if @nd_flags[nid] == 0
1589+ return "0"
1590+ end
1591+ f = @nd_flags[nid]
1592+ parts = "".split(",")
1593+ if f & 4 != 0
1594+ parts.push("1")
1595+ end
1596+ if f & 16 != 0
1597+ parts.push("6")
1598+ end
1599+ if f & 8 != 0
1600+ parts.push("8")
1601+ end
1602+ if parts.length == 0
1603+ return "0"
1604+ end
1605+ parts.join("|")
1606+ end
1607+
1608+ # Index of an InterpolatedRegularExpressionNode in @dyn_regex_node_ids,
1609+ # or -1 if scan_features hasn't registered it (defensive — should not
1610+ # happen for any reachable node).
1611+ def find_dyn_regex_index(nid)
1612+ i = 0
1613+ while i < @dyn_regex_node_ids.length
1614+ if @dyn_regex_node_ids[i] == nid
1615+ return i
1616+ end
1617+ i = i + 1
1618+ end
1619+ -1
1620+ end
1621+
15551622 def find_class_idx(name)
15561623 i = 0
15571624 while i < @cls_names.length
@@ -12146,31 +12213,35 @@
1214612213 @needs_setjmp = 1
1214712214 end
1214812215 end
12216+ if t == "InterpolatedRegularExpressionNode" || t == "InterpolatedMatchLastLineNode"
12217+ # Pre-flag so emit_regexp_runtime fires (defines sp_re_init) even
12218+ # when the program uses ONLY interpolated regexes -- without this,
12219+ # the linker fails on the unreferenced sp_re_init main() call.
12220+ @needs_regexp = 1
12221+ end
12222+ if t == "InterpolatedRegularExpressionNode"
12223+ # Register this AST site so emit_dyn_regex_helpers produces a
12224+ # dedicated `sp_re_dyn_<idx>` cache for it. Idempotent: a node
12225+ # visited twice (e.g. through type-inference + emit passes) keeps
12226+ # its first-assigned index.
12227+ already_dyn = 0
12228+ di = 0
12229+ while di < @dyn_regex_node_ids.length
12230+ if @dyn_regex_node_ids[di] == nid
12231+ already_dyn = 1
12232+ end
12233+ di = di + 1
12234+ end
12235+ if already_dyn == 0
12236+ @dyn_regex_node_ids.push(nid)
12237+ @dyn_regex_flags.push(regex_engine_flags(nid))
12238+ end
12239+ end
1214912240 if t == "RegularExpressionNode"
1215012241 @needs_regexp = 1
1215112242 # Collect pattern and flags
1215212243 pat = @nd_unescaped[nid]
12153- flags = "0"
12154- if @nd_flags[nid] != 0
12155- f = @nd_flags[nid]
12156- parts = "".split(",")
12157- # Prism flag bits → engine `RE_FLAG_*` values (see re_internal.h).
12158- # Prism: IGNORE_CASE=4, EXTENDED=8, MULTI_LINE=16.
12159- # Engine: IGNORECASE=1, MULTILINE=2, DOTALL=4, EXTENDED=8.
12160- # Ruby's /m (dot-matches-newline) maps to MULTILINE|DOTALL = 6.
12161- if f & 4 != 0
12162- parts.push("1")
12163- end
12164- if f & 16 != 0
12165- parts.push("6")
12166- end
12167- if f & 8 != 0
12168- parts.push("8")
12169- end
12170- if parts.length > 0
12171- flags = parts.join("|")
12172- end
12173- end
12244+ flags = regex_engine_flags(nid)
1217412245 # Idempotent: identical patterns share the same compiled global,
1217512246 # so a second visit (e.g. via the LocalVariableWriteNode pre-scan
1217612247 # below) is a no-op.
@@ -13548,6 +13619,7 @@
1354813619 # Emit program-specific regexp patterns
1354913620 if @needs_regexp == 1
1355013621 emit_regexp_runtime
13622+ emit_dyn_regex_helpers
1355113623 end
1355213624 emit_class_structs
1355313625 emit_raw("/*TUPLE_INSERT_POINT*/")
@@ -14267,6 +14339,34 @@
1426714339 emit_raw("")
1426814340 end
1426914341
14342+ # Per-call-site cached helper for InterpolatedRegularExpressionNode.
14343+ # Each AST source location gets a `sp_re_dyn_<idx>(const char *new_pat)`
14344+ # function with its own function-scope statics for the cached pattern
14345+ # string and compiled engine pattern. On a cache hit (strcmp match) we
14346+ # return the existing pattern; on a miss we re_free the old one and
14347+ # recompile with the call-site's baked flags. This bounds heap to one
14348+ # `mrb_regexp_pattern` per call site (count fixed at AOT compile time)
14349+ # and matches Ruby's per-source-location dynamic-regexp cache. The old
14350+ # `sp_re_runtime_compile` leaked a fresh pattern every evaluation.
14351+ def emit_dyn_regex_helpers
14352+ i = 0
14353+ while i < @dyn_regex_node_ids.length
14354+ flags = @dyn_regex_flags[i]
14355+ emit_raw("static mrb_regexp_pattern *sp_re_dyn_" + i.to_s + "(const char *new_pat) {")
14356+ emit_raw(" static char *cached_str = NULL;")
14357+ emit_raw(" static mrb_regexp_pattern *cached_pat = NULL;")
14358+ emit_raw(" if (cached_str != NULL && strcmp(cached_str, new_pat) == 0) return cached_pat;")
14359+ emit_raw(" re_free(cached_pat);")
14360+ emit_raw(" free(cached_str);")
14361+ emit_raw(" cached_str = strdup(new_pat);")
14362+ emit_raw(" cached_pat = re_compile(new_pat, (int64_t)strlen(new_pat), " + flags + ");")
14363+ emit_raw(" return cached_pat;")
14364+ emit_raw("}")
14365+ emit_raw("")
14366+ i = i + 1
14367+ end
14368+ end
14369+
1427014370 # ---- Struct emission ----
1427114371 def emit_global_constants
1427214372 # Emit file-scope constant declarations (initialized in main)
@@ -18412,6 +18512,20 @@
1841218512 $stderr.puts "Spinel: BackReferenceReadNode `" + n + "` not supported"
1841318513 exit(1)
1841418514 end
18515+ if t == "InterpolatedRegularExpressionNode"
18516+ # `/foo_#{x}/` -- pattern is only known at execution time, so we
18517+ # can't pre-compile to an sp_re_pat_<i> global. The pattern string
18518+ # is built via compile_interpolated and passed to a per-call-site
18519+ # cached helper `sp_re_dyn_<idx>` (emitted by emit_dyn_regex_helpers).
18520+ # The helper compares the new pattern bytes to its cached key and
18521+ # only re_free+recompile on miss, so heap is bounded to one engine
18522+ # pattern per source location regardless of how many times the
18523+ # site evaluates. Flags are baked into the helper at emission time.
18524+ @needs_regexp = 1
18525+ pat_c = compile_interpolated(nid)
18526+ idx = find_dyn_regex_index(nid)
18527+ return "sp_re_dyn_" + idx.to_s + "(" + pat_c + ")"
18528+ end
1841518529 if t == "NumberedReferenceReadNode"
1841618530 num = @nd_value[nid]
1841718531 if num >= 1 && num <= 9
@@ -19714,19 +19828,20 @@
1971419828 # regex.match? / regex.match / regex =~ str — receiver is the regex
1971519829 # (typically a constant referring to a /…/ literal). Dispatched here
1971619830 # rather than compile_string_method_expr, which wants a string
19717- # receiver.
19831+ # receiver. Receiver can also be InterpolatedRegularExpressionNode
19832+ # for `/foo_#{x}/.match?(s)` (rare but valid).
1971819833 if recv >= 0 && (mname == "match?" || mname == "=~" || mname == "match")
19719- ridx = find_regexp_index(recv)
19720- if ridx >= 0
19834+ rpat = regex_pat_c_expr(recv)
19835+ if rpat != ""
1972119836 args_id = @nd_arguments[nid]
1972219837 if args_id >= 0
1972319838 arg_ids = get_args(args_id)
1972419839 if arg_ids.length > 0
1972519840 sc = compile_expr(arg_ids[0])
1972619841 if mname == "match?"
19727- return "sp_re_match_p(sp_re_pat_" + ridx.to_s + ", " + sc + ")"
19842+ return "sp_re_match_p(" + rpat + ", " + sc + ")"
1972819843 end
19729- return "sp_re_match(sp_re_pat_" + ridx.to_s + ", " + sc + ")"
19844+ return "sp_re_match(" + rpat + ", " + sc + ")"
1973019845 end
1973119846 end
1973219847 end
@@ -20937,15 +21052,17 @@
2093721052 return "(" + compile_expr(recv) + " >= " + compile_arg0(nid) + ")"
2093821053 end
2093921054 if mname == "=~"
20940- # str =~ /pattern/ → sp_re_match(pat, str)
21055+ # str =~ /pattern/ → sp_re_match(pat, str). Pattern source can be
21056+ # a static literal, a regex-typed local/const (find_regexp_index),
21057+ # or InterpolatedRegularExpressionNode (runtime-compiled).
2094121058 rc = compile_expr_gc_rooted(recv)
2094221059 re_args_id = @nd_arguments[nid]
2094321060 if re_args_id >= 0
2094421061 argl = get_args(re_args_id)
2094521062 if argl.length > 0
20946- ridx = find_regexp_index(argl[0])
20947- if ridx >= 0
20948- return "sp_re_match(sp_re_pat_" + ridx.to_s + ", " + rc + ")"
21063+ rpat = regex_pat_c_expr(argl[0])
21064+ if rpat != ""
21065+ return "sp_re_match(" + rpat + ", " + rc + ")"
2094921066 end
2095021067 end
2095121068 end
@@ -21518,9 +21635,9 @@
2151821635 if args_id >= 0
2151921636 a = get_args(args_id)
2152021637 if a.length > 0
21521- ridx = find_regexp_index(a[0])
21522- if ridx >= 0
21523- return "sp_re_split(sp_re_pat_" + ridx.to_s + ", " + rc + ")"
21638+ rpat = regex_pat_c_expr(a[0])
21639+ if rpat != ""
21640+ return "sp_re_split(" + rpat + ", " + rc + ")"
2152421641 end
2152521642 # Peephole: literal "".split(literal) is the empty-StrArray idiom;
2152621643 # skip the strlen+sep scan and emit a direct allocator call.
@@ -21542,11 +21659,10 @@
2154221659 if args_id >= 0
2154321660 argl = get_args(args_id)
2154421661 if argl.length > 0
21545- ridx = find_regexp_index(argl[0])
21546- if ridx >= 0
21662+ rpat = regex_pat_c_expr(argl[0])
21663+ if rpat != ""
2154721664 @needs_str_array = 1
21548- @needs_regexp = 1
21549- return "sp_re_scan(sp_re_pat_" + ridx.to_s + ", " + rc + ")"
21665+ return "sp_re_scan(" + rpat + ", " + rc + ")"
2155021666 end
2155121667 end
2155221668 end
@@ -21557,9 +21673,9 @@
2155721673 if re_args_id >= 0
2155821674 argl = get_args(re_args_id)
2155921675 if argl.length > 0
21560- ridx = find_regexp_index(argl[0])
21561- if ridx >= 0
21562- return "sp_re_match_p(sp_re_pat_" + ridx.to_s + ", " + rc + ")"
21676+ rpat = regex_pat_c_expr(argl[0])
21677+ if rpat != ""
21678+ return "sp_re_match_p(" + rpat + ", " + rc + ")"
2156321679 end
2156421680 end
2156521681 end
@@ -21570,9 +21686,9 @@
2157021686 if args_id >= 0
2157121687 a = get_args(args_id)
2157221688 if a.length >= 2
21573- ridx = find_regexp_index(a[0])
21574- if ridx >= 0
21575- return "sp_re_gsub(sp_re_pat_" + ridx.to_s + ", " + rc + ", " + compile_expr(a[1]) + ")"
21689+ rpat = regex_pat_c_expr(a[0])
21690+ if rpat != ""
21691+ return "sp_re_gsub(" + rpat + ", " + rc + ", " + compile_expr(a[1]) + ")"
2157621692 end
2157721693 return "sp_str_gsub(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2157821694 end
@@ -21584,9 +21700,9 @@
2158421700 if args_id >= 0
2158521701 a = get_args(args_id)
2158621702 if a.length >= 2
21587- ridx = find_regexp_index(a[0])
21588- if ridx >= 0
21589- return "sp_re_sub(sp_re_pat_" + ridx.to_s + ", " + rc + ", " + compile_expr(a[1]) + ")"
21703+ rpat = regex_pat_c_expr(a[0])
21704+ if rpat != ""
21705+ return "sp_re_sub(" + rpat + ", " + rc + ", " + compile_expr(a[1]) + ")"
2159021706 end
2159121707 return "sp_str_sub(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"
2159221708 end