public static void ImportReferences(ModuleDefinition m) { foreach (var t in m.Types) { TypeVisitor.VisitAllMemberRefs(t, x => { if (x is MethodReference) { return(m.ImportReference((MethodReference)x)); } if (x is FieldReference) { return(m.ImportReference((FieldReference)x)); } return(x); }); TypeVisitor.ReplaceTypeRefs(t, x => m.ImportReference(x)); foreach (var c in t.CustomAttributes) { c.Constructor = m.ImportReference(c.Constructor); } foreach (var me in t.Methods) { foreach (var c in me.CustomAttributes) { c.Constructor = m.ImportReference(c.Constructor); } } } }
public void TransformIL(TypeDefinition t) { TypeVisitor.ReplaceTypeRefs(t, x => { if (x.IsArray) { return(GetArrayType(GetArraySize(x))); } return(x); } ); foreach (var m in t.Methods) { if (!m.HasBody) { continue; } var ilp = m.Body.GetILProcessor(); for (int idx = 0; idx < m.Body.Instructions.Count; idx++) { var inst = m.Body.Instructions[idx]; if (IsLdelm(inst.OpCode)) { var at = GetArrayType(GetArraySize(inst.OpCode)); var getter = at.Methods.Single(x => x.Name == "Get"); ilp.Replace(inst, ilp.Create(OpCodes.Call, getter)); } if (IsStelm(inst.OpCode)) { var at = GetArrayType(GetArraySize(inst.OpCode)); var setter = at.Methods.Single(x => x.Name == "Set"); ilp.Replace(inst, ilp.Create(OpCodes.Call, setter)); } if (inst.OpCode == OpCodes.Ldlen) { // todo add Array_GetLength function var at = GetArrayType(EArrayType._1); var length = at.Methods.Single(x => x.Name == "GetLength"); ilp.Replace(inst, ilp.Create(OpCodes.Call, length)); } if (inst.OpCode == OpCodes.Newarr) { var elemType = (inst.Operand as TypeReference).Resolve(); var arrayType = GetArrayType(GetArraySize(elemType)); var newRef = arrayType.Methods.Single(x => x.Name == "new"); ilp.Replace(inst, ilp.Create(OpCodes.Call, newRef)); } if (inst.OpCode == OpCodes.Ldelema || inst.OpCode == OpCodes.Ldelem_Any || inst.OpCode == OpCodes.Stelem_Any) { throw new NotImplementedException(); } } } }
public static void Optimize(TypeDefinition t) { // C only knows pointers but no references. Replace with pointers; //Type TypeVisitor.ReplaceTypeRefs(t, x => x.IsByReference ? new PointerType(x.GetElementType()) : x); }