internal void LoadStage2() { Console.WriteLine("================================================== Stage 2: {0} ==================================================", File.ReferenceName); for (int typeIndex = 0; typeIndex < Types.Count; ++typeIndex) { IRType type = Types[typeIndex]; TypeDefData typeDefData = File.TypeDefTable[typeIndex]; if (typeDefData.Extends.Type != TypeDefRefOrSpecIndex.TypeDefRefOrSpecType.TypeDef || typeDefData.Extends.TypeDef != null) { type.BaseType = AppDomain.PresolveType(typeDefData.Extends); } for (int fieldIndex = 0; fieldIndex < type.Fields.Count; ++fieldIndex) { IRField field = type.Fields[fieldIndex]; field.Type = AppDomain.PresolveType(typeDefData.FieldList[fieldIndex].ExpandedSignature); if (field.Type == null) { throw new Exception(); } } } for (int methodIndex = 0; methodIndex < Methods.Count; ++methodIndex) { IRMethod method = Methods[methodIndex]; MethodDefData methodDefData = File.MethodDefTable[methodIndex]; var mGenParams = File.GenericParamTable.Where(gp => gp.Owner.Type == TypeOrMethodDefIndex.TypeOrMethodDefType.MethodDef && gp.Owner.MethodDef == methodDefData).ToList(); for (int i = 0; i < mGenParams.Count; i++) { method.GenericParameters.Add(IRType.GetMVarPlaceholder(mGenParams[i].Number)); } method.ReturnType = AppDomain.PresolveType(methodDefData.ExpandedSignature.RetType); for (int parameterIndex = 0; parameterIndex < method.Parameters.Count; ++parameterIndex) { IRParameter parameter = method.Parameters[parameterIndex]; parameter.Type = AppDomain.PresolveType(methodDefData.ExpandedSignature.Params[parameterIndex]); } for (int localIndex = 0; localIndex < method.Locals.Count; ++localIndex) { IRLocal local = method.Locals[localIndex]; local.Type = AppDomain.PresolveType(methodDefData.Body.ExpandedLocalVarSignature.LocalVars[localIndex]); } } for (int methodIndex = 0; methodIndex < Methods.Count; ++methodIndex) { IRMethod method = Methods[methodIndex]; MethodDefData methodDefData = File.MethodDefTable[methodIndex]; if (methodDefData.ExpandedSignature.HasThis && !methodDefData.ExpandedSignature.ExplicitThis) { IRParameter implicitThis = new IRParameter(this); implicitThis.ParentMethod = method; implicitThis.Type = method.ParentType.IsValueType ? AppDomain.GetPointerType(method.ParentType) : method.ParentType; method.Parameters.Insert(0, implicitThis); } } }
public IRType PresolveGenericType(IRType pGenericType, List <IRType> pGenericParameterTypes) { IRType type = new IRType(pGenericType.Assembly); type.Name = pGenericType.Name; type.Namespace = pGenericType.Namespace; type.GenericType = pGenericType; type.GenericParameters.AddRange(pGenericParameterTypes); return(type); }
/// <summary> /// Creates a shallow copy of this field. /// </summary> /// <returns>The shallow copy.</returns> public IRField Clone(IRType newParent) { IRField f = new IRField(this.Assembly); f.Name = this.Name; f.ParentType = newParent; f.Type = this.Type; f.mParentField = this.Type == null ? this : null; return f; }
/// <summary> /// Creates a shallow copy of this field. /// </summary> /// <returns>The shallow copy.</returns> public IRField Clone(IRType newParent) { IRField f = new IRField(this.Assembly); f.Name = this.Name; f.ParentType = newParent; f.Type = this.Type; f.mParentField = this.Type == null ? this : null; return(f); }
/// <summary> /// Resolve any generic types used in this type. /// </summary> /// <param name="selfReference"></param> /// <param name="typeParams"></param> /// <param name="methodParams"></param> public void Resolve(ref IRType selfReference, GenericParameterCollection typeParams, GenericParameterCollection methodParams) { if (!Resolved) { if (IsGeneric) { if (IsTemporaryVar) { selfReference = typeParams[this.TemporaryVarOrMVarIndex]; } else if (IsTemporaryMVar) { selfReference = methodParams[this.TemporaryVarOrMVarIndex]; } else if (IsArrayType) { IRType elemType = ArrayType; elemType.Resolve(ref elemType, typeParams, methodParams); selfReference = Assembly.AppDomain.GetArrayType(elemType); } else if (this.GenericParameters.Resolved) { IRType tp = null; if (!GenericTypes.TryGetValue(this, out tp)) { tp = this.GenericType.Clone(); tp.GenericParameters.Substitute(typeParams, methodParams); GenericTypes[tp] = tp; for (int i = 0; i < tp.GenericParameters.Count; i++) { tp.GenericParameters[i] = this.GenericParameters[i]; } for (int i = 0; i < tp.Methods.Count; i++) { tp.Methods[i] = tp.Methods[i].Resolved ? tp.Methods[i] : tp.Methods[i].Clone(tp); } tp.Substitute(typeParams, methodParams); } selfReference = tp; } else { #warning Need to do the rest of this resolution. } } else { Substitute(GenericParameterCollection.Empty, GenericParameterCollection.Empty); } } }
public IRType GetPointerType(IRType pValueAtPointerType) { IRType type = null; if (!PointerTypes.TryGetValue(pValueAtPointerType, out type)) { type = System_IntPtr.Clone(); type.PointerType = pValueAtPointerType; PointerTypes.Add(pValueAtPointerType, type); } return(type); }
public IRMethod PresolveMethod(MemberRefData pMemberRefData) { if (!pMemberRefData.IsMethodRef) { throw new ArgumentException(); } switch (pMemberRefData.Class.Type) { case MemberRefParentIndex.MemberRefParentType.TypeDef: { IRType type = PresolveType(pMemberRefData.Class.TypeDef); foreach (IRMethod method in type.Methods) { if (method.CompareSignature(pMemberRefData)) { return(method); } } throw new NullReferenceException(); } case MemberRefParentIndex.MemberRefParentType.TypeRef: { IRType type = PresolveType(pMemberRefData.Class.TypeRef); foreach (IRMethod method in type.Methods) { if (method.CompareSignature(pMemberRefData)) { return(method); } } throw new NullReferenceException(); } case MemberRefParentIndex.MemberRefParentType.TypeSpec: { IRType type = PresolveType(pMemberRefData.Class.TypeSpec); foreach (IRMethod method in type.GenericType.Methods) { if (method.CompareSignature(pMemberRefData)) { return(PresolveGenericMethod(method, new List <IRType>(), type.GenericParameters.ToList())); } } throw new NullReferenceException(); } } throw new NullReferenceException(); }
public IRField PresolveField(MemberRefData pMemberRefData) { if (!pMemberRefData.IsFieldRef) { throw new ArgumentException(); } switch (pMemberRefData.Class.Type) { case MemberRefParentIndex.MemberRefParentType.TypeDef: { IRType type = PresolveType(pMemberRefData.Class.TypeDef); foreach (IRField field in type.Fields) { if (field.CompareSignature(pMemberRefData)) { return(field); } } throw new NullReferenceException(); } case MemberRefParentIndex.MemberRefParentType.TypeRef: { IRType type = PresolveType(pMemberRefData.Class.TypeRef); foreach (IRField field in type.Fields) { if (field.CompareSignature(pMemberRefData)) { return(field); } } throw new NullReferenceException(); } case MemberRefParentIndex.MemberRefParentType.TypeSpec: { IRType type = PresolveType(pMemberRefData.Class.TypeSpec); foreach (IRField field in type.GenericType.Fields) { if (field.CompareSignature(pMemberRefData)) { return(field); } } throw new NullReferenceException(); } } throw new NullReferenceException(); }
public uint AddLinearizedLocal(Stack <IRStackObject> pStack, IRType pType) { IRLocal local = null; if (!LinearizedStackLocalLookup.TryGetValue(new Tuple <int, IRType>(pStack.Count, pType), out local)) { local = new IRLocal(Assembly); local.ParentMethod = this; local.Type = pType; local.Index = (uint)Locals.Count; Locals.Add(local); LinearizedStackLocalLookup[new Tuple <int, IRType>(pStack.Count, pType)] = local; } return(local.Index); }
public IRType GetArrayType(IRType pElementType) { IRType type = null; if (!ArrayTypes.TryGetValue(pElementType, out type)) { type = System_Array.Clone(); type.ArrayType = pElementType; ArrayTypes.Add(pElementType, type); } if (pElementType.IsTemporaryVar != type.ArrayType.IsTemporaryVar || pElementType.IsTemporaryMVar != type.ArrayType.IsTemporaryMVar) { throw new Exception(); } return(type); }
/// <summary> /// This creates a shallow clone of this method, but /// does a deep clone of it's instructions, parameters, and locals. /// </summary> /// <param name="newParent">The parent for the new method.</param> /// <returns>The clone of this method.</returns> public IRMethod Clone(IRType newParent) { IRMethod m = new IRMethod(this.Assembly); m.GenericMethod = this.GenericMethod; m.GenericParameters.AddRange(this.GenericParameters); this.Instructions.ForEach(i => m.Instructions.Add(i.Clone(m))); this.Locals.ForEach(l => m.Locals.Add(l.Clone(m))); this.Parameters.ForEach(p => m.Parameters.Add(p.Clone(m))); m.MaximumStackDepth = this.MaximumStackDepth; m.Name = this.Name; m.ParentType = newParent; m.ReturnType = this.ReturnType; m.IsStatic = this.IsStatic; // TODO: Fix Branch/Switch/Leave IRInstruction's to new method instructions based on IRIndex's return(m); }
/// <summary> /// This creates a shallow copy of this <see cref="IRType"/>. /// </summary> /// <returns>The new IRType.</returns> public IRType Clone() { IRType t = new IRType(this.Assembly); this.Fields.ForEach(f => t.Fields.Add(f.Clone(t))); this.Methods.ForEach(m => t.Methods.Add(m.Clone(t))); t.NestedTypes.AddRange(this.NestedTypes); t.GenericParameters.AddRange(this.GenericParameters); t.ArrayType = this.ArrayType; t.BaseType = this.BaseType; t.GenericType = this.GenericType; t.IsTemporaryMVar = this.IsTemporaryMVar; t.IsTemporaryVar = this.IsTemporaryVar; t.Name = this.Name; t.Namespace = this.Namespace; t.PointerType = this.PointerType; t.TemporaryVarOrMVarIndex = this.TemporaryVarOrMVarIndex; return(t); }
public bool CompareSignatures(SigType pSigTypeA, SigType pSigTypeB) { IRType typeA = PresolveType(pSigTypeA); IRType typeB = PresolveType(pSigTypeB); if (typeA.ArrayType != null) { if (typeB.ArrayType == null) { return(false); } return(typeA.ArrayType == typeB.ArrayType); } if (typeA.PointerType != null) { if (typeB.PointerType == null) { return(false); } return(typeA.PointerType == typeB.PointerType); } if (typeA.IsTemporaryVar || typeB.IsTemporaryVar) { if (!typeA.IsTemporaryVar || !typeB.IsTemporaryVar) { return(false); } return(typeA.TemporaryVarOrMVarIndex == typeB.TemporaryVarOrMVarIndex); } if (typeA.IsTemporaryMVar || typeB.IsTemporaryMVar) { if (!typeA.IsTemporaryMVar || !typeB.IsTemporaryMVar) { return(false); } return(typeA.TemporaryVarOrMVarIndex == typeB.TemporaryVarOrMVarIndex); } return(typeA == typeB); }
internal void LoadStage1() { Console.WriteLine("================================================== Stage 1: {0} ==================================================", File.ReferenceName); foreach (TypeDefData typeDefData in File.TypeDefTable) { Types.Add(new IRType(this)); } foreach (FieldData fieldData in File.FieldTable) { Fields.Add(new IRField(this)); } foreach (MethodDefData methodDefData in File.MethodDefTable) { Methods.Add(new IRMethod(this)); } for (int typeIndex = 0; typeIndex < Types.Count; ++typeIndex) { IRType type = Types[typeIndex]; TypeDefData typeDefData = File.TypeDefTable[typeIndex]; type.Namespace = typeDefData.TypeNamespace; type.Name = typeDefData.TypeName; var genParams = File.GenericParamTable.Where(gp => gp.Owner.Type == TypeOrMethodDefIndex.TypeOrMethodDefType.TypeDef && gp.Owner.TypeDef == typeDefData).ToList(); for (int i = 0; i < genParams.Count; i++) { type.GenericParameters.Add(IRType.GetVarPlaceholder(genParams[i].Number)); } foreach (FieldData fieldData in typeDefData.FieldList) { IRField field = Fields[fieldData.TableIndex]; field.Name = fieldData.Name; field.ParentType = type; type.Fields.Add(field); } foreach (MethodDefData methodDefData in typeDefData.MethodList) { IRMethod method = Methods[methodDefData.TableIndex]; method.Name = methodDefData.Name; method.ParentType = type; method.IsStatic = (methodDefData.Flags & MethodAttributes.Static) == MethodAttributes.Static; type.Methods.Add(method); foreach (ParamData paramData in methodDefData.ParamList) { IRParameter parameter = new IRParameter(this); parameter.ParentMethod = method; method.Parameters.Add(parameter); } if (methodDefData.Body != null && methodDefData.Body.ExpandedLocalVarSignature != null) { method.MaximumStackDepth = methodDefData.Body.MaxStack; foreach (SigLocalVar sigLocalVar in methodDefData.Body.ExpandedLocalVarSignature.LocalVars) { IRLocal local = new IRLocal(this); local.ParentMethod = method; local.Index = (uint)method.Locals.Count; method.Locals.Add(local); } } } } for (int typeIndex = 0; typeIndex < Types.Count; ++typeIndex) { IRType type = Types[typeIndex]; TypeDefData typeDefData = File.TypeDefTable[typeIndex]; foreach (TypeDefData nestedTypeDefData in typeDefData.NestedClassList) { IRType nestedType = Types[nestedTypeDefData.TableIndex]; nestedType.Namespace = type.Namespace + "." + type.Name; type.NestedTypes.Add(nestedType); } } if (CORLibrary) { AppDomain.CacheCORTypes(this); } }
public uint AddLinearizedLocal(Stack <IRStackObject> pStack, IRType pType) { return(ParentMethod.AddLinearizedLocal(pStack, pType)); }
/// <summary> /// This creates a shallow copy of this <see cref="IRType"/>. /// </summary> /// <returns>The new IRType.</returns> public IRType Clone() { IRType t = new IRType(this.Assembly); this.Fields.ForEach(f => t.Fields.Add(f.Clone(t))); this.Methods.ForEach(m => t.Methods.Add(m.Clone(t))); t.NestedTypes.AddRange(this.NestedTypes); t.GenericParameters.AddRange(this.GenericParameters); t.ArrayType = this.ArrayType; t.BaseType = this.BaseType; t.GenericType = this.GenericType; t.IsTemporaryMVar = this.IsTemporaryMVar; t.IsTemporaryVar = this.IsTemporaryVar; t.Name = this.Name; t.Namespace = this.Namespace; t.PointerType = this.PointerType; t.TemporaryVarOrMVarIndex = this.TemporaryVarOrMVarIndex; return t; }
public IRType ShiftNumericResult(IRType pValueType, IRType pShiftAmountType) { IRType resultType = null; if (pValueType == System_SByte || pValueType == System_Byte || pValueType == System_Int16 || pValueType == System_UInt16 || pValueType == System_Int32 || pValueType == System_UInt32) { if (pShiftAmountType == System_SByte || pShiftAmountType == System_Byte || pShiftAmountType == System_Int16 || pShiftAmountType == System_UInt16 || pShiftAmountType == System_Int32 || pShiftAmountType == System_UInt32 || pShiftAmountType == System_IntPtr || pShiftAmountType == System_UIntPtr) { resultType = System_Int32; } else { throw new ArgumentException(); } } else if (pValueType == System_Int64 || pValueType == System_UInt64) { if (pShiftAmountType == System_Int32 || pShiftAmountType == System_Int64 || pShiftAmountType == System_UInt64) { resultType = System_Int64; } else { throw new ArgumentException(); } } else if (pValueType == System_IntPtr || pValueType == System_UIntPtr) { if (pShiftAmountType == System_SByte || pShiftAmountType == System_Byte || pShiftAmountType == System_Int16 || pShiftAmountType == System_UInt16 || pShiftAmountType == System_Int32 || pShiftAmountType == System_UInt32 || pShiftAmountType == System_IntPtr || pShiftAmountType == System_UIntPtr) { resultType = System_IntPtr; } else { throw new ArgumentException(); } } else { throw new ArgumentException(); } return(resultType); }
public IRType BinaryNumericResult(IRType pValue1Type, IRType pValue2Type) { IRType resultType = null; if (pValue1Type == System_SByte || pValue1Type == System_Byte || pValue1Type == System_Int16 || pValue1Type == System_UInt16 || pValue1Type == System_Char || pValue1Type == System_Int32 || pValue1Type == System_UInt32) { if (pValue2Type == System_SByte || pValue2Type == System_Byte || pValue2Type == System_Int16 || pValue2Type == System_UInt16 || pValue2Type == System_Char || pValue2Type == System_Int32 || pValue2Type == System_UInt32 || pValue2Type.IsEnumType) { resultType = System_Int32; } else if (pValue2Type == System_IntPtr || pValue2Type == System_UIntPtr) { resultType = System_IntPtr; } else { throw new ArgumentException(); } } else if (pValue1Type == System_Int64 || pValue1Type == System_UInt64) { if (pValue2Type == System_Int64 || pValue2Type == System_UInt64) { resultType = System_Int64; } else { throw new ArgumentException(); } } else if (pValue1Type == System_IntPtr || pValue1Type == System_UIntPtr) { if (pValue2Type == System_SByte || pValue2Type == System_Byte || pValue2Type == System_Int16 || pValue2Type == System_UInt16 || pValue2Type == System_Char || pValue2Type == System_Int32 || pValue2Type == System_UInt32 || pValue2Type == System_IntPtr || pValue2Type == System_UIntPtr) { resultType = System_IntPtr; } else { throw new ArgumentException(); } } else if (pValue1Type.IsPointerType) { if (pValue2Type == System_SByte || pValue2Type == System_Byte || pValue2Type == System_Int16 || pValue2Type == System_UInt16 || pValue2Type == System_Char || pValue2Type == System_Int32 || pValue2Type == System_UInt32 || pValue2Type == System_IntPtr || pValue2Type == System_UIntPtr || pValue2Type.IsPointerType) { resultType = System_IntPtr; } else { throw new ArgumentException(); } } else if (pValue1Type == System_Single || pValue1Type == System_Double) { if (pValue2Type == System_Single || pValue2Type == System_Double) { resultType = System_Double; } else { throw new ArgumentException(); } } else if (pValue1Type.IsEnumType && pValue2Type.IsEnumType && pValue1Type == pValue2Type) { resultType = pValue1Type; } else if (pValue1Type.IsEnumType && !pValue2Type.IsEnumType) { if (pValue2Type == System_SByte || pValue2Type == System_Byte || pValue2Type == System_Int16 || pValue2Type == System_UInt16 || pValue2Type == System_Char || pValue2Type == System_Int32 || pValue2Type == System_UInt32) { resultType = pValue1Type; } else { throw new ArgumentException(); } } else { throw new ArgumentException(); } return(resultType); }
/// <summary> /// This creates a shallow clone of this method, but /// does a deep clone of it's instructions, parameters, and locals. /// </summary> /// <param name="newParent">The parent for the new method.</param> /// <returns>The clone of this method.</returns> public IRMethod Clone(IRType newParent) { IRMethod m = new IRMethod(this.Assembly); m.GenericMethod = this.GenericMethod; m.GenericParameters.AddRange(this.GenericParameters); this.Instructions.ForEach(i => m.Instructions.Add(i.Clone(m))); this.Locals.ForEach(l => m.Locals.Add(l.Clone(m))); this.Parameters.ForEach(p => m.Parameters.Add(p.Clone(m))); m.MaximumStackDepth = this.MaximumStackDepth; m.Name = this.Name; m.ParentType = newParent; m.ReturnType = this.ReturnType; m.IsStatic = this.IsStatic; // TODO: Fix Branch/Switch/Leave IRInstruction's to new method instructions based on IRIndex's return m; }
public uint AddLinearizedLocal(Stack<IRStackObject> pStack, IRType pType) { IRLocal local = null; if (!LinearizedStackLocalLookup.TryGetValue(new Tuple<int, IRType>(pStack.Count, pType), out local)) { local = new IRLocal(Assembly); local.ParentMethod = this; local.Type = pType; local.Index = (uint)Locals.Count; Locals.Add(local); LinearizedStackLocalLookup[new Tuple<int, IRType>(pStack.Count, pType)] = local; } return local.Index; }
public IRType PresolveType(SigType pSigType) { IRType type = null; switch (pSigType.ElementType) { case SigElementType.Void: type = System_Void; break; case SigElementType.Boolean: type = System_Boolean; break; case SigElementType.Char: type = System_Char; break; case SigElementType.I1: type = System_SByte; break; case SigElementType.U1: type = System_Byte; break; case SigElementType.I2: type = System_Int16; break; case SigElementType.U2: type = System_UInt16; break; case SigElementType.I4: type = System_Int32; break; case SigElementType.U4: type = System_UInt32; break; case SigElementType.I8: type = System_Int64; break; case SigElementType.U8: type = System_UInt64; break; case SigElementType.R4: type = System_Single; break; case SigElementType.R8: type = System_Double; break; case SigElementType.String: type = System_String; break; case SigElementType.Pointer: { if (pSigType.PtrVoid) { type = GetPointerType(System_Void); } else { type = GetPointerType(PresolveType(pSigType.PtrType)); } break; } case SigElementType.ValueType: type = PresolveType(pSigType.CLIFile.ExpandTypeDefRefOrSpecToken(pSigType.ValueTypeDefOrRefOrSpecToken)); break; case SigElementType.Class: type = PresolveType(pSigType.CLIFile.ExpandTypeDefRefOrSpecToken(pSigType.ClassTypeDefOrRefOrSpecToken)); break; case SigElementType.Array: type = GetArrayType(PresolveType(pSigType.ArrayType)); break; case SigElementType.GenericInstantiation: { IRType genericType = PresolveType(pSigType.CLIFile.ExpandTypeDefRefOrSpecToken(pSigType.GenericInstTypeDefOrRefOrSpecToken)); List <IRType> genericTypeParameters = new List <IRType>(); foreach (SigType paramType in pSigType.GenericInstGenArgs) { genericTypeParameters.Add(PresolveType(paramType)); } type = PresolveGenericType(genericType, genericTypeParameters); break; } case SigElementType.IPointer: type = System_IntPtr; break; case SigElementType.UPointer: type = System_UIntPtr; break; case SigElementType.Object: type = System_Object; break; case SigElementType.SingleDimensionArray: type = GetArrayType(PresolveType(pSigType.SZArrayType)); break; case SigElementType.Var: type = IRType.GetVarPlaceholder(pSigType.VarNumber); break; case SigElementType.MethodVar: type = IRType.GetMVarPlaceholder(pSigType.MVarNumber); break; case SigElementType.Type: type = System_Type; break; default: break; } if (type == null) { throw new NullReferenceException(); } return(type); }
public IRType PresolveType(TypeRefData pTypeRefData) { if (pTypeRefData.ExportedType != null) { switch (pTypeRefData.ExportedType.Implementation.Type) { case ImplementationIndex.ImplementationType.File: { IRAssembly assembly = null; if (!AssemblyFileReferenceNameLookup.TryGetValue(pTypeRefData.ExportedType.Implementation.File.Name, out assembly)) { throw new KeyNotFoundException(); } IRType type = assembly.Types.Find(t => t.Namespace == pTypeRefData.TypeNamespace && t.Name == pTypeRefData.TypeName); if (type == null) { throw new NullReferenceException(); } return(type); } case ImplementationIndex.ImplementationType.AssemblyRef: { IRAssembly assembly = null; if (!AssemblyFileReferenceNameLookup.TryGetValue(pTypeRefData.ExportedType.Implementation.AssemblyRef.Name, out assembly)) { throw new KeyNotFoundException(); } IRType type = assembly.Types.Find(t => t.Namespace == pTypeRefData.TypeNamespace && t.Name == pTypeRefData.TypeName); if (type == null) { throw new NullReferenceException(); } return(type); } default: break; } throw new NullReferenceException(); } switch (pTypeRefData.ResolutionScope.Type) { case ResolutionScopeIndex.ResolutionScopeType.AssemblyRef: { IRAssembly assembly = null; if (!AssemblyFileReferenceNameLookup.TryGetValue(pTypeRefData.ResolutionScope.AssemblyRef.Name, out assembly)) { throw new KeyNotFoundException(); } IRType type = assembly.Types.Find(t => t.Namespace == pTypeRefData.TypeNamespace && t.Name == pTypeRefData.TypeName); if (type == null) { throw new NullReferenceException(); } return(type); } case ResolutionScopeIndex.ResolutionScopeType.TypeRef: { IRType type = PresolveType(pTypeRefData.ResolutionScope.TypeRef); if (type == null) { throw new NullReferenceException(); } type = type.NestedTypes.Find(t => t.Namespace == pTypeRefData.TypeNamespace && t.Name == pTypeRefData.TypeName); if (type == null) { throw new NullReferenceException(); } return(type); } } throw new NullReferenceException(); }
public void ConvertInstructions(MethodDefData pMethodDefData) { if (pMethodDefData.Body == null) { return; } ILReader reader = new ILReader(pMethodDefData.CLIFile.Data, pMethodDefData.Body.CodeRVA, pMethodDefData.Body.CodeSize); ILOpcode opcode = ILOpcode.Nop; ILExtendedOpcode extendedOpcode = ILExtendedOpcode.ArgList; MethodSig methodSignature = pMethodDefData.ExpandedSignature; IRPrefixFlags prefixFlags = IRPrefixFlags.None; uint prefixConstrainedToken = 0; Console.WriteLine("Converting {0}.{1}.{2}", ParentType.Namespace, ParentType.Name, Name); while (!reader.EndOfCode) { bool clearFlags = true; uint startOfInstruction = reader.Offset; opcode = reader.ReadOpcode(); switch (opcode) { case ILOpcode.Nop: AddInstruction(startOfInstruction, new IRNopInstruction(true)); break; case ILOpcode.Break: AddInstruction(startOfInstruction, new IRBreakInstruction()); break; case ILOpcode.LdArg_0: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(0)); break; case ILOpcode.LdArg_1: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(1)); break; case ILOpcode.LdArg_2: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(2)); break; case ILOpcode.LdArg_3: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(3)); break; case ILOpcode.LdLoc_0: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(0)); break; case ILOpcode.LdLoc_1: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(1)); break; case ILOpcode.LdLoc_2: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(2)); break; case ILOpcode.LdLoc_3: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(3)); break; case ILOpcode.StLoc_0: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(0)); break; case ILOpcode.StLoc_1: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(1)); break; case ILOpcode.StLoc_2: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(2)); break; case ILOpcode.StLoc_3: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(3)); break; case ILOpcode.LdArg_S: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(reader.ReadByte())); break; case ILOpcode.LdArgA_S: AddInstruction(startOfInstruction, new IRLoadParameterAddressInstruction(reader.ReadByte())); break; case ILOpcode.StArg_S: AddInstruction(startOfInstruction, new IRStoreParameterInstruction(reader.ReadByte())); break; case ILOpcode.LdLoc_S: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(reader.ReadByte())); break; case ILOpcode.LdLocA_S: AddInstruction(startOfInstruction, new IRLoadLocalAddressInstruction(reader.ReadByte())); break; case ILOpcode.StLoc_S: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(reader.ReadByte())); break; case ILOpcode.LdNull: AddInstruction(startOfInstruction, new IRLoadNullInstruction()); break; case ILOpcode.Ldc_I4_M1: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(-1)); break; case ILOpcode.Ldc_I4_0: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(0)); break; case ILOpcode.Ldc_I4_1: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(1)); break; case ILOpcode.Ldc_I4_2: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(2)); break; case ILOpcode.Ldc_I4_3: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(3)); break; case ILOpcode.Ldc_I4_4: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(4)); break; case ILOpcode.Ldc_I4_5: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(5)); break; case ILOpcode.Ldc_I4_6: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(6)); break; case ILOpcode.Ldc_I4_7: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(7)); break; case ILOpcode.Ldc_I4_8: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(8)); break; case ILOpcode.Ldc_I4_S: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(reader.ReadByte())); break; case ILOpcode.Ldc_I4: AddInstruction(startOfInstruction, new IRLoadInteger32Instruction(reader.ReadInt32())); break; case ILOpcode.Ldc_I8: AddInstruction(startOfInstruction, new IRLoadInteger64Instruction(reader.ReadInt64())); break; case ILOpcode.Ldc_R4: AddInstruction(startOfInstruction, new IRLoadReal32Instruction(reader.ReadSingle())); break; case ILOpcode.Ldc_R8: AddInstruction(startOfInstruction, new IRLoadReal64Instruction(reader.ReadDouble())); break; case ILOpcode.Dup: AddInstruction(startOfInstruction, new IRDuplicateInstruction()); break; case ILOpcode.Pop: AddInstruction(startOfInstruction, new IRPopInstruction()); break; case ILOpcode.Jmp: AddInstruction(startOfInstruction, new IRJumpInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.Call: AddInstruction(startOfInstruction, new IRCallInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), false)); break; case ILOpcode.CallI: throw new NotImplementedException("CallI"); case ILOpcode.Ret: AddInstruction(startOfInstruction, new IRReturnInstruction()); break; case ILOpcode.Br_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Always, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.BrFalse_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.False, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.BrTrue_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.True, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Beq_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Equal, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Bge_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterOrEqual, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Bgt_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Greater, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Ble_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessOrEqual, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Blt_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Less, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Bne_Un_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.NotEqualUnsigned, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Bge_Un_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterOrEqualUnsigned, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Bgt_Un_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterUnsigned, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Ble_Un_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessOrEqualUnsigned, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Blt_Un_S: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessUnsigned, (int)(reader.ReadSByte() + reader.Offset))); break; case ILOpcode.Br: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Always, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.BrFalse: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.False, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.BrTrue: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.True, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Beq: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Equal, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Bge: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterOrEqual, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Bgt: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Greater, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Ble: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessOrEqual, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Blt: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.Less, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Bne_Un: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.NotEqualUnsigned, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Bge_Un: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterOrEqualUnsigned, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Bgt_Un: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.GreaterUnsigned, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Ble_Un: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessOrEqualUnsigned, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Blt_Un: AddInstruction(startOfInstruction, new IRBranchInstruction(IRBranchCondition.LessUnsigned, (int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Switch: { uint targetCount = reader.ReadUInt32(); int[] targetILOffsets = new int[targetCount]; for (int index = 0; index < targetCount; ++index) { targetILOffsets[index] = reader.ReadInt32(); } for (int index = 0; index < targetCount; ++index) { targetILOffsets[index] += (int)reader.Offset; } AddInstruction(startOfInstruction, new IRSwitchInstruction(targetILOffsets)); break; } case ILOpcode.LdInd_I1: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_SByte)); break; case ILOpcode.LdInd_U1: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Byte)); break; case ILOpcode.LdInd_I2: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Int16)); break; case ILOpcode.LdInd_U2: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_UInt16)); break; case ILOpcode.LdInd_I4: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Int32)); break; case ILOpcode.LdInd_U4: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_UInt32)); break; case ILOpcode.LdInd_I8: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Int64)); break; case ILOpcode.LdInd_I: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_IntPtr)); break; case ILOpcode.LdInd_R4: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Single)); break; case ILOpcode.LdInd_R8: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Double)); break; case ILOpcode.LdInd_Ref: AddInstruction(startOfInstruction, new IRLoadIndirectInstruction(Assembly.AppDomain.System_Object)); break; case ILOpcode.StInd_Ref: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Object)); break; case ILOpcode.StInd_I1: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_SByte)); break; case ILOpcode.StInd_I2: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Int16)); break; case ILOpcode.StInd_I4: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Int32)); break; case ILOpcode.StInd_I8: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Int64)); break; case ILOpcode.StInd_R4: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Single)); break; case ILOpcode.StInd_R8: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_Double)); break; case ILOpcode.Add: AddInstruction(startOfInstruction, new IRAddInstruction(IROverflowType.None)); break; case ILOpcode.Sub: AddInstruction(startOfInstruction, new IRSubtractInstruction(IROverflowType.None)); break; case ILOpcode.Mul: AddInstruction(startOfInstruction, new IRMultiplyInstruction(IROverflowType.None)); break; case ILOpcode.Div: AddInstruction(startOfInstruction, new IRDivideInstruction(IROverflowType.Signed)); break; case ILOpcode.Div_Un: AddInstruction(startOfInstruction, new IRDivideInstruction(IROverflowType.Unsigned)); break; case ILOpcode.Rem: AddInstruction(startOfInstruction, new IRRemainderInstruction(IROverflowType.Signed)); break; case ILOpcode.Rem_Un: AddInstruction(startOfInstruction, new IRRemainderInstruction(IROverflowType.Unsigned)); break; case ILOpcode.And: AddInstruction(startOfInstruction, new IRAndInstruction()); break; case ILOpcode.Or: AddInstruction(startOfInstruction, new IROrInstruction()); break; case ILOpcode.Xor: AddInstruction(startOfInstruction, new IRXorInstruction()); break; case ILOpcode.Shl: AddInstruction(startOfInstruction, new IRShiftInstruction(IRShiftType.Left)); break; case ILOpcode.Shr: AddInstruction(startOfInstruction, new IRShiftInstruction(IRShiftType.RightSignExtended)); break; case ILOpcode.Shr_Un: AddInstruction(startOfInstruction, new IRShiftInstruction(IRShiftType.Right)); break; case ILOpcode.Neg: AddInstruction(startOfInstruction, new IRNegateInstruction()); break; case ILOpcode.Not: AddInstruction(startOfInstruction, new IRNotInstruction()); break; case ILOpcode.Conv_I1: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_SByte)); break; case ILOpcode.Conv_I2: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Int16)); break; case ILOpcode.Conv_I4: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Int32)); break; case ILOpcode.Conv_I8: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Int64)); break; case ILOpcode.Conv_R4: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Single)); break; case ILOpcode.Conv_R8: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Double)); break; case ILOpcode.Conv_U4: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_UInt32)); break; case ILOpcode.Conv_U8: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_UInt64)); break; case ILOpcode.CallVirt: AddInstruction(startOfInstruction, new IRCallInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), true)); break; case ILOpcode.CpObj: AddInstruction(startOfInstruction, new IRCopyObjectInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdObj: AddInstruction(startOfInstruction, new IRLoadObjectInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdStr: AddInstruction(startOfInstruction, new IRLoadStringInstruction((string)Assembly.File.ExpandMetadataToken(reader.ReadUInt32()).Data)); break; case ILOpcode.NewObj: AddInstruction(startOfInstruction, new IRNewObjectInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.CastClass: AddInstruction(startOfInstruction, new IRCastInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), true)); break; case ILOpcode.IsInst: AddInstruction(startOfInstruction, new IRCastInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), false)); break; case ILOpcode.Conv_R_Un: throw new NotImplementedException("Conv_R_Un"); case ILOpcode.Unbox: AddInstruction(startOfInstruction, new IRUnboxInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), false)); break; case ILOpcode.Throw: AddInstruction(startOfInstruction, new IRThrowInstruction()); break; case ILOpcode.LdFld: AddInstruction(startOfInstruction, new IRLoadFieldInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdFldA: AddInstruction(startOfInstruction, new IRLoadFieldAddressInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.StFld: AddInstruction(startOfInstruction, new IRStoreFieldInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdSFld: AddInstruction(startOfInstruction, new IRLoadStaticFieldInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdSFldA: AddInstruction(startOfInstruction, new IRLoadStaticFieldAddressInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.StSFld: AddInstruction(startOfInstruction, new IRStoreStaticFieldInstruction(Assembly.AppDomain.PresolveField(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.StObj: AddInstruction(startOfInstruction, new IRStoreObjectInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.Conv_Ovf_I1_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_SByte, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_I2_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int16, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_I4_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int32, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_I8_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int64, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_U1_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_SByte, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_U2_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int16, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_U4_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int32, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_U8_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int64, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_I_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_IntPtr, IROverflowType.Unsigned)); break; case ILOpcode.Conv_Ovf_U_Un: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_UIntPtr, IROverflowType.Unsigned)); break; case ILOpcode.Box: AddInstruction(startOfInstruction, new IRBoxInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.NewArr: AddInstruction(startOfInstruction, new IRNewArrayInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdLen: AddInstruction(startOfInstruction, new IRLoadArrayLengthInstruction()); break; case ILOpcode.LdElemA: AddInstruction(startOfInstruction, new IRLoadArrayElementAddressInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.LdElem_I1: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_SByte)); break; case ILOpcode.LdElem_U1: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Byte)); break; case ILOpcode.LdElem_I2: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Int16)); break; case ILOpcode.LdElem_U2: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_UInt16)); break; case ILOpcode.LdElem_I4: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Int32)); break; case ILOpcode.LdElem_U4: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_UInt32)); break; case ILOpcode.LdElem_I8: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Int64)); break; case ILOpcode.LdElem_I: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_IntPtr)); break; case ILOpcode.LdElem_R4: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Single)); break; case ILOpcode.LdElem_R8: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.System_Double)); break; case ILOpcode.LdElem_Ref: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(null)); break; case ILOpcode.StElem_I: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_IntPtr)); break; case ILOpcode.StElem_I1: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_SByte)); break; case ILOpcode.StElem_I2: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_Int16)); break; case ILOpcode.StElem_I4: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_Int32)); break; case ILOpcode.StElem_I8: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_Int64)); break; case ILOpcode.StElem_R4: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_Single)); break; case ILOpcode.StElem_R8: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.System_Double)); break; case ILOpcode.StElem_Ref: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(null)); break; case ILOpcode.LdElem: AddInstruction(startOfInstruction, new IRLoadArrayElementInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.StElem: AddInstruction(startOfInstruction, new IRStoreArrayElementInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.Unbox_Any: AddInstruction(startOfInstruction, new IRUnboxInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), true)); break; case ILOpcode.Conv_Ovf_I1: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_SByte, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_U1: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Byte, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_I2: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int16, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_U2: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_UInt16, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_I4: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int32, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_U4: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_UInt32, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_I8: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_Int64, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_U8: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_UInt64, IROverflowType.Signed)); break; case ILOpcode.RefAnyVal: AddInstruction(startOfInstruction, new IRLoadTypedReferenceAddressInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILOpcode.CkFinite: AddInstruction(startOfInstruction, new IRCheckFiniteInstruction()); break; case ILOpcode.MkRefAny: AddInstruction(startOfInstruction, new IRLoadTypedReferenceInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandTypeDefRefOrSpecToken(reader.ReadUInt32())))); break; case ILOpcode.LdToken: { IRType type = null; IRMethod method = null; IRField field = null; MetadataToken token = Assembly.File.ExpandMetadataToken(reader.ReadUInt32()); switch (token.Table) { case CLIMetadataTables.TypeDef: type = Assembly.AppDomain.PresolveType((TypeDefData)token.Data); break; case CLIMetadataTables.TypeRef: type = Assembly.AppDomain.PresolveType((TypeRefData)token.Data); break; case CLIMetadataTables.TypeSpec: type = Assembly.AppDomain.PresolveType((TypeSpecData)token.Data); break; case CLIMetadataTables.MethodDef: method = Assembly.AppDomain.PresolveMethod((MethodDefData)token.Data); break; case CLIMetadataTables.MethodSpec: method = Assembly.AppDomain.PresolveMethod((MethodSpecData)token.Data); break; case CLIMetadataTables.Field: field = Assembly.AppDomain.PresolveField((FieldData)token.Data); break; case CLIMetadataTables.MemberRef: { MemberRefData memberRefData = (MemberRefData)token.Data; if (memberRefData.IsMethodRef) { method = Assembly.AppDomain.PresolveMethod(memberRefData); } else { field = Assembly.AppDomain.PresolveField(memberRefData); } break; } } AddInstruction(startOfInstruction, new IRLoadRuntimeHandleInstruction(type, method, field)); break; } case ILOpcode.Conv_U2: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_UInt16)); break; case ILOpcode.Conv_U1: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_Byte)); break; case ILOpcode.Conv_I: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_IntPtr)); break; case ILOpcode.Conv_Ovf_I: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_IntPtr, IROverflowType.Signed)); break; case ILOpcode.Conv_Ovf_U: AddInstruction(startOfInstruction, new IRConvertCheckedInstruction(Assembly.AppDomain.System_UIntPtr, IROverflowType.Signed)); break; case ILOpcode.Add_Ovf: AddInstruction(startOfInstruction, new IRAddInstruction(IROverflowType.Signed)); break; case ILOpcode.Add_Ovf_Un: AddInstruction(startOfInstruction, new IRAddInstruction(IROverflowType.Unsigned)); break; case ILOpcode.Mul_Ovf: AddInstruction(startOfInstruction, new IRMultiplyInstruction(IROverflowType.Signed)); break; case ILOpcode.Mul_Ovf_Un: AddInstruction(startOfInstruction, new IRMultiplyInstruction(IROverflowType.Unsigned)); break; case ILOpcode.Sub_Ovf: AddInstruction(startOfInstruction, new IRSubtractInstruction(IROverflowType.Signed)); break; case ILOpcode.Sub_Ovf_Un: AddInstruction(startOfInstruction, new IRSubtractInstruction(IROverflowType.Unsigned)); break; case ILOpcode.EndFinally: AddInstruction(startOfInstruction, new IREndFinallyInstruction()); break; case ILOpcode.Leave: AddInstruction(startOfInstruction, new IRLeaveInstruction((int)(reader.ReadInt32() + reader.Offset))); break; case ILOpcode.Leave_S: AddInstruction(startOfInstruction, new IRLeaveInstruction((int)(reader.ReadByte() + reader.Offset))); break; case ILOpcode.StInd_I: AddInstruction(startOfInstruction, new IRStoreIndirectInstruction(Assembly.AppDomain.System_IntPtr)); break; case ILOpcode.Conv_U: AddInstruction(startOfInstruction, new IRConvertUncheckedInstruction(Assembly.AppDomain.System_UIntPtr)); break; case ILOpcode.Extended: { extendedOpcode = (ILExtendedOpcode)reader.ReadByte(); switch (extendedOpcode) { case ILExtendedOpcode.ArgList: throw new NotImplementedException("ArgList"); case ILExtendedOpcode.Ceq: AddInstruction(startOfInstruction, new IRCompareInstruction(IRCompareCondition.Equal)); break; case ILExtendedOpcode.Cgt: AddInstruction(startOfInstruction, new IRCompareInstruction(IRCompareCondition.GreaterThan)); break; case ILExtendedOpcode.Cgt_Un: AddInstruction(startOfInstruction, new IRCompareInstruction(IRCompareCondition.GreaterThanUnsigned)); break; case ILExtendedOpcode.Clt: AddInstruction(startOfInstruction, new IRCompareInstruction(IRCompareCondition.LessThan)); break; case ILExtendedOpcode.Clt_Un: AddInstruction(startOfInstruction, new IRCompareInstruction(IRCompareCondition.LessThanUnsigned)); break; case ILExtendedOpcode.LdFtn: AddInstruction(startOfInstruction, new IRLoadFunctionInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), false)); break; case ILExtendedOpcode.LdVirtFtn: AddInstruction(startOfInstruction, new IRLoadFunctionInstruction(Assembly.AppDomain.PresolveMethod(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())), true)); break; case ILExtendedOpcode.LdArg: AddInstruction(startOfInstruction, new IRLoadParameterInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.LdArgA: AddInstruction(startOfInstruction, new IRLoadParameterAddressInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.StArg: AddInstruction(startOfInstruction, new IRStoreParameterInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.LdLoc: AddInstruction(startOfInstruction, new IRLoadLocalInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.LdLocA: AddInstruction(startOfInstruction, new IRLoadLocalAddressInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.StLoc: AddInstruction(startOfInstruction, new IRStoreLocalInstruction(reader.ReadUInt16())); break; case ILExtendedOpcode.LocAlloc: AddInstruction(startOfInstruction, new IRStackAllocateInstruction()); break; case ILExtendedOpcode.EndFilter: throw new NotImplementedException("EndFilter"); case ILExtendedOpcode.Unaligned__: prefixFlags |= IRPrefixFlags.Unaligned; clearFlags = false; break; case ILExtendedOpcode.Volatile__: prefixFlags |= IRPrefixFlags.Volatile; clearFlags = false; break; case ILExtendedOpcode.Tail__: prefixFlags |= IRPrefixFlags.Tail; clearFlags = false; break; case ILExtendedOpcode.InitObj: AddInstruction(startOfInstruction, new IRInitializeObjectInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILExtendedOpcode.Constrained__: prefixConstrainedToken = reader.ReadUInt32(); prefixFlags |= IRPrefixFlags.Constrained; clearFlags = false; break; case ILExtendedOpcode.CpBlk: AddInstruction(startOfInstruction, new IRCopyBlockInstruction()); break; case ILExtendedOpcode.InitBlk: AddInstruction(startOfInstruction, new IRInitializeBlockInstruction()); break; case ILExtendedOpcode.No__: prefixFlags |= IRPrefixFlags.No; clearFlags = false; break; case ILExtendedOpcode.ReThrow: throw new NotImplementedException("ReThrow"); case ILExtendedOpcode.SizeOf: AddInstruction(startOfInstruction, new IRSizeOfInstruction(Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(reader.ReadUInt32())))); break; case ILExtendedOpcode.RefAnyType: AddInstruction(startOfInstruction, new IRLoadTypedReferenceTypeInstruction()); break; case ILExtendedOpcode.ReadOnly__: prefixFlags |= IRPrefixFlags.ReadOnly; clearFlags = false; break; } break; } default: break; } if (clearFlags) { prefixFlags = IRPrefixFlags.None; prefixConstrainedToken = 0; } } Instructions.FixTargetInstructions(); }
private void LinearizePath(MethodDefData pMethodDefData, IRInstruction pStartInstruction, Stack <IRStackObject> pStack, Queue <Tuple <IRInstruction, Stack <IRStackObject> > > pBranches) { int stackReturn = pStack.Count; IRInstruction currentInstruction = pStartInstruction; MethodDefData.MethodDefBodyData.MethodDefBodyExceptionData exceptionData = null; while (currentInstruction != null) { if (currentInstruction.Linearized && pStack.Count == stackReturn) { break; } if ((exceptionData = Array.Find(pMethodDefData.Body.Exceptions, e => e.Flags == 0 && e.HandlerOffset == currentInstruction.ILOffset)) != null) { IRType exceptionType = Assembly.AppDomain.PresolveType(Assembly.File.ExpandMetadataToken(exceptionData.ClassTokenOrFilterOffset)); IRStackObject exceptionObj = new IRStackObject(); exceptionObj.Type = exceptionType; exceptionObj.LinearizedTarget = new IRLinearizedLocation(IRLinearizedLocationType.Local); exceptionObj.LinearizedTarget.Local.LocalIndex = currentInstruction.AddLinearizedLocal(pStack, exceptionType); pStack.Push(exceptionObj); } currentInstruction.Linearize(pStack); currentInstruction.Linearized = true; switch (currentInstruction.Opcode) { case IROpcode.Branch: { IRBranchInstruction branchInstruction = (IRBranchInstruction)currentInstruction; if (branchInstruction.BranchCondition == IRBranchCondition.Always) { currentInstruction = branchInstruction.TargetIRInstruction; } else { pBranches.Enqueue(new Tuple <IRInstruction, Stack <IRStackObject> >(branchInstruction.TargetIRInstruction, pStack.Duplicate())); currentInstruction = Instructions[currentInstruction.IRIndex + 1]; } break; } case IROpcode.Switch: { IRSwitchInstruction switchInstruction = (IRSwitchInstruction)currentInstruction; foreach (IRInstruction targetInstruction in switchInstruction.TargetIRInstructions) { pBranches.Enqueue(new Tuple <IRInstruction, Stack <IRStackObject> >(targetInstruction, pStack.Duplicate())); } currentInstruction = Instructions[currentInstruction.IRIndex + 1]; break; } case IROpcode.Leave: { IRLeaveInstruction leaveInstruction = (IRLeaveInstruction)currentInstruction; currentInstruction = leaveInstruction.TargetIRInstruction; break; } case IROpcode.Jump: case IROpcode.Throw: case IROpcode.Return: currentInstruction = null; break; default: currentInstruction = currentInstruction.IRIndex >= Instructions.Count ? null : Instructions[currentInstruction.IRIndex + 1]; break; } } }
public uint AddLinearizedLocal(Stack<IRStackObject> pStack, IRType pType) { return ParentMethod.AddLinearizedLocal(pStack, pType); }
internal void CacheCORTypes(IRAssembly pAssembly) { foreach (IRType type in pAssembly.Types) { if (type.Namespace == "System") { switch (type.Name) { case "Array": System_Array = type; break; case "Boolean": System_Boolean = type; break; case "Byte": System_Byte = type; break; case "Char": System_Char = type; break; case "Double": System_Double = type; break; case "Enum": System_Enum = type; break; case "Exception": System_Exception = type; break; case "Int16": System_Int16 = type; break; case "Int32": System_Int32 = type; break; case "Int64": System_Int64 = type; break; case "IntPtr": System_IntPtr = type; break; case "Object": System_Object = type; break; case "RuntimeFieldHandle": System_RuntimeFieldHandle = type; break; case "RuntimeMethodHandle": System_RuntimeMethodHandle = type; break; case "RuntimeTypeHandle": System_RuntimeTypeHandle = type; break; case "SByte": System_SByte = type; break; case "Single": System_Single = type; break; case "String": System_String = type; break; case "Type": System_Type = type; break; case "TypedReference": System_TypedReference = type; break; case "UInt16": System_UInt16 = type; break; case "UInt32": System_UInt32 = type; break; case "UInt64": System_UInt64 = type; break; case "UIntPtr": System_UIntPtr = type; break; case "ValueType": System_ValueType = type; break; case "Void": System_Void = type; break; default: break; } } } }