Skip to content

Commit

Permalink
peephole optimize rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Nov 30, 2024
1 parent ce208aa commit f77a648
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions DMCompiler/Optimizer/CompactorOptimizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,46 @@ public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index
}
}

// PushNFloats [count] [float] ... [float]
// Rgb [argType] [count]
// -> PushString [result]
// Only works when [argType] is FromStack and the [count] of both opcodes matches
internal sealed class EvalRgb : IOptimization {
public OptPass OptimizationPass => OptPass.ListCompactor;

public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
return [
DreamProcOpcode.PushNFloats,
DreamProcOpcode.Rgb
];
}

public bool CheckPreconditions(List<IAnnotatedBytecode> input, int index) {
var floatCount = ((AnnotatedBytecodeInstruction)input[index]).GetArg<AnnotatedBytecodeInteger>(0).Value;
var rgbInst = (AnnotatedBytecodeInstruction)input[index + 1];
var argType = rgbInst.GetArg<AnnotatedBytecodeArgumentType>(0).Value;
var stackDelta = rgbInst.GetArg<AnnotatedBytecodeStackDelta>(1).Delta;

return argType == DMCallArgumentsType.FromStack && floatCount == stackDelta;
}

public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index) {
compiler.ApplyCount += 1;

Check failure on line 402 in DMCompiler/Optimizer/CompactorOptimizations.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'DMCompiler' does not contain a definition for 'ApplyCount' and no accessible extension method 'ApplyCount' accepting a first argument of type 'DMCompiler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 402 in DMCompiler/Optimizer/CompactorOptimizations.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'DMCompiler' does not contain a definition for 'ApplyCount' and no accessible extension method 'ApplyCount' accepting a first argument of type 'DMCompiler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 402 in DMCompiler/Optimizer/CompactorOptimizations.cs

View workflow job for this annotation

GitHub Actions / build

'DMCompiler' does not contain a definition for 'ApplyCount' and no accessible extension method 'ApplyCount' accepting a first argument of type 'DMCompiler' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 402 in DMCompiler/Optimizer/CompactorOptimizations.cs

View workflow job for this annotation

GitHub Actions / build

'DMCompiler' does not contain a definition for 'ApplyCount' and no accessible extension method 'ApplyCount' accepting a first argument of type 'DMCompiler' could be found (are you missing a using directive or an assembly reference?)
var floats = (AnnotatedBytecodeInstruction)(input[index]);
var floatArgs = floats.GetArgs();
(string?, float?)[] values = new (string?, float?)[floatArgs.Count - 1];
for (int i = 1; i < floatArgs.Count; i++) { // skip the first value since it's the [count] of floats
values[i - 1] = (null, ((AnnotatedBytecodeFloat)floatArgs[i]).Value);
}

var resultStr = SharedOperations.ParseRgb(values);
var resultId = compiler.DMObjectTree.AddString(resultStr);

List<IAnnotatedBytecode> args = [new AnnotatedBytecodeString(resultId, floats.Location)];

input.RemoveRange(index, 2);
input.Insert(index, new AnnotatedBytecodeInstruction(DreamProcOpcode.PushString, 1, args));
}
}

#endregion

0 comments on commit f77a648

Please sign in to comment.