Example #1
0
        public Constant Make(Microsoft.Boogie.Type t)
        {
            Constant c;

            ConstantsByType.TryGetValue(t, out c);
            if (c == null)
            {
                c = Factory.MakeConstant("AA_CONST_" + t.ToString(), t);
                ConstantsByType.Add(t, c);
                Constants.Add(c);
            }
            return(c);
        }
Example #2
0
        private Procedure GetInitLocalsProc(Microsoft.Boogie.Type ty)
        {
            var typeToStr = ty.ToString().Replace('[', '_').Replace(']', '_');

            if (!typeToInitLocalsProc.ContainsKey(typeToStr))
            {
                var outVar = new Formal(Token.NoToken, new TypedIdent(Token.NoToken, "x", ty), false);
                var reProc = new Procedure(Token.NoToken, initLocProcName + typeToStr, new List <TypeVariable>(), new List <Variable>(), new List <Variable> {
                    outVar
                }, new List <Requires>(), new List <IdentifierExpr>(), new List <Ensures>());
                typeToInitLocalsProc.Add(typeToStr, reProc);
            }
            return(typeToInitLocalsProc[typeToStr]);
        }
Example #3
0
        private void AddUpdateLocksetFunc(Microsoft.Boogie.Type type = null)
        {
            var str = "_UPDATE_CLS_$";

            var inParams  = new List <Variable>();
            var outParams = new List <Variable>();
            var in1       = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken,
                                                                            "lock", this.AC.MemoryModelType));
            var in2 = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken,
                                                                      "isLocked", Microsoft.Boogie.Type.Bool));

            if (type != null)
            {
                str += type.ToString() + "$";
                outParams.Add(new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken,
                                                                              "$r", type)));
            }

            inParams.Add(in1);
            inParams.Add(in2);

            Procedure proc = new Procedure(Token.NoToken, str + this.EP.Name,
                                           new List <TypeVariable>(), inParams, outParams,
                                           new List <Requires>(), new List <IdentifierExpr>(), new List <Ensures>());

            proc.AddAttribute("inline", new object[] { new LiteralExpr(Token.NoToken, BigNum.FromInt(1)) });

            foreach (var ls in this.AC.CurrentLocksets)
            {
                if (this.ShouldSkipLockset(ls))
                {
                    continue;
                }

                proc.Modifies.Add(new IdentifierExpr(ls.Id.tok, ls.Id));
            }

            this.AC.TopLevelDeclarations.Add(proc);
            this.AC.ResContext.AddProcedure(proc);

            Block b = new Block(Token.NoToken, "_UPDATE", new List <Cmd>(), new ReturnCmd(Token.NoToken));

            foreach (var ls in this.AC.CurrentLocksets)
            {
                if (this.ShouldSkipLockset(ls))
                {
                    continue;
                }

                List <AssignLhs> newLhss = new List <AssignLhs>();
                List <Expr>      newRhss = new List <Expr>();

                newLhss.Add(new SimpleAssignLhs(ls.Id.tok, new IdentifierExpr(ls.Id.tok, ls.Id)));
                newRhss.Add(new NAryExpr(Token.NoToken, new IfThenElse(Token.NoToken),
                                         new List <Expr>(new Expr[] { Expr.Eq(new IdentifierExpr(in1.tok, in1),
                                                                              new IdentifierExpr(ls.Lock.tok, ls.Lock)),
                                                                      new IdentifierExpr(in2.tok, in2), new IdentifierExpr(ls.Id.tok, ls.Id) })));

                var assign = new AssignCmd(Token.NoToken, newLhss, newRhss);
                b.Cmds.Add(assign);
            }

            Implementation impl = new Implementation(Token.NoToken, str + this.EP.Name,
                                                     new List <TypeVariable>(), inParams, outParams,
                                                     new List <Variable>(), new List <Block>());

            if (type != null)
            {
                var bVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken,
                                                                           "$b", Microsoft.Boogie.Type.Bool));
                var bVarId = new IdentifierExpr(Token.NoToken, bVar);
                impl.LocVars.Add(bVar);
                b.Cmds.Add(new HavocCmd(Token.NoToken, new List <IdentifierExpr> {
                    bVarId
                }));
                b.Cmds.Add(new AssignCmd(Token.NoToken,
                                         new List <AssignLhs> {
                    new SimpleAssignLhs(Token.NoToken,
                                        new IdentifierExpr(Token.NoToken, outParams[0]))
                },
                                         new List <Expr> {
                    bVarId
                }));
            }

            impl.Blocks.Add(b);
            impl.Proc = proc;
            impl.AddAttribute("inline", new object[] { new LiteralExpr(Token.NoToken, BigNum.FromInt(1)) });

            this.AC.TopLevelDeclarations.Add(impl);
        }
Example #4
0
 private string SanitizeTypeName(btype type)
 {
     return(type.ToString().Replace("[", "$$").Replace("]", "$$"));
 }
Example #5
0
        public static string GetSMTLIBType(Microsoft.Boogie.Type T)
        {
            Microsoft.Boogie.Type theType = null;

            // Handle type synonyms. E.g. ``type arrayId = bv2``
            // Perhaps we should run a pass in the program to remove
            // type synonyms?
            theType = T;
            while (theType is TypeSynonymAnnotation)
            {
                theType = (theType as TypeSynonymAnnotation).ExpandedType;
            }

            if (theType is BvType)
            {
                var BVT = theType as BvType;
                return("(_ BitVec " + BVT.Bits + ")");
            }
            else if (theType is BasicType)
            {
                var ST = (theType as BasicType).T;
                switch (ST)
                {
                case SimpleType.Bool:
                    return("Bool");

                case SimpleType.Int:
                    return("Int");

                case SimpleType.Real:
                    return("Real");

                default:
                    throw new NotImplementedException("Unsupported SimpleType " + ST.ToString());
                }
            }
            else if (theType is MapType)
            {
                var MT = theType as MapType;
                // We are using Z3's Native ArrayTheory (allows for Arrays of Arrays) here. I don't know if other Solvers support this.
                Debug.Assert(MT.Arguments.Count >= 1, "MapType has too few arguments");
                string mapTypeAsString = "";
                foreach (var domainType in MT.Arguments)
                {
                    mapTypeAsString += "(Array " + GetSMTLIBType(domainType) + " ";
                }

                // Now print the final result from the map (the codomain)
                mapTypeAsString += GetSMTLIBType(MT.Result) + " ";

                // Now add closing braces
                for (int index = 0; index < MT.Arguments.Count; ++index)
                {
                    mapTypeAsString += ")";
                }
                return(mapTypeAsString);
            }
            else if (theType is CtorType)
            {
                var CT = theType as CtorType;
                return(GetCustomSortName(CT.Decl));
            }
            else
            {
                throw new NotImplementedException("The type " + theType.ToString() + " is not supported");
            }
        }
        public Bpl.Expr FromUnion(Bpl.IToken tok, Bpl.Type boogieType, Bpl.Expr expr, bool isStruct)
        {
            if (boogieType == UnionType || boogieType == RefType)
            {
                return(expr);
            }

            Bpl.Function conversion = null;
            if (boogieType == Bpl.Type.Bool)
            {
                conversion = this.Union2Bool;
            }
            else if (boogieType == Bpl.Type.Int)
            {
                conversion = this.Union2Int;
            }
            else if (boogieType == RealType)
            {
                conversion = this.Union2Real;
            }
            else
            {
                throw new InvalidOperationException(String.Format("Unknown Boogie type: '{0}'", boogieType.ToString()));
            }

            var callExpr = new Bpl.NAryExpr(
                tok,
                new Bpl.FunctionCall(conversion),
                new List <Bpl.Expr>(new Bpl.Expr[] { expr })
                );

            callExpr.Type = boogieType;
            return(callExpr);
        }
        public Bpl.Expr ToUnion(Bpl.IToken tok, Bpl.Type boogieType, Bpl.Expr expr, bool isStruct, Bpl.StmtListBuilder builder)
        {
            if (boogieType == UnionType || boogieType == RefType)
            {
                return(expr);
            }

            Bpl.Expr callConversion;
            if (boogieType == Bpl.Type.Bool)
            {
                callConversion = new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Bool2Union), new List <Bpl.Expr>(new Bpl.Expr[] { expr }));
                builder.Add(
                    new Bpl.AssumeCmd(tok,
                                      Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq,
                                                      new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Union2Bool), new List <Bpl.Expr>(new Bpl.Expr[] { callConversion })),
                                                      expr)));
            }
            else if (boogieType == Bpl.Type.Int)
            {
                callConversion = new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Int2Union), new List <Bpl.Expr>(new Bpl.Expr[] { expr }));
                builder.Add(
                    new Bpl.AssumeCmd(tok,
                                      Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq,
                                                      new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Union2Int), new List <Bpl.Expr>(new Bpl.Expr[] { callConversion })),
                                                      expr)));
            }
            else if (boogieType == RealType)
            {
                callConversion = new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Real2Union), new List <Bpl.Expr>(new Bpl.Expr[] { expr }));
                builder.Add(
                    new Bpl.AssumeCmd(tok,
                                      Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq,
                                                      new Bpl.NAryExpr(tok, new Bpl.FunctionCall(this.Union2Real), new List <Bpl.Expr>(new Bpl.Expr[] { callConversion })),
                                                      expr)));
            }
            else
            {
                throw new InvalidOperationException(String.Format("Unknown Boogie type: '{0}'", boogieType.ToString()));
            }
            return(callConversion);
        }