private static void BuildAssignment(Sink sink, Bpl.StmtListBuilder stmtBuilder, List <Bpl.Variable> lvars, List <Bpl.Variable> rvars) { for (int i = 0; i < lvars.Count; i++) { Bpl.Variable lvar = lvars[i]; Bpl.Type ltype = lvar.TypedIdent.Type; Bpl.Variable rvar = rvars[i]; Bpl.Type rtype = rvar.TypedIdent.Type; Bpl.IdentifierExpr lexpr = Bpl.Expr.Ident(lvar); Bpl.Expr rexpr = Bpl.Expr.Ident(rvar); if (rtype == ltype) { // do nothing } else if (ltype == sink.Heap.UnionType) { rexpr = sink.Heap.ToUnion(Bpl.Token.NoToken, rtype, rexpr, false, stmtBuilder); } else if (rtype == sink.Heap.UnionType) { rexpr = sink.Heap.FromUnion(Bpl.Token.NoToken, ltype, rexpr, false); } else { System.Diagnostics.Debug.Assert(false); } stmtBuilder.Add(TranslationHelper.BuildAssignCmd(lexpr, rexpr)); } }
public bool isOutParam(bpl.Variable vbl) { if (vbl == null) { return(false); } return(hasAttrKey(vbl.Attributes, outParamAttr)); }
public bool isAllTypes(bpl.Variable vbl) { if (vbl == null) { return(false); } return(isTemplate(vbl) && !isGlobal(vbl) && !isInParam(vbl) && !isOutParam(vbl) && !isLocal(vbl)); }
public bool isNonTemplate(bpl.Variable vbl) { if (vbl == null) { return(false); } return(!hasAttrKey(vbl.Attributes, templateAttr)); }
public bool isLocal(bpl.Variable vbl) { if (vbl == null) { return(false); } return(hasAttrKey(vbl.Attributes, localAttr)); }
public BplMatch(string v2Prefix) { _v2Prefix = v2Prefix; _renamer = new Utils.VarRenamer(v2Prefix + ".", new string[] { }); _eqVarsCounter = 0; _numOverallAsserts = 0; _havocVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, HavocVarName, BType.Bool)); }
private Tuple<Variable, Variable> createNewExplainConstants(Variable v) { Contract.Assert(impl != null); Contract.Assert(CommandLineOptions.Clo.ExplainHoudini); Variable v1 = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("{0}_{1}_{2}", v.Name, impl.Name, "pos"), Microsoft.Boogie.BasicType.Bool)); Variable v2 = new Constant(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("{0}_{1}_{2}", v.Name, impl.Name, "neg"), Microsoft.Boogie.BasicType.Bool)); return Tuple.Create(v1, v2); }
private void addPhoneTopLevelDeclarations() { if (PhoneCodeHelper.instance().PhoneNavigationToggled) { Bpl.Variable continueOnPageVar = sink.FindOrCreateGlobalVariable(PhoneCodeHelper.BOOGIE_CONTINUE_ON_PAGE_VARIABLE, Bpl.Type.Bool); sink.TranslatedProgram.AddTopLevelDeclaration(continueOnPageVar); Bpl.Variable navigationCheckVar = sink.FindOrCreateGlobalVariable(PhoneCodeHelper.BOOGIE_NAVIGATION_CHECK_VARIABLE, Bpl.Type.Bool); sink.TranslatedProgram.AddTopLevelDeclaration(navigationCheckVar); } }
public override void TraverseChildren(IFieldDefinition fieldDefinition) { Bpl.Variable fieldVar = this.sink.FindOrCreateFieldVariable(fieldDefinition); // if tracked by the phone plugin, we need to find out the bpl assigned name for future use if (PhoneCodeHelper.instance().PhonePlugin != null) { trackControlVariableName(fieldDefinition, fieldVar); trackNavigationVariableName(fieldDefinition, fieldVar); } }
public bpl.Declaration getExistConstDeclaration(bpl.Variable vbl) { var res = new bpl.Constant(bpl.Token.NoToken, new bpl.TypedIdent(bpl.Token.NoToken, vbl.Name, vbl.TypedIdent.Type), false); var paramList = new List <object>(); paramList.Add(new bpl.LiteralExpr(bpl.Token.NoToken, true)); var attr = new bpl.QKeyValue(bpl.Token.NoToken, existsConstAttr, paramList, null); attr.Next = res.Attributes; res.Attributes = attr; return(res); }
public Bpl.Variable getBoogieVariableForName(string varName) { Bpl.Variable boogieVar = null; try { boogieVar = boogieObjects[varName] as Bpl.Variable; } catch (KeyNotFoundException) { } if (boogieVar == null) { throw new ArgumentException("The boogie variable " + varName + " is not defined."); } return(boogieVar); }
private static void GenerateInAndOutExprs(Bpl.Expr e, List <Bpl.Variable> invars, List <Bpl.Variable> outvars, out List <Bpl.Expr> inExprs, out List <Bpl.IdentifierExpr> outExprs) { inExprs = new List <Bpl.Expr>(); inExprs.Add(e); for (int i = 1; i < invars.Count; i++) { Bpl.Variable f = invars[i]; inExprs.Add(Bpl.Expr.Ident(f)); } outExprs = new List <Bpl.IdentifierExpr>(); foreach (Bpl.Formal f in outvars) { outExprs.Add(Bpl.Expr.Ident(f)); } }
public MethodParameter(IParameterDefinition parameterDefinition, Bpl.Type ptype) { this.underlyingParameter = parameterDefinition; var parameterToken = parameterDefinition.Token(); var typeToken = parameterDefinition.Type.Token(); var parameterName = TranslationHelper.TurnStringIntoValidIdentifier(parameterDefinition.Name.Value); if (String.IsNullOrWhiteSpace(parameterName)) parameterName = "P" + parameterDefinition.Index.ToString(); this.inParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$in", ptype), true); if (parameterDefinition.IsByReference) { this.outParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$out", ptype), false); } else { this.outParameterCopy = new Bpl.LocalVariable(parameterToken, new Bpl.TypedIdent(typeToken, parameterName, ptype)); } }
private Function TriggerFunction(Variable v) { PopulateExistsVars(v); if (firstExistsVars.Contains(v)) { return first.TriggerFunction(v); } else if (secondExistsVars.Contains(v)) { return second.TriggerFunction(v); } else { Debug.Assert(false); return null; } }
//////////////////////////////////////////////////////////////////////////////////// //TODO:Check if we get functions here private Term makeExpression(QuantifierExpr qe, bool old) { Term e = null; { List <Logic.Variable> quantifiedVariables = new List <Logic.Variable>(); foreach (Microsoft.Boogie.Variable bv in qe.Dummies) { quantifiedVariables.Add(new Logic.Basic.Variable(bv.TypedIdent.Name)); } foreach (var v in quantifiedVariables) { boundVariables.Add(v.name, v); } e = makeExpression(qe.Body, old); foreach (var v in quantifiedVariables) { boundVariables.Remove(v.name); } } { Logic.Quantifier q = makeQuantifier(qe.Kind); for (int i = qe.Dummies.Length - 1; i >= 0; i--) { Microsoft.Boogie.Variable v = qe.Dummies[i]; Logic.Sort ls = new Logic.Boogie.BoogieSort(v.TypedIdent.Type); Logic.Variable lv = new Logic.Boogie.BoogieVariable(v); e = new Boogie.QuantifiedExpression(qe, q, lv, ls, e); } } { Logic.TypeQuantifier tq = makeTypeQuantifier(qe.Kind); for (int i = qe.TypeParameters.Length - 1; i >= 0; i--) { Microsoft.Boogie.TypeVariable btv = qe.TypeParameters[i]; Logic.TypeVariable tv = new Logic.Boogie.TypeVariable(btv); e = new Boogie.QuantifiedTypeExpression(qe, tq, tv, e); } } return(e); }
public MethodParameter(IParameterDefinition parameterDefinition) { this.underlyingParameter = parameterDefinition; Bpl.Type ptype = Bpl.Type.Int; var parameterToken = parameterDefinition.Token(); var typeToken = parameterDefinition.Type.Token(); var parameterName = parameterDefinition.Name.Value; if (!parameterDefinition.IsOut) { this.inParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$in", ptype), true); } if (parameterDefinition.IsByReference || parameterDefinition.IsOut) { this.outParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$out", ptype), false); } else { this.outParameterCopy = new Bpl.LocalVariable(parameterToken, new Bpl.TypedIdent(typeToken, parameterName, ptype)); } }
public MethodParameter(IParameterDefinition parameterDefinition, Bpl.Type ptype) { this.underlyingParameter = parameterDefinition; var parameterToken = parameterDefinition.Token(); var typeToken = parameterDefinition.Type.Token(); var parameterName = TranslationHelper.TurnStringIntoValidIdentifier(parameterDefinition.Name.Value); if (String.IsNullOrWhiteSpace(parameterName)) { parameterName = "P" + parameterDefinition.Index.ToString(); } this.inParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$in", ptype), true); if (parameterDefinition.IsByReference) { this.outParameterCopy = new Bpl.Formal(parameterToken, new Bpl.TypedIdent(typeToken, parameterName + "$out", ptype), false); } else { this.outParameterCopy = new Bpl.LocalVariable(parameterToken, new Bpl.TypedIdent(typeToken, parameterName, ptype)); } }
public override Variable VisitVariable(Variable node) { CivlAttributes.RemoveLayerAttribute(node); return base.VisitVariable(node); }
private int FindLocalVariableLayer(Declaration decl, Variable v, int enclosingProcLayerNum) { var layers = FindLayers(v.Attributes); if (layers.Count == 0) return int.MinValue; if (layers.Count > 1) { Error(decl, "Incorrect number of layers"); return int.MinValue; } if (layers[0] > enclosingProcLayerNum) { Error(decl, "Layer of local variable cannot be greater than the creation layer of enclosing procedure"); return int.MinValue; } return layers[0]; }
//Other special variables public bool isAtomicBlockVar(bpl.Variable variable) { return(variable != null && variable is bpl.GlobalVariable && variable.Name == atomicBlockVar); }
public Bpl.Procedure addHandlerStubCaller(Sink sink, IMethodDefinition def) { MethodBody callerBody = new MethodBody(); MethodDefinition callerDef = new MethodDefinition() { InternFactory = (def as MethodDefinition).InternFactory, ContainingTypeDefinition = def.ContainingTypeDefinition, IsStatic = true, Name = sink.host.NameTable.GetNameFor("BOOGIE_STUB_CALLER_" + def.Name.Value), Type = sink.host.PlatformType.SystemVoid, Body = callerBody, }; callerBody.MethodDefinition = callerDef; Sink.ProcedureInfo procInfo = sink.FindOrCreateProcedure(def); Sink.ProcedureInfo callerInfo = sink.FindOrCreateProcedure(callerDef); Bpl.LocalVariable[] localVars = new Bpl.LocalVariable[procInfo.Decl.InParams.Count]; Bpl.IdentifierExpr[] varExpr = new Bpl.IdentifierExpr[procInfo.Decl.InParams.Count]; for (int i = 0; i < procInfo.Decl.InParams.Count; i++) { Bpl.LocalVariable loc = new Bpl.LocalVariable(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, TranslationHelper.GenerateTempVarName(), procInfo.Decl.InParams[i].TypedIdent.Type)); localVars[i] = loc; varExpr[i] = new Bpl.IdentifierExpr(Bpl.Token.NoToken, loc); } Bpl.StmtListBuilder builder = new Bpl.StmtListBuilder(); builder.Add(getResetNavigationCheck(sink)); string pageXaml = PhoneCodeHelper.instance().PhonePlugin.getXAMLForPage(def.ContainingTypeDefinition.ToString()); Bpl.Variable boogieCurrentURI = sink.FindOrCreateFieldVariable(PhoneCodeHelper.CurrentURIFieldDefinition); Bpl.Constant boogieXamlConstant; if (pageXaml != null) { boogieXamlConstant = sink.FindOrCreateConstant(pageXaml); } else { boogieXamlConstant = null; } // NAVIGATION TODO: For now just assume we are in this page to be able to call the handler, this is NOT true for any handler // NAVIGATION TODO: ie, a network event handler if (boogieXamlConstant != null) { Bpl.AssumeCmd assumeCurrentPage = new Bpl.AssumeCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieCurrentURI), new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieXamlConstant))); builder.Add(assumeCurrentPage); } // NAVIGATION TODO: have to do the pair generation all in one go instead of having different files that need to be sed'ed boogieXamlConstant = sink.FindOrCreateConstant(BOOGIE_STARTING_URI_PLACEHOLDER); Bpl.AssumeCmd assumeStartPage = new Bpl.AssumeCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Eq, new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieCurrentURI), new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieXamlConstant))); builder.Add(assumeStartPage); builder.Add(new Bpl.CallCmd(Bpl.Token.NoToken, procInfo.Decl.Name, new List <Bpl.Expr>(varExpr), new List <Bpl.IdentifierExpr>())); boogieXamlConstant = sink.FindOrCreateConstant(BOOGIE_ENDING_URI_PLACEHOLDER); Bpl.AssertCmd assertEndPage = new Bpl.AssertCmd(Bpl.Token.NoToken, Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieCurrentURI), new Bpl.IdentifierExpr(Bpl.Token.NoToken, boogieXamlConstant))); Bpl.Expr guard = new Bpl.IdentifierExpr(Bpl.Token.NoToken, sink.FindOrCreateGlobalVariable(PhoneCodeHelper.BOOGIE_NAVIGATION_CHECK_VARIABLE, Bpl.Type.Bool)); Bpl.StmtListBuilder thenBuilder = new Bpl.StmtListBuilder(); thenBuilder.Add(assertEndPage); Bpl.IfCmd ifNavigated = new Bpl.IfCmd(Bpl.Token.NoToken, guard, thenBuilder.Collect(Bpl.Token.NoToken), null, new Bpl.StmtListBuilder().Collect(Bpl.Token.NoToken)); builder.Add(ifNavigated); Bpl.Implementation impl = new Bpl.Implementation(Bpl.Token.NoToken, callerInfo.Decl.Name, new List <Bpl.TypeVariable>(), new List <Bpl.Variable>(), new List <Bpl.Variable>(), new List <Bpl.Variable>(localVars), builder.Collect(Bpl.Token.NoToken), null, new Bpl.Errors()); sink.TranslatedProgram.AddTopLevelDeclaration(impl); return(impl.Proc); }
public override Variable VisitVariable(Variable node) { string domainName = FindDomainName(node); if (domainName != null) { if (!domainNameToCollectors.ContainsKey(domainName)) { domainNameToCollectors[domainName] = new Dictionary<Type,Function>(); } LinearKind kind = FindLinearKind(node); if (kind != LinearKind.LINEAR) { if (node is GlobalVariable || node is LocalVariable || (node is Formal && !(node as Formal).InComing)) { Error(node, "Variable must be declared linear (as opposed to linear_in or linear_out)"); } } } return base.VisitVariable(node); }
private Expr SubsetExprs(LinearDomain domain, HashSet<Variable> scope, Variable partition, int count, Expr expr) { foreach (Variable v in scope) { if (!domain.collectors.ContainsKey(v.TypedIdent.Type)) continue; Expr ie = new NAryExpr(Token.NoToken, new FunctionCall(domain.collectors[v.TypedIdent.Type]), new List<Expr> { Expr.Ident(v) }); expr = Expr.And(SubsetExpr(domain, ie, partition, count), expr); count++; } expr = new ExistsExpr(Token.NoToken, new List<Variable> { partition }, expr); expr.Resolve(new ResolutionContext(null)); expr.Typecheck(new TypecheckingContext(null)); return expr; }
public virtual Variable VisitVariable(Variable node) { Contract.Requires(node != null); Contract.Ensures(Contract.Result<Variable>() != null); node.TypedIdent = this.VisitTypedIdent(node.TypedIdent); return node; }
public string FindDomainName(Variable v) { if (globalVarToDomainName.ContainsKey(v)) return globalVarToDomainName[v]; if (inParamToLinearQualifier.ContainsKey(v)) return inParamToLinearQualifier[v].domainName; if (outParamToDomainName.ContainsKey(v)) return outParamToDomainName[v]; string domainName = QKeyValue.FindStringAttribute(v.Attributes, "linear"); if (domainName != null) return domainName; domainName = QKeyValue.FindStringAttribute(v.Attributes, "linear_in"); if (domainName != null) return domainName; return QKeyValue.FindStringAttribute(v.Attributes, "linear_out"); }
private static void trackControlVariableName(IFieldDefinition fieldDefinition, Bpl.Variable fieldVar) { INamespaceTypeReference namedContainerRef = fieldDefinition.ContainingType as INamespaceTypeReference; if (namedContainerRef != null) { string containerName = namedContainerRef.ContainingUnitNamespace.Unit.Name.Value + "." + namedContainerRef.Name.Value; IEnumerable <ControlInfoStructure> controls = PhoneCodeHelper.instance().PhonePlugin.getControlsForPage(containerName); if (controls != null) { ControlInfoStructure ctrlInfo = controls.FirstOrDefault(ctrl => ctrl.Name == fieldDefinition.Name.Value); if (ctrlInfo != null) { ctrlInfo.BplName = fieldVar.Name; } } } }
public Function TriggerFunction(Variable v) { if (!triggerFuns.ContainsKey(v)) { List<Variable> args = new List<Variable>(); args.Add(new Formal(v.tok, new TypedIdent(v.tok, "v", v.TypedIdent.Type), true)); Variable result = new Formal(v.tok, new TypedIdent(v.tok, "r", Type.Bool), false); triggerFuns[v] = new Function(v.tok, string.Format("Trigger_{0}_{1}", proc.Name, v.Name), args, result); } return triggerFuns[v]; }
private static void trackNavigationVariableName(IFieldDefinition fieldDefinition, Bpl.Variable fieldVar) { if (fieldDefinition.Name.Value.Equals(PhoneCodeHelper.IL_CURRENT_NAVIGATION_URI_VARIABLE)) { PhoneCodeHelper.instance().setBoogieNavigationVariable(fieldVar.Name); } }
private static void CreateDispatchMethod(Sink sink, ITypeDefinition type, HashSet <IMethodDefinition> delegates) { Contract.Assert(type.IsDelegate); IMethodDefinition invokeMethod = null; foreach (IMethodDefinition m in type.Methods) { if (m.Name.Value == "Invoke") { invokeMethod = m; break; } } try { IMethodDefinition unspecializedInvokeMethod = Sink.Unspecialize(invokeMethod).ResolvedMethod; Sink.ProcedureInfo invokeProcedureInfo = sink.FindOrCreateProcedure(unspecializedInvokeMethod); Bpl.Procedure invokeProcedure = (Bpl.Procedure)invokeProcedureInfo.Decl; invokeProcedure.AddAttribute("inline", Bpl.Expr.Literal(1)); Bpl.Formal delegateVariable = invokeProcedureInfo.ThisVariable; Bpl.IToken token = invokeMethod.Token(); List <Bpl.Variable> dispatchProcInExprs = new List <Bpl.Variable>(); for (int i = 1; i < invokeProcedure.InParams.Count; i++) { Bpl.Variable v = invokeProcedure.InParams[i]; dispatchProcInExprs.Add(v); } List <Bpl.Variable> dispatchProcOutExprs = new List <Bpl.Variable>(); foreach (Bpl.Variable v in invokeProcedure.OutParams) { dispatchProcOutExprs.Add(v); } List <Bpl.Variable> localVariables = new List <Bpl.Variable>(); Bpl.StmtListBuilder stmtBuilder = new Bpl.StmtListBuilder(); int localCounter = 0; foreach (IMethodDefinition defn in delegates) { Bpl.Constant c = sink.FindOrCreateDelegateMethodConstant(defn); Sink.ProcedureInfo delegateProcedureInfo = sink.FindOrCreateProcedure(defn); Bpl.Procedure delegateProcedure = (Bpl.Procedure)delegateProcedureInfo.Decl; Bpl.Formal thisVariable = delegateProcedureInfo.ThisVariable; int numArguments = defn.ParameterCount; List <Bpl.Variable> tempInputs = new List <Bpl.Variable>(); List <Bpl.Variable> tempOutputs = new List <Bpl.Variable>(); for (int i = 0; i < defn.ParameterCount; i++) { Bpl.Variable v = delegateProcedure.InParams[(thisVariable == null ? 0 : 1) + i]; Bpl.LocalVariable localVariable = new Bpl.LocalVariable(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, "local" + localCounter++, v.TypedIdent.Type)); localVariables.Add(localVariable); tempInputs.Add(localVariable); } for (int i = 0; i < delegateProcedure.OutParams.Count; i++) { Bpl.Variable v = delegateProcedure.OutParams[i]; Bpl.LocalVariable localVariable = new Bpl.LocalVariable(Bpl.Token.NoToken, new Bpl.TypedIdent(Bpl.Token.NoToken, "local" + localCounter++, v.TypedIdent.Type)); localVariables.Add(localVariable); tempOutputs.Add(localVariable); } List <Bpl.Expr> ins = new List <Bpl.Expr>(); List <Bpl.IdentifierExpr> outs = new List <Bpl.IdentifierExpr>(); if (!defn.IsStatic) { ins.Add(sink.ReadReceiver(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable))); } for (int i = 0; i < tempInputs.Count; i++) { ins.Add(Bpl.Expr.Ident(tempInputs[i])); } if (defn.IsGeneric) { for (int i = 0; i < defn.GenericParameterCount; i++) { ins.Add(new Bpl.NAryExpr(Bpl.Token.NoToken, new Bpl.FunctionCall(sink.FindOrCreateTypeParameterFunction(i)), new List <Bpl.Expr>(new Bpl.Expr[] { sink.ReadTypeParameters(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)) }))); } } if (defn.IsStatic) { int numTypeParameters = Sink.ConsolidatedGenericParameterCount(defn.ContainingType); for (int i = 0; i < numTypeParameters; i++) { ins.Add(new Bpl.NAryExpr(Bpl.Token.NoToken, new Bpl.FunctionCall(sink.FindOrCreateTypeParameterFunction(i)), new List <Bpl.Expr>(new Bpl.Expr[] { sink.ReadTypeParameters(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)) }))); } } for (int i = 0; i < tempOutputs.Count; i++) { outs.Add(Bpl.Expr.Ident(tempOutputs[i])); } Bpl.Expr bexpr = sink.ReadMethod(Bpl.Expr.Ident(c), Bpl.Expr.Ident(delegateVariable)); Bpl.StmtListBuilder ifStmtBuilder = new Bpl.StmtListBuilder(); System.Diagnostics.Debug.Assert(tempInputs.Count == dispatchProcInExprs.Count); if (tempInputs.Count > 0) { BuildAssignment(sink, ifStmtBuilder, tempInputs, dispatchProcInExprs); } ifStmtBuilder.Add(EmitDummySourceContext()); ifStmtBuilder.Add(new Bpl.CallCmd(token, delegateProcedure.Name, ins, outs)); System.Diagnostics.Debug.Assert(tempOutputs.Count == dispatchProcOutExprs.Count); if (tempOutputs.Count > 0) { BuildAssignment(sink, ifStmtBuilder, dispatchProcOutExprs, tempOutputs); } stmtBuilder.Add(new Bpl.IfCmd(bexpr.tok, bexpr, ifStmtBuilder.Collect(bexpr.tok), null, null)); } Bpl.Implementation dispatchImpl = new Bpl.Implementation(token, invokeProcedure.Name, new List <Bpl.TypeVariable>(), invokeProcedure.InParams, invokeProcedure.OutParams, localVariables, stmtBuilder.Collect(token) ); dispatchImpl.Proc = invokeProcedure; dispatchImpl.AddAttribute("inline", Bpl.Expr.Literal(1)); sink.TranslatedProgram.AddTopLevelDeclaration(dispatchImpl); } catch (TranslationException te) { throw new NotImplementedException(te.ToString()); } catch { throw; } finally { // Maybe this is a good place to add the procedure to the toplevel declarations } }
public override void TraverseChildren(ITryCatchFinallyStatement tryCatchFinallyStatement) { if (this.sink.Options.modelExceptions == 0) { this.Traverse(tryCatchFinallyStatement.TryBody); if (tryCatchFinallyStatement.FinallyBody != null) { this.Traverse(tryCatchFinallyStatement.FinallyBody); } return; } this.sink.nestedTryCatchFinallyStatements.Add(new Tuple <ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InTry)); this.Traverse(tryCatchFinallyStatement.TryBody); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(-1))); StmtBuilder.Add(new Bpl.GotoCmd(Bpl.Token.NoToken, new List <string>(new string[] { this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement) }))); this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1); StmtBuilder.AddLabelCmd(this.sink.FindOrCreateCatchLabel(tryCatchFinallyStatement)); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LocalExcVariable), Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable))); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.Heap.NullRef))); List <Bpl.StmtList> catchStatements = new List <Bpl.StmtList>(); List <Bpl.Expr> typeReferences = new List <Bpl.Expr>(); this.sink.nestedTryCatchFinallyStatements.Add(new Tuple <ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InCatch)); foreach (ICatchClause catchClause in tryCatchFinallyStatement.CatchClauses) { typeReferences.Insert(0, this.sink.FindOrCreateTypeReference(catchClause.ExceptionType, true)); StatementTraverser catchTraverser = this.factory.MakeStatementTraverser(this.sink, this.PdbReader, this.contractContext); if (catchClause.ExceptionContainer != Dummy.LocalVariable) { Bpl.Variable catchClauseVariable = this.sink.FindOrCreateLocalVariable(catchClause.ExceptionContainer); catchTraverser.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(catchClauseVariable), Bpl.Expr.Ident(this.sink.LocalExcVariable))); } catchTraverser.Traverse(catchClause.Body); catchTraverser.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Literal(-1))); catchTraverser.StmtBuilder.Add(new Bpl.GotoCmd(Bpl.Token.NoToken, new List <string>(new string[] { this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement) }))); catchStatements.Insert(0, catchTraverser.StmtBuilder.Collect(catchClause.Token())); } Bpl.IfCmd elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, Bpl.Expr.Literal(false), TranslationHelper.BuildStmtList(new Bpl.ReturnCmd(Bpl.Token.NoToken)), null, null); Bpl.Expr dynTypeOfOperand = this.sink.Heap.DynamicType(Bpl.Expr.Ident(this.sink.LocalExcVariable)); for (int i = 0; i < catchStatements.Count; i++) { Bpl.Expr expr = new Bpl.NAryExpr(Bpl.Token.NoToken, new Bpl.FunctionCall(this.sink.Heap.Subtype), new List <Bpl.Expr>(new Bpl.Expr[] { dynTypeOfOperand, typeReferences[i] })); elseIfCmd = new Bpl.IfCmd(Bpl.Token.NoToken, expr, catchStatements[i], elseIfCmd, null); } this.StmtBuilder.Add(elseIfCmd); this.StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.LocalExcVariable))); RaiseException(); this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1); this.StmtBuilder.AddLabelCmd(this.sink.FindOrCreateFinallyLabel(tryCatchFinallyStatement)); if (tryCatchFinallyStatement.FinallyBody != null) { this.sink.nestedTryCatchFinallyStatements.Add(new Tuple <ITryCatchFinallyStatement, Sink.TryCatchFinallyContext>(tryCatchFinallyStatement, Sink.TryCatchFinallyContext.InFinally)); Bpl.Variable savedExcVariable = this.sink.CreateFreshLocal(this.sink.Heap.RefType); Bpl.Variable savedLabelVariable = this.sink.CreateFreshLocal(Bpl.Type.Int); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(savedExcVariable), Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable))); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(savedLabelVariable), Bpl.Expr.Ident(this.sink.LabelVariable))); this.Traverse(tryCatchFinallyStatement.FinallyBody); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(savedExcVariable))); StmtBuilder.Add(TranslationHelper.BuildAssignCmd(Bpl.Expr.Ident(this.sink.LabelVariable), Bpl.Expr.Ident(savedLabelVariable))); this.sink.nestedTryCatchFinallyStatements.RemoveAt(this.sink.nestedTryCatchFinallyStatements.Count - 1); } GenerateDispatchContinuation(tryCatchFinallyStatement); StmtBuilder.AddLabelCmd(this.sink.FindOrCreateContinuationLabel(tryCatchFinallyStatement)); Bpl.Expr raiseExpr = Bpl.Expr.Binary(Bpl.BinaryOperator.Opcode.Neq, Bpl.Expr.Ident(this.sink.Heap.ExceptionVariable), Bpl.Expr.Ident(this.sink.Heap.NullRef)); RaiseException(raiseExpr); }
public string getShadowVarName(bpl.Variable glbl) { return(glbl.Name + shadowVarSuffix); }
public bool isTidOtherVar(bpl.Variable variable) { return(variable != null && variable is bpl.GlobalVariable && variable.Name == tidOther); }
public override Variable VisitVariable(Variable node) { Contract.Ensures(Contract.Result<Variable>() == node); this.VisitTypedIdent(node.TypedIdent); return node; }
public override Implementation VisitImplementation(Implementation node) { enclosingImpl = node; dummyLocalVar = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "og_dummy", Type.Bool)); Implementation impl = base.VisitImplementation(node); implMap[impl] = node; impl.LocVars.Add(dummyLocalVar); impl.Name = impl.Proc.Name; return impl; }
public override Variable VisitVariable(Variable node) { node.Attributes = RemoveLinearAttribute(node.Attributes); return base.VisitVariable(node); }
public override Variable VisitVariable(Variable node) { //Contract.Requires(node != null); Contract.Ensures(Contract.Result<Variable>() != null); return node; }
public LinearKind FindLinearKind(Variable v) { if (globalVarToDomainName.ContainsKey(v)) return LinearKind.LINEAR; if (inParamToLinearQualifier.ContainsKey(v)) return inParamToLinearQualifier[v].kind; if (outParamToDomainName.ContainsKey(v)) return LinearKind.LINEAR; if (QKeyValue.FindStringAttribute(v.Attributes, "linear") != null) { return LinearKind.LINEAR; } else if (QKeyValue.FindStringAttribute(v.Attributes, "linear_in") != null) { return LinearKind.LINEAR_IN; } else if (QKeyValue.FindStringAttribute(v.Attributes, "linear_out") != null) { return LinearKind.LINEAR_OUT; } else { Debug.Assert(false); return LinearKind.LINEAR; } }
public Expr/*?*/ Method(Variable v) { Contract.Requires(v != null); if(map.ContainsKey(v)) { return map[v]; } Expr e; if (nameMap != null && proc != null && nameMap.TryGetValue(UniqueName(v, proc), out e)) { return e; } return null; }
private Expr SubsetExpr(LinearDomain domain, Expr ie, Variable partition, int partitionCount) { Expr e = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapConstInt), new List<Expr> { new LiteralExpr(Token.NoToken, Microsoft.Basetypes.BigNum.FromInt(partitionCount)) }); e = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapEqInt), new List<Expr> { Expr.Ident(partition), e }); e = new NAryExpr(Token.NoToken, new FunctionCall(domain.mapImpBool), new List<Expr> { ie, e }); e = Expr.Eq(e, new NAryExpr(Token.NoToken, new FunctionCall(domain.mapConstBool), new List<Expr> { Expr.True })); return e; }
public bool InFrame(Variable v) { Contract.Requires(v != null); Contract.Requires(Frame != null); return Frame.Any(f => f.Decl == v); }
public Expr DisjointnessExpr(string domainName, Variable inputVar, HashSet<Variable> scope) { LinearDomain domain = linearDomains[domainName]; BoundVariable partition = new BoundVariable(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("partition_{0}", domainName), new MapType(Token.NoToken, new List<TypeVariable>(), new List<Type> { domain.elementType }, Microsoft.Boogie.Type.Int))); return SubsetExprs(domain, scope, partition, 1, SubsetExpr(domain, Expr.Ident(inputVar), partition, 0)); }
static Bpl.Expr BplForall(Bpl.Variable arg, Bpl.Trigger trg, Bpl.Expr body) { return(BplForall(Singleton(arg), trg, body)); }
void AssignPredicates(Graph<Block> blockGraph, DomRelation<Block> dom, DomRelation<Block> pdom, IEnumerable<Block> headerDominance, IEnumerator<Tuple<Block, bool>> i, Variable headPredicate, ref int predCount) { var header = i.Current.Item1; var regionPreds = new List<Tuple<Block, Variable>>(); var ownedPreds = new HashSet<Variable>(); ownedMap[header] = ownedPreds; if (headPredicate != null) { predMap[header] = headPredicate; defMap[header] = headPredicate; regionPreds.Add(new Tuple<Block, Variable>(header, headPredicate)); } while (i.MoveNext()) { var block = i.Current; if (block.Item2) { if (block.Item1 == header) { return; } } if (uni != null && uni.IsUniform(impl.Name, block.Item1)) { if (blockGraph.Headers.Contains(block.Item1)) { parentMap[block.Item1] = header; AssignPredicates(blockGraph, dom, pdom, headerDominance, i, headPredicate, ref predCount); } continue; } if (!block.Item2) { if (blockGraph.Headers.Contains(block.Item1)) { parentMap[block.Item1] = header; var loopPred = FreshPredicate(ref predCount); ownedPreds.Add(loopPred); AssignPredicates(blockGraph, dom, pdom, headerDominance, i, loopPred, ref predCount); } else { bool foundExisting = false; foreach (var regionPred in regionPreds) { if (dom.DominatedBy(block.Item1, regionPred.Item1) && pdom.DominatedBy(regionPred.Item1, block.Item1)) { predMap[block.Item1] = regionPred.Item2; foundExisting = true; break; } } if (!foundExisting) { var condPred = FreshPredicate(ref predCount); predMap[block.Item1] = condPred; defMap[block.Item1] = condPred; var headerIterator = headerDominance.GetEnumerator(); // Add the predicate to the loop header H that dominates the node (if one // exists) such that H does not dominate another header which also dominates // the node. Since predicates are owned by loop headers (or the program entry // node), this is the block 'closest' to block to which we are assigning a // that can be made to own the predicate. Block node = null; while (headerIterator.MoveNext()) { var current = headerIterator.Current; if (dom.DominatedBy(block.Item1, current)) { node = current; break; } } if (node != null) { ownedMap[node].Add(condPred); } else { // In this case the header is the program entry node. ownedPreds.Add(condPred); } regionPreds.Add(new Tuple<Block, Variable>(block.Item1, condPred)); } } } } }
public abstract Element Eliminate(Element element, Variable v);
private void SetupRefinementCheck(Implementation impl, out List<Variable> newLocalVars, out Dictionary<string, Variable> domainNameToInputVar, out Dictionary<string, Variable> domainNameToLocalVar, out Dictionary<Variable, Variable> ogOldGlobalMap) { pc = null; ok = null; alpha = null; beta = null; frame = null; newLocalVars = new List<Variable>(); Program program = linearTypeChecker.program; ogOldGlobalMap = new Dictionary<Variable, Variable>(); foreach (IdentifierExpr ie in globalMods) { Variable g = ie.Decl; LocalVariable l = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("og_global_old_{0}", g.Name), g.TypedIdent.Type)); ogOldGlobalMap[g] = l; newLocalVars.Add(l); } Procedure originalProc = implMap[impl].Proc; ActionInfo actionInfo = civlTypeChecker.procToActionInfo[originalProc]; if (actionInfo.createdAtLayerNum == this.layerNum) { pc = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "og_pc", Type.Bool)); newLocalVars.Add(pc); ok = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, "og_ok", Type.Bool)); newLocalVars.Add(ok); Dictionary<Variable, Expr> alwaysMap = new Dictionary<Variable, Expr>(); for (int i = 0; i < originalProc.InParams.Count; i++) { alwaysMap[originalProc.InParams[i]] = Expr.Ident(impl.InParams[i]); } for (int i = 0; i < originalProc.OutParams.Count; i++) { alwaysMap[originalProc.OutParams[i]] = Expr.Ident(impl.OutParams[i]); } Substitution always = Substituter.SubstitutionFromHashtable(alwaysMap); Dictionary<Variable, Expr> foroldMap = new Dictionary<Variable, Expr>(); foreach (IdentifierExpr ie in globalMods) { foroldMap[ie.Decl] = Expr.Ident(ogOldGlobalMap[ie.Decl]); } Substitution forold = Substituter.SubstitutionFromHashtable(foroldMap); frame = new HashSet<Variable>(civlTypeChecker.SharedVariables); foreach (Variable v in civlTypeChecker.SharedVariables) { if (civlTypeChecker.globalVarToSharedVarInfo[v].hideLayerNum <= actionInfo.createdAtLayerNum || civlTypeChecker.globalVarToSharedVarInfo[v].introLayerNum > actionInfo.createdAtLayerNum) { frame.Remove(v); } } AtomicActionInfo atomicActionInfo = actionInfo as AtomicActionInfo; if (atomicActionInfo == null) { beta = Expr.True; foreach (var v in frame) { beta = Expr.And(beta, Expr.Eq(Expr.Ident(v), foroldMap[v])); } alpha = Expr.True; } else { Expr betaExpr = (new MoverCheck.TransitionRelationComputation(civlTypeChecker.program, atomicActionInfo, frame, new HashSet<Variable>())).TransitionRelationCompute(true); beta = Substituter.ApplyReplacingOldExprs(always, forold, betaExpr); Expr alphaExpr = Expr.True; foreach (AssertCmd assertCmd in atomicActionInfo.gate) { alphaExpr = Expr.And(alphaExpr, assertCmd.Expr); alphaExpr.Type = Type.Bool; } alpha = Substituter.Apply(always, alphaExpr); } foreach (Variable f in impl.OutParams) { LocalVariable copy = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, string.Format("og_old_{0}", f.Name), f.TypedIdent.Type)); newLocalVars.Add(copy); ogOldGlobalMap[f] = copy; } } domainNameToInputVar = new Dictionary<string, Variable>(); domainNameToLocalVar = new Dictionary<string, Variable>(); { int i = impl.InParams.Count - linearTypeChecker.linearDomains.Count; foreach (string domainName in linearTypeChecker.linearDomains.Keys) { Variable inParam = impl.InParams[i]; domainNameToInputVar[domainName] = inParam; Variable l = new LocalVariable(Token.NoToken, new TypedIdent(Token.NoToken, inParam.Name + "_local", inParam.TypedIdent.Type)); domainNameToLocalVar[domainName] = l; newLocalVars.Add(l); i++; } } }
bool BruteForceCmd(Block b, int startidx, int endidx, Variable reachvar, Implementation impl, VerifierCallback callback) { Contract.Requires(b != null); Contract.Requires(reachvar != null); Contract.Requires(impl != null); Contract.Requires(callback != null); #region Modify Cmds List<Cmd> backup = b.Cmds; Contract.Assert(backup != null); List<Cmd> cs = new List<Cmd>(); for (int i = 0; i < startidx; i++) { cs.Add(b.Cmds[i]); } for (int i = startidx; i <= endidx; i++) { Cmd c = b.Cmds[i]; if (ContainsReachVariable(c)) { cs.Add(c); continue; } cs.Add(new AssertCmd(c.tok, Expr.True)); } for (int i = endidx + 1; i < b.Cmds.Length; i++) { cs.Add(b.Cmds[i]); } b.Cmds = cs; #endregion #region Recheck ProverInterface.Outcome outcome = ProverInterface.Outcome.Undetermined; if (!ReCheckImpl(reachvar, impl, callback, out outcome)) { b.Cmds = backup; return false; } #endregion if (outcome == ProverInterface.Outcome.Valid) { return true; } else if (outcome == ProverInterface.Outcome.Invalid) { b.Cmds = backup; if (startidx >= endidx) { if (!ContainsReachVariable(b.Cmds[endidx])) { // Console.Write(" Witness ("); // // ConsoleColor col = Console.ForegroundColor; // Console.ForegroundColor = ConsoleColor.White; // Console.Write("{0};{1}", b.Cmds[endidx].tok.line, b.Cmds[endidx].tok.col ); // Console.ForegroundColor = col; // Console.Write("): "); // Console.ForegroundColor = ConsoleColor.Yellow; // b.Cmds[endidx].Emit(new TokenTextWriter("<console>", Console.Out, false), 0); // Console.ForegroundColor = col; m_doomedCmds.Add(b.Cmds[endidx]); return true; } else { return false; } } else { int mid = startidx + (endidx - startidx) / 2; BruteForceCmd(b, startidx, mid, reachvar, impl, callback); BruteForceCmd(b, mid + 1, endidx, reachvar, impl, callback); return false; // This is pure random } } else { b.Cmds = backup; return false; } }
static string UniqueName(Variable variable, Procedure proc) { // TODO(wuestholz): Maybe we should define structural equality for variables instead. var scope = "#global_scope#"; if (proc != null && !(variable is GlobalVariable || variable is Constant)) { scope = proc.Name; } return string.Format("{0}.{1}", scope, variable.Name); }
bool ReCheckImpl(Variable reachvar, Implementation impl, VerifierCallback callback, out ProverInterface.Outcome outcome) { Contract.Requires(reachvar != null); Contract.Requires(impl != null); Contract.Requires(callback != null); Checker checker = FindCheckerFor(impl, 1000); Contract.Assert(checker != null); DoomCheck dc = new DoomCheck(impl, this.exitBlock, checker, m_UncheckableBlocks); dc.ErrorHandler = new DoomErrorHandler(dc.Label2Absy, callback); outcome = ProverInterface.Outcome.Undetermined; List<Variable> rv = new List<Variable>(); rv.Add(reachvar); if (!dc.CheckLabel(rv,null, out outcome)) { checker.Close(); return false; } checker.Close(); return true; }
public void AddVariable(Variable var, bool global) { Contract.Requires(var != null); var previous = FindVariable(cce.NonNull(var.Name), !global); if (previous == null) { varContext.VarSymbols.Add(var.Name, var); } else { var r = (Variable)SelectNonExtern(var, previous); if (r == null) { Error(var, "more than one declaration of variable name: {0}", var.Name); } else { varContext.VarSymbols[var.Name] = r; } } }
/// <summary> /// Use this constructor for translating expressions that do occur within /// the context of the statements in a method body. /// </summary> public ExpressionTraverser(Sink sink, StatementTraverser/*?*/ statementTraverser) { this.sink = sink; HeapVariable = sink.HeapVariable; this.StmtTraverser = statementTraverser; TranslatedExpressions = new Stack<Bpl.Expr>(); }
bool BruteForceCESearch(Variable reachvar, Implementation impl, VerifierCallback callback, Dictionary<Block, List<Cmd>> cmdbackup, int startidx, int endidx) { Contract.Requires(reachvar != null); Contract.Requires(impl != null); Contract.Requires(callback != null); Contract.Requires(cce.NonNullElements(cmdbackup)); #region Modify implementation for (int i = startidx; i <= endidx; i++) { if (_copiedBlock.Contains(impl.Blocks[i])) continue; List<Cmd> cs = new List<Cmd>(); cmdbackup.Add(impl.Blocks[i], impl.Blocks[i].Cmds); foreach (Cmd c in impl.Blocks[i].Cmds) { Contract.Assert(c != null); if (ContainsReachVariable(c)) { cs.Add(c); continue; } AssertCmd ac = c as AssertCmd; AssumeCmd uc = c as AssumeCmd; if (ac != null) { cs.Add(new AssertCmd(ac.tok, Expr.True)); } else if (uc != null) { cs.Add(new AssertCmd(uc.tok, Expr.True)); } else { cs.Add(c); } } impl.Blocks[i].Cmds = cs; } #endregion ProverInterface.Outcome outcome = ProverInterface.Outcome.Undetermined; if (!ReCheckImpl(reachvar, impl, callback, out outcome)) { UndoBlockModifications(impl, cmdbackup, startidx, endidx); return false; } if (outcome == ProverInterface.Outcome.Valid) { return true; } else if (outcome == ProverInterface.Outcome.Invalid) { UndoBlockModifications(impl, cmdbackup, startidx, endidx); int mid = startidx + (endidx - startidx) / 2; if (startidx >= endidx) { // Now we found an interesting Block and we have to // search for the interesting statements. int cmdcount = impl.Blocks[endidx].Cmds.Length; BruteForceCmd(impl.Blocks[endidx], 0, cmdcount / 2 - 1, reachvar, impl, callback); BruteForceCmd(impl.Blocks[endidx], cmdcount / 2, cmdcount - 1, reachvar, impl, callback); return true; } else { BruteForceCESearch(reachvar, impl, callback, cmdbackup, startidx, mid); BruteForceCESearch(reachvar, impl, callback, cmdbackup, mid + 1, endidx); return true; } } else { UndoBlockModifications(impl, cmdbackup, startidx, endidx); return false; } }
public override string getVarUName(Variable v) { Debug.Assert(v.Name == v.TypedIdent.Name); return(program.getVarUName(v)); }