Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

initial commit

  • Loading branch information...
commit 714ac9e6de3606a1f71966ff1684d964d8ae1334 0 parents
@jbevain authored
Showing with 35,007 additions and 0 deletions.
  1. +7 −0 .gitignore
  2. +192 −0 Mono.Cecil-SL.csproj
  3. +252 −0 Mono.Cecil.Cil/Code.cs
  4. +607 −0 Mono.Cecil.Cil/CodeReader.cs
  5. +606 −0 Mono.Cecil.Cil/CodeWriter.cs
  6. +111 −0 Mono.Cecil.Cil/Document.cs
  7. +95 −0 Mono.Cecil.Cil/ExceptionHandler.cs
  8. +394 −0 Mono.Cecil.Cil/ILProcessor.cs
  9. +122 −0 Mono.Cecil.Cil/Instruction.cs
  10. +178 −0 Mono.Cecil.Cil/MethodBody.cs
  11. +525 −0 Mono.Cecil.Cil/OpCode.cs
  12. +912 −0 Mono.Cecil.Cil/OpCodes.cs
  13. +70 −0 Mono.Cecil.Cil/SequencePoint.cs
  14. +280 −0 Mono.Cecil.Cil/Symbols.cs
  15. +52 −0 Mono.Cecil.Cil/VariableDefinition.cs
  16. +75 −0 Mono.Cecil.Cil/VariableReference.cs
  17. +59 −0 Mono.Cecil.Metadata/BlobHeap.cs
  18. +374 −0 Mono.Cecil.Metadata/Buffers.cs
  19. +46 −0 Mono.Cecil.Metadata/CodedIndex.cs
  20. +73 −0 Mono.Cecil.Metadata/ElementType.cs
  21. +59 −0 Mono.Cecil.Metadata/GuidHeap.cs
  22. +48 −0 Mono.Cecil.Metadata/Heap.cs
  23. +105 −0 Mono.Cecil.Metadata/MetadataToken.cs
  24. +130 −0 Mono.Cecil.Metadata/Row.cs
  25. +80 −0 Mono.Cecil.Metadata/StringHeap.cs
  26. +153 −0 Mono.Cecil.Metadata/TableHeap.cs
  27. +56 −0 Mono.Cecil.Metadata/TokenType.cs
  28. +59 −0 Mono.Cecil.Metadata/UserStringHeap.cs
  29. +529 −0 Mono.Cecil.Metadata/Utilities.cs
  30. +51 −0 Mono.Cecil.PE/BinaryStreamReader.cs
  31. +96 −0 Mono.Cecil.PE/BinaryStreamWriter.cs
  32. +339 −0 Mono.Cecil.PE/ByteBuffer.cs
  33. +61 −0 Mono.Cecil.PE/ByteBufferEqualityComparer.cs
  34. +50 −0 Mono.Cecil.PE/DataDirectory.cs
  35. +159 −0 Mono.Cecil.PE/Image.cs
  36. +691 −0 Mono.Cecil.PE/ImageReader.cs
  37. +833 −0 Mono.Cecil.PE/ImageWriter.cs
  38. +43 −0 Mono.Cecil.PE/Section.cs
  39. +128 −0 Mono.Cecil.PE/TextMap.cs
  40. +179 −0 Mono.Cecil.csproj
  41. +9 −0 Mono.Cecil.nunit
  42. +85 −0 Mono.Cecil.sln
  43. +145 −0 Mono.Cecil/ArrayType.cs
  44. +184 −0 Mono.Cecil/AssemblyDefinition.cs
  45. +41 −0 Mono.Cecil/AssemblyFlags.cs
  46. +36 −0 Mono.Cecil/AssemblyHashAlgorithm.cs
  47. +45 −0 Mono.Cecil/AssemblyInfo.cs
  48. +57 −0 Mono.Cecil/AssemblyLinkedResource.cs
  49. +50 −0 Mono.Cecil/AssemblyNameDefinition.cs
  50. +262 −0 Mono.Cecil/AssemblyNameReference.cs
  51. +3,002 −0 Mono.Cecil/AssemblyReader.cs
  52. +2,517 −0 Mono.Cecil/AssemblyWriter.cs
  53. +264 −0 Mono.Cecil/BaseAssemblyResolver.cs
  54. +44 −0 Mono.Cecil/CallSite.cs
  55. +201 −0 Mono.Cecil/CustomAttribute.cs
  56. +75 −0 Mono.Cecil/DefaultAssemblyResolver.cs
  57. +90 −0 Mono.Cecil/EmbeddedResource.cs
  58. +39 −0 Mono.Cecil/EventAttributes.cs
  59. +164 −0 Mono.Cecil/EventDefinition.cs
  60. +55 −0 Mono.Cecil/EventReference.cs
  61. +235 −0 Mono.Cecil/ExportedType.cs
  62. +59 −0 Mono.Cecil/FieldAttributes.cs
  63. +270 −0 Mono.Cecil/FieldDefinition.cs
  64. +75 −0 Mono.Cecil/FieldReference.cs
  65. +35 −0 Mono.Cecil/FileAttributes.cs
  66. +114 −0 Mono.Cecil/FunctionPointerType.cs
  67. +80 −0 Mono.Cecil/GenericInstanceMethod.cs
  68. +77 −0 Mono.Cecil/GenericInstanceType.cs
  69. +194 −0 Mono.Cecil/GenericParameter.cs
  70. +45 −0 Mono.Cecil/GenericParameterAttributes.cs
  71. +52 −0 Mono.Cecil/IConstantProvider.cs
  72. +63 −0 Mono.Cecil/ICustomAttributeProvider.cs
  73. +55 −0 Mono.Cecil/IGenericInstance.cs
  74. +74 −0 Mono.Cecil/IGenericParameterProvider.cs
  75. +58 −0 Mono.Cecil/IMarshalInfoProvider.cs
  76. +100 −0 Mono.Cecil/IMemberDefinition.cs
  77. +41 −0 Mono.Cecil/IMetadataScope.cs
  78. +35 −0 Mono.Cecil/IMetadataTokenProvider.cs
  79. +70 −0 Mono.Cecil/IMethodSignature.cs
  80. +528 −0 Mono.Cecil/Import.cs
  81. +60 −0 Mono.Cecil/LinkedResource.cs
  82. +39 −0 Mono.Cecil/ManifestResourceAttributes.cs
  83. +164 −0 Mono.Cecil/MarshalInfo.cs
  84. +92 −0 Mono.Cecil/MemberDefinitionCollection.cs
  85. +93 −0 Mono.Cecil/MemberReference.cs
  86. +279 −0 Mono.Cecil/MetadataResolver.cs
  87. +325 −0 Mono.Cecil/MetadataSystem.cs
  88. +66 −0 Mono.Cecil/MethodAttributes.cs
  89. +40 −0 Mono.Cecil/MethodCallingConvention.cs
  90. +482 −0 Mono.Cecil/MethodDefinition.cs
  91. +53 −0 Mono.Cecil/MethodImplAttributes.cs
  92. +188 −0 Mono.Cecil/MethodReference.cs
  93. +94 −0 Mono.Cecil/MethodReturnType.cs
  94. +43 −0 Mono.Cecil/MethodSemanticsAttributes.cs
  95. +99 −0 Mono.Cecil/MethodSpecification.cs
  96. +129 −0 Mono.Cecil/Modifiers.cs
  97. +737 −0 Mono.Cecil/ModuleDefinition.cs
  98. +52 −0 Mono.Cecil/ModuleKind.cs
  99. +63 −0 Mono.Cecil/ModuleReference.cs
  100. +73 −0 Mono.Cecil/NativeType.cs
  101. +53 −0 Mono.Cecil/PInvokeAttributes.cs
  102. +118 −0 Mono.Cecil/PInvokeInfo.cs
  103. +43 −0 Mono.Cecil/ParameterAttributes.cs
  104. +156 −0 Mono.Cecil/ParameterDefinition.cs
  105. +82 −0 Mono.Cecil/ParameterDefinitionCollection.cs
  106. +75 −0 Mono.Cecil/ParameterReference.cs
  107. +53 −0 Mono.Cecil/PinnedType.cs
  108. +61 −0 Mono.Cecil/PointerType.cs
  109. +41 −0 Mono.Cecil/PropertyAttributes.cs
  110. +254 −0 Mono.Cecil/PropertyDefinition.cs
  111. +57 −0 Mono.Cecil/PropertyReference.cs
  112. +61 −0 Mono.Cecil/ReferenceType.cs
  113. +76 −0 Mono.Cecil/Resource.cs
  114. +196 −0 Mono.Cecil/SecurityDeclaration.cs
  115. +53 −0 Mono.Cecil/SentinelType.cs
  116. +37 −0 Mono.Cecil/TargetRuntime.cs
  117. +80 −0 Mono.Cecil/TypeAttributes.cs
  118. +490 −0 Mono.Cecil/TypeDefinition.cs
  119. +102 −0 Mono.Cecil/TypeDefinitionCollection.cs
  120. +241 −0 Mono.Cecil/TypeReference.cs
  121. +84 −0 Mono.Cecil/TypeSpecification.cs
  122. +354 −0 Mono.Cecil/TypeSystem.cs
  123. +53 −0 Mono.Cecil/VariantType.cs
  124. +396 −0 Mono.Collections.Generic/Collection.cs
  125. +243 −0 Mono.Security.Cryptography/CryptoConvert.cs
  126. +208 −0 Mono.Security.Cryptography/CryptoService.cs
  127. +38 −0 Mono/Actions.cs
  128. +55 −0 Mono/Empty.cs
  129. +39 −0 Mono/Funcs.cs
  130. +186 −0 NOTES.txt
  131. +40 −0 System.Runtime.CompilerServices/ExtensionAttribute.cs
  132. +7 −0 Test/.gitignore
  133. +118 −0 Test/Mono.Cecil.Tests.csproj
  134. +303 −0 Test/Mono.Cecil.Tests/Addin.cs
  135. +36 −0 Test/Mono.Cecil.Tests/AssemblyInfo.cs
  136. +25 −0 Test/Mono.Cecil.Tests/AssemblyTests.cs
  137. +66 −0 Test/Mono.Cecil.Tests/BaseTestFixture.cs
  138. +237 −0 Test/Mono.Cecil.Tests/CompilationService.cs
  139. +417 −0 Test/Mono.Cecil.Tests/CustomAttributesTests.cs
  140. +66 −0 Test/Mono.Cecil.Tests/EventTests.cs
  141. +85 −0 Test/Mono.Cecil.Tests/Extensions.cs
  142. +262 −0 Test/Mono.Cecil.Tests/FieldTests.cs
  143. +173 −0 Test/Mono.Cecil.Tests/Formatter.cs
  144. +79 −0 Test/Mono.Cecil.Tests/ILProcessorTests.cs
  145. +127 −0 Test/Mono.Cecil.Tests/ImageReadTests.cs
  146. +270 −0 Test/Mono.Cecil.Tests/ImportCecilTests.cs
  147. +268 −0 Test/Mono.Cecil.Tests/ImportReflectionTests.cs
  148. +47 −0 Test/Mono.Cecil.Tests/Linq.cs
  149. +230 −0 Test/Mono.Cecil.Tests/MethodBodyTests.cs
  150. +159 −0 Test/Mono.Cecil.Tests/MethodTests.cs
  151. +204 −0 Test/Mono.Cecil.Tests/ModuleTests.cs
  152. +45 −0 Test/Mono.Cecil.Tests/NestedTypesTests.cs
  153. +92 −0 Test/Mono.Cecil.Tests/ParameterTests.cs
  154. +88 −0 Test/Mono.Cecil.Tests/PropertyTests.cs
  155. +156 −0 Test/Mono.Cecil.Tests/ResolveTests.cs
  156. +199 −0 Test/Mono.Cecil.Tests/SecurityDeclarationTests.cs
  157. +143 −0 Test/Mono.Cecil.Tests/TypeTests.cs
  158. BIN  Test/Resources/assemblies/catch.exe
  159. BIN  Test/Resources/assemblies/decsec-att.dll
  160. BIN  Test/Resources/assemblies/decsec-xml.dll
  161. BIN  Test/Resources/assemblies/fptr.exe
  162. BIN  Test/Resources/assemblies/hello.anycpu.exe
  163. BIN  Test/Resources/assemblies/hello.exe
  164. BIN  Test/Resources/assemblies/hello.ia64.exe
  165. BIN  Test/Resources/assemblies/hello.x64.exe
  166. BIN  Test/Resources/assemblies/hello.x86.exe
  167. BIN  Test/Resources/assemblies/hello1.exe
  168. BIN  Test/Resources/assemblies/hellow.exe
  169. BIN  Test/Resources/assemblies/libhello.dll
  170. BIN  Test/Resources/assemblies/libres.dll
  171. BIN  Test/Resources/assemblies/mma.exe
  172. BIN  Test/Resources/assemblies/moda.netmodule
  173. BIN  Test/Resources/assemblies/modb.netmodule
  174. BIN  Test/Resources/assemblies/pinvoke.exe
  175. BIN  Test/Resources/assemblies/switch.exe
  176. +1 −0  Test/Resources/assemblies/text_file.txt
  177. BIN  Test/Resources/assemblies/varargs.exe
  178. +140 −0 Test/Resources/cs/CustomAttributes.cs
  179. +8 −0 Test/Resources/cs/Events.cs
  180. +49 −0 Test/Resources/cs/Fields.cs
  181. +92 −0 Test/Resources/cs/Generics.cs
  182. +22 −0 Test/Resources/cs/Interfaces.cs
  183. +16 −0 Test/Resources/cs/Layouts.cs
  184. +21 −0 Test/Resources/cs/Methods.cs
  185. +16 −0 Test/Resources/cs/NestedTypes.cs
  186. +8 −0 Test/Resources/cs/Properties.cs
  187. +50 −0 Test/Resources/il/hello.il
  188. +43 −0 Test/Resources/il/methodspecs.il
  189. +81 −0 Test/Resources/il/others.il
  190. +37 −0 Test/Resources/il/types.il
  191. +60 −0 dbg/Program.cs
  192. +36 −0 dbg/Properties/AssemblyInfo.cs
  193. +109 −0 dbg/dbg.csproj
  194. BIN  mono.snk
  195. +7 −0 rocks/.gitignore
  196. +66 −0 rocks/Mono.Cecil.Rocks.csproj
  197. +60 −0 rocks/Mono.Cecil.Rocks/Functional.cs
  198. +229 −0 rocks/Mono.Cecil.Rocks/ILParser.cs
  199. +413 −0 rocks/Mono.Cecil.Rocks/MethodBodyRocks.cs
  200. +47 −0 rocks/Mono.Cecil.Rocks/ModuleDefinitionRocks.cs
  201. +11 −0 rocks/Mono.Cecil.Rocks/ParameterReferenceRocks.cs
  202. +159 −0 rocks/Mono.Cecil.Rocks/SecurityDeclarationRocks.cs
  203. +70 −0 rocks/Mono.Cecil.Rocks/TypeDefinitionRocks.cs
  204. +104 −0 rocks/Mono.Cecil.Rocks/TypeReferenceRocks.cs
  205. +7 −0 rocks/Test/.gitignore
  206. +77 −0 rocks/Test/Mono.Cecil.Rocks.Tests.csproj
  207. +27 −0 rocks/Test/Mono.Cecil.Tests/ModuleDefinitionRocksTests.cs
  208. +61 −0 rocks/Test/Mono.Cecil.Tests/SecurityDeclarationRocksTests.cs
  209. +68 −0 rocks/Test/Mono.Cecil.Tests/TypeDefinitionRocksTests.cs
  210. +124 −0 rocks/Test/Mono.Cecil.Tests/TypeReferenceRocksTests.cs
  211. BIN  rocks/Test/Resources/assemblies/decsec-att.dll
  212. BIN  rocks/Test/Resources/assemblies/decsec-xml.dll
  213. +14 −0 rocks/Test/Resources/cs/Types.cs
  214. +63 −0 symbols/mdb/Mono.Cecil.Mdb.csproj
  215. +41 −0 symbols/mdb/Mono.Cecil.Mdb/AssemblyInfo.cs
  216. +223 −0 symbols/mdb/Mono.Cecil.Mdb/MdbReader.cs
  217. +246 −0 symbols/mdb/Mono.Cecil.Mdb/MdbWriter.cs
  218. +727 −0 symbols/mdb/Mono.CompilerServices.SymbolWriter/MonoSymbolFile.cs
  219. +1,382 −0 symbols/mdb/Mono.CompilerServices.SymbolWriter/MonoSymbolTable.cs
  220. +390 −0 symbols/mdb/Mono.CompilerServices.SymbolWriter/MonoSymbolWriter.cs
Sorry, we could not display the entire diff because it was too big.
7 .gitignore
@@ -0,0 +1,7 @@
+bin
+obj
+*.suo
+*.user
+*.pidb
+*.userprefs
+*.xml
192 Mono.Cecil-SL.csproj
@@ -0,0 +1,192 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>9.0.30729</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{59019979-D337-441B-851F-2133452191A8}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>Mono.Cecil</RootNamespace>
+ <AssemblyName>Mono.Cecil</AssemblyName>
+ <TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ <ProjectTypeGuids>{A1591282-1198-4647-A2B1-27E5FF5F6F3B};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
+ <SilverlightApplication>false</SilverlightApplication>
+ <DefineConstants>TRACE;DEBUG;SILVERLIGHT</DefineConstants>
+ <NoStdLib>true</NoStdLib>
+ <NoConfig>true</NoConfig>
+ <SignAssembly>true</SignAssembly>
+ <AssemblyOriginatorKeyFile>mono.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\DebugSL\</OutputPath>
+ <DefineConstants>DEBUG;TRACE;SILVERLIGHT</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\ReleaseSL\</OutputPath>
+ <DefineConstants>TRACE;SILVERLIGHT</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="mscorlib" />
+ <Reference Include="system" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Mono.Cecil.Cil\Code.cs" />
+ <Compile Include="Mono.Cecil.Cil\CodeWriter.cs" />
+ <Compile Include="Mono.Cecil.Cil\CodeReader.cs" />
+ <Compile Include="Mono.Cecil.Cil\Document.cs" />
+ <Compile Include="Mono.Cecil.Cil\ExceptionHandler.cs" />
+ <Compile Include="Mono.Cecil.Cil\ILProcessor.cs" />
+ <Compile Include="Mono.Cecil.Cil\Instruction.cs" />
+ <Compile Include="Mono.Cecil.Cil\MethodBody.cs" />
+ <Compile Include="Mono.Cecil.Cil\OpCode.cs" />
+ <Compile Include="Mono.Cecil.Cil\OpCodes.cs" />
+ <Compile Include="Mono.Cecil.Cil\SequencePoint.cs" />
+ <Compile Include="Mono.Cecil.Cil\Symbols.cs" />
+ <Compile Include="Mono.Cecil.Cil\VariableDefinition.cs" />
+ <Compile Include="Mono.Cecil.Cil\VariableReference.cs" />
+ <Compile Include="Mono.Cecil.Metadata\BlobHeap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\Buffers.cs" />
+ <Compile Include="Mono.Cecil.Metadata\CodedIndex.cs" />
+ <Compile Include="Mono.Cecil.Metadata\ElementType.cs" />
+ <Compile Include="Mono.Cecil.Metadata\GuidHeap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\Heap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\MetadataToken.cs" />
+ <Compile Include="Mono.Cecil.Metadata\Row.cs" />
+ <Compile Include="Mono.Cecil.Metadata\StringHeap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\TableHeap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\TokenType.cs" />
+ <Compile Include="Mono.Cecil.Metadata\UserStringHeap.cs" />
+ <Compile Include="Mono.Cecil.Metadata\Utilities.cs" />
+ <Compile Include="Mono.Cecil.PE\BinaryStreamReader.cs" />
+ <Compile Include="Mono.Cecil.PE\BinaryStreamWriter.cs" />
+ <Compile Include="Mono.Cecil.PE\ByteBuffer.cs" />
+ <Compile Include="Mono.Cecil.PE\ByteBufferEqualityComparer.cs" />
+ <Compile Include="Mono.Cecil.PE\DataDirectory.cs" />
+ <Compile Include="Mono.Cecil.PE\Image.cs" />
+ <Compile Include="Mono.Cecil.PE\ImageReader.cs" />
+ <Compile Include="Mono.Cecil.PE\ImageWriter.cs" />
+ <Compile Include="Mono.Cecil.PE\Section.cs" />
+ <Compile Include="Mono.Cecil.PE\TextMap.cs" />
+ <Compile Include="Mono.Cecil\ArrayType.cs" />
+ <Compile Include="Mono.Cecil\AssemblyDefinition.cs" />
+ <Compile Include="Mono.Cecil\AssemblyFlags.cs" />
+ <Compile Include="Mono.Cecil\AssemblyHashAlgorithm.cs" />
+ <Compile Include="Mono.Cecil\AssemblyInfo.cs" />
+ <Compile Include="Mono.Cecil\AssemblyLinkedResource.cs" />
+ <Compile Include="Mono.Cecil\AssemblyNameDefinition.cs" />
+ <Compile Include="Mono.Cecil\AssemblyNameReference.cs" />
+ <Compile Include="Mono.Cecil\AssemblyReader.cs" />
+ <Compile Include="Mono.Cecil\AssemblyWriter.cs" />
+ <Compile Include="Mono.Cecil\BaseAssemblyResolver.cs" />
+ <Compile Include="Mono.Cecil\CallSite.cs" />
+ <Compile Include="Mono.Cecil\CustomAttribute.cs" />
+ <Compile Include="Mono.Cecil\DefaultAssemblyResolver.cs" />
+ <Compile Include="Mono.Cecil\ExportedType.cs" />
+ <Compile Include="Mono.Cecil\FileAttributes.cs" />
+ <Compile Include="Mono.Cecil\FunctionPointerType.cs" />
+ <Compile Include="Mono.Cecil\GenericInstanceMethod.cs" />
+ <Compile Include="Mono.Cecil\GenericInstanceType.cs" />
+ <Compile Include="Mono.Cecil\GenericParameter.cs" />
+ <Compile Include="Mono.Cecil\GenericParameterAttributes.cs" />
+ <Compile Include="Mono.Cecil\IConstantProvider.cs" />
+ <Compile Include="Mono.Cecil\ICustomAttributeProvider.cs" />
+ <Compile Include="Mono.Cecil\IGenericInstance.cs" />
+ <Compile Include="Mono.Cecil\IGenericParameterProvider.cs" />
+ <Compile Include="Mono.Cecil\IMarshalInfoProvider.cs" />
+ <Compile Include="Mono.Cecil\MarshalInfo.cs" />
+ <Compile Include="Mono.Cecil\MetadataResolver.cs" />
+ <Compile Include="Mono.Cecil\Modifiers.cs" />
+ <Compile Include="Mono.Cecil\NativeType.cs" />
+ <Compile Include="Mono.Cecil\PinnedType.cs" />
+ <Compile Include="Mono.Cecil\MetadataSystem.cs" />
+ <Compile Include="Mono.Cecil\IMethodSignature.cs" />
+ <Compile Include="Mono.Cecil\ParameterDefinitionCollection.cs" />
+ <Compile Include="Mono.Cecil\EmbeddedResource.cs" />
+ <Compile Include="Mono.Cecil\EventAttributes.cs" />
+ <Compile Include="Mono.Cecil\EventDefinition.cs" />
+ <Compile Include="Mono.Cecil\EventReference.cs" />
+ <Compile Include="Mono.Cecil\FieldAttributes.cs" />
+ <Compile Include="Mono.Cecil\FieldDefinition.cs" />
+ <Compile Include="Mono.Cecil\FieldReference.cs" />
+ <Compile Include="Mono.Cecil\IMemberDefinition.cs" />
+ <Compile Include="Mono.Cecil\Import.cs" />
+ <Compile Include="Mono.Cecil\LinkedResource.cs" />
+ <Compile Include="Mono.Cecil\MemberReference.cs" />
+ <Compile Include="Mono.Cecil\MethodAttributes.cs" />
+ <Compile Include="Mono.Cecil\MethodCallingConvention.cs" />
+ <Compile Include="Mono.Cecil\MethodDefinition.cs" />
+ <Compile Include="Mono.Cecil\MethodImplAttributes.cs" />
+ <Compile Include="Mono.Cecil\MethodReference.cs" />
+ <Compile Include="Mono.Cecil\MethodReturnType.cs" />
+ <Compile Include="Mono.Cecil\MethodSemanticsAttributes.cs" />
+ <Compile Include="Mono.Cecil\MethodSpecification.cs" />
+ <Compile Include="Mono.Cecil\ParameterAttributes.cs" />
+ <Compile Include="Mono.Cecil\ParameterDefinition.cs" />
+ <Compile Include="Mono.Cecil\ParameterReference.cs" />
+ <Compile Include="Mono.Cecil\PInvokeAttributes.cs" />
+ <Compile Include="Mono.Cecil\PInvokeInfo.cs" />
+ <Compile Include="Mono.Cecil\PointerType.cs" />
+ <Compile Include="Mono.Cecil\PropertyAttributes.cs" />
+ <Compile Include="Mono.Cecil\PropertyDefinition.cs" />
+ <Compile Include="Mono.Cecil\PropertyReference.cs" />
+ <Compile Include="Mono.Cecil\ReferenceType.cs" />
+ <Compile Include="Mono.Cecil\IMetadataScope.cs" />
+ <Compile Include="Mono.Cecil\IMetadataTokenProvider.cs" />
+ <Compile Include="Mono.Cecil\ManifestResourceAttributes.cs" />
+ <Compile Include="Mono.Cecil\ModuleReference.cs" />
+ <Compile Include="Mono.Cecil\MemberDefinitionCollection.cs" />
+ <Compile Include="Mono.Cecil\ModuleDefinition.cs" />
+ <Compile Include="Mono.Cecil\ModuleKind.cs" />
+ <Compile Include="Mono.Cecil\Resource.cs" />
+ <Compile Include="Mono.Cecil\SecurityDeclaration.cs" />
+ <Compile Include="Mono.Cecil\SentinelType.cs" />
+ <Compile Include="Mono.Cecil\TargetRuntime.cs" />
+ <Compile Include="Mono.Cecil\TypeAttributes.cs" />
+ <Compile Include="Mono.Cecil\TypeDefinition.cs" />
+ <Compile Include="Mono.Cecil\TypeDefinitionCollection.cs" />
+ <Compile Include="Mono.Cecil\TypeReference.cs" />
+ <Compile Include="Mono.Cecil\TypeSpecification.cs" />
+ <Compile Include="Mono.Cecil\TypeSystem.cs" />
+ <Compile Include="Mono.Cecil\VariantType.cs" />
+ <Compile Include="Mono.Collections.Generic\Collection.cs" />
+ <Compile Include="Mono.Security.Cryptography\CryptoConvert.cs" />
+ <Compile Include="Mono.Security.Cryptography\CryptoService.cs" />
+ <Compile Include="System.Runtime.CompilerServices\ExtensionAttribute.cs" />
+ <Compile Include="System\Actions.cs" />
+ <Compile Include="System\Empty.cs" />
+ <Compile Include="System\Funcs.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <Content Include="NOTES.txt" />
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="mono.snk" />
+ </ItemGroup>
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\Silverlight\v3.0\Microsoft.Silverlight.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+ <ProjectExtensions>
+ <VisualStudio>
+ <FlavorProperties GUID="{A1591282-1198-4647-A2B1-27E5FF5F6F3B}">
+ <SilverlightProjectProperties />
+ </FlavorProperties>
+ </VisualStudio>
+ </ProjectExtensions>
+</Project>
252 Mono.Cecil.Cil/Code.cs
@@ -0,0 +1,252 @@
+//
+// Code.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+namespace Mono.Cecil.Cil {
+
+ public enum Code {
+ Nop,
+ Break,
+ Ldarg_0,
+ Ldarg_1,
+ Ldarg_2,
+ Ldarg_3,
+ Ldloc_0,
+ Ldloc_1,
+ Ldloc_2,
+ Ldloc_3,
+ Stloc_0,
+ Stloc_1,
+ Stloc_2,
+ Stloc_3,
+ Ldarg_S,
+ Ldarga_S,
+ Starg_S,
+ Ldloc_S,
+ Ldloca_S,
+ Stloc_S,
+ Ldnull,
+ Ldc_I4_M1,
+ Ldc_I4_0,
+ Ldc_I4_1,
+ Ldc_I4_2,
+ Ldc_I4_3,
+ Ldc_I4_4,
+ Ldc_I4_5,
+ Ldc_I4_6,
+ Ldc_I4_7,
+ Ldc_I4_8,
+ Ldc_I4_S,
+ Ldc_I4,
+ Ldc_I8,
+ Ldc_R4,
+ Ldc_R8,
+ Dup,
+ Pop,
+ Jmp,
+ Call,
+ Calli,
+ Ret,
+ Br_S,
+ Brfalse_S,
+ Brtrue_S,
+ Beq_S,
+ Bge_S,
+ Bgt_S,
+ Ble_S,
+ Blt_S,
+ Bne_Un_S,
+ Bge_Un_S,
+ Bgt_Un_S,
+ Ble_Un_S,
+ Blt_Un_S,
+ Br,
+ Brfalse,
+ Brtrue,
+ Beq,
+ Bge,
+ Bgt,
+ Ble,
+ Blt,
+ Bne_Un,
+ Bge_Un,
+ Bgt_Un,
+ Ble_Un,
+ Blt_Un,
+ Switch,
+ Ldind_I1,
+ Ldind_U1,
+ Ldind_I2,
+ Ldind_U2,
+ Ldind_I4,
+ Ldind_U4,
+ Ldind_I8,
+ Ldind_I,
+ Ldind_R4,
+ Ldind_R8,
+ Ldind_Ref,
+ Stind_Ref,
+ Stind_I1,
+ Stind_I2,
+ Stind_I4,
+ Stind_I8,
+ Stind_R4,
+ Stind_R8,
+ Add,
+ Sub,
+ Mul,
+ Div,
+ Div_Un,
+ Rem,
+ Rem_Un,
+ And,
+ Or,
+ Xor,
+ Shl,
+ Shr,
+ Shr_Un,
+ Neg,
+ Not,
+ Conv_I1,
+ Conv_I2,
+ Conv_I4,
+ Conv_I8,
+ Conv_R4,
+ Conv_R8,
+ Conv_U4,
+ Conv_U8,
+ Callvirt,
+ Cpobj,
+ Ldobj,
+ Ldstr,
+ Newobj,
+ Castclass,
+ Isinst,
+ Conv_R_Un,
+ Unbox,
+ Throw,
+ Ldfld,
+ Ldflda,
+ Stfld,
+ Ldsfld,
+ Ldsflda,
+ Stsfld,
+ Stobj,
+ Conv_Ovf_I1_Un,
+ Conv_Ovf_I2_Un,
+ Conv_Ovf_I4_Un,
+ Conv_Ovf_I8_Un,
+ Conv_Ovf_U1_Un,
+ Conv_Ovf_U2_Un,
+ Conv_Ovf_U4_Un,
+ Conv_Ovf_U8_Un,
+ Conv_Ovf_I_Un,
+ Conv_Ovf_U_Un,
+ Box,
+ Newarr,
+ Ldlen,
+ Ldelema,
+ Ldelem_I1,
+ Ldelem_U1,
+ Ldelem_I2,
+ Ldelem_U2,
+ Ldelem_I4,
+ Ldelem_U4,
+ Ldelem_I8,
+ Ldelem_I,
+ Ldelem_R4,
+ Ldelem_R8,
+ Ldelem_Ref,
+ Stelem_I,
+ Stelem_I1,
+ Stelem_I2,
+ Stelem_I4,
+ Stelem_I8,
+ Stelem_R4,
+ Stelem_R8,
+ Stelem_Ref,
+ Ldelem_Any,
+ Stelem_Any,
+ Unbox_Any,
+ Conv_Ovf_I1,
+ Conv_Ovf_U1,
+ Conv_Ovf_I2,
+ Conv_Ovf_U2,
+ Conv_Ovf_I4,
+ Conv_Ovf_U4,
+ Conv_Ovf_I8,
+ Conv_Ovf_U8,
+ Refanyval,
+ Ckfinite,
+ Mkrefany,
+ Ldtoken,
+ Conv_U2,
+ Conv_U1,
+ Conv_I,
+ Conv_Ovf_I,
+ Conv_Ovf_U,
+ Add_Ovf,
+ Add_Ovf_Un,
+ Mul_Ovf,
+ Mul_Ovf_Un,
+ Sub_Ovf,
+ Sub_Ovf_Un,
+ Endfinally,
+ Leave,
+ Leave_S,
+ Stind_I,
+ Conv_U,
+ Arglist,
+ Ceq,
+ Cgt,
+ Cgt_Un,
+ Clt,
+ Clt_Un,
+ Ldftn,
+ Ldvirtftn,
+ Ldarg,
+ Ldarga,
+ Starg,
+ Ldloc,
+ Ldloca,
+ Stloc,
+ Localloc,
+ Endfilter,
+ Unaligned,
+ Volatile,
+ Tail,
+ Initobj,
+ Constrained,
+ Cpblk,
+ Initblk,
+ No,
+ Rethrow,
+ Sizeof,
+ Refanytype,
+ Readonly,
+ }
+}
607 Mono.Cecil.Cil/CodeReader.cs
@@ -0,0 +1,607 @@
+//
+// CodeReader.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+using Mono.Cecil.PE;
+using Mono.Collections.Generic;
+
+using RVA = System.UInt32;
+
+namespace Mono.Cecil.Cil {
+
+ sealed class CodeReader : ByteBuffer {
+
+ readonly internal MetadataReader reader;
+
+ int start;
+ Section code_section;
+
+ MethodDefinition method;
+ MethodBody body;
+
+ int Offset {
+ get { return base.position - start; }
+ }
+
+ CodeReader (Section section, MetadataReader reader)
+ : base (section.Data)
+ {
+ this.code_section = section;
+ this.reader = reader;
+ }
+
+ public static CodeReader CreateCodeReader (MetadataReader metadata)
+ {
+ return new CodeReader (metadata.image.MetadataSection, metadata);
+ }
+
+ public MethodBody ReadMethodBody (MethodDefinition method)
+ {
+ this.method = method;
+ this.body = new MethodBody (method);
+
+ reader.context = method;
+
+ ReadMethodBody ();
+
+ return this.body;
+ }
+
+ public void MoveTo (int rva)
+ {
+ if (!IsInSection (rva)) {
+ code_section = reader.image.GetSectionAtVirtualAddress ((uint) rva);
+ Reset (code_section.Data);
+ }
+
+ base.position = rva - (int) code_section.VirtualAddress;
+ }
+
+ bool IsInSection (int rva)
+ {
+ return code_section.VirtualAddress <= rva && rva < code_section.VirtualAddress + code_section.SizeOfRawData;
+ }
+
+ void ReadMethodBody ()
+ {
+ MoveTo (method.RVA);
+
+ var flags = ReadByte ();
+ switch (flags & 0x3) {
+ case 0x2: // tiny
+ body.code_size = flags >> 2;
+ body.MaxStackSize = 8;
+ ReadCode ();
+ break;
+ case 0x3: // fat
+ base.position--;
+ ReadFatMethod ();
+ break;
+ default:
+ throw new InvalidOperationException ();
+ }
+
+ var symbol_reader = reader.module.SymbolReader;
+
+ if (symbol_reader != null) {
+ var instructions = body.Instructions;
+ symbol_reader.Read (body, offset => GetInstruction (instructions, offset));
+ }
+ }
+
+ void ReadFatMethod ()
+ {
+ var flags = ReadUInt16 ();
+ body.max_stack_size = ReadUInt16 ();
+ body.code_size = (int) ReadUInt32 ();
+ body.local_var_token = new MetadataToken (ReadUInt32 ());
+ body.init_locals = (flags & 0x10) != 0;
+
+ if (body.LocalVarToken.RID != 0)
+ body.variables = ReadVariables (body.local_var_token);
+
+ ReadCode ();
+
+ if ((flags & 0x8) != 0)
+ ReadSection ();
+ }
+
+ public VariableDefinitionCollection ReadVariables (MetadataToken local_var_token)
+ {
+ var position = reader.position;
+ var variables = reader.ReadVariables (local_var_token);
+ reader.position = position;
+
+ return variables;
+ }
+
+ void ReadCode ()
+ {
+ start = position;
+ var code_size = body.code_size;
+ var end = start + code_size;
+ Instruction previous = null;
+ var instructions = body.instructions = new Collection<Instruction> ((code_size / 3));
+
+ while (position < end) {
+ var offset = base.position - start;
+ var opcode = ReadOpCode ();
+ var current = new Instruction (offset, opcode);
+ if (previous != null) {
+ current.previous = previous;
+ previous.next = current;
+ }
+
+ if (opcode.OperandType != OperandType.InlineNone)
+ current.operand = ReadOperand (current);
+
+ instructions.Add (current);
+
+ previous = current;
+ }
+
+ ResolveBranches (instructions);
+ }
+
+ OpCode ReadOpCode ()
+ {
+ var il_opcode = ReadByte ();
+ return il_opcode != 0xfe
+ ? OpCodes.OneByteOpCode [il_opcode]
+ : OpCodes.TwoBytesOpCode [ReadByte ()];
+ }
+
+ object ReadOperand (Instruction instruction)
+ {
+ switch (instruction.opcode.OperandType) {
+ case OperandType.InlineSwitch:
+ var length = ReadInt32 ();
+ var base_offset = Offset + (4 * length);
+ var branches = new int [length];
+ for (int i = 0; i < length; i++)
+ branches [i] = base_offset + ReadInt32 ();
+ return branches;
+ case OperandType.ShortInlineBrTarget:
+ return ReadSByte () + Offset;
+ case OperandType.InlineBrTarget:
+ return ReadInt32 () + Offset;
+ case OperandType.ShortInlineI:
+ if (instruction.opcode == OpCodes.Ldc_I4_S)
+ return ReadSByte ();
+
+ return ReadByte ();
+ case OperandType.InlineI:
+ return ReadInt32 ();
+ case OperandType.ShortInlineR:
+ return ReadSingle ();
+ case OperandType.InlineR:
+ return ReadDouble ();
+ case OperandType.InlineI8:
+ return ReadInt64 ();
+ case OperandType.ShortInlineVar:
+ return GetVariable (ReadByte ());
+ case OperandType.InlineVar:
+ return GetVariable (ReadUInt16 ());
+ case OperandType.ShortInlineArg:
+ return GetParameter (ReadByte ());
+ case OperandType.InlineArg:
+ return GetParameter (ReadUInt16 ());
+ case OperandType.InlineSig:
+ return GetCallSite (ReadToken ());
+ case OperandType.InlineString:
+ return GetString (ReadToken ());
+ case OperandType.InlineTok:
+ case OperandType.InlineType:
+ case OperandType.InlineMethod:
+ case OperandType.InlineField:
+ return reader.LookupToken (ReadToken ());
+ default:
+ throw new NotSupportedException ();
+ }
+ }
+
+ public string GetString (MetadataToken token)
+ {
+ return reader.image.UserStringHeap.Read (token.RID);
+ }
+
+ public ParameterDefinition GetParameter (int index)
+ {
+ return body.GetParameter (index);
+ }
+
+ public VariableDefinition GetVariable (int index)
+ {
+ return body.Variables [index];
+ }
+
+ public CallSite GetCallSite (MetadataToken token)
+ {
+ return reader.ReadCallSite (token);
+ }
+
+ void ResolveBranches (Collection<Instruction> instructions)
+ {
+ var items = instructions.items;
+ var size = instructions.size;
+
+ for (int i = 0; i < size; i++) {
+ var instruction = items [i];
+ switch (instruction.opcode.OperandType) {
+ case OperandType.ShortInlineBrTarget:
+ case OperandType.InlineBrTarget:
+ instruction.operand = GetInstruction ((int) instruction.operand);
+ break;
+ case OperandType.InlineSwitch:
+ var offsets = (int []) instruction.operand;
+ var branches = new Instruction [offsets.Length];
+ for (int j = 0; j < offsets.Length; j++)
+ branches [j] = GetInstruction (offsets [j]);
+
+ instruction.operand = branches;
+ break;
+ }
+ }
+ }
+
+ Instruction GetInstruction (int offset)
+ {
+ return GetInstruction (body.Instructions, offset);
+ }
+
+ static Instruction GetInstruction (Collection<Instruction> instructions, int offset)
+ {
+ var size = instructions.size;
+ var items = instructions.items;
+ if (offset < 0 || offset > items [size - 1].offset)
+ return null;
+
+ int min = 0;
+ int max = size - 1;
+ while (min <= max) {
+ int mid = min + ((max - min) / 2);
+ var instruction = items [mid];
+ var instruction_offset = instruction.offset;
+
+ if (offset == instruction_offset)
+ return instruction;
+
+ if (offset < instruction_offset)
+ max = mid - 1;
+ else
+ min = mid + 1;
+ }
+
+ return null;
+ }
+
+ void ReadSection ()
+ {
+ Align (4);
+
+ const byte fat_format = 0x40;
+ const byte more_sects = 0x80;
+
+ var flags = ReadByte ();
+ if ((flags & fat_format) == 0)
+ ReadSmallSection ();
+ else
+ ReadFatSection ();
+
+ if ((flags & more_sects) != 0)
+ ReadSection ();
+ }
+
+ void ReadSmallSection ()
+ {
+ var count = ReadByte () / 12;
+ Advance (2);
+
+ ReadExceptionHandlers (
+ count,
+ () => (int) ReadUInt16 (),
+ () => (int) ReadByte ());
+ }
+
+ void ReadFatSection ()
+ {
+ position--;
+ var count = (ReadInt32 () >> 8) / 24;
+
+ ReadExceptionHandlers (
+ count,
+ ReadInt32,
+ ReadInt32);
+ }
+
+ // inline ?
+ void ReadExceptionHandlers (int count, Func<int> read_entry, Func<int> read_length)
+ {
+ for (int i = 0; i < count; i++) {
+ var handler = new ExceptionHandler (
+ (ExceptionHandlerType) (read_entry () & 0x7));
+
+ handler.TryStart = GetInstruction (read_entry ());
+ handler.TryEnd = GetInstruction (handler.TryStart.Offset + read_length ());
+
+ handler.HandlerStart = GetInstruction (read_entry ());
+ handler.HandlerEnd = GetInstruction (handler.HandlerStart.Offset + read_length ());
+
+ ReadExceptionHandlerSpecific (handler);
+
+ this.body.ExceptionHandlers.Add (handler);
+ }
+ }
+
+ void ReadExceptionHandlerSpecific (ExceptionHandler handler)
+ {
+ switch (handler.HandlerType) {
+ case ExceptionHandlerType.Catch:
+ handler.CatchType = (TypeReference) reader.LookupToken (ReadToken ());
+ break;
+ case ExceptionHandlerType.Filter:
+ handler.FilterStart = GetInstruction (ReadInt32 ());
+ handler.FilterEnd = handler.HandlerStart.Previous;
+ break;
+ default:
+ Advance (4);
+ break;
+ }
+ }
+
+ void Align (int align)
+ {
+ align--;
+ Advance (((position + align) & ~align) - position);
+ }
+
+ public MetadataToken ReadToken ()
+ {
+ return new MetadataToken (ReadUInt32 ());
+ }
+
+#if !READ_ONLY
+
+ public ByteBuffer PatchRawMethodBody (MethodDefinition method, CodeWriter writer, out MethodSymbols symbols)
+ {
+ var buffer = new ByteBuffer ();
+ symbols = new MethodSymbols (method.Name);
+
+ this.method = method;
+ reader.context = method;
+
+ MoveTo (method.RVA);
+
+ var flags = ReadByte ();
+
+ MetadataToken local_var_token;
+
+ switch (flags & 0x3) {
+ case 0x2: // tiny
+ buffer.WriteByte (flags);
+ local_var_token = MetadataToken.Zero;
+ symbols.code_size = flags >> 2;
+ PatchRawCode (buffer, symbols.code_size, writer);
+ break;
+ case 0x3: // fat
+ base.position--;
+
+ PatchRawFatMethod (buffer, symbols, writer, out local_var_token);
+ break;
+ default:
+ throw new NotSupportedException ();
+ }
+
+ var symbol_reader = reader.module.SymbolReader;
+ if (symbol_reader != null && writer.metadata.write_symbols) {
+ symbols.method_token = GetOriginalToken (writer.metadata, method);
+ symbols.local_var_token = local_var_token;
+ symbol_reader.Read (symbols);
+ }
+
+ return buffer;
+ }
+
+ void PatchRawFatMethod (ByteBuffer buffer, MethodSymbols symbols, CodeWriter writer, out MetadataToken local_var_token)
+ {
+ var flags = ReadUInt16 ();
+ buffer.WriteUInt16 (flags);
+ buffer.WriteUInt16 (ReadUInt16 ());
+ symbols.code_size = ReadInt32 ();
+ buffer.WriteInt32 (symbols.code_size);
+ local_var_token = ReadToken ();
+
+ if (local_var_token.RID > 0) {
+ var variables = symbols.variables = ReadVariables (local_var_token);
+ buffer.WriteUInt32 (variables != null
+ ? writer.GetStandAloneSignature (symbols.variables).ToUInt32 ()
+ : 0);
+ } else
+ buffer.WriteUInt32 (0);
+
+ PatchRawCode (buffer, symbols.code_size, writer);
+
+ if ((flags & 0x8) != 0)
+ PatchRawSection (buffer, writer.metadata);
+ }
+
+ static MetadataToken GetOriginalToken (MetadataBuilder metadata, MethodDefinition method)
+ {
+ MetadataToken original;
+ if (metadata.TryGetOriginalMethodToken (method.token, out original))
+ return original;
+
+ return MetadataToken.Zero;
+ }
+
+ void PatchRawCode (ByteBuffer buffer, int code_size, CodeWriter writer)
+ {
+ var metadata = writer.metadata;
+ buffer.WriteBytes (ReadBytes (code_size));
+ var end = buffer.position;
+ buffer.position -= code_size;
+
+ while (buffer.position < end) {
+ OpCode opcode;
+ var il_opcode = buffer.ReadByte ();
+ if (il_opcode != 0xfe) {
+ opcode = OpCodes.OneByteOpCode [il_opcode];
+ } else {
+ var il_opcode2 = buffer.ReadByte ();
+ opcode = OpCodes.TwoBytesOpCode [il_opcode2];
+ }
+
+ switch (opcode.OperandType) {
+ case OperandType.ShortInlineI:
+ case OperandType.ShortInlineBrTarget:
+ case OperandType.ShortInlineVar:
+ case OperandType.ShortInlineArg:
+ buffer.position += 1;
+ break;
+ case OperandType.InlineVar:
+ case OperandType.InlineArg:
+ buffer.position += 2;
+ break;
+ case OperandType.InlineBrTarget:
+ case OperandType.ShortInlineR:
+ case OperandType.InlineI:
+ buffer.position += 4;
+ break;
+ case OperandType.InlineI8:
+ case OperandType.InlineR:
+ buffer.position += 8;
+ break;
+ case OperandType.InlineSwitch:
+ var length = buffer.ReadInt32 ();
+ buffer.position += length * 4;
+ break;
+ case OperandType.InlineString:
+ var @string = GetString (new MetadataToken (buffer.ReadUInt32 ()));
+ buffer.position -= 4;
+ buffer.WriteUInt32 (
+ new MetadataToken (
+ TokenType.String,
+ metadata.user_string_heap.GetStringIndex (@string)).ToUInt32 ());
+ break;
+ case OperandType.InlineSig:
+ var call_site = GetCallSite (new MetadataToken (buffer.ReadUInt32 ()));
+ buffer.position -= 4;
+ buffer.WriteUInt32 (writer.GetStandAloneSignature (call_site).ToUInt32 ());
+ break;
+ case OperandType.InlineTok:
+ case OperandType.InlineType:
+ case OperandType.InlineMethod:
+ case OperandType.InlineField:
+ var provider = reader.LookupToken (new MetadataToken (buffer.ReadUInt32 ()));
+ buffer.position -= 4;
+ buffer.WriteUInt32 (metadata.LookupToken (provider).ToUInt32 ());
+ break;
+ }
+ }
+ }
+
+ void PatchRawSection (ByteBuffer buffer, MetadataBuilder metadata)
+ {
+ var position = base.position;
+ Align (4);
+ buffer.WriteBytes (base.position - position);
+
+ const byte fat_format = 0x40;
+ const byte more_sects = 0x80;
+
+ var flags = ReadByte ();
+ if ((flags & fat_format) == 0) {
+ buffer.WriteByte (flags);
+ PatchRawSmallSection (buffer, metadata);
+ } else
+ PatchRawFatSection (buffer, metadata);
+
+ if ((flags & more_sects) != 0)
+ PatchRawSection (buffer, metadata);
+ }
+
+ void PatchRawSmallSection (ByteBuffer buffer, MetadataBuilder metadata)
+ {
+ var length = ReadByte ();
+ buffer.WriteByte (length);
+ Advance (2);
+
+ buffer.WriteUInt16 (0);
+
+ var count = length / 12;
+
+ PatchRawExceptionHandlers (buffer, metadata, count, false);
+ }
+
+ void PatchRawFatSection (ByteBuffer buffer, MetadataBuilder metadata)
+ {
+ position--;
+ var length = ReadInt32 ();
+ buffer.WriteInt32 (length);
+
+ var count = (length >> 8) / 24;
+
+ PatchRawExceptionHandlers (buffer, metadata, count, true);
+ }
+
+ void PatchRawExceptionHandlers (ByteBuffer buffer, MetadataBuilder metadata, int count, bool fat_entry)
+ {
+ const int fat_entry_size = 16;
+ const int small_entry_size = 6;
+
+ for (int i = 0; i < count; i++) {
+ ExceptionHandlerType handler_type;
+ if (fat_entry) {
+ var type = ReadUInt32 ();
+ handler_type = (ExceptionHandlerType) (type & 0x7);
+ buffer.WriteUInt32 (type);
+ } else {
+ var type = ReadUInt16 ();
+ handler_type = (ExceptionHandlerType) (type & 0x7);
+ buffer.WriteUInt16 (type);
+ }
+
+ buffer.WriteBytes (ReadBytes (fat_entry ? fat_entry_size : small_entry_size));
+
+ switch (handler_type) {
+ case ExceptionHandlerType.Catch:
+ var exception = reader.LookupToken (ReadToken ());
+ buffer.WriteUInt32 (metadata.LookupToken (exception).ToUInt32 ());
+ break;
+ default:
+ buffer.WriteUInt32 (ReadUInt32 ());
+ break;
+ }
+ }
+ }
+
+#endif
+
+ }
+}
606 Mono.Cecil.Cil/CodeWriter.cs
@@ -0,0 +1,606 @@
+//
+// CodeWriter.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Collections.Generic;
+
+using Mono.Collections.Generic;
+
+using Mono.Cecil.Metadata;
+using Mono.Cecil.PE;
+
+using RVA = System.UInt32;
+
+#if !READ_ONLY
+
+namespace Mono.Cecil.Cil {
+
+ sealed class CodeWriter : ByteBuffer {
+
+ readonly RVA code_base;
+ internal readonly MetadataBuilder metadata;
+ readonly Dictionary<uint, MetadataToken> standalone_signatures;
+
+ RVA current;
+ MethodBody body;
+
+ public CodeWriter (MetadataBuilder metadata)
+ : base (0)
+ {
+ this.code_base = metadata.text_map.GetNextRVA (TextSegment.CLIHeader);
+ this.current = code_base;
+ this.metadata = metadata;
+ this.standalone_signatures = new Dictionary<uint, MetadataToken> ();
+ }
+
+ public RVA WriteMethodBody (MethodDefinition method)
+ {
+ var rva = BeginMethod ();
+
+ if (IsUnresolved (method))
+ WriteUnresolvedMethodBody (method);
+ else
+ WriteResolvedMethodBody (method);
+
+ EndMethod ();
+ return rva;
+ }
+
+ static bool IsUnresolved (MethodDefinition method)
+ {
+ return method.HasBody && method.HasImage && method.body == null;
+ }
+
+ void WriteUnresolvedMethodBody (MethodDefinition method)
+ {
+ var code_reader = metadata.module.Read (method, (_, reader) => reader.code);
+
+ MethodSymbols symbols;
+ var buffer = code_reader.PatchRawMethodBody (method, this, out symbols);
+
+ WriteBytes (buffer);
+
+ Align (4);
+
+ if (symbols.Offsets.IsNullOrEmpty ())
+ return;
+
+ symbols.method_token = method.token;
+ symbols.local_var_token = GetLocalVarToken (buffer, symbols);
+
+ var symbol_writer = metadata.symbol_writer;
+ if (symbol_writer != null)
+ symbol_writer.Write (symbols);
+ }
+
+ static MetadataToken GetLocalVarToken (ByteBuffer buffer, MethodSymbols symbols)
+ {
+ if (symbols.variables.IsNullOrEmpty ())
+ return MetadataToken.Zero;
+
+ buffer.position = 8;
+ return new MetadataToken (buffer.ReadUInt32 ());
+ }
+
+ void WriteResolvedMethodBody (MethodDefinition method)
+ {
+ body = method.Body;
+ ComputeHeader ();
+ if (RequiresFatHeader ())
+ WriteFatHeader ();
+ else
+ WriteByte ((byte) (0x2 | (body.CodeSize << 2))); // tiny
+
+ WriteInstructions ();
+
+ if (body.HasExceptionHandlers)
+ WriteExceptionHandlers ();
+
+ var symbol_writer = metadata.symbol_writer;
+ if (symbol_writer != null)
+ symbol_writer.Write (body);
+ }
+
+ void WriteFatHeader ()
+ {
+ var body = this.body;
+ byte flags = 0x3; // fat
+ if (body.InitLocals)
+ flags |= 0x10; // init locals
+ if (body.HasExceptionHandlers)
+ flags |= 0x8; // more sections
+
+ WriteByte (flags);
+ WriteByte (0x30);
+ WriteInt16 ((short) body.max_stack_size);
+ WriteInt32 (body.code_size);
+ WriteMetadataToken (body.HasVariables
+ ? GetStandAloneSignature (body.Variables)
+ : MetadataToken.Zero);
+ }
+
+ void WriteInstructions ()
+ {
+ var instructions = body.Instructions;
+ var items = instructions.items;
+ var size = instructions.size;
+
+ for (int i = 0; i < size; i++) {
+ var instruction = items [i];
+ WriteOpCode (instruction.opcode);
+ WriteOperand (instruction);
+ }
+ }
+
+ void WriteOpCode (OpCode opcode)
+ {
+ if (opcode.Size == 1) {
+ WriteByte (opcode.Op2);
+ } else {
+ WriteByte (opcode.Op1);
+ WriteByte (opcode.Op2);
+ }
+ }
+
+ void WriteOperand (Instruction instruction)
+ {
+ var opcode = instruction.opcode;
+ var operand_type = opcode.OperandType;
+ if (operand_type == OperandType.InlineNone)
+ return;
+
+ var operand = instruction.operand;
+ if (operand == null)
+ throw new ArgumentException ();
+
+ switch (operand_type) {
+ case OperandType.InlineSwitch: {
+ var targets = (Instruction []) operand;
+ WriteInt32 (targets.Length);
+ var diff = instruction.Offset + opcode.Size + (4 * (targets.Length + 1));
+ for (int i = 0; i < targets.Length; i++)
+ WriteInt32 (GetTargetOffset (targets [i]) - diff);
+ break;
+ }
+ case OperandType.ShortInlineBrTarget: {
+ var target = (Instruction) operand;
+ WriteSByte ((sbyte) (GetTargetOffset (target) - (instruction.Offset + opcode.Size + 1)));
+ break;
+ }
+ case OperandType.InlineBrTarget: {
+ var target = (Instruction) operand;
+ WriteInt32 (GetTargetOffset (target) - (instruction.Offset + opcode.Size + 4));
+ break;
+ }
+ case OperandType.ShortInlineVar:
+ WriteByte ((byte) GetVariableIndex ((VariableDefinition) operand));
+ break;
+ case OperandType.ShortInlineArg:
+ WriteByte ((byte) GetParameterIndex ((ParameterDefinition) operand));
+ break;
+ case OperandType.InlineVar:
+ WriteInt16 ((short) GetVariableIndex ((VariableDefinition) operand));
+ break;
+ case OperandType.InlineArg:
+ WriteInt16 ((short) GetParameterIndex ((ParameterDefinition) operand));
+ break;
+ case OperandType.InlineSig:
+ WriteMetadataToken (GetStandAloneSignature ((CallSite) operand));
+ break;
+ case OperandType.ShortInlineI:
+ if (opcode == OpCodes.Ldc_I4_S)
+ WriteSByte ((sbyte) operand);
+ else
+ WriteByte ((byte) operand);
+ break;
+ case OperandType.InlineI:
+ WriteInt32 ((int) operand);
+ break;
+ case OperandType.InlineI8:
+ WriteInt64 ((long) operand);
+ break;
+ case OperandType.ShortInlineR:
+ WriteSingle ((float) operand);
+ break;
+ case OperandType.InlineR:
+ WriteDouble ((double) operand);
+ break;
+ case OperandType.InlineString:
+ WriteMetadataToken (
+ new MetadataToken (
+ TokenType.String,
+ GetUserStringIndex ((string) operand)));
+ break;
+ case OperandType.InlineType:
+ case OperandType.InlineField:
+ case OperandType.InlineMethod:
+ case OperandType.InlineTok:
+ WriteMetadataToken (metadata.LookupToken ((IMetadataTokenProvider) operand));
+ break;
+ default:
+ throw new ArgumentException ();
+ }
+ }
+
+ static int GetTargetOffset (Instruction instruction)
+ {
+ return instruction != null ? instruction.offset : int.MaxValue;
+ }
+
+ uint GetUserStringIndex (string @string)
+ {
+ if (@string == null)
+ return 0;
+
+ return metadata.user_string_heap.GetStringIndex (@string);
+ }
+
+ static int GetVariableIndex (VariableDefinition variable)
+ {
+ return variable.Index;
+ }
+
+ int GetParameterIndex (ParameterDefinition parameter)
+ {
+ if (body.method.HasThis) {
+ if (parameter == body.this_parameter)
+ return 0;
+
+ return parameter.Index + 1;
+ }
+
+ return parameter.Index;
+ }
+
+ bool RequiresFatHeader ()
+ {
+ var body = this.body;
+ return body.CodeSize > 64
+ || body.InitLocals
+ || body.HasVariables
+ || body.HasExceptionHandlers
+ || body.MaxStackSize > 8;
+ }
+
+ void ComputeHeader ()
+ {
+ int offset = 0;
+ var instructions = body.instructions;
+ var items = instructions.items;
+ var count = instructions.size;
+ var stack_size = 0;
+ var max_stack = 0;
+ Dictionary<Instruction, int> stack_sizes = null;
+
+ if (body.HasExceptionHandlers)
+ ComputeExceptionHandlerStackSize (ref stack_sizes);
+
+ for (int i = 0; i < count; i++) {
+ var instruction = items [i];
+ instruction.offset = offset;
+ offset += instruction.GetSize ();
+
+ ComputeStackSize (instruction, ref stack_sizes, ref stack_size, ref max_stack);
+ }
+
+ body.code_size = offset;
+ body.max_stack_size = max_stack;
+ }
+
+ void ComputeExceptionHandlerStackSize (ref Dictionary<Instruction, int> stack_sizes)
+ {
+ var exception_handlers = body.ExceptionHandlers;
+
+ for (int i = 0; i < exception_handlers.Count; i++) {
+ var exception_handler = exception_handlers [i];
+ switch (exception_handler.HandlerType) {
+ case ExceptionHandlerType.Catch:
+ case ExceptionHandlerType.Filter:
+ if (stack_sizes == null)
+ stack_sizes = new Dictionary<Instruction, int> ();
+
+ stack_sizes [exception_handler.HandlerStart] = 1;
+ break;
+ }
+ }
+ }
+
+ static void ComputeStackSize (Instruction instruction, ref Dictionary<Instruction, int> stack_sizes, ref int stack_size, ref int max_stack)
+ {
+ int computed_size;
+ if (stack_sizes != null && stack_sizes.TryGetValue (instruction, out computed_size))
+ stack_size = computed_size;
+
+ max_stack = System.Math.Max (max_stack, stack_size);
+ ComputeStackDelta (instruction, ref stack_size);
+ max_stack = System.Math.Max (max_stack, stack_size);
+
+ CopyBranchStackSize (instruction, ref stack_sizes, stack_size);
+ ComputeStackSize (instruction, ref stack_size);
+ }
+
+ static void CopyBranchStackSize (Instruction instruction, ref Dictionary<Instruction, int> stack_sizes, int stack_size)
+ {
+ if (stack_size == 0)
+ return;
+
+ switch (instruction.opcode.OperandType) {
+ case OperandType.ShortInlineBrTarget:
+ case OperandType.InlineBrTarget:
+ CopyBranchStackSize (ref stack_sizes, (Instruction) instruction.operand, stack_size);
+ break;
+ case OperandType.InlineSwitch:
+ var targets = (Instruction[]) instruction.operand;
+ for (int i = 0; i < targets.Length; i++)
+ CopyBranchStackSize (ref stack_sizes, targets [i], stack_size);
+ break;
+ }
+ }
+
+ static void CopyBranchStackSize (ref Dictionary<Instruction, int> stack_sizes, Instruction target, int stack_size)
+ {
+ if (stack_sizes == null)
+ stack_sizes = new Dictionary<Instruction, int> ();
+
+ int branch_stack_size = stack_size;
+
+ int computed_size;
+ if (stack_sizes.TryGetValue (target, out computed_size))
+ branch_stack_size = System.Math.Max (branch_stack_size, computed_size);
+
+ stack_sizes [target] = branch_stack_size;
+ }
+
+ static void ComputeStackSize (Instruction instruction, ref int stack_size)
+ {
+ switch (instruction.opcode.FlowControl) {
+ case FlowControl.Branch:
+ case FlowControl.Break:
+ case FlowControl.Throw:
+ case FlowControl.Return:
+ stack_size = 0;
+ break;
+ }
+ }
+
+ static void ComputeStackDelta (Instruction instruction, ref int stack_size)
+ {
+ switch (instruction.opcode.FlowControl) {
+ case FlowControl.Call: {
+ var method = (IMethodSignature) instruction.operand;
+ stack_size -= (method.HasParameters ? method.Parameters.Count : 0)
+ + (method.HasThis && instruction.opcode.Code != Code.Newobj ? 1 : 0);
+ stack_size += (method.ReturnType.etype == ElementType.Void ? 0 : 1)
+ + (method.HasThis && instruction.opcode.Code == Code.Newobj ? 1 : 0);
+ break;
+ }
+ default:
+ ComputePopDelta (instruction.opcode.StackBehaviourPop, ref stack_size);
+ ComputePushDelta (instruction.opcode.StackBehaviourPush, ref stack_size);
+ break;
+ }
+ }
+
+ static void ComputePopDelta (StackBehaviour pop_behavior, ref int stack_size)
+ {
+ switch (pop_behavior) {
+ case StackBehaviour.Popi:
+ case StackBehaviour.Popref:
+ case StackBehaviour.Pop1:
+ stack_size--;
+ break;
+ case StackBehaviour.Pop1_pop1:
+ case StackBehaviour.Popi_pop1:
+ case StackBehaviour.Popi_popi:
+ case StackBehaviour.Popi_popi8:
+ case StackBehaviour.Popi_popr4:
+ case StackBehaviour.Popi_popr8:
+ case StackBehaviour.Popref_pop1:
+ case StackBehaviour.Popref_popi:
+ stack_size -= 2;
+ break;
+ case StackBehaviour.Popi_popi_popi:
+ case StackBehaviour.Popref_popi_popi:
+ case StackBehaviour.Popref_popi_popi8:
+ case StackBehaviour.Popref_popi_popr4:
+ case StackBehaviour.Popref_popi_popr8:
+ case StackBehaviour.Popref_popi_popref:
+ stack_size -= 3;
+ break;
+ case StackBehaviour.PopAll:
+ stack_size = 0;
+ break;
+ }
+ }
+
+ static void ComputePushDelta (StackBehaviour push_behaviour, ref int stack_size)
+ {
+ switch (push_behaviour) {
+ case StackBehaviour.Push1:
+ case StackBehaviour.Pushi:
+ case StackBehaviour.Pushi8:
+ case StackBehaviour.Pushr4:
+ case StackBehaviour.Pushr8:
+ case StackBehaviour.Pushref:
+ stack_size++;
+ break;
+ case StackBehaviour.Push1_push1:
+ stack_size += 2;
+ break;
+ }
+ }
+
+ void WriteExceptionHandlers ()
+ {
+ Align (4);
+ var handlers = body.ExceptionHandlers;
+
+ if (handlers.Count < 0x15 && !RequiresFatSection (handlers))
+ WriteSmallSection (handlers);
+ else
+ WriteFatSection (handlers);
+ }
+
+ static bool RequiresFatSection (Collection<ExceptionHandler> handlers)
+ {
+ for (int i = 0; i < handlers.Count; i++) {
+ var handler = handlers [i];
+
+ if (IsFatRange (handler.TryStart, handler.TryEnd))
+ return true;
+
+ if (IsFatRange (handler.HandlerStart, handler.HandlerEnd))
+ return true;
+
+ if (handler.HandlerType == ExceptionHandlerType.Filter
+ && IsFatRange (handler.FilterStart, handler.FilterEnd))
+ return true;
+ }
+
+ return false;
+ }
+
+ static bool IsFatRange (Instruction start, Instruction end)
+ {
+ if (end == null)
+ return true;
+
+ return end.Offset - start.Offset > 255 || start.Offset > 65535;
+ }
+
+ void WriteSmallSection (Collection<ExceptionHandler> handlers)
+ {
+ const byte eh_table = 0x1;
+
+ WriteByte (eh_table);
+ WriteByte ((byte) (handlers.Count * 12 + 4));
+ WriteBytes (2);
+
+ WriteExceptionHandlers (
+ handlers,
+ i => WriteUInt16 ((ushort) i),
+ i => WriteByte ((byte) i));
+ }
+
+ void WriteFatSection (Collection<ExceptionHandler> handlers)
+ {
+ const byte eh_table = 0x1;
+ const byte fat_format = 0x40;
+
+ WriteByte (eh_table | fat_format);
+
+ int size = handlers.Count * 24 + 4;
+ WriteByte ((byte) (size & 0xff));
+ WriteByte ((byte) ((size >> 8) & 0xff));
+ WriteByte ((byte) ((size >> 16) & 0xff));
+
+ WriteExceptionHandlers (handlers, WriteInt32, WriteInt32);
+ }
+
+ void WriteExceptionHandlers (Collection<ExceptionHandler> handlers, Action<int> write_entry, Action<int> write_length)
+ {
+ for (int i = 0; i < handlers.Count; i++) {
+ var handler = handlers [i];
+
+ write_entry ((int) handler.HandlerType);
+
+ write_entry (handler.TryStart.Offset);
+ write_length (GetTargetOffset (handler.TryEnd) - handler.TryStart.Offset);
+
+ write_entry (handler.HandlerStart.Offset);
+ write_length (GetTargetOffset (handler.HandlerEnd) - handler.HandlerStart.Offset);
+
+ WriteExceptionHandlerSpecific (handler);
+ }
+ }
+
+ void WriteExceptionHandlerSpecific (ExceptionHandler handler)
+ {
+ switch (handler.HandlerType) {
+ case ExceptionHandlerType.Catch:
+ WriteMetadataToken (metadata.LookupToken (handler.CatchType));
+ break;
+ case ExceptionHandlerType.Filter:
+ WriteInt32 (handler.FilterStart.Offset);
+ break;
+ default:
+ WriteInt32 (0);
+ break;
+ }
+ }
+
+ public MetadataToken GetStandAloneSignature (Collection<VariableDefinition> variables)
+ {
+ var signature = metadata.GetLocalVariableBlobIndex (variables);
+
+ return GetStandAloneSignatureToken (signature);
+ }
+
+ public MetadataToken GetStandAloneSignature (CallSite call_site)
+ {
+ var signature = metadata.GetCallSiteBlobIndex (call_site);
+ var token = GetStandAloneSignatureToken (signature);
+ call_site.MetadataToken = token;
+ return token;
+ }
+
+ MetadataToken GetStandAloneSignatureToken (uint signature)
+ {
+ MetadataToken token;
+ if (standalone_signatures.TryGetValue (signature, out token))
+ return token;
+
+ token = new MetadataToken (TokenType.Signature, metadata.AddStandAloneSignature (signature));
+ standalone_signatures.Add (signature, token);
+ return token;
+ }
+
+ RVA BeginMethod ()
+ {
+ return current;
+ }
+
+ void WriteMetadataToken (MetadataToken token)
+ {
+ WriteUInt32 (token.ToUInt32 ());
+ }
+
+ void Align (int align)
+ {
+ align--;
+ WriteBytes (((position + align) & ~align) - position);
+ }
+
+ void EndMethod ()
+ {
+ current = (RVA) (code_base + position);
+ }
+ }
+}
+
+#endif
111 Mono.Cecil.Cil/Document.cs
@@ -0,0 +1,111 @@
+//
+// Document.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+namespace Mono.Cecil.Cil {
+
+ public enum DocumentType {
+ Other,
+ Text,
+ }
+
+ public enum DocumentHashAlgorithm {
+ None,
+ MD5,
+ SHA1,
+ }
+
+ public enum DocumentLanguage {
+ Other,
+ C,
+ Cpp,
+ CSharp,
+ Basic,
+ Java,
+ Cobol,
+ Pascal,
+ Cil,
+ JScript,
+ Smc,
+ MCpp,
+ }
+
+ public enum DocumentLanguageVendor {
+ Other,
+ Microsoft,
+ }
+
+ public sealed class Document {
+
+ string url;
+
+ byte type;
+ byte hash_algorithm;
+ byte language;
+ byte language_vendor;
+
+ byte [] hash;
+
+ public string Url {
+ get { return url; }
+ set { url = value; }
+ }
+
+ public DocumentType Type {
+ get { return (DocumentType) type; }
+ set { type = (byte) value; }
+ }
+
+ public DocumentHashAlgorithm HashAlgorithm {
+ get { return (DocumentHashAlgorithm) hash_algorithm; }
+ set { hash_algorithm = (byte) value; }
+ }
+
+ public DocumentLanguage Language {
+ get { return (DocumentLanguage) language; }
+ set { language = (byte) value; }
+ }
+
+ public DocumentLanguageVendor LanguageVendor {
+ get { return (DocumentLanguageVendor) language_vendor; }
+ set { language_vendor = (byte) value; }
+ }
+
+ public byte [] Hash {
+ get { return hash; }
+ set { hash = value; }
+ }
+
+ public Document (string url)
+ {
+ this.url = url;
+ this.hash = Empty<byte>.Array;
+ }
+ }
+}
95 Mono.Cecil.Cil/ExceptionHandler.cs
@@ -0,0 +1,95 @@
+//
+// ExceptionHandler.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+namespace Mono.Cecil.Cil {
+
+ public enum ExceptionHandlerType {
+ Catch = 0,
+ Filter = 1,
+ Finally = 2,
+ Fault = 4,
+ }
+
+ public sealed class ExceptionHandler {
+
+ Instruction try_start;
+ Instruction try_end;
+ Instruction filter_start;
+ Instruction filter_end;
+ Instruction handler_start;
+ Instruction handler_end;
+
+ TypeReference catch_type;
+ ExceptionHandlerType handler_type;
+
+ public Instruction TryStart {
+ get { return try_start; }
+ set { try_start = value; }
+ }
+
+ public Instruction TryEnd {
+ get { return try_end; }
+ set { try_end = value; }
+ }
+
+ public Instruction FilterStart {
+ get { return filter_start; }
+ set { filter_start = value; }
+ }
+
+ public Instruction FilterEnd {
+ get { return filter_end; }
+ set { filter_end = value; }
+ }
+
+ public Instruction HandlerStart {
+ get { return handler_start; }
+ set { handler_start = value; }
+ }
+
+ public Instruction HandlerEnd {
+ get { return handler_end; }
+ set { handler_end = value; }
+ }
+
+ public TypeReference CatchType {
+ get { return catch_type; }
+ set { catch_type = value; }
+ }
+
+ public ExceptionHandlerType HandlerType {
+ get { return handler_type; }
+ set { handler_type = value; }
+ }
+
+ public ExceptionHandler (ExceptionHandlerType handlerType)
+ {
+ this.handler_type = handlerType;
+ }
+ }
+}
394 Mono.Cecil.Cil/ILProcessor.cs
@@ -0,0 +1,394 @@
+//
+// ILProcessor.cs
+//
+// Author:
+// Jb Evain (jbevain@gmail.com)
+//
+// Copyright (c) 2008 - 2010 Jb Evain
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+using Mono.Collections.Generic;
+
+namespace Mono.Cecil.Cil {
+
+ public sealed class ILProcessor {
+
+ readonly MethodBody body;
+ readonly Collection<Instruction> instructions;
+
+ internal ILProcessor (MethodBody body)
+ {
+ this.body = body;
+ this.instructions = body.Instructions;
+ }
+
+ public Instruction Create (OpCode opcode)
+ {
+ if (opcode.OperandType != OperandType.InlineNone)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode);
+ }
+
+ public Instruction Create (OpCode opcode, TypeReference type)
+ {
+ if (type == null)
+ throw new ArgumentNullException ("type");
+ if (opcode.OperandType != OperandType.InlineType &&
+ opcode.OperandType != OperandType.InlineTok)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, type);
+ }
+
+ public Instruction Create (OpCode opcode, CallSite site)
+ {
+ if (site == null)
+ throw new ArgumentNullException ("site");
+ if (opcode.Code != Code.Calli)
+ throw new ArgumentException ("code");
+
+ return FinalCreate (opcode, site);
+ }
+
+ public Instruction Create (OpCode opcode, MethodReference method)
+ {
+ if (method == null)
+ throw new ArgumentNullException ("method");
+ if (opcode.OperandType != OperandType.InlineMethod &&
+ opcode.OperandType != OperandType.InlineTok)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, method);
+ }
+
+ public Instruction Create (OpCode opcode, FieldReference field)
+ {
+ if (field == null)
+ throw new ArgumentNullException ("field");
+ if (opcode.OperandType != OperandType.InlineField &&
+ opcode.OperandType != OperandType.InlineTok)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, field);
+ }
+
+ public Instruction Create (OpCode opcode, string value)
+ {
+ if (value == null)
+ throw new ArgumentNullException ("value");
+ if (opcode.OperandType != OperandType.InlineString)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, sbyte value)
+ {
+ if (opcode.OperandType != OperandType.ShortInlineI &&
+ opcode != OpCodes.Ldc_I4_S)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, byte value)
+ {
+ if (opcode.OperandType == OperandType.ShortInlineVar)
+ return Create (opcode, body.Variables [value]);
+
+ if (opcode.OperandType == OperandType.ShortInlineArg)
+ return Create (opcode, body.GetParameter (value));
+
+ if (opcode.OperandType != OperandType.ShortInlineI ||
+ opcode == OpCodes.Ldc_I4_S)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, int value)
+ {
+ if (opcode.OperandType == OperandType.InlineVar)
+ return Create (opcode, body.Variables [value]);
+
+ if (opcode.OperandType == OperandType.InlineArg)
+ return Create (opcode, body.GetParameter (value));
+
+ if (opcode.OperandType != OperandType.InlineI)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, long value)
+ {
+ if (opcode.OperandType != OperandType.InlineI8)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, float value)
+ {
+ if (opcode.OperandType != OperandType.ShortInlineR)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, double value)
+ {
+ if (opcode.OperandType != OperandType.InlineR)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, value);
+ }
+
+ public Instruction Create (OpCode opcode, Instruction label)
+ {
+ if (label == null)
+ throw new ArgumentNullException ("label");
+ if (opcode.OperandType != OperandType.InlineBrTarget &&
+ opcode.OperandType != OperandType.ShortInlineBrTarget)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, label);
+ }
+
+ public Instruction Create (OpCode opcode, Instruction [] targets)
+ {
+ if (targets == null)
+ throw new ArgumentNullException ("targets");
+ if (opcode.OperandType != OperandType.InlineSwitch)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, targets);
+ }
+
+ public Instruction Create (OpCode opcode, VariableDefinition variable)
+ {
+ if (variable == null)
+ throw new ArgumentNullException ("variable");
+ if (opcode.OperandType != OperandType.ShortInlineVar &&
+ opcode.OperandType != OperandType.InlineVar)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, variable);
+ }
+
+ public Instruction Create (OpCode opcode, ParameterDefinition parameter)
+ {
+ if (parameter == null)
+ throw new ArgumentNullException ("parameter");
+ if (opcode.OperandType != OperandType.ShortInlineArg &&
+ opcode.OperandType != OperandType.InlineArg)
+ throw new ArgumentException ("opcode");
+
+ return FinalCreate (opcode, parameter);
+ }
+
+ static Instruction FinalCreate (OpCode opcode)
+ {
+ return FinalCreate (opcode, null);
+ }
+
+ static Instruction FinalCreate (OpCode opcode, object operand)
+ {
+ return new Instruction (opcode, operand);
+ }
+
+ public Instruction Emit (OpCode opcode)
+ {
+ var instruction = Create (opcode);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, TypeReference type)
+ {
+ var instruction = Create (opcode, type);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, MethodReference method)
+ {
+ var instruction = Create (opcode, method);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, CallSite site)
+ {
+ var instruction = Create (opcode, site);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, FieldReference field)
+ {
+ var instruction = Create (opcode, field);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, string value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, byte value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, sbyte value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, int value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, long value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, float value)
+ {
+ var instruction = Create (opcode, value);
+ Append (instruction);
+ return instruction;
+ }
+
+ public Instruction Emit (OpCode opcode, double value)
+ {
+ var instruction = Create (opcode, value);
+