add single ir generator methods

This commit is contained in:
Youmu Liu
2026-08-05 18:13:24 +08:00
parent 00e0d84603
commit 068ad18998
3 changed files with 78 additions and 19 deletions
+1 -2
View File
@@ -12,7 +12,6 @@
#define _FMA_SELECT(i) _MM_SHUFFLE(i, i, i, i)
SN_MK_PROC_DECL_STD(float, sse, , fma)
{
__mk_param_std(LEFT_RIGHT_ANS, float);
@@ -125,7 +124,7 @@ SN_MK_PROC_DECL_STD(float, avx, _fma, fma)
register __m256 a1 = _mm256_load_ps((float *)&a_base->y[1]);
register __m256 a2 = _mm256_load_ps((float *)&a_base->y[2]);
register __m256 a3 = _mm256_load_ps((float *)&a_base->y[3]);
SN_MK_FMA_F32_AVX2_CYCLE(0);
SN_MK_FMA_F32_AVX2_CYCLE(1);
SN_MK_FMA_F32_AVX2_CYCLE(2);
@@ -38,6 +38,20 @@ namespace StgSharp.RegularAnalysis.Abstraction
emitter.Append(string.IsNullOrEmpty(line) ? string.Empty : line);
}
public static SequenceEmitter<string> AppendLine(
this SequenceEmitter<string> emitter,
params string[] lines
)
{
if (lines is null || lines.Length == 0) {
return emitter;
}
foreach (string line in lines) {
emitter = emitter.AppendLine(line);
}
return emitter;
}
public static SequenceEmitter<string> AppendLine(
this SequenceEmitter<string> emitter,
string line,
@@ -76,6 +76,24 @@ namespace StgSharp.RegularAnalysis.Text
#region code
private static int TryGeneratePack(
int outer_level,
ReadOnlySpan<RegexIR> ir_span,
SourceGenContext sc,
in SequenceEmitter<string> code,
in List<SequenceEmitter<string>> local_func
)
{
if (ir_span[0] is not RegexPackIR pack) {
return -1;
}
string group_name = pack.GroupName;
string begin = sc.GroupBegin(group_name);
string end = sc.GroupEnd(group_name);
_ = code.AppendLine(begin).AppendLine(end);
return 1;
}
private static int TryGenerateCountSet(
int outer_level,
ReadOnlySpan<RegexIR> ir_span,
@@ -87,10 +105,18 @@ namespace StgSharp.RegularAnalysis.Text
if (ir_span[0] is not RegexCountSetIR count_set) {
return -1;
}
bool is_greedy = count_set.IsGreedy;
int min = count_set.Min;
int max = count_set.Max;
int leave = is_greedy ? max : min;
string counter = sc.RequireNewCounter();
string set_source = count_set.CharSet;
RegexCharSet[] set_arr = FigureCharSetType(set_source, out bool accept);
_ = code.AppendLine(accept ? @$"if(" : @$"if(!(");
_ = code.AppendLine($@"for(; {counter}<{leave}; {counter}++)")
.AppendLine("{")
.AppendLine(accept ? @$"if(" : @$"if(!(");
string _case_code;
RegexCharSet set;
for (int i = 1; i < set_arr.Length - 1; i++)
@@ -101,10 +127,21 @@ namespace StgSharp.RegularAnalysis.Text
}
set = set_arr[^1];
_case_code = GenLine(set, sc, ")");
_ = code.AppendLine(_case_code)
.AppendLine("{")
.AppendLine("-------------------------------------")// TODO case success
.AppendLine("}");
_ = code.AppendLine(_case_code, //
"{", // in case match
"}", //
"else", //
"{", //
"break;", // in case miss match
"}", //
"}", // end of for
@$"if({counter}>{min})", // match here
"{", // in case count reaches min count request
"}", //
"else", //
"{", // in case count does not reach min count request
"}", //
string.Empty); //endof count
return 1;
}
@@ -128,18 +165,17 @@ namespace StgSharp.RegularAnalysis.Text
string counter = sc.RequireNewCounter();
// TODO cannot check real count outside the cycle
_ = code.AppendLine($@"int {counter} = 0;")
.AppendLine(@$"for(; {counter}<{leave}; {counter}++)")
.AppendLine("{")
.AppendLine(@$"if({sc.InputSpan}[{sc.Position}..].StartWith({literal}))")
.AppendLine("{")
.AppendLine(@$"{sc.Position}+={seq.Length};")
.AppendLine("}")
.AppendLine("}")
.AppendLine()
.AppendLine(@$"if({counter}>{min})") // TODO match here
.AppendLine($@"else"); // TODO fail here
_ = code.AppendLine($@"int {counter} = 0;", //
@$"for(; {counter}<{leave}; {counter}++)", //
"{", //
@$"if({sc.InputSpan}[{sc.Position}..].StartWith({literal}))", //
"{", //
@$"{sc.Position}+={seq.Length};", //
"}", //
"}", //
@$"if({counter}>{min})", // TODO match here
$@"else", //
"}"); // TODO fail here
return 1;
}
@@ -181,6 +217,16 @@ namespace StgSharp.RegularAnalysis.Text
_ = code.AppendLine(accept ? @$"if(" : @$"if(!(");
string _case_code;
RegexCharSet set;
/*
* in loop:
* for( is cas1 1 ||
* is case 2 ||
* .....
* is case last)
* {
* success here
* }
*/
for (int i = 1; i < set_arr.Length - 1; i++)
{
set = set_arr[i];