private void WriteCustomAttributes(CilMethodDefinition methodDefinition) { foreach (var attribute in methodDefinition.CustomAttributes) { attribute.Accept(this); } }
private void WritePropertyOrEventAccessor(CilMethodDefinition accessor) { int i = 0; StringBuilder genericParameters = new StringBuilder(); foreach (var genericParameter in accessor.GenericParameters) { if (i == 0) { genericParameters.Append('<'); } genericParameters.Append(genericParameter); genericParameters.Append(','); i++; } if (i > 0) { genericParameters.Length -= 1; //Delete trailing , genericParameters.Append('>'); } if (accessor.Signature.Header.IsInstance) { _writer.Write("instance "); } _writer.Write(string.Format("{0} {1}::{2}{3}{4}", accessor.Signature.ReturnType, accessor.DeclaringType.FullName, accessor.Name, genericParameters.ToString(), CilDecoder.DecodeSignatureParamerTypes(accessor.Signature))); _writer.WriteLine(); }
private void WriteLocals(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".locals"); if (methodDefinition.LocalVariablesInitialized) { _writer.Write(" init"); } int i = 0; var locals = methodDefinition.Locals; _writer.Write('('); foreach (var local in locals) { if (i > 0) { WriteIndentation(); _writer.Write(string.Format("{0,13}", "")); } _writer.Write(string.Format("[{0}] ", i)); local.Accept(this); if (i < locals.Length - 1) { _writer.WriteLine(','); } i++; } _writer.WriteLine(')'); }
private void WriteMethodDefinition(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".method "); if (_options.ShowBytes) { _writer.Write(string.Format("/* {0:X8} */ ", methodDefinition.Token)); } _writer.WriteLine(methodDefinition.GetDecodedSignature()); WriteIndentation(); _writer.WriteLine('{'); }
private static string GetVariableName(OpCode opCode, int token, CilMethodDefinition methodDefinition) { if (HasArgument(opCode)) { if (methodDefinition.Signature.Header.IsInstance) { token--; //the first parameter is "this". } return(methodDefinition.GetParameter(token).Name); } return(methodDefinition.GetLocal(token).Name); }
private int WriteMethodBody(CilMethodDefinition methodDefinition, IReadOnlyList <CilExceptionRegion> exceptionRegions, ref int instructionIndex, int ilOffset, int regionIndex, out int nextRegionIndex) { int lastRegionIndex = regionIndex - 1; var instructions = methodDefinition.Instructions; for (; instructionIndex < instructions.Length; instructionIndex++) { var instruction = instructions[instructionIndex]; if (EndFilterRegion(exceptionRegions, lastRegionIndex, ilOffset)) { Unindent(); WriteIndentation(); _writer.WriteLine("} // end filter"); WriteIndentation(); _writer.WriteLine("{ // handler"); Indent(); } if (StartRegion(exceptionRegions, regionIndex, ilOffset)) { var region = exceptionRegions[regionIndex]; WriteIndentation(); _writer.WriteLine(region.ToString(methodDefinition.Provider)); WriteIndentation(); _writer.WriteLine('{'); Indent(); ilOffset = WriteMethodBody(methodDefinition, exceptionRegions, ref instructionIndex, ilOffset, regionIndex + 1, out regionIndex); Unindent(); WriteIndentation(); _writer.Write('}'); _writer.WriteLine(string.Format(" // end {0}", (region.Kind == HandlerKind.Try ? ".try" : "handler"))); } else { WriteIndentation(); _writer.Write(string.Format("IL_{0:x4}:", ilOffset)); _writer.Write(_indent); instruction.Accept(this); ilOffset += instruction.Size; } if (EndRegion(exceptionRegions, lastRegionIndex, ilOffset)) { break; } } nextRegionIndex = regionIndex; return(ilOffset); }
public void Visit(CilMethodDefinition methodDefinition) { WriteMethodDefinition(methodDefinition); Indent(); WriteMethodHeader(methodDefinition); int ilOffset = 0; int instructionIndex = 0; int lastRegionIndex = 0; if (methodDefinition.RelativeVirtualAddress != 0) { WriteMethodBody(methodDefinition, methodDefinition.ExceptionRegions, ref instructionIndex, ilOffset, lastRegionIndex, out lastRegionIndex); } Unindent(); WriteIndentation(); _writer.Write('}'); _writer.WriteLine(string.Format(" // end of method {0}", methodDefinition.Name)); _writer.WriteLine(); }
private void WriteMethodHeader(CilMethodDefinition methodDefinition) { WriteCustomAttributes(methodDefinition); if (methodDefinition.RelativeVirtualAddress == 0) { return; } foreach (var parameter in methodDefinition.GetOptionalParameters()) { WriteIndentation(); _writer.Write(".param "); _writer.WriteLine(string.Format("[{0}] = {1}", parameter.SequenceNumber, parameter.DefaultValue.GetValueString())); } if (methodDefinition.IsImplementation) { WriteOverridenMethod(methodDefinition); } if (methodDefinition.IsEntryPoint) { WriteIndentation(); _writer.WriteLine(".entrypoint"); } WriteIndentation(); _writer.WriteLine(string.Format("// code size {0,8} (0x{0:x})", methodDefinition.Size)); WriteIndentation(); _writer.WriteLine(string.Format(".maxstack {0,2}", methodDefinition.MaxStack)); if (methodDefinition.HasLocals) { WriteLocals(methodDefinition); } }
private void WriteOverridenMethod(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".override "); int token = methodDefinition.MethodDeclarationToken; if (CilDecoder.IsMemberReference(token)) { _writer.Write("method "); _writer.Write(CilDecoder.SolveMethodName(methodDefinition._readers.MdReader, token, methodDefinition.Provider)); if (_options.ShowBytes) { _writer.Write(string.Format(" /* {0:X8} */", token)); } _writer.WriteLine(); return; } _writer.Write(CilDecoder.DecodeOverridenMethodName(methodDefinition._readers.MdReader, token, methodDefinition.Provider)); if (_options.ShowBytes) { _writer.Write(string.Format(" /* {0:X8} */", token)); } _writer.WriteLine(); }
public static IEnumerable<CilInstruction> DecodeMethodBody(CilMethodDefinition methodDefinition) { return DecodeMethodBody(methodDefinition.IlReader, methodDefinition._readers.MdReader, methodDefinition.Provider, methodDefinition); }
private static string GetVariableName(OpCode opCode, int token, CilMethodDefinition methodDefinition) { if (HasArgument(opCode)) { if (methodDefinition.Signature.Header.IsInstance) { token--; //the first parameter is "this". } return methodDefinition.GetParameter(token).Name; } return methodDefinition.GetLocal(token).Name; }
public void Visit(CilMethodDefinition methodDefinition) { WriteMethodDefinition(methodDefinition); Indent(); WriteMethodHeader(methodDefinition); int ilOffset = 0; int instructionIndex = 0; int lastRegionIndex = 0; if (methodDefinition.RelativeVirtualAddress != 0) WriteMethodBody(methodDefinition, methodDefinition.ExceptionRegions, ref instructionIndex, ilOffset, lastRegionIndex, out lastRegionIndex); Unindent(); WriteIndentation(); _writer.Write('}'); _writer.WriteLine(string.Format(" // end of method {0}", methodDefinition.Name)); _writer.WriteLine(); }
internal static IEnumerable <string> DecodeGenericParameters(MethodDefinition methodDefinition, CilMethodDefinition method) { foreach (var handle in methodDefinition.GetGenericParameters()) { var parameter = method._readers.MdReader.GetGenericParameter(handle); yield return(method._readers.MdReader.GetString(parameter.Name)); } }
private static IEnumerable <CilInstruction> DecodeMethodBody(BlobReader ilReader, MetadataReader metadataReader, CilTypeProvider provider, CilMethodDefinition methodDefinition) { ilReader.Reset(); int intOperand; ushort shortOperand; int ilOffset = 0; CilInstruction instruction = null; while (ilReader.Offset < ilReader.Length) { OpCode opCode; int expectedSize; byte _byte = ilReader.ReadByte(); /*If the byte read is 0xfe it means is a two byte instruction, * so since it is going to read the second byte to get the actual * instruction it has to check that the offset is still less than the length.*/ if (_byte == 0xfe && ilReader.Offset < ilReader.Length) { opCode = CilDecoderHelpers.Instance.twoByteOpCodes[ilReader.ReadByte()]; expectedSize = 2; } else { opCode = CilDecoderHelpers.Instance.oneByteOpCodes[_byte]; expectedSize = 1; } switch (opCode.OperandType) { //The instruction size is the expected size (1 or 2 depending if it is a one or two byte instruction) + the size of the operand. case OperandType.InlineField: intOperand = ilReader.ReadInt32(); string fieldInfo = GetFieldInformation(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, fieldInfo, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineString: intOperand = ilReader.ReadInt32(); bool isPrintable; string str = GetArgumentString(metadataReader, intOperand, out isPrintable); instruction = new CilStringInstruction(opCode, str, intOperand, expectedSize + (int)CilInstructionSize.Int32, isPrintable); break; case OperandType.InlineMethod: intOperand = ilReader.ReadInt32(); string methodCall = SolveMethodName(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, methodCall, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineType: intOperand = ilReader.ReadInt32(); string type = GetTypeInformation(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, type, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineTok: intOperand = ilReader.ReadInt32(); string tokenType = GetInlineTokenType(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, tokenType, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineI: instruction = new CilInt32Instruction(opCode, ilReader.ReadInt32(), -1, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineI8: instruction = new CilInt64Instruction(opCode, ilReader.ReadInt64(), -1, expectedSize + (int)CilInstructionSize.Int64); break; case OperandType.InlineR: instruction = new CilDoubleInstruction(opCode, ilReader.ReadDouble(), -1, expectedSize + (int)CilInstructionSize.Double); break; case OperandType.InlineSwitch: instruction = CreateSwitchInstruction(ref ilReader, expectedSize, ilOffset, opCode); break; case OperandType.ShortInlineBrTarget: instruction = new CilInt16BranchInstruction(opCode, ilReader.ReadSByte(), ilOffset, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.InlineBrTarget: instruction = new CilBranchInstruction(opCode, ilReader.ReadInt32(), ilOffset, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.ShortInlineI: instruction = new CilByteInstruction(opCode, ilReader.ReadByte(), -1, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.ShortInlineR: instruction = new CilSingleInstruction(opCode, ilReader.ReadSingle(), -1, expectedSize + (int)CilInstructionSize.Single); break; case OperandType.InlineNone: instruction = new CilInstructionWithNoValue(opCode, expectedSize); break; case OperandType.ShortInlineVar: byte token = ilReader.ReadByte(); instruction = new CilInt16VariableInstruction(opCode, GetVariableName(opCode, token, methodDefinition), token, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.InlineVar: shortOperand = ilReader.ReadUInt16(); instruction = new CilVariableInstruction(opCode, GetVariableName(opCode, shortOperand, methodDefinition), shortOperand, expectedSize + (int)CilInstructionSize.Int16); break; case OperandType.InlineSig: intOperand = ilReader.ReadInt32(); instruction = new CilStringInstruction(opCode, GetSignature(metadataReader, intOperand, provider), intOperand, expectedSize + (int)CilInstructionSize.Int32); break; default: break; } ilOffset += instruction.Size; yield return(instruction); } }
public static IEnumerable <CilInstruction> DecodeMethodBody(CilMethodDefinition methodDefinition) { return(DecodeMethodBody(methodDefinition.IlReader, methodDefinition._readers.MdReader, methodDefinition.Provider, methodDefinition)); }
internal static IEnumerable<string> DecodeGenericParameters(MethodDefinition methodDefinition, CilMethodDefinition method) { int count = method.Signature.GenericParameterCount; foreach (var handle in methodDefinition.GetGenericParameters()) { var parameter = method._readers.MdReader.GetGenericParameter(handle); yield return method._readers.MdReader.GetString(parameter.Name); } }
private void WriteCustomAttributes(CilMethodDefinition methodDefinition) { foreach (var attribute in methodDefinition.CustomAttributes) { attribute.Accept(this); } }
private void WriteOverridenMethod(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".override "); int token = methodDefinition.MethodDeclarationToken; if (CilDecoder.IsMemberReference(token)) { _writer.Write("method "); _writer.Write(CilDecoder.SolveMethodName(methodDefinition._readers.MdReader, token, methodDefinition.Provider)); if (_options.ShowBytes) _writer.Write(string.Format(" /* {0:X8} */", token)); _writer.WriteLine(); return; } _writer.Write(CilDecoder.DecodeOverridenMethodName(methodDefinition._readers.MdReader, token, methodDefinition.Provider)); if (_options.ShowBytes) _writer.Write(string.Format(" /* {0:X8} */", token)); _writer.WriteLine(); }
private void WriteLocals(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".locals"); if (methodDefinition.LocalVariablesInitialized) { _writer.Write(" init"); } int i = 0; var locals = methodDefinition.Locals; _writer.Write('('); foreach (var local in locals) { if (i > 0) { WriteIndentation(); _writer.Write(string.Format("{0,13}", "")); } _writer.Write(string.Format("[{0}] ", i)); local.Accept(this); if (i < locals.Length - 1) { _writer.WriteLine(','); } i++; } _writer.WriteLine(')'); }
private void WriteMethodHeader(CilMethodDefinition methodDefinition) { WriteCustomAttributes(methodDefinition); if (methodDefinition.RelativeVirtualAddress == 0) { return; } foreach(var parameter in methodDefinition.GetOptionalParameters()) { WriteIndentation(); _writer.Write(".param "); _writer.WriteLine(string.Format("[{0}] = {1}", parameter.SequenceNumber, parameter.DefaultValue.GetValueString())); } if (methodDefinition.IsImplementation) { WriteOverridenMethod(methodDefinition); } if (methodDefinition.IsEntryPoint) { WriteIndentation(); _writer.WriteLine(".entrypoint"); } WriteIndentation(); _writer.WriteLine(string.Format("// code size {0,8} (0x{0:x})", methodDefinition.Size)); WriteIndentation(); _writer.WriteLine(string.Format(".maxstack {0,2}", methodDefinition.MaxStack)); if (methodDefinition.HasLocals) { WriteLocals(methodDefinition); } }
private void WriteMethodDefinition(CilMethodDefinition methodDefinition) { WriteIndentation(); _writer.Write(".method "); if (_options.ShowBytes) _writer.Write(string.Format("/* {0:X8} */ ", methodDefinition.Token)); _writer.WriteLine(methodDefinition.GetDecodedSignature()); WriteIndentation(); _writer.WriteLine('{'); }
private int WriteMethodBody(CilMethodDefinition methodDefinition, IReadOnlyList<CilExceptionRegion> exceptionRegions, ref int instructionIndex, int ilOffset, int regionIndex, out int nextRegionIndex) { int lastRegionIndex = regionIndex - 1; var instructions = methodDefinition.Instructions; for (; instructionIndex < instructions.Length; instructionIndex++) { var instruction = instructions[instructionIndex]; if (EndFilterRegion(exceptionRegions, lastRegionIndex, ilOffset)) { Unindent(); WriteIndentation(); _writer.WriteLine("} // end filter"); WriteIndentation(); _writer.WriteLine("{ // handler"); Indent(); } if (StartRegion(exceptionRegions, regionIndex, ilOffset)) { var region = exceptionRegions[regionIndex]; WriteIndentation(); _writer.WriteLine(region.ToString(methodDefinition.Provider)); WriteIndentation(); _writer.WriteLine('{'); Indent(); ilOffset = WriteMethodBody(methodDefinition, exceptionRegions, ref instructionIndex, ilOffset, regionIndex + 1, out regionIndex); Unindent(); WriteIndentation(); _writer.Write('}'); _writer.WriteLine(string.Format(" // end {0}", (region.Kind == HandlerKind.Try ? ".try" : "handler"))); } else { WriteIndentation(); _writer.Write(string.Format("IL_{0:x4}:", ilOffset)); _writer.Write(_indent); instruction.Accept(this); ilOffset += instruction.Size; } if (EndRegion(exceptionRegions, lastRegionIndex, ilOffset)) { break; } } nextRegionIndex = regionIndex; return ilOffset; }
private static IEnumerable<CilInstruction> DecodeMethodBody(BlobReader ilReader, MetadataReader metadataReader, CilTypeProvider provider, CilMethodDefinition methodDefinition) { ilReader.Reset(); int intOperand; ushort shortOperand; int ilOffset = 0; CilInstruction instruction = null; while (ilReader.Offset < ilReader.Length) { OpCode opCode; int expectedSize; byte _byte = ilReader.ReadByte(); /*If the byte read is 0xfe it means is a two byte instruction, so since it is going to read the second byte to get the actual instruction it has to check that the offset is still less than the length.*/ if (_byte == 0xfe && ilReader.Offset < ilReader.Length) { opCode = CilDecoderHelpers.Instance.twoByteOpCodes[ilReader.ReadByte()]; expectedSize = 2; } else { opCode = CilDecoderHelpers.Instance.oneByteOpCodes[_byte]; expectedSize = 1; } switch (opCode.OperandType) { //The instruction size is the expected size (1 or 2 depending if it is a one or two byte instruction) + the size of the operand. case OperandType.InlineField: intOperand = ilReader.ReadInt32(); string fieldInfo = GetFieldInformation(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, fieldInfo, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineString: intOperand = ilReader.ReadInt32(); bool isPrintable; string str = GetArgumentString(metadataReader, intOperand, out isPrintable); instruction = new CilStringInstruction(opCode, str, intOperand, expectedSize + (int)CilInstructionSize.Int32, isPrintable); break; case OperandType.InlineMethod: intOperand = ilReader.ReadInt32(); string methodCall = SolveMethodName(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, methodCall, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineType: intOperand = ilReader.ReadInt32(); string type = GetTypeInformation(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, type, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineTok: intOperand = ilReader.ReadInt32(); string tokenType = GetInlineTokenType(metadataReader, intOperand, provider); instruction = new CilStringInstruction(opCode, tokenType, intOperand, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineI: instruction = new CilInt32Instruction(opCode, ilReader.ReadInt32(), -1, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.InlineI8: instruction = new CilInt64Instruction(opCode, ilReader.ReadInt64(), -1, expectedSize + (int)CilInstructionSize.Int64); break; case OperandType.InlineR: instruction = new CilDoubleInstruction(opCode, ilReader.ReadDouble(), -1, expectedSize + (int)CilInstructionSize.Double); break; case OperandType.InlineSwitch: instruction = CreateSwitchInstruction(ref ilReader, expectedSize, ilOffset, opCode); break; case OperandType.ShortInlineBrTarget: instruction = new CilInt16BranchInstruction(opCode, ilReader.ReadSByte(), ilOffset, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.InlineBrTarget: instruction = new CilBranchInstruction(opCode, ilReader.ReadInt32(), ilOffset, expectedSize + (int)CilInstructionSize.Int32); break; case OperandType.ShortInlineI: instruction = new CilByteInstruction(opCode, ilReader.ReadByte(), -1, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.ShortInlineR: instruction = new CilSingleInstruction(opCode, ilReader.ReadSingle(), -1, expectedSize + (int)CilInstructionSize.Single); break; case OperandType.InlineNone: instruction = new CilInstructionWithNoValue(opCode, expectedSize); break; case OperandType.ShortInlineVar: byte token = ilReader.ReadByte(); instruction = new CilInt16VariableInstruction(opCode, GetVariableName(opCode, token, methodDefinition), token, expectedSize + (int)CilInstructionSize.Byte); break; case OperandType.InlineVar: shortOperand = ilReader.ReadUInt16(); instruction = new CilVariableInstruction(opCode, GetVariableName(opCode, shortOperand, methodDefinition), shortOperand, expectedSize + (int)CilInstructionSize.Int16); break; case OperandType.InlineSig: intOperand = ilReader.ReadInt32(); instruction = new CilStringInstruction(opCode, GetSignature(metadataReader, intOperand, provider), intOperand, expectedSize + (int)CilInstructionSize.Int32); break; default: break; } ilOffset += instruction.Size; yield return instruction; } }
private void WritePropertyOrEventAccessor(CilMethodDefinition accessor) { int i = 0; StringBuilder genericParameters = new StringBuilder(); foreach (var genericParameter in accessor.GenericParameters) { if (i == 0) { genericParameters.Append('<'); } genericParameters.Append(genericParameter); genericParameters.Append(','); i++; } if (i > 0) { genericParameters.Length -= 1; //Delete trailing , genericParameters.Append('>'); } if (accessor.Signature.Header.IsInstance) { _writer.Write("instance "); } _writer.Write(string.Format("{0} {1}::{2}{3}{4}", accessor.Signature.ReturnType, accessor.DeclaringType.FullName, accessor.Name, genericParameters.ToString(), CilDecoder.DecodeSignatureParamerTypes(accessor.Signature))); _writer.WriteLine(); }
public MethodNode(CilMethodDefinition methodDef) { _methodDef = methodDef; }