OpenASIP  2.0
OperationBehaviorProxy.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 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 OperationBehaviorProxy.cc
26  *
27  * Definition of OperationBehaviorProxy class.
28  *
29  * @author Jussi Nykänen 2004 (nykanen-no.spam-cs.tut.fi)
30  * @author Mikael Lepistö 2007 (mikael.lepisto-no.spam-tut.fi)
31  * @note rating: yellow
32  * @note reviewed 19 August 2004 by pj, jn, ao, ac
33  */
34 
35 #include <string>
36 
38 #include "Application.hh"
39 #include "Operation.hh"
40 #include "SimValue.hh"
41 #include "OperationContext.hh"
43 
44 using std::string;
45 
46 /**
47  * Constructor.
48  *
49  * Registers the operation for which this proxy represents the behavior model.
50  * Registers the operation behavior loader that is used to find the appropriate
51  * behavior model of the operation.
52  *
53  * @param targetOperation Target operation for proxy.
54  * @param loader Operation behavior loader for the proxy.
55  */
57  Operation& targetOperation,
59  bool alwaysReloadBehavior) :
60  OperationBehavior(), target_(&targetOperation), loader_(&loader),
61  initialized_(false), alwaysReloadBehavior_(alwaysReloadBehavior),
62  alreadyCreatingState_(false) {
63 }
64 
65 /**
66  * Destructor.
67  *
68  * Operation behavior loader frees all loaded operation behavior models.
69  */
71  while (!cleanUs_.empty()) {
72  delete *(cleanUs_.begin());
73  cleanUs_.erase(cleanUs_.begin());
74  }
75 }
76 
77 /**
78  * Imports operation behavior model for the operation that owns this proxy
79  * and then delegates the work to that imported model.
80  *
81  * After initializing the operation with imported operation behavior model
82  * proxy has done its job.
83  *
84  * @param io The input and output operands.
85  * @param context The operation context.
86  * @return True if all values could be computed, false otherwise.
87  * @exception IllegalOperationBehavior If trigger command lacks return
88  * statement.
89  */
90 bool
92  SimValue** io,
93  OperationContext& context) const {
94 
96  bool retVal = target_->simulateTrigger(io, context);
98  return retVal;
99 }
100 
101 /**
102  * Imports operation behavior model for the operation that owns this proxy
103  * and then delegates the work to that imported model.
104  *
105  * After initializing the operation with imported operation behavior model
106  * proxy has done its job.
107  *
108  * @param context The operation context to add the state in.
109  */
110 void
113 
114  // If operation does not have operation behavior defined or behavior file
115  // is missing it causes infinite recursive call between
116  // Operation::createState() and this function. Catch such event and break
117  // out of it.
118  if (alreadyCreatingState_) {
119  throw IllegalOperationBehavior(__FILE__, __LINE__, __func__,
120  std::string("Error while loading operation behavior for "
121  "operation \"")
122  + target_->name()
123  + "\". Operation behavior is undefined or missing.");
124  }
125  alreadyCreatingState_ = true;
126 
127  target_->createState(context);
128 
129  if (alwaysReloadBehavior_) {
131  }
132  alreadyCreatingState_ = true;
133 }
134 
135 /**
136  * Imports operation behavior model for the operation that owns this proxy
137  * and then delegates the work to that imported model.
138  *
139  * After initializing the operation with imported operation behavior model
140  * proxy has done its job.
141  *
142  * @param context The operation context to delete the state from.
143  */
144 void
147  target_->deleteState(context);
149 }
150 
151 
152 /**
153  * Imports operation behavior model for the operation that owns this proxy
154  * and then delegates the work to that imported model.
155  *
156  * After initializing the operation with imported operation behavior model
157  * proxy has done its job.
158  *
159  * @return True if this operation has behavior, or dag which is
160  */
161 bool
163  try {
165  } catch (Exception&) {
167  return false;
168  }
169  // if initialization was not success
170  if (&target_->behavior() == this) {
172  return false;
173  } else {
174  bool retVal = target_->canBeSimulated();
176  return retVal;
177  }
178 }
179 
180 /**
181  * Initializes the operation that owns this proxy with an operation behavior
182  * model.
183  *
184  * Proxy replaces itself with the imported operation behavior model. After that
185  * proxy methods are never called.
186  *
187  * This method is executed only once. After that, this function does nothing.
188  * This function may abort the program, if error condition occurs while
189  * operation behavior model is imported.
190  *
191  * @exception DynamicLibraryException Leaked from importBehavior in case
192  * behavior file was invalid.
193  */
194 void
196 
197  if (initialized_) {
198  return;
199  }
200 
201  try {
203  target_->setBehavior(ob);
204  return;
205  } catch (FileNotFound&) {
206  } catch (SymbolNotFound& e) {
207  // try loading behavior from DAG
208  if (target_->dagCount() == 0) {
209  throw e;
210  }
211  } catch (Exception& e) {
212  throw e;
213  }
214 
215  // no compiled behavior in .opb found
216 
217  // if there is DAG to create behavior model...
218  if (target_->dagCount() == 0) {
219  initialized_ = true;
220  return;
221  }
222 
223  // there was not behavior plugin so let's use dag for simulation
224  OperationDAGBehavior* behavior =
226  target_->dag(0),
227  target_->numberOfInputs() +
229  *target_);
230  target_->setBehavior(*behavior);
231 
232  // add for cleanup
233  cleanUs_.insert(behavior);
234 
235  initialized_ = true;
236 }
237 
238 
239 /**
240  * Uninitializes the behavior description from the operation so it will be
241  * reloaded again when a behavior method is called the next time.
242  */
243 void
245  while (!cleanUs_.empty()) {
246  OperationBehavior* behavior = *cleanUs_.begin();
247  delete behavior;
248  cleanUs_.erase(cleanUs_.begin());
249  }
250  target_->setBehavior(const_cast<OperationBehaviorProxy&>(*this));
252  initialized_ = false;
253 }
OperationBehaviorLoader::importBehavior
OperationBehavior & importBehavior(const Operation &parent)
Definition: OperationBehaviorLoader.cc:91
OperationBehaviorProxy.hh
OperationBehaviorProxy::alwaysReloadBehavior_
bool alwaysReloadBehavior_
If this is true, the behavior is always (re)loaded from the dynamic library or the DAG instead of loa...
Definition: OperationBehaviorProxy.hh:98
FileNotFound
Definition: Exception.hh:224
OperationDAGBehavior
Definition: OperationDAGBehavior.hh:54
OperationContext
Definition: OperationContext.hh:56
OperationBehaviorProxy::~OperationBehaviorProxy
virtual ~OperationBehaviorProxy()
Definition: OperationBehaviorProxy.cc:70
Operation::numberOfInputs
virtual int numberOfInputs() const
Definition: Operation.cc:192
Operation::canBeSimulated
virtual bool canBeSimulated() const
Definition: Operation.cc:612
IllegalOperationBehavior
Definition: Exception.hh:844
OperationBehaviorLoader.hh
Operation::name
virtual TCEString name() const
Definition: Operation.cc:93
OperationBehaviorProxy::target_
Operation * target_
Operation that owns this proxy;.
Definition: OperationBehaviorProxy.hh:88
OperationBehaviorProxy::canBeSimulated
virtual bool canBeSimulated() const
Definition: OperationBehaviorProxy.cc:162
SimValue
Definition: SimValue.hh:96
OperationBehaviorProxy::initialized_
bool initialized_
Flag indicating whether proxy is initialized or not.
Definition: OperationBehaviorProxy.hh:92
Operation::setBehavior
virtual void setBehavior(OperationBehavior &behavior)
Definition: Operation.cc:378
SymbolNotFound
Definition: Exception.hh:623
OperationBehaviorProxy
Definition: OperationBehaviorProxy.hh:58
Application.hh
OperationBehaviorProxy::initializeBehavior
void initializeBehavior() const
Definition: OperationBehaviorProxy.cc:195
__func__
#define __func__
Definition: Application.hh:67
Operation.hh
OperationBehaviorLoader
Definition: OperationBehaviorLoader.hh:54
Exception
Definition: Exception.hh:54
Operation::deleteState
virtual void deleteState(OperationContext &context) const
Definition: Operation.cc:601
Operation
Definition: Operation.hh:59
OperationBehaviorProxy::uninitializeBehavior
void uninitializeBehavior() const
Definition: OperationBehaviorProxy.cc:244
Operation::createState
virtual void createState(OperationContext &context) const
Definition: Operation.cc:590
Operation::dagCount
virtual int dagCount() const
Definition: Operation.cc:134
OperationBehavior
Definition: OperationBehavior.hh:53
false
find Finds info of the inner loops in the false
Definition: InnerLoopFinder.cc:81
SimValue.hh
OperationBehaviorProxy::deleteState
virtual void deleteState(OperationContext &context) const
Definition: OperationBehaviorProxy.cc:145
OperationBehaviorLoader::freeBehavior
void freeBehavior()
Definition: OperationBehaviorLoader.cc:136
Operation::simulateTrigger
virtual bool simulateTrigger(SimValue **, OperationContext &context) const
Definition: Operation.cc:555
OperationBehaviorProxy::simulateTrigger
virtual bool simulateTrigger(SimValue **io, OperationContext &context) const
Definition: OperationBehaviorProxy.cc:91
Operation::behavior
virtual OperationBehavior & behavior() const
Definition: Operation.cc:388
OperationBehaviorProxy::alreadyCreatingState_
bool alreadyCreatingState_
Helpers variable to catch infinite recursive function call due to missing or undefined operation beha...
Definition: OperationBehaviorProxy.hh:101
OperationBehaviorProxy::cleanUs_
std::set< OperationDAGBehavior * > cleanUs_
Clean up list for created OperationDAGBehaviors.
Definition: OperationBehaviorProxy.hh:94
OperationBehaviorProxy::createState
virtual void createState(OperationContext &context) const
Definition: OperationBehaviorProxy.cc:111
Operation::numberOfOutputs
virtual int numberOfOutputs() const
Definition: Operation.cc:202
OperationBehaviorProxy::loader_
OperationBehaviorLoader * loader_
Used to load behavior model for operation.
Definition: OperationBehaviorProxy.hh:90
OperationContext.hh
OperationBehaviorProxy::OperationBehaviorProxy
OperationBehaviorProxy(Operation &targetOperation, OperationBehaviorLoader &loader, bool alwaysReloadBehavior=false)
Definition: OperationBehaviorProxy.cc:56
Operation::dag
virtual OperationDAG & dag(int index) const
Definition: Operation.cc:148