Пример #1
0
        public void PropertyDefinitions(AnalysisNet.Types.TypeDefinition analysisNetType, Cecil.TypeDefinition cecilTypeDef)
        {
            foreach (AnalysisNet.Types.PropertyDefinition analysisNetProp in analysisNetType.PropertyDefinitions)
            {
                Cecil.PropertyDefinition cecilProp = new Cecil.PropertyDefinition(analysisNetProp.Name,
                                                                                  Cecil.PropertyAttributes.None,
                                                                                  ReferenceGenerator.TypeReference(analysisNetProp.PropertyType));

                if (analysisNetProp.Getter != null)
                {
                    Cecil.MethodDefinition getterDef = ReferenceGenerator.MethodReference(analysisNetProp.Getter).Resolve();
                    cecilProp.GetMethod = getterDef;
                }
                if (analysisNetProp.Setter != null)
                {
                    Cecil.MethodDefinition setterDef = ReferenceGenerator.MethodReference(analysisNetProp.Setter).Resolve();
                    cecilProp.SetMethod = setterDef;
                }

                SetCustomAttributes(analysisNetProp.Attributes, cecilProp.CustomAttributes);

                // Properties.Add sets this field
                //cecilProp.DeclaringType = ReferenceGenerator.TypeReference(analysisNetType).Resolve();
                cecilTypeDef.Properties.Add(cecilProp);
            }
        }
Пример #2
0
        private Cecil.FieldDefinition CreateFieldDefinition(AnalysisNet.Types.FieldDefinition fieldDefinition)
        {
            Cecil.FieldAttributes fieldAttribute = Cecil.FieldAttributes.Public;
            if (fieldDefinition.IsStatic)
            {
                fieldAttribute |= Cecil.FieldAttributes.Static;
            }

            Cecil.TypeReference   fieldType  = ReferenceGenerator.TypeReference(fieldDefinition.Type);
            Cecil.FieldDefinition cecilField = new Cecil.FieldDefinition(fieldDefinition.Name, fieldAttribute, fieldType);

            if (fieldDefinition.Value != null)
            {
                cecilField.Constant    = fieldDefinition.Value.Value;
                cecilField.HasConstant = true;
            }

            byte[] newArray = new byte[fieldDefinition.InitialValue.Length];
            Array.Copy(fieldDefinition.InitialValue, newArray, newArray.Length);
            cecilField.InitialValue = newArray;

            if (newArray.Length > 0)
            {
                cecilField.Attributes |= Cecil.FieldAttributes.HasFieldRVA;
            }

            return(cecilField);
        }
Пример #3
0
        private void SetOverrides(AnalysisNet.Types.MethodDefinition methodDefinition, Cecil.MethodDefinition methodDef)
        {
            ISet <AnalysisNet.Types.MethodImplementation>        impls        = methodDefinition.ContainingType.ExplicitOverrides;
            IEnumerable <AnalysisNet.Types.MethodImplementation> matchedImpls = impls.Where(impl => methodDefinition.MatchReference(impl.ImplementingMethod));

            methodDef.Overrides.AddRange(matchedImpls.Select(impl => ReferenceGenerator.MethodReference(impl.ImplementedMethod)));
        }
Пример #4
0
        public Cecil.MethodDefinition MethodDefinition(AnalysisNet.Types.MethodDefinition methodDefinition)
        {
            Cecil.MethodDefinition cecilMethodDefinition = new Cecil.MethodDefinition(methodDefinition.Name, 0, Context.CurrentModule.TypeSystem.Void);
            GenerateMethodAttributes(methodDefinition, cecilMethodDefinition);
            cecilMethodDefinition.CreateGenericParameters(methodDefinition.GenericParameters.Count);

            Cecil.TypeReference returnType = ReferenceGenerator.TypeReference(methodDefinition.ReturnType);
            cecilMethodDefinition.ReturnType = returnType;
            AddConstraintsToGenericParameters(methodDefinition, cecilMethodDefinition);

            Cecil.TypeReference  typeRef        = ReferenceGenerator.TypeReference(methodDefinition.ContainingType);
            Cecil.TypeDefinition containingType = typeRef.Resolve();
            cecilMethodDefinition.DeclaringType = containingType as Cecil.TypeDefinition;

            SetOverrides(methodDefinition, cecilMethodDefinition);
            SetCustomAttributes(methodDefinition.Attributes, cecilMethodDefinition.CustomAttributes);
            IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions = CreateParameters(methodDefinition, cecilMethodDefinition);

            if (methodDefinition.HasBody)
            {
                cecilMethodDefinition.Body.MaxStackSize = methodDefinition.Body.MaxStack;
                cecilMethodDefinition.Body.InitLocals   = methodDefinition.Body.LocalVariables.Count > 0;
                IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions = CreateLocalVariables(methodDefinition, cecilMethodDefinition);
                InstructionGenerator instructionGenerator = new InstructionGenerator(ReferenceGenerator);

                // analysis-net instruction -> [cecil instruction]
                IDictionary <AnalysisNet.Bytecode.Instruction, IList <Cecil.Cil.Instruction> > mapInstructions = instructionGenerator.CreateInstructions(methodDefinition, cecilMethodDefinition, variableDefinitions, parameterDefinitions);

                CreateExceptionHandlers(mapInstructions, methodDefinition.Body, cecilMethodDefinition.Body);
            }

            return(cecilMethodDefinition);
        }
Пример #5
0
 private void AddInterfaceImplementations(AnalysisNet.Types.TypeDefinition typeDefinition, Cecil.TypeDefinition t)
 {
     foreach (AnalysisNet.Types.IBasicType inter in typeDefinition.Interfaces)
     {
         t.Interfaces.Add(new Cecil.InterfaceImplementation(ReferenceGenerator.TypeReference(inter)));
     }
 }
Пример #6
0
 public BytecodeTranslator(AnalysisNet.Types.MethodDefinition methodDefinition,
                           IDictionary <AnalysisNetTac.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions,
                           IDictionary <AnalysisNetTac.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions,
                           ReferenceGenerator referenceGenerator,
                           Cecil.Cil.ILProcessor processor)
 {
     this.methodDefinition     = methodDefinition;
     this.processor            = processor;
     this.variableDefinitions  = variableDefinitions;
     this.parameterDefinitions = parameterDefinitions;
     this.referenceGenerator   = referenceGenerator;
 }
Пример #7
0
        private Cecil.TypeDefinition CreateEnumDefinition(AnalysisNet.Types.TypeDefinition typeDefinition)
        {
            Cecil.TypeDefinition def = CreateClassDefinition(typeDefinition);
            def.IsSealed = true;
            foreach (Cecil.FieldDefinition field in def.Fields)
            {
                field.IsStatic              = true;
                field.IsLiteral             = true;
                field.HasDefault            = true;
                field.FieldType.IsValueType = true;
            }

            Cecil.TypeReference   underlyingType = ReferenceGenerator.TypeReference(typeDefinition.UnderlayingType);
            Cecil.FieldDefinition value__        = new Cecil.FieldDefinition("value__", Cecil.FieldAttributes.RTSpecialName | Cecil.FieldAttributes.SpecialName, underlyingType);
            def.Fields.Insert(0, value__);

            return(def);
        }
Пример #8
0
        private void CreateExceptionHandlers(IDictionary <Model.Bytecode.Instruction, IList <Mono.Cecil.Cil.Instruction> > map,
                                             AnalysisNet.Types.MethodBody analysisNetBody,
                                             Cecil.Cil.MethodBody cecilBody)
        {
            foreach (AnalysisNet.ProtectedBlock protectedBlock in analysisNetBody.ExceptionInformation)
            {
                Cecil.Cil.ExceptionHandler handler = new Cecil.Cil.ExceptionHandler(GetExceptionHandlerType(protectedBlock.Handler.Kind))
                {
                    TryStart = GetTarget(protectedBlock.Start, map),
                    TryEnd   = GetTarget(protectedBlock.End, map)
                };

                if (protectedBlock.Handler is AnalysisNet.FilterExceptionHandler filterHandler)
                {
                    handler.CatchType    = ReferenceGenerator.TypeReference(filterHandler.ExceptionType);
                    handler.FilterStart  = GetTarget(filterHandler.FilterStart, map);
                    handler.HandlerStart = GetTarget(filterHandler.Start, map);
                    handler.HandlerEnd   = GetTarget(filterHandler.End, map);
                }
                else if (protectedBlock.Handler is AnalysisNet.CatchExceptionHandler catchHandler)
                {
                    handler.CatchType    = ReferenceGenerator.TypeReference(catchHandler.ExceptionType);
                    handler.HandlerStart = GetTarget(catchHandler.Start, map);
                    handler.HandlerEnd   = GetTarget(catchHandler.End, map);
                }
                else if (protectedBlock.Handler is AnalysisNet.FaultExceptionHandler faultHandler)
                {
                    handler.HandlerStart = GetTarget(faultHandler.Start, map);
                    handler.HandlerEnd   = GetTarget(faultHandler.End, map);
                }
                else if (protectedBlock.Handler is AnalysisNet.FinallyExceptionHandler finallyHandler)
                {
                    handler.HandlerStart = GetTarget(finallyHandler.Start, map);
                    handler.HandlerEnd   = GetTarget(finallyHandler.End, map);
                }
                else
                {
                    throw new NotImplementedException();
                }

                cecilBody.ExceptionHandlers.Add(handler);
            }
        }
Пример #9
0
        private IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> CreateParameters(AnalysisNet.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef)
        {
            IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition> parameterDefinitions = new Dictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.ParameterDefinition>();

            for (int idx = 0; idx < methodDefinition.Parameters.Count; idx++)
            {
                AnalysisNet.Types.MethodParameter methodParameter = methodDefinition.Parameters.ElementAt(idx);
                //if (methodParameter.Name.Equals("this"))
                //    continue;

                Cecil.ParameterDefinition paramDef = new Cecil.ParameterDefinition(ReferenceGenerator.TypeReference(methodParameter.Type));
                if (methodParameter.DefaultValue != null)
                {
                    paramDef.Constant   = methodParameter.DefaultValue.Value;
                    paramDef.HasDefault = true;
                }

                if (methodParameter.Kind == AnalysisNet.Types.MethodParameterKind.In)
                {
                    paramDef.IsIn = true;
                }
                else if (methodParameter.Kind == AnalysisNet.Types.MethodParameterKind.Out)
                {
                    paramDef.IsOut = true;
                }

                methodDef.Parameters.Add(paramDef);

                // map body parameters to cecil parameters
                if (methodDefinition.HasBody && methodDefinition.Body.Parameters.Count > 0)
                {
                    // body parameters contain 'this' while analysis-net's parameters do not
                    int localIdx = (methodDefinition.IsStatic ? 0 : 1) + idx;
                    AnalysisNet.ThreeAddressCode.Values.IVariable localVariable = methodDefinition.Body.Parameters.ElementAt(localIdx);
                    parameterDefinitions[localVariable] = paramDef;
                }
            }

            return(parameterDefinitions);
        }
Пример #10
0
        private void CreateDefinitions(ModelMapping modelMapping)
        {
            IDictionary <Model.Assembly, AssemblyDefinition> assembliesMap = modelMapping.AssembliesMap;

            foreach (KeyValuePair <Model.Assembly, AssemblyDefinition> keyval in assembliesMap)
            {
                AssemblyDefinition cecilAssembly       = keyval.Value;
                Model.Assembly     analysisNetAssembly = keyval.Key;

                ReferenceGenerator referenceGen = new ReferenceGenerator(new Context(cecilAssembly.MainModule, modelMapping, Host));

                // TraverseTypes returns every nested type in A before returning A
                // this is assumed by the TypeGenerator and MethodGenerator
                foreach (Model.Types.TypeDefinition analysisNetType in analysisNetAssembly.TraverseTypes())
                {
                    TypeGenerator             typeGenerator = new TypeGenerator(referenceGen);
                    Mono.Cecil.TypeDefinition cecilTypeDef  = typeGenerator.TypeDefinition(analysisNetType);

                    // nested types are not added directly to the main module
                    // instead they are added to their enclosing type (that's the way cecil works)
                    if (cecilTypeDef.DeclaringType == null)
                    {
                        cecilAssembly.MainModule.Types.Add(cecilTypeDef);
                    }

                    foreach (Model.Types.MethodDefinition analysisNetMethod in analysisNetType.Methods)
                    {
                        MethodGenerator methodGenerator = new MethodGenerator(referenceGen);
                        cecilTypeDef.Methods.Add(methodGenerator.MethodDefinition(analysisNetMethod));
                    }

                    // we need to have every method definition created
                    typeGenerator.PropertyDefinitions(analysisNetType, cecilTypeDef);
                }
            }
        }
Пример #11
0
 public MethodGenerator(ReferenceGenerator referenceGenerator) : base(referenceGenerator)
 {
 }
Пример #12
0
 private void SetDeclaringType(AnalysisNet.Types.TypeDefinition typeDefinition, Cecil.TypeDefinition cecilDef)
 {
     Cecil.TypeReference declaringTypeRef = typeDefinition.ContainingType == null ? null : ReferenceGenerator.TypeReference(typeDefinition.ContainingType);
     if (declaringTypeRef != null)
     {
         Cecil.TypeDefinition declaringType = declaringTypeRef.Resolve();
         declaringType.NestedTypes.Add(cecilDef);
         cecilDef.DeclaringType = declaringType;
     }
 }
Пример #13
0
 public InstructionGenerator(ReferenceGenerator referenceGenerator)
 {
     Context            = referenceGenerator.Context;
     ReferenceGenerator = referenceGenerator;
 }
Пример #14
0
        private IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> CreateLocalVariables(Model.Types.MethodDefinition methodDefinition, Mono.Cecil.MethodDefinition methodDef)
        {
            IDictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition> variableDefinitions = new Dictionary <AnalysisNet.ThreeAddressCode.Values.IVariable, Cecil.Cil.VariableDefinition>();

            foreach (AnalysisNet.ThreeAddressCode.Values.IVariable localVariable in methodDefinition.Body.LocalVariables)
            {
                Cecil.Cil.VariableDefinition varDef = new Cecil.Cil.VariableDefinition(ReferenceGenerator.TypeReference(localVariable.Type));
                methodDef.Body.Variables.Add(varDef);
                variableDefinitions[localVariable] = varDef;
            }

            return(variableDefinitions);
        }
Пример #15
0
 public TypeGenerator(ReferenceGenerator referenceGenerator) : base(referenceGenerator)
 {
 }
Пример #16
0
 private void SetBaseType(AnalysisNet.Types.TypeDefinition typeDefinition, Cecil.TypeDefinition cecilDef)
 {
     Cecil.TypeReference baseType = typeDefinition.Base == null ? null : ReferenceGenerator.TypeReference(typeDefinition.Base);
     cecilDef.BaseType = baseType;
 }
Пример #17
0
 public DefinitionGenerator(ReferenceGenerator referenceGenerator)
 {
     Context            = referenceGenerator.Context;
     ReferenceGenerator = referenceGenerator;
 }