OpenASIP  2.0
SourceField.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 SourceField.cc
26  *
27  * Implementation of SourceField class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include "SourceField.hh"
34 #include "MoveSlot.hh"
35 #include "BridgeEncoding.hh"
36 #include "ImmediateEncoding.hh"
37 #include "NullImmediateEncoding.hh"
38 #include "NullBridgeEncoding.hh"
39 #include "BEMTester.hh"
40 #include "BEMTextGenerator.hh"
41 #include "ContainerTools.hh"
42 #include "SequenceTools.hh"
43 #include "Application.hh"
44 #include "ObjectState.hh"
45 
46 using std::string;
47 using boost::format;
48 
49 const std::string SourceField::OSNAME_SOURCE_FIELD = "source_field";
50 
51 /**
52  * The constructor.
53  *
54  * Creates a source field and registers it into the given move slot.
55  *
56  * @param componentIDPos Position of the socket or bridge ID within the
57  * source field.
58  * @param parent The parent move slot.
59  * @exception ObjectAlreadyExists If the given move slot already has a source
60  * field.
61  * @exception IllegalParameters If the given component ID position is not the
62  * same with other source fields in the binary
63  * encoding map.
64  */
66  BinaryEncoding::Position componentIDPos, MoveSlot& parent)
67  : SlotField(componentIDPos, parent), immEncoding_(NULL) {
68  BinaryEncoding* bem = parent.parent();
69  for (int i = 0; i < bem->moveSlotCount(); i++) {
70  MoveSlot& slot = bem->moveSlot(i);
71  if (slot.hasSourceField() &&
72  (slot.sourceField().componentIDPosition() != componentIDPos)) {
73  const string procName = "SourceField::SourceField";
74  throw IllegalParameters(__FILE__, __LINE__, procName);
75  } else {
76  break;
77  }
78  }
79 
80  setParent(NULL);
81  parent.setSourceField(*this);
82  setParent(&parent);
83 }
84 
85 /**
86  * The constructor.
87  *
88  * Loads the state of the object from the given ObjectState tree.
89  *
90  * @param state The ObjectState tree.
91  * @param parent The parent move slot.
92  * @exception ObjectStateLoadingException If an error occurs while loading
93  * the state.
94  * @exception ObjectAlreadyExists If the given move slot already has a source
95  * field.
96  */
98  : SlotField(state, parent), immEncoding_(NULL) {
99  loadState(state);
100  setParent(NULL);
101  parent.setSourceField(*this);
102  setParent(&parent);
103 }
104 
105 /**
106  * The destructor.
107  */
111  MoveSlot* parent = this->parent();
112  setParent(NULL);
114 }
115 
116 
117 /**
118  * Adds the given encoding for a bridge to the source field.
119  *
120  * This method is to be called from the constructor of BridgeEncoding class.
121  *
122  * @param encoding The encoding to be added.
123  * @exception ObjectAlreadyExists If the field already has an encoding for
124  * the given bridge or if the encoding is
125  * ambiguous with another encoding or if the
126  * source field has encodings for two bridges
127  * already.
128  */
129 void
131  assert(encoding.parent() == NULL);
132  string bridge = encoding.bridgeName();
133 
134  const string procName = "SourceField::addBridgeEncoding";
135 
136  if (hasBridgeEncoding(bridge) ||
138  *this, encoding.encoding(), encoding.extraBits()) ||
139  bridgeEncodingCount() == 2) {
140  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
141  }
142 
143  bridgeEncodings_.push_back(&encoding);
144 }
145 
146 /**
147  * Removes the given bridge encoding from the source field.
148  *
149  * This method is to be called from the destructor of BridgeEncoding class.
150  *
151  * @param encoding The encoding to be removed.
152  */
153 void
155  assert(encoding.parent() == NULL);
157 }
158 
159 
160 /**
161  * Tells whether the source field has an encoding defined for the given
162  * bridge.
163  *
164  * @param bridge Name of the bridge.
165  * @return True if there is an encoding for the given bridge, otherwise
166  * false.
167  */
168 bool
169 SourceField::hasBridgeEncoding(const std::string& bridge) const {
170 
171  int bridgeEncodings = bridgeEncodingCount();
172  for (int i = 0; i < bridgeEncodings; i++) {
173  BridgeEncoding& encoding = bridgeEncoding(i);
174  if (encoding.bridgeName() == bridge) {
175  return true;
176  }
177  }
178 
179  return false;
180 }
181 
182 
183 /**
184  * Returns the encoding for the given bridge.
185  *
186  * @param bridge Name of the bridge.
187  * @return The bridge encoding for the given bridge or NullBridgeEncoding
188  * instance if there is no encoding for the given bridge.
189  */
191 SourceField::bridgeEncoding(const std::string& bridge) const {
192  int encodingCount = bridgeEncodingCount();
193  for (int i = 0; i < encodingCount; i++) {
194  BridgeEncoding& encoding = bridgeEncoding(i);
195  if (encoding.bridgeName() == bridge) {
196  return encoding;
197  }
198  }
200 }
201 
202 
203 /**
204  * Returns the number of bridge encodings defined for this source field.
205  *
206  * @return The number of bridge encodings.
207  */
208 int
210  return bridgeEncodings_.size();
211 }
212 
213 
214 /**
215  * Returns the bridge encoding stored at the given position.
216  *
217  * @param index The position.
218  * @exception OutOfRange If the given index is negative or not smaller than
219  * the number of bridge encodings.
220  */
222 SourceField::bridgeEncoding(int index) const {
223  if (index < 0 || index >= bridgeEncodingCount()) {
224  const string procName = "SourceField::bridgeEncoding";
225  throw OutOfRange(__FILE__, __LINE__, procName);
226  }
227 
228  return *bridgeEncodings_[index];
229 }
230 
231 /**
232  * Sets the given encoding for inline immediates.
233  *
234  * This method is to be called from the constructor of ImmediateEncoding.
235  *
236  * @param encoding The encoding to be set.
237  * @exception ObjectAlreadyExists If the source field has an immediate
238  * encoding already or if the given encoding
239  * is ambiguous with some socket or bridge
240  * encoding.
241  */
242 void
244  assert(encoding.parent() == NULL);
245 
246  if (hasImmediateEncoding() ||
248  *this, encoding.encoding(), encoding.extraBits())) {
249  BEMTextGenerator textGen;
250  format text = textGen.text(BEMTextGenerator::TXT_ILLEGAL_IMM_ENC);
251  text % parent()->name();
252  const string procName = "SourceField::setImmediateEncoding";
253  throw ObjectAlreadyExists(__FILE__, __LINE__, procName, text.str());
254  }
255 
256  immEncoding_ = &encoding;
257 }
258 
259 /**
260  * Unsets the immediate encoding.
261  *
262  * This method is to be called from the destructor of ImmediateEncoding.
263  */
264 void
267  assert(immediateEncoding().parent() == NULL);
268  immEncoding_ = NULL;
269 }
270 
271 
272 /**
273  * Tells whether the source field has an immediate encoding.
274  *
275  * @return True if the source field has an immediate encoding, otherwise
276  * false.
277  */
278 bool
280  return immEncoding_ != NULL;
281 }
282 
283 
284 /**
285  * Returns the immediate encoding.
286  *
287  * Returns NullImmediateEncoding instance if the source field does not have
288  * an immediate encoding.
289  *
290  * @return The immediate encoding.
291  */
294  if (hasImmediateEncoding()) {
295  return *immEncoding_;
296  } else {
298  }
299 }
300 
301 
302 /**
303  * Returns the bit width of the source field.
304  *
305  * @return The bit width.
306  */
307 int
309 
310  int maxEncodingWidth = SlotField::width() - extraBits();
311 
312  for (int i = 0; i < bridgeEncodingCount(); i++) {
313  BridgeEncoding& encoding = bridgeEncoding(i);
314  if (encoding.width() > maxEncodingWidth) {
315  maxEncodingWidth = encoding.width();
316  }
317  }
318 
320  maxEncodingWidth) {
321  maxEncodingWidth = immediateEncoding().width();
322  }
323 
324  return maxEncodingWidth + extraBits();
325 }
326 
327 
328 /**
329  * Loads the state of the object from the given ObjectState tree.
330  *
331  * @param state The ObjectState tree.
332  * @exception ObjectStateLoadingException If an error occurs while loading
333  * the state.
334  */
335 void
337  const string procName = "SourceField::loadState";
338 
339  if (state->name() != OSNAME_SOURCE_FIELD) {
340  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
341  }
342 
345  SlotField::loadState(state);
346 
347  try {
348  for (int i = 0; i < state->childCount(); i++) {
349  ObjectState* child = state->child(i);
351  new BridgeEncoding(child, *this);
352  } else if (child->name() ==
354  new ImmediateEncoding(child, *this);
355  }
356  }
357  } catch (const Exception& exception) {
359  __FILE__, __LINE__, procName, exception.errorMessage());
360  }
361 
362  // TODO: check that the positions of the encodings match with the
363  // ones given in the ObjectState instances
364 }
365 
366 /**
367  * Saves the state of the object to an ObjectState tree.
368  *
369  * @return The newly created ObjectState tree.
370  */
373 
376 
377  // add bridge encodings
378  for (int i = 0; i < bridgeEncodingCount(); i++) {
379  BridgeEncoding& enc = bridgeEncoding(i);
380  state->addChild(enc.saveState());
381  }
382 
383  // add immediate encoding
384  if (hasImmediateEncoding()) {
386  }
387 
388  return state;
389 }
390 
391 
392 /**
393  * Clears all the bridge encodings from the source field.
394  */
395 void
398 }
399 
400 
401 /**
402  * Deletes the immediate encoding if one exists.
403  */
404 void
406  if (hasImmediateEncoding()) {
407  delete immEncoding_;
408  }
409 }
NullBridgeEncoding::instance
static NullBridgeEncoding & instance()
Definition: NullBridgeEncoding.cc:66
SourceField::setImmediateEncoding
void setImmediateEncoding(ImmediateEncoding &encoding)
Definition: SourceField.cc:243
SourceField::loadState
virtual void loadState(const ObjectState *state)
Definition: SourceField.cc:336
BinaryEncoding
Definition: BinaryEncoding.hh:61
MoveSlot::name
std::string name() const
Definition: MoveSlot.cc:136
SourceField::saveState
virtual ObjectState * saveState() const
Definition: SourceField.cc:372
MoveSlot
Definition: MoveSlot.hh:60
SourceField::bridgeEncodings_
BridgeEncodingTable bridgeEncodings_
Container for bridge encodings.
Definition: SourceField.hh:84
ObjectStateLoadingException
Definition: Exception.hh:551
ImmediateEncoding::OSNAME_IMM_ENCODING
static const std::string OSNAME_IMM_ENCODING
ObjectState name for immediate encoding.
Definition: ImmediateEncoding.hh:63
SourceField::unsetImmediateEncoding
void unsetImmediateEncoding()
Definition: SourceField.cc:265
SourceField::hasImmediateEncoding
bool hasImmediateEncoding() const
Definition: SourceField.cc:279
BridgeEncoding::parent
SourceField * parent() const
Definition: BridgeEncoding.cc:116
OutOfRange
Definition: Exception.hh:320
SlotField::saveState
virtual ObjectState * saveState() const
Definition: SlotField.cc:388
SequenceTools.hh
ImmediateEncoding::parent
SourceField * parent() const
Definition: ImmediateEncoding.cc:126
BridgeEncoding
Definition: BridgeEncoding.hh:47
ObjectState
Definition: ObjectState.hh:59
ImmediateEncoding.hh
NullImmediateEncoding::instance
static NullImmediateEncoding & instance()
Definition: NullImmediateEncoding.cc:65
SourceField.hh
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
ObjectState::setName
void setName(const std::string &name)
BEMTextGenerator
Definition: BEMTextGenerator.hh:42
ImmediateEncoding::width
virtual int width() const
Definition: ImmediateEncoding.cc:207
BridgeEncoding::OSNAME_BRIDGE_ENCODING
static const std::string OSNAME_BRIDGE_ENCODING
ObjectState name for bridge encoding.
Definition: BridgeEncoding.hh:62
MoveSlot.hh
BridgeEncoding.hh
assert
#define assert(condition)
Definition: Application.hh:86
SourceField::immEncoding_
ImmediateEncoding * immEncoding_
The immediate encoding.
Definition: SourceField.hh:86
Encoding::width
virtual int width() const
Definition: Encoding.cc:130
MoveSlot::hasSourceField
bool hasSourceField() const
Definition: MoveSlot.cc:264
SequenceTools::deleteAllItems
static void deleteAllItems(SequenceType &aSequence)
SourceField::hasBridgeEncoding
bool hasBridgeEncoding(const std::string &bridge) const
Definition: SourceField.cc:169
IllegalParameters
Definition: Exception.hh:113
BinaryEncoding::Position
Position
Definition: BinaryEncoding.hh:63
ContainerTools::removeValueIfExists
static bool removeValueIfExists(ContainerType &aContainer, const ElementType &aKey)
SourceField::immediateEncoding
ImmediateEncoding & immediateEncoding() const
Definition: SourceField.cc:293
SourceField::removeBridgeEncoding
void removeBridgeEncoding(BridgeEncoding &encoding)
Definition: SourceField.cc:154
Application.hh
InstructionField::setParent
void setParent(InstructionField *parent)
Definition: InstructionField.cc:282
ObjectState.hh
SlotField::width
virtual int width() const
Definition: SlotField.cc:307
BEMTester.hh
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
BridgeEncoding::saveState
virtual ObjectState * saveState() const
Definition: BridgeEncoding.cc:160
SourceField::width
virtual int width() const
Definition: SourceField.cc:308
ObjectState::childCount
int childCount() const
BridgeEncoding::bridgeName
std::string bridgeName() const
Definition: BridgeEncoding.cc:134
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
MoveSlot::sourceField
SourceField & sourceField() const
Definition: MoveSlot.cc:277
ObjectState::name
std::string name() const
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
SourceField::clearBridgeEncodings
void clearBridgeEncodings()
Definition: SourceField.cc:396
MoveSlot::setSourceField
void setSourceField(SourceField &field)
Definition: MoveSlot.cc:233
InstructionField::extraBits
int extraBits() const
Definition: InstructionField.cc:229
SourceField::clearImmediateEncoding
void clearImmediateEncoding()
Definition: SourceField.cc:405
MoveSlot::parent
BinaryEncoding * parent() const
Definition: MoveSlot.cc:118
ObjectAlreadyExists
Definition: Exception.hh:1002
SlotField
Definition: SlotField.hh:58
SourceField::bridgeEncodingCount
int bridgeEncodingCount() const
Definition: SourceField.cc:209
BEMTester::canAddComponentEncoding
static bool canAddComponentEncoding(SlotField &field, unsigned int encoding, unsigned int extraBits)
Definition: BEMTester.cc:73
BinaryEncoding::moveSlot
MoveSlot & moveSlot(int index) const
Definition: BinaryEncoding.cc:121
SourceField::~SourceField
virtual ~SourceField()
Definition: SourceField.cc:108
SourceField::bridgeEncoding
BridgeEncoding & bridgeEncoding(const std::string &bridge) const
Definition: SourceField.cc:191
SourceField::addBridgeEncoding
void addBridgeEncoding(BridgeEncoding &encoding)
Definition: SourceField.cc:130
ImmediateEncoding
Definition: ImmediateEncoding.hh:44
BinaryEncoding::moveSlotCount
int moveSlotCount() const
Definition: BinaryEncoding.cc:104
SlotField::parent
MoveSlot * parent() const
Definition: SlotField.cc:98
MoveSlot::unsetSourceField
void unsetSourceField()
Definition: MoveSlot.cc:251
NullImmediateEncoding.hh
SourceField::OSNAME_SOURCE_FIELD
static const std::string OSNAME_SOURCE_FIELD
ObjectState name for source field.
Definition: SourceField.hh:74
BEMTextGenerator.hh
SourceField::SourceField
SourceField(BinaryEncoding::Position componentIDPos, MoveSlot &parent)
Definition: SourceField.cc:65
NullBridgeEncoding.hh
Encoding::extraBits
unsigned int extraBits() const
Definition: Encoding.cc:119
BEMTextGenerator::TXT_ILLEGAL_IMM_ENC
@ TXT_ILLEGAL_IMM_ENC
Definition: BEMTextGenerator.hh:49
SlotField::loadState
virtual void loadState(const ObjectState *state)
Definition: SlotField.cc:358
ContainerTools.hh