Cosmos/source/Cosmos.IL2CPU/IL/Sub.cs
fanoI 6e1bfb8d92 - Implemented Neg Opcode for floating point
- Added test for Neg OpCode for float and double
- Conv.U8 wrongly changed the sign of double before converting it to ulong
- Div_Un, Add_Ovf and Add_Ovf_Un had code for floating point but they should not have that for ECMA specs
2016-07-17 17:05:00 +02:00

92 lines
3.4 KiB
C#

using System;
using CPUx86 = Cosmos.Assembler.x86;
using Cosmos.Assembler.x86;
using Cosmos.Assembler.x86.x87;
using XSharp.Compiler;
using static XSharp.Compiler.XSRegisters;
namespace Cosmos.IL2CPU.X86.IL
{
[Cosmos.IL2CPU.OpCode(ILOpCode.Code.Sub)]
public class Sub: ILOp
{
public Sub(Cosmos.Assembler.Assembler aAsmblr)
: base(aAsmblr)
{
}
public override void Execute(MethodInfo aMethod, ILOpCode aOpCode)
{
var xStackTop = aOpCode.StackPopTypes[0];
var xStackTop2 = aOpCode.StackPopTypes[0];
var xStackTopSize = SizeOfType(xStackTop);
var xStackTop2Size = SizeOfType(xStackTop2);
if (xStackTopSize != xStackTop2Size)
{
throw new Exception("Different size for substract: " + aMethod.MethodBase + "!");
}
var xStackTopIsFloat = TypeIsFloat(xStackTop);
switch (xStackTopSize)
{
case 1:
case 2:
case 4:
if (xStackTopIsFloat)
{
XS.SSE.MoveSS(XMM0, ESP, sourceIsIndirect: true);
XS.Add(ESP, 4);
XS.SSE.MoveSS(XMM1, ESP, sourceIsIndirect: true);
//XS.LiteralCode("movss XMM1, [ESP + 4]");
XS.SSE.SubSS(XMM1, XMM0);
XS.SSE.MoveSS(ESP, XMM1, destinationIsIndirect: true);
}
else
{
XS.Pop(XSRegisters.ECX);
XS.Pop(XSRegisters.EAX);
XS.Sub(XSRegisters.EAX, XSRegisters.ECX);
XS.Push(XSRegisters.EAX);
}
break;
case 8:
if (xStackTopIsFloat)
{
XS.SSE2.MoveSD(XMM0, ESP, sourceIsIndirect: true);
XS.Add(ESP, 8);
XS.SSE2.MoveSD(XMM1, ESP, sourceIsIndirect: true);
XS.SSE2.SubSD(XMM1, XMM0);
XS.SSE2.MoveSD(ESP, XMM1, destinationIsIndirect: true);
#if false
new FloatLoad
{
DestinationReg = RegistersEnum.ESP,
Size = 64,
DestinationIsIndirect = true,
DestinationDisplacement = 8
};
new FloatSub
{
DestinationReg = RegistersEnum.ESP,
DestinationIsIndirect = true,
Size = 64
};
XS.Add(ESP, 8);
XS.FPU.FloatStoreAndPop(ESP, isIndirect: true, size: RegisterSize.Long64);
#endif
}
else
{
XS.Pop(EAX);
XS.Pop(EDX);
XS.Sub(ESP, EAX, destinationIsIndirect: true);
XS.SubWithCarry(ESP, EDX, destinationDisplacement: 4);
}
break;
default:
throw new NotImplementedException("not implemented");
}
}
}
}