|
2 | 2 | using XSharp.Assembler.x86; |
3 | 3 | using XSharp; |
4 | 4 | using static XSharp.XSRegisters; |
| 5 | +using System.Reflection; |
5 | 6 |
|
6 | 7 | /* Add.Ovf is signed integer addition with check for overflow */ |
7 | 8 | namespace Cosmos.IL2CPU.X86.IL |
8 | 9 | { |
9 | | - [OpCode(ILOpCode.Code.Add_Ovf)] |
10 | | - public class Add_Ovf : ILOp |
| 10 | + [OpCode(ILOpCode.Code.Add_Ovf)] |
| 11 | + public class Add_Ovf : ILOp |
| 12 | + { |
| 13 | + public Add_Ovf(XSharp.Assembler.Assembler aAsmblr) |
| 14 | + : base(aAsmblr) |
11 | 15 | { |
12 | | - public Add_Ovf(XSharp.Assembler.Assembler aAsmblr) |
13 | | - : base(aAsmblr) |
14 | | - { |
15 | | - } |
| 16 | + } |
16 | 17 |
|
17 | | - public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode) |
18 | | - { |
19 | | - var xType = aOpCode.StackPopTypes[0]; |
20 | | - var xSize = SizeOfType(xType); |
21 | | - var xIsFloat = TypeIsFloat(xType); |
| 18 | + public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode) |
| 19 | + { |
| 20 | + var xType = aOpCode.StackPopTypes[0]; |
| 21 | + var xSize = SizeOfType(xType); |
| 22 | + var xIsFloat = TypeIsFloat(xType); |
22 | 23 |
|
23 | | - if (xIsFloat) |
24 | | - { |
25 | | - throw new Exception("Cosmos.IL2CPU.x86->IL->Add_Ovf.cs->Error: Expected signed integer operands but get float!"); |
26 | | - } |
| 24 | + if (xIsFloat) |
| 25 | + { |
| 26 | + throw new Exception("Cosmos.IL2CPU.x86->IL->Add_Ovf.cs->Error: Expected signed integer operands but get float!"); |
| 27 | + } |
27 | 28 |
|
28 | | - if (xSize > 8) |
29 | | - { |
30 | | - //EmitNotImplementedException( Assembler, aServiceProvider, "Size '" + xSize.Size + "' not supported (add)", aCurrentLabel, aCurrentMethodInfo, aCurrentOffset, aNextLabel ); |
31 | | - throw new NotImplementedException("Cosmos.IL2CPU.x86->IL->Add_Ovf.cs->Error: StackSize > 8 not supported"); |
32 | | - } |
33 | | - else |
34 | | - { |
35 | | - var xBaseLabel = GetLabel(aMethod, aOpCode) + "."; |
36 | | - var xSuccessLabel = xBaseLabel + "Success"; |
37 | | - if (xSize > 4) // long |
38 | | - { |
39 | | - XS.Pop(EDX); // low part |
40 | | - XS.Pop(EAX); // high part |
41 | | - XS.Add(ESP, EDX, destinationIsIndirect: true); |
42 | | - XS.AddWithCarry(ESP, EAX, destinationDisplacement: 4); |
| 29 | + if (xSize > 8) |
| 30 | + { |
| 31 | + //EmitNotImplementedException( Assembler, aServiceProvider, "Size '" + xSize.Size + "' not supported (add)", aCurrentLabel, aCurrentMethodInfo, aCurrentOffset, aNextLabel ); |
| 32 | + throw new NotImplementedException("Cosmos.IL2CPU.x86->IL->Add_Ovf.cs->Error: StackSize > 8 not supported"); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + var xBaseLabel = GetLabel(aMethod, aOpCode) + "."; |
| 37 | + var xSuccessLabel = xBaseLabel + "Success"; |
| 38 | + if (xSize > 4) // long |
| 39 | + { |
| 40 | + XS.Pop(EDX); // low part |
| 41 | + XS.Pop(EAX); // high part |
| 42 | + XS.Add(ESP, EDX, destinationIsIndirect: true); |
| 43 | + XS.AddWithCarry(ESP, EAX, destinationDisplacement: 4); |
43 | 44 |
|
44 | | - } |
45 | | - else //integer |
46 | | - { |
| 45 | + } |
| 46 | + else //integer |
| 47 | + { |
47 | 48 |
|
48 | | - XS.Pop(EAX); |
49 | | - XS.Add(ESP, EAX, destinationIsIndirect: true); |
50 | | - } |
| 49 | + XS.Pop(EAX); |
| 50 | + XS.Add(ESP, EAX, destinationIsIndirect: true); |
| 51 | + } |
51 | 52 |
|
52 | | - // Let's check if we add overflow and if so throw OverflowException |
53 | | - XS.Jump(ConditionalTestEnum.NoOverflow, xSuccessLabel); |
54 | | - ThrowOverflowException(); |
55 | | - XS.Label(xSuccessLabel); |
56 | | - } |
| 53 | + // Let's check if we add overflow and if so throw OverflowException |
| 54 | + XS.Jump(ConditionalTestEnum.NoOverflow, xSuccessLabel); |
| 55 | + if (xSize > 4) // Hack to stop stack corruption |
| 56 | + { |
| 57 | + XS.Add(ESP, 8); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + XS.Add(ESP, 4); |
57 | 62 | } |
| 63 | + Call.DoExecute(Assembler, aMethod, typeof(ExceptionHelper).GetMethod("ThrowOverflow", BindingFlags.Static | BindingFlags.Public), aOpCode, GetLabel(aMethod, aOpCode), xSuccessLabel, DebugEnabled); |
| 64 | + XS.Label(xSuccessLabel); |
| 65 | + } |
58 | 66 | } |
| 67 | + } |
59 | 68 | } |
0 commit comments