OpenASIP 2.2
Loading...
Searching...
No Matches
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"
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 */
54public:
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 */
68
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
111private:
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()) {
141 *context_, *reader_);
142 } else {
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 */
175void
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 */
188void
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}
#define assert(condition)
UInt32 InstructionAddress
Definition BaseType.hh:175
#define SIM_INTERP_QUIT_COMMAND
The command used to quit the command line interface.
#define SIM_COMMAND_PROMPT
The command prompt of the simulator interpreter.
CycleCount ClockCycleCount
Alias for ClockCycleCount.
#define SIM_DEFAULT_COMMAND_LOG
The default command log file name.
@ SRE_RUNTIME_ERROR
A fatal runtime error occured in the simulated program.
static CmdLineOptions * cmdLineOptions()
static char ** argv()
static int argc()
virtual bool unregisterListener(int event, Listener *listener)
Definition Informer.cc:104
virtual bool registerListener(int event, Listener *listener)
Definition Informer.cc:87
virtual void setInputHistoryLog(const std::string &historyFilename)
virtual void initialize(std::string defPrompt="", FILE *in=stdin, FILE *out=stdout, FILE *err=stderr)=0
virtual std::string readLine(std::string prompt="")=0
SimulatorFrontend & target_
Simulator frontend to use.
RuntimeErrorReporter(SimulatorFrontend &target)
virtual ~RuntimeErrorReporter()
virtual void handleEvent()
virtual std::string result()
@ SE_RUNTIME_ERROR
Sent when a runtime error is detected in the simulated program.
SimulatorInterpreter * interpreter_
virtual ~SimulatorCLI()
RuntimeErrorReporter * errorReporter_
virtual void run()
SimulatorCLI()=delete
SimulatorCmdLineOptions * options_
SimulatorInterpreterContext * context_
LineReader * reader_
SimulatorFrontend & simFront_
virtual void interpreteAndPrintResults(const TCEString &scriptString)
SimulationEventHandler & eventHandler()
ClockCycleCount cycleCount() const
virtual InstructionAddress lastExecutedInstruction(int coreId=-1) const
std::size_t programErrorReportCount(RuntimeErrorSeverity severity)
@ RES_FATAL
Fatal runtime error, there is a serious error in the simulated program, thus it makes no sense to go ...
@ RES_MINOR
Minor runtime error, no abort necessary.
InstructionAddress programCounter() const
bool isCompiledSimulation() const
std::string programErrorReport(RuntimeErrorSeverity severity, std::size_t index)
void prepareToStop(StopReason reason)
static std::string trim(const std::string &source)
virtual bool interpret(const std::string &commandLine)