Skip to content

Commit

Permalink
IndexRefWithString peephole opt
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Dec 10, 2024
1 parent ac33d86 commit ed5173a
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DMCompiler/Bytecode/DreamProcOpcode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ public enum DreamProcOpcode : byte {
ReturnReferenceValue = 0x97,
[OpcodeMetadata(0, OpcodeArgType.Float)]
ReturnFloat = 0x98,
[OpcodeMetadata(1, OpcodeArgType.Reference, OpcodeArgType.String)]
IndexRefWithString = 0x99,
}
// ReSharper restore MissingBlankLines

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

// PushReferenceValue [ref]
// PushString [string]
// DereferenceIndex [value] [string]
// -> IndexRefWithString [ref, string]
internal sealed class IndexRefWithString : IOptimization {
public OptPass OptimizationPass => OptPass.PeepholeOptimization;

public ReadOnlySpan<DreamProcOpcode> GetOpcodes() {
return [
DreamProcOpcode.PushReferenceValue,
DreamProcOpcode.PushString,
DreamProcOpcode.DereferenceIndex
];
}

public void Apply(DMCompiler compiler, List<IAnnotatedBytecode> input, int index) {
AnnotatedBytecodeInstruction firstInstruction = (AnnotatedBytecodeInstruction)(input[index]);
AnnotatedBytecodeReference pushVal = firstInstruction.GetArg<AnnotatedBytecodeReference>(0);

AnnotatedBytecodeInstruction secondInstruction = (AnnotatedBytecodeInstruction)(input[index + 1]);
AnnotatedBytecodeString strIndex = secondInstruction.GetArg<AnnotatedBytecodeString>(0);

input.RemoveRange(index, 3);
input.Insert(index, new AnnotatedBytecodeInstruction(DreamProcOpcode.IndexRefWithString, -1,
[pushVal, strIndex]));
}
}

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

public static ProcStatus IndexRefWithString(DMProcState state) {
DreamReference reference = state.ReadReference();
var refValue = state.GetReferenceValue(reference);

var index = new DreamValue(state.ReadString());
var indexResult = state.GetIndex(refValue, index, state);

state.Push(indexResult);
return ProcStatus.Continue;
}

public static ProcStatus DereferenceCall(DMProcState state) {
string name = state.ReadString();
var argumentInfo = state.ReadProcArguments();
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/DMProc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public sealed class DMProcState : ProcState {
{DreamProcOpcode.JumpIfFalseReference, DMOpcodeHandlers.JumpIfFalseReference},
{DreamProcOpcode.DereferenceField, DMOpcodeHandlers.DereferenceField},
{DreamProcOpcode.DereferenceIndex, DMOpcodeHandlers.DereferenceIndex},
{DreamProcOpcode.IndexRefWithString, DMOpcodeHandlers.IndexRefWithString},
{DreamProcOpcode.DereferenceCall, DMOpcodeHandlers.DereferenceCall},
{DreamProcOpcode.PopReference, DMOpcodeHandlers.PopReference},
{DreamProcOpcode.BitShiftLeftReference,DMOpcodeHandlers.BitShiftLeftReference},
Expand Down
1 change: 1 addition & 0 deletions OpenDreamRuntime/Procs/ProcDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public ITuple DecodeInstruction() {
return (opcode, ReadReference(), ReadReference());

case DreamProcOpcode.PushRefAndDereferenceField:
case DreamProcOpcode.IndexRefWithString:
return (opcode, ReadReference(), ReadString());

case DreamProcOpcode.CallStatement:
Expand Down

0 comments on commit ed5173a

Please sign in to comment.