about summary refs log tree commit diff homepage
path: root/lib/Expr/ExprSMTLIBLetPrinter.cpp
blob: d4243452d9447a17f4c32c960969d2fbce7ab26e (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//===-- ExprSMTLIBLetPrinter.cpp ------------------------------------------*-
//C++ -*-===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/CommandLine.h"
#include "klee/util/ExprSMTLIBLetPrinter.h"

namespace ExprSMTLIBOptions {
llvm::cl::opt<bool>
useLetExpressions("smtlib-use-let-expressions",
                  llvm::cl::desc("Enables generated SMT-LIBv2 files to use "
                                 "(let) expressions (default=on)"),
                  llvm::cl::init(true));
}

namespace klee {
const char ExprSMTLIBLetPrinter::BINDING_PREFIX[] = "?B";

ExprSMTLIBLetPrinter::ExprSMTLIBLetPrinter()
    : bindings(), firstEO(), twoOrMoreEO(), disablePrintedAbbreviations(false) {
}

void ExprSMTLIBLetPrinter::generateOutput() {
  if (p == NULL || query == NULL || o == NULL) {
    llvm::errs() << "Can't print SMTLIBv2. Output or query bad!\n";
    return;
  }

  generateBindings();

  if (isHumanReadable())
    printNotice();
  printOptions();
  printSetLogic();
  printArrayDeclarations();
  printLetExpression();
  printAction();
  printExit();
}

void ExprSMTLIBLetPrinter::scan(const ref<Expr> &e) {
  if (isa<ConstantExpr>(e))
    return; // we don't need to scan simple constants

  if (firstEO.insert(e).second) {
    // We've not seen this expression before

    if (const ReadExpr *re = dyn_cast<ReadExpr>(e)) {

      // Attempt to insert array and if array wasn't present before do more
      // things
      if (usedArrays.insert(re->updates.root).second) {

        // check if the array is constant
        if (re->updates.root->isConstantArray())
          haveConstantArray = true;

        // scan the update list
        scanUpdates(re->updates.head);
      }
    }

    // recurse into the children
    Expr *ep = e.get();
    for (unsigned int i = 0; i < ep->getNumKids(); i++)
      scan(ep->getKid(i));
  } else {
    /* We must of seen the expression before. Add it to
     * the set of twoOrMoreOccurances. We don't need to
     * check if the insertion fails.
     */
    twoOrMoreEO.insert(e);
  }
}

void ExprSMTLIBLetPrinter::generateBindings() {
  // Assign a number to each binding that will be used
  unsigned int counter = 0;
  for (std::set<ref<Expr> >::const_iterator i = twoOrMoreEO.begin();
       i != twoOrMoreEO.end(); ++i) {
    bindings.insert(std::make_pair(*i, counter));
    ++counter;
  }
}

void ExprSMTLIBLetPrinter::printExpression(
    const ref<Expr> &e, ExprSMTLIBPrinter::SMTLIB_SORT expectedSort) {
  std::map<const ref<Expr>, unsigned int>::const_iterator i = bindings.find(e);

  if (disablePrintedAbbreviations || i == bindings.end()) {
    /*There is no abbreviation for this expression so print it normally.
     * Do this by using the parent method.
     */
    ExprSMTLIBPrinter::printExpression(e, expectedSort);
  } else {
    // Use binding name e.g. "?B1"

    /* Handle the corner case where the expectedSort
     * doesn't match the sort of the abbreviation. Not really very efficient
     * (calls bindings.find() twice),
     * we'll cast and call ourself again but with the correct expectedSort.
     */
    if (getSort(e) != expectedSort) {
      printCastToSort(e, expectedSort);
      return;
    }

    // No casting is needed in this depth of recursion, just print the
    // abbreviation
    *p << BINDING_PREFIX << i->second;
  }
}

void ExprSMTLIBLetPrinter::reset() {
  // Let parent clean up first
  ExprSMTLIBPrinter::reset();

  firstEO.clear();
  twoOrMoreEO.clear();
  bindings.clear();
}

void ExprSMTLIBLetPrinter::printLetExpression() {
  *p << "(assert";
  p->pushIndent();
  printSeperator();

  if (bindings.size() != 0) {
    // Only print let expression if we have bindings to use.
    *p << "(let";
    p->pushIndent();
    printSeperator();
    *p << "( ";
    p->pushIndent();

    // Print each binding
    for (std::map<const ref<Expr>, unsigned int>::const_iterator i =
             bindings.begin();
         i != bindings.end(); ++i) {
      printSeperator();
      *p << "(" << BINDING_PREFIX << i->second << " ";
      p->pushIndent();

      // Disable abbreviations so none are used here.
      disablePrintedAbbreviations = true;

      // We can abbreviate SORT_BOOL or SORT_BITVECTOR in let expressions
      printExpression(i->first, getSort(i->first));

      p->popIndent();
      printSeperator();
      *p << ")";
    }

    p->popIndent();
    printSeperator();
    *p << ")";
    printSeperator();

    // Re-enable printing abbreviations.
    disablePrintedAbbreviations = false;
  }

  // print out Expressions with abbreviations.
  unsigned int numberOfItems = query->constraints.size() + 1; //+1 for query
  unsigned int itemsLeft = numberOfItems;
  std::vector<ref<Expr> >::const_iterator constraint =
      query->constraints.begin();

  /* Produce nested (and () () statements. If the constraint set
   * is empty then we will only print the "queryAssert".
   */
  for (; itemsLeft != 0; --itemsLeft) {
    if (itemsLeft >= 2) {
      *p << "(and";
      p->pushIndent();
      printSeperator();
      printExpression(*constraint,
                      SORT_BOOL); // We must and together bool expressions
      printSeperator();
      ++constraint;
      continue;
    } else {
      // must have 1 item left (i.e. the "queryAssert")
      if (isHumanReadable()) {
        *p << "; query";
        p->breakLineI();
      }
      printExpression(queryAssert,
                      SORT_BOOL); // The query must be a bool expression
    }
  }

  /* Produce closing brackets for nested "and statements".
   * Number of "nested ands" = numberOfItems -1
   */
  itemsLeft = numberOfItems - 1;
  for (; itemsLeft != 0; --itemsLeft) {
    p->popIndent();
    printSeperator();
    *p << ")";
  }

  if (bindings.size() != 0) {
    // end let expression
    p->popIndent();
    printSeperator();
    *p << ")";
    printSeperator();
  }

  // end assert
  p->popIndent();
  printSeperator();
  *p << ")";
  p->breakLineI();
}

ExprSMTLIBPrinter *createSMTLIBPrinter() {
  if (ExprSMTLIBOptions::useLetExpressions)
    return new ExprSMTLIBLetPrinter();
  else
    return new ExprSMTLIBPrinter();
}
}