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 "expr/expr_iomanip.h"
31 #include "expr/expr_manager.h"
32 #include "main/command_executor.h"
34 #ifdef PORTFOLIO_BUILD
35 # include "main/command_executor_portfolio.h"
38 #include "main/interactive_shell.h"
39 #include "main/main.h"
40 #include "options/options.h"
41 #include "options/set_language.h"
42 #include "parser/parser.h"
43 #include "parser/parser_builder.h"
44 #include "parser/parser_exception.h"
45 #include "smt/command.h"
46 #include "util/result.h"
47 #include "util/statistics_registry.h"
51 using namespace CVC4::parser
;
52 using namespace CVC4::main
;
56 /** Global options variable */
57 CVC4_THREADLOCAL(Options
*) pOptions
;
62 /** Just the basename component of argv[0] */
65 /** A pointer to the CommandExecutor (the signal handlers need it) */
66 CVC4::main::CommandExecutor
* pExecutor
= NULL
;
68 /** A pointer to the totalTime driver stat (the signal handlers need it) */
69 CVC4::TimerStat
* pTotalTime
= NULL
;
71 }/* CVC4::main namespace */
75 void printUsage(Options
& opts
, bool full
) {
77 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
79 << "Without an input file, or with `-', CVC4 reads from standard input."
81 << "CVC4 options:" << endl
;
83 Options::printUsage( ss
.str(), *(opts
.getOut()) );
85 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
89 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
92 pTotalTime
= new TimerStat("totalTime");
95 // For the signal handlers' benefit
98 // Initialize the signal handlers
104 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
106 # ifndef PORTFOLIO_BUILD
107 if( opts
.wasSetByUserThreads() ||
108 opts
.wasSetByUserThreadStackSize() ||
109 (! opts
.getThreadArgv().empty()) ) {
110 throw OptionException("Thread options cannot be used with sequential CVC4. Please build and use the portfolio binary `pcvc4'.");
114 progName
= opts
.getBinaryName().c_str();
116 if( opts
.getHelp() ) {
117 printUsage(opts
, true);
119 } else if( opts
.getLanguageHelp() ) {
120 Options::printLanguageHelp(*(opts
.getOut()));
122 } else if( opts
.getVersion() ) {
123 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
127 segvSpin
= opts
.getSegvSpin();
129 // If in competition mode, set output stream option to flush immediately
130 #ifdef CVC4_COMPETITION_MODE
131 *(opts
.getOut()) << unitbuf
;
132 #endif /* CVC4_COMPETITION_MODE */
134 // We only accept one input file
135 if(filenames
.size() > 1) {
136 throw Exception("Too many input files specified.");
139 // If no file supplied we will read from standard input
140 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
142 // if we're reading from stdin on a TTY, default to interactive mode
143 if(!opts
.wasSetByUserInteractive()) {
144 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
147 // Auto-detect input language by filename extension
148 const char* filename
= inputFromStdin
? "<stdin>" : filenames
[0].c_str();
150 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
151 if( inputFromStdin
) {
152 // We can't do any fancy detection on stdin
153 opts
.setInputLanguage(language::input::LANG_CVC4
);
155 unsigned len
= strlen(filename
);
156 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
157 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_0
);
158 } else if(len
>= 4 && !strcmp(".smt", filename
+ len
- 4)) {
159 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
160 } else if(len
>= 5 && !strcmp(".smt1", filename
+ len
- 5)) {
161 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
162 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
163 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
164 opts
.setInputLanguage(language::input::LANG_TPTP
);
165 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
166 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
167 opts
.setInputLanguage(language::input::LANG_CVC4
);
168 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
169 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
170 opts
.setInputLanguage(language::input::LANG_SYGUS
);
171 //since there is no sygus output language, set this to SMT lib 2
172 //opts.setOutputLanguage(language::output::LANG_SMTLIB_V2_0);
177 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
178 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
181 // if doing sygus, turn on CEGQI by default
182 if(opts
.getInputLanguage() == language::input::LANG_SYGUS
){
183 if( !opts
.wasSetByUserCeGuidedInst()) {
184 opts
.setCeGuidedInst(true);
186 if( !opts
.wasSetByUserDumpSynth()) {
187 opts
.setDumpSynth(true);
191 // Determine which messages to show based on smtcomp_mode and verbosity
192 if(Configuration::isMuzzledBuild()) {
193 DebugChannel
.setStream(&CVC4::null_os
);
194 TraceChannel
.setStream(&CVC4::null_os
);
195 NoticeChannel
.setStream(&CVC4::null_os
);
196 ChatChannel
.setStream(&CVC4::null_os
);
197 MessageChannel
.setStream(&CVC4::null_os
);
198 WarningChannel
.setStream(&CVC4::null_os
);
201 // important even for muzzled builds (to get result output right)
202 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
204 // Create the expression manager using appropriate options
205 ExprManager
* exprMgr
;
206 # ifndef PORTFOLIO_BUILD
207 exprMgr
= new ExprManager(opts
);
208 pExecutor
= new CommandExecutor(*exprMgr
, opts
);
210 OptionsList threadOpts
;
211 parseThreadSpecificOptions(threadOpts
, opts
);
213 bool useParallelExecutor
= true;
215 if(opts
.wasSetByUserIncrementalSolving() &&
216 opts
.getIncrementalSolving() &&
217 (! opts
.getIncrementalParallel()) ) {
218 Notice() << "Notice: In --incremental mode, using the sequential solver"
219 << " unless forced by...\n"
220 << "Notice: ...the experimental --incremental-parallel option.\n";
221 useParallelExecutor
= false;
224 if(opts
.getCheckProofs()) {
225 if(opts
.getFallbackSequential()) {
226 Warning() << "Warning: Falling back to sequential mode, as cannot run"
227 << " portfolio in check-proofs mode.\n";
228 useParallelExecutor
= false;
231 throw OptionException("Cannot run portfolio in check-proofs mode.");
234 // pick appropriate one
235 if(useParallelExecutor
) {
236 exprMgr
= new ExprManager(threadOpts
[0]);
237 pExecutor
= new CommandExecutorPortfolio(*exprMgr
, opts
, threadOpts
);
239 exprMgr
= new ExprManager(opts
);
240 pExecutor
= new CommandExecutor(*exprMgr
, opts
);
244 Parser
* replayParser
= NULL
;
245 if( opts
.getReplayInputFilename() != "" ) {
246 std::string replayFilename
= opts
.getReplayInputFilename();
247 ParserBuilder
replayParserBuilder(exprMgr
, replayFilename
, opts
);
249 if( replayFilename
== "-") {
250 if( inputFromStdin
) {
251 throw OptionException("Replay file and input file can't both be stdin.");
253 replayParserBuilder
.withStreamInput(cin
);
255 replayParser
= replayParserBuilder
.build();
256 pExecutor
->setReplayStream(new Parser::ExprStream(replayParser
));
262 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
265 // Filename statistics
266 ReferenceStat
< const char* > s_statFilename("filename", filename
);
267 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
270 // Parse and execute commands until we are done
273 if(opts
.getInteractive() && inputFromStdin
) {
274 if(opts
.getTearDownIncremental() > 0) {
275 throw OptionException(
276 "--tear-down-incremental doesn't work in interactive mode");
278 #ifndef PORTFOLIO_BUILD
279 if(!opts
.wasSetByUserIncrementalSolving()) {
280 cmd
= new SetOptionCommand("incremental", SExpr(true));
282 pExecutor
->doCommand(cmd
);
285 #endif /* PORTFOLIO_BUILD */
286 InteractiveShell
shell(*exprMgr
, opts
);
287 if(opts
.getInteractivePrompt()) {
288 Message() << Configuration::getPackageName()
289 << " " << Configuration::getVersionString();
290 if(Configuration::isGitBuild()) {
291 Message() << " [" << Configuration::getGitId() << "]";
292 } else if(Configuration::isSubversionBuild()) {
293 Message() << " [" << Configuration::getSubversionId() << "]";
295 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
297 << (Configuration::isAssertionBuild() ? "on" : "off")
300 if(replayParser
!= NULL
) {
301 // have the replay parser use the declarations input interactively
302 replayParser
->useDeclarationsFrom(shell
.getParser());
307 cmd
= shell
.readCommand();
308 } catch(UnsafeInterruptException
& e
) {
309 (*opts
.getOut()) << CommandInterrupted();
314 status
= pExecutor
->doCommand(cmd
) && status
;
315 if (cmd
->interrupted()) {
321 } else if( opts
.getTearDownIncremental() > 0) {
322 if(!opts
.getIncrementalSolving()) {
323 cmd
= new SetOptionCommand("incremental", SExpr(true));
325 pExecutor
->doCommand(cmd
);
327 // if(opts.wasSetByUserIncrementalSolving()) {
328 // throw OptionException(
329 // "--tear-down-incremental incompatible with --incremental");
332 // cmd = new SetOptionCommand("incremental", SExpr(false));
333 // cmd->setMuted(true);
334 // pExecutor->doCommand(cmd);
338 ParserBuilder
parserBuilder(exprMgr
, filename
, opts
);
340 if( inputFromStdin
) {
341 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
342 parserBuilder
.withStreamInput(cin
);
343 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
344 parserBuilder
.withLineBufferedStreamInput(cin
);
345 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
348 vector
< vector
<Command
*> > allCommands
;
349 allCommands
.push_back(vector
<Command
*>());
350 Parser
*parser
= parserBuilder
.build();
351 if(replayParser
!= NULL
) {
352 // have the replay parser use the file's declarations
353 replayParser
->useDeclarationsFrom(parser
);
356 // true if one of the commands was interrupted
357 bool interrupted
= false;
358 while (status
|| opts
.getContinuedExecution()) {
360 (*opts
.getOut()) << CommandInterrupted();
365 cmd
= parser
->nextCommand();
366 if (cmd
== NULL
) break;
367 } catch (UnsafeInterruptException
& e
) {
372 if(dynamic_cast<PushCommand
*>(cmd
) != NULL
) {
373 if(needReset
>= opts
.getTearDownIncremental()) {
375 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
376 if (interrupted
) break;
377 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
379 Command
* cmd
= allCommands
[i
][j
]->clone();
381 pExecutor
->doCommand(cmd
);
382 if(cmd
->interrupted()) {
390 allCommands
.push_back(vector
<Command
*>());
391 Command
* copy
= cmd
->clone();
392 allCommands
.back().push_back(copy
);
393 status
= pExecutor
->doCommand(cmd
);
394 if(cmd
->interrupted()) {
398 } else if(dynamic_cast<PopCommand
*>(cmd
) != NULL
) {
399 allCommands
.pop_back(); // fixme leaks cmds here
400 if (needReset
>= opts
.getTearDownIncremental()) {
402 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
403 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
405 Command
* cmd
= allCommands
[i
][j
]->clone();
407 pExecutor
->doCommand(cmd
);
408 if(cmd
->interrupted()) {
414 if (interrupted
) continue;
415 (*opts
.getOut()) << CommandSuccess();
418 status
= pExecutor
->doCommand(cmd
);
419 if(cmd
->interrupted()) {
424 } else if(dynamic_cast<CheckSatCommand
*>(cmd
) != NULL
||
425 dynamic_cast<QueryCommand
*>(cmd
) != NULL
) {
426 if(needReset
>= opts
.getTearDownIncremental()) {
428 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
429 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
431 Command
* cmd
= allCommands
[i
][j
]->clone();
433 pExecutor
->doCommand(cmd
);
434 if(cmd
->interrupted()) {
448 status
= pExecutor
->doCommand(cmd
);
449 if(cmd
->interrupted()) {
453 } else if(dynamic_cast<ResetCommand
*>(cmd
) != NULL
) {
454 pExecutor
->doCommand(cmd
);
456 allCommands
.push_back(vector
<Command
*>());
458 // We shouldn't copy certain commands, because they can cause
459 // an error on replay since there's no associated sat/unsat check
461 if(dynamic_cast<GetUnsatCoreCommand
*>(cmd
) == NULL
&&
462 dynamic_cast<GetProofCommand
*>(cmd
) == NULL
&&
463 dynamic_cast<GetValueCommand
*>(cmd
) == NULL
&&
464 dynamic_cast<GetModelCommand
*>(cmd
) == NULL
&&
465 dynamic_cast<GetAssignmentCommand
*>(cmd
) == NULL
&&
466 dynamic_cast<GetInstantiationsCommand
*>(cmd
) == NULL
&&
467 dynamic_cast<GetAssertionsCommand
*>(cmd
) == NULL
&&
468 dynamic_cast<GetInfoCommand
*>(cmd
) == NULL
&&
469 dynamic_cast<GetOptionCommand
*>(cmd
) == NULL
&&
470 dynamic_cast<EchoCommand
*>(cmd
) == NULL
) {
471 Command
* copy
= cmd
->clone();
472 allCommands
.back().push_back(copy
);
474 status
= pExecutor
->doCommand(cmd
);
475 if(cmd
->interrupted()) {
480 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
490 if(!opts
.wasSetByUserIncrementalSolving()) {
491 cmd
= new SetOptionCommand("incremental", SExpr(false));
493 pExecutor
->doCommand(cmd
);
497 ParserBuilder
parserBuilder(exprMgr
, filename
, opts
);
499 if( inputFromStdin
) {
500 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
501 parserBuilder
.withStreamInput(cin
);
502 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
503 parserBuilder
.withLineBufferedStreamInput(cin
);
504 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
507 Parser
*parser
= parserBuilder
.build();
508 if(replayParser
!= NULL
) {
509 // have the replay parser use the file's declarations
510 replayParser
->useDeclarationsFrom(parser
);
512 bool interrupted
= false;
513 while(status
|| opts
.getContinuedExecution()) {
515 (*opts
.getOut()) << CommandInterrupted();
520 cmd
= parser
->nextCommand();
521 if (cmd
== NULL
) break;
522 } catch (UnsafeInterruptException
& e
) {
527 status
= pExecutor
->doCommand(cmd
);
528 if (cmd
->interrupted() && status
== 0) {
533 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
545 result
= pExecutor
->getResult();
548 // there was some kind of error
552 #ifdef CVC4_COMPETITION_MODE
554 // exit, don't return (don't want destructors to run)
555 // _exit() from unistd.h doesn't run global destructors
556 // or other on_exit/atexit stuff.
558 #endif /* CVC4_COMPETITION_MODE */
560 ReferenceStat
< Result
> s_statSatResult("sat/unsat", result
);
561 RegisterStatistic
statSatResultReg(&pExecutor
->getStatisticsRegistry(),
566 // Tim: I think that following comment is out of date?
567 // Set the global executor pointer to NULL first. If we get a
568 // signal while dumping statistics, we don't want to try again.
569 pExecutor
->flushOutputStreams();
572 if(opts
.getEarlyExit() && opts
.wasSetByUserEarlyExit()) {
575 #else /* CVC4_DEBUG */
576 if(opts
.getEarlyExit()) {
579 #endif /* CVC4_DEBUG */
582 // On exceptional exit, these are leaked, but that's okay... they
583 // need to be around in that case for main() to print statistics.