Skip to content

Commit

Permalink
Port more code; remove cecil refs
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Nov 2, 2012
1 parent 7091617 commit 65e6887
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 874 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[submodule "cecil"]
path = cecil
url = [email protected]:0xd4d/cecil.git
[submodule "dot10"]
path = dot10
url = e:/work/dot10.git
95 changes: 94 additions & 1 deletion blocks/DotNetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ You should have received a copy of the GNU General Public License

using System;
using System.Collections.Generic;
#if PORT
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Metadata;
#endif

//TODO: Remove these
using DN = dot10.DotNet;
Expand All @@ -38,6 +40,7 @@ public enum FrameworkType {
Zune,
}

#if PORT
class TypeCache {
ModuleDefinition module;
de4dot.blocks.OLD_REMOVE.TypeDefinitionDict<TypeDefinition> typeRefToDef = new de4dot.blocks.OLD_REMOVE.TypeDefinitionDict<TypeDefinition>();
Expand All @@ -56,7 +59,9 @@ public TypeDefinition lookup(TypeReference typeReference) {
return typeRefToDef.find(typeReference);
}
}
#endif

#if PORT
public class TypeCaches {
Dictionary<ModuleDefinition, TypeCache> typeCaches = new Dictionary<ModuleDefinition, TypeCache>();

Expand All @@ -82,7 +87,9 @@ public TypeDefinition lookup(ModuleDefinition module, TypeReference typeReferenc
return typeCache.lookup(typeReference);
}
}
#endif

#if PORT
public class CallCounter {
Dictionary<de4dot.blocks.OLD_REMOVE.MethodReferenceAndDeclaringTypeKey, int> calls = new Dictionary<de4dot.blocks.OLD_REMOVE.MethodReferenceAndDeclaringTypeKey, int>();

Expand Down Expand Up @@ -111,7 +118,9 @@ public MethodReference most(out int numCalls) {
return method;
}
}
#endif

#if PORT
public class MethodCalls {
Dictionary<string, int> methodCalls = new Dictionary<string, int>(StringComparer.Ordinal);

Expand Down Expand Up @@ -142,9 +151,12 @@ public bool called(string methodFullName) {
return count(methodFullName) != 0;
}
}
#endif

public static class DotNetUtils {
#if PORT
public static readonly TypeCaches typeCaches = new TypeCaches();
#endif

public static DN.TypeDef getModuleType(DN.ModuleDef module) {
return module.GlobalType;
Expand Down Expand Up @@ -176,6 +188,7 @@ public static bool isEmptyObfuscated(DN.MethodDef method) {
return true;
}

#if PORT
public static FieldDefinition findFieldType(TypeDefinition typeDefinition, string typeName, bool isStatic) {
if (typeDefinition == null)
return null;
Expand Down Expand Up @@ -208,6 +221,7 @@ public static IEnumerable<MethodDefinition> findMethods(IEnumerable<MethodDefini
next: ;
}
}
#endif

public static bool isDelegate(DN.IType type) {
if (type == null)
Expand All @@ -220,18 +234,21 @@ public static bool derivesFromDelegate(DN.TypeDef type) {
return type != null && isDelegate(type.BaseType);
}

#if PORT
public static bool isSameAssembly(TypeReference type, string assembly) {
return MemberReferenceHelper.getCanonicalizedScopeName(type.Scope) == assembly.ToLowerInvariant();
}

public static bool isMethod(MethodReference method, string returnType, string parameters) {
return method != null && method.FullName == returnType + " " + method.DeclaringType.FullName + "::" + method.Name + parameters;
}
#endif

public static bool isMethod(DN.IMethod method, string returnType, string parameters) {
return method != null && method.FullName == returnType + " " + method.DeclaringType.FullName + "::" + method.Name + parameters;
}

#if PORT
public static bool hasPinvokeMethod(TypeDefinition type, string methodName) {
return getPInvokeMethod(type, methodName) != null;
}
Expand Down Expand Up @@ -319,6 +336,7 @@ public static MethodDefinition getMethod(TypeDefinition type, string returnType,
}
return null;
}
#endif

public static DN.MethodDef getMethod2(DN.ModuleDef module, DN.IMethod method) {
if (method == null)
Expand Down Expand Up @@ -354,6 +372,7 @@ public static DN.MethodDef getMethod(DN.TypeDef type, DN.IMethod methodRef) {
return type.FindMethod(methodRef.Name, methodRef.MethodSig);
}

#if PORT
public static IEnumerable<MethodDefinition> getNormalMethods(TypeDefinition type) {
foreach (var method in type.Methods) {
if (method.HasPInvokeInfo)
Expand Down Expand Up @@ -451,6 +470,7 @@ public static IList<string> getCodeStrings(MethodDefinition method) {
}
return strings;
}
#endif

public static IList<string> getCodeStrings(DN.MethodDef method) {
var strings = new List<string>();
Expand Down Expand Up @@ -493,6 +513,7 @@ static string removeFromNullChar(string s) {
return s.Substring(0, index);
}

#if PORT
// Copies most things but not everything
public static MethodDefinition clone(MethodDefinition method) {
var newMethod = new MethodDefinition(method.Name, method.Attributes, method.MethodReturnType.ReturnType);
Expand All @@ -511,12 +532,14 @@ public static MethodDefinition clone(MethodDefinition method) {
copyBodyFromTo(method, newMethod);
return newMethod;
}
#endif

// Copies most things but not everything
public static DN.MethodDef clone(DN.MethodDef method) {
return null; //TODO:
}

#if PORT
public static Instruction clone(Instruction instr) {
return new Instruction {
Offset = instr.Offset,
Expand Down Expand Up @@ -573,7 +596,57 @@ static Instruction getInstruction(IList<Instruction> instructions, IDictionary<I
return null;
return instructions[instructionToIndex[instruction]];
}
#endif

public static void copyBody(DN.MethodDef method, out IList<DNE.Instruction> instructions, out IList<DNE.ExceptionHandler> exceptionHandlers) {
if (method == null || !method.HasCilBody) {
instructions = new List<DNE.Instruction>();
exceptionHandlers = new List<DNE.ExceptionHandler>();
return;
}

var oldInstrs = method.CilBody.Instructions;
var oldExHandlers = method.CilBody.ExceptionHandlers;
instructions = new List<DNE.Instruction>(oldInstrs.Count);
exceptionHandlers = new List<DNE.ExceptionHandler>(oldExHandlers.Count);
var oldToIndex = Utils.createObjectToIndexDictionary(oldInstrs);

foreach (var oldInstr in oldInstrs)
instructions.Add(oldInstr.Clone());

foreach (var newInstr in instructions) {
var operand = newInstr.Operand;
if (operand is DNE.Instruction)
newInstr.Operand = instructions[oldToIndex[(DNE.Instruction)operand]];
else if (operand is IList<DNE.Instruction>) {
var oldArray = (IList<DNE.Instruction>)operand;
var newArray = new DNE.Instruction[oldArray.Count];
for (int i = 0; i < oldArray.Count; i++)
newArray[i] = instructions[oldToIndex[oldArray[i]]];
newInstr.Operand = newArray;
}
}

foreach (var oldEx in oldExHandlers) {
var newEx = new DNE.ExceptionHandler(oldEx.HandlerType) {
TryStart = getInstruction(instructions, oldToIndex, oldEx.TryStart),
TryEnd = getInstruction(instructions, oldToIndex, oldEx.TryEnd),
FilterStart = getInstruction(instructions, oldToIndex, oldEx.FilterStart),
HandlerStart = getInstruction(instructions, oldToIndex, oldEx.HandlerStart),
HandlerEnd = getInstruction(instructions, oldToIndex, oldEx.HandlerEnd),
CatchType = oldEx.CatchType,
};
exceptionHandlers.Add(newEx);
}
}

static DNE.Instruction getInstruction(IList<DNE.Instruction> instructions, IDictionary<DNE.Instruction, int> instructionToIndex, DNE.Instruction instruction) {
if (instruction == null)
return null;
return instructions[instructionToIndex[instruction]];
}

#if PORT
public static void restoreBody(MethodDefinition method, IEnumerable<Instruction> instructions, IEnumerable<ExceptionHandler> exceptionHandlers) {
if (method == null || !method.HasBody)
return;
Expand All @@ -588,7 +661,7 @@ public static void restoreBody(MethodDefinition method, IEnumerable<Instruction>
foreach (var eh in exceptionHandlers)
bodyExceptionHandlers.Add(eh);
}

#endif

public static void restoreBody(DN.MethodDef method, IEnumerable<DNE.Instruction> instructions, IEnumerable<DNE.ExceptionHandler> exceptionHandlers) {
if (method == null || method.CilBody == null)
Expand All @@ -605,6 +678,7 @@ public static void restoreBody(DN.MethodDef method, IEnumerable<DNE.Instruction>
bodyExceptionHandlers.Add(eh);
}

#if PORT
public static void copyBodyFromTo(MethodDefinition fromMethod, MethodDefinition toMethod) {
if (fromMethod == toMethod)
return;
Expand Down Expand Up @@ -712,6 +786,7 @@ public static bool hasReturnValue(IMethodSignature method) {
type = ((TypeSpecification)type).ElementType;
return type.EType != ElementType.Void;
}
#endif

public static bool hasReturnValue(DN.IMethod method) {
if (method == null || method.MethodSig == null)
Expand All @@ -720,6 +795,7 @@ public static bool hasReturnValue(DN.IMethod method) {
return method.MethodSig.RetType.ElementType != DN.ElementType.Void;
}

#if PORT
public static void updateStack(Instruction instr, ref int stack, bool methodHasReturnValue) {
int pushes, pops;
calculateStackUsage(instr, methodHasReturnValue, out pushes, out pops);
Expand Down Expand Up @@ -930,13 +1006,21 @@ public static ParameterDefinition getParameter(IList<ParameterDefinition> parame
return parameters[index];
return null;
}
#endif

public static DN.Parameter getParameter(IList<DN.Parameter> parameters, int index) {
if (0 <= index && index < parameters.Count)
return parameters[index];
return null;
}

public static DN.TypeSig getArg(IList<DN.TypeSig> args, int index) {
if (0 <= index && index < args.Count)
return args[index];
return null;
}

#if PORT
public static List<TypeReference> getArgs(MethodReference method) {
var args = new List<TypeReference>(method.Parameters.Count + 1);
if (method.HasImplicitThis)
Expand All @@ -945,6 +1029,7 @@ public static List<TypeReference> getArgs(MethodReference method) {
args.Add(arg.ParameterType);
return args;
}
#endif

public static List<DN.TypeSig> getArgs(DN.IMethod method) {
var sig = method.MethodSig;
Expand All @@ -956,6 +1041,7 @@ public static List<TypeReference> getArgs(MethodReference method) {
return args;
}

#if PORT
public static TypeReference getArgType(MethodReference method, Instruction instr) {
return getArgType(getArgs(method), instr);
}
Expand All @@ -976,6 +1062,7 @@ public static int getArgsCount(MethodReference method) {
count++;
return count;
}
#endif

public static int getArgsCount(DN.IMethod method) {
var sig = method.MethodSig;
Expand All @@ -987,6 +1074,7 @@ public static int getArgsCount(DN.IMethod method) {
return count;
}

#if PORT
// Doesn't fix everything (eg. T[] aren't replaced with eg. int[], but T -> int will be fixed)
public static IList<TypeReference> replaceGenericParameters(GenericInstanceType typeOwner, GenericInstanceMethod methodOwner, IList<TypeReference> types) {
//TODO: You should use MemberRefInstance.cs
Expand Down Expand Up @@ -1027,6 +1115,7 @@ public static Instruction getInstruction(IList<Instruction> instructions, ref in
}
return null;
}
#endif

public static DNE.Instruction getInstruction(IList<DNE.Instruction> instructions, ref int index) {
for (int i = 0; i < 10; i++) {
Expand All @@ -1047,6 +1136,7 @@ public static DNE.Instruction getInstruction(IList<DNE.Instruction> instructions
return null;
}

#if PORT
public static PropertyDefinition createPropertyDefinition(string name, TypeReference propType, MethodDefinition getter, MethodDefinition setter) {
return new PropertyDefinition(name, PropertyAttributes.None, propType) {
MetadataToken = nextPropertyToken(),
Expand Down Expand Up @@ -1115,6 +1205,7 @@ public static TypeReference findOrCreateTypeReference(ModuleDefinition module, A
typeRef.IsValueType = isValueType;
return typeRef;
}
#endif

public static DN.TypeDefOrRefSig findOrCreateTypeReference(DN.ModuleDef module, DN.AssemblyRef asmRef, string ns, string name, bool isValueType) {
var typeRef = module.UpdateRowId(new DN.TypeRefUser(module, ns, name, asmRef));
Expand All @@ -1124,6 +1215,7 @@ public static DN.TypeDefOrRefSig findOrCreateTypeReference(DN.ModuleDef module,
return new DN.ClassSig(typeRef);
}

#if PORT
public static FrameworkType getFrameworkType(ModuleDefinition module) {
foreach (var modRef in module.AssemblyReferences) {
if (modRef.Name != "mscorlib")
Expand Down Expand Up @@ -1244,5 +1336,6 @@ public static ModuleReference addModuleReference(ModuleDefinition module, Module
module.ModuleReferences.Add(newModRef);
return newModRef;
}
#endif
}
}
3 changes: 3 additions & 0 deletions blocks/MemberRefInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/

#if PORT

// Create a new type, method, etc, where all generic parameters have been replaced with the
// corresponding generic argument.

Expand Down Expand Up @@ -226,3 +228,4 @@ PropertyReference makeInstance() {
}
}
}
#endif
2 changes: 2 additions & 0 deletions blocks/MemberReferenceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/

#if PORT
using System;
using System.Collections.Generic;
using Mono.Cecil;
Expand Down Expand Up @@ -858,3 +859,4 @@ public static bool compareTypes(TypeReference a, TypeReference b) {
}
}
}
#endif
2 changes: 2 additions & 0 deletions blocks/TypeReferenceUpdaterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
*/

#if PORT
using System;
using Mono.Cecil;

Expand Down Expand Up @@ -88,3 +89,4 @@ protected virtual TypeReference updateTypeReference(TypeReference a) {
}
}
}
#endif
4 changes: 0 additions & 4 deletions blocks/blocks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@
<Compile Include="Utils.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\cecil\Mono.Cecil.csproj">
<Project>{D68133BD-1E63-496E-9EDE-4FBDBF77B486}</Project>
<Name>Mono.Cecil</Name>
</ProjectReference>
<ProjectReference Include="..\dot10\src\dot10.csproj">
<Project>{FDFC1237-143F-4919-8318-4926901F4639}</Project>
<Name>dot10</Name>
Expand Down
1 change: 0 additions & 1 deletion cecil
Submodule cecil deleted from 119a3d
Loading

0 comments on commit 65e6887

Please sign in to comment.