OpenASIP 2.2
Loading...
Searching...
No Matches
TTASim.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 TTASim.cc
26 *
27 * Implementation of ttasim.
28 *
29 * The command line version of the TTA Simulator / Debugger
30 *
31 * @author Pekka Jääskeläinen 2005-2012 (pjaaskel-no.spam-cs.tut.fi)
32 * @note rating: red
33 */
34
35#include <string>
36#include <iostream>
37#include <boost/shared_ptr.hpp>
38
40#include "SimulatorFrontend.hh"
41#include "FileSystem.hh"
43#include "SimulatorCLI.hh"
44#include "SimulatorToolbox.hh"
45
46/**
47 * A handler class for Ctrl-c signal.
48 *
49 * Stops the simulation (if it's running).
50 */
52public:
53 /**
54 * Constructor.
55 *
56 * @param target The target SimulatorFrontend instance.
57 */
59 }
60
61 /**
62 * Stops the simulation.
63 */
64 virtual void execute(int /*data*/, siginfo_t* /*info*/) {
66 }
67private:
68 /// Simulator frontend to use when stopping the simulation.
70};
71
72/**
73 * A handler class for SIGFPE signal
74 *
75 * Stops the simulation (if it's running). Used for catching
76 * errors from the simulated program in the compiled simulation
77 * engine.
78 */
80public:
81 /**
82 * Constructor.
83 *
84 * @param target The target SimulatorFrontend instance.
85 */
87 }
88
89 /**
90 * Terminates the simulation.
91 *
92 * @exception SimulationExecutionError thrown always
93 */
94 virtual void execute(int, siginfo_t *info) {
95 std::string msg("Unknown floating point exception");
96
97 if (info->si_code == FPE_INTDIV) {
98 msg = "integer division by zero";
99 } else if (info->si_code == FPE_FLTDIV) {
100 msg = "floating-point division by zero";
101 } else if (info->si_code == FPE_INTOVF) {
102 msg = "integer overflow";
103 } else if (info->si_code == FPE_FLTOVF) {
104 msg = "floating-point overflow";
105 } else if (info->si_code == FPE_FLTUND) {
106 msg = "floating-point underflow";
107 } else if (info->si_code == FPE_FLTRES) {
108 msg = "floating-point inexact result";
109 } else if (info->si_code == FPE_FLTINV) {
110 msg = "invalid floating-point operation";
111 } else if (info->si_code == FPE_FLTSUB) {
112 msg = " Subscript out of range";
113 }
114
118
119 throw SimulationExecutionError(__FILE__, __LINE__, __FUNCTION__, msg);
120 }
121private:
122 /// Simulator frontend to use when stopping the simulation.
124};
125
126/**
127 * A handler class for SIGSEGV signal
128 *
129 * Stops the simulation (if it's running). Used for catching
130 * errors from the simulated program in the compiled simulation
131 * engine.
132 */
134public:
135 /**
136 * Constructor.
137 *
138 * @param target The target SimulatorFrontend instance.
139 */
141 }
142
143 /**
144 * Terminates the simulation.
145 *
146 * @exception SimulationExecutionError thrown always
147 */
148 virtual void execute(int, siginfo_t*) {
149 std::string msg("Invalid memory reference");
150
154
155 throw SimulationExecutionError(__FILE__, __LINE__, __FUNCTION__, msg);
156 }
157private:
158 /// Simulator frontend to use when stopping the simulation.
160};
161
162/**
163 * Main function.
164 *
165 * Parses the command line and figures out whether to start the interactive
166 * debugging mode or not.
167 *
168 * @param argc The command line argument count.
169 * @param argv The command line arguments (passed to the interpreter).
170 * @return The return status.
171 */
172int main(int argc, char* argv[]) {
173
174 Application::initialize(argc, argv);
175
176 boost::shared_ptr<SimulatorFrontend> simFront;
177 /// @todo: read command line options and initialize the
178 /// simulator (frontend) using the given values.
180 try {
181 options->parse(argv, argc);
183 } catch (ParserStopRequest const&) {
184 return EXIT_SUCCESS;
185 } catch (const IllegalCommandLine& i) {
186 std::cerr << i.errorMessage() << std::endl;
187 return EXIT_FAILURE;
188 }
189
190 simFront.reset(new SimulatorFrontend(options->backendType()));
191
192 SimulatorCLI* cli = new SimulatorCLI(*simFront);
193
194 if (options->debugMode()) {
195 // handler for catching ctrl-c from the user (stops simulation)
196 SigINTHandler* ctrlcHandler = new SigINTHandler(*simFront);
197 Application::setSignalHandler(SIGINT, *ctrlcHandler);
198
199 if (simFront->isCompiledSimulation()) {
200
201 /* Catch errors caused by the simulated program
202 in compiled simulation these show up as normal
203 signals as the simulation code is native code we are
204 running in the simulation process. */
205 SigFPEHandler fpeHandler(*simFront);
206 SigSegvHandler segvHandler(*simFront);
207 Application::setSignalHandler(SIGFPE, fpeHandler);
208 Application::setSignalHandler(SIGSEGV, segvHandler);
209 }
210 }
211
212 // check if there is an initialization file in user's home dir and
213 // execute it
214 const std::string personalInitScript =
216 if (FileSystem::fileExists(personalInitScript)) {
217 cli->interpreter().processScriptFile(personalInitScript);
218 }
219
220 std::string machineToLoad = options->machineFile();
221 std::string programToLoad = options->programFile();
222 const std::string scriptString = options->scriptString();
223
224 if (options->numberOfArguments() != 0) {
226 Texts::TXT_ILLEGAL_ARGUMENTS).str() << std::endl;
227 return EXIT_FAILURE;
228 }
229
230 // check if there is an initialization file in the current dir and
231 // execute it
232 const std::string currentDirInitScript =
234 if (FileSystem::fileExists(currentDirInitScript)) {
235 cli->interpreter().processScriptFile(currentDirInitScript);
236 }
237
238 if (machineToLoad != "") {
239 cli->interpreteAndPrintResults(std::string("mach " ) + machineToLoad);
240 }
241
242 if (programToLoad != "") {
243 cli->interpreteAndPrintResults(std::string("prog " ) + programToLoad);
244 if (scriptString == "") {
245 // by default, if program is given, start simulation immediately
246 cli->interpreteAndPrintResults("run");
247 }
248 }
249
250 if (scriptString != "") {
251 cli->interpreteAndPrintResults(scriptString);
252 }
253
254
255 if (options->debugMode()) {
256 cli->run();
258 if (simFront->isCompiledSimulation()) {
261 }
262 }
263 delete cli;
264 return EXIT_SUCCESS;
265}
static MachInfoCmdLineOptions options
Definition MachInfo.cc:46
@ SRE_USER_REQUESTED
User requested the simulation to stop explicitly, e.g., by pressing ctrl-c in the CLI.
@ SRE_RUNTIME_ERROR
A fatal runtime error occured in the simulated program.
#define SIM_INIT_FILE_NAME
The initialization script file name.
int main(int argc, char *argv[])
Definition TTASim.cc:172
static void setCmdLineOptions(CmdLineOptions *options_)
static void setSignalHandler(int signalNum, UnixSignalHandler &handler)
static void restoreSignalHandler(int signalNum)
static void initialize()
void parse(char *argv[], int argc)
virtual int numberOfArguments() const
std::string errorMessage() const
Definition Exception.cc:123
static std::string homeDirectory()
static std::string currentWorkingDir()
static bool fileExists(const std::string fileName)
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition TTASim.cc:123
virtual void execute(int, siginfo_t *info)
Definition TTASim.cc:94
SigFPEHandler(SimulatorFrontend &target)
Definition TTASim.cc:86
virtual void execute(int, siginfo_t *)
Definition TTASim.cc:64
SigINTHandler(SimulatorFrontend &target)
Definition TTASim.cc:58
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition TTASim.cc:69
SigSegvHandler(SimulatorFrontend &target)
Definition TTASim.cc:140
SimulatorFrontend & target_
Simulator frontend to use when stopping the simulation.
Definition TTASim.cc:159
virtual void execute(int, siginfo_t *)
Definition TTASim.cc:148
virtual void run()
virtual void interpreteAndPrintResults(const TCEString &scriptString)
SimulatorInterpreter & interpreter()
void reportSimulatedProgramError(RuntimeErrorSeverity severity, const std::string &description)
@ RES_FATAL
Fatal runtime error, there is a serious error in the simulated program, thus it makes no sense to go ...
void prepareToStop(StopReason reason)
static SimulatorTextGenerator & textGenerator()
virtual bool processScriptFile(const std::string &scriptFileName)
virtual boost::format text(int textId)
@ TXT_ILLEGAL_ARGUMENTS