summaryrefslogtreecommitdiff
path: root/Source/Core/VCExp.ssc
blob: 4b42a639eca6edba98054f8f3b1eec29978bf59a (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
//-----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation.  All Rights Reserved.
//
//-----------------------------------------------------------------------------
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Microsoft.Contracts;
using Microsoft.Basetypes;


namespace Microsoft.Boogie {

  public class ProverOptions
  {
    public class OptionException : Exception {
      public OptionException(string! msg) { base(msg); }
    }

    public string/*?*/ LogFilename = null;
    public bool AppendLogFile = false;
    public bool SeparateLogFiles = false;
    // Say (DBG_WAS_VALID) or (DBG_WAS_INVALID) after query
    public bool ForceLogStatus = false;
    public int TimeLimit = 0;
    public int MemoryLimit = 0;
    public CommandLineOptions.BvHandling BitVectors = CommandLineOptions.BvHandling.None;
    public int Verbosity = 0;

    private string! stringRepr = "";

    [Pure]
    public override string! ToString()
    {
      return stringRepr;
    }

    // The usual thing to override.
    protected virtual bool Parse(string! opt)
    {
      return ParseString(opt, "LOG_FILE", ref LogFilename) ||
             ParseBool(opt, "APPEND_LOG_FILE", ref AppendLogFile) ||
             ParseBool(opt, "FORCE_LOG_STATUS", ref ForceLogStatus) ||
             ParseInt(opt, "MEMORY_LIMIT", ref MemoryLimit) ||
             ParseInt(opt, "VERBOSITY", ref Verbosity) ||
             ParseInt(opt, "TIME_LIMIT", ref TimeLimit);
             // || base.Parse(opt)
    }

    public virtual void Parse(List<string!>! opts)
    {
      StringBuilder! sb = new StringBuilder(stringRepr);
      foreach (string! opt in opts) {
        if (!Parse(opt)) {
          ReportError("Unrecognised prover option: " + opt);
        }
        sb.Append(opt).Append(" ");
      }
      stringRepr = sb.ToString();
      PostParse();
    }

    protected virtual void PostParse()
    {
      if (LogFilename != null && LogFilename.Contains("@PROC@")) {
        SeparateLogFiles = true;
      }
    }

    protected void ReportError(string! msg)
    {
      throw new OptionException(msg);
    }

    protected virtual bool ParseString(string! opt, string! name, ref string field)
    {
      if (opt.Length >= name.Length && opt.StartsWith(name)) {
        if (opt.Length == name.Length) {
          field = "";
          return true;
        } else if (opt[name.Length] == '=') {
          field = opt.Substring(name.Length + 1);
          return true;
        }
      }
      return false;
    }

    protected virtual bool ParseBool(string! opt, string! name, ref bool field)
    {
      string tmp = null;
      if (ParseString(opt, name, ref tmp))
        switch (((!)tmp).ToLower()) {
        case "1":
        case "true":
        case "":
          field = true;
          return true;
        case "0":
        case "false":
          field = false;
          return true;
        default:
          ReportError("Invalid Boolean option \"" + opt + "\"");
          return false;
        }
      return false;
    }

    protected virtual bool ParseInt(string! opt, string! name, ref int field)
    {
      string tmp = null;
      int t2;
      if (ParseString(opt, name, ref tmp)) {
        if (int.TryParse((!)tmp, out t2)) {
          field = t2;
          return true;
        } else {
          ReportError("Invalid integer option \"" + opt + "\"");
        }
      }
      return false;
    }

    static int sequenceNumber = 0;
    public virtual TextWriter? OpenLog(string/*?*/ descName)
    {
      if (LogFilename != null) {
        string! filename = LogFilename;
        if (descName != null)
          filename = Helpers.SubstituteAtPROC(descName, filename);
        return new StreamWriter(filename, AppendLogFile);
      } else {
        return null;
      }
    }
  }

  public abstract class ProverFactory
  {
    // Really returns ProverInterface.
    public abstract object! SpawnProver(ProverOptions! options, object! ctxt);

    // Really returns ProverContext
    public abstract object! NewProverContext(ProverOptions! options);

    public virtual ProverOptions! BlankProverOptions()
    {
      return new ProverOptions();
    }

    // return true if the prover supports DAG AST as opposed to LET AST
    public virtual bool SupportsDags
    {
      get { return false; }
    }

    public virtual CommandLineOptions.VCVariety DefaultVCVariety
    {
      get 
        ensures result != CommandLineOptions.VCVariety.Unspecified;
        { return CommandLineOptions.VCVariety.Dag; }
    }

    public virtual void Close()
    {
    }

    public static ProverFactory! Load(string! proverName)
      ensures result.IsNew && Microsoft.Contracts.Owner.New(result);
    {
      string! path;
      if (proverName.IndexOf("/") > 0 || proverName.IndexOf("\\") > 0) {
        path = proverName;
      } else {
        string! codebase = (!) System.IO.Path.GetDirectoryName(
                                 (!)System.Reflection.Assembly.GetExecutingAssembly().Location);
        path = System.IO.Path.Combine(codebase, "Provers." + proverName + ".dll");
      }
      Assembly asm = (!)Assembly.LoadFrom(path);
      string name = (!)asm.GetName().Name;
      System.Type factoryType = (!)asm.GetType("Microsoft.Boogie." + name.Replace("Provers.", "") + ".Factory");
      return (ProverFactory!)Activator.CreateInstance(factoryType);
    }
  }
}