summaryrefslogtreecommitdiff
path: root/Source/Provers/SMTLib/Inspector.cs
blob: 13a2076ef56943aee04deb9e3f87654952721c6d (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
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
//using util;
using Microsoft.Boogie;
using Microsoft.Basetypes;
using Microsoft.Boogie.VCExprAST;

namespace Microsoft.Boogie.SMTLib
{
  internal class FindLabelsVisitor : TraversingVCExprVisitor<bool, bool> {
    public HashSet<string/*!*/>/*!*/ Labels = new HashSet<string/*!*/>();
    [ContractInvariantMethod]
    void ObjectInvariant() {
      Contract.Invariant(cce.NonNull(Labels));
    }


    public static HashSet<string/*!*/>/*!*/ FindLabels(VCExpr/*!*/ expr) {
      Contract.Requires(expr != null);
      Contract.Ensures(cce.NonNull(Contract.Result<HashSet<string/*!*/>/*!*/>()));

      FindLabelsVisitor visitor = new FindLabelsVisitor();
      visitor.Traverse(expr, true);
      return visitor.Labels;
    }
    
    protected override bool StandardResult(VCExpr node, bool arg) {
      //Contract.Requires(node!=null);
      VCExprNAry nary = node as VCExprNAry;
      if (nary != null) {
        VCExprLabelOp lab = nary.Op as VCExprLabelOp;
        if (lab != null) {
          Labels.Add(lab.label);
        }
      }
      return true;
    }
  }

  internal class Inspector {
    [Rep] protected readonly Process inspector;
    [Rep] readonly TextReader fromInspector;
    [Rep] readonly TextWriter toInspector;
    [ContractInvariantMethod]
    void ObjectInvariant() 
    {
      Contract.Invariant(inspector!=null);
      Contract.Invariant(fromInspector!=null);
      Contract.Invariant(toInspector != null);
    }


    public Inspector(SMTLibProverOptions opts)
    {
      Contract.Requires(opts != null);
      ProcessStartInfo psi = new ProcessStartInfo(opts.Inspector);
      Contract.Assert(psi!=null);
      psi.CreateNoWindow = true;
      psi.UseShellExecute = false;
      psi.RedirectStandardInput = true;
      psi.RedirectStandardOutput = true;
      psi.RedirectStandardError = false;

      try 
      {
        Process inspector = Process.Start(psi);
        this.inspector = inspector;
        fromInspector = inspector.StandardOutput;
        toInspector = inspector.StandardInput;
      } 
      catch (System.ComponentModel.Win32Exception e)
      {
        throw new Exception(string.Format("Unable to start the inspector process {0}: {1}", opts.Inspector, e.Message));
      }
    }

    public void NewProblem(string descriptiveName, VCExpr vc, ProverInterface.ErrorHandler handler)
    {
      Contract.Requires(descriptiveName != null);
      Contract.Requires(vc != null);
      Contract.Requires(handler != null);
      HashSet<string/*!*/>/*!*/ labels = FindLabelsVisitor.FindLabels(vc);
      Contract.Assert(labels!=null);
      toInspector.WriteLine("PROBLEM " + descriptiveName);
      toInspector.WriteLine("TOKEN BEGIN");
      foreach (string lab in labels) {
        Contract.Assert(lab!=null);
        string no = lab.Substring(1);
        Absy absy = handler.Label2Absy(no);
        
        IToken tok = absy.tok;
        AssertCmd assrt = absy as AssertCmd;
        Block blk = absy as Block;
        string val = tok.val; // might require computation, so cache it
        if (val == "foo" || tok.filename == null) continue; // no token

        toInspector.Write("TOKEN ");
        toInspector.Write(lab);
        toInspector.Write(" ");

        if (assrt != null) {
          toInspector.Write("ASSERT");
          string errData = assrt.ErrorData as string;
          if (errData != null) {
            val = errData;
          } else if (assrt.ErrorMessage != null) {
            val = assrt.ErrorMessage;
          }
        } else if (blk != null) {
          toInspector.Write("BLOCK ");
          toInspector.Write(blk.Label);
        } else {
          Contract.Assume( false);
        }
        if (val == null || val == "assert" || val == "ensures") { val = ""; }

        if (absy is LoopInitAssertCmd) {
          val += " (loop entry)";
        } else if (absy is LoopInvMaintainedAssertCmd) {
          val += " (loop body)";
        } else if (val.IndexOf("#VCCERR") >= 0) { 
          // skip further transformations
        } else if (absy is AssertRequiresCmd) {
          AssertRequiresCmd req = (AssertRequiresCmd)absy;
          IToken t2 = req.Requires.tok;
          string tval = t2.val;
          if (tval == "requires")
            tval = string.Format("{0}({1},{2}))", t2.filename, t2.line, t2.col);
          string call = "";
          if (val != "call") call = " in call to " + val;
          val = string.Format("precondition {0}{1}", tval, call);
        }

        val = val.Replace("\r", "").Replace("\n", " ");

        toInspector.WriteLine(string.Format(" {0} {1} :@:{2}:@:{3}", tok.line, tok.col, tok.filename, val));
      }
      toInspector.WriteLine("TOKEN END");
    }

    public void StatsLine(string line)
    {
      Contract.Requires(line != null);
      toInspector.WriteLine(line);
      toInspector.Flush();
    }
  }
}