summaryrefslogtreecommitdiff
path: root/Source/Provers/Z3/TypeDeclCollector.ssc
blob: 8173f7f3c649c1ef882bf90728ae9d7dc6d356c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Contracts;
using Microsoft.Boogie.VCExprAST;

namespace Microsoft.Boogie.Z3
{
  // Visitor for collecting the occurring function symbols in a VCExpr,
  // and for creating the corresponding declarations that can be fed into
  // Z3

  // (should this be rather done by Context.DeclareFunction? right now,
  // the TypeErasure visitor introduces new function symbols that are
  // not passed to this method)

  public class TypeDeclCollector : BoundVarTraversingVCExprVisitor<bool, bool> {

    private readonly UniqueNamer! Namer;

    private readonly bool NativeBv;

    public TypeDeclCollector(UniqueNamer! namer, bool nativeBv) {
      this.Namer = namer;
      this.NativeBv = nativeBv;
      AllDecls = new List<string!> ();
      IncDecls = new List<string!> ();
      KnownFunctions = new Dictionary<Function!, bool> ();
      KnownVariables = new Dictionary<VCExprVar!, bool> ();
      KnownTypes = new Dictionary<Type!, bool> ();
      KnownBvOps = new Dictionary<string!, bool> ();
    }

    internal TypeDeclCollector(UniqueNamer! namer, TypeDeclCollector! coll) {
      this.Namer = namer;
      this.NativeBv = coll.NativeBv;
      AllDecls = new List<string!> (coll.AllDecls);
      IncDecls = new List<string!> (coll.IncDecls);
      KnownFunctions = new Dictionary<Function!, bool> (coll.KnownFunctions);
      KnownVariables = new Dictionary<VCExprVar!, bool> (coll.KnownVariables);
      KnownTypes = new Dictionary<Type!, bool> (coll.KnownTypes);
      KnownBvOps = new Dictionary<string!, bool> (coll.KnownBvOps);
    }

    // not used
    protected override bool StandardResult(VCExpr! node, bool arg) {
      return true;
    }

    private readonly List<string!>! AllDecls;
    private readonly List<string!>! IncDecls;

    private readonly IDictionary<Function!, bool>! KnownFunctions;
    private readonly IDictionary<VCExprVar!, bool>! KnownVariables;

    // bitvector types have to be registered as well
    private readonly IDictionary<Type!, bool>! KnownTypes;

    // the names of registered BvConcatOps and BvExtractOps
    private readonly IDictionary<string!, bool>! KnownBvOps;

    public List<string!>! AllDeclarations { get {
      List<string!>! res = new List<string!> ();
      res.AddRange(AllDecls);
      return res;
    } }

    public List<string!>! GetNewDeclarations() {
      List<string!>! res = new List<string!> ();
      res.AddRange(IncDecls);
      IncDecls.Clear();
      return res;
    }

    private void AddDeclaration(string! decl) {
      AllDecls.Add(decl);
      IncDecls.Add(decl);
    }

    public void Collect(VCExpr! expr) {
      Traverse(expr, true);
    }

    ///////////////////////////////////////////////////////////////////////////

    private static string! TypeToString(Type! t) {
      return SimplifyLikeExprLineariser.TypeToString(t);
    }

    private void RegisterType(Type! type)
    {
      if (!type.IsBv || !NativeBv) return;

      if (!KnownTypes.ContainsKey(type)) {
        int bits = type.BvBits;
        string name = TypeToString(type);

        AddDeclaration("(DEFTYPE " + name + " :BUILTIN BitVec " + bits + ")");
        // If we add the BUILTIN then the conversion axiom does not work
        AddDeclaration("(DEFOP " + name + "_to_int " + name + " $int)"); // :BUILTIN bv2int $int)");
        AddDeclaration("(DEFOP $make_bv" + bits + " $int " + name + " :BUILTIN int2bv " + bits + ")");
        string! expr = "($make_bv" + bits + " (" + name + "_to_int  x))";
        AddDeclaration("(BG_PUSH (FORALL (x :TYPE " + name + ") (PATS "
                       + expr + ") (QID bvconv" + bits + ") (EQ " + expr + " x)))");

        KnownTypes.Add(type, true);
      }
    }

    public override bool Visit(VCExprNAry! node, bool arg) {
      // there are a couple of cases where operators have to be
      // registered by generating appropriate Z3 statements

      if (node.Op is VCExprBvConcatOp) {
        //
        if (NativeBv) {
          RegisterType(node[0].Type);
          RegisterType(node[1].Type);
          RegisterType(node.Type);

          string name = SimplifyLikeExprLineariser.BvConcatOpName(node);
          if (!KnownBvOps.ContainsKey(name)) {
            AddDeclaration("(DEFOP " + name +
                           " " + TypeToString(node[0].Type) +
                           " " + TypeToString(node[1].Type) +
                           " " + TypeToString(node.Type) +
                           " :BUILTIN concat)");
            KnownBvOps.Add(name, true);
          }
        }
        //
      } else if (node.Op is VCExprBvExtractOp) {
        //
        if (NativeBv) {
          RegisterType(node[0].Type);
          RegisterType(node.Type);

          VCExprBvExtractOp! op = (VCExprBvExtractOp)node.Op;
          string name = SimplifyLikeExprLineariser.BvExtractOpName(node);
          if (!KnownBvOps.ContainsKey(name)) {
            AddDeclaration("(DEFOP " + name +
                           " " + TypeToString(node[0].Type) +
                           " " + TypeToString(node.Type) + 
                           " :BUILTIN extract " +
                           (op.End - 1) + " " + op.Start + ")");
            KnownBvOps.Add(name, true);
          }
        }
        //
      } else {
        //
        VCExprBoogieFunctionOp op = node.Op as VCExprBoogieFunctionOp;
        if (op != null && !KnownFunctions.ContainsKey(op.Func)) {
          Function! f = op.Func;
          string! printedName = Namer.GetName(f, f.Name);
          string! decl = "(DEFOP " + SimplifyLikeExprLineariser.MakeIdPrintable(printedName);

          foreach (Variable! v in f.InParams) {
            decl += " " + TypeToString(v.TypedIdent.Type);
            RegisterType(v.TypedIdent.Type);
          }
          assert f.OutParams.Length == 1;
          foreach (Variable! v in f.OutParams) {
            decl += " " + TypeToString(v.TypedIdent.Type);
            RegisterType(v.TypedIdent.Type);
          }

          string? builtin = ExtractBuiltin(f);
          if (builtin != null)
            decl += " :BUILTIN " + builtin;

          decl += ")";
        
          AddDeclaration(decl);
          KnownFunctions.Add(f, true);
        }
        //
      }

      return base.Visit(node, arg);
    }

    private string? ExtractBuiltin(Function! f) {
      if (NativeBv) {
        return f.FindStringAttribute("bvbuiltin");
      } else {
        return null;
      }
    }

    public override bool Visit(VCExprVar! node, bool arg) {
      if (!BoundTermVars.Contains(node) && !KnownVariables.ContainsKey(node)) {
        RegisterType(node.Type);
        string! printedName = Namer.GetName(node, node.Name);
        string! decl =
          "(DEFOP " + SimplifyLikeExprLineariser.MakeIdPrintable(printedName)
          + " " + TypeToString(node.Type) + ")";
        AddDeclaration(decl);
        KnownVariables.Add(node, true);
      }

      return base.Visit(node, arg);
    }
  }

}