OpenASIP  2.0
StopPointManager.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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 StopPointManager.cc
26  *
27  * Definition of StopPointManager class.
28  *
29  * @author Pekka Jääskeläinen 2005 (pjaaskel-no.spam-cs.tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 #include <vector>
35 #include <map>
36 
37 #include "Exception.hh"
38 #include "StopPointManager.hh"
40 #include "SimulationController.hh"
41 #include "MapTools.hh"
42 #include "Application.hh"
43 
44 using std::string;
45 using std::vector;
46 using std::map;
47 using std::make_pair;
48 
49 
50 /**
51  * Constructor.
52  *
53  * @param controller The simulation controller the stop points should use to
54  * stop the simulation.
55  */
57  TTASimulationController& controller,
58  SimulationEventHandler& eventHandler) :
59  handleCount_(0), lastStopCycle_(0), controller_(controller),
60  eventHandler_(eventHandler) {
61 }
62 
63 
64 /**
65  * Destructor.
66  */
69 }
70 
71 
72 /**
73  * Function that adds a new stop point.
74  *
75  * Copies the given stop point.
76  *
77  * @param stopPoint The stop point to be added.
78  * @return Handle for the added stop point.
79  */
80 unsigned int
81 StopPointManager::add(const StopPoint& stopPoint) {
82 
83  StopPoint* toAdd = stopPoint.copy();
84 
85  ++handleCount_;
86 
87  stopPoints_.insert(make_pair(handleCount_, toAdd));
88  handles_.push_back(handleCount_);
89 
90  toAdd->setEnabled(true);
91 
92  if (stopPoints_.size() == 1) {
93  // this is the first added stop point, enable event
94  // listening
97  }
98  return handleCount_;
99 }
100 
101 
102 /**
103  * Remove a stop point by the given handle.
104  *
105  * @param handle The handle of the stop point to be removed.
106  */
107 void
108 StopPointManager::deleteStopPoint(unsigned int handle) {
109  StopPoint* stopPoint = findStopPoint(handle);
110 
111  StopPointIndex::iterator findResult = stopPoints_.find(handle);
112  stopPoints_.erase(findResult);
113 
114  for (HandleContainer::iterator i = handles_.begin(); i != handles_.end();
115  i++) {
116  if ((*i) == handle) {
117  handles_.erase(i);
118  break;
119  }
120  }
121 
122  delete stopPoint;
123  stopPoint = NULL;
124 
125  if (stopPoints_.size() == 0) {
126  // this was the last stop point, disable event listening
129  }
130 }
131 
132 /**
133  * Removes all stop points.
134  */
135 void
137  while (handles_.size() > 0) {
138  deleteStopPoint(handles_.at(0));
139  }
140 }
141 
142 
143 /**
144  * Enables the stop point by the given handle.
145  *
146  * @param handle The handle to identify the stop point.
147  */
148 void
149 StopPointManager::enable(unsigned int handle) {
150  findStopPoint(handle)->setEnabled(true);
151 }
152 
153 /**
154  * Enables all stop points.
155  */
156 void
158  for (unsigned int i = 0; i < handles_.size(); i++) {
159  enable(handles_.at(i));
160  }
161 }
162 
163 
164 /**
165  * Enables the stop point by the given handle and sets it to be deleted
166  * after being triggered.
167  *
168  * @param handle The handle for the stop point.
169  * @throw InstanceNotFound if the given handle cannot be found.
170  */
171 void
173  StopPoint* stopPoint = findStopPoint(handle);
174 
175  stopPoint->setEnabled(true);
176  stopPoint->setDeletedAfterTriggered(true);
177 }
178 
179 /**
180  * Enables the stop point by the given handle and sets it to be disabled
181  * after being triggered.
182  *
183  * @param handle The handle for the stop point.
184  * @throw InstanceNotFound if the given handle cannot be found.
185  */
186 void
188  StopPoint* stopPoint = findStopPoint(handle);
189 
190  stopPoint->setEnabled(true);
191  stopPoint->setDisabledAfterTriggered(true);
192 }
193 
194 /**
195  * Disables the stop point by the given handle.
196  *
197  * @param handle The handle for the stop point.
198  */
199 void
200 StopPointManager::disable(unsigned int handle) {
201  findStopPoint(handle)->setEnabled(false);
202 }
203 
204 /**
205  * Disables all stop oints.
206  */
207 void
209  for (unsigned int i = 0; i < handles_.size(); i++) {
210  disable(handles_.at(i));
211  }
212 }
213 
214 
215 /**
216  * Returns the handle of the stop point by the given index.
217  *
218  * The index is not a direct index to the container used to store stop points,
219  * but more just a way to go through all of the stop points in the manager.
220  * A stop point may not have the same index every call.
221  *
222  * All of the stop points can be accessed by going through the stop points
223  * with the indices between 0 and stopPointCount - 1, inclusive. The
224  * stop points are in no particular order.
225  *
226  * @param index The index.
227  * @return A Copy of the stop point by the given index.
228  * @throw OutOfRange If there is no stop point by the given index.
229  */
230 unsigned int
232  if (index < stopPointCount()) {
233  return handles_.at(index);
234  } else {
235  /// @todo textgenerator, to be displayed in the UI.
236  string msg = "No stop point found by the given index.";
237  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
238  }
239 }
240 /**
241  * Returns the stop point with the given handle.
242  *
243  * @param handle The handle.
244  * @return Stop point with the given handle,
245  * @throw InstanceNotFound If there is no stop point with the given handle.
246  */
247 const StopPoint&
248 StopPointManager::stopPointWithHandleConst(unsigned int handle) const {
249  StopPointIndex::const_iterator containerEnd = stopPoints_.end();
250  StopPointIndex::const_iterator findResult = stopPoints_.find(handle);
251 
252  if (findResult == containerEnd) {
253  /// @todo textgenerator, to be displayed in the UI.
254  std::string msg = "No stop point found by the given handle.";
255  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
256  } else {
257  return *findResult->second;
258  }
259 }
260 
261 /**
262  * Returns the current number of stop points in the manager.
263  *
264  * Can be used to access all stop points in the manager.
265  *
266  * @return The number of stop points in the manager.
267  */
268 unsigned int
270  return handles_.size();
271 }
272 
273 
274 /**
275  * Sets the number of times the stop point by the given handle should not
276  * be triggered when the condition is met.
277  *
278  * @param handle The handle for the stop point.
279  * @param count The number of times the stop point should be ignored.
280  * @exception InstanceNotFound If no stop point with given handle is found.
281  */
282 void
283 StopPointManager::setIgnore(unsigned int handle, unsigned int count) {
284  findStopPoint(handle)->setIgnoreCount(count);
285 }
286 
287 /**
288  * Sets the condition of triggering for the stop point by the given handle.
289  *
290  * @param handle The handle for the stop point.
291  * @param condition The condition to be used to determine if the stop point
292  * should be fired.
293  * @exception InstanceNotFound If no stop point with given handle is found.
294  */
295 void
297  unsigned int handle, const ConditionScript& condition) {
298  findStopPoint(handle)->setCondition(condition);
299 }
300 
301 /**
302  * Removes the condition of triggering of the stop point by the given handle.
303  *
304  * @param handle The handle for the stop point.
305  * @exception InstanceNotFound If no stop point with given handle is found.
306  */
307 void
308 StopPointManager::removeCondition(unsigned int handle) {
309  findStopPoint(handle)->removeCondition();
310 }
311 
312 /**
313  * Tries to find a stop point by the given handle. If no such stop point is
314  * found, throws an exception.
315  *
316  * @param handle The handle for the stop point.
317  * @exception InstanceNotFound If no stop point with given handle is found.
318  */
319 StopPoint*
320 StopPointManager::findStopPoint(unsigned int handle) {
321  StopPointIndex::iterator containerEnd = stopPoints_.end();
322  StopPointIndex::iterator findResult = stopPoints_.find(handle);
323 
324  if (findResult == containerEnd) {
325  /// @todo textgenerator, to be displayed in the UI.
326  string msg = "No stop point found by the given handle.";
327  throw InstanceNotFound(__FILE__, __LINE__, __func__, msg);
328  } else {
329  return findResult->second;
330  }
331 }
332 
333 /**
334  * Returns the handle of a stop causing stop point with given index in the
335  * container of stop causing stop points.
336  *
337  * Stop causing stop point is a stop point that caused the latest stop of
338  * simulator.
339  *
340  * @param index Index of the stop point in the container.
341  * @return the handle of the stop point.
342  * @exception OutOfRange
343  */
344 unsigned int
345 StopPointManager::stopCausingStopPoint(unsigned int index) const {
346  unsigned int count = 0;
347  for (StopPointIndex::const_iterator i = stopPoints_.begin();
348  i != stopPoints_.end(); ++i) {
349  if ((*i).second->isTriggered()) {
350  if (count == index)
351  return (*i).first;
352  ++count;
353  }
354  }
355  throw OutOfRange(
356  __FILE__, __LINE__, __func__, "Stop point index out of range.");
357 }
358 
359 /**
360  * Returns the count of stop causing stop points.
361  *
362  * Stop causing stop point is a stop point that caused the latest stop of
363  * simulator.
364  *
365  * @return the count of stop points.
366  */
367 unsigned int
370  return 0;
371  }
372  std::size_t count = 0;
373  for (StopPointIndex::const_iterator i = stopPoints_.begin();
374  i != stopPoints_.end(); ++i) {
375  if ((*i).second->isTriggered())
376  ++count;
377  }
378  return count;
379 }
380 
381 
382 /**
383  * Stops simulation if there is at least one stop point requesting it.
384  *
385  * Receives SE_NEW_INSTRUCTION events.
386  */
387 void
389 
390  // find all the stop points watching the new instruction address
391  StopPointIndex::iterator i = stopPoints_.begin();
392  HandleContainer toBeDeletedStopPoints;
393  while (i != stopPoints_.end()) {
394 
395  StopPoint& stopPoint = *(*i).second;
396  const int handle = (*i).first;
397  if (stopPoint.isEnabled() && stopPoint.isTriggered()) {
398  // we found a stop point that is triggered at this clock cycle
399 
400  if (stopPoint.ignoreCount() == 0 && stopPoint.isConditionOK()) {
401 
404  if (stopPoint.isDisabledAfterTriggered()) {
405  stopPoint.setEnabled(false);
406  }
407 
408  if (stopPoint.isDeletedAfterTriggered()) {
409  toBeDeletedStopPoints.push_back(handle);
410  }
411 
412  } else if (stopPoint.isConditionOK()) {
413  // decrease the ignore count only if the (possible) condition
414  // of the breakpoint is also true
415  stopPoint.decreaseIgnoreCount();
416  }
417  }
418  ++i;
419  }
420 
421  // delete stop points that wanted to be deleted after triggered
422  for (size_t i = 0; i < toBeDeletedStopPoints.size(); ++i) {
423  deleteStopPoint(toBeDeletedStopPoints[i]);
424  }
425 }
426 
StopPointManager::enableOnceAndDelete
void enableOnceAndDelete(unsigned int handle)
Definition: StopPointManager.cc:172
StopPointManager.hh
StopPointManager::handleCount_
unsigned int handleCount_
Represents the next free handle.
Definition: StopPointManager.hh:97
StopPoint::setEnabled
virtual void setEnabled(bool flag)
Definition: StopPoint.cc:63
StopPointManager::controller_
TTASimulationController & controller_
The simulation controller to use to stop the simulation.
Definition: StopPointManager.hh:101
StopPoint::setIgnoreCount
virtual void setIgnoreCount(unsigned int count)
Definition: StopPoint.cc:182
Exception.hh
StopPointManager::add
unsigned int add(const StopPoint &stopPoint)
Definition: StopPointManager.cc:81
StopPoint::setDisabledAfterTriggered
virtual void setDisabledAfterTriggered(bool flag)
Definition: StopPoint.cc:85
StopPointManager::eventHandler_
SimulationEventHandler & eventHandler_
The event handler to use to register stop points to.
Definition: StopPointManager.hh:103
StopPointManager::stopPoints_
StopPointIndex stopPoints_
The stop points.
Definition: StopPointManager.hh:93
OutOfRange
Definition: Exception.hh:320
MapTools.hh
SimulationEventHandler
Definition: SimulationEventHandler.hh:41
StopPoint::isDeletedAfterTriggered
virtual bool isDeletedAfterTriggered() const
Definition: StopPoint.cc:119
MapTools::deleteAllValues
static void deleteAllValues(MapType &aMap)
StopPointManager::enableOnceAndDisable
void enableOnceAndDisable(unsigned int handle)
Definition: StopPointManager.cc:187
StopPoint::ignoreCount
virtual unsigned int ignoreCount() const
Definition: StopPoint.cc:193
StopPointManager::deleteStopPoint
void deleteStopPoint(unsigned int handle)
Definition: StopPointManager.cc:108
TTASimulationController::prepareToStop
virtual void prepareToStop(StopReason reason)
Definition: TTASimulationController.cc:90
StopPointManager::setCondition
void setCondition(unsigned int handle, const ConditionScript &condition)
Definition: StopPointManager.cc:296
StopPointManager::lastStopCycle_
ClockCycleCount lastStopCycle_
The clock cycle in which simulation was stopped last.
Definition: StopPointManager.hh:99
ConditionScript
Definition: ConditionScript.hh:45
StopPoint::copy
virtual StopPoint * copy() const =0
SimulationEventHandler.hh
StopPointManager::stopPointCount
unsigned int stopPointCount()
Definition: StopPointManager.cc:269
StopPointManager::stopCausingStopPoint
unsigned int stopCausingStopPoint(unsigned int index) const
Definition: StopPointManager.cc:345
TTASimulationController
Definition: TTASimulationController.hh:69
StopPointManager::StopPointManager
StopPointManager(TTASimulationController &controller, SimulationEventHandler &eventHandler)
Definition: StopPointManager.cc:56
StopPointManager::stopPointWithHandleConst
const StopPoint & stopPointWithHandleConst(unsigned int handle) const
Definition: StopPointManager.cc:248
StopPoint
Definition: StopPoint.hh:53
Informer::unregisterListener
virtual bool unregisterListener(int event, Listener *listener)
Definition: Informer.cc:104
StopPointManager::enable
void enable(unsigned int handle)
Definition: StopPointManager.cc:149
StopPointManager::HandleContainer
std::vector< unsigned int > HandleContainer
The handle storage.
Definition: StopPointManager.hh:88
Application.hh
__func__
#define __func__
Definition: Application.hh:67
Informer::registerListener
virtual bool registerListener(int event, Listener *listener)
Definition: Informer.cc:87
StopPointManager::deleteAll
void deleteAll()
Definition: StopPointManager.cc:136
StopPointManager::stopCausingStopPointCount
unsigned int stopCausingStopPointCount() const
Definition: StopPointManager.cc:368
SRE_BREAKPOINT
@ SRE_BREAKPOINT
Stopped because of at least one breakpoint.
Definition: SimulatorConstants.hh:65
StopPoint::isDisabledAfterTriggered
virtual bool isDisabledAfterTriggered() const
Definition: StopPoint.cc:97
StopPointManager::disable
void disable(unsigned int handle)
Definition: StopPointManager.cc:200
StopPoint::isConditionOK
virtual bool isConditionOK()
Definition: StopPoint.cc:215
StopPoint::isEnabled
virtual bool isEnabled() const
Definition: StopPoint.cc:73
StopPointManager::removeCondition
void removeCondition(unsigned int handle)
Definition: StopPointManager.cc:308
TTASimulationController::clockCount
virtual ClockCycleCount clockCount() const
Definition: TTASimulationController.cc:161
StopPoint::isTriggered
virtual bool isTriggered() const =0
StopPointManager::disableAll
void disableAll()
Definition: StopPointManager.cc:208
StopPoint::removeCondition
virtual void removeCondition()
Definition: StopPoint.cc:142
SimulationController.hh
StopPointManager::handles_
HandleContainer handles_
The stop point handles.
Definition: StopPointManager.hh:95
StopPointManager::stopPointHandle
unsigned int stopPointHandle(unsigned int index)
Definition: StopPointManager.cc:231
StopPointManager::~StopPointManager
virtual ~StopPointManager()
Definition: StopPointManager.cc:67
StopPointManager::enableAll
void enableAll()
Definition: StopPointManager.cc:157
StopPoint::setDeletedAfterTriggered
virtual void setDeletedAfterTriggered(bool flag)
Definition: StopPoint.cc:107
StopPointManager::handleEvent
void handleEvent()
Definition: StopPointManager.cc:388
StopPoint::setCondition
virtual void setCondition(const ConditionScript &condition)
Definition: StopPoint.cc:131
StopPointManager::setIgnore
void setIgnore(unsigned int handle, unsigned int count)
Definition: StopPointManager.cc:283
InstanceNotFound
Definition: Exception.hh:304
StopPointManager::findStopPoint
StopPoint * findStopPoint(unsigned int handle)
Definition: StopPointManager.cc:320
StopPoint::decreaseIgnoreCount
virtual void decreaseIgnoreCount()
Definition: StopPoint.cc:201
SimulationEventHandler::SE_NEW_INSTRUCTION
@ SE_NEW_INSTRUCTION
Generated before executing a new instructon.
Definition: SimulationEventHandler.hh:48