From b215b2eacc1e16ad0256b0696d6d2f29cce6fd3f Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 27 Oct 2024 21:48:09 -0700 Subject: [PATCH 1/7] - Preparing for SafePoints --- Source/Mosa.Compiler.ARM32/Architecture.cs | 26 ++-- Source/Mosa.Compiler.ARM64/Architecture.cs | 26 ++-- .../Analysis/LoopDetector.cs | 28 ++++ Source/Mosa.Compiler.Framework/Compiler.cs | 137 ++++++++++-------- .../Mosa.Compiler.Framework/CompilerHooks.cs | 3 +- Source/Mosa.Compiler.Framework/Operand.cs | 2 +- .../Stages/LoopInvariantCodeMotionStage.cs | 33 +---- .../Stages/SafePointStage.cs | 25 +++- Source/Mosa.Compiler.x64/Architecture.cs | 26 ++-- Source/Mosa.Compiler.x86/Architecture.cs | 26 ++-- .../MainWindow.axaml.cs | 2 +- Source/Mosa.Tool.Explorer/MainForm.cs | 2 +- 12 files changed, 178 insertions(+), 158 deletions(-) diff --git a/Source/Mosa.Compiler.ARM32/Architecture.cs b/Source/Mosa.Compiler.ARM32/Architecture.cs index 4997736453..80c0d2018b 100644 --- a/Source/Mosa.Compiler.ARM32/Architecture.cs +++ b/Source/Mosa.Compiler.ARM32/Architecture.cs @@ -162,24 +162,18 @@ public override void ExtendMethodCompilerPipeline(Pipeline( - new BaseMethodCompilerStage[] - { - new AdvanceTransformStage(), - new IRTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - new PlatformTransformStage(), - }); + [ + new AdvanceTransformStage(), + new IRTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + new PlatformTransformStage(), + ]); pipeline.InsertBefore( - new BaseMethodCompilerStage[] - { - new PlatformTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - }); - - pipeline.InsertBefore( - new JumpOptimizationStage() - ); + [ + new PlatformTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + ]); //pipeline.InsertBefore( // new StopStage() diff --git a/Source/Mosa.Compiler.ARM64/Architecture.cs b/Source/Mosa.Compiler.ARM64/Architecture.cs index 2b37a744db..2fd612342f 100644 --- a/Source/Mosa.Compiler.ARM64/Architecture.cs +++ b/Source/Mosa.Compiler.ARM64/Architecture.cs @@ -187,27 +187,21 @@ public override void ExtendCompilerPipeline(Pipeline pipeline public override void ExtendMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) { pipeline.InsertBefore( - new Stages.RuntimeCallStage() + new RuntimeCallStage() ); pipeline.InsertAfterLast( - new BaseMethodCompilerStage[] - { - new IRTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - new PlatformTransformStage(), - }); + [ + new IRTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + new PlatformTransformStage(), + ]); pipeline.InsertBefore( - new BaseMethodCompilerStage[] - { - new PlatformTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - }); - - pipeline.InsertBefore( - new JumpOptimizationStage() - ); + [ + new PlatformTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + ]); //pipeline.InsertBefore( // new StopStage() diff --git a/Source/Mosa.Compiler.Framework/Analysis/LoopDetector.cs b/Source/Mosa.Compiler.Framework/Analysis/LoopDetector.cs index 71322620d5..7bb7e875dc 100644 --- a/Source/Mosa.Compiler.Framework/Analysis/LoopDetector.cs +++ b/Source/Mosa.Compiler.Framework/Analysis/LoopDetector.cs @@ -1,5 +1,6 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. +using System.Text; using Mosa.Compiler.Framework.Common; namespace Mosa.Compiler.Framework.Analysis; @@ -85,4 +86,31 @@ private static void PopulateLoopNodes(BasicBlocks basicBlocks, Loop loop) } } } + + public static void DumpLoops(TraceLog loopTrace, List loops) + { + if (loopTrace == null) + return; + + foreach (var loop in loops) + { + loopTrace.Log($"Header: {loop.Header}"); + foreach (var backedge in loop.Backedges) + { + loopTrace.Log($" Backedge: {backedge}"); + } + + var sb = new StringBuilder(); + + foreach (var block in loop.LoopBlocks) + { + sb.Append(block); + sb.Append(", "); + } + + sb.Length -= 2; + + loopTrace.Log($" Members: {sb}"); + } + } } diff --git a/Source/Mosa.Compiler.Framework/Compiler.cs b/Source/Mosa.Compiler.Framework/Compiler.cs index 5eb29149d5..d5031e91c1 100644 --- a/Source/Mosa.Compiler.Framework/Compiler.cs +++ b/Source/Mosa.Compiler.Framework/Compiler.cs @@ -145,64 +145,78 @@ public sealed class Compiler !string.IsNullOrEmpty(mosaSettings.InlinedFile) ? new InlinedFileStage() : null, }; - private static List GetDefaultMethodPipeline(MosaSettings mosaSettings) => new List + private static void InitializeMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) { - new CILDecoderStage(), - new ExceptionStage(), - new FastBlockOrderingStage(), - mosaSettings.Devirtualization ? new DevirtualizeCallStage() : null, - mosaSettings.BasicOptimizations ? new OptimizationStage(false) : null, - new IRTransformsStage(), - new PlugStage(), - new RuntimeStage(), - - mosaSettings.InlineMethods || mosaSettings.InlineExplicit ? new InlineStage() : null, - - mosaSettings.BasicOptimizations ? new OptimizationStage(false) : null, - mosaSettings.SSA ? new EdgeSplitStage() : null, - mosaSettings.SSA ? new EnterSSAStage() : null, - mosaSettings.BasicOptimizations && mosaSettings.SSA ? new OptimizationStage(false) : null, - - mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null, - mosaSettings.LoopInvariantCodeMotion && mosaSettings.SSA ? new LoopInvariantCodeMotionStage() : null, - mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null, - mosaSettings.BasicOptimizations && mosaSettings.SSA && (mosaSettings.ValueNumbering || mosaSettings.LoopInvariantCodeMotion || mosaSettings.SparseConditionalConstantPropagation) ? new OptimizationStage(false) : null, - mosaSettings.BitTracker ? new BitTrackerStage() : null, - mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null, - mosaSettings.BasicOptimizations ? new OptimizationStage(mosaSettings.LongExpansion) : null, - - mosaSettings.TwoPassOptimization && mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null, - mosaSettings.TwoPassOptimization && mosaSettings.LoopInvariantCodeMotion && mosaSettings.SSA ? new LoopInvariantCodeMotionStage() : null, - mosaSettings.TwoPassOptimization && mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null, - mosaSettings.TwoPassOptimization && mosaSettings.BitTracker ? new BitTrackerStage() : null, - mosaSettings.TwoPassOptimization && mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null, - mosaSettings.TwoPassOptimization && mosaSettings.BasicOptimizations && mosaSettings.SSA ? new OptimizationStage(mosaSettings.LongExpansion) : null, - - new NopRemovalStage(), - new FastBlockOrderingStage(), - - mosaSettings.SSA ? new ExitSSAStage() : null, - - new DeadBlockStage(), - - mosaSettings.InlineMethods || mosaSettings.InlineExplicit ? new InlineEvaluationStage() : null, - new NewObjectStage(), - new CallStage(), - new CompoundStage(), - new PlatformIntrinsicStage(), - - new PlatformEdgeSplitStage(), - new VirtualRegisterReindexStage(), - new GreedyRegisterAllocatorStage(), - new StackLayoutStage(), - new DeadBlockStage(), - new AdvancedBlockOrderingStage(), - - new SafePointStage(), - - new CodeGenerationStage(), - mosaSettings.EmitBinary ? new ProtectedRegionLayoutStage() : null, - }; + pipeline.Add( + [ + new CILDecoderStage(), + new ExceptionStage(), + new FastBlockOrderingStage(), + mosaSettings.Devirtualization ? new DevirtualizeCallStage() : null, + mosaSettings.BasicOptimizations ? new OptimizationStage(false) : null, + new IRTransformsStage(), + new PlugStage(), + new RuntimeStage(), + + mosaSettings.InlineMethods || mosaSettings.InlineExplicit ? new InlineStage() : null, + + mosaSettings.BasicOptimizations ? new OptimizationStage(false) : null, + mosaSettings.SSA ? new EdgeSplitStage() : null, + mosaSettings.SSA ? new EnterSSAStage() : null, + mosaSettings.BasicOptimizations && mosaSettings.SSA ? new OptimizationStage(false) : null, + + mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null, + mosaSettings.LoopInvariantCodeMotion && mosaSettings.SSA ? new LoopInvariantCodeMotionStage() : null, + mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null, + mosaSettings.BasicOptimizations && mosaSettings.SSA && (mosaSettings.ValueNumbering || mosaSettings.LoopInvariantCodeMotion || mosaSettings.SparseConditionalConstantPropagation) ? new OptimizationStage(false) : null, + mosaSettings.BitTracker ? new BitTrackerStage() : null, + mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null, + mosaSettings.BasicOptimizations ? new OptimizationStage(mosaSettings.LongExpansion) : null, + + mosaSettings.TwoPassOptimization && mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null, + mosaSettings.TwoPassOptimization && mosaSettings.LoopInvariantCodeMotion && mosaSettings.SSA ? new LoopInvariantCodeMotionStage() : null, + mosaSettings.TwoPassOptimization && mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null, + mosaSettings.TwoPassOptimization && mosaSettings.BitTracker ? new BitTrackerStage() : null, + mosaSettings.TwoPassOptimization && mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null, + mosaSettings.TwoPassOptimization && mosaSettings.BasicOptimizations && mosaSettings.SSA ? new OptimizationStage(mosaSettings.LongExpansion) : null, + + new NopRemovalStage(), + new FastBlockOrderingStage(), + + mosaSettings.SSA ? new ExitSSAStage() : null, + + new DeadBlockStage(), + + mosaSettings.InlineMethods || mosaSettings.InlineExplicit ? new InlineEvaluationStage() : null, + new NewObjectStage(), + new CallStage(), + new CompoundStage(), + new PlatformIntrinsicStage(), + + new PlatformEdgeSplitStage(), + new VirtualRegisterReindexStage(), + new GreedyRegisterAllocatorStage(), + new StackLayoutStage(), + + new CodeGenerationStage(), + ]); + } + + private static void ExtendMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) + { + pipeline.InsertBefore( + [ + new DeadBlockStage(), + new SafePointStage(), + new AdvancedBlockOrderingStage(), + new JumpOptimizationStage() + ]); + + pipeline.InsertAfterLast( + [ + new ProtectedRegionLayoutStage(), + ]); + } #endregion Static Methods @@ -334,13 +348,18 @@ private Pipeline GetOrCreateMethodStagePipeline(int thr MethodStagePipelines[threadID] = pipeline; - pipeline.Add(GetDefaultMethodPipeline(MosaSettings)); + // Setup the initial pipeline + InitializeMethodCompilerPipeline(pipeline, MosaSettings); // Call hook to allow for the extension of the pipeline - CompilerHooks.ExtendMethodCompilerPipeline?.Invoke(pipeline); + CompilerHooks.ExtendMethodCompilerPipeline?.Invoke(pipeline, MosaSettings); + // Extend pipeline with architecture stages Architecture.ExtendMethodCompilerPipeline(pipeline, MosaSettings); + // Extend pipeline after all hooks and architecture stages are added + ExtendMethodCompilerPipeline(pipeline, MosaSettings); + foreach (var stage in pipeline) { stage.Initialize(this); diff --git a/Source/Mosa.Compiler.Framework/CompilerHooks.cs b/Source/Mosa.Compiler.Framework/CompilerHooks.cs index d261685a6c..183dbbedbc 100644 --- a/Source/Mosa.Compiler.Framework/CompilerHooks.cs +++ b/Source/Mosa.Compiler.Framework/CompilerHooks.cs @@ -1,6 +1,7 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. using Mosa.Compiler.MosaTypeSystem; +using Mosa.Utility.Configuration; namespace Mosa.Compiler.Framework; @@ -24,7 +25,7 @@ public class CompilerHooks public delegate void ExtendCompilerPipelineHandler(Pipeline pipeline); - public delegate void ExtendMethodCompilerPipelineHandler(Pipeline pipeline); + public delegate void ExtendMethodCompilerPipelineHandler(Pipeline pipeline, MosaSettings mosaSettings); public delegate int? GetMethodTraceLevelHandler(MosaMethod method); diff --git a/Source/Mosa.Compiler.Framework/Operand.cs b/Source/Mosa.Compiler.Framework/Operand.cs index 0dc6d2429e..4f1bfd7bcb 100644 --- a/Source/Mosa.Compiler.Framework/Operand.cs +++ b/Source/Mosa.Compiler.Framework/Operand.cs @@ -37,7 +37,7 @@ public enum ConstantType #region Member Data - private BitValue bitValue; + //private BitValue bitValue; #endregion Member Data diff --git a/Source/Mosa.Compiler.Framework/Stages/LoopInvariantCodeMotionStage.cs b/Source/Mosa.Compiler.Framework/Stages/LoopInvariantCodeMotionStage.cs index 6b72a69fd6..b7c793488c 100644 --- a/Source/Mosa.Compiler.Framework/Stages/LoopInvariantCodeMotionStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/LoopInvariantCodeMotionStage.cs @@ -53,7 +53,9 @@ protected override void Run() if (trace != null) { - DumpLoops(loops); + var loopTrace = CreateTraceLog("Loops"); + + LoopDetector.DumpLoops(loopTrace, loops); } SortLoopsByBlockCount(loops); @@ -74,35 +76,6 @@ protected override void Finish() ParamStoreSet = null; } - public void DumpLoops(List loops) - { - var loopTrace = CreateTraceLog("Loops"); - - if (loopTrace == null) - return; - - foreach (var loop in loops) - { - loopTrace.Log($"Header: {loop.Header}"); - foreach (var backedge in loop.Backedges) - { - loopTrace.Log($" Backedge: {backedge}"); - } - - var sb = new StringBuilder(); - - foreach (var block in loop.LoopBlocks) - { - sb.Append(block); - sb.Append(", "); - } - - sb.Length -= 2; - - loopTrace.Log($" Members: {sb}"); - } - } - private static void SortLoopsByBlockCount(List loops) { loops.Sort((Loop p1, Loop p2) => p1.LoopBlocks.Count < p2.LoopBlocks.Count ? 0 : 1); diff --git a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs index bece041e2b..4355b97515 100644 --- a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs @@ -1,9 +1,20 @@ // Copyright (c) MOSA Project. Licensed under the New BSD License. +using Mosa.Compiler.Framework.Analysis; + namespace Mosa.Compiler.Framework.Stages; +// TODO: +// 1. Analyze method and determine: +// A. Determine if method contain any refernces to objects, or have any managed pointers +// B. Determine start/end ranges of objects and managed pointers on the local stack and parameters +// C. Place SafePoint at: +// i. Method prologue +// ii. Each loop backedge +// 2. Add SafePoint and for each determine which registers contain objects or managed pointers + /// -/// This stage inserts the GC safe points. +/// This stage inserts the garbage collection safe points. /// public class SafePointStage : BaseMethodCompilerStage { @@ -15,6 +26,18 @@ protected override void Run() return; trace = CreateTraceLog(); + + var loops = LoopDetector.FindLoops(BasicBlocks); + + if (trace != null) + { + var loopTrace = CreateTraceLog("Loops"); + + loopTrace.Log($"Loops: Total = {loops.Count}"); + loopTrace.Log(); + + LoopDetector.DumpLoops(loopTrace, loops); + } } protected override void Finish() diff --git a/Source/Mosa.Compiler.x64/Architecture.cs b/Source/Mosa.Compiler.x64/Architecture.cs index 1cb6ac349e..66865af9e7 100644 --- a/Source/Mosa.Compiler.x64/Architecture.cs +++ b/Source/Mosa.Compiler.x64/Architecture.cs @@ -168,27 +168,21 @@ public override void ExtendCompilerPipeline(Pipeline pipeline public override void ExtendMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) { pipeline.InsertBefore( - new Stages.RuntimeCallStage() + new RuntimeCallStage() ); pipeline.InsertAfterLast( - new BaseMethodCompilerStage[] - { - new IRTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - new PlatformTransformStage(), - }); + [ + new IRTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + new PlatformTransformStage(), + ]); pipeline.InsertBefore( - new BaseMethodCompilerStage[] - { - new PlatformTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - }); - - pipeline.InsertBefore( - new JumpOptimizationStage() - ); + [ + new PlatformTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + ]); } /// diff --git a/Source/Mosa.Compiler.x86/Architecture.cs b/Source/Mosa.Compiler.x86/Architecture.cs index b3e883d880..38d03c0386 100644 --- a/Source/Mosa.Compiler.x86/Architecture.cs +++ b/Source/Mosa.Compiler.x86/Architecture.cs @@ -152,24 +152,18 @@ public override void ExtendMethodCompilerPipeline(Pipeline( - new BaseMethodCompilerStage[] - { - //new AdvanceIRTransformStage(), - new IRTransformationStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - new PlatformTransformStage(), - }); + [ + //new AdvanceIRTransformStage(), + new IRTransformationStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + new PlatformTransformStage(), + ]); pipeline.InsertBefore( - new BaseMethodCompilerStage[] - { - new PlatformTransformStage(), - mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, - }); - - pipeline.InsertBefore( - new JumpOptimizationStage() - ); + [ + new PlatformTransformStage(), + mosaSettings.PlatformOptimizations ? new Stages.OptimizationStage() : null, + ]); } /// diff --git a/Source/Mosa.Tool.Explorer.Avalonia/MainWindow.axaml.cs b/Source/Mosa.Tool.Explorer.Avalonia/MainWindow.axaml.cs index 7296055d45..d1ea2816b1 100755 --- a/Source/Mosa.Tool.Explorer.Avalonia/MainWindow.axaml.cs +++ b/Source/Mosa.Tool.Explorer.Avalonia/MainWindow.axaml.cs @@ -681,7 +681,7 @@ private void OpenFile() private static void ExtendCompilerPipeline(Pipeline pipeline) => pipeline.InsertAfterFirst(new ExplorerMethodCompileTimeStage()); - private void ExtendMethodCompilerPipeline(Pipeline pipeline) + private void ExtendMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) { pipeline.Add(new DisassemblyStage()); pipeline.Add(new DebugInfoStage()); diff --git a/Source/Mosa.Tool.Explorer/MainForm.cs b/Source/Mosa.Tool.Explorer/MainForm.cs index 80eb1fa240..447be3d80c 100644 --- a/Source/Mosa.Tool.Explorer/MainForm.cs +++ b/Source/Mosa.Tool.Explorer/MainForm.cs @@ -575,7 +575,7 @@ private void ExtendCompilerPipeline(Pipeline pipeline) ); } - private void ExtendMethodCompilerPipeline(Pipeline pipeline) + private void ExtendMethodCompilerPipeline(Pipeline pipeline, MosaSettings mosaSettings) { pipeline.Add(new DisassemblyStage()); pipeline.Add(new DebugInfoStage()); From 66ec4b4c1d8aad23e7459fe25395ebf1a4685f4d Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 23 Nov 2024 16:35:15 -0800 Subject: [PATCH 2/7] - Preparing for SafePoints --- Source/Data/IR-Instructions.json | 16 ++++++++++++++++ Source/Mosa.Compiler.Framework/IR.cs | 2 ++ .../Instructions/UnstableRegionEnd.cs | 18 ++++++++++++++++++ .../Instructions/UnstableRegionStart.cs | 18 ++++++++++++++++++ .../Stages/SafePointStage.cs | 2 +- .../Compound/BaseCompoundTransform.cs | 4 ++++ 6 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 Source/Mosa.Compiler.Framework/Instructions/UnstableRegionEnd.cs create mode 100644 Source/Mosa.Compiler.Framework/Instructions/UnstableRegionStart.cs diff --git a/Source/Data/IR-Instructions.json b/Source/Data/IR-Instructions.json index e453d10a7d..883cc572d2 100644 --- a/Source/Data/IR-Instructions.json +++ b/Source/Data/IR-Instructions.json @@ -1501,6 +1501,22 @@ "IgnoreDuringCodeGeneration": "true", "UnspecifiedSideEffect": "true" }, + { + "Name": "UnstableRegionStart", + "FamilyName": "IR", + "ResultCount": 0, + "OperandCount": 0, + "IgnoreDuringCodeGeneration": "true", + "UnspecifiedSideEffect": "true" + }, + { + "Name": "UnstableRegionEnd", + "FamilyName": "IR", + "ResultCount": 0, + "OperandCount": 0, + "IgnoreDuringCodeGeneration": "true", + "UnspecifiedSideEffect": "true" + }, { "Name": "Rethrow", "FamilyName": "IR", diff --git a/Source/Mosa.Compiler.Framework/IR.cs b/Source/Mosa.Compiler.Framework/IR.cs index 4249c041a3..43d40954fc 100644 --- a/Source/Mosa.Compiler.Framework/IR.cs +++ b/Source/Mosa.Compiler.Framework/IR.cs @@ -225,6 +225,8 @@ public static class IR public static readonly BaseInstruction TryEnd = new TryEnd(); public static readonly BaseInstruction TryStart = new TryStart(); public static readonly BaseInstruction SafePoint = new SafePoint(); + public static readonly BaseInstruction UnstableRegionStart = new UnstableRegionStart(); + public static readonly BaseInstruction UnstableRegionEnd = new UnstableRegionEnd(); public static readonly BaseInstruction Rethrow = new Rethrow(); public static readonly BaseInstruction GetVirtualFunctionPtr = new GetVirtualFunctionPtr(); public static readonly BaseInstruction MemoryCopy = new MemoryCopy(); diff --git a/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionEnd.cs b/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionEnd.cs new file mode 100644 index 0000000000..9eded77267 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionEnd.cs @@ -0,0 +1,18 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +// This code was generated by an automated template. + +namespace Mosa.Compiler.Framework.Instructions; + +/// +/// UnstableRegionEnd +/// +public sealed class UnstableRegionEnd : BaseIRInstruction +{ + public UnstableRegionEnd() + : base(0, 0) + { + } + + public override bool IgnoreDuringCodeGeneration => true; +} diff --git a/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionStart.cs b/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionStart.cs new file mode 100644 index 0000000000..61346ff9b9 --- /dev/null +++ b/Source/Mosa.Compiler.Framework/Instructions/UnstableRegionStart.cs @@ -0,0 +1,18 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +// This code was generated by an automated template. + +namespace Mosa.Compiler.Framework.Instructions; + +/// +/// UnstableRegionStart +/// +public sealed class UnstableRegionStart : BaseIRInstruction +{ + public UnstableRegionStart() + : base(0, 0) + { + } + + public override bool IgnoreDuringCodeGeneration => true; +} diff --git a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs index 4355b97515..3844f49c92 100644 --- a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs @@ -6,7 +6,7 @@ namespace Mosa.Compiler.Framework.Stages; // TODO: // 1. Analyze method and determine: -// A. Determine if method contain any refernces to objects, or have any managed pointers +// A. Determine if method contains any references to objects, or have any managed pointers // B. Determine start/end ranges of objects and managed pointers on the local stack and parameters // C. Place SafePoint at: // i. Method prologue diff --git a/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs b/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs index e430b8e3bf..c815a231fa 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs @@ -26,6 +26,8 @@ protected static void CopyCompound(Transform transform, Context context, Operand var srcReg = transform.VirtualRegisters.AllocateNativeInteger(); var dstReg = transform.VirtualRegisters.AllocateNativeInteger(); + context.SetInstruction(IR.UnstableRegionStart); + context.SetInstruction(transform.AddInstruction, srcReg, sourceBase, source); context.AppendInstruction(transform.AddInstruction, dstReg, destinationBase, destination); @@ -70,6 +72,8 @@ protected static void CopyCompound(Transform transform, Context context, Operand continue; } } + + context.SetInstruction(IR.UnstableRegionEnd); } #endregion Helpers From a56b343619158005e4a1e3c8475a55169a326847 Mon Sep 17 00:00:00 2001 From: Phil Date: Sat, 23 Nov 2024 16:45:18 -0800 Subject: [PATCH 3/7] - Preparing for SafePoints --- .../Transforms/Compound/BaseCompoundTransform.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs b/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs index c815a231fa..531e0bc973 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Compound/BaseCompoundTransform.cs @@ -28,7 +28,7 @@ protected static void CopyCompound(Transform transform, Context context, Operand context.SetInstruction(IR.UnstableRegionStart); - context.SetInstruction(transform.AddInstruction, srcReg, sourceBase, source); + context.AppendInstruction(transform.AddInstruction, srcReg, sourceBase, source); context.AppendInstruction(transform.AddInstruction, dstReg, destinationBase, destination); var tmp = transform.VirtualRegisters.AllocateNativeInteger(); @@ -73,7 +73,7 @@ protected static void CopyCompound(Transform transform, Context context, Operand } } - context.SetInstruction(IR.UnstableRegionEnd); + context.AppendInstruction(IR.UnstableRegionEnd); } #endregion Helpers From 8e64bb73b74d327fa6ec9a8b86751bf777d8bae0 Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 1 Dec 2024 14:16:52 -0800 Subject: [PATCH 4/7] - Preparing for SafePoints --- Source/Docs/mosa-runtime-tables.dot | 18 ++++++++++++++---- Source/Docs/runtime-tables.rst | 12 ++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/Source/Docs/mosa-runtime-tables.dot b/Source/Docs/mosa-runtime-tables.dot index 00d78f68e3..1927aec29e 100644 --- a/Source/Docs/mosa-runtime-tables.dot +++ b/Source/Docs/mosa-runtime-tables.dot @@ -11,7 +11,7 @@ digraph "MOSA Runtime Tables" ]; "NameString" [ - label = "String Object|Object Header|Pointer to Method Table|String Length|Unicode String" + label = "Internal String Object|Object Header|Pointer to Method Table|String Length|Unicode String" shape = "record" color = "red" ]; @@ -209,7 +209,7 @@ digraph "MOSA Runtime Tables" ]; "MethodDefinition" [ - label = "Method Definition|Pointer to Method Name|~Pointer to Custom Attributes|Method Attributes|Local & Parameter Stack Size|Pointer to Method|Pointer to Return Type Definition|~Pointer to Protected Region Table|Pointer to SafePoint Table|Number of Parameters|Pointer to Parameter Definition 1|...|Pointer to Parameter Definition N" + label = "Method Definition|Pointer to Method Name|~Pointer to Custom Attributes|Method Attributes|Local & Parameter Stack Size|Pointer to Method|Pointer to Return Type Definition|~Pointer to Protected Region Table|Pointer to Method GC Data|Number of Parameters|Pointer to Parameter Definition 1|...|Pointer to Parameter Definition N" shape = "record" color = "purple" ]; @@ -232,7 +232,7 @@ digraph "MOSA Runtime Tables" color = "blue" ]; - "MethodDefinition":f8 -> "SafePointTable":f0 [ + "MethodDefinition":f8 -> "Method GC Data":f0 [ id = 0 ]; @@ -258,6 +258,16 @@ digraph "MOSA Runtime Tables" id = 0 color = "blue" ]; + + "Method GC Data" [ + label = "Method GC Data|Pointer to SafePoint Table|Pointer to Method GC Stack Data" + shape = "record" + ]; + + "Method GC Data":f1 -> "SafePoint":f0 [ + id = 0 + style = "dotted" + ]; "SafePointTable" [ label = "SafePoint Table|Number of SafePoints|SafePoint 1|..|SafePoint N" @@ -265,7 +275,7 @@ digraph "MOSA Runtime Tables" ]; "SafePoint" [ - label = "SafePoint|Address Offset|Address Range (signed)|Breakpoint Indicator|CPU Registers Bitmap (64 bit)" + label = "SafePoint|Address Offset|Address Range (signed)|Breakpoint Indicator|CPU Registers Bitmap (32 bit)" shape = "record" ]; diff --git a/Source/Docs/runtime-tables.rst b/Source/Docs/runtime-tables.rst index 08ba0b06bc..40f6e2dcca 100644 --- a/Source/Docs/runtime-tables.rst +++ b/Source/Docs/runtime-tables.rst @@ -5,3 +5,15 @@ MOSA Runtime Tables This diagram represents the internal runtime tables within the MOSA virtual machine: .. graphviz:: mosa-runtime-tables.dot + +Internal String Object +---------------------- + +.. csv-table:: + :header: "Fields" + :widths: 200 + + Object Header + Pointer to Method Table + String Length + Unicode String From 807de8df52568457664f5a60345c010f38b7b8e2 Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 1 Dec 2024 14:24:22 -0800 Subject: [PATCH 5/7] Merge branch 'master' of https://github.com/mosa/MOSA-Project into 511-safepoints # Conflicts: # Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs --- Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs index 23a72282ed..3844f49c92 100644 --- a/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs +++ b/Source/Mosa.Compiler.Framework/Stages/SafePointStage.cs @@ -6,11 +6,7 @@ namespace Mosa.Compiler.Framework.Stages; // TODO: // 1. Analyze method and determine: -<<<<<<< HEAD // A. Determine if method contains any references to objects, or have any managed pointers -======= -// A. Determine if method contain any refernces to objects, or have any managed pointers ->>>>>>> d95db0ac4b68e28fc2e97b50da5bb5aebe7dabce // B. Determine start/end ranges of objects and managed pointers on the local stack and parameters // C. Place SafePoint at: // i. Method prologue From 9b1a27d86f34a32d2d9e4c6ac6d1b3d8fb498c04 Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 1 Dec 2024 15:28:35 -0800 Subject: [PATCH 6/7] - Updated docs --- Source/Docs/runtime-tables.rst | 80 ++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/Source/Docs/runtime-tables.rst b/Source/Docs/runtime-tables.rst index 40f6e2dcca..6e44c8d820 100644 --- a/Source/Docs/runtime-tables.rst +++ b/Source/Docs/runtime-tables.rst @@ -10,10 +10,84 @@ Internal String Object ---------------------- .. csv-table:: - :header: "Fields" + :header: "Object Header" :widths: 200 - - Object Header + Pointer to Method Table String Length Unicode String + +Internal String Object +---------------------- + +.. csv-table:: + :header: "Assembly Table" + :widths: 200 + + Number of Assemblies + Pointer to Assembly 1…N + +Assembly +-------- + +.. csv-table:: + :header: "Assembly" + :widths: 200 + + Pointer to Assembly Name + Pointer to Custom Attributes + Flags: IsReflectionOnly + Number of Types + Pointer to Type Definition 1..N + +Type Definition +--------------- + +.. csv-table:: + :header: "Type Definition" + :widths: 200 + + Pointer to Typeame + Pointer to Custom Attributes + Type Code & Attributes + Type Size + Pointer to Assembly + Pointer to Parent Type + Pointer to Declaring Type + Pointer to Element Type + Pointer to Default Constructor Method + Pointer to Properties Table + Pointer to Fields Table + Pointer to Interface Slot Table + Pointer to Interface Bitmap + Number of Methods + Pointer to Method 1..N + Pointer to Method Definition 1..N + +Fields Table +--------------- + +.. csv-table:: + :header: "Fields Table" + :widths: 200 + + Number of Fields + Pointer to Field Definition 1..N + +.. csv-table:: + :header: "Field Definition" + :widths: 200 + + Pointer to Fieldame + Pointer to Custom Attributes + Field Attributes + Pointer to Field Type + Address + Offset / Size + +.. csv-table:: + :header: "Properties Table" + :widths: 200 + + Number of Properties + Pointer to Property Definition 1..N From a05ba3fb957a898d7396bc1d8f8faafaeeddc59a Mon Sep 17 00:00:00 2001 From: Phil Date: Sun, 1 Dec 2024 15:30:56 -0800 Subject: [PATCH 7/7] - Updated docs --- Source/Docs/runtime-tables.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Docs/runtime-tables.rst b/Source/Docs/runtime-tables.rst index 6e44c8d820..532e24cb6b 100644 --- a/Source/Docs/runtime-tables.rst +++ b/Source/Docs/runtime-tables.rst @@ -25,7 +25,7 @@ Internal String Object :widths: 200 Number of Assemblies - Pointer to Assembly 1…N + Pointer to Assembly 1..N Assembly --------