@@ -618,6 +618,78 @@ public void Apply(List<IAnnotatedBytecode> input, int index) {
618618}
619619
620620#region Constant Folding
621+
622+ // PushFloat [constant]
623+ // BitNot
624+ // -> PushFloat [result]
625+ internal sealed class ConstFoldBitNot : IPeepholeOptimization {
626+ public ReadOnlySpan < DreamProcOpcode > GetOpcodes ( ) {
627+ return [
628+ DreamProcOpcode . PushFloat ,
629+ DreamProcOpcode . BitNot
630+ ] ;
631+ }
632+
633+ public void Apply ( List < IAnnotatedBytecode > input , int index ) {
634+ var firstInstruction = IPeepholeOptimization . GetInstructionAndValue ( input [ index ] , out var pushVal1 ) ;
635+
636+ var args = new List < IAnnotatedBytecode > ( 1 ) { new AnnotatedBytecodeFloat ( ( ( ~ ( int ) pushVal1 ) & 0xFFFFFF ) , firstInstruction . Location ) } ;
637+
638+ IPeepholeOptimization . ReplaceInstructions ( input , index , 2 ,
639+ new AnnotatedBytecodeInstruction ( DreamProcOpcode . PushFloat , 1 , args ) ) ;
640+ }
641+ }
642+
643+ // PushFloat [constant]
644+ // PushFloat [constant]
645+ // BitOr
646+ // -> PushFloat [result]
647+ internal sealed class ConstFoldBitOr : IPeepholeOptimization {
648+ public ReadOnlySpan < DreamProcOpcode > GetOpcodes ( ) {
649+ return [
650+ DreamProcOpcode . PushFloat ,
651+ DreamProcOpcode . PushFloat ,
652+ DreamProcOpcode . BitOr ,
653+ ] ;
654+ }
655+
656+ public void Apply ( List < IAnnotatedBytecode > input , int index ) {
657+ var firstInstruction = IPeepholeOptimization . GetInstructionAndValue ( input [ index ] , out var pushVal1 ) ;
658+
659+ IPeepholeOptimization . GetInstructionAndValue ( input [ index + 1 ] , out var pushVal2 ) ;
660+
661+ var args = new List < IAnnotatedBytecode > ( 1 ) { new AnnotatedBytecodeFloat ( ( ( int ) pushVal1 | ( int ) pushVal2 ) , firstInstruction . Location ) } ;
662+
663+ IPeepholeOptimization . ReplaceInstructions ( input , index , 3 ,
664+ new AnnotatedBytecodeInstruction ( DreamProcOpcode . PushFloat , 1 , args ) ) ;
665+ }
666+ }
667+
668+ // PushFloat [constant]
669+ // PushFloat [constant]
670+ // BitAnd
671+ // -> PushFloat [result]
672+ internal sealed class ConstFoldBitAnd : IPeepholeOptimization {
673+ public ReadOnlySpan < DreamProcOpcode > GetOpcodes ( ) {
674+ return [
675+ DreamProcOpcode . PushFloat ,
676+ DreamProcOpcode . PushFloat ,
677+ DreamProcOpcode . BitAnd ,
678+ ] ;
679+ }
680+
681+ public void Apply ( List < IAnnotatedBytecode > input , int index ) {
682+ var firstInstruction = IPeepholeOptimization . GetInstructionAndValue ( input [ index ] , out var pushVal1 ) ;
683+
684+ IPeepholeOptimization . GetInstructionAndValue ( input [ index + 1 ] , out var pushVal2 ) ;
685+
686+ var args = new List < IAnnotatedBytecode > ( 1 ) { new AnnotatedBytecodeFloat ( ( ( int ) pushVal1 & ( int ) pushVal2 ) , firstInstruction . Location ) } ;
687+
688+ IPeepholeOptimization . ReplaceInstructions ( input , index , 3 ,
689+ new AnnotatedBytecodeInstruction ( DreamProcOpcode . PushFloat , 1 , args ) ) ;
690+ }
691+ }
692+
621693// PushFloat [constant]
622694// PushFloat [constant]
623695// Multiply
0 commit comments