OpenASIP 2.2
Loading...
Searching...
No Matches
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"
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
46using std::string;
47using boost::format;
48
49const 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) {
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);
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);
103}
104
105/**
106 * The destructor.
107 */
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 */
129void
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 */
153void
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 */
168bool
169SourceField::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 */
191SourceField::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 */
208int
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 */
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 */
242void
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 */
264void
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 */
278bool
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 */
307int
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 */
335void
337 const string procName = "SourceField::loadState";
338
339 if (state->name() != OSNAME_SOURCE_FIELD) {
340 throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
341 }
342
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++) {
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 */
395void
399
400
401/**
402 * Deletes the immediate encoding if one exists.
403 */
404void
#define assert(condition)
static bool canAddComponentEncoding(SlotField &field, unsigned int encoding, unsigned int extraBits)
Definition BEMTester.cc:73
int moveSlotCount() const
MoveSlot & moveSlot(int index) const
static const std::string OSNAME_BRIDGE_ENCODING
ObjectState name for bridge encoding.
std::string bridgeName() const
SourceField * parent() const
virtual ObjectState * saveState() const
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
SourceField * parent() const
static const std::string OSNAME_IMM_ENCODING
ObjectState name for immediate encoding.
virtual int width() const
void setParent(InstructionField *parent)
BinaryEncoding * parent() const
Definition MoveSlot.cc:118
SourceField & sourceField() const
Definition MoveSlot.cc:277
void setSourceField(SourceField &field)
Definition MoveSlot.cc:233
void unsetSourceField()
Definition MoveSlot.cc:251
std::string name() const
Definition MoveSlot.cc:136
bool hasSourceField() const
Definition MoveSlot.cc:264
static NullBridgeEncoding & instance()
static NullImmediateEncoding & instance()
void setName(const std::string &name)
ObjectState * child(int index) const
void addChild(ObjectState *child)
std::string name() const
int childCount() const
static void deleteAllItems(SequenceType &aSequence)
MoveSlot * parent() const
Definition SlotField.cc:98
virtual int width() const
Definition SlotField.cc:307
virtual ObjectState * saveState() const
Definition SlotField.cc:388
virtual void loadState(const ObjectState *state)
Definition SlotField.cc:358
BinaryEncoding::Position componentIDPosition() const
Definition SlotField.cc:296
ImmediateEncoding * immEncoding_
The immediate encoding.
BridgeEncodingTable bridgeEncodings_
Container for bridge encodings.
void clearBridgeEncodings()
int bridgeEncodingCount() const
virtual void loadState(const ObjectState *state)
void unsetImmediateEncoding()
static const std::string OSNAME_SOURCE_FIELD
ObjectState name for source field.
void addBridgeEncoding(BridgeEncoding &encoding)
bool hasBridgeEncoding(const std::string &bridge) const
virtual int width() const
void removeBridgeEncoding(BridgeEncoding &encoding)
SourceField(BinaryEncoding::Position componentIDPos, MoveSlot &parent)
void clearImmediateEncoding()
virtual ~SourceField()
ImmediateEncoding & immediateEncoding() const
void setImmediateEncoding(ImmediateEncoding &encoding)
bool hasImmediateEncoding() const
virtual ObjectState * saveState() const
BridgeEncoding & bridgeEncoding(const std::string &bridge) const
virtual boost::format text(int textId)