OpenASIP 2.2
Loading...
Searching...
No Matches
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"
44#include "Application.hh"
45#include "ObjectState.hh"
46#include "BEMTester.hh"
47
48using std::string;
49
50const std::string MoveSlot::OSNAME_MOVE_SLOT = "move_slot";
51const 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 */
64MoveSlot::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);
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);
95}
96
97/**
98 * The destructor.
99 */
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 */
135std::string
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 */
148void
149MoveSlot::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 */
170void
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 */
188void
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 */
201bool
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 */
216 if (hasGuardField()) {
217 return *guardField_;
218 } else {
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 */
232void
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 */
250void
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 */
263bool
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 {
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 */
295void
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 */
313void
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 */
326bool
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 */
356int
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 */
380MoveSlot::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 */
405int
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 */
431void
436
437 ObjectState* newState = new ObjectState(*state);
438 reorderSubfields(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
474
475 if (hasGuardField()) {
476 state->addChild(guardField().saveState());
477 }
478 if (hasSourceField()) {
479 state->addChild(sourceField().saveState());
480 }
481 if (hasDestinationField()) {
483 }
484
485 return state;
486}
487
488
489/**
490 * Deletes the guard field of the move slot.
491 */
492void
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 */
504void
506 if (sourceField_ != NULL) {
507 delete sourceField_;
508 sourceField_ = NULL;
509 }
510}
511
512/**
513 * Deletes the destination field of the move slot.
514 */
515void
517 if (destinationField_ != NULL) {
518 delete destinationField_;
519 destinationField_ = NULL;
520 }
521}
#define assert(condition)
void removeMoveSlot(MoveSlot &slot)
void addMoveSlot(MoveSlot &slot)
static const std::string OSNAME_DESTINATION_FIELD
ObjectState name for destination field.
std::string errorMessage() const
Definition Exception.cc:123
virtual int width() const
MoveSlot * parent() const
static const std::string OSNAME_GUARD_FIELD
ObjectState name for guard field.
Definition GuardField.hh:99
InstructionField * parent() const
virtual void loadState(const ObjectState *state)
void setParent(InstructionField *parent)
static void reorderSubfields(ObjectState *state)
virtual ObjectState * saveState() const
virtual InstructionField & childField(int position) const
int relativePosition() const
void deleteSourceField()
Definition MoveSlot.cc:505
BinaryEncoding * parent() const
Definition MoveSlot.cc:118
virtual ~MoveSlot()
Definition MoveSlot.cc:100
SourceField & sourceField() const
Definition MoveSlot.cc:277
virtual ObjectState * saveState() const
Definition MoveSlot.cc:469
void setName(const std::string &name)
Definition MoveSlot.cc:149
virtual InstructionField & childField(int position) const
Definition MoveSlot.cc:380
void deleteGuardField()
Definition MoveSlot.cc:493
static const std::string OSKEY_BUS_NAME
ObjectState attribute key for the name of the bus.
Definition MoveSlot.hh:98
void setSourceField(SourceField &field)
Definition MoveSlot.cc:233
void unsetSourceField()
Definition MoveSlot.cc:251
std::string name_
The bus name.
Definition MoveSlot.hh:106
void setGuardField(GuardField &field)
Definition MoveSlot.cc:171
DestinationField & destinationField() const
Definition MoveSlot.cc:341
virtual int width() const
Definition MoveSlot.cc:406
MoveSlot(const std::string &busName, BinaryEncoding &parent)
Definition MoveSlot.cc:64
GuardField * guardField_
The guard field.
Definition MoveSlot.hh:108
std::string name() const
Definition MoveSlot.cc:136
GuardField & guardField() const
Definition MoveSlot.cc:215
void unsetDestinationField()
Definition MoveSlot.cc:314
DestinationField * destinationField_
The destination field.
Definition MoveSlot.hh:112
void deleteDestinationField()
Definition MoveSlot.cc:516
void setDestinationField(DestinationField &field)
Definition MoveSlot.cc:296
bool hasSourceField() const
Definition MoveSlot.cc:264
static const std::string OSNAME_MOVE_SLOT
ObjectState name for move slot.
Definition MoveSlot.hh:96
virtual void loadState(const ObjectState *state)
Definition MoveSlot.cc:432
void unsetGuardField()
Definition MoveSlot.cc:189
virtual int childFieldCount() const
Definition MoveSlot.cc:357
bool hasDestinationField() const
Definition MoveSlot.cc:327
bool hasGuardField() const
Definition MoveSlot.cc:202
SourceField * sourceField_
The source field.
Definition MoveSlot.hh:110
static NullDestinationField & instance()
static NullGuardField & instance()
static NullInstructionField & instance()
static NullSourceField & 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)
std::string stringAttribute(const std::string &name) const
std::string name() const
int childCount() const
MoveSlot * parent() const
Definition SlotField.cc:98
virtual int width() const
Definition SlotField.cc:307
static const std::string OSNAME_SOURCE_FIELD
ObjectState name for source field.
virtual int width() const