Beispiel #1
0
        private AnalysisNet.IInstruction ProcessStoreLocal(Cecil.Cil.Instruction op)
        {
            int localIdx = -1;

            Cecil.Cil.VariableDefinition variable = null;
            switch (op.OpCode.Code)
            {
            case Mono.Cecil.Cil.Code.Stloc_S:
            case Mono.Cecil.Cil.Code.Stloc: variable = (Cecil.Cil.VariableDefinition)op.Operand; break;

            case Mono.Cecil.Cil.Code.Stloc_0: localIdx = 0; break;

            case Mono.Cecil.Cil.Code.Stloc_1: localIdx = 1; break;

            case Mono.Cecil.Cil.Code.Stloc_2: localIdx = 2; break;

            case Mono.Cecil.Cil.Code.Stloc_3: localIdx = 3; break;

            default:
                throw new NotImplementedException();
            }

            AnalysisNetTac.Values.IVariable dest;
            if (variable != null)
            {
                dest = locals[variable.Index];
            }
            else
            {
                dest = locals[localIdx];
            }

            AnalysisNetBytecode.StoreInstruction instruction = new AnalysisNetBytecode.StoreInstruction((uint)op.Offset, dest);
            return(instruction);
        }
Beispiel #2
0
            private void IncrementUseCount(Mono.Cecil.Cil.VariableDefinition register)
            {
                int count;

                if (!registerUseCounters.TryGetValue(register, out count))
                {
                    count = 0;
                }
                count++;
                registerUseCounters[register] = count;
            }
Beispiel #3
0
        private string GetLocalSourceName(Cecil.Cil.VariableDefinition local)
        {
            //var name = local.Name.Value;
            string name = local.ToString();

            //if (sourceLocationProvider != null)
            //{
            //    bool isCompilerGenerated;
            //    name = sourceLocationProvider.GetSourceNameFor(local, out isCompilerGenerated);
            //}

            return(name);
        }
Beispiel #4
0
        private IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> CreateLocalVariables(Model.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef)
        {
            IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions = new Dictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition>();

            foreach (AnalysisNet.ThreeAddressCode.Values.IVariable localVariable in methodDefinition.Body.LocalVariables)
            {
                Cecil.Cil.VariableDefinition varDef = new Cecil.Cil.VariableDefinition(ReferenceGenerator.TypeReference(localVariable.Type));
                methodDef.Body.Variables.Add(varDef);
                variableDefinitions[localVariable] = varDef;
            }

            return(variableDefinitions);
        }
Beispiel #5
0
        public static Thread.ReaderWriterLockSlim GetLock(object instance)
        {
            Thread.ReaderWriterLockSlim @lock;

            instance = instance ?? StaticInstance;

            if (!Locks.TryGetValue(instance, out @lock))
            {
                @lock = Locks[instance] = new Thread.ReaderWriterLockSlim();
            }

            return(@lock);
        }
Beispiel #6
0
        /// <summary>
        /// Aspect code to inject at the beginning of weaved method
        /// </summary>
        /// <param name="typeBuilder">Type Builder</param>
        /// <param name="method">Method</param>
        /// <param name="parameter">Parameter</param>
        /// <param name="il">ILGenerator</param>
        internal void BeginBlock(Mono.Cecil.TypeDefinition typeBuilder, Mono.Cecil.MethodDefinition method, Mono.Cecil.ParameterDefinition parameter, Mono.Cecil.Cil.ILProcessor il)
        {
            var meth       = method;
            var returnType = meth.ReturnType.ReflectionType();

            @lock = il.DeclareLocal(typeof(Thread.ReaderWriterLockSlim));

            il.Emit(method.IsStatic ? Mono.Cecil.Cil.OpCodes.Ldnull : Mono.Cecil.Cil.OpCodes.Ldarg_0);
            il.Emit(Mono.Cecil.Cil.OpCodes.Call, GetLockMethod);
            il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, @lock);

            il.BeginExceptionBlock();
            il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, @lock);
            il.Emit(Mono.Cecil.Cil.OpCodes.Callvirt, EnterMethod);
        }
Beispiel #7
0
        /// <summary>
        /// Aspect code to inject at the beginning of weaved method
        /// </summary>
        /// <param name="typeBuilder">Type Builder</param>
        /// <param name="method">Method</param>
        /// <param name="parameter">Parameter</param>
        /// <param name="il">ILGenerator</param>
        internal void BeginBlock(Mono.Cecil.TypeDefinition typeBuilder, Mono.Cecil.MethodDefinition method, Mono.Cecil.ParameterDefinition parameter, Mono.Cecil.Cil.ILProcessor il)
        {
            var meth = method;

            lockWasTokenLocal = il.DeclareLocal(typeof(bool));
            tempLocal         = il.DeclareLocal(typeof(object));

            il.Emit(method.IsStatic ? Mono.Cecil.Cil.OpCodes.Ldnull : Mono.Cecil.Cil.OpCodes.Ldarg_0);
            il.Emit(Mono.Cecil.Cil.OpCodes.Call, GetLockObjectMethod);
            il.Emit(Mono.Cecil.Cil.OpCodes.Stloc, tempLocal);

            il.BeginExceptionBlock();
            il.Emit(Mono.Cecil.Cil.OpCodes.Ldloc, tempLocal);
            il.Emit(Mono.Cecil.Cil.OpCodes.Ldloca, lockWasTokenLocal);
            il.Emit(Mono.Cecil.Cil.OpCodes.Call, EnterMethod);
        }
Beispiel #8
0
        private AnalysisNet.IInstruction ProcessLoadLocal(Cecil.Cil.Instruction op)
        {
            AnalysisNetBytecode.LoadOperation operation = OperationHelper.ToLoadOperation(op.OpCode.Code);

            AnalysisNetTac.Values.IVariable source = null;
            int localIdx = -1;

            switch (op.OpCode.Code)
            {
            case Mono.Cecil.Cil.Code.Ldloc_0: localIdx = 0; break;

            case Mono.Cecil.Cil.Code.Ldloc_1: localIdx = 1; break;

            case Mono.Cecil.Cil.Code.Ldloc_2: localIdx = 2; break;

            case Mono.Cecil.Cil.Code.Ldloc_3: localIdx = 3; break;

            case Mono.Cecil.Cil.Code.Ldloc_S:
            case Mono.Cecil.Cil.Code.Ldloca_S:
            case Mono.Cecil.Cil.Code.Ldloc:
            case Mono.Cecil.Cil.Code.Ldloca:
                Cecil.Cil.VariableDefinition varDef = (Cecil.Cil.VariableDefinition)op.Operand;
                source = locals[varDef.Index]; break;

            default:
                throw new NotImplementedException();
            }

            if (localIdx > -1)
            {
                source = locals[localIdx];
            }

            AnalysisNetBytecode.LoadInstruction instruction = new AnalysisNetBytecode.LoadInstruction((uint)op.Offset, operation, source);
            return(instruction);
        }
Beispiel #9
0
        unsafe void InitToken(ref OpCode code, object token, Dictionary <Mono.Cecil.Cil.Instruction, int> addr)
        {
            switch (code.Code)
            {
            case OpCodeEnum.Leave:
            case OpCodeEnum.Leave_S:
            case OpCodeEnum.Br:
            case OpCodeEnum.Br_S:
            case OpCodeEnum.Brtrue:
            case OpCodeEnum.Brtrue_S:
            case OpCodeEnum.Brfalse:
            case OpCodeEnum.Brfalse_S:
            //比较流程控制
            case OpCodeEnum.Beq:
            case OpCodeEnum.Beq_S:
            case OpCodeEnum.Bne_Un:
            case OpCodeEnum.Bne_Un_S:
            case OpCodeEnum.Bge:
            case OpCodeEnum.Bge_S:
            case OpCodeEnum.Bge_Un:
            case OpCodeEnum.Bge_Un_S:
            case OpCodeEnum.Bgt:
            case OpCodeEnum.Bgt_S:
            case OpCodeEnum.Bgt_Un:
            case OpCodeEnum.Bgt_Un_S:
            case OpCodeEnum.Ble:
            case OpCodeEnum.Ble_S:
            case OpCodeEnum.Ble_Un:
            case OpCodeEnum.Ble_Un_S:
            case OpCodeEnum.Blt:
            case OpCodeEnum.Blt_S:
            case OpCodeEnum.Blt_Un:
            case OpCodeEnum.Blt_Un_S:
                code.TokenInteger = addr[(Mono.Cecil.Cil.Instruction)token];
                break;

            case OpCodeEnum.Ldc_I4:
                code.TokenInteger = (int)token;
                break;

            case OpCodeEnum.Ldc_I4_S:
                code.TokenInteger = (sbyte)token;
                break;

            case OpCodeEnum.Ldc_I8:
                code.TokenLong = (long)token;
                break;

            case OpCodeEnum.Ldc_R4:
            {
                float val = (float)token;
                code.TokenInteger = *(int *)&val;
            }
            break;

            case OpCodeEnum.Ldc_R8:
            {
                double val = (double)token;
                code.TokenLong = *(long *)&val;
            }
            break;

            case OpCodeEnum.Stloc:
            case OpCodeEnum.Stloc_S:
            case OpCodeEnum.Ldloc:
            case OpCodeEnum.Ldloc_S:
            case OpCodeEnum.Ldloca:
            case OpCodeEnum.Ldloca_S:
            {
                Mono.Cecil.Cil.VariableDefinition vd = (Mono.Cecil.Cil.VariableDefinition)token;
                code.TokenInteger = vd.Index;
            }
            break;

            case OpCodeEnum.Ldarg_S:
            case OpCodeEnum.Ldarg:
            case OpCodeEnum.Ldarga:
            case OpCodeEnum.Ldarga_S:
            case OpCodeEnum.Starg:
            case OpCodeEnum.Starg_S:
            {
                Mono.Cecil.ParameterDefinition vd = (Mono.Cecil.ParameterDefinition)token;
                code.TokenInteger = vd.Index;
                if (HasThis)
                {
                    code.TokenInteger++;
                }
            }
            break;

            case OpCodeEnum.Call:
            case OpCodeEnum.Newobj:
            case OpCodeEnum.Ldftn:
            case OpCodeEnum.Ldvirtftn:
            case OpCodeEnum.Callvirt:
            {
                bool invalidToken;
                var  m = appdomain.GetMethod(token, declaringType, this, out invalidToken);
                if (m != null)
                {
                    if (invalidToken)
                    {
                        code.TokenInteger = m.GetHashCode();
                    }
                    else
                    {
                        code.TokenInteger = token.GetHashCode();
                    }
                }
                else
                {
                    //Cannot find method or the method is dummy
                    MethodReference _ref     = (MethodReference)token;
                    int             paramCnt = _ref.HasParameters ? _ref.Parameters.Count : 0;
                    if (_ref.HasThis)
                    {
                        paramCnt++;
                    }
                    code.TokenLong = paramCnt;
                }
            }
            break;

            case OpCodeEnum.Constrained:
            case OpCodeEnum.Box:
            case OpCodeEnum.Unbox_Any:
            case OpCodeEnum.Unbox:
            case OpCodeEnum.Initobj:
            case OpCodeEnum.Isinst:
            case OpCodeEnum.Newarr:
            case OpCodeEnum.Stobj:
            case OpCodeEnum.Ldobj:
            {
                code.TokenInteger = GetTypeTokenHashCode(token);
            }
            break;

            case OpCodeEnum.Stfld:
            case OpCodeEnum.Ldfld:
            case OpCodeEnum.Ldflda:
            {
                code.TokenLong = appdomain.GetStaticFieldIndex(token, declaringType, this);
            }
            break;

            case OpCodeEnum.Stsfld:
            case OpCodeEnum.Ldsfld:
            case OpCodeEnum.Ldsflda:
            {
                code.TokenLong = appdomain.GetStaticFieldIndex(token, declaringType, this);
            }
            break;

            case OpCodeEnum.Ldstr:
            {
                long hashCode = appdomain.CacheString(token);
                code.TokenLong = hashCode;
            }
            break;

            case OpCodeEnum.Ldtoken:
            {
                if (token is FieldReference)
                {
                    code.TokenInteger = 0;
                    code.TokenLong    = appdomain.GetStaticFieldIndex(token, declaringType, this);
                }
                else if (token is TypeReference)
                {
                    code.TokenInteger = 1;
                    code.TokenLong    = GetTypeTokenHashCode(token);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            break;

            case OpCodeEnum.Switch:
            {
                PrepareJumpTable(token, addr);
                code.TokenInteger = token.GetHashCode();
            }
            break;
            }
        }
Beispiel #10
0
 void EmitInstruction(ILGenerator il, OpCode opcode, Cecil.Cil.VariableDefinition operand)
 {
     il.Emit(opcode, operand.Index);
 }
Beispiel #11
0
        protected override void Visit(Mono.Cecil.Cil.VariableDefinition variableDefinition, Context context)
        {
            Touch(variableDefinition);

            base.Visit(variableDefinition, context);
        }
 protected override bool ShouldInlineAggressively(Mono.Cecil.Cil.VariableDefinition variable)
 {
     return(true);
 }