OpenASIP 2.2
Loading...
Searching...
No Matches
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
40using std::string;
41
42const std::string ImmediateEncoding::OSNAME_IMM_ENCODING = "imm_encoding";
43const 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
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
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 */
143int
147
148
149/**
150 * Returns the encoding width.
151 *
152 * @return The encoding width.
153 */
154int
158
159
160/**
161 * Returns the position of the immediate encoding.
162 *
163 * @return The position of the immediate encoding.
164 */
165int
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 */
180int
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 */
195int
197 return 0;
198}
199
200
201/**
202 * Returns the width of the encoding + immediate width.
203 *
204 * @return The width.
205 */
206int
210
211
212/**
213 * Saves the state of the object to an ObjectState instance.
214 *
215 * @return The newly created ObjectState instance.
216 */
222 return state;
223}
#define __func__
#define assert(condition)
virtual ObjectState * saveState() const
Definition Encoding.cc:141
InstructionField * parent() const
Definition Encoding.cc:97
virtual int width() const
Definition Encoding.cc:130
void setParent(InstructionField *parent)
Definition Encoding.cc:155
std::string errorMessage() const
Definition Exception.cc:123
SourceField * parent() const
virtual int bitPosition() const
static const std::string OSNAME_IMM_ENCODING
ObjectState name for immediate encoding.
virtual int width() const
static const std::string OSKEY_IMM_WIDTH
ObjectState attribute key for the immediate width.
ImmediateEncoding(unsigned int encoding, unsigned int extraBits, int immediateWidth, SourceField &parent)
virtual ObjectState * saveState() const
int immediatePosition() const
void setName(const std::string &name)
void setAttribute(const std::string &name, const std::string &value)
int intAttribute(const std::string &name) const
std::string name() const
void unsetImmediateEncoding()
virtual int width() const
void setImmediateEncoding(ImmediateEncoding &encoding)