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 = 0282282
@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 one286+
# `sp_re_dyn_<idx>` helper per source location with its own287+
# function-scope cache (string key + compiled pattern). Collected288+
# 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 the286293
# corresponding `@regexp_patterns` index, or -1 when the same name@@ -1552,6 +1559,66 @@
15521559
-115531560
end15541561
1562+
# Returns a C expression evaluating to a `mrb_regexp_pattern *`, or "" if1563+
# the node isn't a regex source. Static literals resolve to their1564+
# pre-compiled `sp_re_pat_<i>` global; InterpolatedRegularExpressionNode1565+
# gets a runtime `sp_re_runtime_compile(...)` call. Centralizes the1566+
# dispatch so each =~/match?/match/gsub/sub/scan/split call site doesn't1567+
# have to repeat the InterpolatedRegex check.1568+
def regex_pat_c_expr(nid)1569+
ridx = find_regexp_index(nid)1570+
if ridx >= 01571+
return "sp_re_pat_" + ridx.to_s1572+
end1573+
if @nd_type[nid] == "InterpolatedRegularExpressionNode"1574+
@needs_regexp = 11575+
return compile_expr(nid)1576+
end1577+
""1578+
end1579+
1580+
# Maps Prism's regex flag bits to the engine's `RE_FLAG_*` values and1581+
# returns a C bitwise-OR string ("0", "1", "1|6", etc.). Single source1582+
# of truth used by both the static-regex collector arm in scan_features1583+
# 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] == 01589+
return "0"1590+
end1591+
f = @nd_flags[nid]1592+
parts = "".split(",")1593+
if f & 4 != 01594+
parts.push("1")1595+
end1596+
if f & 16 != 01597+
parts.push("6")1598+
end1599+
if f & 8 != 01600+
parts.push("8")1601+
end1602+
if parts.length == 01603+
return "0"1604+
end1605+
parts.join("|")1606+
end1607+
1608+
# Index of an InterpolatedRegularExpressionNode in @dyn_regex_node_ids,1609+
# or -1 if scan_features hasn't registered it (defensive — should not1610+
# happen for any reachable node).1611+
def find_dyn_regex_index(nid)1612+
i = 01613+
while i < @dyn_regex_node_ids.length1614+
if @dyn_regex_node_ids[i] == nid1615+
return i1616+
end1617+
i = i + 11618+
end1619+
-11620+
end1621+
15551622
def find_class_idx(name)15561623
i = 015571624
while i < @cls_names.length@@ -12146,31 +12213,35 @@
1214612213
@needs_setjmp = 11214712214
end1214812215
end12216+
if t == "InterpolatedRegularExpressionNode" || t == "InterpolatedMatchLastLineNode"12217+
# Pre-flag so emit_regexp_runtime fires (defines sp_re_init) even12218+
# when the program uses ONLY interpolated regexes -- without this,12219+
# the linker fails on the unreferenced sp_re_init main() call.12220+
@needs_regexp = 112221+
end12222+
if t == "InterpolatedRegularExpressionNode"12223+
# Register this AST site so emit_dyn_regex_helpers produces a12224+
# dedicated `sp_re_dyn_<idx>` cache for it. Idempotent: a node12225+
# visited twice (e.g. through type-inference + emit passes) keeps12226+
# its first-assigned index.12227+
already_dyn = 012228+
di = 012229+
while di < @dyn_regex_node_ids.length12230+
if @dyn_regex_node_ids[di] == nid12231+
already_dyn = 112232+
end12233+
di = di + 112234+
end12235+
if already_dyn == 012236+
@dyn_regex_node_ids.push(nid)12237+
@dyn_regex_flags.push(regex_engine_flags(nid))12238+
end12239+
end1214912240
if t == "RegularExpressionNode"1215012241
@needs_regexp = 11215112242
# Collect pattern and flags1215212243
pat = @nd_unescaped[nid]12153-
flags = "0"12154-
if @nd_flags[nid] != 012155-
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 != 012162-
parts.push("1")12163-
end12164-
if f & 16 != 012165-
parts.push("6")12166-
end12167-
if f & 8 != 012168-
parts.push("8")12169-
end12170-
if parts.length > 012171-
flags = parts.join("|")12172-
end12173-
end12244+
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-scan1217612247
# below) is a no-op.@@ -13548,6 +13619,7 @@
1354813619
# Emit program-specific regexp patterns1354913620
if @needs_regexp == 11355013621
emit_regexp_runtime13622+
emit_dyn_regex_helpers1355113623
end1355213624
emit_class_structs1355313625
emit_raw("/*TUPLE_INSERT_POINT*/")@@ -14267,6 +14339,34 @@
1426714339
emit_raw("")1426814340
end1426914341
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 pattern14345+
# string and compiled engine pattern. On a cache hit (strcmp match) we14346+
# return the existing pattern; on a miss we re_free the old one and14347+
# recompile with the call-site's baked flags. This bounds heap to one14348+
# `mrb_regexp_pattern` per call site (count fixed at AOT compile time)14349+
# and matches Ruby's per-source-location dynamic-regexp cache. The old14350+
# `sp_re_runtime_compile` leaked a fresh pattern every evaluation.14351+
def emit_dyn_regex_helpers14352+
i = 014353+
while i < @dyn_regex_node_ids.length14354+
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 + 114367+
end14368+
end14369+
1427014370
# ---- Struct emission ----1427114371
def emit_global_constants1427214372
# Emit file-scope constant declarations (initialized in main)@@ -18412,6 +18512,20 @@
1841218512
$stderr.puts "Spinel: BackReferenceReadNode `" + n + "` not supported"1841318513
exit(1)1841418514
end18515+
if t == "InterpolatedRegularExpressionNode"18516+
# `/foo_#{x}/` -- pattern is only known at execution time, so we18517+
# can't pre-compile to an sp_re_pat_<i> global. The pattern string18518+
# is built via compile_interpolated and passed to a per-call-site18519+
# cached helper `sp_re_dyn_<idx>` (emitted by emit_dyn_regex_helpers).18520+
# The helper compares the new pattern bytes to its cached key and18521+
# only re_free+recompile on miss, so heap is bounded to one engine18522+
# pattern per source location regardless of how many times the18523+
# site evaluates. Flags are baked into the helper at emission time.18524+
@needs_regexp = 118525+
pat_c = compile_interpolated(nid)18526+
idx = find_dyn_regex_index(nid)18527+
return "sp_re_dyn_" + idx.to_s + "(" + pat_c + ")"18528+
end1841518529
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 regex1971519829
# (typically a constant referring to a /…/ literal). Dispatched here1971619830
# rather than compile_string_method_expr, which wants a string19717-
# receiver.19831+
# receiver. Receiver can also be InterpolatedRegularExpressionNode19832+
# 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 >= 019834+
rpat = regex_pat_c_expr(recv)19835+
if rpat != ""1972119836
args_id = @nd_arguments[nid]1972219837
if args_id >= 01972319838
arg_ids = get_args(args_id)1972419839
if arg_ids.length > 01972519840
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
end19729-
return "sp_re_match(sp_re_pat_" + ridx.to_s + ", " + sc + ")"19844+
return "sp_re_match(" + rpat + ", " + sc + ")"1973019845
end1973119846
end1973219847
end@@ -20937,15 +21052,17 @@
2093721052
return "(" + compile_expr(recv) + " >= " + compile_arg0(nid) + ")"2093821053
end2093921054
if mname == "=~"20940-
# str =~ /pattern/ → sp_re_match(pat, str)21055+
# str =~ /pattern/ → sp_re_match(pat, str). Pattern source can be21056+
# 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 >= 02094421061
argl = get_args(re_args_id)2094521062
if argl.length > 020946-
ridx = find_regexp_index(argl[0])20947-
if ridx >= 020948-
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
end2095021067
end2095121068
end@@ -21518,9 +21635,9 @@
2151821635
if args_id >= 02151921636
a = get_args(args_id)2152021637
if a.length > 021521-
ridx = find_regexp_index(a[0])21522-
if ridx >= 021523-
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
end2152521642
# 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 >= 02154321660
argl = get_args(args_id)2154421661
if argl.length > 021545-
ridx = find_regexp_index(argl[0])21546-
if ridx >= 021662+
rpat = regex_pat_c_expr(argl[0])21663+
if rpat != ""2154721664
@needs_str_array = 121548-
@needs_regexp = 121549-
return "sp_re_scan(sp_re_pat_" + ridx.to_s + ", " + rc + ")"21665+
return "sp_re_scan(" + rpat + ", " + rc + ")"2155021666
end2155121667
end2155221668
end@@ -21557,9 +21673,9 @@
2155721673
if re_args_id >= 02155821674
argl = get_args(re_args_id)2155921675
if argl.length > 021560-
ridx = find_regexp_index(argl[0])21561-
if ridx >= 021562-
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
end2156421680
end2156521681
end@@ -21570,9 +21686,9 @@
2157021686
if args_id >= 02157121687
a = get_args(args_id)2157221688
if a.length >= 221573-
ridx = find_regexp_index(a[0])21574-
if ridx >= 021575-
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
end2157721693
return "sp_str_gsub(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"2157821694
end@@ -21584,9 +21700,9 @@
2158421700
if args_id >= 02158521701
a = get_args(args_id)2158621702
if a.length >= 221587-
ridx = find_regexp_index(a[0])21588-
if ridx >= 021589-
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
end2159121707
return "sp_str_sub(" + rc + ", " + compile_expr(a[0]) + ", " + compile_expr(a[1]) + ")"2159221708
endspinel_parse.cmodifiedView file
Loading diff…
test/regex.rbmodifiedView file
Loading diff…
test/regex.rb.expectedmodifiedView file
Loading diff…