From 9d203f141ed8300bbd4497f1b8df6daf2f3fcbca Mon Sep 17 00:00:00 2001 From: Matthijs ter Woord Date: Thu, 16 Jun 2016 20:08:41 -0400 Subject: [PATCH] . --- source/Cosmos.IL2CPU/IL/Box.cs | 2 +- source/Cosmos.IL2CPU/IL/Shr.cs | 9 +++-- source/Cosmos.IL2CPU/IL/Shr_Un.cs | 31 ++++++++------- source/XSharp.Compiler/XS.cs | 65 +++++++++++++++++++++++-------- 4 files changed, 71 insertions(+), 36 deletions(-) diff --git a/source/Cosmos.IL2CPU/IL/Box.cs b/source/Cosmos.IL2CPU/IL/Box.cs index d21454db5..9b3dcef21 100644 --- a/source/Cosmos.IL2CPU/IL/Box.cs +++ b/source/Cosmos.IL2CPU/IL/Box.cs @@ -34,7 +34,7 @@ namespace Cosmos.IL2CPU.X86.IL for (int i = 0; i < (xSize / 4); i++) { XS.Pop(OldToNewRegister(CPUx86.RegistersEnum.EDX)); - new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.ESI, DestinationIsIndirect = true, DestinationDisplacement = (ObjectImpl.FieldDataOffset + (i * 4)), SourceReg = CPUx86.RegistersEnum.EDX, Size = 32 }; + XS.Set(ESI, EDX, destinationDisplacement: (ObjectImpl.FieldDataOffset + (i * 4)), size: RegisterSize.Int32); } XS.Push(OldToNewRegister(CPUx86.RegistersEnum.EAX)); } diff --git a/source/Cosmos.IL2CPU/IL/Shr.cs b/source/Cosmos.IL2CPU/IL/Shr.cs index 5408db3b0..27bba07cd 100644 --- a/source/Cosmos.IL2CPU/IL/Shr.cs +++ b/source/Cosmos.IL2CPU/IL/Shr.cs @@ -3,6 +3,7 @@ using CPU = Cosmos.Assembler.x86; using CPUx86 = Cosmos.Assembler.x86; using Cosmos.Assembler; using XSharp.Compiler; +using static XSharp.Compiler.XSRegisters; namespace Cosmos.IL2CPU.X86.IL { @@ -16,7 +17,7 @@ namespace Cosmos.IL2CPU.X86.IL public override void Execute( MethodInfo aMethod, ILOpCode aOpCode ) { - XS.Pop(XSRegisters.OldToNewRegister(CPU.RegistersEnum.ECX)); // shift amount + XS.Pop(OldToNewRegister(CPU.RegistersEnum.ECX)); // shift amount var xStackItem_ShiftAmount = aOpCode.StackPopTypes[0]; var xStackItem_Value = aOpCode.StackPopTypes[1]; var xStackItem_Value_Size = SizeOfType(xStackItem_Value); @@ -42,7 +43,7 @@ namespace Cosmos.IL2CPU.X86.IL // [ESP + 4] is high part // move high part in EAX - XS.Set(XSRegisters.OldToNewRegister(CPU.RegistersEnum.EAX), XSRegisters.OldToNewRegister(CPU.RegistersEnum.ESP), sourceDisplacement: 4); + XS.Set(OldToNewRegister(CPU.RegistersEnum.EAX), OldToNewRegister(CPU.RegistersEnum.ESP), sourceDisplacement: 4); new CPUx86.Compare { DestinationReg = CPUx86.RegistersEnum.CL, SourceValue = 32, Size = 8 }; XS.Jump(CPU.ConditionalTestEnum.AboveOrEqual, HighPartIsZero); @@ -50,7 +51,7 @@ namespace Cosmos.IL2CPU.X86.IL // shift lower part new CPUx86.ShiftRightDouble { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true, SourceReg = CPUx86.RegistersEnum.EAX, ArgumentReg = CPUx86.RegistersEnum.CL }; // shift higher part - new CPUx86.ShiftRight { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true, DestinationDisplacement = 4, Size = 32, SourceReg = CPUx86.RegistersEnum.CL }; + XS.ShiftRight(ESP, CL, destinationDisplacement: 4, size: RegisterSize.Int32); XS.Jump(End_Shr); XS.Label(HighPartIsZero); @@ -59,7 +60,7 @@ namespace Cosmos.IL2CPU.X86.IL // shift high part and move it in low part new CPUx86.ShiftRight{ DestinationReg = CPUx86.RegistersEnum.EAX, Size = 32, SourceReg = CPUx86.RegistersEnum.CL }; - XS.Set(XSRegisters.ESP, XSRegisters.EAX, destinationIsIndirect: true); + XS.Set(ESP, EAX, destinationIsIndirect: true); // replace unknown high part with a zero, if <= 32 new CPUx86.Mov { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true, DestinationDisplacement = 4, SourceValue = 0}; diff --git a/source/Cosmos.IL2CPU/IL/Shr_Un.cs b/source/Cosmos.IL2CPU/IL/Shr_Un.cs index 885b5290a..1ecd7432f 100644 --- a/source/Cosmos.IL2CPU/IL/Shr_Un.cs +++ b/source/Cosmos.IL2CPU/IL/Shr_Un.cs @@ -3,6 +3,7 @@ using CPUx86 = Cosmos.Assembler.x86; using CPU = Cosmos.Assembler.x86; using Cosmos.Assembler; using XSharp.Compiler; +using static XSharp.Compiler.XSRegisters; namespace Cosmos.IL2CPU.X86.IL { @@ -26,27 +27,27 @@ namespace Cosmos.IL2CPU.X86.IL var xStackItem_Value_Size = SizeOfType(xStackItem_Value); if( xStackItem_Value_Size <= 4 ) { - XS.Pop(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX)); // shift amount - XS.Pop(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EBX)); // value - XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.CL), XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.AL)); - XS.ShiftRight(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EBX), XSRegisters.CL); - XS.Push(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EBX)); + XS.Pop(OldToNewRegister(CPUx86.RegistersEnum.EAX)); // shift amount + XS.Pop(OldToNewRegister(CPUx86.RegistersEnum.EBX)); // value + XS.Set(OldToNewRegister(CPUx86.RegistersEnum.CL), OldToNewRegister(CPUx86.RegistersEnum.AL)); + XS.ShiftRight(OldToNewRegister(CPUx86.RegistersEnum.EBX), CL); + XS.Push(OldToNewRegister(CPUx86.RegistersEnum.EBX)); return; } if( xStackItem_Value_Size <= 8 ) { - XS.Pop(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EDX)); - XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX), 0); + XS.Pop(OldToNewRegister(CPUx86.RegistersEnum.EDX)); + XS.Set(OldToNewRegister(CPUx86.RegistersEnum.EAX), 0); XS.Label(xBaseLabel + "__StartLoop" ); - XS.Compare(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EDX), XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX)); + XS.Compare(OldToNewRegister(CPUx86.RegistersEnum.EDX), OldToNewRegister(CPUx86.RegistersEnum.EAX)); XS.Jump(CPUx86.ConditionalTestEnum.Equal, xBaseLabel + "__EndLoop"); - XS.Set(XSRegisters.EBX, XSRegisters.ESP, sourceIsIndirect: true); - XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.CL), 1); - XS.ShiftRight(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EBX), XSRegisters.CL); - XS.Set(XSRegisters.ESP, XSRegisters.EBX, destinationIsIndirect: true); - XS.Set(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.CL), 1); - new CPUx86.RotateThroughCarryRight { DestinationReg = CPUx86.RegistersEnum.ESP, DestinationIsIndirect = true, DestinationDisplacement = 4, Size = 32, SourceReg = CPUx86.RegistersEnum.CL }; - XS.Add(XSRegisters.OldToNewRegister(CPUx86.RegistersEnum.EAX), 1); + XS.Set(EBX, ESP, sourceIsIndirect: true); + XS.Set(OldToNewRegister(CPUx86.RegistersEnum.CL), 1); + XS.ShiftRight(OldToNewRegister(CPUx86.RegistersEnum.EBX), CL); + XS.Set(ESP, EBX, destinationIsIndirect: true); + XS.Set(OldToNewRegister(CPUx86.RegistersEnum.CL), 1); + XS.RotateThroughCarryRight(ESP, CL, destinationDisplacement: 4, size: RegisterSize.Int32); + XS.Add(OldToNewRegister(CPUx86.RegistersEnum.EAX), 1); new CPUx86.Jump { DestinationLabel = xBaseLabel + "__StartLoop" }; XS.Label(xBaseLabel + "__EndLoop" ); diff --git a/source/XSharp.Compiler/XS.cs b/source/XSharp.Compiler/XS.cs index 2a1f9f184..a7dc23588 100644 --- a/source/XSharp.Compiler/XS.cs +++ b/source/XSharp.Compiler/XS.cs @@ -227,7 +227,8 @@ namespace XSharp.Compiler int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, - bool skipSizeCheck = false) + bool skipSizeCheck = false, + RegisterSize? size = null) where T : InstructionWithDestinationAndSourceAndSize, new() { if (destinationDisplacement != null) @@ -256,19 +257,21 @@ namespace XSharp.Compiler { throw new Exception("Both destination and source cannot be indirect!"); } - RegisterSize xSize; - if (!destinationIsIndirect) + if (size == null) { - xSize = destination.Size; - } - else - { - xSize = source.Size; + if (!destinationIsIndirect) + { + size = destination.Size; + } + else + { + size = source.Size; + } } new T { - Size = (byte)xSize, + Size = (byte)size, DestinationReg = destination.RegEnum, DestinationIsIndirect = destinationIsIndirect, DestinationDisplacement = destinationDisplacement, @@ -591,9 +594,9 @@ namespace XSharp.Compiler Do(destination, value, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); } - public static void Set(Register destination, Register source, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null) + public static void Set(Register destination, Register source, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize? size = null) { - Do(destination, source, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement); + Do(destination, source, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size: size); } public static void SetByte(uint address, byte value) @@ -658,18 +661,18 @@ namespace XSharp.Compiler Do(register, bitCount); } - public static void ShiftRight(Register register, byte bitCount) + public static void ShiftRight(Register destination, byte bitCount) { - Do(register, bitCount); + Do(destination, bitCount); } - public static void ShiftRight(Register register, Register8 bitCount) + public static void ShiftRight(Register destination, Register8 source, bool destinationIsIndirect = false, int? destinationDisplacement = null, RegisterSize? size = null) { - if (bitCount != CL) + if (source != CL) { throw new InvalidOperationException(); } - Do(register, bitCount, skipSizeCheck: true); + Do(destination, source, skipSizeCheck: true, destinationIsIndirect:destinationIsIndirect, destinationDisplacement: destinationDisplacement, size: size); } public static void ShiftLeft(Register register, byte bitCount) @@ -1113,5 +1116,35 @@ namespace XSharp.Compiler DestinationIsIndirect = isIndirect }; } + + public static void RotateThroughCarryRight(string destination, Register source, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize? size = null) + { + Do(destination, source, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); + } + + public static void RotateThroughCarryRight(string destination, UInt32 value, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize size = RegisterSize.Int32) + { + Do(destination, value, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); + } + + public static void RotateThroughCarryRight(string destination, string source, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize size = RegisterSize.Int32) + { + Do(destination, source, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); + } + + public static void RotateThroughCarryRight(Register destination, string sourceLabel, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize? size = null) + { + Do(destination, sourceLabel, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); + } + + public static void RotateThroughCarryRight(Register destination, uint value, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize? size = null) + { + Do(destination, value, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size); + } + + public static void RotateThroughCarryRight(Register destination, Register source, bool destinationIsIndirect = false, int? destinationDisplacement = null, bool sourceIsIndirect = false, int? sourceDisplacement = null, RegisterSize? size = null) + { + Do(destination, source, destinationIsIndirect, destinationDisplacement, sourceIsIndirect, sourceDisplacement, size: size); + } } }