OpenASIP  2.0
ImmediateEncoding.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 ImmediateEncoding.cc
26  *
27  * Implementation of ImmediateEncoding 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 "ImmediateEncoding.hh"
36 #include "SourceField.hh"
37 #include "Application.hh"
38 #include "ObjectState.hh"
39 
40 using std::string;
41 
42 const std::string ImmediateEncoding::OSNAME_IMM_ENCODING = "imm_encoding";
43 const std::string ImmediateEncoding::OSKEY_IMM_WIDTH = "imm_width";
44 
45 /**
46  * The constructor.
47  *
48  * Registers the encoding automatically to the parent source field.
49  *
50  * @param encoding The encoding for inline immediates.
51  * @param extraBits The number of extra bits in the encoding.
52  * @param immediateWidth Width of the inline immediate.
53  * @param parent The parent source field.
54  * @exception ObjectAlreadyExists If the given source field has an immediate
55  * encoding already or if the encoding is
56  * ambiguous with some socket or bridge
57  * encoding in the source field.
58  * @exception OutOfRange If the given immediate width is negative.
59  */
61  unsigned int encoding, unsigned int extraBits, int immediateWidth,
62  SourceField& parent)
63  : Encoding(encoding, extraBits, NULL), immediateWidth_(immediateWidth) {
64  if (immediateWidth < 0) {
65  throw OutOfRange(__FILE__, __LINE__, __func__);
66  }
67 
69  setParent(&parent);
70 }
71 
72 /**
73  * The constructor.
74  *
75  * Loads the state of the object from the given ObjectState instance.
76  *
77  * @param state The ObjectState instance.
78  * @param parent The parent source field.
79  * @exception ObjectAlreadyExists If the given source field has an immediate
80  * encoding already or if the encoding is
81  * ambiguous with some socket or bridge
82  * encoding in the source field.
83  * @exception ObjectStateLoadingException If the given ObjectState instance
84  * is erroneous.
85  */
87  const ObjectState* state, SourceField& parent)
88  : Encoding(state, NULL) {
89  const string procName = "ImmediateEncoding::ImmediateEncoding";
90 
91  if (state->name() != OSNAME_IMM_ENCODING) {
92  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
93  }
94 
95  try {
97  if (immediateWidth_ < 0) {
98  throw OutOfRange(__FILE__, __LINE__, __func__);
99  }
100  } catch (const Exception& e) {
102  __FILE__, __LINE__, __func__, e.errorMessage());
103  }
104 
106  setParent(&parent);
107 }
108 
109 /**
110  * The destructor.
111  */
113  SourceField* oldParent = parent();
114  assert(oldParent != NULL);
115  setParent(NULL);
116  oldParent->unsetImmediateEncoding();
117 }
118 
119 
120 /**
121  * Returns the parent source field.
122  *
123  * @return The parent.
124  */
128  if (parent != NULL) {
129  SourceField* sField = dynamic_cast<SourceField*>(parent);
130  assert(sField != NULL);
131  return sField;
132  } else {
133  return NULL;
134  }
135 }
136 
137 
138 /**
139  * Returns the immediate width.
140  *
141  * @return The immediate width.
142  */
143 int
145  return immediateWidth_;
146 }
147 
148 
149 /**
150  * Returns the encoding width.
151  *
152  * @return The encoding width.
153  */
154 int
156  return Encoding::width();
157 }
158 
159 
160 /**
161  * Returns the position of the immediate encoding.
162  *
163  * @return The position of the immediate encoding.
164  */
165 int
167  if (parent()->componentIDPosition() == BinaryEncoding::LEFT) {
168  return parent()->width() - parent()->extraBits() - encodingWidth();
169  } else {
170  return 0;
171  }
172 }
173 
174 
175 /**
176  * Returns the position of the short immediate within the source field.
177  *
178  * @return The position of the short immediate.
179  */
180 int
182  if (parent()->componentIDPosition() == BinaryEncoding::LEFT) {
183  return 0;
184  } else {
185  return parent()->width() - parent()->extraBits() - immediateWidth();
186  }
187 }
188 
189 
190 /**
191  * Returns always 0.
192  *
193  * @return The position of the encoding.
194  */
195 int
197  return 0;
198 }
199 
200 
201 /**
202  * Returns the width of the encoding + immediate width.
203  *
204  * @return The width.
205  */
206 int
208  return immediateWidth() + encodingWidth();
209 }
210 
211 
212 /**
213  * Saves the state of the object to an ObjectState instance.
214  *
215  * @return The newly created ObjectState instance.
216  */
219  ObjectState* state = Encoding::saveState();
222  return state;
223 }
SourceField::setImmediateEncoding
void setImmediateEncoding(ImmediateEncoding &encoding)
Definition: SourceField.cc:243
ImmediateEncoding::saveState
virtual ObjectState * saveState() const
Definition: ImmediateEncoding.cc:218
ObjectStateLoadingException
Definition: Exception.hh:551
ImmediateEncoding::OSNAME_IMM_ENCODING
static const std::string OSNAME_IMM_ENCODING
ObjectState name for immediate encoding.
Definition: ImmediateEncoding.hh:63
SourceField::unsetImmediateEncoding
void unsetImmediateEncoding()
Definition: SourceField.cc:265
OutOfRange
Definition: Exception.hh:320
Encoding::setParent
void setParent(InstructionField *parent)
Definition: Encoding.cc:155
Encoding::parent
InstructionField * parent() const
Definition: Encoding.cc:97
ImmediateEncoding::parent
SourceField * parent() const
Definition: ImmediateEncoding.cc:126
ObjectState
Definition: ObjectState.hh:59
ImmediateEncoding.hh
ImmediateEncoding::bitPosition
virtual int bitPosition() const
Definition: ImmediateEncoding.cc:196
SourceField.hh
ObjectState::setName
void setName(const std::string &name)
ImmediateEncoding::width
virtual int width() const
Definition: ImmediateEncoding.cc:207
ImmediateEncoding::immediateWidth_
int immediateWidth_
Definition: ImmediateEncoding.hh:68
InstructionField
Definition: InstructionField.hh:43
Encoding
Definition: Encoding.hh:46
assert
#define assert(condition)
Definition: Application.hh:86
Encoding::width
virtual int width() const
Definition: Encoding.cc:130
ImmediateEncoding::~ImmediateEncoding
virtual ~ImmediateEncoding()
Definition: ImmediateEncoding.cc:112
ImmediateEncoding::OSKEY_IMM_WIDTH
static const std::string OSKEY_IMM_WIDTH
ObjectState attribute key for the immediate width.
Definition: ImmediateEncoding.hh:65
Application.hh
BinaryEncoding::LEFT
@ LEFT
Definition: BinaryEncoding.hh:64
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
SourceField::width
virtual int width() const
Definition: SourceField.cc:308
Exception
Definition: Exception.hh:54
ObjectState::name
std::string name() const
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
ImmediateEncoding::immediatePosition
int immediatePosition() const
Definition: ImmediateEncoding.cc:181
ImmediateEncoding::immediateWidth
int immediateWidth() const
Definition: ImmediateEncoding.cc:144
Encoding::saveState
virtual ObjectState * saveState() const
Definition: Encoding.cc:141
InstructionField::extraBits
int extraBits() const
Definition: InstructionField.cc:229
ImmediateEncoding::encodingWidth
int encodingWidth() const
Definition: ImmediateEncoding.cc:155
ImmediateEncoding::encodingPosition
int encodingPosition() const
Definition: ImmediateEncoding.cc:166
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
ImmediateEncoding::ImmediateEncoding
ImmediateEncoding(unsigned int encoding, unsigned int extraBits, int immediateWidth, SourceField &parent)
Definition: ImmediateEncoding.cc:60
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100
SourceField
Definition: SourceField.hh:48