-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Sub_Ovh implementation. #64
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,66 @@ | ||
using System; | ||
using CPUx86 = XSharp.Assembler.x86; | ||
using XSharp.Assembler.x86; | ||
|
||
using XSharp; | ||
using static XSharp.XSRegisters; | ||
using System.Reflection; | ||
|
||
namespace Cosmos.IL2CPU.X86.IL | ||
{ | ||
[Cosmos.IL2CPU.OpCode(ILOpCode.Code.Sub_Ovf)] | ||
public class Sub_Ovf: ILOp | ||
{ | ||
public Sub_Ovf(XSharp.Assembler.Assembler aAsmblr):base(aAsmblr) { | ||
} | ||
[Cosmos.IL2CPU.OpCode(ILOpCode.Code.Sub_Ovf)] | ||
public class Sub_Ovf : ILOp | ||
{ | ||
public Sub_Ovf(XSharp.Assembler.Assembler aAsmblr) : base(aAsmblr) | ||
{ | ||
} | ||
|
||
public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode) | ||
{ | ||
var xType = aOpCode.StackPopTypes[0]; | ||
var xSize = SizeOfType(xType); | ||
var xIsFloat = TypeIsFloat(xType); | ||
if (xIsFloat) | ||
{ | ||
throw new Exception("Cosmos.IL2CPU.x86->IL->Sub_Ovf.cs->Error: Expected signed integer operands but get float!"); | ||
} | ||
|
||
if (xSize > 8) | ||
{ | ||
throw new NotImplementedException("Cosmos.IL2CPU.x86->IL->Sub_Ovf.cs->Error: StackSize > 8 not supported"); | ||
} | ||
else | ||
{ | ||
var xBaseLabel = GetLabel(aMethod, aOpCode) + "."; | ||
var xSuccessLabel = xBaseLabel + "Success"; | ||
if (xSize > 4) // long | ||
{ | ||
XS.Pop(EAX);//low part | ||
XS.Pop(EDX);//high part | ||
XS.Sub(ESP, EAX, destinationIsIndirect: true); | ||
XS.SubWithCarry(ESP, EDX, destinationDisplacement: 4); | ||
|
||
} | ||
else //integer | ||
{ | ||
XS.Pop(ECX);//first integer | ||
XS.Pop(EAX);//second integer | ||
XS.Sub(EAX, ECX); | ||
XS.Push(EAX);//push result on stack | ||
} | ||
|
||
public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode) { | ||
//if (Assembler.Stack.Peek().IsFloat) { | ||
// throw new NotImplementedException("Sub_Ovf: TODO need to call Sub IL"); | ||
//} | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
// Let's check if we add overflow and if so throw OverflowException | ||
XS.Jump(ConditionalTestEnum.NoOverflow, xSuccessLabel); | ||
if (xSize > 4) // Hack to stop stack corruption | ||
{ | ||
XS.Add(ESP, 8); | ||
} | ||
else | ||
{ | ||
XS.Add(ESP, 4); | ||
} | ||
Call.DoExecute(Assembler, aMethod, typeof(ExceptionHelper).GetMethod("ThrowOverflow", BindingFlags.Static | BindingFlags.Public), aOpCode, GetLabel(aMethod, aOpCode), xSuccessLabel, DebugEnabled); | ||
XS.Label(xSuccessLabel); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is something we will have to consider on exception handling. When throwing in the middle of an opcode, this will probably work for most of the simple cases, but not sure about more complex ones. I'm not sure about what the CLI spec defines for the stack, still have to read that, but if the following is valid IL:
And we throw at the first
add.ovf
, the stack is not in a good state to jump to the catch block (still has the other local on the stack, which was expected to be consumed by the secondadd.ovf
). Roslyn seems to emit good IL for this (not sure if by spec, still have to check), so this should work for now.Also, I think we should file an issue about this or add this info to #16.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to ECMA-335, the
throw
opcode description is this:So I think we should always empty the stack when throwing an exception, we should add some common infrastructure to throw exceptions, probably part of #16, I'll add a note there.