Inheritance: ByteBuffer
Esempio n. 1
0
		public static MethodBody ReadMethodBody (MethodDefinition method, MetadataReader metadata)
		{
			var reader = new CodeReader (method, metadata);
			reader.body = new MethodBody (method);

			reader.ReadMethodBody ();

			return reader.body;
		}
Esempio n. 2
0
 public ReflectionReader(ModuleDefinition module)
 {
     m_module = module;
     m_reader = m_module.ImageReader;
     m_root = m_module.Image.MetadataRoot;
     m_tHeap = m_root.Streams.TablesHeap;
     m_checkDeleted = (m_tHeap.HeapSizes & 0x80) != 0;
     if (m_reader != null)
         m_tableReader = m_reader.MetadataReader.TableReader;
     m_codeReader = new CodeReader (this);
     m_sigReader = new SignatureReader (m_root, this);
     m_isCorlib = module.Assembly.Name.Name == Constants.Corlib;
 }
Esempio n. 3
0
        public Instruction Create(OpCode opcode, int i)
        {
            if (opcode.OperandType == OperandType.InlineVar)
            {
                return(Create(opcode, m_mbody.Variables [i]));
            }

            if (opcode.OperandType == OperandType.InlineParam)
            {
                return(Create(opcode, CodeReader.GetParameter(m_mbody, i)));
            }

            if (opcode.OperandType != OperandType.InlineI)
            {
                throw new ArgumentException("opcode");
            }

            return(FinalCreate(opcode, i));
        }
Esempio n. 4
0
        public Instruction Create(OpCode opcode, byte b)
        {
            if (opcode.OperandType == OperandType.ShortInlineVar)
            {
                return(Create(opcode, m_mbody.Variables [b]));
            }

            if (opcode.OperandType == OperandType.ShortInlineParam)
            {
                return(Create(opcode, CodeReader.GetParameter(m_mbody, b)));
            }

            if (opcode.OperandType != OperandType.ShortInlineI ||
                opcode == OpCodes.Ldc_I4_S)
            {
                throw new ArgumentException("opcode");
            }

            return(FinalCreate(opcode, b));
        }
Esempio n. 5
0
        public ByteBuffer PatchRawMethodBody(MethodDefinition method, CodeWriter writer, out MethodSymbols symbols)
        {
            MetadataToken zero;
            ByteBuffer    byteBuffer = new ByteBuffer();

            symbols             = new MethodSymbols(method.Name);
            this.method         = method;
            this.reader.context = method;
            this.MoveTo(method.RVA);
            byte num  = base.ReadByte();
            int  num1 = num & 3;

            if (num1 == 2)
            {
                byteBuffer.WriteByte(num);
                zero = MetadataToken.Zero;
                symbols.code_size = num >> 2;
                this.PatchRawCode(byteBuffer, symbols.code_size, writer);
            }
            else
            {
                if (num1 != 3)
                {
                    throw new NotSupportedException();
                }
                this.position--;
                this.PatchRawFatMethod(byteBuffer, symbols, writer, out zero);
            }
            ISymbolReader symbolReader = this.reader.module.symbol_reader;

            if (symbolReader != null && writer.metadata.write_symbols)
            {
                symbols.method_token    = CodeReader.GetOriginalToken(writer.metadata, method);
                symbols.local_var_token = zero;
                symbolReader.Read(symbols);
            }
            return(byteBuffer);
        }
Esempio n. 6
0
        public static void FromBytes(MethodDefinition method, byte[] bytes)
        {
            CodeReader cr = new CodeReader(method, bytes);

            method.Body = cr.ReadMethodBody(method);
        }
        public void Simplify()
        {
            foreach (Instruction i in this.Instructions)
            {
                if (i.OpCode.OpCodeType != OpCodeType.Macro)
                {
                    continue;
                }

                switch (i.OpCode.Code)
                {
                case Code.Ldarg_0:
                    Modify(i, OpCodes.Ldarg,
                           CodeReader.GetParameter(this, 0));
                    break;

                case Code.Ldarg_1:
                    Modify(i, OpCodes.Ldarg,
                           CodeReader.GetParameter(this, 1));
                    break;

                case Code.Ldarg_2:
                    Modify(i, OpCodes.Ldarg,
                           CodeReader.GetParameter(this, 2));
                    break;

                case Code.Ldarg_3:
                    Modify(i, OpCodes.Ldarg,
                           CodeReader.GetParameter(this, 3));
                    break;

                case Code.Ldloc_0:
                    Modify(i, OpCodes.Ldloc,
                           CodeReader.GetVariable(this, 0));
                    break;

                case Code.Ldloc_1:
                    Modify(i, OpCodes.Ldloc,
                           CodeReader.GetVariable(this, 1));
                    break;

                case Code.Ldloc_2:
                    Modify(i, OpCodes.Ldloc,
                           CodeReader.GetVariable(this, 2));
                    break;

                case Code.Ldloc_3:
                    Modify(i, OpCodes.Ldloc,
                           CodeReader.GetVariable(this, 3));
                    break;

                case Code.Stloc_0:
                    Modify(i, OpCodes.Stloc,
                           CodeReader.GetVariable(this, 0));
                    break;

                case Code.Stloc_1:
                    Modify(i, OpCodes.Stloc,
                           CodeReader.GetVariable(this, 1));
                    break;

                case Code.Stloc_2:
                    Modify(i, OpCodes.Stloc,
                           CodeReader.GetVariable(this, 2));
                    break;

                case Code.Stloc_3:
                    Modify(i, OpCodes.Stloc,
                           CodeReader.GetVariable(this, 3));
                    break;

                case Code.Ldarg_S:
                    i.OpCode = OpCodes.Ldarg;
                    break;

                case Code.Ldarga_S:
                    i.OpCode = OpCodes.Ldarga;
                    break;

                case Code.Starg_S:
                    i.OpCode = OpCodes.Starg;
                    break;

                case Code.Ldloc_S:
                    i.OpCode = OpCodes.Ldloc;
                    break;

                case Code.Ldloca_S:
                    i.OpCode = OpCodes.Ldloca;
                    break;

                case Code.Stloc_S:
                    i.OpCode = OpCodes.Stloc;
                    break;

                case Code.Ldc_I4_M1:
                    Modify(i, OpCodes.Ldc_I4, -1);
                    break;

                case Code.Ldc_I4_0:
                    Modify(i, OpCodes.Ldc_I4, 0);
                    break;

                case Code.Ldc_I4_1:
                    Modify(i, OpCodes.Ldc_I4, 1);
                    break;

                case Code.Ldc_I4_2:
                    Modify(i, OpCodes.Ldc_I4, 2);
                    break;

                case Code.Ldc_I4_3:
                    Modify(i, OpCodes.Ldc_I4, 3);
                    break;

                case Code.Ldc_I4_4:
                    Modify(i, OpCodes.Ldc_I4, 4);
                    break;

                case Code.Ldc_I4_5:
                    Modify(i, OpCodes.Ldc_I4, 5);
                    break;

                case Code.Ldc_I4_6:
                    Modify(i, OpCodes.Ldc_I4, 6);
                    break;

                case Code.Ldc_I4_7:
                    Modify(i, OpCodes.Ldc_I4, 7);
                    break;

                case Code.Ldc_I4_8:
                    Modify(i, OpCodes.Ldc_I4, 8);
                    break;

                case Code.Ldc_I4_S:
                    i.OpCode  = OpCodes.Ldc_I4;
                    i.Operand = (int)(sbyte)i.Operand;
                    break;

                case Code.Br_S:
                    i.OpCode = OpCodes.Br;
                    break;

                case Code.Brfalse_S:
                    i.OpCode = OpCodes.Brfalse;
                    break;

                case Code.Brtrue_S:
                    i.OpCode = OpCodes.Brtrue;
                    break;

                case Code.Beq_S:
                    i.OpCode = OpCodes.Beq;
                    break;

                case Code.Bge_S:
                    i.OpCode = OpCodes.Bge;
                    break;

                case Code.Bgt_S:
                    i.OpCode = OpCodes.Bgt;
                    break;

                case Code.Ble_S:
                    i.OpCode = OpCodes.Ble;
                    break;

                case Code.Blt_S:
                    i.OpCode = OpCodes.Blt;
                    break;

                case Code.Bne_Un_S:
                    i.OpCode = OpCodes.Bne_Un;
                    break;

                case Code.Bge_Un_S:
                    i.OpCode = OpCodes.Bge_Un;
                    break;

                case Code.Bgt_Un_S:
                    i.OpCode = OpCodes.Bgt_Un;
                    break;

                case Code.Ble_Un_S:
                    i.OpCode = OpCodes.Ble_Un;
                    break;

                case Code.Blt_Un_S:
                    i.OpCode = OpCodes.Blt_Un;
                    break;

                case Code.Leave_S:
                    i.OpCode = OpCodes.Leave;
                    break;
                }
            }
        }
        public void Simplify()
        {
            foreach (Instruction i in this.Instructions)
            {
                if (i.OpCode.OpCodeType != OpCodeType.Macro)
                {
                    continue;
                }

                switch (i.OpCode.Code)
                {
                case Code.Ldarg_0:
                    Simplify(i, OpCodes.Ldarg,
                             CodeReader.GetParameter(this, 0));
                    break;

                case Code.Ldarg_1:
                    Simplify(i, OpCodes.Ldarg,
                             CodeReader.GetParameter(this, 1));
                    break;

                case Code.Ldarg_2:
                    Simplify(i, OpCodes.Ldarg,
                             CodeReader.GetParameter(this, 2));
                    break;

                case Code.Ldarg_3:
                    Simplify(i, OpCodes.Ldarg,
                             CodeReader.GetParameter(this, 3));
                    break;

                case Code.Ldloc_0:
                    Simplify(i, OpCodes.Ldloc,
                             CodeReader.GetVariable(this, 0));
                    break;

                case Code.Ldloc_1:
                    Simplify(i, OpCodes.Ldloc,
                             CodeReader.GetVariable(this, 1));
                    break;

                case Code.Ldloc_2:
                    Simplify(i, OpCodes.Ldloc,
                             CodeReader.GetVariable(this, 2));
                    break;

                case Code.Ldloc_3:
                    Simplify(i, OpCodes.Ldloc,
                             CodeReader.GetVariable(this, 3));
                    break;

                case Code.Stloc_0:
                    Simplify(i, OpCodes.Stloc,
                             CodeReader.GetVariable(this, 0));
                    break;

                case Code.Stloc_1:
                    Simplify(i, OpCodes.Stloc,
                             CodeReader.GetVariable(this, 1));
                    break;

                case Code.Stloc_2:
                    Simplify(i, OpCodes.Stloc,
                             CodeReader.GetVariable(this, 2));
                    break;

                case Code.Stloc_3:
                    Simplify(i, OpCodes.Stloc,
                             CodeReader.GetVariable(this, 3));
                    break;

                case Code.Ldarg_S:
                    i.OpCode = OpCodes.Ldarg;
                    break;

                case Code.Ldarga_S:
                    SimplifyFromByte(i, OpCodes.Ldarga);
                    break;

                case Code.Starg_S:
                    i.OpCode = OpCodes.Starg;
                    break;

                case Code.Ldloc_S:
                    i.OpCode = OpCodes.Ldloc;
                    break;

                case Code.Ldloca_S:
                    Simplify(i, OpCodes.Ldloca,
                             CodeReader.GetVariable(this, (sbyte)i.Operand));
                    break;

                case Code.Stloc_S:
                    i.OpCode = OpCodes.Stloc;
                    break;

                case Code.Ldc_I4_M1:
                    Simplify(i, OpCodes.Ldc_I4, -1);
                    break;

                case Code.Ldc_I4_0:
                    Simplify(i, OpCodes.Ldc_I4, 0);
                    break;

                case Code.Ldc_I4_1:
                    Simplify(i, OpCodes.Ldc_I4, 1);
                    break;

                case Code.Ldc_I4_2:
                    Simplify(i, OpCodes.Ldc_I4, 2);
                    break;

                case Code.Ldc_I4_3:
                    Simplify(i, OpCodes.Ldc_I4, 3);
                    break;

                case Code.Ldc_I4_4:
                    Simplify(i, OpCodes.Ldc_I4, 4);
                    break;

                case Code.Ldc_I4_5:
                    Simplify(i, OpCodes.Ldc_I4, 5);
                    break;

                case Code.Ldc_I4_6:
                    Simplify(i, OpCodes.Ldc_I4, 6);
                    break;

                case Code.Ldc_I4_7:
                    Simplify(i, OpCodes.Ldc_I4, 7);
                    break;

                case Code.Ldc_I4_8:
                    Simplify(i, OpCodes.Ldc_I4, 8);
                    break;

                case Code.Ldc_I4_S:
                    SimplifyFromSByte(i, OpCodes.Ldc_I4);
                    break;

                case Code.Br_S:
                    SimplifyFromSByte(i, OpCodes.Br);
                    break;

                case Code.Brfalse_S:
                    SimplifyFromSByte(i, OpCodes.Brfalse);
                    break;

                case Code.Brtrue_S:
                    SimplifyFromSByte(i, OpCodes.Brtrue);
                    break;

                case Code.Beq_S:
                    SimplifyFromSByte(i, OpCodes.Beq);
                    break;

                case Code.Bge_S:
                    SimplifyFromSByte(i, OpCodes.Bge);
                    break;

                case Code.Bgt_S:
                    SimplifyFromSByte(i, OpCodes.Bgt);
                    break;

                case Code.Ble_S:
                    SimplifyFromSByte(i, OpCodes.Ble);
                    break;

                case Code.Blt_S:
                    SimplifyFromSByte(i, OpCodes.Blt);
                    break;

                case Code.Bne_Un_S:
                    SimplifyFromSByte(i, OpCodes.Bne_Un);
                    break;

                case Code.Bge_Un_S:
                    SimplifyFromSByte(i, OpCodes.Bge_Un);
                    break;

                case Code.Bgt_Un_S:
                    SimplifyFromSByte(i, OpCodes.Bgt_Un);
                    break;

                case Code.Ble_Un_S:
                    SimplifyFromSByte(i, OpCodes.Ble_Un);
                    break;

                case Code.Blt_Un_S:
                    SimplifyFromSByte(i, OpCodes.Blt_Un);
                    break;
                }
            }
        }
 public static void FromBytes(MethodDefinition method, byte[] bytes)
 {
     CodeReader cr = new CodeReader(method, bytes);
     method.Body = cr.ReadMethodBody(method);
 }
Esempio n. 10
0
 private Instruction GetInstruction(int offset)
 {
     return(CodeReader.GetInstruction(this.body.Instructions, offset));
 }