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-2018 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 // This must come before PORTFOLIO_BUILD.
27 #include "cvc4autoconfig.h"
29 #include "api/cvc4cpp.h"
30 #include "base/configuration.h"
31 #include "base/output.h"
32 #include "expr/expr_iomanip.h"
33 #include "expr/expr_manager.h"
34 #include "main/command_executor.h"
36 #ifdef PORTFOLIO_BUILD
37 # include "main/command_executor_portfolio.h"
40 #include "main/interactive_shell.h"
41 #include "main/main.h"
42 #include "options/options.h"
43 #include "options/set_language.h"
44 #include "parser/parser.h"
45 #include "parser/parser_builder.h"
46 #include "parser/parser_exception.h"
47 #include "smt/command.h"
48 #include "util/result.h"
49 #include "util/statistics_registry.h"
53 using namespace CVC4::parser
;
54 using namespace CVC4::main
;
58 /** Global options variable */
59 thread_local Options
* pOptions
;
64 /** Just the basename component of argv[0] */
65 const std::string
*progName
;
67 /** A pointer to the CommandExecutor (the signal handlers need it) */
68 CVC4::main::CommandExecutor
* pExecutor
= NULL
;
70 /** A pointer to the totalTime driver stat (the signal handlers need it) */
71 CVC4::TimerStat
* pTotalTime
= NULL
;
73 }/* CVC4::main namespace */
77 void printUsage(Options
& opts
, bool full
) {
79 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
81 << "Without an input file, or with `-', CVC4 reads from standard input."
83 << "CVC4 options:" << endl
;
85 Options::printUsage( ss
.str(), *(opts
.getOut()) );
87 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
91 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
94 pTotalTime
= new TimerStat("totalTime");
97 // For the signal handlers' benefit
100 // Initialize the signal handlers
106 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
108 # ifndef PORTFOLIO_BUILD
109 if( opts
.wasSetByUserThreads() ||
110 opts
.wasSetByUserThreadStackSize() ||
111 (! opts
.getThreadArgv().empty()) ) {
112 throw OptionException("Thread options cannot be used with sequential CVC4. Please build and use the portfolio binary `pcvc4'.");
116 string progNameStr
= opts
.getBinaryName();
117 progName
= &progNameStr
;
119 if( opts
.getHelp() ) {
120 printUsage(opts
, true);
122 } else if( opts
.getLanguageHelp() ) {
123 Options::printLanguageHelp(*(opts
.getOut()));
125 } else if( opts
.getVersion() ) {
126 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
130 segvSpin
= opts
.getSegvSpin();
132 // If in competition mode, set output stream option to flush immediately
133 #ifdef CVC4_COMPETITION_MODE
134 *(opts
.getOut()) << unitbuf
;
135 #endif /* CVC4_COMPETITION_MODE */
137 // We only accept one input file
138 if(filenames
.size() > 1) {
139 throw Exception("Too many input files specified.");
142 // If no file supplied we will read from standard input
143 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
145 // if we're reading from stdin on a TTY, default to interactive mode
146 if(!opts
.wasSetByUserInteractive()) {
147 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
150 // Auto-detect input language by filename extension
151 std::string
filenameStr("<stdin>");
152 if (!inputFromStdin
) {
153 // Use swap to avoid copying the string
154 // TODO: use std::move() when switching to c++11
155 filenameStr
.swap(filenames
[0]);
157 const char* filename
= filenameStr
.c_str();
159 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
160 if( inputFromStdin
) {
161 // We can't do any fancy detection on stdin
162 opts
.setInputLanguage(language::input::LANG_CVC4
);
164 unsigned len
= filenameStr
.size();
165 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
166 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_6
);
167 } else if(len
>= 4 && !strcmp(".smt", filename
+ len
- 4)) {
168 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
169 } else if(len
>= 5 && !strcmp(".smt1", filename
+ len
- 5)) {
170 opts
.setInputLanguage(language::input::LANG_SMTLIB_V1
);
171 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
172 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
173 opts
.setInputLanguage(language::input::LANG_TPTP
);
174 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
175 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
176 opts
.setInputLanguage(language::input::LANG_CVC4
);
177 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
178 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
179 opts
.setInputLanguage(language::input::LANG_SYGUS
);
180 //since there is no sygus output language, set this to SMT lib 2
181 //opts.setOutputLanguage(language::output::LANG_SMTLIB_V2_0);
186 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
187 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
190 // Determine which messages to show based on smtcomp_mode and verbosity
191 if(Configuration::isMuzzledBuild()) {
192 DebugChannel
.setStream(&CVC4::null_os
);
193 TraceChannel
.setStream(&CVC4::null_os
);
194 NoticeChannel
.setStream(&CVC4::null_os
);
195 ChatChannel
.setStream(&CVC4::null_os
);
196 MessageChannel
.setStream(&CVC4::null_os
);
197 WarningChannel
.setStream(&CVC4::null_os
);
200 // important even for muzzled builds (to get result output right)
201 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
203 // Create the expression manager using appropriate options
204 std::unique_ptr
<api::Solver
> solver
;
205 # ifndef PORTFOLIO_BUILD
206 solver
.reset(new api::Solver(&opts
));
207 pExecutor
= new CommandExecutor(solver
.get(), opts
);
209 OptionsList threadOpts
;
210 parseThreadSpecificOptions(threadOpts
, opts
);
212 bool useParallelExecutor
= true;
214 if(opts
.wasSetByUserIncrementalSolving() &&
215 opts
.getIncrementalSolving() &&
216 (! opts
.getIncrementalParallel()) ) {
217 Notice() << "Notice: In --incremental mode, using the sequential solver"
218 << " unless forced by...\n"
219 << "Notice: ...the experimental --incremental-parallel option.\n";
220 useParallelExecutor
= false;
223 if(opts
.getCheckProofs()) {
224 if(opts
.getFallbackSequential()) {
225 Warning() << "Warning: Falling back to sequential mode, as cannot run"
226 << " portfolio in check-proofs mode.\n";
227 useParallelExecutor
= false;
230 throw OptionException("Cannot run portfolio in check-proofs mode.");
233 // pick appropriate one
234 if (useParallelExecutor
)
236 solver
.reset(&threadOpts
[0]);
237 pExecutor
= new CommandExecutorPortfolio(solver
.get(), opts
, threadOpts
);
242 pExecutor
= new CommandExecutor(solver
.get(), opts
);
246 std::unique_ptr
<Parser
> replayParser
;
247 if (opts
.getReplayInputFilename() != "")
249 std::string replayFilename
= opts
.getReplayInputFilename();
250 ParserBuilder
replayParserBuilder(solver
.get(), replayFilename
, opts
);
252 if( replayFilename
== "-") {
253 if( inputFromStdin
) {
254 throw OptionException("Replay file and input file can't both be stdin.");
256 replayParserBuilder
.withStreamInput(cin
);
258 replayParser
.reset(replayParserBuilder
.build());
259 pExecutor
->setReplayStream(new Parser::ExprStream(replayParser
.get()));
265 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
268 // Filename statistics
269 ReferenceStat
<std::string
> s_statFilename("filename", filenameStr
);
270 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
273 // Parse and execute commands until we are done
276 if(opts
.getInteractive() && inputFromStdin
) {
277 if(opts
.getTearDownIncremental() > 0) {
278 throw OptionException(
279 "--tear-down-incremental doesn't work in interactive mode");
281 #ifndef PORTFOLIO_BUILD
282 if(!opts
.wasSetByUserIncrementalSolving()) {
283 cmd
= new SetOptionCommand("incremental", SExpr(true));
285 pExecutor
->doCommand(cmd
);
288 #endif /* PORTFOLIO_BUILD */
289 InteractiveShell
shell(solver
.get());
290 if(opts
.getInteractivePrompt()) {
291 Message() << Configuration::getPackageName()
292 << " " << Configuration::getVersionString();
293 if(Configuration::isGitBuild()) {
294 Message() << " [" << Configuration::getGitId() << "]";
296 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
298 << (Configuration::isAssertionBuild() ? "on" : "off")
300 Message() << Configuration::copyright() << endl
;
303 // have the replay parser use the declarations input interactively
304 replayParser
->useDeclarationsFrom(shell
.getParser());
309 cmd
= shell
.readCommand();
310 } catch(UnsafeInterruptException
& e
) {
311 (*opts
.getOut()) << CommandInterrupted();
316 status
= pExecutor
->doCommand(cmd
) && status
;
317 if (cmd
->interrupted()) {
323 } else if( opts
.getTearDownIncremental() > 0) {
324 if(!opts
.getIncrementalSolving() && opts
.getTearDownIncremental() > 1) {
325 // For tear-down-incremental values greater than 1, need incremental
327 cmd
= new SetOptionCommand("incremental", SExpr(true));
329 pExecutor
->doCommand(cmd
);
331 // if(opts.wasSetByUserIncrementalSolving()) {
332 // throw OptionException(
333 // "--tear-down-incremental incompatible with --incremental");
336 // cmd = new SetOptionCommand("incremental", SExpr(false));
337 // cmd->setMuted(true);
338 // pExecutor->doCommand(cmd);
342 ParserBuilder
parserBuilder(solver
.get(), filename
, opts
);
344 if( inputFromStdin
) {
345 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
346 parserBuilder
.withStreamInput(cin
);
347 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
348 parserBuilder
.withLineBufferedStreamInput(cin
);
349 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
352 vector
< vector
<Command
*> > allCommands
;
353 allCommands
.push_back(vector
<Command
*>());
354 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
356 // have the replay parser use the file's declarations
357 replayParser
->useDeclarationsFrom(parser
.get());
360 // true if one of the commands was interrupted
361 bool interrupted
= false;
362 while (status
|| opts
.getContinuedExecution()) {
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;
515 while(status
|| opts
.getContinuedExecution()) {
517 (*opts
.getOut()) << CommandInterrupted();
522 cmd
= parser
->nextCommand();
523 if (cmd
== NULL
) break;
524 } catch (UnsafeInterruptException
& e
) {
529 status
= pExecutor
->doCommand(cmd
);
530 if (cmd
->interrupted() && status
== 0) {
535 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.