Example #1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="module">Module that will own the method body</param>
        /// <param name="obj">This can be one of several supported types: the delegate instance
        /// created by DynamicMethod.CreateDelegate(), a DynamicMethod instance, a RTDynamicMethod
        /// instance or a DynamicResolver instance.</param>
        /// <param name="gpContext">Generic parameter context</param>
        public DynamicMethodBodyReader(ModuleDef module, object obj, GenericParamContext gpContext)
        {
            this.module = module;
            this.importer = new Importer(module, ImporterOptions.TryToUseDefs, gpContext);
            this.gpContext = gpContext;
            this.methodName = null;

            if (obj == null)
                throw new ArgumentNullException("obj");

            var del = obj as Delegate;
            if (del != null) {
                obj = del.Method;
                if (obj == null)
                    throw new Exception("Delegate.Method == null");
            }

            if (obj.GetType().ToString() == "System.Reflection.Emit.DynamicMethod+RTDynamicMethod") {
                obj = rtdmOwnerFieldInfo.Read(obj) as DynamicMethod;
                if (obj == null)
                    throw new Exception("RTDynamicMethod.m_owner is null or invalid");
            }

            if (obj is DynamicMethod) {
                methodName = ((DynamicMethod)obj).Name;
                obj = dmResolverFieldInfo.Read(obj);
                if (obj == null)
                    throw new Exception("No resolver found");
            }

            if (obj.GetType().ToString() != "System.Reflection.Emit.DynamicResolver")
                throw new Exception("Couldn't find DynamicResolver");

            var code = rslvCodeFieldInfo.Read(obj) as byte[];
            if (code == null)
                throw new Exception("No code");
            codeSize = code.Length;
            var delMethod = rslvMethodFieldInfo.Read(obj) as SR.MethodBase;
            if (delMethod == null)
                throw new Exception("No method");
            maxStack = (int)rslvMaxStackFieldInfo.Read(obj);

            var scope = rslvDynamicScopeFieldInfo.Read(obj);
            if (scope == null)
                throw new Exception("No scope");
            var tokensList = scopeTokensFieldInfo.Read(scope) as System.Collections.IList;
            if (tokensList == null)
                throw new Exception("No tokens");
            tokens = new List<object>(tokensList.Count);
            for (int i = 0; i < tokensList.Count; i++)
                tokens.Add(tokensList[i]);

            ehInfos = (IList<object>)rslvExceptionsFieldInfo.Read(obj);
            ehHeader = rslvExceptionHeaderFieldInfo.Read(obj) as byte[];

            UpdateLocals(rslvLocalsFieldInfo.Read(obj) as byte[]);
            this.reader = MemoryImageStream.Create(code);
            this.method = CreateMethodDef(delMethod);
            this.parameters = this.method.Parameters;
        }
Example #2
0
 /// <summary>
 /// Creates a CIL method body or returns an empty one if <paramref name="reader"/> doesn't
 /// point to the start of a valid CIL method body.
 /// </summary>
 /// <param name="opResolver">The operand resolver</param>
 /// <param name="reader">A reader positioned at the start of a .NET method body</param>
 /// <param name="method">Use parameters from this method</param>
 /// <param name="gpContext">Generic parameter context</param>
 public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader reader, MethodDef method, GenericParamContext gpContext)
 {
     return(CreateCilBody(opResolver, reader, null, method.Parameters, gpContext));
 }
Example #3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="opResolver">The operand resolver</param>
 /// <param name="reader">A reader positioned at the start of a .NET method body</param>
 /// <param name="parameters">Method parameters</param>
 /// <param name="gpContext">Generic parameter context</param>
 public MethodBodyReader(IInstructionOperandResolver opResolver, IBinaryReader reader, IList <Parameter> parameters, GenericParamContext gpContext)
     : this(opResolver, reader, null, parameters, gpContext)
 {
 }
Example #4
0
        /// <summary>
        /// Creates a CIL method body or returns an empty one if <paramref name="codeReader"/> doesn't
        /// point to the start of a valid CIL method body.
        /// </summary>
        /// <param name="opResolver">The operand resolver</param>
        /// <param name="codeReader">A reader positioned at the start of a .NET method body</param>
        /// <param name="ehReader">Exception handler reader or <c>null</c> if exceptions aren't
        /// present or if <paramref name="codeReader"/> contains the exception handlers</param>
        /// <param name="parameters">Method parameters</param>
        /// <param name="gpContext">Generic parameter context</param>
        public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader codeReader, IBinaryReader ehReader, IList <Parameter> parameters, GenericParamContext gpContext)
        {
            var mbReader = new MethodBodyReader(opResolver, codeReader, ehReader, parameters, gpContext);

            if (!mbReader.Read())
            {
                return(new CilBody());
            }
            return(mbReader.CreateCilBody());
        }
Example #5
0
        static Instruction neg_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            switch (reader.ReadByte())
            {
            case 0: return(OpCodes.Neg.ToInstruction());

            case 1: return(OpCodes.Not.ToInstruction());

            default: throw new ApplicationException("Invalid opcode");
            }
        }
Example #6
0
        static Instruction switch_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            int numTargets = reader.ReadInt32();

            int[] targetDispls = new int[numTargets];
            for (int i = 0; i < targetDispls.Length; i++)
            {
                targetDispls[i] = reader.ReadInt32();
            }
            return(new Instruction(OpCodes.Switch, new SwitchTargetDisplOperand(targetDispls)));
        }
Example #7
0
 PortablePdbCustomDebugInfoReader(ModuleDef module, TypeDef typeOpt, CilBody bodyOpt, GenericParamContext gpContext, ref DataReader reader)
 {
     this.module    = module;
     this.typeOpt   = typeOpt;
     this.bodyOpt   = bodyOpt;
     this.gpContext = gpContext;
     this.reader    = reader;
 }
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="reader">A reader positioned at the start of a .NET method body</param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="gpContext">Generic parameter context</param>
		public MethodBodyReader(IInstructionOperandResolver opResolver, IBinaryReader reader, IList<Parameter> parameters, GenericParamContext gpContext)
			: this(opResolver, reader, null, parameters, gpContext) {
		}
Example #9
0
        static Instruction convert_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            byte type   = reader.ReadByte();
            bool second = reader.ReadBoolean();
            bool third  = reader.ReadBoolean();

            Instruction instr = null;

            foreach (var info in instructionInfos1)
            {
                if (type != info.Type || info.Second != second || info.Third != third)
                {
                    continue;
                }

                instr = new Instruction(info.OpCode);
                break;
            }
            if (instr == null)
            {
                throw new ApplicationException("Invalid opcode");
            }

            return(instr);
        }
Example #10
0
        static Instruction compare_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            int         type  = reader.ReadByte();
            Instruction instr = new Instruction();

            switch (type)
            {
            case 0: instr.OpCode = OpCodes.Br; break;

            case 1: instr.OpCode = OpCodes.Brtrue; break;

            case 2: instr.OpCode = OpCodes.Brfalse; break;

            case 3: instr.OpCode = OpCodes.Beq; break;

            case 4: instr.OpCode = OpCodes.Bge; break;

            case 5: instr.OpCode = OpCodes.Bgt; break;

            case 6: instr.OpCode = OpCodes.Ble; break;

            case 7: instr.OpCode = OpCodes.Blt; break;

            case 8: instr.OpCode = OpCodes.Bne_Un; break;

            case 9: instr.OpCode = OpCodes.Bge_Un; break;

            case 10: instr.OpCode = OpCodes.Bgt_Un; break;

            case 11: instr.OpCode = OpCodes.Ble_Un; break;

            case 12: instr.OpCode = OpCodes.Blt_Un; break;

            case 13: instr.OpCode = OpCodes.Ceq; break;

            case 14: instr.OpCode = OpCodes.Cgt; break;

            case 15: instr.OpCode = OpCodes.Clt; break;

            case 16: instr.OpCode = OpCodes.Cgt_Un; break;

            case 17: instr.OpCode = OpCodes.Clt_Un; break;

            default: throw new ApplicationException("Invalid opcode");
            }
            if (type < 13)
            {
                instr.Operand = new TargetDisplOperand(reader.ReadInt32());
            }

            return(instr);
        }
Example #11
0
        static Instruction cast_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            var instr = new Instruction();

            switch (reader.ReadByte())
            {
            case 0: instr.OpCode = OpCodes.Castclass; break;

            case 1: instr.OpCode = OpCodes.Isinst; break;

            default: throw new ApplicationException("Invalid opcode");
            }
            instr.Operand = resolver.ResolveToken(reader.ReadUInt32(), gpContext);
            return(instr);
        }
Example #12
0
        static Instruction arithmetic_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            switch (reader.ReadByte())
            {
            case 0: return(OpCodes.Add.ToInstruction());

            case 1: return(OpCodes.Add_Ovf.ToInstruction());

            case 2: return(OpCodes.Add_Ovf_Un.ToInstruction());

            case 3: return(OpCodes.Sub.ToInstruction());

            case 4: return(OpCodes.Sub_Ovf.ToInstruction());

            case 5: return(OpCodes.Sub_Ovf_Un.ToInstruction());

            case 6: return(OpCodes.Mul.ToInstruction());

            case 7: return(OpCodes.Mul_Ovf.ToInstruction());

            case 8: return(OpCodes.Mul_Ovf_Un.ToInstruction());

            case 9: return(OpCodes.Div.ToInstruction());

            case 10: return(OpCodes.Div_Un.ToInstruction());

            case 11: return(OpCodes.Rem.ToInstruction());

            case 12: return(OpCodes.Rem_Un.ToInstruction());

            default: throw new ApplicationException("Invalid opcode");
            }
        }
Example #13
0
 protected override void InitializeCustomAttributes()
 {
     readerModule.InitCustomAttributes(this, ref customAttributes, GenericParamContext.Create(ownerType));
 }
Example #14
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="module">Module that will own the method body</param>
        /// <param name="obj">This can be one of several supported types: the delegate instance
        /// created by DynamicMethod.CreateDelegate(), a DynamicMethod instance, a RTDynamicMethod
        /// instance or a DynamicResolver instance.</param>
        /// <param name="gpContext">Generic parameter context</param>
        public SuperDynamicReader(ModuleDef module, object obj, GenericParamContext gpContext)
        {
            this.module    = module;
            this.importer  = new Importer(module, ImporterOptions.TryToUseDefs, gpContext);
            this.gpContext = gpContext;

            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }

            var del = obj as Delegate;

            if (del != null)
            {
                obj = del.Method;
                if (obj == null)
                {
                    throw new Exception("Delegate.Method == null");
                }
            }

            if (obj.GetType().ToString() == "System.Reflection.Emit.DynamicMethod+RTDynamicMethod")
            {
                obj = rtdmOwnerFieldInfo.Read(obj) as DynamicMethod;
                if (obj == null)
                {
                    throw new Exception("RTDynamicMethod.m_owner is null or invalid");
                }
            }

            if (obj is DynamicMethod)
            {
                object obj2 = obj;
                obj = dmResolverFieldInfo.Read(obj);
                if (obj == null)
                {
                    // could be compiled from dynamic info instead of raw shit
                    obj = obj2;
                    obj = methodDynamicInfo.Read(obj);
                    if (obj == null)
                    {
                        throw new Exception("No resolver found");
                    }

                    SecondOption(obj);
                    return;
                }
            }

            if (obj.GetType().ToString() != "System.Reflection.Emit.DynamicResolver")
            {
                throw new Exception("Couldn't find DynamicResolver");
            }

            var code = rslvCodeFieldInfo.Read(obj) as byte[];

            if (code == null)
            {
                throw new Exception("No code");
            }
            codeSize = code.Length;
            var delMethod = rslvMethodFieldInfo.Read(obj) as SR.MethodBase;

            if (delMethod == null)
            {
                throw new Exception("No method");
            }
            maxStack = (int)rslvMaxStackFieldInfo.Read(obj);

            var scope = rslvDynamicScopeFieldInfo.Read(obj);

            if (scope == null)
            {
                throw new Exception("No scope");
            }
            var tokensList = scopeTokensFieldInfo.Read(scope) as System.Collections.IList;

            if (tokensList == null)
            {
                throw new Exception("No tokens");
            }
            tokens = new List <object>(tokensList.Count);
            for (int i = 0; i < tokensList.Count; i++)
            {
                tokens.Add(tokensList[i]);
            }

            ehInfos  = (IList <object>)rslvExceptionsFieldInfo.Read(obj);
            ehHeader = rslvExceptionHeaderFieldInfo.Read(obj) as byte[];

            UpdateLocals(rslvLocalsFieldInfo.Read(obj) as byte[]);
            this.reader     = MemoryImageStream.Create(code);
            this.method     = CreateMethodDef(delMethod);
            this.parameters = this.method.Parameters;
        }
Example #15
0
 ITypeDefOrRef ISignatureReaderHelper.ResolveTypeDefOrRef(uint codedToken, GenericParamContext gpContext)
 {
     uint token;
     if (!CodedToken.TypeDefOrRef.Decode(codedToken, out token))
         return null;
     uint rid = MDToken.ToRID(token);
     switch (MDToken.ToTable(token)) {
     case Table.TypeDef:
     case Table.TypeRef:
     case Table.TypeSpec:
         return ImportType(rid);
     }
     return null;
 }
		/// <summary>
		/// Creates a CIL method body or returns an empty one if <paramref name="codeReader"/> doesn't
		/// point to the start of a valid CIL method body.
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="codeReader">A reader positioned at the start of a .NET method body</param>
		/// <param name="ehReader">Exception handler reader or <c>null</c> if exceptions aren't
		/// present or if <paramref name="codeReader"/> contains the exception handlers</param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="gpContext">Generic parameter context</param>
		public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader codeReader, IBinaryReader ehReader, IList<Parameter> parameters, GenericParamContext gpContext) {
			var mbReader = new MethodBodyReader(opResolver, codeReader, ehReader, parameters, gpContext);
			if (!mbReader.Read())
				return new CilBody();
			return mbReader.CreateCilBody();
		}
Example #17
0
        static Instruction ldelem_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            Instruction instr  = null;
            bool        first  = reader.ReadBoolean();
            bool        second = reader.ReadBoolean();
            int         value  = reader.ReadInt32();

            foreach (var info in instructionInfos2)
            {
                if (info.First != first || info.Second != second)
                {
                    continue;
                }
                if (second && value != info.Value)
                {
                    continue;
                }

                if (second)
                {
                    instr = new Instruction(info.OpCode);
                }
                else
                {
                    instr = new Instruction(info.OpCode, resolver.ResolveToken((uint)value, gpContext));
                }
                break;
            }
            if (instr == null)
            {
                throw new ApplicationException("Invalid opcode");
            }

            return(instr);
        }
		/// <summary>
		/// Creates a CIL method body or returns an empty one if <paramref name="reader"/> doesn't
		/// point to the start of a valid CIL method body.
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="reader">A reader positioned at the start of a .NET method body</param>
		/// <param name="method">Use parameters from this method</param>
		/// <param name="gpContext">Generic parameter context</param>
		public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader reader, MethodDef method, GenericParamContext gpContext) {
			return CreateCilBody(opResolver, reader, null, method.Parameters, gpContext);
		}
Example #19
0
 static Instruction endfinally_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     return(OpCodes.Endfinally.ToInstruction());
 }
Example #20
0
 static Instruction stobj_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     return(new Instruction(OpCodes.Stobj, null));
 }
Example #21
0
        static Instruction ldfld_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            byte b     = reader.ReadByte();
            var  field = resolver.ResolveToken(reader.ReadUInt32(), gpContext) as IField;

            switch (b)
            {
            case 0: return(new Instruction(null, new FieldInstructionOperand(OpCodes.Ldsfld, OpCodes.Ldfld, field)));

            case 1: return(new Instruction(null, new FieldInstructionOperand(OpCodes.Ldsflda, OpCodes.Ldflda, field)));

            case 2: return(new Instruction(null, new FieldInstructionOperand(OpCodes.Stsfld, OpCodes.Stfld, field)));

            default: throw new ApplicationException("Invalid opcode");
            }
        }
Example #22
0
 static Instruction throw_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     return(OpCodes.Throw.ToInstruction());
 }
Example #23
0
        static Instruction ldloca_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            Instruction instr = new Instruction();

            if (reader.ReadBoolean())
            {
                instr.OpCode  = OpCodes.Ldarga;
                instr.Operand = new ArgOperand(reader.ReadUInt16());
            }
            else
            {
                instr.OpCode  = OpCodes.Ldloca;
                instr.Operand = new LocalOperand(reader.ReadUInt16());
            }

            return(instr);
        }
Example #24
0
 /// <summary>
 /// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not
 /// a valid CIL method body.
 /// </summary>
 /// <param name="opResolver">The operand resolver</param>
 /// <param name="code">All code</param>
 /// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in
 /// <paramref name="code"/></param>
 /// <param name="parameters">Method parameters</param>
 /// <param name="gpContext">Generic parameter context</param>
 public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList <Parameter> parameters, GenericParamContext gpContext)
 {
     return(CreateCilBody(opResolver, MemoryImageStream.Create(code), exceptions == null ? null : MemoryImageStream.Create(exceptions), parameters, gpContext));
 }
Example #25
0
 static Instruction ldstr_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     return(OpCodes.Ldstr.ToInstruction(reader.ReadString()));
 }
Example #26
0
        /// <summary>
        /// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not
        /// a valid CIL method body.
        /// </summary>
        /// <param name="opResolver">The operand resolver</param>
        /// <param name="code">All code</param>
        /// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in
        /// <paramref name="code"/></param>
        /// <param name="parameters">Method parameters</param>
        /// <param name="flags">Method header flags, eg. 2 if tiny method</param>
        /// <param name="maxStack">Max stack</param>
        /// <param name="codeSize">Code size</param>
        /// <param name="localVarSigTok">Local variable signature token or 0 if none</param>
        /// <param name="gpContext">Generic parameter context</param>
        public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList <Parameter> parameters, ushort flags, ushort maxStack, uint codeSize, uint localVarSigTok, GenericParamContext gpContext)
        {
            var codeReader = MemoryImageStream.Create(code);
            var ehReader   = exceptions == null ? null : MemoryImageStream.Create(exceptions);
            var mbReader   = new MethodBodyReader(opResolver, codeReader, ehReader, parameters, gpContext);

            mbReader.SetHeader(flags, maxStack, codeSize, localVarSigTok);
            if (!mbReader.Read())
            {
                return(new CilBody());
            }
            return(mbReader.CreateCilBody());
        }
Example #27
0
 static Instruction ldtoken_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     return(new Instruction(OpCodes.Ldtoken, resolver.ResolveToken(reader.ReadUInt32(), gpContext)));
 }
Example #28
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="opResolver">The operand resolver</param>
 /// <param name="codeReader">A reader positioned at the start of a .NET method body</param>
 /// <param name="ehReader">Exception handler reader or <c>null</c> if exceptions aren't
 /// present or if <paramref name="codeReader"/> contains the exception handlers</param>
 /// <param name="parameters">Method parameters</param>
 /// <param name="gpContext">Generic parameter context</param>
 public MethodBodyReader(IInstructionOperandResolver opResolver, IBinaryReader codeReader, IBinaryReader ehReader, IList <Parameter> parameters, GenericParamContext gpContext)
     : base(codeReader, parameters)
 {
     this.opResolver       = opResolver;
     this.exceptionsReader = ehReader;
     this.gpContext        = gpContext;
 }
Example #29
0
        static Instruction leave_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            int displacement = reader.ReadInt32();

            return(new Instruction(OpCodes.Leave, new TargetDisplOperand(displacement)));
        }
Example #30
0
 /// <summary>
 /// Creates a CIL method body or returns an empty one if <paramref name="reader"/> doesn't
 /// point to the start of a valid CIL method body.
 /// </summary>
 /// <param name="opResolver">The operand resolver</param>
 /// <param name="reader">A reader positioned at the start of a .NET method body</param>
 /// <param name="parameters">Method parameters</param>
 /// <param name="gpContext">Generic parameter context</param>
 public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader reader, IList <Parameter> parameters, GenericParamContext gpContext)
 {
     return(CreateCilBody(opResolver, reader, null, parameters, gpContext));
 }
Example #31
0
        static Instruction ldc_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            switch ((ElementType)reader.ReadByte())
            {
            case ElementType.I4: return(Instruction.CreateLdcI4(reader.ReadInt32()));

            case ElementType.I8: return(OpCodes.Ldc_I8.ToInstruction(reader.ReadInt64()));

            case ElementType.R4: return(OpCodes.Ldc_R4.ToInstruction(reader.ReadSingle()));

            case ElementType.R8: return(OpCodes.Ldc_R8.ToInstruction(reader.ReadDouble()));

            case ElementType.Object: return(OpCodes.Ldnull.ToInstruction());

            default: throw new ApplicationException("Invalid opcode");
            }
        }
		/// <summary>
		/// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not
		/// a valid CIL method body.
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="code">All code</param>
		/// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in
		/// <paramref name="code"/></param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="gpContext">Generic parameter context</param>
		public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList<Parameter> parameters, GenericParamContext gpContext) {
			return CreateCilBody(opResolver, MemoryImageStream.Create(code), exceptions == null ? null : MemoryImageStream.Create(exceptions), parameters, gpContext);
		}
Example #33
0
        static Instruction ldftn_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            byte code  = reader.ReadByte();
            uint token = reader.ReadUInt32();

            switch (code)
            {
            case 0:
                return(new Instruction(OpCodes.Ldftn, resolver.ResolveToken(token, gpContext)));

            case 1:
                reader.ReadInt32();                     // token of newobj .ctor
                return(new Instruction(OpCodes.Ldvirtftn, resolver.ResolveToken(token, gpContext)));

            default:
                throw new ApplicationException("Invalid opcode");
            }
        }
		/// <summary>
		/// Creates a CIL method body or returns an empty one if <paramref name="code"/> is not
		/// a valid CIL method body.
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="code">All code</param>
		/// <param name="exceptions">Exceptions or <c>null</c> if all exception handlers are in
		/// <paramref name="code"/></param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="flags">Method header flags, eg. 2 if tiny method</param>
		/// <param name="maxStack">Max stack</param>
		/// <param name="codeSize">Code size</param>
		/// <param name="localVarSigTok">Local variable signature token or 0 if none</param>
		/// <param name="gpContext">Generic parameter context</param>
		public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, byte[] code, byte[] exceptions, IList<Parameter> parameters, ushort flags, ushort maxStack, uint codeSize, uint localVarSigTok, GenericParamContext gpContext) {
			var codeReader = MemoryImageStream.Create(code);
			var ehReader = exceptions == null ? null : MemoryImageStream.Create(exceptions);
			var mbReader = new MethodBodyReader(opResolver, codeReader, ehReader, parameters, gpContext);
			mbReader.SetHeader(flags, maxStack, codeSize, localVarSigTok);
			if (!mbReader.Read())
				return new CilBody();
			return mbReader.CreateCilBody();
		}
Example #35
0
        static Instruction logical_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            switch (reader.ReadByte())
            {
            case 0: return(OpCodes.And.ToInstruction());

            case 1: return(OpCodes.Or.ToInstruction());

            case 2: return(OpCodes.Xor.ToInstruction());

            case 3: return(OpCodes.Shl.ToInstruction());

            case 4: return(OpCodes.Shr.ToInstruction());

            case 5: return(OpCodes.Shr_Un.ToInstruction());

            default: throw new ApplicationException("Invalid opcode");
            }
        }
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="codeReader">A reader positioned at the start of a .NET method body</param>
		/// <param name="ehReader">Exception handler reader or <c>null</c> if exceptions aren't
		/// present or if <paramref name="codeReader"/> contains the exception handlers</param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="gpContext">Generic parameter context</param>
		public MethodBodyReader(IInstructionOperandResolver opResolver, IBinaryReader codeReader, IBinaryReader ehReader, IList<Parameter> parameters, GenericParamContext gpContext)
			: base(codeReader, parameters) {
			this.opResolver = opResolver;
			this.exceptionsReader = ehReader;
			this.gpContext = gpContext;
		}
Example #37
0
 static Instruction ret_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
 {
     reader.ReadInt32();                 // token of current method
     return(OpCodes.Ret.ToInstruction());
 }
		/// <summary>
		/// Creates a CIL method body or returns an empty one if <paramref name="reader"/> doesn't
		/// point to the start of a valid CIL method body.
		/// </summary>
		/// <param name="opResolver">The operand resolver</param>
		/// <param name="reader">A reader positioned at the start of a .NET method body</param>
		/// <param name="parameters">Method parameters</param>
		/// <param name="gpContext">Generic parameter context</param>
		public static CilBody CreateCilBody(IInstructionOperandResolver opResolver, IBinaryReader reader, IList<Parameter> parameters, GenericParamContext gpContext) {
			return CreateCilBody(opResolver, reader, null, parameters, gpContext);
		}
Example #39
0
        static Instruction stloc_read(BinaryReader reader, IInstructionOperandResolver resolver, GenericParamContext gpContext)
        {
            bool   isStarg = reader.ReadBoolean();
            ushort index   = reader.ReadUInt16();

            var instr = new Instruction();

            if (isStarg)
            {
                instr.OpCode  = OpCodes.Starg;
                instr.Operand = new ArgOperand(index);
            }
            else
            {
                instr.OpCode  = OpCodes.Stloc;
                instr.Operand = new LocalOperand(index);
                reader.ReadInt32();                     // ElementType of local
            }

            return(instr);
        }