Skip to content

Commit

Permalink
- WIP (working)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Oct 28, 2023
1 parent 7a5ed0e commit ed6d2eb
Show file tree
Hide file tree
Showing 122 changed files with 292 additions and 306 deletions.
52 changes: 14 additions & 38 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,36 +101,6 @@ public sealed class Compiler
/// </summary>
public bool IsStopped { get; private set; }

/// <summary>
/// The stack frame
/// </summary>
public Operand StackFrame { get; }

/// <summary>
/// The stack frame
/// </summary>
public Operand StackPointer { get; }

/// <summary>
/// The program counter
/// </summary>
internal Operand ProgramCounter { get; }

/// <summary>
/// The link register
/// </summary>
internal Operand LinkRegister { get; }

/// <summary>
/// The exception register
/// </summary>
public Operand ExceptionRegister { get; }

/// <summary>
/// The ;eave target register
/// </summary>
public Operand LeaveTargetRegister { get; }

public CompilerHooks CompilerHooks { get; }

public int TraceLevel { get; }
Expand Down Expand Up @@ -245,14 +215,6 @@ public Compiler(MosaCompiler mosaCompiler)

ObjectHeaderSize = Architecture.NativePointerSize + 4 + 4; // Hash Value (32-bit) + Lock & Status (32-bit) + Method Table

StackFrame = Operand.CreateCPURegisterNativeInteger(Architecture.StackFrameRegister, Architecture.Is32BitPlatform);
StackPointer = Operand.CreateCPURegisterNativeInteger(Architecture.StackPointerRegister, Architecture.Is32BitPlatform);
ExceptionRegister = Operand.CreateCPURegisterObject(Architecture.ExceptionRegister);
LeaveTargetRegister = Operand.CreateCPURegisterNativeInteger(Architecture.LeaveTargetRegister, Architecture.Is32BitPlatform);

LinkRegister = Architecture.LinkRegister == null ? null : Operand.CreateCPURegisterNativeInteger(Architecture.LinkRegister, Architecture.Is32BitPlatform);
ProgramCounter = Architecture.ProgramCounter == null ? null : Operand.CreateCPURegisterNativeInteger(Architecture.ProgramCounter, Architecture.Is32BitPlatform);

MethodStagePipelines = new Pipeline<BaseMethodCompilerStage>[MaxThreads];

MethodScheduler = new MethodScheduler(this);
Expand Down Expand Up @@ -335,6 +297,20 @@ public void CompileMethod(MosaMethod method, BasicBlocks basicBlocks, int thread
CompilerHooks.NotifyMethodCompiled?.Invoke(method);
}

public void CompileMethod(Transform transform)
{
PostEvent(CompilerEvent.MethodCompileStart, transform.Method.FullName, transform.MethodCompiler.ThreadID);

var pipeline = GetOrCreateMethodStagePipeline(transform.MethodCompiler.ThreadID);

transform.MethodCompiler.Pipeline = pipeline;
transform.MethodCompiler.Compile();

PostEvent(CompilerEvent.MethodCompileEnd, transform.Method.FullName, transform.MethodCompiler.ThreadID);

CompilerHooks.NotifyMethodCompiled?.Invoke(transform.Method);
}

private Pipeline<BaseMethodCompilerStage> GetOrCreateMethodStagePipeline(int threadID)
{
var pipeline = MethodStagePipelines[threadID];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal static partial class IntrinsicMethods
[IntrinsicMethod("Mosa.Runtime.Intrinsic::GetExceptionRegister")]
private static void GetExceptionRegister(Context context, Transform transform)
{
context.SetInstruction(transform.MoveInstruction, context.Result, transform.Compiler.ExceptionRegister);
context.SetInstruction(transform.MoveInstruction, context.Result, transform.ExceptionRegister);
}
}
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/Intrinsics/GetStackFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal static partial class IntrinsicMethods
[IntrinsicMethod("Mosa.Runtime.Intrinsic::GetStackFrame")]
private static void GetStackFrame(Context context, Transform transform)
{
context.SetInstruction(transform.MoveInstruction, context.Result, transform.Compiler.StackFrame);
context.SetInstruction(transform.MoveInstruction, context.Result, transform.StackFrame);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal static partial class IntrinsicMethods
[IntrinsicMethod("Mosa.Runtime.Intrinsic::GetStackPointer")]
private static void GetStackPointer(Context context, Transform transform)
{
context.SetInstruction(transform.MoveInstruction, context.Result, transform.Compiler.StackPointer);
context.SetInstruction(transform.MoveInstruction, context.Result, transform.StackPointer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1420,7 +1420,7 @@ protected void CreatePhysicalRegisterOperands()
if (liveInterval.AssignedPhysicalRegister == null)
continue;

liveInterval.AssignedPhysicalOperand = Operand.CreateCPURegister(liveInterval.Register.RegisterOperand, liveInterval.AssignedPhysicalRegister);
liveInterval.AssignedPhysicalOperand = Transform.PhysicalRegisters.Allocate(liveInterval.Register.RegisterOperand, liveInterval.AssignedPhysicalRegister);
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions Source/Mosa.Compiler.Framework/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public sealed class Transform

public VirtualRegisters VirtualRegisters { get; private set; }

public PhysicalRegisters PhysicalRegisters { get; private set; }

public LocalStack LocalStack { get; set; }

public BasicBlocks BasicBlocks { get; set; }
Expand Down Expand Up @@ -68,29 +70,29 @@ public sealed class Transform

#region Properties - Registers

public Operand StackFrame => MethodCompiler.Compiler.StackFrame;
public Operand StackFrame => MethodCompiler.StackFrame;

public Operand StackPointer => MethodCompiler.Compiler.StackPointer;
public Operand StackPointer => MethodCompiler.StackPointer;

/// <summary>
/// Gets the link register.
/// </summary>
public Operand LinkRegister => MethodCompiler.Compiler.LinkRegister;
public Operand LinkRegister => MethodCompiler.LinkRegister;

/// <summary>
/// Gets the program counter
/// </summary>
public Operand ProgramCounter => MethodCompiler.Compiler.ProgramCounter;
public Operand ProgramCounter => MethodCompiler.ProgramCounter;

/// <summary>
/// Gets the exception register.
/// </summary>
public Operand ExceptionRegister => MethodCompiler.Compiler.ExceptionRegister;
public Operand ExceptionRegister => MethodCompiler.ExceptionRegister;

/// <summary>
/// Gets the leave target register.
/// </summary>
public Operand LeaveTargetRegister => MethodCompiler.Compiler.LeaveTargetRegister;
public Operand LeaveTargetRegister => MethodCompiler.LeaveTargetRegister;

#endregion Properties - Registers

Expand Down Expand Up @@ -149,6 +151,7 @@ public void SetCompiler(Compiler compiler)
// clear - just in case
MethodCompiler = null;
VirtualRegisters = null;
PhysicalRegisters = null;
LocalStack = null;
BasicBlocks = null;
}
Expand All @@ -157,6 +160,7 @@ public void SetMethodCompiler(MethodCompiler methodCompiler)
{
MethodCompiler = methodCompiler;
VirtualRegisters = methodCompiler.VirtualRegisters;
PhysicalRegisters = methodCompiler.PhysicalRegisters;
LocalStack = methodCompiler.LocalStack;
BasicBlocks = methodCompiler.BasicBlocks;
AreCPURegistersAllocated = methodCompiler.AreCPURegistersAllocated;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,34 +106,34 @@ private static void GetReturnValue(Transform transform, Context context, Operand

if (result.IsObject)
{
var returnLow = Operand.CreateCPURegisterObject(transform.Architecture.ReturnRegister);
var returnLow = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ReturnRegister);
context.AppendInstruction(IRInstruction.Gen, returnLow);
context.AppendInstruction(IRInstruction.MoveObject, result, returnLow);
}
else if (result.IsInt64 && transform.Is32BitPlatform)
{
var returnLow = Operand.CreateCPURegister32(transform.Architecture.ReturnRegister);
var returnHigh = Operand.CreateCPURegister32(transform.Architecture.ReturnHighRegister);
var returnLow = transform.PhysicalRegisters.Allocate32(transform.Architecture.ReturnRegister);
var returnHigh = transform.PhysicalRegisters.Allocate32(transform.Architecture.ReturnHighRegister);

context.AppendInstruction(IRInstruction.Gen, returnLow);
context.AppendInstruction(IRInstruction.Gen, returnHigh);
context.AppendInstruction(IRInstruction.To64, result, returnLow, returnHigh);
}
else if (result.IsInteger)
{
var returnLow = Operand.CreateCPURegister(result, transform.Architecture.ReturnRegister);
var returnLow = transform.PhysicalRegisters.Allocate(result, transform.Architecture.ReturnRegister);
context.AppendInstruction(IRInstruction.Gen, returnLow);
context.AppendInstruction(transform.MoveInstruction, result, returnLow);
}
else if (result.IsR4)
{
var returnFP = Operand.CreateCPURegisterR4(transform.Architecture.ReturnFloatingPointRegister);
var returnFP = transform.PhysicalRegisters.AllocateR4(transform.Architecture.ReturnFloatingPointRegister);
context.AppendInstruction(IRInstruction.Gen, returnFP);
context.AppendInstruction(IRInstruction.MoveR4, result, returnFP);
}
else if (result.IsR8)
{
var returnFP = Operand.CreateCPURegisterR8(transform.Architecture.ReturnFloatingPointRegister);
var returnFP = transform.PhysicalRegisters.AllocateR8(transform.Architecture.ReturnFloatingPointRegister);
context.AppendInstruction(IRInstruction.Gen, returnFP);
context.AppendInstruction(IRInstruction.MoveR8, result, returnFP);
}
Expand All @@ -143,7 +143,7 @@ private static void GetReturnValue(Transform transform, Context context, Operand
}
else
{
var returnLow = Operand.CreateCPURegister(result, transform.Architecture.ReturnRegister);
var returnLow = transform.PhysicalRegisters.Allocate(result, transform.Architecture.ReturnRegister);
context.AppendInstruction(IRInstruction.Gen, returnLow);
context.AppendInstruction(transform.MoveInstruction, result, returnLow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public override bool Match(Context context, Transform transform)

public override void Transform(Context context, Transform transform)
{
context.SetInstruction(IRInstruction.Move32, Operand.CreateCPURegister32(transform.Architecture.ReturnRegister), context.Operand1);
context.SetInstruction(IRInstruction.Move32, transform.PhysicalRegisters.Allocate32(transform.Architecture.ReturnRegister), context.Operand1);
}
}
6 changes: 3 additions & 3 deletions Source/Mosa.Compiler.Framework/Transforms/Call/SetReturn64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public override void Transform(Context context, Transform transform)

if (transform.Is32BitPlatform)
{
context.SetInstruction(IRInstruction.GetLow32, Operand.CreateCPURegister32(transform.Architecture.ReturnRegister), operand);
context.AppendInstruction(IRInstruction.GetHigh32, Operand.CreateCPURegister32(transform.Architecture.ReturnHighRegister), operand);
context.SetInstruction(IRInstruction.GetLow32, transform.PhysicalRegisters.Allocate32(transform.Architecture.ReturnRegister), operand);
context.AppendInstruction(IRInstruction.GetHigh32, transform.PhysicalRegisters.Allocate32(transform.Architecture.ReturnHighRegister), operand);
}
else
{
context.SetInstruction(IRInstruction.Move64, Operand.CreateCPURegister64(transform.Architecture.ReturnRegister), context.Operand1);
context.SetInstruction(IRInstruction.Move64, transform.PhysicalRegisters.Allocate64(transform.Architecture.ReturnRegister), context.Operand1);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public override bool Match(Context context, Transform transform)

public override void Transform(Context context, Transform transform)
{
context.SetInstruction(IRInstruction.MoveManagedPointer, Operand.CreateCPURegisterObject(transform.Architecture.ReturnRegister), context.Operand1);
context.SetInstruction(IRInstruction.MoveManagedPointer, transform.PhysicalRegisters.AllocateObject(transform.Architecture.ReturnRegister), context.Operand1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public override bool Match(Context context, Transform transform)

public override void Transform(Context context, Transform transform)
{
context.SetInstruction(IRInstruction.MoveObject, Operand.CreateCPURegisterObject(transform.Architecture.ReturnRegister), context.Operand1);
context.SetInstruction(IRInstruction.MoveObject, transform.PhysicalRegisters.AllocateObject(transform.Architecture.ReturnRegister), context.Operand1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public override bool Match(Context context, Transform transform)

public override void Transform(Context context, Transform transform)
{
context.SetInstruction(IRInstruction.MoveR4, Operand.CreateCPURegisterR4(transform.Architecture.ReturnFloatingPointRegister), context.Operand1);
context.SetInstruction(IRInstruction.MoveR4, transform.PhysicalRegisters.AllocateR4(transform.Architecture.ReturnFloatingPointRegister), context.Operand1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public override bool Match(Context context, Transform transform)

public override void Transform(Context context, Transform transform)
{
context.SetInstruction(IRInstruction.MoveR8, Operand.CreateCPURegisterR8(transform.Architecture.ReturnFloatingPointRegister), context.Operand1);
context.SetInstruction(IRInstruction.MoveR8, transform.PhysicalRegisters.AllocateR8(transform.Architecture.ReturnFloatingPointRegister), context.Operand1);
}
}
33 changes: 18 additions & 15 deletions Source/Mosa.Compiler.x64/CompilerStages/MultibootStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,30 @@ protected override void Finalization()

private void CreateMultibootMethod()
{
var basicBlocks = new BasicBlocks();

var methodCompiler = new MethodCompiler(Compiler, multibootMethod, basicBlocks, 0);
methodCompiler.MethodData.DoNotInline = true;

var transform = new Transform();
transform.SetCompiler(Compiler);
transform.SetMethodCompiler(methodCompiler);

var startUpType = TypeSystem.GetTypeByName("Mosa.Runtime.StartUp");
var initializeMethod = startUpType.FindMethodByName("Initialize");

Compiler.GetMethodData(initializeMethod).DoNotInline = true;

var entryPoint = Operand.CreateLabel(initializeMethod, Architecture.Is32BitPlatform);

var rax = Operand.CreateCPURegister64(CPURegister.RAX);
var rbx = Operand.CreateCPURegister64(CPURegister.RBX);
var rbp = Operand.CreateCPURegister64(CPURegister.RBP);
var rsp = Operand.CreateCPURegister64(CPURegister.RSP);
var rax = transform.PhysicalRegisters.Allocate64(CPURegister.RAX);
var rbx = transform.PhysicalRegisters.Allocate64(CPURegister.RBX);
var rbp = transform.PhysicalRegisters.Allocate64(CPURegister.RBP);
var rsp = transform.PhysicalRegisters.Allocate64(CPURegister.RSP);

var multibootEAX = Operand.CreateLabel(MultibootEAX, Architecture.Is32BitPlatform);
var multibootEBX = Operand.CreateLabel(MultibootEBX, Architecture.Is32BitPlatform);
var stackBottom = Operand.CreateLabel(MultibootInitialStack, Architecture.Is32BitPlatform);

var stackTopOffset = CreateConstant(StackSize - 8);
var zero = CreateConstant(0);
var offset = CreateConstant(8);

var basicBlocks = new BasicBlocks();
var stackTopOffset = CreateConstant(StackSize - 16);

var prologueBlock = basicBlocks.CreatePrologueBlock();

Expand All @@ -46,12 +49,12 @@ private void CreateMultibootMethod()
context.AppendInstruction(X64.Add64, rsp, rsp, stackTopOffset);
context.AppendInstruction(X64.Mov64, rbp, stackBottom);
context.AppendInstruction(X64.Add64, rbp, rbp, stackTopOffset);
context.AppendInstruction(X64.MovStore64, null, rsp, zero, zero);
context.AppendInstruction(X64.MovStore64, null, rsp, offset, zero);
context.AppendInstruction(X64.MovStore64, null, rsp, Operand.Constant64_0, Operand.Constant64_0);
context.AppendInstruction(X64.MovStore64, null, rsp, Operand.Constant64_16, Operand.Constant64_0);

// Place the multiboot address into a static field
context.AppendInstruction(X64.MovStore64, null, multibootEAX, zero, rax);
context.AppendInstruction(X64.MovStore64, null, multibootEBX, zero, rbx);
context.AppendInstruction(X64.MovStore64, null, multibootEAX, Operand.Constant64_0, rax);
context.AppendInstruction(X64.MovStore64, null, multibootEBX, Operand.Constant64_0, rbx);

context.AppendInstruction(X64.Call, null, entryPoint);
context.AppendInstruction(X64.Ret);
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.x64/Intrinsic/AllocateStackSpace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private static void AllocateStackSpace(Context context, Transform transform)
Operand result = context.Result;
Operand size = context.Operand1;

Operand esp = Operand.CreateCPURegister64(CPURegister.RSP);
Operand esp = transform.PhysicalRegisters.Allocate64(CPURegister.RSP);

context.SetInstruction(X64.Sub64, esp, esp, size);
context.AppendInstruction(X64.Mov64, result, esp);
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.x64/Intrinsic/CmpXChgLoad64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ private static void CmpXChgLoad64(Context context, Transform transform)
var comparand = context.Operand3;
var result = context.Result;

var rax = Operand.CreateCPURegister64(CPURegister.RAX);
var rax = transform.PhysicalRegisters.Allocate64(CPURegister.RAX);
var v1 = transform.VirtualRegisters.Allocate64();

context.SetInstruction(X64.Mov64, rax, comparand);
Expand Down
8 changes: 4 additions & 4 deletions Source/Mosa.Compiler.x64/Intrinsic/CpuIdRAX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ private static void CpuIdRAX(Context context, Transform transform)
var operand1 = context.Operand1;
var operand2 = context.Operand2;

var rax = Operand.CreateCPURegister64(CPURegister.RAX);
var rbx = Operand.CreateCPURegister64(CPURegister.RBX);
var rcx = Operand.CreateCPURegister64(CPURegister.RCX);
var rdx = Operand.CreateCPURegister64(CPURegister.RDX);
var rax = transform.PhysicalRegisters.Allocate64(CPURegister.RAX);
var rbx = transform.PhysicalRegisters.Allocate64(CPURegister.RBX);
var rcx = transform.PhysicalRegisters.Allocate64(CPURegister.RCX);
var rdx = transform.PhysicalRegisters.Allocate64(CPURegister.RDX);

context.SetInstruction(X64.Mov64, rax, operand1);
context.AppendInstruction(X64.Mov64, rcx, operand2);
Expand Down
8 changes: 4 additions & 4 deletions Source/Mosa.Compiler.x64/Intrinsic/CpuIdRBX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ private static void CpuIdRBX(Context context, Transform transform)
var operand1 = context.Operand1;
var operand2 = context.Operand2;

var rax = Operand.CreateCPURegister64(CPURegister.RAX);
var rbx = Operand.CreateCPURegister64(CPURegister.RBX);
var rcx = Operand.CreateCPURegister64(CPURegister.RCX);
var rdx = Operand.CreateCPURegister64(CPURegister.RDX);
var rax = transform.PhysicalRegisters.Allocate64(CPURegister.RAX);
var rbx = transform.PhysicalRegisters.Allocate64(CPURegister.RBX);
var rcx = transform.PhysicalRegisters.Allocate64(CPURegister.RCX);
var rdx = transform.PhysicalRegisters.Allocate64(CPURegister.RDX);

context.SetInstruction(X64.Mov64, rax, operand1);
context.AppendInstruction(X64.Mov64, rcx, operand2);
Expand Down
8 changes: 4 additions & 4 deletions Source/Mosa.Compiler.x64/Intrinsic/CpuIdRCX.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ private static void CpuIdRCX(Context context, Transform transform)
var operand1 = context.Operand1;
var operand2 = context.Operand2;

var rax = Operand.CreateCPURegister64(CPURegister.RAX);
var rbx = Operand.CreateCPURegister64(CPURegister.RBX);
var rcx = Operand.CreateCPURegister64(CPURegister.RCX);
var rdx = Operand.CreateCPURegister64(CPURegister.RDX);
var rax = transform.PhysicalRegisters.Allocate64(CPURegister.RAX);
var rbx = transform.PhysicalRegisters.Allocate64(CPURegister.RBX);
var rcx = transform.PhysicalRegisters.Allocate64(CPURegister.RCX);
var rdx = transform.PhysicalRegisters.Allocate64(CPURegister.RDX);

context.SetInstruction(X64.Mov64, rax, operand1);
context.AppendInstruction(X64.Mov64, rcx, operand2);
Expand Down
Loading

0 comments on commit ed6d2eb

Please sign in to comment.