OpenASIP  2.0
ImmediateSlotField.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 ImmediateSlotField.cc
26  *
27  * Implementation of ImmediateSlotField 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 "Application.hh"
36 #include "ImmediateSlotField.hh"
37 #include "BinaryEncoding.hh"
38 #include "ObjectState.hh"
39 
40 using std::string;
41 
43  "imm_slot_field";
44 const std::string ImmediateSlotField::OSKEY_NAME = "name";
45 const std::string ImmediateSlotField::OSKEY_WIDTH = "width";
46 
47 /**
48  * The constructor.
49  *
50  * @param name Name of the immediate slot programmed by this field.
51  * @param width Bit width of the field.
52  * @parent The parent binary encoding map.
53  * @exception OutOfRange If the bit width is smaller than 1.
54  * @exception ObjectAlreadyExists If the parent binary encoding already has
55  * an immediate slot with the given name.
56  */
58  const std::string& name, int width, BinaryEncoding& parent)
59  : InstructionField(&parent), name_(name), width_(width) {
60  if (width < 1) {
61  const string procName = "ImmediateSlotField::ImmediateSlotField";
62  throw OutOfRange(__FILE__, __LINE__, procName);
63  }
64 
65  setParent(NULL);
66  parent.addImmediateSlot(*this);
67  setParent(&parent);
68 }
69 
70 /**
71  * The constructor.
72  *
73  * Loads the state of the object from the given ObjectState instance.
74  *
75  * @param state The ObjectState instance.
76  * @exception ObjectStateLoadingException If an error occurs while loading
77  * the state.
78  */
80  const ObjectState* state, BinaryEncoding& parent)
81  : InstructionField(state, &parent), name_(""), width_(0) {
82  loadState(state);
83  setParent(NULL);
84  parent.addImmediateSlot(*this);
85  setParent(&parent);
86 }
87 
88 /**
89  * The destructor.
90  */
92  BinaryEncoding* parent = this->parent();
93  assert(parent != NULL);
94  setParent(NULL);
96 }
97 
98 
99 /**
100  * Returns the parent binary encoding map.
101  *
102  * @return The parent binary encoding map.
103  */
107  if (parent == NULL) {
108  return NULL;
109  } else {
110  BinaryEncoding* bemParent = dynamic_cast<BinaryEncoding*>(parent);
111  assert(bemParent != NULL);
112  return bemParent;
113  }
114 }
115 
116 
117 /**
118  * Returns the name of the immediate slot programmed by this field.
119  *
120  * @return The name of the immediate slot.
121  */
122 std::string
124  return name_;
125 }
126 
127 
128 /**
129  * Sets the name of the immediate slot programmed by this field.
130  *
131  * @param name The name.
132  * @exception ObjectAlreadyExists If the parent binary encoding has an
133  * immediate slot field for the given
134  * immediate slot already.
135  */
136 void
137 ImmediateSlotField::setName(const std::string& name) {
138  if (name == this->name()) {
139  return;
140  }
141 
142  if (parent()->hasImmediateSlot(name)) {
143  const string procName = "ImmediateSlotField::setName";
144  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
145  } else {
146  name_ = name;
147  }
148 }
149 
150 /**
151  * Returns 0 always since immediate slot does not have any child fields.
152  *
153  * @return 0.
154  */
155 int
157  return 0;
158 }
159 
160 
161 /**
162  * Returns the bit width of the field.
163  *
164  * @return The bit width.
165  */
166 int
168  return width_;
169 }
170 
171 
172 /**
173  * Sets the bit width of the field.
174  *
175  * @param width The new bit width.
176  * @exception OutOfRange If the given width is smaller than 1.
177  */
178 void
180  if (width < 1) {
181  const string procName = "ImmediateSlotField::setWidth";
182  throw OutOfRange(__FILE__, __LINE__, procName);
183  }
184  width_ = width;
185 }
186 
187 /**
188  * Saves the state of the object to an ObjectState instance.
189  *
190  * @return The newly created ObjectState instance.
191  */
196  state->setAttribute(OSKEY_NAME, name());
197  state->setAttribute(OSKEY_WIDTH, width());
198  return state;
199 }
200 
201 
202 /**
203  * Loads the state of the object from the given ObjectState instance.
204  *
205  * @param state The ObjectState instance.
206  * @exception ObjectStateLoadingException If an error occurs while loading
207  * the state.
208  */
209 void
211  const string procName = "ImmediateSlotField::loadState";
212 
213  if (state->name() != OSNAME_IMMEDIATE_SLOT_FIELD) {
214  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
215  }
216 
218 
219  try {
222  } catch (const Exception&) {
223  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
224  }
225 }
BinaryEncoding
Definition: BinaryEncoding.hh:61
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
ObjectStateLoadingException
Definition: Exception.hh:551
ImmediateSlotField::saveState
virtual ObjectState * saveState() const
Definition: ImmediateSlotField.cc:193
OutOfRange
Definition: Exception.hh:320
ImmediateSlotField::OSNAME_IMMEDIATE_SLOT_FIELD
static const std::string OSNAME_IMMEDIATE_SLOT_FIELD
ObjectState name for immediate slot field.
Definition: ImmediateSlotField.hh:63
ImmediateSlotField.hh
ObjectState
Definition: ObjectState.hh:59
ObjectState::setName
void setName(const std::string &name)
ImmediateSlotField::ImmediateSlotField
ImmediateSlotField(const std::string &name, int width, BinaryEncoding &parent)
Definition: ImmediateSlotField.cc:57
InstructionField
Definition: InstructionField.hh:43
ImmediateSlotField::setWidth
void setWidth(int width)
Definition: ImmediateSlotField.cc:179
InstructionField::saveState
virtual ObjectState * saveState() const
Definition: InstructionField.cc:268
ImmediateSlotField::loadState
virtual void loadState(const ObjectState *state)
Definition: ImmediateSlotField.cc:210
assert
#define assert(condition)
Definition: Application.hh:86
ImmediateSlotField::parent
BinaryEncoding * parent() const
Definition: ImmediateSlotField.cc:105
BinaryEncoding.hh
Application.hh
InstructionField::setParent
void setParent(InstructionField *parent)
Definition: InstructionField.cc:282
InstructionField::parent
InstructionField * parent() const
Definition: InstructionField.cc:100
ObjectState.hh
ImmediateSlotField::OSKEY_NAME
static const std::string OSKEY_NAME
ObjectState attribute key for the name of the immediate slot.
Definition: ImmediateSlotField.hh:65
Exception
Definition: Exception.hh:54
ImmediateSlotField::childFieldCount
virtual int childFieldCount() const
Definition: ImmediateSlotField.cc:156
ImmediateSlotField::OSKEY_WIDTH
static const std::string OSKEY_WIDTH
ObjectState attribute key for the width of the field.
Definition: ImmediateSlotField.hh:67
ImmediateSlotField::width
virtual int width() const
Definition: ImmediateSlotField.cc:167
ObjectState::name
std::string name() const
ImmediateSlotField::setName
void setName(const std::string &name)
Definition: ImmediateSlotField.cc:137
BinaryEncoding::removeImmediateSlot
void removeImmediateSlot(ImmediateSlotField &slot)
Definition: BinaryEncoding.cc:320
ImmediateSlotField::name
std::string name() const
Definition: ImmediateSlotField.cc:123
ObjectAlreadyExists
Definition: Exception.hh:1002
ImmediateSlotField::name_
std::string name_
Name of the immediate slot.
Definition: ImmediateSlotField.hh:71
InstructionField::loadState
virtual void loadState(const ObjectState *state)
Definition: InstructionField.cc:242
ImmediateSlotField::~ImmediateSlotField
virtual ~ImmediateSlotField()
Definition: ImmediateSlotField.cc:91
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
BinaryEncoding::addImmediateSlot
void addImmediateSlot(ImmediateSlotField &slot)
Definition: BinaryEncoding.cc:300
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
ImmediateSlotField::width_
int width_
The bit width of the field.
Definition: ImmediateSlotField.hh:73