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 ed6d2eb commit c4a1c9d
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 121 deletions.
120 changes: 11 additions & 109 deletions Source/Mosa.Compiler.Framework/Operand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,118 +554,31 @@ public static Operand CreateLabelObject(string label)

#endregion Factory Methods - Constants

#region Factory Methods - Operands

public static Operand CreateCPURegister(Operand operand, PhysicalRegister register, int index = 0)
{
Debug.Assert(operand.Type == null);

return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = operand.Primitive,
Register = register,
Index = index
};
}

#endregion Factory Methods - Operands

#region Factory Methods - CPURegister

public static Operand CreateCPURegister32(PhysicalRegister register, int index = 0)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.Int32,
Register = register,
Index = index
};
}

public static Operand CreateCPURegister64(PhysicalRegister register, int index = 0)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.Int64,
Register = register,
Index = index
};
}

public static Operand CreateCPURegisterR4(PhysicalRegister register, int index = 0)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.R4,
Register = register,
Index = index
};
}

public static Operand CreateCPURegisterR8(PhysicalRegister register, int index = 0)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.R8,
Register = register,
Index = index
};
}
#region Factory Methods - Standard

public static Operand CreateCPURegisterObject(PhysicalRegister register, int index = 0)
internal static Operand CreateVirtualRegister(PrimitiveType primitiveType, int index, MosaType type = null)
{
return new Operand()
return new Operand
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.Object,
Register = register,
Index = index
Location = LocationType.VirtualRegister,
Primitive = primitiveType,
Index = index,
Type = primitiveType == PrimitiveType.ValueType ? type : null,
};
}

public static Operand CreateCPURegisterManagedPointer(PhysicalRegister register, int index = 0)
internal static Operand CreateCPURegister(PrimitiveType primitiveType, PhysicalRegister register, int index)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = PrimitiveType.ManagedPointer,
Register = register,
Index = index
};
}

public static Operand CreateCPURegisterNativeInteger(PhysicalRegister register, bool is32Platform, int index = 0)
{
return new Operand
{
Location = LocationType.PhysicalRegister,
Primitive = is32Platform ? PrimitiveType.Int32 : PrimitiveType.Int64,
Primitive = primitiveType,
Register = register,
Index = index
};
}

#endregion Factory Methods - CPURegister

#region Factory Methods - Standard

public static Operand CreateVirtualRegister(PrimitiveType primitiveType, int index, MosaType type = null)
{
return new Operand
{
Location = LocationType.VirtualRegister,
Primitive = primitiveType,
Index = index,
Type = primitiveType == PrimitiveType.ValueType ? type : null,
};
}

public static Operand CreateStackLocal(PrimitiveType primitiveType, int index, bool pinned, MosaType type = null)
internal static Operand CreateStackLocal(PrimitiveType primitiveType, int index, bool pinned, MosaType type = null)
{
return new Operand
{
Expand All @@ -679,7 +592,7 @@ public static Operand CreateStackLocal(PrimitiveType primitiveType, int index, b
};
}

public static Operand CreateStackParameter(PrimitiveType primitiveType, ElementType elementType, int index, string name, int offset, uint size, MosaType type = null)
internal static Operand CreateStackParameter(PrimitiveType primitiveType, ElementType elementType, int index, string name, int offset, uint size, MosaType type = null)
{
return new Operand
{
Expand All @@ -695,17 +608,6 @@ public static Operand CreateStackParameter(PrimitiveType primitiveType, ElementT
};
}

public static Operand CreateCPURegister(PrimitiveType primitiveType, PhysicalRegister register, int index = 0)
{
return new Operand()
{
Location = LocationType.PhysicalRegister,
Primitive = primitiveType,
Register = register,
Index = index
};
}

#endregion Factory Methods - Standard

#region Factory Methods - Long Operand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ public override void Transform(Context context, Transform transform)
}

var handlerBlock = transform.BasicBlocks.GetByLabel(handler.HandlerStart);
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.MoveObject, transform.LeaveTargetRegister, Operand.CreateConstant32(target.Label));
context.AppendInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.MoveObject, exceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.Jmp, handlerBlock);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public ExceptionStart() : base(IRInstruction.ExceptionStart, TransformType.Manua
public override void Transform(Context context, Transform transform)
{
var exceptionVirtualRegister = context.Result;
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.KillAll);
context.AppendInstruction(IRInstruction.Gen, transform.ExceptionRegister);
context.AppendInstruction(IRInstruction.MoveObject, exceptionVirtualRegister, transform.ExceptionRegister);
context.AppendInstruction(IRInstruction.Gen, exceptionRegister);
context.AppendInstruction(IRInstruction.MoveObject, exceptionVirtualRegister, exceptionRegister);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public override void Transform(Context context, Transform transform)
context.SetInstruction(IRInstruction.BranchObject, ConditionCode.NotEqual, null, exceptionVirtualRegister, Operand.NullObject, exceptionCallBlock.Block);
context.AppendInstruction(IRInstruction.Jmp, newBlocks[1].Block);

exceptionCallBlock.AppendInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, exceptionVirtualRegister);
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

exceptionCallBlock.AppendInstruction(IRInstruction.MoveObject, exceptionRegister, exceptionVirtualRegister);
exceptionCallBlock.AppendInstruction(IRInstruction.CallStatic, null, Operand.CreateLabel(exceptionManager.ExceptionHandler, transform.Is32BitPlatform));

transform.MethodScanner.MethodInvoked(exceptionManager.ExceptionHandler, transform.Method);
Expand All @@ -74,7 +76,7 @@ public override void Transform(Context context, Transform transform)

if (next != null)
{
finallyCallBlock.AppendInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, Operand.NullObject);
finallyCallBlock.AppendInstruction(IRInstruction.MoveObject, exceptionRegister, Operand.NullObject);
finallyCallBlock.AppendInstruction(IRInstruction.MoveObject, transform.LeaveTargetRegister, leaveTargetRegister);
finallyCallBlock.AppendInstruction(IRInstruction.Jmp, transform.BasicBlocks.GetByLabel(next.HandlerStart));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public override void Transform(Context context, Transform transform)

var exceptionVirtualRegister = context.Result;
var leaveTargetVirtualRegister = context.Result2;
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.KillAll);
context.AppendInstruction(IRInstruction.Gen, transform.ExceptionRegister);
context.AppendInstruction(IRInstruction.Gen, exceptionRegister);
context.AppendInstruction(IRInstruction.Gen, transform.LeaveTargetRegister);

context.AppendInstruction(IRInstruction.MoveObject, exceptionVirtualRegister, transform.ExceptionRegister);
context.AppendInstruction(IRInstruction.MoveObject, exceptionVirtualRegister, exceptionRegister);
context.AppendInstruction(IRInstruction.MoveObject, leaveTargetVirtualRegister, transform.LeaveTargetRegister);

exceptionManager.ExceptionVirtualRegisters.Add(context.Block, exceptionVirtualRegister);
Expand Down
3 changes: 2 additions & 1 deletion Source/Mosa.Compiler.Framework/Transforms/Exception/Throw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public Throw() : base(IRInstruction.Throw, TransformType.Manual | TransformType.
public override void Transform(Context context, Transform transform)
{
var method = transform.Compiler.PlatformInternalRuntimeType.FindMethodByName("ExceptionHandler");
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, context.Operand1);
context.SetInstruction(IRInstruction.MoveObject, exceptionRegister, context.Operand1);
context.AppendInstruction(IRInstruction.CallStatic, null, Operand.CreateLabel(method, transform.Is32BitPlatform));

transform.MethodScanner.MethodInvoked(method, transform.Method);
Expand Down
8 changes: 6 additions & 2 deletions Source/Mosa.Compiler.Framework/Transforms/Exception/TryEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public override void Transform(Context context, Transform transform)

if (immediate.ExceptionHandlerType == ExceptionHandlerType.Finally)
{
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.MoveObject, transform.LeaveTargetRegister, Operand.CreateConstant32(target.Label));
context.AppendInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.MoveObject, exceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.Jmp, transform.BasicBlocks.GetByLabel(immediate.HandlerStart));
return;
}
Expand All @@ -36,8 +38,10 @@ public override void Transform(Context context, Transform transform)

if (next != null && next.HandlerEnd > immediate.HandlerEnd)
{
var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

context.SetInstruction(IRInstruction.MoveObject, transform.LeaveTargetRegister, Operand.CreateConstant32(target.Label));
context.AppendInstruction(IRInstruction.MoveObject, transform.ExceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.MoveObject, exceptionRegister, Operand.NullObject);
context.AppendInstruction(IRInstruction.Jmp, transform.BasicBlocks.GetByLabel(next.HandlerStart));
return;
}
Expand Down
4 changes: 3 additions & 1 deletion Source/Mosa.Compiler.x64/Intrinsic/FrameJump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ private static void FrameJump(Context context, Transform transform)
var rbx = transform.PhysicalRegisters.Allocate64(CPURegister.RBX);
var rcx = transform.PhysicalRegisters.Allocate64(CPURegister.RCX);

var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

// Move all virtual registers into physical registers - necessary since stack frame pointer will change
context.SetInstruction(X64.Mov64, rax, v0);
context.AppendInstruction(X64.Mov64, rbx, v1);
context.AppendInstruction(X64.Mov64, rcx, v2);
context.AppendInstruction(X64.Mov64, transform.ExceptionRegister, v3);
context.AppendInstruction(X64.Mov64, exceptionRegister, v3);

// Update the frame and stack registers
context.AppendInstruction(X64.Mov64, rbp, rcx);
Expand Down
4 changes: 3 additions & 1 deletion Source/Mosa.Compiler.x86/Intrinsic/FrameJump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ private static void FrameJump(Context context, Transform transform)
var ebx = transform.PhysicalRegisters.Allocate32(CPURegister.EBX);
var ecx = transform.PhysicalRegisters.Allocate32(CPURegister.ECX);

var exceptionRegister = transform.PhysicalRegisters.AllocateObject(transform.Architecture.ExceptionRegister);

// Move all virtual registers into physical registers - necessary since stack frame pointer will change
context.SetInstruction(X86.Mov32, eax, v0);
context.AppendInstruction(X86.Mov32, ebx, v1);
context.AppendInstruction(X86.Mov32, ecx, v2);
context.AppendInstruction(X86.Mov32, transform.ExceptionRegister, v3);
context.AppendInstruction(X86.Mov32, exceptionRegister, v3);

// Update the frame and stack registers
context.AppendInstruction(X86.Mov32, ebp, ecx);
Expand Down

0 comments on commit c4a1c9d

Please sign in to comment.