OpenASIP  2.0
OperationTriggeredEncoding.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 OperationTriggeredEncoding.cc
26  *
27  * Implementation of OperationTriggeredEncoding class.
28  *
29  * @author Kari Hepola 2021 (kari.hepola@tuni.fi)
30  * @note rating: red
31  */
32 
35 #include "InstructionFormat.hh"
36 #include "ObjectState.hh"
37 #include <algorithm>
38 
40  "ota-encoding";
41 
43  "name";
44 
45 /**
46  * The constructor.
47  *
48  * Adds the instruction format to the parent binary encoding automatically.
49  *
50  * @param name Name of the instruction format.
51  * @param parent The parent Instruction Format.
52  */
53 
55  const std::string& name, InstructionFormat& parent)
56  : name_(name) {
57  parent.addEncoding(*this);
58 }
59 
60 /**
61  * The constructor.
62  *
63  * Loads the state of the instruction format from the given ObjectState tree.
64  *
65  * @param state The ObjectState tree.
66  * @param parent The parent binary encoding map.
67  * @exception ObjectStateLoadingException If an error occurs while loading
68  the state.
69  */
70 
72  const ObjectState* state, InstructionFormat& parent) {
73  parent.addEncoding(*this);
74  loadState(state);
75 }
76 
77 /**
78  * The destructor
79  *
80  */
81 
83  for (unsigned int i = 0; i < fields_.size(); i++) {
84  delete fields_.at(i);
85  }
86 }
87 
88 /**
89  * Returns the name of the instruction format.
90  *
91  * @return The name of the instruction format
92  */
93 
94 std::string
96  return name_;
97 }
98 
99 /**
100  * Sets the name of the instruction format.
101  *
102  * @param name The name of the instruction format.
103  */
104 
105 void
106 OperationTriggeredEncoding::setName(const std::string& name) {
107  name_ = name;
108 }
109 
110 /**
111  * Returns the number of operation triggered fields.
112  *
113  * @return The number of child fields.
114  */
115 
116 int
118  return fields_.size();
119 }
120 
121 /**
122  * Returns the bit width of the instruction format.
123  *
124  * @return Bit width of the instruction format.
125  */
126 
127 int
129  int width = 0;
130  for (unsigned int i = 0; i < fields_.size(); i++) {
131  OperationTriggeredField* field = fields_.at(i);
132  width += field->width();
133  }
134  return width;
135 }
136 
137 void
139  // TODO: Inspect that field is valid
140  fields_.push_back(&field);
141 }
142 
143 /**
144  * Loads the state of the instruction format from the given ObjectState tree.
145  *
146  * @param state The ObjectState tree.
147  * @exception ObjectStateLoadingException If an error occurs while loading
148  * the state.
149  */
150 
151 void
153  ObjectState* newState = new ObjectState(*state);
154  try {
156  for (int i = 0; i < newState->childCount(); i++) {
157  ObjectState* child = newState->child(i);
159  new OperationTriggeredField(child, *this);
160  }
161  }
162  } catch (const Exception& exception) {
163  const std::string procName = "OperationTriggeredEncoding::loadState";
165  __FILE__, __LINE__, procName, exception.errorMessage());
166  }
167  delete newState;
168 }
169 
170 /**
171  * Saves the state of the instruction format to an ObjectState tree.
172  *
173  * @return The newly created ObjectState tree.
174  */
175 
180  for (unsigned int i = 0; i < fields_.size(); i++) {
181  OperationTriggeredField* it = fields_.at(i);
182  ObjectState* fieldObj = it->saveState();
183  state->addChild(fieldObj);
184  }
185  return state;
186 }
OperationTriggeredEncoding.hh
OperationTriggeredEncoding::OSNAME_OTA_ENCODING
static const std::string OSNAME_OTA_ENCODING
Definition: OperationTriggeredEncoding.hh:68
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
OperationTriggeredField.hh
ObjectStateLoadingException
Definition: Exception.hh:551
OperationTriggeredField::OSNAME_FIELD
static const std::string OSNAME_FIELD
Definition: OperationTriggeredField.hh:65
ObjectState
Definition: ObjectState.hh:59
OperationTriggeredEncoding::OperationTriggeredEncoding
OperationTriggeredEncoding(const std::string &name, InstructionFormat &parent)
Definition: OperationTriggeredEncoding.cc:54
OperationTriggeredEncoding::addField
void addField(OperationTriggeredField &field)
Definition: OperationTriggeredEncoding.cc:138
OperationTriggeredEncoding::name_
std::string name_
Definition: OperationTriggeredEncoding.hh:74
OperationTriggeredField::saveState
virtual ObjectState * saveState() const
Definition: OperationTriggeredField.cc:170
ObjectState.hh
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
ObjectState::addChild
void addChild(ObjectState *child)
Definition: ObjectState.cc:376
OperationTriggeredEncoding::~OperationTriggeredEncoding
~OperationTriggeredEncoding()
Definition: OperationTriggeredEncoding.cc:82
ObjectState::childCount
int childCount() const
Exception
Definition: Exception.hh:54
OperationTriggeredField
Definition: OperationTriggeredField.hh:43
ObjectState::name
std::string name() const
InstructionFormat
Definition: InstructionFormat.hh:46
Exception::errorMessage
std::string errorMessage() const
Definition: Exception.cc:123
OperationTriggeredEncoding::setName
void setName(const std::string &name)
Definition: OperationTriggeredEncoding.cc:106
OperationTriggeredEncoding::saveState
virtual ObjectState * saveState() const
Definition: OperationTriggeredEncoding.cc:177
OperationTriggeredField::width
virtual int width() const
Definition: OperationTriggeredField.cc:126
OperationTriggeredEncoding::childFieldCount
virtual int childFieldCount() const
Definition: OperationTriggeredEncoding.cc:117
InstructionFormat.hh
OperationTriggeredEncoding::loadState
virtual void loadState(const ObjectState *state)
Definition: OperationTriggeredEncoding.cc:152
InstructionFormat::addEncoding
void addEncoding(OperationTriggeredEncoding &encoding)
Definition: InstructionFormat.cc:139
OperationTriggeredEncoding::name
std::string name() const
Definition: OperationTriggeredEncoding.cc:95
OperationTriggeredEncoding::fields_
std::vector< OperationTriggeredField * > fields_
Definition: OperationTriggeredEncoding.hh:76
OperationTriggeredEncoding::OSKEY_OTA_ENCODING_NAME
static const std::string OSKEY_OTA_ENCODING_NAME
Definition: OperationTriggeredEncoding.hh:69
OperationTriggeredEncoding::width
virtual int width() const
Definition: OperationTriggeredEncoding.cc:128
ObjectState::setAttribute
void setAttribute(const std::string &name, const std::string &value)
Definition: ObjectState.cc:100