Example #1
0
        public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
        {
            // apparently, Roslyn changed something to the output. We now have to figure out where to jump to.
            if (aOpCode.CurrentExceptionRegion.Kind.HasFlag(ExceptionRegionKind.Finally) &&
                aOpCode.CurrentExceptionRegion.HandlerOffset > aOpCode.Position)
            {
                XS.Set(aMethod.MethodBase.GetFullName() + "_" + "LeaveAddress_" + aOpCode.CurrentExceptionRegion.HandlerOffset.ToString("X2"), Assembler.CurrentIlLabel + "." + (Assembler.AsmIlIdx + 2).ToString("X2"), destinationIsIndirect: true, size: RegisterSize.Int32);
                XS.Jump(AppAssembler.TmpPosLabel(aMethod, aOpCode.CurrentExceptionRegion.HandlerOffset));
            }

            XS.Jump(AppAssembler.TmpBranchLabel(aMethod, aOpCode));
        }
Example #2
0
 public override void Execute(MethodInfo aMethod, ILOpCode aOpCode)
 {
     // apparently, Roslyn changed something to the output. We now have to figure out where to jump to.
     if (aOpCode.CurrentExceptionHandler.Flags.HasFlag(ExceptionHandlingClauseOptions.Finally) &&
         aOpCode.CurrentExceptionHandler.HandlerOffset > aOpCode.Position)
     {
         new CPUx86.Jump {
             DestinationLabel = AppAssembler.TmpPosLabel(aMethod, aOpCode.CurrentExceptionHandler.HandlerOffset)
         };
     }
     else
     {
         new CPUx86.Jump {
             DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
         };
     }
 }
Example #3
0
        public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
        {
            string jumpTarget = AppAssembler.TmpBranchLabel(aMethod, aOpCode);

            // apparently, Roslyn changed something to the output. We now have to figure out where to jump to.
            if (aOpCode.CurrentExceptionRegion.Kind.HasFlag(ExceptionRegionKind.Finally) &&
                aOpCode.CurrentExceptionRegion.HandlerOffset > aOpCode.Position)
            {
                string destination = $"{aMethod.MethodBase.GetFullName()}_LeaveAddress_{aOpCode.CurrentExceptionRegion.HandlerOffset:X2}";
                string source      = AppAssembler.TmpBranchLabel(aMethod, aOpCode);
                XS.Set(destination, source, destinationIsIndirect: true, size: RegisterSize.Int32);
                XS.Jump(AppAssembler.TmpPosLabel(aMethod, aOpCode.CurrentExceptionRegion.HandlerOffset));
            }
            else
            {
                XS.Jump(jumpTarget);
            }
        }
Example #4
0
 public override void Execute(MethodInfo aMethod, ILOpCode aOpCode)
 {
     new CPUx86.Jump {
         DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
     };
 }
Example #5
0
 public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
 {
     XS.Jump(AppAssembler.TmpBranchLabel(aMethod, aOpCode));
     //new CPU.Jump { DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode) };
 }
        public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
        {
            var xIsSingleCompare = true;

            switch (aOpCode.OpCode)
            {
            case ILOpCode.Code.Beq:
            case ILOpCode.Code.Bge:
            case ILOpCode.Code.Bgt:
            case ILOpCode.Code.Bge_Un:
            case ILOpCode.Code.Bgt_Un:
            case ILOpCode.Code.Ble:
            case ILOpCode.Code.Ble_Un:
            case ILOpCode.Code.Bne_Un:
            case ILOpCode.Code.Blt:
            case ILOpCode.Code.Blt_Un:
                xIsSingleCompare = false;
                break;
            }

            var xStackContent     = aOpCode.StackPopTypes[0];
            var xStackContentSize = SizeOfType(xStackContent);

            if (xStackContentSize > 8)
            {
                throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: StackSize > 8 not supported");
            }

            CPU.ConditionalTestEnum xTestOp;
            // all conditions are inverted here?
            switch (aOpCode.OpCode)
            {
            case ILOpCode.Code.Beq:
                xTestOp = CPU.ConditionalTestEnum.Zero;
                break;

            case ILOpCode.Code.Bge:
                xTestOp = CPU.ConditionalTestEnum.GreaterThanOrEqualTo;
                break;

            case ILOpCode.Code.Bgt:
                xTestOp = CPU.ConditionalTestEnum.GreaterThan;
                break;

            case ILOpCode.Code.Ble:
                xTestOp = CPU.ConditionalTestEnum.LessThanOrEqualTo;
                break;

            case ILOpCode.Code.Blt:
                xTestOp = CPU.ConditionalTestEnum.LessThan;
                break;

            case ILOpCode.Code.Bne_Un:
                xTestOp = CPU.ConditionalTestEnum.NotEqual;
                break;

            case ILOpCode.Code.Bge_Un:
                xTestOp = CPU.ConditionalTestEnum.AboveOrEqual;
                break;

            case ILOpCode.Code.Bgt_Un:
                xTestOp = CPU.ConditionalTestEnum.Above;
                break;

            case ILOpCode.Code.Ble_Un:
                xTestOp = CPU.ConditionalTestEnum.BelowOrEqual;
                break;

            case ILOpCode.Code.Blt_Un:
                xTestOp = CPU.ConditionalTestEnum.Below;
                break;

            case ILOpCode.Code.Brfalse:
                xTestOp = CPU.ConditionalTestEnum.Zero;
                break;

            case ILOpCode.Code.Brtrue:
                xTestOp = CPU.ConditionalTestEnum.NotZero;
                break;

            default:
                throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Unknown OpCode for conditional branch.");
            }
            if (!xIsSingleCompare)
            {
                if (xStackContentSize <= 4)
                {
                    //if (xStackContent.IsFloat)
                    //{
                    //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Comparison of floats (System.Single) is not yet supported!");
                    //}
                    //else
                    //{
                    XS.Pop(XSRegisters.EAX);
                    XS.Pop(XSRegisters.EBX);
                    XS.Compare(XSRegisters.EBX, XSRegisters.EAX);
                    new ConditionalJump {
                        Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                    };
                    //}
                }
                else
                {
                    //if (xStackContent.IsFloat)
                    //{
                    //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Comparison of doubles (System.Double) is not yet supported!");
                    //}
                    //else
                    //{
                    var xNoJump = GetLabel(aMethod, aOpCode) + "__NoBranch";

                    // value 2  EBX:EAX
                    XS.Pop(XSRegisters.EAX);
                    XS.Pop(XSRegisters.EBX);
                    // value 1  EDX:ECX
                    XS.Pop(XSRegisters.ECX);
                    XS.Pop(XSRegisters.EDX);
                    switch (xTestOp)
                    {
                    case ConditionalTestEnum.Zero:     // Equal
                    case ConditionalTestEnum.NotEqual: // NotZero
                        XS.Xor(XSRegisters.EAX, XSRegisters.ECX);
                        new ConditionalJump {
                            Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Xor(XSRegisters.EBX, XSRegisters.EDX);
                        new ConditionalJump {
                            Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.GreaterThanOrEqualTo:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        XS.Jump(ConditionalTestEnum.LessThan, xNoJump);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.GreaterThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        XS.Jump(ConditionalTestEnum.Below, xNoJump);
                        break;

                    case ConditionalTestEnum.GreaterThan:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        XS.Jump(ConditionalTestEnum.LessThan, xNoJump);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.GreaterThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        XS.Jump(ConditionalTestEnum.BelowOrEqual, xNoJump);
                        break;

                    case ConditionalTestEnum.LessThanOrEqualTo:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.LessThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Jump(ConditionalTestEnum.GreaterThan, xNoJump);
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.BelowOrEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.LessThan:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.LessThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Jump(ConditionalTestEnum.GreaterThan, xNoJump);
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Below, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    // from here all unsigned
                    case ConditionalTestEnum.AboveOrEqual:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        XS.Jump(ConditionalTestEnum.Below, xNoJump);
                        break;

                    case ConditionalTestEnum.Above:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        XS.Jump(ConditionalTestEnum.BelowOrEqual, xNoJump);
                        break;

                    case ConditionalTestEnum.BelowOrEqual:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Jump(ConditionalTestEnum.Below, xNoJump);
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.Below:
                        XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        XS.Jump(ConditionalTestEnum.Below, xNoJump);
                        XS.Compare(XSRegisters.ECX, XSRegisters.EAX);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.AboveOrEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    default:
                        throw new Exception("Unknown OpCode for conditional branch in 64-bit.");
                    }
                    XS.Label(xNoJump);
                    //}
                }
            }
            else
            {
                //if (xStackContent.IsFloat)
                //{
                //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Simple comparison of floating point numbers is not yet supported!");
                //}
                //else
                //{
                // todo: improve code clarity
                if (xStackContentSize <= 4)
                {
                    XS.Pop(XSRegisters.EAX);
                    if (xTestOp == ConditionalTestEnum.Zero)
                    {
                        XS.Compare(XSRegisters.EAX, 0);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.Equal, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                    }
                    else if (xTestOp == ConditionalTestEnum.NotZero)
                    {
                        XS.Compare(XSRegisters.EAX, 0);
                        new ConditionalJump {
                            Condition = ConditionalTestEnum.NotEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                    }
                    else
                    {
                        throw new NotSupportedException("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Situation not supported yet! (In the Simple Comparison)");
                    }
                }
                else
                {
                    if (TypeIsReferenceType(xStackContent))
                    {
                        XS.Add(XSRegisters.ESP, 4);
                        XS.Pop(XSRegisters.EAX);
                    }
                    else
                    {
                        XS.Pop(XSRegisters.EAX);
                        XS.Pop(XSRegisters.EBX);
                    }

                    switch (xTestOp)
                    {
                    case ConditionalTestEnum.Zero:    // Equal
                    case ConditionalTestEnum.NotZero: // NotEqual
                        if (TypeIsReferenceType(xStackContent))
                        {
                            XS.Xor(XSRegisters.EAX, 0);
                            new ConditionalJump {
                                Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                            };
                        }
                        else
                        {
                            XS.Xor(XSRegisters.EAX, 0);
                            new ConditionalJump {
                                Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                            };
                            XS.Xor(XSRegisters.EBX, 0);
                            new ConditionalJump {
                                Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                            };
                        }
                        break;

                    default:
                        throw new NotImplementedException("Cosmos.IL2CPU.X86.IL.Branch: Simple branch " + aOpCode.OpCode + " not implemented for operand ");
                    }
                }
            }
            //}
        }
Example #7
0
        public override void Execute(MethodInfo aMethod, ILOpCode aOpCode)
        {
            var xIsSingleCompare = true;

            switch (aOpCode.OpCode)
            {
            case ILOpCode.Code.Beq:
            case ILOpCode.Code.Bge:
            case ILOpCode.Code.Bgt:
            case ILOpCode.Code.Bge_Un:
            case ILOpCode.Code.Bgt_Un:
            case ILOpCode.Code.Ble:
            case ILOpCode.Code.Ble_Un:
            case ILOpCode.Code.Bne_Un:
            case ILOpCode.Code.Blt:
            case ILOpCode.Code.Blt_Un:
                xIsSingleCompare = false;
                break;
            }

            var xStackContent     = aOpCode.StackPopTypes[0];
            var xStackContentSize = SizeOfType(xStackContent);

            if (xStackContentSize > 8)
            {
                throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: StackSize > 8 not supported");
            }

            CPU.ConditionalTestEnum xTestOp;
            // all conditions are inverted here?
            switch (aOpCode.OpCode)
            {
            case ILOpCode.Code.Beq:
                xTestOp = CPU.ConditionalTestEnum.Zero;
                break;

            case ILOpCode.Code.Bge:
                xTestOp = CPU.ConditionalTestEnum.GreaterThanOrEqualTo;
                break;

            case ILOpCode.Code.Bgt:
                xTestOp = CPU.ConditionalTestEnum.GreaterThan;
                break;

            case ILOpCode.Code.Ble:
                xTestOp = CPU.ConditionalTestEnum.LessThanOrEqualTo;
                break;

            case ILOpCode.Code.Blt:
                xTestOp = CPU.ConditionalTestEnum.LessThan;
                break;

            case ILOpCode.Code.Bne_Un:
                xTestOp = CPU.ConditionalTestEnum.NotEqual;
                break;

            case ILOpCode.Code.Bge_Un:
                xTestOp = CPU.ConditionalTestEnum.AboveOrEqual;
                break;

            case ILOpCode.Code.Bgt_Un:
                xTestOp = CPU.ConditionalTestEnum.Above;
                break;

            case ILOpCode.Code.Ble_Un:
                xTestOp = CPU.ConditionalTestEnum.BelowOrEqual;
                break;

            case ILOpCode.Code.Blt_Un:
                xTestOp = CPU.ConditionalTestEnum.Below;
                break;

            case ILOpCode.Code.Brfalse:
                xTestOp = CPU.ConditionalTestEnum.Zero;
                break;

            case ILOpCode.Code.Brtrue:
                xTestOp = CPU.ConditionalTestEnum.NotZero;
                break;

            default:
                throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Unknown OpCode for conditional branch.");
            }
            if (!xIsSingleCompare)
            {
                if (xStackContentSize <= 4)
                {
                    //if (xStackContent.IsFloat)
                    //{
                    //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Comparison of floats (System.Single) is not yet supported!");
                    //}
                    //else
                    //{
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.EAX
                    };
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.EBX
                    };
                    new CPU.Compare {
                        DestinationReg = CPU.Registers.EBX, SourceReg = CPU.Registers.EAX
                    };
                    new CPU.ConditionalJump {
                        Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                    };
                    //}
                }
                else
                {
                    //if (xStackContent.IsFloat)
                    //{
                    //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Comparison of doubles (System.Double) is not yet supported!");
                    //}
                    //else
                    //{
                    var xNoJump = GetLabel(aMethod, aOpCode) + "__NoBranch";

                    // value 2  EBX:EAX
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.EAX
                    };
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.EBX
                    };
                    // value 1  EDX:ECX
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.ECX
                    };
                    new CPU.Pop {
                        DestinationReg = CPU.Registers.EDX
                    };
                    switch (xTestOp)
                    {
                    case ConditionalTestEnum.Zero:     // Equal
                    case ConditionalTestEnum.NotEqual: // NotZero
                        new CPU.Xor {
                            DestinationReg = CPU.Registers.EAX, SourceReg = CPU.Registers.ECX
                        };
                        new CPU.ConditionalJump {
                            Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.Xor {
                            DestinationReg = CPU.Registers.EBX, SourceReg = CPU.Registers.EDX
                        };
                        new CPU.ConditionalJump {
                            Condition = xTestOp, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.GreaterThanOrEqualTo:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.LessThan, DestinationLabel = xNoJump
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.GreaterThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Below, DestinationLabel = xNoJump
                        };
                        break;

                    case ConditionalTestEnum.GreaterThan:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.LessThan, DestinationLabel = xNoJump
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.GreaterThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.BelowOrEqual, DestinationLabel = xNoJump
                        };
                        break;

                    case ConditionalTestEnum.LessThanOrEqualTo:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.LessThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.GreaterThan, DestinationLabel = xNoJump
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.BelowOrEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.LessThan:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.LessThan, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.GreaterThan, DestinationLabel = xNoJump
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Below, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    // from here all unsigned
                    case ConditionalTestEnum.AboveOrEqual:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Below, DestinationLabel = xNoJump
                        };
                        break;

                    case ConditionalTestEnum.Above:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.BelowOrEqual, DestinationLabel = xNoJump
                        };
                        break;

                    case ConditionalTestEnum.BelowOrEqual:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Below, DestinationLabel = xNoJump
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    case ConditionalTestEnum.Below:
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.EDX, SourceReg = CPU.Registers.EBX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Above, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.Below, DestinationLabel = xNoJump
                        };
                        new CPU.Compare {
                            DestinationReg = CPU.Registers.ECX, SourceReg = CPU.Registers.EAX
                        };
                        new CPU.ConditionalJump {
                            Condition = CPU.ConditionalTestEnum.AboveOrEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                        };
                        break;

                    default:
                        throw new Exception("Unknown OpCode for conditional branch in 64-bit.");
                    }
                    new Label(xNoJump);
                    //}
                }
            }
            else
            {
                //if (xStackContent.IsFloat)
                //{
                //    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Simple comparison of floating point numbers is not yet supported!");
                //}
                //else
                //{
                // todo: improve code clarity
                if (xStackContentSize > 4)
                {
                    throw new Exception("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Simple branches are not yet supported on operands > 4 bytes!");
                }
                new CPU.Pop {
                    DestinationReg = CPU.Registers.EAX
                };
                if (xTestOp == ConditionalTestEnum.Zero)
                {
                    new CPU.Compare {
                        DestinationReg = CPU.Registers.EAX, SourceValue = 0
                    };
                    new CPU.ConditionalJump {
                        Condition = ConditionalTestEnum.Equal, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                    };
                }
                else if (xTestOp == ConditionalTestEnum.NotZero)
                {
                    new CPU.Compare {
                        DestinationReg = CPU.Registers.EAX, SourceValue = 0
                    };
                    new CPU.ConditionalJump {
                        Condition = ConditionalTestEnum.NotEqual, DestinationLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode)
                    };
                }
                else
                {
                    throw new NotSupportedException("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: Situation not supported yet! (In the Simple Comparison)");
                }
            }
            //}
        }
Example #8
0
File: Br.cs Project: tgiphil/IL2CPU
 public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
 {
     XS.Jump(AppAssembler.TmpBranchLabel(aMethod, aOpCode));
 }
Example #9
0
        public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
        {
            var xOp          = aOpCode.OpCode;
            var xBranchLabel = AppAssembler.TmpBranchLabel(aMethod, aOpCode);

            var xStackType = aOpCode.StackPopTypes[0];
            var xSize      = SizeOfType(xStackType);
            var xIsFloat   = TypeIsFloat(xStackType);

            if (xOp == ILOpCode.Code.Brtrue ||
                xOp == ILOpCode.Code.Brfalse)
            {
                if (xIsFloat)
                {
                    throw new NotSupportedException();
                }

                if (xSize <= 4)
                {
                    XS.Pop(EAX);
                    XS.Compare(EAX, 0);
                    XS.Jump(xOp == ILOpCode.Code.Brtrue ? ConditionalTestEnum.NotEqual : ConditionalTestEnum.Equal, xBranchLabel);
                }
                else if (xSize <= 8)
                {
                    XS.Pop(EAX);
                    XS.Pop(EBX);

                    if (xOp == ILOpCode.Code.Brtrue)
                    {
                        XS.Compare(EAX, 0);
                        XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);

                        XS.Compare(EBX, 0);
                        XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);
                    }
                    else
                    {
                        var xEndLabel = GetLabel(aMethod, aOpCode) + ".End";

                        XS.Compare(EAX, 0);
                        XS.Jump(ConditionalTestEnum.NotEqual, xEndLabel);

                        XS.Compare(EBX, 0);
                        XS.Jump(ConditionalTestEnum.Equal, xBranchLabel);

                        XS.Label(xEndLabel);
                    }
                }
                else if (xSize > 8)
                {
                    throw new NotImplementedException("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: StackSize > 8 not supported");
                }

                return;
            }

            if (xIsFloat)
            {
                var xTestOp      = SseCompareOPs[xOp];
                var xIsUnordered = xOp == ILOpCode.Code.Bge_Un ||
                                   xOp == ILOpCode.Code.Bgt_Un ||
                                   xOp == ILOpCode.Code.Ble_Un ||
                                   xOp == ILOpCode.Code.Blt_Un ||
                                   xOp == ILOpCode.Code.Bne_Un;

                if (xSize <= 4)
                {
                    XS.SSE.MoveSS(XMM0, ESP, sourceIsIndirect: true);
                    XS.Add(ESP, 4);
                    XS.SSE.MoveSS(XMM1, ESP, sourceIsIndirect: true);
                    XS.Add(ESP, 4);

                    if (xIsUnordered)
                    {
                        XS.SSE.MoveSS(XMM2, XMM1);
                        XS.SSE.CompareSS(XMM2, XMM0, ComparePseudoOpcodes.Unordered);
                        XS.MoveD(EAX, XMM2);
                        XS.Compare(EAX, 0);
                        XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);
                    }

                    XS.SSE.CompareSS(XMM1, XMM0, xTestOp);
                    XS.MoveD(EAX, XMM1);
                    XS.Compare(EAX, 0);
                    XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);
                }
                else if (xSize <= 8)
                {
                    XS.SSE2.MoveSD(XMM0, ESP, sourceIsIndirect: true);
                    XS.Add(ESP, 8);
                    XS.SSE2.MoveSD(XMM1, ESP, sourceIsIndirect: true);
                    XS.Add(ESP, 8);

                    if (xIsUnordered)
                    {
                        XS.SSE2.MoveSD(XMM2, XMM1);
                        XS.SSE2.CompareSD(XMM2, XMM0, ComparePseudoOpcodes.Unordered);
                        XS.MoveD(EAX, XMM2);
                        XS.Compare(EAX, 0);
                        XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);
                    }

                    XS.SSE2.CompareSD(XMM1, XMM0, xTestOp);
                    XS.MoveD(EAX, XMM1);
                    XS.Compare(EAX, 0);
                    XS.Jump(ConditionalTestEnum.NotEqual, xBranchLabel);
                }
            }
            else
            {
                var xTestOp = TestOPs[xOp];

                if (xSize <= 4)
                {
                    XS.Pop(EAX);
                    XS.Pop(EBX);
                    XS.Compare(EBX, EAX);
                    XS.Jump(xTestOp, xBranchLabel);
                }
                else if (xSize <= 8)
                {
                    var xEndLabel = GetLabel(aMethod, aOpCode) + ".End";

                    XS.Pop(EAX);
                    XS.Pop(EDX);

                    XS.Pop(EBX);
                    XS.Pop(ECX);

                    switch (xOp)
                    {
                    case ILOpCode.Code.Beq:
                    case ILOpCode.Code.Bne_Un:
                        XS.Compare(ECX, EDX);
                        XS.Jump(ConditionalTestEnum.NotEqual, xOp == ILOpCode.Code.Beq ? xEndLabel : xBranchLabel);
                        XS.Compare(EBX, EAX);
                        XS.Jump(xTestOp, xBranchLabel);

                        break;

                    case ILOpCode.Code.Bge:
                    case ILOpCode.Code.Bgt:
                        XS.Compare(ECX, EDX);
                        XS.Jump(ConditionalTestEnum.GreaterThan, xBranchLabel);
                        XS.Jump(ConditionalTestEnum.NotEqual, xEndLabel);
                        XS.Compare(EBX, EAX);
                        XS.Jump(xTestOp, xBranchLabel);

                        break;

                    case ILOpCode.Code.Ble:
                    case ILOpCode.Code.Blt:
                        XS.Compare(ECX, EDX);
                        XS.Jump(ConditionalTestEnum.LessThan, xBranchLabel);
                        XS.Jump(ConditionalTestEnum.NotEqual, xEndLabel);
                        XS.Compare(EBX, EAX);
                        XS.Jump(xTestOp, xBranchLabel);

                        break;

                    case ILOpCode.Code.Bge_Un:
                    case ILOpCode.Code.Bgt_Un:
                        XS.Compare(ECX, EDX);
                        XS.Jump(ConditionalTestEnum.Above, xBranchLabel);
                        XS.Jump(ConditionalTestEnum.NotEqual, xEndLabel);
                        XS.Compare(EBX, EAX);
                        XS.Jump(xTestOp, xBranchLabel);

                        break;

                    case ILOpCode.Code.Ble_Un:
                    case ILOpCode.Code.Blt_Un:
                        XS.Compare(ECX, EDX);
                        XS.Jump(ConditionalTestEnum.Below, xBranchLabel);
                        XS.Jump(ConditionalTestEnum.NotEqual, xEndLabel);
                        XS.Compare(EBX, EAX);
                        XS.Jump(xTestOp, xBranchLabel);

                        break;
                    }

                    XS.Label(xEndLabel);
                }
            }

            if (xSize > 8)
            {
                throw new NotImplementedException("Cosmos.IL2CPU.x86->IL->Branch.cs->Error: StackSize > 8 not supported");
            }
        }