diff --git a/Source/Mosa.Compiler.Framework/Operand.cs b/Source/Mosa.Compiler.Framework/Operand.cs index 211011e74c..0aad7a4cb1 100644 --- a/Source/Mosa.Compiler.Framework/Operand.cs +++ b/Source/Mosa.Compiler.Framework/Operand.cs @@ -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 { @@ -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 { @@ -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 diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionEnd.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionEnd.cs index 11c8ce09c3..6e1721fc63 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionEnd.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionEnd.cs @@ -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); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionStart.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionStart.cs index a2c641fb92..436e5e9eba 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionStart.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/ExceptionStart.cs @@ -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); } } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyEnd.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyEnd.cs index fd4f6ee783..f7ef01cc0b 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyEnd.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyEnd.cs @@ -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); @@ -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)); } diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyStart.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyStart.cs index e38a122854..778fa91389 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyStart.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyStart.cs @@ -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); diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/Throw.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/Throw.cs index d7a587b835..daa05f5473 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/Throw.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/Throw.cs @@ -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); diff --git a/Source/Mosa.Compiler.Framework/Transforms/Exception/TryEnd.cs b/Source/Mosa.Compiler.Framework/Transforms/Exception/TryEnd.cs index 6c997639a3..713d6f63c4 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/Exception/TryEnd.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/Exception/TryEnd.cs @@ -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; } @@ -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; } diff --git a/Source/Mosa.Compiler.x64/Intrinsic/FrameJump.cs b/Source/Mosa.Compiler.x64/Intrinsic/FrameJump.cs index 67c25ec59a..84d5cbec3c 100644 --- a/Source/Mosa.Compiler.x64/Intrinsic/FrameJump.cs +++ b/Source/Mosa.Compiler.x64/Intrinsic/FrameJump.cs @@ -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); diff --git a/Source/Mosa.Compiler.x86/Intrinsic/FrameJump.cs b/Source/Mosa.Compiler.x86/Intrinsic/FrameJump.cs index 1592e57a13..8644009151 100644 --- a/Source/Mosa.Compiler.x86/Intrinsic/FrameJump.cs +++ b/Source/Mosa.Compiler.x86/Intrinsic/FrameJump.cs @@ -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);