OpenASIP 2.2
Loading...
Searching...
No Matches
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
48using std::string;
49
50const std::string SlotField::OSNAME_SLOT_FIELD = "slot_field";
51const 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 */
92
93
94/**
95 * Returns the parent move slot.
96 */
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 */
120void
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 */
142void
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 */
155int
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 */
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 */
187bool
188SlotField::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 */
210SlotField::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 */
233void
235 assert(encoding.parent() == NULL);
236
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 */
252void
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 */
266bool
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 */
283 return *nopEncoding_;
284 } else {
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 */
299
300
301/**
302 * Returns the bit width required by the socket encodings.
303 *
304 * @return The bit width.
305 */
306int
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 */
332int
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 */
357void
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
393
394 // add socket encodings
395 for (int i = 0; i < socketEncodingCount(); i++) {
397 state->addChild(enc.saveState());
398 }
399
400 // add NOP encoding
403 }
404
405 return state;
406}
407
408
409/**
410 * Clears all the socket encodings from the slot field.
411 */
412void
416
417
418/**
419 * Deletes the NOP encoding if one exists.
420 */
421void
#define assert(condition)
static bool canAddComponentEncoding(SlotField &field, unsigned int encoding, unsigned int extraBits)
Definition BEMTester.cc:73
static bool removeValueIfExists(ContainerType &aContainer, const ElementType &aKey)
unsigned int extraBits() const
Definition Encoding.cc:119
virtual int width() const
Definition Encoding.cc:130
unsigned int encoding() const
Definition Encoding.cc:108
std::string errorMessage() const
Definition Exception.cc:123
InstructionField * parent() const
virtual void loadState(const ObjectState *state)
virtual ObjectState * saveState() const
static const std::string OSNAME_NOP_ENCODING
ObjectState name for NOP encoding.
SlotField * parent() const
static NullNOPEncoding & instance()
static NullSocketEncoding & instance()
void setName(const std::string &name)
void setAttribute(const std::string &name, const std::string &value)
ObjectState * child(int index) const
void addChild(ObjectState *child)
int intAttribute(const std::string &name) const
std::string name() const
int childCount() const
static void deleteAllItems(SequenceType &aSequence)
static const std::string OSNAME_SLOT_FIELD
ObjectState name for slot field.
Definition SlotField.hh:89
MoveSlot * parent() const
Definition SlotField.cc:98
void addSocketEncoding(SocketEncoding &encoding)
Definition SlotField.cc:121
SlotField(BinaryEncoding::Position componentIDPos, MoveSlot &parent)
Definition SlotField.cc:60
bool hasSocketEncoding(const std::string &socket) const
Definition SlotField.cc:188
NOPEncoding * nopEncoding_
The NOP encoding.
Definition SlotField.hh:105
virtual int width() const
Definition SlotField.cc:307
virtual InstructionField & childField(int position) const
Definition SlotField.cc:345
SocketEncodingTable encodings_
The container for socket encodings.
Definition SlotField.hh:107
SocketEncoding & socketEncoding(int index) const
Definition SlotField.cc:170
virtual ObjectState * saveState() const
Definition SlotField.cc:388
bool hasNoOperationEncoding() const
Definition SlotField.cc:267
void removeSocketEncoding(SocketEncoding &encoding)
Definition SlotField.cc:143
void clearSocketEncodings()
Definition SlotField.cc:413
NOPEncoding & noOperationEncoding() const
Definition SlotField.cc:281
BinaryEncoding::Position componentIDPos_
Position of the socket and bridge IDs within the field.
Definition SlotField.hh:109
virtual void loadState(const ObjectState *state)
Definition SlotField.cc:358
BinaryEncoding::Position componentIDPosition() const
Definition SlotField.cc:296
void setNoOperationEncoding(NOPEncoding &encoding)
Definition SlotField.cc:234
virtual ~SlotField()
Definition SlotField.cc:88
void unsetNoOperationEncoding()
Definition SlotField.cc:253
void clearNoOperationEncoding()
Definition SlotField.cc:422
static const std::string OSKEY_COMPONENT_ID_POSITION
ObjectState attribute key for component ID position.
Definition SlotField.hh:91
virtual int childFieldCount() const
Definition SlotField.cc:333
int socketEncodingCount() const
Definition SlotField.cc:156
virtual ObjectState * saveState() const
virtual int width() const
static const std::string OSNAME_SOCKET_ENCODING
ObjectState name for socket encoding.
std::string socketName() const
SlotField * parent() const