OpenASIP  2.0
SocketEncoding.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 SocketEncoding.cc
26  *
27  * Implementation of SocketEncoding class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @note rating: red
31  */
32 
33 #include <string>
34 
35 #include "SocketEncoding.hh"
36 #include "SlotField.hh"
37 #include "SocketCodeTable.hh"
38 #include "MoveSlot.hh"
39 #include "NullSocketCodeTable.hh"
40 #include "Application.hh"
41 #include "MathTools.hh"
42 #include "ObjectState.hh"
43 
44 using std::string;
45 
46 const std::string SocketEncoding::OSNAME_SOCKET_ENCODING = "socket_encoding";
47 const std::string SocketEncoding::OSKEY_SOCKET_NAME = "socket_name";
48 const std::string SocketEncoding::OSKEY_SC_TABLE = "sc_table";
49 
50 /**
51  * The constructor.
52  *
53  * Creates a socket encoding and registers it into the given slot field. The
54  * socket is identified by name. The (fixed) part of the socket encoding and
55  * the parent slot field are given as parameters.
56  *
57  * @param name Name of the socket.
58  * @param encoding Fixed part of the encoding.
59  * @param extraBits The number of extra (zero) bits in the encoding.
60  * @param parent The parent slot field.
61  * @exception ObjectAlreadyExists If the parent slot field already has an
62  * encoding for the same socket, or if the
63  * given encoding is ambiguous with another
64  * encoding in the parent slot field.
65  */
67  const std::string& name, unsigned int encoding, unsigned int extraBits,
68  SlotField& parent)
69  : Encoding(encoding, extraBits, NULL), name_(name), socketCodes_(NULL) {
71  setParent(&parent);
72 }
73 
74 /**
75  * The constructor.
76  *
77  * Loads the state of the object from the given ObjectState instance.
78  *
79  * @param state The ObjectState instance.
80  * @param parent The parent slot field.
81  * @exception ObjectStateLoadingException If an error occurs while loading
82  * the state.
83  * @exception ObjectAlreadyExists If the parent slot field already has an
84  * encoding for the same socket, or if the
85  * given encoding is ambiguous with another
86  * encoding in the parent slot field.
87  */
89  : Encoding(state, &parent), name_(""), socketCodes_(NULL) {
90  const string procName = "SocketEncoding::SocketEncoding";
91 
92  try {
94  if (state->hasAttribute(OSKEY_SC_TABLE)) {
95  string tableName = state->stringAttribute(OSKEY_SC_TABLE);
96  BinaryEncoding* bem = parent.parent()->parent();
97  if (&bem->socketCodeTable(tableName) ==
100  __FILE__, __LINE__, procName);
101  } else {
102  setSocketCodes(bem->socketCodeTable(tableName));
103  }
104  }
105 
106  } catch (const Exception& exception) {
108  __FILE__, __LINE__, procName, exception.errorMessage());
109  }
110 
111  setParent(NULL);
112  parent.addSocketEncoding(*this);
113  setParent(&parent);
114 }
115 
116 /**
117  * The destructor.
118  */
120 
121 /**
122  * Returns the parent slot field.
123  *
124  * @return The parent slot field.
125  */
126 SlotField*
129  if (parent != NULL) {
130  SlotField* sField = dynamic_cast<SlotField*>(parent);
131  assert(sField != NULL);
132  return sField;
133  } else {
134  return NULL;
135  }
136 }
137 
138 
139 /**
140  * Returns the name of the socket.
141  *
142  * @return The name of the socket.
143  */
144 std::string
146  return name_;
147 }
148 
149 
150 /**
151  * Attaches a set of socket codes to the socket encoding.
152  *
153  * @param codeTable The socket code table to be attached.
154  */
155 void
157  socketCodes_ = &codeTable;
158 }
159 
160 
161 /**
162  * Removes the socket codes from the socket encoding.
163  */
164 void
166  socketCodes_ = NULL;
167 }
168 
169 
170 /**
171  * Tells whether the socket encoding contains a socket code table.
172  *
173  * @return True if the socket encoding contains a socket code table, otherwise
174  * false.
175  */
176 bool
178  return socketCodes_ != NULL;
179 }
180 
181 
182 /**
183  * Returns the socket code table.
184  *
185  * Returns a NullSocketCodeTable instance if the socket does not have a socket
186  * code table set.
187  *
188  * @return The socket code table.
189  */
192  if (!hasSocketCodes()) {
194  } else {
195  return *socketCodes_;
196  }
197 }
198 
199 /**
200  * Returns the position of the socket code within the slot field.
201  */
202 int
204  if (parent()->componentIDPosition() == BinaryEncoding::RIGHT) {
205  return parent()->width() - parent()->extraBits() -
206  socketCodes().width();
207  } else {
208  return 0;
209  }
210 }
211 
212 /**
213  * Sets new encoding.
214  *
215  * @param encoding The encoding.
216  * @param extraBits The extrabits of the encoding.
217  * @exception ObjectAlreadyExists If the parent slot field already has an
218  * encoding for the same socket, or if the
219  * given encoding is ambiguous with another
220  * encoding in the parent slot field.
221  */
222 void
223 SocketEncoding::setEncoding(unsigned int encoding, unsigned int extraBits) {
224  SlotField* parent = this->parent();
225  unsigned int oldEncoding = this->encoding();
226  unsigned int oldExtrabits = this->extraBits();
228  Encoding::setEncoding(encoding, extraBits);
229  try {
230  parent->addSocketEncoding(*this);
231  } catch (ObjectAlreadyExists& e) {
232  Encoding::setEncoding(oldEncoding, oldExtrabits);
233  parent->addSocketEncoding(*this);
234  }
235  setParent(parent);
236 }
237 
238 /**
239  * Returns the position of the socket ID within the slot field.
240  *
241  * The position is always 0 if socket ID is in the right end of the
242  * field. However, if socket code is in the right end of the field,
243  * the position of the socket ID depends on the bit width of the
244  * socket code.
245  *
246  * @return The position of the socket ID.
247  */
248 int
250  if (parent()->componentIDPosition() == BinaryEncoding::RIGHT) {
251  return 0;
252  } else {
253  return parent()->width() - parent()->extraBits() - socketIDWidth();
254  }
255 }
256 
257 
258 /**
259  * Returns the bit width of the fixed part of the socket encoding.
260  *
261  * @return The bit width.
262  */
263 int
266 }
267 
268 
269 /**
270  * Returns the number of bits required to encode all the destinations of this
271  * socket.
272  *
273  * @return The number of bits.
274  */
275 int
277  int width = socketIDWidth();
278  if (hasSocketCodes()) {
279  width += socketCodes().width();
280  }
281  return width;
282 }
283 
284 
285 /**
286  * Returns always 0.
287  *
288  * @return The position.
289  */
290 int
292  return 0;
293 }
294 
295 
296 /**
297  * Saves the state of the socket encoding to an ObjectState instance.
298  *
299  * @return The newly created ObjectState instance.
300  */
303  ObjectState* state = Encoding::saveState();
306  if (hasSocketCodes()) {
307  state->setAttribute(OSKEY_SC_TABLE, socketCodes().name());
308  }
309  return state;
310 }
311 
312 /**
313  * Removes itself from the parent slot field.
314  */
315 void
317  if (this->parent() == nullptr) {
318  return;
319  }
320  SlotField* parent = this->parent();
321  setParent(nullptr);
322  parent->removeSocketEncoding(*this);
323 }
SocketEncoding::detachFromParent
void detachFromParent()
Definition: SocketEncoding.cc:316
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
BinaryEncoding
Definition: BinaryEncoding.hh:61
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
SocketEncoding::setSocketCodes
void setSocketCodes(SocketCodeTable &codeTable)
Definition: SocketEncoding.cc:156
SocketEncoding::bitPosition
virtual int bitPosition() const
Definition: SocketEncoding.cc:291
ObjectStateLoadingException
Definition: Exception.hh:551
SocketEncoding::saveState
virtual ObjectState * saveState() const
Definition: SocketEncoding.cc:302
Encoding::setEncoding
void setEncoding(unsigned int encoding, unsigned int extraBits)
Definition: Encoding.cc:163
Encoding::setParent
void setParent(InstructionField *parent)
Definition: Encoding.cc:155
Encoding::parent
InstructionField * parent() const
Definition: Encoding.cc:97
SocketCodeTable.hh
ObjectState
Definition: ObjectState.hh:59
SocketEncoding::~SocketEncoding
virtual ~SocketEncoding()
Definition: SocketEncoding.cc:119
SocketEncoding::socketCodePosition
int socketCodePosition() const
Definition: SocketEncoding.cc:203
SocketEncoding::width
virtual int width() const
Definition: SocketEncoding.cc:276
ObjectState::setName
void setName(const std::string &name)
BinaryEncoding::socketCodeTable
SocketCodeTable & socketCodeTable(int index) const
Definition: BinaryEncoding.cc:583
MoveSlot.hh
SlotField::removeSocketEncoding
void removeSocketEncoding(SocketEncoding &encoding)
Definition: SlotField.cc:143
InstructionField
Definition: InstructionField.hh:43
SocketCodeTable
Definition: SocketCodeTable.hh:68
Encoding
Definition: Encoding.hh:46
assert
#define assert(condition)
Definition: Application.hh:86
SocketEncoding::OSKEY_SC_TABLE
static const std::string OSKEY_SC_TABLE
ObjectState attribute key for the name of the socket code table.
Definition: SocketEncoding.hh:83
SocketEncoding::OSNAME_SOCKET_ENCODING
static const std::string OSNAME_SOCKET_ENCODING
ObjectState name for socket encoding.
Definition: SocketEncoding.hh:79
SlotField.hh
SocketEncoding::setEncoding
void setEncoding(unsigned int encoding, unsigned int extraBits)
Definition: SocketEncoding.cc:223
Application.hh
SocketEncoding::socketName
std::string socketName() const
Definition: SocketEncoding.cc:145
ObjectState.hh
SlotField::width
virtual int width() const
Definition: SlotField.cc:307
SocketEncoding::SocketEncoding
SocketEncoding(const std::string &name, unsigned int encoding, unsigned int extraBits, SlotField &parent)
Definition: SocketEncoding.cc:66
SocketEncoding::hasSocketCodes
bool hasSocketCodes() const
Definition: SocketEncoding.cc:177
NullSocketCodeTable::instance
static NullSocketCodeTable & instance()
Definition: NullSocketCodeTable.cc:59
Encoding::encoding
unsigned int encoding() const
Definition: Encoding.cc:108
Exception
Definition: Exception.hh:54
SocketEncoding.hh
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
SlotField::addSocketEncoding
void addSocketEncoding(SocketEncoding &encoding)
Definition: SlotField.cc:121
Encoding::saveState
virtual ObjectState * saveState() const
Definition: Encoding.cc:141
InstructionField::extraBits
int extraBits() const
Definition: InstructionField.cc:229
SocketEncoding::socketIDWidth
int socketIDWidth() const
Definition: SocketEncoding.cc:264
SocketEncoding::socketIDPosition
int socketIDPosition() const
Definition: SocketEncoding.cc:249
MoveSlot::parent
BinaryEncoding * parent() const
Definition: MoveSlot.cc:118
BinaryEncoding::RIGHT
@ RIGHT
Definition: BinaryEncoding.hh:65
ObjectAlreadyExists
Definition: Exception.hh:1002
SlotField
Definition: SlotField.hh:58
SocketEncoding::parent
SlotField * parent() const
Definition: SocketEncoding.cc:127
MathTools::bitLength
static unsigned int bitLength(long unsigned int number)
SocketEncoding::unsetSocketCodes
void unsetSocketCodes()
Definition: SocketEncoding.cc:165
NullSocketCodeTable.hh
SocketEncoding::socketCodes_
SocketCodeTable * socketCodes_
Socket code table.
Definition: SocketEncoding.hh:92
SocketEncoding::name_
std::string name_
Name of the socket.
Definition: SocketEncoding.hh:90
MathTools.hh
SocketEncoding::socketCodes
SocketCodeTable & socketCodes() const
Definition: SocketEncoding.cc:191
SlotField::parent
MoveSlot * parent() const
Definition: SlotField.cc:98
SocketCodeTable::width
int width() const
Definition: SocketCodeTable.cc:200
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
SocketEncoding::OSKEY_SOCKET_NAME
static const std::string OSKEY_SOCKET_NAME
ObjectState attribute key for the name of the socket.
Definition: SocketEncoding.hh:81