Skip to content

Commit

Permalink
ReturnFloat peephole optimization (#2049)
Browse files Browse the repository at this point in the history
Co-authored-by: ike709 <[email protected]>
  • Loading branch information
ike709 and ike709 authored Oct 22, 2024
1 parent e8f095f commit 2d93944
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ public enum DreamProcOpcode : byte {
NullRef = 0x96,
[OpcodeMetadata(0, OpcodeArgType.Reference)]
ReturnReferenceValue = 0x97,
[OpcodeMetadata(0, OpcodeArgType.Float)]
ReturnFloat = 0x98,
}
// ReSharper restore MissingBlankLines

Expand Down
18 changes: 18 additions & 0 deletions DMCompiler/Optimizer/PeepholeOptimizations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ public void Apply(List<IAnnotatedBytecode> input, int index) {
}
}

// PushFloat [float]
// Return
// -> ReturnFloat [float]
internal class ReturnFloat : IPeepholeOptimization {
public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
return [
DreamProcOpcode.PushFloat,
DreamProcOpcode.Return
];
}

public void Apply(List<IAnnotatedBytecode> input, int index) {
var firstInstruction = IPeepholeOptimization.GetInstructionAndValue(input[index], out var pushVal);
IPeepholeOptimization.ReplaceInstructions(input, index, 2,
new AnnotatedBytecodeInstruction(DreamProcOpcode.ReturnFloat, [new AnnotatedBytecodeFloat(pushVal, firstInstruction.Location)]));
}
}

// PushReferenceValue [ref]
// JumpIfFalse [label]
// -> JumpIfReferenceFalse [ref] [label]
Expand Down
5 changes: 5 additions & 0 deletions OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,11 @@ public static ProcStatus IsTypeDirect(DMProcState state) {
return ProcStatus.Continue;
}

public static ProcStatus ReturnFloat(DMProcState state) {
state.SetReturn(new DreamValue(state.ReadFloat()));
return ProcStatus.Returned;
}

#endregion
}
}
3 changes: 2 additions & 1 deletion OpenDreamRuntime/Procs/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ public sealed class DMProcState : ProcState {
{DreamProcOpcode.CreateListNRefs, DMOpcodeHandlers.CreateListNRefs},
{DreamProcOpcode.CreateListNResources, DMOpcodeHandlers.CreateListNResources},
{DreamProcOpcode.IsTypeDirect, DMOpcodeHandlers.IsTypeDirect},
{DreamProcOpcode.ReturnReferenceValue, DMOpcodeHandlers.ReturnReferenceValue}
{DreamProcOpcode.ReturnReferenceValue, DMOpcodeHandlers.ReturnReferenceValue},
{DreamProcOpcode.ReturnFloat, DMOpcodeHandlers.ReturnFloat}
};

public static readonly unsafe delegate*<DMProcState, ProcStatus>[] OpcodeHandlers;
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/ProcDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public ITuple DecodeInstruction() {
return (opcode, ReadValueType());

case DreamProcOpcode.PushFloat:
case DreamProcOpcode.ReturnFloat:
return (opcode, ReadFloat());

case DreamProcOpcode.SwitchOnFloat:
Expand Down

0 comments on commit 2d93944

Please sign in to comment.