OpenASIP 2.2
Loading...
Searching...
No Matches
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
44using 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 */
90bool
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 */
110void
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.
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 }
126
127 target_->createState(context);
128
131 }
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 */
144void
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 */
161bool
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 */
194void
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),
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 */
243void
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}
#define __func__
find Finds info of the inner loops in the false
OperationBehavior & importBehavior(const Operation &parent)
virtual void deleteState(OperationContext &context) const
OperationBehaviorProxy(Operation &targetOperation, OperationBehaviorLoader &loader, bool alwaysReloadBehavior=false)
std::set< OperationDAGBehavior * > cleanUs_
Clean up list for created OperationDAGBehaviors.
bool alreadyCreatingState_
Helpers variable to catch infinite recursive function call due to missing or undefined operation beha...
bool initialized_
Flag indicating whether proxy is initialized or not.
Operation * target_
Operation that owns this proxy;.
virtual bool canBeSimulated() const
OperationBehaviorLoader * loader_
Used to load behavior model for operation.
virtual bool simulateTrigger(SimValue **io, OperationContext &context) const
bool alwaysReloadBehavior_
If this is true, the behavior is always (re)loaded from the dynamic library or the DAG instead of loa...
virtual void createState(OperationContext &context) const
virtual void setBehavior(OperationBehavior &behavior)
Definition Operation.cc:378
virtual OperationDAG & dag(int index) const
Definition Operation.cc:148
virtual TCEString name() const
Definition Operation.cc:93
virtual void deleteState(OperationContext &context) const
Definition Operation.cc:601
virtual OperationBehavior & behavior() const
Definition Operation.cc:388
virtual void createState(OperationContext &context) const
Definition Operation.cc:590
virtual bool simulateTrigger(SimValue **, OperationContext &context) const
Definition Operation.cc:555
virtual int numberOfInputs() const
Definition Operation.cc:192
virtual bool canBeSimulated() const
Definition Operation.cc:612
virtual int dagCount() const
Definition Operation.cc:134
virtual int numberOfOutputs() const
Definition Operation.cc:202