Esempio n. 1
0
 private void RunCodeWithTry(CodeBody body, StackFrame stack)
 {
     try
     {
         RunCode(stack, body);
     }
     catch (Exception err)
     {
         bool bEH = false;
         if (body.bodyNative.HasExceptionHandlers)
         {
             bEH = JumpToErr(body, stack, err);
         }
         if (!bEH)
         {
             throw err;
         }
     }
 }
Esempio n. 2
0
 private void RunCodeWithTry(CodeBody body, StackFrame stack)
 {
     try
     {
         this.RunCode(stack, body);
     }
     catch (Exception ex)
     {
         bool flag = false;
         bool hasExceptionHandlers = body.bodyNative.HasExceptionHandlers;
         if (hasExceptionHandlers)
         {
             flag = this.JumpToErr(body, stack, ex);
         }
         bool flag2 = !flag;
         if (flag2)
         {
             throw ex;
         }
     }
 }
Esempio n. 3
0
            public void InitToken(ThreadContext context, CodeBody body, object _p)
            {
                switch (code)
                {
                case CodeEx.Leave:
                case CodeEx.Leave_S:
                case CodeEx.Br:
                case CodeEx.Br_S:
                case CodeEx.Brtrue:
                case CodeEx.Brtrue_S:
                case CodeEx.Brfalse:
                case CodeEx.Brfalse_S:
                //比较流程控制
                case CodeEx.Beq:
                case CodeEx.Beq_S:
                case CodeEx.Bne_Un:
                case CodeEx.Bne_Un_S:
                case CodeEx.Bge:
                case CodeEx.Bge_S:
                case CodeEx.Bge_Un:
                case CodeEx.Bge_Un_S:
                case CodeEx.Bgt:
                case CodeEx.Bgt_S:
                case CodeEx.Bgt_Un:
                case CodeEx.Bgt_Un_S:
                case CodeEx.Ble:
                case CodeEx.Ble_S:
                case CodeEx.Ble_Un:
                case CodeEx.Ble_Un_S:
                case CodeEx.Blt:
                case CodeEx.Blt_S:
                case CodeEx.Blt_Un:
                case CodeEx.Blt_Un_S:
                    //this.tokenAddr = ((Mono.Cecil.Cil.Instruction)_p).Offset;
                    this.tokenAddr_Index = body.addr[((Mono.Cecil.Cil.Instruction)_p).Offset];
                    break;

                case CodeEx.Isinst:
                case CodeEx.Constrained:
                case CodeEx.Box:
                case CodeEx.Initobj:
                case CodeEx.Castclass:
                    this.tokenType = context.GetType(_p);
                    //this.tokenUnknown = _p;
                    break;

                case CodeEx.Ldfld:
                case CodeEx.Ldflda:
                case CodeEx.Ldsfld:
                case CodeEx.Ldsflda:
                case CodeEx.Stfld:
                case CodeEx.Stsfld:
                    this.tokenField = context.GetField(_p);
                    //this.tokenUnknown = _p;
                    break;

                case CodeEx.Call:
                case CodeEx.Callvirt:
                case CodeEx.Newobj:
                case CodeEx.Ldftn:
                case CodeEx.Ldvirtftn:

                    this.tokenMethod = context.GetMethod(_p);

                    break;

                case CodeEx.Ldc_I4:
                    this.tokenI32 = (int)_p;
                    break;

                case CodeEx.Ldc_I4_S:
                    this.tokenI32 = (int)Convert.ToDecimal(_p);
                    break;

                case CodeEx.Ldc_I4_M1:
                    this.tokenI32 = -1;
                    break;

                case CodeEx.Ldc_I4_0:
                    this.tokenI32 = 0;
                    break;

                case CodeEx.Ldc_I4_1:
                    this.tokenI32 = 1;
                    break;

                case CodeEx.Ldc_I4_2:
                    this.tokenI32 = 2;
                    break;

                case CodeEx.Ldc_I4_3:
                    this.tokenI32 = 3;
                    break;

                case CodeEx.Ldc_I4_4:
                    this.tokenI32 = 4;
                    break;

                case CodeEx.Ldc_I4_5:
                    this.tokenI32 = 5;
                    break;

                case CodeEx.Ldc_I4_6:
                    this.tokenI32 = 6;
                    break;

                case CodeEx.Ldc_I4_7:
                    this.tokenI32 = 7;
                    break;

                case CodeEx.Ldc_I4_8:
                    this.tokenI32 = 8;
                    break;

                case CodeEx.Ldc_I8:
                    this.tokenI64 = (Int64)_p;
                    break;

                case CodeEx.Ldc_R4:
                    this.tokenR32 = (float)_p;
                    break;

                case CodeEx.Ldc_R8:
                    this.tokenR64 = (double)_p;
                    break;

                case CodeEx.Ldstr:
                    this.tokenStr = _p as string;
                    break;

                case CodeEx.Ldloca:
                case CodeEx.Ldloca_S:
                case CodeEx.Ldloc_S:
                case CodeEx.Stloc_S:
                    this.tokenI32 = ((VariableDefinition)_p).Index;
                    //this.tokenUnknown = _p;
                    break;

                case CodeEx.Ldloc:
                case CodeEx.Stloc:
                    this.tokenI32 = (int)_p;
                    break;

                case CodeEx.Ldloc_0:
                    this.tokenI32 = 0;
                    break;

                case CodeEx.Ldloc_1:
                    this.tokenI32 = 1;
                    break;

                case CodeEx.Ldloc_2:
                    this.tokenI32 = 2;
                    break;

                case CodeEx.Ldloc_3:
                    this.tokenI32 = 3;
                    break;

                case CodeEx.Ldarga:
                case CodeEx.Ldarga_S:
                case CodeEx.Starg:
                case CodeEx.Starg_S:
                    this.tokenI32 = (_p as Mono.Cecil.ParameterDefinition).Index;
                    break;

                case CodeEx.Switch:
                {
                    Mono.Cecil.Cil.Instruction[] e = _p as Mono.Cecil.Cil.Instruction[];
                    tokenAddr_Switch = new int[e.Length];
                    for (int i = 0; i < e.Length; i++)
                    {
                        tokenAddr_Switch[i] = body.addr[(e[i].Offset)];
                    }
                }
                break;

                case CodeEx.Ldarg:
                    this.tokenI32 = (int)_p;
                    break;

                case CodeEx.Ldarg_S:
                    this.tokenI32 = (_p as Mono.Cecil.ParameterReference).Index;
                    break;

                case CodeEx.Volatile:
                    break;

                default:
                    this.tokenUnknown = _p;
                    break;
                }
            }
Esempio n. 4
0
        private void RunCode(StackFrame stack, CodeBody body)
        {
            CodeBody.OpCode opCode;
            while (true)
            {
                int codepos = stack._codepos;
                opCode = body.opCodes[codepos];
                bool flag = this.DebugLevel >= 9;
                if (flag)
                {
                    this.environment.logger.Log(opCode.ToString());
                }
                switch (opCode.code)
                {
                case CodeEx.Nop:
                    stack.Nop();
                    continue;

                case CodeEx.Break:
                    stack.Break(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldarg_0:
                    stack.Ldarg(0);
                    continue;

                case CodeEx.Ldarg_1:
                    stack.Ldarg(1);
                    continue;

                case CodeEx.Ldarg_2:
                    stack.Ldarg(2);
                    continue;

                case CodeEx.Ldarg_3:
                    stack.Ldarg(3);
                    continue;

                case CodeEx.Ldloc_0:
                    stack.Ldloc(0);
                    continue;

                case CodeEx.Ldloc_1:
                    stack.Ldloc(1);
                    continue;

                case CodeEx.Ldloc_2:
                    stack.Ldloc(2);
                    continue;

                case CodeEx.Ldloc_3:
                    stack.Ldloc(3);
                    continue;

                case CodeEx.Stloc_0:
                    stack.Stloc(0);
                    continue;

                case CodeEx.Stloc_1:
                    stack.Stloc(1);
                    continue;

                case CodeEx.Stloc_2:
                    stack.Stloc(2);
                    continue;

                case CodeEx.Stloc_3:
                    stack.Stloc(3);
                    continue;

                case CodeEx.Ldarg_S:
                {
                    bool isStatic = body.bodyNative.Method.IsStatic;
                    if (isStatic)
                    {
                        stack.Ldarg(opCode.tokenI32);
                    }
                    else
                    {
                        stack.Ldarg(opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Ldarga_S:
                {
                    bool isStatic2 = body.bodyNative.Method.IsStatic;
                    if (isStatic2)
                    {
                        stack.Ldarga(opCode.tokenI32);
                    }
                    else
                    {
                        stack.Ldarga(opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Starg_S:
                {
                    bool isStatic3 = body.bodyNative.Method.IsStatic;
                    if (isStatic3)
                    {
                        stack.Starg(this, opCode.tokenI32);
                    }
                    else
                    {
                        stack.Starg(this, opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Ldloc_S:
                    stack.Ldloc(opCode.tokenI32);
                    continue;

                case CodeEx.Ldloca_S:
                    stack.Ldloca(opCode.tokenI32);
                    continue;

                case CodeEx.Stloc_S:
                    stack.Stloc(opCode.tokenI32);
                    continue;

                case CodeEx.Ldnull:
                    stack.Ldnull();
                    continue;

                case CodeEx.Ldc_I4_M1:
                    stack.Ldc_I4(-1);
                    continue;

                case CodeEx.Ldc_I4_0:
                    stack.Ldc_I4(0);
                    continue;

                case CodeEx.Ldc_I4_1:
                    stack.Ldc_I4(1);
                    continue;

                case CodeEx.Ldc_I4_2:
                    stack.Ldc_I4(2);
                    continue;

                case CodeEx.Ldc_I4_3:
                    stack.Ldc_I4(3);
                    continue;

                case CodeEx.Ldc_I4_4:
                    stack.Ldc_I4(4);
                    continue;

                case CodeEx.Ldc_I4_5:
                    stack.Ldc_I4(5);
                    continue;

                case CodeEx.Ldc_I4_6:
                    stack.Ldc_I4(6);
                    continue;

                case CodeEx.Ldc_I4_7:
                    stack.Ldc_I4(7);
                    continue;

                case CodeEx.Ldc_I4_8:
                    stack.Ldc_I4(8);
                    continue;

                case CodeEx.Ldc_I4_S:
                    stack.Ldc_I4(opCode.tokenI32);
                    continue;

                case CodeEx.Ldc_I4:
                    stack.Ldc_I4(opCode.tokenI32);
                    continue;

                case CodeEx.Ldc_I8:
                    stack.Ldc_I8(opCode.tokenI64);
                    continue;

                case CodeEx.Ldc_R4:
                    stack.Ldc_R4(opCode.tokenR32);
                    continue;

                case CodeEx.Ldc_R8:
                    stack.Ldc_R8(opCode.tokenR64);
                    continue;

                case CodeEx.Dup:
                    stack.Dup();
                    continue;

                case CodeEx.Pop:
                    stack.Pop();
                    continue;

                case CodeEx.Jmp:
                    stack.Jmp(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Call:
                    stack.Call(this, opCode.tokenMethod, false);
                    continue;

                case CodeEx.Calli:
                    stack.Calli(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ret:
                    goto IL_3CF;

                case CodeEx.Br_S:
                    stack.Br(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Brfalse_S:
                    stack.Brfalse(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Brtrue_S:
                    stack.Brtrue(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Beq_S:
                    stack.Beq(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bge_S:
                    stack.Bge(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bgt_S:
                    stack.Bgt(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Ble_S:
                    stack.Ble(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Blt_S:
                    stack.Blt(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bne_Un_S:
                    stack.Bne_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bge_Un_S:
                    stack.Bge_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bgt_Un_S:
                    stack.Bgt_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Ble_Un_S:
                    stack.Ble_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Blt_Un_S:
                    stack.Blt_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Br:
                    stack.Br(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Brfalse:
                    stack.Brfalse(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Brtrue:
                    stack.Brtrue(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Beq:
                    stack.Beq(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bge:
                    stack.Bge(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bgt:
                    stack.Bgt(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Ble:
                    stack.Ble(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Blt:
                    stack.Blt(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bne_Un:
                    stack.Bne_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bge_Un:
                    stack.Bge_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Bgt_Un:
                    stack.Bgt_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Ble_Un:
                    stack.Ble_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Blt_Un:
                    stack.Blt_Un(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Switch:
                    stack.Switch(this, opCode.tokenAddr_Switch);
                    continue;

                case CodeEx.Ldind_I1:
                    stack.Ldind_I1();
                    continue;

                case CodeEx.Ldind_U1:
                    stack.Ldind_U1();
                    continue;

                case CodeEx.Ldind_I2:
                    stack.Ldind_I2();
                    continue;

                case CodeEx.Ldind_U2:
                    stack.Ldind_U2();
                    continue;

                case CodeEx.Ldind_I4:
                    stack.Ldind_I4();
                    continue;

                case CodeEx.Ldind_U4:
                    stack.Ldind_U4();
                    continue;

                case CodeEx.Ldind_I8:
                    stack.Ldind_I8();
                    continue;

                case CodeEx.Ldind_I:
                    stack.Ldind_I();
                    continue;

                case CodeEx.Ldind_R4:
                    stack.Ldind_R4();
                    continue;

                case CodeEx.Ldind_R8:
                    stack.Ldind_R8();
                    continue;

                case CodeEx.Ldind_Ref:
                    stack.Ldind_Ref();
                    continue;

                case CodeEx.Stind_Ref:
                    stack.Stind_Ref(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_I1:
                    stack.Stind_I1(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_I2:
                    stack.Stind_I2(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_I4:
                    stack.Stind_I4(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_I8:
                    stack.Stind_I8(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_R4:
                    stack.Stind_R4(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Stind_R8:
                    stack.Stind_R8(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Add:
                    stack.Add();
                    continue;

                case CodeEx.Sub:
                    stack.Sub();
                    continue;

                case CodeEx.Mul:
                    stack.Mul();
                    continue;

                case CodeEx.Div:
                    stack.Div();
                    continue;

                case CodeEx.Div_Un:
                    stack.Div_Un();
                    continue;

                case CodeEx.Rem:
                    stack.Rem();
                    continue;

                case CodeEx.Rem_Un:
                    stack.Rem_Un();
                    continue;

                case CodeEx.And:
                    stack.And();
                    continue;

                case CodeEx.Or:
                    stack.Or();
                    continue;

                case CodeEx.Xor:
                    stack.Xor();
                    continue;

                case CodeEx.Shl:
                    stack.Shl(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Shr:
                    stack.Shr(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Shr_Un:
                    stack.Shr_Un(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Neg:
                    stack.Neg();
                    continue;

                case CodeEx.Not:
                    stack.Not();
                    continue;

                case CodeEx.Conv_I1:
                    stack.Conv_I1();
                    continue;

                case CodeEx.Conv_I2:
                    stack.Conv_I2();
                    continue;

                case CodeEx.Conv_I4:
                    stack.Conv_I4();
                    continue;

                case CodeEx.Conv_I8:
                    stack.Conv_I8();
                    continue;

                case CodeEx.Conv_R4:
                    stack.Conv_R4();
                    continue;

                case CodeEx.Conv_R8:
                    stack.Conv_R8();
                    continue;

                case CodeEx.Conv_U4:
                    stack.Conv_U4();
                    continue;

                case CodeEx.Conv_U8:
                    stack.Conv_U8();
                    continue;

                case CodeEx.Callvirt:
                    stack.Call(this, opCode.tokenMethod, true);
                    continue;

                case CodeEx.Cpobj:
                    stack.Cpobj(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldobj:
                    stack.Ldobj(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldstr:
                    stack.Ldstr(opCode.tokenStr);
                    continue;

                case CodeEx.Newobj:
                    stack.NewObj(this, opCode.tokenMethod);
                    continue;

                case CodeEx.Castclass:
                    stack.Castclass(this, opCode.tokenType);
                    continue;

                case CodeEx.Isinst:
                    stack.Isinst(this, opCode.tokenType);
                    continue;

                case CodeEx.Conv_R_Un:
                    stack.Conv_R_Un();
                    continue;

                case CodeEx.Unbox:
                    stack.Unbox();
                    continue;

                case CodeEx.Throw:
                    stack.Throw(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldfld:
                    stack.Ldfld(this, opCode.tokenField);
                    continue;

                case CodeEx.Ldflda:
                    stack.Ldflda(this, opCode.tokenField);
                    continue;

                case CodeEx.Stfld:
                    stack.Stfld(this, opCode.tokenField);
                    continue;

                case CodeEx.Ldsfld:
                    stack.Ldsfld(this, opCode.tokenField);
                    continue;

                case CodeEx.Ldsflda:
                    stack.Ldsflda(this, opCode.tokenField);
                    continue;

                case CodeEx.Stsfld:
                    stack.Stsfld(this, opCode.tokenField);
                    continue;

                case CodeEx.Stobj:
                    stack.Stobj(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Conv_Ovf_I1_Un:
                    stack.Conv_Ovf_I1_Un();
                    continue;

                case CodeEx.Conv_Ovf_I2_Un:
                    stack.Conv_Ovf_I2_Un();
                    continue;

                case CodeEx.Conv_Ovf_I4_Un:
                    stack.Conv_Ovf_I4_Un();
                    continue;

                case CodeEx.Conv_Ovf_I8_Un:
                    stack.Conv_Ovf_I8_Un();
                    continue;

                case CodeEx.Conv_Ovf_U1_Un:
                    stack.Conv_Ovf_U1_Un();
                    continue;

                case CodeEx.Conv_Ovf_U2_Un:
                    stack.Conv_Ovf_U2_Un();
                    continue;

                case CodeEx.Conv_Ovf_U4_Un:
                    stack.Conv_Ovf_U4_Un();
                    continue;

                case CodeEx.Conv_Ovf_U8_Un:
                    stack.Conv_Ovf_U8_Un();
                    continue;

                case CodeEx.Conv_Ovf_I_Un:
                    stack.Conv_Ovf_I_Un();
                    continue;

                case CodeEx.Conv_Ovf_U_Un:
                    stack.Conv_Ovf_U_Un();
                    continue;

                case CodeEx.Box:
                    stack.Box(opCode.tokenType);
                    continue;

                case CodeEx.Newarr:
                    stack.NewArr(this, opCode.tokenType.TypeForSystem);
                    continue;

                case CodeEx.Ldlen:
                    stack.LdLen();
                    continue;

                case CodeEx.Ldelema:
                    stack.Ldelema(opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldelem_I1:
                    stack.Ldelem_I1();
                    continue;

                case CodeEx.Ldelem_U1:
                    stack.Ldelem_U1();
                    continue;

                case CodeEx.Ldelem_I2:
                    stack.Ldelem_I2();
                    continue;

                case CodeEx.Ldelem_U2:
                    stack.Ldelem_U2();
                    continue;

                case CodeEx.Ldelem_I4:
                    stack.Ldelem_I4();
                    continue;

                case CodeEx.Ldelem_U4:
                    stack.Ldelem_U4();
                    continue;

                case CodeEx.Ldelem_I8:
                    stack.Ldelem_I8();
                    continue;

                case CodeEx.Ldelem_I:
                    stack.Ldelem_I();
                    continue;

                case CodeEx.Ldelem_R4:
                    stack.Ldelem_R4();
                    continue;

                case CodeEx.Ldelem_R8:
                    stack.Ldelem_R8();
                    continue;

                case CodeEx.Ldelem_Ref:
                    stack.Ldelem_Ref();
                    continue;

                case CodeEx.Stelem_I:
                    stack.Stelem_I();
                    continue;

                case CodeEx.Stelem_I1:
                    stack.Stelem_I1();
                    continue;

                case CodeEx.Stelem_I2:
                    stack.Stelem_I2();
                    continue;

                case CodeEx.Stelem_I4:
                    stack.Stelem_I4();
                    continue;

                case CodeEx.Stelem_I8:
                    stack.Stelem_I8();
                    continue;

                case CodeEx.Stelem_R4:
                    stack.Stelem_R4();
                    continue;

                case CodeEx.Stelem_R8:
                    stack.Stelem_R8();
                    continue;

                case CodeEx.Stelem_Ref:
                    stack.Stelem_Ref();
                    continue;

                case CodeEx.Ldelem_Any:
                    stack.Ldelem_Any(opCode.tokenUnknown);
                    continue;

                case CodeEx.Stelem_Any:
                    stack.Stelem_Any();
                    continue;

                case CodeEx.Unbox_Any:
                    stack.Unbox_Any();
                    continue;

                case CodeEx.Conv_Ovf_I1:
                    stack.Conv_Ovf_I1();
                    continue;

                case CodeEx.Conv_Ovf_U1:
                    stack.Conv_Ovf_U1();
                    continue;

                case CodeEx.Conv_Ovf_I2:
                    stack.Conv_Ovf_I2();
                    continue;

                case CodeEx.Conv_Ovf_U2:
                    stack.Conv_Ovf_U2();
                    continue;

                case CodeEx.Conv_Ovf_I4:
                    stack.Conv_Ovf_I4();
                    continue;

                case CodeEx.Conv_Ovf_U4:
                    stack.Conv_Ovf_U4();
                    continue;

                case CodeEx.Conv_Ovf_I8:
                    stack.Conv_Ovf_I8();
                    continue;

                case CodeEx.Conv_Ovf_U8:
                    stack.Conv_Ovf_U8();
                    continue;

                case CodeEx.Refanyval:
                    stack.Refanyval(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ckfinite:
                    stack.Ckfinite();
                    continue;

                case CodeEx.Mkrefany:
                    stack.Mkrefany(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ldtoken:
                    stack.Ldtoken(this, this.GetToken(opCode.tokenUnknown));
                    continue;

                case CodeEx.Conv_U2:
                    stack.Conv_U2();
                    continue;

                case CodeEx.Conv_U1:
                    stack.Conv_U1();
                    continue;

                case CodeEx.Conv_I:
                    stack.Conv_I();
                    continue;

                case CodeEx.Conv_Ovf_I:
                    stack.Conv_Ovf_I();
                    continue;

                case CodeEx.Conv_Ovf_U:
                    stack.Conv_Ovf_U();
                    continue;

                case CodeEx.Add_Ovf:
                    stack.Add_Ovf(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Add_Ovf_Un:
                    stack.Add_Ovf_Un(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Mul_Ovf:
                    stack.Mul_Ovf(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Mul_Ovf_Un:
                    stack.Mul_Ovf_Un(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Sub_Ovf:
                    stack.Sub_Ovf(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Sub_Ovf_Un:
                    stack.Sub_Ovf_Un(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Endfinally:
                    stack.Endfinally(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Leave:
                    stack.Leave(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Leave_S:
                    stack.Leave(opCode.tokenAddr_Index);
                    continue;

                case CodeEx.Stind_I:
                    stack.Stind_I(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Conv_U:
                    stack.Conv_U();
                    continue;

                case CodeEx.Arglist:
                    stack.Arglist(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Ceq:
                    stack.Ceq();
                    continue;

                case CodeEx.Cgt:
                    stack.Cgt();
                    continue;

                case CodeEx.Cgt_Un:
                    stack.Cgt_Un();
                    continue;

                case CodeEx.Clt:
                    stack.Clt();
                    continue;

                case CodeEx.Clt_Un:
                    stack.Clt_Un();
                    continue;

                case CodeEx.Ldftn:
                    stack.Ldftn(this, opCode.tokenMethod);
                    continue;

                case CodeEx.Ldvirtftn:
                    stack.Ldvirtftn(this, opCode.tokenMethod);
                    continue;

                case CodeEx.Ldarg:
                {
                    bool isStatic4 = body.bodyNative.Method.IsStatic;
                    if (isStatic4)
                    {
                        stack.Ldarg(opCode.tokenI32);
                    }
                    else
                    {
                        stack.Ldarg(opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Ldarga:
                {
                    bool isStatic5 = body.bodyNative.Method.IsStatic;
                    if (isStatic5)
                    {
                        stack.Ldarga(opCode.tokenI32);
                    }
                    else
                    {
                        stack.Ldarga(opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Starg:
                {
                    bool isStatic6 = body.bodyNative.Method.IsStatic;
                    if (isStatic6)
                    {
                        stack.Starg(this, opCode.tokenI32);
                    }
                    else
                    {
                        stack.Starg(this, opCode.tokenI32 + 1);
                    }
                    continue;
                }

                case CodeEx.Ldloc:
                    stack.Ldloc(opCode.tokenI32);
                    continue;

                case CodeEx.Ldloca:
                    stack.Ldloca(opCode.tokenI32);
                    continue;

                case CodeEx.Stloc:
                    stack.Stloc(opCode.tokenI32);
                    continue;

                case CodeEx.Localloc:
                    stack.Localloc(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Endfilter:
                    stack.Endfilter(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Unaligned:
                    stack.Unaligned(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Volatile:
                    stack.Volatile();
                    continue;

                case CodeEx.Tail:
                    stack.Tail(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Initobj:
                    stack.Initobj(this, opCode.tokenType);
                    continue;

                case CodeEx.Constrained:
                    stack.Constrained(this, opCode.tokenType);
                    continue;

                case CodeEx.Cpblk:
                    stack.Cpblk(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Initblk:
                    stack.Initblk(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.No:
                    stack.No(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Rethrow:
                    stack.Rethrow(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Sizeof:
                    stack.Sizeof(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Refanytype:
                    stack.Refanytype(this, opCode.tokenUnknown);
                    continue;

                case CodeEx.Readonly:
                    stack.Readonly(this, opCode.tokenUnknown);
                    continue;
                }
                break;
            }
            throw new Exception("未实现的OpCode:" + opCode.code);
IL_3CF:
            stack.Ret();
        }
Esempio n. 5
0
        private bool JumpToErr(CodeBody body, StackFrame frame, Exception err)
        {
            Instruction             code             = frame.GetCode();
            List <ExceptionHandler> list             = new List <ExceptionHandler>();
            ExceptionHandler        exceptionHandler = null;
            int num = -1;

            foreach (ExceptionHandler current in body.bodyNative.ExceptionHandlers)
            {
                bool flag = current.HandlerType == ExceptionHandlerType.Catch;
                if (flag)
                {
                    Type typeForSystem = this.GetType(current.CatchType).TypeForSystem;
                    bool flag2         = typeForSystem == err.GetType() || err.GetType().IsSubclassOf(typeForSystem);
                    if (flag2)
                    {
                        bool flag3 = current.TryStart.Offset <= code.Offset && current.TryEnd.Offset >= code.Offset;
                        if (flag3)
                        {
                            bool flag4 = exceptionHandler == null;
                            if (flag4)
                            {
                                exceptionHandler = current;
                                num = this.GetBaseCount(typeForSystem, err.GetType());
                            }
                            else
                            {
                                bool flag5 = current.TryStart.Offset > exceptionHandler.TryStart.Offset || current.TryEnd.Offset < exceptionHandler.TryEnd.Offset;
                                if (flag5)
                                {
                                    exceptionHandler = current;
                                    num = this.GetBaseCount(typeForSystem, err.GetType());
                                }
                                else
                                {
                                    bool flag6 = current.TryStart.Offset == exceptionHandler.TryStart.Offset || current.TryEnd.Offset == exceptionHandler.TryEnd.Offset;
                                    if (flag6)
                                    {
                                        bool flag7 = typeForSystem == err.GetType();
                                        if (flag7)
                                        {
                                            exceptionHandler = current;
                                            num = this.GetBaseCount(typeForSystem, err.GetType());
                                        }
                                        else
                                        {
                                            bool flag8 = this.GetType(exceptionHandler.CatchType).TypeForSystem == err.GetType();
                                            if (flag8)
                                            {
                                                continue;
                                            }
                                            int  baseCount = this.GetBaseCount(typeForSystem, err.GetType());
                                            bool flag9     = baseCount == -1;
                                            if (flag9)
                                            {
                                                continue;
                                            }
                                            bool flag10 = baseCount < num;
                                            if (flag10)
                                            {
                                                exceptionHandler = current;
                                                num = baseCount;
                                            }
                                        }
                                    }
                                }
                            }
                            list.Add(current);
                        }
                    }
                }
            }
            bool flag11 = exceptionHandler != null;
            bool result;

            if (flag11)
            {
                frame.stackCalc.Push(err);
                frame.SetCodePos(exceptionHandler.HandlerStart.Offset);
                this.RunCodeWithTry(body, frame);
                result = true;
            }
            else
            {
                result = false;
            }
            return(result);
        }
Esempio n. 6
0
            public void InitToken(ThreadContext context, CodeBody body, object _p)
            {
                switch (code)
                {
                    case CodeEx.Leave:
                    case CodeEx.Leave_S:
                    case CodeEx.Br:
                    case CodeEx.Br_S:
                    case CodeEx.Brtrue:
                    case CodeEx.Brtrue_S:
                    case CodeEx.Brfalse:
                    case CodeEx.Brfalse_S:
                    //比较流程控制
                    case CodeEx.Beq:
                    case CodeEx.Beq_S:
                    case CodeEx.Bne_Un:
                    case CodeEx.Bne_Un_S:
                    case CodeEx.Bge:
                    case CodeEx.Bge_S:
                    case CodeEx.Bge_Un:
                    case CodeEx.Bge_Un_S:
                    case CodeEx.Bgt:
                    case CodeEx.Bgt_S:
                    case CodeEx.Bgt_Un:
                    case CodeEx.Bgt_Un_S:
                    case CodeEx.Ble:
                    case CodeEx.Ble_S:
                    case CodeEx.Ble_Un:
                    case CodeEx.Ble_Un_S:
                    case CodeEx.Blt:
                    case CodeEx.Blt_S:
                    case CodeEx.Blt_Un:
                    case CodeEx.Blt_Un_S:
                        //this.tokenAddr = ((Mono.Cecil.Cil.Instruction)_p).Offset;
                        this.tokenAddr_Index = body.addr[((Mono.Cecil.Cil.Instruction)_p).Offset];
                        break;
                    case CodeEx.Isinst:
                    case CodeEx.Constrained:
                    case CodeEx.Box:
                    case CodeEx.Initobj:
                    case CodeEx.Castclass:
                    case CodeEx.Newarr:
                        this.tokenType = context.GetType(_p);
                        //this.tokenUnknown = _p;
                        break;
                    case CodeEx.Ldfld:
                    case CodeEx.Ldflda:
                    case CodeEx.Ldsfld:
                    case CodeEx.Ldsflda:
                    case CodeEx.Stfld:
                    case CodeEx.Stsfld:
                        this.tokenField = context.GetField(_p);
                        //this.tokenUnknown = _p;
                        break;
                    case CodeEx.Call:
                    case CodeEx.Callvirt:
                    case CodeEx.Newobj:
                    case CodeEx.Ldftn:
                    case CodeEx.Ldvirtftn:

                            this.tokenMethod = context.GetMethod(_p);
 
                        break;
                    case CodeEx.Ldc_I4:
                        this.tokenI32 = (int)_p;
                        break;
                    case CodeEx.Ldc_I4_S:
                        this.tokenI32 = (int)Convert.ToDecimal(_p);
                        break;
                    case CodeEx.Ldc_I4_M1:
                        this.tokenI32 = -1;
                        break;
                    case CodeEx.Ldc_I4_0:
                        this.tokenI32 = 0;
                        break;
                    case CodeEx.Ldc_I4_1:
                        this.tokenI32 = 1;
                        break;
                    case CodeEx.Ldc_I4_2:
                        this.tokenI32 = 2;
                        break;
                    case CodeEx.Ldc_I4_3:
                        this.tokenI32 = 3;
                        break;
                    case CodeEx.Ldc_I4_4:
                        this.tokenI32 = 4;
                        break;
                    case CodeEx.Ldc_I4_5:
                        this.tokenI32 = 5;
                        break;
                    case CodeEx.Ldc_I4_6:
                        this.tokenI32 = 6;
                        break;
                    case CodeEx.Ldc_I4_7:
                        this.tokenI32 = 7;
                        break;
                    case CodeEx.Ldc_I4_8:
                        this.tokenI32 = 8;
                        break;
                    case CodeEx.Ldc_I8:
                        this.tokenI64 = (Int64)_p;
                        break;
                    case CodeEx.Ldc_R4:
                        this.tokenR32 = (float)_p;
                        break;
                    case CodeEx.Ldc_R8:
                        this.tokenR64 = (double)_p;
                        break;

                    case CodeEx.Ldstr:
                        this.tokenStr = _p as string;
                        break;

                    case CodeEx.Ldloca:
                    case CodeEx.Ldloca_S:
                    case CodeEx.Ldloc_S:
                    case CodeEx.Stloc_S:
                        this.tokenI32 = ((VariableDefinition)_p).Index;
                        //this.tokenUnknown = _p;
                        break;
                    case CodeEx.Ldloc:
                    case CodeEx.Stloc:
                        this.tokenI32 = (int)_p;
                        break;
                    case CodeEx.Ldloc_0:
                        this.tokenI32 = 0;
                        break;
                    case CodeEx.Ldloc_1:
                        this.tokenI32 = 1;
                        break;
                    case CodeEx.Ldloc_2:
                        this.tokenI32 = 2;
                        break;
                    case CodeEx.Ldloc_3:
                        this.tokenI32 = 3;
                        break;

                    case CodeEx.Ldarga:
                    case CodeEx.Ldarga_S:
                    case CodeEx.Starg:
                    case CodeEx.Starg_S:
                        this.tokenI32 = (_p as Mono.Cecil.ParameterDefinition).Index;
                        break;
                    case CodeEx.Switch:
                        {
                            Mono.Cecil.Cil.Instruction[] e = _p as Mono.Cecil.Cil.Instruction[];
                            tokenAddr_Switch = new int[e.Length];
                            for (int i = 0; i < e.Length; i++)
                            {
                                tokenAddr_Switch[i] = body.addr[(e[i].Offset)];
                            }

                        }
                        break;
                    case CodeEx.Ldarg:
                        this.tokenI32 = (int)_p;
                        break;
                    case CodeEx.Ldarg_S:
                        this.tokenI32 = (_p as Mono.Cecil.ParameterReference).Index;
                        break;
                    case CodeEx.Volatile:
                    case CodeEx.  Ldind_I1:
                    case CodeEx.  Ldind_U1:
                    case CodeEx.   Ldind_I2:
                    case CodeEx.  Ldind_U2:
                    case CodeEx.  Ldind_I4:
                    case CodeEx.  Ldind_U4:
                    case CodeEx.  Ldind_I8:
                    case CodeEx.   Ldind_I:
                    case CodeEx.  Ldind_R4:
                    case CodeEx.  Ldind_R8:
                    case CodeEx.  Ldind_Ref:
                        break;
                    default:
                        this.tokenUnknown = _p;
                        break;
                }
            }
Esempio n. 7
0
File: Context.cs Progetto: xqy/game
        void RunCode(StackFrame stack, CodeBody body)
        {
            while (true)
            {
                //var code = stack._pos;
                int _pos = stack._codepos;
                var _code = body.opCodes[_pos];
                if (DebugLevel >= 9)
                {
                    environment.logger.Log(_code.ToString());
                }
                switch (_code.code)
                {
                    ///////////
                    //流程控制
                    case CodeEx.Nop:
                        stack.Nop();
                        break;
                    case CodeEx.Ret:
                        stack.Ret();
                        return;
                    case CodeEx.Leave:
                        stack.Leave(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Leave_S:
                        stack.Leave(_code.tokenAddr_Index);
                        break;
                    //流程控制之goto
                    case CodeEx.Br:
                        stack.Br(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Br_S:
                        stack.Br(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Brtrue:
                        stack.Brtrue(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Brtrue_S:
                        stack.Brtrue(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Brfalse:
                        stack.Brfalse(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Brfalse_S:
                        stack.Brfalse(_code.tokenAddr_Index);
                        break;

                    //比较流程控制
                    case CodeEx.Beq:
                        stack.Beq(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Beq_S:
                        stack.Beq(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bne_Un:
                        stack.Bne_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bne_Un_S:
                        stack.Bne_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bge:
                        stack.Bge(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bge_S:
                        stack.Bge(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bge_Un:
                        stack.Bge_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bge_Un_S:
                        stack.Bge_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bgt:
                        stack.Bgt(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bgt_S:
                        stack.Bgt(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bgt_Un:
                        stack.Bgt_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Bgt_Un_S:
                        stack.Bge_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Ble:
                        stack.Ble(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Ble_S:
                        stack.Ble(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Ble_Un:
                        stack.Ble_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Ble_Un_S:
                        stack.Ble_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Blt:
                        stack.Blt(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Blt_S:
                        stack.Blt(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Blt_Un:
                        stack.Blt_Un(_code.tokenAddr_Index);
                        break;
                    case CodeEx.Blt_Un_S:
                        stack.Ble_Un(_code.tokenAddr_Index);
                        break;
                    //逻辑计算
                    case CodeEx.Ceq:
                        stack.Ceq();
                        break;
                    case CodeEx.Cgt:
                        stack.Cgt();
                        break;
                    case CodeEx.Cgt_Un:
                        stack.Cgt_Un();
                        break;
                    case CodeEx.Clt:
                        stack.Clt();
                        break;
                    case CodeEx.Clt_Un:
                        stack.Clt_Un();
                        break;
                    case CodeEx.Ckfinite:
                        stack.Ckfinite();
                        break;
                    //常量加载
                    case CodeEx.Ldc_I4:
                        stack.Ldc_I4(_code.tokenI32);
                        break;
                    case CodeEx.Ldc_I4_S:
                        stack.Ldc_I4(_code.tokenI32);
                        break;
                    case CodeEx.Ldc_I4_M1:
                        stack.Ldc_I4(-1);
                        break;
                    case CodeEx.Ldc_I4_0:
                        stack.Ldc_I4(0);
                        break;
                    case CodeEx.Ldc_I4_1:
                        stack.Ldc_I4(1);
                        break;
                    case CodeEx.Ldc_I4_2:
                        stack.Ldc_I4(2);
                        break;
                    case CodeEx.Ldc_I4_3:
                        stack.Ldc_I4(3);
                        break;
                    case CodeEx.Ldc_I4_4:
                        stack.Ldc_I4(4);
                        break;
                    case CodeEx.Ldc_I4_5:
                        stack.Ldc_I4(5);
                        break;
                    case CodeEx.Ldc_I4_6:
                        stack.Ldc_I4(6);
                        break;
                    case CodeEx.Ldc_I4_7:
                        stack.Ldc_I4(7);
                        break;
                    case CodeEx.Ldc_I4_8:
                        stack.Ldc_I4(8);
                        break;
                    case CodeEx.Ldc_I8:
                        stack.Ldc_I8(_code.tokenI64);
                        break;
                    case CodeEx.Ldc_R4:
                        stack.Ldc_R4(_code.tokenR32);
                        break;
                    case CodeEx.Ldc_R8:
                        stack.Ldc_R8(_code.tokenR64);
                        break;

                    //定义为临时变量
                    case CodeEx.Stloc:
                        stack.Stloc(_code.tokenI32);
                        break;
                    case CodeEx.Stloc_S:
                        stack.Stloc(_code.tokenI32);
                        break;
                    case CodeEx.Stloc_0:
                        stack.Stloc(0);
                        break;
                    case CodeEx.Stloc_1:
                        stack.Stloc(1);
                        break;
                    case CodeEx.Stloc_2:
                        stack.Stloc(2);
                        break;
                    case CodeEx.Stloc_3:
                        stack.Stloc(3);
                        break;
                    //从临时变量加载
                    case CodeEx.Ldloc:
                        stack.Ldloc(_code.tokenI32);
                        break;
                    case CodeEx.Ldloc_S:
                        stack.Ldloc(_code.tokenI32);
                        break;
                    case CodeEx.Ldloc_0:
                        stack.Ldloc(0);
                        break;
                    case CodeEx.Ldloc_1:
                        stack.Ldloc(1);
                        break;
                    case CodeEx.Ldloc_2:
                        stack.Ldloc(2);
                        break;
                    case CodeEx.Ldloc_3:
                        stack.Ldloc(3);
                        break;
                    case CodeEx.Ldloca:
                        stack.Ldloca(_code.tokenI32);
                        break;
                    case CodeEx.Ldloca_S:
                        stack.Ldloca(_code.tokenI32);
                        break;
                    //加载字符串
                    case CodeEx.Ldstr:
                        stack.Ldstr(_code.tokenStr);
                        break;
                    //呼叫函数
                    case CodeEx.Call:
                        stack.Call(this, _code.tokenMethod, false);
                        break;
                    case CodeEx.Callvirt:
                        stack.Call(this, _code.tokenMethod, true);
                        break;
                    //算术指令
                    case CodeEx.Add:
                        stack.Add();
                        break;
                    case CodeEx.Sub:
                        stack.Sub();
                        break;
                    case CodeEx.Mul:
                        stack.Mul();
                        break;
                    case CodeEx.Div:
                        stack.Div();
                        break;
                    case CodeEx.Div_Un:
                        stack.Div_Un();
                        break;
                    case CodeEx.Rem:
                        stack.Rem();
                        break;
                    case CodeEx.Rem_Un:
                        stack.Rem_Un();
                        break;
                    case CodeEx.Neg:
                        stack.Neg();
                        break;

                    //装箱
                    case CodeEx.Box:
                        stack.Box(_code.tokenType);
                        break;
                    case CodeEx.Unbox:
                        stack.Unbox();
                        break;
                    case CodeEx.Unbox_Any:
                        stack.Unbox_Any();
                        break;

                    //加载参数
                    case CodeEx.Ldarg:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Ldarg(_code.tokenI32);
                        else
                            stack.Ldarg(_code.tokenI32 + 1);

                        break;
                    case CodeEx.Ldarg_S:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Ldarg(_code.tokenI32);
                        else
                            stack.Ldarg(_code.tokenI32 + 1);
                        break;
                    case CodeEx.Ldarg_0:
                        stack.Ldarg(0);
                        break;
                    case CodeEx.Ldarg_1:
                        stack.Ldarg(1);
                        break;
                    case CodeEx.Ldarg_2:
                        stack.Ldarg(2);
                        break;
                    case CodeEx.Ldarg_3:
                        stack.Ldarg(3);
                        break;
                    case CodeEx.Ldarga:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Ldarga(_code.tokenI32);
                        else
                            stack.Ldarga(_code.tokenI32 + 1);

                        break;
                    case CodeEx.Ldarga_S:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Ldarga(_code.tokenI32);
                        else
                            stack.Ldarga(_code.tokenI32 + 1);

                        break;
                    //转换
                    case CodeEx.Conv_I1:
                        stack.Conv_I1();
                        break;
                    case CodeEx.Conv_U1:
                        stack.Conv_U1();
                        break;
                    case CodeEx.Conv_I2:
                        stack.Conv_I2();
                        break;
                    case CodeEx.Conv_U2:
                        stack.Conv_U2();
                        break;
                    case CodeEx.Conv_I4:
                        stack.Conv_I4();
                        break;
                    case CodeEx.Conv_U4:
                        stack.Conv_U4();
                        break;
                    case CodeEx.Conv_I8:
                        stack.Conv_I8();
                        break;
                    case CodeEx.Conv_U8:
                        stack.Conv_U8();
                        break;
                    case CodeEx.Conv_I:
                        stack.Conv_I();
                        break;
                    case CodeEx.Conv_U:
                        stack.Conv_U();
                        break;
                    case CodeEx.Conv_R4:
                        stack.Conv_R4();
                        break;
                    case CodeEx.Conv_R8:
                        stack.Conv_R8();
                        break;
                    case CodeEx.Conv_R_Un:
                        stack.Conv_R_Un();
                        break;
                    case CodeEx.Conv_Ovf_I1:
                        stack.Conv_Ovf_I1();
                        break;
                    case CodeEx.Conv_Ovf_U1:
                        stack.Conv_Ovf_U1();
                        break;
                    case CodeEx.Conv_Ovf_I2:
                        stack.Conv_Ovf_I2();
                        break;
                    case CodeEx.Conv_Ovf_U2:
                        stack.Conv_Ovf_U2();
                        break;
                    case CodeEx.Conv_Ovf_I4:
                        stack.Conv_Ovf_I4();
                        break;
                    case CodeEx.Conv_Ovf_U4:
                        stack.Conv_Ovf_U4();
                        break;

                    case CodeEx.Conv_Ovf_I8:
                        stack.Conv_Ovf_I8();
                        break;
                    case CodeEx.Conv_Ovf_U8:
                        stack.Conv_Ovf_U8();
                        break;
                    case CodeEx.Conv_Ovf_I:
                        stack.Conv_Ovf_I();
                        break;
                    case CodeEx.Conv_Ovf_U:
                        stack.Conv_Ovf_U();
                        break;
                    case CodeEx.Conv_Ovf_I1_Un:
                        stack.Conv_Ovf_I1_Un();
                        break;

                    case CodeEx.Conv_Ovf_U1_Un:
                        stack.Conv_Ovf_U1_Un();
                        break;
                    case CodeEx.Conv_Ovf_I2_Un:
                        stack.Conv_Ovf_I2_Un();
                        break;
                    case CodeEx.Conv_Ovf_U2_Un:
                        stack.Conv_Ovf_U2_Un();
                        break;
                    case CodeEx.Conv_Ovf_I4_Un:
                        stack.Conv_Ovf_I4_Un();
                        break;
                    case CodeEx.Conv_Ovf_U4_Un:
                        stack.Conv_Ovf_U4_Un();
                        break;

                    case CodeEx.Conv_Ovf_I8_Un:
                        stack.Conv_Ovf_I8_Un();
                        break;
                    case CodeEx.Conv_Ovf_U8_Un:
                        stack.Conv_Ovf_U8_Un();
                        break;
                    case CodeEx.Conv_Ovf_I_Un:
                        stack.Conv_Ovf_I_Un();
                        break;
                    case CodeEx.Conv_Ovf_U_Un:
                        stack.Conv_Ovf_U_Un();
                        break;
                    //数组
                    case CodeEx.Newarr:
                        stack.NewArr(this, GetNewForArray(_code.tokenUnknown));
                        break;
                    case CodeEx.Ldlen:
                        stack.LdLen();
                        break;
                    case CodeEx.Ldelema:
                        stack.Ldelema(_code.tokenUnknown);
                        break;
                    case CodeEx.Ldelem_I1:
                        stack.Ldelem_I1();
                        break;
                    case CodeEx.Ldelem_U1:
                        stack.Ldelem_U1();
                        break;
                    case CodeEx.Ldelem_I2:
                        stack.Ldelem_I2();
                        break;
                    case CodeEx.Ldelem_U2:
                        stack.Ldelem_U2();
                        break;
                    case CodeEx.Ldelem_I4:
                        stack.Ldelem_I4();
                        break;
                    case CodeEx.Ldelem_U4:
                        stack.Ldelem_U4();
                        break;
                    case CodeEx.Ldelem_I8:
                        stack.Ldelem_I8();
                        break;
                    case CodeEx.Ldelem_I:
                        stack.Ldelem_I();
                        break;
                    case CodeEx.Ldelem_R4:
                        stack.Ldelem_R4();
                        break;
                    case CodeEx.Ldelem_R8:
                        stack.Ldelem_R8();
                        break;
                    case CodeEx.Ldelem_Ref:
                        stack.Ldelem_Ref();
                        break;
                    case CodeEx.Ldelem_Any:
                        stack.Ldelem_Any(_code.tokenUnknown);
                        break;

                    case CodeEx.Stelem_I:
                        stack.Stelem_I();
                        break;
                    case CodeEx.Stelem_I1:
                        stack.Stelem_I1();
                        break;
                    case CodeEx.Stelem_I2:
                        stack.Stelem_I2();
                        break;
                    case CodeEx.Stelem_I4:
                        stack.Stelem_I4();
                        break;
                    case CodeEx.Stelem_I8:
                        stack.Stelem_I8();
                        break;
                    case CodeEx.Stelem_R4:
                        stack.Stelem_R4();
                        break;
                    case CodeEx.Stelem_R8:
                        stack.Stelem_R8();
                        break;
                    case CodeEx.Stelem_Ref:
                        stack.Stelem_Ref();
                        break;
                    case CodeEx.Stelem_Any:
                        stack.Stelem_Any();
                        break;

                    case CodeEx.Newobj:
                        stack.NewObj(this, _code.tokenMethod);
                        break;

                    case CodeEx.Dup:
                        stack.Dup();
                        break;
                    case CodeEx.Pop:
                        stack.Pop();
                        break;

                    case CodeEx.Ldfld:
                        stack.Ldfld(this, _code.tokenField);
                        break;
                    case CodeEx.Ldflda:
                        stack.Ldflda(this, _code.tokenField);
                        break;
                    case CodeEx.Ldsfld:
                        stack.Ldsfld(this, _code.tokenField);
                        break;
                    case CodeEx.Ldsflda:
                        stack.Ldsflda(this, _code.tokenField);
                        break;
                    case CodeEx.Stfld:
                        stack.Stfld(this, _code.tokenField);
                        break;
                    case CodeEx.Stsfld:
                        stack.Stsfld(this, _code.tokenField);
                        break;

                    case CodeEx.Constrained:
                        stack.Constrained(this, _code.tokenType);
                        break;

                    case CodeEx.Isinst:
                        stack.Isinst(this, _code.tokenType);
                        break;
                    case CodeEx.Ldtoken:
                        stack.Ldtoken(this, GetToken(_code.tokenUnknown));
                        break;

                    case CodeEx.Ldftn:
                        stack.Ldftn(this, _code.tokenMethod);
                        break;
                    case CodeEx.Ldvirtftn:
                        stack.Ldvirtftn(this, _code.tokenMethod);
                        break;

                    case CodeEx.Calli:
                        stack.Calli(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Starg_S:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Starg(this, _code.tokenI32);
                        else
                            stack.Starg(this, _code.tokenI32 + 1);
                        break;
                    case CodeEx.Starg:
                        if (body.bodyNative.Method.IsStatic)
                            stack.Starg(this, _code.tokenI32);
                        else
                            stack.Starg(this, _code.tokenI32 + 1);
                        break;
                    case CodeEx.Volatile:
                        stack.Volatile();
                        break;
                    ///下面是还没有处理的指令
                    case CodeEx.Break:
                        stack.Break(this, _code.tokenUnknown);
                        break;

                    case CodeEx.Ldnull:
                        stack.Ldnull();
                        break;
                    case CodeEx.Jmp:
                        stack.Jmp(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Switch:
                        stack.Switch(this, _code.tokenAddr_Switch);
                        break;
                    case CodeEx.Ldind_I1:
                        stack.Ldind_I1(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_U1:
                        stack.Ldind_U1(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_I2:
                        stack.Ldind_I2(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_U2:
                        stack.Ldind_U2(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_I4:
                        stack.Ldind_I4(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_U4:
                        stack.Ldind_U4(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_I8:
                        stack.Ldind_I8(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_I:
                        stack.Ldind_I(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_R4:
                        stack.Ldind_R4(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_R8:
                        stack.Ldind_R8(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldind_Ref:
                        stack.Ldind_Ref(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_Ref:
                        stack.Stind_Ref(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_I1:
                        stack.Stind_I1(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_I2:
                        stack.Stind_I2(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_I4:
                        stack.Stind_I4(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_I8:
                        stack.Stind_I8(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_R4:
                        stack.Stind_R4(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_R8:
                        stack.Stind_R8(this, _code.tokenUnknown);
                        break;
                    case CodeEx.And:
                        stack.And();
                        break;
                    case CodeEx.Or:
                        stack.Or();
                        break;
                    case CodeEx.Xor:
                        stack.Xor();
                        break;
                    case CodeEx.Shl:
                        stack.Shl(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Shr:
                        stack.Shr(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Shr_Un:
                        stack.Shr_Un(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Not:
                        stack.Not();
                        break;
                    case CodeEx.Cpobj:
                        stack.Cpobj(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Ldobj:
                        stack.Ldobj(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Castclass:
                        stack.Castclass(this, _code.tokenType);
                        break;
                    case CodeEx.Throw:
                        stack.Throw(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stobj:
                        stack.Stobj(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Refanyval:
                        stack.Refanyval(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Mkrefany:
                        stack.Mkrefany(this, _code.tokenUnknown);
                        break;

                    case CodeEx.Add_Ovf:
                        stack.Add_Ovf(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Add_Ovf_Un:
                        stack.Add_Ovf_Un(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Mul_Ovf:
                        stack.Mul_Ovf(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Mul_Ovf_Un:
                        stack.Mul_Ovf_Un(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Sub_Ovf:
                        stack.Sub_Ovf(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Sub_Ovf_Un:
                        stack.Sub_Ovf_Un(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Endfinally:
                        stack.Endfinally(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Stind_I:
                        stack.Stind_I(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Arglist:
                        stack.Arglist(this, _code.tokenUnknown);
                        break;

                    case CodeEx.Localloc:
                        stack.Localloc(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Endfilter:
                        stack.Endfilter(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Unaligned:
                        stack.Unaligned(this, _code.tokenUnknown);
                        break;

                    case CodeEx.Tail:
                        stack.Tail(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Initobj:
                        stack.Initobj(this, _code.tokenType);
                        break;
                    case CodeEx.Cpblk:
                        stack.Cpblk(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Initblk:
                        stack.Initblk(this, _code.tokenUnknown);
                        break;
                    case CodeEx.No:
                        stack.No(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Rethrow:
                        stack.Rethrow(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Sizeof:
                        stack.Sizeof(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Refanytype:
                        stack.Refanytype(this, _code.tokenUnknown);
                        break;
                    case CodeEx.Readonly:
                        stack.Readonly(this, _code.tokenUnknown);
                        break;
                    default:
                        throw new Exception("未实现的OpCode:" + _code.code);
                }
            }
        }
Esempio n. 8
0
File: Context.cs Progetto: xqy/game
        bool JumpToErr(CodeBody body, StackFrame frame, Exception err)
        {
            var posnow = frame.GetCode();
            List<Mono.Cecil.Cil.ExceptionHandler> ehs = new List<ExceptionHandler>();
            Mono.Cecil.Cil.ExceptionHandler ehNear = null;
            int ehNearB = -1;
            foreach (var eh in body.bodyNative.ExceptionHandlers)
            {
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                {
                    Type ehtype = GetType(eh.CatchType).TypeForSystem;
                    if (ehtype == err.GetType() || err.GetType().IsSubclassOf(ehtype))
                    //if(GetType(eh.CatchType)== environment.GetType(err.GetType()))
                    {
                        if (eh.TryStart.Offset <= posnow.Offset && eh.TryEnd.Offset >= posnow.Offset)
                        {
                            if (ehNear == null)
                            {
                                ehNear = eh;//第一个
                                ehNearB = GetBaseCount(ehtype, err.GetType());
                            }
                            else
                            {
                                if (eh.TryStart.Offset > ehNear.TryStart.Offset || eh.TryEnd.Offset < ehNear.TryEnd.Offset)//范围更小
                                {
                                    ehNear = eh;
                                    ehNearB = GetBaseCount(ehtype, err.GetType());
                                }
                                else if (eh.TryStart.Offset == ehNear.TryStart.Offset || eh.TryEnd.Offset == ehNear.TryEnd.Offset)//范围相等
                                {
                                    if (ehtype == err.GetType())//类型一致,没有比这个更牛的了
                                    {
                                        ehNear = eh;
                                        ehNearB = GetBaseCount(ehtype, err.GetType());
                                    }
                                    else if (GetType(ehNear.CatchType).TypeForSystem == err.GetType())//上次找到的就是第一,不用比了
                                    {
                                        continue;
                                    }
                                    else //比较上次找到的类型,和这次找到的类型的亲缘性;
                                    {
                                        int newehNearB = GetBaseCount(ehtype, err.GetType());
                                        if (newehNearB == -1) continue;
                                        if (newehNearB < ehNearB)
                                        {
                                            ehNear = eh;
                                            ehNearB = newehNearB;
                                        }
                                    }
                                }
                            }
                            ehs.Add(eh);
                        }
                    }

                }
            }
            if (ehNear != null)
            {
                frame.stackCalc.Push(err);
                frame.SetCodePos(ehNear.HandlerStart.Offset);// ._pos = ehNear.HandlerStart;
                RunCodeWithTry(body, frame);
                return true;
            }
            return false;
        }
Esempio n. 9
0
File: Context.cs Progetto: xqy/game
        private void RunCodeWithTry(CodeBody body, StackFrame stack)
        {
            try
            {

                RunCode(stack, body);

            }
            catch (Exception err)
            {
                bool bEH = false;
                if (body.bodyNative.HasExceptionHandlers)
                {
                    bEH = JumpToErr(body, stack, err);
                }
                if (!bEH)
                {
                    throw err;
                }
            }
        }
Esempio n. 10
0
        void RunCode(StackFrame stack, CodeBody body)
        {
            while (true)
            {
                //var code = stack._pos;
                int _pos  = stack._codepos;
                var _code = body.opCodes[_pos];
                if (DebugLevel >= 9)
                {
                    environment.logger.Log(_code.ToString());
                }
                switch (_code.code)
                {
                ///////////
                //流程控制
                case CodeEx.Nop:
                    stack.Nop();
                    break;

                case CodeEx.Ret:
                    stack.Ret();
                    return;

                case CodeEx.Leave:
                    stack.Leave(_code.tokenAddr_Index);
                    break;

                case CodeEx.Leave_S:
                    stack.Leave(_code.tokenAddr_Index);
                    break;

                //流程控制之goto
                case CodeEx.Br:
                    stack.Br(_code.tokenAddr_Index);
                    break;

                case CodeEx.Br_S:
                    stack.Br(_code.tokenAddr_Index);
                    break;

                case CodeEx.Brtrue:
                    stack.Brtrue(_code.tokenAddr_Index);
                    break;

                case CodeEx.Brtrue_S:
                    stack.Brtrue(_code.tokenAddr_Index);
                    break;

                case CodeEx.Brfalse:
                    stack.Brfalse(_code.tokenAddr_Index);
                    break;

                case CodeEx.Brfalse_S:
                    stack.Brfalse(_code.tokenAddr_Index);
                    break;

                //比较流程控制
                case CodeEx.Beq:
                    stack.Beq(_code.tokenAddr_Index);
                    break;

                case CodeEx.Beq_S:
                    stack.Beq(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bne_Un:
                    stack.Bne_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bne_Un_S:
                    stack.Bne_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bge:
                    stack.Bge(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bge_S:
                    stack.Bge(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bge_Un:
                    stack.Bge_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bge_Un_S:
                    stack.Bge_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bgt:
                    stack.Bgt(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bgt_S:
                    stack.Bgt(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bgt_Un:
                    stack.Bgt_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Bgt_Un_S:
                    stack.Bge_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Ble:
                    stack.Ble(_code.tokenAddr_Index);
                    break;

                case CodeEx.Ble_S:
                    stack.Ble(_code.tokenAddr_Index);
                    break;

                case CodeEx.Ble_Un:
                    stack.Ble_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Ble_Un_S:
                    stack.Ble_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Blt:
                    stack.Blt(_code.tokenAddr_Index);
                    break;

                case CodeEx.Blt_S:
                    stack.Blt(_code.tokenAddr_Index);
                    break;

                case CodeEx.Blt_Un:
                    stack.Blt_Un(_code.tokenAddr_Index);
                    break;

                case CodeEx.Blt_Un_S:
                    stack.Ble_Un(_code.tokenAddr_Index);
                    break;

                //逻辑计算
                case CodeEx.Ceq:
                    stack.Ceq();
                    break;

                case CodeEx.Cgt:
                    stack.Cgt();
                    break;

                case CodeEx.Cgt_Un:
                    stack.Cgt_Un();
                    break;

                case CodeEx.Clt:
                    stack.Clt();
                    break;

                case CodeEx.Clt_Un:
                    stack.Clt_Un();
                    break;

                case CodeEx.Ckfinite:
                    stack.Ckfinite();
                    break;

                //常量加载
                case CodeEx.Ldc_I4:
                    stack.Ldc_I4(_code.tokenI32);
                    break;

                case CodeEx.Ldc_I4_S:
                    stack.Ldc_I4(_code.tokenI32);
                    break;

                case CodeEx.Ldc_I4_M1:
                    stack.Ldc_I4(-1);
                    break;

                case CodeEx.Ldc_I4_0:
                    stack.Ldc_I4(0);
                    break;

                case CodeEx.Ldc_I4_1:
                    stack.Ldc_I4(1);
                    break;

                case CodeEx.Ldc_I4_2:
                    stack.Ldc_I4(2);
                    break;

                case CodeEx.Ldc_I4_3:
                    stack.Ldc_I4(3);
                    break;

                case CodeEx.Ldc_I4_4:
                    stack.Ldc_I4(4);
                    break;

                case CodeEx.Ldc_I4_5:
                    stack.Ldc_I4(5);
                    break;

                case CodeEx.Ldc_I4_6:
                    stack.Ldc_I4(6);
                    break;

                case CodeEx.Ldc_I4_7:
                    stack.Ldc_I4(7);
                    break;

                case CodeEx.Ldc_I4_8:
                    stack.Ldc_I4(8);
                    break;

                case CodeEx.Ldc_I8:
                    stack.Ldc_I8(_code.tokenI64);
                    break;

                case CodeEx.Ldc_R4:
                    stack.Ldc_R4(_code.tokenR32);
                    break;

                case CodeEx.Ldc_R8:
                    stack.Ldc_R8(_code.tokenR64);
                    break;

                //定义为临时变量
                case CodeEx.Stloc:
                    stack.Stloc(_code.tokenI32);
                    break;

                case CodeEx.Stloc_S:
                    stack.Stloc(_code.tokenI32);
                    break;

                case CodeEx.Stloc_0:
                    stack.Stloc(0);
                    break;

                case CodeEx.Stloc_1:
                    stack.Stloc(1);
                    break;

                case CodeEx.Stloc_2:
                    stack.Stloc(2);
                    break;

                case CodeEx.Stloc_3:
                    stack.Stloc(3);
                    break;

                //从临时变量加载
                case CodeEx.Ldloc:
                    stack.Ldloc(_code.tokenI32);
                    break;

                case CodeEx.Ldloc_S:
                    stack.Ldloc(_code.tokenI32);
                    break;

                case CodeEx.Ldloc_0:
                    stack.Ldloc(0);
                    break;

                case CodeEx.Ldloc_1:
                    stack.Ldloc(1);
                    break;

                case CodeEx.Ldloc_2:
                    stack.Ldloc(2);
                    break;

                case CodeEx.Ldloc_3:
                    stack.Ldloc(3);
                    break;

                case CodeEx.Ldloca:
                    stack.Ldloca(_code.tokenI32);
                    break;

                case CodeEx.Ldloca_S:
                    stack.Ldloca(_code.tokenI32);
                    break;

                //加载字符串
                case CodeEx.Ldstr:
                    stack.Ldstr(_code.tokenStr);
                    break;

                //呼叫函数
                case CodeEx.Call:
                    stack.Call(this, _code.tokenMethod, false);
                    break;

                case CodeEx.Callvirt:
                    stack.Call(this, _code.tokenMethod, true);
                    break;

                //算术指令
                case CodeEx.Add:
                    stack.Add();
                    break;

                case CodeEx.Sub:
                    stack.Sub();
                    break;

                case CodeEx.Mul:
                    stack.Mul();
                    break;

                case CodeEx.Div:
                    stack.Div();
                    break;

                case CodeEx.Div_Un:
                    stack.Div_Un();
                    break;

                case CodeEx.Rem:
                    stack.Rem();
                    break;

                case CodeEx.Rem_Un:
                    stack.Rem_Un();
                    break;

                case CodeEx.Neg:
                    stack.Neg();
                    break;

                //装箱
                case CodeEx.Box:
                    stack.Box(_code.tokenType);
                    break;

                case CodeEx.Unbox:
                    stack.Unbox();
                    break;

                case CodeEx.Unbox_Any:
                    stack.Unbox_Any();
                    break;

                //加载参数
                case CodeEx.Ldarg:
                    if (body.bodyNative.Method.IsStatic)
                    {
                        stack.Ldarg(_code.tokenI32);
                    }
                    else
                    {
                        stack.Ldarg(_code.tokenI32 + 1);
                    }

                    break;

                case CodeEx.Ldarg_S:
                    if (body.bodyNative.Method.IsStatic)
                    {
                        stack.Ldarg(_code.tokenI32);
                    }
                    else
                    {
                        stack.Ldarg(_code.tokenI32 + 1);
                    }
                    break;

                case CodeEx.Ldarg_0:
                    stack.Ldarg(0);
                    break;

                case CodeEx.Ldarg_1:
                    stack.Ldarg(1);
                    break;

                case CodeEx.Ldarg_2:
                    stack.Ldarg(2);
                    break;

                case CodeEx.Ldarg_3:
                    stack.Ldarg(3);
                    break;

                case CodeEx.Ldarga:
                    if (body.bodyNative.Method.IsStatic)
                    {
                        stack.Ldarga(_code.tokenI32);
                    }
                    else
                    {
                        stack.Ldarga(_code.tokenI32 + 1);
                    }

                    break;

                case CodeEx.Ldarga_S:
                    if (body.bodyNative.Method.IsStatic)
                    {
                        stack.Ldarga(_code.tokenI32);
                    }
                    else
                    {
                        stack.Ldarga(_code.tokenI32 + 1);
                    }

                    break;

                //转换
                case CodeEx.Conv_I1:
                    stack.Conv_I1();
                    break;

                case CodeEx.Conv_U1:
                    stack.Conv_U1();
                    break;

                case CodeEx.Conv_I2:
                    stack.Conv_I2();
                    break;

                case CodeEx.Conv_U2:
                    stack.Conv_U2();
                    break;

                case CodeEx.Conv_I4:
                    stack.Conv_I4();
                    break;

                case CodeEx.Conv_U4:
                    stack.Conv_U4();
                    break;

                case CodeEx.Conv_I8:
                    stack.Conv_I8();
                    break;

                case CodeEx.Conv_U8:
                    stack.Conv_U8();
                    break;

                case CodeEx.Conv_I:
                    stack.Conv_I();
                    break;

                case CodeEx.Conv_U:
                    stack.Conv_U();
                    break;

                case CodeEx.Conv_R4:
                    stack.Conv_R4();
                    break;

                case CodeEx.Conv_R8:
                    stack.Conv_R8();
                    break;

                case CodeEx.Conv_R_Un:
                    stack.Conv_R_Un();
                    break;

                case CodeEx.Conv_Ovf_I1:
                    stack.Conv_Ovf_I1();
                    break;

                case CodeEx.Conv_Ovf_U1:
                    stack.Conv_Ovf_U1();
                    break;

                case CodeEx.Conv_Ovf_I2:
                    stack.Conv_Ovf_I2();
                    break;

                case CodeEx.Conv_Ovf_U2:
                    stack.Conv_Ovf_U2();
                    break;

                case CodeEx.Conv_Ovf_I4:
                    stack.Conv_Ovf_I4();
                    break;

                case CodeEx.Conv_Ovf_U4:
                    stack.Conv_Ovf_U4();
                    break;

                case CodeEx.Conv_Ovf_I8:
                    stack.Conv_Ovf_I8();
                    break;

                case CodeEx.Conv_Ovf_U8:
                    stack.Conv_Ovf_U8();
                    break;

                case CodeEx.Conv_Ovf_I:
                    stack.Conv_Ovf_I();
                    break;

                case CodeEx.Conv_Ovf_U:
                    stack.Conv_Ovf_U();
                    break;

                case CodeEx.Conv_Ovf_I1_Un:
                    stack.Conv_Ovf_I1_Un();
                    break;

                case CodeEx.Conv_Ovf_U1_Un:
                    stack.Conv_Ovf_U1_Un();
                    break;

                case CodeEx.Conv_Ovf_I2_Un:
                    stack.Conv_Ovf_I2_Un();
                    break;

                case CodeEx.Conv_Ovf_U2_Un:
                    stack.Conv_Ovf_U2_Un();
                    break;

                case CodeEx.Conv_Ovf_I4_Un:
                    stack.Conv_Ovf_I4_Un();
                    break;

                case CodeEx.Conv_Ovf_U4_Un:
                    stack.Conv_Ovf_U4_Un();
                    break;

                case CodeEx.Conv_Ovf_I8_Un:
                    stack.Conv_Ovf_I8_Un();
                    break;

                case CodeEx.Conv_Ovf_U8_Un:
                    stack.Conv_Ovf_U8_Un();
                    break;

                case CodeEx.Conv_Ovf_I_Un:
                    stack.Conv_Ovf_I_Un();
                    break;

                case CodeEx.Conv_Ovf_U_Un:
                    stack.Conv_Ovf_U_Un();
                    break;

                //数组
                case CodeEx.Newarr:
                    stack.NewArr(this, GetNewForArray(_code.tokenUnknown));
                    break;

                case CodeEx.Ldlen:
                    stack.LdLen();
                    break;

                case CodeEx.Ldelema:
                    stack.Ldelema(_code.tokenUnknown);
                    break;

                case CodeEx.Ldelem_I1:
                    stack.Ldelem_I1();
                    break;

                case CodeEx.Ldelem_U1:
                    stack.Ldelem_U1();
                    break;

                case CodeEx.Ldelem_I2:
                    stack.Ldelem_I2();
                    break;

                case CodeEx.Ldelem_U2:
                    stack.Ldelem_U2();
                    break;

                case CodeEx.Ldelem_I4:
                    stack.Ldelem_I4();
                    break;

                case CodeEx.Ldelem_U4:
                    stack.Ldelem_U4();
                    break;

                case CodeEx.Ldelem_I8:
                    stack.Ldelem_I8();
                    break;

                case CodeEx.Ldelem_I:
                    stack.Ldelem_I();
                    break;

                case CodeEx.Ldelem_R4:
                    stack.Ldelem_R4();
                    break;

                case CodeEx.Ldelem_R8:
                    stack.Ldelem_R8();
                    break;

                case CodeEx.Ldelem_Ref:
                    stack.Ldelem_Ref();
                    break;

                case CodeEx.Ldelem_Any:
                    stack.Ldelem_Any(_code.tokenUnknown);
                    break;

                case CodeEx.Stelem_I:
                    stack.Stelem_I();
                    break;

                case CodeEx.Stelem_I1:
                    stack.Stelem_I1();
                    break;

                case CodeEx.Stelem_I2:
                    stack.Stelem_I2();
                    break;

                case CodeEx.Stelem_I4:
                    stack.Stelem_I4();
                    break;

                case CodeEx.Stelem_I8:
                    stack.Stelem_I8();
                    break;

                case CodeEx.Stelem_R4:
                    stack.Stelem_R4();
                    break;

                case CodeEx.Stelem_R8:
                    stack.Stelem_R8();
                    break;

                case CodeEx.Stelem_Ref:
                    stack.Stelem_Ref();
                    break;

                case CodeEx.Stelem_Any:
                    stack.Stelem_Any();
                    break;

                case CodeEx.Newobj:
                    stack.NewObj(this, _code.tokenMethod);
                    break;

                case CodeEx.Dup:
                    stack.Dup();
                    break;

                case CodeEx.Pop:
                    stack.Pop();
                    break;

                case CodeEx.Ldfld:
                    stack.Ldfld(this, _code.tokenField);
                    break;

                case CodeEx.Ldflda:
                    stack.Ldflda(this, _code.tokenField);
                    break;

                case CodeEx.Ldsfld:
                    stack.Ldsfld(this, _code.tokenField);
                    break;

                case CodeEx.Ldsflda:
                    stack.Ldsflda(this, _code.tokenField);
                    break;

                case CodeEx.Stfld:
                    stack.Stfld(this, _code.tokenField);
                    break;

                case CodeEx.Stsfld:
                    stack.Stsfld(this, _code.tokenField);
                    break;


                case CodeEx.Constrained:
                    stack.Constrained(this, _code.tokenType);
                    break;

                case CodeEx.Isinst:
                    stack.Isinst(this, _code.tokenType);
                    break;

                case CodeEx.Ldtoken:
                    stack.Ldtoken(this, GetToken(_code.tokenUnknown));
                    break;

                case CodeEx.Ldftn:
                    stack.Ldftn(this, _code.tokenMethod);
                    break;

                case CodeEx.Ldvirtftn:
                    stack.Ldvirtftn(this, _code.tokenMethod);
                    break;

                case CodeEx.Calli:
                    stack.Calli(this, _code.tokenUnknown);
                    break;

                ///下面是还没有处理的指令
                case CodeEx.Break:
                    stack.Break(this, _code.tokenUnknown);
                    break;

                case CodeEx.Starg_S:
                    stack.Starg_S(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldnull:
                    stack.Ldnull();
                    break;

                case CodeEx.Jmp:
                    stack.Jmp(this, _code.tokenUnknown);
                    break;

                case CodeEx.Switch:
                    stack.Switch(this, _code.tokenAddr_Switch);
                    break;

                case CodeEx.Ldind_I1:
                    stack.Ldind_I1(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_U1:
                    stack.Ldind_U1(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_I2:
                    stack.Ldind_I2(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_U2:
                    stack.Ldind_U2(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_I4:
                    stack.Ldind_I4(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_U4:
                    stack.Ldind_U4(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_I8:
                    stack.Ldind_I8(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_I:
                    stack.Ldind_I(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_R4:
                    stack.Ldind_R4(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_R8:
                    stack.Ldind_R8(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldind_Ref:
                    stack.Ldind_Ref(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_Ref:
                    stack.Stind_Ref(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_I1:
                    stack.Stind_I1(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_I2:
                    stack.Stind_I2(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_I4:
                    stack.Stind_I4(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_I8:
                    stack.Stind_I8(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_R4:
                    stack.Stind_R4(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_R8:
                    stack.Stind_R8(this, _code.tokenUnknown);
                    break;

                case CodeEx.And:
                    stack.And();
                    break;

                case CodeEx.Or:
                    stack.Or();
                    break;

                case CodeEx.Xor:
                    stack.Xor();
                    break;

                case CodeEx.Shl:
                    stack.Shl(this, _code.tokenUnknown);
                    break;

                case CodeEx.Shr:
                    stack.Shr(this, _code.tokenUnknown);
                    break;

                case CodeEx.Shr_Un:
                    stack.Shr_Un(this, _code.tokenUnknown);
                    break;

                case CodeEx.Not:
                    stack.Not();
                    break;

                case CodeEx.Cpobj:
                    stack.Cpobj(this, _code.tokenUnknown);
                    break;

                case CodeEx.Ldobj:
                    stack.Ldobj(this, _code.tokenUnknown);
                    break;

                case CodeEx.Castclass:
                    stack.Castclass(this, _code.tokenType);
                    break;

                case CodeEx.Throw:
                    stack.Throw(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stobj:
                    stack.Stobj(this, _code.tokenUnknown);
                    break;

                case CodeEx.Refanyval:
                    stack.Refanyval(this, _code.tokenUnknown);
                    break;

                case CodeEx.Mkrefany:
                    stack.Mkrefany(this, _code.tokenUnknown);
                    break;

                case CodeEx.Add_Ovf:
                    stack.Add_Ovf(this, _code.tokenUnknown);
                    break;

                case CodeEx.Add_Ovf_Un:
                    stack.Add_Ovf_Un(this, _code.tokenUnknown);
                    break;

                case CodeEx.Mul_Ovf:
                    stack.Mul_Ovf(this, _code.tokenUnknown);
                    break;

                case CodeEx.Mul_Ovf_Un:
                    stack.Mul_Ovf_Un(this, _code.tokenUnknown);
                    break;

                case CodeEx.Sub_Ovf:
                    stack.Sub_Ovf(this, _code.tokenUnknown);
                    break;

                case CodeEx.Sub_Ovf_Un:
                    stack.Sub_Ovf_Un(this, _code.tokenUnknown);
                    break;

                case CodeEx.Endfinally:
                    stack.Endfinally(this, _code.tokenUnknown);
                    break;

                case CodeEx.Stind_I:
                    stack.Stind_I(this, _code.tokenUnknown);
                    break;

                case CodeEx.Arglist:
                    stack.Arglist(this, _code.tokenUnknown);
                    break;

                case CodeEx.Starg:
                    stack.Starg(this, _code.tokenUnknown);
                    break;

                case CodeEx.Localloc:
                    stack.Localloc(this, _code.tokenUnknown);
                    break;

                case CodeEx.Endfilter:
                    stack.Endfilter(this, _code.tokenUnknown);
                    break;

                case CodeEx.Unaligned:
                    stack.Unaligned(this, _code.tokenUnknown);
                    break;

                case CodeEx.Volatile:
                    stack.Volatile(this, _code.tokenUnknown);
                    break;

                case CodeEx.Tail:
                    stack.Tail(this, _code.tokenUnknown);
                    break;

                case CodeEx.Initobj:
                    stack.Initobj(this, _code.tokenType);
                    break;

                case CodeEx.Cpblk:
                    stack.Cpblk(this, _code.tokenUnknown);
                    break;

                case CodeEx.Initblk:
                    stack.Initblk(this, _code.tokenUnknown);
                    break;

                case CodeEx.No:
                    stack.No(this, _code.tokenUnknown);
                    break;

                case CodeEx.Rethrow:
                    stack.Rethrow(this, _code.tokenUnknown);
                    break;

                case CodeEx.Sizeof:
                    stack.Sizeof(this, _code.tokenUnknown);
                    break;

                case CodeEx.Refanytype:
                    stack.Refanytype(this, _code.tokenUnknown);
                    break;

                case CodeEx.Readonly:
                    stack.Readonly(this, _code.tokenUnknown);
                    break;

                default:
                    throw new Exception("未实现的OpCode:" + _code.code);
                }
            }
        }
Esempio n. 11
0
        bool JumpToErr(CodeBody body, StackFrame frame, Exception err)
        {
            var posnow = frame.GetCode();
            List <Mono.Cecil.Cil.ExceptionHandler> ehs = new List <ExceptionHandler>();

            Mono.Cecil.Cil.ExceptionHandler ehNear = null;
            int ehNearB = -1;

            foreach (var eh in body.bodyNative.ExceptionHandlers)
            {
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                {
                    Type ehtype = GetType(eh.CatchType).TypeForSystem;
                    if (ehtype == err.GetType() || err.GetType().IsSubclassOf(ehtype))
                    //if(GetType(eh.CatchType)== environment.GetType(err.GetType()))
                    {
                        if (eh.TryStart.Offset <= posnow.Offset && eh.TryEnd.Offset >= posnow.Offset)
                        {
                            if (ehNear == null)
                            {
                                ehNear  = eh;//第一个
                                ehNearB = GetBaseCount(ehtype, err.GetType());
                            }
                            else
                            {
                                if (eh.TryStart.Offset > ehNear.TryStart.Offset || eh.TryEnd.Offset < ehNear.TryEnd.Offset)//范围更小
                                {
                                    ehNear  = eh;
                                    ehNearB = GetBaseCount(ehtype, err.GetType());
                                }
                                else if (eh.TryStart.Offset == ehNear.TryStart.Offset || eh.TryEnd.Offset == ehNear.TryEnd.Offset) //范围相等
                                {
                                    if (ehtype == err.GetType())                                                                   //类型一致,没有比这个更牛的了
                                    {
                                        ehNear  = eh;
                                        ehNearB = GetBaseCount(ehtype, err.GetType());
                                    }
                                    else if (GetType(ehNear.CatchType).TypeForSystem == err.GetType())//上次找到的就是第一,不用比了
                                    {
                                        continue;
                                    }
                                    else //比较上次找到的类型,和这次找到的类型的亲缘性;
                                    {
                                        int newehNearB = GetBaseCount(ehtype, err.GetType());
                                        if (newehNearB == -1)
                                        {
                                            continue;
                                        }
                                        if (newehNearB < ehNearB)
                                        {
                                            ehNear  = eh;
                                            ehNearB = newehNearB;
                                        }
                                    }
                                }
                            }
                            ehs.Add(eh);
                        }
                    }
                }
            }
            if (ehNear != null)
            {
                frame.stackCalc.Push(err);
                frame.SetCodePos(ehNear.HandlerStart.Offset);// ._pos = ehNear.HandlerStart;
                RunCodeWithTry(body, frame);
                return(true);
            }
            return(false);
        }
Esempio n. 12
0
        void RunCode(StackFrame stack, CodeBody body)
        {
            Mono.Collections.Generic.Collection <Mono.Cecil.Cil.Instruction> codes = body.bodyNative.Instructions;
            while (true)
            {
                var code = stack._pos;
                if (DebugLevel >= 9)
                {
                    environment.logger.Log(code.ToString());
                }
                switch (code.OpCode.Code)
                {
                ///////////
                //流程控制

                case Code.Nop:
                    stack.Nop();
                    break;

                case Code.Ret:
                    stack.Ret();
                    return;

                case Code.Leave:
                    stack.Leave(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Leave_S:
                    //stack.Ret();
                    stack.Leave(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                //流程控制之goto
                case Code.Br:
                    stack.Br(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Br_S:
                    stack.Br(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Brtrue:
                    stack.Brtrue(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Brtrue_S:
                    stack.Brtrue(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Brfalse:
                    stack.Brfalse(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Brfalse_S:
                    stack.Brfalse(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                //比较流程控制
                case Code.Beq:
                    stack.Beq(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Beq_S:
                    stack.Beq(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bne_Un:
                    stack.Bne_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bne_Un_S:
                    stack.Bne_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bge:
                    stack.Bge(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bge_S:
                    stack.Bge(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bge_Un:
                    stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bge_Un_S:
                    stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bgt:
                    stack.Bgt(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bgt_S:
                    stack.Bgt(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bgt_Un:
                    stack.Bgt_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Bgt_Un_S:
                    stack.Bge_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Ble:
                    stack.Ble(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Ble_S:
                    stack.Ble(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Ble_Un:
                    stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Ble_Un_S:
                    stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Blt:
                    stack.Blt(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Blt_S:
                    stack.Blt(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Blt_Un:
                    stack.Blt_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                case Code.Blt_Un_S:
                    stack.Ble_Un(code.Operand as Mono.Cecil.Cil.Instruction);
                    break;

                //逻辑计算
                case Code.Ceq:
                    stack.Ceq();
                    break;

                case Code.Cgt:
                    stack.Cgt();
                    break;

                case Code.Cgt_Un:
                    stack.Cgt_Un();
                    break;

                case Code.Clt:
                    stack.Clt();
                    break;

                case Code.Clt_Un:
                    stack.Clt_Un();
                    break;

                case Code.Ckfinite:
                    stack.Ckfinite();
                    break;

                //常量加载
                case Code.Ldc_I4:
                    stack.Ldc_I4((int)Convert.ToDecimal(code.Operand));
                    break;

                case Code.Ldc_I4_S:
                    stack.Ldc_I4((int)Convert.ToDecimal(code.Operand));
                    break;

                case Code.Ldc_I4_M1:
                    stack.Ldc_I4(-1);
                    break;

                case Code.Ldc_I4_0:
                    stack.Ldc_I4(0);
                    break;

                case Code.Ldc_I4_1:
                    stack.Ldc_I4(1);
                    break;

                case Code.Ldc_I4_2:
                    stack.Ldc_I4(2);
                    break;

                case Code.Ldc_I4_3:
                    stack.Ldc_I4(3);
                    break;

                case Code.Ldc_I4_4:
                    stack.Ldc_I4(4);
                    break;

                case Code.Ldc_I4_5:
                    stack.Ldc_I4(5);
                    break;

                case Code.Ldc_I4_6:
                    stack.Ldc_I4(6);
                    break;

                case Code.Ldc_I4_7:
                    stack.Ldc_I4(7);
                    break;

                case Code.Ldc_I4_8:
                    stack.Ldc_I4(8);
                    break;

                case Code.Ldc_I8:
                    stack.Ldc_I8((Int64)(Convert.ToDecimal(code.Operand)));
                    break;

                case Code.Ldc_R4:
                    stack.Ldc_R4((float)(Convert.ToDecimal(code.Operand)));
                    break;

                case Code.Ldc_R8:
                    stack.Ldc_R8((double)(Convert.ToDecimal(code.Operand)));
                    break;

                //定义为临时变量
                case Code.Stloc:
                    stack.Stloc((int)code.Operand);
                    break;

                case Code.Stloc_S:
                    stack.Stloc(((VariableDefinition)code.Operand).Index);
                    break;

                case Code.Stloc_0:
                    stack.Stloc(0);
                    break;

                case Code.Stloc_1:
                    stack.Stloc(1);
                    break;

                case Code.Stloc_2:
                    stack.Stloc(2);
                    break;

                case Code.Stloc_3:
                    stack.Stloc(3);
                    break;

                //从临时变量加载
                case Code.Ldloc:
                    stack.Ldloc((int)code.Operand);
                    break;

                case Code.Ldloc_S:
                    stack.Ldloc(((VariableDefinition)code.Operand).Index);
                    break;

                case Code.Ldloc_0:
                    stack.Ldloc(0);
                    break;

                case Code.Ldloc_1:
                    stack.Ldloc(1);
                    break;

                case Code.Ldloc_2:
                    stack.Ldloc(2);
                    break;

                case Code.Ldloc_3:
                    stack.Ldloc(3);
                    break;

                case Code.Ldloca:
                    stack.Ldloca(((VariableDefinition)code.Operand).Index);
                    break;

                case Code.Ldloca_S:
                    stack.Ldloca(((VariableDefinition)code.Operand).Index);
                    break;

                //加载字符串
                case Code.Ldstr:
                    stack.Ldstr(code.Operand as string);
                    break;

                //呼叫函数
                case Code.Call:
                    stack.Call(this, GetMethod(code.Operand), false);
                    break;

                case Code.Callvirt:
                    stack.Call(this, GetMethod(code.Operand), true);
                    break;

                //算术指令
                case Code.Add:
                    stack.Add();
                    break;

                case Code.Sub:
                    stack.Sub();
                    break;

                case Code.Mul:
                    stack.Mul();
                    break;

                case Code.Div:
                    stack.Div();
                    break;

                case Code.Div_Un:
                    stack.Div_Un();
                    break;

                case Code.Rem:
                    stack.Rem();
                    break;

                case Code.Rem_Un:
                    stack.Rem_Un();
                    break;

                case Code.Neg:
                    stack.Neg();
                    break;

                //装箱
                case Code.Box:
                    stack.Box(GetType(code.Operand));
                    break;

                case Code.Unbox:
                    stack.Unbox();
                    break;

                case Code.Unbox_Any:
                    stack.Unbox_Any();
                    break;

                //加载参数
                case Code.Ldarg:
                    stack.Ldarg((int)code.Operand);
                    break;

                case Code.Ldarg_S:
                    stack.Ldarg(GetParamPos(code.Operand));
                    break;

                case Code.Ldarg_0:
                    stack.Ldarg(0);
                    break;

                case Code.Ldarg_1:
                    stack.Ldarg(1);
                    break;

                case Code.Ldarg_2:
                    stack.Ldarg(2);
                    break;

                case Code.Ldarg_3:
                    stack.Ldarg(3);
                    break;

                //转换
                case Code.Conv_I1:
                    stack.Conv_I1();
                    break;

                case Code.Conv_U1:
                    stack.Conv_U1();
                    break;

                case Code.Conv_I2:
                    stack.Conv_I2();
                    break;

                case Code.Conv_U2:
                    stack.Conv_U2();
                    break;

                case Code.Conv_I4:
                    stack.Conv_I4();
                    break;

                case Code.Conv_U4:
                    stack.Conv_U4();
                    break;

                case Code.Conv_I8:
                    stack.Conv_I8();
                    break;

                case Code.Conv_U8:
                    stack.Conv_U8();
                    break;

                case Code.Conv_I:
                    stack.Conv_I();
                    break;

                case Code.Conv_U:
                    stack.Conv_U();
                    break;

                case Code.Conv_R4:
                    stack.Conv_R4();
                    break;

                case Code.Conv_R8:
                    stack.Conv_R8();
                    break;

                case Code.Conv_R_Un:
                    stack.Conv_R_Un();
                    break;

                case Code.Conv_Ovf_I1:
                    stack.Conv_Ovf_I1();
                    break;

                case Code.Conv_Ovf_U1:
                    stack.Conv_Ovf_U1();
                    break;

                case Code.Conv_Ovf_I2:
                    stack.Conv_Ovf_I2();
                    break;

                case Code.Conv_Ovf_U2:
                    stack.Conv_Ovf_U2();
                    break;

                case Code.Conv_Ovf_I4:
                    stack.Conv_Ovf_I4();
                    break;

                case Code.Conv_Ovf_U4:
                    stack.Conv_Ovf_U4();
                    break;

                case Code.Conv_Ovf_I8:
                    stack.Conv_Ovf_I8();
                    break;

                case Code.Conv_Ovf_U8:
                    stack.Conv_Ovf_U8();
                    break;

                case Code.Conv_Ovf_I:
                    stack.Conv_Ovf_I();
                    break;

                case Code.Conv_Ovf_U:
                    stack.Conv_Ovf_U();
                    break;

                case Code.Conv_Ovf_I1_Un:
                    stack.Conv_Ovf_I1_Un();
                    break;

                case Code.Conv_Ovf_U1_Un:
                    stack.Conv_Ovf_U1_Un();
                    break;

                case Code.Conv_Ovf_I2_Un:
                    stack.Conv_Ovf_I2_Un();
                    break;

                case Code.Conv_Ovf_U2_Un:
                    stack.Conv_Ovf_U2_Un();
                    break;

                case Code.Conv_Ovf_I4_Un:
                    stack.Conv_Ovf_I4_Un();
                    break;

                case Code.Conv_Ovf_U4_Un:
                    stack.Conv_Ovf_U4_Un();
                    break;

                case Code.Conv_Ovf_I8_Un:
                    stack.Conv_Ovf_I8_Un();
                    break;

                case Code.Conv_Ovf_U8_Un:
                    stack.Conv_Ovf_U8_Un();
                    break;

                case Code.Conv_Ovf_I_Un:
                    stack.Conv_Ovf_I_Un();
                    break;

                case Code.Conv_Ovf_U_Un:
                    stack.Conv_Ovf_U_Un();
                    break;

                //数组
                case Code.Newarr:
                    stack.NewArr(this, GetNewForArray(code.Operand));
                    break;

                case Code.Ldlen:
                    stack.LdLen();
                    break;

                case Code.Ldelema:
                    stack.Ldelema(code.Operand);
                    break;

                case Code.Ldelem_I1:
                    stack.Ldelem_I1();
                    break;

                case Code.Ldelem_U1:
                    stack.Ldelem_U1();
                    break;

                case Code.Ldelem_I2:
                    stack.Ldelem_I2();
                    break;

                case Code.Ldelem_U2:
                    stack.Ldelem_U2();
                    break;

                case Code.Ldelem_I4:
                    stack.Ldelem_I4();
                    break;

                case Code.Ldelem_U4:
                    stack.Ldelem_U4();
                    break;

                case Code.Ldelem_I8:
                    stack.Ldelem_I8();
                    break;

                case Code.Ldelem_I:
                    stack.Ldelem_I();
                    break;

                case Code.Ldelem_R4:
                    stack.Ldelem_R4();
                    break;

                case Code.Ldelem_R8:
                    stack.Ldelem_R8();
                    break;

                case Code.Ldelem_Ref:
                    stack.Ldelem_Ref();
                    break;

                case Code.Ldelem_Any:
                    stack.Ldelem_Any(code.Operand);
                    break;

                case Code.Stelem_I:
                    stack.Stelem_I();
                    break;

                case Code.Stelem_I1:
                    stack.Stelem_I1();
                    break;

                case Code.Stelem_I2:
                    stack.Stelem_I2();
                    break;

                case Code.Stelem_I4:
                    stack.Stelem_I4();
                    break;

                case Code.Stelem_I8:
                    stack.Stelem_I8();
                    break;

                case Code.Stelem_R4:
                    stack.Stelem_R4();
                    break;

                case Code.Stelem_R8:
                    stack.Stelem_R8();
                    break;

                case Code.Stelem_Ref:
                    stack.Stelem_Ref();
                    break;

                case Code.Stelem_Any:
                    stack.Stelem_Any();
                    break;

                case Code.Newobj:
                    stack.NewObj(this, GetMethod(code.Operand));
                    break;

                case Code.Dup:
                    stack.Dup();
                    break;

                case Code.Pop:
                    stack.Pop();
                    break;

                case Code.Ldfld:
                    stack.Ldfld(this, GetField(code.Operand));
                    break;

                case Code.Ldflda:
                    stack.Ldflda(this, GetField(code.Operand));
                    break;

                case Code.Ldsfld:
                    stack.Ldsfld(this, GetField(code.Operand));
                    break;

                case Code.Ldsflda:
                    stack.Ldsflda(this, GetField(code.Operand));
                    break;

                case Code.Stfld:
                    stack.Stfld(this, GetField(code.Operand));
                    break;

                case Code.Stsfld:
                    stack.Stsfld(this, GetField(code.Operand));
                    break;


                case Code.Constrained:
                    stack.Constrained(this, GetType(code.Operand));
                    break;

                case Code.Isinst:
                    stack.Isinst(this, GetType(code.Operand));
                    break;

                case Code.Ldtoken:
                    stack.Ldtoken(this, GetToken(code.Operand));
                    break;

                case Code.Ldftn:
                    stack.Ldftn(this, GetMethod(code.Operand));
                    break;

                case Code.Ldvirtftn:
                    stack.Ldvirtftn(this, GetMethod(code.Operand));
                    break;

                case Code.Ldarga:
                    stack.Ldarga(this, code.Operand);
                    break;

                case Code.Ldarga_S:
                    stack.Ldarga(this, code.Operand);
                    break;

                case Code.Calli:
                    stack.Calli(this, code.Operand);
                    break;

                ///下面是还没有处理的指令
                case Code.Break:
                    stack.Break(this, code.Operand);
                    break;

                case Code.Starg_S:
                    stack.Starg_S(this, code.Operand);
                    break;

                case Code.Ldnull:
                    stack.Ldnull();
                    break;

                case Code.Jmp:
                    stack.Jmp(this, code.Operand);
                    break;

                case Code.Switch:
                    stack.Switch(this, code.Operand as Mono.Cecil.Cil.Instruction[]);
                    break;

                case Code.Ldind_I1:
                    stack.Ldind_I1(this, code.Operand);
                    break;

                case Code.Ldind_U1:
                    stack.Ldind_U1(this, code.Operand);
                    break;

                case Code.Ldind_I2:
                    stack.Ldind_I2(this, code.Operand);
                    break;

                case Code.Ldind_U2:
                    stack.Ldind_U2(this, code.Operand);
                    break;

                case Code.Ldind_I4:
                    stack.Ldind_I4(this, code.Operand);
                    break;

                case Code.Ldind_U4:
                    stack.Ldind_U4(this, code.Operand);
                    break;

                case Code.Ldind_I8:
                    stack.Ldind_I8(this, code.Operand);
                    break;

                case Code.Ldind_I:
                    stack.Ldind_I(this, code.Operand);
                    break;

                case Code.Ldind_R4:
                    stack.Ldind_R4(this, code.Operand);
                    break;

                case Code.Ldind_R8:
                    stack.Ldind_R8(this, code.Operand);
                    break;

                case Code.Ldind_Ref:
                    stack.Ldind_Ref(this, code.Operand);
                    break;

                case Code.Stind_Ref:
                    stack.Stind_Ref(this, code.Operand);
                    break;

                case Code.Stind_I1:
                    stack.Stind_I1(this, code.Operand);
                    break;

                case Code.Stind_I2:
                    stack.Stind_I2(this, code.Operand);
                    break;

                case Code.Stind_I4:
                    stack.Stind_I4(this, code.Operand);
                    break;

                case Code.Stind_I8:
                    stack.Stind_I8(this, code.Operand);
                    break;

                case Code.Stind_R4:
                    stack.Stind_R4(this, code.Operand);
                    break;

                case Code.Stind_R8:
                    stack.Stind_R8(this, code.Operand);
                    break;

                case Code.And:
                    stack.And(this, code.Operand);
                    break;

                case Code.Or:
                    stack.Or(this, code.Operand);
                    break;

                case Code.Xor:
                    stack.Xor(this, code.Operand);
                    break;

                case Code.Shl:
                    stack.Shl(this, code.Operand);
                    break;

                case Code.Shr:
                    stack.Shr(this, code.Operand);
                    break;

                case Code.Shr_Un:
                    stack.Shr_Un(this, code.Operand);
                    break;

                case Code.Not:
                    stack.Not(this, code.Operand);
                    break;

                case Code.Cpobj:
                    stack.Cpobj(this, code.Operand);
                    break;

                case Code.Ldobj:
                    stack.Ldobj(this, code.Operand);
                    break;

                case Code.Castclass:
                    stack.Castclass(this, code.Operand);
                    break;

                case Code.Throw:
                    stack.Throw(this, code.Operand);
                    break;

                case Code.Stobj:
                    stack.Stobj(this, code.Operand);
                    break;

                case Code.Refanyval:
                    stack.Refanyval(this, code.Operand);
                    break;

                case Code.Mkrefany:
                    stack.Mkrefany(this, code.Operand);
                    break;

                case Code.Add_Ovf:
                    stack.Add_Ovf(this, code.Operand);
                    break;

                case Code.Add_Ovf_Un:
                    stack.Add_Ovf_Un(this, code.Operand);
                    break;

                case Code.Mul_Ovf:
                    stack.Mul_Ovf(this, code.Operand);
                    break;

                case Code.Mul_Ovf_Un:
                    stack.Mul_Ovf_Un(this, code.Operand);
                    break;

                case Code.Sub_Ovf:
                    stack.Sub_Ovf(this, code.Operand);
                    break;

                case Code.Sub_Ovf_Un:
                    stack.Sub_Ovf_Un(this, code.Operand);
                    break;

                case Code.Endfinally:
                    stack.Endfinally(this, code.Operand);
                    break;

                case Code.Stind_I:
                    stack.Stind_I(this, code.Operand);
                    break;

                case Code.Arglist:
                    stack.Arglist(this, code.Operand);
                    break;

                case Code.Starg:
                    stack.Starg(this, code.Operand);
                    break;

                case Code.Localloc:
                    stack.Localloc(this, code.Operand);
                    break;

                case Code.Endfilter:
                    stack.Endfilter(this, code.Operand);
                    break;

                case Code.Unaligned:
                    stack.Unaligned(this, code.Operand);
                    break;

                case Code.Volatile:
                    stack.Volatile(this, code.Operand);
                    break;

                case Code.Tail:
                    stack.Tail(this, code.Operand);
                    break;

                case Code.Initobj:
                    stack.Initobj(this, this.GetType(code.Operand));
                    break;

                case Code.Cpblk:
                    stack.Cpblk(this, code.Operand);
                    break;

                case Code.Initblk:
                    stack.Initblk(this, code.Operand);
                    break;

                case Code.No:
                    stack.No(this, code.Operand);
                    break;

                case Code.Rethrow:
                    stack.Rethrow(this, code.Operand);
                    break;

                case Code.Sizeof:
                    stack.Sizeof(this, code.Operand);
                    break;

                case Code.Refanytype:
                    stack.Refanytype(this, code.Operand);
                    break;

                case Code.Readonly:
                    stack.Readonly(this, code.Operand);
                    break;

                default:
                    throw new Exception("未实现的OpCode:" + code.OpCode.Code);
                }
            }
        }
Esempio n. 13
0
            public void InitToken(ThreadContext context, CodeBody body, object _p)
            {
                CodeEx codeEx = this.code;

                switch (codeEx)
                {
                case CodeEx.Ldloc_0:
                    this.tokenI32 = 0;
                    return;

                case CodeEx.Ldloc_1:
                    this.tokenI32 = 1;
                    return;

                case CodeEx.Ldloc_2:
                    this.tokenI32 = 2;
                    return;

                case CodeEx.Ldloc_3:
                    this.tokenI32 = 3;
                    return;

                case CodeEx.Stloc_0:
                case CodeEx.Stloc_1:
                case CodeEx.Stloc_2:
                case CodeEx.Stloc_3:
                case CodeEx.Ldnull:
                case CodeEx.Dup:
                case CodeEx.Pop:
                case CodeEx.Jmp:
                case CodeEx.Calli:
                case CodeEx.Ret:
                case CodeEx.Stind_Ref:
                case CodeEx.Stind_I1:
                case CodeEx.Stind_I2:
                case CodeEx.Stind_I4:
                case CodeEx.Stind_I8:
                case CodeEx.Stind_R4:
                case CodeEx.Stind_R8:
                case CodeEx.Add:
                case CodeEx.Sub:
                case CodeEx.Mul:
                case CodeEx.Div:
                case CodeEx.Div_Un:
                case CodeEx.Rem:
                case CodeEx.Rem_Un:
                case CodeEx.And:
                case CodeEx.Or:
                case CodeEx.Xor:
                case CodeEx.Shl:
                case CodeEx.Shr:
                case CodeEx.Shr_Un:
                case CodeEx.Neg:
                case CodeEx.Not:
                case CodeEx.Conv_I1:
                case CodeEx.Conv_I2:
                case CodeEx.Conv_I4:
                case CodeEx.Conv_I8:
                case CodeEx.Conv_R4:
                case CodeEx.Conv_R8:
                case CodeEx.Conv_U4:
                case CodeEx.Conv_U8:
                case CodeEx.Cpobj:
                case CodeEx.Ldobj:
                case CodeEx.Conv_R_Un:
                case CodeEx.Unbox:
                case CodeEx.Throw:
                case CodeEx.Stobj:
                case CodeEx.Conv_Ovf_I1_Un:
                case CodeEx.Conv_Ovf_I2_Un:
                case CodeEx.Conv_Ovf_I4_Un:
                case CodeEx.Conv_Ovf_I8_Un:
                case CodeEx.Conv_Ovf_U1_Un:
                case CodeEx.Conv_Ovf_U2_Un:
                case CodeEx.Conv_Ovf_U4_Un:
                case CodeEx.Conv_Ovf_U8_Un:
                case CodeEx.Conv_Ovf_I_Un:
                case CodeEx.Conv_Ovf_U_Un:
                    goto IL_4AA;

                case CodeEx.Ldarg_S:
                    this.tokenI32 = (_p as ParameterReference).Index;
                    return;

                case CodeEx.Ldarga_S:
                case CodeEx.Starg_S:
                    goto IL_42C;

                case CodeEx.Ldloc_S:
                case CodeEx.Ldloca_S:
                case CodeEx.Stloc_S:
                    goto IL_3D5;

                case CodeEx.Ldc_I4_M1:
                    this.tokenI32 = -1;
                    return;

                case CodeEx.Ldc_I4_0:
                    this.tokenI32 = 0;
                    return;

                case CodeEx.Ldc_I4_1:
                    this.tokenI32 = 1;
                    return;

                case CodeEx.Ldc_I4_2:
                    this.tokenI32 = 2;
                    return;

                case CodeEx.Ldc_I4_3:
                    this.tokenI32 = 3;
                    return;

                case CodeEx.Ldc_I4_4:
                    this.tokenI32 = 4;
                    return;

                case CodeEx.Ldc_I4_5:
                    this.tokenI32 = 5;
                    return;

                case CodeEx.Ldc_I4_6:
                    this.tokenI32 = 6;
                    return;

                case CodeEx.Ldc_I4_7:
                    this.tokenI32 = 7;
                    return;

                case CodeEx.Ldc_I4_8:
                    this.tokenI32 = 8;
                    return;

                case CodeEx.Ldc_I4_S:
                    this.tokenI32 = (int)Convert.ToDecimal(_p);
                    return;

                case CodeEx.Ldc_I4:
                    this.tokenI32 = (int)_p;
                    return;

                case CodeEx.Ldc_I8:
                    this.tokenI64 = (long)_p;
                    return;

                case CodeEx.Ldc_R4:
                    this.tokenR32 = (float)_p;
                    return;

                case CodeEx.Ldc_R8:
                    this.tokenR64 = (double)_p;
                    return;

                case CodeEx.Call:
                case CodeEx.Callvirt:
                case CodeEx.Newobj:
                    goto IL_2E0;

                case CodeEx.Br_S:
                case CodeEx.Brfalse_S:
                case CodeEx.Brtrue_S:
                case CodeEx.Beq_S:
                case CodeEx.Bge_S:
                case CodeEx.Bgt_S:
                case CodeEx.Ble_S:
                case CodeEx.Blt_S:
                case CodeEx.Bne_Un_S:
                case CodeEx.Bge_Un_S:
                case CodeEx.Bgt_Un_S:
                case CodeEx.Ble_Un_S:
                case CodeEx.Blt_Un_S:
                case CodeEx.Br:
                case CodeEx.Brfalse:
                case CodeEx.Brtrue:
                case CodeEx.Beq:
                case CodeEx.Bge:
                case CodeEx.Bgt:
                case CodeEx.Ble:
                case CodeEx.Blt:
                case CodeEx.Bne_Un:
                case CodeEx.Bge_Un:
                case CodeEx.Bgt_Un:
                case CodeEx.Ble_Un:
                case CodeEx.Blt_Un:
                    break;

                case CodeEx.Switch:
                {
                    Instruction[] array = _p as Instruction[];
                    this.tokenAddr_Switch = new int[array.Length];
                    for (int i = 0; i < array.Length; i++)
                    {
                        this.tokenAddr_Switch[i] = body.addr[array[i].Offset];
                    }
                    return;
                }

                case CodeEx.Ldind_I1:
                case CodeEx.Ldind_U1:
                case CodeEx.Ldind_I2:
                case CodeEx.Ldind_U2:
                case CodeEx.Ldind_I4:
                case CodeEx.Ldind_U4:
                case CodeEx.Ldind_I8:
                case CodeEx.Ldind_I:
                case CodeEx.Ldind_R4:
                case CodeEx.Ldind_R8:
                case CodeEx.Ldind_Ref:
                    goto IL_4A8;

                case CodeEx.Ldstr:
                    this.tokenStr = (_p as string);
                    return;

                case CodeEx.Castclass:
                case CodeEx.Isinst:
                case CodeEx.Box:
                case CodeEx.Newarr:
                    goto IL_2BC;

                case CodeEx.Ldfld:
                case CodeEx.Ldflda:
                case CodeEx.Stfld:
                case CodeEx.Ldsfld:
                case CodeEx.Ldsflda:
                case CodeEx.Stsfld:
                    this.tokenField = context.GetField(_p);
                    return;

                default:
                    switch (codeEx)
                    {
                    case CodeEx.Leave:
                    case CodeEx.Leave_S:
                        break;

                    case CodeEx.Stind_I:
                    case CodeEx.Conv_U:
                    case CodeEx.Arglist:
                    case CodeEx.Ceq:
                    case CodeEx.Cgt:
                    case CodeEx.Cgt_Un:
                    case CodeEx.Clt:
                    case CodeEx.Clt_Un:
                    case CodeEx.Localloc:
                    case CodeEx.Endfilter:
                    case CodeEx.Unaligned:
                    case CodeEx.Tail:
                        goto IL_4AA;

                    case CodeEx.Ldftn:
                    case CodeEx.Ldvirtftn:
                        goto IL_2E0;

                    case CodeEx.Ldarg:
                        this.tokenI32 = (int)_p;
                        return;

                    case CodeEx.Ldarga:
                    case CodeEx.Starg:
                        goto IL_42C;

                    case CodeEx.Ldloc:
                    case CodeEx.Stloc:
                        this.tokenI32 = (int)_p;
                        return;

                    case CodeEx.Ldloca:
                        goto IL_3D5;

                    case CodeEx.Volatile:
                        goto IL_4A8;

                    case CodeEx.Initobj:
                    case CodeEx.Constrained:
                        goto IL_2BC;

                    default:
                        goto IL_4AA;
                    }
                    break;
                }
                this.tokenAddr_Index = body.addr[((Instruction)_p).Offset];
                return;

IL_2BC:
                this.tokenType = context.GetType(_p);
                return;

IL_2E0:
                this.tokenMethod = context.GetMethod(_p);
                return;

IL_3D5:
                this.tokenI32 = ((VariableDefinition)_p).Index;
                return;

IL_42C:
                this.tokenI32 = (_p as ParameterDefinition).Index;
IL_4A8:
                return;

IL_4AA:
                this.tokenUnknown = _p;
            }