OpenASIP 2.2
Loading...
Searching...
No Matches
CompiledSimController.cc
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2010 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 CompiledSimController.cc
26 *
27 * Definition of CompiledSimController class.
28 *
29 * @author Viljami Korhonen 2007 (viljami.korhonen-no.spam-tut.fi)
30 * @author Pekka Jääskeläinen 2010
31 * @note rating: red
32 */
33
34#include <cstdlib>
35
36#include "SimulatorFrontend.hh"
40#include "FileSystem.hh"
41#include "MemorySystem.hh"
42#include "SimulatorToolbox.hh"
43#include "CompiledSimulation.hh"
44#include "POMValidator.hh"
46#include "Instruction.hh"
47#include "MathTools.hh"
48#include "Program.hh"
50#include "Conversion.hh"
51#include "Machine.hh"
52
53using std::endl;
54using namespace TTAMachine;
55
56//#define DEBUG_COMPILED_SIMULATION
57
58/**
59 * The constructor
60 *
61 * @param frontend Simulator frontend
62 * @param machine The machine model
63 * @param program The program to be simulated
64 * @param leaveDirty Set to true in case the engine should not clean up the
65 * source files to the engine (in /tmp) after finishing,
66 * e.g., for debugging purposes.
67 */
70 const TTAProgram::Program& program, bool leaveDirty) :
72 pluginTools_(true, false), compiledSimulationPath_(""),
73 leaveDirty_(leaveDirty) {
74
75#ifdef DEBUG_COMPILED_SIMULATION
76 leaveDirty_ = true;
77#endif
78
79 // Make the symbols unique in case we use more than one
80 // simulator engines in the same process.
81 static int instanceCount = 0;
82 instanceId_ = instanceCount;
83
84 // Note, this is not thread safe. Thus, the engines should be
85 // initialized sequentially.
86 ++instanceCount;
87
88 reset();
89}
90
91/**
92 * The destructor
93 */
97
98/**
99 * Advances simulation by given amount of cycles
100 *
101 * @Note The accuracy of this function is one basic block!
102 */
103void
106 stopReasons_.clear();
108 stopRequested_ = false;
109
110 try {
111 simulation_->step(count);
112 } catch (const Exception& e) {
114 return;
115 } catch(...) {
117 return;
118 }
119
121
122 if (simulation_->isFinished()) {
124 } else {
126 }
127
130}
131
132/**
133 * Advances simulation by a given amout of steps and skip procedure calls.,
134 *
135 * @Note The accuracy of this function is one basic block!
136 */
137void
140 stopReasons_.clear();
142 stopRequested_ = false;
143
144 try {
145 simulation_->next(count);
146 } catch (const Exception& e) {
148 return;
149 } catch(...) {
151 return;
152 }
153
155
156 if (simulation_->isFinished()) {
158 } else {
160 }
161
164}
165
166/**
167 * Advances the simulation until the program ends
168 *
169 * @exception SimulationExecutionError If a runtime error occurs in
170 * the simulated program.
171 */
172void
174 stopRequested_ = false;
175 stopReasons_.clear();
177
178 // Run the program
179 try {
180 simulation_->run();
181 } catch (const Exception& e) {
183 return;
184 } catch(...) {
186 return;
187 }
188
189 if (simulation_->isFinished()) {
191 } else {
193 }
194
197}
198
199/**
200 * Advances the simulation until a given address is reached.
201 *
202 * @Note The accuracy of this function is one basic block!
203 *
204 * @param address the address after the simulation execution should end
205 * @exception SimulationExecutionError If a runtime error occurs in
206 * the simulated program.
207 */
208void
210 stopRequested_ = false;
211 stopReasons_.clear();
213 address = basicBlockStart(address); // find nearest bb start
214
215 try {
216 simulation_->runUntil(address);
217 } catch (const Exception& e) {
219 return;
220 } catch(...) {
222 return;
223 }
224
226
227 if (simulation_->isFinished()) {
229 } else {
231 }
232
235}
236
237/**
238 * Resets the simulation so it can be started from the beginning.
239 *
240 * Resets everything to initial values (program counter, clock cycles etc.)
241 */
242void
244
246 stopRequested_ = false;
247 clockCount_ = 0;
248
250
252 if (compiledSimulationPath_ == "") {
254 << "Cannot create temporary path "
255 << "for the generated simulation code!" << endl;
256 return;
257 }
258
259 // Generate all simulation code at once
260 CompiledSimCodeGenerator generator(
261 sourceMachine_, program_, *this,
267
269#ifdef DEBUG_COMPILED_SIMULATION
270 std::cerr << "Generated compiled simulation sources to: '"
271 << compiledSimulationPath_ << "'" << std::endl;
272#endif
273 basicBlocks_ = generator.basicBlocks();
275
276 CompiledSimCompiler compiler;
277
278 // Compile everything when using static compiled simulation
280 if (compiler.compileDirectory(compiledSimulationPath_, "", false)
281 != 0) {
282 Application::logStream() << "Compilation aborted." << endl;
283 return;
284 }
285 } else { // Compile main engine file
287 + FileSystem::DIRECTORY_SEPARATOR + "CompiledSimulationEngine.cc");
288
289 // Precompile the simulation header
291 + FileSystem::DIRECTORY_SEPARATOR + "CompiledSimulationEngine.hh",
292 "-xc++-header", ".gch");
293 }
294
295 SimulationGetterFunction* simulationGetter = NULL;
297
298 // register individual simulation functions
299 std::vector<std::string> sos;
300 FileSystem::globPath(compiledSimulationPath_ + "/simulate_*.so", sos);
301 for (std::size_t i = 0; i < sos.size(); ++i) {
302 const std::string& so = FileSystem::fileOfPath(sos.at(i));
304 }
305
306 // register simulation getter function symbol
307 pluginTools_.registerModule("CompiledSimulationEngine.so");
309 "getSimulation_" + Conversion::toString(instanceId_), simulationGetter);
310 simulation_.reset(
311 simulationGetter(sourceMachine_, program_.entryAddress().location(),
315
317}
318
319/**
320 * Returns the program counter value.
321 *
322 * @return Program counter value.
323 */
326 return simulation_->programCounter();
327}
328
329/**
330 * Returns the address of the last executed instruction.
331 *
332 * @return The address of the last executed instruction
333 */
336 return simulation_->lastExecutedInstruction();
337}
338
339/**
340 * Returns the count of clock cycles simulated.
341 *
342 * @return Count of simulated clock cycles.
343 */
346 if (simulation_) {
347 return simulation_->cycleCount();
348 } else {
349 return 0;
350 }
351}
352
353/**
354 * Returns a pointer to the current CompiledSimulation object
355 *
356 * @return A pointer to the current CompiledSimulation object
357 */
358boost::shared_ptr<CompiledSimulation>
362
363/**
364 * Will delete all generated code files if leaveDirty_ is not set
365 */
366void
377
378/**
379 * Returns the start of the basic block containing address
380 *
381 * @param address
382 * @return instruction address of the basic block start
383 */
386 return basicBlocks_.lower_bound(address)->second;
387}
388
389/**
390 * Returns the program model
391 * @return the program model
392 */
395 return program_;
396}
397
398/**
399 * Returns a string containing the value(s) of the register file
400 *
401 * @param rfName name of the register file to search for
402 * @param registerIndex index of the register. if -1, all registers are listed
403 * @return A string containing the value(s) of the register file
404 * @exception InstanceNotFound If the register cannot be found.
405 */
406std::string
408 const std::string& rfName, int registerIndex) {
409 std::string stringValue("");
410
411 if (registerIndex >= 0) {
412 stringValue += compiledSimulation()->
413 registerFileValue(rfName.c_str(), registerIndex).hexValue();
414 } else {
417 RegisterFile& rf = *navigator.item(rfName);
418
419 bool firstReg = true;
420 for (int i = 0; i < rf.numberOfRegisters(); ++i) {
421 if (!firstReg)
422 stringValue += "\n";
423 const std::string registerName =
424 rfName + "." + Conversion::toString(i);
425
426 SimValue value = compiledSimulation()->
427 registerFileValue(rfName.c_str(), i);
428 stringValue += registerName + " " + value.hexValue();
429
430 firstReg = false;
431 }
432 }
433
434 return stringValue;
435}
436
437/**
438 * Returns the current value of a IU register
439 *
440 * @param iuName name of the immediate unit
441 * @param index index of the register
442 * @return Current value of a IU register
443 */
446 const std::string& iuName,
447 int index) {
448 return compiledSimulation()->immediateUnitRegisterValue(iuName.c_str(),
449 index);
450}
451
452/**
453 * Returns a FU port's value
454 *
455 * @param fuName name of the function unit
456 * @param portIndex Index of the port
457 * @return A FU port's value
458 */
461 const std::string& fuName,
462 const std::string& portName) {
463 return compiledSimulation()->FUPortValue(fuName.c_str(), portName.c_str());
464}
465
466/**
467 * Sends a stop request to the simulation controller and compiled simulation
468 *
469 * @param reason The stop reason
470 */
471void
#define assert(condition)
#define CATCH_ANY(XXX__)
Word UIntWord
Definition BaseType.hh:144
UInt32 InstructionAddress
Definition BaseType.hh:175
TTAMachine::Machine * machine
the architecture definition of the estimated processor
find Finds info of the inner loops in the program
find Finds info of the inner loops in the false
CycleCount ClockCycleCount
Alias for ClockCycleCount.
StopReason
The reasons to stop simulation.
@ SRE_AFTER_UNTIL
Stopped after running to the wanted.
@ SRE_AFTER_STEPPING
Stopped after stepping the given count.
@ SRE_RUNTIME_ERROR
A fatal runtime error occured in the simulated program.
static std::ostream & logStream()
virtual AddressMap basicBlocks() const
virtual void generateToDirectory(const std::string &dirName)
virtual ProcedureBBRelations procedureBBRelations() const
int compileFile(const std::string &path, const std::string &flags="", const std::string &outputExtension=".o", bool verbose=false) const
int compileToSO(const std::string &path, const std::string &flags="", bool verbose=false) const
int compileDirectory(const std::string &dirName, const std::string &flags="", bool verbose=false) const
virtual void next(int count=1)
int instanceId_
The unique identifier for this simulation engine. Used for enabling multiple compiled engines in the ...
ProcedureBBRelations procedureBBRelations_
A struct for tracking basic blocks and their relation to their procedures.
boost::shared_ptr< CompiledSimulation > simulation_
Pointer to the loaded simulation.
virtual InstructionAddress lastExecutedInstruction() const
CompiledSimulation *() SimulationGetterFunction(const TTAMachine::Machine &machine, InstructionAddress entryAddress, InstructionAddress lastInstruction, SimulatorFrontend &frontend, CompiledSimController &controller, MemorySystem &memorySystem, bool dynamicCompilation, ProcedureBBRelations &procedureBBRelations)
Function type for the getSimulation() function.
virtual void step(double count=1)
virtual boost::shared_ptr< CompiledSimulation > compiledSimulation()
virtual SimValue FUPortValue(const std::string &fuName, const std::string &portName)
CompiledSimController(SimulatorFrontend &frontend, const TTAMachine::Machine &machine, const TTAProgram::Program &program, bool leaveDirty=false)
virtual void prepareToStop(StopReason reason)
bool leaveDirty_
True, if the simulation should leave all the generated code files.
virtual SimValue immediateUnitRegisterValue(const std::string &iuName, int index=-1)
const TTAProgram::Program & program() const
virtual ClockCycleCount clockCount() const
virtual std::string registerFileValue(const std::string &rfName, int registerIndex=-1)
virtual InstructionAddress programCounter() const
InstructionAddress basicBlockStart(InstructionAddress address) const
virtual void runUntil(UIntWord address)
std::string compiledSimulationPath_
Path to the generated simulation files.
PluginTools pluginTools_
Used for loading the compiled simulation plugin.
CompiledSimCodeGenerator::AddressMap basicBlocks_
A map containing the basic blocks' start..end pairs.
static std::string toString(const T &source)
static void globPath(const std::string &pattern, std::vector< std::string > &filenames)
static bool removeFileOrDirectory(const std::string &path)
static const std::string DIRECTORY_SEPARATOR
static std::string createTempDirectory(const std::string &path="/tmp", const std::string &tempDirPrefix="tmp_tce_")
static std::string fileOfPath(const std::string pathName)
static bool fileExists(const std::string fileName)
void handleEvent(int event)
void importSymbol(const std::string &symbolName, T *&target, const std::string &module)
void addSearchPath(const std::string &searchPath)
void registerModule(const std::string &module)
TCEString hexValue(bool noHexIdentifier=false) const
Definition SimValue.cc:1150
@ SE_SIMULATION_STOPPED
Generated after simulation has stopped, temporarily or permantently, and control is being returned to...
SimulationEventHandler & eventHandler()
bool staticCompilation() const
bool fuResourceConflictDetection() const
bool executionTracing() const
bool procedureTransferTracing() const
virtual int numberOfRegisters() const
ComponentType * item(int index) const
virtual RegisterFileNavigator registerFileNavigator() const
Definition Machine.cc:450
InstructionAddress location() const
Address address() const
Address entryAddress() const
Definition Program.cc:330
Instruction & lastInstruction() const
Definition Program.cc:463
StopReasonContainer stopReasons_
The set of reasons the simulation was stopped.
const TTAMachine::Machine & sourceMachine_
The simulated Machine Object Model.
bool stopRequested_
Flag indicating that simulation should stop.
virtual void prepareToStop(StopReason reason)
@ STA_FINISHED
Simulation ended after executing the last instruction.
@ STA_INITIALIZING
Simulation is being initialized.
@ STA_RUNNING
A run command (run, stepi, until...) given.
@ STA_STOPPED
Simulation stopped for some reason.
@ STA_INITIALIZED
Simulation initialized and ready to run.
const TTAProgram::Program & program_
Program object model of the simulated program.
SimulationStatus state_
The current state of the simulation.
ClockCycleCount clockCount_
How many clock cycles have been simulated.
SimulatorFrontend & frontend_
Reference to the simulator frontend.
virtual MemorySystem & memorySystem(int coreId=-1)