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-2019 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
26 #include "cvc4autoconfig.h"
28 #include "api/cvc4cpp.h"
29 #include "base/configuration.h"
30 #include "base/output.h"
31 #include "expr/expr_iomanip.h"
32 #include "expr/expr_manager.h"
33 #include "main/command_executor.h"
34 #include "main/interactive_shell.h"
35 #include "main/main.h"
36 #include "options/options.h"
37 #include "options/set_language.h"
38 #include "parser/parser.h"
39 #include "parser/parser_builder.h"
40 #include "parser/parser_exception.h"
41 #include "smt/command.h"
42 #include "util/result.h"
43 #include "util/statistics_registry.h"
45 // The PORTFOLIO_BUILD is defined when compiling pcvc4 (the parallel version of
46 // CVC4) and undefined otherwise. The macro can only be used in
47 // driver_unified.cpp because we do not recompile all files for pcvc4.
48 #ifdef PORTFOLIO_BUILD
49 # include "main/command_executor_portfolio.h"
54 using namespace CVC4::parser
;
55 using namespace CVC4::main
;
59 /** Global options variable */
60 thread_local Options
* pOptions
;
65 /** Just the basename component of argv[0] */
66 const std::string
*progName
;
68 /** A pointer to the CommandExecutor (the signal handlers need it) */
69 CVC4::main::CommandExecutor
* pExecutor
= NULL
;
71 /** A pointer to the totalTime driver stat (the signal handlers need it) */
72 CVC4::TimerStat
* pTotalTime
= NULL
;
74 }/* CVC4::main namespace */
78 void printUsage(Options
& opts
, bool full
) {
80 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
82 << "Without an input file, or with `-', CVC4 reads from standard input."
84 << "CVC4 options:" << endl
;
86 Options::printUsage( ss
.str(), *(opts
.getOut()) );
88 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
92 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
95 pTotalTime
= new TimerStat("totalTime");
98 // For the signal handlers' benefit
101 // Initialize the signal handlers
107 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
109 # ifndef PORTFOLIO_BUILD
110 if( opts
.wasSetByUserThreads() ||
111 opts
.wasSetByUserThreadStackSize() ||
112 (! opts
.getThreadArgv().empty()) ) {
113 throw OptionException("Thread options cannot be used with sequential CVC4. Please build and use the portfolio binary `pcvc4'.");
117 string progNameStr
= opts
.getBinaryName();
118 progName
= &progNameStr
;
120 if( opts
.getHelp() ) {
121 printUsage(opts
, true);
123 } else if( opts
.getLanguageHelp() ) {
124 Options::printLanguageHelp(*(opts
.getOut()));
126 } else if( opts
.getVersion() ) {
127 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
131 segvSpin
= opts
.getSegvSpin();
133 // If in competition mode, set output stream option to flush immediately
134 #ifdef CVC4_COMPETITION_MODE
135 *(opts
.getOut()) << unitbuf
;
136 #endif /* CVC4_COMPETITION_MODE */
138 // We only accept one input file
139 if(filenames
.size() > 1) {
140 throw Exception("Too many input files specified.");
143 // If no file supplied we will read from standard input
144 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
146 // if we're reading from stdin on a TTY, default to interactive mode
147 if(!opts
.wasSetByUserInteractive()) {
148 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
151 // Auto-detect input language by filename extension
152 std::string
filenameStr("<stdin>");
153 if (!inputFromStdin
) {
154 // Use swap to avoid copying the string
155 // TODO: use std::move() when switching to c++11
156 filenameStr
.swap(filenames
[0]);
158 const char* filename
= filenameStr
.c_str();
160 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
161 if( inputFromStdin
) {
162 // We can't do any fancy detection on stdin
163 opts
.setInputLanguage(language::input::LANG_CVC4
);
165 unsigned len
= filenameStr
.size();
166 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
167 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_6
);
168 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
169 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
170 opts
.setInputLanguage(language::input::LANG_TPTP
);
171 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
172 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
173 opts
.setInputLanguage(language::input::LANG_CVC4
);
174 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
175 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
176 opts
.setInputLanguage(language::input::LANG_SYGUS
);
177 //since there is no sygus output language, set this to SMT lib 2
178 //opts.setOutputLanguage(language::output::LANG_SMTLIB_V2_0);
183 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
184 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
187 // Determine which messages to show based on smtcomp_mode and verbosity
188 if(Configuration::isMuzzledBuild()) {
189 DebugChannel
.setStream(&CVC4::null_os
);
190 TraceChannel
.setStream(&CVC4::null_os
);
191 NoticeChannel
.setStream(&CVC4::null_os
);
192 ChatChannel
.setStream(&CVC4::null_os
);
193 MessageChannel
.setStream(&CVC4::null_os
);
194 WarningChannel
.setStream(&CVC4::null_os
);
197 // important even for muzzled builds (to get result output right)
198 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
200 // Create the expression manager using appropriate options
201 std::unique_ptr
<api::Solver
> solver
;
202 # ifndef PORTFOLIO_BUILD
203 solver
.reset(new api::Solver(&opts
));
204 pExecutor
= new CommandExecutor(solver
.get(), opts
);
206 OptionsList threadOpts
;
207 parseThreadSpecificOptions(threadOpts
, opts
);
209 bool useParallelExecutor
= true;
211 if(opts
.wasSetByUserIncrementalSolving() &&
212 opts
.getIncrementalSolving() &&
213 (! opts
.getIncrementalParallel()) ) {
214 Notice() << "Notice: In --incremental mode, using the sequential solver"
215 << " unless forced by...\n"
216 << "Notice: ...the experimental --incremental-parallel option.\n";
217 useParallelExecutor
= false;
220 if(opts
.getCheckProofs()) {
221 if(opts
.getFallbackSequential()) {
222 Warning() << "Warning: Falling back to sequential mode, as cannot run"
223 << " portfolio in check-proofs mode.\n";
224 useParallelExecutor
= false;
227 throw OptionException("Cannot run portfolio in check-proofs mode.");
230 // pick appropriate one
231 if (useParallelExecutor
)
233 solver
.reset(new api::Solver(&threadOpts
[0]));
234 pExecutor
= new CommandExecutorPortfolio(solver
.get(), opts
, threadOpts
);
238 solver
.reset(new api::Solver(&opts
));
239 pExecutor
= new CommandExecutor(solver
.get(), opts
);
243 std::unique_ptr
<Parser
> replayParser
;
244 if (opts
.getReplayInputFilename() != "")
246 std::string replayFilename
= opts
.getReplayInputFilename();
247 ParserBuilder
replayParserBuilder(solver
.get(), 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
.reset(replayParserBuilder
.build());
256 pExecutor
->setReplayStream(new Parser::ExprStream(replayParser
.get()));
262 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
265 // Filename statistics
266 ReferenceStat
<std::string
> s_statFilename("filename", filenameStr
);
267 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
269 // set filename in smt engine
270 pExecutor
->getSmtEngine()->setFilename(filenameStr
);
272 // Parse and execute commands until we are done
275 if(opts
.getInteractive() && inputFromStdin
) {
276 if(opts
.getTearDownIncremental() > 0) {
277 throw OptionException(
278 "--tear-down-incremental doesn't work in interactive mode");
280 #ifndef PORTFOLIO_BUILD
281 if(!opts
.wasSetByUserIncrementalSolving()) {
282 cmd
= new SetOptionCommand("incremental", SExpr(true));
284 pExecutor
->doCommand(cmd
);
287 #endif /* PORTFOLIO_BUILD */
288 InteractiveShell
shell(solver
.get());
289 if(opts
.getInteractivePrompt()) {
290 Message() << Configuration::getPackageName()
291 << " " << Configuration::getVersionString();
292 if(Configuration::isGitBuild()) {
293 Message() << " [" << Configuration::getGitId() << "]";
295 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
297 << (Configuration::isAssertionBuild() ? "on" : "off")
299 Message() << Configuration::copyright() << endl
;
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(solver
.get(), 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 std::unique_ptr
<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;
364 (*opts
.getOut()) << CommandInterrupted();
369 cmd
= parser
->nextCommand();
370 if (cmd
== NULL
) break;
371 } catch (UnsafeInterruptException
& e
) {
376 if(dynamic_cast<PushCommand
*>(cmd
) != NULL
) {
377 if(needReset
>= opts
.getTearDownIncremental()) {
379 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
380 if (interrupted
) break;
381 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
383 Command
* cmd
= allCommands
[i
][j
]->clone();
385 pExecutor
->doCommand(cmd
);
386 if(cmd
->interrupted()) {
394 allCommands
.push_back(vector
<Command
*>());
395 Command
* copy
= cmd
->clone();
396 allCommands
.back().push_back(copy
);
397 status
= pExecutor
->doCommand(cmd
);
398 if(cmd
->interrupted()) {
402 } else if(dynamic_cast<PopCommand
*>(cmd
) != NULL
) {
403 allCommands
.pop_back(); // fixme leaks cmds here
404 if (needReset
>= opts
.getTearDownIncremental()) {
406 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
407 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
409 Command
* cmd
= allCommands
[i
][j
]->clone();
411 pExecutor
->doCommand(cmd
);
412 if(cmd
->interrupted()) {
418 if (interrupted
) continue;
419 (*opts
.getOut()) << CommandSuccess();
422 status
= pExecutor
->doCommand(cmd
);
423 if(cmd
->interrupted()) {
428 } else if(dynamic_cast<CheckSatCommand
*>(cmd
) != NULL
||
429 dynamic_cast<QueryCommand
*>(cmd
) != NULL
) {
430 if(needReset
>= opts
.getTearDownIncremental()) {
432 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
433 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
435 Command
* cmd
= allCommands
[i
][j
]->clone();
437 pExecutor
->doCommand(cmd
);
438 if(cmd
->interrupted()) {
452 status
= pExecutor
->doCommand(cmd
);
453 if(cmd
->interrupted()) {
457 } else if(dynamic_cast<ResetCommand
*>(cmd
) != NULL
) {
458 pExecutor
->doCommand(cmd
);
460 allCommands
.push_back(vector
<Command
*>());
462 // We shouldn't copy certain commands, because they can cause
463 // an error on replay since there's no associated sat/unsat check
465 if(dynamic_cast<GetUnsatCoreCommand
*>(cmd
) == NULL
&&
466 dynamic_cast<GetProofCommand
*>(cmd
) == NULL
&&
467 dynamic_cast<GetValueCommand
*>(cmd
) == NULL
&&
468 dynamic_cast<GetModelCommand
*>(cmd
) == NULL
&&
469 dynamic_cast<GetAssignmentCommand
*>(cmd
) == NULL
&&
470 dynamic_cast<GetInstantiationsCommand
*>(cmd
) == NULL
&&
471 dynamic_cast<GetAssertionsCommand
*>(cmd
) == NULL
&&
472 dynamic_cast<GetInfoCommand
*>(cmd
) == NULL
&&
473 dynamic_cast<GetOptionCommand
*>(cmd
) == NULL
&&
474 dynamic_cast<EchoCommand
*>(cmd
) == NULL
) {
475 Command
* copy
= cmd
->clone();
476 allCommands
.back().push_back(copy
);
478 status
= pExecutor
->doCommand(cmd
);
479 if(cmd
->interrupted()) {
484 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
492 if(!opts
.wasSetByUserIncrementalSolving()) {
493 cmd
= new SetOptionCommand("incremental", SExpr(false));
495 pExecutor
->doCommand(cmd
);
499 ParserBuilder
parserBuilder(solver
.get(), filename
, opts
);
501 if( inputFromStdin
) {
502 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
503 parserBuilder
.withStreamInput(cin
);
504 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
505 parserBuilder
.withLineBufferedStreamInput(cin
);
506 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
509 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
511 // have the replay parser use the file's declarations
512 replayParser
->useDeclarationsFrom(parser
.get());
514 bool interrupted
= false;
518 (*opts
.getOut()) << CommandInterrupted();
523 cmd
= parser
->nextCommand();
524 if (cmd
== NULL
) break;
525 } catch (UnsafeInterruptException
& e
) {
530 status
= pExecutor
->doCommand(cmd
);
531 if (cmd
->interrupted() && status
== 0) {
536 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
546 result
= pExecutor
->getResult();
549 // there was some kind of error
553 #ifdef CVC4_COMPETITION_MODE
555 // exit, don't return (don't want destructors to run)
556 // _exit() from unistd.h doesn't run global destructors
557 // or other on_exit/atexit stuff.
559 #endif /* CVC4_COMPETITION_MODE */
561 ReferenceStat
< Result
> s_statSatResult("sat/unsat", result
);
562 RegisterStatistic
statSatResultReg(&pExecutor
->getStatisticsRegistry(),
567 // Tim: I think that following comment is out of date?
568 // Set the global executor pointer to NULL first. If we get a
569 // signal while dumping statistics, we don't want to try again.
570 pExecutor
->flushOutputStreams();
573 if(opts
.getEarlyExit() && opts
.wasSetByUserEarlyExit()) {
576 #else /* CVC4_DEBUG */
577 if(opts
.getEarlyExit()) {
580 #endif /* CVC4_DEBUG */
583 // On exceptional exit, these are leaked, but that's okay... they
584 // need to be around in that case for main() to print statistics.