OpenASIP 2.2
Loading...
Searching...
No Matches
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
42const std::string InstructionFormat::OSNAME_INSTRUCTION_FORMAT = "ota-format";
43
46 "ota-operation";
47const 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
103bool
104InstructionFormat::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
114std::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
125void
126InstructionFormat::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
138void
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
157void
158InstructionFormat::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
173int
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
184int
186 int width = 0;
187 for (unsigned int i = 0; i < encodings_.size(); i++) {
189 width += slot->width();
190 }
191 return width;
192}
193
194int
196 return operations_.size();
197}
198
199std::string
201 if (index > operationCount() - 1) {
202 const std::string msg = "Operation index out of range.";
203 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
204 }
205 auto it = operations_.begin();
206 std::advance(it, index);
207 return it->first;
208}
209
210
211int
212InstructionFormat::encoding(const std::string& op) const {
214 return operations_.at(op);
215}
216
217/**
218 * Loads the state of the instruction format from the given ObjectState tree.
219 *
220 * @param state The ObjectState tree.
221 * @exception ObjectStateLoadingException If an error occurs while loading
222 * the state.
223 */
224
225void
227 ObjectState* newState = new ObjectState(*state);
228 try {
230 for (int i = 0; i < newState->childCount(); i++) {
231 ObjectState* child = newState->child(i);
232 if (child->name() ==
234 new OperationTriggeredEncoding(child, *this);
235 } else if (child->name() == OSKEY_OTA_OPERATION_NAME) {
239 }
240 }
241 } catch (const Exception& exception) {
242 const std::string procName = "InstructionFormat::loadState";
244 __FILE__, __LINE__, procName, exception.errorMessage());
245 }
246}
247
248/**
249 * Saves the state of the instruction format to an ObjectState tree.
250 *
251 * @return The newly created ObjectState tree.
252 */
253
258 for (unsigned int i = 0; i < encodings_.size(); i++) {
260 ObjectState* obj = it->saveState();
261 state->addChild(obj);
262 }
263 for (auto const& val : operations_) {
265 obj->setAttribute(OSKEY_OPERATION_NAME, val.first);
267 state->addChild(obj);
268 }
269 return state;
270}
#define __func__
#define assert(condition)
std::string errorMessage() const
Definition Exception.cc:123
bool hasOperation(const std::string &op) const
static const std::string OSKEY_OTA_OPERATION_NAME
void addOperation(std::string op, int encoding)
BinaryEncoding * parent_
static const std::string OSKEY_OPERATION_NAME
virtual ObjectState * saveState() const
InstructionFormat(const std::string &name, BinaryEncoding &parent)
static const std::string OSKEY_OPERATION_ENCODING_NAME
virtual int childFieldCount() const
std::string operationAtIndex(const int index) const
void setName(const std::string &name)
static const std::string OSNAME_INSTRUCTION_FORMAT
virtual void loadState(const ObjectState *state)
std::map< std::string, int > operations_
int encoding(const std::string &op) const
std::vector< OperationTriggeredEncoding * > encodings_
std::string name() const
static const std::string OSKEY_INSTRUCTION_FORMAT_NAME
virtual int width() const
InstructionField * parent() const
void addEncoding(OperationTriggeredEncoding &encoding)
static bool containsKey(const MapType &aMap, const KeyType &aKey)
void setAttribute(const std::string &name, const std::string &value)
ObjectState * child(int index) const
void addChild(ObjectState *child)
std::string stringAttribute(const std::string &name) const
int intAttribute(const std::string &name) const
std::string name() const
int childCount() const
virtual ObjectState * saveState() const
static const std::string OSNAME_OTA_ENCODING
static bool containsValue(const ContainerType &aVec, const ValueType &aValue)