From 75464aa353ff21520f3aa83f6607964e9cca5520 Mon Sep 17 00:00:00 2001 From: Phil Garcia Date: Thu, 19 Oct 2023 07:53:30 -0700 Subject: [PATCH] Phi Branch Hoisting (#1154) --- .../InvalidOperationCompilerException.cs | 31 +++++++++ .../Mosa.Compiler.Framework/BaseTransform.cs | 4 +- Source/Mosa.Compiler.Framework/Context.cs | 11 +++- .../InstructionNode.cs | 4 +- .../Mosa.Compiler.Framework/MethodCompiler.cs | 6 +- .../Mosa.Compiler.Framework/MosaTypeLayout.cs | 2 +- Source/Mosa.Compiler.Framework/PhiHelper.cs | 1 - .../Stages/CILDecoderStage.cs | 2 +- .../Stages/EnterSSAStage.cs | 4 +- .../Transforms/LowerTo32/Lower32Transforms.cs | 3 +- .../ConstantMove/BranchManagedPointer.cs | 29 +++++++++ .../Manual/ConstantMove/BranchObject.cs | 29 +++++++++ .../Optimizations/Manual/ManualTransforms.cs | 17 ++++- .../Manual/PHI/BasePhiTransform.cs | 64 +++++++++++++++++++ .../Optimizations/Manual/PHI/Phi32Add32.cs | 2 +- .../Manual/PHI/Phi32BranchHoisting.cs | 59 +++++++++++++++++ .../Manual/PHI/Phi32Conditional.cs | 2 +- .../Optimizations/Manual/PHI/Phi32Dead.cs | 2 +- .../Optimizations/Manual/PHI/Phi32Update.cs | 4 +- .../Manual/PHI/Phi64BranchHoisting.cs | 59 +++++++++++++++++ .../Optimizations/Manual/PHI/Phi64Update.cs | 4 +- .../PHI/PhiManagedPointerBranchHoisting.cs | 59 +++++++++++++++++ .../Manual/PHI/PhiManagedPointerDead.cs | 32 ++++++++++ .../Manual/PHI/PhiManagedPointerUpdate.cs | 20 ++++++ .../Manual/PHI/PhiObjectBranchHoisting.cs | 59 +++++++++++++++++ .../Optimizations/Manual/PHI/PhiObjectDead.cs | 32 ++++++++++ .../Manual/PHI/PhiObjectUpdate.cs | 20 ++++++ .../Optimizations/Manual/PHI/PhiR4Dead.cs | 2 +- .../Optimizations/Manual/PHI/PhiR4Update.cs | 4 +- .../Optimizations/Manual/PHI/PhiR8Dead.cs | 2 +- .../Optimizations/Manual/PHI/PhiR8Update.cs | 4 +- .../MoveManagedPointerPropagateConstant.cs | 34 ++++++++++ .../Manual/Special/CombineAddressOfStore32.cs | 2 +- .../ClrTypeResolver.cs | 4 +- .../Dnlib/DnlibExtension.cs | 12 ++-- .../Dnlib/RecursionCounter.cs | 2 +- .../Metadata/ClrMetadata.cs | 2 +- .../Metadata/ClrMetadataCache.cs | 8 +-- .../Metadata/ClrMetadataLoader.cs | 16 ++--- .../Metadata/ClrMetadataResolver.cs | 20 +++--- .../Utils/GenericArgumentResolver.cs | 4 +- .../Utils/ScopedToken.cs | 2 +- .../SignatureComparer.cs | 8 +-- .../TypeSystem.cs | 2 +- .../TypeSystemController.cs | 2 +- .../Units/MosaMethod.cs | 2 +- .../Units/MosaProperty.cs | 4 +- Source/Mosa.Utility.Launcher/Starter.cs | 2 +- 48 files changed, 624 insertions(+), 75 deletions(-) create mode 100644 Source/Mosa.Compiler.Common/Exceptions/InvalidOperationCompilerException.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchManagedPointer.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchObject.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/BasePhiTransform.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32BranchHoisting.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64BranchHoisting.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerBranchHoisting.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerDead.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerUpdate.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectBranchHoisting.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectDead.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectUpdate.cs create mode 100644 Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Propagate/MoveManagedPointerPropagateConstant.cs diff --git a/Source/Mosa.Compiler.Common/Exceptions/InvalidOperationCompilerException.cs b/Source/Mosa.Compiler.Common/Exceptions/InvalidOperationCompilerException.cs new file mode 100644 index 0000000000..5782f8b1b5 --- /dev/null +++ b/Source/Mosa.Compiler.Common/Exceptions/InvalidOperationCompilerException.cs @@ -0,0 +1,31 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +using System.Diagnostics; + +namespace Mosa.Compiler.Common.Exceptions; + +[Serializable] +public class InvalidOperationCompilerException : CompilerException +{ + /// + /// Initializes a new instance of the class. + /// + public InvalidOperationCompilerException() : base() + { + var callStack = new StackFrame(1, true); + var method = callStack.GetMethod(); + + BaseMessage = $"Invalid Operation: {method.DeclaringType.Name}.{method.Name} at line {callStack.GetFileLineNumber}"; + } + + public InvalidOperationCompilerException(string message, bool includeMethod = true) : base(message) + { + if (includeMethod) + { + var callStack = new StackFrame(1, true); + var method = callStack.GetMethod(); + + BaseMessage = $"{message}: {method.DeclaringType.Name}.{method.Name} at line {callStack.GetFileLineNumber}"; + } + } +} diff --git a/Source/Mosa.Compiler.Framework/BaseTransform.cs b/Source/Mosa.Compiler.Framework/BaseTransform.cs index 1956164268..8671408226 100644 --- a/Source/Mosa.Compiler.Framework/BaseTransform.cs +++ b/Source/Mosa.Compiler.Framework/BaseTransform.cs @@ -973,7 +973,7 @@ protected static bool Compare32(ConditionCode conditionCode, Operand a, Operand ConditionCode.UnsignedGreaterOrEqual => a.ConstantUnsigned32 >= b.ConstantUnsigned32, ConditionCode.UnsignedLess => a.ConstantUnsigned32 < b.ConstantUnsigned32, ConditionCode.UnsignedLessOrEqual => a.ConstantUnsigned32 <= b.ConstantUnsigned32, - _ => throw new InvalidCompilerOperationException() + _ => throw new InvalidOperationCompilerException() }; } @@ -991,7 +991,7 @@ protected static bool Compare64(ConditionCode conditionCode, Operand a, Operand ConditionCode.UnsignedGreaterOrEqual => a.ConstantUnsigned64 >= b.ConstantUnsigned64, ConditionCode.UnsignedLess => a.ConstantUnsigned64 < b.ConstantUnsigned64, ConditionCode.UnsignedLessOrEqual => a.ConstantUnsigned64 <= b.ConstantUnsigned64, - _ => throw new InvalidCompilerOperationException() + _ => throw new InvalidOperationCompilerException() }; } diff --git a/Source/Mosa.Compiler.Framework/Context.cs b/Source/Mosa.Compiler.Framework/Context.cs index 66bf3be610..a6145f7032 100644 --- a/Source/Mosa.Compiler.Framework/Context.cs +++ b/Source/Mosa.Compiler.Framework/Context.cs @@ -1,6 +1,5 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. -using System.Collections.Generic; using System.Diagnostics; namespace Mosa.Compiler.Framework; @@ -230,6 +229,11 @@ public void GotoNext() Node = Node.Next; } + public void NextNonEmpty() + { + Node = Node.NextNonEmpty; + } + /// /// Gotos to the previous instruction. /// @@ -238,6 +242,11 @@ public void GotoPrevious() Node = Node.Previous; } + public void PreviousNonEmpty() + { + Node = Node.PreviousNonEmpty; + } + /// /// Appends an (empty) instruction after the current index. /// diff --git a/Source/Mosa.Compiler.Framework/InstructionNode.cs b/Source/Mosa.Compiler.Framework/InstructionNode.cs index 349ad31672..a4c6e95553 100644 --- a/Source/Mosa.Compiler.Framework/InstructionNode.cs +++ b/Source/Mosa.Compiler.Framework/InstructionNode.cs @@ -937,7 +937,7 @@ public InstructionNode NextNonEmpty next = next.Next; } - return next.IsBlockEndInstruction ? null : next; + return next; } } @@ -952,7 +952,7 @@ public InstructionNode PreviousNonEmpty previous = previous.Previous; } - return previous.IsBlockStartInstruction ? null : previous; + return previous; } } diff --git a/Source/Mosa.Compiler.Framework/MethodCompiler.cs b/Source/Mosa.Compiler.Framework/MethodCompiler.cs index 1bccb6cd1d..b1944d80a1 100644 --- a/Source/Mosa.Compiler.Framework/MethodCompiler.cs +++ b/Source/Mosa.Compiler.Framework/MethodCompiler.cs @@ -703,7 +703,7 @@ public void SplitOperand(Operand operand, out Operand operandLow, out Operand op return; } - throw new InvalidCompilerOperationException(); + throw new InvalidOperationCompilerException(); } /// @@ -980,7 +980,7 @@ public BaseInstruction GetLoadParamInstruction(ElementType elementType) ElementType.I when Is32BitPlatform => IRInstruction.LoadParam32, ElementType.I when Is64BitPlatform => IRInstruction.LoadParam64, ElementType.ManagedPointer => IRInstruction.LoadParamManagedPointer, - _ => throw new InvalidCompilerOperationException(), + _ => throw new InvalidOperationCompilerException(), }; } @@ -995,7 +995,7 @@ public BaseInstruction GetReturnInstruction(PrimitiveType primitiveType) PrimitiveType.Object => IRInstruction.SetReturnObject, PrimitiveType.ValueType => IRInstruction.SetReturnCompound, PrimitiveType.ManagedPointer => IRInstruction.SetReturnManagedPointer, - _ => throw new InvalidCompilerOperationException(), + _ => throw new InvalidOperationCompilerException(), }; } diff --git a/Source/Mosa.Compiler.Framework/MosaTypeLayout.cs b/Source/Mosa.Compiler.Framework/MosaTypeLayout.cs index 0a9520fe79..730f09c612 100644 --- a/Source/Mosa.Compiler.Framework/MosaTypeLayout.cs +++ b/Source/Mosa.Compiler.Framework/MosaTypeLayout.cs @@ -750,7 +750,7 @@ private MosaMethod FindInterfaceMethod(MosaType type, MosaMethod interfaceMethod if (methodFound != null) return methodFound; - throw new InvalidCompilerOperationException($"Failed to find implicit interface implementation for type {type} and interface method {interfaceMethod}"); + throw new InvalidOperationCompilerException($"Failed to find implicit interface implementation for type {type} and interface method {interfaceMethod}"); } private MosaMethod FindImplicitInterfaceMethod(MosaType type, MosaMethod interfaceMethod) diff --git a/Source/Mosa.Compiler.Framework/PhiHelper.cs b/Source/Mosa.Compiler.Framework/PhiHelper.cs index 355573d821..560d285d4c 100644 --- a/Source/Mosa.Compiler.Framework/PhiHelper.cs +++ b/Source/Mosa.Compiler.Framework/PhiHelper.cs @@ -1,6 +1,5 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. -using System.Collections.Generic; using System.Diagnostics; namespace Mosa.Compiler.Framework diff --git a/Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs b/Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs index c99689ff92..f653f1ec7b 100644 --- a/Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs @@ -1168,7 +1168,7 @@ private BaseInstruction GetLoadInstruction(MosaType type) else if ((type.IsI || type.IsN) && Is64BitPlatform) return IRInstruction.Load64; - throw new InvalidCompilerOperationException(); + throw new InvalidOperationCompilerException(); } #endregion Instruction Maps diff --git a/Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs b/Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs index d6c6c82b4a..1f9ddf1a57 100644 --- a/Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs @@ -105,7 +105,7 @@ private int WhichPredecessor(BasicBlock y, BasicBlock x) } } - throw new InvalidCompilerOperationException(); + throw new InvalidOperationCompilerException(); } #endregion Helpers Methods @@ -373,7 +373,7 @@ public static BaseInstruction GetPhiInstruction(PrimitiveType primitiveType) PrimitiveType.R8 => IRInstruction.PhiR8, PrimitiveType.Object => IRInstruction.PhiObject, PrimitiveType.ManagedPointer => IRInstruction.PhiManagedPointer, - _ => throw new InvalidCompilerOperationException(), + _ => throw new InvalidOperationCompilerException(), }; } diff --git a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/Lower32Transforms.cs b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/Lower32Transforms.cs index 312cee635d..7b89e4c46c 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/Lower32Transforms.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/Lower32Transforms.cs @@ -14,7 +14,7 @@ public static class LowerTo32Transforms new Branch64Extends(), new Compare64x32EqualOrNotEqual(), new Compare64x64EqualOrNotEqual(), - //Compare64x32UnsignedGreater(), // + //Compare64x32UnsignedGreater(), new ArithShiftRight64By32(), new ShiftRight64ByConstant32(), new ShiftRight64ByConstant32Plus(), @@ -42,7 +42,6 @@ public static class LowerTo32Transforms new ZeroExtend32x64(), new Move64(), - // LowerTo32 -- but try other transformations first! new Compare64x32Rest(), new Compare64x32RestInSSA(), new Compare64x64Rest(), diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchManagedPointer.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchManagedPointer.cs new file mode 100644 index 0000000000..8fe1f07881 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchManagedPointer.cs @@ -0,0 +1,29 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.ConstantMove; + +public sealed class BranchManagedPointer : BaseTransform +{ + public BranchManagedPointer() : base(IRInstruction.BranchManagedPointer, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (IsConstant(context.Operand2)) + return false; + + if (!IsConstant(context.Operand1)) + return false; + + if (context.Block.NextBlocks.Count == 1) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + context.SetInstruction(IRInstruction.BranchManagedPointer, context.ConditionCode.GetReverse(), context.Result, context.Operand2, context.Operand1, context.BranchTargets[0]); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchObject.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchObject.cs new file mode 100644 index 0000000000..db05d536f8 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ConstantMove/BranchObject.cs @@ -0,0 +1,29 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.ConstantMove; + +public sealed class BranchObject : BaseTransform +{ + public BranchObject() : base(IRInstruction.BranchObject, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (IsConstant(context.Operand2)) + return false; + + if (!IsConstant(context.Operand1)) + return false; + + if (context.Block.NextBlocks.Count == 1) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + context.SetInstruction(IRInstruction.BranchObject, context.ConditionCode.GetReverse(), context.Result, context.Operand2, context.Operand1, context.BranchTargets[0]); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ManualTransforms.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ManualTransforms.cs index 4cada09624..7ca751efda 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ManualTransforms.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/ManualTransforms.cs @@ -15,6 +15,8 @@ public static class ManualTransforms new ConstantMove.Compare64x64(), new ConstantMove.Branch32(), new ConstantMove.Branch64(), + new ConstantMove.BranchObject(), + new ConstantMove.BranchManagedPointer(), new ConstantMove.AddCarryOut32(), new ConstantMove.AddCarryOut64(), new ConstantMove.AddOverflowOut32(), @@ -99,7 +101,7 @@ public static class ManualTransforms new Propagate.MoveObjectPropagate(), new Propagate.MoveManagedPointerPropagate(), new Propagate.MoveObjectPropagateConstant(), - // new Propagate.MoveManagedPointerPropagateConstant(), + new Propagate.MoveManagedPointerPropagateConstant(), new Propagate.Phi32Propagate(), new Propagate.Phi64Propagate(), @@ -293,5 +295,18 @@ public static class ManualTransforms new Useless.ZeroExtend8x32Compare32x32(), new Useless.ZeroExtend8x64Compare32x64(), new Useless.ZeroExtend8x64Compare64x64(), + + new ConstantMove.BranchManagedPointer(), + new ConstantMove.BranchObject(), + + new Phi.Phi32BranchHoisting(), + new Phi.Phi64BranchHoisting(), + new Phi.PhiObjectBranchHoisting(), + new Phi.PhiManagedPointerBranchHoisting(), + + new Phi.PhiObjectDead(), + new Phi.PhiObjectUpdate(), + new Phi.PhiManagedPointerDead(), + new Phi.PhiManagedPointerUpdate(), }; } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/BasePhiTransform.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/BasePhiTransform.cs new file mode 100644 index 0000000000..332e579c79 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/BasePhiTransform.cs @@ -0,0 +1,64 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi +{ + public abstract class BasePhiTransform : BaseTransform + { + public BasePhiTransform(BaseInstruction instruction, TransformType type, bool log = false) + : base(instruction, type, log) + { } + + #region Helpers + + protected static void ReplaceBranchTarget(BasicBlock source, BasicBlock oldTarget, BasicBlock newTarget) + { + for (var node = source.BeforeLast; !node.IsBlockStartInstruction; node = node.Previous) + { + if (node.IsEmptyOrNop) + continue; + + if (node.Instruction.IsConditionalBranch || node.Instruction.IsUnconditionalBranch) + { + if (node.BranchTargets[0] == oldTarget) + { + node.UpdateBranchTarget(0, newTarget); + } + continue; + } + + break; + } + } + + #endregion Helpers + + #region Phi Helpers + + public static void UpdatePhiTarget(BasicBlock target, BasicBlock source, BasicBlock newSource) + { + PhiHelper.UpdatePhiTarget(target, source, newSource); + } + + public static void UpdatePhiTargets(List targets, BasicBlock source, BasicBlock newSource) + { + PhiHelper.UpdatePhiTargets(targets, source, newSource); + } + + public static void UpdatePhiBlocks(List phiBlocks) + { + PhiHelper.UpdatePhiBlocks(phiBlocks); + } + + public static void UpdatePhiBlock(BasicBlock phiBlock) + { + PhiHelper.UpdatePhiBlock(phiBlock); + } + + public static void UpdatePhi(InstructionNode node) + { + PhiHelper.UpdatePhi(node); + } + + #endregion Phi Helpers + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Add32.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Add32.cs index e823ae347b..f85cb9c015 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Add32.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Add32.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class Phi32Add32 : BaseTransform +public sealed class Phi32Add32 : BasePhiTransform { public Phi32Add32() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32BranchHoisting.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32BranchHoisting.cs new file mode 100644 index 0000000000..704ac0a4ee --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32BranchHoisting.cs @@ -0,0 +1,59 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class Phi32BranchHoisting : BasePhiTransform +{ + public Phi32BranchHoisting() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.Block.PreviousBlocks.Count != 2 || context.Block.NextBlocks.Count != 2) + return false; + + if (!context.Result.IsUsedOnce) + return false; + + if (!context.Result.IsDefinedOnce) + return false; + + if (!(context.Operand1.IsResolvedConstant || context.Operand2.IsResolvedConstant)) + return false; + + var ctx = context.Result.Uses[0]; + + if (ctx.Instruction != IRInstruction.Branch32) + return false; + + if (!ctx.Operand2.IsResolvedConstant) + return false; + + if (!context.Node.PreviousNonEmpty.IsBlockStartInstruction) + return false; + + if (context.Node.NextNonEmpty != ctx) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var phiValue = context.Operand1.IsResolvedConstant ? context.Operand1 : context.Operand2; + var incomingBlock = context.PhiBlocks[context.Operand1.IsResolvedConstant ? 0 : 1]; + + var ctx = context.Result.Uses[0]; + + var result = Compare32(ctx.ConditionCode, phiValue, ctx.Operand2); + + var targetBlock = result + ? ctx.BranchTargets[0] + : ctx.NextNonEmpty.BranchTargets[0]; + + ReplaceBranchTarget(incomingBlock, ctx.Block, targetBlock); + + UpdatePhiBlock(context.Block); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Conditional.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Conditional.cs index 034f1e5108..984bd463f0 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Conditional.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Conditional.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class Phi32Conditional : BaseTransform +public sealed class Phi32Conditional : BasePhiTransform { public Phi32Conditional() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Dead.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Dead.cs index 91d0095e09..a8107645dc 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Dead.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Dead.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class Phi32Dead : BaseTransform +public sealed class Phi32Dead : BasePhiTransform { public Phi32Dead() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Update.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Update.cs index 47200d475e..f865b385d9 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Update.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi32Update.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class Phi32Update : BaseTransform +public sealed class Phi32Update : BasePhiTransform { public Phi32Update() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { @@ -15,6 +15,6 @@ public override bool Match(Context context, TransformContext transform) public override void Transform(Context context, TransformContext transform) { - TransformContext.UpdatePhi(context.Node); + UpdatePhi(context.Node); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64BranchHoisting.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64BranchHoisting.cs new file mode 100644 index 0000000000..6e034ef710 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64BranchHoisting.cs @@ -0,0 +1,59 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class Phi64BranchHoisting : BasePhiTransform +{ + public Phi64BranchHoisting() : base(IRInstruction.Phi64, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.Block.PreviousBlocks.Count != 2 || context.Block.NextBlocks.Count != 2) + return false; + + if (!context.Result.IsUsedOnce) + return false; + + if (!context.Result.IsDefinedOnce) + return false; + + if (!(context.Operand1.IsResolvedConstant || context.Operand2.IsResolvedConstant)) + return false; + + var ctx = context.Result.Uses[0]; + + if (ctx.Instruction != IRInstruction.Branch64) + return false; + + if (!ctx.Operand2.IsResolvedConstant) + return false; + + if (!context.Node.PreviousNonEmpty.IsBlockStartInstruction) + return false; + + if (context.Node.NextNonEmpty != ctx) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var phiValue = context.Operand1.IsResolvedConstant ? context.Operand1 : context.Operand2; + var incomingBlock = context.PhiBlocks[context.Operand1.IsResolvedConstant ? 0 : 1]; + + var ctx = context.Result.Uses[0]; + + var result = Compare64(ctx.ConditionCode, phiValue, ctx.Operand2); + + var targetBlock = result + ? ctx.BranchTargets[0] + : ctx.NextNonEmpty.BranchTargets[0]; + + ReplaceBranchTarget(incomingBlock, ctx.Block, targetBlock); + + UpdatePhiBlock(context.Block); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64Update.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64Update.cs index bbc6ad4d7d..60d7c2184e 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64Update.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/Phi64Update.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class Phi64Update : BaseTransform +public sealed class Phi64Update : BasePhiTransform { public Phi64Update() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { @@ -15,6 +15,6 @@ public override bool Match(Context context, TransformContext transform) public override void Transform(Context context, TransformContext transform) { - TransformContext.UpdatePhi(context.Node); + UpdatePhi(context.Node); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerBranchHoisting.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerBranchHoisting.cs new file mode 100644 index 0000000000..4972fc5145 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerBranchHoisting.cs @@ -0,0 +1,59 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiManagedPointerBranchHoisting : BasePhiTransform +{ + public PhiManagedPointerBranchHoisting() : base(IRInstruction.PhiManagedPointer, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.Block.PreviousBlocks.Count != 2 || context.Block.NextBlocks.Count != 2) + return false; + + if (!context.Result.IsUsedOnce) + return false; + + if (!context.Result.IsDefinedOnce) + return false; + + if (!(context.Operand1.IsResolvedConstant || context.Operand2.IsResolvedConstant)) + return false; + + var ctx = context.Result.Uses[0]; + + if (ctx.Instruction != IRInstruction.BranchManagedPointer) + return false; + + if (!ctx.Operand2.IsResolvedConstant) + return false; + + if (!context.Node.PreviousNonEmpty.IsBlockStartInstruction) + return false; + + if (context.Node.NextNonEmpty != ctx) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var phiValue = context.Operand1.IsResolvedConstant ? context.Operand1 : context.Operand2; + var incomingBlock = context.PhiBlocks[context.Operand1.IsResolvedConstant ? 0 : 1]; + + var ctx = context.Result.Uses[0]; + + var result = Compare64(ctx.ConditionCode, phiValue, ctx.Operand2); + + var targetBlock = result + ? ctx.BranchTargets[0] + : ctx.NextNonEmpty.BranchTargets[0]; + + ReplaceBranchTarget(incomingBlock, ctx.Block, targetBlock); + + UpdatePhiBlock(context.Block); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerDead.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerDead.cs new file mode 100644 index 0000000000..a14af30c4f --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerDead.cs @@ -0,0 +1,32 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiManagedPointerDead : BaseTransform +{ + public PhiManagedPointerDead() : base(IRInstruction.PhiManagedPointer, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.ResultCount == 0) + return true; + + var result = context.Result; + var node = context.Node; + + foreach (var use in result.Uses) + { + if (use != node) + return false; + } + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + context.SetNop(); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerUpdate.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerUpdate.cs new file mode 100644 index 0000000000..22fa3cab10 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiManagedPointerUpdate.cs @@ -0,0 +1,20 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiManagedPointerUpdate : BasePhiTransform +{ + public PhiManagedPointerUpdate() : base(IRInstruction.PhiManagedPointer, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + return context.OperandCount != context.Block.PreviousBlocks.Count; + } + + public override void Transform(Context context, TransformContext transform) + { + UpdatePhi(context.Node); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectBranchHoisting.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectBranchHoisting.cs new file mode 100644 index 0000000000..1932533949 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectBranchHoisting.cs @@ -0,0 +1,59 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiObjectBranchHoisting : BasePhiTransform +{ + public PhiObjectBranchHoisting() : base(IRInstruction.PhiObject, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.Block.PreviousBlocks.Count != 2 || context.Block.NextBlocks.Count != 2) + return false; + + if (!context.Result.IsUsedOnce) + return false; + + if (!context.Result.IsDefinedOnce) + return false; + + if (!(context.Operand1.IsResolvedConstant || context.Operand2.IsResolvedConstant)) + return false; + + var ctx = context.Result.Uses[0]; + + if (ctx.Instruction != IRInstruction.BranchObject) + return false; + + if (!ctx.Operand2.IsResolvedConstant) + return false; + + if (!context.Node.PreviousNonEmpty.IsBlockStartInstruction) + return false; + + if (context.Node.NextNonEmpty != ctx) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var phiValue = context.Operand1.IsResolvedConstant ? context.Operand1 : context.Operand2; + var incomingBlock = context.PhiBlocks[context.Operand1.IsResolvedConstant ? 0 : 1]; + + var ctx = context.Result.Uses[0]; + + var result = Compare64(ctx.ConditionCode, phiValue, ctx.Operand2); + + var targetBlock = result + ? ctx.BranchTargets[0] + : ctx.NextNonEmpty.BranchTargets[0]; + + ReplaceBranchTarget(incomingBlock, ctx.Block, targetBlock); + + UpdatePhiBlock(context.Block); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectDead.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectDead.cs new file mode 100644 index 0000000000..5423d133b5 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectDead.cs @@ -0,0 +1,32 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiObjectDead : BaseTransform +{ + public PhiObjectDead() : base(IRInstruction.PhiObject, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (context.ResultCount == 0) + return true; + + var result = context.Result; + var node = context.Node; + + foreach (var use in result.Uses) + { + if (use != node) + return false; + } + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + context.SetNop(); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectUpdate.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectUpdate.cs new file mode 100644 index 0000000000..97483b5c51 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiObjectUpdate.cs @@ -0,0 +1,20 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; + +public sealed class PhiObjectUpdate : BasePhiTransform +{ + public PhiObjectUpdate() : base(IRInstruction.PhiObject, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + return context.OperandCount != context.Block.PreviousBlocks.Count; + } + + public override void Transform(Context context, TransformContext transform) + { + UpdatePhi(context.Node); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Dead.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Dead.cs index d502a90844..c52c42ddc6 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Dead.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Dead.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class PhiR4Dead : BaseTransform +public sealed class PhiR4Dead : BasePhiTransform { public PhiR4Dead() : base(IRInstruction.PhiR4, TransformType.Manual | TransformType.Optimization) { diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Update.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Update.cs index 4a911ac062..94740507ef 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Update.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR4Update.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class PhiR4Update : BaseTransform +public sealed class PhiR4Update : BasePhiTransform { public PhiR4Update() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { @@ -15,6 +15,6 @@ public override bool Match(Context context, TransformContext transform) public override void Transform(Context context, TransformContext transform) { - TransformContext.UpdatePhi(context.Node); + UpdatePhi(context.Node); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Dead.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Dead.cs index 4225024929..0c3046060d 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Dead.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Dead.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class PhiR8Dead : BaseTransform +public sealed class PhiR8Dead : BasePhiTransform { public PhiR8Dead() : base(IRInstruction.PhiR8, TransformType.Manual | TransformType.Optimization) { diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Update.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Update.cs index c04d51b4ac..b44cb8233b 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Update.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/PHI/PhiR8Update.cs @@ -2,7 +2,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Phi; -public sealed class PhiR8Update : BaseTransform +public sealed class PhiR8Update : BasePhiTransform { public PhiR8Update() : base(IRInstruction.Phi32, TransformType.Manual | TransformType.Optimization) { @@ -15,6 +15,6 @@ public override bool Match(Context context, TransformContext transform) public override void Transform(Context context, TransformContext transform) { - TransformContext.UpdatePhi(context.Node); + UpdatePhi(context.Node); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Propagate/MoveManagedPointerPropagateConstant.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Propagate/MoveManagedPointerPropagateConstant.cs new file mode 100644 index 0000000000..1791b9bc42 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Propagate/MoveManagedPointerPropagateConstant.cs @@ -0,0 +1,34 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.Propagate; + +public sealed class MoveManagedPointerPropagateConstant : BaseTransform +{ + public MoveManagedPointerPropagateConstant() : base(IRInstruction.MoveManagedPointer, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (!context.Result.IsDefinedOnce) + return false; + + if (!context.Operand1.IsResolvedConstant) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var result = context.Result; + var operand1 = context.Operand1; + + foreach (var use in result.Uses.ToArray()) + { + use.ReplaceOperand(result, operand1); + } + + context.SetNop(); + } +} diff --git a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Special/CombineAddressOfStore32.cs b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Special/CombineAddressOfStore32.cs index 198dd785a9..feaf07e3f2 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Special/CombineAddressOfStore32.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Optimizations/Manual/Special/CombineAddressOfStore32.cs @@ -35,7 +35,7 @@ public override bool Match(Context context, TransformContext transform) var store2 = context.Node.PreviousNonEmpty; - if (store2 == null) + if (store2.IsBlockStartInstruction) return false; if (store2.Instruction != IRInstruction.Store32) diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/ClrTypeResolver.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/ClrTypeResolver.cs index 39176e27dd..a95b8e0f15 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/ClrTypeResolver.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/ClrTypeResolver.cs @@ -24,7 +24,7 @@ public MosaType ResolveType(MosaModule module, BuiltInType type) var resolvedType = GetTypeByName(module, typeName); if (resolvedType == null) - throw new InvalidCompilerOperationException($"Type {typeName} is null or does not exist!"); + throw new InvalidOperationCompilerException($"Type {typeName} is null or does not exist!"); return resolvedType; } @@ -35,7 +35,7 @@ public MosaType ResolveType(MosaModule module, MosaTypeCode type) var resolvedType = GetTypeByName(module, typeName); if (resolvedType == null) - throw new InvalidCompilerOperationException($"Type {typeName} is null or does not exist!"); + throw new InvalidOperationCompilerException($"Type {typeName} is null or does not exist!"); return resolvedType; } diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/DnlibExtension.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/DnlibExtension.cs index 0c1632518a..2b45aa395b 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/DnlibExtension.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/DnlibExtension.cs @@ -96,22 +96,22 @@ public static bool HasModifierOrPinned(this TypeSig signature) public static TypeSig GetTypeSig(this MosaType type) { - return type.GetUnderlyingObject>()?.Signature ?? throw new InvalidCompilerOperationException("Type signature is null!"); + return type.GetUnderlyingObject>()?.Signature ?? throw new InvalidOperationCompilerException("Type signature is null!"); } public static MethodSig GetMethodSig(this MosaMethod method) { - return method.GetUnderlyingObject>()?.Signature ?? throw new InvalidCompilerOperationException("Method signature is null!"); + return method.GetUnderlyingObject>()?.Signature ?? throw new InvalidOperationCompilerException("Method signature is null!"); } public static FieldSig GetFieldSig(this MosaField field) { - return field.GetUnderlyingObject>()?.Signature ?? throw new InvalidCompilerOperationException("Field signature is null!"); + return field.GetUnderlyingObject>()?.Signature ?? throw new InvalidOperationCompilerException("Field signature is null!"); } public static PropertySig GetPropertySig(this MosaProperty property) { - return property.GetUnderlyingObject>()?.Signature ?? throw new InvalidCompilerOperationException("Property signature is null!"); + return property.GetUnderlyingObject>()?.Signature ?? throw new InvalidOperationCompilerException("Property signature is null!"); } public static IList GetGenericArguments(this IReadOnlyList types) @@ -122,7 +122,7 @@ public static IList GetGenericArguments(this IReadOnlyList ty { var typeSig = type.GetTypeSig(); if (typeSig == null) - throw new InvalidCompilerOperationException("Type signature of type is null!"); + throw new InvalidOperationCompilerException("Type signature of type is null!"); result.Add(typeSig); } @@ -138,7 +138,7 @@ public static IList GetGenericArguments(this IList types) { var typeSig = type.GetTypeSig(); if (typeSig == null) - throw new InvalidCompilerOperationException("Type signature of type is null!"); + throw new InvalidOperationCompilerException("Type signature of type is null!"); result.Add(typeSig); } diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/RecursionCounter.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/RecursionCounter.cs index acda9e7878..a80bc87f7c 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/RecursionCounter.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Dnlib/RecursionCounter.cs @@ -46,7 +46,7 @@ public void Decrement() { #if DEBUG if (Counter <= 0) - throw new InvalidCompilerOperationException("recursionCounter <= 0"); + throw new InvalidOperationCompilerException("recursionCounter <= 0"); #endif Counter--; } diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadata.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadata.cs index d5aa36b1e9..4d8aaa2926 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadata.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadata.cs @@ -52,7 +52,7 @@ public void LoadMetadata() var modules = Cache?.Modules.Values; if (modules == null) - throw new InvalidCompilerOperationException("Modules list is empty!"); + throw new InvalidOperationCompilerException("Modules list is empty!"); foreach (var module in modules) { diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataCache.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataCache.cs index 2035500498..0d69e3c9a4 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataCache.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataCache.cs @@ -43,7 +43,7 @@ public void AddType(MosaType type) { var unitDesc = type.GetUnderlyingObject>(); if (unitDesc == null) - throw new InvalidCompilerOperationException("Underlying object (unit description) of type is null!"); + throw new InvalidOperationCompilerException("Underlying object (unit description) of type is null!"); typeLookup.Add(unitDesc.Token, type); } @@ -57,7 +57,7 @@ public void AddMethod(MosaMethod method) { var unitDesc = method.GetUnderlyingObject>(); if (unitDesc == null) - throw new InvalidCompilerOperationException("Underlying object (unit description) of method is null!"); + throw new InvalidOperationCompilerException("Underlying object (unit description) of method is null!"); methodLookup.Add(unitDesc.Token, method); } @@ -71,7 +71,7 @@ public void AddField(MosaField field) { var unitDesc = field.GetUnderlyingObject>(); if (unitDesc == null) - throw new InvalidCompilerOperationException("Underlying object (unit description) of field is null!"); + throw new InvalidOperationCompilerException("Underlying object (unit description) of field is null!"); fieldLookup.Add(unitDesc.Token, field); } @@ -85,7 +85,7 @@ public void AddProperty(MosaProperty property) { var unitDesc = property.GetUnderlyingObject>(); if (unitDesc == null) - throw new InvalidCompilerOperationException("Underlying object (unit description) of property is null!"); + throw new InvalidOperationCompilerException("Underlying object (unit description) of property is null!"); propertyLookup.Add(unitDesc.Token, property); } diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataLoader.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataLoader.cs index 73add64e20..ba9bf572a8 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataLoader.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Metadata/ClrMetadataLoader.cs @@ -302,7 +302,7 @@ private MosaType Load(TypeSig typeSig) case ElementType.Ptr: result = elementType?.ToUnmanagedPointer(); if (result == null) - throw new InvalidCompilerOperationException("Unmanaged pointer of element type is null!"); + throw new InvalidOperationCompilerException("Unmanaged pointer of element type is null!"); using (var ptrType = metadata.Controller.MutateType(result)) ptrType.UnderlyingObject = elementType?.GetUnderlyingObject>()?.Clone(typeSig); @@ -311,7 +311,7 @@ private MosaType Load(TypeSig typeSig) case ElementType.ByRef: result = elementType?.ToManagedPointer(); if (result == null) - throw new InvalidCompilerOperationException("Unmanaged pointer of element type is null!"); + throw new InvalidOperationCompilerException("Unmanaged pointer of element type is null!"); using (var ptrType = metadata.Controller.MutateType(result)) ptrType.UnderlyingObject = elementType?.GetUnderlyingObject>()?.Clone(typeSig); @@ -335,7 +335,7 @@ private MosaType Load(TypeSig typeSig) case ElementType.SZArray: result = elementType?.ToSZArray(); if (result == null) - throw new InvalidCompilerOperationException("SZ array of element type is null!"); + throw new InvalidOperationCompilerException("SZ array of element type is null!"); using (var arrayType = metadata.Controller.MutateType(result)) { @@ -348,7 +348,7 @@ private MosaType Load(TypeSig typeSig) GetType(new GenericInstSig(iListSig, typeSig.Next)); if (szHelperMethods == null) - throw new InvalidCompilerOperationException("szHelperMethods array is null!"); + throw new InvalidOperationCompilerException("szHelperMethods array is null!"); foreach (var method in szHelperMethods) { @@ -367,7 +367,7 @@ private MosaType Load(TypeSig typeSig) var arrayInfo = new MosaArrayInfo(array.LowerBounds, array.Rank, array.Sizes); result = elementType?.ToArray(arrayInfo); if (result == null) - throw new InvalidCompilerOperationException("Array of element type is null!"); + throw new InvalidOperationCompilerException("Array of element type is null!"); using (var arrayType = metadata.Controller.MutateType(result)) arrayType.UnderlyingObject = elementType?.GetUnderlyingObject>()?.Clone(typeSig); @@ -463,7 +463,7 @@ private MosaType LoadGenericTypeInstanceSig(GenericInstSig typeSig) var propertySig = property.GetPropertySig(); if (propertySig == null) - throw new InvalidCompilerOperationException("Property signature is null!"); + throw new InvalidOperationCompilerException("Property signature is null!"); var newSig = propertySig.Clone(); newSig.RetType = resolver.Resolve(newSig.RetType); @@ -494,7 +494,7 @@ public MosaMethod LoadGenericMethodInstance(IMethodDefOrRef method, IList>()?.Definition; if (typeDef == null) - throw new InvalidCompilerOperationException("Definition of type is null!"); + throw new InvalidOperationCompilerException("Definition of type is null!"); if (typeDef.BaseType != null) { @@ -115,7 +115,7 @@ public void Resolve() var definition = module.GetUnderlyingObject>()?.Definition; if (definition == null) - throw new InvalidCompilerOperationException("Module's definition is null!"); + throw new InvalidOperationCompilerException("Module's definition is null!"); ResolveCustomAttributes(mosaModule, definition); break; @@ -259,7 +259,7 @@ private void ResolveType(MosaType type) var definition = srcType.GetUnderlyingObject>()?.Definition; if (definition == null) - throw new InvalidCompilerOperationException("Source type definition is null!"); + throw new InvalidOperationCompilerException("Source type definition is null!"); ResolveCustomAttributes(mosaType, definition); } @@ -285,7 +285,7 @@ private void ResolveField(MosaField field) var definition = field.GetUnderlyingObject>()?.Definition; if (definition == null) - throw new InvalidCompilerOperationException("Field definition is null!"); + throw new InvalidOperationCompilerException("Field definition is null!"); ResolveCustomAttributes(mosaField, definition); } @@ -304,7 +304,7 @@ private void ResolveProperty(MosaProperty property) var definition = property.GetUnderlyingObject>()?.Definition; if (definition == null) - throw new InvalidCompilerOperationException("Property definition is null!"); + throw new InvalidOperationCompilerException("Property definition is null!"); ResolveCustomAttributes(mosaProperty, definition); } @@ -312,7 +312,7 @@ private void ResolveProperty(MosaProperty property) private void ResolveMethod(MosaMethod method) { if (method.DeclaringType == null) - throw new InvalidCompilerOperationException("Method's declaring type is null!"); + throw new InvalidOperationCompilerException("Method's declaring type is null!"); var resolver = new GenericArgumentResolver(); var hasOpening = method.DeclaringType.HasOpenGenericParams; @@ -341,10 +341,10 @@ private void ResolveMethod(MosaMethod method) var desc = method.GetUnderlyingObject>(); if (desc == null) - throw new InvalidCompilerOperationException("Underlying object for method is null!"); + throw new InvalidOperationCompilerException("Underlying object for method is null!"); if (desc.Signature == null) - throw new InvalidCompilerOperationException("Signature for unit description is null!"); + throw new InvalidOperationCompilerException("Signature for unit description is null!"); var returnType = metadata.Loader.GetType(resolver.Resolve(desc.Signature.RetType)); hasOpening |= returnType.HasOpenGenericParams; @@ -631,7 +631,7 @@ private void ResolveSZArray(MosaType? arrayType) var szHelper = typeSystem.GetTypeByName(typeSystem.CorLib, "System.Array+SZArrayHelper"); if (szHelper == null) - throw new InvalidCompilerOperationException("Type is null or does not exist"); + throw new InvalidOperationCompilerException("Type is null or does not exist"); using var type = typeSystem.Controller.MutateType(arrayType); using var szHelperType = typeSystem.Controller.MutateType(szHelper); @@ -662,7 +662,7 @@ private void ResolveSZArray(MosaType? arrayType) foreach (var iface in list) { if (iface == null) - throw new InvalidCompilerOperationException("Interface is null or does not exist"); + throw new InvalidOperationCompilerException("Interface is null or does not exist"); type.Interfaces.Add(iface); foreach (var property in iface.Properties) diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/GenericArgumentResolver.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/GenericArgumentResolver.cs index e7cbecf41b..b1be2ed89c 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/GenericArgumentResolver.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/GenericArgumentResolver.cs @@ -124,7 +124,7 @@ private MethodSig ResolveGenericArgs(MethodSig sig, MethodSig old) private TypeSig ResolveGenericArgs(TypeSig typeSig) { if (!recursionCounter.Increment()) - throw new InvalidCompilerOperationException("Could not increment recursion counter!"); + throw new InvalidOperationCompilerException("Could not increment recursion counter!"); if (ReplaceGenericArg(ref typeSig)) { @@ -149,7 +149,7 @@ private TypeSig ResolveGenericArgs(TypeSig typeSig) var fnPtrSig = typeSig as FnPtrSig; if (fnPtrSig?.Signature is null) - throw new InvalidCompilerOperationException("Function pointer signature is null"); + throw new InvalidOperationCompilerException("Function pointer signature is null"); result = new FnPtrSig(fnPtrSig.Signature); break; diff --git a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/ScopedToken.cs b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/ScopedToken.cs index 6624503f7c..6e6bc56a81 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/ScopedToken.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem.CLR/Utils/ScopedToken.cs @@ -32,7 +32,7 @@ public override bool Equals(object? obj) public override int GetHashCode() { if (Module == null) - throw new InvalidCompilerOperationException("Module is null!"); + throw new InvalidOperationCompilerException("Module is null!"); return Module.GetHashCode() * 7 + (int)Token.Raw; } diff --git a/Source/Mosa.Compiler.MosaTypeSystem/SignatureComparer.cs b/Source/Mosa.Compiler.MosaTypeSystem/SignatureComparer.cs index bd62091546..653f90f14e 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem/SignatureComparer.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem/SignatureComparer.cs @@ -63,14 +63,14 @@ public static int GetHashCode(MosaType type) // Hash code DOES not need to be unique, so to save time ArrayInfo is not hashed case MosaTypeCode.Array: if (type.ElementType == null) - throw new InvalidCompilerOperationException("Element type of type is null"); + throw new InvalidOperationCompilerException("Element type of type is null"); result += result * 7 + GetHashCode(type.ElementType); break; case MosaTypeCode.FunctionPointer: if (type.FunctionPtrSig == null) - throw new InvalidCompilerOperationException("Function pointer signature of type is null"); + throw new InvalidOperationCompilerException("Function pointer signature of type is null"); result += GetHashCode(type.FunctionPtrSig); break; @@ -101,14 +101,14 @@ bool IEqualityComparer.Equals(MosaMethodSignature? x, MosaM public static int GetHashCode(MosaMethodSignature method) { if (method.ReturnType == null) - throw new InvalidCompilerOperationException("Return type of method is null"); + throw new InvalidOperationCompilerException("Return type of method is null"); var result = GetHashCode(method.ReturnType); foreach (var param in method.Parameters) { if (param.ParameterType == null) - throw new InvalidCompilerOperationException("Type of parameter is null"); + throw new InvalidOperationCompilerException("Type of parameter is null"); result += result * 7 + GetHashCode(param.ParameterType); } diff --git a/Source/Mosa.Compiler.MosaTypeSystem/TypeSystem.cs b/Source/Mosa.Compiler.MosaTypeSystem/TypeSystem.cs index fe75df28f5..7520b7fe8a 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem/TypeSystem.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem/TypeSystem.cs @@ -43,7 +43,7 @@ public IEnumerable AllTypes { var types = module?.Types.Values; if (types == null) - throw new InvalidCompilerOperationException("Types list is null"); + throw new InvalidOperationCompilerException("Types list is null"); foreach (var type in types) { diff --git a/Source/Mosa.Compiler.MosaTypeSystem/TypeSystemController.cs b/Source/Mosa.Compiler.MosaTypeSystem/TypeSystemController.cs index 769d832561..11df870fbb 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem/TypeSystemController.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem/TypeSystemController.cs @@ -151,7 +151,7 @@ public void AddModule(MosaModule module) public void AddType(MosaType type) { if (type.Module == null || type.FullName == null) - throw new InvalidCompilerOperationException("Type's module or full name is null"); + throw new InvalidOperationCompilerException("Type's module or full name is null"); if (!type.Module.Types.ContainsKey(type.ID)) type.Module.Types.Add(type.ID, type); diff --git a/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaMethod.cs b/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaMethod.cs index c64210090c..eaf09daf81 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaMethod.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaMethod.cs @@ -206,7 +206,7 @@ public bool Equals(MosaMethod? x, MosaMethod? y) public int GetHashCode(MosaMethod obj) { if (obj.FullName == null) - throw new InvalidCompilerOperationException("Full name of method is null"); + throw new InvalidOperationCompilerException("Full name of method is null"); return obj.FullName.GetHashCode(); } diff --git a/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaProperty.cs b/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaProperty.cs index 5d4270a4ab..8da44efd17 100644 --- a/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaProperty.cs +++ b/Source/Mosa.Compiler.MosaTypeSystem/Units/MosaProperty.cs @@ -19,7 +19,7 @@ public MosaMethod? GetterMethod get { if (GetterMethodName == null) - throw new InvalidCompilerOperationException("Method name is null, unable to get getter method"); + throw new InvalidOperationCompilerException("Method name is null, unable to get getter method"); return DeclaringType?.FindMethodByName(GetterMethodName); } @@ -32,7 +32,7 @@ public MosaMethod? SetterMethod get { if (SetterMethodName == null) - throw new InvalidCompilerOperationException("Method name is null, unable to get setter method"); + throw new InvalidOperationCompilerException("Method name is null, unable to get setter method"); return DeclaringType?.FindMethodByName(SetterMethodName); } diff --git a/Source/Mosa.Utility.Launcher/Starter.cs b/Source/Mosa.Utility.Launcher/Starter.cs index 41aadd6271..4c961429df 100644 --- a/Source/Mosa.Utility.Launcher/Starter.cs +++ b/Source/Mosa.Utility.Launcher/Starter.cs @@ -244,7 +244,7 @@ private bool StartSerialMonitor(Process process) "bochs" => LaunchBochs(), "vmware" => LaunchVMware(), "virtualbox" => LaunchVirtualBox(), - _ => throw new InvalidCompilerOperationException() + _ => throw new InvalidOperationCompilerException() }; private Process LaunchQemu()