OpenASIP  2.0
SlotField.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 SlotField.cc
26  *
27  * Implementation of SlotField class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "SlotField.hh"
34 #include "SocketEncoding.hh"
35 #include "NullSocketEncoding.hh"
36 #include "NOPEncoding.hh"
37 #include "NullNOPEncoding.hh"
38 #include "MoveSlot.hh"
39 #include "GuardField.hh"
40 #include "SourceField.hh"
41 #include "DestinationField.hh"
42 #include "ContainerTools.hh"
43 #include "Application.hh"
44 #include "BEMTester.hh"
45 #include "SequenceTools.hh"
46 #include "ObjectState.hh"
47 
48 using std::string;
49 
50 const std::string SlotField::OSNAME_SLOT_FIELD = "slot_field";
51 const std::string SlotField::OSKEY_COMPONENT_ID_POSITION = "comp_id_pos";
52 
53 /**
54  * The constructor.
55  *
56  * @param componentIDPos Position of the socket (or bridge) ID within the
57  * source or destination field.
58  * @param parent The parent move slot.
59  */
61  BinaryEncoding::Position componentIDPos,
62  MoveSlot& parent) :
63  InstructionField(&parent), nopEncoding_(NULL),
64  componentIDPos_(componentIDPos) {
65 }
66 
67 
68 /**
69  * The constructor.
70  *
71  * Loads the state of the object from the given ObjectState tree.
72  *
73  * @param state The ObjectState tree.
74  * @param parent The parent move slot.
75  * @exception ObjectStateLoadingException If an error occurs while loading
76  * the state.
77  */
79  : InstructionField(state, &parent),
80  nopEncoding_(NULL),
81  componentIDPos_(BinaryEncoding::LEFT) {
82  loadState(state);
83 }
84 
85 /**
86  * The destructor.
87  */
91 }
92 
93 
94 /**
95  * Returns the parent move slot.
96  */
97 MoveSlot*
100  if (parent == NULL) {
101  return NULL;
102  } else {
103  MoveSlot* slot = dynamic_cast<MoveSlot*>(parent);
104  assert(slot != NULL);
105  return slot;
106  }
107 }
108 
109 
110 /**
111  * Adds the given socket encoding.
112  *
113  * This method is to be called from the constructor of SocketEncoding.
114  *
115  * @param encoding The socket encoding to be added.
116  * @exception ObjectAlreadyExists If the field already has an encoding for
117  * the same socket or if the encoding is
118  * already assigned to another socket.
119  */
120 void
122  // verify that this is called from SocketEncoding constructor
123  assert(encoding.parent() == NULL);
124 
125  if (hasSocketEncoding(encoding.socketName()) ||
127  *this, encoding.encoding(), encoding.extraBits())) {
128  const string procName = "SlotField::addSocketEncoding";
129  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
130  }
131 
132  encodings_.push_back(&encoding);
133 }
134 
135 /**
136  * Removes the given socket encoding.
137  *
138  * This method is to be called from SocketEncoding destructor.
139  *
140  * @param encoding The socket encoding to be removed.
141  */
142 void
144  // verify that this is called from SocketEncoding destructor
145  assert(encoding.parent() == NULL);
147 }
148 
149 
150 /**
151  * Returns the number of sockets that are encoded in this field.
152  *
153  * @return The number of sockets.
154  */
155 int
157  return encodings_.size();
158 }
159 
160 
161 /**
162  * Returns the socket encoding stored at the given position.
163  *
164  * @param index The position.
165  * @return The socket encoding.
166  * @exception OutOfRange If the given index is negative or not smaller than
167  * the number of sockets encoded in the field.
168  */
170 SlotField::socketEncoding(int index) const {
171  if (index < 0 || index >= socketEncodingCount()) {
172  const string procName = "SlotField::socketEncoding";
173  throw OutOfRange(__FILE__, __LINE__, procName);
174  }
175 
176  return *encodings_[index];
177 }
178 
179 /**
180  * Tells whether the slot field has an encoding for the socket with the given
181  * name.
182  *
183  * @param socket Name of the socket.
184  * @return True if the slot field has an encoding for the given socket,
185  * otherwise false.
186  */
187 bool
188 SlotField::hasSocketEncoding(const std::string& socket) const {
189  for (SocketEncodingTable::const_iterator iter = encodings_.begin();
190  iter != encodings_.end(); iter++) {
191  SocketEncoding* encoding = *iter;
192  if (encoding->socketName() == socket) {
193  return true;
194  }
195  }
196  return false;
197 }
198 
199 
200 /**
201  * Returns the socket encoding of the socket with the given name.
202  *
203  * Returns a NullSocketEncoding instance if this field does not encode the
204  * socket.
205  *
206  * @param socket Name of the socket.
207  * @return The socket encoding of the given socket.
208  */
210 SlotField::socketEncoding(const std::string& socket) const {
211  for (SocketEncodingTable::const_iterator iter = encodings_.begin();
212  iter != encodings_.end(); iter++) {
213  SocketEncoding* encoding = *iter;
214  if (encoding->socketName() == socket) {
215  return **iter;
216  }
217  }
218 
220 }
221 
222 
223 /**
224  * Sets the given encoding for no operation.
225  *
226  * This method is to be called from the constructor of NOPEncoding.
227  *
228  * @param encoding The encoding to be set.
229  * @exception ObjectAlreadyExists If the slot field has a NOP
230  * encoding already or if the given encoding
231  * is ambiguous with some other encoding.
232  */
233 void
235  assert(encoding.parent() == NULL);
236 
237  if (hasNoOperationEncoding() ||
239  *this, encoding.encoding(), encoding.extraBits())) {
240  const string procName = "SlotField::setNoOperationEncoding";
241  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
242  }
243 
244  nopEncoding_ = &encoding;
245 }
246 
247 /**
248  * Unsets the NOP encoding.
249  *
250  * This method is to be called from the destructor of NOPEncoding.
251  */
252 void
255  assert(noOperationEncoding().parent() == NULL);
256  nopEncoding_ = NULL;
257 }
258 
259 
260 /**
261  * Tells whether the slot field has a NOP encoding.
262  *
263  * @return True if the slot field has a NOP encoding, otherwise
264  * false.
265  */
266 bool
268  return nopEncoding_ != NULL;
269 }
270 
271 
272 /**
273  * Returns the NOP encoding.
274  *
275  * Returns NullNOPEncoding instance if the slot field does not have
276  * a NOP encoding.
277  *
278  * @return The NOP encoding.
279  */
282  if (hasNoOperationEncoding()) {
283  return *nopEncoding_;
284  } else {
285  return NullNOPEncoding::instance();
286  }
287 }
288 
289 
290 /**
291  * Returns the position of the component ID within the slot field.
292  *
293  * @return Position of the component ID.
294  */
297  return componentIDPos_;
298 }
299 
300 
301 /**
302  * Returns the bit width required by the socket encodings.
303  *
304  * @return The bit width.
305  */
306 int
308 
309  int width(0);
310  int encodings = socketEncodingCount();
311  for (int i = 0; i < encodings; i++) {
312  SocketEncoding& encoding = socketEncoding(i);
313  int encodingWidth = encoding.width();
314  if (encodingWidth > width) {
315  width = encodingWidth;
316  }
317  }
318 
321  }
322 
323  return width + extraBits();
324 }
325 
326 
327 /**
328  * Always returns 0 because slot fields do not have any child fields.
329  *
330  * @return 0.
331  */
332 int
334  return 0;
335 }
336 
337 
338 /**
339  * Always throws OutOfRange because slot fields do not have any child fields.
340  *
341  * @return Never returns.
342  * @exception OutOfRange Always throws.
343  */
346  const string procName = "SlotField::childField";
347  throw OutOfRange(__FILE__, __LINE__, procName);
348 }
349 
350 /**
351  * Loads the state of the object from the given ObjectState tree.
352  *
353  * @param state The ObjectState tree.
354  * @exception ObjectStateLoadingException If an error occurs while loading
355  * the state.
356  */
357 void
362 
363  const string procName = "SlotField::loadState";
364 
365  try {
368  for (int i = 0; i < state->childCount(); i++) {
369  ObjectState* child = state->child(i);
371  new SocketEncoding(child, *this);
372  } else if (child->name() == NOPEncoding::OSNAME_NOP_ENCODING) {
373  new NOPEncoding(child, *this);
374  }
375  }
376  } catch (const Exception& exception) {
378  __FILE__, __LINE__, procName, exception.errorMessage());
379  }
380 }
381 
382 /**
383  * Saves the state of the object to an ObjectState tree.
384  *
385  * @return The newly created ObjectState tree.
386  */
389 
391  state->setName(OSNAME_SLOT_FIELD);
393 
394  // add socket encodings
395  for (int i = 0; i < socketEncodingCount(); i++) {
396  SocketEncoding& enc = socketEncoding(i);
397  state->addChild(enc.saveState());
398  }
399 
400  // add NOP encoding
401  if (hasNoOperationEncoding()) {
403  }
404 
405  return state;
406 }
407 
408 
409 /**
410  * Clears all the socket encodings from the slot field.
411  */
412 void
415 }
416 
417 
418 /**
419  * Deletes the NOP encoding if one exists.
420  */
421 void
423  if (hasNoOperationEncoding()) {
424  delete nopEncoding_;
425  }
426 }
SlotField::noOperationEncoding
NOPEncoding & noOperationEncoding() const
Definition: SlotField.cc:281
BinaryEncoding
Definition: BinaryEncoding.hh:61
MoveSlot
Definition: MoveSlot.hh:60
SlotField::componentIDPos_
BinaryEncoding::Position componentIDPos_
Position of the socket and bridge IDs within the field.
Definition: SlotField.hh:109
ObjectStateLoadingException
Definition: Exception.hh:551
SocketEncoding::saveState
virtual ObjectState * saveState() const
Definition: SocketEncoding.cc:302
GuardField.hh
SlotField::hasSocketEncoding
bool hasSocketEncoding(const std::string &socket) const
Definition: SlotField.cc:188
OutOfRange
Definition: Exception.hh:320
SlotField::saveState
virtual ObjectState * saveState() const
Definition: SlotField.cc:388
SlotField::childFieldCount
virtual int childFieldCount() const
Definition: SlotField.cc:333
NullSocketEncoding.hh
SequenceTools.hh
ObjectState
Definition: ObjectState.hh:59
SlotField::hasNoOperationEncoding
bool hasNoOperationEncoding() const
Definition: SlotField.cc:267
SocketEncoding::width
virtual int width() const
Definition: SocketEncoding.cc:276
SourceField.hh
ObjectState::setName
void setName(const std::string &name)
NullNOPEncoding::instance
static NullNOPEncoding & instance()
Definition: NullNOPEncoding.cc:65
SlotField::socketEncoding
SocketEncoding & socketEncoding(int index) const
Definition: SlotField.cc:170
MoveSlot.hh
SlotField::removeSocketEncoding
void removeSocketEncoding(SocketEncoding &encoding)
Definition: SlotField.cc:143
InstructionField
Definition: InstructionField.hh:43
InstructionField::saveState
virtual ObjectState * saveState() const
Definition: InstructionField.cc:268
SlotField::setNoOperationEncoding
void setNoOperationEncoding(NOPEncoding &encoding)
Definition: SlotField.cc:234
assert
#define assert(condition)
Definition: Application.hh:86
NOPEncoding::parent
SlotField * parent() const
Definition: NOPEncoding.cc:105
Encoding::width
virtual int width() const
Definition: Encoding.cc:130
SlotField::clearNoOperationEncoding
void clearNoOperationEncoding()
Definition: SlotField.cc:422
SequenceTools::deleteAllItems
static void deleteAllItems(SequenceType &aSequence)
SocketEncoding::OSNAME_SOCKET_ENCODING
static const std::string OSNAME_SOCKET_ENCODING
ObjectState name for socket encoding.
Definition: SocketEncoding.hh:79
SlotField.hh
BinaryEncoding::Position
Position
Definition: BinaryEncoding.hh:63
SlotField::encodings_
SocketEncodingTable encodings_
The container for socket encodings.
Definition: SlotField.hh:107
ContainerTools::removeValueIfExists
static bool removeValueIfExists(ContainerType &aContainer, const ElementType &aKey)
Application.hh
SocketEncoding::socketName
std::string socketName() const
Definition: SocketEncoding.cc:145
SlotField::clearSocketEncodings
void clearSocketEncodings()
Definition: SlotField.cc:413
InstructionField::parent
InstructionField * parent() const
Definition: InstructionField.cc:100
ObjectState.hh
SlotField::width
virtual int width() const
Definition: SlotField.cc:307
SlotField::nopEncoding_
NOPEncoding * nopEncoding_
The NOP encoding.
Definition: SlotField.hh:105
BEMTester.hh
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
NOPEncoding.hh
ObjectState::childCount
int childCount() const
Encoding::encoding
unsigned int encoding() const
Definition: Encoding.cc:108
SlotField::componentIDPosition
BinaryEncoding::Position componentIDPosition() const
Definition: SlotField.cc:296
Exception
Definition: Exception.hh:54
SlotField::~SlotField
virtual ~SlotField()
Definition: SlotField.cc:88
SlotField::unsetNoOperationEncoding
void unsetNoOperationEncoding()
Definition: SlotField.cc:253
NOPEncoding
Definition: NOPEncoding.hh:44
ObjectState::name
std::string name() const
NullSocketEncoding::instance
static NullSocketEncoding & instance()
Definition: NullSocketEncoding.cc:65
SocketEncoding.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
SlotField::addSocketEncoding
void addSocketEncoding(SocketEncoding &encoding)
Definition: SlotField.cc:121
SlotField::childField
virtual InstructionField & childField(int position) const
Definition: SlotField.cc:345
InstructionField::extraBits
int extraBits() const
Definition: InstructionField.cc:229
SlotField::OSNAME_SLOT_FIELD
static const std::string OSNAME_SLOT_FIELD
ObjectState name for slot field.
Definition: SlotField.hh:89
ObjectAlreadyExists
Definition: Exception.hh:1002
SocketEncoding::parent
SlotField * parent() const
Definition: SocketEncoding.cc:127
InstructionField::loadState
virtual void loadState(const ObjectState *state)
Definition: InstructionField.cc:242
BEMTester::canAddComponentEncoding
static bool canAddComponentEncoding(SlotField &field, unsigned int encoding, unsigned int extraBits)
Definition: BEMTester.cc:73
SlotField::socketEncodingCount
int socketEncodingCount() const
Definition: SlotField.cc:156
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
NOPEncoding::OSNAME_NOP_ENCODING
static const std::string OSNAME_NOP_ENCODING
ObjectState name for NOP encoding.
Definition: NOPEncoding.hh:57
DestinationField.hh
SlotField::OSKEY_COMPONENT_ID_POSITION
static const std::string OSKEY_COMPONENT_ID_POSITION
ObjectState attribute key for component ID position.
Definition: SlotField.hh:91
SlotField::SlotField
SlotField(BinaryEncoding::Position componentIDPos, MoveSlot &parent)
Definition: SlotField.cc:60
NullNOPEncoding.hh
SocketEncoding
Definition: SocketEncoding.hh:51
SlotField::parent
MoveSlot * parent() const
Definition: SlotField.cc:98
Encoding::extraBits
unsigned int extraBits() const
Definition: Encoding.cc:119
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
SlotField::loadState
virtual void loadState(const ObjectState *state)
Definition: SlotField.cc:358
ContainerTools.hh