Example #1
0
        public static void CreateAsyncStartMethod(StreamWriter sw)
        {
            StatementList stmts = new StatementList();

            var boogieGetTypeRes = BoogieVariable.GetTempVar(Helpers.ObjectType(), null /* need dict with used var names*/, prefix: "asyncType");
            var stateMachineVar  = BoogieVariable.GetTempVar(Helpers.ObjectType(), null /* need dict with used var names*/, prefix: "stateMachineCopy");

            stmts.Add(BoogieStatement.VariableDeclaration(stateMachineVar));
            stmts.Add(BoogieStatement.VariableDeclaration(boogieGetTypeRes));
            stmts.Add(BoogieStatement.FromString($"{stateMachineVar.Expr} := stateMachine;"));
            foreach (var asyncMethod in Helpers.asyncMoveNexts)
            {
                var asyncType = asyncMethod.ContainingTypeDefinition;
                var bg        = BoogieGenerator.Instance();
                stmts.Add(bg.ProcedureCall(BoogieMethod.GetTypeMethod, new List <Expression> {
                    stateMachineVar
                }, boogieGetTypeRes));
                StatementList ifStmts = new StatementList();
                ifStmts.Add(BoogieGenerator.Instance().ProcedureCall(BoogieMethod.From(asyncMethod), new List <Expression> {
                    stateMachineVar
                }));

                stmts.Add(BoogieStatement.If(Expression.Subtype(boogieGetTypeRes, asyncType), ifStmts));
            }
            stmts.Add(BoogieStatement.FromString($"v0$out := {stateMachineVar.Expr};"));

            sw.WriteLine(@"procedure {:extern} System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start``1$``0$(this : Ref,stateMachine : Ref) returns (v0$out : Ref) {");
            foreach (var stmt in stmts)
            {
                sw.WriteLine(stmt.Stmt);
            }
            sw.WriteLine("}");
        }
Example #2
0
        public static string CreateDefaultValuesStaticVariablesProcedure()
        {
            #region Create body of the procedure
            StatementList body = new StatementList();

            foreach (IFieldReference field in FieldTranslator.GetFieldReferences())
            {
                if (field.IsStatic)
                {
                    BoogieGenerator   bg = BoogieGenerator.Instance();
                    StaticFieldAccess staticFieldAccess = new StaticFieldAccess(field);

                    body.Add(bg.WriteStaticField(staticFieldAccess, GetDefaultConstant(Helpers.GetBoogieType(field.Type))));
                }
            }
            #endregion

            string        procedureName       = "$default_values_static_fields";
            string        attributes          = String.Empty;
            StatementList localVariables      = new StatementList();
            String        parametersWithTypes = String.Empty;
            String        returnTypeIfAny     = String.Empty;

            BoogieProcedureTemplate temp = new BoogieProcedureTemplate(procedureName, attributes, localVariables, body, parametersWithTypes, returnTypeIfAny, false);
            return(temp.TransformText());
        }
Example #3
0
        public StatementList CallWriteArrayElement(Expression array, Expression index, Expression value)
        {
            var l = new List <Expression>();

            l.Add(array);
            l.Add(index);
            l.Add(value);
            return(BoogieGenerator.Instance().ProcedureCall(BoogieMethod.WriteArrayElement, l));
        }
Example #4
0
        public StatementList CallStringProcedure(BoogieMethod method, IVariable op1, IInmediateValue op2, BoogieVariable boogieVariable)
        {
            Contract.Assert(!(op2 is Constant c) || c.Value == null);

            var        arg1 = BoogieGenerator.Instance().ReadAddr(op1);
            Expression arg2 = null;

            if (op2 is Constant constant && constant.Value == null)
            {
                arg2 = BoogieGenerator.Instance().NullObject();
            }
Example #5
0
        public static ISet <String> GetFieldDefinitions()
        {
            ISet <String> values = new HashSet <String>();

            foreach (var item in fieldNames)
            {
                var def = BoogieGenerator.Instance().GetFieldDefinition(item.Key, item.Value);
                values.Add(def);
            }

            return(values);
        }
Example #6
0
        public static string CreateStaticVariablesAllocProcedure()
        {
            string        procedureName       = "$allocate_static_fields";
            string        attributes          = String.Empty;
            StatementList body                = BoogieGenerator.Instance().AllocStaticVariables();
            StatementList localVariables      = new StatementList();
            String        parametersWithTypes = String.Empty;
            String        returnTypeIfAny     = String.Empty;

            BoogieProcedureTemplate temp = new BoogieProcedureTemplate(procedureName, attributes, localVariables, body, parametersWithTypes, returnTypeIfAny, false);

            return(temp.TransformText());
        }
Example #7
0
        StatementList TranslateLocalVariables(Dictionary <string, BoogieVariable> temporalVariables)
        {
            StatementList localVariablesStmts = new StatementList();
            var           bg = BoogieGenerator.Instance();

            var allVariables = methodBody.Variables.Union(methodBody.Parameters).ToList();

            localVariablesStmts.Add(bg.DeclareLocalVariables(allVariables, temporalVariables));

            localVariablesStmts.Add(bg.AllocLocalVariables(allVariables));

            // replace for class generated by compiler
            return(localVariablesStmts);
        }
Example #8
0
        public static void CreateAllAsyncMethods(StreamWriter sw)
        {
            StatementList stmts = new StatementList();

            Action <StatementList> fStmt = (s => stmts.Add(s));
            Action <string>        fStr  = (s => fStmt(BoogieStatement.FromString(s)));

            var taskVar       = BoogieVariable.GetTempVar(Helpers.ObjectType(), null, "task");
            var objectTypeStr = Helpers.ObjectType().FirstUppercase();

            fStr($"var resultAsync : [{objectTypeStr}] {objectTypeStr};");
            fStr($"var builder2task: [{objectTypeStr}] {objectTypeStr};");
            fStr($"procedure {{:extern}} System.Threading.Tasks.Task`1.get_Result(this : {objectTypeStr}) returns ($result : {objectTypeStr}) {{");
            fStr("$result := resultAsync[this];");
            fStr("}");
            fStr("");
            fStr($"procedure {{:extern}} System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Create() returns ($result : {objectTypeStr}) {{");
            fStmt(BoogieStatement.VariableDeclaration(taskVar));
            var bg = BoogieGenerator.Instance();

            fStmt(bg.AllocObject(taskVar));
            fStmt(BoogieStatement.Assume(Expression.IsTask(taskVar)));
            var resultVar = BoogieVariable.ResultVar(Helpers.ObjectType());

            fStmt(bg.AllocObject(resultVar));

            fStmt(BoogieStatement.Assume(Expression.IsAsyncTaskMethodBuilder(resultVar)));
            fStr($"builder2task[$result] := {taskVar.Expr};");
            fStr("}");
            fStr($"procedure {{:extern}} System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.get_Task(this : {objectTypeStr}) returns ($result : {objectTypeStr}) {{");
            fStr("$result := builder2task[this];");
            fStr("}");
            fStr($"procedure {{:extern}} System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.SetResult$`0(this : {objectTypeStr},result : {objectTypeStr}) {{");
            fStr($"var task : {objectTypeStr};");
            fStr("task := builder2task[this];");
            fStr($"resultAsync[task] := result;");
            fStr("}");
            foreach (var stmt in stmts)
            {
                sw.WriteLine(stmt.Stmt);
            }
            CreateAsyncStartMethod(sw);
        }
Example #9
0
        public static string CreateStaticConstructorsCallsProcedure()
        {
            StatementList body = new StatementList();

            foreach (var staticConstructor in staticConstructors)
            {
                var ctor = BoogieMethod.From(staticConstructor);
                body.Add(BoogieGenerator.Instance().ProcedureCall(ctor, new List <Expression>(), null));
            }

            string        procedureName       = "$call_static_constructors";
            string        attributes          = String.Empty;
            StatementList localVariables      = new StatementList();
            String        parametersWithTypes = String.Empty;
            String        returnTypeIfAny     = String.Empty;

            BoogieProcedureTemplate temp = new BoogieProcedureTemplate(procedureName, attributes, localVariables, body, parametersWithTypes, returnTypeIfAny, false);

            return(temp.TransformText());
        }
Example #10
0
        private StatementList Invoke(IMethodDefinition member, BoogieVariable receiver)
        {
            Expression receiverObject = receiver;

            if (receiver.Type.Equals(Helpers.BoogieType.Addr))
            {
                AddressExpression addrExpr = new AddressExpression(member.ContainingType, receiver);
                receiverObject = BoogieGenerator.Instance().ReadAddr(addrExpr);
            }

            Expression subtype = Expression.Subtype(Expression.DynamicType(receiverObject), member.ContainingType);

            StatementList     body         = new StatementList();
            List <Expression> argumentList = new List <Expression>();

            argumentList.Add(receiverObject);
            body.Add(BoogieGenerator.Instance().ProcedureCall(BoogieMethod.From(member), argumentList));
            body.Add(BoogieStatement.ReturnStatement);
            var ifExpr = BoogieStatement.If(subtype, body);

            return(ifExpr);
        }