OpenASIP  2.0
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 
41 namespace TTAMachine {
42 
43 const std::string OperationTriggeredFormat::OSNAME_FORMAT = "ota-format";
44 
45 const 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&) {
92  unsetMachine();
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 
107 std::vector<std::string>
109  return operations_;
110 }
111 
112 void
114  operations_.push_back(op);
115 }
116 
117 void
119  for (unsigned int i = 0; i < operations_.size(); i++) {
120  if (operations_.at(i) == op) {
121  operations_.erase(operations_.begin() + i);
122  }
123  }
124 }
125 
126 int
128  return operations_.size();
129 }
130 
131 std::string
133  if (index > operationCount() - 1) {
134  const std::string msg = "Operation index out of range.";
135  throw OutOfRange(__FILE__, __LINE__, __func__, msg);
136  }
137  return operations_.at(index);
138 }
139 
140 bool
141 OperationTriggeredFormat::hasOperation(const std::string& opName) const {
142  for (const std::string& op : operations_) {
143  if (op == opName) {
144  return true;
145  }
146  }
147  return false;
148 }
149 
150 std::vector<OperationTriggeredOperand*>
152  return operands_;
153 }
154 
155 void
157  operands_.push_back(&op);
158 }
159 
160 /**
161  * Adds the OperationTriggeredFormat to the given machine.
162  *
163  * @param machine Machine to which the OperationTriggeredFormat Format is
164  * added.
165  * @exception ComponentAlreadyExists If there already is another
166  * OperationTriggeredFormat Format by the same name or another empty
167  * OperationTriggeredFormat Format in the given machine.
168  */
169 void
173 }
174 
175 /**
176  * Removes the OperationTriggeredFormat Format from its machine.
177  *
178  * The OperationTriggeredFormat Format is also deleted because it cannot be
179  * alone. It must be registered to a machine.
180  */
181 void
183  Machine* mach = machine();
184  assert(mach != NULL);
186  mach->deleteOperationTriggeredFormat(*this);
187 }
188 
189 void
191  operations_.clear();
192  operands_.clear();
193  const std::string procName = "OperationTriggeredOperand::loadState";
194 
195  ObjectState* newState = new ObjectState(*state);
196 
197  if (newState->name() != OSNAME_FORMAT) {
198  throw ObjectStateLoadingException(__FILE__, __LINE__, procName);
199  }
200  Component::loadState(newState);
201  try {
202  for (int i = 0; i < newState->childCount(); i++) {
203  ObjectState* child = newState->child(i);
205  new OperationTriggeredOperand(child, *this);
206  } else if (
208  operations_.push_back(child->stringValue());
209  }
210  }
211  } catch (const Exception& exception) {
213  __FILE__, __LINE__, procName, exception.errorMessage());
214  }
215  delete newState;
216 }
217 
218 /**
219  * Saves the state of the operation code encoding
220  * to an ObjectState tree.
221  *
222  * @return The newly created ObjectState tree.
223  */
224 
228  state->setName(OSNAME_FORMAT);
229  for (unsigned int i = 0; i < operands_.size(); i++) {
230  ObjectState* operandObject = operands_.at(i)->saveState();
231  state->addChild(operandObject);
232  }
233  for (unsigned int i = 0; i < operations_.size(); i++) {
234  ObjectState* operationObject = new ObjectState(OSKEY_OPERATION);
235  operationObject->setValue(operations_.at(i));
236  state->addChild(operationObject);
237  }
238 
239  return state;
240 }
241 } // namespace TTAMachine
TTAMachine::Component::internalUnsetMachine
void internalUnsetMachine()
TTAMachine::OperationTriggeredFormat::operations
std::vector< std::string > operations() const
Definition: OperationTriggeredFormat.cc:108
TTAMachine::Component::name
virtual TCEString name() const
Definition: MachinePart.cc:125
TTAMachine::OperationTriggeredFormat::operands
std::vector< OperationTriggeredOperand * > operands() const
Definition: OperationTriggeredFormat.cc:151
machine
TTAMachine::Machine * machine
the architecture definition of the estimated processor
Definition: EstimatorCmdLineUI.cc:59
ObjectStateLoadingException
Definition: Exception.hh:551
OutOfRange
Definition: Exception.hh:320
TTAMachine::Component::saveState
virtual ObjectState * saveState() const
Definition: MachinePart.cc:189
TTAMachine::OperationTriggeredFormat::operation
std::string operation(int index) const
Definition: OperationTriggeredFormat.cc:132
TTAMachine::OperationTriggeredFormat::removeOperation
void removeOperation(const std::string &op)
Definition: OperationTriggeredFormat.cc:118
ObjectState
Definition: ObjectState.hh:59
TTAMachine::OperationTriggeredFormat::OperationTriggeredFormat
OperationTriggeredFormat(const std::string &name, Machine &owner)
Definition: OperationTriggeredFormat.cc:56
TTAMachine::Machine::addOperationTriggeredFormat
virtual void addOperationTriggeredFormat(OperationTriggeredFormat &format)
Definition: Machine.cc:287
TTAMachine::OperationTriggeredOperand
Definition: OperationTriggeredOperand.hh:44
Texts::TextGenerator::text
virtual boost::format text(int textId)
Definition: TextGenerator.cc:94
ObjectState::setName
void setName(const std::string &name)
TTAMachine::OperationTriggeredOperand::OSNAME_OPERAND
static const std::string OSNAME_OPERAND
Definition: OperationTriggeredOperand.hh:68
TTAMachine::OperationTriggeredFormat::OSKEY_OPERATION
static const std::string OSKEY_OPERATION
Definition: OperationTriggeredFormat.hh:73
TTAMachine::Component::internalSetMachine
void internalSetMachine(Machine &machine)
MOMTextGenerator::TXT_IT_EXISTS_BY_NAME
@ TXT_IT_EXISTS_BY_NAME
Definition: MOMTextGenerator.hh:69
assert
#define assert(condition)
Definition: Application.hh:86
TTAMachine::OperationTriggeredFormat::saveState
virtual ObjectState * saveState() const
Definition: OperationTriggeredFormat.cc:226
OperationTriggeredOperand.hh
TTAMachine::OperationTriggeredFormat::operands_
std::vector< OperationTriggeredOperand * > operands_
Definition: OperationTriggeredFormat.hh:79
TTAMachine::OperationTriggeredFormat::setMachine
virtual void setMachine(Machine &machine)
Definition: OperationTriggeredFormat.cc:170
TTAMachine::OperationTriggeredFormat::operationCount
int operationCount() const
Definition: OperationTriggeredFormat.cc:127
TTAMachine::Component::loadState
virtual void loadState(const ObjectState *state)
Definition: MachinePart.cc:205
TTAMachine::OperationTriggeredFormat::operations_
std::vector< std::string > operations_
Definition: OperationTriggeredFormat.hh:78
__func__
#define __func__
Definition: Application.hh:67
ObjectState.hh
TTAMachine::OperationTriggeredFormat::OSNAME_FORMAT
static const std::string OSNAME_FORMAT
Definition: OperationTriggeredFormat.hh:72
TTAMachine::Component
Definition: MachinePart.hh:90
TTAMachine::OperationTriggeredFormat::~OperationTriggeredFormat
virtual ~OperationTriggeredFormat()
Definition: OperationTriggeredFormat.cc:100
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
ObjectState::childCount
int childCount() const
Machine.hh
Exception
Definition: Exception.hh:54
ObjectState::name
std::string name() const
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
TTAMachine::OperationTriggeredFormat::addOperation
void addOperation(const std::string &op)
Definition: OperationTriggeredFormat.cc:113
TTAMachine::OperationTriggeredFormat::loadState
virtual void loadState(const ObjectState *state)
Definition: OperationTriggeredFormat.cc:190
TTAMachine::Component::machine
virtual Machine * machine() const
MOMTextGenerator
Definition: MOMTextGenerator.hh:40
ComponentAlreadyExists
Definition: Exception.hh:510
ObjectState::stringValue
std::string stringValue() const
MOMTextGenerator.hh
TTAMachine::OperationTriggeredFormat::addOperand
void addOperand(OperationTriggeredOperand &operand)
Definition: OperationTriggeredFormat.cc:156
OperationTriggeredFormat.hh
TTAMachine
Definition: Assembler.hh:48
TTAMachine::OperationTriggeredFormat::unsetMachine
virtual void unsetMachine()
Definition: OperationTriggeredFormat.cc:182
ObjectState::setValue
void setValue(const std::string &value)
TTAMachine::OperationTriggeredFormat::hasOperation
bool hasOperation(const std::string &opName) const
Definition: OperationTriggeredFormat.cc:141
TTAMachine::Machine::deleteOperationTriggeredFormat
virtual void deleteOperationTriggeredFormat(OperationTriggeredFormat &format)
Definition: Machine.cc:611
TTAMachine::Machine
Definition: Machine.hh:73