Some reversions of recent commits re: portfolio failure.
[cvc5.git] / src / main / driver_unified.cpp
1 /********************* */
2 /*! \file driver_unified.cpp
3 ** \verbatim
4 ** Original author: Kshitij Bansal
5 ** Major contributors: Morgan Deters
6 ** Minor contributors (to current version): Francois Bobot
7 ** This file is part of the CVC4 project.
8 ** Copyright (c) 2009-2013 New York University and The University of Iowa
9 ** See the file COPYING in the top-level source directory for licensing
10 ** information.\endverbatim
11 **
12 ** \brief Driver for CVC4 executable (cvc4) unified for both
13 ** sequential and portfolio versions
14 **/
15
16 #include <cstdlib>
17 #include <cstring>
18 #include <fstream>
19 #include <iostream>
20 #include <new>
21 #include <unistd.h>
22
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #include "cvc4autoconfig.h"
27 #include "main/main.h"
28 #include "main/interactive_shell.h"
29 #include "main/options.h"
30 #include "parser/parser.h"
31 #include "parser/parser_builder.h"
32 #include "parser/parser_exception.h"
33 #include "expr/expr_manager.h"
34 #include "expr/command.h"
35 #include "util/configuration.h"
36 #include "options/options.h"
37 #include "main/command_executor.h"
38 # ifdef PORTFOLIO_BUILD
39 # include "main/command_executor_portfolio.h"
40 # endif
41 #include "main/options.h"
42 #include "smt/options.h"
43 #include "theory/uf/options.h"
44 #include "util/output.h"
45 #include "util/result.h"
46 #include "util/statistics_registry.h"
47
48 using namespace std;
49 using namespace CVC4;
50 using namespace CVC4::parser;
51 using namespace CVC4::main;
52
53 namespace CVC4 {
54 namespace main {
55 /** Global options variable */
56 CVC4_THREADLOCAL(Options*) pOptions;
57
58 /** Full argv[0] */
59 const char *progPath;
60
61 /** Just the basename component of argv[0] */
62 const char *progName;
63
64 /** A pointer to the CommandExecutor (the signal handlers need it) */
65 CVC4::main::CommandExecutor* pExecutor = NULL;
66
67 /** A pointer to the totalTime driver stat (the signal handlers need it) */
68 CVC4::TimerStat* pTotalTime = NULL;
69
70 }/* CVC4::main namespace */
71 }/* CVC4 namespace */
72
73
74 void printUsage(Options& opts, bool full) {
75 stringstream ss;
76 ss << "usage: " << opts[options::binary_name] << " [options] [input-file]" << endl
77 << endl
78 << "Without an input file, or with `-', CVC4 reads from standard input." << endl
79 << endl
80 << "CVC4 options:" << endl;
81 if(full) {
82 Options::printUsage( ss.str(), *opts[options::out] );
83 } else {
84 Options::printShortUsage( ss.str(), *opts[options::out] );
85 }
86 }
87
88 void printStatsFilterZeros(std::ostream& out, const std::string& statsString) {
89 // read each line, if a number, check zero and skip if so
90 // Stat are assumed to one-per line: "<statName>, <statValue>"
91
92 std::istringstream iss(statsString);
93 std::string statName, statValue;
94
95 std::getline(iss, statName, ',');
96
97 while( !iss.eof() ) {
98
99 std::getline(iss, statValue, '\n');
100
101 double curFloat;
102 bool isFloat = (std::istringstream(statValue) >> curFloat);
103
104 if( (isFloat && curFloat == 0) ||
105 statValue == " \"0\"" ||
106 statValue == " \"[]\"") {
107 // skip
108 } else {
109 out << statName << "," << statValue << std::endl;
110 }
111
112 std::getline(iss, statName, ',');
113 }
114
115 }
116
117 int runCvc4(int argc, char* argv[], Options& opts) {
118
119 // Timer statistic
120 pTotalTime = new TimerStat("totalTime");
121 pTotalTime->start();
122
123 // For the signal handlers' benefit
124 pOptions = &opts;
125
126 // Initialize the signal handlers
127 cvc4_init();
128
129 progPath = argv[0];
130
131 // Parse the options
132 vector<string> filenames = opts.parseOptions(argc, argv);
133
134 # ifndef PORTFOLIO_BUILD
135 if( opts.wasSetByUser(options::threads) ||
136 ! opts[options::threadArgv].empty() ) {
137 throw OptionException("Thread options cannot be used with sequential CVC4. Please build and use the portfolio binary `pcvc4'.");
138 }
139 # else
140 if( opts[options::checkProofs] ) {
141 throw OptionException("Cannot run portfolio in check-proofs mode.");
142 }
143 # endif
144
145 progName = opts[options::binary_name].c_str();
146
147 if( opts[options::help] ) {
148 printUsage(opts, true);
149 exit(1);
150 } else if( opts[options::languageHelp] ) {
151 Options::printLanguageHelp(*opts[options::out]);
152 exit(1);
153 } else if( opts[options::version] ) {
154 *opts[options::out] << Configuration::about().c_str() << flush;
155 exit(0);
156 }
157
158 segvSpin = opts[options::segvSpin];
159
160 // If in competition mode, set output stream option to flush immediately
161 #ifdef CVC4_COMPETITION_MODE
162 *opts[options::out] << unitbuf;
163 #endif /* CVC4_COMPETITION_MODE */
164
165 // We only accept one input file
166 if(filenames.size() > 1) {
167 throw Exception("Too many input files specified.");
168 }
169
170 // If no file supplied we will read from standard input
171 const bool inputFromStdin = filenames.empty() || filenames[0] == "-";
172
173 // if we're reading from stdin on a TTY, default to interactive mode
174 if(!opts.wasSetByUser(options::interactive)) {
175 opts.set(options::interactive, inputFromStdin && isatty(fileno(stdin)));
176 }
177
178 // Auto-detect input language by filename extension
179 const char* filename = inputFromStdin ? "<stdin>" : filenames[0].c_str();
180
181 if(opts[options::inputLanguage] == language::input::LANG_AUTO) {
182 if( inputFromStdin ) {
183 // We can't do any fancy detection on stdin
184 opts.set(options::inputLanguage, language::input::LANG_CVC4);
185 } else {
186 unsigned len = strlen(filename);
187 if(len >= 5 && !strcmp(".smt2", filename + len - 5)) {
188 opts.set(options::inputLanguage, language::input::LANG_SMTLIB_V2);
189 } else if(len >= 4 && !strcmp(".smt", filename + len - 4)) {
190 opts.set(options::inputLanguage, language::input::LANG_SMTLIB_V1);
191 } else if(len >= 5 && !strcmp(".smt1", filename + len - 5)) {
192 opts.set(options::inputLanguage, language::input::LANG_SMTLIB_V1);
193 } else if((len >= 2 && !strcmp(".p", filename + len - 2))
194 || (len >= 5 && !strcmp(".tptp", filename + len - 5))) {
195 opts.set(options::inputLanguage, language::input::LANG_TPTP);
196 } else if(( len >= 4 && !strcmp(".cvc", filename + len - 4) )
197 || ( len >= 5 && !strcmp(".cvc4", filename + len - 5) )) {
198 opts.set(options::inputLanguage, language::input::LANG_CVC4);
199 }
200 }
201 }
202
203 if(opts[options::outputLanguage] == language::output::LANG_AUTO) {
204 opts.set(options::outputLanguage, language::toOutputLanguage(opts[options::inputLanguage]));
205 }
206
207 // Determine which messages to show based on smtcomp_mode and verbosity
208 if(Configuration::isMuzzledBuild()) {
209 DebugChannel.setStream(CVC4::null_os);
210 TraceChannel.setStream(CVC4::null_os);
211 NoticeChannel.setStream(CVC4::null_os);
212 ChatChannel.setStream(CVC4::null_os);
213 MessageChannel.setStream(CVC4::null_os);
214 WarningChannel.setStream(CVC4::null_os);
215 }
216
217 // important even for muzzled builds (to get result output right)
218 *opts[options::out] << Expr::setlanguage(opts[options::outputLanguage]);
219
220 // Create the expression manager using appropriate options
221 ExprManager* exprMgr;
222 # ifndef PORTFOLIO_BUILD
223 exprMgr = new ExprManager(opts);
224 pExecutor = new CommandExecutor(*exprMgr, opts);
225 # else
226 vector<Options> threadOpts = parseThreadSpecificOptions(opts);
227 if(opts.wasSetByUser(options::incrementalSolving) &&
228 opts[options::incrementalSolving] &&
229 !opts[options::incrementalParallel]) {
230 Notice() << "Notice: In --incremental mode, using the sequential solver unless forced by...\n"
231 << "Notice: ...the experimental --incremental-parallel option.\n";
232 exprMgr = new ExprManager(opts);
233 pExecutor = new CommandExecutor(*exprMgr, opts);
234 } else {
235 exprMgr = new ExprManager(threadOpts[0]);
236 pExecutor = new CommandExecutorPortfolio(*exprMgr, opts, threadOpts);
237 }
238 # endif
239
240 Parser* replayParser = NULL;
241 if( opts[options::replayFilename] != "" ) {
242 ParserBuilder replayParserBuilder(exprMgr, opts[options::replayFilename], opts);
243
244 if( opts[options::replayFilename] == "-") {
245 if( inputFromStdin ) {
246 throw OptionException("Replay file and input file can't both be stdin.");
247 }
248 replayParserBuilder.withStreamInput(cin);
249 }
250 replayParser = replayParserBuilder.build();
251 opts.set(options::replayStream, new Parser::ExprStream(replayParser));
252 }
253 if( opts[options::replayLog] != NULL ) {
254 *opts[options::replayLog] << Expr::setlanguage(opts[options::outputLanguage]) << Expr::setdepth(-1);
255 }
256
257 int returnValue = 0;
258 {
259 // Timer statistic
260 RegisterStatistic statTotalTime(&pExecutor->getStatisticsRegistry(), pTotalTime);
261
262 // Filename statistics
263 ReferenceStat< const char* > s_statFilename("filename", filename);
264 RegisterStatistic statFilenameReg(&pExecutor->getStatisticsRegistry(), &s_statFilename);
265
266 // Parse and execute commands until we are done
267 Command* cmd;
268 bool status = true;
269 if(opts[options::interactive] && inputFromStdin) {
270 if(opts[options::tearDownIncremental] && opts[options::incrementalSolving]) {
271 if(opts.wasSetByUser(options::incrementalSolving)) {
272 throw OptionException("--tear-down-incremental incompatible with --incremental");
273 }
274
275 cmd = new SetOptionCommand("incremental", false);
276 cmd->setMuted(true);
277 pExecutor->doCommand(cmd);
278 delete cmd;
279 }
280 #ifndef PORTFOLIO_BUILD
281 if(!opts.wasSetByUser(options::incrementalSolving)) {
282 cmd = new SetOptionCommand("incremental", true);
283 cmd->setMuted(true);
284 pExecutor->doCommand(cmd);
285 delete cmd;
286 }
287 #endif /* PORTFOLIO_BUILD */
288 InteractiveShell shell(*exprMgr, opts);
289 if(opts[options::interactivePrompt]) {
290 Message() << Configuration::getPackageName()
291 << " " << Configuration::getVersionString();
292 if(Configuration::isGitBuild()) {
293 Message() << " [" << Configuration::getGitId() << "]";
294 } else if(Configuration::isSubversionBuild()) {
295 Message() << " [" << Configuration::getSubversionId() << "]";
296 }
297 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
298 << " assertions:"
299 << (Configuration::isAssertionBuild() ? "on" : "off")
300 << endl;
301 }
302 if(replayParser != NULL) {
303 // have the replay parser use the declarations input interactively
304 replayParser->useDeclarationsFrom(shell.getParser());
305 }
306 while((cmd = shell.readCommand())) {
307 status = pExecutor->doCommand(cmd) && status;
308 delete cmd;
309 }
310 } else if(opts[options::tearDownIncremental]) {
311 if(opts[options::incrementalSolving]) {
312 if(opts.wasSetByUser(options::incrementalSolving)) {
313 throw OptionException("--tear-down-incremental incompatible with --incremental");
314 }
315
316 cmd = new SetOptionCommand("incremental", false);
317 cmd->setMuted(true);
318 pExecutor->doCommand(cmd);
319 delete cmd;
320 }
321
322 ParserBuilder parserBuilder(exprMgr, filename, opts);
323
324 if( inputFromStdin ) {
325 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
326 parserBuilder.withStreamInput(cin);
327 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
328 parserBuilder.withLineBufferedStreamInput(cin);
329 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
330 }
331
332 vector< vector<Command*> > allCommands;
333 allCommands.push_back(vector<Command*>());
334 Parser *parser = parserBuilder.build();
335 if(replayParser != NULL) {
336 // have the replay parser use the file's declarations
337 replayParser->useDeclarationsFrom(parser);
338 }
339 bool needReset = false;
340 while(status && (cmd = parser->nextCommand())) {
341 if(dynamic_cast<PushCommand*>(cmd) != NULL) {
342 if(needReset) {
343 pExecutor->reset();
344 for(size_t i = 0; i < allCommands.size(); ++i) {
345 for(size_t j = 0; j < allCommands[i].size(); ++j) {
346 Command* cmd = allCommands[i][j]->clone();
347 cmd->setMuted(true);
348 pExecutor->doCommand(cmd);
349 delete cmd;
350 }
351 }
352 needReset = false;
353 }
354 *opts[options::out] << CommandSuccess();
355 allCommands.push_back(vector<Command*>());
356 } else if(dynamic_cast<PopCommand*>(cmd) != NULL) {
357 allCommands.pop_back(); // fixme leaks cmds here
358 pExecutor->reset();
359 for(size_t i = 0; i < allCommands.size(); ++i) {
360 for(size_t j = 0; j < allCommands[i].size(); ++j) {
361 Command* cmd = allCommands[i][j]->clone();
362 cmd->setMuted(true);
363 pExecutor->doCommand(cmd);
364 delete cmd;
365 }
366 }
367 *opts[options::out] << CommandSuccess();
368 } else if(dynamic_cast<CheckSatCommand*>(cmd) != NULL ||
369 dynamic_cast<QueryCommand*>(cmd) != NULL) {
370 if(needReset) {
371 pExecutor->reset();
372 for(size_t i = 0; i < allCommands.size(); ++i) {
373 for(size_t j = 0; j < allCommands[i].size(); ++j) {
374 Command* cmd = allCommands[i][j]->clone();
375 cmd->setMuted(true);
376 pExecutor->doCommand(cmd);
377 delete cmd;
378 }
379 }
380 }
381 status = pExecutor->doCommand(cmd);
382 needReset = true;
383 } else {
384 Command* copy = cmd->clone();
385 allCommands.back().push_back(copy);
386 if(dynamic_cast<QuitCommand*>(cmd) != NULL) {
387 delete cmd;
388 break;
389 }
390 status = pExecutor->doCommand(cmd);
391 }
392 delete cmd;
393 }
394 // Remove the parser
395 delete parser;
396 } else {
397 if(!opts.wasSetByUser(options::incrementalSolving)) {
398 cmd = new SetOptionCommand("incremental", false);
399 cmd->setMuted(true);
400 pExecutor->doCommand(cmd);
401 delete cmd;
402 }
403
404 ParserBuilder parserBuilder(exprMgr, filename, opts);
405
406 if( inputFromStdin ) {
407 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
408 parserBuilder.withStreamInput(cin);
409 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
410 parserBuilder.withLineBufferedStreamInput(cin);
411 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
412 }
413
414 Parser *parser = parserBuilder.build();
415 if(replayParser != NULL) {
416 // have the replay parser use the file's declarations
417 replayParser->useDeclarationsFrom(parser);
418 }
419 while(status && (cmd = parser->nextCommand())) {
420 if(dynamic_cast<QuitCommand*>(cmd) != NULL) {
421 delete cmd;
422 break;
423 }
424 status = pExecutor->doCommand(cmd);
425 delete cmd;
426 }
427 // Remove the parser
428 delete parser;
429 }
430
431 if( opts[options::replayStream] != NULL ) {
432 // this deletes the expression parser too
433 delete opts[options::replayStream];
434 opts.set(options::replayStream, NULL);
435 }
436
437 Result result;
438 if(status) {
439 result = pExecutor->getResult();
440 returnValue = 0;
441 } else {
442 // there was some kind of error
443 returnValue = 1;
444 }
445
446 #ifdef CVC4_COMPETITION_MODE
447 // exit, don't return (don't want destructors to run)
448 // _exit() from unistd.h doesn't run global destructors
449 // or other on_exit/atexit stuff.
450 _exit(returnValue);
451 #endif /* CVC4_COMPETITION_MODE */
452
453 ReferenceStat< Result > s_statSatResult("sat/unsat", result);
454 RegisterStatistic statSatResultReg(&pExecutor->getStatisticsRegistry(), &s_statSatResult);
455
456 pTotalTime->stop();
457
458 // Set the global executor pointer to NULL first. If we get a
459 // signal while dumping statistics, we don't want to try again.
460 if(opts[options::statistics]) {
461 if(opts[options::statsHideZeros] == false) {
462 pExecutor->flushStatistics(*opts[options::err]);
463 } else {
464 std::ostringstream ossStats;
465 pExecutor->flushStatistics(ossStats);
466 printStatsFilterZeros(*opts[options::err], ossStats.str());
467 }
468 }
469
470 // make sure to flush replay output log before early-exit
471 if( opts[options::replayLog] != NULL ) {
472 *opts[options::replayLog] << flush;
473 }
474
475 // make sure out and err streams are flushed too
476 *opts[options::out] << flush;
477 *opts[options::err] << flush;
478
479 #ifdef CVC4_DEBUG
480 if(opts[options::earlyExit] && opts.wasSetByUser(options::earlyExit)) {
481 _exit(returnValue);
482 }
483 #else /* CVC4_DEBUG */
484 if(opts[options::earlyExit]) {
485 _exit(returnValue);
486 }
487 #endif /* CVC4_DEBUG */
488 }
489
490 // On exceptional exit, these are leaked, but that's okay... they
491 // need to be around in that case for main() to print statistics.
492 delete pTotalTime;
493 delete pExecutor;
494 delete exprMgr;
495
496 pTotalTime = NULL;
497 pExecutor = NULL;
498
499 return returnValue;
500 }