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)
25 #include "cvc4autoconfig.h"
27 #include "api/cvc4cpp.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"
33 #include "main/interactive_shell.h"
34 #include "main/main.h"
35 #include "options/options.h"
36 #include "options/set_language.h"
37 #include "parser/parser.h"
38 #include "parser/parser_builder.h"
39 #include "parser/parser_exception.h"
40 #include "smt/command.h"
41 #include "util/result.h"
42 #include "util/statistics_registry.h"
46 using namespace CVC4::parser
;
47 using namespace CVC4::main
;
51 /** Global options variable */
52 thread_local Options
* pOptions
;
57 /** Just the basename component of argv[0] */
58 const std::string
*progName
;
60 /** A pointer to the CommandExecutor (the signal handlers need it) */
61 CVC4::main::CommandExecutor
* pExecutor
= NULL
;
63 /** A pointer to the totalTime driver stat (the signal handlers need it) */
64 CVC4::TimerStat
* pTotalTime
= NULL
;
66 }/* CVC4::main namespace */
70 void printUsage(Options
& opts
, bool full
) {
72 ss
<< "usage: " << opts
.getBinaryName() << " [options] [input-file]"
74 << "Without an input file, or with `-', CVC4 reads from standard input."
76 << "CVC4 options:" << endl
;
78 Options::printUsage( ss
.str(), *(opts
.getOut()) );
80 Options::printShortUsage( ss
.str(), *(opts
.getOut()) );
84 int runCvc4(int argc
, char* argv
[], Options
& opts
) {
87 pTotalTime
= new TimerStat("totalTime");
90 // For the signal handlers' benefit
93 // Initialize the signal handlers
99 vector
<string
> filenames
= Options::parseOptions(&opts
, argc
, argv
);
101 string progNameStr
= opts
.getBinaryName();
102 progName
= &progNameStr
;
104 if( opts
.getHelp() ) {
105 printUsage(opts
, true);
107 } else if( opts
.getLanguageHelp() ) {
108 Options::printLanguageHelp(*(opts
.getOut()));
110 } else if( opts
.getVersion() ) {
111 *(opts
.getOut()) << Configuration::about().c_str() << flush
;
115 segvSpin
= opts
.getSegvSpin();
117 // If in competition mode, set output stream option to flush immediately
118 #ifdef CVC4_COMPETITION_MODE
119 *(opts
.getOut()) << unitbuf
;
120 #endif /* CVC4_COMPETITION_MODE */
122 // We only accept one input file
123 if(filenames
.size() > 1) {
124 throw Exception("Too many input files specified.");
127 // If no file supplied we will read from standard input
128 const bool inputFromStdin
= filenames
.empty() || filenames
[0] == "-";
130 // if we're reading from stdin on a TTY, default to interactive mode
131 if(!opts
.wasSetByUserInteractive()) {
132 opts
.setInteractive(inputFromStdin
&& isatty(fileno(stdin
)));
135 // Auto-detect input language by filename extension
136 std::string
filenameStr("<stdin>");
137 if (!inputFromStdin
) {
138 // Use swap to avoid copying the string
139 // TODO: use std::move() when switching to c++11
140 filenameStr
.swap(filenames
[0]);
142 const char* filename
= filenameStr
.c_str();
144 if(opts
.getInputLanguage() == language::input::LANG_AUTO
) {
145 if( inputFromStdin
) {
146 // We can't do any fancy detection on stdin
147 opts
.setInputLanguage(language::input::LANG_CVC4
);
149 unsigned len
= filenameStr
.size();
150 if(len
>= 5 && !strcmp(".smt2", filename
+ len
- 5)) {
151 opts
.setInputLanguage(language::input::LANG_SMTLIB_V2_6
);
152 } else if((len
>= 2 && !strcmp(".p", filename
+ len
- 2))
153 || (len
>= 5 && !strcmp(".tptp", filename
+ len
- 5))) {
154 opts
.setInputLanguage(language::input::LANG_TPTP
);
155 } else if(( len
>= 4 && !strcmp(".cvc", filename
+ len
- 4) )
156 || ( len
>= 5 && !strcmp(".cvc4", filename
+ len
- 5) )) {
157 opts
.setInputLanguage(language::input::LANG_CVC4
);
158 } else if((len
>= 3 && !strcmp(".sy", filename
+ len
- 3))
159 || (len
>= 3 && !strcmp(".sl", filename
+ len
- 3))) {
160 opts
.setInputLanguage(language::input::LANG_SYGUS
);
161 //since there is no sygus output language, set this to SMT lib 2
162 //opts.setOutputLanguage(language::output::LANG_SMTLIB_V2_0);
167 if(opts
.getOutputLanguage() == language::output::LANG_AUTO
) {
168 opts
.setOutputLanguage(language::toOutputLanguage(opts
.getInputLanguage()));
171 // Determine which messages to show based on smtcomp_mode and verbosity
172 if(Configuration::isMuzzledBuild()) {
173 DebugChannel
.setStream(&CVC4::null_os
);
174 TraceChannel
.setStream(&CVC4::null_os
);
175 NoticeChannel
.setStream(&CVC4::null_os
);
176 ChatChannel
.setStream(&CVC4::null_os
);
177 MessageChannel
.setStream(&CVC4::null_os
);
178 WarningChannel
.setStream(&CVC4::null_os
);
181 // important even for muzzled builds (to get result output right)
182 (*(opts
.getOut())) << language::SetLanguage(opts
.getOutputLanguage());
184 // Create the command executor to execute the parsed commands
185 pExecutor
= new CommandExecutor(opts
);
190 RegisterStatistic
statTotalTime(&pExecutor
->getStatisticsRegistry(),
193 // Filename statistics
194 ReferenceStat
<std::string
> s_statFilename("filename", filenameStr
);
195 RegisterStatistic
statFilenameReg(&pExecutor
->getStatisticsRegistry(),
197 // set filename in smt engine
198 pExecutor
->getSmtEngine()->setFilename(filenameStr
);
200 // Parse and execute commands until we are done
203 if(opts
.getInteractive() && inputFromStdin
) {
204 if(opts
.getTearDownIncremental() > 0) {
205 throw OptionException(
206 "--tear-down-incremental doesn't work in interactive mode");
208 if(!opts
.wasSetByUserIncrementalSolving()) {
209 cmd
= new SetOptionCommand("incremental", SExpr(true));
211 pExecutor
->doCommand(cmd
);
214 InteractiveShell
shell(pExecutor
->getSolver());
215 if(opts
.getInteractivePrompt()) {
216 Message() << Configuration::getPackageName()
217 << " " << Configuration::getVersionString();
218 if(Configuration::isGitBuild()) {
219 Message() << " [" << Configuration::getGitId() << "]";
221 Message() << (Configuration::isDebugBuild() ? " DEBUG" : "")
223 << (Configuration::isAssertionBuild() ? "on" : "off")
225 Message() << Configuration::copyright() << endl
;
230 cmd
= shell
.readCommand();
231 } catch(UnsafeInterruptException
& e
) {
232 (*opts
.getOut()) << CommandInterrupted();
237 status
= pExecutor
->doCommand(cmd
) && status
;
238 if (cmd
->interrupted()) {
244 } else if( opts
.getTearDownIncremental() > 0) {
245 if(!opts
.getIncrementalSolving() && opts
.getTearDownIncremental() > 1) {
246 // For tear-down-incremental values greater than 1, need incremental
248 cmd
= new SetOptionCommand("incremental", SExpr(true));
250 pExecutor
->doCommand(cmd
);
252 // if(opts.wasSetByUserIncrementalSolving()) {
253 // throw OptionException(
254 // "--tear-down-incremental incompatible with --incremental");
257 // cmd = new SetOptionCommand("incremental", SExpr(false));
258 // cmd->setMuted(true);
259 // pExecutor->doCommand(cmd);
263 ParserBuilder
parserBuilder(pExecutor
->getSolver(), filename
, opts
);
265 if( inputFromStdin
) {
266 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
267 parserBuilder
.withStreamInput(cin
);
268 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
269 parserBuilder
.withLineBufferedStreamInput(cin
);
270 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
273 vector
< vector
<Command
*> > allCommands
;
274 allCommands
.push_back(vector
<Command
*>());
275 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
277 // true if one of the commands was interrupted
278 bool interrupted
= false;
282 (*opts
.getOut()) << CommandInterrupted();
287 cmd
= parser
->nextCommand();
288 if (cmd
== NULL
) break;
289 } catch (UnsafeInterruptException
& e
) {
294 if(dynamic_cast<PushCommand
*>(cmd
) != NULL
) {
295 if(needReset
>= opts
.getTearDownIncremental()) {
297 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
298 if (interrupted
) break;
299 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
301 Command
* ccmd
= allCommands
[i
][j
]->clone();
302 ccmd
->setMuted(true);
303 pExecutor
->doCommand(ccmd
);
304 if (ccmd
->interrupted())
313 allCommands
.push_back(vector
<Command
*>());
314 Command
* copy
= cmd
->clone();
315 allCommands
.back().push_back(copy
);
316 status
= pExecutor
->doCommand(cmd
);
317 if(cmd
->interrupted()) {
321 } else if(dynamic_cast<PopCommand
*>(cmd
) != NULL
) {
322 allCommands
.pop_back(); // fixme leaks cmds here
323 if (needReset
>= opts
.getTearDownIncremental()) {
325 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
326 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
328 Command
* ccmd
= allCommands
[i
][j
]->clone();
329 ccmd
->setMuted(true);
330 pExecutor
->doCommand(ccmd
);
331 if (ccmd
->interrupted())
338 if (interrupted
) continue;
339 (*opts
.getOut()) << CommandSuccess();
342 status
= pExecutor
->doCommand(cmd
);
343 if(cmd
->interrupted()) {
348 } else if(dynamic_cast<CheckSatCommand
*>(cmd
) != NULL
||
349 dynamic_cast<QueryCommand
*>(cmd
) != NULL
) {
350 if(needReset
>= opts
.getTearDownIncremental()) {
352 for(size_t i
= 0; i
< allCommands
.size() && !interrupted
; ++i
) {
353 for(size_t j
= 0; j
< allCommands
[i
].size() && !interrupted
; ++j
)
355 Command
* ccmd
= allCommands
[i
][j
]->clone();
356 ccmd
->setMuted(true);
357 pExecutor
->doCommand(ccmd
);
358 if (ccmd
->interrupted())
373 status
= pExecutor
->doCommand(cmd
);
374 if(cmd
->interrupted()) {
378 } else if(dynamic_cast<ResetCommand
*>(cmd
) != NULL
) {
379 pExecutor
->doCommand(cmd
);
381 allCommands
.push_back(vector
<Command
*>());
383 // We shouldn't copy certain commands, because they can cause
384 // an error on replay since there's no associated sat/unsat check
386 if(dynamic_cast<GetUnsatCoreCommand
*>(cmd
) == NULL
&&
387 dynamic_cast<GetProofCommand
*>(cmd
) == NULL
&&
388 dynamic_cast<GetValueCommand
*>(cmd
) == NULL
&&
389 dynamic_cast<GetModelCommand
*>(cmd
) == NULL
&&
390 dynamic_cast<GetAssignmentCommand
*>(cmd
) == NULL
&&
391 dynamic_cast<GetInstantiationsCommand
*>(cmd
) == NULL
&&
392 dynamic_cast<GetAssertionsCommand
*>(cmd
) == NULL
&&
393 dynamic_cast<GetInfoCommand
*>(cmd
) == NULL
&&
394 dynamic_cast<GetOptionCommand
*>(cmd
) == NULL
&&
395 dynamic_cast<EchoCommand
*>(cmd
) == NULL
) {
396 Command
* copy
= cmd
->clone();
397 allCommands
.back().push_back(copy
);
399 status
= pExecutor
->doCommand(cmd
);
400 if(cmd
->interrupted()) {
405 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
413 if(!opts
.wasSetByUserIncrementalSolving()) {
414 cmd
= new SetOptionCommand("incremental", SExpr(false));
416 pExecutor
->doCommand(cmd
);
420 ParserBuilder
parserBuilder(pExecutor
->getSolver(), filename
, opts
);
422 if( inputFromStdin
) {
423 #if defined(CVC4_COMPETITION_MODE) && !defined(CVC4_SMTCOMP_APPLICATION_TRACK)
424 parserBuilder
.withStreamInput(cin
);
425 #else /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
426 parserBuilder
.withLineBufferedStreamInput(cin
);
427 #endif /* CVC4_COMPETITION_MODE && !CVC4_SMTCOMP_APPLICATION_TRACK */
430 std::unique_ptr
<Parser
> parser(parserBuilder
.build());
431 bool interrupted
= false;
435 (*opts
.getOut()) << CommandInterrupted();
440 cmd
= parser
->nextCommand();
441 if (cmd
== NULL
) break;
442 } catch (UnsafeInterruptException
& e
) {
447 status
= pExecutor
->doCommand(cmd
);
448 if (cmd
->interrupted() && status
== 0) {
453 if(dynamic_cast<QuitCommand
*>(cmd
) != NULL
) {
463 result
= pExecutor
->getResult();
466 // there was some kind of error
470 #ifdef CVC4_COMPETITION_MODE
472 // exit, don't return (don't want destructors to run)
473 // _exit() from unistd.h doesn't run global destructors
474 // or other on_exit/atexit stuff.
476 #endif /* CVC4_COMPETITION_MODE */
478 ReferenceStat
< Result
> s_statSatResult("sat/unsat", result
);
479 RegisterStatistic
statSatResultReg(&pExecutor
->getStatisticsRegistry(),
484 // Tim: I think that following comment is out of date?
485 // Set the global executor pointer to NULL first. If we get a
486 // signal while dumping statistics, we don't want to try again.
487 pExecutor
->flushOutputStreams();
490 if(opts
.getEarlyExit() && opts
.wasSetByUserEarlyExit()) {
493 #else /* CVC4_DEBUG */
494 if(opts
.getEarlyExit()) {
497 #endif /* CVC4_DEBUG */
500 // On exceptional exit, these are leaked, but that's okay... they
501 // need to be around in that case for main() to print statistics.