OpenASIP  2.0
OperationContainer.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2011 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 OperationContainer.cc
26  *
27  * Definition of OperationContainer class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Pekka Jääskeläinen 2011
31  * @note rating: red
32  */
33 
34 #include <vector>
35 
36 #include "OperationContainer.hh"
37 #include "OperationIndex.hh"
38 #include "OperationSerializer.hh"
39 #include "Operation.hh"
40 #include "OperationIndex.hh"
43 #include "Application.hh"
44 #include "PluginTools.hh"
45 #include "StringTools.hh"
46 #include "Environment.hh"
47 #include "OperationModule.hh"
48 #include "OperationBehavior.hh"
49 #include "IdealSRAM.hh"
50 #include "SimValue.hh"
51 #include "TCEString.hh"
52 #include "ObjectState.hh"
53 
54 using std::string;
55 using std::vector;
56 
57 const string OperationContainer::CREATE_FUNCTION = "createOpBehavior_";
58 const string OperationContainer::DELETE_FUNCTION = "deleteOpBehavior_";
60 const Word OperationContainer::MEMORY_END = 65535;
61 const Word OperationContainer::MAUSIZE = 8;
62 
68 
69 // TODO: endianess
71  new IdealSRAM(MEMORY_START, MEMORY_END, MAUSIZE, false);
72 
74  memory_, programCounter_, returnAddress_, 0);
75 
76 /**
77  * Constructor.
78  */
80 }
81 
82 /**
83  * Destructor.
84  */
86 }
87 
88 /**
89  * Returns the instance of OperationIndex.
90  *
91  * @return The instance of OperationIndex.
92  */
95  if (index_ == NULL) {
96  index_ = new OperationIndex();
97  vector<string> paths = Environment::osalPaths();
98  for (size_t i = 0; i < paths.size(); i++) {
99  index_->addPath(paths[i]);
100  }
101  }
102  return *index_;
103 }
104 
105 /**
106  * Returns the instance of OperationSerializer.
107  *
108  * @return The instance of OperationSerializer.
109  */
112  if (serializer_ == NULL) {
114  }
115  return *serializer_;
116 }
117 
118 /**
119  * Returns the instance of OperationContext.
120  *
121  * @return Instance of OperationContext.
122  */
125  return context_;
126 }
127 
128 /**
129  * Returns the memory model instance.
130  *
131  * @return Memory model instance.
132  */
133 Memory&
135  return *memory_;
136 }
137 
138 /**
139  * Returns a certain module in a certain path.
140  *
141  * If module is not found, a NullOperationModule is returned.
142  *
143  * @param path The name of the path.
144  * @param mod The name of the module.
145  * @return The module or NullOperationModule.
146  */
148 OperationContainer::module(const std::string& path, const std::string& mod) {
149  OperationIndex& index = operationIndex();
150  try {
151  for (int i = 0; i < index.moduleCount(path); i++) {
152  if (index.module(i, path).name() == mod) {
153  return index.module(i, path);
154  }
155  }
156  } catch (const Exception& e) {
158  }
160 }
161 
162 /**
163  * Returns a certain operation in a certain module and a path.
164  *
165  * If operation is not found, NULL is returned.
166  *
167  * @param path The name of the path.
168  * @param mod The name of the module.
169  * @param oper The name of the operation.
170  * @return The operation.
171  */
172 Operation*
174  const std::string& path,
175  const std::string& mod,
176  const std::string& oper) {
177 
178  OperationModule& opModule = module(path, mod);
179  assert(&opModule != &NullOperationModule::instance());
180 
182  serializer.setSourceFile(opModule.propertiesModule());
183  try {
184  ObjectState* root = serializer.readState();
185  for (int i = 0; i < root->childCount(); i++) {
186  if (root->child(i)->stringAttribute("name") == oper) {
187 
188  OperationIndex* index = new OperationIndex;
189  index->addPath(path);
190 
191  Operation* op =
193  op->loadState(root->child(i));
194 
195  OperationBehaviorLoader* behaviorLoader =
196  new OperationBehaviorLoader(*index);
197  OperationBehaviorProxy* behaviorProxy =
198  new OperationBehaviorProxy(*op, *behaviorLoader, true);
199 
200  op->setBehavior(*behaviorProxy);
201 
202  delete root;
203  return op;
204  }
205  }
206  } catch (const Exception& e) {
207  return NULL;
208  }
209  return NULL;
210 }
211 
212 /**
213  * Returns true if operation exists.
214  *
215  * @param name The name of the operation.
216  * @return True if operation exists, false otherwise.
217  */
218 bool
219 OperationContainer::operationExists(const std::string& name) {
220 
221  OperationIndex& index = operationIndex();
222 
223  for (int i = 0; i < index.moduleCount(); i++) {
224  OperationModule& mod = index.module(i);
225  for (int j = 0; j < index.operationCount(mod); j++) {
226  if (index.operationName(j, mod) == name) {
227  return true;
228  }
229  }
230  }
231  return false;
232 }
233 
234 /**
235  * Returns true if operation is effective.
236  *
237  * Effective means that is is found first in list of search paths.
238  *
239  * @param module The module in which operation is defined.
240  * @param name The name of the operation.
241  * @return True if operation is effective.
242  */
243 bool
245  OperationModule& module,
246  const std::string& name) {
247 
248  try {
249  OperationIndex& index = operationIndex();
250  for (int i = 0; i < index.pathCount(); i++) {
251  string path = index.path(i);
252  for (int j = 0; j < index.moduleCount(path); j++) {
253  OperationModule& mod = index.module(j, path);
254  for (int k = 0; k < index.operationCount(mod); k++) {
255  string opName = index.operationName(k, mod);
256  if (opName == name) {
257  if (&module == &mod) {
258  return true;
259  } else {
260  return false;
261  }
262  }
263  }
264  }
265  }
266  } catch (const Exception& e) {
267  return false;
268  }
269  // never should come here
270  assert(false);
271  return false;
272 }
273 
274 /**
275  * This function is called to clean up the static objects.
276  *
277  * This function should be called only when application is closed.
278  */
279 void
281 
282  if (index_ != NULL) {
283  delete index_;
284  index_ = NULL;
285  }
286 
287  if (serializer_ != NULL) {
288  delete serializer_;
289  serializer_ = NULL;
290  }
291 
292  if (memory_ != NULL) {
293  delete memory_;
294  memory_ = NULL;
295  }
296 }
297 
298 /**
299  * Returns the start point of the memory.
300  *
301  * @return Memory start point.
302  */
303 Word
305  return MEMORY_START;
306 }
307 
308 /**
309  * Returns the end point of the memory.
310  *
311  * @return Memory end point.
312  */
313 Word
315  return MEMORY_END;
316 }
OperationContainer::memory_
static IdealSRAM * memory_
Memory used througout the execution of the program.
Definition: OperationContainer.hh:117
OperationBehaviorProxy.hh
OperationIndex::addPath
void addPath(const std::string &path)
Definition: OperationIndex.cc:82
OperationContainer::DELETE_FUNCTION
static const std::string DELETE_FUNCTION
Operation behavior deletion function name.
Definition: OperationContainer.hh:93
InstructionAddress
UInt32 InstructionAddress
Definition: BaseType.hh:175
ObjectState::stringAttribute
std::string stringAttribute(const std::string &name) const
Definition: ObjectState.cc:249
OperationContainer::operationContext
static OperationContext & operationContext()
Definition: OperationContainer.cc:124
OperationContainer::MEMORY_END
static const Word MEMORY_END
End point of the memory.
Definition: OperationContainer.hh:98
PluginTools
Definition: PluginTools.hh:53
OperationContainer::operationSerializer
static OperationSerializer & operationSerializer()
Definition: OperationContainer.cc:111
OperationContainer::~OperationContainer
virtual ~OperationContainer()
Definition: OperationContainer.cc:85
OperationContext
Definition: OperationContext.hh:56
OperationContainer::module
static OperationModule & module(const std::string &path, const std::string &mod)
Definition: OperationContainer.cc:148
ObjectState
Definition: ObjectState.hh:59
OperationContainer::CREATE_FUNCTION
static const std::string CREATE_FUNCTION
Creation function name for operation behavior.
Definition: OperationContainer.hh:91
OperationBehaviorLoader.hh
OperationContainer::operationExists
static bool operationExists(const std::string &name)
Definition: OperationContainer.cc:219
SimValue
Definition: SimValue.hh:96
TCEString.hh
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
OperationContainer::index_
static OperationIndex * index_
Contains information of operations.
Definition: OperationContainer.hh:103
OperationIndex::pathCount
int pathCount() const
OperationContainer::context_
static OperationContext context_
Operation context used through the execution of the program.
Definition: OperationContainer.hh:107
OperationModule::propertiesModule
virtual std::string propertiesModule() const
Definition: OperationModule.cc:121
OperationContainer::memoryEnd
static Word memoryEnd()
Definition: OperationContainer.cc:314
Operation::setBehavior
virtual void setBehavior(OperationBehavior &behavior)
Definition: Operation.cc:378
OperationContainer::tools_
static PluginTools tools_
PluginTools used by OperationContainer;.
Definition: OperationContainer.hh:119
OperationBehaviorProxy
Definition: OperationBehaviorProxy.hh:58
OperationIndex.hh
OperationSerializer::readState
virtual ObjectState * readState()
Definition: OperationSerializer.cc:118
Application.hh
OperationContainer::destroy
static void destroy()
Definition: OperationContainer.cc:280
ObjectState.hh
OperationSerializer.hh
Operation::loadState
virtual void loadState(const ObjectState *state)
Definition: Operation.cc:480
OperationSerializer
Definition: OperationSerializer.hh:49
ObjectState::child
ObjectState * child(int index) const
Definition: ObjectState.cc:471
Operation.hh
Environment.hh
ObjectState::childCount
int childCount() const
OperationBehaviorLoader
Definition: OperationBehaviorLoader.hh:54
OperationContainer::programCounter_
static InstructionAddress programCounter_
Program counter.
Definition: OperationContainer.hh:109
OperationSerializer::setSourceFile
void setSourceFile(const std::string &filename)
Definition: OperationSerializer.cc:536
Exception
Definition: Exception.hh:54
PluginTools.hh
Environment::osalPaths
static std::vector< std::string > osalPaths()
Definition: Environment.cc:519
Operation
Definition: Operation.hh:59
OperationContainer::serializer_
static OperationSerializer * serializer_
Static instance of operation serializer.
Definition: OperationContainer.hh:105
OperationBehavior.hh
NullOperationBehavior::instance
static NullOperationBehavior & instance()
Definition: OperationBehavior.hh:95
OperationIndex::moduleCount
int moduleCount() const
OperationContainer::OperationContainer
OperationContainer()
Definition: OperationContainer.cc:79
OperationModule
Definition: OperationModule.hh:46
SimValue.hh
IdealSRAM.hh
OperationIndex::path
std::string path(int i) const
OperationContainer::MEMORY_START
static const Word MEMORY_START
Starting point of the memory.
Definition: OperationContainer.hh:96
OperationIndex
Definition: OperationIndex.hh:58
OperationContainer::memory
static Memory & memory()
Definition: OperationContainer.cc:134
OperationContainer::operation
static Operation * operation(const std::string &path, const std::string &module, const std::string &oper)
Definition: OperationContainer.cc:173
IdealSRAM
Definition: IdealSRAM.hh:55
OperationModule::name
virtual std::string name() const
Definition: OperationModule.cc:160
OperationContainer::MAUSIZE
static const Word MAUSIZE
MAU size of the memory.
Definition: OperationContainer.hh:100
NullOperationModule::instance
static NullOperationModule & instance()
OperationIndex::operationCount
int operationCount(const OperationModule &om)
Definition: OperationIndex.cc:363
OperationContainer::operationIndex
static OperationIndex & operationIndex()
Definition: OperationContainer.cc:94
OperationContainer::isEffective
static bool isEffective(OperationModule &module, const std::string &name)
Definition: OperationContainer.cc:244
OperationIndex::module
OperationModule & module(int i)
OperationIndex::operationName
std::string operationName(int i, const OperationModule &om)
Definition: OperationIndex.cc:337
OperationModule.hh
OperationContainer::returnAddress_
static SimValue returnAddress_
Return address.
Definition: OperationContainer.hh:111
OperationContainer::memoryStart
static Word memoryStart()
Definition: OperationContainer.cc:304
Memory
Definition: Memory.hh:74
OperationContainer.hh