OpenASIP  2.0
SimulatorCLI.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2012 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file SimulatorCLI.cc
26  *
27  * Implementation of SimulatorCLI class
28  *
29  * @author Pekka Jääskeläinen 2012 (pjaaskel-no.spam-cs.tut.fi)
30  *
31  * @note rating: red
32  */
33 
34 #include "SimulatorCLI.hh"
35 #include "Application.hh"
36 #include "SimulatorFrontend.hh"
37 #include "SimulatorConstants.hh"
38 #include "Listener.hh"
41 #include "LineReaderFactory.hh"
42 #include "LineReader.hh"
44 #include "SimulatorInterpreter.hh"
46 #include "TCEString.hh"
47 #include "FileSystem.hh"
48 
49 /**
50  * Class that catches simulated program runtime error events and prints
51  * the error reports to stderr.
52  */
54 public:
55  /**
56  * Constructor.
57  *
58  * Registers itself to the SimulationEventHandler to listen to
59  * runtime error events.
60  *
61  * @param target The target SimulatorFrontend instance.
62  */
64  Listener(), target_(target) {
67  }
68 
72  }
73 
74  /**
75  * Handles the runtime error event by printing and error report to
76  * stderr and stopping simulation in case it's a fatal error.
77  */
78  virtual void handleEvent() {
79  size_t minorErrors = target_.programErrorReportCount(
81  size_t fatalErrors = target_.programErrorReportCount(
85  ClockCycleCount cycleCount = target_.cycleCount();
86 
87  if (minorErrors > 0) {
88  for (size_t i = 0; i < minorErrors; ++i) {
89  std::cerr << "warning: runtime error: "
92  << std::endl;
93  }
94  }
95 
96  if (fatalErrors > 0) {
97  for (size_t i = 0; i < fatalErrors; ++i) {
98  std::cerr << "error: runtime error: "
101  << std::endl;
102  }
104  }
105  std::cerr
106  << "Current PC: " << currentPC << " last PC: " << lastPC
107  << " cycle count: " << cycleCount << std::endl;
109  }
110 
111 private:
112  /// Simulator frontend to use.
114 };
115 
116 /**
117  *
118  * @param simulatorInterpreter Use the given interpreter instead of default one.
119  * Takes ownership of the object.
120  */
122  simFront_(frontend),
123  context_(new SimulatorInterpreterContext(frontend)),
124  reader_(LineReaderFactory::lineReader()) {
125 
126  assert(reader_ != NULL);
127 
130 
131  options_ = NULL;
132  if (Application::cmdLineOptions() != NULL) {
133  options_ = dynamic_cast<SimulatorCmdLineOptions*>(
135  }
136 
137  if (frontend.isCompiledSimulation()) {
138  interpreter_ =
141  *context_, *reader_);
142  } else {
143  interpreter_ =
146  *context_, *reader_);
147  }
148 
149  /// Catch runtime errors and print them out to the simulator console.
151 }
152 
154 
155  delete reader_;
156  reader_ = NULL;
157 
158  delete interpreter_;
159  interpreter_ = NULL;
160 
161  delete context_;
162  context_ = NULL;
163 
164  delete errorReporter_;
165  errorReporter_ = NULL;
166 }
167 
168 /**
169  * Executes the given script string in the script interpreter and
170  * prints out possible results.
171  *
172  * @param interpreter Interpreter to use.
173  * @param scriptString Script string to execute.
174  */
175 void
177  interpreter_->interpret(scriptString);
178  if (interpreter_->result().size() > 0)
179  std::cout << interpreter_->result() << std::endl;
180 }
181 
182 /**
183  * Runs the interpreter loop.
184  *
185  * Inputs a line of script from the user, processes it and prints out the
186  * results.
187  */
188 void
190 
191  while (!interpreter_->isQuitCommandGiven()) {
192  std::string command = "";
193  try {
194  command = reader_->readLine();
195  } catch (const EndOfFile&) {
196  // execute the actual interpreter quit command in case user
197  // pressed ctrl-d or the input file ended, to make sure
198  // all uninitialization routines are executed correctly
200  std::cout << interpreter_->result() << std::endl;
201  break;
202  }
203  command = StringTools::trim(command);
204  if (command == "") {
205  continue;
206  }
207  interpreter_->interpret(command);
208  if (interpreter_->result().size() > 0)
209  std::cout << interpreter_->result() << std::endl;
210  }
211 }
CompiledSimInterpreter
Definition: CompiledSimInterpreter.hh:42
SimulatorInterpreterContext
Definition: SimulatorInterpreterContext.hh:45
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
RuntimeErrorReporter::~RuntimeErrorReporter
virtual ~RuntimeErrorReporter()
Definition: SimulatorCLI.cc:69
FileSystem.hh
SimulatorInterpreterContext.hh
ScriptInterpreter::result
virtual std::string result()
Definition: ScriptInterpreter.cc:191
SimulatorCLI::interpreter_
SimulatorInterpreter * interpreter_
Definition: SimulatorCLI.hh:63
RuntimeErrorReporter::RuntimeErrorReporter
RuntimeErrorReporter(SimulatorFrontend &target)
Definition: SimulatorCLI.cc:63
Listener
Definition: Listener.hh:40
SimulatorCmdLineOptions
Definition: SimulatorCmdLineOptions.hh:45
LineReaderFactory
Definition: LineReaderFactory.hh:43
LineReader::setInputHistoryLog
virtual void setInputHistoryLog(const std::string &historyFilename)
Definition: LineReader.cc:124
SimulatorInterpreter::isQuitCommandGiven
bool isQuitCommandGiven() const
Definition: SimulatorInterpreter.cc:134
SimulatorCLI::options_
SimulatorCmdLineOptions * options_
Definition: SimulatorCLI.hh:64
LineReaderFactory.hh
CompiledSimInterpreter.hh
SimulatorCLI::simFront_
SimulatorFrontend & simFront_
Definition: SimulatorCLI.hh:60
LineReader::readLine
virtual std::string readLine(std::string prompt="")=0
SimulatorConstants.hh
SIM_COMMAND_PROMPT
#define SIM_COMMAND_PROMPT
The command prompt of the simulator interpreter.
Definition: SimulatorConstants.hh:42
TCEString.hh
assert
#define assert(condition)
Definition: Application.hh:86
SimulatorFrontend::programCounter
InstructionAddress programCounter() const
Definition: SimulatorFrontend.cc:1169
SimulationEventHandler.hh
Application::argc
static int argc()
Definition: Application.hh:192
SimulatorInterpreter
Definition: SimulatorInterpreter.hh:49
SIM_INTERP_QUIT_COMMAND
#define SIM_INTERP_QUIT_COMMAND
The command used to quit the command line interface.
Definition: SimulatorConstants.hh:45
SimulatorCmdLineOptions.hh
Informer::unregisterListener
virtual bool unregisterListener(int event, Listener *listener)
Definition: Informer.cc:104
SimulatorCLI::run
virtual void run()
Definition: SimulatorCLI.cc:189
RuntimeErrorReporter::handleEvent
virtual void handleEvent()
Definition: SimulatorCLI.cc:78
LineReader.hh
Application.hh
Application::cmdLineOptions
static CmdLineOptions * cmdLineOptions()
Definition: Application.cc:397
SimulatorFrontend::programErrorReport
std::string programErrorReport(RuntimeErrorSeverity severity, std::size_t index)
Definition: SimulatorFrontend.cc:2321
Informer::registerListener
virtual bool registerListener(int event, Listener *listener)
Definition: Informer.cc:87
TclInterpreter::interpret
virtual bool interpret(const std::string &commandLine)
Definition: TclInterpreter.cc:138
SRE_RUNTIME_ERROR
@ SRE_RUNTIME_ERROR
A fatal runtime error occured in the simulated program.
Definition: SimulatorConstants.hh:68
SimulatorFrontend::RES_MINOR
@ RES_MINOR
Minor runtime error, no abort necessary.
Definition: SimulatorFrontend.hh:94
SimulatorFrontend::RES_FATAL
@ RES_FATAL
Fatal runtime error, there is a serious error in the simulated program, thus it makes no sense to go ...
Definition: SimulatorFrontend.hh:95
SimulatorCLI::reader_
LineReader * reader_
Definition: SimulatorCLI.hh:62
SimulationEventHandler::SE_RUNTIME_ERROR
@ SE_RUNTIME_ERROR
Sent when a runtime error is detected in the simulated program.
Definition: SimulationEventHandler.hh:52
SimulatorFrontend::isCompiledSimulation
bool isCompiledSimulation() const
Definition: SimulatorFrontend.cc:1295
SimulatorFrontend::lastExecutedInstruction
virtual InstructionAddress lastExecutedInstruction(int coreId=-1) const
Definition: SimulatorFrontend.cc:1182
SimulatorFrontend::programErrorReportCount
std::size_t programErrorReportCount(RuntimeErrorSeverity severity)
Definition: SimulatorFrontend.cc:2344
SimulatorFrontend.hh
SimulatorCLI::~SimulatorCLI
virtual ~SimulatorCLI()
Definition: SimulatorCLI.cc:153
StringTools::trim
static std::string trim(const std::string &source)
Definition: StringTools.cc:55
SimulatorFrontend::clearProgramErrorReports
void clearProgramErrorReports()
Definition: SimulatorFrontend.cc:2360
SimulatorCLI::errorReporter_
RuntimeErrorReporter * errorReporter_
Definition: SimulatorCLI.hh:65
Application::argv
static char ** argv()
Definition: Application.hh:193
SimulatorCLI.hh
SimulatorFrontend::cycleCount
ClockCycleCount cycleCount() const
Definition: SimulatorFrontend.cc:1194
SimulatorFrontend::eventHandler
SimulationEventHandler & eventHandler()
Definition: SimulatorFrontend.cc:2260
TCEString
Definition: TCEString.hh:53
LineReader::initialize
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)=0
SimulatorCLI::context_
SimulatorInterpreterContext * context_
Definition: SimulatorCLI.hh:61
SimulatorInterpreter.hh
RuntimeErrorReporter::target_
SimulatorFrontend & target_
Simulator frontend to use.
Definition: SimulatorCLI.cc:113
ClockCycleCount
CycleCount ClockCycleCount
Alias for ClockCycleCount.
Definition: SimulatorConstants.hh:57
RuntimeErrorReporter
Definition: SimulatorCLI.cc:53
SimulatorCLI::SimulatorCLI
SimulatorCLI()=delete
EndOfFile
Definition: Exception.hh:189
SIM_DEFAULT_COMMAND_LOG
#define SIM_DEFAULT_COMMAND_LOG
The default command log file name.
Definition: SimulatorConstants.hh:51
SimulatorFrontend
Definition: SimulatorFrontend.hh:89
SimulatorCLI::interpreteAndPrintResults
virtual void interpreteAndPrintResults(const TCEString &scriptString)
Definition: SimulatorCLI.cc:176
SimulatorFrontend::prepareToStop
void prepareToStop(StopReason reason)
Definition: SimulatorFrontend.cc:1331
Listener.hh