Пример #1
0
        public static string CreateMainWrappers()
        {
            StringBuilder sb
                = new StringBuilder();

            foreach (var mainMethodTuple in mainMethods)
            {
                var mainMethod   = mainMethodTuple.Item1;
                var assembly     = mainMethodTuple.Item2;
                var disassembler = new Disassembler(assembly.Host, mainMethod, assembly.PdbReader);
                disassembler.Execute();
                var methodName = BoogieMethod.From(mainMethod).Name;
                var parameters = Helpers.GetParametersWithBoogieType(disassembler.MethodBody.Parameters);
                var returnType = Helpers.GetMethodBoogieReturnType(mainMethod).Equals(Helpers.BoogieType.Void) ? String.Empty : ("returns ($result :" + Helpers.GetMethodBoogieReturnType(mainMethod) + ")");
                sb.AppendLine(String.Format("procedure {{:entrypoint}} $Main_Wrapper_{0}({1}) {2}", methodName, parameters, returnType));
                sb.AppendLine("{");

                var variables = String.Empty;
                IMethodDefinition methodDef = mainMethod as IMethodDefinition;
                if (methodDef != null)
                {
                    variables = String.Join(",", methodDef.Parameters.Select(v => v.Name));
                }
                else
                {
                    variables = String.Join(",", mainMethod.Parameters.Select(v => String.Format("param{0}", v.Index)));
                }

                if (mainMethod.CallingConvention.HasFlag(Microsoft.Cci.CallingConvention.HasThis))
                {
                    variables = String.Format("this", mainMethod.ParameterCount > 0 ? "," : String.Empty, parameters);
                }

                variables = Helpers.Strings.NormalizeStringForCorral(variables);

                sb.AppendLine("\tcall $allocate_static_fields();");
                sb.AppendLine("\tcall $default_values_static_fields();");
                sb.AppendLine("\tcall $initialize_globals();");
                sb.AppendLine("\tcall $call_static_constructors();");
                if (String.IsNullOrEmpty(returnType))
                {
                    sb.AppendLine(String.Format("\tcall {0}({1});", methodName, variables));
                }
                else
                {
                    sb.AppendLine(String.Format("\tcall $result := {0}({1});", methodName, variables));
                }

                sb.AppendLine("\tif ($Exception != null)");
                sb.AppendLine("\t{");
                sb.AppendLine("\t\treturn;");
                sb.AppendLine("\t}");

                sb.AppendLine("}");
            }

            return(sb.ToString());
        }
Пример #2
0
        public String Translate()
        {
            // instructions must be translated before local variables
            // modification to local variables can ocurr while instruction translation is done
            // for example when delegate creation is detected some local variables are deleted.
            var          ins            = TranslateInstructions(out Dictionary <string, BoogieVariable> temporalVariables);
            var          localVariables = TranslateLocalVariables(temporalVariables);
            var          methodName     = BoogieMethod.From(methodDefinition).Name;
            var          attr           = TranslateAttr();
            Disassembler disassembler   = new Disassembler(assembly.Host, methodDefinition, assembly.PdbReader);

            disassembler.Execute();
            var parametersWithTypes = Helpers.GetParametersWithBoogieType(disassembler.MethodBody.Parameters);
            var returnTypeIfAny     = TranslateReturnTypeIfAny();

            var boogieProcedureTemplate = new BoogieProcedureTemplate(methodName, attr, localVariables, ins, parametersWithTypes, returnTypeIfAny, Helpers.IsExternal(methodDefinition));

            return(boogieProcedureTemplate.TransformText());
        }
        public override void TraverseChildren(IMethodDefinition methodDefinition)
        {
            //var signature = MemberHelper.GetMethodSignature(methodDefinition, NameFormattingOptions.Signature | NameFormattingOptions.ParameterName);
            //System.Console.WriteLine(signature);

            if (methodDefinition.IsAbstract || methodDefinition.IsExternal)
            {
                return;
            }

            var disassembler = new Disassembler(host, methodDefinition, sourceLocationProvider);
            var methodBody   = disassembler.Execute();

            foreach (var instruction in methodBody.Instructions)
            {
                var visitor = new AllocAndDelegateFinderVisitor(this);
                instruction.Accept(visitor);
            }
        }
Пример #4
0
 // called from Traverser
 // set in Main
 public static void TranslateAssemblies(ISet <Assembly> assemblies, ClassHierarchyAnalysis CHA)
 {
     foreach (Assembly assembly in assemblies)
     {
         foreach (IMethodDefinition methodDefinition in assembly.GetAllDefinedMethods())
         {
             if (!methodDefinition.IsExternal)
             {
                 try
                 {
                     var disassembler = new Disassembler(assembly.Host, methodDefinition, assembly.PdbReader);
                     disassembler.Execute();
                     MethodBody       mB  = disassembler.MethodBody;
                     ControlFlowGraph cfg = disassembler.ControlFlowGraph;
                     // it would be faster to do this while we do
                     // the global search for field references
                     if (Settings.MemoryModel == ProgramOptions.MemoryModelOption.Mixed)
                     {
                         ReferenceFinder reference = new ReferenceFinder();
                         reference.CollectLocalVariables(mB);
                     }
                     MethodTranslator methodTranslator = new MethodTranslator(methodDefinition, mB, CHA, cfg, assembly, assemblies);
                     // todo: improve this piece of code
                     StreamWriter streamWriter = Program.streamWriter;
                     streamWriter.WriteLine(methodTranslator.Translate());
                     Helpers.addTranslatedMethod(methodDefinition);
                 }
                 catch (InvalidOperationException ex)
                 {
                     Console.WriteLine("WARNING: Exception thrown while translating method (omitting): " + BoogieMethod.From(methodDefinition).Name);
                     if (!Settings.SilentExceptionsForMethods)
                     {
                         throw ex;
                     }
                 }
             }
         }
     }
 }