OpenASIP  2.0
MoveSlot.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2014 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 MoveSlot.cc
26  *
27  * Implementation of MoveSlot class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2014
31  * @note rating: red
32  */
33 
34 #include "MoveSlot.hh"
35 #include "BinaryEncoding.hh"
36 #include "GuardField.hh"
37 #include "NullGuardField.hh"
38 #include "SourceField.hh"
39 #include "NullSourceField.hh"
40 #include "DestinationField.hh"
41 #include "NullDestinationField.hh"
42 #include "ImmediateControlField.hh"
43 #include "NullInstructionField.hh"
44 #include "Application.hh"
45 #include "ObjectState.hh"
46 #include "BEMTester.hh"
47 
48 using std::string;
49 
50 const std::string MoveSlot::OSNAME_MOVE_SLOT = "move_slot";
51 const std::string MoveSlot::OSKEY_BUS_NAME = "bus_name";
52 
53 /**
54  * The constructor.
55  *
56  * Registers the move slot to the parent binary encoding automatically.
57  * The slot is added as the leftmost field of the instruction word.
58  *
59  * @param busName Name of the bus programmed by this move slot.
60  * @param parent The parent BinaryEncoding.
61  * @exception ObjectAlreadyExists If the parent binary encoding has a move
62  * slot with the same bus name already.
63  */
64 MoveSlot::MoveSlot(const std::string& busName, BinaryEncoding& parent)
65  : InstructionField(&parent),
66  name_(busName),
67  guardField_(NULL),
68  sourceField_(NULL),
69  destinationField_(NULL) {
70  setParent(NULL);
71  parent.addMoveSlot(*this);
72  setParent(&parent);
73 }
74 
75 /**
76  * The constructor.
77  *
78  * Loads the state of the move slot from the given ObjectState tree.
79  *
80  * @param state The ObjectState tree.
81  * @param parent The parent binary encoding map.
82  * @exception ObjectStateLoadingException If an error occurs while loading
83  * the state.
84  */
86  : InstructionField(state, &parent),
87  name_(""),
88  guardField_(NULL),
89  sourceField_(NULL),
90  destinationField_(NULL) {
91  loadState(state);
92  setParent(NULL);
93  parent.addMoveSlot(*this);
94  setParent(&parent);
95 }
96 
97 /**
98  * The destructor.
99  */
101 
105 
106  BinaryEncoding* parent = this->parent();
107  setParent(NULL);
108  parent->removeMoveSlot(*this);
109 }
110 
111 
112 /**
113  * Returns the parent binary encoding.
114  *
115  * @return The parent binary encoding.
116  */
120  if (parent == NULL) {
121  return NULL;
122  } else {
123  BinaryEncoding* bem = dynamic_cast<BinaryEncoding*>(parent);
124  assert(bem != NULL);
125  return bem;
126  }
127 }
128 
129 
130 /**
131  * Returns the bus name programmed by this move slot in the instruction word.
132  *
133  * @return Name of the bus.
134  */
135 std::string
136 MoveSlot::name() const {
137  return name_;
138 }
139 
140 
141 /**
142  * Sets the name of the bus programmed by this move slot.
143  *
144  * @param name Name of the bus.
145  * @exception ObjectAlreadyExists If there is a move slot that programs the
146  * given bus already.
147  */
148 void
149 MoveSlot::setName(const std::string& name) {
150  if (name == this->name()) {
151  return;
152  }
153 
154  if (parent()->hasMoveSlot(name)) {
155  const string procName = "MoveSlot::setName";
156  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
157  }
158 
159  name_ = name;
160 }
161 
162 /**
163  * Adds the given guard field to the move slot.
164  *
165  * This method is to be called from the constructor of GuardField.
166  *
167  * @param field The guard field to be added.
168  * @exception ObjectAlreadyExists If the move slot has a guard field already.
169  */
170 void
172  // verify that this is called from GuardField constructor
173  assert(field.parent() == NULL);
174 
175  if (hasGuardField()) {
176  const string procName = "MoveSlot::setGuardField";
177  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
178  }
179 
180  guardField_ = &field;
181 }
182 
183 /**
184  * Removes the guard field from the move slot.
185  *
186  * This method is to be called from the destructor of GuardField.
187  */
188 void
191  assert(guardField().parent() == NULL);
192  guardField_ = NULL;
193 }
194 
195 
196 /**
197  * Tells whether the move slot has a guard field.
198  *
199  * @return True if the move has a guard field, otherwise false.
200  */
201 bool
203  return guardField_ != NULL;
204 }
205 
206 
207 /**
208  * Returns the guard field of the move slot.
209  *
210  * Returns NullGuardField instance if the guard field is missing.
211  *
212  * @return The guard field of the move slot.
213  */
214 GuardField&
216  if (hasGuardField()) {
217  return *guardField_;
218  } else {
219  return NullGuardField::instance();
220  }
221 }
222 
223 /**
224  * Adds the given source field to the move slot.
225  *
226  * This method is to be called from the constructor of SourceField.
227  *
228  * @param field The source field to be added.
229  * @exception ObjectAlreadyExists If the move slot has a source field
230  * already.
231  */
232 void
234  // verify that this is called from SourceField constructor
235  assert(field.parent() == NULL);
236 
237  if (hasSourceField()) {
238  const string procName = "MoveSlot::setSourceField";
239  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
240  }
241 
242  sourceField_ = &field;
243 }
244 
245 /**
246  * Removes the source field from the move slot.
247  *
248  * This method is to be called from the destructor of SourceField.
249  */
250 void
253  assert(sourceField().parent() == NULL);
254  sourceField_ = NULL;
255 }
256 
257 
258 /**
259  * Tells whether the move slot has a source field.
260  *
261  * @return True if the move slot has a source field, otherwise false.
262  */
263 bool
265  return sourceField_ != NULL;
266 }
267 
268 
269 /**
270  * Returns the source field of the move slot.
271  *
272  * Returns NullSourceField if the move slot does not have a source field.
273  *
274  * @return The source field of the move slot.
275  */
278  if (hasSourceField()) {
279  return *sourceField_;
280  } else {
281  return NullSourceField::instance();
282  }
283 }
284 
285 
286 /**
287  * Adds the given destination field to the move slot.
288  *
289  * This method is to be called from the constructor of DestinationField.
290  *
291  * @param field The destination field to be added.
292  * @exception ObjectAlreadyExists If the move slot has a destination field
293  * already.
294  */
295 void
297  // verify that this is called from DestinationField constructor
298  assert(field.parent() == NULL);
299 
300  if (hasDestinationField()) {
301  const string procName = "MoveSlot::setDestinationField";
302  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
303  }
304 
305  destinationField_ = &field;
306 }
307 
308 /**
309  * Removes the destination field from the move slot.
310  *
311  * This method is to be called from the destructor of DestinationField.
312  */
313 void
316  assert(destinationField().parent() == NULL);
317  destinationField_ = NULL;
318 }
319 
320 
321 /**
322  * Tells whether the move slot has a destination field.
323  *
324  * @return True if the move slot has a destination field, otherwise false.
325  */
326 bool
328  return destinationField_ != NULL;
329 }
330 
331 
332 /**
333  * Returns the destination field of the move slot.
334  *
335  * Returns NullDestinationField if the move slot does not have a destination
336  * field.
337  *
338  * @return The destination field of the move slot.
339  */
342  if (hasDestinationField()) {
343  return *destinationField_;
344  } else {
346  }
347 }
348 
349 
350 /**
351  * Returns the number of child fields (guard, source, destination) within the
352  * move slot.
353  *
354  * @return The number of child fields.
355  */
356 int
358  int count(0);
359  if (hasGuardField()) {
360  count++;
361  }
362  if (hasSourceField()) {
363  count++;
364  }
365  if (hasDestinationField()) {
366  count++;
367  }
368  return count;
369 }
370 
371 
372 /**
373  * Returns the child field at the given relative position.
374  *
375  * @param position The position (0 is the rightmost position).
376  * @exception OutOfRange If the position is negative or not smaller than the
377  * number of child fields.
378  */
380 MoveSlot::childField(int position) const {
382 
383  if (hasGuardField() && guardField().relativePosition() == position) {
384  return guardField();
385  }
386 
387  if (hasSourceField() && sourceField().relativePosition() == position) {
388  return sourceField();
389  }
390 
391  if (hasDestinationField() &&
392  destinationField().relativePosition() == position) {
393  return destinationField();
394  }
395 
396  assert(false);
398 }
399 
400 /**
401  * Returns the bit width of the move slot.
402  *
403  * @return The bit width of the move slot.
404  */
405 int
407 
408  int width(0);
409 
410  if (hasGuardField()) {
411  width += guardField().width();
412  }
413  if (hasSourceField()) {
414  width += sourceField().width();
415  }
416  if (hasDestinationField()) {
418  }
419 
420  return width;
421 }
422 
423 
424 /**
425  * Loads the state of the move slot from the given ObjectState tree.
426  *
427  * @param state The ObjectState tree.
428  * @exception ObjectStateLoadingException If an error occurs while loading
429  * the state.
430  */
431 void
436 
437  ObjectState* newState = new ObjectState(*state);
438  reorderSubfields(newState);
439  InstructionField::loadState(newState);
440 
441  try {
443  for (int i = 0; i < newState->childCount(); i++) {
444  ObjectState* child = newState->child(i);
445  if (child->name() == GuardField::OSNAME_GUARD_FIELD) {
446  new GuardField(child, *this);
447  } else if (child->name() == SourceField::OSNAME_SOURCE_FIELD) {
448  new SourceField(child, *this);
449  } else if (
451  new DestinationField(child, *this);
452  }
453  }
454  } catch (const Exception& exception) {
455  const string procName = "MoveSlot::loadState";
457  __FILE__, __LINE__, procName, exception.errorMessage());
458  }
459 
460  delete newState;
461 }
462 
463 /**
464  * Saves the state of the move slot to an ObjectState tree.
465  *
466  * @return The newly created ObjectState tree.
467  */
470 
472  state->setName(OSNAME_MOVE_SLOT);
473  state->setAttribute(OSKEY_BUS_NAME, name());
474 
475  if (hasGuardField()) {
476  state->addChild(guardField().saveState());
477  }
478  if (hasSourceField()) {
479  state->addChild(sourceField().saveState());
480  }
481  if (hasDestinationField()) {
482  state->addChild(destinationField().saveState());
483  }
484 
485  return state;
486 }
487 
488 
489 /**
490  * Deletes the guard field of the move slot.
491  */
492 void
494  if (guardField_ != NULL) {
495  delete guardField_;
496  guardField_ = NULL;
497  }
498 }
499 
500 
501 /**
502  * Deletes the source field of the move slot.
503  */
504 void
506  if (sourceField_ != NULL) {
507  delete sourceField_;
508  sourceField_ = NULL;
509  }
510 }
511 
512 /**
513  * Deletes the destination field of the move slot.
514  */
515 void
517  if (destinationField_ != NULL) {
518  delete destinationField_;
519  destinationField_ = NULL;
520  }
521 }
MoveSlot::OSNAME_MOVE_SLOT
static const std::string OSNAME_MOVE_SLOT
ObjectState name for move slot.
Definition: MoveSlot.hh:96
BinaryEncoding
Definition: BinaryEncoding.hh:61
MoveSlot::name
std::string name() const
Definition: MoveSlot.cc:136
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
DestinationField
Definition: DestinationField.hh:44
BinaryEncoding::removeMoveSlot
void removeMoveSlot(MoveSlot &slot)
Definition: BinaryEncoding.cc:203
ObjectStateLoadingException
Definition: Exception.hh:551
MoveSlot::loadState
virtual void loadState(const ObjectState *state)
Definition: MoveSlot.cc:432
GuardField.hh
InstructionField::childField
virtual InstructionField & childField(int position) const
Definition: InstructionField.cc:117
MoveSlot::hasGuardField
bool hasGuardField() const
Definition: MoveSlot.cc:202
NullDestinationField.hh
ObjectState
Definition: ObjectState.hh:59
GuardField
Definition: GuardField.hh:55
MoveSlot::guardField_
GuardField * guardField_
The guard field.
Definition: MoveSlot.hh:108
MoveSlot::unsetDestinationField
void unsetDestinationField()
Definition: MoveSlot.cc:314
SourceField.hh
ObjectState::setName
void setName(const std::string &name)
NullInstructionField.hh
MoveSlot.hh
NullInstructionField::instance
static NullInstructionField & instance()
Definition: NullInstructionField.cc:62
NullSourceField::instance
static NullSourceField & instance()
Definition: NullSourceField.cc:62
InstructionField
Definition: InstructionField.hh:43
InstructionField::saveState
virtual ObjectState * saveState() const
Definition: InstructionField.cc:268
MoveSlot::setName
void setName(const std::string &name)
Definition: MoveSlot.cc:149
MoveSlot::deleteSourceField
void deleteSourceField()
Definition: MoveSlot.cc:505
assert
#define assert(condition)
Definition: Application.hh:86
MoveSlot::hasSourceField
bool hasSourceField() const
Definition: MoveSlot.cc:264
MoveSlot::deleteDestinationField
void deleteDestinationField()
Definition: MoveSlot.cc:516
MoveSlot::~MoveSlot
virtual ~MoveSlot()
Definition: MoveSlot.cc:100
NullGuardField::instance
static NullGuardField & instance()
Definition: NullGuardField.cc:61
MoveSlot::guardField
GuardField & guardField() const
Definition: MoveSlot.cc:215
BinaryEncoding.hh
MoveSlot::hasDestinationField
bool hasDestinationField() const
Definition: MoveSlot.cc:327
GuardField::parent
MoveSlot * parent() const
Definition: GuardField.cc:117
Application.hh
InstructionField::setParent
void setParent(InstructionField *parent)
Definition: InstructionField.cc:282
InstructionField::parent
InstructionField * parent() const
Definition: InstructionField.cc:100
ObjectState.hh
SlotField::width
virtual int width() const
Definition: SlotField.cc:307
DestinationField::OSNAME_DESTINATION_FIELD
static const std::string OSNAME_DESTINATION_FIELD
ObjectState name for destination field.
Definition: DestinationField.hh:54
BEMTester.hh
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
SourceField::width
virtual int width() const
Definition: SourceField.cc:308
ObjectState::childCount
int childCount() const
NullGuardField.hh
NullSourceField.hh
MoveSlot::sourceField_
SourceField * sourceField_
The source field.
Definition: MoveSlot.hh:110
Exception
Definition: Exception.hh:54
InstructionField::reorderSubfields
static void reorderSubfields(ObjectState *state)
Definition: InstructionField.cc:297
MoveSlot::width
virtual int width() const
Definition: MoveSlot.cc:406
MoveSlot::sourceField
SourceField & sourceField() const
Definition: MoveSlot.cc:277
ObjectState::name
std::string name() const
MoveSlot::deleteGuardField
void deleteGuardField()
Definition: MoveSlot.cc:493
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
GuardField::OSNAME_GUARD_FIELD
static const std::string OSNAME_GUARD_FIELD
ObjectState name for guard field.
Definition: GuardField.hh:99
ImmediateControlField.hh
MoveSlot::saveState
virtual ObjectState * saveState() const
Definition: MoveSlot.cc:469
MoveSlot::setSourceField
void setSourceField(SourceField &field)
Definition: MoveSlot.cc:233
BinaryEncoding::addMoveSlot
void addMoveSlot(MoveSlot &slot)
Definition: BinaryEncoding.cc:183
MoveSlot::parent
BinaryEncoding * parent() const
Definition: MoveSlot.cc:118
MoveSlot::childField
virtual InstructionField & childField(int position) const
Definition: MoveSlot.cc:380
NullDestinationField::instance
static NullDestinationField & instance()
Definition: NullDestinationField.cc:62
ObjectAlreadyExists
Definition: Exception.hh:1002
InstructionField::loadState
virtual void loadState(const ObjectState *state)
Definition: InstructionField.cc:242
MoveSlot::name_
std::string name_
The bus name.
Definition: MoveSlot.hh:106
MoveSlot::OSKEY_BUS_NAME
static const std::string OSKEY_BUS_NAME
ObjectState attribute key for the name of the bus.
Definition: MoveSlot.hh:98
MoveSlot::unsetGuardField
void unsetGuardField()
Definition: MoveSlot.cc:189
InstructionField::relativePosition
int relativePosition() const
Definition: InstructionField.cc:160
MoveSlot::setDestinationField
void setDestinationField(DestinationField &field)
Definition: MoveSlot.cc:296
MoveSlot::destinationField
DestinationField & destinationField() const
Definition: MoveSlot.cc:341
DestinationField.hh
MoveSlot::setGuardField
void setGuardField(GuardField &field)
Definition: MoveSlot.cc:171
SlotField::parent
MoveSlot * parent() const
Definition: SlotField.cc:98
MoveSlot::unsetSourceField
void unsetSourceField()
Definition: MoveSlot.cc:251
SourceField::OSNAME_SOURCE_FIELD
static const std::string OSNAME_SOURCE_FIELD
ObjectState name for source field.
Definition: SourceField.hh:74
MoveSlot::childFieldCount
virtual int childFieldCount() const
Definition: MoveSlot.cc:357
MoveSlot::destinationField_
DestinationField * destinationField_
The destination field.
Definition: MoveSlot.hh:112
MoveSlot::MoveSlot
MoveSlot(const std::string &busName, BinaryEncoding &parent)
Definition: MoveSlot.cc:64
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
GuardField::width
virtual int width() const
Definition: GuardField.cc:533
SourceField
Definition: SourceField.hh:48