Ejemplo n.º 1
0
        public static String GetExternalMethodDefinition(IMethodReference methodRef)
        {
            var methodName = BoogieMethod.From(methodRef).Name;

            if (Helpers.IsCurrentlyMissing(methodRef))
            {
                // TODO(rcastano): Add logger. Print this as INFO or WARNING level.
                Console.WriteLine("WARNING: Creating non-deterministic definition for missing method: " + methodName);
            }
            var parameters = Helpers.GetParametersWithBoogieType(methodRef);
            var returnType = String.Empty;

            if (Settings.NewAddrModelling)
            {
                #region Return variables in NewAddrModelling. We should only add the return variable type from CCI
                if (Helpers.GetMethodBoogieReturnType(methodRef).Equals(Helpers.BoogieType.Void))
                {
                    returnType = "returns ()";
                }
                else
                {
                    var returnVariables = new List <String>();
                    returnVariables.Add(String.Format("$result : {0}", Helpers.GetMethodBoogieReturnType(methodRef)));
                    returnType = String.Format("returns ({0})", String.Join(",", returnVariables));
                }
                #endregion
            }
            else
            {
                #region Return variables in old memory model. We need to add 'ref' parameters as return variables (in addition to the return variable from CCI).
                if (Helpers.GetMethodBoogieReturnType(methodRef).Equals(Helpers.BoogieType.Void))
                {
                    if (methodRef.Parameters.Any(p => p.IsByReference))
                    {
                        var returnVariables = new List <String>();
                        returnVariables = methodRef.Parameters.Where(p => p.IsByReference).Select(p => String.Format("v{0}$out : {1}", p.Index, Helpers.GetBoogieType(p))).ToList();
                        returnType      = String.Format("returns ({0})", String.Join(",", returnVariables));
                    }
                }
                else
                {
                    var returnVariables = new List <String>();
                    returnVariables = methodRef.Parameters.Where(p => p.IsByReference).Select(p => String.Format("v{0}$out : {1}", p.Index, Helpers.GetBoogieType(p))).ToList();
                    returnVariables.Add(String.Format("$result : {0}", Helpers.GetMethodBoogieReturnType(methodRef)));
                    returnType = String.Format("returns ({0})", String.Join(",", returnVariables));
                }
                #endregion
            }

            var t = new BoogieProcedureTemplate(methodName, " {:extern} ", StatementList.Empty, StatementList.Empty, parameters, returnType, true);

            return(t.TransformText());
        }
Ejemplo n.º 2
0
        public string SetProcedureStub(IMethodReference method)
        {
            FieldTranslator field      = new FieldTranslator();
            var             boogieName = field.BoogieNameForField(method.ContainingType, GetFieldName(method));
            var             paramType  = Helpers.GetBoogieType(method.Parameters.ElementAt(0));

            var get = new StatementList();

            get.Add(BoogieStatement.FromString(boogieName + "[obj] := " + "val;"));

            var t = new BoogieProcedureTemplate(BoogieMethod.From(method).Name, "", StatementList.Empty, get, "obj : Ref, val : " + paramType, String.Empty, false);

            return(t.TransformText());
        }
Ejemplo n.º 3
0
        public string GetProcedureStub(IMethodReference method)
        {
            FieldTranslator field      = new FieldTranslator();
            var             boogieName = field.BoogieNameForField(method.ContainingType, GetFieldName(method));
            var             boogieType = Helpers.GetBoogieType(method.Type);

            var get = new StatementList();

            get.Add(BoogieStatement.FromString("$result := " + boogieName + "[obj];"));

            var t = new BoogieProcedureTemplate(BoogieMethod.From(method).Name, "", StatementList.Empty, get, "obj : Ref", String.Format(" returns  ( $result : {0})", boogieType.ToString()), false);

            return(t.TransformText());
        }
Ejemplo n.º 4
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();
            var parametersWithTypes = Helpers.GetParametersWithBoogieType(methodDefinition);
            var returnTypeIfAny     = TranslateReturnTypeIfAny();

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

            return(boogieProcedureTemplate.TransformText());
        }
Ejemplo n.º 5
0
        public string AsyncMethodBuilderStartStub(bool genericVersion)
        {
            StatementList localVars       = new StatementList();
            StatementList instructions    = new StatementList();
            var           param0          = new BoogieVariable(Helpers.BoogieType.Addr, "param0");
            var           moveNextMethods = stateMachinesTypes.Select(t => t.Members.Where(m => m.Name.Value.Contains("MoveNext")).First()).Cast <IMethodDefinition>();
            var           ifCases         = moveNextMethods.Select(m => Invoke(m, param0));

            foreach (var ifCase in ifCases)
            {
                instructions.Add(ifCase);
            }

            string procedureName     = genericVersion ? "System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start``1$``0$" : "System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start``1$``0$";
            var    procedureTemplate = new BoogieProcedureTemplate(procedureName, "", localVars, instructions, "this : Ref,param0: Addr", String.Empty, false);

            return(procedureTemplate.TransformText());
        }
Ejemplo n.º 6
0
        public string AsyncStubsScheduleTask(bool genericVersion = false)
        {
            StatementList localVars = new StatementList();

            localVars.Add(BoogieStatement.FromString("var state : bool;"));

            StatementList instructions = new StatementList();
            var           param0       = new BoogieVariable(Helpers.BoogieType.Object, "sm");


            var procIsCompleted = genericVersion ? BoogieMethod.AsyncStubsTaskAwaiterIsCompletedGeneric : BoogieMethod.AsyncStubsTaskAwaiterIsCompleted;
            var awaiterVar      = new BoogieVariable(Helpers.BoogieType.Object, "awaiter");
            var stateVar        = new BoogieVariable(Helpers.BoogieType.Bool, "state");
            var argsList        = new List <Expression>();

            argsList.Add(awaiterVar);
            var resList = new List <BoogieVariable>();

            resList.Add(stateVar);
            var callIsCompleted = BoogieStatement.ProcedureCall(procIsCompleted, argsList, resList, stateVar);

            var assume = BoogieStatement.Assume(Expression.BinaryOperationExpression(stateVar, new Expression(Helpers.BoogieType.Bool, "true"), Backend.ThreeAddressCode.Instructions.BinaryOperation.Eq));

            var yield = BoogieStatement.FromString("yield;");

            // it is crucial to yield before anything else
            instructions.Add(yield);
            instructions.Add(callIsCompleted);
            instructions.Add(assume);

            var moveNextMethods = stateMachinesTypes.Select(t => t.Members.Where(m => m.Name.Value.Contains("MoveNext")).First()).Cast <IMethodDefinition>();
            var ifCases         = moveNextMethods.Select(m => Invoke(m, param0));

            foreach (var ifCase in ifCases)
            {
                instructions.Add(ifCase);
            }

            string procedureName = genericVersion ? "$AsyncStubs`1$ScheduleTask" : "$AsyncStubs$ScheduleTask";

            var procedureTemplate = new BoogieProcedureTemplate(procedureName, "", localVars, instructions, "awaiter : Object, sm : Object", String.Empty, false);

            return(procedureTemplate.TransformText());
        }