OpenASIP  2.0
InstructionFormat.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2021 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 InstructionFormat.cc
26  *
27  * Implementation of InstructionFormat class.
28  *
29  * @author Kari Hepola 2021 (kari.hepola@tuni.fi)
30  * @note rating: red
31  */
32 
33 #include "InstructionFormat.hh"
35 #include "ObjectState.hh"
36 #include "BinaryEncoding.hh"
37 #include "MapTools.hh"
38 #include "VectorTools.hh"
39 #include "InstructionField.hh"
40 #include <algorithm>
41 
42 const std::string InstructionFormat::OSNAME_INSTRUCTION_FORMAT = "ota-format";
43 
44 const std::string InstructionFormat::OSKEY_INSTRUCTION_FORMAT_NAME = "name";
46  "ota-operation";
47 const std::string InstructionFormat::OSKEY_OPERATION_NAME = "name";
49  "encoding";
50 
51 /**
52  * The constructor.
53  *
54  * Adds the instruction format to the parent binary encoding automatically.
55  *
56  * @param name Name of the instruction format.
57  * @param parent The parent BinaryEncoding.
58  */
59 
61  const std::string& name, BinaryEncoding& parent)
62  : name_(name), parent_(NULL) {
63  parent.addInstructionFormat(*this);
64  parent_ = &parent;
65 }
66 
67 /**
68  * The constructor.
69  *
70  * Loads the state of the instruction format from the given ObjectState tree.
71  *
72  * @param state The ObjectState tree.
73  * @param parent The parent binary encoding map.
74  * @exception ObjectStateLoadingException If an error occurs while loading
75  the state.
76  */
77 
79  const ObjectState* state, BinaryEncoding& parent)
80  : parent_(NULL) {
81  parent.addInstructionFormat(*this);
82  parent_ = &parent;
83  loadState(state);
84 }
85 
86 /**
87  * The destructor
88  *
89  */
90 
92  /*
93  for (unsigned int i = 0; i < encodings_.size(); i++) {
94  delete encodings_.at(i);
95  }*/
96 }
97 
100  return static_cast<InstructionField*>(parent_);
101 }
102 
103 bool
104 InstructionFormat::hasOperation(const std::string& op) const {
106 }
107 
108 /**
109  * Returns the name of the instruction format.
110  *
111  * @return The name of the instruction format
112  */
113 
114 std::string
116  return name_;
117 }
118 
119 /**
120  * Sets the name of the instruction format.
121  *
122  * @param name The name of the instruction format.
123  */
124 
125 void
126 InstructionFormat::setName(const std::string& name) {
127  name_ = name;
128 }
129 
130 /**
131  * Adds an operation triggered move slot to the instruction format
132  *
133  * @param encoding The operation triggered move slot to be addeed
134  * @exception ObjectAlreadyExists If the slot has already been added to the
135  * instruction format.
136  */
137 
138 void
141  encodings_.push_back(&encoding);
142  } else {
143  const std::string procName = "InstructionFormat::addEncoding";
144  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
145  }
146 }
147 
148 /**
149  * Adds an operation to the instruction format
150  *
151  * @param op Operation name
152  * @param encoding Encoding of the operation
153  * @exception ObjectAlreadyExists If the slot has already been added to the
154  * instruction format.
155  */
156 
157 void
158 InstructionFormat::addOperation(std::string op, int encoding) {
160  operations_.insert({op, encoding});
161  } else {
162  const std::string procName = "InstructionFormat::addOperation";
163  throw ObjectAlreadyExists(__FILE__, __LINE__, procName);
164  }
165 }
166 
167 /**
168  * Returns the number of operation triggered encodings.
169  *
170  * @return The number of child fields.
171  */
172 
173 int
175  return encodings_.size();
176 }
177 
178 /**
179  * Returns the bit width of the instruction format.
180  *
181  * @return Bit width of the instruction format.
182  */
183 
184 int
186  int width = 0;
187  for (unsigned int i = 0; i < encodings_.size(); i++) {
189  width += slot->width();
190  }
191  return width;
192 }
193 
194 std::map<std::string, int>
196  return operations_;
197 }
198 
199 int
200 InstructionFormat::encoding(const std::string& op) const {
202  return operations_.at(op);
203 }
204 
205 /**
206  * Loads the state of the instruction format from the given ObjectState tree.
207  *
208  * @param state The ObjectState tree.
209  * @exception ObjectStateLoadingException If an error occurs while loading
210  * the state.
211  */
212 
213 void
215  ObjectState* newState = new ObjectState(*state);
216  try {
218  for (int i = 0; i < newState->childCount(); i++) {
219  ObjectState* child = newState->child(i);
220  if (child->name() ==
222  new OperationTriggeredEncoding(child, *this);
223  } else if (child->name() == OSKEY_OTA_OPERATION_NAME) {
224  addOperation(
227  }
228  }
229  } catch (const Exception& exception) {
230  const std::string procName = "InstructionFormat::loadState";
232  __FILE__, __LINE__, procName, exception.errorMessage());
233  }
234 }
235 
236 /**
237  * Saves the state of the instruction format to an ObjectState tree.
238  *
239  * @return The newly created ObjectState tree.
240  */
241 
246  for (unsigned int i = 0; i < encodings_.size(); i++) {
248  ObjectState* obj = it->saveState();
249  state->addChild(obj);
250  }
251  for (auto const& val : operations_) {
253  obj->setAttribute(OSKEY_OPERATION_NAME, val.first);
255  state->addChild(obj);
256  }
257  return state;
258 }
OperationTriggeredEncoding.hh
InstructionFormat::name
std::string name() const
Definition: InstructionFormat.cc:115
OperationTriggeredEncoding::OSNAME_OTA_ENCODING
static const std::string OSNAME_OTA_ENCODING
Definition: OperationTriggeredEncoding.hh:68
InstructionFormat::OSKEY_OTA_OPERATION_NAME
static const std::string OSKEY_OTA_OPERATION_NAME
Definition: InstructionFormat.hh:79
BinaryEncoding
Definition: BinaryEncoding.hh:61
InstructionField.hh
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
InstructionFormat::addOperation
void addOperation(std::string op, int encoding)
Definition: InstructionFormat.cc:158
InstructionFormat::operations
std::map< std::string, int > operations() const
Definition: InstructionFormat.cc:195
ObjectStateLoadingException
Definition: Exception.hh:551
InstructionFormat::setName
void setName(const std::string &name)
Definition: InstructionFormat.cc:126
InstructionFormat::operations_
std::map< std::string, int > operations_
Definition: InstructionFormat.hh:90
MapTools.hh
ObjectState
Definition: ObjectState.hh:59
InstructionFormat::parent
InstructionField * parent() const
Definition: InstructionFormat.cc:99
InstructionField
Definition: InstructionField.hh:43
InstructionFormat::OSKEY_INSTRUCTION_FORMAT_NAME
static const std::string OSKEY_INSTRUCTION_FORMAT_NAME
Definition: InstructionFormat.hh:78
assert
#define assert(condition)
Definition: Application.hh:86
InstructionFormat::width
virtual int width() const
Definition: InstructionFormat.cc:185
InstructionFormat::encodings_
std::vector< OperationTriggeredEncoding * > encodings_
Definition: InstructionFormat.hh:89
InstructionFormat::OSKEY_OPERATION_ENCODING_NAME
static const std::string OSKEY_OPERATION_ENCODING_NAME
Definition: InstructionFormat.hh:81
BinaryEncoding.hh
ObjectState.hh
InstructionFormat::~InstructionFormat
~InstructionFormat()
Definition: InstructionFormat.cc:91
VectorTools.hh
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
OperationTriggeredEncoding
Definition: OperationTriggeredEncoding.hh:44
ObjectState::childCount
int childCount() const
Exception
Definition: Exception.hh:54
InstructionFormat::InstructionFormat
InstructionFormat(const std::string &name, BinaryEncoding &parent)
Definition: InstructionFormat.cc:60
InstructionFormat::hasOperation
bool hasOperation(const std::string &op) const
Definition: InstructionFormat.cc:104
ObjectState::name
std::string name() const
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
InstructionFormat::childFieldCount
virtual int childFieldCount() const
Definition: InstructionFormat.cc:174
MapTools::containsKey
static bool containsKey(const MapType &aMap, const KeyType &aKey)
InstructionFormat::parent_
BinaryEncoding * parent_
Definition: InstructionFormat.hh:87
OperationTriggeredEncoding::saveState
virtual ObjectState * saveState() const
Definition: OperationTriggeredEncoding.cc:177
ObjectAlreadyExists
Definition: Exception.hh:1002
VectorTools::containsValue
static bool containsValue(const ContainerType &aVec, const ValueType &aValue)
InstructionFormat::encoding
int encoding(const std::string &op) const
Definition: InstructionFormat.cc:200
ObjectState::intAttribute
int intAttribute(const std::string &name) const
Definition: ObjectState.cc:276
InstructionFormat.hh
InstructionFormat::name_
std::string name_
Definition: InstructionFormat.hh:86
InstructionFormat::loadState
virtual void loadState(const ObjectState *state)
Definition: InstructionFormat.cc:214
InstructionFormat::addEncoding
void addEncoding(OperationTriggeredEncoding &encoding)
Definition: InstructionFormat.cc:139
InstructionFormat::saveState
virtual ObjectState * saveState() const
Definition: InstructionFormat.cc:243
InstructionFormat::OSKEY_OPERATION_NAME
static const std::string OSKEY_OPERATION_NAME
Definition: InstructionFormat.hh:80
OperationTriggeredEncoding::width
virtual int width() const
Definition: OperationTriggeredEncoding.cc:128
InstructionFormat::OSNAME_INSTRUCTION_FORMAT
static const std::string OSNAME_INSTRUCTION_FORMAT
Definition: InstructionFormat.hh:77
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100