Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP - x86 Platform #1106

Merged
merged 12 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions Source/Data/IR-Optimizations-LowerTo32.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"Family": "IR",
"Section": "Optimizations.Auto",
"Commutative": [
"IR.Add32",
"IR.Add64",
"IR.AddR4",
"IR.AddR8",
"IR.And32",
"IR.And64",
"IR.Or32",
"IR.Or64",
"IR.Xor32",
"IR.Xor64",
"IR.MulR4",
"IR.MulR8",
"IR.MulUnsigned32",
"IR.MulUnsigned64",
"IR.MulSigned32",
"IR.MulSigned64",
"IR.AddCarryOut64",
"IR.AddCarryOut32",
"IR.AddCarryIn32",
"IR.AddCarryIn64"
],
"Optimizations": [
{
"Type": "LowerTo32",
"Name": "Or32Truncate64x32Truncate64x32",
"SubName": "",
"Expression": "IR.Or32 (IR.Truncate64x32 x) (IR.Truncate64x32 y)",
"Filter": "",
"Prefilter": "Is32BitPlatform(@) && IsLowerTo32(@)",
"Result": "(IR.GetLow32 (IR.Or64 x y))",
"Variations": "No"
}
]
}
9 changes: 0 additions & 9 deletions Source/Data/IR-Optimizations-Simplification.json
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,6 @@
"Result": "(IR.SignExtend16x## (IR.Or## x y))",
"Variations": "Yes"
},
{
"Type": "Simplification",
"Name": "Or32Truncate64x32Truncate64x32",
"SubName": "",
"Expression": "IR.Or32 (IR.Truncate64x32 x) (IR.Truncate64x32 y)",
"Filter": "",
"Result": "(IR.GetLow32 (IR.Or64 x y))",
"Variations": "No"
},
{
"Type": "Simplification",
"Name": "SignExtend32x64Truncate64x32",
Expand Down
2 changes: 1 addition & 1 deletion Source/Data/X64-Instructions.json
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@
"OpcodeEncoding": [
{
"Condition": "",
"Encoding": "[x64-rex32],opcode=0xF2,opcode2=0x0F,opcode3=0x5A,mod=11,reg=reg3:o1,rm=reg3:o2,rex.r=reg4x:o1,rex.x=0,rex.b=reg4x:o1"
"Encoding": "[x64-rex32],opcode=0xF2,opcode2=0x0F,opcode3=0x5A,mod=11,reg=reg3:r,rm=reg3:o1,rex.r=reg4x:r,rex.x=0,rex.b=reg4x:o1"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.BareMetal.HelloWorld.x86/Boot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void Main()

// var fatFileStream = new FatFileStream(fat, location);

// uint len = (uint)fatFileStream.Length;
// var len = (uint)fatFileStream.Length;

// Console.WriteLine(" - Length: " + len + " bytes");

Expand Down
1 change: 1 addition & 0 deletions Source/Mosa.Compiler.Framework/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public void Compile()
private void LogException(Exception exception, string title)
{
Compiler.PostEvent(CompilerEvent.Exception, title);
Compiler.PostEvent(CompilerEvent.Exception, exception.ToString());

var exceptionLog = new TraceLog(TraceType.MethodDebug, Method, "Exception", MethodData.Version);

Expand Down
7 changes: 7 additions & 0 deletions Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1849,6 +1849,13 @@ private bool Compare(Context context, Stack<StackEntry> stack, ConditionCode con
context.AppendInstruction(IRInstruction.Compare64x32, conditionCode, result, entry1.Operand, entry2.Operand);
return true;

case PrimitiveType.Int64 when entry2.PrimitiveType == PrimitiveType.Int32:
var v1 = MethodCompiler.VirtualRegisters.Allocate64();

context.AppendInstruction(IRInstruction.ZeroExtend32x64, v1, entry2.Operand);
context.AppendInstruction(IRInstruction.Compare64x32, conditionCode, result, entry1.Operand, v1);
return true;

default:

// TODO: Managed Pointers
Expand Down
10 changes: 4 additions & 6 deletions Source/Mosa.Compiler.Framework/TransformContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class TransformContext

public BasicBlocks BasicBlocks { get; set; }

public bool LowerTo32 { get; private set; }
public bool IsLowerTo32 { get; private set; }

public bool IsInSSAForm { get; private set; }

Expand Down Expand Up @@ -125,12 +125,10 @@ public void SetCompiler(Compiler compiler)
Is32BitPlatform = Compiler.Architecture.Is32BitPlatform;
TypeSystem = Compiler.TypeSystem;

LowerTo32 = Compiler.MosaSettings.LongExpansion;
IsLowerTo32 = Compiler.MosaSettings.LongExpansion;
Devirtualization = Compiler.MosaSettings.Devirtualization;
Window = Math.Max(Compiler.MosaSettings.OptimizationBasicWindow, 1);

LowerTo32 = Compiler.MosaSettings.LongExpansion;

LoadInstruction = Is32BitPlatform ? IRInstruction.Load32 : IRInstruction.Load64;
StoreInstruction = Is32BitPlatform ? IRInstruction.Store32 : IRInstruction.Store64;
MoveInstruction = Is32BitPlatform ? IRInstruction.Move32 : IRInstruction.Move64;
Expand All @@ -153,7 +151,7 @@ public void SetMethodCompiler(MethodCompiler methodCompiler)
AreCPURegistersAllocated = methodCompiler.AreCPURegistersAllocated;
IsInSSAForm = methodCompiler.IsInSSAForm;

LowerTo32 = false;
IsLowerTo32 = false;
TraceLog = null;
Managers.Clear();

Expand All @@ -162,7 +160,7 @@ public void SetMethodCompiler(MethodCompiler methodCompiler)

public void SetStageOptions(bool lowerTo32)
{
LowerTo32 = Compiler.MosaSettings.LongExpansion && lowerTo32 && Is32BitPlatform;
IsLowerTo32 = Compiler.MosaSettings.LongExpansion && lowerTo32 && Is32BitPlatform;
}

#region Manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ArithShiftRight64By32() : base(IRInstruction.ArithShiftRight64, Transform

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 == 32;
return transform.IsLowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 == 32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public BaseLower32Transform(BaseInstruction instruction, TransformType type, boo

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32;
return transform.IsLowerTo32;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transform)
if (branch64Extends.Match(context, transform))
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public Branch64Extends() : base(IRInstruction.Branch64, TransformType.Manual | T

public override bool Match(Context context, TransformContext transform)
{
if (!transform.LowerTo32)
if (!transform.IsLowerTo32)
return false;

if (!context.Operand1.IsVirtualRegister)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override bool Match(Context context, TransformContext transform)
if (context.ConditionCode != ConditionCode.Equal && context.ConditionCode != ConditionCode.NotEqual)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transform)
if (transform.IsInSSAForm)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transform)
if (!transform.IsInSSAForm)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override bool Match(Context context, TransformContext transform)
if (context.ConditionCode != ConditionCode.UnsignedGreater)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override bool Match(Context context, TransformContext transform)
if (context.ConditionCode != ConditionCode.Equal && context.ConditionCode != ConditionCode.NotEqual)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transform)
if (transform.IsInSSAForm)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transform)
if (!transform.IsInSSAForm)
return false;

return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ShiftLeft64ByConstant32() : base(IRInstruction.ShiftLeft64, TransformType

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 <= 32;
return transform.IsLowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 <= 32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ShiftLeft64ByConstant32Plus() : base(IRInstruction.ShiftLeft64, Transform

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 > 32;
return transform.IsLowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 > 32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ShiftRight64ByConstant32() : base(IRInstruction.ShiftRight64, TransformTy

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 <= 32;
return transform.IsLowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 <= 32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public ShiftRight64ByConstant32Plus() : base(IRInstruction.ShiftRight64, Transfo

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 > 32;
return transform.IsLowerTo32 && context.Operand2.IsResolvedConstant && context.Operand2.ConstantUnsigned32 > 32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public SignExtend16x64() : base(IRInstruction.SignExtend16x64, TransformType.Man

public override bool Match(Context context, TransformContext transform)
{
return transform.LowerTo32;
return transform.IsLowerTo32;
}

public override void Transform(Context context, TransformContext transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,6 @@ public static class AutoTransforms
new Simplification.Or32SignExtend16x32SignExtend16x32_v1(),
new Simplification.Or64SignExtend16x64SignExtend16x64(),
new Simplification.Or64SignExtend16x64SignExtend16x64_v1(),
new Simplification.Or32Truncate64x32Truncate64x32(),
new Simplification.SignExtend32x64Truncate64x32(),
new StrengthReduction.Add32Zero(),
new StrengthReduction.Add32Zero_v1(),
Expand Down Expand Up @@ -771,6 +770,7 @@ public static class AutoTransforms
new Simplification.Xor32Max_v1(),
new Simplification.Xor64Max(),
new Simplification.Xor64Max_v1(),
new LowerTo32.Or32Truncate64x32Truncate64x32(),
new Algebraic.Signed32AAPlusBBPlus2AB(),
new Algebraic.Signed32AAPlusBBPlus2AB_v1(),
new Algebraic.Signed32AAPlusBBPlus2AB_v2(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.Simplification;
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.LowerTo32;

/// <summary>
/// Or32Truncate64x32Truncate64x32
/// </summary>
[Transform("IR.Optimizations.Auto.Simplification")]
[Transform("IR.Optimizations.Auto.LowerTo32")]
public sealed class Or32Truncate64x32Truncate64x32 : BaseTransform
{
public Or32Truncate64x32Truncate64x32() : base(IRInstruction.Or32, TransformType.Auto | TransformType.Optimization)
Expand All @@ -18,6 +18,12 @@ public Or32Truncate64x32Truncate64x32() : base(IRInstruction.Or32, TransformType

public override bool Match(Context context, TransformContext transform)
{
if (!transform.Is32BitPlatform)
return false;

if (!transform.IsLowerTo32)
return false;

if (!context.Operand1.IsVirtualRegister)
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override bool Match(Context context, TransformContext transform)
if (!transform.Is32BitPlatform)
return false;

if (!transform.LowerTo32)
if (!transform.IsLowerTo32)
return false;

if (!context.Operand1.IsCPURegister)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override bool Match(Context context, TransformContext transform)
if (!transform.Is32BitPlatform)
return false;

if (!transform.LowerTo32)
if (!transform.IsLowerTo32)
return false;

if (!context.Operand1.IsVirtualRegister)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override bool Match(Context context, TransformContext transform)
if (context.Operand1 != transform.StackFrame)
return false;

if (context.Operand2.Uses.Count != 2)
if (context.Operand2.Uses.Count != 2) // FUTURE: traverse all uses
return false;

if (!(context.Operand3.IsConstant || context.Operand3.IsDefinedOnce))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public override bool Match(Context context, TransformContext transform)
if (context.Operand1 != transform.StackFrame)
return false;

if (context.Operand2.Uses.Count != 2) // FUTURE: traverse all uses
return false;

if (!(context.Operand3.IsConstant || context.Operand3.IsDefinedOnce))
return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override bool Match(Context context, TransformContext transform)
if (context.Operand1 != transform.StackFrame)
return false;

if (context.Operand2.Uses.Count != 2)
if (context.Operand2.Uses.Count != 2) // FUTURE: traverse all uses
return false;

if (!context.Operand3.IsDefinedOnce)
Expand Down
8 changes: 2 additions & 6 deletions Source/Mosa.Korlib/System.Threading/Interlocked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,8 @@ public static long Decrement(ref long location)
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern int CompareExchange(ref int location1, int value, int comparand);

//[MethodImplAttribute(MethodImplOptions.InternalCall)]
//public static extern long CompareExchange(ref long location1, long value, long comparand);
public static long CompareExchange(ref long location1, long value, long comparand)
{
return 0; //temp
}
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern long CompareExchange(ref long location1, long value, long comparand);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
public static extern float CompareExchange(ref float location1, float value, float comparand);
Expand Down
Loading