OpenASIP 2.2
Loading...
Searching...
No Matches
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"
40#include "Application.hh"
41#include "MathTools.hh"
42#include "ObjectState.hh"
43
44using std::string;
45
46const std::string SocketEncoding::OSNAME_SOCKET_ENCODING = "socket_encoding";
47const std::string SocketEncoding::OSKEY_SOCKET_NAME = "socket_name";
48const 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) {
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);
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);
114}
115
116/**
117 * The destructor.
118 */
120
121/**
122 * Returns the parent slot field.
123 *
124 * @return The parent slot field.
125 */
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 */
144std::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 */
155void
159
160
161/**
162 * Removes the socket codes from the socket encoding.
163 */
164void
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 */
176bool
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 */
202int
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 */
222void
223SocketEncoding::setEncoding(unsigned int encoding, unsigned int extraBits) {
224 SlotField* parent = this->parent();
225 unsigned int oldEncoding = this->encoding();
226 unsigned int oldExtrabits = this->extraBits();
229 try {
231 } catch (ObjectAlreadyExists& e) {
232 Encoding::setEncoding(oldEncoding, oldExtrabits);
234 }
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 */
248int
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 */
263int
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 */
275int
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 */
290int
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 */
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 */
315void
317 if (this->parent() == nullptr) {
318 return;
319 }
320 SlotField* parent = this->parent();
321 setParent(nullptr);
322 parent->removeSocketEncoding(*this);
323}
#define assert(condition)
SocketCodeTable & socketCodeTable(int index) const
unsigned int extraBits() const
Definition Encoding.cc:119
virtual ObjectState * saveState() const
Definition Encoding.cc:141
InstructionField * parent() const
Definition Encoding.cc:97
void setEncoding(unsigned int encoding, unsigned int extraBits)
Definition Encoding.cc:163
void setParent(InstructionField *parent)
Definition Encoding.cc:155
unsigned int encoding() const
Definition Encoding.cc:108
std::string errorMessage() const
Definition Exception.cc:123
static unsigned int bitLength(long unsigned int number)
BinaryEncoding * parent() const
Definition MoveSlot.cc:118
static NullSocketCodeTable & instance()
bool hasAttribute(const std::string &name) const
void setName(const std::string &name)
void setAttribute(const std::string &name, const std::string &value)
std::string stringAttribute(const std::string &name) const
MoveSlot * parent() const
Definition SlotField.cc:98
void addSocketEncoding(SocketEncoding &encoding)
Definition SlotField.cc:121
virtual int width() const
Definition SlotField.cc:307
void removeSocketEncoding(SocketEncoding &encoding)
Definition SlotField.cc:143
virtual ObjectState * saveState() const
virtual int width() const
SocketCodeTable & socketCodes() const
virtual int bitPosition() const
bool hasSocketCodes() const
static const std::string OSNAME_SOCKET_ENCODING
ObjectState name for socket encoding.
void setEncoding(unsigned int encoding, unsigned int extraBits)
virtual ~SocketEncoding()
std::string socketName() const
void setSocketCodes(SocketCodeTable &codeTable)
int socketIDWidth() const
SocketEncoding(const std::string &name, unsigned int encoding, unsigned int extraBits, SlotField &parent)
static const std::string OSKEY_SOCKET_NAME
ObjectState attribute key for the name of the socket.
SlotField * parent() const
SocketCodeTable * socketCodes_
Socket code table.
static const std::string OSKEY_SC_TABLE
ObjectState attribute key for the name of the socket code table.
std::string name_
Name of the socket.
int socketIDPosition() const
int socketCodePosition() const