1 /********************* */
2 /*! \file driver_unified.cpp
4 ** Top contributors (to current version):
5 ** Morgan Deters, Tim King, Liana Hadarean
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2016 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief Driver for CVC4 executable (cvc4) unified for both
13 ** sequential and portfolio versions
25 // This must come before PORTFOLIO_BUILD.
26 #include "cvc4autoconfig.h"
28 #include "base/configuration.h"
29 #include "base/output.h"
30 #include "base/ptr_closer.h"
31 #include "expr/expr_iomanip.h"
32 #include "expr/expr_manager.h"
33 #include "main/command_executor.h"
35 #ifdef PORTFOLIO_BUILD
36 # include "main/command_executor_portfolio.h"
39 #include "main/interactive_shell.h"
40 #include "main/main.h"
41 #include "options/options.h"
42 #include "options/set_language.h"
43 #include "parser/parser.h"
44 #include "parser/parser_builder.h"
45 #include "parser/parser_exception.h"
46 #include "smt/command.h"
47 #include "util/result.h"
48 #include "util/statistics_registry.h"
52 using namespace CVC4::parser
;
53 using namespace CVC4::main
;
57 /** Global options variable */
58 CVC4_THREADLOCAL(Options
*) pOptions
;
63 /** Just the basename component of argv[0] */
66 /** A pointer to the CommandExecutor (the signal handlers need it) */
67 CVC4::main::CommandExecutor
* pExecutor
= NULL
;
69 /** A pointer to the totalTime driver stat (the signal handlers need it) */
70 CVC4::TimerStat
* pTotalTime
= NULL
;
72 }/* CVC4::main namespace */
76 void printUsage(Options
& opts
, bool full
) {
78 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
80 << "Without an input file, or with `-', CVC4 reads from standard input."
82 << "CVC4 options:" << endl
;
84 Options::printUsage( ss
.str(), *(opts
.getOut()) );
86 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
90 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
93 pTotalTime
= new TimerStat("totalTime");
96 // For the signal handlers' benefit
99 // Initialize the signal handlers
105 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
107 # ifndef PORTFOLIO_BUILD
108 if( opts
.wasSetByUserThreads() ||
109 opts
.wasSetByUserThreadStackSize() ||
110 (! opts
.getThreadArgv().empty()) ) {
111 throw OptionException("Thread options cannot be used with sequential CVC4. Please build and use the portfolio binary `pcvc4'.");
115 progName
= opts
.getBinaryName().c_str();
117 if( opts
.getHelp() ) {
118 printUsage(opts
, true);
120 } else if( opts
.getLanguageHelp() ) {
121 Options::printLanguageHelp(*(opts
.getOut()));
123 } else if( opts
.getVersion() ) {
124 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
128 segvSpin
= opts
.getSegvSpin();
130 // If in competition mode, set output stream option to flush immediately
131 #ifdef CVC4_COMPETITION_MODE
132 *(opts
.getOut()) << unitbuf
;
133 #endif /* CVC4_COMPETITION_MODE */
135 // We only accept one input file
136 if(filenames
.size() > 1) {
137 throw Exception("Too many input files specified.");
140 // If no file supplied we will read from standard input
141 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
143 // if we're reading from stdin on a TTY, default to interactive mode
144 if(!opts
.wasSetByUserInteractive()) {
145 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
148 // Auto-detect input language by filename extension
149 const char* filename
= inputFromStdin
? "<stdin>" : filenames
[0].c_str();
151 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
152 if( inputFromStdin
) {
153 // We can't do any fancy detection on stdin
154 opts
.setInputLanguage(language::input::LANG_CVC4
);
156 unsigned len
= strlen(filename
);
157 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
158 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_5
);
159 } else if(len
>= 4 && !strcmp(".smt", filename
+ len
- 4)) {
160 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
161 } else if(len
>= 5 && !strcmp(".smt1", filename
+ len
- 5)) {
162 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
163 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
164 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
165 opts
.setInputLanguage(language::input::LANG_TPTP
);
166 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
167 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
168 opts
.setInputLanguage(language::input::LANG_CVC4
);
169 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
170 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
171 opts
.setInputLanguage(language::input::LANG_SYGUS
);
172 //since there is no sygus output language, set this to SMT lib 2
173 //opts.setOutputLanguage(language::output::LANG_SMTLIB_V2_0);
178 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
179 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
182 // if doing sygus, turn on CEGQI by default
183 if(opts
.getInputLanguage() == language::input::LANG_SYGUS
){
184 if( !opts
.wasSetByUserCeGuidedInst()) {
185 opts
.setCeGuidedInst(true);
187 if( !opts
.wasSetByUserDumpSynth()) {
188 opts
.setDumpSynth(true);
192 // Determine which messages to show based on smtcomp_mode and verbosity
193 if(Configuration::isMuzzledBuild()) {
194 DebugChannel
.setStream(&CVC4::null_os
);
195 TraceChannel
.setStream(&CVC4::null_os
);
196 NoticeChannel
.setStream(&CVC4::null_os
);
197 ChatChannel
.setStream(&CVC4::null_os
);
198 MessageChannel
.setStream(&CVC4::null_os
);
199 WarningChannel
.setStream(&CVC4::null_os
);
202 // important even for muzzled builds (to get result output right)
203 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
205 // Create the expression manager using appropriate options
206 ExprManager
* exprMgr
;
207 # ifndef PORTFOLIO_BUILD
208 exprMgr
= new ExprManager(opts
);
209 pExecutor
= new CommandExecutor(*exprMgr
, opts
);
211 OptionsList threadOpts
;
212 parseThreadSpecificOptions(threadOpts
, opts
);
214 bool useParallelExecutor
= true;
216 if(opts
.wasSetByUserIncrementalSolving() &&
217 opts
.getIncrementalSolving() &&
218 (! opts
.getIncrementalParallel()) ) {
219 Notice() << "Notice: In --incremental mode, using the sequential solver"
220 << " unless forced by...\n"
221 << "Notice: ...the experimental --incremental-parallel option.\n";
222 useParallelExecutor
= false;
225 if(opts
.getCheckProofs()) {
226 if(opts
.getFallbackSequential()) {
227 Warning() << "Warning: Falling back to sequential mode, as cannot run"
228 << " portfolio in check-proofs mode.\n";
229 useParallelExecutor
= false;
232 throw OptionException("Cannot run portfolio in check-proofs mode.");
235 // pick appropriate one
236 if(useParallelExecutor
) {
237 exprMgr
= new ExprManager(threadOpts
[0]);
238 pExecutor
= new CommandExecutorPortfolio(*exprMgr
, opts
, threadOpts
);
240 exprMgr
= new ExprManager(opts
);
241 pExecutor
= new CommandExecutor(*exprMgr
, opts
);
245 PtrCloser
<Parser
> replayParser
;
246 if( opts
.getReplayInputFilename() != "" ) {
247 std::string replayFilename
= opts
.getReplayInputFilename();
248 ParserBuilder
replayParserBuilder(exprMgr
, replayFilename
, opts
);
250 if( replayFilename
== "-") {
251 if( inputFromStdin
) {
252 throw OptionException("Replay file and input file can't both be stdin.");
254 replayParserBuilder
.withStreamInput(cin
);
256 replayParser
.reset(replayParserBuilder
.build());
257 pExecutor
->setReplayStream(new Parser::ExprStream(replayParser
.get()));
263 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
266 // Filename statistics
267 ReferenceStat
< const char* > s_statFilename("filename", filename
);
268 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
271 // Parse and execute commands until we are done
274 if(opts
.getInteractive() && inputFromStdin
) {
275 if(opts
.getTearDownIncremental() > 0) {
276 throw OptionException(
277 "--tear-down-incremental doesn't work in interactive mode");
279 #ifndef PORTFOLIO_BUILD
280 if(!opts
.wasSetByUserIncrementalSolving()) {
281 cmd
= new SetOptionCommand("incremental", SExpr(true));
283 pExecutor
->doCommand(cmd
);
286 #endif /* PORTFOLIO_BUILD */
287 InteractiveShell
shell(*exprMgr
, opts
);
288 if(opts
.getInteractivePrompt()) {
289 Message() << Configuration::getPackageName()
290 << " " << Configuration::getVersionString();
291 if(Configuration::isGitBuild()) {
292 Message() << " [" << Configuration::getGitId() << "]";
293 } else if(Configuration::isSubversionBuild()) {
294 Message() << " [" << Configuration::getSubversionId() << "]";
296 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
298 << (Configuration::isAssertionBuild() ? "on" : "off")
302 // have the replay parser use the declarations input interactively
303 replayParser
->useDeclarationsFrom(shell
.getParser());
308 cmd
= shell
.readCommand();
309 } catch(UnsafeInterruptException
& e
) {
310 (*opts
.getOut()) << CommandInterrupted();
315 status
= pExecutor
->doCommand(cmd
) && status
;
316 if (cmd
->interrupted()) {
322 } else if( opts
.getTearDownIncremental() > 0) {
323 if(!opts
.getIncrementalSolving() && opts
.getTearDownIncremental() > 1) {
324 // For tear-down-incremental values greater than 1, need incremental
326 cmd
= new SetOptionCommand("incremental", SExpr(true));
328 pExecutor
->doCommand(cmd
);
330 // if(opts.wasSetByUserIncrementalSolving()) {
331 // throw OptionException(
332 // "--tear-down-incremental incompatible with --incremental");
335 // cmd = new SetOptionCommand("incremental", SExpr(false));
336 // cmd->setMuted(true);
337 // pExecutor->doCommand(cmd);
341 ParserBuilder
parserBuilder(exprMgr
, filename
, opts
);
343 if( inputFromStdin
) {
344 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
345 parserBuilder
.withStreamInput(cin
);
346 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
347 parserBuilder
.withLineBufferedStreamInput(cin
);
348 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
351 vector
< vector
<Command
*> > allCommands
;
352 allCommands
.push_back(vector
<Command
*>());
353 PtrCloser
<Parser
> parser(parserBuilder
.build());
355 // have the replay parser use the file's declarations
356 replayParser
->useDeclarationsFrom(parser
.get());
359 // true if one of the commands was interrupted
360 bool interrupted
= false;
361 while (status
|| opts
.getContinuedExecution()) {
363 (*opts
.getOut()) << CommandInterrupted();
368 cmd
= parser
->nextCommand();
369 if (cmd
== NULL
) break;
370 } catch (UnsafeInterruptException
& e
) {
375 if(dynamic_cast<PushCommand
*>(cmd
) != NULL
) {
376 if(needReset
>= opts
.getTearDownIncremental()) {
378 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
379 if (interrupted
) break;
380 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
382 Command
* cmd
= allCommands
[i
][j
]->clone();
384 pExecutor
->doCommand(cmd
);
385 if(cmd
->interrupted()) {
393 allCommands
.push_back(vector
<Command
*>());
394 Command
* copy
= cmd
->clone();
395 allCommands
.back().push_back(copy
);
396 status
= pExecutor
->doCommand(cmd
);
397 if(cmd
->interrupted()) {
401 } else if(dynamic_cast<PopCommand
*>(cmd
) != NULL
) {
402 allCommands
.pop_back(); // fixme leaks cmds here
403 if (needReset
>= opts
.getTearDownIncremental()) {
405 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
406 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
408 Command
* cmd
= allCommands
[i
][j
]->clone();
410 pExecutor
->doCommand(cmd
);
411 if(cmd
->interrupted()) {
417 if (interrupted
) continue;
418 (*opts
.getOut()) << CommandSuccess();
421 status
= pExecutor
->doCommand(cmd
);
422 if(cmd
->interrupted()) {
427 } else if(dynamic_cast<CheckSatCommand
*>(cmd
) != NULL
||
428 dynamic_cast<QueryCommand
*>(cmd
) != NULL
) {
429 if(needReset
>= opts
.getTearDownIncremental()) {
431 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
432 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
434 Command
* cmd
= allCommands
[i
][j
]->clone();
436 pExecutor
->doCommand(cmd
);
437 if(cmd
->interrupted()) {
451 status
= pExecutor
->doCommand(cmd
);
452 if(cmd
->interrupted()) {
456 } else if(dynamic_cast<ResetCommand
*>(cmd
) != NULL
) {
457 pExecutor
->doCommand(cmd
);
459 allCommands
.push_back(vector
<Command
*>());
461 // We shouldn't copy certain commands, because they can cause
462 // an error on replay since there's no associated sat/unsat check
464 if(dynamic_cast<GetUnsatCoreCommand
*>(cmd
) == NULL
&&
465 dynamic_cast<GetProofCommand
*>(cmd
) == NULL
&&
466 dynamic_cast<GetValueCommand
*>(cmd
) == NULL
&&
467 dynamic_cast<GetModelCommand
*>(cmd
) == NULL
&&
468 dynamic_cast<GetAssignmentCommand
*>(cmd
) == NULL
&&
469 dynamic_cast<GetInstantiationsCommand
*>(cmd
) == NULL
&&
470 dynamic_cast<GetAssertionsCommand
*>(cmd
) == NULL
&&
471 dynamic_cast<GetInfoCommand
*>(cmd
) == NULL
&&
472 dynamic_cast<GetOptionCommand
*>(cmd
) == NULL
&&
473 dynamic_cast<EchoCommand
*>(cmd
) == NULL
) {
474 Command
* copy
= cmd
->clone();
475 allCommands
.back().push_back(copy
);
477 status
= pExecutor
->doCommand(cmd
);
478 if(cmd
->interrupted()) {
483 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
491 if(!opts
.wasSetByUserIncrementalSolving()) {
492 cmd
= new SetOptionCommand("incremental", SExpr(false));
494 pExecutor
->doCommand(cmd
);
498 ParserBuilder
parserBuilder(exprMgr
, filename
, opts
);
500 if( inputFromStdin
) {
501 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
502 parserBuilder
.withStreamInput(cin
);
503 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
504 parserBuilder
.withLineBufferedStreamInput(cin
);
505 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
508 PtrCloser
<Parser
> parser(parserBuilder
.build());
510 // have the replay parser use the file's declarations
511 replayParser
->useDeclarationsFrom(parser
.get());
513 bool interrupted
= false;
514 while(status
|| opts
.getContinuedExecution()) {
516 (*opts
.getOut()) << CommandInterrupted();
521 cmd
= parser
->nextCommand();
522 if (cmd
== NULL
) break;
523 } catch (UnsafeInterruptException
& e
) {
528 status
= pExecutor
->doCommand(cmd
);
529 if (cmd
->interrupted() && status
== 0) {
534 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
544 result
= pExecutor
->getResult();
547 // there was some kind of error
551 #ifdef CVC4_COMPETITION_MODE
553 // exit, don't return (don't want destructors to run)
554 // _exit() from unistd.h doesn't run global destructors
555 // or other on_exit/atexit stuff.
557 #endif /* CVC4_COMPETITION_MODE */
559 ReferenceStat
< Result
> s_statSatResult("sat/unsat", result
);
560 RegisterStatistic
statSatResultReg(&pExecutor
->getStatisticsRegistry(),
565 // Tim: I think that following comment is out of date?
566 // Set the global executor pointer to NULL first. If we get a
567 // signal while dumping statistics, we don't want to try again.
568 pExecutor
->flushOutputStreams();
571 if(opts
.getEarlyExit() && opts
.wasSetByUserEarlyExit()) {
574 #else /* CVC4_DEBUG */
575 if(opts
.getEarlyExit()) {
578 #endif /* CVC4_DEBUG */
581 // On exceptional exit, these are leaked, but that's okay... they
582 // need to be around in that case for main() to print statistics.