private int EmulateInstruction(Instruction instruction, int i)
        {
            switch (instruction.OpCode.Code)
            {
            case Code.UNKNOWN1:
                throw new NotSupportedException();

            case Code.UNKNOWN2:
                throw new NotSupportedException();

            case Code.Add:
                Add.Emulate(ValueStack);
                break;

            case Code.Add_Ovf:
            case Code.Add_Ovf_Un:
                Add.Emulate_Ovf(ValueStack);
                break;

            case Code.And:
                And.Emulate(ValueStack);
                break;

            case Code.Arglist:
                throw new NotSupportedException();

            case Code.Beq:
            case Code.Beq_S:
                var beqResult = Beq.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(beqResult == -1 ? i : beqResult);

            case Code.Bge:
            case Code.Bge_S:
            case Code.Bge_Un:
            case Code.Bge_Un_S:
                var bgeResult = Bge.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bgeResult == -1 ? i : bgeResult);

            case Code.Bgt:
            case Code.Bgt_S:
            case Code.Bgt_Un:
            case Code.Bgt_Un_S:
                var bgtResult = Bgt.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bgtResult == -1 ? i : bgtResult);

            case Code.Ble:
            case Code.Ble_S:
            case Code.Ble_Un:
            case Code.Ble_Un_S:
                var bleResult = Ble.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bleResult == -1 ? i : bleResult);

            case Code.Blt:
            case Code.Blt_S:
            case Code.Blt_Un:
            case Code.Blt_Un_S:
                var bltResult = Blt.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bltResult == -1 ? i : bltResult);

            case Code.Bne_Un:
            case Code.Bne_Un_S:
                var bneResult = Bne.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bneResult == -1 ? i : bneResult);

            case Code.Box:
                Box.Emulate_Box(ValueStack, instruction);
                break;

            case Code.Br:
            case Code.Br_S:
                return(Br.Emulate(ValueStack, instruction, _instructionsToEmulate));

            case Code.Break:
                break;

            case Code.Brfalse:
            case Code.Brfalse_S:
                var bFalseResult = BrFalse.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bFalseResult == -1 ? i : bFalseResult);

            case Code.Brtrue:
            case Code.Brtrue_S:
                var bTrueResult = BrTrue.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(bTrueResult == -1 ? i : bTrueResult);

            case Code.Call:
                Call.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Calli:
                throw new NotSupportedException();

            case Code.Callvirt:
                CallVirt.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Castclass:
                CastClass.Emulate(ValueStack, instruction);
                break;

            case Code.Ceq:
                Ceq.Emulate(ValueStack);
                break;

            case Code.Cgt:
            case Code.Cgt_Un:
                Ceq.Emulate(ValueStack);
                break;

            case Code.Ckfinite:
                throw new NotSupportedException();

            case Code.Clt:
            case Code.Clt_Un:
                Clt.Emulate(ValueStack);
                break;

            case Code.Constrained:
                throw new NotSupportedException();

            case Code.Conv_I:
                ConvI.Emulation(ValueStack);
                break;

            case Code.Conv_I1:
                throw new NotSupportedException();

            case Code.Conv_I2:
                ConvI2.Emulation(ValueStack);
                break;

            case Code.Conv_I4:
                ConvI4.Emulation(ValueStack);
                break;

            case Code.Conv_I8:
                ConvI8.Emulation(ValueStack);
                break;

            case Code.Conv_Ovf_I:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I1:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I1_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I2:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I2_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I4:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I4_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I8:
                throw new NotSupportedException();

            case Code.Conv_Ovf_I8_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U1:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U1_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U2:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U2_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U4:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U4_Un:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U8:
                throw new NotSupportedException();

            case Code.Conv_Ovf_U8_Un:
                throw new NotSupportedException();

            case Code.Conv_R_Un:
                throw new NotSupportedException();

            case Code.Conv_R4:
                throw new NotSupportedException();

            case Code.Conv_R8:
                throw new NotSupportedException();

            case Code.Conv_U:
                ConvI.UEmulation(ValueStack);
                break;

            case Code.Conv_U1:
                ConvI1.UEmulation(ValueStack);
                break;

            case Code.Conv_U2:
                ConvI2.UEmulation(ValueStack);
                break;

            case Code.Conv_U4:
                ConvI4.UEmulation(ValueStack);
                break;

            case Code.Conv_U8:
                ConvI8.UEmulation(ValueStack);
                break;

            case Code.Cpblk:
                throw new NotSupportedException();

            case Code.Cpobj:
                throw new NotSupportedException();

            case Code.Div:
                Div.Emulate(ValueStack);
                break;

            case Code.Div_Un:
                Div.Emulate_Un(ValueStack);
                break;

            case Code.Dup:
                ValueStack.CallStack.Push(ValueStack.CallStack.Peek());
                break;

            case Code.Endfilter:
                throw new NotSupportedException();

            case Code.Endfinally:
                throw new NotSupportedException();

            case Code.Initblk:
                throw new NotSupportedException();

            case Code.Initobj:
                ValueStack.CallStack.Pop();
                break;

            case Code.Isinst:
                IsInst.Emulate_Box(ValueStack, instruction);
                break;

            case Code.Jmp:
                throw new NotSupportedException();

            case Code.Ldarg:
            case Code.Ldarg_0:
            case Code.Ldarg_1:
            case Code.Ldarg_2:
            case Code.Ldarg_3:
            case Code.Ldarg_S:
                Ldarg.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Ldarga:
                throw new NotSupportedException();

            case Code.Ldarga_S:
                throw new NotSupportedException();

            case Code.Ldc_I4:
            case Code.Ldc_I4_0:
            case Code.Ldc_I4_1:
            case Code.Ldc_I4_2:
            case Code.Ldc_I4_3:
            case Code.Ldc_I4_4:
            case Code.Ldc_I4_5:
            case Code.Ldc_I4_6:
            case Code.Ldc_I4_7:
            case Code.Ldc_I4_8:
                ValueStack.CallStack.Push(instruction.GetLdcI4Value());
                break;

            case Code.Ldc_I4_M1:
                ValueStack.CallStack.Push(-1);
                break;

            case Code.Ldc_I4_S:
                ValueStack.CallStack.Push((sbyte)instruction.GetLdcI4Value());
                break;

            case Code.Ldc_I8:
                ValueStack.CallStack.Push((long)instruction.Operand);
                break;

            case Code.Ldc_R4:
                ValueStack.CallStack.Push((float)instruction.GetOperand());
                break;

            case Code.Ldc_R8:
                ValueStack.CallStack.Push((double)instruction.GetOperand());
                break;

            case Code.Ldelem:
                throw new NotSupportedException();

            case Code.Ldelem_I:
                throw new NotSupportedException();

            case Code.Ldelem_I1:
                throw new NotSupportedException();

            case Code.Ldelem_I2:
                throw new NotSupportedException();

            case Code.Ldelem_I4:
                throw new NotSupportedException();

            case Code.Ldelem_I8:
                throw new NotSupportedException();

            case Code.Ldelem_R4:
                throw new NotSupportedException();

            case Code.Ldelem_R8:
                throw new NotSupportedException();

            case Code.Ldelem_Ref:
                LdelemRef.Emulate(ValueStack);
                break;

            case Code.Ldelem_U1:
                LdelemI1.UEmulate(ValueStack);
                break;

            case Code.Ldelem_U2:
                throw new NotSupportedException();

            case Code.Ldelem_U4:
                LdelemI4.UEmulate(ValueStack);
                break;

            case Code.Ldelema:
                Ldelema.Emulate(ValueStack);
                break;

            case Code.Ldfld:
                throw new NotSupportedException();

            case Code.Ldflda:
                throw new NotSupportedException();

            case Code.Ldftn:
                throw new NotSupportedException();

            case Code.Ldind_I:
                throw new NotSupportedException();

            case Code.Ldind_I1:
                throw new NotSupportedException();

            case Code.Ldind_I2:
                throw new NotSupportedException();

            case Code.Ldind_I4:
                LdindI4.Emulate(ValueStack);
                break;

            case Code.Ldind_I8:
                throw new NotSupportedException();

            case Code.Ldind_R4:
                throw new NotSupportedException();

            case Code.Ldind_R8:
                throw new NotSupportedException();

            case Code.Ldind_Ref:
                throw new NotSupportedException();

            case Code.Ldind_U1:
                LdindI1.UEmulate(ValueStack);
                break;

            case Code.Ldind_U2:
                LdindI2.UEmulate(ValueStack);
                break;

            case Code.Ldind_U4:
                LdindI4.UEmulate(ValueStack);
                break;

            case Code.Ldlen:
                Ldlen.Emulate(ValueStack);
                break;

            case Code.Ldloc:
            case Code.Ldloc_0:
            case Code.Ldloc_1:
            case Code.Ldloc_2:
            case Code.Ldloc_3:
            case Code.Ldloc_S:

                Ldloc.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Ldloca:
            case Code.Ldloca_S:
                Ldloc.EmulateLdloca(ValueStack, instruction, _method);
                break;

            case Code.Ldnull:
                ValueStack.CallStack.Push(null);
                break;

            case Code.Ldobj:
                Ldobj.Emulate(ValueStack, instruction);
                break;

            case Code.Ldsfld:
                Ldsfld.Emulate(ValueStack, instruction);
                break;

            case Code.Ldsflda:
                throw new NotSupportedException();

            case Code.Ldstr:
                ValueStack.CallStack.Push(instruction.Operand.ToString());
                break;

            case Code.Ldtoken:
                Ldtoken.Emulate(ValueStack, instruction);
                break;

            case Code.Ldvirtftn:
                throw new NotSupportedException();

            case Code.Leave:

            case Code.Leave_S:
                return(LEave.Emulate(ValueStack, instruction, _instructionsToEmulate));

            case Code.Localloc:
                Localloc.Emulate(ValueStack);
                break;

            case Code.Mkrefany:
                throw new NotSupportedException();

            case Code.Mul:
            case Code.Mul_Ovf:
            case Code.Mul_Ovf_Un:
                Mul.Emulate(ValueStack);
                break;

            case Code.Neg:
                ValueStack.CallStack.Push(-ValueStack.CallStack.Pop());
                break;

            case Code.Newarr:
                NewArr.Emulate(ValueStack, instruction);
                break;

            case Code.Newobj:
                throw new NotSupportedException();

            case Code.Nop:
                break;

            case Code.Not:
                Not.Emulate(ValueStack);
                break;

            case Code.Or:
                Or.Emulate(ValueStack);
                break;

            case Code.Pop:
                ValueStack.CallStack.Pop();
                break;

            case Code.Prefix1:
                throw new NotSupportedException();

            case Code.Prefix2:
                throw new NotSupportedException();

            case Code.Prefix3:
                throw new NotSupportedException();

            case Code.Prefix4:
                throw new NotSupportedException();

            case Code.Prefix5:
                throw new NotSupportedException();

            case Code.Prefix6:
                throw new NotSupportedException();

            case Code.Prefix7:
                throw new NotSupportedException();

            case Code.Prefixref:
                throw new NotSupportedException();

            case Code.Readonly:
                throw new NotSupportedException();

            case Code.Refanytype:
                throw new NotSupportedException();

            case Code.Refanyval:
                throw new NotSupportedException();

            case Code.Rem:
                Rem.Emulate(ValueStack);
                break;

            case Code.Rem_Un:
                Rem.Emulate_Un(ValueStack);
                break;

            case Code.Ret:
                break;

            case Code.Rethrow:
                throw new NotSupportedException();

            case Code.Shl:
                Shl.Emulate(ValueStack);
                break;

            case Code.Shr:
                Shr.Emulate(ValueStack);
                break;

            case Code.Shr_Un:
                Shr.Emulate_Un(ValueStack);
                break;

            case Code.Sizeof:
                throw new NotSupportedException();

            case Code.Starg:
            case Code.Starg_S:
                Starg.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Stelem:
                throw new NotSupportedException();

            case Code.Stelem_I:

                throw new NotSupportedException();

            case Code.Stelem_I1:
                StelemI1.Emulate(ValueStack);
                break;

            case Code.Stelem_I2:
                break;

            case Code.Stelem_I4:
                Stelem_I4.Emulate(ValueStack);
                break;

            case Code.Stelem_I8:
                throw new NotSupportedException();

            case Code.Stelem_R4:
                throw new NotSupportedException();

            case Code.Stelem_R8:
                throw new NotSupportedException();

            case Code.Stelem_Ref:
                throw new NotSupportedException();

            case Code.Stfld:
                throw new NotSupportedException();

            case Code.Stind_I:
                throw new NotSupportedException();

            case Code.Stind_I1:
                StindI1.Emulate(ValueStack);
                break;

            case Code.Stind_I2:
                throw new NotSupportedException();

            case Code.Stind_I4:
                StindI4.Emulate(ValueStack);
                break;

            case Code.Stind_I8:
                throw new NotSupportedException();

            case Code.Stind_R4:
                throw new NotSupportedException();

            case Code.Stind_R8:
                throw new NotSupportedException();

            case Code.Stind_Ref:
                throw new NotSupportedException();

            case Code.Stloc:
            case Code.Stloc_0:
            case Code.Stloc_1:
            case Code.Stloc_2:
            case Code.Stloc_3:
            case Code.Stloc_S:
                Stloc.Emulate(ValueStack, instruction, _method);
                break;

            case Code.Stobj:
                Stobj.Emulate(ValueStack, instruction);
                break;

            case Code.Stsfld:
                Stsfld.Emulate(ValueStack, instruction);
                break;

            case Code.Sub:
                Sub.Emulate(ValueStack);
                break;

            case Code.Sub_Ovf:
            case Code.Sub_Ovf_Un:
                Sub.Emulate_Ovf(ValueStack);
                break;

            case Code.Switch:
                var switchResult = Switch.Emulate(ValueStack, instruction, _instructionsToEmulate);
                return(switchResult == -1 ? i : switchResult);

            case Code.Tailcall:
                throw new NotSupportedException();

            case Code.Throw:
                throw new NotSupportedException();

            case Code.Unaligned:
                throw new NotSupportedException();

            case Code.Unbox:
                throw new NotSupportedException();

            case Code.Unbox_Any:
                //     Box.Emulate_UnBox_Any(ValueStack,instruction);
                break;

            case Code.Volatile:
                throw new NotSupportedException();

            case Code.Xor:
                Xor.Emulate(ValueStack);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(i);
        }
Exemplo n.º 2
0
 public void Shl_SecondOperandGreaterThan15_ShiftPerformedModulo16()
 {
     // TODO: Get clarification on the DCPU spec that this is actually
     // TODO: how shifts should work.
     var prev = new MutableState().Set(Register.A, 0x1234).Set(Register.B, 20);
     var state = new Shl(new Reg(Register.A), new Reg(Register.B)).Apply(prev);
     Assert.AreEqual(0x2340, state.Get(Register.A));
     Assert.AreEqual(0x0001, state.Get(Register.O));
 }
Exemplo n.º 3
0
        public static IEnumerable <Tile> GetTiles()
        {
            return(new[] {
                LabelTile(),
                ConstTile <long>(),
                MemAccessTile(),
                MemAccessTile <long>(),

                Stack.Call(),
                Stack.Ret(),
                Stack.Push(),
                Stack.Pop(),

                Add.RegReg(),
                Add.RegConst <long>(),
                Add.ConstReg <long>(),

                Sub.RegReg(),
                Sub.RegConst <long>(),
                Sub.ConstReg <long>(),

                Mul.RegReg(),
                Mul.RegConst <long>(),
                Mul.ConstReg <long>(),

                Div.RegReg(),
                Div.RegConst <long>(),
                Div.ConstReg <long>(),

                Mod.RegReg(),
                Mod.RegConst <long>(),
                Mod.ConstReg <long>(),

                Shl.RegConst <long>(),
                Shr.RegConst <long>(),

                BitXor.RegReg(),
                BitXor.RegConst <long>(),
                BitXor.ConstReg <long>(),
                BitAnd.RegReg(),
                BitAnd.RegConst <long>(),
                BitAnd.ConstReg <long>(),
                BitOr.RegReg(),
                BitOr.RegConst <long>(),
                BitOr.ConstReg <long>(),

                LogAnd.RegReg(),
                LogAnd.RegConst(),
                LogAnd.ConstReg(),
                LogOr.RegReg(),
                LogOr.RegConst <long>(),
                LogOr.ConstReg <long>(),

                Unop.Inc_Reg(),
                Unop.Dec_Reg(),
                Unop.Plus_Reg(),
                Unop.Minus_Reg(),
                Unop.Neg_Reg(),
                Unop.BinNot_Reg(),
                Unop.LogNot_Reg(),

                Compare.RegReg_Eq(),
                Compare.RegReg_Neq(),
                Compare.RegReg_Lt(),
                Compare.RegReg_Le(),
                Compare.RegReg_Gt(),
                Compare.RegReg_Ge(),
                Compare.RegConst_Eq <long>(),
                Compare.RegConst_Neq <long>(),
                Compare.RegConst_Lt <long>(),
                Compare.RegConst_Le <long>(),
                Compare.RegConst_Gt <long>(),
                Compare.RegConst_Ge <long>(),
                Compare.ConstReg_Eq <long>(),
                Compare.ConstReg_Neq <long>(),
                Compare.ConstReg_Lt <long>(),
                Compare.ConstReg_Le <long>(),
                Compare.ConstReg_Gt <long>(),
                Compare.ConstReg_Ge <long>(),

                Assign.Reg_Reg(),
                Assign.Reg_Const(),
                Assign.Reg_AddConst(),
                Assign.Reg_SubConst(),
                Assign.MemReg_Reg(),
                Assign.MemReg_Const(),
                Assign.MemConst_Reg(),
                Assign.MemConst_Const(),

                Jump.Unconditional_Label(),
                Jump.Unconditional_Reg(),
                Jump.Cond_RegReg_Eq(),
                Jump.Cond_RegReg_Neq(),
                Jump.Cond_RegReg_Lt(),
                Jump.Cond_RegReg_Le(),
                Jump.Cond_RegReg_Gt(),
                Jump.Cond_RegReg_Ge(),
                Jump.Cond_RegConst_Eq <long>(),
                Jump.Cond_RegConst_Neq <long>(),
                Jump.Cond_RegConst_Lt <long>(),
                Jump.Cond_RegConst_Le <long>(),
                Jump.Cond_RegConst_Gt <long>(),
                Jump.Cond_RegConst_Ge <long>(),
                Jump.Cond_ConstReg_Eq <long>(),
                Jump.Cond_ConstReg_Neq <long>(),
                Jump.Cond_ConstReg_Lt <long>(),
                Jump.Cond_ConstReg_Le <long>(),
                Jump.Cond_ConstReg_Gt <long>(),
                Jump.Cond_ConstReg_Ge <long>(),

                Advanced.EffectiveMultiplication_RegConst(),
                Advanced.EffectiveMultiplication_ConstReg(),
                Advanced.EffectiveAddition_RegReg(),
                Advanced.EffectiveAddition_RegConst <long>(),
                Advanced.EffectiveAddition_ConstReg <long>(),
                Advanced.EffectiveAddition_ConstRegReg1 <long>(),
                Advanced.EffectiveAddition_ConstRegReg2 <long>(),
                Advanced.EffectiveAddition_RegConstReg1 <long>(),
                Advanced.EffectiveAddition_RegConstReg2 <long>(),
                Advanced.EffectiveAddition_RegRegConst1 <long>(),
                Advanced.EffectiveAddition_RegRegConst2 <long>()
            });
        }
Exemplo n.º 4
0
 public void Shl_RegisterAndLiteral_ResultAndOverflowAreCorrect()
 {
     var prev = new MutableState().Set(Register.A, 0x5201);
     var state = new Shl(new Reg(Register.A), new Literal(4)).Apply(prev);
     Assert.AreEqual(0x2010, state.Get(Register.A));
     Assert.AreEqual(0x5, state.Get(Register.O));
 }