Skip to content

Commit

Permalink
- Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Nov 10, 2023
1 parent f417b65 commit 33f0de6
Show file tree
Hide file tree
Showing 98 changed files with 516 additions and 442 deletions.
142 changes: 142 additions & 0 deletions Source/Mosa.BareMetal.TestWorld.x64/Boot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System;
using System.Runtime.CompilerServices;
using System.Threading;
using Mosa.Kernel.BareMetal;
using Mosa.UnitTests.Optimization;

namespace Mosa.BareMetal.TestWorld.x64;

public static class Boot
{
public static void Main()
{
Debug.WriteLine("Boot::Main()");

Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("Mosa.BareMetal.TextWorld.x64");
Console.WriteLine();

Division.DivisionBy7(254u);

//InterruptManager.SetHandler(ProcessInterrupt);

//StartThreading();

// Will never get here!
//Debug.WriteLine("ERROR: Thread Start Failure");

Debug.WriteLine("ERROR: Done!");
Debug.Fatal();
}

private static int counter = 0;

public static void ProcessInterrupt(uint interrupt, uint errorCode)
{
Interlocked.Increment(ref counter);

lock (spinlock)
{
Console.SetCursorPosition(0, 23);
Console.Write("Counter: " + counter + " IRQ: " + interrupt + " Code: " + errorCode);
}
}

private static void StartThreading()
{
Debug.WriteLine("Boot::StartThreadTest()");

Scheduler.CreateThread(Program.EntryPoint);

Scheduler.CreateThread(Thread1);
Scheduler.CreateThread(Thread2);
Scheduler.CreateThread(Thread3);
Scheduler.CreateThread(Thread4);
Scheduler.CreateThread(Thread5);

Scheduler.Start();

Debug.WriteLine("Boot::StartThreadTest() [Exit]");
}

private static readonly object spinlock = new();
private static int totalticks = 0;

private static void UpdateThreadTicks(int thread, uint ticks)
{
Interlocked.Increment(ref totalticks);

if (totalticks % 10000 == 0)
{
lock (spinlock)
{
Console.SetCursorPosition(0, 1 + thread);
Console.Write("Thread #" + thread + ": " + ticks);
}
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Thread1()
{
var ticks = 0u;

while (true)
{
UpdateThreadTicks(1, ++ticks);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Thread2()
{
var ticks = 0u;

while (true)
{
UpdateThreadTicks(2, ++ticks);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Thread3()
{
var ticks = 0u;

while (true)
{
UpdateThreadTicks(3, ++ticks);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Thread4()
{
var ticks = 0u;

while (true)
{
UpdateThreadTicks(4, ++ticks);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void Thread5()
{
var ticks = 0u;

while (true)
{
UpdateThreadTicks(5, ++ticks);
}
}

public static void ForceInclude()
{
Mosa.Kernel.BareMetal.x64.PlatformPlug.ForceInclude();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<Import Project="../Mosa.BareMetal.targets" />

<ItemGroup>
<ProjectReference Include="..\Mosa.BareMetal.TestWorld\Mosa.BareMetal.TestWorld.csproj" />
<ProjectReference Include="..\Mosa.Kernel.BareMetal.x64\Mosa.Kernel.BareMetal.x64.csproj" />
<ProjectReference Include="..\Mosa.UnitTests\Mosa.UnitTests.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion Source/Mosa.BareMetal.TestWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void Main()
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;
Console.Clear();
Console.WriteLine("Mosa.BareMetal.TextWold.x86");
Console.WriteLine("Mosa.BareMetal.TextWorld.x86");
Console.WriteLine();

Division.DivisionBy7(254u);
Expand Down
6 changes: 1 addition & 5 deletions Source/Mosa.Compiler.Framework/BaseTransform.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;
using Mosa.Compiler.Common;
using Mosa.Compiler.Common.Exceptions;

Expand Down Expand Up @@ -76,10 +75,7 @@ protected static bool AreSame(Operand operand1, Operand operand2)
if (operand1 == operand2)
return true;

if (operand1.IsVirtualRegister && operand1.IsVirtualRegister && operand1 == operand2)
return true;

if (operand1.IsPhysicalRegister && operand1.IsPhysicalRegister && operand1 == operand2)
if (operand1.IsPhysicalRegister && operand2.IsPhysicalRegister && operand1.Register == operand2.Register)
return true;

if (operand1.IsResolvedConstant && operand2.IsResolvedConstant)
Expand Down
11 changes: 5 additions & 6 deletions Source/Mosa.Compiler.Framework/BitValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,11 @@ public override string ToString()
{
var sb = new StringBuilder();

sb.Append($" MaxValue: {MaxValue}");
sb.Append($" MinValue: {MinValue}");

sb.Append($" BitsSet: {Convert.ToString((long)BitsSet, 2).PadLeft(64, '0')}");
sb.Append($" BitsClear: {Convert.ToString((long)BitsClear, 2).PadLeft(64, '0')}");
sb.Append($" BitsKnown: {Convert.ToString((long)BitsKnown, 2).PadLeft(64, '0')}");
sb.Append($"MaxValue: {MaxValue}");
sb.Append($"MinValue: {MinValue}");
sb.Append($"BitsSet: {Convert.ToString((long)BitsSet, 2).PadLeft(64, '0')}");
sb.Append($"BitsClear: {Convert.ToString((long)BitsClear, 2).PadLeft(64, '0')}");
sb.Append($"BitsKnown: {Convert.ToString((long)BitsKnown, 2).PadLeft(64, '0')}");

return sb.ToString();
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ public sealed class Compiler
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.BasicOptimizations && mosaSettings.BitTracker ? new OptimizationStage(false) : null,
mosaSettings.BasicOptimizations && mosaSettings.LongExpansion ? new OptimizationStage(mosaSettings.LongExpansion) : null,

mosaSettings.TwoPassOptimization && mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null,
Expand Down
4 changes: 1 addition & 3 deletions Source/Mosa.Compiler.Framework/Managers/CodeMotionManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections.Generic;

namespace Mosa.Compiler.Framework.Managers;

public class CodeMotionManager : BaseTransformManager
{
private readonly HashSet<Node> Motion = new HashSet<Node>();
private readonly HashSet<Node> Motion = new();

public void MarkMotion(Node node)
{
Expand Down
53 changes: 38 additions & 15 deletions Source/Mosa.Compiler.Framework/Stages/BaseTransformStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public abstract class BaseTransformStage : BaseMethodCompilerStage
private readonly List<BaseTransform>[] transforms = new List<BaseTransform>[MaximumInstructionID];
private readonly List<BaseBlockTransform> blockTransforms = new();

protected TraceLog trace;
protected TraceLog Trace;

protected TraceLog specialTrace;
protected TraceLog SpecialTrace;

protected bool EnableTransformOptimizations;
protected bool EnableBlockOptimizations;
Expand Down Expand Up @@ -55,32 +55,32 @@ protected override void Finish()
UpdateCounter(TransformCountStage, TransformCount);
UpdateCounter(OptimizationCountStage, OptimizationCount);

MethodCompiler.Compiler.PostTraceLog(specialTrace);
MethodCompiler.Compiler.PostTraceLog(SpecialTrace);

TransformCount = 0;
OptimizationCount = 0;

trace = null;
Trace = null;
}

protected override void Run()
{
SortByPriority();

trace = CreateTraceLog(5);
Trace = CreateTraceLog(5);

AreCPURegistersAllocated = MethodCompiler.AreCPURegistersAllocated;

Steps = 0;
MethodCompiler.CreateTranformInstructionTrace(this, Steps++);

specialTrace = new TraceLog(TraceType.GlobalDebug, null, null, "Special Optimizations");
SpecialTrace = new TraceLog(TraceType.GlobalDebug, null, null, "Special Optimizations");

Transform.SetLogs(trace, specialTrace);
Transform.SetLogs(Trace, SpecialTrace);

CustomizeTransform(Transform);
Setup();

ExecutePasses();
ExecutePhases();
}

protected void AddTranforms(List<BaseTransform> list)
Expand Down Expand Up @@ -130,28 +130,51 @@ private void SortByPriority()
SortedByPriority = true;
}

protected virtual void CustomizeTransform(Transform transform)
{ }
protected virtual void Setup()
{
}

private void ExecutePasses()
protected virtual bool SetupPhase(int phase)
{
return phase == 0;
}

private void ExecutePhases()
{
var phase = 0;
var pass = 1;

while (true)
{
if (!SetupPhase(phase++))
break;

pass = ExecutePasses(pass, MaxPasses);
}
}

private int ExecutePasses(int pass, int maxPasses)
{
var changed = true;
var firstPass = true;

while (changed)
{
trace?.Log($"*** Pass # {pass}");
Trace?.Log($"*** Pass # {pass}");

var changed1 = InstructionTransformationPass();
var changed2 = (!changed1 || pass == 1) && ApplyBlockTransforms();
var changed2 = (!changed1 || firstPass) && ApplyBlockTransforms();

changed = changed1 || changed2;

pass++;
firstPass = false;

if (pass >= MaxPasses && MaxPasses != 0)
if (pass >= maxPasses && maxPasses != 0)
break;
}

return pass;
}

private bool InstructionTransformationPass()
Expand Down
Loading

0 comments on commit 33f0de6

Please sign in to comment.