OpenASIP  2.0
PortCode.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2014 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 PortCode.cc
26  *
27  * Implementation of PortCode class.
28  *
29  * @author Lasse Laasonen 2005 (lasse.laasonen-no.spam-tut.fi)
30  * @author Pekka Jääskeläinen 2014
31  * @note rating: red
32  */
33 
34 #include "PortCode.hh"
35 #include "ObjectState.hh"
36 #include "MathTools.hh"
37 
38 const std::string PortCode::OSNAME_PORT_CODE = "port_code";
39 const std::string PortCode::OSKEY_UNIT_NAME = "unit_name";
40 const std::string PortCode::OSKEY_ENCODING = "encoding";
41 const std::string PortCode::OSKEY_EXTRA_BITS = "extra_bits";
42 const std::string PortCode::OSKEY_INDEX_WIDTH = "index_width";
43 const std::string PortCode::OSKEY_MAX_INDEX = "max-index";
44 
45 /**
46  * The constructor.
47  *
48  * Creates a port code with port ID.
49  *
50  * @param unitName Name of the unit.
51  * @param encoding The encoding.
52  * @param extraBits The number of extra bits in the encoding.
53  * @param indexWidth The width of the register index.
54  * @exception OutOfRange If the some of the given values is negative.
55  */
57  const std::string& unitName, unsigned int encoding,
58  unsigned int extraBits, int indexWidth)
59  : unitName_(unitName),
60  encoding_(encoding),
61  extraBits_(extraBits),
62  indexWidth_(indexWidth),
63  hasEncoding_(true),
64  parent_(NULL),
65  maxRegIndex_(0) {
66  if (indexWidth_ < 0) {
67  throw OutOfRange(__FILE__, __LINE__, __func__);
68  }
69 }
70 
71 /**
72  * The constructor.
73  *
74  * Creates a port code without port ID.
75  *
76  * @param unitName Name of the unit.
77  * @param indexWidth The width of the register index.
78  * @exception OutOfRange If the some of the given values is negative.
79  */
80 PortCode::PortCode(const std::string& unitName, int indexWidth)
81  : unitName_(unitName),
82  encoding_(0),
83  extraBits_(0),
84  indexWidth_(indexWidth),
85  hasEncoding_(false),
86  parent_(NULL),
87  maxRegIndex_(0) {
88  if (indexWidth_ < 0) {
89  throw OutOfRange(__FILE__, __LINE__, __func__);
90  }
91 }
92 
93 /**
94  * The constructor.
95  *
96  * Loads the state of the object from the given ObjectState instance.
97  *
98  * @param state The ObjectState instance.
99  * @exception ObjectStateLoadingException If the given ObjectState instance
100  * is erroneous.
101  */
103  : unitName_(""),
104  encoding_(0),
105  extraBits_(0),
106  indexWidth_(0),
107  hasEncoding_(false),
108  parent_(NULL),
109  maxRegIndex_(0) {
110  try {
112  if (state->hasAttribute(OSKEY_MAX_INDEX))
114  if (state->hasAttribute(OSKEY_ENCODING)) {
117  hasEncoding_ = true;
118  }
119  if (state->hasAttribute(OSKEY_INDEX_WIDTH)) {
121  }
122  } catch (const Exception&) {
123  throw ObjectStateLoadingException(__FILE__, __LINE__, __func__);
124  }
125 }
126 
127 /**
128  * The destructor.
129  */
131 }
132 
133 
134 /**
135  * Returns the name of the unit.
136  *
137  * @return The name of the unit.
138  */
139 std::string
141  return unitName_;
142 }
143 
144 
145 /**
146  * Tells whether the port code has an encoding (port ID).
147  *
148  * @return True if the port code has an encoding, otherwise false.
149  */
150 bool
152  return hasEncoding_;
153 }
154 
155 
156 /**
157  * Returns the encoding.
158  *
159  * @return The encoding.
160  * @exception NotAvailable If the port code does not have an encoding
161  * (port ID).
162  */
163 unsigned int
165  if (!hasEncoding()) {
166  throw NotAvailable(__FILE__, __LINE__, __func__);
167  }
168  return encoding_;
169 }
170 
171 /**
172  * Returns the number of extra bits in the encoding.
173  *
174  * @return The number of extra bits.
175  */
176 unsigned int
178  return extraBits_;
179 }
180 
181 
182 /**
183  * Returns the width of the whole port code (portID + opcode).
184  *
185  * @return The width.
186  */
187 int
189  if (hasEncoding()) {
190  return MathTools::bitLength(encoding()) + extraBits() +
191  indexWidth();
192  } else {
193  return indexWidth();
194  }
195 }
196 
197 
198 /**
199  * Returns the width of the encoding (port ID).
200  *
201  * @return The width of the encoding.
202  */
203 int
205  return width() - indexWidth();
206 }
207 
208 
209 /**
210  * Returns the width of the register index (opcode).
211  *
212  * @return The width of the register index.
213  */
214 int
216  return indexWidth_;
217 }
218 
219 
220 /**
221  * Returns the parent socket code table.
222  *
223  * @return The parent socket code table.
224  */
227  return parent_;
228 }
229 
230 
231 /**
232  * Saves the state of the object to an ObjectState instance.
233  *
234  * @return The newly created ObjectState instance.
235  */
240  if (hasEncoding()) {
243  }
246  return state;
247 }
248 
249 
250 /**
251  * Sets the parent socket code table pointer.
252  *
253  * @param parent The parent pointer.
254  */
255 void
257  parent_ = parent;
258 }
PortCode::~PortCode
virtual ~PortCode()
Definition: PortCode.cc:130
ObjectState::hasAttribute
bool hasAttribute(const std::string &name) const
Definition: ObjectState.cc:205
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
ObjectStateLoadingException
Definition: Exception.hh:551
PortCode::OSKEY_EXTRA_BITS
static const std::string OSKEY_EXTRA_BITS
ObjectState attribute key for the number of extra bits.
Definition: PortCode.hh:73
OutOfRange
Definition: Exception.hh:320
ObjectState
Definition: ObjectState.hh:59
PortCode::OSKEY_UNIT_NAME
static const std::string OSKEY_UNIT_NAME
ObjectState attribute key for the name of the unit.
Definition: PortCode.hh:69
PortCode::parent_
SocketCodeTable * parent_
The parent socket code table.
Definition: PortCode.hh:99
PortCode::OSKEY_ENCODING
static const std::string OSKEY_ENCODING
ObjectState attribute key for the encoding.
Definition: PortCode.hh:71
NotAvailable
Definition: Exception.hh:728
SocketCodeTable
Definition: SocketCodeTable.hh:68
PortCode::unitName
std::string unitName() const
Definition: PortCode.cc:140
PortCode::extraBits_
unsigned int extraBits_
The number of extra bits in the encoding.
Definition: PortCode.hh:93
PortCode::extraBits
unsigned int extraBits() const
Definition: PortCode.cc:177
PortCode::indexWidth_
int indexWidth_
The width of the register index in the port code.
Definition: PortCode.hh:95
PortCode::OSKEY_MAX_INDEX
static const std::string OSKEY_MAX_INDEX
ObjectState attribute key for the max-index attribute.
Definition: PortCode.hh:77
PortCode::indexWidth
int indexWidth() const
Definition: PortCode.cc:215
PortCode::width
int width() const
Definition: PortCode.cc:188
PortCode::PortCode
PortCode(const std::string &unitName, unsigned int encoding, unsigned int extraBits, int indexWidth)
Definition: PortCode.cc:56
PortCode::maxRegIndex_
unsigned maxRegIndex_
In case this is set to non-zero, the whole width of the index field is not used because the number of...
Definition: PortCode.hh:104
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
Exception
Definition: Exception.hh:54
PortCode::setParent
void setParent(SocketCodeTable *parent)
Definition: PortCode.cc:256
PortCode::encodingWidth
int encodingWidth() const
Definition: PortCode.cc:204
PortCode.hh
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
PortCode::unitName_
std::string unitName_
Name of the unit.
Definition: PortCode.hh:89
PortCode::OSKEY_INDEX_WIDTH
static const std::string OSKEY_INDEX_WIDTH
ObjectState attribute key for the widht of register index.
Definition: PortCode.hh:75
MathTools::bitLength
static unsigned int bitLength(long unsigned int number)
PortCode::encoding_
unsigned int encoding_
The encoding.
Definition: PortCode.hh:91
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
PortCode::hasEncoding
bool hasEncoding() const
Definition: PortCode.cc:151
PortCode::parent
SocketCodeTable * parent() const
Definition: PortCode.cc:226
MathTools.hh
PortCode::OSNAME_PORT_CODE
static const std::string OSNAME_PORT_CODE
ObjectState name for PortCode.
Definition: PortCode.hh:67
PortCode::encoding
unsigned int encoding() const
Definition: PortCode.cc:164
PortCode::saveState
virtual ObjectState * saveState() const
Definition: PortCode.cc:237
PortCode::hasEncoding_
bool hasEncoding_
Tells whether the port code has an encoding.
Definition: PortCode.hh:97
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100