OpenASIP  2.0
LImmDstRegisterField.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 LImmDstRegisterField.cc
26  *
27  * Implementation of LImmDstRegisterField 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 "LImmDstRegisterField.hh"
36 #include "BinaryEncoding.hh"
37 #include "MapTools.hh"
38 #include "Application.hh"
39 #include "ObjectState.hh"
40 
41 using std::string;
42 
44  "dst_reg_field";
45 const std::string LImmDstRegisterField::OSKEY_WIDTH = "width";
46 const std::string LImmDstRegisterField::OSNAME_IU_DESTINATION = "iu_dst";
47 const std::string LImmDstRegisterField::OSKEY_ITEMP = "itemp";
48 const std::string LImmDstRegisterField::OSKEY_DST_IU = "iu";
49 
50 /**
51  * The constructor.
52  *
53  * Registers the field automatically to the given parent.
54  *
55  * @param width Width of the field.
56  * @param parent The parent BinaryEncoding instance.
57  * @exception OutOfRange If the given width is 0 or smaller.
58  */
60  : InstructionField(&parent), width_(width) {
61  if (width < 1) {
62  throw OutOfRange(__FILE__, __LINE__, __func__);
63  }
64 
65  setParent(NULL);
67  setParent(&parent);
68 }
69 
70 /**
71  * The constructor.
72  *
73  * Loads the state of the object from the given ObjectState instance.
74  * Registers the field automatically to the given parent.
75  *
76  * @param state The ObjectState instance.
77  * @param parent The parent BinaryEncoding instance.
78  * @exception ObjectStateLoadingException If the given ObjectState instance
79  * is erroneous.
80  */
82  const ObjectState* state, BinaryEncoding& parent)
83  : InstructionField(state, &parent), width_(0) {
84  loadState(state);
85  setParent(NULL);
87  setParent(&parent);
88 }
89 
90 /**
91  * The destructor.
92  */
94  BinaryEncoding* parent = this->parent();
95  setParent(NULL);
97 }
98 
99 
100 /**
101  * Returns the parent BinaryEncoding instance.
102  *
103  * @return The parent BinaryEncoding instance.
104  */
108  if (parent != NULL) {
109  BinaryEncoding* bem = dynamic_cast<BinaryEncoding*>(parent);
110  assert(bem != NULL);
111  return bem;
112  } else {
113  return NULL;
114  }
115 }
116 
117 
118 /**
119  * Sets that the register index of the given immediate unit in the given
120  * instruction template is given in this field.
121  *
122  * @param instructionTemplate The instruction template.
123  * @param immediateUnit The immediate unit.
124  * @exception NotAvailable If index of some other immediate unit is assigned
125  * to this field in the same instruction template.
126  *
127  */
128 void
130  const std::string& instructionTemplate, const std::string& immediateUnit) {
132  MapTools::valueForKey<string>(destinationMap_, instructionTemplate)
133  != immediateUnit) {
134  throw NotAvailable(__FILE__, __LINE__, __func__);
135  } else {
136  destinationMap_.insert(
137  std::pair<string, string>(instructionTemplate, immediateUnit));
138  }
139 }
140 
141 /**
142  * Returns the number of instruction templates that use this destination
143  * register field.
144  *
145  * @return The number of instruction templates.
146  */
147 int
149  return destinationMap_.size();
150 }
151 
152 
153 /**
154  * By the given index, returns an instruction template that uses this
155  * destination register field.
156  *
157  * @param index The index.
158  * @exception OutOfRange If the index is negative or not smaller than the
159  * number of instruction templates.
160  */
161 std::string
163  if (index < 0 || index >= instructionTemplateCount()) {
164  throw OutOfRange(__FILE__, __LINE__, __func__);
165  }
166 
167  StringMap::const_iterator iter = destinationMap_.begin();
168  for (int i = 0; i < index; i++) {
169  iter++;
170  }
171 
172  return iter->first;
173 }
174 
175 /**
176  * Tells whether the field is used by the given instruction template.
177  *
178  * @param instructionTemplate The instruction template.
179  */
180 bool
182  const std::string& instructionTemplate) const {
183 
185 }
186 
187 
188 /**
189  * Returns the name of the immediate unit for which the field is assigned
190  * in the given instruction template.
191  *
192  * @param instructionTemplate Name of the instruction template.
193  * @exception NotAvailable If the field is not used in the given instruction
194  * template.
195  */
196 std::string
198  const std::string& instructionTemplate) const {
199  try {
200  return MapTools::valueForKey<string>(
202  } catch (const Exception&) {
203  throw NotAvailable(__FILE__, __LINE__, __func__);
204  }
205 }
206 
207 /**
208  * Returns the width of the field.
209  *
210  * @return The bit width of the field.
211  */
212 int
214  return width_;
215 }
216 
217 
218 /**
219  * Returns the number of child fields, that is always 0.
220  *
221  * @return 0.
222  */
223 int
225  return 0;
226 }
227 
228 
229 /**
230  * Saves the state of the object to an ObjectState instance.
231  *
232  * @return The newly created ObjectState instance.
233  */
236 
239  state->setAttribute(OSKEY_WIDTH, width());
240 
241  // add destinations
242  for (StringMap::const_iterator iter = destinationMap_.begin();
243  iter != destinationMap_.end(); iter++) {
245  state->addChild(dstState);
246  dstState->setAttribute(OSKEY_ITEMP, iter->first);
247  dstState->setAttribute(OSKEY_DST_IU, iter->second);
248  }
249 
250  return state;
251 }
252 
253 
254 /**
255  * Loads the state of the object from the given ObjectState instance.
256  *
257  * @param state The ObjectState instance.
258  */
259 void
261  if (state->name() != OSNAME_LIMM_DST_REGISTER_FIELD) {
262  throw ObjectStateLoadingException(__FILE__, __LINE__, __func__);
263  }
264 
266 
267  try {
268  width_ = state->intAttribute(OSKEY_WIDTH);
269  for (int i = 0; i < state->childCount(); i++) {
270  ObjectState* child = state->child(i);
271  if (child->name() != OSNAME_IU_DESTINATION) {
273  __FILE__, __LINE__, __func__);
274  }
275  string iTemp = child->stringAttribute(OSKEY_ITEMP);
276  string iu = child->stringAttribute(OSKEY_DST_IU);
277  addDestination(iTemp, iu);
278  }
279  } catch (const Exception& e) {
281  __FILE__, __LINE__, __func__, e.errorMessage());
282  }
283 }
LImmDstRegisterField::loadState
void loadState(const ObjectState *state)
Definition: LImmDstRegisterField.cc:260
BinaryEncoding
Definition: BinaryEncoding.hh:61
LImmDstRegisterField::destinationMap_
StringMap destinationMap_
Maps instruction templates to immediate units.
Definition: LImmDstRegisterField.hh:89
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
ObjectStateLoadingException
Definition: Exception.hh:551
OutOfRange
Definition: Exception.hh:320
MapTools.hh
LImmDstRegisterField::instructionTemplateCount
int instructionTemplateCount() const
Definition: LImmDstRegisterField.cc:148
ObjectState
Definition: ObjectState.hh:59
LImmDstRegisterField::saveState
virtual ObjectState * saveState() const
Definition: LImmDstRegisterField.cc:235
ObjectState::setName
void setName(const std::string &name)
NotAvailable
Definition: Exception.hh:728
InstructionField
Definition: InstructionField.hh:43
InstructionField::saveState
virtual ObjectState * saveState() const
Definition: InstructionField.cc:268
assert
#define assert(condition)
Definition: Application.hh:86
LImmDstRegisterField::OSKEY_DST_IU
static const std::string OSKEY_DST_IU
ObjectState attribute key for the name of the immediate unit.
Definition: LImmDstRegisterField.hh:78
LImmDstRegisterField::instructionTemplate
std::string instructionTemplate(int index) const
Definition: LImmDstRegisterField.cc:162
LImmDstRegisterField::LImmDstRegisterField
LImmDstRegisterField(int width, BinaryEncoding &parent)
Definition: LImmDstRegisterField.cc:59
BinaryEncoding::addLongImmDstRegisterField
void addLongImmDstRegisterField(LImmDstRegisterField &field)
Definition: BinaryEncoding.cc:459
LImmDstRegisterField::width_
int width_
Width of the field.
Definition: LImmDstRegisterField.hh:87
BinaryEncoding.hh
Application.hh
InstructionField::setParent
void setParent(InstructionField *parent)
Definition: InstructionField.cc:282
InstructionField::parent
InstructionField * parent() const
Definition: InstructionField.cc:100
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
LImmDstRegisterField::immediateUnit
std::string immediateUnit(const std::string &instructionTemplate) const
Definition: LImmDstRegisterField.cc:197
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
ObjectState::childCount
int childCount() const
LImmDstRegisterField::OSKEY_WIDTH
static const std::string OSKEY_WIDTH
ObjectState attribute key for the width of the field.
Definition: LImmDstRegisterField.hh:72
LImmDstRegisterField::width
virtual int width() const
Definition: LImmDstRegisterField.cc:213
Exception
Definition: Exception.hh:54
ObjectState::name
std::string name() const
LImmDstRegisterField::OSNAME_IU_DESTINATION
static const std::string OSNAME_IU_DESTINATION
ObjectState name for a immediate unit destination.
Definition: LImmDstRegisterField.hh:74
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
LImmDstRegisterField::OSNAME_LIMM_DST_REGISTER_FIELD
static const std::string OSNAME_LIMM_DST_REGISTER_FIELD
ObjectState name for long immediate destination register field.
Definition: LImmDstRegisterField.hh:70
LImmDstRegisterField::usedByInstructionTemplate
bool usedByInstructionTemplate(const std::string &instructionTemplate) const
Definition: LImmDstRegisterField.cc:181
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
LImmDstRegisterField::~LImmDstRegisterField
virtual ~LImmDstRegisterField()
Definition: LImmDstRegisterField.cc:93
LImmDstRegisterField::childFieldCount
virtual int childFieldCount() const
Definition: LImmDstRegisterField.cc:224
LImmDstRegisterField::addDestination
void addDestination(const std::string &instructionTemplate, const std::string &immediateUnit)
Definition: LImmDstRegisterField.cc:129
InstructionField::loadState
virtual void loadState(const ObjectState *state)
Definition: InstructionField.cc:242
LImmDstRegisterField::OSKEY_ITEMP
static const std::string OSKEY_ITEMP
ObjectState attribute key for the name of the instruction template.
Definition: LImmDstRegisterField.hh:76
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
BinaryEncoding::removeLongImmDstRegisterField
void removeLongImmDstRegisterField(LImmDstRegisterField &field)
Definition: BinaryEncoding.cc:475
LImmDstRegisterField::parent
BinaryEncoding * parent() const
Definition: LImmDstRegisterField.cc:106
LImmDstRegisterField.hh
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100