Skip to content

Commit 46b206a

Browse files
ike709ike709wixoaGit
authored
String concat const folding (#2061)
Co-authored-by: ike709 <ike709@github.com> Co-authored-by: wixoa <wixoag@gmail.com>
1 parent 9e1a9e6 commit 46b206a

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

DMCompiler/DM/Builders/DMASTFolder.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,6 @@ private DMASTExpression FoldExpression(DMASTExpression? expression) {
219219

220220
break;
221221
}
222-
case DMASTAdd add: {
223-
DMASTConstantString? lhsString = add.LHS as DMASTConstantString;
224-
DMASTConstantString? rhsString = add.RHS as DMASTConstantString;
225-
if (lhsString != null && rhsString != null) return new DMASTConstantString(expression.Location, lhsString.Value + rhsString.Value);
226-
227-
break;
228-
}
229222

230223
#endregion Math
231224

DMCompiler/Optimizer/AnnotatedBytecode.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ public void SetLocation(Location loc) {
235235
public Location GetLocation() {
236236
return Location;
237237
}
238+
239+
public string ResolveString(DMCompiler compiler) {
240+
return compiler.DMObjectTree.StringTable[Id];
241+
}
238242
}
239243

240244
internal sealed class AnnotatedBytecodeArgumentType(DMCallArgumentsType value, Location location) : IAnnotatedBytecode {

DMCompiler/Optimizer/PeepholeOptimizations.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,33 @@ public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index
767767
}
768768
}
769769

770+
// PushString [constant]
771+
// PushString [constant]
772+
// Add
773+
// -> PushString [result]
774+
internal sealed class ConstFoldAddStrings : IPeepholeOptimization {
775+
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
776+
return [
777+
DreamProcOpcode.PushString,
778+
DreamProcOpcode.PushString,
779+
DreamProcOpcode.Add,
780+
];
781+
}
782+
783+
public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index) {
784+
var firstInstruction = (AnnotatedBytecodeInstruction)input[index];
785+
var firstString = firstInstruction.GetArg<AnnotatedBytecodeString>(0);
786+
var secondString = ((AnnotatedBytecodeInstruction)input[index+1]).GetArg<AnnotatedBytecodeString>(0);
787+
788+
var combinedId = compiler.DMObjectTree.AddString(firstString.ResolveString(compiler) + secondString.ResolveString(compiler)); // TODO: Currently doesn't handle removing strings from the string tree that have no other references
789+
790+
var args = new List<IAnnotatedBytecode>(1) {new AnnotatedBytecodeString(combinedId, firstInstruction.Location)};
791+
792+
IPeepholeOptimization.ReplaceInstructions(input, index, 3,
793+
new AnnotatedBytecodeInstruction(DreamProcOpcode.PushString, 1, args));
794+
}
795+
}
796+
770797
// PushFloat [constant]
771798
// PushFloat [constant]
772799
// Subtract

0 commit comments

Comments
 (0)