OpenASIP 2.2
Loading...
Searching...
No Matches
OperationTriggeredFormat.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 OperationTriggeredFormat.cc
26 *
27 * Implementation of OperationTriggeredFormat class.
28 *
29 * @author Kari Hepola 2022 (kari.hepola@tuni.fi)
30 * @note rating: red
31 */
32
33#include <stdio.h>
34
37#include "MOMTextGenerator.hh"
38#include "ObjectState.hh"
39#include "Machine.hh"
40
41namespace TTAMachine {
42
43const std::string OperationTriggeredFormat::OSNAME_FORMAT = "ota-format";
44
45const std::string OperationTriggeredFormat::OSKEY_OPERATION = "ota-operation";
46
47/**
48 * The constructor.
49 *
50 * Registers the operation code encoding to the parent
51 * binary encoding automatically.
52 *
53 * @param parent The parent OperationTriggeredFormat.
54 */
55
57 const std::string& name, Machine& owner)
58 : Component(name) {
59 setMachine(owner);
60}
61
62/**
63 * The constructor
64 *
65 * Loads the state of the operation code encoding from
66 * the given ObjectState tree
67 *
68 * @param state The ObjectState tree
69 * @param parent The parent binary encoding map
70 * @exception ObjectStateLoadingException If an error occurs while loading
71 the state.
72 */
73
75 const ObjectState* state, Machine& owner)
76 : Component(state) {
77 const std::string procName =
78 "OperationTriggeredFormat::OperationTriggeredFormat";
79 try {
80 setMachine(owner);
81 } catch (const ComponentAlreadyExists&) {
82 MOMTextGenerator textGenerator;
83 boost::format errorMsg =
85 errorMsg % name();
87 __FILE__, __LINE__, procName, errorMsg.str());
88 }
89 try {
90 loadState(state);
91 } catch (const ObjectStateLoadingException&) {
93 throw;
94 }
95}
96
97/**
98 * Destructor.
99 */
101 for (unsigned int i = 0; i < operands_.size(); i++) {
102 delete operands_.at(i);
103 }
104 unsetMachine();
105}
106
107void
109 operations_.push_back(op);
110}
111
112void
114 for (unsigned int i = 0; i < operations_.size(); i++) {
115 if (operations_.at(i) == op) {
116 operations_.erase(operations_.begin() + i);
117 }
118 }
119}
120
121int
125
126std::string
128 if (index > operationCount() - 1) {
129 const std::string msg = "Operation index out of range.";
130 throw OutOfRange(__FILE__, __LINE__, __func__, msg);
131 }
132 return operations_.at(index);
133}
134
135bool
136OperationTriggeredFormat::hasOperation(const std::string& opName) const {
137 for (const std::string& op : operations_) {
138 if (op == opName) {
139 return true;
140 }
141 }
142 return false;
143}
144
145std::vector<OperationTriggeredOperand*>
149
150void
154
155/**
156 * Adds the OperationTriggeredFormat to the given machine.
157 *
158 * @param machine Machine to which the OperationTriggeredFormat Format is
159 * added.
160 * @exception ComponentAlreadyExists If there already is another
161 * OperationTriggeredFormat Format by the same name or another empty
162 * OperationTriggeredFormat Format in the given machine.
163 */
164void
169
170/**
171 * Removes the OperationTriggeredFormat Format from its machine.
172 *
173 * The OperationTriggeredFormat Format is also deleted because it cannot be
174 * alone. It must be registered to a machine.
175 */
176void
183
184void
186 operations_.clear();
187 operands_.clear();
188 const std::string procName = "OperationTriggeredOperand::loadState";
189
190 ObjectState* newState = new ObjectState(*state);
191
192 if (newState->name() != OSNAME_FORMAT) {
193 throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
194 }
195 Component::loadState(newState);
196 try {
197 for (int i = 0; i < newState->childCount(); i++) {
198 ObjectState* child = newState->child(i);
200 new OperationTriggeredOperand(child, *this);
201 } else if (
203 operations_.push_back(child->stringValue());
204 }
205 }
206 } catch (const Exception& exception) {
208 __FILE__, __LINE__, procName, exception.errorMessage());
209 }
210 delete newState;
211}
212
213/**
214 * Saves the state of the operation code encoding
215 * to an ObjectState tree.
216 *
217 * @return The newly created ObjectState tree.
218 */
219
223 state->setName(OSNAME_FORMAT);
224 for (unsigned int i = 0; i < operands_.size(); i++) {
225 ObjectState* operandObject = operands_.at(i)->saveState();
226 state->addChild(operandObject);
227 }
228 for (unsigned int i = 0; i < operations_.size(); i++) {
229 ObjectState* operationObject = new ObjectState(OSKEY_OPERATION);
230 operationObject->setValue(operations_.at(i));
231 state->addChild(operationObject);
232 }
233
234 return state;
235}
236} // namespace TTAMachine
#define __func__
#define assert(condition)
TTAMachine::Machine * machine
the architecture definition of the estimated processor
std::string errorMessage() const
Definition Exception.cc:123
void setName(const std::string &name)
void setValue(const std::string &value)
ObjectState * child(int index) const
void addChild(ObjectState *child)
std::string stringValue() const
std::string name() const
int childCount() const
virtual Machine * machine() const
virtual void loadState(const ObjectState *state)
void internalSetMachine(Machine &machine)
virtual TCEString name() const
virtual ObjectState * saveState() const
virtual void deleteOperationTriggeredFormat(OperationTriggeredFormat &format)
Definition Machine.cc:611
virtual void addOperationTriggeredFormat(OperationTriggeredFormat &format)
Definition Machine.cc:287
virtual void loadState(const ObjectState *state)
OperationTriggeredFormat(const std::string &name, Machine &owner)
void addOperand(OperationTriggeredOperand &operand)
std::vector< OperationTriggeredOperand * > operands_
std::vector< OperationTriggeredOperand * > operands() const
bool hasOperation(const std::string &opName) const
virtual boost::format text(int textId)